[llvm] [InstCombine] Preserve NSW flags for `lshr (mul nuw X, C1), C2 -> mul nuw nsw X, (C1 >> C2)` (PR #72625)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 17 01:11:27 PST 2023


================
@@ -1435,12 +1435,11 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
       if (Op0->hasOneUse()) {
         APInt NewMulC = MulC->lshr(ShAmtC);
         // if c is divisible by (1 << ShAmtC):
-        // lshr (mul nuw x, MulC), ShAmtC -> mul nuw x, (MulC >> ShAmtC)
+        // lshr (mul nuw x, MulC), ShAmtC -> mul nuw nsw x, (MulC >> ShAmtC)
         if (MulC->eq(NewMulC.shl(ShAmtC))) {
           auto *NewMul =
               BinaryOperator::CreateNUWMul(X, ConstantInt::get(Ty, NewMulC));
-          BinaryOperator *OrigMul = cast<BinaryOperator>(Op0);
-          NewMul->setHasNoSignedWrap(OrigMul->hasNoSignedWrap());
+          NewMul->setHasNoSignedWrap(true);
----------------
nikic wrote:

Add `assert(ShAmt != 0)`, as this seems to be pre-condition?

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


More information about the llvm-commits mailing list