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