[llvm] r206016 - Implement depth_first and inverse_depth_first range factory functions.

Jim Grosbach grosbach at apple.com
Thu Apr 10 19:05:06 PDT 2014


On Apr 10, 2014, at 6:50 PM, David Blaikie <dblaikie at gmail.com> wrote:

> Author: dblaikie
> Date: Thu Apr 10 20:50:01 2014
> New Revision: 206016
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=206016&view=rev
> Log:
> Implement depth_first and inverse_depth_first range factory functions.
> 
> Also updated as many loops as I could find using df_begin/idf_begin -
> strangely I found no uses of idf_begin. Is that just used out of tree?
> 
> Also a few places couldn't use df_begin because either they used the
> member functions of the depth first iterators or had specific ordering
> constraints (I added a comment in the latter case).
> 
> Based on a patch by Jim Grosbach. (Jim - you just had iterator_range<T>
> where you needed iterator_range<idf_iterator<T>>)
> 

Aha! I should have known. When template code doesn’t work, add more <> pairs until it does.

Thanks, Dave.

-Jim



> Modified:
>    llvm/trunk/include/llvm/ADT/DepthFirstIterator.h
>    llvm/trunk/include/llvm/Analysis/LoopInfoImpl.h
>    llvm/trunk/lib/CodeGen/StackColoring.cpp
>    llvm/trunk/lib/Target/ARM64/ARM64ConditionalCompares.cpp
>    llvm/trunk/lib/Target/R600/AMDILCFGStructurizer.cpp
>    llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp
>    llvm/trunk/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp
>    llvm/trunk/lib/Transforms/Instrumentation/MemorySanitizer.cpp
>    llvm/trunk/lib/Transforms/Scalar/GVN.cpp
>    llvm/trunk/lib/Transforms/Utils/SimplifyInstructions.cpp
> 
> Modified: llvm/trunk/include/llvm/ADT/DepthFirstIterator.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/DepthFirstIterator.h?rev=206016&r1=206015&r2=206016&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/DepthFirstIterator.h (original)
> +++ llvm/trunk/include/llvm/ADT/DepthFirstIterator.h Thu Apr 10 20:50:01 2014
> @@ -33,6 +33,7 @@
> #ifndef LLVM_ADT_DEPTHFIRSTITERATOR_H
> #define LLVM_ADT_DEPTHFIRSTITERATOR_H
> 
> +#include "llvm/ADT/iterator_range.h"
> #include "llvm/ADT/GraphTraits.h"
> #include "llvm/ADT/PointerIntPair.h"
> #include "llvm/ADT/SmallPtrSet.h"
> @@ -207,6 +208,12 @@ df_iterator<T> df_end(const T& G) {
>   return df_iterator<T>::end(G);
> }
> 
> +// Provide an accessor method to use them in range-based patterns.
> +template <class T>
> +iterator_range<df_iterator<T>> depth_first(const T& G) {
> +  return iterator_range<df_iterator<T>>(df_begin(G), df_end(G));
> +}
> +
> // Provide global definitions of external depth first iterators...
> template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
> struct df_ext_iterator : public df_iterator<T, SetTy, true> {
> @@ -244,6 +251,12 @@ idf_iterator<T> idf_end(const T& G){
>   return idf_iterator<T>::end(Inverse<T>(G));
> }
> 
> +// Provide an accessor method to use them in range-based patterns.
> +template <class T>
> +iterator_range<idf_iterator<T>> inverse_depth_first(const T& G) {
> +  return iterator_range<idf_iterator<T>>(idf_begin(G), idf_end(G));
> +}
> +
> // Provide global definitions of external inverse depth first iterators...
> template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
> struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
> 
> Modified: llvm/trunk/include/llvm/Analysis/LoopInfoImpl.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/LoopInfoImpl.h?rev=206016&r1=206015&r2=206016&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/Analysis/LoopInfoImpl.h (original)
> +++ llvm/trunk/include/llvm/Analysis/LoopInfoImpl.h Thu Apr 10 20:50:01 2014
> @@ -270,11 +270,10 @@ void LoopBase<BlockT, LoopT>::verifyLoop
>       // though it is permitted if the predecessor is not itself actually
>       // reachable.
>       BlockT *EntryBB = BB->getParent()->begin();
> -        for (df_iterator<BlockT *> NI = df_begin(EntryBB),
> -               NE = df_end(EntryBB); NI != NE; ++NI)
> -          for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
> -            assert(*NI != OutsideLoopPreds[i] &&
> -                   "Loop has multiple entry points!");
> +      for (BlockT *CB : depth_first(EntryBB))
> +        for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
> +          assert(CB != OutsideLoopPreds[i] &&
> +                 "Loop has multiple entry points!");
>     }
>     assert(HasInsideLoopPreds && "Loop block has no in-loop predecessors!");
>     assert(HasInsideLoopSuccs && "Loop block has no in-loop successors!");
> 
> Modified: llvm/trunk/lib/CodeGen/StackColoring.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/StackColoring.cpp?rev=206016&r1=206015&r2=206016&view=diff
> ==============================================================================
> --- llvm/trunk/lib/CodeGen/StackColoring.cpp (original)
> +++ llvm/trunk/lib/CodeGen/StackColoring.cpp Thu Apr 10 20:50:01 2014
> @@ -193,12 +193,11 @@ void StackColoring::getAnalysisUsage(Ana
> }
> 
> void StackColoring::dump() const {
> -  for (df_iterator<MachineFunction*> FI = df_begin(MF), FE = df_end(MF);
> -       FI != FE; ++FI) {
> -    DEBUG(dbgs()<<"Inspecting block #"<<BasicBlocks.lookup(*FI)<<
> -          " ["<<FI->getName()<<"]\n");
> +  for (MachineBasicBlock *MBB : depth_first(MF)) {
> +    DEBUG(dbgs() << "Inspecting block #" << BasicBlocks.lookup(MBB) << " ["
> +                 << MBB->getName() << "]\n");
> 
> -    LivenessMap::const_iterator BI = BlockLiveness.find(*FI);
> +    LivenessMap::const_iterator BI = BlockLiveness.find(MBB);
>     assert(BI != BlockLiveness.end() && "Block not found");
>     const BlockLifetimeInfo &BlockInfo = BI->second;
> 
> @@ -231,20 +230,19 @@ unsigned StackColoring::collectMarkers(u
>   // NOTE: We use the a reverse-post-order iteration to ensure that we obtain a
>   // deterministic numbering, and because we'll need a post-order iteration
>   // later for solving the liveness dataflow problem.
> -  for (df_iterator<MachineFunction*> FI = df_begin(MF), FE = df_end(MF);
> -       FI != FE; ++FI) {
> +  for (MachineBasicBlock *MBB : depth_first(MF)) {
> 
>     // Assign a serial number to this basic block.
> -    BasicBlocks[*FI] = BasicBlockNumbering.size();
> -    BasicBlockNumbering.push_back(*FI);
> +    BasicBlocks[MBB] = BasicBlockNumbering.size();
> +    BasicBlockNumbering.push_back(MBB);
> 
>     // Keep a reference to avoid repeated lookups.
> -    BlockLifetimeInfo &BlockInfo = BlockLiveness[*FI];
> +    BlockLifetimeInfo &BlockInfo = BlockLiveness[MBB];
> 
>     BlockInfo.Begin.resize(NumSlot);
>     BlockInfo.End.resize(NumSlot);
> 
> -    for (MachineInstr &MI : **FI) {
> +    for (MachineInstr &MI : *MBB) {
>       if (MI.getOpcode() != TargetOpcode::LIFETIME_START &&
>           MI.getOpcode() != TargetOpcode::LIFETIME_END)
>         continue;
> 
> Modified: llvm/trunk/lib/Target/ARM64/ARM64ConditionalCompares.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM64/ARM64ConditionalCompares.cpp?rev=206016&r1=206015&r2=206016&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/ARM64/ARM64ConditionalCompares.cpp (original)
> +++ llvm/trunk/lib/Target/ARM64/ARM64ConditionalCompares.cpp Thu Apr 10 20:50:01 2014
> @@ -908,7 +908,7 @@ bool ARM64ConditionalCompares::runOnMach
>   // Note that updateDomTree() modifies the children of the DomTree node
>   // currently being visited. The df_iterator supports that; it doesn't look at
>   // child_begin() / child_end() until after a node has been visited.
> -  for (auto *I : make_range(df_begin(DomTree), df_end(DomTree)))
> +  for (auto *I : depth_first(DomTree))
>     if (tryConvert(I->getBlock()))
>       Changed = true;
> 
> 
> Modified: llvm/trunk/lib/Target/R600/AMDILCFGStructurizer.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/AMDILCFGStructurizer.cpp?rev=206016&r1=206015&r2=206016&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/R600/AMDILCFGStructurizer.cpp (original)
> +++ llvm/trunk/lib/Target/R600/AMDILCFGStructurizer.cpp Thu Apr 10 20:50:01 2014
> @@ -1075,13 +1075,11 @@ int AMDGPUCFGStructurizer::ifPatternMatc
> 
> int AMDGPUCFGStructurizer::loopendPatternMatch() {
>   std::vector<MachineLoop *> NestedLoops;
> -  for (MachineLoopInfo::iterator It = MLI->begin(), E = MLI->end();
> -      It != E; ++It) {
> -    df_iterator<MachineLoop *> LpIt = df_begin(*It),
> -        LpE = df_end(*It);
> -    for (; LpIt != LpE; ++LpIt)
> -      NestedLoops.push_back(*LpIt);
> -  }
> +  for (MachineLoopInfo::iterator It = MLI->begin(), E = MLI->end(); It != E;
> +       ++It)
> +    for (MachineLoop *ML : depth_first(*It))
> +      NestedLoops.push_back(ML);
> +
>   if (NestedLoops.size() == 0)
>     return 0;
> 
> 
> Modified: llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp?rev=206016&r1=206015&r2=206016&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp (original)
> +++ llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp Thu Apr 10 20:50:01 2014
> @@ -443,11 +443,9 @@ struct FunctionStackPoisoner : public In
>   bool runOnFunction() {
>     if (!ClStack) return false;
>     // Collect alloca, ret, lifetime instructions etc.
> -    for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
> -         DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) {
> -      BasicBlock *BB = *DI;
> +    for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
>       visit(*BB);
> -    }
> +
>     if (AllocaVec.empty()) return false;
> 
>     initializeCallbacks(*F.getParent());
> 
> Modified: llvm/trunk/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp?rev=206016&r1=206015&r2=206016&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp (original)
> +++ llvm/trunk/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp Thu Apr 10 20:50:01 2014
> @@ -680,9 +680,8 @@ bool DataFlowSanitizer::runOnModule(Modu
> 
>     // DFSanVisitor may create new basic blocks, which confuses df_iterator.
>     // Build a copy of the list before iterating over it.
> -    llvm::SmallVector<BasicBlock *, 4> BBList;
> -    std::copy(df_begin(&(*i)->getEntryBlock()), df_end(&(*i)->getEntryBlock()),
> -              std::back_inserter(BBList));
> +    llvm::SmallVector<BasicBlock *, 4> BBList(
> +        depth_first(&(*i)->getEntryBlock()));
> 
>     for (llvm::SmallVector<BasicBlock *, 4>::iterator i = BBList.begin(),
>                                                       e = BBList.end();
> 
> Modified: llvm/trunk/lib/Transforms/Instrumentation/MemorySanitizer.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/MemorySanitizer.cpp?rev=206016&r1=206015&r2=206016&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Instrumentation/MemorySanitizer.cpp (original)
> +++ llvm/trunk/lib/Transforms/Instrumentation/MemorySanitizer.cpp Thu Apr 10 20:50:01 2014
> @@ -662,11 +662,9 @@ struct MemorySanitizerVisitor : public I
>     // Iterate all BBs in depth-first order and create shadow instructions
>     // for all instructions (where applicable).
>     // For PHI nodes we create dummy shadow PHIs which will be finalized later.
> -    for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
> -         DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) {
> -      BasicBlock *BB = *DI;
> +    for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
>       visit(*BB);
> -    }
> +
> 
>     // Finalize PHI nodes.
>     for (size_t i = 0, n = ShadowPHINodes.size(); i < n; i++) {
> 
> Modified: llvm/trunk/lib/Transforms/Scalar/GVN.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/GVN.cpp?rev=206016&r1=206015&r2=206016&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Scalar/GVN.cpp (original)
> +++ llvm/trunk/lib/Transforms/Scalar/GVN.cpp Thu Apr 10 20:50:01 2014
> @@ -2421,10 +2421,7 @@ bool GVN::processBlock(BasicBlock *BB) {
> bool GVN::performPRE(Function &F) {
>   bool Changed = false;
>   SmallVector<std::pair<Value*, BasicBlock*>, 8> predMap;
> -  for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
> -       DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) {
> -    BasicBlock *CurrentBlock = *DI;
> -
> +  for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) {
>     // Nothing to PRE in the entry block.
>     if (CurrentBlock == &F.getEntryBlock()) continue;
> 
> @@ -2637,9 +2634,8 @@ bool GVN::iterateOnFunction(Function &F)
>   //
>   std::vector<BasicBlock *> BBVect;
>   BBVect.reserve(256);
> -  for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()),
> -       DE = df_end(DT->getRootNode()); DI != DE; ++DI)
> -    BBVect.push_back(DI->getBlock());
> +  for (DomTreeNode *x : depth_first(DT->getRootNode()))
> +    BBVect.push_back(x->getBlock());
> 
>   for (std::vector<BasicBlock *>::iterator I = BBVect.begin(), E = BBVect.end();
>        I != E; I++)
> 
> Modified: llvm/trunk/lib/Transforms/Utils/SimplifyInstructions.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyInstructions.cpp?rev=206016&r1=206015&r2=206016&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Utils/SimplifyInstructions.cpp (original)
> +++ llvm/trunk/lib/Transforms/Utils/SimplifyInstructions.cpp Thu Apr 10 20:50:01 2014
> @@ -55,9 +55,10 @@ namespace {
>       bool Changed = false;
> 
>       do {
> -        for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
> -             DE = df_end(&F.getEntryBlock()); DI != DE; ++DI)
> -          for (BasicBlock::iterator BI = DI->begin(), BE = DI->end(); BI != BE;) {
> +        for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
> +          // Here be subtlety: the iterator must be incremented before the loop
> +          // body (not sure why), so a range-for loop won't work here.
> +          for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
>             Instruction *I = BI++;
>             // The first time through the loop ToSimplify is empty and we try to
>             // simplify all instructions.  On later iterations ToSimplify is not
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits





More information about the llvm-commits mailing list