[llvm] 598f152 - [ConstraintElim] Fix integer overflow when negating constraint (#210627)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Jul 19 14:52:02 PDT 2026
Author: Florian Hahn
Date: 2026-07-19T21:51:57Z
New Revision: 598f15297528855682dcf381575a9c14a7faabc3
URL: https://github.com/llvm/llvm-project/commit/598f15297528855682dcf381575a9c14a7faabc3
DIFF: https://github.com/llvm/llvm-project/commit/598f15297528855682dcf381575a9c14a7faabc3.diff
LOG: [ConstraintElim] Fix integer overflow when negating constraint (#210627)
Negating a coefficient (e.g. INT64_MIN) can overflow; skip adding the
inverted constraint if it wraps.
Fixes an UBSan failure for the added test.
Added:
Modified:
llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index cdd15f04730ba..c0fb704306d36 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1816,7 +1816,8 @@ void ConstraintInfo::addFactImpl(CmpInst::Predicate Pred, Value *A, Value *B,
if (R.isEq()) {
// Also add the inverted constraint for equality constraints.
for (auto &Coeff : R.Coefficients)
- Coeff *= -1;
+ if (MulOverflow(Coeff, int64_t(-1), Coeff))
+ return;
CSToUse.addVariableRowFill(R.Coefficients);
DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
diff --git a/llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll b/llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll
index 008e2ba222fce..78a6900268bff 100644
--- a/llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll
+++ b/llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll
@@ -104,3 +104,29 @@ entry:
%c = icmp ult i64 %i4, %lim
ret i1 %c
}
+
+define i1 @eq_inverted_normal_coeff(i64 %a, i64 %b) {
+; CHECK-LABEL: define i1 @eq_inverted_normal_coeff(
+; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; CHECK: then:
+; CHECK-NEXT: [[RES:%.*]] = and i1 true, true
+; CHECK-NEXT: ret i1 [[RES]]
+; CHECK: else:
+; CHECK-NEXT: ret i1 false
+;
+entry:
+ %cmp = icmp eq i64 %a, %b
+ br i1 %cmp, label %then, label %else
+
+then:
+ %t.1 = icmp uge i64 %a, %b
+ %t.2 = icmp ule i64 %a, %b
+ %res = and i1 %t.1, %t.2
+ ret i1 %res
+
+else:
+ ret i1 false
+}
More information about the llvm-commits
mailing list