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