Skip to content

Commit ebcd748

Browse files
committed
Convert a bunch of loops to foreach. NFC.
After r244074, we now have a successors() method to iterate over all the successors of a TerminatorInst. This commit changes a bunch of eligible loops to use it. llvm-svn: 244260
1 parent b9583d2 commit ebcd748

12 files changed

+22
-31
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -5034,8 +5034,7 @@ ScalarEvolution::ComputeExitLimit(const Loop *L, BasicBlock *ExitingBlock) {
50345034
if (!Pred)
50355035
return getCouldNotCompute();
50365036
TerminatorInst *PredTerm = Pred->getTerminator();
5037-
for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) {
5038-
BasicBlock *PredSucc = PredTerm->getSuccessor(i);
5037+
for (const BasicBlock *PredSucc : PredTerm->successors()) {
50395038
if (PredSucc == BB)
50405039
continue;
50415040
// If the predecessor has a successor that isn't BB and isn't

llvm/lib/IR/BasicBlock.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,7 @@ void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) {
404404
// Cope with being called on a BasicBlock that doesn't have a terminator
405405
// yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this.
406406
return;
407-
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
408-
BasicBlock *Succ = TI->getSuccessor(i);
407+
for (BasicBlock *Succ : TI->successors()) {
409408
// N.B. Succ might not be a complete BasicBlock, so don't assume
410409
// that it ends with a non-phi instruction.
411410
for (iterator II = Succ->begin(), IE = Succ->end(); II != IE; ++II) {

llvm/lib/Target/ARM/ARMFastISel.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1355,8 +1355,8 @@ bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
13551355
TII.get(Opc)).addReg(AddrReg));
13561356

13571357
const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1358-
for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1359-
FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1358+
for (const BasicBlock *SuccBB : IB->successors())
1359+
FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
13601360

13611361
return true;
13621362
}

llvm/lib/Target/PowerPC/PPCFastISel.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1759,8 +1759,8 @@ bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
17591759
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
17601760

17611761
const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1762-
for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1763-
FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1762+
for (const BasicBlock *SuccBB : IB->successors())
1763+
FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
17641764

17651765
return true;
17661766
}

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2927,8 +2927,8 @@ static bool AddReachableCodeToWorklist(BasicBlock *BB, const DataLayout &DL,
29272927
}
29282928
}
29292929

2930-
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
2931-
Worklist.push_back(TI->getSuccessor(i));
2930+
for (BasicBlock *SuccBB : TI->successors())
2931+
Worklist.push_back(SuccBB);
29322932
} while (!Worklist.empty());
29332933

29342934
// Once we've found all of the instructions to add to instcombine's worklist,

llvm/lib/Transforms/Scalar/LoopRotation.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,8 @@ bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) {
467467
// terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's
468468
// successors by duplicating their incoming values for OrigHeader.
469469
TerminatorInst *TI = OrigHeader->getTerminator();
470-
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
471-
for (BasicBlock::iterator BI = TI->getSuccessor(i)->begin();
470+
for (BasicBlock *SuccBB : TI->successors())
471+
for (BasicBlock::iterator BI = SuccBB->begin();
472472
PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
473473
PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreheader);
474474

llvm/lib/Transforms/Scalar/SCCP.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1796,10 +1796,9 @@ bool IPSCCP::runOnModule(Module &M) {
17961796
MadeChanges = true;
17971797

17981798
TerminatorInst *TI = BB->getTerminator();
1799-
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1800-
BasicBlock *Succ = TI->getSuccessor(i);
1799+
for (BasicBlock *Succ : TI->successors()) {
18011800
if (!Succ->empty() && isa<PHINode>(Succ->begin()))
1802-
TI->getSuccessor(i)->removePredecessor(BB);
1801+
Succ->removePredecessor(BB);
18031802
}
18041803
if (!TI->use_empty())
18051804
TI->replaceAllUsesWith(UndefValue::get(TI->getType()));

llvm/lib/Transforms/Scalar/StructurizeCFG.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,9 @@ void StructurizeCFG::analyzeLoops(RegionNode *N) {
358358
BasicBlock *BB = N->getNodeAs<BasicBlock>();
359359
BranchInst *Term = cast<BranchInst>(BB->getTerminator());
360360

361-
for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
362-
BasicBlock *Succ = Term->getSuccessor(i);
363-
364-
if (Visited.count(Succ)) {
361+
for (BasicBlock *Succ : Term->successors())
362+
if (Visited.count(Succ))
365363
Loops[Succ] = BB;
366-
}
367-
}
368364
}
369365
}
370366

llvm/lib/Transforms/Utils/BasicBlockUtils.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ void llvm::DeleteDeadBlock(BasicBlock *BB) {
4141

4242
// Loop through all of our successors and make sure they know that one
4343
// of their predecessors is going away.
44-
for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i)
45-
BBTerm->getSuccessor(i)->removePredecessor(BB);
44+
for (BasicBlock *Succ : BBTerm->successors())
45+
Succ->removePredecessor(BB);
4646

4747
// Zap all the instructions in the block.
4848
while (!BB->empty()) {

llvm/lib/Transforms/Utils/CloneFunction.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
400400
// If the director says to skip with a terminate instruction, we still
401401
// need to clone this block's successors.
402402
const TerminatorInst *TI = NewBB->getTerminator();
403-
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
404-
ToClone.push_back(TI->getSuccessor(i));
403+
for (const BasicBlock *Succ : TI->successors())
404+
ToClone.push_back(Succ);
405405
return;
406406
}
407407
assert(Action != CloningDirector::SkipInstruction &&
@@ -450,8 +450,8 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
450450

451451
// Recursively clone any reachable successor blocks.
452452
const TerminatorInst *TI = BB->getTerminator();
453-
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
454-
ToClone.push_back(TI->getSuccessor(i));
453+
for (const BasicBlock *Succ : TI->successors())
454+
ToClone.push_back(Succ);
455455
}
456456

457457
if (CodeInfo) {

llvm/lib/Transforms/Utils/Local.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,8 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
188188
BasicBlock *BB = SI->getParent();
189189

190190
// Remove entries from PHI nodes which we no longer branch to...
191-
for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
191+
for (BasicBlock *Succ : SI->successors()) {
192192
// Found case matching a constant operand?
193-
BasicBlock *Succ = SI->getSuccessor(i);
194193
if (Succ == TheOnlyDest)
195194
TheOnlyDest = nullptr; // Don't modify the first branch to TheOnlyDest
196195
else

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -2565,8 +2565,7 @@ static bool SimplifyTerminatorOnSelect(TerminatorInst *OldTerm, Value *Cond,
25652565
BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB : nullptr;
25662566

25672567
// Then remove the rest.
2568-
for (unsigned I = 0, E = OldTerm->getNumSuccessors(); I != E; ++I) {
2569-
BasicBlock *Succ = OldTerm->getSuccessor(I);
2568+
for (BasicBlock *Succ : OldTerm->successors()) {
25702569
// Make sure only to keep exactly one copy of each edge.
25712570
if (Succ == KeepEdge1)
25722571
KeepEdge1 = nullptr;

0 commit comments

Comments
 (0)