[llvm] [InstCombine] Support division of numbers that can be converted to a shift (PR #88220)

via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 11 08:51:14 PDT 2024


================
@@ -1149,6 +1149,39 @@ Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
         Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap());
         return Mul;
       }
+
+      // If we can reduce these functions so they can factor out to a shift or
+      // something similar (i.e Reduce (X * 150/100) -> (X * 3) >> 1)
+      //
+      // (X * C1)/C2 -> X * (C1/C3) >> log2(C2/C3) where C3 divides exactly C1
+      // and C2 and C2/C3 is a power of 2.
+      // This WILL require NUW, and if it is sdiv then it also requires NSW
+      // TODO: Can we expand this for negative powers of 2? Is it even
+      // profitable or worth it?
+      if (Op0->hasOneUse()) {
----------------
goldsteinn wrote:

The comment about requring NUW/NSW seems out of date...
can you also include your proofs in the desc/commit message.
Its a bit hard to find them once you have resolved the comment you posted them in.

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


More information about the llvm-commits mailing list