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_REGION_ITERATOR_H
12
#define LLVM_ANALYSIS_REGION_ITERATOR_H
14
#include "llvm/ADT/GraphTraits.h"
15
#include "llvm/ADT/SmallPtrSet.h"
16
#include "llvm/ADT/PointerIntPair.h"
17
#include "llvm/Analysis/RegionInfo.h"
18
#include "llvm/Support/CFG.h"
19
#include "llvm/Support/raw_ostream.h"
22
//===----------------------------------------------------------------------===//
23
/// @brief Hierachical 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>
34
class RNSuccIterator : public std::iterator<std::forward_iterator_tag,
37
typedef std::iterator<std::forward_iterator_tag, NodeType, ptrdiff_t> super;
38
// The iterator works in two modes, bb mode or region mode.
40
// In BB mode it returns all successors of this BasicBlock as its
43
// In region mode there is only one successor, thats the regionnode mapping
44
// to the exit block of the regionnode
45
ItRgBegin, // At the beginning of the regionnode successor.
46
ItRgEnd // At the end of the regionnode successor.
49
// Use two bit to represent the mode iterator.
50
PointerIntPair<NodeType*, 2, enum ItMode> Node;
52
// The block successor iterator.
55
// advanceRegionSucc - A region node has only one successor. It reaches end
56
// once we advance it.
57
void advanceRegionSucc() {
58
assert(Node.getInt() == ItRgBegin && "Cannot advance region successor!");
62
NodeType* getNode() const{ return Node.getPointer(); }
64
// isRegionMode - Is the current iterator in region mode?
65
bool isRegionMode() const { return Node.getInt() != ItBB; }
67
// Get the immediate successor. This function may return a Basic Block
68
// RegionNode or a subregion RegionNode.
69
RegionNode* getISucc(BasicBlock* BB) const {
71
succ = getNode()->getParent()->getNode(BB);
72
assert(succ && "BB not in Region or entered subregion!");
76
// getRegionSucc - Return the successor basic block of a SubRegion RegionNode.
77
inline BasicBlock* getRegionSucc() const {
78
assert(Node.getInt() == ItRgBegin && "Cannot get the region successor!");
79
return getNode()->template getNodeAs<Region>()->getExit();
82
// isExit - Is this the exit BB of the Region?
83
inline bool isExit(BasicBlock* BB) const {
84
return getNode()->getParent()->getExit() == BB;
87
typedef RNSuccIterator<NodeType> Self;
89
typedef typename super::pointer pointer;
91
/// @brief Create begin iterator of a RegionNode.
92
inline RNSuccIterator(NodeType* node)
93
: Node(node, node->isSubRegion() ? ItRgBegin : ItBB),
94
BItor(succ_begin(node->getEntry())) {
97
// Skip the exit block
99
while (succ_end(node->getEntry()) != BItor && isExit(*BItor))
102
if (isRegionMode() && isExit(getRegionSucc()))
106
/// @brief Create an end iterator.
107
inline RNSuccIterator(NodeType* node, bool)
108
: Node(node, node->isSubRegion() ? ItRgEnd : ItBB),
109
BItor(succ_end(node->getEntry())) {}
111
inline bool operator==(const Self& x) const {
112
assert(isRegionMode() == x.isRegionMode() && "Broken iterator!");
114
return Node.getInt() == x.Node.getInt();
116
return BItor == x.BItor;
119
inline bool operator!=(const Self& x) const { return !operator==(x); }
121
inline pointer operator*() const {
122
BasicBlock* BB = isRegionMode() ? getRegionSucc() : *BItor;
123
assert(!isExit(BB) && "Iterator out of range!");
127
inline Self& operator++() {
129
// The Region only has 1 successor.
135
while (BItor != succ_end(getNode()->getEntry())
141
inline Self operator++(int) {
147
inline const Self &operator=(const Self &I) {
149
assert(getNode()->getParent() == I.getNode()->getParent()
150
&& "Cannot assign iterators of two different regions!");
159
//===----------------------------------------------------------------------===//
160
/// @brief Flat RegionNode iterator.
162
/// The Flat Region iterator will iterate over all BasicBlock RegionNodes that
163
/// are contained in the Region and its subregions. This is close to a virtual
164
/// control flow graph of the Region.
165
template<class NodeType>
166
class RNSuccIterator<FlatIt<NodeType> >
167
: public std::iterator<std::forward_iterator_tag, NodeType, ptrdiff_t>
169
typedef std::iterator<std::forward_iterator_tag, NodeType, ptrdiff_t> super;
174
typedef RNSuccIterator<FlatIt<NodeType> > Self;
175
typedef typename super::pointer pointer;
177
/// @brief Create the iterator from a RegionNode.
179
/// Note that the incoming node must be a bb node, otherwise it will trigger
180
/// an assertion when we try to get a BasicBlock.
181
inline RNSuccIterator(NodeType* node) : Node(node),
182
Itor(succ_begin(node->getEntry())) {
183
assert(!Node->isSubRegion()
184
&& "Subregion node not allowed in flat iterating mode!");
185
assert(Node->getParent() && "A BB node must have a parent!");
187
// Skip the exit block of the iterating region.
188
while (succ_end(Node->getEntry()) != Itor
189
&& Node->getParent()->getExit() == *Itor)
192
/// @brief Create an end iterator
193
inline RNSuccIterator(NodeType* node, bool) : Node(node),
194
Itor(succ_end(node->getEntry())) {
195
assert(!Node->isSubRegion()
196
&& "Subregion node not allowed in flat iterating mode!");
199
inline bool operator==(const Self& x) const {
200
assert(Node->getParent() == x.Node->getParent()
201
&& "Cannot compare iterators of different regions!");
203
return Itor == x.Itor && Node == x.Node;
206
inline bool operator!=(const Self& x) const { return !operator==(x); }
208
inline pointer operator*() const {
209
BasicBlock* BB = *Itor;
211
// Get the iterating region.
212
Region* Parent = Node->getParent();
214
// The only case that the successor reaches out of the region is it reaches
215
// the exit of the region.
216
assert(Parent->getExit() != BB && "iterator out of range!");
218
return Parent->getBBNode(BB);
221
inline Self& operator++() {
222
// Skip the exit block of the iterating region.
225
while (Itor != succ_end(Node->getEntry())
226
&& Node->getParent()->getExit() == *Itor);
231
inline Self operator++(int) {
237
inline const Self &operator=(const Self &I) {
239
assert(Node->getParent() == I.Node->getParent()
240
&& "Cannot assign iterators to two different regions!");
248
template<class NodeType>
249
inline RNSuccIterator<NodeType> succ_begin(NodeType* Node) {
250
return RNSuccIterator<NodeType>(Node);
253
template<class NodeType>
254
inline RNSuccIterator<NodeType> succ_end(NodeType* Node) {
255
return RNSuccIterator<NodeType>(Node, true);
258
//===--------------------------------------------------------------------===//
259
// RegionNode GraphTraits specialization so the bbs in the region can be
260
// iterate by generic graph iterators.
262
// NodeT can either be region node or const region node, otherwise child_begin
263
// and child_end fail.
265
#define RegionNodeGraphTraits(NodeT) \
266
template<> struct GraphTraits<NodeT*> { \
267
typedef NodeT NodeType; \
268
typedef RNSuccIterator<NodeType> ChildIteratorType; \
269
static NodeType *getEntryNode(NodeType* N) { return N; } \
270
static inline ChildIteratorType child_begin(NodeType *N) { \
271
return RNSuccIterator<NodeType>(N); \
273
static inline ChildIteratorType child_end(NodeType *N) { \
274
return RNSuccIterator<NodeType>(N, true); \
277
template<> struct GraphTraits<FlatIt<NodeT*> > { \
278
typedef NodeT NodeType; \
279
typedef RNSuccIterator<FlatIt<NodeT> > ChildIteratorType; \
280
static NodeType *getEntryNode(NodeType* N) { return N; } \
281
static inline ChildIteratorType child_begin(NodeType *N) { \
282
return RNSuccIterator<FlatIt<NodeType> >(N); \
284
static inline ChildIteratorType child_end(NodeType *N) { \
285
return RNSuccIterator<FlatIt<NodeType> >(N, true); \
289
#define RegionGraphTraits(RegionT, NodeT) \
290
template<> struct GraphTraits<RegionT*> \
291
: public GraphTraits<NodeT*> { \
292
typedef df_iterator<NodeType*> nodes_iterator; \
293
static NodeType *getEntryNode(RegionT* R) { \
294
return R->getNode(R->getEntry()); \
296
static nodes_iterator nodes_begin(RegionT* R) { \
297
return nodes_iterator::begin(getEntryNode(R)); \
299
static nodes_iterator nodes_end(RegionT* R) { \
300
return nodes_iterator::end(getEntryNode(R)); \
303
template<> struct GraphTraits<FlatIt<RegionT*> > \
304
: public GraphTraits<FlatIt<NodeT*> > { \
305
typedef df_iterator<NodeType*, SmallPtrSet<NodeType*, 8>, false, \
306
GraphTraits<FlatIt<NodeType*> > > nodes_iterator; \
307
static NodeType *getEntryNode(RegionT* R) { \
308
return R->getBBNode(R->getEntry()); \
310
static nodes_iterator nodes_begin(RegionT* R) { \
311
return nodes_iterator::begin(getEntryNode(R)); \
313
static nodes_iterator nodes_end(RegionT* R) { \
314
return nodes_iterator::end(getEntryNode(R)); \
318
RegionNodeGraphTraits(RegionNode);
319
RegionNodeGraphTraits(const RegionNode);
321
RegionGraphTraits(Region, RegionNode);
322
RegionGraphTraits(const Region, const RegionNode);
324
template <> struct GraphTraits<RegionInfo*>
325
: public GraphTraits<FlatIt<RegionNode*> > {
326
typedef df_iterator<NodeType*, SmallPtrSet<NodeType*, 8>, false,
327
GraphTraits<FlatIt<NodeType*> > > nodes_iterator;
329
static NodeType *getEntryNode(RegionInfo *RI) {
330
return GraphTraits<FlatIt<Region*> >::getEntryNode(RI->getTopLevelRegion());
332
static nodes_iterator nodes_begin(RegionInfo* RI) {
333
return nodes_iterator::begin(getEntryNode(RI));
335
static nodes_iterator nodes_end(RegionInfo *RI) {
336
return nodes_iterator::end(getEntryNode(RI));
340
} // End namespace llvm