Skip to content

Commit 3c037b7

Browse files
authored
[InstCombine] When -A + B both have nsw flag, set nsw flag. (#72127)
Fixes #72119 https://alive2.llvm.org/ce/z/5f_QuC
1 parent 1609f1c commit 3c037b7

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,11 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
14871487
return BinaryOperator::CreateNeg(Builder.CreateAdd(A, B));
14881488

14891489
// -A + B --> B - A
1490-
return BinaryOperator::CreateSub(RHS, A);
1490+
auto *Sub = BinaryOperator::CreateSub(RHS, A);
1491+
auto *OB0 = dyn_cast<OverflowingBinaryOperator>(LHS);
1492+
Sub->setHasNoSignedWrap(I.hasNoSignedWrap() && OB0->hasNoSignedWrap());
1493+
1494+
return Sub;
14911495
}
14921496

14931497
// A + -B --> A - B

llvm/test/Transforms/InstCombine/add.ll

+30
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,36 @@ define i32 @test5(i32 %A, i32 %B) {
120120
ret i32 %D
121121
}
122122

123+
define i32 @test5_both_nsw(i32 %A, i32 %B) {
124+
; CHECK-LABEL: @test5_both_nsw(
125+
; CHECK-NEXT: [[D:%.*]] = sub nsw i32 [[B:%.*]], [[A:%.*]]
126+
; CHECK-NEXT: ret i32 [[D]]
127+
;
128+
%C = sub nsw i32 0, %A
129+
%D = add nsw i32 %C, %B
130+
ret i32 %D
131+
}
132+
133+
define i32 @test5_neg_nsw(i32 %A, i32 %B) {
134+
; CHECK-LABEL: @test5_neg_nsw(
135+
; CHECK-NEXT: [[D:%.*]] = sub i32 [[B:%.*]], [[A:%.*]]
136+
; CHECK-NEXT: ret i32 [[D]]
137+
;
138+
%C = sub nsw i32 0, %A
139+
%D = add i32 %C, %B
140+
ret i32 %D
141+
}
142+
143+
define i32 @test5_add_nsw(i32 %A, i32 %B) {
144+
; CHECK-LABEL: @test5_add_nsw(
145+
; CHECK-NEXT: [[D:%.*]] = sub i32 [[B:%.*]], [[A:%.*]]
146+
; CHECK-NEXT: ret i32 [[D]]
147+
;
148+
%C = sub i32 0, %A
149+
%D = add nsw i32 %C, %B
150+
ret i32 %D
151+
}
152+
123153
define <2 x i8> @neg_op0_vec_undef_elt(<2 x i8> %a, <2 x i8> %b) {
124154
; CHECK-LABEL: @neg_op0_vec_undef_elt(
125155
; CHECK-NEXT: [[R:%.*]] = sub <2 x i8> [[B:%.*]], [[A:%.*]]

llvm/test/Transforms/InstCombine/pr14365.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ define i32 @test1(i32 %a0) {
3131
; CHECK-LABEL: @test1(
3232
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[A0:%.*]], 1
3333
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], 1431655765
34-
; CHECK-NEXT: [[TMP3:%.*]] = sub i32 [[A0]], [[TMP2]]
34+
; CHECK-NEXT: [[TMP3:%.*]] = sub nsw i32 [[A0]], [[TMP2]]
3535
; CHECK-NEXT: ret i32 [[TMP3]]
3636
;
3737
%1 = ashr i32 %a0, 1
@@ -46,7 +46,7 @@ define <4 x i32> @test1_vec(<4 x i32> %a0) {
4646
; CHECK-LABEL: @test1_vec(
4747
; CHECK-NEXT: [[TMP1:%.*]] = lshr <4 x i32> [[A0:%.*]], <i32 1, i32 1, i32 1, i32 1>
4848
; CHECK-NEXT: [[TMP2:%.*]] = and <4 x i32> [[TMP1]], <i32 1431655765, i32 1431655765, i32 1431655765, i32 1431655765>
49-
; CHECK-NEXT: [[TMP3:%.*]] = sub <4 x i32> [[A0]], [[TMP2]]
49+
; CHECK-NEXT: [[TMP3:%.*]] = sub nsw <4 x i32> [[A0]], [[TMP2]]
5050
; CHECK-NEXT: ret <4 x i32> [[TMP3]]
5151
;
5252
%1 = ashr <4 x i32> %a0, <i32 1, i32 1, i32 1, i32 1>

0 commit comments

Comments
 (0)