24
24
#include " llvm/CodeGen/TargetSchedule.h"
25
25
#include " llvm/CodeGen/TargetSubtargetInfo.h"
26
26
#include " llvm/InitializePasses.h"
27
+ #include " llvm/MC/MCRegisterInfo.h"
27
28
#include " llvm/Pass.h"
28
29
#include " llvm/Support/Debug.h"
29
30
#include " llvm/Support/ErrorHandling.h"
30
31
#include " llvm/Support/Format.h"
31
32
#include " llvm/Support/raw_ostream.h"
33
+ #include < algorithm>
34
+ #include < cassert>
35
+ #include < iterator>
36
+ #include < tuple>
37
+ #include < utility>
32
38
33
39
using namespace llvm ;
34
40
@@ -127,7 +133,7 @@ MachineTraceMetrics::getResources(const MachineBasicBlock *MBB) {
127
133
128
134
// Scale the resource cycles so they are comparable.
129
135
unsigned PROffset = MBB->getNumber () * PRKinds;
130
- for (unsigned K = 0 ; K < PRKinds; ++K)
136
+ for (unsigned K = 0 ; K != PRKinds; ++K)
131
137
ProcReleaseAtCycles[PROffset + K] =
132
138
PRCycles[K] * SchedModel.getResourceFactor (K);
133
139
@@ -140,14 +146,15 @@ MachineTraceMetrics::getProcReleaseAtCycles(unsigned MBBNum) const {
140
146
" getResources() must be called before getProcReleaseAtCycles()" );
141
147
unsigned PRKinds = SchedModel.getNumProcResourceKinds ();
142
148
assert ((MBBNum+1 ) * PRKinds <= ProcReleaseAtCycles.size ());
143
- return ArrayRef{ ProcReleaseAtCycles.data () + MBBNum * PRKinds, PRKinds} ;
149
+ return ArrayRef ( ProcReleaseAtCycles.data () + MBBNum * PRKinds, PRKinds) ;
144
150
}
145
151
146
152
// ===----------------------------------------------------------------------===//
147
153
// Ensemble utility functions
148
154
// ===----------------------------------------------------------------------===//
149
155
150
- MachineTraceMetrics::Ensemble::Ensemble (MachineTraceMetrics *CT) : MTM(*CT) {
156
+ MachineTraceMetrics::Ensemble::Ensemble (MachineTraceMetrics *ct)
157
+ : MTM(*ct) {
151
158
BlockInfo.resize (MTM.BlockInfo .size ());
152
159
unsigned PRKinds = MTM.SchedModel .getNumProcResourceKinds ();
153
160
ProcResourceDepths.resize (MTM.BlockInfo .size () * PRKinds);
@@ -191,7 +198,7 @@ computeDepthResources(const MachineBasicBlock *MBB) {
191
198
// Compute per-resource depths.
192
199
ArrayRef<unsigned > PredPRDepths = getProcResourceDepths (PredNum);
193
200
ArrayRef<unsigned > PredPRCycles = MTM.getProcReleaseAtCycles (PredNum);
194
- for (unsigned K = 0 ; K < PRKinds; ++K)
201
+ for (unsigned K = 0 ; K != PRKinds; ++K)
195
202
ProcResourceDepths[PROffset + K] = PredPRDepths[K] + PredPRCycles[K];
196
203
}
197
204
@@ -224,7 +231,7 @@ computeHeightResources(const MachineBasicBlock *MBB) {
224
231
225
232
// Compute per-resource heights.
226
233
ArrayRef<unsigned > SuccPRHeights = getProcResourceHeights (SuccNum);
227
- for (unsigned K = 0 ; K < PRKinds; ++K)
234
+ for (unsigned K = 0 ; K != PRKinds; ++K)
228
235
ProcResourceHeights[PROffset + K] = SuccPRHeights[K] + PRCycles[K];
229
236
}
230
237
@@ -257,7 +264,7 @@ MachineTraceMetrics::Ensemble::
257
264
getProcResourceDepths (unsigned MBBNum) const {
258
265
unsigned PRKinds = MTM.SchedModel .getNumProcResourceKinds ();
259
266
assert ((MBBNum+1 ) * PRKinds <= ProcResourceDepths.size ());
260
- return ArrayRef{ ProcResourceDepths.data () + MBBNum * PRKinds, PRKinds} ;
267
+ return ArrayRef ( ProcResourceDepths.data () + MBBNum * PRKinds, PRKinds) ;
261
268
}
262
269
263
270
// / Get an array of processor resource heights for MBB. Indexed by processor
@@ -270,7 +277,7 @@ MachineTraceMetrics::Ensemble::
270
277
getProcResourceHeights (unsigned MBBNum) const {
271
278
unsigned PRKinds = MTM.SchedModel .getNumProcResourceKinds ();
272
279
assert ((MBBNum+1 ) * PRKinds <= ProcResourceHeights.size ());
273
- return ArrayRef{ ProcResourceHeights.data () + MBBNum * PRKinds, PRKinds} ;
280
+ return ArrayRef ( ProcResourceHeights.data () + MBBNum * PRKinds, PRKinds) ;
274
281
}
275
282
276
283
// ===----------------------------------------------------------------------===//
@@ -307,8 +314,8 @@ class MinInstrCountEnsemble : public MachineTraceMetrics::Ensemble {
307
314
const MachineBasicBlock *pickTraceSucc (const MachineBasicBlock*) override ;
308
315
309
316
public:
310
- MinInstrCountEnsemble (MachineTraceMetrics *MTM )
311
- : MachineTraceMetrics::Ensemble(MTM ) {}
317
+ MinInstrCountEnsemble (MachineTraceMetrics *mtm )
318
+ : MachineTraceMetrics::Ensemble(mtm ) {}
312
319
};
313
320
314
321
// / Pick only the current basic block for the trace and do not choose any
@@ -388,15 +395,15 @@ MinInstrCountEnsemble::pickTraceSucc(const MachineBasicBlock *MBB) {
388
395
389
396
// Get an Ensemble sub-class for the requested trace strategy.
390
397
MachineTraceMetrics::Ensemble *
391
- MachineTraceMetrics::getEnsemble (MachineTraceStrategy Strategy ) {
392
- assert (Strategy < MachineTraceStrategy::TS_NumStrategies &&
398
+ MachineTraceMetrics::getEnsemble (MachineTraceStrategy strategy ) {
399
+ assert (strategy < MachineTraceStrategy::TS_NumStrategies &&
393
400
" Invalid trace strategy enum" );
394
- Ensemble *&E = Ensembles[static_cast <size_t >(Strategy )];
401
+ Ensemble *&E = Ensembles[static_cast <size_t >(strategy )];
395
402
if (E)
396
403
return E;
397
404
398
405
// Allocate new Ensemble on demand.
399
- switch (Strategy ) {
406
+ switch (strategy ) {
400
407
case MachineTraceStrategy::TS_MinInstrCount:
401
408
return (E = new MinInstrCountEnsemble (this ));
402
409
case MachineTraceStrategy::TS_Local:
@@ -441,9 +448,8 @@ struct LoopBounds {
441
448
const MachineLoopInfo *Loops;
442
449
bool Downward = false ;
443
450
444
- LoopBounds (MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> Blocks,
445
- const MachineLoopInfo *Loops)
446
- : Blocks(Blocks), Loops(Loops) {}
451
+ LoopBounds (MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> blocks,
452
+ const MachineLoopInfo *loops) : Blocks(blocks), Loops(loops) {}
447
453
};
448
454
449
455
} // end anonymous namespace
@@ -457,7 +463,7 @@ class po_iterator_storage<LoopBounds, true> {
457
463
LoopBounds &LB;
458
464
459
465
public:
460
- po_iterator_storage (LoopBounds &LB ) : LB(LB ) {}
466
+ po_iterator_storage (LoopBounds &lb ) : LB(lb ) {}
461
467
462
468
void finishPostorder (const MachineBasicBlock*) {}
463
469
@@ -540,7 +546,7 @@ MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
540
546
if (BadTBI.hasValidHeight ()) {
541
547
BadTBI.invalidateHeight ();
542
548
WorkList.push_back (BadMBB);
543
- while (!WorkList. empty ()) {
549
+ do {
544
550
const MachineBasicBlock *MBB = WorkList.pop_back_val ();
545
551
LLVM_DEBUG (dbgs () << " Invalidate " << printMBBReference (*MBB) << ' '
546
552
<< getName () << " height.\n " );
@@ -558,14 +564,14 @@ MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
558
564
// Verify that TBI.Succ is actually a *I successor.
559
565
assert ((!TBI.Succ || Pred->isSuccessor (TBI.Succ )) && " CFG changed" );
560
566
}
561
- }
567
+ } while (!WorkList. empty ());
562
568
}
563
569
564
570
// Invalidate depth resources of blocks below MBB.
565
571
if (BadTBI.hasValidDepth ()) {
566
572
BadTBI.invalidateDepth ();
567
573
WorkList.push_back (BadMBB);
568
- while (!WorkList. empty ()) {
574
+ do {
569
575
const MachineBasicBlock *MBB = WorkList.pop_back_val ();
570
576
LLVM_DEBUG (dbgs () << " Invalidate " << printMBBReference (*MBB) << ' '
571
577
<< getName () << " depth.\n " );
@@ -583,7 +589,7 @@ MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
583
589
// Verify that TBI.Pred is actually a *I predecessor.
584
590
assert ((!TBI.Pred || Succ->isPredecessor (TBI.Pred )) && " CFG changed" );
585
591
}
586
- }
592
+ } while (!WorkList. empty ());
587
593
}
588
594
589
595
// Clear any per-instruction data. We only have to do this for BadMBB itself
@@ -599,7 +605,7 @@ void MachineTraceMetrics::Ensemble::verify() const {
599
605
#ifndef NDEBUG
600
606
assert (BlockInfo.size () == MTM.MF ->getNumBlockIDs () &&
601
607
" Outdated BlockInfo size" );
602
- for (unsigned Num = 0 ; Num < BlockInfo.size (); ++Num) {
608
+ for (unsigned Num = 0 , e = BlockInfo.size (); Num != e ; ++Num) {
603
609
const TraceBlockInfo &TBI = BlockInfo[Num];
604
610
if (TBI.hasValidDepth () && TBI.Pred ) {
605
611
const MachineBasicBlock *MBB = MTM.MF ->getBlockNumbered (Num);
@@ -680,7 +686,7 @@ static bool getDataDeps(const MachineInstr &UseMI,
680
686
}
681
687
// Collect virtual register reads.
682
688
if (MO.readsReg ())
683
- Deps.emplace_back ( MRI, Reg, MO.getOperandNo ());
689
+ Deps.push_back ( DataDep ( MRI, Reg, MO.getOperandNo () ));
684
690
}
685
691
return HasPhysRegs;
686
692
}
@@ -696,10 +702,10 @@ static void getPHIDeps(const MachineInstr &UseMI,
696
702
if (!Pred)
697
703
return ;
698
704
assert (UseMI.isPHI () && UseMI.getNumOperands () % 2 && " Bad PHI" );
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 );
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) );
703
709
return ;
704
710
}
705
711
}
@@ -733,7 +739,7 @@ static void updatePhysDepsDownwards(const MachineInstr *UseMI,
733
739
SparseSet<LiveRegUnit>::iterator I = RegUnits.find (Unit);
734
740
if (I == RegUnits.end ())
735
741
continue ;
736
- Deps.emplace_back ( I->MI , I->Op , MO.getOperandNo ());
742
+ Deps.push_back ( DataDep ( I->MI , I->Op , MO.getOperandNo () ));
737
743
break ;
738
744
}
739
745
}
@@ -846,14 +852,14 @@ computeInstrDepths(const MachineBasicBlock *MBB) {
846
852
// implies Head->HasValidInstrDepths, so we only need to start from the first
847
853
// block in the trace that needs to be recomputed.
848
854
SmallVector<const MachineBasicBlock*, 8 > Stack;
849
- while (MBB) {
855
+ do {
850
856
TraceBlockInfo &TBI = BlockInfo[MBB->getNumber ()];
851
857
assert (TBI.hasValidDepth () && " Incomplete trace" );
852
858
if (TBI.HasValidInstrDepths )
853
859
break ;
854
860
Stack.push_back (MBB);
855
861
MBB = TBI.Pred ;
856
- }
862
+ } while (MBB);
857
863
858
864
// FIXME: If MBB is non-null at this point, it is the last pre-computed block
859
865
// in the trace. We should track any live-out physregs that were defined in
@@ -874,7 +880,7 @@ computeInstrDepths(const MachineBasicBlock *MBB) {
874
880
LLVM_DEBUG ({
875
881
dbgs () << format (" %7u Instructions\n " , TBI.InstrDepth );
876
882
ArrayRef<unsigned > PRDepths = getProcResourceDepths (MBB->getNumber ());
877
- for (unsigned K = 0 ; K < PRDepths.size (); ++K)
883
+ for (unsigned K = 0 ; K != PRDepths.size (); ++K)
878
884
if (PRDepths[K]) {
879
885
unsigned Factor = MTM.SchedModel .getResourceFactor (K);
880
886
dbgs () << format (" %6uc @ " , MTM.getCycles (PRDepths[K]))
@@ -963,8 +969,10 @@ static bool pushDepHeight(const DataDep &Dep, const MachineInstr &UseMI,
963
969
Dep.UseOp );
964
970
965
971
// Update Heights[DefMI] to be the maximum height seen.
966
- const auto &[I, Inserted] = Heights.insert ({Dep.DefMI , UseHeight});
967
- if (Inserted)
972
+ MIHeightMap::iterator I;
973
+ bool New;
974
+ std::tie (I, New) = Heights.insert (std::make_pair (Dep.DefMI , UseHeight));
975
+ if (New)
968
976
return true ;
969
977
970
978
// DefMI has been pushed before. Give it the max height.
@@ -1002,15 +1010,15 @@ computeInstrHeights(const MachineBasicBlock *MBB) {
1002
1010
// The bottom of the trace may already be computed.
1003
1011
// Find the blocks that need updating.
1004
1012
SmallVector<const MachineBasicBlock*, 8 > Stack;
1005
- while (MBB) {
1013
+ do {
1006
1014
TraceBlockInfo &TBI = BlockInfo[MBB->getNumber ()];
1007
1015
assert (TBI.hasValidHeight () && " Incomplete trace" );
1008
1016
if (TBI.HasValidInstrHeights )
1009
1017
break ;
1010
1018
Stack.push_back (MBB);
1011
1019
TBI.LiveIns .clear ();
1012
1020
MBB = TBI.Succ ;
1013
- }
1021
+ } while (MBB);
1014
1022
1015
1023
// As we move upwards in the trace, keep track of instructions that are
1016
1024
// required by deeper trace instructions. Map MI -> height required so far.
@@ -1052,7 +1060,7 @@ computeInstrHeights(const MachineBasicBlock *MBB) {
1052
1060
LLVM_DEBUG ({
1053
1061
dbgs () << format (" %7u Instructions\n " , TBI.InstrHeight );
1054
1062
ArrayRef<unsigned > PRHeights = getProcResourceHeights (MBB->getNumber ());
1055
- for (unsigned K = 0 ; K < PRHeights.size (); ++K)
1063
+ for (unsigned K = 0 ; K != PRHeights.size (); ++K)
1056
1064
if (PRHeights[K]) {
1057
1065
unsigned Factor = MTM.SchedModel .getResourceFactor (K);
1058
1066
dbgs () << format (" %6uc @ " , MTM.getCycles (PRHeights[K]))
@@ -1137,7 +1145,7 @@ computeInstrHeights(const MachineBasicBlock *MBB) {
1137
1145
1138
1146
// Transfer the live regunits to the live-in list.
1139
1147
for (const LiveRegUnit &RU : RegUnits) {
1140
- TBI.LiveIns .emplace_back ( RU.RegUnit , RU.Cycle );
1148
+ TBI.LiveIns .push_back ( LiveInReg ( RU.RegUnit , RU.Cycle ) );
1141
1149
LLVM_DEBUG (dbgs () << ' ' << printRegUnit (RU.RegUnit , MTM.TRI ) << ' @'
1142
1150
<< RU.Cycle );
1143
1151
}
@@ -1197,7 +1205,7 @@ unsigned MachineTraceMetrics::Trace::getResourceDepth(bool Bottom) const {
1197
1205
ArrayRef<unsigned > PRDepths = TE.getProcResourceDepths (getBlockNum ());
1198
1206
if (Bottom) {
1199
1207
ArrayRef<unsigned > PRCycles = TE.MTM .getProcReleaseAtCycles (getBlockNum ());
1200
- for (unsigned K = 0 ; K < PRDepths.size (); ++K)
1208
+ for (unsigned K = 0 ; K != PRDepths.size (); ++K)
1201
1209
PRMax = std::max (PRMax, PRDepths[K] + PRCycles[K]);
1202
1210
} else {
1203
1211
for (unsigned PRD : PRDepths)
@@ -1227,8 +1235,9 @@ unsigned MachineTraceMetrics::Trace::getResourceLength(
1227
1235
unsigned PRMax = 0 ;
1228
1236
1229
1237
// Capture computing cycles from extra instructions
1230
- auto ExtraCycles = [this ](ArrayRef<const MCSchedClassDesc *> Instrs,
1231
- unsigned ResourceIdx) -> unsigned {
1238
+ auto extraCycles = [this ](ArrayRef<const MCSchedClassDesc *> Instrs,
1239
+ unsigned ResourceIdx)
1240
+ ->unsigned {
1232
1241
unsigned Cycles = 0 ;
1233
1242
for (const MCSchedClassDesc *SC : Instrs) {
1234
1243
if (!SC->isValid ())
@@ -1246,12 +1255,12 @@ unsigned MachineTraceMetrics::Trace::getResourceLength(
1246
1255
return Cycles;
1247
1256
};
1248
1257
1249
- for (unsigned K = 0 ; K < PRDepths.size (); ++K) {
1258
+ for (unsigned K = 0 ; K != PRDepths.size (); ++K) {
1250
1259
unsigned PRCycles = PRDepths[K] + PRHeights[K];
1251
1260
for (const MachineBasicBlock *MBB : Extrablocks)
1252
1261
PRCycles += TE.MTM .getProcReleaseAtCycles (MBB->getNumber ())[K];
1253
- PRCycles += ExtraCycles (ExtraInstrs, K);
1254
- PRCycles -= ExtraCycles (RemoveInstrs, K);
1262
+ PRCycles += extraCycles (ExtraInstrs, K);
1263
+ PRCycles -= extraCycles (RemoveInstrs, K);
1255
1264
PRMax = std::max (PRMax, PRCycles);
1256
1265
}
1257
1266
// Convert to cycle count.
@@ -1283,9 +1292,9 @@ bool MachineTraceMetrics::Trace::isDepInTrace(const MachineInstr &DefMI,
1283
1292
1284
1293
void MachineTraceMetrics::Ensemble::print (raw_ostream &OS) const {
1285
1294
OS << getName () << " ensemble:\n " ;
1286
- for (unsigned Idx = 0 ; Idx < BlockInfo.size (); ++Idx ) {
1287
- OS << " %bb." << Idx << ' \t ' ;
1288
- BlockInfo[Idx ].print (OS);
1295
+ for (unsigned i = 0 , e = BlockInfo.size (); i != e; ++i ) {
1296
+ OS << " %bb." << i << ' \t ' ;
1297
+ BlockInfo[i ].print (OS);
1289
1298
OS << ' \n ' ;
1290
1299
}
1291
1300
}
0 commit comments