SubtargetEmitter.cpp revision 96085a36dbb9cf251c81bc150e41ea9c952c99c0
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 "SubtargetEmitter.h" 15#include "CodeGenTarget.h" 16#include "Record.h" 17#include "llvm/ADT/StringExtras.h" 18#include "llvm/Support/Debug.h" 19#include <algorithm> 20using namespace llvm; 21 22// 23// Enumeration - Emit the specified class as an enumeration. 24// 25void SubtargetEmitter::Enumeration(raw_ostream &OS, 26 const char *ClassName, 27 bool isBits) { 28 // Get all records of class and sort 29 std::vector<Record*> DefList = Records.getAllDerivedDefinitions(ClassName); 30 std::sort(DefList.begin(), DefList.end(), LessRecord()); 31 32 // Open enumeration 33 OS << "enum {\n"; 34 35 // For each record 36 for (unsigned i = 0, N = DefList.size(); i < N;) { 37 // Next record 38 Record *Def = DefList[i]; 39 40 // Get and emit name 41 OS << " " << Def->getName(); 42 43 // If bit flags then emit expression (1 << i) 44 if (isBits) OS << " = " << " 1 << " << i; 45 46 // Depending on 'if more in the list' emit comma 47 if (++i < N) OS << ","; 48 49 OS << "\n"; 50 } 51 52 // Close enumeration 53 OS << "};\n"; 54} 55 56// 57// FeatureKeyValues - Emit data of all the subtarget features. Used by the 58// command line. 59// 60void SubtargetEmitter::FeatureKeyValues(raw_ostream &OS) { 61 // Gather and sort all the features 62 std::vector<Record*> FeatureList = 63 Records.getAllDerivedDefinitions("SubtargetFeature"); 64 std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName()); 65 66 // Begin feature table 67 OS << "// Sorted (by key) array of values for CPU features.\n" 68 << "static const llvm::SubtargetFeatureKV FeatureKV[] = {\n"; 69 70 // For each feature 71 for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) { 72 // Next feature 73 Record *Feature = FeatureList[i]; 74 75 const std::string &Name = Feature->getName(); 76 const std::string &CommandLineName = Feature->getValueAsString("Name"); 77 const std::string &Desc = Feature->getValueAsString("Desc"); 78 79 if (CommandLineName.empty()) continue; 80 81 // Emit as { "feature", "description", featureEnum, i1 | i2 | ... | in } 82 OS << " { " 83 << "\"" << CommandLineName << "\", " 84 << "\"" << Desc << "\", " 85 << Name << ", "; 86 87 const std::vector<Record*> &ImpliesList = 88 Feature->getValueAsListOfDefs("Implies"); 89 90 if (ImpliesList.empty()) { 91 OS << "0"; 92 } else { 93 for (unsigned j = 0, M = ImpliesList.size(); j < M;) { 94 OS << ImpliesList[j]->getName(); 95 if (++j < M) OS << " | "; 96 } 97 } 98 99 OS << " }"; 100 101 // Depending on 'if more in the list' emit comma 102 if ((i + 1) < N) OS << ","; 103 104 OS << "\n"; 105 } 106 107 // End feature table 108 OS << "};\n"; 109 110 // Emit size of table 111 OS<<"\nenum {\n"; 112 OS<<" FeatureKVSize = sizeof(FeatureKV)/sizeof(llvm::SubtargetFeatureKV)\n"; 113 OS<<"};\n"; 114} 115 116// 117// CPUKeyValues - Emit data of all the subtarget processors. Used by command 118// line. 119// 120void SubtargetEmitter::CPUKeyValues(raw_ostream &OS) { 121 // Gather and sort processor information 122 std::vector<Record*> ProcessorList = 123 Records.getAllDerivedDefinitions("Processor"); 124 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName()); 125 126 // Begin processor table 127 OS << "// Sorted (by key) array of values for CPU subtype.\n" 128 << "static const llvm::SubtargetFeatureKV SubTypeKV[] = {\n"; 129 130 // For each processor 131 for (unsigned i = 0, N = ProcessorList.size(); i < N;) { 132 // Next processor 133 Record *Processor = ProcessorList[i]; 134 135 const std::string &Name = Processor->getValueAsString("Name"); 136 const std::vector<Record*> &FeatureList = 137 Processor->getValueAsListOfDefs("Features"); 138 139 // Emit as { "cpu", "description", f1 | f2 | ... fn }, 140 OS << " { " 141 << "\"" << Name << "\", " 142 << "\"Select the " << Name << " processor\", "; 143 144 if (FeatureList.empty()) { 145 OS << "0"; 146 } else { 147 for (unsigned j = 0, M = FeatureList.size(); j < M;) { 148 OS << FeatureList[j]->getName(); 149 if (++j < M) OS << " | "; 150 } 151 } 152 153 // The "0" is for the "implies" section of this data structure. 154 OS << ", 0 }"; 155 156 // Depending on 'if more in the list' emit comma 157 if (++i < N) OS << ","; 158 159 OS << "\n"; 160 } 161 162 // End processor table 163 OS << "};\n"; 164 165 // Emit size of table 166 OS<<"\nenum {\n"; 167 OS<<" SubTypeKVSize = sizeof(SubTypeKV)/sizeof(llvm::SubtargetFeatureKV)\n"; 168 OS<<"};\n"; 169} 170 171// 172// CollectAllItinClasses - Gathers and enumerates all the itinerary classes. 173// Returns itinerary class count. 174// 175unsigned SubtargetEmitter::CollectAllItinClasses(raw_ostream &OS, 176 std::map<std::string, unsigned> &ItinClassesMap) { 177 // Gather and sort all itinerary classes 178 std::vector<Record*> ItinClassList = 179 Records.getAllDerivedDefinitions("InstrItinClass"); 180 std::sort(ItinClassList.begin(), ItinClassList.end(), LessRecord()); 181 182 // For each itinerary class 183 unsigned N = ItinClassList.size(); 184 for (unsigned i = 0; i < N; i++) { 185 // Next itinerary class 186 const Record *ItinClass = ItinClassList[i]; 187 // Get name of itinerary class 188 // Assign itinerary class a unique number 189 ItinClassesMap[ItinClass->getName()] = i; 190 } 191 192 // Emit size of table 193 OS<<"\nenum {\n"; 194 OS<<" ItinClassesSize = " << N << "\n"; 195 OS<<"};\n"; 196 197 // Return itinerary class count 198 return N; 199} 200 201// 202// FormItineraryStageString - Compose a string containing the stage 203// data initialization for the specified itinerary. N is the number 204// of stages. 205// 206void SubtargetEmitter::FormItineraryStageString(Record *ItinData, 207 std::string &ItinString, 208 unsigned &NStages) { 209 // Get states list 210 const std::vector<Record*> &StageList = 211 ItinData->getValueAsListOfDefs("Stages"); 212 213 // For each stage 214 unsigned N = NStages = StageList.size(); 215 for (unsigned i = 0; i < N;) { 216 // Next stage 217 const Record *Stage = StageList[i]; 218 219 // Form string as ,{ cycles, u1 | u2 | ... | un, timeinc, kind } 220 int Cycles = Stage->getValueAsInt("Cycles"); 221 ItinString += " { " + itostr(Cycles) + ", "; 222 223 // Get unit list 224 const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units"); 225 226 // For each unit 227 for (unsigned j = 0, M = UnitList.size(); j < M;) { 228 // Add name and bitwise or 229 ItinString += UnitList[j]->getName(); 230 if (++j < M) ItinString += " | "; 231 } 232 233 int TimeInc = Stage->getValueAsInt("TimeInc"); 234 ItinString += ", " + itostr(TimeInc); 235 236 int Kind = Stage->getValueAsInt("Kind"); 237 ItinString += ", (llvm::InstrStage::ReservationKinds)" + itostr(Kind); 238 239 // Close off stage 240 ItinString += " }"; 241 if (++i < N) ItinString += ", "; 242 } 243} 244 245// 246// FormItineraryOperandCycleString - Compose a string containing the 247// operand cycle initialization for the specified itinerary. N is the 248// number of operands that has cycles specified. 249// 250void SubtargetEmitter::FormItineraryOperandCycleString(Record *ItinData, 251 std::string &ItinString, unsigned &NOperandCycles) { 252 // Get operand cycle list 253 const std::vector<int64_t> &OperandCycleList = 254 ItinData->getValueAsListOfInts("OperandCycles"); 255 256 // For each operand cycle 257 unsigned N = NOperandCycles = OperandCycleList.size(); 258 for (unsigned i = 0; i < N;) { 259 // Next operand cycle 260 const int OCycle = OperandCycleList[i]; 261 262 ItinString += " " + itostr(OCycle); 263 if (++i < N) ItinString += ", "; 264 } 265} 266 267// 268// EmitStageAndOperandCycleData - Generate unique itinerary stages and 269// operand cycle tables. Record itineraries for processors. 270// 271void SubtargetEmitter::EmitStageAndOperandCycleData(raw_ostream &OS, 272 unsigned NItinClasses, 273 std::map<std::string, unsigned> &ItinClassesMap, 274 std::vector<std::vector<InstrItinerary> > &ProcList) { 275 // Gather processor iteraries 276 std::vector<Record*> ProcItinList = 277 Records.getAllDerivedDefinitions("ProcessorItineraries"); 278 279 // If just no itinerary then don't bother 280 if (ProcItinList.size() < 2) return; 281 282 // Begin stages table 283 std::string StageTable = "static const llvm::InstrStage Stages[] = {\n"; 284 StageTable += " { 0, 0, 0, llvm::InstrStage::Required }, // No itinerary\n"; 285 286 // Begin operand cycle table 287 std::string OperandCycleTable = "static const unsigned OperandCycles[] = {\n"; 288 OperandCycleTable += " 0, // No itinerary\n"; 289 290 unsigned StageCount = 1, OperandCycleCount = 1; 291 unsigned ItinStageEnum = 1, ItinOperandCycleEnum = 1; 292 std::map<std::string, unsigned> ItinStageMap, ItinOperandCycleMap; 293 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) { 294 // Next record 295 Record *Proc = ProcItinList[i]; 296 297 // Get processor itinerary name 298 const std::string &Name = Proc->getName(); 299 300 // Skip default 301 if (Name == "NoItineraries") continue; 302 303 // Create and expand processor itinerary to cover all itinerary classes 304 std::vector<InstrItinerary> ItinList; 305 ItinList.resize(NItinClasses); 306 307 // Get itinerary data list 308 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID"); 309 310 // For each itinerary data 311 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) { 312 // Next itinerary data 313 Record *ItinData = ItinDataList[j]; 314 315 // Get string and stage count 316 std::string ItinStageString; 317 unsigned NStages; 318 FormItineraryStageString(ItinData, ItinStageString, NStages); 319 320 // Get string and operand cycle count 321 std::string ItinOperandCycleString; 322 unsigned NOperandCycles; 323 FormItineraryOperandCycleString(ItinData, ItinOperandCycleString, 324 NOperandCycles); 325 326 // Check to see if stage already exists and create if it doesn't 327 unsigned FindStage = 0; 328 if (NStages > 0) { 329 FindStage = ItinStageMap[ItinStageString]; 330 if (FindStage == 0) { 331 // Emit as { cycles, u1 | u2 | ... | un, timeinc }, // index 332 StageTable += ItinStageString + ", // " + itostr(ItinStageEnum) + "\n"; 333 // Record Itin class number. 334 ItinStageMap[ItinStageString] = FindStage = StageCount; 335 StageCount += NStages; 336 ItinStageEnum++; 337 } 338 } 339 340 // Check to see if operand cycle already exists and create if it doesn't 341 unsigned FindOperandCycle = 0; 342 if (NOperandCycles > 0) { 343 FindOperandCycle = ItinOperandCycleMap[ItinOperandCycleString]; 344 if (FindOperandCycle == 0) { 345 // Emit as cycle, // index 346 OperandCycleTable += ItinOperandCycleString + ", // " + 347 itostr(ItinOperandCycleEnum) + "\n"; 348 // Record Itin class number. 349 ItinOperandCycleMap[ItinOperandCycleString] = 350 FindOperandCycle = OperandCycleCount; 351 OperandCycleCount += NOperandCycles; 352 ItinOperandCycleEnum++; 353 } 354 } 355 356 // Set up itinerary as location and location + stage count 357 InstrItinerary Intinerary = { FindStage, FindStage + NStages, 358 FindOperandCycle, FindOperandCycle + NOperandCycles}; 359 360 // Locate where to inject into processor itinerary table 361 const std::string &Name = ItinData->getValueAsDef("TheClass")->getName(); 362 unsigned Find = ItinClassesMap[Name]; 363 364 // Inject - empty slots will be 0, 0 365 ItinList[Find] = Intinerary; 366 } 367 368 // Add process itinerary to list 369 ProcList.push_back(ItinList); 370 } 371 372 // Closing stage 373 StageTable += " { 0, 0, 0, llvm::InstrStage::Required } // End itinerary\n"; 374 StageTable += "};\n"; 375 376 // Closing operand cycles 377 OperandCycleTable += " 0 // End itinerary\n"; 378 OperandCycleTable += "};\n"; 379 380 // Emit tables. 381 OS << StageTable; 382 OS << OperandCycleTable; 383 384 // Emit size of tables 385 OS<<"\nenum {\n"; 386 OS<<" StagesSize = sizeof(Stages)/sizeof(llvm::InstrStage),\n"; 387 OS<<" OperandCyclesSize = sizeof(OperandCycles)/sizeof(unsigned)\n"; 388 OS<<"};\n"; 389} 390 391// 392// EmitProcessorData - Generate data for processor itineraries. 393// 394void SubtargetEmitter::EmitProcessorData(raw_ostream &OS, 395 std::vector<std::vector<InstrItinerary> > &ProcList) { 396 // Get an iterator for processor itinerary stages 397 std::vector<std::vector<InstrItinerary> >::iterator 398 ProcListIter = ProcList.begin(); 399 400 // For each processor itinerary 401 std::vector<Record*> Itins = 402 Records.getAllDerivedDefinitions("ProcessorItineraries"); 403 for (unsigned i = 0, N = Itins.size(); i < N; i++) { 404 // Next record 405 Record *Itin = Itins[i]; 406 407 // Get processor itinerary name 408 const std::string &Name = Itin->getName(); 409 410 // Skip default 411 if (Name == "NoItineraries") continue; 412 413 // Begin processor itinerary table 414 OS << "\n"; 415 OS << "static const llvm::InstrItinerary " << Name << "[] = {\n"; 416 417 // For each itinerary class 418 std::vector<InstrItinerary> &ItinList = *ProcListIter++; 419 for (unsigned j = 0, M = ItinList.size(); j < M; ++j) { 420 InstrItinerary &Intinerary = ItinList[j]; 421 422 // Emit in the form of 423 // { firstStage, lastStage, firstCycle, lastCycle } // index 424 if (Intinerary.FirstStage == 0) { 425 OS << " { 0, 0, 0, 0 }"; 426 } else { 427 OS << " { " << Intinerary.FirstStage << ", " << 428 Intinerary.LastStage << ", " << 429 Intinerary.FirstOperandCycle << ", " << 430 Intinerary.LastOperandCycle << " }"; 431 } 432 433 OS << ", // " << j << "\n"; 434 } 435 436 // End processor itinerary table 437 OS << " { ~0U, ~0U, ~0U, ~0U } // end marker\n"; 438 OS << "};\n"; 439 } 440} 441 442// 443// EmitProcessorLookup - generate cpu name to itinerary lookup table. 444// 445void SubtargetEmitter::EmitProcessorLookup(raw_ostream &OS) { 446 // Gather and sort processor information 447 std::vector<Record*> ProcessorList = 448 Records.getAllDerivedDefinitions("Processor"); 449 std::sort(ProcessorList.begin(), ProcessorList.end(), LessRecordFieldName()); 450 451 // Begin processor table 452 OS << "\n"; 453 OS << "// Sorted (by key) array of itineraries for CPU subtype.\n" 454 << "static const llvm::SubtargetInfoKV ProcItinKV[] = {\n"; 455 456 // For each processor 457 for (unsigned i = 0, N = ProcessorList.size(); i < N;) { 458 // Next processor 459 Record *Processor = ProcessorList[i]; 460 461 const std::string &Name = Processor->getValueAsString("Name"); 462 const std::string &ProcItin = 463 Processor->getValueAsDef("ProcItin")->getName(); 464 465 // Emit as { "cpu", procinit }, 466 OS << " { " 467 << "\"" << Name << "\", " 468 << "(void *)&" << ProcItin; 469 470 OS << " }"; 471 472 // Depending on ''if more in the list'' emit comma 473 if (++i < N) OS << ","; 474 475 OS << "\n"; 476 } 477 478 // End processor table 479 OS << "};\n"; 480 481 // Emit size of table 482 OS<<"\nenum {\n"; 483 OS<<" ProcItinKVSize = sizeof(ProcItinKV)/" 484 "sizeof(llvm::SubtargetInfoKV)\n"; 485 OS<<"};\n"; 486} 487 488// 489// EmitData - Emits all stages and itineries, folding common patterns. 490// 491void SubtargetEmitter::EmitData(raw_ostream &OS) { 492 std::map<std::string, unsigned> ItinClassesMap; 493 std::vector<std::vector<InstrItinerary> > ProcList; 494 495 // Enumerate all the itinerary classes 496 unsigned NItinClasses = CollectAllItinClasses(OS, ItinClassesMap); 497 // Make sure the rest is worth the effort 498 HasItineraries = NItinClasses != 1; // Ignore NoItinerary. 499 500 if (HasItineraries) { 501 // Emit the stage data 502 EmitStageAndOperandCycleData(OS, NItinClasses, ItinClassesMap, ProcList); 503 // Emit the processor itinerary data 504 EmitProcessorData(OS, ProcList); 505 // Emit the processor lookup data 506 EmitProcessorLookup(OS); 507 } 508} 509 510// 511// ParseFeaturesFunction - Produces a subtarget specific function for parsing 512// the subtarget features string. 513// 514void SubtargetEmitter::ParseFeaturesFunction(raw_ostream &OS) { 515 std::vector<Record*> Features = 516 Records.getAllDerivedDefinitions("SubtargetFeature"); 517 std::sort(Features.begin(), Features.end(), LessRecord()); 518 519 OS << "// ParseSubtargetFeatures - Parses features string setting specified\n" 520 << "// subtarget options.\n" 521 << "std::string llvm::"; 522 OS << Target; 523 OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n" 524 << " const std::string &CPU) {\n" 525 << " DEBUG(dbgs() << \"\\nFeatures:\" << FS);\n" 526 << " DEBUG(dbgs() << \"\\nCPU:\" << CPU);\n" 527 << " SubtargetFeatures Features(FS);\n" 528 << " Features.setCPUIfNone(CPU);\n" 529 << " uint32_t Bits = Features.getBits(SubTypeKV, SubTypeKVSize,\n" 530 << " FeatureKV, FeatureKVSize);\n"; 531 532 for (unsigned i = 0; i < Features.size(); i++) { 533 // Next record 534 Record *R = Features[i]; 535 const std::string &Instance = R->getName(); 536 const std::string &Value = R->getValueAsString("Value"); 537 const std::string &Attribute = R->getValueAsString("Attribute"); 538 539 if (Value=="true" || Value=="false") 540 OS << " if ((Bits & " << Instance << ") != 0) " 541 << Attribute << " = " << Value << ";\n"; 542 else 543 OS << " if ((Bits & " << Instance << ") != 0 && " << Attribute << 544 " < " << Value << ") " << Attribute << " = " << Value << ";\n"; 545 } 546 547 if (HasItineraries) { 548 OS << "\n" 549 << " InstrItinerary *Itinerary = (InstrItinerary *)" 550 << "Features.getInfo(ProcItinKV, ProcItinKVSize);\n" 551 << " InstrItins = InstrItineraryData(Stages, OperandCycles, Itinerary);\n"; 552 } 553 554 OS << " return Features.getCPU();\n" 555 << "}\n"; 556} 557 558// 559// SubtargetEmitter::run - Main subtarget enumeration emitter. 560// 561void SubtargetEmitter::run(raw_ostream &OS) { 562 Target = CodeGenTarget().getName(); 563 564 EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS); 565 566 OS << "#include \"llvm/Support/Debug.h\"\n"; 567 OS << "#include \"llvm/Support/raw_ostream.h\"\n"; 568 OS << "#include \"llvm/Target/SubtargetFeature.h\"\n"; 569 OS << "#include \"llvm/Target/TargetInstrItineraries.h\"\n\n"; 570 571 Enumeration(OS, "FuncUnit", true); 572 OS<<"\n"; 573// Enumeration(OS, "InstrItinClass", false); 574// OS<<"\n"; 575 Enumeration(OS, "SubtargetFeature", true); 576 OS<<"\n"; 577 FeatureKeyValues(OS); 578 OS<<"\n"; 579 CPUKeyValues(OS); 580 OS<<"\n"; 581 EmitData(OS); 582 OS<<"\n"; 583 ParseFeaturesFunction(OS); 584} 585