blob: f2dc8d135d71cb4a162c26f41682f874f4dca193 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//=- llvm/Analysis/PostDominators.h - Post Dominator Calculation --*- C++ -*-=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file exposes interfaces to post dominance information.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_POSTDOMINATORS_H
15#define LLVM_ANALYSIS_POSTDOMINATORS_H
16
17#include "llvm/ADT/DepthFirstIterator.h"
18#include "llvm/IR/Dominators.h"
19#include "llvm/IR/PassManager.h"
20#include "llvm/Pass.h"
21
22namespace llvm {
23
24class Function;
25class raw_ostream;
26
27/// PostDominatorTree Class - Concrete subclass of DominatorTree that is used to
28/// compute the post-dominator tree.
29class PostDominatorTree : public PostDomTreeBase<BasicBlock> {
30public:
31 using Base = PostDomTreeBase<BasicBlock>;
32
Andrew Scullcdfcccc2018-10-05 20:58:37 +010033 PostDominatorTree() = default;
34 explicit PostDominatorTree(Function &F) { recalculate(F); }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010035 /// Handle invalidation explicitly.
36 bool invalidate(Function &F, const PreservedAnalyses &PA,
37 FunctionAnalysisManager::Invalidator &);
38};
39
Andrew Scullcdfcccc2018-10-05 20:58:37 +010040/// Analysis pass which computes a \c PostDominatorTree.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010041class PostDominatorTreeAnalysis
42 : public AnalysisInfoMixin<PostDominatorTreeAnalysis> {
43 friend AnalysisInfoMixin<PostDominatorTreeAnalysis>;
44
45 static AnalysisKey Key;
46
47public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010048 /// Provide the result type for this analysis pass.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010049 using Result = PostDominatorTree;
50
Andrew Scullcdfcccc2018-10-05 20:58:37 +010051 /// Run the analysis pass over a function and produce a post dominator
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010052 /// tree.
53 PostDominatorTree run(Function &F, FunctionAnalysisManager &);
54};
55
Andrew Scullcdfcccc2018-10-05 20:58:37 +010056/// Printer pass for the \c PostDominatorTree.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010057class PostDominatorTreePrinterPass
58 : public PassInfoMixin<PostDominatorTreePrinterPass> {
59 raw_ostream &OS;
60
61public:
62 explicit PostDominatorTreePrinterPass(raw_ostream &OS);
63
64 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
65};
66
67struct PostDominatorTreeWrapperPass : public FunctionPass {
68 static char ID; // Pass identification, replacement for typeid
69
70 PostDominatorTree DT;
71
72 PostDominatorTreeWrapperPass() : FunctionPass(ID) {
73 initializePostDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
74 }
75
76 PostDominatorTree &getPostDomTree() { return DT; }
77 const PostDominatorTree &getPostDomTree() const { return DT; }
78
79 bool runOnFunction(Function &F) override;
80
81 void verifyAnalysis() const override;
82
83 void getAnalysisUsage(AnalysisUsage &AU) const override {
84 AU.setPreservesAll();
85 }
86
87 void releaseMemory() override {
88 DT.releaseMemory();
89 }
90
91 void print(raw_ostream &OS, const Module*) const override;
92};
93
94FunctionPass* createPostDomTree();
95
96template <> struct GraphTraits<PostDominatorTree*>
97 : public GraphTraits<DomTreeNode*> {
98 static NodeRef getEntryNode(PostDominatorTree *DT) {
99 return DT->getRootNode();
100 }
101
102 static nodes_iterator nodes_begin(PostDominatorTree *N) {
103 if (getEntryNode(N))
104 return df_begin(getEntryNode(N));
105 else
106 return df_end(getEntryNode(N));
107 }
108
109 static nodes_iterator nodes_end(PostDominatorTree *N) {
110 return df_end(getEntryNode(N));
111 }
112};
113
114} // end namespace llvm
115
116#endif // LLVM_ANALYSIS_POSTDOMINATORS_H