xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/TargetLibraryInfo.cpp (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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/IR/Constants.h"
15 #include "llvm/InitializePasses.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/TargetParser/Triple.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                clEnumValN(TargetLibraryInfoImpl::SLEEFGNUABI, "sleefgnuabi",
36                           "SIMD Library for Evaluating Elementary Functions"),
37                clEnumValN(TargetLibraryInfoImpl::ArmPL, "ArmPL",
38                           "Arm Performance Libraries")));
39 
40 StringLiteral const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] =
41     {
42 #define TLI_DEFINE_STRING
43 #include "llvm/Analysis/TargetLibraryInfo.def"
44 };
45 
46 // Recognized types of library function arguments and return types.
47 enum FuncArgTypeID : char {
48   Void = 0, // Must be zero.
49   Bool,     // 8 bits on all targets
50   Int16,
51   Int32,
52   Int,
53   IntPlus, // Int or bigger.
54   Long,    // Either 32 or 64 bits.
55   IntX,    // Any integer type.
56   Int64,
57   LLong,    // 64 bits on all targets.
58   SizeT,    // size_t.
59   SSizeT,   // POSIX ssize_t.
60   Flt,      // IEEE float.
61   Dbl,      // IEEE double.
62   LDbl,     // Any floating type (TODO: tighten this up).
63   Floating, // Any floating type.
64   Ptr,      // Any pointer type.
65   Struct,   // Any struct type.
66   Ellip,    // The ellipsis (...).
67   Same,     // Same argument type as the previous one.
68 };
69 
70 typedef std::array<FuncArgTypeID, 8> FuncProtoTy;
71 
72 static const FuncProtoTy Signatures[] = {
73 #define TLI_DEFINE_SIG
74 #include "llvm/Analysis/TargetLibraryInfo.def"
75 };
76 
77 static_assert(sizeof Signatures / sizeof *Signatures == LibFunc::NumLibFuncs,
78               "Missing library function signatures");
79 
80 static bool hasSinCosPiStret(const Triple &T) {
81   // Only Darwin variants have _stret versions of combined trig functions.
82   if (!T.isOSDarwin())
83     return false;
84 
85   // The ABI is rather complicated on x86, so don't do anything special there.
86   if (T.getArch() == Triple::x86)
87     return false;
88 
89   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
90     return false;
91 
92   if (T.isiOS() && T.isOSVersionLT(7, 0))
93     return false;
94 
95   return true;
96 }
97 
98 static bool hasBcmp(const Triple &TT) {
99   // Posix removed support from bcmp() in 2001, but the glibc and several
100   // implementations of the libc still have it.
101   if (TT.isOSLinux())
102     return TT.isGNUEnvironment() || TT.isMusl();
103   // Both NetBSD and OpenBSD are planning to remove the function. Windows does
104   // not have it.
105   return TT.isOSFreeBSD() || TT.isOSSolaris();
106 }
107 
108 static bool isCallingConvCCompatible(CallingConv::ID CC, StringRef TT,
109                                      FunctionType *FuncTy) {
110   switch (CC) {
111   default:
112     return false;
113   case llvm::CallingConv::C:
114     return true;
115   case llvm::CallingConv::ARM_APCS:
116   case llvm::CallingConv::ARM_AAPCS:
117   case llvm::CallingConv::ARM_AAPCS_VFP: {
118 
119     // The iOS ABI diverges from the standard in some cases, so for now don't
120     // try to simplify those calls.
121     if (Triple(TT).isiOS())
122       return false;
123 
124     if (!FuncTy->getReturnType()->isPointerTy() &&
125         !FuncTy->getReturnType()->isIntegerTy() &&
126         !FuncTy->getReturnType()->isVoidTy())
127       return false;
128 
129     for (auto *Param : FuncTy->params()) {
130       if (!Param->isPointerTy() && !Param->isIntegerTy())
131         return false;
132     }
133     return true;
134   }
135   }
136   return false;
137 }
138 
139 bool TargetLibraryInfoImpl::isCallingConvCCompatible(CallBase *CI) {
140   return ::isCallingConvCCompatible(CI->getCallingConv(),
141                                     CI->getModule()->getTargetTriple(),
142                                     CI->getFunctionType());
143 }
144 
145 bool TargetLibraryInfoImpl::isCallingConvCCompatible(Function *F) {
146   return ::isCallingConvCCompatible(F->getCallingConv(),
147                                     F->getParent()->getTargetTriple(),
148                                     F->getFunctionType());
149 }
150 
151 /// Initialize the set of available library functions based on the specified
152 /// target triple. This should be carefully written so that a missing target
153 /// triple gets a sane set of defaults.
154 static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
155                        ArrayRef<StringLiteral> StandardNames) {
156   // Verify that the StandardNames array is in alphabetical order.
157   assert(
158       llvm::is_sorted(StandardNames,
159                       [](StringRef LHS, StringRef RHS) { return LHS < RHS; }) &&
160       "TargetLibraryInfoImpl function names must be sorted");
161 
162   // Set IO unlocked variants as unavailable
163   // Set them as available per system below
164   TLI.setUnavailable(LibFunc_getc_unlocked);
165   TLI.setUnavailable(LibFunc_getchar_unlocked);
166   TLI.setUnavailable(LibFunc_putc_unlocked);
167   TLI.setUnavailable(LibFunc_putchar_unlocked);
168   TLI.setUnavailable(LibFunc_fputc_unlocked);
169   TLI.setUnavailable(LibFunc_fgetc_unlocked);
170   TLI.setUnavailable(LibFunc_fread_unlocked);
171   TLI.setUnavailable(LibFunc_fwrite_unlocked);
172   TLI.setUnavailable(LibFunc_fputs_unlocked);
173   TLI.setUnavailable(LibFunc_fgets_unlocked);
174 
175   bool ShouldExtI32Param, ShouldExtI32Return;
176   bool ShouldSignExtI32Param, ShouldSignExtI32Return;
177   TargetLibraryInfo::initExtensionsForTriple(ShouldExtI32Param,
178        ShouldExtI32Return, ShouldSignExtI32Param, ShouldSignExtI32Return, T);
179   TLI.setShouldExtI32Param(ShouldExtI32Param);
180   TLI.setShouldExtI32Return(ShouldExtI32Return);
181   TLI.setShouldSignExtI32Param(ShouldSignExtI32Param);
182   TLI.setShouldSignExtI32Return(ShouldSignExtI32Return);
183 
184   // Let's assume by default that the size of int is 32 bits, unless the target
185   // is a 16-bit architecture because then it most likely is 16 bits. If that
186   // isn't true for a target those defaults should be overridden below.
187   TLI.setIntSize(T.isArch16Bit() ? 16 : 32);
188 
189   // There is really no runtime library on AMDGPU, apart from
190   // __kmpc_alloc/free_shared.
191   if (T.isAMDGPU()) {
192     TLI.disableAllFunctions();
193     TLI.setAvailable(llvm::LibFunc___kmpc_alloc_shared);
194     TLI.setAvailable(llvm::LibFunc___kmpc_free_shared);
195     return;
196   }
197 
198   // memset_pattern{4,8,16} is only available on iOS 3.0 and Mac OS X 10.5 and
199   // later. All versions of watchOS support it.
200   if (T.isMacOSX()) {
201     // available IO unlocked variants on Mac OS X
202     TLI.setAvailable(LibFunc_getc_unlocked);
203     TLI.setAvailable(LibFunc_getchar_unlocked);
204     TLI.setAvailable(LibFunc_putc_unlocked);
205     TLI.setAvailable(LibFunc_putchar_unlocked);
206 
207     if (T.isMacOSXVersionLT(10, 5)) {
208       TLI.setUnavailable(LibFunc_memset_pattern4);
209       TLI.setUnavailable(LibFunc_memset_pattern8);
210       TLI.setUnavailable(LibFunc_memset_pattern16);
211     }
212   } else if (T.isiOS()) {
213     if (T.isOSVersionLT(3, 0)) {
214       TLI.setUnavailable(LibFunc_memset_pattern4);
215       TLI.setUnavailable(LibFunc_memset_pattern8);
216       TLI.setUnavailable(LibFunc_memset_pattern16);
217     }
218   } else if (!T.isWatchOS()) {
219     TLI.setUnavailable(LibFunc_memset_pattern4);
220     TLI.setUnavailable(LibFunc_memset_pattern8);
221     TLI.setUnavailable(LibFunc_memset_pattern16);
222   }
223 
224   if (!hasSinCosPiStret(T)) {
225     TLI.setUnavailable(LibFunc_sinpi);
226     TLI.setUnavailable(LibFunc_sinpif);
227     TLI.setUnavailable(LibFunc_cospi);
228     TLI.setUnavailable(LibFunc_cospif);
229     TLI.setUnavailable(LibFunc_sincospi_stret);
230     TLI.setUnavailable(LibFunc_sincospif_stret);
231   }
232 
233   if (!hasBcmp(T))
234     TLI.setUnavailable(LibFunc_bcmp);
235 
236   if (T.isMacOSX() && T.getArch() == Triple::x86 &&
237       !T.isMacOSXVersionLT(10, 7)) {
238     // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
239     // we don't care about) have two versions; on recent OSX, the one we want
240     // has a $UNIX2003 suffix. The two implementations are identical except
241     // for the return value in some edge cases.  However, we don't want to
242     // generate code that depends on the old symbols.
243     TLI.setAvailableWithName(LibFunc_fwrite, "fwrite$UNIX2003");
244     TLI.setAvailableWithName(LibFunc_fputs, "fputs$UNIX2003");
245   }
246 
247   // iprintf and friends are only available on XCore, TCE, and Emscripten.
248   if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce &&
249       T.getOS() != Triple::Emscripten) {
250     TLI.setUnavailable(LibFunc_iprintf);
251     TLI.setUnavailable(LibFunc_siprintf);
252     TLI.setUnavailable(LibFunc_fiprintf);
253   }
254 
255   // __small_printf and friends are only available on Emscripten.
256   if (T.getOS() != Triple::Emscripten) {
257     TLI.setUnavailable(LibFunc_small_printf);
258     TLI.setUnavailable(LibFunc_small_sprintf);
259     TLI.setUnavailable(LibFunc_small_fprintf);
260   }
261 
262   if (T.isOSWindows() && !T.isOSCygMing()) {
263     // XXX: The earliest documentation available at the moment is for VS2015/VC19:
264     // https://docs.microsoft.com/en-us/cpp/c-runtime-library/floating-point-support?view=vs-2015
265     // XXX: In order to use an MSVCRT older than VC19,
266     // the specific library version must be explicit in the target triple,
267     // e.g., x86_64-pc-windows-msvc18.
268     bool hasPartialC99 = true;
269     if (T.isKnownWindowsMSVCEnvironment()) {
270       VersionTuple Version = T.getEnvironmentVersion();
271       hasPartialC99 = (Version.getMajor() == 0 || Version.getMajor() >= 19);
272     }
273 
274     // Latest targets support C89 math functions, in part.
275     bool isARM = (T.getArch() == Triple::aarch64 ||
276                   T.getArch() == Triple::arm);
277     bool hasPartialFloat = (isARM ||
278                             T.getArch() == Triple::x86_64);
279 
280     // Win32 does not support float C89 math functions, in general.
281     if (!hasPartialFloat) {
282       TLI.setUnavailable(LibFunc_acosf);
283       TLI.setUnavailable(LibFunc_asinf);
284       TLI.setUnavailable(LibFunc_atan2f);
285       TLI.setUnavailable(LibFunc_atanf);
286       TLI.setUnavailable(LibFunc_ceilf);
287       TLI.setUnavailable(LibFunc_cosf);
288       TLI.setUnavailable(LibFunc_coshf);
289       TLI.setUnavailable(LibFunc_expf);
290       TLI.setUnavailable(LibFunc_floorf);
291       TLI.setUnavailable(LibFunc_fmodf);
292       TLI.setUnavailable(LibFunc_log10f);
293       TLI.setUnavailable(LibFunc_logf);
294       TLI.setUnavailable(LibFunc_modff);
295       TLI.setUnavailable(LibFunc_powf);
296       TLI.setUnavailable(LibFunc_remainderf);
297       TLI.setUnavailable(LibFunc_sinf);
298       TLI.setUnavailable(LibFunc_sinhf);
299       TLI.setUnavailable(LibFunc_sqrtf);
300       TLI.setUnavailable(LibFunc_tanf);
301       TLI.setUnavailable(LibFunc_tanhf);
302     }
303     if (!isARM)
304       TLI.setUnavailable(LibFunc_fabsf);
305     TLI.setUnavailable(LibFunc_frexpf);
306     TLI.setUnavailable(LibFunc_ldexpf);
307 
308     // Win32 does not support long double C89 math functions.
309     TLI.setUnavailable(LibFunc_acosl);
310     TLI.setUnavailable(LibFunc_asinl);
311     TLI.setUnavailable(LibFunc_atan2l);
312     TLI.setUnavailable(LibFunc_atanl);
313     TLI.setUnavailable(LibFunc_ceill);
314     TLI.setUnavailable(LibFunc_cosl);
315     TLI.setUnavailable(LibFunc_coshl);
316     TLI.setUnavailable(LibFunc_expl);
317     TLI.setUnavailable(LibFunc_fabsl);
318     TLI.setUnavailable(LibFunc_floorl);
319     TLI.setUnavailable(LibFunc_fmodl);
320     TLI.setUnavailable(LibFunc_frexpl);
321     TLI.setUnavailable(LibFunc_ldexpl);
322     TLI.setUnavailable(LibFunc_log10l);
323     TLI.setUnavailable(LibFunc_logl);
324     TLI.setUnavailable(LibFunc_modfl);
325     TLI.setUnavailable(LibFunc_powl);
326     TLI.setUnavailable(LibFunc_remainderl);
327     TLI.setUnavailable(LibFunc_sinl);
328     TLI.setUnavailable(LibFunc_sinhl);
329     TLI.setUnavailable(LibFunc_sqrtl);
330     TLI.setUnavailable(LibFunc_tanl);
331     TLI.setUnavailable(LibFunc_tanhl);
332 
333     // Win32 does not fully support C99 math functions.
334     if (!hasPartialC99) {
335       TLI.setUnavailable(LibFunc_acosh);
336       TLI.setUnavailable(LibFunc_acoshf);
337       TLI.setUnavailable(LibFunc_asinh);
338       TLI.setUnavailable(LibFunc_asinhf);
339       TLI.setUnavailable(LibFunc_atanh);
340       TLI.setUnavailable(LibFunc_atanhf);
341       TLI.setAvailableWithName(LibFunc_cabs, "_cabs");
342       TLI.setUnavailable(LibFunc_cabsf);
343       TLI.setUnavailable(LibFunc_cbrt);
344       TLI.setUnavailable(LibFunc_cbrtf);
345       TLI.setAvailableWithName(LibFunc_copysign, "_copysign");
346       TLI.setAvailableWithName(LibFunc_copysignf, "_copysignf");
347       TLI.setUnavailable(LibFunc_exp2);
348       TLI.setUnavailable(LibFunc_exp2f);
349       TLI.setUnavailable(LibFunc_expm1);
350       TLI.setUnavailable(LibFunc_expm1f);
351       TLI.setUnavailable(LibFunc_fmax);
352       TLI.setUnavailable(LibFunc_fmaxf);
353       TLI.setUnavailable(LibFunc_fmin);
354       TLI.setUnavailable(LibFunc_fminf);
355       TLI.setUnavailable(LibFunc_log1p);
356       TLI.setUnavailable(LibFunc_log1pf);
357       TLI.setUnavailable(LibFunc_log2);
358       TLI.setUnavailable(LibFunc_log2f);
359       TLI.setAvailableWithName(LibFunc_logb, "_logb");
360       if (hasPartialFloat)
361         TLI.setAvailableWithName(LibFunc_logbf, "_logbf");
362       else
363         TLI.setUnavailable(LibFunc_logbf);
364       TLI.setUnavailable(LibFunc_rint);
365       TLI.setUnavailable(LibFunc_rintf);
366       TLI.setUnavailable(LibFunc_round);
367       TLI.setUnavailable(LibFunc_roundf);
368       TLI.setUnavailable(LibFunc_trunc);
369       TLI.setUnavailable(LibFunc_truncf);
370     }
371 
372     // Win32 does not support long double C99 math functions.
373     TLI.setUnavailable(LibFunc_acoshl);
374     TLI.setUnavailable(LibFunc_asinhl);
375     TLI.setUnavailable(LibFunc_atanhl);
376     TLI.setUnavailable(LibFunc_cabsl);
377     TLI.setUnavailable(LibFunc_cbrtl);
378     TLI.setUnavailable(LibFunc_copysignl);
379     TLI.setUnavailable(LibFunc_exp2l);
380     TLI.setUnavailable(LibFunc_expm1l);
381     TLI.setUnavailable(LibFunc_fmaxl);
382     TLI.setUnavailable(LibFunc_fminl);
383     TLI.setUnavailable(LibFunc_log1pl);
384     TLI.setUnavailable(LibFunc_log2l);
385     TLI.setUnavailable(LibFunc_logbl);
386     TLI.setUnavailable(LibFunc_nearbyintl);
387     TLI.setUnavailable(LibFunc_rintl);
388     TLI.setUnavailable(LibFunc_roundl);
389     TLI.setUnavailable(LibFunc_truncl);
390 
391     // Win32 does not support these functions, but
392     // they are generally available on POSIX-compliant systems.
393     TLI.setUnavailable(LibFunc_access);
394     TLI.setUnavailable(LibFunc_chmod);
395     TLI.setUnavailable(LibFunc_closedir);
396     TLI.setUnavailable(LibFunc_fdopen);
397     TLI.setUnavailable(LibFunc_fileno);
398     TLI.setUnavailable(LibFunc_fseeko);
399     TLI.setUnavailable(LibFunc_fstat);
400     TLI.setUnavailable(LibFunc_ftello);
401     TLI.setUnavailable(LibFunc_gettimeofday);
402     TLI.setUnavailable(LibFunc_memccpy);
403     TLI.setUnavailable(LibFunc_mkdir);
404     TLI.setUnavailable(LibFunc_open);
405     TLI.setUnavailable(LibFunc_opendir);
406     TLI.setUnavailable(LibFunc_pclose);
407     TLI.setUnavailable(LibFunc_popen);
408     TLI.setUnavailable(LibFunc_read);
409     TLI.setUnavailable(LibFunc_rmdir);
410     TLI.setUnavailable(LibFunc_stat);
411     TLI.setUnavailable(LibFunc_strcasecmp);
412     TLI.setUnavailable(LibFunc_strncasecmp);
413     TLI.setUnavailable(LibFunc_unlink);
414     TLI.setUnavailable(LibFunc_utime);
415     TLI.setUnavailable(LibFunc_write);
416   }
417 
418   if (T.isOSWindows() && !T.isWindowsCygwinEnvironment()) {
419     // These functions aren't available in either MSVC or MinGW environments.
420     TLI.setUnavailable(LibFunc_bcmp);
421     TLI.setUnavailable(LibFunc_bcopy);
422     TLI.setUnavailable(LibFunc_bzero);
423     TLI.setUnavailable(LibFunc_chown);
424     TLI.setUnavailable(LibFunc_ctermid);
425     TLI.setUnavailable(LibFunc_ffs);
426     TLI.setUnavailable(LibFunc_flockfile);
427     TLI.setUnavailable(LibFunc_fstatvfs);
428     TLI.setUnavailable(LibFunc_ftrylockfile);
429     TLI.setUnavailable(LibFunc_funlockfile);
430     TLI.setUnavailable(LibFunc_getitimer);
431     TLI.setUnavailable(LibFunc_getlogin_r);
432     TLI.setUnavailable(LibFunc_getpwnam);
433     TLI.setUnavailable(LibFunc_htonl);
434     TLI.setUnavailable(LibFunc_htons);
435     TLI.setUnavailable(LibFunc_lchown);
436     TLI.setUnavailable(LibFunc_lstat);
437     TLI.setUnavailable(LibFunc_memrchr);
438     TLI.setUnavailable(LibFunc_ntohl);
439     TLI.setUnavailable(LibFunc_ntohs);
440     TLI.setUnavailable(LibFunc_pread);
441     TLI.setUnavailable(LibFunc_pwrite);
442     TLI.setUnavailable(LibFunc_readlink);
443     TLI.setUnavailable(LibFunc_realpath);
444     TLI.setUnavailable(LibFunc_setitimer);
445     TLI.setUnavailable(LibFunc_statvfs);
446     TLI.setUnavailable(LibFunc_stpcpy);
447     TLI.setUnavailable(LibFunc_stpncpy);
448     TLI.setUnavailable(LibFunc_times);
449     TLI.setUnavailable(LibFunc_uname);
450     TLI.setUnavailable(LibFunc_unsetenv);
451     TLI.setUnavailable(LibFunc_utimes);
452   }
453 
454   // Pick just one set of new/delete variants.
455   if (T.isOSMSVCRT()) {
456     // MSVC, doesn't have the Itanium new/delete.
457     TLI.setUnavailable(LibFunc_ZdaPv);
458     TLI.setUnavailable(LibFunc_ZdaPvRKSt9nothrow_t);
459     TLI.setUnavailable(LibFunc_ZdaPvSt11align_val_t);
460     TLI.setUnavailable(LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t);
461     TLI.setUnavailable(LibFunc_ZdaPvj);
462     TLI.setUnavailable(LibFunc_ZdaPvjSt11align_val_t);
463     TLI.setUnavailable(LibFunc_ZdaPvm);
464     TLI.setUnavailable(LibFunc_ZdaPvmSt11align_val_t);
465     TLI.setUnavailable(LibFunc_ZdlPv);
466     TLI.setUnavailable(LibFunc_ZdlPvRKSt9nothrow_t);
467     TLI.setUnavailable(LibFunc_ZdlPvSt11align_val_t);
468     TLI.setUnavailable(LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t);
469     TLI.setUnavailable(LibFunc_ZdlPvj);
470     TLI.setUnavailable(LibFunc_ZdlPvjSt11align_val_t);
471     TLI.setUnavailable(LibFunc_ZdlPvm);
472     TLI.setUnavailable(LibFunc_ZdlPvmSt11align_val_t);
473     TLI.setUnavailable(LibFunc_Znaj);
474     TLI.setUnavailable(LibFunc_ZnajRKSt9nothrow_t);
475     TLI.setUnavailable(LibFunc_ZnajSt11align_val_t);
476     TLI.setUnavailable(LibFunc_ZnajSt11align_val_tRKSt9nothrow_t);
477     TLI.setUnavailable(LibFunc_Znam);
478     TLI.setUnavailable(LibFunc_ZnamRKSt9nothrow_t);
479     TLI.setUnavailable(LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t);
480     TLI.setUnavailable(LibFunc_ZnamSt11align_val_t);
481     TLI.setUnavailable(LibFunc_ZnamSt11align_val_tRKSt9nothrow_t);
482     TLI.setUnavailable(LibFunc_Znwj);
483     TLI.setUnavailable(LibFunc_ZnwjRKSt9nothrow_t);
484     TLI.setUnavailable(LibFunc_ZnwjSt11align_val_t);
485     TLI.setUnavailable(LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t);
486     TLI.setUnavailable(LibFunc_Znwm);
487     TLI.setUnavailable(LibFunc_ZnwmRKSt9nothrow_t);
488     TLI.setUnavailable(LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t);
489     TLI.setUnavailable(LibFunc_ZnwmSt11align_val_t);
490     TLI.setUnavailable(LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t);
491     TLI.setUnavailable(LibFunc_Znwm12__hot_cold_t);
492     TLI.setUnavailable(LibFunc_ZnwmSt11align_val_t12__hot_cold_t);
493     TLI.setUnavailable(LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t);
494     TLI.setUnavailable(LibFunc_Znam12__hot_cold_t);
495     TLI.setUnavailable(LibFunc_ZnamSt11align_val_t12__hot_cold_t);
496     TLI.setUnavailable(LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t);
497   } else {
498     // Not MSVC, assume it's Itanium.
499     TLI.setUnavailable(LibFunc_msvc_new_int);
500     TLI.setUnavailable(LibFunc_msvc_new_int_nothrow);
501     TLI.setUnavailable(LibFunc_msvc_new_longlong);
502     TLI.setUnavailable(LibFunc_msvc_new_longlong_nothrow);
503     TLI.setUnavailable(LibFunc_msvc_delete_ptr32);
504     TLI.setUnavailable(LibFunc_msvc_delete_ptr32_nothrow);
505     TLI.setUnavailable(LibFunc_msvc_delete_ptr32_int);
506     TLI.setUnavailable(LibFunc_msvc_delete_ptr64);
507     TLI.setUnavailable(LibFunc_msvc_delete_ptr64_nothrow);
508     TLI.setUnavailable(LibFunc_msvc_delete_ptr64_longlong);
509     TLI.setUnavailable(LibFunc_msvc_new_array_int);
510     TLI.setUnavailable(LibFunc_msvc_new_array_int_nothrow);
511     TLI.setUnavailable(LibFunc_msvc_new_array_longlong);
512     TLI.setUnavailable(LibFunc_msvc_new_array_longlong_nothrow);
513     TLI.setUnavailable(LibFunc_msvc_delete_array_ptr32);
514     TLI.setUnavailable(LibFunc_msvc_delete_array_ptr32_nothrow);
515     TLI.setUnavailable(LibFunc_msvc_delete_array_ptr32_int);
516     TLI.setUnavailable(LibFunc_msvc_delete_array_ptr64);
517     TLI.setUnavailable(LibFunc_msvc_delete_array_ptr64_nothrow);
518     TLI.setUnavailable(LibFunc_msvc_delete_array_ptr64_longlong);
519   }
520 
521   switch (T.getOS()) {
522   case Triple::MacOSX:
523     // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
524     // and their names are __exp10 and __exp10f. exp10l is not available on
525     // OS X or iOS.
526     TLI.setUnavailable(LibFunc_exp10l);
527     if (T.isMacOSXVersionLT(10, 9)) {
528       TLI.setUnavailable(LibFunc_exp10);
529       TLI.setUnavailable(LibFunc_exp10f);
530     } else {
531       TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
532       TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
533     }
534     break;
535   case Triple::IOS:
536   case Triple::TvOS:
537   case Triple::WatchOS:
538     TLI.setUnavailable(LibFunc_exp10l);
539     if (!T.isWatchOS() &&
540         (T.isOSVersionLT(7, 0) || (T.isOSVersionLT(9, 0) && T.isX86()))) {
541       TLI.setUnavailable(LibFunc_exp10);
542       TLI.setUnavailable(LibFunc_exp10f);
543     } else {
544       TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
545       TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
546     }
547     break;
548   case Triple::Linux:
549     // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
550     // buggy prior to glibc version 2.18. Until this version is widely deployed
551     // or we have a reasonable detection strategy, we cannot use exp10 reliably
552     // on Linux.
553     //
554     // Fall through to disable all of them.
555     [[fallthrough]];
556   default:
557     TLI.setUnavailable(LibFunc_exp10);
558     TLI.setUnavailable(LibFunc_exp10f);
559     TLI.setUnavailable(LibFunc_exp10l);
560   }
561 
562   // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
563   // Linux (GLIBC):
564   // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
565   // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
566   // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
567   switch (T.getOS()) {
568   case Triple::Darwin:
569   case Triple::MacOSX:
570   case Triple::IOS:
571   case Triple::TvOS:
572   case Triple::WatchOS:
573   case Triple::FreeBSD:
574   case Triple::Linux:
575     break;
576   default:
577     TLI.setUnavailable(LibFunc_ffsl);
578   }
579 
580   // ffsll is available on at least FreeBSD and Linux (GLIBC):
581   // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
582   // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
583   switch (T.getOS()) {
584   case Triple::Darwin:
585   case Triple::MacOSX:
586   case Triple::IOS:
587   case Triple::TvOS:
588   case Triple::WatchOS:
589   case Triple::FreeBSD:
590   case Triple::Linux:
591     break;
592   default:
593     TLI.setUnavailable(LibFunc_ffsll);
594   }
595 
596   // The following functions are available on at least FreeBSD:
597   // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
598   // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
599   // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
600   if (!T.isOSFreeBSD()) {
601     TLI.setUnavailable(LibFunc_fls);
602     TLI.setUnavailable(LibFunc_flsl);
603     TLI.setUnavailable(LibFunc_flsll);
604   }
605 
606   // The following functions are only available on GNU/Linux (using glibc).
607   // Linux variants without glibc (eg: bionic, musl) may have some subset.
608   if (!T.isOSLinux() || !T.isGNUEnvironment()) {
609     TLI.setUnavailable(LibFunc_dunder_strdup);
610     TLI.setUnavailable(LibFunc_dunder_strtok_r);
611     TLI.setUnavailable(LibFunc_dunder_isoc99_scanf);
612     TLI.setUnavailable(LibFunc_dunder_isoc99_sscanf);
613     TLI.setUnavailable(LibFunc_under_IO_getc);
614     TLI.setUnavailable(LibFunc_under_IO_putc);
615     // But, Android and musl have memalign.
616     if (!T.isAndroid() && !T.isMusl())
617       TLI.setUnavailable(LibFunc_memalign);
618     TLI.setUnavailable(LibFunc_fopen64);
619     TLI.setUnavailable(LibFunc_fseeko64);
620     TLI.setUnavailable(LibFunc_fstat64);
621     TLI.setUnavailable(LibFunc_fstatvfs64);
622     TLI.setUnavailable(LibFunc_ftello64);
623     TLI.setUnavailable(LibFunc_lstat64);
624     TLI.setUnavailable(LibFunc_open64);
625     TLI.setUnavailable(LibFunc_stat64);
626     TLI.setUnavailable(LibFunc_statvfs64);
627     TLI.setUnavailable(LibFunc_tmpfile64);
628 
629     // Relaxed math functions are included in math-finite.h on Linux (GLIBC).
630     // Note that math-finite.h is no longer supported by top-of-tree GLIBC,
631     // so we keep these functions around just so that they're recognized by
632     // the ConstantFolder.
633     TLI.setUnavailable(LibFunc_acos_finite);
634     TLI.setUnavailable(LibFunc_acosf_finite);
635     TLI.setUnavailable(LibFunc_acosl_finite);
636     TLI.setUnavailable(LibFunc_acosh_finite);
637     TLI.setUnavailable(LibFunc_acoshf_finite);
638     TLI.setUnavailable(LibFunc_acoshl_finite);
639     TLI.setUnavailable(LibFunc_asin_finite);
640     TLI.setUnavailable(LibFunc_asinf_finite);
641     TLI.setUnavailable(LibFunc_asinl_finite);
642     TLI.setUnavailable(LibFunc_atan2_finite);
643     TLI.setUnavailable(LibFunc_atan2f_finite);
644     TLI.setUnavailable(LibFunc_atan2l_finite);
645     TLI.setUnavailable(LibFunc_atanh_finite);
646     TLI.setUnavailable(LibFunc_atanhf_finite);
647     TLI.setUnavailable(LibFunc_atanhl_finite);
648     TLI.setUnavailable(LibFunc_cosh_finite);
649     TLI.setUnavailable(LibFunc_coshf_finite);
650     TLI.setUnavailable(LibFunc_coshl_finite);
651     TLI.setUnavailable(LibFunc_exp10_finite);
652     TLI.setUnavailable(LibFunc_exp10f_finite);
653     TLI.setUnavailable(LibFunc_exp10l_finite);
654     TLI.setUnavailable(LibFunc_exp2_finite);
655     TLI.setUnavailable(LibFunc_exp2f_finite);
656     TLI.setUnavailable(LibFunc_exp2l_finite);
657     TLI.setUnavailable(LibFunc_exp_finite);
658     TLI.setUnavailable(LibFunc_expf_finite);
659     TLI.setUnavailable(LibFunc_expl_finite);
660     TLI.setUnavailable(LibFunc_log10_finite);
661     TLI.setUnavailable(LibFunc_log10f_finite);
662     TLI.setUnavailable(LibFunc_log10l_finite);
663     TLI.setUnavailable(LibFunc_log2_finite);
664     TLI.setUnavailable(LibFunc_log2f_finite);
665     TLI.setUnavailable(LibFunc_log2l_finite);
666     TLI.setUnavailable(LibFunc_log_finite);
667     TLI.setUnavailable(LibFunc_logf_finite);
668     TLI.setUnavailable(LibFunc_logl_finite);
669     TLI.setUnavailable(LibFunc_pow_finite);
670     TLI.setUnavailable(LibFunc_powf_finite);
671     TLI.setUnavailable(LibFunc_powl_finite);
672     TLI.setUnavailable(LibFunc_sinh_finite);
673     TLI.setUnavailable(LibFunc_sinhf_finite);
674     TLI.setUnavailable(LibFunc_sinhl_finite);
675     TLI.setUnavailable(LibFunc_sqrt_finite);
676     TLI.setUnavailable(LibFunc_sqrtf_finite);
677     TLI.setUnavailable(LibFunc_sqrtl_finite);
678   }
679 
680   if ((T.isOSLinux() && T.isGNUEnvironment()) ||
681       (T.isAndroid() && !T.isAndroidVersionLT(28))) {
682     // available IO unlocked variants on GNU/Linux and Android P or later
683     TLI.setAvailable(LibFunc_getc_unlocked);
684     TLI.setAvailable(LibFunc_getchar_unlocked);
685     TLI.setAvailable(LibFunc_putc_unlocked);
686     TLI.setAvailable(LibFunc_putchar_unlocked);
687     TLI.setAvailable(LibFunc_fputc_unlocked);
688     TLI.setAvailable(LibFunc_fgetc_unlocked);
689     TLI.setAvailable(LibFunc_fread_unlocked);
690     TLI.setAvailable(LibFunc_fwrite_unlocked);
691     TLI.setAvailable(LibFunc_fputs_unlocked);
692     TLI.setAvailable(LibFunc_fgets_unlocked);
693   }
694 
695   if (T.isAndroid() && T.isAndroidVersionLT(21)) {
696     TLI.setUnavailable(LibFunc_stpcpy);
697     TLI.setUnavailable(LibFunc_stpncpy);
698   }
699 
700   if (T.isPS()) {
701     // PS4/PS5 do have memalign.
702     TLI.setAvailable(LibFunc_memalign);
703 
704     // PS4/PS5 do not have new/delete with "unsigned int" size parameter;
705     // they only have the "unsigned long" versions.
706     TLI.setUnavailable(LibFunc_ZdaPvj);
707     TLI.setUnavailable(LibFunc_ZdaPvjSt11align_val_t);
708     TLI.setUnavailable(LibFunc_ZdlPvj);
709     TLI.setUnavailable(LibFunc_ZdlPvjSt11align_val_t);
710     TLI.setUnavailable(LibFunc_Znaj);
711     TLI.setUnavailable(LibFunc_ZnajRKSt9nothrow_t);
712     TLI.setUnavailable(LibFunc_ZnajSt11align_val_t);
713     TLI.setUnavailable(LibFunc_ZnajSt11align_val_tRKSt9nothrow_t);
714     TLI.setUnavailable(LibFunc_Znwj);
715     TLI.setUnavailable(LibFunc_ZnwjRKSt9nothrow_t);
716     TLI.setUnavailable(LibFunc_ZnwjSt11align_val_t);
717     TLI.setUnavailable(LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t);
718 
719     // None of the *_chk functions.
720     TLI.setUnavailable(LibFunc_memccpy_chk);
721     TLI.setUnavailable(LibFunc_memcpy_chk);
722     TLI.setUnavailable(LibFunc_memmove_chk);
723     TLI.setUnavailable(LibFunc_mempcpy_chk);
724     TLI.setUnavailable(LibFunc_memset_chk);
725     TLI.setUnavailable(LibFunc_snprintf_chk);
726     TLI.setUnavailable(LibFunc_sprintf_chk);
727     TLI.setUnavailable(LibFunc_stpcpy_chk);
728     TLI.setUnavailable(LibFunc_stpncpy_chk);
729     TLI.setUnavailable(LibFunc_strcat_chk);
730     TLI.setUnavailable(LibFunc_strcpy_chk);
731     TLI.setUnavailable(LibFunc_strlcat_chk);
732     TLI.setUnavailable(LibFunc_strlcpy_chk);
733     TLI.setUnavailable(LibFunc_strlen_chk);
734     TLI.setUnavailable(LibFunc_strncat_chk);
735     TLI.setUnavailable(LibFunc_strncpy_chk);
736     TLI.setUnavailable(LibFunc_vsnprintf_chk);
737     TLI.setUnavailable(LibFunc_vsprintf_chk);
738 
739     // Various Posix system functions.
740     TLI.setUnavailable(LibFunc_access);
741     TLI.setUnavailable(LibFunc_chmod);
742     TLI.setUnavailable(LibFunc_chown);
743     TLI.setUnavailable(LibFunc_closedir);
744     TLI.setUnavailable(LibFunc_ctermid);
745     TLI.setUnavailable(LibFunc_execl);
746     TLI.setUnavailable(LibFunc_execle);
747     TLI.setUnavailable(LibFunc_execlp);
748     TLI.setUnavailable(LibFunc_execv);
749     TLI.setUnavailable(LibFunc_execvP);
750     TLI.setUnavailable(LibFunc_execve);
751     TLI.setUnavailable(LibFunc_execvp);
752     TLI.setUnavailable(LibFunc_execvpe);
753     TLI.setUnavailable(LibFunc_fork);
754     TLI.setUnavailable(LibFunc_fstat);
755     TLI.setUnavailable(LibFunc_fstatvfs);
756     TLI.setUnavailable(LibFunc_getenv);
757     TLI.setUnavailable(LibFunc_getitimer);
758     TLI.setUnavailable(LibFunc_getlogin_r);
759     TLI.setUnavailable(LibFunc_getpwnam);
760     TLI.setUnavailable(LibFunc_gettimeofday);
761     TLI.setUnavailable(LibFunc_lchown);
762     TLI.setUnavailable(LibFunc_lstat);
763     TLI.setUnavailable(LibFunc_mkdir);
764     TLI.setUnavailable(LibFunc_open);
765     TLI.setUnavailable(LibFunc_opendir);
766     TLI.setUnavailable(LibFunc_pclose);
767     TLI.setUnavailable(LibFunc_popen);
768     TLI.setUnavailable(LibFunc_pread);
769     TLI.setUnavailable(LibFunc_pwrite);
770     TLI.setUnavailable(LibFunc_read);
771     TLI.setUnavailable(LibFunc_readlink);
772     TLI.setUnavailable(LibFunc_realpath);
773     TLI.setUnavailable(LibFunc_rename);
774     TLI.setUnavailable(LibFunc_rmdir);
775     TLI.setUnavailable(LibFunc_setitimer);
776     TLI.setUnavailable(LibFunc_stat);
777     TLI.setUnavailable(LibFunc_statvfs);
778     TLI.setUnavailable(LibFunc_system);
779     TLI.setUnavailable(LibFunc_times);
780     TLI.setUnavailable(LibFunc_tmpfile);
781     TLI.setUnavailable(LibFunc_unlink);
782     TLI.setUnavailable(LibFunc_uname);
783     TLI.setUnavailable(LibFunc_unsetenv);
784     TLI.setUnavailable(LibFunc_utime);
785     TLI.setUnavailable(LibFunc_utimes);
786     TLI.setUnavailable(LibFunc_valloc);
787     TLI.setUnavailable(LibFunc_write);
788 
789     // Miscellaneous other functions not provided.
790     TLI.setUnavailable(LibFunc_atomic_load);
791     TLI.setUnavailable(LibFunc_atomic_store);
792     TLI.setUnavailable(LibFunc___kmpc_alloc_shared);
793     TLI.setUnavailable(LibFunc___kmpc_free_shared);
794     TLI.setUnavailable(LibFunc_dunder_strndup);
795     TLI.setUnavailable(LibFunc_bcmp);
796     TLI.setUnavailable(LibFunc_bcopy);
797     TLI.setUnavailable(LibFunc_bzero);
798     TLI.setUnavailable(LibFunc_cabs);
799     TLI.setUnavailable(LibFunc_cabsf);
800     TLI.setUnavailable(LibFunc_cabsl);
801     TLI.setUnavailable(LibFunc_ffs);
802     TLI.setUnavailable(LibFunc_flockfile);
803     TLI.setUnavailable(LibFunc_fseeko);
804     TLI.setUnavailable(LibFunc_ftello);
805     TLI.setUnavailable(LibFunc_ftrylockfile);
806     TLI.setUnavailable(LibFunc_funlockfile);
807     TLI.setUnavailable(LibFunc_htonl);
808     TLI.setUnavailable(LibFunc_htons);
809     TLI.setUnavailable(LibFunc_isascii);
810     TLI.setUnavailable(LibFunc_memccpy);
811     TLI.setUnavailable(LibFunc_mempcpy);
812     TLI.setUnavailable(LibFunc_memrchr);
813     TLI.setUnavailable(LibFunc_ntohl);
814     TLI.setUnavailable(LibFunc_ntohs);
815     TLI.setUnavailable(LibFunc_reallocf);
816     TLI.setUnavailable(LibFunc_roundeven);
817     TLI.setUnavailable(LibFunc_roundevenf);
818     TLI.setUnavailable(LibFunc_roundevenl);
819     TLI.setUnavailable(LibFunc_stpcpy);
820     TLI.setUnavailable(LibFunc_stpncpy);
821     TLI.setUnavailable(LibFunc_strlcat);
822     TLI.setUnavailable(LibFunc_strlcpy);
823     TLI.setUnavailable(LibFunc_strndup);
824     TLI.setUnavailable(LibFunc_strnlen);
825     TLI.setUnavailable(LibFunc_toascii);
826   }
827 
828   // As currently implemented in clang, NVPTX code has no standard library to
829   // speak of.  Headers provide a standard-ish library implementation, but many
830   // of the signatures are wrong -- for example, many libm functions are not
831   // extern "C".
832   //
833   // libdevice, an IR library provided by nvidia, is linked in by the front-end,
834   // but only used functions are provided to llvm.  Moreover, most of the
835   // functions in libdevice don't map precisely to standard library functions.
836   //
837   // FIXME: Having no standard library prevents e.g. many fastmath
838   // optimizations, so this situation should be fixed.
839   if (T.isNVPTX()) {
840     TLI.disableAllFunctions();
841     TLI.setAvailable(LibFunc_nvvm_reflect);
842     TLI.setAvailable(llvm::LibFunc_malloc);
843     TLI.setAvailable(llvm::LibFunc_free);
844 
845     // TODO: We could enable the following two according to [0] but we haven't
846     //       done an evaluation wrt. the performance implications.
847     // [0]
848     // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#dynamic-global-memory-allocation-and-operations
849     //
850     //    TLI.setAvailable(llvm::LibFunc_memcpy);
851     //    TLI.setAvailable(llvm::LibFunc_memset);
852 
853     TLI.setAvailable(llvm::LibFunc___kmpc_alloc_shared);
854     TLI.setAvailable(llvm::LibFunc___kmpc_free_shared);
855   } else {
856     TLI.setUnavailable(LibFunc_nvvm_reflect);
857   }
858 
859   // These vec_malloc/free routines are only available on AIX.
860   if (!T.isOSAIX()) {
861     TLI.setUnavailable(LibFunc_vec_calloc);
862     TLI.setUnavailable(LibFunc_vec_malloc);
863     TLI.setUnavailable(LibFunc_vec_realloc);
864     TLI.setUnavailable(LibFunc_vec_free);
865   }
866 
867   TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary, T);
868 }
869 
870 TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
871   // Default to everything being available.
872   memset(AvailableArray, -1, sizeof(AvailableArray));
873 
874   initialize(*this, Triple(), StandardNames);
875 }
876 
877 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
878   // Default to everything being available.
879   memset(AvailableArray, -1, sizeof(AvailableArray));
880 
881   initialize(*this, T, StandardNames);
882 }
883 
884 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
885     : CustomNames(TLI.CustomNames), ShouldExtI32Param(TLI.ShouldExtI32Param),
886       ShouldExtI32Return(TLI.ShouldExtI32Return),
887       ShouldSignExtI32Param(TLI.ShouldSignExtI32Param),
888       ShouldSignExtI32Return(TLI.ShouldSignExtI32Return),
889       SizeOfInt(TLI.SizeOfInt) {
890   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
891   VectorDescs = TLI.VectorDescs;
892   ScalarDescs = TLI.ScalarDescs;
893 }
894 
895 TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
896     : CustomNames(std::move(TLI.CustomNames)),
897       ShouldExtI32Param(TLI.ShouldExtI32Param),
898       ShouldExtI32Return(TLI.ShouldExtI32Return),
899       ShouldSignExtI32Param(TLI.ShouldSignExtI32Param),
900       ShouldSignExtI32Return(TLI.ShouldSignExtI32Return),
901       SizeOfInt(TLI.SizeOfInt) {
902   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
903             AvailableArray);
904   VectorDescs = TLI.VectorDescs;
905   ScalarDescs = TLI.ScalarDescs;
906 }
907 
908 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
909   CustomNames = TLI.CustomNames;
910   ShouldExtI32Param = TLI.ShouldExtI32Param;
911   ShouldExtI32Return = TLI.ShouldExtI32Return;
912   ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
913   ShouldSignExtI32Return = TLI.ShouldSignExtI32Return;
914   SizeOfInt = TLI.SizeOfInt;
915   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
916   return *this;
917 }
918 
919 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
920   CustomNames = std::move(TLI.CustomNames);
921   ShouldExtI32Param = TLI.ShouldExtI32Param;
922   ShouldExtI32Return = TLI.ShouldExtI32Return;
923   ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
924   ShouldSignExtI32Return = TLI.ShouldSignExtI32Return;
925   SizeOfInt = TLI.SizeOfInt;
926   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
927             AvailableArray);
928   return *this;
929 }
930 
931 static StringRef sanitizeFunctionName(StringRef funcName) {
932   // Filter out empty names and names containing null bytes, those can't be in
933   // our table.
934   if (funcName.empty() || funcName.contains('\0'))
935     return StringRef();
936 
937   // Check for \01 prefix that is used to mangle __asm declarations and
938   // strip it if present.
939   return GlobalValue::dropLLVMManglingEscape(funcName);
940 }
941 
942 bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName, LibFunc &F) const {
943   funcName = sanitizeFunctionName(funcName);
944   if (funcName.empty())
945     return false;
946 
947   const auto *Start = std::begin(StandardNames);
948   const auto *End = std::end(StandardNames);
949   const auto *I = std::lower_bound(Start, End, funcName);
950   if (I != End && *I == funcName) {
951     F = (LibFunc)(I - Start);
952     return true;
953   }
954   return false;
955 }
956 
957 // Return true if ArgTy matches Ty.
958 
959 static bool matchType(FuncArgTypeID ArgTy, const Type *Ty, unsigned IntBits,
960                       unsigned SizeTBits) {
961   switch (ArgTy) {
962   case Void:
963     return Ty->isVoidTy();
964   case Bool:
965     return Ty->isIntegerTy(8);
966   case Int16:
967     return Ty->isIntegerTy(16);
968   case Int32:
969     return Ty->isIntegerTy(32);
970   case Int:
971     return Ty->isIntegerTy(IntBits);
972   case IntPlus:
973     return Ty->isIntegerTy() && Ty->getPrimitiveSizeInBits() >= IntBits;
974   case IntX:
975     return Ty->isIntegerTy();
976   case Long:
977     // TODO: Figure out and use long size.
978     return Ty->isIntegerTy() && Ty->getPrimitiveSizeInBits() >= IntBits;
979   case Int64:
980     return Ty->isIntegerTy(64);
981   case LLong:
982     return Ty->isIntegerTy(64);
983   case SizeT:
984   case SSizeT:
985     return Ty->isIntegerTy(SizeTBits);
986   case Flt:
987     return Ty->isFloatTy();
988   case Dbl:
989     return Ty->isDoubleTy();
990     // TODO: Tighten this up.
991   case LDbl:
992     return Ty->isFloatingPointTy();
993   case Floating:
994     return Ty->isFloatingPointTy();
995   case Ptr:
996     return Ty->isPointerTy();
997   case Struct:
998     return Ty->isStructTy();
999   default:
1000     break;
1001   }
1002 
1003   llvm_unreachable("Invalid type");
1004 }
1005 
1006 bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
1007                                                    LibFunc F,
1008                                                    const Module &M) const {
1009   unsigned NumParams = FTy.getNumParams();
1010 
1011   switch (F) {
1012     // Special handling for <complex.h> functions:
1013   case LibFunc_cabs:
1014   case LibFunc_cabsf:
1015   case LibFunc_cabsl: {
1016     Type *RetTy = FTy.getReturnType();
1017     if (!RetTy->isFloatingPointTy())
1018       return false;
1019 
1020     Type *ParamTy = FTy.getParamType(0);
1021     // NOTE: These prototypes are target specific and currently support
1022     // "complex" passed as an array or discrete real & imaginary parameters.
1023     // Add other calling conventions to enable libcall optimizations.
1024     if (NumParams == 1)
1025       return (ParamTy->isArrayTy() && ParamTy->getArrayNumElements() == 2 &&
1026               ParamTy->getArrayElementType() == RetTy);
1027     else if (NumParams == 2)
1028       return ParamTy == RetTy && FTy.getParamType(1) == RetTy;
1029 
1030     return false;
1031   }
1032     // Special handling for the sincospi functions that return either
1033     // a struct or vector:
1034   case LibFunc_sincospi_stret:
1035   case LibFunc_sincospif_stret: {
1036     if (NumParams != 1)
1037       return false;
1038 
1039     Type *RetTy = FTy.getReturnType();
1040     Type *ParamTy = FTy.getParamType(0);
1041     if (auto *Ty = dyn_cast<StructType>(RetTy)) {
1042       if (Ty->getNumElements() != 2)
1043         return false;
1044       return (Ty->getElementType(0) == ParamTy &&
1045               Ty->getElementType(1) == ParamTy);
1046     }
1047 
1048     if (auto *Ty = dyn_cast<FixedVectorType>(RetTy)) {
1049       if (Ty->getNumElements() != 2)
1050         return false;
1051       return Ty->getElementType() == ParamTy;
1052     }
1053 
1054     return false;
1055   }
1056 
1057   default:
1058     break;
1059   }
1060 
1061   unsigned IntBits = getIntSize();
1062   unsigned SizeTBits = getSizeTSize(M);
1063   unsigned Idx = 0;
1064 
1065   // Iterate over the type ids in the function prototype, matching each
1066   // against the function's type FTy, starting with its return type.
1067   // Return true if both match in number and kind, inclduing the ellipsis.
1068   Type *Ty = FTy.getReturnType(), *LastTy = Ty;
1069   const auto &ProtoTypes = Signatures[F];
1070   for (auto TyID : ProtoTypes) {
1071     if (Idx && TyID == Void)
1072       // Except in the first position where it designates the function's
1073       // return type Void ends the argument list.
1074       break;
1075 
1076     if (TyID == Ellip) {
1077       // The ellipsis ends the protoype list but is not a part of FTy's
1078       // argument list.  Except when it's last it must be followed by
1079       // Void.
1080       assert(Idx == ProtoTypes.size() - 1 || ProtoTypes[Idx + 1] == Void);
1081       return FTy.isFunctionVarArg();
1082     }
1083 
1084     if (TyID == Same) {
1085       assert(Idx != 0 && "Type ID 'Same' must not be first!");
1086       if (Ty != LastTy)
1087         return false;
1088     } else {
1089       if (!Ty || !matchType(TyID, Ty, IntBits, SizeTBits))
1090         return false;
1091       LastTy = Ty;
1092     }
1093 
1094     if (Idx == NumParams) {
1095       // There's at least one and at most two more type ids than there are
1096       // arguments in FTy's argument list.
1097       Ty = nullptr;
1098       ++Idx;
1099       continue;
1100     }
1101 
1102     Ty = FTy.getParamType(Idx++);
1103   }
1104 
1105   // Return success only if all entries on both lists have been processed
1106   // and the function is not a variadic one.
1107   return Idx == NumParams + 1 && !FTy.isFunctionVarArg();
1108 }
1109 
1110 bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
1111                                        LibFunc &F) const {
1112   // Intrinsics don't overlap w/libcalls; if our module has a large number of
1113   // intrinsics, this ends up being an interesting compile time win since we
1114   // avoid string normalization and comparison.
1115   if (FDecl.isIntrinsic()) return false;
1116 
1117   const Module *M = FDecl.getParent();
1118   assert(M && "Expecting FDecl to be connected to a Module.");
1119 
1120   return getLibFunc(FDecl.getName(), F) &&
1121          isValidProtoForLibFunc(*FDecl.getFunctionType(), F, *M);
1122 }
1123 
1124 void TargetLibraryInfoImpl::disableAllFunctions() {
1125   memset(AvailableArray, 0, sizeof(AvailableArray));
1126 }
1127 
1128 static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
1129   return LHS.ScalarFnName < RHS.ScalarFnName;
1130 }
1131 
1132 static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
1133   return LHS.VectorFnName < RHS.VectorFnName;
1134 }
1135 
1136 static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
1137   return LHS.ScalarFnName < S;
1138 }
1139 
1140 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
1141   llvm::append_range(VectorDescs, Fns);
1142   llvm::sort(VectorDescs, compareByScalarFnName);
1143 
1144   llvm::append_range(ScalarDescs, Fns);
1145   llvm::sort(ScalarDescs, compareByVectorFnName);
1146 }
1147 
1148 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
1149     enum VectorLibrary VecLib, const llvm::Triple &TargetTriple) {
1150   switch (VecLib) {
1151   case Accelerate: {
1152     const VecDesc VecFuncs[] = {
1153     #define TLI_DEFINE_ACCELERATE_VECFUNCS
1154     #include "llvm/Analysis/VecFuncs.def"
1155     };
1156     addVectorizableFunctions(VecFuncs);
1157     break;
1158   }
1159   case DarwinLibSystemM: {
1160     const VecDesc VecFuncs[] = {
1161     #define TLI_DEFINE_DARWIN_LIBSYSTEM_M_VECFUNCS
1162     #include "llvm/Analysis/VecFuncs.def"
1163     };
1164     addVectorizableFunctions(VecFuncs);
1165     break;
1166   }
1167   case LIBMVEC_X86: {
1168     const VecDesc VecFuncs[] = {
1169     #define TLI_DEFINE_LIBMVEC_X86_VECFUNCS
1170     #include "llvm/Analysis/VecFuncs.def"
1171     };
1172     addVectorizableFunctions(VecFuncs);
1173     break;
1174   }
1175   case MASSV: {
1176     const VecDesc VecFuncs[] = {
1177     #define TLI_DEFINE_MASSV_VECFUNCS
1178     #include "llvm/Analysis/VecFuncs.def"
1179     };
1180     addVectorizableFunctions(VecFuncs);
1181     break;
1182   }
1183   case SVML: {
1184     const VecDesc VecFuncs[] = {
1185     #define TLI_DEFINE_SVML_VECFUNCS
1186     #include "llvm/Analysis/VecFuncs.def"
1187     };
1188     addVectorizableFunctions(VecFuncs);
1189     break;
1190   }
1191   case SLEEFGNUABI: {
1192     const VecDesc VecFuncs_VF2[] = {
1193 #define TLI_DEFINE_SLEEFGNUABI_VF2_VECFUNCS
1194 #define TLI_DEFINE_VECFUNC(SCAL, VEC, VF) {SCAL, VEC, VF, /* MASK = */ false},
1195 #include "llvm/Analysis/VecFuncs.def"
1196     };
1197     const VecDesc VecFuncs_VF4[] = {
1198 #define TLI_DEFINE_SLEEFGNUABI_VF4_VECFUNCS
1199 #define TLI_DEFINE_VECFUNC(SCAL, VEC, VF) {SCAL, VEC, VF, /* MASK = */ false},
1200 #include "llvm/Analysis/VecFuncs.def"
1201     };
1202     const VecDesc VecFuncs_VFScalable[] = {
1203 #define TLI_DEFINE_SLEEFGNUABI_SCALABLE_VECFUNCS
1204 #define TLI_DEFINE_VECFUNC(SCAL, VEC, VF, MASK) {SCAL, VEC, VF, MASK},
1205 #include "llvm/Analysis/VecFuncs.def"
1206     };
1207 
1208     switch (TargetTriple.getArch()) {
1209     default:
1210       break;
1211     case llvm::Triple::aarch64:
1212     case llvm::Triple::aarch64_be:
1213       addVectorizableFunctions(VecFuncs_VF2);
1214       addVectorizableFunctions(VecFuncs_VF4);
1215       addVectorizableFunctions(VecFuncs_VFScalable);
1216       break;
1217     }
1218     break;
1219   }
1220   case ArmPL: {
1221     const VecDesc VecFuncs[] = {
1222 #define TLI_DEFINE_ARMPL_VECFUNCS
1223 #define TLI_DEFINE_VECFUNC(SCAL, VEC, VF, MASK) {SCAL, VEC, VF, MASK},
1224 #include "llvm/Analysis/VecFuncs.def"
1225     };
1226 
1227     switch (TargetTriple.getArch()) {
1228     default:
1229       break;
1230     case llvm::Triple::aarch64:
1231     case llvm::Triple::aarch64_be:
1232       addVectorizableFunctions(VecFuncs);
1233       break;
1234     }
1235     break;
1236   }
1237   case NoLibrary:
1238     break;
1239   }
1240 }
1241 
1242 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
1243   funcName = sanitizeFunctionName(funcName);
1244   if (funcName.empty())
1245     return false;
1246 
1247   std::vector<VecDesc>::const_iterator I =
1248       llvm::lower_bound(VectorDescs, funcName, compareWithScalarFnName);
1249   return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
1250 }
1251 
1252 StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
1253                                                        const ElementCount &VF,
1254                                                        bool Masked) const {
1255   F = sanitizeFunctionName(F);
1256   if (F.empty())
1257     return F;
1258   std::vector<VecDesc>::const_iterator I =
1259       llvm::lower_bound(VectorDescs, F, compareWithScalarFnName);
1260   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
1261     if ((I->VectorizationFactor == VF) && (I->Masked == Masked))
1262       return I->VectorFnName;
1263     ++I;
1264   }
1265   return StringRef();
1266 }
1267 
1268 TargetLibraryInfo TargetLibraryAnalysis::run(const Function &F,
1269                                              FunctionAnalysisManager &) {
1270   if (!BaselineInfoImpl)
1271     BaselineInfoImpl =
1272         TargetLibraryInfoImpl(Triple(F.getParent()->getTargetTriple()));
1273   return TargetLibraryInfo(*BaselineInfoImpl, &F);
1274 }
1275 
1276 unsigned TargetLibraryInfoImpl::getWCharSize(const Module &M) const {
1277   if (auto *ShortWChar = cast_or_null<ConstantAsMetadata>(
1278       M.getModuleFlag("wchar_size")))
1279     return cast<ConstantInt>(ShortWChar->getValue())->getZExtValue();
1280   return 0;
1281 }
1282 
1283 unsigned TargetLibraryInfoImpl::getSizeTSize(const Module &M) const {
1284   // There is really no guarantee that sizeof(size_t) is equal to sizeof(int*).
1285   // If that isn't true then it should be possible to derive the SizeTTy from
1286   // the target triple here instead and do an early return.
1287 
1288   // Historically LLVM assume that size_t has same size as intptr_t (hence
1289   // deriving the size from sizeof(int*) in address space zero). This should
1290   // work for most targets. For future consideration: DataLayout also implement
1291   // getIndexSizeInBits which might map better to size_t compared to
1292   // getPointerSizeInBits. Hard coding address space zero here might be
1293   // unfortunate as well. Maybe getDefaultGlobalsAddressSpace() or
1294   // getAllocaAddrSpace() is better.
1295   unsigned AddressSpace = 0;
1296   return M.getDataLayout().getPointerSizeInBits(AddressSpace);
1297 }
1298 
1299 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
1300     : ImmutablePass(ID), TLA(TargetLibraryInfoImpl()) {
1301   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1302 }
1303 
1304 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
1305     : ImmutablePass(ID), TLA(TargetLibraryInfoImpl(T)) {
1306   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1307 }
1308 
1309 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
1310     const TargetLibraryInfoImpl &TLIImpl)
1311     : ImmutablePass(ID), TLA(TLIImpl) {
1312   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1313 }
1314 
1315 AnalysisKey TargetLibraryAnalysis::Key;
1316 
1317 // Register the basic pass.
1318 INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
1319                 "Target Library Information", false, true)
1320 char TargetLibraryInfoWrapperPass::ID = 0;
1321 
1322 void TargetLibraryInfoWrapperPass::anchor() {}
1323 
1324 void TargetLibraryInfoImpl::getWidestVF(StringRef ScalarF,
1325                                         ElementCount &FixedVF,
1326                                         ElementCount &ScalableVF) const {
1327   ScalarF = sanitizeFunctionName(ScalarF);
1328   // Use '0' here because a type of the form <vscale x 1 x ElTy> is not the
1329   // same as a scalar.
1330   ScalableVF = ElementCount::getScalable(0);
1331   FixedVF = ElementCount::getFixed(1);
1332   if (ScalarF.empty())
1333     return;
1334 
1335   std::vector<VecDesc>::const_iterator I =
1336       llvm::lower_bound(VectorDescs, ScalarF, compareWithScalarFnName);
1337   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == ScalarF) {
1338     ElementCount *VF =
1339         I->VectorizationFactor.isScalable() ? &ScalableVF : &FixedVF;
1340     if (ElementCount::isKnownGT(I->VectorizationFactor, *VF))
1341       *VF = I->VectorizationFactor;
1342     ++I;
1343   }
1344 }
1345