1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This tablegen backend emits information about intrinsic functions. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CodeGenIntrinsics.h" 14 #include "CodeGenTarget.h" 15 #include "SequenceToOffsetTable.h" 16 #include "TableGenBackends.h" 17 #include "llvm/ADT/StringExtras.h" 18 #include "llvm/Support/CommandLine.h" 19 #include "llvm/TableGen/Error.h" 20 #include "llvm/TableGen/Record.h" 21 #include "llvm/TableGen/StringMatcher.h" 22 #include "llvm/TableGen/StringToOffsetTable.h" 23 #include "llvm/TableGen/TableGenBackend.h" 24 #include <algorithm> 25 using namespace llvm; 26 27 cl::OptionCategory GenIntrinsicCat("Options for -gen-intrinsic-enums"); 28 cl::opt<std::string> 29 IntrinsicPrefix("intrinsic-prefix", 30 cl::desc("Generate intrinsics with this target prefix"), 31 cl::value_desc("target prefix"), cl::cat(GenIntrinsicCat)); 32 33 namespace { 34 class IntrinsicEmitter { 35 RecordKeeper &Records; 36 37 public: 38 IntrinsicEmitter(RecordKeeper &R) : Records(R) {} 39 40 void run(raw_ostream &OS, bool Enums); 41 42 void EmitEnumInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS); 43 void EmitTargetInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS); 44 void EmitIntrinsicToNameTable(const CodeGenIntrinsicTable &Ints, 45 raw_ostream &OS); 46 void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints, 47 raw_ostream &OS); 48 void EmitGenerator(const CodeGenIntrinsicTable &Ints, raw_ostream &OS); 49 void EmitAttributes(const CodeGenIntrinsicTable &Ints, raw_ostream &OS); 50 void EmitIntrinsicToBuiltinMap(const CodeGenIntrinsicTable &Ints, bool IsGCC, 51 raw_ostream &OS); 52 }; 53 } // End anonymous namespace 54 55 //===----------------------------------------------------------------------===// 56 // IntrinsicEmitter Implementation 57 //===----------------------------------------------------------------------===// 58 59 void IntrinsicEmitter::run(raw_ostream &OS, bool Enums) { 60 emitSourceFileHeader("Intrinsic Function Source Fragment", OS); 61 62 CodeGenIntrinsicTable Ints(Records); 63 64 if (Enums) { 65 // Emit the enum information. 66 EmitEnumInfo(Ints, OS); 67 } else { 68 // Emit the target metadata. 69 EmitTargetInfo(Ints, OS); 70 71 // Emit the intrinsic ID -> name table. 72 EmitIntrinsicToNameTable(Ints, OS); 73 74 // Emit the intrinsic ID -> overload table. 75 EmitIntrinsicToOverloadTable(Ints, OS); 76 77 // Emit the intrinsic declaration generator. 78 EmitGenerator(Ints, OS); 79 80 // Emit the intrinsic parameter attributes. 81 EmitAttributes(Ints, OS); 82 83 // Emit code to translate GCC builtins into LLVM intrinsics. 84 EmitIntrinsicToBuiltinMap(Ints, true, OS); 85 86 // Emit code to translate MS builtins into LLVM intrinsics. 87 EmitIntrinsicToBuiltinMap(Ints, false, OS); 88 } 89 } 90 91 void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints, 92 raw_ostream &OS) { 93 // Find the TargetSet for which to generate enums. There will be an initial 94 // set with an empty target prefix which will include target independent 95 // intrinsics like dbg.value. 96 const CodeGenIntrinsicTable::TargetSet *Set = nullptr; 97 for (const auto &Target : Ints.Targets) { 98 if (Target.Name == IntrinsicPrefix) { 99 Set = &Target; 100 break; 101 } 102 } 103 if (!Set) { 104 std::vector<std::string> KnownTargets; 105 for (const auto &Target : Ints.Targets) 106 if (!Target.Name.empty()) 107 KnownTargets.push_back(Target.Name); 108 PrintFatalError("tried to generate intrinsics for unknown target " + 109 IntrinsicPrefix + 110 "\nKnown targets are: " + join(KnownTargets, ", ") + "\n"); 111 } 112 113 // Generate a complete header for target specific intrinsics. 114 if (!IntrinsicPrefix.empty()) { 115 std::string UpperPrefix = StringRef(IntrinsicPrefix).upper(); 116 OS << "#ifndef LLVM_IR_INTRINSIC_" << UpperPrefix << "_ENUMS_H\n"; 117 OS << "#define LLVM_IR_INTRINSIC_" << UpperPrefix << "_ENUMS_H\n\n"; 118 OS << "namespace llvm {\n"; 119 OS << "namespace Intrinsic {\n"; 120 OS << "enum " << UpperPrefix << "Intrinsics : unsigned {\n"; 121 } 122 123 OS << "// Enum values for intrinsics\n"; 124 for (unsigned i = Set->Offset, e = Set->Offset + Set->Count; i != e; ++i) { 125 OS << " " << Ints[i].EnumName; 126 127 // Assign a value to the first intrinsic in this target set so that all 128 // intrinsic ids are distinct. 129 if (i == Set->Offset) 130 OS << " = " << (Set->Offset + 1); 131 132 OS << ", "; 133 if (Ints[i].EnumName.size() < 40) 134 OS.indent(40 - Ints[i].EnumName.size()); 135 OS << " // " << Ints[i].Name << "\n"; 136 } 137 138 // Emit num_intrinsics into the target neutral enum. 139 if (IntrinsicPrefix.empty()) { 140 OS << " num_intrinsics = " << (Ints.size() + 1) << "\n"; 141 } else { 142 OS << "}; // enum\n"; 143 OS << "} // namespace Intrinsic\n"; 144 OS << "} // namespace llvm\n\n"; 145 OS << "#endif\n"; 146 } 147 } 148 149 void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints, 150 raw_ostream &OS) { 151 OS << "// Target mapping\n"; 152 OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n"; 153 OS << "struct IntrinsicTargetInfo {\n" 154 << " llvm::StringLiteral Name;\n" 155 << " size_t Offset;\n" 156 << " size_t Count;\n" 157 << "};\n"; 158 OS << "static constexpr IntrinsicTargetInfo TargetInfos[] = {\n"; 159 for (auto Target : Ints.Targets) 160 OS << " {llvm::StringLiteral(\"" << Target.Name << "\"), " << Target.Offset 161 << ", " << Target.Count << "},\n"; 162 OS << "};\n"; 163 OS << "#endif\n\n"; 164 } 165 166 void IntrinsicEmitter::EmitIntrinsicToNameTable( 167 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) { 168 OS << "// Intrinsic ID to name table\n"; 169 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n"; 170 OS << " // Note that entry #0 is the invalid intrinsic!\n"; 171 for (unsigned i = 0, e = Ints.size(); i != e; ++i) 172 OS << " \"" << Ints[i].Name << "\",\n"; 173 OS << "#endif\n\n"; 174 } 175 176 void IntrinsicEmitter::EmitIntrinsicToOverloadTable( 177 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) { 178 OS << "// Intrinsic ID to overload bitset\n"; 179 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n"; 180 OS << "static const uint8_t OTable[] = {\n"; 181 OS << " 0"; 182 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 183 // Add one to the index so we emit a null bit for the invalid #0 intrinsic. 184 if ((i+1)%8 == 0) 185 OS << ",\n 0"; 186 if (Ints[i].isOverloaded) 187 OS << " | (1<<" << (i+1)%8 << ')'; 188 } 189 OS << "\n};\n\n"; 190 // OTable contains a true bit at the position if the intrinsic is overloaded. 191 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n"; 192 OS << "#endif\n\n"; 193 } 194 195 196 // NOTE: This must be kept in synch with the copy in lib/IR/Function.cpp! 197 enum IIT_Info { 198 // Common values should be encoded with 0-15. 199 IIT_Done = 0, 200 IIT_I1 = 1, 201 IIT_I8 = 2, 202 IIT_I16 = 3, 203 IIT_I32 = 4, 204 IIT_I64 = 5, 205 IIT_F16 = 6, 206 IIT_F32 = 7, 207 IIT_F64 = 8, 208 IIT_V2 = 9, 209 IIT_V4 = 10, 210 IIT_V8 = 11, 211 IIT_V16 = 12, 212 IIT_V32 = 13, 213 IIT_PTR = 14, 214 IIT_ARG = 15, 215 216 // Values from 16+ are only encodable with the inefficient encoding. 217 IIT_V64 = 16, 218 IIT_MMX = 17, 219 IIT_TOKEN = 18, 220 IIT_METADATA = 19, 221 IIT_EMPTYSTRUCT = 20, 222 IIT_STRUCT2 = 21, 223 IIT_STRUCT3 = 22, 224 IIT_STRUCT4 = 23, 225 IIT_STRUCT5 = 24, 226 IIT_EXTEND_ARG = 25, 227 IIT_TRUNC_ARG = 26, 228 IIT_ANYPTR = 27, 229 IIT_V1 = 28, 230 IIT_VARARG = 29, 231 IIT_HALF_VEC_ARG = 30, 232 IIT_SAME_VEC_WIDTH_ARG = 31, 233 IIT_PTR_TO_ARG = 32, 234 IIT_PTR_TO_ELT = 33, 235 IIT_VEC_OF_ANYPTRS_TO_ELT = 34, 236 IIT_I128 = 35, 237 IIT_V512 = 36, 238 IIT_V1024 = 37, 239 IIT_STRUCT6 = 38, 240 IIT_STRUCT7 = 39, 241 IIT_STRUCT8 = 40, 242 IIT_F128 = 41, 243 IIT_VEC_ELEMENT = 42, 244 IIT_SCALABLE_VEC = 43, 245 IIT_SUBDIVIDE2_ARG = 44, 246 IIT_SUBDIVIDE4_ARG = 45, 247 IIT_VEC_OF_BITCASTS_TO_INT = 46, 248 IIT_V128 = 47, 249 IIT_BF16 = 48, 250 IIT_STRUCT9 = 49, 251 IIT_V256 = 50, 252 IIT_AMX = 51, 253 IIT_PPCF128 = 52, 254 IIT_V3 = 53, 255 }; 256 257 static void EncodeFixedValueType(MVT::SimpleValueType VT, 258 std::vector<unsigned char> &Sig) { 259 if (MVT(VT).isInteger()) { 260 unsigned BitWidth = MVT(VT).getFixedSizeInBits(); 261 switch (BitWidth) { 262 default: PrintFatalError("unhandled integer type width in intrinsic!"); 263 case 1: return Sig.push_back(IIT_I1); 264 case 8: return Sig.push_back(IIT_I8); 265 case 16: return Sig.push_back(IIT_I16); 266 case 32: return Sig.push_back(IIT_I32); 267 case 64: return Sig.push_back(IIT_I64); 268 case 128: return Sig.push_back(IIT_I128); 269 } 270 } 271 272 switch (VT) { 273 default: PrintFatalError("unhandled MVT in intrinsic!"); 274 case MVT::f16: return Sig.push_back(IIT_F16); 275 case MVT::bf16: return Sig.push_back(IIT_BF16); 276 case MVT::f32: return Sig.push_back(IIT_F32); 277 case MVT::f64: return Sig.push_back(IIT_F64); 278 case MVT::f128: return Sig.push_back(IIT_F128); 279 case MVT::ppcf128: return Sig.push_back(IIT_PPCF128); 280 case MVT::token: return Sig.push_back(IIT_TOKEN); 281 case MVT::Metadata: return Sig.push_back(IIT_METADATA); 282 case MVT::x86mmx: return Sig.push_back(IIT_MMX); 283 case MVT::x86amx: return Sig.push_back(IIT_AMX); 284 // MVT::OtherVT is used to mean the empty struct type here. 285 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT); 286 // MVT::isVoid is used to represent varargs here. 287 case MVT::isVoid: return Sig.push_back(IIT_VARARG); 288 } 289 } 290 291 #if defined(_MSC_VER) && !defined(__clang__) 292 #pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function. 293 #endif 294 295 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes, 296 unsigned &NextArgCode, 297 std::vector<unsigned char> &Sig, 298 ArrayRef<unsigned char> Mapping) { 299 300 if (R->isSubClassOf("LLVMMatchType")) { 301 unsigned Number = Mapping[R->getValueAsInt("Number")]; 302 assert(Number < ArgCodes.size() && "Invalid matching number!"); 303 if (R->isSubClassOf("LLVMExtendedType")) 304 Sig.push_back(IIT_EXTEND_ARG); 305 else if (R->isSubClassOf("LLVMTruncatedType")) 306 Sig.push_back(IIT_TRUNC_ARG); 307 else if (R->isSubClassOf("LLVMHalfElementsVectorType")) 308 Sig.push_back(IIT_HALF_VEC_ARG); 309 else if (R->isSubClassOf("LLVMScalarOrSameVectorWidth")) { 310 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG); 311 Sig.push_back((Number << 3) | ArgCodes[Number]); 312 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy")); 313 EncodeFixedValueType(VT, Sig); 314 return; 315 } 316 else if (R->isSubClassOf("LLVMPointerTo")) 317 Sig.push_back(IIT_PTR_TO_ARG); 318 else if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) { 319 Sig.push_back(IIT_VEC_OF_ANYPTRS_TO_ELT); 320 // Encode overloaded ArgNo 321 Sig.push_back(NextArgCode++); 322 // Encode LLVMMatchType<Number> ArgNo 323 Sig.push_back(Number); 324 return; 325 } else if (R->isSubClassOf("LLVMPointerToElt")) 326 Sig.push_back(IIT_PTR_TO_ELT); 327 else if (R->isSubClassOf("LLVMVectorElementType")) 328 Sig.push_back(IIT_VEC_ELEMENT); 329 else if (R->isSubClassOf("LLVMSubdivide2VectorType")) 330 Sig.push_back(IIT_SUBDIVIDE2_ARG); 331 else if (R->isSubClassOf("LLVMSubdivide4VectorType")) 332 Sig.push_back(IIT_SUBDIVIDE4_ARG); 333 else if (R->isSubClassOf("LLVMVectorOfBitcastsToInt")) 334 Sig.push_back(IIT_VEC_OF_BITCASTS_TO_INT); 335 else 336 Sig.push_back(IIT_ARG); 337 return Sig.push_back((Number << 3) | 7 /*IITDescriptor::AK_MatchType*/); 338 } 339 340 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT")); 341 342 unsigned Tmp = 0; 343 switch (VT) { 344 default: break; 345 case MVT::iPTRAny: ++Tmp; LLVM_FALLTHROUGH; 346 case MVT::vAny: ++Tmp; LLVM_FALLTHROUGH; 347 case MVT::fAny: ++Tmp; LLVM_FALLTHROUGH; 348 case MVT::iAny: ++Tmp; LLVM_FALLTHROUGH; 349 case MVT::Any: { 350 // If this is an "any" valuetype, then the type is the type of the next 351 // type in the list specified to getIntrinsic(). 352 Sig.push_back(IIT_ARG); 353 354 // Figure out what arg # this is consuming, and remember what kind it was. 355 assert(NextArgCode < ArgCodes.size() && ArgCodes[NextArgCode] == Tmp && 356 "Invalid or no ArgCode associated with overloaded VT!"); 357 unsigned ArgNo = NextArgCode++; 358 359 // Encode what sort of argument it must be in the low 3 bits of the ArgNo. 360 return Sig.push_back((ArgNo << 3) | Tmp); 361 } 362 363 case MVT::iPTR: { 364 unsigned AddrSpace = 0; 365 if (R->isSubClassOf("LLVMQualPointerType")) { 366 AddrSpace = R->getValueAsInt("AddrSpace"); 367 assert(AddrSpace < 256 && "Address space exceeds 255"); 368 } 369 if (AddrSpace) { 370 Sig.push_back(IIT_ANYPTR); 371 Sig.push_back(AddrSpace); 372 } else { 373 Sig.push_back(IIT_PTR); 374 } 375 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, NextArgCode, Sig, 376 Mapping); 377 } 378 } 379 380 if (MVT(VT).isVector()) { 381 MVT VVT = VT; 382 if (VVT.isScalableVector()) 383 Sig.push_back(IIT_SCALABLE_VEC); 384 switch (VVT.getVectorMinNumElements()) { 385 default: PrintFatalError("unhandled vector type width in intrinsic!"); 386 case 1: Sig.push_back(IIT_V1); break; 387 case 2: Sig.push_back(IIT_V2); break; 388 case 3: Sig.push_back(IIT_V3); break; 389 case 4: Sig.push_back(IIT_V4); break; 390 case 8: Sig.push_back(IIT_V8); break; 391 case 16: Sig.push_back(IIT_V16); break; 392 case 32: Sig.push_back(IIT_V32); break; 393 case 64: Sig.push_back(IIT_V64); break; 394 case 128: Sig.push_back(IIT_V128); break; 395 case 256: Sig.push_back(IIT_V256); break; 396 case 512: Sig.push_back(IIT_V512); break; 397 case 1024: Sig.push_back(IIT_V1024); break; 398 } 399 400 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig); 401 } 402 403 EncodeFixedValueType(VT, Sig); 404 } 405 406 static void UpdateArgCodes(Record *R, std::vector<unsigned char> &ArgCodes, 407 unsigned int &NumInserted, 408 SmallVectorImpl<unsigned char> &Mapping) { 409 if (R->isSubClassOf("LLVMMatchType")) { 410 if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) { 411 ArgCodes.push_back(3 /*vAny*/); 412 ++NumInserted; 413 } 414 return; 415 } 416 417 unsigned Tmp = 0; 418 switch (getValueType(R->getValueAsDef("VT"))) { 419 default: break; 420 case MVT::iPTR: 421 UpdateArgCodes(R->getValueAsDef("ElTy"), ArgCodes, NumInserted, Mapping); 422 break; 423 case MVT::iPTRAny: 424 ++Tmp; 425 LLVM_FALLTHROUGH; 426 case MVT::vAny: 427 ++Tmp; 428 LLVM_FALLTHROUGH; 429 case MVT::fAny: 430 ++Tmp; 431 LLVM_FALLTHROUGH; 432 case MVT::iAny: 433 ++Tmp; 434 LLVM_FALLTHROUGH; 435 case MVT::Any: 436 unsigned OriginalIdx = ArgCodes.size() - NumInserted; 437 assert(OriginalIdx >= Mapping.size()); 438 Mapping.resize(OriginalIdx+1); 439 Mapping[OriginalIdx] = ArgCodes.size(); 440 ArgCodes.push_back(Tmp); 441 break; 442 } 443 } 444 445 #if defined(_MSC_VER) && !defined(__clang__) 446 #pragma optimize("",on) 447 #endif 448 449 /// ComputeFixedEncoding - If we can encode the type signature for this 450 /// intrinsic into 32 bits, return it. If not, return ~0U. 451 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int, 452 std::vector<unsigned char> &TypeSig) { 453 std::vector<unsigned char> ArgCodes; 454 455 // Add codes for any overloaded result VTs. 456 unsigned int NumInserted = 0; 457 SmallVector<unsigned char, 8> ArgMapping; 458 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i) 459 UpdateArgCodes(Int.IS.RetTypeDefs[i], ArgCodes, NumInserted, ArgMapping); 460 461 // Add codes for any overloaded operand VTs. 462 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i) 463 UpdateArgCodes(Int.IS.ParamTypeDefs[i], ArgCodes, NumInserted, ArgMapping); 464 465 unsigned NextArgCode = 0; 466 if (Int.IS.RetVTs.empty()) 467 TypeSig.push_back(IIT_Done); 468 else if (Int.IS.RetVTs.size() == 1 && 469 Int.IS.RetVTs[0] == MVT::isVoid) 470 TypeSig.push_back(IIT_Done); 471 else { 472 switch (Int.IS.RetVTs.size()) { 473 case 1: break; 474 case 2: TypeSig.push_back(IIT_STRUCT2); break; 475 case 3: TypeSig.push_back(IIT_STRUCT3); break; 476 case 4: TypeSig.push_back(IIT_STRUCT4); break; 477 case 5: TypeSig.push_back(IIT_STRUCT5); break; 478 case 6: TypeSig.push_back(IIT_STRUCT6); break; 479 case 7: TypeSig.push_back(IIT_STRUCT7); break; 480 case 8: TypeSig.push_back(IIT_STRUCT8); break; 481 case 9: TypeSig.push_back(IIT_STRUCT9); break; 482 default: llvm_unreachable("Unhandled case in struct"); 483 } 484 485 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i) 486 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, NextArgCode, TypeSig, 487 ArgMapping); 488 } 489 490 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i) 491 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, NextArgCode, TypeSig, 492 ArgMapping); 493 } 494 495 static void printIITEntry(raw_ostream &OS, unsigned char X) { 496 OS << (unsigned)X; 497 } 498 499 void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints, 500 raw_ostream &OS) { 501 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and 502 // capture it in this vector, otherwise store a ~0U. 503 std::vector<unsigned> FixedEncodings; 504 505 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable; 506 507 std::vector<unsigned char> TypeSig; 508 509 // Compute the unique argument type info. 510 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 511 // Get the signature for the intrinsic. 512 TypeSig.clear(); 513 ComputeFixedEncoding(Ints[i], TypeSig); 514 515 // Check to see if we can encode it into a 32-bit word. We can only encode 516 // 8 nibbles into a 32-bit word. 517 if (TypeSig.size() <= 8) { 518 bool Failed = false; 519 unsigned Result = 0; 520 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) { 521 // If we had an unencodable argument, bail out. 522 if (TypeSig[i] > 15) { 523 Failed = true; 524 break; 525 } 526 Result = (Result << 4) | TypeSig[e-i-1]; 527 } 528 529 // If this could be encoded into a 31-bit word, return it. 530 if (!Failed && (Result >> 31) == 0) { 531 FixedEncodings.push_back(Result); 532 continue; 533 } 534 } 535 536 // Otherwise, we're going to unique the sequence into the 537 // LongEncodingTable, and use its offset in the 32-bit table instead. 538 LongEncodingTable.add(TypeSig); 539 540 // This is a placehold that we'll replace after the table is laid out. 541 FixedEncodings.push_back(~0U); 542 } 543 544 LongEncodingTable.layout(); 545 546 OS << "// Global intrinsic function declaration type table.\n"; 547 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n"; 548 549 OS << "static const unsigned IIT_Table[] = {\n "; 550 551 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) { 552 if ((i & 7) == 7) 553 OS << "\n "; 554 555 // If the entry fit in the table, just emit it. 556 if (FixedEncodings[i] != ~0U) { 557 OS << "0x" << Twine::utohexstr(FixedEncodings[i]) << ", "; 558 continue; 559 } 560 561 TypeSig.clear(); 562 ComputeFixedEncoding(Ints[i], TypeSig); 563 564 565 // Otherwise, emit the offset into the long encoding table. We emit it this 566 // way so that it is easier to read the offset in the .def file. 567 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", "; 568 } 569 570 OS << "0\n};\n\n"; 571 572 // Emit the shared table of register lists. 573 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n"; 574 if (!LongEncodingTable.empty()) 575 LongEncodingTable.emit(OS, printIITEntry); 576 OS << " 255\n};\n\n"; 577 578 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL 579 } 580 581 namespace { 582 struct AttributeComparator { 583 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const { 584 // Sort throwing intrinsics after non-throwing intrinsics. 585 if (L->canThrow != R->canThrow) 586 return R->canThrow; 587 588 if (L->isNoDuplicate != R->isNoDuplicate) 589 return R->isNoDuplicate; 590 591 if (L->isNoMerge != R->isNoMerge) 592 return R->isNoMerge; 593 594 if (L->isNoReturn != R->isNoReturn) 595 return R->isNoReturn; 596 597 if (L->isNoSync != R->isNoSync) 598 return R->isNoSync; 599 600 if (L->isNoFree != R->isNoFree) 601 return R->isNoFree; 602 603 if (L->isWillReturn != R->isWillReturn) 604 return R->isWillReturn; 605 606 if (L->isCold != R->isCold) 607 return R->isCold; 608 609 if (L->isConvergent != R->isConvergent) 610 return R->isConvergent; 611 612 if (L->isSpeculatable != R->isSpeculatable) 613 return R->isSpeculatable; 614 615 if (L->hasSideEffects != R->hasSideEffects) 616 return R->hasSideEffects; 617 618 // Try to order by readonly/readnone attribute. 619 CodeGenIntrinsic::ModRefBehavior LK = L->ModRef; 620 CodeGenIntrinsic::ModRefBehavior RK = R->ModRef; 621 if (LK != RK) return (LK > RK); 622 // Order by argument attributes. 623 // This is reliable because each side is already sorted internally. 624 return (L->ArgumentAttributes < R->ArgumentAttributes); 625 } 626 }; 627 } // End anonymous namespace 628 629 /// EmitAttributes - This emits the Intrinsic::getAttributes method. 630 void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints, 631 raw_ostream &OS) { 632 OS << "// Add parameter attributes that are not common to all intrinsics.\n"; 633 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n"; 634 OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n"; 635 636 // Compute the maximum number of attribute arguments and the map 637 typedef std::map<const CodeGenIntrinsic*, unsigned, 638 AttributeComparator> UniqAttrMapTy; 639 UniqAttrMapTy UniqAttributes; 640 unsigned maxArgAttrs = 0; 641 unsigned AttrNum = 0; 642 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 643 const CodeGenIntrinsic &intrinsic = Ints[i]; 644 maxArgAttrs = 645 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size())); 646 unsigned &N = UniqAttributes[&intrinsic]; 647 if (N) continue; 648 N = ++AttrNum; 649 assert(N < 65536 && "Too many unique attributes for table!"); 650 } 651 652 // Emit an array of AttributeList. Most intrinsics will have at least one 653 // entry, for the function itself (index ~1), which is usually nounwind. 654 OS << " static const uint16_t IntrinsicsToAttributesMap[] = {\n"; 655 656 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 657 const CodeGenIntrinsic &intrinsic = Ints[i]; 658 659 OS << " " << UniqAttributes[&intrinsic] << ", // " 660 << intrinsic.Name << "\n"; 661 } 662 OS << " };\n\n"; 663 664 OS << " AttributeList AS[" << maxArgAttrs + 1 << "];\n"; 665 OS << " unsigned NumAttrs = 0;\n"; 666 OS << " if (id != 0) {\n"; 667 OS << " switch(IntrinsicsToAttributesMap[id - 1]) {\n"; 668 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n"; 669 for (auto UniqAttribute : UniqAttributes) { 670 OS << " case " << UniqAttribute.second << ": {\n"; 671 672 const CodeGenIntrinsic &Intrinsic = *(UniqAttribute.first); 673 674 // Keep track of the number of attributes we're writing out. 675 unsigned numAttrs = 0; 676 677 // The argument attributes are alreadys sorted by argument index. 678 unsigned Ai = 0, Ae = Intrinsic.ArgumentAttributes.size(); 679 if (Ae) { 680 while (Ai != Ae) { 681 unsigned AttrIdx = Intrinsic.ArgumentAttributes[Ai].Index; 682 683 OS << " const Attribute::AttrKind AttrParam" << AttrIdx << "[]= {"; 684 ListSeparator LS(","); 685 686 bool AllValuesAreZero = true; 687 SmallVector<uint64_t, 8> Values; 688 do { 689 switch (Intrinsic.ArgumentAttributes[Ai].Kind) { 690 case CodeGenIntrinsic::NoCapture: 691 OS << LS << "Attribute::NoCapture"; 692 break; 693 case CodeGenIntrinsic::NoAlias: 694 OS << LS << "Attribute::NoAlias"; 695 break; 696 case CodeGenIntrinsic::NoUndef: 697 OS << LS << "Attribute::NoUndef"; 698 break; 699 case CodeGenIntrinsic::Returned: 700 OS << LS << "Attribute::Returned"; 701 break; 702 case CodeGenIntrinsic::ReadOnly: 703 OS << LS << "Attribute::ReadOnly"; 704 break; 705 case CodeGenIntrinsic::WriteOnly: 706 OS << LS << "Attribute::WriteOnly"; 707 break; 708 case CodeGenIntrinsic::ReadNone: 709 OS << LS << "Attribute::ReadNone"; 710 break; 711 case CodeGenIntrinsic::ImmArg: 712 OS << LS << "Attribute::ImmArg"; 713 break; 714 case CodeGenIntrinsic::Alignment: 715 OS << LS << "Attribute::Alignment"; 716 break; 717 } 718 uint64_t V = Intrinsic.ArgumentAttributes[Ai].Value; 719 Values.push_back(V); 720 AllValuesAreZero &= (V == 0); 721 722 ++Ai; 723 } while (Ai != Ae && Intrinsic.ArgumentAttributes[Ai].Index == AttrIdx); 724 OS << "};\n"; 725 726 // Generate attribute value array if not all attribute values are zero. 727 if (!AllValuesAreZero) { 728 OS << " const uint64_t AttrValParam" << AttrIdx << "[]= {"; 729 ListSeparator LSV(","); 730 for (const auto V : Values) 731 OS << LSV << V; 732 OS << "};\n"; 733 } 734 735 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, " 736 << AttrIdx << ", AttrParam" << AttrIdx; 737 if (!AllValuesAreZero) 738 OS << ", AttrValParam" << AttrIdx; 739 OS << ");\n"; 740 } 741 } 742 743 if (!Intrinsic.canThrow || 744 (Intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem && 745 !Intrinsic.hasSideEffects) || 746 Intrinsic.isNoReturn || Intrinsic.isNoSync || Intrinsic.isNoFree || 747 Intrinsic.isWillReturn || Intrinsic.isCold || Intrinsic.isNoDuplicate || 748 Intrinsic.isNoMerge || Intrinsic.isConvergent || 749 Intrinsic.isSpeculatable) { 750 OS << " const Attribute::AttrKind Atts[] = {"; 751 ListSeparator LS(","); 752 if (!Intrinsic.canThrow) 753 OS << LS << "Attribute::NoUnwind"; 754 if (Intrinsic.isNoReturn) 755 OS << LS << "Attribute::NoReturn"; 756 if (Intrinsic.isNoSync) 757 OS << LS << "Attribute::NoSync"; 758 if (Intrinsic.isNoFree) 759 OS << LS << "Attribute::NoFree"; 760 if (Intrinsic.isWillReturn) 761 OS << LS << "Attribute::WillReturn"; 762 if (Intrinsic.isCold) 763 OS << LS << "Attribute::Cold"; 764 if (Intrinsic.isNoDuplicate) 765 OS << LS << "Attribute::NoDuplicate"; 766 if (Intrinsic.isNoMerge) 767 OS << LS << "Attribute::NoMerge"; 768 if (Intrinsic.isConvergent) 769 OS << LS << "Attribute::Convergent"; 770 if (Intrinsic.isSpeculatable) 771 OS << LS << "Attribute::Speculatable"; 772 773 switch (Intrinsic.ModRef) { 774 case CodeGenIntrinsic::NoMem: 775 if (Intrinsic.hasSideEffects) 776 break; 777 OS << LS; 778 OS << "Attribute::ReadNone"; 779 break; 780 case CodeGenIntrinsic::ReadArgMem: 781 OS << LS; 782 OS << "Attribute::ReadOnly,"; 783 OS << "Attribute::ArgMemOnly"; 784 break; 785 case CodeGenIntrinsic::ReadMem: 786 OS << LS; 787 OS << "Attribute::ReadOnly"; 788 break; 789 case CodeGenIntrinsic::ReadInaccessibleMem: 790 OS << LS; 791 OS << "Attribute::ReadOnly,"; 792 OS << "Attribute::InaccessibleMemOnly"; 793 break; 794 case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem: 795 OS << LS; 796 OS << "Attribute::ReadOnly,"; 797 OS << "Attribute::InaccessibleMemOrArgMemOnly"; 798 break; 799 case CodeGenIntrinsic::WriteArgMem: 800 OS << LS; 801 OS << "Attribute::WriteOnly,"; 802 OS << "Attribute::ArgMemOnly"; 803 break; 804 case CodeGenIntrinsic::WriteMem: 805 OS << LS; 806 OS << "Attribute::WriteOnly"; 807 break; 808 case CodeGenIntrinsic::WriteInaccessibleMem: 809 OS << LS; 810 OS << "Attribute::WriteOnly,"; 811 OS << "Attribute::InaccessibleMemOnly"; 812 break; 813 case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem: 814 OS << LS; 815 OS << "Attribute::WriteOnly,"; 816 OS << "Attribute::InaccessibleMemOrArgMemOnly"; 817 break; 818 case CodeGenIntrinsic::ReadWriteArgMem: 819 OS << LS; 820 OS << "Attribute::ArgMemOnly"; 821 break; 822 case CodeGenIntrinsic::ReadWriteInaccessibleMem: 823 OS << LS; 824 OS << "Attribute::InaccessibleMemOnly"; 825 break; 826 case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem: 827 OS << LS; 828 OS << "Attribute::InaccessibleMemOrArgMemOnly"; 829 break; 830 case CodeGenIntrinsic::ReadWriteMem: 831 break; 832 } 833 OS << "};\n"; 834 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, " 835 << "AttributeList::FunctionIndex, Atts);\n"; 836 } 837 838 if (numAttrs) { 839 OS << " NumAttrs = " << numAttrs << ";\n"; 840 OS << " break;\n"; 841 OS << " }\n"; 842 } else { 843 OS << " return AttributeList();\n"; 844 OS << " }\n"; 845 } 846 } 847 848 OS << " }\n"; 849 OS << " }\n"; 850 OS << " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n"; 851 OS << "}\n"; 852 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n"; 853 } 854 855 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap( 856 const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) { 857 StringRef CompilerName = (IsGCC ? "GCC" : "MS"); 858 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy; 859 BIMTy BuiltinMap; 860 StringToOffsetTable Table; 861 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 862 const std::string &BuiltinName = 863 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName; 864 if (!BuiltinName.empty()) { 865 // Get the map for this target prefix. 866 std::map<std::string, std::string> &BIM = 867 BuiltinMap[Ints[i].TargetPrefix]; 868 869 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second) 870 PrintFatalError(Ints[i].TheDef->getLoc(), 871 "Intrinsic '" + Ints[i].TheDef->getName() + 872 "': duplicate " + CompilerName + " builtin name!"); 873 Table.GetOrAddStringOffset(BuiltinName); 874 } 875 } 876 877 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n"; 878 OS << "// This is used by the C front-end. The builtin name is passed\n"; 879 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n"; 880 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n"; 881 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n"; 882 883 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName 884 << "Builtin(const char " 885 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n"; 886 887 if (Table.Empty()) { 888 OS << " return Intrinsic::not_intrinsic;\n"; 889 OS << "}\n"; 890 OS << "#endif\n\n"; 891 return; 892 } 893 894 OS << " static const char BuiltinNames[] = {\n"; 895 Table.EmitCharArray(OS); 896 OS << " };\n\n"; 897 898 OS << " struct BuiltinEntry {\n"; 899 OS << " Intrinsic::ID IntrinID;\n"; 900 OS << " unsigned StrTabOffset;\n"; 901 OS << " const char *getName() const {\n"; 902 OS << " return &BuiltinNames[StrTabOffset];\n"; 903 OS << " }\n"; 904 OS << " bool operator<(StringRef RHS) const {\n"; 905 OS << " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n"; 906 OS << " }\n"; 907 OS << " };\n"; 908 909 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n"; 910 911 // Note: this could emit significantly better code if we cared. 912 for (auto &I : BuiltinMap) { 913 OS << " "; 914 if (!I.first.empty()) 915 OS << "if (TargetPrefix == \"" << I.first << "\") "; 916 else 917 OS << "/* Target Independent Builtins */ "; 918 OS << "{\n"; 919 920 // Emit the comparisons for this target prefix. 921 OS << " static const BuiltinEntry " << I.first << "Names[] = {\n"; 922 for (const auto &P : I.second) { 923 OS << " {Intrinsic::" << P.second << ", " 924 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n"; 925 } 926 OS << " };\n"; 927 OS << " auto I = std::lower_bound(std::begin(" << I.first << "Names),\n"; 928 OS << " std::end(" << I.first << "Names),\n"; 929 OS << " BuiltinNameStr);\n"; 930 OS << " if (I != std::end(" << I.first << "Names) &&\n"; 931 OS << " I->getName() == BuiltinNameStr)\n"; 932 OS << " return I->IntrinID;\n"; 933 OS << " }\n"; 934 } 935 OS << " return "; 936 OS << "Intrinsic::not_intrinsic;\n"; 937 OS << "}\n"; 938 OS << "#endif\n\n"; 939 } 940 941 void llvm::EmitIntrinsicEnums(RecordKeeper &RK, raw_ostream &OS) { 942 IntrinsicEmitter(RK).run(OS, /*Enums=*/true); 943 } 944 945 void llvm::EmitIntrinsicImpl(RecordKeeper &RK, raw_ostream &OS) { 946 IntrinsicEmitter(RK).run(OS, /*Enums=*/false); 947 } 948