llvm.org GIT mirror llvm / 5d7472e
Introduce analysis pass to compute PostDominators in the new pass manager. NFC Differential Revision: http://reviews.llvm.org/D17537 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@261902 91177308-0d34-0410-b5e6-96231b3b80d8 Hongbin Zheng 4 years ago
13 changed file(s) with 129 addition(s) and 90 deletion(s). Raw diff Collapse all Expand all
1616 #include "llvm/IR/Dominators.h"
1717
1818 namespace llvm {
19 // FIXME: Replace this brittle forward declaration with the include of the new
20 // PassManager.h when doing so doesn't break the PassManagerBuilder.
21 class PreservedAnalyses;
1922
2023 /// PostDominatorTree Class - Concrete subclass of DominatorTree that is used to
2124 /// compute the post-dominator tree.
2225 ///
23 struct PostDominatorTree : public FunctionPass {
26 struct PostDominatorTree : public DominatorTreeBase {
27 PostDominatorTree() : DominatorTreeBase(true) {}
28 };
29
30 /// \brief Analysis pass which computes a \c PostDominatorTree.
31 class PostDominatorTreeAnalysis {
32 public:
33 /// \brief Provide the result typedef for this analysis pass.
34 typedef PostDominatorTree Result;
35
36 /// \brief Opaque, unique identifier for this analysis pass.
37 static void *ID() { return (void *)&PassID; }
38
39 /// \brief Run the analysis pass over a function and produce a post dominator
40 /// tree.
41 PostDominatorTree run(Function &F);
42
43 /// \brief Provide access to a name for this pass for debugging purposes.
44 static StringRef name() { return "PostDominatorTreeAnalysis"; }
45
46 private:
47 static char PassID;
48 };
49
50 /// \brief Printer pass for the \c PostDominatorTree.
51 class PostDominatorTreePrinterPass {
52 raw_ostream &OS;
53
54 public:
55 explicit PostDominatorTreePrinterPass(raw_ostream &OS);
56 PreservedAnalyses run(Function &F, AnalysisManager *AM);
57
58 static StringRef name() { return "PostDominatorTreePrinterPass"; }
59 };
60
61 struct PostDominatorTreeWrapperPass : public FunctionPass {
2462 static char ID; // Pass identification, replacement for typeid
25 DominatorTreeBase* DT;
63 PostDominatorTree DT;
2664
27 PostDominatorTree() : FunctionPass(ID) {
28 initializePostDominatorTreePass(*PassRegistry::getPassRegistry());
29 DT = new DominatorTreeBase(true);
65 PostDominatorTreeWrapperPass() : FunctionPass(ID) {
66 initializePostDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
3067 }
3168
32 ~PostDominatorTree() override;
69 PostDominatorTree &getPostDomTree() { return DT; }
70 const PostDominatorTree &getPostDomTree() const { return DT; }
3371
3472 bool runOnFunction(Function &F) override;
3573
3775 AU.setPreservesAll();
3876 }
3977
40 inline const std::vector &getRoots() const {
41 return DT->getRoots();
42 }
43
44 inline DomTreeNode *getRootNode() const {
45 return DT->getRootNode();
46 }
47
48 inline DomTreeNode *operator[](BasicBlock *BB) const {
49 return DT->getNode(BB);
50 }
51
52 inline DomTreeNode *getNode(BasicBlock *BB) const {
53 return DT->getNode(BB);
54 }
55
56 inline bool dominates(DomTreeNode* A, DomTreeNode* B) const {
57 return DT->dominates(A, B);
58 }
59
60 inline bool dominates(const BasicBlock* A, const BasicBlock* B) const {
61 return DT->dominates(A, B);
62 }
63
64 inline bool properlyDominates(const DomTreeNode* A, DomTreeNode* B) const {
65 return DT->properlyDominates(A, B);
66 }
67
68 inline bool properlyDominates(BasicBlock* A, BasicBlock* B) const {
69 return DT->properlyDominates(A, B);
70 }
71
72 inline BasicBlock *findNearestCommonDominator(BasicBlock *A, BasicBlock *B) {
73 return DT->findNearestCommonDominator(A, B);
74 }
75
76 inline const BasicBlock *findNearestCommonDominator(const BasicBlock *A,
77 const BasicBlock *B) {
78 return DT->findNearestCommonDominator(A, B);
79 }
80
81 /// Get all nodes post-dominated by R, including R itself.
82 void getDescendants(BasicBlock *R,
83 SmallVectorImpl &Result) const {
84 DT->getDescendants(R, Result);
85 }
86
8778 void releaseMemory() override {
88 DT->releaseMemory();
79 DT.releaseMemory();
8980 }
9081
9182 void print(raw_ostream &OS, const Module*) const override;
231231 void initializePostDomOnlyViewerPass(PassRegistry&);
232232 void initializePostDomPrinterPass(PassRegistry&);
233233 void initializePostDomViewerPass(PassRegistry&);
234 void initializePostDominatorTreePass(PassRegistry&);
234 void initializePostDominatorTreeWrapperPassPass(PassRegistry&);
235235 void initializePostOrderFunctionAttrsLegacyPassPass(PassRegistry&);
236236 void initializePostRASchedulerPass(PassRegistry&);
237237 void initializePostMachineSchedulerPass(PassRegistry&);
5959 initializeMemoryDependenceAnalysisPass(Registry);
6060 initializeModuleDebugInfoPrinterPass(Registry);
6161 initializeObjCARCAAWrapperPassPass(Registry);
62 initializePostDominatorTreePass(Registry);
62 initializePostDominatorTreeWrapperPassPass(Registry);
6363 initializeRegionInfoPassPass(Registry);
6464 initializeRegionViewerPass(Registry);
6565 initializeRegionPrinterPass(Registry);
257257 INITIALIZE_PASS_BEGIN(DivergenceAnalysis, "divergence", "Divergence Analysis",
258258 false, true)
259259 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
260 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
260 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
261261 INITIALIZE_PASS_END(DivergenceAnalysis, "divergence", "Divergence Analysis",
262262 false, true)
263263
267267
268268 void DivergenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
269269 AU.addRequired();
270 AU.addRequired>();
270 AU.addRequiredWrapperPass>();
271271 AU.setPreservesAll();
272272 }
273273
283283 return false;
284284
285285 DivergentValues.clear();
286 auto &PDT = getAnalysis().getPostDomTree();
286287 DivergencePropagator DP(F, TTI,
287288 getAnalysis().getDomTree(),
288 getAnalysis(), DivergentValues);
289 PDT, DivergentValues);
289290 DP.populateWithSourcesOfDivergence();
290291 DP.propagate();
291292 return false;
110110 }
111111 };
112112
113 struct PostDomViewer
114 : public DOTGraphTraitsViewer {
113 struct PostDominatorTreeWrapperPassAnalysisGraphTraits {
114 static PostDominatorTree *getGraph(PostDominatorTreeWrapperPass *PDTWP) {
115 return &PDTWP->getPostDomTree();
116 }
117 };
118
119 struct PostDomViewer : public DOTGraphTraitsViewer<
120 PostDominatorTreeWrapperPass, false,
121 PostDominatorTree *,
122 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
115123 static char ID;
116124 PostDomViewer() :
117 DOTGraphTraitsViewer, false>("postdom", ID){
125 DOTGraphTraitsViewerWrapperPass, false,
126 PostDominatorTree *,
127 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
128 "postdom", ID){
118129 initializePostDomViewerPass(*PassRegistry::getPassRegistry());
119130 }
120131 };
121132
122 struct PostDomOnlyViewer
123 : public DOTGraphTraitsViewer {
133 struct PostDomOnlyViewer : public DOTGraphTraitsViewer<
134 PostDominatorTreeWrapperPass, true,
135 PostDominatorTree *,
136 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
124137 static char ID;
125138 PostDomOnlyViewer() :
126 DOTGraphTraitsViewer, true>("postdomonly", ID){
139 DOTGraphTraitsViewerWrapperPass, true,
140 PostDominatorTree *,
141 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
142 "postdomonly", ID){
127143 initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
128144 }
129145 };
174190 };
175191
176192 struct PostDomPrinter
177 : public DOTGraphTraitsPrinter<PostDominatorTree, false> {
193 : public DOTGraphTraitsPrinter<
194 PostDominatorTreeWrapperPass, false,
195 PostDominatorTree *,
196 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
178197 static char ID;
179198 PostDomPrinter() :
180 DOTGraphTraitsPrinter, false>("postdom", ID) {
199 DOTGraphTraitsPrinterWrapperPass, false,
200 PostDominatorTree *,
201 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
202 "postdom", ID) {
181203 initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
182204 }
183205 };
184206
185207 struct PostDomOnlyPrinter
186 : public DOTGraphTraitsPrinter<PostDominatorTree, true> {
208 : public DOTGraphTraitsPrinter<
209 PostDominatorTreeWrapperPass, true,
210 PostDominatorTree *,
211 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
187212 static char ID;
188213 PostDomOnlyPrinter() :
189 DOTGraphTraitsPrinter, true>("postdomonly", ID) {
214 DOTGraphTraitsPrinterWrapperPass, true,
215 PostDominatorTree *,
216 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
217 "postdomonly", ID) {
190218 initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
191219 }
192220 };
1515 #include "llvm/ADT/SetOperations.h"
1616 #include "llvm/IR/CFG.h"
1717 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/PassManager.h"
1819 #include "llvm/Support/Debug.h"
1920 #include "llvm/Support/GenericDomTreeConstruction.h"
2021 using namespace llvm;
2526 // PostDominatorTree Implementation
2627 //===----------------------------------------------------------------------===//
2728
28 char PostDominatorTree::ID = 0;
29 INITIALIZE_PASS(PostDominatorTree, "postdomtree",
29 char PostDominatorTreeWrapperPass::ID = 0;
30 INITIALIZE_PASS(PostDominatorTreeWrapperPass, "postdomtree",
3031 "Post-Dominator Tree Construction", true, true)
3132
32 bool PostDominatorTree::runOnFunction(Function &F) {
33 DT->recalculate(F);
33 bool PostDominatorTreeWrapperPass::runOnFunction(Function &F) {
34 DT.recalculate(F);
3435 return false;
3536 }
3637
37 PostDominatorTree::~PostDominatorTree() {
38 delete DT;
38 void PostDominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const {
39 DT.print(OS);
3940 }
4041
41 void PostDominatorTree::print(raw_ostream &OS, const Module *) const {
42 DT->print(OS);
42 FunctionPass* llvm::createPostDomTree() {
43 return new PostDominatorTreeWrapperPass();
4344 }
4445
46 char PostDominatorTreeAnalysis::PassID;
4547
46 FunctionPass* llvm::createPostDomTree() {
47 return new PostDominatorTree();
48 PostDominatorTree PostDominatorTreeAnalysis::run(Function &F) {
49 PostDominatorTree PDT;
50 PDT.recalculate(F);
51 return PDT;
4852 }
4953
54 PostDominatorTreePrinterPass::PostDominatorTreePrinterPass(raw_ostream &OS)
55 : OS(OS) {}
56
57 PreservedAnalyses
58 PostDominatorTreePrinterPass::run(Function &F, FunctionAnalysisManager *AM) {
59 OS << "PostDominatorTree for function: " << F.getName() << "\n";
60 AM->getResult(F).print(OS);
61
62 return PreservedAnalyses::all();
63 }
127127 releaseMemory();
128128
129129 auto DT = &getAnalysis().getDomTree();
130 auto PDT = &getAnalysis>();
130 auto PDT = &getAnalysisWrapperPass>().getPostDomTree();
131131 auto DF = &getAnalysis();
132132
133133 RI.recalculate(F, DT, PDT, DF);
145145 void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
146146 AU.setPreservesAll();
147147 AU.addRequiredTransitive();
148 AU.addRequired();
149148 AU.addRequired();
149 AU.addRequired();
150150 }
151151
152152 void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
164164 INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
165165 "Detect single entry single exit regions", true, true)
166166 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
167 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
168167 INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
168 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
169169 INITIALIZE_PASS_END(RegionInfoPass, "regions",
170170 "Detect single entry single exit regions", true, true)
171171
103103 void MachineRegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
104104 AU.setPreservesAll();
105105 AU.addRequiredTransitive();
106 AU.addRequired>();
106 AU.addRequiredWrapperPass>();
107107 AU.addRequired();
108108 }
109109
2323 #include "llvm/Analysis/CGSCCPassManager.h"
2424 #include "llvm/Analysis/LazyCallGraph.h"
2525 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/Analysis/PostDominators.h"
2627 #include "llvm/Analysis/ScalarEvolution.h"
2728 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
2829 #include "llvm/Analysis/ScopedNoAliasAA.h"
5656 FUNCTION_ANALYSIS("aa", AAManager())
5757 FUNCTION_ANALYSIS("assumptions", AssumptionAnalysis())
5858 FUNCTION_ANALYSIS("domtree", DominatorTreeAnalysis())
59 FUNCTION_ANALYSIS("postdomtree", PostDominatorTreeAnalysis())
5960 FUNCTION_ANALYSIS("loops", LoopAnalysis())
6061 FUNCTION_ANALYSIS("no-op-function", NoOpFunctionAnalysis())
6162 FUNCTION_ANALYSIS("scalar-evolution", ScalarEvolutionAnalysis())
8889 FUNCTION_PASS("print", PrintFunctionPass(dbgs()))
8990 FUNCTION_PASS("print", AssumptionPrinterPass(dbgs()))
9091 FUNCTION_PASS("print", DominatorTreePrinterPass(dbgs()))
92 FUNCTION_PASS("print", PostDominatorTreePrinterPass(dbgs()))
9193 FUNCTION_PASS("print", LoopPrinterPass(dbgs()))
9294 FUNCTION_PASS("print", ScalarEvolutionPrinterPass(dbgs()))
9395 FUNCTION_PASS("simplify-cfg", SimplifyCFGPass())
8989 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
9090 AU.addRequired();
9191 AU.addPreserved();
92 AU.addRequired();
93 AU.addPreserved>();
92 AU.addRequired>();
93 AU.addPreserved();
9494 AU.addRequired();
9595 AU.addPreserved();
9696 FunctionPass::getAnalysisUsage(AU);
146146 INITIALIZE_PASS_BEGIN(HexagonCommonGEP, "hcommgep", "Hexagon Common GEP",
147147 false, false)
148148 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
149 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
149 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
150150 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
151151 INITIALIZE_PASS_END(HexagonCommonGEP, "hcommgep", "Hexagon Common GEP",
152152 false, false)
12751275
12761276 Fn = &F;
12771277 DT = &getAnalysis().getDomTree();
1278 PDT = &getAnalysis>();
1278 PDT = &getAnalysisWrapperPass>().getPostDomTree();
12791279 LI = &getAnalysis().getLoopInfo();
12801280 Ctx = &F.getContext();
12811281
0 ; RUN: opt < %s -postdomtree -analyze | FileCheck %s
1 ; RUN: opt < %s -passes='print' 2>&1 | FileCheck %s
12 ; PR932
23
34 define void @foo(i1 %x) {
2828 bool runOnFunction(Function &F) override {
2929 DominatorTree *DT =
3030 &getAnalysis().getDomTree();
31 PostDominatorTree *PDT = &getAnalysis();
31 PostDominatorTree *PDT =
32 &getAnalysis().getPostDomTree();
3233 Function::iterator FI = F.begin();
3334
3435 BasicBlock *BB0 = &*FI++;
205206 }
206207 void getAnalysisUsage(AnalysisUsage &AU) const override {
207208 AU.addRequired();
208 AU.addRequired>();
209 AU.addRequiredWrapperPass>();
209210 }
210211 DPass() : FunctionPass(ID) {
211212 initializeDPassPass(*PassRegistry::getPassRegistry());
254255
255256 INITIALIZE_PASS_BEGIN(DPass, "dpass", "dpass", false, false)
256257 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
257 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
258 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
258259 INITIALIZE_PASS_END(DPass, "dpass", "dpass", false, false)