[llvm] [InstCombine] Reduce multiplicands of even numbers when a shift is involved (PR #92475)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 9 15:37:54 PDT 2024


================
@@ -1712,6 +1727,36 @@ Instruction *InstCombinerImpl::visitAShr(BinaryOperator &I) {
       return BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
     }
 
+    if (match(Op0, m_OneUse(m_NSWMul(m_Value(X), m_APInt(ShOp1))))) {
+      unsigned CommonZeros = std::min(ShOp1->countr_zero(), ShAmt);
+      if (CommonZeros != 0) {
+        APInt NewMulC = ShOp1->ashr(CommonZeros);
+        unsigned NewShAmtC = ShAmt - CommonZeros;
+        // if c is divisible by (1 << ShAmtC):
+        // ashr (mul nsw x, MulC), ShAmtC -> mul nsw x, (MulC >> ShAmtC)
+        if (NewShAmtC == 0) {
+          auto *NewMul =
+              BinaryOperator::CreateNSWMul(X, ConstantInt::get(Ty, NewMulC));
+          NewMul->setHasNoUnsignedWrap(
+              cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap());
+          return NewMul;
+        }
----------------
goldsteinn wrote:

Likewise, would just drop the `NewShAmtC == 0` case here.

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


More information about the llvm-commits mailing list