1 //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the TargetLibraryInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Analysis/TargetLibraryInfo.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/Support/CommandLine.h"
17 using namespace llvm;
18 
19 static cl::opt<TargetLibraryInfoImpl::VectorLibrary> ClVectorLibrary(
20     "vector-library", cl::Hidden, cl::desc("Vector functions library"),
21     cl::init(TargetLibraryInfoImpl::NoLibrary),
22     cl::values(clEnumValN(TargetLibraryInfoImpl::NoLibrary, "none",
23                           "No vector functions library"),
24                clEnumValN(TargetLibraryInfoImpl::Accelerate, "Accelerate",
25                           "Accelerate framework"),
26                clEnumValN(TargetLibraryInfoImpl::SVML, "SVML",
27                           "Intel SVML library"),
28                clEnumValEnd));
29 
30 const char *const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] = {
31 #define TLI_DEFINE_STRING
32 #include "llvm/Analysis/TargetLibraryInfo.def"
33 };
34 
35 static bool hasSinCosPiStret(const Triple &T) {
36   // Only Darwin variants have _stret versions of combined trig functions.
37   if (!T.isOSDarwin())
38     return false;
39 
40   // The ABI is rather complicated on x86, so don't do anything special there.
41   if (T.getArch() == Triple::x86)
42     return false;
43 
44   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
45     return false;
46 
47   if (T.isiOS() && T.isOSVersionLT(7, 0))
48     return false;
49 
50   return true;
51 }
52 
53 /// initialize - Initialize the set of available library functions based on the
54 /// specified target triple.  This should be carefully written so that a missing
55 /// target triple gets a sane set of defaults.
56 static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
57                        ArrayRef<const char *> StandardNames) {
58   // Verify that the StandardNames array is in alphabetical order.
59   assert(std::is_sorted(StandardNames.begin(), StandardNames.end(),
60                         [](const char *LHS, const char *RHS) {
61                           return strcmp(LHS, RHS) < 0;
62                         }) &&
63          "TargetLibraryInfoImpl function names must be sorted");
64 
65   if (T.getArch() == Triple::r600 ||
66       T.getArch() == Triple::amdgcn) {
67     TLI.setUnavailable(LibFunc::ldexp);
68     TLI.setUnavailable(LibFunc::ldexpf);
69     TLI.setUnavailable(LibFunc::ldexpl);
70     TLI.setUnavailable(LibFunc::exp10);
71     TLI.setUnavailable(LibFunc::exp10f);
72     TLI.setUnavailable(LibFunc::exp10l);
73     TLI.setUnavailable(LibFunc::log10);
74     TLI.setUnavailable(LibFunc::log10f);
75     TLI.setUnavailable(LibFunc::log10l);
76   }
77 
78   // There are no library implementations of mempcy and memset for AMD gpus and
79   // these can be difficult to lower in the backend.
80   if (T.getArch() == Triple::r600 ||
81       T.getArch() == Triple::amdgcn) {
82     TLI.setUnavailable(LibFunc::memcpy);
83     TLI.setUnavailable(LibFunc::memset);
84     TLI.setUnavailable(LibFunc::memset_pattern16);
85     return;
86   }
87 
88   // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
89   // All versions of watchOS support it.
90   if (T.isMacOSX()) {
91     if (T.isMacOSXVersionLT(10, 5))
92       TLI.setUnavailable(LibFunc::memset_pattern16);
93   } else if (T.isiOS()) {
94     if (T.isOSVersionLT(3, 0))
95       TLI.setUnavailable(LibFunc::memset_pattern16);
96   } else if (!T.isWatchOS()) {
97     TLI.setUnavailable(LibFunc::memset_pattern16);
98   }
99 
100   if (!hasSinCosPiStret(T)) {
101     TLI.setUnavailable(LibFunc::sinpi);
102     TLI.setUnavailable(LibFunc::sinpif);
103     TLI.setUnavailable(LibFunc::cospi);
104     TLI.setUnavailable(LibFunc::cospif);
105     TLI.setUnavailable(LibFunc::sincospi_stret);
106     TLI.setUnavailable(LibFunc::sincospif_stret);
107   }
108 
109   if (T.isMacOSX() && T.getArch() == Triple::x86 &&
110       !T.isMacOSXVersionLT(10, 7)) {
111     // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
112     // we don't care about) have two versions; on recent OSX, the one we want
113     // has a $UNIX2003 suffix. The two implementations are identical except
114     // for the return value in some edge cases.  However, we don't want to
115     // generate code that depends on the old symbols.
116     TLI.setAvailableWithName(LibFunc::fwrite, "fwrite$UNIX2003");
117     TLI.setAvailableWithName(LibFunc::fputs, "fputs$UNIX2003");
118   }
119 
120   // iprintf and friends are only available on XCore and TCE.
121   if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) {
122     TLI.setUnavailable(LibFunc::iprintf);
123     TLI.setUnavailable(LibFunc::siprintf);
124     TLI.setUnavailable(LibFunc::fiprintf);
125   }
126 
127   if (T.isOSWindows() && !T.isOSCygMing()) {
128     // Win32 does not support long double
129     TLI.setUnavailable(LibFunc::acosl);
130     TLI.setUnavailable(LibFunc::asinl);
131     TLI.setUnavailable(LibFunc::atanl);
132     TLI.setUnavailable(LibFunc::atan2l);
133     TLI.setUnavailable(LibFunc::ceill);
134     TLI.setUnavailable(LibFunc::copysignl);
135     TLI.setUnavailable(LibFunc::cosl);
136     TLI.setUnavailable(LibFunc::coshl);
137     TLI.setUnavailable(LibFunc::expl);
138     TLI.setUnavailable(LibFunc::fabsf); // Win32 and Win64 both lack fabsf
139     TLI.setUnavailable(LibFunc::fabsl);
140     TLI.setUnavailable(LibFunc::floorl);
141     TLI.setUnavailable(LibFunc::fmaxl);
142     TLI.setUnavailable(LibFunc::fminl);
143     TLI.setUnavailable(LibFunc::fmodl);
144     TLI.setUnavailable(LibFunc::frexpl);
145     TLI.setUnavailable(LibFunc::ldexpf);
146     TLI.setUnavailable(LibFunc::ldexpl);
147     TLI.setUnavailable(LibFunc::logl);
148     TLI.setUnavailable(LibFunc::modfl);
149     TLI.setUnavailable(LibFunc::powl);
150     TLI.setUnavailable(LibFunc::sinl);
151     TLI.setUnavailable(LibFunc::sinhl);
152     TLI.setUnavailable(LibFunc::sqrtl);
153     TLI.setUnavailable(LibFunc::tanl);
154     TLI.setUnavailable(LibFunc::tanhl);
155 
156     // Win32 only has C89 math
157     TLI.setUnavailable(LibFunc::acosh);
158     TLI.setUnavailable(LibFunc::acoshf);
159     TLI.setUnavailable(LibFunc::acoshl);
160     TLI.setUnavailable(LibFunc::asinh);
161     TLI.setUnavailable(LibFunc::asinhf);
162     TLI.setUnavailable(LibFunc::asinhl);
163     TLI.setUnavailable(LibFunc::atanh);
164     TLI.setUnavailable(LibFunc::atanhf);
165     TLI.setUnavailable(LibFunc::atanhl);
166     TLI.setUnavailable(LibFunc::cbrt);
167     TLI.setUnavailable(LibFunc::cbrtf);
168     TLI.setUnavailable(LibFunc::cbrtl);
169     TLI.setUnavailable(LibFunc::exp2);
170     TLI.setUnavailable(LibFunc::exp2f);
171     TLI.setUnavailable(LibFunc::exp2l);
172     TLI.setUnavailable(LibFunc::expm1);
173     TLI.setUnavailable(LibFunc::expm1f);
174     TLI.setUnavailable(LibFunc::expm1l);
175     TLI.setUnavailable(LibFunc::log2);
176     TLI.setUnavailable(LibFunc::log2f);
177     TLI.setUnavailable(LibFunc::log2l);
178     TLI.setUnavailable(LibFunc::log1p);
179     TLI.setUnavailable(LibFunc::log1pf);
180     TLI.setUnavailable(LibFunc::log1pl);
181     TLI.setUnavailable(LibFunc::logb);
182     TLI.setUnavailable(LibFunc::logbf);
183     TLI.setUnavailable(LibFunc::logbl);
184     TLI.setUnavailable(LibFunc::nearbyint);
185     TLI.setUnavailable(LibFunc::nearbyintf);
186     TLI.setUnavailable(LibFunc::nearbyintl);
187     TLI.setUnavailable(LibFunc::rint);
188     TLI.setUnavailable(LibFunc::rintf);
189     TLI.setUnavailable(LibFunc::rintl);
190     TLI.setUnavailable(LibFunc::round);
191     TLI.setUnavailable(LibFunc::roundf);
192     TLI.setUnavailable(LibFunc::roundl);
193     TLI.setUnavailable(LibFunc::trunc);
194     TLI.setUnavailable(LibFunc::truncf);
195     TLI.setUnavailable(LibFunc::truncl);
196 
197     // Win32 provides some C99 math with mangled names
198     TLI.setAvailableWithName(LibFunc::copysign, "_copysign");
199 
200     if (T.getArch() == Triple::x86) {
201       // Win32 on x86 implements single-precision math functions as macros
202       TLI.setUnavailable(LibFunc::acosf);
203       TLI.setUnavailable(LibFunc::asinf);
204       TLI.setUnavailable(LibFunc::atanf);
205       TLI.setUnavailable(LibFunc::atan2f);
206       TLI.setUnavailable(LibFunc::ceilf);
207       TLI.setUnavailable(LibFunc::copysignf);
208       TLI.setUnavailable(LibFunc::cosf);
209       TLI.setUnavailable(LibFunc::coshf);
210       TLI.setUnavailable(LibFunc::expf);
211       TLI.setUnavailable(LibFunc::floorf);
212       TLI.setUnavailable(LibFunc::fminf);
213       TLI.setUnavailable(LibFunc::fmaxf);
214       TLI.setUnavailable(LibFunc::fmodf);
215       TLI.setUnavailable(LibFunc::logf);
216       TLI.setUnavailable(LibFunc::log10f);
217       TLI.setUnavailable(LibFunc::modff);
218       TLI.setUnavailable(LibFunc::powf);
219       TLI.setUnavailable(LibFunc::sinf);
220       TLI.setUnavailable(LibFunc::sinhf);
221       TLI.setUnavailable(LibFunc::sqrtf);
222       TLI.setUnavailable(LibFunc::tanf);
223       TLI.setUnavailable(LibFunc::tanhf);
224     }
225 
226     // Win32 does *not* provide provide these functions, but they are
227     // generally available on POSIX-compliant systems:
228     TLI.setUnavailable(LibFunc::access);
229     TLI.setUnavailable(LibFunc::bcmp);
230     TLI.setUnavailable(LibFunc::bcopy);
231     TLI.setUnavailable(LibFunc::bzero);
232     TLI.setUnavailable(LibFunc::chmod);
233     TLI.setUnavailable(LibFunc::chown);
234     TLI.setUnavailable(LibFunc::closedir);
235     TLI.setUnavailable(LibFunc::ctermid);
236     TLI.setUnavailable(LibFunc::fdopen);
237     TLI.setUnavailable(LibFunc::ffs);
238     TLI.setUnavailable(LibFunc::fileno);
239     TLI.setUnavailable(LibFunc::flockfile);
240     TLI.setUnavailable(LibFunc::fseeko);
241     TLI.setUnavailable(LibFunc::fstat);
242     TLI.setUnavailable(LibFunc::fstatvfs);
243     TLI.setUnavailable(LibFunc::ftello);
244     TLI.setUnavailable(LibFunc::ftrylockfile);
245     TLI.setUnavailable(LibFunc::funlockfile);
246     TLI.setUnavailable(LibFunc::getc_unlocked);
247     TLI.setUnavailable(LibFunc::getitimer);
248     TLI.setUnavailable(LibFunc::getlogin_r);
249     TLI.setUnavailable(LibFunc::getpwnam);
250     TLI.setUnavailable(LibFunc::gettimeofday);
251     TLI.setUnavailable(LibFunc::htonl);
252     TLI.setUnavailable(LibFunc::htons);
253     TLI.setUnavailable(LibFunc::lchown);
254     TLI.setUnavailable(LibFunc::lstat);
255     TLI.setUnavailable(LibFunc::memccpy);
256     TLI.setUnavailable(LibFunc::mkdir);
257     TLI.setUnavailable(LibFunc::ntohl);
258     TLI.setUnavailable(LibFunc::ntohs);
259     TLI.setUnavailable(LibFunc::open);
260     TLI.setUnavailable(LibFunc::opendir);
261     TLI.setUnavailable(LibFunc::pclose);
262     TLI.setUnavailable(LibFunc::popen);
263     TLI.setUnavailable(LibFunc::pread);
264     TLI.setUnavailable(LibFunc::pwrite);
265     TLI.setUnavailable(LibFunc::read);
266     TLI.setUnavailable(LibFunc::readlink);
267     TLI.setUnavailable(LibFunc::realpath);
268     TLI.setUnavailable(LibFunc::rmdir);
269     TLI.setUnavailable(LibFunc::setitimer);
270     TLI.setUnavailable(LibFunc::stat);
271     TLI.setUnavailable(LibFunc::statvfs);
272     TLI.setUnavailable(LibFunc::stpcpy);
273     TLI.setUnavailable(LibFunc::stpncpy);
274     TLI.setUnavailable(LibFunc::strcasecmp);
275     TLI.setUnavailable(LibFunc::strncasecmp);
276     TLI.setUnavailable(LibFunc::times);
277     TLI.setUnavailable(LibFunc::uname);
278     TLI.setUnavailable(LibFunc::unlink);
279     TLI.setUnavailable(LibFunc::unsetenv);
280     TLI.setUnavailable(LibFunc::utime);
281     TLI.setUnavailable(LibFunc::utimes);
282     TLI.setUnavailable(LibFunc::write);
283 
284     // Win32 does *not* provide provide these functions, but they are
285     // specified by C99:
286     TLI.setUnavailable(LibFunc::atoll);
287     TLI.setUnavailable(LibFunc::frexpf);
288     TLI.setUnavailable(LibFunc::llabs);
289   }
290 
291   switch (T.getOS()) {
292   case Triple::MacOSX:
293     // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
294     // and their names are __exp10 and __exp10f. exp10l is not available on
295     // OS X or iOS.
296     TLI.setUnavailable(LibFunc::exp10l);
297     if (T.isMacOSXVersionLT(10, 9)) {
298       TLI.setUnavailable(LibFunc::exp10);
299       TLI.setUnavailable(LibFunc::exp10f);
300     } else {
301       TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
302       TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
303     }
304     break;
305   case Triple::IOS:
306   case Triple::TvOS:
307   case Triple::WatchOS:
308     TLI.setUnavailable(LibFunc::exp10l);
309     if (!T.isWatchOS() && (T.isOSVersionLT(7, 0) ||
310                            (T.isOSVersionLT(9, 0) &&
311                             (T.getArch() == Triple::x86 ||
312                              T.getArch() == Triple::x86_64)))) {
313       TLI.setUnavailable(LibFunc::exp10);
314       TLI.setUnavailable(LibFunc::exp10f);
315     } else {
316       TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
317       TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
318     }
319     break;
320   case Triple::Linux:
321     // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
322     // buggy prior to glibc version 2.18. Until this version is widely deployed
323     // or we have a reasonable detection strategy, we cannot use exp10 reliably
324     // on Linux.
325     //
326     // Fall through to disable all of them.
327     LLVM_FALLTHROUGH;
328   default:
329     TLI.setUnavailable(LibFunc::exp10);
330     TLI.setUnavailable(LibFunc::exp10f);
331     TLI.setUnavailable(LibFunc::exp10l);
332   }
333 
334   // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
335   // Linux (GLIBC):
336   // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
337   // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
338   // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
339   switch (T.getOS()) {
340   case Triple::Darwin:
341   case Triple::MacOSX:
342   case Triple::IOS:
343   case Triple::TvOS:
344   case Triple::WatchOS:
345   case Triple::FreeBSD:
346   case Triple::Linux:
347     break;
348   default:
349     TLI.setUnavailable(LibFunc::ffsl);
350   }
351 
352   // ffsll is available on at least FreeBSD and Linux (GLIBC):
353   // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
354   // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
355   switch (T.getOS()) {
356   case Triple::Darwin:
357   case Triple::MacOSX:
358   case Triple::IOS:
359   case Triple::TvOS:
360   case Triple::WatchOS:
361   case Triple::FreeBSD:
362   case Triple::Linux:
363     break;
364   default:
365     TLI.setUnavailable(LibFunc::ffsll);
366   }
367 
368   // The following functions are available on at least FreeBSD:
369   // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
370   // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
371   // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
372   if (!T.isOSFreeBSD()) {
373     TLI.setUnavailable(LibFunc::fls);
374     TLI.setUnavailable(LibFunc::flsl);
375     TLI.setUnavailable(LibFunc::flsll);
376   }
377 
378   // The following functions are available on at least Linux:
379   if (!T.isOSLinux()) {
380     TLI.setUnavailable(LibFunc::dunder_strdup);
381     TLI.setUnavailable(LibFunc::dunder_strtok_r);
382     TLI.setUnavailable(LibFunc::dunder_isoc99_scanf);
383     TLI.setUnavailable(LibFunc::dunder_isoc99_sscanf);
384     TLI.setUnavailable(LibFunc::under_IO_getc);
385     TLI.setUnavailable(LibFunc::under_IO_putc);
386     TLI.setUnavailable(LibFunc::memalign);
387     TLI.setUnavailable(LibFunc::fopen64);
388     TLI.setUnavailable(LibFunc::fseeko64);
389     TLI.setUnavailable(LibFunc::fstat64);
390     TLI.setUnavailable(LibFunc::fstatvfs64);
391     TLI.setUnavailable(LibFunc::ftello64);
392     TLI.setUnavailable(LibFunc::lstat64);
393     TLI.setUnavailable(LibFunc::open64);
394     TLI.setUnavailable(LibFunc::stat64);
395     TLI.setUnavailable(LibFunc::statvfs64);
396     TLI.setUnavailable(LibFunc::tmpfile64);
397   }
398 
399   // As currently implemented in clang, NVPTX code has no standard library to
400   // speak of.  Headers provide a standard-ish library implementation, but many
401   // of the signatures are wrong -- for example, many libm functions are not
402   // extern "C".
403   //
404   // libdevice, an IR library provided by nvidia, is linked in by the front-end,
405   // but only used functions are provided to llvm.  Moreover, most of the
406   // functions in libdevice don't map precisely to standard library functions.
407   //
408   // FIXME: Having no standard library prevents e.g. many fastmath
409   // optimizations, so this situation should be fixed.
410   if (T.isNVPTX()) {
411     TLI.disableAllFunctions();
412     TLI.setAvailable(LibFunc::nvvm_reflect);
413   } else {
414     TLI.setUnavailable(LibFunc::nvvm_reflect);
415   }
416 
417   TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary);
418 }
419 
420 TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
421   // Default to everything being available.
422   memset(AvailableArray, -1, sizeof(AvailableArray));
423 
424   initialize(*this, Triple(), StandardNames);
425 }
426 
427 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
428   // Default to everything being available.
429   memset(AvailableArray, -1, sizeof(AvailableArray));
430 
431   initialize(*this, T, StandardNames);
432 }
433 
434 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
435     : CustomNames(TLI.CustomNames) {
436   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
437   VectorDescs = TLI.VectorDescs;
438   ScalarDescs = TLI.ScalarDescs;
439 }
440 
441 TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
442     : CustomNames(std::move(TLI.CustomNames)) {
443   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
444             AvailableArray);
445   VectorDescs = TLI.VectorDescs;
446   ScalarDescs = TLI.ScalarDescs;
447 }
448 
449 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
450   CustomNames = TLI.CustomNames;
451   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
452   return *this;
453 }
454 
455 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
456   CustomNames = std::move(TLI.CustomNames);
457   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
458             AvailableArray);
459   return *this;
460 }
461 
462 static StringRef sanitizeFunctionName(StringRef funcName) {
463   // Filter out empty names and names containing null bytes, those can't be in
464   // our table.
465   if (funcName.empty() || funcName.find('\0') != StringRef::npos)
466     return StringRef();
467 
468   // Check for \01 prefix that is used to mangle __asm declarations and
469   // strip it if present.
470   return GlobalValue::getRealLinkageName(funcName);
471 }
472 
473 bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName,
474                                        LibFunc::Func &F) const {
475   const char *const *Start = &StandardNames[0];
476   const char *const *End = &StandardNames[LibFunc::NumLibFuncs];
477 
478   funcName = sanitizeFunctionName(funcName);
479   if (funcName.empty())
480     return false;
481 
482   const char *const *I = std::lower_bound(
483       Start, End, funcName, [](const char *LHS, StringRef RHS) {
484         return std::strncmp(LHS, RHS.data(), RHS.size()) < 0;
485       });
486   if (I != End && *I == funcName) {
487     F = (LibFunc::Func)(I - Start);
488     return true;
489   }
490   return false;
491 }
492 
493 bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
494                                                    LibFunc::Func F,
495                                                    const DataLayout *DL) const {
496   LLVMContext &Ctx = FTy.getContext();
497   Type *PCharTy = Type::getInt8PtrTy(Ctx);
498   Type *SizeTTy = DL ? DL->getIntPtrType(Ctx, /*AS=*/0) : nullptr;
499   auto IsSizeTTy = [SizeTTy](Type *Ty) {
500     return SizeTTy ? Ty == SizeTTy : Ty->isIntegerTy();
501   };
502   unsigned NumParams = FTy.getNumParams();
503 
504   switch (F) {
505   case LibFunc::strlen:
506     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
507             FTy.getReturnType()->isIntegerTy());
508 
509   case LibFunc::strchr:
510   case LibFunc::strrchr:
511     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
512             FTy.getParamType(0) == FTy.getReturnType() &&
513             FTy.getParamType(1)->isIntegerTy());
514 
515   case LibFunc::strtol:
516   case LibFunc::strtod:
517   case LibFunc::strtof:
518   case LibFunc::strtoul:
519   case LibFunc::strtoll:
520   case LibFunc::strtold:
521   case LibFunc::strtoull:
522     return ((NumParams == 2 || NumParams == 3) &&
523             FTy.getParamType(0)->isPointerTy() &&
524             FTy.getParamType(1)->isPointerTy());
525   case LibFunc::strcat:
526     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
527             FTy.getParamType(0) == FTy.getReturnType() &&
528             FTy.getParamType(1) == FTy.getReturnType());
529 
530   case LibFunc::strncat:
531     return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
532             FTy.getParamType(0) == FTy.getReturnType() &&
533             FTy.getParamType(1) == FTy.getReturnType() &&
534             FTy.getParamType(2)->isIntegerTy());
535 
536   case LibFunc::strcpy_chk:
537   case LibFunc::stpcpy_chk:
538     --NumParams;
539     if (!IsSizeTTy(FTy.getParamType(NumParams)))
540       return false;
541     LLVM_FALLTHROUGH;
542   case LibFunc::strcpy:
543   case LibFunc::stpcpy:
544     return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(0) &&
545             FTy.getParamType(0) == FTy.getParamType(1) &&
546             FTy.getParamType(0) == PCharTy);
547 
548   case LibFunc::strncpy_chk:
549   case LibFunc::stpncpy_chk:
550     --NumParams;
551     if (!IsSizeTTy(FTy.getParamType(NumParams)))
552       return false;
553     LLVM_FALLTHROUGH;
554   case LibFunc::strncpy:
555   case LibFunc::stpncpy:
556     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
557             FTy.getParamType(0) == FTy.getParamType(1) &&
558             FTy.getParamType(0) == PCharTy &&
559             FTy.getParamType(2)->isIntegerTy());
560 
561   case LibFunc::strxfrm:
562     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
563             FTy.getParamType(1)->isPointerTy());
564 
565   case LibFunc::strcmp:
566     return (NumParams == 2 && FTy.getReturnType()->isIntegerTy(32) &&
567             FTy.getParamType(0)->isPointerTy() &&
568             FTy.getParamType(0) == FTy.getParamType(1));
569 
570   case LibFunc::strncmp:
571     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
572             FTy.getParamType(0)->isPointerTy() &&
573             FTy.getParamType(0) == FTy.getParamType(1) &&
574             FTy.getParamType(2)->isIntegerTy());
575 
576   case LibFunc::strspn:
577   case LibFunc::strcspn:
578     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
579             FTy.getParamType(0) == FTy.getParamType(1) &&
580             FTy.getReturnType()->isIntegerTy());
581 
582   case LibFunc::strcoll:
583   case LibFunc::strcasecmp:
584   case LibFunc::strncasecmp:
585     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
586             FTy.getParamType(1)->isPointerTy());
587 
588   case LibFunc::strstr:
589     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
590             FTy.getParamType(0)->isPointerTy() &&
591             FTy.getParamType(1)->isPointerTy());
592 
593   case LibFunc::strpbrk:
594     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
595             FTy.getReturnType() == FTy.getParamType(0) &&
596             FTy.getParamType(0) == FTy.getParamType(1));
597 
598   case LibFunc::strtok:
599   case LibFunc::strtok_r:
600     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
601   case LibFunc::scanf:
602   case LibFunc::setbuf:
603   case LibFunc::setvbuf:
604     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
605   case LibFunc::strdup:
606   case LibFunc::strndup:
607     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
608             FTy.getParamType(0)->isPointerTy());
609   case LibFunc::sscanf:
610   case LibFunc::stat:
611   case LibFunc::statvfs:
612   case LibFunc::sprintf:
613     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
614             FTy.getParamType(1)->isPointerTy());
615   case LibFunc::snprintf:
616     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
617             FTy.getParamType(2)->isPointerTy());
618   case LibFunc::setitimer:
619     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
620             FTy.getParamType(2)->isPointerTy());
621   case LibFunc::system:
622     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
623   case LibFunc::malloc:
624     return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
625   case LibFunc::memcmp:
626     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
627             FTy.getParamType(1)->isPointerTy() &&
628             FTy.getReturnType()->isIntegerTy(32));
629 
630   case LibFunc::memchr:
631   case LibFunc::memrchr:
632     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
633             FTy.getParamType(1)->isIntegerTy(32) &&
634             FTy.getParamType(2)->isIntegerTy() &&
635             FTy.getReturnType()->isPointerTy());
636   case LibFunc::modf:
637   case LibFunc::modff:
638   case LibFunc::modfl:
639     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
640 
641   case LibFunc::memcpy_chk:
642   case LibFunc::memmove_chk:
643     --NumParams;
644     if (!IsSizeTTy(FTy.getParamType(NumParams)))
645       return false;
646     LLVM_FALLTHROUGH;
647   case LibFunc::memcpy:
648   case LibFunc::mempcpy:
649   case LibFunc::memmove:
650     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
651             FTy.getParamType(0)->isPointerTy() &&
652             FTy.getParamType(1)->isPointerTy() &&
653             IsSizeTTy(FTy.getParamType(2)));
654 
655   case LibFunc::memset_chk:
656     --NumParams;
657     if (!IsSizeTTy(FTy.getParamType(NumParams)))
658       return false;
659     LLVM_FALLTHROUGH;
660   case LibFunc::memset:
661     return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
662             FTy.getParamType(0)->isPointerTy() &&
663             FTy.getParamType(1)->isIntegerTy() &&
664             IsSizeTTy(FTy.getParamType(2)));
665 
666   case LibFunc::memccpy:
667     return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
668   case LibFunc::memalign:
669     return (FTy.getReturnType()->isPointerTy());
670   case LibFunc::realloc:
671     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
672             FTy.getReturnType()->isPointerTy());
673   case LibFunc::read:
674     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
675   case LibFunc::rewind:
676   case LibFunc::rmdir:
677   case LibFunc::remove:
678   case LibFunc::realpath:
679     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
680   case LibFunc::rename:
681     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
682             FTy.getParamType(1)->isPointerTy());
683   case LibFunc::readlink:
684     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
685             FTy.getParamType(1)->isPointerTy());
686   case LibFunc::write:
687     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
688   case LibFunc::bcopy:
689   case LibFunc::bcmp:
690     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
691             FTy.getParamType(1)->isPointerTy());
692   case LibFunc::bzero:
693     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
694   case LibFunc::calloc:
695     return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
696 
697   case LibFunc::atof:
698   case LibFunc::atoi:
699   case LibFunc::atol:
700   case LibFunc::atoll:
701   case LibFunc::ferror:
702   case LibFunc::getenv:
703   case LibFunc::getpwnam:
704   case LibFunc::pclose:
705   case LibFunc::perror:
706   case LibFunc::printf:
707   case LibFunc::puts:
708   case LibFunc::uname:
709   case LibFunc::under_IO_getc:
710   case LibFunc::unlink:
711   case LibFunc::unsetenv:
712     return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
713 
714   case LibFunc::chmod:
715   case LibFunc::chown:
716   case LibFunc::clearerr:
717   case LibFunc::closedir:
718   case LibFunc::ctermid:
719   case LibFunc::fclose:
720   case LibFunc::feof:
721   case LibFunc::fflush:
722   case LibFunc::fgetc:
723   case LibFunc::fileno:
724   case LibFunc::flockfile:
725   case LibFunc::free:
726   case LibFunc::fseek:
727   case LibFunc::fseeko64:
728   case LibFunc::fseeko:
729   case LibFunc::fsetpos:
730   case LibFunc::ftell:
731   case LibFunc::ftello64:
732   case LibFunc::ftello:
733   case LibFunc::ftrylockfile:
734   case LibFunc::funlockfile:
735   case LibFunc::getc:
736   case LibFunc::getc_unlocked:
737   case LibFunc::getlogin_r:
738   case LibFunc::mkdir:
739   case LibFunc::mktime:
740   case LibFunc::times:
741     return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
742 
743   case LibFunc::access:
744     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
745   case LibFunc::fopen:
746     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
747             FTy.getParamType(0)->isPointerTy() &&
748             FTy.getParamType(1)->isPointerTy());
749   case LibFunc::fdopen:
750     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
751             FTy.getParamType(1)->isPointerTy());
752   case LibFunc::fputc:
753   case LibFunc::fstat:
754   case LibFunc::frexp:
755   case LibFunc::frexpf:
756   case LibFunc::frexpl:
757   case LibFunc::fstatvfs:
758     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
759   case LibFunc::fgets:
760     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
761             FTy.getParamType(2)->isPointerTy());
762   case LibFunc::fread:
763     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
764             FTy.getParamType(3)->isPointerTy());
765   case LibFunc::fwrite:
766     return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() &&
767             FTy.getParamType(0)->isPointerTy() &&
768             FTy.getParamType(1)->isIntegerTy() &&
769             FTy.getParamType(2)->isIntegerTy() &&
770             FTy.getParamType(3)->isPointerTy());
771   case LibFunc::fputs:
772     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
773             FTy.getParamType(1)->isPointerTy());
774   case LibFunc::fscanf:
775   case LibFunc::fprintf:
776     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
777             FTy.getParamType(1)->isPointerTy());
778   case LibFunc::fgetpos:
779     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
780             FTy.getParamType(1)->isPointerTy());
781   case LibFunc::gets:
782   case LibFunc::getchar:
783   case LibFunc::getitimer:
784     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
785   case LibFunc::ungetc:
786     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
787   case LibFunc::utime:
788   case LibFunc::utimes:
789     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
790             FTy.getParamType(1)->isPointerTy());
791   case LibFunc::putc:
792     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
793   case LibFunc::pread:
794   case LibFunc::pwrite:
795     return (NumParams == 4 && FTy.getParamType(1)->isPointerTy());
796   case LibFunc::popen:
797     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
798             FTy.getParamType(0)->isPointerTy() &&
799             FTy.getParamType(1)->isPointerTy());
800   case LibFunc::vscanf:
801     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
802   case LibFunc::vsscanf:
803     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
804             FTy.getParamType(2)->isPointerTy());
805   case LibFunc::vfscanf:
806     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
807             FTy.getParamType(2)->isPointerTy());
808   case LibFunc::valloc:
809     return (FTy.getReturnType()->isPointerTy());
810   case LibFunc::vprintf:
811     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
812   case LibFunc::vfprintf:
813   case LibFunc::vsprintf:
814     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
815             FTy.getParamType(1)->isPointerTy());
816   case LibFunc::vsnprintf:
817     return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
818             FTy.getParamType(2)->isPointerTy());
819   case LibFunc::open:
820     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
821   case LibFunc::opendir:
822     return (NumParams == 1 && FTy.getReturnType()->isPointerTy() &&
823             FTy.getParamType(0)->isPointerTy());
824   case LibFunc::tmpfile:
825     return (FTy.getReturnType()->isPointerTy());
826   case LibFunc::htonl:
827   case LibFunc::htons:
828   case LibFunc::ntohl:
829   case LibFunc::ntohs:
830   case LibFunc::lstat:
831     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
832             FTy.getParamType(1)->isPointerTy());
833   case LibFunc::lchown:
834     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
835   case LibFunc::qsort:
836     return (NumParams == 4 && FTy.getParamType(3)->isPointerTy());
837   case LibFunc::dunder_strdup:
838   case LibFunc::dunder_strndup:
839     return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
840             FTy.getParamType(0)->isPointerTy());
841   case LibFunc::dunder_strtok_r:
842     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
843   case LibFunc::under_IO_putc:
844     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
845   case LibFunc::dunder_isoc99_scanf:
846     return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
847   case LibFunc::stat64:
848   case LibFunc::lstat64:
849   case LibFunc::statvfs64:
850     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
851             FTy.getParamType(1)->isPointerTy());
852   case LibFunc::dunder_isoc99_sscanf:
853     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
854             FTy.getParamType(1)->isPointerTy());
855   case LibFunc::fopen64:
856     return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
857             FTy.getParamType(0)->isPointerTy() &&
858             FTy.getParamType(1)->isPointerTy());
859   case LibFunc::tmpfile64:
860     return (FTy.getReturnType()->isPointerTy());
861   case LibFunc::fstat64:
862   case LibFunc::fstatvfs64:
863     return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
864   case LibFunc::open64:
865     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
866   case LibFunc::gettimeofday:
867     return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
868             FTy.getParamType(1)->isPointerTy());
869 
870   case LibFunc::Znwj:                    // new(unsigned int);
871   case LibFunc::Znwm:                    // new(unsigned long);
872   case LibFunc::Znaj:                    // new[](unsigned int);
873   case LibFunc::Znam:                    // new[](unsigned long);
874   case LibFunc::msvc_new_int:            // new(unsigned int);
875   case LibFunc::msvc_new_longlong:       // new(unsigned long long);
876   case LibFunc::msvc_new_array_int:      // new[](unsigned int);
877   case LibFunc::msvc_new_array_longlong: // new[](unsigned long long);
878     return (NumParams == 1);
879 
880   case LibFunc::memset_pattern16:
881     return (!FTy.isVarArg() && NumParams == 3 &&
882             isa<PointerType>(FTy.getParamType(0)) &&
883             isa<PointerType>(FTy.getParamType(1)) &&
884             isa<IntegerType>(FTy.getParamType(2)));
885 
886   // int __nvvm_reflect(const char *);
887   case LibFunc::nvvm_reflect:
888     return (NumParams == 1 && isa<PointerType>(FTy.getParamType(0)));
889 
890   case LibFunc::sin:
891   case LibFunc::sinf:
892   case LibFunc::sinl:
893   case LibFunc::cos:
894   case LibFunc::cosf:
895   case LibFunc::cosl:
896   case LibFunc::tan:
897   case LibFunc::tanf:
898   case LibFunc::tanl:
899   case LibFunc::exp:
900   case LibFunc::expf:
901   case LibFunc::expl:
902   case LibFunc::exp2:
903   case LibFunc::exp2f:
904   case LibFunc::exp2l:
905   case LibFunc::log:
906   case LibFunc::logf:
907   case LibFunc::logl:
908   case LibFunc::log10:
909   case LibFunc::log10f:
910   case LibFunc::log10l:
911   case LibFunc::log2:
912   case LibFunc::log2f:
913   case LibFunc::log2l:
914   case LibFunc::fabs:
915   case LibFunc::fabsf:
916   case LibFunc::fabsl:
917   case LibFunc::floor:
918   case LibFunc::floorf:
919   case LibFunc::floorl:
920   case LibFunc::ceil:
921   case LibFunc::ceilf:
922   case LibFunc::ceill:
923   case LibFunc::trunc:
924   case LibFunc::truncf:
925   case LibFunc::truncl:
926   case LibFunc::rint:
927   case LibFunc::rintf:
928   case LibFunc::rintl:
929   case LibFunc::nearbyint:
930   case LibFunc::nearbyintf:
931   case LibFunc::nearbyintl:
932   case LibFunc::round:
933   case LibFunc::roundf:
934   case LibFunc::roundl:
935   case LibFunc::sqrt:
936   case LibFunc::sqrtf:
937   case LibFunc::sqrtl:
938     return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() &&
939             FTy.getReturnType() == FTy.getParamType(0));
940 
941   case LibFunc::fmin:
942   case LibFunc::fminf:
943   case LibFunc::fminl:
944   case LibFunc::fmax:
945   case LibFunc::fmaxf:
946   case LibFunc::fmaxl:
947   case LibFunc::copysign:
948   case LibFunc::copysignf:
949   case LibFunc::copysignl:
950   case LibFunc::pow:
951   case LibFunc::powf:
952   case LibFunc::powl:
953     return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
954             FTy.getReturnType() == FTy.getParamType(0) &&
955             FTy.getReturnType() == FTy.getParamType(1));
956 
957   case LibFunc::ffs:
958   case LibFunc::ffsl:
959   case LibFunc::ffsll:
960   case LibFunc::isdigit:
961   case LibFunc::isascii:
962   case LibFunc::toascii:
963     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
964             FTy.getParamType(0)->isIntegerTy());
965 
966   case LibFunc::fls:
967   case LibFunc::flsl:
968   case LibFunc::flsll:
969   case LibFunc::abs:
970   case LibFunc::labs:
971   case LibFunc::llabs:
972     return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() &&
973             FTy.getReturnType() == FTy.getParamType(0));
974 
975   case LibFunc::cxa_atexit:
976     return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() &&
977             FTy.getParamType(0)->isPointerTy() &&
978             FTy.getParamType(1)->isPointerTy() &&
979             FTy.getParamType(2)->isPointerTy());
980 
981   case LibFunc::sinpi:
982   case LibFunc::cospi:
983     return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() &&
984             FTy.getReturnType() == FTy.getParamType(0));
985 
986   case LibFunc::sinpif:
987   case LibFunc::cospif:
988     return (NumParams == 1 && FTy.getReturnType()->isFloatTy() &&
989             FTy.getReturnType() == FTy.getParamType(0));
990 
991   default:
992     // Assume the other functions are correct.
993     // FIXME: It'd be really nice to cover them all.
994     return true;
995   }
996 }
997 
998 bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
999                                        LibFunc::Func &F) const {
1000   const DataLayout *DL =
1001       FDecl.getParent() ? &FDecl.getParent()->getDataLayout() : nullptr;
1002   return getLibFunc(FDecl.getName(), F) &&
1003          isValidProtoForLibFunc(*FDecl.getFunctionType(), F, DL);
1004 }
1005 
1006 void TargetLibraryInfoImpl::disableAllFunctions() {
1007   memset(AvailableArray, 0, sizeof(AvailableArray));
1008 }
1009 
1010 static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
1011   return std::strncmp(LHS.ScalarFnName, RHS.ScalarFnName,
1012                       std::strlen(RHS.ScalarFnName)) < 0;
1013 }
1014 
1015 static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
1016   return std::strncmp(LHS.VectorFnName, RHS.VectorFnName,
1017                       std::strlen(RHS.VectorFnName)) < 0;
1018 }
1019 
1020 static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
1021   return std::strncmp(LHS.ScalarFnName, S.data(), S.size()) < 0;
1022 }
1023 
1024 static bool compareWithVectorFnName(const VecDesc &LHS, StringRef S) {
1025   return std::strncmp(LHS.VectorFnName, S.data(), S.size()) < 0;
1026 }
1027 
1028 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
1029   VectorDescs.insert(VectorDescs.end(), Fns.begin(), Fns.end());
1030   std::sort(VectorDescs.begin(), VectorDescs.end(), compareByScalarFnName);
1031 
1032   ScalarDescs.insert(ScalarDescs.end(), Fns.begin(), Fns.end());
1033   std::sort(ScalarDescs.begin(), ScalarDescs.end(), compareByVectorFnName);
1034 }
1035 
1036 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
1037     enum VectorLibrary VecLib) {
1038   switch (VecLib) {
1039   case Accelerate: {
1040     const VecDesc VecFuncs[] = {
1041         // Floating-Point Arithmetic and Auxiliary Functions
1042         {"ceilf", "vceilf", 4},
1043         {"fabsf", "vfabsf", 4},
1044         {"llvm.fabs.f32", "vfabsf", 4},
1045         {"floorf", "vfloorf", 4},
1046         {"sqrtf", "vsqrtf", 4},
1047         {"llvm.sqrt.f32", "vsqrtf", 4},
1048 
1049         // Exponential and Logarithmic Functions
1050         {"expf", "vexpf", 4},
1051         {"llvm.exp.f32", "vexpf", 4},
1052         {"expm1f", "vexpm1f", 4},
1053         {"logf", "vlogf", 4},
1054         {"llvm.log.f32", "vlogf", 4},
1055         {"log1pf", "vlog1pf", 4},
1056         {"log10f", "vlog10f", 4},
1057         {"llvm.log10.f32", "vlog10f", 4},
1058         {"logbf", "vlogbf", 4},
1059 
1060         // Trigonometric Functions
1061         {"sinf", "vsinf", 4},
1062         {"llvm.sin.f32", "vsinf", 4},
1063         {"cosf", "vcosf", 4},
1064         {"llvm.cos.f32", "vcosf", 4},
1065         {"tanf", "vtanf", 4},
1066         {"asinf", "vasinf", 4},
1067         {"acosf", "vacosf", 4},
1068         {"atanf", "vatanf", 4},
1069 
1070         // Hyperbolic Functions
1071         {"sinhf", "vsinhf", 4},
1072         {"coshf", "vcoshf", 4},
1073         {"tanhf", "vtanhf", 4},
1074         {"asinhf", "vasinhf", 4},
1075         {"acoshf", "vacoshf", 4},
1076         {"atanhf", "vatanhf", 4},
1077     };
1078     addVectorizableFunctions(VecFuncs);
1079     break;
1080   }
1081   case SVML: {
1082     const VecDesc VecFuncs[] = {
1083         {"sin", "__svml_sin2", 2},
1084         {"sin", "__svml_sin4", 4},
1085         {"sin", "__svml_sin8", 8},
1086 
1087         {"sinf", "__svml_sinf4", 4},
1088         {"sinf", "__svml_sinf8", 8},
1089         {"sinf", "__svml_sinf16", 16},
1090 
1091         {"cos", "__svml_cos2", 2},
1092         {"cos", "__svml_cos4", 4},
1093         {"cos", "__svml_cos8", 8},
1094 
1095         {"cosf", "__svml_cosf4", 4},
1096         {"cosf", "__svml_cosf8", 8},
1097         {"cosf", "__svml_cosf16", 16},
1098 
1099         {"pow", "__svml_pow2", 2},
1100         {"pow", "__svml_pow4", 4},
1101         {"pow", "__svml_pow8", 8},
1102 
1103         {"powf", "__svml_powf4", 4},
1104         {"powf", "__svml_powf8", 8},
1105         {"powf", "__svml_powf16", 16},
1106 
1107         {"llvm.pow.f64", "__svml_pow2", 2},
1108         {"llvm.pow.f64", "__svml_pow4", 4},
1109         {"llvm.pow.f64", "__svml_pow8", 8},
1110 
1111         {"llvm.pow.f32", "__svml_powf4", 4},
1112         {"llvm.pow.f32", "__svml_powf8", 8},
1113         {"llvm.pow.f32", "__svml_powf16", 16},
1114 
1115         {"exp", "__svml_exp2", 2},
1116         {"exp", "__svml_exp4", 4},
1117         {"exp", "__svml_exp8", 8},
1118 
1119         {"expf", "__svml_expf4", 4},
1120         {"expf", "__svml_expf8", 8},
1121         {"expf", "__svml_expf16", 16},
1122 
1123         {"llvm.exp.f64", "__svml_exp2", 2},
1124         {"llvm.exp.f64", "__svml_exp4", 4},
1125         {"llvm.exp.f64", "__svml_exp8", 8},
1126 
1127         {"llvm.exp.f32", "__svml_expf4", 4},
1128         {"llvm.exp.f32", "__svml_expf8", 8},
1129         {"llvm.exp.f32", "__svml_expf16", 16},
1130 
1131         {"log", "__svml_log2", 2},
1132         {"log", "__svml_log4", 4},
1133         {"log", "__svml_log8", 8},
1134 
1135         {"logf", "__svml_logf4", 4},
1136         {"logf", "__svml_logf8", 8},
1137         {"logf", "__svml_logf16", 16},
1138 
1139         {"llvm.log.f64", "__svml_log2", 2},
1140         {"llvm.log.f64", "__svml_log4", 4},
1141         {"llvm.log.f64", "__svml_log8", 8},
1142 
1143         {"llvm.log.f32", "__svml_logf4", 4},
1144         {"llvm.log.f32", "__svml_logf8", 8},
1145         {"llvm.log.f32", "__svml_logf16", 16},
1146     };
1147     addVectorizableFunctions(VecFuncs);
1148     break;
1149   }
1150   case NoLibrary:
1151     break;
1152   }
1153 }
1154 
1155 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
1156   funcName = sanitizeFunctionName(funcName);
1157   if (funcName.empty())
1158     return false;
1159 
1160   std::vector<VecDesc>::const_iterator I = std::lower_bound(
1161       VectorDescs.begin(), VectorDescs.end(), funcName,
1162       compareWithScalarFnName);
1163   return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
1164 }
1165 
1166 StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
1167                                                        unsigned VF) const {
1168   F = sanitizeFunctionName(F);
1169   if (F.empty())
1170     return F;
1171   std::vector<VecDesc>::const_iterator I = std::lower_bound(
1172       VectorDescs.begin(), VectorDescs.end(), F, compareWithScalarFnName);
1173   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
1174     if (I->VectorizationFactor == VF)
1175       return I->VectorFnName;
1176     ++I;
1177   }
1178   return StringRef();
1179 }
1180 
1181 StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F,
1182                                                        unsigned &VF) const {
1183   F = sanitizeFunctionName(F);
1184   if (F.empty())
1185     return F;
1186 
1187   std::vector<VecDesc>::const_iterator I = std::lower_bound(
1188       ScalarDescs.begin(), ScalarDescs.end(), F, compareWithVectorFnName);
1189   if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F)
1190     return StringRef();
1191   VF = I->VectorizationFactor;
1192   return I->ScalarFnName;
1193 }
1194 
1195 TargetLibraryInfo TargetLibraryAnalysis::run(Module &M,
1196                                              ModuleAnalysisManager &) {
1197   if (PresetInfoImpl)
1198     return TargetLibraryInfo(*PresetInfoImpl);
1199 
1200   return TargetLibraryInfo(lookupInfoImpl(Triple(M.getTargetTriple())));
1201 }
1202 
1203 TargetLibraryInfo TargetLibraryAnalysis::run(Function &F,
1204                                              FunctionAnalysisManager &) {
1205   if (PresetInfoImpl)
1206     return TargetLibraryInfo(*PresetInfoImpl);
1207 
1208   return TargetLibraryInfo(
1209       lookupInfoImpl(Triple(F.getParent()->getTargetTriple())));
1210 }
1211 
1212 TargetLibraryInfoImpl &TargetLibraryAnalysis::lookupInfoImpl(const Triple &T) {
1213   std::unique_ptr<TargetLibraryInfoImpl> &Impl =
1214       Impls[T.normalize()];
1215   if (!Impl)
1216     Impl.reset(new TargetLibraryInfoImpl(T));
1217 
1218   return *Impl;
1219 }
1220 
1221 
1222 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
1223     : ImmutablePass(ID), TLIImpl(), TLI(TLIImpl) {
1224   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1225 }
1226 
1227 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
1228     : ImmutablePass(ID), TLIImpl(T), TLI(TLIImpl) {
1229   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1230 }
1231 
1232 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
1233     const TargetLibraryInfoImpl &TLIImpl)
1234     : ImmutablePass(ID), TLIImpl(TLIImpl), TLI(this->TLIImpl) {
1235   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1236 }
1237 
1238 char TargetLibraryAnalysis::PassID;
1239 
1240 // Register the basic pass.
1241 INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
1242                 "Target Library Information", false, true)
1243 char TargetLibraryInfoWrapperPass::ID = 0;
1244 
1245 void TargetLibraryInfoWrapperPass::anchor() {}
1246