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