X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTransforms%2FUtils%2FPromoteMemoryToRegister.cpp;h=b84f1a378fa2970409e05a66e87ced6e050f01d7;hb=d76efa018660e806cd87c0a24512e3c532fc1d36;hp=8679701e6a7d0233cd27040e2758cfd0ab233b95;hpb=b9ddce65c281f023780d2b6578e7ed6d2913a2cb;p=oota-llvm.git diff --git a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp index 8679701e6a7..b84f1a378fa 100644 --- a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp +++ b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp @@ -17,15 +17,18 @@ // //===----------------------------------------------------------------------===// -#include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h" +#include "llvm/Transforms/Scalar.h" #include "llvm/Analysis/Dominators.h" #include "llvm/iMemory.h" #include "llvm/iPHINode.h" #include "llvm/iTerminators.h" -#include "llvm/Pass.h" #include "llvm/Function.h" #include "llvm/BasicBlock.h" -#include "llvm/ConstantVals.h" +#include "llvm/Constant.h" +#include "llvm/Type.h" +#include "Support/StatisticReporter.h" + +static Statistic<> NumPromoted("mem2reg\t\t- Number of alloca's promoted"); using std::vector; using std::map; @@ -47,21 +50,23 @@ namespace { // runOnFunction - To run this pass, first we calculate the alloca // instructions that are safe for promotion, then we promote each one. // - virtual bool runOnFunction(Function *F); + virtual bool runOnFunction(Function &F); // getAnalysisUsage - We need dominance frontiers // virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired(DominanceFrontier::ID); + AU.addRequired(); + AU.preservesCFG(); } private: void Traverse(BasicBlock *BB, BasicBlock *Pred, vector &IncVals, set &Visited); bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx); - void FindSafeAllocas(Function *F); + void FindSafeAllocas(Function &F); }; + RegisterOpt X("mem2reg", "Promote Memory to Register"); } // end of anonymous namespace @@ -71,34 +76,23 @@ namespace { static inline bool isSafeAlloca(const AllocaInst *AI) { if (AI->isArrayAllocation()) return false; + // Only allow direct loads and stores... for (Value::use_const_iterator UI = AI->use_begin(), UE = AI->use_end(); - UI != UE; ++UI) { // Loop over all of the uses of the alloca - - // Only allow nonindexed memory access instructions... - if (MemAccessInst *MAI = dyn_cast(*UI)) { - if (MAI->hasIndices()) { // indexed? - // Allow the access if there is only one index and the index is - // zero. - if (*MAI->idx_begin() != ConstantUInt::get(Type::UIntTy, 0) || - MAI->idx_begin()+1 != MAI->idx_end()) - return false; - } - } else { + UI != UE; ++UI) // Loop over all of the uses of the alloca + if (!isa(*UI) && !isa(*UI)) return false; // Not a load or store? - } - } return true; } // FindSafeAllocas - Find allocas that are safe to promote // -void PromotePass::FindSafeAllocas(Function *F) { - BasicBlock *BB = F->getEntryNode(); // Get the entry node for the function +void PromotePass::FindSafeAllocas(Function &F) { + BasicBlock &BB = F.getEntryNode(); // Get the entry node for the function // Look at all instructions in the entry node - for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) - if (AllocaInst *AI = dyn_cast(*I)) // Is it an alloca? + for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) + if (AllocaInst *AI = dyn_cast(&*I)) // Is it an alloca? if (isSafeAlloca(AI)) { // If safe alloca, add alloca to safe list AllocaLookup[AI] = Allocas.size(); // Keep reverse mapping Allocas.push_back(AI); @@ -107,7 +101,7 @@ void PromotePass::FindSafeAllocas(Function *F) { -bool PromotePass::runOnFunction(Function *F) { +bool PromotePass::runOnFunction(Function &F) { // Calculate the set of safe allocas FindSafeAllocas(F); @@ -163,13 +157,13 @@ bool PromotePass::runOnFunction(Function *F) { // vector Values(Allocas.size()); for (unsigned i = 0, e = Allocas.size(); i != e; ++i) - Values[i] = Constant::getNullValue(Allocas[i]->getType()->getElementType()); + Values[i] = Constant::getNullValue(Allocas[i]->getAllocatedType()); // Walks all basic blocks in the function performing the SSA rename algorithm // and inserting the phi nodes we marked as necessary // set Visited; // The basic blocks we've already visited - Traverse(F->front(), 0, Values, Visited); + Traverse(F.begin(), 0, Values, Visited); // Remove all instructions marked by being placed in the KillList... // @@ -177,10 +171,11 @@ bool PromotePass::runOnFunction(Function *F) { Instruction *I = KillList.back(); KillList.pop_back(); - I->getParent()->getInstList().remove(I); - delete I; + I->getParent()->getInstList().erase(I); } + NumPromoted += Allocas.size(); + // Purge data structurse so they are available the next iteration... Allocas.clear(); AllocaLookup.clear(); @@ -201,14 +196,12 @@ bool PromotePass::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo) { // If the BB already has a phi node added for the i'th alloca then we're done! if (BBPNs[AllocaNo]) return false; - // Create a PhiNode using the dereferenced type... - PHINode *PN = new PHINode(Allocas[AllocaNo]->getType()->getElementType(), - Allocas[AllocaNo]->getName()+".mem2reg"); + // Create a PhiNode using the dereferenced type... and add the phi-node to the + // BasicBlock + PHINode *PN = new PHINode(Allocas[AllocaNo]->getAllocatedType(), + Allocas[AllocaNo]->getName()+".mem2reg", + BB->begin()); BBPNs[AllocaNo] = PN; - - // Add the phi-node to the basic-block - BB->getInstList().push_front(PN); - PhiNodes[AllocaNo].push_back(BB); return true; } @@ -237,7 +230,7 @@ void PromotePass::Traverse(BasicBlock *BB, BasicBlock *Pred, // 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 + Instruction *I = II; // get the instruction if (LoadInst *LI = dyn_cast(I)) { Value *Ptr = LI->getPointerOperand();