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 #include "llvm/Transforms/Scalar.h" 00016 #include "llvm/ADT/Statistic.h" 00017 #include "llvm/Analysis/AssumptionTracker.h" 00018 #include "llvm/IR/Dominators.h" 00019 #include "llvm/IR/Function.h" 00020 #include "llvm/IR/Instructions.h" 00021 #include "llvm/Transforms/Utils/PromoteMemToReg.h" 00022 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" 00023 using namespace llvm; 00024 00025 #define DEBUG_TYPE "mem2reg" 00026 00027 STATISTIC(NumPromoted, "Number of alloca's promoted"); 00028 00029 namespace { 00030 struct PromotePass : public FunctionPass { 00031 static char ID; // Pass identification, replacement for typeid 00032 PromotePass() : FunctionPass(ID) { 00033 initializePromotePassPass(*PassRegistry::getPassRegistry()); 00034 } 00035 00036 // runOnFunction - To run this pass, first we calculate the alloca 00037 // instructions that are safe for promotion, then we promote each one. 00038 // 00039 bool runOnFunction(Function &F) override; 00040 00041 void getAnalysisUsage(AnalysisUsage &AU) const override { 00042 AU.addRequired<AssumptionTracker>(); 00043 AU.addRequired<DominatorTreeWrapperPass>(); 00044 AU.setPreservesCFG(); 00045 // This is a cluster of orthogonal Transforms 00046 AU.addPreserved<UnifyFunctionExitNodes>(); 00047 AU.addPreservedID(LowerSwitchID); 00048 AU.addPreservedID(LowerInvokePassID); 00049 } 00050 }; 00051 } // end of anonymous namespace 00052 00053 char PromotePass::ID = 0; 00054 INITIALIZE_PASS_BEGIN(PromotePass, "mem2reg", "Promote Memory to Register", 00055 false, false) 00056 INITIALIZE_PASS_DEPENDENCY(AssumptionTracker) 00057 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 00058 INITIALIZE_PASS_END(PromotePass, "mem2reg", "Promote Memory to Register", 00059 false, false) 00060 00061 bool PromotePass::runOnFunction(Function &F) { 00062 std::vector<AllocaInst*> Allocas; 00063 00064 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function 00065 00066 bool Changed = false; 00067 00068 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 00069 AssumptionTracker *AT = &getAnalysis<AssumptionTracker>(); 00070 00071 while (1) { 00072 Allocas.clear(); 00073 00074 // Find allocas that are safe to promote, by looking at all instructions in 00075 // the entry node 00076 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) 00077 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? 00078 if (isAllocaPromotable(AI)) 00079 Allocas.push_back(AI); 00080 00081 if (Allocas.empty()) break; 00082 00083 PromoteMemToReg(Allocas, DT, nullptr, AT); 00084 NumPromoted += Allocas.size(); 00085 Changed = true; 00086 } 00087 00088 return Changed; 00089 } 00090 00091 // createPromoteMemoryToRegister - Provide an entry point to create this pass. 00092 // 00093 FunctionPass *llvm::createPromoteMemoryToRegisterPass() { 00094 return new PromotePass(); 00095 }