[llvm] [InstCombine] Fold `X udiv Y` to `X lshr cttz(Y)` if Y is a power of 2 (PR #121386)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 9 23:48:57 PST 2025
================
@@ -1623,14 +1623,27 @@ Instruction *InstCombinerImpl::visitUDiv(BinaryOperator &I) {
return Lshr;
}
- // Op1 udiv Op2 -> Op1 lshr log2(Op2), if log2() folds away.
- if (takeLog2(Builder, Op1, /*Depth*/ 0, /*AssumeNonZero*/ true,
- /*DoFold*/ false)) {
- Value *Res = takeLog2(Builder, Op1, /*Depth*/ 0,
- /*AssumeNonZero*/ true, /*DoFold*/ true);
+ auto GetShiftableDenom = [&](Value *Denom) -> Value * {
+ // Op0 udiv Op1 -> Op0 lshr log2(Op1), if log2() folds away.
+ if (takeLog2(Builder, Denom, /*Depth=*/0, /*AssumeNonZero=*/true,
+ /*DoFold=*/false))
+ return takeLog2(Builder, Denom, /*Depth=*/0, /*AssumeNonZero=*/true,
+ /*DoFold=*/true);
----------------
dtcxzyw wrote:
> If you are just going to return the result, you can just do:
>
> ```
> if (auto * R = takeLog2(..., /*DoFold=*/true)) return R;
> ...
> ```
Temporary instructions may be created when `takeLog2` fails. We need the check with `DoFold=false` to ensure the second call must succeed.
https://github.com/llvm/llvm-project/pull/121386
More information about the llvm-commits
mailing list