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::siprintf:
642   case LibFunc::sprintf:
643     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
644             FTy.getParamType(1)->isPointerTy());
645   case LibFunc::snprintf:
646     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
647             FTy.getParamType(2)->isPointerTy());
648   case LibFunc::setitimer:
649     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
650             FTy.getParamType(2)->isPointerTy());
651   case LibFunc::system:
652     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
653   case LibFunc::malloc:
654     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
655   case LibFunc::memcmp:
656     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
657             FTy.getParamType(0)->isPointerTy() &&
658             FTy.getParamType(1)->isPointerTy());
659 
660   case LibFunc::memchr:
661   case LibFunc::memrchr:
662     return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
663             FTy.getReturnType() == FTy.getParamType(0) &&
664             FTy.getParamType(1)->isIntegerTy(32) &&
665             IsSizeTTy(FTy.getParamType(2)));
666   case LibFunc::modf:
667   case LibFunc::modff:
668   case LibFunc::modfl:
669     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
670 
671   case LibFunc::memcpy_chk:
672   case LibFunc::memmove_chk:
673     --NumParams;
674     if (!IsSizeTTy(FTy.getParamType(NumParams)))
675       return false;
676     LLVM_FALLTHROUGH;
677   case LibFunc::memcpy:
678   case LibFunc::mempcpy:
679   case LibFunc::memmove:
680     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
681             FTy.getParamType(0)->isPointerTy() &&
682             FTy.getParamType(1)->isPointerTy() &&
683             IsSizeTTy(FTy.getParamType(2)));
684 
685   case LibFunc::memset_chk:
686     --NumParams;
687     if (!IsSizeTTy(FTy.getParamType(NumParams)))
688       return false;
689     LLVM_FALLTHROUGH;
690   case LibFunc::memset:
691     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
692             FTy.getParamType(0)->isPointerTy() &&
693             FTy.getParamType(1)->isIntegerTy() &&
694             IsSizeTTy(FTy.getParamType(2)));
695 
696   case LibFunc::memccpy:
697     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
698   case LibFunc::memalign:
699     return (FTy.getReturnType()->isPointerTy());
700   case LibFunc::realloc:
701   case LibFunc::reallocf:
702     return (NumParams == 2 && FTy.getReturnType() == PCharTy &&
703             FTy.getParamType(0) == FTy.getReturnType() &&
704             IsSizeTTy(FTy.getParamType(1)));
705   case LibFunc::read:
706     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
707   case LibFunc::rewind:
708   case LibFunc::rmdir:
709   case LibFunc::remove:
710   case LibFunc::realpath:
711     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
712   case LibFunc::rename:
713     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
714             FTy.getParamType(1)->isPointerTy());
715   case LibFunc::readlink:
716     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
717             FTy.getParamType(1)->isPointerTy());
718   case LibFunc::write:
719     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
720   case LibFunc::bcopy:
721   case LibFunc::bcmp:
722     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
723             FTy.getParamType(1)->isPointerTy());
724   case LibFunc::bzero:
725     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
726   case LibFunc::calloc:
727     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
728 
729   case LibFunc::atof:
730   case LibFunc::atoi:
731   case LibFunc::atol:
732   case LibFunc::atoll:
733   case LibFunc::ferror:
734   case LibFunc::getenv:
735   case LibFunc::getpwnam:
736   case LibFunc::iprintf:
737   case LibFunc::pclose:
738   case LibFunc::perror:
739   case LibFunc::printf:
740   case LibFunc::puts:
741   case LibFunc::uname:
742   case LibFunc::under_IO_getc:
743   case LibFunc::unlink:
744   case LibFunc::unsetenv:
745     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
746 
747   case LibFunc::access:
748   case LibFunc::chmod:
749   case LibFunc::chown:
750   case LibFunc::clearerr:
751   case LibFunc::closedir:
752   case LibFunc::ctermid:
753   case LibFunc::fclose:
754   case LibFunc::feof:
755   case LibFunc::fflush:
756   case LibFunc::fgetc:
757   case LibFunc::fileno:
758   case LibFunc::flockfile:
759   case LibFunc::free:
760   case LibFunc::fseek:
761   case LibFunc::fseeko64:
762   case LibFunc::fseeko:
763   case LibFunc::fsetpos:
764   case LibFunc::ftell:
765   case LibFunc::ftello64:
766   case LibFunc::ftello:
767   case LibFunc::ftrylockfile:
768   case LibFunc::funlockfile:
769   case LibFunc::getc:
770   case LibFunc::getc_unlocked:
771   case LibFunc::getlogin_r:
772   case LibFunc::mkdir:
773   case LibFunc::mktime:
774   case LibFunc::times:
775     return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
776 
777   case LibFunc::fopen:
778     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
779             FTy.getParamType(0)->isPointerTy() &&
780             FTy.getParamType(1)->isPointerTy());
781   case LibFunc::fdopen:
782     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
783             FTy.getParamType(1)->isPointerTy());
784   case LibFunc::fputc:
785   case LibFunc::fstat:
786   case LibFunc::frexp:
787   case LibFunc::frexpf:
788   case LibFunc::frexpl:
789   case LibFunc::fstatvfs:
790     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
791   case LibFunc::fgets:
792     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
793             FTy.getParamType(2)->isPointerTy());
794   case LibFunc::fread:
795     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
796             FTy.getParamType(3)->isPointerTy());
797   case LibFunc::fwrite:
798     return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() &&
799             FTy.getParamType(0)->isPointerTy() &&
800             FTy.getParamType(1)->isIntegerTy() &&
801             FTy.getParamType(2)->isIntegerTy() &&
802             FTy.getParamType(3)->isPointerTy());
803   case LibFunc::fputs:
804     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
805             FTy.getParamType(1)->isPointerTy());
806   case LibFunc::fscanf:
807   case LibFunc::fiprintf:
808   case LibFunc::fprintf:
809     return (NumParams >= 2 && FTy.getReturnType()->isIntegerTy() &&
810             FTy.getParamType(0)->isPointerTy() &&
811             FTy.getParamType(1)->isPointerTy());
812   case LibFunc::fgetpos:
813     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
814             FTy.getParamType(1)->isPointerTy());
815   case LibFunc::getchar:
816     return (NumParams == 0 && FTy.getReturnType()->isIntegerTy());
817   case LibFunc::gets:
818     return (NumParams == 1 && FTy.getParamType(0) == PCharTy);
819   case LibFunc::getitimer:
820     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
821   case LibFunc::ungetc:
822     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
823   case LibFunc::utime:
824   case LibFunc::utimes:
825     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
826             FTy.getParamType(1)->isPointerTy());
827   case LibFunc::putc:
828     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
829   case LibFunc::pread:
830   case LibFunc::pwrite:
831     return (NumParams == 4 && FTy.getParamType(1)->isPointerTy());
832   case LibFunc::popen:
833     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
834             FTy.getParamType(0)->isPointerTy() &&
835             FTy.getParamType(1)->isPointerTy());
836   case LibFunc::vscanf:
837     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
838   case LibFunc::vsscanf:
839     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
840             FTy.getParamType(2)->isPointerTy());
841   case LibFunc::vfscanf:
842     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
843             FTy.getParamType(2)->isPointerTy());
844   case LibFunc::valloc:
845     return (FTy.getReturnType()->isPointerTy());
846   case LibFunc::vprintf:
847     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
848   case LibFunc::vfprintf:
849   case LibFunc::vsprintf:
850     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
851             FTy.getParamType(1)->isPointerTy());
852   case LibFunc::vsnprintf:
853     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
854             FTy.getParamType(2)->isPointerTy());
855   case LibFunc::open:
856     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
857   case LibFunc::opendir:
858     return (NumParams == 1 && FTy.getReturnType()->isPointerTy() &&
859             FTy.getParamType(0)->isPointerTy());
860   case LibFunc::tmpfile:
861     return (FTy.getReturnType()->isPointerTy());
862   case LibFunc::htonl:
863   case LibFunc::ntohl:
864     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
865             FTy.getReturnType() == FTy.getParamType(0));
866   case LibFunc::htons:
867   case LibFunc::ntohs:
868     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(16) &&
869             FTy.getReturnType() == FTy.getParamType(0));
870   case LibFunc::lstat:
871     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
872             FTy.getParamType(1)->isPointerTy());
873   case LibFunc::lchown:
874     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
875   case LibFunc::qsort:
876     return (NumParams == 4 && FTy.getParamType(3)->isPointerTy());
877   case LibFunc::dunder_strdup:
878   case LibFunc::dunder_strndup:
879     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
880             FTy.getParamType(0)->isPointerTy());
881   case LibFunc::dunder_strtok_r:
882     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
883   case LibFunc::under_IO_putc:
884     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
885   case LibFunc::dunder_isoc99_scanf:
886     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
887   case LibFunc::stat64:
888   case LibFunc::lstat64:
889   case LibFunc::statvfs64:
890     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
891             FTy.getParamType(1)->isPointerTy());
892   case LibFunc::dunder_isoc99_sscanf:
893     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
894             FTy.getParamType(1)->isPointerTy());
895   case LibFunc::fopen64:
896     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
897             FTy.getParamType(0)->isPointerTy() &&
898             FTy.getParamType(1)->isPointerTy());
899   case LibFunc::tmpfile64:
900     return (FTy.getReturnType()->isPointerTy());
901   case LibFunc::fstat64:
902   case LibFunc::fstatvfs64:
903     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
904   case LibFunc::open64:
905     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
906   case LibFunc::gettimeofday:
907     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
908             FTy.getParamType(1)->isPointerTy());
909 
910   // new(unsigned int);
911   case LibFunc::Znwj:
912   // new(unsigned long);
913   case LibFunc::Znwm:
914   // new[](unsigned int);
915   case LibFunc::Znaj:
916   // new[](unsigned long);
917   case LibFunc::Znam:
918   // new(unsigned int);
919   case LibFunc::msvc_new_int:
920   // new(unsigned long long);
921   case LibFunc::msvc_new_longlong:
922   // new[](unsigned int);
923   case LibFunc::msvc_new_array_int:
924   // new[](unsigned long long);
925   case LibFunc::msvc_new_array_longlong:
926     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
927 
928   // new(unsigned int, nothrow);
929   case LibFunc::ZnwjRKSt9nothrow_t:
930   // new(unsigned long, nothrow);
931   case LibFunc::ZnwmRKSt9nothrow_t:
932   // new[](unsigned int, nothrow);
933   case LibFunc::ZnajRKSt9nothrow_t:
934   // new[](unsigned long, nothrow);
935   case LibFunc::ZnamRKSt9nothrow_t:
936   // new(unsigned int, nothrow);
937   case LibFunc::msvc_new_int_nothrow:
938   // new(unsigned long long, nothrow);
939   case LibFunc::msvc_new_longlong_nothrow:
940   // new[](unsigned int, nothrow);
941   case LibFunc::msvc_new_array_int_nothrow:
942   // new[](unsigned long long, nothrow);
943   case LibFunc::msvc_new_array_longlong_nothrow:
944     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
945 
946   // void operator delete[](void*);
947   case LibFunc::ZdaPv:
948   // void operator delete(void*);
949   case LibFunc::ZdlPv:
950   // void operator delete[](void*);
951   case LibFunc::msvc_delete_array_ptr32:
952   // void operator delete[](void*);
953   case LibFunc::msvc_delete_array_ptr64:
954   // void operator delete(void*);
955   case LibFunc::msvc_delete_ptr32:
956   // void operator delete(void*);
957   case LibFunc::msvc_delete_ptr64:
958     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
959 
960   // void operator delete[](void*, nothrow);
961   case LibFunc::ZdaPvRKSt9nothrow_t:
962   // void operator delete[](void*, unsigned int);
963   case LibFunc::ZdaPvj:
964   // void operator delete[](void*, unsigned long);
965   case LibFunc::ZdaPvm:
966   // void operator delete(void*, nothrow);
967   case LibFunc::ZdlPvRKSt9nothrow_t:
968   // void operator delete(void*, unsigned int);
969   case LibFunc::ZdlPvj:
970   // void operator delete(void*, unsigned long);
971   case LibFunc::ZdlPvm:
972   // void operator delete[](void*, unsigned int);
973   case LibFunc::msvc_delete_array_ptr32_int:
974   // void operator delete[](void*, nothrow);
975   case LibFunc::msvc_delete_array_ptr32_nothrow:
976   // void operator delete[](void*, unsigned long long);
977   case LibFunc::msvc_delete_array_ptr64_longlong:
978   // void operator delete[](void*, nothrow);
979   case LibFunc::msvc_delete_array_ptr64_nothrow:
980   // void operator delete(void*, unsigned int);
981   case LibFunc::msvc_delete_ptr32_int:
982   // void operator delete(void*, nothrow);
983   case LibFunc::msvc_delete_ptr32_nothrow:
984   // void operator delete(void*, unsigned long long);
985   case LibFunc::msvc_delete_ptr64_longlong:
986   // void operator delete(void*, nothrow);
987   case LibFunc::msvc_delete_ptr64_nothrow:
988     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
989 
990   case LibFunc::memset_pattern16:
991     return (!FTy.isVarArg() && NumParams == 3 &&
992             FTy.getParamType(0)->isPointerTy() &&
993             FTy.getParamType(1)->isPointerTy() &&
994             FTy.getParamType(2)->isIntegerTy());
995 
996   case LibFunc::cxa_guard_abort:
997   case LibFunc::cxa_guard_acquire:
998   case LibFunc::cxa_guard_release:
999   case LibFunc::nvvm_reflect:
1000     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1001 
1002   case LibFunc::sincospi_stret:
1003   case LibFunc::sincospif_stret:
1004     return (NumParams == 1 && FTy.getParamType(0)->isFloatingPointTy());
1005 
1006   case LibFunc::acos:
1007   case LibFunc::acosf:
1008   case LibFunc::acosh:
1009   case LibFunc::acoshf:
1010   case LibFunc::acoshl:
1011   case LibFunc::acosl:
1012   case LibFunc::asin:
1013   case LibFunc::asinf:
1014   case LibFunc::asinh:
1015   case LibFunc::asinhf:
1016   case LibFunc::asinhl:
1017   case LibFunc::asinl:
1018   case LibFunc::atan:
1019   case LibFunc::atanf:
1020   case LibFunc::atanh:
1021   case LibFunc::atanhf:
1022   case LibFunc::atanhl:
1023   case LibFunc::atanl:
1024   case LibFunc::cbrt:
1025   case LibFunc::cbrtf:
1026   case LibFunc::cbrtl:
1027   case LibFunc::ceil:
1028   case LibFunc::ceilf:
1029   case LibFunc::ceill:
1030   case LibFunc::cos:
1031   case LibFunc::cosf:
1032   case LibFunc::cosh:
1033   case LibFunc::coshf:
1034   case LibFunc::coshl:
1035   case LibFunc::cosl:
1036   case LibFunc::exp10:
1037   case LibFunc::exp10f:
1038   case LibFunc::exp10l:
1039   case LibFunc::exp2:
1040   case LibFunc::exp2f:
1041   case LibFunc::exp2l:
1042   case LibFunc::exp:
1043   case LibFunc::expf:
1044   case LibFunc::expl:
1045   case LibFunc::expm1:
1046   case LibFunc::expm1f:
1047   case LibFunc::expm1l:
1048   case LibFunc::fabs:
1049   case LibFunc::fabsf:
1050   case LibFunc::fabsl:
1051   case LibFunc::floor:
1052   case LibFunc::floorf:
1053   case LibFunc::floorl:
1054   case LibFunc::log10:
1055   case LibFunc::log10f:
1056   case LibFunc::log10l:
1057   case LibFunc::log1p:
1058   case LibFunc::log1pf:
1059   case LibFunc::log1pl:
1060   case LibFunc::log2:
1061   case LibFunc::log2f:
1062   case LibFunc::log2l:
1063   case LibFunc::log:
1064   case LibFunc::logb:
1065   case LibFunc::logbf:
1066   case LibFunc::logbl:
1067   case LibFunc::logf:
1068   case LibFunc::logl:
1069   case LibFunc::nearbyint:
1070   case LibFunc::nearbyintf:
1071   case LibFunc::nearbyintl:
1072   case LibFunc::rint:
1073   case LibFunc::rintf:
1074   case LibFunc::rintl:
1075   case LibFunc::round:
1076   case LibFunc::roundf:
1077   case LibFunc::roundl:
1078   case LibFunc::sin:
1079   case LibFunc::sinf:
1080   case LibFunc::sinh:
1081   case LibFunc::sinhf:
1082   case LibFunc::sinhl:
1083   case LibFunc::sinl:
1084   case LibFunc::sqrt:
1085   case LibFunc::sqrt_finite:
1086   case LibFunc::sqrtf:
1087   case LibFunc::sqrtf_finite:
1088   case LibFunc::sqrtl:
1089   case LibFunc::sqrtl_finite:
1090   case LibFunc::tan:
1091   case LibFunc::tanf:
1092   case LibFunc::tanh:
1093   case LibFunc::tanhf:
1094   case LibFunc::tanhl:
1095   case LibFunc::tanl:
1096   case LibFunc::trunc:
1097   case LibFunc::truncf:
1098   case LibFunc::truncl:
1099     return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() &&
1100             FTy.getReturnType() == FTy.getParamType(0));
1101 
1102   case LibFunc::atan2:
1103   case LibFunc::atan2f:
1104   case LibFunc::atan2l:
1105   case LibFunc::fmin:
1106   case LibFunc::fminf:
1107   case LibFunc::fminl:
1108   case LibFunc::fmax:
1109   case LibFunc::fmaxf:
1110   case LibFunc::fmaxl:
1111   case LibFunc::fmod:
1112   case LibFunc::fmodf:
1113   case LibFunc::fmodl:
1114   case LibFunc::copysign:
1115   case LibFunc::copysignf:
1116   case LibFunc::copysignl:
1117   case LibFunc::pow:
1118   case LibFunc::powf:
1119   case LibFunc::powl:
1120     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
1121             FTy.getReturnType() == FTy.getParamType(0) &&
1122             FTy.getReturnType() == FTy.getParamType(1));
1123 
1124   case LibFunc::ldexp:
1125   case LibFunc::ldexpf:
1126   case LibFunc::ldexpl:
1127     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
1128             FTy.getReturnType() == FTy.getParamType(0) &&
1129             FTy.getParamType(1)->isIntegerTy(32));
1130 
1131   case LibFunc::ffs:
1132   case LibFunc::ffsl:
1133   case LibFunc::ffsll:
1134   case LibFunc::fls:
1135   case LibFunc::flsl:
1136   case LibFunc::flsll:
1137     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1138             FTy.getParamType(0)->isIntegerTy());
1139 
1140   case LibFunc::isdigit:
1141   case LibFunc::isascii:
1142   case LibFunc::toascii:
1143   case LibFunc::putchar:
1144     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1145             FTy.getReturnType() == FTy.getParamType(0));
1146 
1147   case LibFunc::abs:
1148   case LibFunc::labs:
1149   case LibFunc::llabs:
1150     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() &&
1151             FTy.getReturnType() == FTy.getParamType(0));
1152 
1153   case LibFunc::cxa_atexit:
1154     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() &&
1155             FTy.getParamType(0)->isPointerTy() &&
1156             FTy.getParamType(1)->isPointerTy() &&
1157             FTy.getParamType(2)->isPointerTy());
1158 
1159   case LibFunc::sinpi:
1160   case LibFunc::cospi:
1161     return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() &&
1162             FTy.getReturnType() == FTy.getParamType(0));
1163 
1164   case LibFunc::sinpif:
1165   case LibFunc::cospif:
1166     return (NumParams == 1 && FTy.getReturnType()->isFloatTy() &&
1167             FTy.getReturnType() == FTy.getParamType(0));
1168 
1169   case LibFunc::strnlen:
1170     return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(1) &&
1171             FTy.getParamType(0) == PCharTy &&
1172             FTy.getParamType(1) == SizeTTy);
1173 
1174   case LibFunc::posix_memalign:
1175     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
1176             FTy.getParamType(0)->isPointerTy() &&
1177             FTy.getParamType(1) == SizeTTy && FTy.getParamType(2) == SizeTTy);
1178 
1179   case LibFunc::NumLibFuncs:
1180     break;
1181   }
1182 
1183   llvm_unreachable("Invalid libfunc");
1184 }
1185 
1186 bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
1187                                        LibFunc::Func &F) const {
1188   const DataLayout *DL =
1189       FDecl.getParent() ? &FDecl.getParent()->getDataLayout() : nullptr;
1190   return getLibFunc(FDecl.getName(), F) &&
1191          isValidProtoForLibFunc(*FDecl.getFunctionType(), F, DL);
1192 }
1193 
1194 void TargetLibraryInfoImpl::disableAllFunctions() {
1195   memset(AvailableArray, 0, sizeof(AvailableArray));
1196 }
1197 
1198 static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
1199   return LHS.ScalarFnName < RHS.ScalarFnName;
1200 }
1201 
1202 static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
1203   return LHS.VectorFnName < RHS.VectorFnName;
1204 }
1205 
1206 static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
1207   return LHS.ScalarFnName < S;
1208 }
1209 
1210 static bool compareWithVectorFnName(const VecDesc &LHS, StringRef S) {
1211   return LHS.VectorFnName < S;
1212 }
1213 
1214 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
1215   VectorDescs.insert(VectorDescs.end(), Fns.begin(), Fns.end());
1216   std::sort(VectorDescs.begin(), VectorDescs.end(), compareByScalarFnName);
1217 
1218   ScalarDescs.insert(ScalarDescs.end(), Fns.begin(), Fns.end());
1219   std::sort(ScalarDescs.begin(), ScalarDescs.end(), compareByVectorFnName);
1220 }
1221 
1222 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
1223     enum VectorLibrary VecLib) {
1224   switch (VecLib) {
1225   case Accelerate: {
1226     const VecDesc VecFuncs[] = {
1227         // Floating-Point Arithmetic and Auxiliary Functions
1228         {"ceilf", "vceilf", 4},
1229         {"fabsf", "vfabsf", 4},
1230         {"llvm.fabs.f32", "vfabsf", 4},
1231         {"floorf", "vfloorf", 4},
1232         {"sqrtf", "vsqrtf", 4},
1233         {"llvm.sqrt.f32", "vsqrtf", 4},
1234 
1235         // Exponential and Logarithmic Functions
1236         {"expf", "vexpf", 4},
1237         {"llvm.exp.f32", "vexpf", 4},
1238         {"expm1f", "vexpm1f", 4},
1239         {"logf", "vlogf", 4},
1240         {"llvm.log.f32", "vlogf", 4},
1241         {"log1pf", "vlog1pf", 4},
1242         {"log10f", "vlog10f", 4},
1243         {"llvm.log10.f32", "vlog10f", 4},
1244         {"logbf", "vlogbf", 4},
1245 
1246         // Trigonometric Functions
1247         {"sinf", "vsinf", 4},
1248         {"llvm.sin.f32", "vsinf", 4},
1249         {"cosf", "vcosf", 4},
1250         {"llvm.cos.f32", "vcosf", 4},
1251         {"tanf", "vtanf", 4},
1252         {"asinf", "vasinf", 4},
1253         {"acosf", "vacosf", 4},
1254         {"atanf", "vatanf", 4},
1255 
1256         // Hyperbolic Functions
1257         {"sinhf", "vsinhf", 4},
1258         {"coshf", "vcoshf", 4},
1259         {"tanhf", "vtanhf", 4},
1260         {"asinhf", "vasinhf", 4},
1261         {"acoshf", "vacoshf", 4},
1262         {"atanhf", "vatanhf", 4},
1263     };
1264     addVectorizableFunctions(VecFuncs);
1265     break;
1266   }
1267   case SVML: {
1268     const VecDesc VecFuncs[] = {
1269         {"sin", "__svml_sin2", 2},
1270         {"sin", "__svml_sin4", 4},
1271         {"sin", "__svml_sin8", 8},
1272 
1273         {"sinf", "__svml_sinf4", 4},
1274         {"sinf", "__svml_sinf8", 8},
1275         {"sinf", "__svml_sinf16", 16},
1276 
1277         {"cos", "__svml_cos2", 2},
1278         {"cos", "__svml_cos4", 4},
1279         {"cos", "__svml_cos8", 8},
1280 
1281         {"cosf", "__svml_cosf4", 4},
1282         {"cosf", "__svml_cosf8", 8},
1283         {"cosf", "__svml_cosf16", 16},
1284 
1285         {"pow", "__svml_pow2", 2},
1286         {"pow", "__svml_pow4", 4},
1287         {"pow", "__svml_pow8", 8},
1288 
1289         {"powf", "__svml_powf4", 4},
1290         {"powf", "__svml_powf8", 8},
1291         {"powf", "__svml_powf16", 16},
1292 
1293         {"llvm.pow.f64", "__svml_pow2", 2},
1294         {"llvm.pow.f64", "__svml_pow4", 4},
1295         {"llvm.pow.f64", "__svml_pow8", 8},
1296 
1297         {"llvm.pow.f32", "__svml_powf4", 4},
1298         {"llvm.pow.f32", "__svml_powf8", 8},
1299         {"llvm.pow.f32", "__svml_powf16", 16},
1300 
1301         {"exp", "__svml_exp2", 2},
1302         {"exp", "__svml_exp4", 4},
1303         {"exp", "__svml_exp8", 8},
1304 
1305         {"expf", "__svml_expf4", 4},
1306         {"expf", "__svml_expf8", 8},
1307         {"expf", "__svml_expf16", 16},
1308 
1309         {"llvm.exp.f64", "__svml_exp2", 2},
1310         {"llvm.exp.f64", "__svml_exp4", 4},
1311         {"llvm.exp.f64", "__svml_exp8", 8},
1312 
1313         {"llvm.exp.f32", "__svml_expf4", 4},
1314         {"llvm.exp.f32", "__svml_expf8", 8},
1315         {"llvm.exp.f32", "__svml_expf16", 16},
1316 
1317         {"log", "__svml_log2", 2},
1318         {"log", "__svml_log4", 4},
1319         {"log", "__svml_log8", 8},
1320 
1321         {"logf", "__svml_logf4", 4},
1322         {"logf", "__svml_logf8", 8},
1323         {"logf", "__svml_logf16", 16},
1324 
1325         {"llvm.log.f64", "__svml_log2", 2},
1326         {"llvm.log.f64", "__svml_log4", 4},
1327         {"llvm.log.f64", "__svml_log8", 8},
1328 
1329         {"llvm.log.f32", "__svml_logf4", 4},
1330         {"llvm.log.f32", "__svml_logf8", 8},
1331         {"llvm.log.f32", "__svml_logf16", 16},
1332     };
1333     addVectorizableFunctions(VecFuncs);
1334     break;
1335   }
1336   case NoLibrary:
1337     break;
1338   }
1339 }
1340 
1341 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
1342   funcName = sanitizeFunctionName(funcName);
1343   if (funcName.empty())
1344     return false;
1345 
1346   std::vector<VecDesc>::const_iterator I = std::lower_bound(
1347       VectorDescs.begin(), VectorDescs.end(), funcName,
1348       compareWithScalarFnName);
1349   return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
1350 }
1351 
1352 StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
1353                                                        unsigned VF) const {
1354   F = sanitizeFunctionName(F);
1355   if (F.empty())
1356     return F;
1357   std::vector<VecDesc>::const_iterator I = std::lower_bound(
1358       VectorDescs.begin(), VectorDescs.end(), F, compareWithScalarFnName);
1359   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
1360     if (I->VectorizationFactor == VF)
1361       return I->VectorFnName;
1362     ++I;
1363   }
1364   return StringRef();
1365 }
1366 
1367 StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F,
1368                                                        unsigned &VF) const {
1369   F = sanitizeFunctionName(F);
1370   if (F.empty())
1371     return F;
1372 
1373   std::vector<VecDesc>::const_iterator I = std::lower_bound(
1374       ScalarDescs.begin(), ScalarDescs.end(), F, compareWithVectorFnName);
1375   if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F)
1376     return StringRef();
1377   VF = I->VectorizationFactor;
1378   return I->ScalarFnName;
1379 }
1380 
1381 TargetLibraryInfo TargetLibraryAnalysis::run(Module &M,
1382                                              ModuleAnalysisManager &) {
1383   if (PresetInfoImpl)
1384     return TargetLibraryInfo(*PresetInfoImpl);
1385 
1386   return TargetLibraryInfo(lookupInfoImpl(Triple(M.getTargetTriple())));
1387 }
1388 
1389 TargetLibraryInfo TargetLibraryAnalysis::run(Function &F,
1390                                              FunctionAnalysisManager &) {
1391   if (PresetInfoImpl)
1392     return TargetLibraryInfo(*PresetInfoImpl);
1393 
1394   return TargetLibraryInfo(
1395       lookupInfoImpl(Triple(F.getParent()->getTargetTriple())));
1396 }
1397 
1398 TargetLibraryInfoImpl &TargetLibraryAnalysis::lookupInfoImpl(const Triple &T) {
1399   std::unique_ptr<TargetLibraryInfoImpl> &Impl =
1400       Impls[T.normalize()];
1401   if (!Impl)
1402     Impl.reset(new TargetLibraryInfoImpl(T));
1403 
1404   return *Impl;
1405 }
1406 
1407 
1408 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
1409     : ImmutablePass(ID), TLIImpl(), TLI(TLIImpl) {
1410   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1411 }
1412 
1413 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
1414     : ImmutablePass(ID), TLIImpl(T), TLI(TLIImpl) {
1415   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1416 }
1417 
1418 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
1419     const TargetLibraryInfoImpl &TLIImpl)
1420     : ImmutablePass(ID), TLIImpl(TLIImpl), TLI(this->TLIImpl) {
1421   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1422 }
1423 
1424 AnalysisKey TargetLibraryAnalysis::Key;
1425 
1426 // Register the basic pass.
1427 INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
1428                 "Target Library Information", false, true)
1429 char TargetLibraryInfoWrapperPass::ID = 0;
1430 
1431 void TargetLibraryInfoWrapperPass::anchor() {}
1432