[llvm] [ConstraintElim] Strip IsKnownNonNegative (NFC) (PR #177993)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 26 09:38:21 PST 2026
https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/177993
>From 8b3a25950e147102a6c041329ad270d8b5bbd7c5 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 26 Jan 2026 16:21:08 +0000
Subject: [PATCH 1/3] [ConstraintElim] Strip IsKnownNonNegative (NFC)
The IsKnownNonNegative field is redundant, as the use of ValueTracking's
isKnownNonNegative subsumes this.
---
.../Scalar/ConstraintElimination.cpp | 57 ++++++-------------
1 file changed, 16 insertions(+), 41 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 480ab8b822595..527ec8da5f718 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -335,13 +335,10 @@ class ConstraintInfo {
struct DecompEntry {
int64_t Coefficient;
Value *Variable;
- /// True if the variable is known positive in the current constraint.
- bool IsKnownNonNegative;
DecompEntry(int64_t Coefficient, Value *Variable,
bool IsKnownNonNegative = false)
- : Coefficient(Coefficient), Variable(Variable),
- IsKnownNonNegative(IsKnownNonNegative) {}
+ : Coefficient(Coefficient), Variable(Variable) {}
};
/// Represents an Offset + Coefficient1 * Variable1 + ... decomposition.
@@ -350,9 +347,7 @@ struct Decomposition {
SmallVector<DecompEntry, 3> Vars;
Decomposition(int64_t Offset) : Offset(Offset) {}
- Decomposition(Value *V, bool IsKnownNonNegative = false) {
- Vars.emplace_back(1, V, IsKnownNonNegative);
- }
+ Decomposition(Value *V) { Vars.emplace_back(1, V); }
Decomposition(int64_t Offset, ArrayRef<DecompEntry> Vars)
: Offset(Offset), Vars(Vars) {}
@@ -522,8 +517,6 @@ static Decomposition decompose(Value *V,
if (!Ty->isIntegerTy() || Ty->getIntegerBitWidth() > 64)
return V;
- bool IsKnownNonNegative = false;
-
// Decompose \p V used with a signed predicate.
if (IsSigned) {
if (auto *CI = dyn_cast<ConstantInt>(V)) {
@@ -537,7 +530,6 @@ static Decomposition decompose(Value *V,
V = Op0;
else if (match(V, m_NNegZExt(m_Value(Op0)))) {
V = Op0;
- IsKnownNonNegative = true;
} else if (match(V, m_NSWTrunc(m_Value(Op0)))) {
if (Op0->getType()->getScalarSizeInBits() <= 64)
V = Op0;
@@ -546,7 +538,7 @@ static Decomposition decompose(Value *V,
if (match(V, m_NSWAdd(m_Value(Op0), m_Value(Op1)))) {
if (auto Decomp = MergeResults(Op0, Op1, IsSigned))
return *Decomp;
- return {V, IsKnownNonNegative};
+ return V;
}
if (match(V, m_NSWSub(m_Value(Op0), m_Value(Op1)))) {
@@ -554,7 +546,7 @@ static Decomposition decompose(Value *V,
auto ResB = decompose(Op1, Preconditions, IsSigned, DL);
if (!ResA.sub(ResB))
return ResA;
- return {V, IsKnownNonNegative};
+ return V;
}
ConstantInt *CI;
@@ -562,7 +554,7 @@ static Decomposition decompose(Value *V,
auto Result = decompose(Op0, Preconditions, IsSigned, DL);
if (!Result.mul(CI->getSExtValue()))
return Result;
- return {V, IsKnownNonNegative};
+ return V;
}
// (shl nsw x, shift) is (mul nsw x, (1<<shift)), with the exception of
@@ -574,11 +566,11 @@ static Decomposition decompose(Value *V,
auto Result = decompose(Op0, Preconditions, IsSigned, DL);
if (!Result.mul(int64_t(1) << Shift))
return Result;
- return {V, IsKnownNonNegative};
+ return V;
}
}
- return {V, IsKnownNonNegative};
+ return V;
}
if (auto *CI = dyn_cast<ConstantInt>(V)) {
@@ -589,7 +581,6 @@ static Decomposition decompose(Value *V,
Value *Op0;
if (match(V, m_ZExt(m_Value(Op0)))) {
- IsKnownNonNegative = true;
V = Op0;
} else if (match(V, m_SExt(m_Value(Op0)))) {
V = Op0;
@@ -611,7 +602,7 @@ static Decomposition decompose(Value *V,
if (match(V, m_NUWAdd(m_Value(Op0), m_Value(Op1)))) {
if (auto Decomp = MergeResults(Op0, Op1, IsSigned))
return *Decomp;
- return {V, IsKnownNonNegative};
+ return V;
}
if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative() &&
@@ -621,7 +612,7 @@ static Decomposition decompose(Value *V,
ConstantInt::get(Op0->getType(), CI->getSExtValue() * -1));
if (auto Decomp = MergeResults(Op0, CI, true))
return *Decomp;
- return {V, IsKnownNonNegative};
+ return V;
}
if (match(V, m_NSWAdd(m_Value(Op0), m_Value(Op1)))) {
@@ -634,23 +625,23 @@ static Decomposition decompose(Value *V,
if (auto Decomp = MergeResults(Op0, Op1, IsSigned))
return *Decomp;
- return {V, IsKnownNonNegative};
+ return V;
}
// Decompose or as an add if there are no common bits between the operands.
if (match(V, m_DisjointOr(m_Value(Op0), m_ConstantInt(CI)))) {
if (auto Decomp = MergeResults(Op0, CI, IsSigned))
return *Decomp;
- return {V, IsKnownNonNegative};
+ return V;
}
if (match(V, m_NUWShl(m_Value(Op1), m_ConstantInt(CI))) && canUseSExt(CI)) {
if (CI->getSExtValue() < 0 || CI->getSExtValue() >= 64)
- return {V, IsKnownNonNegative};
+ return V;
auto Result = decompose(Op1, Preconditions, IsSigned, DL);
if (!Result.mul(int64_t{1} << CI->getSExtValue()))
return Result;
- return {V, IsKnownNonNegative};
+ return V;
}
if (match(V, m_NUWMul(m_Value(Op1), m_ConstantInt(CI))) && canUseSExt(CI) &&
@@ -658,7 +649,7 @@ static Decomposition decompose(Value *V,
auto Result = decompose(Op1, Preconditions, IsSigned, DL);
if (!Result.mul(CI->getSExtValue()))
return Result;
- return {V, IsKnownNonNegative};
+ return V;
}
if (match(V, m_NUWSub(m_Value(Op0), m_Value(Op1)))) {
@@ -666,10 +657,10 @@ static Decomposition decompose(Value *V,
auto ResB = decompose(Op1, Preconditions, IsSigned, DL);
if (!ResA.sub(ResB))
return ResA;
- return {V, IsKnownNonNegative};
+ return V;
}
- return {V, IsKnownNonNegative};
+ return V;
}
ConstraintTy
@@ -758,22 +749,15 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
IsSigned, IsEq, IsNe);
// Collect variables that are known to be positive in all uses in the
// constraint.
- SmallDenseMap<Value *, bool> KnownNonNegativeVariables;
auto &R = Res.Coefficients;
for (const auto &KV : VariablesA) {
R[GetOrAddIndex(KV.Variable)] += KV.Coefficient;
- auto I =
- KnownNonNegativeVariables.insert({KV.Variable, KV.IsKnownNonNegative});
- I.first->second &= KV.IsKnownNonNegative;
}
for (const auto &KV : VariablesB) {
auto &Coeff = R[GetOrAddIndex(KV.Variable)];
if (SubOverflow(Coeff, KV.Coefficient, Coeff))
return {};
- auto I =
- KnownNonNegativeVariables.insert({KV.Variable, KV.IsKnownNonNegative});
- I.first->second &= KV.IsKnownNonNegative;
}
int64_t OffsetSum;
@@ -796,15 +780,6 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
NewIndexMap.erase(RemovedV);
}
- // Add extra constraints for variables that are known positive.
- for (auto &KV : KnownNonNegativeVariables) {
- if (!KV.second ||
- (!Value2Index.contains(KV.first) && !NewIndexMap.contains(KV.first)))
- continue;
- auto &C = Res.ExtraInfo.emplace_back(
- Value2Index.size() + NewVariables.size() + 1, 0);
- C[GetOrAddIndex(KV.first)] = -1;
- }
return Res;
}
>From 26c02ab5ef629ce68008d062d8774521630a9974 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 26 Jan 2026 16:47:10 +0000
Subject: [PATCH 2/3] [ConstraintElim] Drop braces (NFC)
---
llvm/lib/Transforms/Scalar/ConstraintElimination.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 527ec8da5f718..daff7b376285f 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -750,9 +750,8 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
// Collect variables that are known to be positive in all uses in the
// constraint.
auto &R = Res.Coefficients;
- for (const auto &KV : VariablesA) {
+ for (const auto &KV : VariablesA)
R[GetOrAddIndex(KV.Variable)] += KV.Coefficient;
- }
for (const auto &KV : VariablesB) {
auto &Coeff = R[GetOrAddIndex(KV.Variable)];
>From 6ba8ca40f8ccd2d701ceb906dc340d9cab731020 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 26 Jan 2026 17:37:16 +0000
Subject: [PATCH 3/3] [ConstraintElim] Fix redundant param (NFC)
---
llvm/lib/Transforms/Scalar/ConstraintElimination.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index daff7b376285f..acf9971f23939 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -336,8 +336,7 @@ struct DecompEntry {
int64_t Coefficient;
Value *Variable;
- DecompEntry(int64_t Coefficient, Value *Variable,
- bool IsKnownNonNegative = false)
+ DecompEntry(int64_t Coefficient, Value *Variable)
: Coefficient(Coefficient), Variable(Variable) {}
};
More information about the llvm-commits
mailing list