1 //===-- 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_ANALYSIS_TARGETLIBRARYINFO_H
11 #define LLVM_ANALYSIS_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*, unsigned int);
31 /// void operator delete[](void*, unsigned long);
33 /// void operator delete(void*);
35 /// void operator delete(void*, nothrow);
37 /// void operator delete(void*, unsigned int);
39 /// void operator delete(void*, unsigned long);
41 /// void *new[](unsigned int);
43 /// void *new[](unsigned int, nothrow);
45 /// void *new[](unsigned long);
47 /// void *new[](unsigned long, nothrow);
49 /// void *new(unsigned int);
51 /// void *new(unsigned int, nothrow);
53 /// void *new(unsigned long);
55 /// void *new(unsigned long, nothrow);
57 /// double __cospi(double x);
59 /// float __cospif(float x);
61 /// int __cxa_atexit(void (*f)(void *), void *p, void *d);
63 /// void __cxa_guard_abort(guard_t *guard);
64 /// guard_t is int64_t in Itanium ABI or int32_t on ARM eabi.
66 /// int __cxa_guard_acquire(guard_t *guard);
68 /// void __cxa_guard_release(guard_t *guard);
70 /// int __isoc99_scanf (const char *format, ...)
72 /// int __isoc99_sscanf(const char *s, const char *format, ...)
74 /// void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size);
76 /// void *__memmove_chk(void *s1, const void *s2, size_t n,
79 /// void *__memset_chk(void *s, char v, size_t n, size_t s1size);
81 /// double __sincospi_stret(double x);
83 /// float __sincospif_stret(float x);
85 /// double __sinpi(double x);
87 /// float __sinpif(float x);
89 /// double __sqrt_finite(double x);
91 /// float __sqrt_finite(float x);
93 /// long double __sqrt_finite(long double x);
95 /// char *__stpcpy_chk(char *s1, const char *s2, size_t s1size);
97 /// char *__stpncpy_chk(char *s1, const char *s2, size_t n,
100 /// char *__strcpy_chk(char *s1, const char *s2, size_t s1size);
102 /// char * __strdup(const char *s);
104 /// char *__strncpy_chk(char *s1, const char *s2, size_t n,
107 /// char *__strndup(const char *s, size_t n);
109 /// char * __strtok_r(char *s, const char *delim, char **save_ptr);
113 /// int access(const char *path, int amode);
115 /// double acos(double x);
117 /// float acosf(float x);
119 /// double acosh(double x);
121 /// float acoshf(float x);
123 /// long double acoshl(long double x);
125 /// long double acosl(long double x);
127 /// double asin(double x);
129 /// float asinf(float x);
131 /// double asinh(double x);
133 /// float asinhf(float x);
135 /// long double asinhl(long double x);
137 /// long double asinl(long double x);
139 /// double atan(double x);
141 /// double atan2(double y, double x);
143 /// float atan2f(float y, float x);
145 /// long double atan2l(long double y, long double x);
147 /// float atanf(float x);
149 /// double atanh(double x);
151 /// float atanhf(float x);
153 /// long double atanhl(long double x);
155 /// long double atanl(long double x);
157 /// double atof(const char *str);
159 /// int atoi(const char *str);
161 /// long atol(const char *str);
163 /// long long atoll(const char *nptr);
165 /// int bcmp(const void *s1, const void *s2, size_t n);
167 /// void bcopy(const void *s1, void *s2, size_t n);
169 /// void bzero(void *s, size_t n);
171 /// void *calloc(size_t count, size_t size);
173 /// double cbrt(double x);
175 /// float cbrtf(float x);
177 /// long double cbrtl(long double x);
179 /// double ceil(double x);
181 /// float ceilf(float x);
183 /// long double ceill(long double x);
185 /// int chmod(const char *path, mode_t mode);
187 /// int chown(const char *path, uid_t owner, gid_t group);
189 /// void clearerr(FILE *stream);
191 /// int closedir(DIR *dirp);
193 /// double copysign(double x, double y);
195 /// float copysignf(float x, float y);
197 /// long double copysignl(long double x, long double y);
199 /// double cos(double x);
201 /// float cosf(float x);
203 /// double cosh(double x);
205 /// float coshf(float x);
207 /// long double coshl(long double x);
209 /// long double cosl(long double x);
211 /// char *ctermid(char *s);
213 /// double exp(double x);
215 /// double exp10(double x);
217 /// float exp10f(float x);
219 /// long double exp10l(long double x);
221 /// double exp2(double x);
223 /// float exp2f(float x);
225 /// long double exp2l(long double x);
227 /// float expf(float x);
229 /// long double expl(long double x);
231 /// double expm1(double x);
233 /// float expm1f(float x);
235 /// long double expm1l(long double x);
237 /// double fabs(double x);
239 /// float fabsf(float x);
241 /// long double fabsl(long double x);
243 /// int fclose(FILE *stream);
245 /// FILE *fdopen(int fildes, const char *mode);
247 /// int feof(FILE *stream);
249 /// int ferror(FILE *stream);
251 /// int fflush(FILE *stream);
255 /// int ffsl(long int i);
257 /// int ffsll(long long int i);
259 /// int fgetc(FILE *stream);
261 /// int fgetpos(FILE *stream, fpos_t *pos);
263 /// char *fgets(char *s, int n, FILE *stream);
265 /// int fileno(FILE *stream);
267 /// int fiprintf(FILE *stream, const char *format, ...);
269 /// void flockfile(FILE *file);
271 /// double floor(double x);
273 /// float floorf(float x);
275 /// long double floorl(long double x);
277 /// double fmax(double x, double y);
279 /// float fmaxf(float x, float y);
281 /// long double fmaxl(long double x, long double y);
283 /// double fmin(double x, double y);
285 /// float fminf(float x, float y);
287 /// long double fminl(long double x, long double y);
289 /// double fmod(double x, double y);
291 /// float fmodf(float x, float y);
293 /// long double fmodl(long double x, long double y);
295 /// FILE *fopen(const char *filename, const char *mode);
297 /// FILE *fopen64(const char *filename, const char *opentype)
299 /// int fprintf(FILE *stream, const char *format, ...);
301 /// int fputc(int c, FILE *stream);
303 /// int fputs(const char *s, FILE *stream);
305 /// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
307 /// void free(void *ptr);
309 /// double frexp(double num, int *exp);
311 /// float frexpf(float num, int *exp);
313 /// long double frexpl(long double num, int *exp);
315 /// int fscanf(FILE *stream, const char *format, ... );
317 /// int fseek(FILE *stream, long offset, int whence);
319 /// int fseeko(FILE *stream, off_t offset, int whence);
321 /// int fseeko64(FILE *stream, off64_t offset, int whence)
323 /// int fsetpos(FILE *stream, const fpos_t *pos);
325 /// int fstat(int fildes, struct stat *buf);
327 /// int fstat64(int filedes, struct stat64 *buf)
329 /// int fstatvfs(int fildes, struct statvfs *buf);
331 /// int fstatvfs64(int fildes, struct statvfs64 *buf);
333 /// long ftell(FILE *stream);
335 /// off_t ftello(FILE *stream);
337 /// off64_t ftello64(FILE *stream)
339 /// int ftrylockfile(FILE *file);
341 /// void funlockfile(FILE *file);
343 /// size_t fwrite(const void *ptr, size_t size, size_t nitems,
346 /// int getc(FILE *stream);
348 /// int getc_unlocked(FILE *stream);
350 /// int getchar(void);
352 /// char *getenv(const char *name);
354 /// int getitimer(int which, struct itimerval *value);
356 /// int getlogin_r(char *name, size_t namesize);
358 /// struct passwd *getpwnam(const char *name);
360 /// char *gets(char *s);
362 /// int gettimeofday(struct timeval *tp, void *tzp);
364 /// uint32_t htonl(uint32_t hostlong);
366 /// uint16_t htons(uint16_t hostshort);
368 /// int iprintf(const char *format, ...);
370 /// int isascii(int c);
372 /// int isdigit(int c);
374 /// long int labs(long int j);
376 /// int lchown(const char *path, uid_t owner, gid_t group);
378 /// double ldexp(double x, int n);
380 /// float ldexpf(float x, int n);
382 /// long double ldexpl(long double x, int n);
384 /// long long int llabs(long long int j);
386 /// double log(double x);
388 /// double log10(double x);
390 /// float log10f(float x);
392 /// long double log10l(long double x);
394 /// double log1p(double x);
396 /// float log1pf(float x);
398 /// long double log1pl(long double x);
400 /// double log2(double x);
402 /// float log2f(float x);
404 /// double long double log2l(long double x);
406 /// double logb(double x);
408 /// float logbf(float x);
410 /// long double logbl(long double x);
412 /// float logf(float x);
414 /// long double logl(long double x);
416 /// int lstat(const char *path, struct stat *buf);
418 /// int lstat64(const char *path, struct stat64 *buf);
420 /// void *malloc(size_t size);
422 /// void *memalign(size_t boundary, size_t size);
424 /// void *memccpy(void *s1, const void *s2, int c, size_t n);
426 /// void *memchr(const void *s, int c, size_t n);
428 /// int memcmp(const void *s1, const void *s2, size_t n);
430 /// void *memcpy(void *s1, const void *s2, size_t n);
432 /// void *memmove(void *s1, const void *s2, size_t n);
434 // void *memrchr(const void *s, int c, size_t n);
436 /// void *memset(void *b, int c, size_t len);
438 /// void memset_pattern16(void *b, const void *pattern16, size_t len);
440 /// int mkdir(const char *path, mode_t mode);
442 /// time_t mktime(struct tm *timeptr);
444 /// double modf(double x, double *iptr);
446 /// float modff(float, float *iptr);
448 /// long double modfl(long double value, long double *iptr);
450 /// double nearbyint(double x);
452 /// float nearbyintf(float x);
454 /// long double nearbyintl(long double x);
456 /// uint32_t ntohl(uint32_t netlong);
458 /// uint16_t ntohs(uint16_t netshort);
460 /// int open(const char *path, int oflag, ... );
462 /// int open64(const char *filename, int flags[, mode_t mode])
464 /// DIR *opendir(const char *dirname);
466 /// int pclose(FILE *stream);
468 /// void perror(const char *s);
470 /// FILE *popen(const char *command, const char *mode);
472 /// int posix_memalign(void **memptr, size_t alignment, size_t size);
474 /// double pow(double x, double y);
476 /// float powf(float x, float y);
478 /// long double powl(long double x, long double y);
480 /// ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset);
482 /// int printf(const char *format, ...);
484 /// int putc(int c, FILE *stream);
486 /// int putchar(int c);
488 /// int puts(const char *s);
490 /// ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
493 /// void qsort(void *base, size_t nel, size_t width,
494 /// int (*compar)(const void *, const void *));
496 /// ssize_t read(int fildes, void *buf, size_t nbyte);
498 /// ssize_t readlink(const char *path, char *buf, size_t bufsize);
500 /// void *realloc(void *ptr, size_t size);
502 /// void *reallocf(void *ptr, size_t size);
504 /// char *realpath(const char *file_name, char *resolved_name);
506 /// int remove(const char *path);
508 /// int rename(const char *old, const char *new);
510 /// void rewind(FILE *stream);
512 /// double rint(double x);
514 /// float rintf(float x);
516 /// long double rintl(long double x);
518 /// int rmdir(const char *path);
520 /// double round(double x);
522 /// float roundf(float x);
524 /// long double roundl(long double x);
526 /// int scanf(const char *restrict format, ... );
528 /// void setbuf(FILE *stream, char *buf);
530 /// int setitimer(int which, const struct itimerval *value,
531 /// struct itimerval *ovalue);
533 /// int setvbuf(FILE *stream, char *buf, int type, size_t size);
535 /// double sin(double x);
537 /// float sinf(float x);
539 /// double sinh(double x);
541 /// float sinhf(float x);
543 /// long double sinhl(long double x);
545 /// long double sinl(long double x);
547 /// int siprintf(char *str, const char *format, ...);
549 /// int snprintf(char *s, size_t n, const char *format, ...);
551 /// int sprintf(char *str, const char *format, ...);
553 /// double sqrt(double x);
555 /// float sqrtf(float x);
557 /// long double sqrtl(long double x);
559 /// int sscanf(const char *s, const char *format, ... );
561 /// int stat(const char *path, struct stat *buf);
563 /// int stat64(const char *path, struct stat64 *buf);
565 /// int statvfs(const char *path, struct statvfs *buf);
567 /// int statvfs64(const char *path, struct statvfs64 *buf)
569 /// char *stpcpy(char *s1, const char *s2);
571 /// char *stpncpy(char *s1, const char *s2, size_t n);
573 /// int strcasecmp(const char *s1, const char *s2);
575 /// char *strcat(char *s1, const char *s2);
577 /// char *strchr(const char *s, int c);
579 /// int strcmp(const char *s1, const char *s2);
581 /// int strcoll(const char *s1, const char *s2);
583 /// char *strcpy(char *s1, const char *s2);
585 /// size_t strcspn(const char *s1, const char *s2);
587 /// char *strdup(const char *s1);
589 /// size_t strlen(const char *s);
591 /// int strncasecmp(const char *s1, const char *s2, size_t n);
593 /// char *strncat(char *s1, const char *s2, size_t n);
595 /// int strncmp(const char *s1, const char *s2, size_t n);
597 /// char *strncpy(char *s1, const char *s2, size_t n);
599 /// char *strndup(const char *s1, size_t n);
601 /// size_t strnlen(const char *s, size_t maxlen);
603 /// char *strpbrk(const char *s1, const char *s2);
605 /// char *strrchr(const char *s, int c);
607 /// size_t strspn(const char *s1, const char *s2);
609 /// char *strstr(const char *s1, const char *s2);
611 /// double strtod(const char *nptr, char **endptr);
613 /// float strtof(const char *nptr, char **endptr);
615 // char *strtok(char *s1, const char *s2);
617 // char *strtok_r(char *s, const char *sep, char **lasts);
619 /// long int strtol(const char *nptr, char **endptr, int base);
621 /// long double strtold(const char *nptr, char **endptr);
623 /// long long int strtoll(const char *nptr, char **endptr, int base);
625 /// unsigned long int strtoul(const char *nptr, char **endptr, int base);
627 /// unsigned long long int strtoull(const char *nptr, char **endptr,
630 /// size_t strxfrm(char *s1, const char *s2, size_t n);
632 /// int system(const char *command);
634 /// double tan(double x);
636 /// float tanf(float x);
638 /// double tanh(double x);
640 /// float tanhf(float x);
642 /// long double tanhl(long double x);
644 /// long double tanl(long double x);
646 /// clock_t times(struct tms *buffer);
648 /// FILE *tmpfile(void);
650 /// FILE *tmpfile64(void)
652 /// int toascii(int c);
654 /// double trunc(double x);
656 /// float truncf(float x);
658 /// long double truncl(long double x);
660 /// int uname(struct utsname *name);
662 /// int ungetc(int c, FILE *stream);
664 /// int unlink(const char *path);
666 /// int unsetenv(const char *name);
668 /// int utime(const char *path, const struct utimbuf *times);
670 /// int utimes(const char *path, const struct timeval times[2]);
672 /// void *valloc(size_t size);
674 /// int vfprintf(FILE *stream, const char *format, va_list ap);
676 /// int vfscanf(FILE *stream, const char *format, va_list arg);
678 /// int vprintf(const char *restrict format, va_list ap);
680 /// int vscanf(const char *format, va_list arg);
682 /// int vsnprintf(char *s, size_t n, const char *format, va_list ap);
684 /// int vsprintf(char *s, const char *format, va_list ap);
686 /// int vsscanf(const char *s, const char *format, va_list arg);
688 /// ssize_t write(int fildes, const void *buf, size_t nbyte);
695 /// \brief Provides information about what library functions are available for
696 /// the current target.
698 /// This both allows optimizations to handle them specially and frontends to
699 /// disable such optimizations through -fno-builtin etc.
700 class TargetLibraryInfo {
701 unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
702 llvm::DenseMap<unsigned, std::string> CustomNames;
703 static const char* StandardNames[LibFunc::NumLibFuncs];
705 enum AvailabilityState {
706 StandardName = 3, // (memset to all ones)
708 Unavailable = 0 // (memset to all zeros)
710 void setState(LibFunc::Func F, AvailabilityState State) {
711 AvailableArray[F/4] &= ~(3 << 2*(F&3));
712 AvailableArray[F/4] |= State << 2*(F&3);
714 AvailabilityState getState(LibFunc::Func F) const {
715 return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
720 explicit TargetLibraryInfo(const Triple &T);
721 explicit TargetLibraryInfo(const TargetLibraryInfo &TLI);
723 /// \brief Searches for a particular function name.
725 /// If it is one of the known library functions, return true and set F to the
726 /// corresponding value.
727 bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
729 /// \brief Tests wether a library function is available.
730 bool has(LibFunc::Func F) const {
731 return getState(F) != Unavailable;
734 /// \brief Tests if the function is both available and a candidate for
735 /// optimized code generation.
736 bool hasOptimizedCodeGen(LibFunc::Func F) const {
737 if (getState(F) == Unavailable)
741 case LibFunc::copysign: case LibFunc::copysignf: case LibFunc::copysignl:
742 case LibFunc::fabs: case LibFunc::fabsf: case LibFunc::fabsl:
743 case LibFunc::sin: case LibFunc::sinf: case LibFunc::sinl:
744 case LibFunc::cos: case LibFunc::cosf: case LibFunc::cosl:
745 case LibFunc::sqrt: case LibFunc::sqrtf: case LibFunc::sqrtl:
746 case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
747 case LibFunc::sqrtl_finite:
748 case LibFunc::fmax: case LibFunc::fmaxf: case LibFunc::fmaxl:
749 case LibFunc::fmin: case LibFunc::fminf: case LibFunc::fminl:
750 case LibFunc::floor: case LibFunc::floorf: case LibFunc::floorl:
751 case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
752 case LibFunc::ceil: case LibFunc::ceilf: case LibFunc::ceill:
753 case LibFunc::rint: case LibFunc::rintf: case LibFunc::rintl:
754 case LibFunc::round: case LibFunc::roundf: case LibFunc::roundl:
755 case LibFunc::trunc: case LibFunc::truncf: case LibFunc::truncl:
756 case LibFunc::log2: case LibFunc::log2f: case LibFunc::log2l:
757 case LibFunc::exp2: case LibFunc::exp2f: case LibFunc::exp2l:
758 case LibFunc::memcmp: case LibFunc::strcmp: case LibFunc::strcpy:
759 case LibFunc::stpcpy: case LibFunc::strlen: case LibFunc::strnlen:
760 case LibFunc::memchr:
766 StringRef getName(LibFunc::Func F) const {
767 AvailabilityState State = getState(F);
768 if (State == Unavailable)
770 if (State == StandardName)
771 return StandardNames[F];
772 assert(State == CustomName);
773 return CustomNames.find(F)->second;
776 /// \brief Forces a function to be marked as unavailable.
777 void setUnavailable(LibFunc::Func F) {
778 setState(F, Unavailable);
781 /// \brief Forces a function to be marked as available.
782 void setAvailable(LibFunc::Func F) {
783 setState(F, StandardName);
786 /// \brief Forces a function to be marked as available and provide an
787 /// alternate name that must be used.
788 void setAvailableWithName(LibFunc::Func F, StringRef Name) {
789 if (StandardNames[F] != Name) {
790 setState(F, CustomName);
791 CustomNames[F] = Name;
792 assert(CustomNames.find(F) != CustomNames.end());
794 setState(F, StandardName);
798 /// \brief Disables all builtins.
800 /// This can be used for options like -fno-builtin.
801 void disableAllFunctions();
804 class TargetLibraryInfoWrapperPass : public ImmutablePass {
805 TargetLibraryInfo TLI;
807 virtual void anchor();
811 TargetLibraryInfoWrapperPass();
812 explicit TargetLibraryInfoWrapperPass(const Triple &T);
813 explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfo &TLI);
815 TargetLibraryInfo &getTLI() { return TLI; }
816 const TargetLibraryInfo &getTLI() const { return TLI; }
819 } // end namespace llvm