LLVM  3.7.0
DepthFirstIterator.h
Go to the documentation of this file.
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"
38 #include "llvm/ADT/SmallPtrSet.h"
40 #include <set>
41 #include <vector>
42 
43 namespace llvm {
44 
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
49 public:
50  SetType Visited;
51 };
52 
53 template<class SetType>
54 class df_iterator_storage<SetType, true> {
55 public:
56  df_iterator_storage(SetType &VSet) : Visited(VSet) {}
58  SetType &Visited;
59 };
60 
61 
62 // Generic Depth First Iterator
63 template<class GraphT,
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;
71 
72  typedef typename GT::NodeType NodeType;
73  typedef typename GT::ChildIteratorType ChildItTy;
75 
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)
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)
99  // End is when stack is empty
100  }
101 
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  }
112 
113  while (It != GT::child_end(Node)) {
114  NodeType *Next = *It++;
115  // Has our next sibling been visited?
116  if (Next && this->Visited.insert(Next).second) {
117  // No, do it now.
118  VisitStack.push_back(std::make_pair(PointerIntTy(Next, 0),
119  GT::child_begin(Next)));
120  return;
121  }
122  }
123 
124  // Oops, ran out of successors... go up a level on the stack.
125  VisitStack.pop_back();
126  } while (!VisitStack.empty());
127  }
128 
129 public:
130  typedef typename super::pointer pointer;
131 
132  // Provide static begin and end methods as our public "constructors"
133  static df_iterator begin(const GraphT &G) {
134  return df_iterator(GT::getEntryNode(G));
135  }
136  static df_iterator end(const GraphT &G) { return df_iterator(); }
137 
138  // Static begin and end methods as our public ctors for external iterators
139  static df_iterator begin(const GraphT &G, SetType &S) {
140  return df_iterator(GT::getEntryNode(G), S);
141  }
142  static df_iterator end(const GraphT &G, SetType &S) { return df_iterator(S); }
143 
144  bool operator==(const df_iterator &x) const {
145  return VisitStack == x.VisitStack;
146  }
147  bool operator!=(const df_iterator &x) const { return !(*this == x); }
148 
149  pointer operator*() const { return VisitStack.back().first.getPointer(); }
150 
151  // This is a nonstandard operator-> that dereferences the pointer an extra
152  // time... so that you can actually call methods ON the Node, because
153  // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
154  //
155  NodeType *operator->() const { return **this; }
156 
157  df_iterator &operator++() { // Preincrement
158  toNext();
159  return *this;
160  }
161 
162  /// \brief Skips all children of the current node and traverses to next node
163  ///
164  /// Note: This function takes care of incrementing the iterator. If you
165  /// always increment and call this function, you risk walking off the end.
167  VisitStack.pop_back();
168  if (!VisitStack.empty())
169  toNext();
170  return *this;
171  }
172 
173  df_iterator operator++(int) { // Postincrement
174  df_iterator tmp = *this;
175  ++*this;
176  return tmp;
177  }
178 
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  bool nodeVisited(NodeType *Node) const {
184  return this->Visited.count(Node) != 0;
185  }
186 
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(); }
190 
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 };
197 
198 
199 // Provide global constructors that automatically figure out correct types...
200 //
201 template <class T>
203  return df_iterator<T>::begin(G);
204 }
205 
206 template <class T>
208  return df_iterator<T>::end(G);
209 }
210 
211 // Provide an accessor method to use them in range-based patterns.
212 template <class T>
214  return make_range(df_begin(G), df_end(G));
215 }
216 
217 // Provide global definitions of external depth first iterators...
219 struct df_ext_iterator : public df_iterator<T, SetTy, true> {
221  : df_iterator<T, SetTy, true>(V) {}
222 };
223 
224 template <class T, class SetTy>
227 }
228 
229 template <class T, class SetTy>
231  return df_ext_iterator<T, SetTy>::end(G, S);
232 }
233 
234 template <class T, class SetTy>
236  SetTy &S) {
237  return make_range(df_ext_begin(G, S), df_ext_end(G, S));
238 }
239 
240 
241 // Provide global definitions of inverse depth first iterators...
242 template <class T,
244  bool External = false>
245 struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
246  idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
247  : df_iterator<Inverse<T>, SetTy, External>(V) {}
248 };
249 
250 template <class T>
253 }
254 
255 template <class T>
257  return idf_iterator<T>::end(Inverse<T>(G));
258 }
259 
260 // Provide an accessor method to use them in range-based patterns.
261 template <class T>
263  return make_range(idf_begin(G), idf_end(G));
264 }
265 
266 // Provide global definitions of external inverse depth first iterators...
268 struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
270  : idf_iterator<T, SetTy, true>(V) {}
272  : idf_iterator<T, SetTy, true>(V) {}
273 };
274 
275 template <class T, class SetTy>
278 }
279 
280 template <class T, class SetTy>
283 }
284 
285 template <class T, class SetTy>
287  SetTy &S) {
288  return make_range(idf_ext_begin(G, S), idf_ext_end(G, S));
289 }
290 
291 } // End llvm namespace
292 
293 #endif
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: GraphTraits.h:27
pointer operator*() const
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
iterator_range< idf_ext_iterator< T, SetTy > > inverse_depth_first_ext(const T &G, SetTy &S)
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition: ISDOpcodes.h:39
df_iterator & skipChildren()
Skips all children of the current node and traverses to next node.
static df_iterator end(const GraphT &G)
super::pointer pointer
#define G(x, y, z)
Definition: MD5.cpp:52
idf_iterator(const df_iterator< Inverse< T >, SetTy, External > &V)
#define T
idf_iterator< T > idf_begin(const T &G)
idf_iterator< T > idf_end(const T &G)
bool operator!=(const df_iterator &x) const
#define true
Definition: ConvertUTF.c:66
PointerIntPair - This class implements a pair of a pointer and small integer.
NodeType * operator->() const
df_ext_iterator< T, SetTy > df_ext_end(const T &G, SetTy &S)
df_iterator< T > df_end(const T &G)
idf_ext_iterator< T, SetTy > idf_ext_end(const T &G, SetTy &S)
iterator_range< df_ext_iterator< T, SetTy > > depth_first_ext(const T &G, SetTy &S)
df_ext_iterator< T, SetTy > df_ext_begin(const T &G, SetTy &S)
idf_ext_iterator(const idf_iterator< T, SetTy, true > &V)
static df_iterator begin(const GraphT &G)
idf_ext_iterator< T, SetTy > idf_ext_begin(const T &G, SetTy &S)
static df_iterator end(const GraphT &G, SetType &S)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
df_ext_iterator(const df_iterator< T, SetTy, true > &V)
iterator_range< idf_iterator< T > > inverse_depth_first(const T &G)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
df_iterator operator++(int)
df_iterator_storage(const df_iterator_storage &S)
df_iterator< T > df_begin(const T &G)
A range adaptor for a pair of iterators.
iterator_range< df_iterator< T > > depth_first(const T &G)
df_iterator & operator++()
idf_ext_iterator(const df_iterator< Inverse< T >, SetTy, true > &V)
NodeType * getPath(unsigned n) const
getPath - Return the n'th node in the path from the entry node to the current node.
static df_iterator begin(const GraphT &G, SetType &S)
bool operator==(const df_iterator &x) const
unsigned getPathLength() const
getPathLength - Return the length of the path from the entry node to the current node, counting both nodes.
bool nodeVisited(NodeType *Node) const