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