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.

220 lines
7.7 KiB

  1. //===---- ADT/SCCIterator.h - Strongly Connected Comp. Iter. ----*- 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 builds on the llvm/ADT/GraphTraits.h file to find the strongly connected
  11. // components (SCCs) of a graph in O(N+E) time using Tarjan's DFS algorithm.
  12. //
  13. // The SCC iterator has the important property that if a node in SCC S1 has an
  14. // edge to a node in SCC S2, then it visits S1 *after* S2.
  15. //
  16. // To visit S1 *before* S2, use the scc_iterator on the Inverse graph.
  17. // (NOTE: This requires some simple wrappers and is not supported yet.)
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_ADT_SCCITERATOR_H
  21. #define LLVM_ADT_SCCITERATOR_H
  22. #include "llvm/ADT/DenseMap.h"
  23. #include "llvm/ADT/GraphTraits.h"
  24. #include <vector>
  25. namespace llvm {
  26. //===----------------------------------------------------------------------===//
  27. ///
  28. /// scc_iterator - Enumerate the SCCs of a directed graph, in
  29. /// reverse topological order of the SCC DAG.
  30. ///
  31. template<class GraphT, class GT = GraphTraits<GraphT> >
  32. class scc_iterator
  33. : public std::iterator<std::forward_iterator_tag,
  34. std::vector<typename GT::NodeType>, ptrdiff_t> {
  35. typedef typename GT::NodeType NodeType;
  36. typedef typename GT::ChildIteratorType ChildItTy;
  37. typedef std::vector<NodeType*> SccTy;
  38. typedef std::iterator<std::forward_iterator_tag,
  39. std::vector<typename GT::NodeType>, ptrdiff_t> super;
  40. typedef typename super::reference reference;
  41. typedef typename super::pointer pointer;
  42. // The visit counters used to detect when a complete SCC is on the stack.
  43. // visitNum is the global counter.
  44. // nodeVisitNumbers are per-node visit numbers, also used as DFS flags.
  45. unsigned visitNum;
  46. DenseMap<NodeType *, unsigned> nodeVisitNumbers;
  47. // SCCNodeStack - Stack holding nodes of the SCC.
  48. std::vector<NodeType *> SCCNodeStack;
  49. // CurrentSCC - The current SCC, retrieved using operator*().
  50. SccTy CurrentSCC;
  51. // VisitStack - Used to maintain the ordering. Top = current block
  52. // First element is basic block pointer, second is the 'next child' to visit
  53. std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
  54. // MinVisitNumStack - Stack holding the "min" values for each node in the DFS.
  55. // This is used to track the minimum uplink values for all children of
  56. // the corresponding node on the VisitStack.
  57. std::vector<unsigned> MinVisitNumStack;
  58. // A single "visit" within the non-recursive DFS traversal.
  59. void DFSVisitOne(NodeType *N) {
  60. ++visitNum; // Global counter for the visit order
  61. nodeVisitNumbers[N] = visitNum;
  62. SCCNodeStack.push_back(N);
  63. MinVisitNumStack.push_back(visitNum);
  64. VisitStack.push_back(std::make_pair(N, GT::child_begin(N)));
  65. //dbgs() << "TarjanSCC: Node " << N <<
  66. // " : visitNum = " << visitNum << "\n";
  67. }
  68. // The stack-based DFS traversal; defined below.
  69. void DFSVisitChildren() {
  70. assert(!VisitStack.empty());
  71. while (VisitStack.back().second != GT::child_end(VisitStack.back().first)) {
  72. // TOS has at least one more child so continue DFS
  73. NodeType *childN = *VisitStack.back().second++;
  74. if (!nodeVisitNumbers.count(childN)) {
  75. // this node has never been seen.
  76. DFSVisitOne(childN);
  77. continue;
  78. }
  79. unsigned childNum = nodeVisitNumbers[childN];
  80. if (MinVisitNumStack.back() > childNum)
  81. MinVisitNumStack.back() = childNum;
  82. }
  83. }
  84. // Compute the next SCC using the DFS traversal.
  85. void GetNextSCC() {
  86. assert(VisitStack.size() == MinVisitNumStack.size());
  87. CurrentSCC.clear(); // Prepare to compute the next SCC
  88. while (!VisitStack.empty()) {
  89. DFSVisitChildren();
  90. assert(VisitStack.back().second ==GT::child_end(VisitStack.back().first));
  91. NodeType *visitingN = VisitStack.back().first;
  92. unsigned minVisitNum = MinVisitNumStack.back();
  93. VisitStack.pop_back();
  94. MinVisitNumStack.pop_back();
  95. if (!MinVisitNumStack.empty() && MinVisitNumStack.back() > minVisitNum)
  96. MinVisitNumStack.back() = minVisitNum;
  97. //dbgs() << "TarjanSCC: Popped node " << visitingN <<
  98. // " : minVisitNum = " << minVisitNum << "; Node visit num = " <<
  99. // nodeVisitNumbers[visitingN] << "\n";
  100. if (minVisitNum != nodeVisitNumbers[visitingN])
  101. continue;
  102. // A full SCC is on the SCCNodeStack! It includes all nodes below
  103. // visitingN on the stack. Copy those nodes to CurrentSCC,
  104. // reset their minVisit values, and return (this suspends
  105. // the DFS traversal till the next ++).
  106. do {
  107. CurrentSCC.push_back(SCCNodeStack.back());
  108. SCCNodeStack.pop_back();
  109. nodeVisitNumbers[CurrentSCC.back()] = ~0U;
  110. } while (CurrentSCC.back() != visitingN);
  111. return;
  112. }
  113. }
  114. inline scc_iterator(NodeType *entryN) : visitNum(0) {
  115. DFSVisitOne(entryN);
  116. GetNextSCC();
  117. }
  118. inline scc_iterator() { /* End is when DFS stack is empty */ }
  119. public:
  120. typedef scc_iterator<GraphT, GT> _Self;
  121. // Provide static "constructors"...
  122. static inline _Self begin(const GraphT &G){return _Self(GT::getEntryNode(G));}
  123. static inline _Self end (const GraphT &) { return _Self(); }
  124. // Direct loop termination test: I.isAtEnd() is more efficient than I == end()
  125. inline bool isAtEnd() const {
  126. assert(!CurrentSCC.empty() || VisitStack.empty());
  127. return CurrentSCC.empty();
  128. }
  129. inline bool operator==(const _Self& x) const {
  130. return VisitStack == x.VisitStack && CurrentSCC == x.CurrentSCC;
  131. }
  132. inline bool operator!=(const _Self& x) const { return !operator==(x); }
  133. // Iterator traversal: forward iteration only
  134. inline _Self& operator++() { // Preincrement
  135. GetNextSCC();
  136. return *this;
  137. }
  138. inline _Self operator++(int) { // Postincrement
  139. _Self tmp = *this; ++*this; return tmp;
  140. }
  141. // Retrieve a reference to the current SCC
  142. inline const SccTy &operator*() const {
  143. assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
  144. return CurrentSCC;
  145. }
  146. inline SccTy &operator*() {
  147. assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
  148. return CurrentSCC;
  149. }
  150. // hasLoop() -- Test if the current SCC has a loop. If it has more than one
  151. // node, this is trivially true. If not, it may still contain a loop if the
  152. // node has an edge back to itself.
  153. bool hasLoop() const {
  154. assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
  155. if (CurrentSCC.size() > 1) return true;
  156. NodeType *N = CurrentSCC.front();
  157. for (ChildItTy CI = GT::child_begin(N), CE=GT::child_end(N); CI != CE; ++CI)
  158. if (*CI == N)
  159. return true;
  160. return false;
  161. }
  162. /// ReplaceNode - This informs the scc_iterator that the specified Old node
  163. /// has been deleted, and New is to be used in its place.
  164. void ReplaceNode(NodeType *Old, NodeType *New) {
  165. assert(nodeVisitNumbers.count(Old) && "Old not in scc_iterator?");
  166. nodeVisitNumbers[New] = nodeVisitNumbers[Old];
  167. nodeVisitNumbers.erase(Old);
  168. }
  169. };
  170. // Global constructor for the SCC iterator.
  171. template <class T>
  172. scc_iterator<T> scc_begin(const T &G) {
  173. return scc_iterator<T>::begin(G);
  174. }
  175. template <class T>
  176. scc_iterator<T> scc_end(const T &G) {
  177. return scc_iterator<T>::end(G);
  178. }
  179. template <class T>
  180. scc_iterator<Inverse<T> > scc_begin(const Inverse<T> &G) {
  181. return scc_iterator<Inverse<T> >::begin(G);
  182. }
  183. template <class T>
  184. scc_iterator<Inverse<T> > scc_end(const Inverse<T> &G) {
  185. return scc_iterator<Inverse<T> >::end(G);
  186. }
  187. } // End llvm namespace
  188. #endif