1 //===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass is a simple pass wrapper around the PromoteMemToReg function call
11 // exposed by the Utils library.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/Utils/Mem2Reg.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/Analysis/AssumptionCache.h"
18 #include "llvm/IR/BasicBlock.h"
19 #include "llvm/IR/Dominators.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Transforms/Scalar.h"
26 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
31 #define DEBUG_TYPE "mem2reg"
33 STATISTIC(NumPromoted, "Number of alloca's promoted");
35 static bool promoteMemoryToRegister(Function &F, DominatorTree &DT,
36 AssumptionCache &AC) {
37 std::vector<AllocaInst *> Allocas;
38 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
44 // Find allocas that are safe to promote, by looking at all instructions in
46 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
47 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
48 if (isAllocaPromotable(AI))
49 Allocas.push_back(AI);
54 PromoteMemToReg(Allocas, DT, &AC);
55 NumPromoted += Allocas.size();
61 PreservedAnalyses PromotePass::run(Function &F, FunctionAnalysisManager &AM) {
62 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
63 auto &AC = AM.getResult<AssumptionAnalysis>(F);
64 if (!promoteMemoryToRegister(F, DT, AC))
65 return PreservedAnalyses::all();
68 PA.preserveSet<CFGAnalyses>();
74 struct PromoteLegacyPass : public FunctionPass {
75 // Pass identification, replacement for typeid
78 PromoteLegacyPass() : FunctionPass(ID) {
79 initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
82 // runOnFunction - To run this pass, first we calculate the alloca
83 // instructions that are safe for promotion, then we promote each one.
84 bool runOnFunction(Function &F) override {
88 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
90 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
91 return promoteMemoryToRegister(F, DT, AC);
94 void getAnalysisUsage(AnalysisUsage &AU) const override {
95 AU.addRequired<AssumptionCacheTracker>();
96 AU.addRequired<DominatorTreeWrapperPass>();
101 } // end anonymous namespace
103 char PromoteLegacyPass::ID = 0;
105 INITIALIZE_PASS_BEGIN(PromoteLegacyPass, "mem2reg", "Promote Memory to "
108 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
109 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
110 INITIALIZE_PASS_END(PromoteLegacyPass, "mem2reg", "Promote Memory to Register",
113 // createPromoteMemoryToRegister - Provide an entry point to create this pass.
114 FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
115 return new PromoteLegacyPass();