Add a few functions to TargetLibraryInfo as part of PR13574.
[oota-llvm.git] / lib / Target / TargetLibraryInfo.cpp
1 //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==//
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 // This file implements the TargetLibraryInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Target/TargetLibraryInfo.h"
15 #include "llvm/ADT/Triple.h"
16 using namespace llvm;
17
18 // Register the default implementation.
19 INITIALIZE_PASS(TargetLibraryInfo, "targetlibinfo",
20                 "Target Library Information", false, true)
21 char TargetLibraryInfo::ID = 0;
22
23 void TargetLibraryInfo::anchor() { }
24
25 const char* TargetLibraryInfo::StandardNames[LibFunc::NumLibFuncs] =
26   {
27     "__cxa_atexit",
28     "__cxa_guard_abort",
29     "__cxa_guard_acquire",
30     "__cxa_guard_release",
31     "__memcpy_chk",
32     "acos",
33     "acosf",
34     "acosh",
35     "acoshf",
36     "acoshl",
37     "acosl",
38     "asin",
39     "asinf",
40     "asinh",
41     "asinhf",
42     "asinhl",
43     "asinl",
44     "atan",
45     "atan2",
46     "atan2f",
47     "atan2l",
48     "atanf",
49     "atanh",
50     "atanhf",
51     "atanhl",
52     "atanl",
53     "cbrt",
54     "cbrtf",
55     "cbrtl",
56     "ceil",
57     "ceilf",
58     "ceill",
59     "copysign",
60     "copysignf",
61     "copysignl",
62     "cos",
63     "cosf",
64     "cosh",
65     "coshf",
66     "coshl",
67     "cosl",
68     "exp",
69     "exp10",
70     "exp10f",
71     "exp10l",
72     "exp2",
73     "exp2f",
74     "exp2l",
75     "expf",
76     "expl",
77     "expm1",
78     "expm1f",
79     "expm1l",
80     "fabs",
81     "fabsf",
82     "fabsl",
83     "fiprintf",
84     "floor",
85     "floorf",
86     "floorl",
87     "fmod",
88     "fmodf",
89     "fmodl",
90     "fputc",
91     "fputs",
92     "fwrite",
93     "iprintf",
94     "log",
95     "log10",
96     "log10f",
97     "log10l",
98     "log1p",
99     "log1pf",
100     "log1pl",
101     "log2",
102     "log2f",
103     "log2l",
104     "logb",
105     "logbf",
106     "logbl",
107     "logf",
108     "logl",
109     "memchr",
110     "memcmp",
111     "memcpy",
112     "memmove",
113     "memset",
114     "memset_pattern16",
115     "nearbyint",
116     "nearbyintf",
117     "nearbyintl",
118     "pow",
119     "powf",
120     "powl",
121     "putchar",
122     "puts",
123     "rint",
124     "rintf",
125     "rintl",
126     "round",
127     "roundf",
128     "roundl",
129     "sin",
130     "sinf",
131     "sinh",
132     "sinhf",
133     "sinhl",
134     "sinl",
135     "siprintf",
136     "sqrt",
137     "sqrtf",
138     "sqrtl",
139     "strcat",
140     "strchr",
141     "strcpy",
142     "strlen",
143     "strncat",
144     "strncmp",
145     "strncpy",
146     "strnlen",
147     "tan",
148     "tanf",
149     "tanh",
150     "tanhf",
151     "tanhl",
152     "tanl",
153     "trunc",
154     "truncf",
155     "truncl"
156   };
157
158 /// initialize - Initialize the set of available library functions based on the
159 /// specified target triple.  This should be carefully written so that a missing
160 /// target triple gets a sane set of defaults.
161 static void initialize(TargetLibraryInfo &TLI, const Triple &T,
162                        const char **StandardNames) {
163   initializeTargetLibraryInfoPass(*PassRegistry::getPassRegistry());
164
165 #ifndef NDEBUG
166   // Verify that the StandardNames array is in alphabetical order.
167   for (unsigned F = 1; F < LibFunc::NumLibFuncs; ++F) {
168     if (strcmp(StandardNames[F-1], StandardNames[F]) >= 0)
169       llvm_unreachable("TargetLibraryInfo function names must be sorted");
170   }
171 #endif // !NDEBUG
172   
173   // memset_pattern16 is only available on iOS 3.0 and Mac OS/X 10.5 and later.
174   if (T.isMacOSX()) {
175     if (T.isMacOSXVersionLT(10, 5))
176       TLI.setUnavailable(LibFunc::memset_pattern16);
177   } else if (T.getOS() == Triple::IOS) {
178     if (T.isOSVersionLT(3, 0))
179       TLI.setUnavailable(LibFunc::memset_pattern16);
180   } else {
181     TLI.setUnavailable(LibFunc::memset_pattern16);
182   }
183
184   if (T.isMacOSX() && T.getArch() == Triple::x86 &&
185       !T.isMacOSXVersionLT(10, 7)) {
186     // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
187     // we don't care about) have two versions; on recent OSX, the one we want
188     // has a $UNIX2003 suffix. The two implementations are identical except
189     // for the return value in some edge cases.  However, we don't want to
190     // generate code that depends on the old symbols.
191     TLI.setAvailableWithName(LibFunc::fwrite, "fwrite$UNIX2003");
192     TLI.setAvailableWithName(LibFunc::fputs, "fputs$UNIX2003");
193   }
194
195   // iprintf and friends are only available on XCore and TCE.
196   if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) {
197     TLI.setUnavailable(LibFunc::iprintf);
198     TLI.setUnavailable(LibFunc::siprintf);
199     TLI.setUnavailable(LibFunc::fiprintf);
200   }
201
202   if (T.getOS() == Triple::Win32) {
203     // Win32 does not support long double
204     TLI.setUnavailable(LibFunc::acosl);
205     TLI.setUnavailable(LibFunc::asinl);
206     TLI.setUnavailable(LibFunc::atanl);
207     TLI.setUnavailable(LibFunc::atan2l);
208     TLI.setUnavailable(LibFunc::ceill);
209     TLI.setUnavailable(LibFunc::copysignl);
210     TLI.setUnavailable(LibFunc::cosl);
211     TLI.setUnavailable(LibFunc::coshl);
212     TLI.setUnavailable(LibFunc::expl);
213     TLI.setUnavailable(LibFunc::fabsf); // Win32 and Win64 both lack fabsf
214     TLI.setUnavailable(LibFunc::fabsl);
215     TLI.setUnavailable(LibFunc::floorl);
216     TLI.setUnavailable(LibFunc::fmodl);
217     TLI.setUnavailable(LibFunc::logl);
218     TLI.setUnavailable(LibFunc::powl);
219     TLI.setUnavailable(LibFunc::sinl);
220     TLI.setUnavailable(LibFunc::sinhl);
221     TLI.setUnavailable(LibFunc::sqrtl);
222     TLI.setUnavailable(LibFunc::tanl);
223     TLI.setUnavailable(LibFunc::tanhl);
224
225     // Win32 only has C89 math
226     TLI.setUnavailable(LibFunc::acosh);
227     TLI.setUnavailable(LibFunc::acoshf);
228     TLI.setUnavailable(LibFunc::acoshl);
229     TLI.setUnavailable(LibFunc::asinh);
230     TLI.setUnavailable(LibFunc::asinhf);
231     TLI.setUnavailable(LibFunc::asinhl);
232     TLI.setUnavailable(LibFunc::atanh);
233     TLI.setUnavailable(LibFunc::atanhf);
234     TLI.setUnavailable(LibFunc::atanhl);
235     TLI.setUnavailable(LibFunc::cbrt);
236     TLI.setUnavailable(LibFunc::cbrtf);
237     TLI.setUnavailable(LibFunc::cbrtl);
238     TLI.setUnavailable(LibFunc::exp10);
239     TLI.setUnavailable(LibFunc::exp10f);
240     TLI.setUnavailable(LibFunc::exp10l);
241     TLI.setUnavailable(LibFunc::exp2);
242     TLI.setUnavailable(LibFunc::exp2f);
243     TLI.setUnavailable(LibFunc::exp2l);
244     TLI.setUnavailable(LibFunc::expm1);
245     TLI.setUnavailable(LibFunc::expm1f);
246     TLI.setUnavailable(LibFunc::expm1l);
247     TLI.setUnavailable(LibFunc::log2);
248     TLI.setUnavailable(LibFunc::log2f);
249     TLI.setUnavailable(LibFunc::log2l);
250     TLI.setUnavailable(LibFunc::log1p);
251     TLI.setUnavailable(LibFunc::log1pf);
252     TLI.setUnavailable(LibFunc::log1pl);
253     TLI.setUnavailable(LibFunc::logb);
254     TLI.setUnavailable(LibFunc::logbf);
255     TLI.setUnavailable(LibFunc::logbl);
256     TLI.setUnavailable(LibFunc::nearbyint);
257     TLI.setUnavailable(LibFunc::nearbyintf);
258     TLI.setUnavailable(LibFunc::nearbyintl);
259     TLI.setUnavailable(LibFunc::rint);
260     TLI.setUnavailable(LibFunc::rintf);
261     TLI.setUnavailable(LibFunc::rintl);
262     TLI.setUnavailable(LibFunc::round);
263     TLI.setUnavailable(LibFunc::roundf);
264     TLI.setUnavailable(LibFunc::roundl);
265     TLI.setUnavailable(LibFunc::trunc);
266     TLI.setUnavailable(LibFunc::truncf);
267     TLI.setUnavailable(LibFunc::truncl);
268
269     // Win32 provides some C99 math with mangled names
270     TLI.setAvailableWithName(LibFunc::copysign, "_copysign");
271
272     if (T.getArch() == Triple::x86) {
273       // Win32 on x86 implements single-precision math functions as macros
274       TLI.setUnavailable(LibFunc::acosf);
275       TLI.setUnavailable(LibFunc::asinf);
276       TLI.setUnavailable(LibFunc::atanf);
277       TLI.setUnavailable(LibFunc::atan2f);
278       TLI.setUnavailable(LibFunc::ceilf);
279       TLI.setUnavailable(LibFunc::copysignf);
280       TLI.setUnavailable(LibFunc::cosf);
281       TLI.setUnavailable(LibFunc::coshf);
282       TLI.setUnavailable(LibFunc::expf);
283       TLI.setUnavailable(LibFunc::floorf);
284       TLI.setUnavailable(LibFunc::fmodf);
285       TLI.setUnavailable(LibFunc::logf);
286       TLI.setUnavailable(LibFunc::powf);
287       TLI.setUnavailable(LibFunc::sinf);
288       TLI.setUnavailable(LibFunc::sinhf);
289       TLI.setUnavailable(LibFunc::sqrtf);
290       TLI.setUnavailable(LibFunc::tanf);
291       TLI.setUnavailable(LibFunc::tanhf);
292     }
293   }
294 }
295
296
297 TargetLibraryInfo::TargetLibraryInfo() : ImmutablePass(ID) {
298   // Default to everything being available.
299   memset(AvailableArray, -1, sizeof(AvailableArray));
300
301   initialize(*this, Triple(), StandardNames);
302 }
303
304 TargetLibraryInfo::TargetLibraryInfo(const Triple &T) : ImmutablePass(ID) {
305   // Default to everything being available.
306   memset(AvailableArray, -1, sizeof(AvailableArray));
307   
308   initialize(*this, T, StandardNames);
309 }
310
311 TargetLibraryInfo::TargetLibraryInfo(const TargetLibraryInfo &TLI)
312   : ImmutablePass(ID) {
313   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
314   CustomNames = TLI.CustomNames;
315 }
316
317 bool TargetLibraryInfo::getLibFunc(StringRef funcName,
318                                    LibFunc::Func &F) const {
319   const char **Start = &StandardNames[0];
320   const char **End = &StandardNames[LibFunc::NumLibFuncs];
321   const char **I = std::lower_bound(Start, End, funcName);
322   if (I != End && *I == funcName) {
323     F = (LibFunc::Func)(I - Start);
324     return true;
325   }
326   return false;
327 }
328
329 /// disableAllFunctions - This disables all builtins, which is used for options
330 /// like -fno-builtin.
331 void TargetLibraryInfo::disableAllFunctions() {
332   memset(AvailableArray, 0, sizeof(AvailableArray));
333 }