1 //===-- Support/SCCIterator.h - SCC iterator --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This builds on the Support/GraphTraits.h file to find the strongly connected
11 // components (SCCs) of a graph in O(N+E) time using Tarjan's DFS algorithm.
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.
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.)
19 //===----------------------------------------------------------------------===//
21 #ifndef SUPPORT_SCCITERATOR_H
22 #define SUPPORT_SCCITERATOR_H
24 #include "Support/GraphTraits.h"
25 #include "Support/iterator"
29 //===----------------------------------------------------------------------===//
31 /// scc_iterator - Enumerate the SCCs of a directed graph, in
32 /// reverse topological order of the SCC DAG.
34 template<class GraphT, class GT = GraphTraits<GraphT> >
36 : public forward_iterator<std::vector<typename GT::NodeType>, ptrdiff_t> {
37 typedef typename GT::NodeType NodeType;
38 typedef typename GT::ChildIteratorType ChildItTy;
39 typedef std::vector<NodeType*> SccTy;
40 typedef forward_iterator<SccTy, ptrdiff_t> super;
41 typedef typename super::reference reference;
42 typedef typename super::pointer pointer;
44 // The visit counters used to detect when a complete SCC is on the stack.
45 // visitNum is the global counter.
46 // nodeVisitNumbers are per-node visit numbers, also used as DFS flags.
48 std::map<NodeType *, unsigned> nodeVisitNumbers;
50 // SCCNodeStack - Stack holding nodes of the SCC.
51 std::vector<NodeType *> SCCNodeStack;
53 // CurrentSCC - The current SCC, retrieved using operator*().
56 // VisitStack - Used to maintain the ordering. Top = current block
57 // First element is basic block pointer, second is the 'next child' to visit
58 std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
60 // MinVistNumStack - Stack holding the "min" values for each node in the DFS.
61 // This is used to track the minimum uplink values for all children of
62 // the corresponding node on the VisitStack.
63 std::vector<unsigned> MinVisitNumStack;
65 // A single "visit" within the non-recursive DFS traversal.
66 void DFSVisitOne(NodeType* N) {
67 ++visitNum; // Global counter for the visit order
68 nodeVisitNumbers[N] = visitNum;
69 SCCNodeStack.push_back(N);
70 MinVisitNumStack.push_back(visitNum);
71 VisitStack.push_back(make_pair(N, GT::child_begin(N)));
72 //DEBUG(std::cerr << "TarjanSCC: Node " << N <<
73 // " : visitNum = " << visitNum << "\n");
76 // The stack-based DFS traversal; defined below.
77 void DFSVisitChildren() {
78 assert(!VisitStack.empty());
79 while (VisitStack.back().second != GT::child_end(VisitStack.back().first))
80 { // TOS has at least one more child so continue DFS
81 NodeType *childN = *VisitStack.back().second++;
82 if (nodeVisitNumbers.find(childN) == nodeVisitNumbers.end())
83 { // this node has never been seen
88 unsigned childNum = nodeVisitNumbers[childN];
89 if (MinVisitNumStack.back() > childNum)
90 MinVisitNumStack.back() = childNum;
95 // Compute the next SCC using the DFS traversal.
97 assert(VisitStack.size() == MinVisitNumStack.size());
98 CurrentSCC.clear(); // Prepare to compute the next SCC
99 while (! VisitStack.empty())
103 assert(VisitStack.back().second ==
104 GT::child_end(VisitStack.back().first));
105 NodeType* visitingN = VisitStack.back().first;
106 unsigned minVisitNum = MinVisitNumStack.back();
107 VisitStack.pop_back();
108 MinVisitNumStack.pop_back();
109 if (! MinVisitNumStack.empty() && MinVisitNumStack.back() > minVisitNum)
110 MinVisitNumStack.back() = minVisitNum;
112 //DEBUG(std::cerr << "TarjanSCC: Popped node " << visitingN <<
113 // " : minVisitNum = " << minVisitNum << "; Node visit num = " <<
114 // nodeVisitNumbers[visitingN] << "\n");
116 if (minVisitNum == nodeVisitNumbers[visitingN])
117 { // A full SCC is on the SCCNodeStack! It includes all nodes below
118 // visitingN on the stack. Copy those nodes to CurrentSCC,
119 // reset their minVisit values, and return (this suspends
120 // the DFS traversal till the next ++).
122 CurrentSCC.push_back(SCCNodeStack.back());
123 SCCNodeStack.pop_back();
124 nodeVisitNumbers[CurrentSCC.back()] = ~0UL;
125 } while (CurrentSCC.back() != visitingN);
131 inline scc_iterator(NodeType *entryN) : visitNum(0) {
135 inline scc_iterator() { /* End is when DFS stack is empty */ }
138 typedef scc_iterator<GraphT, GT> _Self;
140 // Provide static "constructors"...
141 static inline _Self begin(GraphT& G) { return _Self(GT::getEntryNode(G)); }
142 static inline _Self end (GraphT& G) { return _Self(); }
144 // Direct loop termination test (I.fini() is more efficient than I == end())
145 inline bool fini() const {
146 assert(!CurrentSCC.empty() || VisitStack.empty());
147 return CurrentSCC.empty();
150 inline bool operator==(const _Self& x) const {
151 return VisitStack == x.VisitStack && CurrentSCC == x.CurrentSCC;
153 inline bool operator!=(const _Self& x) const { return !operator==(x); }
155 // Iterator traversal: forward iteration only
156 inline _Self& operator++() { // Preincrement
160 inline _Self operator++(int) { // Postincrement
161 _Self tmp = *this; ++*this; return tmp;
164 // Retrieve a reference to the current SCC
165 inline const SccTy &operator*() const {
166 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
169 inline SccTy &operator*() {
170 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
174 // hasLoop() -- Test if the current SCC has a loop. If it has more than one
175 // node, this is trivially true. If not, it may still contain a loop if the
176 // node has an edge back to itself.
177 bool hasLoop() const {
178 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
179 if (CurrentSCC.size() > 1) return true;
180 NodeType *N = CurrentSCC.front();
181 for (ChildItTy CI = GT::child_begin(N), CE=GT::child_end(N); CI != CE; ++CI)
189 // Global constructor for the SCC iterator.
191 scc_iterator<T> scc_begin(T G) {
192 return scc_iterator<T>::begin(G);
196 scc_iterator<T> scc_end(T G) {
197 return scc_iterator<T>::end(G);