Skip to content

Commit f0d674c

Browse files
committed
ConstraintElim: idea for optimizing dry-run further
1 parent 11b306f commit f0d674c

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,13 +1682,11 @@ static void dryRunAddFact(CmpInst::Predicate Pred, Value *A, Value *B,
16821682
ConstraintInfo &Info, unsigned &EstimatedRowsA,
16831683
unsigned &EstimatedRowsB,
16841684
unsigned &EstimatedColumns) {
1685+
// What follows is a simplified dry-run of Info.getConstraint and addFact.
16851686
auto UpdateEstimate = [&Info, &EstimatedRowsA, &EstimatedRowsB,
16861687
&EstimatedColumns](CmpInst::Predicate Pred, Value *A,
16871688
Value *B) {
1688-
// What follows is a simplified dry-run of Info.getConstraint and addFact.
1689-
unsigned NumNewVars = 0;
16901689
bool IsSigned = false;
1691-
16921690
switch (Pred) {
16931691
case CmpInst::ICMP_UGT:
16941692
case CmpInst::ICMP_UGE:
@@ -1703,35 +1701,41 @@ static void dryRunAddFact(CmpInst::Predicate Pred, Value *A, Value *B,
17031701
return;
17041702
}
17051703

1706-
SmallVector<ConditionTy, 4> Preconditions;
1707-
auto &Value2Index = Info.getValue2Index(IsSigned);
1708-
Value *AStrip = A->stripPointerCastsSameRepresentation();
1709-
Value *BStrip = B->stripPointerCastsSameRepresentation();
1710-
SmallVector<DecompEntry> AVars, BVars;
1711-
1712-
if (!Value2Index.contains(AStrip)) {
1713-
AVars =
1714-
decompose(AStrip, Preconditions, IsSigned, Info.getDataLayout()).Vars;
1715-
Value2Index.insert({AStrip, Value2Index.size() + 1});
1716-
}
1717-
if (!Value2Index.contains(BStrip)) {
1718-
BVars =
1719-
decompose(BStrip, Preconditions, IsSigned, Info.getDataLayout()).Vars;
1720-
Value2Index.insert({BStrip, Value2Index.size() + 1});
1721-
}
1704+
// We abuse the Value2Index map by storing a map of Value -> size of
1705+
// decompose(Value).Vars. We do this to avoid wastefully calling decompose
1706+
// on chains we have already seen.
1707+
auto &DecompSizes = Info.getValue2Index(IsSigned);
1708+
const auto &DL = Info.getDataLayout();
1709+
1710+
auto GetDecompSize = [&DL, &DecompSizes, IsSigned](Value *V) {
1711+
if (!DecompSizes.contains(V)) {
1712+
SmallVector<ConditionTy, 4> Preconditions;
1713+
auto Vars = decompose(V, Preconditions, IsSigned, DL).Vars;
1714+
for (const auto &[Idx, KV] : enumerate(Vars))
1715+
// decompose matches the passed Value against some pattern, and
1716+
// recursively calls itself on the matched inner values, merging
1717+
// results into the final chain. We conservatively estimate that any
1718+
// sub-chain in the decompose chain will have the length of the chain
1719+
// minus the index: the results of two sub-chains could be merged,
1720+
// leading to a conservative estimate. This is however, not a problem
1721+
// in practice, as these conservative estimates are anyway less than
1722+
// the the size of the total chain, and std::max on the estimated
1723+
// columns will ignore these smaller estiamtes.
1724+
DecompSizes.insert({KV.Variable, Vars.size() - Idx});
1725+
DecompSizes.insert({V, Vars.size()});
1726+
}
1727+
return DecompSizes.at(V);
1728+
};
17221729

1723-
for (const auto &KV : concat<DecompEntry>(AVars, BVars)) {
1724-
if (KV.Variable == AStrip || KV.Variable == BStrip ||
1725-
!Value2Index.contains(KV.Variable))
1726-
++NumNewVars;
1727-
}
1730+
unsigned ASize = GetDecompSize(A->stripPointerCastsSameRepresentation());
1731+
unsigned BSize = GetDecompSize(B->stripPointerCastsSameRepresentation());
17281732

17291733
if (IsSigned)
17301734
++EstimatedRowsA;
17311735
else
17321736
++EstimatedRowsB;
17331737

1734-
EstimatedColumns = std::max(EstimatedColumns, NumNewVars + 2);
1738+
EstimatedColumns = std::max(EstimatedColumns, ASize + BSize + 2);
17351739
};
17361740

17371741
UpdateEstimate(Pred, A, B);
@@ -1764,7 +1768,7 @@ static void dryRunAddFact(CmpInst::Predicate Pred, Value *A, Value *B,
17641768
}
17651769

17661770
/// Performs a dry run of the transform, computing a conservative estimate of
1767-
/// the total number of columns we need in the underlying storage.
1771+
/// the total number of rows and columns we need in the underlying storage.
17681772
static std::tuple<State, unsigned, unsigned>
17691773
dryRun(Function &F, DominatorTree &DT, LoopInfo &LI, ScalarEvolution &SE) {
17701774
DT.updateDFSNumbers();

0 commit comments

Comments
 (0)