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