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