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