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