TargetLibraryInfo.h revision 8ff4115ef0bcad0a46750bb2bd4376a61b346362
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      /// double acos(double x);
22      acos,
23      /// long double acosl(long double x);
24      acosl,
25      /// float acosf(float x);
26      acosf,
27      /// double asin(double x);
28      asin,
29      /// long double asinl(long double x);
30      asinl,
31      /// float asinf(float x);
32      asinf,
33      /// double atan(double x);
34      atan,
35      /// long double atanl(long double x);
36      atanl,
37      /// float atanf(float x);
38      atanf,
39      /// double ceil(double x);
40      ceil,
41      /// long double ceill(long double x);
42      ceill,
43      /// float ceilf(float x);
44      ceilf,
45      /// double cos(double x);
46      cos,
47      /// long double cosl(long double x);
48      cosl,
49      /// float cosf(float x);
50      cosf,
51      /// double cosh(double x);
52      cosh,
53      /// long double coshl(long double x);
54      coshl,
55      /// float coshf(float x);
56      coshf,
57      /// double exp(double x);
58      exp,
59      /// long double expl(long double x);
60      expl,
61      /// float expf(float x);
62      expf,
63      /// double exp2(double x);
64      exp2,
65      /// long double exp2l(long double x);
66      exp2l,
67      /// float exp2f(float x);
68      exp2f,
69      /// double expm1(double x);
70      expm1,
71      /// long double expm1l(long double x);
72      expm1l,
73      /// float expm1f(float x);
74      expl1f,
75      /// double fabs(double x);
76      fabs,
77      /// long double fabsl(long double x);
78      fabsl,
79      /// float fabsf(float x);
80      fabsf,
81      /// double floor(double x);
82      floor,
83      /// long double floorl(long double x);
84      floorl,
85      /// float floorf(float x);
86      floorf,
87      /// int fiprintf(FILE *stream, const char *format, ...);
88      fiprintf,
89      /// int fputs(const char *s, FILE *stream);
90      fputs,
91      /// size_t fwrite(const void *ptr, size_t size, size_t nitems,
92      /// FILE *stream);
93      fwrite,
94      /// int iprintf(const char *format, ...);
95      iprintf,
96      /// double log(double x);
97      log,
98      /// long double logl(long double x);
99      logl,
100      /// float logf(float x);
101      logf,
102      /// double log2(double x);
103      log2,
104      /// double long double log2l(long double x);
105      log2l,
106      /// float log2f(float x);
107      log2f,
108      /// double log10(double x);
109      log10,
110      /// long double log10l(long double x);
111      log10l,
112      /// float log10f(float x);
113      log10f,
114      /// double log1p(double x);
115      log1p,
116      /// long double log1pl(long double x);
117      log1pl,
118      /// float log1pf(float x);
119      log1pf,
120      /// void *memcpy(void *s1, const void *s2, size_t n);
121      memcpy,
122      /// void *memmove(void *s1, const void *s2, size_t n);
123      memmove,
124      /// void *memset(void *b, int c, size_t len);
125      memset,
126      /// void memset_pattern16(void *b, const void *pattern16, size_t len);
127      memset_pattern16,
128      /// double pow(double x, double y);
129      pow,
130      /// float powf(float x, float y);
131      powf,
132      /// long double powl(long double x, long double y);
133      powl,
134      /// int siprintf(char *str, const char *format, ...);
135      siprintf,
136      /// double sqrt(double x);
137      sqrt,
138      /// long double sqrtl(long double x);
139      sqrtl,
140      /// float sqrtf(float x);
141      sqrtf,
142
143      NumLibFuncs
144    };
145  }
146
147/// TargetLibraryInfo - This immutable pass captures information about what
148/// library functions are available for the current target, and allows a
149/// frontend to disable optimizations through -fno-builtin etc.
150class TargetLibraryInfo : public ImmutablePass {
151  unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
152  llvm::DenseMap<unsigned, std::string> CustomNames;
153  static const char* StandardNames[LibFunc::NumLibFuncs];
154
155  enum AvailabilityState {
156    StandardName = 3, // (memset to all ones)
157    CustomName = 1,
158    Unavailable = 0  // (memset to all zeros)
159  };
160  void setState(LibFunc::Func F, AvailabilityState State) {
161    AvailableArray[F/4] &= ~(3 << 2*(F&3));
162    AvailableArray[F/4] |= State << 2*(F&3);
163  }
164  AvailabilityState getState(LibFunc::Func F) const {
165    return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
166  }
167
168public:
169  static char ID;
170  TargetLibraryInfo();
171  TargetLibraryInfo(const Triple &T);
172  explicit TargetLibraryInfo(const TargetLibraryInfo &TLI);
173
174  /// has - This function is used by optimizations that want to match on or form
175  /// a given library function.
176  bool has(LibFunc::Func F) const {
177    return getState(F) != Unavailable;
178  }
179
180  StringRef getName(LibFunc::Func F) const {
181    AvailabilityState State = getState(F);
182    if (State == Unavailable)
183      return StringRef();
184    if (State == StandardName)
185      return StandardNames[F];
186    assert(State == CustomName);
187    return CustomNames.find(F)->second;
188  }
189
190  /// setUnavailable - this can be used by whatever sets up TargetLibraryInfo to
191  /// ban use of specific library functions.
192  void setUnavailable(LibFunc::Func F) {
193    setState(F, Unavailable);
194  }
195
196  void setAvailable(LibFunc::Func F) {
197    setState(F, StandardName);
198  }
199
200  void setAvailableWithName(LibFunc::Func F, StringRef Name) {
201    if (StandardNames[F] != Name) {
202      setState(F, CustomName);
203      CustomNames[F] = Name;
204      assert(CustomNames.find(F) != CustomNames.end());
205    } else {
206      setState(F, StandardName);
207    }
208  }
209
210  /// disableAllFunctions - This disables all builtins, which is used for
211  /// options like -fno-builtin.
212  void disableAllFunctions();
213};
214
215} // end namespace llvm
216
217#endif
218