1 //===- llvm/ADT/DepthFirstIterator.h - Depth First iterator -----*- 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 file builds on the ADT/GraphTraits.h file to build generic depth
11 // first graph iterator. This file exposes the following functions/types:
13 // df_begin/df_end/df_iterator
14 // * Normal depth-first iteration - visit a node and then all of its children.
16 // idf_begin/idf_end/idf_iterator
17 // * Depth-first iteration on the 'inverse' graph.
19 // df_ext_begin/df_ext_end/df_ext_iterator
20 // * Normal depth-first iteration - visit a node and then all of its children.
21 // This iterator stores the 'visited' set in an external set, which allows
22 // it to be more efficient, and allows external clients to use the set for
25 // idf_ext_begin/idf_ext_end/idf_ext_iterator
26 // * Depth-first iteration on the 'inverse' graph.
27 // This iterator stores the 'visited' set in an external set, which allows
28 // it to be more efficient, and allows external clients to use the set for
31 //===----------------------------------------------------------------------===//
33 #ifndef LLVM_ADT_DEPTHFIRSTITERATOR_H
34 #define LLVM_ADT_DEPTHFIRSTITERATOR_H
36 #include "llvm/ADT/GraphTraits.h"
37 #include "llvm/ADT/iterator.h"
38 #include "llvm/ADT/SmallPtrSet.h"
39 #include "llvm/ADT/PointerIntPair.h"
45 // df_iterator_storage - A private class which is used to figure out where to
46 // store the visited set.
47 template<class SetType, bool External> // Non-external set
48 class df_iterator_storage {
53 template<class SetType>
54 class df_iterator_storage<SetType, true> {
56 df_iterator_storage(SetType &VSet) : Visited(VSet) {}
57 df_iterator_storage(const df_iterator_storage &S) : Visited(S.Visited) {}
62 // Generic Depth First Iterator
63 template<class GraphT,
64 class SetType = llvm::SmallPtrSet<typename GraphTraits<GraphT>::NodeType*, 8>,
65 bool ExtStorage = false, class GT = GraphTraits<GraphT> >
66 class df_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t>,
67 public df_iterator_storage<SetType, ExtStorage> {
68 typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super;
70 typedef typename GT::NodeType NodeType;
71 typedef typename GT::ChildIteratorType ChildItTy;
72 typedef PointerIntPair<NodeType*, 1> PointerIntTy;
74 // VisitStack - Used to maintain the ordering. Top = current block
75 // First element is node pointer, second is the 'next child' to visit
76 // if the int in PointerIntTy is 0, the 'next child' to visit is invalid
77 std::vector<std::pair<PointerIntTy, ChildItTy> > VisitStack;
79 inline df_iterator(NodeType *Node) {
80 this->Visited.insert(Node);
81 VisitStack.push_back(std::make_pair(PointerIntTy(Node, 0),
82 GT::child_begin(Node)));
84 inline df_iterator() {
85 // End is when stack is empty
87 inline df_iterator(NodeType *Node, SetType &S)
88 : df_iterator_storage<SetType, ExtStorage>(S) {
90 VisitStack.push_back(std::make_pair(PointerIntTy(Node, 0),
91 GT::child_begin(Node)));
92 this->Visited.insert(Node);
95 inline df_iterator(SetType &S)
96 : df_iterator_storage<SetType, ExtStorage>(S) {
97 // End is when stack is empty
100 inline void toNext() {
102 std::pair<PointerIntTy, ChildItTy> &Top = VisitStack.back();
103 NodeType *Node = Top.first.getPointer();
104 ChildItTy &It = Top.second;
105 if (!Top.first.getInt()) {
106 // now retrieve the real begin of the children before we dive in
107 It = GT::child_begin(Node);
111 while (It != GT::child_end(Node)) {
112 NodeType *Next = *It++;
113 // Has our next sibling been visited?
114 if (Next && !this->Visited.count(Next)) {
116 this->Visited.insert(Next);
117 VisitStack.push_back(std::make_pair(PointerIntTy(Next, 0),
118 GT::child_begin(Next)));
123 // Oops, ran out of successors... go up a level on the stack.
124 VisitStack.pop_back();
125 } while (!VisitStack.empty());
129 typedef typename super::pointer pointer;
130 typedef df_iterator<GraphT, SetType, ExtStorage, GT> _Self;
132 // Provide static begin and end methods as our public "constructors"
133 static inline _Self begin(const GraphT& G) {
134 return _Self(GT::getEntryNode(G));
136 static inline _Self end(const GraphT& G) { return _Self(); }
138 // Static begin and end methods as our public ctors for external iterators
139 static inline _Self begin(const GraphT& G, SetType &S) {
140 return _Self(GT::getEntryNode(G), S);
142 static inline _Self end(const GraphT& G, SetType &S) { return _Self(S); }
144 inline bool operator==(const _Self& x) const {
145 return VisitStack.size() == x.VisitStack.size() &&
146 VisitStack == x.VisitStack;
148 inline bool operator!=(const _Self& x) const { return !operator==(x); }
150 inline pointer operator*() const {
151 return VisitStack.back().first.getPointer();
154 // This is a nonstandard operator-> that dereferences the pointer an extra
155 // time... so that you can actually call methods ON the Node, because
156 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
158 inline NodeType *operator->() const { return operator*(); }
160 inline _Self& operator++() { // Preincrement
165 // skips all children of the current node and traverses to next node
167 inline _Self& skipChildren() {
168 VisitStack.pop_back();
169 if (!VisitStack.empty())
174 inline _Self operator++(int) { // Postincrement
175 _Self tmp = *this; ++*this; return tmp;
178 // nodeVisited - return true if this iterator has already visited the
179 // specified node. This is public, and will probably be used to iterate over
180 // nodes that a depth first iteration did not find: ie unreachable nodes.
182 inline bool nodeVisited(NodeType *Node) const {
183 return this->Visited.count(Node) != 0;
188 // Provide global constructors that automatically figure out correct types...
191 df_iterator<T> df_begin(const T& G) {
192 return df_iterator<T>::begin(G);
196 df_iterator<T> df_end(const T& G) {
197 return df_iterator<T>::end(G);
200 // Provide global definitions of external depth first iterators...
201 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
202 struct df_ext_iterator : public df_iterator<T, SetTy, true> {
203 df_ext_iterator(const df_iterator<T, SetTy, true> &V)
204 : df_iterator<T, SetTy, true>(V) {}
207 template <class T, class SetTy>
208 df_ext_iterator<T, SetTy> df_ext_begin(const T& G, SetTy &S) {
209 return df_ext_iterator<T, SetTy>::begin(G, S);
212 template <class T, class SetTy>
213 df_ext_iterator<T, SetTy> df_ext_end(const T& G, SetTy &S) {
214 return df_ext_iterator<T, SetTy>::end(G, S);
218 // Provide global definitions of inverse depth first iterators...
220 class SetTy = llvm::SmallPtrSet<typename GraphTraits<T>::NodeType*, 8>,
221 bool External = false>
222 struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
223 idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
224 : df_iterator<Inverse<T>, SetTy, External>(V) {}
228 idf_iterator<T> idf_begin(const T& G) {
229 return idf_iterator<T>::begin(Inverse<T>(G));
233 idf_iterator<T> idf_end(const T& G){
234 return idf_iterator<T>::end(Inverse<T>(G));
237 // Provide global definitions of external inverse depth first iterators...
238 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
239 struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
240 idf_ext_iterator(const idf_iterator<T, SetTy, true> &V)
241 : idf_iterator<T, SetTy, true>(V) {}
242 idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V)
243 : idf_iterator<T, SetTy, true>(V) {}
246 template <class T, class SetTy>
247 idf_ext_iterator<T, SetTy> idf_ext_begin(const T& G, SetTy &S) {
248 return idf_ext_iterator<T, SetTy>::begin(Inverse<T>(G), S);
251 template <class T, class SetTy>
252 idf_ext_iterator<T, SetTy> idf_ext_end(const T& G, SetTy &S) {
253 return idf_ext_iterator<T, SetTy>::end(Inverse<T>(G), S);
256 } // End llvm namespace