DominanceFrontierImpl.h 7.1 KB

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