1 //===-- Intrinsics.cpp - Intrinsic Function Handling ------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements functions required for supporting intrinsic functions. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/IR/Intrinsics.h" 14 #include "llvm/ADT/StringExtras.h" 15 #include "llvm/ADT/StringTable.h" 16 #include "llvm/IR/Function.h" 17 #include "llvm/IR/IntrinsicsAArch64.h" 18 #include "llvm/IR/IntrinsicsAMDGPU.h" 19 #include "llvm/IR/IntrinsicsARM.h" 20 #include "llvm/IR/IntrinsicsBPF.h" 21 #include "llvm/IR/IntrinsicsHexagon.h" 22 #include "llvm/IR/IntrinsicsLoongArch.h" 23 #include "llvm/IR/IntrinsicsMips.h" 24 #include "llvm/IR/IntrinsicsNVPTX.h" 25 #include "llvm/IR/IntrinsicsPowerPC.h" 26 #include "llvm/IR/IntrinsicsR600.h" 27 #include "llvm/IR/IntrinsicsRISCV.h" 28 #include "llvm/IR/IntrinsicsS390.h" 29 #include "llvm/IR/IntrinsicsVE.h" 30 #include "llvm/IR/IntrinsicsX86.h" 31 #include "llvm/IR/IntrinsicsXCore.h" 32 #include "llvm/IR/Module.h" 33 #include "llvm/IR/Type.h" 34 35 using namespace llvm; 36 37 /// Table of string intrinsic names indexed by enum value. 38 #define GET_INTRINSIC_NAME_TABLE 39 #include "llvm/IR/IntrinsicImpl.inc" 40 #undef GET_INTRINSIC_NAME_TABLE 41 42 StringRef Intrinsic::getBaseName(ID id) { 43 assert(id < num_intrinsics && "Invalid intrinsic ID!"); 44 return IntrinsicNameTable[IntrinsicNameOffsetTable[id]]; 45 } 46 47 StringRef Intrinsic::getName(ID id) { 48 assert(id < num_intrinsics && "Invalid intrinsic ID!"); 49 assert(!Intrinsic::isOverloaded(id) && 50 "This version of getName does not support overloading"); 51 return getBaseName(id); 52 } 53 54 /// Returns a stable mangling for the type specified for use in the name 55 /// mangling scheme used by 'any' types in intrinsic signatures. The mangling 56 /// of named types is simply their name. Manglings for unnamed types consist 57 /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions) 58 /// combined with the mangling of their component types. A vararg function 59 /// type will have a suffix of 'vararg'. Since function types can contain 60 /// other function types, we close a function type mangling with suffix 'f' 61 /// which can't be confused with it's prefix. This ensures we don't have 62 /// collisions between two unrelated function types. Otherwise, you might 63 /// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.) 64 /// The HasUnnamedType boolean is set if an unnamed type was encountered, 65 /// indicating that extra care must be taken to ensure a unique name. 66 static std::string getMangledTypeStr(Type *Ty, bool &HasUnnamedType) { 67 std::string Result; 68 if (PointerType *PTyp = dyn_cast<PointerType>(Ty)) { 69 Result += "p" + utostr(PTyp->getAddressSpace()); 70 } else if (ArrayType *ATyp = dyn_cast<ArrayType>(Ty)) { 71 Result += "a" + utostr(ATyp->getNumElements()) + 72 getMangledTypeStr(ATyp->getElementType(), HasUnnamedType); 73 } else if (StructType *STyp = dyn_cast<StructType>(Ty)) { 74 if (!STyp->isLiteral()) { 75 Result += "s_"; 76 if (STyp->hasName()) 77 Result += STyp->getName(); 78 else 79 HasUnnamedType = true; 80 } else { 81 Result += "sl_"; 82 for (auto *Elem : STyp->elements()) 83 Result += getMangledTypeStr(Elem, HasUnnamedType); 84 } 85 // Ensure nested structs are distinguishable. 86 Result += "s"; 87 } else if (FunctionType *FT = dyn_cast<FunctionType>(Ty)) { 88 Result += "f_" + getMangledTypeStr(FT->getReturnType(), HasUnnamedType); 89 for (size_t i = 0; i < FT->getNumParams(); i++) 90 Result += getMangledTypeStr(FT->getParamType(i), HasUnnamedType); 91 if (FT->isVarArg()) 92 Result += "vararg"; 93 // Ensure nested function types are distinguishable. 94 Result += "f"; 95 } else if (VectorType *VTy = dyn_cast<VectorType>(Ty)) { 96 ElementCount EC = VTy->getElementCount(); 97 if (EC.isScalable()) 98 Result += "nx"; 99 Result += "v" + utostr(EC.getKnownMinValue()) + 100 getMangledTypeStr(VTy->getElementType(), HasUnnamedType); 101 } else if (TargetExtType *TETy = dyn_cast<TargetExtType>(Ty)) { 102 Result += "t"; 103 Result += TETy->getName(); 104 for (Type *ParamTy : TETy->type_params()) 105 Result += "_" + getMangledTypeStr(ParamTy, HasUnnamedType); 106 for (unsigned IntParam : TETy->int_params()) 107 Result += "_" + utostr(IntParam); 108 // Ensure nested target extension types are distinguishable. 109 Result += "t"; 110 } else if (Ty) { 111 switch (Ty->getTypeID()) { 112 default: 113 llvm_unreachable("Unhandled type"); 114 case Type::VoidTyID: 115 Result += "isVoid"; 116 break; 117 case Type::MetadataTyID: 118 Result += "Metadata"; 119 break; 120 case Type::HalfTyID: 121 Result += "f16"; 122 break; 123 case Type::BFloatTyID: 124 Result += "bf16"; 125 break; 126 case Type::FloatTyID: 127 Result += "f32"; 128 break; 129 case Type::DoubleTyID: 130 Result += "f64"; 131 break; 132 case Type::X86_FP80TyID: 133 Result += "f80"; 134 break; 135 case Type::FP128TyID: 136 Result += "f128"; 137 break; 138 case Type::PPC_FP128TyID: 139 Result += "ppcf128"; 140 break; 141 case Type::X86_AMXTyID: 142 Result += "x86amx"; 143 break; 144 case Type::IntegerTyID: 145 Result += "i" + utostr(cast<IntegerType>(Ty)->getBitWidth()); 146 break; 147 } 148 } 149 return Result; 150 } 151 152 static std::string getIntrinsicNameImpl(Intrinsic::ID Id, ArrayRef<Type *> Tys, 153 Module *M, FunctionType *FT, 154 bool EarlyModuleCheck) { 155 156 assert(Id < Intrinsic::num_intrinsics && "Invalid intrinsic ID!"); 157 assert((Tys.empty() || Intrinsic::isOverloaded(Id)) && 158 "This version of getName is for overloaded intrinsics only"); 159 (void)EarlyModuleCheck; 160 assert((!EarlyModuleCheck || M || 161 !any_of(Tys, [](Type *T) { return isa<PointerType>(T); })) && 162 "Intrinsic overloading on pointer types need to provide a Module"); 163 bool HasUnnamedType = false; 164 std::string Result(Intrinsic::getBaseName(Id)); 165 for (Type *Ty : Tys) 166 Result += "." + getMangledTypeStr(Ty, HasUnnamedType); 167 if (HasUnnamedType) { 168 assert(M && "unnamed types need a module"); 169 if (!FT) 170 FT = Intrinsic::getType(M->getContext(), Id, Tys); 171 else 172 assert((FT == Intrinsic::getType(M->getContext(), Id, Tys)) && 173 "Provided FunctionType must match arguments"); 174 return M->getUniqueIntrinsicName(Result, Id, FT); 175 } 176 return Result; 177 } 178 179 std::string Intrinsic::getName(ID Id, ArrayRef<Type *> Tys, Module *M, 180 FunctionType *FT) { 181 assert(M && "We need to have a Module"); 182 return getIntrinsicNameImpl(Id, Tys, M, FT, true); 183 } 184 185 std::string Intrinsic::getNameNoUnnamedTypes(ID Id, ArrayRef<Type *> Tys) { 186 return getIntrinsicNameImpl(Id, Tys, nullptr, nullptr, false); 187 } 188 189 /// IIT_Info - These are enumerators that describe the entries returned by the 190 /// getIntrinsicInfoTableEntries function. 191 /// 192 /// Defined in Intrinsics.td. 193 enum IIT_Info { 194 #define GET_INTRINSIC_IITINFO 195 #include "llvm/IR/IntrinsicImpl.inc" 196 #undef GET_INTRINSIC_IITINFO 197 }; 198 199 static void 200 DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos, 201 IIT_Info LastInfo, 202 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) { 203 using namespace Intrinsic; 204 205 bool IsScalableVector = (LastInfo == IIT_SCALABLE_VEC); 206 207 IIT_Info Info = IIT_Info(Infos[NextElt++]); 208 unsigned StructElts = 2; 209 210 switch (Info) { 211 case IIT_Done: 212 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0)); 213 return; 214 case IIT_VARARG: 215 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0)); 216 return; 217 case IIT_MMX: 218 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0)); 219 return; 220 case IIT_AMX: 221 OutputTable.push_back(IITDescriptor::get(IITDescriptor::AMX, 0)); 222 return; 223 case IIT_TOKEN: 224 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0)); 225 return; 226 case IIT_METADATA: 227 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0)); 228 return; 229 case IIT_F16: 230 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0)); 231 return; 232 case IIT_BF16: 233 OutputTable.push_back(IITDescriptor::get(IITDescriptor::BFloat, 0)); 234 return; 235 case IIT_F32: 236 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0)); 237 return; 238 case IIT_F64: 239 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0)); 240 return; 241 case IIT_F128: 242 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Quad, 0)); 243 return; 244 case IIT_PPCF128: 245 OutputTable.push_back(IITDescriptor::get(IITDescriptor::PPCQuad, 0)); 246 return; 247 case IIT_I1: 248 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1)); 249 return; 250 case IIT_I2: 251 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 2)); 252 return; 253 case IIT_I4: 254 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 4)); 255 return; 256 case IIT_AARCH64_SVCOUNT: 257 OutputTable.push_back(IITDescriptor::get(IITDescriptor::AArch64Svcount, 0)); 258 return; 259 case IIT_I8: 260 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8)); 261 return; 262 case IIT_I16: 263 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 16)); 264 return; 265 case IIT_I32: 266 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32)); 267 return; 268 case IIT_I64: 269 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64)); 270 return; 271 case IIT_I128: 272 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128)); 273 return; 274 case IIT_V1: 275 OutputTable.push_back(IITDescriptor::getVector(1, IsScalableVector)); 276 DecodeIITType(NextElt, Infos, Info, OutputTable); 277 return; 278 case IIT_V2: 279 OutputTable.push_back(IITDescriptor::getVector(2, IsScalableVector)); 280 DecodeIITType(NextElt, Infos, Info, OutputTable); 281 return; 282 case IIT_V3: 283 OutputTable.push_back(IITDescriptor::getVector(3, IsScalableVector)); 284 DecodeIITType(NextElt, Infos, Info, OutputTable); 285 return; 286 case IIT_V4: 287 OutputTable.push_back(IITDescriptor::getVector(4, IsScalableVector)); 288 DecodeIITType(NextElt, Infos, Info, OutputTable); 289 return; 290 case IIT_V6: 291 OutputTable.push_back(IITDescriptor::getVector(6, IsScalableVector)); 292 DecodeIITType(NextElt, Infos, Info, OutputTable); 293 return; 294 case IIT_V8: 295 OutputTable.push_back(IITDescriptor::getVector(8, IsScalableVector)); 296 DecodeIITType(NextElt, Infos, Info, OutputTable); 297 return; 298 case IIT_V10: 299 OutputTable.push_back(IITDescriptor::getVector(10, IsScalableVector)); 300 DecodeIITType(NextElt, Infos, Info, OutputTable); 301 return; 302 case IIT_V16: 303 OutputTable.push_back(IITDescriptor::getVector(16, IsScalableVector)); 304 DecodeIITType(NextElt, Infos, Info, OutputTable); 305 return; 306 case IIT_V32: 307 OutputTable.push_back(IITDescriptor::getVector(32, IsScalableVector)); 308 DecodeIITType(NextElt, Infos, Info, OutputTable); 309 return; 310 case IIT_V64: 311 OutputTable.push_back(IITDescriptor::getVector(64, IsScalableVector)); 312 DecodeIITType(NextElt, Infos, Info, OutputTable); 313 return; 314 case IIT_V128: 315 OutputTable.push_back(IITDescriptor::getVector(128, IsScalableVector)); 316 DecodeIITType(NextElt, Infos, Info, OutputTable); 317 return; 318 case IIT_V256: 319 OutputTable.push_back(IITDescriptor::getVector(256, IsScalableVector)); 320 DecodeIITType(NextElt, Infos, Info, OutputTable); 321 return; 322 case IIT_V512: 323 OutputTable.push_back(IITDescriptor::getVector(512, IsScalableVector)); 324 DecodeIITType(NextElt, Infos, Info, OutputTable); 325 return; 326 case IIT_V1024: 327 OutputTable.push_back(IITDescriptor::getVector(1024, IsScalableVector)); 328 DecodeIITType(NextElt, Infos, Info, OutputTable); 329 return; 330 case IIT_EXTERNREF: 331 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 10)); 332 return; 333 case IIT_FUNCREF: 334 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 20)); 335 return; 336 case IIT_PTR: 337 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0)); 338 return; 339 case IIT_ANYPTR: // [ANYPTR addrspace] 340 OutputTable.push_back( 341 IITDescriptor::get(IITDescriptor::Pointer, Infos[NextElt++])); 342 return; 343 case IIT_ARG: { 344 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 345 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo)); 346 return; 347 } 348 case IIT_EXTEND_ARG: { 349 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 350 OutputTable.push_back( 351 IITDescriptor::get(IITDescriptor::ExtendArgument, ArgInfo)); 352 return; 353 } 354 case IIT_TRUNC_ARG: { 355 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 356 OutputTable.push_back( 357 IITDescriptor::get(IITDescriptor::TruncArgument, ArgInfo)); 358 return; 359 } 360 case IIT_HALF_VEC_ARG: { 361 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 362 OutputTable.push_back( 363 IITDescriptor::get(IITDescriptor::HalfVecArgument, ArgInfo)); 364 return; 365 } 366 case IIT_SAME_VEC_WIDTH_ARG: { 367 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 368 OutputTable.push_back( 369 IITDescriptor::get(IITDescriptor::SameVecWidthArgument, ArgInfo)); 370 return; 371 } 372 case IIT_VEC_OF_ANYPTRS_TO_ELT: { 373 unsigned short ArgNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 374 unsigned short RefNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 375 OutputTable.push_back( 376 IITDescriptor::get(IITDescriptor::VecOfAnyPtrsToElt, ArgNo, RefNo)); 377 return; 378 } 379 case IIT_EMPTYSTRUCT: 380 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0)); 381 return; 382 case IIT_STRUCT9: 383 ++StructElts; 384 [[fallthrough]]; 385 case IIT_STRUCT8: 386 ++StructElts; 387 [[fallthrough]]; 388 case IIT_STRUCT7: 389 ++StructElts; 390 [[fallthrough]]; 391 case IIT_STRUCT6: 392 ++StructElts; 393 [[fallthrough]]; 394 case IIT_STRUCT5: 395 ++StructElts; 396 [[fallthrough]]; 397 case IIT_STRUCT4: 398 ++StructElts; 399 [[fallthrough]]; 400 case IIT_STRUCT3: 401 ++StructElts; 402 [[fallthrough]]; 403 case IIT_STRUCT2: { 404 OutputTable.push_back( 405 IITDescriptor::get(IITDescriptor::Struct, StructElts)); 406 407 for (unsigned i = 0; i != StructElts; ++i) 408 DecodeIITType(NextElt, Infos, Info, OutputTable); 409 return; 410 } 411 case IIT_SUBDIVIDE2_ARG: { 412 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 413 OutputTable.push_back( 414 IITDescriptor::get(IITDescriptor::Subdivide2Argument, ArgInfo)); 415 return; 416 } 417 case IIT_SUBDIVIDE4_ARG: { 418 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 419 OutputTable.push_back( 420 IITDescriptor::get(IITDescriptor::Subdivide4Argument, ArgInfo)); 421 return; 422 } 423 case IIT_VEC_ELEMENT: { 424 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 425 OutputTable.push_back( 426 IITDescriptor::get(IITDescriptor::VecElementArgument, ArgInfo)); 427 return; 428 } 429 case IIT_SCALABLE_VEC: { 430 DecodeIITType(NextElt, Infos, Info, OutputTable); 431 return; 432 } 433 case IIT_VEC_OF_BITCASTS_TO_INT: { 434 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 435 OutputTable.push_back( 436 IITDescriptor::get(IITDescriptor::VecOfBitcastsToInt, ArgInfo)); 437 return; 438 } 439 } 440 llvm_unreachable("unhandled"); 441 } 442 443 #define GET_INTRINSIC_GENERATOR_GLOBAL 444 #include "llvm/IR/IntrinsicImpl.inc" 445 #undef GET_INTRINSIC_GENERATOR_GLOBAL 446 447 void Intrinsic::getIntrinsicInfoTableEntries( 448 ID id, SmallVectorImpl<IITDescriptor> &T) { 449 static_assert(sizeof(IIT_Table[0]) == 2, 450 "Expect 16-bit entries in IIT_Table"); 451 // Check to see if the intrinsic's type was expressible by the table. 452 uint16_t TableVal = IIT_Table[id - 1]; 453 454 // Decode the TableVal into an array of IITValues. 455 SmallVector<unsigned char> IITValues; 456 ArrayRef<unsigned char> IITEntries; 457 unsigned NextElt = 0; 458 if (TableVal >> 15) { 459 // This is an offset into the IIT_LongEncodingTable. 460 IITEntries = IIT_LongEncodingTable; 461 462 // Strip sentinel bit. 463 NextElt = TableVal & 0x7fff; 464 } else { 465 // If the entry was encoded into a single word in the table itself, decode 466 // it from an array of nibbles to an array of bytes. 467 do { 468 IITValues.push_back(TableVal & 0xF); 469 TableVal >>= 4; 470 } while (TableVal); 471 472 IITEntries = IITValues; 473 NextElt = 0; 474 } 475 476 // Okay, decode the table into the output vector of IITDescriptors. 477 DecodeIITType(NextElt, IITEntries, IIT_Done, T); 478 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0) 479 DecodeIITType(NextElt, IITEntries, IIT_Done, T); 480 } 481 482 static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos, 483 ArrayRef<Type *> Tys, LLVMContext &Context) { 484 using namespace Intrinsic; 485 486 IITDescriptor D = Infos.front(); 487 Infos = Infos.slice(1); 488 489 switch (D.Kind) { 490 case IITDescriptor::Void: 491 return Type::getVoidTy(Context); 492 case IITDescriptor::VarArg: 493 return Type::getVoidTy(Context); 494 case IITDescriptor::MMX: 495 return llvm::FixedVectorType::get(llvm::IntegerType::get(Context, 64), 1); 496 case IITDescriptor::AMX: 497 return Type::getX86_AMXTy(Context); 498 case IITDescriptor::Token: 499 return Type::getTokenTy(Context); 500 case IITDescriptor::Metadata: 501 return Type::getMetadataTy(Context); 502 case IITDescriptor::Half: 503 return Type::getHalfTy(Context); 504 case IITDescriptor::BFloat: 505 return Type::getBFloatTy(Context); 506 case IITDescriptor::Float: 507 return Type::getFloatTy(Context); 508 case IITDescriptor::Double: 509 return Type::getDoubleTy(Context); 510 case IITDescriptor::Quad: 511 return Type::getFP128Ty(Context); 512 case IITDescriptor::PPCQuad: 513 return Type::getPPC_FP128Ty(Context); 514 case IITDescriptor::AArch64Svcount: 515 return TargetExtType::get(Context, "aarch64.svcount"); 516 517 case IITDescriptor::Integer: 518 return IntegerType::get(Context, D.Integer_Width); 519 case IITDescriptor::Vector: 520 return VectorType::get(DecodeFixedType(Infos, Tys, Context), 521 D.Vector_Width); 522 case IITDescriptor::Pointer: 523 return PointerType::get(Context, D.Pointer_AddressSpace); 524 case IITDescriptor::Struct: { 525 SmallVector<Type *, 8> Elts; 526 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) 527 Elts.push_back(DecodeFixedType(Infos, Tys, Context)); 528 return StructType::get(Context, Elts); 529 } 530 case IITDescriptor::Argument: 531 return Tys[D.getArgumentNumber()]; 532 case IITDescriptor::ExtendArgument: { 533 Type *Ty = Tys[D.getArgumentNumber()]; 534 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) 535 return VectorType::getExtendedElementVectorType(VTy); 536 537 return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth()); 538 } 539 case IITDescriptor::TruncArgument: { 540 Type *Ty = Tys[D.getArgumentNumber()]; 541 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) 542 return VectorType::getTruncatedElementVectorType(VTy); 543 544 IntegerType *ITy = cast<IntegerType>(Ty); 545 assert(ITy->getBitWidth() % 2 == 0); 546 return IntegerType::get(Context, ITy->getBitWidth() / 2); 547 } 548 case IITDescriptor::Subdivide2Argument: 549 case IITDescriptor::Subdivide4Argument: { 550 Type *Ty = Tys[D.getArgumentNumber()]; 551 VectorType *VTy = dyn_cast<VectorType>(Ty); 552 assert(VTy && "Expected an argument of Vector Type"); 553 int SubDivs = D.Kind == IITDescriptor::Subdivide2Argument ? 1 : 2; 554 return VectorType::getSubdividedVectorType(VTy, SubDivs); 555 } 556 case IITDescriptor::HalfVecArgument: 557 return VectorType::getHalfElementsVectorType( 558 cast<VectorType>(Tys[D.getArgumentNumber()])); 559 case IITDescriptor::SameVecWidthArgument: { 560 Type *EltTy = DecodeFixedType(Infos, Tys, Context); 561 Type *Ty = Tys[D.getArgumentNumber()]; 562 if (auto *VTy = dyn_cast<VectorType>(Ty)) 563 return VectorType::get(EltTy, VTy->getElementCount()); 564 return EltTy; 565 } 566 case IITDescriptor::VecElementArgument: { 567 Type *Ty = Tys[D.getArgumentNumber()]; 568 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) 569 return VTy->getElementType(); 570 llvm_unreachable("Expected an argument of Vector Type"); 571 } 572 case IITDescriptor::VecOfBitcastsToInt: { 573 Type *Ty = Tys[D.getArgumentNumber()]; 574 VectorType *VTy = dyn_cast<VectorType>(Ty); 575 assert(VTy && "Expected an argument of Vector Type"); 576 return VectorType::getInteger(VTy); 577 } 578 case IITDescriptor::VecOfAnyPtrsToElt: 579 // Return the overloaded type (which determines the pointers address space) 580 return Tys[D.getOverloadArgNumber()]; 581 } 582 llvm_unreachable("unhandled"); 583 } 584 585 FunctionType *Intrinsic::getType(LLVMContext &Context, ID id, 586 ArrayRef<Type *> Tys) { 587 SmallVector<IITDescriptor, 8> Table; 588 getIntrinsicInfoTableEntries(id, Table); 589 590 ArrayRef<IITDescriptor> TableRef = Table; 591 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context); 592 593 SmallVector<Type *, 8> ArgTys; 594 while (!TableRef.empty()) 595 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context)); 596 597 // DecodeFixedType returns Void for IITDescriptor::Void and 598 // IITDescriptor::VarArg If we see void type as the type of the last argument, 599 // it is vararg intrinsic 600 if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) { 601 ArgTys.pop_back(); 602 return FunctionType::get(ResultTy, ArgTys, true); 603 } 604 return FunctionType::get(ResultTy, ArgTys, false); 605 } 606 607 bool Intrinsic::isOverloaded(ID id) { 608 #define GET_INTRINSIC_OVERLOAD_TABLE 609 #include "llvm/IR/IntrinsicImpl.inc" 610 #undef GET_INTRINSIC_OVERLOAD_TABLE 611 } 612 613 /// Table of per-target intrinsic name tables. 614 #define GET_INTRINSIC_TARGET_DATA 615 #include "llvm/IR/IntrinsicImpl.inc" 616 #undef GET_INTRINSIC_TARGET_DATA 617 618 bool Intrinsic::isTargetIntrinsic(Intrinsic::ID IID) { 619 return IID > TargetInfos[0].Count; 620 } 621 622 /// Looks up Name in NameTable via binary search. NameTable must be sorted 623 /// and all entries must start with "llvm.". If NameTable contains an exact 624 /// match for Name or a prefix of Name followed by a dot, its index in 625 /// NameTable is returned. Otherwise, -1 is returned. 626 static int lookupLLVMIntrinsicByName(ArrayRef<unsigned> NameOffsetTable, 627 StringRef Name, StringRef Target = "") { 628 assert(Name.starts_with("llvm.") && "Unexpected intrinsic prefix"); 629 assert(Name.drop_front(5).starts_with(Target) && "Unexpected target"); 630 631 // Do successive binary searches of the dotted name components. For 632 // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of 633 // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then 634 // "llvm.gc.experimental.statepoint", and then we will stop as the range is 635 // size 1. During the search, we can skip the prefix that we already know is 636 // identical. By using strncmp we consider names with differing suffixes to 637 // be part of the equal range. 638 size_t CmpEnd = 4; // Skip the "llvm" component. 639 if (!Target.empty()) 640 CmpEnd += 1 + Target.size(); // skip the .target component. 641 642 const unsigned *Low = NameOffsetTable.begin(); 643 const unsigned *High = NameOffsetTable.end(); 644 const unsigned *LastLow = Low; 645 while (CmpEnd < Name.size() && High - Low > 0) { 646 size_t CmpStart = CmpEnd; 647 CmpEnd = Name.find('.', CmpStart + 1); 648 CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd; 649 auto Cmp = [CmpStart, CmpEnd](auto LHS, auto RHS) { 650 // `equal_range` requires the comparison to work with either side being an 651 // offset or the value. Detect which kind each side is to set up the 652 // compared strings. 653 StringRef LHSStr; 654 if constexpr (std::is_integral_v<decltype(LHS)>) { 655 LHSStr = IntrinsicNameTable[LHS]; 656 } else { 657 LHSStr = LHS; 658 } 659 StringRef RHSStr; 660 if constexpr (std::is_integral_v<decltype(RHS)>) { 661 RHSStr = IntrinsicNameTable[RHS]; 662 } else { 663 RHSStr = RHS; 664 } 665 return strncmp(LHSStr.data() + CmpStart, RHSStr.data() + CmpStart, 666 CmpEnd - CmpStart) < 0; 667 }; 668 LastLow = Low; 669 std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp); 670 } 671 if (High - Low > 0) 672 LastLow = Low; 673 674 if (LastLow == NameOffsetTable.end()) 675 return -1; 676 StringRef NameFound = IntrinsicNameTable[*LastLow]; 677 if (Name == NameFound || 678 (Name.starts_with(NameFound) && Name[NameFound.size()] == '.')) 679 return LastLow - NameOffsetTable.begin(); 680 return -1; 681 } 682 683 /// Find the segment of \c IntrinsicNameOffsetTable for intrinsics with the same 684 /// target as \c Name, or the generic table if \c Name is not target specific. 685 /// 686 /// Returns the relevant slice of \c IntrinsicNameOffsetTable and the target 687 /// name. 688 static std::pair<ArrayRef<unsigned>, StringRef> 689 findTargetSubtable(StringRef Name) { 690 assert(Name.starts_with("llvm.")); 691 692 ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos); 693 // Drop "llvm." and take the first dotted component. That will be the target 694 // if this is target specific. 695 StringRef Target = Name.drop_front(5).split('.').first; 696 auto It = partition_point( 697 Targets, [=](const IntrinsicTargetInfo &TI) { return TI.Name < Target; }); 698 // We've either found the target or just fall back to the generic set, which 699 // is always first. 700 const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0]; 701 return {ArrayRef(&IntrinsicNameOffsetTable[1] + TI.Offset, TI.Count), 702 TI.Name}; 703 } 704 705 /// This does the actual lookup of an intrinsic ID which matches the given 706 /// function name. 707 Intrinsic::ID Intrinsic::lookupIntrinsicID(StringRef Name) { 708 auto [NameOffsetTable, Target] = findTargetSubtable(Name); 709 int Idx = lookupLLVMIntrinsicByName(NameOffsetTable, Name, Target); 710 if (Idx == -1) 711 return Intrinsic::not_intrinsic; 712 713 // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have 714 // an index into a sub-table. 715 int Adjust = NameOffsetTable.data() - IntrinsicNameOffsetTable; 716 Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust); 717 718 // If the intrinsic is not overloaded, require an exact match. If it is 719 // overloaded, require either exact or prefix match. 720 const auto MatchSize = IntrinsicNameTable[NameOffsetTable[Idx]].size(); 721 assert(Name.size() >= MatchSize && "Expected either exact or prefix match"); 722 bool IsExactMatch = Name.size() == MatchSize; 723 return IsExactMatch || Intrinsic::isOverloaded(ID) ? ID 724 : Intrinsic::not_intrinsic; 725 } 726 727 /// This defines the "Intrinsic::getAttributes(ID id)" method. 728 #define GET_INTRINSIC_ATTRIBUTES 729 #include "llvm/IR/IntrinsicImpl.inc" 730 #undef GET_INTRINSIC_ATTRIBUTES 731 732 Function *Intrinsic::getOrInsertDeclaration(Module *M, ID id, 733 ArrayRef<Type *> Tys) { 734 // There can never be multiple globals with the same name of different types, 735 // because intrinsics must be a specific type. 736 auto *FT = getType(M->getContext(), id, Tys); 737 return cast<Function>( 738 M->getOrInsertFunction( 739 Tys.empty() ? getName(id) : getName(id, Tys, M, FT), FT) 740 .getCallee()); 741 } 742 743 Function *Intrinsic::getDeclarationIfExists(const Module *M, ID id) { 744 return M->getFunction(getName(id)); 745 } 746 747 Function *Intrinsic::getDeclarationIfExists(Module *M, ID id, 748 ArrayRef<Type *> Tys, 749 FunctionType *FT) { 750 return M->getFunction(getName(id, Tys, M, FT)); 751 } 752 753 // This defines the "Intrinsic::getIntrinsicForClangBuiltin()" method. 754 #define GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN 755 #include "llvm/IR/IntrinsicImpl.inc" 756 #undef GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN 757 758 // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method. 759 #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN 760 #include "llvm/IR/IntrinsicImpl.inc" 761 #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN 762 763 bool Intrinsic::isConstrainedFPIntrinsic(ID QID) { 764 switch (QID) { 765 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \ 766 case Intrinsic::INTRINSIC: 767 #include "llvm/IR/ConstrainedOps.def" 768 #undef INSTRUCTION 769 return true; 770 default: 771 return false; 772 } 773 } 774 775 bool Intrinsic::hasConstrainedFPRoundingModeOperand(Intrinsic::ID QID) { 776 switch (QID) { 777 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \ 778 case Intrinsic::INTRINSIC: \ 779 return ROUND_MODE == 1; 780 #include "llvm/IR/ConstrainedOps.def" 781 #undef INSTRUCTION 782 default: 783 return false; 784 } 785 } 786 787 using DeferredIntrinsicMatchPair = 788 std::pair<Type *, ArrayRef<Intrinsic::IITDescriptor>>; 789 790 static bool 791 matchIntrinsicType(Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos, 792 SmallVectorImpl<Type *> &ArgTys, 793 SmallVectorImpl<DeferredIntrinsicMatchPair> &DeferredChecks, 794 bool IsDeferredCheck) { 795 using namespace Intrinsic; 796 797 // If we ran out of descriptors, there are too many arguments. 798 if (Infos.empty()) 799 return true; 800 801 // Do this before slicing off the 'front' part 802 auto InfosRef = Infos; 803 auto DeferCheck = [&DeferredChecks, &InfosRef](Type *T) { 804 DeferredChecks.emplace_back(T, InfosRef); 805 return false; 806 }; 807 808 IITDescriptor D = Infos.front(); 809 Infos = Infos.slice(1); 810 811 switch (D.Kind) { 812 case IITDescriptor::Void: 813 return !Ty->isVoidTy(); 814 case IITDescriptor::VarArg: 815 return true; 816 case IITDescriptor::MMX: { 817 FixedVectorType *VT = dyn_cast<FixedVectorType>(Ty); 818 return !VT || VT->getNumElements() != 1 || 819 !VT->getElementType()->isIntegerTy(64); 820 } 821 case IITDescriptor::AMX: 822 return !Ty->isX86_AMXTy(); 823 case IITDescriptor::Token: 824 return !Ty->isTokenTy(); 825 case IITDescriptor::Metadata: 826 return !Ty->isMetadataTy(); 827 case IITDescriptor::Half: 828 return !Ty->isHalfTy(); 829 case IITDescriptor::BFloat: 830 return !Ty->isBFloatTy(); 831 case IITDescriptor::Float: 832 return !Ty->isFloatTy(); 833 case IITDescriptor::Double: 834 return !Ty->isDoubleTy(); 835 case IITDescriptor::Quad: 836 return !Ty->isFP128Ty(); 837 case IITDescriptor::PPCQuad: 838 return !Ty->isPPC_FP128Ty(); 839 case IITDescriptor::Integer: 840 return !Ty->isIntegerTy(D.Integer_Width); 841 case IITDescriptor::AArch64Svcount: 842 return !isa<TargetExtType>(Ty) || 843 cast<TargetExtType>(Ty)->getName() != "aarch64.svcount"; 844 case IITDescriptor::Vector: { 845 VectorType *VT = dyn_cast<VectorType>(Ty); 846 return !VT || VT->getElementCount() != D.Vector_Width || 847 matchIntrinsicType(VT->getElementType(), Infos, ArgTys, 848 DeferredChecks, IsDeferredCheck); 849 } 850 case IITDescriptor::Pointer: { 851 PointerType *PT = dyn_cast<PointerType>(Ty); 852 return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace; 853 } 854 855 case IITDescriptor::Struct: { 856 StructType *ST = dyn_cast<StructType>(Ty); 857 if (!ST || !ST->isLiteral() || ST->isPacked() || 858 ST->getNumElements() != D.Struct_NumElements) 859 return true; 860 861 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) 862 if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys, 863 DeferredChecks, IsDeferredCheck)) 864 return true; 865 return false; 866 } 867 868 case IITDescriptor::Argument: 869 // If this is the second occurrence of an argument, 870 // verify that the later instance matches the previous instance. 871 if (D.getArgumentNumber() < ArgTys.size()) 872 return Ty != ArgTys[D.getArgumentNumber()]; 873 874 if (D.getArgumentNumber() > ArgTys.size() || 875 D.getArgumentKind() == IITDescriptor::AK_MatchType) 876 return IsDeferredCheck || DeferCheck(Ty); 877 878 assert(D.getArgumentNumber() == ArgTys.size() && !IsDeferredCheck && 879 "Table consistency error"); 880 ArgTys.push_back(Ty); 881 882 switch (D.getArgumentKind()) { 883 case IITDescriptor::AK_Any: 884 return false; // Success 885 case IITDescriptor::AK_AnyInteger: 886 return !Ty->isIntOrIntVectorTy(); 887 case IITDescriptor::AK_AnyFloat: 888 return !Ty->isFPOrFPVectorTy(); 889 case IITDescriptor::AK_AnyVector: 890 return !isa<VectorType>(Ty); 891 case IITDescriptor::AK_AnyPointer: 892 return !isa<PointerType>(Ty); 893 default: 894 break; 895 } 896 llvm_unreachable("all argument kinds not covered"); 897 898 case IITDescriptor::ExtendArgument: { 899 // If this is a forward reference, defer the check for later. 900 if (D.getArgumentNumber() >= ArgTys.size()) 901 return IsDeferredCheck || DeferCheck(Ty); 902 903 Type *NewTy = ArgTys[D.getArgumentNumber()]; 904 if (VectorType *VTy = dyn_cast<VectorType>(NewTy)) 905 NewTy = VectorType::getExtendedElementVectorType(VTy); 906 else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy)) 907 NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth()); 908 else 909 return true; 910 911 return Ty != NewTy; 912 } 913 case IITDescriptor::TruncArgument: { 914 // If this is a forward reference, defer the check for later. 915 if (D.getArgumentNumber() >= ArgTys.size()) 916 return IsDeferredCheck || DeferCheck(Ty); 917 918 Type *NewTy = ArgTys[D.getArgumentNumber()]; 919 if (VectorType *VTy = dyn_cast<VectorType>(NewTy)) 920 NewTy = VectorType::getTruncatedElementVectorType(VTy); 921 else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy)) 922 NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2); 923 else 924 return true; 925 926 return Ty != NewTy; 927 } 928 case IITDescriptor::HalfVecArgument: 929 // If this is a forward reference, defer the check for later. 930 if (D.getArgumentNumber() >= ArgTys.size()) 931 return IsDeferredCheck || DeferCheck(Ty); 932 return !isa<VectorType>(ArgTys[D.getArgumentNumber()]) || 933 VectorType::getHalfElementsVectorType( 934 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty; 935 case IITDescriptor::SameVecWidthArgument: { 936 if (D.getArgumentNumber() >= ArgTys.size()) { 937 // Defer check and subsequent check for the vector element type. 938 Infos = Infos.slice(1); 939 return IsDeferredCheck || DeferCheck(Ty); 940 } 941 auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]); 942 auto *ThisArgType = dyn_cast<VectorType>(Ty); 943 // Both must be vectors of the same number of elements or neither. 944 if ((ReferenceType != nullptr) != (ThisArgType != nullptr)) 945 return true; 946 Type *EltTy = Ty; 947 if (ThisArgType) { 948 if (ReferenceType->getElementCount() != ThisArgType->getElementCount()) 949 return true; 950 EltTy = ThisArgType->getElementType(); 951 } 952 return matchIntrinsicType(EltTy, Infos, ArgTys, DeferredChecks, 953 IsDeferredCheck); 954 } 955 case IITDescriptor::VecOfAnyPtrsToElt: { 956 unsigned RefArgNumber = D.getRefArgNumber(); 957 if (RefArgNumber >= ArgTys.size()) { 958 if (IsDeferredCheck) 959 return true; 960 // If forward referencing, already add the pointer-vector type and 961 // defer the checks for later. 962 ArgTys.push_back(Ty); 963 return DeferCheck(Ty); 964 } 965 966 if (!IsDeferredCheck) { 967 assert(D.getOverloadArgNumber() == ArgTys.size() && 968 "Table consistency error"); 969 ArgTys.push_back(Ty); 970 } 971 972 // Verify the overloaded type "matches" the Ref type. 973 // i.e. Ty is a vector with the same width as Ref. 974 // Composed of pointers to the same element type as Ref. 975 auto *ReferenceType = dyn_cast<VectorType>(ArgTys[RefArgNumber]); 976 auto *ThisArgVecTy = dyn_cast<VectorType>(Ty); 977 if (!ThisArgVecTy || !ReferenceType || 978 (ReferenceType->getElementCount() != ThisArgVecTy->getElementCount())) 979 return true; 980 return !ThisArgVecTy->getElementType()->isPointerTy(); 981 } 982 case IITDescriptor::VecElementArgument: { 983 if (D.getArgumentNumber() >= ArgTys.size()) 984 return IsDeferredCheck ? true : DeferCheck(Ty); 985 auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]); 986 return !ReferenceType || Ty != ReferenceType->getElementType(); 987 } 988 case IITDescriptor::Subdivide2Argument: 989 case IITDescriptor::Subdivide4Argument: { 990 // If this is a forward reference, defer the check for later. 991 if (D.getArgumentNumber() >= ArgTys.size()) 992 return IsDeferredCheck || DeferCheck(Ty); 993 994 Type *NewTy = ArgTys[D.getArgumentNumber()]; 995 if (auto *VTy = dyn_cast<VectorType>(NewTy)) { 996 int SubDivs = D.Kind == IITDescriptor::Subdivide2Argument ? 1 : 2; 997 NewTy = VectorType::getSubdividedVectorType(VTy, SubDivs); 998 return Ty != NewTy; 999 } 1000 return true; 1001 } 1002 case IITDescriptor::VecOfBitcastsToInt: { 1003 if (D.getArgumentNumber() >= ArgTys.size()) 1004 return IsDeferredCheck || DeferCheck(Ty); 1005 auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]); 1006 auto *ThisArgVecTy = dyn_cast<VectorType>(Ty); 1007 if (!ThisArgVecTy || !ReferenceType) 1008 return true; 1009 return ThisArgVecTy != VectorType::getInteger(ReferenceType); 1010 } 1011 } 1012 llvm_unreachable("unhandled"); 1013 } 1014 1015 Intrinsic::MatchIntrinsicTypesResult 1016 Intrinsic::matchIntrinsicSignature(FunctionType *FTy, 1017 ArrayRef<Intrinsic::IITDescriptor> &Infos, 1018 SmallVectorImpl<Type *> &ArgTys) { 1019 SmallVector<DeferredIntrinsicMatchPair, 2> DeferredChecks; 1020 if (matchIntrinsicType(FTy->getReturnType(), Infos, ArgTys, DeferredChecks, 1021 false)) 1022 return MatchIntrinsicTypes_NoMatchRet; 1023 1024 unsigned NumDeferredReturnChecks = DeferredChecks.size(); 1025 1026 for (auto *Ty : FTy->params()) 1027 if (matchIntrinsicType(Ty, Infos, ArgTys, DeferredChecks, false)) 1028 return MatchIntrinsicTypes_NoMatchArg; 1029 1030 for (unsigned I = 0, E = DeferredChecks.size(); I != E; ++I) { 1031 DeferredIntrinsicMatchPair &Check = DeferredChecks[I]; 1032 if (matchIntrinsicType(Check.first, Check.second, ArgTys, DeferredChecks, 1033 true)) 1034 return I < NumDeferredReturnChecks ? MatchIntrinsicTypes_NoMatchRet 1035 : MatchIntrinsicTypes_NoMatchArg; 1036 } 1037 1038 return MatchIntrinsicTypes_Match; 1039 } 1040 1041 bool Intrinsic::matchIntrinsicVarArg( 1042 bool isVarArg, ArrayRef<Intrinsic::IITDescriptor> &Infos) { 1043 // If there are no descriptors left, then it can't be a vararg. 1044 if (Infos.empty()) 1045 return isVarArg; 1046 1047 // There should be only one descriptor remaining at this point. 1048 if (Infos.size() != 1) 1049 return true; 1050 1051 // Check and verify the descriptor. 1052 IITDescriptor D = Infos.front(); 1053 Infos = Infos.slice(1); 1054 if (D.Kind == IITDescriptor::VarArg) 1055 return !isVarArg; 1056 1057 return true; 1058 } 1059 1060 bool Intrinsic::getIntrinsicSignature(Intrinsic::ID ID, FunctionType *FT, 1061 SmallVectorImpl<Type *> &ArgTys) { 1062 if (!ID) 1063 return false; 1064 1065 SmallVector<Intrinsic::IITDescriptor, 8> Table; 1066 getIntrinsicInfoTableEntries(ID, Table); 1067 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table; 1068 1069 if (Intrinsic::matchIntrinsicSignature(FT, TableRef, ArgTys) != 1070 Intrinsic::MatchIntrinsicTypesResult::MatchIntrinsicTypes_Match) { 1071 return false; 1072 } 1073 if (Intrinsic::matchIntrinsicVarArg(FT->isVarArg(), TableRef)) 1074 return false; 1075 return true; 1076 } 1077 1078 bool Intrinsic::getIntrinsicSignature(Function *F, 1079 SmallVectorImpl<Type *> &ArgTys) { 1080 return getIntrinsicSignature(F->getIntrinsicID(), F->getFunctionType(), 1081 ArgTys); 1082 } 1083 1084 std::optional<Function *> Intrinsic::remangleIntrinsicFunction(Function *F) { 1085 SmallVector<Type *, 4> ArgTys; 1086 if (!getIntrinsicSignature(F, ArgTys)) 1087 return std::nullopt; 1088 1089 Intrinsic::ID ID = F->getIntrinsicID(); 1090 StringRef Name = F->getName(); 1091 std::string WantedName = 1092 Intrinsic::getName(ID, ArgTys, F->getParent(), F->getFunctionType()); 1093 if (Name == WantedName) 1094 return std::nullopt; 1095 1096 Function *NewDecl = [&] { 1097 if (auto *ExistingGV = F->getParent()->getNamedValue(WantedName)) { 1098 if (auto *ExistingF = dyn_cast<Function>(ExistingGV)) 1099 if (ExistingF->getFunctionType() == F->getFunctionType()) 1100 return ExistingF; 1101 1102 // The name already exists, but is not a function or has the wrong 1103 // prototype. Make place for the new one by renaming the old version. 1104 // Either this old version will be removed later on or the module is 1105 // invalid and we'll get an error. 1106 ExistingGV->setName(WantedName + ".renamed"); 1107 } 1108 return Intrinsic::getOrInsertDeclaration(F->getParent(), ID, ArgTys); 1109 }(); 1110 1111 NewDecl->setCallingConv(F->getCallingConv()); 1112 assert(NewDecl->getFunctionType() == F->getFunctionType() && 1113 "Shouldn't change the signature"); 1114 return NewDecl; 1115 } 1116