[ASan] Make insertion of version mismatch guard configurable
[llvm-core.git] / lib / Analysis / TargetLibraryInfo.cpp
blobbfa2873ddd9acbb0a6435e00670d46a2ed1b4757
1 //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the TargetLibraryInfo class.
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"
17 using namespace llvm;
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.
39 if (!T.isOSDarwin())
40 return false;
42 // The ABI is rather complicated on x86, so don't do anything special there.
43 if (T.getArch() == Triple::x86)
44 return false;
46 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
47 return false;
49 if (T.isiOS() && T.isOSVersionLT(7, 0))
50 return false;
52 return true;
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.
58 if (TT.isOSLinux())
59 return TT.isGNUEnvironment() || TT.isMusl();
60 // Both NetBSD and OpenBSD are planning to remove the function. Windows does
61 // not have it.
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) {
73 return LHS < RHS;
74 }) &&
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.
100 if (T.isMIPS()) {
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);
128 return;
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.
133 if (T.isMacOSX()) {
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);
158 if (!hasBcmp(T))
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);
228 if (!isARM)
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");
284 if (hasPartialFloat)
285 TLI.setAvailableWithName(LibFunc_logbf, "_logbf");
286 else
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);
373 switch (T.getOS()) {
374 case Triple::MacOSX:
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
377 // OS X or iOS.
378 TLI.setUnavailable(LibFunc_exp10l);
379 if (T.isMacOSXVersionLT(10, 9)) {
380 TLI.setUnavailable(LibFunc_exp10);
381 TLI.setUnavailable(LibFunc_exp10f);
382 } else {
383 TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
384 TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
386 break;
387 case Triple::IOS:
388 case Triple::TvOS:
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);
397 } else {
398 TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
399 TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
401 break;
402 case Triple::Linux:
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
406 // on Linux.
408 // Fall through to disable all of them.
409 LLVM_FALLTHROUGH;
410 default:
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
417 // Linux (GLIBC):
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
421 switch (T.getOS()) {
422 case Triple::Darwin:
423 case Triple::MacOSX:
424 case Triple::IOS:
425 case Triple::TvOS:
426 case Triple::WatchOS:
427 case Triple::FreeBSD:
428 case Triple::Linux:
429 break;
430 default:
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
437 switch (T.getOS()) {
438 case Triple::Darwin:
439 case Triple::MacOSX:
440 case Triple::IOS:
441 case Triple::TvOS:
442 case Triple::WatchOS:
443 case Triple::FreeBSD:
444 case Triple::Linux:
445 break;
446 default:
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
546 // extern "C".
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.
554 if (T.isNVPTX()) {
555 TLI.disableAllFunctions();
556 TLI.setAvailable(LibFunc_nvvm_reflect);
557 } else {
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),
593 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));
604 return *this;
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),
613 AvailableArray);
614 return *this;
617 static StringRef sanitizeFunctionName(StringRef funcName) {
618 // Filter out empty names and names containing null bytes, those can't be in
619 // our table.
620 if (funcName.empty() || funcName.find('\0') != StringRef::npos)
621 return StringRef();
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())
631 return false;
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);
638 return true;
640 return false;
643 bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
644 LibFunc F,
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();
654 switch (F) {
655 case LibFunc_execl:
656 case LibFunc_execlp:
657 case LibFunc_execle:
658 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
659 FTy.getParamType(1)->isPointerTy() &&
660 FTy.getReturnType()->isIntegerTy(32));
661 case LibFunc_execv:
662 case LibFunc_execvp:
663 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
664 FTy.getParamType(1)->isPointerTy() &&
665 FTy.getReturnType()->isIntegerTy(32));
666 case LibFunc_execvP:
667 case LibFunc_execvpe:
668 case LibFunc_execve:
669 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
670 FTy.getParamType(1)->isPointerTy() &&
671 FTy.getParamType(2)->isPointerTy() &&
672 FTy.getReturnType()->isIntegerTy(32));
673 case LibFunc_strlen:
674 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
675 FTy.getReturnType()->isIntegerTy());
677 case LibFunc_strchr:
678 case LibFunc_strrchr:
679 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
680 FTy.getParamType(0) == FTy.getReturnType() &&
681 FTy.getParamType(1)->isIntegerTy());
683 case LibFunc_strtol:
684 case LibFunc_strtod:
685 case LibFunc_strtof:
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:
694 --NumParams;
695 if (!IsSizeTTy(FTy.getParamType(NumParams)))
696 return false;
697 LLVM_FALLTHROUGH;
698 case LibFunc_strcat:
699 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
700 FTy.getParamType(0) == FTy.getReturnType() &&
701 FTy.getParamType(1) == FTy.getReturnType());
703 case LibFunc_strncat_chk:
704 --NumParams;
705 if (!IsSizeTTy(FTy.getParamType(NumParams)))
706 return false;
707 LLVM_FALLTHROUGH;
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:
716 --NumParams;
717 if (!IsSizeTTy(FTy.getParamType(NumParams)))
718 return false;
719 LLVM_FALLTHROUGH;
720 case LibFunc_strcpy:
721 case LibFunc_stpcpy:
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:
728 --NumParams;
729 if (!IsSizeTTy(FTy.getParamType(NumParams)))
730 return false;
731 LLVM_FALLTHROUGH;
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:
741 --NumParams;
742 if (!IsSizeTTy(FTy.getParamType(NumParams)))
743 return false;
744 LLVM_FALLTHROUGH;
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());
756 case LibFunc_strcmp:
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)));
767 case LibFunc_strspn:
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());
779 case LibFunc_strstr:
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));
789 case LibFunc_strtok:
790 case LibFunc_strtok_r:
791 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
792 case LibFunc_scanf:
793 case LibFunc_setbuf:
794 case LibFunc_setvbuf:
795 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
796 case LibFunc_strdup:
797 case LibFunc_strndup:
798 return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
799 FTy.getParamType(0)->isPointerTy());
800 case LibFunc_sscanf:
801 case LibFunc_stat:
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());
833 case LibFunc_system:
834 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
835 case LibFunc_malloc:
836 return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
837 case LibFunc_memcmp:
838 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
839 FTy.getParamType(0)->isPointerTy() &&
840 FTy.getParamType(1)->isPointerTy());
842 case LibFunc_memchr:
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)));
848 case LibFunc_modf:
849 case LibFunc_modff:
850 case LibFunc_modfl:
851 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
853 case LibFunc_memcpy_chk:
854 case LibFunc_memmove_chk:
855 --NumParams;
856 if (!IsSizeTTy(FTy.getParamType(NumParams)))
857 return false;
858 LLVM_FALLTHROUGH;
859 case LibFunc_memcpy:
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:
868 --NumParams;
869 if (!IsSizeTTy(FTy.getParamType(NumParams)))
870 return false;
871 LLVM_FALLTHROUGH;
872 case LibFunc_memset:
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:
879 --NumParams;
880 if (!IsSizeTTy(FTy.getParamType(NumParams)))
881 return false;
882 LLVM_FALLTHROUGH;
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)));
892 case LibFunc_read:
893 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
894 case LibFunc_rewind:
895 case LibFunc_rmdir:
896 case LibFunc_remove:
897 case LibFunc_realpath:
898 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
899 case LibFunc_rename:
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());
905 case LibFunc_write:
906 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
907 case LibFunc_bcopy:
908 case LibFunc_bcmp:
909 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
910 FTy.getParamType(1)->isPointerTy());
911 case LibFunc_bzero:
912 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
913 case LibFunc_calloc:
914 return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
916 case LibFunc_atof:
917 case LibFunc_atoi:
918 case LibFunc_atol:
919 case LibFunc_atoll:
920 case LibFunc_ferror:
921 case LibFunc_getenv:
922 case LibFunc_getpwnam:
923 case LibFunc_iprintf:
924 case LibFunc_small_printf:
925 case LibFunc_pclose:
926 case LibFunc_perror:
927 case LibFunc_printf:
928 case LibFunc_puts:
929 case LibFunc_uname:
930 case LibFunc_under_IO_getc:
931 case LibFunc_unlink:
932 case LibFunc_unsetenv:
933 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
935 case LibFunc_access:
936 case LibFunc_chmod:
937 case LibFunc_chown:
938 case LibFunc_clearerr:
939 case LibFunc_closedir:
940 case LibFunc_ctermid:
941 case LibFunc_fclose:
942 case LibFunc_feof:
943 case LibFunc_fflush:
944 case LibFunc_fgetc:
945 case LibFunc_fgetc_unlocked:
946 case LibFunc_fileno:
947 case LibFunc_flockfile:
948 case LibFunc_free:
949 case LibFunc_fseek:
950 case LibFunc_fseeko64:
951 case LibFunc_fseeko:
952 case LibFunc_fsetpos:
953 case LibFunc_ftell:
954 case LibFunc_ftello64:
955 case LibFunc_ftello:
956 case LibFunc_ftrylockfile:
957 case LibFunc_funlockfile:
958 case LibFunc_getc:
959 case LibFunc_getc_unlocked:
960 case LibFunc_getlogin_r:
961 case LibFunc_mkdir:
962 case LibFunc_mktime:
963 case LibFunc_times:
964 return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
966 case LibFunc_fopen:
967 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
968 FTy.getParamType(0)->isPointerTy() &&
969 FTy.getParamType(1)->isPointerTy());
970 case LibFunc_fork:
971 return (NumParams == 0 && FTy.getReturnType()->isIntegerTy(32));
972 case LibFunc_fdopen:
973 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
974 FTy.getParamType(1)->isPointerTy());
975 case LibFunc_fputc:
976 case LibFunc_fputc_unlocked:
977 case LibFunc_fstat:
978 case LibFunc_frexp:
979 case LibFunc_frexpf:
980 case LibFunc_frexpl:
981 case LibFunc_fstatvfs:
982 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
983 case LibFunc_fgets:
984 case LibFunc_fgets_unlocked:
985 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
986 FTy.getParamType(2)->isPointerTy());
987 case LibFunc_fread:
988 case LibFunc_fread_unlocked:
989 return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
990 FTy.getParamType(3)->isPointerTy());
991 case LibFunc_fwrite:
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());
998 case LibFunc_fputs:
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());
1015 case LibFunc_gets:
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());
1021 case LibFunc_utime:
1022 case LibFunc_utimes:
1023 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1024 FTy.getParamType(1)->isPointerTy());
1025 case LibFunc_putc:
1026 case LibFunc_putc_unlocked:
1027 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1028 case LibFunc_pread:
1029 case LibFunc_pwrite:
1030 return (NumParams == 4 && FTy.getParamType(1)->isPointerTy());
1031 case LibFunc_popen:
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();
1062 case LibFunc_open:
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());
1069 case LibFunc_htonl:
1070 case LibFunc_ntohl:
1071 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1072 FTy.getReturnType() == FTy.getParamType(0));
1073 case LibFunc_htons:
1074 case LibFunc_ntohs:
1075 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(16) &&
1076 FTy.getReturnType() == FTy.getParamType(0));
1077 case LibFunc_lstat:
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());
1082 case LibFunc_qsort:
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);
1118 case LibFunc_Znwj:
1119 // new(unsigned long);
1120 case LibFunc_Znwm:
1121 // new[](unsigned int);
1122 case LibFunc_Znaj:
1123 // new[](unsigned long);
1124 case LibFunc_Znam:
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*);
1172 case LibFunc_ZdaPv:
1173 // void operator delete(void*);
1174 case LibFunc_ZdlPv:
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());
1241 case LibFunc_acos:
1242 case LibFunc_acos_finite:
1243 case LibFunc_acosf:
1244 case LibFunc_acosf_finite:
1245 case LibFunc_acosh:
1246 case LibFunc_acosh_finite:
1247 case LibFunc_acoshf:
1248 case LibFunc_acoshf_finite:
1249 case LibFunc_acoshl:
1250 case LibFunc_acoshl_finite:
1251 case LibFunc_acosl:
1252 case LibFunc_acosl_finite:
1253 case LibFunc_asin:
1254 case LibFunc_asin_finite:
1255 case LibFunc_asinf:
1256 case LibFunc_asinf_finite:
1257 case LibFunc_asinh:
1258 case LibFunc_asinhf:
1259 case LibFunc_asinhl:
1260 case LibFunc_asinl:
1261 case LibFunc_asinl_finite:
1262 case LibFunc_atan:
1263 case LibFunc_atanf:
1264 case LibFunc_atanh:
1265 case LibFunc_atanh_finite:
1266 case LibFunc_atanhf:
1267 case LibFunc_atanhf_finite:
1268 case LibFunc_atanhl:
1269 case LibFunc_atanhl_finite:
1270 case LibFunc_atanl:
1271 case LibFunc_cbrt:
1272 case LibFunc_cbrtf:
1273 case LibFunc_cbrtl:
1274 case LibFunc_ceil:
1275 case LibFunc_ceilf:
1276 case LibFunc_ceill:
1277 case LibFunc_cos:
1278 case LibFunc_cosf:
1279 case LibFunc_cosh:
1280 case LibFunc_cosh_finite:
1281 case LibFunc_coshf:
1282 case LibFunc_coshf_finite:
1283 case LibFunc_coshl:
1284 case LibFunc_coshl_finite:
1285 case LibFunc_cosl:
1286 case LibFunc_exp10:
1287 case LibFunc_exp10_finite:
1288 case LibFunc_exp10f:
1289 case LibFunc_exp10f_finite:
1290 case LibFunc_exp10l:
1291 case LibFunc_exp10l_finite:
1292 case LibFunc_exp2:
1293 case LibFunc_exp2_finite:
1294 case LibFunc_exp2f:
1295 case LibFunc_exp2f_finite:
1296 case LibFunc_exp2l:
1297 case LibFunc_exp2l_finite:
1298 case LibFunc_exp:
1299 case LibFunc_exp_finite:
1300 case LibFunc_expf:
1301 case LibFunc_expf_finite:
1302 case LibFunc_expl:
1303 case LibFunc_expl_finite:
1304 case LibFunc_expm1:
1305 case LibFunc_expm1f:
1306 case LibFunc_expm1l:
1307 case LibFunc_fabs:
1308 case LibFunc_fabsf:
1309 case LibFunc_fabsl:
1310 case LibFunc_floor:
1311 case LibFunc_floorf:
1312 case LibFunc_floorl:
1313 case LibFunc_log10:
1314 case LibFunc_log10_finite:
1315 case LibFunc_log10f:
1316 case LibFunc_log10f_finite:
1317 case LibFunc_log10l:
1318 case LibFunc_log10l_finite:
1319 case LibFunc_log1p:
1320 case LibFunc_log1pf:
1321 case LibFunc_log1pl:
1322 case LibFunc_log2:
1323 case LibFunc_log2_finite:
1324 case LibFunc_log2f:
1325 case LibFunc_log2f_finite:
1326 case LibFunc_log2l:
1327 case LibFunc_log2l_finite:
1328 case LibFunc_log:
1329 case LibFunc_log_finite:
1330 case LibFunc_logb:
1331 case LibFunc_logbf:
1332 case LibFunc_logbl:
1333 case LibFunc_logf:
1334 case LibFunc_logf_finite:
1335 case LibFunc_logl:
1336 case LibFunc_logl_finite:
1337 case LibFunc_nearbyint:
1338 case LibFunc_nearbyintf:
1339 case LibFunc_nearbyintl:
1340 case LibFunc_rint:
1341 case LibFunc_rintf:
1342 case LibFunc_rintl:
1343 case LibFunc_round:
1344 case LibFunc_roundf:
1345 case LibFunc_roundl:
1346 case LibFunc_sin:
1347 case LibFunc_sinf:
1348 case LibFunc_sinh:
1349 case LibFunc_sinh_finite:
1350 case LibFunc_sinhf:
1351 case LibFunc_sinhf_finite:
1352 case LibFunc_sinhl:
1353 case LibFunc_sinhl_finite:
1354 case LibFunc_sinl:
1355 case LibFunc_sqrt:
1356 case LibFunc_sqrt_finite:
1357 case LibFunc_sqrtf:
1358 case LibFunc_sqrtf_finite:
1359 case LibFunc_sqrtl:
1360 case LibFunc_sqrtl_finite:
1361 case LibFunc_tan:
1362 case LibFunc_tanf:
1363 case LibFunc_tanh:
1364 case LibFunc_tanhf:
1365 case LibFunc_tanhl:
1366 case LibFunc_tanl:
1367 case LibFunc_trunc:
1368 case LibFunc_truncf:
1369 case LibFunc_truncl:
1370 return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() &&
1371 FTy.getReturnType() == FTy.getParamType(0));
1373 case LibFunc_atan2:
1374 case LibFunc_atan2_finite:
1375 case LibFunc_atan2f:
1376 case LibFunc_atan2f_finite:
1377 case LibFunc_atan2l:
1378 case LibFunc_atan2l_finite:
1379 case LibFunc_fmin:
1380 case LibFunc_fminf:
1381 case LibFunc_fminl:
1382 case LibFunc_fmax:
1383 case LibFunc_fmaxf:
1384 case LibFunc_fmaxl:
1385 case LibFunc_fmod:
1386 case LibFunc_fmodf:
1387 case LibFunc_fmodl:
1388 case LibFunc_copysign:
1389 case LibFunc_copysignf:
1390 case LibFunc_copysignl:
1391 case LibFunc_pow:
1392 case LibFunc_pow_finite:
1393 case LibFunc_powf:
1394 case LibFunc_powf_finite:
1395 case LibFunc_powl:
1396 case LibFunc_powl_finite:
1397 return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
1398 FTy.getReturnType() == FTy.getParamType(0) &&
1399 FTy.getReturnType() == FTy.getParamType(1));
1401 case LibFunc_ldexp:
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));
1408 case LibFunc_ffs:
1409 case LibFunc_ffsl:
1410 case LibFunc_ffsll:
1411 case LibFunc_fls:
1412 case LibFunc_flsl:
1413 case LibFunc_flsll:
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));
1425 case LibFunc_abs:
1426 case LibFunc_labs:
1427 case LibFunc_llabs:
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());
1437 case LibFunc_sinpi:
1438 case LibFunc_cospi:
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());
1461 case LibFunc_cabs:
1462 case LibFunc_cabsf:
1463 case LibFunc_cabsl: {
1464 Type* RetTy = FTy.getReturnType();
1465 if (!RetTy->isFloatingPointTy())
1466 return false;
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.
1471 if (NumParams == 1)
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);
1477 else
1478 return false;
1480 case LibFunc::NumLibFuncs:
1481 break;
1484 llvm_unreachable("Invalid libfunc");
1487 bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
1488 LibFunc &F) const {
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) {
1530 switch (VecLib) {
1531 case Accelerate: {
1532 const VecDesc VecFuncs[] = {
1533 #define TLI_DEFINE_ACCELERATE_VECFUNCS
1534 #include "llvm/Analysis/VecFuncs.def"
1536 addVectorizableFunctions(VecFuncs);
1537 break;
1539 case MASSV: {
1540 const VecDesc VecFuncs[] = {
1541 #define TLI_DEFINE_MASSV_VECFUNCS
1542 #include "llvm/Analysis/VecFuncs.def"
1544 addVectorizableFunctions(VecFuncs);
1545 break;
1547 case SVML: {
1548 const VecDesc VecFuncs[] = {
1549 #define TLI_DEFINE_SVML_VECFUNCS
1550 #include "llvm/Analysis/VecFuncs.def"
1552 addVectorizableFunctions(VecFuncs);
1553 break;
1555 case NoLibrary:
1556 break;
1560 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
1561 funcName = sanitizeFunctionName(funcName);
1562 if (funcName.empty())
1563 return false;
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);
1573 if (F.empty())
1574 return 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;
1580 ++I;
1582 return StringRef();
1585 StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F,
1586 unsigned &VF) const {
1587 F = sanitizeFunctionName(F);
1588 if (F.empty())
1589 return F;
1591 std::vector<VecDesc>::const_iterator I =
1592 llvm::lower_bound(ScalarDescs, F, compareWithVectorFnName);
1593 if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F)
1594 return StringRef();
1595 VF = I->VectorizationFactor;
1596 return I->ScalarFnName;
1599 TargetLibraryInfo TargetLibraryAnalysis::run(Module &M,
1600 ModuleAnalysisManager &) {
1601 if (PresetInfoImpl)
1602 return TargetLibraryInfo(*PresetInfoImpl);
1604 return TargetLibraryInfo(lookupInfoImpl(Triple(M.getTargetTriple())));
1607 TargetLibraryInfo TargetLibraryAnalysis::run(Function &F,
1608 FunctionAnalysisManager &) {
1609 if (PresetInfoImpl)
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()];
1619 if (!Impl)
1620 Impl.reset(new TargetLibraryInfoImpl(T));
1622 return *Impl;
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();
1629 return 0;
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() {}