From cf6f3cec618da2f36d02653ee08521c331db68b9 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sat, 27 Feb 2021 10:09:25 -0800 Subject: [PATCH] [IR] Use range-based for loops (NFC) --- lib/IR/AsmWriter.cpp | 29 ++++++++++++++--------------- lib/IR/Attributes.cpp | 5 ++--- lib/IR/BasicBlock.cpp | 3 +-- lib/IR/Core.cpp | 15 ++++++--------- lib/IR/DebugInfoMetadata.cpp | 4 ++-- lib/IR/Dominators.cpp | 4 +--- lib/IR/Instructions.cpp | 10 +++++----- lib/IR/LegacyPassManager.cpp | 13 ++++++------- lib/IR/Mangler.cpp | 9 ++++----- 9 files changed, 41 insertions(+), 51 deletions(-) diff --git a/lib/IR/AsmWriter.cpp b/lib/IR/AsmWriter.cpp index bb300a230b9..642ecc92e9b 100644 --- a/lib/IR/AsmWriter.cpp +++ b/lib/IR/AsmWriter.cpp @@ -2332,17 +2332,17 @@ static void writeDIExpression(raw_ostream &Out, const DIExpression *N, Out << "!DIExpression("; FieldSeparator FS; if (N->isValid()) { - for (auto I = N->expr_op_begin(), E = N->expr_op_end(); I != E; ++I) { - auto OpStr = dwarf::OperationEncodingString(I->getOp()); + for (const DIExpression::ExprOperand &Op : N->expr_ops()) { + auto OpStr = dwarf::OperationEncodingString(Op.getOp()); assert(!OpStr.empty() && "Expected valid opcode"); Out << FS << OpStr; - if (I->getOp() == dwarf::DW_OP_LLVM_convert) { - Out << FS << I->getArg(0); - Out << FS << dwarf::AttributeEncodingString(I->getArg(1)); + if (Op.getOp() == dwarf::DW_OP_LLVM_convert) { + Out << FS << Op.getArg(0); + Out << FS << dwarf::AttributeEncodingString(Op.getArg(1)); } else { - for (unsigned A = 0, AE = I->getNumArgs(); A != AE; ++A) - Out << FS << I->getArg(A); + for (unsigned A = 0, AE = Op.getNumArgs(); A != AE; ++A) + Out << FS << Op.getArg(A); } } } else { @@ -2914,12 +2914,11 @@ void AssemblyWriter::printModuleSummaryIndex() { } // Print the TypeIdMap entries. - for (auto TidIter = TheIndex->typeIds().begin(); - TidIter != TheIndex->typeIds().end(); TidIter++) { - Out << "^" << Machine.getTypeIdSlot(TidIter->second.first) - << " = typeid: (name: \"" << TidIter->second.first << "\""; - printTypeIdSummary(TidIter->second.second); - Out << ") ; guid = " << TidIter->first << "\n"; + for (const auto &TID : TheIndex->typeIds()) { + Out << "^" << Machine.getTypeIdSlot(TID.second.first) + << " = typeid: (name: \"" << TID.second.first << "\""; + printTypeIdSummary(TID.second.second); + Out << ") ; guid = " << TID.first << "\n"; } // Print the TypeIdCompatibleVtableMap entries. @@ -4023,8 +4022,8 @@ void AssemblyWriter::printInstruction(const Instruction &I) { } else if (const ExtractValueInst *EVI = dyn_cast(&I)) { Out << ' '; writeOperand(I.getOperand(0), true); - for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i) - Out << ", " << *i; + for (unsigned i : EVI->indices()) + Out << ", " << i; } else if (const InsertValueInst *IVI = dyn_cast(&I)) { Out << ' '; writeOperand(I.getOperand(0), true); Out << ", "; diff --git a/lib/IR/Attributes.cpp b/lib/IR/Attributes.cpp index c4629decc6d..c98e5c36a51 100644 --- a/lib/IR/Attributes.cpp +++ b/lib/IR/Attributes.cpp @@ -1869,9 +1869,8 @@ bool AttrBuilder::operator==(const AttrBuilder &B) const { if (Attrs != B.Attrs) return false; - for (td_const_iterator I = TargetDepAttrs.begin(), - E = TargetDepAttrs.end(); I != E; ++I) - if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end()) + for (const auto &TDA : TargetDepAttrs) + if (B.TargetDepAttrs.find(TDA.first) == B.TargetDepAttrs.end()) return false; return Alignment == B.Alignment && StackAlignment == B.StackAlignment && diff --git a/lib/IR/BasicBlock.cpp b/lib/IR/BasicBlock.cpp index 00ef10dd53a..48227d8e6c6 100644 --- a/lib/IR/BasicBlock.cpp +++ b/lib/IR/BasicBlock.cpp @@ -455,9 +455,8 @@ void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *Old, // Cope with being called on a BasicBlock that doesn't have a terminator // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this. return; - llvm::for_each(successors(TI), [Old, New](BasicBlock *Succ) { + for (BasicBlock *Succ : successors(TI)) Succ->replacePhiUsesWith(Old, New); - }); } void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) { diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp index fdcd64370c0..6631a4ac224 100644 --- a/lib/IR/Core.cpp +++ b/lib/IR/Core.cpp @@ -679,9 +679,8 @@ unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) { void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) { FunctionType *Ty = unwrap(FunctionTy); - for (FunctionType::param_iterator I = Ty->param_begin(), - E = Ty->param_end(); I != E; ++I) - *Dest++ = wrap(*I); + for (Type *T : Ty->params()) + *Dest++ = wrap(T); } /*--.. Operations on struct types ..........................................--*/ @@ -723,9 +722,8 @@ unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) { void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) { StructType *Ty = unwrap(StructTy); - for (StructType::element_iterator I = Ty->element_begin(), - E = Ty->element_end(); I != E; ++I) - *Dest++ = wrap(*I); + for (Type *T : Ty->elements()) + *Dest++ = wrap(T); } LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) { @@ -2495,9 +2493,8 @@ unsigned LLVMCountParams(LLVMValueRef FnRef) { void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) { Function *Fn = unwrap(FnRef); - for (Function::arg_iterator I = Fn->arg_begin(), - E = Fn->arg_end(); I != E; I++) - *ParamRefs++ = wrap(&*I); + for (Argument &A : Fn->args()) + *ParamRefs++ = wrap(&A); } LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) { diff --git a/lib/IR/DebugInfoMetadata.cpp b/lib/IR/DebugInfoMetadata.cpp index 55eff38d3fa..973020c02ff 100644 --- a/lib/IR/DebugInfoMetadata.cpp +++ b/lib/IR/DebugInfoMetadata.cpp @@ -82,8 +82,8 @@ DILocation *DILocation::getMergedLocations(ArrayRef Locs) { if (Locs.size() == 1) return Locs[0]; auto *Merged = Locs[0]; - for (auto I = std::next(Locs.begin()), E = Locs.end(); I != E; ++I) { - Merged = getMergedLocation(Merged, *I); + for (const DILocation *L : llvm::drop_begin(Locs)) { + Merged = getMergedLocation(Merged, L); if (Merged == nullptr) break; } diff --git a/lib/IR/Dominators.cpp b/lib/IR/Dominators.cpp index fbc28c202ae..5d17d6ced3c 100644 --- a/lib/IR/Dominators.cpp +++ b/lib/IR/Dominators.cpp @@ -222,9 +222,7 @@ bool DominatorTree::dominates(const BasicBlockEdge &BBE, // 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. int IsDuplicateEdge = 0; - 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) { // If there are multiple edges between Start and End, by definition they // can't dominate anything. diff --git a/lib/IR/Instructions.cpp b/lib/IR/Instructions.cpp index b73528d8975..2fb277bd12d 100644 --- a/lib/IR/Instructions.cpp +++ b/lib/IR/Instructions.cpp @@ -2105,13 +2105,13 @@ static bool isSingleSourceMaskImpl(ArrayRef Mask, int NumOpElts) { assert(!Mask.empty() && "Shuffle mask must contain elements"); bool UsesLHS = false; bool UsesRHS = false; - for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) { - if (Mask[i] == -1) + for (int I : Mask) { + if (I == -1) continue; - assert(Mask[i] >= 0 && Mask[i] < (NumOpElts * 2) && + assert(I >= 0 && I < (NumOpElts * 2) && "Out-of-bounds shuffle mask element"); - UsesLHS |= (Mask[i] < NumOpElts); - UsesRHS |= (Mask[i] >= NumOpElts); + UsesLHS |= (I < NumOpElts); + UsesRHS |= (I >= NumOpElts); if (UsesLHS && UsesRHS) return false; } diff --git a/lib/IR/LegacyPassManager.cpp b/lib/IR/LegacyPassManager.cpp index 19b03dfc35c..94be208617c 100644 --- a/lib/IR/LegacyPassManager.cpp +++ b/lib/IR/LegacyPassManager.cpp @@ -948,14 +948,13 @@ void PMDataManager::removeNotPreservedAnalysis(Pass *P) { // Check inherited analysis also. If P is not preserving analysis // provided by parent manager then remove it here. - for (unsigned Index = 0; Index < PMT_Last; ++Index) { - - if (!InheritedAnalysis[Index]) + for (DenseMap *IA : InheritedAnalysis) { + if (!IA) continue; - for (DenseMap::iterator - I = InheritedAnalysis[Index]->begin(), - E = InheritedAnalysis[Index]->end(); I != E; ) { + for (DenseMap::iterator I = IA->begin(), + E = IA->end(); + I != E;) { DenseMap::iterator Info = I++; if (Info->second->getAsImmutablePass() == nullptr && !is_contained(PreservedSet, Info->first)) { @@ -965,7 +964,7 @@ void PMDataManager::removeNotPreservedAnalysis(Pass *P) { dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; dbgs() << S->getPassName() << "'\n"; } - InheritedAnalysis[Index]->erase(Info); + IA->erase(Info); } } } diff --git a/lib/IR/Mangler.cpp b/lib/IR/Mangler.cpp index 674ba3cdaa2..bbdde586e6e 100644 --- a/lib/IR/Mangler.cpp +++ b/lib/IR/Mangler.cpp @@ -98,12 +98,11 @@ static void addByteCountSuffix(raw_ostream &OS, const Function *F, const unsigned PtrSize = DL.getPointerSize(); - for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); - AI != AE; ++AI) { + for (const Argument &A : F->args()) { // 'Dereference' type in case of byval or inalloca parameter attribute. - uint64_t AllocSize = AI->hasPassPointeeByValueCopyAttr() ? - AI->getPassPointeeByValueCopySize(DL) : - DL.getTypeAllocSize(AI->getType()); + uint64_t AllocSize = A.hasPassPointeeByValueCopyAttr() ? + A.getPassPointeeByValueCopySize(DL) : + DL.getTypeAllocSize(A.getType()); // Size should be aligned to pointer size. ArgWords += alignTo(AllocSize, PtrSize);