1*0b57cec5SDimitry Andric //===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This pass is a simple pass wrapper around the PromoteMemToReg function call
10*0b57cec5SDimitry Andric // exposed by the Utils library.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric
14*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/Mem2Reg.h"
15*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
16*0b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
17*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
18*0b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
19*0b57cec5SDimitry Andric #include "llvm/IR/Function.h"
20*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
21*0b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
22*0b57cec5SDimitry Andric #include "llvm/InitializePasses.h"
23*0b57cec5SDimitry Andric #include "llvm/Pass.h"
24*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
25*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils.h"
26*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/PromoteMemToReg.h"
27*0b57cec5SDimitry Andric #include <vector>
28*0b57cec5SDimitry Andric
29*0b57cec5SDimitry Andric using namespace llvm;
30*0b57cec5SDimitry Andric
31*0b57cec5SDimitry Andric #define DEBUG_TYPE "mem2reg"
32*0b57cec5SDimitry Andric
33*0b57cec5SDimitry Andric STATISTIC(NumPromoted, "Number of alloca's promoted");
34*0b57cec5SDimitry Andric
promoteMemoryToRegister(Function & F,DominatorTree & DT,AssumptionCache & AC)35*0b57cec5SDimitry Andric static bool promoteMemoryToRegister(Function &F, DominatorTree &DT,
36*0b57cec5SDimitry Andric AssumptionCache &AC) {
37*0b57cec5SDimitry Andric std::vector<AllocaInst *> Allocas;
38*0b57cec5SDimitry Andric BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
39*0b57cec5SDimitry Andric bool Changed = false;
40*0b57cec5SDimitry Andric
41*0b57cec5SDimitry Andric while (true) {
42*0b57cec5SDimitry Andric Allocas.clear();
43*0b57cec5SDimitry Andric
44*0b57cec5SDimitry Andric // Find allocas that are safe to promote, by looking at all instructions in
45*0b57cec5SDimitry Andric // the entry node
46*0b57cec5SDimitry Andric for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
47*0b57cec5SDimitry Andric if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
48*0b57cec5SDimitry Andric if (isAllocaPromotable(AI))
49*0b57cec5SDimitry Andric Allocas.push_back(AI);
50*0b57cec5SDimitry Andric
51*0b57cec5SDimitry Andric if (Allocas.empty())
52*0b57cec5SDimitry Andric break;
53*0b57cec5SDimitry Andric
54*0b57cec5SDimitry Andric PromoteMemToReg(Allocas, DT, &AC);
55*0b57cec5SDimitry Andric NumPromoted += Allocas.size();
56*0b57cec5SDimitry Andric Changed = true;
57*0b57cec5SDimitry Andric }
58*0b57cec5SDimitry Andric return Changed;
59*0b57cec5SDimitry Andric }
60*0b57cec5SDimitry Andric
run(Function & F,FunctionAnalysisManager & AM)61*0b57cec5SDimitry Andric PreservedAnalyses PromotePass::run(Function &F, FunctionAnalysisManager &AM) {
62*0b57cec5SDimitry Andric auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
63*0b57cec5SDimitry Andric auto &AC = AM.getResult<AssumptionAnalysis>(F);
64*0b57cec5SDimitry Andric if (!promoteMemoryToRegister(F, DT, AC))
65*0b57cec5SDimitry Andric return PreservedAnalyses::all();
66*0b57cec5SDimitry Andric
67*0b57cec5SDimitry Andric PreservedAnalyses PA;
68*0b57cec5SDimitry Andric PA.preserveSet<CFGAnalyses>();
69*0b57cec5SDimitry Andric return PA;
70*0b57cec5SDimitry Andric }
71*0b57cec5SDimitry Andric
72*0b57cec5SDimitry Andric namespace {
73*0b57cec5SDimitry Andric
74*0b57cec5SDimitry Andric struct PromoteLegacyPass : public FunctionPass {
75*0b57cec5SDimitry Andric // Pass identification, replacement for typeid
76*0b57cec5SDimitry Andric static char ID;
77*0b57cec5SDimitry Andric
PromoteLegacyPass__anon46ea1e710111::PromoteLegacyPass78*0b57cec5SDimitry Andric PromoteLegacyPass() : FunctionPass(ID) {
79*0b57cec5SDimitry Andric initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
80*0b57cec5SDimitry Andric }
81*0b57cec5SDimitry Andric
82*0b57cec5SDimitry Andric // runOnFunction - To run this pass, first we calculate the alloca
83*0b57cec5SDimitry Andric // instructions that are safe for promotion, then we promote each one.
runOnFunction__anon46ea1e710111::PromoteLegacyPass84*0b57cec5SDimitry Andric bool runOnFunction(Function &F) override {
85*0b57cec5SDimitry Andric if (skipFunction(F))
86*0b57cec5SDimitry Andric return false;
87*0b57cec5SDimitry Andric
88*0b57cec5SDimitry Andric DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
89*0b57cec5SDimitry Andric AssumptionCache &AC =
90*0b57cec5SDimitry Andric getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
91*0b57cec5SDimitry Andric return promoteMemoryToRegister(F, DT, AC);
92*0b57cec5SDimitry Andric }
93*0b57cec5SDimitry Andric
getAnalysisUsage__anon46ea1e710111::PromoteLegacyPass94*0b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
95*0b57cec5SDimitry Andric AU.addRequired<AssumptionCacheTracker>();
96*0b57cec5SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>();
97*0b57cec5SDimitry Andric AU.setPreservesCFG();
98*0b57cec5SDimitry Andric }
99*0b57cec5SDimitry Andric };
100*0b57cec5SDimitry Andric
101*0b57cec5SDimitry Andric } // end anonymous namespace
102*0b57cec5SDimitry Andric
103*0b57cec5SDimitry Andric char PromoteLegacyPass::ID = 0;
104*0b57cec5SDimitry Andric
105*0b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(PromoteLegacyPass, "mem2reg", "Promote Memory to "
106*0b57cec5SDimitry Andric "Register",
107*0b57cec5SDimitry Andric false, false)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)108*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
109*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
110*0b57cec5SDimitry Andric INITIALIZE_PASS_END(PromoteLegacyPass, "mem2reg", "Promote Memory to Register",
111*0b57cec5SDimitry Andric false, false)
112*0b57cec5SDimitry Andric
113*0b57cec5SDimitry Andric // createPromoteMemoryToRegister - Provide an entry point to create this pass.
114*0b57cec5SDimitry Andric FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
115*0b57cec5SDimitry Andric return new PromoteLegacyPass();
116 }
117