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*, nothrow);
29 /// void operator delete(void*);
31 /// void operator delete(void*, nothrow);
33 /// void *new[](unsigned int);
35 /// void *new[](unsigned int, nothrow);
37 /// void *new[](unsigned long);
39 /// void *new[](unsigned long, nothrow);
41 /// void *new(unsigned int);
43 /// void *new(unsigned int, nothrow);
45 /// void *new(unsigned long);
47 /// void *new(unsigned long, nothrow);
49 /// double __cospi(double x);
51 /// float __cospif(float x);
53 /// int __cxa_atexit(void (*f)(void *), void *p, void *d);
55 /// void __cxa_guard_abort(guard_t *guard);
56 /// guard_t is int64_t in Itanium ABI or int32_t on ARM eabi.
58 /// int __cxa_guard_acquire(guard_t *guard);
60 /// void __cxa_guard_release(guard_t *guard);
62 /// int __isoc99_scanf (const char *format, ...)
64 /// int __isoc99_sscanf(const char *s, const char *format, ...)
66 /// void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size);
68 /// double __sincospi_stret(double x);
70 /// float __sincospi_stretf(float x);
72 /// double __sinpi(double x);
74 /// float __sinpif(float x);
76 /// double __sqrt_finite(double x);
78 /// float __sqrt_finite(float x);
80 /// long double __sqrt_finite(long double x);
82 /// char * __strdup(const char *s);
84 /// char *__strndup(const char *s, size_t n);
86 /// char * __strtok_r(char *s, const char *delim, char **save_ptr);
90 /// int access(const char *path, int amode);
92 /// double acos(double x);
94 /// float acosf(float x);
96 /// double acosh(double x);
98 /// float acoshf(float x);
100 /// long double acoshl(long double x);
102 /// long double acosl(long double x);
104 /// double asin(double x);
106 /// float asinf(float x);
108 /// double asinh(double x);
110 /// float asinhf(float x);
112 /// long double asinhl(long double x);
114 /// long double asinl(long double x);
116 /// double atan(double x);
118 /// double atan2(double y, double x);
120 /// float atan2f(float y, float x);
122 /// long double atan2l(long double y, long double x);
124 /// float atanf(float x);
126 /// double atanh(double x);
128 /// float atanhf(float x);
130 /// long double atanhl(long double x);
132 /// long double atanl(long double x);
134 /// double atof(const char *str);
136 /// int atoi(const char *str);
138 /// long atol(const char *str);
140 /// long long atoll(const char *nptr);
142 /// int bcmp(const void *s1, const void *s2, size_t n);
144 /// void bcopy(const void *s1, void *s2, size_t n);
146 /// void bzero(void *s, size_t n);
148 /// void *calloc(size_t count, size_t size);
150 /// double cbrt(double x);
152 /// float cbrtf(float x);
154 /// long double cbrtl(long double x);
156 /// double ceil(double x);
158 /// float ceilf(float x);
160 /// long double ceill(long double x);
162 /// int chmod(const char *path, mode_t mode);
164 /// int chown(const char *path, uid_t owner, gid_t group);
166 /// void clearerr(FILE *stream);
168 /// int closedir(DIR *dirp);
170 /// double copysign(double x, double y);
172 /// float copysignf(float x, float y);
174 /// long double copysignl(long double x, long double y);
176 /// double cos(double x);
178 /// float cosf(float x);
180 /// double cosh(double x);
182 /// float coshf(float x);
184 /// long double coshl(long double x);
186 /// long double cosl(long double x);
188 /// char *ctermid(char *s);
190 /// double exp(double x);
192 /// double exp10(double x);
194 /// float exp10f(float x);
196 /// long double exp10l(long double x);
198 /// double exp2(double x);
200 /// float exp2f(float x);
202 /// long double exp2l(long double x);
204 /// float expf(float x);
206 /// long double expl(long double x);
208 /// double expm1(double x);
210 /// float expm1f(float x);
212 /// long double expm1l(long double x);
214 /// double fabs(double x);
216 /// float fabsf(float x);
218 /// long double fabsl(long double x);
220 /// int fclose(FILE *stream);
222 /// FILE *fdopen(int fildes, const char *mode);
224 /// int feof(FILE *stream);
226 /// int ferror(FILE *stream);
228 /// int fflush(FILE *stream);
232 /// int ffsl(long int i);
234 /// int ffsll(long long int i);
236 /// int fgetc(FILE *stream);
238 /// int fgetpos(FILE *stream, fpos_t *pos);
240 /// char *fgets(char *s, int n, FILE *stream);
242 /// int fileno(FILE *stream);
244 /// int fiprintf(FILE *stream, const char *format, ...);
246 /// void flockfile(FILE *file);
248 /// double floor(double x);
250 /// float floorf(float x);
252 /// long double floorl(long double x);
254 /// double fmax(double x, double y);
256 /// float fmaxf(float x, float y);
258 /// long double fmaxl(long double x, long double y);
260 /// double fmin(double x, double y);
262 /// float fminf(float x, float y);
264 /// long double fminl(long double x, long double y);
266 /// double fmod(double x, double y);
268 /// float fmodf(float x, float y);
270 /// long double fmodl(long double x, long double y);
272 /// FILE *fopen(const char *filename, const char *mode);
274 /// FILE *fopen64(const char *filename, const char *opentype)
276 /// int fprintf(FILE *stream, const char *format, ...);
278 /// int fputc(int c, FILE *stream);
280 /// int fputs(const char *s, FILE *stream);
282 /// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
284 /// void free(void *ptr);
286 /// double frexp(double num, int *exp);
288 /// float frexpf(float num, int *exp);
290 /// long double frexpl(long double num, int *exp);
292 /// int fscanf(FILE *stream, const char *format, ... );
294 /// int fseek(FILE *stream, long offset, int whence);
296 /// int fseeko(FILE *stream, off_t offset, int whence);
298 /// int fseeko64(FILE *stream, off64_t offset, int whence)
300 /// int fsetpos(FILE *stream, const fpos_t *pos);
302 /// int fstat(int fildes, struct stat *buf);
304 /// int fstat64(int filedes, struct stat64 *buf)
306 /// int fstatvfs(int fildes, struct statvfs *buf);
308 /// int fstatvfs64(int fildes, struct statvfs64 *buf);
310 /// long ftell(FILE *stream);
312 /// off_t ftello(FILE *stream);
314 /// off64_t ftello64(FILE *stream)
316 /// int ftrylockfile(FILE *file);
318 /// void funlockfile(FILE *file);
320 /// size_t fwrite(const void *ptr, size_t size, size_t nitems,
323 /// int getc(FILE *stream);
325 /// int getc_unlocked(FILE *stream);
327 /// int getchar(void);
329 /// char *getenv(const char *name);
331 /// int getitimer(int which, struct itimerval *value);
333 /// int getlogin_r(char *name, size_t namesize);
335 /// struct passwd *getpwnam(const char *name);
337 /// char *gets(char *s);
339 /// int gettimeofday(struct timeval *tp, void *tzp);
341 /// uint32_t htonl(uint32_t hostlong);
343 /// uint16_t htons(uint16_t hostshort);
345 /// int iprintf(const char *format, ...);
347 /// int isascii(int c);
349 /// int isdigit(int c);
351 /// long int labs(long int j);
353 /// int lchown(const char *path, uid_t owner, gid_t group);
355 /// long long int llabs(long long int j);
357 /// double log(double x);
359 /// double log10(double x);
361 /// float log10f(float x);
363 /// long double log10l(long double x);
365 /// double log1p(double x);
367 /// float log1pf(float x);
369 /// long double log1pl(long double x);
371 /// double log2(double x);
373 /// float log2f(float x);
375 /// double long double log2l(long double x);
377 /// double logb(double x);
379 /// float logbf(float x);
381 /// long double logbl(long double x);
383 /// float logf(float x);
385 /// long double logl(long double x);
387 /// int lstat(const char *path, struct stat *buf);
389 /// int lstat64(const char *path, struct stat64 *buf);
391 /// void *malloc(size_t size);
393 /// void *memalign(size_t boundary, size_t size);
395 /// void *memccpy(void *s1, const void *s2, int c, size_t n);
397 /// void *memchr(const void *s, int c, size_t n);
399 /// int memcmp(const void *s1, const void *s2, size_t n);
401 /// void *memcpy(void *s1, const void *s2, size_t n);
403 /// void *memmove(void *s1, const void *s2, size_t n);
405 // void *memrchr(const void *s, int c, size_t n);
407 /// void *memset(void *b, int c, size_t len);
409 /// void memset_pattern16(void *b, const void *pattern16, size_t len);
411 /// int mkdir(const char *path, mode_t mode);
413 /// time_t mktime(struct tm *timeptr);
415 /// double modf(double x, double *iptr);
417 /// float modff(float, float *iptr);
419 /// long double modfl(long double value, long double *iptr);
421 /// double nearbyint(double x);
423 /// float nearbyintf(float x);
425 /// long double nearbyintl(long double x);
427 /// uint32_t ntohl(uint32_t netlong);
429 /// uint16_t ntohs(uint16_t netshort);
431 /// int open(const char *path, int oflag, ... );
433 /// int open64(const char *filename, int flags[, mode_t mode])
435 /// DIR *opendir(const char *dirname);
437 /// int pclose(FILE *stream);
439 /// void perror(const char *s);
441 /// FILE *popen(const char *command, const char *mode);
443 /// int posix_memalign(void **memptr, size_t alignment, size_t size);
445 /// double pow(double x, double y);
447 /// float powf(float x, float y);
449 /// long double powl(long double x, long double y);
451 /// ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset);
453 /// int printf(const char *format, ...);
455 /// int putc(int c, FILE *stream);
457 /// int putchar(int c);
459 /// int puts(const char *s);
461 /// ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
464 /// void qsort(void *base, size_t nel, size_t width,
465 /// int (*compar)(const void *, const void *));
467 /// ssize_t read(int fildes, void *buf, size_t nbyte);
469 /// ssize_t readlink(const char *path, char *buf, size_t bufsize);
471 /// void *realloc(void *ptr, size_t size);
473 /// void *reallocf(void *ptr, size_t size);
475 /// char *realpath(const char *file_name, char *resolved_name);
477 /// int remove(const char *path);
479 /// int rename(const char *old, const char *new);
481 /// void rewind(FILE *stream);
483 /// double rint(double x);
485 /// float rintf(float x);
487 /// long double rintl(long double x);
489 /// int rmdir(const char *path);
491 /// double round(double x);
493 /// float roundf(float x);
495 /// long double roundl(long double x);
497 /// int scanf(const char *restrict format, ... );
499 /// void setbuf(FILE *stream, char *buf);
501 /// int setitimer(int which, const struct itimerval *value,
502 /// struct itimerval *ovalue);
504 /// int setvbuf(FILE *stream, char *buf, int type, size_t size);
506 /// double sin(double x);
508 /// float sinf(float x);
510 /// double sinh(double x);
512 /// float sinhf(float x);
514 /// long double sinhl(long double x);
516 /// long double sinl(long double x);
518 /// int siprintf(char *str, const char *format, ...);
520 /// int snprintf(char *s, size_t n, const char *format, ...);
522 /// int sprintf(char *str, const char *format, ...);
524 /// double sqrt(double x);
526 /// float sqrtf(float x);
528 /// long double sqrtl(long double x);
530 /// int sscanf(const char *s, const char *format, ... );
532 /// int stat(const char *path, struct stat *buf);
534 /// int stat64(const char *path, struct stat64 *buf);
536 /// int statvfs(const char *path, struct statvfs *buf);
538 /// int statvfs64(const char *path, struct statvfs64 *buf)
540 /// char *stpcpy(char *s1, const char *s2);
542 /// char *stpncpy(char *s1, const char *s2, size_t n);
544 /// int strcasecmp(const char *s1, const char *s2);
546 /// char *strcat(char *s1, const char *s2);
548 /// char *strchr(const char *s, int c);
550 /// int strcmp(const char *s1, const char *s2);
552 /// int strcoll(const char *s1, const char *s2);
554 /// char *strcpy(char *s1, const char *s2);
556 /// size_t strcspn(const char *s1, const char *s2);
558 /// char *strdup(const char *s1);
560 /// size_t strlen(const char *s);
562 /// int strncasecmp(const char *s1, const char *s2, size_t n);
564 /// char *strncat(char *s1, const char *s2, size_t n);
566 /// int strncmp(const char *s1, const char *s2, size_t n);
568 /// char *strncpy(char *s1, const char *s2, size_t n);
570 /// char *strndup(const char *s1, size_t n);
572 /// size_t strnlen(const char *s, size_t maxlen);
574 /// char *strpbrk(const char *s1, const char *s2);
576 /// char *strrchr(const char *s, int c);
578 /// size_t strspn(const char *s1, const char *s2);
580 /// char *strstr(const char *s1, const char *s2);
582 /// double strtod(const char *nptr, char **endptr);
584 /// float strtof(const char *nptr, char **endptr);
586 // char *strtok(char *s1, const char *s2);
588 // char *strtok_r(char *s, const char *sep, char **lasts);
590 /// long int strtol(const char *nptr, char **endptr, int base);
592 /// long double strtold(const char *nptr, char **endptr);
594 /// long long int strtoll(const char *nptr, char **endptr, int base);
596 /// unsigned long int strtoul(const char *nptr, char **endptr, int base);
598 /// unsigned long long int strtoull(const char *nptr, char **endptr,
601 /// size_t strxfrm(char *s1, const char *s2, size_t n);
603 /// int system(const char *command);
605 /// double tan(double x);
607 /// float tanf(float x);
609 /// double tanh(double x);
611 /// float tanhf(float x);
613 /// long double tanhl(long double x);
615 /// long double tanl(long double x);
617 /// clock_t times(struct tms *buffer);
619 /// FILE *tmpfile(void);
621 /// FILE *tmpfile64(void)
623 /// int toascii(int c);
625 /// double trunc(double x);
627 /// float truncf(float x);
629 /// long double truncl(long double x);
631 /// int uname(struct utsname *name);
633 /// int ungetc(int c, FILE *stream);
635 /// int unlink(const char *path);
637 /// int unsetenv(const char *name);
639 /// int utime(const char *path, const struct utimbuf *times);
641 /// int utimes(const char *path, const struct timeval times[2]);
643 /// void *valloc(size_t size);
645 /// int vfprintf(FILE *stream, const char *format, va_list ap);
647 /// int vfscanf(FILE *stream, const char *format, va_list arg);
649 /// int vprintf(const char *restrict format, va_list ap);
651 /// int vscanf(const char *format, va_list arg);
653 /// int vsnprintf(char *s, size_t n, const char *format, va_list ap);
655 /// int vsprintf(char *s, const char *format, va_list ap);
657 /// int vsscanf(const char *s, const char *format, va_list arg);
659 /// ssize_t write(int fildes, const void *buf, size_t nbyte);
666 /// TargetLibraryInfo - This immutable pass captures information about what
667 /// library functions are available for the current target, and allows a
668 /// frontend to disable optimizations through -fno-builtin etc.
669 class TargetLibraryInfo : public ImmutablePass {
670 virtual void anchor();
671 unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
672 llvm::DenseMap<unsigned, std::string> CustomNames;
673 static const char* StandardNames[LibFunc::NumLibFuncs];
675 enum AvailabilityState {
676 StandardName = 3, // (memset to all ones)
678 Unavailable = 0 // (memset to all zeros)
680 void setState(LibFunc::Func F, AvailabilityState State) {
681 AvailableArray[F/4] &= ~(3 << 2*(F&3));
682 AvailableArray[F/4] |= State << 2*(F&3);
684 AvailabilityState getState(LibFunc::Func F) const {
685 return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
691 TargetLibraryInfo(const Triple &T);
692 explicit TargetLibraryInfo(const TargetLibraryInfo &TLI);
694 /// getLibFunc - Search for a particular function name. If it is one of the
695 /// known library functions, return true and set F to the corresponding value.
696 bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
698 /// has - This function is used by optimizations that want to match on or form
699 /// a given library function.
700 bool has(LibFunc::Func F) const {
701 return getState(F) != Unavailable;
704 /// hasOptimizedCodeGen - Return true if the function is both available as
705 /// a builtin and a candidate for optimized code generation.
706 bool hasOptimizedCodeGen(LibFunc::Func F) const {
707 if (getState(F) == Unavailable)
711 case LibFunc::copysign: case LibFunc::copysignf: case LibFunc::copysignl:
712 case LibFunc::fabs: case LibFunc::fabsf: case LibFunc::fabsl:
713 case LibFunc::sin: case LibFunc::sinf: case LibFunc::sinl:
714 case LibFunc::cos: case LibFunc::cosf: case LibFunc::cosl:
715 case LibFunc::sqrt: case LibFunc::sqrtf: case LibFunc::sqrtl:
716 case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
717 case LibFunc::sqrtl_finite:
718 case LibFunc::fmax: case LibFunc::fmaxf: case LibFunc::fmaxl:
719 case LibFunc::fmin: case LibFunc::fminf: case LibFunc::fminl:
720 case LibFunc::floor: case LibFunc::floorf: case LibFunc::floorl:
721 case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
722 case LibFunc::ceil: case LibFunc::ceilf: case LibFunc::ceill:
723 case LibFunc::rint: case LibFunc::rintf: case LibFunc::rintl:
724 case LibFunc::round: case LibFunc::roundf: case LibFunc::roundl:
725 case LibFunc::trunc: case LibFunc::truncf: case LibFunc::truncl:
726 case LibFunc::log2: case LibFunc::log2f: case LibFunc::log2l:
727 case LibFunc::exp2: case LibFunc::exp2f: case LibFunc::exp2l:
728 case LibFunc::memcmp: case LibFunc::strcmp: case LibFunc::strcpy:
729 case LibFunc::stpcpy: case LibFunc::strlen: case LibFunc::strnlen:
730 case LibFunc::memchr:
736 StringRef getName(LibFunc::Func F) const {
737 AvailabilityState State = getState(F);
738 if (State == Unavailable)
740 if (State == StandardName)
741 return StandardNames[F];
742 assert(State == CustomName);
743 return CustomNames.find(F)->second;
746 /// setUnavailable - this can be used by whatever sets up TargetLibraryInfo to
747 /// ban use of specific library functions.
748 void setUnavailable(LibFunc::Func F) {
749 setState(F, Unavailable);
752 void setAvailable(LibFunc::Func F) {
753 setState(F, StandardName);
756 void setAvailableWithName(LibFunc::Func F, StringRef Name) {
757 if (StandardNames[F] != Name) {
758 setState(F, CustomName);
759 CustomNames[F] = Name;
760 assert(CustomNames.find(F) != CustomNames.end());
762 setState(F, StandardName);
766 /// disableAllFunctions - This disables all builtins, which is used for
767 /// options like -fno-builtin.
768 void disableAllFunctions();
771 } // end namespace llvm