1 //===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by James M. Laskey and is distributed under
6 // the University of Illinois Open Source 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 // Record sort by name function.
26 bool operator()(const Record *Rec1, const Record *Rec2) const {
27 return Rec1->getName() < Rec2->getName();
32 // Record sort by field "Name" function.
34 struct LessRecordFieldName {
35 bool operator()(const Record *Rec1, const Record *Rec2) const {
36 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
41 // Enumeration - Emit the specified class as an enumeration.
43 void SubtargetEmitter::Enumeration(std::ostream &OS,
44 const char *ClassName,
46 // Get all records of class and sort
47 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
48 std::sort(DefList.begin(), DefList.end(), LessRecord());
54 for (unsigned i = 0, N = DefList.size(); i < N;) {
56 Record *Def = DefList[i];
59 std::string Name = Def->getName();
62 // If bit flags then emit expression (1 << i)
63 if (isBits) OS << " = " << " 1 << " << i;
65 // Depending on 'if more in the list' emit comma
66 if (++i < N) OS << ",";
76 // FeatureKeyValues - Emit data of all the subtarget features. Used by command
79 void SubtargetEmitter::FeatureKeyValues(std::ostream &OS) {
80 // Gather and sort all the features
81 std::vector<Record*> FeatureList =
82 Records.getAllDerivedDefinitions("SubtargetFeature");
83 std::sort(FeatureList.begin(), FeatureList.end(), LessRecord());
85 // Begin feature table
86 OS << "// Sorted (by key) array of values for CPU features.\n"
87 << "static llvm::SubtargetFeatureKV FeatureKV[] = {\n";
90 for (unsigned i = 0, N = FeatureList.size(); i < N;) {
92 Record *Feature = FeatureList[i];
94 std::string Name = Feature->getName();
95 std::string CommandLineName = Feature->getValueAsString("Name");
96 std::string Desc = Feature->getValueAsString("Desc");
98 // Emit as { "feature", "decription", feactureEnum }
100 << "\"" << CommandLineName << "\", "
101 << "\"" << Desc << "\", "
105 // Depending on 'if more in the list' emit comma
106 if (++i < N) OS << ",";
114 // Emit size of table
116 OS<<" FeatureKVSize = sizeof(FeatureKV)/sizeof(llvm::SubtargetFeatureKV)\n";
121 // CPUKeyValues - Emit data of all the subtarget processors. Used by command
124 void SubtargetEmitter::CPUKeyValues(std::ostream &OS) {
125 // Gather and sort processor information
126 std::vector<Record*> ProcessorList =
127 Records.getAllDerivedDefinitions("Processor");
128 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
130 // Begin processor table
131 OS << "// Sorted (by key) array of values for CPU subtype.\n"
132 << "static const llvm::SubtargetFeatureKV SubTypeKV[] = {\n";
134 // For each processor
135 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
137 Record *Processor = ProcessorList[i];
139 std::string Name = Processor->getValueAsString("Name");
140 std::vector<Record*> FeatureList =
141 Processor->getValueAsListOfDefs("Features");
143 // Emit as { "cpu", "description", f1 | f2 | ... fn },
145 << "\"" << Name << "\", "
146 << "\"Select the " << Name << " processor\", ";
148 if (FeatureList.empty()) {
151 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
152 Record *Feature = FeatureList[j];
153 std::string Name = Feature->getName();
155 if (++j < M) OS << " | ";
161 // Depending on 'if more in the list' emit comma
162 if (++i < N) OS << ",";
167 // End processor table
170 // Emit size of table
172 OS<<" SubTypeKVSize = sizeof(SubTypeKV)/sizeof(llvm::SubtargetFeatureKV)\n";
177 // CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
178 // Returns itinerary class count.
180 unsigned SubtargetEmitter::CollectAllItinClasses(std::ostream &OS,
181 std::map<std::string, unsigned> &ItinClassesMap) {
182 // Gather and sort all itinerary classes
183 std::vector<Record*> ItinClassList =
184 Records.getAllDerivedDefinitions("InstrItinClass");
185 std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
187 // For each itinerary class
188 unsigned N = ItinClassList.size();
189 for (unsigned i = 0; i < N; i++) {
190 // Next itinerary class
191 Record *ItinClass = ItinClassList[i];
192 // Get name of itinerary class
193 std::string Name = ItinClass->getName();
194 // Assign itinerary class a unique number
195 ItinClassesMap[Name] = i;
198 // Emit size of table
200 OS<<" ItinClassesSize = " << N << "\n";
203 // Return itinerary class count
208 // FormItineraryString - Compose a string containing the data initialization
209 // for the specified itinerary. N is the number of stages.
211 void SubtargetEmitter::FormItineraryString(Record *ItinData,
212 std::string &ItinString,
215 std::vector<Record*> StageList = ItinData->getValueAsListOfDefs("Stages");
218 unsigned N = NStages = StageList.size();
219 for (unsigned i = 0; i < N; i++) {
221 Record *Stage = StageList[i];
223 // Form string as ,{ cycles, u1 | u2 | ... | un }
224 int Cycles = Stage->getValueAsInt("Cycles");
225 ItinString += " { " + itostr(Cycles) + ", ";
228 std::vector<Record*> UnitList = Stage->getValueAsListOfDefs("Units");
231 for (unsigned j = 0, M = UnitList.size(); j < M;) {
233 Record *Unit = UnitList[j];
235 // Add name and bitwise or
236 ItinString += Unit->getName();
237 if (++j < M) ItinString += " | ";
246 // EmitStageData - Generate unique itinerary stages. Record itineraries for
249 void SubtargetEmitter::EmitStageData(std::ostream &OS,
250 unsigned NItinClasses,
251 std::map<std::string, unsigned> &ItinClassesMap,
252 std::vector<std::vector<InstrItinerary> > &ProcList) {
253 // Gather processor iteraries
254 std::vector<Record*> ProcItinList =
255 Records.getAllDerivedDefinitions("ProcessorItineraries");
257 // If just no itinerary then don't bother
258 if (ProcItinList.size() < 2) return;
260 // Begin stages table
261 OS << "static llvm::InstrStage Stages[] = {\n"
262 " { 0, 0 }, // No itinerary\n";
264 unsigned ItinEnum = 1;
265 std::map<std::string, unsigned> ItinMap;
266 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
268 Record *Proc = ProcItinList[i];
270 // Get processor itinerary name
271 std::string Name = Proc->getName();
274 if (Name == "NoItineraries") continue;
276 // Create and expand processor itinerary to cover all itinerary classes
277 std::vector<InstrItinerary> ItinList;
278 ItinList.resize(NItinClasses);
280 // Get itinerary data list
281 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
283 // For each itinerary data
284 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
285 // Next itinerary data
286 Record *ItinData = ItinDataList[j];
288 // Get string and stage count
289 std::string ItinString;
291 FormItineraryString(ItinData, ItinString, NStages);
293 // Check to see if it already exists
294 unsigned Find = ItinMap[ItinString];
298 // Emit as { cycles, u1 | u2 | ... | un }, // index
299 OS << ItinString << ", // " << ItinEnum << "\n";
300 // Record Itin class number
301 ItinMap[ItinString] = Find = ItinEnum++;
304 // Set up itinerary as location and location + stage count
305 InstrItinerary Intinerary = { Find, Find + NStages };
307 // Locate where to inject into processor itinerary table
308 std::string Name = ItinData->getValueAsDef("TheClass")->getName();
309 Find = ItinClassesMap[Name];
311 // Inject - empty slots will be 0, 0
312 ItinList[Find] = Intinerary;
315 // Add process itinerary to list
316 ProcList.push_back(ItinList);
320 OS << " { 0, 0 } // End itinerary\n";
324 // Emit size of table
326 OS<<" StagesSize = sizeof(Stages)/sizeof(llvm::InstrStage)\n";
331 // EmitProcessorData - Generate data for processor itineraries.
333 void SubtargetEmitter::EmitProcessorData(std::ostream &OS,
334 std::vector<std::vector<InstrItinerary> > &ProcList) {
335 // Get an iterator for processor itinerary stages
336 std::vector<std::vector<InstrItinerary> >::iterator
337 ProcListIter = ProcList.begin();
339 // For each processor itinerary
340 std::vector<Record*> Itins =
341 Records.getAllDerivedDefinitions("ProcessorItineraries");
342 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
344 Record *Itin = Itins[i];
346 // Get processor itinerary name
347 std::string Name = Itin->getName();
350 if (Name == "NoItineraries") continue;
352 // Begin processor itinerary table
354 OS << "static llvm::InstrItinerary " << Name << "[] = {\n";
356 // For each itinerary class
357 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
358 for (unsigned j = 0, M = ItinList.size(); j < M;) {
359 InstrItinerary &Intinerary = ItinList[j];
361 // Emit in the form of { first, last } // index
362 if (Intinerary.First == 0) {
365 OS << " { " << Intinerary.First << ", " << Intinerary.Last << " }";
368 // If more in list add comma
369 if (++j < M) OS << ",";
371 OS << " // " << (j - 1) << "\n";
374 // End processor itinerary table
380 // EmitProcessorLookup - generate cpu name to itinerary lookup table.
382 void SubtargetEmitter::EmitProcessorLookup(std::ostream &OS) {
383 // Gather and sort processor information
384 std::vector<Record*> ProcessorList =
385 Records.getAllDerivedDefinitions("Processor");
386 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
388 // Begin processor table
390 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
391 << "static const llvm::SubtargetInfoKV ProcItinKV[] = {\n";
393 // For each processor
394 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
396 Record *Processor = ProcessorList[i];
398 std::string Name = Processor->getValueAsString("Name");
399 std::string ProcItin = Processor->getValueAsDef("ProcItin")->getName();
401 // Emit as { "cpu", procinit },
403 << "\"" << Name << "\", "
404 << "(void *)&" << ProcItin;
408 // Depending on ''if more in the list'' emit comma
409 if (++i < N) OS << ",";
414 // End processor table
417 // Emit size of table
419 OS<<" ProcItinKVSize = sizeof(ProcItinKV)/"
420 "sizeof(llvm::SubtargetInfoKV)\n";
425 // EmitData - Emits all stages and itineries, folding common patterns.
427 void SubtargetEmitter::EmitData(std::ostream &OS) {
428 std::map<std::string, unsigned> ItinClassesMap;
429 std::vector<std::vector<InstrItinerary> > ProcList;
431 // Enumerate all the itinerary classes
432 unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap);
433 // Make sure the rest is worth the effort
434 HasItineraries = NItinClasses != 1; // Ignore NoItinerary.
436 if (HasItineraries) {
437 // Emit the stage data
438 EmitStageData(OS, NItinClasses, ItinClassesMap, ProcList);
439 // Emit the processor itinerary data
440 EmitProcessorData(OS, ProcList);
441 // Emit the processor lookup data
442 EmitProcessorLookup(OS);
447 // ParseFeaturesFunction - Produces a subtarget specific function for parsing
448 // the subtarget features string.
450 void SubtargetEmitter::ParseFeaturesFunction(std::ostream &OS) {
451 std::vector<Record*> Features =
452 Records.getAllDerivedDefinitions("SubtargetFeature");
453 std::sort(Features.begin(), Features.end(), LessRecord());
455 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
456 "// subtarget options.\n"
459 OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
460 " const std::string &CPU) {\n"
461 " SubtargetFeatures Features(FS);\n"
462 " Features.setCPUIfNone(CPU);\n"
463 " uint32_t Bits = Features.getBits(SubTypeKV, SubTypeKVSize,\n"
464 " FeatureKV, FeatureKVSize);\n";
466 for (unsigned i = 0; i < Features.size(); i++) {
468 Record *R = Features[i];
469 std::string Instance = R->getName();
470 std::string Name = R->getValueAsString("Name");
471 std::string Value = R->getValueAsString("Value");
472 std::string Attribute = R->getValueAsString("Attribute");
474 OS << " if ((Bits & " << Instance << ") != 0) "
475 << Attribute << " = " << Value << ";\n";
478 if (HasItineraries) {
480 << " InstrItinerary *Itinerary = (InstrItinerary *)"
481 "Features.getInfo(ProcItinKV, ProcItinKVSize);\n"
482 " InstrItins = InstrItineraryData(Stages, Itinerary);\n";
489 // SubtargetEmitter::run - Main subtarget enumeration emitter.
491 void SubtargetEmitter::run(std::ostream &OS) {
492 Target = CodeGenTarget().getName();
494 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
496 OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
497 OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
499 Enumeration(OS, "FuncUnit", true);
501 // Enumeration(OS, "InstrItinClass", false);
503 Enumeration(OS, "SubtargetFeature", true);
505 FeatureKeyValues(OS);
511 ParseFeaturesFunction(OS);