Skip to content

Commit 11b306f

Browse files
committed
ConstraintElim: avoid running decompose multiple times
1 parent 50c2e95 commit 11b306f

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "llvm/IR/Instructions.h"
3232
#include "llvm/IR/Module.h"
3333
#include "llvm/IR/PatternMatch.h"
34+
#include "llvm/IR/Value.h"
3435
#include "llvm/IR/Verifier.h"
3536
#include "llvm/Pass.h"
3637
#include "llvm/Support/CommandLine.h"
@@ -1678,7 +1679,7 @@ tryToSimplifyOverflowMath(IntrinsicInst *II, ConstraintInfo &Info,
16781679
/// Performs a dry run of AddFact, computing a conservative estimate of the
16791680
/// number of new variables introduced.
16801681
static void dryRunAddFact(CmpInst::Predicate Pred, Value *A, Value *B,
1681-
const ConstraintInfo &Info, unsigned &EstimatedRowsA,
1682+
ConstraintInfo &Info, unsigned &EstimatedRowsA,
16821683
unsigned &EstimatedRowsB,
16831684
unsigned &EstimatedColumns) {
16841685
auto UpdateEstimate = [&Info, &EstimatedRowsA, &EstimatedRowsB,
@@ -1704,12 +1705,24 @@ static void dryRunAddFact(CmpInst::Predicate Pred, Value *A, Value *B,
17041705

17051706
SmallVector<ConditionTy, 4> Preconditions;
17061707
auto &Value2Index = Info.getValue2Index(IsSigned);
1707-
auto ADec = decompose(A->stripPointerCastsSameRepresentation(),
1708-
Preconditions, IsSigned, Info.getDataLayout());
1709-
auto BDec = decompose(B->stripPointerCastsSameRepresentation(),
1710-
Preconditions, IsSigned, Info.getDataLayout());
1711-
for (const auto &KV : concat<DecompEntry>(ADec.Vars, BDec.Vars)) {
1712-
if (!Value2Index.contains(KV.Variable))
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+
}
1722+
1723+
for (const auto &KV : concat<DecompEntry>(AVars, BVars)) {
1724+
if (KV.Variable == AStrip || KV.Variable == BStrip ||
1725+
!Value2Index.contains(KV.Variable))
17131726
++NumNewVars;
17141727
}
17151728

@@ -1718,8 +1731,7 @@ static void dryRunAddFact(CmpInst::Predicate Pred, Value *A, Value *B,
17181731
else
17191732
++EstimatedRowsB;
17201733

1721-
EstimatedColumns =
1722-
std::max(EstimatedColumns, NumNewVars + Value2Index.size() + 2);
1734+
EstimatedColumns = std::max(EstimatedColumns, NumNewVars + 2);
17231735
};
17241736

17251737
UpdateEstimate(Pred, A, B);
@@ -1756,16 +1768,13 @@ static void dryRunAddFact(CmpInst::Predicate Pred, Value *A, Value *B,
17561768
static std::tuple<State, unsigned, unsigned>
17571769
dryRun(Function &F, DominatorTree &DT, LoopInfo &LI, ScalarEvolution &SE) {
17581770
DT.updateDFSNumbers();
1759-
SmallVector<Value *> FunctionArgs;
1760-
for (Value &Arg : F.args())
1761-
FunctionArgs.push_back(&Arg);
17621771
State S(DT, LI, SE);
1763-
unsigned EstimatedColumns = FunctionArgs.size() + 1;
17641772

17651773
// EstimatedRowsA corresponds to SignedCS, and EstimatedRowsB corresponds to
17661774
// UnsignedCS.
17671775
unsigned EstimatedRowsA = 0, EstimatedRowsB = 1;
1768-
ConstraintInfo Info(F.getDataLayout(), FunctionArgs);
1776+
unsigned EstimatedColumns = 1;
1777+
ConstraintInfo Info(F.getDataLayout(), {});
17691778

17701779
// First, collect conditions implied by branches and blocks with their
17711780
// Dominator DFS in and out numbers.

0 commit comments

Comments
 (0)