[RISCV][VLOPT] Add vector narrowing integer right shift instructions to isSupportedIn...
[llvm-project.git] / llvm / utils / TableGen / SubtargetEmitter.cpp
blob9bd18f5837eea100bba12ab16e9cbf7bb017f950
1 //===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This tablegen backend emits subtarget enumerations.
11 //===----------------------------------------------------------------------===//
13 #include "Common/CodeGenHwModes.h"
14 #include "Common/CodeGenSchedule.h"
15 #include "Common/CodeGenTarget.h"
16 #include "Common/PredicateExpander.h"
17 #include "Common/Utils.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/MC/MCInstrItineraries.h"
26 #include "llvm/MC/MCSchedule.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/Format.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/TableGen/Error.h"
31 #include "llvm/TableGen/Record.h"
32 #include "llvm/TableGen/TableGenBackend.h"
33 #include "llvm/TargetParser/SubtargetFeature.h"
34 #include <algorithm>
35 #include <cassert>
36 #include <cstdint>
37 #include <iterator>
38 #include <string>
39 #include <vector>
41 using namespace llvm;
43 #define DEBUG_TYPE "subtarget-emitter"
45 namespace {
47 using FeatureMapTy = DenseMap<const Record *, unsigned>;
49 /// Sorting predicate to sort record pointers by their
50 /// FieldName field.
51 struct LessRecordFieldFieldName {
52 bool operator()(const Record *Rec1, const Record *Rec2) const {
53 return Rec1->getValueAsString("FieldName") <
54 Rec2->getValueAsString("FieldName");
58 class SubtargetEmitter {
59 // Each processor has a SchedClassDesc table with an entry for each
60 // SchedClass. The SchedClassDesc table indexes into a global write resource
61 // table, write latency table, and read advance table.
62 struct SchedClassTables {
63 std::vector<std::vector<MCSchedClassDesc>> ProcSchedClasses;
64 std::vector<MCWriteProcResEntry> WriteProcResources;
65 std::vector<MCWriteLatencyEntry> WriteLatencies;
66 std::vector<std::string> WriterNames;
67 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
69 // Reserve an invalid entry at index 0
70 SchedClassTables() {
71 ProcSchedClasses.resize(1);
72 WriteProcResources.resize(1);
73 WriteLatencies.resize(1);
74 WriterNames.push_back("InvalidWrite");
75 ReadAdvanceEntries.resize(1);
79 struct LessWriteProcResources {
80 bool operator()(const MCWriteProcResEntry &LHS,
81 const MCWriteProcResEntry &RHS) {
82 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
86 CodeGenTarget TGT;
87 const RecordKeeper &Records;
88 CodeGenSchedModels &SchedModels;
89 std::string Target;
91 FeatureMapTy enumeration(raw_ostream &OS);
92 void emitSubtargetInfoMacroCalls(raw_ostream &OS);
93 unsigned featureKeyValues(raw_ostream &OS, const FeatureMapTy &FeatureMap);
94 unsigned cpuKeyValues(raw_ostream &OS, const FeatureMapTy &FeatureMap);
95 unsigned cpuNames(raw_ostream &OS);
96 void formItineraryStageString(const std::string &Names,
97 const Record *ItinData, std::string &ItinString,
98 unsigned &NStages);
99 void formItineraryOperandCycleString(const Record *ItinData,
100 std::string &ItinString,
101 unsigned &NOperandCycles);
102 void formItineraryBypassString(const std::string &Names,
103 const Record *ItinData,
104 std::string &ItinString,
105 unsigned NOperandCycles);
106 void emitStageAndOperandCycleData(
107 raw_ostream &OS, std::vector<std::vector<InstrItinerary>> &ProcItinLists);
108 void emitItineraries(raw_ostream &OS,
109 std::vector<std::vector<InstrItinerary>> &ProcItinLists);
110 unsigned emitRegisterFileTables(const CodeGenProcModel &ProcModel,
111 raw_ostream &OS);
112 void emitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
113 raw_ostream &OS);
114 void emitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
115 raw_ostream &OS);
116 void emitProcessorProp(raw_ostream &OS, const Record *R, StringRef Name,
117 char Separator);
118 void emitProcessorResourceSubUnits(const CodeGenProcModel &ProcModel,
119 raw_ostream &OS);
120 void emitProcessorResources(const CodeGenProcModel &ProcModel,
121 raw_ostream &OS);
122 const Record *findWriteResources(const CodeGenSchedRW &SchedWrite,
123 const CodeGenProcModel &ProcModel);
124 const Record *findReadAdvance(const CodeGenSchedRW &SchedRead,
125 const CodeGenProcModel &ProcModel);
126 void expandProcResources(ConstRecVec &PRVec,
127 std::vector<int64_t> &ReleaseAtCycles,
128 std::vector<int64_t> &AcquireAtCycles,
129 const CodeGenProcModel &ProcModel);
130 void genSchedClassTables(const CodeGenProcModel &ProcModel,
131 SchedClassTables &SchedTables);
132 void emitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
133 void emitProcessorModels(raw_ostream &OS);
134 void emitSchedModelHelpers(const std::string &ClassName, raw_ostream &OS);
135 void emitSchedModelHelpersImpl(raw_ostream &OS,
136 bool OnlyExpandMCInstPredicates = false);
137 void emitGenMCSubtargetInfo(raw_ostream &OS);
138 void emitMcInstrAnalysisPredicateFunctions(raw_ostream &OS);
140 void emitSchedModel(raw_ostream &OS);
141 void emitGetMacroFusions(const std::string &ClassName, raw_ostream &OS);
142 void emitHwModeCheck(const std::string &ClassName, raw_ostream &OS);
143 void parseFeaturesFunction(raw_ostream &OS);
145 public:
146 SubtargetEmitter(const RecordKeeper &R)
147 : TGT(R), Records(R), SchedModels(TGT.getSchedModels()),
148 Target(TGT.getName()) {}
150 void run(raw_ostream &O);
153 } // end anonymous namespace
156 // Enumeration - Emit the specified class as an enumeration.
158 FeatureMapTy SubtargetEmitter::enumeration(raw_ostream &OS) {
159 ArrayRef<const Record *> DefList =
160 Records.getAllDerivedDefinitions("SubtargetFeature");
162 unsigned N = DefList.size();
163 if (N == 0)
164 return FeatureMapTy();
165 if (N + 1 > MAX_SUBTARGET_FEATURES)
166 PrintFatalError(
167 "Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
169 OS << "namespace " << Target << " {\n";
171 // Open enumeration.
172 OS << "enum {\n";
174 FeatureMapTy FeatureMap;
175 // For each record
176 for (unsigned I = 0; I < N; ++I) {
177 // Next record
178 const Record *Def = DefList[I];
180 // Get and emit name
181 OS << " " << Def->getName() << " = " << I << ",\n";
183 // Save the index for this feature.
184 FeatureMap[Def] = I;
187 OS << " "
188 << "NumSubtargetFeatures = " << N << "\n";
190 // Close enumeration and namespace
191 OS << "};\n";
192 OS << "} // end namespace " << Target << "\n";
193 return FeatureMap;
196 static void printFeatureMask(raw_ostream &OS,
197 ArrayRef<const Record *> FeatureList,
198 const FeatureMapTy &FeatureMap) {
199 std::array<uint64_t, MAX_SUBTARGET_WORDS> Mask = {};
200 for (const Record *Feature : FeatureList) {
201 unsigned Bit = FeatureMap.lookup(Feature);
202 Mask[Bit / 64] |= 1ULL << (Bit % 64);
205 OS << "{ { { ";
206 for (unsigned I = 0; I != Mask.size(); ++I) {
207 OS << "0x";
208 OS.write_hex(Mask[I]);
209 OS << "ULL, ";
211 OS << "} } }";
214 /// Emit some information about the SubtargetFeature as calls to a macro so
215 /// that they can be used from C++.
216 void SubtargetEmitter::emitSubtargetInfoMacroCalls(raw_ostream &OS) {
217 OS << "\n#ifdef GET_SUBTARGETINFO_MACRO\n";
219 std::vector<const Record *> FeatureList =
220 Records.getAllDerivedDefinitions("SubtargetFeature");
221 llvm::sort(FeatureList, LessRecordFieldFieldName());
223 for (const Record *Feature : FeatureList) {
224 const StringRef FieldName = Feature->getValueAsString("FieldName");
225 const StringRef Value = Feature->getValueAsString("Value");
227 // Only handle boolean features for now, excluding BitVectors and enums.
228 const bool IsBool = (Value == "false" || Value == "true") &&
229 !StringRef(FieldName).contains('[');
230 if (!IsBool)
231 continue;
233 // Some features default to true, with values set to false if enabled.
234 const char *Default = Value == "false" ? "true" : "false";
236 // Define the getter with lowercased first char: xxxYyy() { return XxxYyy; }
237 const std::string Getter =
238 FieldName.substr(0, 1).lower() + FieldName.substr(1).str();
240 OS << "GET_SUBTARGETINFO_MACRO(" << FieldName << ", " << Default << ", "
241 << Getter << ")\n";
243 OS << "#undef GET_SUBTARGETINFO_MACRO\n";
244 OS << "#endif // GET_SUBTARGETINFO_MACRO\n\n";
246 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
247 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
249 if (Target == "AArch64")
250 OS << "#include \"llvm/TargetParser/AArch64TargetParser.h\"\n\n";
254 // FeatureKeyValues - Emit data of all the subtarget features. Used by the
255 // command line.
257 unsigned SubtargetEmitter::featureKeyValues(raw_ostream &OS,
258 const FeatureMapTy &FeatureMap) {
259 std::vector<const Record *> FeatureList =
260 Records.getAllDerivedDefinitions("SubtargetFeature");
262 // Remove features with empty name.
263 llvm::erase_if(FeatureList, [](const Record *Rec) {
264 return Rec->getValueAsString("Name").empty();
266 if (FeatureList.empty())
267 return 0;
269 // Sort and check duplicate Feature name.
270 sortAndReportDuplicates(FeatureList, "Feature");
272 // Begin feature table.
273 OS << "// Sorted (by key) array of values for CPU features.\n"
274 << "extern const llvm::SubtargetFeatureKV " << Target
275 << "FeatureKV[] = {\n";
277 for (const Record *Feature : FeatureList) {
278 // Next feature
279 StringRef Name = Feature->getName();
280 StringRef CommandLineName = Feature->getValueAsString("Name");
281 StringRef Desc = Feature->getValueAsString("Desc");
283 // Emit as { "feature", "description", { featureEnum }, { i1 , i2 , ... , in
284 // } }
285 OS << " { "
286 << "\"" << CommandLineName << "\", "
287 << "\"" << Desc << "\", " << Target << "::" << Name << ", ";
289 ConstRecVec ImpliesList = Feature->getValueAsListOfDefs("Implies");
291 printFeatureMask(OS, ImpliesList, FeatureMap);
293 OS << " },\n";
296 // End feature table.
297 OS << "};\n";
299 return FeatureList.size();
302 unsigned SubtargetEmitter::cpuNames(raw_ostream &OS) {
303 // Begin processor name table.
304 OS << "// Sorted array of names of CPU subtypes, including aliases.\n"
305 << "extern const llvm::StringRef " << Target << "Names[] = {\n";
307 std::vector<const Record *> ProcessorList =
308 Records.getAllDerivedDefinitions("Processor");
310 std::vector<const Record *> ProcessorAliasList =
311 Records.getAllDerivedDefinitionsIfDefined("ProcessorAlias");
313 SmallVector<StringRef> Names;
314 Names.reserve(ProcessorList.size() + ProcessorAliasList.size());
316 for (const Record *Processor : ProcessorList) {
317 StringRef Name = Processor->getValueAsString("Name");
318 Names.push_back(Name);
321 for (const Record *Rec : ProcessorAliasList) {
322 auto Name = Rec->getValueAsString("Name");
323 Names.push_back(Name);
326 llvm::sort(Names);
327 llvm::interleave(
328 Names, OS, [&](StringRef Name) { OS << '"' << Name << '"'; }, ",\n");
330 // End processor name table.
331 OS << "};\n";
333 return Names.size();
337 // CPUKeyValues - Emit data of all the subtarget processors. Used by command
338 // line.
340 unsigned SubtargetEmitter::cpuKeyValues(raw_ostream &OS,
341 const FeatureMapTy &FeatureMap) {
342 // Gather and sort processor information
343 std::vector<const Record *> ProcessorList =
344 Records.getAllDerivedDefinitions("Processor");
345 llvm::sort(ProcessorList, LessRecordFieldName());
347 // Note that unlike `FeatureKeyValues`, here we do not need to check for
348 // duplicate processors, since that is already done when the SubtargetEmitter
349 // constructor calls `getSchedModels` to build a `CodeGenSchedModels` object,
350 // which does the duplicate processor check.
352 // Begin processor table.
353 OS << "// Sorted (by key) array of values for CPU subtype.\n"
354 << "extern const llvm::SubtargetSubTypeKV " << Target
355 << "SubTypeKV[] = {\n";
357 for (const Record *Processor : ProcessorList) {
358 StringRef Name = Processor->getValueAsString("Name");
359 ConstRecVec FeatureList = Processor->getValueAsListOfDefs("Features");
360 ConstRecVec TuneFeatureList =
361 Processor->getValueAsListOfDefs("TuneFeatures");
363 // Emit as "{ "cpu", "description", 0, { f1 , f2 , ... fn } },".
364 OS << " { "
365 << "\"" << Name << "\", ";
367 printFeatureMask(OS, FeatureList, FeatureMap);
368 OS << ", ";
369 printFeatureMask(OS, TuneFeatureList, FeatureMap);
371 // Emit the scheduler model pointer.
372 const std::string &ProcModelName =
373 SchedModels.getModelForProc(Processor).ModelName;
374 OS << ", &" << ProcModelName << " },\n";
377 // End processor table.
378 OS << "};\n";
380 return ProcessorList.size();
384 // FormItineraryStageString - Compose a string containing the stage
385 // data initialization for the specified itinerary. N is the number
386 // of stages.
388 void SubtargetEmitter::formItineraryStageString(const std::string &Name,
389 const Record *ItinData,
390 std::string &ItinString,
391 unsigned &NStages) {
392 // Get states list
393 ConstRecVec StageList = ItinData->getValueAsListOfDefs("Stages");
395 // For each stage
396 unsigned N = NStages = StageList.size();
397 for (unsigned I = 0; I < N;) {
398 // Next stage
399 const Record *Stage = StageList[I];
401 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
402 int Cycles = Stage->getValueAsInt("Cycles");
403 ItinString += " { " + itostr(Cycles) + ", ";
405 // Get unit list
406 ConstRecVec UnitList = Stage->getValueAsListOfDefs("Units");
408 // For each unit
409 for (unsigned J = 0, M = UnitList.size(); J < M;) {
410 // Add name and bitwise or
411 ItinString += Name + "FU::" + UnitList[J]->getName().str();
412 if (++J < M)
413 ItinString += " | ";
416 int TimeInc = Stage->getValueAsInt("TimeInc");
417 ItinString += ", " + itostr(TimeInc);
419 int Kind = Stage->getValueAsInt("Kind");
420 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
422 // Close off stage
423 ItinString += " }";
424 if (++I < N)
425 ItinString += ", ";
430 // FormItineraryOperandCycleString - Compose a string containing the
431 // operand cycle initialization for the specified itinerary. N is the
432 // number of operands that has cycles specified.
434 void SubtargetEmitter::formItineraryOperandCycleString(
435 const Record *ItinData, std::string &ItinString, unsigned &NOperandCycles) {
436 // Get operand cycle list
437 std::vector<int64_t> OperandCycleList =
438 ItinData->getValueAsListOfInts("OperandCycles");
440 // For each operand cycle
441 NOperandCycles = OperandCycleList.size();
442 ListSeparator LS;
443 for (int OCycle : OperandCycleList) {
444 // Next operand cycle
445 ItinString += LS;
446 ItinString += " " + itostr(OCycle);
450 void SubtargetEmitter::formItineraryBypassString(const std::string &Name,
451 const Record *ItinData,
452 std::string &ItinString,
453 unsigned NOperandCycles) {
454 ConstRecVec BypassList = ItinData->getValueAsListOfDefs("Bypasses");
455 unsigned N = BypassList.size();
456 unsigned I = 0;
457 ListSeparator LS;
458 for (; I < N; ++I) {
459 ItinString += LS;
460 ItinString += Name + "Bypass::" + BypassList[I]->getName().str();
462 for (; I < NOperandCycles; ++I) {
463 ItinString += LS;
464 ItinString += " 0";
469 // EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
470 // cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
471 // by CodeGenSchedClass::Index.
473 void SubtargetEmitter::emitStageAndOperandCycleData(
474 raw_ostream &OS, std::vector<std::vector<InstrItinerary>> &ProcItinLists) {
475 // Multiple processor models may share an itinerary record. Emit it once.
476 SmallPtrSet<const Record *, 8> ItinsDefSet;
478 // Emit functional units for all the itineraries.
479 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
481 if (!ItinsDefSet.insert(ProcModel.ItinsDef).second)
482 continue;
484 ConstRecVec FUs = ProcModel.ItinsDef->getValueAsListOfDefs("FU");
485 if (FUs.empty())
486 continue;
488 StringRef Name = ProcModel.ItinsDef->getName();
489 OS << "\n// Functional units for \"" << Name << "\"\n"
490 << "namespace " << Name << "FU {\n";
492 for (unsigned J = 0, FUN = FUs.size(); J < FUN; ++J)
493 OS << " const InstrStage::FuncUnits " << FUs[J]->getName()
494 << " = 1ULL << " << J << ";\n";
496 OS << "} // end namespace " << Name << "FU\n";
498 ConstRecVec BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP");
499 if (!BPs.empty()) {
500 OS << "\n// Pipeline forwarding paths for itineraries \"" << Name
501 << "\"\n"
502 << "namespace " << Name << "Bypass {\n";
504 OS << " const unsigned NoBypass = 0;\n";
505 for (unsigned J = 0, BPN = BPs.size(); J < BPN; ++J)
506 OS << " const unsigned " << BPs[J]->getName() << " = 1 << " << J
507 << ";\n";
509 OS << "} // end namespace " << Name << "Bypass\n";
513 // Begin stages table
514 std::string StageTable =
515 "\nextern const llvm::InstrStage " + Target + "Stages[] = {\n";
516 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
518 // Begin operand cycle table
519 std::string OperandCycleTable =
520 "extern const unsigned " + Target + "OperandCycles[] = {\n";
521 OperandCycleTable += " 0, // No itinerary\n";
523 // Begin pipeline bypass table
524 std::string BypassTable =
525 "extern const unsigned " + Target + "ForwardingPaths[] = {\n";
526 BypassTable += " 0, // No itinerary\n";
528 // For each Itinerary across all processors, add a unique entry to the stages,
529 // operand cycles, and pipeline bypass tables. Then add the new Itinerary
530 // object with computed offsets to the ProcItinLists result.
531 unsigned StageCount = 1, OperandCycleCount = 1;
532 StringMap<unsigned> ItinStageMap, ItinOperandMap;
533 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
534 // Add process itinerary to the list.
535 std::vector<InstrItinerary> &ItinList = ProcItinLists.emplace_back();
537 // If this processor defines no itineraries, then leave the itinerary list
538 // empty.
539 if (!ProcModel.hasItineraries())
540 continue;
542 StringRef Name = ProcModel.ItinsDef->getName();
544 ItinList.resize(SchedModels.numInstrSchedClasses());
545 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
547 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
548 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
550 // Next itinerary data
551 const Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
553 // Get string and stage count
554 std::string ItinStageString;
555 unsigned NStages = 0;
556 if (ItinData)
557 formItineraryStageString(std::string(Name), ItinData, ItinStageString,
558 NStages);
560 // Get string and operand cycle count
561 std::string ItinOperandCycleString;
562 unsigned NOperandCycles = 0;
563 std::string ItinBypassString;
564 if (ItinData) {
565 formItineraryOperandCycleString(ItinData, ItinOperandCycleString,
566 NOperandCycles);
568 formItineraryBypassString(std::string(Name), ItinData, ItinBypassString,
569 NOperandCycles);
572 // Check to see if stage already exists and create if it doesn't
573 uint16_t FindStage = 0;
574 if (NStages > 0) {
575 FindStage = ItinStageMap[ItinStageString];
576 if (FindStage == 0) {
577 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
578 StageTable += ItinStageString + ", // " + itostr(StageCount);
579 if (NStages > 1)
580 StageTable += "-" + itostr(StageCount + NStages - 1);
581 StageTable += "\n";
582 // Record Itin class number.
583 ItinStageMap[ItinStageString] = FindStage = StageCount;
584 StageCount += NStages;
588 // Check to see if operand cycle already exists and create if it doesn't
589 uint16_t FindOperandCycle = 0;
590 if (NOperandCycles > 0) {
591 std::string ItinOperandString =
592 ItinOperandCycleString + ItinBypassString;
593 FindOperandCycle = ItinOperandMap[ItinOperandString];
594 if (FindOperandCycle == 0) {
595 // Emit as cycle, // index
596 OperandCycleTable += ItinOperandCycleString + ", // ";
597 std::string OperandIdxComment = itostr(OperandCycleCount);
598 if (NOperandCycles > 1)
599 OperandIdxComment +=
600 "-" + itostr(OperandCycleCount + NOperandCycles - 1);
601 OperandCycleTable += OperandIdxComment + "\n";
602 // Record Itin class number.
603 ItinOperandMap[ItinOperandCycleString] = FindOperandCycle =
604 OperandCycleCount;
605 // Emit as bypass, // index
606 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
607 OperandCycleCount += NOperandCycles;
611 // Set up itinerary as location and location + stage count
612 int16_t NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
613 InstrItinerary Intinerary = {
614 NumUOps,
615 FindStage,
616 uint16_t(FindStage + NStages),
617 FindOperandCycle,
618 uint16_t(FindOperandCycle + NOperandCycles),
621 // Inject - empty slots will be 0, 0
622 ItinList[SchedClassIdx] = Intinerary;
626 // Closing stage
627 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
628 StageTable += "};\n";
630 // Closing operand cycles
631 OperandCycleTable += " 0 // End operand cycles\n";
632 OperandCycleTable += "};\n";
634 BypassTable += " 0 // End bypass tables\n";
635 BypassTable += "};\n";
637 // Emit tables.
638 OS << StageTable;
639 OS << OperandCycleTable;
640 OS << BypassTable;
644 // EmitProcessorData - Generate data for processor itineraries that were
645 // computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
646 // Itineraries for each processor. The Itinerary lists are indexed on
647 // CodeGenSchedClass::Index.
649 void SubtargetEmitter::emitItineraries(
650 raw_ostream &OS, std::vector<std::vector<InstrItinerary>> &ProcItinLists) {
651 // Multiple processor models may share an itinerary record. Emit it once.
652 SmallPtrSet<const Record *, 8> ItinsDefSet;
654 // For each processor's machine model
655 std::vector<std::vector<InstrItinerary>>::iterator ProcItinListsIter =
656 ProcItinLists.begin();
657 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
658 PE = SchedModels.procModelEnd();
659 PI != PE; ++PI, ++ProcItinListsIter) {
661 const Record *ItinsDef = PI->ItinsDef;
662 if (!ItinsDefSet.insert(ItinsDef).second)
663 continue;
665 // Get the itinerary list for the processor.
666 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
667 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
669 // Empty itineraries aren't referenced anywhere in the tablegen output
670 // so don't emit them.
671 if (ItinList.empty())
672 continue;
674 OS << "\n";
675 OS << "static const llvm::InstrItinerary ";
677 // Begin processor itinerary table
678 OS << ItinsDef->getName() << "[] = {\n";
680 // For each itinerary class in CodeGenSchedClass::Index order.
681 for (unsigned J = 0, M = ItinList.size(); J < M; ++J) {
682 InstrItinerary &Intinerary = ItinList[J];
684 // Emit Itinerary in the form of
685 // { firstStage, lastStage, firstCycle, lastCycle } // index
686 OS << " { " << Intinerary.NumMicroOps << ", " << Intinerary.FirstStage
687 << ", " << Intinerary.LastStage << ", " << Intinerary.FirstOperandCycle
688 << ", " << Intinerary.LastOperandCycle << " }"
689 << ", // " << J << " " << SchedModels.getSchedClass(J).Name << "\n";
691 // End processor itinerary table
692 OS << " { 0, uint16_t(~0U), uint16_t(~0U), uint16_t(~0U), uint16_t(~0U) }"
693 "// end marker\n";
694 OS << "};\n";
698 // Emit either the value defined in the TableGen Record, or the default
699 // value defined in the C++ header. The Record is null if the processor does not
700 // define a model.
701 void SubtargetEmitter::emitProcessorProp(raw_ostream &OS, const Record *R,
702 StringRef Name, char Separator) {
703 OS << " ";
704 int V = R ? R->getValueAsInt(Name) : -1;
705 if (V >= 0)
706 OS << V << Separator << " // " << Name;
707 else
708 OS << "MCSchedModel::Default" << Name << Separator;
709 OS << '\n';
712 void SubtargetEmitter::emitProcessorResourceSubUnits(
713 const CodeGenProcModel &ProcModel, raw_ostream &OS) {
714 OS << "\nstatic const unsigned " << ProcModel.ModelName
715 << "ProcResourceSubUnits[] = {\n"
716 << " 0, // Invalid\n";
718 for (unsigned I = 0, E = ProcModel.ProcResourceDefs.size(); I < E; ++I) {
719 const Record *PRDef = ProcModel.ProcResourceDefs[I];
720 if (!PRDef->isSubClassOf("ProcResGroup"))
721 continue;
722 for (const Record *RUDef : PRDef->getValueAsListOfDefs("Resources")) {
723 const Record *RU =
724 SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc());
725 for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) {
726 OS << " " << ProcModel.getProcResourceIdx(RU) << ", ";
729 OS << " // " << PRDef->getName() << "\n";
731 OS << "};\n";
734 static void emitRetireControlUnitInfo(const CodeGenProcModel &ProcModel,
735 raw_ostream &OS) {
736 int64_t ReorderBufferSize = 0, MaxRetirePerCycle = 0;
737 if (const Record *RCU = ProcModel.RetireControlUnit) {
738 ReorderBufferSize =
739 std::max(ReorderBufferSize, RCU->getValueAsInt("ReorderBufferSize"));
740 MaxRetirePerCycle =
741 std::max(MaxRetirePerCycle, RCU->getValueAsInt("MaxRetirePerCycle"));
744 OS << ReorderBufferSize << ", // ReorderBufferSize\n ";
745 OS << MaxRetirePerCycle << ", // MaxRetirePerCycle\n ";
748 static void emitRegisterFileInfo(const CodeGenProcModel &ProcModel,
749 unsigned NumRegisterFiles,
750 unsigned NumCostEntries, raw_ostream &OS) {
751 if (NumRegisterFiles)
752 OS << ProcModel.ModelName << "RegisterFiles,\n " << (1 + NumRegisterFiles);
753 else
754 OS << "nullptr,\n 0";
756 OS << ", // Number of register files.\n ";
757 if (NumCostEntries)
758 OS << ProcModel.ModelName << "RegisterCosts,\n ";
759 else
760 OS << "nullptr,\n ";
761 OS << NumCostEntries << ", // Number of register cost entries.\n";
764 unsigned
765 SubtargetEmitter::emitRegisterFileTables(const CodeGenProcModel &ProcModel,
766 raw_ostream &OS) {
767 if (llvm::all_of(ProcModel.RegisterFiles, [](const CodeGenRegisterFile &RF) {
768 return RF.hasDefaultCosts();
770 return 0;
772 // Print the RegisterCost table first.
773 OS << "\n// {RegisterClassID, Register Cost, AllowMoveElimination }\n";
774 OS << "static const llvm::MCRegisterCostEntry " << ProcModel.ModelName
775 << "RegisterCosts"
776 << "[] = {\n";
778 for (const CodeGenRegisterFile &RF : ProcModel.RegisterFiles) {
779 // Skip register files with a default cost table.
780 if (RF.hasDefaultCosts())
781 continue;
782 // Add entries to the cost table.
783 for (const CodeGenRegisterCost &RC : RF.Costs) {
784 OS << " { ";
785 const Record *Rec = RC.RCDef;
786 if (Rec->getValue("Namespace"))
787 OS << Rec->getValueAsString("Namespace") << "::";
788 OS << Rec->getName() << "RegClassID, " << RC.Cost << ", "
789 << RC.AllowMoveElimination << "},\n";
792 OS << "};\n";
794 // Now generate a table with register file info.
795 OS << "\n // {Name, #PhysRegs, #CostEntries, IndexToCostTbl, "
796 << "MaxMovesEliminatedPerCycle, AllowZeroMoveEliminationOnly }\n";
797 OS << "static const llvm::MCRegisterFileDesc " << ProcModel.ModelName
798 << "RegisterFiles"
799 << "[] = {\n"
800 << " { \"InvalidRegisterFile\", 0, 0, 0, 0, 0 },\n";
801 unsigned CostTblIndex = 0;
803 for (const CodeGenRegisterFile &RD : ProcModel.RegisterFiles) {
804 OS << " { ";
805 OS << '"' << RD.Name << '"' << ", " << RD.NumPhysRegs << ", ";
806 unsigned NumCostEntries = RD.Costs.size();
807 OS << NumCostEntries << ", " << CostTblIndex << ", "
808 << RD.MaxMovesEliminatedPerCycle << ", "
809 << RD.AllowZeroMoveEliminationOnly << "},\n";
810 CostTblIndex += NumCostEntries;
812 OS << "};\n";
814 return CostTblIndex;
817 void SubtargetEmitter::emitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
818 raw_ostream &OS) {
819 unsigned QueueID = 0;
820 if (ProcModel.LoadQueue) {
821 const Record *Queue = ProcModel.LoadQueue->getValueAsDef("QueueDescriptor");
822 QueueID = 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
823 find(ProcModel.ProcResourceDefs, Queue));
825 OS << " " << QueueID << ", // Resource Descriptor for the Load Queue\n";
827 QueueID = 0;
828 if (ProcModel.StoreQueue) {
829 const Record *Queue =
830 ProcModel.StoreQueue->getValueAsDef("QueueDescriptor");
831 QueueID = 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
832 find(ProcModel.ProcResourceDefs, Queue));
834 OS << " " << QueueID << ", // Resource Descriptor for the Store Queue\n";
837 void SubtargetEmitter::emitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
838 raw_ostream &OS) {
839 // Generate a table of register file descriptors (one entry per each user
840 // defined register file), and a table of register costs.
841 unsigned NumCostEntries = emitRegisterFileTables(ProcModel, OS);
843 // Now generate a table for the extra processor info.
844 OS << "\nstatic const llvm::MCExtraProcessorInfo " << ProcModel.ModelName
845 << "ExtraInfo = {\n ";
847 // Add information related to the retire control unit.
848 emitRetireControlUnitInfo(ProcModel, OS);
850 // Add information related to the register files (i.e. where to find register
851 // file descriptors and register costs).
852 emitRegisterFileInfo(ProcModel, ProcModel.RegisterFiles.size(),
853 NumCostEntries, OS);
855 // Add information about load/store queues.
856 emitLoadStoreQueueInfo(ProcModel, OS);
858 OS << "};\n";
861 void SubtargetEmitter::emitProcessorResources(const CodeGenProcModel &ProcModel,
862 raw_ostream &OS) {
863 emitProcessorResourceSubUnits(ProcModel, OS);
865 OS << "\n// {Name, NumUnits, SuperIdx, BufferSize, SubUnitsIdxBegin}\n";
866 OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName
867 << "ProcResources"
868 << "[] = {\n"
869 << " {\"InvalidUnit\", 0, 0, 0, 0},\n";
871 unsigned SubUnitsOffset = 1;
872 for (unsigned I = 0, E = ProcModel.ProcResourceDefs.size(); I < E; ++I) {
873 const Record *PRDef = ProcModel.ProcResourceDefs[I];
875 const Record *SuperDef = nullptr;
876 unsigned SuperIdx = 0;
877 unsigned NumUnits = 0;
878 const unsigned SubUnitsBeginOffset = SubUnitsOffset;
879 int BufferSize = PRDef->getValueAsInt("BufferSize");
880 if (PRDef->isSubClassOf("ProcResGroup")) {
881 for (const Record *RU : PRDef->getValueAsListOfDefs("Resources")) {
882 NumUnits += RU->getValueAsInt("NumUnits");
883 SubUnitsOffset += RU->getValueAsInt("NumUnits");
885 } else {
886 // Find the SuperIdx
887 if (PRDef->getValueInit("Super")->isComplete()) {
888 SuperDef = SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
889 ProcModel, PRDef->getLoc());
890 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
892 NumUnits = PRDef->getValueAsInt("NumUnits");
894 // Emit the ProcResourceDesc
895 OS << " {\"" << PRDef->getName() << "\", ";
896 if (PRDef->getName().size() < 15)
897 OS.indent(15 - PRDef->getName().size());
898 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
899 if (SubUnitsBeginOffset != SubUnitsOffset) {
900 OS << ProcModel.ModelName << "ProcResourceSubUnits + "
901 << SubUnitsBeginOffset;
902 } else {
903 OS << "nullptr";
905 OS << "}, // #" << I + 1;
906 if (SuperDef)
907 OS << ", Super=" << SuperDef->getName();
908 OS << "\n";
910 OS << "};\n";
913 // Find the WriteRes Record that defines processor resources for this
914 // SchedWrite.
915 const Record *
916 SubtargetEmitter::findWriteResources(const CodeGenSchedRW &SchedWrite,
917 const CodeGenProcModel &ProcModel) {
919 // Check if the SchedWrite is already subtarget-specific and directly
920 // specifies a set of processor resources.
921 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
922 return SchedWrite.TheDef;
924 const Record *AliasDef = nullptr;
925 for (const Record *A : SchedWrite.Aliases) {
926 const CodeGenSchedRW &AliasRW =
927 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
928 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
929 const Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
930 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
931 continue;
933 if (AliasDef)
934 PrintFatalError(AliasRW.TheDef->getLoc(),
935 "Multiple aliases "
936 "defined for processor " +
937 ProcModel.ModelName +
938 " Ensure only one SchedAlias exists per RW.");
939 AliasDef = AliasRW.TheDef;
941 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
942 return AliasDef;
944 // Check this processor's list of write resources.
945 const Record *ResDef = nullptr;
946 for (const Record *WR : ProcModel.WriteResDefs) {
947 if (!WR->isSubClassOf("WriteRes"))
948 continue;
949 const Record *WRDef = WR->getValueAsDef("WriteType");
950 if (AliasDef == WRDef || SchedWrite.TheDef == WRDef) {
951 if (ResDef) {
952 PrintFatalError(WR->getLoc(), "Resources are defined for both "
953 "SchedWrite and its alias on processor " +
954 ProcModel.ModelName);
956 ResDef = WR;
957 // If there is no AliasDef and we find a match, we can early exit since
958 // there is no need to verify whether there are resources defined for both
959 // SchedWrite and its alias.
960 if (!AliasDef)
961 break;
964 // TODO: If ProcModel has a base model (previous generation processor),
965 // then call FindWriteResources recursively with that model here.
966 if (!ResDef) {
967 PrintFatalError(ProcModel.ModelDef->getLoc(),
968 Twine("Processor does not define resources for ") +
969 SchedWrite.TheDef->getName());
971 return ResDef;
974 /// Find the ReadAdvance record for the given SchedRead on this processor or
975 /// return NULL.
976 const Record *
977 SubtargetEmitter::findReadAdvance(const CodeGenSchedRW &SchedRead,
978 const CodeGenProcModel &ProcModel) {
979 // Check for SchedReads that directly specify a ReadAdvance.
980 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
981 return SchedRead.TheDef;
983 // Check this processor's list of aliases for SchedRead.
984 const Record *AliasDef = nullptr;
985 for (const Record *A : SchedRead.Aliases) {
986 const CodeGenSchedRW &AliasRW =
987 SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
988 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
989 const Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
990 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
991 continue;
993 if (AliasDef)
994 PrintFatalError(AliasRW.TheDef->getLoc(),
995 "Multiple aliases "
996 "defined for processor " +
997 ProcModel.ModelName +
998 " Ensure only one SchedAlias exists per RW.");
999 AliasDef = AliasRW.TheDef;
1001 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
1002 return AliasDef;
1004 // Check this processor's ReadAdvanceList.
1005 const Record *ResDef = nullptr;
1006 for (const Record *RA : ProcModel.ReadAdvanceDefs) {
1007 if (!RA->isSubClassOf("ReadAdvance"))
1008 continue;
1009 const Record *RADef = RA->getValueAsDef("ReadType");
1010 if (AliasDef == RADef || SchedRead.TheDef == RADef) {
1011 if (ResDef) {
1012 PrintFatalError(RA->getLoc(), "Resources are defined for both "
1013 "SchedRead and its alias on processor " +
1014 ProcModel.ModelName);
1016 ResDef = RA;
1017 // If there is no AliasDef and we find a match, we can early exit since
1018 // there is no need to verify whether there are resources defined for both
1019 // SchedRead and its alias.
1020 if (!AliasDef)
1021 break;
1024 // TODO: If ProcModel has a base model (previous generation processor),
1025 // then call FindReadAdvance recursively with that model here.
1026 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
1027 PrintFatalError(ProcModel.ModelDef->getLoc(),
1028 Twine("Processor does not define resources for ") +
1029 SchedRead.TheDef->getName());
1031 return ResDef;
1034 // Expand an explicit list of processor resources into a full list of implied
1035 // resource groups and super resources that cover them.
1036 void SubtargetEmitter::expandProcResources(
1037 ConstRecVec &PRVec, std::vector<int64_t> &ReleaseAtCycles,
1038 std::vector<int64_t> &AcquireAtCycles, const CodeGenProcModel &PM) {
1039 assert(PRVec.size() == ReleaseAtCycles.size() && "failed precondition");
1040 for (unsigned I = 0, E = PRVec.size(); I != E; ++I) {
1041 const Record *PRDef = PRVec[I];
1042 ConstRecVec SubResources;
1043 if (PRDef->isSubClassOf("ProcResGroup"))
1044 SubResources = PRDef->getValueAsListOfDefs("Resources");
1045 else {
1046 SubResources.push_back(PRDef);
1047 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
1048 for (const Record *SubDef = PRDef;
1049 SubDef->getValueInit("Super")->isComplete();) {
1050 if (SubDef->isSubClassOf("ProcResGroup")) {
1051 // Disallow this for simplicitly.
1052 PrintFatalError(SubDef->getLoc(), "Processor resource group "
1053 " cannot be a super resources.");
1055 const Record *SuperDef = SchedModels.findProcResUnits(
1056 SubDef->getValueAsDef("Super"), PM, SubDef->getLoc());
1057 PRVec.push_back(SuperDef);
1058 ReleaseAtCycles.push_back(ReleaseAtCycles[I]);
1059 AcquireAtCycles.push_back(AcquireAtCycles[I]);
1060 SubDef = SuperDef;
1063 for (const Record *PR : PM.ProcResourceDefs) {
1064 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
1065 continue;
1066 ConstRecVec SuperResources = PR->getValueAsListOfDefs("Resources");
1067 ConstRecIter SubI = SubResources.begin(), SubE = SubResources.end();
1068 for (; SubI != SubE; ++SubI) {
1069 if (!is_contained(SuperResources, *SubI)) {
1070 break;
1073 if (SubI == SubE) {
1074 PRVec.push_back(PR);
1075 ReleaseAtCycles.push_back(ReleaseAtCycles[I]);
1076 AcquireAtCycles.push_back(AcquireAtCycles[I]);
1082 // Generate the SchedClass table for this processor and update global
1083 // tables. Must be called for each processor in order.
1084 void SubtargetEmitter::genSchedClassTables(const CodeGenProcModel &ProcModel,
1085 SchedClassTables &SchedTables) {
1086 std::vector<MCSchedClassDesc> &SCTab =
1087 SchedTables.ProcSchedClasses.emplace_back();
1088 if (!ProcModel.hasInstrSchedModel())
1089 return;
1091 LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
1092 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1093 LLVM_DEBUG(SC.dump(&SchedModels));
1095 MCSchedClassDesc &SCDesc = SCTab.emplace_back();
1096 // SCDesc.Name is guarded by NDEBUG
1097 SCDesc.NumMicroOps = 0;
1098 SCDesc.BeginGroup = false;
1099 SCDesc.EndGroup = false;
1100 SCDesc.RetireOOO = false;
1101 SCDesc.WriteProcResIdx = 0;
1102 SCDesc.WriteLatencyIdx = 0;
1103 SCDesc.ReadAdvanceIdx = 0;
1105 // A Variant SchedClass has no resources of its own.
1106 bool HasVariants = false;
1107 for (const CodeGenSchedTransition &CGT :
1108 make_range(SC.Transitions.begin(), SC.Transitions.end())) {
1109 if (CGT.ProcIndex == ProcModel.Index) {
1110 HasVariants = true;
1111 break;
1114 if (HasVariants) {
1115 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
1116 continue;
1119 // Determine if the SchedClass is actually reachable on this processor. If
1120 // not don't try to locate the processor resources, it will fail.
1121 // If ProcIndices contains 0, this class applies to all processors.
1122 assert(!SC.ProcIndices.empty() && "expect at least one procidx");
1123 if (SC.ProcIndices[0] != 0) {
1124 if (!is_contained(SC.ProcIndices, ProcModel.Index))
1125 continue;
1127 IdxVec Writes = SC.Writes;
1128 IdxVec Reads = SC.Reads;
1129 if (!SC.InstRWs.empty()) {
1130 // This class has a default ReadWrite list which can be overridden by
1131 // InstRW definitions.
1132 const Record *RWDef = nullptr;
1133 for (const Record *RW : SC.InstRWs) {
1134 const Record *RWModelDef = RW->getValueAsDef("SchedModel");
1135 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
1136 RWDef = RW;
1137 break;
1140 if (RWDef) {
1141 Writes.clear();
1142 Reads.clear();
1143 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
1144 Writes, Reads);
1147 if (Writes.empty()) {
1148 // Check this processor's itinerary class resources.
1149 for (const Record *I : ProcModel.ItinRWDefs) {
1150 ConstRecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
1151 if (is_contained(Matched, SC.ItinClassDef)) {
1152 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
1153 Writes, Reads);
1154 break;
1157 if (Writes.empty()) {
1158 LLVM_DEBUG(dbgs() << ProcModel.ModelName
1159 << " does not have resources for class " << SC.Name
1160 << '\n');
1161 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1164 // Sum resources across all operand writes.
1165 std::vector<MCWriteProcResEntry> WriteProcResources;
1166 std::vector<MCWriteLatencyEntry> WriteLatencies;
1167 std::vector<std::string> WriterNames;
1168 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
1169 for (unsigned W : Writes) {
1170 IdxVec WriteSeq;
1171 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false, ProcModel);
1173 // For each operand, create a latency entry.
1174 MCWriteLatencyEntry WLEntry;
1175 WLEntry.Cycles = 0;
1176 unsigned WriteID = WriteSeq.back();
1177 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
1178 // If this Write is not referenced by a ReadAdvance, don't distinguish it
1179 // from other WriteLatency entries.
1180 if (!ProcModel.hasReadOfWrite(SchedModels.getSchedWrite(WriteID).TheDef))
1181 WriteID = 0;
1182 WLEntry.WriteResourceID = WriteID;
1184 for (unsigned WS : WriteSeq) {
1185 const Record *WriteRes =
1186 findWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
1188 // Mark the parent class as invalid for unsupported write types.
1189 if (WriteRes->getValueAsBit("Unsupported")) {
1190 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1191 break;
1193 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
1194 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
1195 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
1196 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
1197 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
1198 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
1199 SCDesc.RetireOOO |= WriteRes->getValueAsBit("RetireOOO");
1201 // Create an entry for each ProcResource listed in WriteRes.
1202 ConstRecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
1203 std::vector<int64_t> ReleaseAtCycles =
1204 WriteRes->getValueAsListOfInts("ReleaseAtCycles");
1206 std::vector<int64_t> AcquireAtCycles =
1207 WriteRes->getValueAsListOfInts("AcquireAtCycles");
1209 // Check consistency of the two vectors carrying the start and
1210 // stop cycles of the resources.
1211 if (!ReleaseAtCycles.empty() &&
1212 ReleaseAtCycles.size() != PRVec.size()) {
1213 // If ReleaseAtCycles is provided, check consistency.
1214 PrintFatalError(
1215 WriteRes->getLoc(),
1216 Twine("Inconsistent release at cycles: size(ReleaseAtCycles) != "
1217 "size(ProcResources): ")
1218 .concat(Twine(PRVec.size()))
1219 .concat(" vs ")
1220 .concat(Twine(ReleaseAtCycles.size())));
1223 if (!AcquireAtCycles.empty() &&
1224 AcquireAtCycles.size() != PRVec.size()) {
1225 PrintFatalError(
1226 WriteRes->getLoc(),
1227 Twine("Inconsistent resource cycles: size(AcquireAtCycles) != "
1228 "size(ProcResources): ")
1229 .concat(Twine(AcquireAtCycles.size()))
1230 .concat(" vs ")
1231 .concat(Twine(PRVec.size())));
1234 if (ReleaseAtCycles.empty()) {
1235 // If ReleaseAtCycles is not provided, default to one cycle
1236 // per resource.
1237 ReleaseAtCycles.resize(PRVec.size(), 1);
1240 if (AcquireAtCycles.empty()) {
1241 // If AcquireAtCycles is not provided, reserve the resource
1242 // starting from cycle 0.
1243 AcquireAtCycles.resize(PRVec.size(), 0);
1246 assert(AcquireAtCycles.size() == ReleaseAtCycles.size());
1248 expandProcResources(PRVec, ReleaseAtCycles, AcquireAtCycles, ProcModel);
1249 assert(AcquireAtCycles.size() == ReleaseAtCycles.size());
1251 for (unsigned PRIdx = 0, PREnd = PRVec.size(); PRIdx != PREnd;
1252 ++PRIdx) {
1253 MCWriteProcResEntry WPREntry;
1254 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
1255 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
1256 WPREntry.ReleaseAtCycle = ReleaseAtCycles[PRIdx];
1257 WPREntry.AcquireAtCycle = AcquireAtCycles[PRIdx];
1258 if (AcquireAtCycles[PRIdx] > ReleaseAtCycles[PRIdx]) {
1259 PrintFatalError(
1260 WriteRes->getLoc(),
1261 Twine("Inconsistent resource cycles: AcquireAtCycles "
1262 "< ReleaseAtCycles must hold."));
1264 if (AcquireAtCycles[PRIdx] < 0) {
1265 PrintFatalError(WriteRes->getLoc(),
1266 Twine("Invalid value: AcquireAtCycle "
1267 "must be a non-negative value."));
1269 // If this resource is already used in this sequence, add the current
1270 // entry's cycles so that the same resource appears to be used
1271 // serially, rather than multiple parallel uses. This is important for
1272 // in-order machine where the resource consumption is a hazard.
1273 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
1274 for (; WPRIdx != WPREnd; ++WPRIdx) {
1275 if (WriteProcResources[WPRIdx].ProcResourceIdx ==
1276 WPREntry.ProcResourceIdx) {
1277 // TODO: multiple use of the same resources would
1278 // require either 1. thinking of how to handle multiple
1279 // intervals for the same resource in
1280 // `<Target>WriteProcResTable` (see
1281 // `SubtargetEmitter::EmitSchedClassTables`), or
1282 // 2. thinking how to merge multiple intervals into a
1283 // single interval.
1284 assert(WPREntry.AcquireAtCycle == 0 &&
1285 "multiple use ofthe same resource is not yet handled");
1286 WriteProcResources[WPRIdx].ReleaseAtCycle +=
1287 WPREntry.ReleaseAtCycle;
1288 break;
1291 if (WPRIdx == WPREnd)
1292 WriteProcResources.push_back(WPREntry);
1295 WriteLatencies.push_back(WLEntry);
1297 // Create an entry for each operand Read in this SchedClass.
1298 // Entries must be sorted first by UseIdx then by WriteResourceID.
1299 for (unsigned UseIdx = 0, EndIdx = Reads.size(); UseIdx != EndIdx;
1300 ++UseIdx) {
1301 const Record *ReadAdvance =
1302 findReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
1303 if (!ReadAdvance)
1304 continue;
1306 // Mark the parent class as invalid for unsupported write types.
1307 if (ReadAdvance->getValueAsBit("Unsupported")) {
1308 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1309 break;
1311 ConstRecVec ValidWrites =
1312 ReadAdvance->getValueAsListOfDefs("ValidWrites");
1313 IdxVec WriteIDs;
1314 if (ValidWrites.empty())
1315 WriteIDs.push_back(0);
1316 else {
1317 for (const Record *VW : ValidWrites) {
1318 unsigned WriteID = SchedModels.getSchedRWIdx(VW, /*IsRead=*/false);
1319 assert(WriteID != 0 &&
1320 "Expected a valid SchedRW in the list of ValidWrites");
1321 WriteIDs.push_back(WriteID);
1324 llvm::sort(WriteIDs);
1325 for (unsigned W : WriteIDs) {
1326 MCReadAdvanceEntry RAEntry;
1327 RAEntry.UseIdx = UseIdx;
1328 RAEntry.WriteResourceID = W;
1329 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1330 ReadAdvanceEntries.push_back(RAEntry);
1333 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1334 WriteProcResources.clear();
1335 WriteLatencies.clear();
1336 ReadAdvanceEntries.clear();
1338 // Add the information for this SchedClass to the global tables using basic
1339 // compression.
1341 // WritePrecRes entries are sorted by ProcResIdx.
1342 llvm::sort(WriteProcResources, LessWriteProcResources());
1344 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1345 std::vector<MCWriteProcResEntry>::iterator WPRPos =
1346 std::search(SchedTables.WriteProcResources.begin(),
1347 SchedTables.WriteProcResources.end(),
1348 WriteProcResources.begin(), WriteProcResources.end());
1349 if (WPRPos != SchedTables.WriteProcResources.end())
1350 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1351 else {
1352 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1353 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1354 WriteProcResources.end());
1356 // Latency entries must remain in operand order.
1357 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1358 std::vector<MCWriteLatencyEntry>::iterator WLPos = std::search(
1359 SchedTables.WriteLatencies.begin(), SchedTables.WriteLatencies.end(),
1360 WriteLatencies.begin(), WriteLatencies.end());
1361 if (WLPos != SchedTables.WriteLatencies.end()) {
1362 unsigned Idx = WLPos - SchedTables.WriteLatencies.begin();
1363 SCDesc.WriteLatencyIdx = Idx;
1364 for (unsigned I = 0, E = WriteLatencies.size(); I < E; ++I)
1365 if (SchedTables.WriterNames[Idx + I].find(WriterNames[I]) ==
1366 std::string::npos) {
1367 SchedTables.WriterNames[Idx + I] += std::string("_") + WriterNames[I];
1369 } else {
1370 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
1371 llvm::append_range(SchedTables.WriteLatencies, WriteLatencies);
1372 llvm::append_range(SchedTables.WriterNames, WriterNames);
1374 // ReadAdvanceEntries must remain in operand order.
1375 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1376 std::vector<MCReadAdvanceEntry>::iterator RAPos =
1377 std::search(SchedTables.ReadAdvanceEntries.begin(),
1378 SchedTables.ReadAdvanceEntries.end(),
1379 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1380 if (RAPos != SchedTables.ReadAdvanceEntries.end())
1381 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1382 else {
1383 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1384 llvm::append_range(SchedTables.ReadAdvanceEntries, ReadAdvanceEntries);
1389 // Emit SchedClass tables for all processors and associated global tables.
1390 void SubtargetEmitter::emitSchedClassTables(SchedClassTables &SchedTables,
1391 raw_ostream &OS) {
1392 // Emit global WriteProcResTable.
1393 OS << "\n// {ProcResourceIdx, ReleaseAtCycle, AcquireAtCycle}\n"
1394 << "extern const llvm::MCWriteProcResEntry " << Target
1395 << "WriteProcResTable[] = {\n"
1396 << " { 0, 0, 0 }, // Invalid\n";
1397 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1398 WPRIdx != WPREnd; ++WPRIdx) {
1399 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1400 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1401 << format("%2d", WPREntry.ReleaseAtCycle) << ", "
1402 << format("%2d", WPREntry.AcquireAtCycle) << "}";
1403 if (WPRIdx + 1 < WPREnd)
1404 OS << ',';
1405 OS << " // #" << WPRIdx << '\n';
1407 OS << "}; // " << Target << "WriteProcResTable\n";
1409 // Emit global WriteLatencyTable.
1410 OS << "\n// {Cycles, WriteResourceID}\n"
1411 << "extern const llvm::MCWriteLatencyEntry " << Target
1412 << "WriteLatencyTable[] = {\n"
1413 << " { 0, 0}, // Invalid\n";
1414 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1415 WLIdx != WLEnd; ++WLIdx) {
1416 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1417 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1418 << format("%2d", WLEntry.WriteResourceID) << "}";
1419 if (WLIdx + 1 < WLEnd)
1420 OS << ',';
1421 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
1423 OS << "}; // " << Target << "WriteLatencyTable\n";
1425 // Emit global ReadAdvanceTable.
1426 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1427 << "extern const llvm::MCReadAdvanceEntry " << Target
1428 << "ReadAdvanceTable[] = {\n"
1429 << " {0, 0, 0}, // Invalid\n";
1430 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1431 RAIdx != RAEnd; ++RAIdx) {
1432 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1433 OS << " {" << RAEntry.UseIdx << ", "
1434 << format("%2d", RAEntry.WriteResourceID) << ", "
1435 << format("%2d", RAEntry.Cycles) << "}";
1436 if (RAIdx + 1 < RAEnd)
1437 OS << ',';
1438 OS << " // #" << RAIdx << '\n';
1440 OS << "}; // " << Target << "ReadAdvanceTable\n";
1442 // Emit a SchedClass table for each processor.
1443 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1444 PE = SchedModels.procModelEnd();
1445 PI != PE; ++PI) {
1446 if (!PI->hasInstrSchedModel())
1447 continue;
1449 std::vector<MCSchedClassDesc> &SCTab =
1450 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
1452 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup, RetireOOO,"
1453 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1454 OS << "static const llvm::MCSchedClassDesc " << PI->ModelName
1455 << "SchedClasses[] = {\n";
1457 // The first class is always invalid. We no way to distinguish it except by
1458 // name and position.
1459 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel" &&
1460 "invalid class not first");
1461 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1462 << MCSchedClassDesc::InvalidNumMicroOps
1463 << ", false, false, false, 0, 0, 0, 0, 0, 0},\n";
1465 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1466 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1467 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1468 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1469 if (SchedClass.Name.size() < 18)
1470 OS.indent(18 - SchedClass.Name.size());
1471 OS << MCDesc.NumMicroOps << ", " << (MCDesc.BeginGroup ? "true" : "false")
1472 << ", " << (MCDesc.EndGroup ? "true" : "false") << ", "
1473 << (MCDesc.RetireOOO ? "true" : "false") << ", "
1474 << format("%2d", MCDesc.WriteProcResIdx) << ", "
1475 << MCDesc.NumWriteProcResEntries << ", "
1476 << format("%2d", MCDesc.WriteLatencyIdx) << ", "
1477 << MCDesc.NumWriteLatencyEntries << ", "
1478 << format("%2d", MCDesc.ReadAdvanceIdx) << ", "
1479 << MCDesc.NumReadAdvanceEntries << "}, // #" << SCIdx << '\n';
1481 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1485 void SubtargetEmitter::emitProcessorModels(raw_ostream &OS) {
1486 // For each processor model.
1487 for (const CodeGenProcModel &PM : SchedModels.procModels()) {
1488 // Emit extra processor info if available.
1489 if (PM.hasExtraProcessorInfo())
1490 emitExtraProcessorInfo(PM, OS);
1491 // Emit processor resource table.
1492 if (PM.hasInstrSchedModel())
1493 emitProcessorResources(PM, OS);
1494 else if (!PM.ProcResourceDefs.empty())
1495 PrintFatalError(PM.ModelDef->getLoc(),
1496 "SchedMachineModel defines "
1497 "ProcResources without defining WriteRes SchedWriteRes");
1499 // Begin processor itinerary properties
1500 OS << "\n";
1501 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1502 emitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1503 emitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1504 emitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1505 emitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1506 emitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1507 emitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
1509 bool PostRAScheduler =
1510 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
1512 OS << " " << (PostRAScheduler ? "true" : "false") << ", // "
1513 << "PostRAScheduler\n";
1515 bool CompleteModel =
1516 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1518 OS << " " << (CompleteModel ? "true" : "false") << ", // "
1519 << "CompleteModel\n";
1521 bool EnableIntervals =
1522 (PM.ModelDef ? PM.ModelDef->getValueAsBit("EnableIntervals") : false);
1524 OS << " " << (EnableIntervals ? "true" : "false") << ", // "
1525 << "EnableIntervals\n";
1527 OS << " " << PM.Index << ", // Processor ID\n";
1528 if (PM.hasInstrSchedModel())
1529 OS << " " << PM.ModelName << "ProcResources"
1530 << ",\n"
1531 << " " << PM.ModelName << "SchedClasses"
1532 << ",\n"
1533 << " " << PM.ProcResourceDefs.size() + 1 << ",\n"
1534 << " "
1535 << (SchedModels.schedClassEnd() - SchedModels.schedClassBegin())
1536 << ",\n";
1537 else
1538 OS << " nullptr, nullptr, 0, 0,"
1539 << " // No instruction-level machine model.\n";
1540 if (PM.hasItineraries())
1541 OS << " " << PM.ItinsDef->getName() << ",\n";
1542 else
1543 OS << " nullptr, // No Itinerary\n";
1544 if (PM.hasExtraProcessorInfo())
1545 OS << " &" << PM.ModelName << "ExtraInfo,\n";
1546 else
1547 OS << " nullptr // No extra processor descriptor\n";
1548 OS << "};\n";
1553 // EmitSchedModel - Emits all scheduling model tables, folding common patterns.
1555 void SubtargetEmitter::emitSchedModel(raw_ostream &OS) {
1556 OS << "#ifdef DBGFIELD\n"
1557 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1558 << "#endif\n"
1559 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
1560 << "#define DBGFIELD(x) x,\n"
1561 << "#else\n"
1562 << "#define DBGFIELD(x)\n"
1563 << "#endif\n";
1565 if (SchedModels.hasItineraries()) {
1566 std::vector<std::vector<InstrItinerary>> ProcItinLists;
1567 // Emit the stage data
1568 emitStageAndOperandCycleData(OS, ProcItinLists);
1569 emitItineraries(OS, ProcItinLists);
1571 OS << "\n// ===============================================================\n"
1572 << "// Data tables for the new per-operand machine model.\n";
1574 SchedClassTables SchedTables;
1575 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1576 genSchedClassTables(ProcModel, SchedTables);
1578 emitSchedClassTables(SchedTables, OS);
1580 OS << "\n#undef DBGFIELD\n";
1582 // Emit the processor machine model
1583 emitProcessorModels(OS);
1586 static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) {
1587 std::string Buffer;
1588 raw_string_ostream Stream(Buffer);
1590 // Print all PredicateProlog records to the output stream.
1591 for (const Record *P : Records.getAllDerivedDefinitions("PredicateProlog"))
1592 Stream << P->getValueAsString("Code") << '\n';
1594 OS << Buffer;
1597 static bool isTruePredicate(const Record *Rec) {
1598 return Rec->isSubClassOf("MCSchedPredicate") &&
1599 Rec->getValueAsDef("Pred")->isSubClassOf("MCTrue");
1602 static void emitPredicates(const CodeGenSchedTransition &T,
1603 const CodeGenSchedClass &SC, PredicateExpander &PE,
1604 raw_ostream &OS) {
1605 std::string Buffer;
1606 raw_string_ostream SS(Buffer);
1608 // If not all predicates are MCTrue, then we need an if-stmt.
1609 unsigned NumNonTruePreds =
1610 T.PredTerm.size() - count_if(T.PredTerm, isTruePredicate);
1612 SS << PE.getIndent();
1614 if (NumNonTruePreds) {
1615 bool FirstNonTruePredicate = true;
1616 SS << "if (";
1618 PE.getIndent() += 2;
1620 for (const Record *Rec : T.PredTerm) {
1621 // Skip predicates that evaluate to "true".
1622 if (isTruePredicate(Rec))
1623 continue;
1625 if (FirstNonTruePredicate) {
1626 FirstNonTruePredicate = false;
1627 } else {
1628 SS << "\n";
1629 SS << PE.getIndent();
1630 SS << "&& ";
1633 if (Rec->isSubClassOf("MCSchedPredicate")) {
1634 PE.expandPredicate(SS, Rec->getValueAsDef("Pred"));
1635 continue;
1638 // Expand this legacy predicate and wrap it around braces if there is more
1639 // than one predicate to expand.
1640 SS << ((NumNonTruePreds > 1) ? "(" : "")
1641 << Rec->getValueAsString("Predicate")
1642 << ((NumNonTruePreds > 1) ? ")" : "");
1645 SS << ")\n"; // end of if-stmt
1646 --PE.getIndent();
1647 SS << PE.getIndent();
1648 --PE.getIndent();
1651 SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
1652 OS << Buffer;
1655 // Used by method `SubtargetEmitter::emitSchedModelHelpersImpl()` to generate
1656 // epilogue code for the auto-generated helper.
1657 static void emitSchedModelHelperEpilogue(raw_ostream &OS,
1658 bool ShouldReturnZero) {
1659 if (ShouldReturnZero) {
1660 OS << " // Don't know how to resolve this scheduling class.\n"
1661 << " return 0;\n";
1662 return;
1665 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n";
1668 static bool hasMCSchedPredicates(const CodeGenSchedTransition &T) {
1669 return all_of(T.PredTerm, [](const Record *Rec) {
1670 return Rec->isSubClassOf("MCSchedPredicate");
1674 static void collectVariantClasses(const CodeGenSchedModels &SchedModels,
1675 IdxVec &VariantClasses,
1676 bool OnlyExpandMCInstPredicates) {
1677 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1678 // Ignore non-variant scheduling classes.
1679 if (SC.Transitions.empty())
1680 continue;
1682 if (OnlyExpandMCInstPredicates) {
1683 // Ignore this variant scheduling class no transitions use any meaningful
1684 // MCSchedPredicate definitions.
1685 if (llvm::none_of(SC.Transitions, hasMCSchedPredicates))
1686 continue;
1689 VariantClasses.push_back(SC.Index);
1693 static void collectProcessorIndices(const CodeGenSchedClass &SC,
1694 IdxVec &ProcIndices) {
1695 // A variant scheduling class may define transitions for multiple
1696 // processors. This function identifies wich processors are associated with
1697 // transition rules specified by variant class `SC`.
1698 for (const CodeGenSchedTransition &T : SC.Transitions) {
1699 IdxVec PI;
1700 std::set_union(&T.ProcIndex, &T.ProcIndex + 1, ProcIndices.begin(),
1701 ProcIndices.end(), std::back_inserter(PI));
1702 ProcIndices = std::move(PI);
1706 static bool isAlwaysTrue(const CodeGenSchedTransition &T) {
1707 return llvm::all_of(T.PredTerm, isTruePredicate);
1710 void SubtargetEmitter::emitSchedModelHelpersImpl(
1711 raw_ostream &OS, bool OnlyExpandMCInstPredicates) {
1712 IdxVec VariantClasses;
1713 collectVariantClasses(SchedModels, VariantClasses,
1714 OnlyExpandMCInstPredicates);
1716 if (VariantClasses.empty()) {
1717 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1718 return;
1721 // Construct a switch statement where the condition is a check on the
1722 // scheduling class identifier. There is a `case` for every variant class
1723 // defined by the processor models of this target.
1724 // Each `case` implements a number of rules to resolve (i.e. to transition
1725 // from) a variant scheduling class to another scheduling class. Rules are
1726 // described by instances of CodeGenSchedTransition. Note that transitions may
1727 // not be valid for all processors.
1728 OS << " switch (SchedClass) {\n";
1729 for (unsigned VC : VariantClasses) {
1730 IdxVec ProcIndices;
1731 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1732 collectProcessorIndices(SC, ProcIndices);
1734 OS << " case " << VC << ": // " << SC.Name << '\n';
1736 PredicateExpander PE(Target);
1737 PE.setByRef(false);
1738 PE.setExpandForMC(OnlyExpandMCInstPredicates);
1739 for (unsigned PI : ProcIndices) {
1740 OS << " ";
1742 // Emit a guard on the processor ID.
1743 if (PI != 0) {
1744 OS << (OnlyExpandMCInstPredicates
1745 ? "if (CPUID == "
1746 : "if (SchedModel->getProcessorID() == ");
1747 OS << PI << ") ";
1748 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName << '\n';
1751 // Now emit transitions associated with processor PI.
1752 const CodeGenSchedTransition *FinalT = nullptr;
1753 for (const CodeGenSchedTransition &T : SC.Transitions) {
1754 if (PI != 0 && T.ProcIndex != PI)
1755 continue;
1757 // Emit only transitions based on MCSchedPredicate, if it's the case.
1758 // At least the transition specified by NoSchedPred is emitted,
1759 // which becomes the default transition for those variants otherwise
1760 // not based on MCSchedPredicate.
1761 // FIXME: preferably, llvm-mca should instead assume a reasonable
1762 // default when a variant transition is not based on MCSchedPredicate
1763 // for a given processor.
1764 if (OnlyExpandMCInstPredicates && !hasMCSchedPredicates(T))
1765 continue;
1767 // If transition is folded to 'return X' it should be the last one.
1768 if (isAlwaysTrue(T)) {
1769 FinalT = &T;
1770 continue;
1772 PE.getIndent() = 3;
1773 emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS);
1775 if (FinalT)
1776 emitPredicates(*FinalT, SchedModels.getSchedClass(FinalT->ToClassIdx),
1777 PE, OS);
1779 OS << " }\n";
1781 if (PI == 0)
1782 break;
1785 if (SC.isInferred())
1786 OS << " return " << SC.Index << ";\n";
1787 OS << " break;\n";
1790 OS << " };\n";
1792 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1795 void SubtargetEmitter::emitSchedModelHelpers(const std::string &ClassName,
1796 raw_ostream &OS) {
1797 OS << "unsigned " << ClassName
1798 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1799 << " const TargetSchedModel *SchedModel) const {\n";
1801 // Emit the predicate prolog code.
1802 emitPredicateProlog(Records, OS);
1804 // Emit target predicates.
1805 emitSchedModelHelpersImpl(OS);
1807 OS << "} // " << ClassName << "::resolveSchedClass\n\n";
1809 OS << "unsigned " << ClassName
1810 << "\n::resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI,"
1811 << " const MCInstrInfo *MCII, unsigned CPUID) const {\n"
1812 << " return " << Target << "_MC"
1813 << "::resolveVariantSchedClassImpl(SchedClass, MI, MCII, CPUID);\n"
1814 << "} // " << ClassName << "::resolveVariantSchedClass\n\n";
1816 STIPredicateExpander PE(Target, /*Indent=*/0);
1817 PE.setClassPrefix(ClassName);
1818 PE.setExpandDefinition(true);
1819 PE.setByRef(false);
1821 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1822 PE.expandSTIPredicate(OS, Fn);
1825 void SubtargetEmitter::emitHwModeCheck(const std::string &ClassName,
1826 raw_ostream &OS) {
1827 const CodeGenHwModes &CGH = TGT.getHwModes();
1828 assert(CGH.getNumModeIds() > 0);
1829 if (CGH.getNumModeIds() == 1)
1830 return;
1832 // Collect all HwModes and related features defined in the TD files,
1833 // and store them as a bit set.
1834 unsigned ValueTypeModes = 0;
1835 unsigned RegInfoModes = 0;
1836 unsigned EncodingInfoModes = 0;
1837 for (const auto &MS : CGH.getHwModeSelects()) {
1838 for (const HwModeSelect::PairType &P : MS.second.Items) {
1839 if (P.first == DefaultMode)
1840 continue;
1841 if (P.second->isSubClassOf("ValueType")) {
1842 ValueTypeModes |= (1 << (P.first - 1));
1843 } else if (P.second->isSubClassOf("RegInfo") ||
1844 P.second->isSubClassOf("SubRegRange")) {
1845 RegInfoModes |= (1 << (P.first - 1));
1846 } else if (P.second->isSubClassOf("InstructionEncoding")) {
1847 EncodingInfoModes |= (1 << (P.first - 1));
1852 // Start emitting for getHwModeSet().
1853 OS << "unsigned " << ClassName << "::getHwModeSet() const {\n";
1854 OS << " // Collect HwModes and store them as a bit set.\n";
1855 OS << " unsigned Modes = 0;\n";
1856 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1857 const HwMode &HM = CGH.getMode(M);
1858 OS << " if (checkFeatures(\"" << HM.Features << "\")) Modes |= (1 << "
1859 << (M - 1) << ");\n";
1861 OS << " return Modes;\n}\n";
1862 // End emitting for getHwModeSet().
1864 auto HandlePerMode = [&](std::string ModeType, unsigned ModeInBitSet) {
1865 OS << " case HwMode_" << ModeType << ":\n"
1866 << " Modes &= " << ModeInBitSet << ";\n"
1867 << " if (!Modes)\n return Modes;\n"
1868 << " if (!llvm::has_single_bit<unsigned>(Modes))\n"
1869 << " llvm_unreachable(\"Two or more HwModes for " << ModeType
1870 << " were found!\");\n"
1871 << " return llvm::countr_zero(Modes) + 1;\n";
1874 // Start emitting for getHwMode().
1875 OS << "unsigned " << ClassName
1876 << "::getHwMode(enum HwModeType type) const {\n";
1877 OS << " unsigned Modes = getHwModeSet();\n\n";
1878 OS << " if (!Modes)\n return Modes;\n\n";
1879 OS << " switch (type) {\n";
1880 OS << " case HwMode_Default:\n return llvm::countr_zero(Modes) + 1;\n";
1881 HandlePerMode("ValueType", ValueTypeModes);
1882 HandlePerMode("RegInfo", RegInfoModes);
1883 HandlePerMode("EncodingInfo", EncodingInfoModes);
1884 OS << " }\n";
1885 OS << " llvm_unreachable(\"unexpected HwModeType\");\n"
1886 << " return 0; // should not get here\n}\n";
1887 // End emitting for getHwMode().
1890 void SubtargetEmitter::emitGetMacroFusions(const std::string &ClassName,
1891 raw_ostream &OS) {
1892 if (!TGT.hasMacroFusion())
1893 return;
1895 OS << "std::vector<MacroFusionPredTy> " << ClassName
1896 << "::getMacroFusions() const {\n";
1897 OS.indent(2) << "std::vector<MacroFusionPredTy> Fusions;\n";
1898 for (auto *Fusion : TGT.getMacroFusions()) {
1899 std::string Name = Fusion->getNameInitAsString();
1900 OS.indent(2) << "if (hasFeature(" << Target << "::" << Name
1901 << ")) Fusions.push_back(llvm::is" << Name << ");\n";
1904 OS.indent(2) << "return Fusions;\n";
1905 OS << "}\n";
1908 // Produces a subtarget specific function for parsing
1909 // the subtarget features string.
1910 void SubtargetEmitter::parseFeaturesFunction(raw_ostream &OS) {
1911 ArrayRef<const Record *> Features =
1912 Records.getAllDerivedDefinitions("SubtargetFeature");
1914 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1915 << "// subtarget options.\n"
1916 << "void llvm::";
1917 OS << Target;
1918 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, "
1919 << "StringRef FS) {\n"
1920 << " LLVM_DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
1921 << " LLVM_DEBUG(dbgs() << \"\\nCPU:\" << CPU);\n"
1922 << " LLVM_DEBUG(dbgs() << \"\\nTuneCPU:\" << TuneCPU << \"\\n\\n\");\n";
1924 if (Features.empty()) {
1925 OS << "}\n";
1926 return;
1929 if (Target == "AArch64")
1930 OS << " CPU = AArch64::resolveCPUAlias(CPU);\n"
1931 << " TuneCPU = AArch64::resolveCPUAlias(TuneCPU);\n";
1933 OS << " InitMCProcessorInfo(CPU, TuneCPU, FS);\n"
1934 << " const FeatureBitset &Bits = getFeatureBits();\n";
1936 for (const Record *R : Features) {
1937 // Next record
1938 StringRef Instance = R->getName();
1939 StringRef Value = R->getValueAsString("Value");
1940 StringRef FieldName = R->getValueAsString("FieldName");
1942 if (Value == "true" || Value == "false")
1943 OS << " if (Bits[" << Target << "::" << Instance << "]) " << FieldName
1944 << " = " << Value << ";\n";
1945 else
1946 OS << " if (Bits[" << Target << "::" << Instance << "] && " << FieldName
1947 << " < " << Value << ") " << FieldName << " = " << Value << ";\n";
1950 OS << "}\n";
1953 void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
1954 OS << "namespace " << Target << "_MC {\n"
1955 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n"
1956 << " const MCInst *MI, const MCInstrInfo *MCII, unsigned CPUID) {\n";
1957 emitSchedModelHelpersImpl(OS, /* OnlyExpandMCPredicates */ true);
1958 OS << "}\n";
1959 OS << "} // end namespace " << Target << "_MC\n\n";
1961 OS << "struct " << Target
1962 << "GenMCSubtargetInfo : public MCSubtargetInfo {\n";
1963 OS << " " << Target << "GenMCSubtargetInfo(const Triple &TT,\n"
1964 << " StringRef CPU, StringRef TuneCPU, StringRef FS,\n"
1965 << " ArrayRef<StringRef> PN,\n"
1966 << " ArrayRef<SubtargetFeatureKV> PF,\n"
1967 << " ArrayRef<SubtargetSubTypeKV> PD,\n"
1968 << " const MCWriteProcResEntry *WPR,\n"
1969 << " const MCWriteLatencyEntry *WL,\n"
1970 << " const MCReadAdvanceEntry *RA, const InstrStage *IS,\n"
1971 << " const unsigned *OC, const unsigned *FP) :\n"
1972 << " MCSubtargetInfo(TT, CPU, TuneCPU, FS, PN, PF, PD,\n"
1973 << " WPR, WL, RA, IS, OC, FP) { }\n\n"
1974 << " unsigned resolveVariantSchedClass(unsigned SchedClass,\n"
1975 << " const MCInst *MI, const MCInstrInfo *MCII,\n"
1976 << " unsigned CPUID) const override {\n"
1977 << " return " << Target << "_MC"
1978 << "::resolveVariantSchedClassImpl(SchedClass, MI, MCII, CPUID);\n";
1979 OS << " }\n";
1980 if (TGT.getHwModes().getNumModeIds() > 1) {
1981 OS << " unsigned getHwModeSet() const override;\n";
1982 OS << " unsigned getHwMode(enum HwModeType type = HwMode_Default) const "
1983 "override;\n";
1985 if (Target == "AArch64")
1986 OS << " bool isCPUStringValid(StringRef CPU) const override {\n"
1987 << " CPU = AArch64::resolveCPUAlias(CPU);\n"
1988 << " return MCSubtargetInfo::isCPUStringValid(CPU);\n"
1989 << " }\n";
1990 OS << "};\n";
1991 emitHwModeCheck(Target + "GenMCSubtargetInfo", OS);
1994 void SubtargetEmitter::emitMcInstrAnalysisPredicateFunctions(raw_ostream &OS) {
1995 OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n";
1996 OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1998 STIPredicateExpander PE(Target, /*Indent=*/0);
1999 PE.setExpandForMC(true);
2000 PE.setByRef(true);
2001 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
2002 PE.expandSTIPredicate(OS, Fn);
2004 OS << "#endif // GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
2006 OS << "\n#ifdef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n";
2007 OS << "#undef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
2009 std::string ClassPrefix = Target + "MCInstrAnalysis";
2010 PE.setExpandDefinition(true);
2011 PE.setClassPrefix(ClassPrefix);
2012 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
2013 PE.expandSTIPredicate(OS, Fn);
2015 OS << "#endif // GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
2019 // SubtargetEmitter::run - Main subtarget enumeration emitter.
2021 void SubtargetEmitter::run(raw_ostream &OS) {
2022 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
2024 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
2025 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
2027 OS << "namespace llvm {\n";
2028 auto FeatureMap = enumeration(OS);
2029 OS << "} // end namespace llvm\n\n";
2030 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
2032 emitSubtargetInfoMacroCalls(OS);
2034 OS << "namespace llvm {\n";
2035 unsigned NumFeatures = featureKeyValues(OS, FeatureMap);
2036 OS << "\n";
2037 emitSchedModel(OS);
2038 OS << "\n";
2039 unsigned NumProcs = cpuKeyValues(OS, FeatureMap);
2040 OS << "\n";
2041 unsigned NumNames = cpuNames(OS);
2042 OS << "\n";
2044 // MCInstrInfo initialization routine.
2045 emitGenMCSubtargetInfo(OS);
2047 OS << "\nstatic inline MCSubtargetInfo *create" << Target
2048 << "MCSubtargetInfoImpl("
2049 << "const Triple &TT, StringRef CPU, StringRef TuneCPU, StringRef FS) {\n";
2050 if (Target == "AArch64")
2051 OS << " CPU = AArch64::resolveCPUAlias(CPU);\n"
2052 << " TuneCPU = AArch64::resolveCPUAlias(TuneCPU);\n";
2053 OS << " return new " << Target
2054 << "GenMCSubtargetInfo(TT, CPU, TuneCPU, FS, ";
2055 if (NumNames)
2056 OS << Target << "Names, ";
2057 else
2058 OS << "{}, ";
2059 if (NumFeatures)
2060 OS << Target << "FeatureKV, ";
2061 else
2062 OS << "{}, ";
2063 if (NumProcs)
2064 OS << Target << "SubTypeKV, ";
2065 else
2066 OS << "{}, ";
2067 OS << '\n';
2068 OS.indent(22);
2069 OS << Target << "WriteProcResTable, " << Target << "WriteLatencyTable, "
2070 << Target << "ReadAdvanceTable, ";
2071 OS << '\n';
2072 OS.indent(22);
2073 if (SchedModels.hasItineraries()) {
2074 OS << Target << "Stages, " << Target << "OperandCycles, " << Target
2075 << "ForwardingPaths";
2076 } else
2077 OS << "nullptr, nullptr, nullptr";
2078 OS << ");\n}\n\n";
2080 OS << "} // end namespace llvm\n\n";
2082 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
2084 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
2085 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
2087 OS << "#include \"llvm/Support/Debug.h\"\n";
2088 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
2089 if (Target == "AArch64")
2090 OS << "#include \"llvm/TargetParser/AArch64TargetParser.h\"\n\n";
2091 parseFeaturesFunction(OS);
2093 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
2095 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
2096 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
2097 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
2099 std::string ClassName = Target + "GenSubtargetInfo";
2100 OS << "namespace llvm {\n";
2101 OS << "class DFAPacketizer;\n";
2102 OS << "namespace " << Target << "_MC {\n"
2103 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,"
2104 << " const MCInst *MI, const MCInstrInfo *MCII, unsigned CPUID);\n"
2105 << "} // end namespace " << Target << "_MC\n\n";
2106 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
2107 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
2108 << "StringRef TuneCPU, StringRef FS);\n"
2109 << "public:\n"
2110 << " unsigned resolveSchedClass(unsigned SchedClass, "
2111 << " const MachineInstr *DefMI,"
2112 << " const TargetSchedModel *SchedModel) const override;\n"
2113 << " unsigned resolveVariantSchedClass(unsigned SchedClass,"
2114 << " const MCInst *MI, const MCInstrInfo *MCII,"
2115 << " unsigned CPUID) const override;\n"
2116 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
2117 << " const;\n";
2118 if (TGT.getHwModes().getNumModeIds() > 1) {
2119 OS << " unsigned getHwModeSet() const override;\n";
2120 OS << " unsigned getHwMode(enum HwModeType type = HwMode_Default) const "
2121 "override;\n";
2123 if (TGT.hasMacroFusion())
2124 OS << " std::vector<MacroFusionPredTy> getMacroFusions() const "
2125 "override;\n";
2127 STIPredicateExpander PE(Target);
2128 PE.setByRef(false);
2129 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
2130 PE.expandSTIPredicate(OS, Fn);
2132 OS << "};\n"
2133 << "} // end namespace llvm\n\n";
2135 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
2137 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
2138 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
2140 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
2141 OS << "namespace llvm {\n";
2142 OS << "extern const llvm::StringRef " << Target << "Names[];\n";
2143 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
2144 OS << "extern const llvm::SubtargetSubTypeKV " << Target << "SubTypeKV[];\n";
2145 OS << "extern const llvm::MCWriteProcResEntry " << Target
2146 << "WriteProcResTable[];\n";
2147 OS << "extern const llvm::MCWriteLatencyEntry " << Target
2148 << "WriteLatencyTable[];\n";
2149 OS << "extern const llvm::MCReadAdvanceEntry " << Target
2150 << "ReadAdvanceTable[];\n";
2152 if (SchedModels.hasItineraries()) {
2153 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
2154 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
2155 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
2158 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
2159 << "StringRef TuneCPU, StringRef FS)\n";
2161 if (Target == "AArch64")
2162 OS << " : TargetSubtargetInfo(TT, AArch64::resolveCPUAlias(CPU),\n"
2163 << " AArch64::resolveCPUAlias(TuneCPU), FS, ";
2164 else
2165 OS << " : TargetSubtargetInfo(TT, CPU, TuneCPU, FS, ";
2166 if (NumNames)
2167 OS << "ArrayRef(" << Target << "Names, " << NumNames << "), ";
2168 else
2169 OS << "{}, ";
2170 if (NumFeatures)
2171 OS << "ArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
2172 else
2173 OS << "{}, ";
2174 if (NumProcs)
2175 OS << "ArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
2176 else
2177 OS << "{}, ";
2178 OS << '\n';
2179 OS.indent(24);
2180 OS << Target << "WriteProcResTable, " << Target << "WriteLatencyTable, "
2181 << Target << "ReadAdvanceTable, ";
2182 OS << '\n';
2183 OS.indent(24);
2184 if (SchedModels.hasItineraries()) {
2185 OS << Target << "Stages, " << Target << "OperandCycles, " << Target
2186 << "ForwardingPaths";
2187 } else
2188 OS << "nullptr, nullptr, nullptr";
2189 OS << ") {}\n\n";
2191 emitSchedModelHelpers(ClassName, OS);
2192 emitHwModeCheck(ClassName, OS);
2193 emitGetMacroFusions(ClassName, OS);
2195 OS << "} // end namespace llvm\n\n";
2197 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
2199 emitMcInstrAnalysisPredicateFunctions(OS);
2202 static TableGen::Emitter::OptClass<SubtargetEmitter>
2203 X("gen-subtarget", "Generate subtarget enumerations");