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