Skip to content

Commit

Permalink
GraphTraits: Add range versions of graph traits functions (graph_node…
Browse files Browse the repository at this point in the history
…s, graph_children, inverse_graph_nodes, inverse_graph_children).

Summary:
Convert all obvious node_begin/node_end and child_begin/child_end
pairs to range based for.

Sending for review in case someone has a good idea how to make
graph_children able to be inferred. It looks like it would require
changing GraphTraits to be two argument or something. I presume
inference does not happen because it would have to check every
GraphTraits in the world to see if the noderef types matched.

Note: This change was 3-staged with clang as well, which uses
Dominators/etc from LLVM.

Reviewers: chandlerc, tstellarAMD, dblaikie, rsmith

Subscribers: arsenm, llvm-commits, nhaehnle

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@294620 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
dberlin committed Feb 9, 2017
1 parent 4c5e906 commit c0fb958
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 60 deletions.
29 changes: 29 additions & 0 deletions include/llvm/ADT/GraphTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef LLVM_ADT_GRAPHTRAITS_H
#define LLVM_ADT_GRAPHTRAITS_H

#include "llvm/ADT/iterator_range.h"

namespace llvm {

// GraphTraits - This class should be specialized by different graph types...
Expand Down Expand Up @@ -86,6 +88,33 @@ struct Inverse {
// inverse falls back to the original graph.
template <class T> struct GraphTraits<Inverse<Inverse<T>>> : GraphTraits<T> {};

// Provide iterator ranges for the graph traits nodes and children
template <class GraphType>
iterator_range<typename GraphTraits<GraphType>::nodes_iterator>
graph_nodes(const GraphType &G) {
return make_range(GraphTraits<GraphType>::nodes_begin(G),
GraphTraits<GraphType>::nodes_end(G));
}
template <class GraphType>
iterator_range<typename GraphTraits<Inverse<GraphType>>::nodes_iterator>
inverse_graph_nodes(const GraphType &G) {
return make_range(GraphTraits<Inverse<GraphType>>::nodes_begin(G),
GraphTraits<Inverse<GraphType>>::nodes_end(G));
}

template <class GraphType>
iterator_range<typename GraphTraits<GraphType>::ChildIteratorType>
graph_children(const typename GraphTraits<GraphType>::NodeRef &G) {
return make_range(GraphTraits<GraphType>::child_begin(G),
GraphTraits<GraphType>::child_end(G));
}

template <class GraphType>
iterator_range<typename GraphTraits<Inverse<GraphType>>::ChildIteratorType>
inverse_graph_children(const typename GraphTraits<GraphType>::NodeRef &G) {
return make_range(GraphTraits<Inverse<GraphType>>::child_begin(G),
GraphTraits<Inverse<GraphType>>::child_end(G));
}
} // End llvm namespace

#endif
8 changes: 3 additions & 5 deletions include/llvm/Support/GenericDomTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -782,11 +782,9 @@ template <class NodeT> class DominatorTreeBase : public DominatorBase<NodeT> {
Calculate<FT, NodeT *>(*this, F);
} else {
// Initialize the roots list
for (typename TraitsTy::nodes_iterator I = TraitsTy::nodes_begin(&F),
E = TraitsTy::nodes_end(&F);
I != E; ++I)
if (TraitsTy::child_begin(*I) == TraitsTy::child_end(*I))
addRoot(*I);
for (auto *Node : graph_nodes(&F))
if (TraitsTy::child_begin(Node) == TraitsTy::child_end(Node))
addRoot(Node);

Calculate<FT, Inverse<NodeT *>>(*this, F);
}
Expand Down
9 changes: 2 additions & 7 deletions include/llvm/Support/GenericDomTreeConstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,12 @@ void Calculate(DominatorTreeBaseByGraphTraits<GraphTraits<NodeT>> &DT,

// initialize the semi dominator to point to the parent node
WInfo.Semi = WInfo.Parent;
typedef GraphTraits<Inverse<NodeT> > InvTraits;
for (typename InvTraits::ChildIteratorType CI =
InvTraits::child_begin(W),
E = InvTraits::child_end(W); CI != E; ++CI) {
typename InvTraits::NodeRef N = *CI;
if (DT.Info.count(N)) { // Only if this predecessor is reachable!
for (const auto &N : inverse_graph_children<NodeT>(W))
if (DT.Info.count(N)) { // Only if this predecessor is reachable!
unsigned SemiU = DT.Info[Eval<GraphT>(DT, N, i + 1)].Semi;
if (SemiU < WInfo.Semi)
WInfo.Semi = SemiU;
}
}

// If V is a non-root vertex and sdom(V) = parent(V), then idom(V) is
// necessarily parent(V). In this case, set idom(V) here and avoid placing
Expand Down
5 changes: 1 addition & 4 deletions lib/Analysis/IteratedDominanceFrontier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ void IDFCalculator<NodeTy>::calculate(
BasicBlock *BB = Node->getBlock();
// Succ is the successor in the direction we are calculating IDF, so it is
// successor for IDF, and predecessor for Reverse IDF.
for (auto SuccIter = GraphTraits<NodeTy>::child_begin(BB),
End = GraphTraits<NodeTy>::child_end(BB);
SuccIter != End; ++SuccIter) {
BasicBlock *Succ = *SuccIter;
for (auto *Succ : graph_children<NodeTy>(BB)) {
DomTreeNode *SuccNode = DT.getNode(Succ);

// Quickly skip all CFG edges that are also dominator tree edges instead
Expand Down
17 changes: 5 additions & 12 deletions lib/Target/AMDGPU/AMDILCFGStructurizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,11 +911,8 @@ void AMDGPUCFGStructurizer::orderBlocks(MachineFunction *MF) {
}
}

//walk through all the block in func to check for unreachable
typedef GraphTraits<MachineFunction *> GTM;
auto It = GTM::nodes_begin(MF), E = GTM::nodes_end(MF);
for (; It != E; ++It) {
MachineBasicBlock *MBB = *It;
// walk through all the block in func to check for unreachable
for (auto *MBB : graph_nodes(MF)) {
SccNum = getSCCNum(MBB);
if (SccNum == INVALIDSCCNUM)
dbgs() << "unreachable block BB" << MBB->getNumber() << "\n";
Expand Down Expand Up @@ -1081,13 +1078,9 @@ int AMDGPUCFGStructurizer::mergeLoop(MachineLoop *LoopRep) {
MachineBasicBlock *ExitBlk = *ExitBlks.begin();
assert(ExitBlk && "Loop has several exit block");
MBBVector LatchBlks;
typedef GraphTraits<Inverse<MachineBasicBlock*>> InvMBBTraits;
InvMBBTraits::ChildIteratorType PI = InvMBBTraits::child_begin(LoopHeader),
PE = InvMBBTraits::child_end(LoopHeader);
for (; PI != PE; PI++) {
if (LoopRep->contains(*PI))
LatchBlks.push_back(*PI);
}
for (auto *LB : inverse_graph_children<MachineBasicBlock*>(LoopHeader))
if (LoopRep->contains(LB))
LatchBlks.push_back(LB);

for (unsigned i = 0, e = ExitingMBBs.size(); i < e; ++i)
mergeLoopbreakBlock(ExitingMBBs[i], ExitBlk);
Expand Down
15 changes: 6 additions & 9 deletions lib/Target/Hexagon/HexagonBitSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ INITIALIZE_PASS_END(HexagonBitSimplify, "hexbit",

bool HexagonBitSimplify::visitBlock(MachineBasicBlock &B, Transformation &T,
RegisterSet &AVs) {
MachineDomTreeNode *N = MDT->getNode(&B);
typedef GraphTraits<MachineDomTreeNode*> GTN;
bool Changed = false;

if (T.TopDown)
Expand All @@ -262,10 +260,9 @@ bool HexagonBitSimplify::visitBlock(MachineBasicBlock &B, Transformation &T,
RegisterSet NewAVs = AVs;
NewAVs.insert(Defs);

for (auto I = GTN::child_begin(N), E = GTN::child_end(N); I != E; ++I) {
MachineBasicBlock *SB = (*I)->getBlock();
Changed |= visitBlock(*SB, T, NewAVs);
}
for (auto *DTN : graph_children<MachineDomTreeNode*>(MDT->getNode(&B)))
Changed |= visitBlock(*(DTN->getBlock()), T, NewAVs);

if (!T.TopDown)
Changed |= T.processBlock(B, AVs);

Expand Down Expand Up @@ -984,9 +981,9 @@ bool DeadCodeElimination::isDead(unsigned R) const {

bool DeadCodeElimination::runOnNode(MachineDomTreeNode *N) {
bool Changed = false;
typedef GraphTraits<MachineDomTreeNode*> GTN;
for (auto I = GTN::child_begin(N), E = GTN::child_end(N); I != E; ++I)
Changed |= runOnNode(*I);

for (auto *DTN : graph_children<MachineDomTreeNode*>(N))
Changed |= runOnNode(DTN);

MachineBasicBlock *B = N->getBlock();
std::vector<MachineInstr*> Instrs;
Expand Down
14 changes: 4 additions & 10 deletions lib/Target/Hexagon/HexagonCommonGEP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,8 @@ void HexagonCommonGEP::getBlockTraversalOrder(BasicBlock *Root,
// visited".

Order.push_back(Root);
DomTreeNode *DTN = DT->getNode(Root);
typedef GraphTraits<DomTreeNode*> GTN;
typedef GTN::ChildIteratorType Iter;
for (Iter I = GTN::child_begin(DTN), E = GTN::child_end(DTN); I != E; ++I)
getBlockTraversalOrder((*I)->getBlock(), Order);
for (auto *DTN : graph_children<DomTreeNode*>(DT->getNode(Root)))
getBlockTraversalOrder(DTN->getBlock(), Order);
}

bool HexagonCommonGEP::isHandledGepForm(GetElementPtrInst *GepI) {
Expand Down Expand Up @@ -1235,11 +1232,8 @@ void HexagonCommonGEP::removeDeadCode() {

for (unsigned i = 0; i < BO.size(); ++i) {
BasicBlock *B = cast<BasicBlock>(BO[i]);
DomTreeNode *N = DT->getNode(B);
typedef GraphTraits<DomTreeNode*> GTN;
typedef GTN::ChildIteratorType Iter;
for (Iter I = GTN::child_begin(N), E = GTN::child_end(N); I != E; ++I)
BO.push_back((*I)->getBlock());
for (auto DTN : graph_children<DomTreeNode*>(DT->getNode(B)))
BO.push_back(DTN->getBlock());
}

for (unsigned i = BO.size(); i > 0; --i) {
Expand Down
7 changes: 2 additions & 5 deletions lib/Target/Hexagon/HexagonGenExtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,8 @@ bool HexagonGenExtract::convert(Instruction *In) {

bool HexagonGenExtract::visitBlock(BasicBlock *B) {
// Depth-first, bottom-up traversal.
DomTreeNode *DTN = DT->getNode(B);
typedef GraphTraits<DomTreeNode*> GTN;
typedef GTN::ChildIteratorType Iter;
for (Iter I = GTN::child_begin(DTN), E = GTN::child_end(DTN); I != E; ++I)
visitBlock((*I)->getBlock());
for (auto *DTN : graph_children<DomTreeNode*>(DT->getNode(B)))
visitBlock(DTN->getBlock());

// Allow limiting the number of generated extracts for debugging purposes.
bool HasCutoff = ExtractCutoff.getPosition();
Expand Down
13 changes: 5 additions & 8 deletions lib/Target/Hexagon/HexagonGenInsert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,11 +947,8 @@ void HexagonGenInsert::collectInBlock(MachineBasicBlock *B,
BlockDefs.insert(InsDefs);
}

MachineDomTreeNode *N = MDT->getNode(B);
typedef GraphTraits<MachineDomTreeNode*> GTN;
typedef GTN::ChildIteratorType ChildIter;
for (ChildIter I = GTN::child_begin(N), E = GTN::child_end(N); I != E; ++I) {
MachineBasicBlock *SB = (*I)->getBlock();
for (auto *DTN : graph_children<MachineDomTreeNode*>(MDT->getNode(B))) {
MachineBasicBlock *SB = DTN->getBlock();
collectInBlock(SB, AVs);
}

Expand Down Expand Up @@ -1422,9 +1419,9 @@ bool HexagonGenInsert::generateInserts() {

bool HexagonGenInsert::removeDeadCode(MachineDomTreeNode *N) {
bool Changed = false;
typedef GraphTraits<MachineDomTreeNode*> GTN;
for (auto I = GTN::child_begin(N), E = GTN::child_end(N); I != E; ++I)
Changed |= removeDeadCode(*I);

for (auto *DTN : graph_children<MachineDomTreeNode*>(N))
Changed |= removeDeadCode(DTN);

MachineBasicBlock *B = N->getBlock();
std::vector<MachineInstr*> Instrs;
Expand Down

0 comments on commit c0fb958

Please sign in to comment.