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