1 //===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend emits subtarget enumerations.
12 //===----------------------------------------------------------------------===//
14 #include "SubtargetEmitter.h"
15 #include "CodeGenTarget.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Support/Debug.h"
23 // Record sort by name function.
26 bool operator()(const Record *Rec1, const Record *Rec2) const {
27 return Rec1->getName() < Rec2->getName();
32 // Record sort by field "Name" function.
34 struct LessRecordFieldName {
35 bool operator()(const Record *Rec1, const Record *Rec2) const {
36 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
41 // Enumeration - Emit the specified class as an enumeration.
43 void SubtargetEmitter::Enumeration(std::ostream &OS,
44 const char *ClassName,
46 // Get all records of class and sort
47 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName);
48 sort(DefList.begin(), DefList.end(), LessRecord());
54 for (unsigned i = 0, N = DefList.size(); i < N;) {
56 Record *Def = DefList[i];
59 std::string Name = Def->getName();
62 // If bit flags then emit expression (1 << i)
63 if (isBits) OS << " = " << " 1 << " << i;
65 // Depending on if more in the list emit comma
66 if (++i < N) OS << ",";
76 // FeatureKeyValues - Emit data of all the subtarget features. Used by command
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());
85 // Begin feature table
86 OS << "// Sorted (by key) array of values for CPU features.\n"
87 << "static llvm::SubtargetFeatureKV FeatureKV[] = {\n";
90 for (unsigned i = 0, N = FeatureList.size(); i < N;) {
92 Record *Feature = FeatureList[i];
94 std::string Name = Feature->getName();
95 std::string CommandLineName = Feature->getValueAsString("Name");
96 std::string Desc = Feature->getValueAsString("Desc");
98 // Emit as { "feature", "decription", feactureEnum }
100 << "\"" << CommandLineName << "\", "
101 << "\"" << Desc << "\", "
105 // Depending on if more in the list emit comma
106 if (++i < N) OS << ",";
114 // Emit size of table
116 OS<<" FeatureKVSize = sizeof(FeatureKV)/sizeof(llvm::SubtargetFeatureKV)\n";
121 // CPUKeyValues - Emit data of all the subtarget processors. Used by command
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());
130 // Begin processor table
131 OS << "// Sorted (by key) array of values for CPU subtype.\n"
132 << "static const llvm::SubtargetFeatureKV SubTypeKV[] = {\n";
134 // For each processor
135 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
137 Record *Processor = ProcessorList[i];
139 std::string Name = Processor->getValueAsString("Name");
140 std::vector<Record*> FeatureList =
141 Processor->getValueAsListOfDefs("Features");
143 // Emit as { "cpu", "description", f1 | f2 | ... fn },
145 << "\"" << Name << "\", "
146 << "\"Select the " << Name << " processor\", ";
148 if (FeatureList.empty()) {
151 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
152 Record *Feature = FeatureList[j];
153 std::string Name = Feature->getName();
155 if (++j < M) OS << " | ";
161 // Depending on if more in the list emit comma
162 if (++i < N) OS << ",";
167 // End processor table
170 // Emit size of table
172 OS<<" SubTypeKVSize = sizeof(SubTypeKV)/sizeof(llvm::SubtargetFeatureKV)\n";
177 // CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
178 // Returns itinerary class count.
180 unsigned SubtargetEmitter::CollectAllItinClasses(std::map<std::string, unsigned>
182 // Gather and sort all itinerary classes
183 std::vector<Record*> ItinClassList =
184 Records.getAllDerivedDefinitions("InstrItinClass");
185 sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
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;
198 // Return itinerary class count
203 // FormItineraryString - Compose a string containing the data initialization
204 // for the specified itinerary. N is the number of stages.
206 void SubtargetEmitter::FormItineraryString(Record *ItinData,
207 std::string &ItinString,
210 std::vector<Record*> StageList = ItinData->getValueAsListOfDefs("Stages");
213 unsigned N = NStages = StageList.size();
214 for (unsigned i = 0; i < N; i++) {
216 Record *Stage = StageList[i];
218 // Form string as ,{ cycles, u1 | u2 | ... | un }
219 int Cycles = Stage->getValueAsInt("Cycles");
220 ItinString += " ,{ " + itostr(Cycles) + ", ";
223 std::vector<Record*> UnitList = Stage->getValueAsListOfDefs("Units");
226 for (unsigned j = 0, M = UnitList.size(); j < M;) {
228 Record *Unit = UnitList[j];
230 // Add name and bitwise or
231 ItinString += Unit->getName();
232 if (++j < M) ItinString += " | ";
241 // EmitStageData - Generate unique itinerary stages. Record itineraries for
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");
252 // If just no itinerary then don't bother
253 if (ProcItinList.size() < 2) return;
255 // Begin stages table
256 OS << "static llvm::InstrStage Stages[] = {\n"
257 " { 0, 0 } // No itinerary\n";
259 unsigned ItinEnum = 1;
260 std::map<std::string, unsigned> ItinMap;
261 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
263 Record *Proc = ProcItinList[i];
265 // Get processor itinerary name
266 std::string Name = Proc->getName();
269 if (Name == "NoItineraries") continue;
271 // Create and expand processor itinerary to cover all itinerary classes
272 std::vector<InstrItinerary> ItinList;
273 ItinList.resize(NItinClasses);
275 // Get itinerary data list
276 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
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];
283 // Get string and stage count
284 std::string ItinString;
286 FormItineraryString(ItinData, ItinString, NStages);
288 // Check to see if it already exists
289 unsigned Find = ItinMap[ItinString];
293 // Emit as ,{ cycles, u1 | u2 | ... | un } // index
294 OS << ItinString << " // " << ItinEnum << "\n";
295 ItinMap[ItinString] = Find = ItinEnum++;
298 // Set up itinerary as location and location + stage count
299 InstrItinerary Intinerary = { Find, Find + NStages };
301 // Locate where to inject into processor itinerary table
302 std::string Name = ItinData->getValueAsDef("TheClass")->getName();
303 Find = ItinClassesMap[Name];
305 // Inject - empty slots will be 0, 0
306 ItinList[Find] = Intinerary;
309 // Add process itinerary to list
310 ProcList.push_back(ItinList);
318 // EmitProcessData - Generate data for processor itineraries.
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();
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++) {
331 Record *Itin = Itins[i];
333 // Get processor itinerary name
334 std::string Name = Itin->getName();
337 if (Name == "NoItineraries") continue;
339 // Begin processor itinerary table
341 OS << "static llvm::InstrItinerary " << Name << "[] = {\n";
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];
349 // Emit in the form of { first, last } // index
350 if (Intinerary.First == 0) {
353 OS << " { " << Intinerary.First << ", " << Intinerary.Last << " }";
356 // If more in list add comma
357 if (++j < M) OS << ",";
359 OS << " // " << (j - 1) << "\n";
362 // End processor itinerary table
368 // EmitData - Emits all stages and itineries, folding common patterns.
370 void SubtargetEmitter::EmitData(std::ostream &OS) {
371 std::map<std::string, unsigned> ItinClassesMap;
372 std::vector<std::vector<InstrItinerary> > ProcList;
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);
383 // ParseFeaturesFunction - Produces a subtarget specific function for parsing
384 // the subtarget features string.
386 void SubtargetEmitter::ParseFeaturesFunction(std::ostream &OS) {
387 std::vector<Record*> Features =
388 Records.getAllDerivedDefinitions("SubtargetFeature");
389 sort(Features.begin(), Features.end(), LessRecord());
391 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
392 "// subtarget options.\n"
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";
402 for (unsigned i = 0; i < Features.size(); i++) {
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");
410 OS << " " << Attribute << " = (Bits & " << Instance << ") != 0;\n";
416 // SubtargetEmitter::run - Main subtarget enumeration emitter.
418 void SubtargetEmitter::run(std::ostream &OS) {
419 Target = CodeGenTarget().getName();
421 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
423 OS << "#include \"llvm/Target/SubtargetFeature.h\"\n";
424 OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
426 Enumeration(OS, "FuncUnit", true);
428 // Enumeration(OS, "InstrItinClass", false);
430 Enumeration(OS, "SubtargetFeature", true);
432 FeatureKeyValues(OS);
438 ParseFeaturesFunction(OS);