TargetLibraryInfo.h revision 982dc84762fc0c2ca35e6947d648a690dd22343c
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      /// int __cxa_atexit(void (*f)(void *), void *p, void *d);
22      cxa_atexit,
23      /// void __cxa_guard_abort(guard_t *guard);
24      /// guard_t is int64_t in Itanium ABI or int32_t on ARM eabi.
25      cxa_guard_abort,
26      /// int __cxa_guard_acquire(guard_t *guard);
27      cxa_guard_acquire,
28      /// void __cxa_guard_release(guard_t *guard);
29      cxa_guard_release,
30      /// void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size);
31      memcpy_chk,
32      /// double acos(double x);
33      acos,
34      /// float acosf(float x);
35      acosf,
36      /// long double acosl(long double x);
37      acosl,
38      /// double asin(double x);
39      asin,
40      /// float asinf(float x);
41      asinf,
42      /// long double asinl(long double x);
43      asinl,
44      /// double atan(double x);
45      atan,
46      /// double atan2(double y, double x);
47      atan2,
48      /// float atan2f(float y, float x);
49      atan2f,
50      /// long double atan2l(long double y, long double x);
51      atan2l,
52      /// float atanf(float x);
53      atanf,
54      /// long double atanl(long double x);
55      atanl,
56      /// double ceil(double x);
57      ceil,
58      /// float ceilf(float x);
59      ceilf,
60      /// long double ceill(long double x);
61      ceill,
62      /// double copysign(double x, double y);
63      copysign,
64      /// float copysignf(float x, float y);
65      copysignf,
66      /// long double copysignl(long double x, long double y);
67      copysignl,
68      /// double cos(double x);
69      cos,
70      /// float cosf(float x);
71      cosf,
72      /// double cosh(double x);
73      cosh,
74      /// float coshf(float x);
75      coshf,
76      /// long double coshl(long double x);
77      coshl,
78      /// long double cosl(long double x);
79      cosl,
80      /// double exp(double x);
81      exp,
82      /// double exp2(double x);
83      exp2,
84      /// float exp2f(float x);
85      exp2f,
86      /// long double exp2l(long double x);
87      exp2l,
88      /// float expf(float x);
89      expf,
90      /// long double expl(long double x);
91      expl,
92      /// double expm1(double x);
93      expm1,
94      /// float expm1f(float x);
95      expm1f,
96      /// long double expm1l(long double x);
97      expm1l,
98      /// double fabs(double x);
99      fabs,
100      /// float fabsf(float x);
101      fabsf,
102      /// long double fabsl(long double x);
103      fabsl,
104      /// int fiprintf(FILE *stream, const char *format, ...);
105      fiprintf,
106      /// double floor(double x);
107      floor,
108      /// float floorf(float x);
109      floorf,
110      /// long double floorl(long double x);
111      floorl,
112      /// double fmod(double x, double y);
113      fmod,
114      /// float fmodf(float x, float y);
115      fmodf,
116      /// long double fmodl(long double x, long double y);
117      fmodl,
118      /// int fputc(int c, FILE *stream);
119      fputc,
120      /// int fputs(const char *s, FILE *stream);
121      fputs,
122      /// size_t fwrite(const void *ptr, size_t size, size_t nitems,
123      /// FILE *stream);
124      fwrite,
125      /// int iprintf(const char *format, ...);
126      iprintf,
127      /// double log(double x);
128      log,
129      /// double log10(double x);
130      log10,
131      /// float log10f(float x);
132      log10f,
133      /// long double log10l(long double x);
134      log10l,
135      /// double log1p(double x);
136      log1p,
137      /// float log1pf(float x);
138      log1pf,
139      /// long double log1pl(long double x);
140      log1pl,
141      /// double log2(double x);
142      log2,
143      /// float log2f(float x);
144      log2f,
145      /// double long double log2l(long double x);
146      log2l,
147      /// float logf(float x);
148      logf,
149      /// long double logl(long double x);
150      logl,
151      /// void *memchr(const void *s, int c, size_t n);
152      memchr,
153      /// int memcmp(const void *s1, const void *s2, size_t n);
154      memcmp,
155      /// void *memcpy(void *s1, const void *s2, size_t n);
156      memcpy,
157      /// void *memmove(void *s1, const void *s2, size_t n);
158      memmove,
159      /// void *memset(void *b, int c, size_t len);
160      memset,
161      /// void memset_pattern16(void *b, const void *pattern16, size_t len);
162      memset_pattern16,
163      /// double nearbyint(double x);
164      nearbyint,
165      /// float nearbyintf(float x);
166      nearbyintf,
167      /// long double nearbyintl(long double x);
168      nearbyintl,
169      /// double pow(double x, double y);
170      pow,
171      /// float powf(float x, float y);
172      powf,
173      /// long double powl(long double x, long double y);
174      powl,
175      /// int putchar(int c);
176      putchar,
177      /// int puts(const char *s);
178      puts,
179      /// double rint(double x);
180      rint,
181      /// float rintf(float x);
182      rintf,
183      /// long double rintl(long double x);
184      rintl,
185      /// double round(double x);
186      round,
187      /// float roundf(float x);
188      roundf,
189      /// long double roundl(long double x);
190      roundl,
191      /// double sin(double x);
192      sin,
193      /// float sinf(float x);
194      sinf,
195      /// double sinh(double x);
196      sinh,
197      /// float sinhf(float x);
198      sinhf,
199      /// long double sinhl(long double x);
200      sinhl,
201      /// long double sinl(long double x);
202      sinl,
203      /// int siprintf(char *str, const char *format, ...);
204      siprintf,
205      /// double sqrt(double x);
206      sqrt,
207      /// float sqrtf(float x);
208      sqrtf,
209      /// long double sqrtl(long double x);
210      sqrtl,
211      /// char *strcat(char *s1, const char *s2);
212      strcat,
213      /// char *strchr(const char *s, int c);
214      strchr,
215      /// char *strcpy(char *s1, const char *s2);
216      strcpy,
217      /// size_t strlen(const char *s);
218      strlen,
219      /// char *strncat(char *s1, const char *s2, size_t n);
220      strncat,
221      /// int strncmp(const char *s1, const char *s2, size_t n);
222      strncmp,
223      /// char *strncpy(char *s1, const char *s2, size_t n);
224      strncpy,
225      /// size_t strnlen(const char *s, size_t maxlen);
226      strnlen,
227      /// double tan(double x);
228      tan,
229      /// float tanf(float x);
230      tanf,
231      /// double tanh(double x);
232      tanh,
233      /// float tanhf(float x);
234      tanhf,
235      /// long double tanhl(long double x);
236      tanhl,
237      /// long double tanl(long double x);
238      tanl,
239      /// double trunc(double x);
240      trunc,
241      /// float truncf(float x);
242      truncf,
243      /// long double truncl(long double x);
244      truncl,
245
246      NumLibFuncs
247    };
248  }
249
250/// TargetLibraryInfo - This immutable pass captures information about what
251/// library functions are available for the current target, and allows a
252/// frontend to disable optimizations through -fno-builtin etc.
253class TargetLibraryInfo : public ImmutablePass {
254  virtual void anchor();
255  unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
256  llvm::DenseMap<unsigned, std::string> CustomNames;
257  static const char* StandardNames[LibFunc::NumLibFuncs];
258
259  enum AvailabilityState {
260    StandardName = 3, // (memset to all ones)
261    CustomName = 1,
262    Unavailable = 0  // (memset to all zeros)
263  };
264  void setState(LibFunc::Func F, AvailabilityState State) {
265    AvailableArray[F/4] &= ~(3 << 2*(F&3));
266    AvailableArray[F/4] |= State << 2*(F&3);
267  }
268  AvailabilityState getState(LibFunc::Func F) const {
269    return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
270  }
271
272public:
273  static char ID;
274  TargetLibraryInfo();
275  TargetLibraryInfo(const Triple &T);
276  explicit TargetLibraryInfo(const TargetLibraryInfo &TLI);
277
278  /// getLibFunc - Search for a particular function name.  If it is one of the
279  /// known library functions, return true and set F to the corresponding value.
280  bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
281
282  /// has - This function is used by optimizations that want to match on or form
283  /// a given library function.
284  bool has(LibFunc::Func F) const {
285    return getState(F) != Unavailable;
286  }
287
288  /// hasOptimizedCodeGen - Return true if the function is both available as
289  /// a builtin and a candidate for optimized code generation.
290  bool hasOptimizedCodeGen(LibFunc::Func F) const {
291    if (getState(F) == Unavailable)
292      return false;
293    switch (F) {
294    default: break;
295    case LibFunc::copysign:  case LibFunc::copysignf:  case LibFunc::copysignl:
296    case LibFunc::fabs:      case LibFunc::fabsf:      case LibFunc::fabsl:
297    case LibFunc::sin:       case LibFunc::sinf:       case LibFunc::sinl:
298    case LibFunc::cos:       case LibFunc::cosf:       case LibFunc::cosl:
299    case LibFunc::sqrt:      case LibFunc::sqrtf:      case LibFunc::sqrtl:
300    case LibFunc::floor:     case LibFunc::floorf:     case LibFunc::floorl:
301    case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
302    case LibFunc::ceil:      case LibFunc::ceilf:      case LibFunc::ceill:
303    case LibFunc::rint:      case LibFunc::rintf:      case LibFunc::rintl:
304    case LibFunc::trunc:     case LibFunc::truncf:     case LibFunc::truncl:
305    case LibFunc::log2:      case LibFunc::log2f:      case LibFunc::log2l:
306    case LibFunc::exp2:      case LibFunc::exp2f:      case LibFunc::exp2l:
307    case LibFunc::memcmp:
308      return true;
309    }
310    return false;
311  }
312
313  StringRef getName(LibFunc::Func F) const {
314    AvailabilityState State = getState(F);
315    if (State == Unavailable)
316      return StringRef();
317    if (State == StandardName)
318      return StandardNames[F];
319    assert(State == CustomName);
320    return CustomNames.find(F)->second;
321  }
322
323  /// setUnavailable - this can be used by whatever sets up TargetLibraryInfo to
324  /// ban use of specific library functions.
325  void setUnavailable(LibFunc::Func F) {
326    setState(F, Unavailable);
327  }
328
329  void setAvailable(LibFunc::Func F) {
330    setState(F, StandardName);
331  }
332
333  void setAvailableWithName(LibFunc::Func F, StringRef Name) {
334    if (StandardNames[F] != Name) {
335      setState(F, CustomName);
336      CustomNames[F] = Name;
337      assert(CustomNames.find(F) != CustomNames.end());
338    } else {
339      setState(F, StandardName);
340    }
341  }
342
343  /// disableAllFunctions - This disables all builtins, which is used for
344  /// options like -fno-builtin.
345  void disableAllFunctions();
346};
347
348} // end namespace llvm
349
350#endif
351