MachineDominators.h 9.9 KB

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