LLVM API Documentation
00001 //===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This pass is a simple pass wrapper around the PromoteMemToReg function call 00011 // exposed by the Utils library. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "mem2reg" 00016 #include "llvm/Transforms/Scalar.h" 00017 #include "llvm/Transforms/Utils/PromoteMemToReg.h" 00018 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" 00019 #include "llvm/Analysis/Dominators.h" 00020 #include "llvm/Instructions.h" 00021 #include "llvm/Function.h" 00022 #include "llvm/ADT/Statistic.h" 00023 #include "llvm/Support/Compiler.h" 00024 using namespace llvm; 00025 00026 STATISTIC(NumPromoted, "Number of alloca's promoted"); 00027 00028 namespace { 00029 struct VISIBILITY_HIDDEN PromotePass : public FunctionPass { 00030 static char ID; // Pass identification, replacement for typeid 00031 PromotePass() : FunctionPass(&ID) {} 00032 00033 // runOnFunction - To run this pass, first we calculate the alloca 00034 // instructions that are safe for promotion, then we promote each one. 00035 // 00036 virtual bool runOnFunction(Function &F); 00037 00038 // getAnalysisUsage - We need dominance frontiers 00039 // 00040 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00041 AU.addRequired<DominatorTree>(); 00042 AU.addRequired<DominanceFrontier>(); 00043 AU.setPreservesCFG(); 00044 // This is a cluster of orthogonal Transforms 00045 AU.addPreserved<UnifyFunctionExitNodes>(); 00046 AU.addPreservedID(LowerSwitchID); 00047 AU.addPreservedID(LowerInvokePassID); 00048 AU.addPreservedID(LowerAllocationsID); 00049 } 00050 }; 00051 } // end of anonymous namespace 00052 00053 char PromotePass::ID = 0; 00054 static RegisterPass<PromotePass> X("mem2reg", "Promote Memory to Register"); 00055 00056 bool PromotePass::runOnFunction(Function &F) { 00057 std::vector<AllocaInst*> Allocas; 00058 00059 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function 00060 00061 bool Changed = false; 00062 00063 DominatorTree &DT = getAnalysis<DominatorTree>(); 00064 DominanceFrontier &DF = getAnalysis<DominanceFrontier>(); 00065 00066 while (1) { 00067 Allocas.clear(); 00068 00069 // Find allocas that are safe to promote, by looking at all instructions in 00070 // the entry node 00071 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) 00072 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? 00073 if (isAllocaPromotable(AI)) 00074 Allocas.push_back(AI); 00075 00076 if (Allocas.empty()) break; 00077 00078 PromoteMemToReg(Allocas, DT, DF); 00079 NumPromoted += Allocas.size(); 00080 Changed = true; 00081 } 00082 00083 return Changed; 00084 } 00085 00086 // Publically exposed interface to pass... 00087 const PassInfo *const llvm::PromoteMemoryToRegisterID = &X; 00088 // createPromoteMemoryToRegister - Provide an entry point to create this pass. 00089 // 00090 FunctionPass *llvm::createPromoteMemoryToRegisterPass() { 00091 return new PromotePass(); 00092 }