Skip to content

Commit 2fb68c0

Browse files
committed
[ConstraintElimination] Replace pair with named struct (NFC).
This slightly improves the readability and allows further extensions in follow-ups.
1 parent 4c46a5d commit 2fb68c0

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,22 @@ class ConstraintInfo {
168168
SmallVectorImpl<StackEntry> &DFSInStack);
169169
};
170170

171+
/// Represents a (Coefficient * Variable) entry after IR decomposition.
172+
struct DecompEntry {
173+
int64_t Coefficient;
174+
Value *Variable;
175+
176+
DecompEntry(int64_t Coefficient, Value *Variable)
177+
: Coefficient(Coefficient), Variable(Variable) {}
178+
};
179+
171180
} // namespace
172181

173-
// Decomposes \p V into a vector of pairs of the form { c, X } where c * X. The
174-
// sum of the pairs equals \p V. The first pair is the constant-factor and X
175-
// must be nullptr. If the expression cannot be decomposed, returns an empty
176-
// vector.
177-
static SmallVector<std::pair<int64_t, Value *>, 4>
182+
// Decomposes \p V into a vector of entries of the form { Coefficient, Variable
183+
// } where Coefficient * Variable. The sum of the pairs equals \p V. The first
184+
// pair is the constant-factor and X must be nullptr. If the expression cannot
185+
// be decomposed, returns an empty vector.
186+
static SmallVector<DecompEntry, 4>
178187
decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
179188
bool IsSigned) {
180189

@@ -195,7 +204,7 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
195204
if (auto *CI = dyn_cast<ConstantInt>(V)) {
196205
if (CI->uge(MaxConstraintValue))
197206
return {};
198-
return {{CI->getZExtValue(), nullptr}};
207+
return {{int64_t(CI->getZExtValue()), nullptr}};
199208
}
200209
auto *GEP = dyn_cast<GetElementPtrInst>(V);
201210
if (GEP && GEP->getNumOperands() == 2 && GEP->isInBounds()) {
@@ -209,7 +218,7 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
209218
CanUseSExt(CI))
210219
return {{0, nullptr},
211220
{1, GEP->getPointerOperand()},
212-
{std::pow(int64_t(2), CI->getSExtValue()), Op1}};
221+
{int64_t(std::pow(int64_t(2), CI->getSExtValue())), Op1}};
213222
if (match(Op0, m_NSWAdd(m_Value(Op1), m_ConstantInt(CI))) &&
214223
CanUseSExt(CI))
215224
return {{CI->getSExtValue(), nullptr},
@@ -222,13 +231,13 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
222231
!CI->isNegative() && CanUseSExt(CI))
223232
return {{CI->getSExtValue(), nullptr}, {1, GEP->getPointerOperand()}};
224233

225-
SmallVector<std::pair<int64_t, Value *>, 4> Result;
234+
SmallVector<DecompEntry, 4> Result;
226235
if (match(GEP->getOperand(GEP->getNumOperands() - 1),
227236
m_NUWShl(m_Value(Op0), m_ConstantInt(CI))) &&
228237
CanUseSExt(CI))
229238
Result = {{0, nullptr},
230239
{1, GEP->getPointerOperand()},
231-
{std::pow(int64_t(2), CI->getSExtValue()), Op0}};
240+
{int(std::pow(int64_t(2), CI->getSExtValue())), Op0}};
232241
else if (match(GEP->getOperand(GEP->getNumOperands() - 1),
233242
m_NSWAdd(m_Value(Op0), m_ConstantInt(CI))) &&
234243
CanUseSExt(CI))
@@ -254,7 +263,7 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
254263
ConstantInt *CI;
255264
if (match(V, m_NUWAdd(m_Value(Op0), m_ConstantInt(CI))) &&
256265
!CI->uge(MaxConstraintValue))
257-
return {{CI->getZExtValue(), nullptr}, {1, Op0}};
266+
return {{int(CI->getZExtValue()), nullptr}, {1, Op0}};
258267
if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative() &&
259268
CanUseSExt(CI)) {
260269
Preconditions.emplace_back(
@@ -321,8 +330,8 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
321330
if (ADec.empty() || BDec.empty())
322331
return {};
323332

324-
int64_t Offset1 = ADec[0].first;
325-
int64_t Offset2 = BDec[0].first;
333+
int64_t Offset1 = ADec[0].Coefficient;
334+
int64_t Offset2 = BDec[0].Coefficient;
326335
Offset1 *= -1;
327336

328337
// Create iterator ranges that skip the constant-factor.
@@ -341,9 +350,8 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
341350
};
342351

343352
// Make sure all variables have entries in Value2Index or NewIndices.
344-
for (const auto &KV :
345-
concat<std::pair<int64_t, Value *>>(VariablesA, VariablesB))
346-
GetOrAddIndex(KV.second);
353+
for (const auto &KV : concat<DecompEntry>(VariablesA, VariablesB))
354+
GetOrAddIndex(KV.Variable);
347355

348356
// Build result constraint, by first adding all coefficients from A and then
349357
// subtracting all coefficients from B.
@@ -353,10 +361,10 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
353361
Res.IsEq = IsEq;
354362
auto &R = Res.Coefficients;
355363
for (const auto &KV : VariablesA)
356-
R[GetOrAddIndex(KV.second)] += KV.first;
364+
R[GetOrAddIndex(KV.Variable)] += KV.Coefficient;
357365

358366
for (const auto &KV : VariablesB)
359-
R[GetOrAddIndex(KV.second)] -= KV.first;
367+
R[GetOrAddIndex(KV.Variable)] -= KV.Coefficient;
360368

361369
int64_t OffsetSum;
362370
if (AddOverflow(Offset1, Offset2, OffsetSum))

0 commit comments

Comments
 (0)