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 DataLayout &DL) const {
788   LLVMContext &Ctx = FTy.getContext();
789   // FIXME: There is really no guarantee that sizeof(size_t) is equal to
790   // sizeof(int*) for every target. So the assumption used here to derive the
791   // SizeTTy based on DataLayout and getIntPtrType isn't always valid.
792   Type *SizeTTy = DL.getIntPtrType(Ctx, /*AddressSpace=*/0);
793   auto IsSizeTTy = [SizeTTy](Type *Ty) {
794     return Ty == SizeTTy;
795   };
796   unsigned NumParams = FTy.getNumParams();
797 
798   switch (F) {
799   case LibFunc_execl:
800   case LibFunc_execlp:
801   case LibFunc_execle:
802     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
803             FTy.getParamType(1)->isPointerTy() &&
804             FTy.getReturnType()->isIntegerTy(32));
805   case LibFunc_execv:
806   case LibFunc_execvp:
807     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
808             FTy.getParamType(1)->isPointerTy() &&
809             FTy.getReturnType()->isIntegerTy(32));
810   case LibFunc_execvP:
811   case LibFunc_execvpe:
812   case LibFunc_execve:
813     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
814             FTy.getParamType(1)->isPointerTy() &&
815             FTy.getParamType(2)->isPointerTy() &&
816             FTy.getReturnType()->isIntegerTy(32));
817   case LibFunc_strlen_chk:
818     --NumParams;
819     if (!IsSizeTTy(FTy.getParamType(NumParams)))
820       return false;
821     LLVM_FALLTHROUGH;
822   case LibFunc_strlen:
823     return NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
824            IsSizeTTy(FTy.getReturnType());
825 
826   case LibFunc_strchr:
827   case LibFunc_strrchr:
828     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
829             FTy.getParamType(0) == FTy.getReturnType() &&
830             FTy.getParamType(1)->isIntegerTy());
831 
832   case LibFunc_strtol:
833   case LibFunc_strtod:
834   case LibFunc_strtof:
835   case LibFunc_strtoul:
836   case LibFunc_strtoll:
837   case LibFunc_strtold:
838   case LibFunc_strtoull:
839     return ((NumParams == 2 || NumParams == 3) &&
840             FTy.getParamType(0)->isPointerTy() &&
841             FTy.getParamType(1)->isPointerTy());
842   case LibFunc_strcat_chk:
843     --NumParams;
844     if (!IsSizeTTy(FTy.getParamType(NumParams)))
845       return false;
846     LLVM_FALLTHROUGH;
847   case LibFunc_strcat:
848     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
849             FTy.getParamType(0) == FTy.getReturnType() &&
850             FTy.getParamType(1) == FTy.getReturnType());
851 
852   case LibFunc_strncat_chk:
853     --NumParams;
854     if (!IsSizeTTy(FTy.getParamType(NumParams)))
855       return false;
856     LLVM_FALLTHROUGH;
857   case LibFunc_strncat:
858     return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
859             FTy.getParamType(0) == FTy.getReturnType() &&
860             FTy.getParamType(1) == FTy.getReturnType() &&
861             IsSizeTTy(FTy.getParamType(2)));
862 
863   case LibFunc_strcpy_chk:
864   case LibFunc_stpcpy_chk:
865     --NumParams;
866     if (!IsSizeTTy(FTy.getParamType(NumParams)))
867       return false;
868     LLVM_FALLTHROUGH;
869   case LibFunc_strcpy:
870   case LibFunc_stpcpy:
871     return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(0) &&
872             FTy.getParamType(0) == FTy.getParamType(1) &&
873             FTy.getParamType(0)->isPointerTy());
874 
875   case LibFunc_strlcat_chk:
876   case LibFunc_strlcpy_chk:
877     --NumParams;
878     if (!IsSizeTTy(FTy.getParamType(NumParams)))
879       return false;
880     LLVM_FALLTHROUGH;
881   case LibFunc_strlcat:
882   case LibFunc_strlcpy:
883     return NumParams == 3 && IsSizeTTy(FTy.getReturnType()) &&
884            FTy.getParamType(0)->isPointerTy() &&
885            FTy.getParamType(1)->isPointerTy() &&
886            IsSizeTTy(FTy.getParamType(2));
887 
888   case LibFunc_strncpy_chk:
889   case LibFunc_stpncpy_chk:
890     --NumParams;
891     if (!IsSizeTTy(FTy.getParamType(NumParams)))
892       return false;
893     LLVM_FALLTHROUGH;
894   case LibFunc_strncpy:
895   case LibFunc_stpncpy:
896     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
897             FTy.getParamType(0) == FTy.getParamType(1) &&
898             FTy.getParamType(0)->isPointerTy() &&
899             IsSizeTTy(FTy.getParamType(2)));
900 
901   case LibFunc_strxfrm:
902     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
903             FTy.getParamType(1)->isPointerTy());
904 
905   case LibFunc_strcmp:
906     return (NumParams == 2 && FTy.getReturnType()->isIntegerTy(32) &&
907             FTy.getParamType(0)->isPointerTy() &&
908             FTy.getParamType(0) == FTy.getParamType(1));
909 
910   case LibFunc_strncmp:
911     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
912             FTy.getParamType(0)->isPointerTy() &&
913             FTy.getParamType(0) == FTy.getParamType(1) &&
914             IsSizeTTy(FTy.getParamType(2)));
915 
916   case LibFunc_strspn:
917   case LibFunc_strcspn:
918     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
919             FTy.getParamType(0) == FTy.getParamType(1) &&
920             FTy.getReturnType()->isIntegerTy());
921 
922   case LibFunc_strcoll:
923   case LibFunc_strcasecmp:
924   case LibFunc_strncasecmp:
925     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
926             FTy.getParamType(1)->isPointerTy());
927 
928   case LibFunc_strstr:
929     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
930             FTy.getParamType(0)->isPointerTy() &&
931             FTy.getParamType(1)->isPointerTy());
932 
933   case LibFunc_strpbrk:
934     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
935             FTy.getReturnType() == FTy.getParamType(0) &&
936             FTy.getParamType(0) == FTy.getParamType(1));
937 
938   case LibFunc_strtok:
939   case LibFunc_strtok_r:
940     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
941   case LibFunc_scanf:
942   case LibFunc_setbuf:
943   case LibFunc_setvbuf:
944     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
945   case LibFunc_strdup:
946   case LibFunc_strndup:
947     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
948             FTy.getParamType(0)->isPointerTy());
949   case LibFunc_sscanf:
950   case LibFunc_stat:
951   case LibFunc_statvfs:
952   case LibFunc_siprintf:
953   case LibFunc_small_sprintf:
954   case LibFunc_sprintf:
955     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
956             FTy.getParamType(1)->isPointerTy() &&
957             FTy.getReturnType()->isIntegerTy(32));
958 
959   case LibFunc_sprintf_chk:
960     return NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
961            FTy.getParamType(1)->isIntegerTy(32) &&
962            IsSizeTTy(FTy.getParamType(2)) &&
963            FTy.getParamType(3)->isPointerTy() &&
964            FTy.getReturnType()->isIntegerTy(32);
965 
966   case LibFunc_snprintf:
967     return NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
968            IsSizeTTy(FTy.getParamType(1)) &&
969            FTy.getParamType(2)->isPointerTy() &&
970            FTy.getReturnType()->isIntegerTy(32);
971 
972   case LibFunc_snprintf_chk:
973     return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
974            IsSizeTTy(FTy.getParamType(1)) &&
975            FTy.getParamType(2)->isIntegerTy(32) &&
976            IsSizeTTy(FTy.getParamType(3)) &&
977            FTy.getParamType(4)->isPointerTy() &&
978            FTy.getReturnType()->isIntegerTy(32);
979 
980   case LibFunc_setitimer:
981     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
982             FTy.getParamType(2)->isPointerTy());
983   case LibFunc_system:
984     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
985   case LibFunc___kmpc_alloc_shared:
986   case LibFunc_malloc:
987   case LibFunc_vec_malloc:
988     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
989   case LibFunc_memcmp:
990     return NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
991            FTy.getParamType(0)->isPointerTy() &&
992            FTy.getParamType(1)->isPointerTy() && IsSizeTTy(FTy.getParamType(2));
993 
994   case LibFunc_memchr:
995   case LibFunc_memrchr:
996     return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
997             FTy.getReturnType() == FTy.getParamType(0) &&
998             FTy.getParamType(1)->isIntegerTy(32) &&
999             IsSizeTTy(FTy.getParamType(2)));
1000   case LibFunc_modf:
1001   case LibFunc_modff:
1002   case LibFunc_modfl:
1003     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
1004 
1005   case LibFunc_memcpy_chk:
1006   case LibFunc_mempcpy_chk:
1007   case LibFunc_memmove_chk:
1008     --NumParams;
1009     if (!IsSizeTTy(FTy.getParamType(NumParams)))
1010       return false;
1011     LLVM_FALLTHROUGH;
1012   case LibFunc_memcpy:
1013   case LibFunc_mempcpy:
1014   case LibFunc_memmove:
1015     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
1016             FTy.getParamType(0)->isPointerTy() &&
1017             FTy.getParamType(1)->isPointerTy() &&
1018             IsSizeTTy(FTy.getParamType(2)));
1019 
1020   case LibFunc_memset_chk:
1021     --NumParams;
1022     if (!IsSizeTTy(FTy.getParamType(NumParams)))
1023       return false;
1024     LLVM_FALLTHROUGH;
1025   case LibFunc_memset:
1026     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
1027             FTy.getParamType(0)->isPointerTy() &&
1028             FTy.getParamType(1)->isIntegerTy() &&
1029             IsSizeTTy(FTy.getParamType(2)));
1030 
1031   case LibFunc_memccpy_chk:
1032       --NumParams;
1033     if (!IsSizeTTy(FTy.getParamType(NumParams)))
1034       return false;
1035     LLVM_FALLTHROUGH;
1036   case LibFunc_memccpy:
1037     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
1038   case LibFunc_memalign:
1039     return (FTy.getReturnType()->isPointerTy());
1040   case LibFunc_realloc:
1041   case LibFunc_reallocf:
1042   case LibFunc_vec_realloc:
1043     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1044             FTy.getParamType(0) == FTy.getReturnType() &&
1045             IsSizeTTy(FTy.getParamType(1)));
1046   case LibFunc_read:
1047     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
1048   case LibFunc_rewind:
1049   case LibFunc_rmdir:
1050   case LibFunc_remove:
1051   case LibFunc_realpath:
1052     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
1053   case LibFunc_rename:
1054     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
1055             FTy.getParamType(1)->isPointerTy());
1056   case LibFunc_readlink:
1057     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
1058             FTy.getParamType(1)->isPointerTy());
1059   case LibFunc_write:
1060     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
1061   case LibFunc_aligned_alloc:
1062     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
1063   case LibFunc_bcopy:
1064   case LibFunc_bcmp:
1065     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
1066             FTy.getParamType(1)->isPointerTy());
1067   case LibFunc_bzero:
1068     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
1069   case LibFunc_calloc:
1070   case LibFunc_vec_calloc:
1071     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1072             FTy.getParamType(0) == FTy.getParamType(1));
1073 
1074   case LibFunc_atof:
1075   case LibFunc_atoi:
1076   case LibFunc_atol:
1077   case LibFunc_atoll:
1078   case LibFunc_ferror:
1079   case LibFunc_getenv:
1080   case LibFunc_getpwnam:
1081   case LibFunc_iprintf:
1082   case LibFunc_small_printf:
1083   case LibFunc_pclose:
1084   case LibFunc_perror:
1085   case LibFunc_printf:
1086   case LibFunc_puts:
1087   case LibFunc_uname:
1088   case LibFunc_under_IO_getc:
1089   case LibFunc_unlink:
1090   case LibFunc_unsetenv:
1091     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1092 
1093   case LibFunc_access:
1094   case LibFunc_chmod:
1095   case LibFunc_chown:
1096   case LibFunc_clearerr:
1097   case LibFunc_closedir:
1098   case LibFunc_ctermid:
1099   case LibFunc_fclose:
1100   case LibFunc_feof:
1101   case LibFunc_fflush:
1102   case LibFunc_fgetc:
1103   case LibFunc_fgetc_unlocked:
1104   case LibFunc_fileno:
1105   case LibFunc_flockfile:
1106   case LibFunc_free:
1107   case LibFunc_fseek:
1108   case LibFunc_fseeko64:
1109   case LibFunc_fseeko:
1110   case LibFunc_fsetpos:
1111   case LibFunc_ftell:
1112   case LibFunc_ftello64:
1113   case LibFunc_ftello:
1114   case LibFunc_ftrylockfile:
1115   case LibFunc_funlockfile:
1116   case LibFunc_getc:
1117   case LibFunc_getc_unlocked:
1118   case LibFunc_getlogin_r:
1119   case LibFunc_mkdir:
1120   case LibFunc_mktime:
1121   case LibFunc_times:
1122   case LibFunc_vec_free:
1123     return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
1124   case LibFunc___kmpc_free_shared:
1125     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1126             IsSizeTTy(FTy.getParamType(1)));
1127 
1128   case LibFunc_fopen:
1129     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1130             FTy.getParamType(0)->isPointerTy() &&
1131             FTy.getParamType(1)->isPointerTy());
1132   case LibFunc_fork:
1133     return (NumParams == 0 && FTy.getReturnType()->isIntegerTy(32));
1134   case LibFunc_fdopen:
1135     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1136             FTy.getParamType(1)->isPointerTy());
1137   case LibFunc_fputc:
1138   case LibFunc_fputc_unlocked:
1139   case LibFunc_fstat:
1140   case LibFunc_frexp:
1141   case LibFunc_frexpf:
1142   case LibFunc_frexpl:
1143   case LibFunc_fstatvfs:
1144     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1145   case LibFunc_fgets:
1146   case LibFunc_fgets_unlocked:
1147     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
1148             FTy.getParamType(2)->isPointerTy());
1149   case LibFunc_fread:
1150   case LibFunc_fread_unlocked:
1151     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
1152             FTy.getParamType(3)->isPointerTy());
1153   case LibFunc_fwrite:
1154   case LibFunc_fwrite_unlocked:
1155     return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() &&
1156             FTy.getParamType(0)->isPointerTy() &&
1157             FTy.getParamType(1)->isIntegerTy() &&
1158             FTy.getParamType(2)->isIntegerTy() &&
1159             FTy.getParamType(3)->isPointerTy());
1160   case LibFunc_fputs:
1161   case LibFunc_fputs_unlocked:
1162     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
1163             FTy.getParamType(1)->isPointerTy());
1164   case LibFunc_fscanf:
1165   case LibFunc_fiprintf:
1166   case LibFunc_small_fprintf:
1167   case LibFunc_fprintf:
1168     return (NumParams >= 2 && FTy.getReturnType()->isIntegerTy() &&
1169             FTy.getParamType(0)->isPointerTy() &&
1170             FTy.getParamType(1)->isPointerTy());
1171   case LibFunc_fgetpos:
1172     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
1173             FTy.getParamType(1)->isPointerTy());
1174   case LibFunc_getchar:
1175   case LibFunc_getchar_unlocked:
1176     return (NumParams == 0 && FTy.getReturnType()->isIntegerTy());
1177   case LibFunc_gets:
1178     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1179   case LibFunc_getitimer:
1180     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1181   case LibFunc_ungetc:
1182     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1183   case LibFunc_utime:
1184   case LibFunc_utimes:
1185     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1186             FTy.getParamType(1)->isPointerTy());
1187   case LibFunc_putc:
1188   case LibFunc_putc_unlocked:
1189     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1190   case LibFunc_pread:
1191   case LibFunc_pwrite:
1192     return (NumParams == 4 && FTy.getParamType(1)->isPointerTy());
1193   case LibFunc_popen:
1194     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1195             FTy.getParamType(0)->isPointerTy() &&
1196             FTy.getParamType(1)->isPointerTy());
1197   case LibFunc_vscanf:
1198     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1199   case LibFunc_vsscanf:
1200     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
1201             FTy.getParamType(2)->isPointerTy());
1202   case LibFunc_vfscanf:
1203     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
1204             FTy.getParamType(2)->isPointerTy());
1205   case LibFunc_valloc:
1206     return (FTy.getReturnType()->isPointerTy());
1207   case LibFunc_vprintf:
1208     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
1209   case LibFunc_vfprintf:
1210   case LibFunc_vsprintf:
1211     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
1212             FTy.getParamType(1)->isPointerTy());
1213   case LibFunc_vsprintf_chk:
1214     return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
1215            FTy.getParamType(1)->isIntegerTy(32) &&
1216            IsSizeTTy(FTy.getParamType(2)) && FTy.getParamType(3)->isPointerTy();
1217   case LibFunc_vsnprintf:
1218     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
1219             FTy.getParamType(2)->isPointerTy());
1220   case LibFunc_vsnprintf_chk:
1221     return NumParams == 6 && FTy.getParamType(0)->isPointerTy() &&
1222            FTy.getParamType(2)->isIntegerTy(32) &&
1223            IsSizeTTy(FTy.getParamType(3)) && FTy.getParamType(4)->isPointerTy();
1224   case LibFunc_open:
1225     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
1226   case LibFunc_opendir:
1227     return (NumParams == 1 && FTy.getReturnType()->isPointerTy() &&
1228             FTy.getParamType(0)->isPointerTy());
1229   case LibFunc_tmpfile:
1230     return (FTy.getReturnType()->isPointerTy());
1231   case LibFunc_htonl:
1232   case LibFunc_ntohl:
1233     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1234             FTy.getReturnType() == FTy.getParamType(0));
1235   case LibFunc_htons:
1236   case LibFunc_ntohs:
1237     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(16) &&
1238             FTy.getReturnType() == FTy.getParamType(0));
1239   case LibFunc_lstat:
1240     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1241             FTy.getParamType(1)->isPointerTy());
1242   case LibFunc_lchown:
1243     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
1244   case LibFunc_qsort:
1245     return (NumParams == 4 && FTy.getParamType(3)->isPointerTy());
1246   case LibFunc_dunder_strdup:
1247   case LibFunc_dunder_strndup:
1248     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
1249             FTy.getParamType(0)->isPointerTy());
1250   case LibFunc_dunder_strtok_r:
1251     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
1252   case LibFunc_under_IO_putc:
1253     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1254   case LibFunc_dunder_isoc99_scanf:
1255     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
1256   case LibFunc_stat64:
1257   case LibFunc_lstat64:
1258   case LibFunc_statvfs64:
1259     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1260             FTy.getParamType(1)->isPointerTy());
1261   case LibFunc_dunder_isoc99_sscanf:
1262     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
1263             FTy.getParamType(1)->isPointerTy());
1264   case LibFunc_fopen64:
1265     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1266             FTy.getParamType(0)->isPointerTy() &&
1267             FTy.getParamType(1)->isPointerTy());
1268   case LibFunc_tmpfile64:
1269     return (FTy.getReturnType()->isPointerTy());
1270   case LibFunc_fstat64:
1271   case LibFunc_fstatvfs64:
1272     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1273   case LibFunc_open64:
1274     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
1275   case LibFunc_gettimeofday:
1276     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1277             FTy.getParamType(1)->isPointerTy());
1278 
1279   // new(unsigned int);
1280   case LibFunc_Znwj:
1281   // new(unsigned long);
1282   case LibFunc_Znwm:
1283   // new[](unsigned int);
1284   case LibFunc_Znaj:
1285   // new[](unsigned long);
1286   case LibFunc_Znam:
1287   // new(unsigned int);
1288   case LibFunc_msvc_new_int:
1289   // new(unsigned long long);
1290   case LibFunc_msvc_new_longlong:
1291   // new[](unsigned int);
1292   case LibFunc_msvc_new_array_int:
1293   // new[](unsigned long long);
1294   case LibFunc_msvc_new_array_longlong:
1295     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
1296 
1297   // new(unsigned int, nothrow);
1298   case LibFunc_ZnwjRKSt9nothrow_t:
1299   // new(unsigned long, nothrow);
1300   case LibFunc_ZnwmRKSt9nothrow_t:
1301   // new[](unsigned int, nothrow);
1302   case LibFunc_ZnajRKSt9nothrow_t:
1303   // new[](unsigned long, nothrow);
1304   case LibFunc_ZnamRKSt9nothrow_t:
1305   // new(unsigned int, nothrow);
1306   case LibFunc_msvc_new_int_nothrow:
1307   // new(unsigned long long, nothrow);
1308   case LibFunc_msvc_new_longlong_nothrow:
1309   // new[](unsigned int, nothrow);
1310   case LibFunc_msvc_new_array_int_nothrow:
1311   // new[](unsigned long long, nothrow);
1312   case LibFunc_msvc_new_array_longlong_nothrow:
1313   // new(unsigned int, align_val_t)
1314   case LibFunc_ZnwjSt11align_val_t:
1315   // new(unsigned long, align_val_t)
1316   case LibFunc_ZnwmSt11align_val_t:
1317   // new[](unsigned int, align_val_t)
1318   case LibFunc_ZnajSt11align_val_t:
1319   // new[](unsigned long, align_val_t)
1320   case LibFunc_ZnamSt11align_val_t:
1321     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
1322 
1323   // new(unsigned int, align_val_t, nothrow)
1324   case LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t:
1325   // new(unsigned long, align_val_t, nothrow)
1326   case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t:
1327   // new[](unsigned int, align_val_t, nothrow)
1328   case LibFunc_ZnajSt11align_val_tRKSt9nothrow_t:
1329   // new[](unsigned long, align_val_t, nothrow)
1330   case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t:
1331     return (NumParams == 3 && FTy.getReturnType()->isPointerTy());
1332 
1333   // void operator delete[](void*);
1334   case LibFunc_ZdaPv:
1335   // void operator delete(void*);
1336   case LibFunc_ZdlPv:
1337   // void operator delete[](void*);
1338   case LibFunc_msvc_delete_array_ptr32:
1339   // void operator delete[](void*);
1340   case LibFunc_msvc_delete_array_ptr64:
1341   // void operator delete(void*);
1342   case LibFunc_msvc_delete_ptr32:
1343   // void operator delete(void*);
1344   case LibFunc_msvc_delete_ptr64:
1345     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1346 
1347   // void operator delete[](void*, nothrow);
1348   case LibFunc_ZdaPvRKSt9nothrow_t:
1349   // void operator delete[](void*, unsigned int);
1350   case LibFunc_ZdaPvj:
1351   // void operator delete[](void*, unsigned long);
1352   case LibFunc_ZdaPvm:
1353   // void operator delete(void*, nothrow);
1354   case LibFunc_ZdlPvRKSt9nothrow_t:
1355   // void operator delete(void*, unsigned int);
1356   case LibFunc_ZdlPvj:
1357   // void operator delete(void*, unsigned long);
1358   case LibFunc_ZdlPvm:
1359   // void operator delete(void*, align_val_t)
1360   case LibFunc_ZdlPvSt11align_val_t:
1361   // void operator delete[](void*, align_val_t)
1362   case LibFunc_ZdaPvSt11align_val_t:
1363   // void operator delete[](void*, unsigned int);
1364   case LibFunc_msvc_delete_array_ptr32_int:
1365   // void operator delete[](void*, nothrow);
1366   case LibFunc_msvc_delete_array_ptr32_nothrow:
1367   // void operator delete[](void*, unsigned long long);
1368   case LibFunc_msvc_delete_array_ptr64_longlong:
1369   // void operator delete[](void*, nothrow);
1370   case LibFunc_msvc_delete_array_ptr64_nothrow:
1371   // void operator delete(void*, unsigned int);
1372   case LibFunc_msvc_delete_ptr32_int:
1373   // void operator delete(void*, nothrow);
1374   case LibFunc_msvc_delete_ptr32_nothrow:
1375   // void operator delete(void*, unsigned long long);
1376   case LibFunc_msvc_delete_ptr64_longlong:
1377   // void operator delete(void*, nothrow);
1378   case LibFunc_msvc_delete_ptr64_nothrow:
1379     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
1380 
1381   // void operator delete(void*, align_val_t, nothrow)
1382   case LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t:
1383   // void operator delete[](void*, align_val_t, nothrow)
1384   case LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t:
1385   // void operator delete(void*, unsigned int, align_val_t)
1386   case LibFunc_ZdlPvjSt11align_val_t:
1387   // void operator delete(void*, unsigned long, align_val_t)
1388   case LibFunc_ZdlPvmSt11align_val_t:
1389   // void operator delete[](void*, unsigned int, align_val_t);
1390   case LibFunc_ZdaPvjSt11align_val_t:
1391   // void operator delete[](void*, unsigned long, align_val_t);
1392   case LibFunc_ZdaPvmSt11align_val_t:
1393     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
1394 
1395   // void __atomic_load(size_t, void *, void *, int)
1396   case LibFunc_atomic_load:
1397   // void __atomic_store(size_t, void *, void *, int)
1398   case LibFunc_atomic_store:
1399     return (NumParams == 4 && FTy.getParamType(0)->isIntegerTy() &&
1400             FTy.getParamType(1)->isPointerTy() &&
1401             FTy.getParamType(2)->isPointerTy() &&
1402             FTy.getParamType(3)->isIntegerTy());
1403 
1404   case LibFunc_memset_pattern16:
1405     return (!FTy.isVarArg() && NumParams == 3 &&
1406             FTy.getParamType(0)->isPointerTy() &&
1407             FTy.getParamType(1)->isPointerTy() &&
1408             FTy.getParamType(2)->isIntegerTy());
1409 
1410   case LibFunc_cxa_guard_abort:
1411   case LibFunc_cxa_guard_acquire:
1412   case LibFunc_cxa_guard_release:
1413   case LibFunc_nvvm_reflect:
1414     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1415 
1416   case LibFunc_sincospi_stret:
1417   case LibFunc_sincospif_stret:
1418     return (NumParams == 1 && FTy.getParamType(0)->isFloatingPointTy());
1419 
1420   case LibFunc_acos:
1421   case LibFunc_acos_finite:
1422   case LibFunc_acosf:
1423   case LibFunc_acosf_finite:
1424   case LibFunc_acosh:
1425   case LibFunc_acosh_finite:
1426   case LibFunc_acoshf:
1427   case LibFunc_acoshf_finite:
1428   case LibFunc_acoshl:
1429   case LibFunc_acoshl_finite:
1430   case LibFunc_acosl:
1431   case LibFunc_acosl_finite:
1432   case LibFunc_asin:
1433   case LibFunc_asin_finite:
1434   case LibFunc_asinf:
1435   case LibFunc_asinf_finite:
1436   case LibFunc_asinh:
1437   case LibFunc_asinhf:
1438   case LibFunc_asinhl:
1439   case LibFunc_asinl:
1440   case LibFunc_asinl_finite:
1441   case LibFunc_atan:
1442   case LibFunc_atanf:
1443   case LibFunc_atanh:
1444   case LibFunc_atanh_finite:
1445   case LibFunc_atanhf:
1446   case LibFunc_atanhf_finite:
1447   case LibFunc_atanhl:
1448   case LibFunc_atanhl_finite:
1449   case LibFunc_atanl:
1450   case LibFunc_cbrt:
1451   case LibFunc_cbrtf:
1452   case LibFunc_cbrtl:
1453   case LibFunc_ceil:
1454   case LibFunc_ceilf:
1455   case LibFunc_ceill:
1456   case LibFunc_cos:
1457   case LibFunc_cosf:
1458   case LibFunc_cosh:
1459   case LibFunc_cosh_finite:
1460   case LibFunc_coshf:
1461   case LibFunc_coshf_finite:
1462   case LibFunc_coshl:
1463   case LibFunc_coshl_finite:
1464   case LibFunc_cosl:
1465   case LibFunc_exp10:
1466   case LibFunc_exp10_finite:
1467   case LibFunc_exp10f:
1468   case LibFunc_exp10f_finite:
1469   case LibFunc_exp10l:
1470   case LibFunc_exp10l_finite:
1471   case LibFunc_exp2:
1472   case LibFunc_exp2_finite:
1473   case LibFunc_exp2f:
1474   case LibFunc_exp2f_finite:
1475   case LibFunc_exp2l:
1476   case LibFunc_exp2l_finite:
1477   case LibFunc_exp:
1478   case LibFunc_exp_finite:
1479   case LibFunc_expf:
1480   case LibFunc_expf_finite:
1481   case LibFunc_expl:
1482   case LibFunc_expl_finite:
1483   case LibFunc_expm1:
1484   case LibFunc_expm1f:
1485   case LibFunc_expm1l:
1486   case LibFunc_fabs:
1487   case LibFunc_fabsf:
1488   case LibFunc_fabsl:
1489   case LibFunc_floor:
1490   case LibFunc_floorf:
1491   case LibFunc_floorl:
1492   case LibFunc_log10:
1493   case LibFunc_log10_finite:
1494   case LibFunc_log10f:
1495   case LibFunc_log10f_finite:
1496   case LibFunc_log10l:
1497   case LibFunc_log10l_finite:
1498   case LibFunc_log1p:
1499   case LibFunc_log1pf:
1500   case LibFunc_log1pl:
1501   case LibFunc_log2:
1502   case LibFunc_log2_finite:
1503   case LibFunc_log2f:
1504   case LibFunc_log2f_finite:
1505   case LibFunc_log2l:
1506   case LibFunc_log2l_finite:
1507   case LibFunc_log:
1508   case LibFunc_log_finite:
1509   case LibFunc_logb:
1510   case LibFunc_logbf:
1511   case LibFunc_logbl:
1512   case LibFunc_logf:
1513   case LibFunc_logf_finite:
1514   case LibFunc_logl:
1515   case LibFunc_logl_finite:
1516   case LibFunc_nearbyint:
1517   case LibFunc_nearbyintf:
1518   case LibFunc_nearbyintl:
1519   case LibFunc_rint:
1520   case LibFunc_rintf:
1521   case LibFunc_rintl:
1522   case LibFunc_round:
1523   case LibFunc_roundf:
1524   case LibFunc_roundl:
1525   case LibFunc_roundeven:
1526   case LibFunc_roundevenf:
1527   case LibFunc_roundevenl:
1528   case LibFunc_sin:
1529   case LibFunc_sinf:
1530   case LibFunc_sinh:
1531   case LibFunc_sinh_finite:
1532   case LibFunc_sinhf:
1533   case LibFunc_sinhf_finite:
1534   case LibFunc_sinhl:
1535   case LibFunc_sinhl_finite:
1536   case LibFunc_sinl:
1537   case LibFunc_sqrt:
1538   case LibFunc_sqrt_finite:
1539   case LibFunc_sqrtf:
1540   case LibFunc_sqrtf_finite:
1541   case LibFunc_sqrtl:
1542   case LibFunc_sqrtl_finite:
1543   case LibFunc_tan:
1544   case LibFunc_tanf:
1545   case LibFunc_tanh:
1546   case LibFunc_tanhf:
1547   case LibFunc_tanhl:
1548   case LibFunc_tanl:
1549   case LibFunc_trunc:
1550   case LibFunc_truncf:
1551   case LibFunc_truncl:
1552     return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() &&
1553             FTy.getReturnType() == FTy.getParamType(0));
1554 
1555   case LibFunc_atan2:
1556   case LibFunc_atan2_finite:
1557   case LibFunc_atan2f:
1558   case LibFunc_atan2f_finite:
1559   case LibFunc_atan2l:
1560   case LibFunc_atan2l_finite:
1561   case LibFunc_fmin:
1562   case LibFunc_fminf:
1563   case LibFunc_fminl:
1564   case LibFunc_fmax:
1565   case LibFunc_fmaxf:
1566   case LibFunc_fmaxl:
1567   case LibFunc_fmod:
1568   case LibFunc_fmodf:
1569   case LibFunc_fmodl:
1570   case LibFunc_remainder:
1571   case LibFunc_remainderf:
1572   case LibFunc_remainderl:
1573   case LibFunc_copysign:
1574   case LibFunc_copysignf:
1575   case LibFunc_copysignl:
1576   case LibFunc_pow:
1577   case LibFunc_pow_finite:
1578   case LibFunc_powf:
1579   case LibFunc_powf_finite:
1580   case LibFunc_powl:
1581   case LibFunc_powl_finite:
1582     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
1583             FTy.getReturnType() == FTy.getParamType(0) &&
1584             FTy.getReturnType() == FTy.getParamType(1));
1585 
1586   case LibFunc_ldexp:
1587   case LibFunc_ldexpf:
1588   case LibFunc_ldexpl:
1589     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
1590             FTy.getReturnType() == FTy.getParamType(0) &&
1591             FTy.getParamType(1)->isIntegerTy(getIntSize()));
1592 
1593   case LibFunc_ffs:
1594   case LibFunc_ffsl:
1595   case LibFunc_ffsll:
1596   case LibFunc_fls:
1597   case LibFunc_flsl:
1598   case LibFunc_flsll:
1599     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1600             FTy.getParamType(0)->isIntegerTy());
1601 
1602   case LibFunc_isdigit:
1603   case LibFunc_isascii:
1604   case LibFunc_toascii:
1605   case LibFunc_putchar:
1606   case LibFunc_putchar_unlocked:
1607     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1608             FTy.getReturnType() == FTy.getParamType(0));
1609 
1610   case LibFunc_abs:
1611   case LibFunc_labs:
1612   case LibFunc_llabs:
1613     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() &&
1614             FTy.getReturnType() == FTy.getParamType(0));
1615 
1616   case LibFunc_cxa_atexit:
1617     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() &&
1618             FTy.getParamType(0)->isPointerTy() &&
1619             FTy.getParamType(1)->isPointerTy() &&
1620             FTy.getParamType(2)->isPointerTy());
1621 
1622   case LibFunc_sinpi:
1623   case LibFunc_cospi:
1624     return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() &&
1625             FTy.getReturnType() == FTy.getParamType(0));
1626 
1627   case LibFunc_sinpif:
1628   case LibFunc_cospif:
1629     return (NumParams == 1 && FTy.getReturnType()->isFloatTy() &&
1630             FTy.getReturnType() == FTy.getParamType(0));
1631 
1632   case LibFunc_strnlen:
1633     return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(1) &&
1634             FTy.getParamType(0)->isPointerTy() &&
1635             IsSizeTTy(FTy.getParamType(1)));
1636 
1637   case LibFunc_posix_memalign:
1638     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
1639             FTy.getParamType(0)->isPointerTy() &&
1640             IsSizeTTy(FTy.getParamType(1)) && IsSizeTTy(FTy.getParamType(2)));
1641 
1642   case LibFunc_wcslen:
1643     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
1644             FTy.getReturnType()->isIntegerTy());
1645 
1646   case LibFunc_cabs:
1647   case LibFunc_cabsf:
1648   case LibFunc_cabsl: {
1649     Type* RetTy = FTy.getReturnType();
1650     if (!RetTy->isFloatingPointTy())
1651       return false;
1652 
1653     // NOTE: These prototypes are target specific and currently support
1654     // "complex" passed as an array or discrete real & imaginary parameters.
1655     // Add other calling conventions to enable libcall optimizations.
1656     if (NumParams == 1)
1657       return (FTy.getParamType(0)->isArrayTy() &&
1658               FTy.getParamType(0)->getArrayNumElements() == 2 &&
1659               FTy.getParamType(0)->getArrayElementType() == RetTy);
1660     else if (NumParams == 2)
1661       return (FTy.getParamType(0) == RetTy && FTy.getParamType(1) == RetTy);
1662     else
1663       return false;
1664   }
1665   case LibFunc::NumLibFuncs:
1666   case LibFunc::NotLibFunc:
1667     break;
1668   }
1669 
1670   llvm_unreachable("Invalid libfunc");
1671 }
1672 
1673 bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
1674                                        LibFunc &F) const {
1675   // Intrinsics don't overlap w/libcalls; if our module has a large number of
1676   // intrinsics, this ends up being an interesting compile time win since we
1677   // avoid string normalization and comparison.
1678   if (FDecl.isIntrinsic()) return false;
1679 
1680   const Module *M = FDecl.getParent();
1681   assert(M && "Expecting FDecl to be connected to a Module.");
1682 
1683   return getLibFunc(FDecl.getName(), F) &&
1684          isValidProtoForLibFunc(*FDecl.getFunctionType(), F, M->getDataLayout());
1685 }
1686 
1687 void TargetLibraryInfoImpl::disableAllFunctions() {
1688   memset(AvailableArray, 0, sizeof(AvailableArray));
1689 }
1690 
1691 static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
1692   return LHS.ScalarFnName < RHS.ScalarFnName;
1693 }
1694 
1695 static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
1696   return LHS.VectorFnName < RHS.VectorFnName;
1697 }
1698 
1699 static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
1700   return LHS.ScalarFnName < S;
1701 }
1702 
1703 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
1704   llvm::append_range(VectorDescs, Fns);
1705   llvm::sort(VectorDescs, compareByScalarFnName);
1706 
1707   llvm::append_range(ScalarDescs, Fns);
1708   llvm::sort(ScalarDescs, compareByVectorFnName);
1709 }
1710 
1711 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
1712     enum VectorLibrary VecLib) {
1713   switch (VecLib) {
1714   case Accelerate: {
1715     const VecDesc VecFuncs[] = {
1716     #define TLI_DEFINE_ACCELERATE_VECFUNCS
1717     #include "llvm/Analysis/VecFuncs.def"
1718     };
1719     addVectorizableFunctions(VecFuncs);
1720     break;
1721   }
1722   case DarwinLibSystemM: {
1723     const VecDesc VecFuncs[] = {
1724     #define TLI_DEFINE_DARWIN_LIBSYSTEM_M_VECFUNCS
1725     #include "llvm/Analysis/VecFuncs.def"
1726     };
1727     addVectorizableFunctions(VecFuncs);
1728     break;
1729   }
1730   case LIBMVEC_X86: {
1731     const VecDesc VecFuncs[] = {
1732     #define TLI_DEFINE_LIBMVEC_X86_VECFUNCS
1733     #include "llvm/Analysis/VecFuncs.def"
1734     };
1735     addVectorizableFunctions(VecFuncs);
1736     break;
1737   }
1738   case MASSV: {
1739     const VecDesc VecFuncs[] = {
1740     #define TLI_DEFINE_MASSV_VECFUNCS
1741     #include "llvm/Analysis/VecFuncs.def"
1742     };
1743     addVectorizableFunctions(VecFuncs);
1744     break;
1745   }
1746   case SVML: {
1747     const VecDesc VecFuncs[] = {
1748     #define TLI_DEFINE_SVML_VECFUNCS
1749     #include "llvm/Analysis/VecFuncs.def"
1750     };
1751     addVectorizableFunctions(VecFuncs);
1752     break;
1753   }
1754   case NoLibrary:
1755     break;
1756   }
1757 }
1758 
1759 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
1760   funcName = sanitizeFunctionName(funcName);
1761   if (funcName.empty())
1762     return false;
1763 
1764   std::vector<VecDesc>::const_iterator I =
1765       llvm::lower_bound(VectorDescs, funcName, compareWithScalarFnName);
1766   return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
1767 }
1768 
1769 StringRef
1770 TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
1771                                              const ElementCount &VF) const {
1772   F = sanitizeFunctionName(F);
1773   if (F.empty())
1774     return F;
1775   std::vector<VecDesc>::const_iterator I =
1776       llvm::lower_bound(VectorDescs, F, compareWithScalarFnName);
1777   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
1778     if (I->VectorizationFactor == VF)
1779       return I->VectorFnName;
1780     ++I;
1781   }
1782   return StringRef();
1783 }
1784 
1785 TargetLibraryInfo TargetLibraryAnalysis::run(const Function &F,
1786                                              FunctionAnalysisManager &) {
1787   if (!BaselineInfoImpl)
1788     BaselineInfoImpl =
1789         TargetLibraryInfoImpl(Triple(F.getParent()->getTargetTriple()));
1790   return TargetLibraryInfo(*BaselineInfoImpl, &F);
1791 }
1792 
1793 unsigned TargetLibraryInfoImpl::getWCharSize(const Module &M) const {
1794   if (auto *ShortWChar = cast_or_null<ConstantAsMetadata>(
1795       M.getModuleFlag("wchar_size")))
1796     return cast<ConstantInt>(ShortWChar->getValue())->getZExtValue();
1797   return 0;
1798 }
1799 
1800 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
1801     : ImmutablePass(ID), TLA(TargetLibraryInfoImpl()) {
1802   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1803 }
1804 
1805 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
1806     : ImmutablePass(ID), TLA(TargetLibraryInfoImpl(T)) {
1807   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1808 }
1809 
1810 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
1811     const TargetLibraryInfoImpl &TLIImpl)
1812     : ImmutablePass(ID), TLA(TLIImpl) {
1813   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1814 }
1815 
1816 AnalysisKey TargetLibraryAnalysis::Key;
1817 
1818 // Register the basic pass.
1819 INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
1820                 "Target Library Information", false, true)
1821 char TargetLibraryInfoWrapperPass::ID = 0;
1822 
1823 void TargetLibraryInfoWrapperPass::anchor() {}
1824 
1825 void TargetLibraryInfoImpl::getWidestVF(StringRef ScalarF,
1826                                         ElementCount &FixedVF,
1827                                         ElementCount &ScalableVF) const {
1828   ScalarF = sanitizeFunctionName(ScalarF);
1829   // Use '0' here because a type of the form <vscale x 1 x ElTy> is not the
1830   // same as a scalar.
1831   ScalableVF = ElementCount::getScalable(0);
1832   FixedVF = ElementCount::getFixed(1);
1833   if (ScalarF.empty())
1834     return;
1835 
1836   std::vector<VecDesc>::const_iterator I =
1837       llvm::lower_bound(VectorDescs, ScalarF, compareWithScalarFnName);
1838   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == ScalarF) {
1839     ElementCount *VF =
1840         I->VectorizationFactor.isScalable() ? &ScalableVF : &FixedVF;
1841     if (ElementCount::isKnownGT(I->VectorizationFactor, *VF))
1842       *VF = I->VectorizationFactor;
1843     ++I;
1844   }
1845 }
1846