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