MachineDominators.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //=- llvm/CodeGen/MachineDominators.h - Machine Dom 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 classes mirroring those in llvm/Analysis/Dominators.h,
  11. // but for target-specific code rather than target-independent IR.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CODEGEN_MACHINEDOMINATORS_H
  15. #define LLVM_CODEGEN_MACHINEDOMINATORS_H
  16. #include "llvm/ADT/SmallSet.h"
  17. #include "llvm/CodeGen/MachineBasicBlock.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineFunctionPass.h"
  20. #include "llvm/Support/GenericDomTree.h"
  21. #include "llvm/Support/GenericDomTreeConstruction.h"
  22. namespace llvm {
  23. template<>
  24. inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) {
  25. this->Roots.push_back(MBB);
  26. }
  27. extern template class DomTreeNodeBase<MachineBasicBlock>;
  28. extern template class DominatorTreeBase<MachineBasicBlock>;
  29. typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
  30. //===-------------------------------------
  31. /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
  32. /// compute a normal dominator tree.
  33. ///
  34. class MachineDominatorTree : public MachineFunctionPass {
  35. /// \brief Helper structure used to hold all the basic blocks
  36. /// involved in the split of a critical edge.
  37. struct CriticalEdge {
  38. MachineBasicBlock *FromBB;
  39. MachineBasicBlock *ToBB;
  40. MachineBasicBlock *NewBB;
  41. };
  42. /// \brief Pile up all the critical edges to be split.
  43. /// The splitting of a critical edge is local and thus, it is possible
  44. /// to apply several of those changes at the same time.
  45. mutable SmallVector<CriticalEdge, 32> CriticalEdgesToSplit;
  46. /// \brief Remember all the basic blocks that are inserted during
  47. /// edge splitting.
  48. /// Invariant: NewBBs == all the basic blocks contained in the NewBB
  49. /// field of all the elements of CriticalEdgesToSplit.
  50. /// I.e., forall elt in CriticalEdgesToSplit, it exists BB in NewBBs
  51. /// such as BB == elt.NewBB.
  52. mutable SmallSet<MachineBasicBlock *, 32> NewBBs;
  53. /// \brief Apply all the recorded critical edges to the DT.
  54. /// This updates the underlying DT information in a way that uses
  55. /// the fast query path of DT as much as possible.
  56. ///
  57. /// \post CriticalEdgesToSplit.empty().
  58. void applySplitCriticalEdges() const;
  59. public:
  60. static char ID; // Pass ID, replacement for typeid
  61. DominatorTreeBase<MachineBasicBlock>* DT;
  62. MachineDominatorTree();
  63. ~MachineDominatorTree() override;
  64. DominatorTreeBase<MachineBasicBlock> &getBase() {
  65. applySplitCriticalEdges();
  66. return *DT;
  67. }
  68. void getAnalysisUsage(AnalysisUsage &AU) const override;
  69. /// getRoots - Return the root blocks of the current CFG. This may include
  70. /// multiple blocks if we are computing post dominators. For forward
  71. /// dominators, this will always be a single block (the entry node).
  72. ///
  73. inline const std::vector<MachineBasicBlock*> &getRoots() const {
  74. applySplitCriticalEdges();
  75. return DT->getRoots();
  76. }
  77. inline MachineBasicBlock *getRoot() const {
  78. applySplitCriticalEdges();
  79. return DT->getRoot();
  80. }
  81. inline MachineDomTreeNode *getRootNode() const {
  82. applySplitCriticalEdges();
  83. return DT->getRootNode();
  84. }
  85. bool runOnMachineFunction(MachineFunction &F) override;
  86. inline bool dominates(const MachineDomTreeNode* A,
  87. const MachineDomTreeNode* B) const {
  88. applySplitCriticalEdges();
  89. return DT->dominates(A, B);
  90. }
  91. inline bool dominates(const MachineBasicBlock* A,
  92. const MachineBasicBlock* B) const {
  93. applySplitCriticalEdges();
  94. return DT->dominates(A, B);
  95. }
  96. // dominates - Return true if A dominates B. This performs the
  97. // special checks necessary if A and B are in the same basic block.
  98. bool dominates(const MachineInstr *A, const MachineInstr *B) const {
  99. applySplitCriticalEdges();
  100. const MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
  101. if (BBA != BBB) return DT->dominates(BBA, BBB);
  102. // Loop through the basic block until we find A or B.
  103. MachineBasicBlock::const_iterator I = BBA->begin();
  104. for (; &*I != A && &*I != B; ++I)
  105. /*empty*/ ;
  106. //if(!DT.IsPostDominators) {
  107. // A dominates B if it is found first in the basic block.
  108. return &*I == A;
  109. //} else {
  110. // // A post-dominates B if B is found first in the basic block.
  111. // return &*I == B;
  112. //}
  113. }
  114. inline bool properlyDominates(const MachineDomTreeNode* A,
  115. const MachineDomTreeNode* B) const {
  116. applySplitCriticalEdges();
  117. return DT->properlyDominates(A, B);
  118. }
  119. inline bool properlyDominates(const MachineBasicBlock* A,
  120. const MachineBasicBlock* B) const {
  121. applySplitCriticalEdges();
  122. return DT->properlyDominates(A, B);
  123. }
  124. /// findNearestCommonDominator - Find nearest common dominator basic block
  125. /// for basic block A and B. If there is no such block then return NULL.
  126. inline MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A,
  127. MachineBasicBlock *B) {
  128. applySplitCriticalEdges();
  129. return DT->findNearestCommonDominator(A, B);
  130. }
  131. inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
  132. applySplitCriticalEdges();
  133. return DT->getNode(BB);
  134. }
  135. /// getNode - return the (Post)DominatorTree node for the specified basic
  136. /// block. This is the same as using operator[] on this class.
  137. ///
  138. inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
  139. applySplitCriticalEdges();
  140. return DT->getNode(BB);
  141. }
  142. /// addNewBlock - Add a new node to the dominator tree information. This
  143. /// creates a new node as a child of DomBB dominator node,linking it into
  144. /// the children list of the immediate dominator.
  145. inline MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB,
  146. MachineBasicBlock *DomBB) {
  147. applySplitCriticalEdges();
  148. return DT->addNewBlock(BB, DomBB);
  149. }
  150. /// changeImmediateDominator - This method is used to update the dominator
  151. /// tree information when a node's immediate dominator changes.
  152. ///
  153. inline void changeImmediateDominator(MachineBasicBlock *N,
  154. MachineBasicBlock* NewIDom) {
  155. applySplitCriticalEdges();
  156. DT->changeImmediateDominator(N, NewIDom);
  157. }
  158. inline void changeImmediateDominator(MachineDomTreeNode *N,
  159. MachineDomTreeNode* NewIDom) {
  160. applySplitCriticalEdges();
  161. DT->changeImmediateDominator(N, NewIDom);
  162. }
  163. /// eraseNode - Removes a node from the dominator tree. Block must not
  164. /// dominate any other blocks. Removes node from its immediate dominator's
  165. /// children list. Deletes dominator node associated with basic block BB.
  166. inline void eraseNode(MachineBasicBlock *BB) {
  167. applySplitCriticalEdges();
  168. DT->eraseNode(BB);
  169. }
  170. /// splitBlock - BB is split and now it has one successor. Update dominator
  171. /// tree to reflect this change.
  172. inline void splitBlock(MachineBasicBlock* NewBB) {
  173. applySplitCriticalEdges();
  174. DT->splitBlock(NewBB);
  175. }
  176. /// isReachableFromEntry - Return true if A is dominated by the entry
  177. /// block of the function containing it.
  178. bool isReachableFromEntry(const MachineBasicBlock *A) {
  179. applySplitCriticalEdges();
  180. return DT->isReachableFromEntry(A);
  181. }
  182. void releaseMemory() override;
  183. void print(raw_ostream &OS, const Module*) const override;
  184. /// \brief Record that the critical edge (FromBB, ToBB) has been
  185. /// split with NewBB.
  186. /// This is best to use this method instead of directly update the
  187. /// underlying information, because this helps mitigating the
  188. /// number of time the DT information is invalidated.
  189. ///
  190. /// \note Do not use this method with regular edges.
  191. ///
  192. /// \note To benefit from the compile time improvement incurred by this
  193. /// method, the users of this method have to limit the queries to the DT
  194. /// interface between two edges splitting. In other words, they have to
  195. /// pack the splitting of critical edges as much as possible.
  196. void recordSplitCriticalEdge(MachineBasicBlock *FromBB,
  197. MachineBasicBlock *ToBB,
  198. MachineBasicBlock *NewBB) {
  199. bool Inserted = NewBBs.insert(NewBB).second;
  200. (void)Inserted;
  201. assert(Inserted &&
  202. "A basic block inserted via edge splitting cannot appear twice");
  203. CriticalEdgesToSplit.push_back({FromBB, ToBB, NewBB});
  204. }
  205. };
  206. //===-------------------------------------
  207. /// DominatorTree GraphTraits specialization so the DominatorTree can be
  208. /// iterable by generic graph iterators.
  209. ///
  210. template<class T> struct GraphTraits;
  211. template <> struct GraphTraits<MachineDomTreeNode *> {
  212. typedef MachineDomTreeNode NodeType;
  213. typedef NodeType::iterator ChildIteratorType;
  214. static NodeType *getEntryNode(NodeType *N) {
  215. return N;
  216. }
  217. static inline ChildIteratorType child_begin(NodeType* N) {
  218. return N->begin();
  219. }
  220. static inline ChildIteratorType child_end(NodeType* N) {
  221. return N->end();
  222. }
  223. };
  224. template <> struct GraphTraits<MachineDominatorTree*>
  225. : public GraphTraits<MachineDomTreeNode *> {
  226. static NodeType *getEntryNode(MachineDominatorTree *DT) {
  227. return DT->getRootNode();
  228. }
  229. };
  230. }
  231. #endif