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] =
32 "_ZnajRKSt9nothrow_t",
34 "_ZnamRKSt9nothrow_t",
36 "_ZnwjRKSt9nothrow_t",
38 "_ZnwmRKSt9nothrow_t",
41 "__cxa_guard_acquire",
42 "__cxa_guard_release",
332 /// initialize - Initialize the set of available library functions based on the
333 /// specified target triple. This should be carefully written so that a missing
334 /// target triple gets a sane set of defaults.
335 static void initialize(TargetLibraryInfo &TLI, const Triple &T,
336 const char **StandardNames) {
337 initializeTargetLibraryInfoPass(*PassRegistry::getPassRegistry());
340 // Verify that the StandardNames array is in alphabetical order.
341 for (unsigned F = 1; F < LibFunc::NumLibFuncs; ++F) {
342 if (strcmp(StandardNames[F-1], StandardNames[F]) >= 0)
343 llvm_unreachable("TargetLibraryInfo function names must be sorted");
347 // memset_pattern16 is only available on iOS 3.0 and Mac OS/X 10.5 and later.
349 if (T.isMacOSXVersionLT(10, 5))
350 TLI.setUnavailable(LibFunc::memset_pattern16);
351 } else if (T.getOS() == Triple::IOS) {
352 if (T.isOSVersionLT(3, 0))
353 TLI.setUnavailable(LibFunc::memset_pattern16);
355 TLI.setUnavailable(LibFunc::memset_pattern16);
358 if (T.isMacOSX() && T.getArch() == Triple::x86 &&
359 !T.isMacOSXVersionLT(10, 7)) {
360 // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
361 // we don't care about) have two versions; on recent OSX, the one we want
362 // has a $UNIX2003 suffix. The two implementations are identical except
363 // for the return value in some edge cases. However, we don't want to
364 // generate code that depends on the old symbols.
365 TLI.setAvailableWithName(LibFunc::fwrite, "fwrite$UNIX2003");
366 TLI.setAvailableWithName(LibFunc::fputs, "fputs$UNIX2003");
369 // iprintf and friends are only available on XCore and TCE.
370 if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) {
371 TLI.setUnavailable(LibFunc::iprintf);
372 TLI.setUnavailable(LibFunc::siprintf);
373 TLI.setUnavailable(LibFunc::fiprintf);
376 if (T.getOS() == Triple::Win32) {
377 // Win32 does not support long double
378 TLI.setUnavailable(LibFunc::acosl);
379 TLI.setUnavailable(LibFunc::asinl);
380 TLI.setUnavailable(LibFunc::atanl);
381 TLI.setUnavailable(LibFunc::atan2l);
382 TLI.setUnavailable(LibFunc::ceill);
383 TLI.setUnavailable(LibFunc::copysignl);
384 TLI.setUnavailable(LibFunc::cosl);
385 TLI.setUnavailable(LibFunc::coshl);
386 TLI.setUnavailable(LibFunc::expl);
387 TLI.setUnavailable(LibFunc::fabsf); // Win32 and Win64 both lack fabsf
388 TLI.setUnavailable(LibFunc::fabsl);
389 TLI.setUnavailable(LibFunc::floorl);
390 TLI.setUnavailable(LibFunc::fmodl);
391 TLI.setUnavailable(LibFunc::frexpl);
392 TLI.setUnavailable(LibFunc::logl);
393 TLI.setUnavailable(LibFunc::modfl);
394 TLI.setUnavailable(LibFunc::powl);
395 TLI.setUnavailable(LibFunc::sinl);
396 TLI.setUnavailable(LibFunc::sinhl);
397 TLI.setUnavailable(LibFunc::sqrtl);
398 TLI.setUnavailable(LibFunc::tanl);
399 TLI.setUnavailable(LibFunc::tanhl);
401 // Win32 only has C89 math
402 TLI.setUnavailable(LibFunc::acosh);
403 TLI.setUnavailable(LibFunc::acoshf);
404 TLI.setUnavailable(LibFunc::acoshl);
405 TLI.setUnavailable(LibFunc::asinh);
406 TLI.setUnavailable(LibFunc::asinhf);
407 TLI.setUnavailable(LibFunc::asinhl);
408 TLI.setUnavailable(LibFunc::atanh);
409 TLI.setUnavailable(LibFunc::atanhf);
410 TLI.setUnavailable(LibFunc::atanhl);
411 TLI.setUnavailable(LibFunc::cbrt);
412 TLI.setUnavailable(LibFunc::cbrtf);
413 TLI.setUnavailable(LibFunc::cbrtl);
414 TLI.setUnavailable(LibFunc::exp10);
415 TLI.setUnavailable(LibFunc::exp10f);
416 TLI.setUnavailable(LibFunc::exp10l);
417 TLI.setUnavailable(LibFunc::exp2);
418 TLI.setUnavailable(LibFunc::exp2f);
419 TLI.setUnavailable(LibFunc::exp2l);
420 TLI.setUnavailable(LibFunc::expm1);
421 TLI.setUnavailable(LibFunc::expm1f);
422 TLI.setUnavailable(LibFunc::expm1l);
423 TLI.setUnavailable(LibFunc::log2);
424 TLI.setUnavailable(LibFunc::log2f);
425 TLI.setUnavailable(LibFunc::log2l);
426 TLI.setUnavailable(LibFunc::log1p);
427 TLI.setUnavailable(LibFunc::log1pf);
428 TLI.setUnavailable(LibFunc::log1pl);
429 TLI.setUnavailable(LibFunc::logb);
430 TLI.setUnavailable(LibFunc::logbf);
431 TLI.setUnavailable(LibFunc::logbl);
432 TLI.setUnavailable(LibFunc::nearbyint);
433 TLI.setUnavailable(LibFunc::nearbyintf);
434 TLI.setUnavailable(LibFunc::nearbyintl);
435 TLI.setUnavailable(LibFunc::rint);
436 TLI.setUnavailable(LibFunc::rintf);
437 TLI.setUnavailable(LibFunc::rintl);
438 TLI.setUnavailable(LibFunc::round);
439 TLI.setUnavailable(LibFunc::roundf);
440 TLI.setUnavailable(LibFunc::roundl);
441 TLI.setUnavailable(LibFunc::trunc);
442 TLI.setUnavailable(LibFunc::truncf);
443 TLI.setUnavailable(LibFunc::truncl);
445 // Win32 provides some C99 math with mangled names
446 TLI.setAvailableWithName(LibFunc::copysign, "_copysign");
448 if (T.getArch() == Triple::x86) {
449 // Win32 on x86 implements single-precision math functions as macros
450 TLI.setUnavailable(LibFunc::acosf);
451 TLI.setUnavailable(LibFunc::asinf);
452 TLI.setUnavailable(LibFunc::atanf);
453 TLI.setUnavailable(LibFunc::atan2f);
454 TLI.setUnavailable(LibFunc::ceilf);
455 TLI.setUnavailable(LibFunc::copysignf);
456 TLI.setUnavailable(LibFunc::cosf);
457 TLI.setUnavailable(LibFunc::coshf);
458 TLI.setUnavailable(LibFunc::expf);
459 TLI.setUnavailable(LibFunc::floorf);
460 TLI.setUnavailable(LibFunc::fmodf);
461 TLI.setUnavailable(LibFunc::logf);
462 TLI.setUnavailable(LibFunc::powf);
463 TLI.setUnavailable(LibFunc::sinf);
464 TLI.setUnavailable(LibFunc::sinhf);
465 TLI.setUnavailable(LibFunc::sqrtf);
466 TLI.setUnavailable(LibFunc::tanf);
467 TLI.setUnavailable(LibFunc::tanhf);
470 // Win32 does *not* provide provide these functions, but they are
471 // generally available on POSIX-compliant systems:
472 TLI.setUnavailable(LibFunc::access);
473 TLI.setUnavailable(LibFunc::bcmp);
474 TLI.setUnavailable(LibFunc::bcopy);
475 TLI.setUnavailable(LibFunc::bzero);
476 TLI.setUnavailable(LibFunc::chmod);
477 TLI.setUnavailable(LibFunc::chown);
478 TLI.setUnavailable(LibFunc::closedir);
479 TLI.setUnavailable(LibFunc::ctermid);
480 TLI.setUnavailable(LibFunc::fdopen);
481 TLI.setUnavailable(LibFunc::ffs);
482 TLI.setUnavailable(LibFunc::fileno);
483 TLI.setUnavailable(LibFunc::flockfile);
484 TLI.setUnavailable(LibFunc::fseeko);
485 TLI.setUnavailable(LibFunc::fstat);
486 TLI.setUnavailable(LibFunc::fstatvfs);
487 TLI.setUnavailable(LibFunc::ftello);
488 TLI.setUnavailable(LibFunc::ftrylockfile);
489 TLI.setUnavailable(LibFunc::funlockfile);
490 TLI.setUnavailable(LibFunc::getc_unlocked);
491 TLI.setUnavailable(LibFunc::getitimer);
492 TLI.setUnavailable(LibFunc::getlogin_r);
493 TLI.setUnavailable(LibFunc::getpwnam);
494 TLI.setUnavailable(LibFunc::gettimeofday);
495 TLI.setUnavailable(LibFunc::htonl);
496 TLI.setUnavailable(LibFunc::htons);
497 TLI.setUnavailable(LibFunc::lchown);
498 TLI.setUnavailable(LibFunc::lstat);
499 TLI.setUnavailable(LibFunc::memccpy);
500 TLI.setUnavailable(LibFunc::mkdir);
501 TLI.setUnavailable(LibFunc::ntohl);
502 TLI.setUnavailable(LibFunc::ntohs);
503 TLI.setUnavailable(LibFunc::open);
504 TLI.setUnavailable(LibFunc::opendir);
505 TLI.setUnavailable(LibFunc::pclose);
506 TLI.setUnavailable(LibFunc::popen);
507 TLI.setUnavailable(LibFunc::pread);
508 TLI.setUnavailable(LibFunc::pwrite);
509 TLI.setUnavailable(LibFunc::read);
510 TLI.setUnavailable(LibFunc::readlink);
511 TLI.setUnavailable(LibFunc::realpath);
512 TLI.setUnavailable(LibFunc::rmdir);
513 TLI.setUnavailable(LibFunc::setitimer);
514 TLI.setUnavailable(LibFunc::stat);
515 TLI.setUnavailable(LibFunc::statvfs);
516 TLI.setUnavailable(LibFunc::stpcpy);
517 TLI.setUnavailable(LibFunc::stpncpy);
518 TLI.setUnavailable(LibFunc::strcasecmp);
519 TLI.setUnavailable(LibFunc::strncasecmp);
520 TLI.setUnavailable(LibFunc::times);
521 TLI.setUnavailable(LibFunc::uname);
522 TLI.setUnavailable(LibFunc::unlink);
523 TLI.setUnavailable(LibFunc::unsetenv);
524 TLI.setUnavailable(LibFunc::utime);
525 TLI.setUnavailable(LibFunc::utimes);
526 TLI.setUnavailable(LibFunc::write);
528 // Win32 does *not* provide provide these functions, but they are
530 TLI.setUnavailable(LibFunc::atoll);
531 TLI.setUnavailable(LibFunc::frexpf);
532 TLI.setUnavailable(LibFunc::llabs);
535 // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
537 // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
538 // http://svn.freebsd.org/base/user/eri/pf45/head/lib/libc/string/ffsl.c
539 // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
544 case Triple::FreeBSD:
548 TLI.setUnavailable(LibFunc::ffsl);
551 // ffsll is available on at least FreeBSD and Linux (GLIBC):
552 // http://svn.freebsd.org/base/user/eri/pf45/head/lib/libc/string/ffsll.c
553 // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
555 case Triple::FreeBSD:
559 TLI.setUnavailable(LibFunc::ffsll);
562 // The following functions are available on at least Linux:
563 if (T.getOS() != Triple::Linux) {
564 TLI.setUnavailable(LibFunc::dunder_strdup);
565 TLI.setUnavailable(LibFunc::dunder_strtok_r);
566 TLI.setUnavailable(LibFunc::dunder_isoc99_scanf);
567 TLI.setUnavailable(LibFunc::dunder_isoc99_sscanf);
568 TLI.setUnavailable(LibFunc::under_IO_getc);
569 TLI.setUnavailable(LibFunc::under_IO_putc);
570 TLI.setUnavailable(LibFunc::memalign);
571 TLI.setUnavailable(LibFunc::fopen64);
572 TLI.setUnavailable(LibFunc::fseeko64);
573 TLI.setUnavailable(LibFunc::fstat64);
574 TLI.setUnavailable(LibFunc::fstatvfs64);
575 TLI.setUnavailable(LibFunc::ftello64);
576 TLI.setUnavailable(LibFunc::lstat64);
577 TLI.setUnavailable(LibFunc::open64);
578 TLI.setUnavailable(LibFunc::stat64);
579 TLI.setUnavailable(LibFunc::statvfs64);
580 TLI.setUnavailable(LibFunc::tmpfile64);
585 TargetLibraryInfo::TargetLibraryInfo() : ImmutablePass(ID) {
586 // Default to everything being available.
587 memset(AvailableArray, -1, sizeof(AvailableArray));
589 initialize(*this, Triple(), StandardNames);
592 TargetLibraryInfo::TargetLibraryInfo(const Triple &T) : ImmutablePass(ID) {
593 // Default to everything being available.
594 memset(AvailableArray, -1, sizeof(AvailableArray));
596 initialize(*this, T, StandardNames);
599 TargetLibraryInfo::TargetLibraryInfo(const TargetLibraryInfo &TLI)
600 : ImmutablePass(ID) {
601 memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
602 CustomNames = TLI.CustomNames;
606 struct StringComparator {
607 /// Compare two strings and return true if LHS is lexicographically less than
608 /// RHS. Requires that RHS doesn't contain any zero bytes.
609 bool operator()(const char *LHS, StringRef RHS) const {
610 // Compare prefixes with strncmp. If prefixes match we know that LHS is
611 // greater or equal to RHS as RHS can't contain any '\0'.
612 return std::strncmp(LHS, RHS.data(), RHS.size()) < 0;
615 // Provided for compatibility with MSVC's debug mode.
616 bool operator()(StringRef LHS, const char *RHS) const { return LHS < RHS; }
617 bool operator()(StringRef LHS, StringRef RHS) const { return LHS < RHS; }
618 bool operator()(const char *LHS, const char *RHS) const {
619 return std::strcmp(LHS, RHS) < 0;
624 bool TargetLibraryInfo::getLibFunc(StringRef funcName,
625 LibFunc::Func &F) const {
626 const char **Start = &StandardNames[0];
627 const char **End = &StandardNames[LibFunc::NumLibFuncs];
629 // Filter out empty names and names containing null bytes, those can't be in
631 if (funcName.empty() || funcName.find('\0') != StringRef::npos)
634 // Check for \01 prefix that is used to mangle __asm declarations and
635 // strip it if present.
636 if (funcName.front() == '\01')
637 funcName = funcName.substr(1);
638 const char **I = std::lower_bound(Start, End, funcName, StringComparator());
639 if (I != End && *I == funcName) {
640 F = (LibFunc::Func)(I - Start);
646 /// disableAllFunctions - This disables all builtins, which is used for options
647 /// like -fno-builtin.
648 void TargetLibraryInfo::disableAllFunctions() {
649 memset(AvailableArray, 0, sizeof(AvailableArray));