1 //===- llvm/ADT/DepthFirstIterator.h - Depth First iterator -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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"
43 // df_iterator_storage - A private class which is used to figure out where to
44 // store the visited set.
45 template<class SetType, bool External> // Non-external set
46 class df_iterator_storage {
51 template<class SetType>
52 class df_iterator_storage<SetType, true> {
54 df_iterator_storage(SetType &VSet) : Visited(VSet) {}
55 df_iterator_storage(const df_iterator_storage &S) : Visited(S.Visited) {}
60 // Generic Depth First Iterator
61 template<class GraphT, class SetType =
62 std::set<typename GraphTraits<GraphT>::NodeType*>,
63 bool ExtStorage = false, class GT = GraphTraits<GraphT> >
64 class df_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t>,
65 public df_iterator_storage<SetType, ExtStorage> {
66 typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super;
68 typedef typename GT::NodeType NodeType;
69 typedef typename GT::ChildIteratorType ChildItTy;
71 // VisitStack - Used to maintain the ordering. Top = current block
72 // First element is node pointer, second is the 'next child' to visit
73 std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
75 inline df_iterator(NodeType *Node) {
76 this->Visited.insert(Node);
77 VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node)));
79 inline df_iterator() { /* End is when stack is empty */ }
81 inline df_iterator(NodeType *Node, SetType &S)
82 : df_iterator_storage<SetType, ExtStorage>(S) {
84 this->Visited.insert(Node);
85 VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node)));
88 inline df_iterator(SetType &S)
89 : df_iterator_storage<SetType, ExtStorage>(S) {
90 // End is when stack is empty
94 typedef typename super::pointer pointer;
95 typedef df_iterator<GraphT, SetType, ExtStorage, GT> _Self;
97 // Provide static begin and end methods as our public "constructors"
98 static inline _Self begin(GraphT G) {
99 return _Self(GT::getEntryNode(G));
101 static inline _Self end(GraphT G) { return _Self(); }
103 // Static begin and end methods as our public ctors for external iterators
104 static inline _Self begin(GraphT G, SetType &S) {
105 return _Self(GT::getEntryNode(G), S);
107 static inline _Self end(GraphT G, SetType &S) { return _Self(S); }
109 inline bool operator==(const _Self& x) const {
110 return VisitStack.size() == x.VisitStack.size() &&
111 VisitStack == x.VisitStack;
113 inline bool operator!=(const _Self& x) const { return !operator==(x); }
115 inline pointer operator*() const {
116 return VisitStack.back().first;
119 // This is a nonstandard operator-> that dereferences the pointer an extra
120 // time... so that you can actually call methods ON the Node, because
121 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
123 inline NodeType *operator->() const { return operator*(); }
125 inline _Self& operator++() { // Preincrement
127 std::pair<NodeType *, ChildItTy> &Top = VisitStack.back();
128 NodeType *Node = Top.first;
129 ChildItTy &It = Top.second;
131 while (It != GT::child_end(Node)) {
132 NodeType *Next = *It++;
133 if (!this->Visited.count(Next)) { // Has our next sibling been visited?
135 this->Visited.insert(Next);
136 VisitStack.push_back(std::make_pair(Next, GT::child_begin(Next)));
141 // Oops, ran out of successors... go up a level on the stack.
142 VisitStack.pop_back();
143 } while (!VisitStack.empty());
147 inline _Self operator++(int) { // Postincrement
148 _Self tmp = *this; ++*this; return tmp;
151 // nodeVisited - return true if this iterator has already visited the
152 // specified node. This is public, and will probably be used to iterate over
153 // nodes that a depth first iteration did not find: ie unreachable nodes.
155 inline bool nodeVisited(NodeType *Node) const {
156 return this->Visited.count(Node) != 0;
161 // Provide global constructors that automatically figure out correct types...
164 df_iterator<T> df_begin(T G) {
165 return df_iterator<T>::begin(G);
169 df_iterator<T> df_end(T G) {
170 return df_iterator<T>::end(G);
173 // Provide global definitions of external depth first iterators...
174 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
175 struct df_ext_iterator : public df_iterator<T, SetTy, true> {
176 df_ext_iterator(const df_iterator<T, SetTy, true> &V)
177 : df_iterator<T, SetTy, true>(V) {}
180 template <class T, class SetTy>
181 df_ext_iterator<T, SetTy> df_ext_begin(T G, SetTy &S) {
182 return df_ext_iterator<T, SetTy>::begin(G, S);
185 template <class T, class SetTy>
186 df_ext_iterator<T, SetTy> df_ext_end(T G, SetTy &S) {
187 return df_ext_iterator<T, SetTy>::end(G, S);
191 // Provide global definitions of inverse depth first iterators...
192 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*>,
193 bool External = false>
194 struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
195 idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
196 : df_iterator<Inverse<T>, SetTy, External>(V) {}
200 idf_iterator<T> idf_begin(T G) {
201 return idf_iterator<T>::begin(G);
205 idf_iterator<T> idf_end(T G){
206 return idf_iterator<T>::end(G);
209 // Provide global definitions of external inverse depth first iterators...
210 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
211 struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
212 idf_ext_iterator(const idf_iterator<T, SetTy, true> &V)
213 : idf_iterator<T, SetTy, true>(V) {}
214 idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V)
215 : idf_iterator<T, SetTy, true>(V) {}
218 template <class T, class SetTy>
219 idf_ext_iterator<T, SetTy> idf_ext_begin(T G, SetTy &S) {
220 return idf_ext_iterator<T, SetTy>::begin(G, S);
223 template <class T, class SetTy>
224 idf_ext_iterator<T, SetTy> idf_ext_end(T G, SetTy &S) {
225 return idf_ext_iterator<T, SetTy>::end(G, S);
228 } // End llvm namespace