2004-09-02 00:55:40 +02:00
|
|
|
//===- llvm/ADT/DepthFirstIterator.h - Depth First iterator -----*- 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
|
|
|
//===----------------------------------------------------------------------===//
|
2001-09-29 00:59:14 +02:00
|
|
|
//
|
2004-09-02 00:55:40 +02:00
|
|
|
// This file builds on the ADT/GraphTraits.h file to build generic depth
|
2003-10-13 17:45:33 +02:00
|
|
|
// first graph iterator. This file exposes the following functions/types:
|
|
|
|
//
|
|
|
|
// df_begin/df_end/df_iterator
|
|
|
|
// * Normal depth-first iteration - visit a node and then all of its children.
|
|
|
|
//
|
|
|
|
// idf_begin/idf_end/idf_iterator
|
|
|
|
// * Depth-first iteration on the 'inverse' graph.
|
2001-09-29 00:59:14 +02:00
|
|
|
//
|
2003-10-13 18:34:26 +02:00
|
|
|
// df_ext_begin/df_ext_end/df_ext_iterator
|
|
|
|
// * Normal depth-first iteration - visit a node and then all of its children.
|
|
|
|
// This iterator stores the 'visited' set in an external set, which allows
|
|
|
|
// it to be more efficient, and allows external clients to use the set for
|
|
|
|
// other purposes.
|
|
|
|
//
|
|
|
|
// idf_ext_begin/idf_ext_end/idf_ext_iterator
|
|
|
|
// * Depth-first iteration on the 'inverse' graph.
|
|
|
|
// This iterator stores the 'visited' set in an external set, which allows
|
|
|
|
// it to be more efficient, and allows external clients to use the set for
|
|
|
|
// other purposes.
|
|
|
|
//
|
2001-09-29 00:59:14 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-02 00:55:40 +02:00
|
|
|
#ifndef LLVM_ADT_DEPTHFIRSTITERATOR_H
|
|
|
|
#define LLVM_ADT_DEPTHFIRSTITERATOR_H
|
2001-09-29 00:59:14 +02:00
|
|
|
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
2007-06-21 23:25:36 +02:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-07-23 08:30:28 +02:00
|
|
|
#include "llvm/ADT/PointerIntPair.h"
|
2001-09-29 00:59:14 +02:00
|
|
|
#include <set>
|
2009-02-20 23:20:18 +01:00
|
|
|
#include <vector>
|
2001-09-29 00:59:14 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2003-10-13 18:34:26 +02:00
|
|
|
// df_iterator_storage - A private class which is used to figure out where to
|
|
|
|
// store the visited set.
|
|
|
|
template<class SetType, bool External> // Non-external set
|
|
|
|
class df_iterator_storage {
|
|
|
|
public:
|
|
|
|
SetType Visited;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class SetType>
|
|
|
|
class df_iterator_storage<SetType, true> {
|
|
|
|
public:
|
|
|
|
df_iterator_storage(SetType &VSet) : Visited(VSet) {}
|
|
|
|
df_iterator_storage(const df_iterator_storage &S) : Visited(S.Visited) {}
|
|
|
|
SetType &Visited;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-09-29 00:59:14 +02:00
|
|
|
// Generic Depth First Iterator
|
2007-06-21 23:25:36 +02:00
|
|
|
template<class GraphT,
|
|
|
|
class SetType = llvm::SmallPtrSet<typename GraphTraits<GraphT>::NodeType*, 8>,
|
2003-10-13 18:34:26 +02:00
|
|
|
bool ExtStorage = false, class GT = GraphTraits<GraphT> >
|
2009-08-27 08:41:46 +02:00
|
|
|
class df_iterator : public std::iterator<std::forward_iterator_tag,
|
|
|
|
typename GT::NodeType, ptrdiff_t>,
|
2003-10-13 18:34:26 +02:00
|
|
|
public df_iterator_storage<SetType, ExtStorage> {
|
2009-08-27 08:41:46 +02:00
|
|
|
typedef std::iterator<std::forward_iterator_tag,
|
|
|
|
typename GT::NodeType, ptrdiff_t> super;
|
2002-07-25 00:08:36 +02:00
|
|
|
|
2001-09-29 00:59:14 +02:00
|
|
|
typedef typename GT::NodeType NodeType;
|
|
|
|
typedef typename GT::ChildIteratorType ChildItTy;
|
2009-07-23 08:30:28 +02:00
|
|
|
typedef PointerIntPair<NodeType*, 1> PointerIntTy;
|
2001-09-29 00:59:14 +02:00
|
|
|
|
|
|
|
// VisitStack - Used to maintain the ordering. Top = current block
|
|
|
|
// First element is node pointer, second is the 'next child' to visit
|
2009-07-23 08:30:28 +02:00
|
|
|
// if the int in PointerIntTy is 0, the 'next child' to visit is invalid
|
|
|
|
std::vector<std::pair<PointerIntTy, ChildItTy> > VisitStack;
|
2001-09-29 00:59:14 +02:00
|
|
|
private:
|
2003-10-13 17:45:33 +02:00
|
|
|
inline df_iterator(NodeType *Node) {
|
2003-10-13 18:34:26 +02:00
|
|
|
this->Visited.insert(Node);
|
2009-07-23 08:30:28 +02:00
|
|
|
VisitStack.push_back(std::make_pair(PointerIntTy(Node, 0),
|
|
|
|
GT::child_begin(Node)));
|
|
|
|
}
|
|
|
|
inline df_iterator() {
|
|
|
|
// End is when stack is empty
|
2001-09-29 00:59:14 +02:00
|
|
|
}
|
2003-10-13 18:34:26 +02:00
|
|
|
inline df_iterator(NodeType *Node, SetType &S)
|
|
|
|
: df_iterator_storage<SetType, ExtStorage>(S) {
|
|
|
|
if (!S.count(Node)) {
|
2009-07-23 08:30:28 +02:00
|
|
|
VisitStack.push_back(std::make_pair(PointerIntTy(Node, 0),
|
|
|
|
GT::child_begin(Node)));
|
2003-10-13 18:34:26 +02:00
|
|
|
this->Visited.insert(Node);
|
|
|
|
}
|
|
|
|
}
|
2005-04-21 22:19:05 +02:00
|
|
|
inline df_iterator(SetType &S)
|
2003-10-13 18:34:26 +02:00
|
|
|
: df_iterator_storage<SetType, ExtStorage>(S) {
|
|
|
|
// End is when stack is empty
|
|
|
|
}
|
|
|
|
|
2009-07-23 08:30:28 +02:00
|
|
|
inline void toNext() {
|
|
|
|
do {
|
|
|
|
std::pair<PointerIntTy, ChildItTy> &Top = VisitStack.back();
|
|
|
|
NodeType *Node = Top.first.getPointer();
|
|
|
|
ChildItTy &It = Top.second;
|
|
|
|
if (!Top.first.getInt()) {
|
|
|
|
// now retrieve the real begin of the children before we dive in
|
|
|
|
It = GT::child_begin(Node);
|
|
|
|
Top.first.setInt(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (It != GT::child_end(Node)) {
|
|
|
|
NodeType *Next = *It++;
|
|
|
|
// Has our next sibling been visited?
|
|
|
|
if (Next && !this->Visited.count(Next)) {
|
|
|
|
// No, do it now.
|
|
|
|
this->Visited.insert(Next);
|
|
|
|
VisitStack.push_back(std::make_pair(PointerIntTy(Next, 0),
|
|
|
|
GT::child_begin(Next)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Oops, ran out of successors... go up a level on the stack.
|
|
|
|
VisitStack.pop_back();
|
|
|
|
} while (!VisitStack.empty());
|
|
|
|
}
|
|
|
|
|
2001-09-29 00:59:14 +02:00
|
|
|
public:
|
2003-07-25 19:49:28 +02:00
|
|
|
typedef typename super::pointer pointer;
|
2003-10-13 18:34:26 +02:00
|
|
|
typedef df_iterator<GraphT, SetType, ExtStorage, GT> _Self;
|
2001-09-29 00:59:14 +02:00
|
|
|
|
|
|
|
// Provide static begin and end methods as our public "constructors"
|
2007-12-15 23:23:24 +01:00
|
|
|
static inline _Self begin(const GraphT& G) {
|
2003-10-13 17:45:33 +02:00
|
|
|
return _Self(GT::getEntryNode(G));
|
2001-09-29 00:59:14 +02:00
|
|
|
}
|
2007-12-15 23:23:24 +01:00
|
|
|
static inline _Self end(const GraphT& G) { return _Self(); }
|
2001-09-29 00:59:14 +02:00
|
|
|
|
2003-10-13 18:34:26 +02:00
|
|
|
// Static begin and end methods as our public ctors for external iterators
|
2007-12-15 23:23:24 +01:00
|
|
|
static inline _Self begin(const GraphT& G, SetType &S) {
|
2003-10-13 18:34:26 +02:00
|
|
|
return _Self(GT::getEntryNode(G), S);
|
|
|
|
}
|
2007-12-15 23:23:24 +01:00
|
|
|
static inline _Self end(const GraphT& G, SetType &S) { return _Self(S); }
|
2001-09-29 00:59:14 +02:00
|
|
|
|
2005-04-21 22:19:05 +02:00
|
|
|
inline bool operator==(const _Self& x) const {
|
2003-10-13 17:45:33 +02:00
|
|
|
return VisitStack.size() == x.VisitStack.size() &&
|
|
|
|
VisitStack == x.VisitStack;
|
2001-09-29 00:59:14 +02:00
|
|
|
}
|
|
|
|
inline bool operator!=(const _Self& x) const { return !operator==(x); }
|
|
|
|
|
2005-04-21 22:19:05 +02:00
|
|
|
inline pointer operator*() const {
|
2009-07-23 08:30:28 +02:00
|
|
|
return VisitStack.back().first.getPointer();
|
2001-09-29 00:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is a nonstandard operator-> that dereferences the pointer an extra
|
|
|
|
// time... so that you can actually call methods ON the Node, because
|
|
|
|
// the contained type is a pointer. This allows BBIt->getTerminator() f.e.
|
|
|
|
//
|
|
|
|
inline NodeType *operator->() const { return operator*(); }
|
|
|
|
|
|
|
|
inline _Self& operator++() { // Preincrement
|
2009-07-23 08:30:28 +02:00
|
|
|
toNext();
|
|
|
|
return *this;
|
|
|
|
}
|
2005-04-21 22:19:05 +02:00
|
|
|
|
2009-07-23 08:30:28 +02:00
|
|
|
// skips all children of the current node and traverses to next node
|
|
|
|
//
|
|
|
|
inline _Self& skipChildren() {
|
|
|
|
VisitStack.pop_back();
|
|
|
|
if (!VisitStack.empty())
|
|
|
|
toNext();
|
2005-04-21 22:19:05 +02:00
|
|
|
return *this;
|
2001-09-29 00:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline _Self operator++(int) { // Postincrement
|
2005-04-21 22:19:05 +02:00
|
|
|
_Self tmp = *this; ++*this; return tmp;
|
2001-09-29 00:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// nodeVisited - return true if this iterator has already visited the
|
|
|
|
// specified node. This is public, and will probably be used to iterate over
|
|
|
|
// nodes that a depth first iteration did not find: ie unreachable nodes.
|
|
|
|
//
|
2005-04-21 22:19:05 +02:00
|
|
|
inline bool nodeVisited(NodeType *Node) const {
|
2003-10-13 18:34:26 +02:00
|
|
|
return this->Visited.count(Node) != 0;
|
2001-09-29 00:59:14 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Provide global constructors that automatically figure out correct types...
|
|
|
|
//
|
|
|
|
template <class T>
|
2007-12-15 23:23:24 +01:00
|
|
|
df_iterator<T> df_begin(const T& G) {
|
2003-10-13 17:45:33 +02:00
|
|
|
return df_iterator<T>::begin(G);
|
2001-09-29 00:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2007-12-15 23:23:24 +01:00
|
|
|
df_iterator<T> df_end(const T& G) {
|
2001-09-29 00:59:14 +02:00
|
|
|
return df_iterator<T>::end(G);
|
|
|
|
}
|
|
|
|
|
2003-10-13 18:34:26 +02:00
|
|
|
// Provide global definitions of external depth first iterators...
|
2003-10-13 18:44:30 +02:00
|
|
|
template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
|
2003-10-13 18:34:26 +02:00
|
|
|
struct df_ext_iterator : public df_iterator<T, SetTy, true> {
|
|
|
|
df_ext_iterator(const df_iterator<T, SetTy, true> &V)
|
|
|
|
: df_iterator<T, SetTy, true>(V) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T, class SetTy>
|
2007-12-15 23:23:24 +01:00
|
|
|
df_ext_iterator<T, SetTy> df_ext_begin(const T& G, SetTy &S) {
|
2003-10-13 18:34:26 +02:00
|
|
|
return df_ext_iterator<T, SetTy>::begin(G, S);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class SetTy>
|
2007-12-15 23:23:24 +01:00
|
|
|
df_ext_iterator<T, SetTy> df_ext_end(const T& G, SetTy &S) {
|
2003-10-13 18:34:26 +02:00
|
|
|
return df_ext_iterator<T, SetTy>::end(G, S);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-29 00:59:14 +02:00
|
|
|
// Provide global definitions of inverse depth first iterators...
|
2007-06-21 23:25:36 +02:00
|
|
|
template <class T,
|
|
|
|
class SetTy = llvm::SmallPtrSet<typename GraphTraits<T>::NodeType*, 8>,
|
2003-10-13 18:34:26 +02:00
|
|
|
bool External = false>
|
|
|
|
struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
|
|
|
|
idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
|
|
|
|
: df_iterator<Inverse<T>, SetTy, External>(V) {}
|
2001-09-29 00:59:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2007-12-15 23:23:24 +01:00
|
|
|
idf_iterator<T> idf_begin(const T& G) {
|
2009-03-21 06:40:09 +01:00
|
|
|
return idf_iterator<T>::begin(Inverse<T>(G));
|
2001-09-29 00:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2007-12-15 23:23:24 +01:00
|
|
|
idf_iterator<T> idf_end(const T& G){
|
2009-03-21 06:40:09 +01:00
|
|
|
return idf_iterator<T>::end(Inverse<T>(G));
|
2001-09-29 00:59:14 +02:00
|
|
|
}
|
|
|
|
|
2003-10-13 18:34:26 +02:00
|
|
|
// Provide global definitions of external inverse depth first iterators...
|
|
|
|
template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
|
|
|
|
struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
|
|
|
|
idf_ext_iterator(const idf_iterator<T, SetTy, true> &V)
|
|
|
|
: idf_iterator<T, SetTy, true>(V) {}
|
|
|
|
idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V)
|
|
|
|
: idf_iterator<T, SetTy, true>(V) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T, class SetTy>
|
2007-12-15 23:23:24 +01:00
|
|
|
idf_ext_iterator<T, SetTy> idf_ext_begin(const T& G, SetTy &S) {
|
2009-03-21 06:40:09 +01:00
|
|
|
return idf_ext_iterator<T, SetTy>::begin(Inverse<T>(G), S);
|
2003-10-13 18:34:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class SetTy>
|
2007-12-15 23:23:24 +01:00
|
|
|
idf_ext_iterator<T, SetTy> idf_ext_end(const T& G, SetTy &S) {
|
2009-03-21 06:40:09 +01:00
|
|
|
return idf_ext_iterator<T, SetTy>::end(Inverse<T>(G), S);
|
2003-10-13 18:34:26 +02:00
|
|
|
}
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
2003-10-13 18:34:26 +02:00
|
|
|
|
2001-09-29 00:59:14 +02:00
|
|
|
#endif
|