Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
11 KiB

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