MachineDominators.h 9.6 KB

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