diff --git a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp --- a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp +++ b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp @@ -585,7 +585,7 @@ StoreRefsForMemset.clear(); StoreRefsForMemsetPattern.clear(); StoreRefsForMemcpy.clear(); - for (Instruction &I : *BB) { + for (auto &I : *BB) { StoreInst *SI = dyn_cast(&I); if (!SI) continue; @@ -625,8 +625,8 @@ // We can only promote stores in this block if they are unconditionally // executed in the loop. For a block to be unconditionally executed, it has // to dominate all the exit blocks of the loop. Verify this now. - for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) - if (!DT->dominates(BB, ExitBlocks[i])) + for (auto *ExitBlock : ExitBlocks) + if (!DT->dominates(BB, ExitBlock)) return false; bool MadeChange = false; @@ -664,7 +664,7 @@ // Do a quadratic search on all of the given stores and find // all of the pairs of stores that follow each other. SmallVector IndexQueue; - for (unsigned i = 0, e = SL.size(); i < e; ++i) { + for (auto i = 0LU, e = SL.size(); i < e; ++i) { assert(SL[i]->isSimple() && "Expected only non-volatile stores."); Value *FirstStoredVal = SL[i]->getValueOperand(); @@ -750,16 +750,13 @@ bool Changed = false; // For stores that start but don't end a link in the chain: - for (SetVector::iterator it = Heads.begin(), e = Heads.end(); - it != e; ++it) { - if (Tails.count(*it)) + for (auto *I : Heads) { + if (Tails.count(I)) continue; // We found a store instr that starts a chain. Now follow the chain and try // to transform it. SmallPtrSet AdjacentStores; - StoreInst *I = *it; - StoreInst *HeadStore = I; unsigned StoreSize = 0; @@ -808,7 +805,7 @@ bool (LoopIdiomRecognize::*Processor)(MemInst *, const SCEV *), const SCEV *BECount) { bool MadeChange = false; - for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) { + for (auto I = BB->begin(), E = BB->end(); I != E;) { Instruction *Inst = &*I++; // Look for memory instructions, which may be optimized to a larger one. if (MemInst *MI = dyn_cast(Inst)) { @@ -1020,9 +1017,8 @@ // which will then no-alias a store to &A[100]. MemoryLocation StoreLoc(Ptr, AccessSize); - for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E; - ++BI) - for (Instruction &I : **BI) + for (auto BI : L->blocks()) + for (auto &I : *BI) if (IgnoredInsts.count(&I) == 0 && isModOrRefSet( intersectModRef(AA.getModRefInfo(&I, StoreLoc), Access))) @@ -1675,24 +1671,22 @@ // step 4: Find the instruction which count the population: cnt2 = cnt1 + 1 { CountInst = nullptr; - for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI()->getIterator(), - IterE = LoopEntry->end(); - Iter != IterE; Iter++) { - Instruction *Inst = &*Iter; - if (Inst->getOpcode() != Instruction::Add) + for (auto &Inst : llvm::make_range( + LoopEntry->getFirstNonPHI()->getIterator(), LoopEntry->end())) { + if (Inst.getOpcode() != Instruction::Add) continue; - ConstantInt *Inc = dyn_cast(Inst->getOperand(1)); + ConstantInt *Inc = dyn_cast(Inst.getOperand(1)); if (!Inc || !Inc->isOne()) continue; - PHINode *Phi = getRecurrenceVar(Inst->getOperand(0), Inst, LoopEntry); + PHINode *Phi = getRecurrenceVar(Inst.getOperand(0), &Inst, LoopEntry); if (!Phi) continue; // Check if the result of the instruction is live of the loop. bool LiveOutLoop = false; - for (User *U : Inst->users()) { + for (auto *U : Inst.users()) { if ((cast(U))->getParent() != LoopEntry) { LiveOutLoop = true; break; @@ -1700,7 +1694,7 @@ } if (LiveOutLoop) { - CountInst = Inst; + CountInst = &Inst; CountPhi = Phi; break; } @@ -1801,22 +1795,20 @@ // plus "cnt0". Currently it is not optimized. // This step could be used to detect POPCNT instruction: // cnt.next = cnt + (x.next & 1) - for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI()->getIterator(), - IterE = LoopEntry->end(); - Iter != IterE; Iter++) { - Instruction *Inst = &*Iter; - if (Inst->getOpcode() != Instruction::Add) + for (auto &Inst : llvm::make_range(LoopEntry->getFirstNonPHI()->getIterator(), + LoopEntry->end())) { + if (Inst.getOpcode() != Instruction::Add) continue; - ConstantInt *Inc = dyn_cast(Inst->getOperand(1)); + ConstantInt *Inc = dyn_cast(Inst.getOperand(1)); if (!Inc || (!Inc->isOne() && !Inc->isMinusOne())) continue; - PHINode *Phi = getRecurrenceVar(Inst->getOperand(0), Inst, LoopEntry); + PHINode *Phi = getRecurrenceVar(Inst.getOperand(0), &Inst, LoopEntry); if (!Phi) continue; - CntInst = Inst; + CntInst = &Inst; CntPhi = Phi; break; } @@ -1848,13 +1840,13 @@ return false; bool IsCntPhiUsedOutsideLoop = false; - for (User *U : CntPhi->users()) + for (auto *U : CntPhi->users()) if (!CurLoop->contains(cast(U))) { IsCntPhiUsedOutsideLoop = true; break; } bool IsCntInstUsedOutsideLoop = false; - for (User *U : CntInst->users()) + for (auto *U : CntInst->users()) if (!CurLoop->contains(cast(U))) { IsCntInstUsedOutsideLoop = true; break;