Rename the Region::block_iterator to Region::block_node_iterator, and

add a new Region::block_iterator which actually iterates over the basic
blocks of the region.

The old iterator, now call 'block_node_iterator' iterates over
RegionNodes which contain a single basic block. This works well with the
GraphTraits-based iterator design, however most users actually want an
iterator over the BasicBlocks inside these RegionNodes. Now the
'block_iterator' is a wrapper which exposes exactly this interface.
Internally it uses the block_node_iterator to walk all nodes which are
single basic blocks, but transparently unwraps the basic block to make
user code simpler.

While this patch is a bit of a wash, most of the updates are to internal
users, not external users of the RegionInfo. I have an accompanying
patch to Polly that is a strict simplification of every user of this
interface, and I'm working on a pass that also wants the same simplified
interface.

This patch alone should have no functional impact.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@156202 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chandler Carruth 2012-05-04 20:55:23 +00:00
parent 49683f3c96
commit 7c52c97a22
4 changed files with 87 additions and 18 deletions

View File

@ -473,19 +473,71 @@ public:
const_iterator end() const { return children.end(); }
//@}
/// @name BasicBlock Iterators
/// @name BasicBlock Node Iterators
///
/// These iterators iterate over all BasicBlock RegionNodes that are
/// contained in this Region. The iterator also iterates over BasicBlocks
/// that are elements of a subregion of this Region. It is therefore called a
/// flat iterator.
/// contained in this Region. The iterator also iterates over BasicBlock
/// RegionNodes that are elements of a subregion of this Region. It is
/// therefore called a flat iterator.
//@{
typedef df_iterator<RegionNode*, SmallPtrSet<RegionNode*, 8>, false,
GraphTraits<FlatIt<RegionNode*> > > block_iterator;
GraphTraits<FlatIt<RegionNode*> > > block_node_iterator;
typedef df_iterator<const RegionNode*, SmallPtrSet<const RegionNode*, 8>,
false, GraphTraits<FlatIt<const RegionNode*> > >
const_block_iterator;
const_block_node_iterator;
block_node_iterator block_node_begin();
block_node_iterator block_node_end();
const_block_node_iterator block_node_begin() const;
const_block_node_iterator block_node_end() const;
//@}
/// @name BasicBlock Iterators
///
/// These iterators iterate over all BasicBlocks that are contained in this
/// Region. The iterator also iterates over BasicBlocks that are elements of
/// a subregion of this Region. It is therefore called a flat iterator.
//@{
template <typename RegionNodeItT>
class block_iterator_wrapper
: public std::iterator<std::forward_iterator_tag, BasicBlock, ptrdiff_t> {
typedef std::iterator<std::forward_iterator_tag, BasicBlock, ptrdiff_t>
super;
RegionNodeItT Iter;
public:
typedef block_iterator_wrapper<RegionNodeItT> Self;
typedef typename super::pointer pointer;
block_iterator_wrapper(RegionNodeItT Iter) : Iter(Iter) {}
bool operator==(const Self &RHS) const { return Iter == RHS.Iter; }
bool operator!=(const Self &RHS) const { return Iter != RHS.Iter; }
pointer operator*() const {
return (*Iter)->template getNodeAs<BasicBlock>();
}
Self& operator++() {
++Iter;
return *this;
}
Self operator++(int) {
Self tmp = *this;
++*this;
return tmp;
}
const Self &operator=(const Self &I) {
Iter = I.Iter;
return *this;
}
};
typedef block_iterator_wrapper<block_node_iterator> block_iterator;
typedef block_iterator_wrapper<const_block_node_iterator>
const_block_iterator;
block_iterator block_begin();
block_iterator block_end();

View File

@ -47,7 +47,7 @@ static cl::opt<enum Region::PrintStyle> printStyle("print-region-style",
cl::values(
clEnumValN(Region::PrintNone, "none", "print no details"),
clEnumValN(Region::PrintBB, "bb",
"print regions in detail with block_iterator"),
"print regions in detail with block_node_iterator"),
clEnumValN(Region::PrintRN, "rn",
"print regions in detail with element_iterator"),
clEnumValEnd));
@ -246,22 +246,38 @@ void Region::verifyRegionNest() const {
verifyRegion();
}
Region::block_iterator Region::block_begin() {
Region::block_node_iterator Region::block_node_begin() {
return GraphTraits<FlatIt<Region*> >::nodes_begin(this);
}
Region::block_iterator Region::block_end() {
Region::block_node_iterator Region::block_node_end() {
return GraphTraits<FlatIt<Region*> >::nodes_end(this);
}
Region::const_block_iterator Region::block_begin() const {
Region::const_block_node_iterator Region::block_node_begin() const {
return GraphTraits<FlatIt<const Region*> >::nodes_begin(this);
}
Region::const_block_iterator Region::block_end() const {
Region::const_block_node_iterator Region::block_node_end() const {
return GraphTraits<FlatIt<const Region*> >::nodes_end(this);
}
Region::block_iterator Region::block_begin() {
return block_node_begin();
}
Region::block_iterator Region::block_end() {
return block_node_end();
}
Region::const_block_iterator Region::block_begin() const {
return block_node_begin();
}
Region::const_block_iterator Region::block_end() const {
return block_node_end();
}
Region::element_iterator Region::element_begin() {
return GraphTraits<Region*>::nodes_begin(this);
}
@ -425,7 +441,9 @@ void Region::print(raw_ostream &OS, bool print_tree, unsigned level,
OS.indent(level*2 + 2);
if (Style == PrintBB) {
for (const_block_iterator I = block_begin(), E = block_end(); I!=E; ++I)
for (const_block_node_iterator I = block_node_begin(),
E = block_node_end();
I != E; ++I)
OS << **I << ", "; // TODO: remove the last ","
} else if (Style == PrintRN) {
for (const_element_iterator I = element_begin(), E = element_end(); I!=E; ++I)

View File

@ -195,7 +195,8 @@ public:
virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
Out << Banner;
for (Region::block_iterator I = R->block_begin(), E = R->block_end();
for (Region::block_node_iterator I = R->block_node_begin(),
E = R->block_node_end();
I != E; ++I)
(*I)->getEntry()->print(Out);

View File

@ -122,13 +122,11 @@ struct DOTGraphTraits<RegionInfo*> : public DOTGraphTraits<RegionNode*> {
RegionInfo *RI = R->getRegionInfo();
for (Region::const_block_iterator BI = R->block_begin(),
BE = R->block_end(); BI != BE; ++BI) {
BasicBlock *BB = (*BI)->getNodeAs<BasicBlock>();
if (RI->getRegionFor(BB) == R)
BE = R->block_end(); BI != BE; ++BI)
if (RI->getRegionFor(*BI) == R)
O.indent(2 * (depth + 1)) << "Node"
<< static_cast<const void*>(RI->getTopLevelRegion()->getBBNode(BB))
<< static_cast<const void*>(RI->getTopLevelRegion()->getBBNode(*BI))
<< ";\n";
}
O.indent(2 * depth) << "}\n";
}