xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/TargetLibraryInfo.cpp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the TargetLibraryInfo class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Analysis/TargetLibraryInfo.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/InitializePasses.h"
17 #include "llvm/Support/CommandLine.h"
18 using namespace llvm;
19 
20 static cl::opt<TargetLibraryInfoImpl::VectorLibrary> ClVectorLibrary(
21     "vector-library", cl::Hidden, cl::desc("Vector functions library"),
22     cl::init(TargetLibraryInfoImpl::NoLibrary),
23     cl::values(clEnumValN(TargetLibraryInfoImpl::NoLibrary, "none",
24                           "No vector functions library"),
25                clEnumValN(TargetLibraryInfoImpl::Accelerate, "Accelerate",
26                           "Accelerate framework"),
27                clEnumValN(TargetLibraryInfoImpl::MASSV, "MASSV",
28                           "IBM MASS vector library"),
29                clEnumValN(TargetLibraryInfoImpl::SVML, "SVML",
30                           "Intel SVML library")));
31 
32 StringLiteral const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] =
33     {
34 #define TLI_DEFINE_STRING
35 #include "llvm/Analysis/TargetLibraryInfo.def"
36 };
37 
38 static bool hasSinCosPiStret(const Triple &T) {
39   // Only Darwin variants have _stret versions of combined trig functions.
40   if (!T.isOSDarwin())
41     return false;
42 
43   // The ABI is rather complicated on x86, so don't do anything special there.
44   if (T.getArch() == Triple::x86)
45     return false;
46 
47   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
48     return false;
49 
50   if (T.isiOS() && T.isOSVersionLT(7, 0))
51     return false;
52 
53   return true;
54 }
55 
56 static bool hasBcmp(const Triple &TT) {
57   // Posix removed support from bcmp() in 2001, but the glibc and several
58   // implementations of the libc still have it.
59   if (TT.isOSLinux())
60     return TT.isGNUEnvironment() || TT.isMusl();
61   // Both NetBSD and OpenBSD are planning to remove the function. Windows does
62   // not have it.
63   return TT.isOSFreeBSD() || TT.isOSSolaris();
64 }
65 
66 /// Initialize the set of available library functions based on the specified
67 /// target triple. This should be carefully written so that a missing target
68 /// triple gets a sane set of defaults.
69 static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
70                        ArrayRef<StringLiteral> StandardNames) {
71   // Verify that the StandardNames array is in alphabetical order.
72   assert(
73       llvm::is_sorted(StandardNames,
74                       [](StringRef LHS, StringRef RHS) { return LHS < RHS; }) &&
75       "TargetLibraryInfoImpl function names must be sorted");
76 
77   // Set IO unlocked variants as unavailable
78   // Set them as available per system below
79   TLI.setUnavailable(LibFunc_getchar_unlocked);
80   TLI.setUnavailable(LibFunc_putc_unlocked);
81   TLI.setUnavailable(LibFunc_putchar_unlocked);
82   TLI.setUnavailable(LibFunc_fputc_unlocked);
83   TLI.setUnavailable(LibFunc_fgetc_unlocked);
84   TLI.setUnavailable(LibFunc_fread_unlocked);
85   TLI.setUnavailable(LibFunc_fwrite_unlocked);
86   TLI.setUnavailable(LibFunc_fputs_unlocked);
87   TLI.setUnavailable(LibFunc_fgets_unlocked);
88 
89   bool ShouldExtI32Param = false, ShouldExtI32Return = false,
90        ShouldSignExtI32Param = false;
91   // PowerPC64, Sparc64, SystemZ need signext/zeroext on i32 parameters and
92   // returns corresponding to C-level ints and unsigned ints.
93   if (T.isPPC64() || T.getArch() == Triple::sparcv9 ||
94       T.getArch() == Triple::systemz) {
95     ShouldExtI32Param = true;
96     ShouldExtI32Return = true;
97   }
98   // Mips, on the other hand, needs signext on i32 parameters corresponding
99   // to both signed and unsigned ints.
100   if (T.isMIPS()) {
101     ShouldSignExtI32Param = true;
102   }
103   TLI.setShouldExtI32Param(ShouldExtI32Param);
104   TLI.setShouldExtI32Return(ShouldExtI32Return);
105   TLI.setShouldSignExtI32Param(ShouldSignExtI32Param);
106 
107   if (T.isAMDGPU())
108     TLI.disableAllFunctions();
109 
110   // There are no library implementations of memcpy and memset for AMD gpus and
111   // these can be difficult to lower in the backend.
112   if (T.isAMDGPU()) {
113     TLI.setUnavailable(LibFunc_memcpy);
114     TLI.setUnavailable(LibFunc_memset);
115     TLI.setUnavailable(LibFunc_memset_pattern16);
116     return;
117   }
118 
119   // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
120   // All versions of watchOS support it.
121   if (T.isMacOSX()) {
122     // available IO unlocked variants on Mac OS X
123     TLI.setAvailable(LibFunc_getc_unlocked);
124     TLI.setAvailable(LibFunc_getchar_unlocked);
125     TLI.setAvailable(LibFunc_putc_unlocked);
126     TLI.setAvailable(LibFunc_putchar_unlocked);
127 
128     if (T.isMacOSXVersionLT(10, 5))
129       TLI.setUnavailable(LibFunc_memset_pattern16);
130   } else if (T.isiOS()) {
131     if (T.isOSVersionLT(3, 0))
132       TLI.setUnavailable(LibFunc_memset_pattern16);
133   } else if (!T.isWatchOS()) {
134     TLI.setUnavailable(LibFunc_memset_pattern16);
135   }
136 
137   if (!hasSinCosPiStret(T)) {
138     TLI.setUnavailable(LibFunc_sinpi);
139     TLI.setUnavailable(LibFunc_sinpif);
140     TLI.setUnavailable(LibFunc_cospi);
141     TLI.setUnavailable(LibFunc_cospif);
142     TLI.setUnavailable(LibFunc_sincospi_stret);
143     TLI.setUnavailable(LibFunc_sincospif_stret);
144   }
145 
146   if (!hasBcmp(T))
147     TLI.setUnavailable(LibFunc_bcmp);
148 
149   if (T.isMacOSX() && T.getArch() == Triple::x86 &&
150       !T.isMacOSXVersionLT(10, 7)) {
151     // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
152     // we don't care about) have two versions; on recent OSX, the one we want
153     // has a $UNIX2003 suffix. The two implementations are identical except
154     // for the return value in some edge cases.  However, we don't want to
155     // generate code that depends on the old symbols.
156     TLI.setAvailableWithName(LibFunc_fwrite, "fwrite$UNIX2003");
157     TLI.setAvailableWithName(LibFunc_fputs, "fputs$UNIX2003");
158   }
159 
160   // iprintf and friends are only available on XCore, TCE, and Emscripten.
161   if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce &&
162       T.getOS() != Triple::Emscripten) {
163     TLI.setUnavailable(LibFunc_iprintf);
164     TLI.setUnavailable(LibFunc_siprintf);
165     TLI.setUnavailable(LibFunc_fiprintf);
166   }
167 
168   // __small_printf and friends are only available on Emscripten.
169   if (T.getOS() != Triple::Emscripten) {
170     TLI.setUnavailable(LibFunc_small_printf);
171     TLI.setUnavailable(LibFunc_small_sprintf);
172     TLI.setUnavailable(LibFunc_small_fprintf);
173   }
174 
175   if (T.isOSWindows() && !T.isOSCygMing()) {
176     // XXX: The earliest documentation available at the moment is for VS2015/VC19:
177     // https://docs.microsoft.com/en-us/cpp/c-runtime-library/floating-point-support?view=vs-2015
178     // XXX: In order to use an MSVCRT older than VC19,
179     // the specific library version must be explicit in the target triple,
180     // e.g., x86_64-pc-windows-msvc18.
181     bool hasPartialC99 = true;
182     if (T.isKnownWindowsMSVCEnvironment()) {
183       unsigned Major, Minor, Micro;
184       T.getEnvironmentVersion(Major, Minor, Micro);
185       hasPartialC99 = (Major == 0 || Major >= 19);
186     }
187 
188     // Latest targets support C89 math functions, in part.
189     bool isARM = (T.getArch() == Triple::aarch64 ||
190                   T.getArch() == Triple::arm);
191     bool hasPartialFloat = (isARM ||
192                             T.getArch() == Triple::x86_64);
193 
194     // Win32 does not support float C89 math functions, in general.
195     if (!hasPartialFloat) {
196       TLI.setUnavailable(LibFunc_acosf);
197       TLI.setUnavailable(LibFunc_asinf);
198       TLI.setUnavailable(LibFunc_atan2f);
199       TLI.setUnavailable(LibFunc_atanf);
200       TLI.setUnavailable(LibFunc_ceilf);
201       TLI.setUnavailable(LibFunc_cosf);
202       TLI.setUnavailable(LibFunc_coshf);
203       TLI.setUnavailable(LibFunc_expf);
204       TLI.setUnavailable(LibFunc_floorf);
205       TLI.setUnavailable(LibFunc_fmodf);
206       TLI.setUnavailable(LibFunc_log10f);
207       TLI.setUnavailable(LibFunc_logf);
208       TLI.setUnavailable(LibFunc_modff);
209       TLI.setUnavailable(LibFunc_powf);
210       TLI.setUnavailable(LibFunc_remainderf);
211       TLI.setUnavailable(LibFunc_sinf);
212       TLI.setUnavailable(LibFunc_sinhf);
213       TLI.setUnavailable(LibFunc_sqrtf);
214       TLI.setUnavailable(LibFunc_tanf);
215       TLI.setUnavailable(LibFunc_tanhf);
216     }
217     if (!isARM)
218       TLI.setUnavailable(LibFunc_fabsf);
219     TLI.setUnavailable(LibFunc_frexpf);
220     TLI.setUnavailable(LibFunc_ldexpf);
221 
222     // Win32 does not support long double C89 math functions.
223     TLI.setUnavailable(LibFunc_acosl);
224     TLI.setUnavailable(LibFunc_asinl);
225     TLI.setUnavailable(LibFunc_atan2l);
226     TLI.setUnavailable(LibFunc_atanl);
227     TLI.setUnavailable(LibFunc_ceill);
228     TLI.setUnavailable(LibFunc_cosl);
229     TLI.setUnavailable(LibFunc_coshl);
230     TLI.setUnavailable(LibFunc_expl);
231     TLI.setUnavailable(LibFunc_fabsl);
232     TLI.setUnavailable(LibFunc_floorl);
233     TLI.setUnavailable(LibFunc_fmodl);
234     TLI.setUnavailable(LibFunc_frexpl);
235     TLI.setUnavailable(LibFunc_ldexpl);
236     TLI.setUnavailable(LibFunc_log10l);
237     TLI.setUnavailable(LibFunc_logl);
238     TLI.setUnavailable(LibFunc_modfl);
239     TLI.setUnavailable(LibFunc_powl);
240     TLI.setUnavailable(LibFunc_remainderl);
241     TLI.setUnavailable(LibFunc_sinl);
242     TLI.setUnavailable(LibFunc_sinhl);
243     TLI.setUnavailable(LibFunc_sqrtl);
244     TLI.setUnavailable(LibFunc_tanl);
245     TLI.setUnavailable(LibFunc_tanhl);
246 
247     // Win32 does not fully support C99 math functions.
248     if (!hasPartialC99) {
249       TLI.setUnavailable(LibFunc_acosh);
250       TLI.setUnavailable(LibFunc_acoshf);
251       TLI.setUnavailable(LibFunc_asinh);
252       TLI.setUnavailable(LibFunc_asinhf);
253       TLI.setUnavailable(LibFunc_atanh);
254       TLI.setUnavailable(LibFunc_atanhf);
255       TLI.setAvailableWithName(LibFunc_cabs, "_cabs");
256       TLI.setUnavailable(LibFunc_cabsf);
257       TLI.setUnavailable(LibFunc_cbrt);
258       TLI.setUnavailable(LibFunc_cbrtf);
259       TLI.setAvailableWithName(LibFunc_copysign, "_copysign");
260       TLI.setAvailableWithName(LibFunc_copysignf, "_copysignf");
261       TLI.setUnavailable(LibFunc_exp2);
262       TLI.setUnavailable(LibFunc_exp2f);
263       TLI.setUnavailable(LibFunc_expm1);
264       TLI.setUnavailable(LibFunc_expm1f);
265       TLI.setUnavailable(LibFunc_fmax);
266       TLI.setUnavailable(LibFunc_fmaxf);
267       TLI.setUnavailable(LibFunc_fmin);
268       TLI.setUnavailable(LibFunc_fminf);
269       TLI.setUnavailable(LibFunc_log1p);
270       TLI.setUnavailable(LibFunc_log1pf);
271       TLI.setUnavailable(LibFunc_log2);
272       TLI.setUnavailable(LibFunc_log2f);
273       TLI.setAvailableWithName(LibFunc_logb, "_logb");
274       if (hasPartialFloat)
275         TLI.setAvailableWithName(LibFunc_logbf, "_logbf");
276       else
277         TLI.setUnavailable(LibFunc_logbf);
278       TLI.setUnavailable(LibFunc_rint);
279       TLI.setUnavailable(LibFunc_rintf);
280       TLI.setUnavailable(LibFunc_round);
281       TLI.setUnavailable(LibFunc_roundf);
282       TLI.setUnavailable(LibFunc_trunc);
283       TLI.setUnavailable(LibFunc_truncf);
284     }
285 
286     // Win32 does not support long double C99 math functions.
287     TLI.setUnavailable(LibFunc_acoshl);
288     TLI.setUnavailable(LibFunc_asinhl);
289     TLI.setUnavailable(LibFunc_atanhl);
290     TLI.setUnavailable(LibFunc_cabsl);
291     TLI.setUnavailable(LibFunc_cbrtl);
292     TLI.setUnavailable(LibFunc_copysignl);
293     TLI.setUnavailable(LibFunc_exp2l);
294     TLI.setUnavailable(LibFunc_expm1l);
295     TLI.setUnavailable(LibFunc_fmaxl);
296     TLI.setUnavailable(LibFunc_fminl);
297     TLI.setUnavailable(LibFunc_log1pl);
298     TLI.setUnavailable(LibFunc_log2l);
299     TLI.setUnavailable(LibFunc_logbl);
300     TLI.setUnavailable(LibFunc_nearbyintl);
301     TLI.setUnavailable(LibFunc_rintl);
302     TLI.setUnavailable(LibFunc_roundl);
303     TLI.setUnavailable(LibFunc_truncl);
304 
305     // Win32 does not support these functions, but
306     // they are generally available on POSIX-compliant systems.
307     TLI.setUnavailable(LibFunc_access);
308     TLI.setUnavailable(LibFunc_bcmp);
309     TLI.setUnavailable(LibFunc_bcopy);
310     TLI.setUnavailable(LibFunc_bzero);
311     TLI.setUnavailable(LibFunc_chmod);
312     TLI.setUnavailable(LibFunc_chown);
313     TLI.setUnavailable(LibFunc_closedir);
314     TLI.setUnavailable(LibFunc_ctermid);
315     TLI.setUnavailable(LibFunc_fdopen);
316     TLI.setUnavailable(LibFunc_ffs);
317     TLI.setUnavailable(LibFunc_fileno);
318     TLI.setUnavailable(LibFunc_flockfile);
319     TLI.setUnavailable(LibFunc_fseeko);
320     TLI.setUnavailable(LibFunc_fstat);
321     TLI.setUnavailable(LibFunc_fstatvfs);
322     TLI.setUnavailable(LibFunc_ftello);
323     TLI.setUnavailable(LibFunc_ftrylockfile);
324     TLI.setUnavailable(LibFunc_funlockfile);
325     TLI.setUnavailable(LibFunc_getitimer);
326     TLI.setUnavailable(LibFunc_getlogin_r);
327     TLI.setUnavailable(LibFunc_getpwnam);
328     TLI.setUnavailable(LibFunc_gettimeofday);
329     TLI.setUnavailable(LibFunc_htonl);
330     TLI.setUnavailable(LibFunc_htons);
331     TLI.setUnavailable(LibFunc_lchown);
332     TLI.setUnavailable(LibFunc_lstat);
333     TLI.setUnavailable(LibFunc_memccpy);
334     TLI.setUnavailable(LibFunc_mkdir);
335     TLI.setUnavailable(LibFunc_ntohl);
336     TLI.setUnavailable(LibFunc_ntohs);
337     TLI.setUnavailable(LibFunc_open);
338     TLI.setUnavailable(LibFunc_opendir);
339     TLI.setUnavailable(LibFunc_pclose);
340     TLI.setUnavailable(LibFunc_popen);
341     TLI.setUnavailable(LibFunc_pread);
342     TLI.setUnavailable(LibFunc_pwrite);
343     TLI.setUnavailable(LibFunc_read);
344     TLI.setUnavailable(LibFunc_readlink);
345     TLI.setUnavailable(LibFunc_realpath);
346     TLI.setUnavailable(LibFunc_rmdir);
347     TLI.setUnavailable(LibFunc_setitimer);
348     TLI.setUnavailable(LibFunc_stat);
349     TLI.setUnavailable(LibFunc_statvfs);
350     TLI.setUnavailable(LibFunc_stpcpy);
351     TLI.setUnavailable(LibFunc_stpncpy);
352     TLI.setUnavailable(LibFunc_strcasecmp);
353     TLI.setUnavailable(LibFunc_strncasecmp);
354     TLI.setUnavailable(LibFunc_times);
355     TLI.setUnavailable(LibFunc_uname);
356     TLI.setUnavailable(LibFunc_unlink);
357     TLI.setUnavailable(LibFunc_unsetenv);
358     TLI.setUnavailable(LibFunc_utime);
359     TLI.setUnavailable(LibFunc_utimes);
360     TLI.setUnavailable(LibFunc_write);
361   }
362 
363   switch (T.getOS()) {
364   case Triple::MacOSX:
365     // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
366     // and their names are __exp10 and __exp10f. exp10l is not available on
367     // OS X or iOS.
368     TLI.setUnavailable(LibFunc_exp10l);
369     if (T.isMacOSXVersionLT(10, 9)) {
370       TLI.setUnavailable(LibFunc_exp10);
371       TLI.setUnavailable(LibFunc_exp10f);
372     } else {
373       TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
374       TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
375     }
376     break;
377   case Triple::IOS:
378   case Triple::TvOS:
379   case Triple::WatchOS:
380     TLI.setUnavailable(LibFunc_exp10l);
381     if (!T.isWatchOS() &&
382         (T.isOSVersionLT(7, 0) || (T.isOSVersionLT(9, 0) && T.isX86()))) {
383       TLI.setUnavailable(LibFunc_exp10);
384       TLI.setUnavailable(LibFunc_exp10f);
385     } else {
386       TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
387       TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
388     }
389     break;
390   case Triple::Linux:
391     // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
392     // buggy prior to glibc version 2.18. Until this version is widely deployed
393     // or we have a reasonable detection strategy, we cannot use exp10 reliably
394     // on Linux.
395     //
396     // Fall through to disable all of them.
397     LLVM_FALLTHROUGH;
398   default:
399     TLI.setUnavailable(LibFunc_exp10);
400     TLI.setUnavailable(LibFunc_exp10f);
401     TLI.setUnavailable(LibFunc_exp10l);
402   }
403 
404   // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
405   // Linux (GLIBC):
406   // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
407   // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
408   // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
409   switch (T.getOS()) {
410   case Triple::Darwin:
411   case Triple::MacOSX:
412   case Triple::IOS:
413   case Triple::TvOS:
414   case Triple::WatchOS:
415   case Triple::FreeBSD:
416   case Triple::Linux:
417     break;
418   default:
419     TLI.setUnavailable(LibFunc_ffsl);
420   }
421 
422   // ffsll is available on at least FreeBSD and Linux (GLIBC):
423   // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
424   // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
425   switch (T.getOS()) {
426   case Triple::Darwin:
427   case Triple::MacOSX:
428   case Triple::IOS:
429   case Triple::TvOS:
430   case Triple::WatchOS:
431   case Triple::FreeBSD:
432   case Triple::Linux:
433     break;
434   default:
435     TLI.setUnavailable(LibFunc_ffsll);
436   }
437 
438   // The following functions are available on at least FreeBSD:
439   // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
440   // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
441   // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
442   if (!T.isOSFreeBSD()) {
443     TLI.setUnavailable(LibFunc_fls);
444     TLI.setUnavailable(LibFunc_flsl);
445     TLI.setUnavailable(LibFunc_flsll);
446   }
447 
448   // The following functions are only available on GNU/Linux (using glibc).
449   // Linux variants without glibc (eg: bionic, musl) may have some subset.
450   if (!T.isOSLinux() || !T.isGNUEnvironment()) {
451     TLI.setUnavailable(LibFunc_dunder_strdup);
452     TLI.setUnavailable(LibFunc_dunder_strtok_r);
453     TLI.setUnavailable(LibFunc_dunder_isoc99_scanf);
454     TLI.setUnavailable(LibFunc_dunder_isoc99_sscanf);
455     TLI.setUnavailable(LibFunc_under_IO_getc);
456     TLI.setUnavailable(LibFunc_under_IO_putc);
457     // But, Android and musl have memalign.
458     if (!T.isAndroid() && !T.isMusl())
459       TLI.setUnavailable(LibFunc_memalign);
460     TLI.setUnavailable(LibFunc_fopen64);
461     TLI.setUnavailable(LibFunc_fseeko64);
462     TLI.setUnavailable(LibFunc_fstat64);
463     TLI.setUnavailable(LibFunc_fstatvfs64);
464     TLI.setUnavailable(LibFunc_ftello64);
465     TLI.setUnavailable(LibFunc_lstat64);
466     TLI.setUnavailable(LibFunc_open64);
467     TLI.setUnavailable(LibFunc_stat64);
468     TLI.setUnavailable(LibFunc_statvfs64);
469     TLI.setUnavailable(LibFunc_tmpfile64);
470 
471     // Relaxed math functions are included in math-finite.h on Linux (GLIBC).
472     // Note that math-finite.h is no longer supported by top-of-tree GLIBC,
473     // so we keep these functions around just so that they're recognized by
474     // the ConstantFolder.
475     TLI.setUnavailable(LibFunc_acos_finite);
476     TLI.setUnavailable(LibFunc_acosf_finite);
477     TLI.setUnavailable(LibFunc_acosl_finite);
478     TLI.setUnavailable(LibFunc_acosh_finite);
479     TLI.setUnavailable(LibFunc_acoshf_finite);
480     TLI.setUnavailable(LibFunc_acoshl_finite);
481     TLI.setUnavailable(LibFunc_asin_finite);
482     TLI.setUnavailable(LibFunc_asinf_finite);
483     TLI.setUnavailable(LibFunc_asinl_finite);
484     TLI.setUnavailable(LibFunc_atan2_finite);
485     TLI.setUnavailable(LibFunc_atan2f_finite);
486     TLI.setUnavailable(LibFunc_atan2l_finite);
487     TLI.setUnavailable(LibFunc_atanh_finite);
488     TLI.setUnavailable(LibFunc_atanhf_finite);
489     TLI.setUnavailable(LibFunc_atanhl_finite);
490     TLI.setUnavailable(LibFunc_cosh_finite);
491     TLI.setUnavailable(LibFunc_coshf_finite);
492     TLI.setUnavailable(LibFunc_coshl_finite);
493     TLI.setUnavailable(LibFunc_exp10_finite);
494     TLI.setUnavailable(LibFunc_exp10f_finite);
495     TLI.setUnavailable(LibFunc_exp10l_finite);
496     TLI.setUnavailable(LibFunc_exp2_finite);
497     TLI.setUnavailable(LibFunc_exp2f_finite);
498     TLI.setUnavailable(LibFunc_exp2l_finite);
499     TLI.setUnavailable(LibFunc_exp_finite);
500     TLI.setUnavailable(LibFunc_expf_finite);
501     TLI.setUnavailable(LibFunc_expl_finite);
502     TLI.setUnavailable(LibFunc_log10_finite);
503     TLI.setUnavailable(LibFunc_log10f_finite);
504     TLI.setUnavailable(LibFunc_log10l_finite);
505     TLI.setUnavailable(LibFunc_log2_finite);
506     TLI.setUnavailable(LibFunc_log2f_finite);
507     TLI.setUnavailable(LibFunc_log2l_finite);
508     TLI.setUnavailable(LibFunc_log_finite);
509     TLI.setUnavailable(LibFunc_logf_finite);
510     TLI.setUnavailable(LibFunc_logl_finite);
511     TLI.setUnavailable(LibFunc_pow_finite);
512     TLI.setUnavailable(LibFunc_powf_finite);
513     TLI.setUnavailable(LibFunc_powl_finite);
514     TLI.setUnavailable(LibFunc_sinh_finite);
515     TLI.setUnavailable(LibFunc_sinhf_finite);
516     TLI.setUnavailable(LibFunc_sinhl_finite);
517   }
518 
519   if ((T.isOSLinux() && T.isGNUEnvironment()) ||
520       (T.isAndroid() && !T.isAndroidVersionLT(28))) {
521     // available IO unlocked variants on GNU/Linux and Android P or later
522     TLI.setAvailable(LibFunc_getc_unlocked);
523     TLI.setAvailable(LibFunc_getchar_unlocked);
524     TLI.setAvailable(LibFunc_putc_unlocked);
525     TLI.setAvailable(LibFunc_putchar_unlocked);
526     TLI.setAvailable(LibFunc_fputc_unlocked);
527     TLI.setAvailable(LibFunc_fgetc_unlocked);
528     TLI.setAvailable(LibFunc_fread_unlocked);
529     TLI.setAvailable(LibFunc_fwrite_unlocked);
530     TLI.setAvailable(LibFunc_fputs_unlocked);
531     TLI.setAvailable(LibFunc_fgets_unlocked);
532   }
533 
534   // As currently implemented in clang, NVPTX code has no standard library to
535   // speak of.  Headers provide a standard-ish library implementation, but many
536   // of the signatures are wrong -- for example, many libm functions are not
537   // extern "C".
538   //
539   // libdevice, an IR library provided by nvidia, is linked in by the front-end,
540   // but only used functions are provided to llvm.  Moreover, most of the
541   // functions in libdevice don't map precisely to standard library functions.
542   //
543   // FIXME: Having no standard library prevents e.g. many fastmath
544   // optimizations, so this situation should be fixed.
545   if (T.isNVPTX()) {
546     TLI.disableAllFunctions();
547     TLI.setAvailable(LibFunc_nvvm_reflect);
548   } else {
549     TLI.setUnavailable(LibFunc_nvvm_reflect);
550   }
551 
552   TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary);
553 }
554 
555 TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
556   // Default to everything being available.
557   memset(AvailableArray, -1, sizeof(AvailableArray));
558 
559   initialize(*this, Triple(), StandardNames);
560 }
561 
562 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
563   // Default to everything being available.
564   memset(AvailableArray, -1, sizeof(AvailableArray));
565 
566   initialize(*this, T, StandardNames);
567 }
568 
569 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
570     : CustomNames(TLI.CustomNames), ShouldExtI32Param(TLI.ShouldExtI32Param),
571       ShouldExtI32Return(TLI.ShouldExtI32Return),
572       ShouldSignExtI32Param(TLI.ShouldSignExtI32Param) {
573   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
574   VectorDescs = TLI.VectorDescs;
575   ScalarDescs = TLI.ScalarDescs;
576 }
577 
578 TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
579     : CustomNames(std::move(TLI.CustomNames)),
580       ShouldExtI32Param(TLI.ShouldExtI32Param),
581       ShouldExtI32Return(TLI.ShouldExtI32Return),
582       ShouldSignExtI32Param(TLI.ShouldSignExtI32Param) {
583   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
584             AvailableArray);
585   VectorDescs = TLI.VectorDescs;
586   ScalarDescs = TLI.ScalarDescs;
587 }
588 
589 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
590   CustomNames = TLI.CustomNames;
591   ShouldExtI32Param = TLI.ShouldExtI32Param;
592   ShouldExtI32Return = TLI.ShouldExtI32Return;
593   ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
594   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
595   return *this;
596 }
597 
598 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
599   CustomNames = std::move(TLI.CustomNames);
600   ShouldExtI32Param = TLI.ShouldExtI32Param;
601   ShouldExtI32Return = TLI.ShouldExtI32Return;
602   ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
603   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
604             AvailableArray);
605   return *this;
606 }
607 
608 static StringRef sanitizeFunctionName(StringRef funcName) {
609   // Filter out empty names and names containing null bytes, those can't be in
610   // our table.
611   if (funcName.empty() || funcName.find('\0') != StringRef::npos)
612     return StringRef();
613 
614   // Check for \01 prefix that is used to mangle __asm declarations and
615   // strip it if present.
616   return GlobalValue::dropLLVMManglingEscape(funcName);
617 }
618 
619 bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName, LibFunc &F) const {
620   funcName = sanitizeFunctionName(funcName);
621   if (funcName.empty())
622     return false;
623 
624   const auto *Start = std::begin(StandardNames);
625   const auto *End = std::end(StandardNames);
626   const auto *I = std::lower_bound(Start, End, funcName);
627   if (I != End && *I == funcName) {
628     F = (LibFunc)(I - Start);
629     return true;
630   }
631   return false;
632 }
633 
634 bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
635                                                    LibFunc F,
636                                                    const DataLayout *DL) const {
637   LLVMContext &Ctx = FTy.getContext();
638   Type *PCharTy = Type::getInt8PtrTy(Ctx);
639   Type *SizeTTy = DL ? DL->getIntPtrType(Ctx, /*AS=*/0) : nullptr;
640   auto IsSizeTTy = [SizeTTy](Type *Ty) {
641     return SizeTTy ? Ty == SizeTTy : Ty->isIntegerTy();
642   };
643   unsigned NumParams = FTy.getNumParams();
644 
645   switch (F) {
646   case LibFunc_execl:
647   case LibFunc_execlp:
648   case LibFunc_execle:
649     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
650             FTy.getParamType(1)->isPointerTy() &&
651             FTy.getReturnType()->isIntegerTy(32));
652   case LibFunc_execv:
653   case LibFunc_execvp:
654     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
655             FTy.getParamType(1)->isPointerTy() &&
656             FTy.getReturnType()->isIntegerTy(32));
657   case LibFunc_execvP:
658   case LibFunc_execvpe:
659   case LibFunc_execve:
660     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
661             FTy.getParamType(1)->isPointerTy() &&
662             FTy.getParamType(2)->isPointerTy() &&
663             FTy.getReturnType()->isIntegerTy(32));
664   case LibFunc_strlen_chk:
665     --NumParams;
666     if (!IsSizeTTy(FTy.getParamType(NumParams)))
667       return false;
668     LLVM_FALLTHROUGH;
669   case LibFunc_strlen:
670     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
671             FTy.getReturnType()->isIntegerTy());
672 
673   case LibFunc_strchr:
674   case LibFunc_strrchr:
675     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
676             FTy.getParamType(0) == FTy.getReturnType() &&
677             FTy.getParamType(1)->isIntegerTy());
678 
679   case LibFunc_strtol:
680   case LibFunc_strtod:
681   case LibFunc_strtof:
682   case LibFunc_strtoul:
683   case LibFunc_strtoll:
684   case LibFunc_strtold:
685   case LibFunc_strtoull:
686     return ((NumParams == 2 || NumParams == 3) &&
687             FTy.getParamType(0)->isPointerTy() &&
688             FTy.getParamType(1)->isPointerTy());
689   case LibFunc_strcat_chk:
690     --NumParams;
691     if (!IsSizeTTy(FTy.getParamType(NumParams)))
692       return false;
693     LLVM_FALLTHROUGH;
694   case LibFunc_strcat:
695     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
696             FTy.getParamType(0) == FTy.getReturnType() &&
697             FTy.getParamType(1) == FTy.getReturnType());
698 
699   case LibFunc_strncat_chk:
700     --NumParams;
701     if (!IsSizeTTy(FTy.getParamType(NumParams)))
702       return false;
703     LLVM_FALLTHROUGH;
704   case LibFunc_strncat:
705     return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
706             FTy.getParamType(0) == FTy.getReturnType() &&
707             FTy.getParamType(1) == FTy.getReturnType() &&
708             IsSizeTTy(FTy.getParamType(2)));
709 
710   case LibFunc_strcpy_chk:
711   case LibFunc_stpcpy_chk:
712     --NumParams;
713     if (!IsSizeTTy(FTy.getParamType(NumParams)))
714       return false;
715     LLVM_FALLTHROUGH;
716   case LibFunc_strcpy:
717   case LibFunc_stpcpy:
718     return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(0) &&
719             FTy.getParamType(0) == FTy.getParamType(1) &&
720             FTy.getParamType(0) == PCharTy);
721 
722   case LibFunc_strlcat_chk:
723   case LibFunc_strlcpy_chk:
724     --NumParams;
725     if (!IsSizeTTy(FTy.getParamType(NumParams)))
726       return false;
727     LLVM_FALLTHROUGH;
728   case LibFunc_strlcat:
729   case LibFunc_strlcpy:
730     return NumParams == 3 && IsSizeTTy(FTy.getReturnType()) &&
731            FTy.getParamType(0)->isPointerTy() &&
732            FTy.getParamType(1)->isPointerTy() &&
733            IsSizeTTy(FTy.getParamType(2));
734 
735   case LibFunc_strncpy_chk:
736   case LibFunc_stpncpy_chk:
737     --NumParams;
738     if (!IsSizeTTy(FTy.getParamType(NumParams)))
739       return false;
740     LLVM_FALLTHROUGH;
741   case LibFunc_strncpy:
742   case LibFunc_stpncpy:
743     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
744             FTy.getParamType(0) == FTy.getParamType(1) &&
745             FTy.getParamType(0) == PCharTy &&
746             IsSizeTTy(FTy.getParamType(2)));
747 
748   case LibFunc_strxfrm:
749     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
750             FTy.getParamType(1)->isPointerTy());
751 
752   case LibFunc_strcmp:
753     return (NumParams == 2 && FTy.getReturnType()->isIntegerTy(32) &&
754             FTy.getParamType(0)->isPointerTy() &&
755             FTy.getParamType(0) == FTy.getParamType(1));
756 
757   case LibFunc_strncmp:
758     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
759             FTy.getParamType(0)->isPointerTy() &&
760             FTy.getParamType(0) == FTy.getParamType(1) &&
761             IsSizeTTy(FTy.getParamType(2)));
762 
763   case LibFunc_strspn:
764   case LibFunc_strcspn:
765     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
766             FTy.getParamType(0) == FTy.getParamType(1) &&
767             FTy.getReturnType()->isIntegerTy());
768 
769   case LibFunc_strcoll:
770   case LibFunc_strcasecmp:
771   case LibFunc_strncasecmp:
772     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
773             FTy.getParamType(1)->isPointerTy());
774 
775   case LibFunc_strstr:
776     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
777             FTy.getParamType(0)->isPointerTy() &&
778             FTy.getParamType(1)->isPointerTy());
779 
780   case LibFunc_strpbrk:
781     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
782             FTy.getReturnType() == FTy.getParamType(0) &&
783             FTy.getParamType(0) == FTy.getParamType(1));
784 
785   case LibFunc_strtok:
786   case LibFunc_strtok_r:
787     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
788   case LibFunc_scanf:
789   case LibFunc_setbuf:
790   case LibFunc_setvbuf:
791     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
792   case LibFunc_strdup:
793   case LibFunc_strndup:
794     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
795             FTy.getParamType(0)->isPointerTy());
796   case LibFunc_sscanf:
797   case LibFunc_stat:
798   case LibFunc_statvfs:
799   case LibFunc_siprintf:
800   case LibFunc_small_sprintf:
801   case LibFunc_sprintf:
802     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
803             FTy.getParamType(1)->isPointerTy() &&
804             FTy.getReturnType()->isIntegerTy(32));
805 
806   case LibFunc_sprintf_chk:
807     return NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
808            FTy.getParamType(1)->isIntegerTy(32) &&
809            IsSizeTTy(FTy.getParamType(2)) &&
810            FTy.getParamType(3)->isPointerTy() &&
811            FTy.getReturnType()->isIntegerTy(32);
812 
813   case LibFunc_snprintf:
814     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
815             FTy.getParamType(2)->isPointerTy() &&
816             FTy.getReturnType()->isIntegerTy(32));
817 
818   case LibFunc_snprintf_chk:
819     return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
820            IsSizeTTy(FTy.getParamType(1)) &&
821            FTy.getParamType(2)->isIntegerTy(32) &&
822            IsSizeTTy(FTy.getParamType(3)) &&
823            FTy.getParamType(4)->isPointerTy() &&
824            FTy.getReturnType()->isIntegerTy(32);
825 
826   case LibFunc_setitimer:
827     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
828             FTy.getParamType(2)->isPointerTy());
829   case LibFunc_system:
830     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
831   case LibFunc_malloc:
832     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
833   case LibFunc_memcmp:
834     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
835             FTy.getParamType(0)->isPointerTy() &&
836             FTy.getParamType(1)->isPointerTy());
837 
838   case LibFunc_memchr:
839   case LibFunc_memrchr:
840     return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
841             FTy.getReturnType() == FTy.getParamType(0) &&
842             FTy.getParamType(1)->isIntegerTy(32) &&
843             IsSizeTTy(FTy.getParamType(2)));
844   case LibFunc_modf:
845   case LibFunc_modff:
846   case LibFunc_modfl:
847     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
848 
849   case LibFunc_memcpy_chk:
850   case LibFunc_memmove_chk:
851     --NumParams;
852     if (!IsSizeTTy(FTy.getParamType(NumParams)))
853       return false;
854     LLVM_FALLTHROUGH;
855   case LibFunc_memcpy:
856   case LibFunc_mempcpy:
857   case LibFunc_memmove:
858     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
859             FTy.getParamType(0)->isPointerTy() &&
860             FTy.getParamType(1)->isPointerTy() &&
861             IsSizeTTy(FTy.getParamType(2)));
862 
863   case LibFunc_memset_chk:
864     --NumParams;
865     if (!IsSizeTTy(FTy.getParamType(NumParams)))
866       return false;
867     LLVM_FALLTHROUGH;
868   case LibFunc_memset:
869     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
870             FTy.getParamType(0)->isPointerTy() &&
871             FTy.getParamType(1)->isIntegerTy() &&
872             IsSizeTTy(FTy.getParamType(2)));
873 
874   case LibFunc_memccpy_chk:
875       --NumParams;
876     if (!IsSizeTTy(FTy.getParamType(NumParams)))
877       return false;
878     LLVM_FALLTHROUGH;
879   case LibFunc_memccpy:
880     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
881   case LibFunc_memalign:
882     return (FTy.getReturnType()->isPointerTy());
883   case LibFunc_realloc:
884   case LibFunc_reallocf:
885     return (NumParams == 2 && FTy.getReturnType() == PCharTy &&
886             FTy.getParamType(0) == FTy.getReturnType() &&
887             IsSizeTTy(FTy.getParamType(1)));
888   case LibFunc_read:
889     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
890   case LibFunc_rewind:
891   case LibFunc_rmdir:
892   case LibFunc_remove:
893   case LibFunc_realpath:
894     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
895   case LibFunc_rename:
896     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
897             FTy.getParamType(1)->isPointerTy());
898   case LibFunc_readlink:
899     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
900             FTy.getParamType(1)->isPointerTy());
901   case LibFunc_write:
902     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
903   case LibFunc_aligned_alloc:
904     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
905   case LibFunc_bcopy:
906   case LibFunc_bcmp:
907     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
908             FTy.getParamType(1)->isPointerTy());
909   case LibFunc_bzero:
910     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
911   case LibFunc_calloc:
912     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
913 
914   case LibFunc_atof:
915   case LibFunc_atoi:
916   case LibFunc_atol:
917   case LibFunc_atoll:
918   case LibFunc_ferror:
919   case LibFunc_getenv:
920   case LibFunc_getpwnam:
921   case LibFunc_iprintf:
922   case LibFunc_small_printf:
923   case LibFunc_pclose:
924   case LibFunc_perror:
925   case LibFunc_printf:
926   case LibFunc_puts:
927   case LibFunc_uname:
928   case LibFunc_under_IO_getc:
929   case LibFunc_unlink:
930   case LibFunc_unsetenv:
931     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
932 
933   case LibFunc_access:
934   case LibFunc_chmod:
935   case LibFunc_chown:
936   case LibFunc_clearerr:
937   case LibFunc_closedir:
938   case LibFunc_ctermid:
939   case LibFunc_fclose:
940   case LibFunc_feof:
941   case LibFunc_fflush:
942   case LibFunc_fgetc:
943   case LibFunc_fgetc_unlocked:
944   case LibFunc_fileno:
945   case LibFunc_flockfile:
946   case LibFunc_free:
947   case LibFunc_fseek:
948   case LibFunc_fseeko64:
949   case LibFunc_fseeko:
950   case LibFunc_fsetpos:
951   case LibFunc_ftell:
952   case LibFunc_ftello64:
953   case LibFunc_ftello:
954   case LibFunc_ftrylockfile:
955   case LibFunc_funlockfile:
956   case LibFunc_getc:
957   case LibFunc_getc_unlocked:
958   case LibFunc_getlogin_r:
959   case LibFunc_mkdir:
960   case LibFunc_mktime:
961   case LibFunc_times:
962     return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
963 
964   case LibFunc_fopen:
965     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
966             FTy.getParamType(0)->isPointerTy() &&
967             FTy.getParamType(1)->isPointerTy());
968   case LibFunc_fork:
969     return (NumParams == 0 && FTy.getReturnType()->isIntegerTy(32));
970   case LibFunc_fdopen:
971     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
972             FTy.getParamType(1)->isPointerTy());
973   case LibFunc_fputc:
974   case LibFunc_fputc_unlocked:
975   case LibFunc_fstat:
976   case LibFunc_frexp:
977   case LibFunc_frexpf:
978   case LibFunc_frexpl:
979   case LibFunc_fstatvfs:
980     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
981   case LibFunc_fgets:
982   case LibFunc_fgets_unlocked:
983     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
984             FTy.getParamType(2)->isPointerTy());
985   case LibFunc_fread:
986   case LibFunc_fread_unlocked:
987     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
988             FTy.getParamType(3)->isPointerTy());
989   case LibFunc_fwrite:
990   case LibFunc_fwrite_unlocked:
991     return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() &&
992             FTy.getParamType(0)->isPointerTy() &&
993             FTy.getParamType(1)->isIntegerTy() &&
994             FTy.getParamType(2)->isIntegerTy() &&
995             FTy.getParamType(3)->isPointerTy());
996   case LibFunc_fputs:
997   case LibFunc_fputs_unlocked:
998     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
999             FTy.getParamType(1)->isPointerTy());
1000   case LibFunc_fscanf:
1001   case LibFunc_fiprintf:
1002   case LibFunc_small_fprintf:
1003   case LibFunc_fprintf:
1004     return (NumParams >= 2 && FTy.getReturnType()->isIntegerTy() &&
1005             FTy.getParamType(0)->isPointerTy() &&
1006             FTy.getParamType(1)->isPointerTy());
1007   case LibFunc_fgetpos:
1008     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
1009             FTy.getParamType(1)->isPointerTy());
1010   case LibFunc_getchar:
1011   case LibFunc_getchar_unlocked:
1012     return (NumParams == 0 && FTy.getReturnType()->isIntegerTy());
1013   case LibFunc_gets:
1014     return (NumParams == 1 && FTy.getParamType(0) == PCharTy);
1015   case LibFunc_getitimer:
1016     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1017   case LibFunc_ungetc:
1018     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1019   case LibFunc_utime:
1020   case LibFunc_utimes:
1021     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1022             FTy.getParamType(1)->isPointerTy());
1023   case LibFunc_putc:
1024   case LibFunc_putc_unlocked:
1025     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1026   case LibFunc_pread:
1027   case LibFunc_pwrite:
1028     return (NumParams == 4 && FTy.getParamType(1)->isPointerTy());
1029   case LibFunc_popen:
1030     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1031             FTy.getParamType(0)->isPointerTy() &&
1032             FTy.getParamType(1)->isPointerTy());
1033   case LibFunc_vscanf:
1034     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1035   case LibFunc_vsscanf:
1036     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
1037             FTy.getParamType(2)->isPointerTy());
1038   case LibFunc_vfscanf:
1039     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
1040             FTy.getParamType(2)->isPointerTy());
1041   case LibFunc_valloc:
1042     return (FTy.getReturnType()->isPointerTy());
1043   case LibFunc_vprintf:
1044     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
1045   case LibFunc_vfprintf:
1046   case LibFunc_vsprintf:
1047     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
1048             FTy.getParamType(1)->isPointerTy());
1049   case LibFunc_vsprintf_chk:
1050     return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
1051            FTy.getParamType(1)->isIntegerTy(32) &&
1052            IsSizeTTy(FTy.getParamType(2)) && FTy.getParamType(3)->isPointerTy();
1053   case LibFunc_vsnprintf:
1054     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
1055             FTy.getParamType(2)->isPointerTy());
1056   case LibFunc_vsnprintf_chk:
1057     return NumParams == 6 && FTy.getParamType(0)->isPointerTy() &&
1058            FTy.getParamType(2)->isIntegerTy(32) &&
1059            IsSizeTTy(FTy.getParamType(3)) && FTy.getParamType(4)->isPointerTy();
1060   case LibFunc_open:
1061     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
1062   case LibFunc_opendir:
1063     return (NumParams == 1 && FTy.getReturnType()->isPointerTy() &&
1064             FTy.getParamType(0)->isPointerTy());
1065   case LibFunc_tmpfile:
1066     return (FTy.getReturnType()->isPointerTy());
1067   case LibFunc_htonl:
1068   case LibFunc_ntohl:
1069     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1070             FTy.getReturnType() == FTy.getParamType(0));
1071   case LibFunc_htons:
1072   case LibFunc_ntohs:
1073     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(16) &&
1074             FTy.getReturnType() == FTy.getParamType(0));
1075   case LibFunc_lstat:
1076     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1077             FTy.getParamType(1)->isPointerTy());
1078   case LibFunc_lchown:
1079     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
1080   case LibFunc_qsort:
1081     return (NumParams == 4 && FTy.getParamType(3)->isPointerTy());
1082   case LibFunc_dunder_strdup:
1083   case LibFunc_dunder_strndup:
1084     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
1085             FTy.getParamType(0)->isPointerTy());
1086   case LibFunc_dunder_strtok_r:
1087     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
1088   case LibFunc_under_IO_putc:
1089     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1090   case LibFunc_dunder_isoc99_scanf:
1091     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
1092   case LibFunc_stat64:
1093   case LibFunc_lstat64:
1094   case LibFunc_statvfs64:
1095     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1096             FTy.getParamType(1)->isPointerTy());
1097   case LibFunc_dunder_isoc99_sscanf:
1098     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
1099             FTy.getParamType(1)->isPointerTy());
1100   case LibFunc_fopen64:
1101     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1102             FTy.getParamType(0)->isPointerTy() &&
1103             FTy.getParamType(1)->isPointerTy());
1104   case LibFunc_tmpfile64:
1105     return (FTy.getReturnType()->isPointerTy());
1106   case LibFunc_fstat64:
1107   case LibFunc_fstatvfs64:
1108     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1109   case LibFunc_open64:
1110     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
1111   case LibFunc_gettimeofday:
1112     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1113             FTy.getParamType(1)->isPointerTy());
1114 
1115   // new(unsigned int);
1116   case LibFunc_Znwj:
1117   // new(unsigned long);
1118   case LibFunc_Znwm:
1119   // new[](unsigned int);
1120   case LibFunc_Znaj:
1121   // new[](unsigned long);
1122   case LibFunc_Znam:
1123   // new(unsigned int);
1124   case LibFunc_msvc_new_int:
1125   // new(unsigned long long);
1126   case LibFunc_msvc_new_longlong:
1127   // new[](unsigned int);
1128   case LibFunc_msvc_new_array_int:
1129   // new[](unsigned long long);
1130   case LibFunc_msvc_new_array_longlong:
1131     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
1132 
1133   // new(unsigned int, nothrow);
1134   case LibFunc_ZnwjRKSt9nothrow_t:
1135   // new(unsigned long, nothrow);
1136   case LibFunc_ZnwmRKSt9nothrow_t:
1137   // new[](unsigned int, nothrow);
1138   case LibFunc_ZnajRKSt9nothrow_t:
1139   // new[](unsigned long, nothrow);
1140   case LibFunc_ZnamRKSt9nothrow_t:
1141   // new(unsigned int, nothrow);
1142   case LibFunc_msvc_new_int_nothrow:
1143   // new(unsigned long long, nothrow);
1144   case LibFunc_msvc_new_longlong_nothrow:
1145   // new[](unsigned int, nothrow);
1146   case LibFunc_msvc_new_array_int_nothrow:
1147   // new[](unsigned long long, nothrow);
1148   case LibFunc_msvc_new_array_longlong_nothrow:
1149   // new(unsigned int, align_val_t)
1150   case LibFunc_ZnwjSt11align_val_t:
1151   // new(unsigned long, align_val_t)
1152   case LibFunc_ZnwmSt11align_val_t:
1153   // new[](unsigned int, align_val_t)
1154   case LibFunc_ZnajSt11align_val_t:
1155   // new[](unsigned long, align_val_t)
1156   case LibFunc_ZnamSt11align_val_t:
1157     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
1158 
1159   // new(unsigned int, align_val_t, nothrow)
1160   case LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t:
1161   // new(unsigned long, align_val_t, nothrow)
1162   case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t:
1163   // new[](unsigned int, align_val_t, nothrow)
1164   case LibFunc_ZnajSt11align_val_tRKSt9nothrow_t:
1165   // new[](unsigned long, align_val_t, nothrow)
1166   case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t:
1167     return (NumParams == 3 && FTy.getReturnType()->isPointerTy());
1168 
1169   // void operator delete[](void*);
1170   case LibFunc_ZdaPv:
1171   // void operator delete(void*);
1172   case LibFunc_ZdlPv:
1173   // void operator delete[](void*);
1174   case LibFunc_msvc_delete_array_ptr32:
1175   // void operator delete[](void*);
1176   case LibFunc_msvc_delete_array_ptr64:
1177   // void operator delete(void*);
1178   case LibFunc_msvc_delete_ptr32:
1179   // void operator delete(void*);
1180   case LibFunc_msvc_delete_ptr64:
1181     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1182 
1183   // void operator delete[](void*, nothrow);
1184   case LibFunc_ZdaPvRKSt9nothrow_t:
1185   // void operator delete[](void*, unsigned int);
1186   case LibFunc_ZdaPvj:
1187   // void operator delete[](void*, unsigned long);
1188   case LibFunc_ZdaPvm:
1189   // void operator delete(void*, nothrow);
1190   case LibFunc_ZdlPvRKSt9nothrow_t:
1191   // void operator delete(void*, unsigned int);
1192   case LibFunc_ZdlPvj:
1193   // void operator delete(void*, unsigned long);
1194   case LibFunc_ZdlPvm:
1195   // void operator delete(void*, align_val_t)
1196   case LibFunc_ZdlPvSt11align_val_t:
1197   // void operator delete[](void*, align_val_t)
1198   case LibFunc_ZdaPvSt11align_val_t:
1199   // void operator delete[](void*, unsigned int);
1200   case LibFunc_msvc_delete_array_ptr32_int:
1201   // void operator delete[](void*, nothrow);
1202   case LibFunc_msvc_delete_array_ptr32_nothrow:
1203   // void operator delete[](void*, unsigned long long);
1204   case LibFunc_msvc_delete_array_ptr64_longlong:
1205   // void operator delete[](void*, nothrow);
1206   case LibFunc_msvc_delete_array_ptr64_nothrow:
1207   // void operator delete(void*, unsigned int);
1208   case LibFunc_msvc_delete_ptr32_int:
1209   // void operator delete(void*, nothrow);
1210   case LibFunc_msvc_delete_ptr32_nothrow:
1211   // void operator delete(void*, unsigned long long);
1212   case LibFunc_msvc_delete_ptr64_longlong:
1213   // void operator delete(void*, nothrow);
1214   case LibFunc_msvc_delete_ptr64_nothrow:
1215     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
1216 
1217   // void operator delete(void*, align_val_t, nothrow)
1218   case LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t:
1219   // void operator delete[](void*, align_val_t, nothrow)
1220   case LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t:
1221   // void operator delete(void*, unsigned int, align_val_t)
1222   case LibFunc_ZdlPvjSt11align_val_t:
1223   // void operator delete(void*, unsigned long, align_val_t)
1224   case LibFunc_ZdlPvmSt11align_val_t:
1225   // void operator delete[](void*, unsigned int, align_val_t);
1226   case LibFunc_ZdaPvjSt11align_val_t:
1227   // void operator delete[](void*, unsigned long, align_val_t);
1228   case LibFunc_ZdaPvmSt11align_val_t:
1229     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
1230 
1231   case LibFunc_memset_pattern16:
1232     return (!FTy.isVarArg() && NumParams == 3 &&
1233             FTy.getParamType(0)->isPointerTy() &&
1234             FTy.getParamType(1)->isPointerTy() &&
1235             FTy.getParamType(2)->isIntegerTy());
1236 
1237   case LibFunc_cxa_guard_abort:
1238   case LibFunc_cxa_guard_acquire:
1239   case LibFunc_cxa_guard_release:
1240   case LibFunc_nvvm_reflect:
1241     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1242 
1243   case LibFunc_sincospi_stret:
1244   case LibFunc_sincospif_stret:
1245     return (NumParams == 1 && FTy.getParamType(0)->isFloatingPointTy());
1246 
1247   case LibFunc_acos:
1248   case LibFunc_acos_finite:
1249   case LibFunc_acosf:
1250   case LibFunc_acosf_finite:
1251   case LibFunc_acosh:
1252   case LibFunc_acosh_finite:
1253   case LibFunc_acoshf:
1254   case LibFunc_acoshf_finite:
1255   case LibFunc_acoshl:
1256   case LibFunc_acoshl_finite:
1257   case LibFunc_acosl:
1258   case LibFunc_acosl_finite:
1259   case LibFunc_asin:
1260   case LibFunc_asin_finite:
1261   case LibFunc_asinf:
1262   case LibFunc_asinf_finite:
1263   case LibFunc_asinh:
1264   case LibFunc_asinhf:
1265   case LibFunc_asinhl:
1266   case LibFunc_asinl:
1267   case LibFunc_asinl_finite:
1268   case LibFunc_atan:
1269   case LibFunc_atanf:
1270   case LibFunc_atanh:
1271   case LibFunc_atanh_finite:
1272   case LibFunc_atanhf:
1273   case LibFunc_atanhf_finite:
1274   case LibFunc_atanhl:
1275   case LibFunc_atanhl_finite:
1276   case LibFunc_atanl:
1277   case LibFunc_cbrt:
1278   case LibFunc_cbrtf:
1279   case LibFunc_cbrtl:
1280   case LibFunc_ceil:
1281   case LibFunc_ceilf:
1282   case LibFunc_ceill:
1283   case LibFunc_cos:
1284   case LibFunc_cosf:
1285   case LibFunc_cosh:
1286   case LibFunc_cosh_finite:
1287   case LibFunc_coshf:
1288   case LibFunc_coshf_finite:
1289   case LibFunc_coshl:
1290   case LibFunc_coshl_finite:
1291   case LibFunc_cosl:
1292   case LibFunc_exp10:
1293   case LibFunc_exp10_finite:
1294   case LibFunc_exp10f:
1295   case LibFunc_exp10f_finite:
1296   case LibFunc_exp10l:
1297   case LibFunc_exp10l_finite:
1298   case LibFunc_exp2:
1299   case LibFunc_exp2_finite:
1300   case LibFunc_exp2f:
1301   case LibFunc_exp2f_finite:
1302   case LibFunc_exp2l:
1303   case LibFunc_exp2l_finite:
1304   case LibFunc_exp:
1305   case LibFunc_exp_finite:
1306   case LibFunc_expf:
1307   case LibFunc_expf_finite:
1308   case LibFunc_expl:
1309   case LibFunc_expl_finite:
1310   case LibFunc_expm1:
1311   case LibFunc_expm1f:
1312   case LibFunc_expm1l:
1313   case LibFunc_fabs:
1314   case LibFunc_fabsf:
1315   case LibFunc_fabsl:
1316   case LibFunc_floor:
1317   case LibFunc_floorf:
1318   case LibFunc_floorl:
1319   case LibFunc_log10:
1320   case LibFunc_log10_finite:
1321   case LibFunc_log10f:
1322   case LibFunc_log10f_finite:
1323   case LibFunc_log10l:
1324   case LibFunc_log10l_finite:
1325   case LibFunc_log1p:
1326   case LibFunc_log1pf:
1327   case LibFunc_log1pl:
1328   case LibFunc_log2:
1329   case LibFunc_log2_finite:
1330   case LibFunc_log2f:
1331   case LibFunc_log2f_finite:
1332   case LibFunc_log2l:
1333   case LibFunc_log2l_finite:
1334   case LibFunc_log:
1335   case LibFunc_log_finite:
1336   case LibFunc_logb:
1337   case LibFunc_logbf:
1338   case LibFunc_logbl:
1339   case LibFunc_logf:
1340   case LibFunc_logf_finite:
1341   case LibFunc_logl:
1342   case LibFunc_logl_finite:
1343   case LibFunc_nearbyint:
1344   case LibFunc_nearbyintf:
1345   case LibFunc_nearbyintl:
1346   case LibFunc_rint:
1347   case LibFunc_rintf:
1348   case LibFunc_rintl:
1349   case LibFunc_round:
1350   case LibFunc_roundf:
1351   case LibFunc_roundl:
1352   case LibFunc_roundeven:
1353   case LibFunc_roundevenf:
1354   case LibFunc_roundevenl:
1355   case LibFunc_sin:
1356   case LibFunc_sinf:
1357   case LibFunc_sinh:
1358   case LibFunc_sinh_finite:
1359   case LibFunc_sinhf:
1360   case LibFunc_sinhf_finite:
1361   case LibFunc_sinhl:
1362   case LibFunc_sinhl_finite:
1363   case LibFunc_sinl:
1364   case LibFunc_sqrt:
1365   case LibFunc_sqrt_finite:
1366   case LibFunc_sqrtf:
1367   case LibFunc_sqrtf_finite:
1368   case LibFunc_sqrtl:
1369   case LibFunc_sqrtl_finite:
1370   case LibFunc_tan:
1371   case LibFunc_tanf:
1372   case LibFunc_tanh:
1373   case LibFunc_tanhf:
1374   case LibFunc_tanhl:
1375   case LibFunc_tanl:
1376   case LibFunc_trunc:
1377   case LibFunc_truncf:
1378   case LibFunc_truncl:
1379     return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() &&
1380             FTy.getReturnType() == FTy.getParamType(0));
1381 
1382   case LibFunc_atan2:
1383   case LibFunc_atan2_finite:
1384   case LibFunc_atan2f:
1385   case LibFunc_atan2f_finite:
1386   case LibFunc_atan2l:
1387   case LibFunc_atan2l_finite:
1388   case LibFunc_fmin:
1389   case LibFunc_fminf:
1390   case LibFunc_fminl:
1391   case LibFunc_fmax:
1392   case LibFunc_fmaxf:
1393   case LibFunc_fmaxl:
1394   case LibFunc_fmod:
1395   case LibFunc_fmodf:
1396   case LibFunc_fmodl:
1397   case LibFunc_remainder:
1398   case LibFunc_remainderf:
1399   case LibFunc_remainderl:
1400   case LibFunc_copysign:
1401   case LibFunc_copysignf:
1402   case LibFunc_copysignl:
1403   case LibFunc_pow:
1404   case LibFunc_pow_finite:
1405   case LibFunc_powf:
1406   case LibFunc_powf_finite:
1407   case LibFunc_powl:
1408   case LibFunc_powl_finite:
1409     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
1410             FTy.getReturnType() == FTy.getParamType(0) &&
1411             FTy.getReturnType() == FTy.getParamType(1));
1412 
1413   case LibFunc_ldexp:
1414   case LibFunc_ldexpf:
1415   case LibFunc_ldexpl:
1416     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
1417             FTy.getReturnType() == FTy.getParamType(0) &&
1418             FTy.getParamType(1)->isIntegerTy(32));
1419 
1420   case LibFunc_ffs:
1421   case LibFunc_ffsl:
1422   case LibFunc_ffsll:
1423   case LibFunc_fls:
1424   case LibFunc_flsl:
1425   case LibFunc_flsll:
1426     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1427             FTy.getParamType(0)->isIntegerTy());
1428 
1429   case LibFunc_isdigit:
1430   case LibFunc_isascii:
1431   case LibFunc_toascii:
1432   case LibFunc_putchar:
1433   case LibFunc_putchar_unlocked:
1434     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1435             FTy.getReturnType() == FTy.getParamType(0));
1436 
1437   case LibFunc_abs:
1438   case LibFunc_labs:
1439   case LibFunc_llabs:
1440     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() &&
1441             FTy.getReturnType() == FTy.getParamType(0));
1442 
1443   case LibFunc_cxa_atexit:
1444     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() &&
1445             FTy.getParamType(0)->isPointerTy() &&
1446             FTy.getParamType(1)->isPointerTy() &&
1447             FTy.getParamType(2)->isPointerTy());
1448 
1449   case LibFunc_sinpi:
1450   case LibFunc_cospi:
1451     return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() &&
1452             FTy.getReturnType() == FTy.getParamType(0));
1453 
1454   case LibFunc_sinpif:
1455   case LibFunc_cospif:
1456     return (NumParams == 1 && FTy.getReturnType()->isFloatTy() &&
1457             FTy.getReturnType() == FTy.getParamType(0));
1458 
1459   case LibFunc_strnlen:
1460     return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(1) &&
1461             FTy.getParamType(0) == PCharTy &&
1462             FTy.getParamType(1) == SizeTTy);
1463 
1464   case LibFunc_posix_memalign:
1465     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
1466             FTy.getParamType(0)->isPointerTy() &&
1467             FTy.getParamType(1) == SizeTTy && FTy.getParamType(2) == SizeTTy);
1468 
1469   case LibFunc_wcslen:
1470     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
1471             FTy.getReturnType()->isIntegerTy());
1472 
1473   case LibFunc_cabs:
1474   case LibFunc_cabsf:
1475   case LibFunc_cabsl: {
1476     Type* RetTy = FTy.getReturnType();
1477     if (!RetTy->isFloatingPointTy())
1478       return false;
1479 
1480     // NOTE: These prototypes are target specific and currently support
1481     // "complex" passed as an array or discrete real & imaginary parameters.
1482     // Add other calling conventions to enable libcall optimizations.
1483     if (NumParams == 1)
1484       return (FTy.getParamType(0)->isArrayTy() &&
1485               FTy.getParamType(0)->getArrayNumElements() == 2 &&
1486               FTy.getParamType(0)->getArrayElementType() == RetTy);
1487     else if (NumParams == 2)
1488       return (FTy.getParamType(0) == RetTy && FTy.getParamType(1) == RetTy);
1489     else
1490       return false;
1491   }
1492   case LibFunc::NumLibFuncs:
1493   case LibFunc::NotLibFunc:
1494     break;
1495   }
1496 
1497   llvm_unreachable("Invalid libfunc");
1498 }
1499 
1500 bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
1501                                        LibFunc &F) const {
1502   // Intrinsics don't overlap w/libcalls; if our module has a large number of
1503   // intrinsics, this ends up being an interesting compile time win since we
1504   // avoid string normalization and comparison.
1505   if (FDecl.isIntrinsic()) return false;
1506 
1507   const DataLayout *DL =
1508       FDecl.getParent() ? &FDecl.getParent()->getDataLayout() : nullptr;
1509   return getLibFunc(FDecl.getName(), F) &&
1510          isValidProtoForLibFunc(*FDecl.getFunctionType(), F, DL);
1511 }
1512 
1513 void TargetLibraryInfoImpl::disableAllFunctions() {
1514   memset(AvailableArray, 0, sizeof(AvailableArray));
1515 }
1516 
1517 static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
1518   return LHS.ScalarFnName < RHS.ScalarFnName;
1519 }
1520 
1521 static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
1522   return LHS.VectorFnName < RHS.VectorFnName;
1523 }
1524 
1525 static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
1526   return LHS.ScalarFnName < S;
1527 }
1528 
1529 static bool compareWithVectorFnName(const VecDesc &LHS, StringRef S) {
1530   return LHS.VectorFnName < S;
1531 }
1532 
1533 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
1534   VectorDescs.insert(VectorDescs.end(), Fns.begin(), Fns.end());
1535   llvm::sort(VectorDescs, compareByScalarFnName);
1536 
1537   ScalarDescs.insert(ScalarDescs.end(), Fns.begin(), Fns.end());
1538   llvm::sort(ScalarDescs, compareByVectorFnName);
1539 }
1540 
1541 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
1542     enum VectorLibrary VecLib) {
1543   switch (VecLib) {
1544   case Accelerate: {
1545     const VecDesc VecFuncs[] = {
1546     #define TLI_DEFINE_ACCELERATE_VECFUNCS
1547     #include "llvm/Analysis/VecFuncs.def"
1548     };
1549     addVectorizableFunctions(VecFuncs);
1550     break;
1551   }
1552   case MASSV: {
1553     const VecDesc VecFuncs[] = {
1554     #define TLI_DEFINE_MASSV_VECFUNCS
1555     #include "llvm/Analysis/VecFuncs.def"
1556     };
1557     addVectorizableFunctions(VecFuncs);
1558     break;
1559   }
1560   case SVML: {
1561     const VecDesc VecFuncs[] = {
1562     #define TLI_DEFINE_SVML_VECFUNCS
1563     #include "llvm/Analysis/VecFuncs.def"
1564     };
1565     addVectorizableFunctions(VecFuncs);
1566     break;
1567   }
1568   case NoLibrary:
1569     break;
1570   }
1571 }
1572 
1573 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
1574   funcName = sanitizeFunctionName(funcName);
1575   if (funcName.empty())
1576     return false;
1577 
1578   std::vector<VecDesc>::const_iterator I =
1579       llvm::lower_bound(VectorDescs, funcName, compareWithScalarFnName);
1580   return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
1581 }
1582 
1583 StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
1584                                                        unsigned VF) const {
1585   F = sanitizeFunctionName(F);
1586   if (F.empty())
1587     return F;
1588   std::vector<VecDesc>::const_iterator I =
1589       llvm::lower_bound(VectorDescs, F, compareWithScalarFnName);
1590   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
1591     if (I->VectorizationFactor == VF)
1592       return I->VectorFnName;
1593     ++I;
1594   }
1595   return StringRef();
1596 }
1597 
1598 StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F,
1599                                                        unsigned &VF) const {
1600   F = sanitizeFunctionName(F);
1601   if (F.empty())
1602     return F;
1603 
1604   std::vector<VecDesc>::const_iterator I =
1605       llvm::lower_bound(ScalarDescs, F, compareWithVectorFnName);
1606   if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F)
1607     return StringRef();
1608   VF = I->VectorizationFactor;
1609   return I->ScalarFnName;
1610 }
1611 
1612 TargetLibraryInfo TargetLibraryAnalysis::run(const Function &F,
1613                                              FunctionAnalysisManager &) {
1614   if (!BaselineInfoImpl)
1615     BaselineInfoImpl =
1616         TargetLibraryInfoImpl(Triple(F.getParent()->getTargetTriple()));
1617   return TargetLibraryInfo(*BaselineInfoImpl, &F);
1618 }
1619 
1620 unsigned TargetLibraryInfoImpl::getWCharSize(const Module &M) const {
1621   if (auto *ShortWChar = cast_or_null<ConstantAsMetadata>(
1622       M.getModuleFlag("wchar_size")))
1623     return cast<ConstantInt>(ShortWChar->getValue())->getZExtValue();
1624   return 0;
1625 }
1626 
1627 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
1628     : ImmutablePass(ID), TLA(TargetLibraryInfoImpl()) {
1629   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1630 }
1631 
1632 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
1633     : ImmutablePass(ID), TLA(TargetLibraryInfoImpl(T)) {
1634   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1635 }
1636 
1637 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
1638     const TargetLibraryInfoImpl &TLIImpl)
1639     : ImmutablePass(ID), TLA(TLIImpl) {
1640   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1641 }
1642 
1643 AnalysisKey TargetLibraryAnalysis::Key;
1644 
1645 // Register the basic pass.
1646 INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
1647                 "Target Library Information", false, true)
1648 char TargetLibraryInfoWrapperPass::ID = 0;
1649 
1650 void TargetLibraryInfoWrapperPass::anchor() {}
1651 
1652 unsigned TargetLibraryInfoImpl::getWidestVF(StringRef ScalarF) const {
1653   ScalarF = sanitizeFunctionName(ScalarF);
1654   if (ScalarF.empty())
1655     return 1;
1656 
1657   unsigned VF = 1;
1658   std::vector<VecDesc>::const_iterator I =
1659       llvm::lower_bound(VectorDescs, ScalarF, compareWithScalarFnName);
1660   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == ScalarF) {
1661     if (I->VectorizationFactor > VF)
1662       VF = I->VectorizationFactor;
1663     ++I;
1664   }
1665   return VF;
1666 }
1667