Skip to content

Commit

Permalink
[Analysis] Change several Analysis pieces to use NodeRef. NFC.
Browse files Browse the repository at this point in the history
Reviewers: dblaikie, grosser

Subscribers: mzolotukhin, llvm-commits

Differential Revision: https://reviews.llvm.org/D23625

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@279156 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
timshen91 committed Aug 18, 2016
1 parent 85a2cff commit 0c6f849
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 66 deletions.
15 changes: 7 additions & 8 deletions include/llvm/Analysis/BlockFrequencyInfoImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ struct BFIDOTGraphTraitsBase : public DefaultDOTGraphTraits {
: DefaultDOTGraphTraits(isSimple) {}

typedef GraphTraits<BlockFrequencyInfoT *> GTraits;
typedef typename GTraits::NodeType NodeType;
typedef typename GTraits::NodeRef NodeRef;
typedef typename GTraits::ChildIteratorType EdgeIter;
typedef typename GTraits::nodes_iterator NodeIter;

Expand All @@ -1260,8 +1260,7 @@ struct BFIDOTGraphTraitsBase : public DefaultDOTGraphTraits {
return G->getFunction()->getName();
}

std::string getNodeAttributes(const NodeType *Node,
const BlockFrequencyInfoT *Graph,
std::string getNodeAttributes(NodeRef Node, const BlockFrequencyInfoT *Graph,
unsigned HotPercentThreshold = 0) {
std::string Result;
if (!HotPercentThreshold)
Expand All @@ -1272,9 +1271,9 @@ struct BFIDOTGraphTraitsBase : public DefaultDOTGraphTraits {
for (NodeIter I = GTraits::nodes_begin(Graph),
E = GTraits::nodes_end(Graph);
I != E; ++I) {
NodeType &N = *I;
NodeRef N = &*I;
MaxFrequency =
std::max(MaxFrequency, Graph->getBlockFreq(&N).getFrequency());
std::max(MaxFrequency, Graph->getBlockFreq(N).getFrequency());
}
}
BlockFrequency Freq = Graph->getBlockFreq(Node);
Expand All @@ -1291,8 +1290,8 @@ struct BFIDOTGraphTraitsBase : public DefaultDOTGraphTraits {
return Result;
}

std::string getNodeLabel(const NodeType *Node,
const BlockFrequencyInfoT *Graph, GVDAGType GType) {
std::string getNodeLabel(NodeRef Node, const BlockFrequencyInfoT *Graph,
GVDAGType GType) {
std::string Result;
raw_string_ostream OS(Result);

Expand All @@ -1319,7 +1318,7 @@ struct BFIDOTGraphTraitsBase : public DefaultDOTGraphTraits {
return Result;
}

std::string getEdgeAttributes(const NodeType *Node, EdgeIter EI,
std::string getEdgeAttributes(NodeRef Node, EdgeIter EI,
const BlockFrequencyInfoT *BFI,
const BranchProbabilityInfoT *BPI,
unsigned HotPercentThreshold = 0) {
Expand Down
4 changes: 2 additions & 2 deletions include/llvm/Analysis/LoopInfoImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ BlockT *LoopBase<BlockT, LoopT>::getLoopPredecessor() const {
for (typename InvBlockTraits::ChildIteratorType PI =
InvBlockTraits::child_begin(Header),
PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) {
typename InvBlockTraits::NodeType *N = *PI;
typename InvBlockTraits::NodeRef N = *PI;
if (!contains(N)) { // If the block is not in the loop...
if (Out && Out != N)
return nullptr; // Multiple predecessors outside the loop
Expand All @@ -162,7 +162,7 @@ BlockT *LoopBase<BlockT, LoopT>::getLoopLatch() const {
InvBlockTraits::child_end(Header);
BlockT *Latch = nullptr;
for (; PI != PE; ++PI) {
typename InvBlockTraits::NodeType *N = *PI;
typename InvBlockTraits::NodeRef N = *PI;
if (contains(N)) {
if (Latch) return nullptr;
Latch = N;
Expand Down
121 changes: 65 additions & 56 deletions include/llvm/Analysis/RegionIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ namespace llvm {
///
/// For a subregion RegionNode there is just one successor. The RegionNode
/// representing the exit of the subregion.
template<class NodeType, class BlockT, class RegionT>
class RNSuccIterator : public std::iterator<std::forward_iterator_tag,
NodeType, ptrdiff_t> {
typedef std::iterator<std::forward_iterator_tag, NodeType, ptrdiff_t> super;
template <class NodeRef, class BlockT, class RegionT>
class RNSuccIterator
: public std::iterator<std::forward_iterator_tag, NodeRef> {
typedef std::iterator<std::forward_iterator_tag, NodeRef> super;

typedef GraphTraits<BlockT*> BlockTraits;
typedef typename BlockTraits::ChildIteratorType SuccIterTy;
Expand All @@ -49,8 +49,14 @@ class RNSuccIterator : public std::iterator<std::forward_iterator_tag,
ItRgEnd // At the end of the regionnode successor.
};

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");

// Use two bit to represent the mode iterator.
PointerIntPair<NodeType*, 2, ItMode> Node;
PointerIntPair<NodeRef, 2, ItMode> Node;

// The block successor iterator.
SuccIterTy BItor;
Expand All @@ -62,15 +68,15 @@ class RNSuccIterator : public std::iterator<std::forward_iterator_tag,
Node.setInt(ItRgEnd);
}

NodeType* getNode() const{ return Node.getPointer(); }
NodeRef getNode() const { return Node.getPointer(); }

// isRegionMode - Is the current iterator in region mode?
bool isRegionMode() const { return Node.getInt() != ItBB; }

// Get the immediate successor. This function may return a Basic Block
// RegionNode or a subregion RegionNode.
NodeType* getISucc(BlockT* BB) const {
NodeType *succ;
NodeRef getISucc(BlockT *BB) const {
NodeRef succ;
succ = getNode()->getParent()->getNode(BB);
assert(succ && "BB not in Region or entered subregion!");
return succ;
Expand All @@ -87,14 +93,14 @@ class RNSuccIterator : public std::iterator<std::forward_iterator_tag,
return getNode()->getParent()->getExit() == BB;
}
public:
typedef RNSuccIterator<NodeType, BlockT, RegionT> Self;
typedef RNSuccIterator<NodeRef, BlockT, RegionT> Self;

typedef typename super::pointer pointer;
typedef typename super::value_type value_type;

/// @brief Create begin iterator of a RegionNode.
inline RNSuccIterator(NodeType* node)
: Node(node, node->isSubRegion() ? ItRgBegin : ItBB),
BItor(BlockTraits::child_begin(node->getEntry())) {
inline RNSuccIterator(NodeRef node)
: Node(node, node->isSubRegion() ? ItRgBegin : ItBB),
BItor(BlockTraits::child_begin(node->getEntry())) {

// Skip the exit block
if (!isRegionMode())
Expand All @@ -106,9 +112,9 @@ class RNSuccIterator : public std::iterator<std::forward_iterator_tag,
}

/// @brief Create an end iterator.
inline RNSuccIterator(NodeType* node, bool)
: Node(node, node->isSubRegion() ? ItRgEnd : ItBB),
BItor(BlockTraits::child_end(node->getEntry())) {}
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!");
Expand All @@ -120,7 +126,7 @@ class RNSuccIterator : public std::iterator<std::forward_iterator_tag,

inline bool operator!=(const Self& x) const { return !operator==(x); }

inline pointer operator*() const {
inline value_type operator*() const {
BlockT *BB = isRegionMode() ? getRegionSucc() : *BItor;
assert(!isExit(BB) && "Iterator out of range!");
return getISucc(BB);
Expand Down Expand Up @@ -154,25 +160,25 @@ class RNSuccIterator : public std::iterator<std::forward_iterator_tag,
/// The Flat Region iterator will iterate over all BasicBlock RegionNodes that
/// are contained in the Region and its subregions. This is close to a virtual
/// control flow graph of the Region.
template<class NodeType, class BlockT, class RegionT>
class RNSuccIterator<FlatIt<NodeType>, BlockT, RegionT>
: public std::iterator<std::forward_iterator_tag, NodeType, ptrdiff_t> {
typedef std::iterator<std::forward_iterator_tag, NodeType, ptrdiff_t> super;
template <class NodeRef, class BlockT, class RegionT>
class RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT>
: public std::iterator<std::forward_iterator_tag, NodeRef> {
typedef std::iterator<std::forward_iterator_tag, NodeRef> super;
typedef GraphTraits<BlockT*> BlockTraits;
typedef typename BlockTraits::ChildIteratorType SuccIterTy;

NodeType* Node;
NodeRef Node;
SuccIterTy Itor;

public:
typedef RNSuccIterator<FlatIt<NodeType>, BlockT, RegionT> Self;
typedef typename super::pointer pointer;
typedef RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT> Self;
typedef typename super::value_type value_type;

/// @brief Create the iterator from a RegionNode.
///
/// Note that the incoming node must be a bb node, otherwise it will trigger
/// an assertion when we try to get a BasicBlock.
inline RNSuccIterator(NodeType *node)
inline RNSuccIterator(NodeRef node)
: Node(node), Itor(BlockTraits::child_begin(node->getEntry())) {
assert(!Node->isSubRegion() &&
"Subregion node not allowed in flat iterating mode!");
Expand All @@ -185,7 +191,7 @@ class RNSuccIterator<FlatIt<NodeType>, BlockT, RegionT>
}

/// @brief Create an end iterator
inline RNSuccIterator(NodeType *node, bool)
inline RNSuccIterator(NodeRef node, bool)
: Node(node), Itor(BlockTraits::child_end(node->getEntry())) {
assert(!Node->isSubRegion() &&
"Subregion node not allowed in flat iterating mode!");
Expand All @@ -200,7 +206,7 @@ class RNSuccIterator<FlatIt<NodeType>, BlockT, RegionT>

inline bool operator!=(const Self& x) const { return !operator==(x); }

inline pointer operator*() const {
inline value_type operator*() const {
BlockT *BB = *Itor;

// Get the iterating region.
Expand Down Expand Up @@ -230,14 +236,14 @@ class RNSuccIterator<FlatIt<NodeType>, BlockT, RegionT>
}
};

template<class NodeType, class BlockT, class RegionT>
inline RNSuccIterator<NodeType, BlockT, RegionT> succ_begin(NodeType* Node) {
return RNSuccIterator<NodeType, BlockT, RegionT>(Node);
template <class NodeRef, class BlockT, class RegionT>
inline RNSuccIterator<NodeRef, BlockT, RegionT> succ_begin(NodeRef Node) {
return RNSuccIterator<NodeRef, BlockT, RegionT>(Node);
}

template<class NodeType, class BlockT, class RegionT>
inline RNSuccIterator<NodeType, BlockT, RegionT> succ_end(NodeType* Node) {
return RNSuccIterator<NodeType, BlockT, RegionT>(Node, true);
template <class NodeRef, class BlockT, class RegionT>
inline RNSuccIterator<NodeRef, BlockT, RegionT> succ_end(NodeRef Node) {
return RNSuccIterator<NodeRef, BlockT, RegionT>(Node, true);
}

//===--------------------------------------------------------------------===//
Expand All @@ -251,32 +257,33 @@ inline RNSuccIterator<NodeType, BlockT, RegionT> succ_end(NodeType* Node) {
template <> struct GraphTraits<NodeT *> { \
typedef NodeT NodeType; \
typedef NodeT *NodeRef; \
typedef RNSuccIterator<NodeType, BlockT, RegionT> ChildIteratorType; \
static NodeType *getEntryNode(NodeType *N) { return N; } \
static inline ChildIteratorType child_begin(NodeType *N) { \
return RNSuccIterator<NodeType, BlockT, RegionT>(N); \
typedef RNSuccIterator<NodeRef, BlockT, RegionT> ChildIteratorType; \
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(NodeType *N) { \
return RNSuccIterator<NodeType, BlockT, RegionT>(N, true); \
static inline ChildIteratorType child_end(NodeRef N) { \
return RNSuccIterator<NodeRef, BlockT, RegionT>(N, true); \
} \
}; \
template <> struct GraphTraits<FlatIt<NodeT *>> { \
typedef NodeT NodeType; \
typedef NodeT *NodeRef; \
typedef RNSuccIterator<FlatIt<NodeT>, BlockT, RegionT> ChildIteratorType; \
static NodeType *getEntryNode(NodeType *N) { return N; } \
static inline ChildIteratorType child_begin(NodeType *N) { \
return RNSuccIterator<FlatIt<NodeType>, BlockT, RegionT>(N); \
typedef RNSuccIterator<FlatIt<NodeRef>, BlockT, RegionT> \
ChildIteratorType; \
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(NodeType *N) { \
return RNSuccIterator<FlatIt<NodeType>, BlockT, RegionT>(N, true); \
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 *> { \
typedef df_iterator<NodeType *> nodes_iterator; \
static NodeType *getEntryNode(RegionT *R) { \
typedef df_iterator<NodeRef> nodes_iterator; \
static NodeRef getEntryNode(RegionT *R) { \
return R->getNode(R->getEntry()); \
} \
static nodes_iterator nodes_begin(RegionT *R) { \
Expand All @@ -289,10 +296,10 @@ inline RNSuccIterator<NodeType, BlockT, RegionT> succ_end(NodeType* Node) {
template <> \
struct GraphTraits<FlatIt<RegionT *>> \
: public GraphTraits<FlatIt<NodeT *>> { \
typedef df_iterator<NodeType *, SmallPtrSet<NodeType *, 8>, false, \
GraphTraits<FlatIt<NodeType *>>> \
typedef df_iterator<NodeRef, SmallPtrSet<NodeRef, 8>, false, \
GraphTraits<FlatIt<NodeRef>>> \
nodes_iterator; \
static NodeType *getEntryNode(RegionT *R) { \
static NodeRef getEntryNode(RegionT *R) { \
return R->getBBNode(R->getEntry()); \
} \
static nodes_iterator nodes_begin(RegionT *R) { \
Expand All @@ -311,10 +318,11 @@ RegionGraphTraits(const Region, const RegionNode);

template <> struct GraphTraits<RegionInfo*>
: public GraphTraits<FlatIt<RegionNode*> > {
typedef df_iterator<NodeType*, SmallPtrSet<NodeType*, 8>, false,
GraphTraits<FlatIt<NodeType*> > > nodes_iterator;
typedef df_iterator<NodeRef, SmallPtrSet<NodeRef, 8>, false,
GraphTraits<FlatIt<NodeRef>>>
nodes_iterator;

static NodeType *getEntryNode(RegionInfo *RI) {
static NodeRef getEntryNode(RegionInfo *RI) {
return GraphTraits<FlatIt<Region*> >::getEntryNode(RI->getTopLevelRegion());
}
static nodes_iterator nodes_begin(RegionInfo* RI) {
Expand All @@ -327,10 +335,11 @@ template <> struct GraphTraits<RegionInfo*>

template <> struct GraphTraits<RegionInfoPass*>
: public GraphTraits<RegionInfo *> {
typedef df_iterator<NodeType*, SmallPtrSet<NodeType*, 8>, false,
GraphTraits<FlatIt<NodeType*> > > nodes_iterator;
typedef df_iterator<NodeRef, SmallPtrSet<NodeRef, 8>, false,
GraphTraits<FlatIt<NodeRef>>>
nodes_iterator;

static NodeType *getEntryNode(RegionInfoPass *RI) {
static NodeRef getEntryNode(RegionInfoPass *RI) {
return GraphTraits<RegionInfo*>::getEntryNode(&RI->getRegionInfo());
}
static nodes_iterator nodes_begin(RegionInfoPass* RI) {
Expand Down

0 comments on commit 0c6f849

Please sign in to comment.