Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 35a415f

Browse files
author
Michael Zolotukhin
committed
[SSAUpdaterBulk] Use SmallVector instead of DenseMap for storing rewrites.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330413 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 50f681d commit 35a415f

File tree

4 files changed

+37
-39
lines changed

4 files changed

+37
-39
lines changed

include/llvm/Transforms/Utils/SSAUpdaterBulk.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class SSAUpdaterBulk {
4747
RewriteInfo(){};
4848
RewriteInfo(StringRef &N, Type *T) : Name(N), Ty(T){};
4949
};
50-
DenseMap<unsigned, RewriteInfo> Rewrites;
50+
SmallVector<RewriteInfo, 4> Rewrites;
5151

5252
PredIteratorCache PredCache;
5353

@@ -60,8 +60,9 @@ class SSAUpdaterBulk {
6060
~SSAUpdaterBulk(){};
6161

6262
/// Add a new variable to the SSA rewriter. This needs to be called before
63-
/// AddAvailableValue or AddUse calls.
64-
void AddVariable(unsigned Var, StringRef Name, Type *Ty);
63+
/// AddAvailableValue or AddUse calls. The return value is the variable ID,
64+
/// which needs to be passed to AddAvailableValue and AddUse.
65+
unsigned AddVariable(StringRef Name, Type *Ty);
6566

6667
/// Indicate that a rewritten value is available in the specified block with
6768
/// the specified value.

lib/Transforms/Scalar/JumpThreading.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,6 @@ bool JumpThreadingPass::ThreadEdge(BasicBlock *BB,
19921992
// PHI insertion, of which we are prepared to do, clean these up now.
19931993
SSAUpdaterBulk SSAUpdate;
19941994

1995-
unsigned VarNum = 0;
19961995
for (Instruction &I : *BB) {
19971996
SmallVector<Use*, 16> UsesToRename;
19981997

@@ -2014,15 +2013,14 @@ bool JumpThreadingPass::ThreadEdge(BasicBlock *BB,
20142013
// If there are no uses outside the block, we're done with this instruction.
20152014
if (UsesToRename.empty())
20162015
continue;
2017-
SSAUpdate.AddVariable(VarNum, I.getName(), I.getType());
2016+
unsigned VarNum = SSAUpdate.AddVariable(I.getName(), I.getType());
20182017

20192018
// We found a use of I outside of BB - we need to rename all uses of I that
20202019
// are outside its block to be uses of the appropriate PHI node etc.
20212020
SSAUpdate.AddAvailableValue(VarNum, BB, &I);
20222021
SSAUpdate.AddAvailableValue(VarNum, NewBB, ValueMapping[&I]);
20232022
for (auto *U : UsesToRename)
20242023
SSAUpdate.AddUse(VarNum, U);
2025-
VarNum++;
20262024
}
20272025

20282026
// Ok, NewBB is good to go. Update the terminator of PredBB to jump to

lib/Transforms/Utils/SSAUpdaterBulk.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,19 @@ static BasicBlock *getUserBB(Use *U) {
3838

3939
/// Add a new variable to the SSA rewriter. This needs to be called before
4040
/// AddAvailableValue or AddUse calls.
41-
void SSAUpdaterBulk::AddVariable(unsigned Var, StringRef Name, Type *Ty) {
42-
assert(Rewrites.find(Var) == Rewrites.end() && "Variable added twice!");
41+
unsigned SSAUpdaterBulk::AddVariable(StringRef Name, Type *Ty) {
42+
unsigned Var = Rewrites.size();
4343
DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": initialized with Ty = " << *Ty
4444
<< ", Name = " << Name << "\n");
4545
RewriteInfo RI(Name, Ty);
46-
Rewrites[Var] = RI;
46+
Rewrites.push_back(RI);
47+
return Var;
4748
}
4849

4950
/// Indicate that a rewritten value is available in the specified block with the
5051
/// specified value.
5152
void SSAUpdaterBulk::AddAvailableValue(unsigned Var, BasicBlock *BB, Value *V) {
52-
assert(Rewrites.find(Var) != Rewrites.end() && "Should add variable first!");
53+
assert(Var < Rewrites.size() && "Variable not found!");
5354
DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": added new available value"
5455
<< *V << " in " << BB->getName() << "\n");
5556
Rewrites[Var].Defines[BB] = V;
@@ -58,7 +59,7 @@ void SSAUpdaterBulk::AddAvailableValue(unsigned Var, BasicBlock *BB, Value *V) {
5859
/// Record a use of the symbolic value. This use will be updated with a
5960
/// rewritten value when RewriteAllUses is called.
6061
void SSAUpdaterBulk::AddUse(unsigned Var, Use *U) {
61-
assert(Rewrites.find(Var) != Rewrites.end() && "Should add variable first!");
62+
assert(Var < Rewrites.size() && "Variable not found!");
6263
DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": added a use" << *U->get()
6364
<< " in " << getUserBB(U)->getName() << "\n");
6465
Rewrites[Var].Uses.push_back(U);
@@ -67,7 +68,7 @@ void SSAUpdaterBulk::AddUse(unsigned Var, Use *U) {
6768
/// Return true if the SSAUpdater already has a value for the specified variable
6869
/// in the specified block.
6970
bool SSAUpdaterBulk::HasValueForBlock(unsigned Var, BasicBlock *BB) {
70-
return Rewrites.count(Var) ? Rewrites[Var].Defines.count(BB) : false;
71+
return (Var < Rewrites.size()) ? Rewrites[Var].Defines.count(BB) : false;
7172
}
7273

7374
// Compute value at the given block BB. We either should already know it, or we
@@ -126,16 +127,14 @@ ComputeLiveInBlocks(const SmallPtrSetImpl<BasicBlock *> &UsingBlocks,
126127
/// requested uses update.
127128
void SSAUpdaterBulk::RewriteAllUses(DominatorTree *DT,
128129
SmallVectorImpl<PHINode *> *InsertedPHIs) {
129-
for (auto &P : Rewrites) {
130+
for (auto &R : Rewrites) {
130131
// Compute locations for new phi-nodes.
131132
// For that we need to initialize DefBlocks from definitions in R.Defines,
132133
// UsingBlocks from uses in R.Uses, then compute LiveInBlocks, and then use
133134
// this set for computing iterated dominance frontier (IDF).
134135
// The IDF blocks are the blocks where we need to insert new phi-nodes.
135136
ForwardIDFCalculator IDF(*DT);
136-
RewriteInfo &R = P.second;
137-
DEBUG(dbgs() << "SSAUpdater: Var=" << P.first << ": rewriting "
138-
<< R.Uses.size() << " use(s)\n");
137+
DEBUG(dbgs() << "SSAUpdater: rewriting " << R.Uses.size() << " use(s)\n");
139138

140139
SmallPtrSet<BasicBlock *, 2> DefBlocks;
141140
for (auto &Def : R.Defines)
@@ -165,7 +164,7 @@ void SSAUpdaterBulk::RewriteAllUses(DominatorTree *DT,
165164
}
166165

167166
// Fill in arguments of the inserted PHIs.
168-
for (auto PN : InsertedPHIsForVar) {
167+
for (auto *PN : InsertedPHIsForVar) {
169168
BasicBlock *PBB = PN->getParent();
170169
for (BasicBlock *Pred : PredCache.get(PBB))
171170
PN->addIncoming(computeValueAt(Pred, R, DT), Pred);
@@ -182,8 +181,8 @@ void SSAUpdaterBulk::RewriteAllUses(DominatorTree *DT,
182181
// Notify that users of the existing value that it is being replaced.
183182
if (OldVal != V && OldVal->hasValueHandle())
184183
ValueHandleBase::ValueIsRAUWd(OldVal, V);
185-
DEBUG(dbgs() << "SSAUpdater: Var=" << P.first << ": replacing" << *OldVal
186-
<< " with " << *V << "\n");
184+
DEBUG(dbgs() << "SSAUpdater: replacing " << *OldVal << " with " << *V
185+
<< "\n");
187186
U->set(V);
188187
}
189188
}

unittests/Transforms/Utils/SSAUpdaterBulk.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,17 @@ TEST(SSAUpdaterBulk, SimpleMerge) {
7373
// SSAUpdater should insert into %merge.
7474
// Intentionally don't touch %8 to see that SSAUpdater only changes
7575
// instructions that were explicitly specified.
76-
Updater.AddVariable(0, "a", I32Ty);
77-
Updater.AddAvailableValue(0, TrueBB, AddOp1);
78-
Updater.AddAvailableValue(0, FalseBB, AddOp2);
79-
Updater.AddUse(0, &I1->getOperandUse(0));
80-
Updater.AddUse(0, &I2->getOperandUse(0));
81-
82-
Updater.AddVariable(1, "b", I32Ty);
83-
Updater.AddAvailableValue(1, TrueBB, SubOp1);
84-
Updater.AddAvailableValue(1, FalseBB, SubOp2);
85-
Updater.AddUse(1, &I3->getOperandUse(0));
86-
Updater.AddUse(1, &I3->getOperandUse(1));
76+
unsigned VarNum = Updater.AddVariable("a", I32Ty);
77+
Updater.AddAvailableValue(VarNum, TrueBB, AddOp1);
78+
Updater.AddAvailableValue(VarNum, FalseBB, AddOp2);
79+
Updater.AddUse(VarNum, &I1->getOperandUse(0));
80+
Updater.AddUse(VarNum, &I2->getOperandUse(0));
81+
82+
VarNum = Updater.AddVariable("b", I32Ty);
83+
Updater.AddAvailableValue(VarNum, TrueBB, SubOp1);
84+
Updater.AddAvailableValue(VarNum, FalseBB, SubOp2);
85+
Updater.AddUse(VarNum, &I3->getOperandUse(0));
86+
Updater.AddUse(VarNum, &I3->getOperandUse(1));
8787

8888
DominatorTree DT(*F);
8989
Updater.RewriteAllUses(&DT);
@@ -161,19 +161,19 @@ TEST(SSAUpdaterBulk, Irreducible) {
161161
// No other rewrites should be made.
162162

163163
// Add use in %3.
164-
Updater.AddVariable(0, "c", I32Ty);
165-
Updater.AddAvailableValue(0, IfBB, AddOp1);
166-
Updater.AddUse(0, &I1->getOperandUse(0));
164+
unsigned VarNum = Updater.AddVariable("c", I32Ty);
165+
Updater.AddAvailableValue(VarNum, IfBB, AddOp1);
166+
Updater.AddUse(VarNum, &I1->getOperandUse(0));
167167

168168
// Add use in %4.
169-
Updater.AddVariable(1, "b", I32Ty);
170-
Updater.AddAvailableValue(1, LoopStartBB, AddOp2);
171-
Updater.AddUse(1, &I2->getOperandUse(0));
169+
VarNum = Updater.AddVariable("b", I32Ty);
170+
Updater.AddAvailableValue(VarNum, LoopStartBB, AddOp2);
171+
Updater.AddUse(VarNum, &I2->getOperandUse(0));
172172

173173
// Add use in the return instruction.
174-
Updater.AddVariable(2, "a", I32Ty);
175-
Updater.AddAvailableValue(2, &F->getEntryBlock(), FirstArg);
176-
Updater.AddUse(2, &Return->getOperandUse(0));
174+
VarNum = Updater.AddVariable("a", I32Ty);
175+
Updater.AddAvailableValue(VarNum, &F->getEntryBlock(), FirstArg);
176+
Updater.AddUse(VarNum, &Return->getOperandUse(0));
177177

178178
// Save all inserted phis into a vector.
179179
SmallVector<PHINode *, 8> Inserted;

0 commit comments

Comments
 (0)