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