TargetLibraryInfo.h revision 939f5002027e3ccb710ac1c3050dc25471251565
1//===-- llvm/Target/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_TARGET_TARGETLIBRARYINFO_H
11#define LLVM_TARGET_TARGETLIBRARYINFO_H
12
13#include "llvm/Pass.h"
14#include "llvm/ADT/DenseMap.h"
15
16namespace llvm {
17  class Triple;
18
19  namespace LibFunc {
20    enum Func {
21      /// void operator delete[](void*);
22      ZdaPv,
23      /// void operator delete(void*);
24      ZdlPv,
25      /// void *new[](unsigned int);
26      Znaj,
27      /// void *new[](unsigned int, nothrow);
28      ZnajRKSt9nothrow_t,
29      /// void *new[](unsigned long);
30      Znam,
31      /// void *new[](unsigned long, nothrow);
32      ZnamRKSt9nothrow_t,
33      /// void *new(unsigned int);
34      Znwj,
35      /// void *new(unsigned int, nothrow);
36      ZnwjRKSt9nothrow_t,
37      /// void *new(unsigned long);
38      Znwm,
39      /// void *new(unsigned long, nothrow);
40      ZnwmRKSt9nothrow_t,
41      /// int __cxa_atexit(void (*f)(void *), void *p, void *d);
42      cxa_atexit,
43      /// void __cxa_guard_abort(guard_t *guard);
44      /// guard_t is int64_t in Itanium ABI or int32_t on ARM eabi.
45      cxa_guard_abort,
46      /// int __cxa_guard_acquire(guard_t *guard);
47      cxa_guard_acquire,
48      /// void __cxa_guard_release(guard_t *guard);
49      cxa_guard_release,
50      /// void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size);
51      memcpy_chk,
52      /// double acos(double x);
53      acos,
54      /// float acosf(float x);
55      acosf,
56      /// double acosh(double x);
57      acosh,
58      /// float acoshf(float x);
59      acoshf,
60      /// long double acoshl(long double x);
61      acoshl,
62      /// long double acosl(long double x);
63      acosl,
64      /// double asin(double x);
65      asin,
66      /// float asinf(float x);
67      asinf,
68      /// double asinh(double x);
69      asinh,
70      /// float asinhf(float x);
71      asinhf,
72      /// long double asinhl(long double x);
73      asinhl,
74      /// long double asinl(long double x);
75      asinl,
76      /// double atan(double x);
77      atan,
78      /// double atan2(double y, double x);
79      atan2,
80      /// float atan2f(float y, float x);
81      atan2f,
82      /// long double atan2l(long double y, long double x);
83      atan2l,
84      /// float atanf(float x);
85      atanf,
86      /// double atanh(double x);
87      atanh,
88      /// float atanhf(float x);
89      atanhf,
90      /// long double atanhl(long double x);
91      atanhl,
92      /// long double atanl(long double x);
93      atanl,
94      /// void *calloc(size_t count, size_t size);
95      calloc,
96      /// double cbrt(double x);
97      cbrt,
98      /// float cbrtf(float x);
99      cbrtf,
100      /// long double cbrtl(long double x);
101      cbrtl,
102      /// double ceil(double x);
103      ceil,
104      /// float ceilf(float x);
105      ceilf,
106      /// long double ceill(long double x);
107      ceill,
108      /// double copysign(double x, double y);
109      copysign,
110      /// float copysignf(float x, float y);
111      copysignf,
112      /// long double copysignl(long double x, long double y);
113      copysignl,
114      /// double cos(double x);
115      cos,
116      /// float cosf(float x);
117      cosf,
118      /// double cosh(double x);
119      cosh,
120      /// float coshf(float x);
121      coshf,
122      /// long double coshl(long double x);
123      coshl,
124      /// long double cosl(long double x);
125      cosl,
126      /// double exp(double x);
127      exp,
128      /// double exp10(double x);
129      exp10,
130      /// float exp10f(float x);
131      exp10f,
132      /// long double exp10l(long double x);
133      exp10l,
134      /// double exp2(double x);
135      exp2,
136      /// float exp2f(float x);
137      exp2f,
138      /// long double exp2l(long double x);
139      exp2l,
140      /// float expf(float x);
141      expf,
142      /// long double expl(long double x);
143      expl,
144      /// double expm1(double x);
145      expm1,
146      /// float expm1f(float x);
147      expm1f,
148      /// long double expm1l(long double x);
149      expm1l,
150      /// double fabs(double x);
151      fabs,
152      /// float fabsf(float x);
153      fabsf,
154      /// long double fabsl(long double x);
155      fabsl,
156      /// int fiprintf(FILE *stream, const char *format, ...);
157      fiprintf,
158      /// double floor(double x);
159      floor,
160      /// float floorf(float x);
161      floorf,
162      /// long double floorl(long double x);
163      floorl,
164      /// double fmod(double x, double y);
165      fmod,
166      /// float fmodf(float x, float y);
167      fmodf,
168      /// long double fmodl(long double x, long double y);
169      fmodl,
170      /// int fputc(int c, FILE *stream);
171      fputc,
172      /// int fputs(const char *s, FILE *stream);
173      fputs,
174      /// void free(void *ptr);
175      free,
176      /// size_t fwrite(const void *ptr, size_t size, size_t nitems,
177      /// FILE *stream);
178      fwrite,
179      /// int iprintf(const char *format, ...);
180      iprintf,
181      /// double log(double x);
182      log,
183      /// double log10(double x);
184      log10,
185      /// float log10f(float x);
186      log10f,
187      /// long double log10l(long double x);
188      log10l,
189      /// double log1p(double x);
190      log1p,
191      /// float log1pf(float x);
192      log1pf,
193      /// long double log1pl(long double x);
194      log1pl,
195      /// double log2(double x);
196      log2,
197      /// float log2f(float x);
198      log2f,
199      /// double long double log2l(long double x);
200      log2l,
201      /// double logb(double x);
202      logb,
203      /// float logbf(float x);
204      logbf,
205      /// long double logbl(long double x);
206      logbl,
207      /// float logf(float x);
208      logf,
209      /// long double logl(long double x);
210      logl,
211      /// void *malloc(size_t size);
212      malloc,
213      /// void *memchr(const void *s, int c, size_t n);
214      memchr,
215      /// int memcmp(const void *s1, const void *s2, size_t n);
216      memcmp,
217      /// void *memcpy(void *s1, const void *s2, size_t n);
218      memcpy,
219      /// void *memmove(void *s1, const void *s2, size_t n);
220      memmove,
221      /// void *memset(void *b, int c, size_t len);
222      memset,
223      /// void memset_pattern16(void *b, const void *pattern16, size_t len);
224      memset_pattern16,
225      /// double nearbyint(double x);
226      nearbyint,
227      /// float nearbyintf(float x);
228      nearbyintf,
229      /// long double nearbyintl(long double x);
230      nearbyintl,
231      /// int posix_memalign(void **memptr, size_t alignment, size_t size);
232      posix_memalign,
233      /// double pow(double x, double y);
234      pow,
235      /// float powf(float x, float y);
236      powf,
237      /// long double powl(long double x, long double y);
238      powl,
239      /// int putchar(int c);
240      putchar,
241      /// int puts(const char *s);
242      puts,
243      /// void *realloc(void *ptr, size_t size);
244      realloc,
245      /// void *reallocf(void *ptr, size_t size);
246      reallocf,
247      /// double rint(double x);
248      rint,
249      /// float rintf(float x);
250      rintf,
251      /// long double rintl(long double x);
252      rintl,
253      /// double round(double x);
254      round,
255      /// float roundf(float x);
256      roundf,
257      /// long double roundl(long double x);
258      roundl,
259      /// double sin(double x);
260      sin,
261      /// float sinf(float x);
262      sinf,
263      /// double sinh(double x);
264      sinh,
265      /// float sinhf(float x);
266      sinhf,
267      /// long double sinhl(long double x);
268      sinhl,
269      /// long double sinl(long double x);
270      sinl,
271      /// int siprintf(char *str, const char *format, ...);
272      siprintf,
273      /// double sqrt(double x);
274      sqrt,
275      /// float sqrtf(float x);
276      sqrtf,
277      /// long double sqrtl(long double x);
278      sqrtl,
279      /// char *stpcpy(char *s1, const char *s2);
280      stpcpy,
281      /// char *strcat(char *s1, const char *s2);
282      strcat,
283      /// char *strchr(const char *s, int c);
284      strchr,
285      /// int strcmp(const char *s1, const char *s2);
286      strcmp,
287      /// char *strcpy(char *s1, const char *s2);
288      strcpy,
289      /// size_t strcspn(const char *s1, const char *s2);
290      strcspn,
291      /// char *strdup(const char *s1);
292      strdup,
293      /// size_t strlen(const char *s);
294      strlen,
295      /// char *strncat(char *s1, const char *s2, size_t n);
296      strncat,
297      /// int strncmp(const char *s1, const char *s2, size_t n);
298      strncmp,
299      /// char *strncpy(char *s1, const char *s2, size_t n);
300      strncpy,
301      /// char *strndup(const char *s1, size_t n);
302      strndup,
303      /// size_t strnlen(const char *s, size_t maxlen);
304      strnlen,
305      /// char *strpbrk(const char *s1, const char *s2);
306      strpbrk,
307      /// char *strrchr(const char *s, int c);
308      strrchr,
309      /// size_t strspn(const char *s1, const char *s2);
310      strspn,
311      /// char *strstr(const char *s1, const char *s2);
312      strstr,
313      /// double strtod(const char *nptr, char **endptr);
314      strtod,
315      /// float strtof(const char *nptr, char **endptr);
316      strtof,
317      /// long int strtol(const char *nptr, char **endptr, int base);
318      strtol,
319      /// long double strtold(const char *nptr, char **endptr);
320      strtold,
321      /// long long int strtoll(const char *nptr, char **endptr, int base);
322      strtoll,
323      /// unsigned long int strtoul(const char *nptr, char **endptr, int base);
324      strtoul,
325      /// unsigned long long int strtoull(const char *nptr, char **endptr,
326      ///                                 int base);
327      strtoull,
328      /// double tan(double x);
329      tan,
330      /// float tanf(float x);
331      tanf,
332      /// double tanh(double x);
333      tanh,
334      /// float tanhf(float x);
335      tanhf,
336      /// long double tanhl(long double x);
337      tanhl,
338      /// long double tanl(long double x);
339      tanl,
340      /// double trunc(double x);
341      trunc,
342      /// float truncf(float x);
343      truncf,
344      /// long double truncl(long double x);
345      truncl,
346      /// void *valloc(size_t size);
347      valloc,
348
349      NumLibFuncs
350    };
351  }
352
353/// TargetLibraryInfo - This immutable pass captures information about what
354/// library functions are available for the current target, and allows a
355/// frontend to disable optimizations through -fno-builtin etc.
356class TargetLibraryInfo : public ImmutablePass {
357  virtual void anchor();
358  unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
359  llvm::DenseMap<unsigned, std::string> CustomNames;
360  static const char* StandardNames[LibFunc::NumLibFuncs];
361
362  enum AvailabilityState {
363    StandardName = 3, // (memset to all ones)
364    CustomName = 1,
365    Unavailable = 0  // (memset to all zeros)
366  };
367  void setState(LibFunc::Func F, AvailabilityState State) {
368    AvailableArray[F/4] &= ~(3 << 2*(F&3));
369    AvailableArray[F/4] |= State << 2*(F&3);
370  }
371  AvailabilityState getState(LibFunc::Func F) const {
372    return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
373  }
374
375public:
376  static char ID;
377  TargetLibraryInfo();
378  TargetLibraryInfo(const Triple &T);
379  explicit TargetLibraryInfo(const TargetLibraryInfo &TLI);
380
381  /// getLibFunc - Search for a particular function name.  If it is one of the
382  /// known library functions, return true and set F to the corresponding value.
383  bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
384
385  /// has - This function is used by optimizations that want to match on or form
386  /// a given library function.
387  bool has(LibFunc::Func F) const {
388    return getState(F) != Unavailable;
389  }
390
391  /// hasOptimizedCodeGen - Return true if the function is both available as
392  /// a builtin and a candidate for optimized code generation.
393  bool hasOptimizedCodeGen(LibFunc::Func F) const {
394    if (getState(F) == Unavailable)
395      return false;
396    switch (F) {
397    default: break;
398    case LibFunc::copysign:  case LibFunc::copysignf:  case LibFunc::copysignl:
399    case LibFunc::fabs:      case LibFunc::fabsf:      case LibFunc::fabsl:
400    case LibFunc::sin:       case LibFunc::sinf:       case LibFunc::sinl:
401    case LibFunc::cos:       case LibFunc::cosf:       case LibFunc::cosl:
402    case LibFunc::sqrt:      case LibFunc::sqrtf:      case LibFunc::sqrtl:
403    case LibFunc::floor:     case LibFunc::floorf:     case LibFunc::floorl:
404    case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
405    case LibFunc::ceil:      case LibFunc::ceilf:      case LibFunc::ceill:
406    case LibFunc::rint:      case LibFunc::rintf:      case LibFunc::rintl:
407    case LibFunc::trunc:     case LibFunc::truncf:     case LibFunc::truncl:
408    case LibFunc::log2:      case LibFunc::log2f:      case LibFunc::log2l:
409    case LibFunc::exp2:      case LibFunc::exp2f:      case LibFunc::exp2l:
410    case LibFunc::memcmp:
411      return true;
412    }
413    return false;
414  }
415
416  StringRef getName(LibFunc::Func F) const {
417    AvailabilityState State = getState(F);
418    if (State == Unavailable)
419      return StringRef();
420    if (State == StandardName)
421      return StandardNames[F];
422    assert(State == CustomName);
423    return CustomNames.find(F)->second;
424  }
425
426  /// setUnavailable - this can be used by whatever sets up TargetLibraryInfo to
427  /// ban use of specific library functions.
428  void setUnavailable(LibFunc::Func F) {
429    setState(F, Unavailable);
430  }
431
432  void setAvailable(LibFunc::Func F) {
433    setState(F, StandardName);
434  }
435
436  void setAvailableWithName(LibFunc::Func F, StringRef Name) {
437    if (StandardNames[F] != Name) {
438      setState(F, CustomName);
439      CustomNames[F] = Name;
440      assert(CustomNames.find(F) != CustomNames.end());
441    } else {
442      setState(F, StandardName);
443    }
444  }
445
446  /// disableAllFunctions - This disables all builtins, which is used for
447  /// options like -fno-builtin.
448  void disableAllFunctions();
449};
450
451} // end namespace llvm
452
453#endif
454