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