1 //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the TargetLibraryInfo class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Target/TargetLibraryInfo.h" 15 #include "llvm/ADT/Triple.h" 16 using namespace llvm; 17 18 // Register the default implementation. 19 INITIALIZE_PASS(TargetLibraryInfo, "targetlibinfo", 20 "Target Library Information", false, true) 21 char TargetLibraryInfo::ID = 0; 22 23 void TargetLibraryInfo::anchor() { } 24 25 const char* TargetLibraryInfo::StandardNames[LibFunc::NumLibFuncs] = 26 { 27 "_IO_getc", 28 "_IO_putc", 29 "_ZdaPv", 30 "_ZdaPvRKSt9nothrow_t", 31 "_ZdlPv", 32 "_ZdlPvRKSt9nothrow_t", 33 "_Znaj", 34 "_ZnajRKSt9nothrow_t", 35 "_Znam", 36 "_ZnamRKSt9nothrow_t", 37 "_Znwj", 38 "_ZnwjRKSt9nothrow_t", 39 "_Znwm", 40 "_ZnwmRKSt9nothrow_t", 41 "__cxa_atexit", 42 "__cxa_guard_abort", 43 "__cxa_guard_acquire", 44 "__cxa_guard_release", 45 "__isoc99_scanf", 46 "__isoc99_sscanf", 47 "__memcpy_chk", 48 "__sqrt_finite", 49 "__sqrtf_finite", 50 "__sqrtl_finite", 51 "__strdup", 52 "__strndup", 53 "__strtok_r", 54 "abs", 55 "access", 56 "acos", 57 "acosf", 58 "acosh", 59 "acoshf", 60 "acoshl", 61 "acosl", 62 "asin", 63 "asinf", 64 "asinh", 65 "asinhf", 66 "asinhl", 67 "asinl", 68 "atan", 69 "atan2", 70 "atan2f", 71 "atan2l", 72 "atanf", 73 "atanh", 74 "atanhf", 75 "atanhl", 76 "atanl", 77 "atof", 78 "atoi", 79 "atol", 80 "atoll", 81 "bcmp", 82 "bcopy", 83 "bzero", 84 "calloc", 85 "cbrt", 86 "cbrtf", 87 "cbrtl", 88 "ceil", 89 "ceilf", 90 "ceill", 91 "chmod", 92 "chown", 93 "clearerr", 94 "closedir", 95 "copysign", 96 "copysignf", 97 "copysignl", 98 "cos", 99 "cosf", 100 "cosh", 101 "coshf", 102 "coshl", 103 "cosl", 104 "ctermid", 105 "exp", 106 "exp10", 107 "exp10f", 108 "exp10l", 109 "exp2", 110 "exp2f", 111 "exp2l", 112 "expf", 113 "expl", 114 "expm1", 115 "expm1f", 116 "expm1l", 117 "fabs", 118 "fabsf", 119 "fabsl", 120 "fclose", 121 "fdopen", 122 "feof", 123 "ferror", 124 "fflush", 125 "ffs", 126 "ffsl", 127 "ffsll", 128 "fgetc", 129 "fgetpos", 130 "fgets", 131 "fileno", 132 "fiprintf", 133 "flockfile", 134 "floor", 135 "floorf", 136 "floorl", 137 "fmod", 138 "fmodf", 139 "fmodl", 140 "fopen", 141 "fopen64", 142 "fprintf", 143 "fputc", 144 "fputs", 145 "fread", 146 "free", 147 "frexp", 148 "frexpf", 149 "frexpl", 150 "fscanf", 151 "fseek", 152 "fseeko", 153 "fseeko64", 154 "fsetpos", 155 "fstat", 156 "fstat64", 157 "fstatvfs", 158 "fstatvfs64", 159 "ftell", 160 "ftello", 161 "ftello64", 162 "ftrylockfile", 163 "funlockfile", 164 "fwrite", 165 "getc", 166 "getc_unlocked", 167 "getchar", 168 "getenv", 169 "getitimer", 170 "getlogin_r", 171 "getpwnam", 172 "gets", 173 "gettimeofday", 174 "htonl", 175 "htons", 176 "iprintf", 177 "isascii", 178 "isdigit", 179 "labs", 180 "lchown", 181 "llabs", 182 "log", 183 "log10", 184 "log10f", 185 "log10l", 186 "log1p", 187 "log1pf", 188 "log1pl", 189 "log2", 190 "log2f", 191 "log2l", 192 "logb", 193 "logbf", 194 "logbl", 195 "logf", 196 "logl", 197 "lstat", 198 "lstat64", 199 "malloc", 200 "memalign", 201 "memccpy", 202 "memchr", 203 "memcmp", 204 "memcpy", 205 "memmove", 206 "memrchr", 207 "memset", 208 "memset_pattern16", 209 "mkdir", 210 "mktime", 211 "modf", 212 "modff", 213 "modfl", 214 "nearbyint", 215 "nearbyintf", 216 "nearbyintl", 217 "ntohl", 218 "ntohs", 219 "open", 220 "open64", 221 "opendir", 222 "pclose", 223 "perror", 224 "popen", 225 "posix_memalign", 226 "pow", 227 "powf", 228 "powl", 229 "pread", 230 "printf", 231 "putc", 232 "putchar", 233 "puts", 234 "pwrite", 235 "qsort", 236 "read", 237 "readlink", 238 "realloc", 239 "reallocf", 240 "realpath", 241 "remove", 242 "rename", 243 "rewind", 244 "rint", 245 "rintf", 246 "rintl", 247 "rmdir", 248 "round", 249 "roundf", 250 "roundl", 251 "scanf", 252 "setbuf", 253 "setitimer", 254 "setvbuf", 255 "sin", 256 "sinf", 257 "sinh", 258 "sinhf", 259 "sinhl", 260 "sinl", 261 "siprintf", 262 "snprintf", 263 "sprintf", 264 "sqrt", 265 "sqrtf", 266 "sqrtl", 267 "sscanf", 268 "stat", 269 "stat64", 270 "statvfs", 271 "statvfs64", 272 "stpcpy", 273 "stpncpy", 274 "strcasecmp", 275 "strcat", 276 "strchr", 277 "strcmp", 278 "strcoll", 279 "strcpy", 280 "strcspn", 281 "strdup", 282 "strlen", 283 "strncasecmp", 284 "strncat", 285 "strncmp", 286 "strncpy", 287 "strndup", 288 "strnlen", 289 "strpbrk", 290 "strrchr", 291 "strspn", 292 "strstr", 293 "strtod", 294 "strtof", 295 "strtok", 296 "strtok_r", 297 "strtol", 298 "strtold", 299 "strtoll", 300 "strtoul", 301 "strtoull", 302 "strxfrm", 303 "system", 304 "tan", 305 "tanf", 306 "tanh", 307 "tanhf", 308 "tanhl", 309 "tanl", 310 "times", 311 "tmpfile", 312 "tmpfile64", 313 "toascii", 314 "trunc", 315 "truncf", 316 "truncl", 317 "uname", 318 "ungetc", 319 "unlink", 320 "unsetenv", 321 "utime", 322 "utimes", 323 "valloc", 324 "vfprintf", 325 "vfscanf", 326 "vprintf", 327 "vscanf", 328 "vsnprintf", 329 "vsprintf", 330 "vsscanf", 331 "write" 332 }; 333 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()); 340 341 #ifndef NDEBUG 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"); 346 } 347 #endif // !NDEBUG 348 349 // memset_pattern16 is only available on iOS 3.0 and Mac OS/X 10.5 and later. 350 if (T.isMacOSX()) { 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); 356 } else { 357 TLI.setUnavailable(LibFunc::memset_pattern16); 358 } 359 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"); 369 } 370 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); 376 } 377 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); 402 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); 446 447 // Win32 provides some C99 math with mangled names 448 TLI.setAvailableWithName(LibFunc::copysign, "_copysign"); 449 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); 470 } 471 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); 529 530 // Win32 does *not* provide provide these functions, but they are 531 // specified by C99: 532 TLI.setUnavailable(LibFunc::atoll); 533 TLI.setUnavailable(LibFunc::frexpf); 534 TLI.setUnavailable(LibFunc::llabs); 535 } 536 537 // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and 538 // Linux (GLIBC): 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 542 switch (T.getOS()) { 543 case Triple::Darwin: 544 case Triple::MacOSX: 545 case Triple::IOS: 546 case Triple::FreeBSD: 547 case Triple::Linux: 548 break; 549 default: 550 TLI.setUnavailable(LibFunc::ffsl); 551 } 552 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 556 switch (T.getOS()) { 557 case Triple::FreeBSD: 558 case Triple::Linux: 559 break; 560 default: 561 TLI.setUnavailable(LibFunc::ffsll); 562 } 563 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); 583 } 584 } 585 586 587 TargetLibraryInfo::TargetLibraryInfo() : ImmutablePass(ID) { 588 // Default to everything being available. 589 memset(AvailableArray, -1, sizeof(AvailableArray)); 590 591 initialize(*this, Triple(), StandardNames); 592 } 593 594 TargetLibraryInfo::TargetLibraryInfo(const Triple &T) : ImmutablePass(ID) { 595 // Default to everything being available. 596 memset(AvailableArray, -1, sizeof(AvailableArray)); 597 598 initialize(*this, T, StandardNames); 599 } 600 601 TargetLibraryInfo::TargetLibraryInfo(const TargetLibraryInfo &TLI) 602 : ImmutablePass(ID) { 603 memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray)); 604 CustomNames = TLI.CustomNames; 605 } 606 607 namespace { 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; 615 } 616 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; 622 } 623 }; 624 } 625 626 bool TargetLibraryInfo::getLibFunc(StringRef funcName, 627 LibFunc::Func &F) const { 628 const char **Start = &StandardNames[0]; 629 const char **End = &StandardNames[LibFunc::NumLibFuncs]; 630 631 // Filter out empty names and names containing null bytes, those can't be in 632 // our table. 633 if (funcName.empty() || funcName.find('\0') != StringRef::npos) 634 return false; 635 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); 643 return true; 644 } 645 return false; 646 } 647 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)); 652 } 653