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