Skip to content

Commit

Permalink
[C++11] Add predecessors(BasicBlock *) / successors(BasicBlock *) ite…
Browse files Browse the repository at this point in the history
…rator ranges.

Summary: This patch introduces two new iterator ranges and updates existing code to use it.  No functional change intended.

Test Plan: All tests (make check-all) still pass.

Reviewers: dblaikie

Reviewed By: dblaikie

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D4481

llvm-svn: 213474
  • Loading branch information
manueljacob committed Jul 20, 2014
1 parent 4100ebd commit d11beff
Show file tree
Hide file tree
Showing 41 changed files with 166 additions and 226 deletions.
5 changes: 2 additions & 3 deletions llvm/docs/ProgrammersManual.rst
Expand Up @@ -1854,12 +1854,11 @@ iterate over all predecessors of BB:
#include "llvm/Support/CFG.h"
BasicBlock *BB = ...;
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
BasicBlock *Pred = *PI;
for (BasicBlock *Pred : predecessors(BB)) {
// ...
}
Similarly, to iterate over successors use ``succ_iterator/succ_begin/succ_end``.
Similarly, to iterate over successors use ``successors``.

.. _simplechanges:

Expand Down
12 changes: 12 additions & 0 deletions llvm/include/llvm/IR/CFG.h
Expand Up @@ -93,6 +93,12 @@ inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
inline const_pred_iterator pred_end(const BasicBlock *BB) {
return const_pred_iterator(BB, true);
}
inline iterator_range<pred_iterator> predecessors(BasicBlock *BB) {
return make_range(pred_begin(BB), pred_end(BB));
}
inline iterator_range<const_pred_iterator> predecessors(const BasicBlock *BB) {
return make_range(pred_begin(BB), pred_end(BB));
}



