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