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