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