DominanceFrontier.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //===- llvm/Analysis/DominanceFrontier.h - Dominator Frontiers --*- 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 DominanceFrontier class, which calculate and holds the
  11. // dominance frontier for a function.
  12. //
  13. // This should be considered deprecated, don't add any more uses of this data
  14. // structure.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_DOMINANCEFRONTIER_H
  18. #define LLVM_ANALYSIS_DOMINANCEFRONTIER_H
  19. #include "llvm/IR/Dominators.h"
  20. #include <map>
  21. #include <set>
  22. namespace llvm {
  23. // //
  24. ///////////////////////////////////////////////////////////////////////////////
  25. /// DominanceFrontierBase - Common base class for computing forward and inverse
  26. /// dominance frontiers for a function.
  27. ///
  28. template <class BlockT>
  29. class DominanceFrontierBase {
  30. public:
  31. typedef std::set<BlockT *> DomSetType; // Dom set for a bb
  32. typedef std::map<BlockT *, DomSetType> DomSetMapType; // Dom set map
  33. protected:
  34. typedef GraphTraits<BlockT *> BlockTraits;
  35. DomSetMapType Frontiers;
  36. std::vector<BlockT *> Roots;
  37. const bool IsPostDominators;
  38. public:
  39. DominanceFrontierBase(bool isPostDom) : IsPostDominators(isPostDom) {}
  40. /// getRoots - Return the root blocks of the current CFG. This may include
  41. /// multiple blocks if we are computing post dominators. For forward
  42. /// dominators, this will always be a single block (the entry node).
  43. ///
  44. inline const std::vector<BlockT *> &getRoots() const {
  45. return Roots;
  46. }
  47. BlockT *getRoot() const {
  48. assert(Roots.size() == 1 && "Should always have entry node!");
  49. return Roots[0];
  50. }
  51. /// isPostDominator - Returns true if analysis based of postdoms
  52. ///
  53. bool isPostDominator() const {
  54. return IsPostDominators;
  55. }
  56. void releaseMemory() {
  57. Frontiers.clear();
  58. }
  59. // Accessor interface:
  60. typedef typename DomSetMapType::iterator iterator;
  61. typedef typename DomSetMapType::const_iterator const_iterator;
  62. iterator begin() { return Frontiers.begin(); }
  63. const_iterator begin() const { return Frontiers.begin(); }
  64. iterator end() { return Frontiers.end(); }
  65. const_iterator end() const { return Frontiers.end(); }
  66. iterator find(BlockT *B) { return Frontiers.find(B); }
  67. const_iterator find(BlockT *B) const { return Frontiers.find(B); }
  68. iterator addBasicBlock(BlockT *BB, const DomSetType &frontier) {
  69. assert(find(BB) == end() && "Block already in DominanceFrontier!");
  70. return Frontiers.insert(std::make_pair(BB, frontier)).first;
  71. }
  72. /// removeBlock - Remove basic block BB's frontier.
  73. void removeBlock(BlockT *BB);
  74. void addToFrontier(iterator I, BlockT *Node);
  75. void removeFromFrontier(iterator I, BlockT *Node);
  76. /// compareDomSet - Return false if two domsets match. Otherwise
  77. /// return true;
  78. bool compareDomSet(DomSetType &DS1, const DomSetType &DS2) const;
  79. /// compare - Return true if the other dominance frontier base matches
  80. /// this dominance frontier base. Otherwise return false.
  81. bool compare(DominanceFrontierBase<BlockT> &Other) const;
  82. /// print - Convert to human readable form
  83. ///
  84. void print(raw_ostream &OS) const;
  85. /// dump - Dump the dominance frontier to dbgs().
  86. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  87. void dump() const;
  88. #endif
  89. };
  90. //===-------------------------------------
  91. /// DominanceFrontier Class - Concrete subclass of DominanceFrontierBase that is
  92. /// used to compute a forward dominator frontiers.
  93. ///
  94. template <class BlockT>
  95. class ForwardDominanceFrontierBase : public DominanceFrontierBase<BlockT> {
  96. private:
  97. typedef GraphTraits<BlockT *> BlockTraits;
  98. public:
  99. typedef DominatorTreeBase<BlockT> DomTreeT;
  100. typedef DomTreeNodeBase<BlockT> DomTreeNodeT;
  101. typedef typename DominanceFrontierBase<BlockT>::DomSetType DomSetType;
  102. ForwardDominanceFrontierBase() : DominanceFrontierBase<BlockT>(false) {}
  103. void analyze(DomTreeT &DT) {
  104. this->Roots = DT.getRoots();
  105. assert(this->Roots.size() == 1 &&
  106. "Only one entry block for forward domfronts!");
  107. calculate(DT, DT[this->Roots[0]]);
  108. }
  109. const DomSetType &calculate(const DomTreeT &DT, const DomTreeNodeT *Node);
  110. };
  111. class DominanceFrontier : public FunctionPass {
  112. ForwardDominanceFrontierBase<BasicBlock> Base;
  113. public:
  114. typedef DominatorTreeBase<BasicBlock> DomTreeT;
  115. typedef DomTreeNodeBase<BasicBlock> DomTreeNodeT;
  116. typedef DominanceFrontierBase<BasicBlock>::DomSetType DomSetType;
  117. typedef DominanceFrontierBase<BasicBlock>::iterator iterator;
  118. typedef DominanceFrontierBase<BasicBlock>::const_iterator const_iterator;
  119. static char ID; // Pass ID, replacement for typeid
  120. DominanceFrontier();
  121. ForwardDominanceFrontierBase<BasicBlock> &getBase() { return Base; }
  122. inline const std::vector<BasicBlock *> &getRoots() const {
  123. return Base.getRoots();
  124. }
  125. BasicBlock *getRoot() const { return Base.getRoot(); }
  126. bool isPostDominator() const { return Base.isPostDominator(); }
  127. iterator begin() { return Base.begin(); }
  128. const_iterator begin() const { return Base.begin(); }
  129. iterator end() { return Base.end(); }
  130. const_iterator end() const { return Base.end(); }
  131. iterator find(BasicBlock *B) { return Base.find(B); }
  132. const_iterator find(BasicBlock *B) const { return Base.find(B); }
  133. iterator addBasicBlock(BasicBlock *BB, const DomSetType &frontier) {
  134. return Base.addBasicBlock(BB, frontier);
  135. }
  136. void removeBlock(BasicBlock *BB) { return Base.removeBlock(BB); }
  137. void addToFrontier(iterator I, BasicBlock *Node) {
  138. return Base.addToFrontier(I, Node);
  139. }
  140. void removeFromFrontier(iterator I, BasicBlock *Node) {
  141. return Base.removeFromFrontier(I, Node);
  142. }
  143. bool compareDomSet(DomSetType &DS1, const DomSetType &DS2) const {
  144. return Base.compareDomSet(DS1, DS2);
  145. }
  146. bool compare(DominanceFrontierBase<BasicBlock> &Other) const {
  147. return Base.compare(Other);
  148. }
  149. void releaseMemory() override;
  150. bool runOnFunction(Function &) override;
  151. void getAnalysisUsage(AnalysisUsage &AU) const override;
  152. void print(raw_ostream &OS, const Module * = nullptr) const override;
  153. void dump() const;
  154. };
  155. extern template class DominanceFrontierBase<BasicBlock>;
  156. extern template class ForwardDominanceFrontierBase<BasicBlock>;
  157. } // End llvm namespace
  158. #endif