Skip to content

Commit 2a90efd

Browse files
authored
[NFC][ConstraintElimination] Optimize code styles (#121422)
This patch does following things, - prefer early exits; - add missing std::move; - avoid duplicate map lookups; - prefer emplace_back to avoid unnecessary copies.
1 parent 6d3d952 commit 2a90efd

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ struct StackEntry {
216216
StackEntry(unsigned NumIn, unsigned NumOut, bool IsSigned,
217217
SmallVector<Value *, 2> ValuesToRelease)
218218
: NumIn(NumIn), NumOut(NumOut), IsSigned(IsSigned),
219-
ValuesToRelease(ValuesToRelease) {}
219+
ValuesToRelease(std::move(ValuesToRelease)) {}
220220
};
221221

222222
struct ConstraintTy {
@@ -726,8 +726,8 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
726726
}
727727

728728
for (const auto &KV : VariablesB) {
729-
if (SubOverflow(R[GetOrAddIndex(KV.Variable)], KV.Coefficient,
730-
R[GetOrAddIndex(KV.Variable)]))
729+
auto &Coeff = R[GetOrAddIndex(KV.Variable)];
730+
if (SubOverflow(Coeff, KV.Coefficient, Coeff))
731731
return {};
732732
auto I =
733733
KnownNonNegativeVariables.insert({KV.Variable, KV.IsKnownNonNegative});
@@ -759,9 +759,9 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
759759
if (!KV.second ||
760760
(!Value2Index.contains(KV.first) && !NewIndexMap.contains(KV.first)))
761761
continue;
762-
SmallVector<int64_t, 8> C(Value2Index.size() + NewVariables.size() + 1, 0);
762+
auto &C = Res.ExtraInfo.emplace_back(
763+
Value2Index.size() + NewVariables.size() + 1, 0);
763764
C[GetOrAddIndex(KV.first)] = -1;
764-
Res.ExtraInfo.push_back(C);
765765
}
766766
return Res;
767767
}
@@ -1591,53 +1591,52 @@ void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B,
15911591

15921592
LLVM_DEBUG(dbgs() << "Adding '"; dumpUnpackedICmp(dbgs(), Pred, A, B);
15931593
dbgs() << "'\n");
1594-
bool Added = false;
15951594
auto &CSToUse = getCS(R.IsSigned);
15961595
if (R.Coefficients.empty())
15971596
return;
15981597

1599-
Added |= CSToUse.addVariableRowFill(R.Coefficients);
1598+
bool Added = CSToUse.addVariableRowFill(R.Coefficients);
1599+
if (!Added)
1600+
return;
16001601

16011602
// If R has been added to the system, add the new variables and queue it for
16021603
// removal once it goes out-of-scope.
1603-
if (Added) {
1604-
SmallVector<Value *, 2> ValuesToRelease;
1605-
auto &Value2Index = getValue2Index(R.IsSigned);
1606-
for (Value *V : NewVariables) {
1607-
Value2Index.insert({V, Value2Index.size() + 1});
1608-
ValuesToRelease.push_back(V);
1609-
}
1610-
1611-
LLVM_DEBUG({
1612-
dbgs() << " constraint: ";
1613-
dumpConstraint(R.Coefficients, getValue2Index(R.IsSigned));
1614-
dbgs() << "\n";
1615-
});
1604+
SmallVector<Value *, 2> ValuesToRelease;
1605+
auto &Value2Index = getValue2Index(R.IsSigned);
1606+
for (Value *V : NewVariables) {
1607+
Value2Index.insert({V, Value2Index.size() + 1});
1608+
ValuesToRelease.push_back(V);
1609+
}
16161610

1617-
DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
1618-
std::move(ValuesToRelease));
1619-
1620-
if (!R.IsSigned) {
1621-
for (Value *V : NewVariables) {
1622-
ConstraintTy VarPos(SmallVector<int64_t, 8>(Value2Index.size() + 1, 0),
1623-
false, false, false);
1624-
VarPos.Coefficients[Value2Index[V]] = -1;
1625-
CSToUse.addVariableRow(VarPos.Coefficients);
1626-
DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
1627-
SmallVector<Value *, 2>());
1628-
}
1629-
}
1611+
LLVM_DEBUG({
1612+
dbgs() << " constraint: ";
1613+
dumpConstraint(R.Coefficients, getValue2Index(R.IsSigned));
1614+
dbgs() << "\n";
1615+
});
16301616

1631-
if (R.isEq()) {
1632-
// Also add the inverted constraint for equality constraints.
1633-
for (auto &Coeff : R.Coefficients)
1634-
Coeff *= -1;
1635-
CSToUse.addVariableRowFill(R.Coefficients);
1617+
DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
1618+
std::move(ValuesToRelease));
16361619

1620+
if (!R.IsSigned) {
1621+
for (Value *V : NewVariables) {
1622+
ConstraintTy VarPos(SmallVector<int64_t, 8>(Value2Index.size() + 1, 0),
1623+
false, false, false);
1624+
VarPos.Coefficients[Value2Index[V]] = -1;
1625+
CSToUse.addVariableRow(VarPos.Coefficients);
16371626
DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
16381627
SmallVector<Value *, 2>());
16391628
}
16401629
}
1630+
1631+
if (R.isEq()) {
1632+
// Also add the inverted constraint for equality constraints.
1633+
for (auto &Coeff : R.Coefficients)
1634+
Coeff *= -1;
1635+
CSToUse.addVariableRowFill(R.Coefficients);
1636+
1637+
DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,
1638+
SmallVector<Value *, 2>());
1639+
}
16411640
}
16421641

16431642
static bool replaceSubOverflowUses(IntrinsicInst *II, Value *A, Value *B,

0 commit comments

Comments
 (0)