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