261ef0e3f0fe5d8242c6d8be9bb787ce5610d3f5
[oota-llvm.git] / include / llvm / Analysis / TargetLibraryInfo.h
1 //===-- TargetLibraryInfo.h - Library information ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
11 #define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
12
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Pass.h"
19
20 namespace llvm {
21 class PreservedAnalyses;
22
23   namespace LibFunc {
24     enum Func {
25       /// int _IO_getc(_IO_FILE * __fp);
26       under_IO_getc,
27       /// int _IO_putc(int __c, _IO_FILE * __fp);
28       under_IO_putc,
29       /// void operator delete[](void*);
30       ZdaPv,
31       /// void operator delete[](void*, nothrow);
32       ZdaPvRKSt9nothrow_t,
33       /// void operator delete[](void*, unsigned int);
34       ZdaPvj,
35       /// void operator delete[](void*, unsigned long);
36       ZdaPvm,
37       /// void operator delete(void*);
38       ZdlPv,
39       /// void operator delete(void*, nothrow);
40       ZdlPvRKSt9nothrow_t,
41       /// void operator delete(void*, unsigned int);
42       ZdlPvj,
43       /// void operator delete(void*, unsigned long);
44       ZdlPvm,
45       /// void *new[](unsigned int);
46       Znaj,
47       /// void *new[](unsigned int, nothrow);
48       ZnajRKSt9nothrow_t,
49       /// void *new[](unsigned long);
50       Znam,
51       /// void *new[](unsigned long, nothrow);
52       ZnamRKSt9nothrow_t,
53       /// void *new(unsigned int);
54       Znwj,
55       /// void *new(unsigned int, nothrow);
56       ZnwjRKSt9nothrow_t,
57       /// void *new(unsigned long);
58       Znwm,
59       /// void *new(unsigned long, nothrow);
60       ZnwmRKSt9nothrow_t,
61       /// double __cospi(double x);
62       cospi,
63       /// float __cospif(float x);
64       cospif,
65       /// int __cxa_atexit(void (*f)(void *), void *p, void *d);
66       cxa_atexit,
67       /// void __cxa_guard_abort(guard_t *guard);
68       /// guard_t is int64_t in Itanium ABI or int32_t on ARM eabi.
69       cxa_guard_abort,
70       /// int __cxa_guard_acquire(guard_t *guard);
71       cxa_guard_acquire,
72       /// void __cxa_guard_release(guard_t *guard);
73       cxa_guard_release,
74       /// int __isoc99_scanf (const char *format, ...)
75       dunder_isoc99_scanf,
76       /// int __isoc99_sscanf(const char *s, const char *format, ...)
77       dunder_isoc99_sscanf,
78       /// void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size);
79       memcpy_chk,
80       /// void *__memmove_chk(void *s1, const void *s2, size_t n,
81       ///                     size_t s1size);
82       memmove_chk,
83       /// void *__memset_chk(void *s, char v, size_t n, size_t s1size);
84       memset_chk,
85       /// double __sincospi_stret(double x);
86       sincospi_stret,
87       /// float __sincospif_stret(float x);
88       sincospif_stret,
89       /// double __sinpi(double x);
90       sinpi,
91       /// float __sinpif(float x);
92       sinpif,
93       /// double __sqrt_finite(double x);
94       sqrt_finite,
95       /// float __sqrt_finite(float x);
96       sqrtf_finite,
97       /// long double __sqrt_finite(long double x);
98       sqrtl_finite,
99       /// char *__stpcpy_chk(char *s1, const char *s2, size_t s1size);
100       stpcpy_chk,
101       /// char *__stpncpy_chk(char *s1, const char *s2, size_t n,
102       ///                     size_t s1size);
103       stpncpy_chk,
104       /// char *__strcpy_chk(char *s1, const char *s2, size_t s1size);
105       strcpy_chk,
106       /// char * __strdup(const char *s);
107       dunder_strdup,
108       /// char *__strncpy_chk(char *s1, const char *s2, size_t n,
109       ///                     size_t s1size);
110       strncpy_chk,
111       /// char *__strndup(const char *s, size_t n);
112       dunder_strndup,
113       /// char * __strtok_r(char *s, const char *delim, char **save_ptr);
114       dunder_strtok_r,
115       /// int abs(int j);
116       abs,
117       /// int access(const char *path, int amode);
118       access,
119       /// double acos(double x);
120       acos,
121       /// float acosf(float x);
122       acosf,
123       /// double acosh(double x);
124       acosh,
125       /// float acoshf(float x);
126       acoshf,
127       /// long double acoshl(long double x);
128       acoshl,
129       /// long double acosl(long double x);
130       acosl,
131       /// double asin(double x);
132       asin,
133       /// float asinf(float x);
134       asinf,
135       /// double asinh(double x);
136       asinh,
137       /// float asinhf(float x);
138       asinhf,
139       /// long double asinhl(long double x);
140       asinhl,
141       /// long double asinl(long double x);
142       asinl,
143       /// double atan(double x);
144       atan,
145       /// double atan2(double y, double x);
146       atan2,
147       /// float atan2f(float y, float x);
148       atan2f,
149       /// long double atan2l(long double y, long double x);
150       atan2l,
151       /// float atanf(float x);
152       atanf,
153       /// double atanh(double x);
154       atanh,
155       /// float atanhf(float x);
156       atanhf,
157       /// long double atanhl(long double x);
158       atanhl,
159       /// long double atanl(long double x);
160       atanl,
161       /// double atof(const char *str);
162       atof,
163       /// int atoi(const char *str);
164       atoi,
165       /// long atol(const char *str);
166       atol,
167       /// long long atoll(const char *nptr);
168       atoll,
169       /// int bcmp(const void *s1, const void *s2, size_t n);
170       bcmp,
171       /// void bcopy(const void *s1, void *s2, size_t n);
172       bcopy,
173       /// void bzero(void *s, size_t n);
174       bzero,
175       /// void *calloc(size_t count, size_t size);
176       calloc,
177       /// double cbrt(double x);
178       cbrt,
179       /// float cbrtf(float x);
180       cbrtf,
181       /// long double cbrtl(long double x);
182       cbrtl,
183       /// double ceil(double x);
184       ceil,
185       /// float ceilf(float x);
186       ceilf,
187       /// long double ceill(long double x);
188       ceill,
189       /// int chmod(const char *path, mode_t mode);
190       chmod,
191       /// int chown(const char *path, uid_t owner, gid_t group);
192       chown,
193       /// void clearerr(FILE *stream);
194       clearerr,
195       /// int closedir(DIR *dirp);
196       closedir,
197       /// double copysign(double x, double y);
198       copysign,
199       /// float copysignf(float x, float y);
200       copysignf,
201       /// long double copysignl(long double x, long double y);
202       copysignl,
203       /// double cos(double x);
204       cos,
205       /// float cosf(float x);
206       cosf,
207       /// double cosh(double x);
208       cosh,
209       /// float coshf(float x);
210       coshf,
211       /// long double coshl(long double x);
212       coshl,
213       /// long double cosl(long double x);
214       cosl,
215       /// char *ctermid(char *s);
216       ctermid,
217       /// double exp(double x);
218       exp,
219       /// double exp10(double x);
220       exp10,
221       /// float exp10f(float x);
222       exp10f,
223       /// long double exp10l(long double x);
224       exp10l,
225       /// double exp2(double x);
226       exp2,
227       /// float exp2f(float x);
228       exp2f,
229       /// long double exp2l(long double x);
230       exp2l,
231       /// float expf(float x);
232       expf,
233       /// long double expl(long double x);
234       expl,
235       /// double expm1(double x);
236       expm1,
237       /// float expm1f(float x);
238       expm1f,
239       /// long double expm1l(long double x);
240       expm1l,
241       /// double fabs(double x);
242       fabs,
243       /// float fabsf(float x);
244       fabsf,
245       /// long double fabsl(long double x);
246       fabsl,
247       /// int fclose(FILE *stream);
248       fclose,
249       /// FILE *fdopen(int fildes, const char *mode);
250       fdopen,
251       /// int feof(FILE *stream);
252       feof,
253       /// int ferror(FILE *stream);
254       ferror,
255       /// int fflush(FILE *stream);
256       fflush,
257       /// int ffs(int i);
258       ffs,
259       /// int ffsl(long int i);
260       ffsl,
261       /// int ffsll(long long int i);
262       ffsll,
263       /// int fgetc(FILE *stream);
264       fgetc,
265       /// int fgetpos(FILE *stream, fpos_t *pos);
266       fgetpos,
267       /// char *fgets(char *s, int n, FILE *stream);
268       fgets,
269       /// int fileno(FILE *stream);
270       fileno,
271       /// int fiprintf(FILE *stream, const char *format, ...);
272       fiprintf,
273       /// void flockfile(FILE *file);
274       flockfile,
275       /// double floor(double x);
276       floor,
277       /// float floorf(float x);
278       floorf,
279       /// long double floorl(long double x);
280       floorl,
281       /// double fmax(double x, double y);
282       fmax,
283       /// float fmaxf(float x, float y);
284       fmaxf,
285       /// long double fmaxl(long double x, long double y);
286       fmaxl,
287       /// double fmin(double x, double y);
288       fmin,
289       /// float fminf(float x, float y);
290       fminf,
291       /// long double fminl(long double x, long double y);
292       fminl,
293       /// double fmod(double x, double y);
294       fmod,
295       /// float fmodf(float x, float y);
296       fmodf,
297       /// long double fmodl(long double x, long double y);
298       fmodl,
299       /// FILE *fopen(const char *filename, const char *mode);
300       fopen,
301       /// FILE *fopen64(const char *filename, const char *opentype)
302       fopen64,
303       /// int fprintf(FILE *stream, const char *format, ...);
304       fprintf,
305       /// int fputc(int c, FILE *stream);
306       fputc,
307       /// int fputs(const char *s, FILE *stream);
308       fputs,
309       /// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
310       fread,
311       /// void free(void *ptr);
312       free,
313       /// double frexp(double num, int *exp);
314       frexp,
315       /// float frexpf(float num, int *exp);
316       frexpf,
317       /// long double frexpl(long double num, int *exp);
318       frexpl,
319       /// int fscanf(FILE *stream, const char *format, ... );
320       fscanf,
321       /// int fseek(FILE *stream, long offset, int whence);
322       fseek,
323       /// int fseeko(FILE *stream, off_t offset, int whence);
324       fseeko,
325       /// int fseeko64(FILE *stream, off64_t offset, int whence)
326       fseeko64,
327       /// int fsetpos(FILE *stream, const fpos_t *pos);
328       fsetpos,
329       /// int fstat(int fildes, struct stat *buf);
330       fstat,
331       /// int fstat64(int filedes, struct stat64 *buf)
332       fstat64,
333       /// int fstatvfs(int fildes, struct statvfs *buf);
334       fstatvfs,
335       /// int fstatvfs64(int fildes, struct statvfs64 *buf);
336       fstatvfs64,
337       /// long ftell(FILE *stream);
338       ftell,
339       /// off_t ftello(FILE *stream);
340       ftello,
341       /// off64_t ftello64(FILE *stream)
342       ftello64,
343       /// int ftrylockfile(FILE *file);
344       ftrylockfile,
345       /// void funlockfile(FILE *file);
346       funlockfile,
347       /// size_t fwrite(const void *ptr, size_t size, size_t nitems,
348       /// FILE *stream);
349       fwrite,
350       /// int getc(FILE *stream);
351       getc,
352       /// int getc_unlocked(FILE *stream);
353       getc_unlocked,
354       /// int getchar(void);
355       getchar,
356       /// char *getenv(const char *name);
357       getenv,
358       /// int getitimer(int which, struct itimerval *value);
359       getitimer,
360       /// int getlogin_r(char *name, size_t namesize);
361       getlogin_r,
362       /// struct passwd *getpwnam(const char *name);
363       getpwnam,
364       /// char *gets(char *s);
365       gets,
366       /// int gettimeofday(struct timeval *tp, void *tzp);
367       gettimeofday,
368       /// uint32_t htonl(uint32_t hostlong);
369       htonl,
370       /// uint16_t htons(uint16_t hostshort);
371       htons,
372       /// int iprintf(const char *format, ...);
373       iprintf,
374       /// int isascii(int c);
375       isascii,
376       /// int isdigit(int c);
377       isdigit,
378       /// long int labs(long int j);
379       labs,
380       /// int lchown(const char *path, uid_t owner, gid_t group);
381       lchown,
382       /// double ldexp(double x, int n);
383       ldexp,
384       /// float ldexpf(float x, int n);
385       ldexpf,
386       /// long double ldexpl(long double x, int n);
387       ldexpl,
388       /// long long int llabs(long long int j);
389       llabs,
390       /// double log(double x);
391       log,
392       /// double log10(double x);
393       log10,
394       /// float log10f(float x);
395       log10f,
396       /// long double log10l(long double x);
397       log10l,
398       /// double log1p(double x);
399       log1p,
400       /// float log1pf(float x);
401       log1pf,
402       /// long double log1pl(long double x);
403       log1pl,
404       /// double log2(double x);
405       log2,
406       /// float log2f(float x);
407       log2f,
408       /// double long double log2l(long double x);
409       log2l,
410       /// double logb(double x);
411       logb,
412       /// float logbf(float x);
413       logbf,
414       /// long double logbl(long double x);
415       logbl,
416       /// float logf(float x);
417       logf,
418       /// long double logl(long double x);
419       logl,
420       /// int lstat(const char *path, struct stat *buf);
421       lstat,
422       /// int lstat64(const char *path, struct stat64 *buf);
423       lstat64,
424       /// void *malloc(size_t size);
425       malloc,
426       /// void *memalign(size_t boundary, size_t size);
427       memalign,
428       /// void *memccpy(void *s1, const void *s2, int c, size_t n);
429       memccpy,
430       /// void *memchr(const void *s, int c, size_t n);
431       memchr,
432       /// int memcmp(const void *s1, const void *s2, size_t n);
433       memcmp,
434       /// void *memcpy(void *s1, const void *s2, size_t n);
435       memcpy,
436       /// void *memmove(void *s1, const void *s2, size_t n);
437       memmove,
438       // void *memrchr(const void *s, int c, size_t n);
439       memrchr,
440       /// void *memset(void *b, int c, size_t len);
441       memset,
442       /// void memset_pattern16(void *b, const void *pattern16, size_t len);
443       memset_pattern16,
444       /// int mkdir(const char *path, mode_t mode);
445       mkdir,
446       /// time_t mktime(struct tm *timeptr);
447       mktime,
448       /// double modf(double x, double *iptr);
449       modf,
450       /// float modff(float, float *iptr);
451       modff,
452       /// long double modfl(long double value, long double *iptr);
453       modfl,
454       /// double nearbyint(double x);
455       nearbyint,
456       /// float nearbyintf(float x);
457       nearbyintf,
458       /// long double nearbyintl(long double x);
459       nearbyintl,
460       /// uint32_t ntohl(uint32_t netlong);
461       ntohl,
462       /// uint16_t ntohs(uint16_t netshort);
463       ntohs,
464       /// int open(const char *path, int oflag, ... );
465       open,
466       /// int open64(const char *filename, int flags[, mode_t mode])
467       open64,
468       /// DIR *opendir(const char *dirname);
469       opendir,
470       /// int pclose(FILE *stream);
471       pclose,
472       /// void perror(const char *s);
473       perror,
474       /// FILE *popen(const char *command, const char *mode);
475       popen,
476       /// int posix_memalign(void **memptr, size_t alignment, size_t size);
477       posix_memalign,
478       /// double pow(double x, double y);
479       pow,
480       /// float powf(float x, float y);
481       powf,
482       /// long double powl(long double x, long double y);
483       powl,
484       /// ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset);
485       pread,
486       /// int printf(const char *format, ...);
487       printf,
488       /// int putc(int c, FILE *stream);
489       putc,
490       /// int putchar(int c);
491       putchar,
492       /// int puts(const char *s);
493       puts,
494       /// ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
495       ///                off_t offset);
496       pwrite,
497       /// void qsort(void *base, size_t nel, size_t width,
498       ///            int (*compar)(const void *, const void *));
499       qsort,
500       /// ssize_t read(int fildes, void *buf, size_t nbyte);
501       read,
502       /// ssize_t readlink(const char *path, char *buf, size_t bufsize);
503       readlink,
504       /// void *realloc(void *ptr, size_t size);
505       realloc,
506       /// void *reallocf(void *ptr, size_t size);
507       reallocf,
508       /// char *realpath(const char *file_name, char *resolved_name);
509       realpath,
510       /// int remove(const char *path);
511       remove,
512       /// int rename(const char *old, const char *new);
513       rename,
514       /// void rewind(FILE *stream);
515       rewind,
516       /// double rint(double x);
517       rint,
518       /// float rintf(float x);
519       rintf,
520       /// long double rintl(long double x);
521       rintl,
522       /// int rmdir(const char *path);
523       rmdir,
524       /// double round(double x);
525       round,
526       /// float roundf(float x);
527       roundf,
528       /// long double roundl(long double x);
529       roundl,
530       /// int scanf(const char *restrict format, ... );
531       scanf,
532       /// void setbuf(FILE *stream, char *buf);
533       setbuf,
534       /// int setitimer(int which, const struct itimerval *value,
535       ///               struct itimerval *ovalue);
536       setitimer,
537       /// int setvbuf(FILE *stream, char *buf, int type, size_t size);
538       setvbuf,
539       /// double sin(double x);
540       sin,
541       /// float sinf(float x);
542       sinf,
543       /// double sinh(double x);
544       sinh,
545       /// float sinhf(float x);
546       sinhf,
547       /// long double sinhl(long double x);
548       sinhl,
549       /// long double sinl(long double x);
550       sinl,
551       /// int siprintf(char *str, const char *format, ...);
552       siprintf,
553       /// int snprintf(char *s, size_t n, const char *format, ...);
554       snprintf,
555       /// int sprintf(char *str, const char *format, ...);
556       sprintf,
557       /// double sqrt(double x);
558       sqrt,
559       /// float sqrtf(float x);
560       sqrtf,
561       /// long double sqrtl(long double x);
562       sqrtl,
563       /// int sscanf(const char *s, const char *format, ... );
564       sscanf,
565       /// int stat(const char *path, struct stat *buf);
566       stat,
567       /// int stat64(const char *path, struct stat64 *buf);
568       stat64,
569       /// int statvfs(const char *path, struct statvfs *buf);
570       statvfs,
571       /// int statvfs64(const char *path, struct statvfs64 *buf)
572       statvfs64,
573       /// char *stpcpy(char *s1, const char *s2);
574       stpcpy,
575       /// char *stpncpy(char *s1, const char *s2, size_t n);
576       stpncpy,
577       /// int strcasecmp(const char *s1, const char *s2);
578       strcasecmp,
579       /// char *strcat(char *s1, const char *s2);
580       strcat,
581       /// char *strchr(const char *s, int c);
582       strchr,
583       /// int strcmp(const char *s1, const char *s2);
584       strcmp,
585       /// int strcoll(const char *s1, const char *s2);
586       strcoll,
587       /// char *strcpy(char *s1, const char *s2);
588       strcpy,
589       /// size_t strcspn(const char *s1, const char *s2);
590       strcspn,
591       /// char *strdup(const char *s1);
592       strdup,
593       /// size_t strlen(const char *s);
594       strlen,
595       /// int strncasecmp(const char *s1, const char *s2, size_t n);
596       strncasecmp,
597       /// char *strncat(char *s1, const char *s2, size_t n);
598       strncat,
599       /// int strncmp(const char *s1, const char *s2, size_t n);
600       strncmp,
601       /// char *strncpy(char *s1, const char *s2, size_t n);
602       strncpy,
603       /// char *strndup(const char *s1, size_t n);
604       strndup,
605       /// size_t strnlen(const char *s, size_t maxlen);
606       strnlen,
607       /// char *strpbrk(const char *s1, const char *s2);
608       strpbrk,
609       /// char *strrchr(const char *s, int c);
610       strrchr,
611       /// size_t strspn(const char *s1, const char *s2);
612       strspn,
613       /// char *strstr(const char *s1, const char *s2);
614       strstr,
615       /// double strtod(const char *nptr, char **endptr);
616       strtod,
617       /// float strtof(const char *nptr, char **endptr);
618       strtof,
619       // char *strtok(char *s1, const char *s2);
620       strtok,
621       // char *strtok_r(char *s, const char *sep, char **lasts);
622       strtok_r,
623       /// long int strtol(const char *nptr, char **endptr, int base);
624       strtol,
625       /// long double strtold(const char *nptr, char **endptr);
626       strtold,
627       /// long long int strtoll(const char *nptr, char **endptr, int base);
628       strtoll,
629       /// unsigned long int strtoul(const char *nptr, char **endptr, int base);
630       strtoul,
631       /// unsigned long long int strtoull(const char *nptr, char **endptr,
632       ///                                 int base);
633       strtoull,
634       /// size_t strxfrm(char *s1, const char *s2, size_t n);
635       strxfrm,
636       /// int system(const char *command);
637       system,
638       /// double tan(double x);
639       tan,
640       /// float tanf(float x);
641       tanf,
642       /// double tanh(double x);
643       tanh,
644       /// float tanhf(float x);
645       tanhf,
646       /// long double tanhl(long double x);
647       tanhl,
648       /// long double tanl(long double x);
649       tanl,
650       /// clock_t times(struct tms *buffer);
651       times,
652       /// FILE *tmpfile(void);
653       tmpfile,
654       /// FILE *tmpfile64(void)
655       tmpfile64,
656       /// int toascii(int c);
657       toascii,
658       /// double trunc(double x);
659       trunc,
660       /// float truncf(float x);
661       truncf,
662       /// long double truncl(long double x);
663       truncl,
664       /// int uname(struct utsname *name);
665       uname,
666       /// int ungetc(int c, FILE *stream);
667       ungetc,
668       /// int unlink(const char *path);
669       unlink,
670       /// int unsetenv(const char *name);
671       unsetenv,
672       /// int utime(const char *path, const struct utimbuf *times);
673       utime,
674       /// int utimes(const char *path, const struct timeval times[2]);
675       utimes,
676       /// void *valloc(size_t size);
677       valloc,
678       /// int vfprintf(FILE *stream, const char *format, va_list ap);
679       vfprintf,
680       /// int vfscanf(FILE *stream, const char *format, va_list arg);
681       vfscanf,
682       /// int vprintf(const char *restrict format, va_list ap);
683       vprintf,
684       /// int vscanf(const char *format, va_list arg);
685       vscanf,
686       /// int vsnprintf(char *s, size_t n, const char *format, va_list ap);
687       vsnprintf,
688       /// int vsprintf(char *s, const char *format, va_list ap);
689       vsprintf,
690       /// int vsscanf(const char *s, const char *format, va_list arg);
691       vsscanf,
692       /// ssize_t write(int fildes, const void *buf, size_t nbyte);
693       write,
694
695       NumLibFuncs
696     };
697   }
698
699 /// \brief Implementation of the target library information.
700 ///
701 /// This class constructs tables that hold the target library information and
702 /// make it available. However, it is somewhat expensive to compute and only
703 /// depends on the triple. So users typicaly interact with the \c
704 /// TargetLibraryInfo wrapper below.
705 class TargetLibraryInfoImpl {
706   friend class TargetLibraryInfo;
707
708   unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
709   llvm::DenseMap<unsigned, std::string> CustomNames;
710   static const char* StandardNames[LibFunc::NumLibFuncs];
711
712   enum AvailabilityState {
713     StandardName = 3, // (memset to all ones)
714     CustomName = 1,
715     Unavailable = 0  // (memset to all zeros)
716   };
717   void setState(LibFunc::Func F, AvailabilityState State) {
718     AvailableArray[F/4] &= ~(3 << 2*(F&3));
719     AvailableArray[F/4] |= State << 2*(F&3);
720   }
721   AvailabilityState getState(LibFunc::Func F) const {
722     return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
723   }
724
725 public:
726   TargetLibraryInfoImpl();
727   explicit TargetLibraryInfoImpl(const Triple &T);
728
729   // Provide value semantics.
730   TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
731   TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
732   TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
733   TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
734
735   /// \brief Searches for a particular function name.
736   ///
737   /// If it is one of the known library functions, return true and set F to the
738   /// corresponding value.
739   bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
740
741   /// \brief Forces a function to be marked as unavailable.
742   void setUnavailable(LibFunc::Func F) {
743     setState(F, Unavailable);
744   }
745
746   /// \brief Forces a function to be marked as available.
747   void setAvailable(LibFunc::Func F) {
748     setState(F, StandardName);
749   }
750
751   /// \brief Forces a function to be marked as available and provide an
752   /// alternate name that must be used.
753   void setAvailableWithName(LibFunc::Func F, StringRef Name) {
754     if (StandardNames[F] != Name) {
755       setState(F, CustomName);
756       CustomNames[F] = Name;
757       assert(CustomNames.find(F) != CustomNames.end());
758     } else {
759       setState(F, StandardName);
760     }
761   }
762
763   /// \brief Disables all builtins.
764   ///
765   /// This can be used for options like -fno-builtin.
766   void disableAllFunctions();
767 };
768
769 /// \brief Provides information about what library functions are available for
770 /// the current target.
771 ///
772 /// This both allows optimizations to handle them specially and frontends to
773 /// disable such optimizations through -fno-builtin etc.
774 class TargetLibraryInfo {
775   friend class TargetLibraryAnalysis;
776   friend class TargetLibraryInfoWrapperPass;
777
778   const TargetLibraryInfoImpl *Impl;
779
780 public:
781   explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
782
783   // Provide value semantics.
784   TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
785   TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
786   TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
787     Impl = TLI.Impl;
788     return *this;
789   }
790   TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
791     Impl = TLI.Impl;
792     return *this;
793   }
794
795   /// \brief Searches for a particular function name.
796   ///
797   /// If it is one of the known library functions, return true and set F to the
798   /// corresponding value.
799   bool getLibFunc(StringRef funcName, LibFunc::Func &F) const {
800     return Impl->getLibFunc(funcName, F);
801   }
802
803   /// \brief Tests wether a library function is available.
804   bool has(LibFunc::Func F) const {
805     return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
806   }
807
808   /// \brief Tests if the function is both available and a candidate for
809   /// optimized code generation.
810   bool hasOptimizedCodeGen(LibFunc::Func F) const {
811     if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
812       return false;
813     switch (F) {
814     default: break;
815     case LibFunc::copysign:  case LibFunc::copysignf:  case LibFunc::copysignl:
816     case LibFunc::fabs:      case LibFunc::fabsf:      case LibFunc::fabsl:
817     case LibFunc::sin:       case LibFunc::sinf:       case LibFunc::sinl:
818     case LibFunc::cos:       case LibFunc::cosf:       case LibFunc::cosl:
819     case LibFunc::sqrt:      case LibFunc::sqrtf:      case LibFunc::sqrtl:
820     case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
821                                                   case LibFunc::sqrtl_finite:
822     case LibFunc::fmax:      case LibFunc::fmaxf:      case LibFunc::fmaxl:
823     case LibFunc::fmin:      case LibFunc::fminf:      case LibFunc::fminl:
824     case LibFunc::floor:     case LibFunc::floorf:     case LibFunc::floorl:
825     case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
826     case LibFunc::ceil:      case LibFunc::ceilf:      case LibFunc::ceill:
827     case LibFunc::rint:      case LibFunc::rintf:      case LibFunc::rintl:
828     case LibFunc::round:     case LibFunc::roundf:     case LibFunc::roundl:
829     case LibFunc::trunc:     case LibFunc::truncf:     case LibFunc::truncl:
830     case LibFunc::log2:      case LibFunc::log2f:      case LibFunc::log2l:
831     case LibFunc::exp2:      case LibFunc::exp2f:      case LibFunc::exp2l:
832     case LibFunc::memcmp:    case LibFunc::strcmp:     case LibFunc::strcpy:
833     case LibFunc::stpcpy:    case LibFunc::strlen:     case LibFunc::strnlen:
834     case LibFunc::memchr:
835       return true;
836     }
837     return false;
838   }
839
840   StringRef getName(LibFunc::Func F) const {
841     auto State = Impl->getState(F);
842     if (State == TargetLibraryInfoImpl::Unavailable)
843       return StringRef();
844     if (State == TargetLibraryInfoImpl::StandardName)
845       return Impl->StandardNames[F];
846     assert(State == TargetLibraryInfoImpl::CustomName);
847     return Impl->CustomNames.find(F)->second;
848   }
849
850   /// \brief Handle invalidation from the pass manager.
851   ///
852   /// If we try to invalidate this info, just return false. It cannot become
853   /// invalid even if the module changes.
854   bool invalidate(Module &, const PreservedAnalyses &) { return false; }
855 };
856
857 /// \brief Analysis pass providing the \c TargetLibraryInfo.
858 ///
859 /// Note that this pass's result cannot be invalidated, it is immutable for the
860 /// life of the module.
861 class TargetLibraryAnalysis {
862 public:
863   typedef TargetLibraryInfo Result;
864
865   /// \brief Opaque, unique identifier for this analysis pass.
866   static void *ID() { return (void *)&PassID; }
867
868   /// \brief Default construct the library analysis.
869   ///
870   /// This will use the module's triple to construct the library info for that
871   /// module.
872   TargetLibraryAnalysis() {}
873
874   /// \brief Construct a library analysis with preset info.
875   ///
876   /// This will directly copy the preset info into the result without
877   /// consulting the module's triple.
878   TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
879       : PresetInfoImpl(std::move(PresetInfoImpl)) {}
880
881   // Move semantics. We spell out the constructors for MSVC.
882   TargetLibraryAnalysis(TargetLibraryAnalysis &&Arg)
883       : PresetInfoImpl(std::move(Arg.PresetInfoImpl)), Impls(std::move(Arg.Impls)) {}
884   TargetLibraryAnalysis &operator=(TargetLibraryAnalysis &&RHS) {
885     PresetInfoImpl = std::move(RHS.PresetInfoImpl);
886     Impls = std::move(RHS.Impls);
887     return *this;
888   }
889
890   TargetLibraryInfo run(Module &M);
891   TargetLibraryInfo run(Function &F);
892
893   /// \brief Provide access to a name for this pass for debugging purposes.
894   static StringRef name() { return "TargetLibraryAnalysis"; }
895
896 private:
897   static char PassID;
898
899   Optional<TargetLibraryInfoImpl> PresetInfoImpl;
900
901   StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
902
903   TargetLibraryInfoImpl &lookupInfoImpl(Triple T);
904 };
905
906 class TargetLibraryInfoWrapperPass : public ImmutablePass {
907   TargetLibraryInfoImpl TLIImpl;
908   TargetLibraryInfo TLI;
909
910   virtual void anchor();
911
912 public:
913   static char ID;
914   TargetLibraryInfoWrapperPass();
915   explicit TargetLibraryInfoWrapperPass(const Triple &T);
916   explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
917
918   TargetLibraryInfo &getTLI() { return TLI; }
919   const TargetLibraryInfo &getTLI() const { return TLI; }
920 };
921
922 } // end namespace llvm
923
924 #endif