| 1 | //===- RuntimeLibcalls.cpp - Interface for runtime libcalls -----*- 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 | #include "llvm/IR/RuntimeLibcalls.h" |
| 10 | #include "llvm/ADT/FloatingPointMode.h" |
| 11 | #include "llvm/IR/Intrinsics.h" |
| 12 | #include "llvm/IR/Module.h" |
| 13 | #include "llvm/IR/SystemLibraries.h" |
| 14 | #include "llvm/IR/Type.h" |
| 15 | #include "llvm/TargetParser/ARMTargetParser.h" |
| 16 | |
| 17 | #define DEBUG_TYPE "runtime-libcalls-info" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | using namespace RTLIB; |
| 21 | |
| 22 | #define GET_RUNTIME_LIBCALLS_INFO |
| 23 | #define GET_INIT_RUNTIME_LIBCALL_NAMES |
| 24 | #define GET_INIT_RUNTIME_LIBCALL_SIGNATURES |
| 25 | #define GET_RUNTIME_LIBCALL_ATTRIBUTES |
| 26 | #define GET_RUNTIME_LIBCALL_MASKED_VECTOR_LIBCALL_MAPPING |
| 27 | #define GET_SET_TARGET_RUNTIME_LIBCALL_SETS |
| 28 | #define DEFINE_GET_LOOKUP_LIBCALL_IMPL_NAME |
| 29 | #define GET_RUNTIME_LIBCALL_INTRINSIC_TO_LIBCALL |
| 30 | #include "llvm/IR/RuntimeLibcalls.inc" |
| 31 | |
| 32 | static RuntimeLibrary getVectorRuntimeLibrary(VectorLibrary VecLib) { |
| 33 | switch (VecLib) { |
| 34 | case VectorLibrary::NoLibrary: |
| 35 | break; |
| 36 | case VectorLibrary::Accelerate: |
| 37 | return RuntimeLibrary::accelerate; |
| 38 | case VectorLibrary::DarwinLibSystemM: |
| 39 | return RuntimeLibrary::darwin_libsystem_m; |
| 40 | case VectorLibrary::LIBMVEC: |
| 41 | return RuntimeLibrary::libmvec; |
| 42 | case VectorLibrary::MASSV: |
| 43 | return RuntimeLibrary::massv; |
| 44 | case VectorLibrary::SVML: |
| 45 | return RuntimeLibrary::svml; |
| 46 | case VectorLibrary::SLEEFGNUABI: |
| 47 | return RuntimeLibrary::sleefgnuabi; |
| 48 | case VectorLibrary::ArmPL: |
| 49 | return RuntimeLibrary::armpl; |
| 50 | case VectorLibrary::AMDLIBM: |
| 51 | return RuntimeLibrary::amdlibm; |
| 52 | } |
| 53 | |
| 54 | llvm_unreachable("no RuntimeLibrary for VectorLibrary" ); |
| 55 | } |
| 56 | |
| 57 | RuntimeLibcallsInfo::RuntimeLibcallsInfo( |
| 58 | const Triple &TT, ExceptionHandling ExceptionModel, unsigned WCharSize, |
| 59 | FloatABI::ABIType FloatABI, std::optional<LongDoubleFormat> LDblFormat, |
| 60 | StringRef ABIName, VectorLibrary VecLib) |
| 61 | : WCharSize(WCharSize ? WCharSize : TT.getDefaultWCharSize()), |
| 62 | LongDoubleFmt(LDblFormat.value_or(u: TT.getDefaultLongDoubleFormat())), |
| 63 | ABIName(ABIName) { |
| 64 | // Only an unspecified model resolves to the triple default; None is left as |
| 65 | // an explicit disable. |
| 66 | if (ExceptionModel == ExceptionHandling::Default) |
| 67 | ExceptionModel = TT.getDefaultExceptionHandling(); |
| 68 | |
| 69 | initLibcalls(TT, ExceptionModel, FloatABI, ABIName, LongDoubleFormat: LongDoubleFmt); |
| 70 | |
| 71 | if (VecLib != VectorLibrary::NoLibrary) { |
| 72 | setAvailableLibraryLibcalls(Library: getVectorRuntimeLibrary(VecLib), TT, |
| 73 | ExceptionModel, FloatABI, ABIName, |
| 74 | LongDoubleFormat: LongDoubleFmt); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // TODO: Consider the remaining module flags. |
| 79 | RuntimeLibcallsInfo::RuntimeLibcallsInfo(const Module &M, StringRef ABIName, |
| 80 | VectorLibrary VecLib) |
| 81 | : RuntimeLibcallsInfo(M.getTargetTriple(), M.getExceptionModel(), |
| 82 | M.getWCharSize(), M.getFloatABI(), |
| 83 | M.getLongDoubleFormat(), ABIName, VecLib) {} |
| 84 | |
| 85 | RTLIB::Libcall RuntimeLibcallsInfo::selectLongDoubleLibcall( |
| 86 | RTLIB::Libcall F80, RTLIB::Libcall F128, RTLIB::Libcall PPCF128) const { |
| 87 | switch (LongDoubleFmt) { |
| 88 | case LongDoubleFormat::X87DoubleExtended: |
| 89 | return F80; |
| 90 | case LongDoubleFormat::IEEEquad: |
| 91 | return F128; |
| 92 | case LongDoubleFormat::PPCDoubleDouble: |
| 93 | return PPCF128; |
| 94 | case LongDoubleFormat::IEEEsingle: |
| 95 | case LongDoubleFormat::IEEEdouble: |
| 96 | // These long double formats aren't properly handled, but we also don't emit |
| 97 | // calls for them. TODO: We could fix the avr_sin/avr_cos hack if we handled |
| 98 | // the float case. |
| 99 | return RTLIB::UNKNOWN_LIBCALL; |
| 100 | } |
| 101 | |
| 102 | llvm_unreachable("unhandled long double format" ); |
| 103 | } |
| 104 | |
| 105 | bool RuntimeLibcallsInfo::isLibraryAvailable(RuntimeLibrary Library) const { |
| 106 | // TODO: Drive this from module-level state (e.g. the linked runtime). For now |
| 107 | // every named library is reported as available. |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | void RuntimeLibcallsInfo::setTargetRuntimeLibcallSets( |
| 112 | const Triple &TT, ExceptionHandling ExceptionModel, |
| 113 | FloatABI::ABIType FloatABI, StringRef ABIName, |
| 114 | LongDoubleFormat LongDoubleFormat) { |
| 115 | CallingConv::ID DefaultCC = getDefaultLibcallCallingConv( |
| 116 | TT, ExceptionModel, FloatABI, ABIName, LongDoubleFormat); |
| 117 | if (DefaultCC != CallingConv::C) |
| 118 | llvm::fill(Range&: LibcallImplCallingConvs, Value&: DefaultCC); |
| 119 | |
| 120 | for (RTLIB::RuntimeLibrary Library : getDefaultRuntimeLibraries(TT)) { |
| 121 | if (isLibraryAvailable(Library)) |
| 122 | setAvailableLibraryLibcalls(Library, TT, ExceptionModel, FloatABI, |
| 123 | ABIName, LongDoubleFormat); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /// Set default libcall names. If a target wants to opt-out of a libcall it |
| 128 | /// should be placed here. |
| 129 | void RuntimeLibcallsInfo::initLibcalls(const Triple &TT, |
| 130 | ExceptionHandling ExceptionModel, |
| 131 | FloatABI::ABIType FloatABI, |
| 132 | StringRef ABIName, |
| 133 | LongDoubleFormat LongDoubleFormat) { |
| 134 | setTargetRuntimeLibcallSets(TT, ExceptionModel, FloatABI, ABIName, |
| 135 | LongDoubleFormat); |
| 136 | } |
| 137 | |
| 138 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 139 | iota_range<RTLIB::LibcallImpl> |
| 140 | RuntimeLibcallsInfo::libcallImplNameHit(uint16_t NameOffsetEntry, |
| 141 | uint16_t StrOffset) { |
| 142 | int NumAliases = 1; |
| 143 | for (uint16_t Entry : ArrayRef(RuntimeLibcallNameOffsetTable) |
| 144 | .drop_front(N: NameOffsetEntry + 1)) { |
| 145 | if (Entry != StrOffset) |
| 146 | break; |
| 147 | ++NumAliases; |
| 148 | } |
| 149 | |
| 150 | RTLIB::LibcallImpl ImplStart = static_cast<RTLIB::LibcallImpl>( |
| 151 | &RuntimeLibcallNameOffsetTable[NameOffsetEntry] - |
| 152 | &RuntimeLibcallNameOffsetTable[0]); |
| 153 | return enum_seq(Begin: ImplStart, |
| 154 | End: static_cast<RTLIB::LibcallImpl>(ImplStart + NumAliases)); |
| 155 | } |
| 156 | |
| 157 | bool RuntimeLibcallsInfo::isAAPCS_ABI(const Triple &TT, StringRef ABIName) { |
| 158 | const ARM::ARMABI TargetABI = ARM::computeTargetABI(TT, ABIName); |
| 159 | return TargetABI == ARM::ARM_ABI_AAPCS || TargetABI == ARM::ARM_ABI_AAPCS16; |
| 160 | } |
| 161 | |
| 162 | // PowerPC64, Sparc64, SystemZ, VE need signext/zeroext on i32 parameters and |
| 163 | // returns corresponding to C-level ints and unsigned ints. |
| 164 | static bool shouldExtI32(const Triple &T) { |
| 165 | return T.isPPC64() || T.getArch() == Triple::sparcv9 || |
| 166 | T.getArch() == Triple::systemz || T.getArch() == Triple::ve; |
| 167 | } |
| 168 | |
| 169 | // LoongArch, Mips, and riscv64, on the other hand, need signext on i32 |
| 170 | // parameters corresponding to both signed and unsigned ints. |
| 171 | static bool shouldSignExtI32Param(const Triple &T) { |
| 172 | return T.isLoongArch() || T.isMIPS() || T.isRISCV64(); |
| 173 | } |
| 174 | |
| 175 | // LoongArch and riscv64 need signext on i32 returns corresponding to both |
| 176 | // signed and unsigned ints. |
| 177 | static bool shouldSignExtI32Return(const Triple &T) { |
| 178 | return T.isLoongArch() || T.isRISCV64(); |
| 179 | } |
| 180 | |
| 181 | void RuntimeLibcallsInfo::initExtensionsForTriple(bool &ShouldExtI32Param, |
| 182 | bool &ShouldExtI32Return, |
| 183 | bool &ShouldSignExtI32Param, |
| 184 | bool &ShouldSignExtI32Return, |
| 185 | const Triple &T) { |
| 186 | ShouldExtI32Param = ShouldExtI32Return = shouldExtI32(T); |
| 187 | ShouldSignExtI32Param = shouldSignExtI32Param(T); |
| 188 | ShouldSignExtI32Return = shouldSignExtI32Return(T); |
| 189 | } |
| 190 | |
| 191 | static bool shouldZExtBool(const Triple &T) { |
| 192 | return !T.isAArch64() && !T.isOSDarwin(); |
| 193 | } |
| 194 | |
| 195 | void RuntimeLibcallsInfo::initBoolExtensionsForTriple( |
| 196 | bool &ShouldZExtBoolParam, bool &ShouldZExtBoolReturn, const Triple &T) { |
| 197 | ShouldZExtBoolParam = ShouldZExtBoolReturn = shouldZExtBool(T); |
| 198 | } |
| 199 | |
| 200 | /// TODO: There is really no guarantee that sizeof(size_t) is equal to the index |
| 201 | /// size of the default address space. This matches TargetLibraryInfo and should |
| 202 | /// be kept in sync. |
| 203 | static IntegerType *getSizeTType(LLVMContext &Ctx, const DataLayout &DL) { |
| 204 | return DL.getIndexType(C&: Ctx, /*AddressSpace=*/0); |
| 205 | } |
| 206 | |
| 207 | static unsigned getIntSize(const Triple &TT) { |
| 208 | return RuntimeLibcallsInfo::getIntSize(TT); |
| 209 | } |
| 210 | |
| 211 | static unsigned getLongSize(const Triple &TT, const DataLayout &DL) { |
| 212 | if (TT.isOSWindowsOrUEFI() && !TT.isWindowsCygwinEnvironment()) |
| 213 | return 32; |
| 214 | return std::max(a: 32u, b: DL.getPointerSizeInBits(/*AS=*/0)); |
| 215 | } |
| 216 | |
| 217 | // TODO: NoCallback probably unsafe in general |
| 218 | static constexpr Attribute::AttrKind CommonFnAttrs[] = { |
| 219 | Attribute::NoCallback, Attribute::NoFree, Attribute::NoSync, |
| 220 | Attribute::NoUnwind, Attribute::WillReturn}; |
| 221 | |
| 222 | static Type *getLongDoubleType(LLVMContext &Ctx, LongDoubleFormat Format) { |
| 223 | switch (Format) { |
| 224 | case LongDoubleFormat::IEEEsingle: |
| 225 | return Type::getFloatTy(C&: Ctx); |
| 226 | case LongDoubleFormat::IEEEdouble: |
| 227 | return Type::getDoubleTy(C&: Ctx); |
| 228 | case LongDoubleFormat::X87DoubleExtended: |
| 229 | return Type::getX86_FP80Ty(C&: Ctx); |
| 230 | case LongDoubleFormat::IEEEquad: |
| 231 | return Type::getFP128Ty(C&: Ctx); |
| 232 | case LongDoubleFormat::PPCDoubleDouble: |
| 233 | return Type::getPPC_FP128Ty(C&: Ctx); |
| 234 | } |
| 235 | |
| 236 | llvm_unreachable("unhandled long double format" ); |
| 237 | } |
| 238 | |
| 239 | static Type *getInt128Type(LLVMContext &Ctx, const Triple &TT) { |
| 240 | if (TT.isOSWindowsOrUEFI() && TT.isX86_64()) |
| 241 | return FixedVectorType::get(ElementType: Type::getInt64Ty(C&: Ctx), NumElts: 2); |
| 242 | return Type::getInt128Ty(C&: Ctx); |
| 243 | } |
| 244 | |
| 245 | static Type *convertToIRType(FuncArgTypeID ID, LLVMContext &Ctx, |
| 246 | const Triple &TT, const DataLayout &DL, |
| 247 | unsigned WCharSize, LongDoubleFormat LdblFormat) { |
| 248 | // FIXME: Use the llvm/ABI library to get accurate IR types and attributes. |
| 249 | switch (ID) { |
| 250 | case Void: |
| 251 | return Type::getVoidTy(C&: Ctx); |
| 252 | case Bool: |
| 253 | return IntegerType::get(C&: Ctx, NumBits: 1); |
| 254 | case Int8: |
| 255 | case UInt8: |
| 256 | return IntegerType::get(C&: Ctx, NumBits: 8); |
| 257 | case Int16: |
| 258 | case UInt16: |
| 259 | return IntegerType::get(C&: Ctx, NumBits: 16); |
| 260 | case Int32: |
| 261 | case UInt32: |
| 262 | return IntegerType::get(C&: Ctx, NumBits: 32); |
| 263 | case Int: |
| 264 | case UInt: |
| 265 | case IntPlus: |
| 266 | case UIntPlus: |
| 267 | case IntX: |
| 268 | case UIntX: |
| 269 | return IntegerType::get(C&: Ctx, NumBits: getIntSize(TT)); |
| 270 | case Long: |
| 271 | case ULong: |
| 272 | return IntegerType::get(C&: Ctx, NumBits: getLongSize(TT, DL)); |
| 273 | case Int64: |
| 274 | case UInt64: |
| 275 | case LLong: |
| 276 | case ULLong: |
| 277 | return IntegerType::get(C&: Ctx, NumBits: 64); |
| 278 | case Int128: |
| 279 | case UInt128: |
| 280 | return getInt128Type(Ctx, TT); |
| 281 | case SizeT: |
| 282 | case SSizeT: |
| 283 | return getSizeTType(Ctx, DL); |
| 284 | case CmpResult: |
| 285 | if (TT.isAArch64() || TT.isAMDGPU() || TT.isWasm()) |
| 286 | return Type::getInt32Ty(C&: Ctx); |
| 287 | if (TT.isAVR()) |
| 288 | return Type::getInt8Ty(C&: Ctx); |
| 289 | return IntegerType::get(C&: Ctx, NumBits: TT.getArchPointerBitWidth()); |
| 290 | case Half: |
| 291 | return Type::getHalfTy(C&: Ctx); |
| 292 | case BFloat: |
| 293 | return Type::getBFloatTy(C&: Ctx); |
| 294 | case Flt: |
| 295 | case Floating: |
| 296 | return Type::getFloatTy(C&: Ctx); |
| 297 | case Dbl: |
| 298 | return Type::getDoubleTy(C&: Ctx); |
| 299 | case LDbl: |
| 300 | return getLongDoubleType(Ctx, Format: LdblFormat); |
| 301 | case X86FP80: |
| 302 | return Type::getX86_FP80Ty(C&: Ctx); |
| 303 | case FP128: |
| 304 | return Type::getFP128Ty(C&: Ctx); |
| 305 | case PPCFP128: |
| 306 | return Type::getPPC_FP128Ty(C&: Ctx); |
| 307 | case Ptr: |
| 308 | return PointerType::get(C&: Ctx, AddressSpace: 0); |
| 309 | case AllocaPtr: |
| 310 | return DL.getAllocaPtrType(Ctx); |
| 311 | case V2F32: |
| 312 | return FixedVectorType::get(ElementType: Type::getFloatTy(C&: Ctx), NumElts: 2); |
| 313 | case V4F32: |
| 314 | return FixedVectorType::get(ElementType: Type::getFloatTy(C&: Ctx), NumElts: 4); |
| 315 | case V8F32: |
| 316 | return FixedVectorType::get(ElementType: Type::getFloatTy(C&: Ctx), NumElts: 8); |
| 317 | case V16F32: |
| 318 | return FixedVectorType::get(ElementType: Type::getFloatTy(C&: Ctx), NumElts: 16); |
| 319 | case NXV4F32: |
| 320 | return ScalableVectorType::get(ElementType: Type::getFloatTy(C&: Ctx), MinNumElts: 4); |
| 321 | case V2F64: |
| 322 | return FixedVectorType::get(ElementType: Type::getDoubleTy(C&: Ctx), NumElts: 2); |
| 323 | case V4F64: |
| 324 | return FixedVectorType::get(ElementType: Type::getDoubleTy(C&: Ctx), NumElts: 4); |
| 325 | case V8F64: |
| 326 | return FixedVectorType::get(ElementType: Type::getDoubleTy(C&: Ctx), NumElts: 8); |
| 327 | case NXV2F64: |
| 328 | return ScalableVectorType::get(ElementType: Type::getDoubleTy(C&: Ctx), MinNumElts: 2); |
| 329 | case NXV2I1: |
| 330 | return ScalableVectorType::get(ElementType: Type::getInt1Ty(C&: Ctx), MinNumElts: 2); |
| 331 | case NXV4I1: |
| 332 | return ScalableVectorType::get(ElementType: Type::getInt1Ty(C&: Ctx), MinNumElts: 4); |
| 333 | case V2I32: |
| 334 | return FixedVectorType::get(ElementType: Type::getInt32Ty(C&: Ctx), NumElts: 2); |
| 335 | case V4I32: |
| 336 | return FixedVectorType::get(ElementType: Type::getInt32Ty(C&: Ctx), NumElts: 4); |
| 337 | case V8I32: |
| 338 | return FixedVectorType::get(ElementType: Type::getInt32Ty(C&: Ctx), NumElts: 8); |
| 339 | case V16I32: |
| 340 | return FixedVectorType::get(ElementType: Type::getInt32Ty(C&: Ctx), NumElts: 16); |
| 341 | case NXV2I32: |
| 342 | return ScalableVectorType::get(ElementType: Type::getInt32Ty(C&: Ctx), MinNumElts: 2); |
| 343 | case NXV4I32: |
| 344 | return ScalableVectorType::get(ElementType: Type::getInt32Ty(C&: Ctx), MinNumElts: 4); |
| 345 | default: |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | ArrayRef<FuncArgTypeID> EltIDs = getStructElementTypes(ID); |
| 350 | if (EltIDs.empty()) |
| 351 | return nullptr; |
| 352 | |
| 353 | SmallVector<Type *, 2> EltTys; |
| 354 | for (FuncArgTypeID EltID : EltIDs) |
| 355 | EltTys.push_back( |
| 356 | Elt: convertToIRType(ID: EltID, Ctx, TT, DL, WCharSize, LdblFormat)); |
| 357 | return StructType::get(Context&: Ctx, Elements: EltTys); |
| 358 | } |
| 359 | |
| 360 | static std::optional<bool> isSignedIntArgType(FuncArgTypeID ID) { |
| 361 | switch (ID) { |
| 362 | case Int8: |
| 363 | case Int16: |
| 364 | case Int32: |
| 365 | case Int: |
| 366 | case IntPlus: |
| 367 | case Long: |
| 368 | case Int64: |
| 369 | case LLong: |
| 370 | case Int128: |
| 371 | case SSizeT: |
| 372 | return true; |
| 373 | case UInt8: |
| 374 | case UInt16: |
| 375 | case UInt32: |
| 376 | case UInt: |
| 377 | case UIntPlus: |
| 378 | case ULong: |
| 379 | case UIntX: |
| 380 | case UInt64: |
| 381 | case ULLong: |
| 382 | case UInt128: |
| 383 | case SizeT: |
| 384 | return false; |
| 385 | default: |
| 386 | return std::nullopt; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | static Attribute::AttrKind getIntExtAttr(FuncArgTypeID ID, Type *Ty, |
| 391 | const Triple &TT, bool IsReturn) { |
| 392 | if (ID == Bool) |
| 393 | return shouldZExtBool(T: TT) ? Attribute::ZExt : Attribute::None; |
| 394 | |
| 395 | std::optional<bool> Signed = isSignedIntArgType(ID); |
| 396 | if (!Signed || !Ty->isIntegerTy()) |
| 397 | return Attribute::None; |
| 398 | |
| 399 | // All targets currently extend types narrower than int according to their |
| 400 | // signedness, but this could become target dependent if a target needs it. |
| 401 | unsigned Bits = Ty->getIntegerBitWidth(); |
| 402 | unsigned IntBits = getIntSize(TT); |
| 403 | |
| 404 | if (Bits < IntBits) |
| 405 | return *Signed ? Attribute::SExt : Attribute::ZExt; |
| 406 | if (Bits != 32 || IntBits != 32) |
| 407 | return Attribute::None; |
| 408 | if (shouldExtI32(T: TT)) |
| 409 | return *Signed ? Attribute::SExt : Attribute::ZExt; |
| 410 | if (IsReturn ? shouldSignExtI32Return(T: TT) : shouldSignExtI32Param(T: TT)) |
| 411 | return Attribute::SExt; |
| 412 | return Attribute::None; |
| 413 | } |
| 414 | |
| 415 | static FuncArgTypeID getCPairElementType(FuncArgTypeID ID) { |
| 416 | switch (ID) { |
| 417 | case CFltPair: |
| 418 | return Flt; |
| 419 | case CDblPair: |
| 420 | return Dbl; |
| 421 | default: |
| 422 | return NoFuncArgType; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | std::pair<Type *, Type *> |
| 427 | RuntimeLibcallsInfo::lowerCPairReturn(const Triple &TT, Type *EltTy) const { |
| 428 | Type *PairTy = StructType::get(elt1: EltTy, elts: EltTy); |
| 429 | // These return small structs in memory. |
| 430 | if (TT.isX86_32() || |
| 431 | ((TT.isARM() || TT.isThumb()) && !isAAPCS_ABI(TT, ABIName))) |
| 432 | return {Type::getVoidTy(C&: EltTy->getContext()), PairTy}; |
| 433 | // x86-64 SysV returns a pair of floats packed in one SSE register. |
| 434 | if (TT.isX86_64() && !TT.isOSWindows() && EltTy->isFloatTy()) |
| 435 | return {FixedVectorType::get(ElementType: EltTy, NumElts: 2), nullptr}; |
| 436 | return {PairTy, nullptr}; |
| 437 | } |
| 438 | |
| 439 | std::pair<FunctionType *, AttributeList> |
| 440 | RuntimeLibcallsInfo::getDefaultFunctionTy( |
| 441 | LLVMContext &Ctx, const Triple &TT, const DataLayout &DL, |
| 442 | RTLIB::LibcallImpl LibcallImpl) const { |
| 443 | const FuncArgTypeID *ProtoTypes = |
| 444 | &SignatureTable[SignatureOffset[LibcallImpl]]; |
| 445 | |
| 446 | if (ProtoTypes[0] == NoFuncArgType || ProtoTypes[0] == Variable || |
| 447 | ProtoTypes[0] == ThreadLocalVariable) |
| 448 | return {}; |
| 449 | |
| 450 | FuncArgTypeID LastID = ProtoTypes[0]; |
| 451 | SmallVector<Type *, 4> ArgTys; |
| 452 | SmallVector<AttributeSet, 4> ArgAttrs; |
| 453 | Type *RetTy; |
| 454 | AttributeSet RetAttrs; |
| 455 | if (FuncArgTypeID EltID = getCPairElementType(ID: LastID); |
| 456 | EltID != NoFuncArgType) { |
| 457 | Type *EltTy = convertToIRType(ID: EltID, Ctx, TT, DL, WCharSize, LdblFormat: LongDoubleFmt); |
| 458 | Type *SRetTy; |
| 459 | std::tie(args&: RetTy, args&: SRetTy) = lowerCPairReturn(TT, EltTy); |
| 460 | if (SRetTy) { |
| 461 | AttrBuilder SRetAttrs(Ctx); |
| 462 | SRetAttrs.addStructRetAttr(Ty: SRetTy); |
| 463 | SRetAttrs.addAlignmentAttr(Align: DL.getABITypeAlign(Ty: SRetTy)); |
| 464 | ArgTys.push_back(Elt: DL.getAllocaPtrType(Ctx)); |
| 465 | ArgAttrs.push_back(Elt: AttributeSet::get(C&: Ctx, B: SRetAttrs)); |
| 466 | } |
| 467 | } else { |
| 468 | RetTy = convertToIRType(ID: LastID, Ctx, TT, DL, WCharSize, LdblFormat: LongDoubleFmt); |
| 469 | if (Attribute::AttrKind AK = |
| 470 | getIntExtAttr(ID: LastID, Ty: RetTy, TT, /*IsReturn=*/true)) |
| 471 | RetAttrs = AttributeSet::get(C&: Ctx, Attrs: Attribute::get(Context&: Ctx, Kind: AK)); |
| 472 | if (isInRegStruct(ID: LastID)) |
| 473 | RetAttrs = RetAttrs.addAttribute(C&: Ctx, Kind: Attribute::InReg); |
| 474 | } |
| 475 | RetAttrs = RetAttrs.addAttributes( |
| 476 | C&: Ctx, AS: getLibcallImplRetAttributes(C&: Ctx, Impl: LibcallImpl)); |
| 477 | |
| 478 | unsigned FirstArgNo = ArgTys.size(); |
| 479 | bool IsVarArg = false; |
| 480 | unsigned Idx = 1; |
| 481 | for (FuncArgTypeID TyID = ProtoTypes[Idx]; TyID != NoFuncArgType; |
| 482 | TyID = ProtoTypes[++Idx]) { |
| 483 | if (TyID == Ellip) { |
| 484 | // The ellipsis ends the protoype list so it must be followed by |
| 485 | // NoFuncArgType. |
| 486 | assert(ProtoTypes[Idx + 1] == NoFuncArgType); |
| 487 | IsVarArg = true; |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | if (TyID == Same) |
| 492 | TyID = LastID; |
| 493 | else |
| 494 | LastID = TyID; |
| 495 | |
| 496 | Type *ArgTy = convertToIRType(ID: TyID, Ctx, TT, DL, WCharSize, LdblFormat: LongDoubleFmt); |
| 497 | ArgTys.push_back(Elt: ArgTy); |
| 498 | |
| 499 | AttributeSet ArgAttrSet; |
| 500 | if (Attribute::AttrKind AK = |
| 501 | getIntExtAttr(ID: TyID, Ty: ArgTy, TT, /*IsReturn=*/false)) |
| 502 | ArgAttrSet = AttributeSet::get(C&: Ctx, Attrs: Attribute::get(Context&: Ctx, Kind: AK)); |
| 503 | ArgAttrs.push_back(Elt: ArgAttrSet); |
| 504 | } |
| 505 | |
| 506 | for (const LibcallArgAttrs &Static : |
| 507 | getLibcallImplArgAttributes(Impl: LibcallImpl)) { |
| 508 | assert(FirstArgNo + Static.ArgNo < ArgAttrs.size() && |
| 509 | "attributes for nonexistent argument" ); |
| 510 | AttributeSet StaticAttrs = getLibcallArgAttributeSet(C&: Ctx, ID: Static.AttrSetID); |
| 511 | if (Static.PointeeType != NoFuncArgType) { |
| 512 | Type *PointeeTy = convertToIRType(ID: Static.PointeeType, Ctx, TT, DL, |
| 513 | WCharSize, LdblFormat: LongDoubleFmt); |
| 514 | Type *AlignTy = |
| 515 | Static.AlignToElement ? PointeeTy->getScalarType() : PointeeTy; |
| 516 | SmallVector<Attribute, 2> PointeeAttrs = { |
| 517 | Attribute::getWithAlignment(Context&: Ctx, Alignment: DL.getABITypeAlign(Ty: AlignTy))}; |
| 518 | TypeSize Size = DL.getTypeStoreSize(Ty: PointeeTy); |
| 519 | if (!Size.isScalable()) { |
| 520 | PointeeAttrs.push_back( |
| 521 | Elt: Attribute::getWithDereferenceableBytes(Context&: Ctx, Bytes: Size.getFixedValue())); |
| 522 | } |
| 523 | |
| 524 | StaticAttrs = StaticAttrs.addSortedAttributes(C&: Ctx, Attrs: PointeeAttrs); |
| 525 | } |
| 526 | |
| 527 | AttributeSet &ArgAttrSet = ArgAttrs[FirstArgNo + Static.ArgNo]; |
| 528 | ArgAttrSet = ArgAttrSet.addAttributes(C&: Ctx, AS: StaticAttrs); |
| 529 | } |
| 530 | |
| 531 | return {FunctionType::get(Result: RetTy, Params: ArgTys, isVarArg: IsVarArg), |
| 532 | AttributeList::get(C&: Ctx, FnAttrs: getLibcallImplFnAttributes(C&: Ctx, Impl: LibcallImpl), |
| 533 | RetAttrs, ArgAttrs)}; |
| 534 | } |
| 535 | |
| 536 | Type * |
| 537 | RuntimeLibcallsInfo::getVariableType(LLVMContext &Ctx, const Triple &TT, |
| 538 | const DataLayout &DL, |
| 539 | RTLIB::LibcallImpl LibcallImpl) const { |
| 540 | const FuncArgTypeID *ProtoTypes = |
| 541 | &SignatureTable[SignatureOffset[LibcallImpl]]; |
| 542 | if (ProtoTypes[0] != Variable && ProtoTypes[0] != ThreadLocalVariable) |
| 543 | return nullptr; |
| 544 | return convertToIRType(ID: ProtoTypes[1], Ctx, TT, DL, WCharSize, LdblFormat: LongDoubleFmt); |
| 545 | } |
| 546 | |
| 547 | FunctionCallee RuntimeLibcallsInfo::getOrInsertLibcallDecl( |
| 548 | Module &M, RTLIB::LibcallImpl LibcallImpl) const { |
| 549 | assert(!isVariable(LibcallImpl) && "not a function declaration" ); |
| 550 | |
| 551 | FunctionType *FuncTy; |
| 552 | AttributeList FuncAttrs; |
| 553 | std::tie(args&: FuncTy, args&: FuncAttrs) = getFunctionTy( |
| 554 | Ctx&: M.getContext(), TT: M.getTargetTriple(), DL: M.getDataLayout(), LibcallImpl); |
| 555 | |
| 556 | // TODO: Every libcall should have a known signature. |
| 557 | if (!FuncTy) { |
| 558 | FuncTy = FunctionType::get(Result: Type::getVoidTy(C&: M.getContext()), Params: {}, |
| 559 | /*IsVarArg=*/isVarArg: true); |
| 560 | } |
| 561 | |
| 562 | return M.getOrInsertFunction( |
| 563 | Name: getLibcallImplName(CallImpl: LibcallImpl), T: FuncTy, InitCallback: [&](Function &F) { |
| 564 | F.setAttributes(FuncAttrs); |
| 565 | F.setCallingConv(getLibcallImplCallingConv(Call: LibcallImpl)); |
| 566 | }); |
| 567 | } |
| 568 | |
| 569 | GlobalValue *RuntimeLibcallsInfo::getOrInsertImplGlobal( |
| 570 | Module &M, RTLIB::LibcallImpl LibcallImpl) const { |
| 571 | assert(isVariable(LibcallImpl) && "not a variable declaration" ); |
| 572 | |
| 573 | StringRef Name = getLibcallImplName(CallImpl: LibcallImpl); |
| 574 | if (GlobalValue *GV = M.getNamedValue(Name)) |
| 575 | return GV; |
| 576 | |
| 577 | Type *VarTy = getVariableType(Ctx&: M.getContext(), TT: M.getTargetTriple(), |
| 578 | DL: M.getDataLayout(), LibcallImpl); |
| 579 | return new GlobalVariable( |
| 580 | M, VarTy, /*isConstant=*/false, GlobalValue::ExternalLinkage, nullptr, |
| 581 | Name, nullptr, |
| 582 | isThreadLocalVariable(LibcallImpl) ? GlobalValue::InitialExecTLSModel |
| 583 | : GlobalValue::NotThreadLocal); |
| 584 | } |
| 585 | |
| 586 | std::pair<FunctionType *, AttributeList> |
| 587 | RuntimeLibcallsInfo::getFunctionTy(LLVMContext &Ctx, const Triple &TT, |
| 588 | const DataLayout &DL, |
| 589 | RTLIB::LibcallImpl LibcallImpl) const { |
| 590 | switch (LibcallImpl) { |
| 591 | // FIXME: Should also include __sincospi[f]_stret |
| 592 | case RTLIB::impl___sincos_stret: |
| 593 | case RTLIB::impl___sincosf_stret: { |
| 594 | auto [FuncTy, Attrs] = getDefaultFunctionTy(Ctx, TT, DL, LibcallImpl); |
| 595 | bool UseSRet = FuncTy->getNumParams() != 0 && |
| 596 | Attrs.hasParamAttr(ArgNo: 0, Kind: Attribute::StructRet); |
| 597 | AttrBuilder FuncAttrBuilder(Ctx); |
| 598 | for (Attribute::AttrKind Attr : CommonFnAttrs) |
| 599 | FuncAttrBuilder.addAttribute(Val: Attr); |
| 600 | FuncAttrBuilder.addMemoryAttr(ME: MemoryEffects::argMemOnly( |
| 601 | MR: UseSRet ? ModRefInfo::Mod : ModRefInfo::NoModRef)); |
| 602 | return {FuncTy, Attrs.addFnAttributes(C&: Ctx, B: FuncAttrBuilder)}; |
| 603 | } |
| 604 | default: |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | return getDefaultFunctionTy(Ctx, TT, DL, LibcallImpl); |
| 609 | } |
| 610 | |