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