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 ||
6762d4215bSChandler Carruth       T.getArch() == Triple::amdgcn) {
6862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::memcpy);
6962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::memset);
7062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::memset_pattern16);
7162d4215bSChandler Carruth     return;
7262d4215bSChandler Carruth   }
7362d4215bSChandler Carruth 
7462d4215bSChandler Carruth   // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
7562d4215bSChandler Carruth   if (T.isMacOSX()) {
7662d4215bSChandler Carruth     if (T.isMacOSXVersionLT(10, 5))
7762d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::memset_pattern16);
7862d4215bSChandler Carruth   } else if (T.isiOS()) {
7962d4215bSChandler Carruth     if (T.isOSVersionLT(3, 0))
8062d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::memset_pattern16);
8162d4215bSChandler Carruth   } else {
8262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::memset_pattern16);
8362d4215bSChandler Carruth   }
8462d4215bSChandler Carruth 
8562d4215bSChandler Carruth   if (!hasSinCosPiStret(T)) {
8662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sinpi);
8762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sinpif);
8862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::cospi);
8962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::cospif);
9062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sincospi_stret);
9162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sincospif_stret);
9262d4215bSChandler Carruth   }
9362d4215bSChandler Carruth 
9462d4215bSChandler Carruth   if (T.isMacOSX() && T.getArch() == Triple::x86 &&
9562d4215bSChandler Carruth       !T.isMacOSXVersionLT(10, 7)) {
9662d4215bSChandler Carruth     // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
9762d4215bSChandler Carruth     // we don't care about) have two versions; on recent OSX, the one we want
9862d4215bSChandler Carruth     // has a $UNIX2003 suffix. The two implementations are identical except
9962d4215bSChandler Carruth     // for the return value in some edge cases.  However, we don't want to
10062d4215bSChandler Carruth     // generate code that depends on the old symbols.
10162d4215bSChandler Carruth     TLI.setAvailableWithName(LibFunc::fwrite, "fwrite$UNIX2003");
10262d4215bSChandler Carruth     TLI.setAvailableWithName(LibFunc::fputs, "fputs$UNIX2003");
10362d4215bSChandler Carruth   }
10462d4215bSChandler Carruth 
10562d4215bSChandler Carruth   // iprintf and friends are only available on XCore and TCE.
10662d4215bSChandler Carruth   if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) {
10762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::iprintf);
10862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::siprintf);
10962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fiprintf);
11062d4215bSChandler Carruth   }
11162d4215bSChandler Carruth 
11262d4215bSChandler Carruth   if (T.isOSWindows() && !T.isOSCygMing()) {
11362d4215bSChandler Carruth     // Win32 does not support long double
11462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::acosl);
11562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::asinl);
11662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::atanl);
11762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::atan2l);
11862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ceill);
11962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::copysignl);
12062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::cosl);
12162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::coshl);
12262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::expl);
12362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fabsf); // Win32 and Win64 both lack fabsf
12462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fabsl);
12562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::floorl);
12662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fmaxl);
12762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fminl);
12862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fmodl);
12962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::frexpl);
13062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ldexpf);
13162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ldexpl);
13262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::logl);
13362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::modfl);
13462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::powl);
13562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sinl);
13662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sinhl);
13762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::sqrtl);
13862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::tanl);
13962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::tanhl);
14062d4215bSChandler Carruth 
14162d4215bSChandler Carruth     // Win32 only has C89 math
14262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::acosh);
14362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::acoshf);
14462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::acoshl);
14562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::asinh);
14662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::asinhf);
14762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::asinhl);
14862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::atanh);
14962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::atanhf);
15062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::atanhl);
15162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::cbrt);
15262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::cbrtf);
15362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::cbrtl);
15462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp2);
15562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp2f);
15662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp2l);
15762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::expm1);
15862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::expm1f);
15962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::expm1l);
16062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::log2);
16162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::log2f);
16262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::log2l);
16362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::log1p);
16462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::log1pf);
16562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::log1pl);
16662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::logb);
16762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::logbf);
16862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::logbl);
16962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::nearbyint);
17062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::nearbyintf);
17162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::nearbyintl);
17262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::rint);
17362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::rintf);
17462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::rintl);
17562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::round);
17662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::roundf);
17762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::roundl);
17862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::trunc);
17962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::truncf);
18062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::truncl);
18162d4215bSChandler Carruth 
18262d4215bSChandler Carruth     // Win32 provides some C99 math with mangled names
18362d4215bSChandler Carruth     TLI.setAvailableWithName(LibFunc::copysign, "_copysign");
18462d4215bSChandler Carruth 
18562d4215bSChandler Carruth     if (T.getArch() == Triple::x86) {
18662d4215bSChandler Carruth       // Win32 on x86 implements single-precision math functions as macros
18762d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::acosf);
18862d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::asinf);
18962d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::atanf);
19062d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::atan2f);
19162d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::ceilf);
19262d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::copysignf);
19362d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::cosf);
19462d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::coshf);
19562d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::expf);
19662d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::floorf);
19762d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::fminf);
19862d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::fmaxf);
19962d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::fmodf);
20062d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::logf);
20162d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::powf);
20262d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::sinf);
20362d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::sinhf);
20462d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::sqrtf);
20562d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::tanf);
20662d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::tanhf);
20762d4215bSChandler Carruth     }
20862d4215bSChandler Carruth 
20962d4215bSChandler Carruth     // Win32 does *not* provide provide these functions, but they are
21062d4215bSChandler Carruth     // generally available on POSIX-compliant systems:
21162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::access);
21262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::bcmp);
21362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::bcopy);
21462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::bzero);
21562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::chmod);
21662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::chown);
21762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::closedir);
21862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ctermid);
21962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fdopen);
22062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ffs);
22162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fileno);
22262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::flockfile);
22362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fseeko);
22462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fstat);
22562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fstatvfs);
22662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ftello);
22762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ftrylockfile);
22862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::funlockfile);
22962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::getc_unlocked);
23062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::getitimer);
23162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::getlogin_r);
23262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::getpwnam);
23362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::gettimeofday);
23462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::htonl);
23562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::htons);
23662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::lchown);
23762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::lstat);
23862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::memccpy);
23962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::mkdir);
24062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ntohl);
24162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ntohs);
24262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::open);
24362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::opendir);
24462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::pclose);
24562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::popen);
24662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::pread);
24762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::pwrite);
24862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::read);
24962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::readlink);
25062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::realpath);
25162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::rmdir);
25262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::setitimer);
25362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::stat);
25462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::statvfs);
25562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::stpcpy);
25662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::stpncpy);
25762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::strcasecmp);
25862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::strncasecmp);
25962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::times);
26062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::uname);
26162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::unlink);
26262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::unsetenv);
26362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::utime);
26462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::utimes);
26562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::write);
26662d4215bSChandler Carruth 
26762d4215bSChandler Carruth     // Win32 does *not* provide provide these functions, but they are
26862d4215bSChandler Carruth     // specified by C99:
26962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::atoll);
27062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::frexpf);
27162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::llabs);
27262d4215bSChandler Carruth   }
27362d4215bSChandler Carruth 
27462d4215bSChandler Carruth   switch (T.getOS()) {
27562d4215bSChandler Carruth   case Triple::MacOSX:
27662d4215bSChandler Carruth     // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
27762d4215bSChandler Carruth     // and their names are __exp10 and __exp10f. exp10l is not available on
27862d4215bSChandler Carruth     // OS X or iOS.
27962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp10l);
28062d4215bSChandler Carruth     if (T.isMacOSXVersionLT(10, 9)) {
28162d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::exp10);
28262d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::exp10f);
28362d4215bSChandler Carruth     } else {
28462d4215bSChandler Carruth       TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
28562d4215bSChandler Carruth       TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
28662d4215bSChandler Carruth     }
28762d4215bSChandler Carruth     break;
28862d4215bSChandler Carruth   case Triple::IOS:
28962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp10l);
29062d4215bSChandler Carruth     if (T.isOSVersionLT(7, 0)) {
29162d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::exp10);
29262d4215bSChandler Carruth       TLI.setUnavailable(LibFunc::exp10f);
29362d4215bSChandler Carruth     } else {
29462d4215bSChandler Carruth       TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
29562d4215bSChandler Carruth       TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
29662d4215bSChandler Carruth     }
29762d4215bSChandler Carruth     break;
29862d4215bSChandler Carruth   case Triple::Linux:
29962d4215bSChandler Carruth     // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
30062d4215bSChandler Carruth     // buggy prior to glibc version 2.18. Until this version is widely deployed
30162d4215bSChandler Carruth     // or we have a reasonable detection strategy, we cannot use exp10 reliably
30262d4215bSChandler Carruth     // on Linux.
30362d4215bSChandler Carruth     //
30462d4215bSChandler Carruth     // Fall through to disable all of them.
30562d4215bSChandler Carruth   default:
30662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp10);
30762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp10f);
30862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::exp10l);
30962d4215bSChandler Carruth   }
31062d4215bSChandler Carruth 
31162d4215bSChandler Carruth   // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
31262d4215bSChandler Carruth   // Linux (GLIBC):
31362d4215bSChandler Carruth   // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
31462d4215bSChandler Carruth   // http://svn.freebsd.org/base/user/eri/pf45/head/lib/libc/string/ffsl.c
31562d4215bSChandler Carruth   // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
31662d4215bSChandler Carruth   switch (T.getOS()) {
31762d4215bSChandler Carruth   case Triple::Darwin:
31862d4215bSChandler Carruth   case Triple::MacOSX:
31962d4215bSChandler Carruth   case Triple::IOS:
32062d4215bSChandler Carruth   case Triple::FreeBSD:
32162d4215bSChandler Carruth   case Triple::Linux:
32262d4215bSChandler Carruth     break;
32362d4215bSChandler Carruth   default:
32462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ffsl);
32562d4215bSChandler Carruth   }
32662d4215bSChandler Carruth 
32762d4215bSChandler Carruth   // ffsll is available on at least FreeBSD and Linux (GLIBC):
32862d4215bSChandler Carruth   // http://svn.freebsd.org/base/user/eri/pf45/head/lib/libc/string/ffsll.c
32962d4215bSChandler Carruth   // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
33062d4215bSChandler Carruth   switch (T.getOS()) {
33162d4215bSChandler Carruth   case Triple::FreeBSD:
33262d4215bSChandler Carruth   case Triple::Linux:
33362d4215bSChandler Carruth     break;
33462d4215bSChandler Carruth   default:
33562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ffsll);
33662d4215bSChandler Carruth   }
33762d4215bSChandler Carruth 
33862d4215bSChandler Carruth   // The following functions are available on at least Linux:
33962d4215bSChandler Carruth   if (!T.isOSLinux()) {
34062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::dunder_strdup);
34162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::dunder_strtok_r);
34262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::dunder_isoc99_scanf);
34362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::dunder_isoc99_sscanf);
34462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::under_IO_getc);
34562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::under_IO_putc);
34662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::memalign);
34762d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fopen64);
34862d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fseeko64);
34962d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fstat64);
35062d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::fstatvfs64);
35162d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::ftello64);
35262d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::lstat64);
35362d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::open64);
35462d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::stat64);
35562d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::statvfs64);
35662d4215bSChandler Carruth     TLI.setUnavailable(LibFunc::tmpfile64);
35762d4215bSChandler Carruth   }
3586d8a2aa9SMichael Zolotukhin 
3596d8a2aa9SMichael Zolotukhin   TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary);
36062d4215bSChandler Carruth }
36162d4215bSChandler Carruth 
362c0291865SChandler Carruth TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
36362d4215bSChandler Carruth   // Default to everything being available.
36462d4215bSChandler Carruth   memset(AvailableArray, -1, sizeof(AvailableArray));
36562d4215bSChandler Carruth 
36662d4215bSChandler Carruth   initialize(*this, Triple(), StandardNames);
36762d4215bSChandler Carruth }
36862d4215bSChandler Carruth 
369c0291865SChandler Carruth TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
37062d4215bSChandler Carruth   // Default to everything being available.
37162d4215bSChandler Carruth   memset(AvailableArray, -1, sizeof(AvailableArray));
37262d4215bSChandler Carruth 
37362d4215bSChandler Carruth   initialize(*this, T, StandardNames);
37462d4215bSChandler Carruth }
37562d4215bSChandler Carruth 
376c0291865SChandler Carruth TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
3778ca43224SChandler Carruth     : CustomNames(TLI.CustomNames) {
37862d4215bSChandler Carruth   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
379e8f2551fSMichael Zolotukhin   VectorDescs = TLI.VectorDescs;
380e8f2551fSMichael Zolotukhin   ScalarDescs = TLI.ScalarDescs;
3818ca43224SChandler Carruth }
3828ca43224SChandler Carruth 
383c0291865SChandler Carruth TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
3848ca43224SChandler Carruth     : CustomNames(std::move(TLI.CustomNames)) {
3858ca43224SChandler Carruth   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
3868ca43224SChandler Carruth             AvailableArray);
387e8f2551fSMichael Zolotukhin   VectorDescs = TLI.VectorDescs;
388e8f2551fSMichael Zolotukhin   ScalarDescs = TLI.ScalarDescs;
3898ca43224SChandler Carruth }
3908ca43224SChandler Carruth 
391c0291865SChandler Carruth TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
39262d4215bSChandler Carruth   CustomNames = TLI.CustomNames;
3938ca43224SChandler Carruth   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
3948ca43224SChandler Carruth   return *this;
3958ca43224SChandler Carruth }
3968ca43224SChandler Carruth 
397c0291865SChandler Carruth TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
3988ca43224SChandler Carruth   CustomNames = std::move(TLI.CustomNames);
3998ca43224SChandler Carruth   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
4008ca43224SChandler Carruth             AvailableArray);
4018ca43224SChandler Carruth   return *this;
40262d4215bSChandler Carruth }
40362d4215bSChandler Carruth 
40421abdf98SMichael Zolotukhin static StringRef sanitizeFunctionName(StringRef funcName) {
40562d4215bSChandler Carruth   // Filter out empty names and names containing null bytes, those can't be in
40662d4215bSChandler Carruth   // our table.
40762d4215bSChandler Carruth   if (funcName.empty() || funcName.find('\0') != StringRef::npos)
40821abdf98SMichael Zolotukhin     return StringRef();
40962d4215bSChandler Carruth 
41062d4215bSChandler Carruth   // Check for \01 prefix that is used to mangle __asm declarations and
41162d4215bSChandler Carruth   // strip it if present.
412cde33036SDavid Majnemer   return GlobalValue::getRealLinkageName(funcName);
41321abdf98SMichael Zolotukhin }
41421abdf98SMichael Zolotukhin 
41521abdf98SMichael Zolotukhin bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName,
41621abdf98SMichael Zolotukhin                                    LibFunc::Func &F) const {
41757a3d084SBenjamin Kramer   const char *const *Start = &StandardNames[0];
41857a3d084SBenjamin Kramer   const char *const *End = &StandardNames[LibFunc::NumLibFuncs];
41921abdf98SMichael Zolotukhin 
42021abdf98SMichael Zolotukhin   funcName = sanitizeFunctionName(funcName);
42121abdf98SMichael Zolotukhin   if (funcName.empty())
42221abdf98SMichael Zolotukhin     return false;
42321abdf98SMichael Zolotukhin 
42457a3d084SBenjamin Kramer   const char *const *I = std::lower_bound(
425d3b76a3bSMichael Zolotukhin       Start, End, funcName, [](const char *LHS, StringRef RHS) {
426d3b76a3bSMichael Zolotukhin         return std::strncmp(LHS, RHS.data(), RHS.size()) < 0;
427d3b76a3bSMichael Zolotukhin       });
42862d4215bSChandler Carruth   if (I != End && *I == funcName) {
42962d4215bSChandler Carruth     F = (LibFunc::Func)(I - Start);
43062d4215bSChandler Carruth     return true;
43162d4215bSChandler Carruth   }
43262d4215bSChandler Carruth   return false;
43362d4215bSChandler Carruth }
43462d4215bSChandler Carruth 
435c0291865SChandler Carruth void TargetLibraryInfoImpl::disableAllFunctions() {
43662d4215bSChandler Carruth   memset(AvailableArray, 0, sizeof(AvailableArray));
43762d4215bSChandler Carruth }
438b98f63dbSChandler Carruth 
439e8f2551fSMichael Zolotukhin static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
440e8f2551fSMichael Zolotukhin   return std::strncmp(LHS.ScalarFnName, RHS.ScalarFnName,
441e8f2551fSMichael Zolotukhin                       std::strlen(RHS.ScalarFnName)) < 0;
442e8f2551fSMichael Zolotukhin }
443e8f2551fSMichael Zolotukhin 
444e8f2551fSMichael Zolotukhin static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
445e8f2551fSMichael Zolotukhin   return std::strncmp(LHS.VectorFnName, RHS.VectorFnName,
446e8f2551fSMichael Zolotukhin                       std::strlen(RHS.VectorFnName)) < 0;
447e8f2551fSMichael Zolotukhin }
448e8f2551fSMichael Zolotukhin 
449e8f2551fSMichael Zolotukhin static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
450e8f2551fSMichael Zolotukhin   return std::strncmp(LHS.ScalarFnName, S.data(), S.size()) < 0;
451e8f2551fSMichael Zolotukhin }
452e8f2551fSMichael Zolotukhin 
453e8f2551fSMichael Zolotukhin static bool compareWithVectorFnName(const VecDesc &LHS, StringRef S) {
454e8f2551fSMichael Zolotukhin   return std::strncmp(LHS.VectorFnName, S.data(), S.size()) < 0;
455e8f2551fSMichael Zolotukhin }
456e8f2551fSMichael Zolotukhin 
457e8f2551fSMichael Zolotukhin void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
458e8f2551fSMichael Zolotukhin   VectorDescs.insert(VectorDescs.end(), Fns.begin(), Fns.end());
459e8f2551fSMichael Zolotukhin   std::sort(VectorDescs.begin(), VectorDescs.end(), compareByScalarFnName);
460e8f2551fSMichael Zolotukhin 
461e8f2551fSMichael Zolotukhin   ScalarDescs.insert(ScalarDescs.end(), Fns.begin(), Fns.end());
462e8f2551fSMichael Zolotukhin   std::sort(ScalarDescs.begin(), ScalarDescs.end(), compareByVectorFnName);
463e8f2551fSMichael Zolotukhin }
464e8f2551fSMichael Zolotukhin 
4656d8a2aa9SMichael Zolotukhin void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
4666d8a2aa9SMichael Zolotukhin     enum VectorLibrary VecLib) {
4676d8a2aa9SMichael Zolotukhin   switch (VecLib) {
4686d8a2aa9SMichael Zolotukhin   case Accelerate: {
4696d8a2aa9SMichael Zolotukhin     const VecDesc VecFuncs[] = {
470*de63aaceSMichael Zolotukhin         // Floating-Point Arithmetic and Auxiliary Functions
471*de63aaceSMichael Zolotukhin         {"ceilf", "vceilf", 4},
4726d8a2aa9SMichael Zolotukhin         {"fabsf", "vfabsf", 4},
4736d8a2aa9SMichael Zolotukhin         {"llvm.fabs.f32", "vfabsf", 4},
474*de63aaceSMichael Zolotukhin         {"floorf", "vfloorf", 4},
475*de63aaceSMichael Zolotukhin         {"sqrtf", "vsqrtf", 4},
476*de63aaceSMichael Zolotukhin         {"llvm.sqrt.f32", "vsqrtf", 4},
477*de63aaceSMichael Zolotukhin 
478*de63aaceSMichael Zolotukhin         // Exponential and Logarithmic Functions
479*de63aaceSMichael Zolotukhin         {"expf", "vexpf", 4},
480*de63aaceSMichael Zolotukhin         {"llvm.exp.f32", "vexpf", 4},
481*de63aaceSMichael Zolotukhin         {"expm1f", "vexpm1f", 4},
482*de63aaceSMichael Zolotukhin         {"logf", "vlogf", 4},
483*de63aaceSMichael Zolotukhin         {"llvm.log.f32", "vlogf", 4},
484*de63aaceSMichael Zolotukhin         {"log1pf", "vlog1pf", 4},
485*de63aaceSMichael Zolotukhin         {"log10f", "vlog10f", 4},
486*de63aaceSMichael Zolotukhin         {"llvm.log10.f32", "vlog10f", 4},
487*de63aaceSMichael Zolotukhin         {"logbf", "vlogbf", 4},
488*de63aaceSMichael Zolotukhin 
489*de63aaceSMichael Zolotukhin         // Trigonometric Functions
490*de63aaceSMichael Zolotukhin         {"sinf", "vsinf", 4},
491*de63aaceSMichael Zolotukhin         {"llvm.sin.f32", "vsinf", 4},
492*de63aaceSMichael Zolotukhin         {"cosf", "vcosf", 4},
493*de63aaceSMichael Zolotukhin         {"llvm.cos.f32", "vcosf", 4},
494*de63aaceSMichael Zolotukhin         {"tanf", "vtanf", 4},
495*de63aaceSMichael Zolotukhin         {"asinf", "vasinf", 4},
496*de63aaceSMichael Zolotukhin         {"acosf", "vacosf", 4},
497*de63aaceSMichael Zolotukhin         {"atanf", "vatanf", 4},
498*de63aaceSMichael Zolotukhin 
499*de63aaceSMichael Zolotukhin         // Hyperbolic Functions
500*de63aaceSMichael Zolotukhin         {"sinhf", "vsinhf", 4},
501*de63aaceSMichael Zolotukhin         {"coshf", "vcoshf", 4},
502*de63aaceSMichael Zolotukhin         {"tanhf", "vtanhf", 4},
503*de63aaceSMichael Zolotukhin         {"asinhf", "vasinhf", 4},
504*de63aaceSMichael Zolotukhin         {"acoshf", "vacoshf", 4},
505*de63aaceSMichael Zolotukhin         {"atanhf", "vatanhf", 4},
5066d8a2aa9SMichael Zolotukhin     };
5076d8a2aa9SMichael Zolotukhin     addVectorizableFunctions(VecFuncs);
5086d8a2aa9SMichael Zolotukhin     break;
5096d8a2aa9SMichael Zolotukhin   }
5106d8a2aa9SMichael Zolotukhin   case NoLibrary:
5116d8a2aa9SMichael Zolotukhin     break;
5126d8a2aa9SMichael Zolotukhin   }
5136d8a2aa9SMichael Zolotukhin }
5146d8a2aa9SMichael Zolotukhin 
515e8f2551fSMichael Zolotukhin bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
516e8f2551fSMichael Zolotukhin   funcName = sanitizeFunctionName(funcName);
517e8f2551fSMichael Zolotukhin   if (funcName.empty())
518e8f2551fSMichael Zolotukhin     return false;
519e8f2551fSMichael Zolotukhin 
520e8f2551fSMichael Zolotukhin   std::vector<VecDesc>::const_iterator I = std::lower_bound(
521e8f2551fSMichael Zolotukhin       VectorDescs.begin(), VectorDescs.end(), funcName,
522e8f2551fSMichael Zolotukhin       compareWithScalarFnName);
523e8f2551fSMichael Zolotukhin   return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
524e8f2551fSMichael Zolotukhin }
525e8f2551fSMichael Zolotukhin 
526e8f2551fSMichael Zolotukhin StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
527e8f2551fSMichael Zolotukhin                                                        unsigned VF) const {
528e8f2551fSMichael Zolotukhin   F = sanitizeFunctionName(F);
529e8f2551fSMichael Zolotukhin   if (F.empty())
530e8f2551fSMichael Zolotukhin     return F;
531e8f2551fSMichael Zolotukhin   std::vector<VecDesc>::const_iterator I = std::lower_bound(
532e8f2551fSMichael Zolotukhin       VectorDescs.begin(), VectorDescs.end(), F, compareWithScalarFnName);
533e8f2551fSMichael Zolotukhin   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
534e8f2551fSMichael Zolotukhin     if (I->VectorizationFactor == VF)
535e8f2551fSMichael Zolotukhin       return I->VectorFnName;
536e8f2551fSMichael Zolotukhin     ++I;
537e8f2551fSMichael Zolotukhin   }
538e8f2551fSMichael Zolotukhin   return StringRef();
539e8f2551fSMichael Zolotukhin }
540e8f2551fSMichael Zolotukhin 
541e8f2551fSMichael Zolotukhin StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F,
542e8f2551fSMichael Zolotukhin                                                        unsigned &VF) const {
543e8f2551fSMichael Zolotukhin   F = sanitizeFunctionName(F);
544e8f2551fSMichael Zolotukhin   if (F.empty())
545e8f2551fSMichael Zolotukhin     return F;
546e8f2551fSMichael Zolotukhin 
547e8f2551fSMichael Zolotukhin   std::vector<VecDesc>::const_iterator I = std::lower_bound(
548e8f2551fSMichael Zolotukhin       ScalarDescs.begin(), ScalarDescs.end(), F, compareWithVectorFnName);
549e8f2551fSMichael Zolotukhin   if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F)
550e8f2551fSMichael Zolotukhin     return StringRef();
551e8f2551fSMichael Zolotukhin   VF = I->VectorizationFactor;
552e8f2551fSMichael Zolotukhin   return I->ScalarFnName;
553e8f2551fSMichael Zolotukhin }
554e8f2551fSMichael Zolotukhin 
555c0291865SChandler Carruth TargetLibraryInfo TargetLibraryAnalysis::run(Module &M) {
556c0291865SChandler Carruth   if (PresetInfoImpl)
557c0291865SChandler Carruth     return TargetLibraryInfo(*PresetInfoImpl);
558c0291865SChandler Carruth 
559c0291865SChandler Carruth   return TargetLibraryInfo(lookupInfoImpl(Triple(M.getTargetTriple())));
560c0291865SChandler Carruth }
561c0291865SChandler Carruth 
562c0291865SChandler Carruth TargetLibraryInfo TargetLibraryAnalysis::run(Function &F) {
563c0291865SChandler Carruth   if (PresetInfoImpl)
564c0291865SChandler Carruth     return TargetLibraryInfo(*PresetInfoImpl);
565c0291865SChandler Carruth 
566c0291865SChandler Carruth   return TargetLibraryInfo(
567c0291865SChandler Carruth       lookupInfoImpl(Triple(F.getParent()->getTargetTriple())));
568c0291865SChandler Carruth }
569c0291865SChandler Carruth 
570c0291865SChandler Carruth TargetLibraryInfoImpl &TargetLibraryAnalysis::lookupInfoImpl(Triple T) {
571c0291865SChandler Carruth   std::unique_ptr<TargetLibraryInfoImpl> &Impl =
572c0291865SChandler Carruth       Impls[T.normalize()];
573c0291865SChandler Carruth   if (!Impl)
574c0291865SChandler Carruth     Impl.reset(new TargetLibraryInfoImpl(T));
575c0291865SChandler Carruth 
576c0291865SChandler Carruth   return *Impl;
577c0291865SChandler Carruth }
578c0291865SChandler Carruth 
579c0291865SChandler Carruth 
580b98f63dbSChandler Carruth TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
581c0291865SChandler Carruth     : ImmutablePass(ID), TLIImpl(), TLI(TLIImpl) {
582b98f63dbSChandler Carruth   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
583b98f63dbSChandler Carruth }
584b98f63dbSChandler Carruth 
585b98f63dbSChandler Carruth TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
586c0291865SChandler Carruth     : ImmutablePass(ID), TLIImpl(T), TLI(TLIImpl) {
587b98f63dbSChandler Carruth   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
588b98f63dbSChandler Carruth }
589b98f63dbSChandler Carruth 
590b98f63dbSChandler Carruth TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
591c0291865SChandler Carruth     const TargetLibraryInfoImpl &TLIImpl)
592c0291865SChandler Carruth     : ImmutablePass(ID), TLIImpl(TLIImpl), TLI(this->TLIImpl) {
593b98f63dbSChandler Carruth   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
594b98f63dbSChandler Carruth }
595b98f63dbSChandler Carruth 
5968ca43224SChandler Carruth char TargetLibraryAnalysis::PassID;
5978ca43224SChandler Carruth 
598b98f63dbSChandler Carruth // Register the basic pass.
599b98f63dbSChandler Carruth INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
600b98f63dbSChandler Carruth                 "Target Library Information", false, true)
601b98f63dbSChandler Carruth char TargetLibraryInfoWrapperPass::ID = 0;
602b98f63dbSChandler Carruth 
603b98f63dbSChandler Carruth void TargetLibraryInfoWrapperPass::anchor() {}
604