From: Chris Lattner Date: Sun, 5 Oct 2003 01:52:53 +0000 (+0000) Subject: * Minor cleanups X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=0fa157127f1e58d0acfa6fbd687617629e6ebf43;p=oota-llvm.git * 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 --- diff --git a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp index d22451c6a13..96b795a1570 100644 --- a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp +++ b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp @@ -51,7 +51,7 @@ bool isAllocaPromotable(const AllocaInst *AI, const TargetData &TD) { namespace { struct PromoteMem2Reg { - const std::vector &Allocas; // the alloca instructions.. + const std::vector &Allocas; // the alloca instructions.. std::vector VersionNumbers; // Current version counters DominanceFrontier &DF; const TargetData &TD; @@ -60,33 +60,29 @@ namespace { std::vector > PhiNodes;// Idx corresponds 2 Allocas - // List of instructions to remove at end of pass - std::vector KillList; - - std::map > NewPhiNodes; // the PhiNodes we're adding + // NewPhiNodes - The PhiNodes we're adding. + std::map > NewPhiNodes; + + // Visited - The set of basic blocks the renamer has already visited. + std::set Visited; public: PromoteMem2Reg(const std::vector &A, DominanceFrontier &df, - const TargetData &td) - : Allocas(A), DF(df), TD(td) {} + const TargetData &td) : Allocas(A), DF(df), TD(td) {} void run(); private: void RenamePass(BasicBlock *BB, BasicBlock *Pred, - std::vector &IncVals, - std::set &Visited); + std::vector &IncVals); bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx); }; } // end of anonymous namespace void PromoteMem2Reg::run() { - // If there is nothing to do, bail out... - if (Allocas.empty()) return; - Function &F = *DF.getRoot()->getParent(); + VersionNumbers.resize(Allocas.size()); for (unsigned i = 0, e = Allocas.size(); i != e; ++i) { @@ -97,11 +93,6 @@ void PromoteMem2Reg::run() { 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 // counting the number of 'redefinitions' of each variable. std::vector > WriteSets;// Idx corresponds to Allocas @@ -153,27 +144,20 @@ void PromoteMem2Reg::run() { // Walks all basic blocks in the function performing the SSA rename algorithm // and inserting the phi nodes we marked as necessary // - std::set Visited; // The basic blocks we've already visited - RenamePass(F.begin(), 0, Values, Visited); + RenamePass(F.begin(), 0, Values); + Visited.clear(); - // Remove all instructions marked by being placed in the KillList... - // - while (!KillList.empty()) { - Instruction *I = KillList.back(); - KillList.pop_back(); + // Remove the allocas themselves from the function... + for (unsigned i = 0, e = Allocas.size(); i != e; ++i) { + Instruction *A = Allocas[i]; - // 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. // Just delete the users now. // - while (!I->use_empty()) { - Instruction *U = cast(I->use_back()); - if (!U->use_empty()) // If uses remain in dead code segment... - U->replaceAllUsesWith(Constant::getNullValue(U->getType())); - U->getParent()->getInstList().erase(U); - } - - I->getParent()->getInstList().erase(I); + if (!A->use_empty()) + A->replaceAllUsesWith(Constant::getNullValue(A->getType())); + A->getParent()->getInstList().erase(A); } } @@ -215,8 +199,7 @@ bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo) { } void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred, - std::vector &IncomingVals, - std::set &Visited) { + std::vector &IncomingVals) { // If this is a BB needing a phi node, lookup/create the phinode for each // variable we need phinodes for. std::vector &BBPNs = NewPhiNodes[BB]; @@ -240,9 +223,9 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred, // mark as visited Visited.insert(BB); - // keep track of the value of each variable we're watching.. how? - for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) { - Instruction *I = II; // get the instruction + BasicBlock::iterator II = BB->begin(); + while (1) { + Instruction *I = II++; // get the instruction, increment iterator if (LoadInst *LI = dyn_cast(I)) { if (AllocaInst *Src = dyn_cast(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 LI->replaceAllUsesWith(V); - KillList.push_back(LI); // Mark the load to be deleted + BB->getInstList().erase(LI); } } } else if (StoreInst *SI = dyn_cast(I)) { @@ -263,7 +246,7 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred, if (ai != AllocaLookup.end()) { // what value were we writing? 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 for (unsigned i = 0; i != TI->getNumSuccessors(); i++) { std::vector 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 &Allocas, DominanceFrontier &DF, const TargetData &TD) { + // If there is nothing to do, bail out... + if (Allocas.empty()) return; PromoteMem2Reg(Allocas, DF, TD).run(); }