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