blob: 4a0bacd19cf9f307f3a53bdfab922c0bd10f9ca4 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerfd6c2f02007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits subtarget enumerations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SubtargetEmitter.h"
15#include "CodeGenTarget.h"
16#include "Record.h"
17#include "llvm/ADT/StringExtras.h"
18#include "llvm/Support/Debug.h"
19#include <algorithm>
20using namespace llvm;
21
22//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023// Enumeration - Emit the specified class as an enumeration.
24//
Daniel Dunbard4287062009-07-03 00:10:29 +000025void SubtargetEmitter::Enumeration(raw_ostream &OS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026 const char *ClassName,
27 bool isBits) {
28 // Get all records of class and sort
29 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
30 std::sort(DefList.begin(), DefList.end(), LessRecord());
31
32 // Open enumeration
33 OS << "enum {\n";
34
35 // For each record
36 for (unsigned i = 0, N = DefList.size(); i < N;) {
37 // Next record
38 Record *Def = DefList[i];
39
40 // Get and emit name
41 OS << " " << Def->getName();
42
43 // If bit flags then emit expression (1 << i)
44 if (isBits) OS << " = " << " 1 << " << i;
45
46 // Depending on 'if more in the list' emit comma
47 if (++i < N) OS << ",";
48
49 OS << "\n";
50 }
51
52 // Close enumeration
53 OS << "};\n";
54}
55
56//
57// FeatureKeyValues - Emit data of all the subtarget features. Used by the
58// command line.
59//
Daniel Dunbard4287062009-07-03 00:10:29 +000060void SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 // Gather and sort all the features
62 std::vector<Record*> FeatureList =
63 Records.getAllDerivedDefinitions("SubtargetFeature");
Jim Grosbach36ed4552008-09-11 17:05:32 +000064 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065
66 // Begin feature table
67 OS << "// Sorted (by key) array of values for CPU features.\n"
Dan Gohman12300e12008-03-25 21:45:14 +000068 << "static const llvm::SubtargetFeatureKV FeatureKV[] = {\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069
70 // For each feature
71 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
72 // Next feature
73 Record *Feature = FeatureList[i];
74
75 const std::string &Name = Feature->getName();
76 const std::string &CommandLineName = Feature->getValueAsString("Name");
77 const std::string &Desc = Feature->getValueAsString("Desc");
78
79 if (CommandLineName.empty()) continue;
80
Jim Grosbachda95ec82009-03-26 16:17:51 +000081 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 OS << " { "
83 << "\"" << CommandLineName << "\", "
84 << "\"" << Desc << "\", "
85 << Name << ", ";
86
87 const std::vector<Record*> &ImpliesList =
88 Feature->getValueAsListOfDefs("Implies");
89
90 if (ImpliesList.empty()) {
91 OS << "0";
92 } else {
93 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
94 OS << ImpliesList[j]->getName();
95 if (++j < M) OS << " | ";
96 }
97 }
98
99 OS << " }";
100
101 // Depending on 'if more in the list' emit comma
102 if ((i + 1) < N) OS << ",";
103
104 OS << "\n";
105 }
106
107 // End feature table
108 OS << "};\n";
109
110 // Emit size of table
111 OS<<"\nenum {\n";
112 OS<<" FeatureKVSize = sizeof(FeatureKV)/sizeof(llvm::SubtargetFeatureKV)\n";
113 OS<<"};\n";
114}
115
116//
117// CPUKeyValues - Emit data of all the subtarget processors. Used by command
118// line.
119//
Daniel Dunbard4287062009-07-03 00:10:29 +0000120void SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 // Gather and sort processor information
122 std::vector<Record*> ProcessorList =
123 Records.getAllDerivedDefinitions("Processor");
124 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
125
126 // Begin processor table
127 OS << "// Sorted (by key) array of values for CPU subtype.\n"
128 << "static const llvm::SubtargetFeatureKV SubTypeKV[] = {\n";
129
130 // For each processor
131 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
132 // Next processor
133 Record *Processor = ProcessorList[i];
134
135 const std::string &Name = Processor->getValueAsString("Name");
136 const std::vector<Record*> &FeatureList =
137 Processor->getValueAsListOfDefs("Features");
138
139 // Emit as { "cpu", "description", f1 | f2 | ... fn },
140 OS << " { "
141 << "\"" << Name << "\", "
142 << "\"Select the " << Name << " processor\", ";
143
144 if (FeatureList.empty()) {
145 OS << "0";
146 } else {
147 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
148 OS << FeatureList[j]->getName();
149 if (++j < M) OS << " | ";
150 }
151 }
152
153 // The "0" is for the "implies" section of this data structure.
154 OS << ", 0 }";
155
156 // Depending on 'if more in the list' emit comma
157 if (++i < N) OS << ",";
158
159 OS << "\n";
160 }
161
162 // End processor table
163 OS << "};\n";
164
165 // Emit size of table
166 OS<<"\nenum {\n";
167 OS<<" SubTypeKVSize = sizeof(SubTypeKV)/sizeof(llvm::SubtargetFeatureKV)\n";
168 OS<<"};\n";
169}
170
171//
172// CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
173// Returns itinerary class count.
174//
Daniel Dunbard4287062009-07-03 00:10:29 +0000175unsigned SubtargetEmitter::CollectAllItinClasses(raw_ostream &OS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 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());
181
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;
190 }
191
192 // Emit size of table
193 OS<<"\nenum {\n";
194 OS<<" ItinClassesSize = " << N << "\n";
195 OS<<"};\n";
196
197 // Return itinerary class count
198 return N;
199}
200
201//
202// FormItineraryString - Compose a string containing the data initialization
203// for the specified itinerary. N is the number of stages.
204//
205void SubtargetEmitter::FormItineraryString(Record *ItinData,
206 std::string &ItinString,
207 unsigned &NStages) {
208 // Get states list
209 const std::vector<Record*> &StageList =
210 ItinData->getValueAsListOfDefs("Stages");
211
212 // For each stage
213 unsigned N = NStages = StageList.size();
214 for (unsigned i = 0; i < N;) {
215 // Next stage
216 const Record *Stage = StageList[i];
217
David Goodwin4b6e4982009-08-12 18:31:53 +0000218 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 int Cycles = Stage->getValueAsInt("Cycles");
220 ItinString += " { " + itostr(Cycles) + ", ";
221
222 // Get unit list
223 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
224
225 // For each unit
226 for (unsigned j = 0, M = UnitList.size(); j < M;) {
227 // Add name and bitwise or
228 ItinString += UnitList[j]->getName();
229 if (++j < M) ItinString += " | ";
230 }
231
David Goodwin4b6e4982009-08-12 18:31:53 +0000232 int TimeInc = Stage->getValueAsInt("TimeInc");
233 ItinString += ", " + itostr(TimeInc);
234
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 // Close off stage
236 ItinString += " }";
237 if (++i < N) ItinString += ", ";
238 }
239}
240
241//
242// EmitStageData - Generate unique itinerary stages. Record itineraries for
243// processors.
244//
Daniel Dunbard4287062009-07-03 00:10:29 +0000245void SubtargetEmitter::EmitStageData(raw_ostream &OS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 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");
252
253 // If just no itinerary then don't bother
254 if (ProcItinList.size() < 2) return;
255
256 // Begin stages table
Dan Gohman12300e12008-03-25 21:45:14 +0000257 OS << "static const llvm::InstrStage Stages[] = {\n"
David Goodwin4b6e4982009-08-12 18:31:53 +0000258 " { 0, 0, 0 }, // No itinerary\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259
Chris Lattner6af92442008-04-06 17:38:14 +0000260 unsigned StageCount = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 unsigned ItinEnum = 1;
262 std::map<std::string, unsigned> ItinMap;
263 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
264 // Next record
265 Record *Proc = ProcItinList[i];
266
267 // Get processor itinerary name
268 const std::string &Name = Proc->getName();
269
270 // Skip default
271 if (Name == "NoItineraries") continue;
272
273 // Create and expand processor itinerary to cover all itinerary classes
274 std::vector<InstrItinerary> ItinList;
275 ItinList.resize(NItinClasses);
276
277 // Get itinerary data list
278 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
279
280 // For each itinerary data
281 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
282 // Next itinerary data
283 Record *ItinData = ItinDataList[j];
284
285 // Get string and stage count
286 std::string ItinString;
287 unsigned NStages;
288 FormItineraryString(ItinData, ItinString, NStages);
289
290 // Check to see if it already exists
291 unsigned Find = ItinMap[ItinString];
292
293 // If new itinerary
294 if (Find == 0) {
David Goodwin4b6e4982009-08-12 18:31:53 +0000295 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // index
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 OS << ItinString << ", // " << ItinEnum << "\n";
Chris Lattner6af92442008-04-06 17:38:14 +0000297 // Record Itin class number.
298 ItinMap[ItinString] = Find = StageCount;
299 StageCount += NStages;
300 ItinEnum++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 }
302
303 // Set up itinerary as location and location + stage count
304 InstrItinerary Intinerary = { Find, Find + NStages };
305
306 // Locate where to inject into processor itinerary table
307 const std::string &Name = ItinData->getValueAsDef("TheClass")->getName();
308 Find = ItinClassesMap[Name];
309
310 // Inject - empty slots will be 0, 0
311 ItinList[Find] = Intinerary;
312 }
313
314 // Add process itinerary to list
315 ProcList.push_back(ItinList);
316 }
317
318 // Closing stage
David Goodwin4b6e4982009-08-12 18:31:53 +0000319 OS << " { 0, 0, 0 } // End itinerary\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 // End stages table
321 OS << "};\n";
322
323 // Emit size of table
324 OS<<"\nenum {\n";
325 OS<<" StagesSize = sizeof(Stages)/sizeof(llvm::InstrStage)\n";
326 OS<<"};\n";
327}
328
329//
330// EmitProcessorData - Generate data for processor itineraries.
331//
Daniel Dunbard4287062009-07-03 00:10:29 +0000332void SubtargetEmitter::EmitProcessorData(raw_ostream &OS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 std::vector<std::vector<InstrItinerary> > &ProcList) {
334 // Get an iterator for processor itinerary stages
335 std::vector<std::vector<InstrItinerary> >::iterator
336 ProcListIter = ProcList.begin();
337
338 // For each processor itinerary
339 std::vector<Record*> Itins =
340 Records.getAllDerivedDefinitions("ProcessorItineraries");
341 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
342 // Next record
343 Record *Itin = Itins[i];
344
345 // Get processor itinerary name
346 const std::string &Name = Itin->getName();
347
348 // Skip default
349 if (Name == "NoItineraries") continue;
350
351 // Begin processor itinerary table
352 OS << "\n";
Dan Gohman12300e12008-03-25 21:45:14 +0000353 OS << "static const llvm::InstrItinerary " << Name << "[] = {\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354
355 // For each itinerary class
356 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
357 for (unsigned j = 0, M = ItinList.size(); j < M;) {
358 InstrItinerary &Intinerary = ItinList[j];
359
360 // Emit in the form of { first, last } // index
361 if (Intinerary.First == 0) {
362 OS << " { 0, 0 }";
363 } else {
364 OS << " { " << Intinerary.First << ", " << Intinerary.Last << " }";
365 }
366
367 // If more in list add comma
368 if (++j < M) OS << ",";
369
370 OS << " // " << (j - 1) << "\n";
371 }
372
373 // End processor itinerary table
374 OS << "};\n";
375 }
376}
377
378//
379// EmitProcessorLookup - generate cpu name to itinerary lookup table.
380//
Daniel Dunbard4287062009-07-03 00:10:29 +0000381void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382 // Gather and sort processor information
383 std::vector<Record*> ProcessorList =
384 Records.getAllDerivedDefinitions("Processor");
385 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
386
387 // Begin processor table
388 OS << "\n";
389 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
390 << "static const llvm::SubtargetInfoKV ProcItinKV[] = {\n";
391
392 // For each processor
393 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
394 // Next processor
395 Record *Processor = ProcessorList[i];
396
397 const std::string &Name = Processor->getValueAsString("Name");
398 const std::string &ProcItin =
399 Processor->getValueAsDef("ProcItin")->getName();
400
401 // Emit as { "cpu", procinit },
402 OS << " { "
403 << "\"" << Name << "\", "
404 << "(void *)&" << ProcItin;
405
406 OS << " }";
407
408 // Depending on ''if more in the list'' emit comma
409 if (++i < N) OS << ",";
410
411 OS << "\n";
412 }
413
414 // End processor table
415 OS << "};\n";
416
417 // Emit size of table
418 OS<<"\nenum {\n";
419 OS<<" ProcItinKVSize = sizeof(ProcItinKV)/"
420 "sizeof(llvm::SubtargetInfoKV)\n";
421 OS<<"};\n";
422}
423
424//
425// EmitData - Emits all stages and itineries, folding common patterns.
426//
Daniel Dunbard4287062009-07-03 00:10:29 +0000427void SubtargetEmitter::EmitData(raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 std::map<std::string, unsigned> ItinClassesMap;
429 std::vector<std::vector<InstrItinerary> > ProcList;
430
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.
435
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);
443 }
444}
445
446//
447// ParseFeaturesFunction - Produces a subtarget specific function for parsing
448// the subtarget features string.
449//
Daniel Dunbard4287062009-07-03 00:10:29 +0000450void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 std::vector<Record*> Features =
452 Records.getAllDerivedDefinitions("SubtargetFeature");
453 std::sort(Features.begin(), Features.end(), LessRecord());
454
455 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
456 << "// subtarget options.\n"
Anton Korobeynikov7357d8f2009-05-23 19:50:50 +0000457 << "std::string llvm::";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 OS << Target;
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";
465
466 for (unsigned i = 0; i < Features.size(); i++) {
467 // Next record
468 Record *R = Features[i];
469 const std::string &Instance = R->getName();
470 const std::string &Value = R->getValueAsString("Value");
471 const std::string &Attribute = R->getValueAsString("Attribute");
472
Dale Johannesen161badc2008-02-14 23:35:16 +0000473 if (Value=="true" || Value=="false")
474 OS << " if ((Bits & " << Instance << ") != 0) "
475 << Attribute << " = " << Value << ";\n";
476 else
477 OS << " if ((Bits & " << Instance << ") != 0 && " << Attribute <<
478 " < " << Value << ") " << Attribute << " = " << Value << ";\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 }
480
481 if (HasItineraries) {
482 OS << "\n"
483 << " InstrItinerary *Itinerary = (InstrItinerary *)"
484 << "Features.getInfo(ProcItinKV, ProcItinKVSize);\n"
485 << " InstrItins = InstrItineraryData(Stages, Itinerary);\n";
486 }
Anton Korobeynikov7357d8f2009-05-23 19:50:50 +0000487
488 OS << " return Features.getCPU();\n"
489 << "}\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490}
491
Anton Korobeynikov7357d8f2009-05-23 19:50:50 +0000492//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493// SubtargetEmitter::run - Main subtarget enumeration emitter.
494//
Daniel Dunbard4287062009-07-03 00:10:29 +0000495void SubtargetEmitter::run(raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 Target = CodeGenTarget().getName();
497
498 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
499
500 OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
501 OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
502
503 Enumeration(OS, "FuncUnit", true);
504 OS<<"\n";
505// Enumeration(OS, "InstrItinClass", false);
506// OS<<"\n";
507 Enumeration(OS, "SubtargetFeature", true);
508 OS<<"\n";
509 FeatureKeyValues(OS);
510 OS<<"\n";
511 CPUKeyValues(OS);
512 OS<<"\n";
513 EmitData(OS);
514 OS<<"\n";
515 ParseFeaturesFunction(OS);
516}