1 //===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This pass is a simple pass wrapper around the PromoteMemToReg function call 11 // exposed by the Utils library. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Transforms/Utils/Mem2Reg.h" 16 #include "llvm/ADT/Statistic.h" 17 #include "llvm/IR/Dominators.h" 18 #include "llvm/IR/Function.h" 19 #include "llvm/IR/Instructions.h" 20 #include "llvm/Transforms/Scalar.h" 21 #include "llvm/Transforms/Utils/PromoteMemToReg.h" 22 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" 23 using namespace llvm; 24 25 #define DEBUG_TYPE "mem2reg" 26 27 STATISTIC(NumPromoted, "Number of alloca's promoted"); 28 29 static bool promoteMemoryToRegister(Function &F, DominatorTree &DT) { 30 std::vector<AllocaInst *> Allocas; 31 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function 32 bool Changed = false; 33 34 while (1) { 35 Allocas.clear(); 36 37 // Find allocas that are safe to promote, by looking at all instructions in 38 // the entry node 39 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) 40 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? 41 if (isAllocaPromotable(AI)) 42 Allocas.push_back(AI); 43 44 if (Allocas.empty()) 45 break; 46 47 PromoteMemToReg(Allocas, DT, nullptr); 48 NumPromoted += Allocas.size(); 49 Changed = true; 50 } 51 return Changed; 52 } 53 54 PreservedAnalyses PromotePass::run(Function &F, FunctionAnalysisManager &AM) { 55 auto &DT = AM.getResult<DominatorTreeAnalysis>(F); 56 if (!promoteMemoryToRegister(F, DT)) 57 return PreservedAnalyses::all(); 58 59 // FIXME: This should also 'preserve the CFG'. 60 return PreservedAnalyses::none(); 61 } 62 63 namespace { 64 struct PromoteLegacyPass : public FunctionPass { 65 static char ID; // Pass identification, replacement for typeid 66 PromoteLegacyPass() : FunctionPass(ID) { 67 initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry()); 68 } 69 70 // runOnFunction - To run this pass, first we calculate the alloca 71 // instructions that are safe for promotion, then we promote each one. 72 // 73 bool runOnFunction(Function &F) override { 74 if (skipFunction(F)) 75 return false; 76 77 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 78 return promoteMemoryToRegister(F, DT); 79 } 80 81 void getAnalysisUsage(AnalysisUsage &AU) const override { 82 AU.addRequired<DominatorTreeWrapperPass>(); 83 AU.setPreservesCFG(); 84 } 85 }; 86 } // end of anonymous namespace 87 88 char PromoteLegacyPass::ID = 0; 89 INITIALIZE_PASS_BEGIN(PromoteLegacyPass, "mem2reg", "Promote Memory to " 90 "Register", 91 false, false) 92 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 93 INITIALIZE_PASS_END(PromoteLegacyPass, "mem2reg", "Promote Memory to Register", 94 false, false) 95 96 // createPromoteMemoryToRegister - Provide an entry point to create this pass. 97 // 98 FunctionPass *llvm::createPromoteMemoryToRegisterPass() { 99 return new PromoteLegacyPass(); 100 } 101