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                clEnumValEnd));
27 
28 const char *const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] = {
29 #define TLI_DEFINE_STRING
30 #include "llvm/Analysis/TargetLibraryInfo.def"
31 };
32 
hasSinCosPiStret(const Triple & T)33 static bool hasSinCosPiStret(const Triple &T) {
34   // Only Darwin variants have _stret versions of combined trig functions.
35   if (!T.isOSDarwin())
36     return false;
37 
38   // The ABI is rather complicated on x86, so don't do anything special there.
39   if (T.getArch() == Triple::x86)
40     return false;
41 
42   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
43     return false;
44 
45   if (T.isiOS() && T.isOSVersionLT(7, 0))
46     return false;
47 
48   return true;
49 }
50 
51 /// initialize - Initialize the set of available library functions based on the
52 /// specified target triple.  This should be carefully written so that a missing
53 /// target triple gets a sane set of defaults.
initialize(TargetLibraryInfoImpl & TLI,const Triple & T,const char * const * StandardNames)54 static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
55                        const char *const *StandardNames) {
56 #ifndef NDEBUG
57   // Verify that the StandardNames array is in alphabetical order.
58   for (unsigned F = 1; F < LibFunc::NumLibFuncs; ++F) {
59     if (strcmp(StandardNames[F-1], StandardNames[F]) >= 0)
60       llvm_unreachable("TargetLibraryInfoImpl function names must be sorted");
61   }
62 #endif // !NDEBUG
63 
64   if (T.getArch() == Triple::r600 ||
65       T.getArch() == Triple::amdgcn) {
66     TLI.setUnavailable(LibFunc::ldexp);
67     TLI.setUnavailable(LibFunc::ldexpf);
68     TLI.setUnavailable(LibFunc::ldexpl);
69   }
70 
71   // There are no library implementations of mempcy and memset for AMD gpus and
72   // these can be difficult to lower in the backend.
73   if (T.getArch() == Triple::r600 ||
74       T.getArch() == Triple::amdgcn ||
75       T.getArch() == Triple::wasm32 ||
76       T.getArch() == Triple::wasm64) {
77     TLI.setUnavailable(LibFunc::memcpy);
78     TLI.setUnavailable(LibFunc::memset);
79     TLI.setUnavailable(LibFunc::memset_pattern16);
80     return;
81   }
82 
83   // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
84   // All versions of watchOS support it.
85   if (T.isMacOSX()) {
86     if (T.isMacOSXVersionLT(10, 5))
87       TLI.setUnavailable(LibFunc::memset_pattern16);
88   } else if (T.isiOS()) {
89     if (T.isOSVersionLT(3, 0))
90       TLI.setUnavailable(LibFunc::memset_pattern16);
91   } else if (!T.isWatchOS()) {
92     TLI.setUnavailable(LibFunc::memset_pattern16);
93   }
94 
95   if (!hasSinCosPiStret(T)) {
96     TLI.setUnavailable(LibFunc::sinpi);
97     TLI.setUnavailable(LibFunc::sinpif);
98     TLI.setUnavailable(LibFunc::cospi);
99     TLI.setUnavailable(LibFunc::cospif);
100     TLI.setUnavailable(LibFunc::sincospi_stret);
101     TLI.setUnavailable(LibFunc::sincospif_stret);
102   }
103 
104   if (T.isMacOSX() && T.getArch() == Triple::x86 &&
105       !T.isMacOSXVersionLT(10, 7)) {
106     // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
107     // we don't care about) have two versions; on recent OSX, the one we want
108     // has a $UNIX2003 suffix. The two implementations are identical except
109     // for the return value in some edge cases.  However, we don't want to
110     // generate code that depends on the old symbols.
111     TLI.setAvailableWithName(LibFunc::fwrite, "fwrite$UNIX2003");
112     TLI.setAvailableWithName(LibFunc::fputs, "fputs$UNIX2003");
113   }
114 
115   // iprintf and friends are only available on XCore and TCE.
116   if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) {
117     TLI.setUnavailable(LibFunc::iprintf);
118     TLI.setUnavailable(LibFunc::siprintf);
119     TLI.setUnavailable(LibFunc::fiprintf);
120   }
121 
122   if (T.isOSWindows() && !T.isOSCygMing()) {
123     // Win32 does not support long double
124     TLI.setUnavailable(LibFunc::acosl);
125     TLI.setUnavailable(LibFunc::asinl);
126     TLI.setUnavailable(LibFunc::atanl);
127     TLI.setUnavailable(LibFunc::atan2l);
128     TLI.setUnavailable(LibFunc::ceill);
129     TLI.setUnavailable(LibFunc::copysignl);
130     TLI.setUnavailable(LibFunc::cosl);
131     TLI.setUnavailable(LibFunc::coshl);
132     TLI.setUnavailable(LibFunc::expl);
133     TLI.setUnavailable(LibFunc::fabsf); // Win32 and Win64 both lack fabsf
134     TLI.setUnavailable(LibFunc::fabsl);
135     TLI.setUnavailable(LibFunc::floorl);
136     TLI.setUnavailable(LibFunc::fmaxl);
137     TLI.setUnavailable(LibFunc::fminl);
138     TLI.setUnavailable(LibFunc::fmodl);
139     TLI.setUnavailable(LibFunc::frexpl);
140     TLI.setUnavailable(LibFunc::ldexpf);
141     TLI.setUnavailable(LibFunc::ldexpl);
142     TLI.setUnavailable(LibFunc::logl);
143     TLI.setUnavailable(LibFunc::modfl);
144     TLI.setUnavailable(LibFunc::powl);
145     TLI.setUnavailable(LibFunc::sinl);
146     TLI.setUnavailable(LibFunc::sinhl);
147     TLI.setUnavailable(LibFunc::sqrtl);
148     TLI.setUnavailable(LibFunc::tanl);
149     TLI.setUnavailable(LibFunc::tanhl);
150 
151     // Win32 only has C89 math
152     TLI.setUnavailable(LibFunc::acosh);
153     TLI.setUnavailable(LibFunc::acoshf);
154     TLI.setUnavailable(LibFunc::acoshl);
155     TLI.setUnavailable(LibFunc::asinh);
156     TLI.setUnavailable(LibFunc::asinhf);
157     TLI.setUnavailable(LibFunc::asinhl);
158     TLI.setUnavailable(LibFunc::atanh);
159     TLI.setUnavailable(LibFunc::atanhf);
160     TLI.setUnavailable(LibFunc::atanhl);
161     TLI.setUnavailable(LibFunc::cbrt);
162     TLI.setUnavailable(LibFunc::cbrtf);
163     TLI.setUnavailable(LibFunc::cbrtl);
164     TLI.setUnavailable(LibFunc::exp2);
165     TLI.setUnavailable(LibFunc::exp2f);
166     TLI.setUnavailable(LibFunc::exp2l);
167     TLI.setUnavailable(LibFunc::expm1);
168     TLI.setUnavailable(LibFunc::expm1f);
169     TLI.setUnavailable(LibFunc::expm1l);
170     TLI.setUnavailable(LibFunc::log2);
171     TLI.setUnavailable(LibFunc::log2f);
172     TLI.setUnavailable(LibFunc::log2l);
173     TLI.setUnavailable(LibFunc::log1p);
174     TLI.setUnavailable(LibFunc::log1pf);
175     TLI.setUnavailable(LibFunc::log1pl);
176     TLI.setUnavailable(LibFunc::logb);
177     TLI.setUnavailable(LibFunc::logbf);
178     TLI.setUnavailable(LibFunc::logbl);
179     TLI.setUnavailable(LibFunc::nearbyint);
180     TLI.setUnavailable(LibFunc::nearbyintf);
181     TLI.setUnavailable(LibFunc::nearbyintl);
182     TLI.setUnavailable(LibFunc::rint);
183     TLI.setUnavailable(LibFunc::rintf);
184     TLI.setUnavailable(LibFunc::rintl);
185     TLI.setUnavailable(LibFunc::round);
186     TLI.setUnavailable(LibFunc::roundf);
187     TLI.setUnavailable(LibFunc::roundl);
188     TLI.setUnavailable(LibFunc::trunc);
189     TLI.setUnavailable(LibFunc::truncf);
190     TLI.setUnavailable(LibFunc::truncl);
191 
192     // Win32 provides some C99 math with mangled names
193     TLI.setAvailableWithName(LibFunc::copysign, "_copysign");
194 
195     if (T.getArch() == Triple::x86) {
196       // Win32 on x86 implements single-precision math functions as macros
197       TLI.setUnavailable(LibFunc::acosf);
198       TLI.setUnavailable(LibFunc::asinf);
199       TLI.setUnavailable(LibFunc::atanf);
200       TLI.setUnavailable(LibFunc::atan2f);
201       TLI.setUnavailable(LibFunc::ceilf);
202       TLI.setUnavailable(LibFunc::copysignf);
203       TLI.setUnavailable(LibFunc::cosf);
204       TLI.setUnavailable(LibFunc::coshf);
205       TLI.setUnavailable(LibFunc::expf);
206       TLI.setUnavailable(LibFunc::floorf);
207       TLI.setUnavailable(LibFunc::fminf);
208       TLI.setUnavailable(LibFunc::fmaxf);
209       TLI.setUnavailable(LibFunc::fmodf);
210       TLI.setUnavailable(LibFunc::logf);
211       TLI.setUnavailable(LibFunc::powf);
212       TLI.setUnavailable(LibFunc::sinf);
213       TLI.setUnavailable(LibFunc::sinhf);
214       TLI.setUnavailable(LibFunc::sqrtf);
215       TLI.setUnavailable(LibFunc::tanf);
216       TLI.setUnavailable(LibFunc::tanhf);
217     }
218 
219     // Win32 does *not* provide provide these functions, but they are
220     // generally available on POSIX-compliant systems:
221     TLI.setUnavailable(LibFunc::access);
222     TLI.setUnavailable(LibFunc::bcmp);
223     TLI.setUnavailable(LibFunc::bcopy);
224     TLI.setUnavailable(LibFunc::bzero);
225     TLI.setUnavailable(LibFunc::chmod);
226     TLI.setUnavailable(LibFunc::chown);
227     TLI.setUnavailable(LibFunc::closedir);
228     TLI.setUnavailable(LibFunc::ctermid);
229     TLI.setUnavailable(LibFunc::fdopen);
230     TLI.setUnavailable(LibFunc::ffs);
231     TLI.setUnavailable(LibFunc::fileno);
232     TLI.setUnavailable(LibFunc::flockfile);
233     TLI.setUnavailable(LibFunc::fseeko);
234     TLI.setUnavailable(LibFunc::fstat);
235     TLI.setUnavailable(LibFunc::fstatvfs);
236     TLI.setUnavailable(LibFunc::ftello);
237     TLI.setUnavailable(LibFunc::ftrylockfile);
238     TLI.setUnavailable(LibFunc::funlockfile);
239     TLI.setUnavailable(LibFunc::getc_unlocked);
240     TLI.setUnavailable(LibFunc::getitimer);
241     TLI.setUnavailable(LibFunc::getlogin_r);
242     TLI.setUnavailable(LibFunc::getpwnam);
243     TLI.setUnavailable(LibFunc::gettimeofday);
244     TLI.setUnavailable(LibFunc::htonl);
245     TLI.setUnavailable(LibFunc::htons);
246     TLI.setUnavailable(LibFunc::lchown);
247     TLI.setUnavailable(LibFunc::lstat);
248     TLI.setUnavailable(LibFunc::memccpy);
249     TLI.setUnavailable(LibFunc::mkdir);
250     TLI.setUnavailable(LibFunc::ntohl);
251     TLI.setUnavailable(LibFunc::ntohs);
252     TLI.setUnavailable(LibFunc::open);
253     TLI.setUnavailable(LibFunc::opendir);
254     TLI.setUnavailable(LibFunc::pclose);
255     TLI.setUnavailable(LibFunc::popen);
256     TLI.setUnavailable(LibFunc::pread);
257     TLI.setUnavailable(LibFunc::pwrite);
258     TLI.setUnavailable(LibFunc::read);
259     TLI.setUnavailable(LibFunc::readlink);
260     TLI.setUnavailable(LibFunc::realpath);
261     TLI.setUnavailable(LibFunc::rmdir);
262     TLI.setUnavailable(LibFunc::setitimer);
263     TLI.setUnavailable(LibFunc::stat);
264     TLI.setUnavailable(LibFunc::statvfs);
265     TLI.setUnavailable(LibFunc::stpcpy);
266     TLI.setUnavailable(LibFunc::stpncpy);
267     TLI.setUnavailable(LibFunc::strcasecmp);
268     TLI.setUnavailable(LibFunc::strncasecmp);
269     TLI.setUnavailable(LibFunc::times);
270     TLI.setUnavailable(LibFunc::uname);
271     TLI.setUnavailable(LibFunc::unlink);
272     TLI.setUnavailable(LibFunc::unsetenv);
273     TLI.setUnavailable(LibFunc::utime);
274     TLI.setUnavailable(LibFunc::utimes);
275     TLI.setUnavailable(LibFunc::write);
276 
277     // Win32 does *not* provide provide these functions, but they are
278     // specified by C99:
279     TLI.setUnavailable(LibFunc::atoll);
280     TLI.setUnavailable(LibFunc::frexpf);
281     TLI.setUnavailable(LibFunc::llabs);
282   }
283 
284   switch (T.getOS()) {
285   case Triple::MacOSX:
286     // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
287     // and their names are __exp10 and __exp10f. exp10l is not available on
288     // OS X or iOS.
289     TLI.setUnavailable(LibFunc::exp10l);
290     if (T.isMacOSXVersionLT(10, 9)) {
291       TLI.setUnavailable(LibFunc::exp10);
292       TLI.setUnavailable(LibFunc::exp10f);
293     } else {
294       TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
295       TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
296     }
297     break;
298   case Triple::IOS:
299   case Triple::TvOS:
300   case Triple::WatchOS:
301     TLI.setUnavailable(LibFunc::exp10l);
302     if (!T.isWatchOS() && (T.isOSVersionLT(7, 0) ||
303                            (T.isOSVersionLT(9, 0) &&
304                             (T.getArch() == Triple::x86 ||
305                              T.getArch() == Triple::x86_64)))) {
306       TLI.setUnavailable(LibFunc::exp10);
307       TLI.setUnavailable(LibFunc::exp10f);
308     } else {
309       TLI.setAvailableWithName(LibFunc::exp10, "__exp10");
310       TLI.setAvailableWithName(LibFunc::exp10f, "__exp10f");
311     }
312     break;
313   case Triple::Linux:
314     // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
315     // buggy prior to glibc version 2.18. Until this version is widely deployed
316     // or we have a reasonable detection strategy, we cannot use exp10 reliably
317     // on Linux.
318     //
319     // Fall through to disable all of them.
320   default:
321     TLI.setUnavailable(LibFunc::exp10);
322     TLI.setUnavailable(LibFunc::exp10f);
323     TLI.setUnavailable(LibFunc::exp10l);
324   }
325 
326   // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
327   // Linux (GLIBC):
328   // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
329   // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
330   // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
331   switch (T.getOS()) {
332   case Triple::Darwin:
333   case Triple::MacOSX:
334   case Triple::IOS:
335   case Triple::TvOS:
336   case Triple::WatchOS:
337   case Triple::FreeBSD:
338   case Triple::Linux:
339     break;
340   default:
341     TLI.setUnavailable(LibFunc::ffsl);
342   }
343 
344   // ffsll is available on at least FreeBSD and Linux (GLIBC):
345   // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
346   // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
347   switch (T.getOS()) {
348   case Triple::Darwin:
349   case Triple::MacOSX:
350   case Triple::IOS:
351   case Triple::TvOS:
352   case Triple::WatchOS:
353   case Triple::FreeBSD:
354   case Triple::Linux:
355     break;
356   default:
357     TLI.setUnavailable(LibFunc::ffsll);
358   }
359 
360   // The following functions are available on at least FreeBSD:
361   // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
362   // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
363   // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
364   if (!T.isOSFreeBSD()) {
365     TLI.setUnavailable(LibFunc::fls);
366     TLI.setUnavailable(LibFunc::flsl);
367     TLI.setUnavailable(LibFunc::flsll);
368   }
369 
370   // The following functions are available on at least Linux:
371   if (!T.isOSLinux()) {
372     TLI.setUnavailable(LibFunc::dunder_strdup);
373     TLI.setUnavailable(LibFunc::dunder_strtok_r);
374     TLI.setUnavailable(LibFunc::dunder_isoc99_scanf);
375     TLI.setUnavailable(LibFunc::dunder_isoc99_sscanf);
376     TLI.setUnavailable(LibFunc::under_IO_getc);
377     TLI.setUnavailable(LibFunc::under_IO_putc);
378     TLI.setUnavailable(LibFunc::memalign);
379     TLI.setUnavailable(LibFunc::fopen64);
380     TLI.setUnavailable(LibFunc::fseeko64);
381     TLI.setUnavailable(LibFunc::fstat64);
382     TLI.setUnavailable(LibFunc::fstatvfs64);
383     TLI.setUnavailable(LibFunc::ftello64);
384     TLI.setUnavailable(LibFunc::lstat64);
385     TLI.setUnavailable(LibFunc::open64);
386     TLI.setUnavailable(LibFunc::stat64);
387     TLI.setUnavailable(LibFunc::statvfs64);
388     TLI.setUnavailable(LibFunc::tmpfile64);
389   }
390 
391   TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary);
392 }
393 
TargetLibraryInfoImpl()394 TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
395   // Default to everything being available.
396   memset(AvailableArray, -1, sizeof(AvailableArray));
397 
398   initialize(*this, Triple(), StandardNames);
399 }
400 
TargetLibraryInfoImpl(const Triple & T)401 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
402   // Default to everything being available.
403   memset(AvailableArray, -1, sizeof(AvailableArray));
404 
405   initialize(*this, T, StandardNames);
406 }
407 
TargetLibraryInfoImpl(const TargetLibraryInfoImpl & TLI)408 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
409     : CustomNames(TLI.CustomNames) {
410   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
411   VectorDescs = TLI.VectorDescs;
412   ScalarDescs = TLI.ScalarDescs;
413 }
414 
TargetLibraryInfoImpl(TargetLibraryInfoImpl && TLI)415 TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
416     : CustomNames(std::move(TLI.CustomNames)) {
417   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
418             AvailableArray);
419   VectorDescs = TLI.VectorDescs;
420   ScalarDescs = TLI.ScalarDescs;
421 }
422 
operator =(const TargetLibraryInfoImpl & TLI)423 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
424   CustomNames = TLI.CustomNames;
425   memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
426   return *this;
427 }
428 
operator =(TargetLibraryInfoImpl && TLI)429 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
430   CustomNames = std::move(TLI.CustomNames);
431   std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
432             AvailableArray);
433   return *this;
434 }
435 
sanitizeFunctionName(StringRef funcName)436 static StringRef sanitizeFunctionName(StringRef funcName) {
437   // Filter out empty names and names containing null bytes, those can't be in
438   // our table.
439   if (funcName.empty() || funcName.find('\0') != StringRef::npos)
440     return StringRef();
441 
442   // Check for \01 prefix that is used to mangle __asm declarations and
443   // strip it if present.
444   return GlobalValue::getRealLinkageName(funcName);
445 }
446 
getLibFunc(StringRef funcName,LibFunc::Func & F) const447 bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName,
448                                    LibFunc::Func &F) const {
449   const char *const *Start = &StandardNames[0];
450   const char *const *End = &StandardNames[LibFunc::NumLibFuncs];
451 
452   funcName = sanitizeFunctionName(funcName);
453   if (funcName.empty())
454     return false;
455 
456   const char *const *I = std::lower_bound(
457       Start, End, funcName, [](const char *LHS, StringRef RHS) {
458         return std::strncmp(LHS, RHS.data(), RHS.size()) < 0;
459       });
460   if (I != End && *I == funcName) {
461     F = (LibFunc::Func)(I - Start);
462     return true;
463   }
464   return false;
465 }
466 
disableAllFunctions()467 void TargetLibraryInfoImpl::disableAllFunctions() {
468   memset(AvailableArray, 0, sizeof(AvailableArray));
469 }
470 
compareByScalarFnName(const VecDesc & LHS,const VecDesc & RHS)471 static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
472   return std::strncmp(LHS.ScalarFnName, RHS.ScalarFnName,
473                       std::strlen(RHS.ScalarFnName)) < 0;
474 }
475 
compareByVectorFnName(const VecDesc & LHS,const VecDesc & RHS)476 static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
477   return std::strncmp(LHS.VectorFnName, RHS.VectorFnName,
478                       std::strlen(RHS.VectorFnName)) < 0;
479 }
480 
compareWithScalarFnName(const VecDesc & LHS,StringRef S)481 static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
482   return std::strncmp(LHS.ScalarFnName, S.data(), S.size()) < 0;
483 }
484 
compareWithVectorFnName(const VecDesc & LHS,StringRef S)485 static bool compareWithVectorFnName(const VecDesc &LHS, StringRef S) {
486   return std::strncmp(LHS.VectorFnName, S.data(), S.size()) < 0;
487 }
488 
addVectorizableFunctions(ArrayRef<VecDesc> Fns)489 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
490   VectorDescs.insert(VectorDescs.end(), Fns.begin(), Fns.end());
491   std::sort(VectorDescs.begin(), VectorDescs.end(), compareByScalarFnName);
492 
493   ScalarDescs.insert(ScalarDescs.end(), Fns.begin(), Fns.end());
494   std::sort(ScalarDescs.begin(), ScalarDescs.end(), compareByVectorFnName);
495 }
496 
addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib)497 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
498     enum VectorLibrary VecLib) {
499   switch (VecLib) {
500   case Accelerate: {
501     const VecDesc VecFuncs[] = {
502         // Floating-Point Arithmetic and Auxiliary Functions
503         {"ceilf", "vceilf", 4},
504         {"fabsf", "vfabsf", 4},
505         {"llvm.fabs.f32", "vfabsf", 4},
506         {"floorf", "vfloorf", 4},
507         {"sqrtf", "vsqrtf", 4},
508         {"llvm.sqrt.f32", "vsqrtf", 4},
509 
510         // Exponential and Logarithmic Functions
511         {"expf", "vexpf", 4},
512         {"llvm.exp.f32", "vexpf", 4},
513         {"expm1f", "vexpm1f", 4},
514         {"logf", "vlogf", 4},
515         {"llvm.log.f32", "vlogf", 4},
516         {"log1pf", "vlog1pf", 4},
517         {"log10f", "vlog10f", 4},
518         {"llvm.log10.f32", "vlog10f", 4},
519         {"logbf", "vlogbf", 4},
520 
521         // Trigonometric Functions
522         {"sinf", "vsinf", 4},
523         {"llvm.sin.f32", "vsinf", 4},
524         {"cosf", "vcosf", 4},
525         {"llvm.cos.f32", "vcosf", 4},
526         {"tanf", "vtanf", 4},
527         {"asinf", "vasinf", 4},
528         {"acosf", "vacosf", 4},
529         {"atanf", "vatanf", 4},
530 
531         // Hyperbolic Functions
532         {"sinhf", "vsinhf", 4},
533         {"coshf", "vcoshf", 4},
534         {"tanhf", "vtanhf", 4},
535         {"asinhf", "vasinhf", 4},
536         {"acoshf", "vacoshf", 4},
537         {"atanhf", "vatanhf", 4},
538     };
539     addVectorizableFunctions(VecFuncs);
540     break;
541   }
542   case NoLibrary:
543     break;
544   }
545 }
546 
isFunctionVectorizable(StringRef funcName) const547 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
548   funcName = sanitizeFunctionName(funcName);
549   if (funcName.empty())
550     return false;
551 
552   std::vector<VecDesc>::const_iterator I = std::lower_bound(
553       VectorDescs.begin(), VectorDescs.end(), funcName,
554       compareWithScalarFnName);
555   return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
556 }
557 
getVectorizedFunction(StringRef F,unsigned VF) const558 StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
559                                                        unsigned VF) const {
560   F = sanitizeFunctionName(F);
561   if (F.empty())
562     return F;
563   std::vector<VecDesc>::const_iterator I = std::lower_bound(
564       VectorDescs.begin(), VectorDescs.end(), F, compareWithScalarFnName);
565   while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
566     if (I->VectorizationFactor == VF)
567       return I->VectorFnName;
568     ++I;
569   }
570   return StringRef();
571 }
572 
getScalarizedFunction(StringRef F,unsigned & VF) const573 StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F,
574                                                        unsigned &VF) const {
575   F = sanitizeFunctionName(F);
576   if (F.empty())
577     return F;
578 
579   std::vector<VecDesc>::const_iterator I = std::lower_bound(
580       ScalarDescs.begin(), ScalarDescs.end(), F, compareWithVectorFnName);
581   if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F)
582     return StringRef();
583   VF = I->VectorizationFactor;
584   return I->ScalarFnName;
585 }
586 
run(Module & M)587 TargetLibraryInfo TargetLibraryAnalysis::run(Module &M) {
588   if (PresetInfoImpl)
589     return TargetLibraryInfo(*PresetInfoImpl);
590 
591   return TargetLibraryInfo(lookupInfoImpl(Triple(M.getTargetTriple())));
592 }
593 
run(Function & F)594 TargetLibraryInfo TargetLibraryAnalysis::run(Function &F) {
595   if (PresetInfoImpl)
596     return TargetLibraryInfo(*PresetInfoImpl);
597 
598   return TargetLibraryInfo(
599       lookupInfoImpl(Triple(F.getParent()->getTargetTriple())));
600 }
601 
lookupInfoImpl(Triple T)602 TargetLibraryInfoImpl &TargetLibraryAnalysis::lookupInfoImpl(Triple T) {
603   std::unique_ptr<TargetLibraryInfoImpl> &Impl =
604       Impls[T.normalize()];
605   if (!Impl)
606     Impl.reset(new TargetLibraryInfoImpl(T));
607 
608   return *Impl;
609 }
610 
611 
TargetLibraryInfoWrapperPass()612 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
613     : ImmutablePass(ID), TLIImpl(), TLI(TLIImpl) {
614   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
615 }
616 
TargetLibraryInfoWrapperPass(const Triple & T)617 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
618     : ImmutablePass(ID), TLIImpl(T), TLI(TLIImpl) {
619   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
620 }
621 
TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl & TLIImpl)622 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
623     const TargetLibraryInfoImpl &TLIImpl)
624     : ImmutablePass(ID), TLIImpl(TLIImpl), TLI(this->TLIImpl) {
625   initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
626 }
627 
628 char TargetLibraryAnalysis::PassID;
629 
630 // Register the basic pass.
631 INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
632                 "Target Library Information", false, true)
633 char TargetLibraryInfoWrapperPass::ID = 0;
634 
anchor()635 void TargetLibraryInfoWrapperPass::anchor() {}
636