1 //===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===// 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 tablegen backend emits subtarget enumerations. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CodeGenTarget.h" 14 #include "CodeGenSchedule.h" 15 #include "PredicateExpander.h" 16 #include "llvm/ADT/SmallPtrSet.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/StringExtras.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/MC/MCInstrItineraries.h" 21 #include "llvm/MC/MCSchedule.h" 22 #include "llvm/MC/SubtargetFeature.h" 23 #include "llvm/Support/Debug.h" 24 #include "llvm/Support/Format.h" 25 #include "llvm/Support/raw_ostream.h" 26 #include "llvm/TableGen/Error.h" 27 #include "llvm/TableGen/Record.h" 28 #include "llvm/TableGen/TableGenBackend.h" 29 #include <algorithm> 30 #include <cassert> 31 #include <cstdint> 32 #include <iterator> 33 #include <map> 34 #include <string> 35 #include <vector> 36 37 using namespace llvm; 38 39 #define DEBUG_TYPE "subtarget-emitter" 40 41 namespace { 42 43 class SubtargetEmitter { 44 // Each processor has a SchedClassDesc table with an entry for each SchedClass. 45 // The SchedClassDesc table indexes into a global write resource table, write 46 // latency table, and read advance table. 47 struct SchedClassTables { 48 std::vector<std::vector<MCSchedClassDesc>> ProcSchedClasses; 49 std::vector<MCWriteProcResEntry> WriteProcResources; 50 std::vector<MCWriteLatencyEntry> WriteLatencies; 51 std::vector<std::string> WriterNames; 52 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries; 53 54 // Reserve an invalid entry at index 0 55 SchedClassTables() { 56 ProcSchedClasses.resize(1); 57 WriteProcResources.resize(1); 58 WriteLatencies.resize(1); 59 WriterNames.push_back("InvalidWrite"); 60 ReadAdvanceEntries.resize(1); 61 } 62 }; 63 64 struct LessWriteProcResources { 65 bool operator()(const MCWriteProcResEntry &LHS, 66 const MCWriteProcResEntry &RHS) { 67 return LHS.ProcResourceIdx < RHS.ProcResourceIdx; 68 } 69 }; 70 71 const CodeGenTarget &TGT; 72 RecordKeeper &Records; 73 CodeGenSchedModels &SchedModels; 74 std::string Target; 75 76 void Enumeration(raw_ostream &OS, DenseMap<Record *, unsigned> &FeatureMap); 77 unsigned FeatureKeyValues(raw_ostream &OS, 78 const DenseMap<Record *, unsigned> &FeatureMap); 79 unsigned CPUKeyValues(raw_ostream &OS, 80 const DenseMap<Record *, unsigned> &FeatureMap); 81 void FormItineraryStageString(const std::string &Names, 82 Record *ItinData, std::string &ItinString, 83 unsigned &NStages); 84 void FormItineraryOperandCycleString(Record *ItinData, std::string &ItinString, 85 unsigned &NOperandCycles); 86 void FormItineraryBypassString(const std::string &Names, 87 Record *ItinData, 88 std::string &ItinString, unsigned NOperandCycles); 89 void EmitStageAndOperandCycleData(raw_ostream &OS, 90 std::vector<std::vector<InstrItinerary>> 91 &ProcItinLists); 92 void EmitItineraries(raw_ostream &OS, 93 std::vector<std::vector<InstrItinerary>> 94 &ProcItinLists); 95 unsigned EmitRegisterFileTables(const CodeGenProcModel &ProcModel, 96 raw_ostream &OS); 97 void EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel, 98 raw_ostream &OS); 99 void EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel, 100 raw_ostream &OS); 101 void EmitProcessorProp(raw_ostream &OS, const Record *R, StringRef Name, 102 char Separator); 103 void EmitProcessorResourceSubUnits(const CodeGenProcModel &ProcModel, 104 raw_ostream &OS); 105 void EmitProcessorResources(const CodeGenProcModel &ProcModel, 106 raw_ostream &OS); 107 Record *FindWriteResources(const CodeGenSchedRW &SchedWrite, 108 const CodeGenProcModel &ProcModel); 109 Record *FindReadAdvance(const CodeGenSchedRW &SchedRead, 110 const CodeGenProcModel &ProcModel); 111 void ExpandProcResources(RecVec &PRVec, std::vector<int64_t> &Cycles, 112 const CodeGenProcModel &ProcModel); 113 void GenSchedClassTables(const CodeGenProcModel &ProcModel, 114 SchedClassTables &SchedTables); 115 void EmitSchedClassTables(SchedClassTables &SchedTables, raw_ostream &OS); 116 void EmitProcessorModels(raw_ostream &OS); 117 void EmitSchedModelHelpers(const std::string &ClassName, raw_ostream &OS); 118 void emitSchedModelHelpersImpl(raw_ostream &OS, 119 bool OnlyExpandMCInstPredicates = false); 120 void emitGenMCSubtargetInfo(raw_ostream &OS); 121 void EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS); 122 123 void EmitSchedModel(raw_ostream &OS); 124 void EmitHwModeCheck(const std::string &ClassName, raw_ostream &OS); 125 void ParseFeaturesFunction(raw_ostream &OS, unsigned NumFeatures, 126 unsigned NumProcs); 127 128 public: 129 SubtargetEmitter(RecordKeeper &R, CodeGenTarget &TGT) 130 : TGT(TGT), Records(R), SchedModels(TGT.getSchedModels()), 131 Target(TGT.getName()) {} 132 133 void run(raw_ostream &o); 134 }; 135 136 } // end anonymous namespace 137 138 // 139 // Enumeration - Emit the specified class as an enumeration. 140 // 141 void SubtargetEmitter::Enumeration(raw_ostream &OS, 142 DenseMap<Record *, unsigned> &FeatureMap) { 143 // Get all records of class and sort 144 std::vector<Record*> DefList = 145 Records.getAllDerivedDefinitions("SubtargetFeature"); 146 llvm::sort(DefList, LessRecord()); 147 148 unsigned N = DefList.size(); 149 if (N == 0) 150 return; 151 if (N + 1 > MAX_SUBTARGET_FEATURES) 152 PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES."); 153 154 OS << "namespace " << Target << " {\n"; 155 156 // Open enumeration. 157 OS << "enum {\n"; 158 159 // For each record 160 for (unsigned i = 0; i < N; ++i) { 161 // Next record 162 Record *Def = DefList[i]; 163 164 // Get and emit name 165 OS << " " << Def->getName() << " = " << i << ",\n"; 166 167 // Save the index for this feature. 168 FeatureMap[Def] = i; 169 } 170 171 OS << " " 172 << "NumSubtargetFeatures = " << N << "\n"; 173 174 // Close enumeration and namespace 175 OS << "};\n"; 176 OS << "} // end namespace " << Target << "\n"; 177 } 178 179 static void printFeatureMask(raw_ostream &OS, RecVec &FeatureList, 180 const DenseMap<Record *, unsigned> &FeatureMap) { 181 std::array<uint64_t, MAX_SUBTARGET_WORDS> Mask = {}; 182 for (unsigned j = 0, M = FeatureList.size(); j < M; ++j) { 183 unsigned Bit = FeatureMap.lookup(FeatureList[j]); 184 Mask[Bit / 64] |= 1ULL << (Bit % 64); 185 } 186 187 OS << "{ { { "; 188 for (unsigned i = 0; i != Mask.size(); ++i) { 189 OS << "0x"; 190 OS.write_hex(Mask[i]); 191 OS << "ULL, "; 192 } 193 OS << "} } }"; 194 } 195 196 // 197 // FeatureKeyValues - Emit data of all the subtarget features. Used by the 198 // command line. 199 // 200 unsigned SubtargetEmitter::FeatureKeyValues( 201 raw_ostream &OS, const DenseMap<Record *, unsigned> &FeatureMap) { 202 // Gather and sort all the features 203 std::vector<Record*> FeatureList = 204 Records.getAllDerivedDefinitions("SubtargetFeature"); 205 206 if (FeatureList.empty()) 207 return 0; 208 209 llvm::sort(FeatureList, LessRecordFieldName()); 210 211 // Begin feature table 212 OS << "// Sorted (by key) array of values for CPU features.\n" 213 << "extern const llvm::SubtargetFeatureKV " << Target 214 << "FeatureKV[] = {\n"; 215 216 // For each feature 217 unsigned NumFeatures = 0; 218 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) { 219 // Next feature 220 Record *Feature = FeatureList[i]; 221 222 StringRef Name = Feature->getName(); 223 StringRef CommandLineName = Feature->getValueAsString("Name"); 224 StringRef Desc = Feature->getValueAsString("Desc"); 225 226 if (CommandLineName.empty()) continue; 227 228 // Emit as { "feature", "description", { featureEnum }, { i1 , i2 , ... , in } } 229 OS << " { " 230 << "\"" << CommandLineName << "\", " 231 << "\"" << Desc << "\", " 232 << Target << "::" << Name << ", "; 233 234 RecVec ImpliesList = Feature->getValueAsListOfDefs("Implies"); 235 236 printFeatureMask(OS, ImpliesList, FeatureMap); 237 238 OS << " },\n"; 239 ++NumFeatures; 240 } 241 242 // End feature table 243 OS << "};\n"; 244 245 return NumFeatures; 246 } 247 248 // 249 // CPUKeyValues - Emit data of all the subtarget processors. Used by command 250 // line. 251 // 252 unsigned 253 SubtargetEmitter::CPUKeyValues(raw_ostream &OS, 254 const DenseMap<Record *, unsigned> &FeatureMap) { 255 // Gather and sort processor information 256 std::vector<Record*> ProcessorList = 257 Records.getAllDerivedDefinitions("Processor"); 258 llvm::sort(ProcessorList, LessRecordFieldName()); 259 260 // Begin processor table 261 OS << "// Sorted (by key) array of values for CPU subtype.\n" 262 << "extern const llvm::SubtargetSubTypeKV " << Target 263 << "SubTypeKV[] = {\n"; 264 265 // For each processor 266 for (Record *Processor : ProcessorList) { 267 StringRef Name = Processor->getValueAsString("Name"); 268 RecVec FeatureList = Processor->getValueAsListOfDefs("Features"); 269 RecVec TuneFeatureList = Processor->getValueAsListOfDefs("TuneFeatures"); 270 271 // Emit as { "cpu", "description", 0, { f1 , f2 , ... fn } }, 272 OS << " { " 273 << "\"" << Name << "\", "; 274 275 printFeatureMask(OS, FeatureList, FeatureMap); 276 OS << ", "; 277 printFeatureMask(OS, TuneFeatureList, FeatureMap); 278 279 // Emit the scheduler model pointer. 280 const std::string &ProcModelName = 281 SchedModels.getModelForProc(Processor).ModelName; 282 OS << ", &" << ProcModelName << " },\n"; 283 } 284 285 // End processor table 286 OS << "};\n"; 287 288 return ProcessorList.size(); 289 } 290 291 // 292 // FormItineraryStageString - Compose a string containing the stage 293 // data initialization for the specified itinerary. N is the number 294 // of stages. 295 // 296 void SubtargetEmitter::FormItineraryStageString(const std::string &Name, 297 Record *ItinData, 298 std::string &ItinString, 299 unsigned &NStages) { 300 // Get states list 301 RecVec StageList = ItinData->getValueAsListOfDefs("Stages"); 302 303 // For each stage 304 unsigned N = NStages = StageList.size(); 305 for (unsigned i = 0; i < N;) { 306 // Next stage 307 const Record *Stage = StageList[i]; 308 309 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind } 310 int Cycles = Stage->getValueAsInt("Cycles"); 311 ItinString += " { " + itostr(Cycles) + ", "; 312 313 // Get unit list 314 RecVec UnitList = Stage->getValueAsListOfDefs("Units"); 315 316 // For each unit 317 for (unsigned j = 0, M = UnitList.size(); j < M;) { 318 // Add name and bitwise or 319 ItinString += Name + "FU::" + UnitList[j]->getName().str(); 320 if (++j < M) ItinString += " | "; 321 } 322 323 int TimeInc = Stage->getValueAsInt("TimeInc"); 324 ItinString += ", " + itostr(TimeInc); 325 326 int Kind = Stage->getValueAsInt("Kind"); 327 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind); 328 329 // Close off stage 330 ItinString += " }"; 331 if (++i < N) ItinString += ", "; 332 } 333 } 334 335 // 336 // FormItineraryOperandCycleString - Compose a string containing the 337 // operand cycle initialization for the specified itinerary. N is the 338 // number of operands that has cycles specified. 339 // 340 void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData, 341 std::string &ItinString, unsigned &NOperandCycles) { 342 // Get operand cycle list 343 std::vector<int64_t> OperandCycleList = 344 ItinData->getValueAsListOfInts("OperandCycles"); 345 346 // For each operand cycle 347 unsigned N = NOperandCycles = OperandCycleList.size(); 348 for (unsigned i = 0; i < N;) { 349 // Next operand cycle 350 const int OCycle = OperandCycleList[i]; 351 352 ItinString += " " + itostr(OCycle); 353 if (++i < N) ItinString += ", "; 354 } 355 } 356 357 void SubtargetEmitter::FormItineraryBypassString(const std::string &Name, 358 Record *ItinData, 359 std::string &ItinString, 360 unsigned NOperandCycles) { 361 RecVec BypassList = ItinData->getValueAsListOfDefs("Bypasses"); 362 unsigned N = BypassList.size(); 363 unsigned i = 0; 364 for (; i < N;) { 365 ItinString += Name + "Bypass::" + BypassList[i]->getName().str(); 366 if (++i < NOperandCycles) ItinString += ", "; 367 } 368 for (; i < NOperandCycles;) { 369 ItinString += " 0"; 370 if (++i < NOperandCycles) ItinString += ", "; 371 } 372 } 373 374 // 375 // EmitStageAndOperandCycleData - Generate unique itinerary stages and operand 376 // cycle tables. Create a list of InstrItinerary objects (ProcItinLists) indexed 377 // by CodeGenSchedClass::Index. 378 // 379 void SubtargetEmitter:: 380 EmitStageAndOperandCycleData(raw_ostream &OS, 381 std::vector<std::vector<InstrItinerary>> 382 &ProcItinLists) { 383 // Multiple processor models may share an itinerary record. Emit it once. 384 SmallPtrSet<Record*, 8> ItinsDefSet; 385 386 // Emit functional units for all the itineraries. 387 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) { 388 389 if (!ItinsDefSet.insert(ProcModel.ItinsDef).second) 390 continue; 391 392 RecVec FUs = ProcModel.ItinsDef->getValueAsListOfDefs("FU"); 393 if (FUs.empty()) 394 continue; 395 396 StringRef Name = ProcModel.ItinsDef->getName(); 397 OS << "\n// Functional units for \"" << Name << "\"\n" 398 << "namespace " << Name << "FU {\n"; 399 400 for (unsigned j = 0, FUN = FUs.size(); j < FUN; ++j) 401 OS << " const InstrStage::FuncUnits " << FUs[j]->getName() 402 << " = 1ULL << " << j << ";\n"; 403 404 OS << "} // end namespace " << Name << "FU\n"; 405 406 RecVec BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP"); 407 if (!BPs.empty()) { 408 OS << "\n// Pipeline forwarding paths for itineraries \"" << Name 409 << "\"\n" << "namespace " << Name << "Bypass {\n"; 410 411 OS << " const unsigned NoBypass = 0;\n"; 412 for (unsigned j = 0, BPN = BPs.size(); j < BPN; ++j) 413 OS << " const unsigned " << BPs[j]->getName() 414 << " = 1 << " << j << ";\n"; 415 416 OS << "} // end namespace " << Name << "Bypass\n"; 417 } 418 } 419 420 // Begin stages table 421 std::string StageTable = "\nextern const llvm::InstrStage " + Target + 422 "Stages[] = {\n"; 423 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n"; 424 425 // Begin operand cycle table 426 std::string OperandCycleTable = "extern const unsigned " + Target + 427 "OperandCycles[] = {\n"; 428 OperandCycleTable += " 0, // No itinerary\n"; 429 430 // Begin pipeline bypass table 431 std::string BypassTable = "extern const unsigned " + Target + 432 "ForwardingPaths[] = {\n"; 433 BypassTable += " 0, // No itinerary\n"; 434 435 // For each Itinerary across all processors, add a unique entry to the stages, 436 // operand cycles, and pipeline bypass tables. Then add the new Itinerary 437 // object with computed offsets to the ProcItinLists result. 438 unsigned StageCount = 1, OperandCycleCount = 1; 439 std::map<std::string, unsigned> ItinStageMap, ItinOperandMap; 440 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) { 441 // Add process itinerary to the list. 442 ProcItinLists.resize(ProcItinLists.size()+1); 443 444 // If this processor defines no itineraries, then leave the itinerary list 445 // empty. 446 std::vector<InstrItinerary> &ItinList = ProcItinLists.back(); 447 if (!ProcModel.hasItineraries()) 448 continue; 449 450 StringRef Name = ProcModel.ItinsDef->getName(); 451 452 ItinList.resize(SchedModels.numInstrSchedClasses()); 453 assert(ProcModel.ItinDefList.size() == ItinList.size() && "bad Itins"); 454 455 for (unsigned SchedClassIdx = 0, SchedClassEnd = ItinList.size(); 456 SchedClassIdx < SchedClassEnd; ++SchedClassIdx) { 457 458 // Next itinerary data 459 Record *ItinData = ProcModel.ItinDefList[SchedClassIdx]; 460 461 // Get string and stage count 462 std::string ItinStageString; 463 unsigned NStages = 0; 464 if (ItinData) 465 FormItineraryStageString(std::string(Name), ItinData, ItinStageString, 466 NStages); 467 468 // Get string and operand cycle count 469 std::string ItinOperandCycleString; 470 unsigned NOperandCycles = 0; 471 std::string ItinBypassString; 472 if (ItinData) { 473 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString, 474 NOperandCycles); 475 476 FormItineraryBypassString(std::string(Name), ItinData, ItinBypassString, 477 NOperandCycles); 478 } 479 480 // Check to see if stage already exists and create if it doesn't 481 uint16_t FindStage = 0; 482 if (NStages > 0) { 483 FindStage = ItinStageMap[ItinStageString]; 484 if (FindStage == 0) { 485 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // indices 486 StageTable += ItinStageString + ", // " + itostr(StageCount); 487 if (NStages > 1) 488 StageTable += "-" + itostr(StageCount + NStages - 1); 489 StageTable += "\n"; 490 // Record Itin class number. 491 ItinStageMap[ItinStageString] = FindStage = StageCount; 492 StageCount += NStages; 493 } 494 } 495 496 // Check to see if operand cycle already exists and create if it doesn't 497 uint16_t FindOperandCycle = 0; 498 if (NOperandCycles > 0) { 499 std::string ItinOperandString = ItinOperandCycleString+ItinBypassString; 500 FindOperandCycle = ItinOperandMap[ItinOperandString]; 501 if (FindOperandCycle == 0) { 502 // Emit as cycle, // index 503 OperandCycleTable += ItinOperandCycleString + ", // "; 504 std::string OperandIdxComment = itostr(OperandCycleCount); 505 if (NOperandCycles > 1) 506 OperandIdxComment += "-" 507 + itostr(OperandCycleCount + NOperandCycles - 1); 508 OperandCycleTable += OperandIdxComment + "\n"; 509 // Record Itin class number. 510 ItinOperandMap[ItinOperandCycleString] = 511 FindOperandCycle = OperandCycleCount; 512 // Emit as bypass, // index 513 BypassTable += ItinBypassString + ", // " + OperandIdxComment + "\n"; 514 OperandCycleCount += NOperandCycles; 515 } 516 } 517 518 // Set up itinerary as location and location + stage count 519 int16_t NumUOps = ItinData ? ItinData->getValueAsInt("NumMicroOps") : 0; 520 InstrItinerary Intinerary = { 521 NumUOps, 522 FindStage, 523 uint16_t(FindStage + NStages), 524 FindOperandCycle, 525 uint16_t(FindOperandCycle + NOperandCycles), 526 }; 527 528 // Inject - empty slots will be 0, 0 529 ItinList[SchedClassIdx] = Intinerary; 530 } 531 } 532 533 // Closing stage 534 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End stages\n"; 535 StageTable += "};\n"; 536 537 // Closing operand cycles 538 OperandCycleTable += " 0 // End operand cycles\n"; 539 OperandCycleTable += "};\n"; 540 541 BypassTable += " 0 // End bypass tables\n"; 542 BypassTable += "};\n"; 543 544 // Emit tables. 545 OS << StageTable; 546 OS << OperandCycleTable; 547 OS << BypassTable; 548 } 549 550 // 551 // EmitProcessorData - Generate data for processor itineraries that were 552 // computed during EmitStageAndOperandCycleData(). ProcItinLists lists all 553 // Itineraries for each processor. The Itinerary lists are indexed on 554 // CodeGenSchedClass::Index. 555 // 556 void SubtargetEmitter:: 557 EmitItineraries(raw_ostream &OS, 558 std::vector<std::vector<InstrItinerary>> &ProcItinLists) { 559 // Multiple processor models may share an itinerary record. Emit it once. 560 SmallPtrSet<Record*, 8> ItinsDefSet; 561 562 // For each processor's machine model 563 std::vector<std::vector<InstrItinerary>>::iterator 564 ProcItinListsIter = ProcItinLists.begin(); 565 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(), 566 PE = SchedModels.procModelEnd(); PI != PE; ++PI, ++ProcItinListsIter) { 567 568 Record *ItinsDef = PI->ItinsDef; 569 if (!ItinsDefSet.insert(ItinsDef).second) 570 continue; 571 572 // Get the itinerary list for the processor. 573 assert(ProcItinListsIter != ProcItinLists.end() && "bad iterator"); 574 std::vector<InstrItinerary> &ItinList = *ProcItinListsIter; 575 576 // Empty itineraries aren't referenced anywhere in the tablegen output 577 // so don't emit them. 578 if (ItinList.empty()) 579 continue; 580 581 OS << "\n"; 582 OS << "static const llvm::InstrItinerary "; 583 584 // Begin processor itinerary table 585 OS << ItinsDef->getName() << "[] = {\n"; 586 587 // For each itinerary class in CodeGenSchedClass::Index order. 588 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) { 589 InstrItinerary &Intinerary = ItinList[j]; 590 591 // Emit Itinerary in the form of 592 // { firstStage, lastStage, firstCycle, lastCycle } // index 593 OS << " { " << 594 Intinerary.NumMicroOps << ", " << 595 Intinerary.FirstStage << ", " << 596 Intinerary.LastStage << ", " << 597 Intinerary.FirstOperandCycle << ", " << 598 Intinerary.LastOperandCycle << " }" << 599 ", // " << j << " " << SchedModels.getSchedClass(j).Name << "\n"; 600 } 601 // End processor itinerary table 602 OS << " { 0, uint16_t(~0U), uint16_t(~0U), uint16_t(~0U), uint16_t(~0U) }" 603 "// end marker\n"; 604 OS << "};\n"; 605 } 606 } 607 608 // Emit either the value defined in the TableGen Record, or the default 609 // value defined in the C++ header. The Record is null if the processor does not 610 // define a model. 611 void SubtargetEmitter::EmitProcessorProp(raw_ostream &OS, const Record *R, 612 StringRef Name, char Separator) { 613 OS << " "; 614 int V = R ? R->getValueAsInt(Name) : -1; 615 if (V >= 0) 616 OS << V << Separator << " // " << Name; 617 else 618 OS << "MCSchedModel::Default" << Name << Separator; 619 OS << '\n'; 620 } 621 622 void SubtargetEmitter::EmitProcessorResourceSubUnits( 623 const CodeGenProcModel &ProcModel, raw_ostream &OS) { 624 OS << "\nstatic const unsigned " << ProcModel.ModelName 625 << "ProcResourceSubUnits[] = {\n" 626 << " 0, // Invalid\n"; 627 628 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) { 629 Record *PRDef = ProcModel.ProcResourceDefs[i]; 630 if (!PRDef->isSubClassOf("ProcResGroup")) 631 continue; 632 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources"); 633 for (Record *RUDef : ResUnits) { 634 Record *const RU = 635 SchedModels.findProcResUnits(RUDef, ProcModel, PRDef->getLoc()); 636 for (unsigned J = 0; J < RU->getValueAsInt("NumUnits"); ++J) { 637 OS << " " << ProcModel.getProcResourceIdx(RU) << ", "; 638 } 639 } 640 OS << " // " << PRDef->getName() << "\n"; 641 } 642 OS << "};\n"; 643 } 644 645 static void EmitRetireControlUnitInfo(const CodeGenProcModel &ProcModel, 646 raw_ostream &OS) { 647 int64_t ReorderBufferSize = 0, MaxRetirePerCycle = 0; 648 if (Record *RCU = ProcModel.RetireControlUnit) { 649 ReorderBufferSize = 650 std::max(ReorderBufferSize, RCU->getValueAsInt("ReorderBufferSize")); 651 MaxRetirePerCycle = 652 std::max(MaxRetirePerCycle, RCU->getValueAsInt("MaxRetirePerCycle")); 653 } 654 655 OS << ReorderBufferSize << ", // ReorderBufferSize\n "; 656 OS << MaxRetirePerCycle << ", // MaxRetirePerCycle\n "; 657 } 658 659 static void EmitRegisterFileInfo(const CodeGenProcModel &ProcModel, 660 unsigned NumRegisterFiles, 661 unsigned NumCostEntries, raw_ostream &OS) { 662 if (NumRegisterFiles) 663 OS << ProcModel.ModelName << "RegisterFiles,\n " << (1 + NumRegisterFiles); 664 else 665 OS << "nullptr,\n 0"; 666 667 OS << ", // Number of register files.\n "; 668 if (NumCostEntries) 669 OS << ProcModel.ModelName << "RegisterCosts,\n "; 670 else 671 OS << "nullptr,\n "; 672 OS << NumCostEntries << ", // Number of register cost entries.\n"; 673 } 674 675 unsigned 676 SubtargetEmitter::EmitRegisterFileTables(const CodeGenProcModel &ProcModel, 677 raw_ostream &OS) { 678 if (llvm::all_of(ProcModel.RegisterFiles, [](const CodeGenRegisterFile &RF) { 679 return RF.hasDefaultCosts(); 680 })) 681 return 0; 682 683 // Print the RegisterCost table first. 684 OS << "\n// {RegisterClassID, Register Cost, AllowMoveElimination }\n"; 685 OS << "static const llvm::MCRegisterCostEntry " << ProcModel.ModelName 686 << "RegisterCosts" 687 << "[] = {\n"; 688 689 for (const CodeGenRegisterFile &RF : ProcModel.RegisterFiles) { 690 // Skip register files with a default cost table. 691 if (RF.hasDefaultCosts()) 692 continue; 693 // Add entries to the cost table. 694 for (const CodeGenRegisterCost &RC : RF.Costs) { 695 OS << " { "; 696 Record *Rec = RC.RCDef; 697 if (Rec->getValue("Namespace")) 698 OS << Rec->getValueAsString("Namespace") << "::"; 699 OS << Rec->getName() << "RegClassID, " << RC.Cost << ", " 700 << RC.AllowMoveElimination << "},\n"; 701 } 702 } 703 OS << "};\n"; 704 705 // Now generate a table with register file info. 706 OS << "\n // {Name, #PhysRegs, #CostEntries, IndexToCostTbl, " 707 << "MaxMovesEliminatedPerCycle, AllowZeroMoveEliminationOnly }\n"; 708 OS << "static const llvm::MCRegisterFileDesc " << ProcModel.ModelName 709 << "RegisterFiles" 710 << "[] = {\n" 711 << " { \"InvalidRegisterFile\", 0, 0, 0, 0, 0 },\n"; 712 unsigned CostTblIndex = 0; 713 714 for (const CodeGenRegisterFile &RD : ProcModel.RegisterFiles) { 715 OS << " { "; 716 OS << '"' << RD.Name << '"' << ", " << RD.NumPhysRegs << ", "; 717 unsigned NumCostEntries = RD.Costs.size(); 718 OS << NumCostEntries << ", " << CostTblIndex << ", " 719 << RD.MaxMovesEliminatedPerCycle << ", " 720 << RD.AllowZeroMoveEliminationOnly << "},\n"; 721 CostTblIndex += NumCostEntries; 722 } 723 OS << "};\n"; 724 725 return CostTblIndex; 726 } 727 728 void SubtargetEmitter::EmitLoadStoreQueueInfo(const CodeGenProcModel &ProcModel, 729 raw_ostream &OS) { 730 unsigned QueueID = 0; 731 if (ProcModel.LoadQueue) { 732 const Record *Queue = ProcModel.LoadQueue->getValueAsDef("QueueDescriptor"); 733 QueueID = 734 1 + std::distance(ProcModel.ProcResourceDefs.begin(), 735 std::find(ProcModel.ProcResourceDefs.begin(), 736 ProcModel.ProcResourceDefs.end(), Queue)); 737 } 738 OS << " " << QueueID << ", // Resource Descriptor for the Load Queue\n"; 739 740 QueueID = 0; 741 if (ProcModel.StoreQueue) { 742 const Record *Queue = 743 ProcModel.StoreQueue->getValueAsDef("QueueDescriptor"); 744 QueueID = 745 1 + std::distance(ProcModel.ProcResourceDefs.begin(), 746 std::find(ProcModel.ProcResourceDefs.begin(), 747 ProcModel.ProcResourceDefs.end(), Queue)); 748 } 749 OS << " " << QueueID << ", // Resource Descriptor for the Store Queue\n"; 750 } 751 752 void SubtargetEmitter::EmitExtraProcessorInfo(const CodeGenProcModel &ProcModel, 753 raw_ostream &OS) { 754 // Generate a table of register file descriptors (one entry per each user 755 // defined register file), and a table of register costs. 756 unsigned NumCostEntries = EmitRegisterFileTables(ProcModel, OS); 757 758 // Now generate a table for the extra processor info. 759 OS << "\nstatic const llvm::MCExtraProcessorInfo " << ProcModel.ModelName 760 << "ExtraInfo = {\n "; 761 762 // Add information related to the retire control unit. 763 EmitRetireControlUnitInfo(ProcModel, OS); 764 765 // Add information related to the register files (i.e. where to find register 766 // file descriptors and register costs). 767 EmitRegisterFileInfo(ProcModel, ProcModel.RegisterFiles.size(), 768 NumCostEntries, OS); 769 770 // Add information about load/store queues. 771 EmitLoadStoreQueueInfo(ProcModel, OS); 772 773 OS << "};\n"; 774 } 775 776 void SubtargetEmitter::EmitProcessorResources(const CodeGenProcModel &ProcModel, 777 raw_ostream &OS) { 778 EmitProcessorResourceSubUnits(ProcModel, OS); 779 780 OS << "\n// {Name, NumUnits, SuperIdx, BufferSize, SubUnitsIdxBegin}\n"; 781 OS << "static const llvm::MCProcResourceDesc " << ProcModel.ModelName 782 << "ProcResources" 783 << "[] = {\n" 784 << " {\"InvalidUnit\", 0, 0, 0, 0},\n"; 785 786 unsigned SubUnitsOffset = 1; 787 for (unsigned i = 0, e = ProcModel.ProcResourceDefs.size(); i < e; ++i) { 788 Record *PRDef = ProcModel.ProcResourceDefs[i]; 789 790 Record *SuperDef = nullptr; 791 unsigned SuperIdx = 0; 792 unsigned NumUnits = 0; 793 const unsigned SubUnitsBeginOffset = SubUnitsOffset; 794 int BufferSize = PRDef->getValueAsInt("BufferSize"); 795 if (PRDef->isSubClassOf("ProcResGroup")) { 796 RecVec ResUnits = PRDef->getValueAsListOfDefs("Resources"); 797 for (Record *RU : ResUnits) { 798 NumUnits += RU->getValueAsInt("NumUnits"); 799 SubUnitsOffset += RU->getValueAsInt("NumUnits"); 800 } 801 } 802 else { 803 // Find the SuperIdx 804 if (PRDef->getValueInit("Super")->isComplete()) { 805 SuperDef = 806 SchedModels.findProcResUnits(PRDef->getValueAsDef("Super"), 807 ProcModel, PRDef->getLoc()); 808 SuperIdx = ProcModel.getProcResourceIdx(SuperDef); 809 } 810 NumUnits = PRDef->getValueAsInt("NumUnits"); 811 } 812 // Emit the ProcResourceDesc 813 OS << " {\"" << PRDef->getName() << "\", "; 814 if (PRDef->getName().size() < 15) 815 OS.indent(15 - PRDef->getName().size()); 816 OS << NumUnits << ", " << SuperIdx << ", " << BufferSize << ", "; 817 if (SubUnitsBeginOffset != SubUnitsOffset) { 818 OS << ProcModel.ModelName << "ProcResourceSubUnits + " 819 << SubUnitsBeginOffset; 820 } else { 821 OS << "nullptr"; 822 } 823 OS << "}, // #" << i+1; 824 if (SuperDef) 825 OS << ", Super=" << SuperDef->getName(); 826 OS << "\n"; 827 } 828 OS << "};\n"; 829 } 830 831 // Find the WriteRes Record that defines processor resources for this 832 // SchedWrite. 833 Record *SubtargetEmitter::FindWriteResources( 834 const CodeGenSchedRW &SchedWrite, const CodeGenProcModel &ProcModel) { 835 836 // Check if the SchedWrite is already subtarget-specific and directly 837 // specifies a set of processor resources. 838 if (SchedWrite.TheDef->isSubClassOf("SchedWriteRes")) 839 return SchedWrite.TheDef; 840 841 Record *AliasDef = nullptr; 842 for (Record *A : SchedWrite.Aliases) { 843 const CodeGenSchedRW &AliasRW = 844 SchedModels.getSchedRW(A->getValueAsDef("AliasRW")); 845 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) { 846 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel"); 847 if (&SchedModels.getProcModel(ModelDef) != &ProcModel) 848 continue; 849 } 850 if (AliasDef) 851 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases " 852 "defined for processor " + ProcModel.ModelName + 853 " Ensure only one SchedAlias exists per RW."); 854 AliasDef = AliasRW.TheDef; 855 } 856 if (AliasDef && AliasDef->isSubClassOf("SchedWriteRes")) 857 return AliasDef; 858 859 // Check this processor's list of write resources. 860 Record *ResDef = nullptr; 861 for (Record *WR : ProcModel.WriteResDefs) { 862 if (!WR->isSubClassOf("WriteRes")) 863 continue; 864 if (AliasDef == WR->getValueAsDef("WriteType") 865 || SchedWrite.TheDef == WR->getValueAsDef("WriteType")) { 866 if (ResDef) { 867 PrintFatalError(WR->getLoc(), "Resources are defined for both " 868 "SchedWrite and its alias on processor " + 869 ProcModel.ModelName); 870 } 871 ResDef = WR; 872 } 873 } 874 // TODO: If ProcModel has a base model (previous generation processor), 875 // then call FindWriteResources recursively with that model here. 876 if (!ResDef) { 877 PrintFatalError(ProcModel.ModelDef->getLoc(), 878 Twine("Processor does not define resources for ") + 879 SchedWrite.TheDef->getName()); 880 } 881 return ResDef; 882 } 883 884 /// Find the ReadAdvance record for the given SchedRead on this processor or 885 /// return NULL. 886 Record *SubtargetEmitter::FindReadAdvance(const CodeGenSchedRW &SchedRead, 887 const CodeGenProcModel &ProcModel) { 888 // Check for SchedReads that directly specify a ReadAdvance. 889 if (SchedRead.TheDef->isSubClassOf("SchedReadAdvance")) 890 return SchedRead.TheDef; 891 892 // Check this processor's list of aliases for SchedRead. 893 Record *AliasDef = nullptr; 894 for (Record *A : SchedRead.Aliases) { 895 const CodeGenSchedRW &AliasRW = 896 SchedModels.getSchedRW(A->getValueAsDef("AliasRW")); 897 if (AliasRW.TheDef->getValueInit("SchedModel")->isComplete()) { 898 Record *ModelDef = AliasRW.TheDef->getValueAsDef("SchedModel"); 899 if (&SchedModels.getProcModel(ModelDef) != &ProcModel) 900 continue; 901 } 902 if (AliasDef) 903 PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases " 904 "defined for processor " + ProcModel.ModelName + 905 " Ensure only one SchedAlias exists per RW."); 906 AliasDef = AliasRW.TheDef; 907 } 908 if (AliasDef && AliasDef->isSubClassOf("SchedReadAdvance")) 909 return AliasDef; 910 911 // Check this processor's ReadAdvanceList. 912 Record *ResDef = nullptr; 913 for (Record *RA : ProcModel.ReadAdvanceDefs) { 914 if (!RA->isSubClassOf("ReadAdvance")) 915 continue; 916 if (AliasDef == RA->getValueAsDef("ReadType") 917 || SchedRead.TheDef == RA->getValueAsDef("ReadType")) { 918 if (ResDef) { 919 PrintFatalError(RA->getLoc(), "Resources are defined for both " 920 "SchedRead and its alias on processor " + 921 ProcModel.ModelName); 922 } 923 ResDef = RA; 924 } 925 } 926 // TODO: If ProcModel has a base model (previous generation processor), 927 // then call FindReadAdvance recursively with that model here. 928 if (!ResDef && SchedRead.TheDef->getName() != "ReadDefault") { 929 PrintFatalError(ProcModel.ModelDef->getLoc(), 930 Twine("Processor does not define resources for ") + 931 SchedRead.TheDef->getName()); 932 } 933 return ResDef; 934 } 935 936 // Expand an explicit list of processor resources into a full list of implied 937 // resource groups and super resources that cover them. 938 void SubtargetEmitter::ExpandProcResources(RecVec &PRVec, 939 std::vector<int64_t> &Cycles, 940 const CodeGenProcModel &PM) { 941 assert(PRVec.size() == Cycles.size() && "failed precondition"); 942 for (unsigned i = 0, e = PRVec.size(); i != e; ++i) { 943 Record *PRDef = PRVec[i]; 944 RecVec SubResources; 945 if (PRDef->isSubClassOf("ProcResGroup")) 946 SubResources = PRDef->getValueAsListOfDefs("Resources"); 947 else { 948 SubResources.push_back(PRDef); 949 PRDef = SchedModels.findProcResUnits(PRDef, PM, PRDef->getLoc()); 950 for (Record *SubDef = PRDef; 951 SubDef->getValueInit("Super")->isComplete();) { 952 if (SubDef->isSubClassOf("ProcResGroup")) { 953 // Disallow this for simplicitly. 954 PrintFatalError(SubDef->getLoc(), "Processor resource group " 955 " cannot be a super resources."); 956 } 957 Record *SuperDef = 958 SchedModels.findProcResUnits(SubDef->getValueAsDef("Super"), PM, 959 SubDef->getLoc()); 960 PRVec.push_back(SuperDef); 961 Cycles.push_back(Cycles[i]); 962 SubDef = SuperDef; 963 } 964 } 965 for (Record *PR : PM.ProcResourceDefs) { 966 if (PR == PRDef || !PR->isSubClassOf("ProcResGroup")) 967 continue; 968 RecVec SuperResources = PR->getValueAsListOfDefs("Resources"); 969 RecIter SubI = SubResources.begin(), SubE = SubResources.end(); 970 for( ; SubI != SubE; ++SubI) { 971 if (!is_contained(SuperResources, *SubI)) { 972 break; 973 } 974 } 975 if (SubI == SubE) { 976 PRVec.push_back(PR); 977 Cycles.push_back(Cycles[i]); 978 } 979 } 980 } 981 } 982 983 // Generate the SchedClass table for this processor and update global 984 // tables. Must be called for each processor in order. 985 void SubtargetEmitter::GenSchedClassTables(const CodeGenProcModel &ProcModel, 986 SchedClassTables &SchedTables) { 987 SchedTables.ProcSchedClasses.resize(SchedTables.ProcSchedClasses.size() + 1); 988 if (!ProcModel.hasInstrSchedModel()) 989 return; 990 991 std::vector<MCSchedClassDesc> &SCTab = SchedTables.ProcSchedClasses.back(); 992 LLVM_DEBUG(dbgs() << "\n+++ SCHED CLASSES (GenSchedClassTables) +++\n"); 993 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) { 994 LLVM_DEBUG(SC.dump(&SchedModels)); 995 996 SCTab.resize(SCTab.size() + 1); 997 MCSchedClassDesc &SCDesc = SCTab.back(); 998 // SCDesc.Name is guarded by NDEBUG 999 SCDesc.NumMicroOps = 0; 1000 SCDesc.BeginGroup = false; 1001 SCDesc.EndGroup = false; 1002 SCDesc.WriteProcResIdx = 0; 1003 SCDesc.WriteLatencyIdx = 0; 1004 SCDesc.ReadAdvanceIdx = 0; 1005 1006 // A Variant SchedClass has no resources of its own. 1007 bool HasVariants = false; 1008 for (const CodeGenSchedTransition &CGT : 1009 make_range(SC.Transitions.begin(), SC.Transitions.end())) { 1010 if (CGT.ProcIndex == ProcModel.Index) { 1011 HasVariants = true; 1012 break; 1013 } 1014 } 1015 if (HasVariants) { 1016 SCDesc.NumMicroOps = MCSchedClassDesc::VariantNumMicroOps; 1017 continue; 1018 } 1019 1020 // Determine if the SchedClass is actually reachable on this processor. If 1021 // not don't try to locate the processor resources, it will fail. 1022 // If ProcIndices contains 0, this class applies to all processors. 1023 assert(!SC.ProcIndices.empty() && "expect at least one procidx"); 1024 if (SC.ProcIndices[0] != 0) { 1025 if (!is_contained(SC.ProcIndices, ProcModel.Index)) 1026 continue; 1027 } 1028 IdxVec Writes = SC.Writes; 1029 IdxVec Reads = SC.Reads; 1030 if (!SC.InstRWs.empty()) { 1031 // This class has a default ReadWrite list which can be overridden by 1032 // InstRW definitions. 1033 Record *RWDef = nullptr; 1034 for (Record *RW : SC.InstRWs) { 1035 Record *RWModelDef = RW->getValueAsDef("SchedModel"); 1036 if (&ProcModel == &SchedModels.getProcModel(RWModelDef)) { 1037 RWDef = RW; 1038 break; 1039 } 1040 } 1041 if (RWDef) { 1042 Writes.clear(); 1043 Reads.clear(); 1044 SchedModels.findRWs(RWDef->getValueAsListOfDefs("OperandReadWrites"), 1045 Writes, Reads); 1046 } 1047 } 1048 if (Writes.empty()) { 1049 // Check this processor's itinerary class resources. 1050 for (Record *I : ProcModel.ItinRWDefs) { 1051 RecVec Matched = I->getValueAsListOfDefs("MatchedItinClasses"); 1052 if (is_contained(Matched, SC.ItinClassDef)) { 1053 SchedModels.findRWs(I->getValueAsListOfDefs("OperandReadWrites"), 1054 Writes, Reads); 1055 break; 1056 } 1057 } 1058 if (Writes.empty()) { 1059 LLVM_DEBUG(dbgs() << ProcModel.ModelName 1060 << " does not have resources for class " << SC.Name 1061 << '\n'); 1062 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps; 1063 } 1064 } 1065 // Sum resources across all operand writes. 1066 std::vector<MCWriteProcResEntry> WriteProcResources; 1067 std::vector<MCWriteLatencyEntry> WriteLatencies; 1068 std::vector<std::string> WriterNames; 1069 std::vector<MCReadAdvanceEntry> ReadAdvanceEntries; 1070 for (unsigned W : Writes) { 1071 IdxVec WriteSeq; 1072 SchedModels.expandRWSeqForProc(W, WriteSeq, /*IsRead=*/false, 1073 ProcModel); 1074 1075 // For each operand, create a latency entry. 1076 MCWriteLatencyEntry WLEntry; 1077 WLEntry.Cycles = 0; 1078 unsigned WriteID = WriteSeq.back(); 1079 WriterNames.push_back(SchedModels.getSchedWrite(WriteID).Name); 1080 // If this Write is not referenced by a ReadAdvance, don't distinguish it 1081 // from other WriteLatency entries. 1082 if (!SchedModels.hasReadOfWrite( 1083 SchedModels.getSchedWrite(WriteID).TheDef)) { 1084 WriteID = 0; 1085 } 1086 WLEntry.WriteResourceID = WriteID; 1087 1088 for (unsigned WS : WriteSeq) { 1089 1090 Record *WriteRes = 1091 FindWriteResources(SchedModels.getSchedWrite(WS), ProcModel); 1092 1093 // Mark the parent class as invalid for unsupported write types. 1094 if (WriteRes->getValueAsBit("Unsupported")) { 1095 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps; 1096 break; 1097 } 1098 WLEntry.Cycles += WriteRes->getValueAsInt("Latency"); 1099 SCDesc.NumMicroOps += WriteRes->getValueAsInt("NumMicroOps"); 1100 SCDesc.BeginGroup |= WriteRes->getValueAsBit("BeginGroup"); 1101 SCDesc.EndGroup |= WriteRes->getValueAsBit("EndGroup"); 1102 SCDesc.BeginGroup |= WriteRes->getValueAsBit("SingleIssue"); 1103 SCDesc.EndGroup |= WriteRes->getValueAsBit("SingleIssue"); 1104 1105 // Create an entry for each ProcResource listed in WriteRes. 1106 RecVec PRVec = WriteRes->getValueAsListOfDefs("ProcResources"); 1107 std::vector<int64_t> Cycles = 1108 WriteRes->getValueAsListOfInts("ResourceCycles"); 1109 1110 if (Cycles.empty()) { 1111 // If ResourceCycles is not provided, default to one cycle per 1112 // resource. 1113 Cycles.resize(PRVec.size(), 1); 1114 } else if (Cycles.size() != PRVec.size()) { 1115 // If ResourceCycles is provided, check consistency. 1116 PrintFatalError( 1117 WriteRes->getLoc(), 1118 Twine("Inconsistent resource cycles: !size(ResourceCycles) != " 1119 "!size(ProcResources): ") 1120 .concat(Twine(PRVec.size())) 1121 .concat(" vs ") 1122 .concat(Twine(Cycles.size()))); 1123 } 1124 1125 ExpandProcResources(PRVec, Cycles, ProcModel); 1126 1127 for (unsigned PRIdx = 0, PREnd = PRVec.size(); 1128 PRIdx != PREnd; ++PRIdx) { 1129 MCWriteProcResEntry WPREntry; 1130 WPREntry.ProcResourceIdx = ProcModel.getProcResourceIdx(PRVec[PRIdx]); 1131 assert(WPREntry.ProcResourceIdx && "Bad ProcResourceIdx"); 1132 WPREntry.Cycles = Cycles[PRIdx]; 1133 // If this resource is already used in this sequence, add the current 1134 // entry's cycles so that the same resource appears to be used 1135 // serially, rather than multiple parallel uses. This is important for 1136 // in-order machine where the resource consumption is a hazard. 1137 unsigned WPRIdx = 0, WPREnd = WriteProcResources.size(); 1138 for( ; WPRIdx != WPREnd; ++WPRIdx) { 1139 if (WriteProcResources[WPRIdx].ProcResourceIdx 1140 == WPREntry.ProcResourceIdx) { 1141 WriteProcResources[WPRIdx].Cycles += WPREntry.Cycles; 1142 break; 1143 } 1144 } 1145 if (WPRIdx == WPREnd) 1146 WriteProcResources.push_back(WPREntry); 1147 } 1148 } 1149 WriteLatencies.push_back(WLEntry); 1150 } 1151 // Create an entry for each operand Read in this SchedClass. 1152 // Entries must be sorted first by UseIdx then by WriteResourceID. 1153 for (unsigned UseIdx = 0, EndIdx = Reads.size(); 1154 UseIdx != EndIdx; ++UseIdx) { 1155 Record *ReadAdvance = 1156 FindReadAdvance(SchedModels.getSchedRead(Reads[UseIdx]), ProcModel); 1157 if (!ReadAdvance) 1158 continue; 1159 1160 // Mark the parent class as invalid for unsupported write types. 1161 if (ReadAdvance->getValueAsBit("Unsupported")) { 1162 SCDesc.NumMicroOps = MCSchedClassDesc::InvalidNumMicroOps; 1163 break; 1164 } 1165 RecVec ValidWrites = ReadAdvance->getValueAsListOfDefs("ValidWrites"); 1166 IdxVec WriteIDs; 1167 if (ValidWrites.empty()) 1168 WriteIDs.push_back(0); 1169 else { 1170 for (Record *VW : ValidWrites) { 1171 WriteIDs.push_back(SchedModels.getSchedRWIdx(VW, /*IsRead=*/false)); 1172 } 1173 } 1174 llvm::sort(WriteIDs); 1175 for(unsigned W : WriteIDs) { 1176 MCReadAdvanceEntry RAEntry; 1177 RAEntry.UseIdx = UseIdx; 1178 RAEntry.WriteResourceID = W; 1179 RAEntry.Cycles = ReadAdvance->getValueAsInt("Cycles"); 1180 ReadAdvanceEntries.push_back(RAEntry); 1181 } 1182 } 1183 if (SCDesc.NumMicroOps == MCSchedClassDesc::InvalidNumMicroOps) { 1184 WriteProcResources.clear(); 1185 WriteLatencies.clear(); 1186 ReadAdvanceEntries.clear(); 1187 } 1188 // Add the information for this SchedClass to the global tables using basic 1189 // compression. 1190 // 1191 // WritePrecRes entries are sorted by ProcResIdx. 1192 llvm::sort(WriteProcResources, LessWriteProcResources()); 1193 1194 SCDesc.NumWriteProcResEntries = WriteProcResources.size(); 1195 std::vector<MCWriteProcResEntry>::iterator WPRPos = 1196 std::search(SchedTables.WriteProcResources.begin(), 1197 SchedTables.WriteProcResources.end(), 1198 WriteProcResources.begin(), WriteProcResources.end()); 1199 if (WPRPos != SchedTables.WriteProcResources.end()) 1200 SCDesc.WriteProcResIdx = WPRPos - SchedTables.WriteProcResources.begin(); 1201 else { 1202 SCDesc.WriteProcResIdx = SchedTables.WriteProcResources.size(); 1203 SchedTables.WriteProcResources.insert(WPRPos, WriteProcResources.begin(), 1204 WriteProcResources.end()); 1205 } 1206 // Latency entries must remain in operand order. 1207 SCDesc.NumWriteLatencyEntries = WriteLatencies.size(); 1208 std::vector<MCWriteLatencyEntry>::iterator WLPos = 1209 std::search(SchedTables.WriteLatencies.begin(), 1210 SchedTables.WriteLatencies.end(), 1211 WriteLatencies.begin(), WriteLatencies.end()); 1212 if (WLPos != SchedTables.WriteLatencies.end()) { 1213 unsigned idx = WLPos - SchedTables.WriteLatencies.begin(); 1214 SCDesc.WriteLatencyIdx = idx; 1215 for (unsigned i = 0, e = WriteLatencies.size(); i < e; ++i) 1216 if (SchedTables.WriterNames[idx + i].find(WriterNames[i]) == 1217 std::string::npos) { 1218 SchedTables.WriterNames[idx + i] += std::string("_") + WriterNames[i]; 1219 } 1220 } 1221 else { 1222 SCDesc.WriteLatencyIdx = SchedTables.WriteLatencies.size(); 1223 SchedTables.WriteLatencies.insert(SchedTables.WriteLatencies.end(), 1224 WriteLatencies.begin(), 1225 WriteLatencies.end()); 1226 SchedTables.WriterNames.insert(SchedTables.WriterNames.end(), 1227 WriterNames.begin(), WriterNames.end()); 1228 } 1229 // ReadAdvanceEntries must remain in operand order. 1230 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size(); 1231 std::vector<MCReadAdvanceEntry>::iterator RAPos = 1232 std::search(SchedTables.ReadAdvanceEntries.begin(), 1233 SchedTables.ReadAdvanceEntries.end(), 1234 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end()); 1235 if (RAPos != SchedTables.ReadAdvanceEntries.end()) 1236 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin(); 1237 else { 1238 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size(); 1239 SchedTables.ReadAdvanceEntries.insert(RAPos, ReadAdvanceEntries.begin(), 1240 ReadAdvanceEntries.end()); 1241 } 1242 } 1243 } 1244 1245 // Emit SchedClass tables for all processors and associated global tables. 1246 void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables, 1247 raw_ostream &OS) { 1248 // Emit global WriteProcResTable. 1249 OS << "\n// {ProcResourceIdx, Cycles}\n" 1250 << "extern const llvm::MCWriteProcResEntry " 1251 << Target << "WriteProcResTable[] = {\n" 1252 << " { 0, 0}, // Invalid\n"; 1253 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size(); 1254 WPRIdx != WPREnd; ++WPRIdx) { 1255 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx]; 1256 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", " 1257 << format("%2d", WPREntry.Cycles) << "}"; 1258 if (WPRIdx + 1 < WPREnd) 1259 OS << ','; 1260 OS << " // #" << WPRIdx << '\n'; 1261 } 1262 OS << "}; // " << Target << "WriteProcResTable\n"; 1263 1264 // Emit global WriteLatencyTable. 1265 OS << "\n// {Cycles, WriteResourceID}\n" 1266 << "extern const llvm::MCWriteLatencyEntry " 1267 << Target << "WriteLatencyTable[] = {\n" 1268 << " { 0, 0}, // Invalid\n"; 1269 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size(); 1270 WLIdx != WLEnd; ++WLIdx) { 1271 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx]; 1272 OS << " {" << format("%2d", WLEntry.Cycles) << ", " 1273 << format("%2d", WLEntry.WriteResourceID) << "}"; 1274 if (WLIdx + 1 < WLEnd) 1275 OS << ','; 1276 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n'; 1277 } 1278 OS << "}; // " << Target << "WriteLatencyTable\n"; 1279 1280 // Emit global ReadAdvanceTable. 1281 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n" 1282 << "extern const llvm::MCReadAdvanceEntry " 1283 << Target << "ReadAdvanceTable[] = {\n" 1284 << " {0, 0, 0}, // Invalid\n"; 1285 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size(); 1286 RAIdx != RAEnd; ++RAIdx) { 1287 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx]; 1288 OS << " {" << RAEntry.UseIdx << ", " 1289 << format("%2d", RAEntry.WriteResourceID) << ", " 1290 << format("%2d", RAEntry.Cycles) << "}"; 1291 if (RAIdx + 1 < RAEnd) 1292 OS << ','; 1293 OS << " // #" << RAIdx << '\n'; 1294 } 1295 OS << "}; // " << Target << "ReadAdvanceTable\n"; 1296 1297 // Emit a SchedClass table for each processor. 1298 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(), 1299 PE = SchedModels.procModelEnd(); PI != PE; ++PI) { 1300 if (!PI->hasInstrSchedModel()) 1301 continue; 1302 1303 std::vector<MCSchedClassDesc> &SCTab = 1304 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())]; 1305 1306 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup," 1307 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n"; 1308 OS << "static const llvm::MCSchedClassDesc " 1309 << PI->ModelName << "SchedClasses[] = {\n"; 1310 1311 // The first class is always invalid. We no way to distinguish it except by 1312 // name and position. 1313 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel" 1314 && "invalid class not first"); 1315 OS << " {DBGFIELD(\"InvalidSchedClass\") " 1316 << MCSchedClassDesc::InvalidNumMicroOps 1317 << ", false, false, 0, 0, 0, 0, 0, 0},\n"; 1318 1319 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) { 1320 MCSchedClassDesc &MCDesc = SCTab[SCIdx]; 1321 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx); 1322 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") "; 1323 if (SchedClass.Name.size() < 18) 1324 OS.indent(18 - SchedClass.Name.size()); 1325 OS << MCDesc.NumMicroOps 1326 << ", " << ( MCDesc.BeginGroup ? "true" : "false" ) 1327 << ", " << ( MCDesc.EndGroup ? "true" : "false" ) 1328 << ", " << format("%2d", MCDesc.WriteProcResIdx) 1329 << ", " << MCDesc.NumWriteProcResEntries 1330 << ", " << format("%2d", MCDesc.WriteLatencyIdx) 1331 << ", " << MCDesc.NumWriteLatencyEntries 1332 << ", " << format("%2d", MCDesc.ReadAdvanceIdx) 1333 << ", " << MCDesc.NumReadAdvanceEntries 1334 << "}, // #" << SCIdx << '\n'; 1335 } 1336 OS << "}; // " << PI->ModelName << "SchedClasses\n"; 1337 } 1338 } 1339 1340 void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) { 1341 // For each processor model. 1342 for (const CodeGenProcModel &PM : SchedModels.procModels()) { 1343 // Emit extra processor info if available. 1344 if (PM.hasExtraProcessorInfo()) 1345 EmitExtraProcessorInfo(PM, OS); 1346 // Emit processor resource table. 1347 if (PM.hasInstrSchedModel()) 1348 EmitProcessorResources(PM, OS); 1349 else if(!PM.ProcResourceDefs.empty()) 1350 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines " 1351 "ProcResources without defining WriteRes SchedWriteRes"); 1352 1353 // Begin processor itinerary properties 1354 OS << "\n"; 1355 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n"; 1356 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ','); 1357 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ','); 1358 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ','); 1359 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ','); 1360 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ','); 1361 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ','); 1362 1363 bool PostRAScheduler = 1364 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false); 1365 1366 OS << " " << (PostRAScheduler ? "true" : "false") << ", // " 1367 << "PostRAScheduler\n"; 1368 1369 bool CompleteModel = 1370 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false); 1371 1372 OS << " " << (CompleteModel ? "true" : "false") << ", // " 1373 << "CompleteModel\n"; 1374 1375 OS << " " << PM.Index << ", // Processor ID\n"; 1376 if (PM.hasInstrSchedModel()) 1377 OS << " " << PM.ModelName << "ProcResources" << ",\n" 1378 << " " << PM.ModelName << "SchedClasses" << ",\n" 1379 << " " << PM.ProcResourceDefs.size()+1 << ",\n" 1380 << " " << (SchedModels.schedClassEnd() 1381 - SchedModels.schedClassBegin()) << ",\n"; 1382 else 1383 OS << " nullptr, nullptr, 0, 0," 1384 << " // No instruction-level machine model.\n"; 1385 if (PM.hasItineraries()) 1386 OS << " " << PM.ItinsDef->getName() << ",\n"; 1387 else 1388 OS << " nullptr, // No Itinerary\n"; 1389 if (PM.hasExtraProcessorInfo()) 1390 OS << " &" << PM.ModelName << "ExtraInfo,\n"; 1391 else 1392 OS << " nullptr // No extra processor descriptor\n"; 1393 OS << "};\n"; 1394 } 1395 } 1396 1397 // 1398 // EmitSchedModel - Emits all scheduling model tables, folding common patterns. 1399 // 1400 void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) { 1401 OS << "#ifdef DBGFIELD\n" 1402 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n" 1403 << "#endif\n" 1404 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n" 1405 << "#define DBGFIELD(x) x,\n" 1406 << "#else\n" 1407 << "#define DBGFIELD(x)\n" 1408 << "#endif\n"; 1409 1410 if (SchedModels.hasItineraries()) { 1411 std::vector<std::vector<InstrItinerary>> ProcItinLists; 1412 // Emit the stage data 1413 EmitStageAndOperandCycleData(OS, ProcItinLists); 1414 EmitItineraries(OS, ProcItinLists); 1415 } 1416 OS << "\n// ===============================================================\n" 1417 << "// Data tables for the new per-operand machine model.\n"; 1418 1419 SchedClassTables SchedTables; 1420 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) { 1421 GenSchedClassTables(ProcModel, SchedTables); 1422 } 1423 EmitSchedClassTables(SchedTables, OS); 1424 1425 OS << "\n#undef DBGFIELD\n"; 1426 1427 // Emit the processor machine model 1428 EmitProcessorModels(OS); 1429 } 1430 1431 static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) { 1432 std::string Buffer; 1433 raw_string_ostream Stream(Buffer); 1434 1435 // Collect all the PredicateProlog records and print them to the output 1436 // stream. 1437 std::vector<Record *> Prologs = 1438 Records.getAllDerivedDefinitions("PredicateProlog"); 1439 llvm::sort(Prologs, LessRecord()); 1440 for (Record *P : Prologs) 1441 Stream << P->getValueAsString("Code") << '\n'; 1442 1443 Stream.flush(); 1444 OS << Buffer; 1445 } 1446 1447 static bool isTruePredicate(const Record *Rec) { 1448 return Rec->isSubClassOf("MCSchedPredicate") && 1449 Rec->getValueAsDef("Pred")->isSubClassOf("MCTrue"); 1450 } 1451 1452 static void emitPredicates(const CodeGenSchedTransition &T, 1453 const CodeGenSchedClass &SC, PredicateExpander &PE, 1454 raw_ostream &OS) { 1455 std::string Buffer; 1456 raw_string_ostream SS(Buffer); 1457 1458 // If not all predicates are MCTrue, then we need an if-stmt. 1459 unsigned NumNonTruePreds = 1460 T.PredTerm.size() - count_if(T.PredTerm, isTruePredicate); 1461 1462 SS.indent(PE.getIndentLevel() * 2); 1463 1464 if (NumNonTruePreds) { 1465 bool FirstNonTruePredicate = true; 1466 SS << "if ("; 1467 1468 PE.setIndentLevel(PE.getIndentLevel() + 2); 1469 1470 for (const Record *Rec : T.PredTerm) { 1471 // Skip predicates that evaluate to "true". 1472 if (isTruePredicate(Rec)) 1473 continue; 1474 1475 if (FirstNonTruePredicate) { 1476 FirstNonTruePredicate = false; 1477 } else { 1478 SS << "\n"; 1479 SS.indent(PE.getIndentLevel() * 2); 1480 SS << "&& "; 1481 } 1482 1483 if (Rec->isSubClassOf("MCSchedPredicate")) { 1484 PE.expandPredicate(SS, Rec->getValueAsDef("Pred")); 1485 continue; 1486 } 1487 1488 // Expand this legacy predicate and wrap it around braces if there is more 1489 // than one predicate to expand. 1490 SS << ((NumNonTruePreds > 1) ? "(" : "") 1491 << Rec->getValueAsString("Predicate") 1492 << ((NumNonTruePreds > 1) ? ")" : ""); 1493 } 1494 1495 SS << ")\n"; // end of if-stmt 1496 PE.decreaseIndentLevel(); 1497 SS.indent(PE.getIndentLevel() * 2); 1498 PE.decreaseIndentLevel(); 1499 } 1500 1501 SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n'; 1502 SS.flush(); 1503 OS << Buffer; 1504 } 1505 1506 // Used by method `SubtargetEmitter::emitSchedModelHelpersImpl()` to generate 1507 // epilogue code for the auto-generated helper. 1508 static void emitSchedModelHelperEpilogue(raw_ostream &OS, 1509 bool ShouldReturnZero) { 1510 if (ShouldReturnZero) { 1511 OS << " // Don't know how to resolve this scheduling class.\n" 1512 << " return 0;\n"; 1513 return; 1514 } 1515 1516 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"; 1517 } 1518 1519 static bool hasMCSchedPredicates(const CodeGenSchedTransition &T) { 1520 return all_of(T.PredTerm, [](const Record *Rec) { 1521 return Rec->isSubClassOf("MCSchedPredicate"); 1522 }); 1523 } 1524 1525 static void collectVariantClasses(const CodeGenSchedModels &SchedModels, 1526 IdxVec &VariantClasses, 1527 bool OnlyExpandMCInstPredicates) { 1528 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) { 1529 // Ignore non-variant scheduling classes. 1530 if (SC.Transitions.empty()) 1531 continue; 1532 1533 if (OnlyExpandMCInstPredicates) { 1534 // Ignore this variant scheduling class no transitions use any meaningful 1535 // MCSchedPredicate definitions. 1536 if (!any_of(SC.Transitions, [](const CodeGenSchedTransition &T) { 1537 return hasMCSchedPredicates(T); 1538 })) 1539 continue; 1540 } 1541 1542 VariantClasses.push_back(SC.Index); 1543 } 1544 } 1545 1546 static void collectProcessorIndices(const CodeGenSchedClass &SC, 1547 IdxVec &ProcIndices) { 1548 // A variant scheduling class may define transitions for multiple 1549 // processors. This function identifies wich processors are associated with 1550 // transition rules specified by variant class `SC`. 1551 for (const CodeGenSchedTransition &T : SC.Transitions) { 1552 IdxVec PI; 1553 std::set_union(&T.ProcIndex, &T.ProcIndex + 1, ProcIndices.begin(), 1554 ProcIndices.end(), std::back_inserter(PI)); 1555 ProcIndices.swap(PI); 1556 } 1557 } 1558 1559 static bool isAlwaysTrue(const CodeGenSchedTransition &T) { 1560 return llvm::all_of(T.PredTerm, 1561 [](const Record *R) { return isTruePredicate(R); }); 1562 } 1563 1564 void SubtargetEmitter::emitSchedModelHelpersImpl( 1565 raw_ostream &OS, bool OnlyExpandMCInstPredicates) { 1566 IdxVec VariantClasses; 1567 collectVariantClasses(SchedModels, VariantClasses, 1568 OnlyExpandMCInstPredicates); 1569 1570 if (VariantClasses.empty()) { 1571 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates); 1572 return; 1573 } 1574 1575 // Construct a switch statement where the condition is a check on the 1576 // scheduling class identifier. There is a `case` for every variant class 1577 // defined by the processor models of this target. 1578 // Each `case` implements a number of rules to resolve (i.e. to transition from) 1579 // a variant scheduling class to another scheduling class. Rules are 1580 // described by instances of CodeGenSchedTransition. Note that transitions may 1581 // not be valid for all processors. 1582 OS << " switch (SchedClass) {\n"; 1583 for (unsigned VC : VariantClasses) { 1584 IdxVec ProcIndices; 1585 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC); 1586 collectProcessorIndices(SC, ProcIndices); 1587 1588 OS << " case " << VC << ": // " << SC.Name << '\n'; 1589 1590 PredicateExpander PE(Target); 1591 PE.setByRef(false); 1592 PE.setExpandForMC(OnlyExpandMCInstPredicates); 1593 for (unsigned PI : ProcIndices) { 1594 OS << " "; 1595 1596 // Emit a guard on the processor ID. 1597 if (PI != 0) { 1598 OS << (OnlyExpandMCInstPredicates 1599 ? "if (CPUID == " 1600 : "if (SchedModel->getProcessorID() == "); 1601 OS << PI << ") "; 1602 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName << '\n'; 1603 } 1604 1605 // Now emit transitions associated with processor PI. 1606 const CodeGenSchedTransition *FinalT = nullptr; 1607 for (const CodeGenSchedTransition &T : SC.Transitions) { 1608 if (PI != 0 && T.ProcIndex != PI) 1609 continue; 1610 1611 // Emit only transitions based on MCSchedPredicate, if it's the case. 1612 // At least the transition specified by NoSchedPred is emitted, 1613 // which becomes the default transition for those variants otherwise 1614 // not based on MCSchedPredicate. 1615 // FIXME: preferably, llvm-mca should instead assume a reasonable 1616 // default when a variant transition is not based on MCSchedPredicate 1617 // for a given processor. 1618 if (OnlyExpandMCInstPredicates && !hasMCSchedPredicates(T)) 1619 continue; 1620 1621 // If transition is folded to 'return X' it should be the last one. 1622 if (isAlwaysTrue(T)) { 1623 FinalT = &T; 1624 continue; 1625 } 1626 PE.setIndentLevel(3); 1627 emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS); 1628 } 1629 if (FinalT) 1630 emitPredicates(*FinalT, SchedModels.getSchedClass(FinalT->ToClassIdx), 1631 PE, OS); 1632 1633 OS << " }\n"; 1634 1635 if (PI == 0) 1636 break; 1637 } 1638 1639 if (SC.isInferred()) 1640 OS << " return " << SC.Index << ";\n"; 1641 OS << " break;\n"; 1642 } 1643 1644 OS << " };\n"; 1645 1646 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates); 1647 } 1648 1649 void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName, 1650 raw_ostream &OS) { 1651 OS << "unsigned " << ClassName 1652 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI," 1653 << " const TargetSchedModel *SchedModel) const {\n"; 1654 1655 // Emit the predicate prolog code. 1656 emitPredicateProlog(Records, OS); 1657 1658 // Emit target predicates. 1659 emitSchedModelHelpersImpl(OS); 1660 1661 OS << "} // " << ClassName << "::resolveSchedClass\n\n"; 1662 1663 OS << "unsigned " << ClassName 1664 << "\n::resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI," 1665 << " const MCInstrInfo *MCII, unsigned CPUID) const {\n" 1666 << " return " << Target << "_MC" 1667 << "::resolveVariantSchedClassImpl(SchedClass, MI, MCII, CPUID);\n" 1668 << "} // " << ClassName << "::resolveVariantSchedClass\n\n"; 1669 1670 STIPredicateExpander PE(Target); 1671 PE.setClassPrefix(ClassName); 1672 PE.setExpandDefinition(true); 1673 PE.setByRef(false); 1674 PE.setIndentLevel(0); 1675 1676 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates()) 1677 PE.expandSTIPredicate(OS, Fn); 1678 } 1679 1680 void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName, 1681 raw_ostream &OS) { 1682 const CodeGenHwModes &CGH = TGT.getHwModes(); 1683 assert(CGH.getNumModeIds() > 0); 1684 if (CGH.getNumModeIds() == 1) 1685 return; 1686 1687 OS << "unsigned " << ClassName << "::getHwMode() const {\n"; 1688 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) { 1689 const HwMode &HM = CGH.getMode(M); 1690 OS << " if (checkFeatures(\"" << HM.Features 1691 << "\")) return " << M << ";\n"; 1692 } 1693 OS << " return 0;\n}\n"; 1694 } 1695 1696 // 1697 // ParseFeaturesFunction - Produces a subtarget specific function for parsing 1698 // the subtarget features string. 1699 // 1700 void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS, 1701 unsigned NumFeatures, 1702 unsigned NumProcs) { 1703 std::vector<Record*> Features = 1704 Records.getAllDerivedDefinitions("SubtargetFeature"); 1705 llvm::sort(Features, LessRecord()); 1706 1707 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n" 1708 << "// subtarget options.\n" 1709 << "void llvm::"; 1710 OS << Target; 1711 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, " 1712 << "StringRef FS) {\n" 1713 << " LLVM_DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n" 1714 << " LLVM_DEBUG(dbgs() << \"\\nCPU:\" << CPU);\n" 1715 << " LLVM_DEBUG(dbgs() << \"\\nTuneCPU:\" << TuneCPU << \"\\n\\n\");\n"; 1716 1717 if (Features.empty()) { 1718 OS << "}\n"; 1719 return; 1720 } 1721 1722 OS << " InitMCProcessorInfo(CPU, TuneCPU, FS);\n" 1723 << " const FeatureBitset &Bits = getFeatureBits();\n"; 1724 1725 for (Record *R : Features) { 1726 // Next record 1727 StringRef Instance = R->getName(); 1728 StringRef Value = R->getValueAsString("Value"); 1729 StringRef Attribute = R->getValueAsString("Attribute"); 1730 1731 if (Value=="true" || Value=="false") 1732 OS << " if (Bits[" << Target << "::" 1733 << Instance << "]) " 1734 << Attribute << " = " << Value << ";\n"; 1735 else 1736 OS << " if (Bits[" << Target << "::" 1737 << Instance << "] && " 1738 << Attribute << " < " << Value << ") " 1739 << Attribute << " = " << Value << ";\n"; 1740 } 1741 1742 OS << "}\n"; 1743 } 1744 1745 void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) { 1746 OS << "namespace " << Target << "_MC {\n" 1747 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n" 1748 << " const MCInst *MI, const MCInstrInfo *MCII, unsigned CPUID) {\n"; 1749 emitSchedModelHelpersImpl(OS, /* OnlyExpandMCPredicates */ true); 1750 OS << "}\n"; 1751 OS << "} // end namespace " << Target << "_MC\n\n"; 1752 1753 OS << "struct " << Target 1754 << "GenMCSubtargetInfo : public MCSubtargetInfo {\n"; 1755 OS << " " << Target << "GenMCSubtargetInfo(const Triple &TT,\n" 1756 << " StringRef CPU, StringRef TuneCPU, StringRef FS,\n" 1757 << " ArrayRef<SubtargetFeatureKV> PF,\n" 1758 << " ArrayRef<SubtargetSubTypeKV> PD,\n" 1759 << " const MCWriteProcResEntry *WPR,\n" 1760 << " const MCWriteLatencyEntry *WL,\n" 1761 << " const MCReadAdvanceEntry *RA, const InstrStage *IS,\n" 1762 << " const unsigned *OC, const unsigned *FP) :\n" 1763 << " MCSubtargetInfo(TT, CPU, TuneCPU, FS, PF, PD,\n" 1764 << " WPR, WL, RA, IS, OC, FP) { }\n\n" 1765 << " unsigned resolveVariantSchedClass(unsigned SchedClass,\n" 1766 << " const MCInst *MI, const MCInstrInfo *MCII,\n" 1767 << " unsigned CPUID) const override {\n" 1768 << " return " << Target << "_MC" 1769 << "::resolveVariantSchedClassImpl(SchedClass, MI, MCII, CPUID);\n"; 1770 OS << " }\n"; 1771 if (TGT.getHwModes().getNumModeIds() > 1) 1772 OS << " unsigned getHwMode() const override;\n"; 1773 OS << "};\n"; 1774 EmitHwModeCheck(Target + "GenMCSubtargetInfo", OS); 1775 } 1776 1777 void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) { 1778 OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n"; 1779 OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n"; 1780 1781 STIPredicateExpander PE(Target); 1782 PE.setExpandForMC(true); 1783 PE.setByRef(true); 1784 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates()) 1785 PE.expandSTIPredicate(OS, Fn); 1786 1787 OS << "#endif // GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n"; 1788 1789 OS << "\n#ifdef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n"; 1790 OS << "#undef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n"; 1791 1792 std::string ClassPrefix = Target + "MCInstrAnalysis"; 1793 PE.setExpandDefinition(true); 1794 PE.setClassPrefix(ClassPrefix); 1795 PE.setIndentLevel(0); 1796 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates()) 1797 PE.expandSTIPredicate(OS, Fn); 1798 1799 OS << "#endif // GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n"; 1800 } 1801 1802 // 1803 // SubtargetEmitter::run - Main subtarget enumeration emitter. 1804 // 1805 void SubtargetEmitter::run(raw_ostream &OS) { 1806 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS); 1807 1808 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n"; 1809 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n"; 1810 1811 DenseMap<Record *, unsigned> FeatureMap; 1812 1813 OS << "namespace llvm {\n"; 1814 Enumeration(OS, FeatureMap); 1815 OS << "} // end namespace llvm\n\n"; 1816 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n"; 1817 1818 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n"; 1819 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n"; 1820 1821 OS << "namespace llvm {\n"; 1822 #if 0 1823 OS << "namespace {\n"; 1824 #endif 1825 unsigned NumFeatures = FeatureKeyValues(OS, FeatureMap); 1826 OS << "\n"; 1827 EmitSchedModel(OS); 1828 OS << "\n"; 1829 unsigned NumProcs = CPUKeyValues(OS, FeatureMap); 1830 OS << "\n"; 1831 #if 0 1832 OS << "} // end anonymous namespace\n\n"; 1833 #endif 1834 1835 // MCInstrInfo initialization routine. 1836 emitGenMCSubtargetInfo(OS); 1837 1838 OS << "\nstatic inline MCSubtargetInfo *create" << Target 1839 << "MCSubtargetInfoImpl(" 1840 << "const Triple &TT, StringRef CPU, StringRef TuneCPU, StringRef FS) {\n"; 1841 OS << " return new " << Target 1842 << "GenMCSubtargetInfo(TT, CPU, TuneCPU, FS, "; 1843 if (NumFeatures) 1844 OS << Target << "FeatureKV, "; 1845 else 1846 OS << "None, "; 1847 if (NumProcs) 1848 OS << Target << "SubTypeKV, "; 1849 else 1850 OS << "None, "; 1851 OS << '\n'; OS.indent(22); 1852 OS << Target << "WriteProcResTable, " 1853 << Target << "WriteLatencyTable, " 1854 << Target << "ReadAdvanceTable, "; 1855 OS << '\n'; OS.indent(22); 1856 if (SchedModels.hasItineraries()) { 1857 OS << Target << "Stages, " 1858 << Target << "OperandCycles, " 1859 << Target << "ForwardingPaths"; 1860 } else 1861 OS << "nullptr, nullptr, nullptr"; 1862 OS << ");\n}\n\n"; 1863 1864 OS << "} // end namespace llvm\n\n"; 1865 1866 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n"; 1867 1868 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n"; 1869 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n"; 1870 1871 OS << "#include \"llvm/Support/Debug.h\"\n"; 1872 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n"; 1873 ParseFeaturesFunction(OS, NumFeatures, NumProcs); 1874 1875 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n"; 1876 1877 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization. 1878 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n"; 1879 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n"; 1880 1881 std::string ClassName = Target + "GenSubtargetInfo"; 1882 OS << "namespace llvm {\n"; 1883 OS << "class DFAPacketizer;\n"; 1884 OS << "namespace " << Target << "_MC {\n" 1885 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass," 1886 << " const MCInst *MI, const MCInstrInfo *MCII, unsigned CPUID);\n" 1887 << "} // end namespace " << Target << "_MC\n\n"; 1888 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n" 1889 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, " 1890 << "StringRef TuneCPU, StringRef FS);\n" 1891 << "public:\n" 1892 << " unsigned resolveSchedClass(unsigned SchedClass, " 1893 << " const MachineInstr *DefMI," 1894 << " const TargetSchedModel *SchedModel) const override;\n" 1895 << " unsigned resolveVariantSchedClass(unsigned SchedClass," 1896 << " const MCInst *MI, const MCInstrInfo *MCII," 1897 << " unsigned CPUID) const override;\n" 1898 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)" 1899 << " const;\n"; 1900 if (TGT.getHwModes().getNumModeIds() > 1) 1901 OS << " unsigned getHwMode() const override;\n"; 1902 1903 STIPredicateExpander PE(Target); 1904 PE.setByRef(false); 1905 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates()) 1906 PE.expandSTIPredicate(OS, Fn); 1907 1908 OS << "};\n" 1909 << "} // end namespace llvm\n\n"; 1910 1911 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n"; 1912 1913 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n"; 1914 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n"; 1915 1916 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n"; 1917 OS << "namespace llvm {\n"; 1918 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n"; 1919 OS << "extern const llvm::SubtargetSubTypeKV " << Target << "SubTypeKV[];\n"; 1920 OS << "extern const llvm::MCWriteProcResEntry " 1921 << Target << "WriteProcResTable[];\n"; 1922 OS << "extern const llvm::MCWriteLatencyEntry " 1923 << Target << "WriteLatencyTable[];\n"; 1924 OS << "extern const llvm::MCReadAdvanceEntry " 1925 << Target << "ReadAdvanceTable[];\n"; 1926 1927 if (SchedModels.hasItineraries()) { 1928 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n"; 1929 OS << "extern const unsigned " << Target << "OperandCycles[];\n"; 1930 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n"; 1931 } 1932 1933 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, " 1934 << "StringRef TuneCPU, StringRef FS)\n" 1935 << " : TargetSubtargetInfo(TT, CPU, TuneCPU, FS, "; 1936 if (NumFeatures) 1937 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), "; 1938 else 1939 OS << "None, "; 1940 if (NumProcs) 1941 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), "; 1942 else 1943 OS << "None, "; 1944 OS << '\n'; OS.indent(24); 1945 OS << Target << "WriteProcResTable, " 1946 << Target << "WriteLatencyTable, " 1947 << Target << "ReadAdvanceTable, "; 1948 OS << '\n'; OS.indent(24); 1949 if (SchedModels.hasItineraries()) { 1950 OS << Target << "Stages, " 1951 << Target << "OperandCycles, " 1952 << Target << "ForwardingPaths"; 1953 } else 1954 OS << "nullptr, nullptr, nullptr"; 1955 OS << ") {}\n\n"; 1956 1957 EmitSchedModelHelpers(ClassName, OS); 1958 EmitHwModeCheck(ClassName, OS); 1959 1960 OS << "} // end namespace llvm\n\n"; 1961 1962 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n"; 1963 1964 EmitMCInstrAnalysisPredicateFunctions(OS); 1965 } 1966 1967 namespace llvm { 1968 1969 void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) { 1970 CodeGenTarget CGTarget(RK); 1971 SubtargetEmitter(RK, CGTarget).run(OS); 1972 } 1973 1974 } // end namespace llvm 1975