SubtargetEmitter.cpp 71 KB

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