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