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 StringLiteral
const TargetLibraryInfoImpl::StandardNames
[LibFunc::NumLibFuncs
] =
33 #define TLI_DEFINE_STRING
34 #include "llvm/Analysis/TargetLibraryInfo.def"
37 static bool hasSinCosPiStret(const Triple
&T
) {
38 // Only Darwin variants have _stret versions of combined trig functions.
42 // The ABI is rather complicated on x86, so don't do anything special there.
43 if (T
.getArch() == Triple::x86
)
46 if (T
.isMacOSX() && T
.isMacOSXVersionLT(10, 9))
49 if (T
.isiOS() && T
.isOSVersionLT(7, 0))
55 static bool hasBcmp(const Triple
&TT
) {
56 // Posix removed support from bcmp() in 2001, but the glibc and several
57 // implementations of the libc still have it.
59 return TT
.isGNUEnvironment() || TT
.isMusl();
60 // Both NetBSD and OpenBSD are planning to remove the function. Windows does
62 return TT
.isOSFreeBSD() || TT
.isOSSolaris();
65 /// Initialize the set of available library functions based on the specified
66 /// target triple. This should be carefully written so that a missing target
67 /// triple gets a sane set of defaults.
68 static void initialize(TargetLibraryInfoImpl
&TLI
, const Triple
&T
,
69 ArrayRef
<StringLiteral
> StandardNames
) {
70 // Verify that the StandardNames array is in alphabetical order.
71 assert(std::is_sorted(StandardNames
.begin(), StandardNames
.end(),
72 [](StringRef LHS
, StringRef RHS
) {
75 "TargetLibraryInfoImpl function names must be sorted");
77 // Set IO unlocked variants as unavailable
78 // Set them as available per system below
79 TLI
.setUnavailable(LibFunc_getchar_unlocked
);
80 TLI
.setUnavailable(LibFunc_putc_unlocked
);
81 TLI
.setUnavailable(LibFunc_putchar_unlocked
);
82 TLI
.setUnavailable(LibFunc_fputc_unlocked
);
83 TLI
.setUnavailable(LibFunc_fgetc_unlocked
);
84 TLI
.setUnavailable(LibFunc_fread_unlocked
);
85 TLI
.setUnavailable(LibFunc_fwrite_unlocked
);
86 TLI
.setUnavailable(LibFunc_fputs_unlocked
);
87 TLI
.setUnavailable(LibFunc_fgets_unlocked
);
89 bool ShouldExtI32Param
= false, ShouldExtI32Return
= false,
90 ShouldSignExtI32Param
= false;
91 // PowerPC64, Sparc64, SystemZ need signext/zeroext on i32 parameters and
92 // returns corresponding to C-level ints and unsigned ints.
93 if (T
.isPPC64() || T
.getArch() == Triple::sparcv9
||
94 T
.getArch() == Triple::systemz
) {
95 ShouldExtI32Param
= true;
96 ShouldExtI32Return
= true;
98 // Mips, on the other hand, needs signext on i32 parameters corresponding
99 // to both signed and unsigned ints.
101 ShouldSignExtI32Param
= true;
103 TLI
.setShouldExtI32Param(ShouldExtI32Param
);
104 TLI
.setShouldExtI32Return(ShouldExtI32Return
);
105 TLI
.setShouldSignExtI32Param(ShouldSignExtI32Param
);
107 if (T
.getArch() == Triple::r600
||
108 T
.getArch() == Triple::amdgcn
) {
109 TLI
.setUnavailable(LibFunc_ldexp
);
110 TLI
.setUnavailable(LibFunc_ldexpf
);
111 TLI
.setUnavailable(LibFunc_ldexpl
);
112 TLI
.setUnavailable(LibFunc_exp10
);
113 TLI
.setUnavailable(LibFunc_exp10f
);
114 TLI
.setUnavailable(LibFunc_exp10l
);
115 TLI
.setUnavailable(LibFunc_log10
);
116 TLI
.setUnavailable(LibFunc_log10f
);
117 TLI
.setUnavailable(LibFunc_log10l
);
118 TLI
.setUnavailable(LibFunc_printf
);
121 // There are no library implementations of memcpy and memset for AMD gpus and
122 // these can be difficult to lower in the backend.
123 if (T
.getArch() == Triple::r600
||
124 T
.getArch() == Triple::amdgcn
) {
125 TLI
.setUnavailable(LibFunc_memcpy
);
126 TLI
.setUnavailable(LibFunc_memset
);
127 TLI
.setUnavailable(LibFunc_memset_pattern16
);
131 // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
132 // All versions of watchOS support it.
134 // available IO unlocked variants on Mac OS X
135 TLI
.setAvailable(LibFunc_getc_unlocked
);
136 TLI
.setAvailable(LibFunc_getchar_unlocked
);
137 TLI
.setAvailable(LibFunc_putc_unlocked
);
138 TLI
.setAvailable(LibFunc_putchar_unlocked
);
140 if (T
.isMacOSXVersionLT(10, 5))
141 TLI
.setUnavailable(LibFunc_memset_pattern16
);
142 } else if (T
.isiOS()) {
143 if (T
.isOSVersionLT(3, 0))
144 TLI
.setUnavailable(LibFunc_memset_pattern16
);
145 } else if (!T
.isWatchOS()) {
146 TLI
.setUnavailable(LibFunc_memset_pattern16
);
149 if (!hasSinCosPiStret(T
)) {
150 TLI
.setUnavailable(LibFunc_sinpi
);
151 TLI
.setUnavailable(LibFunc_sinpif
);
152 TLI
.setUnavailable(LibFunc_cospi
);
153 TLI
.setUnavailable(LibFunc_cospif
);
154 TLI
.setUnavailable(LibFunc_sincospi_stret
);
155 TLI
.setUnavailable(LibFunc_sincospif_stret
);
159 TLI
.setUnavailable(LibFunc_bcmp
);
161 if (T
.isMacOSX() && T
.getArch() == Triple::x86
&&
162 !T
.isMacOSXVersionLT(10, 7)) {
163 // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
164 // we don't care about) have two versions; on recent OSX, the one we want
165 // has a $UNIX2003 suffix. The two implementations are identical except
166 // for the return value in some edge cases. However, we don't want to
167 // generate code that depends on the old symbols.
168 TLI
.setAvailableWithName(LibFunc_fwrite
, "fwrite$UNIX2003");
169 TLI
.setAvailableWithName(LibFunc_fputs
, "fputs$UNIX2003");
172 // iprintf and friends are only available on XCore, TCE, and Emscripten.
173 if (T
.getArch() != Triple::xcore
&& T
.getArch() != Triple::tce
&&
174 T
.getOS() != Triple::Emscripten
) {
175 TLI
.setUnavailable(LibFunc_iprintf
);
176 TLI
.setUnavailable(LibFunc_siprintf
);
177 TLI
.setUnavailable(LibFunc_fiprintf
);
180 // __small_printf and friends are only available on Emscripten.
181 if (T
.getOS() != Triple::Emscripten
) {
182 TLI
.setUnavailable(LibFunc_small_printf
);
183 TLI
.setUnavailable(LibFunc_small_sprintf
);
184 TLI
.setUnavailable(LibFunc_small_fprintf
);
187 if (T
.isOSWindows() && !T
.isOSCygMing()) {
188 // XXX: The earliest documentation available at the moment is for VS2015/VC19:
189 // https://docs.microsoft.com/en-us/cpp/c-runtime-library/floating-point-support?view=vs-2015
190 // XXX: In order to use an MSVCRT older than VC19,
191 // the specific library version must be explicit in the target triple,
192 // e.g., x86_64-pc-windows-msvc18.
193 bool hasPartialC99
= true;
194 if (T
.isKnownWindowsMSVCEnvironment()) {
195 unsigned Major
, Minor
, Micro
;
196 T
.getEnvironmentVersion(Major
, Minor
, Micro
);
197 hasPartialC99
= (Major
== 0 || Major
>= 19);
200 // Latest targets support C89 math functions, in part.
201 bool isARM
= (T
.getArch() == Triple::aarch64
||
202 T
.getArch() == Triple::arm
);
203 bool hasPartialFloat
= (isARM
||
204 T
.getArch() == Triple::x86_64
);
206 // Win32 does not support float C89 math functions, in general.
207 if (!hasPartialFloat
) {
208 TLI
.setUnavailable(LibFunc_acosf
);
209 TLI
.setUnavailable(LibFunc_asinf
);
210 TLI
.setUnavailable(LibFunc_atan2f
);
211 TLI
.setUnavailable(LibFunc_atanf
);
212 TLI
.setUnavailable(LibFunc_ceilf
);
213 TLI
.setUnavailable(LibFunc_cosf
);
214 TLI
.setUnavailable(LibFunc_coshf
);
215 TLI
.setUnavailable(LibFunc_expf
);
216 TLI
.setUnavailable(LibFunc_floorf
);
217 TLI
.setUnavailable(LibFunc_fmodf
);
218 TLI
.setUnavailable(LibFunc_log10f
);
219 TLI
.setUnavailable(LibFunc_logf
);
220 TLI
.setUnavailable(LibFunc_modff
);
221 TLI
.setUnavailable(LibFunc_powf
);
222 TLI
.setUnavailable(LibFunc_sinf
);
223 TLI
.setUnavailable(LibFunc_sinhf
);
224 TLI
.setUnavailable(LibFunc_sqrtf
);
225 TLI
.setUnavailable(LibFunc_tanf
);
226 TLI
.setUnavailable(LibFunc_tanhf
);
229 TLI
.setUnavailable(LibFunc_fabsf
);
230 TLI
.setUnavailable(LibFunc_frexpf
);
231 TLI
.setUnavailable(LibFunc_ldexpf
);
233 // Win32 does not support long double C89 math functions.
234 TLI
.setUnavailable(LibFunc_acosl
);
235 TLI
.setUnavailable(LibFunc_asinl
);
236 TLI
.setUnavailable(LibFunc_atan2l
);
237 TLI
.setUnavailable(LibFunc_atanl
);
238 TLI
.setUnavailable(LibFunc_ceill
);
239 TLI
.setUnavailable(LibFunc_cosl
);
240 TLI
.setUnavailable(LibFunc_coshl
);
241 TLI
.setUnavailable(LibFunc_expl
);
242 TLI
.setUnavailable(LibFunc_fabsl
);
243 TLI
.setUnavailable(LibFunc_floorl
);
244 TLI
.setUnavailable(LibFunc_fmodl
);
245 TLI
.setUnavailable(LibFunc_frexpl
);
246 TLI
.setUnavailable(LibFunc_ldexpl
);
247 TLI
.setUnavailable(LibFunc_log10l
);
248 TLI
.setUnavailable(LibFunc_logl
);
249 TLI
.setUnavailable(LibFunc_modfl
);
250 TLI
.setUnavailable(LibFunc_powl
);
251 TLI
.setUnavailable(LibFunc_sinl
);
252 TLI
.setUnavailable(LibFunc_sinhl
);
253 TLI
.setUnavailable(LibFunc_sqrtl
);
254 TLI
.setUnavailable(LibFunc_tanl
);
255 TLI
.setUnavailable(LibFunc_tanhl
);
257 // Win32 does not fully support C99 math functions.
258 if (!hasPartialC99
) {
259 TLI
.setUnavailable(LibFunc_acosh
);
260 TLI
.setUnavailable(LibFunc_acoshf
);
261 TLI
.setUnavailable(LibFunc_asinh
);
262 TLI
.setUnavailable(LibFunc_asinhf
);
263 TLI
.setUnavailable(LibFunc_atanh
);
264 TLI
.setUnavailable(LibFunc_atanhf
);
265 TLI
.setAvailableWithName(LibFunc_cabs
, "_cabs");
266 TLI
.setUnavailable(LibFunc_cabsf
);
267 TLI
.setUnavailable(LibFunc_cbrt
);
268 TLI
.setUnavailable(LibFunc_cbrtf
);
269 TLI
.setAvailableWithName(LibFunc_copysign
, "_copysign");
270 TLI
.setAvailableWithName(LibFunc_copysignf
, "_copysignf");
271 TLI
.setUnavailable(LibFunc_exp2
);
272 TLI
.setUnavailable(LibFunc_exp2f
);
273 TLI
.setUnavailable(LibFunc_expm1
);
274 TLI
.setUnavailable(LibFunc_expm1f
);
275 TLI
.setUnavailable(LibFunc_fmax
);
276 TLI
.setUnavailable(LibFunc_fmaxf
);
277 TLI
.setUnavailable(LibFunc_fmin
);
278 TLI
.setUnavailable(LibFunc_fminf
);
279 TLI
.setUnavailable(LibFunc_log1p
);
280 TLI
.setUnavailable(LibFunc_log1pf
);
281 TLI
.setUnavailable(LibFunc_log2
);
282 TLI
.setUnavailable(LibFunc_log2f
);
283 TLI
.setAvailableWithName(LibFunc_logb
, "_logb");
285 TLI
.setAvailableWithName(LibFunc_logbf
, "_logbf");
287 TLI
.setUnavailable(LibFunc_logbf
);
288 TLI
.setUnavailable(LibFunc_rint
);
289 TLI
.setUnavailable(LibFunc_rintf
);
290 TLI
.setUnavailable(LibFunc_round
);
291 TLI
.setUnavailable(LibFunc_roundf
);
292 TLI
.setUnavailable(LibFunc_trunc
);
293 TLI
.setUnavailable(LibFunc_truncf
);
296 // Win32 does not support long double C99 math functions.
297 TLI
.setUnavailable(LibFunc_acoshl
);
298 TLI
.setUnavailable(LibFunc_asinhl
);
299 TLI
.setUnavailable(LibFunc_atanhl
);
300 TLI
.setUnavailable(LibFunc_cabsl
);
301 TLI
.setUnavailable(LibFunc_cbrtl
);
302 TLI
.setUnavailable(LibFunc_copysignl
);
303 TLI
.setUnavailable(LibFunc_exp2l
);
304 TLI
.setUnavailable(LibFunc_expm1l
);
305 TLI
.setUnavailable(LibFunc_fmaxl
);
306 TLI
.setUnavailable(LibFunc_fminl
);
307 TLI
.setUnavailable(LibFunc_log1pl
);
308 TLI
.setUnavailable(LibFunc_log2l
);
309 TLI
.setUnavailable(LibFunc_logbl
);
310 TLI
.setUnavailable(LibFunc_nearbyintl
);
311 TLI
.setUnavailable(LibFunc_rintl
);
312 TLI
.setUnavailable(LibFunc_roundl
);
313 TLI
.setUnavailable(LibFunc_truncl
);
315 // Win32 does not support these functions, but
316 // they are generally available on POSIX-compliant systems.
317 TLI
.setUnavailable(LibFunc_access
);
318 TLI
.setUnavailable(LibFunc_bcmp
);
319 TLI
.setUnavailable(LibFunc_bcopy
);
320 TLI
.setUnavailable(LibFunc_bzero
);
321 TLI
.setUnavailable(LibFunc_chmod
);
322 TLI
.setUnavailable(LibFunc_chown
);
323 TLI
.setUnavailable(LibFunc_closedir
);
324 TLI
.setUnavailable(LibFunc_ctermid
);
325 TLI
.setUnavailable(LibFunc_fdopen
);
326 TLI
.setUnavailable(LibFunc_ffs
);
327 TLI
.setUnavailable(LibFunc_fileno
);
328 TLI
.setUnavailable(LibFunc_flockfile
);
329 TLI
.setUnavailable(LibFunc_fseeko
);
330 TLI
.setUnavailable(LibFunc_fstat
);
331 TLI
.setUnavailable(LibFunc_fstatvfs
);
332 TLI
.setUnavailable(LibFunc_ftello
);
333 TLI
.setUnavailable(LibFunc_ftrylockfile
);
334 TLI
.setUnavailable(LibFunc_funlockfile
);
335 TLI
.setUnavailable(LibFunc_getitimer
);
336 TLI
.setUnavailable(LibFunc_getlogin_r
);
337 TLI
.setUnavailable(LibFunc_getpwnam
);
338 TLI
.setUnavailable(LibFunc_gettimeofday
);
339 TLI
.setUnavailable(LibFunc_htonl
);
340 TLI
.setUnavailable(LibFunc_htons
);
341 TLI
.setUnavailable(LibFunc_lchown
);
342 TLI
.setUnavailable(LibFunc_lstat
);
343 TLI
.setUnavailable(LibFunc_memccpy
);
344 TLI
.setUnavailable(LibFunc_mkdir
);
345 TLI
.setUnavailable(LibFunc_ntohl
);
346 TLI
.setUnavailable(LibFunc_ntohs
);
347 TLI
.setUnavailable(LibFunc_open
);
348 TLI
.setUnavailable(LibFunc_opendir
);
349 TLI
.setUnavailable(LibFunc_pclose
);
350 TLI
.setUnavailable(LibFunc_popen
);
351 TLI
.setUnavailable(LibFunc_pread
);
352 TLI
.setUnavailable(LibFunc_pwrite
);
353 TLI
.setUnavailable(LibFunc_read
);
354 TLI
.setUnavailable(LibFunc_readlink
);
355 TLI
.setUnavailable(LibFunc_realpath
);
356 TLI
.setUnavailable(LibFunc_rmdir
);
357 TLI
.setUnavailable(LibFunc_setitimer
);
358 TLI
.setUnavailable(LibFunc_stat
);
359 TLI
.setUnavailable(LibFunc_statvfs
);
360 TLI
.setUnavailable(LibFunc_stpcpy
);
361 TLI
.setUnavailable(LibFunc_stpncpy
);
362 TLI
.setUnavailable(LibFunc_strcasecmp
);
363 TLI
.setUnavailable(LibFunc_strncasecmp
);
364 TLI
.setUnavailable(LibFunc_times
);
365 TLI
.setUnavailable(LibFunc_uname
);
366 TLI
.setUnavailable(LibFunc_unlink
);
367 TLI
.setUnavailable(LibFunc_unsetenv
);
368 TLI
.setUnavailable(LibFunc_utime
);
369 TLI
.setUnavailable(LibFunc_utimes
);
370 TLI
.setUnavailable(LibFunc_write
);
375 // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
376 // and their names are __exp10 and __exp10f. exp10l is not available on
378 TLI
.setUnavailable(LibFunc_exp10l
);
379 if (T
.isMacOSXVersionLT(10, 9)) {
380 TLI
.setUnavailable(LibFunc_exp10
);
381 TLI
.setUnavailable(LibFunc_exp10f
);
383 TLI
.setAvailableWithName(LibFunc_exp10
, "__exp10");
384 TLI
.setAvailableWithName(LibFunc_exp10f
, "__exp10f");
389 case Triple::WatchOS
:
390 TLI
.setUnavailable(LibFunc_exp10l
);
391 if (!T
.isWatchOS() && (T
.isOSVersionLT(7, 0) ||
392 (T
.isOSVersionLT(9, 0) &&
393 (T
.getArch() == Triple::x86
||
394 T
.getArch() == Triple::x86_64
)))) {
395 TLI
.setUnavailable(LibFunc_exp10
);
396 TLI
.setUnavailable(LibFunc_exp10f
);
398 TLI
.setAvailableWithName(LibFunc_exp10
, "__exp10");
399 TLI
.setAvailableWithName(LibFunc_exp10f
, "__exp10f");
403 // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
404 // buggy prior to glibc version 2.18. Until this version is widely deployed
405 // or we have a reasonable detection strategy, we cannot use exp10 reliably
408 // Fall through to disable all of them.
411 TLI
.setUnavailable(LibFunc_exp10
);
412 TLI
.setUnavailable(LibFunc_exp10f
);
413 TLI
.setUnavailable(LibFunc_exp10l
);
416 // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
418 // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
419 // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
420 // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
426 case Triple::WatchOS
:
427 case Triple::FreeBSD
:
431 TLI
.setUnavailable(LibFunc_ffsl
);
434 // ffsll is available on at least FreeBSD and Linux (GLIBC):
435 // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
436 // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
442 case Triple::WatchOS
:
443 case Triple::FreeBSD
:
447 TLI
.setUnavailable(LibFunc_ffsll
);
450 // The following functions are available on at least FreeBSD:
451 // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
452 // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
453 // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
454 if (!T
.isOSFreeBSD()) {
455 TLI
.setUnavailable(LibFunc_fls
);
456 TLI
.setUnavailable(LibFunc_flsl
);
457 TLI
.setUnavailable(LibFunc_flsll
);
460 // The following functions are only available on GNU/Linux (using glibc).
461 // Linux variants without glibc (eg: bionic, musl) may have some subset.
462 if (!T
.isOSLinux() || !T
.isGNUEnvironment()) {
463 TLI
.setUnavailable(LibFunc_dunder_strdup
);
464 TLI
.setUnavailable(LibFunc_dunder_strtok_r
);
465 TLI
.setUnavailable(LibFunc_dunder_isoc99_scanf
);
466 TLI
.setUnavailable(LibFunc_dunder_isoc99_sscanf
);
467 TLI
.setUnavailable(LibFunc_under_IO_getc
);
468 TLI
.setUnavailable(LibFunc_under_IO_putc
);
469 // But, Android and musl have memalign.
470 if (!T
.isAndroid() && !T
.isMusl())
471 TLI
.setUnavailable(LibFunc_memalign
);
472 TLI
.setUnavailable(LibFunc_fopen64
);
473 TLI
.setUnavailable(LibFunc_fseeko64
);
474 TLI
.setUnavailable(LibFunc_fstat64
);
475 TLI
.setUnavailable(LibFunc_fstatvfs64
);
476 TLI
.setUnavailable(LibFunc_ftello64
);
477 TLI
.setUnavailable(LibFunc_lstat64
);
478 TLI
.setUnavailable(LibFunc_open64
);
479 TLI
.setUnavailable(LibFunc_stat64
);
480 TLI
.setUnavailable(LibFunc_statvfs64
);
481 TLI
.setUnavailable(LibFunc_tmpfile64
);
483 // Relaxed math functions are included in math-finite.h on Linux (GLIBC).
484 TLI
.setUnavailable(LibFunc_acos_finite
);
485 TLI
.setUnavailable(LibFunc_acosf_finite
);
486 TLI
.setUnavailable(LibFunc_acosl_finite
);
487 TLI
.setUnavailable(LibFunc_acosh_finite
);
488 TLI
.setUnavailable(LibFunc_acoshf_finite
);
489 TLI
.setUnavailable(LibFunc_acoshl_finite
);
490 TLI
.setUnavailable(LibFunc_asin_finite
);
491 TLI
.setUnavailable(LibFunc_asinf_finite
);
492 TLI
.setUnavailable(LibFunc_asinl_finite
);
493 TLI
.setUnavailable(LibFunc_atan2_finite
);
494 TLI
.setUnavailable(LibFunc_atan2f_finite
);
495 TLI
.setUnavailable(LibFunc_atan2l_finite
);
496 TLI
.setUnavailable(LibFunc_atanh_finite
);
497 TLI
.setUnavailable(LibFunc_atanhf_finite
);
498 TLI
.setUnavailable(LibFunc_atanhl_finite
);
499 TLI
.setUnavailable(LibFunc_cosh_finite
);
500 TLI
.setUnavailable(LibFunc_coshf_finite
);
501 TLI
.setUnavailable(LibFunc_coshl_finite
);
502 TLI
.setUnavailable(LibFunc_exp10_finite
);
503 TLI
.setUnavailable(LibFunc_exp10f_finite
);
504 TLI
.setUnavailable(LibFunc_exp10l_finite
);
505 TLI
.setUnavailable(LibFunc_exp2_finite
);
506 TLI
.setUnavailable(LibFunc_exp2f_finite
);
507 TLI
.setUnavailable(LibFunc_exp2l_finite
);
508 TLI
.setUnavailable(LibFunc_exp_finite
);
509 TLI
.setUnavailable(LibFunc_expf_finite
);
510 TLI
.setUnavailable(LibFunc_expl_finite
);
511 TLI
.setUnavailable(LibFunc_log10_finite
);
512 TLI
.setUnavailable(LibFunc_log10f_finite
);
513 TLI
.setUnavailable(LibFunc_log10l_finite
);
514 TLI
.setUnavailable(LibFunc_log2_finite
);
515 TLI
.setUnavailable(LibFunc_log2f_finite
);
516 TLI
.setUnavailable(LibFunc_log2l_finite
);
517 TLI
.setUnavailable(LibFunc_log_finite
);
518 TLI
.setUnavailable(LibFunc_logf_finite
);
519 TLI
.setUnavailable(LibFunc_logl_finite
);
520 TLI
.setUnavailable(LibFunc_pow_finite
);
521 TLI
.setUnavailable(LibFunc_powf_finite
);
522 TLI
.setUnavailable(LibFunc_powl_finite
);
523 TLI
.setUnavailable(LibFunc_sinh_finite
);
524 TLI
.setUnavailable(LibFunc_sinhf_finite
);
525 TLI
.setUnavailable(LibFunc_sinhl_finite
);
528 if ((T
.isOSLinux() && T
.isGNUEnvironment()) ||
529 (T
.isAndroid() && !T
.isAndroidVersionLT(28))) {
530 // available IO unlocked variants on GNU/Linux and Android P or later
531 TLI
.setAvailable(LibFunc_getc_unlocked
);
532 TLI
.setAvailable(LibFunc_getchar_unlocked
);
533 TLI
.setAvailable(LibFunc_putc_unlocked
);
534 TLI
.setAvailable(LibFunc_putchar_unlocked
);
535 TLI
.setAvailable(LibFunc_fputc_unlocked
);
536 TLI
.setAvailable(LibFunc_fgetc_unlocked
);
537 TLI
.setAvailable(LibFunc_fread_unlocked
);
538 TLI
.setAvailable(LibFunc_fwrite_unlocked
);
539 TLI
.setAvailable(LibFunc_fputs_unlocked
);
540 TLI
.setAvailable(LibFunc_fgets_unlocked
);
543 // As currently implemented in clang, NVPTX code has no standard library to
544 // speak of. Headers provide a standard-ish library implementation, but many
545 // of the signatures are wrong -- for example, many libm functions are not
548 // libdevice, an IR library provided by nvidia, is linked in by the front-end,
549 // but only used functions are provided to llvm. Moreover, most of the
550 // functions in libdevice don't map precisely to standard library functions.
552 // FIXME: Having no standard library prevents e.g. many fastmath
553 // optimizations, so this situation should be fixed.
555 TLI
.disableAllFunctions();
556 TLI
.setAvailable(LibFunc_nvvm_reflect
);
558 TLI
.setUnavailable(LibFunc_nvvm_reflect
);
561 TLI
.addVectorizableFunctionsFromVecLib(ClVectorLibrary
);
564 TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
565 // Default to everything being available.
566 memset(AvailableArray
, -1, sizeof(AvailableArray
));
568 initialize(*this, Triple(), StandardNames
);
571 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple
&T
) {
572 // Default to everything being available.
573 memset(AvailableArray
, -1, sizeof(AvailableArray
));
575 initialize(*this, T
, StandardNames
);
578 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl
&TLI
)
579 : CustomNames(TLI
.CustomNames
), ShouldExtI32Param(TLI
.ShouldExtI32Param
),
580 ShouldExtI32Return(TLI
.ShouldExtI32Return
),
581 ShouldSignExtI32Param(TLI
.ShouldSignExtI32Param
) {
582 memcpy(AvailableArray
, TLI
.AvailableArray
, sizeof(AvailableArray
));
583 VectorDescs
= TLI
.VectorDescs
;
584 ScalarDescs
= TLI
.ScalarDescs
;
587 TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl
&&TLI
)
588 : CustomNames(std::move(TLI
.CustomNames
)),
589 ShouldExtI32Param(TLI
.ShouldExtI32Param
),
590 ShouldExtI32Return(TLI
.ShouldExtI32Return
),
591 ShouldSignExtI32Param(TLI
.ShouldSignExtI32Param
) {
592 std::move(std::begin(TLI
.AvailableArray
), std::end(TLI
.AvailableArray
),
594 VectorDescs
= TLI
.VectorDescs
;
595 ScalarDescs
= TLI
.ScalarDescs
;
598 TargetLibraryInfoImpl
&TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl
&TLI
) {
599 CustomNames
= TLI
.CustomNames
;
600 ShouldExtI32Param
= TLI
.ShouldExtI32Param
;
601 ShouldExtI32Return
= TLI
.ShouldExtI32Return
;
602 ShouldSignExtI32Param
= TLI
.ShouldSignExtI32Param
;
603 memcpy(AvailableArray
, TLI
.AvailableArray
, sizeof(AvailableArray
));
607 TargetLibraryInfoImpl
&TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl
&&TLI
) {
608 CustomNames
= std::move(TLI
.CustomNames
);
609 ShouldExtI32Param
= TLI
.ShouldExtI32Param
;
610 ShouldExtI32Return
= TLI
.ShouldExtI32Return
;
611 ShouldSignExtI32Param
= TLI
.ShouldSignExtI32Param
;
612 std::move(std::begin(TLI
.AvailableArray
), std::end(TLI
.AvailableArray
),
617 static StringRef
sanitizeFunctionName(StringRef funcName
) {
618 // Filter out empty names and names containing null bytes, those can't be in
620 if (funcName
.empty() || funcName
.find('\0') != StringRef::npos
)
623 // Check for \01 prefix that is used to mangle __asm declarations and
624 // strip it if present.
625 return GlobalValue::dropLLVMManglingEscape(funcName
);
628 bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName
, LibFunc
&F
) const {
629 funcName
= sanitizeFunctionName(funcName
);
630 if (funcName
.empty())
633 const auto *Start
= std::begin(StandardNames
);
634 const auto *End
= std::end(StandardNames
);
635 const auto *I
= std::lower_bound(Start
, End
, funcName
);
636 if (I
!= End
&& *I
== funcName
) {
637 F
= (LibFunc
)(I
- Start
);
643 bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType
&FTy
,
645 const DataLayout
*DL
) const {
646 LLVMContext
&Ctx
= FTy
.getContext();
647 Type
*PCharTy
= Type::getInt8PtrTy(Ctx
);
648 Type
*SizeTTy
= DL
? DL
->getIntPtrType(Ctx
, /*AS=*/0) : nullptr;
649 auto IsSizeTTy
= [SizeTTy
](Type
*Ty
) {
650 return SizeTTy
? Ty
== SizeTTy
: Ty
->isIntegerTy();
652 unsigned NumParams
= FTy
.getNumParams();
658 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
659 FTy
.getParamType(1)->isPointerTy() &&
660 FTy
.getReturnType()->isIntegerTy(32));
663 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
664 FTy
.getParamType(1)->isPointerTy() &&
665 FTy
.getReturnType()->isIntegerTy(32));
667 case LibFunc_execvpe
:
669 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy() &&
670 FTy
.getParamType(1)->isPointerTy() &&
671 FTy
.getParamType(2)->isPointerTy() &&
672 FTy
.getReturnType()->isIntegerTy(32));
674 return (NumParams
== 1 && FTy
.getParamType(0)->isPointerTy() &&
675 FTy
.getReturnType()->isIntegerTy());
678 case LibFunc_strrchr
:
679 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
680 FTy
.getParamType(0) == FTy
.getReturnType() &&
681 FTy
.getParamType(1)->isIntegerTy());
686 case LibFunc_strtoul
:
687 case LibFunc_strtoll
:
688 case LibFunc_strtold
:
689 case LibFunc_strtoull
:
690 return ((NumParams
== 2 || NumParams
== 3) &&
691 FTy
.getParamType(0)->isPointerTy() &&
692 FTy
.getParamType(1)->isPointerTy());
693 case LibFunc_strcat_chk
:
695 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
699 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
700 FTy
.getParamType(0) == FTy
.getReturnType() &&
701 FTy
.getParamType(1) == FTy
.getReturnType());
703 case LibFunc_strncat_chk
:
705 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
708 case LibFunc_strncat
:
709 return (NumParams
== 3 && FTy
.getReturnType()->isPointerTy() &&
710 FTy
.getParamType(0) == FTy
.getReturnType() &&
711 FTy
.getParamType(1) == FTy
.getReturnType() &&
712 IsSizeTTy(FTy
.getParamType(2)));
714 case LibFunc_strcpy_chk
:
715 case LibFunc_stpcpy_chk
:
717 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
722 return (NumParams
== 2 && FTy
.getReturnType() == FTy
.getParamType(0) &&
723 FTy
.getParamType(0) == FTy
.getParamType(1) &&
724 FTy
.getParamType(0) == PCharTy
);
726 case LibFunc_strlcat_chk
:
727 case LibFunc_strlcpy_chk
:
729 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
732 case LibFunc_strlcat
:
733 case LibFunc_strlcpy
:
734 return NumParams
== 3 && IsSizeTTy(FTy
.getReturnType()) &&
735 FTy
.getParamType(0)->isPointerTy() &&
736 FTy
.getParamType(1)->isPointerTy() &&
737 IsSizeTTy(FTy
.getParamType(2));
739 case LibFunc_strncpy_chk
:
740 case LibFunc_stpncpy_chk
:
742 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
745 case LibFunc_strncpy
:
746 case LibFunc_stpncpy
:
747 return (NumParams
== 3 && FTy
.getReturnType() == FTy
.getParamType(0) &&
748 FTy
.getParamType(0) == FTy
.getParamType(1) &&
749 FTy
.getParamType(0) == PCharTy
&&
750 IsSizeTTy(FTy
.getParamType(2)));
752 case LibFunc_strxfrm
:
753 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy() &&
754 FTy
.getParamType(1)->isPointerTy());
757 return (NumParams
== 2 && FTy
.getReturnType()->isIntegerTy(32) &&
758 FTy
.getParamType(0)->isPointerTy() &&
759 FTy
.getParamType(0) == FTy
.getParamType(1));
761 case LibFunc_strncmp
:
762 return (NumParams
== 3 && FTy
.getReturnType()->isIntegerTy(32) &&
763 FTy
.getParamType(0)->isPointerTy() &&
764 FTy
.getParamType(0) == FTy
.getParamType(1) &&
765 IsSizeTTy(FTy
.getParamType(2)));
768 case LibFunc_strcspn
:
769 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
770 FTy
.getParamType(0) == FTy
.getParamType(1) &&
771 FTy
.getReturnType()->isIntegerTy());
773 case LibFunc_strcoll
:
774 case LibFunc_strcasecmp
:
775 case LibFunc_strncasecmp
:
776 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
777 FTy
.getParamType(1)->isPointerTy());
780 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
781 FTy
.getParamType(0)->isPointerTy() &&
782 FTy
.getParamType(1)->isPointerTy());
784 case LibFunc_strpbrk
:
785 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
786 FTy
.getReturnType() == FTy
.getParamType(0) &&
787 FTy
.getParamType(0) == FTy
.getParamType(1));
790 case LibFunc_strtok_r
:
791 return (NumParams
>= 2 && FTy
.getParamType(1)->isPointerTy());
794 case LibFunc_setvbuf
:
795 return (NumParams
>= 1 && FTy
.getParamType(0)->isPointerTy());
797 case LibFunc_strndup
:
798 return (NumParams
>= 1 && FTy
.getReturnType()->isPointerTy() &&
799 FTy
.getParamType(0)->isPointerTy());
802 case LibFunc_statvfs
:
803 case LibFunc_siprintf
:
804 case LibFunc_small_sprintf
:
805 case LibFunc_sprintf
:
806 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
807 FTy
.getParamType(1)->isPointerTy() &&
808 FTy
.getReturnType()->isIntegerTy(32));
810 case LibFunc_sprintf_chk
:
811 return NumParams
== 4 && FTy
.getParamType(0)->isPointerTy() &&
812 FTy
.getParamType(1)->isIntegerTy(32) &&
813 IsSizeTTy(FTy
.getParamType(2)) &&
814 FTy
.getParamType(3)->isPointerTy() &&
815 FTy
.getReturnType()->isIntegerTy(32);
817 case LibFunc_snprintf
:
818 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy() &&
819 FTy
.getParamType(2)->isPointerTy() &&
820 FTy
.getReturnType()->isIntegerTy(32));
822 case LibFunc_snprintf_chk
:
823 return NumParams
== 5 && FTy
.getParamType(0)->isPointerTy() &&
824 IsSizeTTy(FTy
.getParamType(1)) &&
825 FTy
.getParamType(2)->isIntegerTy(32) &&
826 IsSizeTTy(FTy
.getParamType(3)) &&
827 FTy
.getParamType(4)->isPointerTy() &&
828 FTy
.getReturnType()->isIntegerTy(32);
830 case LibFunc_setitimer
:
831 return (NumParams
== 3 && FTy
.getParamType(1)->isPointerTy() &&
832 FTy
.getParamType(2)->isPointerTy());
834 return (NumParams
== 1 && FTy
.getParamType(0)->isPointerTy());
836 return (NumParams
== 1 && FTy
.getReturnType()->isPointerTy());
838 return (NumParams
== 3 && FTy
.getReturnType()->isIntegerTy(32) &&
839 FTy
.getParamType(0)->isPointerTy() &&
840 FTy
.getParamType(1)->isPointerTy());
843 case LibFunc_memrchr
:
844 return (NumParams
== 3 && FTy
.getReturnType()->isPointerTy() &&
845 FTy
.getReturnType() == FTy
.getParamType(0) &&
846 FTy
.getParamType(1)->isIntegerTy(32) &&
847 IsSizeTTy(FTy
.getParamType(2)));
851 return (NumParams
>= 2 && FTy
.getParamType(1)->isPointerTy());
853 case LibFunc_memcpy_chk
:
854 case LibFunc_memmove_chk
:
856 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
860 case LibFunc_mempcpy
:
861 case LibFunc_memmove
:
862 return (NumParams
== 3 && FTy
.getReturnType() == FTy
.getParamType(0) &&
863 FTy
.getParamType(0)->isPointerTy() &&
864 FTy
.getParamType(1)->isPointerTy() &&
865 IsSizeTTy(FTy
.getParamType(2)));
867 case LibFunc_memset_chk
:
869 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
873 return (NumParams
== 3 && FTy
.getReturnType() == FTy
.getParamType(0) &&
874 FTy
.getParamType(0)->isPointerTy() &&
875 FTy
.getParamType(1)->isIntegerTy() &&
876 IsSizeTTy(FTy
.getParamType(2)));
878 case LibFunc_memccpy_chk
:
880 if (!IsSizeTTy(FTy
.getParamType(NumParams
)))
883 case LibFunc_memccpy
:
884 return (NumParams
>= 2 && FTy
.getParamType(1)->isPointerTy());
885 case LibFunc_memalign
:
886 return (FTy
.getReturnType()->isPointerTy());
887 case LibFunc_realloc
:
888 case LibFunc_reallocf
:
889 return (NumParams
== 2 && FTy
.getReturnType() == PCharTy
&&
890 FTy
.getParamType(0) == FTy
.getReturnType() &&
891 IsSizeTTy(FTy
.getParamType(1)));
893 return (NumParams
== 3 && FTy
.getParamType(1)->isPointerTy());
897 case LibFunc_realpath
:
898 return (NumParams
>= 1 && FTy
.getParamType(0)->isPointerTy());
900 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
901 FTy
.getParamType(1)->isPointerTy());
902 case LibFunc_readlink
:
903 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
904 FTy
.getParamType(1)->isPointerTy());
906 return (NumParams
== 3 && FTy
.getParamType(1)->isPointerTy());
909 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy() &&
910 FTy
.getParamType(1)->isPointerTy());
912 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy());
914 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy());
922 case LibFunc_getpwnam
:
923 case LibFunc_iprintf
:
924 case LibFunc_small_printf
:
930 case LibFunc_under_IO_getc
:
932 case LibFunc_unsetenv
:
933 return (NumParams
== 1 && FTy
.getParamType(0)->isPointerTy());
938 case LibFunc_clearerr
:
939 case LibFunc_closedir
:
940 case LibFunc_ctermid
:
945 case LibFunc_fgetc_unlocked
:
947 case LibFunc_flockfile
:
950 case LibFunc_fseeko64
:
952 case LibFunc_fsetpos
:
954 case LibFunc_ftello64
:
956 case LibFunc_ftrylockfile
:
957 case LibFunc_funlockfile
:
959 case LibFunc_getc_unlocked
:
960 case LibFunc_getlogin_r
:
964 return (NumParams
!= 0 && FTy
.getParamType(0)->isPointerTy());
967 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
968 FTy
.getParamType(0)->isPointerTy() &&
969 FTy
.getParamType(1)->isPointerTy());
971 return (NumParams
== 0 && FTy
.getReturnType()->isIntegerTy(32));
973 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
974 FTy
.getParamType(1)->isPointerTy());
976 case LibFunc_fputc_unlocked
:
981 case LibFunc_fstatvfs
:
982 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
984 case LibFunc_fgets_unlocked
:
985 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy() &&
986 FTy
.getParamType(2)->isPointerTy());
988 case LibFunc_fread_unlocked
:
989 return (NumParams
== 4 && FTy
.getParamType(0)->isPointerTy() &&
990 FTy
.getParamType(3)->isPointerTy());
992 case LibFunc_fwrite_unlocked
:
993 return (NumParams
== 4 && FTy
.getReturnType()->isIntegerTy() &&
994 FTy
.getParamType(0)->isPointerTy() &&
995 FTy
.getParamType(1)->isIntegerTy() &&
996 FTy
.getParamType(2)->isIntegerTy() &&
997 FTy
.getParamType(3)->isPointerTy());
999 case LibFunc_fputs_unlocked
:
1000 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
1001 FTy
.getParamType(1)->isPointerTy());
1002 case LibFunc_fscanf
:
1003 case LibFunc_fiprintf
:
1004 case LibFunc_small_fprintf
:
1005 case LibFunc_fprintf
:
1006 return (NumParams
>= 2 && FTy
.getReturnType()->isIntegerTy() &&
1007 FTy
.getParamType(0)->isPointerTy() &&
1008 FTy
.getParamType(1)->isPointerTy());
1009 case LibFunc_fgetpos
:
1010 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
1011 FTy
.getParamType(1)->isPointerTy());
1012 case LibFunc_getchar
:
1013 case LibFunc_getchar_unlocked
:
1014 return (NumParams
== 0 && FTy
.getReturnType()->isIntegerTy());
1016 return (NumParams
== 1 && FTy
.getParamType(0) == PCharTy
);
1017 case LibFunc_getitimer
:
1018 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
1019 case LibFunc_ungetc
:
1020 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
1022 case LibFunc_utimes
:
1023 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
1024 FTy
.getParamType(1)->isPointerTy());
1026 case LibFunc_putc_unlocked
:
1027 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
1029 case LibFunc_pwrite
:
1030 return (NumParams
== 4 && FTy
.getParamType(1)->isPointerTy());
1032 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
1033 FTy
.getParamType(0)->isPointerTy() &&
1034 FTy
.getParamType(1)->isPointerTy());
1035 case LibFunc_vscanf
:
1036 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
1037 case LibFunc_vsscanf
:
1038 return (NumParams
== 3 && FTy
.getParamType(1)->isPointerTy() &&
1039 FTy
.getParamType(2)->isPointerTy());
1040 case LibFunc_vfscanf
:
1041 return (NumParams
== 3 && FTy
.getParamType(1)->isPointerTy() &&
1042 FTy
.getParamType(2)->isPointerTy());
1043 case LibFunc_valloc
:
1044 return (FTy
.getReturnType()->isPointerTy());
1045 case LibFunc_vprintf
:
1046 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy());
1047 case LibFunc_vfprintf
:
1048 case LibFunc_vsprintf
:
1049 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy() &&
1050 FTy
.getParamType(1)->isPointerTy());
1051 case LibFunc_vsprintf_chk
:
1052 return NumParams
== 5 && FTy
.getParamType(0)->isPointerTy() &&
1053 FTy
.getParamType(1)->isIntegerTy(32) &&
1054 IsSizeTTy(FTy
.getParamType(2)) && FTy
.getParamType(3)->isPointerTy();
1055 case LibFunc_vsnprintf
:
1056 return (NumParams
== 4 && FTy
.getParamType(0)->isPointerTy() &&
1057 FTy
.getParamType(2)->isPointerTy());
1058 case LibFunc_vsnprintf_chk
:
1059 return NumParams
== 6 && FTy
.getParamType(0)->isPointerTy() &&
1060 FTy
.getParamType(2)->isIntegerTy(32) &&
1061 IsSizeTTy(FTy
.getParamType(3)) && FTy
.getParamType(4)->isPointerTy();
1063 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy());
1064 case LibFunc_opendir
:
1065 return (NumParams
== 1 && FTy
.getReturnType()->isPointerTy() &&
1066 FTy
.getParamType(0)->isPointerTy());
1067 case LibFunc_tmpfile
:
1068 return (FTy
.getReturnType()->isPointerTy());
1071 return (NumParams
== 1 && FTy
.getReturnType()->isIntegerTy(32) &&
1072 FTy
.getReturnType() == FTy
.getParamType(0));
1075 return (NumParams
== 1 && FTy
.getReturnType()->isIntegerTy(16) &&
1076 FTy
.getReturnType() == FTy
.getParamType(0));
1078 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
1079 FTy
.getParamType(1)->isPointerTy());
1080 case LibFunc_lchown
:
1081 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy());
1083 return (NumParams
== 4 && FTy
.getParamType(3)->isPointerTy());
1084 case LibFunc_dunder_strdup
:
1085 case LibFunc_dunder_strndup
:
1086 return (NumParams
>= 1 && FTy
.getReturnType()->isPointerTy() &&
1087 FTy
.getParamType(0)->isPointerTy());
1088 case LibFunc_dunder_strtok_r
:
1089 return (NumParams
== 3 && FTy
.getParamType(1)->isPointerTy());
1090 case LibFunc_under_IO_putc
:
1091 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
1092 case LibFunc_dunder_isoc99_scanf
:
1093 return (NumParams
>= 1 && FTy
.getParamType(0)->isPointerTy());
1094 case LibFunc_stat64
:
1095 case LibFunc_lstat64
:
1096 case LibFunc_statvfs64
:
1097 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
1098 FTy
.getParamType(1)->isPointerTy());
1099 case LibFunc_dunder_isoc99_sscanf
:
1100 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy() &&
1101 FTy
.getParamType(1)->isPointerTy());
1102 case LibFunc_fopen64
:
1103 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy() &&
1104 FTy
.getParamType(0)->isPointerTy() &&
1105 FTy
.getParamType(1)->isPointerTy());
1106 case LibFunc_tmpfile64
:
1107 return (FTy
.getReturnType()->isPointerTy());
1108 case LibFunc_fstat64
:
1109 case LibFunc_fstatvfs64
:
1110 return (NumParams
== 2 && FTy
.getParamType(1)->isPointerTy());
1111 case LibFunc_open64
:
1112 return (NumParams
>= 2 && FTy
.getParamType(0)->isPointerTy());
1113 case LibFunc_gettimeofday
:
1114 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy() &&
1115 FTy
.getParamType(1)->isPointerTy());
1117 // new(unsigned int);
1119 // new(unsigned long);
1121 // new[](unsigned int);
1123 // new[](unsigned long);
1125 // new(unsigned int);
1126 case LibFunc_msvc_new_int
:
1127 // new(unsigned long long);
1128 case LibFunc_msvc_new_longlong
:
1129 // new[](unsigned int);
1130 case LibFunc_msvc_new_array_int
:
1131 // new[](unsigned long long);
1132 case LibFunc_msvc_new_array_longlong
:
1133 return (NumParams
== 1 && FTy
.getReturnType()->isPointerTy());
1135 // new(unsigned int, nothrow);
1136 case LibFunc_ZnwjRKSt9nothrow_t
:
1137 // new(unsigned long, nothrow);
1138 case LibFunc_ZnwmRKSt9nothrow_t
:
1139 // new[](unsigned int, nothrow);
1140 case LibFunc_ZnajRKSt9nothrow_t
:
1141 // new[](unsigned long, nothrow);
1142 case LibFunc_ZnamRKSt9nothrow_t
:
1143 // new(unsigned int, nothrow);
1144 case LibFunc_msvc_new_int_nothrow
:
1145 // new(unsigned long long, nothrow);
1146 case LibFunc_msvc_new_longlong_nothrow
:
1147 // new[](unsigned int, nothrow);
1148 case LibFunc_msvc_new_array_int_nothrow
:
1149 // new[](unsigned long long, nothrow);
1150 case LibFunc_msvc_new_array_longlong_nothrow
:
1151 // new(unsigned int, align_val_t)
1152 case LibFunc_ZnwjSt11align_val_t
:
1153 // new(unsigned long, align_val_t)
1154 case LibFunc_ZnwmSt11align_val_t
:
1155 // new[](unsigned int, align_val_t)
1156 case LibFunc_ZnajSt11align_val_t
:
1157 // new[](unsigned long, align_val_t)
1158 case LibFunc_ZnamSt11align_val_t
:
1159 return (NumParams
== 2 && FTy
.getReturnType()->isPointerTy());
1161 // new(unsigned int, align_val_t, nothrow)
1162 case LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t
:
1163 // new(unsigned long, align_val_t, nothrow)
1164 case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t
:
1165 // new[](unsigned int, align_val_t, nothrow)
1166 case LibFunc_ZnajSt11align_val_tRKSt9nothrow_t
:
1167 // new[](unsigned long, align_val_t, nothrow)
1168 case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t
:
1169 return (NumParams
== 3 && FTy
.getReturnType()->isPointerTy());
1171 // void operator delete[](void*);
1173 // void operator delete(void*);
1175 // void operator delete[](void*);
1176 case LibFunc_msvc_delete_array_ptr32
:
1177 // void operator delete[](void*);
1178 case LibFunc_msvc_delete_array_ptr64
:
1179 // void operator delete(void*);
1180 case LibFunc_msvc_delete_ptr32
:
1181 // void operator delete(void*);
1182 case LibFunc_msvc_delete_ptr64
:
1183 return (NumParams
== 1 && FTy
.getParamType(0)->isPointerTy());
1185 // void operator delete[](void*, nothrow);
1186 case LibFunc_ZdaPvRKSt9nothrow_t
:
1187 // void operator delete[](void*, unsigned int);
1188 case LibFunc_ZdaPvj
:
1189 // void operator delete[](void*, unsigned long);
1190 case LibFunc_ZdaPvm
:
1191 // void operator delete(void*, nothrow);
1192 case LibFunc_ZdlPvRKSt9nothrow_t
:
1193 // void operator delete(void*, unsigned int);
1194 case LibFunc_ZdlPvj
:
1195 // void operator delete(void*, unsigned long);
1196 case LibFunc_ZdlPvm
:
1197 // void operator delete(void*, align_val_t)
1198 case LibFunc_ZdlPvSt11align_val_t
:
1199 // void operator delete[](void*, align_val_t)
1200 case LibFunc_ZdaPvSt11align_val_t
:
1201 // void operator delete[](void*, unsigned int);
1202 case LibFunc_msvc_delete_array_ptr32_int
:
1203 // void operator delete[](void*, nothrow);
1204 case LibFunc_msvc_delete_array_ptr32_nothrow
:
1205 // void operator delete[](void*, unsigned long long);
1206 case LibFunc_msvc_delete_array_ptr64_longlong
:
1207 // void operator delete[](void*, nothrow);
1208 case LibFunc_msvc_delete_array_ptr64_nothrow
:
1209 // void operator delete(void*, unsigned int);
1210 case LibFunc_msvc_delete_ptr32_int
:
1211 // void operator delete(void*, nothrow);
1212 case LibFunc_msvc_delete_ptr32_nothrow
:
1213 // void operator delete(void*, unsigned long long);
1214 case LibFunc_msvc_delete_ptr64_longlong
:
1215 // void operator delete(void*, nothrow);
1216 case LibFunc_msvc_delete_ptr64_nothrow
:
1217 return (NumParams
== 2 && FTy
.getParamType(0)->isPointerTy());
1219 // void operator delete(void*, align_val_t, nothrow)
1220 case LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t
:
1221 // void operator delete[](void*, align_val_t, nothrow)
1222 case LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t
:
1223 return (NumParams
== 3 && FTy
.getParamType(0)->isPointerTy());
1225 case LibFunc_memset_pattern16
:
1226 return (!FTy
.isVarArg() && NumParams
== 3 &&
1227 FTy
.getParamType(0)->isPointerTy() &&
1228 FTy
.getParamType(1)->isPointerTy() &&
1229 FTy
.getParamType(2)->isIntegerTy());
1231 case LibFunc_cxa_guard_abort
:
1232 case LibFunc_cxa_guard_acquire
:
1233 case LibFunc_cxa_guard_release
:
1234 case LibFunc_nvvm_reflect
:
1235 return (NumParams
== 1 && FTy
.getParamType(0)->isPointerTy());
1237 case LibFunc_sincospi_stret
:
1238 case LibFunc_sincospif_stret
:
1239 return (NumParams
== 1 && FTy
.getParamType(0)->isFloatingPointTy());
1242 case LibFunc_acos_finite
:
1244 case LibFunc_acosf_finite
:
1246 case LibFunc_acosh_finite
:
1247 case LibFunc_acoshf
:
1248 case LibFunc_acoshf_finite
:
1249 case LibFunc_acoshl
:
1250 case LibFunc_acoshl_finite
:
1252 case LibFunc_acosl_finite
:
1254 case LibFunc_asin_finite
:
1256 case LibFunc_asinf_finite
:
1258 case LibFunc_asinhf
:
1259 case LibFunc_asinhl
:
1261 case LibFunc_asinl_finite
:
1265 case LibFunc_atanh_finite
:
1266 case LibFunc_atanhf
:
1267 case LibFunc_atanhf_finite
:
1268 case LibFunc_atanhl
:
1269 case LibFunc_atanhl_finite
:
1280 case LibFunc_cosh_finite
:
1282 case LibFunc_coshf_finite
:
1284 case LibFunc_coshl_finite
:
1287 case LibFunc_exp10_finite
:
1288 case LibFunc_exp10f
:
1289 case LibFunc_exp10f_finite
:
1290 case LibFunc_exp10l
:
1291 case LibFunc_exp10l_finite
:
1293 case LibFunc_exp2_finite
:
1295 case LibFunc_exp2f_finite
:
1297 case LibFunc_exp2l_finite
:
1299 case LibFunc_exp_finite
:
1301 case LibFunc_expf_finite
:
1303 case LibFunc_expl_finite
:
1305 case LibFunc_expm1f
:
1306 case LibFunc_expm1l
:
1311 case LibFunc_floorf
:
1312 case LibFunc_floorl
:
1314 case LibFunc_log10_finite
:
1315 case LibFunc_log10f
:
1316 case LibFunc_log10f_finite
:
1317 case LibFunc_log10l
:
1318 case LibFunc_log10l_finite
:
1320 case LibFunc_log1pf
:
1321 case LibFunc_log1pl
:
1323 case LibFunc_log2_finite
:
1325 case LibFunc_log2f_finite
:
1327 case LibFunc_log2l_finite
:
1329 case LibFunc_log_finite
:
1334 case LibFunc_logf_finite
:
1336 case LibFunc_logl_finite
:
1337 case LibFunc_nearbyint
:
1338 case LibFunc_nearbyintf
:
1339 case LibFunc_nearbyintl
:
1344 case LibFunc_roundf
:
1345 case LibFunc_roundl
:
1349 case LibFunc_sinh_finite
:
1351 case LibFunc_sinhf_finite
:
1353 case LibFunc_sinhl_finite
:
1356 case LibFunc_sqrt_finite
:
1358 case LibFunc_sqrtf_finite
:
1360 case LibFunc_sqrtl_finite
:
1368 case LibFunc_truncf
:
1369 case LibFunc_truncl
:
1370 return (NumParams
== 1 && FTy
.getReturnType()->isFloatingPointTy() &&
1371 FTy
.getReturnType() == FTy
.getParamType(0));
1374 case LibFunc_atan2_finite
:
1375 case LibFunc_atan2f
:
1376 case LibFunc_atan2f_finite
:
1377 case LibFunc_atan2l
:
1378 case LibFunc_atan2l_finite
:
1388 case LibFunc_copysign
:
1389 case LibFunc_copysignf
:
1390 case LibFunc_copysignl
:
1392 case LibFunc_pow_finite
:
1394 case LibFunc_powf_finite
:
1396 case LibFunc_powl_finite
:
1397 return (NumParams
== 2 && FTy
.getReturnType()->isFloatingPointTy() &&
1398 FTy
.getReturnType() == FTy
.getParamType(0) &&
1399 FTy
.getReturnType() == FTy
.getParamType(1));
1402 case LibFunc_ldexpf
:
1403 case LibFunc_ldexpl
:
1404 return (NumParams
== 2 && FTy
.getReturnType()->isFloatingPointTy() &&
1405 FTy
.getReturnType() == FTy
.getParamType(0) &&
1406 FTy
.getParamType(1)->isIntegerTy(32));
1414 return (NumParams
== 1 && FTy
.getReturnType()->isIntegerTy(32) &&
1415 FTy
.getParamType(0)->isIntegerTy());
1417 case LibFunc_isdigit
:
1418 case LibFunc_isascii
:
1419 case LibFunc_toascii
:
1420 case LibFunc_putchar
:
1421 case LibFunc_putchar_unlocked
:
1422 return (NumParams
== 1 && FTy
.getReturnType()->isIntegerTy(32) &&
1423 FTy
.getReturnType() == FTy
.getParamType(0));
1428 return (NumParams
== 1 && FTy
.getReturnType()->isIntegerTy() &&
1429 FTy
.getReturnType() == FTy
.getParamType(0));
1431 case LibFunc_cxa_atexit
:
1432 return (NumParams
== 3 && FTy
.getReturnType()->isIntegerTy() &&
1433 FTy
.getParamType(0)->isPointerTy() &&
1434 FTy
.getParamType(1)->isPointerTy() &&
1435 FTy
.getParamType(2)->isPointerTy());
1439 return (NumParams
== 1 && FTy
.getReturnType()->isDoubleTy() &&
1440 FTy
.getReturnType() == FTy
.getParamType(0));
1442 case LibFunc_sinpif
:
1443 case LibFunc_cospif
:
1444 return (NumParams
== 1 && FTy
.getReturnType()->isFloatTy() &&
1445 FTy
.getReturnType() == FTy
.getParamType(0));
1447 case LibFunc_strnlen
:
1448 return (NumParams
== 2 && FTy
.getReturnType() == FTy
.getParamType(1) &&
1449 FTy
.getParamType(0) == PCharTy
&&
1450 FTy
.getParamType(1) == SizeTTy
);
1452 case LibFunc_posix_memalign
:
1453 return (NumParams
== 3 && FTy
.getReturnType()->isIntegerTy(32) &&
1454 FTy
.getParamType(0)->isPointerTy() &&
1455 FTy
.getParamType(1) == SizeTTy
&& FTy
.getParamType(2) == SizeTTy
);
1457 case LibFunc_wcslen
:
1458 return (NumParams
== 1 && FTy
.getParamType(0)->isPointerTy() &&
1459 FTy
.getReturnType()->isIntegerTy());
1463 case LibFunc_cabsl
: {
1464 Type
* RetTy
= FTy
.getReturnType();
1465 if (!RetTy
->isFloatingPointTy())
1468 // NOTE: These prototypes are target specific and currently support
1469 // "complex" passed as an array or discrete real & imaginary parameters.
1470 // Add other calling conventions to enable libcall optimizations.
1472 return (FTy
.getParamType(0)->isArrayTy() &&
1473 FTy
.getParamType(0)->getArrayNumElements() == 2 &&
1474 FTy
.getParamType(0)->getArrayElementType() == RetTy
);
1475 else if (NumParams
== 2)
1476 return (FTy
.getParamType(0) == RetTy
&& FTy
.getParamType(1) == RetTy
);
1480 case LibFunc::NumLibFuncs
:
1484 llvm_unreachable("Invalid libfunc");
1487 bool TargetLibraryInfoImpl::getLibFunc(const Function
&FDecl
,
1489 // Intrinsics don't overlap w/libcalls; if our module has a large number of
1490 // intrinsics, this ends up being an interesting compile time win since we
1491 // avoid string normalization and comparison.
1492 if (FDecl
.isIntrinsic()) return false;
1494 const DataLayout
*DL
=
1495 FDecl
.getParent() ? &FDecl
.getParent()->getDataLayout() : nullptr;
1496 return getLibFunc(FDecl
.getName(), F
) &&
1497 isValidProtoForLibFunc(*FDecl
.getFunctionType(), F
, DL
);
1500 void TargetLibraryInfoImpl::disableAllFunctions() {
1501 memset(AvailableArray
, 0, sizeof(AvailableArray
));
1504 static bool compareByScalarFnName(const VecDesc
&LHS
, const VecDesc
&RHS
) {
1505 return LHS
.ScalarFnName
< RHS
.ScalarFnName
;
1508 static bool compareByVectorFnName(const VecDesc
&LHS
, const VecDesc
&RHS
) {
1509 return LHS
.VectorFnName
< RHS
.VectorFnName
;
1512 static bool compareWithScalarFnName(const VecDesc
&LHS
, StringRef S
) {
1513 return LHS
.ScalarFnName
< S
;
1516 static bool compareWithVectorFnName(const VecDesc
&LHS
, StringRef S
) {
1517 return LHS
.VectorFnName
< S
;
1520 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef
<VecDesc
> Fns
) {
1521 VectorDescs
.insert(VectorDescs
.end(), Fns
.begin(), Fns
.end());
1522 llvm::sort(VectorDescs
, compareByScalarFnName
);
1524 ScalarDescs
.insert(ScalarDescs
.end(), Fns
.begin(), Fns
.end());
1525 llvm::sort(ScalarDescs
, compareByVectorFnName
);
1528 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
1529 enum VectorLibrary VecLib
) {
1532 const VecDesc VecFuncs
[] = {
1533 #define TLI_DEFINE_ACCELERATE_VECFUNCS
1534 #include "llvm/Analysis/VecFuncs.def"
1536 addVectorizableFunctions(VecFuncs
);
1540 const VecDesc VecFuncs
[] = {
1541 #define TLI_DEFINE_MASSV_VECFUNCS
1542 #include "llvm/Analysis/VecFuncs.def"
1544 addVectorizableFunctions(VecFuncs
);
1548 const VecDesc VecFuncs
[] = {
1549 #define TLI_DEFINE_SVML_VECFUNCS
1550 #include "llvm/Analysis/VecFuncs.def"
1552 addVectorizableFunctions(VecFuncs
);
1560 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName
) const {
1561 funcName
= sanitizeFunctionName(funcName
);
1562 if (funcName
.empty())
1565 std::vector
<VecDesc
>::const_iterator I
=
1566 llvm::lower_bound(VectorDescs
, funcName
, compareWithScalarFnName
);
1567 return I
!= VectorDescs
.end() && StringRef(I
->ScalarFnName
) == funcName
;
1570 StringRef
TargetLibraryInfoImpl::getVectorizedFunction(StringRef F
,
1571 unsigned VF
) const {
1572 F
= sanitizeFunctionName(F
);
1575 std::vector
<VecDesc
>::const_iterator I
=
1576 llvm::lower_bound(VectorDescs
, F
, compareWithScalarFnName
);
1577 while (I
!= VectorDescs
.end() && StringRef(I
->ScalarFnName
) == F
) {
1578 if (I
->VectorizationFactor
== VF
)
1579 return I
->VectorFnName
;
1585 StringRef
TargetLibraryInfoImpl::getScalarizedFunction(StringRef F
,
1586 unsigned &VF
) const {
1587 F
= sanitizeFunctionName(F
);
1591 std::vector
<VecDesc
>::const_iterator I
=
1592 llvm::lower_bound(ScalarDescs
, F
, compareWithVectorFnName
);
1593 if (I
== VectorDescs
.end() || StringRef(I
->VectorFnName
) != F
)
1595 VF
= I
->VectorizationFactor
;
1596 return I
->ScalarFnName
;
1599 TargetLibraryInfo
TargetLibraryAnalysis::run(Module
&M
,
1600 ModuleAnalysisManager
&) {
1602 return TargetLibraryInfo(*PresetInfoImpl
);
1604 return TargetLibraryInfo(lookupInfoImpl(Triple(M
.getTargetTriple())));
1607 TargetLibraryInfo
TargetLibraryAnalysis::run(Function
&F
,
1608 FunctionAnalysisManager
&) {
1610 return TargetLibraryInfo(*PresetInfoImpl
);
1612 return TargetLibraryInfo(
1613 lookupInfoImpl(Triple(F
.getParent()->getTargetTriple())));
1616 TargetLibraryInfoImpl
&TargetLibraryAnalysis::lookupInfoImpl(const Triple
&T
) {
1617 std::unique_ptr
<TargetLibraryInfoImpl
> &Impl
=
1618 Impls
[T
.normalize()];
1620 Impl
.reset(new TargetLibraryInfoImpl(T
));
1625 unsigned TargetLibraryInfoImpl::getWCharSize(const Module
&M
) const {
1626 if (auto *ShortWChar
= cast_or_null
<ConstantAsMetadata
>(
1627 M
.getModuleFlag("wchar_size")))
1628 return cast
<ConstantInt
>(ShortWChar
->getValue())->getZExtValue();
1632 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
1633 : ImmutablePass(ID
), TLIImpl(), TLI(TLIImpl
) {
1634 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1637 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple
&T
)
1638 : ImmutablePass(ID
), TLIImpl(T
), TLI(TLIImpl
) {
1639 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1642 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
1643 const TargetLibraryInfoImpl
&TLIImpl
)
1644 : ImmutablePass(ID
), TLIImpl(TLIImpl
), TLI(this->TLIImpl
) {
1645 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1648 AnalysisKey
TargetLibraryAnalysis::Key
;
1650 // Register the basic pass.
1651 INITIALIZE_PASS(TargetLibraryInfoWrapperPass
, "targetlibinfo",
1652 "Target Library Information", false, true)
1653 char TargetLibraryInfoWrapperPass::ID
= 0;
1655 void TargetLibraryInfoWrapperPass::anchor() {}