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"
16 #include "llvm/TableGen/Record.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());
32 unsigned N = DefList.size();
36 errs() << "Too many (> 64) subtarget features!\n";
40 OS << "namespace " << Target << " {\n";
46 for (unsigned i = 0; i < N;) {
48 Record *Def = DefList[i];
51 OS << " " << Def->getName();
53 // If bit flags then emit expression (1 << i)
54 if (isBits) OS << " = " << " 1ULL << " << i;
56 // Depending on 'if more in the list' emit comma
57 if (++i < N) OS << ",";
69 // FeatureKeyValues - Emit data of all the subtarget features. Used by the
72 unsigned SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) {
73 // Gather and sort all the features
74 std::vector<Record*> FeatureList =
75 Records.getAllDerivedDefinitions("SubtargetFeature");
77 if (FeatureList.empty())
80 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
82 // Begin feature table
83 OS << "// Sorted (by key) array of values for CPU features.\n"
84 << "extern const llvm::SubtargetFeatureKV " << Target
85 << "FeatureKV[] = {\n";
88 unsigned NumFeatures = 0;
89 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
91 Record *Feature = FeatureList[i];
93 const std::string &Name = Feature->getName();
94 const std::string &CommandLineName = Feature->getValueAsString("Name");
95 const std::string &Desc = Feature->getValueAsString("Desc");
97 if (CommandLineName.empty()) continue;
99 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in }
101 << "\"" << CommandLineName << "\", "
102 << "\"" << Desc << "\", "
103 << Target << "::" << Name << ", ";
105 const std::vector<Record*> &ImpliesList =
106 Feature->getValueAsListOfDefs("Implies");
108 if (ImpliesList.empty()) {
111 for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
112 OS << Target << "::" << ImpliesList[j]->getName();
113 if (++j < M) OS << " | ";
120 // Depending on 'if more in the list' emit comma
121 if ((i + 1) < N) OS << ",";
133 // CPUKeyValues - Emit data of all the subtarget processors. Used by command
136 unsigned SubtargetEmitter::CPUKeyValues(raw_ostream &OS) {
137 // Gather and sort processor information
138 std::vector<Record*> ProcessorList =
139 Records.getAllDerivedDefinitions("Processor");
140 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
142 // Begin processor table
143 OS << "// Sorted (by key) array of values for CPU subtype.\n"
144 << "extern const llvm::SubtargetFeatureKV " << Target
145 << "SubTypeKV[] = {\n";
147 // For each processor
148 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
150 Record *Processor = ProcessorList[i];
152 const std::string &Name = Processor->getValueAsString("Name");
153 const std::vector<Record*> &FeatureList =
154 Processor->getValueAsListOfDefs("Features");
156 // Emit as { "cpu", "description", f1 | f2 | ... fn },
158 << "\"" << Name << "\", "
159 << "\"Select the " << Name << " processor\", ";
161 if (FeatureList.empty()) {
164 for (unsigned j = 0, M = FeatureList.size(); j < M;) {
165 OS << Target << "::" << FeatureList[j]->getName();
166 if (++j < M) OS << " | ";
170 // The "0" is for the "implies" section of this data structure.
173 // Depending on 'if more in the list' emit comma
174 if (++i < N) OS << ",";
179 // End processor table
182 return ProcessorList.size();
186 // CollectAllItinClasses - Gathers and enumerates all the itinerary classes.
187 // Returns itinerary class count.
189 unsigned SubtargetEmitter::
190 CollectAllItinClasses(raw_ostream &OS,
191 std::map<std::string, unsigned> &ItinClassesMap,
192 std::vector<Record*> &ItinClassList) {
193 // For each itinerary class
194 unsigned N = ItinClassList.size();
195 for (unsigned i = 0; i < N; i++) {
196 // Next itinerary class
197 const Record *ItinClass = ItinClassList[i];
198 // Get name of itinerary class
199 // Assign itinerary class a unique number
200 ItinClassesMap[ItinClass->getName()] = i;
203 // Return itinerary class count
208 // FormItineraryStageString - Compose a string containing the stage
209 // data initialization for the specified itinerary. N is the number
212 void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
214 std::string &ItinString,
217 const std::vector<Record*> &StageList =
218 ItinData->getValueAsListOfDefs("Stages");
221 unsigned N = NStages = StageList.size();
222 for (unsigned i = 0; i < N;) {
224 const Record *Stage = StageList[i];
226 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
227 int Cycles = Stage->getValueAsInt("Cycles");
228 ItinString += " { " + itostr(Cycles) + ", ";
231 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
234 for (unsigned j = 0, M = UnitList.size(); j < M;) {
235 // Add name and bitwise or
236 ItinString += Name + "FU::" + UnitList[j]->getName();
237 if (++j < M) ItinString += " | ";
240 int TimeInc = Stage->getValueAsInt("TimeInc");
241 ItinString += ", " + itostr(TimeInc);
243 int Kind = Stage->getValueAsInt("Kind");
244 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
248 if (++i < N) ItinString += ", ";
253 // FormItineraryOperandCycleString - Compose a string containing the
254 // operand cycle initialization for the specified itinerary. N is the
255 // number of operands that has cycles specified.
257 void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
258 std::string &ItinString, unsigned &NOperandCycles) {
259 // Get operand cycle list
260 const std::vector<int64_t> &OperandCycleList =
261 ItinData->getValueAsListOfInts("OperandCycles");
263 // For each operand cycle
264 unsigned N = NOperandCycles = OperandCycleList.size();
265 for (unsigned i = 0; i < N;) {
266 // Next operand cycle
267 const int OCycle = OperandCycleList[i];
269 ItinString += " " + itostr(OCycle);
270 if (++i < N) ItinString += ", ";
274 void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
276 std::string &ItinString,
277 unsigned NOperandCycles) {
278 const std::vector<Record*> &BypassList =
279 ItinData->getValueAsListOfDefs("Bypasses");
280 unsigned N = BypassList.size();
283 ItinString += Name + "Bypass::" + BypassList[i]->getName();
284 if (++i < NOperandCycles) ItinString += ", ";
286 for (; i < NOperandCycles;) {
288 if (++i < NOperandCycles) ItinString += ", ";
293 // EmitStageAndOperandCycleData - Generate unique itinerary stages and
294 // operand cycle tables. Record itineraries for processors.
296 void SubtargetEmitter::EmitStageAndOperandCycleData(raw_ostream &OS,
297 unsigned NItinClasses,
298 std::map<std::string, unsigned> &ItinClassesMap,
299 std::vector<Record*> &ItinClassList,
300 std::vector<std::vector<InstrItinerary> > &ProcList) {
301 // Gather processor iteraries
302 std::vector<Record*> ProcItinList =
303 Records.getAllDerivedDefinitions("ProcessorItineraries");
305 // If just no itinerary then don't bother
306 if (ProcItinList.size() < 2) return;
308 // Emit functional units for all the itineraries.
309 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
311 Record *Proc = ProcItinList[i];
313 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
317 const std::string &Name = Proc->getName();
318 OS << "\n// Functional units for itineraries \"" << Name << "\"\n"
319 << "namespace " << Name << "FU {\n";
321 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
322 OS << " const unsigned " << FUs[j]->getName()
323 << " = 1 << " << j << ";\n";
327 std::vector<Record*> BPs = Proc->getValueAsListOfDefs("BP");
329 OS << "\n// Pipeline forwarding pathes for itineraries \"" << Name
330 << "\"\n" << "namespace " << Name << "Bypass {\n";
332 OS << " const unsigned NoBypass = 0;\n";
333 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
334 OS << " const unsigned " << BPs[j]->getName()
335 << " = 1 << " << j << ";\n";
341 // Begin stages table
342 std::string StageTable = "\nextern const llvm::InstrStage " + Target +
344 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
346 // Begin operand cycle table
347 std::string OperandCycleTable = "extern const unsigned " + Target +
348 "OperandCycles[] = {\n";
349 OperandCycleTable += " 0, // No itinerary\n";
351 // Begin pipeline bypass table
352 std::string BypassTable = "extern const unsigned " + Target +
353 "ForwardingPathes[] = {\n";
354 BypassTable += " 0, // No itinerary\n";
356 unsigned StageCount = 1, OperandCycleCount = 1;
357 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
358 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
360 Record *Proc = ProcItinList[i];
362 // Get processor itinerary name
363 const std::string &Name = Proc->getName();
366 if (Name == "NoItineraries") continue;
368 // Create and expand processor itinerary to cover all itinerary classes
369 std::vector<InstrItinerary> ItinList;
370 ItinList.resize(NItinClasses);
372 // Get itinerary data list
373 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
375 // For each itinerary data
376 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
377 // Next itinerary data
378 Record *ItinData = ItinDataList[j];
380 // Get string and stage count
381 std::string ItinStageString;
383 FormItineraryStageString(Name, ItinData, ItinStageString, NStages);
385 // Get string and operand cycle count
386 std::string ItinOperandCycleString;
387 unsigned NOperandCycles;
388 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
391 std::string ItinBypassString;
392 FormItineraryBypassString(Name, ItinData, ItinBypassString,
395 // Check to see if stage already exists and create if it doesn't
396 unsigned FindStage = 0;
398 FindStage = ItinStageMap[ItinStageString];
399 if (FindStage == 0) {
400 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
401 StageTable += ItinStageString + ", // " + itostr(StageCount);
403 StageTable += "-" + itostr(StageCount + NStages - 1);
405 // Record Itin class number.
406 ItinStageMap[ItinStageString] = FindStage = StageCount;
407 StageCount += NStages;
411 // Check to see if operand cycle already exists and create if it doesn't
412 unsigned FindOperandCycle = 0;
413 if (NOperandCycles > 0) {
414 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
415 FindOperandCycle = ItinOperandMap[ItinOperandString];
416 if (FindOperandCycle == 0) {
417 // Emit as cycle, // index
418 OperandCycleTable += ItinOperandCycleString + ", // ";
419 std::string OperandIdxComment = itostr(OperandCycleCount);
420 if (NOperandCycles > 1)
421 OperandIdxComment += "-"
422 + itostr(OperandCycleCount + NOperandCycles - 1);
423 OperandCycleTable += OperandIdxComment + "\n";
424 // Record Itin class number.
425 ItinOperandMap[ItinOperandCycleString] =
426 FindOperandCycle = OperandCycleCount;
427 // Emit as bypass, // index
428 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
429 OperandCycleCount += NOperandCycles;
433 // Locate where to inject into processor itinerary table
434 const std::string &Name = ItinData->getValueAsDef("TheClass")->getName();
435 unsigned Find = ItinClassesMap[Name];
437 // Set up itinerary as location and location + stage count
438 unsigned NumUOps = ItinClassList[Find]->getValueAsInt("NumMicroOps");
439 InstrItinerary Intinerary = { NumUOps, FindStage, FindStage + NStages,
441 FindOperandCycle + NOperandCycles};
443 // Inject - empty slots will be 0, 0
444 ItinList[Find] = Intinerary;
447 // Add process itinerary to list
448 ProcList.push_back(ItinList);
452 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End itinerary\n";
453 StageTable += "};\n";
455 // Closing operand cycles
456 OperandCycleTable += " 0 // End itinerary\n";
457 OperandCycleTable += "};\n";
459 BypassTable += " 0 // End itinerary\n";
460 BypassTable += "};\n";
464 OS << OperandCycleTable;
469 // EmitProcessorData - Generate data for processor itineraries.
471 void SubtargetEmitter::
472 EmitProcessorData(raw_ostream &OS,
473 std::vector<Record*> &ItinClassList,
474 std::vector<std::vector<InstrItinerary> > &ProcList) {
475 // Get an iterator for processor itinerary stages
476 std::vector<std::vector<InstrItinerary> >::iterator
477 ProcListIter = ProcList.begin();
479 // For each processor itinerary
480 std::vector<Record*> Itins =
481 Records.getAllDerivedDefinitions("ProcessorItineraries");
482 for (unsigned i = 0, N = Itins.size(); i < N; i++) {
484 Record *Itin = Itins[i];
486 // Get processor itinerary name
487 const std::string &Name = Itin->getName();
490 if (Name == "NoItineraries") continue;
492 // Begin processor itinerary table
494 OS << "static const llvm::InstrItinerary " << Name << "[] = {\n";
496 // For each itinerary class
497 std::vector<InstrItinerary> &ItinList = *ProcListIter++;
498 assert(ItinList.size() == ItinClassList.size() && "bad itinerary");
499 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
500 InstrItinerary &Intinerary = ItinList[j];
502 // Emit in the form of
503 // { firstStage, lastStage, firstCycle, lastCycle } // index
504 if (Intinerary.FirstStage == 0) {
505 OS << " { 1, 0, 0, 0, 0 }";
508 Intinerary.NumMicroOps << ", " <<
509 Intinerary.FirstStage << ", " <<
510 Intinerary.LastStage << ", " <<
511 Intinerary.FirstOperandCycle << ", " <<
512 Intinerary.LastOperandCycle << " }";
515 OS << ", // " << j << " " << ItinClassList[j]->getName() << "\n";
518 // End processor itinerary table
519 OS << " { 1, ~0U, ~0U, ~0U, ~0U } // end marker\n";
525 // EmitProcessorLookup - generate cpu name to itinerary lookup table.
527 void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) {
528 // Gather and sort processor information
529 std::vector<Record*> ProcessorList =
530 Records.getAllDerivedDefinitions("Processor");
531 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName());
533 // Begin processor table
535 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n"
536 << "extern const llvm::SubtargetInfoKV "
537 << Target << "ProcItinKV[] = {\n";
539 // For each processor
540 for (unsigned i = 0, N = ProcessorList.size(); i < N;) {
542 Record *Processor = ProcessorList[i];
544 const std::string &Name = Processor->getValueAsString("Name");
545 const std::string &ProcItin =
546 Processor->getValueAsDef("ProcItin")->getName();
548 // Emit as { "cpu", procinit },
550 << "\"" << Name << "\", "
551 << "(void *)&" << ProcItin;
555 // Depending on ''if more in the list'' emit comma
556 if (++i < N) OS << ",";
561 // End processor table
566 // EmitData - Emits all stages and itineries, folding common patterns.
568 void SubtargetEmitter::EmitData(raw_ostream &OS) {
569 std::map<std::string, unsigned> ItinClassesMap;
570 // Gather and sort all itinerary classes
571 std::vector<Record*> ItinClassList =
572 Records.getAllDerivedDefinitions("InstrItinClass");
573 std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord());
575 // Enumerate all the itinerary classes
576 unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap,
578 // Make sure the rest is worth the effort
579 HasItineraries = NItinClasses != 1; // Ignore NoItinerary.
581 if (HasItineraries) {
582 std::vector<std::vector<InstrItinerary> > ProcList;
583 // Emit the stage data
584 EmitStageAndOperandCycleData(OS, NItinClasses, ItinClassesMap,
585 ItinClassList, ProcList);
586 // Emit the processor itinerary data
587 EmitProcessorData(OS, ItinClassList, ProcList);
588 // Emit the processor lookup data
589 EmitProcessorLookup(OS);
594 // ParseFeaturesFunction - Produces a subtarget specific function for parsing
595 // the subtarget features string.
597 void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
598 unsigned NumFeatures,
600 std::vector<Record*> Features =
601 Records.getAllDerivedDefinitions("SubtargetFeature");
602 std::sort(Features.begin(), Features.end(), LessRecord());
604 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
605 << "// subtarget options.\n"
608 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef FS) {\n"
609 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
610 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU);\n";
612 if (Features.empty()) {
617 OS << " uint64_t Bits = ReInitMCSubtargetInfo(CPU, FS);\n";
619 for (unsigned i = 0; i < Features.size(); i++) {
621 Record *R = Features[i];
622 const std::string &Instance = R->getName();
623 const std::string &Value = R->getValueAsString("Value");
624 const std::string &Attribute = R->getValueAsString("Attribute");
626 if (Value=="true" || Value=="false")
627 OS << " if ((Bits & " << Target << "::"
628 << Instance << ") != 0) "
629 << Attribute << " = " << Value << ";\n";
631 OS << " if ((Bits & " << Target << "::"
632 << Instance << ") != 0 && "
633 << Attribute << " < " << Value << ") "
634 << Attribute << " = " << Value << ";\n";
641 // SubtargetEmitter::run - Main subtarget enumeration emitter.
643 void SubtargetEmitter::run(raw_ostream &OS) {
644 Target = CodeGenTarget(Records).getName();
646 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
648 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
649 OS << "#undef GET_SUBTARGETINFO_ENUM\n";
651 OS << "namespace llvm {\n";
652 Enumeration(OS, "SubtargetFeature", true);
653 OS << "} // End llvm namespace \n";
654 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
656 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
657 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n";
659 OS << "namespace llvm {\n";
661 OS << "namespace {\n";
663 unsigned NumFeatures = FeatureKeyValues(OS);
665 unsigned NumProcs = CPUKeyValues(OS);
673 // MCInstrInfo initialization routine.
674 OS << "static inline void Init" << Target
675 << "MCSubtargetInfo(MCSubtargetInfo *II, "
676 << "StringRef TT, StringRef CPU, StringRef FS) {\n";
677 OS << " II->InitMCSubtargetInfo(TT, CPU, FS, ";
679 OS << Target << "FeatureKV, ";
683 OS << Target << "SubTypeKV, ";
686 if (HasItineraries) {
687 OS << Target << "ProcItinKV, "
688 << Target << "Stages, "
689 << Target << "OperandCycles, "
690 << Target << "ForwardingPathes, ";
692 OS << "0, 0, 0, 0, ";
693 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
695 OS << "} // End llvm namespace \n";
697 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
699 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
700 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n";
702 OS << "#include \"llvm/Support/Debug.h\"\n";
703 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
704 ParseFeaturesFunction(OS, NumFeatures, NumProcs);
706 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
708 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
709 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
710 OS << "#undef GET_SUBTARGETINFO_HEADER\n";
712 std::string ClassName = Target + "GenSubtargetInfo";
713 OS << "namespace llvm {\n";
714 OS << "class DFAPacketizer;\n";
715 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
716 << " explicit " << ClassName << "(StringRef TT, StringRef CPU, "
717 << "StringRef FS);\n"
719 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
722 OS << "} // End llvm namespace \n";
724 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
726 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
727 OS << "#undef GET_SUBTARGETINFO_CTOR\n";
729 OS << "namespace llvm {\n";
730 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
731 OS << "extern const llvm::SubtargetFeatureKV " << Target << "SubTypeKV[];\n";
732 if (HasItineraries) {
733 OS << "extern const llvm::SubtargetInfoKV " << Target << "ProcItinKV[];\n";
734 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
735 OS << "extern const unsigned " << Target << "OperandCycles[];\n";
736 OS << "extern const unsigned " << Target << "ForwardingPathes[];\n";
739 OS << ClassName << "::" << ClassName << "(StringRef TT, StringRef CPU, "
741 << " : TargetSubtargetInfo() {\n"
742 << " InitMCSubtargetInfo(TT, CPU, FS, ";
744 OS << Target << "FeatureKV, ";
748 OS << Target << "SubTypeKV, ";
751 if (HasItineraries) {
752 OS << Target << "ProcItinKV, "
753 << Target << "Stages, "
754 << Target << "OperandCycles, "
755 << Target << "ForwardingPathes, ";
757 OS << "0, 0, 0, 0, ";
758 OS << NumFeatures << ", " << NumProcs << ");\n}\n\n";
759 OS << "} // End llvm namespace \n";
761 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";