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::map<std::string, unsigned>
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 // Return itinerary class count
204 // FormItineraryString - Compose a string containing the data initialization
205 // for the specified itinerary. N is the number of stages.
207 void SubtargetEmitter::FormItineraryString(Record *ItinData,
208 std::string &ItinString,
211 std::vector<Record*> StageList = ItinData->getValueAsListOfDefs("Stages");
214 unsigned N = NStages = StageList.size();
215 for (unsigned i = 0; i < N; i++) {
217 Record *Stage = StageList[i];
219 // Form string as ,{ cycles, u1 | u2 | ... | un }
220 int Cycles = Stage->getValueAsInt("Cycles");
221 ItinString += " ,{ " + itostr(Cycles) + ", ";
224 std::vector<Record*> UnitList = Stage->getValueAsListOfDefs("Units");
227 for (unsigned j = 0, M = UnitList.size(); j < M;) {
229 Record *Unit = UnitList[j];
231 // Add name and bitwise or
232 ItinString += Unit->getName();
233 if (++j < M) ItinString += " | ";
242 // EmitStageData - Generate unique itinerary stages. Record itineraries for
245 void SubtargetEmitter::EmitStageData(std::ostream &OS,
246 unsigned NItinClasses,
247 std::map<std::string, unsigned> &ItinClassesMap,
248 std::vector<std::vector<InstrItinerary> > &ProcList) {
249 // Gather processor iteraries
250 std::vector<Record*> ProcItinList =
251 Records.getAllDerivedDefinitions("ProcessorItineraries");
253 // If just no itinerary then don't bother
254 if (ProcItinList.size() < 2) return;
256 // Begin stages table
257 OS << "static llvm::InstrStage Stages[] = {\n"
258 " { 0, 0 } // No itinerary\n";
260 unsigned ItinEnum = 1;
261 std::map<std::string, unsigned> ItinMap;
262 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
264 Record *Proc = ProcItinList[i];
266 // Get processor itinerary name
267 std::string Name = Proc->getName();
270 if (Name == "NoItineraries") continue;
272 // Create and expand processor itinerary to cover all itinerary classes
273 std::vector<InstrItinerary> ItinList;
274 ItinList.resize(NItinClasses);
276 // Get itinerary data list
277 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
279 // For each itinerary data
280 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
281 // Next itinerary data
282 Record *ItinData = ItinDataList[j];
284 // Get string and stage count
285 std::string ItinString;
287 FormItineraryString(ItinData, ItinString, NStages);
289 // Check to see if it already exists
290 unsigned Find = ItinMap[ItinString];
294 // Emit as ,{ cycles, u1 | u2 | ... | un } // index
295 OS << ItinString << " // " << ItinEnum << "\n";
296 ItinMap[ItinString] = Find = ItinEnum++;
299 // Set up itinerary as location and location + stage count
300 InstrItinerary Intinerary = { Find, Find + NStages };
302 // Locate where to inject into processor itinerary table
303 std::string Name = ItinData->getValueAsDef("TheClass")->getName();
304 Find = ItinClassesMap[Name];
306 // Inject - empty slots will be 0, 0
307 ItinList[Find] = Intinerary;
310 // Add process itinerary to list
311 ProcList.push_back(ItinList);
319 // EmitProcessorData - Generate data for processor itineraries.
321 void SubtargetEmitter::EmitProcessorData(std::ostream &OS,
322 std::vector<std::vector<InstrItinerary> > &ProcList) {
323 // Get an iterator for processor itinerary stages
324 std::vector<std::vector<InstrItinerary> >::iterator
325 ProcListIter = ProcList.begin();
327 // For each processor itinerary
328 std::vector<Record*> Itins =
329 Records.getAllDerivedDefinitions("ProcessorItineraries");
330 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
332 Record *Itin = Itins[i];
334 // Get processor itinerary name
335 std::string Name = Itin->getName();
338 if (Name == "NoItineraries") continue;
340 // Begin processor itinerary table
342 OS << "static llvm::InstrItinerary " << Name << "[] = {\n";
344 // For each itinerary class
345 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
346 unsigned ItinIndex = 0;
347 for (unsigned j = 0, M = ItinList.size(); j < M;) {
348 InstrItinerary &Intinerary = ItinList[j];
350 // Emit in the form of { first, last } // index
351 if (Intinerary.First == 0) {
354 OS << " { " << Intinerary.First << ", " << Intinerary.Last << " }";
357 // If more in list add comma
358 if (++j < M) OS << ",";
360 OS << " // " << (j - 1) << "\n";
363 // End processor itinerary table
368 OS << "static llvm::InstrItinerary NoItineraries[] = {};\n";
372 // EmitProcessorLookup - generate cpu name to itinerary lookup table.
374 void SubtargetEmitter::EmitProcessorLookup(std::ostream &OS) {
375 // Gather and sort processor information
376 std::vector<Record*> ProcessorList =
377 Records.getAllDerivedDefinitions("Processor");
378 sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
380 // Begin processor table
382 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
383 << "static const llvm::SubtargetInfoKV SubTypeInfoKV[] = {\n";
385 // For each processor
386 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
388 Record *Processor = ProcessorList[i];
390 std::string Name = Processor->getValueAsString("Name");
391 std::string ProcItin = Processor->getValueAsDef("ProcItin")->getName();
393 // Emit as { "cpu", procinit },
395 << "\"" << Name << "\", "
396 << "(void *)&" << ProcItin;
400 // Depending on ''if more in the list'' emit comma
401 if (++i < N) OS << ",";
406 // End processor table
409 // Emit size of table
411 OS<<" SubTypeInfoKVSize = sizeof(SubTypeInfoKV)/"
412 "sizeof(llvm::SubtargetInfoKV)\n";
417 // EmitData - Emits all stages and itineries, folding common patterns.
419 void SubtargetEmitter::EmitData(std::ostream &OS) {
420 std::map<std::string, unsigned> ItinClassesMap;
421 std::vector<std::vector<InstrItinerary> > ProcList;
423 // Enumerate all the itinerary classes
424 unsigned NItinClasses = CollectAllItinClasses(ItinClassesMap);
425 // Emit the stage data
426 EmitStageData(OS, NItinClasses, ItinClassesMap, ProcList);
427 // Emit the processor itinerary data
428 EmitProcessorData(OS, ProcList);
429 // Emit the processor lookup data
430 EmitProcessorLookup(OS);
434 // ParseFeaturesFunction - Produces a subtarget specific function for parsing
435 // the subtarget features string.
437 void SubtargetEmitter::ParseFeaturesFunction(std::ostream &OS) {
438 std::vector<Record*> Features =
439 Records.getAllDerivedDefinitions("SubtargetFeature");
440 sort(Features.begin(), Features.end(), LessRecord());
442 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
443 "// subtarget options.\n"
446 OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
447 " const std::string &CPU) {\n"
448 " SubtargetFeatures Features(FS);\n"
449 " Features.setCPUIfNone(CPU);\n"
450 " uint32_t Bits = Features.getBits(SubTypeKV, SubTypeKVSize,\n"
451 " FeatureKV, FeatureKVSize);\n";
453 for (unsigned i = 0; i < Features.size(); i++) {
455 Record *R = Features[i];
456 std::string Instance = R->getName();
457 std::string Name = R->getValueAsString("Name");
458 std::string Type = R->getValueAsString("Type");
459 std::string Attribute = R->getValueAsString("Attribute");
461 OS << " " << Attribute << " = (Bits & " << Instance << ") != 0;\n";
464 << " InstrItinerary *Itin = (InstrItinerary *)"
465 "Features.getInfo(SubTypeInfoKV, SubTypeInfoKVSize);\n";
470 // SubtargetEmitter::run - Main subtarget enumeration emitter.
472 void SubtargetEmitter::run(std::ostream &OS) {
473 Target = CodeGenTarget().getName();
475 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
477 OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
478 OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
480 Enumeration(OS, "FuncUnit", true);
482 // Enumeration(OS, "InstrItinClass", false);
484 Enumeration(OS, "SubtargetFeature", true);
486 FeatureKeyValues(OS);
492 ParseFeaturesFunction(OS);