PostOrderIterator.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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/SmallPtrSet.h"
  24. #include "llvm/ADT/SmallVector.h"
  25. #include "llvm/ADT/iterator_range.h"
  26. #include <iterator>
  27. #include <optional>
  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(std::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>
  81. bool insertEdge(std::optional<NodeRef> From, NodeRef To) {
  82. return Visited.insert(To).second;
  83. }
  84. // Called after all children of BB have been visited.
  85. template <class NodeRef> void finishPostorder(NodeRef BB) {}
  86. };
  87. template <class GraphT,
  88. class SetType = SmallPtrSet<typename GraphTraits<GraphT>::NodeRef, 8>,
  89. bool ExtStorage = false, class GT = GraphTraits<GraphT>>
  90. class po_iterator : public po_iterator_storage<SetType, ExtStorage> {
  91. public:
  92. using iterator_category = std::forward_iterator_tag;
  93. using value_type = typename GT::NodeRef;
  94. using difference_type = std::ptrdiff_t;
  95. using pointer = value_type *;
  96. using reference = value_type &;
  97. private:
  98. using NodeRef = typename GT::NodeRef;
  99. using ChildItTy = typename GT::ChildIteratorType;
  100. // VisitStack - Used to maintain the ordering. Top = current block
  101. // First element is basic block pointer, second is the 'next child' to visit
  102. SmallVector<std::pair<NodeRef, ChildItTy>, 8> VisitStack;
  103. po_iterator(NodeRef BB) {
  104. this->insertEdge(std::optional<NodeRef>(), BB);
  105. VisitStack.push_back(std::make_pair(BB, GT::child_begin(BB)));
  106. traverseChild();
  107. }
  108. po_iterator() = default; // End is when stack is empty.
  109. po_iterator(NodeRef BB, SetType &S)
  110. : po_iterator_storage<SetType, ExtStorage>(S) {
  111. if (this->insertEdge(std::optional<NodeRef>(), BB)) {
  112. VisitStack.push_back(std::make_pair(BB, GT::child_begin(BB)));
  113. traverseChild();
  114. }
  115. }
  116. po_iterator(SetType &S)
  117. : po_iterator_storage<SetType, ExtStorage>(S) {
  118. } // End is when stack is empty.
  119. void traverseChild() {
  120. while (VisitStack.back().second != GT::child_end(VisitStack.back().first)) {
  121. NodeRef BB = *VisitStack.back().second++;
  122. if (this->insertEdge(std::optional<NodeRef>(VisitStack.back().first),
  123. BB)) {
  124. // If the block is not visited...
  125. VisitStack.push_back(std::make_pair(BB, GT::child_begin(BB)));
  126. }
  127. }
  128. }
  129. public:
  130. // Provide static "constructors"...
  131. static po_iterator begin(const GraphT &G) {
  132. return po_iterator(GT::getEntryNode(G));
  133. }
  134. static po_iterator end(const GraphT &G) { return po_iterator(); }
  135. static po_iterator begin(const GraphT &G, SetType &S) {
  136. return po_iterator(GT::getEntryNode(G), S);
  137. }
  138. static po_iterator end(const GraphT &G, SetType &S) { return po_iterator(S); }
  139. bool operator==(const po_iterator &x) const {
  140. return VisitStack == x.VisitStack;
  141. }
  142. bool operator!=(const po_iterator &x) const { return !(*this == x); }
  143. const NodeRef &operator*() const { return VisitStack.back().first; }
  144. // This is a nonstandard operator-> that dereferences the pointer an extra
  145. // time... so that you can actually call methods ON the BasicBlock, because
  146. // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
  147. //
  148. NodeRef operator->() const { return **this; }
  149. po_iterator &operator++() { // Preincrement
  150. this->finishPostorder(VisitStack.back().first);
  151. VisitStack.pop_back();
  152. if (!VisitStack.empty())
  153. traverseChild();
  154. return *this;
  155. }
  156. po_iterator operator++(int) { // Postincrement
  157. po_iterator tmp = *this;
  158. ++*this;
  159. return tmp;
  160. }
  161. };
  162. // Provide global constructors that automatically figure out correct types...
  163. //
  164. template <class T>
  165. po_iterator<T> po_begin(const T &G) { return po_iterator<T>::begin(G); }
  166. template <class T>
  167. po_iterator<T> po_end (const T &G) { return po_iterator<T>::end(G); }
  168. template <class T> iterator_range<po_iterator<T>> post_order(const T &G) {
  169. return make_range(po_begin(G), po_end(G));
  170. }
  171. // Provide global definitions of external postorder iterators...
  172. template <class T, class SetType = std::set<typename GraphTraits<T>::NodeRef>>
  173. struct po_ext_iterator : public po_iterator<T, SetType, true> {
  174. po_ext_iterator(const po_iterator<T, SetType, true> &V) :
  175. po_iterator<T, SetType, true>(V) {}
  176. };
  177. template<class T, class SetType>
  178. po_ext_iterator<T, SetType> po_ext_begin(T G, SetType &S) {
  179. return po_ext_iterator<T, SetType>::begin(G, S);
  180. }
  181. template<class T, class SetType>
  182. po_ext_iterator<T, SetType> po_ext_end(T G, SetType &S) {
  183. return po_ext_iterator<T, SetType>::end(G, S);
  184. }
  185. template <class T, class SetType>
  186. iterator_range<po_ext_iterator<T, SetType>> post_order_ext(const T &G, SetType &S) {
  187. return make_range(po_ext_begin(G, S), po_ext_end(G, S));
  188. }
  189. // Provide global definitions of inverse post order iterators...
  190. template <class T, class SetType = std::set<typename GraphTraits<T>::NodeRef>,
  191. bool External = false>
  192. struct ipo_iterator : public po_iterator<Inverse<T>, SetType, External> {
  193. ipo_iterator(const po_iterator<Inverse<T>, SetType, External> &V) :
  194. po_iterator<Inverse<T>, SetType, External> (V) {}
  195. };
  196. template <class T>
  197. ipo_iterator<T> ipo_begin(const T &G) {
  198. return ipo_iterator<T>::begin(G);
  199. }
  200. template <class T>
  201. ipo_iterator<T> ipo_end(const T &G){
  202. return ipo_iterator<T>::end(G);
  203. }
  204. template <class T>
  205. iterator_range<ipo_iterator<T>> inverse_post_order(const T &G) {
  206. return make_range(ipo_begin(G), ipo_end(G));
  207. }
  208. // Provide global definitions of external inverse postorder iterators...
  209. template <class T, class SetType = std::set<typename GraphTraits<T>::NodeRef>>
  210. struct ipo_ext_iterator : public ipo_iterator<T, SetType, true> {
  211. ipo_ext_iterator(const ipo_iterator<T, SetType, true> &V) :
  212. ipo_iterator<T, SetType, true>(V) {}
  213. ipo_ext_iterator(const po_iterator<Inverse<T>, SetType, true> &V) :
  214. ipo_iterator<T, SetType, true>(V) {}
  215. };
  216. template <class T, class SetType>
  217. ipo_ext_iterator<T, SetType> ipo_ext_begin(const T &G, SetType &S) {
  218. return ipo_ext_iterator<T, SetType>::begin(G, S);
  219. }
  220. template <class T, class SetType>
  221. ipo_ext_iterator<T, SetType> ipo_ext_end(const T &G, SetType &S) {
  222. return ipo_ext_iterator<T, SetType>::end(G, S);
  223. }
  224. template <class T, class SetType>
  225. iterator_range<ipo_ext_iterator<T, SetType>>
  226. inverse_post_order_ext(const T &G, SetType &S) {
  227. return make_range(ipo_ext_begin(G, S), ipo_ext_end(G, S));
  228. }
  229. //===--------------------------------------------------------------------===//
  230. // Reverse Post Order CFG iterator code
  231. //===--------------------------------------------------------------------===//
  232. //
  233. // This is used to visit basic blocks in a method in reverse post order. This
  234. // class is awkward to use because I don't know a good incremental algorithm to
  235. // computer RPO from a graph. Because of this, the construction of the
  236. // ReversePostOrderTraversal object is expensive (it must walk the entire graph
  237. // with a postorder iterator to build the data structures). The moral of this
  238. // story is: Don't create more ReversePostOrderTraversal classes than necessary.
  239. //
  240. // Because it does the traversal in its constructor, it won't invalidate when
  241. // BasicBlocks are removed, *but* it may contain erased blocks. Some places
  242. // rely on this behavior (i.e. GVN).
  243. //
  244. // This class should be used like this:
  245. // {
  246. // ReversePostOrderTraversal<Function*> RPOT(FuncPtr); // Expensive to create
  247. // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
  248. // ...
  249. // }
  250. // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
  251. // ...
  252. // }
  253. // }
  254. //
  255. template<class GraphT, class GT = GraphTraits<GraphT>>
  256. class ReversePostOrderTraversal {
  257. using NodeRef = typename GT::NodeRef;
  258. std::vector<NodeRef> Blocks; // Block list in normal PO order
  259. void Initialize(const GraphT &G) {
  260. std::copy(po_begin(G), po_end(G), std::back_inserter(Blocks));
  261. }
  262. public:
  263. using rpo_iterator = typename std::vector<NodeRef>::reverse_iterator;
  264. using const_rpo_iterator = typename std::vector<NodeRef>::const_reverse_iterator;
  265. ReversePostOrderTraversal(const GraphT &G) { Initialize(G); }
  266. // Because we want a reverse post order, use reverse iterators from the vector
  267. rpo_iterator begin() { return Blocks.rbegin(); }
  268. const_rpo_iterator begin() const { return Blocks.crbegin(); }
  269. rpo_iterator end() { return Blocks.rend(); }
  270. const_rpo_iterator end() const { return Blocks.crend(); }
  271. };
  272. } // end namespace llvm
  273. #endif // LLVM_ADT_POSTORDERITERATOR_H
  274. #ifdef __GNUC__
  275. #pragma GCC diagnostic pop
  276. #endif