[llvm] 1d4a2f3 - [Transforms/Utils] Use range-based for loops (NFC)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 26 22:37:00 PST 2021
Author: Kazu Hirata
Date: 2021-02-26T22:36:40-08:00
New Revision: 1d4a2f37785b98fd2321f6c3f684717596ce7377
URL: https://github.com/llvm/llvm-project/commit/1d4a2f37785b98fd2321f6c3f684717596ce7377
DIFF: https://github.com/llvm/llvm-project/commit/1d4a2f37785b98fd2321f6c3f684717596ce7377.diff
LOG: [Transforms/Utils] Use range-based for loops (NFC)
Added:
Modified:
llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
llvm/lib/Transforms/Utils/CodeExtractor.cpp
llvm/lib/Transforms/Utils/Evaluator.cpp
llvm/lib/Transforms/Utils/Local.cpp
llvm/lib/Transforms/Utils/PredicateInfo.cpp
llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
index df44e5010c61..31948dd17f7b 100644
--- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -1080,9 +1080,8 @@ ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
// If the return instruction returns a value, and if the value was a
// PHI node in "BB", propagate the right value into the return.
- for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end();
- i != e; ++i) {
- Value *V = *i;
+ for (Use &Op : NewRet->operands()) {
+ Value *V = Op;
Instruction *NewBC = nullptr;
if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {
// Return value might be bitcasted. Clone and insert it before the
@@ -1090,7 +1089,7 @@ ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
V = BCI->getOperand(0);
NewBC = BCI->clone();
Pred->getInstList().insert(NewRet->getIterator(), NewBC);
- *i = NewBC;
+ Op = NewBC;
}
Instruction *NewEV = nullptr;
@@ -1102,7 +1101,7 @@ ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
Pred->getInstList().insert(NewBC->getIterator(), NewEV);
} else {
Pred->getInstList().insert(NewRet->getIterator(), NewEV);
- *i = NewEV;
+ Op = NewEV;
}
}
@@ -1113,7 +1112,7 @@ ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
} else if (NewBC)
NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));
else
- *i = PN->getIncomingValueForBlock(Pred);
+ Op = PN->getIncomingValueForBlock(Pred);
}
}
}
diff --git a/llvm/lib/Transforms/Utils/CodeExtractor.cpp b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
index 05189a1b6cfe..b9f886d6217d 100644
--- a/llvm/lib/Transforms/Utils/CodeExtractor.cpp
+++ b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
@@ -426,9 +426,8 @@ CodeExtractor::findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock) {
BasicBlock *NewExitBlock = CommonExitBlock->splitBasicBlock(
CommonExitBlock->getFirstNonPHI()->getIterator());
- for (auto PI = pred_begin(CommonExitBlock), PE = pred_end(CommonExitBlock);
- PI != PE;) {
- BasicBlock *Pred = *PI++;
+ for (BasicBlock *Pred :
+ llvm::make_early_inc_range(predecessors(CommonExitBlock))) {
if (Blocks.count(Pred))
continue;
Pred->getTerminator()->replaceUsesOfWith(CommonExitBlock, NewExitBlock);
diff --git a/llvm/lib/Transforms/Utils/Evaluator.cpp b/llvm/lib/Transforms/Utils/Evaluator.cpp
index 732b00635e29..091b5ac30308 100644
--- a/llvm/lib/Transforms/Utils/Evaluator.cpp
+++ b/llvm/lib/Transforms/Utils/Evaluator.cpp
@@ -428,9 +428,8 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst,
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) {
Constant *P = getVal(GEP->getOperand(0));
SmallVector<Constant*, 8> GEPOps;
- for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end();
- i != e; ++i)
- GEPOps.push_back(getVal(*i));
+ for (Use &Op : llvm::drop_begin(GEP->operands()))
+ GEPOps.push_back(getVal(Op));
InstResult =
ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), P, GEPOps,
cast<GEPOperator>(GEP)->isInBounds());
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 915e5d7eba69..971f4a137bff 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -1702,11 +1702,9 @@ void llvm::replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
DIBuilder &Builder, int Offset) {
if (auto *L = LocalAsMetadata::getIfExists(AI))
if (auto *MDV = MetadataAsValue::getIfExists(AI->getContext(), L))
- for (auto UI = MDV->use_begin(), UE = MDV->use_end(); UI != UE;) {
- Use &U = *UI++;
+ for (Use &U : llvm::make_early_inc_range(MDV->uses()))
if (auto *DVI = dyn_cast<DbgValueInst>(U.getUser()))
replaceOneDbgValueForAlloca(DVI, NewAllocaAddress, Builder, Offset);
- }
}
/// Wrap \p V in a ValueAsMetadata instance.
diff --git a/llvm/lib/Transforms/Utils/PredicateInfo.cpp b/llvm/lib/Transforms/Utils/PredicateInfo.cpp
index 71b1926b92e2..4c262f60014c 100644
--- a/llvm/lib/Transforms/Utils/PredicateInfo.cpp
+++ b/llvm/lib/Transforms/Utils/PredicateInfo.cpp
@@ -865,15 +865,14 @@ void PredicateInfoPrinterLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
// Replace ssa_copy calls created by PredicateInfo with their operand.
static void replaceCreatedSSACopys(PredicateInfo &PredInfo, Function &F) {
- for (auto I = inst_begin(F), E = inst_end(F); I != E;) {
- Instruction *Inst = &*I++;
- const auto *PI = PredInfo.getPredicateInfoFor(Inst);
- auto *II = dyn_cast<IntrinsicInst>(Inst);
+ for (Instruction &Inst : llvm::make_early_inc_range(instructions(F))) {
+ const auto *PI = PredInfo.getPredicateInfoFor(&Inst);
+ auto *II = dyn_cast<IntrinsicInst>(&Inst);
if (!PI || !II || II->getIntrinsicID() != Intrinsic::ssa_copy)
continue;
- Inst->replaceAllUsesWith(II->getOperand(0));
- Inst->eraseFromParent();
+ Inst.replaceAllUsesWith(II->getOperand(0));
+ Inst.eraseFromParent();
}
}
diff --git a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
index 86bbb6a889e6..d04df2cc0bab 100644
--- a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
+++ b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
@@ -313,10 +313,8 @@ static void removeIntrinsicUsers(AllocaInst *AI) {
// Knowing that this alloca is promotable, we know that it's safe to kill all
// instructions except for load and store.
- for (auto UI = AI->use_begin(), UE = AI->use_end(); UI != UE;) {
- Instruction *I = cast<Instruction>(UI->getUser());
- Use &U = *UI;
- ++UI;
+ for (Use &U : llvm::make_early_inc_range(AI->uses())) {
+ Instruction *I = cast<Instruction>(U.getUser());
if (isa<LoadInst>(I) || isa<StoreInst>(I))
continue;
@@ -330,10 +328,8 @@ static void removeIntrinsicUsers(AllocaInst *AI) {
// The only users of this bitcast/GEP instruction are lifetime intrinsics.
// Follow the use/def chain to erase them now instead of leaving it for
// dead code elimination later.
- for (auto UUI = I->use_begin(), UUE = I->use_end(); UUI != UUE;) {
- Instruction *Inst = cast<Instruction>(UUI->getUser());
- Use &UU = *UUI;
- ++UUI;
+ for (Use &UU : llvm::make_early_inc_range(I->uses())) {
+ Instruction *Inst = cast<Instruction>(UU.getUser());
// Drop the use of I in droppable instructions.
if (Inst->isDroppable()) {
diff --git a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
index 2dbf732ad364..5256796105fa 100644
--- a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -929,9 +929,8 @@ bool SCEVExpander::isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV,
// Addrec operands are always loop-invariant, so this can only happen
// if there are instructions which haven't been hoisted.
if (L == IVIncInsertLoop) {
- for (User::op_iterator OI = IncV->op_begin()+1,
- OE = IncV->op_end(); OI != OE; ++OI)
- if (Instruction *OInst = dyn_cast<Instruction>(OI))
+ for (Use &Op : llvm::drop_begin(IncV->operands()))
+ if (Instruction *OInst = dyn_cast<Instruction>(Op))
if (!SE.DT.dominates(OInst, IVIncInsertPos))
return false;
}
@@ -978,10 +977,10 @@ Instruction *SCEVExpander::getIVIncOperand(Instruction *IncV,
case Instruction::BitCast:
return dyn_cast<Instruction>(IncV->getOperand(0));
case Instruction::GetElementPtr:
- for (auto I = IncV->op_begin() + 1, E = IncV->op_end(); I != E; ++I) {
- if (isa<Constant>(*I))
+ for (Use &U : llvm::drop_begin(IncV->operands())) {
+ if (isa<Constant>(U))
continue;
- if (Instruction *OInst = dyn_cast<Instruction>(*I)) {
+ if (Instruction *OInst = dyn_cast<Instruction>(U)) {
if (!SE.DT.dominates(OInst, InsertPos))
return nullptr;
}
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 30d9daf844b3..e8dd03367113 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -420,8 +420,8 @@ static bool dominatesMergePoint(Value *V, BasicBlock *BB,
// Okay, we can only really hoist these out if their operands do
// not take us over the cost threshold.
- for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
- if (!dominatesMergePoint(*i, BB, AggressiveInsts, Cost, Budget, TTI,
+ for (Use &Op : I->operands())
+ if (!dominatesMergePoint(Op, BB, AggressiveInsts, Cost, Budget, TTI,
Depth + 1))
return false;
// Okay, it's safe to do this! Remember this instruction.
@@ -4160,9 +4160,8 @@ bool SimplifyCFGOpt::simplifyCommonResume(ResumeInst *RI) {
while (PhiLPInst->getBasicBlockIndex(TrivialBB) != -1)
BB->removePredecessor(TrivialBB, true);
- for (pred_iterator PI = pred_begin(TrivialBB), PE = pred_end(TrivialBB);
- PI != PE;) {
- BasicBlock *Pred = *PI++;
+ for (BasicBlock *Pred :
+ llvm::make_early_inc_range(predecessors(TrivialBB))) {
removeUnwindEdge(Pred, DTU);
++NumInvokes;
}
@@ -4202,8 +4201,7 @@ bool SimplifyCFGOpt::simplifySingleResume(ResumeInst *RI) {
return false;
// Turn all invokes that unwind here into calls and delete the basic block.
- for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
- BasicBlock *Pred = *PI++;
+ for (BasicBlock *Pred : llvm::make_early_inc_range(predecessors(BB))) {
removeUnwindEdge(Pred, DTU);
++NumInvokes;
}
@@ -4325,9 +4323,8 @@ static bool removeEmptyCleanup(CleanupReturnInst *RI, DomTreeUpdater *DTU) {
std::vector<DominatorTree::UpdateType> Updates;
- for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
- // The iterator must be updated here because we are removing this pred.
- BasicBlock *PredBB = *PI++;
+ // We use make_early_inc_range here because we may remove some predecessors.
+ for (BasicBlock *PredBB : llvm::make_early_inc_range(predecessors(BB))) {
if (UnwindDest == nullptr) {
if (DTU)
DTU->applyUpdates(Updates);
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 1053bf81c745..2816d49d163f 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -842,8 +842,8 @@ Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilderBase &B) {
StrLen, B, DL, TLI);
if (!StrNCmp)
return nullptr;
- for (auto UI = CI->user_begin(), UE = CI->user_end(); UI != UE;) {
- ICmpInst *Old = cast<ICmpInst>(*UI++);
+ for (User *U : llvm::make_early_inc_range(CI->users())) {
+ ICmpInst *Old = cast<ICmpInst>(U);
Value *Cmp =
B.CreateICmp(Old->getPredicate(), StrNCmp,
ConstantInt::getNullValue(StrNCmp->getType()), "cmp");
More information about the llvm-commits
mailing list