PostOrderIterator.h 11 KB

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