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