aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib
diff options
context:
space:
mode:
authorYingwei Zheng <dtcxzyw2333@gmail.com>2023-11-17 21:50:21 +0800
committerGitHub <noreply@github.com>2023-11-17 21:50:21 +0800
commit26ce3e4239150ccc3328c43e4b47264989c07411 (patch)
tree8ab9ae3c0a7b26163bcf2bc27d62eff72cbea0c2 /llvm/lib
parentf049395fc8d6d8bbbc711c7a2ce293210c580240 (diff)
downloadllvm-26ce3e4239150ccc3328c43e4b47264989c07411.zip
llvm-26ce3e4239150ccc3328c43e4b47264989c07411.tar.gz
llvm-26ce3e4239150ccc3328c43e4b47264989c07411.tar.bz2
[InstCombine] Preserve NSW flags for `lshr (mul nuw X, C1), C2 -> mul nuw nsw X, (C1 >> C2)` (#72625)
Alive2: https://alive2.llvm.org/ce/z/TU_V9M This missed optimization is discovered with the help of https://github.com/AliveToolkit/alive2/pull/962.
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp7
1 files changed, 4 insertions, 3 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 9d4a2cc..f72546d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -1435,12 +1435,13 @@ 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());
+ assert(ShAmtC != 0 &&
+ "lshr X, 0 should be handled by simplifyLShrInst.");
+ NewMul->setHasNoSignedWrap(true);
return NewMul;
}
}