blob: e8c4af6c8f22d851f3c99a114eda73a67f6ab34b [file] [log] [blame]
Chris Lattner78a5c1a2003-08-31 20:01:57 +00001//===-- Support/SCCIterator.h - SCC iterator --------------------*- C++ -*-===//
Vikram S. Advea9c3afb2002-11-04 14:15:57 +00002//
Chris Lattner78a5c1a2003-08-31 20:01:57 +00003// This builds on the Support/GraphTraits.h file to find the strongly connected
4// components (SCCs) of a graph in O(N+E) time using Tarjan's DFS algorithm.
Vikram S. Advea9c3afb2002-11-04 14:15:57 +00005//
Chris Lattner78a5c1a2003-08-31 20:01:57 +00006// The SCC iterator has the important property that if a node in SCC S1 has an
7// edge to a node in SCC S2, then it visits S1 *after* S2.
Vikram S. Advea9c3afb2002-11-04 14:15:57 +00008//
Chris Lattner78a5c1a2003-08-31 20:01:57 +00009// To visit S1 *before* S2, use the scc_iterator on the Inverse graph.
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000010// (NOTE: This requires some simple wrappers and is not supported yet.)
Chris Lattner52de05c2003-06-22 03:08:05 +000011//
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000012//===----------------------------------------------------------------------===//
13
Chris Lattner78a5c1a2003-08-31 20:01:57 +000014#ifndef SUPPORT_SCCITERATOR_H
15#define SUPPORT_SCCITERATOR_H
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000016
17#include "Support/GraphTraits.h"
Chris Lattner52de05c2003-06-22 03:08:05 +000018#include "Support/iterator"
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000019#include <vector>
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000020#include <map>
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000021
Chris Lattner71e71f22003-08-31 19:55:31 +000022//===----------------------------------------------------------------------===//
23///
Chris Lattner78a5c1a2003-08-31 20:01:57 +000024/// scc_iterator - Enumerate the SCCs of a directed graph, in
Chris Lattner71e71f22003-08-31 19:55:31 +000025/// reverse topological order of the SCC DAG.
26///
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000027template<class GraphT, class GT = GraphTraits<GraphT> >
Chris Lattner78a5c1a2003-08-31 20:01:57 +000028class scc_iterator
Chris Lattner71e71f22003-08-31 19:55:31 +000029 : public forward_iterator<std::vector<typename GT::NodeType>, ptrdiff_t> {
30 typedef typename GT::NodeType NodeType;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000031 typedef typename GT::ChildIteratorType ChildItTy;
Chris Lattner71e71f22003-08-31 19:55:31 +000032 typedef std::vector<NodeType*> SccTy;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000033 typedef forward_iterator<SccTy, ptrdiff_t> super;
34 typedef typename super::reference reference;
35 typedef typename super::pointer pointer;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000036
37 // The visit counters used to detect when a complete SCC is on the stack.
38 // visitNum is the global counter.
39 // nodeVisitNumbers are per-node visit numbers, also used as DFS flags.
Chris Lattnera74a63c2003-08-31 01:48:21 +000040 unsigned visitNum;
41 std::map<NodeType *, unsigned> nodeVisitNumbers;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000042
43 // SCCNodeStack - Stack holding nodes of the SCC.
Chris Lattnera74a63c2003-08-31 01:48:21 +000044 std::vector<NodeType *> SCCNodeStack;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000045
46 // CurrentSCC - The current SCC, retrieved using operator*().
47 SccTy CurrentSCC;
48
49 // VisitStack - Used to maintain the ordering. Top = current block
50 // First element is basic block pointer, second is the 'next child' to visit
Chris Lattnera74a63c2003-08-31 01:48:21 +000051 std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000052
53 // MinVistNumStack - Stack holding the "min" values for each node in the DFS.
54 // This is used to track the minimum uplink values for all children of
55 // the corresponding node on the VisitStack.
Chris Lattnera74a63c2003-08-31 01:48:21 +000056 std::vector<unsigned> MinVisitNumStack;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000057
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;
Chris Lattnera74a63c2003-08-31 01:48:21 +000062 SCCNodeStack.push_back(N);
63 MinVisitNumStack.push_back(visitNum);
64 VisitStack.push_back(make_pair(N, GT::child_begin(N)));
Chris Lattnerb1c0cd02003-08-31 01:45:00 +000065 //DEBUG(std::cerr << "TarjanSCC: Node " << N <<
66 // " : visitNum = " << visitNum << "\n");
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000067 }
68
69 // The stack-based DFS traversal; defined below.
70 void DFSVisitChildren() {
71 assert(!VisitStack.empty());
Chris Lattnera74a63c2003-08-31 01:48:21 +000072 while (VisitStack.back().second != GT::child_end(VisitStack.back().first))
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000073 { // TOS has at least one more child so continue DFS
Chris Lattnera74a63c2003-08-31 01:48:21 +000074 NodeType *childN = *VisitStack.back().second++;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000075 if (nodeVisitNumbers.find(childN) == nodeVisitNumbers.end())
76 { // this node has never been seen
77 DFSVisitOne(childN);
78 }
79 else
80 {
Chris Lattnera74a63c2003-08-31 01:48:21 +000081 unsigned childNum = nodeVisitNumbers[childN];
82 if (MinVisitNumStack.back() > childNum)
83 MinVisitNumStack.back() = childNum;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +000084 }
85 }
86 }
87
88 // Compute the next SCC using the DFS traversal.
89 void GetNextSCC() {
90 assert(VisitStack.size() == MinVisitNumStack.size());
91 CurrentSCC.clear(); // Prepare to compute the next SCC
92 while (! VisitStack.empty())
93 {
94 DFSVisitChildren();
95
Chris Lattnera74a63c2003-08-31 01:48:21 +000096 assert(VisitStack.back().second ==
97 GT::child_end(VisitStack.back().first));
98 NodeType* visitingN = VisitStack.back().first;
99 unsigned minVisitNum = MinVisitNumStack.back();
100 VisitStack.pop_back();
101 MinVisitNumStack.pop_back();
102 if (! MinVisitNumStack.empty() && MinVisitNumStack.back() > minVisitNum)
103 MinVisitNumStack.back() = minVisitNum;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000104
Chris Lattnerb1c0cd02003-08-31 01:45:00 +0000105 //DEBUG(std::cerr << "TarjanSCC: Popped node " << visitingN <<
106 // " : minVisitNum = " << minVisitNum << "; Node visit num = " <<
107 // nodeVisitNumbers[visitingN] << "\n");
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000108
109 if (minVisitNum == nodeVisitNumbers[visitingN])
110 { // A full SCC is on the SCCNodeStack! It includes all nodes below
111 // visitingN on the stack. Copy those nodes to CurrentSCC,
112 // reset their minVisit values, and return (this suspends
113 // the DFS traversal till the next ++).
114 do {
Chris Lattnera74a63c2003-08-31 01:48:21 +0000115 CurrentSCC.push_back(SCCNodeStack.back());
116 SCCNodeStack.pop_back();
Chris Lattnerfb185592002-11-15 18:04:16 +0000117 nodeVisitNumbers[CurrentSCC.back()] = ~0UL;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000118 } while (CurrentSCC.back() != visitingN);
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000119 return;
120 }
121 }
122 }
123
Chris Lattner78a5c1a2003-08-31 20:01:57 +0000124 inline scc_iterator(NodeType *entryN) : visitNum(0) {
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000125 DFSVisitOne(entryN);
126 GetNextSCC();
127 }
Chris Lattner78a5c1a2003-08-31 20:01:57 +0000128 inline scc_iterator() { /* End is when DFS stack is empty */ }
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000129
130public:
Chris Lattner78a5c1a2003-08-31 20:01:57 +0000131 typedef scc_iterator<GraphT, GT> _Self;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000132
133 // Provide static "constructors"...
134 static inline _Self begin(GraphT& G) { return _Self(GT::getEntryNode(G)); }
135 static inline _Self end (GraphT& G) { return _Self(); }
136
137 // Direct loop termination test (I.fini() is more efficient than I == end())
138 inline bool fini() const {
Vikram S. Adve80cac472002-12-06 15:02:22 +0000139 assert(!CurrentSCC.empty() || VisitStack.empty());
140 return CurrentSCC.empty();
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000141 }
142
143 inline bool operator==(const _Self& x) const {
Vikram S. Adve80cac472002-12-06 15:02:22 +0000144 return VisitStack == x.VisitStack && CurrentSCC == x.CurrentSCC;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000145 }
146 inline bool operator!=(const _Self& x) const { return !operator==(x); }
147
148 // Iterator traversal: forward iteration only
149 inline _Self& operator++() { // Preincrement
150 GetNextSCC();
151 return *this;
152 }
153 inline _Self operator++(int) { // Postincrement
154 _Self tmp = *this; ++*this; return tmp;
155 }
156
Chris Lattnerf0209062003-08-31 19:34:27 +0000157 // Retrieve a reference to the current SCC
158 inline const SccTy &operator*() const {
159 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
160 return CurrentSCC;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000161 }
Chris Lattnerf0209062003-08-31 19:34:27 +0000162 inline SccTy &operator*() {
163 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
164 return CurrentSCC;
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000165 }
Chris Lattner5cac4dd2003-08-31 19:51:22 +0000166
167 // hasLoop() -- Test if the current SCC has a loop. If it has more than one
168 // node, this is trivially true. If not, it may still contain a loop if the
169 // node has an edge back to itself.
170 bool hasLoop() const {
171 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
172 if (CurrentSCC.size() > 1) return true;
173 NodeType *N = CurrentSCC.front();
174 for (ChildItTy CI = GT::child_begin(N), CE=GT::child_end(N); CI != CE; ++CI)
175 if (*CI == N)
176 return true;
177 return false;
178 }
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000179};
180
181
Chris Lattner78a5c1a2003-08-31 20:01:57 +0000182// Global constructor for the SCC iterator.
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000183template <class T>
Chris Lattner78a5c1a2003-08-31 20:01:57 +0000184scc_iterator<T> scc_begin(T G) {
185 return scc_iterator<T>::begin(G);
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000186}
187
Chris Lattnerc0a07a42002-11-10 23:46:31 +0000188template <class T>
Chris Lattner78a5c1a2003-08-31 20:01:57 +0000189scc_iterator<T> scc_end(T G) {
190 return scc_iterator<T>::end(G);
Chris Lattnerc0a07a42002-11-10 23:46:31 +0000191}
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000192
Vikram S. Advea9c3afb2002-11-04 14:15:57 +0000193#endif