SubtargetEmitter.cpp revision da4231f134989af7dc6bd3408821ba573def27b2
1//===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
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// Enumeration - Emit the specified class as an enumeration.
24//
25void SubtargetEmitter::Enumeration(std::ostream &OS,
26                                   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//
60void SubtargetEmitter::FeatureKeyValues(std::ostream &OS) {
61  // Gather and sort all the features
62  std::vector<Record*> FeatureList =
63                           Records.getAllDerivedDefinitions("SubtargetFeature");
64  std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
65
66  // Begin feature table
67  OS << "// Sorted (by key) array of values for CPU features.\n"
68     << "static const llvm::SubtargetFeatureKV FeatureKV[] = {\n";
69
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
81    // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
82    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//
120void SubtargetEmitter::CPUKeyValues(std::ostream &OS) {
121  // 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//
175unsigned SubtargetEmitter::CollectAllItinClasses(std::ostream &OS,
176                              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
218    // Form string as ,{ cycles, u1 | u2 | ... | un }
219    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
232    // Close off stage
233    ItinString += " }";
234    if (++i < N) ItinString += ", ";
235  }
236}
237
238//
239// EmitStageData - Generate unique itinerary stages.  Record itineraries for
240// processors.
241//
242void SubtargetEmitter::EmitStageData(std::ostream &OS,
243       unsigned NItinClasses,
244       std::map<std::string, unsigned> &ItinClassesMap,
245       std::vector<std::vector<InstrItinerary> > &ProcList) {
246  // Gather processor iteraries
247  std::vector<Record*> ProcItinList =
248                       Records.getAllDerivedDefinitions("ProcessorItineraries");
249
250  // If just no itinerary then don't bother
251  if (ProcItinList.size() < 2) return;
252
253  // Begin stages table
254  OS << "static const llvm::InstrStage Stages[] = {\n"
255        "  { 0, 0 }, // No itinerary\n";
256
257  unsigned StageCount = 1;
258  unsigned ItinEnum = 1;
259  std::map<std::string, unsigned> ItinMap;
260  for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
261    // Next record
262    Record *Proc = ProcItinList[i];
263
264    // Get processor itinerary name
265    const std::string &Name = Proc->getName();
266
267    // Skip default
268    if (Name == "NoItineraries") continue;
269
270    // Create and expand processor itinerary to cover all itinerary classes
271    std::vector<InstrItinerary> ItinList;
272    ItinList.resize(NItinClasses);
273
274    // Get itinerary data list
275    std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
276
277    // For each itinerary data
278    for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
279      // Next itinerary data
280      Record *ItinData = ItinDataList[j];
281
282      // Get string and stage count
283      std::string ItinString;
284      unsigned NStages;
285      FormItineraryString(ItinData, ItinString, NStages);
286
287      // Check to see if it already exists
288      unsigned Find = ItinMap[ItinString];
289
290      // If new itinerary
291      if (Find == 0) {
292        // Emit as { cycles, u1 | u2 | ... | un }, // index
293        OS << ItinString << ", // " << ItinEnum << "\n";
294        // Record Itin class number.
295        ItinMap[ItinString] = Find = StageCount;
296        StageCount += NStages;
297        ItinEnum++;
298      }
299
300      // Set up itinerary as location and location + stage count
301      InstrItinerary Intinerary = { Find, Find + NStages };
302
303      // Locate where to inject into processor itinerary table
304      const std::string &Name = ItinData->getValueAsDef("TheClass")->getName();
305      Find = ItinClassesMap[Name];
306
307      // Inject - empty slots will be 0, 0
308      ItinList[Find] = Intinerary;
309    }
310
311    // Add process itinerary to list
312    ProcList.push_back(ItinList);
313  }
314
315  // Closing stage
316  OS << "  { 0, 0 } // End itinerary\n";
317  // End stages table
318  OS << "};\n";
319
320  // Emit size of table
321  OS<<"\nenum {\n";
322  OS<<"  StagesSize = sizeof(Stages)/sizeof(llvm::InstrStage)\n";
323  OS<<"};\n";
324}
325
326//
327// EmitProcessorData - Generate data for processor itineraries.
328//
329void SubtargetEmitter::EmitProcessorData(std::ostream &OS,
330      std::vector<std::vector<InstrItinerary> > &ProcList) {
331  // Get an iterator for processor itinerary stages
332  std::vector<std::vector<InstrItinerary> >::iterator
333      ProcListIter = ProcList.begin();
334
335  // For each processor itinerary
336  std::vector<Record*> Itins =
337                       Records.getAllDerivedDefinitions("ProcessorItineraries");
338  for (unsigned i = 0, N = Itins.size(); i < N; i++) {
339    // Next record
340    Record *Itin = Itins[i];
341
342    // Get processor itinerary name
343    const std::string &Name = Itin->getName();
344
345    // Skip default
346    if (Name == "NoItineraries") continue;
347
348    // Begin processor itinerary table
349    OS << "\n";
350    OS << "static const llvm::InstrItinerary " << Name << "[] = {\n";
351
352    // For each itinerary class
353    std::vector<InstrItinerary> &ItinList = *ProcListIter++;
354    for (unsigned j = 0, M = ItinList.size(); j < M;) {
355      InstrItinerary &Intinerary = ItinList[j];
356
357      // Emit in the form of { first, last } // index
358      if (Intinerary.First == 0) {
359        OS << "  { 0, 0 }";
360      } else {
361        OS << "  { " << Intinerary.First << ", " << Intinerary.Last << " }";
362      }
363
364      // If more in list add comma
365      if (++j < M) OS << ",";
366
367      OS << " // " << (j - 1) << "\n";
368    }
369
370    // End processor itinerary table
371    OS << "};\n";
372  }
373}
374
375//
376// EmitProcessorLookup - generate cpu name to itinerary lookup table.
377//
378void SubtargetEmitter::EmitProcessorLookup(std::ostream &OS) {
379  // Gather and sort processor information
380  std::vector<Record*> ProcessorList =
381                          Records.getAllDerivedDefinitions("Processor");
382  std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
383
384  // Begin processor table
385  OS << "\n";
386  OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
387     << "static const llvm::SubtargetInfoKV ProcItinKV[] = {\n";
388
389  // For each processor
390  for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
391    // Next processor
392    Record *Processor = ProcessorList[i];
393
394    const std::string &Name = Processor->getValueAsString("Name");
395    const std::string &ProcItin =
396      Processor->getValueAsDef("ProcItin")->getName();
397
398    // Emit as { "cpu", procinit },
399    OS << "  { "
400       << "\"" << Name << "\", "
401       << "(void *)&" << ProcItin;
402
403    OS << " }";
404
405    // Depending on ''if more in the list'' emit comma
406    if (++i < N) OS << ",";
407
408    OS << "\n";
409  }
410
411  // End processor table
412  OS << "};\n";
413
414  // Emit size of table
415  OS<<"\nenum {\n";
416  OS<<"  ProcItinKVSize = sizeof(ProcItinKV)/"
417                            "sizeof(llvm::SubtargetInfoKV)\n";
418  OS<<"};\n";
419}
420
421//
422// EmitData - Emits all stages and itineries, folding common patterns.
423//
424void SubtargetEmitter::EmitData(std::ostream &OS) {
425  std::map<std::string, unsigned> ItinClassesMap;
426  std::vector<std::vector<InstrItinerary> > ProcList;
427
428  // Enumerate all the itinerary classes
429  unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap);
430  // Make sure the rest is worth the effort
431  HasItineraries = NItinClasses != 1;   // Ignore NoItinerary.
432
433  if (HasItineraries) {
434    // Emit the stage data
435    EmitStageData(OS, NItinClasses, ItinClassesMap, ProcList);
436    // Emit the processor itinerary data
437    EmitProcessorData(OS, ProcList);
438    // Emit the processor lookup data
439    EmitProcessorLookup(OS);
440  }
441}
442
443//
444// ParseFeaturesFunction - Produces a subtarget specific function for parsing
445// the subtarget features string.
446//
447void SubtargetEmitter::ParseFeaturesFunction(std::ostream &OS) {
448  std::vector<Record*> Features =
449                       Records.getAllDerivedDefinitions("SubtargetFeature");
450  std::sort(Features.begin(), Features.end(), LessRecord());
451
452  OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
453     << "// subtarget options.\n"
454     << "void llvm::";
455  OS << Target;
456  OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
457     << "                                  const std::string &CPU) {\n"
458     << "  SubtargetFeatures Features(FS);\n"
459     << "  Features.setCPUIfNone(CPU);\n"
460     << "  uint32_t Bits =  Features.getBits(SubTypeKV, SubTypeKVSize,\n"
461     << "                                    FeatureKV, FeatureKVSize);\n";
462
463  for (unsigned i = 0; i < Features.size(); i++) {
464    // Next record
465    Record *R = Features[i];
466    const std::string &Instance = R->getName();
467    const std::string &Value = R->getValueAsString("Value");
468    const std::string &Attribute = R->getValueAsString("Attribute");
469
470    if (Value=="true" || Value=="false")
471      OS << "  if ((Bits & " << Instance << ") != 0) "
472         << Attribute << " = " << Value << ";\n";
473    else
474      OS << "  if ((Bits & " << Instance << ") != 0 && " << Attribute <<
475            " < " << Value << ") " << Attribute << " = " << Value << ";\n";
476  }
477
478  if (HasItineraries) {
479    OS << "\n"
480       << "  InstrItinerary *Itinerary = (InstrItinerary *)"
481       <<              "Features.getInfo(ProcItinKV, ProcItinKVSize);\n"
482       << "  InstrItins = InstrItineraryData(Stages, Itinerary);\n";
483  }
484
485  OS << "}\n";
486}
487
488//
489// SubtargetEmitter::run - Main subtarget enumeration emitter.
490//
491void SubtargetEmitter::run(std::ostream &OS) {
492  Target = CodeGenTarget().getName();
493
494  EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
495
496  OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
497  OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
498
499  Enumeration(OS, "FuncUnit", true);
500  OS<<"\n";
501//  Enumeration(OS, "InstrItinClass", false);
502//  OS<<"\n";
503  Enumeration(OS, "SubtargetFeature", true);
504  OS<<"\n";
505  FeatureKeyValues(OS);
506  OS<<"\n";
507  CPUKeyValues(OS);
508  OS<<"\n";
509  EmitData(OS);
510  OS<<"\n";
511  ParseFeaturesFunction(OS);
512}
513