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