1 //===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This tablegen backend emits subtarget enumerations.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CodeGenTarget.h"
14 #include "CodeGenSchedule.h"
15 #include "PredicateExpander.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/MC/MCInstrItineraries.h"
21 #include "llvm/MC/MCSchedule.h"
22 #include "llvm/MC/SubtargetFeature.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/TableGen/Error.h"
27 #include "llvm/TableGen/Record.h"
28 #include "llvm/TableGen/TableGenBackend.h"
29 #include <algorithm>
30 #include <cassert>
31 #include <cstdint>
32 #include <iterator>
33 #include <map>
34 #include <string>
35 #include <vector>
36 
37 using namespace llvm;
38 
39 #define DEBUG_TYPE "subtarget-emitter"
40 
41 namespace {
42 
43 class SubtargetEmitter {
44   // Each processor has a SchedClassDesc table with an entry for each SchedClass.
45   // The SchedClassDesc table indexes into a global write resource table, write
46   // latency table, and read advance table.
47   struct SchedClassTables {
48     std::vector<std::vector<MCSchedClassDesc>> ProcSchedClasses;
49     std::vector<MCWriteProcResEntry> WriteProcResources;
50     std::vector<MCWriteLatencyEntry> WriteLatencies;
51     std::vector<std::string> WriterNames;
52     std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
53 
54     // Reserve an invalid entry at index 0
55     SchedClassTables() {
56       ProcSchedClasses.resize(1);
57       WriteProcResources.resize(1);
58       WriteLatencies.resize(1);
59       WriterNames.push_back("InvalidWrite");
60       ReadAdvanceEntries.resize(1);
61     }
62   };
63 
64   struct LessWriteProcResources {
65     bool operator()(const MCWriteProcResEntry &LHS,
66                     const MCWriteProcResEntry &RHS) {
67       return LHS.ProcResourceIdx < RHS.ProcResourceIdx;
68     }
69   };
70 
71   const CodeGenTarget &TGT;
72   RecordKeeper &Records;
73   CodeGenSchedModels &SchedModels;
74   std::string Target;
75 
76   void Enumeration(raw_ostream &OS, DenseMap<Record *, unsigned> &FeatureMap);
77   unsigned FeatureKeyValues(raw_ostream &OS,
78                             const DenseMap<Record *, unsigned> &FeatureMap);
79   unsigned CPUKeyValues(raw_ostream &OS,
80                         const DenseMap<Record *, unsigned> &FeatureMap);
81   void FormItineraryStageString(const std::string &Names,
82                                 Record *ItinData, std::string &ItinString,
83                                 unsigned &NStages);
84   void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString,
85                                        unsigned &NOperandCycles);
86   void FormItineraryBypassString(const std::string &Names,
87                                  Record *ItinData,
88                                  std::string &ItinString, unsigned NOperandCycles);
89   void EmitStageAndOperandCycleData(raw_ostream &OS,
90                                     std::vector<std::vector<InstrItinerary>>
91                                       &ProcItinLists);
92   void EmitItineraries(raw_ostream &OS,
93                        std::vector<std::vector<InstrItinerary>>
94                          &ProcItinLists);
95   unsigned EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
96                                   raw_ostream &OS);
97   void EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
98                               raw_ostream &OS);
99   void EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
100                               raw_ostream &OS);
101   void EmitProcessorProp(raw_ostream &OS, const Record *R, StringRef Name,
102                          char Separator);
103   void EmitProcessorResourceSubUnits(const CodeGenProcModel &ProcModel,
104                                      raw_ostream &OS);
105   void EmitProcessorResources(const CodeGenProcModel &ProcModel,
106                               raw_ostream &OS);
107   Record *FindWriteResources(const CodeGenSchedRW &SchedWrite,
108                              const CodeGenProcModel &ProcModel);
109   Record *FindReadAdvance(const CodeGenSchedRW &SchedRead,
110                           const CodeGenProcModel &ProcModel);
111   void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles,
112                            const CodeGenProcModel &ProcModel);
113   void GenSchedClassTables(const CodeGenProcModel &ProcModel,
114                            SchedClassTables &SchedTables);
115   void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS);
116   void EmitProcessorModels(raw_ostream &OS);
117   void EmitSchedModelHelpers(const std::string &ClassName, raw_ostream &OS);
118   void emitSchedModelHelpersImpl(raw_ostream &OS,
119                                  bool OnlyExpandMCInstPredicates = false);
120   void emitGenMCSubtargetInfo(raw_ostream &OS);
121   void EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS);
122 
123   void EmitSchedModel(raw_ostream &OS);
124   void EmitHwModeCheck(const std::string &ClassName, raw_ostream &OS);
125   void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures,
126                              unsigned NumProcs);
127 
128 public:
129   SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT)
130       : TGT(TGT), Records(R), SchedModels(TGT.getSchedModels()),
131         Target(TGT.getName()) {}
132 
133   void run(raw_ostream &o);
134 };
135 
136 } // end anonymous namespace
137 
138 //
139 // Enumeration - Emit the specified class as an enumeration.
140 //
141 void SubtargetEmitter::Enumeration(raw_ostream &OS,
142                                    DenseMap<Record *, unsigned> &FeatureMap) {
143   // Get all records of class and sort
144   std::vector<Record*> DefList =
145     Records.getAllDerivedDefinitions("SubtargetFeature");
146   llvm::sort(DefList, LessRecord());
147 
148   unsigned N = DefList.size();
149   if (N == 0)
150     return;
151   if (N + 1 > MAX_SUBTARGET_FEATURES)
152     PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
153 
154   OS << "namespace " << Target << " {\n";
155 
156   // Open enumeration.
157   OS << "enum {\n";
158 
159   // For each record
160   for (unsigned i = 0; i < N; ++i) {
161     // Next record
162     Record *Def = DefList[i];
163 
164     // Get and emit name
165     OS << "  " << Def->getName() << " = " << i << ",\n";
166 
167     // Save the index for this feature.
168     FeatureMap[Def] = i;
169   }
170 
171   OS << "  "
172      << "NumSubtargetFeatures = " << N << "\n";
173 
174   // Close enumeration and namespace
175   OS << "};\n";
176   OS << "} // end namespace " << Target << "\n";
177 }
178 
179 static void printFeatureMask(raw_ostream &OS, RecVec &FeatureList,
180                              const DenseMap<Record *, unsigned> &FeatureMap) {
181   std::array<uint64_t, MAX_SUBTARGET_WORDS> Mask = {};
182   for (const Record *Feature : FeatureList) {
183     unsigned Bit = FeatureMap.lookup(Feature);
184     Mask[Bit / 64] |= 1ULL << (Bit % 64);
185   }
186 
187   OS << "{ { { ";
188   for (unsigned i = 0; i != Mask.size(); ++i) {
189     OS << "0x";
190     OS.write_hex(Mask[i]);
191     OS << "ULL, ";
192   }
193   OS << "} } }";
194 }
195 
196 //
197 // FeatureKeyValues - Emit data of all the subtarget features.  Used by the
198 // command line.
199 //
200 unsigned SubtargetEmitter::FeatureKeyValues(
201     raw_ostream &OS, const DenseMap<Record *, unsigned> &FeatureMap) {
202   // Gather and sort all the features
203   std::vector<Record*> FeatureList =
204                            Records.getAllDerivedDefinitions("SubtargetFeature");
205 
206   if (FeatureList.empty())
207     return 0;
208 
209   llvm::sort(FeatureList, LessRecordFieldName());
210 
211   // Begin feature table
212   OS << "// Sorted (by key) array of values for CPU features.\n"
213      << "extern const llvm::SubtargetFeatureKV " << Target
214      << "FeatureKV[] = {\n";
215 
216   // For each feature
217   unsigned NumFeatures = 0;
218   for (const Record *Feature : FeatureList) {
219     // Next feature
220     StringRef Name = Feature->getName();
221     StringRef CommandLineName = Feature->getValueAsString("Name");
222     StringRef Desc = Feature->getValueAsString("Desc");
223 
224     if (CommandLineName.empty()) continue;
225 
226     // Emit as { "feature", "description", { featureEnum }, { i1 , i2 , ... , in } }
227     OS << "  { "
228        << "\"" << CommandLineName << "\", "
229        << "\"" << Desc << "\", "
230        << Target << "::" << Name << ", ";
231 
232     RecVec ImpliesList = Feature->getValueAsListOfDefs("Implies");
233 
234     printFeatureMask(OS, ImpliesList, FeatureMap);
235 
236     OS << " },\n";
237     ++NumFeatures;
238   }
239 
240   // End feature table
241   OS << "};\n";
242 
243   return NumFeatures;
244 }
245 
246 //
247 // CPUKeyValues - Emit data of all the subtarget processors.  Used by command
248 // line.
249 //
250 unsigned
251 SubtargetEmitter::CPUKeyValues(raw_ostream &OS,
252                                const DenseMap<Record *, unsigned> &FeatureMap) {
253   // Gather and sort processor information
254   std::vector<Record*> ProcessorList =
255                           Records.getAllDerivedDefinitions("Processor");
256   llvm::sort(ProcessorList, LessRecordFieldName());
257 
258   // Begin processor table
259   OS << "// Sorted (by key) array of values for CPU subtype.\n"
260      << "extern const llvm::SubtargetSubTypeKV " << Target
261      << "SubTypeKV[] = {\n";
262 
263   // For each processor
264   for (Record *Processor : ProcessorList) {
265     StringRef Name = Processor->getValueAsString("Name");
266     RecVec FeatureList = Processor->getValueAsListOfDefs("Features");
267     RecVec TuneFeatureList = Processor->getValueAsListOfDefs("TuneFeatures");
268 
269     // Emit as { "cpu", "description", 0, { f1 , f2 , ... fn } },
270     OS << " { "
271        << "\"" << Name << "\", ";
272 
273     printFeatureMask(OS, FeatureList, FeatureMap);
274     OS << ", ";
275     printFeatureMask(OS, TuneFeatureList, FeatureMap);
276 
277     // Emit the scheduler model pointer.
278     const std::string &ProcModelName =
279       SchedModels.getModelForProc(Processor).ModelName;
280     OS << ", &" << ProcModelName << " },\n";
281   }
282 
283   // End processor table
284   OS << "};\n";
285 
286   return ProcessorList.size();
287 }
288 
289 //
290 // FormItineraryStageString - Compose a string containing the stage
291 // data initialization for the specified itinerary.  N is the number
292 // of stages.
293 //
294 void SubtargetEmitter::FormItineraryStageString(const std::string &Name,
295                                                 Record *ItinData,
296                                                 std::string &ItinString,
297                                                 unsigned &NStages) {
298   // Get states list
299   RecVec StageList = ItinData->getValueAsListOfDefs("Stages");
300 
301   // For each stage
302   unsigned N = NStages = StageList.size();
303   for (unsigned i = 0; i < N;) {
304     // Next stage
305     const Record *Stage = StageList[i];
306 
307     // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind }
308     int Cycles = Stage->getValueAsInt("Cycles");
309     ItinString += "  { " + itostr(Cycles) + ", ";
310 
311     // Get unit list
312     RecVec UnitList = Stage->getValueAsListOfDefs("Units");
313 
314     // For each unit
315     for (unsigned j = 0, M = UnitList.size(); j < M;) {
316       // Add name and bitwise or
317       ItinString += Name + "FU::" + UnitList[j]->getName().str();
318       if (++j < M) ItinString += " | ";
319     }
320 
321     int TimeInc = Stage->getValueAsInt("TimeInc");
322     ItinString += ", " + itostr(TimeInc);
323 
324     int Kind = Stage->getValueAsInt("Kind");
325     ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind);
326 
327     // Close off stage
328     ItinString += " }";
329     if (++i < N) ItinString += ", ";
330   }
331 }
332 
333 //
334 // FormItineraryOperandCycleString - Compose a string containing the
335 // operand cycle initialization for the specified itinerary.  N is the
336 // number of operands that has cycles specified.
337 //
338 void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData,
339                          std::string &ItinString, unsigned &NOperandCycles) {
340   // Get operand cycle list
341   std::vector<int64_t> OperandCycleList =
342     ItinData->getValueAsListOfInts("OperandCycles");
343 
344   // For each operand cycle
345   unsigned N = NOperandCycles = OperandCycleList.size();
346   for (unsigned i = 0; i < N;) {
347     // Next operand cycle
348     const int OCycle = OperandCycleList[i];
349 
350     ItinString += "  " + itostr(OCycle);
351     if (++i < N) ItinString += ", ";
352   }
353 }
354 
355 void SubtargetEmitter::FormItineraryBypassString(const std::string &Name,
356                                                  Record *ItinData,
357                                                  std::string &ItinString,
358                                                  unsigned NOperandCycles) {
359   RecVec BypassList = ItinData->getValueAsListOfDefs("Bypasses");
360   unsigned N = BypassList.size();
361   unsigned i = 0;
362   for (; i < N;) {
363     ItinString += Name + "Bypass::" + BypassList[i]->getName().str();
364     if (++i < NOperandCycles) ItinString += ", ";
365   }
366   for (; i < NOperandCycles;) {
367     ItinString += " 0";
368     if (++i < NOperandCycles) ItinString += ", ";
369   }
370 }
371 
372 //
373 // EmitStageAndOperandCycleData - Generate unique itinerary stages and operand
374 // cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed
375 // by CodeGenSchedClass::Index.
376 //
377 void SubtargetEmitter::
378 EmitStageAndOperandCycleData(raw_ostream &OS,
379                              std::vector<std::vector<InstrItinerary>>
380                                &ProcItinLists) {
381   // Multiple processor models may share an itinerary record. Emit it once.
382   SmallPtrSet<Record*, 8> ItinsDefSet;
383 
384   // Emit functional units for all the itineraries.
385   for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
386 
387     if (!ItinsDefSet.insert(ProcModel.ItinsDef).second)
388       continue;
389 
390     RecVec FUs = ProcModel.ItinsDef->getValueAsListOfDefs("FU");
391     if (FUs.empty())
392       continue;
393 
394     StringRef Name = ProcModel.ItinsDef->getName();
395     OS << "\n// Functional units for \"" << Name << "\"\n"
396        << "namespace " << Name << "FU {\n";
397 
398     for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j)
399       OS << "  const InstrStage::FuncUnits " << FUs[j]->getName()
400          << " = 1ULL << " << j << ";\n";
401 
402     OS << "} // end namespace " << Name << "FU\n";
403 
404     RecVec BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP");
405     if (!BPs.empty()) {
406       OS << "\n// Pipeline forwarding paths for itineraries \"" << Name
407          << "\"\n" << "namespace " << Name << "Bypass {\n";
408 
409       OS << "  const unsigned NoBypass = 0;\n";
410       for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j)
411         OS << "  const unsigned " << BPs[j]->getName()
412            << " = 1 << " << j << ";\n";
413 
414       OS << "} // end namespace " << Name << "Bypass\n";
415     }
416   }
417 
418   // Begin stages table
419   std::string StageTable = "\nextern const llvm::InstrStage " + Target +
420                            "Stages[] = {\n";
421   StageTable += "  { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n";
422 
423   // Begin operand cycle table
424   std::string OperandCycleTable = "extern const unsigned " + Target +
425     "OperandCycles[] = {\n";
426   OperandCycleTable += "  0, // No itinerary\n";
427 
428   // Begin pipeline bypass table
429   std::string BypassTable = "extern const unsigned " + Target +
430     "ForwardingPaths[] = {\n";
431   BypassTable += " 0, // No itinerary\n";
432 
433   // For each Itinerary across all processors, add a unique entry to the stages,
434   // operand cycles, and pipeline bypass tables. Then add the new Itinerary
435   // object with computed offsets to the ProcItinLists result.
436   unsigned StageCount = 1, OperandCycleCount = 1;
437   std::map<std::string, unsigned> ItinStageMap, ItinOperandMap;
438   for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
439     // Add process itinerary to the list.
440     ProcItinLists.resize(ProcItinLists.size()+1);
441 
442     // If this processor defines no itineraries, then leave the itinerary list
443     // empty.
444     std::vector<InstrItinerary> &ItinList = ProcItinLists.back();
445     if (!ProcModel.hasItineraries())
446       continue;
447 
448     StringRef Name = ProcModel.ItinsDef->getName();
449 
450     ItinList.resize(SchedModels.numInstrSchedClasses());
451     assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins");
452 
453     for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size();
454          SchedClassIdx < SchedClassEnd; ++SchedClassIdx) {
455 
456       // Next itinerary data
457       Record *ItinData = ProcModel.ItinDefList[SchedClassIdx];
458 
459       // Get string and stage count
460       std::string ItinStageString;
461       unsigned NStages = 0;
462       if (ItinData)
463         FormItineraryStageString(std::string(Name), ItinData, ItinStageString,
464                                  NStages);
465 
466       // Get string and operand cycle count
467       std::string ItinOperandCycleString;
468       unsigned NOperandCycles = 0;
469       std::string ItinBypassString;
470       if (ItinData) {
471         FormItineraryOperandCycleString(ItinData, ItinOperandCycleString,
472                                         NOperandCycles);
473 
474         FormItineraryBypassString(std::string(Name), ItinData, ItinBypassString,
475                                   NOperandCycles);
476       }
477 
478       // Check to see if stage already exists and create if it doesn't
479       uint16_t FindStage = 0;
480       if (NStages > 0) {
481         FindStage = ItinStageMap[ItinStageString];
482         if (FindStage == 0) {
483           // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices
484           StageTable += ItinStageString + ", // " + itostr(StageCount);
485           if (NStages > 1)
486             StageTable += "-" + itostr(StageCount + NStages - 1);
487           StageTable += "\n";
488           // Record Itin class number.
489           ItinStageMap[ItinStageString] = FindStage = StageCount;
490           StageCount += NStages;
491         }
492       }
493 
494       // Check to see if operand cycle already exists and create if it doesn't
495       uint16_t FindOperandCycle = 0;
496       if (NOperandCycles > 0) {
497         std::string ItinOperandString = ItinOperandCycleString+ItinBypassString;
498         FindOperandCycle = ItinOperandMap[ItinOperandString];
499         if (FindOperandCycle == 0) {
500           // Emit as  cycle, // index
501           OperandCycleTable += ItinOperandCycleString + ", // ";
502           std::string OperandIdxComment = itostr(OperandCycleCount);
503           if (NOperandCycles > 1)
504             OperandIdxComment += "-"
505               + itostr(OperandCycleCount + NOperandCycles - 1);
506           OperandCycleTable += OperandIdxComment + "\n";
507           // Record Itin class number.
508           ItinOperandMap[ItinOperandCycleString] =
509             FindOperandCycle = OperandCycleCount;
510           // Emit as bypass, // index
511           BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n";
512           OperandCycleCount += NOperandCycles;
513         }
514       }
515 
516       // Set up itinerary as location and location + stage count
517       int16_t NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0;
518       InstrItinerary Intinerary = {
519           NumUOps,
520           FindStage,
521           uint16_t(FindStage + NStages),
522           FindOperandCycle,
523           uint16_t(FindOperandCycle + NOperandCycles),
524       };
525 
526       // Inject - empty slots will be 0, 0
527       ItinList[SchedClassIdx] = Intinerary;
528     }
529   }
530 
531   // Closing stage
532   StageTable += "  { 0, 0, 0, llvm::InstrStage::Required } // End stages\n";
533   StageTable += "};\n";
534 
535   // Closing operand cycles
536   OperandCycleTable += "  0 // End operand cycles\n";
537   OperandCycleTable += "};\n";
538 
539   BypassTable += " 0 // End bypass tables\n";
540   BypassTable += "};\n";
541 
542   // Emit tables.
543   OS << StageTable;
544   OS << OperandCycleTable;
545   OS << BypassTable;
546 }
547 
548 //
549 // EmitProcessorData - Generate data for processor itineraries that were
550 // computed during EmitStageAndOperandCycleData(). ProcItinLists lists all
551 // Itineraries for each processor. The Itinerary lists are indexed on
552 // CodeGenSchedClass::Index.
553 //
554 void SubtargetEmitter::
555 EmitItineraries(raw_ostream &OS,
556                 std::vector<std::vector<InstrItinerary>> &ProcItinLists) {
557   // Multiple processor models may share an itinerary record. Emit it once.
558   SmallPtrSet<Record*, 8> ItinsDefSet;
559 
560   // For each processor's machine model
561   std::vector<std::vector<InstrItinerary>>::iterator
562       ProcItinListsIter = ProcItinLists.begin();
563   for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
564          PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) {
565 
566     Record *ItinsDef = PI->ItinsDef;
567     if (!ItinsDefSet.insert(ItinsDef).second)
568       continue;
569 
570     // Get the itinerary list for the processor.
571     assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator");
572     std::vector<InstrItinerary> &ItinList = *ProcItinListsIter;
573 
574     // Empty itineraries aren't referenced anywhere in the tablegen output
575     // so don't emit them.
576     if (ItinList.empty())
577       continue;
578 
579     OS << "\n";
580     OS << "static const llvm::InstrItinerary ";
581 
582     // Begin processor itinerary table
583     OS << ItinsDef->getName() << "[] = {\n";
584 
585     // For each itinerary class in CodeGenSchedClass::Index order.
586     for (unsigned j = 0, M = ItinList.size(); j < M; ++j) {
587       InstrItinerary &Intinerary = ItinList[j];
588 
589       // Emit Itinerary in the form of
590       // { firstStage, lastStage, firstCycle, lastCycle } // index
591       OS << "  { " <<
592         Intinerary.NumMicroOps << ", " <<
593         Intinerary.FirstStage << ", " <<
594         Intinerary.LastStage << ", " <<
595         Intinerary.FirstOperandCycle << ", " <<
596         Intinerary.LastOperandCycle << " }" <<
597         ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n";
598     }
599     // End processor itinerary table
600     OS << "  { 0, uint16_t(~0U), uint16_t(~0U), uint16_t(~0U), uint16_t(~0U) }"
601           "// end marker\n";
602     OS << "};\n";
603   }
604 }
605 
606 // Emit either the value defined in the TableGen Record, or the default
607 // value defined in the C++ header. The Record is null if the processor does not
608 // define a model.
609 void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R,
610                                          StringRef Name, char Separator) {
611   OS << "  ";
612   int V = R ? R->getValueAsInt(Name) : -1;
613   if (V >= 0)
614     OS << V << Separator << " // " << Name;
615   else
616     OS << "MCSchedModel::Default" << Name << Separator;
617   OS << '\n';
618 }
619 
620 void SubtargetEmitter::EmitProcessorResourceSubUnits(
621     const CodeGenProcModel &ProcModel, raw_ostream &OS) {
622   OS << "\nstatic const unsigned " << ProcModel.ModelName
623      << "ProcResourceSubUnits[] = {\n"
624      << "  0,  // Invalid\n";
625 
626   for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
627     Record *PRDef = ProcModel.ProcResourceDefs[i];
628     if (!PRDef->isSubClassOf("ProcResGroup"))
629       continue;
630     RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
631     for (Record *RUDef : ResUnits) {
632       Record *const RU =
633           SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc());
634       for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) {
635         OS << "  " << ProcModel.getProcResourceIdx(RU) << ", ";
636       }
637     }
638     OS << "  // " << PRDef->getName() << "\n";
639   }
640   OS << "};\n";
641 }
642 
643 static void EmitRetireControlUnitInfo(const CodeGenProcModel &ProcModel,
644                                       raw_ostream &OS) {
645   int64_t ReorderBufferSize = 0, MaxRetirePerCycle = 0;
646   if (Record *RCU = ProcModel.RetireControlUnit) {
647     ReorderBufferSize =
648         std::max(ReorderBufferSize, RCU->getValueAsInt("ReorderBufferSize"));
649     MaxRetirePerCycle =
650         std::max(MaxRetirePerCycle, RCU->getValueAsInt("MaxRetirePerCycle"));
651   }
652 
653   OS << ReorderBufferSize << ", // ReorderBufferSize\n  ";
654   OS << MaxRetirePerCycle << ", // MaxRetirePerCycle\n  ";
655 }
656 
657 static void EmitRegisterFileInfo(const CodeGenProcModel &ProcModel,
658                                  unsigned NumRegisterFiles,
659                                  unsigned NumCostEntries, raw_ostream &OS) {
660   if (NumRegisterFiles)
661     OS << ProcModel.ModelName << "RegisterFiles,\n  " << (1 + NumRegisterFiles);
662   else
663     OS << "nullptr,\n  0";
664 
665   OS << ", // Number of register files.\n  ";
666   if (NumCostEntries)
667     OS << ProcModel.ModelName << "RegisterCosts,\n  ";
668   else
669     OS << "nullptr,\n  ";
670   OS << NumCostEntries << ", // Number of register cost entries.\n";
671 }
672 
673 unsigned
674 SubtargetEmitter::EmitRegisterFileTables(const CodeGenProcModel &ProcModel,
675                                          raw_ostream &OS) {
676   if (llvm::all_of(ProcModel.RegisterFiles, [](const CodeGenRegisterFile &RF) {
677         return RF.hasDefaultCosts();
678       }))
679     return 0;
680 
681   // Print the RegisterCost table first.
682   OS << "\n// {RegisterClassID, Register Cost, AllowMoveElimination }\n";
683   OS << "static const llvm::MCRegisterCostEntry " << ProcModel.ModelName
684      << "RegisterCosts"
685      << "[] = {\n";
686 
687   for (const CodeGenRegisterFile &RF : ProcModel.RegisterFiles) {
688     // Skip register files with a default cost table.
689     if (RF.hasDefaultCosts())
690       continue;
691     // Add entries to the cost table.
692     for (const CodeGenRegisterCost &RC : RF.Costs) {
693       OS << "  { ";
694       Record *Rec = RC.RCDef;
695       if (Rec->getValue("Namespace"))
696         OS << Rec->getValueAsString("Namespace") << "::";
697       OS << Rec->getName() << "RegClassID, " << RC.Cost << ", "
698          << RC.AllowMoveElimination << "},\n";
699     }
700   }
701   OS << "};\n";
702 
703   // Now generate a table with register file info.
704   OS << "\n // {Name, #PhysRegs, #CostEntries, IndexToCostTbl, "
705      << "MaxMovesEliminatedPerCycle, AllowZeroMoveEliminationOnly }\n";
706   OS << "static const llvm::MCRegisterFileDesc " << ProcModel.ModelName
707      << "RegisterFiles"
708      << "[] = {\n"
709      << "  { \"InvalidRegisterFile\", 0, 0, 0, 0, 0 },\n";
710   unsigned CostTblIndex = 0;
711 
712   for (const CodeGenRegisterFile &RD : ProcModel.RegisterFiles) {
713     OS << "  { ";
714     OS << '"' << RD.Name << '"' << ", " << RD.NumPhysRegs << ", ";
715     unsigned NumCostEntries = RD.Costs.size();
716     OS << NumCostEntries << ", " << CostTblIndex << ", "
717        << RD.MaxMovesEliminatedPerCycle << ", "
718        << RD.AllowZeroMoveEliminationOnly << "},\n";
719     CostTblIndex += NumCostEntries;
720   }
721   OS << "};\n";
722 
723   return CostTblIndex;
724 }
725 
726 void SubtargetEmitter::EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel,
727                                               raw_ostream &OS) {
728   unsigned QueueID = 0;
729   if (ProcModel.LoadQueue) {
730     const Record *Queue = ProcModel.LoadQueue->getValueAsDef("QueueDescriptor");
731     QueueID = 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
732                                 find(ProcModel.ProcResourceDefs, Queue));
733   }
734   OS << "  " << QueueID << ", // Resource Descriptor for the Load Queue\n";
735 
736   QueueID = 0;
737   if (ProcModel.StoreQueue) {
738     const Record *Queue =
739         ProcModel.StoreQueue->getValueAsDef("QueueDescriptor");
740     QueueID = 1 + std::distance(ProcModel.ProcResourceDefs.begin(),
741                                 find(ProcModel.ProcResourceDefs, Queue));
742   }
743   OS << "  " << QueueID << ", // Resource Descriptor for the Store Queue\n";
744 }
745 
746 void SubtargetEmitter::EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel,
747                                               raw_ostream &OS) {
748   // Generate a table of register file descriptors (one entry per each user
749   // defined register file), and a table of register costs.
750   unsigned NumCostEntries = EmitRegisterFileTables(ProcModel, OS);
751 
752   // Now generate a table for the extra processor info.
753   OS << "\nstatic const llvm::MCExtraProcessorInfo " << ProcModel.ModelName
754      << "ExtraInfo = {\n  ";
755 
756   // Add information related to the retire control unit.
757   EmitRetireControlUnitInfo(ProcModel, OS);
758 
759   // Add information related to the register files (i.e. where to find register
760   // file descriptors and register costs).
761   EmitRegisterFileInfo(ProcModel, ProcModel.RegisterFiles.size(),
762                        NumCostEntries, OS);
763 
764   // Add information about load/store queues.
765   EmitLoadStoreQueueInfo(ProcModel, OS);
766 
767   OS << "};\n";
768 }
769 
770 void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel,
771                                               raw_ostream &OS) {
772   EmitProcessorResourceSubUnits(ProcModel, OS);
773 
774   OS << "\n// {Name, NumUnits, SuperIdx, BufferSize, SubUnitsIdxBegin}\n";
775   OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName
776      << "ProcResources"
777      << "[] = {\n"
778      << "  {\"InvalidUnit\", 0, 0, 0, 0},\n";
779 
780   unsigned SubUnitsOffset = 1;
781   for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) {
782     Record *PRDef = ProcModel.ProcResourceDefs[i];
783 
784     Record *SuperDef = nullptr;
785     unsigned SuperIdx = 0;
786     unsigned NumUnits = 0;
787     const unsigned SubUnitsBeginOffset = SubUnitsOffset;
788     int BufferSize = PRDef->getValueAsInt("BufferSize");
789     if (PRDef->isSubClassOf("ProcResGroup")) {
790       RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources");
791       for (Record *RU : ResUnits) {
792         NumUnits += RU->getValueAsInt("NumUnits");
793         SubUnitsOffset += RU->getValueAsInt("NumUnits");
794       }
795     }
796     else {
797       // Find the SuperIdx
798       if (PRDef->getValueInit("Super")->isComplete()) {
799         SuperDef =
800             SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"),
801                                          ProcModel, PRDef->getLoc());
802         SuperIdx = ProcModel.getProcResourceIdx(SuperDef);
803       }
804       NumUnits = PRDef->getValueAsInt("NumUnits");
805     }
806     // Emit the ProcResourceDesc
807     OS << "  {\"" << PRDef->getName() << "\", ";
808     if (PRDef->getName().size() < 15)
809       OS.indent(15 - PRDef->getName().size());
810     OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", ";
811     if (SubUnitsBeginOffset != SubUnitsOffset) {
812       OS << ProcModel.ModelName << "ProcResourceSubUnits + "
813          << SubUnitsBeginOffset;
814     } else {
815       OS << "nullptr";
816     }
817     OS << "}, // #" << i+1;
818     if (SuperDef)
819       OS << ", Super=" << SuperDef->getName();
820     OS << "\n";
821   }
822   OS << "};\n";
823 }
824 
825 // Find the WriteRes Record that defines processor resources for this
826 // SchedWrite.
827 Record *SubtargetEmitter::FindWriteResources(
828   const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) {
829 
830   // Check if the SchedWrite is already subtarget-specific and directly
831   // specifies a set of processor resources.
832   if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes"))
833     return SchedWrite.TheDef;
834 
835   Record *AliasDef = nullptr;
836   for (Record *A : SchedWrite.Aliases) {
837     const CodeGenSchedRW &AliasRW =
838       SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
839     if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
840       Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
841       if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
842         continue;
843     }
844     if (AliasDef)
845       PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
846                     "defined for processor " + ProcModel.ModelName +
847                     " Ensure only one SchedAlias exists per RW.");
848     AliasDef = AliasRW.TheDef;
849   }
850   if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes"))
851     return AliasDef;
852 
853   // Check this processor's list of write resources.
854   Record *ResDef = nullptr;
855   for (Record *WR : ProcModel.WriteResDefs) {
856     if (!WR->isSubClassOf("WriteRes"))
857       continue;
858     if (AliasDef == WR->getValueAsDef("WriteType")
859         || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) {
860       if (ResDef) {
861         PrintFatalError(WR->getLoc(), "Resources are defined for both "
862                       "SchedWrite and its alias on processor " +
863                       ProcModel.ModelName);
864       }
865       ResDef = WR;
866     }
867   }
868   // TODO: If ProcModel has a base model (previous generation processor),
869   // then call FindWriteResources recursively with that model here.
870   if (!ResDef) {
871     PrintFatalError(ProcModel.ModelDef->getLoc(),
872                     Twine("Processor does not define resources for ") +
873                     SchedWrite.TheDef->getName());
874   }
875   return ResDef;
876 }
877 
878 /// Find the ReadAdvance record for the given SchedRead on this processor or
879 /// return NULL.
880 Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead,
881                                           const CodeGenProcModel &ProcModel) {
882   // Check for SchedReads that directly specify a ReadAdvance.
883   if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance"))
884     return SchedRead.TheDef;
885 
886   // Check this processor's list of aliases for SchedRead.
887   Record *AliasDef = nullptr;
888   for (Record *A : SchedRead.Aliases) {
889     const CodeGenSchedRW &AliasRW =
890       SchedModels.getSchedRW(A->getValueAsDef("AliasRW"));
891     if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) {
892       Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel");
893       if (&SchedModels.getProcModel(ModelDef) != &ProcModel)
894         continue;
895     }
896     if (AliasDef)
897       PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
898                     "defined for processor " + ProcModel.ModelName +
899                     " Ensure only one SchedAlias exists per RW.");
900     AliasDef = AliasRW.TheDef;
901   }
902   if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance"))
903     return AliasDef;
904 
905   // Check this processor's ReadAdvanceList.
906   Record *ResDef = nullptr;
907   for (Record *RA : ProcModel.ReadAdvanceDefs) {
908     if (!RA->isSubClassOf("ReadAdvance"))
909       continue;
910     if (AliasDef == RA->getValueAsDef("ReadType")
911         || SchedRead.TheDef == RA->getValueAsDef("ReadType")) {
912       if (ResDef) {
913         PrintFatalError(RA->getLoc(), "Resources are defined for both "
914                       "SchedRead and its alias on processor " +
915                       ProcModel.ModelName);
916       }
917       ResDef = RA;
918     }
919   }
920   // TODO: If ProcModel has a base model (previous generation processor),
921   // then call FindReadAdvance recursively with that model here.
922   if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") {
923     PrintFatalError(ProcModel.ModelDef->getLoc(),
924                     Twine("Processor does not define resources for ") +
925                     SchedRead.TheDef->getName());
926   }
927   return ResDef;
928 }
929 
930 // Expand an explicit list of processor resources into a full list of implied
931 // resource groups and super resources that cover them.
932 void SubtargetEmitter::ExpandProcResources(RecVec &PRVec,
933                                            std::vector<int64_t> &Cycles,
934                                            const CodeGenProcModel &PM) {
935   assert(PRVec.size() == Cycles.size() && "failed precondition");
936   for (unsigned i = 0, e = PRVec.size(); i != e; ++i) {
937     Record *PRDef = PRVec[i];
938     RecVec SubResources;
939     if (PRDef->isSubClassOf("ProcResGroup"))
940       SubResources = PRDef->getValueAsListOfDefs("Resources");
941     else {
942       SubResources.push_back(PRDef);
943       PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc());
944       for (Record *SubDef = PRDef;
945            SubDef->getValueInit("Super")->isComplete();) {
946         if (SubDef->isSubClassOf("ProcResGroup")) {
947           // Disallow this for simplicitly.
948           PrintFatalError(SubDef->getLoc(), "Processor resource group "
949                           " cannot be a super resources.");
950         }
951         Record *SuperDef =
952             SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM,
953                                          SubDef->getLoc());
954         PRVec.push_back(SuperDef);
955         Cycles.push_back(Cycles[i]);
956         SubDef = SuperDef;
957       }
958     }
959     for (Record *PR : PM.ProcResourceDefs) {
960       if (PR == PRDef || !PR->isSubClassOf("ProcResGroup"))
961         continue;
962       RecVec SuperResources = PR->getValueAsListOfDefs("Resources");
963       RecIter SubI = SubResources.begin(), SubE = SubResources.end();
964       for( ; SubI != SubE; ++SubI) {
965         if (!is_contained(SuperResources, *SubI)) {
966           break;
967         }
968       }
969       if (SubI == SubE) {
970         PRVec.push_back(PR);
971         Cycles.push_back(Cycles[i]);
972       }
973     }
974   }
975 }
976 
977 // Generate the SchedClass table for this processor and update global
978 // tables. Must be called for each processor in order.
979 void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel,
980                                            SchedClassTables &SchedTables) {
981   SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1);
982   if (!ProcModel.hasInstrSchedModel())
983     return;
984 
985   std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back();
986   LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n");
987   for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
988     LLVM_DEBUG(SC.dump(&SchedModels));
989 
990     SCTab.resize(SCTab.size() + 1);
991     MCSchedClassDesc &SCDesc = SCTab.back();
992     // SCDesc.Name is guarded by NDEBUG
993     SCDesc.NumMicroOps = 0;
994     SCDesc.BeginGroup = false;
995     SCDesc.EndGroup = false;
996     SCDesc.WriteProcResIdx = 0;
997     SCDesc.WriteLatencyIdx = 0;
998     SCDesc.ReadAdvanceIdx = 0;
999 
1000     // A Variant SchedClass has no resources of its own.
1001     bool HasVariants = false;
1002     for (const CodeGenSchedTransition &CGT :
1003            make_range(SC.Transitions.begin(), SC.Transitions.end())) {
1004       if (CGT.ProcIndex == ProcModel.Index) {
1005         HasVariants = true;
1006         break;
1007       }
1008     }
1009     if (HasVariants) {
1010       SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps;
1011       continue;
1012     }
1013 
1014     // Determine if the SchedClass is actually reachable on this processor. If
1015     // not don't try to locate the processor resources, it will fail.
1016     // If ProcIndices contains 0, this class applies to all processors.
1017     assert(!SC.ProcIndices.empty() && "expect at least one procidx");
1018     if (SC.ProcIndices[0] != 0) {
1019       if (!is_contained(SC.ProcIndices, ProcModel.Index))
1020         continue;
1021     }
1022     IdxVec Writes = SC.Writes;
1023     IdxVec Reads = SC.Reads;
1024     if (!SC.InstRWs.empty()) {
1025       // This class has a default ReadWrite list which can be overridden by
1026       // InstRW definitions.
1027       Record *RWDef = nullptr;
1028       for (Record *RW : SC.InstRWs) {
1029         Record *RWModelDef = RW->getValueAsDef("SchedModel");
1030         if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) {
1031           RWDef = RW;
1032           break;
1033         }
1034       }
1035       if (RWDef) {
1036         Writes.clear();
1037         Reads.clear();
1038         SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
1039                             Writes, Reads);
1040       }
1041     }
1042     if (Writes.empty()) {
1043       // Check this processor's itinerary class resources.
1044       for (Record *I : ProcModel.ItinRWDefs) {
1045         RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses");
1046         if (is_contained(Matched, SC.ItinClassDef)) {
1047           SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"),
1048                               Writes, Reads);
1049           break;
1050         }
1051       }
1052       if (Writes.empty()) {
1053         LLVM_DEBUG(dbgs() << ProcModel.ModelName
1054                           << " does not have resources for class " << SC.Name
1055                           << '\n');
1056         SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1057       }
1058     }
1059     // Sum resources across all operand writes.
1060     std::vector<MCWriteProcResEntry> WriteProcResources;
1061     std::vector<MCWriteLatencyEntry> WriteLatencies;
1062     std::vector<std::string> WriterNames;
1063     std::vector<MCReadAdvanceEntry> ReadAdvanceEntries;
1064     for (unsigned W : Writes) {
1065       IdxVec WriteSeq;
1066       SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false,
1067                                      ProcModel);
1068 
1069       // For each operand, create a latency entry.
1070       MCWriteLatencyEntry WLEntry;
1071       WLEntry.Cycles = 0;
1072       unsigned WriteID = WriteSeq.back();
1073       WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name);
1074       // If this Write is not referenced by a ReadAdvance, don't distinguish it
1075       // from other WriteLatency entries.
1076       if (!SchedModels.hasReadOfWrite(
1077             SchedModels.getSchedWrite(WriteID).TheDef)) {
1078         WriteID = 0;
1079       }
1080       WLEntry.WriteResourceID = WriteID;
1081 
1082       for (unsigned WS : WriteSeq) {
1083 
1084         Record *WriteRes =
1085           FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel);
1086 
1087         // Mark the parent class as invalid for unsupported write types.
1088         if (WriteRes->getValueAsBit("Unsupported")) {
1089           SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1090           break;
1091         }
1092         WLEntry.Cycles += WriteRes->getValueAsInt("Latency");
1093         SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps");
1094         SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup");
1095         SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup");
1096         SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue");
1097         SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue");
1098 
1099         // Create an entry for each ProcResource listed in WriteRes.
1100         RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources");
1101         std::vector<int64_t> Cycles =
1102           WriteRes->getValueAsListOfInts("ResourceCycles");
1103 
1104         if (Cycles.empty()) {
1105           // If ResourceCycles is not provided, default to one cycle per
1106           // resource.
1107           Cycles.resize(PRVec.size(), 1);
1108         } else if (Cycles.size() != PRVec.size()) {
1109           // If ResourceCycles is provided, check consistency.
1110           PrintFatalError(
1111               WriteRes->getLoc(),
1112               Twine("Inconsistent resource cycles: !size(ResourceCycles) != "
1113                     "!size(ProcResources): ")
1114                   .concat(Twine(PRVec.size()))
1115                   .concat(" vs ")
1116                   .concat(Twine(Cycles.size())));
1117         }
1118 
1119         ExpandProcResources(PRVec, Cycles, ProcModel);
1120 
1121         for (unsigned PRIdx = 0, PREnd = PRVec.size();
1122              PRIdx != PREnd; ++PRIdx) {
1123           MCWriteProcResEntry WPREntry;
1124           WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]);
1125           assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx");
1126           WPREntry.Cycles = Cycles[PRIdx];
1127           // If this resource is already used in this sequence, add the current
1128           // entry's cycles so that the same resource appears to be used
1129           // serially, rather than multiple parallel uses. This is important for
1130           // in-order machine where the resource consumption is a hazard.
1131           unsigned WPRIdx = 0, WPREnd = WriteProcResources.size();
1132           for( ; WPRIdx != WPREnd; ++WPRIdx) {
1133             if (WriteProcResources[WPRIdx].ProcResourceIdx
1134                 == WPREntry.ProcResourceIdx) {
1135               WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles;
1136               break;
1137             }
1138           }
1139           if (WPRIdx == WPREnd)
1140             WriteProcResources.push_back(WPREntry);
1141         }
1142       }
1143       WriteLatencies.push_back(WLEntry);
1144     }
1145     // Create an entry for each operand Read in this SchedClass.
1146     // Entries must be sorted first by UseIdx then by WriteResourceID.
1147     for (unsigned UseIdx = 0, EndIdx = Reads.size();
1148          UseIdx != EndIdx; ++UseIdx) {
1149       Record *ReadAdvance =
1150         FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel);
1151       if (!ReadAdvance)
1152         continue;
1153 
1154       // Mark the parent class as invalid for unsupported write types.
1155       if (ReadAdvance->getValueAsBit("Unsupported")) {
1156         SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps;
1157         break;
1158       }
1159       RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites");
1160       IdxVec WriteIDs;
1161       if (ValidWrites.empty())
1162         WriteIDs.push_back(0);
1163       else {
1164         for (Record *VW : ValidWrites) {
1165           WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false));
1166         }
1167       }
1168       llvm::sort(WriteIDs);
1169       for(unsigned W : WriteIDs) {
1170         MCReadAdvanceEntry RAEntry;
1171         RAEntry.UseIdx = UseIdx;
1172         RAEntry.WriteResourceID = W;
1173         RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles");
1174         ReadAdvanceEntries.push_back(RAEntry);
1175       }
1176     }
1177     if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) {
1178       WriteProcResources.clear();
1179       WriteLatencies.clear();
1180       ReadAdvanceEntries.clear();
1181     }
1182     // Add the information for this SchedClass to the global tables using basic
1183     // compression.
1184     //
1185     // WritePrecRes entries are sorted by ProcResIdx.
1186     llvm::sort(WriteProcResources, LessWriteProcResources());
1187 
1188     SCDesc.NumWriteProcResEntries = WriteProcResources.size();
1189     std::vector<MCWriteProcResEntry>::iterator WPRPos =
1190       std::search(SchedTables.WriteProcResources.begin(),
1191                   SchedTables.WriteProcResources.end(),
1192                   WriteProcResources.begin(), WriteProcResources.end());
1193     if (WPRPos != SchedTables.WriteProcResources.end())
1194       SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin();
1195     else {
1196       SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size();
1197       SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(),
1198                                             WriteProcResources.end());
1199     }
1200     // Latency entries must remain in operand order.
1201     SCDesc.NumWriteLatencyEntries = WriteLatencies.size();
1202     std::vector<MCWriteLatencyEntry>::iterator WLPos =
1203       std::search(SchedTables.WriteLatencies.begin(),
1204                   SchedTables.WriteLatencies.end(),
1205                   WriteLatencies.begin(), WriteLatencies.end());
1206     if (WLPos != SchedTables.WriteLatencies.end()) {
1207       unsigned idx = WLPos - SchedTables.WriteLatencies.begin();
1208       SCDesc.WriteLatencyIdx = idx;
1209       for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i)
1210         if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) ==
1211             std::string::npos) {
1212           SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i];
1213         }
1214     }
1215     else {
1216       SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size();
1217       llvm::append_range(SchedTables.WriteLatencies, WriteLatencies);
1218       llvm::append_range(SchedTables.WriterNames, WriterNames);
1219     }
1220     // ReadAdvanceEntries must remain in operand order.
1221     SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size();
1222     std::vector<MCReadAdvanceEntry>::iterator RAPos =
1223       std::search(SchedTables.ReadAdvanceEntries.begin(),
1224                   SchedTables.ReadAdvanceEntries.end(),
1225                   ReadAdvanceEntries.begin(), ReadAdvanceEntries.end());
1226     if (RAPos != SchedTables.ReadAdvanceEntries.end())
1227       SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin();
1228     else {
1229       SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size();
1230       llvm::append_range(SchedTables.ReadAdvanceEntries, ReadAdvanceEntries);
1231     }
1232   }
1233 }
1234 
1235 // Emit SchedClass tables for all processors and associated global tables.
1236 void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables,
1237                                             raw_ostream &OS) {
1238   // Emit global WriteProcResTable.
1239   OS << "\n// {ProcResourceIdx, Cycles}\n"
1240      << "extern const llvm::MCWriteProcResEntry "
1241      << Target << "WriteProcResTable[] = {\n"
1242      << "  { 0,  0}, // Invalid\n";
1243   for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size();
1244        WPRIdx != WPREnd; ++WPRIdx) {
1245     MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx];
1246     OS << "  {" << format("%2d", WPREntry.ProcResourceIdx) << ", "
1247        << format("%2d", WPREntry.Cycles) << "}";
1248     if (WPRIdx + 1 < WPREnd)
1249       OS << ',';
1250     OS << " // #" << WPRIdx << '\n';
1251   }
1252   OS << "}; // " << Target << "WriteProcResTable\n";
1253 
1254   // Emit global WriteLatencyTable.
1255   OS << "\n// {Cycles, WriteResourceID}\n"
1256      << "extern const llvm::MCWriteLatencyEntry "
1257      << Target << "WriteLatencyTable[] = {\n"
1258      << "  { 0,  0}, // Invalid\n";
1259   for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size();
1260        WLIdx != WLEnd; ++WLIdx) {
1261     MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx];
1262     OS << "  {" << format("%2d", WLEntry.Cycles) << ", "
1263        << format("%2d", WLEntry.WriteResourceID) << "}";
1264     if (WLIdx + 1 < WLEnd)
1265       OS << ',';
1266     OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n';
1267   }
1268   OS << "}; // " << Target << "WriteLatencyTable\n";
1269 
1270   // Emit global ReadAdvanceTable.
1271   OS << "\n// {UseIdx, WriteResourceID, Cycles}\n"
1272      << "extern const llvm::MCReadAdvanceEntry "
1273      << Target << "ReadAdvanceTable[] = {\n"
1274      << "  {0,  0,  0}, // Invalid\n";
1275   for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size();
1276        RAIdx != RAEnd; ++RAIdx) {
1277     MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx];
1278     OS << "  {" << RAEntry.UseIdx << ", "
1279        << format("%2d", RAEntry.WriteResourceID) << ", "
1280        << format("%2d", RAEntry.Cycles) << "}";
1281     if (RAIdx + 1 < RAEnd)
1282       OS << ',';
1283     OS << " // #" << RAIdx << '\n';
1284   }
1285   OS << "}; // " << Target << "ReadAdvanceTable\n";
1286 
1287   // Emit a SchedClass table for each processor.
1288   for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(),
1289          PE = SchedModels.procModelEnd(); PI != PE; ++PI) {
1290     if (!PI->hasInstrSchedModel())
1291       continue;
1292 
1293     std::vector<MCSchedClassDesc> &SCTab =
1294       SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())];
1295 
1296     OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup,"
1297        << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n";
1298     OS << "static const llvm::MCSchedClassDesc "
1299        << PI->ModelName << "SchedClasses[] = {\n";
1300 
1301     // The first class is always invalid. We no way to distinguish it except by
1302     // name and position.
1303     assert(SchedModels.getSchedClass(0).Name == "NoInstrModel"
1304            && "invalid class not first");
1305     OS << "  {DBGFIELD(\"InvalidSchedClass\")  "
1306        << MCSchedClassDesc::InvalidNumMicroOps
1307        << ", false, false,  0, 0,  0, 0,  0, 0},\n";
1308 
1309     for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) {
1310       MCSchedClassDesc &MCDesc = SCTab[SCIdx];
1311       const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx);
1312       OS << "  {DBGFIELD(\"" << SchedClass.Name << "\") ";
1313       if (SchedClass.Name.size() < 18)
1314         OS.indent(18 - SchedClass.Name.size());
1315       OS << MCDesc.NumMicroOps
1316          << ", " << ( MCDesc.BeginGroup ? "true" : "false" )
1317          << ", " << ( MCDesc.EndGroup ? "true" : "false" )
1318          << ", " << format("%2d", MCDesc.WriteProcResIdx)
1319          << ", " << MCDesc.NumWriteProcResEntries
1320          << ", " << format("%2d", MCDesc.WriteLatencyIdx)
1321          << ", " << MCDesc.NumWriteLatencyEntries
1322          << ", " << format("%2d", MCDesc.ReadAdvanceIdx)
1323          << ", " << MCDesc.NumReadAdvanceEntries
1324          << "}, // #" << SCIdx << '\n';
1325     }
1326     OS << "}; // " << PI->ModelName << "SchedClasses\n";
1327   }
1328 }
1329 
1330 void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) {
1331   // For each processor model.
1332   for (const CodeGenProcModel &PM : SchedModels.procModels()) {
1333     // Emit extra processor info if available.
1334     if (PM.hasExtraProcessorInfo())
1335       EmitExtraProcessorInfo(PM, OS);
1336     // Emit processor resource table.
1337     if (PM.hasInstrSchedModel())
1338       EmitProcessorResources(PM, OS);
1339     else if(!PM.ProcResourceDefs.empty())
1340       PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines "
1341                     "ProcResources without defining WriteRes SchedWriteRes");
1342 
1343     // Begin processor itinerary properties
1344     OS << "\n";
1345     OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n";
1346     EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ',');
1347     EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ',');
1348     EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ',');
1349     EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ',');
1350     EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ',');
1351     EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ',');
1352 
1353     bool PostRAScheduler =
1354       (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false);
1355 
1356     OS << "  " << (PostRAScheduler ? "true" : "false")  << ", // "
1357        << "PostRAScheduler\n";
1358 
1359     bool CompleteModel =
1360       (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false);
1361 
1362     OS << "  " << (CompleteModel ? "true" : "false") << ", // "
1363        << "CompleteModel\n";
1364 
1365     OS << "  " << PM.Index << ", // Processor ID\n";
1366     if (PM.hasInstrSchedModel())
1367       OS << "  " << PM.ModelName << "ProcResources" << ",\n"
1368          << "  " << PM.ModelName << "SchedClasses" << ",\n"
1369          << "  " << PM.ProcResourceDefs.size()+1 << ",\n"
1370          << "  " << (SchedModels.schedClassEnd()
1371                      - SchedModels.schedClassBegin()) << ",\n";
1372     else
1373       OS << "  nullptr, nullptr, 0, 0,"
1374          << " // No instruction-level machine model.\n";
1375     if (PM.hasItineraries())
1376       OS << "  " << PM.ItinsDef->getName() << ",\n";
1377     else
1378       OS << "  nullptr, // No Itinerary\n";
1379     if (PM.hasExtraProcessorInfo())
1380       OS << "  &" << PM.ModelName << "ExtraInfo,\n";
1381     else
1382       OS << "  nullptr // No extra processor descriptor\n";
1383     OS << "};\n";
1384   }
1385 }
1386 
1387 //
1388 // EmitSchedModel - Emits all scheduling model tables, folding common patterns.
1389 //
1390 void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
1391   OS << "#ifdef DBGFIELD\n"
1392      << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n"
1393      << "#endif\n"
1394      << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n"
1395      << "#define DBGFIELD(x) x,\n"
1396      << "#else\n"
1397      << "#define DBGFIELD(x)\n"
1398      << "#endif\n";
1399 
1400   if (SchedModels.hasItineraries()) {
1401     std::vector<std::vector<InstrItinerary>> ProcItinLists;
1402     // Emit the stage data
1403     EmitStageAndOperandCycleData(OS, ProcItinLists);
1404     EmitItineraries(OS, ProcItinLists);
1405   }
1406   OS << "\n// ===============================================================\n"
1407      << "// Data tables for the new per-operand machine model.\n";
1408 
1409   SchedClassTables SchedTables;
1410   for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) {
1411     GenSchedClassTables(ProcModel, SchedTables);
1412   }
1413   EmitSchedClassTables(SchedTables, OS);
1414 
1415   OS << "\n#undef DBGFIELD\n";
1416 
1417   // Emit the processor machine model
1418   EmitProcessorModels(OS);
1419 }
1420 
1421 static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) {
1422   std::string Buffer;
1423   raw_string_ostream Stream(Buffer);
1424 
1425   // Collect all the PredicateProlog records and print them to the output
1426   // stream.
1427   std::vector<Record *> Prologs =
1428       Records.getAllDerivedDefinitions("PredicateProlog");
1429   llvm::sort(Prologs, LessRecord());
1430   for (Record *P : Prologs)
1431     Stream << P->getValueAsString("Code") << '\n';
1432 
1433   Stream.flush();
1434   OS << Buffer;
1435 }
1436 
1437 static bool isTruePredicate(const Record *Rec) {
1438   return Rec->isSubClassOf("MCSchedPredicate") &&
1439          Rec->getValueAsDef("Pred")->isSubClassOf("MCTrue");
1440 }
1441 
1442 static void emitPredicates(const CodeGenSchedTransition &T,
1443                            const CodeGenSchedClass &SC, PredicateExpander &PE,
1444                            raw_ostream &OS) {
1445   std::string Buffer;
1446   raw_string_ostream SS(Buffer);
1447 
1448   // If not all predicates are MCTrue, then we need an if-stmt.
1449   unsigned NumNonTruePreds =
1450       T.PredTerm.size() - count_if(T.PredTerm, isTruePredicate);
1451 
1452   SS.indent(PE.getIndentLevel() * 2);
1453 
1454   if (NumNonTruePreds) {
1455     bool FirstNonTruePredicate = true;
1456     SS << "if (";
1457 
1458     PE.setIndentLevel(PE.getIndentLevel() + 2);
1459 
1460     for (const Record *Rec : T.PredTerm) {
1461       // Skip predicates that evaluate to "true".
1462       if (isTruePredicate(Rec))
1463         continue;
1464 
1465       if (FirstNonTruePredicate) {
1466         FirstNonTruePredicate = false;
1467       } else {
1468         SS << "\n";
1469         SS.indent(PE.getIndentLevel() * 2);
1470         SS << "&& ";
1471       }
1472 
1473       if (Rec->isSubClassOf("MCSchedPredicate")) {
1474         PE.expandPredicate(SS, Rec->getValueAsDef("Pred"));
1475         continue;
1476       }
1477 
1478       // Expand this legacy predicate and wrap it around braces if there is more
1479       // than one predicate to expand.
1480       SS << ((NumNonTruePreds > 1) ? "(" : "")
1481          << Rec->getValueAsString("Predicate")
1482          << ((NumNonTruePreds > 1) ? ")" : "");
1483     }
1484 
1485     SS << ")\n"; // end of if-stmt
1486     PE.decreaseIndentLevel();
1487     SS.indent(PE.getIndentLevel() * 2);
1488     PE.decreaseIndentLevel();
1489   }
1490 
1491   SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
1492   SS.flush();
1493   OS << Buffer;
1494 }
1495 
1496 // Used by method `SubtargetEmitter::emitSchedModelHelpersImpl()` to generate
1497 // epilogue code for the auto-generated helper.
1498 static void emitSchedModelHelperEpilogue(raw_ostream &OS,
1499                                          bool ShouldReturnZero) {
1500   if (ShouldReturnZero) {
1501     OS << "  // Don't know how to resolve this scheduling class.\n"
1502        << "  return 0;\n";
1503     return;
1504   }
1505 
1506   OS << "  report_fatal_error(\"Expected a variant SchedClass\");\n";
1507 }
1508 
1509 static bool hasMCSchedPredicates(const CodeGenSchedTransition &T) {
1510   return all_of(T.PredTerm, [](const Record *Rec) {
1511     return Rec->isSubClassOf("MCSchedPredicate");
1512   });
1513 }
1514 
1515 static void collectVariantClasses(const CodeGenSchedModels &SchedModels,
1516                                   IdxVec &VariantClasses,
1517                                   bool OnlyExpandMCInstPredicates) {
1518   for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) {
1519     // Ignore non-variant scheduling classes.
1520     if (SC.Transitions.empty())
1521       continue;
1522 
1523     if (OnlyExpandMCInstPredicates) {
1524       // Ignore this variant scheduling class no transitions use any meaningful
1525       // MCSchedPredicate definitions.
1526       if (!any_of(SC.Transitions, [](const CodeGenSchedTransition &T) {
1527             return hasMCSchedPredicates(T);
1528           }))
1529         continue;
1530     }
1531 
1532     VariantClasses.push_back(SC.Index);
1533   }
1534 }
1535 
1536 static void collectProcessorIndices(const CodeGenSchedClass &SC,
1537                                     IdxVec &ProcIndices) {
1538   // A variant scheduling class may define transitions for multiple
1539   // processors.  This function identifies wich processors are associated with
1540   // transition rules specified by variant class `SC`.
1541   for (const CodeGenSchedTransition &T : SC.Transitions) {
1542     IdxVec PI;
1543     std::set_union(&T.ProcIndex, &T.ProcIndex + 1, ProcIndices.begin(),
1544                    ProcIndices.end(), std::back_inserter(PI));
1545     ProcIndices.swap(PI);
1546   }
1547 }
1548 
1549 static bool isAlwaysTrue(const CodeGenSchedTransition &T) {
1550   return llvm::all_of(T.PredTerm,
1551                       [](const Record *R) { return isTruePredicate(R); });
1552 }
1553 
1554 void SubtargetEmitter::emitSchedModelHelpersImpl(
1555     raw_ostream &OS, bool OnlyExpandMCInstPredicates) {
1556   IdxVec VariantClasses;
1557   collectVariantClasses(SchedModels, VariantClasses,
1558                         OnlyExpandMCInstPredicates);
1559 
1560   if (VariantClasses.empty()) {
1561     emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1562     return;
1563   }
1564 
1565   // Construct a switch statement where the condition is a check on the
1566   // scheduling class identifier. There is a `case` for every variant class
1567   // defined by the processor models of this target.
1568   // Each `case` implements a number of rules to resolve (i.e. to transition from)
1569   // a variant scheduling class to another scheduling class.  Rules are
1570   // described by instances of CodeGenSchedTransition. Note that transitions may
1571   // not be valid for all processors.
1572   OS << "  switch (SchedClass) {\n";
1573   for (unsigned VC : VariantClasses) {
1574     IdxVec ProcIndices;
1575     const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC);
1576     collectProcessorIndices(SC, ProcIndices);
1577 
1578     OS << "  case " << VC << ": // " << SC.Name << '\n';
1579 
1580     PredicateExpander PE(Target);
1581     PE.setByRef(false);
1582     PE.setExpandForMC(OnlyExpandMCInstPredicates);
1583     for (unsigned PI : ProcIndices) {
1584       OS << "    ";
1585 
1586       // Emit a guard on the processor ID.
1587       if (PI != 0) {
1588         OS << (OnlyExpandMCInstPredicates
1589                    ? "if (CPUID == "
1590                    : "if (SchedModel->getProcessorID() == ");
1591         OS << PI << ") ";
1592         OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName << '\n';
1593       }
1594 
1595       // Now emit transitions associated with processor PI.
1596       const CodeGenSchedTransition *FinalT = nullptr;
1597       for (const CodeGenSchedTransition &T : SC.Transitions) {
1598         if (PI != 0 && T.ProcIndex != PI)
1599           continue;
1600 
1601         // Emit only transitions based on MCSchedPredicate, if it's the case.
1602         // At least the transition specified by NoSchedPred is emitted,
1603         // which becomes the default transition for those variants otherwise
1604         // not based on MCSchedPredicate.
1605         // FIXME: preferably, llvm-mca should instead assume a reasonable
1606         // default when a variant transition is not based on MCSchedPredicate
1607         // for a given processor.
1608         if (OnlyExpandMCInstPredicates && !hasMCSchedPredicates(T))
1609           continue;
1610 
1611         // If transition is folded to 'return X' it should be the last one.
1612         if (isAlwaysTrue(T)) {
1613           FinalT = &T;
1614           continue;
1615         }
1616         PE.setIndentLevel(3);
1617         emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS);
1618       }
1619       if (FinalT)
1620         emitPredicates(*FinalT, SchedModels.getSchedClass(FinalT->ToClassIdx),
1621                        PE, OS);
1622 
1623       OS << "    }\n";
1624 
1625       if (PI == 0)
1626         break;
1627     }
1628 
1629     if (SC.isInferred())
1630       OS << "    return " << SC.Index << ";\n";
1631     OS << "    break;\n";
1632   }
1633 
1634   OS << "  };\n";
1635 
1636   emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates);
1637 }
1638 
1639 void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
1640                                              raw_ostream &OS) {
1641   OS << "unsigned " << ClassName
1642      << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI,"
1643      << " const TargetSchedModel *SchedModel) const {\n";
1644 
1645   // Emit the predicate prolog code.
1646   emitPredicateProlog(Records, OS);
1647 
1648   // Emit target predicates.
1649   emitSchedModelHelpersImpl(OS);
1650 
1651   OS << "} // " << ClassName << "::resolveSchedClass\n\n";
1652 
1653   OS << "unsigned " << ClassName
1654      << "\n::resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI,"
1655      << " const MCInstrInfo *MCII, unsigned CPUID) const {\n"
1656      << "  return " << Target << "_MC"
1657      << "::resolveVariantSchedClassImpl(SchedClass, MI, MCII, CPUID);\n"
1658      << "} // " << ClassName << "::resolveVariantSchedClass\n\n";
1659 
1660   STIPredicateExpander PE(Target);
1661   PE.setClassPrefix(ClassName);
1662   PE.setExpandDefinition(true);
1663   PE.setByRef(false);
1664   PE.setIndentLevel(0);
1665 
1666   for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1667     PE.expandSTIPredicate(OS, Fn);
1668 }
1669 
1670 void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName,
1671                                        raw_ostream &OS) {
1672   const CodeGenHwModes &CGH = TGT.getHwModes();
1673   assert(CGH.getNumModeIds() > 0);
1674   if (CGH.getNumModeIds() == 1)
1675     return;
1676 
1677   OS << "unsigned " << ClassName << "::getHwMode() const {\n";
1678   for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) {
1679     const HwMode &HM = CGH.getMode(M);
1680     OS << "  if (checkFeatures(\"" << HM.Features
1681        << "\")) return " << M << ";\n";
1682   }
1683   OS << "  return 0;\n}\n";
1684 }
1685 
1686 //
1687 // ParseFeaturesFunction - Produces a subtarget specific function for parsing
1688 // the subtarget features string.
1689 //
1690 void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS,
1691                                              unsigned NumFeatures,
1692                                              unsigned NumProcs) {
1693   std::vector<Record*> Features =
1694                        Records.getAllDerivedDefinitions("SubtargetFeature");
1695   llvm::sort(Features, LessRecord());
1696 
1697   OS << "// ParseSubtargetFeatures - Parses features string setting specified\n"
1698      << "// subtarget options.\n"
1699      << "void llvm::";
1700   OS << Target;
1701   OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, "
1702      << "StringRef FS) {\n"
1703      << "  LLVM_DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n"
1704      << "  LLVM_DEBUG(dbgs() << \"\\nCPU:\" << CPU);\n"
1705      << "  LLVM_DEBUG(dbgs() << \"\\nTuneCPU:\" << TuneCPU << \"\\n\\n\");\n";
1706 
1707   if (Features.empty()) {
1708     OS << "}\n";
1709     return;
1710   }
1711 
1712   OS << "  InitMCProcessorInfo(CPU, TuneCPU, FS);\n"
1713      << "  const FeatureBitset &Bits = getFeatureBits();\n";
1714 
1715   for (Record *R : Features) {
1716     // Next record
1717     StringRef Instance = R->getName();
1718     StringRef Value = R->getValueAsString("Value");
1719     StringRef Attribute = R->getValueAsString("Attribute");
1720 
1721     if (Value=="true" || Value=="false")
1722       OS << "  if (Bits[" << Target << "::"
1723          << Instance << "]) "
1724          << Attribute << " = " << Value << ";\n";
1725     else
1726       OS << "  if (Bits[" << Target << "::"
1727          << Instance << "] && "
1728          << Attribute << " < " << Value << ") "
1729          << Attribute << " = " << Value << ";\n";
1730   }
1731 
1732   OS << "}\n";
1733 }
1734 
1735 void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
1736   OS << "namespace " << Target << "_MC {\n"
1737      << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n"
1738      << "    const MCInst *MI, const MCInstrInfo *MCII, unsigned CPUID) {\n";
1739   emitSchedModelHelpersImpl(OS, /* OnlyExpandMCPredicates */ true);
1740   OS << "}\n";
1741   OS << "} // end namespace " << Target << "_MC\n\n";
1742 
1743   OS << "struct " << Target
1744      << "GenMCSubtargetInfo : public MCSubtargetInfo {\n";
1745   OS << "  " << Target << "GenMCSubtargetInfo(const Triple &TT,\n"
1746      << "    StringRef CPU, StringRef TuneCPU, StringRef FS,\n"
1747      << "    ArrayRef<SubtargetFeatureKV> PF,\n"
1748      << "    ArrayRef<SubtargetSubTypeKV> PD,\n"
1749      << "    const MCWriteProcResEntry *WPR,\n"
1750      << "    const MCWriteLatencyEntry *WL,\n"
1751      << "    const MCReadAdvanceEntry *RA, const InstrStage *IS,\n"
1752      << "    const unsigned *OC, const unsigned *FP) :\n"
1753      << "      MCSubtargetInfo(TT, CPU, TuneCPU, FS, PF, PD,\n"
1754      << "                      WPR, WL, RA, IS, OC, FP) { }\n\n"
1755      << "  unsigned resolveVariantSchedClass(unsigned SchedClass,\n"
1756      << "      const MCInst *MI, const MCInstrInfo *MCII,\n"
1757      << "      unsigned CPUID) const override {\n"
1758      << "    return " << Target << "_MC"
1759      << "::resolveVariantSchedClassImpl(SchedClass, MI, MCII, CPUID);\n";
1760   OS << "  }\n";
1761   if (TGT.getHwModes().getNumModeIds() > 1)
1762     OS << "  unsigned getHwMode() const override;\n";
1763   OS << "};\n";
1764   EmitHwModeCheck(Target + "GenMCSubtargetInfo", OS);
1765 }
1766 
1767 void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
1768   OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n";
1769   OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1770 
1771   STIPredicateExpander PE(Target);
1772   PE.setExpandForMC(true);
1773   PE.setByRef(true);
1774   for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1775     PE.expandSTIPredicate(OS, Fn);
1776 
1777   OS << "#endif // GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1778 
1779   OS << "\n#ifdef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n";
1780   OS << "#undef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1781 
1782   std::string ClassPrefix = Target + "MCInstrAnalysis";
1783   PE.setExpandDefinition(true);
1784   PE.setClassPrefix(ClassPrefix);
1785   PE.setIndentLevel(0);
1786   for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1787     PE.expandSTIPredicate(OS, Fn);
1788 
1789   OS << "#endif // GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1790 }
1791 
1792 //
1793 // SubtargetEmitter::run - Main subtarget enumeration emitter.
1794 //
1795 void SubtargetEmitter::run(raw_ostream &OS) {
1796   emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
1797 
1798   OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
1799   OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
1800 
1801   DenseMap<Record *, unsigned> FeatureMap;
1802 
1803   OS << "namespace llvm {\n";
1804   Enumeration(OS, FeatureMap);
1805   OS << "} // end namespace llvm\n\n";
1806   OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
1807 
1808   OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
1809   OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
1810 
1811   OS << "namespace llvm {\n";
1812 #if 0
1813   OS << "namespace {\n";
1814 #endif
1815   unsigned NumFeatures = FeatureKeyValues(OS, FeatureMap);
1816   OS << "\n";
1817   EmitSchedModel(OS);
1818   OS << "\n";
1819   unsigned NumProcs = CPUKeyValues(OS, FeatureMap);
1820   OS << "\n";
1821 #if 0
1822   OS << "} // end anonymous namespace\n\n";
1823 #endif
1824 
1825   // MCInstrInfo initialization routine.
1826   emitGenMCSubtargetInfo(OS);
1827 
1828   OS << "\nstatic inline MCSubtargetInfo *create" << Target
1829      << "MCSubtargetInfoImpl("
1830      << "const Triple &TT, StringRef CPU, StringRef TuneCPU, StringRef FS) {\n";
1831   OS << "  return new " << Target
1832      << "GenMCSubtargetInfo(TT, CPU, TuneCPU, FS, ";
1833   if (NumFeatures)
1834     OS << Target << "FeatureKV, ";
1835   else
1836     OS << "None, ";
1837   if (NumProcs)
1838     OS << Target << "SubTypeKV, ";
1839   else
1840     OS << "None, ";
1841   OS << '\n'; OS.indent(22);
1842   OS << Target << "WriteProcResTable, "
1843      << Target << "WriteLatencyTable, "
1844      << Target << "ReadAdvanceTable, ";
1845   OS << '\n'; OS.indent(22);
1846   if (SchedModels.hasItineraries()) {
1847     OS << Target << "Stages, "
1848        << Target << "OperandCycles, "
1849        << Target << "ForwardingPaths";
1850   } else
1851     OS << "nullptr, nullptr, nullptr";
1852   OS << ");\n}\n\n";
1853 
1854   OS << "} // end namespace llvm\n\n";
1855 
1856   OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
1857 
1858   OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
1859   OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
1860 
1861   OS << "#include \"llvm/Support/Debug.h\"\n";
1862   OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
1863   ParseFeaturesFunction(OS, NumFeatures, NumProcs);
1864 
1865   OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
1866 
1867   // Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
1868   OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
1869   OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
1870 
1871   std::string ClassName = Target + "GenSubtargetInfo";
1872   OS << "namespace llvm {\n";
1873   OS << "class DFAPacketizer;\n";
1874   OS << "namespace " << Target << "_MC {\n"
1875      << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,"
1876      << " const MCInst *MI, const MCInstrInfo *MCII, unsigned CPUID);\n"
1877      << "} // end namespace " << Target << "_MC\n\n";
1878   OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
1879      << "  explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
1880      << "StringRef TuneCPU, StringRef FS);\n"
1881      << "public:\n"
1882      << "  unsigned resolveSchedClass(unsigned SchedClass, "
1883      << " const MachineInstr *DefMI,"
1884      << " const TargetSchedModel *SchedModel) const override;\n"
1885      << "  unsigned resolveVariantSchedClass(unsigned SchedClass,"
1886      << " const MCInst *MI, const MCInstrInfo *MCII,"
1887      << " unsigned CPUID) const override;\n"
1888      << "  DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)"
1889      << " const;\n";
1890   if (TGT.getHwModes().getNumModeIds() > 1)
1891     OS << "  unsigned getHwMode() const override;\n";
1892 
1893   STIPredicateExpander PE(Target);
1894   PE.setByRef(false);
1895   for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
1896     PE.expandSTIPredicate(OS, Fn);
1897 
1898   OS << "};\n"
1899      << "} // end namespace llvm\n\n";
1900 
1901   OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
1902 
1903   OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
1904   OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
1905 
1906   OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
1907   OS << "namespace llvm {\n";
1908   OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
1909   OS << "extern const llvm::SubtargetSubTypeKV " << Target << "SubTypeKV[];\n";
1910   OS << "extern const llvm::MCWriteProcResEntry "
1911      << Target << "WriteProcResTable[];\n";
1912   OS << "extern const llvm::MCWriteLatencyEntry "
1913      << Target << "WriteLatencyTable[];\n";
1914   OS << "extern const llvm::MCReadAdvanceEntry "
1915      << Target << "ReadAdvanceTable[];\n";
1916 
1917   if (SchedModels.hasItineraries()) {
1918     OS << "extern const llvm::InstrStage " << Target << "Stages[];\n";
1919     OS << "extern const unsigned " << Target << "OperandCycles[];\n";
1920     OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
1921   }
1922 
1923   OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
1924      << "StringRef TuneCPU, StringRef FS)\n"
1925      << "  : TargetSubtargetInfo(TT, CPU, TuneCPU, FS, ";
1926   if (NumFeatures)
1927     OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), ";
1928   else
1929     OS << "None, ";
1930   if (NumProcs)
1931     OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), ";
1932   else
1933     OS << "None, ";
1934   OS << '\n'; OS.indent(24);
1935   OS << Target << "WriteProcResTable, "
1936      << Target << "WriteLatencyTable, "
1937      << Target << "ReadAdvanceTable, ";
1938   OS << '\n'; OS.indent(24);
1939   if (SchedModels.hasItineraries()) {
1940     OS << Target << "Stages, "
1941        << Target << "OperandCycles, "
1942        << Target << "ForwardingPaths";
1943   } else
1944     OS << "nullptr, nullptr, nullptr";
1945   OS << ") {}\n\n";
1946 
1947   EmitSchedModelHelpers(ClassName, OS);
1948   EmitHwModeCheck(ClassName, OS);
1949 
1950   OS << "} // end namespace llvm\n\n";
1951 
1952   OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
1953 
1954   EmitMCInstrAnalysisPredicateFunctions(OS);
1955 }
1956 
1957 namespace llvm {
1958 
1959 void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) {
1960   CodeGenTarget CGTarget(RK);
1961   SubtargetEmitter(RK, CGTarget).run(OS);
1962 }
1963 
1964 } // end namespace llvm
1965