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