1 //===-- Support/SCCIterator.h - Strongly Connected Comp. Iter. --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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 LLVM_ADT_SCCITERATOR_H
22 #define LLVM_ADT_SCCITERATOR_H
24 #include "llvm/ADT/GraphTraits.h"
30 //===----------------------------------------------------------------------===//
32 /// scc_iterator - Enumerate the SCCs of a directed graph, in
33 /// reverse topological order of the SCC DAG.
35 template<class GraphT, class GT = GraphTraits<GraphT> >
37 : public std::iterator<std::forward_iterator_tag, std::vector<typename GT::NodeType>, ptrdiff_t> {
38 typedef typename GT::NodeType NodeType;
39 typedef typename GT::ChildIteratorType ChildItTy;
40 typedef std::vector<NodeType*> SccTy;
41 typedef std::iterator<std::forward_iterator_tag, std::vector<typename GT::NodeType>, ptrdiff_t> super;
42 typedef typename super::reference reference;
43 typedef typename super::pointer pointer;
45 // The visit counters used to detect when a complete SCC is on the stack.
46 // visitNum is the global counter.
47 // nodeVisitNumbers are per-node visit numbers, also used as DFS flags.
49 std::map<NodeType *, unsigned> nodeVisitNumbers;
51 // SCCNodeStack - Stack holding nodes of the SCC.
52 std::vector<NodeType *> SCCNodeStack;
54 // CurrentSCC - The current SCC, retrieved using operator*().
57 // VisitStack - Used to maintain the ordering. Top = current block
58 // First element is basic block pointer, second is the 'next child' to visit
59 std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
61 // MinVistNumStack - Stack holding the "min" values for each node in the DFS.
62 // This is used to track the minimum uplink values for all children of
63 // the corresponding node on the VisitStack.
64 std::vector<unsigned> MinVisitNumStack;
66 // A single "visit" within the non-recursive DFS traversal.
67 void DFSVisitOne(NodeType* N) {
68 ++visitNum; // Global counter for the visit order
69 nodeVisitNumbers[N] = visitNum;
70 SCCNodeStack.push_back(N);
71 MinVisitNumStack.push_back(visitNum);
72 VisitStack.push_back(std::make_pair(N, GT::child_begin(N)));
73 //errs() << "TarjanSCC: Node " << N <<
74 // " : visitNum = " << visitNum << "\n";
77 // The stack-based DFS traversal; defined below.
78 void DFSVisitChildren() {
79 assert(!VisitStack.empty());
80 while (VisitStack.back().second != GT::child_end(VisitStack.back().first)) {
81 // TOS has at least one more child so continue DFS
82 NodeType *childN = *VisitStack.back().second++;
83 if (!nodeVisitNumbers.count(childN)) {
84 // this node has never been seen
87 unsigned childNum = nodeVisitNumbers[childN];
88 if (MinVisitNumStack.back() > childNum)
89 MinVisitNumStack.back() = childNum;
94 // Compute the next SCC using the DFS traversal.
96 assert(VisitStack.size() == MinVisitNumStack.size());
97 CurrentSCC.clear(); // Prepare to compute the next SCC
98 while (!VisitStack.empty()) {
100 assert(VisitStack.back().second ==GT::child_end(VisitStack.back().first));
101 NodeType* visitingN = VisitStack.back().first;
102 unsigned minVisitNum = MinVisitNumStack.back();
103 VisitStack.pop_back();
104 MinVisitNumStack.pop_back();
105 if (!MinVisitNumStack.empty() && MinVisitNumStack.back() > minVisitNum)
106 MinVisitNumStack.back() = minVisitNum;
108 //errs() << "TarjanSCC: Popped node " << visitingN <<
109 // " : minVisitNum = " << minVisitNum << "; Node visit num = " <<
110 // nodeVisitNumbers[visitingN] << "\n";
112 if (minVisitNum == nodeVisitNumbers[visitingN]) {
113 // A full SCC is on the SCCNodeStack! It includes all nodes below
114 // visitingN on the stack. Copy those nodes to CurrentSCC,
115 // reset their minVisit values, and return (this suspends
116 // the DFS traversal till the next ++).
118 CurrentSCC.push_back(SCCNodeStack.back());
119 SCCNodeStack.pop_back();
120 nodeVisitNumbers[CurrentSCC.back()] = ~0U;
121 } while (CurrentSCC.back() != visitingN);
127 inline scc_iterator(NodeType *entryN) : visitNum(0) {
131 inline scc_iterator() { /* End is when DFS stack is empty */ }
134 typedef scc_iterator<GraphT, GT> _Self;
136 // Provide static "constructors"...
137 static inline _Self begin(GraphT& G) { return _Self(GT::getEntryNode(G)); }
138 static inline _Self end (GraphT& G) { return _Self(); }
140 // Direct loop termination test (I.fini() is more efficient than I == end())
141 inline bool fini() const {
142 assert(!CurrentSCC.empty() || VisitStack.empty());
143 return CurrentSCC.empty();
146 inline bool operator==(const _Self& x) const {
147 return VisitStack == x.VisitStack && CurrentSCC == x.CurrentSCC;
149 inline bool operator!=(const _Self& x) const { return !operator==(x); }
151 // Iterator traversal: forward iteration only
152 inline _Self& operator++() { // Preincrement
156 inline _Self operator++(int) { // Postincrement
157 _Self tmp = *this; ++*this; return tmp;
160 // Retrieve a reference to the current SCC
161 inline const SccTy &operator*() const {
162 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
165 inline SccTy &operator*() {
166 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
170 // hasLoop() -- Test if the current SCC has a loop. If it has more than one
171 // node, this is trivially true. If not, it may still contain a loop if the
172 // node has an edge back to itself.
173 bool hasLoop() const {
174 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
175 if (CurrentSCC.size() > 1) return true;
176 NodeType *N = CurrentSCC.front();
177 for (ChildItTy CI = GT::child_begin(N), CE=GT::child_end(N); CI != CE; ++CI)
185 // Global constructor for the SCC iterator.
187 scc_iterator<T> scc_begin(T G) {
188 return scc_iterator<T>::begin(G);
192 scc_iterator<T> scc_end(T G) {
193 return scc_iterator<T>::end(G);
196 } // End llvm namespace