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