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