[llvm] [ConstraintElim] Avoid overflow when negating offsets (PR #218580)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 24 21:01:22 PDT 2026
https://github.com/Lane0218 created https://github.com/llvm/llvm-project/pull/218580
This patch fixes a miscompilation in ConstraintElimination caused by negating an INT64_MIN offset while constructing a constraint.
Use MulOverflow when negating the decomposed offset and skip constraint construction if the negation overflows. Add a regression test for the reported reproducer in Issue #218569.
Before this change, the reproducer was incorrectly folded to `ret i1 true`. After this change, the comparison is preserved.
Tests:
- build-cir/bin/llvm-lit llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll
---
本补丁修复 ConstraintElimination 在构造约束时对 INT64_MIN 偏移取反导致的错误编译问题。
在对分解后的偏移取反时使用 MulOverflow 检查;如果取反发生溢出,则放弃构造该约束。新增 Issue #218569 的回归测试。
修复前,测试用例会被错误折叠为 `ret i1 true`。修复后,该比较会被正确保留。
>From fc6c24d4dd0a4f7227d3a76b4c5260bce749a9ef Mon Sep 17 00:00:00 2001
From: Lane0218 <laneljc at qq.com>
Date: Tue, 25 Aug 2026 11:54:06 +0800
Subject: [PATCH] [ConstraintElim] Avoid overflow when negating offsets
---
.../Scalar/ConstraintElimination.cpp | 3 ++-
.../constraint-overflow.ll | 22 +++++++++++++++++++
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index ccdc51d67ac60..1cf5cc4a8ab5f 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -776,7 +776,8 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
IsSigned, DL);
int64_t Offset1 = ADec.Offset;
int64_t Offset2 = BDec.Offset;
- Offset1 *= -1;
+ if (MulOverflow(Offset1, int64_t(-1), Offset1))
+ return {};
auto &VariablesA = ADec.Vars;
auto &VariablesB = BDec.Vars;
diff --git a/llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll b/llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll
index 44f1554f161e9..bf2e4a933cf6d 100644
--- a/llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll
+++ b/llvm/test/Transforms/ConstraintElimination/constraint-overflow.ll
@@ -193,3 +193,25 @@ then:
else:
ret i1 false
}
+
+; The offset of %a is INT64_MIN. Negating it overflows, so no constraint can
+; be built for the assume. Make sure %r is not folded to true.
+define i1 @constraint_offset_negation_overflow(i64 %x) {
+; CHECK-LABEL: define i1 @constraint_offset_negation_overflow(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[A0:%.*]] = add nsw i64 [[X]], -9223372036854775807
+; CHECK-NEXT: [[A:%.*]] = add nsw i64 [[A0]], -1
+; CHECK-NEXT: [[A_NONPOS:%.*]] = icmp sle i64 [[A]], 0
+; CHECK-NEXT: call void @llvm.assume(i1 [[A_NONPOS]])
+; CHECK-NEXT: [[R:%.*]] = icmp slt i64 [[X]], 0
+; CHECK-NEXT: ret i1 [[R]]
+;
+entry:
+ %a0 = add nsw i64 %x, -9223372036854775807
+ %a = add nsw i64 %a0, -1
+ %a.nonpos = icmp sle i64 %a, 0
+ call void @llvm.assume(i1 %a.nonpos)
+ %r = icmp slt i64 %x, 0
+ ret i1 %r
+}
More information about the llvm-commits
mailing list