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 "CodeGenTarget.h" 15 #include "IntrinsicEmitter.h" 16 #include "StringMatcher.h" 17 #include "llvm/TableGen/Record.h" 18 #include "llvm/ADT/StringExtras.h" 19 #include <algorithm> 20 using namespace llvm; 21 22 //===----------------------------------------------------------------------===// 23 // IntrinsicEmitter Implementation 24 //===----------------------------------------------------------------------===// 25 26 void IntrinsicEmitter::run(raw_ostream &OS) { 27 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS); 28 29 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly); 30 31 if (TargetOnly && !Ints.empty()) 32 TargetPrefix = Ints[0].TargetPrefix; 33 34 EmitPrefix(OS); 35 36 // Emit the enum information. 37 EmitEnumInfo(Ints, OS); 38 39 // Emit the intrinsic ID -> name table. 40 EmitIntrinsicToNameTable(Ints, OS); 41 42 // Emit the intrinsic ID -> overload table. 43 EmitIntrinsicToOverloadTable(Ints, OS); 44 45 // Emit the function name recognizer. 46 EmitFnNameRecognizer(Ints, OS); 47 48 // Emit the intrinsic verifier. 49 EmitVerifier(Ints, OS); 50 51 // Emit the intrinsic declaration generator. 52 EmitGenerator(Ints, OS); 53 54 // Emit the intrinsic parameter attributes. 55 EmitAttributes(Ints, OS); 56 57 // Emit intrinsic alias analysis mod/ref behavior. 58 EmitModRefBehavior(Ints, OS); 59 60 // Emit a list of intrinsics with corresponding GCC builtins. 61 EmitGCCBuiltinList(Ints, OS); 62 63 // Emit code to translate GCC builtins into LLVM intrinsics. 64 EmitIntrinsicToGCCBuiltinMap(Ints, OS); 65 66 EmitSuffix(OS); 67 } 68 69 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) { 70 OS << "// VisualStudio defines setjmp as _setjmp\n" 71 "#if defined(_MSC_VER) && defined(setjmp) && \\\n" 72 " !defined(setjmp_undefined_for_msvc)\n" 73 "# pragma push_macro(\"setjmp\")\n" 74 "# undef setjmp\n" 75 "# define setjmp_undefined_for_msvc\n" 76 "#endif\n\n"; 77 } 78 79 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) { 80 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n" 81 "// let's return it to _setjmp state\n" 82 "# pragma pop_macro(\"setjmp\")\n" 83 "# undef setjmp_undefined_for_msvc\n" 84 "#endif\n\n"; 85 } 86 87 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints, 88 raw_ostream &OS) { 89 OS << "// Enum values for Intrinsics.h\n"; 90 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n"; 91 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 92 OS << " " << Ints[i].EnumName; 93 OS << ((i != e-1) ? ", " : " "); 94 OS << std::string(40-Ints[i].EnumName.size(), ' ') 95 << "// " << Ints[i].Name << "\n"; 96 } 97 OS << "#endif\n\n"; 98 } 99 100 void IntrinsicEmitter:: 101 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints, 102 raw_ostream &OS) { 103 // Build a 'first character of function name' -> intrinsic # mapping. 104 std::map<char, std::vector<unsigned> > IntMapping; 105 for (unsigned i = 0, e = Ints.size(); i != e; ++i) 106 IntMapping[Ints[i].Name[5]].push_back(i); 107 108 OS << "// Function name -> enum value recognizer code.\n"; 109 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n"; 110 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n"; 111 OS << " switch (Name[5]) { // Dispatch on first letter.\n"; 112 OS << " default: break;\n"; 113 // Emit the intrinsic matching stuff by first letter. 114 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(), 115 E = IntMapping.end(); I != E; ++I) { 116 OS << " case '" << I->first << "':\n"; 117 std::vector<unsigned> &IntList = I->second; 118 119 // Emit all the overloaded intrinsics first, build a table of the 120 // non-overloaded ones. 121 std::vector<StringMatcher::StringPair> MatchTable; 122 123 for (unsigned i = 0, e = IntList.size(); i != e; ++i) { 124 unsigned IntNo = IntList[i]; 125 std::string Result = "return " + TargetPrefix + "Intrinsic::" + 126 Ints[IntNo].EnumName + ";"; 127 128 if (!Ints[IntNo].isOverloaded) { 129 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result)); 130 continue; 131 } 132 133 // For overloaded intrinsics, only the prefix needs to match 134 std::string TheStr = Ints[IntNo].Name.substr(6); 135 TheStr += '.'; // Require "bswap." instead of bswap. 136 OS << " if (NameR.startswith(\"" << TheStr << "\")) " 137 << Result << '\n'; 138 } 139 140 // Emit the matcher logic for the fixed length strings. 141 StringMatcher("NameR", MatchTable, OS).Emit(1); 142 OS << " break; // end of '" << I->first << "' case.\n"; 143 } 144 145 OS << " }\n"; 146 OS << "#endif\n\n"; 147 } 148 149 void IntrinsicEmitter:: 150 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints, 151 raw_ostream &OS) { 152 OS << "// Intrinsic ID to name table\n"; 153 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n"; 154 OS << " // Note that entry #0 is the invalid intrinsic!\n"; 155 for (unsigned i = 0, e = Ints.size(); i != e; ++i) 156 OS << " \"" << Ints[i].Name << "\",\n"; 157 OS << "#endif\n\n"; 158 } 159 160 void IntrinsicEmitter:: 161 EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints, 162 raw_ostream &OS) { 163 OS << "// Intrinsic ID to overload table\n"; 164 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n"; 165 OS << " // Note that entry #0 is the invalid intrinsic!\n"; 166 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 167 OS << " "; 168 if (Ints[i].isOverloaded) 169 OS << "true"; 170 else 171 OS << "false"; 172 OS << ",\n"; 173 } 174 OS << "#endif\n\n"; 175 } 176 177 static void EmitTypeForValueType(raw_ostream &OS, MVT::SimpleValueType VT) { 178 if (EVT(VT).isInteger()) { 179 unsigned BitWidth = EVT(VT).getSizeInBits(); 180 OS << "IntegerType::get(Context, " << BitWidth << ")"; 181 } else if (VT == MVT::Other) { 182 // MVT::OtherVT is used to mean the empty struct type here. 183 OS << "StructType::get(Context)"; 184 } else if (VT == MVT::f16) { 185 OS << "Type::getHalfTy(Context)"; 186 } else if (VT == MVT::f32) { 187 OS << "Type::getFloatTy(Context)"; 188 } else if (VT == MVT::f64) { 189 OS << "Type::getDoubleTy(Context)"; 190 } else if (VT == MVT::f80) { 191 OS << "Type::getX86_FP80Ty(Context)"; 192 } else if (VT == MVT::f128) { 193 OS << "Type::getFP128Ty(Context)"; 194 } else if (VT == MVT::ppcf128) { 195 OS << "Type::getPPC_FP128Ty(Context)"; 196 } else if (VT == MVT::isVoid) { 197 OS << "Type::getVoidTy(Context)"; 198 } else if (VT == MVT::Metadata) { 199 OS << "Type::getMetadataTy(Context)"; 200 } else if (VT == MVT::x86mmx) { 201 OS << "Type::getX86_MMXTy(Context)"; 202 } else { 203 assert(false && "Unsupported ValueType!"); 204 } 205 } 206 207 static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType, 208 unsigned &ArgNo); 209 210 static void EmitTypeGenerate(raw_ostream &OS, 211 const std::vector<Record*> &ArgTypes, 212 unsigned &ArgNo) { 213 if (ArgTypes.empty()) 214 return EmitTypeForValueType(OS, MVT::isVoid); 215 216 if (ArgTypes.size() == 1) 217 return EmitTypeGenerate(OS, ArgTypes.front(), ArgNo); 218 219 OS << "StructType::get("; 220 221 for (std::vector<Record*>::const_iterator 222 I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) { 223 EmitTypeGenerate(OS, *I, ArgNo); 224 OS << ", "; 225 } 226 227 OS << " NULL)"; 228 } 229 230 static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType, 231 unsigned &ArgNo) { 232 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT")); 233 234 if (ArgType->isSubClassOf("LLVMMatchType")) { 235 unsigned Number = ArgType->getValueAsInt("Number"); 236 assert(Number < ArgNo && "Invalid matching number!"); 237 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType")) 238 OS << "VectorType::getExtendedElementVectorType" 239 << "(dyn_cast<VectorType>(Tys[" << Number << "]))"; 240 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType")) 241 OS << "VectorType::getTruncatedElementVectorType" 242 << "(dyn_cast<VectorType>(Tys[" << Number << "]))"; 243 else 244 OS << "Tys[" << Number << "]"; 245 } else if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny) { 246 // NOTE: The ArgNo variable here is not the absolute argument number, it is 247 // the index of the "arbitrary" type in the Tys array passed to the 248 // Intrinsic::getDeclaration function. Consequently, we only want to 249 // increment it when we actually hit an overloaded type. Getting this wrong 250 // leads to very subtle bugs! 251 OS << "Tys[" << ArgNo++ << "]"; 252 } else if (EVT(VT).isVector()) { 253 EVT VVT = VT; 254 OS << "VectorType::get("; 255 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT().SimpleTy); 256 OS << ", " << VVT.getVectorNumElements() << ")"; 257 } else if (VT == MVT::iPTR) { 258 OS << "PointerType::getUnqual("; 259 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo); 260 OS << ")"; 261 } else if (VT == MVT::iPTRAny) { 262 // Make sure the user has passed us an argument type to overload. If not, 263 // treat it as an ordinary (not overloaded) intrinsic. 264 OS << "(" << ArgNo << " < Tys.size()) ? Tys[" << ArgNo 265 << "] : PointerType::getUnqual("; 266 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo); 267 OS << ")"; 268 ++ArgNo; 269 } else if (VT == MVT::isVoid) { 270 if (ArgNo == 0) 271 OS << "Type::getVoidTy(Context)"; 272 else 273 // MVT::isVoid is used to mean varargs here. 274 OS << "..."; 275 } else { 276 EmitTypeForValueType(OS, VT); 277 } 278 } 279 280 /// RecordListComparator - Provide a deterministic comparator for lists of 281 /// records. 282 namespace { 283 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair; 284 struct RecordListComparator { 285 bool operator()(const RecPair &LHS, 286 const RecPair &RHS) const { 287 unsigned i = 0; 288 const std::vector<Record*> *LHSVec = &LHS.first; 289 const std::vector<Record*> *RHSVec = &RHS.first; 290 unsigned RHSSize = RHSVec->size(); 291 unsigned LHSSize = LHSVec->size(); 292 293 for (; i != LHSSize; ++i) { 294 if (i == RHSSize) return false; // RHS is shorter than LHS. 295 if ((*LHSVec)[i] != (*RHSVec)[i]) 296 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName(); 297 } 298 299 if (i != RHSSize) return true; 300 301 i = 0; 302 LHSVec = &LHS.second; 303 RHSVec = &RHS.second; 304 RHSSize = RHSVec->size(); 305 LHSSize = LHSVec->size(); 306 307 for (i = 0; i != LHSSize; ++i) { 308 if (i == RHSSize) return false; // RHS is shorter than LHS. 309 if ((*LHSVec)[i] != (*RHSVec)[i]) 310 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName(); 311 } 312 313 return i != RHSSize; 314 } 315 }; 316 } 317 318 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, 319 raw_ostream &OS) { 320 OS << "// Verifier::visitIntrinsicFunctionCall code.\n"; 321 OS << "#ifdef GET_INTRINSIC_VERIFIER\n"; 322 OS << " switch (ID) {\n"; 323 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n"; 324 325 // This checking can emit a lot of very common code. To reduce the amount of 326 // code that we emit, batch up cases that have identical types. This avoids 327 // problems where GCC can run out of memory compiling Verifier.cpp. 328 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy; 329 MapTy UniqueArgInfos; 330 331 // Compute the unique argument type info. 332 for (unsigned i = 0, e = Ints.size(); i != e; ++i) 333 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs, 334 Ints[i].IS.ParamTypeDefs)].push_back(i); 335 336 // Loop through the array, emitting one comparison for each batch. 337 for (MapTy::iterator I = UniqueArgInfos.begin(), 338 E = UniqueArgInfos.end(); I != E; ++I) { 339 for (unsigned i = 0, e = I->second.size(); i != e; ++i) 340 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// " 341 << Ints[I->second[i]].Name << "\n"; 342 343 const RecPair &ArgTypes = I->first; 344 const std::vector<Record*> &RetTys = ArgTypes.first; 345 const std::vector<Record*> &ParamTys = ArgTypes.second; 346 std::vector<unsigned> OverloadedTypeIndices; 347 348 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", " 349 << ParamTys.size(); 350 351 // Emit return types. 352 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) { 353 Record *ArgType = RetTys[j]; 354 OS << ", "; 355 356 if (ArgType->isSubClassOf("LLVMMatchType")) { 357 unsigned Number = ArgType->getValueAsInt("Number"); 358 assert(Number < OverloadedTypeIndices.size() && 359 "Invalid matching number!"); 360 Number = OverloadedTypeIndices[Number]; 361 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType")) 362 OS << "~(ExtendedElementVectorType | " << Number << ")"; 363 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType")) 364 OS << "~(TruncatedElementVectorType | " << Number << ")"; 365 else 366 OS << "~" << Number; 367 } else { 368 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT")); 369 OS << getEnumName(VT); 370 371 if (EVT(VT).isOverloaded()) 372 OverloadedTypeIndices.push_back(j); 373 374 if (VT == MVT::isVoid && j != 0 && j != je - 1) 375 throw "Var arg type not last argument"; 376 } 377 } 378 379 // Emit the parameter types. 380 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) { 381 Record *ArgType = ParamTys[j]; 382 OS << ", "; 383 384 if (ArgType->isSubClassOf("LLVMMatchType")) { 385 unsigned Number = ArgType->getValueAsInt("Number"); 386 assert(Number < OverloadedTypeIndices.size() && 387 "Invalid matching number!"); 388 Number = OverloadedTypeIndices[Number]; 389 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType")) 390 OS << "~(ExtendedElementVectorType | " << Number << ")"; 391 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType")) 392 OS << "~(TruncatedElementVectorType | " << Number << ")"; 393 else 394 OS << "~" << Number; 395 } else { 396 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT")); 397 OS << getEnumName(VT); 398 399 if (EVT(VT).isOverloaded()) 400 OverloadedTypeIndices.push_back(j + RetTys.size()); 401 402 if (VT == MVT::isVoid && j != 0 && j != je - 1) 403 throw "Var arg type not last argument"; 404 } 405 } 406 407 OS << ");\n"; 408 OS << " break;\n"; 409 } 410 OS << " }\n"; 411 OS << "#endif\n\n"; 412 } 413 414 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints, 415 raw_ostream &OS) { 416 OS << "// Code for generating Intrinsic function declarations.\n"; 417 OS << "#ifdef GET_INTRINSIC_GENERATOR\n"; 418 OS << " switch (id) {\n"; 419 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n"; 420 421 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical 422 // types. 423 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy; 424 MapTy UniqueArgInfos; 425 426 // Compute the unique argument type info. 427 for (unsigned i = 0, e = Ints.size(); i != e; ++i) 428 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs, 429 Ints[i].IS.ParamTypeDefs)].push_back(i); 430 431 // Loop through the array, emitting one generator for each batch. 432 std::string IntrinsicStr = TargetPrefix + "Intrinsic::"; 433 434 for (MapTy::iterator I = UniqueArgInfos.begin(), 435 E = UniqueArgInfos.end(); I != E; ++I) { 436 for (unsigned i = 0, e = I->second.size(); i != e; ++i) 437 OS << " case " << IntrinsicStr << Ints[I->second[i]].EnumName 438 << ":\t\t// " << Ints[I->second[i]].Name << "\n"; 439 440 const RecPair &ArgTypes = I->first; 441 const std::vector<Record*> &RetTys = ArgTypes.first; 442 const std::vector<Record*> &ParamTys = ArgTypes.second; 443 444 unsigned N = ParamTys.size(); 445 446 if (N > 1 && 447 getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) { 448 OS << " IsVarArg = true;\n"; 449 --N; 450 } 451 452 unsigned ArgNo = 0; 453 OS << " ResultTy = "; 454 EmitTypeGenerate(OS, RetTys, ArgNo); 455 OS << ";\n"; 456 457 for (unsigned j = 0; j != N; ++j) { 458 OS << " ArgTys.push_back("; 459 EmitTypeGenerate(OS, ParamTys[j], ArgNo); 460 OS << ");\n"; 461 } 462 463 OS << " break;\n"; 464 } 465 466 OS << " }\n"; 467 OS << "#endif\n\n"; 468 } 469 470 namespace { 471 enum ModRefKind { 472 MRK_none, 473 MRK_readonly, 474 MRK_readnone 475 }; 476 477 ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) { 478 switch (intrinsic.ModRef) { 479 case CodeGenIntrinsic::NoMem: 480 return MRK_readnone; 481 case CodeGenIntrinsic::ReadArgMem: 482 case CodeGenIntrinsic::ReadMem: 483 return MRK_readonly; 484 case CodeGenIntrinsic::ReadWriteArgMem: 485 case CodeGenIntrinsic::ReadWriteMem: 486 return MRK_none; 487 } 488 llvm_unreachable("bad mod-ref kind"); 489 } 490 491 struct AttributeComparator { 492 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const { 493 // Sort throwing intrinsics after non-throwing intrinsics. 494 if (L->canThrow != R->canThrow) 495 return R->canThrow; 496 497 // Try to order by readonly/readnone attribute. 498 ModRefKind LK = getModRefKind(*L); 499 ModRefKind RK = getModRefKind(*R); 500 if (LK != RK) return (LK > RK); 501 502 // Order by argument attributes. 503 // This is reliable because each side is already sorted internally. 504 return (L->ArgumentAttributes < R->ArgumentAttributes); 505 } 506 }; 507 } 508 509 /// EmitAttributes - This emits the Intrinsic::getAttributes method. 510 void IntrinsicEmitter:: 511 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) { 512 OS << "// Add parameter attributes that are not common to all intrinsics.\n"; 513 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n"; 514 if (TargetOnly) 515 OS << "static AttrListPtr getAttributes(" << TargetPrefix 516 << "Intrinsic::ID id) {\n"; 517 else 518 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {\n"; 519 520 // Compute the maximum number of attribute arguments. 521 std::vector<const CodeGenIntrinsic*> sortedIntrinsics(Ints.size()); 522 unsigned maxArgAttrs = 0; 523 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 524 const CodeGenIntrinsic &intrinsic = Ints[i]; 525 sortedIntrinsics[i] = &intrinsic; 526 maxArgAttrs = 527 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size())); 528 } 529 530 // Emit an array of AttributeWithIndex. Most intrinsics will have 531 // at least one entry, for the function itself (index ~1), which is 532 // usually nounwind. 533 OS << " AttributeWithIndex AWI[" << maxArgAttrs+1 << "];\n"; 534 OS << " unsigned NumAttrs = 0;\n"; 535 OS << " switch (id) {\n"; 536 OS << " default: break;\n"; 537 538 AttributeComparator precedes; 539 540 std::stable_sort(sortedIntrinsics.begin(), sortedIntrinsics.end(), precedes); 541 542 for (unsigned i = 0, e = sortedIntrinsics.size(); i != e; ++i) { 543 const CodeGenIntrinsic &intrinsic = *sortedIntrinsics[i]; 544 OS << " case " << TargetPrefix << "Intrinsic::" 545 << intrinsic.EnumName << ":\n"; 546 547 // Fill out the case if this is the last case for this range of 548 // intrinsics. 549 if (i + 1 != e && !precedes(&intrinsic, sortedIntrinsics[i + 1])) 550 continue; 551 552 // Keep track of the number of attributes we're writing out. 553 unsigned numAttrs = 0; 554 555 // The argument attributes are alreadys sorted by argument index. 556 for (unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); ai != ae;) { 557 unsigned argNo = intrinsic.ArgumentAttributes[ai].first; 558 559 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(" 560 << argNo+1 << ", "; 561 562 bool moreThanOne = false; 563 564 do { 565 if (moreThanOne) OS << '|'; 566 567 switch (intrinsic.ArgumentAttributes[ai].second) { 568 case CodeGenIntrinsic::NoCapture: 569 OS << "Attribute::NoCapture"; 570 break; 571 } 572 573 ++ai; 574 moreThanOne = true; 575 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo); 576 577 OS << ");\n"; 578 } 579 580 ModRefKind modRef = getModRefKind(intrinsic); 581 582 if (!intrinsic.canThrow || modRef) { 583 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(~0, "; 584 if (!intrinsic.canThrow) { 585 OS << "Attribute::NoUnwind"; 586 if (modRef) OS << '|'; 587 } 588 switch (modRef) { 589 case MRK_none: break; 590 case MRK_readonly: OS << "Attribute::ReadOnly"; break; 591 case MRK_readnone: OS << "Attribute::ReadNone"; break; 592 } 593 OS << ");\n"; 594 } 595 596 if (numAttrs) { 597 OS << " NumAttrs = " << numAttrs << ";\n"; 598 OS << " break;\n"; 599 } else { 600 OS << " return AttrListPtr();\n"; 601 } 602 } 603 604 OS << " }\n"; 605 OS << " return AttrListPtr::get(AWI, NumAttrs);\n"; 606 OS << "}\n"; 607 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n"; 608 } 609 610 /// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior. 611 void IntrinsicEmitter:: 612 EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){ 613 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"; 614 OS << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"; 615 OS << "switch (iid) {\n"; 616 OS << "default:\n return UnknownModRefBehavior;\n"; 617 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 618 if (Ints[i].ModRef == CodeGenIntrinsic::ReadWriteMem) 619 continue; 620 OS << "case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName 621 << ":\n"; 622 switch (Ints[i].ModRef) { 623 default: 624 assert(false && "Unknown Mod/Ref type!"); 625 case CodeGenIntrinsic::NoMem: 626 OS << " return DoesNotAccessMemory;\n"; 627 break; 628 case CodeGenIntrinsic::ReadArgMem: 629 OS << " return OnlyReadsArgumentPointees;\n"; 630 break; 631 case CodeGenIntrinsic::ReadMem: 632 OS << " return OnlyReadsMemory;\n"; 633 break; 634 case CodeGenIntrinsic::ReadWriteArgMem: 635 OS << " return OnlyAccessesArgumentPointees;\n"; 636 break; 637 } 638 } 639 OS << "}\n"; 640 OS << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n"; 641 } 642 643 void IntrinsicEmitter:: 644 EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){ 645 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n"; 646 OS << "#ifdef GET_GCC_BUILTIN_NAME\n"; 647 OS << " switch (F->getIntrinsicID()) {\n"; 648 OS << " default: BuiltinName = \"\"; break;\n"; 649 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 650 if (!Ints[i].GCCBuiltinName.empty()) { 651 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \"" 652 << Ints[i].GCCBuiltinName << "\"; break;\n"; 653 } 654 } 655 OS << " }\n"; 656 OS << "#endif\n\n"; 657 } 658 659 /// EmitTargetBuiltins - All of the builtins in the specified map are for the 660 /// same target, and we already checked it. 661 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM, 662 const std::string &TargetPrefix, 663 raw_ostream &OS) { 664 665 std::vector<StringMatcher::StringPair> Results; 666 667 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(), 668 E = BIM.end(); I != E; ++I) { 669 std::string ResultCode = 670 "return " + TargetPrefix + "Intrinsic::" + I->second + ";"; 671 Results.push_back(StringMatcher::StringPair(I->first, ResultCode)); 672 } 673 674 StringMatcher("BuiltinName", Results, OS).Emit(); 675 } 676 677 678 void IntrinsicEmitter:: 679 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints, 680 raw_ostream &OS) { 681 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy; 682 BIMTy BuiltinMap; 683 for (unsigned i = 0, e = Ints.size(); i != e; ++i) { 684 if (!Ints[i].GCCBuiltinName.empty()) { 685 // Get the map for this target prefix. 686 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix]; 687 688 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName, 689 Ints[i].EnumName)).second) 690 throw "Intrinsic '" + Ints[i].TheDef->getName() + 691 "': duplicate GCC builtin name!"; 692 } 693 } 694 695 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n"; 696 OS << "// This is used by the C front-end. The GCC builtin name is passed\n"; 697 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n"; 698 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n"; 699 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n"; 700 701 if (TargetOnly) { 702 OS << "static " << TargetPrefix << "Intrinsic::ID " 703 << "getIntrinsicForGCCBuiltin(const char " 704 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n"; 705 } else { 706 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char " 707 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n"; 708 } 709 710 OS << " StringRef BuiltinName(BuiltinNameStr);\n"; 711 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n"; 712 713 // Note: this could emit significantly better code if we cared. 714 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){ 715 OS << " "; 716 if (!I->first.empty()) 717 OS << "if (TargetPrefix == \"" << I->first << "\") "; 718 else 719 OS << "/* Target Independent Builtins */ "; 720 OS << "{\n"; 721 722 // Emit the comparisons for this target prefix. 723 EmitTargetBuiltins(I->second, TargetPrefix, OS); 724 OS << " }\n"; 725 } 726 OS << " return "; 727 if (!TargetPrefix.empty()) 728 OS << "(" << TargetPrefix << "Intrinsic::ID)"; 729 OS << "Intrinsic::not_intrinsic;\n"; 730 OS << "}\n"; 731 OS << "#endif\n\n"; 732 } 733