MachineDominators.h 10 KB

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