2004-09-02 00:55:40 +02:00
|
|
|
//===-- Support/SCCIterator.h - Strongly Connected Comp. Iter. --*- C++ -*-===//
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 21:46:57 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 21:46:57 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
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.
|
2005-04-21 22:19:05 +02: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"
|
2009-09-15 06:37:49 +02:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2009-02-20 23:20:18 +01:00
|
|
|
#include <vector>
|
2002-11-04 15:15:57 +01:00
|
|
|
|
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
|
2009-08-28 01:44:33 +02:00
|
|
|
: public std::iterator<std::forward_iterator_tag,
|
|
|
|
std::vector<typename GT::NodeType>, ptrdiff_t> {
|
2003-08-31 21:55:31 +02:00
|
|
|
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;
|
2009-08-28 01:44:33 +02:00
|
|
|
typedef std::iterator<std::forward_iterator_tag,
|
|
|
|
std::vector<typename GT::NodeType>, ptrdiff_t> super;
|
2002-11-04 15:15:57 +01: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 03:48:21 +02:00
|
|
|
unsigned visitNum;
|
2009-09-15 06:37:49 +02:00
|
|
|
DenseMap<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.
|
2010-04-17 09:17:19 +02:00
|
|
|
void DFSVisitOne(NodeType *N) {
|
2002-11-04 15:15:57 +01:00
|
|
|
++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)));
|
2009-12-23 18:18:22 +01:00
|
|
|
//dbgs() << "TarjanSCC: Node " << N <<
|
2006-11-29 00:31:42 +01:00
|
|
|
// " : 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)) {
|
2010-04-17 09:17:19 +02:00
|
|
|
// this node has never been seen.
|
2003-11-13 03:01:41 +01:00
|
|
|
DFSVisitOne(childN);
|
2010-04-16 23:57:10 +02:00
|
|
|
continue;
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
2010-04-16 23:57:10 +02:00
|
|
|
|
|
|
|
unsigned childNum = nodeVisitNumbers[childN];
|
|
|
|
if (MinVisitNumStack.back() > childNum)
|
|
|
|
MinVisitNumStack.back() = childNum;
|
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));
|
2010-04-16 23:57:10 +02:00
|
|
|
NodeType *visitingN = VisitStack.back().first;
|
2003-11-13 03:01:41 +01:00
|
|
|
unsigned minVisitNum = MinVisitNumStack.back();
|
|
|
|
VisitStack.pop_back();
|
|
|
|
MinVisitNumStack.pop_back();
|
|
|
|
if (!MinVisitNumStack.empty() && MinVisitNumStack.back() > minVisitNum)
|
|
|
|
MinVisitNumStack.back() = minVisitNum;
|
|
|
|
|
2009-12-23 18:18:22 +01:00
|
|
|
//dbgs() << "TarjanSCC: Popped node " << visitingN <<
|
2003-11-13 03:01:41 +01:00
|
|
|
// " : minVisitNum = " << minVisitNum << "; Node visit num = " <<
|
2006-11-29 00:31:42 +01:00
|
|
|
// nodeVisitNumbers[visitingN] << "\n";
|
2003-11-13 03:01:41 +01:00
|
|
|
|
2010-04-16 23:57:10 +02: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 03:01:41 +01:00
|
|
|
}
|
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"...
|
2010-04-17 00:59:24 +02: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 15:15:57 +01:00
|
|
|
|
2010-04-17 00:59:24 +02:00
|
|
|
// Direct loop termination test: I.isAtEnd() is more efficient than I == end()
|
|
|
|
inline bool isAtEnd() 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
|
|
|
}
|
|
|
|
|
2005-04-21 22:19:05 +02: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();
|
2005-04-21 22:19:05 +02:00
|
|
|
return *this;
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
|
|
|
inline _Self operator++(int) { // Postincrement
|
2005-04-21 22:19:05 +02:00
|
|
|
_Self tmp = *this; ++*this; return tmp;
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
|
|
|
|
2003-08-31 21:34:27 +02:00
|
|
|
// Retrieve a reference to the current SCC
|
2005-04-21 22:19:05 +02:00
|
|
|
inline const SccTy &operator*() const {
|
2003-08-31 21:34:27 +02:00
|
|
|
assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
|
|
|
|
return CurrentSCC;
|
2002-11-04 15:15:57 +01:00
|
|
|
}
|
2005-04-21 22:19:05 +02:00
|
|
|
inline SccTy &operator*() {
|
2003-08-31 21:34:27 +02:00
|
|
|
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;
|
|
|
|
}
|
2010-04-17 01:04:30 +02: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 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>
|
2010-04-16 23:57:10 +02:00
|
|
|
scc_iterator<T> scc_begin(const T &G) {
|
2003-08-31 22:01:57 +02:00
|
|
|
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>
|
2010-04-16 23:57:10 +02:00
|
|
|
scc_iterator<T> scc_end(const T &G) {
|
2003-08-31 22:01:57 +02:00
|
|
|
return scc_iterator<T>::end(G);
|
2002-11-11 00:46:31 +01:00
|
|
|
}
|
2002-11-04 15:15:57 +01:00
|
|
|
|
2009-11-17 11:54:25 +01:00
|
|
|
template <class T>
|
2010-04-16 23:57:10 +02:00
|
|
|
scc_iterator<Inverse<T> > scc_begin(const Inverse<T> &G) {
|
|
|
|
return scc_iterator<Inverse<T> >::begin(G);
|
2009-11-17 11:54:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2010-04-16 23:57:10 +02:00
|
|
|
scc_iterator<Inverse<T> > scc_end(const Inverse<T> &G) {
|
|
|
|
return scc_iterator<Inverse<T> >::end(G);
|
2009-11-17 11:54:25 +01:00
|
|
|
}
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2002-11-04 15:15:57 +01:00
|
|
|
#endif
|