CodeGenSchedule.cpp 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289
  1. //===- CodeGenSchedule.cpp - Scheduling MachineModels ---------------------===//
  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 file defines structures to encapsulate the machine model as described in
  10. // the target description.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenSchedule.h"
  14. #include "CodeGenInstruction.h"
  15. #include "CodeGenTarget.h"
  16. #include "llvm/ADT/MapVector.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/ADT/SmallSet.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/Support/Casting.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Support/Regex.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include "llvm/TableGen/Error.h"
  26. #include <algorithm>
  27. #include <iterator>
  28. #include <utility>
  29. using namespace llvm;
  30. #define DEBUG_TYPE "subtarget-emitter"
  31. #ifndef NDEBUG
  32. static void dumpIdxVec(ArrayRef<unsigned> V) {
  33. for (unsigned Idx : V)
  34. dbgs() << Idx << ", ";
  35. }
  36. #endif
  37. namespace {
  38. // (instrs a, b, ...) Evaluate and union all arguments. Identical to AddOp.
  39. struct InstrsOp : public SetTheory::Operator {
  40. void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
  41. ArrayRef<SMLoc> Loc) override {
  42. ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
  43. }
  44. };
  45. // (instregex "OpcPat",...) Find all instructions matching an opcode pattern.
  46. struct InstRegexOp : public SetTheory::Operator {
  47. const CodeGenTarget &Target;
  48. InstRegexOp(const CodeGenTarget &t): Target(t) {}
  49. /// Remove any text inside of parentheses from S.
  50. static std::string removeParens(llvm::StringRef S) {
  51. std::string Result;
  52. unsigned Paren = 0;
  53. // NB: We don't care about escaped parens here.
  54. for (char C : S) {
  55. switch (C) {
  56. case '(':
  57. ++Paren;
  58. break;
  59. case ')':
  60. --Paren;
  61. break;
  62. default:
  63. if (Paren == 0)
  64. Result += C;
  65. }
  66. }
  67. return Result;
  68. }
  69. void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
  70. ArrayRef<SMLoc> Loc) override {
  71. ArrayRef<const CodeGenInstruction *> Instructions =
  72. Target.getInstructionsByEnumValue();
  73. unsigned NumGeneric = Target.getNumFixedInstructions();
  74. unsigned NumPseudos = Target.getNumPseudoInstructions();
  75. auto Generics = Instructions.slice(0, NumGeneric);
  76. auto Pseudos = Instructions.slice(NumGeneric, NumPseudos);
  77. auto NonPseudos = Instructions.slice(NumGeneric + NumPseudos);
  78. for (Init *Arg : Expr->getArgs()) {
  79. StringInit *SI = dyn_cast<StringInit>(Arg);
  80. if (!SI)
  81. PrintFatalError(Loc, "instregex requires pattern string: " +
  82. Expr->getAsString());
  83. StringRef Original = SI->getValue();
  84. // Extract a prefix that we can binary search on.
  85. static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
  86. auto FirstMeta = Original.find_first_of(RegexMetachars);
  87. // Look for top-level | or ?. We cannot optimize them to binary search.
  88. if (removeParens(Original).find_first_of("|?") != std::string::npos)
  89. FirstMeta = 0;
  90. Optional<Regex> Regexpr = None;
  91. StringRef Prefix = Original.substr(0, FirstMeta);
  92. StringRef PatStr = Original.substr(FirstMeta);
  93. if (!PatStr.empty()) {
  94. // For the rest use a python-style prefix match.
  95. std::string pat = std::string(PatStr);
  96. if (pat[0] != '^') {
  97. pat.insert(0, "^(");
  98. pat.insert(pat.end(), ')');
  99. }
  100. Regexpr = Regex(pat);
  101. }
  102. int NumMatches = 0;
  103. // The generic opcodes are unsorted, handle them manually.
  104. for (auto *Inst : Generics) {
  105. StringRef InstName = Inst->TheDef->getName();
  106. if (InstName.startswith(Prefix) &&
  107. (!Regexpr || Regexpr->match(InstName.substr(Prefix.size())))) {
  108. Elts.insert(Inst->TheDef);
  109. NumMatches++;
  110. }
  111. }
  112. // Target instructions are split into two ranges: pseudo instructions
  113. // first, than non-pseudos. Each range is in lexicographical order
  114. // sorted by name. Find the sub-ranges that start with our prefix.
  115. struct Comp {
  116. bool operator()(const CodeGenInstruction *LHS, StringRef RHS) {
  117. return LHS->TheDef->getName() < RHS;
  118. }
  119. bool operator()(StringRef LHS, const CodeGenInstruction *RHS) {
  120. return LHS < RHS->TheDef->getName() &&
  121. !RHS->TheDef->getName().startswith(LHS);
  122. }
  123. };
  124. auto Range1 =
  125. std::equal_range(Pseudos.begin(), Pseudos.end(), Prefix, Comp());
  126. auto Range2 = std::equal_range(NonPseudos.begin(), NonPseudos.end(),
  127. Prefix, Comp());
  128. // For these ranges we know that instruction names start with the prefix.
  129. // Check if there's a regex that needs to be checked.
  130. const auto HandleNonGeneric = [&](const CodeGenInstruction *Inst) {
  131. StringRef InstName = Inst->TheDef->getName();
  132. if (!Regexpr || Regexpr->match(InstName.substr(Prefix.size()))) {
  133. Elts.insert(Inst->TheDef);
  134. NumMatches++;
  135. }
  136. };
  137. std::for_each(Range1.first, Range1.second, HandleNonGeneric);
  138. std::for_each(Range2.first, Range2.second, HandleNonGeneric);
  139. if (0 == NumMatches)
  140. PrintFatalError(Loc, "instregex has no matches: " + Original);
  141. }
  142. }
  143. };
  144. } // end anonymous namespace
  145. /// CodeGenModels ctor interprets machine model records and populates maps.
  146. CodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK,
  147. const CodeGenTarget &TGT):
  148. Records(RK), Target(TGT) {
  149. Sets.addFieldExpander("InstRW", "Instrs");
  150. // Allow Set evaluation to recognize the dags used in InstRW records:
  151. // (instrs Op1, Op1...)
  152. Sets.addOperator("instrs", std::make_unique<InstrsOp>());
  153. Sets.addOperator("instregex", std::make_unique<InstRegexOp>(Target));
  154. // Instantiate a CodeGenProcModel for each SchedMachineModel with the values
  155. // that are explicitly referenced in tablegen records. Resources associated
  156. // with each processor will be derived later. Populate ProcModelMap with the
  157. // CodeGenProcModel instances.
  158. collectProcModels();
  159. // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly
  160. // defined, and populate SchedReads and SchedWrites vectors. Implicit
  161. // SchedReadWrites that represent sequences derived from expanded variant will
  162. // be inferred later.
  163. collectSchedRW();
  164. // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly
  165. // required by an instruction definition, and populate SchedClassIdxMap. Set
  166. // NumItineraryClasses to the number of explicit itinerary classes referenced
  167. // by instructions. Set NumInstrSchedClasses to the number of itinerary
  168. // classes plus any classes implied by instructions that derive from class
  169. // Sched and provide SchedRW list. This does not infer any new classes from
  170. // SchedVariant.
  171. collectSchedClasses();
  172. // Find instruction itineraries for each processor. Sort and populate
  173. // CodeGenProcModel::ItinDefList. (Cycle-to-cycle itineraries). This requires
  174. // all itinerary classes to be discovered.
  175. collectProcItins();
  176. // Find ItinRW records for each processor and itinerary class.
  177. // (For per-operand resources mapped to itinerary classes).
  178. collectProcItinRW();
  179. // Find UnsupportedFeatures records for each processor.
  180. // (For per-operand resources mapped to itinerary classes).
  181. collectProcUnsupportedFeatures();
  182. // Infer new SchedClasses from SchedVariant.
  183. inferSchedClasses();
  184. // Populate each CodeGenProcModel's WriteResDefs, ReadAdvanceDefs, and
  185. // ProcResourceDefs.
  186. LLVM_DEBUG(
  187. dbgs() << "\n+++ RESOURCE DEFINITIONS (collectProcResources) +++\n");
  188. collectProcResources();
  189. // Collect optional processor description.
  190. collectOptionalProcessorInfo();
  191. // Check MCInstPredicate definitions.
  192. checkMCInstPredicates();
  193. // Check STIPredicate definitions.
  194. checkSTIPredicates();
  195. // Find STIPredicate definitions for each processor model, and construct
  196. // STIPredicateFunction objects.
  197. collectSTIPredicates();
  198. checkCompleteness();
  199. }
  200. void CodeGenSchedModels::checkSTIPredicates() const {
  201. DenseMap<StringRef, const Record *> Declarations;
  202. // There cannot be multiple declarations with the same name.
  203. const RecVec Decls = Records.getAllDerivedDefinitions("STIPredicateDecl");
  204. for (const Record *R : Decls) {
  205. StringRef Name = R->getValueAsString("Name");
  206. const auto It = Declarations.find(Name);
  207. if (It == Declarations.end()) {
  208. Declarations[Name] = R;
  209. continue;
  210. }
  211. PrintError(R->getLoc(), "STIPredicate " + Name + " multiply declared.");
  212. PrintFatalNote(It->second->getLoc(), "Previous declaration was here.");
  213. }
  214. // Disallow InstructionEquivalenceClasses with an empty instruction list.
  215. const RecVec Defs =
  216. Records.getAllDerivedDefinitions("InstructionEquivalenceClass");
  217. for (const Record *R : Defs) {
  218. RecVec Opcodes = R->getValueAsListOfDefs("Opcodes");
  219. if (Opcodes.empty()) {
  220. PrintFatalError(R->getLoc(), "Invalid InstructionEquivalenceClass "
  221. "defined with an empty opcode list.");
  222. }
  223. }
  224. }
  225. // Used by function `processSTIPredicate` to construct a mask of machine
  226. // instruction operands.
  227. static APInt constructOperandMask(ArrayRef<int64_t> Indices) {
  228. APInt OperandMask;
  229. if (Indices.empty())
  230. return OperandMask;
  231. int64_t MaxIndex = *std::max_element(Indices.begin(), Indices.end());
  232. assert(MaxIndex >= 0 && "Invalid negative indices in input!");
  233. OperandMask = OperandMask.zext(MaxIndex + 1);
  234. for (const int64_t Index : Indices) {
  235. assert(Index >= 0 && "Invalid negative indices!");
  236. OperandMask.setBit(Index);
  237. }
  238. return OperandMask;
  239. }
  240. static void
  241. processSTIPredicate(STIPredicateFunction &Fn,
  242. const ProcModelMapTy &ProcModelMap) {
  243. DenseMap<const Record *, unsigned> Opcode2Index;
  244. using OpcodeMapPair = std::pair<const Record *, OpcodeInfo>;
  245. std::vector<OpcodeMapPair> OpcodeMappings;
  246. std::vector<std::pair<APInt, APInt>> OpcodeMasks;
  247. DenseMap<const Record *, unsigned> Predicate2Index;
  248. unsigned NumUniquePredicates = 0;
  249. // Number unique predicates and opcodes used by InstructionEquivalenceClass
  250. // definitions. Each unique opcode will be associated with an OpcodeInfo
  251. // object.
  252. for (const Record *Def : Fn.getDefinitions()) {
  253. RecVec Classes = Def->getValueAsListOfDefs("Classes");
  254. for (const Record *EC : Classes) {
  255. const Record *Pred = EC->getValueAsDef("Predicate");
  256. if (Predicate2Index.find(Pred) == Predicate2Index.end())
  257. Predicate2Index[Pred] = NumUniquePredicates++;
  258. RecVec Opcodes = EC->getValueAsListOfDefs("Opcodes");
  259. for (const Record *Opcode : Opcodes) {
  260. if (Opcode2Index.find(Opcode) == Opcode2Index.end()) {
  261. Opcode2Index[Opcode] = OpcodeMappings.size();
  262. OpcodeMappings.emplace_back(Opcode, OpcodeInfo());
  263. }
  264. }
  265. }
  266. }
  267. // Initialize vector `OpcodeMasks` with default values. We want to keep track
  268. // of which processors "use" which opcodes. We also want to be able to
  269. // identify predicates that are used by different processors for a same
  270. // opcode.
  271. // This information is used later on by this algorithm to sort OpcodeMapping
  272. // elements based on their processor and predicate sets.
  273. OpcodeMasks.resize(OpcodeMappings.size());
  274. APInt DefaultProcMask(ProcModelMap.size(), 0);
  275. APInt DefaultPredMask(NumUniquePredicates, 0);
  276. for (std::pair<APInt, APInt> &MaskPair : OpcodeMasks)
  277. MaskPair = std::make_pair(DefaultProcMask, DefaultPredMask);
  278. // Construct a OpcodeInfo object for every unique opcode declared by an
  279. // InstructionEquivalenceClass definition.
  280. for (const Record *Def : Fn.getDefinitions()) {
  281. RecVec Classes = Def->getValueAsListOfDefs("Classes");
  282. const Record *SchedModel = Def->getValueAsDef("SchedModel");
  283. unsigned ProcIndex = ProcModelMap.find(SchedModel)->second;
  284. APInt ProcMask(ProcModelMap.size(), 0);
  285. ProcMask.setBit(ProcIndex);
  286. for (const Record *EC : Classes) {
  287. RecVec Opcodes = EC->getValueAsListOfDefs("Opcodes");
  288. std::vector<int64_t> OpIndices =
  289. EC->getValueAsListOfInts("OperandIndices");
  290. APInt OperandMask = constructOperandMask(OpIndices);
  291. const Record *Pred = EC->getValueAsDef("Predicate");
  292. APInt PredMask(NumUniquePredicates, 0);
  293. PredMask.setBit(Predicate2Index[Pred]);
  294. for (const Record *Opcode : Opcodes) {
  295. unsigned OpcodeIdx = Opcode2Index[Opcode];
  296. if (OpcodeMasks[OpcodeIdx].first[ProcIndex]) {
  297. std::string Message =
  298. "Opcode " + Opcode->getName().str() +
  299. " used by multiple InstructionEquivalenceClass definitions.";
  300. PrintFatalError(EC->getLoc(), Message);
  301. }
  302. OpcodeMasks[OpcodeIdx].first |= ProcMask;
  303. OpcodeMasks[OpcodeIdx].second |= PredMask;
  304. OpcodeInfo &OI = OpcodeMappings[OpcodeIdx].second;
  305. OI.addPredicateForProcModel(ProcMask, OperandMask, Pred);
  306. }
  307. }
  308. }
  309. // Sort OpcodeMappings elements based on their CPU and predicate masks.
  310. // As a last resort, order elements by opcode identifier.
  311. llvm::sort(OpcodeMappings,
  312. [&](const OpcodeMapPair &Lhs, const OpcodeMapPair &Rhs) {
  313. unsigned LhsIdx = Opcode2Index[Lhs.first];
  314. unsigned RhsIdx = Opcode2Index[Rhs.first];
  315. const std::pair<APInt, APInt> &LhsMasks = OpcodeMasks[LhsIdx];
  316. const std::pair<APInt, APInt> &RhsMasks = OpcodeMasks[RhsIdx];
  317. auto LessThan = [](const APInt &Lhs, const APInt &Rhs) {
  318. unsigned LhsCountPopulation = Lhs.countPopulation();
  319. unsigned RhsCountPopulation = Rhs.countPopulation();
  320. return ((LhsCountPopulation < RhsCountPopulation) ||
  321. ((LhsCountPopulation == RhsCountPopulation) &&
  322. (Lhs.countLeadingZeros() > Rhs.countLeadingZeros())));
  323. };
  324. if (LhsMasks.first != RhsMasks.first)
  325. return LessThan(LhsMasks.first, RhsMasks.first);
  326. if (LhsMasks.second != RhsMasks.second)
  327. return LessThan(LhsMasks.second, RhsMasks.second);
  328. return LhsIdx < RhsIdx;
  329. });
  330. // Now construct opcode groups. Groups are used by the SubtargetEmitter when
  331. // expanding the body of a STIPredicate function. In particular, each opcode
  332. // group is expanded into a sequence of labels in a switch statement.
  333. // It identifies opcodes for which different processors define same predicates
  334. // and same opcode masks.
  335. for (OpcodeMapPair &Info : OpcodeMappings)
  336. Fn.addOpcode(Info.first, std::move(Info.second));
  337. }
  338. void CodeGenSchedModels::collectSTIPredicates() {
  339. // Map STIPredicateDecl records to elements of vector
  340. // CodeGenSchedModels::STIPredicates.
  341. DenseMap<const Record *, unsigned> Decl2Index;
  342. RecVec RV = Records.getAllDerivedDefinitions("STIPredicate");
  343. for (const Record *R : RV) {
  344. const Record *Decl = R->getValueAsDef("Declaration");
  345. const auto It = Decl2Index.find(Decl);
  346. if (It == Decl2Index.end()) {
  347. Decl2Index[Decl] = STIPredicates.size();
  348. STIPredicateFunction Predicate(Decl);
  349. Predicate.addDefinition(R);
  350. STIPredicates.emplace_back(std::move(Predicate));
  351. continue;
  352. }
  353. STIPredicateFunction &PreviousDef = STIPredicates[It->second];
  354. PreviousDef.addDefinition(R);
  355. }
  356. for (STIPredicateFunction &Fn : STIPredicates)
  357. processSTIPredicate(Fn, ProcModelMap);
  358. }
  359. void OpcodeInfo::addPredicateForProcModel(const llvm::APInt &CpuMask,
  360. const llvm::APInt &OperandMask,
  361. const Record *Predicate) {
  362. auto It = llvm::find_if(
  363. Predicates, [&OperandMask, &Predicate](const PredicateInfo &P) {
  364. return P.Predicate == Predicate && P.OperandMask == OperandMask;
  365. });
  366. if (It == Predicates.end()) {
  367. Predicates.emplace_back(CpuMask, OperandMask, Predicate);
  368. return;
  369. }
  370. It->ProcModelMask |= CpuMask;
  371. }
  372. void CodeGenSchedModels::checkMCInstPredicates() const {
  373. RecVec MCPredicates = Records.getAllDerivedDefinitions("TIIPredicate");
  374. if (MCPredicates.empty())
  375. return;
  376. // A target cannot have multiple TIIPredicate definitions with a same name.
  377. llvm::StringMap<const Record *> TIIPredicates(MCPredicates.size());
  378. for (const Record *TIIPred : MCPredicates) {
  379. StringRef Name = TIIPred->getValueAsString("FunctionName");
  380. StringMap<const Record *>::const_iterator It = TIIPredicates.find(Name);
  381. if (It == TIIPredicates.end()) {
  382. TIIPredicates[Name] = TIIPred;
  383. continue;
  384. }
  385. PrintError(TIIPred->getLoc(),
  386. "TIIPredicate " + Name + " is multiply defined.");
  387. PrintFatalNote(It->second->getLoc(),
  388. " Previous definition of " + Name + " was here.");
  389. }
  390. }
  391. void CodeGenSchedModels::collectRetireControlUnits() {
  392. RecVec Units = Records.getAllDerivedDefinitions("RetireControlUnit");
  393. for (Record *RCU : Units) {
  394. CodeGenProcModel &PM = getProcModel(RCU->getValueAsDef("SchedModel"));
  395. if (PM.RetireControlUnit) {
  396. PrintError(RCU->getLoc(),
  397. "Expected a single RetireControlUnit definition");
  398. PrintNote(PM.RetireControlUnit->getLoc(),
  399. "Previous definition of RetireControlUnit was here");
  400. }
  401. PM.RetireControlUnit = RCU;
  402. }
  403. }
  404. void CodeGenSchedModels::collectLoadStoreQueueInfo() {
  405. RecVec Queues = Records.getAllDerivedDefinitions("MemoryQueue");
  406. for (Record *Queue : Queues) {
  407. CodeGenProcModel &PM = getProcModel(Queue->getValueAsDef("SchedModel"));
  408. if (Queue->isSubClassOf("LoadQueue")) {
  409. if (PM.LoadQueue) {
  410. PrintError(Queue->getLoc(),
  411. "Expected a single LoadQueue definition");
  412. PrintNote(PM.LoadQueue->getLoc(),
  413. "Previous definition of LoadQueue was here");
  414. }
  415. PM.LoadQueue = Queue;
  416. }
  417. if (Queue->isSubClassOf("StoreQueue")) {
  418. if (PM.StoreQueue) {
  419. PrintError(Queue->getLoc(),
  420. "Expected a single StoreQueue definition");
  421. PrintNote(PM.LoadQueue->getLoc(),
  422. "Previous definition of StoreQueue was here");
  423. }
  424. PM.StoreQueue = Queue;
  425. }
  426. }
  427. }
  428. /// Collect optional processor information.
  429. void CodeGenSchedModels::collectOptionalProcessorInfo() {
  430. // Find register file definitions for each processor.
  431. collectRegisterFiles();
  432. // Collect processor RetireControlUnit descriptors if available.
  433. collectRetireControlUnits();
  434. // Collect information about load/store queues.
  435. collectLoadStoreQueueInfo();
  436. checkCompleteness();
  437. }
  438. /// Gather all processor models.
  439. void CodeGenSchedModels::collectProcModels() {
  440. RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor");
  441. llvm::sort(ProcRecords, LessRecordFieldName());
  442. // Reserve space because we can. Reallocation would be ok.
  443. ProcModels.reserve(ProcRecords.size()+1);
  444. // Use idx=0 for NoModel/NoItineraries.
  445. Record *NoModelDef = Records.getDef("NoSchedModel");
  446. Record *NoItinsDef = Records.getDef("NoItineraries");
  447. ProcModels.emplace_back(0, "NoSchedModel", NoModelDef, NoItinsDef);
  448. ProcModelMap[NoModelDef] = 0;
  449. // For each processor, find a unique machine model.
  450. LLVM_DEBUG(dbgs() << "+++ PROCESSOR MODELs (addProcModel) +++\n");
  451. for (Record *ProcRecord : ProcRecords)
  452. addProcModel(ProcRecord);
  453. }
  454. /// Get a unique processor model based on the defined MachineModel and
  455. /// ProcessorItineraries.
  456. void CodeGenSchedModels::addProcModel(Record *ProcDef) {
  457. Record *ModelKey = getModelOrItinDef(ProcDef);
  458. if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
  459. return;
  460. std::string Name = std::string(ModelKey->getName());
  461. if (ModelKey->isSubClassOf("SchedMachineModel")) {
  462. Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
  463. ProcModels.emplace_back(ProcModels.size(), Name, ModelKey, ItinsDef);
  464. }
  465. else {
  466. // An itinerary is defined without a machine model. Infer a new model.
  467. if (!ModelKey->getValueAsListOfDefs("IID").empty())
  468. Name = Name + "Model";
  469. ProcModels.emplace_back(ProcModels.size(), Name,
  470. ProcDef->getValueAsDef("SchedModel"), ModelKey);
  471. }
  472. LLVM_DEBUG(ProcModels.back().dump());
  473. }
  474. // Recursively find all reachable SchedReadWrite records.
  475. static void scanSchedRW(Record *RWDef, RecVec &RWDefs,
  476. SmallPtrSet<Record*, 16> &RWSet) {
  477. if (!RWSet.insert(RWDef).second)
  478. return;
  479. RWDefs.push_back(RWDef);
  480. // Reads don't currently have sequence records, but it can be added later.
  481. if (RWDef->isSubClassOf("WriteSequence")) {
  482. RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
  483. for (Record *WSRec : Seq)
  484. scanSchedRW(WSRec, RWDefs, RWSet);
  485. }
  486. else if (RWDef->isSubClassOf("SchedVariant")) {
  487. // Visit each variant (guarded by a different predicate).
  488. RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
  489. for (Record *Variant : Vars) {
  490. // Visit each RW in the sequence selected by the current variant.
  491. RecVec Selected = Variant->getValueAsListOfDefs("Selected");
  492. for (Record *SelDef : Selected)
  493. scanSchedRW(SelDef, RWDefs, RWSet);
  494. }
  495. }
  496. }
  497. // Collect and sort all SchedReadWrites reachable via tablegen records.
  498. // More may be inferred later when inferring new SchedClasses from variants.
  499. void CodeGenSchedModels::collectSchedRW() {
  500. // Reserve idx=0 for invalid writes/reads.
  501. SchedWrites.resize(1);
  502. SchedReads.resize(1);
  503. SmallPtrSet<Record*, 16> RWSet;
  504. // Find all SchedReadWrites referenced by instruction defs.
  505. RecVec SWDefs, SRDefs;
  506. for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
  507. Record *SchedDef = Inst->TheDef;
  508. if (SchedDef->isValueUnset("SchedRW"))
  509. continue;
  510. RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
  511. for (Record *RW : RWs) {
  512. if (RW->isSubClassOf("SchedWrite"))
  513. scanSchedRW(RW, SWDefs, RWSet);
  514. else {
  515. assert(RW->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
  516. scanSchedRW(RW, SRDefs, RWSet);
  517. }
  518. }
  519. }
  520. // Find all ReadWrites referenced by InstRW.
  521. RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
  522. for (Record *InstRWDef : InstRWDefs) {
  523. // For all OperandReadWrites.
  524. RecVec RWDefs = InstRWDef->getValueAsListOfDefs("OperandReadWrites");
  525. for (Record *RWDef : RWDefs) {
  526. if (RWDef->isSubClassOf("SchedWrite"))
  527. scanSchedRW(RWDef, SWDefs, RWSet);
  528. else {
  529. assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
  530. scanSchedRW(RWDef, SRDefs, RWSet);
  531. }
  532. }
  533. }
  534. // Find all ReadWrites referenced by ItinRW.
  535. RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
  536. for (Record *ItinRWDef : ItinRWDefs) {
  537. // For all OperandReadWrites.
  538. RecVec RWDefs = ItinRWDef->getValueAsListOfDefs("OperandReadWrites");
  539. for (Record *RWDef : RWDefs) {
  540. if (RWDef->isSubClassOf("SchedWrite"))
  541. scanSchedRW(RWDef, SWDefs, RWSet);
  542. else {
  543. assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
  544. scanSchedRW(RWDef, SRDefs, RWSet);
  545. }
  546. }
  547. }
  548. // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted
  549. // for the loop below that initializes Alias vectors.
  550. RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias");
  551. llvm::sort(AliasDefs, LessRecord());
  552. for (Record *ADef : AliasDefs) {
  553. Record *MatchDef = ADef->getValueAsDef("MatchRW");
  554. Record *AliasDef = ADef->getValueAsDef("AliasRW");
  555. if (MatchDef->isSubClassOf("SchedWrite")) {
  556. if (!AliasDef->isSubClassOf("SchedWrite"))
  557. PrintFatalError(ADef->getLoc(), "SchedWrite Alias must be SchedWrite");
  558. scanSchedRW(AliasDef, SWDefs, RWSet);
  559. }
  560. else {
  561. assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
  562. if (!AliasDef->isSubClassOf("SchedRead"))
  563. PrintFatalError(ADef->getLoc(), "SchedRead Alias must be SchedRead");
  564. scanSchedRW(AliasDef, SRDefs, RWSet);
  565. }
  566. }
  567. // Sort and add the SchedReadWrites directly referenced by instructions or
  568. // itinerary resources. Index reads and writes in separate domains.
  569. llvm::sort(SWDefs, LessRecord());
  570. for (Record *SWDef : SWDefs) {
  571. assert(!getSchedRWIdx(SWDef, /*IsRead=*/false) && "duplicate SchedWrite");
  572. SchedWrites.emplace_back(SchedWrites.size(), SWDef);
  573. }
  574. llvm::sort(SRDefs, LessRecord());
  575. for (Record *SRDef : SRDefs) {
  576. assert(!getSchedRWIdx(SRDef, /*IsRead-*/true) && "duplicate SchedWrite");
  577. SchedReads.emplace_back(SchedReads.size(), SRDef);
  578. }
  579. // Initialize WriteSequence vectors.
  580. for (CodeGenSchedRW &CGRW : SchedWrites) {
  581. if (!CGRW.IsSequence)
  582. continue;
  583. findRWs(CGRW.TheDef->getValueAsListOfDefs("Writes"), CGRW.Sequence,
  584. /*IsRead=*/false);
  585. }
  586. // Initialize Aliases vectors.
  587. for (Record *ADef : AliasDefs) {
  588. Record *AliasDef = ADef->getValueAsDef("AliasRW");
  589. getSchedRW(AliasDef).IsAlias = true;
  590. Record *MatchDef = ADef->getValueAsDef("MatchRW");
  591. CodeGenSchedRW &RW = getSchedRW(MatchDef);
  592. if (RW.IsAlias)
  593. PrintFatalError(ADef->getLoc(), "Cannot Alias an Alias");
  594. RW.Aliases.push_back(ADef);
  595. }
  596. LLVM_DEBUG(
  597. dbgs() << "\n+++ SCHED READS and WRITES (collectSchedRW) +++\n";
  598. for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
  599. dbgs() << WIdx << ": ";
  600. SchedWrites[WIdx].dump();
  601. dbgs() << '\n';
  602. } for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd;
  603. ++RIdx) {
  604. dbgs() << RIdx << ": ";
  605. SchedReads[RIdx].dump();
  606. dbgs() << '\n';
  607. } RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
  608. for (Record *RWDef
  609. : RWDefs) {
  610. if (!getSchedRWIdx(RWDef, RWDef->isSubClassOf("SchedRead"))) {
  611. StringRef Name = RWDef->getName();
  612. if (Name != "NoWrite" && Name != "ReadDefault")
  613. dbgs() << "Unused SchedReadWrite " << Name << '\n';
  614. }
  615. });
  616. }
  617. /// Compute a SchedWrite name from a sequence of writes.
  618. std::string CodeGenSchedModels::genRWName(ArrayRef<unsigned> Seq, bool IsRead) {
  619. std::string Name("(");
  620. for (auto I = Seq.begin(), E = Seq.end(); I != E; ++I) {
  621. if (I != Seq.begin())
  622. Name += '_';
  623. Name += getSchedRW(*I, IsRead).Name;
  624. }
  625. Name += ')';
  626. return Name;
  627. }
  628. unsigned CodeGenSchedModels::getSchedRWIdx(const Record *Def,
  629. bool IsRead) const {
  630. const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
  631. const auto I = find_if(
  632. RWVec, [Def](const CodeGenSchedRW &RW) { return RW.TheDef == Def; });
  633. return I == RWVec.end() ? 0 : std::distance(RWVec.begin(), I);
  634. }
  635. bool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const {
  636. for (const CodeGenSchedRW &Read : SchedReads) {
  637. Record *ReadDef = Read.TheDef;
  638. if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance"))
  639. continue;
  640. RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites");
  641. if (is_contained(ValidWrites, WriteDef)) {
  642. return true;
  643. }
  644. }
  645. return false;
  646. }
  647. static void splitSchedReadWrites(const RecVec &RWDefs,
  648. RecVec &WriteDefs, RecVec &ReadDefs) {
  649. for (Record *RWDef : RWDefs) {
  650. if (RWDef->isSubClassOf("SchedWrite"))
  651. WriteDefs.push_back(RWDef);
  652. else {
  653. assert(RWDef->isSubClassOf("SchedRead") && "unknown SchedReadWrite");
  654. ReadDefs.push_back(RWDef);
  655. }
  656. }
  657. }
  658. // Split the SchedReadWrites defs and call findRWs for each list.
  659. void CodeGenSchedModels::findRWs(const RecVec &RWDefs,
  660. IdxVec &Writes, IdxVec &Reads) const {
  661. RecVec WriteDefs;
  662. RecVec ReadDefs;
  663. splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
  664. findRWs(WriteDefs, Writes, false);
  665. findRWs(ReadDefs, Reads, true);
  666. }
  667. // Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
  668. void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
  669. bool IsRead) const {
  670. for (Record *RWDef : RWDefs) {
  671. unsigned Idx = getSchedRWIdx(RWDef, IsRead);
  672. assert(Idx && "failed to collect SchedReadWrite");
  673. RWs.push_back(Idx);
  674. }
  675. }
  676. void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
  677. bool IsRead) const {
  678. const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
  679. if (!SchedRW.IsSequence) {
  680. RWSeq.push_back(RWIdx);
  681. return;
  682. }
  683. int Repeat =
  684. SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
  685. for (int i = 0; i < Repeat; ++i) {
  686. for (unsigned I : SchedRW.Sequence) {
  687. expandRWSequence(I, RWSeq, IsRead);
  688. }
  689. }
  690. }
  691. // Expand a SchedWrite as a sequence following any aliases that coincide with
  692. // the given processor model.
  693. void CodeGenSchedModels::expandRWSeqForProc(
  694. unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
  695. const CodeGenProcModel &ProcModel) const {
  696. const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead);
  697. Record *AliasDef = nullptr;
  698. for (const Record *Rec : SchedWrite.Aliases) {
  699. const CodeGenSchedRW &AliasRW = getSchedRW(Rec->getValueAsDef("AliasRW"));
  700. if (Rec->getValueInit("SchedModel")->isComplete()) {
  701. Record *ModelDef = Rec->getValueAsDef("SchedModel");
  702. if (&getProcModel(ModelDef) != &ProcModel)
  703. continue;
  704. }
  705. if (AliasDef)
  706. PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
  707. "defined for processor " + ProcModel.ModelName +
  708. " Ensure only one SchedAlias exists per RW.");
  709. AliasDef = AliasRW.TheDef;
  710. }
  711. if (AliasDef) {
  712. expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead),
  713. RWSeq, IsRead,ProcModel);
  714. return;
  715. }
  716. if (!SchedWrite.IsSequence) {
  717. RWSeq.push_back(RWIdx);
  718. return;
  719. }
  720. int Repeat =
  721. SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1;
  722. for (int I = 0, E = Repeat; I < E; ++I) {
  723. for (unsigned Idx : SchedWrite.Sequence) {
  724. expandRWSeqForProc(Idx, RWSeq, IsRead, ProcModel);
  725. }
  726. }
  727. }
  728. // Find the existing SchedWrite that models this sequence of writes.
  729. unsigned CodeGenSchedModels::findRWForSequence(ArrayRef<unsigned> Seq,
  730. bool IsRead) {
  731. std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
  732. auto I = find_if(RWVec, [Seq](CodeGenSchedRW &RW) {
  733. return makeArrayRef(RW.Sequence) == Seq;
  734. });
  735. // Index zero reserved for invalid RW.
  736. return I == RWVec.end() ? 0 : std::distance(RWVec.begin(), I);
  737. }
  738. /// Add this ReadWrite if it doesn't already exist.
  739. unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
  740. bool IsRead) {
  741. assert(!Seq.empty() && "cannot insert empty sequence");
  742. if (Seq.size() == 1)
  743. return Seq.back();
  744. unsigned Idx = findRWForSequence(Seq, IsRead);
  745. if (Idx)
  746. return Idx;
  747. std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
  748. unsigned RWIdx = RWVec.size();
  749. CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead));
  750. RWVec.push_back(SchedRW);
  751. return RWIdx;
  752. }
  753. /// Visit all the instruction definitions for this target to gather and
  754. /// enumerate the itinerary classes. These are the explicitly specified
  755. /// SchedClasses. More SchedClasses may be inferred.
  756. void CodeGenSchedModels::collectSchedClasses() {
  757. // NoItinerary is always the first class at Idx=0
  758. assert(SchedClasses.empty() && "Expected empty sched class");
  759. SchedClasses.emplace_back(0, "NoInstrModel",
  760. Records.getDef("NoItinerary"));
  761. SchedClasses.back().ProcIndices.push_back(0);
  762. // Create a SchedClass for each unique combination of itinerary class and
  763. // SchedRW list.
  764. for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
  765. Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
  766. IdxVec Writes, Reads;
  767. if (!Inst->TheDef->isValueUnset("SchedRW"))
  768. findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
  769. // ProcIdx == 0 indicates the class applies to all processors.
  770. unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, /*ProcIndices*/{0});
  771. InstrClassMap[Inst->TheDef] = SCIdx;
  772. }
  773. // Create classes for InstRW defs.
  774. RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
  775. llvm::sort(InstRWDefs, LessRecord());
  776. LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (createInstRWClass) +++\n");
  777. for (Record *RWDef : InstRWDefs)
  778. createInstRWClass(RWDef);
  779. NumInstrSchedClasses = SchedClasses.size();
  780. bool EnableDump = false;
  781. LLVM_DEBUG(EnableDump = true);
  782. if (!EnableDump)
  783. return;
  784. LLVM_DEBUG(
  785. dbgs()
  786. << "\n+++ ITINERARIES and/or MACHINE MODELS (collectSchedClasses) +++\n");
  787. for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
  788. StringRef InstName = Inst->TheDef->getName();
  789. unsigned SCIdx = getSchedClassIdx(*Inst);
  790. if (!SCIdx) {
  791. LLVM_DEBUG({
  792. if (!Inst->hasNoSchedulingInfo)
  793. dbgs() << "No machine model for " << Inst->TheDef->getName() << '\n';
  794. });
  795. continue;
  796. }
  797. CodeGenSchedClass &SC = getSchedClass(SCIdx);
  798. if (SC.ProcIndices[0] != 0)
  799. PrintFatalError(Inst->TheDef->getLoc(), "Instruction's sched class "
  800. "must not be subtarget specific.");
  801. IdxVec ProcIndices;
  802. if (SC.ItinClassDef->getName() != "NoItinerary") {
  803. ProcIndices.push_back(0);
  804. dbgs() << "Itinerary for " << InstName << ": "
  805. << SC.ItinClassDef->getName() << '\n';
  806. }
  807. if (!SC.Writes.empty()) {
  808. ProcIndices.push_back(0);
  809. LLVM_DEBUG({
  810. dbgs() << "SchedRW machine model for " << InstName;
  811. for (IdxIter WI = SC.Writes.begin(), WE = SC.Writes.end(); WI != WE;
  812. ++WI)
  813. dbgs() << " " << SchedWrites[*WI].Name;
  814. for (IdxIter RI = SC.Reads.begin(), RE = SC.Reads.end(); RI != RE; ++RI)
  815. dbgs() << " " << SchedReads[*RI].Name;
  816. dbgs() << '\n';
  817. });
  818. }
  819. const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
  820. for (Record *RWDef : RWDefs) {
  821. const CodeGenProcModel &ProcModel =
  822. getProcModel(RWDef->getValueAsDef("SchedModel"));
  823. ProcIndices.push_back(ProcModel.Index);
  824. LLVM_DEBUG(dbgs() << "InstRW on " << ProcModel.ModelName << " for "
  825. << InstName);
  826. IdxVec Writes;
  827. IdxVec Reads;
  828. findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
  829. Writes, Reads);
  830. LLVM_DEBUG({
  831. for (unsigned WIdx : Writes)
  832. dbgs() << " " << SchedWrites[WIdx].Name;
  833. for (unsigned RIdx : Reads)
  834. dbgs() << " " << SchedReads[RIdx].Name;
  835. dbgs() << '\n';
  836. });
  837. }
  838. // If ProcIndices contains zero, the class applies to all processors.
  839. LLVM_DEBUG({
  840. if (!llvm::is_contained(ProcIndices, 0)) {
  841. for (const CodeGenProcModel &PM : ProcModels) {
  842. if (!llvm::is_contained(ProcIndices, PM.Index))
  843. dbgs() << "No machine model for " << Inst->TheDef->getName()
  844. << " on processor " << PM.ModelName << '\n';
  845. }
  846. }
  847. });
  848. }
  849. }
  850. // Get the SchedClass index for an instruction.
  851. unsigned
  852. CodeGenSchedModels::getSchedClassIdx(const CodeGenInstruction &Inst) const {
  853. return InstrClassMap.lookup(Inst.TheDef);
  854. }
  855. std::string
  856. CodeGenSchedModels::createSchedClassName(Record *ItinClassDef,
  857. ArrayRef<unsigned> OperWrites,
  858. ArrayRef<unsigned> OperReads) {
  859. std::string Name;
  860. if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
  861. Name = std::string(ItinClassDef->getName());
  862. for (unsigned Idx : OperWrites) {
  863. if (!Name.empty())
  864. Name += '_';
  865. Name += SchedWrites[Idx].Name;
  866. }
  867. for (unsigned Idx : OperReads) {
  868. Name += '_';
  869. Name += SchedReads[Idx].Name;
  870. }
  871. return Name;
  872. }
  873. std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
  874. std::string Name;
  875. for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
  876. if (I != InstDefs.begin())
  877. Name += '_';
  878. Name += (*I)->getName();
  879. }
  880. return Name;
  881. }
  882. /// Add an inferred sched class from an itinerary class and per-operand list of
  883. /// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
  884. /// processors that may utilize this class.
  885. unsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
  886. ArrayRef<unsigned> OperWrites,
  887. ArrayRef<unsigned> OperReads,
  888. ArrayRef<unsigned> ProcIndices) {
  889. assert(!ProcIndices.empty() && "expect at least one ProcIdx");
  890. auto IsKeyEqual = [=](const CodeGenSchedClass &SC) {
  891. return SC.isKeyEqual(ItinClassDef, OperWrites, OperReads);
  892. };
  893. auto I = find_if(make_range(schedClassBegin(), schedClassEnd()), IsKeyEqual);
  894. unsigned Idx = I == schedClassEnd() ? 0 : std::distance(schedClassBegin(), I);
  895. if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
  896. IdxVec PI;
  897. std::set_union(SchedClasses[Idx].ProcIndices.begin(),
  898. SchedClasses[Idx].ProcIndices.end(),
  899. ProcIndices.begin(), ProcIndices.end(),
  900. std::back_inserter(PI));
  901. SchedClasses[Idx].ProcIndices = std::move(PI);
  902. return Idx;
  903. }
  904. Idx = SchedClasses.size();
  905. SchedClasses.emplace_back(Idx,
  906. createSchedClassName(ItinClassDef, OperWrites,
  907. OperReads),
  908. ItinClassDef);
  909. CodeGenSchedClass &SC = SchedClasses.back();
  910. SC.Writes = OperWrites;
  911. SC.Reads = OperReads;
  912. SC.ProcIndices = ProcIndices;
  913. return Idx;
  914. }
  915. // Create classes for each set of opcodes that are in the same InstReadWrite
  916. // definition across all processors.
  917. void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
  918. // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
  919. // intersects with an existing class via a previous InstRWDef. Instrs that do
  920. // not intersect with an existing class refer back to their former class as
  921. // determined from ItinDef or SchedRW.
  922. SmallMapVector<unsigned, SmallVector<Record *, 8>, 4> ClassInstrs;
  923. // Sort Instrs into sets.
  924. const RecVec *InstDefs = Sets.expand(InstRWDef);
  925. if (InstDefs->empty())
  926. PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
  927. for (Record *InstDef : *InstDefs) {
  928. InstClassMapTy::const_iterator Pos = InstrClassMap.find(InstDef);
  929. if (Pos == InstrClassMap.end())
  930. PrintFatalError(InstDef->getLoc(), "No sched class for instruction.");
  931. unsigned SCIdx = Pos->second;
  932. ClassInstrs[SCIdx].push_back(InstDef);
  933. }
  934. // For each set of Instrs, create a new class if necessary, and map or remap
  935. // the Instrs to it.
  936. for (auto &Entry : ClassInstrs) {
  937. unsigned OldSCIdx = Entry.first;
  938. ArrayRef<Record*> InstDefs = Entry.second;
  939. // If the all instrs in the current class are accounted for, then leave
  940. // them mapped to their old class.
  941. if (OldSCIdx) {
  942. const RecVec &RWDefs = SchedClasses[OldSCIdx].InstRWs;
  943. if (!RWDefs.empty()) {
  944. const RecVec *OrigInstDefs = Sets.expand(RWDefs[0]);
  945. unsigned OrigNumInstrs =
  946. count_if(*OrigInstDefs, [&](Record *OIDef) {
  947. return InstrClassMap[OIDef] == OldSCIdx;
  948. });
  949. if (OrigNumInstrs == InstDefs.size()) {
  950. assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
  951. "expected a generic SchedClass");
  952. Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
  953. // Make sure we didn't already have a InstRW containing this
  954. // instruction on this model.
  955. for (Record *RWD : RWDefs) {
  956. if (RWD->getValueAsDef("SchedModel") == RWModelDef &&
  957. RWModelDef->getValueAsBit("FullInstRWOverlapCheck")) {
  958. assert(!InstDefs.empty()); // Checked at function start.
  959. PrintError(
  960. InstRWDef->getLoc(),
  961. "Overlapping InstRW definition for \"" +
  962. InstDefs.front()->getName() +
  963. "\" also matches previous \"" +
  964. RWD->getValue("Instrs")->getValue()->getAsString() +
  965. "\".");
  966. PrintFatalNote(RWD->getLoc(), "Previous match was here.");
  967. }
  968. }
  969. LLVM_DEBUG(dbgs() << "InstRW: Reuse SC " << OldSCIdx << ":"
  970. << SchedClasses[OldSCIdx].Name << " on "
  971. << RWModelDef->getName() << "\n");
  972. SchedClasses[OldSCIdx].InstRWs.push_back(InstRWDef);
  973. continue;
  974. }
  975. }
  976. }
  977. unsigned SCIdx = SchedClasses.size();
  978. SchedClasses.emplace_back(SCIdx, createSchedClassName(InstDefs), nullptr);
  979. CodeGenSchedClass &SC = SchedClasses.back();
  980. LLVM_DEBUG(dbgs() << "InstRW: New SC " << SCIdx << ":" << SC.Name << " on "
  981. << InstRWDef->getValueAsDef("SchedModel")->getName()
  982. << "\n");
  983. // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
  984. SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
  985. SC.Writes = SchedClasses[OldSCIdx].Writes;
  986. SC.Reads = SchedClasses[OldSCIdx].Reads;
  987. SC.ProcIndices.push_back(0);
  988. // If we had an old class, copy it's InstRWs to this new class.
  989. if (OldSCIdx) {
  990. Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
  991. for (Record *OldRWDef : SchedClasses[OldSCIdx].InstRWs) {
  992. if (OldRWDef->getValueAsDef("SchedModel") == RWModelDef) {
  993. assert(!InstDefs.empty()); // Checked at function start.
  994. PrintError(
  995. InstRWDef->getLoc(),
  996. "Overlapping InstRW definition for \"" +
  997. InstDefs.front()->getName() + "\" also matches previous \"" +
  998. OldRWDef->getValue("Instrs")->getValue()->getAsString() +
  999. "\".");
  1000. PrintFatalNote(OldRWDef->getLoc(), "Previous match was here.");
  1001. }
  1002. assert(OldRWDef != InstRWDef &&
  1003. "SchedClass has duplicate InstRW def");
  1004. SC.InstRWs.push_back(OldRWDef);
  1005. }
  1006. }
  1007. // Map each Instr to this new class.
  1008. for (Record *InstDef : InstDefs)
  1009. InstrClassMap[InstDef] = SCIdx;
  1010. SC.InstRWs.push_back(InstRWDef);
  1011. }
  1012. }
  1013. // True if collectProcItins found anything.
  1014. bool CodeGenSchedModels::hasItineraries() const {
  1015. for (const CodeGenProcModel &PM : make_range(procModelBegin(),procModelEnd()))
  1016. if (PM.hasItineraries())
  1017. return true;
  1018. return false;
  1019. }
  1020. // Gather the processor itineraries.
  1021. void CodeGenSchedModels::collectProcItins() {
  1022. LLVM_DEBUG(dbgs() << "\n+++ PROBLEM ITINERARIES (collectProcItins) +++\n");
  1023. for (CodeGenProcModel &ProcModel : ProcModels) {
  1024. if (!ProcModel.hasItineraries())
  1025. continue;
  1026. RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
  1027. assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
  1028. // Populate ItinDefList with Itinerary records.
  1029. ProcModel.ItinDefList.resize(NumInstrSchedClasses);
  1030. // Insert each itinerary data record in the correct position within
  1031. // the processor model's ItinDefList.
  1032. for (Record *ItinData : ItinRecords) {
  1033. const Record *ItinDef = ItinData->getValueAsDef("TheClass");
  1034. bool FoundClass = false;
  1035. for (const CodeGenSchedClass &SC :
  1036. make_range(schedClassBegin(), schedClassEnd())) {
  1037. // Multiple SchedClasses may share an itinerary. Update all of them.
  1038. if (SC.ItinClassDef == ItinDef) {
  1039. ProcModel.ItinDefList[SC.Index] = ItinData;
  1040. FoundClass = true;
  1041. }
  1042. }
  1043. if (!FoundClass) {
  1044. LLVM_DEBUG(dbgs() << ProcModel.ItinsDef->getName()
  1045. << " missing class for itinerary "
  1046. << ItinDef->getName() << '\n');
  1047. }
  1048. }
  1049. // Check for missing itinerary entries.
  1050. assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
  1051. LLVM_DEBUG(
  1052. for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
  1053. if (!ProcModel.ItinDefList[i])
  1054. dbgs() << ProcModel.ItinsDef->getName()
  1055. << " missing itinerary for class " << SchedClasses[i].Name
  1056. << '\n';
  1057. });
  1058. }
  1059. }
  1060. // Gather the read/write types for each itinerary class.
  1061. void CodeGenSchedModels::collectProcItinRW() {
  1062. RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
  1063. llvm::sort(ItinRWDefs, LessRecord());
  1064. for (Record *RWDef : ItinRWDefs) {
  1065. if (!RWDef->getValueInit("SchedModel")->isComplete())
  1066. PrintFatalError(RWDef->getLoc(), "SchedModel is undefined");
  1067. Record *ModelDef = RWDef->getValueAsDef("SchedModel");
  1068. ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
  1069. if (I == ProcModelMap.end()) {
  1070. PrintFatalError(RWDef->getLoc(), "Undefined SchedMachineModel "
  1071. + ModelDef->getName());
  1072. }
  1073. ProcModels[I->second].ItinRWDefs.push_back(RWDef);
  1074. }
  1075. }
  1076. // Gather the unsupported features for processor models.
  1077. void CodeGenSchedModels::collectProcUnsupportedFeatures() {
  1078. for (CodeGenProcModel &ProcModel : ProcModels)
  1079. append_range(
  1080. ProcModel.UnsupportedFeaturesDefs,
  1081. ProcModel.ModelDef->getValueAsListOfDefs("UnsupportedFeatures"));
  1082. }
  1083. /// Infer new classes from existing classes. In the process, this may create new
  1084. /// SchedWrites from sequences of existing SchedWrites.
  1085. void CodeGenSchedModels::inferSchedClasses() {
  1086. LLVM_DEBUG(
  1087. dbgs() << "\n+++ INFERRING SCHED CLASSES (inferSchedClasses) +++\n");
  1088. LLVM_DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
  1089. // Visit all existing classes and newly created classes.
  1090. for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
  1091. assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
  1092. if (SchedClasses[Idx].ItinClassDef)
  1093. inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
  1094. if (!SchedClasses[Idx].InstRWs.empty())
  1095. inferFromInstRWs(Idx);
  1096. if (!SchedClasses[Idx].Writes.empty()) {
  1097. inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
  1098. Idx, SchedClasses[Idx].ProcIndices);
  1099. }
  1100. assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
  1101. "too many SchedVariants");
  1102. }
  1103. }
  1104. /// Infer classes from per-processor itinerary resources.
  1105. void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
  1106. unsigned FromClassIdx) {
  1107. for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
  1108. const CodeGenProcModel &PM = ProcModels[PIdx];
  1109. // For all ItinRW entries.
  1110. bool HasMatch = false;
  1111. for (const Record *Rec : PM.ItinRWDefs) {
  1112. RecVec Matched = Rec->getValueAsListOfDefs("MatchedItinClasses");
  1113. if (!llvm::is_contained(Matched, ItinClassDef))
  1114. continue;
  1115. if (HasMatch)
  1116. PrintFatalError(Rec->getLoc(), "Duplicate itinerary class "
  1117. + ItinClassDef->getName()
  1118. + " in ItinResources for " + PM.ModelName);
  1119. HasMatch = true;
  1120. IdxVec Writes, Reads;
  1121. findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
  1122. inferFromRW(Writes, Reads, FromClassIdx, PIdx);
  1123. }
  1124. }
  1125. }
  1126. /// Infer classes from per-processor InstReadWrite definitions.
  1127. void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
  1128. for (unsigned I = 0, E = SchedClasses[SCIdx].InstRWs.size(); I != E; ++I) {
  1129. assert(SchedClasses[SCIdx].InstRWs.size() == E && "InstrRWs was mutated!");
  1130. Record *Rec = SchedClasses[SCIdx].InstRWs[I];
  1131. const RecVec *InstDefs = Sets.expand(Rec);
  1132. RecIter II = InstDefs->begin(), IE = InstDefs->end();
  1133. for (; II != IE; ++II) {
  1134. if (InstrClassMap[*II] == SCIdx)
  1135. break;
  1136. }
  1137. // If this class no longer has any instructions mapped to it, it has become
  1138. // irrelevant.
  1139. if (II == IE)
  1140. continue;
  1141. IdxVec Writes, Reads;
  1142. findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
  1143. unsigned PIdx = getProcModel(Rec->getValueAsDef("SchedModel")).Index;
  1144. inferFromRW(Writes, Reads, SCIdx, PIdx); // May mutate SchedClasses.
  1145. SchedClasses[SCIdx].InstRWProcIndices.insert(PIdx);
  1146. }
  1147. }
  1148. namespace {
  1149. // Helper for substituteVariantOperand.
  1150. struct TransVariant {
  1151. Record *VarOrSeqDef; // Variant or sequence.
  1152. unsigned RWIdx; // Index of this variant or sequence's matched type.
  1153. unsigned ProcIdx; // Processor model index or zero for any.
  1154. unsigned TransVecIdx; // Index into PredTransitions::TransVec.
  1155. TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
  1156. VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
  1157. };
  1158. // Associate a predicate with the SchedReadWrite that it guards.
  1159. // RWIdx is the index of the read/write variant.
  1160. struct PredCheck {
  1161. bool IsRead;
  1162. unsigned RWIdx;
  1163. Record *Predicate;
  1164. PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
  1165. };
  1166. // A Predicate transition is a list of RW sequences guarded by a PredTerm.
  1167. struct PredTransition {
  1168. // A predicate term is a conjunction of PredChecks.
  1169. SmallVector<PredCheck, 4> PredTerm;
  1170. SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
  1171. SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
  1172. unsigned ProcIndex = 0;
  1173. PredTransition() = default;
  1174. PredTransition(ArrayRef<PredCheck> PT, unsigned ProcId) {
  1175. PredTerm.assign(PT.begin(), PT.end());
  1176. ProcIndex = ProcId;
  1177. }
  1178. };
  1179. // Encapsulate a set of partially constructed transitions.
  1180. // The results are built by repeated calls to substituteVariants.
  1181. class PredTransitions {
  1182. CodeGenSchedModels &SchedModels;
  1183. public:
  1184. std::vector<PredTransition> TransVec;
  1185. PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
  1186. bool substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
  1187. bool IsRead, unsigned StartIdx);
  1188. bool substituteVariants(const PredTransition &Trans);
  1189. #ifndef NDEBUG
  1190. void dump() const;
  1191. #endif
  1192. private:
  1193. bool mutuallyExclusive(Record *PredDef, ArrayRef<Record *> Preds,
  1194. ArrayRef<PredCheck> Term);
  1195. void getIntersectingVariants(
  1196. const CodeGenSchedRW &SchedRW, unsigned TransIdx,
  1197. std::vector<TransVariant> &IntersectingVariants);
  1198. void pushVariant(const TransVariant &VInfo, bool IsRead);
  1199. };
  1200. } // end anonymous namespace
  1201. // Return true if this predicate is mutually exclusive with a PredTerm. This
  1202. // degenerates into checking if the predicate is mutually exclusive with any
  1203. // predicate in the Term's conjunction.
  1204. //
  1205. // All predicates associated with a given SchedRW are considered mutually
  1206. // exclusive. This should work even if the conditions expressed by the
  1207. // predicates are not exclusive because the predicates for a given SchedWrite
  1208. // are always checked in the order they are defined in the .td file. Later
  1209. // conditions implicitly negate any prior condition.
  1210. bool PredTransitions::mutuallyExclusive(Record *PredDef,
  1211. ArrayRef<Record *> Preds,
  1212. ArrayRef<PredCheck> Term) {
  1213. for (const PredCheck &PC: Term) {
  1214. if (PC.Predicate == PredDef)
  1215. return false;
  1216. const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(PC.RWIdx, PC.IsRead);
  1217. assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
  1218. RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
  1219. if (any_of(Variants, [PredDef](const Record *R) {
  1220. return R->getValueAsDef("Predicate") == PredDef;
  1221. })) {
  1222. // To check if PredDef is mutually exclusive with PC we also need to
  1223. // check that PC.Predicate is exclusive with all predicates from variant
  1224. // we're expanding. Consider following RW sequence with two variants
  1225. // (1 & 2), where A, B and C are predicates from corresponding SchedVars:
  1226. //
  1227. // 1:A/B - 2:C/B
  1228. //
  1229. // Here C is not mutually exclusive with variant (1), because A doesn't
  1230. // exist in variant (2). This means we have possible transitions from A
  1231. // to C and from A to B, and fully expanded sequence would look like:
  1232. //
  1233. // if (A & C) return ...;
  1234. // if (A & B) return ...;
  1235. // if (B) return ...;
  1236. //
  1237. // Now let's consider another sequence:
  1238. //
  1239. // 1:A/B - 2:A/B
  1240. //
  1241. // Here A in variant (2) is mutually exclusive with variant (1), because
  1242. // A also exists in (2). This means A->B transition is impossible and
  1243. // expanded sequence would look like:
  1244. //
  1245. // if (A) return ...;
  1246. // if (B) return ...;
  1247. if (!count(Preds, PC.Predicate))
  1248. continue;
  1249. return true;
  1250. }
  1251. }
  1252. return false;
  1253. }
  1254. static std::vector<Record *> getAllPredicates(ArrayRef<TransVariant> Variants,
  1255. unsigned ProcId) {
  1256. std::vector<Record *> Preds;
  1257. for (auto &Variant : Variants) {
  1258. if (!Variant.VarOrSeqDef->isSubClassOf("SchedVar"))
  1259. continue;
  1260. Preds.push_back(Variant.VarOrSeqDef->getValueAsDef("Predicate"));
  1261. }
  1262. return Preds;
  1263. }
  1264. // Populate IntersectingVariants with any variants or aliased sequences of the
  1265. // given SchedRW whose processor indices and predicates are not mutually
  1266. // exclusive with the given transition.
  1267. void PredTransitions::getIntersectingVariants(
  1268. const CodeGenSchedRW &SchedRW, unsigned TransIdx,
  1269. std::vector<TransVariant> &IntersectingVariants) {
  1270. bool GenericRW = false;
  1271. std::vector<TransVariant> Variants;
  1272. if (SchedRW.HasVariants) {
  1273. unsigned VarProcIdx = 0;
  1274. if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
  1275. Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
  1276. VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
  1277. }
  1278. if (VarProcIdx == 0 || VarProcIdx == TransVec[TransIdx].ProcIndex) {
  1279. // Push each variant. Assign TransVecIdx later.
  1280. const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
  1281. for (Record *VarDef : VarDefs)
  1282. Variants.emplace_back(VarDef, SchedRW.Index, VarProcIdx, 0);
  1283. if (VarProcIdx == 0)
  1284. GenericRW = true;
  1285. }
  1286. }
  1287. for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
  1288. AI != AE; ++AI) {
  1289. // If either the SchedAlias itself or the SchedReadWrite that it aliases
  1290. // to is defined within a processor model, constrain all variants to
  1291. // that processor.
  1292. unsigned AliasProcIdx = 0;
  1293. if ((*AI)->getValueInit("SchedModel")->isComplete()) {
  1294. Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
  1295. AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
  1296. }
  1297. if (AliasProcIdx && AliasProcIdx != TransVec[TransIdx].ProcIndex)
  1298. continue;
  1299. if (!Variants.empty()) {
  1300. const CodeGenProcModel &PM =
  1301. *(SchedModels.procModelBegin() + AliasProcIdx);
  1302. PrintFatalError((*AI)->getLoc(),
  1303. "Multiple variants defined for processor " +
  1304. PM.ModelName +
  1305. " Ensure only one SchedAlias exists per RW.");
  1306. }
  1307. const CodeGenSchedRW &AliasRW =
  1308. SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
  1309. if (AliasRW.HasVariants) {
  1310. const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
  1311. for (Record *VD : VarDefs)
  1312. Variants.emplace_back(VD, AliasRW.Index, AliasProcIdx, 0);
  1313. }
  1314. if (AliasRW.IsSequence)
  1315. Variants.emplace_back(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0);
  1316. if (AliasProcIdx == 0)
  1317. GenericRW = true;
  1318. }
  1319. std::vector<Record *> AllPreds =
  1320. getAllPredicates(Variants, TransVec[TransIdx].ProcIndex);
  1321. for (TransVariant &Variant : Variants) {
  1322. // Don't expand variants if the processor models don't intersect.
  1323. // A zero processor index means any processor.
  1324. if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
  1325. Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
  1326. if (mutuallyExclusive(PredDef, AllPreds, TransVec[TransIdx].PredTerm))
  1327. continue;
  1328. }
  1329. if (IntersectingVariants.empty()) {
  1330. // The first variant builds on the existing transition.
  1331. Variant.TransVecIdx = TransIdx;
  1332. IntersectingVariants.push_back(Variant);
  1333. }
  1334. else {
  1335. // Push another copy of the current transition for more variants.
  1336. Variant.TransVecIdx = TransVec.size();
  1337. IntersectingVariants.push_back(Variant);
  1338. TransVec.push_back(TransVec[TransIdx]);
  1339. }
  1340. }
  1341. if (GenericRW && IntersectingVariants.empty()) {
  1342. PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
  1343. "a matching predicate on any processor");
  1344. }
  1345. }
  1346. // Push the Reads/Writes selected by this variant onto the PredTransition
  1347. // specified by VInfo.
  1348. void PredTransitions::
  1349. pushVariant(const TransVariant &VInfo, bool IsRead) {
  1350. PredTransition &Trans = TransVec[VInfo.TransVecIdx];
  1351. // If this operand transition is reached through a processor-specific alias,
  1352. // then the whole transition is specific to this processor.
  1353. IdxVec SelectedRWs;
  1354. if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
  1355. Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
  1356. Trans.PredTerm.emplace_back(IsRead, VInfo.RWIdx,PredDef);
  1357. RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
  1358. SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
  1359. }
  1360. else {
  1361. assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
  1362. "variant must be a SchedVariant or aliased WriteSequence");
  1363. SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
  1364. }
  1365. const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
  1366. SmallVectorImpl<SmallVector<unsigned,4>> &RWSequences = IsRead
  1367. ? Trans.ReadSequences : Trans.WriteSequences;
  1368. if (SchedRW.IsVariadic) {
  1369. unsigned OperIdx = RWSequences.size()-1;
  1370. // Make N-1 copies of this transition's last sequence.
  1371. RWSequences.reserve(RWSequences.size() + SelectedRWs.size() - 1);
  1372. RWSequences.insert(RWSequences.end(), SelectedRWs.size() - 1,
  1373. RWSequences[OperIdx]);
  1374. // Push each of the N elements of the SelectedRWs onto a copy of the last
  1375. // sequence (split the current operand into N operands).
  1376. // Note that write sequences should be expanded within this loop--the entire
  1377. // sequence belongs to a single operand.
  1378. for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
  1379. RWI != RWE; ++RWI, ++OperIdx) {
  1380. IdxVec ExpandedRWs;
  1381. if (IsRead)
  1382. ExpandedRWs.push_back(*RWI);
  1383. else
  1384. SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
  1385. llvm::append_range(RWSequences[OperIdx], ExpandedRWs);
  1386. }
  1387. assert(OperIdx == RWSequences.size() && "missed a sequence");
  1388. }
  1389. else {
  1390. // Push this transition's expanded sequence onto this transition's last
  1391. // sequence (add to the current operand's sequence).
  1392. SmallVectorImpl<unsigned> &Seq = RWSequences.back();
  1393. IdxVec ExpandedRWs;
  1394. for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
  1395. RWI != RWE; ++RWI) {
  1396. if (IsRead)
  1397. ExpandedRWs.push_back(*RWI);
  1398. else
  1399. SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
  1400. }
  1401. llvm::append_range(Seq, ExpandedRWs);
  1402. }
  1403. }
  1404. // RWSeq is a sequence of all Reads or all Writes for the next read or write
  1405. // operand. StartIdx is an index into TransVec where partial results
  1406. // starts. RWSeq must be applied to all transitions between StartIdx and the end
  1407. // of TransVec.
  1408. bool PredTransitions::substituteVariantOperand(
  1409. const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
  1410. bool Subst = false;
  1411. // Visit each original RW within the current sequence.
  1412. for (SmallVectorImpl<unsigned>::const_iterator
  1413. RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
  1414. const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
  1415. // Push this RW on all partial PredTransitions or distribute variants.
  1416. // New PredTransitions may be pushed within this loop which should not be
  1417. // revisited (TransEnd must be loop invariant).
  1418. for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
  1419. TransIdx != TransEnd; ++TransIdx) {
  1420. // Distribute this partial PredTransition across intersecting variants.
  1421. // This will push a copies of TransVec[TransIdx] on the back of TransVec.
  1422. std::vector<TransVariant> IntersectingVariants;
  1423. getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
  1424. // Now expand each variant on top of its copy of the transition.
  1425. for (const TransVariant &IV : IntersectingVariants)
  1426. pushVariant(IV, IsRead);
  1427. if (IntersectingVariants.empty()) {
  1428. if (IsRead)
  1429. TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
  1430. else
  1431. TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
  1432. continue;
  1433. } else {
  1434. Subst = true;
  1435. }
  1436. }
  1437. }
  1438. return Subst;
  1439. }
  1440. // For each variant of a Read/Write in Trans, substitute the sequence of
  1441. // Read/Writes guarded by the variant. This is exponential in the number of
  1442. // variant Read/Writes, but in practice detection of mutually exclusive
  1443. // predicates should result in linear growth in the total number variants.
  1444. //
  1445. // This is one step in a breadth-first search of nested variants.
  1446. bool PredTransitions::substituteVariants(const PredTransition &Trans) {
  1447. // Build up a set of partial results starting at the back of
  1448. // PredTransitions. Remember the first new transition.
  1449. unsigned StartIdx = TransVec.size();
  1450. bool Subst = false;
  1451. assert(Trans.ProcIndex != 0);
  1452. TransVec.emplace_back(Trans.PredTerm, Trans.ProcIndex);
  1453. // Visit each original write sequence.
  1454. for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
  1455. WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
  1456. WSI != WSE; ++WSI) {
  1457. // Push a new (empty) write sequence onto all partial Transitions.
  1458. for (std::vector<PredTransition>::iterator I =
  1459. TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
  1460. I->WriteSequences.emplace_back();
  1461. }
  1462. Subst |= substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
  1463. }
  1464. // Visit each original read sequence.
  1465. for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
  1466. RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
  1467. RSI != RSE; ++RSI) {
  1468. // Push a new (empty) read sequence onto all partial Transitions.
  1469. for (std::vector<PredTransition>::iterator I =
  1470. TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
  1471. I->ReadSequences.emplace_back();
  1472. }
  1473. Subst |= substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
  1474. }
  1475. return Subst;
  1476. }
  1477. static void addSequences(CodeGenSchedModels &SchedModels,
  1478. const SmallVectorImpl<SmallVector<unsigned, 4>> &Seqs,
  1479. IdxVec &Result, bool IsRead) {
  1480. for (const auto &S : Seqs)
  1481. if (!S.empty())
  1482. Result.push_back(SchedModels.findOrInsertRW(S, IsRead));
  1483. }
  1484. #ifndef NDEBUG
  1485. static void dumpRecVec(const RecVec &RV) {
  1486. for (const Record *R : RV)
  1487. dbgs() << R->getName() << ", ";
  1488. }
  1489. #endif
  1490. static void dumpTransition(const CodeGenSchedModels &SchedModels,
  1491. const CodeGenSchedClass &FromSC,
  1492. const CodeGenSchedTransition &SCTrans,
  1493. const RecVec &Preds) {
  1494. LLVM_DEBUG(dbgs() << "Adding transition from " << FromSC.Name << "("
  1495. << FromSC.Index << ") to "
  1496. << SchedModels.getSchedClass(SCTrans.ToClassIdx).Name << "("
  1497. << SCTrans.ToClassIdx << ") on pred term: (";
  1498. dumpRecVec(Preds);
  1499. dbgs() << ") on processor (" << SCTrans.ProcIndex << ")\n");
  1500. }
  1501. // Create a new SchedClass for each variant found by inferFromRW. Pass
  1502. static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
  1503. unsigned FromClassIdx,
  1504. CodeGenSchedModels &SchedModels) {
  1505. // For each PredTransition, create a new CodeGenSchedTransition, which usually
  1506. // requires creating a new SchedClass.
  1507. for (ArrayRef<PredTransition>::iterator
  1508. I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
  1509. // Variant expansion (substituteVariants) may create unconditional
  1510. // transitions. We don't need to build sched classes for them.
  1511. if (I->PredTerm.empty())
  1512. continue;
  1513. IdxVec OperWritesVariant, OperReadsVariant;
  1514. addSequences(SchedModels, I->WriteSequences, OperWritesVariant, false);
  1515. addSequences(SchedModels, I->ReadSequences, OperReadsVariant, true);
  1516. CodeGenSchedTransition SCTrans;
  1517. // Transition should not contain processor indices already assigned to
  1518. // InstRWs in this scheduling class.
  1519. const CodeGenSchedClass &FromSC = SchedModels.getSchedClass(FromClassIdx);
  1520. if (FromSC.InstRWProcIndices.count(I->ProcIndex))
  1521. continue;
  1522. SCTrans.ProcIndex = I->ProcIndex;
  1523. SCTrans.ToClassIdx =
  1524. SchedModels.addSchedClass(/*ItinClassDef=*/nullptr, OperWritesVariant,
  1525. OperReadsVariant, I->ProcIndex);
  1526. // The final PredTerm is unique set of predicates guarding the transition.
  1527. RecVec Preds;
  1528. transform(I->PredTerm, std::back_inserter(Preds),
  1529. [](const PredCheck &P) {
  1530. return P.Predicate;
  1531. });
  1532. Preds.erase(std::unique(Preds.begin(), Preds.end()), Preds.end());
  1533. dumpTransition(SchedModels, FromSC, SCTrans, Preds);
  1534. SCTrans.PredTerm = std::move(Preds);
  1535. SchedModels.getSchedClass(FromClassIdx)
  1536. .Transitions.push_back(std::move(SCTrans));
  1537. }
  1538. }
  1539. std::vector<unsigned> CodeGenSchedModels::getAllProcIndices() const {
  1540. std::vector<unsigned> ProcIdVec;
  1541. for (const auto &PM : ProcModelMap)
  1542. if (PM.second != 0)
  1543. ProcIdVec.push_back(PM.second);
  1544. // The order of the keys (Record pointers) of ProcModelMap are not stable.
  1545. // Sort to stabalize the values.
  1546. llvm::sort(ProcIdVec);
  1547. return ProcIdVec;
  1548. }
  1549. static std::vector<PredTransition>
  1550. makePerProcessorTransitions(const PredTransition &Trans,
  1551. ArrayRef<unsigned> ProcIndices) {
  1552. std::vector<PredTransition> PerCpuTransVec;
  1553. for (unsigned ProcId : ProcIndices) {
  1554. assert(ProcId != 0);
  1555. PerCpuTransVec.push_back(Trans);
  1556. PerCpuTransVec.back().ProcIndex = ProcId;
  1557. }
  1558. return PerCpuTransVec;
  1559. }
  1560. // Create new SchedClasses for the given ReadWrite list. If any of the
  1561. // ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
  1562. // of the ReadWrite list, following Aliases if necessary.
  1563. void CodeGenSchedModels::inferFromRW(ArrayRef<unsigned> OperWrites,
  1564. ArrayRef<unsigned> OperReads,
  1565. unsigned FromClassIdx,
  1566. ArrayRef<unsigned> ProcIndices) {
  1567. LLVM_DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices);
  1568. dbgs() << ") ");
  1569. // Create a seed transition with an empty PredTerm and the expanded sequences
  1570. // of SchedWrites for the current SchedClass.
  1571. std::vector<PredTransition> LastTransitions;
  1572. LastTransitions.emplace_back();
  1573. for (unsigned WriteIdx : OperWrites) {
  1574. IdxVec WriteSeq;
  1575. expandRWSequence(WriteIdx, WriteSeq, /*IsRead=*/false);
  1576. LastTransitions[0].WriteSequences.emplace_back();
  1577. SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences.back();
  1578. Seq.append(WriteSeq.begin(), WriteSeq.end());
  1579. LLVM_DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
  1580. }
  1581. LLVM_DEBUG(dbgs() << " Reads: ");
  1582. for (unsigned ReadIdx : OperReads) {
  1583. IdxVec ReadSeq;
  1584. expandRWSequence(ReadIdx, ReadSeq, /*IsRead=*/true);
  1585. LastTransitions[0].ReadSequences.emplace_back();
  1586. SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences.back();
  1587. Seq.append(ReadSeq.begin(), ReadSeq.end());
  1588. LLVM_DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
  1589. }
  1590. LLVM_DEBUG(dbgs() << '\n');
  1591. LastTransitions = makePerProcessorTransitions(
  1592. LastTransitions[0], llvm::is_contained(ProcIndices, 0)
  1593. ? ArrayRef<unsigned>(getAllProcIndices())
  1594. : ProcIndices);
  1595. // Collect all PredTransitions for individual operands.
  1596. // Iterate until no variant writes remain.
  1597. bool SubstitutedAny;
  1598. do {
  1599. SubstitutedAny = false;
  1600. PredTransitions Transitions(*this);
  1601. for (const PredTransition &Trans : LastTransitions)
  1602. SubstitutedAny |= Transitions.substituteVariants(Trans);
  1603. LLVM_DEBUG(Transitions.dump());
  1604. LastTransitions.swap(Transitions.TransVec);
  1605. } while (SubstitutedAny);
  1606. // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
  1607. // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
  1608. inferFromTransitions(LastTransitions, FromClassIdx, *this);
  1609. }
  1610. // Check if any processor resource group contains all resource records in
  1611. // SubUnits.
  1612. bool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
  1613. for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
  1614. if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
  1615. continue;
  1616. RecVec SuperUnits =
  1617. PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
  1618. RecIter RI = SubUnits.begin(), RE = SubUnits.end();
  1619. for ( ; RI != RE; ++RI) {
  1620. if (!is_contained(SuperUnits, *RI)) {
  1621. break;
  1622. }
  1623. }
  1624. if (RI == RE)
  1625. return true;
  1626. }
  1627. return false;
  1628. }
  1629. // Verify that overlapping groups have a common supergroup.
  1630. void CodeGenSchedModels::verifyProcResourceGroups(CodeGenProcModel &PM) {
  1631. for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
  1632. if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
  1633. continue;
  1634. RecVec CheckUnits =
  1635. PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
  1636. for (unsigned j = i+1; j < e; ++j) {
  1637. if (!PM.ProcResourceDefs[j]->isSubClassOf("ProcResGroup"))
  1638. continue;
  1639. RecVec OtherUnits =
  1640. PM.ProcResourceDefs[j]->getValueAsListOfDefs("Resources");
  1641. if (std::find_first_of(CheckUnits.begin(), CheckUnits.end(),
  1642. OtherUnits.begin(), OtherUnits.end())
  1643. != CheckUnits.end()) {
  1644. // CheckUnits and OtherUnits overlap
  1645. llvm::append_range(OtherUnits, CheckUnits);
  1646. if (!hasSuperGroup(OtherUnits, PM)) {
  1647. PrintFatalError((PM.ProcResourceDefs[i])->getLoc(),
  1648. "proc resource group overlaps with "
  1649. + PM.ProcResourceDefs[j]->getName()
  1650. + " but no supergroup contains both.");
  1651. }
  1652. }
  1653. }
  1654. }
  1655. }
  1656. // Collect all the RegisterFile definitions available in this target.
  1657. void CodeGenSchedModels::collectRegisterFiles() {
  1658. RecVec RegisterFileDefs = Records.getAllDerivedDefinitions("RegisterFile");
  1659. // RegisterFiles is the vector of CodeGenRegisterFile.
  1660. for (Record *RF : RegisterFileDefs) {
  1661. // For each register file definition, construct a CodeGenRegisterFile object
  1662. // and add it to the appropriate scheduling model.
  1663. CodeGenProcModel &PM = getProcModel(RF->getValueAsDef("SchedModel"));
  1664. PM.RegisterFiles.emplace_back(CodeGenRegisterFile(RF->getName(),RF));
  1665. CodeGenRegisterFile &CGRF = PM.RegisterFiles.back();
  1666. CGRF.MaxMovesEliminatedPerCycle =
  1667. RF->getValueAsInt("MaxMovesEliminatedPerCycle");
  1668. CGRF.AllowZeroMoveEliminationOnly =
  1669. RF->getValueAsBit("AllowZeroMoveEliminationOnly");
  1670. // Now set the number of physical registers as well as the cost of registers
  1671. // in each register class.
  1672. CGRF.NumPhysRegs = RF->getValueAsInt("NumPhysRegs");
  1673. if (!CGRF.NumPhysRegs) {
  1674. PrintFatalError(RF->getLoc(),
  1675. "Invalid RegisterFile with zero physical registers");
  1676. }
  1677. RecVec RegisterClasses = RF->getValueAsListOfDefs("RegClasses");
  1678. std::vector<int64_t> RegisterCosts = RF->getValueAsListOfInts("RegCosts");
  1679. ListInit *MoveElimInfo = RF->getValueAsListInit("AllowMoveElimination");
  1680. for (unsigned I = 0, E = RegisterClasses.size(); I < E; ++I) {
  1681. int Cost = RegisterCosts.size() > I ? RegisterCosts[I] : 1;
  1682. bool AllowMoveElim = false;
  1683. if (MoveElimInfo->size() > I) {
  1684. BitInit *Val = cast<BitInit>(MoveElimInfo->getElement(I));
  1685. AllowMoveElim = Val->getValue();
  1686. }
  1687. CGRF.Costs.emplace_back(RegisterClasses[I], Cost, AllowMoveElim);
  1688. }
  1689. }
  1690. }
  1691. // Collect and sort WriteRes, ReadAdvance, and ProcResources.
  1692. void CodeGenSchedModels::collectProcResources() {
  1693. ProcResourceDefs = Records.getAllDerivedDefinitions("ProcResourceUnits");
  1694. ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
  1695. // Add any subtarget-specific SchedReadWrites that are directly associated
  1696. // with processor resources. Refer to the parent SchedClass's ProcIndices to
  1697. // determine which processors they apply to.
  1698. for (const CodeGenSchedClass &SC :
  1699. make_range(schedClassBegin(), schedClassEnd())) {
  1700. if (SC.ItinClassDef) {
  1701. collectItinProcResources(SC.ItinClassDef);
  1702. continue;
  1703. }
  1704. // This class may have a default ReadWrite list which can be overriden by
  1705. // InstRW definitions.
  1706. for (Record *RW : SC.InstRWs) {
  1707. Record *RWModelDef = RW->getValueAsDef("SchedModel");
  1708. unsigned PIdx = getProcModel(RWModelDef).Index;
  1709. IdxVec Writes, Reads;
  1710. findRWs(RW->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
  1711. collectRWResources(Writes, Reads, PIdx);
  1712. }
  1713. collectRWResources(SC.Writes, SC.Reads, SC.ProcIndices);
  1714. }
  1715. // Add resources separately defined by each subtarget.
  1716. RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes");
  1717. for (Record *WR : WRDefs) {
  1718. Record *ModelDef = WR->getValueAsDef("SchedModel");
  1719. addWriteRes(WR, getProcModel(ModelDef).Index);
  1720. }
  1721. RecVec SWRDefs = Records.getAllDerivedDefinitions("SchedWriteRes");
  1722. for (Record *SWR : SWRDefs) {
  1723. Record *ModelDef = SWR->getValueAsDef("SchedModel");
  1724. addWriteRes(SWR, getProcModel(ModelDef).Index);
  1725. }
  1726. RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance");
  1727. for (Record *RA : RADefs) {
  1728. Record *ModelDef = RA->getValueAsDef("SchedModel");
  1729. addReadAdvance(RA, getProcModel(ModelDef).Index);
  1730. }
  1731. RecVec SRADefs = Records.getAllDerivedDefinitions("SchedReadAdvance");
  1732. for (Record *SRA : SRADefs) {
  1733. if (SRA->getValueInit("SchedModel")->isComplete()) {
  1734. Record *ModelDef = SRA->getValueAsDef("SchedModel");
  1735. addReadAdvance(SRA, getProcModel(ModelDef).Index);
  1736. }
  1737. }
  1738. // Add ProcResGroups that are defined within this processor model, which may
  1739. // not be directly referenced but may directly specify a buffer size.
  1740. RecVec ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
  1741. for (Record *PRG : ProcResGroups) {
  1742. if (!PRG->getValueInit("SchedModel")->isComplete())
  1743. continue;
  1744. CodeGenProcModel &PM = getProcModel(PRG->getValueAsDef("SchedModel"));
  1745. if (!is_contained(PM.ProcResourceDefs, PRG))
  1746. PM.ProcResourceDefs.push_back(PRG);
  1747. }
  1748. // Add ProcResourceUnits unconditionally.
  1749. for (Record *PRU : Records.getAllDerivedDefinitions("ProcResourceUnits")) {
  1750. if (!PRU->getValueInit("SchedModel")->isComplete())
  1751. continue;
  1752. CodeGenProcModel &PM = getProcModel(PRU->getValueAsDef("SchedModel"));
  1753. if (!is_contained(PM.ProcResourceDefs, PRU))
  1754. PM.ProcResourceDefs.push_back(PRU);
  1755. }
  1756. // Finalize each ProcModel by sorting the record arrays.
  1757. for (CodeGenProcModel &PM : ProcModels) {
  1758. llvm::sort(PM.WriteResDefs, LessRecord());
  1759. llvm::sort(PM.ReadAdvanceDefs, LessRecord());
  1760. llvm::sort(PM.ProcResourceDefs, LessRecord());
  1761. LLVM_DEBUG(
  1762. PM.dump();
  1763. dbgs() << "WriteResDefs: "; for (RecIter RI = PM.WriteResDefs.begin(),
  1764. RE = PM.WriteResDefs.end();
  1765. RI != RE; ++RI) {
  1766. if ((*RI)->isSubClassOf("WriteRes"))
  1767. dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " ";
  1768. else
  1769. dbgs() << (*RI)->getName() << " ";
  1770. } dbgs() << "\nReadAdvanceDefs: ";
  1771. for (RecIter RI = PM.ReadAdvanceDefs.begin(),
  1772. RE = PM.ReadAdvanceDefs.end();
  1773. RI != RE; ++RI) {
  1774. if ((*RI)->isSubClassOf("ReadAdvance"))
  1775. dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " ";
  1776. else
  1777. dbgs() << (*RI)->getName() << " ";
  1778. } dbgs()
  1779. << "\nProcResourceDefs: ";
  1780. for (RecIter RI = PM.ProcResourceDefs.begin(),
  1781. RE = PM.ProcResourceDefs.end();
  1782. RI != RE; ++RI) { dbgs() << (*RI)->getName() << " "; } dbgs()
  1783. << '\n');
  1784. verifyProcResourceGroups(PM);
  1785. }
  1786. ProcResourceDefs.clear();
  1787. ProcResGroups.clear();
  1788. }
  1789. void CodeGenSchedModels::checkCompleteness() {
  1790. bool Complete = true;
  1791. bool HadCompleteModel = false;
  1792. for (const CodeGenProcModel &ProcModel : procModels()) {
  1793. const bool HasItineraries = ProcModel.hasItineraries();
  1794. if (!ProcModel.ModelDef->getValueAsBit("CompleteModel"))
  1795. continue;
  1796. for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
  1797. if (Inst->hasNoSchedulingInfo)
  1798. continue;
  1799. if (ProcModel.isUnsupported(*Inst))
  1800. continue;
  1801. unsigned SCIdx = getSchedClassIdx(*Inst);
  1802. if (!SCIdx) {
  1803. if (Inst->TheDef->isValueUnset("SchedRW") && !HadCompleteModel) {
  1804. PrintError(Inst->TheDef->getLoc(),
  1805. "No schedule information for instruction '" +
  1806. Inst->TheDef->getName() + "' in SchedMachineModel '" +
  1807. ProcModel.ModelDef->getName() + "'");
  1808. Complete = false;
  1809. }
  1810. continue;
  1811. }
  1812. const CodeGenSchedClass &SC = getSchedClass(SCIdx);
  1813. if (!SC.Writes.empty())
  1814. continue;
  1815. if (HasItineraries && SC.ItinClassDef != nullptr &&
  1816. SC.ItinClassDef->getName() != "NoItinerary")
  1817. continue;
  1818. const RecVec &InstRWs = SC.InstRWs;
  1819. auto I = find_if(InstRWs, [&ProcModel](const Record *R) {
  1820. return R->getValueAsDef("SchedModel") == ProcModel.ModelDef;
  1821. });
  1822. if (I == InstRWs.end()) {
  1823. PrintError(Inst->TheDef->getLoc(), "'" + ProcModel.ModelName +
  1824. "' lacks information for '" +
  1825. Inst->TheDef->getName() + "'");
  1826. Complete = false;
  1827. }
  1828. }
  1829. HadCompleteModel = true;
  1830. }
  1831. if (!Complete) {
  1832. errs() << "\n\nIncomplete schedule models found.\n"
  1833. << "- Consider setting 'CompleteModel = 0' while developing new models.\n"
  1834. << "- Pseudo instructions can be marked with 'hasNoSchedulingInfo = 1'.\n"
  1835. << "- Instructions should usually have Sched<[...]> as a superclass, "
  1836. "you may temporarily use an empty list.\n"
  1837. << "- Instructions related to unsupported features can be excluded with "
  1838. "list<Predicate> UnsupportedFeatures = [HasA,..,HasY]; in the "
  1839. "processor model.\n\n";
  1840. PrintFatalError("Incomplete schedule model");
  1841. }
  1842. }
  1843. // Collect itinerary class resources for each processor.
  1844. void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
  1845. for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
  1846. const CodeGenProcModel &PM = ProcModels[PIdx];
  1847. // For all ItinRW entries.
  1848. bool HasMatch = false;
  1849. for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
  1850. II != IE; ++II) {
  1851. RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
  1852. if (!llvm::is_contained(Matched, ItinClassDef))
  1853. continue;
  1854. if (HasMatch)
  1855. PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
  1856. + ItinClassDef->getName()
  1857. + " in ItinResources for " + PM.ModelName);
  1858. HasMatch = true;
  1859. IdxVec Writes, Reads;
  1860. findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
  1861. collectRWResources(Writes, Reads, PIdx);
  1862. }
  1863. }
  1864. }
  1865. void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
  1866. ArrayRef<unsigned> ProcIndices) {
  1867. const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
  1868. if (SchedRW.TheDef) {
  1869. if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
  1870. for (unsigned Idx : ProcIndices)
  1871. addWriteRes(SchedRW.TheDef, Idx);
  1872. }
  1873. else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
  1874. for (unsigned Idx : ProcIndices)
  1875. addReadAdvance(SchedRW.TheDef, Idx);
  1876. }
  1877. }
  1878. for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
  1879. AI != AE; ++AI) {
  1880. IdxVec AliasProcIndices;
  1881. if ((*AI)->getValueInit("SchedModel")->isComplete()) {
  1882. AliasProcIndices.push_back(
  1883. getProcModel((*AI)->getValueAsDef("SchedModel")).Index);
  1884. }
  1885. else
  1886. AliasProcIndices = ProcIndices;
  1887. const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
  1888. assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
  1889. IdxVec ExpandedRWs;
  1890. expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
  1891. for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
  1892. SI != SE; ++SI) {
  1893. collectRWResources(*SI, IsRead, AliasProcIndices);
  1894. }
  1895. }
  1896. }
  1897. // Collect resources for a set of read/write types and processor indices.
  1898. void CodeGenSchedModels::collectRWResources(ArrayRef<unsigned> Writes,
  1899. ArrayRef<unsigned> Reads,
  1900. ArrayRef<unsigned> ProcIndices) {
  1901. for (unsigned Idx : Writes)
  1902. collectRWResources(Idx, /*IsRead=*/false, ProcIndices);
  1903. for (unsigned Idx : Reads)
  1904. collectRWResources(Idx, /*IsRead=*/true, ProcIndices);
  1905. }
  1906. // Find the processor's resource units for this kind of resource.
  1907. Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
  1908. const CodeGenProcModel &PM,
  1909. ArrayRef<SMLoc> Loc) const {
  1910. if (ProcResKind->isSubClassOf("ProcResourceUnits"))
  1911. return ProcResKind;
  1912. Record *ProcUnitDef = nullptr;
  1913. assert(!ProcResourceDefs.empty());
  1914. assert(!ProcResGroups.empty());
  1915. for (Record *ProcResDef : ProcResourceDefs) {
  1916. if (ProcResDef->getValueAsDef("Kind") == ProcResKind
  1917. && ProcResDef->getValueAsDef("SchedModel") == PM.ModelDef) {
  1918. if (ProcUnitDef) {
  1919. PrintFatalError(Loc,
  1920. "Multiple ProcessorResourceUnits associated with "
  1921. + ProcResKind->getName());
  1922. }
  1923. ProcUnitDef = ProcResDef;
  1924. }
  1925. }
  1926. for (Record *ProcResGroup : ProcResGroups) {
  1927. if (ProcResGroup == ProcResKind
  1928. && ProcResGroup->getValueAsDef("SchedModel") == PM.ModelDef) {
  1929. if (ProcUnitDef) {
  1930. PrintFatalError(Loc,
  1931. "Multiple ProcessorResourceUnits associated with "
  1932. + ProcResKind->getName());
  1933. }
  1934. ProcUnitDef = ProcResGroup;
  1935. }
  1936. }
  1937. if (!ProcUnitDef) {
  1938. PrintFatalError(Loc,
  1939. "No ProcessorResources associated with "
  1940. + ProcResKind->getName());
  1941. }
  1942. return ProcUnitDef;
  1943. }
  1944. // Iteratively add a resource and its super resources.
  1945. void CodeGenSchedModels::addProcResource(Record *ProcResKind,
  1946. CodeGenProcModel &PM,
  1947. ArrayRef<SMLoc> Loc) {
  1948. while (true) {
  1949. Record *ProcResUnits = findProcResUnits(ProcResKind, PM, Loc);
  1950. // See if this ProcResource is already associated with this processor.
  1951. if (is_contained(PM.ProcResourceDefs, ProcResUnits))
  1952. return;
  1953. PM.ProcResourceDefs.push_back(ProcResUnits);
  1954. if (ProcResUnits->isSubClassOf("ProcResGroup"))
  1955. return;
  1956. if (!ProcResUnits->getValueInit("Super")->isComplete())
  1957. return;
  1958. ProcResKind = ProcResUnits->getValueAsDef("Super");
  1959. }
  1960. }
  1961. // Add resources for a SchedWrite to this processor if they don't exist.
  1962. void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
  1963. assert(PIdx && "don't add resources to an invalid Processor model");
  1964. RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
  1965. if (is_contained(WRDefs, ProcWriteResDef))
  1966. return;
  1967. WRDefs.push_back(ProcWriteResDef);
  1968. // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
  1969. RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
  1970. for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end();
  1971. WritePRI != WritePRE; ++WritePRI) {
  1972. addProcResource(*WritePRI, ProcModels[PIdx], ProcWriteResDef->getLoc());
  1973. }
  1974. }
  1975. // Add resources for a ReadAdvance to this processor if they don't exist.
  1976. void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
  1977. unsigned PIdx) {
  1978. RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
  1979. if (is_contained(RADefs, ProcReadAdvanceDef))
  1980. return;
  1981. RADefs.push_back(ProcReadAdvanceDef);
  1982. }
  1983. unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
  1984. RecIter PRPos = find(ProcResourceDefs, PRDef);
  1985. if (PRPos == ProcResourceDefs.end())
  1986. PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
  1987. "the ProcResources list for " + ModelName);
  1988. // Idx=0 is reserved for invalid.
  1989. return 1 + (PRPos - ProcResourceDefs.begin());
  1990. }
  1991. bool CodeGenProcModel::isUnsupported(const CodeGenInstruction &Inst) const {
  1992. for (const Record *TheDef : UnsupportedFeaturesDefs) {
  1993. for (const Record *PredDef : Inst.TheDef->getValueAsListOfDefs("Predicates")) {
  1994. if (TheDef->getName() == PredDef->getName())
  1995. return true;
  1996. }
  1997. }
  1998. return false;
  1999. }
  2000. #ifndef NDEBUG
  2001. void CodeGenProcModel::dump() const {
  2002. dbgs() << Index << ": " << ModelName << " "
  2003. << (ModelDef ? ModelDef->getName() : "inferred") << " "
  2004. << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
  2005. }
  2006. void CodeGenSchedRW::dump() const {
  2007. dbgs() << Name << (IsVariadic ? " (V) " : " ");
  2008. if (IsSequence) {
  2009. dbgs() << "(";
  2010. dumpIdxVec(Sequence);
  2011. dbgs() << ")";
  2012. }
  2013. }
  2014. void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
  2015. dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
  2016. << " Writes: ";
  2017. for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
  2018. SchedModels->getSchedWrite(Writes[i]).dump();
  2019. if (i < N-1) {
  2020. dbgs() << '\n';
  2021. dbgs().indent(10);
  2022. }
  2023. }
  2024. dbgs() << "\n Reads: ";
  2025. for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
  2026. SchedModels->getSchedRead(Reads[i]).dump();
  2027. if (i < N-1) {
  2028. dbgs() << '\n';
  2029. dbgs().indent(10);
  2030. }
  2031. }
  2032. dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices);
  2033. if (!Transitions.empty()) {
  2034. dbgs() << "\n Transitions for Proc ";
  2035. for (const CodeGenSchedTransition &Transition : Transitions) {
  2036. dbgs() << Transition.ProcIndex << ", ";
  2037. }
  2038. }
  2039. dbgs() << '\n';
  2040. }
  2041. void PredTransitions::dump() const {
  2042. dbgs() << "Expanded Variants:\n";
  2043. for (std::vector<PredTransition>::const_iterator
  2044. TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
  2045. dbgs() << "{";
  2046. for (SmallVectorImpl<PredCheck>::const_iterator
  2047. PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
  2048. PCI != PCE; ++PCI) {
  2049. if (PCI != TI->PredTerm.begin())
  2050. dbgs() << ", ";
  2051. dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
  2052. << ":" << PCI->Predicate->getName();
  2053. }
  2054. dbgs() << "},\n => {";
  2055. for (SmallVectorImpl<SmallVector<unsigned,4>>::const_iterator
  2056. WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
  2057. WSI != WSE; ++WSI) {
  2058. dbgs() << "(";
  2059. for (SmallVectorImpl<unsigned>::const_iterator
  2060. WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
  2061. if (WI != WSI->begin())
  2062. dbgs() << ", ";
  2063. dbgs() << SchedModels.getSchedWrite(*WI).Name;
  2064. }
  2065. dbgs() << "),";
  2066. }
  2067. dbgs() << "}\n";
  2068. }
  2069. }
  2070. #endif // NDEBUG