[llvm] [InstCombine] Fold `X udiv Y` to `X lshr cttz(Y)` if Y is a power of 2 (PR #121386)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 9 08:06:20 PST 2025


================
@@ -1632,6 +1632,16 @@ Instruction *InstCombinerImpl::visitUDiv(BinaryOperator &I) {
         I, Builder.CreateLShr(Op0, Res, I.getName(), I.isExact()));
   }
 
+  // Op0 udiv Op1 -> Op0 lshr cttz(Op1), if Op1 is a power of 2.
+  if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, /*Depth*/ 0, &I)) {
+    // This will increase instruction count but it's okay
+    // since bitwise operations are substantially faster than
+    // division.
----------------
veera-sivarajan wrote:

> 2 extra instructions might not be worth it. 

Could you please explain why?

Doesn't the reduced cycle count outweigh the increased instruction count?


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


More information about the llvm-commits mailing list