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 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_ANALYSIS_REGIONITERATOR_H
12 #define LLVM_ANALYSIS_REGIONITERATOR_H
14 #include "llvm/ADT/GraphTraits.h"
15 #include "llvm/ADT/PointerIntPair.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/Analysis/RegionInfo.h"
18 #include "llvm/IR/CFG.h"
19 #include "llvm/Support/raw_ostream.h"
22 //===----------------------------------------------------------------------===//
23 /// @brief Hierarchical RegionNode successor iterator.
25 /// This iterator iterates over all successors of a RegionNode.
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.
31 /// For a subregion RegionNode there is just one successor. The RegionNode
32 /// representing the exit of the subregion.
33 template<class NodeType, class BlockT, class RegionT>
34 class RNSuccIterator : public std::iterator<std::forward_iterator_tag,
35 NodeType, ptrdiff_t> {
36 typedef std::iterator<std::forward_iterator_tag, NodeType, ptrdiff_t> super;
38 typedef GraphTraits<BlockT*> BlockTraits;
39 typedef typename BlockTraits::ChildIteratorType SuccIterTy;
41 // The iterator works in two modes, bb mode or region mode.
43 // In BB mode it returns all successors of this BasicBlock as its
46 // In region mode there is only one successor, thats the regionnode mapping
47 // to the exit block of the regionnode
48 ItRgBegin, // At the beginning of the regionnode successor.
49 ItRgEnd // At the end of the regionnode successor.
52 // Use two bit to represent the mode iterator.
53 PointerIntPair<NodeType*, 2, ItMode> Node;
55 // The block successor iterator.
58 // advanceRegionSucc - A region node has only one successor. It reaches end
59 // once we advance it.
60 void advanceRegionSucc() {
61 assert(Node.getInt() == ItRgBegin && "Cannot advance region successor!");
65 NodeType* getNode() const{ return Node.getPointer(); }
67 // isRegionMode - Is the current iterator in region mode?
68 bool isRegionMode() const { return Node.getInt() != ItBB; }
70 // Get the immediate successor. This function may return a Basic Block
71 // RegionNode or a subregion RegionNode.
72 NodeType* getISucc(BlockT* BB) const {
74 succ = getNode()->getParent()->getNode(BB);
75 assert(succ && "BB not in Region or entered subregion!");
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();
85 // isExit - Is this the exit BB of the Region?
86 inline bool isExit(BlockT* BB) const {
87 return getNode()->getParent()->getExit() == BB;
90 typedef RNSuccIterator<NodeType, BlockT, RegionT> Self;
92 typedef typename super::pointer pointer;
94 /// @brief Create begin iterator of a RegionNode.
95 inline RNSuccIterator(NodeType* node)
96 : Node(node, node->isSubRegion() ? ItRgBegin : ItBB),
97 BItor(BlockTraits::child_begin(node->getEntry())) {
99 // Skip the exit block
101 while (BlockTraits::child_end(node->getEntry()) != BItor && isExit(*BItor))
104 if (isRegionMode() && isExit(getRegionSucc()))
108 /// @brief Create an end iterator.
109 inline RNSuccIterator(NodeType* node, bool)
110 : Node(node, node->isSubRegion() ? ItRgEnd : ItBB),
111 BItor(BlockTraits::child_end(node->getEntry())) {}
113 inline bool operator==(const Self& x) const {
114 assert(isRegionMode() == x.isRegionMode() && "Broken iterator!");
116 return Node.getInt() == x.Node.getInt();
118 return BItor == x.BItor;
121 inline bool operator!=(const Self& x) const { return !operator==(x); }
123 inline pointer operator*() const {
124 BlockT *BB = isRegionMode() ? getRegionSucc() : *BItor;
125 assert(!isExit(BB) && "Iterator out of range!");
129 inline Self& operator++() {
131 // The Region only has 1 successor.
137 while (BItor != BlockTraits::child_end(getNode()->getEntry())
143 inline Self operator++(int) {
151 //===----------------------------------------------------------------------===//
152 /// @brief Flat RegionNode iterator.
154 /// The Flat Region iterator will iterate over all BasicBlock RegionNodes that
155 /// are contained in the Region and its subregions. This is close to a virtual
156 /// control flow graph of the Region.
157 template<class NodeType, class BlockT, class RegionT>
158 class RNSuccIterator<FlatIt<NodeType>, BlockT, RegionT>
159 : public std::iterator<std::forward_iterator_tag, NodeType, ptrdiff_t> {
160 typedef std::iterator<std::forward_iterator_tag, NodeType, ptrdiff_t> super;
161 typedef GraphTraits<BlockT*> BlockTraits;
162 typedef typename BlockTraits::ChildIteratorType SuccIterTy;
168 typedef RNSuccIterator<FlatIt<NodeType>, BlockT, RegionT> Self;
169 typedef typename super::pointer pointer;
171 /// @brief Create the iterator from a RegionNode.
173 /// Note that the incoming node must be a bb node, otherwise it will trigger
174 /// an assertion when we try to get a BasicBlock.
175 inline RNSuccIterator(NodeType* node) :
177 Itor(BlockTraits::child_begin(node->getEntry())) {
178 assert(!Node->isSubRegion()
179 && "Subregion node not allowed in flat iterating mode!");
180 assert(Node->getParent() && "A BB node must have a parent!");
182 // Skip the exit block of the iterating region.
183 while (BlockTraits::child_end(Node->getEntry()) != Itor
184 && Node->getParent()->getExit() == *Itor)
188 /// @brief Create an end iterator
189 inline RNSuccIterator(NodeType* node, bool) :
191 Itor(BlockTraits::child_end(node->getEntry())) {
192 assert(!Node->isSubRegion()
193 && "Subregion node not allowed in flat iterating mode!");
196 inline bool operator==(const Self& x) const {
197 assert(Node->getParent() == x.Node->getParent()
198 && "Cannot compare iterators of different regions!");
200 return Itor == x.Itor && Node == x.Node;
203 inline bool operator!=(const Self& x) const { return !operator==(x); }
205 inline pointer operator*() const {
208 // Get the iterating region.
209 RegionT *Parent = Node->getParent();
211 // The only case that the successor reaches out of the region is it reaches
212 // the exit of the region.
213 assert(Parent->getExit() != BB && "iterator out of range!");
215 return Parent->getBBNode(BB);
218 inline Self& operator++() {
219 // Skip the exit block of the iterating region.
222 while (Itor != succ_end(Node->getEntry())
223 && Node->getParent()->getExit() == *Itor);
228 inline Self operator++(int) {
235 template<class NodeType, class BlockT, class RegionT>
236 inline RNSuccIterator<NodeType, BlockT, RegionT> succ_begin(NodeType* Node) {
237 return RNSuccIterator<NodeType, BlockT, RegionT>(Node);
240 template<class NodeType, class BlockT, class RegionT>
241 inline RNSuccIterator<NodeType, BlockT, RegionT> succ_end(NodeType* Node) {
242 return RNSuccIterator<NodeType, BlockT, RegionT>(Node, true);
245 //===--------------------------------------------------------------------===//
246 // RegionNode GraphTraits specialization so the bbs in the region can be
247 // iterate by generic graph iterators.
249 // NodeT can either be region node or const region node, otherwise child_begin
250 // and child_end fail.
252 #define RegionNodeGraphTraits(NodeT, BlockT, RegionT) \
253 template<> struct GraphTraits<NodeT*> { \
254 typedef NodeT NodeType; \
255 typedef RNSuccIterator<NodeType, BlockT, RegionT> ChildIteratorType; \
256 static NodeType *getEntryNode(NodeType* N) { return N; } \
257 static inline ChildIteratorType child_begin(NodeType *N) { \
258 return RNSuccIterator<NodeType, BlockT, RegionT>(N); \
260 static inline ChildIteratorType child_end(NodeType *N) { \
261 return RNSuccIterator<NodeType, BlockT, RegionT>(N, true); \
264 template<> struct GraphTraits<FlatIt<NodeT*>> { \
265 typedef NodeT NodeType; \
266 typedef RNSuccIterator<FlatIt<NodeT>, BlockT, RegionT > ChildIteratorType; \
267 static NodeType *getEntryNode(NodeType* N) { return N; } \
268 static inline ChildIteratorType child_begin(NodeType *N) { \
269 return RNSuccIterator<FlatIt<NodeType>, BlockT, RegionT>(N); \
271 static inline ChildIteratorType child_end(NodeType *N) { \
272 return RNSuccIterator<FlatIt<NodeType>, BlockT, RegionT>(N, true); \
276 #define RegionGraphTraits(RegionT, NodeT) \
277 template<> struct GraphTraits<RegionT*> \
278 : public GraphTraits<NodeT*> { \
279 typedef df_iterator<NodeType*> nodes_iterator; \
280 static NodeType *getEntryNode(RegionT* R) { \
281 return R->getNode(R->getEntry()); \
283 static nodes_iterator nodes_begin(RegionT* R) { \
284 return nodes_iterator::begin(getEntryNode(R)); \
286 static nodes_iterator nodes_end(RegionT* R) { \
287 return nodes_iterator::end(getEntryNode(R)); \
290 template<> struct GraphTraits<FlatIt<RegionT*> > \
291 : public GraphTraits<FlatIt<NodeT*> > { \
292 typedef df_iterator<NodeType*, SmallPtrSet<NodeType*, 8>, false, \
293 GraphTraits<FlatIt<NodeType*> > > nodes_iterator; \
294 static NodeType *getEntryNode(RegionT* R) { \
295 return R->getBBNode(R->getEntry()); \
297 static nodes_iterator nodes_begin(RegionT* R) { \
298 return nodes_iterator::begin(getEntryNode(R)); \
300 static nodes_iterator nodes_end(RegionT* R) { \
301 return nodes_iterator::end(getEntryNode(R)); \
305 RegionNodeGraphTraits(RegionNode, BasicBlock, Region);
306 RegionNodeGraphTraits(const RegionNode, BasicBlock, Region);
308 RegionGraphTraits(Region, RegionNode);
309 RegionGraphTraits(const Region, const RegionNode);
311 template <> struct GraphTraits<RegionInfo*>
312 : public GraphTraits<FlatIt<RegionNode*> > {
313 typedef df_iterator<NodeType*, SmallPtrSet<NodeType*, 8>, false,
314 GraphTraits<FlatIt<NodeType*> > > nodes_iterator;
316 static NodeType *getEntryNode(RegionInfo *RI) {
317 return GraphTraits<FlatIt<Region*> >::getEntryNode(RI->getTopLevelRegion());
319 static nodes_iterator nodes_begin(RegionInfo* RI) {
320 return nodes_iterator::begin(getEntryNode(RI));
322 static nodes_iterator nodes_end(RegionInfo *RI) {
323 return nodes_iterator::end(getEntryNode(RI));
327 template <> struct GraphTraits<RegionInfoPass*>
328 : public GraphTraits<RegionInfo *> {
329 typedef df_iterator<NodeType*, SmallPtrSet<NodeType*, 8>, false,
330 GraphTraits<FlatIt<NodeType*> > > nodes_iterator;
332 static NodeType *getEntryNode(RegionInfoPass *RI) {
333 return GraphTraits<RegionInfo*>::getEntryNode(&RI->getRegionInfo());
335 static nodes_iterator nodes_begin(RegionInfoPass* RI) {
336 return GraphTraits<RegionInfo*>::nodes_begin(&RI->getRegionInfo());
338 static nodes_iterator nodes_end(RegionInfoPass *RI) {
339 return GraphTraits<RegionInfo*>::nodes_end(&RI->getRegionInfo());
343 } // End namespace llvm