Skip to content

Commit 0a0181d

Browse files
committed
[ConstraintElim] Fix integer overflow in ConstraintSystem::negate.
This fixes another integer overflow that was exposed by a variant of the test case from #62226.
1 parent fed28ad commit 0a0181d

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

llvm/include/llvm/Analysis/ConstraintSystem.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/ADT/ArrayRef.h"
1414
#include "llvm/ADT/DenseMap.h"
1515
#include "llvm/ADT/SmallVector.h"
16+
#include "llvm/Support/MathExtras.h"
1617

1718
#include <string>
1819

@@ -122,7 +123,8 @@ class ConstraintSystem {
122123
// the constant.
123124
R[0] += 1;
124125
for (auto &C : R)
125-
C *= -1;
126+
if (MulOverflow(C, int64_t(-1), C))
127+
return {};
126128
return R;
127129
}
128130

llvm/lib/Analysis/ConstraintSystem.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ bool ConstraintSystem::isConditionImplied(SmallVector<int64_t, 8> R) const {
206206
// If there is no solution with the negation of R added to the system, the
207207
// condition must hold based on the existing constraints.
208208
R = ConstraintSystem::negate(R);
209+
if (R.empty())
210+
return false;
209211

210212
auto NewSystem = *this;
211213
NewSystem.addVariableRow(R);

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,8 @@ static bool checkAndReplaceCondition(
961961
NumCondsRemoved++;
962962
Changed = true;
963963
}
964-
if (CSToUse.isConditionImplied(ConstraintSystem::negate(R.Coefficients))) {
964+
auto Negated = ConstraintSystem::negate(R.Coefficients);
965+
if (!Negated.empty() && CSToUse.isConditionImplied(Negated)) {
965966
if (!DebugCounter::shouldExecute(EliminatedCounter))
966967
return false;
967968

llvm/test/Transforms/ConstraintElimination/overflows.ll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ bb:
1717
ret i1 %icmp
1818
}
1919

20+
define i1 @test_overflow_in_negate_constraint(i8 %x, i64 %y) {
21+
bb:
22+
%zext = zext i8 %x to i64
23+
%shl = shl nuw nsw i64 %zext, 63
24+
%icmp = icmp uge i64 %y, %shl
25+
ret i1 %icmp
26+
}
2027

0 commit comments

Comments
 (0)