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