Skip to content

Commit ae13df6

Browse files
committed
Don't attempt to add 'nsw' when intermediate instructions had no such guarantee.
llvm-svn: 137572
1 parent de49278 commit ae13df6

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ static bool MaintainNoSignedWrap(BinaryOperator &I, Value *B, Value *C) {
146146
return !Overflow;
147147
}
148148

149-
150149
/// SimplifyAssociativeOrCommutative - This performs a few simplifications for
151150
/// operators which are associative or commutative:
152151
//
@@ -197,7 +196,10 @@ bool InstCombiner::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
197196
I.setOperand(1, V);
198197
// Conservatively clear the optional flags, since they may not be
199198
// preserved by the reassociation.
200-
if (MaintainNoSignedWrap(I, B, C)) {
199+
if (MaintainNoSignedWrap(I, B, C) &&
200+
(!Op0 || (isa<BinaryOperator>(Op0) && Op0->hasNoSignedWrap()))) {
201+
// Note: this is only valid because SimplifyBinOp doesn't look at
202+
// the operands to Op0.
201203
I.clearSubclassOptionalData();
202204
I.setHasNoSignedWrap(true);
203205
} else {
@@ -292,10 +294,11 @@ bool InstCombiner::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
292294
I.setOperand(1, Folded);
293295
// Conservatively clear the optional flags, since they may not be
294296
// preserved by the reassociation.
295-
if (MaintainNoSignedWrap(I, C1, C2)) {
297+
if (MaintainNoSignedWrap(I, C1, C2) && Op0->hasNoSignedWrap() &&
298+
Op1->hasNoSignedWrap()) {
299+
New->setHasNoSignedWrap(true);
296300
I.clearSubclassOptionalData();
297301
I.setHasNoSignedWrap(true);
298-
New->setHasNoSignedWrap(true);
299302
} else {
300303
I.clearSubclassOptionalData();
301304
}

llvm/test/Transforms/InstCombine/nsw.ll

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,38 @@ define i32 @preserve1(i32 %x) nounwind {
4646
ret i32 %add3
4747
}
4848

49+
; CHECK: @preserve2
50+
; CHECK: add nsw i8 %A, %B
51+
; CHECK: add nsw i8
52+
define i8 @preserve2(i8 %A, i8 %B) nounwind {
53+
%x = add nsw i8 %A, 10
54+
%y = add nsw i8 %B, 10
55+
%add = add nsw i8 %x, %y
56+
ret i8 %add
57+
}
58+
4959
; CHECK: @nopreserve1
5060
; CHECK: add i8 %x, -126
5161
define i8 @nopreserve1(i8 %x) nounwind {
5262
%add = add nsw i8 %x, 127
5363
%add3 = add nsw i8 %add, 3
5464
ret i8 %add3
55-
}
65+
}
66+
67+
; CHECK: @nopreserve2
68+
; CHECK: add i8 %x, 3
69+
define i8 @nopreserve2(i8 %x) nounwind {
70+
%add = add i8 %x, 1
71+
%add3 = add nsw i8 %add, 2
72+
ret i8 %add3
73+
}
74+
75+
; CHECK: @nopreserve3
76+
; CHECK: add i8 %A, %B
77+
; CHECK: add i8
78+
define i8 @nopreserve3(i8 %A, i8 %B) nounwind {
79+
%x = add i8 %A, 10
80+
%y = add i8 %B, 10
81+
%add = add nsw i8 %x, %y
82+
ret i8 %add
83+
}

0 commit comments

Comments
 (0)