[llvm] [InstCombine] Reducing multiplications means we can have nsw and nuw tags (PR #92980)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue May 21 19:03:18 PDT 2024


================
@@ -1273,17 +1277,37 @@ Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
   }
 
   // (X << Z) / (X * Y) -> (1 << Z) / Y
-  // TODO: Handle sdiv.
   if (!IsSigned && Op1->hasOneUse() &&
       match(Op0, m_NUWShl(m_Value(X), m_Value(Z))) &&
       match(Op1, m_c_Mul(m_Specific(X), m_Value(Y))))
     if (cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap()) {
       Instruction *NewDiv = BinaryOperator::CreateUDiv(
-          Builder.CreateShl(ConstantInt::get(Ty, 1), Z, "", /*NUW*/ true), Y);
+          Builder.CreateShl(
+              ConstantInt::get(Ty, 1), Z, "", /*NUW*/ true,
+              /*NSW*/ cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap()),
+          Y);
       NewDiv->setIsExact(I.isExact());
       return NewDiv;
     }
 
+  // (X << Z) / (X * Y) -> (1 << Z) / Y
----------------
topperc wrote:

This is a new transform. Your title does not mention any new transforms.

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


More information about the llvm-commits mailing list