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::iPTR: 376 UpdateArgCodes(R->getValueAsDef("ElTy"), ArgCodes, NumInserted, Mapping); 377 break; 378 case MVT::iPTRAny: 379 ++Tmp; 380 LLVM_FALLTHROUGH; 381 case MVT::vAny: 382 ++Tmp; 383 LLVM_FALLTHROUGH; 384 case MVT::fAny: 385 ++Tmp; 386 LLVM_FALLTHROUGH; 387 case MVT::iAny: 388 ++Tmp; 389 LLVM_FALLTHROUGH; 390 case MVT::Any: 391 unsigned OriginalIdx = ArgCodes.size() - NumInserted; 392 assert(OriginalIdx >= Mapping.size()); 393 Mapping.resize(OriginalIdx+1); 394 Mapping[OriginalIdx] = ArgCodes.size(); 395 ArgCodes.push_back(Tmp); 396 break; 397 } 398 } 399 400 #if defined(_MSC_VER) && !defined(__clang__) 401 #pragma optimize("",on) 402 #endif 403 404 /// ComputeFixedEncoding - If we can encode the type signature for this 405 /// intrinsic into 32 bits, return it. If not, return ~0U. 406 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int, 407 std::vector<unsigned char> &TypeSig) { 408 std::vector<unsigned char> ArgCodes; 409 410 // Add codes for any overloaded result VTs. 411 unsigned int NumInserted = 0; 412 SmallVector<unsigned char, 8> ArgMapping; 413 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i) 414 UpdateArgCodes(Int.IS.RetTypeDefs[i], ArgCodes, NumInserted, ArgMapping); 415 416 // Add codes for any overloaded operand VTs. 417 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i) 418 UpdateArgCodes(Int.IS.ParamTypeDefs[i], ArgCodes, NumInserted, ArgMapping); 419 420 unsigned NextArgCode = 0; 421 if (Int.IS.RetVTs.empty()) 422 TypeSig.push_back(IIT_Done); 423 else if (Int.IS.RetVTs.size() == 1 && 424 Int.IS.RetVTs[0] == MVT::isVoid) 425 TypeSig.push_back(IIT_Done); 426 else { 427 switch (Int.IS.RetVTs.size()) { 428 case 1: break; 429 case 2: TypeSig.push_back(IIT_STRUCT2); break; 430 case 3: TypeSig.push_back(IIT_STRUCT3); break; 431 case 4: TypeSig.push_back(IIT_STRUCT4); break; 432 case 5: TypeSig.push_back(IIT_STRUCT5); break; 433 case 6: TypeSig.push_back(IIT_STRUCT6); break; 434 case 7: TypeSig.push_back(IIT_STRUCT7); break; 435 case 8: TypeSig.push_back(IIT_STRUCT8); break; 436 default: llvm_unreachable("Unhandled case in struct"); 437 } 438 439 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i) 440 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, NextArgCode, TypeSig, 441 ArgMapping); 442 } 443 444 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i) 445 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, NextArgCode, TypeSig, 446 ArgMapping); 447 } 448 449 static void printIITEntry(raw_ostream &OS, unsigned char X) { 450 OS << (unsigned)X; 451 } 452 453 void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints, 454 raw_ostream &OS) { 455 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and 456 // capture it in this vector, otherwise store a ~0U. 457 std::vector<unsigned> FixedEncodings; 458 459 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable; 460 461 std::vector<unsigned char> TypeSig; 462 463 // Compute the unique argument type info. 464 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 465 // Get the signature for the intrinsic. 466 TypeSig.clear(); 467 ComputeFixedEncoding(Ints[i], TypeSig); 468 469 // Check to see if we can encode it into a 32-bit word. We can only encode 470 // 8 nibbles into a 32-bit word. 471 if (TypeSig.size() <= 8) { 472 bool Failed = false; 473 unsigned Result = 0; 474 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) { 475 // If we had an unencodable argument, bail out. 476 if (TypeSig[i] > 15) { 477 Failed = true; 478 break; 479 } 480 Result = (Result << 4) | TypeSig[e-i-1]; 481 } 482 483 // If this could be encoded into a 31-bit word, return it. 484 if (!Failed && (Result >> 31) == 0) { 485 FixedEncodings.push_back(Result); 486 continue; 487 } 488 } 489 490 // Otherwise, we're going to unique the sequence into the 491 // LongEncodingTable, and use its offset in the 32-bit table instead. 492 LongEncodingTable.add(TypeSig); 493 494 // This is a placehold that we'll replace after the table is laid out. 495 FixedEncodings.push_back(~0U); 496 } 497 498 LongEncodingTable.layout(); 499 500 OS << "// Global intrinsic function declaration type table.\n"; 501 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n"; 502 503 OS << "static const unsigned IIT_Table[] = {\n "; 504 505 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) { 506 if ((i & 7) == 7) 507 OS << "\n "; 508 509 // If the entry fit in the table, just emit it. 510 if (FixedEncodings[i] != ~0U) { 511 OS << "0x" << Twine::utohexstr(FixedEncodings[i]) << ", "; 512 continue; 513 } 514 515 TypeSig.clear(); 516 ComputeFixedEncoding(Ints[i], TypeSig); 517 518 519 // Otherwise, emit the offset into the long encoding table. We emit it this 520 // way so that it is easier to read the offset in the .def file. 521 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", "; 522 } 523 524 OS << "0\n};\n\n"; 525 526 // Emit the shared table of register lists. 527 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n"; 528 if (!LongEncodingTable.empty()) 529 LongEncodingTable.emit(OS, printIITEntry); 530 OS << " 255\n};\n\n"; 531 532 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL 533 } 534 535 namespace { 536 struct AttributeComparator { 537 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const { 538 // Sort throwing intrinsics after non-throwing intrinsics. 539 if (L->canThrow != R->canThrow) 540 return R->canThrow; 541 542 if (L->isNoDuplicate != R->isNoDuplicate) 543 return R->isNoDuplicate; 544 545 if (L->isNoReturn != R->isNoReturn) 546 return R->isNoReturn; 547 548 if (L->isWillReturn != R->isWillReturn) 549 return R->isWillReturn; 550 551 if (L->isCold != R->isCold) 552 return R->isCold; 553 554 if (L->isConvergent != R->isConvergent) 555 return R->isConvergent; 556 557 if (L->isSpeculatable != R->isSpeculatable) 558 return R->isSpeculatable; 559 560 if (L->hasSideEffects != R->hasSideEffects) 561 return R->hasSideEffects; 562 563 // Try to order by readonly/readnone attribute. 564 CodeGenIntrinsic::ModRefBehavior LK = L->ModRef; 565 CodeGenIntrinsic::ModRefBehavior RK = R->ModRef; 566 if (LK != RK) return (LK > RK); 567 // Order by argument attributes. 568 // This is reliable because each side is already sorted internally. 569 return (L->ArgumentAttributes < R->ArgumentAttributes); 570 } 571 }; 572 } // End anonymous namespace 573 574 /// EmitAttributes - This emits the Intrinsic::getAttributes method. 575 void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints, 576 raw_ostream &OS) { 577 OS << "// Add parameter attributes that are not common to all intrinsics.\n"; 578 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n"; 579 if (TargetOnly) 580 OS << "static AttributeList getAttributes(LLVMContext &C, " << TargetPrefix 581 << "Intrinsic::ID id) {\n"; 582 else 583 OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n"; 584 585 // Compute the maximum number of attribute arguments and the map 586 typedef std::map<const CodeGenIntrinsic*, unsigned, 587 AttributeComparator> UniqAttrMapTy; 588 UniqAttrMapTy UniqAttributes; 589 unsigned maxArgAttrs = 0; 590 unsigned AttrNum = 0; 591 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 592 const CodeGenIntrinsic &intrinsic = Ints[i]; 593 maxArgAttrs = 594 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size())); 595 unsigned &N = UniqAttributes[&intrinsic]; 596 if (N) continue; 597 assert(AttrNum < 256 && "Too many unique attributes for table!"); 598 N = ++AttrNum; 599 } 600 601 // Emit an array of AttributeList. Most intrinsics will have at least one 602 // entry, for the function itself (index ~1), which is usually nounwind. 603 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n"; 604 605 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 606 const CodeGenIntrinsic &intrinsic = Ints[i]; 607 608 OS << " " << UniqAttributes[&intrinsic] << ", // " 609 << intrinsic.Name << "\n"; 610 } 611 OS << " };\n\n"; 612 613 OS << " AttributeList AS[" << maxArgAttrs + 1 << "];\n"; 614 OS << " unsigned NumAttrs = 0;\n"; 615 OS << " if (id != 0) {\n"; 616 OS << " switch(IntrinsicsToAttributesMap[id - "; 617 if (TargetOnly) 618 OS << "Intrinsic::num_intrinsics"; 619 else 620 OS << "1"; 621 OS << "]) {\n"; 622 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n"; 623 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(), 624 E = UniqAttributes.end(); I != E; ++I) { 625 OS << " case " << I->second << ": {\n"; 626 627 const CodeGenIntrinsic &intrinsic = *(I->first); 628 629 // Keep track of the number of attributes we're writing out. 630 unsigned numAttrs = 0; 631 632 // The argument attributes are alreadys sorted by argument index. 633 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); 634 if (ae) { 635 while (ai != ae) { 636 unsigned argNo = intrinsic.ArgumentAttributes[ai].first; 637 unsigned attrIdx = argNo + 1; // Must match AttributeList::FirstArgIndex 638 639 OS << " const Attribute::AttrKind AttrParam" << attrIdx << "[]= {"; 640 bool addComma = false; 641 642 do { 643 switch (intrinsic.ArgumentAttributes[ai].second) { 644 case CodeGenIntrinsic::NoCapture: 645 if (addComma) 646 OS << ","; 647 OS << "Attribute::NoCapture"; 648 addComma = true; 649 break; 650 case CodeGenIntrinsic::Returned: 651 if (addComma) 652 OS << ","; 653 OS << "Attribute::Returned"; 654 addComma = true; 655 break; 656 case CodeGenIntrinsic::ReadOnly: 657 if (addComma) 658 OS << ","; 659 OS << "Attribute::ReadOnly"; 660 addComma = true; 661 break; 662 case CodeGenIntrinsic::WriteOnly: 663 if (addComma) 664 OS << ","; 665 OS << "Attribute::WriteOnly"; 666 addComma = true; 667 break; 668 case CodeGenIntrinsic::ReadNone: 669 if (addComma) 670 OS << ","; 671 OS << "Attribute::ReadNone"; 672 addComma = true; 673 break; 674 case CodeGenIntrinsic::ImmArg: 675 if (addComma) 676 OS << ','; 677 OS << "Attribute::ImmArg"; 678 addComma = true; 679 break; 680 } 681 682 ++ai; 683 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo); 684 OS << "};\n"; 685 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, " 686 << attrIdx << ", AttrParam" << attrIdx << ");\n"; 687 } 688 } 689 690 if (!intrinsic.canThrow || 691 (intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem && !intrinsic.hasSideEffects) || 692 intrinsic.isNoReturn || intrinsic.isWillReturn || intrinsic.isCold || 693 intrinsic.isNoDuplicate || intrinsic.isConvergent || 694 intrinsic.isSpeculatable) { 695 OS << " const Attribute::AttrKind Atts[] = {"; 696 bool addComma = false; 697 if (!intrinsic.canThrow) { 698 OS << "Attribute::NoUnwind"; 699 addComma = true; 700 } 701 if (intrinsic.isNoReturn) { 702 if (addComma) 703 OS << ","; 704 OS << "Attribute::NoReturn"; 705 addComma = true; 706 } 707 if (intrinsic.isWillReturn) { 708 if (addComma) 709 OS << ","; 710 OS << "Attribute::WillReturn"; 711 addComma = true; 712 } 713 if (intrinsic.isCold) { 714 if (addComma) 715 OS << ","; 716 OS << "Attribute::Cold"; 717 addComma = true; 718 } 719 if (intrinsic.isNoDuplicate) { 720 if (addComma) 721 OS << ","; 722 OS << "Attribute::NoDuplicate"; 723 addComma = true; 724 } 725 if (intrinsic.isConvergent) { 726 if (addComma) 727 OS << ","; 728 OS << "Attribute::Convergent"; 729 addComma = true; 730 } 731 if (intrinsic.isSpeculatable) { 732 if (addComma) 733 OS << ","; 734 OS << "Attribute::Speculatable"; 735 addComma = true; 736 } 737 738 switch (intrinsic.ModRef) { 739 case CodeGenIntrinsic::NoMem: 740 if (intrinsic.hasSideEffects) 741 break; 742 if (addComma) 743 OS << ","; 744 OS << "Attribute::ReadNone"; 745 break; 746 case CodeGenIntrinsic::ReadArgMem: 747 if (addComma) 748 OS << ","; 749 OS << "Attribute::ReadOnly,"; 750 OS << "Attribute::ArgMemOnly"; 751 break; 752 case CodeGenIntrinsic::ReadMem: 753 if (addComma) 754 OS << ","; 755 OS << "Attribute::ReadOnly"; 756 break; 757 case CodeGenIntrinsic::ReadInaccessibleMem: 758 if (addComma) 759 OS << ","; 760 OS << "Attribute::ReadOnly,"; 761 OS << "Attribute::InaccessibleMemOnly"; 762 break; 763 case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem: 764 if (addComma) 765 OS << ","; 766 OS << "Attribute::ReadOnly,"; 767 OS << "Attribute::InaccessibleMemOrArgMemOnly"; 768 break; 769 case CodeGenIntrinsic::WriteArgMem: 770 if (addComma) 771 OS << ","; 772 OS << "Attribute::WriteOnly,"; 773 OS << "Attribute::ArgMemOnly"; 774 break; 775 case CodeGenIntrinsic::WriteMem: 776 if (addComma) 777 OS << ","; 778 OS << "Attribute::WriteOnly"; 779 break; 780 case CodeGenIntrinsic::WriteInaccessibleMem: 781 if (addComma) 782 OS << ","; 783 OS << "Attribute::WriteOnly,"; 784 OS << "Attribute::InaccessibleMemOnly"; 785 break; 786 case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem: 787 if (addComma) 788 OS << ","; 789 OS << "Attribute::WriteOnly,"; 790 OS << "Attribute::InaccessibleMemOrArgMemOnly"; 791 break; 792 case CodeGenIntrinsic::ReadWriteArgMem: 793 if (addComma) 794 OS << ","; 795 OS << "Attribute::ArgMemOnly"; 796 break; 797 case CodeGenIntrinsic::ReadWriteInaccessibleMem: 798 if (addComma) 799 OS << ","; 800 OS << "Attribute::InaccessibleMemOnly"; 801 break; 802 case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem: 803 if (addComma) 804 OS << ","; 805 OS << "Attribute::InaccessibleMemOrArgMemOnly"; 806 break; 807 case CodeGenIntrinsic::ReadWriteMem: 808 break; 809 } 810 OS << "};\n"; 811 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, " 812 << "AttributeList::FunctionIndex, Atts);\n"; 813 } 814 815 if (numAttrs) { 816 OS << " NumAttrs = " << numAttrs << ";\n"; 817 OS << " break;\n"; 818 OS << " }\n"; 819 } else { 820 OS << " return AttributeList();\n"; 821 OS << " }\n"; 822 } 823 } 824 825 OS << " }\n"; 826 OS << " }\n"; 827 OS << " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n"; 828 OS << "}\n"; 829 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n"; 830 } 831 832 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap( 833 const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) { 834 StringRef CompilerName = (IsGCC ? "GCC" : "MS"); 835 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy; 836 BIMTy BuiltinMap; 837 StringToOffsetTable Table; 838 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 839 const std::string &BuiltinName = 840 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName; 841 if (!BuiltinName.empty()) { 842 // Get the map for this target prefix. 843 std::map<std::string, std::string> &BIM = 844 BuiltinMap[Ints[i].TargetPrefix]; 845 846 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second) 847 PrintFatalError(Ints[i].TheDef->getLoc(), 848 "Intrinsic '" + Ints[i].TheDef->getName() + 849 "': duplicate " + CompilerName + " builtin name!"); 850 Table.GetOrAddStringOffset(BuiltinName); 851 } 852 } 853 854 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n"; 855 OS << "// This is used by the C front-end. The builtin name is passed\n"; 856 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n"; 857 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n"; 858 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n"; 859 860 if (TargetOnly) { 861 OS << "static " << TargetPrefix << "Intrinsic::ID " 862 << "getIntrinsicFor" << CompilerName << "Builtin(const char " 863 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n"; 864 } else { 865 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName 866 << "Builtin(const char " 867 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n"; 868 } 869 870 if (Table.Empty()) { 871 OS << " return "; 872 if (!TargetPrefix.empty()) 873 OS << "(" << TargetPrefix << "Intrinsic::ID)"; 874 OS << "Intrinsic::not_intrinsic;\n"; 875 OS << "}\n"; 876 OS << "#endif\n\n"; 877 return; 878 } 879 880 OS << " static const char BuiltinNames[] = {\n"; 881 Table.EmitCharArray(OS); 882 OS << " };\n\n"; 883 884 OS << " struct BuiltinEntry {\n"; 885 OS << " Intrinsic::ID IntrinID;\n"; 886 OS << " unsigned StrTabOffset;\n"; 887 OS << " const char *getName() const {\n"; 888 OS << " return &BuiltinNames[StrTabOffset];\n"; 889 OS << " }\n"; 890 OS << " bool operator<(StringRef RHS) const {\n"; 891 OS << " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n"; 892 OS << " }\n"; 893 OS << " };\n"; 894 895 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n"; 896 897 // Note: this could emit significantly better code if we cared. 898 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){ 899 OS << " "; 900 if (!I->first.empty()) 901 OS << "if (TargetPrefix == \"" << I->first << "\") "; 902 else 903 OS << "/* Target Independent Builtins */ "; 904 OS << "{\n"; 905 906 // Emit the comparisons for this target prefix. 907 OS << " static const BuiltinEntry " << I->first << "Names[] = {\n"; 908 for (const auto &P : I->second) { 909 OS << " {Intrinsic::" << P.second << ", " 910 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n"; 911 } 912 OS << " };\n"; 913 OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n"; 914 OS << " std::end(" << I->first << "Names),\n"; 915 OS << " BuiltinNameStr);\n"; 916 OS << " if (I != std::end(" << I->first << "Names) &&\n"; 917 OS << " I->getName() == BuiltinNameStr)\n"; 918 OS << " return I->IntrinID;\n"; 919 OS << " }\n"; 920 } 921 OS << " return "; 922 if (!TargetPrefix.empty()) 923 OS << "(" << TargetPrefix << "Intrinsic::ID)"; 924 OS << "Intrinsic::not_intrinsic;\n"; 925 OS << "}\n"; 926 OS << "#endif\n\n"; 927 } 928 929 void llvm::EmitIntrinsicEnums(RecordKeeper &RK, raw_ostream &OS, 930 bool TargetOnly) { 931 IntrinsicEmitter(RK, TargetOnly).run(OS, /*Enums=*/true); 932 } 933 934 void llvm::EmitIntrinsicImpl(RecordKeeper &RK, raw_ostream &OS, 935 bool TargetOnly) { 936 IntrinsicEmitter(RK, TargetOnly).run(OS, /*Enums=*/false); 937 } 938