DominanceFrontierImpl.h 7.3 KB

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