1 //===-- llvm/Target/TargetLibraryInfo.h - Library information ---*- C++ -*-===//
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 #ifndef LLVM_TARGET_TARGETLIBRARYINFO_H
11 #define LLVM_TARGET_TARGETLIBRARYINFO_H
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/Pass.h"
21 /// int _IO_getc(_IO_FILE * __fp);
23 /// int _IO_putc(int __c, _IO_FILE * __fp);
25 /// void operator delete[](void*);
27 /// void operator delete(void*);
29 /// void *new[](unsigned int);
31 /// void *new[](unsigned int, nothrow);
33 /// void *new[](unsigned long);
35 /// void *new[](unsigned long, nothrow);
37 /// void *new(unsigned int);
39 /// void *new(unsigned int, nothrow);
41 /// void *new(unsigned long);
43 /// void *new(unsigned long, nothrow);
45 /// int __cxa_atexit(void (*f)(void *), void *p, void *d);
47 /// void __cxa_guard_abort(guard_t *guard);
48 /// guard_t is int64_t in Itanium ABI or int32_t on ARM eabi.
50 /// int __cxa_guard_acquire(guard_t *guard);
52 /// void __cxa_guard_release(guard_t *guard);
54 /// int __isoc99_scanf (const char *format, ...)
56 /// int __isoc99_sscanf(const char *s, const char *format, ...)
58 /// void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size);
60 /// char * __strdup(const char *s);
62 /// char *__strndup(const char *s, size_t n);
64 /// char * __strtok_r(char *s, const char *delim, char **save_ptr);
68 /// int access(const char *path, int amode);
70 /// double acos(double x);
72 /// float acosf(float x);
74 /// double acosh(double x);
76 /// float acoshf(float x);
78 /// long double acoshl(long double x);
80 /// long double acosl(long double x);
82 /// double asin(double x);
84 /// float asinf(float x);
86 /// double asinh(double x);
88 /// float asinhf(float x);
90 /// long double asinhl(long double x);
92 /// long double asinl(long double x);
94 /// double atan(double x);
96 /// double atan2(double y, double x);
98 /// float atan2f(float y, float x);
100 /// long double atan2l(long double y, long double x);
102 /// float atanf(float x);
104 /// double atanh(double x);
106 /// float atanhf(float x);
108 /// long double atanhl(long double x);
110 /// long double atanl(long double x);
112 /// double atof(const char *str);
114 /// int atoi(const char *str);
116 /// long atol(const char *str);
118 /// long long atoll(const char *nptr);
120 /// int bcmp(const void *s1, const void *s2, size_t n);
122 /// void bcopy(const void *s1, void *s2, size_t n);
124 /// void bzero(void *s, size_t n);
126 /// void *calloc(size_t count, size_t size);
128 /// double cbrt(double x);
130 /// float cbrtf(float x);
132 /// long double cbrtl(long double x);
134 /// double ceil(double x);
136 /// float ceilf(float x);
138 /// long double ceill(long double x);
140 /// int chmod(const char *path, mode_t mode);
142 /// int chown(const char *path, uid_t owner, gid_t group);
144 /// void clearerr(FILE *stream);
146 /// int closedir(DIR *dirp);
148 /// double copysign(double x, double y);
150 /// float copysignf(float x, float y);
152 /// long double copysignl(long double x, long double y);
154 /// double cos(double x);
156 /// float cosf(float x);
158 /// double cosh(double x);
160 /// float coshf(float x);
162 /// long double coshl(long double x);
164 /// long double cosl(long double x);
166 /// char *ctermid(char *s);
168 /// double exp(double x);
170 /// double exp10(double x);
172 /// float exp10f(float x);
174 /// long double exp10l(long double x);
176 /// double exp2(double x);
178 /// float exp2f(float x);
180 /// long double exp2l(long double x);
182 /// float expf(float x);
184 /// long double expl(long double x);
186 /// double expm1(double x);
188 /// float expm1f(float x);
190 /// long double expm1l(long double x);
192 /// double fabs(double x);
194 /// float fabsf(float x);
196 /// long double fabsl(long double x);
198 /// int fclose(FILE *stream);
200 /// FILE *fdopen(int fildes, const char *mode);
202 /// int feof(FILE *stream);
204 /// int ferror(FILE *stream);
206 /// int fflush(FILE *stream);
210 /// int ffsl(long int i);
212 /// int ffsll(long long int i);
214 /// int fgetc(FILE *stream);
216 /// int fgetpos(FILE *stream, fpos_t *pos);
218 /// char *fgets(char *s, int n, FILE *stream);
220 /// int fileno(FILE *stream);
222 /// int fiprintf(FILE *stream, const char *format, ...);
224 /// void flockfile(FILE *file);
226 /// double floor(double x);
228 /// float floorf(float x);
230 /// long double floorl(long double x);
232 /// double fmod(double x, double y);
234 /// float fmodf(float x, float y);
236 /// long double fmodl(long double x, long double y);
238 /// FILE *fopen(const char *filename, const char *mode);
240 /// FILE *fopen64(const char *filename, const char *opentype)
242 /// int fprintf(FILE *stream, const char *format, ...);
244 /// int fputc(int c, FILE *stream);
246 /// int fputs(const char *s, FILE *stream);
248 /// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
250 /// void free(void *ptr);
252 /// double frexp(double num, int *exp);
254 /// float frexpf(float num, int *exp);
256 /// long double frexpl(long double num, int *exp);
258 /// int fscanf(FILE *stream, const char *format, ... );
260 /// int fseek(FILE *stream, long offset, int whence);
262 /// int fseeko(FILE *stream, off_t offset, int whence);
264 /// int fseeko64(FILE *stream, off64_t offset, int whence)
266 /// int fsetpos(FILE *stream, const fpos_t *pos);
268 /// int fstat(int fildes, struct stat *buf);
270 /// int fstat64(int filedes, struct stat64 *buf)
272 /// int fstatvfs(int fildes, struct statvfs *buf);
274 /// int fstatvfs64(int fildes, struct statvfs64 *buf);
276 /// long ftell(FILE *stream);
278 /// off_t ftello(FILE *stream);
280 /// off64_t ftello64(FILE *stream)
282 /// int ftrylockfile(FILE *file);
284 /// void funlockfile(FILE *file);
286 /// size_t fwrite(const void *ptr, size_t size, size_t nitems,
289 /// int getc(FILE *stream);
291 /// int getc_unlocked(FILE *stream);
293 /// int getchar(void);
295 /// char *getenv(const char *name);
297 /// int getitimer(int which, struct itimerval *value);
299 /// int getlogin_r(char *name, size_t namesize);
301 /// struct passwd *getpwnam(const char *name);
303 /// char *gets(char *s);
305 /// uint32_t htonl(uint32_t hostlong);
307 /// uint16_t htons(uint16_t hostshort);
309 /// int iprintf(const char *format, ...);
311 /// int isascii(int c);
313 /// int isdigit(int c);
315 /// long int labs(long int j);
317 /// int lchown(const char *path, uid_t owner, gid_t group);
319 /// long long int llabs(long long int j);
321 /// double log(double x);
323 /// double log10(double x);
325 /// float log10f(float x);
327 /// long double log10l(long double x);
329 /// double log1p(double x);
331 /// float log1pf(float x);
333 /// long double log1pl(long double x);
335 /// double log2(double x);
337 /// float log2f(float x);
339 /// double long double log2l(long double x);
341 /// double logb(double x);
343 /// float logbf(float x);
345 /// long double logbl(long double x);
347 /// float logf(float x);
349 /// long double logl(long double x);
351 /// int lstat(const char *path, struct stat *buf);
353 /// int lstat64(const char *path, struct stat64 *buf);
355 /// void *malloc(size_t size);
357 /// void *memalign(size_t boundary, size_t size);
359 /// void *memccpy(void *s1, const void *s2, int c, size_t n);
361 /// void *memchr(const void *s, int c, size_t n);
363 /// int memcmp(const void *s1, const void *s2, size_t n);
365 /// void *memcpy(void *s1, const void *s2, size_t n);
367 /// void *memmove(void *s1, const void *s2, size_t n);
369 // void *memrchr(const void *s, int c, size_t n);
371 /// void *memset(void *b, int c, size_t len);
373 /// void memset_pattern16(void *b, const void *pattern16, size_t len);
375 /// int mkdir(const char *path, mode_t mode);
377 /// time_t mktime(struct tm *timeptr);
379 /// double modf(double x, double *iptr);
381 /// float modff(float, float *iptr);
383 /// long double modfl(long double value, long double *iptr);
385 /// double nearbyint(double x);
387 /// float nearbyintf(float x);
389 /// long double nearbyintl(long double x);
391 /// uint32_t ntohl(uint32_t netlong);
393 /// uint16_t ntohs(uint16_t netshort);
395 /// int open(const char *path, int oflag, ... );
397 /// int open64(const char *filename, int flags[, mode_t mode])
399 /// DIR *opendir(const char *dirname);
401 /// int pclose(FILE *stream);
403 /// void perror(const char *s);
405 /// FILE *popen(const char *command, const char *mode);
407 /// int posix_memalign(void **memptr, size_t alignment, size_t size);
409 /// double pow(double x, double y);
411 /// float powf(float x, float y);
413 /// long double powl(long double x, long double y);
415 /// ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset);
417 /// int printf(const char *format, ...);
419 /// int putc(int c, FILE *stream);
421 /// int putchar(int c);
423 /// int puts(const char *s);
425 /// ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
428 /// void qsort(void *base, size_t nel, size_t width,
429 /// int (*compar)(const void *, const void *));
431 /// ssize_t read(int fildes, void *buf, size_t nbyte);
433 /// ssize_t readlink(const char *path, char *buf, size_t bufsize);
435 /// void *realloc(void *ptr, size_t size);
437 /// void *reallocf(void *ptr, size_t size);
439 /// char *realpath(const char *file_name, char *resolved_name);
441 /// int remove(const char *path);
443 /// int rename(const char *old, const char *new);
445 /// void rewind(FILE *stream);
447 /// double rint(double x);
449 /// float rintf(float x);
451 /// long double rintl(long double x);
453 /// int rmdir(const char *path);
455 /// double round(double x);
457 /// float roundf(float x);
459 /// long double roundl(long double x);
461 /// int scanf(const char *restrict format, ... );
463 /// void setbuf(FILE *stream, char *buf);
465 /// int setitimer(int which, const struct itimerval *value,
466 /// struct itimerval *ovalue);
468 /// int setvbuf(FILE *stream, char *buf, int type, size_t size);
470 /// double sin(double x);
472 /// float sinf(float x);
474 /// double sinh(double x);
476 /// float sinhf(float x);
478 /// long double sinhl(long double x);
480 /// long double sinl(long double x);
482 /// int siprintf(char *str, const char *format, ...);
484 /// int snprintf(char *s, size_t n, const char *format, ...);
486 /// int sprintf(char *str, const char *format, ...);
488 /// double sqrt(double x);
490 /// float sqrtf(float x);
492 /// long double sqrtl(long double x);
494 /// int sscanf(const char *s, const char *format, ... );
496 /// int stat(const char *path, struct stat *buf);
498 /// int stat64(const char *path, struct stat64 *buf);
500 /// int statvfs(const char *path, struct statvfs *buf);
502 /// int statvfs64(const char *path, struct statvfs64 *buf)
504 /// char *stpcpy(char *s1, const char *s2);
506 /// char *stpncpy(char *s1, const char *s2, size_t n);
508 /// int strcasecmp(const char *s1, const char *s2);
510 /// char *strcat(char *s1, const char *s2);
512 /// char *strchr(const char *s, int c);
514 /// int strcmp(const char *s1, const char *s2);
516 /// int strcoll(const char *s1, const char *s2);
518 /// char *strcpy(char *s1, const char *s2);
520 /// size_t strcspn(const char *s1, const char *s2);
522 /// char *strdup(const char *s1);
524 /// size_t strlen(const char *s);
526 /// int strncasecmp(const char *s1, const char *s2, size_t n);
528 /// char *strncat(char *s1, const char *s2, size_t n);
530 /// int strncmp(const char *s1, const char *s2, size_t n);
532 /// char *strncpy(char *s1, const char *s2, size_t n);
534 /// char *strndup(const char *s1, size_t n);
536 /// size_t strnlen(const char *s, size_t maxlen);
538 /// char *strpbrk(const char *s1, const char *s2);
540 /// char *strrchr(const char *s, int c);
542 /// size_t strspn(const char *s1, const char *s2);
544 /// char *strstr(const char *s1, const char *s2);
546 /// double strtod(const char *nptr, char **endptr);
548 /// float strtof(const char *nptr, char **endptr);
550 // char *strtok(char *s1, const char *s2);
552 // char *strtok_r(char *s, const char *sep, char **lasts);
554 /// long int strtol(const char *nptr, char **endptr, int base);
556 /// long double strtold(const char *nptr, char **endptr);
558 /// long long int strtoll(const char *nptr, char **endptr, int base);
560 /// unsigned long int strtoul(const char *nptr, char **endptr, int base);
562 /// unsigned long long int strtoull(const char *nptr, char **endptr,
565 /// size_t strxfrm(char *s1, const char *s2, size_t n);
567 /// int system(const char *command);
569 /// double tan(double x);
571 /// float tanf(float x);
573 /// double tanh(double x);
575 /// float tanhf(float x);
577 /// long double tanhl(long double x);
579 /// long double tanl(long double x);
581 /// clock_t times(struct tms *buffer);
583 /// FILE *tmpfile(void);
585 /// FILE *tmpfile64(void)
587 /// int toascii(int c);
589 /// double trunc(double x);
591 /// float truncf(float x);
593 /// long double truncl(long double x);
595 /// int uname(struct utsname *name);
597 /// int ungetc(int c, FILE *stream);
599 /// int unlink(const char *path);
601 /// int unsetenv(const char *name);
603 /// int utime(const char *path, const struct utimbuf *times);
605 /// int utimes(const char *path, const struct timeval times[2]);
607 /// void *valloc(size_t size);
609 /// int vfprintf(FILE *stream, const char *format, va_list ap);
611 /// int vfscanf(FILE *stream, const char *format, va_list arg);
613 /// int vprintf(const char *restrict format, va_list ap);
615 /// int vscanf(const char *format, va_list arg);
617 /// int vsnprintf(char *s, size_t n, const char *format, va_list ap);
619 /// int vsprintf(char *s, const char *format, va_list ap);
621 /// int vsscanf(const char *s, const char *format, va_list arg);
623 /// ssize_t write(int fildes, const void *buf, size_t nbyte);
630 /// TargetLibraryInfo - This immutable pass captures information about what
631 /// library functions are available for the current target, and allows a
632 /// frontend to disable optimizations through -fno-builtin etc.
633 class TargetLibraryInfo : public ImmutablePass {
634 virtual void anchor();
635 unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
636 llvm::DenseMap<unsigned, std::string> CustomNames;
637 static const char* StandardNames[LibFunc::NumLibFuncs];
639 enum AvailabilityState {
640 StandardName = 3, // (memset to all ones)
642 Unavailable = 0 // (memset to all zeros)
644 void setState(LibFunc::Func F, AvailabilityState State) {
645 AvailableArray[F/4] &= ~(3 << 2*(F&3));
646 AvailableArray[F/4] |= State << 2*(F&3);
648 AvailabilityState getState(LibFunc::Func F) const {
649 return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
655 TargetLibraryInfo(const Triple &T);
656 explicit TargetLibraryInfo(const TargetLibraryInfo &TLI);
658 /// getLibFunc - Search for a particular function name. If it is one of the
659 /// known library functions, return true and set F to the corresponding value.
660 bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
662 /// has - This function is used by optimizations that want to match on or form
663 /// a given library function.
664 bool has(LibFunc::Func F) const {
665 return getState(F) != Unavailable;
668 /// hasOptimizedCodeGen - Return true if the function is both available as
669 /// a builtin and a candidate for optimized code generation.
670 bool hasOptimizedCodeGen(LibFunc::Func F) const {
671 if (getState(F) == Unavailable)
675 case LibFunc::copysign: case LibFunc::copysignf: case LibFunc::copysignl:
676 case LibFunc::fabs: case LibFunc::fabsf: case LibFunc::fabsl:
677 case LibFunc::sin: case LibFunc::sinf: case LibFunc::sinl:
678 case LibFunc::cos: case LibFunc::cosf: case LibFunc::cosl:
679 case LibFunc::sqrt: case LibFunc::sqrtf: case LibFunc::sqrtl:
680 case LibFunc::floor: case LibFunc::floorf: case LibFunc::floorl:
681 case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
682 case LibFunc::ceil: case LibFunc::ceilf: case LibFunc::ceill:
683 case LibFunc::rint: case LibFunc::rintf: case LibFunc::rintl:
684 case LibFunc::trunc: case LibFunc::truncf: case LibFunc::truncl:
685 case LibFunc::log2: case LibFunc::log2f: case LibFunc::log2l:
686 case LibFunc::exp2: case LibFunc::exp2f: case LibFunc::exp2l:
687 case LibFunc::memcmp:
693 StringRef getName(LibFunc::Func F) const {
694 AvailabilityState State = getState(F);
695 if (State == Unavailable)
697 if (State == StandardName)
698 return StandardNames[F];
699 assert(State == CustomName);
700 return CustomNames.find(F)->second;
703 /// setUnavailable - this can be used by whatever sets up TargetLibraryInfo to
704 /// ban use of specific library functions.
705 void setUnavailable(LibFunc::Func F) {
706 setState(F, Unavailable);
709 void setAvailable(LibFunc::Func F) {
710 setState(F, StandardName);
713 void setAvailableWithName(LibFunc::Func F, StringRef Name) {
714 if (StandardNames[F] != Name) {
715 setState(F, CustomName);
716 CustomNames[F] = Name;
717 assert(CustomNames.find(F) != CustomNames.end());
719 setState(F, StandardName);
723 /// disableAllFunctions - This disables all builtins, which is used for
724 /// options like -fno-builtin.
725 void disableAllFunctions();
728 } // end namespace llvm