1 //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the TargetLibraryInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Analysis/TargetLibraryInfo.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/Support/CommandLine.h"
19 static cl::opt
<TargetLibraryInfoImpl::VectorLibrary
> ClVectorLibrary(
20 "vector-library", cl::Hidden
, cl::desc("Vector functions library"),
21 cl::init(TargetLibraryInfoImpl::NoLibrary
),
22 cl::values(clEnumValN(TargetLibraryInfoImpl::NoLibrary
, "none",
23 "No vector functions library"),
24 clEnumValN(TargetLibraryInfoImpl::Accelerate
, "Accelerate",
25 "Accelerate framework"),
26 clEnumValN(TargetLibraryInfoImpl::MASSV
, "MASSV",
27 "IBM MASS vector library"),
28 clEnumValN(TargetLibraryInfoImpl::SVML
, "SVML",
29 "Intel SVML library")));
31 StringRef
const TargetLibraryInfoImpl::StandardNames
[LibFunc::NumLibFuncs
] = {
32 #define TLI_DEFINE_STRING
33 #include "llvm/Analysis/TargetLibraryInfo.def"
36 static bool hasSinCosPiStret(const Triple
&T
) {
37 // Only Darwin variants have _stret versions of combined trig functions.
41 // The ABI is rather complicated on x86, so don't do anything special there.
42 if (T
.getArch() == Triple::x86
)
45 if (T
.isMacOSX() && T
.isMacOSXVersionLT(10, 9))
48 if (T
.isiOS() && T
.isOSVersionLT(7, 0))
54 static bool hasBcmp(const Triple
&TT
) {
55 // Posix removed support from bcmp() in 2001, but the glibc and several
56 // implementations of the libc still have it.
58 return TT
.isGNUEnvironment() || TT
.isMusl();
59 // Both NetBSD and OpenBSD are planning to remove the function. Windows does
61 return TT
.isOSFreeBSD() || TT
.isOSSolaris();
64 /// Initialize the set of available library functions based on the specified
65 /// target triple. This should be carefully written so that a missing target
66 /// triple gets a sane set of defaults.
67 static void initialize(TargetLibraryInfoImpl
&TLI
, const Triple
&T
,
68 ArrayRef
<StringRef
> StandardNames
) {
69 // Verify that the StandardNames array is in alphabetical order.
70 assert(std::is_sorted(StandardNames
.begin(), StandardNames
.end(),
71 [](StringRef LHS
, StringRef RHS
) {
74 "TargetLibraryInfoImpl function names must be sorted");
76 // Set IO unlocked variants as unavailable
77 // Set them as available per system below
78 TLI
.setUnavailable(LibFunc_getchar_unlocked
);
79 TLI
.setUnavailable(LibFunc_putc_unlocked
);
80 TLI
.setUnavailable(LibFunc_putchar_unlocked
);
81 TLI
.setUnavailable(LibFunc_fputc_unlocked
);
82 TLI
.setUnavailable(LibFunc_fgetc_unlocked
);
83 TLI
.setUnavailable(LibFunc_fread_unlocked
);
84 TLI
.setUnavailable(LibFunc_fwrite_unlocked
);
85 TLI
.setUnavailable(LibFunc_fputs_unlocked
);
86 TLI
.setUnavailable(LibFunc_fgets_unlocked
);
88 bool ShouldExtI32Param
= false, ShouldExtI32Return
= false,
89 ShouldSignExtI32Param
= false;
90 // PowerPC64, Sparc64, SystemZ need signext/zeroext on i32 parameters and
91 // returns corresponding to C-level ints and unsigned ints.
92 if (T
.isPPC64() || T
.getArch() == Triple::sparcv9
||
93 T
.getArch() == Triple::systemz
) {
94 ShouldExtI32Param
= true;
95 ShouldExtI32Return
= true;
97 // Mips, on the other hand, needs signext on i32 parameters corresponding
98 // to both signed and unsigned ints.
100 ShouldSignExtI32Param
= true;
102 TLI
.setShouldExtI32Param(ShouldExtI32Param
);
103 TLI
.setShouldExtI32Return(ShouldExtI32Return
);
104 TLI
.setShouldSignExtI32Param(ShouldSignExtI32Param
);
106 if (T
.getArch() == Triple::r600
||
107 T
.getArch() == Triple::amdgcn
) {
108 TLI
.setUnavailable(LibFunc_ldexp
);
109 TLI
.setUnavailable(LibFunc_ldexpf
);
110 TLI
.setUnavailable(LibFunc_ldexpl
);
111 TLI
.setUnavailable(LibFunc_exp10
);
112 TLI
.setUnavailable(LibFunc_exp10f
);
113 TLI
.setUnavailable(LibFunc_exp10l
);
114 TLI
.setUnavailable(LibFunc_log10
);
115 TLI
.setUnavailable(LibFunc_log10f
);
116 TLI
.setUnavailable(LibFunc_log10l
);
117 TLI
.setUnavailable(LibFunc_printf
);
120 // There are no library implementations of memcpy and memset for AMD gpus and
121 // these can be difficult to lower in the backend.
122 if (T
.getArch() == Triple::r600
||
123 T
.getArch() == Triple::amdgcn
) {
124 TLI
.setUnavailable(LibFunc_memcpy
);
125 TLI
.setUnavailable(LibFunc_memset
);
126 TLI
.setUnavailable(LibFunc_memset_pattern16
);
130 // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
131 // All versions of watchOS support it.
133 // available IO unlocked variants on Mac OS X
134 TLI
.setAvailable(LibFunc_getc_unlocked
);
135 TLI
.setAvailable(LibFunc_getchar_unlocked
);
136 TLI
.setAvailable(LibFunc_putc_unlocked
);
137 TLI
.setAvailable(LibFunc_putchar_unlocked
);
139 if (T
.isMacOSXVersionLT(10, 5))
140 TLI
.setUnavailable(LibFunc_memset_pattern16
);
141 } else if (T
.isiOS()) {
142 if (T
.isOSVersionLT(3, 0))
143 TLI
.setUnavailable(LibFunc_memset_pattern16
);
144 } else if (!T
.isWatchOS()) {
145 TLI
.setUnavailable(LibFunc_memset_pattern16
);
148 if (!hasSinCosPiStret(T
)) {
149 TLI
.setUnavailable(LibFunc_sinpi
);
150 TLI
.setUnavailable(LibFunc_sinpif
);
151 TLI
.setUnavailable(LibFunc_cospi
);
152 TLI
.setUnavailable(LibFunc_cospif
);
153 TLI
.setUnavailable(LibFunc_sincospi_stret
);
154 TLI
.setUnavailable(LibFunc_sincospif_stret
);
158 TLI
.setUnavailable(LibFunc_bcmp
);
160 if (T
.isMacOSX() && T
.getArch() == Triple::x86
&&
161 !T
.isMacOSXVersionLT(10, 7)) {
162 // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
163 // we don't care about) have two versions; on recent OSX, the one we want
164 // has a $UNIX2003 suffix. The two implementations are identical except
165 // for the return value in some edge cases. However, we don't want to
166 // generate code that depends on the old symbols.
167 TLI
.setAvailableWithName(LibFunc_fwrite
, "fwrite$UNIX2003");
168 TLI
.setAvailableWithName(LibFunc_fputs
, "fputs$UNIX2003");
171 // iprintf and friends are only available on XCore, TCE, and Emscripten.
172 if (T
.getArch() != Triple::xcore
&& T
.getArch() != Triple::tce
&&
173 T
.getOS() != Triple::Emscripten
) {
174 TLI
.setUnavailable(LibFunc_iprintf
);
175 TLI
.setUnavailable(LibFunc_siprintf
);
176 TLI
.setUnavailable(LibFunc_fiprintf
);
179 // __small_printf and friends are only available on Emscripten.
180 if (T
.getOS() != Triple::Emscripten
) {
181 TLI
.setUnavailable(LibFunc_small_printf
);
182 TLI
.setUnavailable(LibFunc_small_sprintf
);
183 TLI
.setUnavailable(LibFunc_small_fprintf
);
186 if (T
.isOSWindows() && !T
.isOSCygMing()) {
187 // XXX: The earliest documentation available at the moment is for VS2015/VC19:
188 // https://docs.microsoft.com/en-us/cpp/c-runtime-library/floating-point-support?view=vs-2015
189 // XXX: In order to use an MSVCRT older than VC19,
190 // the specific library version must be explicit in the target triple,
191 // e.g., x86_64-pc-windows-msvc18.
192 bool hasPartialC99
= true;
193 if (T
.isKnownWindowsMSVCEnvironment()) {
194 unsigned Major
, Minor
, Micro
;
195 T
.getEnvironmentVersion(Major
, Minor
, Micro
);
196 hasPartialC99
= (Major
== 0 || Major
>= 19);
199 // Latest targets support C89 math functions, in part.
200 bool isARM
= (T
.getArch() == Triple::aarch64
||
201 T
.getArch() == Triple::arm
);
202 bool hasPartialFloat
= (isARM
||
203 T
.getArch() == Triple::x86_64
);
205 // Win32 does not support float C89 math functions, in general.
206 if (!hasPartialFloat
) {
207 TLI
.setUnavailable(LibFunc_acosf
);
208 TLI
.setUnavailable(LibFunc_asinf
);
209 TLI
.setUnavailable(LibFunc_atan2f
);
210 TLI
.setUnavailable(LibFunc_atanf
);
211 TLI
.setUnavailable(LibFunc_ceilf
);
212 TLI
.setUnavailable(LibFunc_cosf
);
213 TLI
.setUnavailable(LibFunc_coshf
);
214 TLI
.setUnavailable(LibFunc_expf
);
215 TLI
.setUnavailable(LibFunc_floorf
);
216 TLI
.setUnavailable(LibFunc_fmodf
);
217 TLI
.setUnavailable(LibFunc_log10f
);
218 TLI
.setUnavailable(LibFunc_logf
);
219 TLI
.setUnavailable(LibFunc_modff
);
220 TLI
.setUnavailable(LibFunc_powf
);
221 TLI
.setUnavailable(LibFunc_sinf
);
222 TLI
.setUnavailable(LibFunc_sinhf
);
223 TLI
.setUnavailable(LibFunc_sqrtf
);
224 TLI
.setUnavailable(LibFunc_tanf
);
225 TLI
.setUnavailable(LibFunc_tanhf
);
228 TLI
.setUnavailable(LibFunc_fabsf
);
229 TLI
.setUnavailable(LibFunc_frexpf
);
230 TLI
.setUnavailable(LibFunc_ldexpf
);
232 // Win32 does not support long double C89 math functions.
233 TLI
.setUnavailable(LibFunc_acosl
);
234 TLI
.setUnavailable(LibFunc_asinl
);
235 TLI
.setUnavailable(LibFunc_atan2l
);
236 TLI
.setUnavailable(LibFunc_atanl
);
237 TLI
.setUnavailable(LibFunc_ceill
);
238 TLI
.setUnavailable(LibFunc_cosl
);
239 TLI
.setUnavailable(LibFunc_coshl
);
240 TLI
.setUnavailable(LibFunc_expl
);
241 TLI
.setUnavailable(LibFunc_fabsl
);
242 TLI
.setUnavailable(LibFunc_floorl
);
243 TLI
.setUnavailable(LibFunc_fmodl
);
244 TLI
.setUnavailable(LibFunc_frexpl
);
245 TLI
.setUnavailable(LibFunc_ldexpl
);
246 TLI
.setUnavailable(LibFunc_log10l
);
247 TLI
.setUnavailable(LibFunc_logl
);
248 TLI
.setUnavailable(LibFunc_modfl
);
249 TLI
.setUnavailable(LibFunc_powl
);
250 TLI
.setUnavailable(LibFunc_sinl
);
251 TLI
.setUnavailable(LibFunc_sinhl
);
252 TLI
.setUnavailable(LibFunc_sqrtl
);
253 TLI
.setUnavailable(LibFunc_tanl
);
254 TLI
.setUnavailable(LibFunc_tanhl
);
256 // Win32 does not fully support C99 math functions.
257 if (!hasPartialC99
) {
258 TLI
.setUnavailable(LibFunc_acosh
);
259 TLI
.setUnavailable(LibFunc_acoshf
);
260 TLI
.setUnavailable(LibFunc_asinh
);
261 TLI
.setUnavailable(LibFunc_asinhf
);
262 TLI
.setUnavailable(LibFunc_atanh
);
263 TLI
.setUnavailable(LibFunc_atanhf
);
264 TLI
.setAvailableWithName(LibFunc_cabs
, "_cabs");
265 TLI
.setUnavailable(LibFunc_cabsf
);
266 TLI
.setUnavailable(LibFunc_cbrt
);
267 TLI
.setUnavailable(LibFunc_cbrtf
);
268 TLI
.setAvailableWithName(LibFunc_copysign
, "_copysign");
269 TLI
.setAvailableWithName(LibFunc_copysignf
, "_copysignf");
270 TLI
.setUnavailable(LibFunc_exp2
);
271 TLI
.setUnavailable(LibFunc_exp2f
);
272 TLI
.setUnavailable(LibFunc_expm1
);
273 TLI
.setUnavailable(LibFunc_expm1f
);
274 TLI
.setUnavailable(LibFunc_fmax
);
275 TLI
.setUnavailable(LibFunc_fmaxf
);
276 TLI
.setUnavailable(LibFunc_fmin
);
277 TLI
.setUnavailable(LibFunc_fminf
);
278 TLI
.setUnavailable(LibFunc_log1p
);
279 TLI
.setUnavailable(LibFunc_log1pf
);
280 TLI
.setUnavailable(LibFunc_log2
);
281 TLI
.setUnavailable(LibFunc_log2f
);
282 TLI
.setAvailableWithName(LibFunc_logb
, "_logb");
284 TLI
.setAvailableWithName(LibFunc_logbf
, "_logbf");
286 TLI
.setUnavailable(LibFunc_logbf
);
287 TLI
.setUnavailable(LibFunc_rint
);
288 TLI
.setUnavailable(LibFunc_rintf
);
289 TLI
.setUnavailable(LibFunc_round
);
290 TLI
.setUnavailable(LibFunc_roundf
);
291 TLI
.setUnavailable(LibFunc_trunc
);
292 TLI
.setUnavailable(LibFunc_truncf
);
295 // Win32 does not support long double C99 math functions.
296 TLI
.setUnavailable(LibFunc_acoshl
);
297 TLI
.setUnavailable(LibFunc_asinhl
);
298 TLI
.setUnavailable(LibFunc_atanhl
);
299 TLI
.setUnavailable(LibFunc_cabsl
);
300 TLI
.setUnavailable(LibFunc_cbrtl
);
301 TLI
.setUnavailable(LibFunc_copysignl
);
302 TLI
.setUnavailable(LibFunc_exp2l
);
303 TLI
.setUnavailable(LibFunc_expm1l
);
304 TLI
.setUnavailable(LibFunc_fmaxl
);
305 TLI
.setUnavailable(LibFunc_fminl
);
306 TLI
.setUnavailable(LibFunc_log1pl
);
307 TLI
.setUnavailable(LibFunc_log2l
);
308 TLI
.setUnavailable(LibFunc_logbl
);
309 TLI
.setUnavailable(LibFunc_nearbyintl
);
310 TLI
.setUnavailable(LibFunc_rintl
);
311 TLI
.setUnavailable(LibFunc_roundl
);
312 TLI
.setUnavailable(LibFunc_truncl
);
314 // Win32 does not support these functions, but
315 // they are generally available on POSIX-compliant systems.
316 TLI
.setUnavailable(LibFunc_access
);
317 TLI
.setUnavailable(LibFunc_bcmp
);
318 TLI
.setUnavailable(LibFunc_bcopy
);
319 TLI
.setUnavailable(LibFunc_bzero
);
320 TLI
.setUnavailable(LibFunc_chmod
);
321 TLI
.setUnavailable(LibFunc_chown
);
322 TLI
.setUnavailable(LibFunc_closedir
);
323 TLI
.setUnavailable(LibFunc_ctermid
);
324 TLI
.setUnavailable(LibFunc_fdopen
);
325 TLI
.setUnavailable(LibFunc_ffs
);
326 TLI
.setUnavailable(LibFunc_fileno
);
327 TLI
.setUnavailable(LibFunc_flockfile
);
328 TLI
.setUnavailable(LibFunc_fseeko
);
329 TLI
.setUnavailable(LibFunc_fstat
);
330 TLI
.setUnavailable(LibFunc_fstatvfs
);
331 TLI
.setUnavailable(LibFunc_ftello
);
332 TLI
.setUnavailable(LibFunc_ftrylockfile
);
333 TLI
.setUnavailable(LibFunc_funlockfile
);
334 TLI
.setUnavailable(LibFunc_getitimer
);
335 TLI
.setUnavailable(LibFunc_getlogin_r
);
336 TLI
.setUnavailable(LibFunc_getpwnam
);
337 TLI
.setUnavailable(LibFunc_gettimeofday
);
338 TLI
.setUnavailable(LibFunc_htonl
);
339 TLI
.setUnavailable(LibFunc_htons
);
340 TLI
.setUnavailable(LibFunc_lchown
);
341 TLI
.setUnavailable(LibFunc_lstat
);
342 TLI
.setUnavailable(LibFunc_memccpy
);
343 TLI
.setUnavailable(LibFunc_mkdir
);
344 TLI
.setUnavailable(LibFunc_ntohl
);
345 TLI
.setUnavailable(LibFunc_ntohs
);
346 TLI
.setUnavailable(LibFunc_open
);
347 TLI
.setUnavailable(LibFunc_opendir
);
348 TLI
.setUnavailable(LibFunc_pclose
);
349 TLI
.setUnavailable(LibFunc_popen
);
350 TLI
.setUnavailable(LibFunc_pread
);
351 TLI
.setUnavailable(LibFunc_pwrite
);
352 TLI
.setUnavailable(LibFunc_read
);
353 TLI
.setUnavailable(LibFunc_readlink
);
354 TLI
.setUnavailable(LibFunc_realpath
);
355 TLI
.setUnavailable(LibFunc_rmdir
);
356 TLI
.setUnavailable(LibFunc_setitimer
);
357 TLI
.setUnavailable(LibFunc_stat
);
358 TLI
.setUnavailable(LibFunc_statvfs
);
359 TLI
.setUnavailable(LibFunc_stpcpy
);
360 TLI
.setUnavailable(LibFunc_stpncpy
);
361 TLI
.setUnavailable(LibFunc_strcasecmp
);
362 TLI
.setUnavailable(LibFunc_strncasecmp
);
363 TLI
.setUnavailable(LibFunc_times
);
364 TLI
.setUnavailable(LibFunc_uname
);
365 TLI
.setUnavailable(LibFunc_unlink
);
366 TLI
.setUnavailable(LibFunc_unsetenv
);
367 TLI
.setUnavailable(LibFunc_utime
);
368 TLI
.setUnavailable(LibFunc_utimes
);
369 TLI
.setUnavailable(LibFunc_write
);
374 // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
375 // and their names are __exp10 and __exp10f. exp10l is not available on
377 TLI
.setUnavailable(LibFunc_exp10l
);
378 if (T
.isMacOSXVersionLT(10, 9)) {
379 TLI
.setUnavailable(LibFunc_exp10
);
380 TLI
.setUnavailable(LibFunc_exp10f
);
382 TLI
.setAvailableWithName(LibFunc_exp10
, "__exp10");
383 TLI
.setAvailableWithName(LibFunc_exp10f
, "__exp10f");
388 case Triple::WatchOS
:
389 TLI
.setUnavailable(LibFunc_exp10l
);
390 if (!T
.isWatchOS() && (T
.isOSVersionLT(7, 0) ||
391 (T
.isOSVersionLT(9, 0) &&
392 (T
.getArch() == Triple::x86
||
393 T
.getArch() == Triple::x86_64
)))) {
394 TLI
.setUnavailable(LibFunc_exp10
);
395 TLI
.setUnavailable(LibFunc_exp10f
);
397 TLI
.setAvailableWithName(LibFunc_exp10
, "__exp10");
398 TLI
.setAvailableWithName(LibFunc_exp10f
, "__exp10f");
402 // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
403 // buggy prior to glibc version 2.18. Until this version is widely deployed
404 // or we have a reasonable detection strategy, we cannot use exp10 reliably
407 // Fall through to disable all of them.
410 TLI
.setUnavailable(LibFunc_exp10
);
411 TLI
.setUnavailable(LibFunc_exp10f
);
412 TLI
.setUnavailable(LibFunc_exp10l
);
415 // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
417 // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
418 // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
419 // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
425 case Triple::WatchOS
:
426 case Triple::FreeBSD
:
430 TLI
.setUnavailable(LibFunc_ffsl
);
433 // ffsll is available on at least FreeBSD and Linux (GLIBC):
434 // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
435 // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
441 case Triple::WatchOS
:
442 case Triple::FreeBSD
:
446 TLI
.setUnavailable(LibFunc_ffsll
);
449 // The following functions are available on at least FreeBSD:
450 // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
451 // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
452 // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
453 if (!T
.isOSFreeBSD()) {
454 TLI
.setUnavailable(LibFunc_fls
);
455 TLI
.setUnavailable(LibFunc_flsl
);
456 TLI
.setUnavailable(LibFunc_flsll
);
459 // The following functions are only available on GNU/Linux (using glibc).
460 // Linux variants without glibc (eg: bionic, musl) may have some subset.
461 if (!T
.isOSLinux() || !T
.isGNUEnvironment()) {
462 TLI
.setUnavailable(LibFunc_dunder_strdup
);
463 TLI
.setUnavailable(LibFunc_dunder_strtok_r
);
464 TLI
.setUnavailable(LibFunc_dunder_isoc99_scanf
);
465 TLI
.setUnavailable(LibFunc_dunder_isoc99_sscanf
);
466 TLI
.setUnavailable(LibFunc_under_IO_getc
);
467 TLI
.setUnavailable(LibFunc_under_IO_putc
);
468 // But, Android and musl have memalign.
469 if (!T
.isAndroid() && !T
.isMusl())
470 TLI
.setUnavailable(LibFunc_memalign
);
471 TLI
.setUnavailable(LibFunc_fopen64
);
472 TLI
.setUnavailable(LibFunc_fseeko64
);
473 TLI
.setUnavailable(LibFunc_fstat64
);
474 TLI
.setUnavailable(LibFunc_fstatvfs64
);
475 TLI
.setUnavailable(LibFunc_ftello64
);
476 TLI
.setUnavailable(LibFunc_lstat64
);
477 TLI
.setUnavailable(LibFunc_open64
);
478 TLI
.setUnavailable(LibFunc_stat64
);
479 TLI
.setUnavailable(LibFunc_statvfs64
);
480 TLI
.setUnavailable(LibFunc_tmpfile64
);
482 // Relaxed math functions are included in math-finite.h on Linux (GLIBC).
483 TLI
.setUnavailable(LibFunc_acos_finite
);
484 TLI
.setUnavailable(LibFunc_acosf_finite
);
485 TLI
.setUnavailable(LibFunc_acosl_finite
);
486 TLI
.setUnavailable(LibFunc_acosh_finite
);
487 TLI
.setUnavailable(LibFunc_acoshf_finite
);
488 TLI
.setUnavailable(LibFunc_acoshl_finite
);
489 TLI
.setUnavailable(LibFunc_asin_finite
);
490 TLI
.setUnavailable(LibFunc_asinf_finite
);
491 TLI
.setUnavailable(LibFunc_asinl_finite
);
492 TLI
.setUnavailable(LibFunc_atan2_finite
);
493 TLI
.setUnavailable(LibFunc_atan2f_finite
);
494 TLI
.setUnavailable(LibFunc_atan2l_finite
);
495 TLI
.setUnavailable(LibFunc_atanh_finite
);
496 TLI
.setUnavailable(LibFunc_atanhf_finite
);
497 TLI
.setUnavailable(LibFunc_atanhl_finite
);
498 TLI
.setUnavailable(LibFunc_cosh_finite
);
499 TLI
.setUnavailable(LibFunc_coshf_finite
);
500 TLI
.setUnavailable(LibFunc_coshl_finite
);
501 TLI
.setUnavailable(LibFunc_exp10_finite
);
502 TLI
.setUnavailable(LibFunc_exp10f_finite
);
503 TLI
.setUnavailable(LibFunc_exp10l_finite
);
504 TLI
.setUnavailable(LibFunc_exp2_finite
);
505 TLI
.setUnavailable(LibFunc_exp2f_finite
);
506 TLI
.setUnavailable(LibFunc_exp2l_finite
);
507 TLI
.setUnavailable(LibFunc_exp_finite
);
508 TLI
.setUnavailable(LibFunc_expf_finite
);
509 TLI
.setUnavailable(LibFunc_expl_finite
);
510 TLI
.setUnavailable(LibFunc_log10_finite
);
511 TLI
.setUnavailable(LibFunc_log10f_finite
);
512 TLI
.setUnavailable(LibFunc_log10l_finite
);
513 TLI
.setUnavailable(LibFunc_log2_finite
);
514 TLI
.setUnavailable(LibFunc_log2f_finite
);
515 TLI
.setUnavailable(LibFunc_log2l_finite
);
516 TLI
.setUnavailable(LibFunc_log_finite
);
517 TLI
.setUnavailable(LibFunc_logf_finite
);
518 TLI
.setUnavailable(LibFunc_logl_finite
);
519 TLI
.setUnavailable(LibFunc_pow_finite
);
520 TLI
.setUnavailable(LibFunc_powf_finite
);
521 TLI
.setUnavailable(LibFunc_powl_finite
);
522 TLI
.setUnavailable(LibFunc_sinh_finite
);
523 TLI
.setUnavailable(LibFunc_sinhf_finite
);
524 TLI
.setUnavailable(LibFunc_sinhl_finite
);
527 if ((T
.isOSLinux() && T
.isGNUEnvironment()) ||
528 (T
.isAndroid() && !T
.isAndroidVersionLT(28))) {
529 // available IO unlocked variants on GNU/Linux and Android P or later
530 TLI
.setAvailable(LibFunc_getc_unlocked
);
531 TLI
.setAvailable(LibFunc_getchar_unlocked
);
532 TLI
.setAvailable(LibFunc_putc_unlocked
);
533 TLI
.setAvailable(LibFunc_putchar_unlocked
);
534 TLI
.setAvailable(LibFunc_fputc_unlocked
);
535 TLI
.setAvailable(LibFunc_fgetc_unlocked
);
536 TLI
.setAvailable(LibFunc_fread_unlocked
);
537 TLI
.setAvailable(LibFunc_fwrite_unlocked
);
538 TLI
.setAvailable(LibFunc_fputs_unlocked
);
539 TLI
.setAvailable(LibFunc_fgets_unlocked
);
542 // As currently implemented in clang, NVPTX code has no standard library to
543 // speak of. Headers provide a standard-ish library implementation, but many
544 // of the signatures are wrong -- for example, many libm functions are not
547 // libdevice, an IR library provided by nvidia, is linked in by the front-end,
548 // but only used functions are provided to llvm. Moreover, most of the
549 // functions in libdevice don't map precisely to standard library functions.
551 // FIXME: Having no standard library prevents e.g. many fastmath
552 // optimizations, so this situation should be fixed.
554 TLI
.disableAllFunctions();
555 TLI
.setAvailable(LibFunc_nvvm_reflect
);
557 TLI
.setUnavailable(LibFunc_nvvm_reflect
);
560 TLI
.addVectorizableFunctionsFromVecLib(ClVectorLibrary
);
563 TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
564 // Default to everything being available.
565 memset(AvailableArray
, -1, sizeof(AvailableArray
));
567 initialize(*this, Triple(), StandardNames
);
570 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple
&T
) {
571 // Default to everything being available.
572 memset(AvailableArray
, -1, sizeof(AvailableArray
));
574 initialize(*this, T
, StandardNames
);
577 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl
&TLI
)
578 : CustomNames(TLI
.CustomNames
), ShouldExtI32Param(TLI
.ShouldExtI32Param
),
579 ShouldExtI32Return(TLI
.ShouldExtI32Return
),
580 ShouldSignExtI32Param(TLI
.ShouldSignExtI32Param
) {
581 memcpy(AvailableArray
, TLI
.AvailableArray
, sizeof(AvailableArray
));
582 VectorDescs
= TLI
.VectorDescs
;
583 ScalarDescs
= TLI
.ScalarDescs
;
586 TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl
&&TLI
)
587 : CustomNames(std::move(TLI
.CustomNames
)),
588 ShouldExtI32Param(TLI
.ShouldExtI32Param
),
589 ShouldExtI32Return(TLI
.ShouldExtI32Return
),
590 ShouldSignExtI32Param(TLI
.ShouldSignExtI32Param
) {
591 std::move(std::begin(TLI
.AvailableArray
), std::end(TLI
.AvailableArray
),
593 VectorDescs
= TLI
.VectorDescs
;
594 ScalarDescs
= TLI
.ScalarDescs
;
597 TargetLibraryInfoImpl
&TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl
&TLI
) {
598 CustomNames
= TLI
.CustomNames
;
599 ShouldExtI32Param
= TLI
.ShouldExtI32Param
;
600 ShouldExtI32Return
= TLI
.ShouldExtI32Return
;
601 ShouldSignExtI32Param
= TLI
.ShouldSignExtI32Param
;
602 memcpy(AvailableArray
, TLI
.AvailableArray
, sizeof(AvailableArray
));
606 TargetLibraryInfoImpl
&TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl
&&TLI
) {
607 CustomNames
= std::move(TLI
.CustomNames
);
608 ShouldExtI32Param
= TLI
.ShouldExtI32Param
;
609 ShouldExtI32Return
= TLI
.ShouldExtI32Return
;
610 ShouldSignExtI32Param
= TLI
.ShouldSignExtI32Param
;
611 std::move(std::begin(TLI
.AvailableArray
), std::end(TLI
.AvailableArray
),
616 static StringRef
sanitizeFunctionName(StringRef funcName
) {
617 // Filter out empty names and names containing null bytes, those can't be in
619 if (funcName
.empty() || funcName
.find('\0') != StringRef::npos
)
622 // Check for \01 prefix that is used to mangle __asm declarations and
623 // strip it if present.
624 return GlobalValue::dropLLVMManglingEscape(funcName
);
627 bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName
,
629 StringRef
const *Start
= &StandardNames
[0];
630 StringRef
const *End
= &StandardNames
[NumLibFuncs
];
632 funcName
= sanitizeFunctionName(funcName
);
633 if (funcName
.empty())
636 StringRef
const *I
= std::lower_bound(
637 Start
, End
, funcName
, [](StringRef LHS
, StringRef RHS
) {
640 if (I
!= End
&& *I
== funcName
) {
641 F
= (LibFunc
)(I
- Start
);
647 bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType
&FTy
,
649 const DataLayout
*DL
) const {
650 LLVMContext
&Ctx
= FTy
.getContext();
651 Type
*PCharTy
= Type::getInt8PtrTy(Ctx
);
652 Type
*SizeTTy
= DL
? DL
->getIntPtrType(Ctx
, /*AS=*/0) : nullptr;
653 auto IsSizeTTy
= [SizeTTy
](Type
*Ty
) {
654 return SizeTTy
? Ty
== SizeTTy
: Ty
->isIntegerTy();
656 unsigned NumParams
= FTy
.getNumParams();
662 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
663 FTy
.getParamType(1)->isPointerTy() &&
664 FTy
.getReturnType()->isIntegerTy(32));
667 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
668 FTy
.getParamType(1)->isPointerTy() &&
669 FTy
.getReturnType()->isIntegerTy(32));
671 case LibFunc_execvpe
:
673 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy() &&
674 FTy
.getParamType(1)->isPointerTy() &&
675 FTy
.getParamType(2)->isPointerTy() &&
676 FTy
.getReturnType()->isIntegerTy(32));
678 return (NumParams
== 1 && FTy
.getParamType(0)->isPointerTy() &&
679 FTy
.getReturnType()->isIntegerTy());
682 case LibFunc_strrchr
:
683 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
684 FTy
.getParamType(0) == FTy
.getReturnType() &&
685 FTy
.getParamType(1)->isIntegerTy());
690 case LibFunc_strtoul
:
691 case LibFunc_strtoll
:
692 case LibFunc_strtold
:
693 case LibFunc_strtoull
:
694 return ((NumParams
== 2 || NumParams
== 3) &&
695 FTy
.getParamType(0)->isPointerTy() &&
696 FTy
.getParamType(1)->isPointerTy());
697 case LibFunc_strcat_chk
:
699 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
703 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
704 FTy
.getParamType(0) == FTy
.getReturnType() &&
705 FTy
.getParamType(1) == FTy
.getReturnType());
707 case LibFunc_strncat_chk
:
709 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
712 case LibFunc_strncat
:
713 return (NumParams
== 3 && FTy
.getReturnType()->isPointerTy() &&
714 FTy
.getParamType(0) == FTy
.getReturnType() &&
715 FTy
.getParamType(1) == FTy
.getReturnType() &&
716 IsSizeTTy(FTy
.getParamType(2)));
718 case LibFunc_strcpy_chk
:
719 case LibFunc_stpcpy_chk
:
721 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
726 return (NumParams
== 2 && FTy
.getReturnType() == FTy
.getParamType(0) &&
727 FTy
.getParamType(0) == FTy
.getParamType(1) &&
728 FTy
.getParamType(0) == PCharTy
);
730 case LibFunc_strlcat_chk
:
731 case LibFunc_strlcpy_chk
:
733 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
736 case LibFunc_strlcat
:
737 case LibFunc_strlcpy
:
738 return NumParams
== 3 && IsSizeTTy(FTy
.getReturnType()) &&
739 FTy
.getParamType(0)->isPointerTy() &&
740 FTy
.getParamType(1)->isPointerTy() &&
741 IsSizeTTy(FTy
.getParamType(2));
743 case LibFunc_strncpy_chk
:
744 case LibFunc_stpncpy_chk
:
746 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
749 case LibFunc_strncpy
:
750 case LibFunc_stpncpy
:
751 return (NumParams
== 3 && FTy
.getReturnType() == FTy
.getParamType(0) &&
752 FTy
.getParamType(0) == FTy
.getParamType(1) &&
753 FTy
.getParamType(0) == PCharTy
&&
754 IsSizeTTy(FTy
.getParamType(2)));
756 case LibFunc_strxfrm
:
757 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy() &&
758 FTy
.getParamType(1)->isPointerTy());
761 return (NumParams
== 2 && FTy
.getReturnType()->isIntegerTy(32) &&
762 FTy
.getParamType(0)->isPointerTy() &&
763 FTy
.getParamType(0) == FTy
.getParamType(1));
765 case LibFunc_strncmp
:
766 return (NumParams
== 3 && FTy
.getReturnType()->isIntegerTy(32) &&
767 FTy
.getParamType(0)->isPointerTy() &&
768 FTy
.getParamType(0) == FTy
.getParamType(1) &&
769 IsSizeTTy(FTy
.getParamType(2)));
772 case LibFunc_strcspn
:
773 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
774 FTy
.getParamType(0) == FTy
.getParamType(1) &&
775 FTy
.getReturnType()->isIntegerTy());
777 case LibFunc_strcoll
:
778 case LibFunc_strcasecmp
:
779 case LibFunc_strncasecmp
:
780 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
781 FTy
.getParamType(1)->isPointerTy());
784 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
785 FTy
.getParamType(0)->isPointerTy() &&
786 FTy
.getParamType(1)->isPointerTy());
788 case LibFunc_strpbrk
:
789 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
790 FTy
.getReturnType() == FTy
.getParamType(0) &&
791 FTy
.getParamType(0) == FTy
.getParamType(1));
794 case LibFunc_strtok_r
:
795 return (NumParams
>= 2 && FTy
.getParamType(1)->isPointerTy());
798 case LibFunc_setvbuf
:
799 return (NumParams
>= 1 && FTy
.getParamType(0)->isPointerTy());
801 case LibFunc_strndup
:
802 return (NumParams
>= 1 && FTy
.getReturnType()->isPointerTy() &&
803 FTy
.getParamType(0)->isPointerTy());
806 case LibFunc_statvfs
:
807 case LibFunc_siprintf
:
808 case LibFunc_small_sprintf
:
809 case LibFunc_sprintf
:
810 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
811 FTy
.getParamType(1)->isPointerTy() &&
812 FTy
.getReturnType()->isIntegerTy(32));
814 case LibFunc_sprintf_chk
:
815 return NumParams
== 4 && FTy
.getParamType(0)->isPointerTy() &&
816 FTy
.getParamType(1)->isIntegerTy(32) &&
817 IsSizeTTy(FTy
.getParamType(2)) &&
818 FTy
.getParamType(3)->isPointerTy() &&
819 FTy
.getReturnType()->isIntegerTy(32);
821 case LibFunc_snprintf
:
822 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy() &&
823 FTy
.getParamType(2)->isPointerTy() &&
824 FTy
.getReturnType()->isIntegerTy(32));
826 case LibFunc_snprintf_chk
:
827 return NumParams
== 5 && FTy
.getParamType(0)->isPointerTy() &&
828 IsSizeTTy(FTy
.getParamType(1)) &&
829 FTy
.getParamType(2)->isIntegerTy(32) &&
830 IsSizeTTy(FTy
.getParamType(3)) &&
831 FTy
.getParamType(4)->isPointerTy() &&
832 FTy
.getReturnType()->isIntegerTy(32);
834 case LibFunc_setitimer
:
835 return (NumParams
== 3 && FTy
.getParamType(1)->isPointerTy() &&
836 FTy
.getParamType(2)->isPointerTy());
838 return (NumParams
== 1 && FTy
.getParamType(0)->isPointerTy());
840 return (NumParams
== 1 && FTy
.getReturnType()->isPointerTy());
842 return (NumParams
== 3 && FTy
.getReturnType()->isIntegerTy(32) &&
843 FTy
.getParamType(0)->isPointerTy() &&
844 FTy
.getParamType(1)->isPointerTy());
847 case LibFunc_memrchr
:
848 return (NumParams
== 3 && FTy
.getReturnType()->isPointerTy() &&
849 FTy
.getReturnType() == FTy
.getParamType(0) &&
850 FTy
.getParamType(1)->isIntegerTy(32) &&
851 IsSizeTTy(FTy
.getParamType(2)));
855 return (NumParams
>= 2 && FTy
.getParamType(1)->isPointerTy());
857 case LibFunc_memcpy_chk
:
858 case LibFunc_memmove_chk
:
860 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
864 case LibFunc_mempcpy
:
865 case LibFunc_memmove
:
866 return (NumParams
== 3 && FTy
.getReturnType() == FTy
.getParamType(0) &&
867 FTy
.getParamType(0)->isPointerTy() &&
868 FTy
.getParamType(1)->isPointerTy() &&
869 IsSizeTTy(FTy
.getParamType(2)));
871 case LibFunc_memset_chk
:
873 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
877 return (NumParams
== 3 && FTy
.getReturnType() == FTy
.getParamType(0) &&
878 FTy
.getParamType(0)->isPointerTy() &&
879 FTy
.getParamType(1)->isIntegerTy() &&
880 IsSizeTTy(FTy
.getParamType(2)));
882 case LibFunc_memccpy_chk
:
884 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
887 case LibFunc_memccpy
:
888 return (NumParams
>= 2 && FTy
.getParamType(1)->isPointerTy());
889 case LibFunc_memalign
:
890 return (FTy
.getReturnType()->isPointerTy());
891 case LibFunc_realloc
:
892 case LibFunc_reallocf
:
893 return (NumParams
== 2 && FTy
.getReturnType() == PCharTy
&&
894 FTy
.getParamType(0) == FTy
.getReturnType() &&
895 IsSizeTTy(FTy
.getParamType(1)));
897 return (NumParams
== 3 && FTy
.getParamType(1)->isPointerTy());
901 case LibFunc_realpath
:
902 return (NumParams
>= 1 && FTy
.getParamType(0)->isPointerTy());
904 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
905 FTy
.getParamType(1)->isPointerTy());
906 case LibFunc_readlink
:
907 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
908 FTy
.getParamType(1)->isPointerTy());
910 return (NumParams
== 3 && FTy
.getParamType(1)->isPointerTy());
913 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy() &&
914 FTy
.getParamType(1)->isPointerTy());
916 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy());
918 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy());
926 case LibFunc_getpwnam
:
927 case LibFunc_iprintf
:
928 case LibFunc_small_printf
:
934 case LibFunc_under_IO_getc
:
936 case LibFunc_unsetenv
:
937 return (NumParams
== 1 && FTy
.getParamType(0)->isPointerTy());
942 case LibFunc_clearerr
:
943 case LibFunc_closedir
:
944 case LibFunc_ctermid
:
949 case LibFunc_fgetc_unlocked
:
951 case LibFunc_flockfile
:
954 case LibFunc_fseeko64
:
956 case LibFunc_fsetpos
:
958 case LibFunc_ftello64
:
960 case LibFunc_ftrylockfile
:
961 case LibFunc_funlockfile
:
963 case LibFunc_getc_unlocked
:
964 case LibFunc_getlogin_r
:
968 return (NumParams
!= 0 && FTy
.getParamType(0)->isPointerTy());
971 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
972 FTy
.getParamType(0)->isPointerTy() &&
973 FTy
.getParamType(1)->isPointerTy());
975 return (NumParams
== 0 && FTy
.getReturnType()->isIntegerTy(32));
977 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
978 FTy
.getParamType(1)->isPointerTy());
980 case LibFunc_fputc_unlocked
:
985 case LibFunc_fstatvfs
:
986 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
988 case LibFunc_fgets_unlocked
:
989 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy() &&
990 FTy
.getParamType(2)->isPointerTy());
992 case LibFunc_fread_unlocked
:
993 return (NumParams
== 4 && FTy
.getParamType(0)->isPointerTy() &&
994 FTy
.getParamType(3)->isPointerTy());
996 case LibFunc_fwrite_unlocked
:
997 return (NumParams
== 4 && FTy
.getReturnType()->isIntegerTy() &&
998 FTy
.getParamType(0)->isPointerTy() &&
999 FTy
.getParamType(1)->isIntegerTy() &&
1000 FTy
.getParamType(2)->isIntegerTy() &&
1001 FTy
.getParamType(3)->isPointerTy());
1003 case LibFunc_fputs_unlocked
:
1004 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
1005 FTy
.getParamType(1)->isPointerTy());
1006 case LibFunc_fscanf
:
1007 case LibFunc_fiprintf
:
1008 case LibFunc_small_fprintf
:
1009 case LibFunc_fprintf
:
1010 return (NumParams
>= 2 && FTy
.getReturnType()->isIntegerTy() &&
1011 FTy
.getParamType(0)->isPointerTy() &&
1012 FTy
.getParamType(1)->isPointerTy());
1013 case LibFunc_fgetpos
:
1014 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
1015 FTy
.getParamType(1)->isPointerTy());
1016 case LibFunc_getchar
:
1017 case LibFunc_getchar_unlocked
:
1018 return (NumParams
== 0 && FTy
.getReturnType()->isIntegerTy());
1020 return (NumParams
== 1 && FTy
.getParamType(0) == PCharTy
);
1021 case LibFunc_getitimer
:
1022 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
1023 case LibFunc_ungetc
:
1024 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
1026 case LibFunc_utimes
:
1027 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
1028 FTy
.getParamType(1)->isPointerTy());
1030 case LibFunc_putc_unlocked
:
1031 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
1033 case LibFunc_pwrite
:
1034 return (NumParams
== 4 && FTy
.getParamType(1)->isPointerTy());
1036 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
1037 FTy
.getParamType(0)->isPointerTy() &&
1038 FTy
.getParamType(1)->isPointerTy());
1039 case LibFunc_vscanf
:
1040 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
1041 case LibFunc_vsscanf
:
1042 return (NumParams
== 3 && FTy
.getParamType(1)->isPointerTy() &&
1043 FTy
.getParamType(2)->isPointerTy());
1044 case LibFunc_vfscanf
:
1045 return (NumParams
== 3 && FTy
.getParamType(1)->isPointerTy() &&
1046 FTy
.getParamType(2)->isPointerTy());
1047 case LibFunc_valloc
:
1048 return (FTy
.getReturnType()->isPointerTy());
1049 case LibFunc_vprintf
:
1050 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy());
1051 case LibFunc_vfprintf
:
1052 case LibFunc_vsprintf
:
1053 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy() &&
1054 FTy
.getParamType(1)->isPointerTy());
1055 case LibFunc_vsprintf_chk
:
1056 return NumParams
== 5 && FTy
.getParamType(0)->isPointerTy() &&
1057 FTy
.getParamType(1)->isIntegerTy(32) &&
1058 IsSizeTTy(FTy
.getParamType(2)) && FTy
.getParamType(3)->isPointerTy();
1059 case LibFunc_vsnprintf
:
1060 return (NumParams
== 4 && FTy
.getParamType(0)->isPointerTy() &&
1061 FTy
.getParamType(2)->isPointerTy());
1062 case LibFunc_vsnprintf_chk
:
1063 return NumParams
== 6 && FTy
.getParamType(0)->isPointerTy() &&
1064 FTy
.getParamType(2)->isIntegerTy(32) &&
1065 IsSizeTTy(FTy
.getParamType(3)) && FTy
.getParamType(4)->isPointerTy();
1067 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy());
1068 case LibFunc_opendir
:
1069 return (NumParams
== 1 && FTy
.getReturnType()->isPointerTy() &&
1070 FTy
.getParamType(0)->isPointerTy());
1071 case LibFunc_tmpfile
:
1072 return (FTy
.getReturnType()->isPointerTy());
1075 return (NumParams
== 1 && FTy
.getReturnType()->isIntegerTy(32) &&
1076 FTy
.getReturnType() == FTy
.getParamType(0));
1079 return (NumParams
== 1 && FTy
.getReturnType()->isIntegerTy(16) &&
1080 FTy
.getReturnType() == FTy
.getParamType(0));
1082 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
1083 FTy
.getParamType(1)->isPointerTy());
1084 case LibFunc_lchown
:
1085 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy());
1087 return (NumParams
== 4 && FTy
.getParamType(3)->isPointerTy());
1088 case LibFunc_dunder_strdup
:
1089 case LibFunc_dunder_strndup
:
1090 return (NumParams
>= 1 && FTy
.getReturnType()->isPointerTy() &&
1091 FTy
.getParamType(0)->isPointerTy());
1092 case LibFunc_dunder_strtok_r
:
1093 return (NumParams
== 3 && FTy
.getParamType(1)->isPointerTy());
1094 case LibFunc_under_IO_putc
:
1095 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
1096 case LibFunc_dunder_isoc99_scanf
:
1097 return (NumParams
>= 1 && FTy
.getParamType(0)->isPointerTy());
1098 case LibFunc_stat64
:
1099 case LibFunc_lstat64
:
1100 case LibFunc_statvfs64
:
1101 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
1102 FTy
.getParamType(1)->isPointerTy());
1103 case LibFunc_dunder_isoc99_sscanf
:
1104 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
1105 FTy
.getParamType(1)->isPointerTy());
1106 case LibFunc_fopen64
:
1107 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
1108 FTy
.getParamType(0)->isPointerTy() &&
1109 FTy
.getParamType(1)->isPointerTy());
1110 case LibFunc_tmpfile64
:
1111 return (FTy
.getReturnType()->isPointerTy());
1112 case LibFunc_fstat64
:
1113 case LibFunc_fstatvfs64
:
1114 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
1115 case LibFunc_open64
:
1116 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy());
1117 case LibFunc_gettimeofday
:
1118 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
1119 FTy
.getParamType(1)->isPointerTy());
1121 // new(unsigned int);
1123 // new(unsigned long);
1125 // new[](unsigned int);
1127 // new[](unsigned long);
1129 // new(unsigned int);
1130 case LibFunc_msvc_new_int
:
1131 // new(unsigned long long);
1132 case LibFunc_msvc_new_longlong
:
1133 // new[](unsigned int);
1134 case LibFunc_msvc_new_array_int
:
1135 // new[](unsigned long long);
1136 case LibFunc_msvc_new_array_longlong
:
1137 return (NumParams
== 1 && FTy
.getReturnType()->isPointerTy());
1139 // new(unsigned int, nothrow);
1140 case LibFunc_ZnwjRKSt9nothrow_t
:
1141 // new(unsigned long, nothrow);
1142 case LibFunc_ZnwmRKSt9nothrow_t
:
1143 // new[](unsigned int, nothrow);
1144 case LibFunc_ZnajRKSt9nothrow_t
:
1145 // new[](unsigned long, nothrow);
1146 case LibFunc_ZnamRKSt9nothrow_t
:
1147 // new(unsigned int, nothrow);
1148 case LibFunc_msvc_new_int_nothrow
:
1149 // new(unsigned long long, nothrow);
1150 case LibFunc_msvc_new_longlong_nothrow
:
1151 // new[](unsigned int, nothrow);
1152 case LibFunc_msvc_new_array_int_nothrow
:
1153 // new[](unsigned long long, nothrow);
1154 case LibFunc_msvc_new_array_longlong_nothrow
:
1155 // new(unsigned int, align_val_t)
1156 case LibFunc_ZnwjSt11align_val_t
:
1157 // new(unsigned long, align_val_t)
1158 case LibFunc_ZnwmSt11align_val_t
:
1159 // new[](unsigned int, align_val_t)
1160 case LibFunc_ZnajSt11align_val_t
:
1161 // new[](unsigned long, align_val_t)
1162 case LibFunc_ZnamSt11align_val_t
:
1163 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy());
1165 // new(unsigned int, align_val_t, nothrow)
1166 case LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t
:
1167 // new(unsigned long, align_val_t, nothrow)
1168 case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t
:
1169 // new[](unsigned int, align_val_t, nothrow)
1170 case LibFunc_ZnajSt11align_val_tRKSt9nothrow_t
:
1171 // new[](unsigned long, align_val_t, nothrow)
1172 case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t
:
1173 return (NumParams
== 3 && FTy
.getReturnType()->isPointerTy());
1175 // void operator delete[](void*);
1177 // void operator delete(void*);
1179 // void operator delete[](void*);
1180 case LibFunc_msvc_delete_array_ptr32
:
1181 // void operator delete[](void*);
1182 case LibFunc_msvc_delete_array_ptr64
:
1183 // void operator delete(void*);
1184 case LibFunc_msvc_delete_ptr32
:
1185 // void operator delete(void*);
1186 case LibFunc_msvc_delete_ptr64
:
1187 return (NumParams
== 1 && FTy
.getParamType(0)->isPointerTy());
1189 // void operator delete[](void*, nothrow);
1190 case LibFunc_ZdaPvRKSt9nothrow_t
:
1191 // void operator delete[](void*, unsigned int);
1192 case LibFunc_ZdaPvj
:
1193 // void operator delete[](void*, unsigned long);
1194 case LibFunc_ZdaPvm
:
1195 // void operator delete(void*, nothrow);
1196 case LibFunc_ZdlPvRKSt9nothrow_t
:
1197 // void operator delete(void*, unsigned int);
1198 case LibFunc_ZdlPvj
:
1199 // void operator delete(void*, unsigned long);
1200 case LibFunc_ZdlPvm
:
1201 // void operator delete(void*, align_val_t)
1202 case LibFunc_ZdlPvSt11align_val_t
:
1203 // void operator delete[](void*, align_val_t)
1204 case LibFunc_ZdaPvSt11align_val_t
:
1205 // void operator delete[](void*, unsigned int);
1206 case LibFunc_msvc_delete_array_ptr32_int
:
1207 // void operator delete[](void*, nothrow);
1208 case LibFunc_msvc_delete_array_ptr32_nothrow
:
1209 // void operator delete[](void*, unsigned long long);
1210 case LibFunc_msvc_delete_array_ptr64_longlong
:
1211 // void operator delete[](void*, nothrow);
1212 case LibFunc_msvc_delete_array_ptr64_nothrow
:
1213 // void operator delete(void*, unsigned int);
1214 case LibFunc_msvc_delete_ptr32_int
:
1215 // void operator delete(void*, nothrow);
1216 case LibFunc_msvc_delete_ptr32_nothrow
:
1217 // void operator delete(void*, unsigned long long);
1218 case LibFunc_msvc_delete_ptr64_longlong
:
1219 // void operator delete(void*, nothrow);
1220 case LibFunc_msvc_delete_ptr64_nothrow
:
1221 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy());
1223 // void operator delete(void*, align_val_t, nothrow)
1224 case LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t
:
1225 // void operator delete[](void*, align_val_t, nothrow)
1226 case LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t
:
1227 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy());
1229 case LibFunc_memset_pattern16
:
1230 return (!FTy
.isVarArg() && NumParams
== 3 &&
1231 FTy
.getParamType(0)->isPointerTy() &&
1232 FTy
.getParamType(1)->isPointerTy() &&
1233 FTy
.getParamType(2)->isIntegerTy());
1235 case LibFunc_cxa_guard_abort
:
1236 case LibFunc_cxa_guard_acquire
:
1237 case LibFunc_cxa_guard_release
:
1238 case LibFunc_nvvm_reflect
:
1239 return (NumParams
== 1 && FTy
.getParamType(0)->isPointerTy());
1241 case LibFunc_sincospi_stret
:
1242 case LibFunc_sincospif_stret
:
1243 return (NumParams
== 1 && FTy
.getParamType(0)->isFloatingPointTy());
1246 case LibFunc_acos_finite
:
1248 case LibFunc_acosf_finite
:
1250 case LibFunc_acosh_finite
:
1251 case LibFunc_acoshf
:
1252 case LibFunc_acoshf_finite
:
1253 case LibFunc_acoshl
:
1254 case LibFunc_acoshl_finite
:
1256 case LibFunc_acosl_finite
:
1258 case LibFunc_asin_finite
:
1260 case LibFunc_asinf_finite
:
1262 case LibFunc_asinhf
:
1263 case LibFunc_asinhl
:
1265 case LibFunc_asinl_finite
:
1269 case LibFunc_atanh_finite
:
1270 case LibFunc_atanhf
:
1271 case LibFunc_atanhf_finite
:
1272 case LibFunc_atanhl
:
1273 case LibFunc_atanhl_finite
:
1284 case LibFunc_cosh_finite
:
1286 case LibFunc_coshf_finite
:
1288 case LibFunc_coshl_finite
:
1291 case LibFunc_exp10_finite
:
1292 case LibFunc_exp10f
:
1293 case LibFunc_exp10f_finite
:
1294 case LibFunc_exp10l
:
1295 case LibFunc_exp10l_finite
:
1297 case LibFunc_exp2_finite
:
1299 case LibFunc_exp2f_finite
:
1301 case LibFunc_exp2l_finite
:
1303 case LibFunc_exp_finite
:
1305 case LibFunc_expf_finite
:
1307 case LibFunc_expl_finite
:
1309 case LibFunc_expm1f
:
1310 case LibFunc_expm1l
:
1315 case LibFunc_floorf
:
1316 case LibFunc_floorl
:
1318 case LibFunc_log10_finite
:
1319 case LibFunc_log10f
:
1320 case LibFunc_log10f_finite
:
1321 case LibFunc_log10l
:
1322 case LibFunc_log10l_finite
:
1324 case LibFunc_log1pf
:
1325 case LibFunc_log1pl
:
1327 case LibFunc_log2_finite
:
1329 case LibFunc_log2f_finite
:
1331 case LibFunc_log2l_finite
:
1333 case LibFunc_log_finite
:
1338 case LibFunc_logf_finite
:
1340 case LibFunc_logl_finite
:
1341 case LibFunc_nearbyint
:
1342 case LibFunc_nearbyintf
:
1343 case LibFunc_nearbyintl
:
1348 case LibFunc_roundf
:
1349 case LibFunc_roundl
:
1353 case LibFunc_sinh_finite
:
1355 case LibFunc_sinhf_finite
:
1357 case LibFunc_sinhl_finite
:
1360 case LibFunc_sqrt_finite
:
1362 case LibFunc_sqrtf_finite
:
1364 case LibFunc_sqrtl_finite
:
1372 case LibFunc_truncf
:
1373 case LibFunc_truncl
:
1374 return (NumParams
== 1 && FTy
.getReturnType()->isFloatingPointTy() &&
1375 FTy
.getReturnType() == FTy
.getParamType(0));
1378 case LibFunc_atan2_finite
:
1379 case LibFunc_atan2f
:
1380 case LibFunc_atan2f_finite
:
1381 case LibFunc_atan2l
:
1382 case LibFunc_atan2l_finite
:
1392 case LibFunc_copysign
:
1393 case LibFunc_copysignf
:
1394 case LibFunc_copysignl
:
1396 case LibFunc_pow_finite
:
1398 case LibFunc_powf_finite
:
1400 case LibFunc_powl_finite
:
1401 return (NumParams
== 2 && FTy
.getReturnType()->isFloatingPointTy() &&
1402 FTy
.getReturnType() == FTy
.getParamType(0) &&
1403 FTy
.getReturnType() == FTy
.getParamType(1));
1406 case LibFunc_ldexpf
:
1407 case LibFunc_ldexpl
:
1408 return (NumParams
== 2 && FTy
.getReturnType()->isFloatingPointTy() &&
1409 FTy
.getReturnType() == FTy
.getParamType(0) &&
1410 FTy
.getParamType(1)->isIntegerTy(32));
1418 return (NumParams
== 1 && FTy
.getReturnType()->isIntegerTy(32) &&
1419 FTy
.getParamType(0)->isIntegerTy());
1421 case LibFunc_isdigit
:
1422 case LibFunc_isascii
:
1423 case LibFunc_toascii
:
1424 case LibFunc_putchar
:
1425 case LibFunc_putchar_unlocked
:
1426 return (NumParams
== 1 && FTy
.getReturnType()->isIntegerTy(32) &&
1427 FTy
.getReturnType() == FTy
.getParamType(0));
1432 return (NumParams
== 1 && FTy
.getReturnType()->isIntegerTy() &&
1433 FTy
.getReturnType() == FTy
.getParamType(0));
1435 case LibFunc_cxa_atexit
:
1436 return (NumParams
== 3 && FTy
.getReturnType()->isIntegerTy() &&
1437 FTy
.getParamType(0)->isPointerTy() &&
1438 FTy
.getParamType(1)->isPointerTy() &&
1439 FTy
.getParamType(2)->isPointerTy());
1443 return (NumParams
== 1 && FTy
.getReturnType()->isDoubleTy() &&
1444 FTy
.getReturnType() == FTy
.getParamType(0));
1446 case LibFunc_sinpif
:
1447 case LibFunc_cospif
:
1448 return (NumParams
== 1 && FTy
.getReturnType()->isFloatTy() &&
1449 FTy
.getReturnType() == FTy
.getParamType(0));
1451 case LibFunc_strnlen
:
1452 return (NumParams
== 2 && FTy
.getReturnType() == FTy
.getParamType(1) &&
1453 FTy
.getParamType(0) == PCharTy
&&
1454 FTy
.getParamType(1) == SizeTTy
);
1456 case LibFunc_posix_memalign
:
1457 return (NumParams
== 3 && FTy
.getReturnType()->isIntegerTy(32) &&
1458 FTy
.getParamType(0)->isPointerTy() &&
1459 FTy
.getParamType(1) == SizeTTy
&& FTy
.getParamType(2) == SizeTTy
);
1461 case LibFunc_wcslen
:
1462 return (NumParams
== 1 && FTy
.getParamType(0)->isPointerTy() &&
1463 FTy
.getReturnType()->isIntegerTy());
1467 case LibFunc_cabsl
: {
1468 Type
* RetTy
= FTy
.getReturnType();
1469 if (!RetTy
->isFloatingPointTy())
1472 // NOTE: These prototypes are target specific and currently support
1473 // "complex" passed as an array or discrete real & imaginary parameters.
1474 // Add other calling conventions to enable libcall optimizations.
1476 return (FTy
.getParamType(0)->isArrayTy() &&
1477 FTy
.getParamType(0)->getArrayNumElements() == 2 &&
1478 FTy
.getParamType(0)->getArrayElementType() == RetTy
);
1479 else if (NumParams
== 2)
1480 return (FTy
.getParamType(0) == RetTy
&& FTy
.getParamType(1) == RetTy
);
1484 case LibFunc::NumLibFuncs
:
1488 llvm_unreachable("Invalid libfunc");
1491 bool TargetLibraryInfoImpl::getLibFunc(const Function
&FDecl
,
1493 // Intrinsics don't overlap w/libcalls; if our module has a large number of
1494 // intrinsics, this ends up being an interesting compile time win since we
1495 // avoid string normalization and comparison.
1496 if (FDecl
.isIntrinsic()) return false;
1498 const DataLayout
*DL
=
1499 FDecl
.getParent() ? &FDecl
.getParent()->getDataLayout() : nullptr;
1500 return getLibFunc(FDecl
.getName(), F
) &&
1501 isValidProtoForLibFunc(*FDecl
.getFunctionType(), F
, DL
);
1504 void TargetLibraryInfoImpl::disableAllFunctions() {
1505 memset(AvailableArray
, 0, sizeof(AvailableArray
));
1508 static bool compareByScalarFnName(const VecDesc
&LHS
, const VecDesc
&RHS
) {
1509 return LHS
.ScalarFnName
< RHS
.ScalarFnName
;
1512 static bool compareByVectorFnName(const VecDesc
&LHS
, const VecDesc
&RHS
) {
1513 return LHS
.VectorFnName
< RHS
.VectorFnName
;
1516 static bool compareWithScalarFnName(const VecDesc
&LHS
, StringRef S
) {
1517 return LHS
.ScalarFnName
< S
;
1520 static bool compareWithVectorFnName(const VecDesc
&LHS
, StringRef S
) {
1521 return LHS
.VectorFnName
< S
;
1524 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef
<VecDesc
> Fns
) {
1525 VectorDescs
.insert(VectorDescs
.end(), Fns
.begin(), Fns
.end());
1526 llvm::sort(VectorDescs
, compareByScalarFnName
);
1528 ScalarDescs
.insert(ScalarDescs
.end(), Fns
.begin(), Fns
.end());
1529 llvm::sort(ScalarDescs
, compareByVectorFnName
);
1532 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
1533 enum VectorLibrary VecLib
) {
1536 const VecDesc VecFuncs
[] = {
1537 #define TLI_DEFINE_ACCELERATE_VECFUNCS
1538 #include "llvm/Analysis/VecFuncs.def"
1540 addVectorizableFunctions(VecFuncs
);
1544 const VecDesc VecFuncs
[] = {
1545 #define TLI_DEFINE_MASSV_VECFUNCS
1546 #include "llvm/Analysis/VecFuncs.def"
1548 addVectorizableFunctions(VecFuncs
);
1552 const VecDesc VecFuncs
[] = {
1553 #define TLI_DEFINE_SVML_VECFUNCS
1554 #include "llvm/Analysis/VecFuncs.def"
1556 addVectorizableFunctions(VecFuncs
);
1564 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName
) const {
1565 funcName
= sanitizeFunctionName(funcName
);
1566 if (funcName
.empty())
1569 std::vector
<VecDesc
>::const_iterator I
=
1570 llvm::lower_bound(VectorDescs
, funcName
, compareWithScalarFnName
);
1571 return I
!= VectorDescs
.end() && StringRef(I
->ScalarFnName
) == funcName
;
1574 StringRef
TargetLibraryInfoImpl::getVectorizedFunction(StringRef F
,
1575 unsigned VF
) const {
1576 F
= sanitizeFunctionName(F
);
1579 std::vector
<VecDesc
>::const_iterator I
=
1580 llvm::lower_bound(VectorDescs
, F
, compareWithScalarFnName
);
1581 while (I
!= VectorDescs
.end() && StringRef(I
->ScalarFnName
) == F
) {
1582 if (I
->VectorizationFactor
== VF
)
1583 return I
->VectorFnName
;
1589 StringRef
TargetLibraryInfoImpl::getScalarizedFunction(StringRef F
,
1590 unsigned &VF
) const {
1591 F
= sanitizeFunctionName(F
);
1595 std::vector
<VecDesc
>::const_iterator I
=
1596 llvm::lower_bound(ScalarDescs
, F
, compareWithVectorFnName
);
1597 if (I
== VectorDescs
.end() || StringRef(I
->VectorFnName
) != F
)
1599 VF
= I
->VectorizationFactor
;
1600 return I
->ScalarFnName
;
1603 TargetLibraryInfo
TargetLibraryAnalysis::run(Module
&M
,
1604 ModuleAnalysisManager
&) {
1606 return TargetLibraryInfo(*PresetInfoImpl
);
1608 return TargetLibraryInfo(lookupInfoImpl(Triple(M
.getTargetTriple())));
1611 TargetLibraryInfo
TargetLibraryAnalysis::run(Function
&F
,
1612 FunctionAnalysisManager
&) {
1614 return TargetLibraryInfo(*PresetInfoImpl
);
1616 return TargetLibraryInfo(
1617 lookupInfoImpl(Triple(F
.getParent()->getTargetTriple())));
1620 TargetLibraryInfoImpl
&TargetLibraryAnalysis::lookupInfoImpl(const Triple
&T
) {
1621 std::unique_ptr
<TargetLibraryInfoImpl
> &Impl
=
1622 Impls
[T
.normalize()];
1624 Impl
.reset(new TargetLibraryInfoImpl(T
));
1629 unsigned TargetLibraryInfoImpl::getWCharSize(const Module
&M
) const {
1630 if (auto *ShortWChar
= cast_or_null
<ConstantAsMetadata
>(
1631 M
.getModuleFlag("wchar_size")))
1632 return cast
<ConstantInt
>(ShortWChar
->getValue())->getZExtValue();
1636 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
1637 : ImmutablePass(ID
), TLIImpl(), TLI(TLIImpl
) {
1638 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1641 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple
&T
)
1642 : ImmutablePass(ID
), TLIImpl(T
), TLI(TLIImpl
) {
1643 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1646 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
1647 const TargetLibraryInfoImpl
&TLIImpl
)
1648 : ImmutablePass(ID
), TLIImpl(TLIImpl
), TLI(this->TLIImpl
) {
1649 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1652 AnalysisKey
TargetLibraryAnalysis::Key
;
1654 // Register the basic pass.
1655 INITIALIZE_PASS(TargetLibraryInfoWrapperPass
, "targetlibinfo",
1656 "Target Library Information", false, true)
1657 char TargetLibraryInfoWrapperPass::ID
= 0;
1659 void TargetLibraryInfoWrapperPass::anchor() {}