Skip to content

Commit 45ff287

Browse files
committed
[ConstraintSystem] Fix signed overflow in negate.
Use AddOverflow for potentially overflowing addition to fixed signed integer overflow. Compile-time impact is in the noise https://llvm-compile-time-tracker.com/compare.php?from=bfb26202e05ee2932b4368b5fca607df01e8247f&to=195b0707148b567c674235e59712458e7ce1bb0e&stat=instructions:u
1 parent fef54d0 commit 45ff287

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

llvm/include/llvm/Analysis/ConstraintSystem.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ class ConstraintSystem {
113113
static SmallVector<int64_t, 8> negate(SmallVector<int64_t, 8> R) {
114114
// The negated constraint R is obtained by multiplying by -1 and adding 1 to
115115
// the constant.
116-
R[0] += 1;
116+
if (AddOverflow(R[0], int64_t(1), R[0]))
117+
return {};
118+
117119
return negateOrEqual(R);
118120
}
119121

llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,17 @@ exit:
3838
}
3939

4040
declare void @llvm.assume(i1)
41+
42+
define i1 @negate_overflow_add_1(i64 %x) {
43+
; CHECK-LABEL: define i1 @negate_overflow_add_1(
44+
; CHECK-SAME: i64 [[X:%.*]]) {
45+
; CHECK-NEXT: entry:
46+
; CHECK-NEXT: [[SUB:%.*]] = add nsw i64 [[X]], -9223372036854775807
47+
; CHECK-NEXT: [[C:%.*]] = icmp slt i64 0, [[SUB]]
48+
; CHECK-NEXT: ret i1 [[C]]
49+
;
50+
entry:
51+
%sub = add nsw i64 %x, -9223372036854775807
52+
%c = icmp slt i64 0, %sub
53+
ret i1 %c
54+
}

0 commit comments

Comments
 (0)