[llvm] [ConstraintElim] Fix integer overflow when negating constraint (PR #210627)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Sun Jul 19 14:14:44 PDT 2026
https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/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.
>From 57434ec24f33284631ae4374a9f04da2679e7243 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sun, 19 Jul 2026 22:04:04 +0100
Subject: [PATCH] [ConstraintElim] Fix integer overflow when negating
constraint
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.
---
.../Scalar/ConstraintElimination.cpp | 3 ++-
.../constraint-overflow.ll | 26 +++++++++++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)
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