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