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