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