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 }; 252 253 static void EncodeFixedValueType(MVT::SimpleValueType VT, 254 std::vector<unsigned char> &Sig) { 255 if (MVT(VT).isInteger()) { 256 unsigned BitWidth = MVT(VT).getSizeInBits(); 257 switch (BitWidth) { 258 default: PrintFatalError("unhandled integer type width in intrinsic!"); 259 case 1: return Sig.push_back(IIT_I1); 260 case 8: return Sig.push_back(IIT_I8); 261 case 16: return Sig.push_back(IIT_I16); 262 case 32: return Sig.push_back(IIT_I32); 263 case 64: return Sig.push_back(IIT_I64); 264 case 128: return Sig.push_back(IIT_I128); 265 } 266 } 267 268 switch (VT) { 269 default: PrintFatalError("unhandled MVT in intrinsic!"); 270 case MVT::f16: return Sig.push_back(IIT_F16); 271 case MVT::bf16: return Sig.push_back(IIT_BF16); 272 case MVT::f32: return Sig.push_back(IIT_F32); 273 case MVT::f64: return Sig.push_back(IIT_F64); 274 case MVT::f128: return Sig.push_back(IIT_F128); 275 case MVT::token: return Sig.push_back(IIT_TOKEN); 276 case MVT::Metadata: return Sig.push_back(IIT_METADATA); 277 case MVT::x86mmx: return Sig.push_back(IIT_MMX); 278 // MVT::OtherVT is used to mean the empty struct type here. 279 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT); 280 // MVT::isVoid is used to represent varargs here. 281 case MVT::isVoid: return Sig.push_back(IIT_VARARG); 282 } 283 } 284 285 #if defined(_MSC_VER) && !defined(__clang__) 286 #pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function. 287 #endif 288 289 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes, 290 unsigned &NextArgCode, 291 std::vector<unsigned char> &Sig, 292 ArrayRef<unsigned char> Mapping) { 293 294 if (R->isSubClassOf("LLVMMatchType")) { 295 unsigned Number = Mapping[R->getValueAsInt("Number")]; 296 assert(Number < ArgCodes.size() && "Invalid matching number!"); 297 if (R->isSubClassOf("LLVMExtendedType")) 298 Sig.push_back(IIT_EXTEND_ARG); 299 else if (R->isSubClassOf("LLVMTruncatedType")) 300 Sig.push_back(IIT_TRUNC_ARG); 301 else if (R->isSubClassOf("LLVMHalfElementsVectorType")) 302 Sig.push_back(IIT_HALF_VEC_ARG); 303 else if (R->isSubClassOf("LLVMScalarOrSameVectorWidth")) { 304 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG); 305 Sig.push_back((Number << 3) | ArgCodes[Number]); 306 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy")); 307 EncodeFixedValueType(VT, Sig); 308 return; 309 } 310 else if (R->isSubClassOf("LLVMPointerTo")) 311 Sig.push_back(IIT_PTR_TO_ARG); 312 else if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) { 313 Sig.push_back(IIT_VEC_OF_ANYPTRS_TO_ELT); 314 // Encode overloaded ArgNo 315 Sig.push_back(NextArgCode++); 316 // Encode LLVMMatchType<Number> ArgNo 317 Sig.push_back(Number); 318 return; 319 } else if (R->isSubClassOf("LLVMPointerToElt")) 320 Sig.push_back(IIT_PTR_TO_ELT); 321 else if (R->isSubClassOf("LLVMVectorElementType")) 322 Sig.push_back(IIT_VEC_ELEMENT); 323 else if (R->isSubClassOf("LLVMSubdivide2VectorType")) 324 Sig.push_back(IIT_SUBDIVIDE2_ARG); 325 else if (R->isSubClassOf("LLVMSubdivide4VectorType")) 326 Sig.push_back(IIT_SUBDIVIDE4_ARG); 327 else if (R->isSubClassOf("LLVMVectorOfBitcastsToInt")) 328 Sig.push_back(IIT_VEC_OF_BITCASTS_TO_INT); 329 else 330 Sig.push_back(IIT_ARG); 331 return Sig.push_back((Number << 3) | 7 /*IITDescriptor::AK_MatchType*/); 332 } 333 334 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT")); 335 336 unsigned Tmp = 0; 337 switch (VT) { 338 default: break; 339 case MVT::iPTRAny: ++Tmp; LLVM_FALLTHROUGH; 340 case MVT::vAny: ++Tmp; LLVM_FALLTHROUGH; 341 case MVT::fAny: ++Tmp; LLVM_FALLTHROUGH; 342 case MVT::iAny: ++Tmp; LLVM_FALLTHROUGH; 343 case MVT::Any: { 344 // If this is an "any" valuetype, then the type is the type of the next 345 // type in the list specified to getIntrinsic(). 346 Sig.push_back(IIT_ARG); 347 348 // Figure out what arg # this is consuming, and remember what kind it was. 349 assert(NextArgCode < ArgCodes.size() && ArgCodes[NextArgCode] == Tmp && 350 "Invalid or no ArgCode associated with overloaded VT!"); 351 unsigned ArgNo = NextArgCode++; 352 353 // Encode what sort of argument it must be in the low 3 bits of the ArgNo. 354 return Sig.push_back((ArgNo << 3) | Tmp); 355 } 356 357 case MVT::iPTR: { 358 unsigned AddrSpace = 0; 359 if (R->isSubClassOf("LLVMQualPointerType")) { 360 AddrSpace = R->getValueAsInt("AddrSpace"); 361 assert(AddrSpace < 256 && "Address space exceeds 255"); 362 } 363 if (AddrSpace) { 364 Sig.push_back(IIT_ANYPTR); 365 Sig.push_back(AddrSpace); 366 } else { 367 Sig.push_back(IIT_PTR); 368 } 369 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, NextArgCode, Sig, 370 Mapping); 371 } 372 } 373 374 if (MVT(VT).isVector()) { 375 MVT VVT = VT; 376 if (VVT.isScalableVector()) 377 Sig.push_back(IIT_SCALABLE_VEC); 378 switch (VVT.getVectorNumElements()) { 379 default: PrintFatalError("unhandled vector type width in intrinsic!"); 380 case 1: Sig.push_back(IIT_V1); break; 381 case 2: Sig.push_back(IIT_V2); break; 382 case 4: Sig.push_back(IIT_V4); break; 383 case 8: Sig.push_back(IIT_V8); break; 384 case 16: Sig.push_back(IIT_V16); break; 385 case 32: Sig.push_back(IIT_V32); break; 386 case 64: Sig.push_back(IIT_V64); break; 387 case 128: Sig.push_back(IIT_V128); break; 388 case 512: Sig.push_back(IIT_V512); break; 389 case 1024: Sig.push_back(IIT_V1024); break; 390 } 391 392 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig); 393 } 394 395 EncodeFixedValueType(VT, Sig); 396 } 397 398 static void UpdateArgCodes(Record *R, std::vector<unsigned char> &ArgCodes, 399 unsigned int &NumInserted, 400 SmallVectorImpl<unsigned char> &Mapping) { 401 if (R->isSubClassOf("LLVMMatchType")) { 402 if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) { 403 ArgCodes.push_back(3 /*vAny*/); 404 ++NumInserted; 405 } 406 return; 407 } 408 409 unsigned Tmp = 0; 410 switch (getValueType(R->getValueAsDef("VT"))) { 411 default: break; 412 case MVT::iPTR: 413 UpdateArgCodes(R->getValueAsDef("ElTy"), ArgCodes, NumInserted, Mapping); 414 break; 415 case MVT::iPTRAny: 416 ++Tmp; 417 LLVM_FALLTHROUGH; 418 case MVT::vAny: 419 ++Tmp; 420 LLVM_FALLTHROUGH; 421 case MVT::fAny: 422 ++Tmp; 423 LLVM_FALLTHROUGH; 424 case MVT::iAny: 425 ++Tmp; 426 LLVM_FALLTHROUGH; 427 case MVT::Any: 428 unsigned OriginalIdx = ArgCodes.size() - NumInserted; 429 assert(OriginalIdx >= Mapping.size()); 430 Mapping.resize(OriginalIdx+1); 431 Mapping[OriginalIdx] = ArgCodes.size(); 432 ArgCodes.push_back(Tmp); 433 break; 434 } 435 } 436 437 #if defined(_MSC_VER) && !defined(__clang__) 438 #pragma optimize("",on) 439 #endif 440 441 /// ComputeFixedEncoding - If we can encode the type signature for this 442 /// intrinsic into 32 bits, return it. If not, return ~0U. 443 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int, 444 std::vector<unsigned char> &TypeSig) { 445 std::vector<unsigned char> ArgCodes; 446 447 // Add codes for any overloaded result VTs. 448 unsigned int NumInserted = 0; 449 SmallVector<unsigned char, 8> ArgMapping; 450 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i) 451 UpdateArgCodes(Int.IS.RetTypeDefs[i], ArgCodes, NumInserted, ArgMapping); 452 453 // Add codes for any overloaded operand VTs. 454 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i) 455 UpdateArgCodes(Int.IS.ParamTypeDefs[i], ArgCodes, NumInserted, ArgMapping); 456 457 unsigned NextArgCode = 0; 458 if (Int.IS.RetVTs.empty()) 459 TypeSig.push_back(IIT_Done); 460 else if (Int.IS.RetVTs.size() == 1 && 461 Int.IS.RetVTs[0] == MVT::isVoid) 462 TypeSig.push_back(IIT_Done); 463 else { 464 switch (Int.IS.RetVTs.size()) { 465 case 1: break; 466 case 2: TypeSig.push_back(IIT_STRUCT2); break; 467 case 3: TypeSig.push_back(IIT_STRUCT3); break; 468 case 4: TypeSig.push_back(IIT_STRUCT4); break; 469 case 5: TypeSig.push_back(IIT_STRUCT5); break; 470 case 6: TypeSig.push_back(IIT_STRUCT6); break; 471 case 7: TypeSig.push_back(IIT_STRUCT7); break; 472 case 8: TypeSig.push_back(IIT_STRUCT8); break; 473 case 9: TypeSig.push_back(IIT_STRUCT9); break; 474 default: llvm_unreachable("Unhandled case in struct"); 475 } 476 477 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i) 478 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, NextArgCode, TypeSig, 479 ArgMapping); 480 } 481 482 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i) 483 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, NextArgCode, TypeSig, 484 ArgMapping); 485 } 486 487 static void printIITEntry(raw_ostream &OS, unsigned char X) { 488 OS << (unsigned)X; 489 } 490 491 void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints, 492 raw_ostream &OS) { 493 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and 494 // capture it in this vector, otherwise store a ~0U. 495 std::vector<unsigned> FixedEncodings; 496 497 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable; 498 499 std::vector<unsigned char> TypeSig; 500 501 // Compute the unique argument type info. 502 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 503 // Get the signature for the intrinsic. 504 TypeSig.clear(); 505 ComputeFixedEncoding(Ints[i], TypeSig); 506 507 // Check to see if we can encode it into a 32-bit word. We can only encode 508 // 8 nibbles into a 32-bit word. 509 if (TypeSig.size() <= 8) { 510 bool Failed = false; 511 unsigned Result = 0; 512 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) { 513 // If we had an unencodable argument, bail out. 514 if (TypeSig[i] > 15) { 515 Failed = true; 516 break; 517 } 518 Result = (Result << 4) | TypeSig[e-i-1]; 519 } 520 521 // If this could be encoded into a 31-bit word, return it. 522 if (!Failed && (Result >> 31) == 0) { 523 FixedEncodings.push_back(Result); 524 continue; 525 } 526 } 527 528 // Otherwise, we're going to unique the sequence into the 529 // LongEncodingTable, and use its offset in the 32-bit table instead. 530 LongEncodingTable.add(TypeSig); 531 532 // This is a placehold that we'll replace after the table is laid out. 533 FixedEncodings.push_back(~0U); 534 } 535 536 LongEncodingTable.layout(); 537 538 OS << "// Global intrinsic function declaration type table.\n"; 539 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n"; 540 541 OS << "static const unsigned IIT_Table[] = {\n "; 542 543 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) { 544 if ((i & 7) == 7) 545 OS << "\n "; 546 547 // If the entry fit in the table, just emit it. 548 if (FixedEncodings[i] != ~0U) { 549 OS << "0x" << Twine::utohexstr(FixedEncodings[i]) << ", "; 550 continue; 551 } 552 553 TypeSig.clear(); 554 ComputeFixedEncoding(Ints[i], TypeSig); 555 556 557 // Otherwise, emit the offset into the long encoding table. We emit it this 558 // way so that it is easier to read the offset in the .def file. 559 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", "; 560 } 561 562 OS << "0\n};\n\n"; 563 564 // Emit the shared table of register lists. 565 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n"; 566 if (!LongEncodingTable.empty()) 567 LongEncodingTable.emit(OS, printIITEntry); 568 OS << " 255\n};\n\n"; 569 570 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL 571 } 572 573 namespace { 574 struct AttributeComparator { 575 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const { 576 // Sort throwing intrinsics after non-throwing intrinsics. 577 if (L->canThrow != R->canThrow) 578 return R->canThrow; 579 580 if (L->isNoDuplicate != R->isNoDuplicate) 581 return R->isNoDuplicate; 582 583 if (L->isNoReturn != R->isNoReturn) 584 return R->isNoReturn; 585 586 if (L->isNoSync != R->isNoSync) 587 return R->isNoSync; 588 589 if (L->isNoFree != R->isNoFree) 590 return R->isNoFree; 591 592 if (L->isWillReturn != R->isWillReturn) 593 return R->isWillReturn; 594 595 if (L->isCold != R->isCold) 596 return R->isCold; 597 598 if (L->isConvergent != R->isConvergent) 599 return R->isConvergent; 600 601 if (L->isSpeculatable != R->isSpeculatable) 602 return R->isSpeculatable; 603 604 if (L->hasSideEffects != R->hasSideEffects) 605 return R->hasSideEffects; 606 607 // Try to order by readonly/readnone attribute. 608 CodeGenIntrinsic::ModRefBehavior LK = L->ModRef; 609 CodeGenIntrinsic::ModRefBehavior RK = R->ModRef; 610 if (LK != RK) return (LK > RK); 611 // Order by argument attributes. 612 // This is reliable because each side is already sorted internally. 613 return (L->ArgumentAttributes < R->ArgumentAttributes); 614 } 615 }; 616 } // End anonymous namespace 617 618 /// EmitAttributes - This emits the Intrinsic::getAttributes method. 619 void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints, 620 raw_ostream &OS) { 621 OS << "// Add parameter attributes that are not common to all intrinsics.\n"; 622 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n"; 623 OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n"; 624 625 // Compute the maximum number of attribute arguments and the map 626 typedef std::map<const CodeGenIntrinsic*, unsigned, 627 AttributeComparator> UniqAttrMapTy; 628 UniqAttrMapTy UniqAttributes; 629 unsigned maxArgAttrs = 0; 630 unsigned AttrNum = 0; 631 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 632 const CodeGenIntrinsic &intrinsic = Ints[i]; 633 maxArgAttrs = 634 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size())); 635 unsigned &N = UniqAttributes[&intrinsic]; 636 if (N) continue; 637 assert(AttrNum < 256 && "Too many unique attributes for table!"); 638 N = ++AttrNum; 639 } 640 641 // Emit an array of AttributeList. Most intrinsics will have at least one 642 // entry, for the function itself (index ~1), which is usually nounwind. 643 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n"; 644 645 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 646 const CodeGenIntrinsic &intrinsic = Ints[i]; 647 648 OS << " " << UniqAttributes[&intrinsic] << ", // " 649 << intrinsic.Name << "\n"; 650 } 651 OS << " };\n\n"; 652 653 OS << " AttributeList AS[" << maxArgAttrs + 1 << "];\n"; 654 OS << " unsigned NumAttrs = 0;\n"; 655 OS << " if (id != 0) {\n"; 656 OS << " switch(IntrinsicsToAttributesMap[id - 1]) {\n"; 657 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n"; 658 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(), 659 E = UniqAttributes.end(); I != E; ++I) { 660 OS << " case " << I->second << ": {\n"; 661 662 const CodeGenIntrinsic &intrinsic = *(I->first); 663 664 // Keep track of the number of attributes we're writing out. 665 unsigned numAttrs = 0; 666 667 // The argument attributes are alreadys sorted by argument index. 668 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); 669 if (ae) { 670 while (ai != ae) { 671 unsigned attrIdx = intrinsic.ArgumentAttributes[ai].Index; 672 673 OS << " const Attribute::AttrKind AttrParam" << attrIdx << "[]= {"; 674 bool addComma = false; 675 676 bool AllValuesAreZero = true; 677 SmallVector<uint64_t, 8> Values; 678 do { 679 switch (intrinsic.ArgumentAttributes[ai].Kind) { 680 case CodeGenIntrinsic::NoCapture: 681 if (addComma) 682 OS << ","; 683 OS << "Attribute::NoCapture"; 684 addComma = true; 685 break; 686 case CodeGenIntrinsic::NoAlias: 687 if (addComma) 688 OS << ","; 689 OS << "Attribute::NoAlias"; 690 addComma = true; 691 break; 692 case CodeGenIntrinsic::NoUndef: 693 if (addComma) 694 OS << ","; 695 OS << "Attribute::NoUndef"; 696 addComma = true; 697 break; 698 case CodeGenIntrinsic::Returned: 699 if (addComma) 700 OS << ","; 701 OS << "Attribute::Returned"; 702 addComma = true; 703 break; 704 case CodeGenIntrinsic::ReadOnly: 705 if (addComma) 706 OS << ","; 707 OS << "Attribute::ReadOnly"; 708 addComma = true; 709 break; 710 case CodeGenIntrinsic::WriteOnly: 711 if (addComma) 712 OS << ","; 713 OS << "Attribute::WriteOnly"; 714 addComma = true; 715 break; 716 case CodeGenIntrinsic::ReadNone: 717 if (addComma) 718 OS << ","; 719 OS << "Attribute::ReadNone"; 720 addComma = true; 721 break; 722 case CodeGenIntrinsic::ImmArg: 723 if (addComma) 724 OS << ','; 725 OS << "Attribute::ImmArg"; 726 addComma = true; 727 break; 728 case CodeGenIntrinsic::Alignment: 729 if (addComma) 730 OS << ','; 731 OS << "Attribute::Alignment"; 732 addComma = true; 733 break; 734 } 735 uint64_t V = intrinsic.ArgumentAttributes[ai].Value; 736 Values.push_back(V); 737 AllValuesAreZero &= (V == 0); 738 739 ++ai; 740 } while (ai != ae && intrinsic.ArgumentAttributes[ai].Index == attrIdx); 741 OS << "};\n"; 742 743 // Generate attribute value array if not all attribute values are zero. 744 if (!AllValuesAreZero) { 745 OS << " const uint64_t AttrValParam" << attrIdx << "[]= {"; 746 addComma = false; 747 for (const auto V : Values) { 748 if (addComma) 749 OS << ','; 750 OS << V; 751 addComma = true; 752 } 753 OS << "};\n"; 754 } 755 756 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, " 757 << attrIdx << ", AttrParam" << attrIdx; 758 if (!AllValuesAreZero) 759 OS << ", AttrValParam" << attrIdx; 760 OS << ");\n"; 761 } 762 } 763 764 if (!intrinsic.canThrow || 765 (intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem && 766 !intrinsic.hasSideEffects) || 767 intrinsic.isNoReturn || intrinsic.isNoSync || intrinsic.isNoFree || 768 intrinsic.isWillReturn || intrinsic.isCold || intrinsic.isNoDuplicate || 769 intrinsic.isConvergent || intrinsic.isSpeculatable) { 770 OS << " const Attribute::AttrKind Atts[] = {"; 771 bool addComma = false; 772 if (!intrinsic.canThrow) { 773 OS << "Attribute::NoUnwind"; 774 addComma = true; 775 } 776 if (intrinsic.isNoReturn) { 777 if (addComma) 778 OS << ","; 779 OS << "Attribute::NoReturn"; 780 addComma = true; 781 } 782 if (intrinsic.isNoSync) { 783 if (addComma) 784 OS << ","; 785 OS << "Attribute::NoSync"; 786 addComma = true; 787 } 788 if (intrinsic.isNoFree) { 789 if (addComma) 790 OS << ","; 791 OS << "Attribute::NoFree"; 792 addComma = true; 793 } 794 if (intrinsic.isWillReturn) { 795 if (addComma) 796 OS << ","; 797 OS << "Attribute::WillReturn"; 798 addComma = true; 799 } 800 if (intrinsic.isCold) { 801 if (addComma) 802 OS << ","; 803 OS << "Attribute::Cold"; 804 addComma = true; 805 } 806 if (intrinsic.isNoDuplicate) { 807 if (addComma) 808 OS << ","; 809 OS << "Attribute::NoDuplicate"; 810 addComma = true; 811 } 812 if (intrinsic.isConvergent) { 813 if (addComma) 814 OS << ","; 815 OS << "Attribute::Convergent"; 816 addComma = true; 817 } 818 if (intrinsic.isSpeculatable) { 819 if (addComma) 820 OS << ","; 821 OS << "Attribute::Speculatable"; 822 addComma = true; 823 } 824 825 switch (intrinsic.ModRef) { 826 case CodeGenIntrinsic::NoMem: 827 if (intrinsic.hasSideEffects) 828 break; 829 if (addComma) 830 OS << ","; 831 OS << "Attribute::ReadNone"; 832 break; 833 case CodeGenIntrinsic::ReadArgMem: 834 if (addComma) 835 OS << ","; 836 OS << "Attribute::ReadOnly,"; 837 OS << "Attribute::ArgMemOnly"; 838 break; 839 case CodeGenIntrinsic::ReadMem: 840 if (addComma) 841 OS << ","; 842 OS << "Attribute::ReadOnly"; 843 break; 844 case CodeGenIntrinsic::ReadInaccessibleMem: 845 if (addComma) 846 OS << ","; 847 OS << "Attribute::ReadOnly,"; 848 OS << "Attribute::InaccessibleMemOnly"; 849 break; 850 case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem: 851 if (addComma) 852 OS << ","; 853 OS << "Attribute::ReadOnly,"; 854 OS << "Attribute::InaccessibleMemOrArgMemOnly"; 855 break; 856 case CodeGenIntrinsic::WriteArgMem: 857 if (addComma) 858 OS << ","; 859 OS << "Attribute::WriteOnly,"; 860 OS << "Attribute::ArgMemOnly"; 861 break; 862 case CodeGenIntrinsic::WriteMem: 863 if (addComma) 864 OS << ","; 865 OS << "Attribute::WriteOnly"; 866 break; 867 case CodeGenIntrinsic::WriteInaccessibleMem: 868 if (addComma) 869 OS << ","; 870 OS << "Attribute::WriteOnly,"; 871 OS << "Attribute::InaccessibleMemOnly"; 872 break; 873 case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem: 874 if (addComma) 875 OS << ","; 876 OS << "Attribute::WriteOnly,"; 877 OS << "Attribute::InaccessibleMemOrArgMemOnly"; 878 break; 879 case CodeGenIntrinsic::ReadWriteArgMem: 880 if (addComma) 881 OS << ","; 882 OS << "Attribute::ArgMemOnly"; 883 break; 884 case CodeGenIntrinsic::ReadWriteInaccessibleMem: 885 if (addComma) 886 OS << ","; 887 OS << "Attribute::InaccessibleMemOnly"; 888 break; 889 case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem: 890 if (addComma) 891 OS << ","; 892 OS << "Attribute::InaccessibleMemOrArgMemOnly"; 893 break; 894 case CodeGenIntrinsic::ReadWriteMem: 895 break; 896 } 897 OS << "};\n"; 898 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, " 899 << "AttributeList::FunctionIndex, Atts);\n"; 900 } 901 902 if (numAttrs) { 903 OS << " NumAttrs = " << numAttrs << ";\n"; 904 OS << " break;\n"; 905 OS << " }\n"; 906 } else { 907 OS << " return AttributeList();\n"; 908 OS << " }\n"; 909 } 910 } 911 912 OS << " }\n"; 913 OS << " }\n"; 914 OS << " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n"; 915 OS << "}\n"; 916 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n"; 917 } 918 919 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap( 920 const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) { 921 StringRef CompilerName = (IsGCC ? "GCC" : "MS"); 922 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy; 923 BIMTy BuiltinMap; 924 StringToOffsetTable Table; 925 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 926 const std::string &BuiltinName = 927 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName; 928 if (!BuiltinName.empty()) { 929 // Get the map for this target prefix. 930 std::map<std::string, std::string> &BIM = 931 BuiltinMap[Ints[i].TargetPrefix]; 932 933 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second) 934 PrintFatalError(Ints[i].TheDef->getLoc(), 935 "Intrinsic '" + Ints[i].TheDef->getName() + 936 "': duplicate " + CompilerName + " builtin name!"); 937 Table.GetOrAddStringOffset(BuiltinName); 938 } 939 } 940 941 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n"; 942 OS << "// This is used by the C front-end. The builtin name is passed\n"; 943 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n"; 944 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n"; 945 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n"; 946 947 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName 948 << "Builtin(const char " 949 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n"; 950 951 if (Table.Empty()) { 952 OS << " return Intrinsic::not_intrinsic;\n"; 953 OS << "}\n"; 954 OS << "#endif\n\n"; 955 return; 956 } 957 958 OS << " static const char BuiltinNames[] = {\n"; 959 Table.EmitCharArray(OS); 960 OS << " };\n\n"; 961 962 OS << " struct BuiltinEntry {\n"; 963 OS << " Intrinsic::ID IntrinID;\n"; 964 OS << " unsigned StrTabOffset;\n"; 965 OS << " const char *getName() const {\n"; 966 OS << " return &BuiltinNames[StrTabOffset];\n"; 967 OS << " }\n"; 968 OS << " bool operator<(StringRef RHS) const {\n"; 969 OS << " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n"; 970 OS << " }\n"; 971 OS << " };\n"; 972 973 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n"; 974 975 // Note: this could emit significantly better code if we cared. 976 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){ 977 OS << " "; 978 if (!I->first.empty()) 979 OS << "if (TargetPrefix == \"" << I->first << "\") "; 980 else 981 OS << "/* Target Independent Builtins */ "; 982 OS << "{\n"; 983 984 // Emit the comparisons for this target prefix. 985 OS << " static const BuiltinEntry " << I->first << "Names[] = {\n"; 986 for (const auto &P : I->second) { 987 OS << " {Intrinsic::" << P.second << ", " 988 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n"; 989 } 990 OS << " };\n"; 991 OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n"; 992 OS << " std::end(" << I->first << "Names),\n"; 993 OS << " BuiltinNameStr);\n"; 994 OS << " if (I != std::end(" << I->first << "Names) &&\n"; 995 OS << " I->getName() == BuiltinNameStr)\n"; 996 OS << " return I->IntrinID;\n"; 997 OS << " }\n"; 998 } 999 OS << " return "; 1000 OS << "Intrinsic::not_intrinsic;\n"; 1001 OS << "}\n"; 1002 OS << "#endif\n\n"; 1003 } 1004 1005 void llvm::EmitIntrinsicEnums(RecordKeeper &RK, raw_ostream &OS) { 1006 IntrinsicEmitter(RK).run(OS, /*Enums=*/true); 1007 } 1008 1009 void llvm::EmitIntrinsicImpl(RecordKeeper &RK, raw_ostream &OS) { 1010 IntrinsicEmitter(RK).run(OS, /*Enums=*/false); 1011 } 1012