| 1 | //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==// |
| 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 the TargetLibraryInfo class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 14 | #include "llvm/ADT/DenseMap.h" |
| 15 | #include "llvm/ADT/SmallString.h" |
| 16 | #include "llvm/ADT/StringTable.h" |
| 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/Module.h" |
| 19 | #include "llvm/IR/SystemLibraries.h" |
| 20 | #include "llvm/InitializePasses.h" |
| 21 | #include "llvm/TargetParser/Triple.h" |
| 22 | using namespace llvm; |
| 23 | |
| 24 | #define GET_TARGET_LIBRARY_INFO_STRING_TABLE |
| 25 | #include "llvm/Analysis/TargetLibraryInfo.inc" |
| 26 | |
| 27 | std::string VecDesc::getVectorFunctionABIVariantString() const { |
| 28 | assert(!VectorFnName.empty() && "Vector function name must not be empty." ); |
| 29 | SmallString<256> Buffer; |
| 30 | llvm::raw_svector_ostream Out(Buffer); |
| 31 | Out << VABIPrefix << "_" << ScalarFnName << "(" << VectorFnName << ")" ; |
| 32 | return std::string(Out.str()); |
| 33 | } |
| 34 | |
| 35 | #define GET_TARGET_LIBRARY_INFO_SIGNATURE_TABLE |
| 36 | #include "llvm/Analysis/TargetLibraryInfo.inc" |
| 37 | |
| 38 | static bool hasSinCosPiStret(const Triple &T) { |
| 39 | // Only Darwin variants have _stret versions of combined trig functions. |
| 40 | if (!T.isOSDarwin()) |
| 41 | return false; |
| 42 | |
| 43 | // The ABI is rather complicated on x86, so don't do anything special there. |
| 44 | if (T.getArch() == Triple::x86) |
| 45 | return false; |
| 46 | |
| 47 | if (T.isMacOSX() && T.isMacOSXVersionLT(Major: 10, Minor: 9)) |
| 48 | return false; |
| 49 | |
| 50 | if (T.isiOS() && T.isOSVersionLT(Major: 7, Minor: 0)) |
| 51 | return false; |
| 52 | |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | static bool hasBcmp(const Triple &TT) { |
| 57 | // Posix removed support from bcmp() in 2001, but the glibc and several |
| 58 | // implementations of the libc still have it. |
| 59 | if (TT.isOSLinux()) |
| 60 | return TT.isGNUEnvironment() || TT.isMusl(); |
| 61 | // Both NetBSD and OpenBSD are planning to remove the function. Windows does |
| 62 | // not have it. |
| 63 | return TT.isOSFreeBSD() || TT.isOSSolaris(); |
| 64 | } |
| 65 | |
| 66 | static bool isCallingConvCCompatible(CallingConv::ID CC, const Triple &TT, |
| 67 | FunctionType *FuncTy) { |
| 68 | switch (CC) { |
| 69 | default: |
| 70 | return false; |
| 71 | case llvm::CallingConv::C: |
| 72 | return true; |
| 73 | case llvm::CallingConv::ARM_APCS: |
| 74 | case llvm::CallingConv::ARM_AAPCS: |
| 75 | case llvm::CallingConv::ARM_AAPCS_VFP: { |
| 76 | |
| 77 | // The iOS ABI diverges from the standard in some cases, so for now don't |
| 78 | // try to simplify those calls. |
| 79 | if (TT.isiOS()) |
| 80 | return false; |
| 81 | |
| 82 | if (!FuncTy->getReturnType()->isPointerTy() && |
| 83 | !FuncTy->getReturnType()->isIntegerTy() && |
| 84 | !FuncTy->getReturnType()->isVoidTy()) |
| 85 | return false; |
| 86 | |
| 87 | for (auto *Param : FuncTy->params()) { |
| 88 | if (!Param->isPointerTy() && !Param->isIntegerTy()) |
| 89 | return false; |
| 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | bool TargetLibraryInfoImpl::isCallingConvCCompatible(CallBase *CI) { |
| 98 | return ::isCallingConvCCompatible(CC: CI->getCallingConv(), |
| 99 | TT: CI->getModule()->getTargetTriple(), |
| 100 | FuncTy: CI->getFunctionType()); |
| 101 | } |
| 102 | |
| 103 | bool TargetLibraryInfoImpl::isCallingConvCCompatible(Function *F) { |
| 104 | return ::isCallingConvCCompatible(CC: F->getCallingConv(), |
| 105 | TT: F->getParent()->getTargetTriple(), |
| 106 | FuncTy: F->getFunctionType()); |
| 107 | } |
| 108 | |
| 109 | static void initializeBase(TargetLibraryInfoImpl &TLI, const Triple &T) { |
| 110 | bool ShouldExtI32Param, ShouldExtI32Return; |
| 111 | bool ShouldSignExtI32Param, ShouldSignExtI32Return; |
| 112 | TargetLibraryInfo::initExtensionsForTriple( |
| 113 | ShouldExtI32Param, ShouldExtI32Return, ShouldSignExtI32Param, |
| 114 | ShouldSignExtI32Return, T); |
| 115 | TLI.setShouldExtI32Param(ShouldExtI32Param); |
| 116 | TLI.setShouldExtI32Return(ShouldExtI32Return); |
| 117 | TLI.setShouldSignExtI32Param(ShouldSignExtI32Param); |
| 118 | TLI.setShouldSignExtI32Return(ShouldSignExtI32Return); |
| 119 | |
| 120 | // Let's assume by default that the size of int is 32 bits, unless the target |
| 121 | // is a 16-bit architecture because then it most likely is 16 bits. If that |
| 122 | // isn't true for a target those defaults should be overridden below. |
| 123 | TLI.setIntSize(T.isArch16Bit() ? 16 : 32); |
| 124 | } |
| 125 | |
| 126 | /// Initialize the set of available library functions based on the specified |
| 127 | /// target triple. This should be carefully written so that a missing target |
| 128 | /// triple gets a sane set of defaults. |
| 129 | static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T, |
| 130 | const llvm::StringTable &StandardNames, |
| 131 | VectorLibrary VecLib) { |
| 132 | // Set IO unlocked variants as unavailable |
| 133 | // Set them as available per system below |
| 134 | TLI.setUnavailable(LibFunc_getc_unlocked); |
| 135 | TLI.setUnavailable(LibFunc_getchar_unlocked); |
| 136 | TLI.setUnavailable(LibFunc_putc_unlocked); |
| 137 | TLI.setUnavailable(LibFunc_putchar_unlocked); |
| 138 | TLI.setUnavailable(LibFunc_fputc_unlocked); |
| 139 | TLI.setUnavailable(LibFunc_fgetc_unlocked); |
| 140 | TLI.setUnavailable(LibFunc_fread_unlocked); |
| 141 | TLI.setUnavailable(LibFunc_fwrite_unlocked); |
| 142 | TLI.setUnavailable(LibFunc_fputs_unlocked); |
| 143 | TLI.setUnavailable(LibFunc_fgets_unlocked); |
| 144 | |
| 145 | // There is really no runtime library on AMDGPU, apart from |
| 146 | // __kmpc_alloc/free_shared. |
| 147 | if (T.isAMDGPU()) { |
| 148 | TLI.disableAllFunctions(); |
| 149 | TLI.setAvailable(llvm::LibFunc___kmpc_alloc_shared); |
| 150 | TLI.setAvailable(llvm::LibFunc___kmpc_free_shared); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | // DXIL does not support libcalls, and disabling them here prevents a number |
| 155 | // of passes from introducing libcalls into DXIL which would otherwise |
| 156 | // complicate lowering/legalization |
| 157 | if (T.isDXIL()) { |
| 158 | TLI.disableAllFunctions(); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | // memset_pattern{4,8,16} is only available on iOS 3.0 and Mac OS X 10.5 and |
| 163 | // later. All versions of watchOS support it. |
| 164 | if (T.isMacOSX()) { |
| 165 | // available IO unlocked variants on Mac OS X |
| 166 | TLI.setAvailable(LibFunc_getc_unlocked); |
| 167 | TLI.setAvailable(LibFunc_getchar_unlocked); |
| 168 | TLI.setAvailable(LibFunc_putc_unlocked); |
| 169 | TLI.setAvailable(LibFunc_putchar_unlocked); |
| 170 | TLI.setUnavailable(LibFunc_memrchr); |
| 171 | |
| 172 | if (T.isMacOSXVersionLT(Major: 10, Minor: 5)) { |
| 173 | TLI.setUnavailable(LibFunc_memset_pattern4); |
| 174 | TLI.setUnavailable(LibFunc_memset_pattern8); |
| 175 | TLI.setUnavailable(LibFunc_memset_pattern16); |
| 176 | } |
| 177 | } else if (T.isiOS()) { |
| 178 | if (T.isOSVersionLT(Major: 3, Minor: 0)) { |
| 179 | TLI.setUnavailable(LibFunc_memset_pattern4); |
| 180 | TLI.setUnavailable(LibFunc_memset_pattern8); |
| 181 | TLI.setUnavailable(LibFunc_memset_pattern16); |
| 182 | } |
| 183 | } else if (!T.isWatchOS()) { |
| 184 | TLI.setUnavailable(LibFunc_memset_pattern4); |
| 185 | TLI.setUnavailable(LibFunc_memset_pattern8); |
| 186 | TLI.setUnavailable(LibFunc_memset_pattern16); |
| 187 | } |
| 188 | |
| 189 | if (!hasSinCosPiStret(T)) { |
| 190 | TLI.setUnavailable(LibFunc_sinpi); |
| 191 | TLI.setUnavailable(LibFunc_sinpif); |
| 192 | TLI.setUnavailable(LibFunc_cospi); |
| 193 | TLI.setUnavailable(LibFunc_cospif); |
| 194 | TLI.setUnavailable(LibFunc_sincospi_stret); |
| 195 | TLI.setUnavailable(LibFunc_sincospif_stret); |
| 196 | } |
| 197 | |
| 198 | if (!hasBcmp(TT: T)) |
| 199 | TLI.setUnavailable(LibFunc_bcmp); |
| 200 | |
| 201 | if (T.isMacOSX() && T.getArch() == Triple::x86 && |
| 202 | !T.isMacOSXVersionLT(Major: 10, Minor: 7)) { |
| 203 | // x86-32 OSX has a scheme where fwrite and fputs (and some other functions |
| 204 | // we don't care about) have two versions; on recent OSX, the one we want |
| 205 | // has a $UNIX2003 suffix. The two implementations are identical except |
| 206 | // for the return value in some edge cases. However, we don't want to |
| 207 | // generate code that depends on the old symbols. |
| 208 | TLI.setAvailableWithName(F: LibFunc_fwrite, Name: "fwrite$UNIX2003" ); |
| 209 | TLI.setAvailableWithName(F: LibFunc_fputs, Name: "fputs$UNIX2003" ); |
| 210 | } |
| 211 | |
| 212 | // iprintf and friends are only available on XCore, TCE, and Emscripten. |
| 213 | if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce && |
| 214 | T.getOS() != Triple::Emscripten) { |
| 215 | TLI.setUnavailable(LibFunc_iprintf); |
| 216 | TLI.setUnavailable(LibFunc_siprintf); |
| 217 | TLI.setUnavailable(LibFunc_fiprintf); |
| 218 | } |
| 219 | |
| 220 | // __small_printf and friends are only available on Emscripten. |
| 221 | if (T.getOS() != Triple::Emscripten) { |
| 222 | TLI.setUnavailable(LibFunc_small_printf); |
| 223 | TLI.setUnavailable(LibFunc_small_sprintf); |
| 224 | TLI.setUnavailable(LibFunc_small_fprintf); |
| 225 | } |
| 226 | |
| 227 | if (T.isOSWindows() && !T.isOSCygMing()) { |
| 228 | // XXX: The earliest documentation available at the moment is for VS2015/VC19: |
| 229 | // https://docs.microsoft.com/en-us/cpp/c-runtime-library/floating-point-support?view=vs-2015 |
| 230 | // XXX: In order to use an MSVCRT older than VC19, |
| 231 | // the specific library version must be explicit in the target triple, |
| 232 | // e.g., x86_64-pc-windows-msvc18. |
| 233 | bool hasPartialC99 = true; |
| 234 | if (T.isKnownWindowsMSVCEnvironment()) { |
| 235 | VersionTuple Version = T.getEnvironmentVersion(); |
| 236 | hasPartialC99 = (Version.getMajor() == 0 || Version.getMajor() >= 19); |
| 237 | } |
| 238 | |
| 239 | // Latest targets support C89 math functions, in part. |
| 240 | bool isARM = (T.getArch() == Triple::aarch64 || |
| 241 | T.getArch() == Triple::arm); |
| 242 | bool hasPartialFloat = (isARM || |
| 243 | T.getArch() == Triple::x86_64); |
| 244 | |
| 245 | // Win32 does not support float C89 math functions, in general. |
| 246 | if (!hasPartialFloat) { |
| 247 | TLI.setUnavailable(LibFunc_acosf); |
| 248 | TLI.setUnavailable(LibFunc_asinf); |
| 249 | TLI.setUnavailable(LibFunc_atan2f); |
| 250 | TLI.setUnavailable(LibFunc_atanf); |
| 251 | TLI.setUnavailable(LibFunc_ceilf); |
| 252 | TLI.setUnavailable(LibFunc_cosf); |
| 253 | TLI.setUnavailable(LibFunc_coshf); |
| 254 | TLI.setUnavailable(LibFunc_expf); |
| 255 | TLI.setUnavailable(LibFunc_floorf); |
| 256 | TLI.setUnavailable(LibFunc_fmodf); |
| 257 | TLI.setUnavailable(LibFunc_hypotf); |
| 258 | TLI.setUnavailable(LibFunc_log10f); |
| 259 | TLI.setUnavailable(LibFunc_logf); |
| 260 | TLI.setUnavailable(LibFunc_modff); |
| 261 | TLI.setUnavailable(LibFunc_powf); |
| 262 | TLI.setUnavailable(LibFunc_remainderf); |
| 263 | TLI.setUnavailable(LibFunc_remquof); |
| 264 | TLI.setUnavailable(LibFunc_fdimf); |
| 265 | TLI.setUnavailable(LibFunc_sinf); |
| 266 | TLI.setUnavailable(LibFunc_sinhf); |
| 267 | TLI.setUnavailable(LibFunc_sqrtf); |
| 268 | TLI.setUnavailable(LibFunc_tanf); |
| 269 | TLI.setUnavailable(LibFunc_tanhf); |
| 270 | } |
| 271 | if (!isARM) |
| 272 | TLI.setUnavailable(LibFunc_fabsf); |
| 273 | TLI.setUnavailable(LibFunc_frexpf); |
| 274 | TLI.setUnavailable(LibFunc_ldexpf); |
| 275 | |
| 276 | // Win32 does not support long double C89 math functions. |
| 277 | TLI.setUnavailable(LibFunc_acosl); |
| 278 | TLI.setUnavailable(LibFunc_asinl); |
| 279 | TLI.setUnavailable(LibFunc_atan2l); |
| 280 | TLI.setUnavailable(LibFunc_atanl); |
| 281 | TLI.setUnavailable(LibFunc_ceill); |
| 282 | TLI.setUnavailable(LibFunc_cosl); |
| 283 | TLI.setUnavailable(LibFunc_coshl); |
| 284 | TLI.setUnavailable(LibFunc_expl); |
| 285 | TLI.setUnavailable(LibFunc_fabsl); |
| 286 | TLI.setUnavailable(LibFunc_floorl); |
| 287 | TLI.setUnavailable(LibFunc_fmodl); |
| 288 | TLI.setUnavailable(LibFunc_frexpl); |
| 289 | TLI.setUnavailable(LibFunc_hypotl); |
| 290 | TLI.setUnavailable(LibFunc_ldexpl); |
| 291 | TLI.setUnavailable(LibFunc_log10l); |
| 292 | TLI.setUnavailable(LibFunc_logl); |
| 293 | TLI.setUnavailable(LibFunc_modfl); |
| 294 | TLI.setUnavailable(LibFunc_powl); |
| 295 | TLI.setUnavailable(LibFunc_remainderl); |
| 296 | TLI.setUnavailable(LibFunc_remquol); |
| 297 | TLI.setUnavailable(LibFunc_fdiml); |
| 298 | TLI.setUnavailable(LibFunc_sinl); |
| 299 | TLI.setUnavailable(LibFunc_sinhl); |
| 300 | TLI.setUnavailable(LibFunc_sqrtl); |
| 301 | TLI.setUnavailable(LibFunc_tanl); |
| 302 | TLI.setUnavailable(LibFunc_tanhl); |
| 303 | |
| 304 | // Win32 does not fully support C99 math functions. |
| 305 | if (!hasPartialC99) { |
| 306 | TLI.setUnavailable(LibFunc_acosh); |
| 307 | TLI.setUnavailable(LibFunc_acoshf); |
| 308 | TLI.setUnavailable(LibFunc_asinh); |
| 309 | TLI.setUnavailable(LibFunc_asinhf); |
| 310 | TLI.setUnavailable(LibFunc_atanh); |
| 311 | TLI.setUnavailable(LibFunc_atanhf); |
| 312 | TLI.setAvailableWithName(F: LibFunc_cabs, Name: "_cabs" ); |
| 313 | TLI.setUnavailable(LibFunc_cabsf); |
| 314 | TLI.setUnavailable(LibFunc_cbrt); |
| 315 | TLI.setUnavailable(LibFunc_cbrtf); |
| 316 | TLI.setAvailableWithName(F: LibFunc_copysign, Name: "_copysign" ); |
| 317 | TLI.setAvailableWithName(F: LibFunc_copysignf, Name: "_copysignf" ); |
| 318 | TLI.setUnavailable(LibFunc_exp2); |
| 319 | TLI.setUnavailable(LibFunc_exp2f); |
| 320 | TLI.setUnavailable(LibFunc_expm1); |
| 321 | TLI.setUnavailable(LibFunc_expm1f); |
| 322 | TLI.setUnavailable(LibFunc_fmax); |
| 323 | TLI.setUnavailable(LibFunc_fmaxf); |
| 324 | TLI.setUnavailable(LibFunc_fmin); |
| 325 | TLI.setUnavailable(LibFunc_fminf); |
| 326 | TLI.setUnavailable(LibFunc_log1p); |
| 327 | TLI.setUnavailable(LibFunc_log1pf); |
| 328 | TLI.setUnavailable(LibFunc_log2); |
| 329 | TLI.setUnavailable(LibFunc_log2f); |
| 330 | TLI.setAvailableWithName(F: LibFunc_logb, Name: "_logb" ); |
| 331 | TLI.setUnavailable(LibFunc_ilogb); |
| 332 | TLI.setUnavailable(LibFunc_ilogbf); |
| 333 | if (hasPartialFloat) |
| 334 | TLI.setAvailableWithName(F: LibFunc_logbf, Name: "_logbf" ); |
| 335 | else |
| 336 | TLI.setUnavailable(LibFunc_logbf); |
| 337 | TLI.setUnavailable(LibFunc_nextafter); |
| 338 | TLI.setUnavailable(LibFunc_nextafterf); |
| 339 | TLI.setUnavailable(LibFunc_nexttoward); |
| 340 | TLI.setUnavailable(LibFunc_nexttowardf); |
| 341 | TLI.setUnavailable(LibFunc_rint); |
| 342 | TLI.setUnavailable(LibFunc_rintf); |
| 343 | TLI.setUnavailable(LibFunc_round); |
| 344 | TLI.setUnavailable(LibFunc_roundf); |
| 345 | TLI.setUnavailable(LibFunc_scalbln); |
| 346 | TLI.setUnavailable(LibFunc_scalblnf); |
| 347 | TLI.setUnavailable(LibFunc_scalblnl); |
| 348 | TLI.setUnavailable(LibFunc_scalbn); |
| 349 | TLI.setUnavailable(LibFunc_scalbnf); |
| 350 | TLI.setUnavailable(LibFunc_scalbnl); |
| 351 | TLI.setUnavailable(LibFunc_trunc); |
| 352 | TLI.setUnavailable(LibFunc_truncf); |
| 353 | } |
| 354 | |
| 355 | // Win32 does not support long double C99 math functions. |
| 356 | TLI.setUnavailable(LibFunc_acoshl); |
| 357 | TLI.setUnavailable(LibFunc_asinhl); |
| 358 | TLI.setUnavailable(LibFunc_atanhl); |
| 359 | TLI.setUnavailable(LibFunc_cabsl); |
| 360 | TLI.setUnavailable(LibFunc_cbrtl); |
| 361 | TLI.setUnavailable(LibFunc_copysignl); |
| 362 | TLI.setUnavailable(LibFunc_exp2l); |
| 363 | TLI.setUnavailable(LibFunc_expm1l); |
| 364 | TLI.setUnavailable(LibFunc_fmaxl); |
| 365 | TLI.setUnavailable(LibFunc_fminl); |
| 366 | TLI.setUnavailable(LibFunc_log1pl); |
| 367 | TLI.setUnavailable(LibFunc_log2l); |
| 368 | TLI.setUnavailable(LibFunc_logbl); |
| 369 | TLI.setUnavailable(LibFunc_ilogbl); |
| 370 | TLI.setUnavailable(LibFunc_nearbyintl); |
| 371 | TLI.setUnavailable(LibFunc_nextafterl); |
| 372 | TLI.setUnavailable(LibFunc_nexttowardl); |
| 373 | TLI.setUnavailable(LibFunc_rintl); |
| 374 | TLI.setUnavailable(LibFunc_roundl); |
| 375 | TLI.setUnavailable(LibFunc_scalblnl); |
| 376 | TLI.setUnavailable(LibFunc_scalbnl); |
| 377 | TLI.setUnavailable(LibFunc_truncl); |
| 378 | |
| 379 | // Win32 does not support these functions, but |
| 380 | // they are generally available on POSIX-compliant systems. |
| 381 | TLI.setUnavailable(LibFunc_access); |
| 382 | TLI.setUnavailable(LibFunc_chmod); |
| 383 | TLI.setUnavailable(LibFunc_closedir); |
| 384 | TLI.setUnavailable(LibFunc_fdopen); |
| 385 | TLI.setUnavailable(LibFunc_fileno); |
| 386 | TLI.setUnavailable(LibFunc_fseeko); |
| 387 | TLI.setUnavailable(LibFunc_fstat); |
| 388 | TLI.setUnavailable(LibFunc_ftello); |
| 389 | TLI.setUnavailable(LibFunc_gettimeofday); |
| 390 | TLI.setUnavailable(LibFunc_memccpy); |
| 391 | TLI.setUnavailable(LibFunc_mkdir); |
| 392 | TLI.setUnavailable(LibFunc_open); |
| 393 | TLI.setUnavailable(LibFunc_opendir); |
| 394 | TLI.setUnavailable(LibFunc_pclose); |
| 395 | TLI.setUnavailable(LibFunc_popen); |
| 396 | TLI.setUnavailable(LibFunc_read); |
| 397 | TLI.setUnavailable(LibFunc_rmdir); |
| 398 | TLI.setUnavailable(LibFunc_stat); |
| 399 | TLI.setUnavailable(LibFunc_strcasecmp); |
| 400 | TLI.setUnavailable(LibFunc_strncasecmp); |
| 401 | TLI.setUnavailable(LibFunc_unlink); |
| 402 | TLI.setUnavailable(LibFunc_utime); |
| 403 | TLI.setUnavailable(LibFunc_write); |
| 404 | } |
| 405 | |
| 406 | if (T.isOSWindows() && !T.isWindowsCygwinEnvironment()) { |
| 407 | // These functions aren't available in either MSVC or MinGW environments. |
| 408 | TLI.setUnavailable(LibFunc_bcmp); |
| 409 | TLI.setUnavailable(LibFunc_bcopy); |
| 410 | TLI.setUnavailable(LibFunc_bzero); |
| 411 | TLI.setUnavailable(LibFunc_chown); |
| 412 | TLI.setUnavailable(LibFunc_ctermid); |
| 413 | TLI.setUnavailable(LibFunc_ffs); |
| 414 | TLI.setUnavailable(LibFunc_flockfile); |
| 415 | TLI.setUnavailable(LibFunc_fstatvfs); |
| 416 | TLI.setUnavailable(LibFunc_ftrylockfile); |
| 417 | TLI.setUnavailable(LibFunc_funlockfile); |
| 418 | TLI.setUnavailable(LibFunc_getitimer); |
| 419 | TLI.setUnavailable(LibFunc_getlogin_r); |
| 420 | TLI.setUnavailable(LibFunc_getpwnam); |
| 421 | TLI.setUnavailable(LibFunc_htonl); |
| 422 | TLI.setUnavailable(LibFunc_htons); |
| 423 | TLI.setUnavailable(LibFunc_lchown); |
| 424 | TLI.setUnavailable(LibFunc_lstat); |
| 425 | TLI.setUnavailable(LibFunc_memrchr); |
| 426 | TLI.setUnavailable(LibFunc_ntohl); |
| 427 | TLI.setUnavailable(LibFunc_ntohs); |
| 428 | TLI.setUnavailable(LibFunc_pread); |
| 429 | TLI.setUnavailable(LibFunc_pwrite); |
| 430 | TLI.setUnavailable(LibFunc_readlink); |
| 431 | TLI.setUnavailable(LibFunc_realpath); |
| 432 | TLI.setUnavailable(LibFunc_setitimer); |
| 433 | TLI.setUnavailable(LibFunc_statvfs); |
| 434 | TLI.setUnavailable(LibFunc_stpcpy); |
| 435 | TLI.setUnavailable(LibFunc_stpncpy); |
| 436 | TLI.setUnavailable(LibFunc_times); |
| 437 | TLI.setUnavailable(LibFunc_uname); |
| 438 | TLI.setUnavailable(LibFunc_unsetenv); |
| 439 | TLI.setUnavailable(LibFunc_utimes); |
| 440 | |
| 441 | // MinGW does have ldexpf, but it is a plain wrapper over regular ldexp. |
| 442 | // Therefore it's not beneficial to transform code to use it, i.e. |
| 443 | // just pretend that the function is not available. |
| 444 | TLI.setUnavailable(LibFunc_ldexpf); |
| 445 | } |
| 446 | |
| 447 | // Pick just one set of new/delete variants. |
| 448 | if (T.isOSMSVCRT()) { |
| 449 | // MSVC, doesn't have the Itanium new/delete. |
| 450 | TLI.setUnavailable(LibFunc_ZdaPv); |
| 451 | TLI.setUnavailable(LibFunc_ZdaPvRKSt9nothrow_t); |
| 452 | TLI.setUnavailable(LibFunc_ZdaPvSt11align_val_t); |
| 453 | TLI.setUnavailable(LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t); |
| 454 | TLI.setUnavailable(LibFunc_ZdaPvj); |
| 455 | TLI.setUnavailable(LibFunc_ZdaPvjSt11align_val_t); |
| 456 | TLI.setUnavailable(LibFunc_ZdaPvm); |
| 457 | TLI.setUnavailable(LibFunc_ZdaPvmSt11align_val_t); |
| 458 | TLI.setUnavailable(LibFunc_ZdlPv); |
| 459 | TLI.setUnavailable(LibFunc_ZdlPvRKSt9nothrow_t); |
| 460 | TLI.setUnavailable(LibFunc_ZdlPvSt11align_val_t); |
| 461 | TLI.setUnavailable(LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t); |
| 462 | TLI.setUnavailable(LibFunc_ZdlPvj); |
| 463 | TLI.setUnavailable(LibFunc_ZdlPvjSt11align_val_t); |
| 464 | TLI.setUnavailable(LibFunc_ZdlPvm); |
| 465 | TLI.setUnavailable(LibFunc_ZdlPvmSt11align_val_t); |
| 466 | TLI.setUnavailable(LibFunc_Znaj); |
| 467 | TLI.setUnavailable(LibFunc_ZnajRKSt9nothrow_t); |
| 468 | TLI.setUnavailable(LibFunc_ZnajSt11align_val_t); |
| 469 | TLI.setUnavailable(LibFunc_ZnajSt11align_val_tRKSt9nothrow_t); |
| 470 | TLI.setUnavailable(LibFunc_Znam); |
| 471 | TLI.setUnavailable(LibFunc_ZnamRKSt9nothrow_t); |
| 472 | TLI.setUnavailable(LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t); |
| 473 | TLI.setUnavailable(LibFunc_ZnamSt11align_val_t); |
| 474 | TLI.setUnavailable(LibFunc_ZnamSt11align_val_tRKSt9nothrow_t); |
| 475 | TLI.setUnavailable(LibFunc_Znwj); |
| 476 | TLI.setUnavailable(LibFunc_ZnwjRKSt9nothrow_t); |
| 477 | TLI.setUnavailable(LibFunc_ZnwjSt11align_val_t); |
| 478 | TLI.setUnavailable(LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t); |
| 479 | TLI.setUnavailable(LibFunc_Znwm); |
| 480 | TLI.setUnavailable(LibFunc_ZnwmRKSt9nothrow_t); |
| 481 | TLI.setUnavailable(LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t); |
| 482 | TLI.setUnavailable(LibFunc_ZnwmSt11align_val_t); |
| 483 | TLI.setUnavailable(LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t); |
| 484 | TLI.setUnavailable(LibFunc_Znwm12__hot_cold_t); |
| 485 | TLI.setUnavailable(LibFunc_ZnwmSt11align_val_t12__hot_cold_t); |
| 486 | TLI.setUnavailable(LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t); |
| 487 | TLI.setUnavailable(LibFunc_Znam12__hot_cold_t); |
| 488 | TLI.setUnavailable(LibFunc_ZnamSt11align_val_t12__hot_cold_t); |
| 489 | TLI.setUnavailable(LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t); |
| 490 | TLI.setUnavailable(LibFunc_size_returning_new); |
| 491 | TLI.setUnavailable(LibFunc_size_returning_new_hot_cold); |
| 492 | TLI.setUnavailable(LibFunc_size_returning_new_aligned); |
| 493 | TLI.setUnavailable(LibFunc_size_returning_new_aligned_hot_cold); |
| 494 | } else { |
| 495 | // Not MSVC, assume it's Itanium. |
| 496 | TLI.setUnavailable(LibFunc_msvc_new_int); |
| 497 | TLI.setUnavailable(LibFunc_msvc_new_int_nothrow); |
| 498 | TLI.setUnavailable(LibFunc_msvc_new_longlong); |
| 499 | TLI.setUnavailable(LibFunc_msvc_new_longlong_nothrow); |
| 500 | TLI.setUnavailable(LibFunc_msvc_delete_ptr32); |
| 501 | TLI.setUnavailable(LibFunc_msvc_delete_ptr32_nothrow); |
| 502 | TLI.setUnavailable(LibFunc_msvc_delete_ptr32_int); |
| 503 | TLI.setUnavailable(LibFunc_msvc_delete_ptr64); |
| 504 | TLI.setUnavailable(LibFunc_msvc_delete_ptr64_nothrow); |
| 505 | TLI.setUnavailable(LibFunc_msvc_delete_ptr64_longlong); |
| 506 | TLI.setUnavailable(LibFunc_msvc_new_array_int); |
| 507 | TLI.setUnavailable(LibFunc_msvc_new_array_int_nothrow); |
| 508 | TLI.setUnavailable(LibFunc_msvc_new_array_longlong); |
| 509 | TLI.setUnavailable(LibFunc_msvc_new_array_longlong_nothrow); |
| 510 | TLI.setUnavailable(LibFunc_msvc_delete_array_ptr32); |
| 511 | TLI.setUnavailable(LibFunc_msvc_delete_array_ptr32_nothrow); |
| 512 | TLI.setUnavailable(LibFunc_msvc_delete_array_ptr32_int); |
| 513 | TLI.setUnavailable(LibFunc_msvc_delete_array_ptr64); |
| 514 | TLI.setUnavailable(LibFunc_msvc_delete_array_ptr64_nothrow); |
| 515 | TLI.setUnavailable(LibFunc_msvc_delete_array_ptr64_longlong); |
| 516 | } |
| 517 | |
| 518 | switch (T.getOS()) { |
| 519 | case Triple::MacOSX: |
| 520 | // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0 |
| 521 | // and their names are __exp10 and __exp10f. exp10l is not available on |
| 522 | // OS X or iOS. |
| 523 | TLI.setUnavailable(LibFunc_exp10l); |
| 524 | if (T.isMacOSXVersionLT(Major: 10, Minor: 9)) { |
| 525 | TLI.setUnavailable(LibFunc_exp10); |
| 526 | TLI.setUnavailable(LibFunc_exp10f); |
| 527 | } else { |
| 528 | TLI.setAvailableWithName(F: LibFunc_exp10, Name: "__exp10" ); |
| 529 | TLI.setAvailableWithName(F: LibFunc_exp10f, Name: "__exp10f" ); |
| 530 | } |
| 531 | break; |
| 532 | case Triple::IOS: |
| 533 | case Triple::TvOS: |
| 534 | case Triple::WatchOS: |
| 535 | case Triple::XROS: |
| 536 | TLI.setUnavailable(LibFunc_exp10l); |
| 537 | if (!T.isWatchOS() && |
| 538 | (T.isOSVersionLT(Major: 7, Minor: 0) || (T.isOSVersionLT(Major: 9, Minor: 0) && T.isX86()))) { |
| 539 | TLI.setUnavailable(LibFunc_exp10); |
| 540 | TLI.setUnavailable(LibFunc_exp10f); |
| 541 | } else { |
| 542 | TLI.setAvailableWithName(F: LibFunc_exp10, Name: "__exp10" ); |
| 543 | TLI.setAvailableWithName(F: LibFunc_exp10f, Name: "__exp10f" ); |
| 544 | } |
| 545 | break; |
| 546 | case Triple::Linux: |
| 547 | // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely |
| 548 | // buggy prior to glibc version 2.18. Until this version is widely deployed |
| 549 | // or we have a reasonable detection strategy, we cannot use exp10 reliably |
| 550 | // on Linux. |
| 551 | // |
| 552 | // Fall through to disable all of them. |
| 553 | [[fallthrough]]; |
| 554 | default: |
| 555 | TLI.setUnavailable(LibFunc_exp10); |
| 556 | TLI.setUnavailable(LibFunc_exp10f); |
| 557 | TLI.setUnavailable(LibFunc_exp10l); |
| 558 | } |
| 559 | |
| 560 | // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and |
| 561 | // Linux (GLIBC): |
| 562 | // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html |
| 563 | // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c |
| 564 | // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html |
| 565 | switch (T.getOS()) { |
| 566 | case Triple::Darwin: |
| 567 | case Triple::MacOSX: |
| 568 | case Triple::IOS: |
| 569 | case Triple::TvOS: |
| 570 | case Triple::WatchOS: |
| 571 | case Triple::XROS: |
| 572 | case Triple::FreeBSD: |
| 573 | case Triple::Linux: |
| 574 | break; |
| 575 | default: |
| 576 | TLI.setUnavailable(LibFunc_ffsl); |
| 577 | } |
| 578 | |
| 579 | // ffsll is available on at least FreeBSD and Linux (GLIBC): |
| 580 | // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c |
| 581 | // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html |
| 582 | switch (T.getOS()) { |
| 583 | case Triple::Darwin: |
| 584 | case Triple::MacOSX: |
| 585 | case Triple::IOS: |
| 586 | case Triple::TvOS: |
| 587 | case Triple::WatchOS: |
| 588 | case Triple::XROS: |
| 589 | case Triple::FreeBSD: |
| 590 | case Triple::Linux: |
| 591 | break; |
| 592 | default: |
| 593 | TLI.setUnavailable(LibFunc_ffsll); |
| 594 | } |
| 595 | |
| 596 | // The following functions are available on at least FreeBSD: |
| 597 | // http://svn.freebsd.org/base/head/lib/libc/string/fls.c |
| 598 | // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c |
| 599 | // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c |
| 600 | if (!T.isOSFreeBSD()) { |
| 601 | TLI.setUnavailable(LibFunc_fls); |
| 602 | TLI.setUnavailable(LibFunc_flsl); |
| 603 | TLI.setUnavailable(LibFunc_flsll); |
| 604 | } |
| 605 | |
| 606 | // The following functions are only available on GNU/Linux (using glibc). |
| 607 | // Linux variants without glibc (eg: bionic, musl) may have some subset. |
| 608 | if (!T.isOSLinux() || !T.isGNUEnvironment()) { |
| 609 | TLI.setUnavailable(LibFunc_dunder_strdup); |
| 610 | TLI.setUnavailable(LibFunc_dunder_strtok_r); |
| 611 | TLI.setUnavailable(LibFunc_dunder_isoc99_scanf); |
| 612 | TLI.setUnavailable(LibFunc_dunder_isoc99_sscanf); |
| 613 | TLI.setUnavailable(LibFunc_under_IO_getc); |
| 614 | TLI.setUnavailable(LibFunc_under_IO_putc); |
| 615 | // But, Android and musl have memalign. |
| 616 | if (!T.isAndroid() && !T.isMusl()) |
| 617 | TLI.setUnavailable(LibFunc_memalign); |
| 618 | TLI.setUnavailable(LibFunc_fopen64); |
| 619 | TLI.setUnavailable(LibFunc_fseeko64); |
| 620 | TLI.setUnavailable(LibFunc_fstat64); |
| 621 | TLI.setUnavailable(LibFunc_fstatvfs64); |
| 622 | TLI.setUnavailable(LibFunc_ftello64); |
| 623 | TLI.setUnavailable(LibFunc_lstat64); |
| 624 | TLI.setUnavailable(LibFunc_open64); |
| 625 | TLI.setUnavailable(LibFunc_stat64); |
| 626 | TLI.setUnavailable(LibFunc_statvfs64); |
| 627 | TLI.setUnavailable(LibFunc_tmpfile64); |
| 628 | |
| 629 | // Relaxed math functions are included in math-finite.h on Linux (GLIBC). |
| 630 | // Note that math-finite.h is no longer supported by top-of-tree GLIBC, |
| 631 | // so we keep these functions around just so that they're recognized by |
| 632 | // the ConstantFolder. |
| 633 | TLI.setUnavailable(LibFunc_acos_finite); |
| 634 | TLI.setUnavailable(LibFunc_acosf_finite); |
| 635 | TLI.setUnavailable(LibFunc_acosl_finite); |
| 636 | TLI.setUnavailable(LibFunc_acosh_finite); |
| 637 | TLI.setUnavailable(LibFunc_acoshf_finite); |
| 638 | TLI.setUnavailable(LibFunc_acoshl_finite); |
| 639 | TLI.setUnavailable(LibFunc_asin_finite); |
| 640 | TLI.setUnavailable(LibFunc_asinf_finite); |
| 641 | TLI.setUnavailable(LibFunc_asinl_finite); |
| 642 | TLI.setUnavailable(LibFunc_atan2_finite); |
| 643 | TLI.setUnavailable(LibFunc_atan2f_finite); |
| 644 | TLI.setUnavailable(LibFunc_atan2l_finite); |
| 645 | TLI.setUnavailable(LibFunc_atanh_finite); |
| 646 | TLI.setUnavailable(LibFunc_atanhf_finite); |
| 647 | TLI.setUnavailable(LibFunc_atanhl_finite); |
| 648 | TLI.setUnavailable(LibFunc_cosh_finite); |
| 649 | TLI.setUnavailable(LibFunc_coshf_finite); |
| 650 | TLI.setUnavailable(LibFunc_coshl_finite); |
| 651 | TLI.setUnavailable(LibFunc_exp10_finite); |
| 652 | TLI.setUnavailable(LibFunc_exp10f_finite); |
| 653 | TLI.setUnavailable(LibFunc_exp10l_finite); |
| 654 | TLI.setUnavailable(LibFunc_exp2_finite); |
| 655 | TLI.setUnavailable(LibFunc_exp2f_finite); |
| 656 | TLI.setUnavailable(LibFunc_exp2l_finite); |
| 657 | TLI.setUnavailable(LibFunc_exp_finite); |
| 658 | TLI.setUnavailable(LibFunc_expf_finite); |
| 659 | TLI.setUnavailable(LibFunc_expl_finite); |
| 660 | TLI.setUnavailable(LibFunc_log10_finite); |
| 661 | TLI.setUnavailable(LibFunc_log10f_finite); |
| 662 | TLI.setUnavailable(LibFunc_log10l_finite); |
| 663 | TLI.setUnavailable(LibFunc_log2_finite); |
| 664 | TLI.setUnavailable(LibFunc_log2f_finite); |
| 665 | TLI.setUnavailable(LibFunc_log2l_finite); |
| 666 | TLI.setUnavailable(LibFunc_log_finite); |
| 667 | TLI.setUnavailable(LibFunc_logf_finite); |
| 668 | TLI.setUnavailable(LibFunc_logl_finite); |
| 669 | TLI.setUnavailable(LibFunc_pow_finite); |
| 670 | TLI.setUnavailable(LibFunc_powf_finite); |
| 671 | TLI.setUnavailable(LibFunc_powl_finite); |
| 672 | TLI.setUnavailable(LibFunc_sinh_finite); |
| 673 | TLI.setUnavailable(LibFunc_sinhf_finite); |
| 674 | TLI.setUnavailable(LibFunc_sinhl_finite); |
| 675 | TLI.setUnavailable(LibFunc_sqrt_finite); |
| 676 | TLI.setUnavailable(LibFunc_sqrtf_finite); |
| 677 | TLI.setUnavailable(LibFunc_sqrtl_finite); |
| 678 | } |
| 679 | |
| 680 | if ((T.isOSLinux() && T.isGNUEnvironment()) || |
| 681 | (T.isAndroid() && !T.isAndroidVersionLT(Major: 28))) { |
| 682 | // available IO unlocked variants on GNU/Linux and Android P or later |
| 683 | TLI.setAvailable(LibFunc_getc_unlocked); |
| 684 | TLI.setAvailable(LibFunc_getchar_unlocked); |
| 685 | TLI.setAvailable(LibFunc_putc_unlocked); |
| 686 | TLI.setAvailable(LibFunc_putchar_unlocked); |
| 687 | TLI.setAvailable(LibFunc_fputc_unlocked); |
| 688 | TLI.setAvailable(LibFunc_fgetc_unlocked); |
| 689 | TLI.setAvailable(LibFunc_fread_unlocked); |
| 690 | TLI.setAvailable(LibFunc_fwrite_unlocked); |
| 691 | TLI.setAvailable(LibFunc_fputs_unlocked); |
| 692 | TLI.setAvailable(LibFunc_fgets_unlocked); |
| 693 | } |
| 694 | |
| 695 | if (T.isPS()) { |
| 696 | // PS4/PS5 do have memalign. |
| 697 | TLI.setAvailable(LibFunc_memalign); |
| 698 | |
| 699 | // PS4/PS5 do not have new/delete with "unsigned int" size parameter; |
| 700 | // they only have the "unsigned long" versions. |
| 701 | TLI.setUnavailable(LibFunc_ZdaPvj); |
| 702 | TLI.setUnavailable(LibFunc_ZdaPvjSt11align_val_t); |
| 703 | TLI.setUnavailable(LibFunc_ZdlPvj); |
| 704 | TLI.setUnavailable(LibFunc_ZdlPvjSt11align_val_t); |
| 705 | TLI.setUnavailable(LibFunc_Znaj); |
| 706 | TLI.setUnavailable(LibFunc_ZnajRKSt9nothrow_t); |
| 707 | TLI.setUnavailable(LibFunc_ZnajSt11align_val_t); |
| 708 | TLI.setUnavailable(LibFunc_ZnajSt11align_val_tRKSt9nothrow_t); |
| 709 | TLI.setUnavailable(LibFunc_Znwj); |
| 710 | TLI.setUnavailable(LibFunc_ZnwjRKSt9nothrow_t); |
| 711 | TLI.setUnavailable(LibFunc_ZnwjSt11align_val_t); |
| 712 | TLI.setUnavailable(LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t); |
| 713 | |
| 714 | // None of the *_chk functions. |
| 715 | TLI.setUnavailable(LibFunc_memccpy_chk); |
| 716 | TLI.setUnavailable(LibFunc_memcpy_chk); |
| 717 | TLI.setUnavailable(LibFunc_memmove_chk); |
| 718 | TLI.setUnavailable(LibFunc_mempcpy_chk); |
| 719 | TLI.setUnavailable(LibFunc_memset_chk); |
| 720 | TLI.setUnavailable(LibFunc_snprintf_chk); |
| 721 | TLI.setUnavailable(LibFunc_sprintf_chk); |
| 722 | TLI.setUnavailable(LibFunc_stpcpy_chk); |
| 723 | TLI.setUnavailable(LibFunc_stpncpy_chk); |
| 724 | TLI.setUnavailable(LibFunc_strcat_chk); |
| 725 | TLI.setUnavailable(LibFunc_strcpy_chk); |
| 726 | TLI.setUnavailable(LibFunc_strlcat_chk); |
| 727 | TLI.setUnavailable(LibFunc_strlcpy_chk); |
| 728 | TLI.setUnavailable(LibFunc_strlen_chk); |
| 729 | TLI.setUnavailable(LibFunc_strncat_chk); |
| 730 | TLI.setUnavailable(LibFunc_strncpy_chk); |
| 731 | TLI.setUnavailable(LibFunc_vsnprintf_chk); |
| 732 | TLI.setUnavailable(LibFunc_vsprintf_chk); |
| 733 | |
| 734 | // Various Posix system functions. |
| 735 | TLI.setUnavailable(LibFunc_access); |
| 736 | TLI.setUnavailable(LibFunc_chmod); |
| 737 | TLI.setUnavailable(LibFunc_chown); |
| 738 | TLI.setUnavailable(LibFunc_closedir); |
| 739 | TLI.setUnavailable(LibFunc_ctermid); |
| 740 | TLI.setUnavailable(LibFunc_execl); |
| 741 | TLI.setUnavailable(LibFunc_execle); |
| 742 | TLI.setUnavailable(LibFunc_execlp); |
| 743 | TLI.setUnavailable(LibFunc_execv); |
| 744 | TLI.setUnavailable(LibFunc_execvP); |
| 745 | TLI.setUnavailable(LibFunc_execve); |
| 746 | TLI.setUnavailable(LibFunc_execvp); |
| 747 | TLI.setUnavailable(LibFunc_execvpe); |
| 748 | TLI.setUnavailable(LibFunc_fork); |
| 749 | TLI.setUnavailable(LibFunc_fstat); |
| 750 | TLI.setUnavailable(LibFunc_fstatvfs); |
| 751 | TLI.setUnavailable(LibFunc_getenv); |
| 752 | TLI.setUnavailable(LibFunc_getitimer); |
| 753 | TLI.setUnavailable(LibFunc_getlogin_r); |
| 754 | TLI.setUnavailable(LibFunc_getpwnam); |
| 755 | TLI.setUnavailable(LibFunc_gettimeofday); |
| 756 | TLI.setUnavailable(LibFunc_lchown); |
| 757 | TLI.setUnavailable(LibFunc_lstat); |
| 758 | TLI.setUnavailable(LibFunc_mkdir); |
| 759 | TLI.setUnavailable(LibFunc_open); |
| 760 | TLI.setUnavailable(LibFunc_opendir); |
| 761 | TLI.setUnavailable(LibFunc_pclose); |
| 762 | TLI.setUnavailable(LibFunc_popen); |
| 763 | TLI.setUnavailable(LibFunc_pread); |
| 764 | TLI.setUnavailable(LibFunc_pvalloc); |
| 765 | TLI.setUnavailable(LibFunc_pwrite); |
| 766 | TLI.setUnavailable(LibFunc_read); |
| 767 | TLI.setUnavailable(LibFunc_readlink); |
| 768 | TLI.setUnavailable(LibFunc_realpath); |
| 769 | TLI.setUnavailable(LibFunc_rename); |
| 770 | TLI.setUnavailable(LibFunc_rmdir); |
| 771 | TLI.setUnavailable(LibFunc_setitimer); |
| 772 | TLI.setUnavailable(LibFunc_stat); |
| 773 | TLI.setUnavailable(LibFunc_statvfs); |
| 774 | TLI.setUnavailable(LibFunc_system); |
| 775 | TLI.setUnavailable(LibFunc_times); |
| 776 | TLI.setUnavailable(LibFunc_tmpfile); |
| 777 | TLI.setUnavailable(LibFunc_unlink); |
| 778 | TLI.setUnavailable(LibFunc_uname); |
| 779 | TLI.setUnavailable(LibFunc_unsetenv); |
| 780 | TLI.setUnavailable(LibFunc_utime); |
| 781 | TLI.setUnavailable(LibFunc_utimes); |
| 782 | TLI.setUnavailable(LibFunc_valloc); |
| 783 | TLI.setUnavailable(LibFunc_write); |
| 784 | |
| 785 | // Miscellaneous other functions not provided. |
| 786 | TLI.setUnavailable(LibFunc_atomic_load); |
| 787 | TLI.setUnavailable(LibFunc_atomic_store); |
| 788 | TLI.setUnavailable(LibFunc___kmpc_alloc_shared); |
| 789 | TLI.setUnavailable(LibFunc___kmpc_free_shared); |
| 790 | TLI.setUnavailable(LibFunc_dunder_strndup); |
| 791 | TLI.setUnavailable(LibFunc_bcmp); |
| 792 | TLI.setUnavailable(LibFunc_bcopy); |
| 793 | TLI.setUnavailable(LibFunc_bzero); |
| 794 | TLI.setUnavailable(LibFunc_cabs); |
| 795 | TLI.setUnavailable(LibFunc_cabsf); |
| 796 | TLI.setUnavailable(LibFunc_cabsl); |
| 797 | TLI.setUnavailable(LibFunc_ffs); |
| 798 | TLI.setUnavailable(LibFunc_flockfile); |
| 799 | TLI.setUnavailable(LibFunc_fseeko); |
| 800 | TLI.setUnavailable(LibFunc_ftello); |
| 801 | TLI.setUnavailable(LibFunc_ftrylockfile); |
| 802 | TLI.setUnavailable(LibFunc_funlockfile); |
| 803 | TLI.setUnavailable(LibFunc_htonl); |
| 804 | TLI.setUnavailable(LibFunc_htons); |
| 805 | TLI.setUnavailable(LibFunc_isascii); |
| 806 | TLI.setUnavailable(LibFunc_memccpy); |
| 807 | TLI.setUnavailable(LibFunc_mempcpy); |
| 808 | TLI.setUnavailable(LibFunc_memrchr); |
| 809 | TLI.setUnavailable(LibFunc_ntohl); |
| 810 | TLI.setUnavailable(LibFunc_ntohs); |
| 811 | TLI.setUnavailable(LibFunc_reallocarray); |
| 812 | TLI.setUnavailable(LibFunc_reallocf); |
| 813 | TLI.setUnavailable(LibFunc_roundeven); |
| 814 | TLI.setUnavailable(LibFunc_roundevenf); |
| 815 | TLI.setUnavailable(LibFunc_roundevenl); |
| 816 | TLI.setUnavailable(LibFunc_stpcpy); |
| 817 | TLI.setUnavailable(LibFunc_stpncpy); |
| 818 | TLI.setUnavailable(LibFunc_strlcat); |
| 819 | TLI.setUnavailable(LibFunc_strlcpy); |
| 820 | TLI.setUnavailable(LibFunc_strndup); |
| 821 | TLI.setUnavailable(LibFunc_strnlen); |
| 822 | TLI.setUnavailable(LibFunc_toascii); |
| 823 | } |
| 824 | |
| 825 | if (T.isOSFreeBSD()) { |
| 826 | TLI.setAvailable(LibFunc_dunder_strtok_r); |
| 827 | TLI.setAvailable(LibFunc_memalign); |
| 828 | TLI.setAvailable(LibFunc_fputc_unlocked); |
| 829 | TLI.setAvailable(LibFunc_fputs_unlocked); |
| 830 | TLI.setAvailable(LibFunc_fread_unlocked); |
| 831 | TLI.setAvailable(LibFunc_fwrite_unlocked); |
| 832 | TLI.setAvailable(LibFunc_getc_unlocked); |
| 833 | TLI.setAvailable(LibFunc_getchar_unlocked); |
| 834 | TLI.setAvailable(LibFunc_putc_unlocked); |
| 835 | TLI.setAvailable(LibFunc_putchar_unlocked); |
| 836 | |
| 837 | TLI.setUnavailable(LibFunc___kmpc_alloc_shared); |
| 838 | TLI.setUnavailable(LibFunc___kmpc_free_shared); |
| 839 | TLI.setUnavailable(LibFunc_dunder_strndup); |
| 840 | TLI.setUnavailable(LibFunc_memccpy_chk); |
| 841 | TLI.setUnavailable(LibFunc_strlen_chk); |
| 842 | TLI.setUnavailable(LibFunc_fmaximum_num); |
| 843 | TLI.setUnavailable(LibFunc_fmaximum_numf); |
| 844 | TLI.setUnavailable(LibFunc_fmaximum_numl); |
| 845 | TLI.setUnavailable(LibFunc_fminimum_num); |
| 846 | TLI.setUnavailable(LibFunc_fminimum_numf); |
| 847 | TLI.setUnavailable(LibFunc_fminimum_numl); |
| 848 | TLI.setUnavailable(LibFunc_roundeven); |
| 849 | TLI.setUnavailable(LibFunc_roundevenf); |
| 850 | TLI.setUnavailable(LibFunc_roundevenl); |
| 851 | } |
| 852 | |
| 853 | // As currently implemented in clang, NVPTX code has no standard library to |
| 854 | // speak of. Headers provide a standard-ish library implementation, but many |
| 855 | // of the signatures are wrong -- for example, many libm functions are not |
| 856 | // extern "C". |
| 857 | // |
| 858 | // libdevice, an IR library provided by nvidia, is linked in by the front-end, |
| 859 | // but only used functions are provided to llvm. Moreover, most of the |
| 860 | // functions in libdevice don't map precisely to standard library functions. |
| 861 | // |
| 862 | // FIXME: Having no standard library prevents e.g. many fastmath |
| 863 | // optimizations, so this situation should be fixed. |
| 864 | if (T.isNVPTX()) { |
| 865 | TLI.disableAllFunctions(); |
| 866 | TLI.setAvailable(LibFunc_nvvm_reflect); |
| 867 | TLI.setAvailable(llvm::LibFunc_malloc); |
| 868 | TLI.setAvailable(llvm::LibFunc_free); |
| 869 | |
| 870 | // TODO: We could enable the following two according to [0] but we haven't |
| 871 | // done an evaluation wrt. the performance implications. |
| 872 | // [0] |
| 873 | // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#dynamic-global-memory-allocation-and-operations |
| 874 | // |
| 875 | // TLI.setAvailable(llvm::LibFunc_memcpy); |
| 876 | // TLI.setAvailable(llvm::LibFunc_memset); |
| 877 | |
| 878 | TLI.setAvailable(llvm::LibFunc___kmpc_alloc_shared); |
| 879 | TLI.setAvailable(llvm::LibFunc___kmpc_free_shared); |
| 880 | } else { |
| 881 | TLI.setUnavailable(LibFunc_nvvm_reflect); |
| 882 | } |
| 883 | |
| 884 | // These vec_malloc/free routines are only available on AIX. |
| 885 | if (!T.isOSAIX()) { |
| 886 | TLI.setUnavailable(LibFunc_vec_calloc); |
| 887 | TLI.setUnavailable(LibFunc_vec_malloc); |
| 888 | TLI.setUnavailable(LibFunc_vec_realloc); |
| 889 | TLI.setUnavailable(LibFunc_vec_free); |
| 890 | } |
| 891 | |
| 892 | if (T.isOSAIX()) |
| 893 | TLI.setUnavailable(LibFunc_memrchr); |
| 894 | |
| 895 | TLI.addVectorizableFunctionsFromVecLib(VecLib, TargetTriple: T); |
| 896 | } |
| 897 | |
| 898 | /// Initialize the set of available library functions based on the specified |
| 899 | /// target triple. This should be carefully written so that a missing target |
| 900 | /// triple gets a sane set of defaults. |
| 901 | static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, |
| 902 | const llvm::StringTable &StandardNames, |
| 903 | VectorLibrary VecLib) { |
| 904 | initializeBase(TLI, T); |
| 905 | initializeLibCalls(TLI, T, StandardNames, VecLib); |
| 906 | } |
| 907 | |
| 908 | TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T, |
| 909 | VectorLibrary VecLib) { |
| 910 | // Default to everything being available. |
| 911 | memset(s: AvailableArray, c: -1, n: sizeof(AvailableArray)); |
| 912 | |
| 913 | initialize(TLI&: *this, T, StandardNames: StandardNamesStrTable, VecLib); |
| 914 | } |
| 915 | |
| 916 | TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI) |
| 917 | : CustomNames(TLI.CustomNames), ShouldExtI32Param(TLI.ShouldExtI32Param), |
| 918 | ShouldExtI32Return(TLI.ShouldExtI32Return), |
| 919 | ShouldSignExtI32Param(TLI.ShouldSignExtI32Param), |
| 920 | ShouldSignExtI32Return(TLI.ShouldSignExtI32Return), |
| 921 | SizeOfInt(TLI.SizeOfInt) { |
| 922 | memcpy(dest: AvailableArray, src: TLI.AvailableArray, n: sizeof(AvailableArray)); |
| 923 | VectorDescs = TLI.VectorDescs; |
| 924 | ScalarDescs = TLI.ScalarDescs; |
| 925 | } |
| 926 | |
| 927 | TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI) |
| 928 | : CustomNames(std::move(TLI.CustomNames)), |
| 929 | ShouldExtI32Param(TLI.ShouldExtI32Param), |
| 930 | ShouldExtI32Return(TLI.ShouldExtI32Return), |
| 931 | ShouldSignExtI32Param(TLI.ShouldSignExtI32Param), |
| 932 | ShouldSignExtI32Return(TLI.ShouldSignExtI32Return), |
| 933 | SizeOfInt(TLI.SizeOfInt) { |
| 934 | std::move(first: std::begin(arr&: TLI.AvailableArray), last: std::end(arr&: TLI.AvailableArray), |
| 935 | result: AvailableArray); |
| 936 | VectorDescs = TLI.VectorDescs; |
| 937 | ScalarDescs = TLI.ScalarDescs; |
| 938 | } |
| 939 | |
| 940 | TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) { |
| 941 | CustomNames = TLI.CustomNames; |
| 942 | ShouldExtI32Param = TLI.ShouldExtI32Param; |
| 943 | ShouldExtI32Return = TLI.ShouldExtI32Return; |
| 944 | ShouldSignExtI32Param = TLI.ShouldSignExtI32Param; |
| 945 | ShouldSignExtI32Return = TLI.ShouldSignExtI32Return; |
| 946 | SizeOfInt = TLI.SizeOfInt; |
| 947 | memcpy(dest: AvailableArray, src: TLI.AvailableArray, n: sizeof(AvailableArray)); |
| 948 | return *this; |
| 949 | } |
| 950 | |
| 951 | TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) { |
| 952 | CustomNames = std::move(TLI.CustomNames); |
| 953 | ShouldExtI32Param = TLI.ShouldExtI32Param; |
| 954 | ShouldExtI32Return = TLI.ShouldExtI32Return; |
| 955 | ShouldSignExtI32Param = TLI.ShouldSignExtI32Param; |
| 956 | ShouldSignExtI32Return = TLI.ShouldSignExtI32Return; |
| 957 | SizeOfInt = TLI.SizeOfInt; |
| 958 | std::move(first: std::begin(arr&: TLI.AvailableArray), last: std::end(arr&: TLI.AvailableArray), |
| 959 | result: AvailableArray); |
| 960 | return *this; |
| 961 | } |
| 962 | |
| 963 | static StringRef sanitizeFunctionName(StringRef funcName) { |
| 964 | // Filter out empty names and names containing null bytes, those can't be in |
| 965 | // our table. |
| 966 | if (funcName.empty() || funcName.contains(C: '\0')) |
| 967 | return StringRef(); |
| 968 | |
| 969 | // Check for \01 prefix that is used to mangle __asm declarations and |
| 970 | // strip it if present. |
| 971 | return GlobalValue::dropLLVMManglingEscape(Name: funcName); |
| 972 | } |
| 973 | |
| 974 | static DenseMap<StringRef, LibFunc> |
| 975 | buildIndexMap(const llvm::StringTable &StandardNames) { |
| 976 | DenseMap<StringRef, LibFunc> Indices; |
| 977 | unsigned Idx = 0; |
| 978 | Indices.reserve(NumEntries: LibFunc::NumLibFuncs); |
| 979 | for (const auto &Func : StandardNames) |
| 980 | Indices[Func] = static_cast<LibFunc>(Idx++); |
| 981 | return Indices; |
| 982 | } |
| 983 | |
| 984 | bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName, LibFunc &F) const { |
| 985 | funcName = sanitizeFunctionName(funcName); |
| 986 | if (funcName.empty()) |
| 987 | return false; |
| 988 | |
| 989 | static const DenseMap<StringRef, LibFunc> Indices = |
| 990 | buildIndexMap(StandardNames: StandardNamesStrTable); |
| 991 | |
| 992 | if (auto Loc = Indices.find(Val: funcName); Loc != Indices.end()) { |
| 993 | F = Loc->second; |
| 994 | return true; |
| 995 | } |
| 996 | return false; |
| 997 | } |
| 998 | |
| 999 | // Return true if ArgTy matches Ty. |
| 1000 | |
| 1001 | static bool matchType(FuncArgTypeID ArgTy, const Type *Ty, unsigned IntBits, |
| 1002 | unsigned SizeTBits) { |
| 1003 | switch (ArgTy) { |
| 1004 | case Void: |
| 1005 | return Ty->isVoidTy(); |
| 1006 | case Bool: |
| 1007 | return Ty->isIntegerTy(Bitwidth: 8); |
| 1008 | case Int16: |
| 1009 | return Ty->isIntegerTy(Bitwidth: 16); |
| 1010 | case Int32: |
| 1011 | return Ty->isIntegerTy(Bitwidth: 32); |
| 1012 | case Int: |
| 1013 | return Ty->isIntegerTy(Bitwidth: IntBits); |
| 1014 | case IntPlus: |
| 1015 | return Ty->isIntegerTy() && Ty->getPrimitiveSizeInBits() >= IntBits; |
| 1016 | case IntX: |
| 1017 | return Ty->isIntegerTy(); |
| 1018 | case Long: |
| 1019 | // TODO: Figure out and use long size. |
| 1020 | return Ty->isIntegerTy() && Ty->getPrimitiveSizeInBits() >= IntBits; |
| 1021 | case Int64: |
| 1022 | return Ty->isIntegerTy(Bitwidth: 64); |
| 1023 | case LLong: |
| 1024 | return Ty->isIntegerTy(Bitwidth: 64); |
| 1025 | case SizeT: |
| 1026 | case SSizeT: |
| 1027 | return Ty->isIntegerTy(Bitwidth: SizeTBits); |
| 1028 | case Flt: |
| 1029 | return Ty->isFloatTy(); |
| 1030 | case Dbl: |
| 1031 | return Ty->isDoubleTy(); |
| 1032 | // TODO: Tighten this up. |
| 1033 | case LDbl: |
| 1034 | return Ty->isFloatingPointTy(); |
| 1035 | case Floating: |
| 1036 | return Ty->isFloatingPointTy(); |
| 1037 | case Ptr: |
| 1038 | return Ty->isPointerTy(); |
| 1039 | case Struct: |
| 1040 | return Ty->isStructTy(); |
| 1041 | default: |
| 1042 | break; |
| 1043 | } |
| 1044 | |
| 1045 | llvm_unreachable("Invalid type" ); |
| 1046 | } |
| 1047 | |
| 1048 | static bool isValidProtoForSizeReturningNew(const FunctionType &FTy, LibFunc F, |
| 1049 | const Module &M, |
| 1050 | int SizeTSizeBits) { |
| 1051 | switch (F) { |
| 1052 | case LibFunc_size_returning_new: { |
| 1053 | if (FTy.getNumParams() != 1 || |
| 1054 | !FTy.getParamType(i: 0)->isIntegerTy(Bitwidth: SizeTSizeBits)) { |
| 1055 | return false; |
| 1056 | } |
| 1057 | } break; |
| 1058 | case LibFunc_size_returning_new_hot_cold: { |
| 1059 | if (FTy.getNumParams() != 2 || |
| 1060 | !FTy.getParamType(i: 0)->isIntegerTy(Bitwidth: SizeTSizeBits) || |
| 1061 | !FTy.getParamType(i: 1)->isIntegerTy(Bitwidth: 8)) { |
| 1062 | return false; |
| 1063 | } |
| 1064 | } break; |
| 1065 | case LibFunc_size_returning_new_aligned: { |
| 1066 | if (FTy.getNumParams() != 2 || |
| 1067 | !FTy.getParamType(i: 0)->isIntegerTy(Bitwidth: SizeTSizeBits) || |
| 1068 | !FTy.getParamType(i: 1)->isIntegerTy(Bitwidth: SizeTSizeBits)) { |
| 1069 | return false; |
| 1070 | } |
| 1071 | } break; |
| 1072 | case LibFunc_size_returning_new_aligned_hot_cold: |
| 1073 | if (FTy.getNumParams() != 3 || |
| 1074 | !FTy.getParamType(i: 0)->isIntegerTy(Bitwidth: SizeTSizeBits) || |
| 1075 | !FTy.getParamType(i: 1)->isIntegerTy(Bitwidth: SizeTSizeBits) || |
| 1076 | !FTy.getParamType(i: 2)->isIntegerTy(Bitwidth: 8)) { |
| 1077 | return false; |
| 1078 | } |
| 1079 | break; |
| 1080 | default: |
| 1081 | return false; |
| 1082 | } |
| 1083 | |
| 1084 | auto &Context = M.getContext(); |
| 1085 | PointerType *PtrTy = PointerType::get(C&: Context, AddressSpace: 0); |
| 1086 | StructType *SizedPtrTy = StructType::get( |
| 1087 | Context, Elements: {PtrTy, Type::getIntNTy(C&: Context, N: SizeTSizeBits)}); |
| 1088 | return FTy.getReturnType() == SizedPtrTy; |
| 1089 | } |
| 1090 | |
| 1091 | bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy, |
| 1092 | LibFunc F, |
| 1093 | const Module &M) const { |
| 1094 | unsigned NumParams = FTy.getNumParams(); |
| 1095 | |
| 1096 | switch (F) { |
| 1097 | // Special handling for <complex.h> functions: |
| 1098 | case LibFunc_cabs: |
| 1099 | case LibFunc_cabsf: |
| 1100 | case LibFunc_cabsl: { |
| 1101 | Type *RetTy = FTy.getReturnType(); |
| 1102 | if (!RetTy->isFloatingPointTy() || NumParams == 0) |
| 1103 | return false; |
| 1104 | |
| 1105 | Type *ParamTy = FTy.getParamType(i: 0); |
| 1106 | // NOTE: These prototypes are target specific and currently support |
| 1107 | // "complex" passed as an array or discrete real & imaginary parameters. |
| 1108 | // Add other calling conventions to enable libcall optimizations. |
| 1109 | if (NumParams == 1) |
| 1110 | return (ParamTy->isArrayTy() && ParamTy->getArrayNumElements() == 2 && |
| 1111 | ParamTy->getArrayElementType() == RetTy); |
| 1112 | else if (NumParams == 2) |
| 1113 | return ParamTy == RetTy && FTy.getParamType(i: 1) == RetTy; |
| 1114 | |
| 1115 | return false; |
| 1116 | } |
| 1117 | // Special handling for the sincospi functions that return either |
| 1118 | // a struct or vector: |
| 1119 | case LibFunc_sincospi_stret: |
| 1120 | case LibFunc_sincospif_stret: { |
| 1121 | if (NumParams != 1) |
| 1122 | return false; |
| 1123 | |
| 1124 | Type *RetTy = FTy.getReturnType(); |
| 1125 | Type *ParamTy = FTy.getParamType(i: 0); |
| 1126 | if (auto *Ty = dyn_cast<StructType>(Val: RetTy)) { |
| 1127 | if (Ty->getNumElements() != 2) |
| 1128 | return false; |
| 1129 | return (Ty->getElementType(N: 0) == ParamTy && |
| 1130 | Ty->getElementType(N: 1) == ParamTy); |
| 1131 | } |
| 1132 | |
| 1133 | if (auto *Ty = dyn_cast<FixedVectorType>(Val: RetTy)) { |
| 1134 | if (Ty->getNumElements() != 2) |
| 1135 | return false; |
| 1136 | return Ty->getElementType() == ParamTy; |
| 1137 | } |
| 1138 | |
| 1139 | return false; |
| 1140 | } |
| 1141 | // Special handling of __size_returning_new functions that return a struct |
| 1142 | // of type {void*, size_t}. |
| 1143 | case LibFunc_size_returning_new: |
| 1144 | case LibFunc_size_returning_new_hot_cold: |
| 1145 | case LibFunc_size_returning_new_aligned: |
| 1146 | case LibFunc_size_returning_new_aligned_hot_cold: |
| 1147 | return isValidProtoForSizeReturningNew(FTy, F, M, SizeTSizeBits: getSizeTSize(M)); |
| 1148 | default: |
| 1149 | break; |
| 1150 | } |
| 1151 | |
| 1152 | unsigned IntBits = getIntSize(); |
| 1153 | unsigned SizeTBits = getSizeTSize(M); |
| 1154 | unsigned Idx = 0; |
| 1155 | |
| 1156 | // Iterate over the type ids in the function prototype, matching each |
| 1157 | // against the function's type FTy, starting with its return type. |
| 1158 | // Return true if both match in number and kind, inclduing the ellipsis. |
| 1159 | Type *Ty = FTy.getReturnType(), *LastTy = Ty; |
| 1160 | const auto *ProtoTypes = &SignatureTable[SignatureOffset[F]]; |
| 1161 | for (auto TyID = ProtoTypes[Idx]; TyID != NoFuncArgType; |
| 1162 | TyID = ProtoTypes[++Idx]) { |
| 1163 | if (TyID == NoFuncArgType) |
| 1164 | break; |
| 1165 | |
| 1166 | if (TyID == Ellip) { |
| 1167 | // The ellipsis ends the protoype list but is not a part of FTy's |
| 1168 | // argument list. Except when it's last it must be followed by |
| 1169 | // NoFuncArgType. |
| 1170 | assert(ProtoTypes[Idx] == NoFuncArgType || |
| 1171 | ProtoTypes[Idx + 1] == NoFuncArgType); |
| 1172 | return FTy.isFunctionVarArg(); |
| 1173 | } |
| 1174 | |
| 1175 | if (TyID == Same) { |
| 1176 | assert(Idx != 0 && "Type ID 'Same' must not be first!" ); |
| 1177 | if (Ty != LastTy) |
| 1178 | return false; |
| 1179 | } else { |
| 1180 | if (!Ty || !matchType(ArgTy: TyID, Ty, IntBits, SizeTBits)) |
| 1181 | return false; |
| 1182 | LastTy = Ty; |
| 1183 | } |
| 1184 | |
| 1185 | if (Idx == NumParams) { |
| 1186 | // There's at least one and at most two more type ids than there are |
| 1187 | // arguments in FTy's argument list. |
| 1188 | Ty = nullptr; |
| 1189 | continue; |
| 1190 | } |
| 1191 | |
| 1192 | Ty = FTy.getParamType(i: Idx); |
| 1193 | } |
| 1194 | |
| 1195 | // Return success only if all entries on both lists have been processed |
| 1196 | // and the function is not a variadic one. |
| 1197 | return Idx == NumParams + 1 && !FTy.isFunctionVarArg(); |
| 1198 | } |
| 1199 | |
| 1200 | bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl, |
| 1201 | LibFunc &F) const { |
| 1202 | // Intrinsics don't overlap w/libcalls; if our module has a large number of |
| 1203 | // intrinsics, this ends up being an interesting compile time win since we |
| 1204 | // avoid string normalization and comparison. |
| 1205 | if (FDecl.isIntrinsic()) return false; |
| 1206 | |
| 1207 | const Module *M = FDecl.getParent(); |
| 1208 | assert(M && "Expecting FDecl to be connected to a Module." ); |
| 1209 | |
| 1210 | if (FDecl.LibFuncCache == Function::UnknownLibFunc) |
| 1211 | if (!getLibFunc(funcName: FDecl.getName(), F&: FDecl.LibFuncCache)) |
| 1212 | FDecl.LibFuncCache = NotLibFunc; |
| 1213 | |
| 1214 | if (FDecl.LibFuncCache == NotLibFunc) |
| 1215 | return false; |
| 1216 | |
| 1217 | F = FDecl.LibFuncCache; |
| 1218 | return isValidProtoForLibFunc(FTy: *FDecl.getFunctionType(), F, M: *M); |
| 1219 | } |
| 1220 | |
| 1221 | bool TargetLibraryInfoImpl::getLibFunc(unsigned int Opcode, Type *Ty, |
| 1222 | LibFunc &F) const { |
| 1223 | // Must be a frem instruction with float or double arguments. |
| 1224 | if (Opcode != Instruction::FRem || (!Ty->isDoubleTy() && !Ty->isFloatTy())) |
| 1225 | return false; |
| 1226 | |
| 1227 | F = Ty->isDoubleTy() ? LibFunc_fmod : LibFunc_fmodf; |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
| 1231 | void TargetLibraryInfoImpl::disableAllFunctions() { |
| 1232 | memset(s: AvailableArray, c: 0, n: sizeof(AvailableArray)); |
| 1233 | } |
| 1234 | |
| 1235 | static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) { |
| 1236 | return LHS.getScalarFnName() < RHS.getScalarFnName(); |
| 1237 | } |
| 1238 | |
| 1239 | static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) { |
| 1240 | return LHS.getVectorFnName() < RHS.getVectorFnName(); |
| 1241 | } |
| 1242 | |
| 1243 | static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) { |
| 1244 | return LHS.getScalarFnName() < S; |
| 1245 | } |
| 1246 | |
| 1247 | void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) { |
| 1248 | llvm::append_range(C&: VectorDescs, R&: Fns); |
| 1249 | llvm::sort(C&: VectorDescs, Comp: compareByScalarFnName); |
| 1250 | |
| 1251 | llvm::append_range(C&: ScalarDescs, R&: Fns); |
| 1252 | llvm::sort(C&: ScalarDescs, Comp: compareByVectorFnName); |
| 1253 | } |
| 1254 | |
| 1255 | static const VecDesc VecFuncs_Accelerate[] = { |
| 1256 | #define TLI_DEFINE_ACCELERATE_VECFUNCS |
| 1257 | #include "llvm/Analysis/VecFuncs.def" |
| 1258 | #undef TLI_DEFINE_ACCELERATE_VECFUNCS |
| 1259 | }; |
| 1260 | |
| 1261 | static const VecDesc VecFuncs_DarwinLibSystemM[] = { |
| 1262 | #define TLI_DEFINE_DARWIN_LIBSYSTEM_M_VECFUNCS |
| 1263 | #include "llvm/Analysis/VecFuncs.def" |
| 1264 | #undef TLI_DEFINE_DARWIN_LIBSYSTEM_M_VECFUNCS |
| 1265 | }; |
| 1266 | |
| 1267 | static const VecDesc VecFuncs_LIBMVEC_X86[] = { |
| 1268 | #define TLI_DEFINE_LIBMVEC_X86_VECFUNCS |
| 1269 | #include "llvm/Analysis/VecFuncs.def" |
| 1270 | #undef TLI_DEFINE_LIBMVEC_X86_VECFUNCS |
| 1271 | }; |
| 1272 | |
| 1273 | static const VecDesc VecFuncs_LIBMVEC_AARCH64[] = { |
| 1274 | #define TLI_DEFINE_LIBMVEC_AARCH64_VECFUNCS |
| 1275 | #define TLI_DEFINE_VECFUNC(SCAL, VEC, VF, MASK, VABI_PREFIX, CC) \ |
| 1276 | {SCAL, VEC, VF, MASK, VABI_PREFIX, CC}, |
| 1277 | #include "llvm/Analysis/VecFuncs.def" |
| 1278 | #undef TLI_DEFINE_LIBMVEC_AARCH64_VECFUNCS |
| 1279 | }; |
| 1280 | |
| 1281 | static const VecDesc VecFuncs_MASSV[] = { |
| 1282 | #define TLI_DEFINE_MASSV_VECFUNCS |
| 1283 | #include "llvm/Analysis/VecFuncs.def" |
| 1284 | #undef TLI_DEFINE_MASSV_VECFUNCS |
| 1285 | }; |
| 1286 | |
| 1287 | static const VecDesc VecFuncs_SVML[] = { |
| 1288 | #define TLI_DEFINE_SVML_VECFUNCS |
| 1289 | #include "llvm/Analysis/VecFuncs.def" |
| 1290 | #undef TLI_DEFINE_SVML_VECFUNCS |
| 1291 | }; |
| 1292 | |
| 1293 | static const VecDesc VecFuncs_SLEEFGNUABI_VF2[] = { |
| 1294 | #define TLI_DEFINE_SLEEFGNUABI_VF2_VECFUNCS |
| 1295 | #define TLI_DEFINE_VECFUNC(SCAL, VEC, VF, VABI_PREFIX) \ |
| 1296 | {SCAL, VEC, VF, /* MASK = */ false, VABI_PREFIX, /* CC = */ std::nullopt}, |
| 1297 | #include "llvm/Analysis/VecFuncs.def" |
| 1298 | #undef TLI_DEFINE_SLEEFGNUABI_VF2_VECFUNCS |
| 1299 | }; |
| 1300 | static const VecDesc VecFuncs_SLEEFGNUABI_VF4[] = { |
| 1301 | #define TLI_DEFINE_SLEEFGNUABI_VF4_VECFUNCS |
| 1302 | #define TLI_DEFINE_VECFUNC(SCAL, VEC, VF, VABI_PREFIX) \ |
| 1303 | {SCAL, VEC, VF, /* MASK = */ false, VABI_PREFIX, /* CC = */ std::nullopt}, |
| 1304 | #include "llvm/Analysis/VecFuncs.def" |
| 1305 | #undef TLI_DEFINE_SLEEFGNUABI_VF4_VECFUNCS |
| 1306 | }; |
| 1307 | static const VecDesc VecFuncs_SLEEFGNUABI_VFScalable[] = { |
| 1308 | #define TLI_DEFINE_SLEEFGNUABI_SCALABLE_VECFUNCS |
| 1309 | #define TLI_DEFINE_VECFUNC(SCAL, VEC, VF, MASK, VABI_PREFIX) \ |
| 1310 | {SCAL, VEC, VF, MASK, VABI_PREFIX, /* CC = */ std::nullopt}, |
| 1311 | #include "llvm/Analysis/VecFuncs.def" |
| 1312 | #undef TLI_DEFINE_SLEEFGNUABI_SCALABLE_VECFUNCS |
| 1313 | }; |
| 1314 | |
| 1315 | static const VecDesc VecFuncs_SLEEFGNUABI_VFScalableRISCV[] = { |
| 1316 | #define TLI_DEFINE_SLEEFGNUABI_SCALABLE_VECFUNCS_RISCV |
| 1317 | #define TLI_DEFINE_VECFUNC(SCAL, VEC, VF, MASK, VABI_PREFIX) \ |
| 1318 | {SCAL, VEC, VF, MASK, VABI_PREFIX, /* CC = */ std::nullopt}, |
| 1319 | #include "llvm/Analysis/VecFuncs.def" |
| 1320 | #undef TLI_DEFINE_SLEEFGNUABI_SCALABLE_VECFUNCS_RISCV |
| 1321 | }; |
| 1322 | |
| 1323 | static const VecDesc VecFuncs_ArmPL[] = { |
| 1324 | #define TLI_DEFINE_ARMPL_VECFUNCS |
| 1325 | #define TLI_DEFINE_VECFUNC(SCAL, VEC, VF, MASK, VABI_PREFIX, CC) \ |
| 1326 | {SCAL, VEC, VF, MASK, VABI_PREFIX, CC}, |
| 1327 | #include "llvm/Analysis/VecFuncs.def" |
| 1328 | #undef TLI_DEFINE_ARMPL_VECFUNCS |
| 1329 | }; |
| 1330 | |
| 1331 | const VecDesc VecFuncs_AMDLIBM[] = { |
| 1332 | #define TLI_DEFINE_AMDLIBM_VECFUNCS |
| 1333 | #define TLI_DEFINE_VECFUNC(SCAL, VEC, VF, MASK, VABI_PREFIX) \ |
| 1334 | {SCAL, VEC, VF, MASK, VABI_PREFIX, /* CC = */ std::nullopt}, |
| 1335 | #include "llvm/Analysis/VecFuncs.def" |
| 1336 | #undef TLI_DEFINE_AMDLIBM_VECFUNCS |
| 1337 | }; |
| 1338 | |
| 1339 | void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib( |
| 1340 | enum VectorLibrary VecLib, const llvm::Triple &TargetTriple) { |
| 1341 | switch (VecLib) { |
| 1342 | case VectorLibrary::Accelerate: { |
| 1343 | addVectorizableFunctions(Fns: VecFuncs_Accelerate); |
| 1344 | break; |
| 1345 | } |
| 1346 | case VectorLibrary::DarwinLibSystemM: { |
| 1347 | addVectorizableFunctions(Fns: VecFuncs_DarwinLibSystemM); |
| 1348 | break; |
| 1349 | } |
| 1350 | case VectorLibrary::LIBMVEC: { |
| 1351 | switch (TargetTriple.getArch()) { |
| 1352 | default: |
| 1353 | break; |
| 1354 | case llvm::Triple::x86: |
| 1355 | case llvm::Triple::x86_64: |
| 1356 | addVectorizableFunctions(Fns: VecFuncs_LIBMVEC_X86); |
| 1357 | break; |
| 1358 | case llvm::Triple::aarch64: |
| 1359 | case llvm::Triple::aarch64_be: |
| 1360 | addVectorizableFunctions(Fns: VecFuncs_LIBMVEC_AARCH64); |
| 1361 | break; |
| 1362 | } |
| 1363 | break; |
| 1364 | } |
| 1365 | case VectorLibrary::MASSV: { |
| 1366 | addVectorizableFunctions(Fns: VecFuncs_MASSV); |
| 1367 | break; |
| 1368 | } |
| 1369 | case VectorLibrary::SVML: { |
| 1370 | addVectorizableFunctions(Fns: VecFuncs_SVML); |
| 1371 | break; |
| 1372 | } |
| 1373 | case VectorLibrary::SLEEFGNUABI: { |
| 1374 | switch (TargetTriple.getArch()) { |
| 1375 | default: |
| 1376 | break; |
| 1377 | case llvm::Triple::aarch64: |
| 1378 | case llvm::Triple::aarch64_be: |
| 1379 | addVectorizableFunctions(Fns: VecFuncs_SLEEFGNUABI_VF2); |
| 1380 | addVectorizableFunctions(Fns: VecFuncs_SLEEFGNUABI_VF4); |
| 1381 | addVectorizableFunctions(Fns: VecFuncs_SLEEFGNUABI_VFScalable); |
| 1382 | break; |
| 1383 | case llvm::Triple::riscv64: |
| 1384 | addVectorizableFunctions(Fns: VecFuncs_SLEEFGNUABI_VFScalableRISCV); |
| 1385 | break; |
| 1386 | } |
| 1387 | break; |
| 1388 | } |
| 1389 | case VectorLibrary::ArmPL: { |
| 1390 | switch (TargetTriple.getArch()) { |
| 1391 | default: |
| 1392 | break; |
| 1393 | case llvm::Triple::aarch64: |
| 1394 | case llvm::Triple::aarch64_be: |
| 1395 | addVectorizableFunctions(Fns: VecFuncs_ArmPL); |
| 1396 | break; |
| 1397 | } |
| 1398 | break; |
| 1399 | } |
| 1400 | case VectorLibrary::AMDLIBM: { |
| 1401 | addVectorizableFunctions(Fns: VecFuncs_AMDLIBM); |
| 1402 | break; |
| 1403 | } |
| 1404 | case VectorLibrary::NoLibrary: |
| 1405 | break; |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const { |
| 1410 | funcName = sanitizeFunctionName(funcName); |
| 1411 | if (funcName.empty()) |
| 1412 | return false; |
| 1413 | |
| 1414 | std::vector<VecDesc>::const_iterator I = |
| 1415 | llvm::lower_bound(Range: VectorDescs, Value&: funcName, C: compareWithScalarFnName); |
| 1416 | return I != VectorDescs.end() && StringRef(I->getScalarFnName()) == funcName; |
| 1417 | } |
| 1418 | |
| 1419 | StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F, |
| 1420 | const ElementCount &VF, |
| 1421 | bool Masked) const { |
| 1422 | const VecDesc *VD = getVectorMappingInfo(F, VF, Masked); |
| 1423 | if (VD) |
| 1424 | return VD->getVectorFnName(); |
| 1425 | return StringRef(); |
| 1426 | } |
| 1427 | |
| 1428 | const VecDesc * |
| 1429 | TargetLibraryInfoImpl::getVectorMappingInfo(StringRef F, const ElementCount &VF, |
| 1430 | bool Masked) const { |
| 1431 | F = sanitizeFunctionName(funcName: F); |
| 1432 | if (F.empty()) |
| 1433 | return nullptr; |
| 1434 | std::vector<VecDesc>::const_iterator I = |
| 1435 | llvm::lower_bound(Range: VectorDescs, Value&: F, C: compareWithScalarFnName); |
| 1436 | while (I != VectorDescs.end() && StringRef(I->getScalarFnName()) == F) { |
| 1437 | if ((I->getVectorizationFactor() == VF) && (I->isMasked() == Masked)) |
| 1438 | return &(*I); |
| 1439 | ++I; |
| 1440 | } |
| 1441 | return nullptr; |
| 1442 | } |
| 1443 | |
| 1444 | TargetLibraryInfo TargetLibraryAnalysis::run(const Function &F, |
| 1445 | FunctionAnalysisManager &) { |
| 1446 | if (!BaselineInfoImpl) |
| 1447 | BaselineInfoImpl = TargetLibraryInfoImpl(F.getParent()->getTargetTriple()); |
| 1448 | return TargetLibraryInfo(*BaselineInfoImpl, &F); |
| 1449 | } |
| 1450 | |
| 1451 | unsigned TargetLibraryInfoImpl::getWCharSize(const Module &M) const { |
| 1452 | if (auto *ShortWChar = cast_or_null<ConstantAsMetadata>( |
| 1453 | Val: M.getModuleFlag(Key: "wchar_size" ))) |
| 1454 | return cast<ConstantInt>(Val: ShortWChar->getValue())->getZExtValue(); |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | unsigned TargetLibraryInfoImpl::getSizeTSize(const Module &M) const { |
| 1459 | // There is really no guarantee that sizeof(size_t) is equal to the index |
| 1460 | // size of the default address space. If that isn't true then it should be |
| 1461 | // possible to derive the SizeTTy from the target triple here instead and do |
| 1462 | // an early return. |
| 1463 | |
| 1464 | // Hard coding address space zero may seem unfortunate, but a number of |
| 1465 | // configurations of common targets (i386, x86-64 x32, aarch64 x32, possibly |
| 1466 | // others) have larger-than-size_t index sizes on non-default address spaces, |
| 1467 | // making this the best default. |
| 1468 | return M.getDataLayout().getIndexSizeInBits(/*AddressSpace=*/AS: 0); |
| 1469 | } |
| 1470 | |
| 1471 | TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass() |
| 1472 | : ImmutablePass(ID), TLA(TargetLibraryInfoImpl(Triple())) {} |
| 1473 | |
| 1474 | TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T) |
| 1475 | : ImmutablePass(ID), TLA(TargetLibraryInfoImpl(T)) {} |
| 1476 | |
| 1477 | TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass( |
| 1478 | const TargetLibraryInfoImpl &TLIImpl) |
| 1479 | : ImmutablePass(ID), TLA(TLIImpl) {} |
| 1480 | |
| 1481 | TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass( |
| 1482 | const TargetLibraryInfo &TLIOther) |
| 1483 | : TargetLibraryInfoWrapperPass(*TLIOther.Impl) {} |
| 1484 | |
| 1485 | AnalysisKey TargetLibraryAnalysis::Key; |
| 1486 | |
| 1487 | // Register the basic pass. |
| 1488 | INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo" , |
| 1489 | "Target Library Information" , false, true) |
| 1490 | char TargetLibraryInfoWrapperPass::ID = 0; |
| 1491 | |
| 1492 | void TargetLibraryInfoWrapperPass::anchor() {} |
| 1493 | |
| 1494 | void TargetLibraryInfoImpl::getWidestVF(StringRef ScalarF, |
| 1495 | ElementCount &FixedVF, |
| 1496 | ElementCount &ScalableVF) const { |
| 1497 | ScalarF = sanitizeFunctionName(funcName: ScalarF); |
| 1498 | // Use '0' here because a type of the form <vscale x 1 x ElTy> is not the |
| 1499 | // same as a scalar. |
| 1500 | ScalableVF = ElementCount::getScalable(MinVal: 0); |
| 1501 | FixedVF = ElementCount::getFixed(MinVal: 1); |
| 1502 | if (ScalarF.empty()) |
| 1503 | return; |
| 1504 | |
| 1505 | std::vector<VecDesc>::const_iterator I = |
| 1506 | llvm::lower_bound(Range: VectorDescs, Value&: ScalarF, C: compareWithScalarFnName); |
| 1507 | while (I != VectorDescs.end() && StringRef(I->getScalarFnName()) == ScalarF) { |
| 1508 | ElementCount *VF = |
| 1509 | I->getVectorizationFactor().isScalable() ? &ScalableVF : &FixedVF; |
| 1510 | if (ElementCount::isKnownGT(LHS: I->getVectorizationFactor(), RHS: *VF)) |
| 1511 | *VF = I->getVectorizationFactor(); |
| 1512 | ++I; |
| 1513 | } |
| 1514 | } |
| 1515 | |