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; ++i) {
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 if (CommandLineName.empty()) continue;
100 // Emit as { "feature", "decription", feactureEnum }
102 << "\"" << CommandLineName << "\", "
103 << "\"" << Desc << "\", "
107 // Depending on 'if more in the list' emit comma
108 if ((i + 1) < N) OS << ",";
116 // Emit size of table
118 OS<<" FeatureKVSize = sizeof(FeatureKV)/sizeof(llvm::SubtargetFeatureKV)\n";
123 // CPUKeyValues - Emit data of all the subtarget processors. Used by command
126 void SubtargetEmitter::CPUKeyValues(std::ostream &OS) {
127 // Gather and sort processor information
128 std::vector<Record*> ProcessorList =
129 Records.getAllDerivedDefinitions("Processor");
130 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
132 // Begin processor table
133 OS << "// Sorted (by key) array of values for CPU subtype.\n"
134 << "static const llvm::SubtargetFeatureKV SubTypeKV[] = {\n";
136 // For each processor
137 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
139 Record *Processor = ProcessorList[i];
141 std::string Name = Processor->getValueAsString("Name");
142 std::vector<Record*> FeatureList =
143 Processor->getValueAsListOfDefs("Features");
145 // Emit as { "cpu", "description", f1 | f2 | ... fn },
147 << "\"" << Name << "\", "
148 << "\"Select the " << Name << " processor\", ";
150 if (FeatureList.empty()) {
153 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
154 Record *Feature = FeatureList[j];
155 std::string Name = Feature->getName();
157 if (++j < M) OS << " | ";
163 // Depending on 'if more in the list' emit comma
164 if (++i < N) OS << ",";
169 // End processor table
172 // Emit size of table
174 OS<<" SubTypeKVSize = sizeof(SubTypeKV)/sizeof(llvm::SubtargetFeatureKV)\n";
179 // CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
180 // Returns itinerary class count.
182 unsigned SubtargetEmitter::CollectAllItinClasses(std::ostream &OS,
183 std::map<std::string, unsigned> &ItinClassesMap) {
184 // Gather and sort all itinerary classes
185 std::vector<Record*> ItinClassList =
186 Records.getAllDerivedDefinitions("InstrItinClass");
187 std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
189 // For each itinerary class
190 unsigned N = ItinClassList.size();
191 for (unsigned i = 0; i < N; i++) {
192 // Next itinerary class
193 Record *ItinClass = ItinClassList[i];
194 // Get name of itinerary class
195 std::string Name = ItinClass->getName();
196 // Assign itinerary class a unique number
197 ItinClassesMap[Name] = i;
200 // Emit size of table
202 OS<<" ItinClassesSize = " << N << "\n";
205 // Return itinerary class count
210 // FormItineraryString - Compose a string containing the data initialization
211 // for the specified itinerary. N is the number of stages.
213 void SubtargetEmitter::FormItineraryString(Record *ItinData,
214 std::string &ItinString,
217 std::vector<Record*> StageList = ItinData->getValueAsListOfDefs("Stages");
220 unsigned N = NStages = StageList.size();
221 for (unsigned i = 0; i < N;) {
223 Record *Stage = StageList[i];
225 // Form string as ,{ cycles, u1 | u2 | ... | un }
226 int Cycles = Stage->getValueAsInt("Cycles");
227 ItinString += " { " + itostr(Cycles) + ", ";
230 std::vector<Record*> UnitList = Stage->getValueAsListOfDefs("Units");
233 for (unsigned j = 0, M = UnitList.size(); j < M;) {
235 Record *Unit = UnitList[j];
237 // Add name and bitwise or
238 ItinString += Unit->getName();
239 if (++j < M) ItinString += " | ";
244 if (++i < N) ItinString += ", ";
249 // EmitStageData - Generate unique itinerary stages. Record itineraries for
252 void SubtargetEmitter::EmitStageData(std::ostream &OS,
253 unsigned NItinClasses,
254 std::map<std::string, unsigned> &ItinClassesMap,
255 std::vector<std::vector<InstrItinerary> > &ProcList) {
256 // Gather processor iteraries
257 std::vector<Record*> ProcItinList =
258 Records.getAllDerivedDefinitions("ProcessorItineraries");
260 // If just no itinerary then don't bother
261 if (ProcItinList.size() < 2) return;
263 // Begin stages table
264 OS << "static llvm::InstrStage Stages[] = {\n"
265 " { 0, 0 }, // No itinerary\n";
267 unsigned ItinEnum = 1;
268 std::map<std::string, unsigned> ItinMap;
269 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
271 Record *Proc = ProcItinList[i];
273 // Get processor itinerary name
274 std::string Name = Proc->getName();
277 if (Name == "NoItineraries") continue;
279 // Create and expand processor itinerary to cover all itinerary classes
280 std::vector<InstrItinerary> ItinList;
281 ItinList.resize(NItinClasses);
283 // Get itinerary data list
284 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
286 // For each itinerary data
287 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
288 // Next itinerary data
289 Record *ItinData = ItinDataList[j];
291 // Get string and stage count
292 std::string ItinString;
294 FormItineraryString(ItinData, ItinString, NStages);
296 // Check to see if it already exists
297 unsigned Find = ItinMap[ItinString];
301 // Emit as { cycles, u1 | u2 | ... | un }, // index
302 OS << ItinString << ", // " << ItinEnum << "\n";
303 // Record Itin class number
304 ItinMap[ItinString] = Find = ItinEnum++;
307 // Set up itinerary as location and location + stage count
308 InstrItinerary Intinerary = { Find, Find + NStages };
310 // Locate where to inject into processor itinerary table
311 std::string Name = ItinData->getValueAsDef("TheClass")->getName();
312 Find = ItinClassesMap[Name];
314 // Inject - empty slots will be 0, 0
315 ItinList[Find] = Intinerary;
318 // Add process itinerary to list
319 ProcList.push_back(ItinList);
323 OS << " { 0, 0 } // End itinerary\n";
327 // Emit size of table
329 OS<<" StagesSize = sizeof(Stages)/sizeof(llvm::InstrStage)\n";
334 // EmitProcessorData - Generate data for processor itineraries.
336 void SubtargetEmitter::EmitProcessorData(std::ostream &OS,
337 std::vector<std::vector<InstrItinerary> > &ProcList) {
338 // Get an iterator for processor itinerary stages
339 std::vector<std::vector<InstrItinerary> >::iterator
340 ProcListIter = ProcList.begin();
342 // For each processor itinerary
343 std::vector<Record*> Itins =
344 Records.getAllDerivedDefinitions("ProcessorItineraries");
345 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
347 Record *Itin = Itins[i];
349 // Get processor itinerary name
350 std::string Name = Itin->getName();
353 if (Name == "NoItineraries") continue;
355 // Begin processor itinerary table
357 OS << "static llvm::InstrItinerary " << Name << "[] = {\n";
359 // For each itinerary class
360 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
361 for (unsigned j = 0, M = ItinList.size(); j < M;) {
362 InstrItinerary &Intinerary = ItinList[j];
364 // Emit in the form of { first, last } // index
365 if (Intinerary.First == 0) {
368 OS << " { " << Intinerary.First << ", " << Intinerary.Last << " }";
371 // If more in list add comma
372 if (++j < M) OS << ",";
374 OS << " // " << (j - 1) << "\n";
377 // End processor itinerary table
383 // EmitProcessorLookup - generate cpu name to itinerary lookup table.
385 void SubtargetEmitter::EmitProcessorLookup(std::ostream &OS) {
386 // Gather and sort processor information
387 std::vector<Record*> ProcessorList =
388 Records.getAllDerivedDefinitions("Processor");
389 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
391 // Begin processor table
393 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
394 << "static const llvm::SubtargetInfoKV ProcItinKV[] = {\n";
396 // For each processor
397 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
399 Record *Processor = ProcessorList[i];
401 std::string Name = Processor->getValueAsString("Name");
402 std::string ProcItin = Processor->getValueAsDef("ProcItin")->getName();
404 // Emit as { "cpu", procinit },
406 << "\"" << Name << "\", "
407 << "(void *)&" << ProcItin;
411 // Depending on ''if more in the list'' emit comma
412 if (++i < N) OS << ",";
417 // End processor table
420 // Emit size of table
422 OS<<" ProcItinKVSize = sizeof(ProcItinKV)/"
423 "sizeof(llvm::SubtargetInfoKV)\n";
428 // EmitData - Emits all stages and itineries, folding common patterns.
430 void SubtargetEmitter::EmitData(std::ostream &OS) {
431 std::map<std::string, unsigned> ItinClassesMap;
432 std::vector<std::vector<InstrItinerary> > ProcList;
434 // Enumerate all the itinerary classes
435 unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap);
436 // Make sure the rest is worth the effort
437 HasItineraries = NItinClasses != 1; // Ignore NoItinerary.
439 if (HasItineraries) {
440 // Emit the stage data
441 EmitStageData(OS, NItinClasses, ItinClassesMap, ProcList);
442 // Emit the processor itinerary data
443 EmitProcessorData(OS, ProcList);
444 // Emit the processor lookup data
445 EmitProcessorLookup(OS);
450 // ParseFeaturesFunction - Produces a subtarget specific function for parsing
451 // the subtarget features string.
453 void SubtargetEmitter::ParseFeaturesFunction(std::ostream &OS) {
454 std::vector<Record*> Features =
455 Records.getAllDerivedDefinitions("SubtargetFeature");
456 std::sort(Features.begin(), Features.end(), LessRecord());
458 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
459 "// subtarget options.\n"
462 OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
463 " const std::string &CPU) {\n"
464 " SubtargetFeatures Features(FS);\n"
465 " Features.setCPUIfNone(CPU);\n"
466 " uint32_t Bits = Features.getBits(SubTypeKV, SubTypeKVSize,\n"
467 " FeatureKV, FeatureKVSize);\n";
469 for (unsigned i = 0; i < Features.size(); i++) {
471 Record *R = Features[i];
472 std::string Instance = R->getName();
473 std::string Name = R->getValueAsString("Name");
474 std::string Value = R->getValueAsString("Value");
475 std::string Attribute = R->getValueAsString("Attribute");
477 OS << " if ((Bits & " << Instance << ") != 0) "
478 << Attribute << " = " << Value << ";\n";
481 if (HasItineraries) {
483 << " InstrItinerary *Itinerary = (InstrItinerary *)"
484 "Features.getInfo(ProcItinKV, ProcItinKVSize);\n"
485 " InstrItins = InstrItineraryData(Stages, Itinerary);\n";
492 // SubtargetEmitter::run - Main subtarget enumeration emitter.
494 void SubtargetEmitter::run(std::ostream &OS) {
495 Target = CodeGenTarget().getName();
497 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
499 OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
500 OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
502 Enumeration(OS, "FuncUnit", true);
504 // Enumeration(OS, "InstrItinClass", false);
506 Enumeration(OS, "SubtargetFeature", true);
508 FeatureKeyValues(OS);
514 ParseFeaturesFunction(OS);