CodeGenSchedule.cpp 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  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. // Check for duplicated names.
  443. auto I = std::adjacent_find(ProcRecords.begin(), ProcRecords.end(),
  444. [](const Record *Rec1, const Record *Rec2) {
  445. return Rec1->getValueAsString("Name") == Rec2->getValueAsString("Name");
  446. });
  447. if (I != ProcRecords.end())
  448. PrintFatalError((*I)->getLoc(), "Duplicate processor name " +
  449. (*I)->getValueAsString("Name"));
  450. // Reserve space because we can. Reallocation would be ok.
  451. ProcModels.reserve(ProcRecords.size()+1);
  452. // Use idx=0 for NoModel/NoItineraries.
  453. Record *NoModelDef = Records.getDef("NoSchedModel");
  454. Record *NoItinsDef = Records.getDef("NoItineraries");
  455. ProcModels.emplace_back(0, "NoSchedModel", NoModelDef, NoItinsDef);
  456. ProcModelMap[NoModelDef] = 0;
  457. // For each processor, find a unique machine model.
  458. LLVM_DEBUG(dbgs() << "+++ PROCESSOR MODELs (addProcModel) +++\n");
  459. for (Record *ProcRecord : ProcRecords)
  460. addProcModel(ProcRecord);
  461. }
  462. /// Get a unique processor model based on the defined MachineModel and
  463. /// ProcessorItineraries.
  464. void CodeGenSchedModels::addProcModel(Record *ProcDef) {
  465. Record *ModelKey = getModelOrItinDef(ProcDef);
  466. if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
  467. return;
  468. std::string Name = std::string(ModelKey->getName());
  469. if (ModelKey->isSubClassOf("SchedMachineModel")) {
  470. Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
  471. ProcModels.emplace_back(ProcModels.size(), Name, ModelKey, ItinsDef);
  472. }
  473. else {
  474. // An itinerary is defined without a machine model. Infer a new model.
  475. if (!ModelKey->getValueAsListOfDefs("IID").empty())
  476. Name = Name + "Model";
  477. ProcModels.emplace_back(ProcModels.size(), Name,
  478. ProcDef->getValueAsDef("SchedModel"), ModelKey);
  479. }
  480. LLVM_DEBUG(ProcModels.back().dump());
  481. }
  482. // Recursively find all reachable SchedReadWrite records.
  483. static void scanSchedRW(Record *RWDef, RecVec &RWDefs,
  484. SmallPtrSet<Record*, 16> &RWSet) {
  485. if (!RWSet.insert(RWDef).second)
  486. return;
  487. RWDefs.push_back(RWDef);
  488. // Reads don't currently have sequence records, but it can be added later.
  489. if (RWDef->isSubClassOf("WriteSequence")) {
  490. RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
  491. for (Record *WSRec : Seq)
  492. scanSchedRW(WSRec, RWDefs, RWSet);
  493. }
  494. else if (RWDef->isSubClassOf("SchedVariant")) {
  495. // Visit each variant (guarded by a different predicate).
  496. RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
  497. for (Record *Variant : Vars) {
  498. // Visit each RW in the sequence selected by the current variant.
  499. RecVec Selected = Variant->getValueAsListOfDefs("Selected");
  500. for (Record *SelDef : Selected)
  501. scanSchedRW(SelDef, RWDefs, RWSet);
  502. }
  503. }
  504. }
  505. // Collect and sort all SchedReadWrites reachable via tablegen records.
  506. // More may be inferred later when inferring new SchedClasses from variants.
  507. void CodeGenSchedModels::collectSchedRW() {
  508. // Reserve idx=0 for invalid writes/reads.
  509. SchedWrites.resize(1);
  510. SchedReads.resize(1);
  511. SmallPtrSet<Record*, 16> RWSet;
  512. // Find all SchedReadWrites referenced by instruction defs.
  513. RecVec SWDefs, SRDefs;
  514. for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
  515. Record *SchedDef = Inst->TheDef;
  516. if (SchedDef->isValueUnset("SchedRW"))
  517. continue;
  518. RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
  519. for (Record *RW : RWs) {
  520. if (RW->isSubClassOf("SchedWrite"))
  521. scanSchedRW(RW, SWDefs, RWSet);
  522. else {
  523. assert(RW->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
  524. scanSchedRW(RW, SRDefs, RWSet);
  525. }
  526. }
  527. }
  528. // Find all ReadWrites referenced by InstRW.
  529. RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
  530. for (Record *InstRWDef : InstRWDefs) {
  531. // For all OperandReadWrites.
  532. RecVec RWDefs = InstRWDef->getValueAsListOfDefs("OperandReadWrites");
  533. for (Record *RWDef : RWDefs) {
  534. if (RWDef->isSubClassOf("SchedWrite"))
  535. scanSchedRW(RWDef, SWDefs, RWSet);
  536. else {
  537. assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
  538. scanSchedRW(RWDef, SRDefs, RWSet);
  539. }
  540. }
  541. }
  542. // Find all ReadWrites referenced by ItinRW.
  543. RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
  544. for (Record *ItinRWDef : ItinRWDefs) {
  545. // For all OperandReadWrites.
  546. RecVec RWDefs = ItinRWDef->getValueAsListOfDefs("OperandReadWrites");
  547. for (Record *RWDef : RWDefs) {
  548. if (RWDef->isSubClassOf("SchedWrite"))
  549. scanSchedRW(RWDef, SWDefs, RWSet);
  550. else {
  551. assert(RWDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
  552. scanSchedRW(RWDef, SRDefs, RWSet);
  553. }
  554. }
  555. }
  556. // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted
  557. // for the loop below that initializes Alias vectors.
  558. RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias");
  559. llvm::sort(AliasDefs, LessRecord());
  560. for (Record *ADef : AliasDefs) {
  561. Record *MatchDef = ADef->getValueAsDef("MatchRW");
  562. Record *AliasDef = ADef->getValueAsDef("AliasRW");
  563. if (MatchDef->isSubClassOf("SchedWrite")) {
  564. if (!AliasDef->isSubClassOf("SchedWrite"))
  565. PrintFatalError(ADef->getLoc(), "SchedWrite Alias must be SchedWrite");
  566. scanSchedRW(AliasDef, SWDefs, RWSet);
  567. }
  568. else {
  569. assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
  570. if (!AliasDef->isSubClassOf("SchedRead"))
  571. PrintFatalError(ADef->getLoc(), "SchedRead Alias must be SchedRead");
  572. scanSchedRW(AliasDef, SRDefs, RWSet);
  573. }
  574. }
  575. // Sort and add the SchedReadWrites directly referenced by instructions or
  576. // itinerary resources. Index reads and writes in separate domains.
  577. llvm::sort(SWDefs, LessRecord());
  578. for (Record *SWDef : SWDefs) {
  579. assert(!getSchedRWIdx(SWDef, /*IsRead=*/false) && "duplicate SchedWrite");
  580. SchedWrites.emplace_back(SchedWrites.size(), SWDef);
  581. }
  582. llvm::sort(SRDefs, LessRecord());
  583. for (Record *SRDef : SRDefs) {
  584. assert(!getSchedRWIdx(SRDef, /*IsRead-*/true) && "duplicate SchedWrite");
  585. SchedReads.emplace_back(SchedReads.size(), SRDef);
  586. }
  587. // Initialize WriteSequence vectors.
  588. for (CodeGenSchedRW &CGRW : SchedWrites) {
  589. if (!CGRW.IsSequence)
  590. continue;
  591. findRWs(CGRW.TheDef->getValueAsListOfDefs("Writes"), CGRW.Sequence,
  592. /*IsRead=*/false);
  593. }
  594. // Initialize Aliases vectors.
  595. for (Record *ADef : AliasDefs) {
  596. Record *AliasDef = ADef->getValueAsDef("AliasRW");
  597. getSchedRW(AliasDef).IsAlias = true;
  598. Record *MatchDef = ADef->getValueAsDef("MatchRW");
  599. CodeGenSchedRW &RW = getSchedRW(MatchDef);
  600. if (RW.IsAlias)
  601. PrintFatalError(ADef->getLoc(), "Cannot Alias an Alias");
  602. RW.Aliases.push_back(ADef);
  603. }
  604. LLVM_DEBUG(
  605. dbgs() << "\n+++ SCHED READS and WRITES (collectSchedRW) +++\n";
  606. for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
  607. dbgs() << WIdx << ": ";
  608. SchedWrites[WIdx].dump();
  609. dbgs() << '\n';
  610. } for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd;
  611. ++RIdx) {
  612. dbgs() << RIdx << ": ";
  613. SchedReads[RIdx].dump();
  614. dbgs() << '\n';
  615. } RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
  616. for (Record *RWDef
  617. : RWDefs) {
  618. if (!getSchedRWIdx(RWDef, RWDef->isSubClassOf("SchedRead"))) {
  619. StringRef Name = RWDef->getName();
  620. if (Name != "NoWrite" && Name != "ReadDefault")
  621. dbgs() << "Unused SchedReadWrite " << Name << '\n';
  622. }
  623. });
  624. }
  625. /// Compute a SchedWrite name from a sequence of writes.
  626. std::string CodeGenSchedModels::genRWName(ArrayRef<unsigned> Seq, bool IsRead) {
  627. std::string Name("(");
  628. ListSeparator LS("_");
  629. for (unsigned I : Seq) {
  630. Name += LS;
  631. Name += getSchedRW(I, IsRead).Name;
  632. }
  633. Name += ')';
  634. return Name;
  635. }
  636. unsigned CodeGenSchedModels::getSchedRWIdx(const Record *Def,
  637. bool IsRead) const {
  638. const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
  639. const auto I = find_if(
  640. RWVec, [Def](const CodeGenSchedRW &RW) { return RW.TheDef == Def; });
  641. return I == RWVec.end() ? 0 : std::distance(RWVec.begin(), I);
  642. }
  643. bool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const {
  644. for (const CodeGenSchedRW &Read : SchedReads) {
  645. Record *ReadDef = Read.TheDef;
  646. if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance"))
  647. continue;
  648. RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites");
  649. if (is_contained(ValidWrites, WriteDef)) {
  650. return true;
  651. }
  652. }
  653. return false;
  654. }
  655. static void splitSchedReadWrites(const RecVec &RWDefs,
  656. RecVec &WriteDefs, RecVec &ReadDefs) {
  657. for (Record *RWDef : RWDefs) {
  658. if (RWDef->isSubClassOf("SchedWrite"))
  659. WriteDefs.push_back(RWDef);
  660. else {
  661. assert(RWDef->isSubClassOf("SchedRead") && "unknown SchedReadWrite");
  662. ReadDefs.push_back(RWDef);
  663. }
  664. }
  665. }
  666. // Split the SchedReadWrites defs and call findRWs for each list.
  667. void CodeGenSchedModels::findRWs(const RecVec &RWDefs,
  668. IdxVec &Writes, IdxVec &Reads) const {
  669. RecVec WriteDefs;
  670. RecVec ReadDefs;
  671. splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
  672. findRWs(WriteDefs, Writes, false);
  673. findRWs(ReadDefs, Reads, true);
  674. }
  675. // Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
  676. void CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
  677. bool IsRead) const {
  678. for (Record *RWDef : RWDefs) {
  679. unsigned Idx = getSchedRWIdx(RWDef, IsRead);
  680. assert(Idx && "failed to collect SchedReadWrite");
  681. RWs.push_back(Idx);
  682. }
  683. }
  684. void CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
  685. bool IsRead) const {
  686. const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
  687. if (!SchedRW.IsSequence) {
  688. RWSeq.push_back(RWIdx);
  689. return;
  690. }
  691. int Repeat =
  692. SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
  693. for (int i = 0; i < Repeat; ++i) {
  694. for (unsigned I : SchedRW.Sequence) {
  695. expandRWSequence(I, RWSeq, IsRead);
  696. }
  697. }
  698. }
  699. // Expand a SchedWrite as a sequence following any aliases that coincide with
  700. // the given processor model.
  701. void CodeGenSchedModels::expandRWSeqForProc(
  702. unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
  703. const CodeGenProcModel &ProcModel) const {
  704. const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead);
  705. Record *AliasDef = nullptr;
  706. for (const Record *Rec : SchedWrite.Aliases) {
  707. const CodeGenSchedRW &AliasRW = getSchedRW(Rec->getValueAsDef("AliasRW"));
  708. if (Rec->getValueInit("SchedModel")->isComplete()) {
  709. Record *ModelDef = Rec->getValueAsDef("SchedModel");
  710. if (&getProcModel(ModelDef) != &ProcModel)
  711. continue;
  712. }
  713. if (AliasDef)
  714. PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
  715. "defined for processor " + ProcModel.ModelName +
  716. " Ensure only one SchedAlias exists per RW.");
  717. AliasDef = AliasRW.TheDef;
  718. }
  719. if (AliasDef) {
  720. expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead),
  721. RWSeq, IsRead,ProcModel);
  722. return;
  723. }
  724. if (!SchedWrite.IsSequence) {
  725. RWSeq.push_back(RWIdx);
  726. return;
  727. }
  728. int Repeat =
  729. SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1;
  730. for (int I = 0, E = Repeat; I < E; ++I) {
  731. for (unsigned Idx : SchedWrite.Sequence) {
  732. expandRWSeqForProc(Idx, RWSeq, IsRead, ProcModel);
  733. }
  734. }
  735. }
  736. // Find the existing SchedWrite that models this sequence of writes.
  737. unsigned CodeGenSchedModels::findRWForSequence(ArrayRef<unsigned> Seq,
  738. bool IsRead) {
  739. std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
  740. auto I = find_if(RWVec, [Seq](CodeGenSchedRW &RW) {
  741. return makeArrayRef(RW.Sequence) == Seq;
  742. });
  743. // Index zero reserved for invalid RW.
  744. return I == RWVec.end() ? 0 : std::distance(RWVec.begin(), I);
  745. }
  746. /// Add this ReadWrite if it doesn't already exist.
  747. unsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
  748. bool IsRead) {
  749. assert(!Seq.empty() && "cannot insert empty sequence");
  750. if (Seq.size() == 1)
  751. return Seq.back();
  752. unsigned Idx = findRWForSequence(Seq, IsRead);
  753. if (Idx)
  754. return Idx;
  755. std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
  756. unsigned RWIdx = RWVec.size();
  757. CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead));
  758. RWVec.push_back(SchedRW);
  759. return RWIdx;
  760. }
  761. /// Visit all the instruction definitions for this target to gather and
  762. /// enumerate the itinerary classes. These are the explicitly specified
  763. /// SchedClasses. More SchedClasses may be inferred.
  764. void CodeGenSchedModels::collectSchedClasses() {
  765. // NoItinerary is always the first class at Idx=0
  766. assert(SchedClasses.empty() && "Expected empty sched class");
  767. SchedClasses.emplace_back(0, "NoInstrModel",
  768. Records.getDef("NoItinerary"));
  769. SchedClasses.back().ProcIndices.push_back(0);
  770. // Create a SchedClass for each unique combination of itinerary class and
  771. // SchedRW list.
  772. for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
  773. Record *ItinDef = Inst->TheDef->getValueAsDef("Itinerary");
  774. IdxVec Writes, Reads;
  775. if (!Inst->TheDef->isValueUnset("SchedRW"))
  776. findRWs(Inst->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
  777. // ProcIdx == 0 indicates the class applies to all processors.
  778. unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, /*ProcIndices*/{0});
  779. InstrClassMap[Inst->TheDef] = SCIdx;
  780. }
  781. // Create classes for InstRW defs.
  782. RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
  783. llvm::sort(InstRWDefs, LessRecord());
  784. LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (createInstRWClass) +++\n");
  785. for (Record *RWDef : InstRWDefs)
  786. createInstRWClass(RWDef);
  787. NumInstrSchedClasses = SchedClasses.size();
  788. bool EnableDump = false;
  789. LLVM_DEBUG(EnableDump = true);
  790. if (!EnableDump)
  791. return;
  792. LLVM_DEBUG(
  793. dbgs()
  794. << "\n+++ ITINERARIES and/or MACHINE MODELS (collectSchedClasses) +++\n");
  795. for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
  796. StringRef InstName = Inst->TheDef->getName();
  797. unsigned SCIdx = getSchedClassIdx(*Inst);
  798. if (!SCIdx) {
  799. LLVM_DEBUG({
  800. if (!Inst->hasNoSchedulingInfo)
  801. dbgs() << "No machine model for " << Inst->TheDef->getName() << '\n';
  802. });
  803. continue;
  804. }
  805. CodeGenSchedClass &SC = getSchedClass(SCIdx);
  806. if (SC.ProcIndices[0] != 0)
  807. PrintFatalError(Inst->TheDef->getLoc(), "Instruction's sched class "
  808. "must not be subtarget specific.");
  809. IdxVec ProcIndices;
  810. if (SC.ItinClassDef->getName() != "NoItinerary") {
  811. ProcIndices.push_back(0);
  812. dbgs() << "Itinerary for " << InstName << ": "
  813. << SC.ItinClassDef->getName() << '\n';
  814. }
  815. if (!SC.Writes.empty()) {
  816. ProcIndices.push_back(0);
  817. LLVM_DEBUG({
  818. dbgs() << "SchedRW machine model for " << InstName;
  819. for (unsigned int Write : SC.Writes)
  820. dbgs() << " " << SchedWrites[Write].Name;
  821. for (unsigned int Read : SC.Reads)
  822. dbgs() << " " << SchedReads[Read].Name;
  823. dbgs() << '\n';
  824. });
  825. }
  826. const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
  827. for (Record *RWDef : RWDefs) {
  828. const CodeGenProcModel &ProcModel =
  829. getProcModel(RWDef->getValueAsDef("SchedModel"));
  830. ProcIndices.push_back(ProcModel.Index);
  831. LLVM_DEBUG(dbgs() << "InstRW on " << ProcModel.ModelName << " for "
  832. << InstName);
  833. IdxVec Writes;
  834. IdxVec Reads;
  835. findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"),
  836. Writes, Reads);
  837. LLVM_DEBUG({
  838. for (unsigned WIdx : Writes)
  839. dbgs() << " " << SchedWrites[WIdx].Name;
  840. for (unsigned RIdx : Reads)
  841. dbgs() << " " << SchedReads[RIdx].Name;
  842. dbgs() << '\n';
  843. });
  844. }
  845. // If ProcIndices contains zero, the class applies to all processors.
  846. LLVM_DEBUG({
  847. if (!llvm::is_contained(ProcIndices, 0)) {
  848. for (const CodeGenProcModel &PM : ProcModels) {
  849. if (!llvm::is_contained(ProcIndices, PM.Index))
  850. dbgs() << "No machine model for " << Inst->TheDef->getName()
  851. << " on processor " << PM.ModelName << '\n';
  852. }
  853. }
  854. });
  855. }
  856. }
  857. // Get the SchedClass index for an instruction.
  858. unsigned
  859. CodeGenSchedModels::getSchedClassIdx(const CodeGenInstruction &Inst) const {
  860. return InstrClassMap.lookup(Inst.TheDef);
  861. }
  862. std::string
  863. CodeGenSchedModels::createSchedClassName(Record *ItinClassDef,
  864. ArrayRef<unsigned> OperWrites,
  865. ArrayRef<unsigned> OperReads) {
  866. std::string Name;
  867. if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
  868. Name = std::string(ItinClassDef->getName());
  869. for (unsigned Idx : OperWrites) {
  870. if (!Name.empty())
  871. Name += '_';
  872. Name += SchedWrites[Idx].Name;
  873. }
  874. for (unsigned Idx : OperReads) {
  875. Name += '_';
  876. Name += SchedReads[Idx].Name;
  877. }
  878. return Name;
  879. }
  880. std::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
  881. std::string Name;
  882. ListSeparator LS("_");
  883. for (const Record *InstDef : InstDefs) {
  884. Name += LS;
  885. Name += InstDef->getName();
  886. }
  887. return Name;
  888. }
  889. /// Add an inferred sched class from an itinerary class and per-operand list of
  890. /// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
  891. /// processors that may utilize this class.
  892. unsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
  893. ArrayRef<unsigned> OperWrites,
  894. ArrayRef<unsigned> OperReads,
  895. ArrayRef<unsigned> ProcIndices) {
  896. assert(!ProcIndices.empty() && "expect at least one ProcIdx");
  897. auto IsKeyEqual = [=](const CodeGenSchedClass &SC) {
  898. return SC.isKeyEqual(ItinClassDef, OperWrites, OperReads);
  899. };
  900. auto I = find_if(make_range(schedClassBegin(), schedClassEnd()), IsKeyEqual);
  901. unsigned Idx = I == schedClassEnd() ? 0 : std::distance(schedClassBegin(), I);
  902. if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
  903. IdxVec PI;
  904. std::set_union(SchedClasses[Idx].ProcIndices.begin(),
  905. SchedClasses[Idx].ProcIndices.end(),
  906. ProcIndices.begin(), ProcIndices.end(),
  907. std::back_inserter(PI));
  908. SchedClasses[Idx].ProcIndices = std::move(PI);
  909. return Idx;
  910. }
  911. Idx = SchedClasses.size();
  912. SchedClasses.emplace_back(Idx,
  913. createSchedClassName(ItinClassDef, OperWrites,
  914. OperReads),
  915. ItinClassDef);
  916. CodeGenSchedClass &SC = SchedClasses.back();
  917. SC.Writes = OperWrites;
  918. SC.Reads = OperReads;
  919. SC.ProcIndices = ProcIndices;
  920. return Idx;
  921. }
  922. // Create classes for each set of opcodes that are in the same InstReadWrite
  923. // definition across all processors.
  924. void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
  925. // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
  926. // intersects with an existing class via a previous InstRWDef. Instrs that do
  927. // not intersect with an existing class refer back to their former class as
  928. // determined from ItinDef or SchedRW.
  929. SmallMapVector<unsigned, SmallVector<Record *, 8>, 4> ClassInstrs;
  930. // Sort Instrs into sets.
  931. const RecVec *InstDefs = Sets.expand(InstRWDef);
  932. if (InstDefs->empty())
  933. PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
  934. for (Record *InstDef : *InstDefs) {
  935. InstClassMapTy::const_iterator Pos = InstrClassMap.find(InstDef);
  936. if (Pos == InstrClassMap.end())
  937. PrintFatalError(InstDef->getLoc(), "No sched class for instruction.");
  938. unsigned SCIdx = Pos->second;
  939. ClassInstrs[SCIdx].push_back(InstDef);
  940. }
  941. // For each set of Instrs, create a new class if necessary, and map or remap
  942. // the Instrs to it.
  943. for (auto &Entry : ClassInstrs) {
  944. unsigned OldSCIdx = Entry.first;
  945. ArrayRef<Record*> InstDefs = Entry.second;
  946. // If the all instrs in the current class are accounted for, then leave
  947. // them mapped to their old class.
  948. if (OldSCIdx) {
  949. const RecVec &RWDefs = SchedClasses[OldSCIdx].InstRWs;
  950. if (!RWDefs.empty()) {
  951. const RecVec *OrigInstDefs = Sets.expand(RWDefs[0]);
  952. unsigned OrigNumInstrs =
  953. count_if(*OrigInstDefs, [&](Record *OIDef) {
  954. return InstrClassMap[OIDef] == OldSCIdx;
  955. });
  956. if (OrigNumInstrs == InstDefs.size()) {
  957. assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
  958. "expected a generic SchedClass");
  959. Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
  960. // Make sure we didn't already have a InstRW containing this
  961. // instruction on this model.
  962. for (Record *RWD : RWDefs) {
  963. if (RWD->getValueAsDef("SchedModel") == RWModelDef &&
  964. RWModelDef->getValueAsBit("FullInstRWOverlapCheck")) {
  965. assert(!InstDefs.empty()); // Checked at function start.
  966. PrintError(
  967. InstRWDef->getLoc(),
  968. "Overlapping InstRW definition for \"" +
  969. InstDefs.front()->getName() +
  970. "\" also matches previous \"" +
  971. RWD->getValue("Instrs")->getValue()->getAsString() +
  972. "\".");
  973. PrintFatalNote(RWD->getLoc(), "Previous match was here.");
  974. }
  975. }
  976. LLVM_DEBUG(dbgs() << "InstRW: Reuse SC " << OldSCIdx << ":"
  977. << SchedClasses[OldSCIdx].Name << " on "
  978. << RWModelDef->getName() << "\n");
  979. SchedClasses[OldSCIdx].InstRWs.push_back(InstRWDef);
  980. continue;
  981. }
  982. }
  983. }
  984. unsigned SCIdx = SchedClasses.size();
  985. SchedClasses.emplace_back(SCIdx, createSchedClassName(InstDefs), nullptr);
  986. CodeGenSchedClass &SC = SchedClasses.back();
  987. LLVM_DEBUG(dbgs() << "InstRW: New SC " << SCIdx << ":" << SC.Name << " on "
  988. << InstRWDef->getValueAsDef("SchedModel")->getName()
  989. << "\n");
  990. // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
  991. SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
  992. SC.Writes = SchedClasses[OldSCIdx].Writes;
  993. SC.Reads = SchedClasses[OldSCIdx].Reads;
  994. SC.ProcIndices.push_back(0);
  995. // If we had an old class, copy it's InstRWs to this new class.
  996. if (OldSCIdx) {
  997. Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
  998. for (Record *OldRWDef : SchedClasses[OldSCIdx].InstRWs) {
  999. if (OldRWDef->getValueAsDef("SchedModel") == RWModelDef) {
  1000. assert(!InstDefs.empty()); // Checked at function start.
  1001. PrintError(
  1002. InstRWDef->getLoc(),
  1003. "Overlapping InstRW definition for \"" +
  1004. InstDefs.front()->getName() + "\" also matches previous \"" +
  1005. OldRWDef->getValue("Instrs")->getValue()->getAsString() +
  1006. "\".");
  1007. PrintFatalNote(OldRWDef->getLoc(), "Previous match was here.");
  1008. }
  1009. assert(OldRWDef != InstRWDef &&
  1010. "SchedClass has duplicate InstRW def");
  1011. SC.InstRWs.push_back(OldRWDef);
  1012. }
  1013. }
  1014. // Map each Instr to this new class.
  1015. for (Record *InstDef : InstDefs)
  1016. InstrClassMap[InstDef] = SCIdx;
  1017. SC.InstRWs.push_back(InstRWDef);
  1018. }
  1019. }
  1020. // True if collectProcItins found anything.
  1021. bool CodeGenSchedModels::hasItineraries() const {
  1022. for (const CodeGenProcModel &PM : make_range(procModelBegin(),procModelEnd()))
  1023. if (PM.hasItineraries())
  1024. return true;
  1025. return false;
  1026. }
  1027. // Gather the processor itineraries.
  1028. void CodeGenSchedModels::collectProcItins() {
  1029. LLVM_DEBUG(dbgs() << "\n+++ PROBLEM ITINERARIES (collectProcItins) +++\n");
  1030. for (CodeGenProcModel &ProcModel : ProcModels) {
  1031. if (!ProcModel.hasItineraries())
  1032. continue;
  1033. RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
  1034. assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
  1035. // Populate ItinDefList with Itinerary records.
  1036. ProcModel.ItinDefList.resize(NumInstrSchedClasses);
  1037. // Insert each itinerary data record in the correct position within
  1038. // the processor model's ItinDefList.
  1039. for (Record *ItinData : ItinRecords) {
  1040. const Record *ItinDef = ItinData->getValueAsDef("TheClass");
  1041. bool FoundClass = false;
  1042. for (const CodeGenSchedClass &SC :
  1043. make_range(schedClassBegin(), schedClassEnd())) {
  1044. // Multiple SchedClasses may share an itinerary. Update all of them.
  1045. if (SC.ItinClassDef == ItinDef) {
  1046. ProcModel.ItinDefList[SC.Index] = ItinData;
  1047. FoundClass = true;
  1048. }
  1049. }
  1050. if (!FoundClass) {
  1051. LLVM_DEBUG(dbgs() << ProcModel.ItinsDef->getName()
  1052. << " missing class for itinerary "
  1053. << ItinDef->getName() << '\n');
  1054. }
  1055. }
  1056. // Check for missing itinerary entries.
  1057. assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
  1058. LLVM_DEBUG(
  1059. for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
  1060. if (!ProcModel.ItinDefList[i])
  1061. dbgs() << ProcModel.ItinsDef->getName()
  1062. << " missing itinerary for class " << SchedClasses[i].Name
  1063. << '\n';
  1064. });
  1065. }
  1066. }
  1067. // Gather the read/write types for each itinerary class.
  1068. void CodeGenSchedModels::collectProcItinRW() {
  1069. RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
  1070. llvm::sort(ItinRWDefs, LessRecord());
  1071. for (Record *RWDef : ItinRWDefs) {
  1072. if (!RWDef->getValueInit("SchedModel")->isComplete())
  1073. PrintFatalError(RWDef->getLoc(), "SchedModel is undefined");
  1074. Record *ModelDef = RWDef->getValueAsDef("SchedModel");
  1075. ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
  1076. if (I == ProcModelMap.end()) {
  1077. PrintFatalError(RWDef->getLoc(), "Undefined SchedMachineModel "
  1078. + ModelDef->getName());
  1079. }
  1080. ProcModels[I->second].ItinRWDefs.push_back(RWDef);
  1081. }
  1082. }
  1083. // Gather the unsupported features for processor models.
  1084. void CodeGenSchedModels::collectProcUnsupportedFeatures() {
  1085. for (CodeGenProcModel &ProcModel : ProcModels)
  1086. append_range(
  1087. ProcModel.UnsupportedFeaturesDefs,
  1088. ProcModel.ModelDef->getValueAsListOfDefs("UnsupportedFeatures"));
  1089. }
  1090. /// Infer new classes from existing classes. In the process, this may create new
  1091. /// SchedWrites from sequences of existing SchedWrites.
  1092. void CodeGenSchedModels::inferSchedClasses() {
  1093. LLVM_DEBUG(
  1094. dbgs() << "\n+++ INFERRING SCHED CLASSES (inferSchedClasses) +++\n");
  1095. LLVM_DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
  1096. // Visit all existing classes and newly created classes.
  1097. for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
  1098. assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
  1099. if (SchedClasses[Idx].ItinClassDef)
  1100. inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
  1101. if (!SchedClasses[Idx].InstRWs.empty())
  1102. inferFromInstRWs(Idx);
  1103. if (!SchedClasses[Idx].Writes.empty()) {
  1104. inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
  1105. Idx, SchedClasses[Idx].ProcIndices);
  1106. }
  1107. assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
  1108. "too many SchedVariants");
  1109. }
  1110. }
  1111. /// Infer classes from per-processor itinerary resources.
  1112. void CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
  1113. unsigned FromClassIdx) {
  1114. for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
  1115. const CodeGenProcModel &PM = ProcModels[PIdx];
  1116. // For all ItinRW entries.
  1117. bool HasMatch = false;
  1118. for (const Record *Rec : PM.ItinRWDefs) {
  1119. RecVec Matched = Rec->getValueAsListOfDefs("MatchedItinClasses");
  1120. if (!llvm::is_contained(Matched, ItinClassDef))
  1121. continue;
  1122. if (HasMatch)
  1123. PrintFatalError(Rec->getLoc(), "Duplicate itinerary class "
  1124. + ItinClassDef->getName()
  1125. + " in ItinResources for " + PM.ModelName);
  1126. HasMatch = true;
  1127. IdxVec Writes, Reads;
  1128. findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
  1129. inferFromRW(Writes, Reads, FromClassIdx, PIdx);
  1130. }
  1131. }
  1132. }
  1133. /// Infer classes from per-processor InstReadWrite definitions.
  1134. void CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
  1135. for (unsigned I = 0, E = SchedClasses[SCIdx].InstRWs.size(); I != E; ++I) {
  1136. assert(SchedClasses[SCIdx].InstRWs.size() == E && "InstrRWs was mutated!");
  1137. Record *Rec = SchedClasses[SCIdx].InstRWs[I];
  1138. const RecVec *InstDefs = Sets.expand(Rec);
  1139. RecIter II = InstDefs->begin(), IE = InstDefs->end();
  1140. for (; II != IE; ++II) {
  1141. if (InstrClassMap[*II] == SCIdx)
  1142. break;
  1143. }
  1144. // If this class no longer has any instructions mapped to it, it has become
  1145. // irrelevant.
  1146. if (II == IE)
  1147. continue;
  1148. IdxVec Writes, Reads;
  1149. findRWs(Rec->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
  1150. unsigned PIdx = getProcModel(Rec->getValueAsDef("SchedModel")).Index;
  1151. inferFromRW(Writes, Reads, SCIdx, PIdx); // May mutate SchedClasses.
  1152. SchedClasses[SCIdx].InstRWProcIndices.insert(PIdx);
  1153. }
  1154. }
  1155. namespace {
  1156. // Helper for substituteVariantOperand.
  1157. struct TransVariant {
  1158. Record *VarOrSeqDef; // Variant or sequence.
  1159. unsigned RWIdx; // Index of this variant or sequence's matched type.
  1160. unsigned ProcIdx; // Processor model index or zero for any.
  1161. unsigned TransVecIdx; // Index into PredTransitions::TransVec.
  1162. TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
  1163. VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
  1164. };
  1165. // Associate a predicate with the SchedReadWrite that it guards.
  1166. // RWIdx is the index of the read/write variant.
  1167. struct PredCheck {
  1168. bool IsRead;
  1169. unsigned RWIdx;
  1170. Record *Predicate;
  1171. PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
  1172. };
  1173. // A Predicate transition is a list of RW sequences guarded by a PredTerm.
  1174. struct PredTransition {
  1175. // A predicate term is a conjunction of PredChecks.
  1176. SmallVector<PredCheck, 4> PredTerm;
  1177. SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
  1178. SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
  1179. unsigned ProcIndex = 0;
  1180. PredTransition() = default;
  1181. PredTransition(ArrayRef<PredCheck> PT, unsigned ProcId) {
  1182. PredTerm.assign(PT.begin(), PT.end());
  1183. ProcIndex = ProcId;
  1184. }
  1185. };
  1186. // Encapsulate a set of partially constructed transitions.
  1187. // The results are built by repeated calls to substituteVariants.
  1188. class PredTransitions {
  1189. CodeGenSchedModels &SchedModels;
  1190. public:
  1191. std::vector<PredTransition> TransVec;
  1192. PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
  1193. bool substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
  1194. bool IsRead, unsigned StartIdx);
  1195. bool substituteVariants(const PredTransition &Trans);
  1196. #ifndef NDEBUG
  1197. void dump() const;
  1198. #endif
  1199. private:
  1200. bool mutuallyExclusive(Record *PredDef, ArrayRef<Record *> Preds,
  1201. ArrayRef<PredCheck> Term);
  1202. void getIntersectingVariants(
  1203. const CodeGenSchedRW &SchedRW, unsigned TransIdx,
  1204. std::vector<TransVariant> &IntersectingVariants);
  1205. void pushVariant(const TransVariant &VInfo, bool IsRead);
  1206. };
  1207. } // end anonymous namespace
  1208. // Return true if this predicate is mutually exclusive with a PredTerm. This
  1209. // degenerates into checking if the predicate is mutually exclusive with any
  1210. // predicate in the Term's conjunction.
  1211. //
  1212. // All predicates associated with a given SchedRW are considered mutually
  1213. // exclusive. This should work even if the conditions expressed by the
  1214. // predicates are not exclusive because the predicates for a given SchedWrite
  1215. // are always checked in the order they are defined in the .td file. Later
  1216. // conditions implicitly negate any prior condition.
  1217. bool PredTransitions::mutuallyExclusive(Record *PredDef,
  1218. ArrayRef<Record *> Preds,
  1219. ArrayRef<PredCheck> Term) {
  1220. for (const PredCheck &PC: Term) {
  1221. if (PC.Predicate == PredDef)
  1222. return false;
  1223. const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(PC.RWIdx, PC.IsRead);
  1224. assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
  1225. RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
  1226. if (any_of(Variants, [PredDef](const Record *R) {
  1227. return R->getValueAsDef("Predicate") == PredDef;
  1228. })) {
  1229. // To check if PredDef is mutually exclusive with PC we also need to
  1230. // check that PC.Predicate is exclusive with all predicates from variant
  1231. // we're expanding. Consider following RW sequence with two variants
  1232. // (1 & 2), where A, B and C are predicates from corresponding SchedVars:
  1233. //
  1234. // 1:A/B - 2:C/B
  1235. //
  1236. // Here C is not mutually exclusive with variant (1), because A doesn't
  1237. // exist in variant (2). This means we have possible transitions from A
  1238. // to C and from A to B, and fully expanded sequence would look like:
  1239. //
  1240. // if (A & C) return ...;
  1241. // if (A & B) return ...;
  1242. // if (B) return ...;
  1243. //
  1244. // Now let's consider another sequence:
  1245. //
  1246. // 1:A/B - 2:A/B
  1247. //
  1248. // Here A in variant (2) is mutually exclusive with variant (1), because
  1249. // A also exists in (2). This means A->B transition is impossible and
  1250. // expanded sequence would look like:
  1251. //
  1252. // if (A) return ...;
  1253. // if (B) return ...;
  1254. if (!llvm::is_contained(Preds, PC.Predicate))
  1255. continue;
  1256. return true;
  1257. }
  1258. }
  1259. return false;
  1260. }
  1261. static std::vector<Record *> getAllPredicates(ArrayRef<TransVariant> Variants,
  1262. unsigned ProcId) {
  1263. std::vector<Record *> Preds;
  1264. for (auto &Variant : Variants) {
  1265. if (!Variant.VarOrSeqDef->isSubClassOf("SchedVar"))
  1266. continue;
  1267. Preds.push_back(Variant.VarOrSeqDef->getValueAsDef("Predicate"));
  1268. }
  1269. return Preds;
  1270. }
  1271. // Populate IntersectingVariants with any variants or aliased sequences of the
  1272. // given SchedRW whose processor indices and predicates are not mutually
  1273. // exclusive with the given transition.
  1274. void PredTransitions::getIntersectingVariants(
  1275. const CodeGenSchedRW &SchedRW, unsigned TransIdx,
  1276. std::vector<TransVariant> &IntersectingVariants) {
  1277. bool GenericRW = false;
  1278. std::vector<TransVariant> Variants;
  1279. if (SchedRW.HasVariants) {
  1280. unsigned VarProcIdx = 0;
  1281. if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
  1282. Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
  1283. VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
  1284. }
  1285. if (VarProcIdx == 0 || VarProcIdx == TransVec[TransIdx].ProcIndex) {
  1286. // Push each variant. Assign TransVecIdx later.
  1287. const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
  1288. for (Record *VarDef : VarDefs)
  1289. Variants.emplace_back(VarDef, SchedRW.Index, VarProcIdx, 0);
  1290. if (VarProcIdx == 0)
  1291. GenericRW = true;
  1292. }
  1293. }
  1294. for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
  1295. AI != AE; ++AI) {
  1296. // If either the SchedAlias itself or the SchedReadWrite that it aliases
  1297. // to is defined within a processor model, constrain all variants to
  1298. // that processor.
  1299. unsigned AliasProcIdx = 0;
  1300. if ((*AI)->getValueInit("SchedModel")->isComplete()) {
  1301. Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
  1302. AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
  1303. }
  1304. if (AliasProcIdx && AliasProcIdx != TransVec[TransIdx].ProcIndex)
  1305. continue;
  1306. if (!Variants.empty()) {
  1307. const CodeGenProcModel &PM =
  1308. *(SchedModels.procModelBegin() + AliasProcIdx);
  1309. PrintFatalError((*AI)->getLoc(),
  1310. "Multiple variants defined for processor " +
  1311. PM.ModelName +
  1312. " Ensure only one SchedAlias exists per RW.");
  1313. }
  1314. const CodeGenSchedRW &AliasRW =
  1315. SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
  1316. if (AliasRW.HasVariants) {
  1317. const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
  1318. for (Record *VD : VarDefs)
  1319. Variants.emplace_back(VD, AliasRW.Index, AliasProcIdx, 0);
  1320. }
  1321. if (AliasRW.IsSequence)
  1322. Variants.emplace_back(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0);
  1323. if (AliasProcIdx == 0)
  1324. GenericRW = true;
  1325. }
  1326. std::vector<Record *> AllPreds =
  1327. getAllPredicates(Variants, TransVec[TransIdx].ProcIndex);
  1328. for (TransVariant &Variant : Variants) {
  1329. // Don't expand variants if the processor models don't intersect.
  1330. // A zero processor index means any processor.
  1331. if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
  1332. Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
  1333. if (mutuallyExclusive(PredDef, AllPreds, TransVec[TransIdx].PredTerm))
  1334. continue;
  1335. }
  1336. if (IntersectingVariants.empty()) {
  1337. // The first variant builds on the existing transition.
  1338. Variant.TransVecIdx = TransIdx;
  1339. IntersectingVariants.push_back(Variant);
  1340. }
  1341. else {
  1342. // Push another copy of the current transition for more variants.
  1343. Variant.TransVecIdx = TransVec.size();
  1344. IntersectingVariants.push_back(Variant);
  1345. TransVec.push_back(TransVec[TransIdx]);
  1346. }
  1347. }
  1348. if (GenericRW && IntersectingVariants.empty()) {
  1349. PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
  1350. "a matching predicate on any processor");
  1351. }
  1352. }
  1353. // Push the Reads/Writes selected by this variant onto the PredTransition
  1354. // specified by VInfo.
  1355. void PredTransitions::
  1356. pushVariant(const TransVariant &VInfo, bool IsRead) {
  1357. PredTransition &Trans = TransVec[VInfo.TransVecIdx];
  1358. // If this operand transition is reached through a processor-specific alias,
  1359. // then the whole transition is specific to this processor.
  1360. IdxVec SelectedRWs;
  1361. if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
  1362. Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
  1363. Trans.PredTerm.emplace_back(IsRead, VInfo.RWIdx,PredDef);
  1364. RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
  1365. SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
  1366. }
  1367. else {
  1368. assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
  1369. "variant must be a SchedVariant or aliased WriteSequence");
  1370. SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
  1371. }
  1372. const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
  1373. SmallVectorImpl<SmallVector<unsigned,4>> &RWSequences = IsRead
  1374. ? Trans.ReadSequences : Trans.WriteSequences;
  1375. if (SchedRW.IsVariadic) {
  1376. unsigned OperIdx = RWSequences.size()-1;
  1377. // Make N-1 copies of this transition's last sequence.
  1378. RWSequences.reserve(RWSequences.size() + SelectedRWs.size() - 1);
  1379. RWSequences.insert(RWSequences.end(), SelectedRWs.size() - 1,
  1380. RWSequences[OperIdx]);
  1381. // Push each of the N elements of the SelectedRWs onto a copy of the last
  1382. // sequence (split the current operand into N operands).
  1383. // Note that write sequences should be expanded within this loop--the entire
  1384. // sequence belongs to a single operand.
  1385. for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
  1386. RWI != RWE; ++RWI, ++OperIdx) {
  1387. IdxVec ExpandedRWs;
  1388. if (IsRead)
  1389. ExpandedRWs.push_back(*RWI);
  1390. else
  1391. SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
  1392. llvm::append_range(RWSequences[OperIdx], ExpandedRWs);
  1393. }
  1394. assert(OperIdx == RWSequences.size() && "missed a sequence");
  1395. }
  1396. else {
  1397. // Push this transition's expanded sequence onto this transition's last
  1398. // sequence (add to the current operand's sequence).
  1399. SmallVectorImpl<unsigned> &Seq = RWSequences.back();
  1400. IdxVec ExpandedRWs;
  1401. for (unsigned int SelectedRW : SelectedRWs) {
  1402. if (IsRead)
  1403. ExpandedRWs.push_back(SelectedRW);
  1404. else
  1405. SchedModels.expandRWSequence(SelectedRW, ExpandedRWs, IsRead);
  1406. }
  1407. llvm::append_range(Seq, ExpandedRWs);
  1408. }
  1409. }
  1410. // RWSeq is a sequence of all Reads or all Writes for the next read or write
  1411. // operand. StartIdx is an index into TransVec where partial results
  1412. // starts. RWSeq must be applied to all transitions between StartIdx and the end
  1413. // of TransVec.
  1414. bool PredTransitions::substituteVariantOperand(
  1415. const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
  1416. bool Subst = false;
  1417. // Visit each original RW within the current sequence.
  1418. for (unsigned int RWI : RWSeq) {
  1419. const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(RWI, IsRead);
  1420. // Push this RW on all partial PredTransitions or distribute variants.
  1421. // New PredTransitions may be pushed within this loop which should not be
  1422. // revisited (TransEnd must be loop invariant).
  1423. for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
  1424. TransIdx != TransEnd; ++TransIdx) {
  1425. // Distribute this partial PredTransition across intersecting variants.
  1426. // This will push a copies of TransVec[TransIdx] on the back of TransVec.
  1427. std::vector<TransVariant> IntersectingVariants;
  1428. getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
  1429. // Now expand each variant on top of its copy of the transition.
  1430. for (const TransVariant &IV : IntersectingVariants)
  1431. pushVariant(IV, IsRead);
  1432. if (IntersectingVariants.empty()) {
  1433. if (IsRead)
  1434. TransVec[TransIdx].ReadSequences.back().push_back(RWI);
  1435. else
  1436. TransVec[TransIdx].WriteSequences.back().push_back(RWI);
  1437. continue;
  1438. } else {
  1439. Subst = true;
  1440. }
  1441. }
  1442. }
  1443. return Subst;
  1444. }
  1445. // For each variant of a Read/Write in Trans, substitute the sequence of
  1446. // Read/Writes guarded by the variant. This is exponential in the number of
  1447. // variant Read/Writes, but in practice detection of mutually exclusive
  1448. // predicates should result in linear growth in the total number variants.
  1449. //
  1450. // This is one step in a breadth-first search of nested variants.
  1451. bool PredTransitions::substituteVariants(const PredTransition &Trans) {
  1452. // Build up a set of partial results starting at the back of
  1453. // PredTransitions. Remember the first new transition.
  1454. unsigned StartIdx = TransVec.size();
  1455. bool Subst = false;
  1456. assert(Trans.ProcIndex != 0);
  1457. TransVec.emplace_back(Trans.PredTerm, Trans.ProcIndex);
  1458. // Visit each original write sequence.
  1459. for (const auto &WriteSequence : Trans.WriteSequences) {
  1460. // Push a new (empty) write sequence onto all partial Transitions.
  1461. for (std::vector<PredTransition>::iterator I =
  1462. TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
  1463. I->WriteSequences.emplace_back();
  1464. }
  1465. Subst |=
  1466. substituteVariantOperand(WriteSequence, /*IsRead=*/false, StartIdx);
  1467. }
  1468. // Visit each original read sequence.
  1469. for (const auto &ReadSequence : Trans.ReadSequences) {
  1470. // Push a new (empty) read sequence onto all partial Transitions.
  1471. for (std::vector<PredTransition>::iterator I =
  1472. TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
  1473. I->ReadSequences.emplace_back();
  1474. }
  1475. Subst |= substituteVariantOperand(ReadSequence, /*IsRead=*/true, StartIdx);
  1476. }
  1477. return Subst;
  1478. }
  1479. static void addSequences(CodeGenSchedModels &SchedModels,
  1480. const SmallVectorImpl<SmallVector<unsigned, 4>> &Seqs,
  1481. IdxVec &Result, bool IsRead) {
  1482. for (const auto &S : Seqs)
  1483. if (!S.empty())
  1484. Result.push_back(SchedModels.findOrInsertRW(S, IsRead));
  1485. }
  1486. #ifndef NDEBUG
  1487. static void dumpRecVec(const RecVec &RV) {
  1488. for (const Record *R : RV)
  1489. dbgs() << R->getName() << ", ";
  1490. }
  1491. #endif
  1492. static void dumpTransition(const CodeGenSchedModels &SchedModels,
  1493. const CodeGenSchedClass &FromSC,
  1494. const CodeGenSchedTransition &SCTrans,
  1495. const RecVec &Preds) {
  1496. LLVM_DEBUG(dbgs() << "Adding transition from " << FromSC.Name << "("
  1497. << FromSC.Index << ") to "
  1498. << SchedModels.getSchedClass(SCTrans.ToClassIdx).Name << "("
  1499. << SCTrans.ToClassIdx << ") on pred term: (";
  1500. dumpRecVec(Preds);
  1501. dbgs() << ") on processor (" << SCTrans.ProcIndex << ")\n");
  1502. }
  1503. // Create a new SchedClass for each variant found by inferFromRW. Pass
  1504. static void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
  1505. unsigned FromClassIdx,
  1506. CodeGenSchedModels &SchedModels) {
  1507. // For each PredTransition, create a new CodeGenSchedTransition, which usually
  1508. // requires creating a new SchedClass.
  1509. for (const auto &LastTransition : LastTransitions) {
  1510. // Variant expansion (substituteVariants) may create unconditional
  1511. // transitions. We don't need to build sched classes for them.
  1512. if (LastTransition.PredTerm.empty())
  1513. continue;
  1514. IdxVec OperWritesVariant, OperReadsVariant;
  1515. addSequences(SchedModels, LastTransition.WriteSequences, OperWritesVariant,
  1516. false);
  1517. addSequences(SchedModels, LastTransition.ReadSequences, OperReadsVariant,
  1518. true);
  1519. CodeGenSchedTransition SCTrans;
  1520. // Transition should not contain processor indices already assigned to
  1521. // InstRWs in this scheduling class.
  1522. const CodeGenSchedClass &FromSC = SchedModels.getSchedClass(FromClassIdx);
  1523. if (FromSC.InstRWProcIndices.count(LastTransition.ProcIndex))
  1524. continue;
  1525. SCTrans.ProcIndex = LastTransition.ProcIndex;
  1526. SCTrans.ToClassIdx =
  1527. SchedModels.addSchedClass(/*ItinClassDef=*/nullptr, OperWritesVariant,
  1528. OperReadsVariant, LastTransition.ProcIndex);
  1529. // The final PredTerm is unique set of predicates guarding the transition.
  1530. RecVec Preds;
  1531. transform(LastTransition.PredTerm, std::back_inserter(Preds),
  1532. [](const PredCheck &P) { return P.Predicate; });
  1533. Preds.erase(std::unique(Preds.begin(), Preds.end()), Preds.end());
  1534. dumpTransition(SchedModels, FromSC, SCTrans, Preds);
  1535. SCTrans.PredTerm = std::move(Preds);
  1536. SchedModels.getSchedClass(FromClassIdx)
  1537. .Transitions.push_back(std::move(SCTrans));
  1538. }
  1539. }
  1540. std::vector<unsigned> CodeGenSchedModels::getAllProcIndices() const {
  1541. std::vector<unsigned> ProcIdVec;
  1542. for (const auto &PM : ProcModelMap)
  1543. if (PM.second != 0)
  1544. ProcIdVec.push_back(PM.second);
  1545. // The order of the keys (Record pointers) of ProcModelMap are not stable.
  1546. // Sort to stabalize the values.
  1547. llvm::sort(ProcIdVec);
  1548. return ProcIdVec;
  1549. }
  1550. static std::vector<PredTransition>
  1551. makePerProcessorTransitions(const PredTransition &Trans,
  1552. ArrayRef<unsigned> ProcIndices) {
  1553. std::vector<PredTransition> PerCpuTransVec;
  1554. for (unsigned ProcId : ProcIndices) {
  1555. assert(ProcId != 0);
  1556. PerCpuTransVec.push_back(Trans);
  1557. PerCpuTransVec.back().ProcIndex = ProcId;
  1558. }
  1559. return PerCpuTransVec;
  1560. }
  1561. // Create new SchedClasses for the given ReadWrite list. If any of the
  1562. // ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
  1563. // of the ReadWrite list, following Aliases if necessary.
  1564. void CodeGenSchedModels::inferFromRW(ArrayRef<unsigned> OperWrites,
  1565. ArrayRef<unsigned> OperReads,
  1566. unsigned FromClassIdx,
  1567. ArrayRef<unsigned> ProcIndices) {
  1568. LLVM_DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices);
  1569. dbgs() << ") ");
  1570. // Create a seed transition with an empty PredTerm and the expanded sequences
  1571. // of SchedWrites for the current SchedClass.
  1572. std::vector<PredTransition> LastTransitions;
  1573. LastTransitions.emplace_back();
  1574. for (unsigned WriteIdx : OperWrites) {
  1575. IdxVec WriteSeq;
  1576. expandRWSequence(WriteIdx, WriteSeq, /*IsRead=*/false);
  1577. LastTransitions[0].WriteSequences.emplace_back();
  1578. SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences.back();
  1579. Seq.append(WriteSeq.begin(), WriteSeq.end());
  1580. LLVM_DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
  1581. }
  1582. LLVM_DEBUG(dbgs() << " Reads: ");
  1583. for (unsigned ReadIdx : OperReads) {
  1584. IdxVec ReadSeq;
  1585. expandRWSequence(ReadIdx, ReadSeq, /*IsRead=*/true);
  1586. LastTransitions[0].ReadSequences.emplace_back();
  1587. SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences.back();
  1588. Seq.append(ReadSeq.begin(), ReadSeq.end());
  1589. LLVM_DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
  1590. }
  1591. LLVM_DEBUG(dbgs() << '\n');
  1592. LastTransitions = makePerProcessorTransitions(
  1593. LastTransitions[0], llvm::is_contained(ProcIndices, 0)
  1594. ? ArrayRef<unsigned>(getAllProcIndices())
  1595. : ProcIndices);
  1596. // Collect all PredTransitions for individual operands.
  1597. // Iterate until no variant writes remain.
  1598. bool SubstitutedAny;
  1599. do {
  1600. SubstitutedAny = false;
  1601. PredTransitions Transitions(*this);
  1602. for (const PredTransition &Trans : LastTransitions)
  1603. SubstitutedAny |= Transitions.substituteVariants(Trans);
  1604. LLVM_DEBUG(Transitions.dump());
  1605. LastTransitions.swap(Transitions.TransVec);
  1606. } while (SubstitutedAny);
  1607. // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
  1608. // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
  1609. inferFromTransitions(LastTransitions, FromClassIdx, *this);
  1610. }
  1611. // Check if any processor resource group contains all resource records in
  1612. // SubUnits.
  1613. bool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
  1614. for (Record *ProcResourceDef : PM.ProcResourceDefs) {
  1615. if (!ProcResourceDef->isSubClassOf("ProcResGroup"))
  1616. continue;
  1617. RecVec SuperUnits = ProcResourceDef->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(); dbgs() << "WriteResDefs: "; for (auto WriteResDef
  1763. : PM.WriteResDefs) {
  1764. if (WriteResDef->isSubClassOf("WriteRes"))
  1765. dbgs() << WriteResDef->getValueAsDef("WriteType")->getName() << " ";
  1766. else
  1767. dbgs() << WriteResDef->getName() << " ";
  1768. } dbgs() << "\nReadAdvanceDefs: ";
  1769. for (Record *ReadAdvanceDef
  1770. : PM.ReadAdvanceDefs) {
  1771. if (ReadAdvanceDef->isSubClassOf("ReadAdvance"))
  1772. dbgs() << ReadAdvanceDef->getValueAsDef("ReadType")->getName()
  1773. << " ";
  1774. else
  1775. dbgs() << ReadAdvanceDef->getName() << " ";
  1776. } dbgs()
  1777. << "\nProcResourceDefs: ";
  1778. for (Record *ProcResourceDef
  1779. : PM.ProcResourceDefs) {
  1780. dbgs() << ProcResourceDef->getName() << " ";
  1781. } dbgs()
  1782. << '\n');
  1783. verifyProcResourceGroups(PM);
  1784. }
  1785. ProcResourceDefs.clear();
  1786. ProcResGroups.clear();
  1787. }
  1788. void CodeGenSchedModels::checkCompleteness() {
  1789. bool Complete = true;
  1790. for (const CodeGenProcModel &ProcModel : procModels()) {
  1791. const bool HasItineraries = ProcModel.hasItineraries();
  1792. if (!ProcModel.ModelDef->getValueAsBit("CompleteModel"))
  1793. continue;
  1794. for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
  1795. if (Inst->hasNoSchedulingInfo)
  1796. continue;
  1797. if (ProcModel.isUnsupported(*Inst))
  1798. continue;
  1799. unsigned SCIdx = getSchedClassIdx(*Inst);
  1800. if (!SCIdx) {
  1801. if (Inst->TheDef->isValueUnset("SchedRW")) {
  1802. PrintError(Inst->TheDef->getLoc(),
  1803. "No schedule information for instruction '" +
  1804. Inst->TheDef->getName() + "' in SchedMachineModel '" +
  1805. ProcModel.ModelDef->getName() + "'");
  1806. Complete = false;
  1807. }
  1808. continue;
  1809. }
  1810. const CodeGenSchedClass &SC = getSchedClass(SCIdx);
  1811. if (!SC.Writes.empty())
  1812. continue;
  1813. if (HasItineraries && SC.ItinClassDef != nullptr &&
  1814. SC.ItinClassDef->getName() != "NoItinerary")
  1815. continue;
  1816. const RecVec &InstRWs = SC.InstRWs;
  1817. auto I = find_if(InstRWs, [&ProcModel](const Record *R) {
  1818. return R->getValueAsDef("SchedModel") == ProcModel.ModelDef;
  1819. });
  1820. if (I == InstRWs.end()) {
  1821. PrintError(Inst->TheDef->getLoc(), "'" + ProcModel.ModelName +
  1822. "' lacks information for '" +
  1823. Inst->TheDef->getName() + "'");
  1824. Complete = false;
  1825. }
  1826. }
  1827. }
  1828. if (!Complete) {
  1829. errs() << "\n\nIncomplete schedule models found.\n"
  1830. << "- Consider setting 'CompleteModel = 0' while developing new models.\n"
  1831. << "- Pseudo instructions can be marked with 'hasNoSchedulingInfo = 1'.\n"
  1832. << "- Instructions should usually have Sched<[...]> as a superclass, "
  1833. "you may temporarily use an empty list.\n"
  1834. << "- Instructions related to unsupported features can be excluded with "
  1835. "list<Predicate> UnsupportedFeatures = [HasA,..,HasY]; in the "
  1836. "processor model.\n\n";
  1837. PrintFatalError("Incomplete schedule model");
  1838. }
  1839. }
  1840. // Collect itinerary class resources for each processor.
  1841. void CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
  1842. for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
  1843. const CodeGenProcModel &PM = ProcModels[PIdx];
  1844. // For all ItinRW entries.
  1845. bool HasMatch = false;
  1846. for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
  1847. II != IE; ++II) {
  1848. RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
  1849. if (!llvm::is_contained(Matched, ItinClassDef))
  1850. continue;
  1851. if (HasMatch)
  1852. PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
  1853. + ItinClassDef->getName()
  1854. + " in ItinResources for " + PM.ModelName);
  1855. HasMatch = true;
  1856. IdxVec Writes, Reads;
  1857. findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
  1858. collectRWResources(Writes, Reads, PIdx);
  1859. }
  1860. }
  1861. }
  1862. void CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
  1863. ArrayRef<unsigned> ProcIndices) {
  1864. const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
  1865. if (SchedRW.TheDef) {
  1866. if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
  1867. for (unsigned Idx : ProcIndices)
  1868. addWriteRes(SchedRW.TheDef, Idx);
  1869. }
  1870. else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
  1871. for (unsigned Idx : ProcIndices)
  1872. addReadAdvance(SchedRW.TheDef, Idx);
  1873. }
  1874. }
  1875. for (auto *Alias : SchedRW.Aliases) {
  1876. IdxVec AliasProcIndices;
  1877. if (Alias->getValueInit("SchedModel")->isComplete()) {
  1878. AliasProcIndices.push_back(
  1879. getProcModel(Alias->getValueAsDef("SchedModel")).Index);
  1880. } else
  1881. AliasProcIndices = ProcIndices;
  1882. const CodeGenSchedRW &AliasRW = getSchedRW(Alias->getValueAsDef("AliasRW"));
  1883. assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
  1884. IdxVec ExpandedRWs;
  1885. expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
  1886. for (unsigned int ExpandedRW : ExpandedRWs) {
  1887. collectRWResources(ExpandedRW, IsRead, AliasProcIndices);
  1888. }
  1889. }
  1890. }
  1891. // Collect resources for a set of read/write types and processor indices.
  1892. void CodeGenSchedModels::collectRWResources(ArrayRef<unsigned> Writes,
  1893. ArrayRef<unsigned> Reads,
  1894. ArrayRef<unsigned> ProcIndices) {
  1895. for (unsigned Idx : Writes)
  1896. collectRWResources(Idx, /*IsRead=*/false, ProcIndices);
  1897. for (unsigned Idx : Reads)
  1898. collectRWResources(Idx, /*IsRead=*/true, ProcIndices);
  1899. }
  1900. // Find the processor's resource units for this kind of resource.
  1901. Record *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
  1902. const CodeGenProcModel &PM,
  1903. ArrayRef<SMLoc> Loc) const {
  1904. if (ProcResKind->isSubClassOf("ProcResourceUnits"))
  1905. return ProcResKind;
  1906. Record *ProcUnitDef = nullptr;
  1907. assert(!ProcResourceDefs.empty());
  1908. assert(!ProcResGroups.empty());
  1909. for (Record *ProcResDef : ProcResourceDefs) {
  1910. if (ProcResDef->getValueAsDef("Kind") == ProcResKind
  1911. && ProcResDef->getValueAsDef("SchedModel") == PM.ModelDef) {
  1912. if (ProcUnitDef) {
  1913. PrintFatalError(Loc,
  1914. "Multiple ProcessorResourceUnits associated with "
  1915. + ProcResKind->getName());
  1916. }
  1917. ProcUnitDef = ProcResDef;
  1918. }
  1919. }
  1920. for (Record *ProcResGroup : ProcResGroups) {
  1921. if (ProcResGroup == ProcResKind
  1922. && ProcResGroup->getValueAsDef("SchedModel") == PM.ModelDef) {
  1923. if (ProcUnitDef) {
  1924. PrintFatalError(Loc,
  1925. "Multiple ProcessorResourceUnits associated with "
  1926. + ProcResKind->getName());
  1927. }
  1928. ProcUnitDef = ProcResGroup;
  1929. }
  1930. }
  1931. if (!ProcUnitDef) {
  1932. PrintFatalError(Loc,
  1933. "No ProcessorResources associated with "
  1934. + ProcResKind->getName());
  1935. }
  1936. return ProcUnitDef;
  1937. }
  1938. // Iteratively add a resource and its super resources.
  1939. void CodeGenSchedModels::addProcResource(Record *ProcResKind,
  1940. CodeGenProcModel &PM,
  1941. ArrayRef<SMLoc> Loc) {
  1942. while (true) {
  1943. Record *ProcResUnits = findProcResUnits(ProcResKind, PM, Loc);
  1944. // See if this ProcResource is already associated with this processor.
  1945. if (is_contained(PM.ProcResourceDefs, ProcResUnits))
  1946. return;
  1947. PM.ProcResourceDefs.push_back(ProcResUnits);
  1948. if (ProcResUnits->isSubClassOf("ProcResGroup"))
  1949. return;
  1950. if (!ProcResUnits->getValueInit("Super")->isComplete())
  1951. return;
  1952. ProcResKind = ProcResUnits->getValueAsDef("Super");
  1953. }
  1954. }
  1955. // Add resources for a SchedWrite to this processor if they don't exist.
  1956. void CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
  1957. assert(PIdx && "don't add resources to an invalid Processor model");
  1958. RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
  1959. if (is_contained(WRDefs, ProcWriteResDef))
  1960. return;
  1961. WRDefs.push_back(ProcWriteResDef);
  1962. // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
  1963. RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
  1964. for (auto *ProcResDef : ProcResDefs) {
  1965. addProcResource(ProcResDef, ProcModels[PIdx], ProcWriteResDef->getLoc());
  1966. }
  1967. }
  1968. // Add resources for a ReadAdvance to this processor if they don't exist.
  1969. void CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
  1970. unsigned PIdx) {
  1971. RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
  1972. if (is_contained(RADefs, ProcReadAdvanceDef))
  1973. return;
  1974. RADefs.push_back(ProcReadAdvanceDef);
  1975. }
  1976. unsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
  1977. RecIter PRPos = find(ProcResourceDefs, PRDef);
  1978. if (PRPos == ProcResourceDefs.end())
  1979. PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
  1980. "the ProcResources list for " + ModelName);
  1981. // Idx=0 is reserved for invalid.
  1982. return 1 + (PRPos - ProcResourceDefs.begin());
  1983. }
  1984. bool CodeGenProcModel::isUnsupported(const CodeGenInstruction &Inst) const {
  1985. for (const Record *TheDef : UnsupportedFeaturesDefs) {
  1986. for (const Record *PredDef : Inst.TheDef->getValueAsListOfDefs("Predicates")) {
  1987. if (TheDef->getName() == PredDef->getName())
  1988. return true;
  1989. }
  1990. }
  1991. return false;
  1992. }
  1993. #ifndef NDEBUG
  1994. void CodeGenProcModel::dump() const {
  1995. dbgs() << Index << ": " << ModelName << " "
  1996. << (ModelDef ? ModelDef->getName() : "inferred") << " "
  1997. << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
  1998. }
  1999. void CodeGenSchedRW::dump() const {
  2000. dbgs() << Name << (IsVariadic ? " (V) " : " ");
  2001. if (IsSequence) {
  2002. dbgs() << "(";
  2003. dumpIdxVec(Sequence);
  2004. dbgs() << ")";
  2005. }
  2006. }
  2007. void CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
  2008. dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
  2009. << " Writes: ";
  2010. for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
  2011. SchedModels->getSchedWrite(Writes[i]).dump();
  2012. if (i < N-1) {
  2013. dbgs() << '\n';
  2014. dbgs().indent(10);
  2015. }
  2016. }
  2017. dbgs() << "\n Reads: ";
  2018. for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
  2019. SchedModels->getSchedRead(Reads[i]).dump();
  2020. if (i < N-1) {
  2021. dbgs() << '\n';
  2022. dbgs().indent(10);
  2023. }
  2024. }
  2025. dbgs() << "\n ProcIdx: "; dumpIdxVec(ProcIndices);
  2026. if (!Transitions.empty()) {
  2027. dbgs() << "\n Transitions for Proc ";
  2028. for (const CodeGenSchedTransition &Transition : Transitions) {
  2029. dbgs() << Transition.ProcIndex << ", ";
  2030. }
  2031. }
  2032. dbgs() << '\n';
  2033. }
  2034. void PredTransitions::dump() const {
  2035. dbgs() << "Expanded Variants:\n";
  2036. for (const auto &TI : TransVec) {
  2037. dbgs() << "{";
  2038. ListSeparator LS;
  2039. for (const PredCheck &PC : TI.PredTerm)
  2040. dbgs() << LS << SchedModels.getSchedRW(PC.RWIdx, PC.IsRead).Name << ":"
  2041. << PC.Predicate->getName();
  2042. dbgs() << "},\n => {";
  2043. for (SmallVectorImpl<SmallVector<unsigned, 4>>::const_iterator
  2044. WSI = TI.WriteSequences.begin(),
  2045. WSE = TI.WriteSequences.end();
  2046. WSI != WSE; ++WSI) {
  2047. dbgs() << "(";
  2048. ListSeparator LS;
  2049. for (unsigned N : *WSI)
  2050. dbgs() << LS << SchedModels.getSchedWrite(N).Name;
  2051. dbgs() << "),";
  2052. }
  2053. dbgs() << "}\n";
  2054. }
  2055. }
  2056. #endif // NDEBUG