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