diff --git a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp index a56420f5b14..7dd2dcd2ae3 100644 --- a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp +++ b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp @@ -51,14 +51,20 @@ bool isAllocaPromotable(const AllocaInst *AI, const TargetData &TD) { namespace { struct PromoteMem2Reg { - const std::vector &Allocas; // the alloca instructions.. - std::vector VersionNumbers; // Current version counters + // Allocas - The alloca instructions being promoted + const std::vector &Allocas; DominanceFrontier &DF; const TargetData &TD; - std::map AllocaLookup; // reverse mapping of above + // AllocaLookup - Reverse mapping of Allocas + std::map AllocaLookup; + + // VersionNumbers - Current version counters for each alloca + std::vector VersionNumbers; - std::vector > PhiNodes;// Idx corresponds 2 Allocas + // PhiNodes - Each alloca contains a list of basic blocks which contain PHI + // nodes for the alloca. + std::vector > PhiNodes; // NewPhiNodes - The PhiNodes we're adding. std::map > NewPhiNodes; @@ -84,26 +90,25 @@ void PromoteMem2Reg::run() { Function &F = *DF.getRoot()->getParent(); VersionNumbers.resize(Allocas.size()); + PhiNodes.resize(Allocas.size()); - for (unsigned i = 0, e = Allocas.size(); i != e; ++i) { - assert(isAllocaPromotable(Allocas[i], TD) && + for (unsigned i = 0; i != Allocas.size(); ++i) { + AllocaInst *AI = Allocas[i]; + + assert(isAllocaPromotable(AI, TD) && "Cannot promote non-promotable alloca!"); assert(Allocas[i]->getParent()->getParent() == &F && "All allocas should be in the same function, which is same as DF!"); - AllocaLookup[Allocas[i]] = i; - } - - PhiNodes.resize(Allocas.size()); - for (unsigned i = 0; i != Allocas.size(); ++i) { - AllocaInst *AI = Allocas[i]; // Calculate the set of write-locations for each alloca. This is analogous // to counting the number of 'redefinitions' of each variable. std::vector WriteSets; for (Value::use_iterator U =AI->use_begin(), E = AI->use_end(); U != E; ++U) - if (StoreInst *SI = dyn_cast(*U)) + if (StoreInst *SI = dyn_cast(cast(*U))) // jot down the basic-block it came from WriteSets.push_back(SI->getParent()); + + AllocaLookup[Allocas[i]] = i; // Compute the locations where PhiNodes need to be inserted. Look at the // dominance frontier of EACH basic-block we have a write in. @@ -120,12 +125,13 @@ void PromoteMem2Reg::run() { } // Perform iterative step - for (unsigned k = 0; k != PhiNodes[i].size(); k++) { - DominanceFrontier::const_iterator it = DF.find(PhiNodes[i][k]); + std::vector &AllocaPhiNodes = PhiNodes[i]; + for (unsigned k = 0; k != AllocaPhiNodes.size(); k++) { + DominanceFrontier::const_iterator it = DF.find(AllocaPhiNodes[k]); if (it != DF.end()) { - const DominanceFrontier::DomSetType &S = it->second; - for (DominanceFrontier::DomSetType::iterator P = S.begin(),PE = S.end(); - P != PE; ++P) + const DominanceFrontier::DomSetType &S = it->second; + for (DominanceFrontier::DomSetType::iterator + P = S.begin(), PE = S.end(); P != PE; ++P) QueuePhiNode(*P, i); } } @@ -231,7 +237,7 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred, if (LoadInst *LI = dyn_cast(I)) { if (AllocaInst *Src = dyn_cast(LI->getPointerOperand())) { - std::map::iterator AI = AllocaLookup.find(Src); + std::map::iterator AI = AllocaLookup.find(Src); if (AI != AllocaLookup.end()) { Value *V = IncomingVals[AI->second]; @@ -244,7 +250,7 @@ void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred, // Delete this instruction and mark the name as the current holder of the // value if (AllocaInst *Dest = dyn_cast(SI->getPointerOperand())) { - std::map::iterator ai =AllocaLookup.find(Dest); + std::map::iterator ai = AllocaLookup.find(Dest); if (ai != AllocaLookup.end()) { // what value were we writing? IncomingVals[ai->second] = SI->getOperand(0);