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