[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / lib / Transforms / Utils / Mem2Reg.cpp
blob5ad7aeb463ecb201df81f1e456b04f0f735bc81b
1 //===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass is a simple pass wrapper around the PromoteMemToReg function call
10 // exposed by the Utils library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Utils/Mem2Reg.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/Analysis/AssumptionCache.h"
17 #include "llvm/IR/BasicBlock.h"
18 #include "llvm/IR/Dominators.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/PassManager.h"
22 #include "llvm/InitializePasses.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Transforms/Utils.h"
26 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
27 #include <vector>
29 using namespace llvm;
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
39 bool Changed = false;
41 while (true) {
42 Allocas.clear();
44 // Find allocas that are safe to promote, by looking at all instructions in
45 // the entry node
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);
51 if (Allocas.empty())
52 break;
54 PromoteMemToReg(Allocas, DT, &AC);
55 NumPromoted += Allocas.size();
56 Changed = true;
58 return Changed;
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();
67 PreservedAnalyses PA;
68 PA.preserveSet<CFGAnalyses>();
69 return PA;
72 namespace {
74 struct PromoteLegacyPass : public FunctionPass {
75 // Pass identification, replacement for typeid
76 static char ID;
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 {
85 if (skipFunction(F))
86 return false;
88 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
89 AssumptionCache &AC =
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>();
97 AU.setPreservesCFG();
101 } // end anonymous namespace
103 char PromoteLegacyPass::ID = 0;
105 INITIALIZE_PASS_BEGIN(PromoteLegacyPass, "mem2reg", "Promote Memory to "
106 "Register",
107 false, false)
108 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
109 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
110 INITIALIZE_PASS_END(PromoteLegacyPass, "mem2reg", "Promote Memory to Register",
111 false, false)
113 // createPromoteMemoryToRegister - Provide an entry point to create this pass.
114 FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
115 return new PromoteLegacyPass();