RegionIterator.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- RegionIterator.h - Iterators to iteratate over Regions ---*- 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. // This file defines the iterators to iterate over the elements of a Region.
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_ANALYSIS_REGIONITERATOR_H
  16. #define LLVM_ANALYSIS_REGIONITERATOR_H
  17. #include "llvm/ADT/DepthFirstIterator.h"
  18. #include "llvm/ADT/GraphTraits.h"
  19. #include "llvm/ADT/PointerIntPair.h"
  20. #include "llvm/Analysis/RegionInfo.h"
  21. #include "llvm/IR/CFG.h"
  22. #include <cassert>
  23. #include <iterator>
  24. #include <type_traits>
  25. namespace llvm {
  26. class BasicBlock;
  27. //===----------------------------------------------------------------------===//
  28. /// Hierarchical RegionNode successor iterator.
  29. ///
  30. /// This iterator iterates over all successors of a RegionNode.
  31. ///
  32. /// For a BasicBlock RegionNode it skips all BasicBlocks that are not part of
  33. /// the parent Region. Furthermore for BasicBlocks that start a subregion, a
  34. /// RegionNode representing the subregion is returned.
  35. ///
  36. /// For a subregion RegionNode there is just one successor. The RegionNode
  37. /// representing the exit of the subregion.
  38. template <class NodeRef, class BlockT, class RegionT>
  39. class RNSuccIterator
  40. : public std::iterator<std::forward_iterator_tag, NodeRef> {
  41. using super = std::iterator<std::forward_iterator_tag, NodeRef>;
  42. using BlockTraits = GraphTraits<BlockT *>;
  43. using SuccIterTy = typename BlockTraits::ChildIteratorType;
  44. // The iterator works in two modes, bb mode or region mode.
  45. enum ItMode {
  46. // In BB mode it returns all successors of this BasicBlock as its
  47. // successors.
  48. ItBB,
  49. // In region mode there is only one successor, thats the regionnode mapping
  50. // to the exit block of the regionnode
  51. ItRgBegin, // At the beginning of the regionnode successor.
  52. ItRgEnd // At the end of the regionnode successor.
  53. };
  54. static_assert(std::is_pointer<NodeRef>::value,
  55. "FIXME: Currently RNSuccIterator only supports NodeRef as "
  56. "pointers due to the use of pointer-specific data structures "
  57. "(e.g. PointerIntPair and SmallPtrSet) internally. Generalize "
  58. "it to support non-pointer types");
  59. // Use two bit to represent the mode iterator.
  60. PointerIntPair<NodeRef, 2, ItMode> Node;
  61. // The block successor iterator.
  62. SuccIterTy BItor;
  63. // advanceRegionSucc - A region node has only one successor. It reaches end
  64. // once we advance it.
  65. void advanceRegionSucc() {
  66. assert(Node.getInt() == ItRgBegin && "Cannot advance region successor!");
  67. Node.setInt(ItRgEnd);
  68. }
  69. NodeRef getNode() const { return Node.getPointer(); }
  70. // isRegionMode - Is the current iterator in region mode?
  71. bool isRegionMode() const { return Node.getInt() != ItBB; }
  72. // Get the immediate successor. This function may return a Basic Block
  73. // RegionNode or a subregion RegionNode.
  74. NodeRef getISucc(BlockT *BB) const {
  75. NodeRef succ;
  76. succ = getNode()->getParent()->getNode(BB);
  77. assert(succ && "BB not in Region or entered subregion!");
  78. return succ;
  79. }
  80. // getRegionSucc - Return the successor basic block of a SubRegion RegionNode.
  81. inline BlockT* getRegionSucc() const {
  82. assert(Node.getInt() == ItRgBegin && "Cannot get the region successor!");
  83. return getNode()->template getNodeAs<RegionT>()->getExit();
  84. }
  85. // isExit - Is this the exit BB of the Region?
  86. inline bool isExit(BlockT* BB) const {
  87. return getNode()->getParent()->getExit() == BB;
  88. }
  89. public:
  90. using Self = RNSuccIterator<NodeRef, BlockT, RegionT>;
  91. using value_type = typename super::value_type;
  92. /// Create begin iterator of a RegionNode.
  93. inline RNSuccIterator(NodeRef node)
  94. : Node(node, node->isSubRegion() ? ItRgBegin : ItBB),
  95. BItor(BlockTraits::child_begin(node->getEntry())) {
  96. // Skip the exit block
  97. if (!isRegionMode())
  98. while (BlockTraits::child_end(node->getEntry()) != BItor && isExit(*BItor))
  99. ++BItor;
  100. if (isRegionMode() && isExit(getRegionSucc()))
  101. advanceRegionSucc();
  102. }
  103. /// Create an end iterator.
  104. inline RNSuccIterator(NodeRef node, bool)
  105. : Node(node, node->isSubRegion() ? ItRgEnd : ItBB),
  106. BItor(BlockTraits::child_end(node->getEntry())) {}
  107. inline bool operator==(const Self& x) const {
  108. assert(isRegionMode() == x.isRegionMode() && "Broken iterator!");
  109. if (isRegionMode())
  110. return Node.getInt() == x.Node.getInt();
  111. else
  112. return BItor == x.BItor;
  113. }
  114. inline bool operator!=(const Self& x) const { return !operator==(x); }
  115. inline value_type operator*() const {
  116. BlockT *BB = isRegionMode() ? getRegionSucc() : *BItor;
  117. assert(!isExit(BB) && "Iterator out of range!");
  118. return getISucc(BB);
  119. }
  120. inline Self& operator++() {
  121. if(isRegionMode()) {
  122. // The Region only has 1 successor.
  123. advanceRegionSucc();
  124. } else {
  125. // Skip the exit.
  126. do
  127. ++BItor;
  128. while (BItor != BlockTraits::child_end(getNode()->getEntry())
  129. && isExit(*BItor));
  130. }
  131. return *this;
  132. }
  133. inline Self operator++(int) {
  134. Self tmp = *this;
  135. ++*this;
  136. return tmp;
  137. }
  138. };
  139. //===----------------------------------------------------------------------===//
  140. /// Flat RegionNode iterator.
  141. ///
  142. /// The Flat Region iterator will iterate over all BasicBlock RegionNodes that
  143. /// are contained in the Region and its subregions. This is close to a virtual
  144. /// control flow graph of the Region.
  145. template <class NodeRef, class BlockT, class RegionT>
  146. class RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>
  147. : public std::iterator<std::forward_iterator_tag, NodeRef> {
  148. using super = std::iterator<std::forward_iterator_tag, NodeRef>;
  149. using BlockTraits = GraphTraits<BlockT *>;
  150. using SuccIterTy = typename BlockTraits::ChildIteratorType;
  151. NodeRef Node;
  152. SuccIterTy Itor;
  153. public:
  154. using Self = RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>;
  155. using value_type = typename super::value_type;
  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
  308. #ifdef __GNUC__
  309. #pragma GCC diagnostic pop
  310. #endif