* Minor cleanups

* Eliminate the KillList instance variable, instead, just delete loads and
  stores as they are "renamed", and delete allocas when they are done
* Make the 'visited' set an instance variable to avoid passing it on the stack.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@8857 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2003-10-05 01:52:53 +00:00
parent 27db7e0472
commit 0fa157127f

View File

@ -51,7 +51,7 @@ bool isAllocaPromotable(const AllocaInst *AI, const TargetData &TD) {
namespace { namespace {
struct PromoteMem2Reg { struct PromoteMem2Reg {
const std::vector<AllocaInst*> &Allocas; // the alloca instructions.. const std::vector<AllocaInst*> &Allocas; // the alloca instructions..
std::vector<unsigned> VersionNumbers; // Current version counters std::vector<unsigned> VersionNumbers; // Current version counters
DominanceFrontier &DF; DominanceFrontier &DF;
const TargetData &TD; const TargetData &TD;
@ -60,33 +60,29 @@ namespace {
std::vector<std::vector<BasicBlock*> > PhiNodes;// Idx corresponds 2 Allocas std::vector<std::vector<BasicBlock*> > PhiNodes;// Idx corresponds 2 Allocas
// List of instructions to remove at end of pass // NewPhiNodes - The PhiNodes we're adding.
std::vector<Instruction *> KillList; std::map<BasicBlock*, std::vector<PHINode*> > NewPhiNodes;
std::map<BasicBlock*, // Visited - The set of basic blocks the renamer has already visited.
std::vector<PHINode*> > NewPhiNodes; // the PhiNodes we're adding std::set<BasicBlock*> Visited;
public: public:
PromoteMem2Reg(const std::vector<AllocaInst*> &A, DominanceFrontier &df, PromoteMem2Reg(const std::vector<AllocaInst*> &A, DominanceFrontier &df,
const TargetData &td) const TargetData &td) : Allocas(A), DF(df), TD(td) {}
: Allocas(A), DF(df), TD(td) {}
void run(); void run();
private: private:
void RenamePass(BasicBlock *BB, BasicBlock *Pred, void RenamePass(BasicBlock *BB, BasicBlock *Pred,
std::vector<Value*> &IncVals, std::vector<Value*> &IncVals);
std::set<BasicBlock*> &Visited);
bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx); bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx);
}; };
} // end of anonymous namespace } // end of anonymous namespace
void PromoteMem2Reg::run() { void PromoteMem2Reg::run() {
// If there is nothing to do, bail out...
if (Allocas.empty()) return;
Function &F = *DF.getRoot()->getParent(); Function &F = *DF.getRoot()->getParent();
VersionNumbers.resize(Allocas.size()); VersionNumbers.resize(Allocas.size());
for (unsigned i = 0, e = Allocas.size(); i != e; ++i) { for (unsigned i = 0, e = Allocas.size(); i != e; ++i) {
@ -97,11 +93,6 @@ void PromoteMem2Reg::run() {
AllocaLookup[Allocas[i]] = i; AllocaLookup[Allocas[i]] = i;
} }
// Add each alloca to the KillList. Note: KillList is destroyed MOST recently
// added to least recently.
KillList.assign(Allocas.begin(), Allocas.end());
// Calculate the set of write-locations for each alloca. This is analogous to // Calculate the set of write-locations for each alloca. This is analogous to
// counting the number of 'redefinitions' of each variable. // counting the number of 'redefinitions' of each variable.
std::vector<std::vector<BasicBlock*> > WriteSets;// Idx corresponds to Allocas std::vector<std::vector<BasicBlock*> > WriteSets;// Idx corresponds to Allocas
@ -153,27 +144,20 @@ void PromoteMem2Reg::run() {
// Walks all basic blocks in the function performing the SSA rename algorithm // Walks all basic blocks in the function performing the SSA rename algorithm
// and inserting the phi nodes we marked as necessary // and inserting the phi nodes we marked as necessary
// //
std::set<BasicBlock*> Visited; // The basic blocks we've already visited RenamePass(F.begin(), 0, Values);
RenamePass(F.begin(), 0, Values, Visited); Visited.clear();
// Remove all instructions marked by being placed in the KillList... // Remove the allocas themselves from the function...
// for (unsigned i = 0, e = Allocas.size(); i != e; ++i) {
while (!KillList.empty()) { Instruction *A = Allocas[i];
Instruction *I = KillList.back();
KillList.pop_back();
// If there are any uses of these instructions left, they must be in // If there are any uses of the alloca instructions left, they must be in
// sections of dead code that were not processed on the dominance frontier. // sections of dead code that were not processed on the dominance frontier.
// Just delete the users now. // Just delete the users now.
// //
while (!I->use_empty()) { if (!A->use_empty())
Instruction *U = cast<Instruction>(I->use_back()); A->replaceAllUsesWith(Constant::getNullValue(A->getType()));
if (!U->use_empty()) // If uses remain in dead code segment... A->getParent()->getInstList().erase(A);
U->replaceAllUsesWith(Constant::getNullValue(U->getType()));
U->getParent()->getInstList().erase(U);
}
I->getParent()->getInstList().erase(I);
} }
} }
@ -215,8 +199,7 @@ bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo) {
} }
void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred, void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
std::vector<Value*> &IncomingVals, std::vector<Value*> &IncomingVals) {
std::set<BasicBlock*> &Visited) {
// If this is a BB needing a phi node, lookup/create the phinode for each // If this is a BB needing a phi node, lookup/create the phinode for each
// variable we need phinodes for. // variable we need phinodes for.
std::vector<PHINode *> &BBPNs = NewPhiNodes[BB]; std::vector<PHINode *> &BBPNs = NewPhiNodes[BB];
@ -240,9 +223,9 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
// mark as visited // mark as visited
Visited.insert(BB); Visited.insert(BB);
// keep track of the value of each variable we're watching.. how? BasicBlock::iterator II = BB->begin();
for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) { while (1) {
Instruction *I = II; // get the instruction Instruction *I = II++; // get the instruction, increment iterator
if (LoadInst *LI = dyn_cast<LoadInst>(I)) { if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
if (AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand())) { if (AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand())) {
@ -252,7 +235,7 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
// walk the use list of this load and replace all uses with r // walk the use list of this load and replace all uses with r
LI->replaceAllUsesWith(V); LI->replaceAllUsesWith(V);
KillList.push_back(LI); // Mark the load to be deleted BB->getInstList().erase(LI);
} }
} }
} else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
@ -263,7 +246,7 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
if (ai != AllocaLookup.end()) { if (ai != AllocaLookup.end()) {
// what value were we writing? // what value were we writing?
IncomingVals[ai->second] = SI->getOperand(0); IncomingVals[ai->second] = SI->getOperand(0);
KillList.push_back(SI); // Mark the store to be deleted BB->getInstList().erase(SI);
} }
} }
@ -271,8 +254,9 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
// Recurse across our successors // Recurse across our successors
for (unsigned i = 0; i != TI->getNumSuccessors(); i++) { for (unsigned i = 0; i != TI->getNumSuccessors(); i++) {
std::vector<Value*> OutgoingVals(IncomingVals); std::vector<Value*> OutgoingVals(IncomingVals);
RenamePass(TI->getSuccessor(i), BB, OutgoingVals, Visited); RenamePass(TI->getSuccessor(i), BB, OutgoingVals);
} }
break;
} }
} }
} }
@ -284,5 +268,7 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
/// ///
void PromoteMemToReg(const std::vector<AllocaInst*> &Allocas, void PromoteMemToReg(const std::vector<AllocaInst*> &Allocas,
DominanceFrontier &DF, const TargetData &TD) { DominanceFrontier &DF, const TargetData &TD) {
// If there is nothing to do, bail out...
if (Allocas.empty()) return;
PromoteMem2Reg(Allocas, DF, TD).run(); PromoteMem2Reg(Allocas, DF, TD).run();
} }