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 36 StringLiteral const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] = 37 { 38 #define TLI_DEFINE_STRING 39 #include "llvm/Analysis/TargetLibraryInfo.def" 40 }; 41 42 static bool hasSinCosPiStret(const Triple &T) { 43 // Only Darwin variants have _stret versions of combined trig functions. 44 if (!T.isOSDarwin()) 45 return false; 46 47 // The ABI is rather complicated on x86, so don't do anything special there. 48 if (T.getArch() == Triple::x86) 49 return false; 50 51 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9)) 52 return false; 53 54 if (T.isiOS() && T.isOSVersionLT(7, 0)) 55 return false; 56 57 return true; 58 } 59 60 static bool hasBcmp(const Triple &TT) { 61 // Posix removed support from bcmp() in 2001, but the glibc and several 62 // implementations of the libc still have it. 63 if (TT.isOSLinux()) 64 return TT.isGNUEnvironment() || TT.isMusl(); 65 // Both NetBSD and OpenBSD are planning to remove the function. Windows does 66 // not have it. 67 return TT.isOSFreeBSD() || TT.isOSSolaris(); 68 } 69 70 static bool isCallingConvCCompatible(CallingConv::ID CC, StringRef TT, 71 FunctionType *FuncTy) { 72 switch (CC) { 73 default: 74 return false; 75 case llvm::CallingConv::C: 76 return true; 77 case llvm::CallingConv::ARM_APCS: 78 case llvm::CallingConv::ARM_AAPCS: 79 case llvm::CallingConv::ARM_AAPCS_VFP: { 80 81 // The iOS ABI diverges from the standard in some cases, so for now don't 82 // try to simplify those calls. 83 if (Triple(TT).isiOS()) 84 return false; 85 86 if (!FuncTy->getReturnType()->isPointerTy() && 87 !FuncTy->getReturnType()->isIntegerTy() && 88 !FuncTy->getReturnType()->isVoidTy()) 89 return false; 90 91 for (auto *Param : FuncTy->params()) { 92 if (!Param->isPointerTy() && !Param->isIntegerTy()) 93 return false; 94 } 95 return true; 96 } 97 } 98 return false; 99 } 100 101 bool TargetLibraryInfoImpl::isCallingConvCCompatible(CallBase *CI) { 102 return ::isCallingConvCCompatible(CI->getCallingConv(), 103 CI->getModule()->getTargetTriple(), 104 CI->getFunctionType()); 105 } 106 107 bool TargetLibraryInfoImpl::isCallingConvCCompatible(Function *F) { 108 return ::isCallingConvCCompatible(F->getCallingConv(), 109 F->getParent()->getTargetTriple(), 110 F->getFunctionType()); 111 } 112 113 /// Initialize the set of available library functions based on the specified 114 /// target triple. This should be carefully written so that a missing target 115 /// triple gets a sane set of defaults. 116 static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, 117 ArrayRef<StringLiteral> StandardNames) { 118 // Verify that the StandardNames array is in alphabetical order. 119 assert( 120 llvm::is_sorted(StandardNames, 121 [](StringRef LHS, StringRef RHS) { return LHS < RHS; }) && 122 "TargetLibraryInfoImpl function names must be sorted"); 123 124 // Set IO unlocked variants as unavailable 125 // Set them as available per system below 126 TLI.setUnavailable(LibFunc_getchar_unlocked); 127 TLI.setUnavailable(LibFunc_putc_unlocked); 128 TLI.setUnavailable(LibFunc_putchar_unlocked); 129 TLI.setUnavailable(LibFunc_fputc_unlocked); 130 TLI.setUnavailable(LibFunc_fgetc_unlocked); 131 TLI.setUnavailable(LibFunc_fread_unlocked); 132 TLI.setUnavailable(LibFunc_fwrite_unlocked); 133 TLI.setUnavailable(LibFunc_fputs_unlocked); 134 TLI.setUnavailable(LibFunc_fgets_unlocked); 135 136 bool ShouldExtI32Param = false, ShouldExtI32Return = false, 137 ShouldSignExtI32Param = false; 138 // PowerPC64, Sparc64, SystemZ need signext/zeroext on i32 parameters and 139 // returns corresponding to C-level ints and unsigned ints. 140 if (T.isPPC64() || T.getArch() == Triple::sparcv9 || 141 T.getArch() == Triple::systemz) { 142 ShouldExtI32Param = true; 143 ShouldExtI32Return = true; 144 } 145 // Mips, on the other hand, needs signext on i32 parameters corresponding 146 // to both signed and unsigned ints. 147 if (T.isMIPS()) { 148 ShouldSignExtI32Param = true; 149 } 150 TLI.setShouldExtI32Param(ShouldExtI32Param); 151 TLI.setShouldExtI32Return(ShouldExtI32Return); 152 TLI.setShouldSignExtI32Param(ShouldSignExtI32Param); 153 154 // Let's assume by default that the size of int is 32 bits, unless the target 155 // is a 16-bit architecture because then it most likely is 16 bits. If that 156 // isn't true for a target those defaults should be overridden below. 157 TLI.setIntSize(T.isArch16Bit() ? 16 : 32); 158 159 if (T.isAMDGPU()) 160 TLI.disableAllFunctions(); 161 162 // There are no library implementations of memcpy and memset for AMD gpus and 163 // these can be difficult to lower in the backend. 164 if (T.isAMDGPU()) { 165 TLI.setUnavailable(LibFunc_memcpy); 166 TLI.setUnavailable(LibFunc_memset); 167 TLI.setUnavailable(LibFunc_memset_pattern16); 168 TLI.setAvailable(llvm::LibFunc___kmpc_alloc_shared); 169 TLI.setAvailable(llvm::LibFunc___kmpc_free_shared); 170 return; 171 } 172 173 // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later. 174 // All versions of watchOS support it. 175 if (T.isMacOSX()) { 176 // available IO unlocked variants on Mac OS X 177 TLI.setAvailable(LibFunc_getc_unlocked); 178 TLI.setAvailable(LibFunc_getchar_unlocked); 179 TLI.setAvailable(LibFunc_putc_unlocked); 180 TLI.setAvailable(LibFunc_putchar_unlocked); 181 182 if (T.isMacOSXVersionLT(10, 5)) 183 TLI.setUnavailable(LibFunc_memset_pattern16); 184 } else if (T.isiOS()) { 185 if (T.isOSVersionLT(3, 0)) 186 TLI.setUnavailable(LibFunc_memset_pattern16); 187 } else if (!T.isWatchOS()) { 188 TLI.setUnavailable(LibFunc_memset_pattern16); 189 } 190 191 if (!hasSinCosPiStret(T)) { 192 TLI.setUnavailable(LibFunc_sinpi); 193 TLI.setUnavailable(LibFunc_sinpif); 194 TLI.setUnavailable(LibFunc_cospi); 195 TLI.setUnavailable(LibFunc_cospif); 196 TLI.setUnavailable(LibFunc_sincospi_stret); 197 TLI.setUnavailable(LibFunc_sincospif_stret); 198 } 199 200 if (!hasBcmp(T)) 201 TLI.setUnavailable(LibFunc_bcmp); 202 203 if (T.isMacOSX() && T.getArch() == Triple::x86 && 204 !T.isMacOSXVersionLT(10, 7)) { 205 // x86-32 OSX has a scheme where fwrite and fputs (and some other functions 206 // we don't care about) have two versions; on recent OSX, the one we want 207 // has a $UNIX2003 suffix. The two implementations are identical except 208 // for the return value in some edge cases. However, we don't want to 209 // generate code that depends on the old symbols. 210 TLI.setAvailableWithName(LibFunc_fwrite, "fwrite$UNIX2003"); 211 TLI.setAvailableWithName(LibFunc_fputs, "fputs$UNIX2003"); 212 } 213 214 // iprintf and friends are only available on XCore, TCE, and Emscripten. 215 if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce && 216 T.getOS() != Triple::Emscripten) { 217 TLI.setUnavailable(LibFunc_iprintf); 218 TLI.setUnavailable(LibFunc_siprintf); 219 TLI.setUnavailable(LibFunc_fiprintf); 220 } 221 222 // __small_printf and friends are only available on Emscripten. 223 if (T.getOS() != Triple::Emscripten) { 224 TLI.setUnavailable(LibFunc_small_printf); 225 TLI.setUnavailable(LibFunc_small_sprintf); 226 TLI.setUnavailable(LibFunc_small_fprintf); 227 } 228 229 if (T.isOSWindows() && !T.isOSCygMing()) { 230 // XXX: The earliest documentation available at the moment is for VS2015/VC19: 231 // https://docs.microsoft.com/en-us/cpp/c-runtime-library/floating-point-support?view=vs-2015 232 // XXX: In order to use an MSVCRT older than VC19, 233 // the specific library version must be explicit in the target triple, 234 // e.g., x86_64-pc-windows-msvc18. 235 bool hasPartialC99 = true; 236 if (T.isKnownWindowsMSVCEnvironment()) { 237 unsigned Major, Minor, Micro; 238 T.getEnvironmentVersion(Major, Minor, Micro); 239 hasPartialC99 = (Major == 0 || Major >= 19); 240 } 241 242 // Latest targets support C89 math functions, in part. 243 bool isARM = (T.getArch() == Triple::aarch64 || 244 T.getArch() == Triple::arm); 245 bool hasPartialFloat = (isARM || 246 T.getArch() == Triple::x86_64); 247 248 // Win32 does not support float C89 math functions, in general. 249 if (!hasPartialFloat) { 250 TLI.setUnavailable(LibFunc_acosf); 251 TLI.setUnavailable(LibFunc_asinf); 252 TLI.setUnavailable(LibFunc_atan2f); 253 TLI.setUnavailable(LibFunc_atanf); 254 TLI.setUnavailable(LibFunc_ceilf); 255 TLI.setUnavailable(LibFunc_cosf); 256 TLI.setUnavailable(LibFunc_coshf); 257 TLI.setUnavailable(LibFunc_expf); 258 TLI.setUnavailable(LibFunc_floorf); 259 TLI.setUnavailable(LibFunc_fmodf); 260 TLI.setUnavailable(LibFunc_log10f); 261 TLI.setUnavailable(LibFunc_logf); 262 TLI.setUnavailable(LibFunc_modff); 263 TLI.setUnavailable(LibFunc_powf); 264 TLI.setUnavailable(LibFunc_remainderf); 265 TLI.setUnavailable(LibFunc_sinf); 266 TLI.setUnavailable(LibFunc_sinhf); 267 TLI.setUnavailable(LibFunc_sqrtf); 268 TLI.setUnavailable(LibFunc_tanf); 269 TLI.setUnavailable(LibFunc_tanhf); 270 } 271 if (!isARM) 272 TLI.setUnavailable(LibFunc_fabsf); 273 TLI.setUnavailable(LibFunc_frexpf); 274 TLI.setUnavailable(LibFunc_ldexpf); 275 276 // Win32 does not support long double C89 math functions. 277 TLI.setUnavailable(LibFunc_acosl); 278 TLI.setUnavailable(LibFunc_asinl); 279 TLI.setUnavailable(LibFunc_atan2l); 280 TLI.setUnavailable(LibFunc_atanl); 281 TLI.setUnavailable(LibFunc_ceill); 282 TLI.setUnavailable(LibFunc_cosl); 283 TLI.setUnavailable(LibFunc_coshl); 284 TLI.setUnavailable(LibFunc_expl); 285 TLI.setUnavailable(LibFunc_fabsl); 286 TLI.setUnavailable(LibFunc_floorl); 287 TLI.setUnavailable(LibFunc_fmodl); 288 TLI.setUnavailable(LibFunc_frexpl); 289 TLI.setUnavailable(LibFunc_ldexpl); 290 TLI.setUnavailable(LibFunc_log10l); 291 TLI.setUnavailable(LibFunc_logl); 292 TLI.setUnavailable(LibFunc_modfl); 293 TLI.setUnavailable(LibFunc_powl); 294 TLI.setUnavailable(LibFunc_remainderl); 295 TLI.setUnavailable(LibFunc_sinl); 296 TLI.setUnavailable(LibFunc_sinhl); 297 TLI.setUnavailable(LibFunc_sqrtl); 298 TLI.setUnavailable(LibFunc_tanl); 299 TLI.setUnavailable(LibFunc_tanhl); 300 301 // Win32 does not fully support C99 math functions. 302 if (!hasPartialC99) { 303 TLI.setUnavailable(LibFunc_acosh); 304 TLI.setUnavailable(LibFunc_acoshf); 305 TLI.setUnavailable(LibFunc_asinh); 306 TLI.setUnavailable(LibFunc_asinhf); 307 TLI.setUnavailable(LibFunc_atanh); 308 TLI.setUnavailable(LibFunc_atanhf); 309 TLI.setAvailableWithName(LibFunc_cabs, "_cabs"); 310 TLI.setUnavailable(LibFunc_cabsf); 311 TLI.setUnavailable(LibFunc_cbrt); 312 TLI.setUnavailable(LibFunc_cbrtf); 313 TLI.setAvailableWithName(LibFunc_copysign, "_copysign"); 314 TLI.setAvailableWithName(LibFunc_copysignf, "_copysignf"); 315 TLI.setUnavailable(LibFunc_exp2); 316 TLI.setUnavailable(LibFunc_exp2f); 317 TLI.setUnavailable(LibFunc_expm1); 318 TLI.setUnavailable(LibFunc_expm1f); 319 TLI.setUnavailable(LibFunc_fmax); 320 TLI.setUnavailable(LibFunc_fmaxf); 321 TLI.setUnavailable(LibFunc_fmin); 322 TLI.setUnavailable(LibFunc_fminf); 323 TLI.setUnavailable(LibFunc_log1p); 324 TLI.setUnavailable(LibFunc_log1pf); 325 TLI.setUnavailable(LibFunc_log2); 326 TLI.setUnavailable(LibFunc_log2f); 327 TLI.setAvailableWithName(LibFunc_logb, "_logb"); 328 if (hasPartialFloat) 329 TLI.setAvailableWithName(LibFunc_logbf, "_logbf"); 330 else 331 TLI.setUnavailable(LibFunc_logbf); 332 TLI.setUnavailable(LibFunc_rint); 333 TLI.setUnavailable(LibFunc_rintf); 334 TLI.setUnavailable(LibFunc_round); 335 TLI.setUnavailable(LibFunc_roundf); 336 TLI.setUnavailable(LibFunc_trunc); 337 TLI.setUnavailable(LibFunc_truncf); 338 } 339 340 // Win32 does not support long double C99 math functions. 341 TLI.setUnavailable(LibFunc_acoshl); 342 TLI.setUnavailable(LibFunc_asinhl); 343 TLI.setUnavailable(LibFunc_atanhl); 344 TLI.setUnavailable(LibFunc_cabsl); 345 TLI.setUnavailable(LibFunc_cbrtl); 346 TLI.setUnavailable(LibFunc_copysignl); 347 TLI.setUnavailable(LibFunc_exp2l); 348 TLI.setUnavailable(LibFunc_expm1l); 349 TLI.setUnavailable(LibFunc_fmaxl); 350 TLI.setUnavailable(LibFunc_fminl); 351 TLI.setUnavailable(LibFunc_log1pl); 352 TLI.setUnavailable(LibFunc_log2l); 353 TLI.setUnavailable(LibFunc_logbl); 354 TLI.setUnavailable(LibFunc_nearbyintl); 355 TLI.setUnavailable(LibFunc_rintl); 356 TLI.setUnavailable(LibFunc_roundl); 357 TLI.setUnavailable(LibFunc_truncl); 358 359 // Win32 does not support these functions, but 360 // they are generally available on POSIX-compliant systems. 361 TLI.setUnavailable(LibFunc_access); 362 TLI.setUnavailable(LibFunc_chmod); 363 TLI.setUnavailable(LibFunc_closedir); 364 TLI.setUnavailable(LibFunc_fdopen); 365 TLI.setUnavailable(LibFunc_fileno); 366 TLI.setUnavailable(LibFunc_fseeko); 367 TLI.setUnavailable(LibFunc_fstat); 368 TLI.setUnavailable(LibFunc_ftello); 369 TLI.setUnavailable(LibFunc_gettimeofday); 370 TLI.setUnavailable(LibFunc_memccpy); 371 TLI.setUnavailable(LibFunc_mkdir); 372 TLI.setUnavailable(LibFunc_open); 373 TLI.setUnavailable(LibFunc_opendir); 374 TLI.setUnavailable(LibFunc_pclose); 375 TLI.setUnavailable(LibFunc_popen); 376 TLI.setUnavailable(LibFunc_read); 377 TLI.setUnavailable(LibFunc_rmdir); 378 TLI.setUnavailable(LibFunc_stat); 379 TLI.setUnavailable(LibFunc_strcasecmp); 380 TLI.setUnavailable(LibFunc_strncasecmp); 381 TLI.setUnavailable(LibFunc_unlink); 382 TLI.setUnavailable(LibFunc_utime); 383 TLI.setUnavailable(LibFunc_write); 384 } 385 386 if (T.isOSWindows() && !T.isWindowsCygwinEnvironment()) { 387 // These functions aren't available in either MSVC or MinGW environments. 388 TLI.setUnavailable(LibFunc_bcmp); 389 TLI.setUnavailable(LibFunc_bcopy); 390 TLI.setUnavailable(LibFunc_bzero); 391 TLI.setUnavailable(LibFunc_chown); 392 TLI.setUnavailable(LibFunc_ctermid); 393 TLI.setUnavailable(LibFunc_ffs); 394 TLI.setUnavailable(LibFunc_flockfile); 395 TLI.setUnavailable(LibFunc_fstatvfs); 396 TLI.setUnavailable(LibFunc_ftrylockfile); 397 TLI.setUnavailable(LibFunc_funlockfile); 398 TLI.setUnavailable(LibFunc_getitimer); 399 TLI.setUnavailable(LibFunc_getlogin_r); 400 TLI.setUnavailable(LibFunc_getpwnam); 401 TLI.setUnavailable(LibFunc_htonl); 402 TLI.setUnavailable(LibFunc_htons); 403 TLI.setUnavailable(LibFunc_lchown); 404 TLI.setUnavailable(LibFunc_lstat); 405 TLI.setUnavailable(LibFunc_ntohl); 406 TLI.setUnavailable(LibFunc_ntohs); 407 TLI.setUnavailable(LibFunc_pread); 408 TLI.setUnavailable(LibFunc_pwrite); 409 TLI.setUnavailable(LibFunc_readlink); 410 TLI.setUnavailable(LibFunc_realpath); 411 TLI.setUnavailable(LibFunc_setitimer); 412 TLI.setUnavailable(LibFunc_statvfs); 413 TLI.setUnavailable(LibFunc_stpcpy); 414 TLI.setUnavailable(LibFunc_stpncpy); 415 TLI.setUnavailable(LibFunc_times); 416 TLI.setUnavailable(LibFunc_uname); 417 TLI.setUnavailable(LibFunc_unsetenv); 418 TLI.setUnavailable(LibFunc_utimes); 419 } 420 421 switch (T.getOS()) { 422 case Triple::MacOSX: 423 // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0 424 // and their names are __exp10 and __exp10f. exp10l is not available on 425 // OS X or iOS. 426 TLI.setUnavailable(LibFunc_exp10l); 427 if (T.isMacOSXVersionLT(10, 9)) { 428 TLI.setUnavailable(LibFunc_exp10); 429 TLI.setUnavailable(LibFunc_exp10f); 430 } else { 431 TLI.setAvailableWithName(LibFunc_exp10, "__exp10"); 432 TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f"); 433 } 434 break; 435 case Triple::IOS: 436 case Triple::TvOS: 437 case Triple::WatchOS: 438 TLI.setUnavailable(LibFunc_exp10l); 439 if (!T.isWatchOS() && 440 (T.isOSVersionLT(7, 0) || (T.isOSVersionLT(9, 0) && T.isX86()))) { 441 TLI.setUnavailable(LibFunc_exp10); 442 TLI.setUnavailable(LibFunc_exp10f); 443 } else { 444 TLI.setAvailableWithName(LibFunc_exp10, "__exp10"); 445 TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f"); 446 } 447 break; 448 case Triple::Linux: 449 // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely 450 // buggy prior to glibc version 2.18. Until this version is widely deployed 451 // or we have a reasonable detection strategy, we cannot use exp10 reliably 452 // on Linux. 453 // 454 // Fall through to disable all of them. 455 LLVM_FALLTHROUGH; 456 default: 457 TLI.setUnavailable(LibFunc_exp10); 458 TLI.setUnavailable(LibFunc_exp10f); 459 TLI.setUnavailable(LibFunc_exp10l); 460 } 461 462 // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and 463 // Linux (GLIBC): 464 // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html 465 // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c 466 // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html 467 switch (T.getOS()) { 468 case Triple::Darwin: 469 case Triple::MacOSX: 470 case Triple::IOS: 471 case Triple::TvOS: 472 case Triple::WatchOS: 473 case Triple::FreeBSD: 474 case Triple::Linux: 475 break; 476 default: 477 TLI.setUnavailable(LibFunc_ffsl); 478 } 479 480 // ffsll is available on at least FreeBSD and Linux (GLIBC): 481 // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c 482 // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html 483 switch (T.getOS()) { 484 case Triple::Darwin: 485 case Triple::MacOSX: 486 case Triple::IOS: 487 case Triple::TvOS: 488 case Triple::WatchOS: 489 case Triple::FreeBSD: 490 case Triple::Linux: 491 break; 492 default: 493 TLI.setUnavailable(LibFunc_ffsll); 494 } 495 496 // The following functions are available on at least FreeBSD: 497 // http://svn.freebsd.org/base/head/lib/libc/string/fls.c 498 // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c 499 // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c 500 if (!T.isOSFreeBSD()) { 501 TLI.setUnavailable(LibFunc_fls); 502 TLI.setUnavailable(LibFunc_flsl); 503 TLI.setUnavailable(LibFunc_flsll); 504 } 505 506 // The following functions are only available on GNU/Linux (using glibc). 507 // Linux variants without glibc (eg: bionic, musl) may have some subset. 508 if (!T.isOSLinux() || !T.isGNUEnvironment()) { 509 TLI.setUnavailable(LibFunc_dunder_strdup); 510 TLI.setUnavailable(LibFunc_dunder_strtok_r); 511 TLI.setUnavailable(LibFunc_dunder_isoc99_scanf); 512 TLI.setUnavailable(LibFunc_dunder_isoc99_sscanf); 513 TLI.setUnavailable(LibFunc_under_IO_getc); 514 TLI.setUnavailable(LibFunc_under_IO_putc); 515 // But, Android and musl have memalign. 516 if (!T.isAndroid() && !T.isMusl()) 517 TLI.setUnavailable(LibFunc_memalign); 518 TLI.setUnavailable(LibFunc_fopen64); 519 TLI.setUnavailable(LibFunc_fseeko64); 520 TLI.setUnavailable(LibFunc_fstat64); 521 TLI.setUnavailable(LibFunc_fstatvfs64); 522 TLI.setUnavailable(LibFunc_ftello64); 523 TLI.setUnavailable(LibFunc_lstat64); 524 TLI.setUnavailable(LibFunc_open64); 525 TLI.setUnavailable(LibFunc_stat64); 526 TLI.setUnavailable(LibFunc_statvfs64); 527 TLI.setUnavailable(LibFunc_tmpfile64); 528 529 // Relaxed math functions are included in math-finite.h on Linux (GLIBC). 530 // Note that math-finite.h is no longer supported by top-of-tree GLIBC, 531 // so we keep these functions around just so that they're recognized by 532 // the ConstantFolder. 533 TLI.setUnavailable(LibFunc_acos_finite); 534 TLI.setUnavailable(LibFunc_acosf_finite); 535 TLI.setUnavailable(LibFunc_acosl_finite); 536 TLI.setUnavailable(LibFunc_acosh_finite); 537 TLI.setUnavailable(LibFunc_acoshf_finite); 538 TLI.setUnavailable(LibFunc_acoshl_finite); 539 TLI.setUnavailable(LibFunc_asin_finite); 540 TLI.setUnavailable(LibFunc_asinf_finite); 541 TLI.setUnavailable(LibFunc_asinl_finite); 542 TLI.setUnavailable(LibFunc_atan2_finite); 543 TLI.setUnavailable(LibFunc_atan2f_finite); 544 TLI.setUnavailable(LibFunc_atan2l_finite); 545 TLI.setUnavailable(LibFunc_atanh_finite); 546 TLI.setUnavailable(LibFunc_atanhf_finite); 547 TLI.setUnavailable(LibFunc_atanhl_finite); 548 TLI.setUnavailable(LibFunc_cosh_finite); 549 TLI.setUnavailable(LibFunc_coshf_finite); 550 TLI.setUnavailable(LibFunc_coshl_finite); 551 TLI.setUnavailable(LibFunc_exp10_finite); 552 TLI.setUnavailable(LibFunc_exp10f_finite); 553 TLI.setUnavailable(LibFunc_exp10l_finite); 554 TLI.setUnavailable(LibFunc_exp2_finite); 555 TLI.setUnavailable(LibFunc_exp2f_finite); 556 TLI.setUnavailable(LibFunc_exp2l_finite); 557 TLI.setUnavailable(LibFunc_exp_finite); 558 TLI.setUnavailable(LibFunc_expf_finite); 559 TLI.setUnavailable(LibFunc_expl_finite); 560 TLI.setUnavailable(LibFunc_log10_finite); 561 TLI.setUnavailable(LibFunc_log10f_finite); 562 TLI.setUnavailable(LibFunc_log10l_finite); 563 TLI.setUnavailable(LibFunc_log2_finite); 564 TLI.setUnavailable(LibFunc_log2f_finite); 565 TLI.setUnavailable(LibFunc_log2l_finite); 566 TLI.setUnavailable(LibFunc_log_finite); 567 TLI.setUnavailable(LibFunc_logf_finite); 568 TLI.setUnavailable(LibFunc_logl_finite); 569 TLI.setUnavailable(LibFunc_pow_finite); 570 TLI.setUnavailable(LibFunc_powf_finite); 571 TLI.setUnavailable(LibFunc_powl_finite); 572 TLI.setUnavailable(LibFunc_sinh_finite); 573 TLI.setUnavailable(LibFunc_sinhf_finite); 574 TLI.setUnavailable(LibFunc_sinhl_finite); 575 } 576 577 if ((T.isOSLinux() && T.isGNUEnvironment()) || 578 (T.isAndroid() && !T.isAndroidVersionLT(28))) { 579 // available IO unlocked variants on GNU/Linux and Android P or later 580 TLI.setAvailable(LibFunc_getc_unlocked); 581 TLI.setAvailable(LibFunc_getchar_unlocked); 582 TLI.setAvailable(LibFunc_putc_unlocked); 583 TLI.setAvailable(LibFunc_putchar_unlocked); 584 TLI.setAvailable(LibFunc_fputc_unlocked); 585 TLI.setAvailable(LibFunc_fgetc_unlocked); 586 TLI.setAvailable(LibFunc_fread_unlocked); 587 TLI.setAvailable(LibFunc_fwrite_unlocked); 588 TLI.setAvailable(LibFunc_fputs_unlocked); 589 TLI.setAvailable(LibFunc_fgets_unlocked); 590 } 591 592 // As currently implemented in clang, NVPTX code has no standard library to 593 // speak of. Headers provide a standard-ish library implementation, but many 594 // of the signatures are wrong -- for example, many libm functions are not 595 // extern "C". 596 // 597 // libdevice, an IR library provided by nvidia, is linked in by the front-end, 598 // but only used functions are provided to llvm. Moreover, most of the 599 // functions in libdevice don't map precisely to standard library functions. 600 // 601 // FIXME: Having no standard library prevents e.g. many fastmath 602 // optimizations, so this situation should be fixed. 603 if (T.isNVPTX()) { 604 TLI.disableAllFunctions(); 605 TLI.setAvailable(LibFunc_nvvm_reflect); 606 TLI.setAvailable(llvm::LibFunc_malloc); 607 TLI.setAvailable(llvm::LibFunc_free); 608 609 // TODO: We could enable the following two according to [0] but we haven't 610 // done an evaluation wrt. the performance implications. 611 // [0] 612 // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#dynamic-global-memory-allocation-and-operations 613 // 614 // TLI.setAvailable(llvm::LibFunc_memcpy); 615 // TLI.setAvailable(llvm::LibFunc_memset); 616 617 TLI.setAvailable(llvm::LibFunc___kmpc_alloc_shared); 618 TLI.setAvailable(llvm::LibFunc___kmpc_free_shared); 619 } else { 620 TLI.setUnavailable(LibFunc_nvvm_reflect); 621 } 622 623 // These vec_malloc/free routines are only available on AIX. 624 if (!T.isOSAIX()) { 625 TLI.setUnavailable(LibFunc_vec_calloc); 626 TLI.setUnavailable(LibFunc_vec_malloc); 627 TLI.setUnavailable(LibFunc_vec_realloc); 628 TLI.setUnavailable(LibFunc_vec_free); 629 } 630 631 TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary); 632 } 633 634 TargetLibraryInfoImpl::TargetLibraryInfoImpl() { 635 // Default to everything being available. 636 memset(AvailableArray, -1, sizeof(AvailableArray)); 637 638 initialize(*this, Triple(), StandardNames); 639 } 640 641 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) { 642 // Default to everything being available. 643 memset(AvailableArray, -1, sizeof(AvailableArray)); 644 645 initialize(*this, T, StandardNames); 646 } 647 648 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI) 649 : CustomNames(TLI.CustomNames), ShouldExtI32Param(TLI.ShouldExtI32Param), 650 ShouldExtI32Return(TLI.ShouldExtI32Return), 651 ShouldSignExtI32Param(TLI.ShouldSignExtI32Param), 652 SizeOfInt(TLI.SizeOfInt) { 653 memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray)); 654 VectorDescs = TLI.VectorDescs; 655 ScalarDescs = TLI.ScalarDescs; 656 } 657 658 TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI) 659 : CustomNames(std::move(TLI.CustomNames)), 660 ShouldExtI32Param(TLI.ShouldExtI32Param), 661 ShouldExtI32Return(TLI.ShouldExtI32Return), 662 ShouldSignExtI32Param(TLI.ShouldSignExtI32Param), 663 SizeOfInt(TLI.SizeOfInt) { 664 std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray), 665 AvailableArray); 666 VectorDescs = TLI.VectorDescs; 667 ScalarDescs = TLI.ScalarDescs; 668 } 669 670 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) { 671 CustomNames = TLI.CustomNames; 672 ShouldExtI32Param = TLI.ShouldExtI32Param; 673 ShouldExtI32Return = TLI.ShouldExtI32Return; 674 ShouldSignExtI32Param = TLI.ShouldSignExtI32Param; 675 SizeOfInt = TLI.SizeOfInt; 676 memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray)); 677 return *this; 678 } 679 680 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) { 681 CustomNames = std::move(TLI.CustomNames); 682 ShouldExtI32Param = TLI.ShouldExtI32Param; 683 ShouldExtI32Return = TLI.ShouldExtI32Return; 684 ShouldSignExtI32Param = TLI.ShouldSignExtI32Param; 685 SizeOfInt = TLI.SizeOfInt; 686 std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray), 687 AvailableArray); 688 return *this; 689 } 690 691 static StringRef sanitizeFunctionName(StringRef funcName) { 692 // Filter out empty names and names containing null bytes, those can't be in 693 // our table. 694 if (funcName.empty() || funcName.find('\0') != StringRef::npos) 695 return StringRef(); 696 697 // Check for \01 prefix that is used to mangle __asm declarations and 698 // strip it if present. 699 return GlobalValue::dropLLVMManglingEscape(funcName); 700 } 701 702 bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName, LibFunc &F) const { 703 funcName = sanitizeFunctionName(funcName); 704 if (funcName.empty()) 705 return false; 706 707 const auto *Start = std::begin(StandardNames); 708 const auto *End = std::end(StandardNames); 709 const auto *I = std::lower_bound(Start, End, funcName); 710 if (I != End && *I == funcName) { 711 F = (LibFunc)(I - Start); 712 return true; 713 } 714 return false; 715 } 716 717 bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy, 718 LibFunc F, 719 const DataLayout *DL) const { 720 LLVMContext &Ctx = FTy.getContext(); 721 Type *SizeTTy = DL ? DL->getIntPtrType(Ctx, /*AddressSpace=*/0) : nullptr; 722 auto IsSizeTTy = [SizeTTy](Type *Ty) { 723 return SizeTTy ? Ty == SizeTTy : Ty->isIntegerTy(); 724 }; 725 unsigned NumParams = FTy.getNumParams(); 726 727 switch (F) { 728 case LibFunc_execl: 729 case LibFunc_execlp: 730 case LibFunc_execle: 731 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 732 FTy.getParamType(1)->isPointerTy() && 733 FTy.getReturnType()->isIntegerTy(32)); 734 case LibFunc_execv: 735 case LibFunc_execvp: 736 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 737 FTy.getParamType(1)->isPointerTy() && 738 FTy.getReturnType()->isIntegerTy(32)); 739 case LibFunc_execvP: 740 case LibFunc_execvpe: 741 case LibFunc_execve: 742 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() && 743 FTy.getParamType(1)->isPointerTy() && 744 FTy.getParamType(2)->isPointerTy() && 745 FTy.getReturnType()->isIntegerTy(32)); 746 case LibFunc_strlen_chk: 747 --NumParams; 748 if (!IsSizeTTy(FTy.getParamType(NumParams))) 749 return false; 750 LLVM_FALLTHROUGH; 751 case LibFunc_strlen: 752 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() && 753 FTy.getReturnType()->isIntegerTy()); 754 755 case LibFunc_strchr: 756 case LibFunc_strrchr: 757 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 758 FTy.getParamType(0) == FTy.getReturnType() && 759 FTy.getParamType(1)->isIntegerTy()); 760 761 case LibFunc_strtol: 762 case LibFunc_strtod: 763 case LibFunc_strtof: 764 case LibFunc_strtoul: 765 case LibFunc_strtoll: 766 case LibFunc_strtold: 767 case LibFunc_strtoull: 768 return ((NumParams == 2 || NumParams == 3) && 769 FTy.getParamType(0)->isPointerTy() && 770 FTy.getParamType(1)->isPointerTy()); 771 case LibFunc_strcat_chk: 772 --NumParams; 773 if (!IsSizeTTy(FTy.getParamType(NumParams))) 774 return false; 775 LLVM_FALLTHROUGH; 776 case LibFunc_strcat: 777 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 778 FTy.getParamType(0) == FTy.getReturnType() && 779 FTy.getParamType(1) == FTy.getReturnType()); 780 781 case LibFunc_strncat_chk: 782 --NumParams; 783 if (!IsSizeTTy(FTy.getParamType(NumParams))) 784 return false; 785 LLVM_FALLTHROUGH; 786 case LibFunc_strncat: 787 return (NumParams == 3 && FTy.getReturnType()->isPointerTy() && 788 FTy.getParamType(0) == FTy.getReturnType() && 789 FTy.getParamType(1) == FTy.getReturnType() && 790 IsSizeTTy(FTy.getParamType(2))); 791 792 case LibFunc_strcpy_chk: 793 case LibFunc_stpcpy_chk: 794 --NumParams; 795 if (!IsSizeTTy(FTy.getParamType(NumParams))) 796 return false; 797 LLVM_FALLTHROUGH; 798 case LibFunc_strcpy: 799 case LibFunc_stpcpy: 800 return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(0) && 801 FTy.getParamType(0) == FTy.getParamType(1) && 802 FTy.getParamType(0)->isPointerTy()); 803 804 case LibFunc_strlcat_chk: 805 case LibFunc_strlcpy_chk: 806 --NumParams; 807 if (!IsSizeTTy(FTy.getParamType(NumParams))) 808 return false; 809 LLVM_FALLTHROUGH; 810 case LibFunc_strlcat: 811 case LibFunc_strlcpy: 812 return NumParams == 3 && IsSizeTTy(FTy.getReturnType()) && 813 FTy.getParamType(0)->isPointerTy() && 814 FTy.getParamType(1)->isPointerTy() && 815 IsSizeTTy(FTy.getParamType(2)); 816 817 case LibFunc_strncpy_chk: 818 case LibFunc_stpncpy_chk: 819 --NumParams; 820 if (!IsSizeTTy(FTy.getParamType(NumParams))) 821 return false; 822 LLVM_FALLTHROUGH; 823 case LibFunc_strncpy: 824 case LibFunc_stpncpy: 825 return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) && 826 FTy.getParamType(0) == FTy.getParamType(1) && 827 FTy.getParamType(0)->isPointerTy() && 828 IsSizeTTy(FTy.getParamType(2))); 829 830 case LibFunc_strxfrm: 831 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() && 832 FTy.getParamType(1)->isPointerTy()); 833 834 case LibFunc_strcmp: 835 return (NumParams == 2 && FTy.getReturnType()->isIntegerTy(32) && 836 FTy.getParamType(0)->isPointerTy() && 837 FTy.getParamType(0) == FTy.getParamType(1)); 838 839 case LibFunc_strncmp: 840 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) && 841 FTy.getParamType(0)->isPointerTy() && 842 FTy.getParamType(0) == FTy.getParamType(1) && 843 IsSizeTTy(FTy.getParamType(2))); 844 845 case LibFunc_strspn: 846 case LibFunc_strcspn: 847 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 848 FTy.getParamType(0) == FTy.getParamType(1) && 849 FTy.getReturnType()->isIntegerTy()); 850 851 case LibFunc_strcoll: 852 case LibFunc_strcasecmp: 853 case LibFunc_strncasecmp: 854 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 855 FTy.getParamType(1)->isPointerTy()); 856 857 case LibFunc_strstr: 858 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 859 FTy.getParamType(0)->isPointerTy() && 860 FTy.getParamType(1)->isPointerTy()); 861 862 case LibFunc_strpbrk: 863 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 864 FTy.getReturnType() == FTy.getParamType(0) && 865 FTy.getParamType(0) == FTy.getParamType(1)); 866 867 case LibFunc_strtok: 868 case LibFunc_strtok_r: 869 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy()); 870 case LibFunc_scanf: 871 case LibFunc_setbuf: 872 case LibFunc_setvbuf: 873 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy()); 874 case LibFunc_strdup: 875 case LibFunc_strndup: 876 return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() && 877 FTy.getParamType(0)->isPointerTy()); 878 case LibFunc_sscanf: 879 case LibFunc_stat: 880 case LibFunc_statvfs: 881 case LibFunc_siprintf: 882 case LibFunc_small_sprintf: 883 case LibFunc_sprintf: 884 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 885 FTy.getParamType(1)->isPointerTy() && 886 FTy.getReturnType()->isIntegerTy(32)); 887 888 case LibFunc_sprintf_chk: 889 return NumParams == 4 && FTy.getParamType(0)->isPointerTy() && 890 FTy.getParamType(1)->isIntegerTy(32) && 891 IsSizeTTy(FTy.getParamType(2)) && 892 FTy.getParamType(3)->isPointerTy() && 893 FTy.getReturnType()->isIntegerTy(32); 894 895 case LibFunc_snprintf: 896 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() && 897 FTy.getParamType(2)->isPointerTy() && 898 FTy.getReturnType()->isIntegerTy(32)); 899 900 case LibFunc_snprintf_chk: 901 return NumParams == 5 && FTy.getParamType(0)->isPointerTy() && 902 IsSizeTTy(FTy.getParamType(1)) && 903 FTy.getParamType(2)->isIntegerTy(32) && 904 IsSizeTTy(FTy.getParamType(3)) && 905 FTy.getParamType(4)->isPointerTy() && 906 FTy.getReturnType()->isIntegerTy(32); 907 908 case LibFunc_setitimer: 909 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() && 910 FTy.getParamType(2)->isPointerTy()); 911 case LibFunc_system: 912 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy()); 913 case LibFunc___kmpc_alloc_shared: 914 case LibFunc_malloc: 915 case LibFunc_vec_malloc: 916 return (NumParams == 1 && FTy.getReturnType()->isPointerTy()); 917 case LibFunc_memcmp: 918 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) && 919 FTy.getParamType(0)->isPointerTy() && 920 FTy.getParamType(1)->isPointerTy()); 921 922 case LibFunc_memchr: 923 case LibFunc_memrchr: 924 return (NumParams == 3 && FTy.getReturnType()->isPointerTy() && 925 FTy.getReturnType() == FTy.getParamType(0) && 926 FTy.getParamType(1)->isIntegerTy(32) && 927 IsSizeTTy(FTy.getParamType(2))); 928 case LibFunc_modf: 929 case LibFunc_modff: 930 case LibFunc_modfl: 931 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy()); 932 933 case LibFunc_memcpy_chk: 934 case LibFunc_mempcpy_chk: 935 case LibFunc_memmove_chk: 936 --NumParams; 937 if (!IsSizeTTy(FTy.getParamType(NumParams))) 938 return false; 939 LLVM_FALLTHROUGH; 940 case LibFunc_memcpy: 941 case LibFunc_mempcpy: 942 case LibFunc_memmove: 943 return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) && 944 FTy.getParamType(0)->isPointerTy() && 945 FTy.getParamType(1)->isPointerTy() && 946 IsSizeTTy(FTy.getParamType(2))); 947 948 case LibFunc_memset_chk: 949 --NumParams; 950 if (!IsSizeTTy(FTy.getParamType(NumParams))) 951 return false; 952 LLVM_FALLTHROUGH; 953 case LibFunc_memset: 954 return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) && 955 FTy.getParamType(0)->isPointerTy() && 956 FTy.getParamType(1)->isIntegerTy() && 957 IsSizeTTy(FTy.getParamType(2))); 958 959 case LibFunc_memccpy_chk: 960 --NumParams; 961 if (!IsSizeTTy(FTy.getParamType(NumParams))) 962 return false; 963 LLVM_FALLTHROUGH; 964 case LibFunc_memccpy: 965 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy()); 966 case LibFunc_memalign: 967 return (FTy.getReturnType()->isPointerTy()); 968 case LibFunc_realloc: 969 case LibFunc_reallocf: 970 case LibFunc_vec_realloc: 971 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 972 FTy.getParamType(0) == FTy.getReturnType() && 973 IsSizeTTy(FTy.getParamType(1))); 974 case LibFunc_read: 975 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy()); 976 case LibFunc_rewind: 977 case LibFunc_rmdir: 978 case LibFunc_remove: 979 case LibFunc_realpath: 980 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy()); 981 case LibFunc_rename: 982 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 983 FTy.getParamType(1)->isPointerTy()); 984 case LibFunc_readlink: 985 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 986 FTy.getParamType(1)->isPointerTy()); 987 case LibFunc_write: 988 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy()); 989 case LibFunc_aligned_alloc: 990 return (NumParams == 2 && FTy.getReturnType()->isPointerTy()); 991 case LibFunc_bcopy: 992 case LibFunc_bcmp: 993 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() && 994 FTy.getParamType(1)->isPointerTy()); 995 case LibFunc_bzero: 996 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy()); 997 case LibFunc_calloc: 998 case LibFunc_vec_calloc: 999 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 1000 FTy.getParamType(0) == FTy.getParamType(1)); 1001 1002 case LibFunc_atof: 1003 case LibFunc_atoi: 1004 case LibFunc_atol: 1005 case LibFunc_atoll: 1006 case LibFunc_ferror: 1007 case LibFunc_getenv: 1008 case LibFunc_getpwnam: 1009 case LibFunc_iprintf: 1010 case LibFunc_small_printf: 1011 case LibFunc_pclose: 1012 case LibFunc_perror: 1013 case LibFunc_printf: 1014 case LibFunc_puts: 1015 case LibFunc_uname: 1016 case LibFunc_under_IO_getc: 1017 case LibFunc_unlink: 1018 case LibFunc_unsetenv: 1019 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy()); 1020 1021 case LibFunc_access: 1022 case LibFunc_chmod: 1023 case LibFunc_chown: 1024 case LibFunc_clearerr: 1025 case LibFunc_closedir: 1026 case LibFunc_ctermid: 1027 case LibFunc_fclose: 1028 case LibFunc_feof: 1029 case LibFunc_fflush: 1030 case LibFunc_fgetc: 1031 case LibFunc_fgetc_unlocked: 1032 case LibFunc_fileno: 1033 case LibFunc_flockfile: 1034 case LibFunc_free: 1035 case LibFunc_fseek: 1036 case LibFunc_fseeko64: 1037 case LibFunc_fseeko: 1038 case LibFunc_fsetpos: 1039 case LibFunc_ftell: 1040 case LibFunc_ftello64: 1041 case LibFunc_ftello: 1042 case LibFunc_ftrylockfile: 1043 case LibFunc_funlockfile: 1044 case LibFunc_getc: 1045 case LibFunc_getc_unlocked: 1046 case LibFunc_getlogin_r: 1047 case LibFunc_mkdir: 1048 case LibFunc_mktime: 1049 case LibFunc_times: 1050 case LibFunc_vec_free: 1051 case LibFunc___kmpc_free_shared: 1052 return (NumParams != 0 && FTy.getParamType(0)->isPointerTy()); 1053 1054 case LibFunc_fopen: 1055 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 1056 FTy.getParamType(0)->isPointerTy() && 1057 FTy.getParamType(1)->isPointerTy()); 1058 case LibFunc_fork: 1059 return (NumParams == 0 && FTy.getReturnType()->isIntegerTy(32)); 1060 case LibFunc_fdopen: 1061 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 1062 FTy.getParamType(1)->isPointerTy()); 1063 case LibFunc_fputc: 1064 case LibFunc_fputc_unlocked: 1065 case LibFunc_fstat: 1066 case LibFunc_frexp: 1067 case LibFunc_frexpf: 1068 case LibFunc_frexpl: 1069 case LibFunc_fstatvfs: 1070 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 1071 case LibFunc_fgets: 1072 case LibFunc_fgets_unlocked: 1073 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() && 1074 FTy.getParamType(2)->isPointerTy()); 1075 case LibFunc_fread: 1076 case LibFunc_fread_unlocked: 1077 return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() && 1078 FTy.getParamType(3)->isPointerTy()); 1079 case LibFunc_fwrite: 1080 case LibFunc_fwrite_unlocked: 1081 return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() && 1082 FTy.getParamType(0)->isPointerTy() && 1083 FTy.getParamType(1)->isIntegerTy() && 1084 FTy.getParamType(2)->isIntegerTy() && 1085 FTy.getParamType(3)->isPointerTy()); 1086 case LibFunc_fputs: 1087 case LibFunc_fputs_unlocked: 1088 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 1089 FTy.getParamType(1)->isPointerTy()); 1090 case LibFunc_fscanf: 1091 case LibFunc_fiprintf: 1092 case LibFunc_small_fprintf: 1093 case LibFunc_fprintf: 1094 return (NumParams >= 2 && FTy.getReturnType()->isIntegerTy() && 1095 FTy.getParamType(0)->isPointerTy() && 1096 FTy.getParamType(1)->isPointerTy()); 1097 case LibFunc_fgetpos: 1098 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 1099 FTy.getParamType(1)->isPointerTy()); 1100 case LibFunc_getchar: 1101 case LibFunc_getchar_unlocked: 1102 return (NumParams == 0 && FTy.getReturnType()->isIntegerTy()); 1103 case LibFunc_gets: 1104 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy()); 1105 case LibFunc_getitimer: 1106 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 1107 case LibFunc_ungetc: 1108 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 1109 case LibFunc_utime: 1110 case LibFunc_utimes: 1111 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 1112 FTy.getParamType(1)->isPointerTy()); 1113 case LibFunc_putc: 1114 case LibFunc_putc_unlocked: 1115 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 1116 case LibFunc_pread: 1117 case LibFunc_pwrite: 1118 return (NumParams == 4 && FTy.getParamType(1)->isPointerTy()); 1119 case LibFunc_popen: 1120 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 1121 FTy.getParamType(0)->isPointerTy() && 1122 FTy.getParamType(1)->isPointerTy()); 1123 case LibFunc_vscanf: 1124 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 1125 case LibFunc_vsscanf: 1126 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() && 1127 FTy.getParamType(2)->isPointerTy()); 1128 case LibFunc_vfscanf: 1129 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() && 1130 FTy.getParamType(2)->isPointerTy()); 1131 case LibFunc_valloc: 1132 return (FTy.getReturnType()->isPointerTy()); 1133 case LibFunc_vprintf: 1134 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy()); 1135 case LibFunc_vfprintf: 1136 case LibFunc_vsprintf: 1137 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() && 1138 FTy.getParamType(1)->isPointerTy()); 1139 case LibFunc_vsprintf_chk: 1140 return NumParams == 5 && FTy.getParamType(0)->isPointerTy() && 1141 FTy.getParamType(1)->isIntegerTy(32) && 1142 IsSizeTTy(FTy.getParamType(2)) && FTy.getParamType(3)->isPointerTy(); 1143 case LibFunc_vsnprintf: 1144 return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() && 1145 FTy.getParamType(2)->isPointerTy()); 1146 case LibFunc_vsnprintf_chk: 1147 return NumParams == 6 && FTy.getParamType(0)->isPointerTy() && 1148 FTy.getParamType(2)->isIntegerTy(32) && 1149 IsSizeTTy(FTy.getParamType(3)) && FTy.getParamType(4)->isPointerTy(); 1150 case LibFunc_open: 1151 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy()); 1152 case LibFunc_opendir: 1153 return (NumParams == 1 && FTy.getReturnType()->isPointerTy() && 1154 FTy.getParamType(0)->isPointerTy()); 1155 case LibFunc_tmpfile: 1156 return (FTy.getReturnType()->isPointerTy()); 1157 case LibFunc_htonl: 1158 case LibFunc_ntohl: 1159 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) && 1160 FTy.getReturnType() == FTy.getParamType(0)); 1161 case LibFunc_htons: 1162 case LibFunc_ntohs: 1163 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(16) && 1164 FTy.getReturnType() == FTy.getParamType(0)); 1165 case LibFunc_lstat: 1166 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 1167 FTy.getParamType(1)->isPointerTy()); 1168 case LibFunc_lchown: 1169 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy()); 1170 case LibFunc_qsort: 1171 return (NumParams == 4 && FTy.getParamType(3)->isPointerTy()); 1172 case LibFunc_dunder_strdup: 1173 case LibFunc_dunder_strndup: 1174 return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() && 1175 FTy.getParamType(0)->isPointerTy()); 1176 case LibFunc_dunder_strtok_r: 1177 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy()); 1178 case LibFunc_under_IO_putc: 1179 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 1180 case LibFunc_dunder_isoc99_scanf: 1181 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy()); 1182 case LibFunc_stat64: 1183 case LibFunc_lstat64: 1184 case LibFunc_statvfs64: 1185 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 1186 FTy.getParamType(1)->isPointerTy()); 1187 case LibFunc_dunder_isoc99_sscanf: 1188 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 1189 FTy.getParamType(1)->isPointerTy()); 1190 case LibFunc_fopen64: 1191 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 1192 FTy.getParamType(0)->isPointerTy() && 1193 FTy.getParamType(1)->isPointerTy()); 1194 case LibFunc_tmpfile64: 1195 return (FTy.getReturnType()->isPointerTy()); 1196 case LibFunc_fstat64: 1197 case LibFunc_fstatvfs64: 1198 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 1199 case LibFunc_open64: 1200 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy()); 1201 case LibFunc_gettimeofday: 1202 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 1203 FTy.getParamType(1)->isPointerTy()); 1204 1205 // new(unsigned int); 1206 case LibFunc_Znwj: 1207 // new(unsigned long); 1208 case LibFunc_Znwm: 1209 // new[](unsigned int); 1210 case LibFunc_Znaj: 1211 // new[](unsigned long); 1212 case LibFunc_Znam: 1213 // new(unsigned int); 1214 case LibFunc_msvc_new_int: 1215 // new(unsigned long long); 1216 case LibFunc_msvc_new_longlong: 1217 // new[](unsigned int); 1218 case LibFunc_msvc_new_array_int: 1219 // new[](unsigned long long); 1220 case LibFunc_msvc_new_array_longlong: 1221 return (NumParams == 1 && FTy.getReturnType()->isPointerTy()); 1222 1223 // new(unsigned int, nothrow); 1224 case LibFunc_ZnwjRKSt9nothrow_t: 1225 // new(unsigned long, nothrow); 1226 case LibFunc_ZnwmRKSt9nothrow_t: 1227 // new[](unsigned int, nothrow); 1228 case LibFunc_ZnajRKSt9nothrow_t: 1229 // new[](unsigned long, nothrow); 1230 case LibFunc_ZnamRKSt9nothrow_t: 1231 // new(unsigned int, nothrow); 1232 case LibFunc_msvc_new_int_nothrow: 1233 // new(unsigned long long, nothrow); 1234 case LibFunc_msvc_new_longlong_nothrow: 1235 // new[](unsigned int, nothrow); 1236 case LibFunc_msvc_new_array_int_nothrow: 1237 // new[](unsigned long long, nothrow); 1238 case LibFunc_msvc_new_array_longlong_nothrow: 1239 // new(unsigned int, align_val_t) 1240 case LibFunc_ZnwjSt11align_val_t: 1241 // new(unsigned long, align_val_t) 1242 case LibFunc_ZnwmSt11align_val_t: 1243 // new[](unsigned int, align_val_t) 1244 case LibFunc_ZnajSt11align_val_t: 1245 // new[](unsigned long, align_val_t) 1246 case LibFunc_ZnamSt11align_val_t: 1247 return (NumParams == 2 && FTy.getReturnType()->isPointerTy()); 1248 1249 // new(unsigned int, align_val_t, nothrow) 1250 case LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t: 1251 // new(unsigned long, align_val_t, nothrow) 1252 case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t: 1253 // new[](unsigned int, align_val_t, nothrow) 1254 case LibFunc_ZnajSt11align_val_tRKSt9nothrow_t: 1255 // new[](unsigned long, align_val_t, nothrow) 1256 case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t: 1257 return (NumParams == 3 && FTy.getReturnType()->isPointerTy()); 1258 1259 // void operator delete[](void*); 1260 case LibFunc_ZdaPv: 1261 // void operator delete(void*); 1262 case LibFunc_ZdlPv: 1263 // void operator delete[](void*); 1264 case LibFunc_msvc_delete_array_ptr32: 1265 // void operator delete[](void*); 1266 case LibFunc_msvc_delete_array_ptr64: 1267 // void operator delete(void*); 1268 case LibFunc_msvc_delete_ptr32: 1269 // void operator delete(void*); 1270 case LibFunc_msvc_delete_ptr64: 1271 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy()); 1272 1273 // void operator delete[](void*, nothrow); 1274 case LibFunc_ZdaPvRKSt9nothrow_t: 1275 // void operator delete[](void*, unsigned int); 1276 case LibFunc_ZdaPvj: 1277 // void operator delete[](void*, unsigned long); 1278 case LibFunc_ZdaPvm: 1279 // void operator delete(void*, nothrow); 1280 case LibFunc_ZdlPvRKSt9nothrow_t: 1281 // void operator delete(void*, unsigned int); 1282 case LibFunc_ZdlPvj: 1283 // void operator delete(void*, unsigned long); 1284 case LibFunc_ZdlPvm: 1285 // void operator delete(void*, align_val_t) 1286 case LibFunc_ZdlPvSt11align_val_t: 1287 // void operator delete[](void*, align_val_t) 1288 case LibFunc_ZdaPvSt11align_val_t: 1289 // void operator delete[](void*, unsigned int); 1290 case LibFunc_msvc_delete_array_ptr32_int: 1291 // void operator delete[](void*, nothrow); 1292 case LibFunc_msvc_delete_array_ptr32_nothrow: 1293 // void operator delete[](void*, unsigned long long); 1294 case LibFunc_msvc_delete_array_ptr64_longlong: 1295 // void operator delete[](void*, nothrow); 1296 case LibFunc_msvc_delete_array_ptr64_nothrow: 1297 // void operator delete(void*, unsigned int); 1298 case LibFunc_msvc_delete_ptr32_int: 1299 // void operator delete(void*, nothrow); 1300 case LibFunc_msvc_delete_ptr32_nothrow: 1301 // void operator delete(void*, unsigned long long); 1302 case LibFunc_msvc_delete_ptr64_longlong: 1303 // void operator delete(void*, nothrow); 1304 case LibFunc_msvc_delete_ptr64_nothrow: 1305 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy()); 1306 1307 // void operator delete(void*, align_val_t, nothrow) 1308 case LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t: 1309 // void operator delete[](void*, align_val_t, nothrow) 1310 case LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t: 1311 // void operator delete(void*, unsigned int, align_val_t) 1312 case LibFunc_ZdlPvjSt11align_val_t: 1313 // void operator delete(void*, unsigned long, align_val_t) 1314 case LibFunc_ZdlPvmSt11align_val_t: 1315 // void operator delete[](void*, unsigned int, align_val_t); 1316 case LibFunc_ZdaPvjSt11align_val_t: 1317 // void operator delete[](void*, unsigned long, align_val_t); 1318 case LibFunc_ZdaPvmSt11align_val_t: 1319 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy()); 1320 1321 // void __atomic_load(size_t, void *, void *, int) 1322 case LibFunc_atomic_load: 1323 // void __atomic_store(size_t, void *, void *, int) 1324 case LibFunc_atomic_store: 1325 return (NumParams == 4 && FTy.getParamType(0)->isIntegerTy() && 1326 FTy.getParamType(1)->isPointerTy() && 1327 FTy.getParamType(2)->isPointerTy() && 1328 FTy.getParamType(3)->isIntegerTy()); 1329 1330 case LibFunc_memset_pattern16: 1331 return (!FTy.isVarArg() && NumParams == 3 && 1332 FTy.getParamType(0)->isPointerTy() && 1333 FTy.getParamType(1)->isPointerTy() && 1334 FTy.getParamType(2)->isIntegerTy()); 1335 1336 case LibFunc_cxa_guard_abort: 1337 case LibFunc_cxa_guard_acquire: 1338 case LibFunc_cxa_guard_release: 1339 case LibFunc_nvvm_reflect: 1340 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy()); 1341 1342 case LibFunc_sincospi_stret: 1343 case LibFunc_sincospif_stret: 1344 return (NumParams == 1 && FTy.getParamType(0)->isFloatingPointTy()); 1345 1346 case LibFunc_acos: 1347 case LibFunc_acos_finite: 1348 case LibFunc_acosf: 1349 case LibFunc_acosf_finite: 1350 case LibFunc_acosh: 1351 case LibFunc_acosh_finite: 1352 case LibFunc_acoshf: 1353 case LibFunc_acoshf_finite: 1354 case LibFunc_acoshl: 1355 case LibFunc_acoshl_finite: 1356 case LibFunc_acosl: 1357 case LibFunc_acosl_finite: 1358 case LibFunc_asin: 1359 case LibFunc_asin_finite: 1360 case LibFunc_asinf: 1361 case LibFunc_asinf_finite: 1362 case LibFunc_asinh: 1363 case LibFunc_asinhf: 1364 case LibFunc_asinhl: 1365 case LibFunc_asinl: 1366 case LibFunc_asinl_finite: 1367 case LibFunc_atan: 1368 case LibFunc_atanf: 1369 case LibFunc_atanh: 1370 case LibFunc_atanh_finite: 1371 case LibFunc_atanhf: 1372 case LibFunc_atanhf_finite: 1373 case LibFunc_atanhl: 1374 case LibFunc_atanhl_finite: 1375 case LibFunc_atanl: 1376 case LibFunc_cbrt: 1377 case LibFunc_cbrtf: 1378 case LibFunc_cbrtl: 1379 case LibFunc_ceil: 1380 case LibFunc_ceilf: 1381 case LibFunc_ceill: 1382 case LibFunc_cos: 1383 case LibFunc_cosf: 1384 case LibFunc_cosh: 1385 case LibFunc_cosh_finite: 1386 case LibFunc_coshf: 1387 case LibFunc_coshf_finite: 1388 case LibFunc_coshl: 1389 case LibFunc_coshl_finite: 1390 case LibFunc_cosl: 1391 case LibFunc_exp10: 1392 case LibFunc_exp10_finite: 1393 case LibFunc_exp10f: 1394 case LibFunc_exp10f_finite: 1395 case LibFunc_exp10l: 1396 case LibFunc_exp10l_finite: 1397 case LibFunc_exp2: 1398 case LibFunc_exp2_finite: 1399 case LibFunc_exp2f: 1400 case LibFunc_exp2f_finite: 1401 case LibFunc_exp2l: 1402 case LibFunc_exp2l_finite: 1403 case LibFunc_exp: 1404 case LibFunc_exp_finite: 1405 case LibFunc_expf: 1406 case LibFunc_expf_finite: 1407 case LibFunc_expl: 1408 case LibFunc_expl_finite: 1409 case LibFunc_expm1: 1410 case LibFunc_expm1f: 1411 case LibFunc_expm1l: 1412 case LibFunc_fabs: 1413 case LibFunc_fabsf: 1414 case LibFunc_fabsl: 1415 case LibFunc_floor: 1416 case LibFunc_floorf: 1417 case LibFunc_floorl: 1418 case LibFunc_log10: 1419 case LibFunc_log10_finite: 1420 case LibFunc_log10f: 1421 case LibFunc_log10f_finite: 1422 case LibFunc_log10l: 1423 case LibFunc_log10l_finite: 1424 case LibFunc_log1p: 1425 case LibFunc_log1pf: 1426 case LibFunc_log1pl: 1427 case LibFunc_log2: 1428 case LibFunc_log2_finite: 1429 case LibFunc_log2f: 1430 case LibFunc_log2f_finite: 1431 case LibFunc_log2l: 1432 case LibFunc_log2l_finite: 1433 case LibFunc_log: 1434 case LibFunc_log_finite: 1435 case LibFunc_logb: 1436 case LibFunc_logbf: 1437 case LibFunc_logbl: 1438 case LibFunc_logf: 1439 case LibFunc_logf_finite: 1440 case LibFunc_logl: 1441 case LibFunc_logl_finite: 1442 case LibFunc_nearbyint: 1443 case LibFunc_nearbyintf: 1444 case LibFunc_nearbyintl: 1445 case LibFunc_rint: 1446 case LibFunc_rintf: 1447 case LibFunc_rintl: 1448 case LibFunc_round: 1449 case LibFunc_roundf: 1450 case LibFunc_roundl: 1451 case LibFunc_roundeven: 1452 case LibFunc_roundevenf: 1453 case LibFunc_roundevenl: 1454 case LibFunc_sin: 1455 case LibFunc_sinf: 1456 case LibFunc_sinh: 1457 case LibFunc_sinh_finite: 1458 case LibFunc_sinhf: 1459 case LibFunc_sinhf_finite: 1460 case LibFunc_sinhl: 1461 case LibFunc_sinhl_finite: 1462 case LibFunc_sinl: 1463 case LibFunc_sqrt: 1464 case LibFunc_sqrt_finite: 1465 case LibFunc_sqrtf: 1466 case LibFunc_sqrtf_finite: 1467 case LibFunc_sqrtl: 1468 case LibFunc_sqrtl_finite: 1469 case LibFunc_tan: 1470 case LibFunc_tanf: 1471 case LibFunc_tanh: 1472 case LibFunc_tanhf: 1473 case LibFunc_tanhl: 1474 case LibFunc_tanl: 1475 case LibFunc_trunc: 1476 case LibFunc_truncf: 1477 case LibFunc_truncl: 1478 return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() && 1479 FTy.getReturnType() == FTy.getParamType(0)); 1480 1481 case LibFunc_atan2: 1482 case LibFunc_atan2_finite: 1483 case LibFunc_atan2f: 1484 case LibFunc_atan2f_finite: 1485 case LibFunc_atan2l: 1486 case LibFunc_atan2l_finite: 1487 case LibFunc_fmin: 1488 case LibFunc_fminf: 1489 case LibFunc_fminl: 1490 case LibFunc_fmax: 1491 case LibFunc_fmaxf: 1492 case LibFunc_fmaxl: 1493 case LibFunc_fmod: 1494 case LibFunc_fmodf: 1495 case LibFunc_fmodl: 1496 case LibFunc_remainder: 1497 case LibFunc_remainderf: 1498 case LibFunc_remainderl: 1499 case LibFunc_copysign: 1500 case LibFunc_copysignf: 1501 case LibFunc_copysignl: 1502 case LibFunc_pow: 1503 case LibFunc_pow_finite: 1504 case LibFunc_powf: 1505 case LibFunc_powf_finite: 1506 case LibFunc_powl: 1507 case LibFunc_powl_finite: 1508 return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() && 1509 FTy.getReturnType() == FTy.getParamType(0) && 1510 FTy.getReturnType() == FTy.getParamType(1)); 1511 1512 case LibFunc_ldexp: 1513 case LibFunc_ldexpf: 1514 case LibFunc_ldexpl: 1515 return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() && 1516 FTy.getReturnType() == FTy.getParamType(0) && 1517 FTy.getParamType(1)->isIntegerTy(getIntSize())); 1518 1519 case LibFunc_ffs: 1520 case LibFunc_ffsl: 1521 case LibFunc_ffsll: 1522 case LibFunc_fls: 1523 case LibFunc_flsl: 1524 case LibFunc_flsll: 1525 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) && 1526 FTy.getParamType(0)->isIntegerTy()); 1527 1528 case LibFunc_isdigit: 1529 case LibFunc_isascii: 1530 case LibFunc_toascii: 1531 case LibFunc_putchar: 1532 case LibFunc_putchar_unlocked: 1533 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) && 1534 FTy.getReturnType() == FTy.getParamType(0)); 1535 1536 case LibFunc_abs: 1537 case LibFunc_labs: 1538 case LibFunc_llabs: 1539 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() && 1540 FTy.getReturnType() == FTy.getParamType(0)); 1541 1542 case LibFunc_cxa_atexit: 1543 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() && 1544 FTy.getParamType(0)->isPointerTy() && 1545 FTy.getParamType(1)->isPointerTy() && 1546 FTy.getParamType(2)->isPointerTy()); 1547 1548 case LibFunc_sinpi: 1549 case LibFunc_cospi: 1550 return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() && 1551 FTy.getReturnType() == FTy.getParamType(0)); 1552 1553 case LibFunc_sinpif: 1554 case LibFunc_cospif: 1555 return (NumParams == 1 && FTy.getReturnType()->isFloatTy() && 1556 FTy.getReturnType() == FTy.getParamType(0)); 1557 1558 case LibFunc_strnlen: 1559 return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(1) && 1560 FTy.getParamType(0)->isPointerTy() && 1561 IsSizeTTy(FTy.getParamType(1))); 1562 1563 case LibFunc_posix_memalign: 1564 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) && 1565 FTy.getParamType(0)->isPointerTy() && 1566 IsSizeTTy(FTy.getParamType(1)) && IsSizeTTy(FTy.getParamType(2))); 1567 1568 case LibFunc_wcslen: 1569 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() && 1570 FTy.getReturnType()->isIntegerTy()); 1571 1572 case LibFunc_cabs: 1573 case LibFunc_cabsf: 1574 case LibFunc_cabsl: { 1575 Type* RetTy = FTy.getReturnType(); 1576 if (!RetTy->isFloatingPointTy()) 1577 return false; 1578 1579 // NOTE: These prototypes are target specific and currently support 1580 // "complex" passed as an array or discrete real & imaginary parameters. 1581 // Add other calling conventions to enable libcall optimizations. 1582 if (NumParams == 1) 1583 return (FTy.getParamType(0)->isArrayTy() && 1584 FTy.getParamType(0)->getArrayNumElements() == 2 && 1585 FTy.getParamType(0)->getArrayElementType() == RetTy); 1586 else if (NumParams == 2) 1587 return (FTy.getParamType(0) == RetTy && FTy.getParamType(1) == RetTy); 1588 else 1589 return false; 1590 } 1591 case LibFunc::NumLibFuncs: 1592 case LibFunc::NotLibFunc: 1593 break; 1594 } 1595 1596 llvm_unreachable("Invalid libfunc"); 1597 } 1598 1599 bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl, 1600 LibFunc &F) const { 1601 // Intrinsics don't overlap w/libcalls; if our module has a large number of 1602 // intrinsics, this ends up being an interesting compile time win since we 1603 // avoid string normalization and comparison. 1604 if (FDecl.isIntrinsic()) return false; 1605 1606 const DataLayout *DL = 1607 FDecl.getParent() ? &FDecl.getParent()->getDataLayout() : nullptr; 1608 return getLibFunc(FDecl.getName(), F) && 1609 isValidProtoForLibFunc(*FDecl.getFunctionType(), F, DL); 1610 } 1611 1612 void TargetLibraryInfoImpl::disableAllFunctions() { 1613 memset(AvailableArray, 0, sizeof(AvailableArray)); 1614 } 1615 1616 static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) { 1617 return LHS.ScalarFnName < RHS.ScalarFnName; 1618 } 1619 1620 static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) { 1621 return LHS.VectorFnName < RHS.VectorFnName; 1622 } 1623 1624 static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) { 1625 return LHS.ScalarFnName < S; 1626 } 1627 1628 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) { 1629 llvm::append_range(VectorDescs, Fns); 1630 llvm::sort(VectorDescs, compareByScalarFnName); 1631 1632 llvm::append_range(ScalarDescs, Fns); 1633 llvm::sort(ScalarDescs, compareByVectorFnName); 1634 } 1635 1636 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib( 1637 enum VectorLibrary VecLib) { 1638 switch (VecLib) { 1639 case Accelerate: { 1640 const VecDesc VecFuncs[] = { 1641 #define TLI_DEFINE_ACCELERATE_VECFUNCS 1642 #include "llvm/Analysis/VecFuncs.def" 1643 }; 1644 addVectorizableFunctions(VecFuncs); 1645 break; 1646 } 1647 case DarwinLibSystemM: { 1648 const VecDesc VecFuncs[] = { 1649 #define TLI_DEFINE_DARWIN_LIBSYSTEM_M_VECFUNCS 1650 #include "llvm/Analysis/VecFuncs.def" 1651 }; 1652 addVectorizableFunctions(VecFuncs); 1653 break; 1654 } 1655 case LIBMVEC_X86: { 1656 const VecDesc VecFuncs[] = { 1657 #define TLI_DEFINE_LIBMVEC_X86_VECFUNCS 1658 #include "llvm/Analysis/VecFuncs.def" 1659 }; 1660 addVectorizableFunctions(VecFuncs); 1661 break; 1662 } 1663 case MASSV: { 1664 const VecDesc VecFuncs[] = { 1665 #define TLI_DEFINE_MASSV_VECFUNCS 1666 #include "llvm/Analysis/VecFuncs.def" 1667 }; 1668 addVectorizableFunctions(VecFuncs); 1669 break; 1670 } 1671 case SVML: { 1672 const VecDesc VecFuncs[] = { 1673 #define TLI_DEFINE_SVML_VECFUNCS 1674 #include "llvm/Analysis/VecFuncs.def" 1675 }; 1676 addVectorizableFunctions(VecFuncs); 1677 break; 1678 } 1679 case NoLibrary: 1680 break; 1681 } 1682 } 1683 1684 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const { 1685 funcName = sanitizeFunctionName(funcName); 1686 if (funcName.empty()) 1687 return false; 1688 1689 std::vector<VecDesc>::const_iterator I = 1690 llvm::lower_bound(VectorDescs, funcName, compareWithScalarFnName); 1691 return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName; 1692 } 1693 1694 StringRef 1695 TargetLibraryInfoImpl::getVectorizedFunction(StringRef F, 1696 const ElementCount &VF) const { 1697 F = sanitizeFunctionName(F); 1698 if (F.empty()) 1699 return F; 1700 std::vector<VecDesc>::const_iterator I = 1701 llvm::lower_bound(VectorDescs, F, compareWithScalarFnName); 1702 while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) { 1703 if (I->VectorizationFactor == VF) 1704 return I->VectorFnName; 1705 ++I; 1706 } 1707 return StringRef(); 1708 } 1709 1710 TargetLibraryInfo TargetLibraryAnalysis::run(const Function &F, 1711 FunctionAnalysisManager &) { 1712 if (!BaselineInfoImpl) 1713 BaselineInfoImpl = 1714 TargetLibraryInfoImpl(Triple(F.getParent()->getTargetTriple())); 1715 return TargetLibraryInfo(*BaselineInfoImpl, &F); 1716 } 1717 1718 unsigned TargetLibraryInfoImpl::getWCharSize(const Module &M) const { 1719 if (auto *ShortWChar = cast_or_null<ConstantAsMetadata>( 1720 M.getModuleFlag("wchar_size"))) 1721 return cast<ConstantInt>(ShortWChar->getValue())->getZExtValue(); 1722 return 0; 1723 } 1724 1725 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass() 1726 : ImmutablePass(ID), TLA(TargetLibraryInfoImpl()) { 1727 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 1728 } 1729 1730 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T) 1731 : ImmutablePass(ID), TLA(TargetLibraryInfoImpl(T)) { 1732 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 1733 } 1734 1735 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass( 1736 const TargetLibraryInfoImpl &TLIImpl) 1737 : ImmutablePass(ID), TLA(TLIImpl) { 1738 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 1739 } 1740 1741 AnalysisKey TargetLibraryAnalysis::Key; 1742 1743 // Register the basic pass. 1744 INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo", 1745 "Target Library Information", false, true) 1746 char TargetLibraryInfoWrapperPass::ID = 0; 1747 1748 void TargetLibraryInfoWrapperPass::anchor() {} 1749 1750 void TargetLibraryInfoImpl::getWidestVF(StringRef ScalarF, 1751 ElementCount &FixedVF, 1752 ElementCount &ScalableVF) const { 1753 ScalarF = sanitizeFunctionName(ScalarF); 1754 // Use '0' here because a type of the form <vscale x 1 x ElTy> is not the 1755 // same as a scalar. 1756 ScalableVF = ElementCount::getScalable(0); 1757 FixedVF = ElementCount::getFixed(1); 1758 if (ScalarF.empty()) 1759 return; 1760 1761 std::vector<VecDesc>::const_iterator I = 1762 llvm::lower_bound(VectorDescs, ScalarF, compareWithScalarFnName); 1763 while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == ScalarF) { 1764 ElementCount *VF = 1765 I->VectorizationFactor.isScalable() ? &ScalableVF : &FixedVF; 1766 if (ElementCount::isKnownGT(I->VectorizationFactor, *VF)) 1767 *VF = I->VectorizationFactor; 1768 ++I; 1769 } 1770 } 1771