2004-09-01 22:55:40 +00:00
|
|
|
//===-- Support/SCCIterator.h - Strongly Connected Comp. Iter. --*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 19:46:57 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 19:46:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-04 14:15:57 +00:00
|
|
|
//
|
2004-09-01 22:55:40 +00:00
|
|
|
// This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected
|
2003-08-31 20:01:57 +00:00
|
|
|
// components (SCCs) of a graph in O(N+E) time using Tarjan's DFS algorithm.
|
2002-11-04 14:15:57 +00:00
|
|
|
//
|
2003-08-31 20:01:57 +00: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.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-08-31 20:01:57 +00:00
|
|
|
// To visit S1 *before* S2, use the scc_iterator on the Inverse graph.
|
2002-11-04 14:15:57 +00:00
|
|
|
// (NOTE: This requires some simple wrappers and is not supported yet.)
|
2003-06-22 03:08:05 +00:00
|
|
|
//
|
2002-11-04 14:15:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#ifndef LLVM_ADT_SCCITERATOR_H
|
|
|
|
#define LLVM_ADT_SCCITERATOR_H
|
2002-11-04 14:15:57 +00:00
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
2009-09-15 04:37:49 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2009-02-20 22:20:18 +00:00
|
|
|
#include <vector>
|
2002-11-04 14:15:57 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2003-08-31 19:55:31 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
2003-08-31 20:01:57 +00:00
|
|
|
/// scc_iterator - Enumerate the SCCs of a directed graph, in
|
2003-08-31 19:55:31 +00:00
|
|
|
/// reverse topological order of the SCC DAG.
|
|
|
|
///
|
2002-11-04 14:15:57 +00:00
|
|
|
template<class GraphT, class GT = GraphTraits<GraphT> >
|
2003-08-31 20:01:57 +00:00
|
|
|
class scc_iterator
|
2009-08-27 23:44:33 +00:00
|
|
|
: public std::iterator<std::forward_iterator_tag,
|
|
|
|
std::vector<typename GT::NodeType>, ptrdiff_t> {
|
2003-08-31 19:55:31 +00:00
|
|
|
typedef typename GT::NodeType NodeType;
|
2002-11-04 14:15:57 +00:00
|
|
|
typedef typename GT::ChildIteratorType ChildItTy;
|
2003-08-31 19:55:31 +00:00
|
|
|
typedef std::vector<NodeType*> SccTy;
|
2009-08-27 23:44:33 +00:00
|
|
|
typedef std::iterator<std::forward_iterator_tag,
|
|
|
|
std::vector<typename GT::NodeType>, ptrdiff_t> super;
|
2002-11-04 14:15:57 +00:00
|
|
|
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 01:48:21 +00:00
|
|
|
unsigned visitNum;
|
2009-09-15 04:37:49 +00:00
|
|
|
DenseMap<NodeType *, unsigned> nodeVisitNumbers;
|
2002-11-04 14:15:57 +00:00
|
|
|
|
|
|
|
// SCCNodeStack - Stack holding nodes of the SCC.
|
2003-08-31 01:48:21 +00:00
|
|
|
std::vector<NodeType *> SCCNodeStack;
|
2002-11-04 14:15:57 +00: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 01:48:21 +00:00
|
|
|
std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
|
2002-11-04 14:15:57 +00: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 01:48:21 +00:00
|
|
|
std::vector<unsigned> MinVisitNumStack;
|
2002-11-04 14:15:57 +00:00
|
|
|
|
|
|
|
// A single "visit" within the non-recursive DFS traversal.
|
2010-04-17 07:17:19 +00:00
|
|
|
void DFSVisitOne(NodeType *N) {
|
2002-11-04 14:15:57 +00:00
|
|
|
++visitNum; // Global counter for the visit order
|
|
|
|
nodeVisitNumbers[N] = visitNum;
|
2003-08-31 01:48:21 +00:00
|
|
|
SCCNodeStack.push_back(N);
|
|
|
|
MinVisitNumStack.push_back(visitNum);
|
2003-11-13 02:30:22 +00:00
|
|
|
VisitStack.push_back(std::make_pair(N, GT::child_begin(N)));
|
2009-12-23 17:18:22 +00:00
|
|
|
//dbgs() << "TarjanSCC: Node " << N <<
|
2006-11-28 23:31:42 +00:00
|
|
|
// " : visitNum = " << visitNum << "\n";
|
2002-11-04 14:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The stack-based DFS traversal; defined below.
|
|
|
|
void DFSVisitChildren() {
|
|
|
|
assert(!VisitStack.empty());
|
2003-11-13 02:01:41 +00: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)) {
|
2010-04-17 07:17:19 +00:00
|
|
|
// this node has never been seen.
|
2003-11-13 02:01:41 +00:00
|
|
|
DFSVisitOne(childN);
|
2010-04-16 21:57:10 +00:00
|
|
|
continue;
|
2002-11-04 14:15:57 +00:00
|
|
|
}
|
2010-04-16 21:57:10 +00:00
|
|
|
|
|
|
|
unsigned childNum = nodeVisitNumbers[childN];
|
|
|
|
if (MinVisitNumStack.back() > childNum)
|
|
|
|
MinVisitNumStack.back() = childNum;
|
2003-11-13 02:01:41 +00:00
|
|
|
}
|
2002-11-04 14:15:57 +00: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 02:01:41 +00:00
|
|
|
while (!VisitStack.empty()) {
|
|
|
|
DFSVisitChildren();
|
|
|
|
assert(VisitStack.back().second ==GT::child_end(VisitStack.back().first));
|
2010-04-16 21:57:10 +00:00
|
|
|
NodeType *visitingN = VisitStack.back().first;
|
2003-11-13 02:01:41 +00:00
|
|
|
unsigned minVisitNum = MinVisitNumStack.back();
|
|
|
|
VisitStack.pop_back();
|
|
|
|
MinVisitNumStack.pop_back();
|
|
|
|
if (!MinVisitNumStack.empty() && MinVisitNumStack.back() > minVisitNum)
|
|
|
|
MinVisitNumStack.back() = minVisitNum;
|
|
|
|
|
2009-12-23 17:18:22 +00:00
|
|
|
//dbgs() << "TarjanSCC: Popped node " << visitingN <<
|
2003-11-13 02:01:41 +00:00
|
|
|
// " : minVisitNum = " << minVisitNum << "; Node visit num = " <<
|
2006-11-28 23:31:42 +00:00
|
|
|
// nodeVisitNumbers[visitingN] << "\n";
|
2003-11-13 02:01:41 +00:00
|
|
|
|
2010-04-16 21:57:10 +00:00
|
|
|
if (minVisitNum != nodeVisitNumbers[visitingN])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// 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()] = ~0U;
|
|
|
|
} while (CurrentSCC.back() != visitingN);
|
|
|
|
return;
|
2003-11-13 02:01:41 +00:00
|
|
|
}
|
2002-11-04 14:15:57 +00:00
|
|
|
}
|
|
|
|
|
2003-08-31 20:01:57 +00:00
|
|
|
inline scc_iterator(NodeType *entryN) : visitNum(0) {
|
2002-11-04 14:15:57 +00:00
|
|
|
DFSVisitOne(entryN);
|
|
|
|
GetNextSCC();
|
|
|
|
}
|
2003-08-31 20:01:57 +00:00
|
|
|
inline scc_iterator() { /* End is when DFS stack is empty */ }
|
2002-11-04 14:15:57 +00:00
|
|
|
|
|
|
|
public:
|
2003-08-31 20:01:57 +00:00
|
|
|
typedef scc_iterator<GraphT, GT> _Self;
|
2002-11-04 14:15:57 +00:00
|
|
|
|
|
|
|
// Provide static "constructors"...
|
2010-04-16 22:59:24 +00:00
|
|
|
static inline _Self begin(const GraphT &G){return _Self(GT::getEntryNode(G));}
|
|
|
|
static inline _Self end (const GraphT &G) { return _Self(); }
|
2002-11-04 14:15:57 +00:00
|
|
|
|
2010-04-16 22:59:24 +00:00
|
|
|
// Direct loop termination test: I.isAtEnd() is more efficient than I == end()
|
|
|
|
inline bool isAtEnd() const {
|
2002-12-06 15:02:22 +00:00
|
|
|
assert(!CurrentSCC.empty() || VisitStack.empty());
|
|
|
|
return CurrentSCC.empty();
|
2002-11-04 14:15:57 +00:00
|
|
|
}
|
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
inline bool operator==(const _Self& x) const {
|
2002-12-06 15:02:22 +00:00
|
|
|
return VisitStack == x.VisitStack && CurrentSCC == x.CurrentSCC;
|
2002-11-04 14:15:57 +00:00
|
|
|
}
|
|
|
|
inline bool operator!=(const _Self& x) const { return !operator==(x); }
|
|
|
|
|
|
|
|
// Iterator traversal: forward iteration only
|
|
|
|
inline _Self& operator++() { // Preincrement
|
|
|
|
GetNextSCC();
|
2005-04-21 20:19:05 +00:00
|
|
|
return *this;
|
2002-11-04 14:15:57 +00:00
|
|
|
}
|
|
|
|
inline _Self operator++(int) { // Postincrement
|
2005-04-21 20:19:05 +00:00
|
|
|
_Self tmp = *this; ++*this; return tmp;
|
2002-11-04 14:15:57 +00:00
|
|
|
}
|
|
|
|
|
2003-08-31 19:34:27 +00:00
|
|
|
// Retrieve a reference to the current SCC
|
2005-04-21 20:19:05 +00:00
|
|
|
inline const SccTy &operator*() const {
|
2003-08-31 19:34:27 +00:00
|
|
|
assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
|
|
|
|
return CurrentSCC;
|
2002-11-04 14:15:57 +00:00
|
|
|
}
|
2005-04-21 20:19:05 +00:00
|
|
|
inline SccTy &operator*() {
|
2003-08-31 19:34:27 +00:00
|
|
|
assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
|
|
|
|
return CurrentSCC;
|
2002-11-04 14:15:57 +00:00
|
|
|
}
|
2003-08-31 19:51:22 +00: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;
|
|
|
|
}
|
2010-04-16 23:04:30 +00:00
|
|
|
|
|
|
|
/// ReplaceNode - This informs the scc_iterator that the specified Old node
|
|
|
|
/// has been deleted, and New is to be used in its place.
|
|
|
|
void ReplaceNode(NodeType *Old, NodeType *New) {
|
|
|
|
assert(nodeVisitNumbers.count(Old) && "Old not in scc_iterator?");
|
|
|
|
nodeVisitNumbers[New] = nodeVisitNumbers[Old];
|
|
|
|
nodeVisitNumbers.erase(Old);
|
|
|
|
}
|
2002-11-04 14:15:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-08-31 20:01:57 +00:00
|
|
|
// Global constructor for the SCC iterator.
|
2002-11-04 14:15:57 +00:00
|
|
|
template <class T>
|
2010-04-16 21:57:10 +00:00
|
|
|
scc_iterator<T> scc_begin(const T &G) {
|
2003-08-31 20:01:57 +00:00
|
|
|
return scc_iterator<T>::begin(G);
|
2002-11-04 14:15:57 +00:00
|
|
|
}
|
|
|
|
|
2002-11-10 23:46:31 +00:00
|
|
|
template <class T>
|
2010-04-16 21:57:10 +00:00
|
|
|
scc_iterator<T> scc_end(const T &G) {
|
2003-08-31 20:01:57 +00:00
|
|
|
return scc_iterator<T>::end(G);
|
2002-11-10 23:46:31 +00:00
|
|
|
}
|
2002-11-04 14:15:57 +00:00
|
|
|
|
2009-11-17 10:54:25 +00:00
|
|
|
template <class T>
|
2010-04-16 21:57:10 +00:00
|
|
|
scc_iterator<Inverse<T> > scc_begin(const Inverse<T> &G) {
|
|
|
|
return scc_iterator<Inverse<T> >::begin(G);
|
2009-11-17 10:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2010-04-16 21:57:10 +00:00
|
|
|
scc_iterator<Inverse<T> > scc_end(const Inverse<T> &G) {
|
|
|
|
return scc_iterator<Inverse<T> >::end(G);
|
2009-11-17 10:54:25 +00:00
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2002-11-04 14:15:57 +00:00
|
|
|
#endif
|