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 #include "SubtargetEmitter.h"
15 #include "CodeGenTarget.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Support/Debug.h"
23 // Enumeration - Emit the specified class as an enumeration.
25 void SubtargetEmitter::Enumeration(raw_ostream &OS,
26 const char *ClassName,
28 // Get all records of class and sort
29 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
30 std::sort(DefList.begin(), DefList.end(), LessRecord());
36 for (unsigned i = 0, N = DefList.size(); i < N;) {
38 Record *Def = DefList[i];
41 OS << " " << Def->getName();
43 // If bit flags then emit expression (1 << i)
44 if (isBits) OS << " = " << " 1 << " << i;
46 // Depending on 'if more in the list' emit comma
47 if (++i < N) OS << ",";
57 // FeatureKeyValues - Emit data of all the subtarget features. Used by the
60 void SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
61 // Gather and sort all the features
62 std::vector<Record*> FeatureList =
63 Records.getAllDerivedDefinitions("SubtargetFeature");
64 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
66 // Begin feature table
67 OS << "// Sorted (by key) array of values for CPU features.\n"
68 << "static const llvm::SubtargetFeatureKV FeatureKV[] = {\n";
71 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
73 Record *Feature = FeatureList[i];
75 const std::string &Name = Feature->getName();
76 const std::string &CommandLineName = Feature->getValueAsString("Name");
77 const std::string &Desc = Feature->getValueAsString("Desc");
79 if (CommandLineName.empty()) continue;
81 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
83 << "\"" << CommandLineName << "\", "
84 << "\"" << Desc << "\", "
87 const std::vector<Record*> &ImpliesList =
88 Feature->getValueAsListOfDefs("Implies");
90 if (ImpliesList.empty()) {
93 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
94 OS << ImpliesList[j]->getName();
95 if (++j < M) OS << " | ";
101 // Depending on 'if more in the list' emit comma
102 if ((i + 1) < N) OS << ",";
110 // Emit size of table
112 OS<<" FeatureKVSize = sizeof(FeatureKV)/sizeof(llvm::SubtargetFeatureKV)\n";
117 // CPUKeyValues - Emit data of all the subtarget processors. Used by command
120 void SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
121 // Gather and sort processor information
122 std::vector<Record*> ProcessorList =
123 Records.getAllDerivedDefinitions("Processor");
124 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
126 // Begin processor table
127 OS << "// Sorted (by key) array of values for CPU subtype.\n"
128 << "static const llvm::SubtargetFeatureKV SubTypeKV[] = {\n";
130 // For each processor
131 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
133 Record *Processor = ProcessorList[i];
135 const std::string &Name = Processor->getValueAsString("Name");
136 const std::vector<Record*> &FeatureList =
137 Processor->getValueAsListOfDefs("Features");
139 // Emit as { "cpu", "description", f1 | f2 | ... fn },
141 << "\"" << Name << "\", "
142 << "\"Select the " << Name << " processor\", ";
144 if (FeatureList.empty()) {
147 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
148 OS << FeatureList[j]->getName();
149 if (++j < M) OS << " | ";
153 // The "0" is for the "implies" section of this data structure.
156 // Depending on 'if more in the list' emit comma
157 if (++i < N) OS << ",";
162 // End processor table
165 // Emit size of table
167 OS<<" SubTypeKVSize = sizeof(SubTypeKV)/sizeof(llvm::SubtargetFeatureKV)\n";
172 // CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
173 // Returns itinerary class count.
175 unsigned SubtargetEmitter::
176 CollectAllItinClasses(raw_ostream &OS,
177 std::map<std::string, unsigned> &ItinClassesMap,
178 std::vector<Record*> &ItinClassList) {
179 // For each itinerary class
180 unsigned N = ItinClassList.size();
181 for (unsigned i = 0; i < N; i++) {
182 // Next itinerary class
183 const Record *ItinClass = ItinClassList[i];
184 // Get name of itinerary class
185 // Assign itinerary class a unique number
186 ItinClassesMap[ItinClass->getName()] = i;
189 // Emit size of table
191 OS<<" ItinClassesSize = " << N << "\n";
194 // Return itinerary class count
199 // FormItineraryStageString - Compose a string containing the stage
200 // data initialization for the specified itinerary. N is the number
203 void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
205 std::string &ItinString,
208 const std::vector<Record*> &StageList =
209 ItinData->getValueAsListOfDefs("Stages");
212 unsigned N = NStages = StageList.size();
213 for (unsigned i = 0; i < N;) {
215 const Record *Stage = StageList[i];
217 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
218 int Cycles = Stage->getValueAsInt("Cycles");
219 ItinString += " { " + itostr(Cycles) + ", ";
222 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
225 for (unsigned j = 0, M = UnitList.size(); j < M;) {
226 // Add name and bitwise or
227 ItinString += Name + "FU::" + UnitList[j]->getName();
228 if (++j < M) ItinString += " | ";
231 int TimeInc = Stage->getValueAsInt("TimeInc");
232 ItinString += ", " + itostr(TimeInc);
234 int Kind = Stage->getValueAsInt("Kind");
235 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
239 if (++i < N) ItinString += ", ";
244 // FormItineraryOperandCycleString - Compose a string containing the
245 // operand cycle initialization for the specified itinerary. N is the
246 // number of operands that has cycles specified.
248 void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
249 std::string &ItinString, unsigned &NOperandCycles) {
250 // Get operand cycle list
251 const std::vector<int64_t> &OperandCycleList =
252 ItinData->getValueAsListOfInts("OperandCycles");
254 // For each operand cycle
255 unsigned N = NOperandCycles = OperandCycleList.size();
256 for (unsigned i = 0; i < N;) {
257 // Next operand cycle
258 const int OCycle = OperandCycleList[i];
260 ItinString += " " + itostr(OCycle);
261 if (++i < N) ItinString += ", ";
265 void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
267 std::string &ItinString,
268 unsigned NOperandCycles) {
269 const std::vector<Record*> &BypassList =
270 ItinData->getValueAsListOfDefs("Bypasses");
271 unsigned N = BypassList.size();
274 ItinString += Name + "Bypass::" + BypassList[i]->getName();
275 if (++i < NOperandCycles) ItinString += ", ";
277 for (; i < NOperandCycles;) {
279 if (++i < NOperandCycles) ItinString += ", ";
284 // EmitStageAndOperandCycleData - Generate unique itinerary stages and
285 // operand cycle tables. Record itineraries for processors.
287 void SubtargetEmitter::EmitStageAndOperandCycleData(raw_ostream &OS,
288 unsigned NItinClasses,
289 std::map<std::string, unsigned> &ItinClassesMap,
290 std::vector<Record*> &ItinClassList,
291 std::vector<std::vector<InstrItinerary> > &ProcList) {
292 // Gather processor iteraries
293 std::vector<Record*> ProcItinList =
294 Records.getAllDerivedDefinitions("ProcessorItineraries");
296 // If just no itinerary then don't bother
297 if (ProcItinList.size() < 2) return;
299 // Emit functional units for all the itineraries.
300 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
302 Record *Proc = ProcItinList[i];
304 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
308 const std::string &Name = Proc->getName();
309 OS << "\n// Functional units for itineraries \"" << Name << "\"\n"
310 << "namespace " << Name << "FU {\n";
312 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
313 OS << " const unsigned " << FUs[j]->getName()
314 << " = 1 << " << j << ";\n";
318 std::vector<Record*> BPs = Proc->getValueAsListOfDefs("BP");
320 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
321 << "\"\n" << "namespace " << Name << "Bypass {\n";
323 OS << " const unsigned NoBypass = 0;\n";
324 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
325 OS << " const unsigned " << BPs[j]->getName()
326 << " = 1 << " << j << ";\n";
332 // Begin stages table
333 std::string StageTable = "\nstatic const llvm::InstrStage Stages[] = {\n";
334 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
336 // Begin operand cycle table
337 std::string OperandCycleTable = "static const unsigned OperandCycles[] = {\n";
338 OperandCycleTable += " 0, // No itinerary\n";
340 // Begin pipeline bypass table
341 std::string BypassTable = "static const unsigned ForwardingPathes[] = {\n";
342 BypassTable += " 0, // No itinerary\n";
344 unsigned StageCount = 1, OperandCycleCount = 1;
345 unsigned ItinStageEnum = 1, ItinOperandCycleEnum = 1;
346 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
347 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
349 Record *Proc = ProcItinList[i];
351 // Get processor itinerary name
352 const std::string &Name = Proc->getName();
355 if (Name == "NoItineraries") continue;
357 // Create and expand processor itinerary to cover all itinerary classes
358 std::vector<InstrItinerary> ItinList;
359 ItinList.resize(NItinClasses);
361 // Get itinerary data list
362 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
364 // For each itinerary data
365 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
366 // Next itinerary data
367 Record *ItinData = ItinDataList[j];
369 // Get string and stage count
370 std::string ItinStageString;
372 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
374 // Get string and operand cycle count
375 std::string ItinOperandCycleString;
376 unsigned NOperandCycles;
377 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
380 std::string ItinBypassString;
381 FormItineraryBypassString(Name, ItinData, ItinBypassString,
384 // Check to see if stage already exists and create if it doesn't
385 unsigned FindStage = 0;
387 FindStage = ItinStageMap[ItinStageString];
388 if (FindStage == 0) {
389 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // index
390 StageTable += ItinStageString + ", // " + itostr(ItinStageEnum) + "\n";
391 // Record Itin class number.
392 ItinStageMap[ItinStageString] = FindStage = StageCount;
393 StageCount += NStages;
398 // Check to see if operand cycle already exists and create if it doesn't
399 unsigned FindOperandCycle = 0;
400 if (NOperandCycles > 0) {
401 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
402 FindOperandCycle = ItinOperandMap[ItinOperandString];
403 if (FindOperandCycle == 0) {
404 // Emit as cycle, // index
405 OperandCycleTable += ItinOperandCycleString + ", // " +
406 itostr(ItinOperandCycleEnum) + "\n";
407 // Record Itin class number.
408 ItinOperandMap[ItinOperandCycleString] =
409 FindOperandCycle = OperandCycleCount;
411 // Emit as bypass, // index
412 BypassTable += ItinBypassString + ", // " +
413 itostr(ItinOperandCycleEnum) + "\n";
415 OperandCycleCount += NOperandCycles;
416 ItinOperandCycleEnum++;
420 // Locate where to inject into processor itinerary table
421 const std::string &Name = ItinData->getValueAsDef("TheClass")->getName();
422 unsigned Find = ItinClassesMap[Name];
424 // Set up itinerary as location and location + stage count
425 unsigned NumUOps = ItinClassList[Find]->getValueAsInt("NumMicroOps");
426 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
428 FindOperandCycle + NOperandCycles};
430 // Inject - empty slots will be 0, 0
431 ItinList[Find] = Intinerary;
434 // Add process itinerary to list
435 ProcList.push_back(ItinList);
439 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End itinerary\n";
440 StageTable += "};\n";
442 // Closing operand cycles
443 OperandCycleTable += " 0 // End itinerary\n";
444 OperandCycleTable += "};\n";
446 BypassTable += " 0 // End itinerary\n";
447 BypassTable += "};\n";
451 OS << OperandCycleTable;
454 // Emit size of tables
456 OS<<" StagesSize = sizeof(Stages)/sizeof(llvm::InstrStage),\n";
457 OS<<" OperandCyclesSize = sizeof(OperandCycles)/sizeof(unsigned)\n";
462 // EmitProcessorData - Generate data for processor itineraries.
464 void SubtargetEmitter::EmitProcessorData(raw_ostream &OS,
465 std::vector<std::vector<InstrItinerary> > &ProcList) {
466 // Get an iterator for processor itinerary stages
467 std::vector<std::vector<InstrItinerary> >::iterator
468 ProcListIter = ProcList.begin();
470 // For each processor itinerary
471 std::vector<Record*> Itins =
472 Records.getAllDerivedDefinitions("ProcessorItineraries");
473 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
475 Record *Itin = Itins[i];
477 // Get processor itinerary name
478 const std::string &Name = Itin->getName();
481 if (Name == "NoItineraries") continue;
483 // Begin processor itinerary table
485 OS << "static const llvm::InstrItinerary " << Name << "[] = {\n";
487 // For each itinerary class
488 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
489 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
490 InstrItinerary &Intinerary = ItinList[j];
492 // Emit in the form of
493 // { firstStage, lastStage, firstCycle, lastCycle } // index
494 if (Intinerary.FirstStage == 0) {
495 OS << " { 1, 0, 0, 0, 0 }";
498 Intinerary.NumMicroOps << ", " <<
499 Intinerary.FirstStage << ", " <<
500 Intinerary.LastStage << ", " <<
501 Intinerary.FirstOperandCycle << ", " <<
502 Intinerary.LastOperandCycle << " }";
505 OS << ", // " << j << "\n";
508 // End processor itinerary table
509 OS << " { 1, ~0U, ~0U, ~0U, ~0U } // end marker\n";
515 // EmitProcessorLookup - generate cpu name to itinerary lookup table.
517 void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
518 // Gather and sort processor information
519 std::vector<Record*> ProcessorList =
520 Records.getAllDerivedDefinitions("Processor");
521 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
523 // Begin processor table
525 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
526 << "static const llvm::SubtargetInfoKV ProcItinKV[] = {\n";
528 // For each processor
529 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
531 Record *Processor = ProcessorList[i];
533 const std::string &Name = Processor->getValueAsString("Name");
534 const std::string &ProcItin =
535 Processor->getValueAsDef("ProcItin")->getName();
537 // Emit as { "cpu", procinit },
539 << "\"" << Name << "\", "
540 << "(void *)&" << ProcItin;
544 // Depending on ''if more in the list'' emit comma
545 if (++i < N) OS << ",";
550 // End processor table
553 // Emit size of table
555 OS<<" ProcItinKVSize = sizeof(ProcItinKV)/"
556 "sizeof(llvm::SubtargetInfoKV)\n";
561 // EmitData - Emits all stages and itineries, folding common patterns.
563 void SubtargetEmitter::EmitData(raw_ostream &OS) {
564 std::map<std::string, unsigned> ItinClassesMap;
565 // Gather and sort all itinerary classes
566 std::vector<Record*> ItinClassList =
567 Records.getAllDerivedDefinitions("InstrItinClass");
568 std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
570 // Enumerate all the itinerary classes
571 unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap,
573 // Make sure the rest is worth the effort
574 HasItineraries = NItinClasses != 1; // Ignore NoItinerary.
576 if (HasItineraries) {
577 std::vector<std::vector<InstrItinerary> > ProcList;
578 // Emit the stage data
579 EmitStageAndOperandCycleData(OS, NItinClasses, ItinClassesMap,
580 ItinClassList, ProcList);
581 // Emit the processor itinerary data
582 EmitProcessorData(OS, ProcList);
583 // Emit the processor lookup data
584 EmitProcessorLookup(OS);
589 // ParseFeaturesFunction - Produces a subtarget specific function for parsing
590 // the subtarget features string.
592 void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS) {
593 std::vector<Record*> Features =
594 Records.getAllDerivedDefinitions("SubtargetFeature");
595 std::sort(Features.begin(), Features.end(), LessRecord());
597 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
598 << "// subtarget options.\n"
599 << "std::string llvm::";
601 OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
602 << " const std::string &CPU) {\n"
603 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
604 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU);\n"
605 << " SubtargetFeatures Features(FS);\n"
606 << " Features.setCPUIfNone(CPU);\n"
607 << " uint32_t Bits = Features.getBits(SubTypeKV, SubTypeKVSize,\n"
608 << " FeatureKV, FeatureKVSize);\n";
610 for (unsigned i = 0; i < Features.size(); i++) {
612 Record *R = Features[i];
613 const std::string &Instance = R->getName();
614 const std::string &Value = R->getValueAsString("Value");
615 const std::string &Attribute = R->getValueAsString("Attribute");
617 if (Value=="true" || Value=="false")
618 OS << " if ((Bits & " << Instance << ") != 0) "
619 << Attribute << " = " << Value << ";\n";
621 OS << " if ((Bits & " << Instance << ") != 0 && " << Attribute <<
622 " < " << Value << ") " << Attribute << " = " << Value << ";\n";
625 if (HasItineraries) {
627 << " InstrItinerary *Itinerary = (InstrItinerary *)"
628 << "Features.getInfo(ProcItinKV, ProcItinKVSize);\n"
629 << " InstrItins = InstrItineraryData(Stages, OperandCycles, "
630 << "ForwardingPathes, Itinerary);\n";
633 OS << " return Features.getCPU();\n"
638 // SubtargetEmitter::run - Main subtarget enumeration emitter.
640 void SubtargetEmitter::run(raw_ostream &OS) {
641 Target = CodeGenTarget(Records).getName();
643 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
645 OS << "#include \"llvm/Support/Debug.h\"\n";
646 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
647 OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
648 OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
650 // Enumeration(OS, "FuncUnit", true);
652 // Enumeration(OS, "InstrItinClass", false);
654 Enumeration(OS, "SubtargetFeature", true);
656 FeatureKeyValues(OS);
662 ParseFeaturesFunction(OS);