blob: 14798120df085835537cb2a2c319e437d23e08cf [file] [log] [blame]
Andrew Trick87255e32012-07-07 04:00:00 +00001//===- CodeGenSchedule.h - Scheduling Machine Models ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Alp Tokercb402912014-01-24 17:20:08 +000010// This file defines structures to encapsulate the machine model as described in
Andrew Trick87255e32012-07-07 04:00:00 +000011// the target description.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_UTILS_TABLEGEN_CODEGENSCHEDULE_H
16#define LLVM_UTILS_TABLEGEN_CODEGENSCHEDULE_H
Andrew Trick87255e32012-07-07 04:00:00 +000017
Andrew Trick87255e32012-07-07 04:00:00 +000018#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/StringMap.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000020#include "llvm/Support/ErrorHandling.h"
21#include "llvm/TableGen/Record.h"
James Molloyf1653b52014-06-17 13:10:38 +000022#include "llvm/TableGen/SetTheory.h"
Andrew Trick87255e32012-07-07 04:00:00 +000023
24namespace llvm {
25
26class CodeGenTarget;
Andrew Trick76686492012-09-15 00:19:57 +000027class CodeGenSchedModels;
28class CodeGenInstruction;
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +000029class CodeGenRegisterClass;
Andrew Trick87255e32012-07-07 04:00:00 +000030
Javed Absar67b042c2017-09-13 10:31:10 +000031using RecVec = std::vector<Record*>;
32using RecIter = std::vector<Record*>::const_iterator;
Andrew Trick76686492012-09-15 00:19:57 +000033
Javed Absar67b042c2017-09-13 10:31:10 +000034using IdxVec = std::vector<unsigned>;
35using IdxIter = std::vector<unsigned>::const_iterator;
Andrew Trick76686492012-09-15 00:19:57 +000036
Andrew Trick76686492012-09-15 00:19:57 +000037/// We have two kinds of SchedReadWrites. Explicitly defined and inferred
38/// sequences. TheDef is nonnull for explicit SchedWrites, but Sequence may or
39/// may not be empty. TheDef is null for inferred sequences, and Sequence must
40/// be nonempty.
41///
42/// IsVariadic controls whether the variants are expanded into multiple operands
43/// or a sequence of writes on one operand.
44struct CodeGenSchedRW {
Andrew Trickda984b12012-10-03 23:06:28 +000045 unsigned Index;
Andrew Trick76686492012-09-15 00:19:57 +000046 std::string Name;
47 Record *TheDef;
Andrew Trickda984b12012-10-03 23:06:28 +000048 bool IsRead;
Andrew Trick9257b8f2012-09-22 02:24:21 +000049 bool IsAlias;
Andrew Trick76686492012-09-15 00:19:57 +000050 bool HasVariants;
51 bool IsVariadic;
52 bool IsSequence;
53 IdxVec Sequence;
Andrew Trick9257b8f2012-09-22 02:24:21 +000054 RecVec Aliases;
Andrew Trick76686492012-09-15 00:19:57 +000055
Richard Smitha7bb16a2012-12-20 01:05:39 +000056 CodeGenSchedRW()
Craig Topperada08572014-04-16 04:21:27 +000057 : Index(0), TheDef(nullptr), IsRead(false), IsAlias(false),
Richard Smitha7bb16a2012-12-20 01:05:39 +000058 HasVariants(false), IsVariadic(false), IsSequence(false) {}
59 CodeGenSchedRW(unsigned Idx, Record *Def)
60 : Index(Idx), TheDef(Def), IsAlias(false), IsVariadic(false) {
Andrew Trick76686492012-09-15 00:19:57 +000061 Name = Def->getName();
Andrew Trickda984b12012-10-03 23:06:28 +000062 IsRead = Def->isSubClassOf("SchedRead");
Andrew Trick76686492012-09-15 00:19:57 +000063 HasVariants = Def->isSubClassOf("SchedVariant");
64 if (HasVariants)
65 IsVariadic = Def->getValueAsBit("Variadic");
66
67 // Read records don't currently have sequences, but it can be easily
68 // added. Note that implicit Reads (from ReadVariant) may have a Sequence
69 // (but no record).
70 IsSequence = Def->isSubClassOf("WriteSequence");
71 }
72
Benjamin Kramere1761952015-10-24 12:46:49 +000073 CodeGenSchedRW(unsigned Idx, bool Read, ArrayRef<unsigned> Seq,
Richard Smitha7bb16a2012-12-20 01:05:39 +000074 const std::string &Name)
Benjamin Kramere1761952015-10-24 12:46:49 +000075 : Index(Idx), Name(Name), TheDef(nullptr), IsRead(Read), IsAlias(false),
76 HasVariants(false), IsVariadic(false), IsSequence(true), Sequence(Seq) {
Andrew Trick76686492012-09-15 00:19:57 +000077 assert(Sequence.size() > 1 && "implied sequence needs >1 RWs");
78 }
79
80 bool isValid() const {
81 assert((!HasVariants || TheDef) && "Variant write needs record def");
82 assert((!IsVariadic || HasVariants) && "Variadic write needs variants");
83 assert((!IsSequence || !HasVariants) && "Sequence can't have variant");
84 assert((!IsSequence || !Sequence.empty()) && "Sequence should be nonempty");
Andrew Trick9257b8f2012-09-22 02:24:21 +000085 assert((!IsAlias || Aliases.empty()) && "Alias cannot have aliases");
Andrew Trick76686492012-09-15 00:19:57 +000086 return TheDef || !Sequence.empty();
87 }
88
89#ifndef NDEBUG
90 void dump() const;
91#endif
92};
93
Andrew Trickea28dbd2012-09-18 04:03:30 +000094/// Represent a transition between SchedClasses induced by SchedVariant.
Andrew Trick33401e82012-09-15 00:19:59 +000095struct CodeGenSchedTransition {
96 unsigned ToClassIdx;
97 IdxVec ProcIndices;
98 RecVec PredTerm;
99};
100
Andrew Trick76686492012-09-15 00:19:57 +0000101/// Scheduling class.
102///
103/// Each instruction description will be mapped to a scheduling class. There are
104/// four types of classes:
105///
106/// 1) An explicitly defined itinerary class with ItinClassDef set.
107/// Writes and ReadDefs are empty. ProcIndices contains 0 for any processor.
108///
109/// 2) An implied class with a list of SchedWrites and SchedReads that are
110/// defined in an instruction definition and which are common across all
111/// subtargets. ProcIndices contains 0 for any processor.
112///
113/// 3) An implied class with a list of InstRW records that map instructions to
114/// SchedWrites and SchedReads per-processor. InstrClassMap should map the same
115/// instructions to this class. ProcIndices contains all the processors that
116/// provided InstrRW records for this class. ItinClassDef or Writes/Reads may
117/// still be defined for processors with no InstRW entry.
118///
119/// 4) An inferred class represents a variant of another class that may be
120/// resolved at runtime. ProcIndices contains the set of processors that may
121/// require the class. ProcIndices are propagated through SchedClasses as
122/// variants are expanded. Multiple SchedClasses may be inferred from an
123/// itinerary class. Each inherits the processor index from the ItinRW record
124/// that mapped the itinerary class to the variant Writes or Reads.
Andrew Trick87255e32012-07-07 04:00:00 +0000125struct CodeGenSchedClass {
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000126 unsigned Index;
Andrew Trick87255e32012-07-07 04:00:00 +0000127 std::string Name;
Andrew Trick87255e32012-07-07 04:00:00 +0000128 Record *ItinClassDef;
129
Andrew Trick76686492012-09-15 00:19:57 +0000130 IdxVec Writes;
131 IdxVec Reads;
132 // Sorted list of ProcIdx, where ProcIdx==0 implies any processor.
133 IdxVec ProcIndices;
134
Andrew Trick33401e82012-09-15 00:19:59 +0000135 std::vector<CodeGenSchedTransition> Transitions;
136
Andrew Trick9257b8f2012-09-22 02:24:21 +0000137 // InstRW records associated with this class. These records may refer to an
138 // Instruction no longer mapped to this class by InstrClassMap. These
139 // Instructions should be ignored by this class because they have been split
140 // off to join another inferred class.
Andrew Trick76686492012-09-15 00:19:57 +0000141 RecVec InstRWs;
142
Craig Topper281a19c2018-03-22 06:15:08 +0000143 CodeGenSchedClass(unsigned Index, std::string Name, Record *ItinClassDef)
144 : Index(Index), Name(std::move(Name)), ItinClassDef(ItinClassDef) {}
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000145
Simon Pilgrim4cca3b12018-03-21 17:57:21 +0000146 bool isKeyEqual(Record *IC, ArrayRef<unsigned> W,
147 ArrayRef<unsigned> R) const {
Benjamin Kramere1761952015-10-24 12:46:49 +0000148 return ItinClassDef == IC && makeArrayRef(Writes) == W &&
149 makeArrayRef(Reads) == R;
Andrew Trick87255e32012-07-07 04:00:00 +0000150 }
Andrew Trick76686492012-09-15 00:19:57 +0000151
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000152 // Is this class generated from a variants if existing classes? Instructions
153 // are never mapped directly to inferred scheduling classes.
154 bool isInferred() const { return !ItinClassDef; }
155
Andrew Trick76686492012-09-15 00:19:57 +0000156#ifndef NDEBUG
157 void dump(const CodeGenSchedModels *SchedModels) const;
158#endif
Andrew Trick87255e32012-07-07 04:00:00 +0000159};
160
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000161/// Represent the cost of allocating a register of register class RCDef.
162///
163/// The cost of allocating a register is equivalent to the number of physical
164/// registers used by the register renamer. Register costs are defined at
165/// register class granularity.
166struct CodeGenRegisterCost {
167 Record *RCDef;
168 unsigned Cost;
169 CodeGenRegisterCost(Record *RC, unsigned RegisterCost)
170 : RCDef(RC), Cost(RegisterCost) {}
171 CodeGenRegisterCost(const CodeGenRegisterCost &) = default;
172 CodeGenRegisterCost &operator=(const CodeGenRegisterCost &) = delete;
173};
174
175/// A processor register file.
176///
177/// This class describes a processor register file. Register file information is
178/// currently consumed by external tools like llvm-mca to predict dispatch
179/// stalls due to register pressure.
180struct CodeGenRegisterFile {
181 std::string Name;
182 Record *RegisterFileDef;
183
184 unsigned NumPhysRegs;
185 std::vector<CodeGenRegisterCost> Costs;
186
187 CodeGenRegisterFile(StringRef name, Record *def)
188 : Name(name), RegisterFileDef(def), NumPhysRegs(0) {}
189
190 bool hasDefaultCosts() const { return Costs.empty(); }
191};
192
Andrew Trick87255e32012-07-07 04:00:00 +0000193// Processor model.
194//
195// ModelName is a unique name used to name an instantiation of MCSchedModel.
196//
197// ModelDef is NULL for inferred Models. This happens when a processor defines
Alp Tokercb402912014-01-24 17:20:08 +0000198// an itinerary but no machine model. If the processor defines neither a machine
Andrew Trick87255e32012-07-07 04:00:00 +0000199// model nor itinerary, then ModelDef remains pointing to NoModel. NoModel has
200// the special "NoModel" field set to true.
201//
202// ItinsDef always points to a valid record definition, but may point to the
203// default NoItineraries. NoItineraries has an empty list of InstrItinData
204// records.
205//
206// ItinDefList orders this processor's InstrItinData records by SchedClass idx.
207struct CodeGenProcModel {
Andrew Trick76686492012-09-15 00:19:57 +0000208 unsigned Index;
Andrew Trick87255e32012-07-07 04:00:00 +0000209 std::string ModelName;
210 Record *ModelDef;
211 Record *ItinsDef;
212
Andrew Trick76686492012-09-15 00:19:57 +0000213 // Derived members...
Andrew Trick87255e32012-07-07 04:00:00 +0000214
Andrew Trick76686492012-09-15 00:19:57 +0000215 // Array of InstrItinData records indexed by a CodeGenSchedClass index.
216 // This list is empty if the Processor has no value for Itineraries.
217 // Initialized by collectProcItins().
218 RecVec ItinDefList;
219
220 // Map itinerary classes to per-operand resources.
221 // This list is empty if no ItinRW refers to this Processor.
222 RecVec ItinRWDefs;
223
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000224 // List of unsupported feature.
225 // This list is empty if the Processor has no UnsupportedFeatures.
226 RecVec UnsupportedFeaturesDefs;
227
Andrew Trick1e46d482012-09-15 00:20:02 +0000228 // All read/write resources associated with this processor.
229 RecVec WriteResDefs;
230 RecVec ReadAdvanceDefs;
231
232 // Per-operand machine model resources associated with this processor.
233 RecVec ProcResourceDefs;
234
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000235 // List of Register Files.
236 std::vector<CodeGenRegisterFile> RegisterFiles;
237
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000238 // Optional Retire Control Unit definition.
239 Record *RetireControlUnit;
240
Clement Courbetb4493792018-04-10 08:16:37 +0000241 // List of PfmCounters.
242 RecVec PfmIssueCounterDefs;
243 Record *PfmCycleCounterDef = nullptr;
244
Craig Topper281a19c2018-03-22 06:15:08 +0000245 CodeGenProcModel(unsigned Idx, std::string Name, Record *MDef,
Andrew Trick76686492012-09-15 00:19:57 +0000246 Record *IDef) :
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000247 Index(Idx), ModelName(std::move(Name)), ModelDef(MDef), ItinsDef(IDef),
248 RetireControlUnit(nullptr) {}
Andrew Trick76686492012-09-15 00:19:57 +0000249
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000250 bool hasItineraries() const {
251 return !ItinsDef->getValueAsListOfDefs("IID").empty();
252 }
253
Andrew Trick1e46d482012-09-15 00:20:02 +0000254 bool hasInstrSchedModel() const {
255 return !WriteResDefs.empty() || !ItinRWDefs.empty();
256 }
257
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000258 bool hasExtraProcessorInfo() const {
Clement Courbetb4493792018-04-10 08:16:37 +0000259 return RetireControlUnit || !RegisterFiles.empty() ||
260 !PfmIssueCounterDefs.empty() ||
261 PfmCycleCounterDef != nullptr;
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000262 }
263
Andrew Trick1e46d482012-09-15 00:20:02 +0000264 unsigned getProcResourceIdx(Record *PRDef) const;
265
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000266 bool isUnsupported(const CodeGenInstruction &Inst) const;
267
Andrew Trick76686492012-09-15 00:19:57 +0000268#ifndef NDEBUG
269 void dump() const;
270#endif
Andrew Trick87255e32012-07-07 04:00:00 +0000271};
272
Andrew Trick76686492012-09-15 00:19:57 +0000273/// Top level container for machine model data.
Andrew Trick87255e32012-07-07 04:00:00 +0000274class CodeGenSchedModels {
275 RecordKeeper &Records;
276 const CodeGenTarget &Target;
277
Andrew Trick9e1deb62012-10-03 23:06:32 +0000278 // Map dag expressions to Instruction lists.
279 SetTheory Sets;
280
Andrew Trick76686492012-09-15 00:19:57 +0000281 // List of unique processor models.
282 std::vector<CodeGenProcModel> ProcModels;
283
284 // Map Processor's MachineModel or ProcItin to a CodeGenProcModel index.
Javed Absar67b042c2017-09-13 10:31:10 +0000285 using ProcModelMapTy = DenseMap<Record*, unsigned>;
Andrew Trick76686492012-09-15 00:19:57 +0000286 ProcModelMapTy ProcModelMap;
287
288 // Per-operand SchedReadWrite types.
289 std::vector<CodeGenSchedRW> SchedWrites;
290 std::vector<CodeGenSchedRW> SchedReads;
291
Andrew Trick87255e32012-07-07 04:00:00 +0000292 // List of unique SchedClasses.
293 std::vector<CodeGenSchedClass> SchedClasses;
294
Andrew Trick76686492012-09-15 00:19:57 +0000295 // Any inferred SchedClass has an index greater than NumInstrSchedClassses.
296 unsigned NumInstrSchedClasses;
Andrew Trick87255e32012-07-07 04:00:00 +0000297
Matthias Braun6b1fd9a2016-06-21 03:24:03 +0000298 RecVec ProcResourceDefs;
299 RecVec ProcResGroups;
300
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000301 // Map each instruction to its unique SchedClass index considering the
302 // combination of it's itinerary class, SchedRW list, and InstRW records.
Javed Absar67b042c2017-09-13 10:31:10 +0000303 using InstClassMapTy = DenseMap<Record*, unsigned>;
Andrew Trick76686492012-09-15 00:19:57 +0000304 InstClassMapTy InstrClassMap;
Andrew Trick87255e32012-07-07 04:00:00 +0000305
306public:
307 CodeGenSchedModels(RecordKeeper& RK, const CodeGenTarget &TGT);
308
Jim Grosbachaf814452014-04-18 02:09:04 +0000309 // iterator access to the scheduling classes.
Javed Absar67b042c2017-09-13 10:31:10 +0000310 using class_iterator = std::vector<CodeGenSchedClass>::iterator;
311 using const_class_iterator = std::vector<CodeGenSchedClass>::const_iterator;
Jim Grosbachaf814452014-04-18 02:09:04 +0000312 class_iterator classes_begin() { return SchedClasses.begin(); }
313 const_class_iterator classes_begin() const { return SchedClasses.begin(); }
314 class_iterator classes_end() { return SchedClasses.end(); }
315 const_class_iterator classes_end() const { return SchedClasses.end(); }
316 iterator_range<class_iterator> classes() {
Craig Topper15576e12015-12-06 05:08:07 +0000317 return make_range(classes_begin(), classes_end());
Jim Grosbachaf814452014-04-18 02:09:04 +0000318 }
319 iterator_range<const_class_iterator> classes() const {
Craig Topper15576e12015-12-06 05:08:07 +0000320 return make_range(classes_begin(), classes_end());
Jim Grosbachaf814452014-04-18 02:09:04 +0000321 }
322 iterator_range<class_iterator> explicit_classes() {
Craig Topper15576e12015-12-06 05:08:07 +0000323 return make_range(classes_begin(), classes_begin() + NumInstrSchedClasses);
Jim Grosbachaf814452014-04-18 02:09:04 +0000324 }
325 iterator_range<const_class_iterator> explicit_classes() const {
Craig Topper15576e12015-12-06 05:08:07 +0000326 return make_range(classes_begin(), classes_begin() + NumInstrSchedClasses);
Jim Grosbachaf814452014-04-18 02:09:04 +0000327 }
328
Andrew Trick76686492012-09-15 00:19:57 +0000329 Record *getModelOrItinDef(Record *ProcDef) const {
330 Record *ModelDef = ProcDef->getValueAsDef("SchedModel");
331 Record *ItinsDef = ProcDef->getValueAsDef("ProcItin");
332 if (!ItinsDef->getValueAsListOfDefs("IID").empty()) {
333 assert(ModelDef->getValueAsBit("NoModel")
334 && "Itineraries must be defined within SchedMachineModel");
335 return ItinsDef;
336 }
337 return ModelDef;
338 }
339
340 const CodeGenProcModel &getModelForProc(Record *ProcDef) const {
341 Record *ModelDef = getModelOrItinDef(ProcDef);
342 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
343 assert(I != ProcModelMap.end() && "missing machine model");
344 return ProcModels[I->second];
345 }
346
Andrew Trick40c4f382013-06-15 04:50:06 +0000347 CodeGenProcModel &getProcModel(Record *ModelDef) {
Andrew Trick76686492012-09-15 00:19:57 +0000348 ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
349 assert(I != ProcModelMap.end() && "missing machine model");
350 return ProcModels[I->second];
351 }
Andrew Trick40c4f382013-06-15 04:50:06 +0000352 const CodeGenProcModel &getProcModel(Record *ModelDef) const {
353 return const_cast<CodeGenSchedModels*>(this)->getProcModel(ModelDef);
354 }
Andrew Trick76686492012-09-15 00:19:57 +0000355
356 // Iterate over the unique processor models.
Javed Absar67b042c2017-09-13 10:31:10 +0000357 using ProcIter = std::vector<CodeGenProcModel>::const_iterator;
Andrew Trick76686492012-09-15 00:19:57 +0000358 ProcIter procModelBegin() const { return ProcModels.begin(); }
359 ProcIter procModelEnd() const { return ProcModels.end(); }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000360 ArrayRef<CodeGenProcModel> procModels() const { return ProcModels; }
Andrew Trick76686492012-09-15 00:19:57 +0000361
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000362 // Return true if any processors have itineraries.
363 bool hasItineraries() const;
364
Andrew Trick76686492012-09-15 00:19:57 +0000365 // Get a SchedWrite from its index.
366 const CodeGenSchedRW &getSchedWrite(unsigned Idx) const {
367 assert(Idx < SchedWrites.size() && "bad SchedWrite index");
368 assert(SchedWrites[Idx].isValid() && "invalid SchedWrite");
369 return SchedWrites[Idx];
370 }
371 // Get a SchedWrite from its index.
372 const CodeGenSchedRW &getSchedRead(unsigned Idx) const {
373 assert(Idx < SchedReads.size() && "bad SchedRead index");
374 assert(SchedReads[Idx].isValid() && "invalid SchedRead");
375 return SchedReads[Idx];
376 }
377
378 const CodeGenSchedRW &getSchedRW(unsigned Idx, bool IsRead) const {
379 return IsRead ? getSchedRead(Idx) : getSchedWrite(Idx);
380 }
Andrew Trickda984b12012-10-03 23:06:28 +0000381 CodeGenSchedRW &getSchedRW(Record *Def) {
Andrew Trick9257b8f2012-09-22 02:24:21 +0000382 bool IsRead = Def->isSubClassOf("SchedRead");
Andrew Trickda984b12012-10-03 23:06:28 +0000383 unsigned Idx = getSchedRWIdx(Def, IsRead);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000384 return const_cast<CodeGenSchedRW&>(
385 IsRead ? getSchedRead(Idx) : getSchedWrite(Idx));
386 }
Andrew Trickda984b12012-10-03 23:06:28 +0000387 const CodeGenSchedRW &getSchedRW(Record*Def) const {
388 return const_cast<CodeGenSchedModels&>(*this).getSchedRW(Def);
Andrew Trick9257b8f2012-09-22 02:24:21 +0000389 }
Andrew Trick76686492012-09-15 00:19:57 +0000390
Craig Toppere2611842018-03-21 05:13:04 +0000391 unsigned getSchedRWIdx(Record *Def, bool IsRead) const;
Andrew Trick76686492012-09-15 00:19:57 +0000392
Andrew Trickcfe222c2012-09-19 04:43:19 +0000393 // Return true if the given write record is referenced by a ReadAdvance.
394 bool hasReadOfWrite(Record *WriteDef) const;
395
Andrew Trick87255e32012-07-07 04:00:00 +0000396 // Get a SchedClass from its index.
Andrew Trick76686492012-09-15 00:19:57 +0000397 CodeGenSchedClass &getSchedClass(unsigned Idx) {
398 assert(Idx < SchedClasses.size() && "bad SchedClass index");
399 return SchedClasses[Idx];
400 }
401 const CodeGenSchedClass &getSchedClass(unsigned Idx) const {
Andrew Trick87255e32012-07-07 04:00:00 +0000402 assert(Idx < SchedClasses.size() && "bad SchedClass index");
403 return SchedClasses[Idx];
404 }
405
Andrew Trick76686492012-09-15 00:19:57 +0000406 // Get the SchedClass index for an instruction. Instructions with no
407 // itinerary, no SchedReadWrites, and no InstrReadWrites references return 0
408 // for NoItinerary.
409 unsigned getSchedClassIdx(const CodeGenInstruction &Inst) const;
410
Javed Absar67b042c2017-09-13 10:31:10 +0000411 using SchedClassIter = std::vector<CodeGenSchedClass>::const_iterator;
Andrew Trick76686492012-09-15 00:19:57 +0000412 SchedClassIter schedClassBegin() const { return SchedClasses.begin(); }
413 SchedClassIter schedClassEnd() const { return SchedClasses.end(); }
Craig Topper29c55dcb2016-02-13 06:03:32 +0000414 ArrayRef<CodeGenSchedClass> schedClasses() const { return SchedClasses; }
Andrew Trick87255e32012-07-07 04:00:00 +0000415
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000416 unsigned numInstrSchedClasses() const { return NumInstrSchedClasses; }
417
Andrew Trick76686492012-09-15 00:19:57 +0000418 void findRWs(const RecVec &RWDefs, IdxVec &Writes, IdxVec &Reads) const;
419 void findRWs(const RecVec &RWDefs, IdxVec &RWs, bool IsRead) const;
Andrew Trick33401e82012-09-15 00:19:59 +0000420 void expandRWSequence(unsigned RWIdx, IdxVec &RWSeq, bool IsRead) const;
Andrew Trickda984b12012-10-03 23:06:28 +0000421 void expandRWSeqForProc(unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
422 const CodeGenProcModel &ProcModel) const;
Andrew Trick76686492012-09-15 00:19:57 +0000423
Benjamin Kramere1761952015-10-24 12:46:49 +0000424 unsigned addSchedClass(Record *ItinDef, ArrayRef<unsigned> OperWrites,
425 ArrayRef<unsigned> OperReads,
426 ArrayRef<unsigned> ProcIndices);
Andrew Trick76686492012-09-15 00:19:57 +0000427
428 unsigned findOrInsertRW(ArrayRef<unsigned> Seq, bool IsRead);
429
Benjamin Kramere1761952015-10-24 12:46:49 +0000430 unsigned findSchedClassIdx(Record *ItinClassDef, ArrayRef<unsigned> Writes,
431 ArrayRef<unsigned> Reads) const;
Andrew Trick87255e32012-07-07 04:00:00 +0000432
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000433 Record *findProcResUnits(Record *ProcResKind, const CodeGenProcModel &PM,
434 ArrayRef<SMLoc> Loc) const;
Andrew Trick1e46d482012-09-15 00:20:02 +0000435
Andrew Trick87255e32012-07-07 04:00:00 +0000436private:
Andrew Trick76686492012-09-15 00:19:57 +0000437 void collectProcModels();
Andrew Trick87255e32012-07-07 04:00:00 +0000438
439 // Initialize a new processor model if it is unique.
440 void addProcModel(Record *ProcDef);
441
Andrew Trick76686492012-09-15 00:19:57 +0000442 void collectSchedRW();
443
Benjamin Kramere1761952015-10-24 12:46:49 +0000444 std::string genRWName(ArrayRef<unsigned> Seq, bool IsRead);
445 unsigned findRWForSequence(ArrayRef<unsigned> Seq, bool IsRead);
Andrew Trick76686492012-09-15 00:19:57 +0000446
447 void collectSchedClasses();
448
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000449 void collectRetireControlUnits();
450
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000451 void collectRegisterFiles();
452
Clement Courbetb4493792018-04-10 08:16:37 +0000453 void collectPfmCounters();
454
Andrea Di Biagioc74ad502018-04-05 15:41:41 +0000455 void collectOptionalProcessorInfo();
456
Andrew Trickbf8a28d2013-03-16 18:58:55 +0000457 std::string createSchedClassName(Record *ItinClassDef,
Benjamin Kramere1761952015-10-24 12:46:49 +0000458 ArrayRef<unsigned> OperWrites,
459 ArrayRef<unsigned> OperReads);
Andrew Trick76686492012-09-15 00:19:57 +0000460 std::string createSchedClassName(const RecVec &InstDefs);
461 void createInstRWClass(Record *InstRWDef);
462
463 void collectProcItins();
464
465 void collectProcItinRW();
Andrew Trick33401e82012-09-15 00:19:59 +0000466
Simon Dardis5f95c9a2016-06-24 08:43:27 +0000467 void collectProcUnsupportedFeatures();
468
Andrew Trick33401e82012-09-15 00:19:59 +0000469 void inferSchedClasses();
470
Matthias Braun17cb5792016-03-01 20:03:21 +0000471 void checkCompleteness();
472
Benjamin Kramere1761952015-10-24 12:46:49 +0000473 void inferFromRW(ArrayRef<unsigned> OperWrites, ArrayRef<unsigned> OperReads,
474 unsigned FromClassIdx, ArrayRef<unsigned> ProcIndices);
Andrew Trick33401e82012-09-15 00:19:59 +0000475 void inferFromItinClass(Record *ItinClassDef, unsigned FromClassIdx);
476 void inferFromInstRWs(unsigned SCIdx);
Andrew Trick1e46d482012-09-15 00:20:02 +0000477
Andrew Trickcf398b22013-04-23 23:45:14 +0000478 bool hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM);
479 void verifyProcResourceGroups(CodeGenProcModel &PM);
480
Andrew Trick1e46d482012-09-15 00:20:02 +0000481 void collectProcResources();
482
483 void collectItinProcResources(Record *ItinClassDef);
484
Andrew Trickd0b9c442012-10-10 05:43:13 +0000485 void collectRWResources(unsigned RWIdx, bool IsRead,
Benjamin Kramere1761952015-10-24 12:46:49 +0000486 ArrayRef<unsigned> ProcIndices);
Andrew Trickd0b9c442012-10-10 05:43:13 +0000487
Benjamin Kramere1761952015-10-24 12:46:49 +0000488 void collectRWResources(ArrayRef<unsigned> Writes, ArrayRef<unsigned> Reads,
489 ArrayRef<unsigned> ProcIndices);
Andrew Trick1e46d482012-09-15 00:20:02 +0000490
Evandro Menezes9dc54e22017-11-21 21:33:52 +0000491 void addProcResource(Record *ProcResourceKind, CodeGenProcModel &PM,
492 ArrayRef<SMLoc> Loc);
Andrew Trick1e46d482012-09-15 00:20:02 +0000493
494 void addWriteRes(Record *ProcWriteResDef, unsigned PIdx);
495
496 void addReadAdvance(Record *ProcReadAdvanceDef, unsigned PIdx);
Andrew Trick87255e32012-07-07 04:00:00 +0000497};
498
499} // namespace llvm
500
501#endif