Add range iterators for post order and inverse post order. Use them

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235026 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Berlin 2015-04-15 17:41:42 +00:00
parent 7f5438e2ca
commit 7871b86660
8 changed files with 47 additions and 37 deletions

View File

@ -18,6 +18,7 @@
#include "llvm/ADT/GraphTraits.h" #include "llvm/ADT/GraphTraits.h"
#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/iterator_range.h"
#include <set> #include <set>
#include <vector> #include <vector>
@ -178,6 +179,10 @@ po_iterator<T> po_begin(T G) { return po_iterator<T>::begin(G); }
template <class T> template <class T>
po_iterator<T> po_end (T G) { return po_iterator<T>::end(G); } po_iterator<T> po_end (T G) { return po_iterator<T>::end(G); }
template <class T> iterator_range<po_iterator<T>> post_order(T G) {
return iterator_range<po_iterator<T>>(po_begin(G), po_end(G));
}
// Provide global definitions of external postorder iterators... // Provide global definitions of external postorder iterators...
template<class T, class SetType=std::set<typename GraphTraits<T>::NodeType*> > template<class T, class SetType=std::set<typename GraphTraits<T>::NodeType*> >
struct po_ext_iterator : public po_iterator<T, SetType, true> { struct po_ext_iterator : public po_iterator<T, SetType, true> {
@ -195,6 +200,12 @@ po_ext_iterator<T, SetType> po_ext_end(T G, SetType &S) {
return po_ext_iterator<T, SetType>::end(G, S); return po_ext_iterator<T, SetType>::end(G, S);
} }
template <class T, class SetType>
iterator_range<po_ext_iterator<T, SetType>> post_order_ext(T G, SetType &S) {
return iterator_range<po_ext_iterator<T, SetType>>(po_ext_begin(G, S),
po_ext_end(G, S));
}
// Provide global definitions of inverse post order iterators... // Provide global definitions of inverse post order iterators...
template <class T, template <class T,
class SetType = std::set<typename GraphTraits<T>::NodeType*>, class SetType = std::set<typename GraphTraits<T>::NodeType*>,
@ -214,6 +225,11 @@ ipo_iterator<T> ipo_end(T G){
return ipo_iterator<T>::end(G); return ipo_iterator<T>::end(G);
} }
template <class T>
iterator_range<ipo_iterator<T>> inverse_post_order(T G, bool Reverse = false) {
return iterator_range<ipo_iterator<T>>(ipo_begin(G, Reverse), ipo_end(G));
}
// Provide global definitions of external inverse postorder iterators... // Provide global definitions of external inverse postorder iterators...
template <class T, template <class T,
class SetType = std::set<typename GraphTraits<T>::NodeType*> > class SetType = std::set<typename GraphTraits<T>::NodeType*> >
@ -234,6 +250,13 @@ ipo_ext_iterator<T, SetType> ipo_ext_end(T G, SetType &S) {
return ipo_ext_iterator<T, SetType>::end(G, S); return ipo_ext_iterator<T, SetType>::end(G, S);
} }
template <class T, class SetType>
iterator_range<ipo_ext_iterator<T, SetType>>
inverse_post_order_ext(T G, SetType &S) {
return iterator_range<ipo_ext_iterator<T, SetType>>(ipo_ext_begin(G, S),
ipo_ext_end(G, S));
}
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
// Reverse Post Order CFG iterator code // Reverse Post Order CFG iterator code
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//

View File

