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