DepthFirstIterator.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #ifndef LLVM_ADT_DEPTHFIRSTITERATOR_H
  33. #define LLVM_ADT_DEPTHFIRSTITERATOR_H
  34. #include "llvm/ADT/GraphTraits.h"
  35. #include "llvm/ADT/PointerIntPair.h"
  36. #include "llvm/ADT/SmallPtrSet.h"
  37. #include "llvm/ADT/iterator_range.h"
  38. #include <set>
  39. #include <vector>
  40. namespace llvm {
  41. // df_iterator_storage - A private class which is used to figure out where to
  42. // store the visited set.
  43. template<class SetType, bool External> // Non-external set
  44. class df_iterator_storage {
  45. public:
  46. SetType Visited;
  47. };
  48. template<class SetType>
  49. class df_iterator_storage<SetType, true> {
  50. public:
  51. df_iterator_storage(SetType &VSet) : Visited(VSet) {}
  52. df_iterator_storage(const df_iterator_storage &S) : Visited(S.Visited) {}
  53. SetType &Visited;
  54. };
  55. // Generic Depth First Iterator
  56. template<class GraphT,
  57. class SetType = llvm::SmallPtrSet<typename GraphTraits<GraphT>::NodeType*, 8>,
  58. bool ExtStorage = false, class GT = GraphTraits<GraphT> >
  59. class df_iterator : public std::iterator<std::forward_iterator_tag,
  60. typename GT::NodeType, ptrdiff_t>,
  61. public df_iterator_storage<SetType, ExtStorage> {
  62. typedef std::iterator<std::forward_iterator_tag,
  63. typename GT::NodeType, ptrdiff_t> super;
  64. typedef typename GT::NodeType NodeType;
  65. typedef typename GT::ChildIteratorType ChildItTy;
  66. typedef PointerIntPair<NodeType*, 1> PointerIntTy;
  67. // VisitStack - Used to maintain the ordering. Top = current block
  68. // First element is node pointer, second is the 'next child' to visit
  69. // if the int in PointerIntTy is 0, the 'next child' to visit is invalid
  70. std::vector<std::pair<PointerIntTy, ChildItTy> > VisitStack;
  71. private:
  72. inline df_iterator(NodeType *Node) {
  73. this->Visited.insert(Node);
  74. VisitStack.push_back(std::make_pair(PointerIntTy(Node, 0),
  75. GT::child_begin(Node)));
  76. }
  77. inline df_iterator() {
  78. // End is when stack is empty
  79. }
  80. inline df_iterator(NodeType *Node, SetType &S)
  81. : df_iterator_storage<SetType, ExtStorage>(S) {
  82. if (!S.count(Node)) {
  83. VisitStack.push_back(std::make_pair(PointerIntTy(Node, 0),
  84. GT::child_begin(Node)));
  85. this->Visited.insert(Node);
  86. }
  87. }
  88. inline df_iterator(SetType &S)
  89. : df_iterator_storage<SetType, ExtStorage>(S) {
  90. // End is when stack is empty
  91. }
  92. inline void toNext() {
  93. do {
  94. std::pair<PointerIntTy, ChildItTy> &Top = VisitStack.back();
  95. NodeType *Node = Top.first.getPointer();
  96. ChildItTy &It = Top.second;
  97. if (!Top.first.getInt()) {
  98. // now retrieve the real begin of the children before we dive in
  99. It = GT::child_begin(Node);
  100. Top.first.setInt(1);
  101. }
  102. while (It != GT::child_end(Node)) {
  103. NodeType *Next = *It++;
  104. // Has our next sibling been visited?
  105. if (Next && this->Visited.insert(Next).second) {
  106. // No, do it now.
  107. VisitStack.push_back(std::make_pair(PointerIntTy(Next, 0),
  108. GT::child_begin(Next)));
  109. return;
  110. }
  111. }
  112. // Oops, ran out of successors... go up a level on the stack.
  113. VisitStack.pop_back();
  114. } while (!VisitStack.empty());
  115. }
  116. public:
  117. typedef typename super::pointer pointer;
  118. // Provide static begin and end methods as our public "constructors"
  119. static df_iterator begin(const GraphT &G) {
  120. return df_iterator(GT::getEntryNode(G));
  121. }
  122. static df_iterator end(const GraphT &G) { return df_iterator(); }
  123. // Static begin and end methods as our public ctors for external iterators
  124. static df_iterator begin(const GraphT &G, SetType &S) {
  125. return df_iterator(GT::getEntryNode(G), S);
  126. }
  127. static df_iterator end(const GraphT &G, SetType &S) { return df_iterator(S); }
  128. bool operator==(const df_iterator &x) const {
  129. return VisitStack == x.VisitStack;
  130. }
  131. bool operator!=(const df_iterator &x) const { return !(*this == x); }
  132. pointer operator*() const { return VisitStack.back().first.getPointer(); }
  133. // This is a nonstandard operator-> that dereferences the pointer an extra
  134. // time... so that you can actually call methods ON the Node, because
  135. // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
  136. //
  137. NodeType *operator->() const { return **this; }
  138. df_iterator &operator++() { // Preincrement
  139. toNext();
  140. return *this;
  141. }
  142. /// \brief Skips all children of the current node and traverses to next node
  143. ///
  144. /// Note: This function takes care of incrementing the iterator. If you
  145. /// always increment and call this function, you risk walking off the end.
  146. df_iterator &skipChildren() {
  147. VisitStack.pop_back();
  148. if (!VisitStack.empty())
  149. toNext();
  150. return *this;
  151. }
  152. df_iterator operator++(int) { // Postincrement
  153. df_iterator tmp = *this;
  154. ++*this;
  155. return tmp;
  156. }
  157. // nodeVisited - return true if this iterator has already visited the
  158. // specified node. This is public, and will probably be used to iterate over
  159. // nodes that a depth first iteration did not find: ie unreachable nodes.
  160. //
  161. bool nodeVisited(NodeType *Node) const {
  162. return this->Visited.count(Node) != 0;
  163. }
  164. /// getPathLength - Return the length of the path from the entry node to the
  165. /// current node, counting both nodes.
  166. unsigned getPathLength() const { return VisitStack.size(); }
  167. /// getPath - Return the n'th node in the path from the entry node to the
  168. /// current node.
  169. NodeType *getPath(unsigned n) const {
  170. return VisitStack[n].first.getPointer();
  171. }
  172. };
  173. // Provide global constructors that automatically figure out correct types...
  174. //
  175. template <class T>
  176. df_iterator<T> df_begin(const T& G) {
  177. return df_iterator<T>::begin(G);
  178. }
  179. template <class T>
  180. df_iterator<T> df_end(const T& G) {
  181. return df_iterator<T>::end(G);
  182. }
  183. // Provide an accessor method to use them in range-based patterns.
  184. template <class T>
  185. iterator_range<df_iterator<T>> depth_first(const T& G) {
  186. return make_range(df_begin(G), df_end(G));
  187. }
  188. // Provide global definitions of external depth first iterators...
  189. template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
  190. struct df_ext_iterator : public df_iterator<T, SetTy, true> {
  191. df_ext_iterator(const df_iterator<T, SetTy, true> &V)
  192. : df_iterator<T, SetTy, true>(V) {}
  193. };
  194. template <class T, class SetTy>
  195. df_ext_iterator<T, SetTy> df_ext_begin(const T& G, SetTy &S) {
  196. return df_ext_iterator<T, SetTy>::begin(G, S);
  197. }
  198. template <class T, class SetTy>
  199. df_ext_iterator<T, SetTy> df_ext_end(const T& G, SetTy &S) {
  200. return df_ext_iterator<T, SetTy>::end(G, S);
  201. }
  202. template <class T, class SetTy>
  203. iterator_range<df_ext_iterator<T, SetTy>> depth_first_ext(const T& G,
  204. SetTy &S) {
  205. return make_range(df_ext_begin(G, S), df_ext_end(G, S));
  206. }
  207. // Provide global definitions of inverse depth first iterators...
  208. template <class T,
  209. class SetTy = llvm::SmallPtrSet<typename GraphTraits<T>::NodeType*, 8>,
  210. bool External = false>
  211. struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
  212. idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
  213. : df_iterator<Inverse<T>, SetTy, External>(V) {}
  214. };
  215. template <class T>
  216. idf_iterator<T> idf_begin(const T& G) {
  217. return idf_iterator<T>::begin(Inverse<T>(G));
  218. }
  219. template <class T>
  220. idf_iterator<T> idf_end(const T& G){
  221. return idf_iterator<T>::end(Inverse<T>(G));
  222. }
  223. // Provide an accessor method to use them in range-based patterns.
  224. template <class T>
  225. iterator_range<idf_iterator<T>> inverse_depth_first(const T& G) {
  226. return make_range(idf_begin(G), idf_end(G));
  227. }
  228. // Provide global definitions of external inverse depth first iterators...
  229. template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
  230. struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
  231. idf_ext_iterator(const idf_iterator<T, SetTy, true> &V)
  232. : idf_iterator<T, SetTy, true>(V) {}
  233. idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V)
  234. : idf_iterator<T, SetTy, true>(V) {}
  235. };
  236. template <class T, class SetTy>
  237. idf_ext_iterator<T, SetTy> idf_ext_begin(const T& G, SetTy &S) {
  238. return idf_ext_iterator<T, SetTy>::begin(Inverse<T>(G), S);
  239. }
  240. template <class T, class SetTy>
  241. idf_ext_iterator<T, SetTy> idf_ext_end(const T& G, SetTy &S) {
  242. return idf_ext_iterator<T, SetTy>::end(Inverse<T>(G), S);
  243. }
  244. template <class T, class SetTy>
  245. iterator_range<idf_ext_iterator<T, SetTy>> inverse_depth_first_ext(const T& G,
  246. SetTy &S) {
  247. return make_range(idf_ext_begin(G, S), idf_ext_end(G, S));
  248. }
  249. } // End llvm namespace
  250. #endif