Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
6.7 KiB

  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/Analysis/DominatorInternals.h"
  17. #include "llvm/Analysis/Dominators.h"
  18. #include "llvm/CodeGen/MachineBasicBlock.h"
  19. #include "llvm/CodeGen/MachineFunction.h"
  20. #include "llvm/CodeGen/MachineFunctionPass.h"
  21. namespace llvm {
  22. template<>
  23. inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) {
  24. this->Roots.push_back(MBB);
  25. }
  26. EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
  27. EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<MachineBasicBlock>);
  28. typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
  29. //===-------------------------------------
  30. /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
  31. /// compute a normal dominator tree.
  32. ///
  33. class MachineDominatorTree : public MachineFunctionPass {
  34. public:
  35. static char ID; // Pass ID, replacement for typeid
  36. DominatorTreeBase<MachineBasicBlock>* DT;
  37. MachineDominatorTree();
  38. ~MachineDominatorTree();
  39. DominatorTreeBase<MachineBasicBlock>& getBase() { return *DT; }
  40. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  41. /// getRoots - Return the root blocks of the current CFG. This may include
  42. /// multiple blocks if we are computing post dominators. For forward
  43. /// dominators, this will always be a single block (the entry node).
  44. ///
  45. inline const std::vector<MachineBasicBlock*> &getRoots() const {
  46. return DT->getRoots();
  47. }
  48. inline MachineBasicBlock *getRoot() const {
  49. return DT->getRoot();
  50. }
  51. inline MachineDomTreeNode *getRootNode() const {
  52. return DT->getRootNode();
  53. }
  54. virtual bool runOnMachineFunction(MachineFunction &F);
  55. inline bool dominates(const MachineDomTreeNode* A,
  56. const MachineDomTreeNode* B) const {
  57. return DT->dominates(A, B);
  58. }
  59. inline bool dominates(const MachineBasicBlock* A,
  60. const MachineBasicBlock* B) const {
  61. return DT->dominates(A, B);
  62. }
  63. // dominates - Return true if A dominates B. This performs the
  64. // special checks necessary if A and B are in the same basic block.
  65. bool dominates(const MachineInstr *A, const MachineInstr *B) const {
  66. const MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
  67. if (BBA != BBB) return DT->dominates(BBA, BBB);
  68. // Loop through the basic block until we find A or B.
  69. MachineBasicBlock::const_iterator I = BBA->begin();
  70. for (; &*I != A && &*I != B; ++I)
  71. /*empty*/ ;
  72. //if(!DT.IsPostDominators) {
  73. // A dominates B if it is found first in the basic block.
  74. return &*I == A;
  75. //} else {
  76. // // A post-dominates B if B is found first in the basic block.
  77. // return &*I == B;
  78. //}
  79. }
  80. inline bool properlyDominates(const MachineDomTreeNode* A,
  81. const MachineDomTreeNode* B) const {
  82. return DT->properlyDominates(A, B);
  83. }
  84. inline bool properlyDominates(const MachineBasicBlock* A,
  85. const MachineBasicBlock* B) const {
  86. return DT->properlyDominates(A, B);
  87. }
  88. /// findNearestCommonDominator - Find nearest common dominator basic block
  89. /// for basic block A and B. If there is no such block then return NULL.
  90. inline MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A,
  91. MachineBasicBlock *B) {
  92. return DT->findNearestCommonDominator(A, B);
  93. }
  94. inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
  95. return DT->getNode(BB);
  96. }
  97. /// getNode - return the (Post)DominatorTree node for the specified basic
  98. /// block. This is the same as using operator[] on this class.
  99. ///
  100. inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
  101. return DT->getNode(BB);
  102. }
  103. /// addNewBlock - Add a new node to the dominator tree information. This
  104. /// creates a new node as a child of DomBB dominator node,linking it into
  105. /// the children list of the immediate dominator.
  106. inline MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB,
  107. MachineBasicBlock *DomBB) {
  108. return DT->addNewBlock(BB, DomBB);
  109. }
  110. /// changeImmediateDominator - This method is used to update the dominator
  111. /// tree information when a node's immediate dominator changes.
  112. ///
  113. inline void changeImmediateDominator(MachineBasicBlock *N,
  114. MachineBasicBlock* NewIDom) {
  115. DT->changeImmediateDominator(N, NewIDom);
  116. }
  117. inline void changeImmediateDominator(MachineDomTreeNode *N,
  118. MachineDomTreeNode* NewIDom) {
  119. DT->changeImmediateDominator(N, NewIDom);
  120. }
  121. /// eraseNode - Removes a node from the dominator tree. Block must not
  122. /// dominate any other blocks. Removes node from its immediate dominator's
  123. /// children list. Deletes dominator node associated with basic block BB.
  124. inline void eraseNode(MachineBasicBlock *BB) {
  125. DT->eraseNode(BB);
  126. }
  127. /// splitBlock - BB is split and now it has one successor. Update dominator
  128. /// tree to reflect this change.
  129. inline void splitBlock(MachineBasicBlock* NewBB) {
  130. DT->splitBlock(NewBB);
  131. }
  132. /// isReachableFromEntry - Return true if A is dominated by the entry
  133. /// block of the function containing it.
  134. bool isReachableFromEntry(const MachineBasicBlock *A) {
  135. return DT->isReachableFromEntry(A);
  136. }
  137. virtual void releaseMemory();
  138. virtual void print(raw_ostream &OS, const Module*) const;
  139. };
  140. //===-------------------------------------
  141. /// DominatorTree GraphTraits specialization so the DominatorTree can be
  142. /// iterable by generic graph iterators.
  143. ///
  144. template<class T> struct GraphTraits;
  145. template <> struct GraphTraits<MachineDomTreeNode *> {
  146. typedef MachineDomTreeNode NodeType;
  147. typedef NodeType::iterator ChildIteratorType;
  148. static NodeType *getEntryNode(NodeType *N) {
  149. return N;
  150. }
  151. static inline ChildIteratorType child_begin(NodeType* N) {
  152. return N->begin();
  153. }
  154. static inline ChildIteratorType child_end(NodeType* N) {
  155. return N->end();
  156. }
  157. };
  158. template <> struct GraphTraits<MachineDominatorTree*>
  159. : public GraphTraits<MachineDomTreeNode *> {
  160. static NodeType *getEntryNode(MachineDominatorTree *DT) {
  161. return DT->getRootNode();
  162. }
  163. };
  164. }
  165. #endif