From 249aa54f989d737c2108c6a35525e2a550cf6a26 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Thu, 18 Feb 2021 22:46:43 -0800 Subject: [PATCH] [CodeGen] Use range-based for loops (NFC) --- lib/CodeGen/RegAllocPBQP.cpp | 11 ++---- lib/CodeGen/RegisterCoalescer.cpp | 24 ++++++------ lib/CodeGen/RegisterScavenging.cpp | 9 ++--- lib/CodeGen/ShadowStackGCLowering.cpp | 4 +- lib/CodeGen/SlotIndexes.cpp | 10 ++--- lib/CodeGen/SpillPlacement.cpp | 46 +++++++++++------------ lib/CodeGen/SplitKit.cpp | 4 +- lib/CodeGen/StackSlotColoring.cpp | 13 ++----- lib/CodeGen/TwoAddressInstructionPass.cpp | 5 +-- 9 files changed, 54 insertions(+), 72 deletions(-) diff --git a/lib/CodeGen/RegAllocPBQP.cpp b/lib/CodeGen/RegAllocPBQP.cpp index 7c5af1a0c56..73290f15c43 100644 --- a/lib/CodeGen/RegAllocPBQP.cpp +++ b/lib/CodeGen/RegAllocPBQP.cpp @@ -703,9 +703,8 @@ void RegAllocPBQP::spillVReg(Register VReg, // Copy any newly inserted live intervals into the list of regs to // allocate. - for (LiveRangeEdit::iterator I = LRE.begin(), E = LRE.end(); - I != E; ++I) { - const LiveInterval &LI = LIS.getInterval(*I); + for (const Register &R : LRE) { + const LiveInterval &LI = LIS.getInterval(R); assert(!LI.empty() && "Empty spill range."); LLVM_DEBUG(dbgs() << printReg(LI.reg(), &TRI) << " "); VRegsToAlloc.insert(LI.reg()); @@ -759,10 +758,8 @@ void RegAllocPBQP::finalizeAlloc(MachineFunction &MF, MachineRegisterInfo &MRI = MF.getRegInfo(); // First allocate registers for the empty intervals. - for (RegSet::const_iterator - I = EmptyIntervalVRegs.begin(), E = EmptyIntervalVRegs.end(); - I != E; ++I) { - LiveInterval &LI = LIS.getInterval(*I); + for (const Register &R : EmptyIntervalVRegs) { + LiveInterval &LI = LIS.getInterval(R); Register PReg = MRI.getSimpleHint(LI.reg()); diff --git a/lib/CodeGen/RegisterCoalescer.cpp b/lib/CodeGen/RegisterCoalescer.cpp index 5f770d6b0bc..3e4b2ba0c1a 100644 --- a/lib/CodeGen/RegisterCoalescer.cpp +++ b/lib/CodeGen/RegisterCoalescer.cpp @@ -3877,21 +3877,20 @@ RegisterCoalescer::copyCoalesceInMBB(MachineBasicBlock *MBB) { // are not inherently easier to resolve, but slightly preferable until we // have local live range splitting. In particular this is required by // cmp+jmp macro fusion. - for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end(); - MII != E; ++MII) { - if (!MII->isCopyLike()) + for (MachineInstr &MI : *MBB) { + if (!MI.isCopyLike()) continue; - bool ApplyTerminalRule = applyTerminalRule(*MII); - if (isLocalCopy(&(*MII), LIS)) { + bool ApplyTerminalRule = applyTerminalRule(MI); + if (isLocalCopy(&MI, LIS)) { if (ApplyTerminalRule) - LocalTerminals.push_back(&(*MII)); + LocalTerminals.push_back(&MI); else - LocalWorkList.push_back(&(*MII)); + LocalWorkList.push_back(&MI); } else { if (ApplyTerminalRule) - GlobalTerminals.push_back(&(*MII)); + GlobalTerminals.push_back(&MI); else - WorkList.push_back(&(*MII)); + WorkList.push_back(&MI); } } // Append the copies evicted by the terminal rule at the end of the list. @@ -3935,10 +3934,9 @@ void RegisterCoalescer::joinAllIntervals() { std::vector MBBs; MBBs.reserve(MF->size()); - for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) { - MachineBasicBlock *MBB = &*I; - MBBs.push_back(MBBPriorityInfo(MBB, Loops->getLoopDepth(MBB), - JoinSplitEdges && isSplitEdge(MBB))); + for (MachineBasicBlock &MBB : *MF) { + MBBs.push_back(MBBPriorityInfo(&MBB, Loops->getLoopDepth(&MBB), + JoinSplitEdges && isSplitEdge(&MBB))); } array_pod_sort(MBBs.begin(), MBBs.end(), compareMBBPriority); diff --git a/lib/CodeGen/RegisterScavenging.cpp b/lib/CodeGen/RegisterScavenging.cpp index a833895c115..e885bfd5956 100644 --- a/lib/CodeGen/RegisterScavenging.cpp +++ b/lib/CodeGen/RegisterScavenging.cpp @@ -167,13 +167,12 @@ void RegScavenger::forward() { MachineInstr &MI = *MBBI; - for (SmallVectorImpl::iterator I = Scavenged.begin(), - IE = Scavenged.end(); I != IE; ++I) { - if (I->Restore != &MI) + for (ScavengedInfo &I : Scavenged) { + if (I.Restore != &MI) continue; - I->Reg = 0; - I->Restore = nullptr; + I.Reg = 0; + I.Restore = nullptr; } if (MI.isDebugInstr()) diff --git a/lib/CodeGen/ShadowStackGCLowering.cpp b/lib/CodeGen/ShadowStackGCLowering.cpp index f2111a021ef..36752ef8652 100644 --- a/lib/CodeGen/ShadowStackGCLowering.cpp +++ b/lib/CodeGen/ShadowStackGCLowering.cpp @@ -238,8 +238,8 @@ void ShadowStackGCLowering::CollectRoots(Function &F) { SmallVector, 16> MetaRoots; - for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) - for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) + for (BasicBlock &BB : F) + for (BasicBlock::iterator II = BB.begin(), E = BB.end(); II != E;) if (IntrinsicInst *CI = dyn_cast(II++)) if (Function *F = CI->getCalledFunction()) if (F->getIntrinsicID() == Intrinsic::gcroot) { diff --git a/lib/CodeGen/SlotIndexes.cpp b/lib/CodeGen/SlotIndexes.cpp index d2bfdc663ed..c72bcf2c84d 100644 --- a/lib/CodeGen/SlotIndexes.cpp +++ b/lib/CodeGen/SlotIndexes.cpp @@ -248,12 +248,11 @@ void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB, #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) LLVM_DUMP_METHOD void SlotIndexes::dump() const { - for (IndexList::const_iterator itr = indexList.begin(); - itr != indexList.end(); ++itr) { - dbgs() << itr->getIndex() << " "; + for (const IndexListEntry &ILE : indexList) { + dbgs() << ILE.getIndex() << " "; - if (itr->getInstr()) { - dbgs() << *itr->getInstr(); + if (ILE.getInstr()) { + dbgs() << *ILE.getInstr(); } else { dbgs() << "\n"; } @@ -280,4 +279,3 @@ LLVM_DUMP_METHOD void SlotIndex::dump() const { dbgs() << "\n"; } #endif - diff --git a/lib/CodeGen/SpillPlacement.cpp b/lib/CodeGen/SpillPlacement.cpp index 6f91092097d..91da5e49713 100644 --- a/lib/CodeGen/SpillPlacement.cpp +++ b/lib/CodeGen/SpillPlacement.cpp @@ -121,9 +121,9 @@ struct SpillPlacement::Node { SumLinkWeights += w; // There can be multiple links to the same bundle, add them up. - for (LinkVector::iterator I = Links.begin(), E = Links.end(); I != E; ++I) - if (I->second == b) { - I->first += w; + for (std::pair &L : Links) + if (L.second == b) { + L.first += w; return; } // This must be the first link to b. @@ -153,11 +153,11 @@ struct SpillPlacement::Node { // Compute the weighted sum of inputs. BlockFrequency SumN = BiasN; BlockFrequency SumP = BiasP; - for (LinkVector::iterator I = Links.begin(), E = Links.end(); I != E; ++I) { - if (nodes[I->second].Value == -1) - SumN += I->first; - else if (nodes[I->second].Value == 1) - SumP += I->first; + for (std::pair &L : Links) { + if (nodes[L.second].Value == -1) + SumN += L.first; + else if (nodes[L.second].Value == 1) + SumP += L.first; } // Each weighted sum is going to be less than the total frequency of the @@ -258,35 +258,33 @@ void SpillPlacement::setThreshold(const BlockFrequency &Entry) { /// addConstraints - Compute node biases and weights from a set of constraints. /// Set a bit in NodeMask for each active node. void SpillPlacement::addConstraints(ArrayRef LiveBlocks) { - for (ArrayRef::iterator I = LiveBlocks.begin(), - E = LiveBlocks.end(); I != E; ++I) { - BlockFrequency Freq = BlockFrequencies[I->Number]; + for (const BlockConstraint &LB : LiveBlocks) { + BlockFrequency Freq = BlockFrequencies[LB.Number]; // Live-in to block? - if (I->Entry != DontCare) { - unsigned ib = bundles->getBundle(I->Number, false); + if (LB.Entry != DontCare) { + unsigned ib = bundles->getBundle(LB.Number, false); activate(ib); - nodes[ib].addBias(Freq, I->Entry); + nodes[ib].addBias(Freq, LB.Entry); } // Live-out from block? - if (I->Exit != DontCare) { - unsigned ob = bundles->getBundle(I->Number, true); + if (LB.Exit != DontCare) { + unsigned ob = bundles->getBundle(LB.Number, true); activate(ob); - nodes[ob].addBias(Freq, I->Exit); + nodes[ob].addBias(Freq, LB.Exit); } } } /// addPrefSpill - Same as addConstraints(PrefSpill) void SpillPlacement::addPrefSpill(ArrayRef Blocks, bool Strong) { - for (ArrayRef::iterator I = Blocks.begin(), E = Blocks.end(); - I != E; ++I) { - BlockFrequency Freq = BlockFrequencies[*I]; + for (unsigned B : Blocks) { + BlockFrequency Freq = BlockFrequencies[B]; if (Strong) Freq += Freq; - unsigned ib = bundles->getBundle(*I, false); - unsigned ob = bundles->getBundle(*I, true); + unsigned ib = bundles->getBundle(B, false); + unsigned ob = bundles->getBundle(B, true); activate(ib); activate(ob); nodes[ib].addBias(Freq, PrefSpill); @@ -295,9 +293,7 @@ void SpillPlacement::addPrefSpill(ArrayRef Blocks, bool Strong) { } void SpillPlacement::addLinks(ArrayRef Links) { - for (ArrayRef::iterator I = Links.begin(), E = Links.end(); I != E; - ++I) { - unsigned Number = *I; + for (unsigned Number : Links) { unsigned ib = bundles->getBundle(Number, false); unsigned ob = bundles->getBundle(Number, true); diff --git a/lib/CodeGen/SplitKit.cpp b/lib/CodeGen/SplitKit.cpp index 5f97729bd9d..d3e75ca6001 100644 --- a/lib/CodeGen/SplitKit.cpp +++ b/lib/CodeGen/SplitKit.cpp @@ -1364,8 +1364,8 @@ void SplitEditor::rewriteAssigned(bool ExtendRanges) { void SplitEditor::deleteRematVictims() { SmallVector Dead; - for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){ - LiveInterval *LI = &LIS.getInterval(*I); + for (const Register &R : *Edit) { + LiveInterval *LI = &LIS.getInterval(R); for (const LiveRange::Segment &S : LI->segments) { // Dead defs end at the dead slot. if (S.end != S.valno->def.getDeadSlot()) diff --git a/lib/CodeGen/StackSlotColoring.cpp b/lib/CodeGen/StackSlotColoring.cpp index a6f8974f334..bd5653f63a6 100644 --- a/lib/CodeGen/StackSlotColoring.cpp +++ b/lib/CodeGen/StackSlotColoring.cpp @@ -157,12 +157,8 @@ void StackSlotColoring::ScanForSpillSlotRefs(MachineFunction &MF) { SSRefs.resize(MFI->getObjectIndexEnd()); // FIXME: Need the equivalent of MachineRegisterInfo for frameindex operands. - for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end(); - MBBI != E; ++MBBI) { - MachineBasicBlock *MBB = &*MBBI; - for (MachineBasicBlock::iterator MII = MBB->begin(), EE = MBB->end(); - MII != EE; ++MII) { - MachineInstr &MI = *MII; + for (MachineBasicBlock &MBB : MF) { + for (MachineInstr &MI : MBB) { for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { MachineOperand &MO = MI.getOperand(i); if (!MO.isFI()) @@ -474,9 +470,8 @@ bool StackSlotColoring::RemoveDeadStores(MachineBasicBlock* MBB) { ++I; } - for (SmallVectorImpl::iterator I = toErase.begin(), - E = toErase.end(); I != E; ++I) - (*I)->eraseFromParent(); + for (MachineInstr *MI : toErase) + MI->eraseFromParent(); return changed; } diff --git a/lib/CodeGen/TwoAddressInstructionPass.cpp b/lib/CodeGen/TwoAddressInstructionPass.cpp index 2a9132bd2fe..a011b03d747 100644 --- a/lib/CodeGen/TwoAddressInstructionPass.cpp +++ b/lib/CodeGen/TwoAddressInstructionPass.cpp @@ -1549,9 +1549,8 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &Func) { .set(MachineFunctionProperties::Property::TiedOpsRewritten); TiedOperandMap TiedOperands; - for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end(); - MBBI != MBBE; ++MBBI) { - MBB = &*MBBI; + for (MachineBasicBlock &MBBI : *MF) { + MBB = &MBBI; unsigned Dist = 0; DistanceMap.clear(); SrcRegMap.clear();