]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Analysis/TargetLibraryInfo.cpp
Merge ^/head r311940 through r312200.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Analysis / 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/Analysis/TargetLibraryInfo.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/Support/CommandLine.h"
17 using namespace llvm;
18
19 static cl::opt<TargetLibraryInfoImpl::VectorLibrary> ClVectorLibrary(
20     "vector-library", cl::Hidden, cl::desc("Vector functions library"),
21     cl::init(TargetLibraryInfoImpl::NoLibrary),
22     cl::values(clEnumValN(TargetLibraryInfoImpl::NoLibrary, "none",
23                           "No vector functions library"),
24                clEnumValN(TargetLibraryInfoImpl::Accelerate, "Accelerate",
25                           "Accelerate framework"),
26                clEnumValN(TargetLibraryInfoImpl::SVML, "SVML",
27                           "Intel SVML library")));
28
29 StringRef const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] = {
30 #define TLI_DEFINE_STRING
31 #include "llvm/Analysis/TargetLibraryInfo.def"
32 };
33
34 static bool hasSinCosPiStret(const Triple &T) {
35   // Only Darwin variants have _stret versions of combined trig functions.
36   if (!T.isOSDarwin())
37     return false;
38
39   // The ABI is rather complicated on x86, so don't do anything special there.
40   if (T.getArch() == Triple::x86)
41     return false;
42
43   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
44     return false;
45
46   if (T.isiOS() && T.isOSVersionLT(7, 0))
47     return false;
48
49   return true;
50 }
51
52 /// initialize - Initialize the set of available library functions based on the
53 /// specified target triple.  This should be carefully written so that a missing
54 /// target triple gets a sane set of defaults.
55 static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
56                        ArrayRef<StringRef> StandardNames) {
57   // Verify that the StandardNames array is in alphabetical order.
58   assert(std::is_sorted(StandardNames.begin(), StandardNames.end(),
59                         [](StringRef LHS, StringRef RHS) {
60                           return LHS < RHS;
61                         }) &&
62          "TargetLibraryInfoImpl function names must be sorted");
63
64   bool ShouldExtI32Param = false, ShouldExtI32Return = false,
65        ShouldSignExtI32Param = false;
66   // PowerPC64, Sparc64, SystemZ need signext/zeroext on i32 parameters and
67   // returns corresponding to C-level ints and unsigned ints.
68   if (T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le ||
69       T.getArch() == Triple::sparcv9 || T.getArch() == Triple::systemz) {
70     ShouldExtI32Param = true;
71     ShouldExtI32Return = true;
72   }
73   // Mips, on the other hand, needs signext on i32 parameters corresponding
74   // to both signed and unsigned ints.
75   if (T.getArch() == Triple::mips || T.getArch() == Triple::mipsel ||
76       T.getArch() == Triple::mips64 || T.getArch() == Triple::mips64el) {
77     ShouldSignExtI32Param = true;
78   }
79   TLI.setShouldExtI32Param(ShouldExtI32Param);
80   TLI.setShouldExtI32Return(ShouldExtI32Return);
81   TLI.setShouldSignExtI32Param(ShouldSignExtI32Param);
82
83   if (T.getArch() == Triple::r600 ||
84       T.getArch() == Triple::amdgcn) {
85     TLI.setUnavailable(LibFunc::ldexp);
86     TLI.setUnavailable(LibFunc::ldexpf);
87     TLI.setUnavailable(LibFunc::ldexpl);
88     TLI.setUnavailable(LibFunc::exp10);
89     TLI.setUnavailable(LibFunc::exp10f);
90     TLI.setUnavailable(LibFunc::exp10l);
91     TLI.setUnavailable(LibFunc::log10);
92     TLI.setUnavailable(LibFunc::log10f);
93     TLI.setUnavailable(LibFunc::log10l);
94   }
95
96   // There are no library implementations of mempcy and memset for AMD gpus and
97   // these can be difficult to lower in the backend.
98   if (T.getArch() == Triple::r600 ||
99       T.getArch() == Triple::amdgcn) {
100     TLI.setUnavailable(LibFunc::memcpy);
101     TLI.setUnavailable(LibFunc::memset);
102     TLI.setUnavailable(LibFunc::memset_pattern16);
103     return;
104   }
105
106   // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
107   // All versions of watchOS support it.
108   if (T.isMacOSX()) {
109     if (T.isMacOSXVersionLT(10, 5))
110       TLI.setUnavailable(LibFunc::memset_pattern16);
111   } else if (T.isiOS()) {
112     if (T.isOSVersionLT(3, 0))
113       TLI.setUnavailable(LibFunc::memset_pattern16);
114   } else if (!T.isWatchOS()) {
115     TLI.setUnavailable(LibFunc::memset_pattern16);
116   }
117
118   if (!hasSinCosPiStret(T)) {
119     TLI.setUnavailable(LibFunc::sinpi);
120     TLI.setUnavailable(LibFunc::sinpif);
121     TLI.setUnavailable(LibFunc::cospi);
122     TLI.setUnavailable(LibFunc::cospif);
123     TLI.setUnavailable(LibFunc::sincospi_stret);
124     TLI.setUnavailable(LibFunc::sincospif_stret);
125   }
126
127   if (T.isMacOSX() && T.getArch() == Triple::x86 &&
128       !T.isMacOSXVersionLT(10, 7)) {
129     // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
130     // we don't care about) have two versions; on recent OSX, the one we want
131     // has a $UNIX2003 suffix. The two implementations are identical except
132     // for the return value in some edge cases.  However, we don't want to
133     // generate code that depends on the old symbols.
134     TLI.setAvailableWithName(LibFunc::fwrite, "fwrite$UNIX2003");
135     TLI.setAvailableWithName(LibFunc::fputs, "fputs$UNIX2003");
136   }
137
138   // iprintf and friends are only available on XCore and TCE.
139   if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) {
140     TLI.setUnavailable(LibFunc::iprintf);
141     TLI.setUnavailable(LibFunc::siprintf);
142     TLI.setUnavailable(LibFunc::fiprintf);
143   }
144
145   if (T.isOSWindows() && !T.isOSCygMing()) {
146     // Win32 does not support long double
147     TLI.setUnavailable(LibFunc::acosl);
148     TLI.setUnavailable(LibFunc::asinl);
149     TLI.setUnavailable(LibFunc::atanl);
150     TLI.setUnavailable(LibFunc::atan2l);
151     TLI.setUnavailable(LibFunc::ceill);
152     TLI.setUnavailable(LibFunc::copysignl);
153     TLI.setUnavailable(LibFunc::cosl);
154     TLI.setUnavailable(LibFunc::coshl);
155     TLI.setUnavailable(LibFunc::expl);
156     TLI.setUnavailable(LibFunc::fabsf); // Win32 and Win64 both lack fabsf
157     TLI.setUnavailable(LibFunc::fabsl);
158     TLI.setUnavailable(LibFunc::floorl);
159     TLI.setUnavailable(LibFunc::fmaxl);
160     TLI.setUnavailable(LibFunc::fminl);
161     TLI.setUnavailable(LibFunc::fmodl);
162     TLI.setUnavailable(LibFunc::frexpl);
163     TLI.setUnavailable(LibFunc::ldexpf);
164     TLI.setUnavailable(LibFunc::ldexpl);
165     TLI.setUnavailable(LibFunc::logl);
166     TLI.setUnavailable(LibFunc::modfl);
167     TLI.setUnavailable(LibFunc::powl);
168     TLI.setUnavailable(LibFunc::sinl);
169     TLI.setUnavailable(LibFunc::sinhl);
170     TLI.setUnavailable(LibFunc::sqrtl);
171     TLI.setUnavailable(LibFunc::tanl);
172     TLI.setUnavailable(LibFunc::tanhl);
173
174     // Win32 only has C89 math
175     TLI.setUnavailable(LibFunc::acosh);
176     TLI.setUnavailable(LibFunc::acoshf);
177     TLI.setUnavailable(LibFunc::acoshl);
178     TLI.setUnavailable(LibFunc::asinh);
179     TLI.setUnavailable(LibFunc::asinhf);
180     TLI.setUnavailable(LibFunc::asinhl);
181     TLI.setUnavailable(LibFunc::atanh);
182     TLI.setUnavailable(LibFunc::atanhf);
183     TLI.setUnavailable(LibFunc::atanhl);
184     TLI.setUnavailable(LibFunc::cbrt);
185     TLI.setUnavailable(LibFunc::cbrtf);
186     TLI.setUnavailable(LibFunc::cbrtl);
187     TLI.setUnavailable(LibFunc::exp2);
188     TLI.setUnavailable(LibFunc::exp2f);
189     TLI.setUnavailable(LibFunc::exp2l);
190     TLI.setUnavailable(LibFunc::expm1);
191     TLI.setUnavailable(LibFunc::expm1f);
192     TLI.setUnavailable(LibFunc::expm1l);
193     TLI.setUnavailable(LibFunc::log2);
194     TLI.setUnavailable(LibFunc::log2f);
195     TLI.setUnavailable(LibFunc::log2l);
196     TLI.setUnavailable(LibFunc::log1p);
197     TLI.setUnavailable(LibFunc::log1pf);
198     TLI.setUnavailable(LibFunc::log1pl);
199     TLI.setUnavailable(LibFunc::logb);
200     TLI.setUnavailable(LibFunc::logbf);
201     TLI.setUnavailable(LibFunc::logbl);
202     TLI.setUnavailable(LibFunc::nearbyint);
203     TLI.setUnavailable(LibFunc::nearbyintf);
204     TLI.setUnavailable(LibFunc::nearbyintl);
205     TLI.setUnavailable(LibFunc::rint);
206     TLI.setUnavailable(LibFunc::rintf);
207     TLI.setUnavailable(LibFunc::rintl);
208     TLI.setUnavailable(LibFunc::round);
209     TLI.setUnavailable(LibFunc::roundf);
210     TLI.setUnavailable(LibFunc::roundl);
211     TLI.setUnavailable(LibFunc::trunc);
212     TLI.setUnavailable(LibFunc::truncf);
213     TLI.setUnavailable(LibFunc::truncl);
214
215     // Win32 provides some C99 math with mangled names
216     TLI.setAvailableWithName(LibFunc::copysign, "_copysign");
217
218     if (T.getArch() == Triple::x86) {
219       // Win32 on x86 implements single-precision math functions as macros
220       TLI.setUnavailable(LibFunc::acosf);
221       TLI.setUnavailable(LibFunc::asinf);
222       TLI.setUnavailable(LibFunc::atanf);
223       TLI.setUnavailable(LibFunc::atan2f);
224       TLI.setUnavailable(LibFunc::ceilf);
225       TLI.setUnavailable(LibFunc::copysignf);
226       TLI.setUnavailable(LibFunc::cosf);
227       TLI.setUnavailable(LibFunc::coshf);
228       TLI.setUnavailable(LibFunc::expf);
229       TLI.setUnavailable(LibFunc::floorf);
230       TLI.setUnavailable(LibFunc::fminf);
231       TLI.setUnavailable(LibFunc::fmaxf);
232       TLI.setUnavailable(LibFunc::fmodf);
233       TLI.setUnavailable(LibFunc::logf);
234       TLI.setUnavailable(LibFunc::log10f);
235       TLI.setUnavailable(LibFunc::modff);
236       TLI.setUnavailable(LibFunc::powf);
237       TLI.setUnavailable(LibFunc::sinf);
238       TLI.setUnavailable(LibFunc::sinhf);
239       TLI.setUnavailable(LibFunc::sqrtf);
240       TLI.setUnavailable(LibFunc::tanf);
241       TLI.setUnavailable(LibFunc::tanhf);
242     }
243
244     // Win32 does *not* provide provide these functions, but they are
245     // generally available on POSIX-compliant systems:
246     TLI.setUnavailable(LibFunc::access);
247     TLI.setUnavailable(LibFunc::bcmp);
248     TLI.setUnavailable(LibFunc::bcopy);
249     TLI.setUnavailable(LibFunc::bzero);
250     TLI.setUnavailable(LibFunc::chmod);
251     TLI.setUnavailable(LibFunc::chown);
252     TLI.setUnavailable(LibFunc::closedir);
253     TLI.setUnavailable(LibFunc::ctermid);
254     TLI.setUnavailable(LibFunc::fdopen);
255     TLI.setUnavailable(LibFunc::ffs);
256     TLI.setUnavailable(LibFunc::fileno);
257     TLI.setUnavailable(LibFunc::flockfile);
258     TLI.setUnavailable(LibFunc::fseeko);
259     TLI.setUnavailable(LibFunc::fstat);
260     TLI.setUnavailable(LibFunc::fstatvfs);
261     TLI.setUnavailable(LibFunc::ftello);
262     TLI.setUnavailable(LibFunc::ftrylockfile);
263     TLI.setUnavailable(LibFunc::funlockfile);
264     TLI.setUnavailable(LibFunc::getc_unlocked);
265     TLI.setUnavailable(LibFunc::getitimer);
266     TLI.setUnavailable(LibFunc::getlogin_r);
267     TLI.setUnavailable(LibFunc::getpwnam);
268     TLI.setUnavailable(LibFunc::gettimeofday);
269     TLI.setUnavailable(LibFunc::htonl);
270     TLI.setUnavailable(LibFunc::htons);
271     TLI.setUnavailable(LibFunc::lchown);
272     TLI.setUnavailable(LibFunc::lstat);
273     TLI.setUnavailable(LibFunc::memccpy);
274     TLI.setUnavailable(LibFunc::mkdir);
275     TLI.setUnavailable(LibFunc::ntohl);
276     TLI.setUnavailable(LibFunc::ntohs);
277     TLI.setUnavailable(LibFunc::open);
278     TLI.setUnavailable(LibFunc::opendir);
279     TLI.setUnavailable(LibFunc::pclose);
280     TLI.setUnavailable(LibFunc::popen);
281     TLI.setUnavailable(LibFunc::pread);
282     TLI.setUnavailable(LibFunc::pwrite);
283     TLI.setUnavailable(LibFunc::read);
284     TLI.setUnavailable(LibFunc::readlink);
285     TLI.setUnavailable(LibFunc::realpath);
286     TLI.setUnavailable(LibFunc::rmdir);
287     TLI.setUnavailable(LibFunc::setitimer);
288     TLI.setUnavailable(LibFunc::stat);
289     TLI.setUnavailable(LibFunc::statvfs);
290     TLI.setUnavailable(LibFunc::stpcpy);
291     TLI.setUnavailable(LibFunc::stpncpy);
292     TLI.setUnavailable(LibFunc::strcasecmp);
293     TLI.setUnavailable(LibFunc::strncasecmp);
294     TLI.setUnavailable(LibFunc::times);
295     TLI.setUnavailable(LibFunc::uname);
296     TLI.setUnavailable(LibFunc::unlink);
297     TLI.setUnavailable(LibFunc::unsetenv);
298     TLI.setUnavailable(LibFunc::utime);
299     TLI.setUnavailable(LibFunc::utimes);
300     TLI.setUnavailable(LibFunc::write);
301
302     // Win32 does *not* provide provide these functions, but they are
303     // specified by C99:
304     TLI.setUnavailable(LibFunc::atoll);
305     TLI.setUnavailable(LibFunc::frexpf);
306     TLI.setUnavailable(LibFunc::llabs);
307   }
308
309   switch (T.getOS()) {
310   case Triple::MacOSX:
311     // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
312     // and their names are __exp10 and __exp10f. exp10l is not available on
313     // OS X or iOS.
314     TLI.setUnavailable(LibFunc::exp10l);
315     if (T.isMacOSXVersionLT(10, 9)) {
316       TLI.setUnavailable(LibFunc::exp10);
317       TLI.setUnavailable(LibFunc::exp10f);
318     } else {
319       TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
320       TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
321     }
322     break;
323   case Triple::IOS:
324   case Triple::TvOS:
325   case Triple::WatchOS:
326     TLI.setUnavailable(LibFunc::exp10l);
327     if (!T.isWatchOS() && (T.isOSVersionLT(7, 0) ||
328                            (T.isOSVersionLT(9, 0) &&
329                             (T.getArch() == Triple::x86 ||
330                              T.getArch() == Triple::x86_64)))) {
331       TLI.setUnavailable(LibFunc::exp10);
332       TLI.setUnavailable(LibFunc::exp10f);
333     } else {
334       TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
335       TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
336     }
337     break;
338   case Triple::Linux:
339     // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
340     // buggy prior to glibc version 2.18. Until this version is widely deployed
341     // or we have a reasonable detection strategy, we cannot use exp10 reliably
342     // on Linux.
343     //
344     // Fall through to disable all of them.
345     LLVM_FALLTHROUGH;
346   default:
347     TLI.setUnavailable(LibFunc::exp10);
348     TLI.setUnavailable(LibFunc::exp10f);
349     TLI.setUnavailable(LibFunc::exp10l);
350   }
351
352   // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
353   // Linux (GLIBC):
354   // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
355   // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
356   // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
357   switch (T.getOS()) {
358   case Triple::Darwin:
359   case Triple::MacOSX:
360   case Triple::IOS:
361   case Triple::TvOS:
362   case Triple::WatchOS:
363   case Triple::FreeBSD:
364   case Triple::Linux:
365     break;
366   default:
367     TLI.setUnavailable(LibFunc::ffsl);
368   }
369
370   // ffsll is available on at least FreeBSD and Linux (GLIBC):
371   // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
372   // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
373   switch (T.getOS()) {
374   case Triple::Darwin:
375   case Triple::MacOSX:
376   case Triple::IOS:
377   case Triple::TvOS:
378   case Triple::WatchOS:
379   case Triple::FreeBSD:
380   case Triple::Linux:
381     break;
382   default:
383     TLI.setUnavailable(LibFunc::ffsll);
384   }
385
386   // The following functions are available on at least FreeBSD:
387   // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
388   // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
389   // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
390   if (!T.isOSFreeBSD()) {
391     TLI.setUnavailable(LibFunc::fls);
392     TLI.setUnavailable(LibFunc::flsl);
393     TLI.setUnavailable(LibFunc::flsll);
394   }
395
396   // The following functions are available on at least Linux:
397   if (!T.isOSLinux()) {
398     TLI.setUnavailable(LibFunc::dunder_strdup);
399     TLI.setUnavailable(LibFunc::dunder_strtok_r);
400     TLI.setUnavailable(LibFunc::dunder_isoc99_scanf);
401     TLI.setUnavailable(LibFunc::dunder_isoc99_sscanf);
402     TLI.setUnavailable(LibFunc::under_IO_getc);
403     TLI.setUnavailable(LibFunc::under_IO_putc);
404     TLI.setUnavailable(LibFunc::memalign);
405     TLI.setUnavailable(LibFunc::fopen64);
406     TLI.setUnavailable(LibFunc::fseeko64);
407     TLI.setUnavailable(LibFunc::fstat64);
408     TLI.setUnavailable(LibFunc::fstatvfs64);
409     TLI.setUnavailable(LibFunc::ftello64);
410     TLI.setUnavailable(LibFunc::lstat64);
411     TLI.setUnavailable(LibFunc::open64);
412     TLI.setUnavailable(LibFunc::stat64);
413     TLI.setUnavailable(LibFunc::statvfs64);
414     TLI.setUnavailable(LibFunc::tmpfile64);
415   }
416
417   // As currently implemented in clang, NVPTX code has no standard library to
418   // speak of.  Headers provide a standard-ish library implementation, but many
419   // of the signatures are wrong -- for example, many libm functions are not
420   // extern "C".
421   //
422   // libdevice, an IR library provided by nvidia, is linked in by the front-end,
423   // but only used functions are provided to llvm.  Moreover, most of the
424   // functions in libdevice don't map precisely to standard library functions.
425   //
426   // FIXME: Having no standard library prevents e.g. many fastmath
427   // optimizations, so this situation should be fixed.
428   if (T.isNVPTX()) {
429     TLI.disableAllFunctions();
430     TLI.setAvailable(LibFunc::nvvm_reflect);
431   } else {
432     TLI.setUnavailable(LibFunc::nvvm_reflect);
433   }
434
435   TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary);
436 }
437
438 TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
439   // Default to everything being available.
440   memset(AvailableArray, -1, sizeof(AvailableArray));
441
442   initialize(*this, Triple(), StandardNames);
443 }
444
445 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
446   // Default to everything being available.
447   memset(AvailableArray, -1, sizeof(AvailableArray));
448
449   initialize(*this, T, StandardNames);
450 }
451
452 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
453     : CustomNames(TLI.CustomNames), ShouldExtI32Param(TLI.ShouldExtI32Param),
454       ShouldExtI32Return(TLI.ShouldExtI32Return),
455       ShouldSignExtI32Param(TLI.ShouldSignExtI32Param) {
456   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
457   VectorDescs = TLI.VectorDescs;
458   ScalarDescs = TLI.ScalarDescs;
459 }
460
461 TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
462     : CustomNames(std::move(TLI.CustomNames)),
463       ShouldExtI32Param(TLI.ShouldExtI32Param),
464       ShouldExtI32Return(TLI.ShouldExtI32Return),
465       ShouldSignExtI32Param(TLI.ShouldSignExtI32Param) {
466   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
467             AvailableArray);
468   VectorDescs = TLI.VectorDescs;
469   ScalarDescs = TLI.ScalarDescs;
470 }
471
472 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
473   CustomNames = TLI.CustomNames;
474   ShouldExtI32Param = TLI.ShouldExtI32Param;
475   ShouldExtI32Return = TLI.ShouldExtI32Return;
476   ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
477   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
478   return *this;
479 }
480
481 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
482   CustomNames = std::move(TLI.CustomNames);
483   ShouldExtI32Param = TLI.ShouldExtI32Param;
484   ShouldExtI32Return = TLI.ShouldExtI32Return;
485   ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
486   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
487             AvailableArray);
488   return *this;
489 }
490
491 static StringRef sanitizeFunctionName(StringRef funcName) {
492   // Filter out empty names and names containing null bytes, those can't be in
493   // our table.
494   if (funcName.empty() || funcName.find('\0') != StringRef::npos)
495     return StringRef();
496
497   // Check for \01 prefix that is used to mangle __asm declarations and
498   // strip it if present.
499   return GlobalValue::getRealLinkageName(funcName);
500 }
501
502 bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName,
503                                        LibFunc::Func &F) const {
504   StringRef const *Start = &StandardNames[0];
505   StringRef const *End = &StandardNames[LibFunc::NumLibFuncs];
506
507   funcName = sanitizeFunctionName(funcName);
508   if (funcName.empty())
509     return false;
510
511   StringRef const *I = std::lower_bound(
512       Start, End, funcName, [](StringRef LHS, StringRef RHS) {
513         return LHS < RHS;
514       });
515   if (I != End && *I == funcName) {
516     F = (LibFunc::Func)(I - Start);
517     return true;
518   }
519   return false;
520 }
521
522 bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
523                                                    LibFunc::Func F,
524                                                    const DataLayout *DL) const {
525   LLVMContext &Ctx = FTy.getContext();
526   Type *PCharTy = Type::getInt8PtrTy(Ctx);
527   Type *SizeTTy = DL ? DL->getIntPtrType(Ctx, /*AS=*/0) : nullptr;
528   auto IsSizeTTy = [SizeTTy](Type *Ty) {
529     return SizeTTy ? Ty == SizeTTy : Ty->isIntegerTy();
530   };
531   unsigned NumParams = FTy.getNumParams();
532
533   switch (F) {
534   case LibFunc::strlen:
535     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
536             FTy.getReturnType()->isIntegerTy());
537
538   case LibFunc::strchr:
539   case LibFunc::strrchr:
540     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
541             FTy.getParamType(0) == FTy.getReturnType() &&
542             FTy.getParamType(1)->isIntegerTy());
543
544   case LibFunc::strtol:
545   case LibFunc::strtod:
546   case LibFunc::strtof:
547   case LibFunc::strtoul:
548   case LibFunc::strtoll:
549   case LibFunc::strtold:
550   case LibFunc::strtoull:
551     return ((NumParams == 2 || NumParams == 3) &&
552             FTy.getParamType(0)->isPointerTy() &&
553             FTy.getParamType(1)->isPointerTy());
554   case LibFunc::strcat:
555     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
556             FTy.getParamType(0) == FTy.getReturnType() &&
557             FTy.getParamType(1) == FTy.getReturnType());
558
559   case LibFunc::strncat:
560     return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
561             FTy.getParamType(0) == FTy.getReturnType() &&
562             FTy.getParamType(1) == FTy.getReturnType() &&
563             FTy.getParamType(2)->isIntegerTy());
564
565   case LibFunc::strcpy_chk:
566   case LibFunc::stpcpy_chk:
567     --NumParams;
568     if (!IsSizeTTy(FTy.getParamType(NumParams)))
569       return false;
570     LLVM_FALLTHROUGH;
571   case LibFunc::strcpy:
572   case LibFunc::stpcpy:
573     return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(0) &&
574             FTy.getParamType(0) == FTy.getParamType(1) &&
575             FTy.getParamType(0) == PCharTy);
576
577   case LibFunc::strncpy_chk:
578   case LibFunc::stpncpy_chk:
579     --NumParams;
580     if (!IsSizeTTy(FTy.getParamType(NumParams)))
581       return false;
582     LLVM_FALLTHROUGH;
583   case LibFunc::strncpy:
584   case LibFunc::stpncpy:
585     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
586             FTy.getParamType(0) == FTy.getParamType(1) &&
587             FTy.getParamType(0) == PCharTy &&
588             FTy.getParamType(2)->isIntegerTy());
589
590   case LibFunc::strxfrm:
591     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
592             FTy.getParamType(1)->isPointerTy());
593
594   case LibFunc::strcmp:
595     return (NumParams == 2 && FTy.getReturnType()->isIntegerTy(32) &&
596             FTy.getParamType(0)->isPointerTy() &&
597             FTy.getParamType(0) == FTy.getParamType(1));
598
599   case LibFunc::strncmp:
600     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
601             FTy.getParamType(0)->isPointerTy() &&
602             FTy.getParamType(0) == FTy.getParamType(1) &&
603             FTy.getParamType(2)->isIntegerTy());
604
605   case LibFunc::strspn:
606   case LibFunc::strcspn:
607     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
608             FTy.getParamType(0) == FTy.getParamType(1) &&
609             FTy.getReturnType()->isIntegerTy());
610
611   case LibFunc::strcoll:
612   case LibFunc::strcasecmp:
613   case LibFunc::strncasecmp:
614     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
615             FTy.getParamType(1)->isPointerTy());
616
617   case LibFunc::strstr:
618     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
619             FTy.getParamType(0)->isPointerTy() &&
620             FTy.getParamType(1)->isPointerTy());
621
622   case LibFunc::strpbrk:
623     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
624             FTy.getReturnType() == FTy.getParamType(0) &&
625             FTy.getParamType(0) == FTy.getParamType(1));
626
627   case LibFunc::strtok:
628   case LibFunc::strtok_r:
629     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
630   case LibFunc::scanf:
631   case LibFunc::setbuf:
632   case LibFunc::setvbuf:
633     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
634   case LibFunc::strdup:
635   case LibFunc::strndup:
636     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
637             FTy.getParamType(0)->isPointerTy());
638   case LibFunc::sscanf:
639   case LibFunc::stat:
640   case LibFunc::statvfs:
641   case LibFunc::sprintf:
642     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
643             FTy.getParamType(1)->isPointerTy());
644   case LibFunc::snprintf:
645     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
646             FTy.getParamType(2)->isPointerTy());
647   case LibFunc::setitimer:
648     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
649             FTy.getParamType(2)->isPointerTy());
650   case LibFunc::system:
651     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
652   case LibFunc::malloc:
653     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
654   case LibFunc::memcmp:
655     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
656             FTy.getParamType(1)->isPointerTy() &&
657             FTy.getReturnType()->isIntegerTy(32));
658
659   case LibFunc::memchr:
660   case LibFunc::memrchr:
661     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
662             FTy.getParamType(1)->isIntegerTy(32) &&
663             FTy.getParamType(2)->isIntegerTy() &&
664             FTy.getReturnType()->isPointerTy());
665   case LibFunc::modf:
666   case LibFunc::modff:
667   case LibFunc::modfl:
668     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
669
670   case LibFunc::memcpy_chk:
671   case LibFunc::memmove_chk:
672     --NumParams;
673     if (!IsSizeTTy(FTy.getParamType(NumParams)))
674       return false;
675     LLVM_FALLTHROUGH;
676   case LibFunc::memcpy:
677   case LibFunc::mempcpy:
678   case LibFunc::memmove:
679     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
680             FTy.getParamType(0)->isPointerTy() &&
681             FTy.getParamType(1)->isPointerTy() &&
682             IsSizeTTy(FTy.getParamType(2)));
683
684   case LibFunc::memset_chk:
685     --NumParams;
686     if (!IsSizeTTy(FTy.getParamType(NumParams)))
687       return false;
688     LLVM_FALLTHROUGH;
689   case LibFunc::memset:
690     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
691             FTy.getParamType(0)->isPointerTy() &&
692             FTy.getParamType(1)->isIntegerTy() &&
693             IsSizeTTy(FTy.getParamType(2)));
694
695   case LibFunc::memccpy:
696     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
697   case LibFunc::memalign:
698     return (FTy.getReturnType()->isPointerTy());
699   case LibFunc::realloc:
700     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
701             FTy.getReturnType()->isPointerTy());
702   case LibFunc::read:
703     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
704   case LibFunc::rewind:
705   case LibFunc::rmdir:
706   case LibFunc::remove:
707   case LibFunc::realpath:
708     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
709   case LibFunc::rename:
710     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
711             FTy.getParamType(1)->isPointerTy());
712   case LibFunc::readlink:
713     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
714             FTy.getParamType(1)->isPointerTy());
715   case LibFunc::write:
716     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
717   case LibFunc::bcopy:
718   case LibFunc::bcmp:
719     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
720             FTy.getParamType(1)->isPointerTy());
721   case LibFunc::bzero:
722     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
723   case LibFunc::calloc:
724     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
725
726   case LibFunc::atof:
727   case LibFunc::atoi:
728   case LibFunc::atol:
729   case LibFunc::atoll:
730   case LibFunc::ferror:
731   case LibFunc::getenv:
732   case LibFunc::getpwnam:
733   case LibFunc::pclose:
734   case LibFunc::perror:
735   case LibFunc::printf:
736   case LibFunc::puts:
737   case LibFunc::uname:
738   case LibFunc::under_IO_getc:
739   case LibFunc::unlink:
740   case LibFunc::unsetenv:
741     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
742
743   case LibFunc::chmod:
744   case LibFunc::chown:
745   case LibFunc::clearerr:
746   case LibFunc::closedir:
747   case LibFunc::ctermid:
748   case LibFunc::fclose:
749   case LibFunc::feof:
750   case LibFunc::fflush:
751   case LibFunc::fgetc:
752   case LibFunc::fileno:
753   case LibFunc::flockfile:
754   case LibFunc::free:
755   case LibFunc::fseek:
756   case LibFunc::fseeko64:
757   case LibFunc::fseeko:
758   case LibFunc::fsetpos:
759   case LibFunc::ftell:
760   case LibFunc::ftello64:
761   case LibFunc::ftello:
762   case LibFunc::ftrylockfile:
763   case LibFunc::funlockfile:
764   case LibFunc::getc:
765   case LibFunc::getc_unlocked:
766   case LibFunc::getlogin_r:
767   case LibFunc::mkdir:
768   case LibFunc::mktime:
769   case LibFunc::times:
770     return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
771
772   case LibFunc::access:
773     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
774   case LibFunc::fopen:
775     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
776             FTy.getParamType(0)->isPointerTy() &&
777             FTy.getParamType(1)->isPointerTy());
778   case LibFunc::fdopen:
779     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
780             FTy.getParamType(1)->isPointerTy());
781   case LibFunc::fputc:
782   case LibFunc::fstat:
783   case LibFunc::frexp:
784   case LibFunc::frexpf:
785   case LibFunc::frexpl:
786   case LibFunc::fstatvfs:
787     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
788   case LibFunc::fgets:
789     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
790             FTy.getParamType(2)->isPointerTy());
791   case LibFunc::fread:
792     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
793             FTy.getParamType(3)->isPointerTy());
794   case LibFunc::fwrite:
795     return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() &&
796             FTy.getParamType(0)->isPointerTy() &&
797             FTy.getParamType(1)->isIntegerTy() &&
798             FTy.getParamType(2)->isIntegerTy() &&
799             FTy.getParamType(3)->isPointerTy());
800   case LibFunc::fputs:
801     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
802             FTy.getParamType(1)->isPointerTy());
803   case LibFunc::fscanf:
804   case LibFunc::fprintf:
805     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
806             FTy.getParamType(1)->isPointerTy());
807   case LibFunc::fgetpos:
808     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
809             FTy.getParamType(1)->isPointerTy());
810   case LibFunc::gets:
811   case LibFunc::getchar:
812   case LibFunc::getitimer:
813     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
814   case LibFunc::ungetc:
815     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
816   case LibFunc::utime:
817   case LibFunc::utimes:
818     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
819             FTy.getParamType(1)->isPointerTy());
820   case LibFunc::putc:
821     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
822   case LibFunc::pread:
823   case LibFunc::pwrite:
824     return (NumParams == 4 && FTy.getParamType(1)->isPointerTy());
825   case LibFunc::popen:
826     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
827             FTy.getParamType(0)->isPointerTy() &&
828             FTy.getParamType(1)->isPointerTy());
829   case LibFunc::vscanf:
830     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
831   case LibFunc::vsscanf:
832     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
833             FTy.getParamType(2)->isPointerTy());
834   case LibFunc::vfscanf:
835     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
836             FTy.getParamType(2)->isPointerTy());
837   case LibFunc::valloc:
838     return (FTy.getReturnType()->isPointerTy());
839   case LibFunc::vprintf:
840     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
841   case LibFunc::vfprintf:
842   case LibFunc::vsprintf:
843     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
844             FTy.getParamType(1)->isPointerTy());
845   case LibFunc::vsnprintf:
846     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
847             FTy.getParamType(2)->isPointerTy());
848   case LibFunc::open:
849     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
850   case LibFunc::opendir:
851     return (NumParams == 1 && FTy.getReturnType()->isPointerTy() &&
852             FTy.getParamType(0)->isPointerTy());
853   case LibFunc::tmpfile:
854     return (FTy.getReturnType()->isPointerTy());
855   case LibFunc::htonl:
856   case LibFunc::htons:
857   case LibFunc::ntohl:
858   case LibFunc::ntohs:
859   case LibFunc::lstat:
860     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
861             FTy.getParamType(1)->isPointerTy());
862   case LibFunc::lchown:
863     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
864   case LibFunc::qsort:
865     return (NumParams == 4 && FTy.getParamType(3)->isPointerTy());
866   case LibFunc::dunder_strdup:
867   case LibFunc::dunder_strndup:
868     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
869             FTy.getParamType(0)->isPointerTy());
870   case LibFunc::dunder_strtok_r:
871     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
872   case LibFunc::under_IO_putc:
873     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
874   case LibFunc::dunder_isoc99_scanf:
875     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
876   case LibFunc::stat64:
877   case LibFunc::lstat64:
878   case LibFunc::statvfs64:
879     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
880             FTy.getParamType(1)->isPointerTy());
881   case LibFunc::dunder_isoc99_sscanf:
882     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
883             FTy.getParamType(1)->isPointerTy());
884   case LibFunc::fopen64:
885     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
886             FTy.getParamType(0)->isPointerTy() &&
887             FTy.getParamType(1)->isPointerTy());
888   case LibFunc::tmpfile64:
889     return (FTy.getReturnType()->isPointerTy());
890   case LibFunc::fstat64:
891   case LibFunc::fstatvfs64:
892     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
893   case LibFunc::open64:
894     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
895   case LibFunc::gettimeofday:
896     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
897             FTy.getParamType(1)->isPointerTy());
898
899   case LibFunc::Znwj:                    // new(unsigned int);
900   case LibFunc::Znwm:                    // new(unsigned long);
901   case LibFunc::Znaj:                    // new[](unsigned int);
902   case LibFunc::Znam:                    // new[](unsigned long);
903   case LibFunc::msvc_new_int:            // new(unsigned int);
904   case LibFunc::msvc_new_longlong:       // new(unsigned long long);
905   case LibFunc::msvc_new_array_int:      // new[](unsigned int);
906   case LibFunc::msvc_new_array_longlong: // new[](unsigned long long);
907     return (NumParams == 1);
908
909   case LibFunc::memset_pattern16:
910     return (!FTy.isVarArg() && NumParams == 3 &&
911             isa<PointerType>(FTy.getParamType(0)) &&
912             isa<PointerType>(FTy.getParamType(1)) &&
913             isa<IntegerType>(FTy.getParamType(2)));
914
915   // int __nvvm_reflect(const char *);
916   case LibFunc::nvvm_reflect:
917     return (NumParams == 1 && isa<PointerType>(FTy.getParamType(0)));
918
919   case LibFunc::sin:
920   case LibFunc::sinf:
921   case LibFunc::sinl:
922   case LibFunc::cos:
923   case LibFunc::cosf:
924   case LibFunc::cosl:
925   case LibFunc::tan:
926   case LibFunc::tanf:
927   case LibFunc::tanl:
928   case LibFunc::exp:
929   case LibFunc::expf:
930   case LibFunc::expl:
931   case LibFunc::exp2:
932   case LibFunc::exp2f:
933   case LibFunc::exp2l:
934   case LibFunc::log:
935   case LibFunc::logf:
936   case LibFunc::logl:
937   case LibFunc::log10:
938   case LibFunc::log10f:
939   case LibFunc::log10l:
940   case LibFunc::log2:
941   case LibFunc::log2f:
942   case LibFunc::log2l:
943   case LibFunc::fabs:
944   case LibFunc::fabsf:
945   case LibFunc::fabsl:
946   case LibFunc::floor:
947   case LibFunc::floorf:
948   case LibFunc::floorl:
949   case LibFunc::ceil:
950   case LibFunc::ceilf:
951   case LibFunc::ceill:
952   case LibFunc::trunc:
953   case LibFunc::truncf:
954   case LibFunc::truncl:
955   case LibFunc::rint:
956   case LibFunc::rintf:
957   case LibFunc::rintl:
958   case LibFunc::nearbyint:
959   case LibFunc::nearbyintf:
960   case LibFunc::nearbyintl:
961   case LibFunc::round:
962   case LibFunc::roundf:
963   case LibFunc::roundl:
964   case LibFunc::sqrt:
965   case LibFunc::sqrtf:
966   case LibFunc::sqrtl:
967     return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() &&
968             FTy.getReturnType() == FTy.getParamType(0));
969
970   case LibFunc::fmin:
971   case LibFunc::fminf:
972   case LibFunc::fminl:
973   case LibFunc::fmax:
974   case LibFunc::fmaxf:
975   case LibFunc::fmaxl:
976   case LibFunc::copysign:
977   case LibFunc::copysignf:
978   case LibFunc::copysignl:
979   case LibFunc::pow:
980   case LibFunc::powf:
981   case LibFunc::powl:
982     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
983             FTy.getReturnType() == FTy.getParamType(0) &&
984             FTy.getReturnType() == FTy.getParamType(1));
985
986   case LibFunc::ffs:
987   case LibFunc::ffsl:
988   case LibFunc::ffsll:
989   case LibFunc::fls:
990   case LibFunc::flsl:
991   case LibFunc::flsll:
992     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
993             FTy.getParamType(0)->isIntegerTy());
994
995   case LibFunc::isdigit:
996   case LibFunc::isascii:
997   case LibFunc::toascii:
998     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
999             FTy.getReturnType() == FTy.getParamType(0));
1000
1001   case LibFunc::abs:
1002   case LibFunc::labs:
1003   case LibFunc::llabs:
1004     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() &&
1005             FTy.getReturnType() == FTy.getParamType(0));
1006
1007   case LibFunc::cxa_atexit:
1008     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() &&
1009             FTy.getParamType(0)->isPointerTy() &&
1010             FTy.getParamType(1)->isPointerTy() &&
1011             FTy.getParamType(2)->isPointerTy());
1012
1013   case LibFunc::sinpi:
1014   case LibFunc::cospi:
1015     return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() &&
1016             FTy.getReturnType() == FTy.getParamType(0));
1017
1018   case LibFunc::sinpif:
1019   case LibFunc::cospif:
1020     return (NumParams == 1 && FTy.getReturnType()->isFloatTy() &&
1021             FTy.getReturnType() == FTy.getParamType(0));
1022
1023   default:
1024     // Assume the other functions are correct.
1025     // FIXME: It'd be really nice to cover them all.
1026     return true;
1027   }
1028 }
1029
1030 bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
1031                                        LibFunc::Func &F) const {
1032   const DataLayout *DL =
1033       FDecl.getParent() ? &FDecl.getParent()->getDataLayout() : nullptr;
1034   return getLibFunc(FDecl.getName(), F) &&
1035          isValidProtoForLibFunc(*FDecl.getFunctionType(), F, DL);
1036 }
1037
1038 void TargetLibraryInfoImpl::disableAllFunctions() {
1039   memset(AvailableArray, 0, sizeof(AvailableArray));
1040 }
1041
1042 static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
1043   return LHS.ScalarFnName < RHS.ScalarFnName;
1044 }
1045
1046 static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
1047   return LHS.VectorFnName < RHS.VectorFnName;
1048 }
1049
1050 static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
1051   return LHS.ScalarFnName < S;
1052 }
1053
1054 static bool compareWithVectorFnName(const VecDesc &LHS, StringRef S) {
1055   return LHS.VectorFnName < S;
1056 }
1057
1058 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
1059   VectorDescs.insert(VectorDescs.end(), Fns.begin(), Fns.end());
1060   std::sort(VectorDescs.begin(), VectorDescs.end(), compareByScalarFnName);
1061
1062   ScalarDescs.insert(ScalarDescs.end(), Fns.begin(), Fns.end());
1063   std::sort(ScalarDescs.begin(), ScalarDescs.end(), compareByVectorFnName);
1064 }
1065
1066 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
1067     enum VectorLibrary VecLib) {
1068   switch (VecLib) {
1069   case Accelerate: {
1070     const VecDesc VecFuncs[] = {
1071         // Floating-Point Arithmetic and Auxiliary Functions
1072         {"ceilf", "vceilf", 4},
1073         {"fabsf", "vfabsf", 4},
1074         {"llvm.fabs.f32", "vfabsf", 4},
1075         {"floorf", "vfloorf", 4},
1076         {"sqrtf", "vsqrtf", 4},
1077         {"llvm.sqrt.f32", "vsqrtf", 4},
1078
1079         // Exponential and Logarithmic Functions
1080         {"expf", "vexpf", 4},
1081         {"llvm.exp.f32", "vexpf", 4},
1082         {"expm1f", "vexpm1f", 4},
1083         {"logf", "vlogf", 4},
1084         {"llvm.log.f32", "vlogf", 4},
1085         {"log1pf", "vlog1pf", 4},
1086         {"log10f", "vlog10f", 4},
1087         {"llvm.log10.f32", "vlog10f", 4},
1088         {"logbf", "vlogbf", 4},
1089
1090         // Trigonometric Functions
1091         {"sinf", "vsinf", 4},
1092         {"llvm.sin.f32", "vsinf", 4},
1093         {"cosf", "vcosf", 4},
1094         {"llvm.cos.f32", "vcosf", 4},
1095         {"tanf", "vtanf", 4},
1096         {"asinf", "vasinf", 4},
1097         {"acosf", "vacosf", 4},
1098         {"atanf", "vatanf", 4},
1099
1100         // Hyperbolic Functions
1101         {"sinhf", "vsinhf", 4},
1102         {"coshf", "vcoshf", 4},
1103         {"tanhf", "vtanhf", 4},
1104         {"asinhf", "vasinhf", 4},
1105         {"acoshf", "vacoshf", 4},
1106         {"atanhf", "vatanhf", 4},
1107     };
1108     addVectorizableFunctions(VecFuncs);
1109     break;
1110   }
1111   case SVML: {
1112     const VecDesc VecFuncs[] = {
1113         {"sin", "__svml_sin2", 2},
1114         {"sin", "__svml_sin4", 4},
1115         {"sin", "__svml_sin8", 8},
1116
1117         {"sinf", "__svml_sinf4", 4},
1118         {"sinf", "__svml_sinf8", 8},
1119         {"sinf", "__svml_sinf16", 16},
1120
1121         {"cos", "__svml_cos2", 2},
1122         {"cos", "__svml_cos4", 4},
1123         {"cos", "__svml_cos8", 8},
1124
1125         {"cosf", "__svml_cosf4", 4},
1126         {"cosf", "__svml_cosf8", 8},
1127         {"cosf", "__svml_cosf16", 16},
1128
1129         {"pow", "__svml_pow2", 2},
1130         {"pow", "__svml_pow4", 4},
1131         {"pow", "__svml_pow8", 8},
1132
1133         {"powf", "__svml_powf4", 4},
1134         {"powf", "__svml_powf8", 8},
1135         {"powf", "__svml_powf16", 16},
1136
1137         {"llvm.pow.f64", "__svml_pow2", 2},
1138         {"llvm.pow.f64", "__svml_pow4", 4},
1139         {"llvm.pow.f64", "__svml_pow8", 8},
1140
1141         {"llvm.pow.f32", "__svml_powf4", 4},
1142         {"llvm.pow.f32", "__svml_powf8", 8},
1143         {"llvm.pow.f32", "__svml_powf16", 16},
1144
1145         {"exp", "__svml_exp2", 2},
1146         {"exp", "__svml_exp4", 4},
1147         {"exp", "__svml_exp8", 8},
1148
1149         {"expf", "__svml_expf4", 4},
1150         {"expf", "__svml_expf8", 8},
1151         {"expf", "__svml_expf16", 16},
1152
1153         {"llvm.exp.f64", "__svml_exp2", 2},
1154         {"llvm.exp.f64", "__svml_exp4", 4},
1155         {"llvm.exp.f64", "__svml_exp8", 8},
1156
1157         {"llvm.exp.f32", "__svml_expf4", 4},
1158         {"llvm.exp.f32", "__svml_expf8", 8},
1159         {"llvm.exp.f32", "__svml_expf16", 16},
1160
1161         {"log", "__svml_log2", 2},
1162         {"log", "__svml_log4", 4},
1163         {"log", "__svml_log8", 8},
1164
1165         {"logf", "__svml_logf4", 4},
1166         {"logf", "__svml_logf8", 8},
1167         {"logf", "__svml_logf16", 16},
1168
1169         {"llvm.log.f64", "__svml_log2", 2},
1170         {"llvm.log.f64", "__svml_log4", 4},
1171         {"llvm.log.f64", "__svml_log8", 8},
1172
1173         {"llvm.log.f32", "__svml_logf4", 4},
1174         {"llvm.log.f32", "__svml_logf8", 8},
1175         {"llvm.log.f32", "__svml_logf16", 16},
1176     };
1177     addVectorizableFunctions(VecFuncs);
1178     break;
1179   }
1180   case NoLibrary:
1181     break;
1182   }
1183 }
1184
1185 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
1186   funcName = sanitizeFunctionName(funcName);
1187   if (funcName.empty())
1188     return false;
1189
1190   std::vector<VecDesc>::const_iterator I = std::lower_bound(
1191       VectorDescs.begin(), VectorDescs.end(), funcName,
1192       compareWithScalarFnName);
1193   return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
1194 }
1195
1196 StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
1197                                                        unsigned VF) const {
1198   F = sanitizeFunctionName(F);
1199   if (F.empty())
1200     return F;
1201   std::vector<VecDesc>::const_iterator I = std::lower_bound(
1202       VectorDescs.begin(), VectorDescs.end(), F, compareWithScalarFnName);
1203   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
1204     if (I->VectorizationFactor == VF)
1205       return I->VectorFnName;
1206     ++I;
1207   }
1208   return StringRef();
1209 }
1210
1211 StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F,
1212                                                        unsigned &VF) const {
1213   F = sanitizeFunctionName(F);
1214   if (F.empty())
1215     return F;
1216
1217   std::vector<VecDesc>::const_iterator I = std::lower_bound(
1218       ScalarDescs.begin(), ScalarDescs.end(), F, compareWithVectorFnName);
1219   if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F)
1220     return StringRef();
1221   VF = I->VectorizationFactor;
1222   return I->ScalarFnName;
1223 }
1224
1225 TargetLibraryInfo TargetLibraryAnalysis::run(Module &M,
1226                                              ModuleAnalysisManager &) {
1227   if (PresetInfoImpl)
1228     return TargetLibraryInfo(*PresetInfoImpl);
1229
1230   return TargetLibraryInfo(lookupInfoImpl(Triple(M.getTargetTriple())));
1231 }
1232
1233 TargetLibraryInfo TargetLibraryAnalysis::run(Function &F,
1234                                              FunctionAnalysisManager &) {
1235   if (PresetInfoImpl)
1236     return TargetLibraryInfo(*PresetInfoImpl);
1237
1238   return TargetLibraryInfo(
1239       lookupInfoImpl(Triple(F.getParent()->getTargetTriple())));
1240 }
1241
1242 TargetLibraryInfoImpl &TargetLibraryAnalysis::lookupInfoImpl(const Triple &T) {
1243   std::unique_ptr<TargetLibraryInfoImpl> &Impl =
1244       Impls[T.normalize()];
1245   if (!Impl)
1246     Impl.reset(new TargetLibraryInfoImpl(T));
1247
1248   return *Impl;
1249 }
1250
1251
1252 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
1253     : ImmutablePass(ID), TLIImpl(), TLI(TLIImpl) {
1254   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1255 }
1256
1257 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
1258     : ImmutablePass(ID), TLIImpl(T), TLI(TLIImpl) {
1259   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1260 }
1261
1262 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
1263     const TargetLibraryInfoImpl &TLIImpl)
1264     : ImmutablePass(ID), TLIImpl(TLIImpl), TLI(this->TLIImpl) {
1265   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1266 }
1267
1268 AnalysisKey TargetLibraryAnalysis::Key;
1269
1270 // Register the basic pass.
1271 INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
1272                 "Target Library Information", false, true)
1273 char TargetLibraryInfoWrapperPass::ID = 0;
1274
1275 void TargetLibraryInfoWrapperPass::anchor() {}