162d4215bSChandler Carruth //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==//
262d4215bSChandler Carruth //
362d4215bSChandler Carruth //                     The LLVM Compiler Infrastructure
462d4215bSChandler Carruth //
562d4215bSChandler Carruth // This file is distributed under the University of Illinois Open Source
662d4215bSChandler Carruth // License. See LICENSE.TXT for details.
762d4215bSChandler Carruth //
862d4215bSChandler Carruth //===----------------------------------------------------------------------===//
962d4215bSChandler Carruth //
1062d4215bSChandler Carruth // This file implements the TargetLibraryInfo class.
1162d4215bSChandler Carruth //
1262d4215bSChandler Carruth //===----------------------------------------------------------------------===//
1362d4215bSChandler Carruth 
1462d4215bSChandler Carruth #include "llvm/Analysis/TargetLibraryInfo.h"
1562d4215bSChandler Carruth #include "llvm/ADT/Triple.h"
166d8a2aa9SMichael Zolotukhin #include "llvm/Support/CommandLine.h"
1762d4215bSChandler Carruth using namespace llvm;
1862d4215bSChandler Carruth 
196d8a2aa9SMichael Zolotukhin static cl::opt<TargetLibraryInfoImpl::VectorLibrary> ClVectorLibrary(
206d8a2aa9SMichael Zolotukhin     "vector-library", cl::Hidden, cl::desc("Vector functions library"),
216d8a2aa9SMichael Zolotukhin     cl::init(TargetLibraryInfoImpl::NoLibrary),
226d8a2aa9SMichael Zolotukhin     cl::values(clEnumValN(TargetLibraryInfoImpl::NoLibrary, "none",
236d8a2aa9SMichael Zolotukhin                           "No vector functions library"),
246d8a2aa9SMichael Zolotukhin                clEnumValN(TargetLibraryInfoImpl::Accelerate, "Accelerate",
256d8a2aa9SMichael Zolotukhin                           "Accelerate framework"),
266d8a2aa9SMichael Zolotukhin                clEnumValEnd));
276d8a2aa9SMichael Zolotukhin 
2857a3d084SBenjamin Kramer const char *const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] = {
29cd3d25a2SJan Wen Voung #define TLI_DEFINE_STRING
30cd3d25a2SJan Wen Voung #include "llvm/Analysis/TargetLibraryInfo.def"
3162d4215bSChandler Carruth };
3262d4215bSChandler Carruth 
3362d4215bSChandler Carruth static bool hasSinCosPiStret(const Triple &T) {
3462d4215bSChandler Carruth   // Only Darwin variants have _stret versions of combined trig functions.
3562d4215bSChandler Carruth   if (!T.isOSDarwin())
3662d4215bSChandler Carruth     return false;
3762d4215bSChandler Carruth 
3862d4215bSChandler Carruth   // The ABI is rather complicated on x86, so don't do anything special there.
3962d4215bSChandler Carruth   if (T.getArch() == Triple::x86)
4062d4215bSChandler Carruth     return false;
4162d4215bSChandler Carruth 
4262d4215bSChandler Carruth   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
4362d4215bSChandler Carruth     return false;
4462d4215bSChandler Carruth 
4562d4215bSChandler Carruth   if (T.isiOS() && T.isOSVersionLT(7, 0))
4662d4215bSChandler Carruth     return false;
4762d4215bSChandler Carruth 
4862d4215bSChandler Carruth   return true;
4962d4215bSChandler Carruth }
5062d4215bSChandler Carruth 
5162d4215bSChandler Carruth /// initialize - Initialize the set of available library functions based on the
5262d4215bSChandler Carruth /// specified target triple.  This should be carefully written so that a missing
5362d4215bSChandler Carruth /// target triple gets a sane set of defaults.
54c0291865SChandler Carruth static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
5557a3d084SBenjamin Kramer                        const char *const *StandardNames) {
5662d4215bSChandler Carruth #ifndef NDEBUG
5762d4215bSChandler Carruth   // Verify that the StandardNames array is in alphabetical order.
5862d4215bSChandler Carruth   for (unsigned F = 1; F < LibFunc::NumLibFuncs; ++F) {
5962d4215bSChandler Carruth     if (strcmp(StandardNames[F-1], StandardNames[F]) >= 0)
60c0291865SChandler Carruth       llvm_unreachable("TargetLibraryInfoImpl function names must be sorted");
6162d4215bSChandler Carruth   }
6262d4215bSChandler Carruth #endif // !NDEBUG
6362d4215bSChandler Carruth 
6462d4215bSChandler Carruth   // There are no library implementations of mempcy and memset for AMD gpus and
6562d4215bSChandler Carruth   // these can be difficult to lower in the backend.
6662d4215bSChandler Carruth   if (T.getArch() == Triple::r600 ||
67*ddaa1c7eSJF Bastien       T.getArch() == Triple::amdgcn ||
68*ddaa1c7eSJF Bastien       T.getArch() == Triple::wasm32 ||
69*ddaa1c7eSJF Bastien       T.getArch() == Triple::wasm64) {
7062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::memcpy);
7162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::memset);
7262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::memset_pattern16);
7362d4215bSChandler Carruth     return;
7462d4215bSChandler Carruth   }
7562d4215bSChandler Carruth 
7662d4215bSChandler Carruth   // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
7762d4215bSChandler Carruth   if (T.isMacOSX()) {
7862d4215bSChandler Carruth     if (T.isMacOSXVersionLT(10, 5))
7962d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::memset_pattern16);
8062d4215bSChandler Carruth   } else if (T.isiOS()) {
8162d4215bSChandler Carruth     if (T.isOSVersionLT(3, 0))
8262d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::memset_pattern16);
8362d4215bSChandler Carruth   } else {
8462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::memset_pattern16);
8562d4215bSChandler Carruth   }
8662d4215bSChandler Carruth 
8762d4215bSChandler Carruth   if (!hasSinCosPiStret(T)) {
8862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sinpi);
8962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sinpif);
9062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::cospi);
9162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::cospif);
9262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sincospi_stret);
9362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sincospif_stret);
9462d4215bSChandler Carruth   }
9562d4215bSChandler Carruth 
9662d4215bSChandler Carruth   if (T.isMacOSX() && T.getArch() == Triple::x86 &&
9762d4215bSChandler Carruth       !T.isMacOSXVersionLT(10, 7)) {
9862d4215bSChandler Carruth     // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
9962d4215bSChandler Carruth     // we don't care about) have two versions; on recent OSX, the one we want
10062d4215bSChandler Carruth     // has a $UNIX2003 suffix. The two implementations are identical except
10162d4215bSChandler Carruth     // for the return value in some edge cases.  However, we don't want to
10262d4215bSChandler Carruth     // generate code that depends on the old symbols.
10362d4215bSChandler Carruth     TLI.setAvailableWithName(LibFunc::fwrite, "fwrite$UNIX2003");
10462d4215bSChandler Carruth     TLI.setAvailableWithName(LibFunc::fputs, "fputs$UNIX2003");
10562d4215bSChandler Carruth   }
10662d4215bSChandler Carruth 
10762d4215bSChandler Carruth   // iprintf and friends are only available on XCore and TCE.
10862d4215bSChandler Carruth   if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) {
10962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::iprintf);
11062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::siprintf);
11162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fiprintf);
11262d4215bSChandler Carruth   }
11362d4215bSChandler Carruth 
11462d4215bSChandler Carruth   if (T.isOSWindows() && !T.isOSCygMing()) {
11562d4215bSChandler Carruth     // Win32 does not support long double
11662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::acosl);
11762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::asinl);
11862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::atanl);
11962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::atan2l);
12062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ceill);
12162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::copysignl);
12262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::cosl);
12362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::coshl);
12462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::expl);
12562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fabsf); // Win32 and Win64 both lack fabsf
12662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fabsl);
12762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::floorl);
12862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fmaxl);
12962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fminl);
13062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fmodl);
13162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::frexpl);
13262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ldexpf);
13362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ldexpl);
13462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::logl);
13562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::modfl);
13662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::powl);
13762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sinl);
13862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sinhl);
13962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sqrtl);
14062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::tanl);
14162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::tanhl);
14262d4215bSChandler Carruth 
14362d4215bSChandler Carruth     // Win32 only has C89 math
14462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::acosh);
14562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::acoshf);
14662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::acoshl);
14762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::asinh);
14862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::asinhf);
14962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::asinhl);
15062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::atanh);
15162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::atanhf);
15262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::atanhl);
15362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::cbrt);
15462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::cbrtf);
15562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::cbrtl);
15662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp2);
15762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp2f);
15862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp2l);
15962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::expm1);
16062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::expm1f);
16162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::expm1l);
16262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::log2);
16362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::log2f);
16462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::log2l);
16562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::log1p);
16662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::log1pf);
16762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::log1pl);
16862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::logb);
16962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::logbf);
17062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::logbl);
17162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::nearbyint);
17262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::nearbyintf);
17362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::nearbyintl);
17462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::rint);
17562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::rintf);
17662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::rintl);
17762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::round);
17862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::roundf);
17962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::roundl);
18062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::trunc);
18162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::truncf);
18262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::truncl);
18362d4215bSChandler Carruth 
18462d4215bSChandler Carruth     // Win32 provides some C99 math with mangled names
18562d4215bSChandler Carruth     TLI.setAvailableWithName(LibFunc::copysign, "_copysign");
18662d4215bSChandler Carruth 
18762d4215bSChandler Carruth     if (T.getArch() == Triple::x86) {
18862d4215bSChandler Carruth       // Win32 on x86 implements single-precision math functions as macros
18962d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::acosf);
19062d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::asinf);
19162d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::atanf);
19262d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::atan2f);
19362d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::ceilf);
19462d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::copysignf);
19562d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::cosf);
19662d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::coshf);
19762d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::expf);
19862d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::floorf);
19962d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::fminf);
20062d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::fmaxf);
20162d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::fmodf);
20262d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::logf);
20362d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::powf);
20462d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::sinf);
20562d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::sinhf);
20662d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::sqrtf);
20762d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::tanf);
20862d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::tanhf);
20962d4215bSChandler Carruth     }
21062d4215bSChandler Carruth 
21162d4215bSChandler Carruth     // Win32 does *not* provide provide these functions, but they are
21262d4215bSChandler Carruth     // generally available on POSIX-compliant systems:
21362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::access);
21462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::bcmp);
21562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::bcopy);
21662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::bzero);
21762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::chmod);
21862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::chown);
21962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::closedir);
22062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ctermid);
22162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fdopen);
22262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ffs);
22362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fileno);
22462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::flockfile);
22562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fseeko);
22662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fstat);
22762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fstatvfs);
22862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ftello);
22962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ftrylockfile);
23062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::funlockfile);
23162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::getc_unlocked);
23262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::getitimer);
23362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::getlogin_r);
23462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::getpwnam);
23562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::gettimeofday);
23662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::htonl);
23762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::htons);
23862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::lchown);
23962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::lstat);
24062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::memccpy);
24162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::mkdir);
24262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ntohl);
24362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ntohs);
24462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::open);
24562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::opendir);
24662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::pclose);
24762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::popen);
24862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::pread);
24962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::pwrite);
25062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::read);
25162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::readlink);
25262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::realpath);
25362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::rmdir);
25462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::setitimer);
25562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::stat);
25662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::statvfs);
25762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::stpcpy);
25862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::stpncpy);
25962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::strcasecmp);
26062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::strncasecmp);
26162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::times);
26262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::uname);
26362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::unlink);
26462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::unsetenv);
26562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::utime);
26662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::utimes);
26762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::write);
26862d4215bSChandler Carruth 
26962d4215bSChandler Carruth     // Win32 does *not* provide provide these functions, but they are
27062d4215bSChandler Carruth     // specified by C99:
27162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::atoll);
27262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::frexpf);
27362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::llabs);
27462d4215bSChandler Carruth   }
27562d4215bSChandler Carruth 
27662d4215bSChandler Carruth   switch (T.getOS()) {
27762d4215bSChandler Carruth   case Triple::MacOSX:
27862d4215bSChandler Carruth     // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
27962d4215bSChandler Carruth     // and their names are __exp10 and __exp10f. exp10l is not available on
28062d4215bSChandler Carruth     // OS X or iOS.
28162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp10l);
28262d4215bSChandler Carruth     if (T.isMacOSXVersionLT(10, 9)) {
28362d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::exp10);
28462d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::exp10f);
28562d4215bSChandler Carruth     } else {
28662d4215bSChandler Carruth       TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
28762d4215bSChandler Carruth       TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
28862d4215bSChandler Carruth     }
28962d4215bSChandler Carruth     break;
29062d4215bSChandler Carruth   case Triple::IOS:
29162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp10l);
29262d4215bSChandler Carruth     if (T.isOSVersionLT(7, 0)) {
29362d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::exp10);
29462d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::exp10f);
29562d4215bSChandler Carruth     } else {
29662d4215bSChandler Carruth       TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
29762d4215bSChandler Carruth       TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
29862d4215bSChandler Carruth     }
29962d4215bSChandler Carruth     break;
30062d4215bSChandler Carruth   case Triple::Linux:
30162d4215bSChandler Carruth     // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
30262d4215bSChandler Carruth     // buggy prior to glibc version 2.18. Until this version is widely deployed
30362d4215bSChandler Carruth     // or we have a reasonable detection strategy, we cannot use exp10 reliably
30462d4215bSChandler Carruth     // on Linux.
30562d4215bSChandler Carruth     //
30662d4215bSChandler Carruth     // Fall through to disable all of them.
30762d4215bSChandler Carruth   default:
30862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp10);
30962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp10f);
31062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp10l);
31162d4215bSChandler Carruth   }
31262d4215bSChandler Carruth 
31362d4215bSChandler Carruth   // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
31462d4215bSChandler Carruth   // Linux (GLIBC):
31562d4215bSChandler Carruth   // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
31662d4215bSChandler Carruth   // http://svn.freebsd.org/base/user/eri/pf45/head/lib/libc/string/ffsl.c
31762d4215bSChandler Carruth   // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
31862d4215bSChandler Carruth   switch (T.getOS()) {
31962d4215bSChandler Carruth   case Triple::Darwin:
32062d4215bSChandler Carruth   case Triple::MacOSX:
32162d4215bSChandler Carruth   case Triple::IOS:
32262d4215bSChandler Carruth   case Triple::FreeBSD:
32362d4215bSChandler Carruth   case Triple::Linux:
32462d4215bSChandler Carruth     break;
32562d4215bSChandler Carruth   default:
32662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ffsl);
32762d4215bSChandler Carruth   }
32862d4215bSChandler Carruth 
32962d4215bSChandler Carruth   // ffsll is available on at least FreeBSD and Linux (GLIBC):
33062d4215bSChandler Carruth   // http://svn.freebsd.org/base/user/eri/pf45/head/lib/libc/string/ffsll.c
33162d4215bSChandler Carruth   // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
33262d4215bSChandler Carruth   switch (T.getOS()) {
33362d4215bSChandler Carruth   case Triple::FreeBSD:
33462d4215bSChandler Carruth   case Triple::Linux:
33562d4215bSChandler Carruth     break;
33662d4215bSChandler Carruth   default:
33762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ffsll);
33862d4215bSChandler Carruth   }
33962d4215bSChandler Carruth 
34062d4215bSChandler Carruth   // The following functions are available on at least Linux:
34162d4215bSChandler Carruth   if (!T.isOSLinux()) {
34262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::dunder_strdup);
34362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::dunder_strtok_r);
34462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::dunder_isoc99_scanf);
34562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::dunder_isoc99_sscanf);
34662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::under_IO_getc);
34762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::under_IO_putc);
34862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::memalign);
34962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fopen64);
35062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fseeko64);
35162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fstat64);
35262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fstatvfs64);
35362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ftello64);
35462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::lstat64);
35562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::open64);
35662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::stat64);
35762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::statvfs64);
35862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::tmpfile64);
35962d4215bSChandler Carruth   }
3606d8a2aa9SMichael Zolotukhin 
3616d8a2aa9SMichael Zolotukhin   TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary);
36262d4215bSChandler Carruth }
36362d4215bSChandler Carruth 
364c0291865SChandler Carruth TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
36562d4215bSChandler Carruth   // Default to everything being available.
36662d4215bSChandler Carruth   memset(AvailableArray, -1, sizeof(AvailableArray));
36762d4215bSChandler Carruth 
36862d4215bSChandler Carruth   initialize(*this, Triple(), StandardNames);
36962d4215bSChandler Carruth }
37062d4215bSChandler Carruth 
371c0291865SChandler Carruth TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
37262d4215bSChandler Carruth   // Default to everything being available.
37362d4215bSChandler Carruth   memset(AvailableArray, -1, sizeof(AvailableArray));
37462d4215bSChandler Carruth 
37562d4215bSChandler Carruth   initialize(*this, T, StandardNames);
37662d4215bSChandler Carruth }
37762d4215bSChandler Carruth 
378c0291865SChandler Carruth TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
3798ca43224SChandler Carruth     : CustomNames(TLI.CustomNames) {
38062d4215bSChandler Carruth   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
381e8f2551fSMichael Zolotukhin   VectorDescs = TLI.VectorDescs;
382e8f2551fSMichael Zolotukhin   ScalarDescs = TLI.ScalarDescs;
3838ca43224SChandler Carruth }
3848ca43224SChandler Carruth 
385c0291865SChandler Carruth TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
3868ca43224SChandler Carruth     : CustomNames(std::move(TLI.CustomNames)) {
3878ca43224SChandler Carruth   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
3888ca43224SChandler Carruth             AvailableArray);
389e8f2551fSMichael Zolotukhin   VectorDescs = TLI.VectorDescs;
390e8f2551fSMichael Zolotukhin   ScalarDescs = TLI.ScalarDescs;
3918ca43224SChandler Carruth }
3928ca43224SChandler Carruth 
393c0291865SChandler Carruth TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
39462d4215bSChandler Carruth   CustomNames = TLI.CustomNames;
3958ca43224SChandler Carruth   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
3968ca43224SChandler Carruth   return *this;
3978ca43224SChandler Carruth }
3988ca43224SChandler Carruth 
399c0291865SChandler Carruth TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
4008ca43224SChandler Carruth   CustomNames = std::move(TLI.CustomNames);
4018ca43224SChandler Carruth   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
4028ca43224SChandler Carruth             AvailableArray);
4038ca43224SChandler Carruth   return *this;
40462d4215bSChandler Carruth }
40562d4215bSChandler Carruth 
40621abdf98SMichael Zolotukhin static StringRef sanitizeFunctionName(StringRef funcName) {
40762d4215bSChandler Carruth   // Filter out empty names and names containing null bytes, those can't be in
40862d4215bSChandler Carruth   // our table.
40962d4215bSChandler Carruth   if (funcName.empty() || funcName.find('\0') != StringRef::npos)
41021abdf98SMichael Zolotukhin     return StringRef();
41162d4215bSChandler Carruth 
41262d4215bSChandler Carruth   // Check for \01 prefix that is used to mangle __asm declarations and
41362d4215bSChandler Carruth   // strip it if present.
414cde33036SDavid Majnemer   return GlobalValue::getRealLinkageName(funcName);
41521abdf98SMichael Zolotukhin }
41621abdf98SMichael Zolotukhin 
41721abdf98SMichael Zolotukhin bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName,
41821abdf98SMichael Zolotukhin                                    LibFunc::Func &F) const {
41957a3d084SBenjamin Kramer   const char *const *Start = &StandardNames[0];
42057a3d084SBenjamin Kramer   const char *const *End = &StandardNames[LibFunc::NumLibFuncs];
42121abdf98SMichael Zolotukhin 
42221abdf98SMichael Zolotukhin   funcName = sanitizeFunctionName(funcName);
42321abdf98SMichael Zolotukhin   if (funcName.empty())
42421abdf98SMichael Zolotukhin     return false;
42521abdf98SMichael Zolotukhin 
42657a3d084SBenjamin Kramer   const char *const *I = std::lower_bound(
427d3b76a3bSMichael Zolotukhin       Start, End, funcName, [](const char *LHS, StringRef RHS) {
428d3b76a3bSMichael Zolotukhin         return std::strncmp(LHS, RHS.data(), RHS.size()) < 0;
429d3b76a3bSMichael Zolotukhin       });
43062d4215bSChandler Carruth   if (I != End && *I == funcName) {
43162d4215bSChandler Carruth     F = (LibFunc::Func)(I - Start);
43262d4215bSChandler Carruth     return true;
43362d4215bSChandler Carruth   }
43462d4215bSChandler Carruth   return false;
43562d4215bSChandler Carruth }
43662d4215bSChandler Carruth 
437c0291865SChandler Carruth void TargetLibraryInfoImpl::disableAllFunctions() {
43862d4215bSChandler Carruth   memset(AvailableArray, 0, sizeof(AvailableArray));
43962d4215bSChandler Carruth }
440b98f63dbSChandler Carruth 
441e8f2551fSMichael Zolotukhin static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
442e8f2551fSMichael Zolotukhin   return std::strncmp(LHS.ScalarFnName, RHS.ScalarFnName,
443e8f2551fSMichael Zolotukhin                       std::strlen(RHS.ScalarFnName)) < 0;
444e8f2551fSMichael Zolotukhin }
445e8f2551fSMichael Zolotukhin 
446e8f2551fSMichael Zolotukhin static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
447e8f2551fSMichael Zolotukhin   return std::strncmp(LHS.VectorFnName, RHS.VectorFnName,
448e8f2551fSMichael Zolotukhin                       std::strlen(RHS.VectorFnName)) < 0;
449e8f2551fSMichael Zolotukhin }
450e8f2551fSMichael Zolotukhin 
451e8f2551fSMichael Zolotukhin static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
452e8f2551fSMichael Zolotukhin   return std::strncmp(LHS.ScalarFnName, S.data(), S.size()) < 0;
453e8f2551fSMichael Zolotukhin }
454e8f2551fSMichael Zolotukhin 
455e8f2551fSMichael Zolotukhin static bool compareWithVectorFnName(const VecDesc &LHS, StringRef S) {
456e8f2551fSMichael Zolotukhin   return std::strncmp(LHS.VectorFnName, S.data(), S.size()) < 0;
457e8f2551fSMichael Zolotukhin }
458e8f2551fSMichael Zolotukhin 
459e8f2551fSMichael Zolotukhin void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
460e8f2551fSMichael Zolotukhin   VectorDescs.insert(VectorDescs.end(), Fns.begin(), Fns.end());
461e8f2551fSMichael Zolotukhin   std::sort(VectorDescs.begin(), VectorDescs.end(), compareByScalarFnName);
462e8f2551fSMichael Zolotukhin 
463e8f2551fSMichael Zolotukhin   ScalarDescs.insert(ScalarDescs.end(), Fns.begin(), Fns.end());
464e8f2551fSMichael Zolotukhin   std::sort(ScalarDescs.begin(), ScalarDescs.end(), compareByVectorFnName);
465e8f2551fSMichael Zolotukhin }
466e8f2551fSMichael Zolotukhin 
4676d8a2aa9SMichael Zolotukhin void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
4686d8a2aa9SMichael Zolotukhin     enum VectorLibrary VecLib) {
4696d8a2aa9SMichael Zolotukhin   switch (VecLib) {
4706d8a2aa9SMichael Zolotukhin   case Accelerate: {
4716d8a2aa9SMichael Zolotukhin     const VecDesc VecFuncs[] = {
472de63aaceSMichael Zolotukhin         // Floating-Point Arithmetic and Auxiliary Functions
473de63aaceSMichael Zolotukhin         {"ceilf", "vceilf", 4},
4746d8a2aa9SMichael Zolotukhin         {"fabsf", "vfabsf", 4},
4756d8a2aa9SMichael Zolotukhin         {"llvm.fabs.f32", "vfabsf", 4},
476de63aaceSMichael Zolotukhin         {"floorf", "vfloorf", 4},
477de63aaceSMichael Zolotukhin         {"sqrtf", "vsqrtf", 4},
478de63aaceSMichael Zolotukhin         {"llvm.sqrt.f32", "vsqrtf", 4},
479de63aaceSMichael Zolotukhin 
480de63aaceSMichael Zolotukhin         // Exponential and Logarithmic Functions
481de63aaceSMichael Zolotukhin         {"expf", "vexpf", 4},
482de63aaceSMichael Zolotukhin         {"llvm.exp.f32", "vexpf", 4},
483de63aaceSMichael Zolotukhin         {"expm1f", "vexpm1f", 4},
484de63aaceSMichael Zolotukhin         {"logf", "vlogf", 4},
485de63aaceSMichael Zolotukhin         {"llvm.log.f32", "vlogf", 4},
486de63aaceSMichael Zolotukhin         {"log1pf", "vlog1pf", 4},
487de63aaceSMichael Zolotukhin         {"log10f", "vlog10f", 4},
488de63aaceSMichael Zolotukhin         {"llvm.log10.f32", "vlog10f", 4},
489de63aaceSMichael Zolotukhin         {"logbf", "vlogbf", 4},
490de63aaceSMichael Zolotukhin 
491de63aaceSMichael Zolotukhin         // Trigonometric Functions
492de63aaceSMichael Zolotukhin         {"sinf", "vsinf", 4},
493de63aaceSMichael Zolotukhin         {"llvm.sin.f32", "vsinf", 4},
494de63aaceSMichael Zolotukhin         {"cosf", "vcosf", 4},
495de63aaceSMichael Zolotukhin         {"llvm.cos.f32", "vcosf", 4},
496de63aaceSMichael Zolotukhin         {"tanf", "vtanf", 4},
497de63aaceSMichael Zolotukhin         {"asinf", "vasinf", 4},
498de63aaceSMichael Zolotukhin         {"acosf", "vacosf", 4},
499de63aaceSMichael Zolotukhin         {"atanf", "vatanf", 4},
500de63aaceSMichael Zolotukhin 
501de63aaceSMichael Zolotukhin         // Hyperbolic Functions
502de63aaceSMichael Zolotukhin         {"sinhf", "vsinhf", 4},
503de63aaceSMichael Zolotukhin         {"coshf", "vcoshf", 4},
504de63aaceSMichael Zolotukhin         {"tanhf", "vtanhf", 4},
505de63aaceSMichael Zolotukhin         {"asinhf", "vasinhf", 4},
506de63aaceSMichael Zolotukhin         {"acoshf", "vacoshf", 4},
507de63aaceSMichael Zolotukhin         {"atanhf", "vatanhf", 4},
5086d8a2aa9SMichael Zolotukhin     };
5096d8a2aa9SMichael Zolotukhin     addVectorizableFunctions(VecFuncs);
5106d8a2aa9SMichael Zolotukhin     break;
5116d8a2aa9SMichael Zolotukhin   }
5126d8a2aa9SMichael Zolotukhin   case NoLibrary:
5136d8a2aa9SMichael Zolotukhin     break;
5146d8a2aa9SMichael Zolotukhin   }
5156d8a2aa9SMichael Zolotukhin }
5166d8a2aa9SMichael Zolotukhin 
517e8f2551fSMichael Zolotukhin bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
518e8f2551fSMichael Zolotukhin   funcName = sanitizeFunctionName(funcName);
519e8f2551fSMichael Zolotukhin   if (funcName.empty())
520e8f2551fSMichael Zolotukhin     return false;
521e8f2551fSMichael Zolotukhin 
522e8f2551fSMichael Zolotukhin   std::vector<VecDesc>::const_iterator I = std::lower_bound(
523e8f2551fSMichael Zolotukhin       VectorDescs.begin(), VectorDescs.end(), funcName,
524e8f2551fSMichael Zolotukhin       compareWithScalarFnName);
525e8f2551fSMichael Zolotukhin   return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
526e8f2551fSMichael Zolotukhin }
527e8f2551fSMichael Zolotukhin 
528e8f2551fSMichael Zolotukhin StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
529e8f2551fSMichael Zolotukhin                                                        unsigned VF) const {
530e8f2551fSMichael Zolotukhin   F = sanitizeFunctionName(F);
531e8f2551fSMichael Zolotukhin   if (F.empty())
532e8f2551fSMichael Zolotukhin     return F;
533e8f2551fSMichael Zolotukhin   std::vector<VecDesc>::const_iterator I = std::lower_bound(
534e8f2551fSMichael Zolotukhin       VectorDescs.begin(), VectorDescs.end(), F, compareWithScalarFnName);
535e8f2551fSMichael Zolotukhin   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
536e8f2551fSMichael Zolotukhin     if (I->VectorizationFactor == VF)
537e8f2551fSMichael Zolotukhin       return I->VectorFnName;
538e8f2551fSMichael Zolotukhin     ++I;
539e8f2551fSMichael Zolotukhin   }
540e8f2551fSMichael Zolotukhin   return StringRef();
541e8f2551fSMichael Zolotukhin }
542e8f2551fSMichael Zolotukhin 
543e8f2551fSMichael Zolotukhin StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F,
544e8f2551fSMichael Zolotukhin                                                        unsigned &VF) const {
545e8f2551fSMichael Zolotukhin   F = sanitizeFunctionName(F);
546e8f2551fSMichael Zolotukhin   if (F.empty())
547e8f2551fSMichael Zolotukhin     return F;
548e8f2551fSMichael Zolotukhin 
549e8f2551fSMichael Zolotukhin   std::vector<VecDesc>::const_iterator I = std::lower_bound(
550e8f2551fSMichael Zolotukhin       ScalarDescs.begin(), ScalarDescs.end(), F, compareWithVectorFnName);
551e8f2551fSMichael Zolotukhin   if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F)
552e8f2551fSMichael Zolotukhin     return StringRef();
553e8f2551fSMichael Zolotukhin   VF = I->VectorizationFactor;
554e8f2551fSMichael Zolotukhin   return I->ScalarFnName;
555e8f2551fSMichael Zolotukhin }
556e8f2551fSMichael Zolotukhin 
557c0291865SChandler Carruth TargetLibraryInfo TargetLibraryAnalysis::run(Module &M) {
558c0291865SChandler Carruth   if (PresetInfoImpl)
559c0291865SChandler Carruth     return TargetLibraryInfo(*PresetInfoImpl);
560c0291865SChandler Carruth 
561c0291865SChandler Carruth   return TargetLibraryInfo(lookupInfoImpl(Triple(M.getTargetTriple())));
562c0291865SChandler Carruth }
563c0291865SChandler Carruth 
564c0291865SChandler Carruth TargetLibraryInfo TargetLibraryAnalysis::run(Function &F) {
565c0291865SChandler Carruth   if (PresetInfoImpl)
566c0291865SChandler Carruth     return TargetLibraryInfo(*PresetInfoImpl);
567c0291865SChandler Carruth 
568c0291865SChandler Carruth   return TargetLibraryInfo(
569c0291865SChandler Carruth       lookupInfoImpl(Triple(F.getParent()->getTargetTriple())));
570c0291865SChandler Carruth }
571c0291865SChandler Carruth 
572c0291865SChandler Carruth TargetLibraryInfoImpl &TargetLibraryAnalysis::lookupInfoImpl(Triple T) {
573c0291865SChandler Carruth   std::unique_ptr<TargetLibraryInfoImpl> &Impl =
574c0291865SChandler Carruth       Impls[T.normalize()];
575c0291865SChandler Carruth   if (!Impl)
576c0291865SChandler Carruth     Impl.reset(new TargetLibraryInfoImpl(T));
577c0291865SChandler Carruth 
578c0291865SChandler Carruth   return *Impl;
579c0291865SChandler Carruth }
580c0291865SChandler Carruth 
581c0291865SChandler Carruth 
582b98f63dbSChandler Carruth TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
583c0291865SChandler Carruth     : ImmutablePass(ID), TLIImpl(), TLI(TLIImpl) {
584b98f63dbSChandler Carruth   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
585b98f63dbSChandler Carruth }
586b98f63dbSChandler Carruth 
587b98f63dbSChandler Carruth TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
588c0291865SChandler Carruth     : ImmutablePass(ID), TLIImpl(T), TLI(TLIImpl) {
589b98f63dbSChandler Carruth   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
590b98f63dbSChandler Carruth }
591b98f63dbSChandler Carruth 
592b98f63dbSChandler Carruth TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
593c0291865SChandler Carruth     const TargetLibraryInfoImpl &TLIImpl)
594c0291865SChandler Carruth     : ImmutablePass(ID), TLIImpl(TLIImpl), TLI(this->TLIImpl) {
595b98f63dbSChandler Carruth   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
596b98f63dbSChandler Carruth }
597b98f63dbSChandler Carruth 
5988ca43224SChandler Carruth char TargetLibraryAnalysis::PassID;
5998ca43224SChandler Carruth 
600b98f63dbSChandler Carruth // Register the basic pass.
601b98f63dbSChandler Carruth INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
602b98f63dbSChandler Carruth                 "Target Library Information", false, true)
603b98f63dbSChandler Carruth char TargetLibraryInfoWrapperPass::ID = 0;
604b98f63dbSChandler Carruth 
605b98f63dbSChandler Carruth void TargetLibraryInfoWrapperPass::anchor() {}
606