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