blob: b04eaf88f73a3ddd1013f516dd195ae3baf52a8c [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//
David Goodwine0106972009-08-17 16:02:57 +0000202// FormItineraryStageString - Compose a string containing the stage
203// data initialization for the specified itinerary. N is the number
204// of stages.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205//
Anton Korobeynikov00540072010-04-18 20:31:01 +0000206void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
207 Record *ItinData,
David Goodwine0106972009-08-17 16:02:57 +0000208 std::string &ItinString,
209 unsigned &NStages) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 // Get states list
211 const std::vector<Record*> &StageList =
212 ItinData->getValueAsListOfDefs("Stages");
213
214 // For each stage
215 unsigned N = NStages = StageList.size();
216 for (unsigned i = 0; i < N;) {
217 // Next stage
218 const Record *Stage = StageList[i];
219
Anton Korobeynikov9cc241a2010-04-07 18:19:32 +0000220 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 int Cycles = Stage->getValueAsInt("Cycles");
222 ItinString += " { " + itostr(Cycles) + ", ";
223
224 // Get unit list
225 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
226
227 // For each unit
228 for (unsigned j = 0, M = UnitList.size(); j < M;) {
229 // Add name and bitwise or
Anton Korobeynikov00540072010-04-18 20:31:01 +0000230 ItinString += Name + "FU::" + UnitList[j]->getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 if (++j < M) ItinString += " | ";
232 }
233
David Goodwin4b6e4982009-08-12 18:31:53 +0000234 int TimeInc = Stage->getValueAsInt("TimeInc");
235 ItinString += ", " + itostr(TimeInc);
236
Anton Korobeynikov9cc241a2010-04-07 18:19:32 +0000237 int Kind = Stage->getValueAsInt("Kind");
238 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
239
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 // Close off stage
241 ItinString += " }";
242 if (++i < N) ItinString += ", ";
243 }
244}
245
246//
David Goodwine0106972009-08-17 16:02:57 +0000247// FormItineraryOperandCycleString - Compose a string containing the
248// operand cycle initialization for the specified itinerary. N is the
249// number of operands that has cycles specified.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250//
David Goodwine0106972009-08-17 16:02:57 +0000251void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
252 std::string &ItinString, unsigned &NOperandCycles) {
253 // Get operand cycle list
254 const std::vector<int64_t> &OperandCycleList =
255 ItinData->getValueAsListOfInts("OperandCycles");
256
257 // For each operand cycle
258 unsigned N = NOperandCycles = OperandCycleList.size();
259 for (unsigned i = 0; i < N;) {
260 // Next operand cycle
261 const int OCycle = OperandCycleList[i];
262
263 ItinString += " " + itostr(OCycle);
264 if (++i < N) ItinString += ", ";
265 }
266}
267
268//
269// EmitStageAndOperandCycleData - Generate unique itinerary stages and
270// operand cycle tables. Record itineraries for processors.
271//
272void SubtargetEmitter::EmitStageAndOperandCycleData(raw_ostream &OS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 unsigned NItinClasses,
274 std::map<std::string, unsigned> &ItinClassesMap,
275 std::vector<std::vector<InstrItinerary> > &ProcList) {
276 // Gather processor iteraries
277 std::vector<Record*> ProcItinList =
278 Records.getAllDerivedDefinitions("ProcessorItineraries");
279
280 // If just no itinerary then don't bother
281 if (ProcItinList.size() < 2) return;
282
Anton Korobeynikov00540072010-04-18 20:31:01 +0000283 // Emit functional units for all the itineraries.
284 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
285 // Next record
286 Record *Proc = ProcItinList[i];
287
288 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
289 if (FUs.empty())
290 continue;
291
292 const std::string &Name = Proc->getName();
293 OS << "\n// Functional units for itineraries \"" << Name << "\"\n"
294 << "namespace " << Name << "FU {\n";
295
296 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
297 OS << " const unsigned " << FUs[j]->getName()
298 << " = 1 << " << j << ";\n";
299
300 OS << "}\n";
301 }
302
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 // Begin stages table
Anton Korobeynikov00540072010-04-18 20:31:01 +0000304 std::string StageTable = "\nstatic const llvm::InstrStage Stages[] = {\n";
Anton Korobeynikov9cc241a2010-04-07 18:19:32 +0000305 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306
David Goodwine0106972009-08-17 16:02:57 +0000307 // Begin operand cycle table
308 std::string OperandCycleTable = "static const unsigned OperandCycles[] = {\n";
309 OperandCycleTable += " 0, // No itinerary\n";
310
311 unsigned StageCount = 1, OperandCycleCount = 1;
312 unsigned ItinStageEnum = 1, ItinOperandCycleEnum = 1;
313 std::map<std::string, unsigned> ItinStageMap, ItinOperandCycleMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
315 // Next record
316 Record *Proc = ProcItinList[i];
317
318 // Get processor itinerary name
319 const std::string &Name = Proc->getName();
320
321 // Skip default
322 if (Name == "NoItineraries") continue;
323
324 // Create and expand processor itinerary to cover all itinerary classes
325 std::vector<InstrItinerary> ItinList;
326 ItinList.resize(NItinClasses);
327
328 // Get itinerary data list
329 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
330
331 // For each itinerary data
332 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
333 // Next itinerary data
334 Record *ItinData = ItinDataList[j];
335
336 // Get string and stage count
David Goodwine0106972009-08-17 16:02:57 +0000337 std::string ItinStageString;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 unsigned NStages;
Anton Korobeynikov00540072010-04-18 20:31:01 +0000339 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340
David Goodwine0106972009-08-17 16:02:57 +0000341 // Get string and operand cycle count
342 std::string ItinOperandCycleString;
343 unsigned NOperandCycles;
344 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
345 NOperandCycles);
346
347 // Check to see if stage already exists and create if it doesn't
348 unsigned FindStage = 0;
349 if (NStages > 0) {
350 FindStage = ItinStageMap[ItinStageString];
351 if (FindStage == 0) {
352 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // index
353 StageTable += ItinStageString + ", // " + itostr(ItinStageEnum) + "\n";
354 // Record Itin class number.
355 ItinStageMap[ItinStageString] = FindStage = StageCount;
356 StageCount += NStages;
357 ItinStageEnum++;
358 }
359 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360
David Goodwine0106972009-08-17 16:02:57 +0000361 // Check to see if operand cycle already exists and create if it doesn't
362 unsigned FindOperandCycle = 0;
363 if (NOperandCycles > 0) {
364 FindOperandCycle = ItinOperandCycleMap[ItinOperandCycleString];
365 if (FindOperandCycle == 0) {
366 // Emit as cycle, // index
367 OperandCycleTable += ItinOperandCycleString + ", // " +
368 itostr(ItinOperandCycleEnum) + "\n";
369 // Record Itin class number.
370 ItinOperandCycleMap[ItinOperandCycleString] =
371 FindOperandCycle = OperandCycleCount;
372 OperandCycleCount += NOperandCycles;
373 ItinOperandCycleEnum++;
374 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 }
376
377 // Set up itinerary as location and location + stage count
David Goodwine0106972009-08-17 16:02:57 +0000378 InstrItinerary Intinerary = { FindStage, FindStage + NStages,
379 FindOperandCycle, FindOperandCycle + NOperandCycles};
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380
381 // Locate where to inject into processor itinerary table
382 const std::string &Name = ItinData->getValueAsDef("TheClass")->getName();
David Goodwine0106972009-08-17 16:02:57 +0000383 unsigned Find = ItinClassesMap[Name];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384
385 // Inject - empty slots will be 0, 0
386 ItinList[Find] = Intinerary;
387 }
388
389 // Add process itinerary to list
390 ProcList.push_back(ItinList);
391 }
392
393 // Closing stage
Anton Korobeynikov9cc241a2010-04-07 18:19:32 +0000394 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End itinerary\n";
David Goodwine0106972009-08-17 16:02:57 +0000395 StageTable += "};\n";
396
397 // Closing operand cycles
398 OperandCycleTable += " 0 // End itinerary\n";
399 OperandCycleTable += "};\n";
400
401 // Emit tables.
402 OS << StageTable;
403 OS << OperandCycleTable;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404
David Goodwine0106972009-08-17 16:02:57 +0000405 // Emit size of tables
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 OS<<"\nenum {\n";
David Goodwine0106972009-08-17 16:02:57 +0000407 OS<<" StagesSize = sizeof(Stages)/sizeof(llvm::InstrStage),\n";
408 OS<<" OperandCyclesSize = sizeof(OperandCycles)/sizeof(unsigned)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 OS<<"};\n";
410}
411
412//
413// EmitProcessorData - Generate data for processor itineraries.
414//
Daniel Dunbard4287062009-07-03 00:10:29 +0000415void SubtargetEmitter::EmitProcessorData(raw_ostream &OS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 std::vector<std::vector<InstrItinerary> > &ProcList) {
417 // Get an iterator for processor itinerary stages
418 std::vector<std::vector<InstrItinerary> >::iterator
419 ProcListIter = ProcList.begin();
420
421 // For each processor itinerary
422 std::vector<Record*> Itins =
423 Records.getAllDerivedDefinitions("ProcessorItineraries");
424 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
425 // Next record
426 Record *Itin = Itins[i];
427
428 // Get processor itinerary name
429 const std::string &Name = Itin->getName();
430
431 // Skip default
432 if (Name == "NoItineraries") continue;
433
434 // Begin processor itinerary table
435 OS << "\n";
Dan Gohman12300e12008-03-25 21:45:14 +0000436 OS << "static const llvm::InstrItinerary " << Name << "[] = {\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437
438 // For each itinerary class
439 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
David Goodwined174992009-09-24 20:22:50 +0000440 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 InstrItinerary &Intinerary = ItinList[j];
442
David Goodwine0106972009-08-17 16:02:57 +0000443 // Emit in the form of
444 // { firstStage, lastStage, firstCycle, lastCycle } // index
445 if (Intinerary.FirstStage == 0) {
446 OS << " { 0, 0, 0, 0 }";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 } else {
David Goodwine0106972009-08-17 16:02:57 +0000448 OS << " { " << Intinerary.FirstStage << ", " <<
449 Intinerary.LastStage << ", " <<
450 Intinerary.FirstOperandCycle << ", " <<
451 Intinerary.LastOperandCycle << " }";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 }
453
David Goodwined174992009-09-24 20:22:50 +0000454 OS << ", // " << j << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 }
456
457 // End processor itinerary table
David Goodwined174992009-09-24 20:22:50 +0000458 OS << " { ~0U, ~0U, ~0U, ~0U } // end marker\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 OS << "};\n";
460 }
461}
462
463//
464// EmitProcessorLookup - generate cpu name to itinerary lookup table.
465//
Daniel Dunbard4287062009-07-03 00:10:29 +0000466void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 // Gather and sort processor information
468 std::vector<Record*> ProcessorList =
469 Records.getAllDerivedDefinitions("Processor");
470 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
471
472 // Begin processor table
473 OS << "\n";
474 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
475 << "static const llvm::SubtargetInfoKV ProcItinKV[] = {\n";
476
477 // For each processor
478 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
479 // Next processor
480 Record *Processor = ProcessorList[i];
481
482 const std::string &Name = Processor->getValueAsString("Name");
483 const std::string &ProcItin =
484 Processor->getValueAsDef("ProcItin")->getName();
485
486 // Emit as { "cpu", procinit },
487 OS << " { "
488 << "\"" << Name << "\", "
489 << "(void *)&" << ProcItin;
490
491 OS << " }";
492
493 // Depending on ''if more in the list'' emit comma
494 if (++i < N) OS << ",";
495
496 OS << "\n";
497 }
498
499 // End processor table
500 OS << "};\n";
501
502 // Emit size of table
503 OS<<"\nenum {\n";
504 OS<<" ProcItinKVSize = sizeof(ProcItinKV)/"
505 "sizeof(llvm::SubtargetInfoKV)\n";
506 OS<<"};\n";
507}
508
509//
510// EmitData - Emits all stages and itineries, folding common patterns.
511//
Daniel Dunbard4287062009-07-03 00:10:29 +0000512void SubtargetEmitter::EmitData(raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 std::map<std::string, unsigned> ItinClassesMap;
514 std::vector<std::vector<InstrItinerary> > ProcList;
515
516 // Enumerate all the itinerary classes
517 unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap);
518 // Make sure the rest is worth the effort
519 HasItineraries = NItinClasses != 1; // Ignore NoItinerary.
520
521 if (HasItineraries) {
522 // Emit the stage data
David Goodwine0106972009-08-17 16:02:57 +0000523 EmitStageAndOperandCycleData(OS, NItinClasses, ItinClassesMap, ProcList);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 // Emit the processor itinerary data
525 EmitProcessorData(OS, ProcList);
526 // Emit the processor lookup data
527 EmitProcessorLookup(OS);
528 }
529}
530
531//
532// ParseFeaturesFunction - Produces a subtarget specific function for parsing
533// the subtarget features string.
534//
Daniel Dunbard4287062009-07-03 00:10:29 +0000535void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 std::vector<Record*> Features =
537 Records.getAllDerivedDefinitions("SubtargetFeature");
538 std::sort(Features.begin(), Features.end(), LessRecord());
539
540 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
541 << "// subtarget options.\n"
Anton Korobeynikov7357d8f2009-05-23 19:50:50 +0000542 << "std::string llvm::";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 OS << Target;
544 OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
545 << " const std::string &CPU) {\n"
David Greene144cccb2010-01-05 17:47:41 +0000546 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
547 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU);\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 << " SubtargetFeatures Features(FS);\n"
549 << " Features.setCPUIfNone(CPU);\n"
550 << " uint32_t Bits = Features.getBits(SubTypeKV, SubTypeKVSize,\n"
551 << " FeatureKV, FeatureKVSize);\n";
552
553 for (unsigned i = 0; i < Features.size(); i++) {
554 // Next record
555 Record *R = Features[i];
556 const std::string &Instance = R->getName();
557 const std::string &Value = R->getValueAsString("Value");
558 const std::string &Attribute = R->getValueAsString("Attribute");
559
Dale Johannesen161badc2008-02-14 23:35:16 +0000560 if (Value=="true" || Value=="false")
561 OS << " if ((Bits & " << Instance << ") != 0) "
562 << Attribute << " = " << Value << ";\n";
563 else
564 OS << " if ((Bits & " << Instance << ") != 0 && " << Attribute <<
565 " < " << Value << ") " << Attribute << " = " << Value << ";\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 }
567
568 if (HasItineraries) {
569 OS << "\n"
570 << " InstrItinerary *Itinerary = (InstrItinerary *)"
571 << "Features.getInfo(ProcItinKV, ProcItinKVSize);\n"
David Goodwine0106972009-08-17 16:02:57 +0000572 << " InstrItins = InstrItineraryData(Stages, OperandCycles, Itinerary);\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 }
Anton Korobeynikov7357d8f2009-05-23 19:50:50 +0000574
575 OS << " return Features.getCPU();\n"
576 << "}\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577}
578
Anton Korobeynikov7357d8f2009-05-23 19:50:50 +0000579//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580// SubtargetEmitter::run - Main subtarget enumeration emitter.
581//
Daniel Dunbard4287062009-07-03 00:10:29 +0000582void SubtargetEmitter::run(raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 Target = CodeGenTarget().getName();
584
585 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
586
Sandeep Patel8e51aeb2009-11-11 03:23:46 +0000587 OS << "#include \"llvm/Support/Debug.h\"\n";
588 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
590 OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
Anton Korobeynikov00540072010-04-18 20:31:01 +0000591
592// Enumeration(OS, "FuncUnit", true);
593// OS<<"\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594// Enumeration(OS, "InstrItinClass", false);
595// OS<<"\n";
596 Enumeration(OS, "SubtargetFeature", true);
597 OS<<"\n";
598 FeatureKeyValues(OS);
599 OS<<"\n";
600 CPUKeyValues(OS);
601 OS<<"\n";
602 EmitData(OS);
603 OS<<"\n";
604 ParseFeaturesFunction(OS);
605}