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::CollectAllItinClasses(raw_ostream &OS,
176 std::map<std::string, unsigned> &ItinClassesMap) {
177 // Gather and sort all itinerary classes
178 std::vector<Record*> ItinClassList =
179 Records.getAllDerivedDefinitions("InstrItinClass");
180 std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
182 // For each itinerary class
183 unsigned N = ItinClassList.size();
184 for (unsigned i = 0; i < N; i++) {
185 // Next itinerary class
186 const Record *ItinClass = ItinClassList[i];
187 // Get name of itinerary class
188 // Assign itinerary class a unique number
189 ItinClassesMap[ItinClass->getName()] = i;
192 // Emit size of table
194 OS<<" ItinClassesSize = " << N << "\n";
197 // Return itinerary class count
202 // FormItineraryStageString - Compose a string containing the stage
203 // data initialization for the specified itinerary. N is the number
206 void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
208 std::string &ItinString,
211 const std::vector<Record*> &StageList =
212 ItinData->getValueAsListOfDefs("Stages");
215 unsigned N = NStages = StageList.size();
216 for (unsigned i = 0; i < N;) {
218 const Record *Stage = StageList[i];
220 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
221 int Cycles = Stage->getValueAsInt("Cycles");
222 ItinString += " { " + itostr(Cycles) + ", ";
225 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
228 for (unsigned j = 0, M = UnitList.size(); j < M;) {
229 // Add name and bitwise or
230 ItinString += Name + "FU::" + UnitList[j]->getName();
231 if (++j < M) ItinString += " | ";
234 int TimeInc = Stage->getValueAsInt("TimeInc");
235 ItinString += ", " + itostr(TimeInc);
237 int Kind = Stage->getValueAsInt("Kind");
238 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
242 if (++i < N) ItinString += ", ";
247 // FormItineraryOperandCycleString - Compose a string containing the
248 // operand cycle initialization for the specified itinerary. N is the
249 // number of operands that has cycles specified.
251 void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
252 std::string &ItinString, unsigned &NOperandCycles) {
253 // Get operand cycle list
254 const std::vector<int64_t> &OperandCycleList =
255 ItinData->getValueAsListOfInts("OperandCycles");
257 // For each operand cycle
258 unsigned N = NOperandCycles = OperandCycleList.size();
259 for (unsigned i = 0; i < N;) {
260 // Next operand cycle
261 const int OCycle = OperandCycleList[i];
263 ItinString += " " + itostr(OCycle);
264 if (++i < N) ItinString += ", ";
269 // EmitStageAndOperandCycleData - Generate unique itinerary stages and
270 // operand cycle tables. Record itineraries for processors.
272 void SubtargetEmitter::EmitStageAndOperandCycleData(raw_ostream &OS,
273 unsigned NItinClasses,
274 std::map<std::string, unsigned> &ItinClassesMap,
275 std::vector<std::vector<InstrItinerary> > &ProcList) {
276 // Gather processor iteraries
277 std::vector<Record*> ProcItinList =
278 Records.getAllDerivedDefinitions("ProcessorItineraries");
280 // If just no itinerary then don't bother
281 if (ProcItinList.size() < 2) return;
283 // Emit functional units for all the itineraries.
284 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
286 Record *Proc = ProcItinList[i];
288 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
292 const std::string &Name = Proc->getName();
293 OS << "\n// Functional units for itineraries \"" << Name << "\"\n"
294 << "namespace " << Name << "FU {\n";
296 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
297 OS << " const unsigned " << FUs[j]->getName()
298 << " = 1 << " << j << ";\n";
303 // Begin stages table
304 std::string StageTable = "\nstatic const llvm::InstrStage Stages[] = {\n";
305 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
307 // Begin operand cycle table
308 std::string OperandCycleTable = "static const unsigned OperandCycles[] = {\n";
309 OperandCycleTable += " 0, // No itinerary\n";
311 unsigned StageCount = 1, OperandCycleCount = 1;
312 unsigned ItinStageEnum = 1, ItinOperandCycleEnum = 1;
313 std::map<std::string, unsigned> ItinStageMap, ItinOperandCycleMap;
314 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
316 Record *Proc = ProcItinList[i];
318 // Get processor itinerary name
319 const std::string &Name = Proc->getName();
322 if (Name == "NoItineraries") continue;
324 // Create and expand processor itinerary to cover all itinerary classes
325 std::vector<InstrItinerary> ItinList;
326 ItinList.resize(NItinClasses);
328 // Get itinerary data list
329 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
331 // For each itinerary data
332 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
333 // Next itinerary data
334 Record *ItinData = ItinDataList[j];
336 // Get string and stage count
337 std::string ItinStageString;
339 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
341 // Get string and operand cycle count
342 std::string ItinOperandCycleString;
343 unsigned NOperandCycles;
344 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
347 // Check to see if stage already exists and create if it doesn't
348 unsigned FindStage = 0;
350 FindStage = ItinStageMap[ItinStageString];
351 if (FindStage == 0) {
352 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // index
353 StageTable += ItinStageString + ", // " + itostr(ItinStageEnum) + "\n";
354 // Record Itin class number.
355 ItinStageMap[ItinStageString] = FindStage = StageCount;
356 StageCount += NStages;
361 // Check to see if operand cycle already exists and create if it doesn't
362 unsigned FindOperandCycle = 0;
363 if (NOperandCycles > 0) {
364 FindOperandCycle = ItinOperandCycleMap[ItinOperandCycleString];
365 if (FindOperandCycle == 0) {
366 // Emit as cycle, // index
367 OperandCycleTable += ItinOperandCycleString + ", // " +
368 itostr(ItinOperandCycleEnum) + "\n";
369 // Record Itin class number.
370 ItinOperandCycleMap[ItinOperandCycleString] =
371 FindOperandCycle = OperandCycleCount;
372 OperandCycleCount += NOperandCycles;
373 ItinOperandCycleEnum++;
377 // Set up itinerary as location and location + stage count
378 InstrItinerary Intinerary = { FindStage, FindStage + NStages,
379 FindOperandCycle, FindOperandCycle + NOperandCycles};
381 // Locate where to inject into processor itinerary table
382 const std::string &Name = ItinData->getValueAsDef("TheClass")->getName();
383 unsigned Find = ItinClassesMap[Name];
385 // Inject - empty slots will be 0, 0
386 ItinList[Find] = Intinerary;
389 // Add process itinerary to list
390 ProcList.push_back(ItinList);
394 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End itinerary\n";
395 StageTable += "};\n";
397 // Closing operand cycles
398 OperandCycleTable += " 0 // End itinerary\n";
399 OperandCycleTable += "};\n";
403 OS << OperandCycleTable;
405 // Emit size of tables
407 OS<<" StagesSize = sizeof(Stages)/sizeof(llvm::InstrStage),\n";
408 OS<<" OperandCyclesSize = sizeof(OperandCycles)/sizeof(unsigned)\n";
413 // EmitProcessorData - Generate data for processor itineraries.
415 void SubtargetEmitter::EmitProcessorData(raw_ostream &OS,
416 std::vector<std::vector<InstrItinerary> > &ProcList) {
417 // Get an iterator for processor itinerary stages
418 std::vector<std::vector<InstrItinerary> >::iterator
419 ProcListIter = ProcList.begin();
421 // For each processor itinerary
422 std::vector<Record*> Itins =
423 Records.getAllDerivedDefinitions("ProcessorItineraries");
424 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
426 Record *Itin = Itins[i];
428 // Get processor itinerary name
429 const std::string &Name = Itin->getName();
432 if (Name == "NoItineraries") continue;
434 // Begin processor itinerary table
436 OS << "static const llvm::InstrItinerary " << Name << "[] = {\n";
438 // For each itinerary class
439 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
440 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
441 InstrItinerary &Intinerary = ItinList[j];
443 // Emit in the form of
444 // { firstStage, lastStage, firstCycle, lastCycle } // index
445 if (Intinerary.FirstStage == 0) {
446 OS << " { 0, 0, 0, 0 }";
448 OS << " { " << Intinerary.FirstStage << ", " <<
449 Intinerary.LastStage << ", " <<
450 Intinerary.FirstOperandCycle << ", " <<
451 Intinerary.LastOperandCycle << " }";
454 OS << ", // " << j << "\n";
457 // End processor itinerary table
458 OS << " { ~0U, ~0U, ~0U, ~0U } // end marker\n";
464 // EmitProcessorLookup - generate cpu name to itinerary lookup table.
466 void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
467 // Gather and sort processor information
468 std::vector<Record*> ProcessorList =
469 Records.getAllDerivedDefinitions("Processor");
470 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
472 // Begin processor table
474 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
475 << "static const llvm::SubtargetInfoKV ProcItinKV[] = {\n";
477 // For each processor
478 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
480 Record *Processor = ProcessorList[i];
482 const std::string &Name = Processor->getValueAsString("Name");
483 const std::string &ProcItin =
484 Processor->getValueAsDef("ProcItin")->getName();
486 // Emit as { "cpu", procinit },
488 << "\"" << Name << "\", "
489 << "(void *)&" << ProcItin;
493 // Depending on ''if more in the list'' emit comma
494 if (++i < N) OS << ",";
499 // End processor table
502 // Emit size of table
504 OS<<" ProcItinKVSize = sizeof(ProcItinKV)/"
505 "sizeof(llvm::SubtargetInfoKV)\n";
510 // EmitData - Emits all stages and itineries, folding common patterns.
512 void SubtargetEmitter::EmitData(raw_ostream &OS) {
513 std::map<std::string, unsigned> ItinClassesMap;
514 std::vector<std::vector<InstrItinerary> > ProcList;
516 // Enumerate all the itinerary classes
517 unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap);
518 // Make sure the rest is worth the effort
519 HasItineraries = NItinClasses != 1; // Ignore NoItinerary.
521 if (HasItineraries) {
522 // Emit the stage data
523 EmitStageAndOperandCycleData(OS, NItinClasses, ItinClassesMap, ProcList);
524 // Emit the processor itinerary data
525 EmitProcessorData(OS, ProcList);
526 // Emit the processor lookup data
527 EmitProcessorLookup(OS);
532 // ParseFeaturesFunction - Produces a subtarget specific function for parsing
533 // the subtarget features string.
535 void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS) {
536 std::vector<Record*> Features =
537 Records.getAllDerivedDefinitions("SubtargetFeature");
538 std::sort(Features.begin(), Features.end(), LessRecord());
540 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
541 << "// subtarget options.\n"
542 << "std::string llvm::";
544 OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
545 << " const std::string &CPU) {\n"
546 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
547 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU);\n"
548 << " SubtargetFeatures Features(FS);\n"
549 << " Features.setCPUIfNone(CPU);\n"
550 << " uint32_t Bits = Features.getBits(SubTypeKV, SubTypeKVSize,\n"
551 << " FeatureKV, FeatureKVSize);\n";
553 for (unsigned i = 0; i < Features.size(); i++) {
555 Record *R = Features[i];
556 const std::string &Instance = R->getName();
557 const std::string &Value = R->getValueAsString("Value");
558 const std::string &Attribute = R->getValueAsString("Attribute");
560 if (Value=="true" || Value=="false")
561 OS << " if ((Bits & " << Instance << ") != 0) "
562 << Attribute << " = " << Value << ";\n";
564 OS << " if ((Bits & " << Instance << ") != 0 && " << Attribute <<
565 " < " << Value << ") " << Attribute << " = " << Value << ";\n";
568 if (HasItineraries) {
570 << " InstrItinerary *Itinerary = (InstrItinerary *)"
571 << "Features.getInfo(ProcItinKV, ProcItinKVSize);\n"
572 << " InstrItins = InstrItineraryData(Stages, OperandCycles, Itinerary);\n";
575 OS << " return Features.getCPU();\n"
580 // SubtargetEmitter::run - Main subtarget enumeration emitter.
582 void SubtargetEmitter::run(raw_ostream &OS) {
583 Target = CodeGenTarget().getName();
585 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
587 OS << "#include \"llvm/Support/Debug.h\"\n";
588 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
589 OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
590 OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
592 // Enumeration(OS, "FuncUnit", true);
594 // Enumeration(OS, "InstrItinClass", false);
596 Enumeration(OS, "SubtargetFeature", true);
598 FeatureKeyValues(OS);
604 ParseFeaturesFunction(OS);