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