Dominators.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //===- Dominators.h - Dominator Info 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 defines the DominatorTree class, which provides fast and efficient
  11. // dominance queries.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_IR_DOMINATORS_H
  15. #define LLVM_IR_DOMINATORS_H
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/DepthFirstIterator.h"
  18. #include "llvm/ADT/GraphTraits.h"
  19. #include "llvm/ADT/SmallPtrSet.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/IR/BasicBlock.h"
  22. #include "llvm/IR/CFG.h"
  23. #include "llvm/IR/Function.h"
  24. #include "llvm/Pass.h"
  25. #include "llvm/Support/Compiler.h"
  26. #include "llvm/Support/GenericDomTree.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include <algorithm>
  29. namespace llvm {
  30. // FIXME: Replace this brittle forward declaration with the include of the new
  31. // PassManager.h when doing so doesn't break the PassManagerBuilder.
  32. template <typename IRUnitT> class AnalysisManager;
  33. class PreservedAnalyses;
  34. extern template class DomTreeNodeBase<BasicBlock>;
  35. extern template class DominatorTreeBase<BasicBlock>;
  36. extern template void Calculate<Function, BasicBlock *>(
  37. DominatorTreeBase<GraphTraits<BasicBlock *>::NodeType> &DT, Function &F);
  38. extern template void Calculate<Function, Inverse<BasicBlock *>>(
  39. DominatorTreeBase<GraphTraits<Inverse<BasicBlock *>>::NodeType> &DT,
  40. Function &F);
  41. typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
  42. class BasicBlockEdge {
  43. const BasicBlock *Start;
  44. const BasicBlock *End;
  45. public:
  46. BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
  47. Start(Start_), End(End_) { }
  48. const BasicBlock *getStart() const {
  49. return Start;
  50. }
  51. const BasicBlock *getEnd() const {
  52. return End;
  53. }
  54. bool isSingleEdge() const;
  55. };
  56. /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
  57. /// normal dominator tree.
  58. class DominatorTree : public DominatorTreeBase<BasicBlock> {
  59. public:
  60. typedef DominatorTreeBase<BasicBlock> Base;
  61. DominatorTree() : DominatorTreeBase<BasicBlock>(false) {}
  62. DominatorTree(DominatorTree &&Arg)
  63. : Base(std::move(static_cast<Base &>(Arg))) {}
  64. DominatorTree &operator=(DominatorTree &&RHS) {
  65. Base::operator=(std::move(static_cast<Base &>(RHS)));
  66. return *this;
  67. }
  68. /// \brief Returns *false* if the other dominator tree matches this dominator
  69. /// tree.
  70. inline bool compare(const DominatorTree &Other) const {
  71. const DomTreeNode *R = getRootNode();
  72. const DomTreeNode *OtherR = Other.getRootNode();
  73. if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
  74. return true;
  75. if (Base::compare(Other))
  76. return true;
  77. return false;
  78. }
  79. // Ensure base-class overloads are visible.
  80. using Base::dominates;
  81. /// \brief Return true if Def dominates a use in User.
  82. ///
  83. /// This performs the special checks necessary if Def and User are in the same
  84. /// basic block. Note that Def doesn't dominate a use in Def itself!
  85. bool dominates(const Instruction *Def, const Use &U) const;
  86. bool dominates(const Instruction *Def, const Instruction *User) const;
  87. bool dominates(const Instruction *Def, const BasicBlock *BB) const;
  88. bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
  89. bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
  90. // Ensure base class overloads are visible.
  91. using Base::isReachableFromEntry;
  92. /// \brief Provide an overload for a Use.
  93. bool isReachableFromEntry(const Use &U) const;
  94. /// \brief Verify the correctness of the domtree by re-computing it.
  95. ///
  96. /// This should only be used for debugging as it aborts the program if the
  97. /// verification fails.
  98. void verifyDomTree() const;
  99. };
  100. //===-------------------------------------
  101. // DominatorTree GraphTraits specializations so the DominatorTree can be
  102. // iterable by generic graph iterators.
  103. template <> struct GraphTraits<DomTreeNode*> {
  104. typedef DomTreeNode NodeType;
  105. typedef NodeType::iterator ChildIteratorType;
  106. static NodeType *getEntryNode(NodeType *N) {
  107. return N;
  108. }
  109. static inline ChildIteratorType child_begin(NodeType *N) {
  110. return N->begin();
  111. }
  112. static inline ChildIteratorType child_end(NodeType *N) {
  113. return N->end();
  114. }
  115. typedef df_iterator<DomTreeNode*> nodes_iterator;
  116. static nodes_iterator nodes_begin(DomTreeNode *N) {
  117. return df_begin(getEntryNode(N));
  118. }
  119. static nodes_iterator nodes_end(DomTreeNode *N) {
  120. return df_end(getEntryNode(N));
  121. }
  122. };
  123. template <> struct GraphTraits<DominatorTree*>
  124. : public GraphTraits<DomTreeNode*> {
  125. static NodeType *getEntryNode(DominatorTree *DT) {
  126. return DT->getRootNode();
  127. }
  128. static nodes_iterator nodes_begin(DominatorTree *N) {
  129. return df_begin(getEntryNode(N));
  130. }
  131. static nodes_iterator nodes_end(DominatorTree *N) {
  132. return df_end(getEntryNode(N));
  133. }
  134. };
  135. /// \brief Analysis pass which computes a \c DominatorTree.
  136. class DominatorTreeAnalysis {
  137. public:
  138. /// \brief Provide the result typedef for this analysis pass.
  139. typedef DominatorTree Result;
  140. /// \brief Opaque, unique identifier for this analysis pass.
  141. static void *ID() { return (void *)&PassID; }
  142. /// \brief Run the analysis pass over a function and produce a dominator tree.
  143. DominatorTree run(Function &F);
  144. /// \brief Provide access to a name for this pass for debugging purposes.
  145. static StringRef name() { return "DominatorTreeAnalysis"; }
  146. private:
  147. static char PassID;
  148. };
  149. /// \brief Printer pass for the \c DominatorTree.
  150. class DominatorTreePrinterPass {
  151. raw_ostream &OS;
  152. public:
  153. explicit DominatorTreePrinterPass(raw_ostream &OS);
  154. PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
  155. static StringRef name() { return "DominatorTreePrinterPass"; }
  156. };
  157. /// \brief Verifier pass for the \c DominatorTree.
  158. struct DominatorTreeVerifierPass {
  159. PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
  160. static StringRef name() { return "DominatorTreeVerifierPass"; }
  161. };
  162. /// \brief Legacy analysis pass which computes a \c DominatorTree.
  163. class DominatorTreeWrapperPass : public FunctionPass {
  164. DominatorTree DT;
  165. public:
  166. static char ID;
  167. DominatorTreeWrapperPass() : FunctionPass(ID) {
  168. initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
  169. }
  170. DominatorTree &getDomTree() { return DT; }
  171. const DominatorTree &getDomTree() const { return DT; }
  172. bool runOnFunction(Function &F) override;
  173. void verifyAnalysis() const override;
  174. void getAnalysisUsage(AnalysisUsage &AU) const override {
  175. AU.setPreservesAll();
  176. }
  177. void releaseMemory() override { DT.releaseMemory(); }
  178. void print(raw_ostream &OS, const Module *M = nullptr) const override;
  179. };
  180. } // End llvm namespace
  181. #endif