Skip to content

Commit 9ba5bb4

Browse files
committed
[NFC][LoopIdiom] Make for loops more readable
Patch simplifies for loops in LIR following LLVM guidelines: https://llvm.org/docs/CodingStandards.html#use-range-based-for-loops-wherever-possible. Differential Revision: https://reviews.llvm.org/D112077
1 parent 05a2d17 commit 9ba5bb4

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,8 @@ bool LoopIdiomRecognize::runOnLoopBlock(
625625
// We can only promote stores in this block if they are unconditionally
626626
// executed in the loop. For a block to be unconditionally executed, it has
627627
// to dominate all the exit blocks of the loop. Verify this now.
628-
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
629-
if (!DT->dominates(BB, ExitBlocks[i]))
628+
for (BasicBlock *ExitBlock : ExitBlocks)
629+
if (!DT->dominates(BB, ExitBlock))
630630
return false;
631631

632632
bool MadeChange = false;
@@ -750,16 +750,13 @@ bool LoopIdiomRecognize::processLoopStores(SmallVectorImpl<StoreInst *> &SL,
750750
bool Changed = false;
751751

752752
// For stores that start but don't end a link in the chain:
753-
for (SetVector<StoreInst *>::iterator it = Heads.begin(), e = Heads.end();
754-
it != e; ++it) {
755-
if (Tails.count(*it))
753+
for (StoreInst *I : Heads) {
754+
if (Tails.count(I))
756755
continue;
757756

758757
// We found a store instr that starts a chain. Now follow the chain and try
759758
// to transform it.
760759
SmallPtrSet<Instruction *, 8> AdjacentStores;
761-
StoreInst *I = *it;
762-
763760
StoreInst *HeadStore = I;
764761
unsigned StoreSize = 0;
765762

@@ -1020,9 +1017,8 @@ mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L,
10201017
// which will then no-alias a store to &A[100].
10211018
MemoryLocation StoreLoc(Ptr, AccessSize);
10221019

1023-
for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E;
1024-
++BI)
1025-
for (Instruction &I : **BI)
1020+
for (BasicBlock *B : L->blocks())
1021+
for (Instruction &I : *B)
10261022
if (IgnoredInsts.count(&I) == 0 &&
10271023
isModOrRefSet(
10281024
intersectModRef(AA.getModRefInfo(&I, StoreLoc), Access)))
@@ -1675,32 +1671,30 @@ static bool detectPopcountIdiom(Loop *CurLoop, BasicBlock *PreCondBB,
16751671
// step 4: Find the instruction which count the population: cnt2 = cnt1 + 1
16761672
{
16771673
CountInst = nullptr;
1678-
for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI()->getIterator(),
1679-
IterE = LoopEntry->end();
1680-
Iter != IterE; Iter++) {
1681-
Instruction *Inst = &*Iter;
1682-
if (Inst->getOpcode() != Instruction::Add)
1674+
for (Instruction &Inst : llvm::make_range(
1675+
LoopEntry->getFirstNonPHI()->getIterator(), LoopEntry->end())) {
1676+
if (Inst.getOpcode() != Instruction::Add)
16831677
continue;
16841678

1685-
ConstantInt *Inc = dyn_cast<ConstantInt>(Inst->getOperand(1));
1679+
ConstantInt *Inc = dyn_cast<ConstantInt>(Inst.getOperand(1));
16861680
if (!Inc || !Inc->isOne())
16871681
continue;
16881682

1689-
PHINode *Phi = getRecurrenceVar(Inst->getOperand(0), Inst, LoopEntry);
1683+
PHINode *Phi = getRecurrenceVar(Inst.getOperand(0), &Inst, LoopEntry);
16901684
if (!Phi)
16911685
continue;
16921686

16931687
// Check if the result of the instruction is live of the loop.
16941688
bool LiveOutLoop = false;
1695-
for (User *U : Inst->users()) {
1689+
for (User *U : Inst.users()) {
16961690
if ((cast<Instruction>(U))->getParent() != LoopEntry) {
16971691
LiveOutLoop = true;
16981692
break;
16991693
}
17001694
}
17011695

17021696
if (LiveOutLoop) {
1703-
CountInst = Inst;
1697+
CountInst = &Inst;
17041698
CountPhi = Phi;
17051699
break;
17061700
}
@@ -1801,22 +1795,20 @@ static bool detectShiftUntilZeroIdiom(Loop *CurLoop, const DataLayout &DL,
18011795
// plus "cnt0". Currently it is not optimized.
18021796
// This step could be used to detect POPCNT instruction:
18031797
// cnt.next = cnt + (x.next & 1)
1804-
for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI()->getIterator(),
1805-
IterE = LoopEntry->end();
1806-
Iter != IterE; Iter++) {
1807-
Instruction *Inst = &*Iter;
1808-
if (Inst->getOpcode() != Instruction::Add)
1798+
for (Instruction &Inst : llvm::make_range(
1799+
LoopEntry->getFirstNonPHI()->getIterator(), LoopEntry->end())) {
1800+
if (Inst.getOpcode() != Instruction::Add)
18091801
continue;
18101802

1811-
ConstantInt *Inc = dyn_cast<ConstantInt>(Inst->getOperand(1));
1803+
ConstantInt *Inc = dyn_cast<ConstantInt>(Inst.getOperand(1));
18121804
if (!Inc || (!Inc->isOne() && !Inc->isMinusOne()))
18131805
continue;
18141806

1815-
PHINode *Phi = getRecurrenceVar(Inst->getOperand(0), Inst, LoopEntry);
1807+
PHINode *Phi = getRecurrenceVar(Inst.getOperand(0), &Inst, LoopEntry);
18161808
if (!Phi)
18171809
continue;
18181810

1819-
CntInst = Inst;
1811+
CntInst = &Inst;
18201812
CntPhi = Phi;
18211813
break;
18221814
}

0 commit comments

Comments
 (0)