Skip to content

Commit 1dd97cd

Browse files
artagnonyuxuanchen1997
authored andcommitted
MTM: fix issues after cursory reading (#100404)
Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250547
1 parent 3131eef commit 1dd97cd

File tree

1 file changed

+46
-55
lines changed

1 file changed

+46
-55
lines changed

llvm/lib/CodeGen/MachineTraceMetrics.cpp

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,11 @@
2424
#include "llvm/CodeGen/TargetSchedule.h"
2525
#include "llvm/CodeGen/TargetSubtargetInfo.h"
2626
#include "llvm/InitializePasses.h"
27-
#include "llvm/MC/MCRegisterInfo.h"
2827
#include "llvm/Pass.h"
2928
#include "llvm/Support/Debug.h"
3029
#include "llvm/Support/ErrorHandling.h"
3130
#include "llvm/Support/Format.h"
3231
#include "llvm/Support/raw_ostream.h"
33-
#include <algorithm>
34-
#include <cassert>
35-
#include <iterator>
36-
#include <tuple>
37-
#include <utility>
3832

3933
using namespace llvm;
4034

@@ -133,7 +127,7 @@ MachineTraceMetrics::getResources(const MachineBasicBlock *MBB) {
133127

134128
// Scale the resource cycles so they are comparable.
135129
unsigned PROffset = MBB->getNumber() * PRKinds;
136-
for (unsigned K = 0; K != PRKinds; ++K)
130+
for (unsigned K = 0; K < PRKinds; ++K)
137131
ProcReleaseAtCycles[PROffset + K] =
138132
PRCycles[K] * SchedModel.getResourceFactor(K);
139133

@@ -146,15 +140,14 @@ MachineTraceMetrics::getProcReleaseAtCycles(unsigned MBBNum) const {
146140
"getResources() must be called before getProcReleaseAtCycles()");
147141
unsigned PRKinds = SchedModel.getNumProcResourceKinds();
148142
assert((MBBNum+1) * PRKinds <= ProcReleaseAtCycles.size());
149-
return ArrayRef(ProcReleaseAtCycles.data() + MBBNum * PRKinds, PRKinds);
143+
return ArrayRef{ProcReleaseAtCycles.data() + MBBNum * PRKinds, PRKinds};
150144
}
151145

152146
//===----------------------------------------------------------------------===//
153147
// Ensemble utility functions
154148
//===----------------------------------------------------------------------===//
155149

156-
MachineTraceMetrics::Ensemble::Ensemble(MachineTraceMetrics *ct)
157-
: MTM(*ct) {
150+
MachineTraceMetrics::Ensemble::Ensemble(MachineTraceMetrics *CT) : MTM(*CT) {
158151
BlockInfo.resize(MTM.BlockInfo.size());
159152
unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
160153
ProcResourceDepths.resize(MTM.BlockInfo.size() * PRKinds);
@@ -198,7 +191,7 @@ computeDepthResources(const MachineBasicBlock *MBB) {
198191
// Compute per-resource depths.
199192
ArrayRef<unsigned> PredPRDepths = getProcResourceDepths(PredNum);
200193
ArrayRef<unsigned> PredPRCycles = MTM.getProcReleaseAtCycles(PredNum);
201-
for (unsigned K = 0; K != PRKinds; ++K)
194+
for (unsigned K = 0; K < PRKinds; ++K)
202195
ProcResourceDepths[PROffset + K] = PredPRDepths[K] + PredPRCycles[K];
203196
}
204197

@@ -231,7 +224,7 @@ computeHeightResources(const MachineBasicBlock *MBB) {
231224

232225
// Compute per-resource heights.
233226
ArrayRef<unsigned> SuccPRHeights = getProcResourceHeights(SuccNum);
234-
for (unsigned K = 0; K != PRKinds; ++K)
227+
for (unsigned K = 0; K < PRKinds; ++K)
235228
ProcResourceHeights[PROffset + K] = SuccPRHeights[K] + PRCycles[K];
236229
}
237230

@@ -264,7 +257,7 @@ MachineTraceMetrics::Ensemble::
264257
getProcResourceDepths(unsigned MBBNum) const {
265258
unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
266259
assert((MBBNum+1) * PRKinds <= ProcResourceDepths.size());
267-
return ArrayRef(ProcResourceDepths.data() + MBBNum * PRKinds, PRKinds);
260+
return ArrayRef{ProcResourceDepths.data() + MBBNum * PRKinds, PRKinds};
268261
}
269262

270263
/// Get an array of processor resource heights for MBB. Indexed by processor
@@ -277,7 +270,7 @@ MachineTraceMetrics::Ensemble::
277270
getProcResourceHeights(unsigned MBBNum) const {
278271
unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
279272
assert((MBBNum+1) * PRKinds <= ProcResourceHeights.size());
280-
return ArrayRef(ProcResourceHeights.data() + MBBNum * PRKinds, PRKinds);
273+
return ArrayRef{ProcResourceHeights.data() + MBBNum * PRKinds, PRKinds};
281274
}
282275

283276
//===----------------------------------------------------------------------===//
@@ -314,8 +307,8 @@ class MinInstrCountEnsemble : public MachineTraceMetrics::Ensemble {
314307
const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*) override;
315308

316309
public:
317-
MinInstrCountEnsemble(MachineTraceMetrics *mtm)
318-
: MachineTraceMetrics::Ensemble(mtm) {}
310+
MinInstrCountEnsemble(MachineTraceMetrics *MTM)
311+
: MachineTraceMetrics::Ensemble(MTM) {}
319312
};
320313

321314
/// Pick only the current basic block for the trace and do not choose any
@@ -395,15 +388,15 @@ MinInstrCountEnsemble::pickTraceSucc(const MachineBasicBlock *MBB) {
395388

396389
// Get an Ensemble sub-class for the requested trace strategy.
397390
MachineTraceMetrics::Ensemble *
398-
MachineTraceMetrics::getEnsemble(MachineTraceStrategy strategy) {
399-
assert(strategy < MachineTraceStrategy::TS_NumStrategies &&
391+
MachineTraceMetrics::getEnsemble(MachineTraceStrategy Strategy) {
392+
assert(Strategy < MachineTraceStrategy::TS_NumStrategies &&
400393
"Invalid trace strategy enum");
401-
Ensemble *&E = Ensembles[static_cast<size_t>(strategy)];
394+
Ensemble *&E = Ensembles[static_cast<size_t>(Strategy)];
402395
if (E)
403396
return E;
404397

405398
// Allocate new Ensemble on demand.
406-
switch (strategy) {
399+
switch (Strategy) {
407400
case MachineTraceStrategy::TS_MinInstrCount:
408401
return (E = new MinInstrCountEnsemble(this));
409402
case MachineTraceStrategy::TS_Local:
@@ -448,8 +441,9 @@ struct LoopBounds {
448441
const MachineLoopInfo *Loops;
449442
bool Downward = false;
450443

451-
LoopBounds(MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> blocks,
452-
const MachineLoopInfo *loops) : Blocks(blocks), Loops(loops) {}
444+
LoopBounds(MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> Blocks,
445+
const MachineLoopInfo *Loops)
446+
: Blocks(Blocks), Loops(Loops) {}
453447
};
454448

455449
} // end anonymous namespace
@@ -463,7 +457,7 @@ class po_iterator_storage<LoopBounds, true> {
463457
LoopBounds &LB;
464458

465459
public:
466-
po_iterator_storage(LoopBounds &lb) : LB(lb) {}
460+
po_iterator_storage(LoopBounds &LB) : LB(LB) {}
467461

468462
void finishPostorder(const MachineBasicBlock*) {}
469463

@@ -546,7 +540,7 @@ MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
546540
if (BadTBI.hasValidHeight()) {
547541
BadTBI.invalidateHeight();
548542
WorkList.push_back(BadMBB);
549-
do {
543+
while (!WorkList.empty()) {
550544
const MachineBasicBlock *MBB = WorkList.pop_back_val();
551545
LLVM_DEBUG(dbgs() << "Invalidate " << printMBBReference(*MBB) << ' '
552546
<< getName() << " height.\n");
@@ -564,14 +558,14 @@ MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
564558
// Verify that TBI.Succ is actually a *I successor.
565559
assert((!TBI.Succ || Pred->isSuccessor(TBI.Succ)) && "CFG changed");
566560
}
567-
} while (!WorkList.empty());
561+
}
568562
}
569563

570564
// Invalidate depth resources of blocks below MBB.
571565
if (BadTBI.hasValidDepth()) {
572566
BadTBI.invalidateDepth();
573567
WorkList.push_back(BadMBB);
574-
do {
568+
while (!WorkList.empty()) {
575569
const MachineBasicBlock *MBB = WorkList.pop_back_val();
576570
LLVM_DEBUG(dbgs() << "Invalidate " << printMBBReference(*MBB) << ' '
577571
<< getName() << " depth.\n");
@@ -589,7 +583,7 @@ MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
589583
// Verify that TBI.Pred is actually a *I predecessor.
590584
assert((!TBI.Pred || Succ->isPredecessor(TBI.Pred)) && "CFG changed");
591585
}
592-
} while (!WorkList.empty());
586+
}
593587
}
594588

595589
// Clear any per-instruction data. We only have to do this for BadMBB itself
@@ -605,7 +599,7 @@ void MachineTraceMetrics::Ensemble::verify() const {
605599
#ifndef NDEBUG
606600
assert(BlockInfo.size() == MTM.MF->getNumBlockIDs() &&
607601
"Outdated BlockInfo size");
608-
for (unsigned Num = 0, e = BlockInfo.size(); Num != e; ++Num) {
602+
for (unsigned Num = 0; Num < BlockInfo.size(); ++Num) {
609603
const TraceBlockInfo &TBI = BlockInfo[Num];
610604
if (TBI.hasValidDepth() && TBI.Pred) {
611605
const MachineBasicBlock *MBB = MTM.MF->getBlockNumbered(Num);
@@ -686,7 +680,7 @@ static bool getDataDeps(const MachineInstr &UseMI,
686680
}
687681
// Collect virtual register reads.
688682
if (MO.readsReg())
689-
Deps.push_back(DataDep(MRI, Reg, MO.getOperandNo()));
683+
Deps.emplace_back(MRI, Reg, MO.getOperandNo());
690684
}
691685
return HasPhysRegs;
692686
}
@@ -702,10 +696,10 @@ static void getPHIDeps(const MachineInstr &UseMI,
702696
if (!Pred)
703697
return;
704698
assert(UseMI.isPHI() && UseMI.getNumOperands() % 2 && "Bad PHI");
705-
for (unsigned i = 1; i != UseMI.getNumOperands(); i += 2) {
706-
if (UseMI.getOperand(i + 1).getMBB() == Pred) {
707-
Register Reg = UseMI.getOperand(i).getReg();
708-
Deps.push_back(DataDep(MRI, Reg, i));
699+
for (unsigned Idx = 1; Idx < UseMI.getNumOperands(); Idx += 2) {
700+
if (UseMI.getOperand(Idx + 1).getMBB() == Pred) {
701+
Register Reg = UseMI.getOperand(Idx).getReg();
702+
Deps.emplace_back(MRI, Reg, Idx);
709703
return;
710704
}
711705
}
@@ -739,7 +733,7 @@ static void updatePhysDepsDownwards(const MachineInstr *UseMI,
739733
SparseSet<LiveRegUnit>::iterator I = RegUnits.find(Unit);
740734
if (I == RegUnits.end())
741735
continue;
742-
Deps.push_back(DataDep(I->MI, I->Op, MO.getOperandNo()));
736+
Deps.emplace_back(I->MI, I->Op, MO.getOperandNo());
743737
break;
744738
}
745739
}
@@ -852,14 +846,14 @@ computeInstrDepths(const MachineBasicBlock *MBB) {
852846
// implies Head->HasValidInstrDepths, so we only need to start from the first
853847
// block in the trace that needs to be recomputed.
854848
SmallVector<const MachineBasicBlock*, 8> Stack;
855-
do {
849+
while (MBB) {
856850
TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
857851
assert(TBI.hasValidDepth() && "Incomplete trace");
858852
if (TBI.HasValidInstrDepths)
859853
break;
860854
Stack.push_back(MBB);
861855
MBB = TBI.Pred;
862-
} while (MBB);
856+
}
863857

864858
// FIXME: If MBB is non-null at this point, it is the last pre-computed block
865859
// in the trace. We should track any live-out physregs that were defined in
@@ -880,7 +874,7 @@ computeInstrDepths(const MachineBasicBlock *MBB) {
880874
LLVM_DEBUG({
881875
dbgs() << format("%7u Instructions\n", TBI.InstrDepth);
882876
ArrayRef<unsigned> PRDepths = getProcResourceDepths(MBB->getNumber());
883-
for (unsigned K = 0; K != PRDepths.size(); ++K)
877+
for (unsigned K = 0; K < PRDepths.size(); ++K)
884878
if (PRDepths[K]) {
885879
unsigned Factor = MTM.SchedModel.getResourceFactor(K);
886880
dbgs() << format("%6uc @ ", MTM.getCycles(PRDepths[K]))
@@ -969,10 +963,8 @@ static bool pushDepHeight(const DataDep &Dep, const MachineInstr &UseMI,
969963
Dep.UseOp);
970964

971965
// Update Heights[DefMI] to be the maximum height seen.
972-
MIHeightMap::iterator I;
973-
bool New;
974-
std::tie(I, New) = Heights.insert(std::make_pair(Dep.DefMI, UseHeight));
975-
if (New)
966+
const auto &[I, Inserted] = Heights.insert({Dep.DefMI, UseHeight});
967+
if (Inserted)
976968
return true;
977969

978970
// DefMI has been pushed before. Give it the max height.
@@ -1010,15 +1002,15 @@ computeInstrHeights(const MachineBasicBlock *MBB) {
10101002
// The bottom of the trace may already be computed.
10111003
// Find the blocks that need updating.
10121004
SmallVector<const MachineBasicBlock*, 8> Stack;
1013-
do {
1005+
while (MBB) {
10141006
TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
10151007
assert(TBI.hasValidHeight() && "Incomplete trace");
10161008
if (TBI.HasValidInstrHeights)
10171009
break;
10181010
Stack.push_back(MBB);
10191011
TBI.LiveIns.clear();
10201012
MBB = TBI.Succ;
1021-
} while (MBB);
1013+
}
10221014

10231015
// As we move upwards in the trace, keep track of instructions that are
10241016
// required by deeper trace instructions. Map MI -> height required so far.
@@ -1060,7 +1052,7 @@ computeInstrHeights(const MachineBasicBlock *MBB) {
10601052
LLVM_DEBUG({
10611053
dbgs() << format("%7u Instructions\n", TBI.InstrHeight);
10621054
ArrayRef<unsigned> PRHeights = getProcResourceHeights(MBB->getNumber());
1063-
for (unsigned K = 0; K != PRHeights.size(); ++K)
1055+
for (unsigned K = 0; K < PRHeights.size(); ++K)
10641056
if (PRHeights[K]) {
10651057
unsigned Factor = MTM.SchedModel.getResourceFactor(K);
10661058
dbgs() << format("%6uc @ ", MTM.getCycles(PRHeights[K]))
@@ -1145,7 +1137,7 @@ computeInstrHeights(const MachineBasicBlock *MBB) {
11451137

11461138
// Transfer the live regunits to the live-in list.
11471139
for (const LiveRegUnit &RU : RegUnits) {
1148-
TBI.LiveIns.push_back(LiveInReg(RU.RegUnit, RU.Cycle));
1140+
TBI.LiveIns.emplace_back(RU.RegUnit, RU.Cycle);
11491141
LLVM_DEBUG(dbgs() << ' ' << printRegUnit(RU.RegUnit, MTM.TRI) << '@'
11501142
<< RU.Cycle);
11511143
}
@@ -1205,7 +1197,7 @@ unsigned MachineTraceMetrics::Trace::getResourceDepth(bool Bottom) const {
12051197
ArrayRef<unsigned> PRDepths = TE.getProcResourceDepths(getBlockNum());
12061198
if (Bottom) {
12071199
ArrayRef<unsigned> PRCycles = TE.MTM.getProcReleaseAtCycles(getBlockNum());
1208-
for (unsigned K = 0; K != PRDepths.size(); ++K)
1200+
for (unsigned K = 0; K < PRDepths.size(); ++K)
12091201
PRMax = std::max(PRMax, PRDepths[K] + PRCycles[K]);
12101202
} else {
12111203
for (unsigned PRD : PRDepths)
@@ -1235,9 +1227,8 @@ unsigned MachineTraceMetrics::Trace::getResourceLength(
12351227
unsigned PRMax = 0;
12361228

12371229
// Capture computing cycles from extra instructions
1238-
auto extraCycles = [this](ArrayRef<const MCSchedClassDesc *> Instrs,
1239-
unsigned ResourceIdx)
1240-
->unsigned {
1230+
auto ExtraCycles = [this](ArrayRef<const MCSchedClassDesc *> Instrs,
1231+
unsigned ResourceIdx) -> unsigned {
12411232
unsigned Cycles = 0;
12421233
for (const MCSchedClassDesc *SC : Instrs) {
12431234
if (!SC->isValid())
@@ -1255,12 +1246,12 @@ unsigned MachineTraceMetrics::Trace::getResourceLength(
12551246
return Cycles;
12561247
};
12571248

1258-
for (unsigned K = 0; K != PRDepths.size(); ++K) {
1249+
for (unsigned K = 0; K < PRDepths.size(); ++K) {
12591250
unsigned PRCycles = PRDepths[K] + PRHeights[K];
12601251
for (const MachineBasicBlock *MBB : Extrablocks)
12611252
PRCycles += TE.MTM.getProcReleaseAtCycles(MBB->getNumber())[K];
1262-
PRCycles += extraCycles(ExtraInstrs, K);
1263-
PRCycles -= extraCycles(RemoveInstrs, K);
1253+
PRCycles += ExtraCycles(ExtraInstrs, K);
1254+
PRCycles -= ExtraCycles(RemoveInstrs, K);
12641255
PRMax = std::max(PRMax, PRCycles);
12651256
}
12661257
// Convert to cycle count.
@@ -1292,9 +1283,9 @@ bool MachineTraceMetrics::Trace::isDepInTrace(const MachineInstr &DefMI,
12921283

12931284
void MachineTraceMetrics::Ensemble::print(raw_ostream &OS) const {
12941285
OS << getName() << " ensemble:\n";
1295-
for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
1296-
OS << " %bb." << i << '\t';
1297-
BlockInfo[i].print(OS);
1286+
for (unsigned Idx = 0; Idx < BlockInfo.size(); ++Idx) {
1287+
OS << " %bb." << Idx << '\t';
1288+
BlockInfo[Idx].print(OS);
12981289
OS << '\n';
12991290
}
13001291
}

0 commit comments

Comments
 (0)