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