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
anchor()23 void TargetLibraryInfo::anchor() { }
24
25 const char* TargetLibraryInfo::StandardNames[LibFunc::NumLibFuncs] =
26 {
27 "_IO_getc",
28 "_IO_putc",
29 "_ZdaPv",
30 "_ZdlPv",
31 "_Znaj",
32 "_ZnajRKSt9nothrow_t",
33 "_Znam",
34 "_ZnamRKSt9nothrow_t",
35 "_Znwj",
36 "_ZnwjRKSt9nothrow_t",
37 "_Znwm",
38 "_ZnwmRKSt9nothrow_t",
39 "__cxa_atexit",
40 "__cxa_guard_abort",
41 "__cxa_guard_acquire",
42 "__cxa_guard_release",
43 "__isoc99_scanf",
44 "__isoc99_sscanf",
45 "__memcpy_chk",
46 "__strdup",
47 "__strndup",
48 "__strtok_r",
49 "abs",
50 "access",
51 "acos",
52 "acosf",
53 "acosh",
54 "acoshf",
55 "acoshl",
56 "acosl",
57 "asin",
58 "asinf",
59 "asinh",
60 "asinhf",
61 "asinhl",
62 "asinl",
63 "atan",
64 "atan2",
65 "atan2f",
66 "atan2l",
67 "atanf",
68 "atanh",
69 "atanhf",
70 "atanhl",
71 "atanl",
72 "atof",
73 "atoi",
74 "atol",
75 "atoll",
76 "bcmp",
77 "bcopy",
78 "bzero",
79 "calloc",
80 "cbrt",
81 "cbrtf",
82 "cbrtl",
83 "ceil",
84 "ceilf",
85 "ceill",
86 "chmod",
87 "chown",
88 "clearerr",
89 "closedir",
90 "copysign",
91 "copysignf",
92 "copysignl",
93 "cos",
94 "cosf",
95 "cosh",
96 "coshf",
97 "coshl",
98 "cosl",
99 "ctermid",
100 "exp",
101 "exp10",
102 "exp10f",
103 "exp10l",
104 "exp2",
105 "exp2f",
106 "exp2l",
107 "expf",
108 "expl",
109 "expm1",
110 "expm1f",
111 "expm1l",
112 "fabs",
113 "fabsf",
114 "fabsl",
115 "fclose",
116 "fdopen",
117 "feof",
118 "ferror",
119 "fflush",
120 "ffs",
121 "ffsl",
122 "ffsll",
123 "fgetc",
124 "fgetpos",
125 "fgets",
126 "fileno",
127 "fiprintf",
128 "flockfile",
129 "floor",
130 "floorf",
131 "floorl",
132 "fmod",
133 "fmodf",
134 "fmodl",
135 "fopen",
136 "fopen64",
137 "fprintf",
138 "fputc",
139 "fputs",
140 "fread",
141 "free",
142 "frexp",
143 "frexpf",
144 "frexpl",
145 "fscanf",
146 "fseek",
147 "fseeko",
148 "fseeko64",
149 "fsetpos",
150 "fstat",
151 "fstat64",
152 "fstatvfs",
153 "fstatvfs64",
154 "ftell",
155 "ftello",
156 "ftello64",
157 "ftrylockfile",
158 "funlockfile",
159 "fwrite",
160 "getc",
161 "getc_unlocked",
162 "getchar",
163 "getenv",
164 "getitimer",
165 "getlogin_r",
166 "getpwnam",
167 "gets",
168 "htonl",
169 "htons",
170 "iprintf",
171 "isascii",
172 "isdigit",
173 "labs",
174 "lchown",
175 "llabs",
176 "log",
177 "log10",
178 "log10f",
179 "log10l",
180 "log1p",
181 "log1pf",
182 "log1pl",
183 "log2",
184 "log2f",
185 "log2l",
186 "logb",
187 "logbf",
188 "logbl",
189 "logf",
190 "logl",
191 "lstat",
192 "lstat64",
193 "malloc",
194 "memalign",
195 "memccpy",
196 "memchr",
197 "memcmp",
198 "memcpy",
199 "memmove",
200 "memrchr",
201 "memset",
202 "memset_pattern16",
203 "mkdir",
204 "mktime",
205 "modf",
206 "modff",
207 "modfl",
208 "nearbyint",
209 "nearbyintf",
210 "nearbyintl",
211 "ntohl",
212 "ntohs",
213 "open",
214 "open64",
215 "opendir",
216 "pclose",
217 "perror",
218 "popen",
219 "posix_memalign",
220 "pow",
221 "powf",
222 "powl",
223 "pread",
224 "printf",
225 "putc",
226 "putchar",
227 "puts",
228 "pwrite",
229 "qsort",
230 "read",
231 "readlink",
232 "realloc",
233 "reallocf",
234 "realpath",
235 "remove",
236 "rename",
237 "rewind",
238 "rint",
239 "rintf",
240 "rintl",
241 "rmdir",
242 "round",
243 "roundf",
244 "roundl",
245 "scanf",
246 "setbuf",
247 "setitimer",
248 "setvbuf",
249 "sin",
250 "sinf",
251 "sinh",
252 "sinhf",
253 "sinhl",
254 "sinl",
255 "siprintf",
256 "snprintf",
257 "sprintf",
258 "sqrt",
259 "sqrtf",
260 "sqrtl",
261 "sscanf",
262 "stat",
263 "stat64",
264 "statvfs",
265 "statvfs64",
266 "stpcpy",
267 "stpncpy",
268 "strcasecmp",
269 "strcat",
270 "strchr",
271 "strcmp",
272 "strcoll",
273 "strcpy",
274 "strcspn",
275 "strdup",
276 "strlen",
277 "strncasecmp",
278 "strncat",
279 "strncmp",
280 "strncpy",
281 "strndup",
282 "strnlen",
283 "strpbrk",
284 "strrchr",
285 "strspn",
286 "strstr",
287 "strtod",
288 "strtof",
289 "strtok",
290 "strtok_r",
291 "strtol",
292 "strtold",
293 "strtoll",
294 "strtoul",
295 "strtoull",
296 "strxfrm",
297 "system",
298 "tan",
299 "tanf",
300 "tanh",
301 "tanhf",
302 "tanhl",
303 "tanl",
304 "times",
305 "tmpfile",
306 "tmpfile64",
307 "toascii",
308 "trunc",
309 "truncf",
310 "truncl",
311 "uname",
312 "ungetc",
313 "unlink",
314 "unsetenv",
315 "utime",
316 "utimes",
317 "valloc",
318 "vfprintf",
319 "vfscanf",
320 "vprintf",
321 "vscanf",
322 "vsnprintf",
323 "vsprintf",
324 "vsscanf",
325 "write"
326 };
327
328 /// initialize - Initialize the set of available library functions based on the
329 /// specified target triple. This should be carefully written so that a missing
330 /// target triple gets a sane set of defaults.
initialize(TargetLibraryInfo & TLI,const Triple & T,const char ** StandardNames)331 static void initialize(TargetLibraryInfo &TLI, const Triple &T,
332 const char **StandardNames) {
333 initializeTargetLibraryInfoPass(*PassRegistry::getPassRegistry());
334
335 #ifndef NDEBUG
336 // Verify that the StandardNames array is in alphabetical order.
337 for (unsigned F = 1; F < LibFunc::NumLibFuncs; ++F) {
338 if (strcmp(StandardNames[F-1], StandardNames[F]) >= 0)
339 llvm_unreachable("TargetLibraryInfo function names must be sorted");
340 }
341 #endif // !NDEBUG
342
343 // memset_pattern16 is only available on iOS 3.0 and Mac OS/X 10.5 and later.
344 if (T.isMacOSX()) {
345 if (T.isMacOSXVersionLT(10, 5))
346 TLI.setUnavailable(LibFunc::memset_pattern16);
347 } else if (T.getOS() == Triple::IOS) {
348 if (T.isOSVersionLT(3, 0))
349 TLI.setUnavailable(LibFunc::memset_pattern16);
350 } else {
351 TLI.setUnavailable(LibFunc::memset_pattern16);
352 }
353
354 if (T.isMacOSX() && T.getArch() == Triple::x86 &&
355 !T.isMacOSXVersionLT(10, 7)) {
356 // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
357 // we don't care about) have two versions; on recent OSX, the one we want
358 // has a $UNIX2003 suffix. The two implementations are identical except
359 // for the return value in some edge cases. However, we don't want to
360 // generate code that depends on the old symbols.
361 TLI.setAvailableWithName(LibFunc::fwrite, "fwrite$UNIX2003");
362 TLI.setAvailableWithName(LibFunc::fputs, "fputs$UNIX2003");
363 }
364
365 // iprintf and friends are only available on XCore and TCE.
366 if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) {
367 TLI.setUnavailable(LibFunc::iprintf);
368 TLI.setUnavailable(LibFunc::siprintf);
369 TLI.setUnavailable(LibFunc::fiprintf);
370 }
371
372 if (T.getOS() == Triple::Win32) {
373 // Win32 does not support long double
374 TLI.setUnavailable(LibFunc::acosl);
375 TLI.setUnavailable(LibFunc::asinl);
376 TLI.setUnavailable(LibFunc::atanl);
377 TLI.setUnavailable(LibFunc::atan2l);
378 TLI.setUnavailable(LibFunc::ceill);
379 TLI.setUnavailable(LibFunc::copysignl);
380 TLI.setUnavailable(LibFunc::cosl);
381 TLI.setUnavailable(LibFunc::coshl);
382 TLI.setUnavailable(LibFunc::expl);
383 TLI.setUnavailable(LibFunc::fabsf); // Win32 and Win64 both lack fabsf
384 TLI.setUnavailable(LibFunc::fabsl);
385 TLI.setUnavailable(LibFunc::floorl);
386 TLI.setUnavailable(LibFunc::fmodl);
387 TLI.setUnavailable(LibFunc::frexpl);
388 TLI.setUnavailable(LibFunc::logl);
389 TLI.setUnavailable(LibFunc::modfl);
390 TLI.setUnavailable(LibFunc::powl);
391 TLI.setUnavailable(LibFunc::sinl);
392 TLI.setUnavailable(LibFunc::sinhl);
393 TLI.setUnavailable(LibFunc::sqrtl);
394 TLI.setUnavailable(LibFunc::tanl);
395 TLI.setUnavailable(LibFunc::tanhl);
396
397 // Win32 only has C89 math
398 TLI.setUnavailable(LibFunc::acosh);
399 TLI.setUnavailable(LibFunc::acoshf);
400 TLI.setUnavailable(LibFunc::acoshl);
401 TLI.setUnavailable(LibFunc::asinh);
402 TLI.setUnavailable(LibFunc::asinhf);
403 TLI.setUnavailable(LibFunc::asinhl);
404 TLI.setUnavailable(LibFunc::atanh);
405 TLI.setUnavailable(LibFunc::atanhf);
406 TLI.setUnavailable(LibFunc::atanhl);
407 TLI.setUnavailable(LibFunc::cbrt);
408 TLI.setUnavailable(LibFunc::cbrtf);
409 TLI.setUnavailable(LibFunc::cbrtl);
410 TLI.setUnavailable(LibFunc::exp10);
411 TLI.setUnavailable(LibFunc::exp10f);
412 TLI.setUnavailable(LibFunc::exp10l);
413 TLI.setUnavailable(LibFunc::exp2);
414 TLI.setUnavailable(LibFunc::exp2f);
415 TLI.setUnavailable(LibFunc::exp2l);
416 TLI.setUnavailable(LibFunc::expm1);
417 TLI.setUnavailable(LibFunc::expm1f);
418 TLI.setUnavailable(LibFunc::expm1l);
419 TLI.setUnavailable(LibFunc::log2);
420 TLI.setUnavailable(LibFunc::log2f);
421 TLI.setUnavailable(LibFunc::log2l);
422 TLI.setUnavailable(LibFunc::log1p);
423 TLI.setUnavailable(LibFunc::log1pf);
424 TLI.setUnavailable(LibFunc::log1pl);
425 TLI.setUnavailable(LibFunc::logb);
426 TLI.setUnavailable(LibFunc::logbf);
427 TLI.setUnavailable(LibFunc::logbl);
428 TLI.setUnavailable(LibFunc::nearbyint);
429 TLI.setUnavailable(LibFunc::nearbyintf);
430 TLI.setUnavailable(LibFunc::nearbyintl);
431 TLI.setUnavailable(LibFunc::rint);
432 TLI.setUnavailable(LibFunc::rintf);
433 TLI.setUnavailable(LibFunc::rintl);
434 TLI.setUnavailable(LibFunc::round);
435 TLI.setUnavailable(LibFunc::roundf);
436 TLI.setUnavailable(LibFunc::roundl);
437 TLI.setUnavailable(LibFunc::trunc);
438 TLI.setUnavailable(LibFunc::truncf);
439 TLI.setUnavailable(LibFunc::truncl);
440
441 // Win32 provides some C99 math with mangled names
442 TLI.setAvailableWithName(LibFunc::copysign, "_copysign");
443
444 if (T.getArch() == Triple::x86) {
445 // Win32 on x86 implements single-precision math functions as macros
446 TLI.setUnavailable(LibFunc::acosf);
447 TLI.setUnavailable(LibFunc::asinf);
448 TLI.setUnavailable(LibFunc::atanf);
449 TLI.setUnavailable(LibFunc::atan2f);
450 TLI.setUnavailable(LibFunc::ceilf);
451 TLI.setUnavailable(LibFunc::copysignf);
452 TLI.setUnavailable(LibFunc::cosf);
453 TLI.setUnavailable(LibFunc::coshf);
454 TLI.setUnavailable(LibFunc::expf);
455 TLI.setUnavailable(LibFunc::floorf);
456 TLI.setUnavailable(LibFunc::fmodf);
457 TLI.setUnavailable(LibFunc::logf);
458 TLI.setUnavailable(LibFunc::powf);
459 TLI.setUnavailable(LibFunc::sinf);
460 TLI.setUnavailable(LibFunc::sinhf);
461 TLI.setUnavailable(LibFunc::sqrtf);
462 TLI.setUnavailable(LibFunc::tanf);
463 TLI.setUnavailable(LibFunc::tanhf);
464 }
465
466 // Win32 does *not* provide provide these functions, but they are
467 // generally available on POSIX-compliant systems:
468 TLI.setUnavailable(LibFunc::access);
469 TLI.setUnavailable(LibFunc::bcmp);
470 TLI.setUnavailable(LibFunc::bcopy);
471 TLI.setUnavailable(LibFunc::bzero);
472 TLI.setUnavailable(LibFunc::chmod);
473 TLI.setUnavailable(LibFunc::chown);
474 TLI.setUnavailable(LibFunc::closedir);
475 TLI.setUnavailable(LibFunc::ctermid);
476 TLI.setUnavailable(LibFunc::fdopen);
477 TLI.setUnavailable(LibFunc::ffs);
478 TLI.setUnavailable(LibFunc::fileno);
479 TLI.setUnavailable(LibFunc::flockfile);
480 TLI.setUnavailable(LibFunc::fseeko);
481 TLI.setUnavailable(LibFunc::fstat);
482 TLI.setUnavailable(LibFunc::fstatvfs);
483 TLI.setUnavailable(LibFunc::ftello);
484 TLI.setUnavailable(LibFunc::ftrylockfile);
485 TLI.setUnavailable(LibFunc::funlockfile);
486 TLI.setUnavailable(LibFunc::getc_unlocked);
487 TLI.setUnavailable(LibFunc::getitimer);
488 TLI.setUnavailable(LibFunc::getlogin_r);
489 TLI.setUnavailable(LibFunc::getpwnam);
490 TLI.setUnavailable(LibFunc::htonl);
491 TLI.setUnavailable(LibFunc::htons);
492 TLI.setUnavailable(LibFunc::lchown);
493 TLI.setUnavailable(LibFunc::lstat);
494 TLI.setUnavailable(LibFunc::memccpy);
495 TLI.setUnavailable(LibFunc::mkdir);
496 TLI.setUnavailable(LibFunc::ntohl);
497 TLI.setUnavailable(LibFunc::ntohs);
498 TLI.setUnavailable(LibFunc::open);
499 TLI.setUnavailable(LibFunc::opendir);
500 TLI.setUnavailable(LibFunc::pclose);
501 TLI.setUnavailable(LibFunc::popen);
502 TLI.setUnavailable(LibFunc::pread);
503 TLI.setUnavailable(LibFunc::pwrite);
504 TLI.setUnavailable(LibFunc::read);
505 TLI.setUnavailable(LibFunc::readlink);
506 TLI.setUnavailable(LibFunc::realpath);
507 TLI.setUnavailable(LibFunc::rmdir);
508 TLI.setUnavailable(LibFunc::setitimer);
509 TLI.setUnavailable(LibFunc::stat);
510 TLI.setUnavailable(LibFunc::statvfs);
511 TLI.setUnavailable(LibFunc::stpcpy);
512 TLI.setUnavailable(LibFunc::stpncpy);
513 TLI.setUnavailable(LibFunc::strcasecmp);
514 TLI.setUnavailable(LibFunc::strncasecmp);
515 TLI.setUnavailable(LibFunc::times);
516 TLI.setUnavailable(LibFunc::uname);
517 TLI.setUnavailable(LibFunc::unlink);
518 TLI.setUnavailable(LibFunc::unsetenv);
519 TLI.setUnavailable(LibFunc::utime);
520 TLI.setUnavailable(LibFunc::utimes);
521 TLI.setUnavailable(LibFunc::write);
522
523 // Win32 does *not* provide provide these functions, but they are
524 // specified by C99:
525 TLI.setUnavailable(LibFunc::atoll);
526 TLI.setUnavailable(LibFunc::frexpf);
527 TLI.setUnavailable(LibFunc::llabs);
528 }
529
530 // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
531 // Linux (GLIBC):
532 // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
533 // http://svn.freebsd.org/base/user/eri/pf45/head/lib/libc/string/ffsl.c
534 // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
535 switch (T.getOS()) {
536 case Triple::Darwin:
537 case Triple::MacOSX:
538 case Triple::IOS:
539 case Triple::FreeBSD:
540 case Triple::Linux:
541 break;
542 default:
543 TLI.setUnavailable(LibFunc::ffsl);
544 }
545
546 // ffsll is available on at least FreeBSD and Linux (GLIBC):
547 // http://svn.freebsd.org/base/user/eri/pf45/head/lib/libc/string/ffsll.c
548 // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
549 switch (T.getOS()) {
550 case Triple::FreeBSD:
551 case Triple::Linux:
552 break;
553 default:
554 TLI.setUnavailable(LibFunc::ffsll);
555 }
556
557 // The following functions are available on at least Linux:
558 if (T.getOS() != Triple::Linux) {
559 TLI.setUnavailable(LibFunc::dunder_strdup);
560 TLI.setUnavailable(LibFunc::dunder_strtok_r);
561 TLI.setUnavailable(LibFunc::dunder_isoc99_scanf);
562 TLI.setUnavailable(LibFunc::dunder_isoc99_sscanf);
563 TLI.setUnavailable(LibFunc::under_IO_getc);
564 TLI.setUnavailable(LibFunc::under_IO_putc);
565 TLI.setUnavailable(LibFunc::memalign);
566 TLI.setUnavailable(LibFunc::fopen64);
567 TLI.setUnavailable(LibFunc::fseeko64);
568 TLI.setUnavailable(LibFunc::fstat64);
569 TLI.setUnavailable(LibFunc::fstatvfs64);
570 TLI.setUnavailable(LibFunc::ftello64);
571 TLI.setUnavailable(LibFunc::lstat64);
572 TLI.setUnavailable(LibFunc::open64);
573 TLI.setUnavailable(LibFunc::stat64);
574 TLI.setUnavailable(LibFunc::statvfs64);
575 TLI.setUnavailable(LibFunc::tmpfile64);
576 }
577 }
578
579
TargetLibraryInfo()580 TargetLibraryInfo::TargetLibraryInfo() : ImmutablePass(ID) {
581 // Default to everything being available.
582 memset(AvailableArray, -1, sizeof(AvailableArray));
583
584 initialize(*this, Triple(), StandardNames);
585 }
586
TargetLibraryInfo(const Triple & T)587 TargetLibraryInfo::TargetLibraryInfo(const Triple &T) : ImmutablePass(ID) {
588 // Default to everything being available.
589 memset(AvailableArray, -1, sizeof(AvailableArray));
590
591 initialize(*this, T, StandardNames);
592 }
593
TargetLibraryInfo(const TargetLibraryInfo & TLI)594 TargetLibraryInfo::TargetLibraryInfo(const TargetLibraryInfo &TLI)
595 : ImmutablePass(ID) {
596 memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
597 CustomNames = TLI.CustomNames;
598 }
599
600 namespace {
601 struct StringComparator {
602 /// Compare two strings and return true if LHS is lexicographically less than
603 /// RHS. Requires that RHS doesn't contain any zero bytes.
operator ()__anon1b53fd160111::StringComparator604 bool operator()(const char *LHS, StringRef RHS) const {
605 // Compare prefixes with strncmp. If prefixes match we know that LHS is
606 // greater or equal to RHS as RHS can't contain any '\0'.
607 return std::strncmp(LHS, RHS.data(), RHS.size()) < 0;
608 }
609
610 // Provided for compatibility with MSVC's debug mode.
operator ()__anon1b53fd160111::StringComparator611 bool operator()(StringRef LHS, const char *RHS) const { return LHS < RHS; }
operator ()__anon1b53fd160111::StringComparator612 bool operator()(StringRef LHS, StringRef RHS) const { return LHS < RHS; }
operator ()__anon1b53fd160111::StringComparator613 bool operator()(const char *LHS, const char *RHS) const {
614 return std::strcmp(LHS, RHS) < 0;
615 }
616 };
617 }
618
getLibFunc(StringRef funcName,LibFunc::Func & F) const619 bool TargetLibraryInfo::getLibFunc(StringRef funcName,
620 LibFunc::Func &F) const {
621 const char **Start = &StandardNames[0];
622 const char **End = &StandardNames[LibFunc::NumLibFuncs];
623
624 // Filter out empty names and names containing null bytes, those can't be in
625 // our table.
626 if (funcName.empty() || funcName.find('\0') != StringRef::npos)
627 return false;
628
629 // Check for \01 prefix that is used to mangle __asm declarations and
630 // strip it if present.
631 if (funcName.front() == '\01')
632 funcName = funcName.substr(1);
633 const char **I = std::lower_bound(Start, End, funcName, StringComparator());
634 if (I != End && *I == funcName) {
635 F = (LibFunc::Func)(I - Start);
636 return true;
637 }
638 return false;
639 }
640
641 /// disableAllFunctions - This disables all builtins, which is used for options
642 /// like -fno-builtin.
disableAllFunctions()643 void TargetLibraryInfo::disableAllFunctions() {
644 memset(AvailableArray, 0, sizeof(AvailableArray));
645 }
646