llvm.org GIT mirror llvm / 84645e0
[PM] Port Mem2Reg to the new pass manager. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@272630 91177308-0d34-0410-b5e6-96231b3b80d8 Davide Italiano 3 years ago
7 changed file(s) with 100 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
266266 void initializePrintModulePassWrapperPass(PassRegistry&);
267267 void initializeProcessImplicitDefsPass(PassRegistry&);
268268 void initializeProfileSummaryInfoWrapperPassPass(PassRegistry &);
269 void initializePromotePassPass(PassRegistry&);
269 void initializePromoteLegacyPassPass(PassRegistry &);
270270 void initializePruneEHPass(PassRegistry&);
271271 void initializeReassociateLegacyPassPass(PassRegistry&);
272272 void initializeRegBankSelectPass(PassRegistry &);
0 //===- Mem2Reg.h - The -mem2reg pass, a wrapper around the Utils lib ------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass is a simple pass wrapper around the PromoteMemToReg function call
10 // exposed by the Utils library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TRANSFORMS_UTILS_MEM2REG_H
15 #define LLVM_TRANSFORMS_UTILS_MEM2REG_H
16
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/PassManager.h"
19
20 namespace llvm {
21 class PromotePass : public PassInfoMixin {
22 public:
23 PreservedAnalyses run(Function &F, AnalysisManager &AM);
24 };
25 }
26
27 #endif // LLVM_TRANSFORMS_UTILS_MEM2REG_H
8888 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
8989 #include "llvm/Transforms/Scalar/Sink.h"
9090 #include "llvm/Transforms/Utils/LCSSA.h"
91 #include "llvm/Transforms/Utils/Mem2Reg.h"
9192 #include "llvm/Transforms/Utils/MemorySSA.h"
93
9294 #include
9395
9496 using namespace llvm;
128128 FUNCTION_PASS("lower-expect", LowerExpectIntrinsicPass())
129129 FUNCTION_PASS("guard-widening", GuardWideningPass())
130130 FUNCTION_PASS("gvn", GVN())
131 FUNCTION_PASS("mem2reg", PromotePass())
131132 FUNCTION_PASS("memcpyopt", MemCpyOptPass())
132133 FUNCTION_PASS("mldst-motion", MergedLoadStoreMotionPass())
133134 FUNCTION_PASS("jump-threading", JumpThreadingPass())
1111 //
1212 //===----------------------------------------------------------------------===//
1313
14 #include "llvm/Transforms/Scalar.h"
14 #include "llvm/Transforms/Utils/Mem2Reg.h"
1515 #include "llvm/ADT/Statistic.h"
1616 #include "llvm/Analysis/AssumptionCache.h"
1717 #include "llvm/IR/Dominators.h"
1818 #include "llvm/IR/Function.h"
1919 #include "llvm/IR/Instructions.h"
20 #include "llvm/Transforms/Scalar.h"
2021 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
2122 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
2223 using namespace llvm;
2526
2627 STATISTIC(NumPromoted, "Number of alloca's promoted");
2728
28 namespace {
29 struct PromotePass : public FunctionPass {
30 static char ID; // Pass identification, replacement for typeid
31 PromotePass() : FunctionPass(ID) {
32 initializePromotePassPass(*PassRegistry::getPassRegistry());
33 }
34
35 // runOnFunction - To run this pass, first we calculate the alloca
36 // instructions that are safe for promotion, then we promote each one.
37 //
38 bool runOnFunction(Function &F) override;
39
40 void getAnalysisUsage(AnalysisUsage &AU) const override {
41 AU.addRequired();
42 AU.addRequired();
43 AU.setPreservesCFG();
44 // This is a cluster of orthogonal Transforms
45 AU.addPreserved();
46 AU.addPreservedID(LowerSwitchID);
47 AU.addPreservedID(LowerInvokePassID);
48 }
49 };
50 } // end of anonymous namespace
51
52 char PromotePass::ID = 0;
53 INITIALIZE_PASS_BEGIN(PromotePass, "mem2reg", "Promote Memory to Register",
54 false, false)
55 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
56 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
57 INITIALIZE_PASS_END(PromotePass, "mem2reg", "Promote Memory to Register",
58 false, false)
59
60 bool PromotePass::runOnFunction(Function &F) {
61 if (skipFunction(F))
62 return false;
63
64 std::vector Allocas;
65
66 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
67
68 bool Changed = false;
69
70 DominatorTree &DT = getAnalysis().getDomTree();
71 AssumptionCache &AC =
72 getAnalysis().getAssumptionCache(F);
29 static bool promoteMemoryToRegister(Function &F, DominatorTree &DT,
30 AssumptionCache &AC) {
31 std::vector Allocas;
32 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
33 bool Changed = false;
7334
7435 while (1) {
7536 Allocas.clear();
7738 // Find allocas that are safe to promote, by looking at all instructions in
7839 // the entry node
7940 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
80 if (AllocaInst *AI = dyn_cast(I)) // Is it an alloca?
41 if (AllocaInst *AI = dyn_cast(I)) // Is it an alloca?
8142 if (isAllocaPromotable(AI))
8243 Allocas.push_back(AI);
8344
84 if (Allocas.empty()) break;
45 if (Allocas.empty())
46 break;
8547
8648 PromoteMemToReg(Allocas, DT, nullptr, &AC);
8749 NumPromoted += Allocas.size();
8850 Changed = true;
8951 }
90
9152 return Changed;
9253 }
54
55 PreservedAnalyses PromotePass::run(Function &F, AnalysisManager &AM) {
56 auto &DT = AM.getResult(F);
57 auto &AC = AM.getResult(F);
58 if (!promoteMemoryToRegister(F, DT, AC))
59 return PreservedAnalyses::all();
60
61 // FIXME: This pass should preserve the CFG.
62 // There's currently no way to do it in the new PM.
63 // In the old PM this pass preserved a fair amount of "orthogonal"
64 // transformation passes. This concept has no sense in the new PM,
65 // therefore we don't preserve them here.
66 return PreservedAnalyses::none();
67 }
68
69 namespace {
70 struct PromoteLegacyPass : public FunctionPass {
71 static char ID; // Pass identification, replacement for typeid
72 PromoteLegacyPass() : FunctionPass(ID) {
73 initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
74 }
75
76 // runOnFunction - To run this pass, first we calculate the alloca
77 // instructions that are safe for promotion, then we promote each one.
78 //
79 bool runOnFunction(Function &F) override {
80 if (skipFunction(F))
81 return false;
82
83 DominatorTree &DT = getAnalysis().getDomTree();
84 AssumptionCache &AC =
85 getAnalysis().getAssumptionCache(F);
86 return promoteMemoryToRegister(F, DT, AC);
87 }
88
89 void getAnalysisUsage(AnalysisUsage &AU) const override {
90 AU.addRequired();
91 AU.addRequired();
92 AU.setPreservesCFG();
93 // This is a cluster of orthogonal Transforms
94 AU.addPreserved();
95 AU.addPreservedID(LowerSwitchID);
96 AU.addPreservedID(LowerInvokePassID);
97 }
98 };
99 } // end of anonymous namespace
100
101 char PromoteLegacyPass::ID = 0;
102 INITIALIZE_PASS_BEGIN(PromoteLegacyPass, "mem2reg", "Promote Memory to "
103 "Register",
104 false, false)
105 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
106 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
107 INITIALIZE_PASS_END(PromoteLegacyPass, "mem2reg", "Promote Memory to Register",
108 false, false)
93109
94110 // createPromoteMemoryToRegister - Provide an entry point to create this pass.
95111 //
96112 FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
97 return new PromotePass();
113 return new PromoteLegacyPass();
98114 }
2828 initializeLowerInvokePass(Registry);
2929 initializeLowerSwitchPass(Registry);
3030 initializeNameAnonFunctionPass(Registry);
31 initializePromotePassPass(Registry);
31 initializePromoteLegacyPassPass(Registry);
3232 initializeUnifyFunctionExitNodesPass(Registry);
3333 initializeInstSimplifierPass(Registry);
3434 initializeMetaRenamerPass(Registry);
0 ; RUN: opt -mem2reg < %s -S | FileCheck %s
1 ; RUN: opt -passes=mem2reg < %s -S | FileCheck %s
12
23 declare i32 @def(i32)
34 declare i1 @use(i32)