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