Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //=- 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 | |
| 22 | namespace llvm { |
| 23 | |
| 24 | class Function; |
| 25 | class raw_ostream; |
| 26 | |
| 27 | /// PostDominatorTree Class - Concrete subclass of DominatorTree that is used to |
| 28 | /// compute the post-dominator tree. |
| 29 | class PostDominatorTree : public PostDomTreeBase<BasicBlock> { |
| 30 | public: |
| 31 | using Base = PostDomTreeBase<BasicBlock>; |
| 32 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 33 | PostDominatorTree() = default; |
| 34 | explicit PostDominatorTree(Function &F) { recalculate(F); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 35 | /// Handle invalidation explicitly. |
| 36 | bool invalidate(Function &F, const PreservedAnalyses &PA, |
| 37 | FunctionAnalysisManager::Invalidator &); |
| 38 | }; |
| 39 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 40 | /// Analysis pass which computes a \c PostDominatorTree. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 41 | class PostDominatorTreeAnalysis |
| 42 | : public AnalysisInfoMixin<PostDominatorTreeAnalysis> { |
| 43 | friend AnalysisInfoMixin<PostDominatorTreeAnalysis>; |
| 44 | |
| 45 | static AnalysisKey Key; |
| 46 | |
| 47 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 48 | /// Provide the result type for this analysis pass. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 49 | using Result = PostDominatorTree; |
| 50 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 51 | /// Run the analysis pass over a function and produce a post dominator |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | /// tree. |
| 53 | PostDominatorTree run(Function &F, FunctionAnalysisManager &); |
| 54 | }; |
| 55 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 56 | /// Printer pass for the \c PostDominatorTree. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 57 | class PostDominatorTreePrinterPass |
| 58 | : public PassInfoMixin<PostDominatorTreePrinterPass> { |
| 59 | raw_ostream &OS; |
| 60 | |
| 61 | public: |
| 62 | explicit PostDominatorTreePrinterPass(raw_ostream &OS); |
| 63 | |
| 64 | PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); |
| 65 | }; |
| 66 | |
| 67 | struct 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 | |
| 94 | FunctionPass* createPostDomTree(); |
| 95 | |
| 96 | template <> 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 |