Skip to content

Commit fea0bde

Browse files
committed
[InstCombine] Add convenience helper tryGetLog2; NFC
This just encapsulates the common pattern: ``` if (takeLog2(..., /*DoFold=*/false)) { Value * Log2 = takeLog2(..., /*DoFold=*/true); ... } ```
1 parent b21edc4 commit fea0bde

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
201201
// takeLog2 will succeed, otherwise it may create stray instructions.
202202
Value *takeLog2(Value *Op, unsigned Depth, bool AssumeNonZero, bool DoFold);
203203

204+
Value *tryGetLog2(Value *Op, bool AssumeNonZero) {
205+
if (takeLog2(Op, /*Depth=*/0, AssumeNonZero, /*DoFold=*/false))
206+
return takeLog2(Op, /*Depth=*/0, AssumeNonZero, /*DoFold=*/true);
207+
return nullptr;
208+
}
209+
204210
/// Return nonnull value if V is free to invert under the condition of
205211
/// WillInvertAllUses.
206212
/// If Builder is nonnull, it will return a simplified ~V.

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -528,19 +528,13 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
528528
// (shl Op1, Log2(Op0))
529529
// if Log2(Op1) folds away ->
530530
// (shl Op0, Log2(Op1))
531-
if (takeLog2(Op0, /*Depth*/ 0, /*AssumeNonZero*/ false,
532-
/*DoFold*/ false)) {
533-
Value *Res = takeLog2(Op0, /*Depth*/ 0, /*AssumeNonZero*/ false,
534-
/*DoFold*/ true);
531+
if (Value *Res = tryGetLog2(Op0, /*AssumeNonZero=*/false)) {
535532
BinaryOperator *Shl = BinaryOperator::CreateShl(Op1, Res);
536533
// We can only propegate nuw flag.
537534
Shl->setHasNoUnsignedWrap(HasNUW);
538535
return Shl;
539536
}
540-
if (takeLog2(Op1, /*Depth*/ 0, /*AssumeNonZero*/ false,
541-
/*DoFold*/ false)) {
542-
Value *Res = takeLog2(Op1, /*Depth*/ 0, /*AssumeNonZero*/ false,
543-
/*DoFold*/ true);
537+
if (Value *Res = tryGetLog2(Op0, /*AssumeNonZero=*/false)) {
544538
BinaryOperator *Shl = BinaryOperator::CreateShl(Op0, Res);
545539
// We can only propegate nuw flag.
546540
Shl->setHasNoUnsignedWrap(HasNUW);
@@ -1506,13 +1500,9 @@ Instruction *InstCombinerImpl::visitUDiv(BinaryOperator &I) {
15061500
}
15071501

15081502
// Op1 udiv Op2 -> Op1 lshr log2(Op2), if log2() folds away.
1509-
if (takeLog2(Op1, /*Depth*/ 0, /*AssumeNonZero*/ true,
1510-
/*DoFold*/ false)) {
1511-
Value *Res = takeLog2(Op1, /*Depth*/ 0,
1512-
/*AssumeNonZero*/ true, /*DoFold*/ true);
1503+
if (Value *Res = tryGetLog2(Op1, /*AssumeNonZero=*/true))
15131504
return replaceInstUsesWith(
15141505
I, Builder.CreateLShr(Op0, Res, I.getName(), I.isExact()));
1515-
}
15161506

15171507
return nullptr;
15181508
}

0 commit comments

Comments
 (0)