1 //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the TargetLibraryInfo class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Analysis/TargetLibraryInfo.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/InitializePasses.h"
17 #include "llvm/Support/CommandLine.h"
18 using namespace llvm;
19
20 static cl::opt<TargetLibraryInfoImpl::VectorLibrary> ClVectorLibrary(
21 "vector-library", cl::Hidden, cl::desc("Vector functions library"),
22 cl::init(TargetLibraryInfoImpl::NoLibrary),
23 cl::values(clEnumValN(TargetLibraryInfoImpl::NoLibrary, "none",
24 "No vector functions library"),
25 clEnumValN(TargetLibraryInfoImpl::Accelerate, "Accelerate",
26 "Accelerate framework"),
27 clEnumValN(TargetLibraryInfoImpl::MASSV, "MASSV",
28 "IBM MASS vector library"),
29 clEnumValN(TargetLibraryInfoImpl::SVML, "SVML",
30 "Intel SVML library")));
31
32 StringLiteral const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] =
33 {
34 #define TLI_DEFINE_STRING
35 #include "llvm/Analysis/TargetLibraryInfo.def"
36 };
37
hasSinCosPiStret(const Triple & T)38 static bool hasSinCosPiStret(const Triple &T) {
39 // Only Darwin variants have _stret versions of combined trig functions.
40 if (!T.isOSDarwin())
41 return false;
42
43 // The ABI is rather complicated on x86, so don't do anything special there.
44 if (T.getArch() == Triple::x86)
45 return false;
46
47 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
48 return false;
49
50 if (T.isiOS() && T.isOSVersionLT(7, 0))
51 return false;
52
53 return true;
54 }
55
hasBcmp(const Triple & TT)56 static bool hasBcmp(const Triple &TT) {
57 // Posix removed support from bcmp() in 2001, but the glibc and several
58 // implementations of the libc still have it.
59 if (TT.isOSLinux())
60 return TT.isGNUEnvironment() || TT.isMusl();
61 // Both NetBSD and OpenBSD are planning to remove the function. Windows does
62 // not have it.
63 return TT.isOSFreeBSD() || TT.isOSSolaris();
64 }
65
66 /// Initialize the set of available library functions based on the specified
67 /// target triple. This should be carefully written so that a missing target
68 /// triple gets a sane set of defaults.
initialize(TargetLibraryInfoImpl & TLI,const Triple & T,ArrayRef<StringLiteral> StandardNames)69 static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
70 ArrayRef<StringLiteral> StandardNames) {
71 // Verify that the StandardNames array is in alphabetical order.
72 assert(std::is_sorted(StandardNames.begin(), StandardNames.end(),
73 [](StringRef LHS, StringRef RHS) {
74 return LHS < RHS;
75 }) &&
76 "TargetLibraryInfoImpl function names must be sorted");
77
78 // Set IO unlocked variants as unavailable
79 // Set them as available per system below
80 TLI.setUnavailable(LibFunc_getchar_unlocked);
81 TLI.setUnavailable(LibFunc_putc_unlocked);
82 TLI.setUnavailable(LibFunc_putchar_unlocked);
83 TLI.setUnavailable(LibFunc_fputc_unlocked);
84 TLI.setUnavailable(LibFunc_fgetc_unlocked);
85 TLI.setUnavailable(LibFunc_fread_unlocked);
86 TLI.setUnavailable(LibFunc_fwrite_unlocked);
87 TLI.setUnavailable(LibFunc_fputs_unlocked);
88 TLI.setUnavailable(LibFunc_fgets_unlocked);
89
90 bool ShouldExtI32Param = false, ShouldExtI32Return = false,
91 ShouldSignExtI32Param = false;
92 // PowerPC64, Sparc64, SystemZ need signext/zeroext on i32 parameters and
93 // returns corresponding to C-level ints and unsigned ints.
94 if (T.isPPC64() || T.getArch() == Triple::sparcv9 ||
95 T.getArch() == Triple::systemz) {
96 ShouldExtI32Param = true;
97 ShouldExtI32Return = true;
98 }
99 // Mips, on the other hand, needs signext on i32 parameters corresponding
100 // to both signed and unsigned ints.
101 if (T.isMIPS()) {
102 ShouldSignExtI32Param = true;
103 }
104 TLI.setShouldExtI32Param(ShouldExtI32Param);
105 TLI.setShouldExtI32Return(ShouldExtI32Return);
106 TLI.setShouldSignExtI32Param(ShouldSignExtI32Param);
107
108 if (T.getArch() == Triple::r600 ||
109 T.getArch() == Triple::amdgcn)
110 TLI.disableAllFunctions();
111
112 // There are no library implementations of memcpy and memset for AMD gpus and
113 // these can be difficult to lower in the backend.
114 if (T.getArch() == Triple::r600 ||
115 T.getArch() == Triple::amdgcn) {
116 TLI.setUnavailable(LibFunc_memcpy);
117 TLI.setUnavailable(LibFunc_memset);
118 TLI.setUnavailable(LibFunc_memset_pattern16);
119 return;
120 }
121
122 // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
123 // All versions of watchOS support it.
124 if (T.isMacOSX()) {
125 // available IO unlocked variants on Mac OS X
126 TLI.setAvailable(LibFunc_getc_unlocked);
127 TLI.setAvailable(LibFunc_getchar_unlocked);
128 TLI.setAvailable(LibFunc_putc_unlocked);
129 TLI.setAvailable(LibFunc_putchar_unlocked);
130
131 if (T.isMacOSXVersionLT(10, 5))
132 TLI.setUnavailable(LibFunc_memset_pattern16);
133 } else if (T.isiOS()) {
134 if (T.isOSVersionLT(3, 0))
135 TLI.setUnavailable(LibFunc_memset_pattern16);
136 } else if (!T.isWatchOS()) {
137 TLI.setUnavailable(LibFunc_memset_pattern16);
138 }
139
140 if (!hasSinCosPiStret(T)) {
141 TLI.setUnavailable(LibFunc_sinpi);
142 TLI.setUnavailable(LibFunc_sinpif);
143 TLI.setUnavailable(LibFunc_cospi);
144 TLI.setUnavailable(LibFunc_cospif);
145 TLI.setUnavailable(LibFunc_sincospi_stret);
146 TLI.setUnavailable(LibFunc_sincospif_stret);
147 }
148
149 if (!hasBcmp(T))
150 TLI.setUnavailable(LibFunc_bcmp);
151
152 if (T.isMacOSX() && T.getArch() == Triple::x86 &&
153 !T.isMacOSXVersionLT(10, 7)) {
154 // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
155 // we don't care about) have two versions; on recent OSX, the one we want
156 // has a $UNIX2003 suffix. The two implementations are identical except
157 // for the return value in some edge cases. However, we don't want to
158 // generate code that depends on the old symbols.
159 TLI.setAvailableWithName(LibFunc_fwrite, "fwrite$UNIX2003");
160 TLI.setAvailableWithName(LibFunc_fputs, "fputs$UNIX2003");
161 }
162
163 // iprintf and friends are only available on XCore, TCE, and Emscripten.
164 if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce &&
165 T.getOS() != Triple::Emscripten) {
166 TLI.setUnavailable(LibFunc_iprintf);
167 TLI.setUnavailable(LibFunc_siprintf);
168 TLI.setUnavailable(LibFunc_fiprintf);
169 }
170
171 // __small_printf and friends are only available on Emscripten.
172 if (T.getOS() != Triple::Emscripten) {
173 TLI.setUnavailable(LibFunc_small_printf);
174 TLI.setUnavailable(LibFunc_small_sprintf);
175 TLI.setUnavailable(LibFunc_small_fprintf);
176 }
177
178 if (T.isOSWindows() && !T.isOSCygMing()) {
179 // XXX: The earliest documentation available at the moment is for VS2015/VC19:
180 // https://docs.microsoft.com/en-us/cpp/c-runtime-library/floating-point-support?view=vs-2015
181 // XXX: In order to use an MSVCRT older than VC19,
182 // the specific library version must be explicit in the target triple,
183 // e.g., x86_64-pc-windows-msvc18.
184 bool hasPartialC99 = true;
185 if (T.isKnownWindowsMSVCEnvironment()) {
186 unsigned Major, Minor, Micro;
187 T.getEnvironmentVersion(Major, Minor, Micro);
188 hasPartialC99 = (Major == 0 || Major >= 19);
189 }
190
191 // Latest targets support C89 math functions, in part.
192 bool isARM = (T.getArch() == Triple::aarch64 ||
193 T.getArch() == Triple::arm);
194 bool hasPartialFloat = (isARM ||
195 T.getArch() == Triple::x86_64);
196
197 // Win32 does not support float C89 math functions, in general.
198 if (!hasPartialFloat) {
199 TLI.setUnavailable(LibFunc_acosf);
200 TLI.setUnavailable(LibFunc_asinf);
201 TLI.setUnavailable(LibFunc_atan2f);
202 TLI.setUnavailable(LibFunc_atanf);
203 TLI.setUnavailable(LibFunc_ceilf);
204 TLI.setUnavailable(LibFunc_cosf);
205 TLI.setUnavailable(LibFunc_coshf);
206 TLI.setUnavailable(LibFunc_expf);
207 TLI.setUnavailable(LibFunc_floorf);
208 TLI.setUnavailable(LibFunc_fmodf);
209 TLI.setUnavailable(LibFunc_log10f);
210 TLI.setUnavailable(LibFunc_logf);
211 TLI.setUnavailable(LibFunc_modff);
212 TLI.setUnavailable(LibFunc_powf);
213 TLI.setUnavailable(LibFunc_sinf);
214 TLI.setUnavailable(LibFunc_sinhf);
215 TLI.setUnavailable(LibFunc_sqrtf);
216 TLI.setUnavailable(LibFunc_tanf);
217 TLI.setUnavailable(LibFunc_tanhf);
218 }
219 if (!isARM)
220 TLI.setUnavailable(LibFunc_fabsf);
221 TLI.setUnavailable(LibFunc_frexpf);
222 TLI.setUnavailable(LibFunc_ldexpf);
223
224 // Win32 does not support long double C89 math functions.
225 TLI.setUnavailable(LibFunc_acosl);
226 TLI.setUnavailable(LibFunc_asinl);
227 TLI.setUnavailable(LibFunc_atan2l);
228 TLI.setUnavailable(LibFunc_atanl);
229 TLI.setUnavailable(LibFunc_ceill);
230 TLI.setUnavailable(LibFunc_cosl);
231 TLI.setUnavailable(LibFunc_coshl);
232 TLI.setUnavailable(LibFunc_expl);
233 TLI.setUnavailable(LibFunc_fabsl);
234 TLI.setUnavailable(LibFunc_floorl);
235 TLI.setUnavailable(LibFunc_fmodl);
236 TLI.setUnavailable(LibFunc_frexpl);
237 TLI.setUnavailable(LibFunc_ldexpl);
238 TLI.setUnavailable(LibFunc_log10l);
239 TLI.setUnavailable(LibFunc_logl);
240 TLI.setUnavailable(LibFunc_modfl);
241 TLI.setUnavailable(LibFunc_powl);
242 TLI.setUnavailable(LibFunc_sinl);
243 TLI.setUnavailable(LibFunc_sinhl);
244 TLI.setUnavailable(LibFunc_sqrtl);
245 TLI.setUnavailable(LibFunc_tanl);
246 TLI.setUnavailable(LibFunc_tanhl);
247
248 // Win32 does not fully support C99 math functions.
249 if (!hasPartialC99) {
250 TLI.setUnavailable(LibFunc_acosh);
251 TLI.setUnavailable(LibFunc_acoshf);
252 TLI.setUnavailable(LibFunc_asinh);
253 TLI.setUnavailable(LibFunc_asinhf);
254 TLI.setUnavailable(LibFunc_atanh);
255 TLI.setUnavailable(LibFunc_atanhf);
256 TLI.setAvailableWithName(LibFunc_cabs, "_cabs");
257 TLI.setUnavailable(LibFunc_cabsf);
258 TLI.setUnavailable(LibFunc_cbrt);
259 TLI.setUnavailable(LibFunc_cbrtf);
260 TLI.setAvailableWithName(LibFunc_copysign, "_copysign");
261 TLI.setAvailableWithName(LibFunc_copysignf, "_copysignf");
262 TLI.setUnavailable(LibFunc_exp2);
263 TLI.setUnavailable(LibFunc_exp2f);
264 TLI.setUnavailable(LibFunc_expm1);
265 TLI.setUnavailable(LibFunc_expm1f);
266 TLI.setUnavailable(LibFunc_fmax);
267 TLI.setUnavailable(LibFunc_fmaxf);
268 TLI.setUnavailable(LibFunc_fmin);
269 TLI.setUnavailable(LibFunc_fminf);
270 TLI.setUnavailable(LibFunc_log1p);
271 TLI.setUnavailable(LibFunc_log1pf);
272 TLI.setUnavailable(LibFunc_log2);
273 TLI.setUnavailable(LibFunc_log2f);
274 TLI.setAvailableWithName(LibFunc_logb, "_logb");
275 if (hasPartialFloat)
276 TLI.setAvailableWithName(LibFunc_logbf, "_logbf");
277 else
278 TLI.setUnavailable(LibFunc_logbf);
279 TLI.setUnavailable(LibFunc_rint);
280 TLI.setUnavailable(LibFunc_rintf);
281 TLI.setUnavailable(LibFunc_round);
282 TLI.setUnavailable(LibFunc_roundf);
283 TLI.setUnavailable(LibFunc_trunc);
284 TLI.setUnavailable(LibFunc_truncf);
285 }
286
287 // Win32 does not support long double C99 math functions.
288 TLI.setUnavailable(LibFunc_acoshl);
289 TLI.setUnavailable(LibFunc_asinhl);
290 TLI.setUnavailable(LibFunc_atanhl);
291 TLI.setUnavailable(LibFunc_cabsl);
292 TLI.setUnavailable(LibFunc_cbrtl);
293 TLI.setUnavailable(LibFunc_copysignl);
294 TLI.setUnavailable(LibFunc_exp2l);
295 TLI.setUnavailable(LibFunc_expm1l);
296 TLI.setUnavailable(LibFunc_fmaxl);
297 TLI.setUnavailable(LibFunc_fminl);
298 TLI.setUnavailable(LibFunc_log1pl);
299 TLI.setUnavailable(LibFunc_log2l);
300 TLI.setUnavailable(LibFunc_logbl);
301 TLI.setUnavailable(LibFunc_nearbyintl);
302 TLI.setUnavailable(LibFunc_rintl);
303 TLI.setUnavailable(LibFunc_roundl);
304 TLI.setUnavailable(LibFunc_truncl);
305
306 // Win32 does not support these functions, but
307 // they are generally available on POSIX-compliant systems.
308 TLI.setUnavailable(LibFunc_access);
309 TLI.setUnavailable(LibFunc_bcmp);
310 TLI.setUnavailable(LibFunc_bcopy);
311 TLI.setUnavailable(LibFunc_bzero);
312 TLI.setUnavailable(LibFunc_chmod);
313 TLI.setUnavailable(LibFunc_chown);
314 TLI.setUnavailable(LibFunc_closedir);
315 TLI.setUnavailable(LibFunc_ctermid);
316 TLI.setUnavailable(LibFunc_fdopen);
317 TLI.setUnavailable(LibFunc_ffs);
318 TLI.setUnavailable(LibFunc_fileno);
319 TLI.setUnavailable(LibFunc_flockfile);
320 TLI.setUnavailable(LibFunc_fseeko);
321 TLI.setUnavailable(LibFunc_fstat);
322 TLI.setUnavailable(LibFunc_fstatvfs);
323 TLI.setUnavailable(LibFunc_ftello);
324 TLI.setUnavailable(LibFunc_ftrylockfile);
325 TLI.setUnavailable(LibFunc_funlockfile);
326 TLI.setUnavailable(LibFunc_getitimer);
327 TLI.setUnavailable(LibFunc_getlogin_r);
328 TLI.setUnavailable(LibFunc_getpwnam);
329 TLI.setUnavailable(LibFunc_gettimeofday);
330 TLI.setUnavailable(LibFunc_htonl);
331 TLI.setUnavailable(LibFunc_htons);
332 TLI.setUnavailable(LibFunc_lchown);
333 TLI.setUnavailable(LibFunc_lstat);
334 TLI.setUnavailable(LibFunc_memccpy);
335 TLI.setUnavailable(LibFunc_mkdir);
336 TLI.setUnavailable(LibFunc_ntohl);
337 TLI.setUnavailable(LibFunc_ntohs);
338 TLI.setUnavailable(LibFunc_open);
339 TLI.setUnavailable(LibFunc_opendir);
340 TLI.setUnavailable(LibFunc_pclose);
341 TLI.setUnavailable(LibFunc_popen);
342 TLI.setUnavailable(LibFunc_pread);
343 TLI.setUnavailable(LibFunc_pwrite);
344 TLI.setUnavailable(LibFunc_read);
345 TLI.setUnavailable(LibFunc_readlink);
346 TLI.setUnavailable(LibFunc_realpath);
347 TLI.setUnavailable(LibFunc_rmdir);
348 TLI.setUnavailable(LibFunc_setitimer);
349 TLI.setUnavailable(LibFunc_stat);
350 TLI.setUnavailable(LibFunc_statvfs);
351 TLI.setUnavailable(LibFunc_stpcpy);
352 TLI.setUnavailable(LibFunc_stpncpy);
353 TLI.setUnavailable(LibFunc_strcasecmp);
354 TLI.setUnavailable(LibFunc_strncasecmp);
355 TLI.setUnavailable(LibFunc_times);
356 TLI.setUnavailable(LibFunc_uname);
357 TLI.setUnavailable(LibFunc_unlink);
358 TLI.setUnavailable(LibFunc_unsetenv);
359 TLI.setUnavailable(LibFunc_utime);
360 TLI.setUnavailable(LibFunc_utimes);
361 TLI.setUnavailable(LibFunc_write);
362 }
363
364 switch (T.getOS()) {
365 case Triple::MacOSX:
366 // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
367 // and their names are __exp10 and __exp10f. exp10l is not available on
368 // OS X or iOS.
369 TLI.setUnavailable(LibFunc_exp10l);
370 if (T.isMacOSXVersionLT(10, 9)) {
371 TLI.setUnavailable(LibFunc_exp10);
372 TLI.setUnavailable(LibFunc_exp10f);
373 } else {
374 TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
375 TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
376 }
377 break;
378 case Triple::IOS:
379 case Triple::TvOS:
380 case Triple::WatchOS:
381 TLI.setUnavailable(LibFunc_exp10l);
382 if (!T.isWatchOS() &&
383 (T.isOSVersionLT(7, 0) || (T.isOSVersionLT(9, 0) && T.isX86()))) {
384 TLI.setUnavailable(LibFunc_exp10);
385 TLI.setUnavailable(LibFunc_exp10f);
386 } else {
387 TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
388 TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
389 }
390 break;
391 case Triple::Linux:
392 // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
393 // buggy prior to glibc version 2.18. Until this version is widely deployed
394 // or we have a reasonable detection strategy, we cannot use exp10 reliably
395 // on Linux.
396 //
397 // Fall through to disable all of them.
398 LLVM_FALLTHROUGH;
399 default:
400 TLI.setUnavailable(LibFunc_exp10);
401 TLI.setUnavailable(LibFunc_exp10f);
402 TLI.setUnavailable(LibFunc_exp10l);
403 }
404
405 // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
406 // Linux (GLIBC):
407 // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
408 // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
409 // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
410 switch (T.getOS()) {
411 case Triple::Darwin:
412 case Triple::MacOSX:
413 case Triple::IOS:
414 case Triple::TvOS:
415 case Triple::WatchOS:
416 case Triple::FreeBSD:
417 case Triple::Linux:
418 break;
419 default:
420 TLI.setUnavailable(LibFunc_ffsl);
421 }
422
423 // ffsll is available on at least FreeBSD and Linux (GLIBC):
424 // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
425 // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
426 switch (T.getOS()) {
427 case Triple::Darwin:
428 case Triple::MacOSX:
429 case Triple::IOS:
430 case Triple::TvOS:
431 case Triple::WatchOS:
432 case Triple::FreeBSD:
433 case Triple::Linux:
434 break;
435 default:
436 TLI.setUnavailable(LibFunc_ffsll);
437 }
438
439 // The following functions are available on at least FreeBSD:
440 // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
441 // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
442 // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
443 if (!T.isOSFreeBSD()) {
444 TLI.setUnavailable(LibFunc_fls);
445 TLI.setUnavailable(LibFunc_flsl);
446 TLI.setUnavailable(LibFunc_flsll);
447 }
448
449 // The following functions are only available on GNU/Linux (using glibc).
450 // Linux variants without glibc (eg: bionic, musl) may have some subset.
451 if (!T.isOSLinux() || !T.isGNUEnvironment()) {
452 TLI.setUnavailable(LibFunc_dunder_strdup);
453 TLI.setUnavailable(LibFunc_dunder_strtok_r);
454 TLI.setUnavailable(LibFunc_dunder_isoc99_scanf);
455 TLI.setUnavailable(LibFunc_dunder_isoc99_sscanf);
456 TLI.setUnavailable(LibFunc_under_IO_getc);
457 TLI.setUnavailable(LibFunc_under_IO_putc);
458 // But, Android and musl have memalign.
459 if (!T.isAndroid() && !T.isMusl())
460 TLI.setUnavailable(LibFunc_memalign);
461 TLI.setUnavailable(LibFunc_fopen64);
462 TLI.setUnavailable(LibFunc_fseeko64);
463 TLI.setUnavailable(LibFunc_fstat64);
464 TLI.setUnavailable(LibFunc_fstatvfs64);
465 TLI.setUnavailable(LibFunc_ftello64);
466 TLI.setUnavailable(LibFunc_lstat64);
467 TLI.setUnavailable(LibFunc_open64);
468 TLI.setUnavailable(LibFunc_stat64);
469 TLI.setUnavailable(LibFunc_statvfs64);
470 TLI.setUnavailable(LibFunc_tmpfile64);
471
472 // Relaxed math functions are included in math-finite.h on Linux (GLIBC).
473 // Note that math-finite.h is no longer supported by top-of-tree GLIBC,
474 // so we keep these functions around just so that they're recognized by
475 // the ConstantFolder.
476 TLI.setUnavailable(LibFunc_acos_finite);
477 TLI.setUnavailable(LibFunc_acosf_finite);
478 TLI.setUnavailable(LibFunc_acosl_finite);
479 TLI.setUnavailable(LibFunc_acosh_finite);
480 TLI.setUnavailable(LibFunc_acoshf_finite);
481 TLI.setUnavailable(LibFunc_acoshl_finite);
482 TLI.setUnavailable(LibFunc_asin_finite);
483 TLI.setUnavailable(LibFunc_asinf_finite);
484 TLI.setUnavailable(LibFunc_asinl_finite);
485 TLI.setUnavailable(LibFunc_atan2_finite);
486 TLI.setUnavailable(LibFunc_atan2f_finite);
487 TLI.setUnavailable(LibFunc_atan2l_finite);
488 TLI.setUnavailable(LibFunc_atanh_finite);
489 TLI.setUnavailable(LibFunc_atanhf_finite);
490 TLI.setUnavailable(LibFunc_atanhl_finite);
491 TLI.setUnavailable(LibFunc_cosh_finite);
492 TLI.setUnavailable(LibFunc_coshf_finite);
493 TLI.setUnavailable(LibFunc_coshl_finite);
494 TLI.setUnavailable(LibFunc_exp10_finite);
495 TLI.setUnavailable(LibFunc_exp10f_finite);
496 TLI.setUnavailable(LibFunc_exp10l_finite);
497 TLI.setUnavailable(LibFunc_exp2_finite);
498 TLI.setUnavailable(LibFunc_exp2f_finite);
499 TLI.setUnavailable(LibFunc_exp2l_finite);
500 TLI.setUnavailable(LibFunc_exp_finite);
501 TLI.setUnavailable(LibFunc_expf_finite);
502 TLI.setUnavailable(LibFunc_expl_finite);
503 TLI.setUnavailable(LibFunc_log10_finite);
504 TLI.setUnavailable(LibFunc_log10f_finite);
505 TLI.setUnavailable(LibFunc_log10l_finite);
506 TLI.setUnavailable(LibFunc_log2_finite);
507 TLI.setUnavailable(LibFunc_log2f_finite);
508 TLI.setUnavailable(LibFunc_log2l_finite);
509 TLI.setUnavailable(LibFunc_log_finite);
510 TLI.setUnavailable(LibFunc_logf_finite);
511 TLI.setUnavailable(LibFunc_logl_finite);
512 TLI.setUnavailable(LibFunc_pow_finite);
513 TLI.setUnavailable(LibFunc_powf_finite);
514 TLI.setUnavailable(LibFunc_powl_finite);
515 TLI.setUnavailable(LibFunc_sinh_finite);
516 TLI.setUnavailable(LibFunc_sinhf_finite);
517 TLI.setUnavailable(LibFunc_sinhl_finite);
518 }
519
520 if ((T.isOSLinux() && T.isGNUEnvironment()) ||
521 (T.isAndroid() && !T.isAndroidVersionLT(28))) {
522 // available IO unlocked variants on GNU/Linux and Android P or later
523 TLI.setAvailable(LibFunc_getc_unlocked);
524 TLI.setAvailable(LibFunc_getchar_unlocked);
525 TLI.setAvailable(LibFunc_putc_unlocked);
526 TLI.setAvailable(LibFunc_putchar_unlocked);
527 TLI.setAvailable(LibFunc_fputc_unlocked);
528 TLI.setAvailable(LibFunc_fgetc_unlocked);
529 TLI.setAvailable(LibFunc_fread_unlocked);
530 TLI.setAvailable(LibFunc_fwrite_unlocked);
531 TLI.setAvailable(LibFunc_fputs_unlocked);
532 TLI.setAvailable(LibFunc_fgets_unlocked);
533 }
534
535 // As currently implemented in clang, NVPTX code has no standard library to
536 // speak of. Headers provide a standard-ish library implementation, but many
537 // of the signatures are wrong -- for example, many libm functions are not
538 // extern "C".
539 //
540 // libdevice, an IR library provided by nvidia, is linked in by the front-end,
541 // but only used functions are provided to llvm. Moreover, most of the
542 // functions in libdevice don't map precisely to standard library functions.
543 //
544 // FIXME: Having no standard library prevents e.g. many fastmath
545 // optimizations, so this situation should be fixed.
546 if (T.isNVPTX()) {
547 TLI.disableAllFunctions();
548 TLI.setAvailable(LibFunc_nvvm_reflect);
549 } else {
550 TLI.setUnavailable(LibFunc_nvvm_reflect);
551 }
552
553 TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary);
554 }
555
TargetLibraryInfoImpl()556 TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
557 // Default to everything being available.
558 memset(AvailableArray, -1, sizeof(AvailableArray));
559
560 initialize(*this, Triple(), StandardNames);
561 }
562
TargetLibraryInfoImpl(const Triple & T)563 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
564 // Default to everything being available.
565 memset(AvailableArray, -1, sizeof(AvailableArray));
566
567 initialize(*this, T, StandardNames);
568 }
569
TargetLibraryInfoImpl(const TargetLibraryInfoImpl & TLI)570 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
571 : CustomNames(TLI.CustomNames), ShouldExtI32Param(TLI.ShouldExtI32Param),
572 ShouldExtI32Return(TLI.ShouldExtI32Return),
573 ShouldSignExtI32Param(TLI.ShouldSignExtI32Param) {
574 memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
575 VectorDescs = TLI.VectorDescs;
576 ScalarDescs = TLI.ScalarDescs;
577 }
578
TargetLibraryInfoImpl(TargetLibraryInfoImpl && TLI)579 TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
580 : CustomNames(std::move(TLI.CustomNames)),
581 ShouldExtI32Param(TLI.ShouldExtI32Param),
582 ShouldExtI32Return(TLI.ShouldExtI32Return),
583 ShouldSignExtI32Param(TLI.ShouldSignExtI32Param) {
584 std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
585 AvailableArray);
586 VectorDescs = TLI.VectorDescs;
587 ScalarDescs = TLI.ScalarDescs;
588 }
589
operator =(const TargetLibraryInfoImpl & TLI)590 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
591 CustomNames = TLI.CustomNames;
592 ShouldExtI32Param = TLI.ShouldExtI32Param;
593 ShouldExtI32Return = TLI.ShouldExtI32Return;
594 ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
595 memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
596 return *this;
597 }
598
operator =(TargetLibraryInfoImpl && TLI)599 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
600 CustomNames = std::move(TLI.CustomNames);
601 ShouldExtI32Param = TLI.ShouldExtI32Param;
602 ShouldExtI32Return = TLI.ShouldExtI32Return;
603 ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
604 std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
605 AvailableArray);
606 return *this;
607 }
608
sanitizeFunctionName(StringRef funcName)609 static StringRef sanitizeFunctionName(StringRef funcName) {
610 // Filter out empty names and names containing null bytes, those can't be in
611 // our table.
612 if (funcName.empty() || funcName.find('\0') != StringRef::npos)
613 return StringRef();
614
615 // Check for \01 prefix that is used to mangle __asm declarations and
616 // strip it if present.
617 return GlobalValue::dropLLVMManglingEscape(funcName);
618 }
619
getLibFunc(StringRef funcName,LibFunc & F) const620 bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName, LibFunc &F) const {
621 funcName = sanitizeFunctionName(funcName);
622 if (funcName.empty())
623 return false;
624
625 const auto *Start = std::begin(StandardNames);
626 const auto *End = std::end(StandardNames);
627 const auto *I = std::lower_bound(Start, End, funcName);
628 if (I != End && *I == funcName) {
629 F = (LibFunc)(I - Start);
630 return true;
631 }
632 return false;
633 }
634
isValidProtoForLibFunc(const FunctionType & FTy,LibFunc F,const DataLayout * DL) const635 bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
636 LibFunc F,
637 const DataLayout *DL) const {
638 LLVMContext &Ctx = FTy.getContext();
639 Type *PCharTy = Type::getInt8PtrTy(Ctx);
640 Type *SizeTTy = DL ? DL->getIntPtrType(Ctx, /*AS=*/0) : nullptr;
641 auto IsSizeTTy = [SizeTTy](Type *Ty) {
642 return SizeTTy ? Ty == SizeTTy : Ty->isIntegerTy();
643 };
644 unsigned NumParams = FTy.getNumParams();
645
646 switch (F) {
647 case LibFunc_execl:
648 case LibFunc_execlp:
649 case LibFunc_execle:
650 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
651 FTy.getParamType(1)->isPointerTy() &&
652 FTy.getReturnType()->isIntegerTy(32));
653 case LibFunc_execv:
654 case LibFunc_execvp:
655 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
656 FTy.getParamType(1)->isPointerTy() &&
657 FTy.getReturnType()->isIntegerTy(32));
658 case LibFunc_execvP:
659 case LibFunc_execvpe:
660 case LibFunc_execve:
661 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
662 FTy.getParamType(1)->isPointerTy() &&
663 FTy.getParamType(2)->isPointerTy() &&
664 FTy.getReturnType()->isIntegerTy(32));
665 case LibFunc_strlen:
666 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
667 FTy.getReturnType()->isIntegerTy());
668
669 case LibFunc_strchr:
670 case LibFunc_strrchr:
671 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
672 FTy.getParamType(0) == FTy.getReturnType() &&
673 FTy.getParamType(1)->isIntegerTy());
674
675 case LibFunc_strtol:
676 case LibFunc_strtod:
677 case LibFunc_strtof:
678 case LibFunc_strtoul:
679 case LibFunc_strtoll:
680 case LibFunc_strtold:
681 case LibFunc_strtoull:
682 return ((NumParams == 2 || NumParams == 3) &&
683 FTy.getParamType(0)->isPointerTy() &&
684 FTy.getParamType(1)->isPointerTy());
685 case LibFunc_strcat_chk:
686 --NumParams;
687 if (!IsSizeTTy(FTy.getParamType(NumParams)))
688 return false;
689 LLVM_FALLTHROUGH;
690 case LibFunc_strcat:
691 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
692 FTy.getParamType(0) == FTy.getReturnType() &&
693 FTy.getParamType(1) == FTy.getReturnType());
694
695 case LibFunc_strncat_chk:
696 --NumParams;
697 if (!IsSizeTTy(FTy.getParamType(NumParams)))
698 return false;
699 LLVM_FALLTHROUGH;
700 case LibFunc_strncat:
701 return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
702 FTy.getParamType(0) == FTy.getReturnType() &&
703 FTy.getParamType(1) == FTy.getReturnType() &&
704 IsSizeTTy(FTy.getParamType(2)));
705
706 case LibFunc_strcpy_chk:
707 case LibFunc_stpcpy_chk:
708 --NumParams;
709 if (!IsSizeTTy(FTy.getParamType(NumParams)))
710 return false;
711 LLVM_FALLTHROUGH;
712 case LibFunc_strcpy:
713 case LibFunc_stpcpy:
714 return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(0) &&
715 FTy.getParamType(0) == FTy.getParamType(1) &&
716 FTy.getParamType(0) == PCharTy);
717
718 case LibFunc_strlcat_chk:
719 case LibFunc_strlcpy_chk:
720 --NumParams;
721 if (!IsSizeTTy(FTy.getParamType(NumParams)))
722 return false;
723 LLVM_FALLTHROUGH;
724 case LibFunc_strlcat:
725 case LibFunc_strlcpy:
726 return NumParams == 3 && IsSizeTTy(FTy.getReturnType()) &&
727 FTy.getParamType(0)->isPointerTy() &&
728 FTy.getParamType(1)->isPointerTy() &&
729 IsSizeTTy(FTy.getParamType(2));
730
731 case LibFunc_strncpy_chk:
732 case LibFunc_stpncpy_chk:
733 --NumParams;
734 if (!IsSizeTTy(FTy.getParamType(NumParams)))
735 return false;
736 LLVM_FALLTHROUGH;
737 case LibFunc_strncpy:
738 case LibFunc_stpncpy:
739 return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
740 FTy.getParamType(0) == FTy.getParamType(1) &&
741 FTy.getParamType(0) == PCharTy &&
742 IsSizeTTy(FTy.getParamType(2)));
743
744 case LibFunc_strxfrm:
745 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
746 FTy.getParamType(1)->isPointerTy());
747
748 case LibFunc_strcmp:
749 return (NumParams == 2 && FTy.getReturnType()->isIntegerTy(32) &&
750 FTy.getParamType(0)->isPointerTy() &&
751 FTy.getParamType(0) == FTy.getParamType(1));
752
753 case LibFunc_strncmp:
754 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
755 FTy.getParamType(0)->isPointerTy() &&
756 FTy.getParamType(0) == FTy.getParamType(1) &&
757 IsSizeTTy(FTy.getParamType(2)));
758
759 case LibFunc_strspn:
760 case LibFunc_strcspn:
761 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
762 FTy.getParamType(0) == FTy.getParamType(1) &&
763 FTy.getReturnType()->isIntegerTy());
764
765 case LibFunc_strcoll:
766 case LibFunc_strcasecmp:
767 case LibFunc_strncasecmp:
768 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
769 FTy.getParamType(1)->isPointerTy());
770
771 case LibFunc_strstr:
772 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
773 FTy.getParamType(0)->isPointerTy() &&
774 FTy.getParamType(1)->isPointerTy());
775
776 case LibFunc_strpbrk:
777 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
778 FTy.getReturnType() == FTy.getParamType(0) &&
779 FTy.getParamType(0) == FTy.getParamType(1));
780
781 case LibFunc_strtok:
782 case LibFunc_strtok_r:
783 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
784 case LibFunc_scanf:
785 case LibFunc_setbuf:
786 case LibFunc_setvbuf:
787 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
788 case LibFunc_strdup:
789 case LibFunc_strndup:
790 return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
791 FTy.getParamType(0)->isPointerTy());
792 case LibFunc_sscanf:
793 case LibFunc_stat:
794 case LibFunc_statvfs:
795 case LibFunc_siprintf:
796 case LibFunc_small_sprintf:
797 case LibFunc_sprintf:
798 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
799 FTy.getParamType(1)->isPointerTy() &&
800 FTy.getReturnType()->isIntegerTy(32));
801
802 case LibFunc_sprintf_chk:
803 return NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
804 FTy.getParamType(1)->isIntegerTy(32) &&
805 IsSizeTTy(FTy.getParamType(2)) &&
806 FTy.getParamType(3)->isPointerTy() &&
807 FTy.getReturnType()->isIntegerTy(32);
808
809 case LibFunc_snprintf:
810 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
811 FTy.getParamType(2)->isPointerTy() &&
812 FTy.getReturnType()->isIntegerTy(32));
813
814 case LibFunc_snprintf_chk:
815 return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
816 IsSizeTTy(FTy.getParamType(1)) &&
817 FTy.getParamType(2)->isIntegerTy(32) &&
818 IsSizeTTy(FTy.getParamType(3)) &&
819 FTy.getParamType(4)->isPointerTy() &&
820 FTy.getReturnType()->isIntegerTy(32);
821
822 case LibFunc_setitimer:
823 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
824 FTy.getParamType(2)->isPointerTy());
825 case LibFunc_system:
826 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
827 case LibFunc_malloc:
828 return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
829 case LibFunc_memcmp:
830 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
831 FTy.getParamType(0)->isPointerTy() &&
832 FTy.getParamType(1)->isPointerTy());
833
834 case LibFunc_memchr:
835 case LibFunc_memrchr:
836 return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
837 FTy.getReturnType() == FTy.getParamType(0) &&
838 FTy.getParamType(1)->isIntegerTy(32) &&
839 IsSizeTTy(FTy.getParamType(2)));
840 case LibFunc_modf:
841 case LibFunc_modff:
842 case LibFunc_modfl:
843 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
844
845 case LibFunc_memcpy_chk:
846 case LibFunc_memmove_chk:
847 --NumParams;
848 if (!IsSizeTTy(FTy.getParamType(NumParams)))
849 return false;
850 LLVM_FALLTHROUGH;
851 case LibFunc_memcpy:
852 case LibFunc_mempcpy:
853 case LibFunc_memmove:
854 return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
855 FTy.getParamType(0)->isPointerTy() &&
856 FTy.getParamType(1)->isPointerTy() &&
857 IsSizeTTy(FTy.getParamType(2)));
858
859 case LibFunc_memset_chk:
860 --NumParams;
861 if (!IsSizeTTy(FTy.getParamType(NumParams)))
862 return false;
863 LLVM_FALLTHROUGH;
864 case LibFunc_memset:
865 return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
866 FTy.getParamType(0)->isPointerTy() &&
867 FTy.getParamType(1)->isIntegerTy() &&
868 IsSizeTTy(FTy.getParamType(2)));
869
870 case LibFunc_memccpy_chk:
871 --NumParams;
872 if (!IsSizeTTy(FTy.getParamType(NumParams)))
873 return false;
874 LLVM_FALLTHROUGH;
875 case LibFunc_memccpy:
876 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
877 case LibFunc_memalign:
878 return (FTy.getReturnType()->isPointerTy());
879 case LibFunc_realloc:
880 case LibFunc_reallocf:
881 return (NumParams == 2 && FTy.getReturnType() == PCharTy &&
882 FTy.getParamType(0) == FTy.getReturnType() &&
883 IsSizeTTy(FTy.getParamType(1)));
884 case LibFunc_read:
885 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
886 case LibFunc_rewind:
887 case LibFunc_rmdir:
888 case LibFunc_remove:
889 case LibFunc_realpath:
890 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
891 case LibFunc_rename:
892 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
893 FTy.getParamType(1)->isPointerTy());
894 case LibFunc_readlink:
895 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
896 FTy.getParamType(1)->isPointerTy());
897 case LibFunc_write:
898 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
899 case LibFunc_bcopy:
900 case LibFunc_bcmp:
901 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
902 FTy.getParamType(1)->isPointerTy());
903 case LibFunc_bzero:
904 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
905 case LibFunc_calloc:
906 return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
907
908 case LibFunc_atof:
909 case LibFunc_atoi:
910 case LibFunc_atol:
911 case LibFunc_atoll:
912 case LibFunc_ferror:
913 case LibFunc_getenv:
914 case LibFunc_getpwnam:
915 case LibFunc_iprintf:
916 case LibFunc_small_printf:
917 case LibFunc_pclose:
918 case LibFunc_perror:
919 case LibFunc_printf:
920 case LibFunc_puts:
921 case LibFunc_uname:
922 case LibFunc_under_IO_getc:
923 case LibFunc_unlink:
924 case LibFunc_unsetenv:
925 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
926
927 case LibFunc_access:
928 case LibFunc_chmod:
929 case LibFunc_chown:
930 case LibFunc_clearerr:
931 case LibFunc_closedir:
932 case LibFunc_ctermid:
933 case LibFunc_fclose:
934 case LibFunc_feof:
935 case LibFunc_fflush:
936 case LibFunc_fgetc:
937 case LibFunc_fgetc_unlocked:
938 case LibFunc_fileno:
939 case LibFunc_flockfile:
940 case LibFunc_free:
941 case LibFunc_fseek:
942 case LibFunc_fseeko64:
943 case LibFunc_fseeko:
944 case LibFunc_fsetpos:
945 case LibFunc_ftell:
946 case LibFunc_ftello64:
947 case LibFunc_ftello:
948 case LibFunc_ftrylockfile:
949 case LibFunc_funlockfile:
950 case LibFunc_getc:
951 case LibFunc_getc_unlocked:
952 case LibFunc_getlogin_r:
953 case LibFunc_mkdir:
954 case LibFunc_mktime:
955 case LibFunc_times:
956 return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
957
958 case LibFunc_fopen:
959 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
960 FTy.getParamType(0)->isPointerTy() &&
961 FTy.getParamType(1)->isPointerTy());
962 case LibFunc_fork:
963 return (NumParams == 0 && FTy.getReturnType()->isIntegerTy(32));
964 case LibFunc_fdopen:
965 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
966 FTy.getParamType(1)->isPointerTy());
967 case LibFunc_fputc:
968 case LibFunc_fputc_unlocked:
969 case LibFunc_fstat:
970 case LibFunc_frexp:
971 case LibFunc_frexpf:
972 case LibFunc_frexpl:
973 case LibFunc_fstatvfs:
974 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
975 case LibFunc_fgets:
976 case LibFunc_fgets_unlocked:
977 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
978 FTy.getParamType(2)->isPointerTy());
979 case LibFunc_fread:
980 case LibFunc_fread_unlocked:
981 return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
982 FTy.getParamType(3)->isPointerTy());
983 case LibFunc_fwrite:
984 case LibFunc_fwrite_unlocked:
985 return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() &&
986 FTy.getParamType(0)->isPointerTy() &&
987 FTy.getParamType(1)->isIntegerTy() &&
988 FTy.getParamType(2)->isIntegerTy() &&
989 FTy.getParamType(3)->isPointerTy());
990 case LibFunc_fputs:
991 case LibFunc_fputs_unlocked:
992 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
993 FTy.getParamType(1)->isPointerTy());
994 case LibFunc_fscanf:
995 case LibFunc_fiprintf:
996 case LibFunc_small_fprintf:
997 case LibFunc_fprintf:
998 return (NumParams >= 2 && FTy.getReturnType()->isIntegerTy() &&
999 FTy.getParamType(0)->isPointerTy() &&
1000 FTy.getParamType(1)->isPointerTy());
1001 case LibFunc_fgetpos:
1002 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
1003 FTy.getParamType(1)->isPointerTy());
1004 case LibFunc_getchar:
1005 case LibFunc_getchar_unlocked:
1006 return (NumParams == 0 && FTy.getReturnType()->isIntegerTy());
1007 case LibFunc_gets:
1008 return (NumParams == 1 && FTy.getParamType(0) == PCharTy);
1009 case LibFunc_getitimer:
1010 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1011 case LibFunc_ungetc:
1012 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1013 case LibFunc_utime:
1014 case LibFunc_utimes:
1015 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1016 FTy.getParamType(1)->isPointerTy());
1017 case LibFunc_putc:
1018 case LibFunc_putc_unlocked:
1019 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1020 case LibFunc_pread:
1021 case LibFunc_pwrite:
1022 return (NumParams == 4 && FTy.getParamType(1)->isPointerTy());
1023 case LibFunc_popen:
1024 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1025 FTy.getParamType(0)->isPointerTy() &&
1026 FTy.getParamType(1)->isPointerTy());
1027 case LibFunc_vscanf:
1028 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1029 case LibFunc_vsscanf:
1030 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
1031 FTy.getParamType(2)->isPointerTy());
1032 case LibFunc_vfscanf:
1033 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
1034 FTy.getParamType(2)->isPointerTy());
1035 case LibFunc_valloc:
1036 return (FTy.getReturnType()->isPointerTy());
1037 case LibFunc_vprintf:
1038 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
1039 case LibFunc_vfprintf:
1040 case LibFunc_vsprintf:
1041 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
1042 FTy.getParamType(1)->isPointerTy());
1043 case LibFunc_vsprintf_chk:
1044 return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
1045 FTy.getParamType(1)->isIntegerTy(32) &&
1046 IsSizeTTy(FTy.getParamType(2)) && FTy.getParamType(3)->isPointerTy();
1047 case LibFunc_vsnprintf:
1048 return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
1049 FTy.getParamType(2)->isPointerTy());
1050 case LibFunc_vsnprintf_chk:
1051 return NumParams == 6 && FTy.getParamType(0)->isPointerTy() &&
1052 FTy.getParamType(2)->isIntegerTy(32) &&
1053 IsSizeTTy(FTy.getParamType(3)) && FTy.getParamType(4)->isPointerTy();
1054 case LibFunc_open:
1055 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
1056 case LibFunc_opendir:
1057 return (NumParams == 1 && FTy.getReturnType()->isPointerTy() &&
1058 FTy.getParamType(0)->isPointerTy());
1059 case LibFunc_tmpfile:
1060 return (FTy.getReturnType()->isPointerTy());
1061 case LibFunc_htonl:
1062 case LibFunc_ntohl:
1063 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1064 FTy.getReturnType() == FTy.getParamType(0));
1065 case LibFunc_htons:
1066 case LibFunc_ntohs:
1067 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(16) &&
1068 FTy.getReturnType() == FTy.getParamType(0));
1069 case LibFunc_lstat:
1070 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1071 FTy.getParamType(1)->isPointerTy());
1072 case LibFunc_lchown:
1073 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
1074 case LibFunc_qsort:
1075 return (NumParams == 4 && FTy.getParamType(3)->isPointerTy());
1076 case LibFunc_dunder_strdup:
1077 case LibFunc_dunder_strndup:
1078 return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
1079 FTy.getParamType(0)->isPointerTy());
1080 case LibFunc_dunder_strtok_r:
1081 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
1082 case LibFunc_under_IO_putc:
1083 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1084 case LibFunc_dunder_isoc99_scanf:
1085 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
1086 case LibFunc_stat64:
1087 case LibFunc_lstat64:
1088 case LibFunc_statvfs64:
1089 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1090 FTy.getParamType(1)->isPointerTy());
1091 case LibFunc_dunder_isoc99_sscanf:
1092 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
1093 FTy.getParamType(1)->isPointerTy());
1094 case LibFunc_fopen64:
1095 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
1096 FTy.getParamType(0)->isPointerTy() &&
1097 FTy.getParamType(1)->isPointerTy());
1098 case LibFunc_tmpfile64:
1099 return (FTy.getReturnType()->isPointerTy());
1100 case LibFunc_fstat64:
1101 case LibFunc_fstatvfs64:
1102 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
1103 case LibFunc_open64:
1104 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
1105 case LibFunc_gettimeofday:
1106 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1107 FTy.getParamType(1)->isPointerTy());
1108
1109 // new(unsigned int);
1110 case LibFunc_Znwj:
1111 // new(unsigned long);
1112 case LibFunc_Znwm:
1113 // new[](unsigned int);
1114 case LibFunc_Znaj:
1115 // new[](unsigned long);
1116 case LibFunc_Znam:
1117 // new(unsigned int);
1118 case LibFunc_msvc_new_int:
1119 // new(unsigned long long);
1120 case LibFunc_msvc_new_longlong:
1121 // new[](unsigned int);
1122 case LibFunc_msvc_new_array_int:
1123 // new[](unsigned long long);
1124 case LibFunc_msvc_new_array_longlong:
1125 return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
1126
1127 // new(unsigned int, nothrow);
1128 case LibFunc_ZnwjRKSt9nothrow_t:
1129 // new(unsigned long, nothrow);
1130 case LibFunc_ZnwmRKSt9nothrow_t:
1131 // new[](unsigned int, nothrow);
1132 case LibFunc_ZnajRKSt9nothrow_t:
1133 // new[](unsigned long, nothrow);
1134 case LibFunc_ZnamRKSt9nothrow_t:
1135 // new(unsigned int, nothrow);
1136 case LibFunc_msvc_new_int_nothrow:
1137 // new(unsigned long long, nothrow);
1138 case LibFunc_msvc_new_longlong_nothrow:
1139 // new[](unsigned int, nothrow);
1140 case LibFunc_msvc_new_array_int_nothrow:
1141 // new[](unsigned long long, nothrow);
1142 case LibFunc_msvc_new_array_longlong_nothrow:
1143 // new(unsigned int, align_val_t)
1144 case LibFunc_ZnwjSt11align_val_t:
1145 // new(unsigned long, align_val_t)
1146 case LibFunc_ZnwmSt11align_val_t:
1147 // new[](unsigned int, align_val_t)
1148 case LibFunc_ZnajSt11align_val_t:
1149 // new[](unsigned long, align_val_t)
1150 case LibFunc_ZnamSt11align_val_t:
1151 return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
1152
1153 // new(unsigned int, align_val_t, nothrow)
1154 case LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t:
1155 // new(unsigned long, align_val_t, nothrow)
1156 case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t:
1157 // new[](unsigned int, align_val_t, nothrow)
1158 case LibFunc_ZnajSt11align_val_tRKSt9nothrow_t:
1159 // new[](unsigned long, align_val_t, nothrow)
1160 case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t:
1161 return (NumParams == 3 && FTy.getReturnType()->isPointerTy());
1162
1163 // void operator delete[](void*);
1164 case LibFunc_ZdaPv:
1165 // void operator delete(void*);
1166 case LibFunc_ZdlPv:
1167 // void operator delete[](void*);
1168 case LibFunc_msvc_delete_array_ptr32:
1169 // void operator delete[](void*);
1170 case LibFunc_msvc_delete_array_ptr64:
1171 // void operator delete(void*);
1172 case LibFunc_msvc_delete_ptr32:
1173 // void operator delete(void*);
1174 case LibFunc_msvc_delete_ptr64:
1175 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1176
1177 // void operator delete[](void*, nothrow);
1178 case LibFunc_ZdaPvRKSt9nothrow_t:
1179 // void operator delete[](void*, unsigned int);
1180 case LibFunc_ZdaPvj:
1181 // void operator delete[](void*, unsigned long);
1182 case LibFunc_ZdaPvm:
1183 // void operator delete(void*, nothrow);
1184 case LibFunc_ZdlPvRKSt9nothrow_t:
1185 // void operator delete(void*, unsigned int);
1186 case LibFunc_ZdlPvj:
1187 // void operator delete(void*, unsigned long);
1188 case LibFunc_ZdlPvm:
1189 // void operator delete(void*, align_val_t)
1190 case LibFunc_ZdlPvSt11align_val_t:
1191 // void operator delete[](void*, align_val_t)
1192 case LibFunc_ZdaPvSt11align_val_t:
1193 // void operator delete[](void*, unsigned int);
1194 case LibFunc_msvc_delete_array_ptr32_int:
1195 // void operator delete[](void*, nothrow);
1196 case LibFunc_msvc_delete_array_ptr32_nothrow:
1197 // void operator delete[](void*, unsigned long long);
1198 case LibFunc_msvc_delete_array_ptr64_longlong:
1199 // void operator delete[](void*, nothrow);
1200 case LibFunc_msvc_delete_array_ptr64_nothrow:
1201 // void operator delete(void*, unsigned int);
1202 case LibFunc_msvc_delete_ptr32_int:
1203 // void operator delete(void*, nothrow);
1204 case LibFunc_msvc_delete_ptr32_nothrow:
1205 // void operator delete(void*, unsigned long long);
1206 case LibFunc_msvc_delete_ptr64_longlong:
1207 // void operator delete(void*, nothrow);
1208 case LibFunc_msvc_delete_ptr64_nothrow:
1209 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
1210
1211 // void operator delete(void*, align_val_t, nothrow)
1212 case LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t:
1213 // void operator delete[](void*, align_val_t, nothrow)
1214 case LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t:
1215 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
1216
1217 case LibFunc_memset_pattern16:
1218 return (!FTy.isVarArg() && NumParams == 3 &&
1219 FTy.getParamType(0)->isPointerTy() &&
1220 FTy.getParamType(1)->isPointerTy() &&
1221 FTy.getParamType(2)->isIntegerTy());
1222
1223 case LibFunc_cxa_guard_abort:
1224 case LibFunc_cxa_guard_acquire:
1225 case LibFunc_cxa_guard_release:
1226 case LibFunc_nvvm_reflect:
1227 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1228
1229 case LibFunc_sincospi_stret:
1230 case LibFunc_sincospif_stret:
1231 return (NumParams == 1 && FTy.getParamType(0)->isFloatingPointTy());
1232
1233 case LibFunc_acos:
1234 case LibFunc_acos_finite:
1235 case LibFunc_acosf:
1236 case LibFunc_acosf_finite:
1237 case LibFunc_acosh:
1238 case LibFunc_acosh_finite:
1239 case LibFunc_acoshf:
1240 case LibFunc_acoshf_finite:
1241 case LibFunc_acoshl:
1242 case LibFunc_acoshl_finite:
1243 case LibFunc_acosl:
1244 case LibFunc_acosl_finite:
1245 case LibFunc_asin:
1246 case LibFunc_asin_finite:
1247 case LibFunc_asinf:
1248 case LibFunc_asinf_finite:
1249 case LibFunc_asinh:
1250 case LibFunc_asinhf:
1251 case LibFunc_asinhl:
1252 case LibFunc_asinl:
1253 case LibFunc_asinl_finite:
1254 case LibFunc_atan:
1255 case LibFunc_atanf:
1256 case LibFunc_atanh:
1257 case LibFunc_atanh_finite:
1258 case LibFunc_atanhf:
1259 case LibFunc_atanhf_finite:
1260 case LibFunc_atanhl:
1261 case LibFunc_atanhl_finite:
1262 case LibFunc_atanl:
1263 case LibFunc_cbrt:
1264 case LibFunc_cbrtf:
1265 case LibFunc_cbrtl:
1266 case LibFunc_ceil:
1267 case LibFunc_ceilf:
1268 case LibFunc_ceill:
1269 case LibFunc_cos:
1270 case LibFunc_cosf:
1271 case LibFunc_cosh:
1272 case LibFunc_cosh_finite:
1273 case LibFunc_coshf:
1274 case LibFunc_coshf_finite:
1275 case LibFunc_coshl:
1276 case LibFunc_coshl_finite:
1277 case LibFunc_cosl:
1278 case LibFunc_exp10:
1279 case LibFunc_exp10_finite:
1280 case LibFunc_exp10f:
1281 case LibFunc_exp10f_finite:
1282 case LibFunc_exp10l:
1283 case LibFunc_exp10l_finite:
1284 case LibFunc_exp2:
1285 case LibFunc_exp2_finite:
1286 case LibFunc_exp2f:
1287 case LibFunc_exp2f_finite:
1288 case LibFunc_exp2l:
1289 case LibFunc_exp2l_finite:
1290 case LibFunc_exp:
1291 case LibFunc_exp_finite:
1292 case LibFunc_expf:
1293 case LibFunc_expf_finite:
1294 case LibFunc_expl:
1295 case LibFunc_expl_finite:
1296 case LibFunc_expm1:
1297 case LibFunc_expm1f:
1298 case LibFunc_expm1l:
1299 case LibFunc_fabs:
1300 case LibFunc_fabsf:
1301 case LibFunc_fabsl:
1302 case LibFunc_floor:
1303 case LibFunc_floorf:
1304 case LibFunc_floorl:
1305 case LibFunc_log10:
1306 case LibFunc_log10_finite:
1307 case LibFunc_log10f:
1308 case LibFunc_log10f_finite:
1309 case LibFunc_log10l:
1310 case LibFunc_log10l_finite:
1311 case LibFunc_log1p:
1312 case LibFunc_log1pf:
1313 case LibFunc_log1pl:
1314 case LibFunc_log2:
1315 case LibFunc_log2_finite:
1316 case LibFunc_log2f:
1317 case LibFunc_log2f_finite:
1318 case LibFunc_log2l:
1319 case LibFunc_log2l_finite:
1320 case LibFunc_log:
1321 case LibFunc_log_finite:
1322 case LibFunc_logb:
1323 case LibFunc_logbf:
1324 case LibFunc_logbl:
1325 case LibFunc_logf:
1326 case LibFunc_logf_finite:
1327 case LibFunc_logl:
1328 case LibFunc_logl_finite:
1329 case LibFunc_nearbyint:
1330 case LibFunc_nearbyintf:
1331 case LibFunc_nearbyintl:
1332 case LibFunc_rint:
1333 case LibFunc_rintf:
1334 case LibFunc_rintl:
1335 case LibFunc_round:
1336 case LibFunc_roundf:
1337 case LibFunc_roundl:
1338 case LibFunc_sin:
1339 case LibFunc_sinf:
1340 case LibFunc_sinh:
1341 case LibFunc_sinh_finite:
1342 case LibFunc_sinhf:
1343 case LibFunc_sinhf_finite:
1344 case LibFunc_sinhl:
1345 case LibFunc_sinhl_finite:
1346 case LibFunc_sinl:
1347 case LibFunc_sqrt:
1348 case LibFunc_sqrt_finite:
1349 case LibFunc_sqrtf:
1350 case LibFunc_sqrtf_finite:
1351 case LibFunc_sqrtl:
1352 case LibFunc_sqrtl_finite:
1353 case LibFunc_tan:
1354 case LibFunc_tanf:
1355 case LibFunc_tanh:
1356 case LibFunc_tanhf:
1357 case LibFunc_tanhl:
1358 case LibFunc_tanl:
1359 case LibFunc_trunc:
1360 case LibFunc_truncf:
1361 case LibFunc_truncl:
1362 return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() &&
1363 FTy.getReturnType() == FTy.getParamType(0));
1364
1365 case LibFunc_atan2:
1366 case LibFunc_atan2_finite:
1367 case LibFunc_atan2f:
1368 case LibFunc_atan2f_finite:
1369 case LibFunc_atan2l:
1370 case LibFunc_atan2l_finite:
1371 case LibFunc_fmin:
1372 case LibFunc_fminf:
1373 case LibFunc_fminl:
1374 case LibFunc_fmax:
1375 case LibFunc_fmaxf:
1376 case LibFunc_fmaxl:
1377 case LibFunc_fmod:
1378 case LibFunc_fmodf:
1379 case LibFunc_fmodl:
1380 case LibFunc_copysign:
1381 case LibFunc_copysignf:
1382 case LibFunc_copysignl:
1383 case LibFunc_pow:
1384 case LibFunc_pow_finite:
1385 case LibFunc_powf:
1386 case LibFunc_powf_finite:
1387 case LibFunc_powl:
1388 case LibFunc_powl_finite:
1389 return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
1390 FTy.getReturnType() == FTy.getParamType(0) &&
1391 FTy.getReturnType() == FTy.getParamType(1));
1392
1393 case LibFunc_ldexp:
1394 case LibFunc_ldexpf:
1395 case LibFunc_ldexpl:
1396 return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
1397 FTy.getReturnType() == FTy.getParamType(0) &&
1398 FTy.getParamType(1)->isIntegerTy(32));
1399
1400 case LibFunc_ffs:
1401 case LibFunc_ffsl:
1402 case LibFunc_ffsll:
1403 case LibFunc_fls:
1404 case LibFunc_flsl:
1405 case LibFunc_flsll:
1406 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1407 FTy.getParamType(0)->isIntegerTy());
1408
1409 case LibFunc_isdigit:
1410 case LibFunc_isascii:
1411 case LibFunc_toascii:
1412 case LibFunc_putchar:
1413 case LibFunc_putchar_unlocked:
1414 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
1415 FTy.getReturnType() == FTy.getParamType(0));
1416
1417 case LibFunc_abs:
1418 case LibFunc_labs:
1419 case LibFunc_llabs:
1420 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() &&
1421 FTy.getReturnType() == FTy.getParamType(0));
1422
1423 case LibFunc_cxa_atexit:
1424 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() &&
1425 FTy.getParamType(0)->isPointerTy() &&
1426 FTy.getParamType(1)->isPointerTy() &&
1427 FTy.getParamType(2)->isPointerTy());
1428
1429 case LibFunc_sinpi:
1430 case LibFunc_cospi:
1431 return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() &&
1432 FTy.getReturnType() == FTy.getParamType(0));
1433
1434 case LibFunc_sinpif:
1435 case LibFunc_cospif:
1436 return (NumParams == 1 && FTy.getReturnType()->isFloatTy() &&
1437 FTy.getReturnType() == FTy.getParamType(0));
1438
1439 case LibFunc_strnlen:
1440 return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(1) &&
1441 FTy.getParamType(0) == PCharTy &&
1442 FTy.getParamType(1) == SizeTTy);
1443
1444 case LibFunc_posix_memalign:
1445 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
1446 FTy.getParamType(0)->isPointerTy() &&
1447 FTy.getParamType(1) == SizeTTy && FTy.getParamType(2) == SizeTTy);
1448
1449 case LibFunc_wcslen:
1450 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
1451 FTy.getReturnType()->isIntegerTy());
1452
1453 case LibFunc_cabs:
1454 case LibFunc_cabsf:
1455 case LibFunc_cabsl: {
1456 Type* RetTy = FTy.getReturnType();
1457 if (!RetTy->isFloatingPointTy())
1458 return false;
1459
1460 // NOTE: These prototypes are target specific and currently support
1461 // "complex" passed as an array or discrete real & imaginary parameters.
1462 // Add other calling conventions to enable libcall optimizations.
1463 if (NumParams == 1)
1464 return (FTy.getParamType(0)->isArrayTy() &&
1465 FTy.getParamType(0)->getArrayNumElements() == 2 &&
1466 FTy.getParamType(0)->getArrayElementType() == RetTy);
1467 else if (NumParams == 2)
1468 return (FTy.getParamType(0) == RetTy && FTy.getParamType(1) == RetTy);
1469 else
1470 return false;
1471 }
1472 case LibFunc::NumLibFuncs:
1473 case LibFunc::NotLibFunc:
1474 break;
1475 }
1476
1477 llvm_unreachable("Invalid libfunc");
1478 }
1479
getLibFunc(const Function & FDecl,LibFunc & F) const1480 bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
1481 LibFunc &F) const {
1482 // Intrinsics don't overlap w/libcalls; if our module has a large number of
1483 // intrinsics, this ends up being an interesting compile time win since we
1484 // avoid string normalization and comparison.
1485 if (FDecl.isIntrinsic()) return false;
1486
1487 const DataLayout *DL =
1488 FDecl.getParent() ? &FDecl.getParent()->getDataLayout() : nullptr;
1489 return getLibFunc(FDecl.getName(), F) &&
1490 isValidProtoForLibFunc(*FDecl.getFunctionType(), F, DL);
1491 }
1492
disableAllFunctions()1493 void TargetLibraryInfoImpl::disableAllFunctions() {
1494 memset(AvailableArray, 0, sizeof(AvailableArray));
1495 }
1496
compareByScalarFnName(const VecDesc & LHS,const VecDesc & RHS)1497 static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
1498 return LHS.ScalarFnName < RHS.ScalarFnName;
1499 }
1500
compareByVectorFnName(const VecDesc & LHS,const VecDesc & RHS)1501 static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
1502 return LHS.VectorFnName < RHS.VectorFnName;
1503 }
1504
compareWithScalarFnName(const VecDesc & LHS,StringRef S)1505 static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
1506 return LHS.ScalarFnName < S;
1507 }
1508
compareWithVectorFnName(const VecDesc & LHS,StringRef S)1509 static bool compareWithVectorFnName(const VecDesc &LHS, StringRef S) {
1510 return LHS.VectorFnName < S;
1511 }
1512
addVectorizableFunctions(ArrayRef<VecDesc> Fns)1513 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
1514 VectorDescs.insert(VectorDescs.end(), Fns.begin(), Fns.end());
1515 llvm::sort(VectorDescs, compareByScalarFnName);
1516
1517 ScalarDescs.insert(ScalarDescs.end(), Fns.begin(), Fns.end());
1518 llvm::sort(ScalarDescs, compareByVectorFnName);
1519 }
1520
addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib)1521 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
1522 enum VectorLibrary VecLib) {
1523 switch (VecLib) {
1524 case Accelerate: {
1525 const VecDesc VecFuncs[] = {
1526 #define TLI_DEFINE_ACCELERATE_VECFUNCS
1527 #include "llvm/Analysis/VecFuncs.def"
1528 };
1529 addVectorizableFunctions(VecFuncs);
1530 break;
1531 }
1532 case MASSV: {
1533 const VecDesc VecFuncs[] = {
1534 #define TLI_DEFINE_MASSV_VECFUNCS
1535 #include "llvm/Analysis/VecFuncs.def"
1536 };
1537 addVectorizableFunctions(VecFuncs);
1538 break;
1539 }
1540 case SVML: {
1541 const VecDesc VecFuncs[] = {
1542 #define TLI_DEFINE_SVML_VECFUNCS
1543 #include "llvm/Analysis/VecFuncs.def"
1544 };
1545 addVectorizableFunctions(VecFuncs);
1546 break;
1547 }
1548 case NoLibrary:
1549 break;
1550 }
1551 }
1552
isFunctionVectorizable(StringRef funcName) const1553 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
1554 funcName = sanitizeFunctionName(funcName);
1555 if (funcName.empty())
1556 return false;
1557
1558 std::vector<VecDesc>::const_iterator I =
1559 llvm::lower_bound(VectorDescs, funcName, compareWithScalarFnName);
1560 return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
1561 }
1562
getVectorizedFunction(StringRef F,unsigned VF) const1563 StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
1564 unsigned VF) const {
1565 F = sanitizeFunctionName(F);
1566 if (F.empty())
1567 return F;
1568 std::vector<VecDesc>::const_iterator I =
1569 llvm::lower_bound(VectorDescs, F, compareWithScalarFnName);
1570 while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
1571 if (I->VectorizationFactor == VF)
1572 return I->VectorFnName;
1573 ++I;
1574 }
1575 return StringRef();
1576 }
1577
getScalarizedFunction(StringRef F,unsigned & VF) const1578 StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F,
1579 unsigned &VF) const {
1580 F = sanitizeFunctionName(F);
1581 if (F.empty())
1582 return F;
1583
1584 std::vector<VecDesc>::const_iterator I =
1585 llvm::lower_bound(ScalarDescs, F, compareWithVectorFnName);
1586 if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F)
1587 return StringRef();
1588 VF = I->VectorizationFactor;
1589 return I->ScalarFnName;
1590 }
1591
run(const Function & F,FunctionAnalysisManager &)1592 TargetLibraryInfo TargetLibraryAnalysis::run(const Function &F,
1593 FunctionAnalysisManager &) {
1594 if (!BaselineInfoImpl)
1595 BaselineInfoImpl =
1596 TargetLibraryInfoImpl(Triple(F.getParent()->getTargetTriple()));
1597 return TargetLibraryInfo(*BaselineInfoImpl, &F);
1598 }
1599
getWCharSize(const Module & M) const1600 unsigned TargetLibraryInfoImpl::getWCharSize(const Module &M) const {
1601 if (auto *ShortWChar = cast_or_null<ConstantAsMetadata>(
1602 M.getModuleFlag("wchar_size")))
1603 return cast<ConstantInt>(ShortWChar->getValue())->getZExtValue();
1604 return 0;
1605 }
1606
TargetLibraryInfoWrapperPass()1607 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
1608 : ImmutablePass(ID), TLA(TargetLibraryInfoImpl()) {
1609 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1610 }
1611
TargetLibraryInfoWrapperPass(const Triple & T)1612 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
1613 : ImmutablePass(ID), TLA(TargetLibraryInfoImpl(T)) {
1614 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1615 }
1616
TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl & TLIImpl)1617 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
1618 const TargetLibraryInfoImpl &TLIImpl)
1619 : ImmutablePass(ID), TLA(TLIImpl) {
1620 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1621 }
1622
1623 AnalysisKey TargetLibraryAnalysis::Key;
1624
1625 // Register the basic pass.
1626 INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
1627 "Target Library Information", false, true)
1628 char TargetLibraryInfoWrapperPass::ID = 0;
1629
anchor()1630 void TargetLibraryInfoWrapperPass::anchor() {}
1631
getWidestVF(StringRef ScalarF) const1632 unsigned TargetLibraryInfoImpl::getWidestVF(StringRef ScalarF) const {
1633 ScalarF = sanitizeFunctionName(ScalarF);
1634 if (ScalarF.empty())
1635 return 1;
1636
1637 unsigned VF = 1;
1638 std::vector<VecDesc>::const_iterator I =
1639 llvm::lower_bound(VectorDescs, ScalarF, compareWithScalarFnName);
1640 while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == ScalarF) {
1641 if (I->VectorizationFactor > VF)
1642 VF = I->VectorizationFactor;
1643 ++I;
1644 }
1645 return VF;
1646 }
1647