| 1 | //=- llvm/Analysis/PostDominators.h - Post Dominator Calculation --*- C++ -*-=// |
|---|---|
| 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 file exposes interfaces to post dominance information. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_ANALYSIS_POSTDOMINATORS_H |
| 14 | #define LLVM_ANALYSIS_POSTDOMINATORS_H |
| 15 | |
| 16 | #include "llvm/ADT/DepthFirstIterator.h" |
| 17 | #include "llvm/IR/Dominators.h" |
| 18 | #include "llvm/IR/PassManager.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Support/Compiler.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 | |
| 33 | PostDominatorTree() = default; |
| 34 | explicit PostDominatorTree(Function &F) { recalculate(Func&: F); } |
| 35 | /// Handle invalidation explicitly. |
| 36 | LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, |
| 37 | FunctionAnalysisManager::Invalidator &); |
| 38 | |
| 39 | // Ensure base-class overloads are visible. |
| 40 | using Base::dominates; |
| 41 | |
| 42 | /// Return true if \p I1 dominates \p I2. This checks if \p I2 comes before |
| 43 | /// \p I1 if they belongs to the same basic block. |
| 44 | LLVM_ABI bool dominates(const Instruction *I1, const Instruction *I2) const; |
| 45 | }; |
| 46 | |
| 47 | /// Analysis pass which computes a \c PostDominatorTree. |
| 48 | class PostDominatorTreeAnalysis |
| 49 | : public AnalysisInfoMixin<PostDominatorTreeAnalysis> { |
| 50 | friend AnalysisInfoMixin<PostDominatorTreeAnalysis>; |
| 51 | |
| 52 | LLVM_ABI static AnalysisKey Key; |
| 53 | |
| 54 | public: |
| 55 | /// Provide the result type for this analysis pass. |
| 56 | using Result = PostDominatorTree; |
| 57 | |
| 58 | /// Run the analysis pass over a function and produce a post dominator |
| 59 | /// tree. |
| 60 | LLVM_ABI PostDominatorTree run(Function &F, FunctionAnalysisManager &); |
| 61 | }; |
| 62 | |
| 63 | /// Printer pass for the \c PostDominatorTree. |
| 64 | class PostDominatorTreePrinterPass |
| 65 | : public PassInfoMixin<PostDominatorTreePrinterPass> { |
| 66 | raw_ostream &OS; |
| 67 | |
| 68 | public: |
| 69 | LLVM_ABI explicit PostDominatorTreePrinterPass(raw_ostream &OS); |
| 70 | |
| 71 | LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); |
| 72 | |
| 73 | static bool isRequired() { return true; } |
| 74 | }; |
| 75 | |
| 76 | struct LLVM_ABI PostDominatorTreeWrapperPass : public FunctionPass { |
| 77 | static char ID; // Pass identification, replacement for typeid |
| 78 | |
| 79 | PostDominatorTree DT; |
| 80 | |
| 81 | PostDominatorTreeWrapperPass(); |
| 82 | |
| 83 | PostDominatorTree &getPostDomTree() { return DT; } |
| 84 | const PostDominatorTree &getPostDomTree() const { return DT; } |
| 85 | |
| 86 | bool runOnFunction(Function &F) override; |
| 87 | |
| 88 | void verifyAnalysis() const override; |
| 89 | |
| 90 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 91 | AU.setPreservesAll(); |
| 92 | } |
| 93 | |
| 94 | void releaseMemory() override { DT.reset(); } |
| 95 | |
| 96 | void print(raw_ostream &OS, const Module*) const override; |
| 97 | }; |
| 98 | |
| 99 | LLVM_ABI FunctionPass *createPostDomTree(); |
| 100 | |
| 101 | template <> struct GraphTraits<PostDominatorTree*> |
| 102 | : public GraphTraits<DomTreeNode*> { |
| 103 | static NodeRef getEntryNode(PostDominatorTree *DT) { |
| 104 | return DT->getRootNode(); |
| 105 | } |
| 106 | |
| 107 | static nodes_iterator nodes_begin(PostDominatorTree *N) { |
| 108 | return df_begin(G: getEntryNode(DT: N)); |
| 109 | } |
| 110 | |
| 111 | static nodes_iterator nodes_end(PostDominatorTree *N) { |
| 112 | return df_end(G: getEntryNode(DT: N)); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | } // end namespace llvm |
| 117 | |
| 118 | #endif // LLVM_ANALYSIS_POSTDOMINATORS_H |
| 119 |