Home | History | Annotate | Download | only in Analysis
      1 //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements the TargetLibraryInfo class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/Analysis/TargetLibraryInfo.h"
     15 #include "llvm/ADT/Triple.h"
     16 #include "llvm/Support/CommandLine.h"
     17 using namespace llvm;
     18 
     19 static cl::opt<TargetLibraryInfoImpl::VectorLibrary> ClVectorLibrary(
     20     "vector-library", cl::Hidden, cl::desc("Vector functions library"),
     21     cl::init(TargetLibraryInfoImpl::NoLibrary),
     22     cl::values(clEnumValN(TargetLibraryInfoImpl::NoLibrary, "none",
     23                           "No vector functions library"),
     24                clEnumValN(TargetLibraryInfoImpl::Accelerate, "Accelerate",
     25                           "Accelerate framework"),
     26                clEnumValEnd));
     27 
     28 const char *const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] = {
     29 #define TLI_DEFINE_STRING
     30 #include "llvm/Analysis/TargetLibraryInfo.def"
     31 };
     32 
     33 static bool hasSinCosPiStret(const Triple &T) {
     34   // Only Darwin variants have _stret versions of combined trig functions.
     35   if (!T.isOSDarwin())
     36     return false;
     37 
     38   // The ABI is rather complicated on x86, so don't do anything special there.
     39   if (T.getArch() == Triple::x86)
     40     return false;
     41 
     42   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
     43     return false;
     44 
     45   if (T.isiOS() && T.isOSVersionLT(7, 0))
     46     return false;
     47 
     48   return true;
     49 }
     50 
     51 /// initialize - Initialize the set of available library functions based on the
     52 /// specified target triple.  This should be carefully written so that a missing
     53 /// target triple gets a sane set of defaults.
     54 static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
     55                        ArrayRef<const char *> StandardNames) {
     56   // Verify that the StandardNames array is in alphabetical order.
     57   assert(std::is_sorted(StandardNames.begin(), StandardNames.end(),
     58                         [](const char *LHS, const char *RHS) {
     59                           return strcmp(LHS, RHS) < 0;
     60                         }) &&
     61          "TargetLibraryInfoImpl function names must be sorted");
     62 
     63   if (T.getArch() == Triple::r600 ||
     64       T.getArch() == Triple::amdgcn) {
     65     TLI.setUnavailable(LibFunc::ldexp);
     66     TLI.setUnavailable(LibFunc::ldexpf);
     67     TLI.setUnavailable(LibFunc::ldexpl);
     68     TLI.setUnavailable(LibFunc::exp10);
     69     TLI.setUnavailable(LibFunc::exp10f);
     70     TLI.setUnavailable(LibFunc::exp10l);
     71     TLI.setUnavailable(LibFunc::log10);
     72     TLI.setUnavailable(LibFunc::log10f);
     73     TLI.setUnavailable(LibFunc::log10l);
     74   }
     75 
     76   // There are no library implementations of mempcy and memset for AMD gpus and
     77   // these can be difficult to lower in the backend.
     78   if (T.getArch() == Triple::r600 ||
     79       T.getArch() == Triple::amdgcn) {
     80     TLI.setUnavailable(LibFunc::memcpy);
     81     TLI.setUnavailable(LibFunc::memset);
     82     TLI.setUnavailable(LibFunc::memset_pattern16);
     83     return;
     84   }
     85 
     86   // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
     87   // All versions of watchOS support it.
     88   if (T.isMacOSX()) {
     89     if (T.isMacOSXVersionLT(10, 5))
     90       TLI.setUnavailable(LibFunc::memset_pattern16);
     91   } else if (T.isiOS()) {
     92     if (T.isOSVersionLT(3, 0))
     93       TLI.setUnavailable(LibFunc::memset_pattern16);
     94   } else if (!T.isWatchOS()) {
     95     TLI.setUnavailable(LibFunc::memset_pattern16);
     96   }
     97 
     98   if (!hasSinCosPiStret(T)) {
     99     TLI.setUnavailable(LibFunc::sinpi);
    100     TLI.setUnavailable(LibFunc::sinpif);
    101     TLI.setUnavailable(LibFunc::cospi);
    102     TLI.setUnavailable(LibFunc::cospif);
    103     TLI.setUnavailable(LibFunc::sincospi_stret);
    104     TLI.setUnavailable(LibFunc::sincospif_stret);
    105   }
    106 
    107   if (T.isMacOSX() && T.getArch() == Triple::x86 &&
    108       !T.isMacOSXVersionLT(10, 7)) {
    109     // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
    110     // we don't care about) have two versions; on recent OSX, the one we want
    111     // has a $UNIX2003 suffix. The two implementations are identical except
    112     // for the return value in some edge cases.  However, we don't want to
    113     // generate code that depends on the old symbols.
    114     TLI.setAvailableWithName(LibFunc::fwrite, "fwrite$UNIX2003");
    115     TLI.setAvailableWithName(LibFunc::fputs, "fputs$UNIX2003");
    116   }
    117 
    118   // iprintf and friends are only available on XCore and TCE.
    119   if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) {
    120     TLI.setUnavailable(LibFunc::iprintf);
    121     TLI.setUnavailable(LibFunc::siprintf);
    122     TLI.setUnavailable(LibFunc::fiprintf);
    123   }
    124 
    125   if (T.isOSWindows() && !T.isOSCygMing()) {
    126     // Win32 does not support long double
    127     TLI.setUnavailable(LibFunc::acosl);
    128     TLI.setUnavailable(LibFunc::asinl);
    129     TLI.setUnavailable(LibFunc::atanl);
    130     TLI.setUnavailable(LibFunc::atan2l);
    131     TLI.setUnavailable(LibFunc::ceill);
    132     TLI.setUnavailable(LibFunc::copysignl);
    133     TLI.setUnavailable(LibFunc::cosl);
    134     TLI.setUnavailable(LibFunc::coshl);
    135     TLI.setUnavailable(LibFunc::expl);
    136     TLI.setUnavailable(LibFunc::fabsf); // Win32 and Win64 both lack fabsf
    137     TLI.setUnavailable(LibFunc::fabsl);
    138     TLI.setUnavailable(LibFunc::floorl);
    139     TLI.setUnavailable(LibFunc::fmaxl);
    140     TLI.setUnavailable(LibFunc::fminl);
    141     TLI.setUnavailable(LibFunc::fmodl);
    142     TLI.setUnavailable(LibFunc::frexpl);
    143     TLI.setUnavailable(LibFunc::ldexpf);
    144     TLI.setUnavailable(LibFunc::ldexpl);
    145     TLI.setUnavailable(LibFunc::logl);
    146     TLI.setUnavailable(LibFunc::modfl);
    147     TLI.setUnavailable(LibFunc::powl);
    148     TLI.setUnavailable(LibFunc::sinl);
    149     TLI.setUnavailable(LibFunc::sinhl);
    150     TLI.setUnavailable(LibFunc::sqrtl);
    151     TLI.setUnavailable(LibFunc::tanl);
    152     TLI.setUnavailable(LibFunc::tanhl);
    153 
    154     // Win32 only has C89 math
    155     TLI.setUnavailable(LibFunc::acosh);
    156     TLI.setUnavailable(LibFunc::acoshf);
    157     TLI.setUnavailable(LibFunc::acoshl);
    158     TLI.setUnavailable(LibFunc::asinh);
    159     TLI.setUnavailable(LibFunc::asinhf);
    160     TLI.setUnavailable(LibFunc::asinhl);
    161     TLI.setUnavailable(LibFunc::atanh);
    162     TLI.setUnavailable(LibFunc::atanhf);
    163     TLI.setUnavailable(LibFunc::atanhl);
    164     TLI.setUnavailable(LibFunc::cbrt);
    165     TLI.setUnavailable(LibFunc::cbrtf);
    166     TLI.setUnavailable(LibFunc::cbrtl);
    167     TLI.setUnavailable(LibFunc::exp2);
    168     TLI.setUnavailable(LibFunc::exp2f);
    169     TLI.setUnavailable(LibFunc::exp2l);
    170     TLI.setUnavailable(LibFunc::expm1);
    171     TLI.setUnavailable(LibFunc::expm1f);
    172     TLI.setUnavailable(LibFunc::expm1l);
    173     TLI.setUnavailable(LibFunc::log2);
    174     TLI.setUnavailable(LibFunc::log2f);
    175     TLI.setUnavailable(LibFunc::log2l);
    176     TLI.setUnavailable(LibFunc::log1p);
    177     TLI.setUnavailable(LibFunc::log1pf);
    178     TLI.setUnavailable(LibFunc::log1pl);
    179     TLI.setUnavailable(LibFunc::logb);
    180     TLI.setUnavailable(LibFunc::logbf);
    181     TLI.setUnavailable(LibFunc::logbl);
    182     TLI.setUnavailable(LibFunc::nearbyint);
    183     TLI.setUnavailable(LibFunc::nearbyintf);
    184     TLI.setUnavailable(LibFunc::nearbyintl);
    185     TLI.setUnavailable(LibFunc::rint);
    186     TLI.setUnavailable(LibFunc::rintf);
    187     TLI.setUnavailable(LibFunc::rintl);
    188     TLI.setUnavailable(LibFunc::round);
    189     TLI.setUnavailable(LibFunc::roundf);
    190     TLI.setUnavailable(LibFunc::roundl);
    191     TLI.setUnavailable(LibFunc::trunc);
    192     TLI.setUnavailable(LibFunc::truncf);
    193     TLI.setUnavailable(LibFunc::truncl);
    194 
    195     // Win32 provides some C99 math with mangled names
    196     TLI.setAvailableWithName(LibFunc::copysign, "_copysign");
    197 
    198     if (T.getArch() == Triple::x86) {
    199       // Win32 on x86 implements single-precision math functions as macros
    200       TLI.setUnavailable(LibFunc::acosf);
    201       TLI.setUnavailable(LibFunc::asinf);
    202       TLI.setUnavailable(LibFunc::atanf);
    203       TLI.setUnavailable(LibFunc::atan2f);
    204       TLI.setUnavailable(LibFunc::ceilf);
    205       TLI.setUnavailable(LibFunc::copysignf);
    206       TLI.setUnavailable(LibFunc::cosf);
    207       TLI.setUnavailable(LibFunc::coshf);
    208       TLI.setUnavailable(LibFunc::expf);
    209       TLI.setUnavailable(LibFunc::floorf);
    210       TLI.setUnavailable(LibFunc::fminf);
    211       TLI.setUnavailable(LibFunc::fmaxf);
    212       TLI.setUnavailable(LibFunc::fmodf);
    213       TLI.setUnavailable(LibFunc::logf);
    214       TLI.setUnavailable(LibFunc::log10f);
    215       TLI.setUnavailable(LibFunc::modff);
    216       TLI.setUnavailable(LibFunc::powf);
    217       TLI.setUnavailable(LibFunc::sinf);
    218       TLI.setUnavailable(LibFunc::sinhf);
    219       TLI.setUnavailable(LibFunc::sqrtf);
    220       TLI.setUnavailable(LibFunc::tanf);
    221       TLI.setUnavailable(LibFunc::tanhf);
    222     }
    223 
    224     // Win32 does *not* provide provide these functions, but they are
    225     // generally available on POSIX-compliant systems:
    226     TLI.setUnavailable(LibFunc::access);
    227     TLI.setUnavailable(LibFunc::bcmp);
    228     TLI.setUnavailable(LibFunc::bcopy);
    229     TLI.setUnavailable(LibFunc::bzero);
    230     TLI.setUnavailable(LibFunc::chmod);
    231     TLI.setUnavailable(LibFunc::chown);
    232     TLI.setUnavailable(LibFunc::closedir);
    233     TLI.setUnavailable(LibFunc::ctermid);
    234     TLI.setUnavailable(LibFunc::fdopen);
    235     TLI.setUnavailable(LibFunc::ffs);
    236     TLI.setUnavailable(LibFunc::fileno);
    237     TLI.setUnavailable(LibFunc::flockfile);
    238     TLI.setUnavailable(LibFunc::fseeko);
    239     TLI.setUnavailable(LibFunc::fstat);
    240     TLI.setUnavailable(LibFunc::fstatvfs);
    241     TLI.setUnavailable(LibFunc::ftello);
    242     TLI.setUnavailable(LibFunc::ftrylockfile);
    243     TLI.setUnavailable(LibFunc::funlockfile);
    244     TLI.setUnavailable(LibFunc::getc_unlocked);
    245     TLI.setUnavailable(LibFunc::getitimer);
    246     TLI.setUnavailable(LibFunc::getlogin_r);
    247     TLI.setUnavailable(LibFunc::getpwnam);
    248     TLI.setUnavailable(LibFunc::gettimeofday);
    249     TLI.setUnavailable(LibFunc::htonl);
    250     TLI.setUnavailable(LibFunc::htons);
    251     TLI.setUnavailable(LibFunc::lchown);
    252     TLI.setUnavailable(LibFunc::lstat);
    253     TLI.setUnavailable(LibFunc::memccpy);
    254     TLI.setUnavailable(LibFunc::mkdir);
    255     TLI.setUnavailable(LibFunc::ntohl);
    256     TLI.setUnavailable(LibFunc::ntohs);
    257     TLI.setUnavailable(LibFunc::open);
    258     TLI.setUnavailable(LibFunc::opendir);
    259     TLI.setUnavailable(LibFunc::pclose);
    260     TLI.setUnavailable(LibFunc::popen);
    261     TLI.setUnavailable(LibFunc::pread);
    262     TLI.setUnavailable(LibFunc::pwrite);
    263     TLI.setUnavailable(LibFunc::read);
    264     TLI.setUnavailable(LibFunc::readlink);
    265     TLI.setUnavailable(LibFunc::realpath);
    266     TLI.setUnavailable(LibFunc::rmdir);
    267     TLI.setUnavailable(LibFunc::setitimer);
    268     TLI.setUnavailable(LibFunc::stat);
    269     TLI.setUnavailable(LibFunc::statvfs);
    270     TLI.setUnavailable(LibFunc::stpcpy);
    271     TLI.setUnavailable(LibFunc::stpncpy);
    272     TLI.setUnavailable(LibFunc::strcasecmp);
    273     TLI.setUnavailable(LibFunc::strncasecmp);
    274     TLI.setUnavailable(LibFunc::times);
    275     TLI.setUnavailable(LibFunc::uname);
    276     TLI.setUnavailable(LibFunc::unlink);
    277     TLI.setUnavailable(LibFunc::unsetenv);
    278     TLI.setUnavailable(LibFunc::utime);
    279     TLI.setUnavailable(LibFunc::utimes);
    280     TLI.setUnavailable(LibFunc::write);
    281 
    282     // Win32 does *not* provide provide these functions, but they are
    283     // specified by C99:
    284     TLI.setUnavailable(LibFunc::atoll);
    285     TLI.setUnavailable(LibFunc::frexpf);
    286     TLI.setUnavailable(LibFunc::llabs);
    287   }
    288 
    289   switch (T.getOS()) {
    290   case Triple::MacOSX:
    291     // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
    292     // and their names are __exp10 and __exp10f. exp10l is not available on
    293     // OS X or iOS.
    294     TLI.setUnavailable(LibFunc::exp10l);
    295     if (T.isMacOSXVersionLT(10, 9)) {
    296       TLI.setUnavailable(LibFunc::exp10);
    297       TLI.setUnavailable(LibFunc::exp10f);
    298     } else {
    299       TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
    300       TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
    301     }
    302     break;
    303   case Triple::IOS:
    304   case Triple::TvOS:
    305   case Triple::WatchOS:
    306     TLI.setUnavailable(LibFunc::exp10l);
    307     if (!T.isWatchOS() && (T.isOSVersionLT(7, 0) ||
    308                            (T.isOSVersionLT(9, 0) &&
    309                             (T.getArch() == Triple::x86 ||
    310                              T.getArch() == Triple::x86_64)))) {
    311       TLI.setUnavailable(LibFunc::exp10);
    312       TLI.setUnavailable(LibFunc::exp10f);
    313     } else {
    314       TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
    315       TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
    316     }
    317     break;
    318   case Triple::Linux:
    319     // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
    320     // buggy prior to glibc version 2.18. Until this version is widely deployed
    321     // or we have a reasonable detection strategy, we cannot use exp10 reliably
    322     // on Linux.
    323     //
    324     // Fall through to disable all of them.
    325   default:
    326     TLI.setUnavailable(LibFunc::exp10);
    327     TLI.setUnavailable(LibFunc::exp10f);
    328     TLI.setUnavailable(LibFunc::exp10l);
    329   }
    330 
    331   // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
    332   // Linux (GLIBC):
    333   // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
    334   // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
    335   // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
    336   switch (T.getOS()) {
    337   case Triple::Darwin:
    338   case Triple::MacOSX:
    339   case Triple::IOS:
    340   case Triple::TvOS:
    341   case Triple::WatchOS:
    342   case Triple::FreeBSD:
    343   case Triple::Linux:
    344     break;
    345   default:
    346     TLI.setUnavailable(LibFunc::ffsl);
    347   }
    348 
    349   // ffsll is available on at least FreeBSD and Linux (GLIBC):
    350   // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
    351   // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
    352   switch (T.getOS()) {
    353   case Triple::Darwin:
    354   case Triple::MacOSX:
    355   case Triple::IOS:
    356   case Triple::TvOS:
    357   case Triple::WatchOS:
    358   case Triple::FreeBSD:
    359   case Triple::Linux:
    360     break;
    361   default:
    362     TLI.setUnavailable(LibFunc::ffsll);
    363   }
    364 
    365   // The following functions are available on at least FreeBSD:
    366   // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
    367   // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
    368   // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
    369   if (!T.isOSFreeBSD()) {
    370     TLI.setUnavailable(LibFunc::fls);
    371     TLI.setUnavailable(LibFunc::flsl);
    372     TLI.setUnavailable(LibFunc::flsll);
    373   }
    374 
    375   // The following functions are available on at least Linux:
    376   if (!T.isOSLinux()) {
    377     TLI.setUnavailable(LibFunc::dunder_strdup);
    378     TLI.setUnavailable(LibFunc::dunder_strtok_r);
    379     TLI.setUnavailable(LibFunc::dunder_isoc99_scanf);
    380     TLI.setUnavailable(LibFunc::dunder_isoc99_sscanf);
    381     TLI.setUnavailable(LibFunc::under_IO_getc);
    382     TLI.setUnavailable(LibFunc::under_IO_putc);
    383     TLI.setUnavailable(LibFunc::memalign);
    384     TLI.setUnavailable(LibFunc::fopen64);
    385     TLI.setUnavailable(LibFunc::fseeko64);
    386     TLI.setUnavailable(LibFunc::fstat64);
    387     TLI.setUnavailable(LibFunc::fstatvfs64);
    388     TLI.setUnavailable(LibFunc::ftello64);
    389     TLI.setUnavailable(LibFunc::lstat64);
    390     TLI.setUnavailable(LibFunc::open64);
    391     TLI.setUnavailable(LibFunc::stat64);
    392     TLI.setUnavailable(LibFunc::statvfs64);
    393     TLI.setUnavailable(LibFunc::tmpfile64);
    394   }
    395 
    396   // As currently implemented in clang, NVPTX code has no standard library to
    397   // speak of.  Headers provide a standard-ish library implementation, but many
    398   // of the signatures are wrong -- for example, many libm functions are not
    399   // extern "C".
    400   //
    401   // libdevice, an IR library provided by nvidia, is linked in by the front-end,
    402   // but only used functions are provided to llvm.  Moreover, most of the
    403   // functions in libdevice don't map precisely to standard library functions.
    404   //
    405   // FIXME: Having no standard library prevents e.g. many fastmath
    406   // optimizations, so this situation should be fixed.
    407   if (T.isNVPTX()) {
    408     TLI.disableAllFunctions();
    409     TLI.setAvailable(LibFunc::nvvm_reflect);
    410   } else {
    411     TLI.setUnavailable(LibFunc::nvvm_reflect);
    412   }
    413 
    414   TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary);
    415 }
    416 
    417 TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
    418   // Default to everything being available.
    419   memset(AvailableArray, -1, sizeof(AvailableArray));
    420 
    421   initialize(*this, Triple(), StandardNames);
    422 }
    423 
    424 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
    425   // Default to everything being available.
    426   memset(AvailableArray, -1, sizeof(AvailableArray));
    427 
    428   initialize(*this, T, StandardNames);
    429 }
    430 
    431 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
    432     : CustomNames(TLI.CustomNames) {
    433   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
    434   VectorDescs = TLI.VectorDescs;
    435   ScalarDescs = TLI.ScalarDescs;
    436 }
    437 
    438 TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
    439     : CustomNames(std::move(TLI.CustomNames)) {
    440   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
    441             AvailableArray);
    442   VectorDescs = TLI.VectorDescs;
    443   ScalarDescs = TLI.ScalarDescs;
    444 }
    445 
    446 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
    447   CustomNames = TLI.CustomNames;
    448   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
    449   return *this;
    450 }
    451 
    452 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
    453   CustomNames = std::move(TLI.CustomNames);
    454   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
    455             AvailableArray);
    456   return *this;
    457 }
    458 
    459 static StringRef sanitizeFunctionName(StringRef funcName) {
    460   // Filter out empty names and names containing null bytes, those can't be in
    461   // our table.
    462   if (funcName.empty() || funcName.find('\0') != StringRef::npos)
    463     return StringRef();
    464 
    465   // Check for \01 prefix that is used to mangle __asm declarations and
    466   // strip it if present.
    467   return GlobalValue::getRealLinkageName(funcName);
    468 }
    469 
    470 bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName,
    471                                        LibFunc::Func &F) const {
    472   const char *const *Start = &StandardNames[0];
    473   const char *const *End = &StandardNames[LibFunc::NumLibFuncs];
    474 
    475   funcName = sanitizeFunctionName(funcName);
    476   if (funcName.empty())
    477     return false;
    478 
    479   const char *const *I = std::lower_bound(
    480       Start, End, funcName, [](const char *LHS, StringRef RHS) {
    481         return std::strncmp(LHS, RHS.data(), RHS.size()) < 0;
    482       });
    483   if (I != End && *I == funcName) {
    484     F = (LibFunc::Func)(I - Start);
    485     return true;
    486   }
    487   return false;
    488 }
    489 
    490 bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
    491                                                    LibFunc::Func F,
    492                                                    const DataLayout *DL) const {
    493   LLVMContext &Ctx = FTy.getContext();
    494   Type *PCharTy = Type::getInt8PtrTy(Ctx);
    495   Type *SizeTTy = DL ? DL->getIntPtrType(Ctx, /*AS=*/0) : nullptr;
    496   auto IsSizeTTy = [SizeTTy](Type *Ty) {
    497     return SizeTTy ? Ty == SizeTTy : Ty->isIntegerTy();
    498   };
    499   unsigned NumParams = FTy.getNumParams();
    500 
    501   switch (F) {
    502   case LibFunc::strlen:
    503     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
    504             FTy.getReturnType()->isIntegerTy());
    505 
    506   case LibFunc::strchr:
    507   case LibFunc::strrchr:
    508     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
    509             FTy.getParamType(0) == FTy.getReturnType() &&
    510             FTy.getParamType(1)->isIntegerTy());
    511 
    512   case LibFunc::strtol:
    513   case LibFunc::strtod:
    514   case LibFunc::strtof:
    515   case LibFunc::strtoul:
    516   case LibFunc::strtoll:
    517   case LibFunc::strtold:
    518   case LibFunc::strtoull:
    519     return ((NumParams == 2 || NumParams == 3) &&
    520             FTy.getParamType(0)->isPointerTy() &&
    521             FTy.getParamType(1)->isPointerTy());
    522   case LibFunc::strcat:
    523     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
    524             FTy.getParamType(0) == FTy.getReturnType() &&
    525             FTy.getParamType(1) == FTy.getReturnType());
    526 
    527   case LibFunc::strncat:
    528     return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
    529             FTy.getParamType(0) == FTy.getReturnType() &&
    530             FTy.getParamType(1) == FTy.getReturnType() &&
    531             FTy.getParamType(2)->isIntegerTy());
    532 
    533   case LibFunc::strcpy_chk:
    534   case LibFunc::stpcpy_chk:
    535     --NumParams;
    536     if (!IsSizeTTy(FTy.getParamType(NumParams)))
    537       return false;
    538   // fallthrough
    539   case LibFunc::strcpy:
    540   case LibFunc::stpcpy:
    541     return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(0) &&
    542             FTy.getParamType(0) == FTy.getParamType(1) &&
    543             FTy.getParamType(0) == PCharTy);
    544 
    545   case LibFunc::strncpy_chk:
    546   case LibFunc::stpncpy_chk:
    547     --NumParams;
    548     if (!IsSizeTTy(FTy.getParamType(NumParams)))
    549       return false;
    550   // fallthrough
    551   case LibFunc::strncpy:
    552   case LibFunc::stpncpy:
    553     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
    554             FTy.getParamType(0) == FTy.getParamType(1) &&
    555             FTy.getParamType(0) == PCharTy &&
    556             FTy.getParamType(2)->isIntegerTy());
    557 
    558   case LibFunc::strxfrm:
    559     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
    560             FTy.getParamType(1)->isPointerTy());
    561 
    562   case LibFunc::strcmp:
    563     return (NumParams == 2 && FTy.getReturnType()->isIntegerTy(32) &&
    564             FTy.getParamType(0)->isPointerTy() &&
    565             FTy.getParamType(0) == FTy.getParamType(1));
    566 
    567   case LibFunc::strncmp:
    568     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
    569             FTy.getParamType(0)->isPointerTy() &&
    570             FTy.getParamType(0) == FTy.getParamType(1) &&
    571             FTy.getParamType(2)->isIntegerTy());
    572 
    573   case LibFunc::strspn:
    574   case LibFunc::strcspn:
    575     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
    576             FTy.getParamType(0) == FTy.getParamType(1) &&
    577             FTy.getReturnType()->isIntegerTy());
    578 
    579   case LibFunc::strcoll:
    580   case LibFunc::strcasecmp:
    581   case LibFunc::strncasecmp:
    582     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
    583             FTy.getParamType(1)->isPointerTy());
    584 
    585   case LibFunc::strstr:
    586     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
    587             FTy.getParamType(0)->isPointerTy() &&
    588             FTy.getParamType(1)->isPointerTy());
    589 
    590   case LibFunc::strpbrk:
    591     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
    592             FTy.getReturnType() == FTy.getParamType(0) &&
    593             FTy.getParamType(0) == FTy.getParamType(1));
    594 
    595   case LibFunc::strtok:
    596   case LibFunc::strtok_r:
    597     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
    598   case LibFunc::scanf:
    599   case LibFunc::setbuf:
    600   case LibFunc::setvbuf:
    601     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
    602   case LibFunc::strdup:
    603   case LibFunc::strndup:
    604     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
    605             FTy.getParamType(0)->isPointerTy());
    606   case LibFunc::sscanf:
    607   case LibFunc::stat:
    608   case LibFunc::statvfs:
    609   case LibFunc::sprintf:
    610     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
    611             FTy.getParamType(1)->isPointerTy());
    612   case LibFunc::snprintf:
    613     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
    614             FTy.getParamType(2)->isPointerTy());
    615   case LibFunc::setitimer:
    616     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
    617             FTy.getParamType(2)->isPointerTy());
    618   case LibFunc::system:
    619     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
    620   case LibFunc::malloc:
    621     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
    622   case LibFunc::memcmp:
    623     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
    624             FTy.getParamType(1)->isPointerTy() &&
    625             FTy.getReturnType()->isIntegerTy(32));
    626 
    627   case LibFunc::memchr:
    628   case LibFunc::memrchr:
    629     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
    630             FTy.getParamType(1)->isIntegerTy(32) &&
    631             FTy.getParamType(2)->isIntegerTy() &&
    632             FTy.getReturnType()->isPointerTy());
    633   case LibFunc::modf:
    634   case LibFunc::modff:
    635   case LibFunc::modfl:
    636     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
    637 
    638   case LibFunc::memcpy_chk:
    639   case LibFunc::memmove_chk:
    640     --NumParams;
    641     if (!IsSizeTTy(FTy.getParamType(NumParams)))
    642       return false;
    643   // fallthrough
    644   case LibFunc::memcpy:
    645   case LibFunc::memmove:
    646     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
    647             FTy.getParamType(0)->isPointerTy() &&
    648             FTy.getParamType(1)->isPointerTy() &&
    649             IsSizeTTy(FTy.getParamType(2)));
    650 
    651   case LibFunc::memset_chk:
    652     --NumParams;
    653     if (!IsSizeTTy(FTy.getParamType(NumParams)))
    654       return false;
    655   // fallthrough
    656   case LibFunc::memset:
    657     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
    658             FTy.getParamType(0)->isPointerTy() &&
    659             FTy.getParamType(1)->isIntegerTy() &&
    660             IsSizeTTy(FTy.getParamType(2)));
    661 
    662   case LibFunc::memccpy:
    663     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
    664   case LibFunc::memalign:
    665     return (FTy.getReturnType()->isPointerTy());
    666   case LibFunc::realloc:
    667     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
    668             FTy.getReturnType()->isPointerTy());
    669   case LibFunc::read:
    670     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
    671   case LibFunc::rewind:
    672   case LibFunc::rmdir:
    673   case LibFunc::remove:
    674   case LibFunc::realpath:
    675     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
    676   case LibFunc::rename:
    677     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
    678             FTy.getParamType(1)->isPointerTy());
    679   case LibFunc::readlink:
    680     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
    681             FTy.getParamType(1)->isPointerTy());
    682   case LibFunc::write:
    683     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
    684   case LibFunc::bcopy:
    685   case LibFunc::bcmp:
    686     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
    687             FTy.getParamType(1)->isPointerTy());
    688   case LibFunc::bzero:
    689     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
    690   case LibFunc::calloc:
    691     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
    692 
    693   case LibFunc::atof:
    694   case LibFunc::atoi:
    695   case LibFunc::atol:
    696   case LibFunc::atoll:
    697   case LibFunc::ferror:
    698   case LibFunc::getenv:
    699   case LibFunc::getpwnam:
    700   case LibFunc::pclose:
    701   case LibFunc::perror:
    702   case LibFunc::printf:
    703   case LibFunc::puts:
    704   case LibFunc::uname:
    705   case LibFunc::under_IO_getc:
    706   case LibFunc::unlink:
    707   case LibFunc::unsetenv:
    708     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
    709 
    710   case LibFunc::chmod:
    711   case LibFunc::chown:
    712   case LibFunc::clearerr:
    713   case LibFunc::closedir:
    714   case LibFunc::ctermid:
    715   case LibFunc::fclose:
    716   case LibFunc::feof:
    717   case LibFunc::fflush:
    718   case LibFunc::fgetc:
    719   case LibFunc::fileno:
    720   case LibFunc::flockfile:
    721   case LibFunc::free:
    722   case LibFunc::fseek:
    723   case LibFunc::fseeko64:
    724   case LibFunc::fseeko:
    725   case LibFunc::fsetpos:
    726   case LibFunc::ftell:
    727   case LibFunc::ftello64:
    728   case LibFunc::ftello:
    729   case LibFunc::ftrylockfile:
    730   case LibFunc::funlockfile:
    731   case LibFunc::getc:
    732   case LibFunc::getc_unlocked:
    733   case LibFunc::getlogin_r:
    734   case LibFunc::mkdir:
    735   case LibFunc::mktime:
    736   case LibFunc::times:
    737     return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
    738 
    739   case LibFunc::access:
    740     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
    741   case LibFunc::fopen:
    742     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
    743             FTy.getParamType(0)->isPointerTy() &&
    744             FTy.getParamType(1)->isPointerTy());
    745   case LibFunc::fdopen:
    746     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
    747             FTy.getParamType(1)->isPointerTy());
    748   case LibFunc::fputc:
    749   case LibFunc::fstat:
    750   case LibFunc::frexp:
    751   case LibFunc::frexpf:
    752   case LibFunc::frexpl:
    753   case LibFunc::fstatvfs:
    754     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
    755   case LibFunc::fgets:
    756     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
    757             FTy.getParamType(2)->isPointerTy());
    758   case LibFunc::fread:
    759     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
    760             FTy.getParamType(3)->isPointerTy());
    761   case LibFunc::fwrite:
    762     return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() &&
    763             FTy.getParamType(0)->isPointerTy() &&
    764             FTy.getParamType(1)->isIntegerTy() &&
    765             FTy.getParamType(2)->isIntegerTy() &&
    766             FTy.getParamType(3)->isPointerTy());
    767   case LibFunc::fputs:
    768     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
    769             FTy.getParamType(1)->isPointerTy());
    770   case LibFunc::fscanf:
    771   case LibFunc::fprintf:
    772     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
    773             FTy.getParamType(1)->isPointerTy());
    774   case LibFunc::fgetpos:
    775     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
    776             FTy.getParamType(1)->isPointerTy());
    777   case LibFunc::gets:
    778   case LibFunc::getchar:
    779   case LibFunc::getitimer:
    780     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
    781   case LibFunc::ungetc:
    782     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
    783   case LibFunc::utime:
    784   case LibFunc::utimes:
    785     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
    786             FTy.getParamType(1)->isPointerTy());
    787   case LibFunc::putc:
    788     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
    789   case LibFunc::pread:
    790   case LibFunc::pwrite:
    791     return (NumParams == 4 && FTy.getParamType(1)->isPointerTy());
    792   case LibFunc::popen:
    793     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
    794             FTy.getParamType(0)->isPointerTy() &&
    795             FTy.getParamType(1)->isPointerTy());
    796   case LibFunc::vscanf:
    797     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
    798   case LibFunc::vsscanf:
    799     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
    800             FTy.getParamType(2)->isPointerTy());
    801   case LibFunc::vfscanf:
    802     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
    803             FTy.getParamType(2)->isPointerTy());
    804   case LibFunc::valloc:
    805     return (FTy.getReturnType()->isPointerTy());
    806   case LibFunc::vprintf:
    807     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
    808   case LibFunc::vfprintf:
    809   case LibFunc::vsprintf:
    810     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
    811             FTy.getParamType(1)->isPointerTy());
    812   case LibFunc::vsnprintf:
    813     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
    814             FTy.getParamType(2)->isPointerTy());
    815   case LibFunc::open:
    816     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
    817   case LibFunc::opendir:
    818     return (NumParams == 1 && FTy.getReturnType()->isPointerTy() &&
    819             FTy.getParamType(0)->isPointerTy());
    820   case LibFunc::tmpfile:
    821     return (FTy.getReturnType()->isPointerTy());
    822   case LibFunc::htonl:
    823   case LibFunc::htons:
    824   case LibFunc::ntohl:
    825   case LibFunc::ntohs:
    826   case LibFunc::lstat:
    827     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
    828             FTy.getParamType(1)->isPointerTy());
    829   case LibFunc::lchown:
    830     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
    831   case LibFunc::qsort:
    832     return (NumParams == 4 && FTy.getParamType(3)->isPointerTy());
    833   case LibFunc::dunder_strdup:
    834   case LibFunc::dunder_strndup:
    835     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
    836             FTy.getParamType(0)->isPointerTy());
    837   case LibFunc::dunder_strtok_r:
    838     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
    839   case LibFunc::under_IO_putc:
    840     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
    841   case LibFunc::dunder_isoc99_scanf:
    842     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
    843   case LibFunc::stat64:
    844   case LibFunc::lstat64:
    845   case LibFunc::statvfs64:
    846     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy() &&
    847             FTy.getParamType(1)->isPointerTy());
    848   case LibFunc::dunder_isoc99_sscanf:
    849     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy() &&
    850             FTy.getParamType(1)->isPointerTy());
    851   case LibFunc::fopen64:
    852     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
    853             FTy.getParamType(0)->isPointerTy() &&
    854             FTy.getParamType(1)->isPointerTy());
    855   case LibFunc::tmpfile64:
    856     return (FTy.getReturnType()->isPointerTy());
    857   case LibFunc::fstat64:
    858   case LibFunc::fstatvfs64:
    859     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
    860   case LibFunc::open64:
    861     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
    862   case LibFunc::gettimeofday:
    863     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
    864             FTy.getParamType(1)->isPointerTy());
    865 
    866   case LibFunc::Znwj:                    // new(unsigned int);
    867   case LibFunc::Znwm:                    // new(unsigned long);
    868   case LibFunc::Znaj:                    // new[](unsigned int);
    869   case LibFunc::Znam:                    // new[](unsigned long);
    870   case LibFunc::msvc_new_int:            // new(unsigned int);
    871   case LibFunc::msvc_new_longlong:       // new(unsigned long long);
    872   case LibFunc::msvc_new_array_int:      // new[](unsigned int);
    873   case LibFunc::msvc_new_array_longlong: // new[](unsigned long long);
    874     return (NumParams == 1);
    875 
    876   case LibFunc::memset_pattern16:
    877     return (!FTy.isVarArg() && NumParams == 3 &&
    878             isa<PointerType>(FTy.getParamType(0)) &&
    879             isa<PointerType>(FTy.getParamType(1)) &&
    880             isa<IntegerType>(FTy.getParamType(2)));
    881 
    882   // int __nvvm_reflect(const char *);
    883   case LibFunc::nvvm_reflect:
    884     return (NumParams == 1 && isa<PointerType>(FTy.getParamType(0)));
    885 
    886   case LibFunc::sin:
    887   case LibFunc::sinf:
    888   case LibFunc::sinl:
    889   case LibFunc::cos:
    890   case LibFunc::cosf:
    891   case LibFunc::cosl:
    892   case LibFunc::tan:
    893   case LibFunc::tanf:
    894   case LibFunc::tanl:
    895   case LibFunc::exp:
    896   case LibFunc::expf:
    897   case LibFunc::expl:
    898   case LibFunc::exp2:
    899   case LibFunc::exp2f:
    900   case LibFunc::exp2l:
    901   case LibFunc::log:
    902   case LibFunc::logf:
    903   case LibFunc::logl:
    904   case LibFunc::log10:
    905   case LibFunc::log10f:
    906   case LibFunc::log10l:
    907   case LibFunc::log2:
    908   case LibFunc::log2f:
    909   case LibFunc::log2l:
    910   case LibFunc::fabs:
    911   case LibFunc::fabsf:
    912   case LibFunc::fabsl:
    913   case LibFunc::floor:
    914   case LibFunc::floorf:
    915   case LibFunc::floorl:
    916   case LibFunc::ceil:
    917   case LibFunc::ceilf:
    918   case LibFunc::ceill:
    919   case LibFunc::trunc:
    920   case LibFunc::truncf:
    921   case LibFunc::truncl:
    922   case LibFunc::rint:
    923   case LibFunc::rintf:
    924   case LibFunc::rintl:
    925   case LibFunc::nearbyint:
    926   case LibFunc::nearbyintf:
    927   case LibFunc::nearbyintl:
    928   case LibFunc::round:
    929   case LibFunc::roundf:
    930   case LibFunc::roundl:
    931   case LibFunc::sqrt:
    932   case LibFunc::sqrtf:
    933   case LibFunc::sqrtl:
    934     return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() &&
    935             FTy.getReturnType() == FTy.getParamType(0));
    936 
    937   case LibFunc::fmin:
    938   case LibFunc::fminf:
    939   case LibFunc::fminl:
    940   case LibFunc::fmax:
    941   case LibFunc::fmaxf:
    942   case LibFunc::fmaxl:
    943   case LibFunc::copysign:
    944   case LibFunc::copysignf:
    945   case LibFunc::copysignl:
    946   case LibFunc::pow:
    947   case LibFunc::powf:
    948   case LibFunc::powl:
    949     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
    950             FTy.getReturnType() == FTy.getParamType(0) &&
    951             FTy.getReturnType() == FTy.getParamType(1));
    952 
    953   case LibFunc::ffs:
    954   case LibFunc::ffsl:
    955   case LibFunc::ffsll:
    956   case LibFunc::isdigit:
    957   case LibFunc::isascii:
    958   case LibFunc::toascii:
    959     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
    960             FTy.getParamType(0)->isIntegerTy());
    961 
    962   case LibFunc::fls:
    963   case LibFunc::flsl:
    964   case LibFunc::flsll:
    965   case LibFunc::abs:
    966   case LibFunc::labs:
    967   case LibFunc::llabs:
    968     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() &&
    969             FTy.getReturnType() == FTy.getParamType(0));
    970 
    971   case LibFunc::cxa_atexit:
    972     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() &&
    973             FTy.getParamType(0)->isPointerTy() &&
    974             FTy.getParamType(1)->isPointerTy() &&
    975             FTy.getParamType(2)->isPointerTy());
    976 
    977   case LibFunc::sinpi:
    978   case LibFunc::cospi:
    979     return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() &&
    980             FTy.getReturnType() == FTy.getParamType(0));
    981 
    982   case LibFunc::sinpif:
    983   case LibFunc::cospif:
    984     return (NumParams == 1 && FTy.getReturnType()->isFloatTy() &&
    985             FTy.getReturnType() == FTy.getParamType(0));
    986 
    987   default:
    988     // Assume the other functions are correct.
    989     // FIXME: It'd be really nice to cover them all.
    990     return true;
    991   }
    992 }
    993 
    994 bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
    995                                        LibFunc::Func &F) const {
    996   const DataLayout *DL =
    997       FDecl.getParent() ? &FDecl.getParent()->getDataLayout() : nullptr;
    998   return getLibFunc(FDecl.getName(), F) &&
    999          isValidProtoForLibFunc(*FDecl.getFunctionType(), F, DL);
   1000 }
   1001 
   1002 void TargetLibraryInfoImpl::disableAllFunctions() {
   1003   memset(AvailableArray, 0, sizeof(AvailableArray));
   1004 }
   1005 
   1006 static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
   1007   return std::strncmp(LHS.ScalarFnName, RHS.ScalarFnName,
   1008                       std::strlen(RHS.ScalarFnName)) < 0;
   1009 }
   1010 
   1011 static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
   1012   return std::strncmp(LHS.VectorFnName, RHS.VectorFnName,
   1013                       std::strlen(RHS.VectorFnName)) < 0;
   1014 }
   1015 
   1016 static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
   1017   return std::strncmp(LHS.ScalarFnName, S.data(), S.size()) < 0;
   1018 }
   1019 
   1020 static bool compareWithVectorFnName(const VecDesc &LHS, StringRef S) {
   1021   return std::strncmp(LHS.VectorFnName, S.data(), S.size()) < 0;
   1022 }
   1023 
   1024 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
   1025   VectorDescs.insert(VectorDescs.end(), Fns.begin(), Fns.end());
   1026   std::sort(VectorDescs.begin(), VectorDescs.end(), compareByScalarFnName);
   1027 
   1028   ScalarDescs.insert(ScalarDescs.end(), Fns.begin(), Fns.end());
   1029   std::sort(ScalarDescs.begin(), ScalarDescs.end(), compareByVectorFnName);
   1030 }
   1031 
   1032 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
   1033     enum VectorLibrary VecLib) {
   1034   switch (VecLib) {
   1035   case Accelerate: {
   1036     const VecDesc VecFuncs[] = {
   1037         // Floating-Point Arithmetic and Auxiliary Functions
   1038         {"ceilf", "vceilf", 4},
   1039         {"fabsf", "vfabsf", 4},
   1040         {"llvm.fabs.f32", "vfabsf", 4},
   1041         {"floorf", "vfloorf", 4},
   1042         {"sqrtf", "vsqrtf", 4},
   1043         {"llvm.sqrt.f32", "vsqrtf", 4},
   1044 
   1045         // Exponential and Logarithmic Functions
   1046         {"expf", "vexpf", 4},
   1047         {"llvm.exp.f32", "vexpf", 4},
   1048         {"expm1f", "vexpm1f", 4},
   1049         {"logf", "vlogf", 4},
   1050         {"llvm.log.f32", "vlogf", 4},
   1051         {"log1pf", "vlog1pf", 4},
   1052         {"log10f", "vlog10f", 4},
   1053         {"llvm.log10.f32", "vlog10f", 4},
   1054         {"logbf", "vlogbf", 4},
   1055 
   1056         // Trigonometric Functions
   1057         {"sinf", "vsinf", 4},
   1058         {"llvm.sin.f32", "vsinf", 4},
   1059         {"cosf", "vcosf", 4},
   1060         {"llvm.cos.f32", "vcosf", 4},
   1061         {"tanf", "vtanf", 4},
   1062         {"asinf", "vasinf", 4},
   1063         {"acosf", "vacosf", 4},
   1064         {"atanf", "vatanf", 4},
   1065 
   1066         // Hyperbolic Functions
   1067         {"sinhf", "vsinhf", 4},
   1068         {"coshf", "vcoshf", 4},
   1069         {"tanhf", "vtanhf", 4},
   1070         {"asinhf", "vasinhf", 4},
   1071         {"acoshf", "vacoshf", 4},
   1072         {"atanhf", "vatanhf", 4},
   1073     };
   1074     addVectorizableFunctions(VecFuncs);
   1075     break;
   1076   }
   1077   case NoLibrary:
   1078     break;
   1079   }
   1080 }
   1081 
   1082 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
   1083   funcName = sanitizeFunctionName(funcName);
   1084   if (funcName.empty())
   1085     return false;
   1086 
   1087   std::vector<VecDesc>::const_iterator I = std::lower_bound(
   1088       VectorDescs.begin(), VectorDescs.end(), funcName,
   1089       compareWithScalarFnName);
   1090   return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
   1091 }
   1092 
   1093 StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
   1094                                                        unsigned VF) const {
   1095   F = sanitizeFunctionName(F);
   1096   if (F.empty())
   1097     return F;
   1098   std::vector<VecDesc>::const_iterator I = std::lower_bound(
   1099       VectorDescs.begin(), VectorDescs.end(), F, compareWithScalarFnName);
   1100   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
   1101     if (I->VectorizationFactor == VF)
   1102       return I->VectorFnName;
   1103     ++I;
   1104   }
   1105   return StringRef();
   1106 }
   1107 
   1108 StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F,
   1109                                                        unsigned &VF) const {
   1110   F = sanitizeFunctionName(F);
   1111   if (F.empty())
   1112     return F;
   1113 
   1114   std::vector<VecDesc>::const_iterator I = std::lower_bound(
   1115       ScalarDescs.begin(), ScalarDescs.end(), F, compareWithVectorFnName);
   1116   if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F)
   1117     return StringRef();
   1118   VF = I->VectorizationFactor;
   1119   return I->ScalarFnName;
   1120 }
   1121 
   1122 TargetLibraryInfo TargetLibraryAnalysis::run(Module &M,
   1123                                              ModuleAnalysisManager &) {
   1124   if (PresetInfoImpl)
   1125     return TargetLibraryInfo(*PresetInfoImpl);
   1126 
   1127   return TargetLibraryInfo(lookupInfoImpl(Triple(M.getTargetTriple())));
   1128 }
   1129 
   1130 TargetLibraryInfo TargetLibraryAnalysis::run(Function &F,
   1131                                              FunctionAnalysisManager &) {
   1132   if (PresetInfoImpl)
   1133     return TargetLibraryInfo(*PresetInfoImpl);
   1134 
   1135   return TargetLibraryInfo(
   1136       lookupInfoImpl(Triple(F.getParent()->getTargetTriple())));
   1137 }
   1138 
   1139 TargetLibraryInfoImpl &TargetLibraryAnalysis::lookupInfoImpl(const Triple &T) {
   1140   std::unique_ptr<TargetLibraryInfoImpl> &Impl =
   1141       Impls[T.normalize()];
   1142   if (!Impl)
   1143     Impl.reset(new TargetLibraryInfoImpl(T));
   1144 
   1145   return *Impl;
   1146 }
   1147 
   1148 
   1149 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
   1150     : ImmutablePass(ID), TLIImpl(), TLI(TLIImpl) {
   1151   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
   1152 }
   1153 
   1154 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
   1155     : ImmutablePass(ID), TLIImpl(T), TLI(TLIImpl) {
   1156   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
   1157 }
   1158 
   1159 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
   1160     const TargetLibraryInfoImpl &TLIImpl)
   1161     : ImmutablePass(ID), TLIImpl(TLIImpl), TLI(this->TLIImpl) {
   1162   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
   1163 }
   1164 
   1165 char TargetLibraryAnalysis::PassID;
   1166 
   1167 // Register the basic pass.
   1168 INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
   1169                 "Target Library Information", false, true)
   1170 char TargetLibraryInfoWrapperPass::ID = 0;
   1171 
   1172 void TargetLibraryInfoWrapperPass::anchor() {}
   1173