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   // As currently implemented in clang, NVPTX code has no standard library to
599   // speak of.  Headers provide a standard-ish library implementation, but many
600   // of the signatures are wrong -- for example, many libm functions are not
601   // extern "C".
602   //
603   // libdevice, an IR library provided by nvidia, is linked in by the front-end,
604   // but only used functions are provided to llvm.  Moreover, most of the
605   // functions in libdevice don't map precisely to standard library functions.
606   //
607   // FIXME: Having no standard library prevents e.g. many fastmath
608   // optimizations, so this situation should be fixed.
609   if (T.isNVPTX()) {
610     TLI.disableAllFunctions();
611     TLI.setAvailable(LibFunc_nvvm_reflect);
612     TLI.setAvailable(llvm::LibFunc_malloc);
613     TLI.setAvailable(llvm::LibFunc_free);
614 
615     // TODO: We could enable the following two according to [0] but we haven't
616     //       done an evaluation wrt. the performance implications.
617     // [0]
618     // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#dynamic-global-memory-allocation-and-operations
619     //
620     //    TLI.setAvailable(llvm::LibFunc_memcpy);
621     //    TLI.setAvailable(llvm::LibFunc_memset);
622 
623     TLI.setAvailable(llvm::LibFunc___kmpc_alloc_shared);
624     TLI.setAvailable(llvm::LibFunc___kmpc_free_shared);
625   } else {
626     TLI.setUnavailable(LibFunc_nvvm_reflect);
627   }
628 
629   // These vec_malloc/free routines are only available on AIX.
630   if (!T.isOSAIX()) {
631     TLI.setUnavailable(LibFunc_vec_calloc);
632     TLI.setUnavailable(LibFunc_vec_malloc);
633     TLI.setUnavailable(LibFunc_vec_realloc);
634     TLI.setUnavailable(LibFunc_vec_free);
635   }
636 
637   TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary);
638 }
639 
640 TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
641   // Default to everything being available.
642   memset(AvailableArray, -1, sizeof(AvailableArray));
643 
644   initialize(*this, Triple(), StandardNames);
645 }
646 
647 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
648   // Default to everything being available.
649   memset(AvailableArray, -1, sizeof(AvailableArray));
650 
651   initialize(*this, T, StandardNames);
652 }
653 
654 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
655     : CustomNames(TLI.CustomNames), ShouldExtI32Param(TLI.ShouldExtI32Param),
656       ShouldExtI32Return(TLI.ShouldExtI32Return),
657       ShouldSignExtI32Param(TLI.ShouldSignExtI32Param),
658       SizeOfInt(TLI.SizeOfInt) {
659   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
660   VectorDescs = TLI.VectorDescs;
661   ScalarDescs = TLI.ScalarDescs;
662 }
663 
664 TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
665     : CustomNames(std::move(TLI.CustomNames)),
666       ShouldExtI32Param(TLI.ShouldExtI32Param),
667       ShouldExtI32Return(TLI.ShouldExtI32Return),
668       ShouldSignExtI32Param(TLI.ShouldSignExtI32Param),
669       SizeOfInt(TLI.SizeOfInt) {
670   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
671             AvailableArray);
672   VectorDescs = TLI.VectorDescs;
673   ScalarDescs = TLI.ScalarDescs;
674 }
675 
676 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
677   CustomNames = TLI.CustomNames;
678   ShouldExtI32Param = TLI.ShouldExtI32Param;
679   ShouldExtI32Return = TLI.ShouldExtI32Return;
680   ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
681   SizeOfInt = TLI.SizeOfInt;
682   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
683   return *this;
684 }
685 
686 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
687   CustomNames = std::move(TLI.CustomNames);
688   ShouldExtI32Param = TLI.ShouldExtI32Param;
689   ShouldExtI32Return = TLI.ShouldExtI32Return;
690   ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
691   SizeOfInt = TLI.SizeOfInt;
692   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
693             AvailableArray);
694   return *this;
695 }
696 
697 static StringRef sanitizeFunctionName(StringRef funcName) {
698   // Filter out empty names and names containing null bytes, those can't be in
699   // our table.
700   if (funcName.empty() || funcName.find('\0') != StringRef::npos)
701     return StringRef();
702 
703   // Check for \01 prefix that is used to mangle __asm declarations and
704   // strip it if present.
705   return GlobalValue::dropLLVMManglingEscape(funcName);
706 }
707 
708 bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName, LibFunc &F) const {
709   funcName = sanitizeFunctionName(funcName);
710   if (funcName.empty())
711     return false;
712 
713   const auto *Start = std::begin(StandardNames);
714   const auto *End = std::end(StandardNames);
715   const auto *I = std::lower_bound(Start, End, funcName);
716   if (I != End && *I == funcName) {
717     F = (LibFunc)(I - Start);
718     return true;
719   }
720   return false;
721 }
722 
723 bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
724                                                    LibFunc F,
725                                                    const DataLayout *DL) const {
726   LLVMContext &Ctx = FTy.getContext();
727   Type *SizeTTy = DL ? DL->getIntPtrType(Ctx, /*AddressSpace=*/0) : nullptr;
728   auto IsSizeTTy = [SizeTTy](Type *Ty) {
729     return SizeTTy ? Ty == SizeTTy : Ty->isIntegerTy();
730   };
731   unsigned NumParams = FTy.getNumParams();
732 
733   switch (F) {
734   case LibFunc_execl:
735   case LibFunc_execlp:
736   case LibFunc_execle:
737     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
738             FTy.getParamType(1)->isPointerTy() &&
739             FTy.getReturnType()->isIntegerTy(32));
740   case LibFunc_execv:
741   case LibFunc_execvp:
742     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
743             FTy.getParamType(1)->isPointerTy() &&
744             FTy.getReturnType()->isIntegerTy(32));
745   case LibFunc_execvP:
746   case LibFunc_execvpe:
747   case LibFunc_execve:
748     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
749             FTy.getParamType(1)->isPointerTy() &&
750             FTy.getParamType(2)->isPointerTy() &&
751             FTy.getReturnType()->isIntegerTy(32));
752   case LibFunc_strlen_chk:
753     --NumParams;
754     if (!IsSizeTTy(FTy.getParamType(NumParams)))
755       return false;
756     LLVM_FALLTHROUGH;
757   case LibFunc_strlen:
758     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
759             FTy.getReturnType()->isIntegerTy());
760 
761   case LibFunc_strchr:
762   case LibFunc_strrchr:
763     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
764             FTy.getParamType(0) == FTy.getReturnType() &&
765             FTy.getParamType(1)->isIntegerTy());
766 
767   case LibFunc_strtol:
768   case LibFunc_strtod:
769   case LibFunc_strtof:
770   case LibFunc_strtoul:
771   case LibFunc_strtoll:
772   case LibFunc_strtold:
773   case LibFunc_strtoull:
774     return ((NumParams == 2 || NumParams == 3) &&
775             FTy.getParamType(0)->isPointerTy() &&
776             FTy.getParamType(1)->isPointerTy());
777   case LibFunc_strcat_chk:
778     --NumParams;
779     if (!IsSizeTTy(FTy.getParamType(NumParams)))
780       return false;
781     LLVM_FALLTHROUGH;
782   case LibFunc_strcat:
783     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
784             FTy.getParamType(0) == FTy.getReturnType() &&
785             FTy.getParamType(1) == FTy.getReturnType());
786 
787   case LibFunc_strncat_chk:
788     --NumParams;
789     if (!IsSizeTTy(FTy.getParamType(NumParams)))
790       return false;
791     LLVM_FALLTHROUGH;
792   case LibFunc_strncat:
793     return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
794             FTy.getParamType(0) == FTy.getReturnType() &&
795             FTy.getParamType(1) == FTy.getReturnType() &&
796             IsSizeTTy(FTy.getParamType(2)));
797 
798   case LibFunc_strcpy_chk:
799   case LibFunc_stpcpy_chk:
800     --NumParams;
801     if (!IsSizeTTy(FTy.getParamType(NumParams)))
802       return false;
803     LLVM_FALLTHROUGH;
804   case LibFunc_strcpy:
805   case LibFunc_stpcpy:
806     return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(0) &&
807             FTy.getParamType(0) == FTy.getParamType(1) &&
808             FTy.getParamType(0)->isPointerTy());
809 
810   case LibFunc_strlcat_chk:
811   case LibFunc_strlcpy_chk:
812     --NumParams;
813     if (!IsSizeTTy(FTy.getParamType(NumParams)))
814       return false;
815     LLVM_FALLTHROUGH;
816   case LibFunc_strlcat:
817   case LibFunc_strlcpy:
818     return NumParams == 3 && IsSizeTTy(FTy.getReturnType()) &&
819            FTy.getParamType(0)->isPointerTy() &&
820            FTy.getParamType(1)->isPointerTy() &&
821            IsSizeTTy(FTy.getParamType(2));
822 
823   case LibFunc_strncpy_chk:
824   case LibFunc_stpncpy_chk:
825     --NumParams;
826     if (!IsSizeTTy(FTy.getParamType(NumParams)))
827       return false;
828     LLVM_FALLTHROUGH;
829   case LibFunc_strncpy:
830   case LibFunc_stpncpy:
831     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
832             FTy.getParamType(0) == FTy.getParamType(1) &&
833             FTy.getParamType(0)->isPointerTy() &&
834             IsSizeTTy(FTy.getParamType(2)));
835 
836   case LibFunc_strxfrm:
837     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
838             FTy.getParamType(1)->isPointerTy());
839 
840   case LibFunc_strcmp:
841     return (NumParams == 2 && FTy.getReturnType()->isIntegerTy(32) &&
842             FTy.getParamType(0)->isPointerTy() &&
843             FTy.getParamType(0) == FTy.getParamType(1));
844 
845   case LibFunc_strncmp:
846     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
847             FTy.getParamType(0)->isPointerTy() &&
848             FTy.getParamType(0) == FTy.getParamType(1) &&
849             IsSizeTTy(FTy.getParamType(2)));
850 
851   case LibFunc_strspn:
852   case LibFunc_strcspn:
853     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
854             FTy.getParamType(0) == FTy.getParamType(1) &&
855             FTy.getReturnType()->isIntegerTy());
856 
857   case LibFunc_strcoll:
858   case LibFunc_strcasecmp:
859   case LibFunc_strncasecmp:
860     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
861             FTy.getParamType(1)->isPointerTy());
862 
863   case LibFunc_strstr:
864     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
865             FTy.getParamType(0)->isPointerTy() &&
866             FTy.getParamType(1)->isPointerTy());
867 
868   case LibFunc_strpbrk:
869     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
870             FTy.getReturnType() == FTy.getParamType(0) &&
871             FTy.getParamType(0) == FTy.getParamType(1));
872 
873   case LibFunc_strtok:
874   case LibFunc_strtok_r:
875     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
876   case LibFunc_scanf:
877   case LibFunc_setbuf:
878   case LibFunc_setvbuf:
879     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
880   case LibFunc_strdup:
881   case LibFunc_strndup:
882     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
883             FTy.getParamType(0)->isPointerTy());
884   case LibFunc_sscanf:
885   case LibFunc_stat:
886   case LibFunc_statvfs:
887   case LibFunc_siprintf:
888   case LibFunc_small_sprintf:
889   case LibFunc_sprintf:
890     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
891             FTy.getParamType(1)->isPointerTy() &&
892             FTy.getReturnType()->isIntegerTy(32));
893 
894   case LibFunc_sprintf_chk:
895     return NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
896            FTy.getParamType(1)->isIntegerTy(32) &&
897            IsSizeTTy(FTy.getParamType(2)) &&
898            FTy.getParamType(3)->isPointerTy() &&
899            FTy.getReturnType()->isIntegerTy(32);
900 
901   case LibFunc_snprintf:
902     return NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
903            IsSizeTTy(FTy.getParamType(1)) &&
904            FTy.getParamType(2)->isPointerTy() &&
905            FTy.getReturnType()->isIntegerTy(32);
906 
907   case LibFunc_snprintf_chk:
908     return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
909            IsSizeTTy(FTy.getParamType(1)) &&
910            FTy.getParamType(2)->isIntegerTy(32) &&
911            IsSizeTTy(FTy.getParamType(3)) &&
912            FTy.getParamType(4)->isPointerTy() &&
913            FTy.getReturnType()->isIntegerTy(32);
914 
915   case LibFunc_setitimer:
916     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
917             FTy.getParamType(2)->isPointerTy());
918   case LibFunc_system:
919     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
920   case LibFunc___kmpc_alloc_shared:
921   case LibFunc_malloc:
922   case LibFunc_vec_malloc:
923     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
924   case LibFunc_memcmp:
925     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
926             FTy.getParamType(0)->isPointerTy() &&
927             FTy.getParamType(1)->isPointerTy());
928 
929   case LibFunc_memchr:
930   case LibFunc_memrchr:
931     return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
932             FTy.getReturnType() == FTy.getParamType(0) &&
933             FTy.getParamType(1)->isIntegerTy(32) &&
934             IsSizeTTy(FTy.getParamType(2)));
935   case LibFunc_modf:
936   case LibFunc_modff:
937   case LibFunc_modfl:
938     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
939 
940   case LibFunc_memcpy_chk:
941   case LibFunc_mempcpy_chk:
942   case LibFunc_memmove_chk:
943     --NumParams;
944     if (!IsSizeTTy(FTy.getParamType(NumParams)))
945       return false;
946     LLVM_FALLTHROUGH;
947   case LibFunc_memcpy:
948   case LibFunc_mempcpy:
949   case LibFunc_memmove:
950     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
951             FTy.getParamType(0)->isPointerTy() &&
952             FTy.getParamType(1)->isPointerTy() &&
953             IsSizeTTy(FTy.getParamType(2)));
954 
955   case LibFunc_memset_chk:
956     --NumParams;
957     if (!IsSizeTTy(FTy.getParamType(NumParams)))
958       return false;
959     LLVM_FALLTHROUGH;
960   case LibFunc_memset:
961     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
962             FTy.getParamType(0)->isPointerTy() &&
963             FTy.getParamType(1)->isIntegerTy() &&
964             IsSizeTTy(FTy.getParamType(2)));
965 
966   case LibFunc_memccpy_chk:
967       --NumParams;
968     if (!IsSizeTTy(FTy.getParamType(NumParams)))
969       return false;
970     LLVM_FALLTHROUGH;
971   case LibFunc_memccpy:
972     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
973   case LibFunc_memalign:
974     return (FTy.getReturnType()->isPointerTy());
975   case LibFunc_realloc:
976   case LibFunc_reallocf:
977   case LibFunc_vec_realloc:
978     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
979             FTy.getParamType(0) == FTy.getReturnType() &&
980             IsSizeTTy(FTy.getParamType(1)));
981   case LibFunc_read:
982     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
983   case LibFunc_rewind:
984   case LibFunc_rmdir:
985   case LibFunc_remove:
986   case LibFunc_realpath:
987     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
988   case LibFunc_rename:
989     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
990             FTy.getParamType(1)->isPointerTy());
991   case LibFunc_readlink:
992     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
993             FTy.getParamType(1)->isPointerTy());
994   case LibFunc_write:
995     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
996   case LibFunc_aligned_alloc:
997     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
998   case LibFunc_bcopy:
999   case LibFunc_bcmp:
1000     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
1001             FTy.getParamType(1)->isPointerTy());
1002   case LibFunc_bzero:
1003     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
1004   case LibFunc_calloc:
1005   case LibFunc_vec_calloc:
1006     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1007             FTy.getParamType(0) == FTy.getParamType(1));
1008 
1009   case LibFunc_atof:
1010   case LibFunc_atoi:
1011   case LibFunc_atol:
1012   case LibFunc_atoll:
1013   case LibFunc_ferror:
1014   case LibFunc_getenv:
1015   case LibFunc_getpwnam:
1016   case LibFunc_iprintf:
1017   case LibFunc_small_printf:
1018   case LibFunc_pclose:
1019   case LibFunc_perror:
1020   case LibFunc_printf:
1021   case LibFunc_puts:
1022   case LibFunc_uname:
1023   case LibFunc_under_IO_getc:
1024   case LibFunc_unlink:
1025   case LibFunc_unsetenv:
1026     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1027 
1028   case LibFunc_access:
1029   case LibFunc_chmod:
1030   case LibFunc_chown:
1031   case LibFunc_clearerr:
1032   case LibFunc_closedir:
1033   case LibFunc_ctermid:
1034   case LibFunc_fclose:
1035   case LibFunc_feof:
1036   case LibFunc_fflush:
1037   case LibFunc_fgetc:
1038   case LibFunc_fgetc_unlocked:
1039   case LibFunc_fileno:
1040   case LibFunc_flockfile:
1041   case LibFunc_free:
1042   case LibFunc_fseek:
1043   case LibFunc_fseeko64:
1044   case LibFunc_fseeko:
1045   case LibFunc_fsetpos:
1046   case LibFunc_ftell:
1047   case LibFunc_ftello64:
1048   case LibFunc_ftello:
1049   case LibFunc_ftrylockfile:
1050   case LibFunc_funlockfile:
1051   case LibFunc_getc:
1052   case LibFunc_getc_unlocked:
1053   case LibFunc_getlogin_r:
1054   case LibFunc_mkdir:
1055   case LibFunc_mktime:
1056   case LibFunc_times:
1057   case LibFunc_vec_free:
1058     return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
1059   case LibFunc___kmpc_free_shared:
1060     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1061             IsSizeTTy(FTy.getParamType(1)));
1062 
1063   case LibFunc_fopen:
1064     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1065             FTy.getParamType(0)->isPointerTy() &&
1066             FTy.getParamType(1)->isPointerTy());
1067   case LibFunc_fork:
1068     return (NumParams == 0 && FTy.getReturnType()->isIntegerTy(32));
1069   case LibFunc_fdopen:
1070     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1071             FTy.getParamType(1)->isPointerTy());
1072   case LibFunc_fputc:
1073   case LibFunc_fputc_unlocked:
1074   case LibFunc_fstat:
1075   case LibFunc_frexp:
1076   case LibFunc_frexpf:
1077   case LibFunc_frexpl:
1078   case LibFunc_fstatvfs:
1079     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1080   case LibFunc_fgets:
1081   case LibFunc_fgets_unlocked:
1082     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
1083             FTy.getParamType(2)->isPointerTy());
1084   case LibFunc_fread:
1085   case LibFunc_fread_unlocked:
1086     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
1087             FTy.getParamType(3)->isPointerTy());
1088   case LibFunc_fwrite:
1089   case LibFunc_fwrite_unlocked:
1090     return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() &&
1091             FTy.getParamType(0)->isPointerTy() &&
1092             FTy.getParamType(1)->isIntegerTy() &&
1093             FTy.getParamType(2)->isIntegerTy() &&
1094             FTy.getParamType(3)->isPointerTy());
1095   case LibFunc_fputs:
1096   case LibFunc_fputs_unlocked:
1097     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
1098             FTy.getParamType(1)->isPointerTy());
1099   case LibFunc_fscanf:
1100   case LibFunc_fiprintf:
1101   case LibFunc_small_fprintf:
1102   case LibFunc_fprintf:
1103     return (NumParams >= 2 && FTy.getReturnType()->isIntegerTy() &&
1104             FTy.getParamType(0)->isPointerTy() &&
1105             FTy.getParamType(1)->isPointerTy());
1106   case LibFunc_fgetpos:
1107     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
1108             FTy.getParamType(1)->isPointerTy());
1109   case LibFunc_getchar:
1110   case LibFunc_getchar_unlocked:
1111     return (NumParams == 0 && FTy.getReturnType()->isIntegerTy());
1112   case LibFunc_gets:
1113     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1114   case LibFunc_getitimer:
1115     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1116   case LibFunc_ungetc:
1117     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1118   case LibFunc_utime:
1119   case LibFunc_utimes:
1120     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1121             FTy.getParamType(1)->isPointerTy());
1122   case LibFunc_putc:
1123   case LibFunc_putc_unlocked:
1124     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1125   case LibFunc_pread:
1126   case LibFunc_pwrite:
1127     return (NumParams == 4 && FTy.getParamType(1)->isPointerTy());
1128   case LibFunc_popen:
1129     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1130             FTy.getParamType(0)->isPointerTy() &&
1131             FTy.getParamType(1)->isPointerTy());
1132   case LibFunc_vscanf:
1133     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1134   case LibFunc_vsscanf:
1135     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
1136             FTy.getParamType(2)->isPointerTy());
1137   case LibFunc_vfscanf:
1138     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
1139             FTy.getParamType(2)->isPointerTy());
1140   case LibFunc_valloc:
1141     return (FTy.getReturnType()->isPointerTy());
1142   case LibFunc_vprintf:
1143     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
1144   case LibFunc_vfprintf:
1145   case LibFunc_vsprintf:
1146     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
1147             FTy.getParamType(1)->isPointerTy());
1148   case LibFunc_vsprintf_chk:
1149     return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
1150            FTy.getParamType(1)->isIntegerTy(32) &&
1151            IsSizeTTy(FTy.getParamType(2)) && FTy.getParamType(3)->isPointerTy();
1152   case LibFunc_vsnprintf:
1153     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
1154             FTy.getParamType(2)->isPointerTy());
1155   case LibFunc_vsnprintf_chk:
1156     return NumParams == 6 && FTy.getParamType(0)->isPointerTy() &&
1157            FTy.getParamType(2)->isIntegerTy(32) &&
1158            IsSizeTTy(FTy.getParamType(3)) && FTy.getParamType(4)->isPointerTy();
1159   case LibFunc_open:
1160     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
1161   case LibFunc_opendir:
1162     return (NumParams == 1 && FTy.getReturnType()->isPointerTy() &&
1163             FTy.getParamType(0)->isPointerTy());
1164   case LibFunc_tmpfile:
1165     return (FTy.getReturnType()->isPointerTy());
1166   case LibFunc_htonl:
1167   case LibFunc_ntohl:
1168     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1169             FTy.getReturnType() == FTy.getParamType(0));
1170   case LibFunc_htons:
1171   case LibFunc_ntohs:
1172     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(16) &&
1173             FTy.getReturnType() == FTy.getParamType(0));
1174   case LibFunc_lstat:
1175     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1176             FTy.getParamType(1)->isPointerTy());
1177   case LibFunc_lchown:
1178     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
1179   case LibFunc_qsort:
1180     return (NumParams == 4 && FTy.getParamType(3)->isPointerTy());
1181   case LibFunc_dunder_strdup:
1182   case LibFunc_dunder_strndup:
1183     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
1184             FTy.getParamType(0)->isPointerTy());
1185   case LibFunc_dunder_strtok_r:
1186     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
1187   case LibFunc_under_IO_putc:
1188     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1189   case LibFunc_dunder_isoc99_scanf:
1190     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
1191   case LibFunc_stat64:
1192   case LibFunc_lstat64:
1193   case LibFunc_statvfs64:
1194     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1195             FTy.getParamType(1)->isPointerTy());
1196   case LibFunc_dunder_isoc99_sscanf:
1197     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
1198             FTy.getParamType(1)->isPointerTy());
1199   case LibFunc_fopen64:
1200     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1201             FTy.getParamType(0)->isPointerTy() &&
1202             FTy.getParamType(1)->isPointerTy());
1203   case LibFunc_tmpfile64:
1204     return (FTy.getReturnType()->isPointerTy());
1205   case LibFunc_fstat64:
1206   case LibFunc_fstatvfs64:
1207     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1208   case LibFunc_open64:
1209     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
1210   case LibFunc_gettimeofday:
1211     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1212             FTy.getParamType(1)->isPointerTy());
1213 
1214   // new(unsigned int);
1215   case LibFunc_Znwj:
1216   // new(unsigned long);
1217   case LibFunc_Znwm:
1218   // new[](unsigned int);
1219   case LibFunc_Znaj:
1220   // new[](unsigned long);
1221   case LibFunc_Znam:
1222   // new(unsigned int);
1223   case LibFunc_msvc_new_int:
1224   // new(unsigned long long);
1225   case LibFunc_msvc_new_longlong:
1226   // new[](unsigned int);
1227   case LibFunc_msvc_new_array_int:
1228   // new[](unsigned long long);
1229   case LibFunc_msvc_new_array_longlong:
1230     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
1231 
1232   // new(unsigned int, nothrow);
1233   case LibFunc_ZnwjRKSt9nothrow_t:
1234   // new(unsigned long, nothrow);
1235   case LibFunc_ZnwmRKSt9nothrow_t:
1236   // new[](unsigned int, nothrow);
1237   case LibFunc_ZnajRKSt9nothrow_t:
1238   // new[](unsigned long, nothrow);
1239   case LibFunc_ZnamRKSt9nothrow_t:
1240   // new(unsigned int, nothrow);
1241   case LibFunc_msvc_new_int_nothrow:
1242   // new(unsigned long long, nothrow);
1243   case LibFunc_msvc_new_longlong_nothrow:
1244   // new[](unsigned int, nothrow);
1245   case LibFunc_msvc_new_array_int_nothrow:
1246   // new[](unsigned long long, nothrow);
1247   case LibFunc_msvc_new_array_longlong_nothrow:
1248   // new(unsigned int, align_val_t)
1249   case LibFunc_ZnwjSt11align_val_t:
1250   // new(unsigned long, align_val_t)
1251   case LibFunc_ZnwmSt11align_val_t:
1252   // new[](unsigned int, align_val_t)
1253   case LibFunc_ZnajSt11align_val_t:
1254   // new[](unsigned long, align_val_t)
1255   case LibFunc_ZnamSt11align_val_t:
1256     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
1257 
1258   // new(unsigned int, align_val_t, nothrow)
1259   case LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t:
1260   // new(unsigned long, align_val_t, nothrow)
1261   case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t:
1262   // new[](unsigned int, align_val_t, nothrow)
1263   case LibFunc_ZnajSt11align_val_tRKSt9nothrow_t:
1264   // new[](unsigned long, align_val_t, nothrow)
1265   case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t:
1266     return (NumParams == 3 && FTy.getReturnType()->isPointerTy());
1267 
1268   // void operator delete[](void*);
1269   case LibFunc_ZdaPv:
1270   // void operator delete(void*);
1271   case LibFunc_ZdlPv:
1272   // void operator delete[](void*);
1273   case LibFunc_msvc_delete_array_ptr32:
1274   // void operator delete[](void*);
1275   case LibFunc_msvc_delete_array_ptr64:
1276   // void operator delete(void*);
1277   case LibFunc_msvc_delete_ptr32:
1278   // void operator delete(void*);
1279   case LibFunc_msvc_delete_ptr64:
1280     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1281 
1282   // void operator delete[](void*, nothrow);
1283   case LibFunc_ZdaPvRKSt9nothrow_t:
1284   // void operator delete[](void*, unsigned int);
1285   case LibFunc_ZdaPvj:
1286   // void operator delete[](void*, unsigned long);
1287   case LibFunc_ZdaPvm:
1288   // void operator delete(void*, nothrow);
1289   case LibFunc_ZdlPvRKSt9nothrow_t:
1290   // void operator delete(void*, unsigned int);
1291   case LibFunc_ZdlPvj:
1292   // void operator delete(void*, unsigned long);
1293   case LibFunc_ZdlPvm:
1294   // void operator delete(void*, align_val_t)
1295   case LibFunc_ZdlPvSt11align_val_t:
1296   // void operator delete[](void*, align_val_t)
1297   case LibFunc_ZdaPvSt11align_val_t:
1298   // void operator delete[](void*, unsigned int);
1299   case LibFunc_msvc_delete_array_ptr32_int:
1300   // void operator delete[](void*, nothrow);
1301   case LibFunc_msvc_delete_array_ptr32_nothrow:
1302   // void operator delete[](void*, unsigned long long);
1303   case LibFunc_msvc_delete_array_ptr64_longlong:
1304   // void operator delete[](void*, nothrow);
1305   case LibFunc_msvc_delete_array_ptr64_nothrow:
1306   // void operator delete(void*, unsigned int);
1307   case LibFunc_msvc_delete_ptr32_int:
1308   // void operator delete(void*, nothrow);
1309   case LibFunc_msvc_delete_ptr32_nothrow:
1310   // void operator delete(void*, unsigned long long);
1311   case LibFunc_msvc_delete_ptr64_longlong:
1312   // void operator delete(void*, nothrow);
1313   case LibFunc_msvc_delete_ptr64_nothrow:
1314     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
1315 
1316   // void operator delete(void*, align_val_t, nothrow)
1317   case LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t:
1318   // void operator delete[](void*, align_val_t, nothrow)
1319   case LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t:
1320   // void operator delete(void*, unsigned int, align_val_t)
1321   case LibFunc_ZdlPvjSt11align_val_t:
1322   // void operator delete(void*, unsigned long, align_val_t)
1323   case LibFunc_ZdlPvmSt11align_val_t:
1324   // void operator delete[](void*, unsigned int, align_val_t);
1325   case LibFunc_ZdaPvjSt11align_val_t:
1326   // void operator delete[](void*, unsigned long, align_val_t);
1327   case LibFunc_ZdaPvmSt11align_val_t:
1328     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
1329 
1330   // void __atomic_load(size_t, void *, void *, int)
1331   case LibFunc_atomic_load:
1332   // void __atomic_store(size_t, void *, void *, int)
1333   case LibFunc_atomic_store:
1334     return (NumParams == 4 && FTy.getParamType(0)->isIntegerTy() &&
1335             FTy.getParamType(1)->isPointerTy() &&
1336             FTy.getParamType(2)->isPointerTy() &&
1337             FTy.getParamType(3)->isIntegerTy());
1338 
1339   case LibFunc_memset_pattern16:
1340     return (!FTy.isVarArg() && NumParams == 3 &&
1341             FTy.getParamType(0)->isPointerTy() &&
1342             FTy.getParamType(1)->isPointerTy() &&
1343             FTy.getParamType(2)->isIntegerTy());
1344 
1345   case LibFunc_cxa_guard_abort:
1346   case LibFunc_cxa_guard_acquire:
1347   case LibFunc_cxa_guard_release:
1348   case LibFunc_nvvm_reflect:
1349     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1350 
1351   case LibFunc_sincospi_stret:
1352   case LibFunc_sincospif_stret:
1353     return (NumParams == 1 && FTy.getParamType(0)->isFloatingPointTy());
1354 
1355   case LibFunc_acos:
1356   case LibFunc_acos_finite:
1357   case LibFunc_acosf:
1358   case LibFunc_acosf_finite:
1359   case LibFunc_acosh:
1360   case LibFunc_acosh_finite:
1361   case LibFunc_acoshf:
1362   case LibFunc_acoshf_finite:
1363   case LibFunc_acoshl:
1364   case LibFunc_acoshl_finite:
1365   case LibFunc_acosl:
1366   case LibFunc_acosl_finite:
1367   case LibFunc_asin:
1368   case LibFunc_asin_finite:
1369   case LibFunc_asinf:
1370   case LibFunc_asinf_finite:
1371   case LibFunc_asinh:
1372   case LibFunc_asinhf:
1373   case LibFunc_asinhl:
1374   case LibFunc_asinl:
1375   case LibFunc_asinl_finite:
1376   case LibFunc_atan:
1377   case LibFunc_atanf:
1378   case LibFunc_atanh:
1379   case LibFunc_atanh_finite:
1380   case LibFunc_atanhf:
1381   case LibFunc_atanhf_finite:
1382   case LibFunc_atanhl:
1383   case LibFunc_atanhl_finite:
1384   case LibFunc_atanl:
1385   case LibFunc_cbrt:
1386   case LibFunc_cbrtf:
1387   case LibFunc_cbrtl:
1388   case LibFunc_ceil:
1389   case LibFunc_ceilf:
1390   case LibFunc_ceill:
1391   case LibFunc_cos:
1392   case LibFunc_cosf:
1393   case LibFunc_cosh:
1394   case LibFunc_cosh_finite:
1395   case LibFunc_coshf:
1396   case LibFunc_coshf_finite:
1397   case LibFunc_coshl:
1398   case LibFunc_coshl_finite:
1399   case LibFunc_cosl:
1400   case LibFunc_exp10:
1401   case LibFunc_exp10_finite:
1402   case LibFunc_exp10f:
1403   case LibFunc_exp10f_finite:
1404   case LibFunc_exp10l:
1405   case LibFunc_exp10l_finite:
1406   case LibFunc_exp2:
1407   case LibFunc_exp2_finite:
1408   case LibFunc_exp2f:
1409   case LibFunc_exp2f_finite:
1410   case LibFunc_exp2l:
1411   case LibFunc_exp2l_finite:
1412   case LibFunc_exp:
1413   case LibFunc_exp_finite:
1414   case LibFunc_expf:
1415   case LibFunc_expf_finite:
1416   case LibFunc_expl:
1417   case LibFunc_expl_finite:
1418   case LibFunc_expm1:
1419   case LibFunc_expm1f:
1420   case LibFunc_expm1l:
1421   case LibFunc_fabs:
1422   case LibFunc_fabsf:
1423   case LibFunc_fabsl:
1424   case LibFunc_floor:
1425   case LibFunc_floorf:
1426   case LibFunc_floorl:
1427   case LibFunc_log10:
1428   case LibFunc_log10_finite:
1429   case LibFunc_log10f:
1430   case LibFunc_log10f_finite:
1431   case LibFunc_log10l:
1432   case LibFunc_log10l_finite:
1433   case LibFunc_log1p:
1434   case LibFunc_log1pf:
1435   case LibFunc_log1pl:
1436   case LibFunc_log2:
1437   case LibFunc_log2_finite:
1438   case LibFunc_log2f:
1439   case LibFunc_log2f_finite:
1440   case LibFunc_log2l:
1441   case LibFunc_log2l_finite:
1442   case LibFunc_log:
1443   case LibFunc_log_finite:
1444   case LibFunc_logb:
1445   case LibFunc_logbf:
1446   case LibFunc_logbl:
1447   case LibFunc_logf:
1448   case LibFunc_logf_finite:
1449   case LibFunc_logl:
1450   case LibFunc_logl_finite:
1451   case LibFunc_nearbyint:
1452   case LibFunc_nearbyintf:
1453   case LibFunc_nearbyintl:
1454   case LibFunc_rint:
1455   case LibFunc_rintf:
1456   case LibFunc_rintl:
1457   case LibFunc_round:
1458   case LibFunc_roundf:
1459   case LibFunc_roundl:
1460   case LibFunc_roundeven:
1461   case LibFunc_roundevenf:
1462   case LibFunc_roundevenl:
1463   case LibFunc_sin:
1464   case LibFunc_sinf:
1465   case LibFunc_sinh:
1466   case LibFunc_sinh_finite:
1467   case LibFunc_sinhf:
1468   case LibFunc_sinhf_finite:
1469   case LibFunc_sinhl:
1470   case LibFunc_sinhl_finite:
1471   case LibFunc_sinl:
1472   case LibFunc_sqrt:
1473   case LibFunc_sqrt_finite:
1474   case LibFunc_sqrtf:
1475   case LibFunc_sqrtf_finite:
1476   case LibFunc_sqrtl:
1477   case LibFunc_sqrtl_finite:
1478   case LibFunc_tan:
1479   case LibFunc_tanf:
1480   case LibFunc_tanh:
1481   case LibFunc_tanhf:
1482   case LibFunc_tanhl:
1483   case LibFunc_tanl:
1484   case LibFunc_trunc:
1485   case LibFunc_truncf:
1486   case LibFunc_truncl:
1487     return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() &&
1488             FTy.getReturnType() == FTy.getParamType(0));
1489 
1490   case LibFunc_atan2:
1491   case LibFunc_atan2_finite:
1492   case LibFunc_atan2f:
1493   case LibFunc_atan2f_finite:
1494   case LibFunc_atan2l:
1495   case LibFunc_atan2l_finite:
1496   case LibFunc_fmin:
1497   case LibFunc_fminf:
1498   case LibFunc_fminl:
1499   case LibFunc_fmax:
1500   case LibFunc_fmaxf:
1501   case LibFunc_fmaxl:
1502   case LibFunc_fmod:
1503   case LibFunc_fmodf:
1504   case LibFunc_fmodl:
1505   case LibFunc_remainder:
1506   case LibFunc_remainderf:
1507   case LibFunc_remainderl:
1508   case LibFunc_copysign:
1509   case LibFunc_copysignf:
1510   case LibFunc_copysignl:
1511   case LibFunc_pow:
1512   case LibFunc_pow_finite:
1513   case LibFunc_powf:
1514   case LibFunc_powf_finite:
1515   case LibFunc_powl:
1516   case LibFunc_powl_finite:
1517     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
1518             FTy.getReturnType() == FTy.getParamType(0) &&
1519             FTy.getReturnType() == FTy.getParamType(1));
1520 
1521   case LibFunc_ldexp:
1522   case LibFunc_ldexpf:
1523   case LibFunc_ldexpl:
1524     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
1525             FTy.getReturnType() == FTy.getParamType(0) &&
1526             FTy.getParamType(1)->isIntegerTy(getIntSize()));
1527 
1528   case LibFunc_ffs:
1529   case LibFunc_ffsl:
1530   case LibFunc_ffsll:
1531   case LibFunc_fls:
1532   case LibFunc_flsl:
1533   case LibFunc_flsll:
1534     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1535             FTy.getParamType(0)->isIntegerTy());
1536 
1537   case LibFunc_isdigit:
1538   case LibFunc_isascii:
1539   case LibFunc_toascii:
1540   case LibFunc_putchar:
1541   case LibFunc_putchar_unlocked:
1542     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1543             FTy.getReturnType() == FTy.getParamType(0));
1544 
1545   case LibFunc_abs:
1546   case LibFunc_labs:
1547   case LibFunc_llabs:
1548     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() &&
1549             FTy.getReturnType() == FTy.getParamType(0));
1550 
1551   case LibFunc_cxa_atexit:
1552     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() &&
1553             FTy.getParamType(0)->isPointerTy() &&
1554             FTy.getParamType(1)->isPointerTy() &&
1555             FTy.getParamType(2)->isPointerTy());
1556 
1557   case LibFunc_sinpi:
1558   case LibFunc_cospi:
1559     return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() &&
1560             FTy.getReturnType() == FTy.getParamType(0));
1561 
1562   case LibFunc_sinpif:
1563   case LibFunc_cospif:
1564     return (NumParams == 1 && FTy.getReturnType()->isFloatTy() &&
1565             FTy.getReturnType() == FTy.getParamType(0));
1566 
1567   case LibFunc_strnlen:
1568     return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(1) &&
1569             FTy.getParamType(0)->isPointerTy() &&
1570             IsSizeTTy(FTy.getParamType(1)));
1571 
1572   case LibFunc_posix_memalign:
1573     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
1574             FTy.getParamType(0)->isPointerTy() &&
1575             IsSizeTTy(FTy.getParamType(1)) && IsSizeTTy(FTy.getParamType(2)));
1576 
1577   case LibFunc_wcslen:
1578     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
1579             FTy.getReturnType()->isIntegerTy());
1580 
1581   case LibFunc_cabs:
1582   case LibFunc_cabsf:
1583   case LibFunc_cabsl: {
1584     Type* RetTy = FTy.getReturnType();
1585     if (!RetTy->isFloatingPointTy())
1586       return false;
1587 
1588     // NOTE: These prototypes are target specific and currently support
1589     // "complex" passed as an array or discrete real & imaginary parameters.
1590     // Add other calling conventions to enable libcall optimizations.
1591     if (NumParams == 1)
1592       return (FTy.getParamType(0)->isArrayTy() &&
1593               FTy.getParamType(0)->getArrayNumElements() == 2 &&
1594               FTy.getParamType(0)->getArrayElementType() == RetTy);
1595     else if (NumParams == 2)
1596       return (FTy.getParamType(0) == RetTy && FTy.getParamType(1) == RetTy);
1597     else
1598       return false;
1599   }
1600   case LibFunc::NumLibFuncs:
1601   case LibFunc::NotLibFunc:
1602     break;
1603   }
1604 
1605   llvm_unreachable("Invalid libfunc");
1606 }
1607 
1608 bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
1609                                        LibFunc &F) const {
1610   // Intrinsics don't overlap w/libcalls; if our module has a large number of
1611   // intrinsics, this ends up being an interesting compile time win since we
1612   // avoid string normalization and comparison.
1613   if (FDecl.isIntrinsic()) return false;
1614 
1615   const DataLayout *DL =
1616       FDecl.getParent() ? &FDecl.getParent()->getDataLayout() : nullptr;
1617   return getLibFunc(FDecl.getName(), F) &&
1618          isValidProtoForLibFunc(*FDecl.getFunctionType(), F, DL);
1619 }
1620 
1621 void TargetLibraryInfoImpl::disableAllFunctions() {
1622   memset(AvailableArray, 0, sizeof(AvailableArray));
1623 }
1624 
1625 static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
1626   return LHS.ScalarFnName < RHS.ScalarFnName;
1627 }
1628 
1629 static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
1630   return LHS.VectorFnName < RHS.VectorFnName;
1631 }
1632 
1633 static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
1634   return LHS.ScalarFnName < S;
1635 }
1636 
1637 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
1638   llvm::append_range(VectorDescs, Fns);
1639   llvm::sort(VectorDescs, compareByScalarFnName);
1640 
1641   llvm::append_range(ScalarDescs, Fns);
1642   llvm::sort(ScalarDescs, compareByVectorFnName);
1643 }
1644 
1645 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
1646     enum VectorLibrary VecLib) {
1647   switch (VecLib) {
1648   case Accelerate: {
1649     const VecDesc VecFuncs[] = {
1650     #define TLI_DEFINE_ACCELERATE_VECFUNCS
1651     #include "llvm/Analysis/VecFuncs.def"
1652     };
1653     addVectorizableFunctions(VecFuncs);
1654     break;
1655   }
1656   case DarwinLibSystemM: {
1657     const VecDesc VecFuncs[] = {
1658     #define TLI_DEFINE_DARWIN_LIBSYSTEM_M_VECFUNCS
1659     #include "llvm/Analysis/VecFuncs.def"
1660     };
1661     addVectorizableFunctions(VecFuncs);
1662     break;
1663   }
1664   case LIBMVEC_X86: {
1665     const VecDesc VecFuncs[] = {
1666     #define TLI_DEFINE_LIBMVEC_X86_VECFUNCS
1667     #include "llvm/Analysis/VecFuncs.def"
1668     };
1669     addVectorizableFunctions(VecFuncs);
1670     break;
1671   }
1672   case MASSV: {
1673     const VecDesc VecFuncs[] = {
1674     #define TLI_DEFINE_MASSV_VECFUNCS
1675     #include "llvm/Analysis/VecFuncs.def"
1676     };
1677     addVectorizableFunctions(VecFuncs);
1678     break;
1679   }
1680   case SVML: {
1681     const VecDesc VecFuncs[] = {
1682     #define TLI_DEFINE_SVML_VECFUNCS
1683     #include "llvm/Analysis/VecFuncs.def"
1684     };
1685     addVectorizableFunctions(VecFuncs);
1686     break;
1687   }
1688   case NoLibrary:
1689     break;
1690   }
1691 }
1692 
1693 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
1694   funcName = sanitizeFunctionName(funcName);
1695   if (funcName.empty())
1696     return false;
1697 
1698   std::vector<VecDesc>::const_iterator I =
1699       llvm::lower_bound(VectorDescs, funcName, compareWithScalarFnName);
1700   return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
1701 }
1702 
1703 StringRef
1704 TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
1705                                              const ElementCount &VF) const {
1706   F = sanitizeFunctionName(F);
1707   if (F.empty())
1708     return F;
1709   std::vector<VecDesc>::const_iterator I =
1710       llvm::lower_bound(VectorDescs, F, compareWithScalarFnName);
1711   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
1712     if (I->VectorizationFactor == VF)
1713       return I->VectorFnName;
1714     ++I;
1715   }
1716   return StringRef();
1717 }
1718 
1719 TargetLibraryInfo TargetLibraryAnalysis::run(const Function &F,
1720                                              FunctionAnalysisManager &) {
1721   if (!BaselineInfoImpl)
1722     BaselineInfoImpl =
1723         TargetLibraryInfoImpl(Triple(F.getParent()->getTargetTriple()));
1724   return TargetLibraryInfo(*BaselineInfoImpl, &F);
1725 }
1726 
1727 unsigned TargetLibraryInfoImpl::getWCharSize(const Module &M) const {
1728   if (auto *ShortWChar = cast_or_null<ConstantAsMetadata>(
1729       M.getModuleFlag("wchar_size")))
1730     return cast<ConstantInt>(ShortWChar->getValue())->getZExtValue();
1731   return 0;
1732 }
1733 
1734 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
1735     : ImmutablePass(ID), TLA(TargetLibraryInfoImpl()) {
1736   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1737 }
1738 
1739 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
1740     : ImmutablePass(ID), TLA(TargetLibraryInfoImpl(T)) {
1741   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1742 }
1743 
1744 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
1745     const TargetLibraryInfoImpl &TLIImpl)
1746     : ImmutablePass(ID), TLA(TLIImpl) {
1747   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1748 }
1749 
1750 AnalysisKey TargetLibraryAnalysis::Key;
1751 
1752 // Register the basic pass.
1753 INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
1754                 "Target Library Information", false, true)
1755 char TargetLibraryInfoWrapperPass::ID = 0;
1756 
1757 void TargetLibraryInfoWrapperPass::anchor() {}
1758 
1759 void TargetLibraryInfoImpl::getWidestVF(StringRef ScalarF,
1760                                         ElementCount &FixedVF,
1761                                         ElementCount &ScalableVF) const {
1762   ScalarF = sanitizeFunctionName(ScalarF);
1763   // Use '0' here because a type of the form <vscale x 1 x ElTy> is not the
1764   // same as a scalar.
1765   ScalableVF = ElementCount::getScalable(0);
1766   FixedVF = ElementCount::getFixed(1);
1767   if (ScalarF.empty())
1768     return;
1769 
1770   std::vector<VecDesc>::const_iterator I =
1771       llvm::lower_bound(VectorDescs, ScalarF, compareWithScalarFnName);
1772   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == ScalarF) {
1773     ElementCount *VF =
1774         I->VectorizationFactor.isScalable() ? &ScalableVF : &FixedVF;
1775     if (ElementCount::isKnownGT(I->VectorizationFactor, *VF))
1776       *VF = I->VectorizationFactor;
1777     ++I;
1778   }
1779 }
1780