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