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