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