]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - opencl/llvm.git/blob - include/llvm/ADT/DepthFirstIterator.h
0f6914662ddd06e41f8806ae5ee352ef59ac22b0
[opencl/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 //===----------------------------------------------------------------------===//
33 #ifndef LLVM_ADT_DEPTHFIRSTITERATOR_H
34 #define LLVM_ADT_DEPTHFIRSTITERATOR_H
36 #include "llvm/ADT/iterator_range.h"
37 #include "llvm/ADT/GraphTraits.h"
38 #include "llvm/ADT/PointerIntPair.h"
39 #include "llvm/ADT/SmallPtrSet.h"
40 #include <set>
41 #include <vector>
43 namespace llvm {
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 {
49 public:
50   SetType Visited;
51 };
53 template<class SetType>
54 class df_iterator_storage<SetType, true> {
55 public:
56   df_iterator_storage(SetType &VSet) : Visited(VSet) {}
57   df_iterator_storage(const df_iterator_storage &S) : Visited(S.Visited) {}
58   SetType &Visited;
59 };
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 std::iterator<std::forward_iterator_tag,
67                                          typename GT::NodeType, ptrdiff_t>,
68                     public df_iterator_storage<SetType, ExtStorage> {
69   typedef std::iterator<std::forward_iterator_tag,
70                         typename GT::NodeType, ptrdiff_t> super;
72   typedef typename GT::NodeType          NodeType;
73   typedef typename GT::ChildIteratorType ChildItTy;
74   typedef PointerIntPair<NodeType*, 1>   PointerIntTy;
76   // VisitStack - Used to maintain the ordering.  Top = current block
77   // First element is node pointer, second is the 'next child' to visit
78   // if the int in PointerIntTy is 0, the 'next child' to visit is invalid
79   std::vector<std::pair<PointerIntTy, ChildItTy> > VisitStack;
80 private:
81   inline df_iterator(NodeType *Node) {
82     this->Visited.insert(Node);
83     VisitStack.push_back(std::make_pair(PointerIntTy(Node, 0), 
84                                         GT::child_begin(Node)));
85   }
86   inline df_iterator() { 
87     // End is when stack is empty 
88   }
89   inline df_iterator(NodeType *Node, SetType &S)
90     : df_iterator_storage<SetType, ExtStorage>(S) {
91     if (!S.count(Node)) {
92       VisitStack.push_back(std::make_pair(PointerIntTy(Node, 0), 
93                                           GT::child_begin(Node)));
94       this->Visited.insert(Node);
95     }
96   }
97   inline df_iterator(SetType &S)
98     : df_iterator_storage<SetType, ExtStorage>(S) {
99     // End is when stack is empty
100   }
102   inline void toNext() {
103     do {
104       std::pair<PointerIntTy, ChildItTy> &Top = VisitStack.back();
105       NodeType *Node = Top.first.getPointer();
106       ChildItTy &It  = Top.second;
107       if (!Top.first.getInt()) {
108         // now retrieve the real begin of the children before we dive in
109         It = GT::child_begin(Node);
110         Top.first.setInt(1);
111       }
113       while (It != GT::child_end(Node)) {
114         NodeType *Next = *It++;
115         // Has our next sibling been visited?
116         if (Next && !this->Visited.count(Next)) {  
117           // No, do it now.
118           this->Visited.insert(Next);
119           VisitStack.push_back(std::make_pair(PointerIntTy(Next, 0), 
120                                               GT::child_begin(Next)));
121           return;
122         }
123       }
125       // Oops, ran out of successors... go up a level on the stack.
126       VisitStack.pop_back();
127     } while (!VisitStack.empty());
128   }
130 public:
131   typedef typename super::pointer pointer;
132   typedef df_iterator<GraphT, SetType, ExtStorage, GT> _Self;
134   // Provide static begin and end methods as our public "constructors"
135   static inline _Self begin(const GraphT& G) {
136     return _Self(GT::getEntryNode(G));
137   }
138   static inline _Self end(const GraphT& G) { return _Self(); }
140   // Static begin and end methods as our public ctors for external iterators
141   static inline _Self begin(const GraphT& G, SetType &S) {
142     return _Self(GT::getEntryNode(G), S);
143   }
144   static inline _Self end(const GraphT& G, SetType &S) { return _Self(S); }
146   inline bool operator==(const _Self& x) const {
147     return VisitStack == x.VisitStack;
148   }
149   inline bool operator!=(const _Self& x) const { return !operator==(x); }
151   inline pointer operator*() const {
152     return VisitStack.back().first.getPointer();
153   }
155   // This is a nonstandard operator-> that dereferences the pointer an extra
156   // time... so that you can actually call methods ON the Node, because
157   // the contained type is a pointer.  This allows BBIt->getTerminator() f.e.
158   //
159   inline NodeType *operator->() const { return operator*(); }
161   inline _Self& operator++() {   // Preincrement
162     toNext();
163     return *this;
164   }
166   // skips all children of the current node and traverses to next node
167   //
168   inline _Self& skipChildren() {  
169     VisitStack.pop_back();
170     if (!VisitStack.empty())
171       toNext();
172     return *this;
173   }
175   inline _Self operator++(int) { // Postincrement
176     _Self tmp = *this; ++*this; return tmp;
177   }
179   // nodeVisited - return true if this iterator has already visited the
180   // specified node.  This is public, and will probably be used to iterate over
181   // nodes that a depth first iteration did not find: ie unreachable nodes.
182   //
183   inline bool nodeVisited(NodeType *Node) const {
184     return this->Visited.count(Node) != 0;
185   }
187   /// getPathLength - Return the length of the path from the entry node to the
188   /// current node, counting both nodes.
189   unsigned getPathLength() const { return VisitStack.size(); }
191   /// getPath - Return the n'th node in the path from the entry node to the
192   /// current node.
193   NodeType *getPath(unsigned n) const {
194     return VisitStack[n].first.getPointer();
195   }
196 };
199 // Provide global constructors that automatically figure out correct types...
200 //
201 template <class T>
202 df_iterator<T> df_begin(const T& G) {
203   return df_iterator<T>::begin(G);
206 template <class T>
207 df_iterator<T> df_end(const T& G) {
208   return df_iterator<T>::end(G);
211 // Provide an accessor method to use them in range-based patterns.
212 template <class T>
213 iterator_range<df_iterator<T>> depth_first(const T& G) {
214   return iterator_range<df_iterator<T>>(df_begin(G), df_end(G));
217 // Provide global definitions of external depth first iterators...
218 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
219 struct df_ext_iterator : public df_iterator<T, SetTy, true> {
220   df_ext_iterator(const df_iterator<T, SetTy, true> &V)
221     : df_iterator<T, SetTy, true>(V) {}
222 };
224 template <class T, class SetTy>
225 df_ext_iterator<T, SetTy> df_ext_begin(const T& G, SetTy &S) {
226   return df_ext_iterator<T, SetTy>::begin(G, S);
229 template <class T, class SetTy>
230 df_ext_iterator<T, SetTy> df_ext_end(const T& G, SetTy &S) {
231   return df_ext_iterator<T, SetTy>::end(G, S);
234 template <class T, class SetTy>
235 iterator_range<df_ext_iterator<T, SetTy>> depth_first_ext(const T& G,
236                                                           SetTy &S) {
237   return iterator_range<df_ext_iterator<T, SetTy>>(df_ext_begin(G, S),
238                                                    df_ext_end(G, S));
242 // Provide global definitions of inverse depth first iterators...
243 template <class T,
244   class SetTy = llvm::SmallPtrSet<typename GraphTraits<T>::NodeType*, 8>,
245           bool External = false>
246 struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
247   idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
248     : df_iterator<Inverse<T>, SetTy, External>(V) {}
249 };
251 template <class T>
252 idf_iterator<T> idf_begin(const T& G) {
253   return idf_iterator<T>::begin(Inverse<T>(G));
256 template <class T>
257 idf_iterator<T> idf_end(const T& G){
258   return idf_iterator<T>::end(Inverse<T>(G));
261 // Provide an accessor method to use them in range-based patterns.
262 template <class T>
263 iterator_range<idf_iterator<T>> inverse_depth_first(const T& G) {
264   return iterator_range<idf_iterator<T>>(idf_begin(G), idf_end(G));
267 // Provide global definitions of external inverse depth first iterators...
268 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
269 struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
270   idf_ext_iterator(const idf_iterator<T, SetTy, true> &V)
271     : idf_iterator<T, SetTy, true>(V) {}
272   idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V)
273     : idf_iterator<T, SetTy, true>(V) {}
274 };
276 template <class T, class SetTy>
277 idf_ext_iterator<T, SetTy> idf_ext_begin(const T& G, SetTy &S) {
278   return idf_ext_iterator<T, SetTy>::begin(Inverse<T>(G), S);
281 template <class T, class SetTy>
282 idf_ext_iterator<T, SetTy> idf_ext_end(const T& G, SetTy &S) {
283   return idf_ext_iterator<T, SetTy>::end(Inverse<T>(G), S);
286 template <class T, class SetTy>
287 iterator_range<idf_ext_iterator<T, SetTy>> inverse_depth_first_ext(const T& G,
288                                                                    SetTy &S) {
289   return iterator_range<idf_ext_iterator<T, SetTy>>(idf_ext_begin(G, S),
290                                                     idf_ext_end(G, S));
293 } // End llvm namespace
295 #endif