[llvm] [InstCombine] Fold mul (shr exact (X, N)), 2^N + 1 -> add (X , shr exact (X, N)) (PR #112407)

via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 8 13:00:17 PST 2025


================
@@ -261,6 +261,37 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
     }
   }
 
+  // mul (shr exact X, N), (2^N + 1) -> add (X, shr exact (X, N))
+  {
+    Value *NewOp;
+    const APInt *ShiftC;
+    const APInt *MulAP;
+    if (match(&I, m_Mul(m_Exact(m_Shr(m_Value(NewOp), m_APInt(ShiftC))),
+                        m_APInt(MulAP)))) {
+      if (BitWidth > 2 && (*MulAP - 1).isPowerOf2() &&
+          *ShiftC == MulAP->logBase2()) {
+        Value *BinOp = Op0;
+        BinaryOperator *OpBO = cast<BinaryOperator>(Op0);
+        if (!isGuaranteedNotToBeUndef(NewOp, &AC, &I, &DT))
+          NewOp = Builder.CreateFreeze(NewOp, NewOp->getName() + ".fr");
----------------
AZero13 wrote:

Incorrect https://alive2.llvm.org/ce/z/6cCCnP



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


More information about the llvm-commits mailing list