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 // Individual targets don't need GCC builtin name mappings. 88 if (!TargetOnly) { 89 // Emit code to translate GCC builtins into LLVM intrinsics. 90 EmitIntrinsicToBuiltinMap(Ints, true, OS); 91 92 // Emit code to translate MS builtins into LLVM intrinsics. 93 EmitIntrinsicToBuiltinMap(Ints, false, OS); 94 } 95 96 EmitSuffix(OS); 97 } 98 99 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) { 100 OS << "// VisualStudio defines setjmp as _setjmp\n" 101 "#if defined(_MSC_VER) && defined(setjmp) && \\\n" 102 " !defined(setjmp_undefined_for_msvc)\n" 103 "# pragma push_macro(\"setjmp\")\n" 104 "# undef setjmp\n" 105 "# define setjmp_undefined_for_msvc\n" 106 "#endif\n\n"; 107 } 108 109 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) { 110 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n" 111 "// let's return it to _setjmp state\n" 112 "# pragma pop_macro(\"setjmp\")\n" 113 "# undef setjmp_undefined_for_msvc\n" 114 "#endif\n\n"; 115 } 116 117 void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints, 118 raw_ostream &OS) { 119 OS << "// Enum values for Intrinsics.h\n"; 120 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n"; 121 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 122 OS << " " << Ints[i].EnumName; 123 OS << ((i != e-1) ? ", " : " "); 124 if (Ints[i].EnumName.size() < 40) 125 OS << std::string(40-Ints[i].EnumName.size(), ' '); 126 OS << " // " << Ints[i].Name << "\n"; 127 } 128 OS << "#endif\n\n"; 129 } 130 131 void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints, 132 raw_ostream &OS) { 133 OS << "// Target mapping\n"; 134 OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n"; 135 OS << "struct IntrinsicTargetInfo {\n" 136 << " StringRef Name;\n" 137 << " size_t Offset;\n" 138 << " size_t Count;\n" 139 << "};\n"; 140 OS << "static const IntrinsicTargetInfo TargetInfos[] = {\n"; 141 for (auto Target : Ints.Targets) 142 OS << " {\"" << Target.Name << "\", " << Target.Offset << ", " 143 << Target.Count << "},\n"; 144 OS << "};\n"; 145 OS << "#endif\n\n"; 146 } 147 148 void IntrinsicEmitter::EmitIntrinsicToNameTable( 149 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) { 150 OS << "// Intrinsic ID to name table\n"; 151 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n"; 152 OS << " // Note that entry #0 is the invalid intrinsic!\n"; 153 for (unsigned i = 0, e = Ints.size(); i != e; ++i) 154 OS << " \"" << Ints[i].Name << "\",\n"; 155 OS << "#endif\n\n"; 156 } 157 158 void IntrinsicEmitter::EmitIntrinsicToOverloadTable( 159 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) { 160 OS << "// Intrinsic ID to overload bitset\n"; 161 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n"; 162 OS << "static const uint8_t OTable[] = {\n"; 163 OS << " 0"; 164 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 165 // Add one to the index so we emit a null bit for the invalid #0 intrinsic. 166 if ((i+1)%8 == 0) 167 OS << ",\n 0"; 168 if (Ints[i].isOverloaded) 169 OS << " | (1<<" << (i+1)%8 << ')'; 170 } 171 OS << "\n};\n\n"; 172 // OTable contains a true bit at the position if the intrinsic is overloaded. 173 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n"; 174 OS << "#endif\n\n"; 175 } 176 177 178 // NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp! 179 enum IIT_Info { 180 // Common values should be encoded with 0-15. 181 IIT_Done = 0, 182 IIT_I1 = 1, 183 IIT_I8 = 2, 184 IIT_I16 = 3, 185 IIT_I32 = 4, 186 IIT_I64 = 5, 187 IIT_F16 = 6, 188 IIT_F32 = 7, 189 IIT_F64 = 8, 190 IIT_V2 = 9, 191 IIT_V4 = 10, 192 IIT_V8 = 11, 193 IIT_V16 = 12, 194 IIT_V32 = 13, 195 IIT_PTR = 14, 196 IIT_ARG = 15, 197 198 // Values from 16+ are only encodable with the inefficient encoding. 199 IIT_V64 = 16, 200 IIT_MMX = 17, 201 IIT_TOKEN = 18, 202 IIT_METADATA = 19, 203 IIT_EMPTYSTRUCT = 20, 204 IIT_STRUCT2 = 21, 205 IIT_STRUCT3 = 22, 206 IIT_STRUCT4 = 23, 207 IIT_STRUCT5 = 24, 208 IIT_EXTEND_ARG = 25, 209 IIT_TRUNC_ARG = 26, 210 IIT_ANYPTR = 27, 211 IIT_V1 = 28, 212 IIT_VARARG = 29, 213 IIT_HALF_VEC_ARG = 30, 214 IIT_SAME_VEC_WIDTH_ARG = 31, 215 IIT_PTR_TO_ARG = 32, 216 IIT_VEC_OF_PTRS_TO_ELT = 33, 217 IIT_I128 = 34, 218 IIT_V512 = 35, 219 IIT_V1024 = 36 220 }; 221 222 223 static void EncodeFixedValueType(MVT::SimpleValueType VT, 224 std::vector<unsigned char> &Sig) { 225 if (MVT(VT).isInteger()) { 226 unsigned BitWidth = MVT(VT).getSizeInBits(); 227 switch (BitWidth) { 228 default: PrintFatalError("unhandled integer type width in intrinsic!"); 229 case 1: return Sig.push_back(IIT_I1); 230 case 8: return Sig.push_back(IIT_I8); 231 case 16: return Sig.push_back(IIT_I16); 232 case 32: return Sig.push_back(IIT_I32); 233 case 64: return Sig.push_back(IIT_I64); 234 case 128: return Sig.push_back(IIT_I128); 235 } 236 } 237 238 switch (VT) { 239 default: PrintFatalError("unhandled MVT in intrinsic!"); 240 case MVT::f16: return Sig.push_back(IIT_F16); 241 case MVT::f32: return Sig.push_back(IIT_F32); 242 case MVT::f64: return Sig.push_back(IIT_F64); 243 case MVT::token: return Sig.push_back(IIT_TOKEN); 244 case MVT::Metadata: return Sig.push_back(IIT_METADATA); 245 case MVT::x86mmx: return Sig.push_back(IIT_MMX); 246 // MVT::OtherVT is used to mean the empty struct type here. 247 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT); 248 // MVT::isVoid is used to represent varargs here. 249 case MVT::isVoid: return Sig.push_back(IIT_VARARG); 250 } 251 } 252 253 #if defined(_MSC_VER) && !defined(__clang__) 254 #pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function. 255 #endif 256 257 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes, 258 std::vector<unsigned char> &Sig) { 259 260 if (R->isSubClassOf("LLVMMatchType")) { 261 unsigned Number = R->getValueAsInt("Number"); 262 assert(Number < ArgCodes.size() && "Invalid matching number!"); 263 if (R->isSubClassOf("LLVMExtendedType")) 264 Sig.push_back(IIT_EXTEND_ARG); 265 else if (R->isSubClassOf("LLVMTruncatedType")) 266 Sig.push_back(IIT_TRUNC_ARG); 267 else if (R->isSubClassOf("LLVMHalfElementsVectorType")) 268 Sig.push_back(IIT_HALF_VEC_ARG); 269 else if (R->isSubClassOf("LLVMVectorSameWidth")) { 270 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG); 271 Sig.push_back((Number << 3) | ArgCodes[Number]); 272 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy")); 273 EncodeFixedValueType(VT, Sig); 274 return; 275 } 276 else if (R->isSubClassOf("LLVMPointerTo")) 277 Sig.push_back(IIT_PTR_TO_ARG); 278 else if (R->isSubClassOf("LLVMVectorOfPointersToElt")) 279 Sig.push_back(IIT_VEC_OF_PTRS_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 AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix 498 << "Intrinsic::ID id) {\n"; 499 else 500 OS << "AttributeSet 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 AttributeSet. 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 << " AttributeSet 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++ << "] = AttributeSet::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::WriteArgMem: 647 if (addComma) 648 OS << ","; 649 OS << "Attribute::WriteOnly,"; 650 OS << "Attribute::ArgMemOnly"; 651 break; 652 case CodeGenIntrinsic::WriteMem: 653 if (addComma) 654 OS << ","; 655 OS << "Attribute::WriteOnly"; 656 break; 657 case CodeGenIntrinsic::ReadWriteArgMem: 658 if (addComma) 659 OS << ","; 660 OS << "Attribute::ArgMemOnly"; 661 break; 662 case CodeGenIntrinsic::ReadWriteMem: 663 break; 664 } 665 OS << "};\n"; 666 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, " 667 << "AttributeSet::FunctionIndex, Atts);\n"; 668 } 669 670 if (numAttrs) { 671 OS << " NumAttrs = " << numAttrs << ";\n"; 672 OS << " break;\n"; 673 OS << " }\n"; 674 } else { 675 OS << " return AttributeSet();\n"; 676 OS << " }\n"; 677 } 678 } 679 680 OS << " }\n"; 681 OS << " }\n"; 682 OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n"; 683 OS << "}\n"; 684 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n"; 685 } 686 687 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap( 688 const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) { 689 StringRef CompilerName = (IsGCC ? "GCC" : "MS"); 690 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy; 691 BIMTy BuiltinMap; 692 StringToOffsetTable Table; 693 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 694 const std::string &BuiltinName = 695 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName; 696 if (!BuiltinName.empty()) { 697 // Get the map for this target prefix. 698 std::map<std::string, std::string> &BIM = 699 BuiltinMap[Ints[i].TargetPrefix]; 700 701 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second) 702 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() + 703 "': duplicate " + CompilerName + " builtin name!"); 704 Table.GetOrAddStringOffset(BuiltinName); 705 } 706 } 707 708 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n"; 709 OS << "// This is used by the C front-end. The builtin name is passed\n"; 710 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n"; 711 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n"; 712 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n"; 713 714 if (TargetOnly) { 715 OS << "static " << TargetPrefix << "Intrinsic::ID " 716 << "getIntrinsicFor" << CompilerName << "Builtin(const char " 717 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n"; 718 } else { 719 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName 720 << "Builtin(const char " 721 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n"; 722 } 723 OS << " static const char BuiltinNames[] = {\n"; 724 Table.EmitCharArray(OS); 725 OS << " };\n\n"; 726 727 OS << " struct BuiltinEntry {\n"; 728 OS << " Intrinsic::ID IntrinID;\n"; 729 OS << " unsigned StrTabOffset;\n"; 730 OS << " const char *getName() const {\n"; 731 OS << " return &BuiltinNames[StrTabOffset];\n"; 732 OS << " }\n"; 733 OS << " bool operator<(StringRef RHS) const {\n"; 734 OS << " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n"; 735 OS << " }\n"; 736 OS << " };\n"; 737 738 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n"; 739 740 // Note: this could emit significantly better code if we cared. 741 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){ 742 OS << " "; 743 if (!I->first.empty()) 744 OS << "if (TargetPrefix == \"" << I->first << "\") "; 745 else 746 OS << "/* Target Independent Builtins */ "; 747 OS << "{\n"; 748 749 // Emit the comparisons for this target prefix. 750 OS << " static const BuiltinEntry " << I->first << "Names[] = {\n"; 751 for (const auto &P : I->second) { 752 OS << " {Intrinsic::" << P.second << ", " 753 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n"; 754 } 755 OS << " };\n"; 756 OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n"; 757 OS << " std::end(" << I->first << "Names),\n"; 758 OS << " BuiltinNameStr);\n"; 759 OS << " if (I != std::end(" << I->first << "Names) &&\n"; 760 OS << " I->getName() == BuiltinNameStr)\n"; 761 OS << " return I->IntrinID;\n"; 762 OS << " }\n"; 763 } 764 OS << " return "; 765 if (!TargetPrefix.empty()) 766 OS << "(" << TargetPrefix << "Intrinsic::ID)"; 767 OS << "Intrinsic::not_intrinsic;\n"; 768 OS << "}\n"; 769 OS << "#endif\n\n"; 770 } 771 772 void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) { 773 IntrinsicEmitter(RK, TargetOnly).run(OS); 774 } 775