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