blob: faf45cb53955980fd7c674cc841c43807df0d3bc [file] [log] [blame]
Chris Lattner0e125bb2011-02-18 21:50:34 +00001//===-- 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
Chandler Carruth62d42152015-01-15 02:16:27 +000014#include "llvm/Analysis/TargetLibraryInfo.h"
Chris Lattner0e125bb2011-02-18 21:50:34 +000015#include "llvm/ADT/Triple.h"
Michael Zolotukhin6d8a2aa2015-03-17 19:50:55 +000016#include "llvm/Support/CommandLine.h"
Chris Lattner0e125bb2011-02-18 21:50:34 +000017using namespace llvm;
18
Michael Zolotukhin6d8a2aa2015-03-17 19:50:55 +000019static 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"),
Matt Mastena6669a12016-07-29 16:42:44 +000026 clEnumValN(TargetLibraryInfoImpl::SVML, "SVML",
27 "Intel SVML library"),
Michael Zolotukhin6d8a2aa2015-03-17 19:50:55 +000028 clEnumValEnd));
29
Benjamin Kramer57a3d082015-03-08 16:07:39 +000030const char *const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] = {
Jan Wen Voungcd3d25a2015-03-03 23:41:58 +000031#define TLI_DEFINE_STRING
32#include "llvm/Analysis/TargetLibraryInfo.def"
Benjamin Kramer57a3d082015-03-08 16:07:39 +000033};
Eli Friedman489c0ff2011-11-17 01:27:36 +000034
Bob Wilsond8d92d92013-11-03 06:48:38 +000035static bool hasSinCosPiStret(const Triple &T) {
36 // Only Darwin variants have _stret versions of combined trig functions.
Bob Wilson9868d712014-10-09 05:43:30 +000037 if (!T.isOSDarwin())
Bob Wilsond8d92d92013-11-03 06:48:38 +000038 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
Bob Wilson9868d712014-10-09 05:43:30 +000047 if (T.isiOS() && T.isOSVersionLT(7, 0))
Bob Wilsond8d92d92013-11-03 06:48:38 +000048 return false;
49
50 return true;
51}
52
Chris Lattner0e125bb2011-02-18 21:50:34 +000053/// 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.
Chandler Carruthc0291862015-01-24 02:06:09 +000056static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
Craig Toppere30b8ca2016-01-03 19:43:40 +000057 ArrayRef<const char *> StandardNames) {
Bob Wilsonc740e3f2012-08-03 04:06:22 +000058 // Verify that the StandardNames array is in alphabetical order.
Craig Toppere30b8ca2016-01-03 19:43:40 +000059 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");
Tom Stellard36a03182014-04-02 19:53:29 +000064
Nicolai Hahnle78fd4f02015-12-15 17:24:15 +000065 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);
Nicolai Haehnle377975f2016-06-14 13:14:53 +000070 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);
Nicolai Hahnle78fd4f02015-12-15 17:24:15 +000076 }
77
Tom Stellardd00a9232015-01-07 01:17:37 +000078 // There are no library implementations of mempcy and memset for AMD gpus and
Tom Stellard36a03182014-04-02 19:53:29 +000079 // these can be difficult to lower in the backend.
Tom Stellardd00a9232015-01-07 01:17:37 +000080 if (T.getArch() == Triple::r600 ||
Dan Gohman05532992016-01-19 14:49:23 +000081 T.getArch() == Triple::amdgcn) {
Tom Stellard36a03182014-04-02 19:53:29 +000082 TLI.setUnavailable(LibFunc::memcpy);
83 TLI.setUnavailable(LibFunc::memset);
84 TLI.setUnavailable(LibFunc::memset_pattern16);
85 return;
86 }
87
Nico Weberad156922014-03-07 18:08:54 +000088 // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later.
Tim Northover8b403662015-10-28 22:51:16 +000089 // All versions of watchOS support it.
Daniel Dunbarcd01ed52011-04-20 00:14:25 +000090 if (T.isMacOSX()) {
91 if (T.isMacOSXVersionLT(10, 5))
Daniel Dunbar9483bb62011-04-19 20:44:08 +000092 TLI.setUnavailable(LibFunc::memset_pattern16);
Cameron Esfahani943908b2013-08-29 20:23:14 +000093 } else if (T.isiOS()) {
Daniel Dunbar9483bb62011-04-19 20:44:08 +000094 if (T.isOSVersionLT(3, 0))
95 TLI.setUnavailable(LibFunc::memset_pattern16);
Tim Northover8b403662015-10-28 22:51:16 +000096 } else if (!T.isWatchOS()) {
Chris Lattner0e125bb2011-02-18 21:50:34 +000097 TLI.setUnavailable(LibFunc::memset_pattern16);
Daniel Dunbar9483bb62011-04-19 20:44:08 +000098 }
Richard Osborne815de532011-03-03 13:17:51 +000099
Bob Wilsond8d92d92013-11-03 06:48:38 +0000100 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);
Tim Northover103e6482014-02-04 16:28:20 +0000106 TLI.setUnavailable(LibFunc::sincospif_stret);
Bob Wilsond8d92d92013-11-03 06:48:38 +0000107 }
108
Eli Friedman489c0ff2011-11-17 01:27:36 +0000109 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
Duncan Sandseeb50c82011-06-09 11:11:45 +0000120 // iprintf and friends are only available on XCore and TCE.
121 if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) {
Richard Osborne815de532011-03-03 13:17:51 +0000122 TLI.setUnavailable(LibFunc::iprintf);
Richard Osborne2dfb8882011-03-03 14:09:28 +0000123 TLI.setUnavailable(LibFunc::siprintf);
Richard Osborneaf52c522011-03-03 14:20:22 +0000124 TLI.setUnavailable(LibFunc::fiprintf);
Richard Osborne2dfb8882011-03-03 14:09:28 +0000125 }
Joe Groffa81bcbb2012-04-17 23:05:54 +0000126
Saleem Abdulrasool8dc8fb12014-07-24 22:09:06 +0000127 if (T.isOSWindows() && !T.isOSCygMing()) {
Joe Groffa81bcbb2012-04-17 23:05:54 +0000128 // 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);
Yi Jiang6ab044e2013-12-16 22:42:40 +0000141 TLI.setUnavailable(LibFunc::fmaxl);
142 TLI.setUnavailable(LibFunc::fminl);
Joe Groffa81bcbb2012-04-17 23:05:54 +0000143 TLI.setUnavailable(LibFunc::fmodl);
Meador Ingeb904e6e2013-03-05 21:47:40 +0000144 TLI.setUnavailable(LibFunc::frexpl);
Benjamin Kramer34f460e2014-02-04 20:27:23 +0000145 TLI.setUnavailable(LibFunc::ldexpf);
146 TLI.setUnavailable(LibFunc::ldexpl);
Joe Groffa81bcbb2012-04-17 23:05:54 +0000147 TLI.setUnavailable(LibFunc::logl);
Meador Ingeb904e6e2013-03-05 21:47:40 +0000148 TLI.setUnavailable(LibFunc::modfl);
Joe Groffa81bcbb2012-04-17 23:05:54 +0000149 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
Chad Rosier7fb0cd22012-08-21 23:28:56 +0000157 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);
Joe Groffa81bcbb2012-04-17 23:05:54 +0000169 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);
Chad Rosier7fb0cd22012-08-21 23:28:56 +0000181 TLI.setUnavailable(LibFunc::logb);
182 TLI.setUnavailable(LibFunc::logbf);
183 TLI.setUnavailable(LibFunc::logbl);
Joe Groffa81bcbb2012-04-17 23:05:54 +0000184 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);
Yi Jiang6ab044e2013-12-16 22:42:40 +0000212 TLI.setUnavailable(LibFunc::fminf);
213 TLI.setUnavailable(LibFunc::fmaxf);
Joe Groffa81bcbb2012-04-17 23:05:54 +0000214 TLI.setUnavailable(LibFunc::fmodf);
215 TLI.setUnavailable(LibFunc::logf);
David Majnemereac58d82016-05-08 08:15:50 +0000216 TLI.setUnavailable(LibFunc::log10f);
217 TLI.setUnavailable(LibFunc::modff);
Joe Groffa81bcbb2012-04-17 23:05:54 +0000218 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 }
Meador Inge2526a422012-11-10 03:11:06 +0000225
Meador Ingeb904e6e2013-03-05 21:47:40 +0000226 // 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);
Meador Inge780a1862012-11-22 15:36:42 +0000237 TLI.setUnavailable(LibFunc::ffs);
Meador Ingeb904e6e2013-03-05 21:47:40 +0000238 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);
Michael Gottesmanf7459c72013-07-03 04:00:51 +0000250 TLI.setUnavailable(LibFunc::gettimeofday);
Meador Ingeb904e6e2013-03-05 21:47:40 +0000251 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);
Meador Inge780a1862012-11-22 15:36:42 +0000283
Meador Ingeb904e6e2013-03-05 21:47:40 +0000284 // Win32 does *not* provide provide these functions, but they are
285 // specified by C99:
286 TLI.setUnavailable(LibFunc::atoll);
287 TLI.setUnavailable(LibFunc::frexpf);
Meador Inge780a1862012-11-22 15:36:42 +0000288 TLI.setUnavailable(LibFunc::llabs);
289 }
290
Reid Klecknerf4355ee2013-12-26 19:17:04 +0000291 switch (T.getOS()) {
Reid Klecknerf4355ee2013-12-26 19:17:04 +0000292 case Triple::MacOSX:
Chandler Carruthf5689f82013-12-28 02:40:19 +0000293 // 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.
Reid Klecknerf4355ee2013-12-26 19:17:04 +0000296 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:
Tim Northover89a6eef2015-11-02 18:00:00 +0000306 case Triple::TvOS:
Tim Northover8b403662015-10-28 22:51:16 +0000307 case Triple::WatchOS:
Reid Klecknerf4355ee2013-12-26 19:17:04 +0000308 TLI.setUnavailable(LibFunc::exp10l);
Tim Northover8b403662015-10-28 22:51:16 +0000309 if (!T.isWatchOS() && (T.isOSVersionLT(7, 0) ||
310 (T.isOSVersionLT(9, 0) &&
311 (T.getArch() == Triple::x86 ||
312 T.getArch() == Triple::x86_64)))) {
Reid Klecknerf4355ee2013-12-26 19:17:04 +0000313 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;
Chandler Carruthf5689f82013-12-28 02:40:19 +0000320 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.
Reid Klecknerf4355ee2013-12-26 19:17:04 +0000327 default:
328 TLI.setUnavailable(LibFunc::exp10);
329 TLI.setUnavailable(LibFunc::exp10f);
330 TLI.setUnavailable(LibFunc::exp10l);
331 }
332
Meador Inge780a1862012-11-22 15:36:42 +0000333 // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
334 // Linux (GLIBC):
335 // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
Davide Italiano83b34812015-11-01 17:00:13 +0000336 // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
Meador Inge780a1862012-11-22 15:36:42 +0000337 // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
338 switch (T.getOS()) {
339 case Triple::Darwin:
340 case Triple::MacOSX:
341 case Triple::IOS:
Tim Northover89a6eef2015-11-02 18:00:00 +0000342 case Triple::TvOS:
Tim Northover8b403662015-10-28 22:51:16 +0000343 case Triple::WatchOS:
Meador Inge780a1862012-11-22 15:36:42 +0000344 case Triple::FreeBSD:
345 case Triple::Linux:
346 break;
347 default:
348 TLI.setUnavailable(LibFunc::ffsl);
349 }
350
351 // ffsll is available on at least FreeBSD and Linux (GLIBC):
Davide Italiano83b34812015-11-01 17:00:13 +0000352 // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
Meador Inge780a1862012-11-22 15:36:42 +0000353 // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
354 switch (T.getOS()) {
Tim Northover89a6eef2015-11-02 18:00:00 +0000355 case Triple::Darwin:
356 case Triple::MacOSX:
357 case Triple::IOS:
358 case Triple::TvOS:
359 case Triple::WatchOS:
Meador Inge780a1862012-11-22 15:36:42 +0000360 case Triple::FreeBSD:
361 case Triple::Linux:
362 break;
363 default:
364 TLI.setUnavailable(LibFunc::ffsll);
Joe Groffa81bcbb2012-04-17 23:05:54 +0000365 }
Meador Ingeb904e6e2013-03-05 21:47:40 +0000366
Davide Italianobfd30822015-11-09 23:23:20 +0000367 // The following functions are available on at least FreeBSD:
368 // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
369 // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
370 // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
371 if (!T.isOSFreeBSD()) {
372 TLI.setUnavailable(LibFunc::fls);
373 TLI.setUnavailable(LibFunc::flsl);
374 TLI.setUnavailable(LibFunc::flsll);
375 }
376
Meador Ingeb904e6e2013-03-05 21:47:40 +0000377 // The following functions are available on at least Linux:
Cameron Esfahani943908b2013-08-29 20:23:14 +0000378 if (!T.isOSLinux()) {
Meador Ingeb904e6e2013-03-05 21:47:40 +0000379 TLI.setUnavailable(LibFunc::dunder_strdup);
380 TLI.setUnavailable(LibFunc::dunder_strtok_r);
381 TLI.setUnavailable(LibFunc::dunder_isoc99_scanf);
382 TLI.setUnavailable(LibFunc::dunder_isoc99_sscanf);
383 TLI.setUnavailable(LibFunc::under_IO_getc);
384 TLI.setUnavailable(LibFunc::under_IO_putc);
385 TLI.setUnavailable(LibFunc::memalign);
386 TLI.setUnavailable(LibFunc::fopen64);
387 TLI.setUnavailable(LibFunc::fseeko64);
388 TLI.setUnavailable(LibFunc::fstat64);
389 TLI.setUnavailable(LibFunc::fstatvfs64);
390 TLI.setUnavailable(LibFunc::ftello64);
391 TLI.setUnavailable(LibFunc::lstat64);
392 TLI.setUnavailable(LibFunc::open64);
393 TLI.setUnavailable(LibFunc::stat64);
394 TLI.setUnavailable(LibFunc::statvfs64);
395 TLI.setUnavailable(LibFunc::tmpfile64);
396 }
Michael Zolotukhin6d8a2aa2015-03-17 19:50:55 +0000397
Justin Lebar51132882016-01-26 23:51:06 +0000398 // As currently implemented in clang, NVPTX code has no standard library to
399 // speak of. Headers provide a standard-ish library implementation, but many
400 // of the signatures are wrong -- for example, many libm functions are not
401 // extern "C".
402 //
403 // libdevice, an IR library provided by nvidia, is linked in by the front-end,
404 // but only used functions are provided to llvm. Moreover, most of the
405 // functions in libdevice don't map precisely to standard library functions.
406 //
407 // FIXME: Having no standard library prevents e.g. many fastmath
408 // optimizations, so this situation should be fixed.
David Majnemerae272d72016-03-31 21:29:57 +0000409 if (T.isNVPTX()) {
Justin Lebar51132882016-01-26 23:51:06 +0000410 TLI.disableAllFunctions();
David Majnemerae272d72016-03-31 21:29:57 +0000411 TLI.setAvailable(LibFunc::nvvm_reflect);
412 } else {
413 TLI.setUnavailable(LibFunc::nvvm_reflect);
414 }
Justin Lebar51132882016-01-26 23:51:06 +0000415
Michael Zolotukhin6d8a2aa2015-03-17 19:50:55 +0000416 TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary);
Chris Lattner0e125bb2011-02-18 21:50:34 +0000417}
418
Chandler Carruthc0291862015-01-24 02:06:09 +0000419TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
Chris Lattner0e125bb2011-02-18 21:50:34 +0000420 // Default to everything being available.
421 memset(AvailableArray, -1, sizeof(AvailableArray));
422
Bob Wilsonc740e3f2012-08-03 04:06:22 +0000423 initialize(*this, Triple(), StandardNames);
Chris Lattner0e125bb2011-02-18 21:50:34 +0000424}
425
Chandler Carruthc0291862015-01-24 02:06:09 +0000426TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
Chris Lattner0e125bb2011-02-18 21:50:34 +0000427 // Default to everything being available.
428 memset(AvailableArray, -1, sizeof(AvailableArray));
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000429
Bob Wilsonc740e3f2012-08-03 04:06:22 +0000430 initialize(*this, T, StandardNames);
Chris Lattner0e125bb2011-02-18 21:50:34 +0000431}
Chris Lattner1341df92011-02-18 22:34:03 +0000432
Chandler Carruthc0291862015-01-24 02:06:09 +0000433TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
Chandler Carruth8ca43222015-01-15 11:39:46 +0000434 : CustomNames(TLI.CustomNames) {
Chris Lattner4c0d9e22011-05-21 20:09:13 +0000435 memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
Michael Zolotukhine8f25512015-03-17 19:22:30 +0000436 VectorDescs = TLI.VectorDescs;
437 ScalarDescs = TLI.ScalarDescs;
Chandler Carruth8ca43222015-01-15 11:39:46 +0000438}
439
Chandler Carruthc0291862015-01-24 02:06:09 +0000440TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
Chandler Carruth8ca43222015-01-15 11:39:46 +0000441 : CustomNames(std::move(TLI.CustomNames)) {
442 std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
443 AvailableArray);
Michael Zolotukhine8f25512015-03-17 19:22:30 +0000444 VectorDescs = TLI.VectorDescs;
445 ScalarDescs = TLI.ScalarDescs;
Chandler Carruth8ca43222015-01-15 11:39:46 +0000446}
447
Chandler Carruthc0291862015-01-24 02:06:09 +0000448TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
Eli Friedman489c0ff2011-11-17 01:27:36 +0000449 CustomNames = TLI.CustomNames;
Chandler Carruth8ca43222015-01-15 11:39:46 +0000450 memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
451 return *this;
452}
453
Chandler Carruthc0291862015-01-24 02:06:09 +0000454TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
Chandler Carruth8ca43222015-01-15 11:39:46 +0000455 CustomNames = std::move(TLI.CustomNames);
456 std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
457 AvailableArray);
458 return *this;
Chris Lattner4c0d9e22011-05-21 20:09:13 +0000459}
460
Michael Zolotukhin21abdf92015-03-02 23:24:40 +0000461static StringRef sanitizeFunctionName(StringRef funcName) {
Benjamin Kramer160f72d2013-03-09 13:48:23 +0000462 // Filter out empty names and names containing null bytes, those can't be in
463 // our table.
464 if (funcName.empty() || funcName.find('\0') != StringRef::npos)
Michael Zolotukhin21abdf92015-03-02 23:24:40 +0000465 return StringRef();
Benjamin Kramer160f72d2013-03-09 13:48:23 +0000466
Meador Ingeb904e6e2013-03-05 21:47:40 +0000467 // Check for \01 prefix that is used to mangle __asm declarations and
468 // strip it if present.
David Majnemercde33032015-03-30 22:58:10 +0000469 return GlobalValue::getRealLinkageName(funcName);
Michael Zolotukhin21abdf92015-03-02 23:24:40 +0000470}
471
472bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName,
Ahmed Bougacha220c4012016-04-27 19:04:29 +0000473 LibFunc::Func &F) const {
Benjamin Kramer57a3d082015-03-08 16:07:39 +0000474 const char *const *Start = &StandardNames[0];
475 const char *const *End = &StandardNames[LibFunc::NumLibFuncs];
Michael Zolotukhin21abdf92015-03-02 23:24:40 +0000476
477 funcName = sanitizeFunctionName(funcName);
478 if (funcName.empty())
479 return false;
480
Benjamin Kramer57a3d082015-03-08 16:07:39 +0000481 const char *const *I = std::lower_bound(
Michael Zolotukhind3b76a32015-03-02 20:50:08 +0000482 Start, End, funcName, [](const char *LHS, StringRef RHS) {
483 return std::strncmp(LHS, RHS.data(), RHS.size()) < 0;
484 });
Bob Wilsonc740e3f2012-08-03 04:06:22 +0000485 if (I != End && *I == funcName) {
486 F = (LibFunc::Func)(I - Start);
487 return true;
488 }
489 return false;
490}
Chris Lattner4c0d9e22011-05-21 20:09:13 +0000491
Ahmed Bougachad765a822016-04-27 19:04:35 +0000492bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
493 LibFunc::Func F,
494 const DataLayout *DL) const {
495 LLVMContext &Ctx = FTy.getContext();
496 Type *PCharTy = Type::getInt8PtrTy(Ctx);
497 Type *SizeTTy = DL ? DL->getIntPtrType(Ctx, /*AS=*/0) : nullptr;
498 auto IsSizeTTy = [SizeTTy](Type *Ty) {
499 return SizeTTy ? Ty == SizeTTy : Ty->isIntegerTy();
500 };
501 unsigned NumParams = FTy.getNumParams();
502
503 switch (F) {
504 case LibFunc::strlen:
505 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
506 FTy.getReturnType()->isIntegerTy());
507
508 case LibFunc::strchr:
509 case LibFunc::strrchr:
510 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
511 FTy.getParamType(0) == FTy.getReturnType() &&
512 FTy.getParamType(1)->isIntegerTy());
513
514 case LibFunc::strtol:
515 case LibFunc::strtod:
516 case LibFunc::strtof:
517 case LibFunc::strtoul:
518 case LibFunc::strtoll:
519 case LibFunc::strtold:
520 case LibFunc::strtoull:
521 return ((NumParams == 2 || NumParams == 3) &&
522 FTy.getParamType(0)->isPointerTy() &&
523 FTy.getParamType(1)->isPointerTy());
524 case LibFunc::strcat:
525 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
526 FTy.getParamType(0) == FTy.getReturnType() &&
527 FTy.getParamType(1) == FTy.getReturnType());
528
529 case LibFunc::strncat:
530 return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
531 FTy.getParamType(0) == FTy.getReturnType() &&
532 FTy.getParamType(1) == FTy.getReturnType() &&
533 FTy.getParamType(2)->isIntegerTy());
534
535 case LibFunc::strcpy_chk:
536 case LibFunc::stpcpy_chk:
537 --NumParams;
538 if (!IsSizeTTy(FTy.getParamType(NumParams)))
539 return false;
540 // fallthrough
541 case LibFunc::strcpy:
542 case LibFunc::stpcpy:
543 return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(0) &&
544 FTy.getParamType(0) == FTy.getParamType(1) &&
545 FTy.getParamType(0) == PCharTy);
546
547 case LibFunc::strncpy_chk:
548 case LibFunc::stpncpy_chk:
549 --NumParams;
550 if (!IsSizeTTy(FTy.getParamType(NumParams)))
551 return false;
552 // fallthrough
553 case LibFunc::strncpy:
554 case LibFunc::stpncpy:
555 return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
556 FTy.getParamType(0) == FTy.getParamType(1) &&
557 FTy.getParamType(0) == PCharTy &&
558 FTy.getParamType(2)->isIntegerTy());
559
560 case LibFunc::strxfrm:
561 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
562 FTy.getParamType(1)->isPointerTy());
563
564 case LibFunc::strcmp:
565 return (NumParams == 2 && FTy.getReturnType()->isIntegerTy(32) &&
566 FTy.getParamType(0)->isPointerTy() &&
567 FTy.getParamType(0) == FTy.getParamType(1));
568
569 case LibFunc::strncmp:
570 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
571 FTy.getParamType(0)->isPointerTy() &&
572 FTy.getParamType(0) == FTy.getParamType(1) &&
573 FTy.getParamType(2)->isIntegerTy());
574
575 case LibFunc::strspn:
576 case LibFunc::strcspn:
577 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
578 FTy.getParamType(0) == FTy.getParamType(1) &&
579 FTy.getReturnType()->isIntegerTy());
580
581 case LibFunc::strcoll:
582 case LibFunc::strcasecmp:
583 case LibFunc::strncasecmp:
584 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
585 FTy.getParamType(1)->isPointerTy());
586
587 case LibFunc::strstr:
588 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
589 FTy.getParamType(0)->isPointerTy() &&
590 FTy.getParamType(1)->isPointerTy());
591
592 case LibFunc::strpbrk:
593 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
594 FTy.getReturnType() == FTy.getParamType(0) &&
595 FTy.getParamType(0) == FTy.getParamType(1));
596
597 case LibFunc::strtok:
598 case LibFunc::strtok_r:
599 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
600 case LibFunc::scanf:
Ahmed Bougachad765a822016-04-27 19:04:35 +0000601 case LibFunc::setbuf:
602 case LibFunc::setvbuf:
603 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
604 case LibFunc::strdup:
605 case LibFunc::strndup:
606 return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
607 FTy.getParamType(0)->isPointerTy());
Davide Italiano9cc0bca2016-06-21 04:32:21 +0000608 case LibFunc::sscanf:
Ahmed Bougachad765a822016-04-27 19:04:35 +0000609 case LibFunc::stat:
610 case LibFunc::statvfs:
Ahmed Bougachad765a822016-04-27 19:04:35 +0000611 case LibFunc::sprintf:
612 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
613 FTy.getParamType(1)->isPointerTy());
614 case LibFunc::snprintf:
615 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
616 FTy.getParamType(2)->isPointerTy());
617 case LibFunc::setitimer:
618 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
619 FTy.getParamType(2)->isPointerTy());
620 case LibFunc::system:
621 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
622 case LibFunc::malloc:
623 return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
624 case LibFunc::memcmp:
625 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
626 FTy.getParamType(1)->isPointerTy() &&
627 FTy.getReturnType()->isIntegerTy(32));
628
629 case LibFunc::memchr:
630 case LibFunc::memrchr:
631 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
632 FTy.getParamType(1)->isIntegerTy(32) &&
633 FTy.getParamType(2)->isIntegerTy() &&
634 FTy.getReturnType()->isPointerTy());
635 case LibFunc::modf:
636 case LibFunc::modff:
637 case LibFunc::modfl:
638 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
639
640 case LibFunc::memcpy_chk:
641 case LibFunc::memmove_chk:
642 --NumParams;
643 if (!IsSizeTTy(FTy.getParamType(NumParams)))
644 return false;
645 // fallthrough
646 case LibFunc::memcpy:
Andrew Kaylorb99d1cc2016-07-29 18:23:18 +0000647 case LibFunc::mempcpy:
Ahmed Bougachad765a822016-04-27 19:04:35 +0000648 case LibFunc::memmove:
649 return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
650 FTy.getParamType(0)->isPointerTy() &&
651 FTy.getParamType(1)->isPointerTy() &&
652 IsSizeTTy(FTy.getParamType(2)));
653
654 case LibFunc::memset_chk:
655 --NumParams;
656 if (!IsSizeTTy(FTy.getParamType(NumParams)))
657 return false;
658 // fallthrough
659 case LibFunc::memset:
660 return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
661 FTy.getParamType(0)->isPointerTy() &&
662 FTy.getParamType(1)->isIntegerTy() &&
663 IsSizeTTy(FTy.getParamType(2)));
664
665 case LibFunc::memccpy:
666 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
667 case LibFunc::memalign:
668 return (FTy.getReturnType()->isPointerTy());
Ahmed Bougachad765a822016-04-27 19:04:35 +0000669 case LibFunc::realloc:
670 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
671 FTy.getReturnType()->isPointerTy());
672 case LibFunc::read:
673 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
674 case LibFunc::rewind:
Ahmed Bougachad765a822016-04-27 19:04:35 +0000675 case LibFunc::rmdir:
676 case LibFunc::remove:
677 case LibFunc::realpath:
678 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
679 case LibFunc::rename:
680 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
681 FTy.getParamType(1)->isPointerTy());
682 case LibFunc::readlink:
683 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
684 FTy.getParamType(1)->isPointerTy());
685 case LibFunc::write:
686 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
687 case LibFunc::bcopy:
Ahmed Bougachad765a822016-04-27 19:04:35 +0000688 case LibFunc::bcmp:
689 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
690 FTy.getParamType(1)->isPointerTy());
691 case LibFunc::bzero:
692 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
693 case LibFunc::calloc:
694 return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
Ahmed Bougacha1fe3f1c2016-05-25 20:22:45 +0000695
696 case LibFunc::atof:
Ahmed Bougachad765a822016-04-27 19:04:35 +0000697 case LibFunc::atoi:
698 case LibFunc::atol:
Ahmed Bougachad765a822016-04-27 19:04:35 +0000699 case LibFunc::atoll:
Ahmed Bougacha1fe3f1c2016-05-25 20:22:45 +0000700 case LibFunc::ferror:
Ahmed Bougacha1fe3f1c2016-05-25 20:22:45 +0000701 case LibFunc::getenv:
702 case LibFunc::getpwnam:
703 case LibFunc::pclose:
704 case LibFunc::perror:
705 case LibFunc::printf:
706 case LibFunc::puts:
707 case LibFunc::uname:
708 case LibFunc::under_IO_getc:
709 case LibFunc::unlink:
710 case LibFunc::unsetenv:
Ahmed Bougachad765a822016-04-27 19:04:35 +0000711 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
Ahmed Bougacha1fe3f1c2016-05-25 20:22:45 +0000712
713 case LibFunc::chmod:
714 case LibFunc::chown:
715 case LibFunc::clearerr:
716 case LibFunc::closedir:
717 case LibFunc::ctermid:
718 case LibFunc::fclose:
719 case LibFunc::feof:
720 case LibFunc::fflush:
721 case LibFunc::fgetc:
722 case LibFunc::fileno:
723 case LibFunc::flockfile:
724 case LibFunc::free:
725 case LibFunc::fseek:
Ahmed Bougacha201b97f2016-05-25 21:16:33 +0000726 case LibFunc::fseeko64:
Ahmed Bougacha1fe3f1c2016-05-25 20:22:45 +0000727 case LibFunc::fseeko:
728 case LibFunc::fsetpos:
729 case LibFunc::ftell:
Ahmed Bougacha201b97f2016-05-25 21:16:33 +0000730 case LibFunc::ftello64:
Ahmed Bougacha1fe3f1c2016-05-25 20:22:45 +0000731 case LibFunc::ftello:
732 case LibFunc::ftrylockfile:
733 case LibFunc::funlockfile:
734 case LibFunc::getc:
735 case LibFunc::getc_unlocked:
736 case LibFunc::getlogin_r:
737 case LibFunc::mkdir:
738 case LibFunc::mktime:
739 case LibFunc::times:
740 return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
741
Ahmed Bougachad765a822016-04-27 19:04:35 +0000742 case LibFunc::access:
743 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
744 case LibFunc::fopen:
745 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
746 FTy.getParamType(0)->isPointerTy() &&
747 FTy.getParamType(1)->isPointerTy());
748 case LibFunc::fdopen:
749 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
750 FTy.getParamType(1)->isPointerTy());
Ahmed Bougachad765a822016-04-27 19:04:35 +0000751 case LibFunc::fputc:
752 case LibFunc::fstat:
753 case LibFunc::frexp:
754 case LibFunc::frexpf:
755 case LibFunc::frexpl:
756 case LibFunc::fstatvfs:
757 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
758 case LibFunc::fgets:
759 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
760 FTy.getParamType(2)->isPointerTy());
761 case LibFunc::fread:
762 return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
763 FTy.getParamType(3)->isPointerTy());
764 case LibFunc::fwrite:
765 return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() &&
766 FTy.getParamType(0)->isPointerTy() &&
767 FTy.getParamType(1)->isIntegerTy() &&
768 FTy.getParamType(2)->isIntegerTy() &&
769 FTy.getParamType(3)->isPointerTy());
770 case LibFunc::fputs:
771 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
772 FTy.getParamType(1)->isPointerTy());
773 case LibFunc::fscanf:
774 case LibFunc::fprintf:
775 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
776 FTy.getParamType(1)->isPointerTy());
777 case LibFunc::fgetpos:
778 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
779 FTy.getParamType(1)->isPointerTy());
Ahmed Bougachad765a822016-04-27 19:04:35 +0000780 case LibFunc::gets:
781 case LibFunc::getchar:
782 case LibFunc::getitimer:
783 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
Ahmed Bougachad765a822016-04-27 19:04:35 +0000784 case LibFunc::ungetc:
785 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
Ahmed Bougachad765a822016-04-27 19:04:35 +0000786 case LibFunc::utime:
787 case LibFunc::utimes:
788 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
789 FTy.getParamType(1)->isPointerTy());
790 case LibFunc::putc:
791 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
Ahmed Bougachad765a822016-04-27 19:04:35 +0000792 case LibFunc::pread:
793 case LibFunc::pwrite:
794 return (NumParams == 4 && FTy.getParamType(1)->isPointerTy());
Ahmed Bougachad765a822016-04-27 19:04:35 +0000795 case LibFunc::popen:
796 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
797 FTy.getParamType(0)->isPointerTy() &&
798 FTy.getParamType(1)->isPointerTy());
Ahmed Bougachad765a822016-04-27 19:04:35 +0000799 case LibFunc::vscanf:
800 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
801 case LibFunc::vsscanf:
802 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
803 FTy.getParamType(2)->isPointerTy());
804 case LibFunc::vfscanf:
805 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
806 FTy.getParamType(2)->isPointerTy());
807 case LibFunc::valloc:
808 return (FTy.getReturnType()->isPointerTy());
809 case LibFunc::vprintf:
810 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
811 case LibFunc::vfprintf:
812 case LibFunc::vsprintf:
813 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
814 FTy.getParamType(1)->isPointerTy());
815 case LibFunc::vsnprintf:
816 return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
817 FTy.getParamType(2)->isPointerTy());
818 case LibFunc::open:
819 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
820 case LibFunc::opendir:
821 return (NumParams == 1 && FTy.getReturnType()->isPointerTy() &&
822 FTy.getParamType(0)->isPointerTy());
823 case LibFunc::tmpfile:
824 return (FTy.getReturnType()->isPointerTy());
Ahmed Bougachad765a822016-04-27 19:04:35 +0000825 case LibFunc::htonl:
826 case LibFunc::htons:
827 case LibFunc::ntohl:
828 case LibFunc::ntohs:
829 case LibFunc::lstat:
830 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
831 FTy.getParamType(1)->isPointerTy());
832 case LibFunc::lchown:
833 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
834 case LibFunc::qsort:
835 return (NumParams == 4 && FTy.getParamType(3)->isPointerTy());
836 case LibFunc::dunder_strdup:
837 case LibFunc::dunder_strndup:
838 return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
839 FTy.getParamType(0)->isPointerTy());
840 case LibFunc::dunder_strtok_r:
841 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
Ahmed Bougachad765a822016-04-27 19:04:35 +0000842 case LibFunc::under_IO_putc:
843 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
844 case LibFunc::dunder_isoc99_scanf:
845 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
846 case LibFunc::stat64:
847 case LibFunc::lstat64:
848 case LibFunc::statvfs64:
849 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy() &&
850 FTy.getParamType(1)->isPointerTy());
851 case LibFunc::dunder_isoc99_sscanf:
852 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy() &&
853 FTy.getParamType(1)->isPointerTy());
854 case LibFunc::fopen64:
855 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
856 FTy.getParamType(0)->isPointerTy() &&
857 FTy.getParamType(1)->isPointerTy());
Ahmed Bougachad765a822016-04-27 19:04:35 +0000858 case LibFunc::tmpfile64:
859 return (FTy.getReturnType()->isPointerTy());
860 case LibFunc::fstat64:
861 case LibFunc::fstatvfs64:
862 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
863 case LibFunc::open64:
864 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
865 case LibFunc::gettimeofday:
866 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
867 FTy.getParamType(1)->isPointerTy());
868
869 case LibFunc::Znwj: // new(unsigned int);
870 case LibFunc::Znwm: // new(unsigned long);
871 case LibFunc::Znaj: // new[](unsigned int);
872 case LibFunc::Znam: // new[](unsigned long);
873 case LibFunc::msvc_new_int: // new(unsigned int);
874 case LibFunc::msvc_new_longlong: // new(unsigned long long);
875 case LibFunc::msvc_new_array_int: // new[](unsigned int);
876 case LibFunc::msvc_new_array_longlong: // new[](unsigned long long);
877 return (NumParams == 1);
878
879 case LibFunc::memset_pattern16:
880 return (!FTy.isVarArg() && NumParams == 3 &&
881 isa<PointerType>(FTy.getParamType(0)) &&
882 isa<PointerType>(FTy.getParamType(1)) &&
883 isa<IntegerType>(FTy.getParamType(2)));
884
885 // int __nvvm_reflect(const char *);
886 case LibFunc::nvvm_reflect:
887 return (NumParams == 1 && isa<PointerType>(FTy.getParamType(0)));
888
889 case LibFunc::sin:
890 case LibFunc::sinf:
891 case LibFunc::sinl:
892 case LibFunc::cos:
893 case LibFunc::cosf:
894 case LibFunc::cosl:
David Majnemerb62692e2016-06-15 16:47:23 +0000895 case LibFunc::tan:
896 case LibFunc::tanf:
897 case LibFunc::tanl:
Ahmed Bougachad765a822016-04-27 19:04:35 +0000898 case LibFunc::exp:
899 case LibFunc::expf:
900 case LibFunc::expl:
901 case LibFunc::exp2:
902 case LibFunc::exp2f:
903 case LibFunc::exp2l:
904 case LibFunc::log:
905 case LibFunc::logf:
906 case LibFunc::logl:
907 case LibFunc::log10:
908 case LibFunc::log10f:
909 case LibFunc::log10l:
910 case LibFunc::log2:
911 case LibFunc::log2f:
912 case LibFunc::log2l:
913 case LibFunc::fabs:
914 case LibFunc::fabsf:
915 case LibFunc::fabsl:
916 case LibFunc::floor:
917 case LibFunc::floorf:
918 case LibFunc::floorl:
919 case LibFunc::ceil:
920 case LibFunc::ceilf:
921 case LibFunc::ceill:
922 case LibFunc::trunc:
923 case LibFunc::truncf:
924 case LibFunc::truncl:
925 case LibFunc::rint:
926 case LibFunc::rintf:
927 case LibFunc::rintl:
928 case LibFunc::nearbyint:
929 case LibFunc::nearbyintf:
930 case LibFunc::nearbyintl:
931 case LibFunc::round:
932 case LibFunc::roundf:
933 case LibFunc::roundl:
934 case LibFunc::sqrt:
935 case LibFunc::sqrtf:
936 case LibFunc::sqrtl:
937 return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() &&
938 FTy.getReturnType() == FTy.getParamType(0));
939
940 case LibFunc::fmin:
941 case LibFunc::fminf:
942 case LibFunc::fminl:
943 case LibFunc::fmax:
944 case LibFunc::fmaxf:
945 case LibFunc::fmaxl:
946 case LibFunc::copysign:
947 case LibFunc::copysignf:
948 case LibFunc::copysignl:
949 case LibFunc::pow:
950 case LibFunc::powf:
951 case LibFunc::powl:
952 return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
953 FTy.getReturnType() == FTy.getParamType(0) &&
954 FTy.getReturnType() == FTy.getParamType(1));
955
956 case LibFunc::ffs:
957 case LibFunc::ffsl:
958 case LibFunc::ffsll:
959 case LibFunc::isdigit:
960 case LibFunc::isascii:
961 case LibFunc::toascii:
962 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
963 FTy.getParamType(0)->isIntegerTy());
964
965 case LibFunc::fls:
966 case LibFunc::flsl:
967 case LibFunc::flsll:
968 case LibFunc::abs:
969 case LibFunc::labs:
970 case LibFunc::llabs:
971 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() &&
972 FTy.getReturnType() == FTy.getParamType(0));
973
974 case LibFunc::cxa_atexit:
975 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() &&
976 FTy.getParamType(0)->isPointerTy() &&
977 FTy.getParamType(1)->isPointerTy() &&
978 FTy.getParamType(2)->isPointerTy());
979
980 case LibFunc::sinpi:
981 case LibFunc::cospi:
982 return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() &&
983 FTy.getReturnType() == FTy.getParamType(0));
984
985 case LibFunc::sinpif:
986 case LibFunc::cospif:
987 return (NumParams == 1 && FTy.getReturnType()->isFloatTy() &&
988 FTy.getReturnType() == FTy.getParamType(0));
989
990 default:
991 // Assume the other functions are correct.
992 // FIXME: It'd be really nice to cover them all.
993 return true;
994 }
995}
996
997bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
998 LibFunc::Func &F) const {
999 const DataLayout *DL =
1000 FDecl.getParent() ? &FDecl.getParent()->getDataLayout() : nullptr;
1001 return getLibFunc(FDecl.getName(), F) &&
1002 isValidProtoForLibFunc(*FDecl.getFunctionType(), F, DL);
1003}
1004
Chandler Carruthc0291862015-01-24 02:06:09 +00001005void TargetLibraryInfoImpl::disableAllFunctions() {
Chris Lattner1341df92011-02-18 22:34:03 +00001006 memset(AvailableArray, 0, sizeof(AvailableArray));
1007}
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001008
Michael Zolotukhine8f25512015-03-17 19:22:30 +00001009static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
1010 return std::strncmp(LHS.ScalarFnName, RHS.ScalarFnName,
1011 std::strlen(RHS.ScalarFnName)) < 0;
1012}
1013
1014static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
1015 return std::strncmp(LHS.VectorFnName, RHS.VectorFnName,
1016 std::strlen(RHS.VectorFnName)) < 0;
1017}
1018
1019static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
1020 return std::strncmp(LHS.ScalarFnName, S.data(), S.size()) < 0;
1021}
1022
1023static bool compareWithVectorFnName(const VecDesc &LHS, StringRef S) {
1024 return std::strncmp(LHS.VectorFnName, S.data(), S.size()) < 0;
1025}
1026
1027void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
1028 VectorDescs.insert(VectorDescs.end(), Fns.begin(), Fns.end());
1029 std::sort(VectorDescs.begin(), VectorDescs.end(), compareByScalarFnName);
1030
1031 ScalarDescs.insert(ScalarDescs.end(), Fns.begin(), Fns.end());
1032 std::sort(ScalarDescs.begin(), ScalarDescs.end(), compareByVectorFnName);
1033}
1034
Michael Zolotukhin6d8a2aa2015-03-17 19:50:55 +00001035void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
1036 enum VectorLibrary VecLib) {
1037 switch (VecLib) {
1038 case Accelerate: {
1039 const VecDesc VecFuncs[] = {
Michael Zolotukhinde63aac2015-05-07 17:11:51 +00001040 // Floating-Point Arithmetic and Auxiliary Functions
1041 {"ceilf", "vceilf", 4},
Michael Zolotukhin6d8a2aa2015-03-17 19:50:55 +00001042 {"fabsf", "vfabsf", 4},
1043 {"llvm.fabs.f32", "vfabsf", 4},
Michael Zolotukhinde63aac2015-05-07 17:11:51 +00001044 {"floorf", "vfloorf", 4},
1045 {"sqrtf", "vsqrtf", 4},
1046 {"llvm.sqrt.f32", "vsqrtf", 4},
1047
1048 // Exponential and Logarithmic Functions
1049 {"expf", "vexpf", 4},
1050 {"llvm.exp.f32", "vexpf", 4},
1051 {"expm1f", "vexpm1f", 4},
1052 {"logf", "vlogf", 4},
1053 {"llvm.log.f32", "vlogf", 4},
1054 {"log1pf", "vlog1pf", 4},
1055 {"log10f", "vlog10f", 4},
1056 {"llvm.log10.f32", "vlog10f", 4},
1057 {"logbf", "vlogbf", 4},
1058
1059 // Trigonometric Functions
1060 {"sinf", "vsinf", 4},
1061 {"llvm.sin.f32", "vsinf", 4},
1062 {"cosf", "vcosf", 4},
1063 {"llvm.cos.f32", "vcosf", 4},
1064 {"tanf", "vtanf", 4},
1065 {"asinf", "vasinf", 4},
1066 {"acosf", "vacosf", 4},
1067 {"atanf", "vatanf", 4},
1068
1069 // Hyperbolic Functions
1070 {"sinhf", "vsinhf", 4},
1071 {"coshf", "vcoshf", 4},
1072 {"tanhf", "vtanhf", 4},
1073 {"asinhf", "vasinhf", 4},
1074 {"acoshf", "vacoshf", 4},
1075 {"atanhf", "vatanhf", 4},
Michael Zolotukhin6d8a2aa2015-03-17 19:50:55 +00001076 };
1077 addVectorizableFunctions(VecFuncs);
1078 break;
1079 }
Matt Mastena6669a12016-07-29 16:42:44 +00001080 case SVML: {
1081 const VecDesc VecFuncs[] = {
1082 {"sin", "__svml_sin2", 2},
1083 {"sin", "__svml_sin4", 4},
1084 {"sin", "__svml_sin8", 8},
1085
1086 {"sinf", "__svml_sinf4", 4},
1087 {"sinf", "__svml_sinf8", 8},
1088 {"sinf", "__svml_sinf16", 16},
1089
1090 {"cos", "__svml_cos2", 2},
1091 {"cos", "__svml_cos4", 4},
1092 {"cos", "__svml_cos8", 8},
1093
1094 {"cosf", "__svml_cosf4", 4},
1095 {"cosf", "__svml_cosf8", 8},
1096 {"cosf", "__svml_cosf16", 16},
1097
1098 {"pow", "__svml_pow2", 2},
1099 {"pow", "__svml_pow4", 4},
1100 {"pow", "__svml_pow8", 8},
1101
1102 {"powf", "__svml_powf4", 4},
1103 {"powf", "__svml_powf8", 8},
1104 {"powf", "__svml_powf16", 16},
1105
1106 {"llvm.pow.f64", "__svml_pow2", 2},
1107 {"llvm.pow.f64", "__svml_pow4", 4},
1108 {"llvm.pow.f64", "__svml_pow8", 8},
1109
1110 {"llvm.pow.f32", "__svml_powf4", 4},
1111 {"llvm.pow.f32", "__svml_powf8", 8},
1112 {"llvm.pow.f32", "__svml_powf16", 16},
1113
1114 {"exp", "__svml_exp2", 2},
1115 {"exp", "__svml_exp4", 4},
1116 {"exp", "__svml_exp8", 8},
1117
1118 {"expf", "__svml_expf4", 4},
1119 {"expf", "__svml_expf8", 8},
1120 {"expf", "__svml_expf16", 16},
1121
1122 {"llvm.exp.f64", "__svml_exp2", 2},
1123 {"llvm.exp.f64", "__svml_exp4", 4},
1124 {"llvm.exp.f64", "__svml_exp8", 8},
1125
1126 {"llvm.exp.f32", "__svml_expf4", 4},
1127 {"llvm.exp.f32", "__svml_expf8", 8},
1128 {"llvm.exp.f32", "__svml_expf16", 16},
1129
1130 {"log", "__svml_log2", 2},
1131 {"log", "__svml_log4", 4},
1132 {"log", "__svml_log8", 8},
1133
1134 {"logf", "__svml_logf4", 4},
1135 {"logf", "__svml_logf8", 8},
1136 {"logf", "__svml_logf16", 16},
1137
1138 {"llvm.log.f64", "__svml_log2", 2},
1139 {"llvm.log.f64", "__svml_log4", 4},
1140 {"llvm.log.f64", "__svml_log8", 8},
1141
1142 {"llvm.log.f32", "__svml_logf4", 4},
1143 {"llvm.log.f32", "__svml_logf8", 8},
1144 {"llvm.log.f32", "__svml_logf16", 16},
1145 };
1146 addVectorizableFunctions(VecFuncs);
1147 break;
1148 }
Michael Zolotukhin6d8a2aa2015-03-17 19:50:55 +00001149 case NoLibrary:
1150 break;
1151 }
1152}
1153
Michael Zolotukhine8f25512015-03-17 19:22:30 +00001154bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
1155 funcName = sanitizeFunctionName(funcName);
1156 if (funcName.empty())
1157 return false;
1158
1159 std::vector<VecDesc>::const_iterator I = std::lower_bound(
1160 VectorDescs.begin(), VectorDescs.end(), funcName,
1161 compareWithScalarFnName);
1162 return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
1163}
1164
1165StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
1166 unsigned VF) const {
1167 F = sanitizeFunctionName(F);
1168 if (F.empty())
1169 return F;
1170 std::vector<VecDesc>::const_iterator I = std::lower_bound(
1171 VectorDescs.begin(), VectorDescs.end(), F, compareWithScalarFnName);
1172 while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
1173 if (I->VectorizationFactor == VF)
1174 return I->VectorFnName;
1175 ++I;
1176 }
1177 return StringRef();
1178}
1179
1180StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F,
1181 unsigned &VF) const {
1182 F = sanitizeFunctionName(F);
1183 if (F.empty())
1184 return F;
1185
1186 std::vector<VecDesc>::const_iterator I = std::lower_bound(
1187 ScalarDescs.begin(), ScalarDescs.end(), F, compareWithVectorFnName);
1188 if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F)
1189 return StringRef();
1190 VF = I->VectorizationFactor;
1191 return I->ScalarFnName;
1192}
1193
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001194TargetLibraryInfo TargetLibraryAnalysis::run(Module &M,
1195 ModuleAnalysisManager &) {
Chandler Carruthc0291862015-01-24 02:06:09 +00001196 if (PresetInfoImpl)
1197 return TargetLibraryInfo(*PresetInfoImpl);
1198
1199 return TargetLibraryInfo(lookupInfoImpl(Triple(M.getTargetTriple())));
1200}
1201
Chandler Carruth164a2aa62016-06-17 00:11:01 +00001202TargetLibraryInfo TargetLibraryAnalysis::run(Function &F,
1203 FunctionAnalysisManager &) {
Chandler Carruthc0291862015-01-24 02:06:09 +00001204 if (PresetInfoImpl)
1205 return TargetLibraryInfo(*PresetInfoImpl);
1206
1207 return TargetLibraryInfo(
1208 lookupInfoImpl(Triple(F.getParent()->getTargetTriple())));
1209}
1210
Benjamin Kramerc321e532016-06-08 19:09:22 +00001211TargetLibraryInfoImpl &TargetLibraryAnalysis::lookupInfoImpl(const Triple &T) {
Chandler Carruthc0291862015-01-24 02:06:09 +00001212 std::unique_ptr<TargetLibraryInfoImpl> &Impl =
1213 Impls[T.normalize()];
1214 if (!Impl)
1215 Impl.reset(new TargetLibraryInfoImpl(T));
1216
1217 return *Impl;
1218}
1219
1220
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001221TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
Chandler Carruthc0291862015-01-24 02:06:09 +00001222 : ImmutablePass(ID), TLIImpl(), TLI(TLIImpl) {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001223 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1224}
1225
1226TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
Chandler Carruthc0291862015-01-24 02:06:09 +00001227 : ImmutablePass(ID), TLIImpl(T), TLI(TLIImpl) {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001228 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1229}
1230
1231TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
Chandler Carruthc0291862015-01-24 02:06:09 +00001232 const TargetLibraryInfoImpl &TLIImpl)
1233 : ImmutablePass(ID), TLIImpl(TLIImpl), TLI(this->TLIImpl) {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001234 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1235}
1236
Chandler Carruthb4faf132016-03-11 10:22:49 +00001237char TargetLibraryAnalysis::PassID;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001238
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001239// Register the basic pass.
1240INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
1241 "Target Library Information", false, true)
1242char TargetLibraryInfoWrapperPass::ID = 0;
1243
1244void TargetLibraryInfoWrapperPass::anchor() {}