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 /// int __cxa_atexit(void (*f)(void *), void *p, void *d);
23 /// void __cxa_guard_abort(guard_t *guard);
24 /// guard_t is int64_t in Itanium ABI or int32_t on ARM eabi.
26 /// int __cxa_guard_acquire(guard_t *guard);
28 /// void __cxa_guard_release(guard_t *guard);
30 /// void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size);
32 /// double acos(double x);
34 /// float acosf(float x);
36 /// long double acosl(long double x);
38 /// double asin(double x);
40 /// float asinf(float x);
42 /// long double asinl(long double x);
44 /// double atan(double x);
46 /// double atan2(double y, double x);
48 /// float atan2f(float y, float x);
50 /// long double atan2l(long double y, long double x);
52 /// float atanf(float x);
54 /// long double atanl(long double x);
56 /// double ceil(double x);
58 /// float ceilf(float x);
60 /// long double ceill(long double x);
62 /// double copysign(double x, double y);
64 /// float copysignf(float x, float y);
66 /// long double copysignl(long double x, long double y);
68 /// double cos(double x);
70 /// float cosf(float x);
72 /// double cosh(double x);
74 /// float coshf(float x);
76 /// long double coshl(long double x);
78 /// long double cosl(long double x);
80 /// double exp(double x);
82 /// double exp2(double x);
84 /// float exp2f(float x);
86 /// long double exp2l(long double x);
88 /// float expf(float x);
90 /// long double expl(long double x);
92 /// double expm1(double x);
94 /// float expm1f(float x);
96 /// long double expm1l(long double x);
98 /// double fabs(double x);
100 /// float fabsf(float x);
102 /// long double fabsl(long double x);
104 /// int fiprintf(FILE *stream, const char *format, ...);
106 /// double floor(double x);
108 /// float floorf(float x);
110 /// long double floorl(long double x);
112 /// double fmod(double x, double y);
114 /// float fmodf(float x, float y);
116 /// long double fmodl(long double x, long double y);
118 /// int fputc(int c, FILE *stream);
120 /// int fputs(const char *s, FILE *stream);
122 /// size_t fwrite(const void *ptr, size_t size, size_t nitems,
125 /// int iprintf(const char *format, ...);
127 /// double log(double x);
129 /// double log10(double x);
131 /// float log10f(float x);
133 /// long double log10l(long double x);
135 /// double log1p(double x);
137 /// float log1pf(float x);
139 /// long double log1pl(long double x);
141 /// double log2(double x);
143 /// float log2f(float x);
145 /// double long double log2l(long double x);
147 /// float logf(float x);
149 /// long double logl(long double x);
151 /// void *memchr(const void *s, int c, size_t n);
153 /// int memcmp(const void *s1, const void *s2, size_t n);
155 /// void *memcpy(void *s1, const void *s2, size_t n);
157 /// void *memmove(void *s1, const void *s2, size_t n);
159 /// void *memset(void *b, int c, size_t len);
161 /// void memset_pattern16(void *b, const void *pattern16, size_t len);
163 /// double nearbyint(double x);
165 /// float nearbyintf(float x);
167 /// long double nearbyintl(long double x);
169 /// double pow(double x, double y);
171 /// float powf(float x, float y);
173 /// long double powl(long double x, long double y);
175 /// int putchar(int c);
177 /// int puts(const char *s);
179 /// double rint(double x);
181 /// float rintf(float x);
183 /// long double rintl(long double x);
185 /// double round(double x);
187 /// float roundf(float x);
189 /// long double roundl(long double x);
191 /// double sin(double x);
193 /// float sinf(float x);
195 /// double sinh(double x);
197 /// float sinhf(float x);
199 /// long double sinhl(long double x);
201 /// long double sinl(long double x);
203 /// int siprintf(char *str, const char *format, ...);
205 /// double sqrt(double x);
207 /// float sqrtf(float x);
209 /// long double sqrtl(long double x);
211 /// char *strcat(char *s1, const char *s2);
213 /// char *strchr(const char *s, int c);
215 /// char *strcpy(char *s1, const char *s2);
217 /// size_t strlen(const char *s);
219 /// char *strncat(char *s1, const char *s2, size_t n);
221 /// int strncmp(const char *s1, const char *s2, size_t n);
223 /// char *strncpy(char *s1, const char *s2, size_t n);
225 /// size_t strnlen(const char *s, size_t maxlen);
227 /// double tan(double x);
229 /// float tanf(float x);
231 /// double tanh(double x);
233 /// float tanhf(float x);
235 /// long double tanhl(long double x);
237 /// long double tanl(long double x);
239 /// double trunc(double x);
241 /// float truncf(float x);
243 /// long double truncl(long double x);
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.
253 class 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];
259 enum AvailabilityState {
260 StandardName = 3, // (memset to all ones)
262 Unavailable = 0 // (memset to all zeros)
264 void setState(LibFunc::Func F, AvailabilityState State) {
265 AvailableArray[F/4] &= ~(3 << 2*(F&3));
266 AvailableArray[F/4] |= State << 2*(F&3);
268 AvailabilityState getState(LibFunc::Func F) const {
269 return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
275 TargetLibraryInfo(const Triple &T);
276 explicit TargetLibraryInfo(const TargetLibraryInfo &TLI);
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;
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;
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)
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:
313 StringRef getName(LibFunc::Func F) const {
314 AvailabilityState State = getState(F);
315 if (State == Unavailable)
317 if (State == StandardName)
318 return StandardNames[F];
319 assert(State == CustomName);
320 return CustomNames.find(F)->second;
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);
329 void setAvailable(LibFunc::Func F) {
330 setState(F, StandardName);
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());
339 setState(F, StandardName);
343 /// disableAllFunctions - This disables all builtins, which is used for
344 /// options like -fno-builtin.
345 void disableAllFunctions();
348 } // end namespace llvm