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