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/PointerIntPair.h"
38 #include "llvm/ADT/SmallPtrSet.h"
39 #include "llvm/ADT/iterator_range.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) {}
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 std::iterator<std::forward_iterator_tag,
66 typename GT::NodeType, ptrdiff_t>,
67 public df_iterator_storage<SetType, ExtStorage> {
68 typedef std::iterator<std::forward_iterator_tag,
69 typename GT::NodeType, ptrdiff_t> super;
71 typedef typename GT::NodeType NodeType;
72 typedef typename GT::ChildIteratorType ChildItTy;
73 typedef PointerIntPair<NodeType*, 1> PointerIntTy;
75 // VisitStack - Used to maintain the ordering. Top = current block
76 // First element is node pointer, second is the 'next child' to visit
77 // if the int in PointerIntTy is 0, the 'next child' to visit is invalid
78 std::vector<std::pair<PointerIntTy, ChildItTy>> VisitStack;
81 inline df_iterator(NodeType *Node) {
82 this->Visited.insert(Node);
84 std::make_pair(PointerIntTy(Node, 0), GT::child_begin(Node)));
86 inline df_iterator() {
87 // End is when stack is empty
89 inline df_iterator(NodeType *Node, SetType &S)
90 : df_iterator_storage<SetType, ExtStorage>(S) {
93 std::make_pair(PointerIntTy(Node, 0), GT::child_begin(Node)));
94 this->Visited.insert(Node);
97 inline df_iterator(SetType &S)
98 : df_iterator_storage<SetType, ExtStorage>(S) {
99 // End is when stack is empty
102 inline void toNext() {
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);
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) {
118 VisitStack.push_back(
119 std::make_pair(PointerIntTy(Next, 0), GT::child_begin(Next)));
124 // Oops, ran out of successors... go up a level on the stack.
125 VisitStack.pop_back();
126 } while (!VisitStack.empty());
130 typedef typename super::pointer pointer;
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));
136 static df_iterator end(const GraphT &G) { return df_iterator(); }
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);
142 static df_iterator end(const GraphT &G, SetType &S) { return df_iterator(S); }
144 bool operator==(const df_iterator &x) const {
145 return VisitStack == x.VisitStack;
147 bool operator!=(const df_iterator &x) const { return !(*this == x); }
149 pointer operator*() const { return VisitStack.back().first.getPointer(); }
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.
155 NodeType *operator->() const { return **this; }
157 df_iterator &operator++() { // Preincrement
162 /// \brief Skips all children of the current node and traverses to next node
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.
166 df_iterator &skipChildren() {
167 VisitStack.pop_back();
168 if (!VisitStack.empty())
173 df_iterator operator++(int) { // Postincrement
174 df_iterator tmp = *this;
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.
183 bool nodeVisited(NodeType *Node) const {
184 return this->Visited.count(Node) != 0;
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
193 NodeType *getPath(unsigned n) const {
194 return VisitStack[n].first.getPointer();
198 // Provide global constructors that automatically figure out correct types...
201 df_iterator<T> df_begin(const T& G) {
202 return df_iterator<T>::begin(G);
206 df_iterator<T> df_end(const T& G) {
207 return df_iterator<T>::end(G);
210 // Provide an accessor method to use them in range-based patterns.
212 iterator_range<df_iterator<T>> depth_first(const T& G) {
213 return make_range(df_begin(G), df_end(G));
216 // Provide global definitions of external depth first iterators...
217 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
218 struct df_ext_iterator : public df_iterator<T, SetTy, true> {
219 df_ext_iterator(const df_iterator<T, SetTy, true> &V)
220 : df_iterator<T, SetTy, true>(V) {}
223 template <class T, class SetTy>
224 df_ext_iterator<T, SetTy> df_ext_begin(const T& G, SetTy &S) {
225 return df_ext_iterator<T, SetTy>::begin(G, S);
228 template <class T, class SetTy>
229 df_ext_iterator<T, SetTy> df_ext_end(const T& G, SetTy &S) {
230 return df_ext_iterator<T, SetTy>::end(G, S);
233 template <class T, class SetTy>
234 iterator_range<df_ext_iterator<T, SetTy>> depth_first_ext(const T& G,
236 return make_range(df_ext_begin(G, S), df_ext_end(G, S));
239 // Provide global definitions of inverse depth first iterators...
241 class SetTy = llvm::SmallPtrSet<typename GraphTraits<T>::NodeType*, 8>,
242 bool External = false>
243 struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
244 idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
245 : df_iterator<Inverse<T>, SetTy, External>(V) {}
249 idf_iterator<T> idf_begin(const T& G) {
250 return idf_iterator<T>::begin(Inverse<T>(G));
254 idf_iterator<T> idf_end(const T& G){
255 return idf_iterator<T>::end(Inverse<T>(G));
258 // Provide an accessor method to use them in range-based patterns.
260 iterator_range<idf_iterator<T>> inverse_depth_first(const T& G) {
261 return make_range(idf_begin(G), idf_end(G));
264 // Provide global definitions of external inverse depth first iterators...
265 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
266 struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
267 idf_ext_iterator(const idf_iterator<T, SetTy, true> &V)
268 : idf_iterator<T, SetTy, true>(V) {}
269 idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V)
270 : idf_iterator<T, SetTy, true>(V) {}
273 template <class T, class SetTy>
274 idf_ext_iterator<T, SetTy> idf_ext_begin(const T& G, SetTy &S) {
275 return idf_ext_iterator<T, SetTy>::begin(Inverse<T>(G), S);
278 template <class T, class SetTy>
279 idf_ext_iterator<T, SetTy> idf_ext_end(const T& G, SetTy &S) {
280 return idf_ext_iterator<T, SetTy>::end(Inverse<T>(G), S);
283 template <class T, class SetTy>
284 iterator_range<idf_ext_iterator<T, SetTy>> inverse_depth_first_ext(const T& G,
286 return make_range(idf_ext_begin(G, S), idf_ext_end(G, S));
289 } // End llvm namespace