162d4215bSChandler Carruth //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==//
262d4215bSChandler Carruth //
362d4215bSChandler Carruth //                     The LLVM Compiler Infrastructure
462d4215bSChandler Carruth //
562d4215bSChandler Carruth // This file is distributed under the University of Illinois Open Source
662d4215bSChandler Carruth // License. See LICENSE.TXT for details.
762d4215bSChandler Carruth //
862d4215bSChandler Carruth //===----------------------------------------------------------------------===//
962d4215bSChandler Carruth //
1062d4215bSChandler Carruth // This file implements the TargetLibraryInfo class.
1162d4215bSChandler Carruth //
1262d4215bSChandler Carruth //===----------------------------------------------------------------------===//
1362d4215bSChandler Carruth 
1462d4215bSChandler Carruth #include "llvm/Analysis/TargetLibraryInfo.h"
1562d4215bSChandler Carruth #include "llvm/ADT/Triple.h"
1650ec0b5dSMatthias Braun #include "llvm/IR/Constants.h"
176d8a2aa9SMichael Zolotukhin #include "llvm/Support/CommandLine.h"
1862d4215bSChandler Carruth using namespace llvm;
1962d4215bSChandler Carruth 
206d8a2aa9SMichael Zolotukhin static cl::opt<TargetLibraryInfoImpl::VectorLibrary> ClVectorLibrary(
216d8a2aa9SMichael Zolotukhin     "vector-library", cl::Hidden, cl::desc("Vector functions library"),
226d8a2aa9SMichael Zolotukhin     cl::init(TargetLibraryInfoImpl::NoLibrary),
236d8a2aa9SMichael Zolotukhin     cl::values(clEnumValN(TargetLibraryInfoImpl::NoLibrary, "none",
246d8a2aa9SMichael Zolotukhin                           "No vector functions library"),
256d8a2aa9SMichael Zolotukhin                clEnumValN(TargetLibraryInfoImpl::Accelerate, "Accelerate",
266d8a2aa9SMichael Zolotukhin                           "Accelerate framework"),
27a6669a1eSMatt Masten                clEnumValN(TargetLibraryInfoImpl::SVML, "SVML",
28732afdd0SMehdi Amini                           "Intel SVML library")));
296d8a2aa9SMichael Zolotukhin 
309a72cd7bSMehdi Amini StringRef const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] = {
31cd3d25a2SJan Wen Voung #define TLI_DEFINE_STRING
32cd3d25a2SJan Wen Voung #include "llvm/Analysis/TargetLibraryInfo.def"
3362d4215bSChandler Carruth };
3462d4215bSChandler Carruth 
3562d4215bSChandler Carruth static bool hasSinCosPiStret(const Triple &T) {
3662d4215bSChandler Carruth   // Only Darwin variants have _stret versions of combined trig functions.
3762d4215bSChandler Carruth   if (!T.isOSDarwin())
3862d4215bSChandler Carruth     return false;
3962d4215bSChandler Carruth 
4062d4215bSChandler Carruth   // The ABI is rather complicated on x86, so don't do anything special there.
4162d4215bSChandler Carruth   if (T.getArch() == Triple::x86)
4262d4215bSChandler Carruth     return false;
4362d4215bSChandler Carruth 
4462d4215bSChandler Carruth   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
4562d4215bSChandler Carruth     return false;
4662d4215bSChandler Carruth 
4762d4215bSChandler Carruth   if (T.isiOS() && T.isOSVersionLT(7, 0))
4862d4215bSChandler Carruth     return false;
4962d4215bSChandler Carruth 
5062d4215bSChandler Carruth   return true;
5162d4215bSChandler Carruth }
5262d4215bSChandler Carruth 
53600d24b4SSanjay Patel /// Initialize the set of available library functions based on the specified
54600d24b4SSanjay Patel /// target triple. This should be carefully written so that a missing target
55600d24b4SSanjay Patel /// triple gets a sane set of defaults.
56c0291865SChandler Carruth static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
579a72cd7bSMehdi Amini                        ArrayRef<StringRef> StandardNames) {
5862d4215bSChandler Carruth   // Verify that the StandardNames array is in alphabetical order.
59e30b8ca1SCraig Topper   assert(std::is_sorted(StandardNames.begin(), StandardNames.end(),
609a72cd7bSMehdi Amini                         [](StringRef LHS, StringRef RHS) {
619a72cd7bSMehdi Amini                           return LHS < RHS;
62e30b8ca1SCraig Topper                         }) &&
63e30b8ca1SCraig Topper          "TargetLibraryInfoImpl function names must be sorted");
6462d4215bSChandler Carruth 
65ca22d427SDavid Bolvansky   // Set IO unlocked variants as unavailable
66ca22d427SDavid Bolvansky   // Set them as available per system below
67ca22d427SDavid Bolvansky   TLI.setUnavailable(LibFunc_getchar_unlocked);
68ca22d427SDavid Bolvansky   TLI.setUnavailable(LibFunc_putc_unlocked);
69ca22d427SDavid Bolvansky   TLI.setUnavailable(LibFunc_putchar_unlocked);
70ca22d427SDavid Bolvansky   TLI.setUnavailable(LibFunc_fputc_unlocked);
71ca22d427SDavid Bolvansky   TLI.setUnavailable(LibFunc_fgetc_unlocked);
72ca22d427SDavid Bolvansky   TLI.setUnavailable(LibFunc_fread_unlocked);
73ca22d427SDavid Bolvansky   TLI.setUnavailable(LibFunc_fwrite_unlocked);
74ca22d427SDavid Bolvansky   TLI.setUnavailable(LibFunc_fputs_unlocked);
75ca22d427SDavid Bolvansky   TLI.setUnavailable(LibFunc_fgets_unlocked);
76ca22d427SDavid Bolvansky 
776af8e6c3SMarcin Koscielnicki   bool ShouldExtI32Param = false, ShouldExtI32Return = false,
786af8e6c3SMarcin Koscielnicki        ShouldSignExtI32Param = false;
796af8e6c3SMarcin Koscielnicki   // PowerPC64, Sparc64, SystemZ need signext/zeroext on i32 parameters and
806af8e6c3SMarcin Koscielnicki   // returns corresponding to C-level ints and unsigned ints.
816af8e6c3SMarcin Koscielnicki   if (T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le ||
826af8e6c3SMarcin Koscielnicki       T.getArch() == Triple::sparcv9 || T.getArch() == Triple::systemz) {
836af8e6c3SMarcin Koscielnicki     ShouldExtI32Param = true;
846af8e6c3SMarcin Koscielnicki     ShouldExtI32Return = true;
856af8e6c3SMarcin Koscielnicki   }
866af8e6c3SMarcin Koscielnicki   // Mips, on the other hand, needs signext on i32 parameters corresponding
876af8e6c3SMarcin Koscielnicki   // to both signed and unsigned ints.
886af8e6c3SMarcin Koscielnicki   if (T.getArch() == Triple::mips || T.getArch() == Triple::mipsel ||
896af8e6c3SMarcin Koscielnicki       T.getArch() == Triple::mips64 || T.getArch() == Triple::mips64el) {
906af8e6c3SMarcin Koscielnicki     ShouldSignExtI32Param = true;
916af8e6c3SMarcin Koscielnicki   }
926af8e6c3SMarcin Koscielnicki   TLI.setShouldExtI32Param(ShouldExtI32Param);
936af8e6c3SMarcin Koscielnicki   TLI.setShouldExtI32Return(ShouldExtI32Return);
946af8e6c3SMarcin Koscielnicki   TLI.setShouldSignExtI32Param(ShouldSignExtI32Param);
956af8e6c3SMarcin Koscielnicki 
9678fd4f08SNicolai Hahnle   if (T.getArch() == Triple::r600 ||
9778fd4f08SNicolai Hahnle       T.getArch() == Triple::amdgcn) {
98d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ldexp);
99d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ldexpf);
100d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ldexpl);
101d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_exp10);
102d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_exp10f);
103d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_exp10l);
104d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_log10);
105d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_log10f);
106d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_log10l);
10778fd4f08SNicolai Hahnle   }
10878fd4f08SNicolai Hahnle 
10962d4215bSChandler Carruth   // There are no library implementations of mempcy and memset for AMD gpus and
11062d4215bSChandler Carruth   // these can be difficult to lower in the backend.
11162d4215bSChandler Carruth   if (T.getArch() == Triple::r600 ||
11205532995SDan Gohman       T.getArch() == Triple::amdgcn) {
113d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_memcpy);
114d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_memset);
115d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_memset_pattern16);
11662d4215bSChandler Carruth     return;
11762d4215bSChandler Carruth   }
11862d4215bSChandler Carruth 
11962d4215bSChandler Carruth   // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
1208b40366bSTim Northover   // All versions of watchOS support it.
12162d4215bSChandler Carruth   if (T.isMacOSX()) {
122ca22d427SDavid Bolvansky     // available IO unlocked variants on Mac OS X
123ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_getc_unlocked);
124ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_getchar_unlocked);
125ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_putc_unlocked);
126ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_putchar_unlocked);
127ca22d427SDavid Bolvansky 
12862d4215bSChandler Carruth     if (T.isMacOSXVersionLT(10, 5))
129d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_memset_pattern16);
13062d4215bSChandler Carruth   } else if (T.isiOS()) {
13162d4215bSChandler Carruth     if (T.isOSVersionLT(3, 0))
132d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_memset_pattern16);
1338b40366bSTim Northover   } else if (!T.isWatchOS()) {
134d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_memset_pattern16);
13562d4215bSChandler Carruth   }
13662d4215bSChandler Carruth 
13762d4215bSChandler Carruth   if (!hasSinCosPiStret(T)) {
138d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_sinpi);
139d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_sinpif);
140d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_cospi);
141d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_cospif);
142d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_sincospi_stret);
143d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_sincospif_stret);
14462d4215bSChandler Carruth   }
14562d4215bSChandler Carruth 
14662d4215bSChandler Carruth   if (T.isMacOSX() && T.getArch() == Triple::x86 &&
14762d4215bSChandler Carruth       !T.isMacOSXVersionLT(10, 7)) {
14862d4215bSChandler Carruth     // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
14962d4215bSChandler Carruth     // we don't care about) have two versions; on recent OSX, the one we want
15062d4215bSChandler Carruth     // has a $UNIX2003 suffix. The two implementations are identical except
15162d4215bSChandler Carruth     // for the return value in some edge cases.  However, we don't want to
15262d4215bSChandler Carruth     // generate code that depends on the old symbols.
153d21529faSDavid L. Jones     TLI.setAvailableWithName(LibFunc_fwrite, "fwrite$UNIX2003");
154d21529faSDavid L. Jones     TLI.setAvailableWithName(LibFunc_fputs, "fputs$UNIX2003");
15562d4215bSChandler Carruth   }
15662d4215bSChandler Carruth 
15762d4215bSChandler Carruth   // iprintf and friends are only available on XCore and TCE.
15862d4215bSChandler Carruth   if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) {
159d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_iprintf);
160d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_siprintf);
161d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fiprintf);
16262d4215bSChandler Carruth   }
16362d4215bSChandler Carruth 
16462d4215bSChandler Carruth   if (T.isOSWindows() && !T.isOSCygMing()) {
16562d4215bSChandler Carruth     // Win32 does not support long double
166d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_acosl);
167d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_asinl);
168d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_atanl);
169d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_atan2l);
170d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ceill);
171d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_copysignl);
172d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_cosl);
173d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_coshl);
174d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_expl);
175d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fabsf); // Win32 and Win64 both lack fabsf
176d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fabsl);
177d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_floorl);
178d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fmaxl);
179d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fminl);
180d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fmodl);
181d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_frexpl);
182d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ldexpf);
183d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ldexpl);
184d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_logl);
185d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_modfl);
186d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_powl);
187d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_sinl);
188d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_sinhl);
189d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_sqrtl);
190d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_tanl);
191d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_tanhl);
19262d4215bSChandler Carruth 
19362d4215bSChandler Carruth     // Win32 only has C89 math
194d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_acosh);
195d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_acoshf);
196d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_acoshl);
197d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_asinh);
198d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_asinhf);
199d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_asinhl);
200d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_atanh);
201d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_atanhf);
202d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_atanhl);
2032ff24731SHal Finkel     TLI.setUnavailable(LibFunc_cabs);
2042ff24731SHal Finkel     TLI.setUnavailable(LibFunc_cabsf);
2052ff24731SHal Finkel     TLI.setUnavailable(LibFunc_cabsl);
206d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_cbrt);
207d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_cbrtf);
208d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_cbrtl);
209d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_exp2);
210d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_exp2f);
211d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_exp2l);
212d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_expm1);
213d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_expm1f);
214d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_expm1l);
215d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_log2);
216d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_log2f);
217d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_log2l);
218d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_log1p);
219d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_log1pf);
220d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_log1pl);
221d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_logb);
222d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_logbf);
223d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_logbl);
224d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_nearbyint);
225d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_nearbyintf);
226d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_nearbyintl);
227d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_rint);
228d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_rintf);
229d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_rintl);
230d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_round);
231d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_roundf);
232d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_roundl);
233d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_trunc);
234d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_truncf);
235d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_truncl);
23662d4215bSChandler Carruth 
23762d4215bSChandler Carruth     // Win32 provides some C99 math with mangled names
238d21529faSDavid L. Jones     TLI.setAvailableWithName(LibFunc_copysign, "_copysign");
23962d4215bSChandler Carruth 
24062d4215bSChandler Carruth     if (T.getArch() == Triple::x86) {
24162d4215bSChandler Carruth       // Win32 on x86 implements single-precision math functions as macros
242d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_acosf);
243d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_asinf);
244d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_atanf);
245d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_atan2f);
246d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_ceilf);
247d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_copysignf);
248d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_cosf);
249d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_coshf);
250d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_expf);
251d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_floorf);
252d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_fminf);
253d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_fmaxf);
254d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_fmodf);
255d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_logf);
256d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_log10f);
257d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_modff);
258d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_powf);
259d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_sinf);
260d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_sinhf);
261d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_sqrtf);
262d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_tanf);
263d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_tanhf);
26462d4215bSChandler Carruth     }
26562d4215bSChandler Carruth 
266956ee797SFangrui Song     // Win32 does *not* provide these functions, but they are
26762d4215bSChandler Carruth     // generally available on POSIX-compliant systems:
268d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_access);
269d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_bcmp);
270d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_bcopy);
271d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_bzero);
272d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_chmod);
273d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_chown);
274d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_closedir);
275d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ctermid);
276d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fdopen);
277d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ffs);
278d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fileno);
279d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_flockfile);
280d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fseeko);
281d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fstat);
282d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fstatvfs);
283d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ftello);
284d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ftrylockfile);
285d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_funlockfile);
286d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_getitimer);
287d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_getlogin_r);
288d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_getpwnam);
289d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_gettimeofday);
290d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_htonl);
291d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_htons);
292d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_lchown);
293d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_lstat);
294d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_memccpy);
295d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_mkdir);
296d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ntohl);
297d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ntohs);
298d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_open);
299d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_opendir);
300d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_pclose);
301d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_popen);
302d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_pread);
303d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_pwrite);
304d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_read);
305d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_readlink);
306d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_realpath);
307d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_rmdir);
308d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_setitimer);
309d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_stat);
310d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_statvfs);
311d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_stpcpy);
312d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_stpncpy);
313d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_strcasecmp);
314d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_strncasecmp);
315d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_times);
316d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_uname);
317d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_unlink);
318d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_unsetenv);
319d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_utime);
320d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_utimes);
321d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_write);
32262d4215bSChandler Carruth 
32362d4215bSChandler Carruth     // Win32 does *not* provide provide these functions, but they are
32462d4215bSChandler Carruth     // specified by C99:
325d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_atoll);
326d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_frexpf);
327d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_llabs);
32862d4215bSChandler Carruth   }
32962d4215bSChandler Carruth 
33062d4215bSChandler Carruth   switch (T.getOS()) {
33162d4215bSChandler Carruth   case Triple::MacOSX:
33262d4215bSChandler Carruth     // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
33362d4215bSChandler Carruth     // and their names are __exp10 and __exp10f. exp10l is not available on
33462d4215bSChandler Carruth     // OS X or iOS.
335d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_exp10l);
33662d4215bSChandler Carruth     if (T.isMacOSXVersionLT(10, 9)) {
337d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_exp10);
338d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_exp10f);
33962d4215bSChandler Carruth     } else {
340d21529faSDavid L. Jones       TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
341d21529faSDavid L. Jones       TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
34262d4215bSChandler Carruth     }
34362d4215bSChandler Carruth     break;
34462d4215bSChandler Carruth   case Triple::IOS:
34589a6eefeSTim Northover   case Triple::TvOS:
3468b40366bSTim Northover   case Triple::WatchOS:
347d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_exp10l);
3488b40366bSTim Northover     if (!T.isWatchOS() && (T.isOSVersionLT(7, 0) ||
3498b40366bSTim Northover                            (T.isOSVersionLT(9, 0) &&
3508b40366bSTim Northover                             (T.getArch() == Triple::x86 ||
3518b40366bSTim Northover                              T.getArch() == Triple::x86_64)))) {
352d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_exp10);
353d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_exp10f);
35462d4215bSChandler Carruth     } else {
355d21529faSDavid L. Jones       TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
356d21529faSDavid L. Jones       TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
35762d4215bSChandler Carruth     }
35862d4215bSChandler Carruth     break;
35962d4215bSChandler Carruth   case Triple::Linux:
36062d4215bSChandler Carruth     // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
36162d4215bSChandler Carruth     // buggy prior to glibc version 2.18. Until this version is widely deployed
36262d4215bSChandler Carruth     // or we have a reasonable detection strategy, we cannot use exp10 reliably
36362d4215bSChandler Carruth     // on Linux.
36462d4215bSChandler Carruth     //
36562d4215bSChandler Carruth     // Fall through to disable all of them.
366cd1d5aafSJustin Bogner     LLVM_FALLTHROUGH;
36762d4215bSChandler Carruth   default:
368d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_exp10);
369d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_exp10f);
370d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_exp10l);
37162d4215bSChandler Carruth   }
37262d4215bSChandler Carruth 
37362d4215bSChandler Carruth   // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
37462d4215bSChandler Carruth   // Linux (GLIBC):
37562d4215bSChandler Carruth   // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
37683b34816SDavide Italiano   // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
37762d4215bSChandler Carruth   // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
37862d4215bSChandler Carruth   switch (T.getOS()) {
37962d4215bSChandler Carruth   case Triple::Darwin:
38062d4215bSChandler Carruth   case Triple::MacOSX:
38162d4215bSChandler Carruth   case Triple::IOS:
38289a6eefeSTim Northover   case Triple::TvOS:
3838b40366bSTim Northover   case Triple::WatchOS:
38462d4215bSChandler Carruth   case Triple::FreeBSD:
38562d4215bSChandler Carruth   case Triple::Linux:
38662d4215bSChandler Carruth     break;
38762d4215bSChandler Carruth   default:
388d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ffsl);
38962d4215bSChandler Carruth   }
39062d4215bSChandler Carruth 
39162d4215bSChandler Carruth   // ffsll is available on at least FreeBSD and Linux (GLIBC):
39283b34816SDavide Italiano   // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
39362d4215bSChandler Carruth   // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
39462d4215bSChandler Carruth   switch (T.getOS()) {
39589a6eefeSTim Northover   case Triple::Darwin:
39689a6eefeSTim Northover   case Triple::MacOSX:
39789a6eefeSTim Northover   case Triple::IOS:
39889a6eefeSTim Northover   case Triple::TvOS:
39989a6eefeSTim Northover   case Triple::WatchOS:
40062d4215bSChandler Carruth   case Triple::FreeBSD:
40162d4215bSChandler Carruth   case Triple::Linux:
40262d4215bSChandler Carruth     break;
40362d4215bSChandler Carruth   default:
404d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ffsll);
40562d4215bSChandler Carruth   }
40662d4215bSChandler Carruth 
407bfd3082eSDavide Italiano   // The following functions are available on at least FreeBSD:
408bfd3082eSDavide Italiano   // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
409bfd3082eSDavide Italiano   // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
410bfd3082eSDavide Italiano   // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
411bfd3082eSDavide Italiano   if (!T.isOSFreeBSD()) {
412d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fls);
413d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_flsl);
414d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_flsll);
415bfd3082eSDavide Italiano   }
416bfd3082eSDavide Italiano 
41760d1e79fSChih-Hung Hsieh   // The following functions are available on Linux,
41860d1e79fSChih-Hung Hsieh   // but Android uses bionic instead of glibc.
41960d1e79fSChih-Hung Hsieh   if (!T.isOSLinux() || T.isAndroid()) {
420d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_dunder_strdup);
421d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_dunder_strtok_r);
422d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_dunder_isoc99_scanf);
423d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_dunder_isoc99_sscanf);
424d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_under_IO_getc);
425d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_under_IO_putc);
42660d1e79fSChih-Hung Hsieh     // But, Android has memalign.
42760d1e79fSChih-Hung Hsieh     if (!T.isAndroid())
428d21529faSDavid L. Jones       TLI.setUnavailable(LibFunc_memalign);
429d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fopen64);
430d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fseeko64);
431d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fstat64);
432d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_fstatvfs64);
433d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_ftello64);
434d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_lstat64);
435d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_open64);
436d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_stat64);
437d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_statvfs64);
438d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_tmpfile64);
43952149f03SSanjay Patel 
44052149f03SSanjay Patel     // Relaxed math functions are included in math-finite.h on Linux (GLIBC).
44152149f03SSanjay Patel     TLI.setUnavailable(LibFunc_acos_finite);
44252149f03SSanjay Patel     TLI.setUnavailable(LibFunc_acosf_finite);
44352149f03SSanjay Patel     TLI.setUnavailable(LibFunc_acosl_finite);
44452149f03SSanjay Patel     TLI.setUnavailable(LibFunc_acosh_finite);
44552149f03SSanjay Patel     TLI.setUnavailable(LibFunc_acoshf_finite);
44652149f03SSanjay Patel     TLI.setUnavailable(LibFunc_acoshl_finite);
44752149f03SSanjay Patel     TLI.setUnavailable(LibFunc_asin_finite);
44852149f03SSanjay Patel     TLI.setUnavailable(LibFunc_asinf_finite);
44952149f03SSanjay Patel     TLI.setUnavailable(LibFunc_asinl_finite);
45052149f03SSanjay Patel     TLI.setUnavailable(LibFunc_atan2_finite);
45152149f03SSanjay Patel     TLI.setUnavailable(LibFunc_atan2f_finite);
45252149f03SSanjay Patel     TLI.setUnavailable(LibFunc_atan2l_finite);
45352149f03SSanjay Patel     TLI.setUnavailable(LibFunc_atanh_finite);
45452149f03SSanjay Patel     TLI.setUnavailable(LibFunc_atanhf_finite);
45552149f03SSanjay Patel     TLI.setUnavailable(LibFunc_atanhl_finite);
45652149f03SSanjay Patel     TLI.setUnavailable(LibFunc_cosh_finite);
45752149f03SSanjay Patel     TLI.setUnavailable(LibFunc_coshf_finite);
45852149f03SSanjay Patel     TLI.setUnavailable(LibFunc_coshl_finite);
45952149f03SSanjay Patel     TLI.setUnavailable(LibFunc_exp10_finite);
46052149f03SSanjay Patel     TLI.setUnavailable(LibFunc_exp10f_finite);
46152149f03SSanjay Patel     TLI.setUnavailable(LibFunc_exp10l_finite);
46252149f03SSanjay Patel     TLI.setUnavailable(LibFunc_exp2_finite);
46352149f03SSanjay Patel     TLI.setUnavailable(LibFunc_exp2f_finite);
46452149f03SSanjay Patel     TLI.setUnavailable(LibFunc_exp2l_finite);
46552149f03SSanjay Patel     TLI.setUnavailable(LibFunc_exp_finite);
46652149f03SSanjay Patel     TLI.setUnavailable(LibFunc_expf_finite);
46752149f03SSanjay Patel     TLI.setUnavailable(LibFunc_expl_finite);
46852149f03SSanjay Patel     TLI.setUnavailable(LibFunc_log10_finite);
46952149f03SSanjay Patel     TLI.setUnavailable(LibFunc_log10f_finite);
47052149f03SSanjay Patel     TLI.setUnavailable(LibFunc_log10l_finite);
47152149f03SSanjay Patel     TLI.setUnavailable(LibFunc_log2_finite);
47252149f03SSanjay Patel     TLI.setUnavailable(LibFunc_log2f_finite);
47352149f03SSanjay Patel     TLI.setUnavailable(LibFunc_log2l_finite);
47452149f03SSanjay Patel     TLI.setUnavailable(LibFunc_log_finite);
47552149f03SSanjay Patel     TLI.setUnavailable(LibFunc_logf_finite);
47652149f03SSanjay Patel     TLI.setUnavailable(LibFunc_logl_finite);
47752149f03SSanjay Patel     TLI.setUnavailable(LibFunc_pow_finite);
47852149f03SSanjay Patel     TLI.setUnavailable(LibFunc_powf_finite);
47952149f03SSanjay Patel     TLI.setUnavailable(LibFunc_powl_finite);
48052149f03SSanjay Patel     TLI.setUnavailable(LibFunc_sinh_finite);
48152149f03SSanjay Patel     TLI.setUnavailable(LibFunc_sinhf_finite);
48252149f03SSanjay Patel     TLI.setUnavailable(LibFunc_sinhl_finite);
48362d4215bSChandler Carruth   }
4846d8a2aa9SMichael Zolotukhin 
485c1078872SMartin Storsjo   if ((T.isOSLinux() && T.isGNUEnvironment()) ||
486c1078872SMartin Storsjo       (T.isAndroid() && !T.isAndroidVersionLT(28))) {
487ca22d427SDavid Bolvansky     // available IO unlocked variants on GNU/Linux and Android P or later
488ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_getc_unlocked);
489ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_getchar_unlocked);
490ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_putc_unlocked);
491ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_putchar_unlocked);
492ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_fputc_unlocked);
493ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_fgetc_unlocked);
494ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_fread_unlocked);
495ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_fwrite_unlocked);
496ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_fputs_unlocked);
497ca22d427SDavid Bolvansky     TLI.setAvailable(LibFunc_fgets_unlocked);
498ca22d427SDavid Bolvansky   }
499ca22d427SDavid Bolvansky 
50051132881SJustin Lebar   // As currently implemented in clang, NVPTX code has no standard library to
50151132881SJustin Lebar   // speak of.  Headers provide a standard-ish library implementation, but many
50251132881SJustin Lebar   // of the signatures are wrong -- for example, many libm functions are not
50351132881SJustin Lebar   // extern "C".
50451132881SJustin Lebar   //
50551132881SJustin Lebar   // libdevice, an IR library provided by nvidia, is linked in by the front-end,
50651132881SJustin Lebar   // but only used functions are provided to llvm.  Moreover, most of the
50751132881SJustin Lebar   // functions in libdevice don't map precisely to standard library functions.
50851132881SJustin Lebar   //
50951132881SJustin Lebar   // FIXME: Having no standard library prevents e.g. many fastmath
51051132881SJustin Lebar   // optimizations, so this situation should be fixed.
511ae272d71SDavid Majnemer   if (T.isNVPTX()) {
51251132881SJustin Lebar     TLI.disableAllFunctions();
513d21529faSDavid L. Jones     TLI.setAvailable(LibFunc_nvvm_reflect);
514ae272d71SDavid Majnemer   } else {
515d21529faSDavid L. Jones     TLI.setUnavailable(LibFunc_nvvm_reflect);
516ae272d71SDavid Majnemer   }
51751132881SJustin Lebar 
5186d8a2aa9SMichael Zolotukhin   TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary);
51962d4215bSChandler Carruth }
52062d4215bSChandler Carruth 
521c0291865SChandler Carruth TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
52262d4215bSChandler Carruth   // Default to everything being available.
52362d4215bSChandler Carruth   memset(AvailableArray, -1, sizeof(AvailableArray));
52462d4215bSChandler Carruth 
52562d4215bSChandler Carruth   initialize(*this, Triple(), StandardNames);
52662d4215bSChandler Carruth }
52762d4215bSChandler Carruth 
528c0291865SChandler Carruth TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
52962d4215bSChandler Carruth   // Default to everything being available.
53062d4215bSChandler Carruth   memset(AvailableArray, -1, sizeof(AvailableArray));
53162d4215bSChandler Carruth 
53262d4215bSChandler Carruth   initialize(*this, T, StandardNames);
53362d4215bSChandler Carruth }
53462d4215bSChandler Carruth 
535c0291865SChandler Carruth TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
5365ae2c526SMarcin Koscielnicki     : CustomNames(TLI.CustomNames), ShouldExtI32Param(TLI.ShouldExtI32Param),
5375ae2c526SMarcin Koscielnicki       ShouldExtI32Return(TLI.ShouldExtI32Return),
5385ae2c526SMarcin Koscielnicki       ShouldSignExtI32Param(TLI.ShouldSignExtI32Param) {
53962d4215bSChandler Carruth   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
540e8f2551fSMichael Zolotukhin   VectorDescs = TLI.VectorDescs;
541e8f2551fSMichael Zolotukhin   ScalarDescs = TLI.ScalarDescs;
5428ca43224SChandler Carruth }
5438ca43224SChandler Carruth 
544c0291865SChandler Carruth TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
5455ae2c526SMarcin Koscielnicki     : CustomNames(std::move(TLI.CustomNames)),
5465ae2c526SMarcin Koscielnicki       ShouldExtI32Param(TLI.ShouldExtI32Param),
5475ae2c526SMarcin Koscielnicki       ShouldExtI32Return(TLI.ShouldExtI32Return),
5485ae2c526SMarcin Koscielnicki       ShouldSignExtI32Param(TLI.ShouldSignExtI32Param) {
5498ca43224SChandler Carruth   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
5508ca43224SChandler Carruth             AvailableArray);
551e8f2551fSMichael Zolotukhin   VectorDescs = TLI.VectorDescs;
552e8f2551fSMichael Zolotukhin   ScalarDescs = TLI.ScalarDescs;
5538ca43224SChandler Carruth }
5548ca43224SChandler Carruth 
555c0291865SChandler Carruth TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
55662d4215bSChandler Carruth   CustomNames = TLI.CustomNames;
5575ae2c526SMarcin Koscielnicki   ShouldExtI32Param = TLI.ShouldExtI32Param;
5585ae2c526SMarcin Koscielnicki   ShouldExtI32Return = TLI.ShouldExtI32Return;
5595ae2c526SMarcin Koscielnicki   ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
5608ca43224SChandler Carruth   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
5618ca43224SChandler Carruth   return *this;
5628ca43224SChandler Carruth }
5638ca43224SChandler Carruth 
564c0291865SChandler Carruth TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
5658ca43224SChandler Carruth   CustomNames = std::move(TLI.CustomNames);
5665ae2c526SMarcin Koscielnicki   ShouldExtI32Param = TLI.ShouldExtI32Param;
5675ae2c526SMarcin Koscielnicki   ShouldExtI32Return = TLI.ShouldExtI32Return;
5685ae2c526SMarcin Koscielnicki   ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
5698ca43224SChandler Carruth   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
5708ca43224SChandler Carruth             AvailableArray);
5718ca43224SChandler Carruth   return *this;
57262d4215bSChandler Carruth }
57362d4215bSChandler Carruth 
57421abdf98SMichael Zolotukhin static StringRef sanitizeFunctionName(StringRef funcName) {
57562d4215bSChandler Carruth   // Filter out empty names and names containing null bytes, those can't be in
57662d4215bSChandler Carruth   // our table.
57762d4215bSChandler Carruth   if (funcName.empty() || funcName.find('\0') != StringRef::npos)
57821abdf98SMichael Zolotukhin     return StringRef();
57962d4215bSChandler Carruth 
58062d4215bSChandler Carruth   // Check for \01 prefix that is used to mangle __asm declarations and
58162d4215bSChandler Carruth   // strip it if present.
5826f0ecca3SPeter Collingbourne   return GlobalValue::dropLLVMManglingEscape(funcName);
58321abdf98SMichael Zolotukhin }
58421abdf98SMichael Zolotukhin 
58521abdf98SMichael Zolotukhin bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName,
586d21529faSDavid L. Jones                                        LibFunc &F) const {
5879a72cd7bSMehdi Amini   StringRef const *Start = &StandardNames[0];
588d21529faSDavid L. Jones   StringRef const *End = &StandardNames[NumLibFuncs];
58921abdf98SMichael Zolotukhin 
59021abdf98SMichael Zolotukhin   funcName = sanitizeFunctionName(funcName);
59121abdf98SMichael Zolotukhin   if (funcName.empty())
59221abdf98SMichael Zolotukhin     return false;
59321abdf98SMichael Zolotukhin 
5949a72cd7bSMehdi Amini   StringRef const *I = std::lower_bound(
5959a72cd7bSMehdi Amini       Start, End, funcName, [](StringRef LHS, StringRef RHS) {
5969a72cd7bSMehdi Amini         return LHS < RHS;
597d3b76a3bSMichael Zolotukhin       });
59862d4215bSChandler Carruth   if (I != End && *I == funcName) {
599d21529faSDavid L. Jones     F = (LibFunc)(I - Start);
60062d4215bSChandler Carruth     return true;
60162d4215bSChandler Carruth   }
60262d4215bSChandler Carruth   return false;
60362d4215bSChandler Carruth }
60462d4215bSChandler Carruth 
605d765a82bSAhmed Bougacha bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
606d21529faSDavid L. Jones                                                    LibFunc F,
607d765a82bSAhmed Bougacha                                                    const DataLayout *DL) const {
608d765a82bSAhmed Bougacha   LLVMContext &Ctx = FTy.getContext();
609d765a82bSAhmed Bougacha   Type *PCharTy = Type::getInt8PtrTy(Ctx);
610d765a82bSAhmed Bougacha   Type *SizeTTy = DL ? DL->getIntPtrType(Ctx, /*AS=*/0) : nullptr;
611d765a82bSAhmed Bougacha   auto IsSizeTTy = [SizeTTy](Type *Ty) {
612d765a82bSAhmed Bougacha     return SizeTTy ? Ty == SizeTTy : Ty->isIntegerTy();
613d765a82bSAhmed Bougacha   };
614d765a82bSAhmed Bougacha   unsigned NumParams = FTy.getNumParams();
615d765a82bSAhmed Bougacha 
616d765a82bSAhmed Bougacha   switch (F) {
617d21529faSDavid L. Jones   case LibFunc_strlen:
618d765a82bSAhmed Bougacha     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
619d765a82bSAhmed Bougacha             FTy.getReturnType()->isIntegerTy());
620d765a82bSAhmed Bougacha 
621d21529faSDavid L. Jones   case LibFunc_strchr:
622d21529faSDavid L. Jones   case LibFunc_strrchr:
623d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
624d765a82bSAhmed Bougacha             FTy.getParamType(0) == FTy.getReturnType() &&
625d765a82bSAhmed Bougacha             FTy.getParamType(1)->isIntegerTy());
626d765a82bSAhmed Bougacha 
627d21529faSDavid L. Jones   case LibFunc_strtol:
628d21529faSDavid L. Jones   case LibFunc_strtod:
629d21529faSDavid L. Jones   case LibFunc_strtof:
630d21529faSDavid L. Jones   case LibFunc_strtoul:
631d21529faSDavid L. Jones   case LibFunc_strtoll:
632d21529faSDavid L. Jones   case LibFunc_strtold:
633d21529faSDavid L. Jones   case LibFunc_strtoull:
634d765a82bSAhmed Bougacha     return ((NumParams == 2 || NumParams == 3) &&
635d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
636d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
637d21529faSDavid L. Jones   case LibFunc_strcat:
638d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
639d765a82bSAhmed Bougacha             FTy.getParamType(0) == FTy.getReturnType() &&
640d765a82bSAhmed Bougacha             FTy.getParamType(1) == FTy.getReturnType());
641d765a82bSAhmed Bougacha 
642d21529faSDavid L. Jones   case LibFunc_strncat:
643d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
644d765a82bSAhmed Bougacha             FTy.getParamType(0) == FTy.getReturnType() &&
645d765a82bSAhmed Bougacha             FTy.getParamType(1) == FTy.getReturnType() &&
6467bd3fb15SIgor Laevsky             IsSizeTTy(FTy.getParamType(2)));
647d765a82bSAhmed Bougacha 
648d21529faSDavid L. Jones   case LibFunc_strcpy_chk:
649d21529faSDavid L. Jones   case LibFunc_stpcpy_chk:
650d765a82bSAhmed Bougacha     --NumParams;
651d765a82bSAhmed Bougacha     if (!IsSizeTTy(FTy.getParamType(NumParams)))
652d765a82bSAhmed Bougacha       return false;
653b03fd12cSJustin Bogner     LLVM_FALLTHROUGH;
654d21529faSDavid L. Jones   case LibFunc_strcpy:
655d21529faSDavid L. Jones   case LibFunc_stpcpy:
656d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(0) &&
657d765a82bSAhmed Bougacha             FTy.getParamType(0) == FTy.getParamType(1) &&
658d765a82bSAhmed Bougacha             FTy.getParamType(0) == PCharTy);
659d765a82bSAhmed Bougacha 
660d21529faSDavid L. Jones   case LibFunc_strncpy_chk:
661d21529faSDavid L. Jones   case LibFunc_stpncpy_chk:
662d765a82bSAhmed Bougacha     --NumParams;
663d765a82bSAhmed Bougacha     if (!IsSizeTTy(FTy.getParamType(NumParams)))
664d765a82bSAhmed Bougacha       return false;
665b03fd12cSJustin Bogner     LLVM_FALLTHROUGH;
666d21529faSDavid L. Jones   case LibFunc_strncpy:
667d21529faSDavid L. Jones   case LibFunc_stpncpy:
668d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
669d765a82bSAhmed Bougacha             FTy.getParamType(0) == FTy.getParamType(1) &&
670d765a82bSAhmed Bougacha             FTy.getParamType(0) == PCharTy &&
6717bd3fb15SIgor Laevsky             IsSizeTTy(FTy.getParamType(2)));
672d765a82bSAhmed Bougacha 
673d21529faSDavid L. Jones   case LibFunc_strxfrm:
674d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
675d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
676d765a82bSAhmed Bougacha 
677d21529faSDavid L. Jones   case LibFunc_strcmp:
678d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType()->isIntegerTy(32) &&
679d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
680d765a82bSAhmed Bougacha             FTy.getParamType(0) == FTy.getParamType(1));
681d765a82bSAhmed Bougacha 
682d21529faSDavid L. Jones   case LibFunc_strncmp:
683d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
684d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
685d765a82bSAhmed Bougacha             FTy.getParamType(0) == FTy.getParamType(1) &&
6867bd3fb15SIgor Laevsky             IsSizeTTy(FTy.getParamType(2)));
687d765a82bSAhmed Bougacha 
688d21529faSDavid L. Jones   case LibFunc_strspn:
689d21529faSDavid L. Jones   case LibFunc_strcspn:
690d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
691d765a82bSAhmed Bougacha             FTy.getParamType(0) == FTy.getParamType(1) &&
692d765a82bSAhmed Bougacha             FTy.getReturnType()->isIntegerTy());
693d765a82bSAhmed Bougacha 
694d21529faSDavid L. Jones   case LibFunc_strcoll:
695d21529faSDavid L. Jones   case LibFunc_strcasecmp:
696d21529faSDavid L. Jones   case LibFunc_strncasecmp:
697d765a82bSAhmed Bougacha     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
698d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
699d765a82bSAhmed Bougacha 
700d21529faSDavid L. Jones   case LibFunc_strstr:
701d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
702d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
703d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
704d765a82bSAhmed Bougacha 
705d21529faSDavid L. Jones   case LibFunc_strpbrk:
706d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
707d765a82bSAhmed Bougacha             FTy.getReturnType() == FTy.getParamType(0) &&
708d765a82bSAhmed Bougacha             FTy.getParamType(0) == FTy.getParamType(1));
709d765a82bSAhmed Bougacha 
710d21529faSDavid L. Jones   case LibFunc_strtok:
711d21529faSDavid L. Jones   case LibFunc_strtok_r:
712d765a82bSAhmed Bougacha     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
713d21529faSDavid L. Jones   case LibFunc_scanf:
714d21529faSDavid L. Jones   case LibFunc_setbuf:
715d21529faSDavid L. Jones   case LibFunc_setvbuf:
716d765a82bSAhmed Bougacha     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
717d21529faSDavid L. Jones   case LibFunc_strdup:
718d21529faSDavid L. Jones   case LibFunc_strndup:
719d765a82bSAhmed Bougacha     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
720d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy());
721d21529faSDavid L. Jones   case LibFunc_sscanf:
722d21529faSDavid L. Jones   case LibFunc_stat:
723d21529faSDavid L. Jones   case LibFunc_statvfs:
724d21529faSDavid L. Jones   case LibFunc_siprintf:
725d21529faSDavid L. Jones   case LibFunc_sprintf:
726d765a82bSAhmed Bougacha     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
7270d7c3775SMartin Storsjo             FTy.getParamType(1)->isPointerTy() &&
7280d7c3775SMartin Storsjo             FTy.getReturnType()->isIntegerTy(32));
729d21529faSDavid L. Jones   case LibFunc_snprintf:
730d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
7310d7c3775SMartin Storsjo             FTy.getParamType(2)->isPointerTy() &&
7320d7c3775SMartin Storsjo             FTy.getReturnType()->isIntegerTy(32));
733d21529faSDavid L. Jones   case LibFunc_setitimer:
734d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
735d765a82bSAhmed Bougacha             FTy.getParamType(2)->isPointerTy());
736d21529faSDavid L. Jones   case LibFunc_system:
737d765a82bSAhmed Bougacha     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
738d21529faSDavid L. Jones   case LibFunc_malloc:
739d765a82bSAhmed Bougacha     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
740d21529faSDavid L. Jones   case LibFunc_memcmp:
7416b9be1dbSAhmed Bougacha     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
7426b9be1dbSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
7436b9be1dbSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
744d765a82bSAhmed Bougacha 
745d21529faSDavid L. Jones   case LibFunc_memchr:
746d21529faSDavid L. Jones   case LibFunc_memrchr:
7476b9be1dbSAhmed Bougacha     return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
7486b9be1dbSAhmed Bougacha             FTy.getReturnType() == FTy.getParamType(0) &&
749d765a82bSAhmed Bougacha             FTy.getParamType(1)->isIntegerTy(32) &&
7506b9be1dbSAhmed Bougacha             IsSizeTTy(FTy.getParamType(2)));
751d21529faSDavid L. Jones   case LibFunc_modf:
752d21529faSDavid L. Jones   case LibFunc_modff:
753d21529faSDavid L. Jones   case LibFunc_modfl:
754d765a82bSAhmed Bougacha     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
755d765a82bSAhmed Bougacha 
756d21529faSDavid L. Jones   case LibFunc_memcpy_chk:
757d21529faSDavid L. Jones   case LibFunc_memmove_chk:
758d765a82bSAhmed Bougacha     --NumParams;
759d765a82bSAhmed Bougacha     if (!IsSizeTTy(FTy.getParamType(NumParams)))
760d765a82bSAhmed Bougacha       return false;
761b03fd12cSJustin Bogner     LLVM_FALLTHROUGH;
762d21529faSDavid L. Jones   case LibFunc_memcpy:
763d21529faSDavid L. Jones   case LibFunc_mempcpy:
764d21529faSDavid L. Jones   case LibFunc_memmove:
765d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
766d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
767d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy() &&
768d765a82bSAhmed Bougacha             IsSizeTTy(FTy.getParamType(2)));
769d765a82bSAhmed Bougacha 
770d21529faSDavid L. Jones   case LibFunc_memset_chk:
771d765a82bSAhmed Bougacha     --NumParams;
772d765a82bSAhmed Bougacha     if (!IsSizeTTy(FTy.getParamType(NumParams)))
773d765a82bSAhmed Bougacha       return false;
774b03fd12cSJustin Bogner     LLVM_FALLTHROUGH;
775d21529faSDavid L. Jones   case LibFunc_memset:
776d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
777d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
778d765a82bSAhmed Bougacha             FTy.getParamType(1)->isIntegerTy() &&
779d765a82bSAhmed Bougacha             IsSizeTTy(FTy.getParamType(2)));
780d765a82bSAhmed Bougacha 
781d21529faSDavid L. Jones   case LibFunc_memccpy:
782d765a82bSAhmed Bougacha     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
783d21529faSDavid L. Jones   case LibFunc_memalign:
784d765a82bSAhmed Bougacha     return (FTy.getReturnType()->isPointerTy());
785d21529faSDavid L. Jones   case LibFunc_realloc:
786d21529faSDavid L. Jones   case LibFunc_reallocf:
7876b9be1dbSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType() == PCharTy &&
7886b9be1dbSAhmed Bougacha             FTy.getParamType(0) == FTy.getReturnType() &&
7896b9be1dbSAhmed Bougacha             IsSizeTTy(FTy.getParamType(1)));
790d21529faSDavid L. Jones   case LibFunc_read:
791d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
792d21529faSDavid L. Jones   case LibFunc_rewind:
793d21529faSDavid L. Jones   case LibFunc_rmdir:
794d21529faSDavid L. Jones   case LibFunc_remove:
795d21529faSDavid L. Jones   case LibFunc_realpath:
796d765a82bSAhmed Bougacha     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
797d21529faSDavid L. Jones   case LibFunc_rename:
798d765a82bSAhmed Bougacha     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
799d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
800d21529faSDavid L. Jones   case LibFunc_readlink:
801d765a82bSAhmed Bougacha     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
802d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
803d21529faSDavid L. Jones   case LibFunc_write:
804d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
805d21529faSDavid L. Jones   case LibFunc_bcopy:
806d21529faSDavid L. Jones   case LibFunc_bcmp:
807d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
808d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
809d21529faSDavid L. Jones   case LibFunc_bzero:
810d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
811d21529faSDavid L. Jones   case LibFunc_calloc:
812d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
8131fe3f1caSAhmed Bougacha 
814d21529faSDavid L. Jones   case LibFunc_atof:
815d21529faSDavid L. Jones   case LibFunc_atoi:
816d21529faSDavid L. Jones   case LibFunc_atol:
817d21529faSDavid L. Jones   case LibFunc_atoll:
818d21529faSDavid L. Jones   case LibFunc_ferror:
819d21529faSDavid L. Jones   case LibFunc_getenv:
820d21529faSDavid L. Jones   case LibFunc_getpwnam:
821d21529faSDavid L. Jones   case LibFunc_iprintf:
822d21529faSDavid L. Jones   case LibFunc_pclose:
823d21529faSDavid L. Jones   case LibFunc_perror:
824d21529faSDavid L. Jones   case LibFunc_printf:
825d21529faSDavid L. Jones   case LibFunc_puts:
826d21529faSDavid L. Jones   case LibFunc_uname:
827d21529faSDavid L. Jones   case LibFunc_under_IO_getc:
828d21529faSDavid L. Jones   case LibFunc_unlink:
829d21529faSDavid L. Jones   case LibFunc_unsetenv:
830d765a82bSAhmed Bougacha     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
8311fe3f1caSAhmed Bougacha 
832d21529faSDavid L. Jones   case LibFunc_access:
833d21529faSDavid L. Jones   case LibFunc_chmod:
834d21529faSDavid L. Jones   case LibFunc_chown:
835d21529faSDavid L. Jones   case LibFunc_clearerr:
836d21529faSDavid L. Jones   case LibFunc_closedir:
837d21529faSDavid L. Jones   case LibFunc_ctermid:
838d21529faSDavid L. Jones   case LibFunc_fclose:
839d21529faSDavid L. Jones   case LibFunc_feof:
840d21529faSDavid L. Jones   case LibFunc_fflush:
841d21529faSDavid L. Jones   case LibFunc_fgetc:
842ca22d427SDavid Bolvansky   case LibFunc_fgetc_unlocked:
843d21529faSDavid L. Jones   case LibFunc_fileno:
844d21529faSDavid L. Jones   case LibFunc_flockfile:
845d21529faSDavid L. Jones   case LibFunc_free:
846d21529faSDavid L. Jones   case LibFunc_fseek:
847d21529faSDavid L. Jones   case LibFunc_fseeko64:
848d21529faSDavid L. Jones   case LibFunc_fseeko:
849d21529faSDavid L. Jones   case LibFunc_fsetpos:
850d21529faSDavid L. Jones   case LibFunc_ftell:
851d21529faSDavid L. Jones   case LibFunc_ftello64:
852d21529faSDavid L. Jones   case LibFunc_ftello:
853d21529faSDavid L. Jones   case LibFunc_ftrylockfile:
854d21529faSDavid L. Jones   case LibFunc_funlockfile:
855d21529faSDavid L. Jones   case LibFunc_getc:
856d21529faSDavid L. Jones   case LibFunc_getc_unlocked:
857d21529faSDavid L. Jones   case LibFunc_getlogin_r:
858d21529faSDavid L. Jones   case LibFunc_mkdir:
859d21529faSDavid L. Jones   case LibFunc_mktime:
860d21529faSDavid L. Jones   case LibFunc_times:
8611fe3f1caSAhmed Bougacha     return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
8621fe3f1caSAhmed Bougacha 
863d21529faSDavid L. Jones   case LibFunc_fopen:
864d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
865d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
866d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
867d21529faSDavid L. Jones   case LibFunc_fdopen:
868d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
869d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
870d21529faSDavid L. Jones   case LibFunc_fputc:
871ca22d427SDavid Bolvansky   case LibFunc_fputc_unlocked:
872d21529faSDavid L. Jones   case LibFunc_fstat:
873d21529faSDavid L. Jones   case LibFunc_frexp:
874d21529faSDavid L. Jones   case LibFunc_frexpf:
875d21529faSDavid L. Jones   case LibFunc_frexpl:
876d21529faSDavid L. Jones   case LibFunc_fstatvfs:
877d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
878d21529faSDavid L. Jones   case LibFunc_fgets:
879ca22d427SDavid Bolvansky   case LibFunc_fgets_unlocked:
880d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
881d765a82bSAhmed Bougacha             FTy.getParamType(2)->isPointerTy());
882d21529faSDavid L. Jones   case LibFunc_fread:
883ca22d427SDavid Bolvansky   case LibFunc_fread_unlocked:
884d765a82bSAhmed Bougacha     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
885d765a82bSAhmed Bougacha             FTy.getParamType(3)->isPointerTy());
886d21529faSDavid L. Jones   case LibFunc_fwrite:
887ca22d427SDavid Bolvansky   case LibFunc_fwrite_unlocked:
888d765a82bSAhmed Bougacha     return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() &&
889d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
890d765a82bSAhmed Bougacha             FTy.getParamType(1)->isIntegerTy() &&
891d765a82bSAhmed Bougacha             FTy.getParamType(2)->isIntegerTy() &&
892d765a82bSAhmed Bougacha             FTy.getParamType(3)->isPointerTy());
893d21529faSDavid L. Jones   case LibFunc_fputs:
894ca22d427SDavid Bolvansky   case LibFunc_fputs_unlocked:
895d765a82bSAhmed Bougacha     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
896d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
897d21529faSDavid L. Jones   case LibFunc_fscanf:
898d21529faSDavid L. Jones   case LibFunc_fiprintf:
899d21529faSDavid L. Jones   case LibFunc_fprintf:
9006b9be1dbSAhmed Bougacha     return (NumParams >= 2 && FTy.getReturnType()->isIntegerTy() &&
9016b9be1dbSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
902d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
903d21529faSDavid L. Jones   case LibFunc_fgetpos:
904d765a82bSAhmed Bougacha     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
905d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
906d21529faSDavid L. Jones   case LibFunc_getchar:
907ca22d427SDavid Bolvansky   case LibFunc_getchar_unlocked:
9086b9be1dbSAhmed Bougacha     return (NumParams == 0 && FTy.getReturnType()->isIntegerTy());
909d21529faSDavid L. Jones   case LibFunc_gets:
9106b9be1dbSAhmed Bougacha     return (NumParams == 1 && FTy.getParamType(0) == PCharTy);
911d21529faSDavid L. Jones   case LibFunc_getitimer:
912d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
913d21529faSDavid L. Jones   case LibFunc_ungetc:
914d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
915d21529faSDavid L. Jones   case LibFunc_utime:
916d21529faSDavid L. Jones   case LibFunc_utimes:
917d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
918d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
919d21529faSDavid L. Jones   case LibFunc_putc:
920ca22d427SDavid Bolvansky   case LibFunc_putc_unlocked:
921d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
922d21529faSDavid L. Jones   case LibFunc_pread:
923d21529faSDavid L. Jones   case LibFunc_pwrite:
924d765a82bSAhmed Bougacha     return (NumParams == 4 && FTy.getParamType(1)->isPointerTy());
925d21529faSDavid L. Jones   case LibFunc_popen:
926d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
927d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
928d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
929d21529faSDavid L. Jones   case LibFunc_vscanf:
930d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
931d21529faSDavid L. Jones   case LibFunc_vsscanf:
932d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
933d765a82bSAhmed Bougacha             FTy.getParamType(2)->isPointerTy());
934d21529faSDavid L. Jones   case LibFunc_vfscanf:
935d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
936d765a82bSAhmed Bougacha             FTy.getParamType(2)->isPointerTy());
937d21529faSDavid L. Jones   case LibFunc_valloc:
938d765a82bSAhmed Bougacha     return (FTy.getReturnType()->isPointerTy());
939d21529faSDavid L. Jones   case LibFunc_vprintf:
940d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
941d21529faSDavid L. Jones   case LibFunc_vfprintf:
942d21529faSDavid L. Jones   case LibFunc_vsprintf:
943d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
944d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
945d21529faSDavid L. Jones   case LibFunc_vsnprintf:
946d765a82bSAhmed Bougacha     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
947d765a82bSAhmed Bougacha             FTy.getParamType(2)->isPointerTy());
948d21529faSDavid L. Jones   case LibFunc_open:
949d765a82bSAhmed Bougacha     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
950d21529faSDavid L. Jones   case LibFunc_opendir:
951d765a82bSAhmed Bougacha     return (NumParams == 1 && FTy.getReturnType()->isPointerTy() &&
952d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy());
953d21529faSDavid L. Jones   case LibFunc_tmpfile:
954d765a82bSAhmed Bougacha     return (FTy.getReturnType()->isPointerTy());
955d21529faSDavid L. Jones   case LibFunc_htonl:
956d21529faSDavid L. Jones   case LibFunc_ntohl:
9576b9be1dbSAhmed Bougacha     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
9586b9be1dbSAhmed Bougacha             FTy.getReturnType() == FTy.getParamType(0));
959d21529faSDavid L. Jones   case LibFunc_htons:
960d21529faSDavid L. Jones   case LibFunc_ntohs:
9616b9be1dbSAhmed Bougacha     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(16) &&
9626b9be1dbSAhmed Bougacha             FTy.getReturnType() == FTy.getParamType(0));
963d21529faSDavid L. Jones   case LibFunc_lstat:
964d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
965d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
966d21529faSDavid L. Jones   case LibFunc_lchown:
967d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
968d21529faSDavid L. Jones   case LibFunc_qsort:
969d765a82bSAhmed Bougacha     return (NumParams == 4 && FTy.getParamType(3)->isPointerTy());
970d21529faSDavid L. Jones   case LibFunc_dunder_strdup:
971d21529faSDavid L. Jones   case LibFunc_dunder_strndup:
972d765a82bSAhmed Bougacha     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
973d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy());
974d21529faSDavid L. Jones   case LibFunc_dunder_strtok_r:
975d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
976d21529faSDavid L. Jones   case LibFunc_under_IO_putc:
977d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
978d21529faSDavid L. Jones   case LibFunc_dunder_isoc99_scanf:
979d765a82bSAhmed Bougacha     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
980d21529faSDavid L. Jones   case LibFunc_stat64:
981d21529faSDavid L. Jones   case LibFunc_lstat64:
982d21529faSDavid L. Jones   case LibFunc_statvfs64:
98379dcc274SMichael Kuperstein     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
984d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
985d21529faSDavid L. Jones   case LibFunc_dunder_isoc99_sscanf:
98679dcc274SMichael Kuperstein     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
987d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
988d21529faSDavid L. Jones   case LibFunc_fopen64:
989d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
990d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
991d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
992d21529faSDavid L. Jones   case LibFunc_tmpfile64:
993d765a82bSAhmed Bougacha     return (FTy.getReturnType()->isPointerTy());
994d21529faSDavid L. Jones   case LibFunc_fstat64:
995d21529faSDavid L. Jones   case LibFunc_fstatvfs64:
996d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
997d21529faSDavid L. Jones   case LibFunc_open64:
998d765a82bSAhmed Bougacha     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
999d21529faSDavid L. Jones   case LibFunc_gettimeofday:
1000d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
1001d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy());
1002d765a82bSAhmed Bougacha 
10036b9be1dbSAhmed Bougacha   // new(unsigned int);
1004d21529faSDavid L. Jones   case LibFunc_Znwj:
10056b9be1dbSAhmed Bougacha   // new(unsigned long);
1006d21529faSDavid L. Jones   case LibFunc_Znwm:
10076b9be1dbSAhmed Bougacha   // new[](unsigned int);
1008d21529faSDavid L. Jones   case LibFunc_Znaj:
10096b9be1dbSAhmed Bougacha   // new[](unsigned long);
1010d21529faSDavid L. Jones   case LibFunc_Znam:
10116b9be1dbSAhmed Bougacha   // new(unsigned int);
1012d21529faSDavid L. Jones   case LibFunc_msvc_new_int:
10136b9be1dbSAhmed Bougacha   // new(unsigned long long);
1014d21529faSDavid L. Jones   case LibFunc_msvc_new_longlong:
10156b9be1dbSAhmed Bougacha   // new[](unsigned int);
1016d21529faSDavid L. Jones   case LibFunc_msvc_new_array_int:
10176b9be1dbSAhmed Bougacha   // new[](unsigned long long);
1018d21529faSDavid L. Jones   case LibFunc_msvc_new_array_longlong:
10196b9be1dbSAhmed Bougacha     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
10206b9be1dbSAhmed Bougacha 
10216b9be1dbSAhmed Bougacha   // new(unsigned int, nothrow);
1022d21529faSDavid L. Jones   case LibFunc_ZnwjRKSt9nothrow_t:
10236b9be1dbSAhmed Bougacha   // new(unsigned long, nothrow);
1024d21529faSDavid L. Jones   case LibFunc_ZnwmRKSt9nothrow_t:
10256b9be1dbSAhmed Bougacha   // new[](unsigned int, nothrow);
1026d21529faSDavid L. Jones   case LibFunc_ZnajRKSt9nothrow_t:
10276b9be1dbSAhmed Bougacha   // new[](unsigned long, nothrow);
1028d21529faSDavid L. Jones   case LibFunc_ZnamRKSt9nothrow_t:
10296b9be1dbSAhmed Bougacha   // new(unsigned int, nothrow);
1030d21529faSDavid L. Jones   case LibFunc_msvc_new_int_nothrow:
10316b9be1dbSAhmed Bougacha   // new(unsigned long long, nothrow);
1032d21529faSDavid L. Jones   case LibFunc_msvc_new_longlong_nothrow:
10336b9be1dbSAhmed Bougacha   // new[](unsigned int, nothrow);
1034d21529faSDavid L. Jones   case LibFunc_msvc_new_array_int_nothrow:
10356b9be1dbSAhmed Bougacha   // new[](unsigned long long, nothrow);
1036d21529faSDavid L. Jones   case LibFunc_msvc_new_array_longlong_nothrow:
103796bbec79SEric Fiselier   // new(unsigned int, align_val_t)
103896bbec79SEric Fiselier   case LibFunc_ZnwjSt11align_val_t:
103996bbec79SEric Fiselier   // new(unsigned long, align_val_t)
104096bbec79SEric Fiselier   case LibFunc_ZnwmSt11align_val_t:
104196bbec79SEric Fiselier   // new[](unsigned int, align_val_t)
104296bbec79SEric Fiselier   case LibFunc_ZnajSt11align_val_t:
104396bbec79SEric Fiselier   // new[](unsigned long, align_val_t)
104496bbec79SEric Fiselier   case LibFunc_ZnamSt11align_val_t:
10456b9be1dbSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
10466b9be1dbSAhmed Bougacha 
104796bbec79SEric Fiselier   // new(unsigned int, align_val_t, nothrow)
104896bbec79SEric Fiselier   case LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t:
104996bbec79SEric Fiselier   // new(unsigned long, align_val_t, nothrow)
105096bbec79SEric Fiselier   case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t:
105196bbec79SEric Fiselier   // new[](unsigned int, align_val_t, nothrow)
105296bbec79SEric Fiselier   case LibFunc_ZnajSt11align_val_tRKSt9nothrow_t:
105396bbec79SEric Fiselier   // new[](unsigned long, align_val_t, nothrow)
105496bbec79SEric Fiselier   case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t:
105596bbec79SEric Fiselier     return (NumParams == 3 && FTy.getReturnType()->isPointerTy());
105696bbec79SEric Fiselier 
10576b9be1dbSAhmed Bougacha   // void operator delete[](void*);
1058d21529faSDavid L. Jones   case LibFunc_ZdaPv:
10596b9be1dbSAhmed Bougacha   // void operator delete(void*);
1060d21529faSDavid L. Jones   case LibFunc_ZdlPv:
10616b9be1dbSAhmed Bougacha   // void operator delete[](void*);
1062d21529faSDavid L. Jones   case LibFunc_msvc_delete_array_ptr32:
10636b9be1dbSAhmed Bougacha   // void operator delete[](void*);
1064d21529faSDavid L. Jones   case LibFunc_msvc_delete_array_ptr64:
10656b9be1dbSAhmed Bougacha   // void operator delete(void*);
1066d21529faSDavid L. Jones   case LibFunc_msvc_delete_ptr32:
10676b9be1dbSAhmed Bougacha   // void operator delete(void*);
1068d21529faSDavid L. Jones   case LibFunc_msvc_delete_ptr64:
10696b9be1dbSAhmed Bougacha     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
10706b9be1dbSAhmed Bougacha 
10716b9be1dbSAhmed Bougacha   // void operator delete[](void*, nothrow);
1072d21529faSDavid L. Jones   case LibFunc_ZdaPvRKSt9nothrow_t:
10736b9be1dbSAhmed Bougacha   // void operator delete[](void*, unsigned int);
1074d21529faSDavid L. Jones   case LibFunc_ZdaPvj:
10756b9be1dbSAhmed Bougacha   // void operator delete[](void*, unsigned long);
1076d21529faSDavid L. Jones   case LibFunc_ZdaPvm:
10776b9be1dbSAhmed Bougacha   // void operator delete(void*, nothrow);
1078d21529faSDavid L. Jones   case LibFunc_ZdlPvRKSt9nothrow_t:
10796b9be1dbSAhmed Bougacha   // void operator delete(void*, unsigned int);
1080d21529faSDavid L. Jones   case LibFunc_ZdlPvj:
10816b9be1dbSAhmed Bougacha   // void operator delete(void*, unsigned long);
1082d21529faSDavid L. Jones   case LibFunc_ZdlPvm:
108396bbec79SEric Fiselier   // void operator delete(void*, align_val_t)
108496bbec79SEric Fiselier   case LibFunc_ZdlPvSt11align_val_t:
108596bbec79SEric Fiselier   // void operator delete[](void*, align_val_t)
108696bbec79SEric Fiselier   case LibFunc_ZdaPvSt11align_val_t:
10876b9be1dbSAhmed Bougacha   // void operator delete[](void*, unsigned int);
1088d21529faSDavid L. Jones   case LibFunc_msvc_delete_array_ptr32_int:
10896b9be1dbSAhmed Bougacha   // void operator delete[](void*, nothrow);
1090d21529faSDavid L. Jones   case LibFunc_msvc_delete_array_ptr32_nothrow:
10916b9be1dbSAhmed Bougacha   // void operator delete[](void*, unsigned long long);
1092d21529faSDavid L. Jones   case LibFunc_msvc_delete_array_ptr64_longlong:
10936b9be1dbSAhmed Bougacha   // void operator delete[](void*, nothrow);
1094d21529faSDavid L. Jones   case LibFunc_msvc_delete_array_ptr64_nothrow:
10956b9be1dbSAhmed Bougacha   // void operator delete(void*, unsigned int);
1096d21529faSDavid L. Jones   case LibFunc_msvc_delete_ptr32_int:
10976b9be1dbSAhmed Bougacha   // void operator delete(void*, nothrow);
1098d21529faSDavid L. Jones   case LibFunc_msvc_delete_ptr32_nothrow:
10996b9be1dbSAhmed Bougacha   // void operator delete(void*, unsigned long long);
1100d21529faSDavid L. Jones   case LibFunc_msvc_delete_ptr64_longlong:
11016b9be1dbSAhmed Bougacha   // void operator delete(void*, nothrow);
1102d21529faSDavid L. Jones   case LibFunc_msvc_delete_ptr64_nothrow:
11036b9be1dbSAhmed Bougacha     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
1104d765a82bSAhmed Bougacha 
110596bbec79SEric Fiselier   // void operator delete(void*, align_val_t, nothrow)
110696bbec79SEric Fiselier   case LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t:
110796bbec79SEric Fiselier   // void operator delete[](void*, align_val_t, nothrow)
110896bbec79SEric Fiselier   case LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t:
110996bbec79SEric Fiselier     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
111096bbec79SEric Fiselier 
1111d21529faSDavid L. Jones   case LibFunc_memset_pattern16:
1112d765a82bSAhmed Bougacha     return (!FTy.isVarArg() && NumParams == 3 &&
11136b9be1dbSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
11146b9be1dbSAhmed Bougacha             FTy.getParamType(1)->isPointerTy() &&
11156b9be1dbSAhmed Bougacha             FTy.getParamType(2)->isIntegerTy());
1116d765a82bSAhmed Bougacha 
1117d21529faSDavid L. Jones   case LibFunc_cxa_guard_abort:
1118d21529faSDavid L. Jones   case LibFunc_cxa_guard_acquire:
1119d21529faSDavid L. Jones   case LibFunc_cxa_guard_release:
1120d21529faSDavid L. Jones   case LibFunc_nvvm_reflect:
11216b9be1dbSAhmed Bougacha     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
1122d765a82bSAhmed Bougacha 
1123d21529faSDavid L. Jones   case LibFunc_sincospi_stret:
1124d21529faSDavid L. Jones   case LibFunc_sincospif_stret:
11256b9be1dbSAhmed Bougacha     return (NumParams == 1 && FTy.getParamType(0)->isFloatingPointTy());
11266b9be1dbSAhmed Bougacha 
1127d21529faSDavid L. Jones   case LibFunc_acos:
11283cd8c16dSAndrew Kaylor   case LibFunc_acos_finite:
1129d21529faSDavid L. Jones   case LibFunc_acosf:
11303cd8c16dSAndrew Kaylor   case LibFunc_acosf_finite:
1131d21529faSDavid L. Jones   case LibFunc_acosh:
11323cd8c16dSAndrew Kaylor   case LibFunc_acosh_finite:
1133d21529faSDavid L. Jones   case LibFunc_acoshf:
11343cd8c16dSAndrew Kaylor   case LibFunc_acoshf_finite:
1135d21529faSDavid L. Jones   case LibFunc_acoshl:
11363cd8c16dSAndrew Kaylor   case LibFunc_acoshl_finite:
1137d21529faSDavid L. Jones   case LibFunc_acosl:
11383cd8c16dSAndrew Kaylor   case LibFunc_acosl_finite:
1139d21529faSDavid L. Jones   case LibFunc_asin:
11403cd8c16dSAndrew Kaylor   case LibFunc_asin_finite:
1141d21529faSDavid L. Jones   case LibFunc_asinf:
11423cd8c16dSAndrew Kaylor   case LibFunc_asinf_finite:
1143d21529faSDavid L. Jones   case LibFunc_asinh:
1144d21529faSDavid L. Jones   case LibFunc_asinhf:
1145d21529faSDavid L. Jones   case LibFunc_asinhl:
1146d21529faSDavid L. Jones   case LibFunc_asinl:
11473cd8c16dSAndrew Kaylor   case LibFunc_asinl_finite:
1148d21529faSDavid L. Jones   case LibFunc_atan:
1149d21529faSDavid L. Jones   case LibFunc_atanf:
1150d21529faSDavid L. Jones   case LibFunc_atanh:
11513cd8c16dSAndrew Kaylor   case LibFunc_atanh_finite:
1152d21529faSDavid L. Jones   case LibFunc_atanhf:
11533cd8c16dSAndrew Kaylor   case LibFunc_atanhf_finite:
1154d21529faSDavid L. Jones   case LibFunc_atanhl:
11553cd8c16dSAndrew Kaylor   case LibFunc_atanhl_finite:
1156d21529faSDavid L. Jones   case LibFunc_atanl:
1157d21529faSDavid L. Jones   case LibFunc_cbrt:
1158d21529faSDavid L. Jones   case LibFunc_cbrtf:
1159d21529faSDavid L. Jones   case LibFunc_cbrtl:
1160d21529faSDavid L. Jones   case LibFunc_ceil:
1161d21529faSDavid L. Jones   case LibFunc_ceilf:
1162d21529faSDavid L. Jones   case LibFunc_ceill:
1163d21529faSDavid L. Jones   case LibFunc_cos:
1164d21529faSDavid L. Jones   case LibFunc_cosf:
1165d21529faSDavid L. Jones   case LibFunc_cosh:
11663cd8c16dSAndrew Kaylor   case LibFunc_cosh_finite:
1167d21529faSDavid L. Jones   case LibFunc_coshf:
11683cd8c16dSAndrew Kaylor   case LibFunc_coshf_finite:
1169d21529faSDavid L. Jones   case LibFunc_coshl:
11703cd8c16dSAndrew Kaylor   case LibFunc_coshl_finite:
1171d21529faSDavid L. Jones   case LibFunc_cosl:
1172d21529faSDavid L. Jones   case LibFunc_exp10:
11733cd8c16dSAndrew Kaylor   case LibFunc_exp10_finite:
1174d21529faSDavid L. Jones   case LibFunc_exp10f:
11753cd8c16dSAndrew Kaylor   case LibFunc_exp10f_finite:
1176d21529faSDavid L. Jones   case LibFunc_exp10l:
11773cd8c16dSAndrew Kaylor   case LibFunc_exp10l_finite:
1178d21529faSDavid L. Jones   case LibFunc_exp2:
11793cd8c16dSAndrew Kaylor   case LibFunc_exp2_finite:
1180d21529faSDavid L. Jones   case LibFunc_exp2f:
11813cd8c16dSAndrew Kaylor   case LibFunc_exp2f_finite:
1182d21529faSDavid L. Jones   case LibFunc_exp2l:
11833cd8c16dSAndrew Kaylor   case LibFunc_exp2l_finite:
1184d21529faSDavid L. Jones   case LibFunc_exp:
11853cd8c16dSAndrew Kaylor   case LibFunc_exp_finite:
1186d21529faSDavid L. Jones   case LibFunc_expf:
11873cd8c16dSAndrew Kaylor   case LibFunc_expf_finite:
1188d21529faSDavid L. Jones   case LibFunc_expl:
11893cd8c16dSAndrew Kaylor   case LibFunc_expl_finite:
1190d21529faSDavid L. Jones   case LibFunc_expm1:
1191d21529faSDavid L. Jones   case LibFunc_expm1f:
1192d21529faSDavid L. Jones   case LibFunc_expm1l:
1193d21529faSDavid L. Jones   case LibFunc_fabs:
1194d21529faSDavid L. Jones   case LibFunc_fabsf:
1195d21529faSDavid L. Jones   case LibFunc_fabsl:
1196d21529faSDavid L. Jones   case LibFunc_floor:
1197d21529faSDavid L. Jones   case LibFunc_floorf:
1198d21529faSDavid L. Jones   case LibFunc_floorl:
1199d21529faSDavid L. Jones   case LibFunc_log10:
12003cd8c16dSAndrew Kaylor   case LibFunc_log10_finite:
1201d21529faSDavid L. Jones   case LibFunc_log10f:
12023cd8c16dSAndrew Kaylor   case LibFunc_log10f_finite:
1203d21529faSDavid L. Jones   case LibFunc_log10l:
12043cd8c16dSAndrew Kaylor   case LibFunc_log10l_finite:
1205d21529faSDavid L. Jones   case LibFunc_log1p:
1206d21529faSDavid L. Jones   case LibFunc_log1pf:
1207d21529faSDavid L. Jones   case LibFunc_log1pl:
1208d21529faSDavid L. Jones   case LibFunc_log2:
12093cd8c16dSAndrew Kaylor   case LibFunc_log2_finite:
1210d21529faSDavid L. Jones   case LibFunc_log2f:
12113cd8c16dSAndrew Kaylor   case LibFunc_log2f_finite:
1212d21529faSDavid L. Jones   case LibFunc_log2l:
12133cd8c16dSAndrew Kaylor   case LibFunc_log2l_finite:
1214d21529faSDavid L. Jones   case LibFunc_log:
12153cd8c16dSAndrew Kaylor   case LibFunc_log_finite:
1216d21529faSDavid L. Jones   case LibFunc_logb:
1217d21529faSDavid L. Jones   case LibFunc_logbf:
1218d21529faSDavid L. Jones   case LibFunc_logbl:
1219d21529faSDavid L. Jones   case LibFunc_logf:
12203cd8c16dSAndrew Kaylor   case LibFunc_logf_finite:
1221d21529faSDavid L. Jones   case LibFunc_logl:
12223cd8c16dSAndrew Kaylor   case LibFunc_logl_finite:
1223d21529faSDavid L. Jones   case LibFunc_nearbyint:
1224d21529faSDavid L. Jones   case LibFunc_nearbyintf:
1225d21529faSDavid L. Jones   case LibFunc_nearbyintl:
1226d21529faSDavid L. Jones   case LibFunc_rint:
1227d21529faSDavid L. Jones   case LibFunc_rintf:
1228d21529faSDavid L. Jones   case LibFunc_rintl:
1229d21529faSDavid L. Jones   case LibFunc_round:
1230d21529faSDavid L. Jones   case LibFunc_roundf:
1231d21529faSDavid L. Jones   case LibFunc_roundl:
1232d21529faSDavid L. Jones   case LibFunc_sin:
1233d21529faSDavid L. Jones   case LibFunc_sinf:
1234d21529faSDavid L. Jones   case LibFunc_sinh:
12353cd8c16dSAndrew Kaylor   case LibFunc_sinh_finite:
1236d21529faSDavid L. Jones   case LibFunc_sinhf:
12373cd8c16dSAndrew Kaylor   case LibFunc_sinhf_finite:
1238d21529faSDavid L. Jones   case LibFunc_sinhl:
12393cd8c16dSAndrew Kaylor   case LibFunc_sinhl_finite:
1240d21529faSDavid L. Jones   case LibFunc_sinl:
1241d21529faSDavid L. Jones   case LibFunc_sqrt:
1242d21529faSDavid L. Jones   case LibFunc_sqrt_finite:
1243d21529faSDavid L. Jones   case LibFunc_sqrtf:
1244d21529faSDavid L. Jones   case LibFunc_sqrtf_finite:
1245d21529faSDavid L. Jones   case LibFunc_sqrtl:
1246d21529faSDavid L. Jones   case LibFunc_sqrtl_finite:
1247d21529faSDavid L. Jones   case LibFunc_tan:
1248d21529faSDavid L. Jones   case LibFunc_tanf:
1249d21529faSDavid L. Jones   case LibFunc_tanh:
1250d21529faSDavid L. Jones   case LibFunc_tanhf:
1251d21529faSDavid L. Jones   case LibFunc_tanhl:
1252d21529faSDavid L. Jones   case LibFunc_tanl:
1253d21529faSDavid L. Jones   case LibFunc_trunc:
1254d21529faSDavid L. Jones   case LibFunc_truncf:
1255d21529faSDavid L. Jones   case LibFunc_truncl:
1256d765a82bSAhmed Bougacha     return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() &&
1257d765a82bSAhmed Bougacha             FTy.getReturnType() == FTy.getParamType(0));
1258d765a82bSAhmed Bougacha 
1259d21529faSDavid L. Jones   case LibFunc_atan2:
12603cd8c16dSAndrew Kaylor   case LibFunc_atan2_finite:
1261d21529faSDavid L. Jones   case LibFunc_atan2f:
12623cd8c16dSAndrew Kaylor   case LibFunc_atan2f_finite:
1263d21529faSDavid L. Jones   case LibFunc_atan2l:
12643cd8c16dSAndrew Kaylor   case LibFunc_atan2l_finite:
1265d21529faSDavid L. Jones   case LibFunc_fmin:
1266d21529faSDavid L. Jones   case LibFunc_fminf:
1267d21529faSDavid L. Jones   case LibFunc_fminl:
1268d21529faSDavid L. Jones   case LibFunc_fmax:
1269d21529faSDavid L. Jones   case LibFunc_fmaxf:
1270d21529faSDavid L. Jones   case LibFunc_fmaxl:
1271d21529faSDavid L. Jones   case LibFunc_fmod:
1272d21529faSDavid L. Jones   case LibFunc_fmodf:
1273d21529faSDavid L. Jones   case LibFunc_fmodl:
1274d21529faSDavid L. Jones   case LibFunc_copysign:
1275d21529faSDavid L. Jones   case LibFunc_copysignf:
1276d21529faSDavid L. Jones   case LibFunc_copysignl:
1277d21529faSDavid L. Jones   case LibFunc_pow:
12783cd8c16dSAndrew Kaylor   case LibFunc_pow_finite:
1279d21529faSDavid L. Jones   case LibFunc_powf:
12803cd8c16dSAndrew Kaylor   case LibFunc_powf_finite:
1281d21529faSDavid L. Jones   case LibFunc_powl:
12823cd8c16dSAndrew Kaylor   case LibFunc_powl_finite:
1283d765a82bSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
1284d765a82bSAhmed Bougacha             FTy.getReturnType() == FTy.getParamType(0) &&
1285d765a82bSAhmed Bougacha             FTy.getReturnType() == FTy.getParamType(1));
1286d765a82bSAhmed Bougacha 
1287d21529faSDavid L. Jones   case LibFunc_ldexp:
1288d21529faSDavid L. Jones   case LibFunc_ldexpf:
1289d21529faSDavid L. Jones   case LibFunc_ldexpl:
12906b9be1dbSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
12916b9be1dbSAhmed Bougacha             FTy.getReturnType() == FTy.getParamType(0) &&
12926b9be1dbSAhmed Bougacha             FTy.getParamType(1)->isIntegerTy(32));
12936b9be1dbSAhmed Bougacha 
1294d21529faSDavid L. Jones   case LibFunc_ffs:
1295d21529faSDavid L. Jones   case LibFunc_ffsl:
1296d21529faSDavid L. Jones   case LibFunc_ffsll:
1297d21529faSDavid L. Jones   case LibFunc_fls:
1298d21529faSDavid L. Jones   case LibFunc_flsl:
1299d21529faSDavid L. Jones   case LibFunc_flsll:
130004949fafSSanjay Patel     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
130104949fafSSanjay Patel             FTy.getParamType(0)->isIntegerTy());
130204949fafSSanjay Patel 
1303d21529faSDavid L. Jones   case LibFunc_isdigit:
1304d21529faSDavid L. Jones   case LibFunc_isascii:
1305d21529faSDavid L. Jones   case LibFunc_toascii:
1306d21529faSDavid L. Jones   case LibFunc_putchar:
1307ca22d427SDavid Bolvansky   case LibFunc_putchar_unlocked:
1308d765a82bSAhmed Bougacha     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
130904949fafSSanjay Patel             FTy.getReturnType() == FTy.getParamType(0));
1310d765a82bSAhmed Bougacha 
1311d21529faSDavid L. Jones   case LibFunc_abs:
1312d21529faSDavid L. Jones   case LibFunc_labs:
1313d21529faSDavid L. Jones   case LibFunc_llabs:
1314d765a82bSAhmed Bougacha     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() &&
1315d765a82bSAhmed Bougacha             FTy.getReturnType() == FTy.getParamType(0));
1316d765a82bSAhmed Bougacha 
1317d21529faSDavid L. Jones   case LibFunc_cxa_atexit:
1318d765a82bSAhmed Bougacha     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() &&
1319d765a82bSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
1320d765a82bSAhmed Bougacha             FTy.getParamType(1)->isPointerTy() &&
1321d765a82bSAhmed Bougacha             FTy.getParamType(2)->isPointerTy());
1322d765a82bSAhmed Bougacha 
1323d21529faSDavid L. Jones   case LibFunc_sinpi:
1324d21529faSDavid L. Jones   case LibFunc_cospi:
1325d765a82bSAhmed Bougacha     return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() &&
1326d765a82bSAhmed Bougacha             FTy.getReturnType() == FTy.getParamType(0));
1327d765a82bSAhmed Bougacha 
1328d21529faSDavid L. Jones   case LibFunc_sinpif:
1329d21529faSDavid L. Jones   case LibFunc_cospif:
1330d765a82bSAhmed Bougacha     return (NumParams == 1 && FTy.getReturnType()->isFloatTy() &&
1331d765a82bSAhmed Bougacha             FTy.getReturnType() == FTy.getParamType(0));
1332d765a82bSAhmed Bougacha 
1333d21529faSDavid L. Jones   case LibFunc_strnlen:
13346b9be1dbSAhmed Bougacha     return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(1) &&
13356b9be1dbSAhmed Bougacha             FTy.getParamType(0) == PCharTy &&
13366b9be1dbSAhmed Bougacha             FTy.getParamType(1) == SizeTTy);
13376b9be1dbSAhmed Bougacha 
1338d21529faSDavid L. Jones   case LibFunc_posix_memalign:
13396b9be1dbSAhmed Bougacha     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
13406b9be1dbSAhmed Bougacha             FTy.getParamType(0)->isPointerTy() &&
13416b9be1dbSAhmed Bougacha             FTy.getParamType(1) == SizeTTy && FTy.getParamType(2) == SizeTTy);
13426b9be1dbSAhmed Bougacha 
134360b40b8fSMatthias Braun   case LibFunc_wcslen:
134460b40b8fSMatthias Braun     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
134560b40b8fSMatthias Braun             FTy.getReturnType()->isIntegerTy());
134660b40b8fSMatthias Braun 
13472ff24731SHal Finkel   case LibFunc_cabs:
13482ff24731SHal Finkel   case LibFunc_cabsf:
13492ff24731SHal Finkel   case LibFunc_cabsl: {
13502ff24731SHal Finkel     Type* RetTy = FTy.getReturnType();
13512ff24731SHal Finkel     if (!RetTy->isFloatingPointTy())
13522ff24731SHal Finkel       return false;
13532ff24731SHal Finkel 
13542ff24731SHal Finkel     // NOTE: These prototypes are target specific and currently support
13552ff24731SHal Finkel     // "complex" passed as an array or discrete real & imaginary parameters.
13562ff24731SHal Finkel     // Add other calling conventions to enable libcall optimizations.
13572ff24731SHal Finkel     if (NumParams == 1)
13582ff24731SHal Finkel       return (FTy.getParamType(0)->isArrayTy() &&
13592ff24731SHal Finkel               FTy.getParamType(0)->getArrayNumElements() == 2 &&
13602ff24731SHal Finkel               FTy.getParamType(0)->getArrayElementType() == RetTy);
13612ff24731SHal Finkel     else if (NumParams == 2)
13622ff24731SHal Finkel       return (FTy.getParamType(0) == RetTy && FTy.getParamType(1) == RetTy);
13632ff24731SHal Finkel     else
13642ff24731SHal Finkel       return false;
13652ff24731SHal Finkel   }
13666b9be1dbSAhmed Bougacha   case LibFunc::NumLibFuncs:
136786b680a3SAhmed Bougacha     break;
1368d765a82bSAhmed Bougacha   }
13696b9be1dbSAhmed Bougacha 
137086b680a3SAhmed Bougacha   llvm_unreachable("Invalid libfunc");
1371d765a82bSAhmed Bougacha }
1372d765a82bSAhmed Bougacha 
1373d765a82bSAhmed Bougacha bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
1374d21529faSDavid L. Jones                                        LibFunc &F) const {
1375d765a82bSAhmed Bougacha   const DataLayout *DL =
1376d765a82bSAhmed Bougacha       FDecl.getParent() ? &FDecl.getParent()->getDataLayout() : nullptr;
1377d765a82bSAhmed Bougacha   return getLibFunc(FDecl.getName(), F) &&
1378d765a82bSAhmed Bougacha          isValidProtoForLibFunc(*FDecl.getFunctionType(), F, DL);
1379d765a82bSAhmed Bougacha }
1380d765a82bSAhmed Bougacha 
1381c0291865SChandler Carruth void TargetLibraryInfoImpl::disableAllFunctions() {
138262d4215bSChandler Carruth   memset(AvailableArray, 0, sizeof(AvailableArray));
138362d4215bSChandler Carruth }
1384b98f63dbSChandler Carruth 
1385e8f2551fSMichael Zolotukhin static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
13869a72cd7bSMehdi Amini   return LHS.ScalarFnName < RHS.ScalarFnName;
1387e8f2551fSMichael Zolotukhin }
1388e8f2551fSMichael Zolotukhin 
1389e8f2551fSMichael Zolotukhin static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
13909a72cd7bSMehdi Amini   return LHS.VectorFnName < RHS.VectorFnName;
1391e8f2551fSMichael Zolotukhin }
1392e8f2551fSMichael Zolotukhin 
1393e8f2551fSMichael Zolotukhin static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
13949a72cd7bSMehdi Amini   return LHS.ScalarFnName < S;
1395e8f2551fSMichael Zolotukhin }
1396e8f2551fSMichael Zolotukhin 
1397e8f2551fSMichael Zolotukhin static bool compareWithVectorFnName(const VecDesc &LHS, StringRef S) {
13989a72cd7bSMehdi Amini   return LHS.VectorFnName < S;
1399e8f2551fSMichael Zolotukhin }
1400e8f2551fSMichael Zolotukhin 
1401e8f2551fSMichael Zolotukhin void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
1402e8f2551fSMichael Zolotukhin   VectorDescs.insert(VectorDescs.end(), Fns.begin(), Fns.end());
140397bcade7SMandeep Singh Grang   llvm::sort(VectorDescs.begin(), VectorDescs.end(), compareByScalarFnName);
1404e8f2551fSMichael Zolotukhin 
1405e8f2551fSMichael Zolotukhin   ScalarDescs.insert(ScalarDescs.end(), Fns.begin(), Fns.end());
140697bcade7SMandeep Singh Grang   llvm::sort(ScalarDescs.begin(), ScalarDescs.end(), compareByVectorFnName);
1407e8f2551fSMichael Zolotukhin }
1408e8f2551fSMichael Zolotukhin 
14096d8a2aa9SMichael Zolotukhin void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
14106d8a2aa9SMichael Zolotukhin     enum VectorLibrary VecLib) {
14116d8a2aa9SMichael Zolotukhin   switch (VecLib) {
14126d8a2aa9SMichael Zolotukhin   case Accelerate: {
14136d8a2aa9SMichael Zolotukhin     const VecDesc VecFuncs[] = {
1414de63aaceSMichael Zolotukhin         // Floating-Point Arithmetic and Auxiliary Functions
1415de63aaceSMichael Zolotukhin         {"ceilf", "vceilf", 4},
14166d8a2aa9SMichael Zolotukhin         {"fabsf", "vfabsf", 4},
14176d8a2aa9SMichael Zolotukhin         {"llvm.fabs.f32", "vfabsf", 4},
1418de63aaceSMichael Zolotukhin         {"floorf", "vfloorf", 4},
1419de63aaceSMichael Zolotukhin         {"sqrtf", "vsqrtf", 4},
1420de63aaceSMichael Zolotukhin         {"llvm.sqrt.f32", "vsqrtf", 4},
1421de63aaceSMichael Zolotukhin 
1422de63aaceSMichael Zolotukhin         // Exponential and Logarithmic Functions
1423de63aaceSMichael Zolotukhin         {"expf", "vexpf", 4},
1424de63aaceSMichael Zolotukhin         {"llvm.exp.f32", "vexpf", 4},
1425de63aaceSMichael Zolotukhin         {"expm1f", "vexpm1f", 4},
1426de63aaceSMichael Zolotukhin         {"logf", "vlogf", 4},
1427de63aaceSMichael Zolotukhin         {"llvm.log.f32", "vlogf", 4},
1428de63aaceSMichael Zolotukhin         {"log1pf", "vlog1pf", 4},
1429de63aaceSMichael Zolotukhin         {"log10f", "vlog10f", 4},
1430de63aaceSMichael Zolotukhin         {"llvm.log10.f32", "vlog10f", 4},
1431de63aaceSMichael Zolotukhin         {"logbf", "vlogbf", 4},
1432de63aaceSMichael Zolotukhin 
1433de63aaceSMichael Zolotukhin         // Trigonometric Functions
1434de63aaceSMichael Zolotukhin         {"sinf", "vsinf", 4},
1435de63aaceSMichael Zolotukhin         {"llvm.sin.f32", "vsinf", 4},
1436de63aaceSMichael Zolotukhin         {"cosf", "vcosf", 4},
1437de63aaceSMichael Zolotukhin         {"llvm.cos.f32", "vcosf", 4},
1438de63aaceSMichael Zolotukhin         {"tanf", "vtanf", 4},
1439de63aaceSMichael Zolotukhin         {"asinf", "vasinf", 4},
1440de63aaceSMichael Zolotukhin         {"acosf", "vacosf", 4},
1441de63aaceSMichael Zolotukhin         {"atanf", "vatanf", 4},
1442de63aaceSMichael Zolotukhin 
1443de63aaceSMichael Zolotukhin         // Hyperbolic Functions
1444de63aaceSMichael Zolotukhin         {"sinhf", "vsinhf", 4},
1445de63aaceSMichael Zolotukhin         {"coshf", "vcoshf", 4},
1446de63aaceSMichael Zolotukhin         {"tanhf", "vtanhf", 4},
1447de63aaceSMichael Zolotukhin         {"asinhf", "vasinhf", 4},
1448de63aaceSMichael Zolotukhin         {"acoshf", "vacoshf", 4},
1449de63aaceSMichael Zolotukhin         {"atanhf", "vatanhf", 4},
14506d8a2aa9SMichael Zolotukhin     };
14516d8a2aa9SMichael Zolotukhin     addVectorizableFunctions(VecFuncs);
14526d8a2aa9SMichael Zolotukhin     break;
14536d8a2aa9SMichael Zolotukhin   }
1454a6669a1eSMatt Masten   case SVML: {
1455a6669a1eSMatt Masten     const VecDesc VecFuncs[] = {
1456a6669a1eSMatt Masten         {"sin", "__svml_sin2", 2},
1457a6669a1eSMatt Masten         {"sin", "__svml_sin4", 4},
1458a6669a1eSMatt Masten         {"sin", "__svml_sin8", 8},
1459a6669a1eSMatt Masten 
1460a6669a1eSMatt Masten         {"sinf", "__svml_sinf4", 4},
1461a6669a1eSMatt Masten         {"sinf", "__svml_sinf8", 8},
1462a6669a1eSMatt Masten         {"sinf", "__svml_sinf16", 16},
1463a6669a1eSMatt Masten 
1464*4b1205b4SSanjay Patel         {"llvm.sin.f64", "__svml_sin2", 2},
1465*4b1205b4SSanjay Patel         {"llvm.sin.f64", "__svml_sin4", 4},
1466*4b1205b4SSanjay Patel         {"llvm.sin.f64", "__svml_sin8", 8},
1467*4b1205b4SSanjay Patel 
1468*4b1205b4SSanjay Patel         {"llvm.sin.f32", "__svml_sinf4", 4},
1469*4b1205b4SSanjay Patel         {"llvm.sin.f32", "__svml_sinf8", 8},
1470*4b1205b4SSanjay Patel         {"llvm.sin.f32", "__svml_sinf16", 16},
1471*4b1205b4SSanjay Patel 
1472a6669a1eSMatt Masten         {"cos", "__svml_cos2", 2},
1473a6669a1eSMatt Masten         {"cos", "__svml_cos4", 4},
1474a6669a1eSMatt Masten         {"cos", "__svml_cos8", 8},
1475a6669a1eSMatt Masten 
1476a6669a1eSMatt Masten         {"cosf", "__svml_cosf4", 4},
1477a6669a1eSMatt Masten         {"cosf", "__svml_cosf8", 8},
1478a6669a1eSMatt Masten         {"cosf", "__svml_cosf16", 16},
1479a6669a1eSMatt Masten 
1480*4b1205b4SSanjay Patel         {"llvm.cos.f64", "__svml_cos2", 2},
1481*4b1205b4SSanjay Patel         {"llvm.cos.f64", "__svml_cos4", 4},
1482*4b1205b4SSanjay Patel         {"llvm.cos.f64", "__svml_cos8", 8},
1483*4b1205b4SSanjay Patel 
1484*4b1205b4SSanjay Patel         {"llvm.cos.f32", "__svml_cosf4", 4},
1485*4b1205b4SSanjay Patel         {"llvm.cos.f32", "__svml_cosf8", 8},
1486*4b1205b4SSanjay Patel         {"llvm.cos.f32", "__svml_cosf16", 16},
1487*4b1205b4SSanjay Patel 
1488a6669a1eSMatt Masten         {"pow", "__svml_pow2", 2},
1489a6669a1eSMatt Masten         {"pow", "__svml_pow4", 4},
1490a6669a1eSMatt Masten         {"pow", "__svml_pow8", 8},
1491a6669a1eSMatt Masten 
1492a6669a1eSMatt Masten         {"powf", "__svml_powf4", 4},
1493a6669a1eSMatt Masten         {"powf", "__svml_powf8", 8},
1494a6669a1eSMatt Masten         {"powf", "__svml_powf16", 16},
1495a6669a1eSMatt Masten 
1496b01e94eeSAndrew Kaylor         { "__pow_finite", "__svml_pow2", 2 },
1497b01e94eeSAndrew Kaylor         { "__pow_finite", "__svml_pow4", 4 },
1498b01e94eeSAndrew Kaylor         { "__pow_finite", "__svml_pow8", 8 },
1499b01e94eeSAndrew Kaylor 
1500b01e94eeSAndrew Kaylor         { "__powf_finite", "__svml_powf4", 4 },
1501b01e94eeSAndrew Kaylor         { "__powf_finite", "__svml_powf8", 8 },
1502b01e94eeSAndrew Kaylor         { "__powf_finite", "__svml_powf16", 16 },
1503b01e94eeSAndrew Kaylor 
1504a6669a1eSMatt Masten         {"llvm.pow.f64", "__svml_pow2", 2},
1505a6669a1eSMatt Masten         {"llvm.pow.f64", "__svml_pow4", 4},
1506a6669a1eSMatt Masten         {"llvm.pow.f64", "__svml_pow8", 8},
1507a6669a1eSMatt Masten 
1508a6669a1eSMatt Masten         {"llvm.pow.f32", "__svml_powf4", 4},
1509a6669a1eSMatt Masten         {"llvm.pow.f32", "__svml_powf8", 8},
1510a6669a1eSMatt Masten         {"llvm.pow.f32", "__svml_powf16", 16},
1511a6669a1eSMatt Masten 
1512a6669a1eSMatt Masten         {"exp", "__svml_exp2", 2},
1513a6669a1eSMatt Masten         {"exp", "__svml_exp4", 4},
1514a6669a1eSMatt Masten         {"exp", "__svml_exp8", 8},
1515a6669a1eSMatt Masten 
1516a6669a1eSMatt Masten         {"expf", "__svml_expf4", 4},
1517a6669a1eSMatt Masten         {"expf", "__svml_expf8", 8},
1518a6669a1eSMatt Masten         {"expf", "__svml_expf16", 16},
1519a6669a1eSMatt Masten 
1520b01e94eeSAndrew Kaylor         { "__exp_finite", "__svml_exp2", 2 },
1521b01e94eeSAndrew Kaylor         { "__exp_finite", "__svml_exp4", 4 },
1522b01e94eeSAndrew Kaylor         { "__exp_finite", "__svml_exp8", 8 },
1523b01e94eeSAndrew Kaylor 
1524b01e94eeSAndrew Kaylor         { "__expf_finite", "__svml_expf4", 4 },
1525b01e94eeSAndrew Kaylor         { "__expf_finite", "__svml_expf8", 8 },
1526b01e94eeSAndrew Kaylor         { "__expf_finite", "__svml_expf16", 16 },
1527b01e94eeSAndrew Kaylor 
1528a6669a1eSMatt Masten         {"llvm.exp.f64", "__svml_exp2", 2},
1529a6669a1eSMatt Masten         {"llvm.exp.f64", "__svml_exp4", 4},
1530a6669a1eSMatt Masten         {"llvm.exp.f64", "__svml_exp8", 8},
1531a6669a1eSMatt Masten 
1532a6669a1eSMatt Masten         {"llvm.exp.f32", "__svml_expf4", 4},
1533a6669a1eSMatt Masten         {"llvm.exp.f32", "__svml_expf8", 8},
1534a6669a1eSMatt Masten         {"llvm.exp.f32", "__svml_expf16", 16},
1535a6669a1eSMatt Masten 
1536a6669a1eSMatt Masten         {"log", "__svml_log2", 2},
1537a6669a1eSMatt Masten         {"log", "__svml_log4", 4},
1538a6669a1eSMatt Masten         {"log", "__svml_log8", 8},
1539a6669a1eSMatt Masten 
1540a6669a1eSMatt Masten         {"logf", "__svml_logf4", 4},
1541a6669a1eSMatt Masten         {"logf", "__svml_logf8", 8},
1542a6669a1eSMatt Masten         {"logf", "__svml_logf16", 16},
1543a6669a1eSMatt Masten 
1544b01e94eeSAndrew Kaylor         { "__log_finite", "__svml_log2", 2 },
1545b01e94eeSAndrew Kaylor         { "__log_finite", "__svml_log4", 4 },
1546b01e94eeSAndrew Kaylor         { "__log_finite", "__svml_log8", 8 },
1547b01e94eeSAndrew Kaylor 
1548b01e94eeSAndrew Kaylor         { "__logf_finite", "__svml_logf4", 4 },
1549b01e94eeSAndrew Kaylor         { "__logf_finite", "__svml_logf8", 8 },
1550b01e94eeSAndrew Kaylor         { "__logf_finite", "__svml_logf16", 16 },
1551b01e94eeSAndrew Kaylor 
1552a6669a1eSMatt Masten         {"llvm.log.f64", "__svml_log2", 2},
1553a6669a1eSMatt Masten         {"llvm.log.f64", "__svml_log4", 4},
1554a6669a1eSMatt Masten         {"llvm.log.f64", "__svml_log8", 8},
1555a6669a1eSMatt Masten 
1556a6669a1eSMatt Masten         {"llvm.log.f32", "__svml_logf4", 4},
1557a6669a1eSMatt Masten         {"llvm.log.f32", "__svml_logf8", 8},
1558a6669a1eSMatt Masten         {"llvm.log.f32", "__svml_logf16", 16},
1559a6669a1eSMatt Masten     };
1560a6669a1eSMatt Masten     addVectorizableFunctions(VecFuncs);
1561a6669a1eSMatt Masten     break;
1562a6669a1eSMatt Masten   }
15636d8a2aa9SMichael Zolotukhin   case NoLibrary:
15646d8a2aa9SMichael Zolotukhin     break;
15656d8a2aa9SMichael Zolotukhin   }
15666d8a2aa9SMichael Zolotukhin }
15676d8a2aa9SMichael Zolotukhin 
1568e8f2551fSMichael Zolotukhin bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
1569e8f2551fSMichael Zolotukhin   funcName = sanitizeFunctionName(funcName);
1570e8f2551fSMichael Zolotukhin   if (funcName.empty())
1571e8f2551fSMichael Zolotukhin     return false;
1572e8f2551fSMichael Zolotukhin 
1573e8f2551fSMichael Zolotukhin   std::vector<VecDesc>::const_iterator I = std::lower_bound(
1574e8f2551fSMichael Zolotukhin       VectorDescs.begin(), VectorDescs.end(), funcName,
1575e8f2551fSMichael Zolotukhin       compareWithScalarFnName);
1576e8f2551fSMichael Zolotukhin   return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
1577e8f2551fSMichael Zolotukhin }
1578e8f2551fSMichael Zolotukhin 
1579e8f2551fSMichael Zolotukhin StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
1580e8f2551fSMichael Zolotukhin                                                        unsigned VF) const {
1581e8f2551fSMichael Zolotukhin   F = sanitizeFunctionName(F);
1582e8f2551fSMichael Zolotukhin   if (F.empty())
1583e8f2551fSMichael Zolotukhin     return F;
1584e8f2551fSMichael Zolotukhin   std::vector<VecDesc>::const_iterator I = std::lower_bound(
1585e8f2551fSMichael Zolotukhin       VectorDescs.begin(), VectorDescs.end(), F, compareWithScalarFnName);
1586e8f2551fSMichael Zolotukhin   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
1587e8f2551fSMichael Zolotukhin     if (I->VectorizationFactor == VF)
1588e8f2551fSMichael Zolotukhin       return I->VectorFnName;
1589e8f2551fSMichael Zolotukhin     ++I;
1590e8f2551fSMichael Zolotukhin   }
1591e8f2551fSMichael Zolotukhin   return StringRef();
1592e8f2551fSMichael Zolotukhin }
1593e8f2551fSMichael Zolotukhin 
1594e8f2551fSMichael Zolotukhin StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F,
1595e8f2551fSMichael Zolotukhin                                                        unsigned &VF) const {
1596e8f2551fSMichael Zolotukhin   F = sanitizeFunctionName(F);
1597e8f2551fSMichael Zolotukhin   if (F.empty())
1598e8f2551fSMichael Zolotukhin     return F;
1599e8f2551fSMichael Zolotukhin 
1600e8f2551fSMichael Zolotukhin   std::vector<VecDesc>::const_iterator I = std::lower_bound(
1601e8f2551fSMichael Zolotukhin       ScalarDescs.begin(), ScalarDescs.end(), F, compareWithVectorFnName);
1602e8f2551fSMichael Zolotukhin   if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F)
1603e8f2551fSMichael Zolotukhin     return StringRef();
1604e8f2551fSMichael Zolotukhin   VF = I->VectorizationFactor;
1605e8f2551fSMichael Zolotukhin   return I->ScalarFnName;
1606e8f2551fSMichael Zolotukhin }
1607e8f2551fSMichael Zolotukhin 
1608164a2aa6SChandler Carruth TargetLibraryInfo TargetLibraryAnalysis::run(Module &M,
1609164a2aa6SChandler Carruth                                              ModuleAnalysisManager &) {
1610c0291865SChandler Carruth   if (PresetInfoImpl)
1611c0291865SChandler Carruth     return TargetLibraryInfo(*PresetInfoImpl);
1612c0291865SChandler Carruth 
1613c0291865SChandler Carruth   return TargetLibraryInfo(lookupInfoImpl(Triple(M.getTargetTriple())));
1614c0291865SChandler Carruth }
1615c0291865SChandler Carruth 
1616164a2aa6SChandler Carruth TargetLibraryInfo TargetLibraryAnalysis::run(Function &F,
1617164a2aa6SChandler Carruth                                              FunctionAnalysisManager &) {
1618c0291865SChandler Carruth   if (PresetInfoImpl)
1619c0291865SChandler Carruth     return TargetLibraryInfo(*PresetInfoImpl);
1620c0291865SChandler Carruth 
1621c0291865SChandler Carruth   return TargetLibraryInfo(
1622c0291865SChandler Carruth       lookupInfoImpl(Triple(F.getParent()->getTargetTriple())));
1623c0291865SChandler Carruth }
1624c0291865SChandler Carruth 
1625c321e534SBenjamin Kramer TargetLibraryInfoImpl &TargetLibraryAnalysis::lookupInfoImpl(const Triple &T) {
1626c0291865SChandler Carruth   std::unique_ptr<TargetLibraryInfoImpl> &Impl =
1627c0291865SChandler Carruth       Impls[T.normalize()];
1628c0291865SChandler Carruth   if (!Impl)
1629c0291865SChandler Carruth     Impl.reset(new TargetLibraryInfoImpl(T));
1630c0291865SChandler Carruth 
1631c0291865SChandler Carruth   return *Impl;
1632c0291865SChandler Carruth }
1633c0291865SChandler Carruth 
163450ec0b5dSMatthias Braun unsigned TargetLibraryInfoImpl::getWCharSize(const Module &M) const {
163550ec0b5dSMatthias Braun   if (auto *ShortWChar = cast_or_null<ConstantAsMetadata>(
163650ec0b5dSMatthias Braun       M.getModuleFlag("wchar_size")))
163750ec0b5dSMatthias Braun     return cast<ConstantInt>(ShortWChar->getValue())->getZExtValue();
1638cc603ee3SMatthias Braun   return 0;
163950ec0b5dSMatthias Braun }
1640c0291865SChandler Carruth 
1641b98f63dbSChandler Carruth TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
1642c0291865SChandler Carruth     : ImmutablePass(ID), TLIImpl(), TLI(TLIImpl) {
1643b98f63dbSChandler Carruth   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1644b98f63dbSChandler Carruth }
1645b98f63dbSChandler Carruth 
1646b98f63dbSChandler Carruth TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
1647c0291865SChandler Carruth     : ImmutablePass(ID), TLIImpl(T), TLI(TLIImpl) {
1648b98f63dbSChandler Carruth   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1649b98f63dbSChandler Carruth }
1650b98f63dbSChandler Carruth 
1651b98f63dbSChandler Carruth TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
1652c0291865SChandler Carruth     const TargetLibraryInfoImpl &TLIImpl)
1653c0291865SChandler Carruth     : ImmutablePass(ID), TLIImpl(TLIImpl), TLI(this->TLIImpl) {
1654b98f63dbSChandler Carruth   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1655b98f63dbSChandler Carruth }
1656b98f63dbSChandler Carruth 
1657dab4eae2SChandler Carruth AnalysisKey TargetLibraryAnalysis::Key;
1658df0cd726SNAKAMURA Takumi 
1659b98f63dbSChandler Carruth // Register the basic pass.
1660b98f63dbSChandler Carruth INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
1661b98f63dbSChandler Carruth                 "Target Library Information", false, true)
1662b98f63dbSChandler Carruth char TargetLibraryInfoWrapperPass::ID = 0;
1663b98f63dbSChandler Carruth 
1664b98f63dbSChandler Carruth void TargetLibraryInfoWrapperPass::anchor() {}
1665