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 was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source 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 #include "llvm/Transforms/Scalar.h" 00016 #include "llvm/Transforms/Utils/PromoteMemToReg.h" 00017 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" 00018 #include "llvm/Analysis/Dominators.h" 00019 #include "llvm/Instructions.h" 00020 #include "llvm/Function.h" 00021 #include "llvm/Target/TargetData.h" 00022 #include "llvm/ADT/Statistic.h" 00023 #include "llvm/Support/Visibility.h" 00024 using namespace llvm; 00025 00026 namespace { 00027 Statistic<> NumPromoted("mem2reg", "Number of alloca's promoted"); 00028 00029 struct VISIBILITY_HIDDEN PromotePass : public FunctionPass { 00030 // runOnFunction - To run this pass, first we calculate the alloca 00031 // instructions that are safe for promotion, then we promote each one. 00032 // 00033 virtual bool runOnFunction(Function &F); 00034 00035 // getAnalysisUsage - We need dominance frontiers 00036 // 00037 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00038 AU.addRequired<DominatorTree>(); 00039 AU.addRequired<DominanceFrontier>(); 00040 AU.addRequired<TargetData>(); 00041 AU.setPreservesCFG(); 00042 // This is a cluster of orthogonal Transforms 00043 AU.addPreserved<UnifyFunctionExitNodes>(); 00044 AU.addPreservedID(LowerSelectID); 00045 AU.addPreservedID(LowerSwitchID); 00046 AU.addPreservedID(LowerInvokePassID); 00047 AU.addPreservedID(LowerAllocationsID); 00048 } 00049 }; 00050 00051 RegisterOpt<PromotePass> X("mem2reg", "Promote Memory to Register"); 00052 } // end of anonymous namespace 00053 00054 bool PromotePass::runOnFunction(Function &F) { 00055 std::vector<AllocaInst*> Allocas; 00056 const TargetData &TD = getAnalysis<TargetData>(); 00057 00058 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function 00059 00060 bool Changed = false; 00061 00062 DominatorTree &DT = getAnalysis<DominatorTree>(); 00063 DominanceFrontier &DF = getAnalysis<DominanceFrontier>(); 00064 00065 while (1) { 00066 Allocas.clear(); 00067 00068 // Find allocas that are safe to promote, by looking at all instructions in 00069 // the entry node 00070 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) 00071 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? 00072 if (isAllocaPromotable(AI, TD)) 00073 Allocas.push_back(AI); 00074 00075 if (Allocas.empty()) break; 00076 00077 PromoteMemToReg(Allocas, DT, DF, TD); 00078 NumPromoted += Allocas.size(); 00079 Changed = true; 00080 } 00081 00082 return Changed; 00083 } 00084 00085 // Publically exposed interface to pass... 00086 const PassInfo *llvm::PromoteMemoryToRegisterID = X.getPassInfo(); 00087 // createPromoteMemoryToRegister - Provide an entry point to create this pass. 00088 // 00089 FunctionPass *llvm::createPromoteMemoryToRegisterPass() { 00090 return new PromotePass(); 00091 }