Expand Down Expand Up @@ -257,6 +263,12 @@ inline succ_iterator succ_end(BasicBlock *BB) {
inline succ_const_iterator succ_end(const BasicBlock *BB) {
return succ_const_iterator(BB->getTerminator(), true);
}
inline iterator_range<succ_iterator> successors(BasicBlock *BB) {
return make_range(succ_begin(BB), succ_end(BB));
}
inline iterator_range<succ_const_iterator> successors(const BasicBlock *BB) {
return make_range(succ_begin(BB), succ_end(BB));
}

template <typename T, typename U> struct isPodLike<SuccIterator<T, U> > {
static const bool value = isPodLike<T>::value;
Expand Down
8 changes: 3 additions & 5 deletions llvm/lib/Analysis/BranchProbabilityInfo.cpp
Expand Up @@ -530,9 +530,8 @@ void BranchProbabilityInfo::print(raw_ostream &OS, const Module *) const {
assert(LastF && "Cannot print prior to running over a function");
for (Function::const_iterator BI = LastF->begin(), BE = LastF->end();
BI != BE; ++BI) {
for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI);
SI != SE; ++SI) {
printEdgeProbability(OS << " ", BI, *SI);
for (const BasicBlock *Succ : successors(BI)) {
printEdgeProbability(OS << " ", BI, Succ);
}
}
}
Expand Down Expand Up @@ -563,8 +562,7 @@ BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
uint32_t MaxWeight = 0;
BasicBlock *MaxSucc = nullptr;

for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
BasicBlock *Succ = *I;
for (BasicBlock *Succ : successors(BB)) {
uint32_t Weight = getEdgeWeight(BB, Succ);
uint32_t PrevSum = Sum;

Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Analysis/Interval.cpp
Expand Up @@ -29,9 +29,8 @@ using namespace llvm;
bool Interval::isLoop() const {
// There is a loop in this interval iff one of the predecessors of the header
// node lives in the interval.
for (::pred_iterator I = ::pred_begin(HeaderNode), E = ::pred_end(HeaderNode);
I != E; ++I)
if (contains(*I))
for (BasicBlock *Pred : predecessors(HeaderNode))
if (contains(Pred))
return true;
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/LazyValueInfo.cpp
Expand Up @@ -625,9 +625,9 @@ bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV,
// Loop over all of our predecessors, merging what we know from them into
// result.
bool EdgesMissing = false;
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
for (BasicBlock *Pred : predecessors(BB)) {
LVILatticeVal EdgeResult;
EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
EdgesMissing |= !getEdgeValue(Val, Pred, BB, EdgeResult);
if (EdgesMissing)
continue;

Expand Down
19 changes: 9 additions & 10 deletions llvm/lib/Analysis/LoopInfo.cpp
Expand Up @@ -336,9 +336,8 @@ bool Loop::hasDedicatedExits() const {
SmallVector<BasicBlock *, 4> ExitBlocks;
getExitBlocks(ExitBlocks);
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
for (pred_iterator PI = pred_begin(ExitBlocks[i]),
PE = pred_end(ExitBlocks[i]); PI != PE; ++PI)
if (!contains(*PI))
for (BasicBlock *Pred : predecessors(ExitBlocks[i]))
if (!contains(Pred))
return false;
// All the requirements are met.
return true;
Expand All @@ -360,12 +359,12 @@ Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const {
BasicBlock *current = *BI;
switchExitBlocks.clear();

for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) {
for (BasicBlock *Succ : successors(*BI)) {
// If block is inside the loop then it is not a exit block.
if (contains(*I))
if (contains(Succ))
continue;

pred_iterator PI = pred_begin(*I);
pred_iterator PI = pred_begin(Succ);
BasicBlock *firstPred = *PI;

// If current basic block is this exit block's first predecessor
Expand All @@ -379,17 +378,17 @@ Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const {
// then it is possible that there are multiple edges from current block
// to one exit block.
if (std::distance(succ_begin(current), succ_end(current)) <= 2) {
ExitBlocks.push_back(*I);
ExitBlocks.push_back(Succ);
continue;
}

// In case of multiple edges from current block to exit block, collect
// only one edge in ExitBlocks. Use switchExitBlocks to keep track of
// duplicate edges.
if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), Succ)
== switchExitBlocks.end()) {
switchExitBlocks.push_back(*I);
ExitBlocks.push_back(*I);
switchExitBlocks.push_back(Succ);
ExitBlocks.push_back(Succ);
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Expand Up @@ -4512,13 +4512,12 @@ ScalarEvolution::ComputeExitLimit(const Loop *L, BasicBlock *ExitingBlock) {
// lead to the loop header.
bool MustExecuteLoopHeader = true;
BasicBlock *Exit = nullptr;
for (succ_iterator SI = succ_begin(ExitingBlock), SE = succ_end(ExitingBlock);
SI != SE; ++SI)
if (!L->contains(*SI)) {
for (BasicBlock *Succ : successors(ExitingBlock))
if (!L->contains(Succ)) {
if (Exit) // Multiple exit successors.
return getCouldNotCompute();
Exit = *SI;
} else if (*SI != L->getHeader()) {
Exit = Succ;
} else if (Succ != L->getHeader()) {
MustExecuteLoopHeader = false;
}

Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/CodeGen/CodeGenPrepare.cpp
Expand Up @@ -453,8 +453,8 @@ void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) {
for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
PN->addIncoming(InVal, BBPN->getIncomingBlock(i));
} else {
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
PN->addIncoming(InVal, *PI);
for (BasicBlock *Pred : predecessors(BB))
PN->addIncoming(InVal, Pred);
}
}
}
Expand Down Expand Up @@ -977,11 +977,11 @@ bool CodeGenPrepare::DupRetToEnableTailCallOpts(BasicBlock *BB) {
}
} else {
SmallPtrSet<BasicBlock*, 4> VisitedBBs;
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
if (!VisitedBBs.insert(*PI))
for (BasicBlock *Pred : predecessors(BB)) {
if (!VisitedBBs.insert(Pred))
continue;

BasicBlock::InstListType &InstList = (*PI)->getInstList();
BasicBlock::InstListType &InstList = Pred->getInstList();
BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin();
BasicBlock::InstListType::reverse_iterator RE = InstList.rend();
do { ++RI; } while (RI != RE && isa<DbgInfoIntrinsic>(&*RI));
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
Expand Up @@ -1052,9 +1052,8 @@ void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) {

if (OptLevel != CodeGenOpt::None) {
bool AllPredsVisited = true;
for (const_pred_iterator PI = pred_begin(LLVMBB), PE = pred_end(LLVMBB);
PI != PE; ++PI) {
if (!FuncInfo->VisitedBBs.count(*PI)) {
for (const BasicBlock *Pred : predecessors(LLVMBB)) {
if (!FuncInfo->VisitedBBs.count(Pred)) {
AllPredsVisited = false;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/SjLjEHPrepare.cpp
Expand Up @@ -142,8 +142,8 @@ static void MarkBlocksLiveIn(BasicBlock *BB,
if (!LiveBBs.insert(BB))
return; // already been here.

for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
MarkBlocksLiveIn(*PI, LiveBBs);
for (BasicBlock *Pred : predecessors(BB))
MarkBlocksLiveIn(Pred, LiveBBs);
}

/// substituteLPadValues - Substitute the values returned by the landingpad
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/UnreachableBlockElim.cpp
Expand Up @@ -79,8 +79,8 @@ bool UnreachableBlockElim::runOnFunction(Function &F) {
PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
BB->getInstList().pop_front();
}
for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
(*SI)->removePredecessor(BB);
for (BasicBlock *S : successors(BB))
S->removePredecessor(BB);
BB->dropAllReferences();
}

Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/IR/BasicBlock.cpp
Expand Up @@ -321,10 +321,9 @@ BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName) {
// successors. If there were PHI nodes in the successors, then they need to
// know that incoming branches will be from New, not from Old.
//
for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) {
for (BasicBlock *Successor : successors(New)) {
// Loop over any phi nodes in the basic block, updating the BB field of
// incoming values...
BasicBlock *Successor = *I;
PHINode *PN;
for (BasicBlock::iterator II = Successor->begin();
(PN = dyn_cast<PHINode>(II)); ++II) {
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/IR/Dominators.cpp
Expand Up @@ -179,9 +179,7 @@ bool DominatorTree::dominates(const BasicBlockEdge &BBE,
// trivially dominates itself, so we only have to find if it dominates the
// other predecessors. Since the only way out of X is via NormalDest, X can
// only properly dominate a node if NormalDest dominates that node too.
for (const_pred_iterator PI = pred_begin(End), E = pred_end(End);
PI != E; ++PI) {
const BasicBlock *BB = *PI;
for (const BasicBlock *BB : predecessors(End)) {
if (BB == Start)
continue;

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/IR/Verifier.cpp
Expand Up @@ -2107,8 +2107,8 @@ void Verifier::visitLandingPadInst(LandingPadInst &LPI) {

// The landingpad instruction defines its parent as a landing pad block. The
// landing pad block may be branched to only by the unwind edge of an invoke.
for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
const InvokeInst *II = dyn_cast<InvokeInst>((*I)->getTerminator());
for (BasicBlock *Pred : predecessors(BB)) {
const InvokeInst *II = dyn_cast<InvokeInst>(Pred->getTerminator());
Assert1(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
"Block containing LandingPadInst must be jumped to "
"only by the unwind edge of an invoke.", &LPI);
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
Expand Up @@ -473,8 +473,7 @@ bool ArgPromotion::isSafeToPromoteArgument(Argument *Arg,
// Now check every path from the entry block to the load for transparency.
// To do this, we perform a depth first search on the inverse CFG from the
// loading block.
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
BasicBlock *P = *PI;
for (BasicBlock *P : predecessors(BB)) {
for (idf_ext_iterator<BasicBlock*, SmallPtrSet<BasicBlock*, 16> >
I = idf_ext_begin(P, TranspBlocks),
E = idf_ext_end(P, TranspBlocks); I != E; ++I)
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Transforms/IPO/LoopExtractor.cpp
Expand Up @@ -229,9 +229,7 @@ void BlockExtractorPass::SplitLandingPadPreds(Function *F) {
// Look through the landing pad's predecessors. If one of them ends in an
// 'invoke', then we want to split the landing pad.
bool Split = false;
for (pred_iterator
PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ++PI) {
BasicBlock *BB = *PI;
for (BasicBlock *BB : predecessors(LPad)) {
if (BB->isLandingPad() && BB != Parent &&
isa<InvokeInst>(Parent->getTerminator())) {
Split = true;
Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/Transforms/IPO/PartialInlining.cpp
Expand Up @@ -58,13 +58,12 @@ Function* PartialInliner::unswitchFunction(Function* F) {
BasicBlock* returnBlock = nullptr;
BasicBlock* nonReturnBlock = nullptr;
unsigned returnCount = 0;
for (succ_iterator SI = succ_begin(entryBlock), SE = succ_end(entryBlock);
SI != SE; ++SI)
if (isa<ReturnInst>((*SI)->getTerminator())) {
returnBlock = *SI;
for (BasicBlock *Succ : successors(entryBlock))
if (isa<ReturnInst>(Succ->getTerminator())) {
returnBlock = Succ;
returnCount++;
} else
nonReturnBlock = *SI;
nonReturnBlock = Succ;

if (returnCount != 1)
return nullptr;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Expand Up @@ -2718,8 +2718,8 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {
if (UserParent != BB) {
bool UserIsSuccessor = false;
// See if the user is one of our successors.
for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
if (*SI == UserParent) {
for (BasicBlock *Succ : successors(BB))
if (Succ == UserParent) {
UserIsSuccessor = true;
break;
}
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
Expand Up @@ -642,8 +642,7 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
/// them to F.
static void FindUnconditionalPreds(SmallVectorImpl<BasicBlock *> &Blocks,
BasicBlock *BB, DominatorTree *DT) {
for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
BasicBlock *Pred = *I;
for (BasicBlock *Pred : predecessors(BB)) {
if (Pred == BB) continue;
TerminatorInst *PredTI = Pred->getTerminator();
if (PredTI->getNumSuccessors() != 1)
Expand Down
15 changes: 5 additions & 10 deletions llvm/lib/Transforms/Scalar/GVN.cpp
Expand Up @@ -1555,9 +1555,7 @@ bool GVN::PerformLoadPRE(LoadInst *LI, AvailValInBlkVect &ValuesPerBlock,
FullyAvailableBlocks[UnavailableBlocks[i]] = false;

SmallVector<BasicBlock *, 4> CriticalEdgePred;
for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB);
PI != E; ++PI) {
BasicBlock *Pred = *PI;
for (BasicBlock *Pred : predecessors(LoadBB)) {
if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks, 0)) {
continue;
}
Expand Down Expand Up @@ -2483,9 +2481,7 @@ bool GVN::performPRE(Function &F) {
BasicBlock *PREPred = nullptr;
predMap.clear();

for (pred_iterator PI = pred_begin(CurrentBlock),
PE = pred_end(CurrentBlock); PI != PE; ++PI) {
BasicBlock *P = *PI;
for (BasicBlock *P : predecessors(CurrentBlock)) {
// We're not interested in PRE where the block is its
// own predecessor, or in blocks with predecessors
// that are not reachable.
Expand Down Expand Up @@ -2713,14 +2709,13 @@ void GVN::addDeadBlock(BasicBlock *BB) {
for (SmallVectorImpl<BasicBlock *>::iterator I = Dom.begin(),
E = Dom.end(); I != E; I++) {
BasicBlock *B = *I;
for (succ_iterator SI = succ_begin(B), SE = succ_end(B); SI != SE; SI++) {
BasicBlock *S = *SI;
for (BasicBlock *S : successors(B)) {
if (DeadBlocks.count(S))
continue;

bool AllPredDead = true;
for (pred_iterator PI = pred_begin(S), PE = pred_end(S); PI != PE; PI++)
if (!DeadBlocks.count(*PI)) {
for (BasicBlock *Pred : predecessors(S))
if (!DeadBlocks.count(Pred)) {
AllPredDead = false;
break;
}
Expand Down

0 comments on commit d11beff

Please sign in to comment.