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