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 llvm::append_range(SchedTables.WriteLatencies, WriteLatencies); 1224 llvm::append_range(SchedTables.WriterNames, WriterNames); 1225 } 1226 // ReadAdvanceEntries must remain in operand order. 1227 SCDesc.NumReadAdvanceEntries = ReadAdvanceEntries.size(); 1228 std::vector<MCReadAdvanceEntry>::iterator RAPos = 1229 std::search(SchedTables.ReadAdvanceEntries.begin(), 1230 SchedTables.ReadAdvanceEntries.end(), 1231 ReadAdvanceEntries.begin(), ReadAdvanceEntries.end()); 1232 if (RAPos != SchedTables.ReadAdvanceEntries.end()) 1233 SCDesc.ReadAdvanceIdx = RAPos - SchedTables.ReadAdvanceEntries.begin(); 1234 else { 1235 SCDesc.ReadAdvanceIdx = SchedTables.ReadAdvanceEntries.size(); 1236 llvm::append_range(SchedTables.ReadAdvanceEntries, ReadAdvanceEntries); 1237 } 1238 } 1239 } 1240 1241 // Emit SchedClass tables for all processors and associated global tables. 1242 void SubtargetEmitter::EmitSchedClassTables(SchedClassTables &SchedTables, 1243 raw_ostream &OS) { 1244 // Emit global WriteProcResTable. 1245 OS << "\n// {ProcResourceIdx, Cycles}\n" 1246 << "extern const llvm::MCWriteProcResEntry " 1247 << Target << "WriteProcResTable[] = {\n" 1248 << " { 0, 0}, // Invalid\n"; 1249 for (unsigned WPRIdx = 1, WPREnd = SchedTables.WriteProcResources.size(); 1250 WPRIdx != WPREnd; ++WPRIdx) { 1251 MCWriteProcResEntry &WPREntry = SchedTables.WriteProcResources[WPRIdx]; 1252 OS << " {" << format("%2d", WPREntry.ProcResourceIdx) << ", " 1253 << format("%2d", WPREntry.Cycles) << "}"; 1254 if (WPRIdx + 1 < WPREnd) 1255 OS << ','; 1256 OS << " // #" << WPRIdx << '\n'; 1257 } 1258 OS << "}; // " << Target << "WriteProcResTable\n"; 1259 1260 // Emit global WriteLatencyTable. 1261 OS << "\n// {Cycles, WriteResourceID}\n" 1262 << "extern const llvm::MCWriteLatencyEntry " 1263 << Target << "WriteLatencyTable[] = {\n" 1264 << " { 0, 0}, // Invalid\n"; 1265 for (unsigned WLIdx = 1, WLEnd = SchedTables.WriteLatencies.size(); 1266 WLIdx != WLEnd; ++WLIdx) { 1267 MCWriteLatencyEntry &WLEntry = SchedTables.WriteLatencies[WLIdx]; 1268 OS << " {" << format("%2d", WLEntry.Cycles) << ", " 1269 << format("%2d", WLEntry.WriteResourceID) << "}"; 1270 if (WLIdx + 1 < WLEnd) 1271 OS << ','; 1272 OS << " // #" << WLIdx << " " << SchedTables.WriterNames[WLIdx] << '\n'; 1273 } 1274 OS << "}; // " << Target << "WriteLatencyTable\n"; 1275 1276 // Emit global ReadAdvanceTable. 1277 OS << "\n// {UseIdx, WriteResourceID, Cycles}\n" 1278 << "extern const llvm::MCReadAdvanceEntry " 1279 << Target << "ReadAdvanceTable[] = {\n" 1280 << " {0, 0, 0}, // Invalid\n"; 1281 for (unsigned RAIdx = 1, RAEnd = SchedTables.ReadAdvanceEntries.size(); 1282 RAIdx != RAEnd; ++RAIdx) { 1283 MCReadAdvanceEntry &RAEntry = SchedTables.ReadAdvanceEntries[RAIdx]; 1284 OS << " {" << RAEntry.UseIdx << ", " 1285 << format("%2d", RAEntry.WriteResourceID) << ", " 1286 << format("%2d", RAEntry.Cycles) << "}"; 1287 if (RAIdx + 1 < RAEnd) 1288 OS << ','; 1289 OS << " // #" << RAIdx << '\n'; 1290 } 1291 OS << "}; // " << Target << "ReadAdvanceTable\n"; 1292 1293 // Emit a SchedClass table for each processor. 1294 for (CodeGenSchedModels::ProcIter PI = SchedModels.procModelBegin(), 1295 PE = SchedModels.procModelEnd(); PI != PE; ++PI) { 1296 if (!PI->hasInstrSchedModel()) 1297 continue; 1298 1299 std::vector<MCSchedClassDesc> &SCTab = 1300 SchedTables.ProcSchedClasses[1 + (PI - SchedModels.procModelBegin())]; 1301 1302 OS << "\n// {Name, NumMicroOps, BeginGroup, EndGroup," 1303 << " WriteProcResIdx,#, WriteLatencyIdx,#, ReadAdvanceIdx,#}\n"; 1304 OS << "static const llvm::MCSchedClassDesc " 1305 << PI->ModelName << "SchedClasses[] = {\n"; 1306 1307 // The first class is always invalid. We no way to distinguish it except by 1308 // name and position. 1309 assert(SchedModels.getSchedClass(0).Name == "NoInstrModel" 1310 && "invalid class not first"); 1311 OS << " {DBGFIELD(\"InvalidSchedClass\") " 1312 << MCSchedClassDesc::InvalidNumMicroOps 1313 << ", false, false, 0, 0, 0, 0, 0, 0},\n"; 1314 1315 for (unsigned SCIdx = 1, SCEnd = SCTab.size(); SCIdx != SCEnd; ++SCIdx) { 1316 MCSchedClassDesc &MCDesc = SCTab[SCIdx]; 1317 const CodeGenSchedClass &SchedClass = SchedModels.getSchedClass(SCIdx); 1318 OS << " {DBGFIELD(\"" << SchedClass.Name << "\") "; 1319 if (SchedClass.Name.size() < 18) 1320 OS.indent(18 - SchedClass.Name.size()); 1321 OS << MCDesc.NumMicroOps 1322 << ", " << ( MCDesc.BeginGroup ? "true" : "false" ) 1323 << ", " << ( MCDesc.EndGroup ? "true" : "false" ) 1324 << ", " << format("%2d", MCDesc.WriteProcResIdx) 1325 << ", " << MCDesc.NumWriteProcResEntries 1326 << ", " << format("%2d", MCDesc.WriteLatencyIdx) 1327 << ", " << MCDesc.NumWriteLatencyEntries 1328 << ", " << format("%2d", MCDesc.ReadAdvanceIdx) 1329 << ", " << MCDesc.NumReadAdvanceEntries 1330 << "}, // #" << SCIdx << '\n'; 1331 } 1332 OS << "}; // " << PI->ModelName << "SchedClasses\n"; 1333 } 1334 } 1335 1336 void SubtargetEmitter::EmitProcessorModels(raw_ostream &OS) { 1337 // For each processor model. 1338 for (const CodeGenProcModel &PM : SchedModels.procModels()) { 1339 // Emit extra processor info if available. 1340 if (PM.hasExtraProcessorInfo()) 1341 EmitExtraProcessorInfo(PM, OS); 1342 // Emit processor resource table. 1343 if (PM.hasInstrSchedModel()) 1344 EmitProcessorResources(PM, OS); 1345 else if(!PM.ProcResourceDefs.empty()) 1346 PrintFatalError(PM.ModelDef->getLoc(), "SchedMachineModel defines " 1347 "ProcResources without defining WriteRes SchedWriteRes"); 1348 1349 // Begin processor itinerary properties 1350 OS << "\n"; 1351 OS << "static const llvm::MCSchedModel " << PM.ModelName << " = {\n"; 1352 EmitProcessorProp(OS, PM.ModelDef, "IssueWidth", ','); 1353 EmitProcessorProp(OS, PM.ModelDef, "MicroOpBufferSize", ','); 1354 EmitProcessorProp(OS, PM.ModelDef, "LoopMicroOpBufferSize", ','); 1355 EmitProcessorProp(OS, PM.ModelDef, "LoadLatency", ','); 1356 EmitProcessorProp(OS, PM.ModelDef, "HighLatency", ','); 1357 EmitProcessorProp(OS, PM.ModelDef, "MispredictPenalty", ','); 1358 1359 bool PostRAScheduler = 1360 (PM.ModelDef ? PM.ModelDef->getValueAsBit("PostRAScheduler") : false); 1361 1362 OS << " " << (PostRAScheduler ? "true" : "false") << ", // " 1363 << "PostRAScheduler\n"; 1364 1365 bool CompleteModel = 1366 (PM.ModelDef ? PM.ModelDef->getValueAsBit("CompleteModel") : false); 1367 1368 OS << " " << (CompleteModel ? "true" : "false") << ", // " 1369 << "CompleteModel\n"; 1370 1371 OS << " " << PM.Index << ", // Processor ID\n"; 1372 if (PM.hasInstrSchedModel()) 1373 OS << " " << PM.ModelName << "ProcResources" << ",\n" 1374 << " " << PM.ModelName << "SchedClasses" << ",\n" 1375 << " " << PM.ProcResourceDefs.size()+1 << ",\n" 1376 << " " << (SchedModels.schedClassEnd() 1377 - SchedModels.schedClassBegin()) << ",\n"; 1378 else 1379 OS << " nullptr, nullptr, 0, 0," 1380 << " // No instruction-level machine model.\n"; 1381 if (PM.hasItineraries()) 1382 OS << " " << PM.ItinsDef->getName() << ",\n"; 1383 else 1384 OS << " nullptr, // No Itinerary\n"; 1385 if (PM.hasExtraProcessorInfo()) 1386 OS << " &" << PM.ModelName << "ExtraInfo,\n"; 1387 else 1388 OS << " nullptr // No extra processor descriptor\n"; 1389 OS << "};\n"; 1390 } 1391 } 1392 1393 // 1394 // EmitSchedModel - Emits all scheduling model tables, folding common patterns. 1395 // 1396 void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) { 1397 OS << "#ifdef DBGFIELD\n" 1398 << "#error \"<target>GenSubtargetInfo.inc requires a DBGFIELD macro\"\n" 1399 << "#endif\n" 1400 << "#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n" 1401 << "#define DBGFIELD(x) x,\n" 1402 << "#else\n" 1403 << "#define DBGFIELD(x)\n" 1404 << "#endif\n"; 1405 1406 if (SchedModels.hasItineraries()) { 1407 std::vector<std::vector<InstrItinerary>> ProcItinLists; 1408 // Emit the stage data 1409 EmitStageAndOperandCycleData(OS, ProcItinLists); 1410 EmitItineraries(OS, ProcItinLists); 1411 } 1412 OS << "\n// ===============================================================\n" 1413 << "// Data tables for the new per-operand machine model.\n"; 1414 1415 SchedClassTables SchedTables; 1416 for (const CodeGenProcModel &ProcModel : SchedModels.procModels()) { 1417 GenSchedClassTables(ProcModel, SchedTables); 1418 } 1419 EmitSchedClassTables(SchedTables, OS); 1420 1421 OS << "\n#undef DBGFIELD\n"; 1422 1423 // Emit the processor machine model 1424 EmitProcessorModels(OS); 1425 } 1426 1427 static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) { 1428 std::string Buffer; 1429 raw_string_ostream Stream(Buffer); 1430 1431 // Collect all the PredicateProlog records and print them to the output 1432 // stream. 1433 std::vector<Record *> Prologs = 1434 Records.getAllDerivedDefinitions("PredicateProlog"); 1435 llvm::sort(Prologs, LessRecord()); 1436 for (Record *P : Prologs) 1437 Stream << P->getValueAsString("Code") << '\n'; 1438 1439 Stream.flush(); 1440 OS << Buffer; 1441 } 1442 1443 static bool isTruePredicate(const Record *Rec) { 1444 return Rec->isSubClassOf("MCSchedPredicate") && 1445 Rec->getValueAsDef("Pred")->isSubClassOf("MCTrue"); 1446 } 1447 1448 static void emitPredicates(const CodeGenSchedTransition &T, 1449 const CodeGenSchedClass &SC, PredicateExpander &PE, 1450 raw_ostream &OS) { 1451 std::string Buffer; 1452 raw_string_ostream SS(Buffer); 1453 1454 // If not all predicates are MCTrue, then we need an if-stmt. 1455 unsigned NumNonTruePreds = 1456 T.PredTerm.size() - count_if(T.PredTerm, isTruePredicate); 1457 1458 SS.indent(PE.getIndentLevel() * 2); 1459 1460 if (NumNonTruePreds) { 1461 bool FirstNonTruePredicate = true; 1462 SS << "if ("; 1463 1464 PE.setIndentLevel(PE.getIndentLevel() + 2); 1465 1466 for (const Record *Rec : T.PredTerm) { 1467 // Skip predicates that evaluate to "true". 1468 if (isTruePredicate(Rec)) 1469 continue; 1470 1471 if (FirstNonTruePredicate) { 1472 FirstNonTruePredicate = false; 1473 } else { 1474 SS << "\n"; 1475 SS.indent(PE.getIndentLevel() * 2); 1476 SS << "&& "; 1477 } 1478 1479 if (Rec->isSubClassOf("MCSchedPredicate")) { 1480 PE.expandPredicate(SS, Rec->getValueAsDef("Pred")); 1481 continue; 1482 } 1483 1484 // Expand this legacy predicate and wrap it around braces if there is more 1485 // than one predicate to expand. 1486 SS << ((NumNonTruePreds > 1) ? "(" : "") 1487 << Rec->getValueAsString("Predicate") 1488 << ((NumNonTruePreds > 1) ? ")" : ""); 1489 } 1490 1491 SS << ")\n"; // end of if-stmt 1492 PE.decreaseIndentLevel(); 1493 SS.indent(PE.getIndentLevel() * 2); 1494 PE.decreaseIndentLevel(); 1495 } 1496 1497 SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n'; 1498 SS.flush(); 1499 OS << Buffer; 1500 } 1501 1502 // Used by method `SubtargetEmitter::emitSchedModelHelpersImpl()` to generate 1503 // epilogue code for the auto-generated helper. 1504 static void emitSchedModelHelperEpilogue(raw_ostream &OS, 1505 bool ShouldReturnZero) { 1506 if (ShouldReturnZero) { 1507 OS << " // Don't know how to resolve this scheduling class.\n" 1508 << " return 0;\n"; 1509 return; 1510 } 1511 1512 OS << " report_fatal_error(\"Expected a variant SchedClass\");\n"; 1513 } 1514 1515 static bool hasMCSchedPredicates(const CodeGenSchedTransition &T) { 1516 return all_of(T.PredTerm, [](const Record *Rec) { 1517 return Rec->isSubClassOf("MCSchedPredicate"); 1518 }); 1519 } 1520 1521 static void collectVariantClasses(const CodeGenSchedModels &SchedModels, 1522 IdxVec &VariantClasses, 1523 bool OnlyExpandMCInstPredicates) { 1524 for (const CodeGenSchedClass &SC : SchedModels.schedClasses()) { 1525 // Ignore non-variant scheduling classes. 1526 if (SC.Transitions.empty()) 1527 continue; 1528 1529 if (OnlyExpandMCInstPredicates) { 1530 // Ignore this variant scheduling class no transitions use any meaningful 1531 // MCSchedPredicate definitions. 1532 if (!any_of(SC.Transitions, [](const CodeGenSchedTransition &T) { 1533 return hasMCSchedPredicates(T); 1534 })) 1535 continue; 1536 } 1537 1538 VariantClasses.push_back(SC.Index); 1539 } 1540 } 1541 1542 static void collectProcessorIndices(const CodeGenSchedClass &SC, 1543 IdxVec &ProcIndices) { 1544 // A variant scheduling class may define transitions for multiple 1545 // processors. This function identifies wich processors are associated with 1546 // transition rules specified by variant class `SC`. 1547 for (const CodeGenSchedTransition &T : SC.Transitions) { 1548 IdxVec PI; 1549 std::set_union(&T.ProcIndex, &T.ProcIndex + 1, ProcIndices.begin(), 1550 ProcIndices.end(), std::back_inserter(PI)); 1551 ProcIndices.swap(PI); 1552 } 1553 } 1554 1555 static bool isAlwaysTrue(const CodeGenSchedTransition &T) { 1556 return llvm::all_of(T.PredTerm, 1557 [](const Record *R) { return isTruePredicate(R); }); 1558 } 1559 1560 void SubtargetEmitter::emitSchedModelHelpersImpl( 1561 raw_ostream &OS, bool OnlyExpandMCInstPredicates) { 1562 IdxVec VariantClasses; 1563 collectVariantClasses(SchedModels, VariantClasses, 1564 OnlyExpandMCInstPredicates); 1565 1566 if (VariantClasses.empty()) { 1567 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates); 1568 return; 1569 } 1570 1571 // Construct a switch statement where the condition is a check on the 1572 // scheduling class identifier. There is a `case` for every variant class 1573 // defined by the processor models of this target. 1574 // Each `case` implements a number of rules to resolve (i.e. to transition from) 1575 // a variant scheduling class to another scheduling class. Rules are 1576 // described by instances of CodeGenSchedTransition. Note that transitions may 1577 // not be valid for all processors. 1578 OS << " switch (SchedClass) {\n"; 1579 for (unsigned VC : VariantClasses) { 1580 IdxVec ProcIndices; 1581 const CodeGenSchedClass &SC = SchedModels.getSchedClass(VC); 1582 collectProcessorIndices(SC, ProcIndices); 1583 1584 OS << " case " << VC << ": // " << SC.Name << '\n'; 1585 1586 PredicateExpander PE(Target); 1587 PE.setByRef(false); 1588 PE.setExpandForMC(OnlyExpandMCInstPredicates); 1589 for (unsigned PI : ProcIndices) { 1590 OS << " "; 1591 1592 // Emit a guard on the processor ID. 1593 if (PI != 0) { 1594 OS << (OnlyExpandMCInstPredicates 1595 ? "if (CPUID == " 1596 : "if (SchedModel->getProcessorID() == "); 1597 OS << PI << ") "; 1598 OS << "{ // " << (SchedModels.procModelBegin() + PI)->ModelName << '\n'; 1599 } 1600 1601 // Now emit transitions associated with processor PI. 1602 const CodeGenSchedTransition *FinalT = nullptr; 1603 for (const CodeGenSchedTransition &T : SC.Transitions) { 1604 if (PI != 0 && T.ProcIndex != PI) 1605 continue; 1606 1607 // Emit only transitions based on MCSchedPredicate, if it's the case. 1608 // At least the transition specified by NoSchedPred is emitted, 1609 // which becomes the default transition for those variants otherwise 1610 // not based on MCSchedPredicate. 1611 // FIXME: preferably, llvm-mca should instead assume a reasonable 1612 // default when a variant transition is not based on MCSchedPredicate 1613 // for a given processor. 1614 if (OnlyExpandMCInstPredicates && !hasMCSchedPredicates(T)) 1615 continue; 1616 1617 // If transition is folded to 'return X' it should be the last one. 1618 if (isAlwaysTrue(T)) { 1619 FinalT = &T; 1620 continue; 1621 } 1622 PE.setIndentLevel(3); 1623 emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS); 1624 } 1625 if (FinalT) 1626 emitPredicates(*FinalT, SchedModels.getSchedClass(FinalT->ToClassIdx), 1627 PE, OS); 1628 1629 OS << " }\n"; 1630 1631 if (PI == 0) 1632 break; 1633 } 1634 1635 if (SC.isInferred()) 1636 OS << " return " << SC.Index << ";\n"; 1637 OS << " break;\n"; 1638 } 1639 1640 OS << " };\n"; 1641 1642 emitSchedModelHelperEpilogue(OS, OnlyExpandMCInstPredicates); 1643 } 1644 1645 void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName, 1646 raw_ostream &OS) { 1647 OS << "unsigned " << ClassName 1648 << "\n::resolveSchedClass(unsigned SchedClass, const MachineInstr *MI," 1649 << " const TargetSchedModel *SchedModel) const {\n"; 1650 1651 // Emit the predicate prolog code. 1652 emitPredicateProlog(Records, OS); 1653 1654 // Emit target predicates. 1655 emitSchedModelHelpersImpl(OS); 1656 1657 OS << "} // " << ClassName << "::resolveSchedClass\n\n"; 1658 1659 OS << "unsigned " << ClassName 1660 << "\n::resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI," 1661 << " const MCInstrInfo *MCII, unsigned CPUID) const {\n" 1662 << " return " << Target << "_MC" 1663 << "::resolveVariantSchedClassImpl(SchedClass, MI, MCII, CPUID);\n" 1664 << "} // " << ClassName << "::resolveVariantSchedClass\n\n"; 1665 1666 STIPredicateExpander PE(Target); 1667 PE.setClassPrefix(ClassName); 1668 PE.setExpandDefinition(true); 1669 PE.setByRef(false); 1670 PE.setIndentLevel(0); 1671 1672 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates()) 1673 PE.expandSTIPredicate(OS, Fn); 1674 } 1675 1676 void SubtargetEmitter::EmitHwModeCheck(const std::string &ClassName, 1677 raw_ostream &OS) { 1678 const CodeGenHwModes &CGH = TGT.getHwModes(); 1679 assert(CGH.getNumModeIds() > 0); 1680 if (CGH.getNumModeIds() == 1) 1681 return; 1682 1683 OS << "unsigned " << ClassName << "::getHwMode() const {\n"; 1684 for (unsigned M = 1, NumModes = CGH.getNumModeIds(); M != NumModes; ++M) { 1685 const HwMode &HM = CGH.getMode(M); 1686 OS << " if (checkFeatures(\"" << HM.Features 1687 << "\")) return " << M << ";\n"; 1688 } 1689 OS << " return 0;\n}\n"; 1690 } 1691 1692 // 1693 // ParseFeaturesFunction - Produces a subtarget specific function for parsing 1694 // the subtarget features string. 1695 // 1696 void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS, 1697 unsigned NumFeatures, 1698 unsigned NumProcs) { 1699 std::vector<Record*> Features = 1700 Records.getAllDerivedDefinitions("SubtargetFeature"); 1701 llvm::sort(Features, LessRecord()); 1702 1703 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n" 1704 << "// subtarget options.\n" 1705 << "void llvm::"; 1706 OS << Target; 1707 OS << "Subtarget::ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, " 1708 << "StringRef FS) {\n" 1709 << " LLVM_DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n" 1710 << " LLVM_DEBUG(dbgs() << \"\\nCPU:\" << CPU);\n" 1711 << " LLVM_DEBUG(dbgs() << \"\\nTuneCPU:\" << TuneCPU << \"\\n\\n\");\n"; 1712 1713 if (Features.empty()) { 1714 OS << "}\n"; 1715 return; 1716 } 1717 1718 OS << " InitMCProcessorInfo(CPU, TuneCPU, FS);\n" 1719 << " const FeatureBitset &Bits = getFeatureBits();\n"; 1720 1721 for (Record *R : Features) { 1722 // Next record 1723 StringRef Instance = R->getName(); 1724 StringRef Value = R->getValueAsString("Value"); 1725 StringRef Attribute = R->getValueAsString("Attribute"); 1726 1727 if (Value=="true" || Value=="false") 1728 OS << " if (Bits[" << Target << "::" 1729 << Instance << "]) " 1730 << Attribute << " = " << Value << ";\n"; 1731 else 1732 OS << " if (Bits[" << Target << "::" 1733 << Instance << "] && " 1734 << Attribute << " < " << Value << ") " 1735 << Attribute << " = " << Value << ";\n"; 1736 } 1737 1738 OS << "}\n"; 1739 } 1740 1741 void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) { 1742 OS << "namespace " << Target << "_MC {\n" 1743 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n" 1744 << " const MCInst *MI, const MCInstrInfo *MCII, unsigned CPUID) {\n"; 1745 emitSchedModelHelpersImpl(OS, /* OnlyExpandMCPredicates */ true); 1746 OS << "}\n"; 1747 OS << "} // end namespace " << Target << "_MC\n\n"; 1748 1749 OS << "struct " << Target 1750 << "GenMCSubtargetInfo : public MCSubtargetInfo {\n"; 1751 OS << " " << Target << "GenMCSubtargetInfo(const Triple &TT,\n" 1752 << " StringRef CPU, StringRef TuneCPU, StringRef FS,\n" 1753 << " ArrayRef<SubtargetFeatureKV> PF,\n" 1754 << " ArrayRef<SubtargetSubTypeKV> PD,\n" 1755 << " const MCWriteProcResEntry *WPR,\n" 1756 << " const MCWriteLatencyEntry *WL,\n" 1757 << " const MCReadAdvanceEntry *RA, const InstrStage *IS,\n" 1758 << " const unsigned *OC, const unsigned *FP) :\n" 1759 << " MCSubtargetInfo(TT, CPU, TuneCPU, FS, PF, PD,\n" 1760 << " WPR, WL, RA, IS, OC, FP) { }\n\n" 1761 << " unsigned resolveVariantSchedClass(unsigned SchedClass,\n" 1762 << " const MCInst *MI, const MCInstrInfo *MCII,\n" 1763 << " unsigned CPUID) const override {\n" 1764 << " return " << Target << "_MC" 1765 << "::resolveVariantSchedClassImpl(SchedClass, MI, MCII, CPUID);\n"; 1766 OS << " }\n"; 1767 if (TGT.getHwModes().getNumModeIds() > 1) 1768 OS << " unsigned getHwMode() const override;\n"; 1769 OS << "};\n"; 1770 EmitHwModeCheck(Target + "GenMCSubtargetInfo", OS); 1771 } 1772 1773 void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) { 1774 OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n"; 1775 OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n"; 1776 1777 STIPredicateExpander PE(Target); 1778 PE.setExpandForMC(true); 1779 PE.setByRef(true); 1780 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates()) 1781 PE.expandSTIPredicate(OS, Fn); 1782 1783 OS << "#endif // GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n"; 1784 1785 OS << "\n#ifdef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n"; 1786 OS << "#undef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n"; 1787 1788 std::string ClassPrefix = Target + "MCInstrAnalysis"; 1789 PE.setExpandDefinition(true); 1790 PE.setClassPrefix(ClassPrefix); 1791 PE.setIndentLevel(0); 1792 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates()) 1793 PE.expandSTIPredicate(OS, Fn); 1794 1795 OS << "#endif // GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n"; 1796 } 1797 1798 // 1799 // SubtargetEmitter::run - Main subtarget enumeration emitter. 1800 // 1801 void SubtargetEmitter::run(raw_ostream &OS) { 1802 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS); 1803 1804 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n"; 1805 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n"; 1806 1807 DenseMap<Record *, unsigned> FeatureMap; 1808 1809 OS << "namespace llvm {\n"; 1810 Enumeration(OS, FeatureMap); 1811 OS << "} // end namespace llvm\n\n"; 1812 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n"; 1813 1814 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n"; 1815 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n"; 1816 1817 OS << "namespace llvm {\n"; 1818 #if 0 1819 OS << "namespace {\n"; 1820 #endif 1821 unsigned NumFeatures = FeatureKeyValues(OS, FeatureMap); 1822 OS << "\n"; 1823 EmitSchedModel(OS); 1824 OS << "\n"; 1825 unsigned NumProcs = CPUKeyValues(OS, FeatureMap); 1826 OS << "\n"; 1827 #if 0 1828 OS << "} // end anonymous namespace\n\n"; 1829 #endif 1830 1831 // MCInstrInfo initialization routine. 1832 emitGenMCSubtargetInfo(OS); 1833 1834 OS << "\nstatic inline MCSubtargetInfo *create" << Target 1835 << "MCSubtargetInfoImpl(" 1836 << "const Triple &TT, StringRef CPU, StringRef TuneCPU, StringRef FS) {\n"; 1837 OS << " return new " << Target 1838 << "GenMCSubtargetInfo(TT, CPU, TuneCPU, FS, "; 1839 if (NumFeatures) 1840 OS << Target << "FeatureKV, "; 1841 else 1842 OS << "None, "; 1843 if (NumProcs) 1844 OS << Target << "SubTypeKV, "; 1845 else 1846 OS << "None, "; 1847 OS << '\n'; OS.indent(22); 1848 OS << Target << "WriteProcResTable, " 1849 << Target << "WriteLatencyTable, " 1850 << Target << "ReadAdvanceTable, "; 1851 OS << '\n'; OS.indent(22); 1852 if (SchedModels.hasItineraries()) { 1853 OS << Target << "Stages, " 1854 << Target << "OperandCycles, " 1855 << Target << "ForwardingPaths"; 1856 } else 1857 OS << "nullptr, nullptr, nullptr"; 1858 OS << ");\n}\n\n"; 1859 1860 OS << "} // end namespace llvm\n\n"; 1861 1862 OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n"; 1863 1864 OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n"; 1865 OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n"; 1866 1867 OS << "#include \"llvm/Support/Debug.h\"\n"; 1868 OS << "#include \"llvm/Support/raw_ostream.h\"\n\n"; 1869 ParseFeaturesFunction(OS, NumFeatures, NumProcs); 1870 1871 OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n"; 1872 1873 // Create a TargetSubtargetInfo subclass to hide the MC layer initialization. 1874 OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n"; 1875 OS << "#undef GET_SUBTARGETINFO_HEADER\n\n"; 1876 1877 std::string ClassName = Target + "GenSubtargetInfo"; 1878 OS << "namespace llvm {\n"; 1879 OS << "class DFAPacketizer;\n"; 1880 OS << "namespace " << Target << "_MC {\n" 1881 << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass," 1882 << " const MCInst *MI, const MCInstrInfo *MCII, unsigned CPUID);\n" 1883 << "} // end namespace " << Target << "_MC\n\n"; 1884 OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n" 1885 << " explicit " << ClassName << "(const Triple &TT, StringRef CPU, " 1886 << "StringRef TuneCPU, StringRef FS);\n" 1887 << "public:\n" 1888 << " unsigned resolveSchedClass(unsigned SchedClass, " 1889 << " const MachineInstr *DefMI," 1890 << " const TargetSchedModel *SchedModel) const override;\n" 1891 << " unsigned resolveVariantSchedClass(unsigned SchedClass," 1892 << " const MCInst *MI, const MCInstrInfo *MCII," 1893 << " unsigned CPUID) const override;\n" 1894 << " DFAPacketizer *createDFAPacketizer(const InstrItineraryData *IID)" 1895 << " const;\n"; 1896 if (TGT.getHwModes().getNumModeIds() > 1) 1897 OS << " unsigned getHwMode() const override;\n"; 1898 1899 STIPredicateExpander PE(Target); 1900 PE.setByRef(false); 1901 for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates()) 1902 PE.expandSTIPredicate(OS, Fn); 1903 1904 OS << "};\n" 1905 << "} // end namespace llvm\n\n"; 1906 1907 OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n"; 1908 1909 OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n"; 1910 OS << "#undef GET_SUBTARGETINFO_CTOR\n\n"; 1911 1912 OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n"; 1913 OS << "namespace llvm {\n"; 1914 OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n"; 1915 OS << "extern const llvm::SubtargetSubTypeKV " << Target << "SubTypeKV[];\n"; 1916 OS << "extern const llvm::MCWriteProcResEntry " 1917 << Target << "WriteProcResTable[];\n"; 1918 OS << "extern const llvm::MCWriteLatencyEntry " 1919 << Target << "WriteLatencyTable[];\n"; 1920 OS << "extern const llvm::MCReadAdvanceEntry " 1921 << Target << "ReadAdvanceTable[];\n"; 1922 1923 if (SchedModels.hasItineraries()) { 1924 OS << "extern const llvm::InstrStage " << Target << "Stages[];\n"; 1925 OS << "extern const unsigned " << Target << "OperandCycles[];\n"; 1926 OS << "extern const unsigned " << Target << "ForwardingPaths[];\n"; 1927 } 1928 1929 OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, " 1930 << "StringRef TuneCPU, StringRef FS)\n" 1931 << " : TargetSubtargetInfo(TT, CPU, TuneCPU, FS, "; 1932 if (NumFeatures) 1933 OS << "makeArrayRef(" << Target << "FeatureKV, " << NumFeatures << "), "; 1934 else 1935 OS << "None, "; 1936 if (NumProcs) 1937 OS << "makeArrayRef(" << Target << "SubTypeKV, " << NumProcs << "), "; 1938 else 1939 OS << "None, "; 1940 OS << '\n'; OS.indent(24); 1941 OS << Target << "WriteProcResTable, " 1942 << Target << "WriteLatencyTable, " 1943 << Target << "ReadAdvanceTable, "; 1944 OS << '\n'; OS.indent(24); 1945 if (SchedModels.hasItineraries()) { 1946 OS << Target << "Stages, " 1947 << Target << "OperandCycles, " 1948 << Target << "ForwardingPaths"; 1949 } else 1950 OS << "nullptr, nullptr, nullptr"; 1951 OS << ") {}\n\n"; 1952 1953 EmitSchedModelHelpers(ClassName, OS); 1954 EmitHwModeCheck(ClassName, OS); 1955 1956 OS << "} // end namespace llvm\n\n"; 1957 1958 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n"; 1959 1960 EmitMCInstrAnalysisPredicateFunctions(OS); 1961 } 1962 1963 namespace llvm { 1964 1965 void EmitSubtarget(RecordKeeper &RK, raw_ostream &OS) { 1966 CodeGenTarget CGTarget(RK); 1967 SubtargetEmitter(RK, CGTarget).run(OS); 1968 } 1969 1970 } // end namespace llvm 1971