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/IR/Dominators.h" 18 #include "llvm/IR/PassManager.h" 19 20 namespace llvm { 21 22 /// PostDominatorTree Class - Concrete subclass of DominatorTree that is used to 23 /// compute the post-dominator tree. 24 /// 25 struct PostDominatorTree : public DominatorTreeBase<BasicBlock> { 26 typedef DominatorTreeBase<BasicBlock> Base; 27 28 PostDominatorTree() : DominatorTreeBase<BasicBlock>(true) {} 29 30 PostDominatorTree(PostDominatorTree &&Arg) 31 : Base(std::move(static_cast<Base &>(Arg))) {} 32 33 PostDominatorTree &operator=(PostDominatorTree &&RHS) { 34 Base::operator=(std::move(static_cast<Base &>(RHS))); 35 return *this; 36 } 37 }; 38 39 /// \brief Analysis pass which computes a \c PostDominatorTree. 40 class PostDominatorTreeAnalysis 41 : public AnalysisInfoMixin<PostDominatorTreeAnalysis> { 42 friend AnalysisInfoMixin<PostDominatorTreeAnalysis>; 43 static char PassID; 44 45 public: 46 /// \brief Provide the result typedef for this analysis pass. 47 typedef PostDominatorTree Result; 48 49 /// \brief Run the analysis pass over a function and produce a post dominator 50 /// tree. 51 PostDominatorTree run(Function &F, FunctionAnalysisManager &); 52 }; 53 54 /// \brief Printer pass for the \c PostDominatorTree. 55 class PostDominatorTreePrinterPass 56 : public PassInfoMixin<PostDominatorTreePrinterPass> { 57 raw_ostream &OS; 58 59 public: 60 explicit PostDominatorTreePrinterPass(raw_ostream &OS); 61 PreservedAnalyses run(Function &F, AnalysisManager<Function> &AM); 62 }; 63 64 struct PostDominatorTreeWrapperPass : public FunctionPass { 65 static char ID; // Pass identification, replacement for typeid 66 PostDominatorTree DT; 67 68 PostDominatorTreeWrapperPass() : FunctionPass(ID) { 69 initializePostDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry()); 70 } 71 72 PostDominatorTree &getPostDomTree() { return DT; } 73 const PostDominatorTree &getPostDomTree() const { return DT; } 74 75 bool runOnFunction(Function &F) override; 76 77 void getAnalysisUsage(AnalysisUsage &AU) const override { 78 AU.setPreservesAll(); 79 } 80 81 void releaseMemory() override { 82 DT.releaseMemory(); 83 } 84 85 void print(raw_ostream &OS, const Module*) const override; 86 }; 87 88 FunctionPass* createPostDomTree(); 89 90 template <> struct GraphTraits<PostDominatorTree*> 91 : public GraphTraits<DomTreeNode*> { 92 static NodeType *getEntryNode(PostDominatorTree *DT) { 93 return DT->getRootNode(); 94 } 95 96 static nodes_iterator nodes_begin(PostDominatorTree *N) { 97 if (getEntryNode(N)) 98 return df_begin(getEntryNode(N)); 99 else 100 return df_end(getEntryNode(N)); 101 } 102 103 static nodes_iterator nodes_end(PostDominatorTree *N) { 104 return df_end(getEntryNode(N)); 105 } 106 }; 107 108 } // End llvm namespace 109 110 #endif 111