1 //===- RegionIterator.h - Iterators to iteratate over Regions ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // This file defines the iterators to iterate over the elements of a Region.
10 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_ANALYSIS_REGIONITERATOR_H
13 #define LLVM_ANALYSIS_REGIONITERATOR_H
15 #include "llvm/ADT/DepthFirstIterator.h"
16 #include "llvm/ADT/GraphTraits.h"
17 #include "llvm/ADT/PointerIntPair.h"
18 #include "llvm/Analysis/RegionInfo.h"
19 #include "llvm/IR/CFG.h"
22 #include <type_traits>
28 //===----------------------------------------------------------------------===//
29 /// @brief Hierarchical RegionNode successor iterator.
31 /// This iterator iterates over all successors of a RegionNode.
33 /// For a BasicBlock RegionNode it skips all BasicBlocks that are not part of
34 /// the parent Region. Furthermore for BasicBlocks that start a subregion, a
35 /// RegionNode representing the subregion is returned.
37 /// For a subregion RegionNode there is just one successor. The RegionNode
38 /// representing the exit of the subregion.
39 template <class NodeRef, class BlockT, class RegionT>
41 : public std::iterator<std::forward_iterator_tag, NodeRef> {
42 using super = std::iterator<std::forward_iterator_tag, NodeRef>;
43 using BlockTraits = GraphTraits<BlockT *>;
44 using SuccIterTy = typename BlockTraits::ChildIteratorType;
46 // The iterator works in two modes, bb mode or region mode.
48 // In BB mode it returns all successors of this BasicBlock as its
51 // In region mode there is only one successor, thats the regionnode mapping
52 // to the exit block of the regionnode
53 ItRgBegin, // At the beginning of the regionnode successor.
54 ItRgEnd // At the end of the regionnode successor.
57 static_assert(std::is_pointer<NodeRef>::value,
58 "FIXME: Currently RNSuccIterator only supports NodeRef as "
59 "pointers due to the use of pointer-specific data structures "
60 "(e.g. PointerIntPair and SmallPtrSet) internally. Generalize "
61 "it to support non-pointer types");
63 // Use two bit to represent the mode iterator.
64 PointerIntPair<NodeRef, 2, ItMode> Node;
66 // The block successor iterator.
69 // advanceRegionSucc - A region node has only one successor. It reaches end
70 // once we advance it.
71 void advanceRegionSucc() {
72 assert(Node.getInt() == ItRgBegin && "Cannot advance region successor!");
76 NodeRef getNode() const { return Node.getPointer(); }
78 // isRegionMode - Is the current iterator in region mode?
79 bool isRegionMode() const { return Node.getInt() != ItBB; }
81 // Get the immediate successor. This function may return a Basic Block
82 // RegionNode or a subregion RegionNode.
83 NodeRef getISucc(BlockT *BB) const {
85 succ = getNode()->getParent()->getNode(BB);
86 assert(succ && "BB not in Region or entered subregion!");
90 // getRegionSucc - Return the successor basic block of a SubRegion RegionNode.
91 inline BlockT* getRegionSucc() const {
92 assert(Node.getInt() == ItRgBegin && "Cannot get the region successor!");
93 return getNode()->template getNodeAs<RegionT>()->getExit();
96 // isExit - Is this the exit BB of the Region?
97 inline bool isExit(BlockT* BB) const {
98 return getNode()->getParent()->getExit() == BB;
102 using Self = RNSuccIterator<NodeRef, BlockT, RegionT>;
103 using value_type = typename super::value_type;
105 /// @brief Create begin iterator of a RegionNode.
106 inline RNSuccIterator(NodeRef node)
107 : Node(node, node->isSubRegion() ? ItRgBegin : ItBB),
108 BItor(BlockTraits::child_begin(node->getEntry())) {
109 // Skip the exit block
111 while (BlockTraits::child_end(node->getEntry()) != BItor && isExit(*BItor))
114 if (isRegionMode() && isExit(getRegionSucc()))
118 /// @brief Create an end iterator.
119 inline RNSuccIterator(NodeRef node, bool)
120 : Node(node, node->isSubRegion() ? ItRgEnd : ItBB),
121 BItor(BlockTraits::child_end(node->getEntry())) {}
123 inline bool operator==(const Self& x) const {
124 assert(isRegionMode() == x.isRegionMode() && "Broken iterator!");
126 return Node.getInt() == x.Node.getInt();
128 return BItor == x.BItor;
131 inline bool operator!=(const Self& x) const { return !operator==(x); }
133 inline value_type operator*() const {
134 BlockT *BB = isRegionMode() ? getRegionSucc() : *BItor;
135 assert(!isExit(BB) && "Iterator out of range!");
139 inline Self& operator++() {
141 // The Region only has 1 successor.
147 while (BItor != BlockTraits::child_end(getNode()->getEntry())
153 inline Self operator++(int) {
160 //===----------------------------------------------------------------------===//
161 /// @brief Flat RegionNode iterator.
163 /// The Flat Region iterator will iterate over all BasicBlock RegionNodes that
164 /// are contained in the Region and its subregions. This is close to a virtual
165 /// control flow graph of the Region.
166 template <class NodeRef, class BlockT, class RegionT>
167 class RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>
168 : public std::iterator<std::forward_iterator_tag, NodeRef> {
169 using super = std::iterator<std::forward_iterator_tag, NodeRef>;
170 using BlockTraits = GraphTraits<BlockT *>;
171 using SuccIterTy = typename BlockTraits::ChildIteratorType;
177 using Self = RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>;
178 using value_type = typename super::value_type;
180 /// @brief Create the iterator from a RegionNode.
182 /// Note that the incoming node must be a bb node, otherwise it will trigger
183 /// an assertion when we try to get a BasicBlock.
184 inline RNSuccIterator(NodeRef node)
185 : Node(node), Itor(BlockTraits::child_begin(node->getEntry())) {
186 assert(!Node->isSubRegion() &&
187 "Subregion node not allowed in flat iterating mode!");
188 assert(Node->getParent() && "A BB node must have a parent!");
190 // Skip the exit block of the iterating region.
191 while (BlockTraits::child_end(Node->getEntry()) != Itor &&
192 Node->getParent()->getExit() == *Itor)
196 /// @brief Create an end iterator
197 inline RNSuccIterator(NodeRef node, bool)
198 : Node(node), Itor(BlockTraits::child_end(node->getEntry())) {
199 assert(!Node->isSubRegion() &&
200 "Subregion node not allowed in flat iterating mode!");
203 inline bool operator==(const Self& x) const {
204 assert(Node->getParent() == x.Node->getParent()
205 && "Cannot compare iterators of different regions!");
207 return Itor == x.Itor && Node == x.Node;
210 inline bool operator!=(const Self& x) const { return !operator==(x); }
212 inline value_type operator*() const {
215 // Get the iterating region.
216 RegionT *Parent = Node->getParent();
218 // The only case that the successor reaches out of the region is it reaches
219 // the exit of the region.
220 assert(Parent->getExit() != BB && "iterator out of range!");
222 return Parent->getBBNode(BB);
225 inline Self& operator++() {
226 // Skip the exit block of the iterating region.
229 while (Itor != succ_end(Node->getEntry())
230 && Node->getParent()->getExit() == *Itor);
235 inline Self operator++(int) {
242 template <class NodeRef, class BlockT, class RegionT>
243 inline RNSuccIterator<NodeRef, BlockT, RegionT> succ_begin(NodeRef Node) {
244 return RNSuccIterator<NodeRef, BlockT, RegionT>(Node);
247 template <class NodeRef, class BlockT, class RegionT>
248 inline RNSuccIterator<NodeRef, BlockT, RegionT> succ_end(NodeRef Node) {
249 return RNSuccIterator<NodeRef, BlockT, RegionT>(Node, true);
252 //===--------------------------------------------------------------------===//
253 // RegionNode GraphTraits specialization so the bbs in the region can be
254 // iterate by generic graph iterators.
256 // NodeT can either be region node or const region node, otherwise child_begin
257 // and child_end fail.
259 #define RegionNodeGraphTraits(NodeT, BlockT, RegionT) \
260 template <> struct GraphTraits<NodeT *> { \
261 using NodeRef = NodeT *; \
262 using ChildIteratorType = RNSuccIterator<NodeRef, BlockT, RegionT>; \
263 static NodeRef getEntryNode(NodeRef N) { return N; } \
264 static inline ChildIteratorType child_begin(NodeRef N) { \
265 return RNSuccIterator<NodeRef, BlockT, RegionT>(N); \
267 static inline ChildIteratorType child_end(NodeRef N) { \
268 return RNSuccIterator<NodeRef, BlockT, RegionT>(N, true); \
271 template <> struct GraphTraits<FlatIt<NodeT *>> { \
272 using NodeRef = NodeT *; \
273 using ChildIteratorType = \
274 RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>; \
275 static NodeRef getEntryNode(NodeRef N) { return N; } \
276 static inline ChildIteratorType child_begin(NodeRef N) { \
277 return RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>(N); \
279 static inline ChildIteratorType child_end(NodeRef N) { \
280 return RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>(N, true); \
284 #define RegionGraphTraits(RegionT, NodeT) \
285 template <> struct GraphTraits<RegionT *> : public GraphTraits<NodeT *> { \
286 using nodes_iterator = df_iterator<NodeRef>; \
287 static NodeRef getEntryNode(RegionT *R) { \
288 return R->getNode(R->getEntry()); \
290 static nodes_iterator nodes_begin(RegionT *R) { \
291 return nodes_iterator::begin(getEntryNode(R)); \
293 static nodes_iterator nodes_end(RegionT *R) { \
294 return nodes_iterator::end(getEntryNode(R)); \
298 struct GraphTraits<FlatIt<RegionT *>> \
299 : public GraphTraits<FlatIt<NodeT *>> { \
300 using nodes_iterator = \
301 df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false, \
302 GraphTraits<FlatIt<NodeRef>>>; \
303 static NodeRef getEntryNode(RegionT *R) { \
304 return R->getBBNode(R->getEntry()); \
306 static nodes_iterator nodes_begin(RegionT *R) { \
307 return nodes_iterator::begin(getEntryNode(R)); \
309 static nodes_iterator nodes_end(RegionT *R) { \
310 return nodes_iterator::end(getEntryNode(R)); \
314 RegionNodeGraphTraits(RegionNode, BasicBlock, Region);
315 RegionNodeGraphTraits(const RegionNode, BasicBlock, Region);
317 RegionGraphTraits(Region, RegionNode);
318 RegionGraphTraits(const Region, const RegionNode);
320 template <> struct GraphTraits<RegionInfo*>
321 : public GraphTraits<FlatIt<RegionNode*>> {
322 using nodes_iterator =
323 df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false,
324 GraphTraits<FlatIt<NodeRef>>>;
326 static NodeRef getEntryNode(RegionInfo *RI) {
327 return GraphTraits<FlatIt<Region*>>::getEntryNode(RI->getTopLevelRegion());
330 static nodes_iterator nodes_begin(RegionInfo* RI) {
331 return nodes_iterator::begin(getEntryNode(RI));
334 static nodes_iterator nodes_end(RegionInfo *RI) {
335 return nodes_iterator::end(getEntryNode(RI));
339 template <> struct GraphTraits<RegionInfoPass*>
340 : public GraphTraits<RegionInfo *> {
341 using nodes_iterator =
342 df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false,
343 GraphTraits<FlatIt<NodeRef>>>;
345 static NodeRef getEntryNode(RegionInfoPass *RI) {
346 return GraphTraits<RegionInfo*>::getEntryNode(&RI->getRegionInfo());
349 static nodes_iterator nodes_begin(RegionInfoPass* RI) {
350 return GraphTraits<RegionInfo*>::nodes_begin(&RI->getRegionInfo());
353 static nodes_iterator nodes_end(RegionInfoPass *RI) {
354 return GraphTraits<RegionInfo*>::nodes_end(&RI->getRegionInfo());
358 } // end namespace llvm
360 #endif // LLVM_ANALYSIS_REGIONITERATOR_H