PostOrderIterator.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //===- llvm/ADT/PostOrderIterator.h - PostOrder 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 a generic graph
  11. // post order iterator. This should work over any graph type that has a
  12. // GraphTraits specialization.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_ADT_POSTORDERITERATOR_H
  16. #define LLVM_ADT_POSTORDERITERATOR_H
  17. #include "llvm/ADT/GraphTraits.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/ADT/iterator_range.h"
  20. #include <set>
  21. #include <vector>
  22. namespace llvm {
  23. // The po_iterator_storage template provides access to the set of already
  24. // visited nodes during the po_iterator's depth-first traversal.
  25. //
  26. // The default implementation simply contains a set of visited nodes, while
  27. // the Extended=true version uses a reference to an external set.
  28. //
  29. // It is possible to prune the depth-first traversal in several ways:
  30. //
  31. // - When providing an external set that already contains some graph nodes,
  32. // those nodes won't be visited again. This is useful for restarting a
  33. // post-order traversal on a graph with nodes that aren't dominated by a
  34. // single node.
  35. //
  36. // - By providing a custom SetType class, unwanted graph nodes can be excluded
  37. // by having the insert() function return false. This could for example
  38. // confine a CFG traversal to blocks in a specific loop.
  39. //
  40. // - Finally, by specializing the po_iterator_storage template itself, graph
  41. // edges can be pruned by returning false in the insertEdge() function. This
  42. // could be used to remove loop back-edges from the CFG seen by po_iterator.
  43. //
  44. // A specialized po_iterator_storage class can observe both the pre-order and
  45. // the post-order. The insertEdge() function is called in a pre-order, while
  46. // the finishPostorder() function is called just before the po_iterator moves
  47. // on to the next node.
  48. /// Default po_iterator_storage implementation with an internal set object.
  49. template<class SetType, bool External>
  50. class po_iterator_storage {
  51. SetType Visited;
  52. public:
  53. // Return true if edge destination should be visited.
  54. template<typename NodeType>
  55. bool insertEdge(NodeType *From, NodeType *To) {
  56. return Visited.insert(To).second;
  57. }
  58. // Called after all children of BB have been visited.
  59. template<typename NodeType>
  60. void finishPostorder(NodeType *BB) {}
  61. };
  62. /// Specialization of po_iterator_storage that references an external set.
  63. template<class SetType>
  64. class po_iterator_storage<SetType, true> {
  65. SetType &Visited;
  66. public:
  67. po_iterator_storage(SetType &VSet) : Visited(VSet) {}
  68. po_iterator_storage(const po_iterator_storage &S) : Visited(S.Visited) {}
  69. // Return true if edge destination should be visited, called with From = 0 for
  70. // the root node.
  71. // Graph edges can be pruned by specializing this function.
  72. template <class NodeType> bool insertEdge(NodeType *From, NodeType *To) {
  73. return Visited.insert(To).second;
  74. }
  75. // Called after all children of BB have been visited.
  76. template<class NodeType>
  77. void finishPostorder(NodeType *BB) {}
  78. };
  79. template<class GraphT,
  80. class SetType = llvm::SmallPtrSet<typename GraphTraits<GraphT>::NodeType*, 8>,
  81. bool ExtStorage = false,
  82. class GT = GraphTraits<GraphT> >
  83. class po_iterator : public std::iterator<std::forward_iterator_tag,
  84. typename GT::NodeType, ptrdiff_t>,
  85. public po_iterator_storage<SetType, ExtStorage> {
  86. typedef std::iterator<std::forward_iterator_tag,
  87. typename GT::NodeType, ptrdiff_t> super;
  88. typedef typename GT::NodeType NodeType;
  89. typedef typename GT::ChildIteratorType ChildItTy;
  90. // VisitStack - Used to maintain the ordering. Top = current block
  91. // First element is basic block pointer, second is the 'next child' to visit
  92. std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
  93. void traverseChild() {
  94. while (VisitStack.back().second != GT::child_end(VisitStack.back().first)) {
  95. NodeType *BB = *VisitStack.back().second++;
  96. if (this->insertEdge(VisitStack.back().first, BB)) {
  97. // If the block is not visited...
  98. VisitStack.push_back(std::make_pair(BB, GT::child_begin(BB)));
  99. }
  100. }
  101. }
  102. po_iterator(NodeType *BB) {
  103. this->insertEdge((NodeType*)nullptr, BB);
  104. VisitStack.push_back(std::make_pair(BB, GT::child_begin(BB)));
  105. traverseChild();
  106. }
  107. po_iterator() {} // End is when stack is empty.
  108. po_iterator(NodeType *BB, SetType &S)
  109. : po_iterator_storage<SetType, ExtStorage>(S) {
  110. if (this->insertEdge((NodeType*)nullptr, BB)) {
  111. VisitStack.push_back(std::make_pair(BB, GT::child_begin(BB)));
  112. traverseChild();
  113. }
  114. }
  115. po_iterator(SetType &S)
  116. : po_iterator_storage<SetType, ExtStorage>(S) {
  117. } // End is when stack is empty.
  118. public:
  119. typedef typename super::pointer pointer;
  120. // Provide static "constructors"...
  121. static po_iterator begin(GraphT G) {
  122. return po_iterator(GT::getEntryNode(G));
  123. }
  124. static po_iterator end(GraphT G) { return po_iterator(); }
  125. static po_iterator begin(GraphT G, SetType &S) {
  126. return po_iterator(GT::getEntryNode(G), S);
  127. }
  128. static po_iterator end(GraphT G, SetType &S) { return po_iterator(S); }
  129. bool operator==(const po_iterator &x) const {
  130. return VisitStack == x.VisitStack;
  131. }
  132. bool operator!=(const po_iterator &x) const { return !(*this == x); }
  133. pointer operator*() const { return VisitStack.back().first; }
  134. // This is a nonstandard operator-> that dereferences the pointer an extra
  135. // time... so that you can actually call methods ON the BasicBlock, because
  136. // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
  137. //
  138. NodeType *operator->() const { return **this; }
  139. po_iterator &operator++() { // Preincrement
  140. this->finishPostorder(VisitStack.back().first);
  141. VisitStack.pop_back();
  142. if (!VisitStack.empty())
  143. traverseChild();
  144. return *this;
  145. }
  146. po_iterator operator++(int) { // Postincrement
  147. po_iterator tmp = *this;
  148. ++*this;
  149. return tmp;
  150. }
  151. };
  152. // Provide global constructors that automatically figure out correct types...
  153. //
  154. template <class T>
  155. po_iterator<T> po_begin(const T &G) { return po_iterator<T>::begin(G); }
  156. template <class T>
  157. po_iterator<T> po_end (const T &G) { return po_iterator<T>::end(G); }
  158. template <class T> iterator_range<po_iterator<T>> post_order(const T &G) {
  159. return make_range(po_begin(G), po_end(G));
  160. }
  161. // Provide global definitions of external postorder iterators...
  162. template<class T, class SetType=std::set<typename GraphTraits<T>::NodeType*> >
  163. struct po_ext_iterator : public po_iterator<T, SetType, true> {
  164. po_ext_iterator(const po_iterator<T, SetType, true> &V) :
  165. po_iterator<T, SetType, true>(V) {}
  166. };
  167. template<class T, class SetType>
  168. po_ext_iterator<T, SetType> po_ext_begin(T G, SetType &S) {
  169. return po_ext_iterator<T, SetType>::begin(G, S);
  170. }
  171. template<class T, class SetType>
  172. po_ext_iterator<T, SetType> po_ext_end(T G, SetType &S) {
  173. return po_ext_iterator<T, SetType>::end(G, S);
  174. }
  175. template <class T, class SetType>
  176. iterator_range<po_ext_iterator<T, SetType>> post_order_ext(const T &G, SetType &S) {
  177. return make_range(po_ext_begin(G, S), po_ext_end(G, S));
  178. }
  179. // Provide global definitions of inverse post order iterators...
  180. template <class T,
  181. class SetType = std::set<typename GraphTraits<T>::NodeType*>,
  182. bool External = false>
  183. struct ipo_iterator : public po_iterator<Inverse<T>, SetType, External > {
  184. ipo_iterator(const po_iterator<Inverse<T>, SetType, External> &V) :
  185. po_iterator<Inverse<T>, SetType, External> (V) {}
  186. };
  187. template <class T>
  188. ipo_iterator<T> ipo_begin(const T &G, bool Reverse = false) {
  189. return ipo_iterator<T>::begin(G, Reverse);
  190. }
  191. template <class T>
  192. ipo_iterator<T> ipo_end(const T &G){
  193. return ipo_iterator<T>::end(G);
  194. }
  195. template <class T>
  196. iterator_range<ipo_iterator<T>> inverse_post_order(const T &G, bool Reverse = false) {
  197. return make_range(ipo_begin(G, Reverse), ipo_end(G));
  198. }
  199. // Provide global definitions of external inverse postorder iterators...
  200. template <class T,
  201. class SetType = std::set<typename GraphTraits<T>::NodeType*> >
  202. struct ipo_ext_iterator : public ipo_iterator<T, SetType, true> {
  203. ipo_ext_iterator(const ipo_iterator<T, SetType, true> &V) :
  204. ipo_iterator<T, SetType, true>(V) {}
  205. ipo_ext_iterator(const po_iterator<Inverse<T>, SetType, true> &V) :
  206. ipo_iterator<T, SetType, true>(V) {}
  207. };
  208. template <class T, class SetType>
  209. ipo_ext_iterator<T, SetType> ipo_ext_begin(const T &G, SetType &S) {
  210. return ipo_ext_iterator<T, SetType>::begin(G, S);
  211. }
  212. template <class T, class SetType>
  213. ipo_ext_iterator<T, SetType> ipo_ext_end(const T &G, SetType &S) {
  214. return ipo_ext_iterator<T, SetType>::end(G, S);
  215. }
  216. template <class T, class SetType>
  217. iterator_range<ipo_ext_iterator<T, SetType>>
  218. inverse_post_order_ext(const T &G, SetType &S) {
  219. return make_range(ipo_ext_begin(G, S), ipo_ext_end(G, S));
  220. }
  221. //===--------------------------------------------------------------------===//
  222. // Reverse Post Order CFG iterator code
  223. //===--------------------------------------------------------------------===//
  224. //
  225. // This is used to visit basic blocks in a method in reverse post order. This
  226. // class is awkward to use because I don't know a good incremental algorithm to
  227. // computer RPO from a graph. Because of this, the construction of the
  228. // ReversePostOrderTraversal object is expensive (it must walk the entire graph
  229. // with a postorder iterator to build the data structures). The moral of this
  230. // story is: Don't create more ReversePostOrderTraversal classes than necessary.
  231. //
  232. // This class should be used like this:
  233. // {
  234. // ReversePostOrderTraversal<Function*> RPOT(FuncPtr); // Expensive to create
  235. // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
  236. // ...
  237. // }
  238. // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
  239. // ...
  240. // }
  241. // }
  242. //
  243. template<class GraphT, class GT = GraphTraits<GraphT> >
  244. class ReversePostOrderTraversal {
  245. typedef typename GT::NodeType NodeType;
  246. std::vector<NodeType*> Blocks; // Block list in normal PO order
  247. void Initialize(NodeType *BB) {
  248. std::copy(po_begin(BB), po_end(BB), std::back_inserter(Blocks));
  249. }
  250. public:
  251. typedef typename std::vector<NodeType*>::reverse_iterator rpo_iterator;
  252. ReversePostOrderTraversal(GraphT G) { Initialize(GT::getEntryNode(G)); }
  253. // Because we want a reverse post order, use reverse iterators from the vector
  254. rpo_iterator begin() { return Blocks.rbegin(); }
  255. rpo_iterator end() { return Blocks.rend(); }
  256. };
  257. } // End llvm namespace
  258. #endif