forked from OSchip/llvm-project
[SSAUpdaterBulk] Use SmallVector instead of DenseMap for storing rewrites.
llvm-svn: 330413
This commit is contained in:
parent
2bf7c51d0e
commit
9dea079315
|
@ -47,7 +47,7 @@ class SSAUpdaterBulk {
|
|||
RewriteInfo(){};
|
||||
RewriteInfo(StringRef &N, Type *T) : Name(N), Ty(T){};
|
||||
};
|
||||
DenseMap<unsigned, RewriteInfo> Rewrites;
|
||||
SmallVector<RewriteInfo, 4> Rewrites;
|
||||
|
||||
PredIteratorCache PredCache;
|
||||
|
||||
|
@ -60,8 +60,9 @@ public:
|
|||
~SSAUpdaterBulk(){};
|
||||
|
||||
/// Add a new variable to the SSA rewriter. This needs to be called before
|
||||
/// AddAvailableValue or AddUse calls.
|
||||
void AddVariable(unsigned Var, StringRef Name, Type *Ty);
|
||||
/// AddAvailableValue or AddUse calls. The return value is the variable ID,
|
||||
/// which needs to be passed to AddAvailableValue and AddUse.
|
||||
unsigned AddVariable(StringRef Name, Type *Ty);
|
||||
|
||||
/// Indicate that a rewritten value is available in the specified block with
|
||||
/// the specified value.
|
||||
|
|
|
@ -1992,7 +1992,6 @@ bool JumpThreadingPass::ThreadEdge(BasicBlock *BB,
|
|||
// PHI insertion, of which we are prepared to do, clean these up now.
|
||||
SSAUpdaterBulk SSAUpdate;
|
||||
|
||||
unsigned VarNum = 0;
|
||||
for (Instruction &I : *BB) {
|
||||
SmallVector<Use*, 16> UsesToRename;
|
||||
|
||||
|
@ -2014,7 +2013,7 @@ bool JumpThreadingPass::ThreadEdge(BasicBlock *BB,
|
|||
// If there are no uses outside the block, we're done with this instruction.
|
||||
if (UsesToRename.empty())
|
||||
continue;
|
||||
SSAUpdate.AddVariable(VarNum, I.getName(), I.getType());
|
||||
unsigned VarNum = SSAUpdate.AddVariable(I.getName(), I.getType());
|
||||
|
||||
// We found a use of I outside of BB - we need to rename all uses of I that
|
||||
// are outside its block to be uses of the appropriate PHI node etc.
|
||||
|
@ -2022,7 +2021,6 @@ bool JumpThreadingPass::ThreadEdge(BasicBlock *BB,
|
|||
SSAUpdate.AddAvailableValue(VarNum, NewBB, ValueMapping[&I]);
|
||||
for (auto *U : UsesToRename)
|
||||
SSAUpdate.AddUse(VarNum, U);
|
||||
VarNum++;
|
||||
}
|
||||
|
||||
// Ok, NewBB is good to go. Update the terminator of PredBB to jump to
|
||||
|
|
|
@ -38,18 +38,19 @@ static BasicBlock *getUserBB(Use *U) {
|
|||
|
||||
/// Add a new variable to the SSA rewriter. This needs to be called before
|
||||
/// AddAvailableValue or AddUse calls.
|
||||
void SSAUpdaterBulk::AddVariable(unsigned Var, StringRef Name, Type *Ty) {
|
||||
assert(Rewrites.find(Var) == Rewrites.end() && "Variable added twice!");
|
||||
unsigned SSAUpdaterBulk::AddVariable(StringRef Name, Type *Ty) {
|
||||
unsigned Var = Rewrites.size();
|
||||
DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": initialized with Ty = " << *Ty
|
||||
<< ", Name = " << Name << "\n");
|
||||
RewriteInfo RI(Name, Ty);
|
||||
Rewrites[Var] = RI;
|
||||
Rewrites.push_back(RI);
|
||||
return Var;
|
||||
}
|
||||
|
||||
/// Indicate that a rewritten value is available in the specified block with the
|
||||
/// specified value.
|
||||
void SSAUpdaterBulk::AddAvailableValue(unsigned Var, BasicBlock *BB, Value *V) {
|
||||
assert(Rewrites.find(Var) != Rewrites.end() && "Should add variable first!");
|
||||
assert(Var < Rewrites.size() && "Variable not found!");
|
||||
DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": added new available value"
|
||||
<< *V << " in " << BB->getName() << "\n");
|
||||
Rewrites[Var].Defines[BB] = V;
|
||||
|
@ -58,7 +59,7 @@ void SSAUpdaterBulk::AddAvailableValue(unsigned Var, BasicBlock *BB, Value *V) {
|
|||
/// Record a use of the symbolic value. This use will be updated with a
|
||||
/// rewritten value when RewriteAllUses is called.
|
||||
void SSAUpdaterBulk::AddUse(unsigned Var, Use *U) {
|
||||
assert(Rewrites.find(Var) != Rewrites.end() && "Should add variable first!");
|
||||
assert(Var < Rewrites.size() && "Variable not found!");
|
||||
DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": added a use" << *U->get()
|
||||
<< " in " << getUserBB(U)->getName() << "\n");
|
||||
Rewrites[Var].Uses.push_back(U);
|
||||
|
@ -67,7 +68,7 @@ void SSAUpdaterBulk::AddUse(unsigned Var, Use *U) {
|
|||
/// Return true if the SSAUpdater already has a value for the specified variable
|
||||
/// in the specified block.
|
||||
bool SSAUpdaterBulk::HasValueForBlock(unsigned Var, BasicBlock *BB) {
|
||||
return Rewrites.count(Var) ? Rewrites[Var].Defines.count(BB) : false;
|
||||
return (Var < Rewrites.size()) ? Rewrites[Var].Defines.count(BB) : false;
|
||||
}
|
||||
|
||||
// Compute value at the given block BB. We either should already know it, or we
|
||||
|
@ -126,16 +127,14 @@ ComputeLiveInBlocks(const SmallPtrSetImpl<BasicBlock *> &UsingBlocks,
|
|||
/// requested uses update.
|
||||
void SSAUpdaterBulk::RewriteAllUses(DominatorTree *DT,
|
||||
SmallVectorImpl<PHINode *> *InsertedPHIs) {
|
||||
for (auto &P : Rewrites) {
|
||||
for (auto &R : Rewrites) {
|
||||
// Compute locations for new phi-nodes.
|
||||
// For that we need to initialize DefBlocks from definitions in R.Defines,
|
||||
// UsingBlocks from uses in R.Uses, then compute LiveInBlocks, and then use
|
||||
// this set for computing iterated dominance frontier (IDF).
|
||||
// The IDF blocks are the blocks where we need to insert new phi-nodes.
|
||||
ForwardIDFCalculator IDF(*DT);
|
||||
RewriteInfo &R = P.second;
|
||||
DEBUG(dbgs() << "SSAUpdater: Var=" << P.first << ": rewriting "
|
||||
<< R.Uses.size() << " use(s)\n");
|
||||
DEBUG(dbgs() << "SSAUpdater: rewriting " << R.Uses.size() << " use(s)\n");
|
||||
|
||||
SmallPtrSet<BasicBlock *, 2> DefBlocks;
|
||||
for (auto &Def : R.Defines)
|
||||
|
@ -165,7 +164,7 @@ void SSAUpdaterBulk::RewriteAllUses(DominatorTree *DT,
|
|||
}
|
||||
|
||||
// Fill in arguments of the inserted PHIs.
|
||||
for (auto PN : InsertedPHIsForVar) {
|
||||
for (auto *PN : InsertedPHIsForVar) {
|
||||
BasicBlock *PBB = PN->getParent();
|
||||
for (BasicBlock *Pred : PredCache.get(PBB))
|
||||
PN->addIncoming(computeValueAt(Pred, R, DT), Pred);
|
||||
|
@ -182,8 +181,8 @@ void SSAUpdaterBulk::RewriteAllUses(DominatorTree *DT,
|
|||
// Notify that users of the existing value that it is being replaced.
|
||||
if (OldVal != V && OldVal->hasValueHandle())
|
||||
ValueHandleBase::ValueIsRAUWd(OldVal, V);
|
||||
DEBUG(dbgs() << "SSAUpdater: Var=" << P.first << ": replacing" << *OldVal
|
||||
<< " with " << *V << "\n");
|
||||
DEBUG(dbgs() << "SSAUpdater: replacing " << *OldVal << " with " << *V
|
||||
<< "\n");
|
||||
U->set(V);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,17 +73,17 @@ TEST(SSAUpdaterBulk, SimpleMerge) {
|
|||
// SSAUpdater should insert into %merge.
|
||||
// Intentionally don't touch %8 to see that SSAUpdater only changes
|
||||
// instructions that were explicitly specified.
|
||||
Updater.AddVariable(0, "a", I32Ty);
|
||||
Updater.AddAvailableValue(0, TrueBB, AddOp1);
|
||||
Updater.AddAvailableValue(0, FalseBB, AddOp2);
|
||||
Updater.AddUse(0, &I1->getOperandUse(0));
|
||||
Updater.AddUse(0, &I2->getOperandUse(0));
|
||||
unsigned VarNum = Updater.AddVariable("a", I32Ty);
|
||||
Updater.AddAvailableValue(VarNum, TrueBB, AddOp1);
|
||||
Updater.AddAvailableValue(VarNum, FalseBB, AddOp2);
|
||||
Updater.AddUse(VarNum, &I1->getOperandUse(0));
|
||||
Updater.AddUse(VarNum, &I2->getOperandUse(0));
|
||||
|
||||
Updater.AddVariable(1, "b", I32Ty);
|
||||
Updater.AddAvailableValue(1, TrueBB, SubOp1);
|
||||
Updater.AddAvailableValue(1, FalseBB, SubOp2);
|
||||
Updater.AddUse(1, &I3->getOperandUse(0));
|
||||
Updater.AddUse(1, &I3->getOperandUse(1));
|
||||
VarNum = Updater.AddVariable("b", I32Ty);
|
||||
Updater.AddAvailableValue(VarNum, TrueBB, SubOp1);
|
||||
Updater.AddAvailableValue(VarNum, FalseBB, SubOp2);
|
||||
Updater.AddUse(VarNum, &I3->getOperandUse(0));
|
||||
Updater.AddUse(VarNum, &I3->getOperandUse(1));
|
||||
|
||||
DominatorTree DT(*F);
|
||||
Updater.RewriteAllUses(&DT);
|
||||
|
@ -161,19 +161,19 @@ TEST(SSAUpdaterBulk, Irreducible) {
|
|||
// No other rewrites should be made.
|
||||
|
||||
// Add use in %3.
|
||||
Updater.AddVariable(0, "c", I32Ty);
|
||||
Updater.AddAvailableValue(0, IfBB, AddOp1);
|
||||
Updater.AddUse(0, &I1->getOperandUse(0));
|
||||
unsigned VarNum = Updater.AddVariable("c", I32Ty);
|
||||
Updater.AddAvailableValue(VarNum, IfBB, AddOp1);
|
||||
Updater.AddUse(VarNum, &I1->getOperandUse(0));
|
||||
|
||||
// Add use in %4.
|
||||
Updater.AddVariable(1, "b", I32Ty);
|
||||
Updater.AddAvailableValue(1, LoopStartBB, AddOp2);
|
||||
Updater.AddUse(1, &I2->getOperandUse(0));
|
||||
VarNum = Updater.AddVariable("b", I32Ty);
|
||||
Updater.AddAvailableValue(VarNum, LoopStartBB, AddOp2);
|
||||
Updater.AddUse(VarNum, &I2->getOperandUse(0));
|
||||
|
||||
// Add use in the return instruction.
|
||||
Updater.AddVariable(2, "a", I32Ty);
|
||||
Updater.AddAvailableValue(2, &F->getEntryBlock(), FirstArg);
|
||||
Updater.AddUse(2, &Return->getOperandUse(0));
|
||||
VarNum = Updater.AddVariable("a", I32Ty);
|
||||
Updater.AddAvailableValue(VarNum, &F->getEntryBlock(), FirstArg);
|
||||
Updater.AddUse(VarNum, &Return->getOperandUse(0));
|
||||
|
||||
// Save all inserted phis into a vector.
|
||||
SmallVector<PHINode *, 8> Inserted;
|
||||
|
|
Loading…
Reference in New Issue