1 //===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend emits subtarget enumerations.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "subtarget-emitter"
16 #include "CodeGenTarget.h"
17 #include "CodeGenSchedule.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/MC/MCInstrItineraries.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/TableGen/Error.h"
24 #include "llvm/TableGen/Record.h"
25 #include "llvm/TableGen/TableGenBackend.h"
33 class SubtargetEmitter {
34 // Each processor has a SchedClassDesc table with an entry for each SchedClass.
35 // The SchedClassDesc table indexes into a global write resource table, write
36 // latency table, and read advance table.
37 struct SchedClassTables {
38 std::vector<std::vector<MCSchedClassDesc> > ProcSchedClasses;
39 std::vector<MCWriteProcResEntry> WriteProcResources;
40 std::vector<MCWriteLatencyEntry> WriteLatencies;
41 std::vector<std::string> WriterNames;
42 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
44 // Reserve an invalid entry at index 0
46 ProcSchedClasses.resize(1);
47 WriteProcResources.resize(1);
48 WriteLatencies.resize(1);
49 WriterNames.push_back("InvalidWrite");
50 ReadAdvanceEntries.resize(1);
54 struct LessWriteProcResources {
55 bool operator()(const MCWriteProcResEntry &LHS,
56 const MCWriteProcResEntry &RHS) {
57 return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
61 RecordKeeper &Records;
62 CodeGenSchedModels &SchedModels;
65 void Enumeration(raw_ostream &OS, const char *ClassName, bool isBits);
66 unsigned FeatureKeyValues(raw_ostream &OS);
67 unsigned CPUKeyValues(raw_ostream &OS);
68 void FormItineraryStageString(const std::string &Names,
69 Record *ItinData, std::string &ItinString,
71 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
72 unsigned &NOperandCycles);
73 void FormItineraryBypassString(const std::string &Names,
75 std::string &ItinString, unsigned NOperandCycles);
76 void EmitStageAndOperandCycleData(raw_ostream &OS,
77 std::vector<std::vector<InstrItinerary> >
79 void EmitItineraries(raw_ostream &OS,
80 std::vector<std::vector<InstrItinerary> >
82 void EmitProcessorProp(raw_ostream &OS, const Record *R, const char *Name,
84 void EmitProcessorResources(const CodeGenProcModel &ProcModel,
86 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
87 const CodeGenProcModel &ProcModel);
88 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
89 const CodeGenProcModel &ProcModel);
90 void GenSchedClassTables(const CodeGenProcModel &ProcModel,
91 SchedClassTables &SchedTables);
92 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
93 void EmitProcessorModels(raw_ostream &OS);
94 void EmitProcessorLookup(raw_ostream &OS);
95 void EmitSchedModelHelpers(std::string ClassName, raw_ostream &OS);
96 void EmitSchedModel(raw_ostream &OS);
97 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
101 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT):
102 Records(R), SchedModels(TGT.getSchedModels()), Target(TGT.getName()) {}
104 void run(raw_ostream &o);
107 } // End anonymous namespace
110 // Enumeration - Emit the specified class as an enumeration.
112 void SubtargetEmitter::Enumeration(raw_ostream &OS,
113 const char *ClassName,
115 // Get all records of class and sort
116 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
117 std::sort(DefList.begin(), DefList.end(), LessRecord());
119 unsigned N = DefList.size();
123 errs() << "Too many (> 64) subtarget features!\n";
127 OS << "namespace " << Target << " {\n";
129 // For bit flag enumerations with more than 32 items, emit constants.
130 // Emit an enum for everything else.
131 if (isBits && N > 32) {
133 for (unsigned i = 0; i < N; i++) {
135 Record *Def = DefList[i];
137 // Get and emit name and expression (1 << i)
138 OS << " const uint64_t " << Def->getName() << " = 1ULL << " << i << ";\n";
145 for (unsigned i = 0; i < N;) {
147 Record *Def = DefList[i];
150 OS << " " << Def->getName();
152 // If bit flags then emit expression (1 << i)
153 if (isBits) OS << " = " << " 1ULL << " << i;
155 // Depending on 'if more in the list' emit comma
156 if (++i < N) OS << ",";
169 // FeatureKeyValues - Emit data of all the subtarget features. Used by the
172 unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
173 // Gather and sort all the features
174 std::vector<Record*> FeatureList =
175 Records.getAllDerivedDefinitions("SubtargetFeature");
177 if (FeatureList.empty())
180 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
182 // Begin feature table
183 OS << "// Sorted (by key) array of values for CPU features.\n"
184 << "extern const llvm::SubtargetFeatureKV " << Target
185 << "FeatureKV[] = {\n";
188 unsigned NumFeatures = 0;
189 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
191 Record *Feature = FeatureList[i];
193 const std::string &Name = Feature->getName();
194 const std::string &CommandLineName = Feature->getValueAsString("Name");
195 const std::string &Desc = Feature->getValueAsString("Desc");
197 if (CommandLineName.empty()) continue;
199 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
201 << "\"" << CommandLineName << "\", "
202 << "\"" << Desc << "\", "
203 << Target << "::" << Name << ", ";
205 const std::vector<Record*> &ImpliesList =
206 Feature->getValueAsListOfDefs("Implies");
208 if (ImpliesList.empty()) {
211 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
212 OS << Target << "::" << ImpliesList[j]->getName();
213 if (++j < M) OS << " | ";
220 // Depending on 'if more in the list' emit comma
221 if ((i + 1) < N) OS << ",";
233 // CPUKeyValues - Emit data of all the subtarget processors. Used by command
236 unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
237 // Gather and sort processor information
238 std::vector<Record*> ProcessorList =
239 Records.getAllDerivedDefinitions("Processor");
240 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
242 // Begin processor table
243 OS << "// Sorted (by key) array of values for CPU subtype.\n"
244 << "extern const llvm::SubtargetFeatureKV " << Target
245 << "SubTypeKV[] = {\n";
247 // For each processor
248 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
250 Record *Processor = ProcessorList[i];
252 const std::string &Name = Processor->getValueAsString("Name");
253 const std::vector<Record*> &FeatureList =
254 Processor->getValueAsListOfDefs("Features");
256 // Emit as { "cpu", "description", f1 | f2 | ... fn },
258 << "\"" << Name << "\", "
259 << "\"Select the " << Name << " processor\", ";
261 if (FeatureList.empty()) {
264 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
265 OS << Target << "::" << FeatureList[j]->getName();
266 if (++j < M) OS << " | ";
270 // The "0" is for the "implies" section of this data structure.
273 // Depending on 'if more in the list' emit comma
274 if (++i < N) OS << ",";
279 // End processor table
282 return ProcessorList.size();
286 // FormItineraryStageString - Compose a string containing the stage
287 // data initialization for the specified itinerary. N is the number
290 void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
292 std::string &ItinString,
295 const std::vector<Record*> &StageList =
296 ItinData->getValueAsListOfDefs("Stages");
299 unsigned N = NStages = StageList.size();
300 for (unsigned i = 0; i < N;) {
302 const Record *Stage = StageList[i];
304 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
305 int Cycles = Stage->getValueAsInt("Cycles");
306 ItinString += " { " + itostr(Cycles) + ", ";
309 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
312 for (unsigned j = 0, M = UnitList.size(); j < M;) {
313 // Add name and bitwise or
314 ItinString += Name + "FU::" + UnitList[j]->getName();
315 if (++j < M) ItinString += " | ";
318 int TimeInc = Stage->getValueAsInt("TimeInc");
319 ItinString += ", " + itostr(TimeInc);
321 int Kind = Stage->getValueAsInt("Kind");
322 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
326 if (++i < N) ItinString += ", ";
331 // FormItineraryOperandCycleString - Compose a string containing the
332 // operand cycle initialization for the specified itinerary. N is the
333 // number of operands that has cycles specified.
335 void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
336 std::string &ItinString, unsigned &NOperandCycles) {
337 // Get operand cycle list
338 const std::vector<int64_t> &OperandCycleList =
339 ItinData->getValueAsListOfInts("OperandCycles");
341 // For each operand cycle
342 unsigned N = NOperandCycles = OperandCycleList.size();
343 for (unsigned i = 0; i < N;) {
344 // Next operand cycle
345 const int OCycle = OperandCycleList[i];
347 ItinString += " " + itostr(OCycle);
348 if (++i < N) ItinString += ", ";
352 void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
354 std::string &ItinString,
355 unsigned NOperandCycles) {
356 const std::vector<Record*> &BypassList =
357 ItinData->getValueAsListOfDefs("Bypasses");
358 unsigned N = BypassList.size();
361 ItinString += Name + "Bypass::" + BypassList[i]->getName();
362 if (++i < NOperandCycles) ItinString += ", ";
364 for (; i < NOperandCycles;) {
366 if (++i < NOperandCycles) ItinString += ", ";
371 // EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
372 // cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
373 // by CodeGenSchedClass::Index.
375 void SubtargetEmitter::
376 EmitStageAndOperandCycleData(raw_ostream &OS,
377 std::vector<std::vector<InstrItinerary> >
380 // Multiple processor models may share an itinerary record. Emit it once.
381 SmallPtrSet<Record*, 8> ItinsDefSet;
383 // Emit functional units for all the itineraries.
384 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
385 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
387 if (!ItinsDefSet.insert(PI->ItinsDef))
390 std::vector<Record*> FUs = PI->ItinsDef->getValueAsListOfDefs("FU");
394 const std::string &Name = PI->ItinsDef->getName();
395 OS << "\n// Functional units for \"" << Name << "\"\n"
396 << "namespace " << Name << "FU {\n";
398 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
399 OS << " const unsigned " << FUs[j]->getName()
400 << " = 1 << " << j << ";\n";
404 std::vector<Record*> BPs = PI->ItinsDef->getValueAsListOfDefs("BP");
406 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
407 << "\"\n" << "namespace " << Name << "Bypass {\n";
409 OS << " const unsigned NoBypass = 0;\n";
410 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
411 OS << " const unsigned " << BPs[j]->getName()
412 << " = 1 << " << j << ";\n";
418 // Begin stages table
419 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
421 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
423 // Begin operand cycle table
424 std::string OperandCycleTable = "extern const unsigned " + Target +
425 "OperandCycles[] = {\n";
426 OperandCycleTable += " 0, // No itinerary\n";
428 // Begin pipeline bypass table
429 std::string BypassTable = "extern const unsigned " + Target +
430 "ForwardingPaths[] = {\n";
431 BypassTable += " 0, // No itinerary\n";
433 // For each Itinerary across all processors, add a unique entry to the stages,
434 // operand cycles, and pipepine bypess tables. Then add the new Itinerary
435 // object with computed offsets to the ProcItinLists result.
436 unsigned StageCount = 1, OperandCycleCount = 1;
437 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
438 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
439 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
440 const CodeGenProcModel &ProcModel = *PI;
442 // Add process itinerary to the list.
443 ProcItinLists.resize(ProcItinLists.size()+1);
445 // If this processor defines no itineraries, then leave the itinerary list
447 std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
448 if (ProcModel.ItinDefList.empty())
451 // Reserve index==0 for NoItinerary.
452 ItinList.resize(SchedModels.numItineraryClasses()+1);
454 const std::string &Name = ProcModel.ItinsDef->getName();
456 // For each itinerary data
457 for (unsigned SchedClassIdx = 0,
458 SchedClassEnd = ProcModel.ItinDefList.size();
459 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
461 // Next itinerary data
462 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
464 // Get string and stage count
465 std::string ItinStageString;
466 unsigned NStages = 0;
468 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
470 // Get string and operand cycle count
471 std::string ItinOperandCycleString;
472 unsigned NOperandCycles = 0;
473 std::string ItinBypassString;
475 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
478 FormItineraryBypassString(Name, ItinData, ItinBypassString,
482 // Check to see if stage already exists and create if it doesn't
483 unsigned FindStage = 0;
485 FindStage = ItinStageMap[ItinStageString];
486 if (FindStage == 0) {
487 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
488 StageTable += ItinStageString + ", // " + itostr(StageCount);
490 StageTable += "-" + itostr(StageCount + NStages - 1);
492 // Record Itin class number.
493 ItinStageMap[ItinStageString] = FindStage = StageCount;
494 StageCount += NStages;
498 // Check to see if operand cycle already exists and create if it doesn't
499 unsigned FindOperandCycle = 0;
500 if (NOperandCycles > 0) {
501 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
502 FindOperandCycle = ItinOperandMap[ItinOperandString];
503 if (FindOperandCycle == 0) {
504 // Emit as cycle, // index
505 OperandCycleTable += ItinOperandCycleString + ", // ";
506 std::string OperandIdxComment = itostr(OperandCycleCount);
507 if (NOperandCycles > 1)
508 OperandIdxComment += "-"
509 + itostr(OperandCycleCount + NOperandCycles - 1);
510 OperandCycleTable += OperandIdxComment + "\n";
511 // Record Itin class number.
512 ItinOperandMap[ItinOperandCycleString] =
513 FindOperandCycle = OperandCycleCount;
514 // Emit as bypass, // index
515 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
516 OperandCycleCount += NOperandCycles;
520 // Set up itinerary as location and location + stage count
521 int NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
522 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
524 FindOperandCycle + NOperandCycles};
526 // Inject - empty slots will be 0, 0
527 ItinList[SchedClassIdx] = Intinerary;
532 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
533 StageTable += "};\n";
535 // Closing operand cycles
536 OperandCycleTable += " 0 // End operand cycles\n";
537 OperandCycleTable += "};\n";
539 BypassTable += " 0 // End bypass tables\n";
540 BypassTable += "};\n";
544 OS << OperandCycleTable;
549 // EmitProcessorData - Generate data for processor itineraries that were
550 // computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
551 // Itineraries for each processor. The Itinerary lists are indexed on
552 // CodeGenSchedClass::Index.
554 void SubtargetEmitter::
555 EmitItineraries(raw_ostream &OS,
556 std::vector<std::vector<InstrItinerary> > &ProcItinLists) {
558 // Multiple processor models may share an itinerary record. Emit it once.
559 SmallPtrSet<Record*, 8> ItinsDefSet;
561 // For each processor's machine model
562 std::vector<std::vector<InstrItinerary> >::iterator
563 ProcItinListsIter = ProcItinLists.begin();
564 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
565 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
567 Record *ItinsDef = PI->ItinsDef;
568 if (!ItinsDefSet.insert(ItinsDef))
571 // Get processor itinerary name
572 const std::string &Name = ItinsDef->getName();
574 // Get the itinerary list for the processor.
575 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
576 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
579 OS << "static const llvm::InstrItinerary ";
580 if (ItinList.empty()) {
581 OS << '*' << Name << " = 0;\n";
585 // Begin processor itinerary table
586 OS << Name << "[] = {\n";
588 // For each itinerary class in CodeGenSchedClass::Index order.
589 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
590 InstrItinerary &Intinerary = ItinList[j];
592 // Emit Itinerary in the form of
593 // { firstStage, lastStage, firstCycle, lastCycle } // index
595 Intinerary.NumMicroOps << ", " <<
596 Intinerary.FirstStage << ", " <<
597 Intinerary.LastStage << ", " <<
598 Intinerary.FirstOperandCycle << ", " <<
599 Intinerary.LastOperandCycle << " }" <<
600 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
602 // End processor itinerary table
603 OS << " { 0, ~0U, ~0U, ~0U, ~0U } // end marker\n";
608 // Emit either the value defined in the TableGen Record, or the default
609 // value defined in the C++ header. The Record is null if the processor does not
611 void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
612 const char *Name, char Separator) {
614 int V = R ? R->getValueAsInt(Name) : -1;
616 OS << V << Separator << " // " << Name;
618 OS << "MCSchedModel::Default" << Name << Separator;
622 void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
624 char Sep = ProcModel.ProcResourceDefs.empty() ? ' ' : ',';
626 OS << "\n// {Name, NumUnits, SuperIdx, IsBuffered}\n";
627 OS << "static const llvm::MCProcResourceDesc "
628 << ProcModel.ModelName << "ProcResources" << "[] = {\n"
629 << " {DBGFIELD(\"InvalidUnit\") 0, 0, 0}" << Sep << "\n";
631 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
632 Record *PRDef = ProcModel.ProcResourceDefs[i];
635 unsigned SuperIdx = 0;
636 Record *SuperDef = 0;
637 if (PRDef->getValueInit("Super")->isComplete()) {
639 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"), ProcModel);
640 SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
642 // Emit the ProcResourceDesc
645 OS << " {DBGFIELD(\"" << PRDef->getName() << "\") ";
646 if (PRDef->getName().size() < 15)
647 OS.indent(15 - PRDef->getName().size());
648 OS << PRDef->getValueAsInt("NumUnits") << ", " << SuperIdx << ", "
649 << PRDef->getValueAsBit("Buffered") << "}" << Sep << " // #" << i+1;
651 OS << ", Super=" << SuperDef->getName();
657 // Find the WriteRes Record that defines processor resources for this
659 Record *SubtargetEmitter::FindWriteResources(
660 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
662 // Check if the SchedWrite is already subtarget-specific and directly
663 // specifies a set of processor resources.
664 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
665 return SchedWrite.TheDef;
667 Record *AliasDef = 0;
668 for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
670 const CodeGenSchedRW &AliasRW =
671 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
672 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
673 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
674 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
678 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
679 "defined for processor " + ProcModel.ModelName +
680 " Ensure only one SchedAlias exists per RW.");
681 AliasDef = AliasRW.TheDef;
683 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
686 // Check this processor's list of write resources.
688 for (RecIter WRI = ProcModel.WriteResDefs.begin(),
689 WRE = ProcModel.WriteResDefs.end(); WRI != WRE; ++WRI) {
690 if (!(*WRI)->isSubClassOf("WriteRes"))
692 if (AliasDef == (*WRI)->getValueAsDef("WriteType")
693 || SchedWrite.TheDef == (*WRI)->getValueAsDef("WriteType")) {
695 PrintFatalError((*WRI)->getLoc(), "Resources are defined for both "
696 "SchedWrite and its alias on processor " +
697 ProcModel.ModelName);
702 // TODO: If ProcModel has a base model (previous generation processor),
703 // then call FindWriteResources recursively with that model here.
705 PrintFatalError(ProcModel.ModelDef->getLoc(),
706 std::string("Processor does not define resources for ")
707 + SchedWrite.TheDef->getName());
712 /// Find the ReadAdvance record for the given SchedRead on this processor or
714 Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
715 const CodeGenProcModel &ProcModel) {
716 // Check for SchedReads that directly specify a ReadAdvance.
717 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
718 return SchedRead.TheDef;
720 // Check this processor's list of aliases for SchedRead.
721 Record *AliasDef = 0;
722 for (RecIter AI = SchedRead.Aliases.begin(), AE = SchedRead.Aliases.end();
724 const CodeGenSchedRW &AliasRW =
725 SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
726 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
727 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
728 if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
732 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
733 "defined for processor " + ProcModel.ModelName +
734 " Ensure only one SchedAlias exists per RW.");
735 AliasDef = AliasRW.TheDef;
737 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
740 // Check this processor's ReadAdvanceList.
742 for (RecIter RAI = ProcModel.ReadAdvanceDefs.begin(),
743 RAE = ProcModel.ReadAdvanceDefs.end(); RAI != RAE; ++RAI) {
744 if (!(*RAI)->isSubClassOf("ReadAdvance"))
746 if (AliasDef == (*RAI)->getValueAsDef("ReadType")
747 || SchedRead.TheDef == (*RAI)->getValueAsDef("ReadType")) {
749 PrintFatalError((*RAI)->getLoc(), "Resources are defined for both "
750 "SchedRead and its alias on processor " +
751 ProcModel.ModelName);
756 // TODO: If ProcModel has a base model (previous generation processor),
757 // then call FindReadAdvance recursively with that model here.
758 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
759 PrintFatalError(ProcModel.ModelDef->getLoc(),
760 std::string("Processor does not define resources for ")
761 + SchedRead.TheDef->getName());
766 // Generate the SchedClass table for this processor and update global
767 // tables. Must be called for each processor in order.
768 void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
769 SchedClassTables &SchedTables) {
770 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
771 if (!ProcModel.hasInstrSchedModel())
774 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
775 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
776 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
777 DEBUG(SCI->dump(&SchedModels));
779 SCTab.resize(SCTab.size() + 1);
780 MCSchedClassDesc &SCDesc = SCTab.back();
781 // SCDesc.Name is guarded by NDEBUG
782 SCDesc.NumMicroOps = 0;
783 SCDesc.BeginGroup = false;
784 SCDesc.EndGroup = false;
785 SCDesc.WriteProcResIdx = 0;
786 SCDesc.WriteLatencyIdx = 0;
787 SCDesc.ReadAdvanceIdx = 0;
789 // A Variant SchedClass has no resources of its own.
790 if (!SCI->Transitions.empty()) {
791 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
795 // Determine if the SchedClass is actually reachable on this processor. If
796 // not don't try to locate the processor resources, it will fail.
797 // If ProcIndices contains 0, this class applies to all processors.
798 assert(!SCI->ProcIndices.empty() && "expect at least one procidx");
799 if (SCI->ProcIndices[0] != 0) {
800 IdxIter PIPos = std::find(SCI->ProcIndices.begin(),
801 SCI->ProcIndices.end(), ProcModel.Index);
802 if (PIPos == SCI->ProcIndices.end())
805 IdxVec Writes = SCI->Writes;
806 IdxVec Reads = SCI->Reads;
807 if (SCI->ItinClassDef) {
808 assert(SCI->InstRWs.empty() && "ItinClass should not have InstRWs");
809 // Check this processor's itinerary class resources.
810 for (RecIter II = ProcModel.ItinRWDefs.begin(),
811 IE = ProcModel.ItinRWDefs.end(); II != IE; ++II) {
812 RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
813 if (std::find(Matched.begin(), Matched.end(), SCI->ItinClassDef)
815 SchedModels.findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"),
820 if (Writes.empty()) {
821 DEBUG(dbgs() << ProcModel.ItinsDef->getName()
822 << " does not have resources for itinerary class "
823 << SCI->ItinClassDef->getName() << '\n');
826 else if (!SCI->InstRWs.empty()) {
827 // This class may have a default ReadWrite list which can be overriden by
828 // InstRW definitions.
830 for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
832 Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
833 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
841 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
845 // Sum resources across all operand writes.
846 std::vector<MCWriteProcResEntry> WriteProcResources;
847 std::vector<MCWriteLatencyEntry> WriteLatencies;
848 std::vector<std::string> WriterNames;
849 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
850 for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI) {
852 SchedModels.expandRWSeqForProc(*WI, WriteSeq, /*IsRead=*/false,
855 // For each operand, create a latency entry.
856 MCWriteLatencyEntry WLEntry;
858 unsigned WriteID = WriteSeq.back();
859 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
860 // If this Write is not referenced by a ReadAdvance, don't distinguish it
861 // from other WriteLatency entries.
862 if (!SchedModels.hasReadOfWrite(SchedModels.getSchedWrite(WriteID).TheDef)) {
865 WLEntry.WriteResourceID = WriteID;
867 for (IdxIter WSI = WriteSeq.begin(), WSE = WriteSeq.end();
871 FindWriteResources(SchedModels.getSchedWrite(*WSI), ProcModel);
873 // Mark the parent class as invalid for unsupported write types.
874 if (WriteRes->getValueAsBit("Unsupported")) {
875 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
878 WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
879 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
880 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
881 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
883 // Create an entry for each ProcResource listed in WriteRes.
884 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
885 std::vector<int64_t> Cycles =
886 WriteRes->getValueAsListOfInts("ResourceCycles");
887 for (unsigned PRIdx = 0, PREnd = PRVec.size();
888 PRIdx != PREnd; ++PRIdx) {
889 MCWriteProcResEntry WPREntry;
890 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
891 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
892 if (Cycles.size() > PRIdx)
893 WPREntry.Cycles = Cycles[PRIdx];
896 WriteProcResources.push_back(WPREntry);
899 WriteLatencies.push_back(WLEntry);
901 // Create an entry for each operand Read in this SchedClass.
902 // Entries must be sorted first by UseIdx then by WriteResourceID.
903 for (unsigned UseIdx = 0, EndIdx = Reads.size();
904 UseIdx != EndIdx; ++UseIdx) {
905 Record *ReadAdvance =
906 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
910 // Mark the parent class as invalid for unsupported write types.
911 if (ReadAdvance->getValueAsBit("Unsupported")) {
912 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
915 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
917 if (ValidWrites.empty())
918 WriteIDs.push_back(0);
920 for (RecIter VWI = ValidWrites.begin(), VWE = ValidWrites.end();
922 WriteIDs.push_back(SchedModels.getSchedRWIdx(*VWI, /*IsRead=*/false));
925 std::sort(WriteIDs.begin(), WriteIDs.end());
926 for(IdxIter WI = WriteIDs.begin(), WE = WriteIDs.end(); WI != WE; ++WI) {
927 MCReadAdvanceEntry RAEntry;
928 RAEntry.UseIdx = UseIdx;
929 RAEntry.WriteResourceID = *WI;
930 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
931 ReadAdvanceEntries.push_back(RAEntry);
934 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
935 WriteProcResources.clear();
936 WriteLatencies.clear();
937 ReadAdvanceEntries.clear();
939 // Add the information for this SchedClass to the global tables using basic
942 // WritePrecRes entries are sorted by ProcResIdx.
943 std::sort(WriteProcResources.begin(), WriteProcResources.end(),
944 LessWriteProcResources());
946 SCDesc.NumWriteProcResEntries = WriteProcResources.size();
947 std::vector<MCWriteProcResEntry>::iterator WPRPos =
948 std::search(SchedTables.WriteProcResources.begin(),
949 SchedTables.WriteProcResources.end(),
950 WriteProcResources.begin(), WriteProcResources.end());
951 if (WPRPos != SchedTables.WriteProcResources.end())
952 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
954 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
955 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
956 WriteProcResources.end());
958 // Latency entries must remain in operand order.
959 SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
960 std::vector<MCWriteLatencyEntry>::iterator WLPos =
961 std::search(SchedTables.WriteLatencies.begin(),
962 SchedTables.WriteLatencies.end(),
963 WriteLatencies.begin(), WriteLatencies.end());
964 if (WLPos != SchedTables.WriteLatencies.end()) {
965 unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
966 SCDesc.WriteLatencyIdx = idx;
967 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
968 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
970 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
974 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
975 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(),
976 WriteLatencies.begin(),
977 WriteLatencies.end());
978 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(),
979 WriterNames.begin(), WriterNames.end());
981 // ReadAdvanceEntries must remain in operand order.
982 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
983 std::vector<MCReadAdvanceEntry>::iterator RAPos =
984 std::search(SchedTables.ReadAdvanceEntries.begin(),
985 SchedTables.ReadAdvanceEntries.end(),
986 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
987 if (RAPos != SchedTables.ReadAdvanceEntries.end())
988 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
990 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
991 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(),
992 ReadAdvanceEntries.end());
997 // Emit SchedClass tables for all processors and associated global tables.
998 void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1000 // Emit global WriteProcResTable.
1001 OS << "\n// {ProcResourceIdx, Cycles}\n"
1002 << "extern const llvm::MCWriteProcResEntry "
1003 << Target << "WriteProcResTable[] = {\n"
1004 << " { 0, 0}, // Invalid\n";
1005 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1006 WPRIdx != WPREnd; ++WPRIdx) {
1007 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1008 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1009 << format("%2d", WPREntry.Cycles) << "}";
1010 if (WPRIdx + 1 < WPREnd)
1012 OS << " // #" << WPRIdx << '\n';
1014 OS << "}; // " << Target << "WriteProcResTable\n";
1016 // Emit global WriteLatencyTable.
1017 OS << "\n// {Cycles, WriteResourceID}\n"
1018 << "extern const llvm::MCWriteLatencyEntry "
1019 << Target << "WriteLatencyTable[] = {\n"
1020 << " { 0, 0}, // Invalid\n";
1021 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1022 WLIdx != WLEnd; ++WLIdx) {
1023 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1024 OS << " {" << format("%2d", WLEntry.Cycles) << ", "
1025 << format("%2d", WLEntry.WriteResourceID) << "}";
1026 if (WLIdx + 1 < WLEnd)
1028 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
1030 OS << "}; // " << Target << "WriteLatencyTable\n";
1032 // Emit global ReadAdvanceTable.
1033 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1034 << "extern const llvm::MCReadAdvanceEntry "
1035 << Target << "ReadAdvanceTable[] = {\n"
1036 << " {0, 0, 0}, // Invalid\n";
1037 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1038 RAIdx != RAEnd; ++RAIdx) {
1039 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1040 OS << " {" << RAEntry.UseIdx << ", "
1041 << format("%2d", RAEntry.WriteResourceID) << ", "
1042 << format("%2d", RAEntry.Cycles) << "}";
1043 if (RAIdx + 1 < RAEnd)
1045 OS << " // #" << RAIdx << '\n';
1047 OS << "}; // " << Target << "ReadAdvanceTable\n";
1049 // Emit a SchedClass table for each processor.
1050 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1051 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1052 if (!PI->hasInstrSchedModel())
1055 std::vector<MCSchedClassDesc> &SCTab =
1056 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
1058 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1059 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1060 OS << "static const llvm::MCSchedClassDesc "
1061 << PI->ModelName << "SchedClasses[] = {\n";
1063 // The first class is always invalid. We no way to distinguish it except by
1064 // name and position.
1065 assert(SchedModels.getSchedClass(0).Name == "NoItinerary"
1066 && "invalid class not first");
1067 OS << " {DBGFIELD(\"InvalidSchedClass\") "
1068 << MCSchedClassDesc::InvalidNumMicroOps
1069 << ", 0, 0, 0, 0, 0, 0, 0, 0},\n";
1071 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1072 MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1073 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1074 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") ";
1075 if (SchedClass.Name.size() < 18)
1076 OS.indent(18 - SchedClass.Name.size());
1077 OS << MCDesc.NumMicroOps
1078 << ", " << MCDesc.BeginGroup << ", " << MCDesc.EndGroup
1079 << ", " << format("%2d", MCDesc.WriteProcResIdx)
1080 << ", " << MCDesc.NumWriteProcResEntries
1081 << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1082 << ", " << MCDesc.NumWriteLatencyEntries
1083 << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
1084 << ", " << MCDesc.NumReadAdvanceEntries << "}";
1085 if (SCIdx + 1 < SCEnd)
1087 OS << " // #" << SCIdx << '\n';
1089 OS << "}; // " << PI->ModelName << "SchedClasses\n";
1093 void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1094 // For each processor model.
1095 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1096 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1097 // Emit processor resource table.
1098 if (PI->hasInstrSchedModel())
1099 EmitProcessorResources(*PI, OS);
1100 else if(!PI->ProcResourceDefs.empty())
1101 PrintFatalError(PI->ModelDef->getLoc(), "SchedMachineModel defines "
1102 "ProcResources without defining WriteRes SchedWriteRes");
1104 // Begin processor itinerary properties
1106 OS << "static const llvm::MCSchedModel " << PI->ModelName << "(\n";
1107 EmitProcessorProp(OS, PI->ModelDef, "IssueWidth", ',');
1108 EmitProcessorProp(OS, PI->ModelDef, "MinLatency", ',');
1109 EmitProcessorProp(OS, PI->ModelDef, "LoadLatency", ',');
1110 EmitProcessorProp(OS, PI->ModelDef, "HighLatency", ',');
1111 EmitProcessorProp(OS, PI->ModelDef, "MispredictPenalty", ',');
1112 OS << " " << PI->Index << ", // Processor ID\n";
1113 if (PI->hasInstrSchedModel())
1114 OS << " " << PI->ModelName << "ProcResources" << ",\n"
1115 << " " << PI->ModelName << "SchedClasses" << ",\n"
1116 << " " << PI->ProcResourceDefs.size()+1 << ",\n"
1117 << " " << (SchedModels.schedClassEnd()
1118 - SchedModels.schedClassBegin()) << ",\n";
1120 OS << " 0, 0, 0, 0, // No instruction-level machine model.\n";
1121 if (SchedModels.hasItineraryClasses())
1122 OS << " " << PI->ItinsDef->getName() << ");\n";
1124 OS << " 0); // No Itinerary\n";
1129 // EmitProcessorLookup - generate cpu name to itinerary lookup table.
1131 void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
1132 // Gather and sort processor information
1133 std::vector<Record*> ProcessorList =
1134 Records.getAllDerivedDefinitions("Processor");
1135 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
1137 // Begin processor table
1139 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
1140 << "extern const llvm::SubtargetInfoKV "
1141 << Target << "ProcSchedKV[] = {\n";
1143 // For each processor
1144 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
1146 Record *Processor = ProcessorList[i];
1148 const std::string &Name = Processor->getValueAsString("Name");
1149 const std::string &ProcModelName =
1150 SchedModels.getModelForProc(Processor).ModelName;
1152 // Emit as { "cpu", procinit },
1153 OS << " { \"" << Name << "\", (const void *)&" << ProcModelName << " }";
1155 // Depending on ''if more in the list'' emit comma
1156 if (++i < N) OS << ",";
1161 // End processor table
1166 // EmitSchedModel - Emits all scheduling model tables, folding common patterns.
1168 void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
1169 OS << "#ifdef DBGFIELD\n"
1170 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1172 << "#ifndef NDEBUG\n"
1173 << "#define DBGFIELD(x) x,\n"
1175 << "#define DBGFIELD(x)\n"
1178 if (SchedModels.hasItineraryClasses()) {
1179 std::vector<std::vector<InstrItinerary> > ProcItinLists;
1180 // Emit the stage data
1181 EmitStageAndOperandCycleData(OS, ProcItinLists);
1182 EmitItineraries(OS, ProcItinLists);
1184 OS << "\n// ===============================================================\n"
1185 << "// Data tables for the new per-operand machine model.\n";
1187 SchedClassTables SchedTables;
1188 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1189 PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1190 GenSchedClassTables(*PI, SchedTables);
1192 EmitSchedClassTables(SchedTables, OS);
1194 // Emit the processor machine model
1195 EmitProcessorModels(OS);
1196 // Emit the processor lookup data
1197 EmitProcessorLookup(OS);
1199 OS << "#undef DBGFIELD";
1202 void SubtargetEmitter::EmitSchedModelHelpers(std::string ClassName,
1204 OS << "unsigned " << ClassName
1205 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1206 << " const TargetSchedModel *SchedModel) const {\n";
1208 std::vector<Record*> Prologs = Records.getAllDerivedDefinitions("PredicateProlog");
1209 std::sort(Prologs.begin(), Prologs.end(), LessRecord());
1210 for (std::vector<Record*>::const_iterator
1211 PI = Prologs.begin(), PE = Prologs.end(); PI != PE; ++PI) {
1212 OS << (*PI)->getValueAsString("Code") << '\n';
1214 IdxVec VariantClasses;
1215 for (CodeGenSchedModels::SchedClassIter SCI = SchedModels.schedClassBegin(),
1216 SCE = SchedModels.schedClassEnd(); SCI != SCE; ++SCI) {
1217 if (SCI->Transitions.empty())
1219 VariantClasses.push_back(SCI - SchedModels.schedClassBegin());
1221 if (!VariantClasses.empty()) {
1222 OS << " switch (SchedClass) {\n";
1223 for (IdxIter VCI = VariantClasses.begin(), VCE = VariantClasses.end();
1224 VCI != VCE; ++VCI) {
1225 const CodeGenSchedClass &SC = SchedModels.getSchedClass(*VCI);
1226 OS << " case " << *VCI << ": // " << SC.Name << '\n';
1228 for (std::vector<CodeGenSchedTransition>::const_iterator
1229 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1232 std::set_union(TI->ProcIndices.begin(), TI->ProcIndices.end(),
1233 ProcIndices.begin(), ProcIndices.end(),
1234 std::back_inserter(PI));
1235 ProcIndices.swap(PI);
1237 for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end();
1241 OS << "if (SchedModel->getProcessorID() == " << *PI << ") ";
1242 OS << "{ // " << (SchedModels.procModelBegin() + *PI)->ModelName
1244 for (std::vector<CodeGenSchedTransition>::const_iterator
1245 TI = SC.Transitions.begin(), TE = SC.Transitions.end();
1248 if (*PI != 0 && !std::count(TI->ProcIndices.begin(),
1249 TI->ProcIndices.end(), *PI)) {
1252 for (RecIter RI = TI->PredTerm.begin(), RE = TI->PredTerm.end();
1254 if (RI != TI->PredTerm.begin())
1256 OS << "(" << (*RI)->getValueAsString("Predicate") << ")";
1259 << " return " << TI->ToClassIdx << "; // "
1260 << SchedModels.getSchedClass(TI->ToClassIdx).Name << '\n';
1267 if (SC.ItinClassDef)
1268 SCIdx = SchedModels.getSchedClassIdxForItin(SC.ItinClassDef);
1270 SCIdx = SchedModels.findSchedClassIdx(SC.Writes, SC.Reads);
1272 OS << " return " << SCIdx << ";\n";
1277 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"
1278 << "} // " << ClassName << "::resolveSchedClass\n";
1282 // ParseFeaturesFunction - Produces a subtarget specific function for parsing
1283 // the subtarget features string.
1285 void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1286 unsigned NumFeatures,
1287 unsigned NumProcs) {
1288 std::vector<Record*> Features =
1289 Records.getAllDerivedDefinitions("SubtargetFeature");
1290 std::sort(Features.begin(), Features.end(), LessRecord());
1292 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1293 << "// subtarget options.\n"
1296 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
1297 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
1298 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU << \"\\n\\n\");\n";
1300 if (Features.empty()) {
1305 OS << " InitMCProcessorInfo(CPU, FS);\n"
1306 << " uint64_t Bits = getFeatureBits();\n";
1308 for (unsigned i = 0; i < Features.size(); i++) {
1310 Record *R = Features[i];
1311 const std::string &Instance = R->getName();
1312 const std::string &Value = R->getValueAsString("Value");
1313 const std::string &Attribute = R->getValueAsString("Attribute");
1315 if (Value=="true" || Value=="false")
1316 OS << " if ((Bits & " << Target << "::"
1317 << Instance << ") != 0) "
1318 << Attribute << " = " << Value << ";\n";
1320 OS << " if ((Bits & " << Target << "::"
1321 << Instance << ") != 0 && "
1322 << Attribute << " < " << Value << ") "
1323 << Attribute << " = " << Value << ";\n";
1330 // SubtargetEmitter::run - Main subtarget enumeration emitter.
1332 void SubtargetEmitter::run(raw_ostream &OS) {
1333 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
1335 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
1336 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
1338 OS << "namespace llvm {\n";
1339 Enumeration(OS, "SubtargetFeature", true);
1340 OS << "} // End llvm namespace \n";
1341 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1343 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
1344 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
1346 OS << "namespace llvm {\n";
1348 OS << "namespace {\n";
1350 unsigned NumFeatures = FeatureKeyValues(OS);
1352 unsigned NumProcs = CPUKeyValues(OS);
1360 // MCInstrInfo initialization routine.
1361 OS << "static inline void Init" << Target
1362 << "MCSubtargetInfo(MCSubtargetInfo *II, "
1363 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
1364 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
1366 OS << Target << "FeatureKV, ";
1370 OS << Target << "SubTypeKV, ";
1373 OS << '\n'; OS.indent(22);
1374 OS << Target << "ProcSchedKV, "
1375 << Target << "WriteProcResTable, "
1376 << Target << "WriteLatencyTable, "
1377 << Target << "ReadAdvanceTable, ";
1378 if (SchedModels.hasItineraryClasses()) {
1379 OS << '\n'; OS.indent(22);
1380 OS << Target << "Stages, "
1381 << Target << "OperandCycles, "
1382 << Target << "ForwardingPaths, ";
1385 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
1387 OS << "} // End llvm namespace \n";
1389 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1391 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
1392 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
1394 OS << "#include \"llvm/Support/Debug.h\"\n";
1395 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
1396 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1398 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1400 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
1401 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
1402 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
1404 std::string ClassName = Target + "GenSubtargetInfo";
1405 OS << "namespace llvm {\n";
1406 OS << "class DFAPacketizer;\n";
1407 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
1408 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
1409 << "StringRef FS);\n"
1411 << " unsigned resolveSchedClass(unsigned SchedClass, const MachineInstr *DefMI,"
1412 << " const TargetSchedModel *SchedModel) const;\n"
1413 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
1416 OS << "} // End llvm namespace \n";
1418 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1420 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
1421 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
1423 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n";
1424 OS << "namespace llvm {\n";
1425 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1426 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
1427 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcSchedKV[];\n";
1428 OS << "extern const llvm::MCWriteProcResEntry "
1429 << Target << "WriteProcResTable[];\n";
1430 OS << "extern const llvm::MCWriteLatencyEntry "
1431 << Target << "WriteLatencyTable[];\n";
1432 OS << "extern const llvm::MCReadAdvanceEntry "
1433 << Target << "ReadAdvanceTable[];\n";
1435 if (SchedModels.hasItineraryClasses()) {
1436 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1437 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
1438 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
1441 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
1442 << "StringRef FS)\n"
1443 << " : TargetSubtargetInfo() {\n"
1444 << " InitMCSubtargetInfo(TT, CPU, FS, ";
1446 OS << Target << "FeatureKV, ";
1450 OS << Target << "SubTypeKV, ";
1453 OS << '\n'; OS.indent(22);
1454 OS << Target << "ProcSchedKV, "
1455 << Target << "WriteProcResTable, "
1456 << Target << "WriteLatencyTable, "
1457 << Target << "ReadAdvanceTable, ";
1458 OS << '\n'; OS.indent(22);
1459 if (SchedModels.hasItineraryClasses()) {
1460 OS << Target << "Stages, "
1461 << Target << "OperandCycles, "
1462 << Target << "ForwardingPaths, ";
1465 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
1467 EmitSchedModelHelpers(ClassName, OS);
1469 OS << "} // End llvm namespace \n";
1471 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
1476 void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
1477 CodeGenTarget CGTarget(RK);
1478 SubtargetEmitter(RK, CGTarget).run(OS);
1481 } // End llvm namespace