1 //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the TargetLibraryInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/TargetLibraryInfo.h"
15 #include "llvm/ADT/Triple.h"
18 // Register the default implementation.
19 INITIALIZE_PASS(TargetLibraryInfo, "targetlibinfo",
20 "Target Library Information", false, true)
21 char TargetLibraryInfo::ID = 0;
23 void TargetLibraryInfo::anchor() { }
25 const char* TargetLibraryInfo::StandardNames[LibFunc::NumLibFuncs] =
30 "_ZdaPvRKSt9nothrow_t",
32 "_ZdlPvRKSt9nothrow_t",
34 "_ZnajRKSt9nothrow_t",
36 "_ZnamRKSt9nothrow_t",
38 "_ZnwjRKSt9nothrow_t",
40 "_ZnwmRKSt9nothrow_t",
45 "__cxa_guard_acquire",
46 "__cxa_guard_release",
349 static bool hasSinCosPiStret(const Triple &T) {
350 // Only Darwin variants have _stret versions of combined trig functions.
351 if (!T.isMacOSX() && T.getOS() != Triple::IOS)
354 // The ABI is rather complicated on x86, so don't do anything special there.
355 if (T.getArch() == Triple::x86)
358 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
361 if (T.getOS() == Triple::IOS && T.isOSVersionLT(7, 0))
367 /// initialize - Initialize the set of available library functions based on the
368 /// specified target triple. This should be carefully written so that a missing
369 /// target triple gets a sane set of defaults.
370 static void initialize(TargetLibraryInfo &TLI, const Triple &T,
371 const char **StandardNames) {
372 initializeTargetLibraryInfoPass(*PassRegistry::getPassRegistry());
375 // Verify that the StandardNames array is in alphabetical order.
376 for (unsigned F = 1; F < LibFunc::NumLibFuncs; ++F) {
377 if (strcmp(StandardNames[F-1], StandardNames[F]) >= 0)
378 llvm_unreachable("TargetLibraryInfo function names must be sorted");
382 // There are no library implementations of mempcy and memset for r600 and
383 // these can be difficult to lower in the backend.
384 if (T.getArch() == Triple::r600) {
385 TLI.setUnavailable(LibFunc::memcpy);
386 TLI.setUnavailable(LibFunc::memset);
387 TLI.setUnavailable(LibFunc::memset_pattern16);
391 // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
393 if (T.isMacOSXVersionLT(10, 5))
394 TLI.setUnavailable(LibFunc::memset_pattern16);
395 } else if (T.isiOS()) {
396 if (T.isOSVersionLT(3, 0))
397 TLI.setUnavailable(LibFunc::memset_pattern16);
399 TLI.setUnavailable(LibFunc::memset_pattern16);
402 if (!hasSinCosPiStret(T)) {
403 TLI.setUnavailable(LibFunc::sinpi);
404 TLI.setUnavailable(LibFunc::sinpif);
405 TLI.setUnavailable(LibFunc::cospi);
406 TLI.setUnavailable(LibFunc::cospif);
407 TLI.setUnavailable(LibFunc::sincospi_stret);
408 TLI.setUnavailable(LibFunc::sincospif_stret);
411 if (T.isMacOSX() && T.getArch() == Triple::x86 &&
412 !T.isMacOSXVersionLT(10, 7)) {
413 // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
414 // we don't care about) have two versions; on recent OSX, the one we want
415 // has a $UNIX2003 suffix. The two implementations are identical except
416 // for the return value in some edge cases. However, we don't want to
417 // generate code that depends on the old symbols.
418 TLI.setAvailableWithName(LibFunc::fwrite, "fwrite$UNIX2003");
419 TLI.setAvailableWithName(LibFunc::fputs, "fputs$UNIX2003");
422 // iprintf and friends are only available on XCore and TCE.
423 if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) {
424 TLI.setUnavailable(LibFunc::iprintf);
425 TLI.setUnavailable(LibFunc::siprintf);
426 TLI.setUnavailable(LibFunc::fiprintf);
429 if (T.isKnownWindowsMSVCEnvironment()) {
430 // Win32 does not support long double
431 TLI.setUnavailable(LibFunc::acosl);
432 TLI.setUnavailable(LibFunc::asinl);
433 TLI.setUnavailable(LibFunc::atanl);
434 TLI.setUnavailable(LibFunc::atan2l);
435 TLI.setUnavailable(LibFunc::ceill);
436 TLI.setUnavailable(LibFunc::copysignl);
437 TLI.setUnavailable(LibFunc::cosl);
438 TLI.setUnavailable(LibFunc::coshl);
439 TLI.setUnavailable(LibFunc::expl);
440 TLI.setUnavailable(LibFunc::fabsf); // Win32 and Win64 both lack fabsf
441 TLI.setUnavailable(LibFunc::fabsl);
442 TLI.setUnavailable(LibFunc::floorl);
443 TLI.setUnavailable(LibFunc::fmaxl);
444 TLI.setUnavailable(LibFunc::fminl);
445 TLI.setUnavailable(LibFunc::fmodl);
446 TLI.setUnavailable(LibFunc::frexpl);
447 TLI.setUnavailable(LibFunc::ldexpf);
448 TLI.setUnavailable(LibFunc::ldexpl);
449 TLI.setUnavailable(LibFunc::logl);
450 TLI.setUnavailable(LibFunc::modfl);
451 TLI.setUnavailable(LibFunc::powl);
452 TLI.setUnavailable(LibFunc::sinl);
453 TLI.setUnavailable(LibFunc::sinhl);
454 TLI.setUnavailable(LibFunc::sqrtl);
455 TLI.setUnavailable(LibFunc::tanl);
456 TLI.setUnavailable(LibFunc::tanhl);
458 // Win32 only has C89 math
459 TLI.setUnavailable(LibFunc::acosh);
460 TLI.setUnavailable(LibFunc::acoshf);
461 TLI.setUnavailable(LibFunc::acoshl);
462 TLI.setUnavailable(LibFunc::asinh);
463 TLI.setUnavailable(LibFunc::asinhf);
464 TLI.setUnavailable(LibFunc::asinhl);
465 TLI.setUnavailable(LibFunc::atanh);
466 TLI.setUnavailable(LibFunc::atanhf);
467 TLI.setUnavailable(LibFunc::atanhl);
468 TLI.setUnavailable(LibFunc::cbrt);
469 TLI.setUnavailable(LibFunc::cbrtf);
470 TLI.setUnavailable(LibFunc::cbrtl);
471 TLI.setUnavailable(LibFunc::exp2);
472 TLI.setUnavailable(LibFunc::exp2f);
473 TLI.setUnavailable(LibFunc::exp2l);
474 TLI.setUnavailable(LibFunc::expm1);
475 TLI.setUnavailable(LibFunc::expm1f);
476 TLI.setUnavailable(LibFunc::expm1l);
477 TLI.setUnavailable(LibFunc::log2);
478 TLI.setUnavailable(LibFunc::log2f);
479 TLI.setUnavailable(LibFunc::log2l);
480 TLI.setUnavailable(LibFunc::log1p);
481 TLI.setUnavailable(LibFunc::log1pf);
482 TLI.setUnavailable(LibFunc::log1pl);
483 TLI.setUnavailable(LibFunc::logb);
484 TLI.setUnavailable(LibFunc::logbf);
485 TLI.setUnavailable(LibFunc::logbl);
486 TLI.setUnavailable(LibFunc::nearbyint);
487 TLI.setUnavailable(LibFunc::nearbyintf);
488 TLI.setUnavailable(LibFunc::nearbyintl);
489 TLI.setUnavailable(LibFunc::rint);
490 TLI.setUnavailable(LibFunc::rintf);
491 TLI.setUnavailable(LibFunc::rintl);
492 TLI.setUnavailable(LibFunc::round);
493 TLI.setUnavailable(LibFunc::roundf);
494 TLI.setUnavailable(LibFunc::roundl);
495 TLI.setUnavailable(LibFunc::trunc);
496 TLI.setUnavailable(LibFunc::truncf);
497 TLI.setUnavailable(LibFunc::truncl);
499 // Win32 provides some C99 math with mangled names
500 TLI.setAvailableWithName(LibFunc::copysign, "_copysign");
502 if (T.getArch() == Triple::x86) {
503 // Win32 on x86 implements single-precision math functions as macros
504 TLI.setUnavailable(LibFunc::acosf);
505 TLI.setUnavailable(LibFunc::asinf);
506 TLI.setUnavailable(LibFunc::atanf);
507 TLI.setUnavailable(LibFunc::atan2f);
508 TLI.setUnavailable(LibFunc::ceilf);
509 TLI.setUnavailable(LibFunc::copysignf);
510 TLI.setUnavailable(LibFunc::cosf);
511 TLI.setUnavailable(LibFunc::coshf);
512 TLI.setUnavailable(LibFunc::expf);
513 TLI.setUnavailable(LibFunc::floorf);
514 TLI.setUnavailable(LibFunc::fminf);
515 TLI.setUnavailable(LibFunc::fmaxf);
516 TLI.setUnavailable(LibFunc::fmodf);
517 TLI.setUnavailable(LibFunc::logf);
518 TLI.setUnavailable(LibFunc::powf);
519 TLI.setUnavailable(LibFunc::sinf);
520 TLI.setUnavailable(LibFunc::sinhf);
521 TLI.setUnavailable(LibFunc::sqrtf);
522 TLI.setUnavailable(LibFunc::tanf);
523 TLI.setUnavailable(LibFunc::tanhf);
526 // Win32 does *not* provide provide these functions, but they are
527 // generally available on POSIX-compliant systems:
528 TLI.setUnavailable(LibFunc::access);
529 TLI.setUnavailable(LibFunc::bcmp);
530 TLI.setUnavailable(LibFunc::bcopy);
531 TLI.setUnavailable(LibFunc::bzero);
532 TLI.setUnavailable(LibFunc::chmod);
533 TLI.setUnavailable(LibFunc::chown);
534 TLI.setUnavailable(LibFunc::closedir);
535 TLI.setUnavailable(LibFunc::ctermid);
536 TLI.setUnavailable(LibFunc::fdopen);
537 TLI.setUnavailable(LibFunc::ffs);
538 TLI.setUnavailable(LibFunc::fileno);
539 TLI.setUnavailable(LibFunc::flockfile);
540 TLI.setUnavailable(LibFunc::fseeko);
541 TLI.setUnavailable(LibFunc::fstat);
542 TLI.setUnavailable(LibFunc::fstatvfs);
543 TLI.setUnavailable(LibFunc::ftello);
544 TLI.setUnavailable(LibFunc::ftrylockfile);
545 TLI.setUnavailable(LibFunc::funlockfile);
546 TLI.setUnavailable(LibFunc::getc_unlocked);
547 TLI.setUnavailable(LibFunc::getitimer);
548 TLI.setUnavailable(LibFunc::getlogin_r);
549 TLI.setUnavailable(LibFunc::getpwnam);
550 TLI.setUnavailable(LibFunc::gettimeofday);
551 TLI.setUnavailable(LibFunc::htonl);
552 TLI.setUnavailable(LibFunc::htons);
553 TLI.setUnavailable(LibFunc::lchown);
554 TLI.setUnavailable(LibFunc::lstat);
555 TLI.setUnavailable(LibFunc::memccpy);
556 TLI.setUnavailable(LibFunc::mkdir);
557 TLI.setUnavailable(LibFunc::ntohl);
558 TLI.setUnavailable(LibFunc::ntohs);
559 TLI.setUnavailable(LibFunc::open);
560 TLI.setUnavailable(LibFunc::opendir);
561 TLI.setUnavailable(LibFunc::pclose);
562 TLI.setUnavailable(LibFunc::popen);
563 TLI.setUnavailable(LibFunc::pread);
564 TLI.setUnavailable(LibFunc::pwrite);
565 TLI.setUnavailable(LibFunc::read);
566 TLI.setUnavailable(LibFunc::readlink);
567 TLI.setUnavailable(LibFunc::realpath);
568 TLI.setUnavailable(LibFunc::rmdir);
569 TLI.setUnavailable(LibFunc::setitimer);
570 TLI.setUnavailable(LibFunc::stat);
571 TLI.setUnavailable(LibFunc::statvfs);
572 TLI.setUnavailable(LibFunc::stpcpy);
573 TLI.setUnavailable(LibFunc::stpncpy);
574 TLI.setUnavailable(LibFunc::strcasecmp);
575 TLI.setUnavailable(LibFunc::strncasecmp);
576 TLI.setUnavailable(LibFunc::times);
577 TLI.setUnavailable(LibFunc::uname);
578 TLI.setUnavailable(LibFunc::unlink);
579 TLI.setUnavailable(LibFunc::unsetenv);
580 TLI.setUnavailable(LibFunc::utime);
581 TLI.setUnavailable(LibFunc::utimes);
582 TLI.setUnavailable(LibFunc::write);
584 // Win32 does *not* provide provide these functions, but they are
586 TLI.setUnavailable(LibFunc::atoll);
587 TLI.setUnavailable(LibFunc::frexpf);
588 TLI.setUnavailable(LibFunc::llabs);
593 // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
594 // and their names are __exp10 and __exp10f. exp10l is not available on
596 TLI.setUnavailable(LibFunc::exp10l);
597 if (T.isMacOSXVersionLT(10, 9)) {
598 TLI.setUnavailable(LibFunc::exp10);
599 TLI.setUnavailable(LibFunc::exp10f);
601 TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
602 TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
606 TLI.setUnavailable(LibFunc::exp10l);
607 if (T.isOSVersionLT(7, 0)) {
608 TLI.setUnavailable(LibFunc::exp10);
609 TLI.setUnavailable(LibFunc::exp10f);
611 TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
612 TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
616 // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
617 // buggy prior to glibc version 2.18. Until this version is widely deployed
618 // or we have a reasonable detection strategy, we cannot use exp10 reliably
621 // Fall through to disable all of them.
623 TLI.setUnavailable(LibFunc::exp10);
624 TLI.setUnavailable(LibFunc::exp10f);
625 TLI.setUnavailable(LibFunc::exp10l);
628 // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
630 // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
631 // http://svn.freebsd.org/base/user/eri/pf45/head/lib/libc/string/ffsl.c
632 // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
637 case Triple::FreeBSD:
641 TLI.setUnavailable(LibFunc::ffsl);
644 // ffsll is available on at least FreeBSD and Linux (GLIBC):
645 // http://svn.freebsd.org/base/user/eri/pf45/head/lib/libc/string/ffsll.c
646 // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
648 case Triple::FreeBSD:
652 TLI.setUnavailable(LibFunc::ffsll);
655 // The following functions are available on at least Linux:
656 if (!T.isOSLinux()) {
657 TLI.setUnavailable(LibFunc::dunder_strdup);
658 TLI.setUnavailable(LibFunc::dunder_strtok_r);
659 TLI.setUnavailable(LibFunc::dunder_isoc99_scanf);
660 TLI.setUnavailable(LibFunc::dunder_isoc99_sscanf);
661 TLI.setUnavailable(LibFunc::under_IO_getc);
662 TLI.setUnavailable(LibFunc::under_IO_putc);
663 TLI.setUnavailable(LibFunc::memalign);
664 TLI.setUnavailable(LibFunc::fopen64);
665 TLI.setUnavailable(LibFunc::fseeko64);
666 TLI.setUnavailable(LibFunc::fstat64);
667 TLI.setUnavailable(LibFunc::fstatvfs64);
668 TLI.setUnavailable(LibFunc::ftello64);
669 TLI.setUnavailable(LibFunc::lstat64);
670 TLI.setUnavailable(LibFunc::open64);
671 TLI.setUnavailable(LibFunc::stat64);
672 TLI.setUnavailable(LibFunc::statvfs64);
673 TLI.setUnavailable(LibFunc::tmpfile64);
678 TargetLibraryInfo::TargetLibraryInfo() : ImmutablePass(ID) {
679 // Default to everything being available.
680 memset(AvailableArray, -1, sizeof(AvailableArray));
682 initialize(*this, Triple(), StandardNames);
685 TargetLibraryInfo::TargetLibraryInfo(const Triple &T) : ImmutablePass(ID) {
686 // Default to everything being available.
687 memset(AvailableArray, -1, sizeof(AvailableArray));
689 initialize(*this, T, StandardNames);
692 TargetLibraryInfo::TargetLibraryInfo(const TargetLibraryInfo &TLI)
693 : ImmutablePass(ID) {
694 memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
695 CustomNames = TLI.CustomNames;
699 struct StringComparator {
700 /// Compare two strings and return true if LHS is lexicographically less than
701 /// RHS. Requires that RHS doesn't contain any zero bytes.
702 bool operator()(const char *LHS, StringRef RHS) const {
703 // Compare prefixes with strncmp. If prefixes match we know that LHS is
704 // greater or equal to RHS as RHS can't contain any '\0'.
705 return std::strncmp(LHS, RHS.data(), RHS.size()) < 0;
708 // Provided for compatibility with MSVC's debug mode.
709 bool operator()(StringRef LHS, const char *RHS) const { return LHS < RHS; }
710 bool operator()(StringRef LHS, StringRef RHS) const { return LHS < RHS; }
711 bool operator()(const char *LHS, const char *RHS) const {
712 return std::strcmp(LHS, RHS) < 0;
717 bool TargetLibraryInfo::getLibFunc(StringRef funcName,
718 LibFunc::Func &F) const {
719 const char **Start = &StandardNames[0];
720 const char **End = &StandardNames[LibFunc::NumLibFuncs];
722 // Filter out empty names and names containing null bytes, those can't be in
724 if (funcName.empty() || funcName.find('\0') != StringRef::npos)
727 // Check for \01 prefix that is used to mangle __asm declarations and
728 // strip it if present.
729 if (funcName.front() == '\01')
730 funcName = funcName.substr(1);
731 const char **I = std::lower_bound(Start, End, funcName, StringComparator());
732 if (I != End && *I == funcName) {
733 F = (LibFunc::Func)(I - Start);
739 /// disableAllFunctions - This disables all builtins, which is used for options
740 /// like -fno-builtin.
741 void TargetLibraryInfo::disableAllFunctions() {
742 memset(AvailableArray, 0, sizeof(AvailableArray));