Index: include/llvm/ADT/GraphTraits.h =================================================================== --- include/llvm/ADT/GraphTraits.h +++ include/llvm/ADT/GraphTraits.h @@ -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... @@ -86,6 +88,33 @@ // inverse falls back to the original graph. template struct GraphTraits>> : GraphTraits {}; +// Provide iterator ranges for the graph traits nodes and children +template +iterator_range::nodes_iterator> +nodes(const GraphType &G) { + return make_range(GraphTraits::nodes_begin(G), + GraphTraits::nodes_end(G)); +} +template +iterator_range>::nodes_iterator> +inverse_nodes(const GraphType &G) { + return make_range(GraphTraits>::nodes_begin(G), + GraphTraits>::nodes_end(G)); +} + +template +iterator_range::ChildIteratorType> +children(const typename GraphTraits::NodeRef &G) { + return make_range(GraphTraits::child_begin(G), + GraphTraits::child_end(G)); +} + +template +iterator_range>::ChildIteratorType> +inverse_children(const typename GraphTraits::NodeRef &G) { + return make_range(GraphTraits>::child_begin(G), + GraphTraits>::child_end(G)); +} } // End llvm namespace #endif Index: include/llvm/Support/GenericDomTree.h =================================================================== --- include/llvm/Support/GenericDomTree.h +++ include/llvm/Support/GenericDomTree.h @@ -782,11 +782,9 @@ Calculate(*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 : nodes(&F)) + if (TraitsTy::child_begin(Node) == TraitsTy::child_end(Node)) + addRoot(Node); Calculate>(*this, F); } Index: include/llvm/Support/GenericDomTreeConstruction.h =================================================================== --- include/llvm/Support/GenericDomTreeConstruction.h +++ include/llvm/Support/GenericDomTreeConstruction.h @@ -200,17 +200,12 @@ // initialize the semi dominator to point to the parent node WInfo.Semi = WInfo.Parent; - typedef GraphTraits > 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_children(W)) + if (DT.Info.count(N)) { // Only if this predecessor is reachable! unsigned SemiU = DT.Info[Eval(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 Index: lib/Analysis/IteratedDominanceFrontier.cpp =================================================================== --- lib/Analysis/IteratedDominanceFrontier.cpp +++ lib/Analysis/IteratedDominanceFrontier.cpp @@ -64,10 +64,7 @@ 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::child_begin(BB), - End = GraphTraits::child_end(BB); - SuccIter != End; ++SuccIter) { - BasicBlock *Succ = *SuccIter; + for (auto *Succ : children(BB)) { DomTreeNode *SuccNode = DT.getNode(Succ); // Quickly skip all CFG edges that are also dominator tree edges instead Index: lib/Target/AMDGPU/AMDILCFGStructurizer.cpp =================================================================== --- lib/Target/AMDGPU/AMDILCFGStructurizer.cpp +++ lib/Target/AMDGPU/AMDILCFGStructurizer.cpp @@ -911,11 +911,8 @@ } } - //walk through all the block in func to check for unreachable - typedef GraphTraits 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 : nodes(MF)) { SccNum = getSCCNum(MBB); if (SccNum == INVALIDSCCNUM) dbgs() << "unreachable block BB" << MBB->getNumber() << "\n"; @@ -1081,13 +1078,9 @@ MachineBasicBlock *ExitBlk = *ExitBlks.begin(); assert(ExitBlk && "Loop has several exit block"); MBBVector LatchBlks; - typedef GraphTraits> 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_children(LoopHeader)) + if (LoopRep->contains(LB)) + LatchBlks.push_back(LB); for (unsigned i = 0, e = ExitingMBBs.size(); i < e; ++i) mergeLoopbreakBlock(ExitingMBBs[i], ExitBlk); Index: lib/Target/Hexagon/HexagonBitSimplify.cpp =================================================================== --- lib/Target/Hexagon/HexagonBitSimplify.cpp +++ lib/Target/Hexagon/HexagonBitSimplify.cpp @@ -249,8 +249,6 @@ bool HexagonBitSimplify::visitBlock(MachineBasicBlock &B, Transformation &T, RegisterSet &AVs) { - MachineDomTreeNode *N = MDT->getNode(&B); - typedef GraphTraits GTN; bool Changed = false; if (T.TopDown) @@ -262,10 +260,9 @@ 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 : children(MDT->getNode(&B))) + Changed |= visitBlock(*(DTN->getBlock()), T, NewAVs); + if (!T.TopDown) Changed |= T.processBlock(B, AVs); @@ -984,9 +981,9 @@ bool DeadCodeElimination::runOnNode(MachineDomTreeNode *N) { bool Changed = false; - typedef GraphTraits GTN; - for (auto I = GTN::child_begin(N), E = GTN::child_end(N); I != E; ++I) - Changed |= runOnNode(*I); + + for (auto *DTN : children(N)) + Changed |= runOnNode(DTN); MachineBasicBlock *B = N->getBlock(); std::vector Instrs; Index: lib/Target/Hexagon/HexagonCommonGEP.cpp =================================================================== --- lib/Target/Hexagon/HexagonCommonGEP.cpp +++ lib/Target/Hexagon/HexagonCommonGEP.cpp @@ -315,11 +315,8 @@ // visited". Order.push_back(Root); - DomTreeNode *DTN = DT->getNode(Root); - typedef GraphTraits 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 : children(DT->getNode(Root))) + getBlockTraversalOrder(DTN->getBlock(), Order); } bool HexagonCommonGEP::isHandledGepForm(GetElementPtrInst *GepI) { @@ -1235,11 +1232,8 @@ for (unsigned i = 0; i < BO.size(); ++i) { BasicBlock *B = cast(BO[i]); - DomTreeNode *N = DT->getNode(B); - typedef GraphTraits 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 : children(DT->getNode(B))) + BO.push_back(DTN->getBlock()); } for (unsigned i = BO.size(); i > 0; --i) { Index: lib/Target/Hexagon/HexagonGenExtract.cpp =================================================================== --- lib/Target/Hexagon/HexagonGenExtract.cpp +++ lib/Target/Hexagon/HexagonGenExtract.cpp @@ -221,11 +221,8 @@ bool HexagonGenExtract::visitBlock(BasicBlock *B) { // Depth-first, bottom-up traversal. - DomTreeNode *DTN = DT->getNode(B); - typedef GraphTraits 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 : children(DT->getNode(B))) + visitBlock(DTN->getBlock()); // Allow limiting the number of generated extracts for debugging purposes. bool HasCutoff = ExtractCutoff.getPosition(); Index: lib/Target/Hexagon/HexagonGenInsert.cpp =================================================================== --- lib/Target/Hexagon/HexagonGenInsert.cpp +++ lib/Target/Hexagon/HexagonGenInsert.cpp @@ -947,11 +947,8 @@ BlockDefs.insert(InsDefs); } - MachineDomTreeNode *N = MDT->getNode(B); - typedef GraphTraits 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 : children(MDT->getNode(B))) { + MachineBasicBlock *SB = DTN->getBlock(); collectInBlock(SB, AVs); } @@ -1422,9 +1419,9 @@ bool HexagonGenInsert::removeDeadCode(MachineDomTreeNode *N) { bool Changed = false; - typedef GraphTraits GTN; - for (auto I = GTN::child_begin(N), E = GTN::child_end(N); I != E; ++I) - Changed |= removeDeadCode(*I); + + for (auto *DTN : children(N)) + Changed |= removeDeadCode(DTN); MachineBasicBlock *B = N->getBlock(); std::vector Instrs;