RegionIterator.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //===- RegionIterator.h - Iterators to iteratate over Regions ---*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // This file defines the iterators to iterate over the elements of a Region.
  9. //===----------------------------------------------------------------------===//
  10. #ifndef LLVM_ANALYSIS_REGIONITERATOR_H
  11. #define LLVM_ANALYSIS_REGIONITERATOR_H
  12. #include "llvm/ADT/DepthFirstIterator.h"
  13. #include "llvm/ADT/GraphTraits.h"
  14. #include "llvm/ADT/PointerIntPair.h"
  15. #include "llvm/Analysis/RegionInfo.h"
  16. #include "llvm/IR/CFG.h"
  17. #include <cassert>
  18. #include <iterator>
  19. #include <type_traits>
  20. namespace llvm {
  21. class BasicBlock;
  22. //===----------------------------------------------------------------------===//
  23. /// Hierarchical RegionNode successor iterator.
  24. ///
  25. /// This iterator iterates over all successors of a RegionNode.
  26. ///
  27. /// For a BasicBlock RegionNode it skips all BasicBlocks that are not part of
  28. /// the parent Region. Furthermore for BasicBlocks that start a subregion, a
  29. /// RegionNode representing the subregion is returned.
  30. ///
  31. /// For a subregion RegionNode there is just one successor. The RegionNode
  32. /// representing the exit of the subregion.
  33. template <class NodeRef, class BlockT, class RegionT> class RNSuccIterator {
  34. public:
  35. using iterator_category = std::forward_iterator_tag;
  36. using value_type = NodeRef;
  37. using difference_type = std::ptrdiff_t;
  38. using pointer = value_type *;
  39. using reference = value_type &;
  40. private:
  41. using BlockTraits = GraphTraits<BlockT *>;
  42. using SuccIterTy = typename BlockTraits::ChildIteratorType;
  43. // The iterator works in two modes, bb mode or region mode.
  44. enum ItMode {
  45. // In BB mode it returns all successors of this BasicBlock as its
  46. // successors.
  47. ItBB,
  48. // In region mode there is only one successor, thats the regionnode mapping
  49. // to the exit block of the regionnode
  50. ItRgBegin, // At the beginning of the regionnode successor.
  51. ItRgEnd // At the end of the regionnode successor.
  52. };
  53. static_assert(std::is_pointer<NodeRef>::value,
  54. "FIXME: Currently RNSuccIterator only supports NodeRef as "
  55. "pointers due to the use of pointer-specific data structures "
  56. "(e.g. PointerIntPair and SmallPtrSet) internally. Generalize "
  57. "it to support non-pointer types");
  58. // Use two bit to represent the mode iterator.
  59. PointerIntPair<NodeRef, 2, ItMode> Node;
  60. // The block successor iterator.
  61. SuccIterTy BItor;
  62. // advanceRegionSucc - A region node has only one successor. It reaches end
  63. // once we advance it.
  64. void advanceRegionSucc() {
  65. assert(Node.getInt() == ItRgBegin && "Cannot advance region successor!");
  66. Node.setInt(ItRgEnd);
  67. }
  68. NodeRef getNode() const { return Node.getPointer(); }
  69. // isRegionMode - Is the current iterator in region mode?
  70. bool isRegionMode() const { return Node.getInt() != ItBB; }
  71. // Get the immediate successor. This function may return a Basic Block
  72. // RegionNode or a subregion RegionNode.
  73. NodeRef getISucc(BlockT *BB) const {
  74. NodeRef succ;
  75. succ = getNode()->getParent()->getNode(BB);
  76. assert(succ && "BB not in Region or entered subregion!");
  77. return succ;
  78. }
  79. // getRegionSucc - Return the successor basic block of a SubRegion RegionNode.
  80. inline BlockT* getRegionSucc() const {
  81. assert(Node.getInt() == ItRgBegin && "Cannot get the region successor!");
  82. return getNode()->template getNodeAs<RegionT>()->getExit();
  83. }
  84. // isExit - Is this the exit BB of the Region?
  85. inline bool isExit(BlockT* BB) const {
  86. return getNode()->getParent()->getExit() == BB;
  87. }
  88. public:
  89. using Self = RNSuccIterator<NodeRef, BlockT, RegionT>;
  90. /// Create begin iterator of a RegionNode.
  91. inline RNSuccIterator(NodeRef node)
  92. : Node(node, node->isSubRegion() ? ItRgBegin : ItBB),
  93. BItor(BlockTraits::child_begin(node->getEntry())) {
  94. // Skip the exit block
  95. if (!isRegionMode())
  96. while (BlockTraits::child_end(node->getEntry()) != BItor && isExit(*BItor))
  97. ++BItor;
  98. if (isRegionMode() && isExit(getRegionSucc()))
  99. advanceRegionSucc();
  100. }
  101. /// Create an end iterator.
  102. inline RNSuccIterator(NodeRef node, bool)
  103. : Node(node, node->isSubRegion() ? ItRgEnd : ItBB),
  104. BItor(BlockTraits::child_end(node->getEntry())) {}
  105. inline bool operator==(const Self& x) const {
  106. assert(isRegionMode() == x.isRegionMode() && "Broken iterator!");
  107. if (isRegionMode())
  108. return Node.getInt() == x.Node.getInt();
  109. else
  110. return BItor == x.BItor;
  111. }
  112. inline bool operator!=(const Self& x) const { return !operator==(x); }
  113. inline value_type operator*() const {
  114. BlockT *BB = isRegionMode() ? getRegionSucc() : *BItor;
  115. assert(!isExit(BB) && "Iterator out of range!");
  116. return getISucc(BB);
  117. }
  118. inline Self& operator++() {
  119. if(isRegionMode()) {
  120. // The Region only has 1 successor.
  121. advanceRegionSucc();
  122. } else {
  123. // Skip the exit.
  124. do
  125. ++BItor;
  126. while (BItor != BlockTraits::child_end(getNode()->getEntry())
  127. && isExit(*BItor));
  128. }
  129. return *this;
  130. }
  131. inline Self operator++(int) {
  132. Self tmp = *this;
  133. ++*this;
  134. return tmp;
  135. }
  136. };
  137. //===----------------------------------------------------------------------===//
  138. /// Flat RegionNode iterator.
  139. ///
  140. /// The Flat Region iterator will iterate over all BasicBlock RegionNodes that
  141. /// are contained in the Region and its subregions. This is close to a virtual
  142. /// control flow graph of the Region.
  143. template <class NodeRef, class BlockT, class RegionT>
  144. class RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT> {
  145. using BlockTraits = GraphTraits<BlockT *>;
  146. using SuccIterTy = typename BlockTraits::ChildIteratorType;
  147. NodeRef Node;
  148. SuccIterTy Itor;
  149. public:
  150. using iterator_category = std::forward_iterator_tag;
  151. using value_type = NodeRef;
  152. using difference_type = std::ptrdiff_t;
  153. using pointer = value_type *;
  154. using reference = value_type &;
  155. using Self = RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>;
  156. /// Create the iterator from a RegionNode.
  157. ///
  158. /// Note that the incoming node must be a bb node, otherwise it will trigger
  159. /// an assertion when we try to get a BasicBlock.
  160. inline RNSuccIterator(NodeRef node)
  161. : Node(node), Itor(BlockTraits::child_begin(node->getEntry())) {
  162. assert(!Node->isSubRegion() &&
  163. "Subregion node not allowed in flat iterating mode!");
  164. assert(Node->getParent() && "A BB node must have a parent!");
  165. // Skip the exit block of the iterating region.
  166. while (BlockTraits::child_end(Node->getEntry()) != Itor &&
  167. Node->getParent()->getExit() == *Itor)
  168. ++Itor;
  169. }
  170. /// Create an end iterator
  171. inline RNSuccIterator(NodeRef node, bool)
  172. : Node(node), Itor(BlockTraits::child_end(node->getEntry())) {
  173. assert(!Node->isSubRegion() &&
  174. "Subregion node not allowed in flat iterating mode!");
  175. }
  176. inline bool operator==(const Self& x) const {
  177. assert(Node->getParent() == x.Node->getParent()
  178. && "Cannot compare iterators of different regions!");
  179. return Itor == x.Itor && Node == x.Node;
  180. }
  181. inline bool operator!=(const Self& x) const { return !operator==(x); }
  182. inline value_type operator*() const {
  183. BlockT *BB = *Itor;
  184. // Get the iterating region.
  185. RegionT *Parent = Node->getParent();
  186. // The only case that the successor reaches out of the region is it reaches
  187. // the exit of the region.
  188. assert(Parent->getExit() != BB && "iterator out of range!");
  189. return Parent->getBBNode(BB);
  190. }
  191. inline Self& operator++() {
  192. // Skip the exit block of the iterating region.
  193. do
  194. ++Itor;
  195. while (Itor != succ_end(Node->getEntry())
  196. && Node->getParent()->getExit() == *Itor);
  197. return *this;
  198. }
  199. inline Self operator++(int) {
  200. Self tmp = *this;
  201. ++*this;
  202. return tmp;
  203. }
  204. };
  205. template <class NodeRef, class BlockT, class RegionT>
  206. inline RNSuccIterator<NodeRef, BlockT, RegionT> succ_begin(NodeRef Node) {
  207. return RNSuccIterator<NodeRef, BlockT, RegionT>(Node);
  208. }
  209. template <class NodeRef, class BlockT, class RegionT>
  210. inline RNSuccIterator<NodeRef, BlockT, RegionT> succ_end(NodeRef Node) {
  211. return RNSuccIterator<NodeRef, BlockT, RegionT>(Node, true);
  212. }
  213. //===--------------------------------------------------------------------===//
  214. // RegionNode GraphTraits specialization so the bbs in the region can be
  215. // iterate by generic graph iterators.
  216. //
  217. // NodeT can either be region node or const region node, otherwise child_begin
  218. // and child_end fail.
  219. #define RegionNodeGraphTraits(NodeT, BlockT, RegionT) \
  220. template <> struct GraphTraits<NodeT *> { \
  221. using NodeRef = NodeT *; \
  222. using ChildIteratorType = RNSuccIterator<NodeRef, BlockT, RegionT>; \
  223. static NodeRef getEntryNode(NodeRef N) { return N; } \
  224. static inline ChildIteratorType child_begin(NodeRef N) { \
  225. return RNSuccIterator<NodeRef, BlockT, RegionT>(N); \
  226. } \
  227. static inline ChildIteratorType child_end(NodeRef N) { \
  228. return RNSuccIterator<NodeRef, BlockT, RegionT>(N, true); \
  229. } \
  230. }; \
  231. template <> struct GraphTraits<FlatIt<NodeT *>> { \
  232. using NodeRef = NodeT *; \
  233. using ChildIteratorType = \
  234. RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>; \
  235. static NodeRef getEntryNode(NodeRef N) { return N; } \
  236. static inline ChildIteratorType child_begin(NodeRef N) { \
  237. return RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>(N); \
  238. } \
  239. static inline ChildIteratorType child_end(NodeRef N) { \
  240. return RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>(N, true); \
  241. } \
  242. }
  243. #define RegionGraphTraits(RegionT, NodeT) \
  244. template <> struct GraphTraits<RegionT *> : public GraphTraits<NodeT *> { \
  245. using nodes_iterator = df_iterator<NodeRef>; \
  246. static NodeRef getEntryNode(RegionT *R) { \
  247. return R->getNode(R->getEntry()); \
  248. } \
  249. static nodes_iterator nodes_begin(RegionT *R) { \
  250. return nodes_iterator::begin(getEntryNode(R)); \
  251. } \
  252. static nodes_iterator nodes_end(RegionT *R) { \
  253. return nodes_iterator::end(getEntryNode(R)); \
  254. } \
  255. }; \
  256. template <> \
  257. struct GraphTraits<FlatIt<RegionT *>> \
  258. : public GraphTraits<FlatIt<NodeT *>> { \
  259. using nodes_iterator = \
  260. df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false, \
  261. GraphTraits<FlatIt<NodeRef>>>; \
  262. static NodeRef getEntryNode(RegionT *R) { \
  263. return R->getBBNode(R->getEntry()); \
  264. } \
  265. static nodes_iterator nodes_begin(RegionT *R) { \
  266. return nodes_iterator::begin(getEntryNode(R)); \
  267. } \
  268. static nodes_iterator nodes_end(RegionT *R) { \
  269. return nodes_iterator::end(getEntryNode(R)); \
  270. } \
  271. }
  272. RegionNodeGraphTraits(RegionNode, BasicBlock, Region);
  273. RegionNodeGraphTraits(const RegionNode, BasicBlock, Region);
  274. RegionGraphTraits(Region, RegionNode);
  275. RegionGraphTraits(const Region, const RegionNode);
  276. template <> struct GraphTraits<RegionInfo*>
  277. : public GraphTraits<FlatIt<RegionNode*>> {
  278. using nodes_iterator =
  279. df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false,
  280. GraphTraits<FlatIt<NodeRef>>>;
  281. static NodeRef getEntryNode(RegionInfo *RI) {
  282. return GraphTraits<FlatIt<Region*>>::getEntryNode(RI->getTopLevelRegion());
  283. }
  284. static nodes_iterator nodes_begin(RegionInfo* RI) {
  285. return nodes_iterator::begin(getEntryNode(RI));
  286. }
  287. static nodes_iterator nodes_end(RegionInfo *RI) {
  288. return nodes_iterator::end(getEntryNode(RI));
  289. }
  290. };
  291. template <> struct GraphTraits<RegionInfoPass*>
  292. : public GraphTraits<RegionInfo *> {
  293. using nodes_iterator =
  294. df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false,
  295. GraphTraits<FlatIt<NodeRef>>>;
  296. static NodeRef getEntryNode(RegionInfoPass *RI) {
  297. return GraphTraits<RegionInfo*>::getEntryNode(&RI->getRegionInfo());
  298. }
  299. static nodes_iterator nodes_begin(RegionInfoPass* RI) {
  300. return GraphTraits<RegionInfo*>::nodes_begin(&RI->getRegionInfo());
  301. }
  302. static nodes_iterator nodes_end(RegionInfoPass *RI) {
  303. return GraphTraits<RegionInfo*>::nodes_end(&RI->getRegionInfo());
  304. }
  305. };
  306. } // end namespace llvm
  307. #endif // LLVM_ANALYSIS_REGIONITERATOR_H