@ -498,10 +498,9 @@ Analyze(DominatorTreeBase<BlockT> &DomTree) {
// Postorder traversal of the dominator tree. // Postorder traversal of the dominator tree.
DomTreeNodeBase<BlockT>* DomRoot = DomTree.getRootNode(); DomTreeNodeBase<BlockT>* DomRoot = DomTree.getRootNode();
for (po_iterator<DomTreeNodeBase<BlockT>*> DomIter = po_begin(DomRoot), for (auto DomNode : post_order(DomRoot)) {
DomEnd = po_end(DomRoot); DomIter != DomEnd; ++DomIter) {
BlockT *Header = DomIter->getBlock(); BlockT *Header = DomNode->getBlock();
SmallVector<BlockT *, 4> Backedges; SmallVector<BlockT *, 4> Backedges;
// Check each predecessor of the potential loop header. // Check each predecessor of the potential loop header.

View File

@ -714,10 +714,8 @@ void RegionInfoBase<Tr>::scanForRegions(FuncT &F, BBtoBBMap *ShortCut) {
// regions from the bottom of the dominance tree. If the small regions are // regions from the bottom of the dominance tree. If the small regions are
// detected first, detection of bigger regions is faster, as we can jump // detected first, detection of bigger regions is faster, as we can jump
// over the small regions. // over the small regions.
for (po_iterator<DomTreeNodeT *> FI = po_begin(N), FE = po_end(N); FI != FE; for (auto DomNode : post_order(N))
++FI) { findRegionsWithEntry(DomNode->getBlock(), ShortCut);
findRegionsWithEntry(FI->getBlock(), ShortCut);
}
} }
template <class Tr> template <class Tr>

View File

@ -507,25 +507,23 @@ bool BranchProbabilityInfo::runOnFunction(Function &F) {
// Walk the basic blocks in post-order so that we can build up state about // Walk the basic blocks in post-order so that we can build up state about
// the successors of a block iteratively. // the successors of a block iteratively.
for (po_iterator<BasicBlock *> I = po_begin(&F.getEntryBlock()), for (auto BB : post_order(&F.getEntryBlock())) {
E = po_end(&F.getEntryBlock()); DEBUG(dbgs() << "Computing probabilities for " << BB->getName() << "\n");
I != E; ++I) { if (calcUnreachableHeuristics(BB))
DEBUG(dbgs() << "Computing probabilities for " << I->getName() << "\n");
if (calcUnreachableHeuristics(*I))
continue; continue;
if (calcMetadataWeights(*I)) if (calcMetadataWeights(BB))
continue; continue;
if (calcColdCallHeuristics(*I)) if (calcColdCallHeuristics(BB))
continue; continue;
if (calcLoopBranchHeuristics(*I)) if (calcLoopBranchHeuristics(BB))
continue; continue;
if (calcPointerHeuristics(*I)) if (calcPointerHeuristics(BB))
continue; continue;
if (calcZeroHeuristics(*I)) if (calcZeroHeuristics(BB))
continue; continue;
if (calcFloatingPointHeuristics(*I)) if (calcFloatingPointHeuristics(BB))
continue; continue;
calcInvokeHeuristics(*I); calcInvokeHeuristics(BB);
} }
PostDominatedByUnreachable.clear(); PostDominatedByUnreachable.clear();

View File

@ -797,9 +797,8 @@ bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) {
// if-conversion in a single pass. The tryConvertIf() function may erase // if-conversion in a single pass. The tryConvertIf() function may erase
// blocks, but only blocks dominated by the head block. This makes it safe to // blocks, but only blocks dominated by the head block. This makes it safe to
// update the dominator tree while the post-order iterator is still active. // update the dominator tree while the post-order iterator is still active.
for (po_iterator<MachineDominatorTree*> for (auto DomNode : post_order(DomTree))
I = po_begin(DomTree), E = po_end(DomTree); I != E; ++I) if (tryConvertIf(DomNode->getBlock()))
if (tryConvertIf(I->getBlock()))
Changed = true; Changed = true;
return Changed; return Changed;

View File

@ -463,13 +463,11 @@ void MachineTraceMetrics::Ensemble::computeTrace(const MachineBasicBlock *MBB) {
// Run an upwards post-order search for the trace start. // Run an upwards post-order search for the trace start.
Bounds.Downward = false; Bounds.Downward = false;
Bounds.Visited.clear(); Bounds.Visited.clear();
typedef ipo_ext_iterator<const MachineBasicBlock*, LoopBounds> UpwardPO; for (auto I : inverse_post_order_ext(MBB, Bounds)) {
for (UpwardPO I = ipo_ext_begin(MBB, Bounds), E = ipo_ext_end(MBB, Bounds);
I != E; ++I) {
DEBUG(dbgs() << " pred for BB#" << I->getNumber() << ": "); DEBUG(dbgs() << " pred for BB#" << I->getNumber() << ": ");
TraceBlockInfo &TBI = BlockInfo[I->getNumber()]; TraceBlockInfo &TBI = BlockInfo[I->getNumber()];
// All the predecessors have been visited, pick the preferred one. // All the predecessors have been visited, pick the preferred one.
TBI.Pred = pickTracePred(*I); TBI.Pred = pickTracePred(I);
DEBUG({ DEBUG({
if (TBI.Pred) if (TBI.Pred)
dbgs() << "BB#" << TBI.Pred->getNumber() << '\n'; dbgs() << "BB#" << TBI.Pred->getNumber() << '\n';
@ -477,19 +475,17 @@ void MachineTraceMetrics::Ensemble::computeTrace(const MachineBasicBlock *MBB) {
dbgs() << "null\n"; dbgs() << "null\n";
}); });
// The trace leading to I is now known, compute the depth resources. // The trace leading to I is now known, compute the depth resources.
computeDepthResources(*I); computeDepthResources(I);
} }
// Run a downwards post-order search for the trace end. // Run a downwards post-order search for the trace end.
Bounds.Downward = true; Bounds.Downward = true;
Bounds.Visited.clear(); Bounds.Visited.clear();
typedef po_ext_iterator<const MachineBasicBlock*, LoopBounds> DownwardPO; for (auto I : post_order_ext(MBB, Bounds)) {
for (DownwardPO I = po_ext_begin(MBB, Bounds), E = po_ext_end(MBB, Bounds);
I != E; ++I) {
DEBUG(dbgs() << " succ for BB#" << I->getNumber() << ": "); DEBUG(dbgs() << " succ for BB#" << I->getNumber() << ": ");
TraceBlockInfo &TBI = BlockInfo[I->getNumber()]; TraceBlockInfo &TBI = BlockInfo[I->getNumber()];
// All the successors have been visited, pick the preferred one. // All the successors have been visited, pick the preferred one.
TBI.Succ = pickTraceSucc(*I); TBI.Succ = pickTraceSucc(I);
DEBUG({ DEBUG({
if (TBI.Succ) if (TBI.Succ)
dbgs() << "BB#" << TBI.Succ->getNumber() << '\n'; dbgs() << "BB#" << TBI.Succ->getNumber() << '\n';
@ -497,7 +493,7 @@ void MachineTraceMetrics::Ensemble::computeTrace(const MachineBasicBlock *MBB) {
dbgs() << "null\n"; dbgs() << "null\n";
}); });
// The trace leaving I is now known, compute the height resources. // The trace leaving I is now known, compute the height resources.
computeHeightResources(*I); computeHeightResources(I);
} }
} }

View File

@ -652,8 +652,7 @@ void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
if (FT->isVarArg() && !MMI->usesVAFloatArgument()) { if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) { for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
Type* T = I.getArgOperand(i)->getType(); Type* T = I.getArgOperand(i)->getType();
for (po_iterator<Type*> i = po_begin(T), e = po_end(T); for (auto i : post_order(T)) {
i != e; ++i) {
if (i->isFloatingPointTy()) { if (i->isFloatingPointTy()) {
MMI->setUsesVAFloatArgument(true); MMI->setUsesVAFloatArgument(true);
return; return;

View File

@ -3101,9 +3101,7 @@ struct SLPVectorizer : public FunctionPass {
// delete instructions. // delete instructions.
// Scan the blocks in the function in post order. // Scan the blocks in the function in post order.
for (po_iterator<BasicBlock*> it = po_begin(&F.getEntryBlock()), for (auto BB : post_order(&F.getEntryBlock())) {
e = po_end(&F.getEntryBlock()); it != e; ++it) {
BasicBlock *BB = *it;
// Vectorize trees that end at stores. // Vectorize trees that end at stores.
if (unsigned count = collectStores(BB, R)) { if (unsigned count = collectStores(BB, R)) {
(void)count; (void)count;