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