DominanceFrontierImpl.h 7.3 KB

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