Don't attribute in file headers anymore. See llvmdev for the
[oota-llvm.git] / include / llvm / ADT / DepthFirstIterator.h
1 //===- llvm/ADT/DepthFirstIterator.h - Depth First iterator -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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:
12 //
13 // df_begin/df_end/df_iterator
14 //   * Normal depth-first iteration - visit a node and then all of its children.
15 //
16 // idf_begin/idf_end/idf_iterator
17 //   * Depth-first iteration on the 'inverse' graph.
18 //
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
23 //     other purposes.
24 //
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
29 //     other purposes.
30 //
31 //===----------------------------------------------------------------------===//
32
33 #ifndef LLVM_ADT_DEPTHFIRSTITERATOR_H
34 #define LLVM_ADT_DEPTHFIRSTITERATOR_H
35
36 #include "llvm/ADT/GraphTraits.h"
37 #include "llvm/ADT/iterator"
38 #include "llvm/ADT/SmallPtrSet.h"
39 #include <vector>
40 #include <set>
41
42 namespace llvm {
43
44 // df_iterator_storage - A private class which is used to figure out where to
45 // store the visited set.
46 template<class SetType, bool External>   // Non-external set
47 class df_iterator_storage {
48 public:
49   SetType Visited;
50 };
51
52 template<class SetType>
53 class df_iterator_storage<SetType, true> {
54 public:
55   df_iterator_storage(SetType &VSet) : Visited(VSet) {}
56   df_iterator_storage(const df_iterator_storage &S) : Visited(S.Visited) {}
57   SetType &Visited;
58 };
59
60
61 // Generic Depth First Iterator
62 template<class GraphT,
63 class SetType = llvm::SmallPtrSet<typename GraphTraits<GraphT>::NodeType*, 8>,
64          bool ExtStorage = false, class GT = GraphTraits<GraphT> >
65 class df_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t>,
66                     public df_iterator_storage<SetType, ExtStorage> {
67   typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super;
68
69   typedef typename GT::NodeType          NodeType;
70   typedef typename GT::ChildIteratorType ChildItTy;
71
72   // VisitStack - Used to maintain the ordering.  Top = current block
73   // First element is node pointer, second is the 'next child' to visit
74   std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
75 private:
76   inline df_iterator(NodeType *Node) {
77     this->Visited.insert(Node);
78     VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node)));
79   }
80   inline df_iterator() { /* End is when stack is empty */ }
81
82   inline df_iterator(NodeType *Node, SetType &S)
83     : df_iterator_storage<SetType, ExtStorage>(S) {
84     if (!S.count(Node)) {
85       this->Visited.insert(Node);
86       VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node)));
87     }
88   }
89   inline df_iterator(SetType &S)
90     : df_iterator_storage<SetType, ExtStorage>(S) {
91     // End is when stack is empty
92   }
93
94 public:
95   typedef typename super::pointer pointer;
96   typedef df_iterator<GraphT, SetType, ExtStorage, GT> _Self;
97
98   // Provide static begin and end methods as our public "constructors"
99   static inline _Self begin(const GraphT& G) {
100     return _Self(GT::getEntryNode(G));
101   }
102   static inline _Self end(const GraphT& G) { return _Self(); }
103
104   // Static begin and end methods as our public ctors for external iterators
105   static inline _Self begin(const GraphT& G, SetType &S) {
106     return _Self(GT::getEntryNode(G), S);
107   }
108   static inline _Self end(const GraphT& G, SetType &S) { return _Self(S); }
109
110   inline bool operator==(const _Self& x) const {
111     return VisitStack.size() == x.VisitStack.size() &&
112            VisitStack == x.VisitStack;
113   }
114   inline bool operator!=(const _Self& x) const { return !operator==(x); }
115
116   inline pointer operator*() const {
117     return VisitStack.back().first;
118   }
119
120   // This is a nonstandard operator-> that dereferences the pointer an extra
121   // time... so that you can actually call methods ON the Node, because
122   // the contained type is a pointer.  This allows BBIt->getTerminator() f.e.
123   //
124   inline NodeType *operator->() const { return operator*(); }
125
126   inline _Self& operator++() {   // Preincrement
127     do {
128       std::pair<NodeType *, ChildItTy> &Top = VisitStack.back();
129       NodeType *Node = Top.first;
130       ChildItTy &It  = Top.second;
131
132       while (It != GT::child_end(Node)) {
133         NodeType *Next = *It++;
134         if (!this->Visited.count(Next)) {  // Has our next sibling been visited?
135           // No, do it now.
136           this->Visited.insert(Next);
137           VisitStack.push_back(std::make_pair(Next, GT::child_begin(Next)));
138           return *this;
139         }
140       }
141
142       // Oops, ran out of successors... go up a level on the stack.
143       VisitStack.pop_back();
144     } while (!VisitStack.empty());
145     return *this;
146   }
147
148   inline _Self operator++(int) { // Postincrement
149     _Self tmp = *this; ++*this; return tmp;
150   }
151
152   // nodeVisited - return true if this iterator has already visited the
153   // specified node.  This is public, and will probably be used to iterate over
154   // nodes that a depth first iteration did not find: ie unreachable nodes.
155   //
156   inline bool nodeVisited(NodeType *Node) const {
157     return this->Visited.count(Node) != 0;
158   }
159 };
160
161
162 // Provide global constructors that automatically figure out correct types...
163 //
164 template <class T>
165 df_iterator<T> df_begin(const T& G) {
166   return df_iterator<T>::begin(G);
167 }
168
169 template <class T>
170 df_iterator<T> df_end(const T& G) {
171   return df_iterator<T>::end(G);
172 }
173
174 // Provide global definitions of external depth first iterators...
175 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
176 struct df_ext_iterator : public df_iterator<T, SetTy, true> {
177   df_ext_iterator(const df_iterator<T, SetTy, true> &V)
178     : df_iterator<T, SetTy, true>(V) {}
179 };
180
181 template <class T, class SetTy>
182 df_ext_iterator<T, SetTy> df_ext_begin(const T& G, SetTy &S) {
183   return df_ext_iterator<T, SetTy>::begin(G, S);
184 }
185
186 template <class T, class SetTy>
187 df_ext_iterator<T, SetTy> df_ext_end(const T& G, SetTy &S) {
188   return df_ext_iterator<T, SetTy>::end(G, S);
189 }
190
191
192 // Provide global definitions of inverse depth first iterators...
193 template <class T,
194   class SetTy = llvm::SmallPtrSet<typename GraphTraits<T>::NodeType*, 8>,
195           bool External = false>
196 struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
197   idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
198     : df_iterator<Inverse<T>, SetTy, External>(V) {}
199 };
200
201 template <class T>
202 idf_iterator<T> idf_begin(const T& G) {
203   Inverse<T> DummyG;
204   return idf_iterator<T>::begin(DummyG);
205 }
206
207 template <class T>
208 idf_iterator<T> idf_end(const T& G){
209   Inverse<T> DummyG;
210   return idf_iterator<T>::end(DummyG);
211 }
212
213 // Provide global definitions of external inverse depth first iterators...
214 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
215 struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
216   idf_ext_iterator(const idf_iterator<T, SetTy, true> &V)
217     : idf_iterator<T, SetTy, true>(V) {}
218   idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V)
219     : idf_iterator<T, SetTy, true>(V) {}
220 };
221
222 template <class T, class SetTy>
223 idf_ext_iterator<T, SetTy> idf_ext_begin(const T& G, SetTy &S) {
224   Inverse<T> DummyG(G);
225   return idf_ext_iterator<T, SetTy>::begin(DummyG, S);
226 }
227
228 template <class T, class SetTy>
229 idf_ext_iterator<T, SetTy> idf_ext_end(const T& G, SetTy &S) {
230   Inverse<T> DummyG(G);
231   return idf_ext_iterator<T, SetTy>::end(DummyG, S);
232 }
233
234 } // End llvm namespace
235
236 #endif