blob: 1f833efcaac897d44968bef1ae033a4e750c2072 [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//
23// Record sort by name function.
24//
25struct LessRecord {
26 bool operator()(const Record *Rec1, const Record *Rec2) const {
27 return Rec1->getName() < Rec2->getName();
28 }
29};
30
31//
32// Record sort by field "Name" function.
33//
34struct LessRecordFieldName {
35 bool operator()(const Record *Rec1, const Record *Rec2) const {
36 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
37 }
38};
39
40//
41// Enumeration - Emit the specified class as an enumeration.
42//
43void SubtargetEmitter::Enumeration(std::ostream &OS,
44 const char *ClassName,
45 bool isBits) {
46 // Get all records of class and sort
47 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
48 std::sort(DefList.begin(), DefList.end(), LessRecord());
49
50 // Open enumeration
51 OS << "enum {\n";
52
53 // For each record
54 for (unsigned i = 0, N = DefList.size(); i < N;) {
55 // Next record
56 Record *Def = DefList[i];
57
58 // Get and emit name
59 OS << " " << Def->getName();
60
61 // If bit flags then emit expression (1 << i)
62 if (isBits) OS << " = " << " 1 << " << i;
63
64 // Depending on 'if more in the list' emit comma
65 if (++i < N) OS << ",";
66
67 OS << "\n";
68 }
69
70 // Close enumeration
71 OS << "};\n";
72}
73
74//
75// FeatureKeyValues - Emit data of all the subtarget features. Used by the
76// command line.
77//
78void SubtargetEmitter::FeatureKeyValues(std::ostream &OS) {
79 // Gather and sort all the features
80 std::vector<Record*> FeatureList =
81 Records.getAllDerivedDefinitions("SubtargetFeature");
82 std::sort(FeatureList.begin(), FeatureList.end(), LessRecord());
83
84 // Begin feature table
85 OS << "// Sorted (by key) array of values for CPU features.\n"
Dan Gohman12300e12008-03-25 21:45:14 +000086 << "static const llvm::SubtargetFeatureKV FeatureKV[] = {\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
88 // For each feature
89 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
90 // Next feature
91 Record *Feature = FeatureList[i];
92
93 const std::string &Name = Feature->getName();
94 const std::string &CommandLineName = Feature->getValueAsString("Name");
95 const std::string &Desc = Feature->getValueAsString("Desc");
96
97 if (CommandLineName.empty()) continue;
98
99 // Emit as { "feature", "decription", feactureEnum, i1 | i2 | ... | in }
100 OS << " { "
101 << "\"" << CommandLineName << "\", "
102 << "\"" << Desc << "\", "
103 << Name << ", ";
104
105 const std::vector<Record*> &ImpliesList =
106 Feature->getValueAsListOfDefs("Implies");
107
108 if (ImpliesList.empty()) {
109 OS << "0";
110 } else {
111 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
112 OS << ImpliesList[j]->getName();
113 if (++j < M) OS << " | ";
114 }
115 }
116
117 OS << " }";
118
119 // Depending on 'if more in the list' emit comma
120 if ((i + 1) < N) OS << ",";
121
122 OS << "\n";
123 }
124
125 // End feature table
126 OS << "};\n";
127
128 // Emit size of table
129 OS<<"\nenum {\n";
130 OS<<" FeatureKVSize = sizeof(FeatureKV)/sizeof(llvm::SubtargetFeatureKV)\n";
131 OS<<"};\n";
132}
133
134//
135// CPUKeyValues - Emit data of all the subtarget processors. Used by command
136// line.
137//
138void SubtargetEmitter::CPUKeyValues(std::ostream &OS) {
139 // Gather and sort processor information
140 std::vector<Record*> ProcessorList =
141 Records.getAllDerivedDefinitions("Processor");
142 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
143
144 // Begin processor table
145 OS << "// Sorted (by key) array of values for CPU subtype.\n"
146 << "static const llvm::SubtargetFeatureKV SubTypeKV[] = {\n";
147
148 // For each processor
149 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
150 // Next processor
151 Record *Processor = ProcessorList[i];
152
153 const std::string &Name = Processor->getValueAsString("Name");
154 const std::vector<Record*> &FeatureList =
155 Processor->getValueAsListOfDefs("Features");
156
157 // Emit as { "cpu", "description", f1 | f2 | ... fn },
158 OS << " { "
159 << "\"" << Name << "\", "
160 << "\"Select the " << Name << " processor\", ";
161
162 if (FeatureList.empty()) {
163 OS << "0";
164 } else {
165 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
166 OS << FeatureList[j]->getName();
167 if (++j < M) OS << " | ";
168 }
169 }
170
171 // The "0" is for the "implies" section of this data structure.
172 OS << ", 0 }";
173
174 // Depending on 'if more in the list' emit comma
175 if (++i < N) OS << ",";
176
177 OS << "\n";
178 }
179
180 // End processor table
181 OS << "};\n";
182
183 // Emit size of table
184 OS<<"\nenum {\n";
185 OS<<" SubTypeKVSize = sizeof(SubTypeKV)/sizeof(llvm::SubtargetFeatureKV)\n";
186 OS<<"};\n";
187}
188
189//
190// CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
191// Returns itinerary class count.
192//
193unsigned SubtargetEmitter::CollectAllItinClasses(std::ostream &OS,
194 std::map<std::string, unsigned> &ItinClassesMap) {
195 // Gather and sort all itinerary classes
196 std::vector<Record*> ItinClassList =
197 Records.getAllDerivedDefinitions("InstrItinClass");
198 std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
199
200 // For each itinerary class
201 unsigned N = ItinClassList.size();
202 for (unsigned i = 0; i < N; i++) {
203 // Next itinerary class
204 const Record *ItinClass = ItinClassList[i];
205 // Get name of itinerary class
206 // Assign itinerary class a unique number
207 ItinClassesMap[ItinClass->getName()] = i;
208 }
209
210 // Emit size of table
211 OS<<"\nenum {\n";
212 OS<<" ItinClassesSize = " << N << "\n";
213 OS<<"};\n";
214
215 // Return itinerary class count
216 return N;
217}
218
219//
220// FormItineraryString - Compose a string containing the data initialization
221// for the specified itinerary. N is the number of stages.
222//
223void SubtargetEmitter::FormItineraryString(Record *ItinData,
224 std::string &ItinString,
225 unsigned &NStages) {
226 // Get states list
227 const std::vector<Record*> &StageList =
228 ItinData->getValueAsListOfDefs("Stages");
229
230 // For each stage
231 unsigned N = NStages = StageList.size();
232 for (unsigned i = 0; i < N;) {
233 // Next stage
234 const Record *Stage = StageList[i];
235
236 // Form string as ,{ cycles, u1 | u2 | ... | un }
237 int Cycles = Stage->getValueAsInt("Cycles");
238 ItinString += " { " + itostr(Cycles) + ", ";
239
240 // Get unit list
241 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
242
243 // For each unit
244 for (unsigned j = 0, M = UnitList.size(); j < M;) {
245 // Add name and bitwise or
246 ItinString += UnitList[j]->getName();
247 if (++j < M) ItinString += " | ";
248 }
249
250 // Close off stage
251 ItinString += " }";
252 if (++i < N) ItinString += ", ";
253 }
254}
255
256//
257// EmitStageData - Generate unique itinerary stages. Record itineraries for
258// processors.
259//
260void SubtargetEmitter::EmitStageData(std::ostream &OS,
261 unsigned NItinClasses,
262 std::map<std::string, unsigned> &ItinClassesMap,
263 std::vector<std::vector<InstrItinerary> > &ProcList) {
264 // Gather processor iteraries
265 std::vector<Record*> ProcItinList =
266 Records.getAllDerivedDefinitions("ProcessorItineraries");
267
268 // If just no itinerary then don't bother
269 if (ProcItinList.size() < 2) return;
270
271 // Begin stages table
Dan Gohman12300e12008-03-25 21:45:14 +0000272 OS << "static const llvm::InstrStage Stages[] = {\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 " { 0, 0 }, // No itinerary\n";
274
Chris Lattner6af92442008-04-06 17:38:14 +0000275 unsigned StageCount = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 unsigned ItinEnum = 1;
277 std::map<std::string, unsigned> ItinMap;
278 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
279 // Next record
280 Record *Proc = ProcItinList[i];
281
282 // Get processor itinerary name
283 const std::string &Name = Proc->getName();
284
285 // Skip default
286 if (Name == "NoItineraries") continue;
287
288 // Create and expand processor itinerary to cover all itinerary classes
289 std::vector<InstrItinerary> ItinList;
290 ItinList.resize(NItinClasses);
291
292 // Get itinerary data list
293 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
294
295 // For each itinerary data
296 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
297 // Next itinerary data
298 Record *ItinData = ItinDataList[j];
299
300 // Get string and stage count
301 std::string ItinString;
302 unsigned NStages;
303 FormItineraryString(ItinData, ItinString, NStages);
304
305 // Check to see if it already exists
306 unsigned Find = ItinMap[ItinString];
307
308 // If new itinerary
309 if (Find == 0) {
310 // Emit as { cycles, u1 | u2 | ... | un }, // index
311 OS << ItinString << ", // " << ItinEnum << "\n";
Chris Lattner6af92442008-04-06 17:38:14 +0000312 // Record Itin class number.
313 ItinMap[ItinString] = Find = StageCount;
314 StageCount += NStages;
315 ItinEnum++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 }
317
318 // Set up itinerary as location and location + stage count
319 InstrItinerary Intinerary = { Find, Find + NStages };
320
321 // Locate where to inject into processor itinerary table
322 const std::string &Name = ItinData->getValueAsDef("TheClass")->getName();
323 Find = ItinClassesMap[Name];
324
325 // Inject - empty slots will be 0, 0
326 ItinList[Find] = Intinerary;
327 }
328
329 // Add process itinerary to list
330 ProcList.push_back(ItinList);
331 }
332
333 // Closing stage
334 OS << " { 0, 0 } // End itinerary\n";
335 // End stages table
336 OS << "};\n";
337
338 // Emit size of table
339 OS<<"\nenum {\n";
340 OS<<" StagesSize = sizeof(Stages)/sizeof(llvm::InstrStage)\n";
341 OS<<"};\n";
342}
343
344//
345// EmitProcessorData - Generate data for processor itineraries.
346//
347void SubtargetEmitter::EmitProcessorData(std::ostream &OS,
348 std::vector<std::vector<InstrItinerary> > &ProcList) {
349 // Get an iterator for processor itinerary stages
350 std::vector<std::vector<InstrItinerary> >::iterator
351 ProcListIter = ProcList.begin();
352
353 // For each processor itinerary
354 std::vector<Record*> Itins =
355 Records.getAllDerivedDefinitions("ProcessorItineraries");
356 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
357 // Next record
358 Record *Itin = Itins[i];
359
360 // Get processor itinerary name
361 const std::string &Name = Itin->getName();
362
363 // Skip default
364 if (Name == "NoItineraries") continue;
365
366 // Begin processor itinerary table
367 OS << "\n";
Dan Gohman12300e12008-03-25 21:45:14 +0000368 OS << "static const llvm::InstrItinerary " << Name << "[] = {\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369
370 // For each itinerary class
371 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
372 for (unsigned j = 0, M = ItinList.size(); j < M;) {
373 InstrItinerary &Intinerary = ItinList[j];
374
375 // Emit in the form of { first, last } // index
376 if (Intinerary.First == 0) {
377 OS << " { 0, 0 }";
378 } else {
379 OS << " { " << Intinerary.First << ", " << Intinerary.Last << " }";
380 }
381
382 // If more in list add comma
383 if (++j < M) OS << ",";
384
385 OS << " // " << (j - 1) << "\n";
386 }
387
388 // End processor itinerary table
389 OS << "};\n";
390 }
391}
392
393//
394// EmitProcessorLookup - generate cpu name to itinerary lookup table.
395//
396void SubtargetEmitter::EmitProcessorLookup(std::ostream &OS) {
397 // Gather and sort processor information
398 std::vector<Record*> ProcessorList =
399 Records.getAllDerivedDefinitions("Processor");
400 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
401
402 // Begin processor table
403 OS << "\n";
404 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
405 << "static const llvm::SubtargetInfoKV ProcItinKV[] = {\n";
406
407 // For each processor
408 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
409 // Next processor
410 Record *Processor = ProcessorList[i];
411
412 const std::string &Name = Processor->getValueAsString("Name");
413 const std::string &ProcItin =
414 Processor->getValueAsDef("ProcItin")->getName();
415
416 // Emit as { "cpu", procinit },
417 OS << " { "
418 << "\"" << Name << "\", "
419 << "(void *)&" << ProcItin;
420
421 OS << " }";
422
423 // Depending on ''if more in the list'' emit comma
424 if (++i < N) OS << ",";
425
426 OS << "\n";
427 }
428
429 // End processor table
430 OS << "};\n";
431
432 // Emit size of table
433 OS<<"\nenum {\n";
434 OS<<" ProcItinKVSize = sizeof(ProcItinKV)/"
435 "sizeof(llvm::SubtargetInfoKV)\n";
436 OS<<"};\n";
437}
438
439//
440// EmitData - Emits all stages and itineries, folding common patterns.
441//
442void SubtargetEmitter::EmitData(std::ostream &OS) {
443 std::map<std::string, unsigned> ItinClassesMap;
444 std::vector<std::vector<InstrItinerary> > ProcList;
445
446 // Enumerate all the itinerary classes
447 unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap);
448 // Make sure the rest is worth the effort
449 HasItineraries = NItinClasses != 1; // Ignore NoItinerary.
450
451 if (HasItineraries) {
452 // Emit the stage data
453 EmitStageData(OS, NItinClasses, ItinClassesMap, ProcList);
454 // Emit the processor itinerary data
455 EmitProcessorData(OS, ProcList);
456 // Emit the processor lookup data
457 EmitProcessorLookup(OS);
458 }
459}
460
461//
462// ParseFeaturesFunction - Produces a subtarget specific function for parsing
463// the subtarget features string.
464//
465void SubtargetEmitter::ParseFeaturesFunction(std::ostream &OS) {
466 std::vector<Record*> Features =
467 Records.getAllDerivedDefinitions("SubtargetFeature");
468 std::sort(Features.begin(), Features.end(), LessRecord());
469
470 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
471 << "// subtarget options.\n"
472 << "void llvm::";
473 OS << Target;
474 OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
475 << " const std::string &CPU) {\n"
476 << " SubtargetFeatures Features(FS);\n"
477 << " Features.setCPUIfNone(CPU);\n"
478 << " uint32_t Bits = Features.getBits(SubTypeKV, SubTypeKVSize,\n"
479 << " FeatureKV, FeatureKVSize);\n";
480
481 for (unsigned i = 0; i < Features.size(); i++) {
482 // Next record
483 Record *R = Features[i];
484 const std::string &Instance = R->getName();
485 const std::string &Value = R->getValueAsString("Value");
486 const std::string &Attribute = R->getValueAsString("Attribute");
487
Dale Johannesen161badc2008-02-14 23:35:16 +0000488 if (Value=="true" || Value=="false")
489 OS << " if ((Bits & " << Instance << ") != 0) "
490 << Attribute << " = " << Value << ";\n";
491 else
492 OS << " if ((Bits & " << Instance << ") != 0 && " << Attribute <<
493 " < " << Value << ") " << Attribute << " = " << Value << ";\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 }
495
496 if (HasItineraries) {
497 OS << "\n"
498 << " InstrItinerary *Itinerary = (InstrItinerary *)"
499 << "Features.getInfo(ProcItinKV, ProcItinKVSize);\n"
500 << " InstrItins = InstrItineraryData(Stages, Itinerary);\n";
501 }
502
503 OS << "}\n";
504}
505
506//
507// SubtargetEmitter::run - Main subtarget enumeration emitter.
508//
509void SubtargetEmitter::run(std::ostream &OS) {
510 Target = CodeGenTarget().getName();
511
512 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
513
514 OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
515 OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
516
517 Enumeration(OS, "FuncUnit", true);
518 OS<<"\n";
519// Enumeration(OS, "InstrItinClass", false);
520// OS<<"\n";
521 Enumeration(OS, "SubtargetFeature", true);
522 OS<<"\n";
523 FeatureKeyValues(OS);
524 OS<<"\n";
525 CPUKeyValues(OS);
526 OS<<"\n";
527 EmitData(OS);
528 OS<<"\n";
529 ParseFeaturesFunction(OS);
530}