86b5201d5446fe75559bc430e1a90c5ba3fff5ba
[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   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   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   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::map<std::string, unsigned>
181                                                               &ItinClassesMap) {
182   // Gather and sort all itinerary classes
183   std::vector<Record*> ItinClassList =
184                             Records.getAllDerivedDefinitions("InstrItinClass");
185   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   // Return itinerary class count
199   return N;
200 }
201
202 //
203 // FormItineraryString - Compose a string containing the data initialization
204 // for the specified itinerary.  N is the number of stages.
205 //
206 void SubtargetEmitter::FormItineraryString(Record *ItinData,
207                                            std::string &ItinString,
208                                            unsigned &NStages) {
209   // Get states list
210   std::vector<Record*> StageList = ItinData->getValueAsListOfDefs("Stages");
211
212   // For each stage
213   unsigned N = NStages = StageList.size();
214   for (unsigned i = 0; i < N; i++) {
215     // Next stage
216     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     std::vector<Record*> UnitList = Stage->getValueAsListOfDefs("Units");
224     
225     // For each unit
226     for (unsigned j = 0, M = UnitList.size(); j < M;) {
227       // Next unit
228       Record *Unit = UnitList[j];
229       
230       // Add name and bitwise or
231       ItinString += Unit->getName();
232       if (++j < M) ItinString += " | ";
233     }
234     
235     // Close off stage
236     ItinString += " }";
237   }
238 }
239
240 //
241 // EmitStageData - Generate unique itinerary stages.  Record itineraries for 
242 // processors.
243 //
244 void SubtargetEmitter::EmitStageData(std::ostream &OS,
245        unsigned NItinClasses,
246        std::map<std::string, unsigned> &ItinClassesMap, 
247        std::vector<std::vector<InstrItinerary> > &ProcList) {
248   // Gather processor iteraries
249   std::vector<Record*> ProcItinList =
250                        Records.getAllDerivedDefinitions("ProcessorItineraries");
251   
252   // If just no itinerary then don't bother
253   if (ProcItinList.size() < 2) return;
254
255   // Begin stages table
256   OS << "static llvm::InstrStage Stages[] = {\n"
257         "  { 0, 0 } // No itinerary\n";
258         
259   unsigned ItinEnum = 1;
260   std::map<std::string, unsigned> ItinMap;
261   for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
262     // Next record
263     Record *Proc = ProcItinList[i];
264     
265     // Get processor itinerary name
266     std::string Name = Proc->getName();
267     
268     // Skip default
269     if (Name == "NoItineraries") continue;
270     
271     // Create and expand processor itinerary to cover all itinerary classes
272     std::vector<InstrItinerary> ItinList;
273     ItinList.resize(NItinClasses);
274     
275     // Get itinerary data list
276     std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
277     
278     // For each itinerary data
279     for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
280       // Next itinerary data
281       Record *ItinData = ItinDataList[j];
282       
283       // Get string and stage count
284       std::string ItinString;
285       unsigned NStages;
286       FormItineraryString(ItinData, ItinString, NStages);
287
288       // Check to see if it already exists
289       unsigned Find = ItinMap[ItinString];
290       
291       // If new itinerary
292       if (Find == 0) {
293         // Emit as ,{ cycles, u1 | u2 | ... | un } // index
294         OS << ItinString << " // " << ItinEnum << "\n";
295         ItinMap[ItinString] = Find = ItinEnum++;
296       }
297       
298       // Set up itinerary as location and location + stage count
299       InstrItinerary Intinerary = { Find, Find + NStages };
300
301       // Locate where to inject into processor itinerary table
302       std::string Name = ItinData->getValueAsDef("TheClass")->getName();
303       Find = ItinClassesMap[Name];
304       
305       // Inject - empty slots will be 0, 0
306       ItinList[Find] = Intinerary;
307     }
308     
309     // Add process itinerary to list
310     ProcList.push_back(ItinList);
311   }
312   
313   // End stages table
314   OS << "};\n";
315 }
316
317 //
318 // EmitProcessData - Generate data for processor itineraries.
319 //
320 void SubtargetEmitter::EmitProcessData(std::ostream &OS,
321       std::vector<std::vector<InstrItinerary> > &ProcList) {
322   // Get an iterator for processor itinerary stages
323   std::vector<std::vector<InstrItinerary> >::iterator
324       ProcListIter = ProcList.begin();
325   
326   // For each processor itinerary
327   std::vector<Record*> Itins =
328                        Records.getAllDerivedDefinitions("ProcessorItineraries");
329   for (unsigned i = 0, N = Itins.size(); i < N; i++) {
330     // Next record
331     Record *Itin = Itins[i];
332
333     // Get processor itinerary name
334     std::string Name = Itin->getName();
335     
336     // Skip default
337     if (Name == "NoItineraries") continue;
338
339     // Begin processor itinerary table
340     OS << "\n";
341     OS << "static llvm::InstrItinerary " << Name << "[] = {\n";
342     
343     // For each itinerary class
344     std::vector<InstrItinerary> &ItinList = *ProcListIter++;
345     unsigned ItinIndex = 0;
346     for (unsigned j = 0, M = ItinList.size(); j < M;) {
347       InstrItinerary &Intinerary = ItinList[j];
348       
349       // Emit in the form of { first, last } // index
350       if (Intinerary.First == 0) {
351         OS << "  { 0, 0 }";
352       } else {
353         OS << "  { " << Intinerary.First << ", " << Intinerary.Last << " }";
354       }
355       
356       // If more in list add comma
357       if (++j < M) OS << ",";
358       
359       OS << " // " << (j - 1) << "\n";
360     }
361     
362     // End processor itinerary table
363     OS << "};\n";
364   }
365 }
366
367 //
368 // EmitData - Emits all stages and itineries, folding common patterns.
369 //
370 void SubtargetEmitter::EmitData(std::ostream &OS) {
371   std::map<std::string, unsigned> ItinClassesMap;
372   std::vector<std::vector<InstrItinerary> > ProcList;
373   
374   // Enumerate all the itinerary classes
375   unsigned NItinClasses = CollectAllItinClasses(ItinClassesMap);
376   // Emit the stage data
377   EmitStageData(OS, NItinClasses, ItinClassesMap, ProcList);
378   // Emit the processor itinerary data
379   EmitProcessData(OS, ProcList);
380 }
381
382 //
383 // ParseFeaturesFunction - Produces a subtarget specific function for parsing
384 // the subtarget features string.
385 //
386 void SubtargetEmitter::ParseFeaturesFunction(std::ostream &OS) {
387   std::vector<Record*> Features =
388                        Records.getAllDerivedDefinitions("SubtargetFeature");
389   sort(Features.begin(), Features.end(), LessRecord());
390
391   OS << "// ParseSubtargetFeatures - Parses features string setting specified\n" 
392         "// subtarget options.\n" 
393         "void llvm::";
394   OS << Target;
395   OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
396         "                                  const std::string &CPU) {\n"
397         "  SubtargetFeatures Features(FS);\n"
398         "  Features.setCPUIfNone(CPU);\n"
399         "  uint32_t Bits =  Features.getBits(SubTypeKV, SubTypeKVSize,\n"
400         "                                    FeatureKV, FeatureKVSize);\n";
401         
402   for (unsigned i = 0; i < Features.size(); i++) {
403     // Next record
404     Record *R = Features[i];
405     std::string Instance = R->getName();
406     std::string Name = R->getValueAsString("Name");
407     std::string Type = R->getValueAsString("Type");
408     std::string Attribute = R->getValueAsString("Attribute");
409     
410     OS << "  " << Attribute << " = (Bits & " << Instance << ") != 0;\n";
411   }
412   OS << "}\n";
413 }
414
415 // 
416 // SubtargetEmitter::run - Main subtarget enumeration emitter.
417 //
418 void SubtargetEmitter::run(std::ostream &OS) {
419   Target = CodeGenTarget().getName();
420
421   EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
422
423   OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
424   OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
425   
426   Enumeration(OS, "FuncUnit", true);
427   OS<<"\n";
428 //  Enumeration(OS, "InstrItinClass", false);
429 //  OS<<"\n";
430   Enumeration(OS, "SubtargetFeature", true);
431   OS<<"\n";
432   FeatureKeyValues(OS);
433   OS<<"\n";
434   CPUKeyValues(OS);
435   OS<<"\n";
436   EmitData(OS);
437   OS<<"\n";
438   ParseFeaturesFunction(OS);
439 }