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