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