2004-09-02 00:55:40 +02:00
|
|
|
//===-- Support/SCCIterator.h - Strongly Connected Comp. Iter. --*- C++ -*-===//
|
2003-10-20 21:46:57 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-04 15:15:57 +01:00
|
|
|
//
|
2004-09-02 00:55:40 +02:00
|
|
|
// This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected
|
2003-08-31 22:01:57 +02:00
|
|
|
// components (SCCs) of a graph in O(N+E) time using Tarjan's DFS algorithm.
|
2002-11-04 15:15:57 +01:00
|
|
|
//
|
2003-08-31 22:01:57 +02:00
|
|
|
// The SCC iterator has the important property that if a node in SCC S1 has an
|
|
|
|
// edge to a node in SCC S2, then it visits S1 *after* S2.
|
2002-11-04 15:15:57 +01:00
|
|
|
//
|
2003-08-31 22:01:57 +02:00
|
|
|
// To visit S1 *before* S2, use the scc_iterator on the Inverse graph.
|
2002-11-04 15:15:57 +01:00
|
|
|
// (NOTE: This requires some simple wrappers and is not supported yet.)
|
2003-06-22 05:08:05 +02:00
|
|
|
//
|
2002-11-04 15:15:57 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-02 00:55:40 +02:00
|
|
|
#ifndef LLVM_ADT_SCCITERATOR_H
|
|
|
|
#define LLVM_ADT_SCCITERATOR_H
|
2002-11-04 15:15:57 +01:00
|
|
|
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
|
|
|
#include "llvm/ADT/iterator"
|
2002-11-04 15:15:57 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2003-08-31 21:55:31 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
2003-08-31 22:01:57 +02:00
|
|
|
/// scc_iterator - Enumerate the SCCs of a directed graph, in
|
2003-08-31 21:55:31 +02:00
|
|
|
/// reverse topological order of the SCC DAG.
|
|
|
|
///
|
2002-11-04 15:15:57 +01:00
|
|
|
template<class GraphT, class GT = GraphTraits<GraphT> >
|
2003-08-31 22:01:57 +02:00
|
|
|
class scc_iterator
|
2003-08-31 21:55:31 +02:00
|
|
|
: public forward_iterator<std::vector<typename GT::NodeType>, ptrdiff_t> {
|
|
|
|
typedef typename GT::NodeType NodeType;
|
2002-11-04 15:15:57 +01:00
|
|
|
typedef typename GT::ChildIteratorType ChildItTy;
|
2003-08-31 21:55:31 +02:00
|
|
|
typedef std::vector<NodeType*> SccTy;
|
2002-11-04 15:15:57 +01:00
|
|
|
typedef forward_iterator<SccTy, ptrdiff_t> super;
|
|
|
|
typedef typename super::reference reference;
|
|
|
|
typedef typename super::pointer pointer;
|
|
|
|
|
|
|
|
// The visit counters used to detect when a complete SCC is on the stack.
|
|
|
|
// visitNum is the global counter.
|
|
|
|
// nodeVisitNumbers are per-node visit numbers, also used as DFS flags.
|
2003-08-31 03:48:21 +02:00
|
|
|
unsigned visitNum;
|
|
|
|
std::map<NodeType *, unsigned> nodeVisitNumbers;
|
2002-11-04 15:15:57 +01:00
|
|
|
|
|
|
|
// SCCNodeStack - Stack holding nodes of the SCC.
|
2003-08-31 03:48:21 +02:00
|
|
|
std::vector<NodeType *> SCCNodeStack;
|
2002-11-04 15:15:57 +01:00
|
|
|
|
|
|
|
// CurrentSCC - The current SCC, retrieved using operator*().
|
|
|
|
SccTy CurrentSCC;
|
|
|
|
|
|
|
|
// VisitStack - Used to maintain the ordering. Top = current block
|
|
|
|
// First element is basic block pointer, second is the 'next child' to visit
|
2003-08-31 03:48:21 +02:00
|
|
|
std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
|
2002-11-04 15:15:57 +01:00
|
|
|
|
|
|
|
// MinVistNumStack - Stack holding the "min" values for each node in the DFS.
|
|
|
|
// This is used to track the minimum uplink values for all children of
|
|
|
|
// the corresponding node on the VisitStack.
|
2003-08-31 03:48:21 +02:00
|
|
|
std::vector<unsigned> MinVisitNumStack;
|
2002-11-04 15:15:57 +01:00
|
|
|
|
|
|
|
// A single "visit" within the non-recursive DFS traversal.
|
|
|
|
void DFSVisitOne(NodeType* N) {
|
|
|
|
++visitNum; // Global counter for the visit order
|
|
|
|
nodeVisitNumbers[N] = visitNum;
|
2003-08-31 03:48:21 +02:00
|
|
|
SCCNodeStack.push_back(N);
|
|
|
|
MinVisitNumStack.push_back(visitNum);
|
2003-11-13 03:30:22 +01:00
|
|
|
VisitStack.push_back(std::make_pair(N, GT::child_begin(N)));
|
2003-08-31 03:45:00 +02:00
|
|
|
//DEBUG(std::cerr << "TarjanSCC: Node " << N <<
|
|
|
|
// " : visitNum = " << visitNum << "\n");
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The stack-based DFS traversal; defined below.
|
|
|
|
void DFSVisitChildren() {
|
|
|
|
assert(!VisitStack.empty());
|
2003-11-13 03:01:41 +01:00
|
|
|
while (VisitStack.back().second != GT::child_end(VisitStack.back().first)) {
|
|
|
|
// TOS has at least one more child so continue DFS
|
|
|
|
NodeType *childN = *VisitStack.back().second++;
|
|
|
|
if (!nodeVisitNumbers.count(childN)) {
|
|
|
|
// this node has never been seen
|
|
|
|
DFSVisitOne(childN);
|
|
|
|
} else {
|
|
|
|
unsigned childNum = nodeVisitNumbers[childN];
|
|
|
|
if (MinVisitNumStack.back() > childNum)
|
|
|
|
MinVisitNumStack.back() = childNum;
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
2003-11-13 03:01:41 +01:00
|
|
|
}
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the next SCC using the DFS traversal.
|
|
|
|
void GetNextSCC() {
|
|
|
|
assert(VisitStack.size() == MinVisitNumStack.size());
|
|
|
|
CurrentSCC.clear(); // Prepare to compute the next SCC
|
2003-11-13 03:01:41 +01:00
|
|
|
while (!VisitStack.empty()) {
|
|
|
|
DFSVisitChildren();
|
|
|
|
assert(VisitStack.back().second ==GT::child_end(VisitStack.back().first));
|
|
|
|
NodeType* visitingN = VisitStack.back().first;
|
|
|
|
unsigned minVisitNum = MinVisitNumStack.back();
|
|
|
|
VisitStack.pop_back();
|
|
|
|
MinVisitNumStack.pop_back();
|
|
|
|
if (!MinVisitNumStack.empty() && MinVisitNumStack.back() > minVisitNum)
|
|
|
|
MinVisitNumStack.back() = minVisitNum;
|
|
|
|
|
|
|
|
//DEBUG(std::cerr << "TarjanSCC: Popped node " << visitingN <<
|
|
|
|
// " : minVisitNum = " << minVisitNum << "; Node visit num = " <<
|
|
|
|
// nodeVisitNumbers[visitingN] << "\n");
|
|
|
|
|
|
|
|
if (minVisitNum == nodeVisitNumbers[visitingN]) {
|
|
|
|
// A full SCC is on the SCCNodeStack! It includes all nodes below
|
|
|
|
// visitingN on the stack. Copy those nodes to CurrentSCC,
|
|
|
|
// reset their minVisit values, and return (this suspends
|
|
|
|
// the DFS traversal till the next ++).
|
|
|
|
do {
|
|
|
|
CurrentSCC.push_back(SCCNodeStack.back());
|
|
|
|
SCCNodeStack.pop_back();
|
|
|
|
nodeVisitNumbers[CurrentSCC.back()] = ~0UL;
|
|
|
|
} while (CurrentSCC.back() != visitingN);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
|
|
|
|
2003-08-31 22:01:57 +02:00
|
|
|
inline scc_iterator(NodeType *entryN) : visitNum(0) {
|
2002-11-04 15:15:57 +01:00
|
|
|
DFSVisitOne(entryN);
|
|
|
|
GetNextSCC();
|
|
|
|
}
|
2003-08-31 22:01:57 +02:00
|
|
|
inline scc_iterator() { /* End is when DFS stack is empty */ }
|
2002-11-04 15:15:57 +01:00
|
|
|
|
|
|
|
public:
|
2003-08-31 22:01:57 +02:00
|
|
|
typedef scc_iterator<GraphT, GT> _Self;
|
2002-11-04 15:15:57 +01:00
|
|
|
|
|
|
|
// Provide static "constructors"...
|
|
|
|
static inline _Self begin(GraphT& G) { return _Self(GT::getEntryNode(G)); }
|
|
|
|
static inline _Self end (GraphT& G) { return _Self(); }
|
|
|
|
|
|
|
|
// Direct loop termination test (I.fini() is more efficient than I == end())
|
|
|
|
inline bool fini() const {
|
2002-12-06 16:02:22 +01:00
|
|
|
assert(!CurrentSCC.empty() || VisitStack.empty());
|
|
|
|
return CurrentSCC.empty();
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const _Self& x) const {
|
2002-12-06 16:02:22 +01:00
|
|
|
return VisitStack == x.VisitStack && CurrentSCC == x.CurrentSCC;
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
|
|
|
inline bool operator!=(const _Self& x) const { return !operator==(x); }
|
|
|
|
|
|
|
|
// Iterator traversal: forward iteration only
|
|
|
|
inline _Self& operator++() { // Preincrement
|
|
|
|
GetNextSCC();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline _Self operator++(int) { // Postincrement
|
|
|
|
_Self tmp = *this; ++*this; return tmp;
|
|
|
|
}
|
|
|
|
|
2003-08-31 21:34:27 +02:00
|
|
|
// Retrieve a reference to the current SCC
|
|
|
|
inline const SccTy &operator*() const {
|
|
|
|
assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
|
|
|
|
return CurrentSCC;
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
2003-08-31 21:34:27 +02:00
|
|
|
inline SccTy &operator*() {
|
|
|
|
assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
|
|
|
|
return CurrentSCC;
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
2003-08-31 21:51:22 +02:00
|
|
|
|
|
|
|
// hasLoop() -- Test if the current SCC has a loop. If it has more than one
|
|
|
|
// node, this is trivially true. If not, it may still contain a loop if the
|
|
|
|
// node has an edge back to itself.
|
|
|
|
bool hasLoop() const {
|
|
|
|
assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
|
|
|
|
if (CurrentSCC.size() > 1) return true;
|
|
|
|
NodeType *N = CurrentSCC.front();
|
|
|
|
for (ChildItTy CI = GT::child_begin(N), CE=GT::child_end(N); CI != CE; ++CI)
|
|
|
|
if (*CI == N)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2002-11-04 15:15:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-08-31 22:01:57 +02:00
|
|
|
// Global constructor for the SCC iterator.
|
2002-11-04 15:15:57 +01:00
|
|
|
template <class T>
|
2003-08-31 22:01:57 +02:00
|
|
|
scc_iterator<T> scc_begin(T G) {
|
|
|
|
return scc_iterator<T>::begin(G);
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
|
|
|
|
2002-11-11 00:46:31 +01:00
|
|
|
template <class T>
|
2003-08-31 22:01:57 +02:00
|
|
|
scc_iterator<T> scc_end(T G) {
|
|
|
|
return scc_iterator<T>::end(G);
|
2002-11-11 00:46:31 +01:00
|
|
|
}
|
2002-11-04 15:15:57 +01:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2002-11-04 15:15:57 +01:00
|
|
|
#endif
|