#ifndef LLVM_ANALYSIS_REGIONITERATOR_H
#define LLVM_ANALYSIS_REGIONITERATOR_H
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/GraphTraits.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/Analysis/RegionInfo.h"
#include <cassert>
#include <iterator>
#include <type_traits>
namespace llvm {
class BasicBlock;
class RegionInfo;
template <class NodeRef, class BlockT, class RegionT> class RNSuccIterator {
public:
using iterator_category = std::forward_iterator_tag;
using value_type = NodeRef;
using difference_type = std::ptrdiff_t;
using pointer = value_type *;
using reference = value_type &;
private:
using BlockTraits = GraphTraits<BlockT *>;
using SuccIterTy = typename BlockTraits::ChildIteratorType;
enum ItMode {
ItBB,
ItRgBegin, ItRgEnd };
static_assert(std::is_pointer<NodeRef>::value,
"FIXME: Currently RNSuccIterator only supports NodeRef as "
"pointers due to the use of pointer-specific data structures "
"(e.g. PointerIntPair and SmallPtrSet) internally. Generalize "
"it to support non-pointer types");
PointerIntPair<NodeRef, 2, ItMode> Node;
SuccIterTy BItor;
void advanceRegionSucc() {
assert(Node.getInt() == ItRgBegin && "Cannot advance region successor!");
Node.setInt(ItRgEnd);
}
NodeRef getNode() const { return Node.getPointer(); }
bool isRegionMode() const { return Node.getInt() != ItBB; }
NodeRef getISucc(BlockT *BB) const {
NodeRef succ;
succ = getNode()->getParent()->getNode(BB);
assert(succ && "BB not in Region or entered subregion!");
return succ;
}
inline BlockT* getRegionSucc() const {
assert(Node.getInt() == ItRgBegin && "Cannot get the region successor!");
return getNode()->template getNodeAs<RegionT>()->getExit();
}
inline bool isExit(BlockT* BB) const {
return getNode()->getParent()->getExit() == BB;
}
public:
using Self = RNSuccIterator<NodeRef, BlockT, RegionT>;
inline RNSuccIterator(NodeRef node)
: Node(node, node->isSubRegion() ? ItRgBegin : ItBB),
BItor(BlockTraits::child_begin(node->getEntry())) {
if (!isRegionMode())
while (BlockTraits::child_end(node->getEntry()) != BItor && isExit(*BItor))
++BItor;
if (isRegionMode() && isExit(getRegionSucc()))
advanceRegionSucc();
}
inline RNSuccIterator(NodeRef node, bool)
: Node(node, node->isSubRegion() ? ItRgEnd : ItBB),
BItor(BlockTraits::child_end(node->getEntry())) {}
inline bool operator==(const Self& x) const {
assert(isRegionMode() == x.isRegionMode() && "Broken iterator!");
if (isRegionMode())
return Node.getInt() == x.Node.getInt();
else
return BItor == x.BItor;
}
inline bool operator!=(const Self& x) const { return !operator==(x); }
inline value_type operator*() const {
BlockT *BB = isRegionMode() ? getRegionSucc() : *BItor;
assert(!isExit(BB) && "Iterator out of range!");
return getISucc(BB);
}
inline Self& operator++() {
if(isRegionMode()) {
advanceRegionSucc();
} else {
do
++BItor;
while (BItor != BlockTraits::child_end(getNode()->getEntry())
&& isExit(*BItor));
}
return *this;
}
inline Self operator++(int) {
Self tmp = *this;
++*this;
return tmp;
}
};
template <class NodeRef, class BlockT, class RegionT>
class RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT> {
using BlockTraits = GraphTraits<BlockT *>;
using SuccIterTy = typename BlockTraits::ChildIteratorType;
NodeRef Node;
SuccIterTy Itor;
public:
using iterator_category = std::forward_iterator_tag;
using value_type = NodeRef;
using difference_type = std::ptrdiff_t;
using pointer = value_type *;
using reference = value_type &;
using Self = RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>;
inline RNSuccIterator(NodeRef node)
: Node(node), Itor(BlockTraits::child_begin(node->getEntry())) {
assert(!Node->isSubRegion() &&
"Subregion node not allowed in flat iterating mode!");
assert(Node->getParent() && "A BB node must have a parent!");
while (BlockTraits::child_end(Node->getEntry()) != Itor &&
Node->getParent()->getExit() == *Itor)
++Itor;
}
inline RNSuccIterator(NodeRef node, bool)
: Node(node), Itor(BlockTraits::child_end(node->getEntry())) {
assert(!Node->isSubRegion() &&
"Subregion node not allowed in flat iterating mode!");
}
inline bool operator==(const Self& x) const {
assert(Node->getParent() == x.Node->getParent()
&& "Cannot compare iterators of different regions!");
return Itor == x.Itor && Node == x.Node;
}
inline bool operator!=(const Self& x) const { return !operator==(x); }
inline value_type operator*() const {
BlockT *BB = *Itor;
RegionT *Parent = Node->getParent();
assert(Parent->getExit() != BB && "iterator out of range!");
return Parent->getBBNode(BB);
}
inline Self& operator++() {
do
++Itor;
while (Itor != succ_end(Node->getEntry())
&& Node->getParent()->getExit() == *Itor);
return *this;
}
inline Self operator++(int) {
Self tmp = *this;
++*this;
return tmp;
}
};
template <class NodeRef, class BlockT, class RegionT>
inline RNSuccIterator<NodeRef, BlockT, RegionT> succ_begin(NodeRef Node) {
return RNSuccIterator<NodeRef, BlockT, RegionT>(Node);
}
template <class NodeRef, class BlockT, class RegionT>
inline RNSuccIterator<NodeRef, BlockT, RegionT> succ_end(NodeRef Node) {
return RNSuccIterator<NodeRef, BlockT, RegionT>(Node, true);
}
#define RegionNodeGraphTraits(NodeT, BlockT, RegionT) \
template <> struct GraphTraits<NodeT *> { \
using NodeRef = NodeT *; \
using ChildIteratorType = RNSuccIterator<NodeRef, BlockT, RegionT>; \
static NodeRef getEntryNode(NodeRef N) { return N; } \
static inline ChildIteratorType child_begin(NodeRef N) { \
return RNSuccIterator<NodeRef, BlockT, RegionT>(N); \
} \
static inline ChildIteratorType child_end(NodeRef N) { \
return RNSuccIterator<NodeRef, BlockT, RegionT>(N, true); \
} \
}; \
template <> struct GraphTraits<FlatIt<NodeT *>> { \
using NodeRef = NodeT *; \
using ChildIteratorType = \
RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>; \
static NodeRef getEntryNode(NodeRef N) { return N; } \
static inline ChildIteratorType child_begin(NodeRef N) { \
return RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>(N); \
} \
static inline ChildIteratorType child_end(NodeRef N) { \
return RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>(N, true); \
} \
}
#define RegionGraphTraits(RegionT, NodeT) \
template <> struct GraphTraits<RegionT *> : public GraphTraits<NodeT *> { \
using nodes_iterator = df_iterator<NodeRef>; \
static NodeRef getEntryNode(RegionT *R) { \
return R->getNode(R->getEntry()); \
} \
static nodes_iterator nodes_begin(RegionT *R) { \
return nodes_iterator::begin(getEntryNode(R)); \
} \
static nodes_iterator nodes_end(RegionT *R) { \
return nodes_iterator::end(getEntryNode(R)); \
} \
}; \
template <> \
struct GraphTraits<FlatIt<RegionT *>> \
: public GraphTraits<FlatIt<NodeT *>> { \
using nodes_iterator = \
df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false, \
GraphTraits<FlatIt<NodeRef>>>; \
static NodeRef getEntryNode(RegionT *R) { \
return R->getBBNode(R->getEntry()); \
} \
static nodes_iterator nodes_begin(RegionT *R) { \
return nodes_iterator::begin(getEntryNode(R)); \
} \
static nodes_iterator nodes_end(RegionT *R) { \
return nodes_iterator::end(getEntryNode(R)); \
} \
}
RegionNodeGraphTraits(RegionNode, BasicBlock, Region);
RegionNodeGraphTraits(const RegionNode, BasicBlock, Region);
RegionGraphTraits(Region, RegionNode);
RegionGraphTraits(const Region, const RegionNode);
template <> struct GraphTraits<RegionInfo*>
: public GraphTraits<FlatIt<RegionNode*>> {
using nodes_iterator =
df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false,
GraphTraits<FlatIt<NodeRef>>>;
static NodeRef getEntryNode(RegionInfo *RI) {
return GraphTraits<FlatIt<Region*>>::getEntryNode(RI->getTopLevelRegion());
}
static nodes_iterator nodes_begin(RegionInfo* RI) {
return nodes_iterator::begin(getEntryNode(RI));
}
static nodes_iterator nodes_end(RegionInfo *RI) {
return nodes_iterator::end(getEntryNode(RI));
}
};
template <> struct GraphTraits<RegionInfoPass*>
: public GraphTraits<RegionInfo *> {
using nodes_iterator =
df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false,
GraphTraits<FlatIt<NodeRef>>>;
static NodeRef getEntryNode(RegionInfoPass *RI) {
return GraphTraits<RegionInfo*>::getEntryNode(&RI->getRegionInfo());
}
static nodes_iterator nodes_begin(RegionInfoPass* RI) {
return GraphTraits<RegionInfo*>::nodes_begin(&RI->getRegionInfo());
}
static nodes_iterator nodes_end(RegionInfoPass *RI) {
return GraphTraits<RegionInfo*>::nodes_end(&RI->getRegionInfo());
}
};
}
#endif