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