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. The format is in a state
11 // flux and will be tightened up when integration to scheduling is complete.
13 //===----------------------------------------------------------------------===//
15 #include "SubtargetEmitter.h"
16 #include "CodeGenTarget.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Support/Debug.h"
24 // Record sort by name function.
27 bool operator()(const Record *Rec1, const Record *Rec2) const {
28 return Rec1->getName() < Rec2->getName();
33 // Record sort by field "Name" function.
35 struct LessRecordFieldName {
36 bool operator()(const Record *Rec1, const Record *Rec2) const {
37 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
42 // Enumeration - Emit the specified class as an enumeration.
44 void SubtargetEmitter::Enumeration(std::ostream &OS,
45 const char *ClassName,
47 // Get all records of class and sort
48 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
49 sort(DefList.begin(), DefList.end(), LessRecord());
55 for (unsigned i = 0, N = DefList.size(); i < N;) {
57 Record *Def = DefList[i];
60 std::string Name = Def->getName();
63 // If bit flags then emit expression (1 << i)
64 if (isBits) OS << " = " << " 1 << " << i;
66 // Depending on 'if more in the list' emit comma
67 if (++i < N) OS << ",";
77 // FeatureKeyValues - Emit data of all the subtarget features. Used by command
80 void SubtargetEmitter::FeatureKeyValues(std::ostream &OS) {
81 // Gather and sort all the features
82 std::vector<Record*> FeatureList =
83 Records.getAllDerivedDefinitions("SubtargetFeature");
84 sort(FeatureList.begin(), FeatureList.end(), LessRecord());
86 // Begin feature table
87 OS << "// Sorted (by key) array of values for CPU features.\n"
88 << "static llvm::SubtargetFeatureKV FeatureKV[] = {\n";
91 for (unsigned i = 0, N = FeatureList.size(); i < N;) {
93 Record *Feature = FeatureList[i];
95 std::string Name = Feature->getName();
96 std::string CommandLineName = Feature->getValueAsString("Name");
97 std::string Desc = Feature->getValueAsString("Desc");
99 // Emit as { "feature", "decription", feactureEnum }
101 << "\"" << CommandLineName << "\", "
102 << "\"" << Desc << "\", "
106 // Depending on 'if more in the list' emit comma
107 if (++i < N) OS << ",";
115 // Emit size of table
117 OS<<" FeatureKVSize = sizeof(FeatureKV)/sizeof(llvm::SubtargetFeatureKV)\n";
122 // CPUKeyValues - Emit data of all the subtarget processors. Used by command
125 void SubtargetEmitter::CPUKeyValues(std::ostream &OS) {
126 // Gather and sort processor information
127 std::vector<Record*> ProcessorList =
128 Records.getAllDerivedDefinitions("Processor");
129 sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
131 // Begin processor table
132 OS << "// Sorted (by key) array of values for CPU subtype.\n"
133 << "static const llvm::SubtargetFeatureKV SubTypeKV[] = {\n";
135 // For each processor
136 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
138 Record *Processor = ProcessorList[i];
140 std::string Name = Processor->getValueAsString("Name");
141 std::vector<Record*> FeatureList =
142 Processor->getValueAsListOfDefs("Features");
144 // Emit as { "cpu", "description", f1 | f2 | ... fn },
146 << "\"" << Name << "\", "
147 << "\"Select the " << Name << " processor\", ";
149 if (FeatureList.empty()) {
152 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
153 Record *Feature = FeatureList[j];
154 std::string Name = Feature->getName();
156 if (++j < M) OS << " | ";
162 // Depending on 'if more in the list' emit comma
163 if (++i < N) OS << ",";
168 // End processor table
171 // Emit size of table
173 OS<<" SubTypeKVSize = sizeof(SubTypeKV)/sizeof(llvm::SubtargetFeatureKV)\n";
178 // CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
179 // Returns itinerary class count.
181 unsigned SubtargetEmitter::CollectAllItinClasses(std::ostream &OS,
182 std::map<std::string, unsigned> &ItinClassesMap) {
183 // Gather and sort all itinerary classes
184 std::vector<Record*> ItinClassList =
185 Records.getAllDerivedDefinitions("InstrItinClass");
186 sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
188 // For each itinerary class
189 unsigned N = ItinClassList.size();
190 for (unsigned i = 0; i < N; i++) {
191 // Next itinerary class
192 Record *ItinClass = ItinClassList[i];
193 // Get name of itinerary class
194 std::string Name = ItinClass->getName();
195 // Assign itinerary class a unique number
196 ItinClassesMap[Name] = i;
199 // Emit size of table
201 OS<<" ItinClassesSize = " << N << "\n";
204 // Return itinerary class count
209 // FormItineraryString - Compose a string containing the data initialization
210 // for the specified itinerary. N is the number of stages.
212 void SubtargetEmitter::FormItineraryString(Record *ItinData,
213 std::string &ItinString,
216 std::vector<Record*> StageList = ItinData->getValueAsListOfDefs("Stages");
219 unsigned N = NStages = StageList.size();
220 for (unsigned i = 0; i < N; i++) {
222 Record *Stage = StageList[i];
224 // Form string as ,{ cycles, u1 | u2 | ... | un }
225 int Cycles = Stage->getValueAsInt("Cycles");
226 ItinString += " { " + itostr(Cycles) + ", ";
229 std::vector<Record*> UnitList = Stage->getValueAsListOfDefs("Units");
232 for (unsigned j = 0, M = UnitList.size(); j < M;) {
234 Record *Unit = UnitList[j];
236 // Add name and bitwise or
237 ItinString += Unit->getName();
238 if (++j < M) ItinString += " | ";
247 // EmitStageData - Generate unique itinerary stages. Record itineraries for
250 void SubtargetEmitter::EmitStageData(std::ostream &OS,
251 unsigned NItinClasses,
252 std::map<std::string, unsigned> &ItinClassesMap,
253 std::vector<std::vector<InstrItinerary> > &ProcList) {
254 // Gather processor iteraries
255 std::vector<Record*> ProcItinList =
256 Records.getAllDerivedDefinitions("ProcessorItineraries");
258 // If just no itinerary then don't bother
259 if (ProcItinList.size() < 2) return;
261 // Begin stages table
262 OS << "static llvm::InstrStage Stages[] = {\n"
263 " { 0, 0 }, // No itinerary\n";
265 unsigned ItinEnum = 1;
266 std::map<std::string, unsigned> ItinMap;
267 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
269 Record *Proc = ProcItinList[i];
271 // Get processor itinerary name
272 std::string Name = Proc->getName();
275 if (Name == "NoItineraries") continue;
277 // Create and expand processor itinerary to cover all itinerary classes
278 std::vector<InstrItinerary> ItinList;
279 ItinList.resize(NItinClasses);
281 // Get itinerary data list
282 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
284 // For each itinerary data
285 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
286 // Next itinerary data
287 Record *ItinData = ItinDataList[j];
289 // Get string and stage count
290 std::string ItinString;
292 FormItineraryString(ItinData, ItinString, NStages);
294 // Check to see if it already exists
295 unsigned Find = ItinMap[ItinString];
299 // Emit as { cycles, u1 | u2 | ... | un }, // index
300 OS << ItinString << ", // " << ItinEnum << "\n";
301 // Record Itin class number
302 ItinMap[ItinString] = Find = ItinEnum++;
305 // Set up itinerary as location and location + stage count
306 InstrItinerary Intinerary = { Find, Find + NStages };
308 // Locate where to inject into processor itinerary table
309 std::string Name = ItinData->getValueAsDef("TheClass")->getName();
310 Find = ItinClassesMap[Name];
312 // Inject - empty slots will be 0, 0
313 ItinList[Find] = Intinerary;
316 // Add process itinerary to list
317 ProcList.push_back(ItinList);
321 OS << " { 0, 0 } // End itinerary\n";
325 // Emit size of table
327 OS<<" StagesSize = sizeof(Stages)/sizeof(llvm::InstrStage)\n";
332 // EmitProcessorData - Generate data for processor itineraries.
334 void SubtargetEmitter::EmitProcessorData(std::ostream &OS,
335 std::vector<std::vector<InstrItinerary> > &ProcList) {
336 // Get an iterator for processor itinerary stages
337 std::vector<std::vector<InstrItinerary> >::iterator
338 ProcListIter = ProcList.begin();
340 // For each processor itinerary
341 std::vector<Record*> Itins =
342 Records.getAllDerivedDefinitions("ProcessorItineraries");
343 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
345 Record *Itin = Itins[i];
347 // Get processor itinerary name
348 std::string Name = Itin->getName();
351 if (Name == "NoItineraries") continue;
353 // Begin processor itinerary table
355 OS << "static llvm::InstrItinerary " << Name << "[] = {\n";
357 // For each itinerary class
358 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
359 unsigned ItinIndex = 0;
360 for (unsigned j = 0, M = ItinList.size(); j < M;) {
361 InstrItinerary &Intinerary = ItinList[j];
363 // Emit in the form of { first, last } // index
364 if (Intinerary.First == 0) {
367 OS << " { " << Intinerary.First << ", " << Intinerary.Last << " }";
370 // If more in list add comma
371 if (++j < M) OS << ",";
373 OS << " // " << (j - 1) << "\n";
376 // End processor itinerary table
381 OS << "static llvm::InstrItinerary NoItineraries[] = {};\n";
385 // EmitProcessorLookup - generate cpu name to itinerary lookup table.
387 void SubtargetEmitter::EmitProcessorLookup(std::ostream &OS) {
388 // Gather and sort processor information
389 std::vector<Record*> ProcessorList =
390 Records.getAllDerivedDefinitions("Processor");
391 sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
393 // Begin processor table
395 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
396 << "static const llvm::SubtargetInfoKV ProcItinKV[] = {\n";
398 // For each processor
399 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
401 Record *Processor = ProcessorList[i];
403 std::string Name = Processor->getValueAsString("Name");
404 std::string ProcItin = Processor->getValueAsDef("ProcItin")->getName();
406 // Emit as { "cpu", procinit },
408 << "\"" << Name << "\", "
409 << "(void *)&" << ProcItin;
413 // Depending on ''if more in the list'' emit comma
414 if (++i < N) OS << ",";
419 // End processor table
422 // Emit size of table
424 OS<<" ProcItinKVSize = sizeof(ProcItinKV)/"
425 "sizeof(llvm::SubtargetInfoKV)\n";
430 // EmitData - Emits all stages and itineries, folding common patterns.
432 void SubtargetEmitter::EmitData(std::ostream &OS) {
433 std::map<std::string, unsigned> ItinClassesMap;
434 std::vector<std::vector<InstrItinerary> > ProcList;
436 // Enumerate all the itinerary classes
437 unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap);
438 // Make sure the rest is worth the effort
439 HasItineraries = NItinClasses != 0;
441 if (HasItineraries) {
442 // Emit the stage data
443 EmitStageData(OS, NItinClasses, ItinClassesMap, ProcList);
444 // Emit the processor itinerary data
445 EmitProcessorData(OS, ProcList);
446 // Emit the processor lookup data
447 EmitProcessorLookup(OS);
452 // ParseFeaturesFunction - Produces a subtarget specific function for parsing
453 // the subtarget features string.
455 void SubtargetEmitter::ParseFeaturesFunction(std::ostream &OS) {
456 std::vector<Record*> Features =
457 Records.getAllDerivedDefinitions("SubtargetFeature");
458 sort(Features.begin(), Features.end(), LessRecord());
460 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
461 "// subtarget options.\n"
464 OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
465 " const std::string &CPU) {\n"
466 " SubtargetFeatures Features(FS);\n"
467 " Features.setCPUIfNone(CPU);\n"
468 " uint32_t Bits = Features.getBits(SubTypeKV, SubTypeKVSize,\n"
469 " FeatureKV, FeatureKVSize);\n";
471 for (unsigned i = 0; i < Features.size(); i++) {
473 Record *R = Features[i];
474 std::string Instance = R->getName();
475 std::string Name = R->getValueAsString("Name");
476 std::string Type = R->getValueAsString("Type");
477 std::string Attribute = R->getValueAsString("Attribute");
479 OS << " " << Attribute << " = (Bits & " << Instance << ") != 0;\n";
482 if (HasItineraries) {
484 << " InstrItinerary *Itinerary = (InstrItinerary *)"
485 "Features.getInfo(ProcItinKV, ProcItinKVSize);\n"
486 " InstrItins = InstrItineraryData(Stages, Itinerary);\n";
493 // SubtargetEmitter::run - Main subtarget enumeration emitter.
495 void SubtargetEmitter::run(std::ostream &OS) {
496 Target = CodeGenTarget().getName();
498 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
500 OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
501 OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
503 Enumeration(OS, "FuncUnit", true);
505 // Enumeration(OS, "InstrItinClass", false);
507 Enumeration(OS, "SubtargetFeature", true);
509 FeatureKeyValues(OS);
515 ParseFeaturesFunction(OS);