DominanceFrontierImpl.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //===- llvm/Analysis/DominanceFrontier.h - Dominator Frontiers --*- 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 is the generic implementation of the DominanceFrontier class, which
  11. // calculate and holds the dominance frontier for a function for.
  12. //
  13. // This should be considered deprecated, don't add any more uses of this data
  14. // structure.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_DOMINANCEFRONTIERIMPL_H
  18. #define LLVM_ANALYSIS_DOMINANCEFRONTIERIMPL_H
  19. #include "llvm/ADT/SmallPtrSet.h"
  20. #include "llvm/Analysis/DominanceFrontier.h"
  21. #include "llvm/Support/Debug.h"
  22. #include "llvm/Support/GenericDomTree.h"
  23. namespace llvm {
  24. template <class BlockT>
  25. class DFCalculateWorkObject {
  26. public:
  27. typedef DomTreeNodeBase<BlockT> DomTreeNodeT;
  28. DFCalculateWorkObject(BlockT *B, BlockT *P, const DomTreeNodeT *N,
  29. const DomTreeNodeT *PN)
  30. : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
  31. BlockT *currentBB;
  32. BlockT *parentBB;
  33. const DomTreeNodeT *Node;
  34. const DomTreeNodeT *parentNode;
  35. };
  36. template <class BlockT>
  37. void DominanceFrontierBase<BlockT>::removeBlock(BlockT *BB) {
  38. assert(find(BB) != end() && "Block is not in DominanceFrontier!");
  39. for (iterator I = begin(), E = end(); I != E; ++I)
  40. I->second.erase(BB);
  41. Frontiers.erase(BB);
  42. }
  43. template <class BlockT>
  44. void DominanceFrontierBase<BlockT>::addToFrontier(iterator I,
  45. BlockT *Node) {
  46. assert(I != end() && "BB is not in DominanceFrontier!");
  47. assert(I->second.count(Node) && "Node is not in DominanceFrontier of BB");
  48. I->second.erase(Node);
  49. }
  50. template <class BlockT>
  51. void DominanceFrontierBase<BlockT>::removeFromFrontier(iterator I,
  52. BlockT *Node) {
  53. assert(I != end() && "BB is not in DominanceFrontier!");
  54. assert(I->second.count(Node) && "Node is not in DominanceFrontier of BB");
  55. I->second.erase(Node);
  56. }
  57. template <class BlockT>
  58. bool DominanceFrontierBase<BlockT>::compareDomSet(DomSetType &DS1,
  59. const DomSetType &DS2) const {
  60. std::set<BlockT *> tmpSet;
  61. for (BlockT *BB : DS2)
  62. tmpSet.insert(BB);
  63. for (typename DomSetType::const_iterator I = DS1.begin(), E = DS1.end();
  64. I != E;) {
  65. BlockT *Node = *I++;
  66. if (tmpSet.erase(Node) == 0)
  67. // Node is in DS1 but tnot in DS2.
  68. return true;
  69. }
  70. if (!tmpSet.empty()) {
  71. // There are nodes that are in DS2 but not in DS1.
  72. return true;
  73. }
  74. // DS1 and DS2 matches.
  75. return false;
  76. }
  77. template <class BlockT>
  78. bool DominanceFrontierBase<BlockT>::compare(
  79. DominanceFrontierBase<BlockT> &Other) const {
  80. DomSetMapType tmpFrontiers;
  81. for (typename DomSetMapType::const_iterator I = Other.begin(),
  82. E = Other.end();
  83. I != E; ++I)
  84. tmpFrontiers.insert(std::make_pair(I->first, I->second));
  85. for (typename DomSetMapType::iterator I = tmpFrontiers.begin(),
  86. E = tmpFrontiers.end();
  87. I != E;) {
  88. BlockT *Node = I->first;
  89. const_iterator DFI = find(Node);
  90. if (DFI == end())
  91. return true;
  92. if (compareDomSet(I->second, DFI->second))
  93. return true;
  94. ++I;
  95. tmpFrontiers.erase(Node);
  96. }
  97. if (!tmpFrontiers.empty())
  98. return true;
  99. return false;
  100. }
  101. template <class BlockT>
  102. void DominanceFrontierBase<BlockT>::print(raw_ostream &OS) const {
  103. for (const_iterator I = begin(), E = end(); I != E; ++I) {
  104. OS << " DomFrontier for BB ";
  105. if (I->first)
  106. I->first->printAsOperand(OS, false);
  107. else
  108. OS << " <<exit node>>";
  109. OS << " is:\t";
  110. const std::set<BlockT *> &BBs = I->second;
  111. for (const BlockT *BB : BBs) {
  112. OS << ' ';
  113. if (BB)
  114. BB->printAsOperand(OS, false);
  115. else
  116. OS << "<<exit node>>";
  117. }
  118. OS << '\n';
  119. }
  120. }
  121. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  122. template <class BlockT>
  123. void DominanceFrontierBase<BlockT>::dump() const {
  124. print(dbgs());
  125. }
  126. #endif
  127. template <class BlockT>
  128. const typename ForwardDominanceFrontierBase<BlockT>::DomSetType &
  129. ForwardDominanceFrontierBase<BlockT>::calculate(const DomTreeT &DT,
  130. const DomTreeNodeT *Node) {
  131. BlockT *BB = Node->getBlock();
  132. DomSetType *Result = nullptr;
  133. std::vector<DFCalculateWorkObject<BlockT>> workList;
  134. SmallPtrSet<BlockT *, 32> visited;
  135. workList.push_back(DFCalculateWorkObject<BlockT>(BB, nullptr, Node, nullptr));
  136. do {
  137. DFCalculateWorkObject<BlockT> *currentW = &workList.back();
  138. assert(currentW && "Missing work object.");
  139. BlockT *currentBB = currentW->currentBB;
  140. BlockT *parentBB = currentW->parentBB;
  141. const DomTreeNodeT *currentNode = currentW->Node;
  142. const DomTreeNodeT *parentNode = currentW->parentNode;
  143. assert(currentBB && "Invalid work object. Missing current Basic Block");
  144. assert(currentNode && "Invalid work object. Missing current Node");
  145. DomSetType &S = this->Frontiers[currentBB];
  146. // Visit each block only once.
  147. if (visited.insert(currentBB).second) {
  148. // Loop over CFG successors to calculate DFlocal[currentNode]
  149. for (auto SI = BlockTraits::child_begin(currentBB),
  150. SE = BlockTraits::child_end(currentBB);
  151. SI != SE; ++SI) {
  152. // Does Node immediately dominate this successor?
  153. if (DT[*SI]->getIDom() != currentNode)
  154. S.insert(*SI);
  155. }
  156. }
  157. // At this point, S is DFlocal. Now we union in DFup's of our children...
  158. // Loop through and visit the nodes that Node immediately dominates (Node's
  159. // children in the IDomTree)
  160. bool visitChild = false;
  161. for (typename DomTreeNodeT::const_iterator NI = currentNode->begin(),
  162. NE = currentNode->end();
  163. NI != NE; ++NI) {
  164. DomTreeNodeT *IDominee = *NI;
  165. BlockT *childBB = IDominee->getBlock();
  166. if (visited.count(childBB) == 0) {
  167. workList.push_back(DFCalculateWorkObject<BlockT>(
  168. childBB, currentBB, IDominee, currentNode));
  169. visitChild = true;
  170. }
  171. }
  172. // If all children are visited or there is any child then pop this block
  173. // from the workList.
  174. if (!visitChild) {
  175. if (!parentBB) {
  176. Result = &S;
  177. break;
  178. }
  179. typename DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
  180. DomSetType &parentSet = this->Frontiers[parentBB];
  181. for (; CDFI != CDFE; ++CDFI) {
  182. if (!DT.properlyDominates(parentNode, DT[*CDFI]))
  183. parentSet.insert(*CDFI);
  184. }
  185. workList.pop_back();
  186. }
  187. } while (!workList.empty());
  188. return *Result;
  189. }
  190. } // End llvm namespace
  191. #endif