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/Pass.h"
14 #include "llvm/ADT/DenseMap.h"
21 /// void operator delete[](void*);
23 /// void operator delete(void*);
25 /// void *new[](unsigned int);
27 /// void *new[](unsigned int, nothrow);
29 /// void *new[](unsigned long);
31 /// void *new[](unsigned long, nothrow);
33 /// void *new(unsigned int);
35 /// void *new(unsigned int, nothrow);
37 /// void *new(unsigned long);
39 /// void *new(unsigned long, nothrow);
41 /// int __cxa_atexit(void (*f)(void *), void *p, void *d);
43 /// void __cxa_guard_abort(guard_t *guard);
44 /// guard_t is int64_t in Itanium ABI or int32_t on ARM eabi.
46 /// int __cxa_guard_acquire(guard_t *guard);
48 /// void __cxa_guard_release(guard_t *guard);
50 /// void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size);
54 /// double acos(double x);
56 /// float acosf(float x);
58 /// double acosh(double x);
60 /// float acoshf(float x);
62 /// long double acoshl(long double x);
64 /// long double acosl(long double x);
66 /// double asin(double x);
68 /// float asinf(float x);
70 /// double asinh(double x);
72 /// float asinhf(float x);
74 /// long double asinhl(long double x);
76 /// long double asinl(long double x);
78 /// double atan(double x);
80 /// double atan2(double y, double x);
82 /// float atan2f(float y, float x);
84 /// long double atan2l(long double y, long double x);
86 /// float atanf(float x);
88 /// double atanh(double x);
90 /// float atanhf(float x);
92 /// long double atanhl(long double x);
94 /// long double atanl(long double x);
96 /// void *calloc(size_t count, size_t size);
98 /// double cbrt(double x);
100 /// float cbrtf(float x);
102 /// long double cbrtl(long double x);
104 /// double ceil(double x);
106 /// float ceilf(float x);
108 /// long double ceill(long double x);
110 /// double copysign(double x, double y);
112 /// float copysignf(float x, float y);
114 /// long double copysignl(long double x, long double y);
116 /// double cos(double x);
118 /// float cosf(float x);
120 /// double cosh(double x);
122 /// float coshf(float x);
124 /// long double coshl(long double x);
126 /// long double cosl(long double x);
128 /// double exp(double x);
130 /// double exp10(double x);
132 /// float exp10f(float x);
134 /// long double exp10l(long double x);
136 /// double exp2(double x);
138 /// float exp2f(float x);
140 /// long double exp2l(long double x);
142 /// float expf(float x);
144 /// long double expl(long double x);
146 /// double expm1(double x);
148 /// float expm1f(float x);
150 /// long double expm1l(long double x);
152 /// double fabs(double x);
154 /// float fabsf(float x);
156 /// long double fabsl(long double x);
160 /// int ffsl(long int i);
162 /// int ffsll(long long int i);
164 /// int fiprintf(FILE *stream, const char *format, ...);
166 /// double floor(double x);
168 /// float floorf(float x);
170 /// long double floorl(long double x);
172 /// double fmod(double x, double y);
174 /// float fmodf(float x, float y);
176 /// long double fmodl(long double x, long double y);
178 /// int fprintf(FILE *stream, const char *format, ...);
180 /// int fputc(int c, FILE *stream);
182 /// int fputs(const char *s, FILE *stream);
184 /// void free(void *ptr);
186 /// size_t fwrite(const void *ptr, size_t size, size_t nitems,
189 /// int iprintf(const char *format, ...);
191 /// int isascii(int c);
193 /// int isdigit(int c);
195 /// long int labs(long int j);
197 /// long long int llabs(long long int j);
199 /// double log(double x);
201 /// double log10(double x);
203 /// float log10f(float x);
205 /// long double log10l(long double x);
207 /// double log1p(double x);
209 /// float log1pf(float x);
211 /// long double log1pl(long double x);
213 /// double log2(double x);
215 /// float log2f(float x);
217 /// double long double log2l(long double x);
219 /// double logb(double x);
221 /// float logbf(float x);
223 /// long double logbl(long double x);
225 /// float logf(float x);
227 /// long double logl(long double x);
229 /// void *malloc(size_t size);
231 /// void *memchr(const void *s, int c, size_t n);
233 /// int memcmp(const void *s1, const void *s2, size_t n);
235 /// void *memcpy(void *s1, const void *s2, size_t n);
237 /// void *memmove(void *s1, const void *s2, size_t n);
239 /// void *memset(void *b, int c, size_t len);
241 /// void memset_pattern16(void *b, const void *pattern16, size_t len);
243 /// double nearbyint(double x);
245 /// float nearbyintf(float x);
247 /// long double nearbyintl(long double x);
249 /// int posix_memalign(void **memptr, size_t alignment, size_t size);
251 /// double pow(double x, double y);
253 /// float powf(float x, float y);
255 /// long double powl(long double x, long double y);
257 /// int printf(const char *format, ...);
259 /// int putchar(int c);
261 /// int puts(const char *s);
263 /// void *realloc(void *ptr, size_t size);
265 /// void *reallocf(void *ptr, size_t size);
267 /// double rint(double x);
269 /// float rintf(float x);
271 /// long double rintl(long double x);
273 /// double round(double x);
275 /// float roundf(float x);
277 /// long double roundl(long double x);
279 /// double sin(double x);
281 /// float sinf(float x);
283 /// double sinh(double x);
285 /// float sinhf(float x);
287 /// long double sinhl(long double x);
289 /// long double sinl(long double x);
291 /// int siprintf(char *str, const char *format, ...);
293 /// int sprintf(char *str, const char *format, ...);
295 /// double sqrt(double x);
297 /// float sqrtf(float x);
299 /// long double sqrtl(long double x);
301 /// char *stpcpy(char *s1, const char *s2);
303 /// char *strcat(char *s1, const char *s2);
305 /// char *strchr(const char *s, int c);
307 /// int strcmp(const char *s1, const char *s2);
309 /// char *strcpy(char *s1, const char *s2);
311 /// size_t strcspn(const char *s1, const char *s2);
313 /// char *strdup(const char *s1);
315 /// size_t strlen(const char *s);
317 /// char *strncat(char *s1, const char *s2, size_t n);
319 /// int strncmp(const char *s1, const char *s2, size_t n);
321 /// char *strncpy(char *s1, const char *s2, size_t n);
323 /// char *strndup(const char *s1, size_t n);
325 /// size_t strnlen(const char *s, size_t maxlen);
327 /// char *strpbrk(const char *s1, const char *s2);
329 /// char *strrchr(const char *s, int c);
331 /// size_t strspn(const char *s1, const char *s2);
333 /// char *strstr(const char *s1, const char *s2);
335 /// double strtod(const char *nptr, char **endptr);
337 /// float strtof(const char *nptr, char **endptr);
339 /// long int strtol(const char *nptr, char **endptr, int base);
341 /// long double strtold(const char *nptr, char **endptr);
343 /// long long int strtoll(const char *nptr, char **endptr, int base);
345 /// unsigned long int strtoul(const char *nptr, char **endptr, int base);
347 /// unsigned long long int strtoull(const char *nptr, char **endptr,
350 /// double tan(double x);
352 /// float tanf(float x);
354 /// double tanh(double x);
356 /// float tanhf(float x);
358 /// long double tanhl(long double x);
360 /// long double tanl(long double x);
362 /// int toascii(int c);
364 /// double trunc(double x);
366 /// float truncf(float x);
368 /// long double truncl(long double x);
370 /// void *valloc(size_t size);
377 /// TargetLibraryInfo - This immutable pass captures information about what
378 /// library functions are available for the current target, and allows a
379 /// frontend to disable optimizations through -fno-builtin etc.
380 class TargetLibraryInfo : public ImmutablePass {
381 virtual void anchor();
382 unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
383 llvm::DenseMap<unsigned, std::string> CustomNames;
384 static const char* StandardNames[LibFunc::NumLibFuncs];
386 enum AvailabilityState {
387 StandardName = 3, // (memset to all ones)
389 Unavailable = 0 // (memset to all zeros)
391 void setState(LibFunc::Func F, AvailabilityState State) {
392 AvailableArray[F/4] &= ~(3 << 2*(F&3));
393 AvailableArray[F/4] |= State << 2*(F&3);
395 AvailabilityState getState(LibFunc::Func F) const {
396 return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
402 TargetLibraryInfo(const Triple &T);
403 explicit TargetLibraryInfo(const TargetLibraryInfo &TLI);
405 /// getLibFunc - Search for a particular function name. If it is one of the
406 /// known library functions, return true and set F to the corresponding value.
407 bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
409 /// has - This function is used by optimizations that want to match on or form
410 /// a given library function.
411 bool has(LibFunc::Func F) const {
412 return getState(F) != Unavailable;
415 /// hasOptimizedCodeGen - Return true if the function is both available as
416 /// a builtin and a candidate for optimized code generation.
417 bool hasOptimizedCodeGen(LibFunc::Func F) const {
418 if (getState(F) == Unavailable)
422 case LibFunc::copysign: case LibFunc::copysignf: case LibFunc::copysignl:
423 case LibFunc::fabs: case LibFunc::fabsf: case LibFunc::fabsl:
424 case LibFunc::sin: case LibFunc::sinf: case LibFunc::sinl:
425 case LibFunc::cos: case LibFunc::cosf: case LibFunc::cosl:
426 case LibFunc::sqrt: case LibFunc::sqrtf: case LibFunc::sqrtl:
427 case LibFunc::floor: case LibFunc::floorf: case LibFunc::floorl:
428 case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
429 case LibFunc::ceil: case LibFunc::ceilf: case LibFunc::ceill:
430 case LibFunc::rint: case LibFunc::rintf: case LibFunc::rintl:
431 case LibFunc::trunc: case LibFunc::truncf: case LibFunc::truncl:
432 case LibFunc::log2: case LibFunc::log2f: case LibFunc::log2l:
433 case LibFunc::exp2: case LibFunc::exp2f: case LibFunc::exp2l:
434 case LibFunc::memcmp:
440 StringRef getName(LibFunc::Func F) const {
441 AvailabilityState State = getState(F);
442 if (State == Unavailable)
444 if (State == StandardName)
445 return StandardNames[F];
446 assert(State == CustomName);
447 return CustomNames.find(F)->second;
450 /// setUnavailable - this can be used by whatever sets up TargetLibraryInfo to
451 /// ban use of specific library functions.
452 void setUnavailable(LibFunc::Func F) {
453 setState(F, Unavailable);
456 void setAvailable(LibFunc::Func F) {
457 setState(F, StandardName);
460 void setAvailableWithName(LibFunc::Func F, StringRef Name) {
461 if (StandardNames[F] != Name) {
462 setState(F, CustomName);
463 CustomNames[F] = Name;
464 assert(CustomNames.find(F) != CustomNames.end());
466 setState(F, StandardName);
470 /// disableAllFunctions - This disables all builtins, which is used for
471 /// options like -fno-builtin.
472 void disableAllFunctions();
475 } // end namespace llvm