[llvm] [InstCombine] When -A + B both have nsw flag, set nsw flag. (PR #72127)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 13 17:08:46 PST 2023
================
@@ -1487,7 +1487,11 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
return BinaryOperator::CreateNeg(Builder.CreateAdd(A, B));
// -A + B --> B - A
- return BinaryOperator::CreateSub(RHS, A);
+ auto *Sub = BinaryOperator::CreateSub(RHS, A);
+ auto *OB0 = dyn_cast<OverflowingBinaryOperator>(LHS);
+ Sub->setHasNoSignedWrap(I.hasNoSignedWrap() && OB0->hasNoSignedWrap());
----------------
Z572 wrote:
https://alive2.llvm.org/ce/z/7nnL25
if only `I.hasNoSignedWrap()`
```
define i32 @test5_add_nsw(i32 %A, i32 %B) {
%C = sub i32 0, %A
%D = add nsw i32 %C, %B
ret i32 %D
}
```
-->
```
define i32 @test5_add_nsw(i32 %A, i32 %B) {
%D = sub nsw i32 %B, %A
ret i32 %D
}
```
is error
https://github.com/llvm/llvm-project/pull/72127
More information about the llvm-commits
mailing list