Rollback changes to take a different tack.
[oota-llvm.git] / utils / TableGen / SubtargetEmitter.cpp
1 //===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
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>
20 using namespace llvm;
21
22 //
23 // Record sort by name function.
24 //
25 struct 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 //
34 struct 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 //
43 void 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     std::string Name = Def->getName();
60     OS << "  " << Name;
61     
62     // If bit flags then emit expression (1 << i)
63     if (isBits)  OS << " = " << " 1 << " << i;
64
65     // Depending on 'if more in the list' emit comma
66     if (++i < N) OS << ",";
67     
68     OS << "\n";
69   }
70   
71   // Close enumeration
72   OS << "};\n";
73 }
74
75 //
76 // FeatureKeyValues - Emit data of all the subtarget features.  Used by command
77 // line.
78 //
79 void SubtargetEmitter::FeatureKeyValues(std::ostream &OS) {
80   // Gather and sort all the features
81   std::vector<Record*> FeatureList =
82                            Records.getAllDerivedDefinitions("SubtargetFeature");
83   std::sort(FeatureList.begin(), FeatureList.end(), LessRecord());
84
85   // Begin feature table
86   OS << "// Sorted (by key) array of values for CPU features.\n"
87      << "static llvm::SubtargetFeatureKV FeatureKV[] = {\n";
88   
89   // For each feature
90   for (unsigned i = 0, N = FeatureList.size(); i < N;) {
91     // Next feature
92     Record *Feature = FeatureList[i];
93
94     std::string Name = Feature->getName();
95     std::string CommandLineName = Feature->getValueAsString("Name");
96     std::string Desc = Feature->getValueAsString("Desc");
97     
98     // Emit as { "feature", "decription", feactureEnum }
99     OS << "  { "
100        << "\"" << CommandLineName << "\", "
101        << "\"" << Desc << "\", "
102        << Name
103        << " }";
104     
105     // Depending on 'if more in the list' emit comma
106     if (++i < N) OS << ",";
107     
108     OS << "\n";
109   }
110   
111   // End feature table
112   OS << "};\n";
113
114   // Emit size of table
115   OS<<"\nenum {\n";
116   OS<<"  FeatureKVSize = sizeof(FeatureKV)/sizeof(llvm::SubtargetFeatureKV)\n";
117   OS<<"};\n";
118 }
119
120 //
121 // CPUKeyValues - Emit data of all the subtarget processors.  Used by command
122 // line.
123 //
124 void SubtargetEmitter::CPUKeyValues(std::ostream &OS) {
125   // Gather and sort processor information
126   std::vector<Record*> ProcessorList =
127                           Records.getAllDerivedDefinitions("Processor");
128   std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
129
130   // Begin processor table
131   OS << "// Sorted (by key) array of values for CPU subtype.\n"
132      << "static const llvm::SubtargetFeatureKV SubTypeKV[] = {\n";
133      
134   // For each processor
135   for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
136     // Next processor
137     Record *Processor = ProcessorList[i];
138
139     std::string Name = Processor->getValueAsString("Name");
140     std::vector<Record*> FeatureList = 
141       Processor->getValueAsListOfDefs("Features");
142     
143     // Emit as { "cpu", "description", f1 | f2 | ... fn },
144     OS << "  { "
145        << "\"" << Name << "\", "
146        << "\"Select the " << Name << " processor\", ";
147     
148     if (FeatureList.empty()) {
149       OS << "0";
150     } else {
151       for (unsigned j = 0, M = FeatureList.size(); j < M;) {
152         Record *Feature = FeatureList[j];
153         std::string Name = Feature->getName();
154         OS << Name;
155         if (++j < M) OS << " | ";
156       }
157     }
158     
159     OS << " }";
160     
161     // Depending on 'if more in the list' emit comma
162     if (++i < N) OS << ",";
163     
164     OS << "\n";
165   }
166   
167   // End processor table
168   OS << "};\n";
169
170   // Emit size of table
171   OS<<"\nenum {\n";
172   OS<<"  SubTypeKVSize = sizeof(SubTypeKV)/sizeof(llvm::SubtargetFeatureKV)\n";
173   OS<<"};\n";
174 }
175
176 //
177 // CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
178 // Returns itinerary class count.
179 //
180 unsigned SubtargetEmitter::CollectAllItinClasses(std::ostream &OS,
181                               std::map<std::string, unsigned> &ItinClassesMap) {
182   // Gather and sort all itinerary classes
183   std::vector<Record*> ItinClassList =
184                             Records.getAllDerivedDefinitions("InstrItinClass");
185   std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
186
187   // For each itinerary class
188   unsigned N = ItinClassList.size();
189   for (unsigned i = 0; i < N; i++) {
190     // Next itinerary class
191     Record *ItinClass = ItinClassList[i];
192     // Get name of itinerary class
193     std::string Name = ItinClass->getName();
194     // Assign itinerary class a unique number
195     ItinClassesMap[Name] = i;
196   }
197   
198   // Emit size of table
199   OS<<"\nenum {\n";
200   OS<<"  ItinClassesSize = " << N << "\n";
201   OS<<"};\n";
202
203   // Return itinerary class count
204   return N;
205 }
206
207 //
208 // FormItineraryString - Compose a string containing the data initialization
209 // for the specified itinerary.  N is the number of stages.
210 //
211 void SubtargetEmitter::FormItineraryString(Record *ItinData,
212                                            std::string &ItinString,
213                                            unsigned &NStages) {
214   // Get states list
215   std::vector<Record*> StageList = ItinData->getValueAsListOfDefs("Stages");
216
217   // For each stage
218   unsigned N = NStages = StageList.size();
219   for (unsigned i = 0; i < N; i++) {
220     // Next stage
221     Record *Stage = StageList[i];
222   
223     // Form string as ,{ cycles, u1 | u2 | ... | un }
224     int Cycles = Stage->getValueAsInt("Cycles");
225     ItinString += "  { " + itostr(Cycles) + ", ";
226     
227     // Get unit list
228     std::vector<Record*> UnitList = Stage->getValueAsListOfDefs("Units");
229     
230     // For each unit
231     for (unsigned j = 0, M = UnitList.size(); j < M;) {
232       // Next unit
233       Record *Unit = UnitList[j];
234       
235       // Add name and bitwise or
236       ItinString += Unit->getName();
237       if (++j < M) ItinString += " | ";
238     }
239     
240     // Close off stage
241     ItinString += " }";
242   }
243 }
244
245 //
246 // EmitStageData - Generate unique itinerary stages.  Record itineraries for 
247 // processors.
248 //
249 void SubtargetEmitter::EmitStageData(std::ostream &OS,
250        unsigned NItinClasses,
251        std::map<std::string, unsigned> &ItinClassesMap, 
252        std::vector<std::vector<InstrItinerary> > &ProcList) {
253   // Gather processor iteraries
254   std::vector<Record*> ProcItinList =
255                        Records.getAllDerivedDefinitions("ProcessorItineraries");
256   
257   // If just no itinerary then don't bother
258   if (ProcItinList.size() < 2) return;
259
260   // Begin stages table
261   OS << "static llvm::InstrStage Stages[] = {\n"
262         "  { 0, 0 }, // No itinerary\n";
263         
264   unsigned ItinEnum = 1;
265   std::map<std::string, unsigned> ItinMap;
266   for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
267     // Next record
268     Record *Proc = ProcItinList[i];
269     
270     // Get processor itinerary name
271     std::string Name = Proc->getName();
272     
273     // Skip default
274     if (Name == "NoItineraries") continue;
275     
276     // Create and expand processor itinerary to cover all itinerary classes
277     std::vector<InstrItinerary> ItinList;
278     ItinList.resize(NItinClasses);
279     
280     // Get itinerary data list
281     std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
282     
283     // For each itinerary data
284     for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
285       // Next itinerary data
286       Record *ItinData = ItinDataList[j];
287       
288       // Get string and stage count
289       std::string ItinString;
290       unsigned NStages;
291       FormItineraryString(ItinData, ItinString, NStages);
292
293       // Check to see if it already exists
294       unsigned Find = ItinMap[ItinString];
295       
296       // If new itinerary
297       if (Find == 0) {
298         // Emit as { cycles, u1 | u2 | ... | un }, // index
299         OS << ItinString << ", // " << ItinEnum << "\n";
300         // Record Itin class number
301         ItinMap[ItinString] = Find = ItinEnum++;
302       }
303       
304       // Set up itinerary as location and location + stage count
305       InstrItinerary Intinerary = { Find, Find + NStages };
306
307       // Locate where to inject into processor itinerary table
308       std::string Name = ItinData->getValueAsDef("TheClass")->getName();
309       Find = ItinClassesMap[Name];
310       
311       // Inject - empty slots will be 0, 0
312       ItinList[Find] = Intinerary;
313     }
314     
315     // Add process itinerary to list
316     ProcList.push_back(ItinList);
317   }
318   
319   // Closing stage
320   OS << "  { 0, 0 } // End itinerary\n";
321   // End stages table
322   OS << "};\n";
323   
324   // Emit size of table
325   OS<<"\nenum {\n";
326   OS<<"  StagesSize = sizeof(Stages)/sizeof(llvm::InstrStage)\n";
327   OS<<"};\n";
328 }
329
330 //
331 // EmitProcessorData - Generate data for processor itineraries.
332 //
333 void SubtargetEmitter::EmitProcessorData(std::ostream &OS,
334       std::vector<std::vector<InstrItinerary> > &ProcList) {
335   // Get an iterator for processor itinerary stages
336   std::vector<std::vector<InstrItinerary> >::iterator
337       ProcListIter = ProcList.begin();
338   
339   // For each processor itinerary
340   std::vector<Record*> Itins =
341                        Records.getAllDerivedDefinitions("ProcessorItineraries");
342   for (unsigned i = 0, N = Itins.size(); i < N; i++) {
343     // Next record
344     Record *Itin = Itins[i];
345
346     // Get processor itinerary name
347     std::string Name = Itin->getName();
348     
349     // Skip default
350     if (Name == "NoItineraries") continue;
351
352     // Begin processor itinerary table
353     OS << "\n";
354     OS << "static llvm::InstrItinerary " << Name << "[] = {\n";
355     
356     // For each itinerary class
357     std::vector<InstrItinerary> &ItinList = *ProcListIter++;
358     for (unsigned j = 0, M = ItinList.size(); j < M;) {
359       InstrItinerary &Intinerary = ItinList[j];
360       
361       // Emit in the form of { first, last } // index
362       if (Intinerary.First == 0) {
363         OS << "  { 0, 0 }";
364       } else {
365         OS << "  { " << Intinerary.First << ", " << Intinerary.Last << " }";
366       }
367       
368       // If more in list add comma
369       if (++j < M) OS << ",";
370       
371       OS << " // " << (j - 1) << "\n";
372     }
373     
374     // End processor itinerary table
375     OS << "};\n";
376   }
377 }
378
379 //
380 // EmitProcessorLookup - generate cpu name to itinerary lookup table.
381 //
382 void SubtargetEmitter::EmitProcessorLookup(std::ostream &OS) {
383   // Gather and sort processor information
384   std::vector<Record*> ProcessorList =
385                           Records.getAllDerivedDefinitions("Processor");
386   std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
387
388   // Begin processor table
389   OS << "\n";
390   OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
391      << "static const llvm::SubtargetInfoKV ProcItinKV[] = {\n";
392      
393   // For each processor
394   for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
395     // Next processor
396     Record *Processor = ProcessorList[i];
397
398     std::string Name = Processor->getValueAsString("Name");
399     std::string ProcItin = 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 //
427 void SubtargetEmitter::EmitData(std::ostream &OS) {
428   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 //
450 void SubtargetEmitter::ParseFeaturesFunction(std::ostream &OS) {
451   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" 
457         "void llvm::";
458   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     std::string Instance = R->getName();
470     std::string Name = R->getValueAsString("Name");
471     std::string Value = R->getValueAsString("Value");
472     std::string Attribute = R->getValueAsString("Attribute");
473
474     OS << "  if ((Bits & " << Instance << ") != 0) "
475        << 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 //
491 void 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 }