[llvm] [InstCombine] Fold the `log2_ceil` idiom (PR #76661)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 6 10:04:08 PST 2024


================
@@ -1730,6 +1730,28 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
         I, Builder.CreateIntrinsic(Intrinsic::ctpop, {I.getType()},
                                    {Builder.CreateOr(A, B)}));
 
+  // Fold the log2_ceil idiom:
+  // zext(ctpop(A, true) >u/!= 1) + (ctlz(A) ^ (BW - 1)) --> BW - ctlz(A - 1)
+  // TODO: Add one-use checks?
----------------
dtcxzyw wrote:

Done.

https://github.com/llvm/llvm-project/pull/76661


More information about the llvm-commits mailing list