blob: 9daa6bd149cec5909b00ad0aaf4e2ca9b5cbd9e1 [file] [log] [blame]
Chris Lattner52de05c2003-06-22 03:08:05 +00001//===-- Support/TarjanSCCIterator.h - Tarjan SCC iterator -------*- C++ -*-===//
Vikram S. Advea9c3afb2002-11-04 14:15:57 +00002//
3// This builds on the Support/GraphTraits.h file to find the strongly
4// connected components (SCCs) of a graph in O(N+E) time using
5// Tarjan's DFS algorithm.
6//
7// The SCC iterator has the important property that if a node in SCC S1
8// has an edge to a node in SCC S2, then it visits S1 *after* S2.
9//
10// To visit S1 *before* S2, use the TarjanSCCIterator on the Inverse graph.
11// (NOTE: This requires some simple wrappers and is not supported yet.)
Chris Lattner52de05c2003-06-22 03:08:05 +000012//
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000013//===----------------------------------------------------------------------===//
14
Brian Gaekea7a50132003-06-17 00:35:55 +000015#ifndef SUPPORT_TARJANSCCITERATOR_H
16#define SUPPORT_TARJANSCCITERATOR_H
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000017
18#include "Support/GraphTraits.h"
Chris Lattner52de05c2003-06-22 03:08:05 +000019#include "Support/iterator"
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000020#include <vector>
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000021#include <map>
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000022
Chris Lattner71e71f22003-08-31 19:55:31 +000023//===----------------------------------------------------------------------===//
24///
25/// TarjanSCC_iterator - Enumerate the SCCs of a directed graph, in
26/// reverse topological order of the SCC DAG.
27///
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000028template<class GraphT, class GT = GraphTraits<GraphT> >
Chris Lattner71e71f22003-08-31 19:55:31 +000029class TarjanSCC_iterator
30 : public forward_iterator<std::vector<typename GT::NodeType>, ptrdiff_t> {
31 typedef typename GT::NodeType NodeType;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000032 typedef typename GT::ChildIteratorType ChildItTy;
Chris Lattner71e71f22003-08-31 19:55:31 +000033 typedef std::vector<NodeType*> SccTy;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000034 typedef forward_iterator<SccTy, ptrdiff_t> super;
35 typedef typename super::reference reference;
36 typedef typename super::pointer pointer;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000037
38 // The visit counters used to detect when a complete SCC is on the stack.
39 // visitNum is the global counter.
40 // nodeVisitNumbers are per-node visit numbers, also used as DFS flags.
Chris Lattnera74a63c2003-08-31 01:48:21 +000041 unsigned visitNum;
42 std::map<NodeType *, unsigned> nodeVisitNumbers;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000043
44 // SCCNodeStack - Stack holding nodes of the SCC.
Chris Lattnera74a63c2003-08-31 01:48:21 +000045 std::vector<NodeType *> SCCNodeStack;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000046
47 // CurrentSCC - The current SCC, retrieved using operator*().
48 SccTy CurrentSCC;
49
50 // VisitStack - Used to maintain the ordering. Top = current block
51 // First element is basic block pointer, second is the 'next child' to visit
Chris Lattnera74a63c2003-08-31 01:48:21 +000052 std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000053
54 // MinVistNumStack - 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.
Chris Lattnera74a63c2003-08-31 01:48:21 +000057 std::vector<unsigned> MinVisitNumStack;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000058
59 // A single "visit" within the non-recursive DFS traversal.
60 void DFSVisitOne(NodeType* N) {
61 ++visitNum; // Global counter for the visit order
62 nodeVisitNumbers[N] = visitNum;
Chris Lattnera74a63c2003-08-31 01:48:21 +000063 SCCNodeStack.push_back(N);
64 MinVisitNumStack.push_back(visitNum);
65 VisitStack.push_back(make_pair(N, GT::child_begin(N)));
Chris Lattnerb1c0cd02003-08-31 01:45:00 +000066 //DEBUG(std::cerr << "TarjanSCC: Node " << N <<
67 // " : visitNum = " << visitNum << "\n");
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000068 }
69
70 // The stack-based DFS traversal; defined below.
71 void DFSVisitChildren() {
72 assert(!VisitStack.empty());
Chris Lattnera74a63c2003-08-31 01:48:21 +000073 while (VisitStack.back().second != GT::child_end(VisitStack.back().first))
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000074 { // TOS has at least one more child so continue DFS
Chris Lattnera74a63c2003-08-31 01:48:21 +000075 NodeType *childN = *VisitStack.back().second++;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000076 if (nodeVisitNumbers.find(childN) == nodeVisitNumbers.end())
77 { // this node has never been seen
78 DFSVisitOne(childN);
79 }
80 else
81 {
Chris Lattnera74a63c2003-08-31 01:48:21 +000082 unsigned childNum = nodeVisitNumbers[childN];
83 if (MinVisitNumStack.back() > childNum)
84 MinVisitNumStack.back() = childNum;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000085 }
86 }
87 }
88
89 // Compute the next SCC using the DFS traversal.
90 void GetNextSCC() {
91 assert(VisitStack.size() == MinVisitNumStack.size());
92 CurrentSCC.clear(); // Prepare to compute the next SCC
93 while (! VisitStack.empty())
94 {
95 DFSVisitChildren();
96
Chris Lattnera74a63c2003-08-31 01:48:21 +000097 assert(VisitStack.back().second ==
98 GT::child_end(VisitStack.back().first));
99 NodeType* visitingN = VisitStack.back().first;
100 unsigned minVisitNum = MinVisitNumStack.back();
101 VisitStack.pop_back();
102 MinVisitNumStack.pop_back();
103 if (! MinVisitNumStack.empty() && MinVisitNumStack.back() > minVisitNum)
104 MinVisitNumStack.back() = minVisitNum;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000105
Chris Lattnerb1c0cd02003-08-31 01:45:00 +0000106 //DEBUG(std::cerr << "TarjanSCC: Popped node " << visitingN <<
107 // " : minVisitNum = " << minVisitNum << "; Node visit num = " <<
108 // nodeVisitNumbers[visitingN] << "\n");
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000109
110 if (minVisitNum == nodeVisitNumbers[visitingN])
111 { // A full SCC is on the SCCNodeStack! It includes all nodes below
112 // visitingN on the stack. Copy those nodes to CurrentSCC,
113 // reset their minVisit values, and return (this suspends
114 // the DFS traversal till the next ++).
115 do {
Chris Lattnera74a63c2003-08-31 01:48:21 +0000116 CurrentSCC.push_back(SCCNodeStack.back());
117 SCCNodeStack.pop_back();
Chris Lattnerfb185592002-11-15 18:04:16 +0000118 nodeVisitNumbers[CurrentSCC.back()] = ~0UL;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000119 } while (CurrentSCC.back() != visitingN);
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000120 return;
121 }
122 }
123 }
124
125 inline TarjanSCC_iterator(NodeType *entryN) : visitNum(0) {
126 DFSVisitOne(entryN);
127 GetNextSCC();
128 }
129 inline TarjanSCC_iterator() { /* End is when DFS stack is empty */ }
130
131public:
132 typedef TarjanSCC_iterator<GraphT, GT> _Self;
133
134 // Provide static "constructors"...
135 static inline _Self begin(GraphT& G) { return _Self(GT::getEntryNode(G)); }
136 static inline _Self end (GraphT& G) { return _Self(); }
137
138 // Direct loop termination test (I.fini() is more efficient than I == end())
139 inline bool fini() const {
Vikram S. Adve80cac472002-12-06 15:02:22 +0000140 assert(!CurrentSCC.empty() || VisitStack.empty());
141 return CurrentSCC.empty();
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000142 }
143
144 inline bool operator==(const _Self& x) const {
Vikram S. Adve80cac472002-12-06 15:02:22 +0000145 return VisitStack == x.VisitStack && CurrentSCC == x.CurrentSCC;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000146 }
147 inline bool operator!=(const _Self& x) const { return !operator==(x); }
148
149 // Iterator traversal: forward iteration only
150 inline _Self& operator++() { // Preincrement
151 GetNextSCC();
152 return *this;
153 }
154 inline _Self operator++(int) { // Postincrement
155 _Self tmp = *this; ++*this; return tmp;
156 }
157
Chris Lattnerf0209062003-08-31 19:34:27 +0000158 // Retrieve a reference to the current SCC
159 inline const SccTy &operator*() const {
160 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
161 return CurrentSCC;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000162 }
Chris Lattnerf0209062003-08-31 19:34:27 +0000163 inline SccTy &operator*() {
164 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
165 return CurrentSCC;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000166 }
Chris Lattner5cac4dd2003-08-31 19:51:22 +0000167
168 // hasLoop() -- Test if the current SCC has a loop. If it has more than one
169 // node, this is trivially true. If not, it may still contain a loop if the
170 // node has an edge back to itself.
171 bool hasLoop() const {
172 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
173 if (CurrentSCC.size() > 1) return true;
174 NodeType *N = CurrentSCC.front();
175 for (ChildItTy CI = GT::child_begin(N), CE=GT::child_end(N); CI != CE; ++CI)
176 if (*CI == N)
177 return true;
178 return false;
179 }
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000180};
181
182
Chris Lattnerf0209062003-08-31 19:34:27 +0000183// Global constructor for the Tarjan SCC iterator.
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000184template <class T>
Chris Lattnerf0209062003-08-31 19:34:27 +0000185TarjanSCC_iterator<T> tarj_begin(T G) {
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000186 return TarjanSCC_iterator<T>::begin(G);
187}
188
Chris Lattnerc0a07a42002-11-10 23:46:31 +0000189template <class T>
Chris Lattnerf0209062003-08-31 19:34:27 +0000190TarjanSCC_iterator<T> tarj_end(T G) {
Chris Lattnerc0a07a42002-11-10 23:46:31 +0000191 return TarjanSCC_iterator<T>::end(G);
192}
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000193
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000194#endif