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