1 //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. --*- C++ -*-===// 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 is responsible for emitting a description of the target 10 // instruction set for the code generator. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CodeGenDAGPatterns.h" 15 #include "CodeGenInstruction.h" 16 #include "CodeGenSchedule.h" 17 #include "CodeGenTarget.h" 18 #include "PredicateExpander.h" 19 #include "SequenceToOffsetTable.h" 20 #include "SubtargetFeatureInfo.h" 21 #include "TableGenBackends.h" 22 #include "Types.h" 23 #include "llvm/ADT/ArrayRef.h" 24 #include "llvm/ADT/STLExtras.h" 25 #include "llvm/ADT/StringExtras.h" 26 #include "llvm/Support/Casting.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include "llvm/TableGen/Error.h" 29 #include "llvm/TableGen/Record.h" 30 #include "llvm/TableGen/TableGenBackend.h" 31 #include <cassert> 32 #include <cstdint> 33 #include <iterator> 34 #include <map> 35 #include <string> 36 #include <utility> 37 #include <vector> 38 39 using namespace llvm; 40 41 cl::OptionCategory InstrInfoEmitterCat("Options for -gen-instr-info"); 42 static cl::opt<bool> ExpandMIOperandInfo( 43 "instr-info-expand-mi-operand-info", 44 cl::desc("Expand operand's MIOperandInfo DAG into suboperands"), 45 cl::cat(InstrInfoEmitterCat), cl::init(true)); 46 47 namespace { 48 49 class InstrInfoEmitter { 50 RecordKeeper &Records; 51 CodeGenDAGPatterns CDP; 52 const CodeGenSchedModels &SchedModels; 53 54 public: 55 InstrInfoEmitter(RecordKeeper &R): 56 Records(R), CDP(R), SchedModels(CDP.getTargetInfo().getSchedModels()) {} 57 58 // run - Output the instruction set description. 59 void run(raw_ostream &OS); 60 61 private: 62 void emitEnums(raw_ostream &OS); 63 64 typedef std::vector<std::string> OperandInfoTy; 65 typedef std::vector<OperandInfoTy> OperandInfoListTy; 66 typedef std::map<OperandInfoTy, unsigned> OperandInfoMapTy; 67 68 /// The keys of this map are maps which have OpName enum values as their keys 69 /// and instruction operand indices as their values. The values of this map 70 /// are lists of instruction names. 71 typedef std::map<std::map<unsigned, unsigned>, 72 std::vector<std::string>> OpNameMapTy; 73 typedef std::map<std::string, unsigned>::iterator StrUintMapIter; 74 75 /// Generate member functions in the target-specific GenInstrInfo class. 76 /// 77 /// This method is used to custom expand TIIPredicate definitions. 78 /// See file llvm/Target/TargetInstPredicates.td for a description of what is 79 /// a TIIPredicate and how to use it. 80 void emitTIIHelperMethods(raw_ostream &OS, StringRef TargetName, 81 bool ExpandDefinition = true); 82 83 /// Expand TIIPredicate definitions to functions that accept a const MCInst 84 /// reference. 85 void emitMCIIHelperMethods(raw_ostream &OS, StringRef TargetName); 86 87 /// Write verifyInstructionPredicates methods. 88 void emitFeatureVerifier(raw_ostream &OS, const CodeGenTarget &Target); 89 void emitRecord(const CodeGenInstruction &Inst, unsigned Num, 90 Record *InstrInfo, 91 std::map<std::vector<Record *>, unsigned> &EL, 92 const OperandInfoMapTy &OperandInfo, raw_ostream &OS); 93 void emitOperandTypeMappings( 94 raw_ostream &OS, const CodeGenTarget &Target, 95 ArrayRef<const CodeGenInstruction *> NumberedInstructions); 96 void initOperandMapData( 97 ArrayRef<const CodeGenInstruction *> NumberedInstructions, 98 StringRef Namespace, 99 std::map<std::string, unsigned> &Operands, 100 OpNameMapTy &OperandMap); 101 void emitOperandNameMappings(raw_ostream &OS, const CodeGenTarget &Target, 102 ArrayRef<const CodeGenInstruction*> NumberedInstructions); 103 104 void emitLogicalOperandSizeMappings( 105 raw_ostream &OS, StringRef Namespace, 106 ArrayRef<const CodeGenInstruction *> NumberedInstructions); 107 void emitLogicalOperandTypeMappings( 108 raw_ostream &OS, StringRef Namespace, 109 ArrayRef<const CodeGenInstruction *> NumberedInstructions); 110 111 // Operand information. 112 unsigned CollectOperandInfo(OperandInfoListTy &OperandInfoList, 113 OperandInfoMapTy &OperandInfoMap); 114 void EmitOperandInfo(raw_ostream &OS, OperandInfoListTy &OperandInfoList); 115 OperandInfoTy GetOperandInfo(const CodeGenInstruction &Inst); 116 }; 117 118 } // end anonymous namespace 119 120 //===----------------------------------------------------------------------===// 121 // Operand Info Emission. 122 //===----------------------------------------------------------------------===// 123 124 InstrInfoEmitter::OperandInfoTy 125 InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) { 126 OperandInfoTy Result; 127 128 for (auto &Op : Inst.Operands) { 129 // Handle aggregate operands and normal operands the same way by expanding 130 // either case into a list of operands for this op. 131 std::vector<CGIOperandList::OperandInfo> OperandList; 132 133 // This might be a multiple operand thing. Targets like X86 have 134 // registers in their multi-operand operands. It may also be an anonymous 135 // operand, which has a single operand, but no declared class for the 136 // operand. 137 DagInit *MIOI = Op.MIOperandInfo; 138 139 if (!MIOI || MIOI->getNumArgs() == 0) { 140 // Single, anonymous, operand. 141 OperandList.push_back(Op); 142 } else { 143 for (unsigned j = 0, e = Op.MINumOperands; j != e; ++j) { 144 OperandList.push_back(Op); 145 146 auto *OpR = cast<DefInit>(MIOI->getArg(j))->getDef(); 147 OperandList.back().Rec = OpR; 148 } 149 } 150 151 for (unsigned j = 0, e = OperandList.size(); j != e; ++j) { 152 Record *OpR = OperandList[j].Rec; 153 std::string Res; 154 155 if (OpR->isSubClassOf("RegisterOperand")) 156 OpR = OpR->getValueAsDef("RegClass"); 157 if (OpR->isSubClassOf("RegisterClass")) 158 Res += getQualifiedName(OpR) + "RegClassID, "; 159 else if (OpR->isSubClassOf("PointerLikeRegClass")) 160 Res += utostr(OpR->getValueAsInt("RegClassKind")) + ", "; 161 else 162 // -1 means the operand does not have a fixed register class. 163 Res += "-1, "; 164 165 // Fill in applicable flags. 166 Res += "0"; 167 168 // Ptr value whose register class is resolved via callback. 169 if (OpR->isSubClassOf("PointerLikeRegClass")) 170 Res += "|(1<<MCOI::LookupPtrRegClass)"; 171 172 // Predicate operands. Check to see if the original unexpanded operand 173 // was of type PredicateOp. 174 if (Op.Rec->isSubClassOf("PredicateOp")) 175 Res += "|(1<<MCOI::Predicate)"; 176 177 // Optional def operands. Check to see if the original unexpanded operand 178 // was of type OptionalDefOperand. 179 if (Op.Rec->isSubClassOf("OptionalDefOperand")) 180 Res += "|(1<<MCOI::OptionalDef)"; 181 182 // Branch target operands. Check to see if the original unexpanded 183 // operand was of type BranchTargetOperand. 184 if (Op.Rec->isSubClassOf("BranchTargetOperand")) 185 Res += "|(1<<MCOI::BranchTarget)"; 186 187 // Fill in operand type. 188 Res += ", "; 189 assert(!Op.OperandType.empty() && "Invalid operand type."); 190 Res += Op.OperandType; 191 192 // Fill in constraint info. 193 Res += ", "; 194 195 const CGIOperandList::ConstraintInfo &Constraint = 196 Op.Constraints[j]; 197 if (Constraint.isNone()) 198 Res += "0"; 199 else if (Constraint.isEarlyClobber()) 200 Res += "MCOI_EARLY_CLOBBER"; 201 else { 202 assert(Constraint.isTied()); 203 Res += "MCOI_TIED_TO(" + utostr(Constraint.getTiedOperand()) + ")"; 204 } 205 206 Result.push_back(Res); 207 } 208 } 209 210 return Result; 211 } 212 213 unsigned 214 InstrInfoEmitter::CollectOperandInfo(OperandInfoListTy &OperandInfoList, 215 OperandInfoMapTy &OperandInfoMap) { 216 const CodeGenTarget &Target = CDP.getTargetInfo(); 217 unsigned Offset = 0; 218 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) { 219 OperandInfoTy OperandInfo = GetOperandInfo(*Inst); 220 if (OperandInfoMap.insert({OperandInfo, Offset}).second) { 221 OperandInfoList.push_back(OperandInfo); 222 Offset += OperandInfo.size(); 223 } 224 } 225 return Offset; 226 } 227 228 void InstrInfoEmitter::EmitOperandInfo(raw_ostream &OS, 229 OperandInfoListTy &OperandInfoList) { 230 unsigned Offset = 0; 231 for (auto &OperandInfo : OperandInfoList) { 232 OS << " /* " << Offset << " */"; 233 for (auto &Info : OperandInfo) 234 OS << " { " << Info << " },"; 235 OS << '\n'; 236 Offset += OperandInfo.size(); 237 } 238 } 239 240 /// Initialize data structures for generating operand name mappings. 241 /// 242 /// \param Operands [out] A map used to generate the OpName enum with operand 243 /// names as its keys and operand enum values as its values. 244 /// \param OperandMap [out] A map for representing the operand name mappings for 245 /// each instructions. This is used to generate the OperandMap table as 246 /// well as the getNamedOperandIdx() function. 247 void InstrInfoEmitter::initOperandMapData( 248 ArrayRef<const CodeGenInstruction *> NumberedInstructions, 249 StringRef Namespace, 250 std::map<std::string, unsigned> &Operands, 251 OpNameMapTy &OperandMap) { 252 unsigned NumOperands = 0; 253 for (const CodeGenInstruction *Inst : NumberedInstructions) { 254 if (!Inst->TheDef->getValueAsBit("UseNamedOperandTable")) 255 continue; 256 std::map<unsigned, unsigned> OpList; 257 for (const auto &Info : Inst->Operands) { 258 StrUintMapIter I = Operands.find(Info.Name); 259 260 if (I == Operands.end()) { 261 I = Operands.insert(Operands.begin(), 262 std::pair<std::string, unsigned>(Info.Name, NumOperands++)); 263 } 264 OpList[I->second] = Info.MIOperandNo; 265 } 266 OperandMap[OpList].push_back(Namespace.str() + "::" + 267 Inst->TheDef->getName().str()); 268 } 269 } 270 271 /// Generate a table and function for looking up the indices of operands by 272 /// name. 273 /// 274 /// This code generates: 275 /// - An enum in the llvm::TargetNamespace::OpName namespace, with one entry 276 /// for each operand name. 277 /// - A 2-dimensional table called OperandMap for mapping OpName enum values to 278 /// operand indices. 279 /// - A function called getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx) 280 /// for looking up the operand index for an instruction, given a value from 281 /// OpName enum 282 void InstrInfoEmitter::emitOperandNameMappings(raw_ostream &OS, 283 const CodeGenTarget &Target, 284 ArrayRef<const CodeGenInstruction*> NumberedInstructions) { 285 StringRef Namespace = Target.getInstNamespace(); 286 std::string OpNameNS = "OpName"; 287 // Map of operand names to their enumeration value. This will be used to 288 // generate the OpName enum. 289 std::map<std::string, unsigned> Operands; 290 OpNameMapTy OperandMap; 291 292 initOperandMapData(NumberedInstructions, Namespace, Operands, OperandMap); 293 294 OS << "#ifdef GET_INSTRINFO_OPERAND_ENUM\n"; 295 OS << "#undef GET_INSTRINFO_OPERAND_ENUM\n"; 296 OS << "namespace llvm {\n"; 297 OS << "namespace " << Namespace << " {\n"; 298 OS << "namespace " << OpNameNS << " {\n"; 299 OS << "enum {\n"; 300 for (const auto &Op : Operands) 301 OS << " " << Op.first << " = " << Op.second << ",\n"; 302 303 OS << " OPERAND_LAST"; 304 OS << "\n};\n"; 305 OS << "} // end namespace OpName\n"; 306 OS << "} // end namespace " << Namespace << "\n"; 307 OS << "} // end namespace llvm\n"; 308 OS << "#endif //GET_INSTRINFO_OPERAND_ENUM\n\n"; 309 310 OS << "#ifdef GET_INSTRINFO_NAMED_OPS\n"; 311 OS << "#undef GET_INSTRINFO_NAMED_OPS\n"; 312 OS << "namespace llvm {\n"; 313 OS << "namespace " << Namespace << " {\n"; 314 OS << "LLVM_READONLY\n"; 315 OS << "int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx) {\n"; 316 if (!Operands.empty()) { 317 OS << " static const int16_t OperandMap [][" << Operands.size() 318 << "] = {\n"; 319 for (const auto &Entry : OperandMap) { 320 const std::map<unsigned, unsigned> &OpList = Entry.first; 321 OS << "{"; 322 323 // Emit a row of the OperandMap table 324 for (unsigned i = 0, e = Operands.size(); i != e; ++i) 325 OS << (OpList.count(i) == 0 ? -1 : (int)OpList.find(i)->second) << ", "; 326 327 OS << "},\n"; 328 } 329 OS << "};\n"; 330 331 OS << " switch(Opcode) {\n"; 332 unsigned TableIndex = 0; 333 for (const auto &Entry : OperandMap) { 334 for (const std::string &Name : Entry.second) 335 OS << " case " << Name << ":\n"; 336 337 OS << " return OperandMap[" << TableIndex++ << "][NamedIdx];\n"; 338 } 339 OS << " default: return -1;\n"; 340 OS << " }\n"; 341 } else { 342 // There are no operands, so no need to emit anything 343 OS << " return -1;\n"; 344 } 345 OS << "}\n"; 346 OS << "} // end namespace " << Namespace << "\n"; 347 OS << "} // end namespace llvm\n"; 348 OS << "#endif //GET_INSTRINFO_NAMED_OPS\n\n"; 349 } 350 351 /// Generate an enum for all the operand types for this target, under the 352 /// llvm::TargetNamespace::OpTypes namespace. 353 /// Operand types are all definitions derived of the Operand Target.td class. 354 void InstrInfoEmitter::emitOperandTypeMappings( 355 raw_ostream &OS, const CodeGenTarget &Target, 356 ArrayRef<const CodeGenInstruction *> NumberedInstructions) { 357 358 StringRef Namespace = Target.getInstNamespace(); 359 std::vector<Record *> Operands = Records.getAllDerivedDefinitions("Operand"); 360 std::vector<Record *> RegisterOperands = 361 Records.getAllDerivedDefinitions("RegisterOperand"); 362 std::vector<Record *> RegisterClasses = 363 Records.getAllDerivedDefinitions("RegisterClass"); 364 365 OS << "#ifdef GET_INSTRINFO_OPERAND_TYPES_ENUM\n"; 366 OS << "#undef GET_INSTRINFO_OPERAND_TYPES_ENUM\n"; 367 OS << "namespace llvm {\n"; 368 OS << "namespace " << Namespace << " {\n"; 369 OS << "namespace OpTypes {\n"; 370 OS << "enum OperandType {\n"; 371 372 unsigned EnumVal = 0; 373 for (const std::vector<Record *> *RecordsToAdd : 374 {&Operands, &RegisterOperands, &RegisterClasses}) { 375 for (const Record *Op : *RecordsToAdd) { 376 if (!Op->isAnonymous()) 377 OS << " " << Op->getName() << " = " << EnumVal << ",\n"; 378 ++EnumVal; 379 } 380 } 381 382 OS << " OPERAND_TYPE_LIST_END" << "\n};\n"; 383 OS << "} // end namespace OpTypes\n"; 384 OS << "} // end namespace " << Namespace << "\n"; 385 OS << "} // end namespace llvm\n"; 386 OS << "#endif // GET_INSTRINFO_OPERAND_TYPES_ENUM\n\n"; 387 388 OS << "#ifdef GET_INSTRINFO_OPERAND_TYPE\n"; 389 OS << "#undef GET_INSTRINFO_OPERAND_TYPE\n"; 390 OS << "namespace llvm {\n"; 391 OS << "namespace " << Namespace << " {\n"; 392 OS << "LLVM_READONLY\n"; 393 OS << "static int getOperandType(uint16_t Opcode, uint16_t OpIdx) {\n"; 394 auto getInstrName = [&](int I) -> StringRef { 395 return NumberedInstructions[I]->TheDef->getName(); 396 }; 397 // TODO: Factor out duplicate operand lists to compress the tables. 398 if (!NumberedInstructions.empty()) { 399 std::vector<int> OperandOffsets; 400 std::vector<Record *> OperandRecords; 401 int CurrentOffset = 0; 402 for (const CodeGenInstruction *Inst : NumberedInstructions) { 403 OperandOffsets.push_back(CurrentOffset); 404 for (const auto &Op : Inst->Operands) { 405 const DagInit *MIOI = Op.MIOperandInfo; 406 if (!ExpandMIOperandInfo || !MIOI || MIOI->getNumArgs() == 0) { 407 // Single, anonymous, operand. 408 OperandRecords.push_back(Op.Rec); 409 ++CurrentOffset; 410 } else { 411 for (Init *Arg : MIOI->getArgs()) { 412 OperandRecords.push_back(cast<DefInit>(Arg)->getDef()); 413 ++CurrentOffset; 414 } 415 } 416 } 417 } 418 419 // Emit the table of offsets (indexes) into the operand type table. 420 // Size the unsigned integer offset to save space. 421 assert(OperandRecords.size() <= UINT32_MAX && 422 "Too many operands for offset table"); 423 OS << " static const " << getMinimalTypeForRange(OperandRecords.size()); 424 OS << " Offsets[] = {\n"; 425 for (int I = 0, E = OperandOffsets.size(); I != E; ++I) { 426 OS << " /* " << getInstrName(I) << " */\n"; 427 OS << " " << OperandOffsets[I] << ",\n"; 428 } 429 OS << " };\n"; 430 431 // Add an entry for the end so that we don't need to special case it below. 432 OperandOffsets.push_back(OperandRecords.size()); 433 434 // Emit the actual operand types in a flat table. 435 // Size the signed integer operand type to save space. 436 assert(EnumVal <= INT16_MAX && 437 "Too many operand types for operand types table"); 438 OS << "\n using namespace OpTypes;\n"; 439 OS << " static"; 440 OS << ((EnumVal <= INT8_MAX) ? " const int8_t" : " const int16_t"); 441 OS << " OpcodeOperandTypes[] = {\n "; 442 for (int I = 0, E = OperandRecords.size(), CurOffset = 0; I != E; ++I) { 443 // We print each Opcode's operands in its own row. 444 if (I == OperandOffsets[CurOffset]) { 445 OS << "\n /* " << getInstrName(CurOffset) << " */\n "; 446 while (OperandOffsets[++CurOffset] == I) 447 OS << "/* " << getInstrName(CurOffset) << " */\n "; 448 } 449 Record *OpR = OperandRecords[I]; 450 if ((OpR->isSubClassOf("Operand") || 451 OpR->isSubClassOf("RegisterOperand") || 452 OpR->isSubClassOf("RegisterClass")) && 453 !OpR->isAnonymous()) 454 OS << OpR->getName(); 455 else 456 OS << -1; 457 OS << ", "; 458 } 459 OS << "\n };\n"; 460 461 OS << " return OpcodeOperandTypes[Offsets[Opcode] + OpIdx];\n"; 462 } else { 463 OS << " llvm_unreachable(\"No instructions defined\");\n"; 464 } 465 OS << "}\n"; 466 OS << "} // end namespace " << Namespace << "\n"; 467 OS << "} // end namespace llvm\n"; 468 OS << "#endif // GET_INSTRINFO_OPERAND_TYPE\n\n"; 469 470 OS << "#ifdef GET_INSTRINFO_MEM_OPERAND_SIZE\n"; 471 OS << "#undef GET_INSTRINFO_MEM_OPERAND_SIZE\n"; 472 OS << "namespace llvm {\n"; 473 OS << "namespace " << Namespace << " {\n"; 474 OS << "LLVM_READONLY\n"; 475 OS << "static int getMemOperandSize(int OpType) {\n"; 476 OS << " switch (OpType) {\n"; 477 std::map<int, std::vector<StringRef>> SizeToOperandName; 478 for (const Record *Op : Operands) { 479 if (!Op->isSubClassOf("X86MemOperand")) 480 continue; 481 if (int Size = Op->getValueAsInt("Size")) 482 SizeToOperandName[Size].push_back(Op->getName()); 483 } 484 OS << " default: return 0;\n"; 485 for (auto KV : SizeToOperandName) { 486 for (const StringRef &OperandName : KV.second) 487 OS << " case OpTypes::" << OperandName << ":\n"; 488 OS << " return " << KV.first << ";\n\n"; 489 } 490 OS << " }\n}\n"; 491 OS << "} // end namespace " << Namespace << "\n"; 492 OS << "} // end namespace llvm\n"; 493 OS << "#endif // GET_INSTRINFO_MEM_OPERAND_SIZE\n\n"; 494 } 495 496 void InstrInfoEmitter::emitLogicalOperandSizeMappings( 497 raw_ostream &OS, StringRef Namespace, 498 ArrayRef<const CodeGenInstruction *> NumberedInstructions) { 499 std::map<std::vector<unsigned>, unsigned> LogicalOpSizeMap; 500 501 std::map<unsigned, std::vector<std::string>> InstMap; 502 503 size_t LogicalOpListSize = 0U; 504 std::vector<unsigned> LogicalOpList; 505 for (const auto *Inst : NumberedInstructions) { 506 if (!Inst->TheDef->getValueAsBit("UseLogicalOperandMappings")) 507 continue; 508 509 LogicalOpList.clear(); 510 llvm::transform(Inst->Operands, std::back_inserter(LogicalOpList), 511 [](const CGIOperandList::OperandInfo &Op) -> unsigned { 512 auto *MIOI = Op.MIOperandInfo; 513 if (!MIOI || MIOI->getNumArgs() == 0) 514 return 1; 515 return MIOI->getNumArgs(); 516 }); 517 LogicalOpListSize = std::max(LogicalOpList.size(), LogicalOpListSize); 518 519 auto I = 520 LogicalOpSizeMap.insert({LogicalOpList, LogicalOpSizeMap.size()}).first; 521 InstMap[I->second].push_back( 522 (Namespace + "::" + Inst->TheDef->getName()).str()); 523 } 524 525 OS << "#ifdef GET_INSTRINFO_LOGICAL_OPERAND_SIZE_MAP\n"; 526 OS << "#undef GET_INSTRINFO_LOGICAL_OPERAND_SIZE_MAP\n"; 527 OS << "namespace llvm {\n"; 528 OS << "namespace " << Namespace << " {\n"; 529 OS << "LLVM_READONLY static unsigned\n"; 530 OS << "getLogicalOperandSize(uint16_t Opcode, uint16_t LogicalOpIdx) {\n"; 531 if (!InstMap.empty()) { 532 std::vector<const std::vector<unsigned> *> LogicalOpSizeList( 533 LogicalOpSizeMap.size()); 534 for (auto &P : LogicalOpSizeMap) { 535 LogicalOpSizeList[P.second] = &P.first; 536 } 537 OS << " static const unsigned SizeMap[][" << LogicalOpListSize 538 << "] = {\n"; 539 for (auto &R : LogicalOpSizeList) { 540 const auto &Row = *R; 541 OS << " {"; 542 int i; 543 for (i = 0; i < static_cast<int>(Row.size()); ++i) { 544 OS << Row[i] << ", "; 545 } 546 for (; i < static_cast<int>(LogicalOpListSize); ++i) { 547 OS << "0, "; 548 } 549 OS << "}, "; 550 OS << "\n"; 551 } 552 OS << " };\n"; 553 554 OS << " switch (Opcode) {\n"; 555 OS << " default: return LogicalOpIdx;\n"; 556 for (auto &P : InstMap) { 557 auto OpMapIdx = P.first; 558 const auto &Insts = P.second; 559 for (const auto &Inst : Insts) { 560 OS << " case " << Inst << ":\n"; 561 } 562 OS << " return SizeMap[" << OpMapIdx << "][LogicalOpIdx];\n"; 563 } 564 OS << " }\n"; 565 } else { 566 OS << " return LogicalOpIdx;\n"; 567 } 568 OS << "}\n"; 569 570 OS << "LLVM_READONLY static inline unsigned\n"; 571 OS << "getLogicalOperandIdx(uint16_t Opcode, uint16_t LogicalOpIdx) {\n"; 572 OS << " auto S = 0U;\n"; 573 OS << " for (auto i = 0U; i < LogicalOpIdx; ++i)\n"; 574 OS << " S += getLogicalOperandSize(Opcode, i);\n"; 575 OS << " return S;\n"; 576 OS << "}\n"; 577 578 OS << "} // end namespace " << Namespace << "\n"; 579 OS << "} // end namespace llvm\n"; 580 OS << "#endif // GET_INSTRINFO_LOGICAL_OPERAND_SIZE_MAP\n\n"; 581 } 582 583 void InstrInfoEmitter::emitLogicalOperandTypeMappings( 584 raw_ostream &OS, StringRef Namespace, 585 ArrayRef<const CodeGenInstruction *> NumberedInstructions) { 586 std::map<std::vector<std::string>, unsigned> LogicalOpTypeMap; 587 588 std::map<unsigned, std::vector<std::string>> InstMap; 589 590 size_t OpTypeListSize = 0U; 591 std::vector<std::string> LogicalOpTypeList; 592 for (const auto *Inst : NumberedInstructions) { 593 if (!Inst->TheDef->getValueAsBit("UseLogicalOperandMappings")) 594 continue; 595 596 LogicalOpTypeList.clear(); 597 for (const auto &Op : Inst->Operands) { 598 auto *OpR = Op.Rec; 599 if ((OpR->isSubClassOf("Operand") || 600 OpR->isSubClassOf("RegisterOperand") || 601 OpR->isSubClassOf("RegisterClass")) && 602 !OpR->isAnonymous()) { 603 LogicalOpTypeList.push_back( 604 (Namespace + "::OpTypes::" + Op.Rec->getName()).str()); 605 } else { 606 LogicalOpTypeList.push_back("-1"); 607 } 608 } 609 OpTypeListSize = std::max(LogicalOpTypeList.size(), OpTypeListSize); 610 611 auto I = 612 LogicalOpTypeMap.insert({LogicalOpTypeList, LogicalOpTypeMap.size()}) 613 .first; 614 InstMap[I->second].push_back( 615 (Namespace + "::" + Inst->TheDef->getName()).str()); 616 } 617 618 OS << "#ifdef GET_INSTRINFO_LOGICAL_OPERAND_TYPE_MAP\n"; 619 OS << "#undef GET_INSTRINFO_LOGICAL_OPERAND_TYPE_MAP\n"; 620 OS << "namespace llvm {\n"; 621 OS << "namespace " << Namespace << " {\n"; 622 OS << "LLVM_READONLY static int\n"; 623 OS << "getLogicalOperandType(uint16_t Opcode, uint16_t LogicalOpIdx) {\n"; 624 if (!InstMap.empty()) { 625 std::vector<const std::vector<std::string> *> LogicalOpTypeList( 626 LogicalOpTypeMap.size()); 627 for (auto &P : LogicalOpTypeMap) { 628 LogicalOpTypeList[P.second] = &P.first; 629 } 630 OS << " static const int TypeMap[][" << OpTypeListSize << "] = {\n"; 631 for (int r = 0, rs = LogicalOpTypeList.size(); r < rs; ++r) { 632 const auto &Row = *LogicalOpTypeList[r]; 633 OS << " {"; 634 int i, s = Row.size(); 635 for (i = 0; i < s; ++i) { 636 if (i > 0) 637 OS << ", "; 638 OS << Row[i]; 639 } 640 for (; i < static_cast<int>(OpTypeListSize); ++i) { 641 if (i > 0) 642 OS << ", "; 643 OS << "-1"; 644 } 645 OS << "}"; 646 if (r != rs - 1) 647 OS << ","; 648 OS << "\n"; 649 } 650 OS << " };\n"; 651 652 OS << " switch (Opcode) {\n"; 653 OS << " default: return -1;\n"; 654 for (auto &P : InstMap) { 655 auto OpMapIdx = P.first; 656 const auto &Insts = P.second; 657 for (const auto &Inst : Insts) { 658 OS << " case " << Inst << ":\n"; 659 } 660 OS << " return TypeMap[" << OpMapIdx << "][LogicalOpIdx];\n"; 661 } 662 OS << " }\n"; 663 } else { 664 OS << " return -1;\n"; 665 } 666 OS << "}\n"; 667 OS << "} // end namespace " << Namespace << "\n"; 668 OS << "} // end namespace llvm\n"; 669 OS << "#endif // GET_INSTRINFO_LOGICAL_OPERAND_TYPE_MAP\n\n"; 670 } 671 672 void InstrInfoEmitter::emitMCIIHelperMethods(raw_ostream &OS, 673 StringRef TargetName) { 674 RecVec TIIPredicates = Records.getAllDerivedDefinitions("TIIPredicate"); 675 676 OS << "#ifdef GET_INSTRINFO_MC_HELPER_DECLS\n"; 677 OS << "#undef GET_INSTRINFO_MC_HELPER_DECLS\n\n"; 678 679 OS << "namespace llvm {\n"; 680 OS << "class MCInst;\n"; 681 OS << "class FeatureBitset;\n\n"; 682 683 OS << "namespace " << TargetName << "_MC {\n\n"; 684 685 for (const Record *Rec : TIIPredicates) { 686 OS << "bool " << Rec->getValueAsString("FunctionName") 687 << "(const MCInst &MI);\n"; 688 } 689 690 OS << "void verifyInstructionPredicates(unsigned Opcode, const FeatureBitset " 691 "&Features);\n"; 692 693 OS << "\n} // end namespace " << TargetName << "_MC\n"; 694 OS << "} // end namespace llvm\n\n"; 695 696 OS << "#endif // GET_INSTRINFO_MC_HELPER_DECLS\n\n"; 697 698 OS << "#ifdef GET_INSTRINFO_MC_HELPERS\n"; 699 OS << "#undef GET_INSTRINFO_MC_HELPERS\n\n"; 700 701 OS << "namespace llvm {\n"; 702 OS << "namespace " << TargetName << "_MC {\n\n"; 703 704 PredicateExpander PE(TargetName); 705 PE.setExpandForMC(true); 706 707 for (const Record *Rec : TIIPredicates) { 708 OS << "bool " << Rec->getValueAsString("FunctionName"); 709 OS << "(const MCInst &MI) {\n"; 710 711 OS.indent(PE.getIndentLevel() * 2); 712 PE.expandStatement(OS, Rec->getValueAsDef("Body")); 713 OS << "\n}\n\n"; 714 } 715 716 OS << "} // end namespace " << TargetName << "_MC\n"; 717 OS << "} // end namespace llvm\n\n"; 718 719 OS << "#endif // GET_GENISTRINFO_MC_HELPERS\n\n"; 720 } 721 722 static std::string 723 getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) { 724 std::string Name = "CEFBS"; 725 for (const auto &Feature : FeatureBitset) 726 Name += ("_" + Feature->getName()).str(); 727 return Name; 728 } 729 730 void InstrInfoEmitter::emitFeatureVerifier(raw_ostream &OS, 731 const CodeGenTarget &Target) { 732 const auto &All = SubtargetFeatureInfo::getAll(Records); 733 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> SubtargetFeatures; 734 SubtargetFeatures.insert(All.begin(), All.end()); 735 736 OS << "#if defined(ENABLE_INSTR_PREDICATE_VERIFIER) && !defined(NDEBUG)\n" 737 << "#define GET_COMPUTE_FEATURES\n" 738 << "#endif\n"; 739 OS << "#ifdef GET_COMPUTE_FEATURES\n" 740 << "#undef GET_COMPUTE_FEATURES\n" 741 << "namespace llvm {\n" 742 << "namespace " << Target.getName() << "_MC {\n\n"; 743 744 // Emit the subtarget feature enumeration. 745 SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(SubtargetFeatures, 746 OS); 747 // Emit the available features compute function. 748 OS << "inline "; 749 SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures( 750 Target.getName(), "", "computeAvailableFeatures", SubtargetFeatures, OS); 751 752 std::vector<std::vector<Record *>> FeatureBitsets; 753 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) { 754 FeatureBitsets.emplace_back(); 755 for (Record *Predicate : Inst->TheDef->getValueAsListOfDefs("Predicates")) { 756 const auto &I = SubtargetFeatures.find(Predicate); 757 if (I != SubtargetFeatures.end()) 758 FeatureBitsets.back().push_back(I->second.TheDef); 759 } 760 } 761 762 llvm::sort(FeatureBitsets, [&](const std::vector<Record *> &A, 763 const std::vector<Record *> &B) { 764 if (A.size() < B.size()) 765 return true; 766 if (A.size() > B.size()) 767 return false; 768 for (auto Pair : zip(A, B)) { 769 if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName()) 770 return true; 771 if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName()) 772 return false; 773 } 774 return false; 775 }); 776 FeatureBitsets.erase( 777 std::unique(FeatureBitsets.begin(), FeatureBitsets.end()), 778 FeatureBitsets.end()); 779 OS << "inline FeatureBitset computeRequiredFeatures(unsigned Opcode) {\n" 780 << " enum : " << getMinimalTypeForRange(FeatureBitsets.size()) << " {\n" 781 << " CEFBS_None,\n"; 782 for (const auto &FeatureBitset : FeatureBitsets) { 783 if (FeatureBitset.empty()) 784 continue; 785 OS << " " << getNameForFeatureBitset(FeatureBitset) << ",\n"; 786 } 787 OS << " };\n\n" 788 << " static constexpr FeatureBitset FeatureBitsets[] = {\n" 789 << " {}, // CEFBS_None\n"; 790 for (const auto &FeatureBitset : FeatureBitsets) { 791 if (FeatureBitset.empty()) 792 continue; 793 OS << " {"; 794 for (const auto &Feature : FeatureBitset) { 795 const auto &I = SubtargetFeatures.find(Feature); 796 assert(I != SubtargetFeatures.end() && "Didn't import predicate?"); 797 OS << I->second.getEnumBitName() << ", "; 798 } 799 OS << "},\n"; 800 } 801 OS << " };\n" 802 << " static " << getMinimalTypeForRange(FeatureBitsets.size()) 803 << " RequiredFeaturesRefs[] = {\n"; 804 unsigned InstIdx = 0; 805 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) { 806 OS << " CEFBS"; 807 unsigned NumPredicates = 0; 808 for (Record *Predicate : Inst->TheDef->getValueAsListOfDefs("Predicates")) { 809 const auto &I = SubtargetFeatures.find(Predicate); 810 if (I != SubtargetFeatures.end()) { 811 OS << '_' << I->second.TheDef->getName(); 812 NumPredicates++; 813 } 814 } 815 if (!NumPredicates) 816 OS << "_None"; 817 OS << ", // " << Inst->TheDef->getName() << " = " << InstIdx << "\n"; 818 InstIdx++; 819 } 820 OS << " };\n\n" 821 << " assert(Opcode < " << InstIdx << ");\n" 822 << " return FeatureBitsets[RequiredFeaturesRefs[Opcode]];\n" 823 << "}\n\n"; 824 825 OS << "} // end namespace " << Target.getName() << "_MC\n" 826 << "} // end namespace llvm\n" 827 << "#endif // GET_COMPUTE_FEATURES\n\n"; 828 829 OS << "#ifdef ENABLE_INSTR_PREDICATE_VERIFIER\n" 830 << "#undef ENABLE_INSTR_PREDICATE_VERIFIER\n" 831 << "#include <sstream>\n\n"; 832 833 OS << "namespace llvm {\n"; 834 OS << "namespace " << Target.getName() << "_MC {\n\n"; 835 836 // Emit the name table for error messages. 837 OS << "#ifndef NDEBUG\n"; 838 SubtargetFeatureInfo::emitNameTable(SubtargetFeatures, OS); 839 OS << "#endif // NDEBUG\n\n"; 840 841 // Emit the predicate verifier. 842 OS << "void verifyInstructionPredicates(\n" 843 << " unsigned Opcode, const FeatureBitset &Features) {\n" 844 << "#ifndef NDEBUG\n"; 845 OS << " FeatureBitset AvailableFeatures = " 846 "computeAvailableFeatures(Features);\n"; 847 OS << " FeatureBitset RequiredFeatures = " 848 << "computeRequiredFeatures(Opcode);\n"; 849 OS << " FeatureBitset MissingFeatures =\n" 850 << " (AvailableFeatures & RequiredFeatures) ^\n" 851 << " RequiredFeatures;\n" 852 << " if (MissingFeatures.any()) {\n" 853 << " std::ostringstream Msg;\n" 854 << " Msg << \"Attempting to emit \" << &" << Target.getName() 855 << "InstrNameData[" << Target.getName() << "InstrNameIndices[Opcode]]\n" 856 << " << \" instruction but the \";\n" 857 << " for (unsigned i = 0, e = MissingFeatures.size(); i != e; ++i)\n" 858 << " if (MissingFeatures.test(i))\n" 859 << " Msg << SubtargetFeatureNames[i] << \" \";\n" 860 << " Msg << \"predicate(s) are not met\";\n" 861 << " report_fatal_error(Msg.str().c_str());\n" 862 << " }\n" 863 << "#endif // NDEBUG\n"; 864 OS << "}\n"; 865 OS << "} // end namespace " << Target.getName() << "_MC\n"; 866 OS << "} // end namespace llvm\n"; 867 OS << "#endif // ENABLE_INSTR_PREDICATE_VERIFIER\n\n"; 868 } 869 870 void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS, 871 StringRef TargetName, 872 bool ExpandDefinition) { 873 RecVec TIIPredicates = Records.getAllDerivedDefinitions("TIIPredicate"); 874 if (TIIPredicates.empty()) 875 return; 876 877 PredicateExpander PE(TargetName); 878 PE.setExpandForMC(false); 879 880 for (const Record *Rec : TIIPredicates) { 881 OS << (ExpandDefinition ? "" : "static ") << "bool "; 882 if (ExpandDefinition) 883 OS << TargetName << "InstrInfo::"; 884 OS << Rec->getValueAsString("FunctionName"); 885 OS << "(const MachineInstr &MI)"; 886 if (!ExpandDefinition) { 887 OS << ";\n"; 888 continue; 889 } 890 891 OS << " {\n"; 892 OS.indent(PE.getIndentLevel() * 2); 893 PE.expandStatement(OS, Rec->getValueAsDef("Body")); 894 OS << "\n}\n\n"; 895 } 896 } 897 898 //===----------------------------------------------------------------------===// 899 // Main Output. 900 //===----------------------------------------------------------------------===// 901 902 // run - Emit the main instruction description records for the target... 903 void InstrInfoEmitter::run(raw_ostream &OS) { 904 emitSourceFileHeader("Target Instruction Enum Values and Descriptors", OS); 905 emitEnums(OS); 906 907 CodeGenTarget &Target = CDP.getTargetInfo(); 908 const std::string &TargetName = std::string(Target.getName()); 909 Record *InstrInfo = Target.getInstructionSet(); 910 911 // Collect all of the operand info records. 912 Records.startTimer("Collect operand info"); 913 OperandInfoListTy OperandInfoList; 914 OperandInfoMapTy OperandInfoMap; 915 unsigned OperandInfoSize = 916 CollectOperandInfo(OperandInfoList, OperandInfoMap); 917 918 // Collect all of the instruction's implicit uses and defs. 919 Records.startTimer("Collect uses/defs"); 920 std::map<std::vector<Record*>, unsigned> EmittedLists; 921 std::vector<std::vector<Record *>> ImplicitLists; 922 unsigned ImplicitListSize = 0; 923 for (const CodeGenInstruction *II : Target.getInstructionsByEnumValue()) { 924 std::vector<Record *> ImplicitOps = II->ImplicitUses; 925 llvm::append_range(ImplicitOps, II->ImplicitDefs); 926 if (EmittedLists.insert({ImplicitOps, ImplicitListSize}).second) { 927 ImplicitLists.push_back(ImplicitOps); 928 ImplicitListSize += ImplicitOps.size(); 929 } 930 } 931 932 ArrayRef<const CodeGenInstruction *> NumberedInstructions = 933 Target.getInstructionsByEnumValue(); 934 OS << "#if defined(GET_INSTRINFO_MC_DESC) || " 935 "defined(GET_INSTRINFO_CTOR_DTOR)\n"; 936 OS << "namespace llvm {\n\n"; 937 938 OS << "struct " << TargetName << "InstrTable {\n"; 939 OS << " MCInstrDesc Insts[" << NumberedInstructions.size() << "];\n"; 940 OS << " static_assert(alignof(MCInstrDesc) >= alignof(MCOperandInfo), " 941 "\"Unwanted padding between Insts and OperandInfo\");\n"; 942 OS << " MCOperandInfo OperandInfo[" << OperandInfoSize << "];\n"; 943 OS << " static_assert(alignof(MCOperandInfo) >= alignof(MCPhysReg), " 944 "\"Unwanted padding between OperandInfo and ImplicitOps\");\n"; 945 OS << " MCPhysReg ImplicitOps[" << std::max(ImplicitListSize, 1U) << "];\n"; 946 OS << "};\n\n"; 947 948 OS << "} // end namespace llvm\n"; 949 OS << "#endif // defined(GET_INSTRINFO_MC_DESC) || " 950 "defined(GET_INSTRINFO_CTOR_DTOR)\n\n"; 951 952 OS << "#ifdef GET_INSTRINFO_MC_DESC\n"; 953 OS << "#undef GET_INSTRINFO_MC_DESC\n"; 954 OS << "namespace llvm {\n\n"; 955 956 // Emit all of the MCInstrDesc records in reverse ENUM ordering. 957 Records.startTimer("Emit InstrDesc records"); 958 OS << "static_assert(sizeof(MCOperandInfo) % sizeof(MCPhysReg) == 0);\n"; 959 OS << "static constexpr unsigned " << TargetName << "ImpOpBase = sizeof " 960 << TargetName << "InstrTable::OperandInfo / (sizeof(MCPhysReg));\n\n"; 961 962 OS << "extern const " << TargetName << "InstrTable " << TargetName 963 << "Descs = {\n {\n"; 964 SequenceToOffsetTable<std::string> InstrNames; 965 unsigned Num = NumberedInstructions.size(); 966 for (const CodeGenInstruction *Inst : reverse(NumberedInstructions)) { 967 // Keep a list of the instruction names. 968 InstrNames.add(std::string(Inst->TheDef->getName())); 969 // Emit the record into the table. 970 emitRecord(*Inst, --Num, InstrInfo, EmittedLists, OperandInfoMap, OS); 971 } 972 973 OS << " }, {\n"; 974 975 // Emit all of the operand info records. 976 Records.startTimer("Emit operand info"); 977 EmitOperandInfo(OS, OperandInfoList); 978 979 OS << " }, {\n"; 980 981 // Emit all of the instruction's implicit uses and defs. 982 Records.startTimer("Emit uses/defs"); 983 for (auto &List : ImplicitLists) { 984 OS << " /* " << EmittedLists[List] << " */"; 985 for (auto &Reg : List) 986 OS << ' ' << getQualifiedName(Reg) << ','; 987 OS << '\n'; 988 } 989 990 OS << " }\n};\n\n"; 991 992 // Emit the array of instruction names. 993 Records.startTimer("Emit instruction names"); 994 InstrNames.layout(); 995 InstrNames.emitStringLiteralDef(OS, Twine("extern const char ") + TargetName + 996 "InstrNameData[]"); 997 998 OS << "extern const unsigned " << TargetName <<"InstrNameIndices[] = {"; 999 Num = 0; 1000 for (const CodeGenInstruction *Inst : NumberedInstructions) { 1001 // Newline every eight entries. 1002 if (Num % 8 == 0) 1003 OS << "\n "; 1004 OS << InstrNames.get(std::string(Inst->TheDef->getName())) << "U, "; 1005 ++Num; 1006 } 1007 OS << "\n};\n\n"; 1008 1009 bool HasDeprecationFeatures = 1010 llvm::any_of(NumberedInstructions, [](const CodeGenInstruction *Inst) { 1011 return !Inst->HasComplexDeprecationPredicate && 1012 !Inst->DeprecatedReason.empty(); 1013 }); 1014 if (HasDeprecationFeatures) { 1015 OS << "extern const uint8_t " << TargetName 1016 << "InstrDeprecationFeatures[] = {"; 1017 Num = 0; 1018 for (const CodeGenInstruction *Inst : NumberedInstructions) { 1019 if (Num % 8 == 0) 1020 OS << "\n "; 1021 if (!Inst->HasComplexDeprecationPredicate && 1022 !Inst->DeprecatedReason.empty()) 1023 OS << Target.getInstNamespace() << "::" << Inst->DeprecatedReason 1024 << ", "; 1025 else 1026 OS << "uint8_t(-1), "; 1027 ++Num; 1028 } 1029 OS << "\n};\n\n"; 1030 } 1031 1032 bool HasComplexDeprecationInfos = 1033 llvm::any_of(NumberedInstructions, [](const CodeGenInstruction *Inst) { 1034 return Inst->HasComplexDeprecationPredicate; 1035 }); 1036 if (HasComplexDeprecationInfos) { 1037 OS << "extern const MCInstrInfo::ComplexDeprecationPredicate " << TargetName 1038 << "InstrComplexDeprecationInfos[] = {"; 1039 Num = 0; 1040 for (const CodeGenInstruction *Inst : NumberedInstructions) { 1041 if (Num % 8 == 0) 1042 OS << "\n "; 1043 if (Inst->HasComplexDeprecationPredicate) 1044 // Emit a function pointer to the complex predicate method. 1045 OS << "&get" << Inst->DeprecatedReason << "DeprecationInfo, "; 1046 else 1047 OS << "nullptr, "; 1048 ++Num; 1049 } 1050 OS << "\n};\n\n"; 1051 } 1052 1053 // MCInstrInfo initialization routine. 1054 Records.startTimer("Emit initialization routine"); 1055 OS << "static inline void Init" << TargetName 1056 << "MCInstrInfo(MCInstrInfo *II) {\n"; 1057 OS << " II->InitMCInstrInfo(" << TargetName << "Descs.Insts, " << TargetName 1058 << "InstrNameIndices, " << TargetName << "InstrNameData, "; 1059 if (HasDeprecationFeatures) 1060 OS << TargetName << "InstrDeprecationFeatures, "; 1061 else 1062 OS << "nullptr, "; 1063 if (HasComplexDeprecationInfos) 1064 OS << TargetName << "InstrComplexDeprecationInfos, "; 1065 else 1066 OS << "nullptr, "; 1067 OS << NumberedInstructions.size() << ");\n}\n\n"; 1068 1069 OS << "} // end namespace llvm\n"; 1070 1071 OS << "#endif // GET_INSTRINFO_MC_DESC\n\n"; 1072 1073 // Create a TargetInstrInfo subclass to hide the MC layer initialization. 1074 OS << "#ifdef GET_INSTRINFO_HEADER\n"; 1075 OS << "#undef GET_INSTRINFO_HEADER\n"; 1076 1077 std::string ClassName = TargetName + "GenInstrInfo"; 1078 OS << "namespace llvm {\n"; 1079 OS << "struct " << ClassName << " : public TargetInstrInfo {\n" 1080 << " explicit " << ClassName 1081 << "(unsigned CFSetupOpcode = ~0u, unsigned CFDestroyOpcode = ~0u, " 1082 "unsigned CatchRetOpcode = ~0u, unsigned ReturnOpcode = ~0u);\n" 1083 << " ~" << ClassName << "() override = default;\n"; 1084 1085 1086 OS << "\n};\n} // end namespace llvm\n"; 1087 1088 OS << "#endif // GET_INSTRINFO_HEADER\n\n"; 1089 1090 OS << "#ifdef GET_INSTRINFO_HELPER_DECLS\n"; 1091 OS << "#undef GET_INSTRINFO_HELPER_DECLS\n\n"; 1092 emitTIIHelperMethods(OS, TargetName, /* ExpandDefinition = */ false); 1093 OS << "\n"; 1094 OS << "#endif // GET_INSTRINFO_HELPER_DECLS\n\n"; 1095 1096 OS << "#ifdef GET_INSTRINFO_HELPERS\n"; 1097 OS << "#undef GET_INSTRINFO_HELPERS\n\n"; 1098 emitTIIHelperMethods(OS, TargetName, /* ExpandDefinition = */ true); 1099 OS << "#endif // GET_INSTRINFO_HELPERS\n\n"; 1100 1101 OS << "#ifdef GET_INSTRINFO_CTOR_DTOR\n"; 1102 OS << "#undef GET_INSTRINFO_CTOR_DTOR\n"; 1103 1104 OS << "namespace llvm {\n"; 1105 OS << "extern const " << TargetName << "InstrTable " << TargetName 1106 << "Descs;\n"; 1107 OS << "extern const unsigned " << TargetName << "InstrNameIndices[];\n"; 1108 OS << "extern const char " << TargetName << "InstrNameData[];\n"; 1109 if (HasDeprecationFeatures) 1110 OS << "extern const uint8_t " << TargetName 1111 << "InstrDeprecationFeatures[];\n"; 1112 if (HasComplexDeprecationInfos) 1113 OS << "extern const MCInstrInfo::ComplexDeprecationPredicate " << TargetName 1114 << "InstrComplexDeprecationInfos[];\n"; 1115 OS << ClassName << "::" << ClassName 1116 << "(unsigned CFSetupOpcode, unsigned CFDestroyOpcode, unsigned " 1117 "CatchRetOpcode, unsigned ReturnOpcode)\n" 1118 << " : TargetInstrInfo(CFSetupOpcode, CFDestroyOpcode, CatchRetOpcode, " 1119 "ReturnOpcode) {\n" 1120 << " InitMCInstrInfo(" << TargetName << "Descs.Insts, " << TargetName 1121 << "InstrNameIndices, " << TargetName << "InstrNameData, "; 1122 if (HasDeprecationFeatures) 1123 OS << TargetName << "InstrDeprecationFeatures, "; 1124 else 1125 OS << "nullptr, "; 1126 if (HasComplexDeprecationInfos) 1127 OS << TargetName << "InstrComplexDeprecationInfos, "; 1128 else 1129 OS << "nullptr, "; 1130 OS << NumberedInstructions.size() << ");\n}\n"; 1131 OS << "} // end namespace llvm\n"; 1132 1133 OS << "#endif // GET_INSTRINFO_CTOR_DTOR\n\n"; 1134 1135 Records.startTimer("Emit operand name mappings"); 1136 emitOperandNameMappings(OS, Target, NumberedInstructions); 1137 1138 Records.startTimer("Emit operand type mappings"); 1139 emitOperandTypeMappings(OS, Target, NumberedInstructions); 1140 1141 Records.startTimer("Emit logical operand size mappings"); 1142 emitLogicalOperandSizeMappings(OS, TargetName, NumberedInstructions); 1143 1144 Records.startTimer("Emit logical operand type mappings"); 1145 emitLogicalOperandTypeMappings(OS, TargetName, NumberedInstructions); 1146 1147 Records.startTimer("Emit helper methods"); 1148 emitMCIIHelperMethods(OS, TargetName); 1149 1150 Records.startTimer("Emit verifier methods"); 1151 emitFeatureVerifier(OS, Target); 1152 } 1153 1154 void InstrInfoEmitter::emitRecord( 1155 const CodeGenInstruction &Inst, unsigned Num, Record *InstrInfo, 1156 std::map<std::vector<Record *>, unsigned> &EmittedLists, 1157 const OperandInfoMapTy &OperandInfoMap, raw_ostream &OS) { 1158 int MinOperands = 0; 1159 if (!Inst.Operands.empty()) 1160 // Each logical operand can be multiple MI operands. 1161 MinOperands = Inst.Operands.back().MIOperandNo + 1162 Inst.Operands.back().MINumOperands; 1163 1164 OS << " { "; 1165 OS << Num << ",\t" << MinOperands << ",\t" << Inst.Operands.NumDefs << ",\t" 1166 << Inst.TheDef->getValueAsInt("Size") << ",\t" 1167 << SchedModels.getSchedClassIdx(Inst) << ",\t"; 1168 1169 CodeGenTarget &Target = CDP.getTargetInfo(); 1170 1171 // Emit the implicit use/def list... 1172 OS << Inst.ImplicitUses.size() << ",\t" << Inst.ImplicitDefs.size() << ",\t"; 1173 std::vector<Record *> ImplicitOps = Inst.ImplicitUses; 1174 llvm::append_range(ImplicitOps, Inst.ImplicitDefs); 1175 OS << Target.getName() << "ImpOpBase + " << EmittedLists[ImplicitOps] 1176 << ",\t"; 1177 1178 // Emit the operand info offset. 1179 OperandInfoTy OperandInfo = GetOperandInfo(Inst); 1180 OS << OperandInfoMap.find(OperandInfo)->second << ",\t0"; 1181 1182 // Emit all of the target independent flags... 1183 if (Inst.isPreISelOpcode) OS << "|(1ULL<<MCID::PreISelOpcode)"; 1184 if (Inst.isPseudo) OS << "|(1ULL<<MCID::Pseudo)"; 1185 if (Inst.isMeta) OS << "|(1ULL<<MCID::Meta)"; 1186 if (Inst.isReturn) OS << "|(1ULL<<MCID::Return)"; 1187 if (Inst.isEHScopeReturn) OS << "|(1ULL<<MCID::EHScopeReturn)"; 1188 if (Inst.isBranch) OS << "|(1ULL<<MCID::Branch)"; 1189 if (Inst.isIndirectBranch) OS << "|(1ULL<<MCID::IndirectBranch)"; 1190 if (Inst.isCompare) OS << "|(1ULL<<MCID::Compare)"; 1191 if (Inst.isMoveImm) OS << "|(1ULL<<MCID::MoveImm)"; 1192 if (Inst.isMoveReg) OS << "|(1ULL<<MCID::MoveReg)"; 1193 if (Inst.isBitcast) OS << "|(1ULL<<MCID::Bitcast)"; 1194 if (Inst.isAdd) OS << "|(1ULL<<MCID::Add)"; 1195 if (Inst.isTrap) OS << "|(1ULL<<MCID::Trap)"; 1196 if (Inst.isSelect) OS << "|(1ULL<<MCID::Select)"; 1197 if (Inst.isBarrier) OS << "|(1ULL<<MCID::Barrier)"; 1198 if (Inst.hasDelaySlot) OS << "|(1ULL<<MCID::DelaySlot)"; 1199 if (Inst.isCall) OS << "|(1ULL<<MCID::Call)"; 1200 if (Inst.canFoldAsLoad) OS << "|(1ULL<<MCID::FoldableAsLoad)"; 1201 if (Inst.mayLoad) OS << "|(1ULL<<MCID::MayLoad)"; 1202 if (Inst.mayStore) OS << "|(1ULL<<MCID::MayStore)"; 1203 if (Inst.mayRaiseFPException) OS << "|(1ULL<<MCID::MayRaiseFPException)"; 1204 if (Inst.isPredicable) OS << "|(1ULL<<MCID::Predicable)"; 1205 if (Inst.isConvertibleToThreeAddress) OS << "|(1ULL<<MCID::ConvertibleTo3Addr)"; 1206 if (Inst.isCommutable) OS << "|(1ULL<<MCID::Commutable)"; 1207 if (Inst.isTerminator) OS << "|(1ULL<<MCID::Terminator)"; 1208 if (Inst.isReMaterializable) OS << "|(1ULL<<MCID::Rematerializable)"; 1209 if (Inst.isNotDuplicable) OS << "|(1ULL<<MCID::NotDuplicable)"; 1210 if (Inst.Operands.hasOptionalDef) OS << "|(1ULL<<MCID::HasOptionalDef)"; 1211 if (Inst.usesCustomInserter) OS << "|(1ULL<<MCID::UsesCustomInserter)"; 1212 if (Inst.hasPostISelHook) OS << "|(1ULL<<MCID::HasPostISelHook)"; 1213 if (Inst.Operands.isVariadic)OS << "|(1ULL<<MCID::Variadic)"; 1214 if (Inst.hasSideEffects) OS << "|(1ULL<<MCID::UnmodeledSideEffects)"; 1215 if (Inst.isAsCheapAsAMove) OS << "|(1ULL<<MCID::CheapAsAMove)"; 1216 if (!Target.getAllowRegisterRenaming() || Inst.hasExtraSrcRegAllocReq) 1217 OS << "|(1ULL<<MCID::ExtraSrcRegAllocReq)"; 1218 if (!Target.getAllowRegisterRenaming() || Inst.hasExtraDefRegAllocReq) 1219 OS << "|(1ULL<<MCID::ExtraDefRegAllocReq)"; 1220 if (Inst.isRegSequence) OS << "|(1ULL<<MCID::RegSequence)"; 1221 if (Inst.isExtractSubreg) OS << "|(1ULL<<MCID::ExtractSubreg)"; 1222 if (Inst.isInsertSubreg) OS << "|(1ULL<<MCID::InsertSubreg)"; 1223 if (Inst.isConvergent) OS << "|(1ULL<<MCID::Convergent)"; 1224 if (Inst.variadicOpsAreDefs) OS << "|(1ULL<<MCID::VariadicOpsAreDefs)"; 1225 if (Inst.isAuthenticated) OS << "|(1ULL<<MCID::Authenticated)"; 1226 1227 // Emit all of the target-specific flags... 1228 BitsInit *TSF = Inst.TheDef->getValueAsBitsInit("TSFlags"); 1229 if (!TSF) 1230 PrintFatalError(Inst.TheDef->getLoc(), "no TSFlags?"); 1231 uint64_t Value = 0; 1232 for (unsigned i = 0, e = TSF->getNumBits(); i != e; ++i) { 1233 if (const auto *Bit = dyn_cast<BitInit>(TSF->getBit(i))) 1234 Value |= uint64_t(Bit->getValue()) << i; 1235 else 1236 PrintFatalError(Inst.TheDef->getLoc(), 1237 "Invalid TSFlags bit in " + Inst.TheDef->getName()); 1238 } 1239 OS << ", 0x"; 1240 OS.write_hex(Value); 1241 OS << "ULL"; 1242 1243 OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n"; 1244 } 1245 1246 // emitEnums - Print out enum values for all of the instructions. 1247 void InstrInfoEmitter::emitEnums(raw_ostream &OS) { 1248 OS << "#ifdef GET_INSTRINFO_ENUM\n"; 1249 OS << "#undef GET_INSTRINFO_ENUM\n"; 1250 1251 OS << "namespace llvm {\n\n"; 1252 1253 const CodeGenTarget &Target = CDP.getTargetInfo(); 1254 1255 // We must emit the PHI opcode first... 1256 StringRef Namespace = Target.getInstNamespace(); 1257 1258 if (Namespace.empty()) 1259 PrintFatalError("No instructions defined!"); 1260 1261 OS << "namespace " << Namespace << " {\n"; 1262 OS << " enum {\n"; 1263 unsigned Num = 0; 1264 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) 1265 OS << " " << Inst->TheDef->getName() << "\t= " << Num++ << ",\n"; 1266 OS << " INSTRUCTION_LIST_END = " << Num << "\n"; 1267 OS << " };\n\n"; 1268 OS << "} // end namespace " << Namespace << "\n"; 1269 OS << "} // end namespace llvm\n"; 1270 OS << "#endif // GET_INSTRINFO_ENUM\n\n"; 1271 1272 OS << "#ifdef GET_INSTRINFO_SCHED_ENUM\n"; 1273 OS << "#undef GET_INSTRINFO_SCHED_ENUM\n"; 1274 OS << "namespace llvm {\n\n"; 1275 OS << "namespace " << Namespace << " {\n"; 1276 OS << "namespace Sched {\n"; 1277 OS << " enum {\n"; 1278 Num = 0; 1279 for (const auto &Class : SchedModels.explicit_classes()) 1280 OS << " " << Class.Name << "\t= " << Num++ << ",\n"; 1281 OS << " SCHED_LIST_END = " << Num << "\n"; 1282 OS << " };\n"; 1283 OS << "} // end namespace Sched\n"; 1284 OS << "} // end namespace " << Namespace << "\n"; 1285 OS << "} // end namespace llvm\n"; 1286 1287 OS << "#endif // GET_INSTRINFO_SCHED_ENUM\n\n"; 1288 } 1289 1290 static void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) { 1291 RK.startTimer("Analyze DAG patterns"); 1292 InstrInfoEmitter(RK).run(OS); 1293 RK.startTimer("Emit map table"); 1294 EmitMapTable(RK, OS); 1295 } 1296 1297 static TableGen::Emitter::Opt X("gen-instr-info", EmitInstrInfo, 1298 "Generate instruction descriptions"); 1299