1 //===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 // Enumeration - Emit the specified class as an enumeration.
25 void SubtargetEmitter::Enumeration(raw_ostream
&OS
,
26 const char *ClassName
,
28 // Get all records of class and sort
29 std::vector
<Record
*> DefList
= Records
.getAllDerivedDefinitions(ClassName
);
30 std::sort(DefList
.begin(), DefList
.end(), LessRecord());
36 for (unsigned i
= 0, N
= DefList
.size(); i
< N
;) {
38 Record
*Def
= DefList
[i
];
41 OS
<< " " << Def
->getName();
43 // If bit flags then emit expression (1 << i)
44 if (isBits
) OS
<< " = " << " 1 << " << i
;
46 // Depending on 'if more in the list' emit comma
47 if (++i
< N
) OS
<< ",";
57 // FeatureKeyValues - Emit data of all the subtarget features. Used by the
60 void SubtargetEmitter::FeatureKeyValues(raw_ostream
&OS
) {
61 // Gather and sort all the features
62 std::vector
<Record
*> FeatureList
=
63 Records
.getAllDerivedDefinitions("SubtargetFeature");
64 std::sort(FeatureList
.begin(), FeatureList
.end(), LessRecordFieldName());
66 // Begin feature table
67 OS
<< "// Sorted (by key) array of values for CPU features.\n"
68 << "static const llvm::SubtargetFeatureKV FeatureKV[] = {\n";
71 for (unsigned i
= 0, N
= FeatureList
.size(); i
< N
; ++i
) {
73 Record
*Feature
= FeatureList
[i
];
75 const std::string
&Name
= Feature
->getName();
76 const std::string
&CommandLineName
= Feature
->getValueAsString("Name");
77 const std::string
&Desc
= Feature
->getValueAsString("Desc");
79 if (CommandLineName
.empty()) continue;
81 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
83 << "\"" << CommandLineName
<< "\", "
84 << "\"" << Desc
<< "\", "
87 const std::vector
<Record
*> &ImpliesList
=
88 Feature
->getValueAsListOfDefs("Implies");
90 if (ImpliesList
.empty()) {
93 for (unsigned j
= 0, M
= ImpliesList
.size(); j
< M
;) {
94 OS
<< ImpliesList
[j
]->getName();
95 if (++j
< M
) OS
<< " | ";
101 // Depending on 'if more in the list' emit comma
102 if ((i
+ 1) < N
) OS
<< ",";
110 // Emit size of table
112 OS
<<" FeatureKVSize = sizeof(FeatureKV)/sizeof(llvm::SubtargetFeatureKV)\n";
117 // CPUKeyValues - Emit data of all the subtarget processors. Used by command
120 void SubtargetEmitter::CPUKeyValues(raw_ostream
&OS
) {
121 // Gather and sort processor information
122 std::vector
<Record
*> ProcessorList
=
123 Records
.getAllDerivedDefinitions("Processor");
124 std::sort(ProcessorList
.begin(), ProcessorList
.end(), LessRecordFieldName());
126 // Begin processor table
127 OS
<< "// Sorted (by key) array of values for CPU subtype.\n"
128 << "static const llvm::SubtargetFeatureKV SubTypeKV[] = {\n";
130 // For each processor
131 for (unsigned i
= 0, N
= ProcessorList
.size(); i
< N
;) {
133 Record
*Processor
= ProcessorList
[i
];
135 const std::string
&Name
= Processor
->getValueAsString("Name");
136 const std::vector
<Record
*> &FeatureList
=
137 Processor
->getValueAsListOfDefs("Features");
139 // Emit as { "cpu", "description", f1 | f2 | ... fn },
141 << "\"" << Name
<< "\", "
142 << "\"Select the " << Name
<< " processor\", ";
144 if (FeatureList
.empty()) {
147 for (unsigned j
= 0, M
= FeatureList
.size(); j
< M
;) {
148 OS
<< FeatureList
[j
]->getName();
149 if (++j
< M
) OS
<< " | ";
153 // The "0" is for the "implies" section of this data structure.
156 // Depending on 'if more in the list' emit comma
157 if (++i
< N
) OS
<< ",";
162 // End processor table
165 // Emit size of table
167 OS
<<" SubTypeKVSize = sizeof(SubTypeKV)/sizeof(llvm::SubtargetFeatureKV)\n";
172 // CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
173 // Returns itinerary class count.
175 unsigned SubtargetEmitter::
176 CollectAllItinClasses(raw_ostream
&OS
,
177 std::map
<std::string
, unsigned> &ItinClassesMap
,
178 std::vector
<Record
*> &ItinClassList
) {
179 // For each itinerary class
180 unsigned N
= ItinClassList
.size();
181 for (unsigned i
= 0; i
< N
; i
++) {
182 // Next itinerary class
183 const Record
*ItinClass
= ItinClassList
[i
];
184 // Get name of itinerary class
185 // Assign itinerary class a unique number
186 ItinClassesMap
[ItinClass
->getName()] = i
;
189 // Emit size of table
191 OS
<<" ItinClassesSize = " << N
<< "\n";
194 // Return itinerary class count
199 // FormItineraryStageString - Compose a string containing the stage
200 // data initialization for the specified itinerary. N is the number
203 void SubtargetEmitter::FormItineraryStageString(const std::string
&Name
,
205 std::string
&ItinString
,
208 const std::vector
<Record
*> &StageList
=
209 ItinData
->getValueAsListOfDefs("Stages");
212 unsigned N
= NStages
= StageList
.size();
213 for (unsigned i
= 0; i
< N
;) {
215 const Record
*Stage
= StageList
[i
];
217 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
218 int Cycles
= Stage
->getValueAsInt("Cycles");
219 ItinString
+= " { " + itostr(Cycles
) + ", ";
222 const std::vector
<Record
*> &UnitList
= Stage
->getValueAsListOfDefs("Units");
225 for (unsigned j
= 0, M
= UnitList
.size(); j
< M
;) {
226 // Add name and bitwise or
227 ItinString
+= Name
+ "FU::" + UnitList
[j
]->getName();
228 if (++j
< M
) ItinString
+= " | ";
231 int TimeInc
= Stage
->getValueAsInt("TimeInc");
232 ItinString
+= ", " + itostr(TimeInc
);
234 int Kind
= Stage
->getValueAsInt("Kind");
235 ItinString
+= ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind
);
239 if (++i
< N
) ItinString
+= ", ";
244 // FormItineraryOperandCycleString - Compose a string containing the
245 // operand cycle initialization for the specified itinerary. N is the
246 // number of operands that has cycles specified.
248 void SubtargetEmitter::FormItineraryOperandCycleString(Record
*ItinData
,
249 std::string
&ItinString
, unsigned &NOperandCycles
) {
250 // Get operand cycle list
251 const std::vector
<int64_t> &OperandCycleList
=
252 ItinData
->getValueAsListOfInts("OperandCycles");
254 // For each operand cycle
255 unsigned N
= NOperandCycles
= OperandCycleList
.size();
256 for (unsigned i
= 0; i
< N
;) {
257 // Next operand cycle
258 const int OCycle
= OperandCycleList
[i
];
260 ItinString
+= " " + itostr(OCycle
);
261 if (++i
< N
) ItinString
+= ", ";
265 void SubtargetEmitter::FormItineraryBypassString(const std::string
&Name
,
267 std::string
&ItinString
,
268 unsigned NOperandCycles
) {
269 const std::vector
<Record
*> &BypassList
=
270 ItinData
->getValueAsListOfDefs("Bypasses");
271 unsigned N
= BypassList
.size();
274 ItinString
+= Name
+ "Bypass::" + BypassList
[i
]->getName();
275 if (++i
< NOperandCycles
) ItinString
+= ", ";
277 for (; i
< NOperandCycles
;) {
279 if (++i
< NOperandCycles
) ItinString
+= ", ";
284 // EmitStageAndOperandCycleData - Generate unique itinerary stages and
285 // operand cycle tables. Record itineraries for processors.
287 void SubtargetEmitter::EmitStageAndOperandCycleData(raw_ostream
&OS
,
288 unsigned NItinClasses
,
289 std::map
<std::string
, unsigned> &ItinClassesMap
,
290 std::vector
<Record
*> &ItinClassList
,
291 std::vector
<std::vector
<InstrItinerary
> > &ProcList
) {
292 // Gather processor iteraries
293 std::vector
<Record
*> ProcItinList
=
294 Records
.getAllDerivedDefinitions("ProcessorItineraries");
296 // If just no itinerary then don't bother
297 if (ProcItinList
.size() < 2) return;
299 // Emit functional units for all the itineraries.
300 for (unsigned i
= 0, N
= ProcItinList
.size(); i
< N
; ++i
) {
302 Record
*Proc
= ProcItinList
[i
];
304 std::vector
<Record
*> FUs
= Proc
->getValueAsListOfDefs("FU");
308 const std::string
&Name
= Proc
->getName();
309 OS
<< "\n// Functional units for itineraries \"" << Name
<< "\"\n"
310 << "namespace " << Name
<< "FU {\n";
312 for (unsigned j
= 0, FUN
= FUs
.size(); j
< FUN
; ++j
)
313 OS
<< " const unsigned " << FUs
[j
]->getName()
314 << " = 1 << " << j
<< ";\n";
318 std::vector
<Record
*> BPs
= Proc
->getValueAsListOfDefs("BP");
320 OS
<< "\n// Pipeline forwarding pathes for itineraries \"" << Name
321 << "\"\n" << "namespace " << Name
<< "Bypass {\n";
323 OS
<< " const unsigned NoBypass = 0;\n";
324 for (unsigned j
= 0, BPN
= BPs
.size(); j
< BPN
; ++j
)
325 OS
<< " const unsigned " << BPs
[j
]->getName()
326 << " = 1 << " << j
<< ";\n";
332 // Begin stages table
333 std::string StageTable
= "\nstatic const llvm::InstrStage Stages[] = {\n";
334 StageTable
+= " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
336 // Begin operand cycle table
337 std::string OperandCycleTable
= "static const unsigned OperandCycles[] = {\n";
338 OperandCycleTable
+= " 0, // No itinerary\n";
340 // Begin pipeline bypass table
341 std::string BypassTable
= "static const unsigned ForwardingPathes[] = {\n";
342 BypassTable
+= " 0, // No itinerary\n";
344 unsigned StageCount
= 1, OperandCycleCount
= 1;
345 std::map
<std::string
, unsigned> ItinStageMap
, ItinOperandMap
;
346 for (unsigned i
= 0, N
= ProcItinList
.size(); i
< N
; i
++) {
348 Record
*Proc
= ProcItinList
[i
];
350 // Get processor itinerary name
351 const std::string
&Name
= Proc
->getName();
354 if (Name
== "NoItineraries") continue;
356 // Create and expand processor itinerary to cover all itinerary classes
357 std::vector
<InstrItinerary
> ItinList
;
358 ItinList
.resize(NItinClasses
);
360 // Get itinerary data list
361 std::vector
<Record
*> ItinDataList
= Proc
->getValueAsListOfDefs("IID");
363 // For each itinerary data
364 for (unsigned j
= 0, M
= ItinDataList
.size(); j
< M
; j
++) {
365 // Next itinerary data
366 Record
*ItinData
= ItinDataList
[j
];
368 // Get string and stage count
369 std::string ItinStageString
;
371 FormItineraryStageString(Name
, ItinData
, ItinStageString
, NStages
);
373 // Get string and operand cycle count
374 std::string ItinOperandCycleString
;
375 unsigned NOperandCycles
;
376 FormItineraryOperandCycleString(ItinData
, ItinOperandCycleString
,
379 std::string ItinBypassString
;
380 FormItineraryBypassString(Name
, ItinData
, ItinBypassString
,
383 // Check to see if stage already exists and create if it doesn't
384 unsigned FindStage
= 0;
386 FindStage
= ItinStageMap
[ItinStageString
];
387 if (FindStage
== 0) {
388 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
389 StageTable
+= ItinStageString
+ ", // " + itostr(StageCount
);
391 StageTable
+= "-" + itostr(StageCount
+ NStages
- 1);
393 // Record Itin class number.
394 ItinStageMap
[ItinStageString
] = FindStage
= StageCount
;
395 StageCount
+= NStages
;
399 // Check to see if operand cycle already exists and create if it doesn't
400 unsigned FindOperandCycle
= 0;
401 if (NOperandCycles
> 0) {
402 std::string ItinOperandString
= ItinOperandCycleString
+ItinBypassString
;
403 FindOperandCycle
= ItinOperandMap
[ItinOperandString
];
404 if (FindOperandCycle
== 0) {
405 // Emit as cycle, // index
406 OperandCycleTable
+= ItinOperandCycleString
+ ", // ";
407 std::string OperandIdxComment
= itostr(OperandCycleCount
);
408 if (NOperandCycles
> 1)
409 OperandIdxComment
+= "-"
410 + itostr(OperandCycleCount
+ NOperandCycles
- 1);
411 OperandCycleTable
+= OperandIdxComment
+ "\n";
412 // Record Itin class number.
413 ItinOperandMap
[ItinOperandCycleString
] =
414 FindOperandCycle
= OperandCycleCount
;
415 // Emit as bypass, // index
416 BypassTable
+= ItinBypassString
+ ", // " + OperandIdxComment
+ "\n";
417 OperandCycleCount
+= NOperandCycles
;
421 // Locate where to inject into processor itinerary table
422 const std::string
&Name
= ItinData
->getValueAsDef("TheClass")->getName();
423 unsigned Find
= ItinClassesMap
[Name
];
425 // Set up itinerary as location and location + stage count
426 unsigned NumUOps
= ItinClassList
[Find
]->getValueAsInt("NumMicroOps");
427 InstrItinerary Intinerary
= { NumUOps
, FindStage
, FindStage
+ NStages
,
429 FindOperandCycle
+ NOperandCycles
};
431 // Inject - empty slots will be 0, 0
432 ItinList
[Find
] = Intinerary
;
435 // Add process itinerary to list
436 ProcList
.push_back(ItinList
);
440 StageTable
+= " { 0, 0, 0, llvm::InstrStage::Required } // End itinerary\n";
441 StageTable
+= "};\n";
443 // Closing operand cycles
444 OperandCycleTable
+= " 0 // End itinerary\n";
445 OperandCycleTable
+= "};\n";
447 BypassTable
+= " 0 // End itinerary\n";
448 BypassTable
+= "};\n";
452 OS
<< OperandCycleTable
;
455 // Emit size of tables
457 OS
<<" StagesSize = sizeof(Stages)/sizeof(llvm::InstrStage),\n";
458 OS
<<" OperandCyclesSize = sizeof(OperandCycles)/sizeof(unsigned)\n";
463 // EmitProcessorData - Generate data for processor itineraries.
465 void SubtargetEmitter::
466 EmitProcessorData(raw_ostream
&OS
,
467 std::vector
<Record
*> &ItinClassList
,
468 std::vector
<std::vector
<InstrItinerary
> > &ProcList
) {
469 // Get an iterator for processor itinerary stages
470 std::vector
<std::vector
<InstrItinerary
> >::iterator
471 ProcListIter
= ProcList
.begin();
473 // For each processor itinerary
474 std::vector
<Record
*> Itins
=
475 Records
.getAllDerivedDefinitions("ProcessorItineraries");
476 for (unsigned i
= 0, N
= Itins
.size(); i
< N
; i
++) {
478 Record
*Itin
= Itins
[i
];
480 // Get processor itinerary name
481 const std::string
&Name
= Itin
->getName();
484 if (Name
== "NoItineraries") continue;
486 // Begin processor itinerary table
488 OS
<< "static const llvm::InstrItinerary " << Name
<< "[] = {\n";
490 // For each itinerary class
491 std::vector
<InstrItinerary
> &ItinList
= *ProcListIter
++;
492 assert(ItinList
.size() == ItinClassList
.size() && "bad itinerary");
493 for (unsigned j
= 0, M
= ItinList
.size(); j
< M
; ++j
) {
494 InstrItinerary
&Intinerary
= ItinList
[j
];
496 // Emit in the form of
497 // { firstStage, lastStage, firstCycle, lastCycle } // index
498 if (Intinerary
.FirstStage
== 0) {
499 OS
<< " { 1, 0, 0, 0, 0 }";
502 Intinerary
.NumMicroOps
<< ", " <<
503 Intinerary
.FirstStage
<< ", " <<
504 Intinerary
.LastStage
<< ", " <<
505 Intinerary
.FirstOperandCycle
<< ", " <<
506 Intinerary
.LastOperandCycle
<< " }";
509 OS
<< ", // " << j
<< " " << ItinClassList
[j
]->getName() << "\n";
512 // End processor itinerary table
513 OS
<< " { 1, ~0U, ~0U, ~0U, ~0U } // end marker\n";
519 // EmitProcessorLookup - generate cpu name to itinerary lookup table.
521 void SubtargetEmitter::EmitProcessorLookup(raw_ostream
&OS
) {
522 // Gather and sort processor information
523 std::vector
<Record
*> ProcessorList
=
524 Records
.getAllDerivedDefinitions("Processor");
525 std::sort(ProcessorList
.begin(), ProcessorList
.end(), LessRecordFieldName());
527 // Begin processor table
529 OS
<< "// Sorted (by key) array of itineraries for CPU subtype.\n"
530 << "static const llvm::SubtargetInfoKV ProcItinKV[] = {\n";
532 // For each processor
533 for (unsigned i
= 0, N
= ProcessorList
.size(); i
< N
;) {
535 Record
*Processor
= ProcessorList
[i
];
537 const std::string
&Name
= Processor
->getValueAsString("Name");
538 const std::string
&ProcItin
=
539 Processor
->getValueAsDef("ProcItin")->getName();
541 // Emit as { "cpu", procinit },
543 << "\"" << Name
<< "\", "
544 << "(void *)&" << ProcItin
;
548 // Depending on ''if more in the list'' emit comma
549 if (++i
< N
) OS
<< ",";
554 // End processor table
557 // Emit size of table
559 OS
<<" ProcItinKVSize = sizeof(ProcItinKV)/"
560 "sizeof(llvm::SubtargetInfoKV)\n";
565 // EmitData - Emits all stages and itineries, folding common patterns.
567 void SubtargetEmitter::EmitData(raw_ostream
&OS
) {
568 std::map
<std::string
, unsigned> ItinClassesMap
;
569 // Gather and sort all itinerary classes
570 std::vector
<Record
*> ItinClassList
=
571 Records
.getAllDerivedDefinitions("InstrItinClass");
572 std::sort(ItinClassList
.begin(), ItinClassList
.end(), LessRecord());
574 // Enumerate all the itinerary classes
575 unsigned NItinClasses
= CollectAllItinClasses(OS
, ItinClassesMap
,
577 // Make sure the rest is worth the effort
578 HasItineraries
= NItinClasses
!= 1; // Ignore NoItinerary.
580 if (HasItineraries
) {
581 std::vector
<std::vector
<InstrItinerary
> > ProcList
;
582 // Emit the stage data
583 EmitStageAndOperandCycleData(OS
, NItinClasses
, ItinClassesMap
,
584 ItinClassList
, ProcList
);
585 // Emit the processor itinerary data
586 EmitProcessorData(OS
, ItinClassList
, ProcList
);
587 // Emit the processor lookup data
588 EmitProcessorLookup(OS
);
593 // ParseFeaturesFunction - Produces a subtarget specific function for parsing
594 // the subtarget features string.
596 void SubtargetEmitter::ParseFeaturesFunction(raw_ostream
&OS
) {
597 std::vector
<Record
*> Features
=
598 Records
.getAllDerivedDefinitions("SubtargetFeature");
599 std::sort(Features
.begin(), Features
.end(), LessRecord());
601 OS
<< "// ParseSubtargetFeatures - Parses features string setting specified\n"
602 << "// subtarget options.\n"
603 << "std::string llvm::";
605 OS
<< "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
606 << " const std::string &CPU) {\n"
607 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
608 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU);\n"
609 << " SubtargetFeatures Features(FS);\n"
610 << " Features.setCPUIfNone(CPU);\n"
611 << " uint32_t Bits = Features.getBits(SubTypeKV, SubTypeKVSize,\n"
612 << " FeatureKV, FeatureKVSize);\n";
614 for (unsigned i
= 0; i
< Features
.size(); i
++) {
616 Record
*R
= Features
[i
];
617 const std::string
&Instance
= R
->getName();
618 const std::string
&Value
= R
->getValueAsString("Value");
619 const std::string
&Attribute
= R
->getValueAsString("Attribute");
621 if (Value
=="true" || Value
=="false")
622 OS
<< " if ((Bits & " << Instance
<< ") != 0) "
623 << Attribute
<< " = " << Value
<< ";\n";
625 OS
<< " if ((Bits & " << Instance
<< ") != 0 && " << Attribute
<<
626 " < " << Value
<< ") " << Attribute
<< " = " << Value
<< ";\n";
629 if (HasItineraries
) {
631 << " InstrItinerary *Itinerary = (InstrItinerary *)"
632 << "Features.getInfo(ProcItinKV, ProcItinKVSize);\n"
633 << " InstrItins = InstrItineraryData(Stages, OperandCycles, "
634 << "ForwardingPathes, Itinerary);\n";
637 OS
<< " return Features.getCPU();\n"
642 // SubtargetEmitter::run - Main subtarget enumeration emitter.
644 void SubtargetEmitter::run(raw_ostream
&OS
) {
645 Target
= CodeGenTarget(Records
).getName();
647 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS
);
649 OS
<< "#include \"llvm/Support/Debug.h\"\n";
650 OS
<< "#include \"llvm/Support/raw_ostream.h\"\n";
651 OS
<< "#include \"llvm/Target/SubtargetFeature.h\"\n";
652 OS
<< "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n";
654 // Enumeration(OS, "FuncUnit", true);
656 // Enumeration(OS, "InstrItinClass", false);
658 Enumeration(OS
, "SubtargetFeature", true);
660 FeatureKeyValues(OS
);
666 ParseFeaturesFunction(OS
);