Implement the block_iterator of Region based on df_iterator.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@161177 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Hongbin Zheng 2012-08-02 14:20:02 +00:00
parent 847a32b78f
commit f63f883425
2 changed files with 40 additions and 48 deletions

View File

@ -500,50 +500,58 @@ public:
/// Region. The iterator also iterates over BasicBlocks that are elements of /// Region. The iterator also iterates over BasicBlocks that are elements of
/// a subregion of this Region. It is therefore called a flat iterator. /// a subregion of this Region. It is therefore called a flat iterator.
//@{ //@{
template <typename RegionNodeItT> template <bool IsConst>
class block_iterator_wrapper class block_iterator_wrapper
: public std::iterator<std::forward_iterator_tag, BasicBlock, ptrdiff_t> { : public df_iterator<typename conditional<IsConst,
typedef std::iterator<std::forward_iterator_tag, BasicBlock, ptrdiff_t> const BasicBlock,
BasicBlock>::type*> {
typedef df_iterator<typename conditional<IsConst,
const BasicBlock,
BasicBlock>::type*>
super; super;
RegionNodeItT Iter;
public: public:
typedef block_iterator_wrapper<RegionNodeItT> Self; typedef block_iterator_wrapper<IsConst> Self;
typedef typename super::pointer pointer; typedef typename super::pointer pointer;
block_iterator_wrapper(RegionNodeItT Iter) : Iter(Iter) {} // Construct the begin iterator.
block_iterator_wrapper(pointer Entry, pointer Exit) : super(df_begin(Entry))
bool operator==(const Self &RHS) const { return Iter == RHS.Iter; } {
bool operator!=(const Self &RHS) const { return Iter != RHS.Iter; } // Mark the exit of the region as visited, so that the children of the
pointer operator*() const { // exit and the exit itself, i.e. the block outside the region will never
return (*Iter)->template getNodeAs<BasicBlock>(); // be visited.
super::Visited.insert(Exit);
} }
Self& operator++() { // Construct the end iterator.
++Iter; block_iterator_wrapper() : super(df_end<pointer>(0)) {}
return *this;
}
Self operator++(int) {
Self tmp = *this;
++*this;
return tmp;
}
const Self &operator=(const Self &I) { /*implicit*/ block_iterator_wrapper(super I) : super(I) {}
Iter = I.Iter;
return *this; // FIXME: Even a const_iterator returns a non-const BasicBlock pointer.
// This was introduced for backwards compatibility, but should
// be removed as soon as all users are fixed.
BasicBlock *operator*() const {
return const_cast<BasicBlock*>(super::operator*());
} }
}; };
typedef block_iterator_wrapper<block_node_iterator> block_iterator;
typedef block_iterator_wrapper<const_block_node_iterator>
const_block_iterator;
block_iterator block_begin(); typedef block_iterator_wrapper<false> block_iterator;
block_iterator block_end(); typedef block_iterator_wrapper<true> const_block_iterator;
const_block_iterator block_begin() const; block_iterator block_begin() {
const_block_iterator block_end() const; return block_iterator(getEntry(), getExit());
}
block_iterator block_end() {
return block_iterator();
}
const_block_iterator block_begin() const {
return const_block_iterator(getEntry(), getExit());
}
const_block_iterator block_end() const {
return const_block_iterator();
}
//@} //@}
/// @name Element Iterators /// @name Element Iterators

View File

@ -262,22 +262,6 @@ Region::const_block_node_iterator Region::block_node_end() const {
return GraphTraits<FlatIt<const Region*> >::nodes_end(this); 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() { Region::element_iterator Region::element_begin() {
return GraphTraits<Region*>::nodes_begin(this); return GraphTraits<Region*>::nodes_begin(this);
} }