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