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