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 clEnumValN(TargetLibraryInfoImpl::SVML, "SVML", 27 "Intel SVML library"))); 28 29 StringRef const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] = { 30 #define TLI_DEFINE_STRING 31 #include "llvm/Analysis/TargetLibraryInfo.def" 32 }; 33 34 static bool hasSinCosPiStret(const Triple &T) { 35 // Only Darwin variants have _stret versions of combined trig functions. 36 if (!T.isOSDarwin()) 37 return false; 38 39 // The ABI is rather complicated on x86, so don't do anything special there. 40 if (T.getArch() == Triple::x86) 41 return false; 42 43 if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9)) 44 return false; 45 46 if (T.isiOS() && T.isOSVersionLT(7, 0)) 47 return false; 48 49 return true; 50 } 51 52 /// initialize - Initialize the set of available library functions based on the 53 /// specified target triple. This should be carefully written so that a missing 54 /// target triple gets a sane set of defaults. 55 static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, 56 ArrayRef<StringRef> StandardNames) { 57 // Verify that the StandardNames array is in alphabetical order. 58 assert(std::is_sorted(StandardNames.begin(), StandardNames.end(), 59 [](StringRef LHS, StringRef RHS) { 60 return LHS < RHS; 61 }) && 62 "TargetLibraryInfoImpl function names must be sorted"); 63 64 bool ShouldExtI32Param = false, ShouldExtI32Return = false, 65 ShouldSignExtI32Param = false; 66 // PowerPC64, Sparc64, SystemZ need signext/zeroext on i32 parameters and 67 // returns corresponding to C-level ints and unsigned ints. 68 if (T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le || 69 T.getArch() == Triple::sparcv9 || T.getArch() == Triple::systemz) { 70 ShouldExtI32Param = true; 71 ShouldExtI32Return = true; 72 } 73 // Mips, on the other hand, needs signext on i32 parameters corresponding 74 // to both signed and unsigned ints. 75 if (T.getArch() == Triple::mips || T.getArch() == Triple::mipsel || 76 T.getArch() == Triple::mips64 || T.getArch() == Triple::mips64el) { 77 ShouldSignExtI32Param = true; 78 } 79 TLI.setShouldExtI32Param(ShouldExtI32Param); 80 TLI.setShouldExtI32Return(ShouldExtI32Return); 81 TLI.setShouldSignExtI32Param(ShouldSignExtI32Param); 82 83 if (T.getArch() == Triple::r600 || 84 T.getArch() == Triple::amdgcn) { 85 TLI.setUnavailable(LibFunc_ldexp); 86 TLI.setUnavailable(LibFunc_ldexpf); 87 TLI.setUnavailable(LibFunc_ldexpl); 88 TLI.setUnavailable(LibFunc_exp10); 89 TLI.setUnavailable(LibFunc_exp10f); 90 TLI.setUnavailable(LibFunc_exp10l); 91 TLI.setUnavailable(LibFunc_log10); 92 TLI.setUnavailable(LibFunc_log10f); 93 TLI.setUnavailable(LibFunc_log10l); 94 } 95 96 // There are no library implementations of mempcy and memset for AMD gpus and 97 // these can be difficult to lower in the backend. 98 if (T.getArch() == Triple::r600 || 99 T.getArch() == Triple::amdgcn) { 100 TLI.setUnavailable(LibFunc_memcpy); 101 TLI.setUnavailable(LibFunc_memset); 102 TLI.setUnavailable(LibFunc_memset_pattern16); 103 return; 104 } 105 106 // memset_pattern16 is only available on iOS 3.0 and Mac OS X 10.5 and later. 107 // All versions of watchOS support it. 108 if (T.isMacOSX()) { 109 if (T.isMacOSXVersionLT(10, 5)) 110 TLI.setUnavailable(LibFunc_memset_pattern16); 111 } else if (T.isiOS()) { 112 if (T.isOSVersionLT(3, 0)) 113 TLI.setUnavailable(LibFunc_memset_pattern16); 114 } else if (!T.isWatchOS()) { 115 TLI.setUnavailable(LibFunc_memset_pattern16); 116 } 117 118 if (!hasSinCosPiStret(T)) { 119 TLI.setUnavailable(LibFunc_sinpi); 120 TLI.setUnavailable(LibFunc_sinpif); 121 TLI.setUnavailable(LibFunc_cospi); 122 TLI.setUnavailable(LibFunc_cospif); 123 TLI.setUnavailable(LibFunc_sincospi_stret); 124 TLI.setUnavailable(LibFunc_sincospif_stret); 125 } 126 127 if (T.isMacOSX() && T.getArch() == Triple::x86 && 128 !T.isMacOSXVersionLT(10, 7)) { 129 // x86-32 OSX has a scheme where fwrite and fputs (and some other functions 130 // we don't care about) have two versions; on recent OSX, the one we want 131 // has a $UNIX2003 suffix. The two implementations are identical except 132 // for the return value in some edge cases. However, we don't want to 133 // generate code that depends on the old symbols. 134 TLI.setAvailableWithName(LibFunc_fwrite, "fwrite$UNIX2003"); 135 TLI.setAvailableWithName(LibFunc_fputs, "fputs$UNIX2003"); 136 } 137 138 // iprintf and friends are only available on XCore and TCE. 139 if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce) { 140 TLI.setUnavailable(LibFunc_iprintf); 141 TLI.setUnavailable(LibFunc_siprintf); 142 TLI.setUnavailable(LibFunc_fiprintf); 143 } 144 145 if (T.isOSWindows() && !T.isOSCygMing()) { 146 // Win32 does not support long double 147 TLI.setUnavailable(LibFunc_acosl); 148 TLI.setUnavailable(LibFunc_asinl); 149 TLI.setUnavailable(LibFunc_atanl); 150 TLI.setUnavailable(LibFunc_atan2l); 151 TLI.setUnavailable(LibFunc_ceill); 152 TLI.setUnavailable(LibFunc_copysignl); 153 TLI.setUnavailable(LibFunc_cosl); 154 TLI.setUnavailable(LibFunc_coshl); 155 TLI.setUnavailable(LibFunc_expl); 156 TLI.setUnavailable(LibFunc_fabsf); // Win32 and Win64 both lack fabsf 157 TLI.setUnavailable(LibFunc_fabsl); 158 TLI.setUnavailable(LibFunc_floorl); 159 TLI.setUnavailable(LibFunc_fmaxl); 160 TLI.setUnavailable(LibFunc_fminl); 161 TLI.setUnavailable(LibFunc_fmodl); 162 TLI.setUnavailable(LibFunc_frexpl); 163 TLI.setUnavailable(LibFunc_ldexpf); 164 TLI.setUnavailable(LibFunc_ldexpl); 165 TLI.setUnavailable(LibFunc_logl); 166 TLI.setUnavailable(LibFunc_modfl); 167 TLI.setUnavailable(LibFunc_powl); 168 TLI.setUnavailable(LibFunc_sinl); 169 TLI.setUnavailable(LibFunc_sinhl); 170 TLI.setUnavailable(LibFunc_sqrtl); 171 TLI.setUnavailable(LibFunc_tanl); 172 TLI.setUnavailable(LibFunc_tanhl); 173 174 // Win32 only has C89 math 175 TLI.setUnavailable(LibFunc_acosh); 176 TLI.setUnavailable(LibFunc_acoshf); 177 TLI.setUnavailable(LibFunc_acoshl); 178 TLI.setUnavailable(LibFunc_asinh); 179 TLI.setUnavailable(LibFunc_asinhf); 180 TLI.setUnavailable(LibFunc_asinhl); 181 TLI.setUnavailable(LibFunc_atanh); 182 TLI.setUnavailable(LibFunc_atanhf); 183 TLI.setUnavailable(LibFunc_atanhl); 184 TLI.setUnavailable(LibFunc_cbrt); 185 TLI.setUnavailable(LibFunc_cbrtf); 186 TLI.setUnavailable(LibFunc_cbrtl); 187 TLI.setUnavailable(LibFunc_exp2); 188 TLI.setUnavailable(LibFunc_exp2f); 189 TLI.setUnavailable(LibFunc_exp2l); 190 TLI.setUnavailable(LibFunc_expm1); 191 TLI.setUnavailable(LibFunc_expm1f); 192 TLI.setUnavailable(LibFunc_expm1l); 193 TLI.setUnavailable(LibFunc_log2); 194 TLI.setUnavailable(LibFunc_log2f); 195 TLI.setUnavailable(LibFunc_log2l); 196 TLI.setUnavailable(LibFunc_log1p); 197 TLI.setUnavailable(LibFunc_log1pf); 198 TLI.setUnavailable(LibFunc_log1pl); 199 TLI.setUnavailable(LibFunc_logb); 200 TLI.setUnavailable(LibFunc_logbf); 201 TLI.setUnavailable(LibFunc_logbl); 202 TLI.setUnavailable(LibFunc_nearbyint); 203 TLI.setUnavailable(LibFunc_nearbyintf); 204 TLI.setUnavailable(LibFunc_nearbyintl); 205 TLI.setUnavailable(LibFunc_rint); 206 TLI.setUnavailable(LibFunc_rintf); 207 TLI.setUnavailable(LibFunc_rintl); 208 TLI.setUnavailable(LibFunc_round); 209 TLI.setUnavailable(LibFunc_roundf); 210 TLI.setUnavailable(LibFunc_roundl); 211 TLI.setUnavailable(LibFunc_trunc); 212 TLI.setUnavailable(LibFunc_truncf); 213 TLI.setUnavailable(LibFunc_truncl); 214 215 // Win32 provides some C99 math with mangled names 216 TLI.setAvailableWithName(LibFunc_copysign, "_copysign"); 217 218 if (T.getArch() == Triple::x86) { 219 // Win32 on x86 implements single-precision math functions as macros 220 TLI.setUnavailable(LibFunc_acosf); 221 TLI.setUnavailable(LibFunc_asinf); 222 TLI.setUnavailable(LibFunc_atanf); 223 TLI.setUnavailable(LibFunc_atan2f); 224 TLI.setUnavailable(LibFunc_ceilf); 225 TLI.setUnavailable(LibFunc_copysignf); 226 TLI.setUnavailable(LibFunc_cosf); 227 TLI.setUnavailable(LibFunc_coshf); 228 TLI.setUnavailable(LibFunc_expf); 229 TLI.setUnavailable(LibFunc_floorf); 230 TLI.setUnavailable(LibFunc_fminf); 231 TLI.setUnavailable(LibFunc_fmaxf); 232 TLI.setUnavailable(LibFunc_fmodf); 233 TLI.setUnavailable(LibFunc_logf); 234 TLI.setUnavailable(LibFunc_log10f); 235 TLI.setUnavailable(LibFunc_modff); 236 TLI.setUnavailable(LibFunc_powf); 237 TLI.setUnavailable(LibFunc_sinf); 238 TLI.setUnavailable(LibFunc_sinhf); 239 TLI.setUnavailable(LibFunc_sqrtf); 240 TLI.setUnavailable(LibFunc_tanf); 241 TLI.setUnavailable(LibFunc_tanhf); 242 } 243 244 // Win32 does *not* provide provide these functions, but they are 245 // generally available on POSIX-compliant systems: 246 TLI.setUnavailable(LibFunc_access); 247 TLI.setUnavailable(LibFunc_bcmp); 248 TLI.setUnavailable(LibFunc_bcopy); 249 TLI.setUnavailable(LibFunc_bzero); 250 TLI.setUnavailable(LibFunc_chmod); 251 TLI.setUnavailable(LibFunc_chown); 252 TLI.setUnavailable(LibFunc_closedir); 253 TLI.setUnavailable(LibFunc_ctermid); 254 TLI.setUnavailable(LibFunc_fdopen); 255 TLI.setUnavailable(LibFunc_ffs); 256 TLI.setUnavailable(LibFunc_fileno); 257 TLI.setUnavailable(LibFunc_flockfile); 258 TLI.setUnavailable(LibFunc_fseeko); 259 TLI.setUnavailable(LibFunc_fstat); 260 TLI.setUnavailable(LibFunc_fstatvfs); 261 TLI.setUnavailable(LibFunc_ftello); 262 TLI.setUnavailable(LibFunc_ftrylockfile); 263 TLI.setUnavailable(LibFunc_funlockfile); 264 TLI.setUnavailable(LibFunc_getc_unlocked); 265 TLI.setUnavailable(LibFunc_getitimer); 266 TLI.setUnavailable(LibFunc_getlogin_r); 267 TLI.setUnavailable(LibFunc_getpwnam); 268 TLI.setUnavailable(LibFunc_gettimeofday); 269 TLI.setUnavailable(LibFunc_htonl); 270 TLI.setUnavailable(LibFunc_htons); 271 TLI.setUnavailable(LibFunc_lchown); 272 TLI.setUnavailable(LibFunc_lstat); 273 TLI.setUnavailable(LibFunc_memccpy); 274 TLI.setUnavailable(LibFunc_mkdir); 275 TLI.setUnavailable(LibFunc_ntohl); 276 TLI.setUnavailable(LibFunc_ntohs); 277 TLI.setUnavailable(LibFunc_open); 278 TLI.setUnavailable(LibFunc_opendir); 279 TLI.setUnavailable(LibFunc_pclose); 280 TLI.setUnavailable(LibFunc_popen); 281 TLI.setUnavailable(LibFunc_pread); 282 TLI.setUnavailable(LibFunc_pwrite); 283 TLI.setUnavailable(LibFunc_read); 284 TLI.setUnavailable(LibFunc_readlink); 285 TLI.setUnavailable(LibFunc_realpath); 286 TLI.setUnavailable(LibFunc_rmdir); 287 TLI.setUnavailable(LibFunc_setitimer); 288 TLI.setUnavailable(LibFunc_stat); 289 TLI.setUnavailable(LibFunc_statvfs); 290 TLI.setUnavailable(LibFunc_stpcpy); 291 TLI.setUnavailable(LibFunc_stpncpy); 292 TLI.setUnavailable(LibFunc_strcasecmp); 293 TLI.setUnavailable(LibFunc_strncasecmp); 294 TLI.setUnavailable(LibFunc_times); 295 TLI.setUnavailable(LibFunc_uname); 296 TLI.setUnavailable(LibFunc_unlink); 297 TLI.setUnavailable(LibFunc_unsetenv); 298 TLI.setUnavailable(LibFunc_utime); 299 TLI.setUnavailable(LibFunc_utimes); 300 TLI.setUnavailable(LibFunc_write); 301 302 // Win32 does *not* provide provide these functions, but they are 303 // specified by C99: 304 TLI.setUnavailable(LibFunc_atoll); 305 TLI.setUnavailable(LibFunc_frexpf); 306 TLI.setUnavailable(LibFunc_llabs); 307 } 308 309 switch (T.getOS()) { 310 case Triple::MacOSX: 311 // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0 312 // and their names are __exp10 and __exp10f. exp10l is not available on 313 // OS X or iOS. 314 TLI.setUnavailable(LibFunc_exp10l); 315 if (T.isMacOSXVersionLT(10, 9)) { 316 TLI.setUnavailable(LibFunc_exp10); 317 TLI.setUnavailable(LibFunc_exp10f); 318 } else { 319 TLI.setAvailableWithName(LibFunc_exp10, "__exp10"); 320 TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f"); 321 } 322 break; 323 case Triple::IOS: 324 case Triple::TvOS: 325 case Triple::WatchOS: 326 TLI.setUnavailable(LibFunc_exp10l); 327 if (!T.isWatchOS() && (T.isOSVersionLT(7, 0) || 328 (T.isOSVersionLT(9, 0) && 329 (T.getArch() == Triple::x86 || 330 T.getArch() == Triple::x86_64)))) { 331 TLI.setUnavailable(LibFunc_exp10); 332 TLI.setUnavailable(LibFunc_exp10f); 333 } else { 334 TLI.setAvailableWithName(LibFunc_exp10, "__exp10"); 335 TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f"); 336 } 337 break; 338 case Triple::Linux: 339 // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely 340 // buggy prior to glibc version 2.18. Until this version is widely deployed 341 // or we have a reasonable detection strategy, we cannot use exp10 reliably 342 // on Linux. 343 // 344 // Fall through to disable all of them. 345 LLVM_FALLTHROUGH; 346 default: 347 TLI.setUnavailable(LibFunc_exp10); 348 TLI.setUnavailable(LibFunc_exp10f); 349 TLI.setUnavailable(LibFunc_exp10l); 350 } 351 352 // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and 353 // Linux (GLIBC): 354 // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html 355 // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c 356 // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html 357 switch (T.getOS()) { 358 case Triple::Darwin: 359 case Triple::MacOSX: 360 case Triple::IOS: 361 case Triple::TvOS: 362 case Triple::WatchOS: 363 case Triple::FreeBSD: 364 case Triple::Linux: 365 break; 366 default: 367 TLI.setUnavailable(LibFunc_ffsl); 368 } 369 370 // ffsll is available on at least FreeBSD and Linux (GLIBC): 371 // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c 372 // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html 373 switch (T.getOS()) { 374 case Triple::Darwin: 375 case Triple::MacOSX: 376 case Triple::IOS: 377 case Triple::TvOS: 378 case Triple::WatchOS: 379 case Triple::FreeBSD: 380 case Triple::Linux: 381 break; 382 default: 383 TLI.setUnavailable(LibFunc_ffsll); 384 } 385 386 // The following functions are available on at least FreeBSD: 387 // http://svn.freebsd.org/base/head/lib/libc/string/fls.c 388 // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c 389 // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c 390 if (!T.isOSFreeBSD()) { 391 TLI.setUnavailable(LibFunc_fls); 392 TLI.setUnavailable(LibFunc_flsl); 393 TLI.setUnavailable(LibFunc_flsll); 394 } 395 396 // The following functions are available on at least Linux: 397 if (!T.isOSLinux()) { 398 TLI.setUnavailable(LibFunc_dunder_strdup); 399 TLI.setUnavailable(LibFunc_dunder_strtok_r); 400 TLI.setUnavailable(LibFunc_dunder_isoc99_scanf); 401 TLI.setUnavailable(LibFunc_dunder_isoc99_sscanf); 402 TLI.setUnavailable(LibFunc_under_IO_getc); 403 TLI.setUnavailable(LibFunc_under_IO_putc); 404 TLI.setUnavailable(LibFunc_memalign); 405 TLI.setUnavailable(LibFunc_fopen64); 406 TLI.setUnavailable(LibFunc_fseeko64); 407 TLI.setUnavailable(LibFunc_fstat64); 408 TLI.setUnavailable(LibFunc_fstatvfs64); 409 TLI.setUnavailable(LibFunc_ftello64); 410 TLI.setUnavailable(LibFunc_lstat64); 411 TLI.setUnavailable(LibFunc_open64); 412 TLI.setUnavailable(LibFunc_stat64); 413 TLI.setUnavailable(LibFunc_statvfs64); 414 TLI.setUnavailable(LibFunc_tmpfile64); 415 } 416 417 // As currently implemented in clang, NVPTX code has no standard library to 418 // speak of. Headers provide a standard-ish library implementation, but many 419 // of the signatures are wrong -- for example, many libm functions are not 420 // extern "C". 421 // 422 // libdevice, an IR library provided by nvidia, is linked in by the front-end, 423 // but only used functions are provided to llvm. Moreover, most of the 424 // functions in libdevice don't map precisely to standard library functions. 425 // 426 // FIXME: Having no standard library prevents e.g. many fastmath 427 // optimizations, so this situation should be fixed. 428 if (T.isNVPTX()) { 429 TLI.disableAllFunctions(); 430 TLI.setAvailable(LibFunc_nvvm_reflect); 431 } else { 432 TLI.setUnavailable(LibFunc_nvvm_reflect); 433 } 434 435 TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary); 436 } 437 438 TargetLibraryInfoImpl::TargetLibraryInfoImpl() { 439 // Default to everything being available. 440 memset(AvailableArray, -1, sizeof(AvailableArray)); 441 442 initialize(*this, Triple(), StandardNames); 443 } 444 445 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) { 446 // Default to everything being available. 447 memset(AvailableArray, -1, sizeof(AvailableArray)); 448 449 initialize(*this, T, StandardNames); 450 } 451 452 TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI) 453 : CustomNames(TLI.CustomNames), ShouldExtI32Param(TLI.ShouldExtI32Param), 454 ShouldExtI32Return(TLI.ShouldExtI32Return), 455 ShouldSignExtI32Param(TLI.ShouldSignExtI32Param) { 456 memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray)); 457 VectorDescs = TLI.VectorDescs; 458 ScalarDescs = TLI.ScalarDescs; 459 } 460 461 TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI) 462 : CustomNames(std::move(TLI.CustomNames)), 463 ShouldExtI32Param(TLI.ShouldExtI32Param), 464 ShouldExtI32Return(TLI.ShouldExtI32Return), 465 ShouldSignExtI32Param(TLI.ShouldSignExtI32Param) { 466 std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray), 467 AvailableArray); 468 VectorDescs = TLI.VectorDescs; 469 ScalarDescs = TLI.ScalarDescs; 470 } 471 472 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) { 473 CustomNames = TLI.CustomNames; 474 ShouldExtI32Param = TLI.ShouldExtI32Param; 475 ShouldExtI32Return = TLI.ShouldExtI32Return; 476 ShouldSignExtI32Param = TLI.ShouldSignExtI32Param; 477 memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray)); 478 return *this; 479 } 480 481 TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) { 482 CustomNames = std::move(TLI.CustomNames); 483 ShouldExtI32Param = TLI.ShouldExtI32Param; 484 ShouldExtI32Return = TLI.ShouldExtI32Return; 485 ShouldSignExtI32Param = TLI.ShouldSignExtI32Param; 486 std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray), 487 AvailableArray); 488 return *this; 489 } 490 491 static StringRef sanitizeFunctionName(StringRef funcName) { 492 // Filter out empty names and names containing null bytes, those can't be in 493 // our table. 494 if (funcName.empty() || funcName.find('\0') != StringRef::npos) 495 return StringRef(); 496 497 // Check for \01 prefix that is used to mangle __asm declarations and 498 // strip it if present. 499 return GlobalValue::getRealLinkageName(funcName); 500 } 501 502 bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName, 503 LibFunc &F) const { 504 StringRef const *Start = &StandardNames[0]; 505 StringRef const *End = &StandardNames[NumLibFuncs]; 506 507 funcName = sanitizeFunctionName(funcName); 508 if (funcName.empty()) 509 return false; 510 511 StringRef const *I = std::lower_bound( 512 Start, End, funcName, [](StringRef LHS, StringRef RHS) { 513 return LHS < RHS; 514 }); 515 if (I != End && *I == funcName) { 516 F = (LibFunc)(I - Start); 517 return true; 518 } 519 return false; 520 } 521 522 bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy, 523 LibFunc F, 524 const DataLayout *DL) const { 525 LLVMContext &Ctx = FTy.getContext(); 526 Type *PCharTy = Type::getInt8PtrTy(Ctx); 527 Type *SizeTTy = DL ? DL->getIntPtrType(Ctx, /*AS=*/0) : nullptr; 528 auto IsSizeTTy = [SizeTTy](Type *Ty) { 529 return SizeTTy ? Ty == SizeTTy : Ty->isIntegerTy(); 530 }; 531 unsigned NumParams = FTy.getNumParams(); 532 533 switch (F) { 534 case LibFunc_strlen: 535 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() && 536 FTy.getReturnType()->isIntegerTy()); 537 538 case LibFunc_strchr: 539 case LibFunc_strrchr: 540 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 541 FTy.getParamType(0) == FTy.getReturnType() && 542 FTy.getParamType(1)->isIntegerTy()); 543 544 case LibFunc_strtol: 545 case LibFunc_strtod: 546 case LibFunc_strtof: 547 case LibFunc_strtoul: 548 case LibFunc_strtoll: 549 case LibFunc_strtold: 550 case LibFunc_strtoull: 551 return ((NumParams == 2 || NumParams == 3) && 552 FTy.getParamType(0)->isPointerTy() && 553 FTy.getParamType(1)->isPointerTy()); 554 case LibFunc_strcat: 555 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 556 FTy.getParamType(0) == FTy.getReturnType() && 557 FTy.getParamType(1) == FTy.getReturnType()); 558 559 case LibFunc_strncat: 560 return (NumParams == 3 && FTy.getReturnType()->isPointerTy() && 561 FTy.getParamType(0) == FTy.getReturnType() && 562 FTy.getParamType(1) == FTy.getReturnType() && 563 FTy.getParamType(2)->isIntegerTy()); 564 565 case LibFunc_strcpy_chk: 566 case LibFunc_stpcpy_chk: 567 --NumParams; 568 if (!IsSizeTTy(FTy.getParamType(NumParams))) 569 return false; 570 LLVM_FALLTHROUGH; 571 case LibFunc_strcpy: 572 case LibFunc_stpcpy: 573 return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(0) && 574 FTy.getParamType(0) == FTy.getParamType(1) && 575 FTy.getParamType(0) == PCharTy); 576 577 case LibFunc_strncpy_chk: 578 case LibFunc_stpncpy_chk: 579 --NumParams; 580 if (!IsSizeTTy(FTy.getParamType(NumParams))) 581 return false; 582 LLVM_FALLTHROUGH; 583 case LibFunc_strncpy: 584 case LibFunc_stpncpy: 585 return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) && 586 FTy.getParamType(0) == FTy.getParamType(1) && 587 FTy.getParamType(0) == PCharTy && 588 FTy.getParamType(2)->isIntegerTy()); 589 590 case LibFunc_strxfrm: 591 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() && 592 FTy.getParamType(1)->isPointerTy()); 593 594 case LibFunc_strcmp: 595 return (NumParams == 2 && FTy.getReturnType()->isIntegerTy(32) && 596 FTy.getParamType(0)->isPointerTy() && 597 FTy.getParamType(0) == FTy.getParamType(1)); 598 599 case LibFunc_strncmp: 600 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) && 601 FTy.getParamType(0)->isPointerTy() && 602 FTy.getParamType(0) == FTy.getParamType(1) && 603 FTy.getParamType(2)->isIntegerTy()); 604 605 case LibFunc_strspn: 606 case LibFunc_strcspn: 607 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 608 FTy.getParamType(0) == FTy.getParamType(1) && 609 FTy.getReturnType()->isIntegerTy()); 610 611 case LibFunc_strcoll: 612 case LibFunc_strcasecmp: 613 case LibFunc_strncasecmp: 614 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 615 FTy.getParamType(1)->isPointerTy()); 616 617 case LibFunc_strstr: 618 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 619 FTy.getParamType(0)->isPointerTy() && 620 FTy.getParamType(1)->isPointerTy()); 621 622 case LibFunc_strpbrk: 623 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 624 FTy.getReturnType() == FTy.getParamType(0) && 625 FTy.getParamType(0) == FTy.getParamType(1)); 626 627 case LibFunc_strtok: 628 case LibFunc_strtok_r: 629 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy()); 630 case LibFunc_scanf: 631 case LibFunc_setbuf: 632 case LibFunc_setvbuf: 633 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy()); 634 case LibFunc_strdup: 635 case LibFunc_strndup: 636 return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() && 637 FTy.getParamType(0)->isPointerTy()); 638 case LibFunc_sscanf: 639 case LibFunc_stat: 640 case LibFunc_statvfs: 641 case LibFunc_siprintf: 642 case LibFunc_sprintf: 643 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 644 FTy.getParamType(1)->isPointerTy()); 645 case LibFunc_snprintf: 646 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() && 647 FTy.getParamType(2)->isPointerTy()); 648 case LibFunc_setitimer: 649 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() && 650 FTy.getParamType(2)->isPointerTy()); 651 case LibFunc_system: 652 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy()); 653 case LibFunc_malloc: 654 return (NumParams == 1 && FTy.getReturnType()->isPointerTy()); 655 case LibFunc_memcmp: 656 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) && 657 FTy.getParamType(0)->isPointerTy() && 658 FTy.getParamType(1)->isPointerTy()); 659 660 case LibFunc_memchr: 661 case LibFunc_memrchr: 662 return (NumParams == 3 && FTy.getReturnType()->isPointerTy() && 663 FTy.getReturnType() == FTy.getParamType(0) && 664 FTy.getParamType(1)->isIntegerTy(32) && 665 IsSizeTTy(FTy.getParamType(2))); 666 case LibFunc_modf: 667 case LibFunc_modff: 668 case LibFunc_modfl: 669 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy()); 670 671 case LibFunc_memcpy_chk: 672 case LibFunc_memmove_chk: 673 --NumParams; 674 if (!IsSizeTTy(FTy.getParamType(NumParams))) 675 return false; 676 LLVM_FALLTHROUGH; 677 case LibFunc_memcpy: 678 case LibFunc_mempcpy: 679 case LibFunc_memmove: 680 return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) && 681 FTy.getParamType(0)->isPointerTy() && 682 FTy.getParamType(1)->isPointerTy() && 683 IsSizeTTy(FTy.getParamType(2))); 684 685 case LibFunc_memset_chk: 686 --NumParams; 687 if (!IsSizeTTy(FTy.getParamType(NumParams))) 688 return false; 689 LLVM_FALLTHROUGH; 690 case LibFunc_memset: 691 return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) && 692 FTy.getParamType(0)->isPointerTy() && 693 FTy.getParamType(1)->isIntegerTy() && 694 IsSizeTTy(FTy.getParamType(2))); 695 696 case LibFunc_memccpy: 697 return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy()); 698 case LibFunc_memalign: 699 return (FTy.getReturnType()->isPointerTy()); 700 case LibFunc_realloc: 701 case LibFunc_reallocf: 702 return (NumParams == 2 && FTy.getReturnType() == PCharTy && 703 FTy.getParamType(0) == FTy.getReturnType() && 704 IsSizeTTy(FTy.getParamType(1))); 705 case LibFunc_read: 706 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy()); 707 case LibFunc_rewind: 708 case LibFunc_rmdir: 709 case LibFunc_remove: 710 case LibFunc_realpath: 711 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy()); 712 case LibFunc_rename: 713 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 714 FTy.getParamType(1)->isPointerTy()); 715 case LibFunc_readlink: 716 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 717 FTy.getParamType(1)->isPointerTy()); 718 case LibFunc_write: 719 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy()); 720 case LibFunc_bcopy: 721 case LibFunc_bcmp: 722 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() && 723 FTy.getParamType(1)->isPointerTy()); 724 case LibFunc_bzero: 725 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy()); 726 case LibFunc_calloc: 727 return (NumParams == 2 && FTy.getReturnType()->isPointerTy()); 728 729 case LibFunc_atof: 730 case LibFunc_atoi: 731 case LibFunc_atol: 732 case LibFunc_atoll: 733 case LibFunc_ferror: 734 case LibFunc_getenv: 735 case LibFunc_getpwnam: 736 case LibFunc_iprintf: 737 case LibFunc_pclose: 738 case LibFunc_perror: 739 case LibFunc_printf: 740 case LibFunc_puts: 741 case LibFunc_uname: 742 case LibFunc_under_IO_getc: 743 case LibFunc_unlink: 744 case LibFunc_unsetenv: 745 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy()); 746 747 case LibFunc_access: 748 case LibFunc_chmod: 749 case LibFunc_chown: 750 case LibFunc_clearerr: 751 case LibFunc_closedir: 752 case LibFunc_ctermid: 753 case LibFunc_fclose: 754 case LibFunc_feof: 755 case LibFunc_fflush: 756 case LibFunc_fgetc: 757 case LibFunc_fileno: 758 case LibFunc_flockfile: 759 case LibFunc_free: 760 case LibFunc_fseek: 761 case LibFunc_fseeko64: 762 case LibFunc_fseeko: 763 case LibFunc_fsetpos: 764 case LibFunc_ftell: 765 case LibFunc_ftello64: 766 case LibFunc_ftello: 767 case LibFunc_ftrylockfile: 768 case LibFunc_funlockfile: 769 case LibFunc_getc: 770 case LibFunc_getc_unlocked: 771 case LibFunc_getlogin_r: 772 case LibFunc_mkdir: 773 case LibFunc_mktime: 774 case LibFunc_times: 775 return (NumParams != 0 && FTy.getParamType(0)->isPointerTy()); 776 777 case LibFunc_fopen: 778 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 779 FTy.getParamType(0)->isPointerTy() && 780 FTy.getParamType(1)->isPointerTy()); 781 case LibFunc_fdopen: 782 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 783 FTy.getParamType(1)->isPointerTy()); 784 case LibFunc_fputc: 785 case LibFunc_fstat: 786 case LibFunc_frexp: 787 case LibFunc_frexpf: 788 case LibFunc_frexpl: 789 case LibFunc_fstatvfs: 790 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 791 case LibFunc_fgets: 792 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() && 793 FTy.getParamType(2)->isPointerTy()); 794 case LibFunc_fread: 795 return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() && 796 FTy.getParamType(3)->isPointerTy()); 797 case LibFunc_fwrite: 798 return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() && 799 FTy.getParamType(0)->isPointerTy() && 800 FTy.getParamType(1)->isIntegerTy() && 801 FTy.getParamType(2)->isIntegerTy() && 802 FTy.getParamType(3)->isPointerTy()); 803 case LibFunc_fputs: 804 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 805 FTy.getParamType(1)->isPointerTy()); 806 case LibFunc_fscanf: 807 case LibFunc_fiprintf: 808 case LibFunc_fprintf: 809 return (NumParams >= 2 && FTy.getReturnType()->isIntegerTy() && 810 FTy.getParamType(0)->isPointerTy() && 811 FTy.getParamType(1)->isPointerTy()); 812 case LibFunc_fgetpos: 813 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 814 FTy.getParamType(1)->isPointerTy()); 815 case LibFunc_getchar: 816 return (NumParams == 0 && FTy.getReturnType()->isIntegerTy()); 817 case LibFunc_gets: 818 return (NumParams == 1 && FTy.getParamType(0) == PCharTy); 819 case LibFunc_getitimer: 820 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 821 case LibFunc_ungetc: 822 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 823 case LibFunc_utime: 824 case LibFunc_utimes: 825 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 826 FTy.getParamType(1)->isPointerTy()); 827 case LibFunc_putc: 828 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 829 case LibFunc_pread: 830 case LibFunc_pwrite: 831 return (NumParams == 4 && FTy.getParamType(1)->isPointerTy()); 832 case LibFunc_popen: 833 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 834 FTy.getParamType(0)->isPointerTy() && 835 FTy.getParamType(1)->isPointerTy()); 836 case LibFunc_vscanf: 837 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 838 case LibFunc_vsscanf: 839 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() && 840 FTy.getParamType(2)->isPointerTy()); 841 case LibFunc_vfscanf: 842 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() && 843 FTy.getParamType(2)->isPointerTy()); 844 case LibFunc_valloc: 845 return (FTy.getReturnType()->isPointerTy()); 846 case LibFunc_vprintf: 847 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy()); 848 case LibFunc_vfprintf: 849 case LibFunc_vsprintf: 850 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() && 851 FTy.getParamType(1)->isPointerTy()); 852 case LibFunc_vsnprintf: 853 return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() && 854 FTy.getParamType(2)->isPointerTy()); 855 case LibFunc_open: 856 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy()); 857 case LibFunc_opendir: 858 return (NumParams == 1 && FTy.getReturnType()->isPointerTy() && 859 FTy.getParamType(0)->isPointerTy()); 860 case LibFunc_tmpfile: 861 return (FTy.getReturnType()->isPointerTy()); 862 case LibFunc_htonl: 863 case LibFunc_ntohl: 864 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) && 865 FTy.getReturnType() == FTy.getParamType(0)); 866 case LibFunc_htons: 867 case LibFunc_ntohs: 868 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(16) && 869 FTy.getReturnType() == FTy.getParamType(0)); 870 case LibFunc_lstat: 871 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 872 FTy.getParamType(1)->isPointerTy()); 873 case LibFunc_lchown: 874 return (NumParams == 3 && FTy.getParamType(0)->isPointerTy()); 875 case LibFunc_qsort: 876 return (NumParams == 4 && FTy.getParamType(3)->isPointerTy()); 877 case LibFunc_dunder_strdup: 878 case LibFunc_dunder_strndup: 879 return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() && 880 FTy.getParamType(0)->isPointerTy()); 881 case LibFunc_dunder_strtok_r: 882 return (NumParams == 3 && FTy.getParamType(1)->isPointerTy()); 883 case LibFunc_under_IO_putc: 884 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 885 case LibFunc_dunder_isoc99_scanf: 886 return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy()); 887 case LibFunc_stat64: 888 case LibFunc_lstat64: 889 case LibFunc_statvfs64: 890 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 891 FTy.getParamType(1)->isPointerTy()); 892 case LibFunc_dunder_isoc99_sscanf: 893 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() && 894 FTy.getParamType(1)->isPointerTy()); 895 case LibFunc_fopen64: 896 return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && 897 FTy.getParamType(0)->isPointerTy() && 898 FTy.getParamType(1)->isPointerTy()); 899 case LibFunc_tmpfile64: 900 return (FTy.getReturnType()->isPointerTy()); 901 case LibFunc_fstat64: 902 case LibFunc_fstatvfs64: 903 return (NumParams == 2 && FTy.getParamType(1)->isPointerTy()); 904 case LibFunc_open64: 905 return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy()); 906 case LibFunc_gettimeofday: 907 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && 908 FTy.getParamType(1)->isPointerTy()); 909 910 // new(unsigned int); 911 case LibFunc_Znwj: 912 // new(unsigned long); 913 case LibFunc_Znwm: 914 // new[](unsigned int); 915 case LibFunc_Znaj: 916 // new[](unsigned long); 917 case LibFunc_Znam: 918 // new(unsigned int); 919 case LibFunc_msvc_new_int: 920 // new(unsigned long long); 921 case LibFunc_msvc_new_longlong: 922 // new[](unsigned int); 923 case LibFunc_msvc_new_array_int: 924 // new[](unsigned long long); 925 case LibFunc_msvc_new_array_longlong: 926 return (NumParams == 1 && FTy.getReturnType()->isPointerTy()); 927 928 // new(unsigned int, nothrow); 929 case LibFunc_ZnwjRKSt9nothrow_t: 930 // new(unsigned long, nothrow); 931 case LibFunc_ZnwmRKSt9nothrow_t: 932 // new[](unsigned int, nothrow); 933 case LibFunc_ZnajRKSt9nothrow_t: 934 // new[](unsigned long, nothrow); 935 case LibFunc_ZnamRKSt9nothrow_t: 936 // new(unsigned int, nothrow); 937 case LibFunc_msvc_new_int_nothrow: 938 // new(unsigned long long, nothrow); 939 case LibFunc_msvc_new_longlong_nothrow: 940 // new[](unsigned int, nothrow); 941 case LibFunc_msvc_new_array_int_nothrow: 942 // new[](unsigned long long, nothrow); 943 case LibFunc_msvc_new_array_longlong_nothrow: 944 return (NumParams == 2 && FTy.getReturnType()->isPointerTy()); 945 946 // void operator delete[](void*); 947 case LibFunc_ZdaPv: 948 // void operator delete(void*); 949 case LibFunc_ZdlPv: 950 // void operator delete[](void*); 951 case LibFunc_msvc_delete_array_ptr32: 952 // void operator delete[](void*); 953 case LibFunc_msvc_delete_array_ptr64: 954 // void operator delete(void*); 955 case LibFunc_msvc_delete_ptr32: 956 // void operator delete(void*); 957 case LibFunc_msvc_delete_ptr64: 958 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy()); 959 960 // void operator delete[](void*, nothrow); 961 case LibFunc_ZdaPvRKSt9nothrow_t: 962 // void operator delete[](void*, unsigned int); 963 case LibFunc_ZdaPvj: 964 // void operator delete[](void*, unsigned long); 965 case LibFunc_ZdaPvm: 966 // void operator delete(void*, nothrow); 967 case LibFunc_ZdlPvRKSt9nothrow_t: 968 // void operator delete(void*, unsigned int); 969 case LibFunc_ZdlPvj: 970 // void operator delete(void*, unsigned long); 971 case LibFunc_ZdlPvm: 972 // void operator delete[](void*, unsigned int); 973 case LibFunc_msvc_delete_array_ptr32_int: 974 // void operator delete[](void*, nothrow); 975 case LibFunc_msvc_delete_array_ptr32_nothrow: 976 // void operator delete[](void*, unsigned long long); 977 case LibFunc_msvc_delete_array_ptr64_longlong: 978 // void operator delete[](void*, nothrow); 979 case LibFunc_msvc_delete_array_ptr64_nothrow: 980 // void operator delete(void*, unsigned int); 981 case LibFunc_msvc_delete_ptr32_int: 982 // void operator delete(void*, nothrow); 983 case LibFunc_msvc_delete_ptr32_nothrow: 984 // void operator delete(void*, unsigned long long); 985 case LibFunc_msvc_delete_ptr64_longlong: 986 // void operator delete(void*, nothrow); 987 case LibFunc_msvc_delete_ptr64_nothrow: 988 return (NumParams == 2 && FTy.getParamType(0)->isPointerTy()); 989 990 case LibFunc_memset_pattern16: 991 return (!FTy.isVarArg() && NumParams == 3 && 992 FTy.getParamType(0)->isPointerTy() && 993 FTy.getParamType(1)->isPointerTy() && 994 FTy.getParamType(2)->isIntegerTy()); 995 996 case LibFunc_cxa_guard_abort: 997 case LibFunc_cxa_guard_acquire: 998 case LibFunc_cxa_guard_release: 999 case LibFunc_nvvm_reflect: 1000 return (NumParams == 1 && FTy.getParamType(0)->isPointerTy()); 1001 1002 case LibFunc_sincospi_stret: 1003 case LibFunc_sincospif_stret: 1004 return (NumParams == 1 && FTy.getParamType(0)->isFloatingPointTy()); 1005 1006 case LibFunc_acos: 1007 case LibFunc_acosf: 1008 case LibFunc_acosh: 1009 case LibFunc_acoshf: 1010 case LibFunc_acoshl: 1011 case LibFunc_acosl: 1012 case LibFunc_asin: 1013 case LibFunc_asinf: 1014 case LibFunc_asinh: 1015 case LibFunc_asinhf: 1016 case LibFunc_asinhl: 1017 case LibFunc_asinl: 1018 case LibFunc_atan: 1019 case LibFunc_atanf: 1020 case LibFunc_atanh: 1021 case LibFunc_atanhf: 1022 case LibFunc_atanhl: 1023 case LibFunc_atanl: 1024 case LibFunc_cbrt: 1025 case LibFunc_cbrtf: 1026 case LibFunc_cbrtl: 1027 case LibFunc_ceil: 1028 case LibFunc_ceilf: 1029 case LibFunc_ceill: 1030 case LibFunc_cos: 1031 case LibFunc_cosf: 1032 case LibFunc_cosh: 1033 case LibFunc_coshf: 1034 case LibFunc_coshl: 1035 case LibFunc_cosl: 1036 case LibFunc_exp10: 1037 case LibFunc_exp10f: 1038 case LibFunc_exp10l: 1039 case LibFunc_exp2: 1040 case LibFunc_exp2f: 1041 case LibFunc_exp2l: 1042 case LibFunc_exp: 1043 case LibFunc_expf: 1044 case LibFunc_expl: 1045 case LibFunc_expm1: 1046 case LibFunc_expm1f: 1047 case LibFunc_expm1l: 1048 case LibFunc_fabs: 1049 case LibFunc_fabsf: 1050 case LibFunc_fabsl: 1051 case LibFunc_floor: 1052 case LibFunc_floorf: 1053 case LibFunc_floorl: 1054 case LibFunc_log10: 1055 case LibFunc_log10f: 1056 case LibFunc_log10l: 1057 case LibFunc_log1p: 1058 case LibFunc_log1pf: 1059 case LibFunc_log1pl: 1060 case LibFunc_log2: 1061 case LibFunc_log2f: 1062 case LibFunc_log2l: 1063 case LibFunc_log: 1064 case LibFunc_logb: 1065 case LibFunc_logbf: 1066 case LibFunc_logbl: 1067 case LibFunc_logf: 1068 case LibFunc_logl: 1069 case LibFunc_nearbyint: 1070 case LibFunc_nearbyintf: 1071 case LibFunc_nearbyintl: 1072 case LibFunc_rint: 1073 case LibFunc_rintf: 1074 case LibFunc_rintl: 1075 case LibFunc_round: 1076 case LibFunc_roundf: 1077 case LibFunc_roundl: 1078 case LibFunc_sin: 1079 case LibFunc_sinf: 1080 case LibFunc_sinh: 1081 case LibFunc_sinhf: 1082 case LibFunc_sinhl: 1083 case LibFunc_sinl: 1084 case LibFunc_sqrt: 1085 case LibFunc_sqrt_finite: 1086 case LibFunc_sqrtf: 1087 case LibFunc_sqrtf_finite: 1088 case LibFunc_sqrtl: 1089 case LibFunc_sqrtl_finite: 1090 case LibFunc_tan: 1091 case LibFunc_tanf: 1092 case LibFunc_tanh: 1093 case LibFunc_tanhf: 1094 case LibFunc_tanhl: 1095 case LibFunc_tanl: 1096 case LibFunc_trunc: 1097 case LibFunc_truncf: 1098 case LibFunc_truncl: 1099 return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() && 1100 FTy.getReturnType() == FTy.getParamType(0)); 1101 1102 case LibFunc_atan2: 1103 case LibFunc_atan2f: 1104 case LibFunc_atan2l: 1105 case LibFunc_fmin: 1106 case LibFunc_fminf: 1107 case LibFunc_fminl: 1108 case LibFunc_fmax: 1109 case LibFunc_fmaxf: 1110 case LibFunc_fmaxl: 1111 case LibFunc_fmod: 1112 case LibFunc_fmodf: 1113 case LibFunc_fmodl: 1114 case LibFunc_copysign: 1115 case LibFunc_copysignf: 1116 case LibFunc_copysignl: 1117 case LibFunc_pow: 1118 case LibFunc_powf: 1119 case LibFunc_powl: 1120 return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() && 1121 FTy.getReturnType() == FTy.getParamType(0) && 1122 FTy.getReturnType() == FTy.getParamType(1)); 1123 1124 case LibFunc_ldexp: 1125 case LibFunc_ldexpf: 1126 case LibFunc_ldexpl: 1127 return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() && 1128 FTy.getReturnType() == FTy.getParamType(0) && 1129 FTy.getParamType(1)->isIntegerTy(32)); 1130 1131 case LibFunc_ffs: 1132 case LibFunc_ffsl: 1133 case LibFunc_ffsll: 1134 case LibFunc_fls: 1135 case LibFunc_flsl: 1136 case LibFunc_flsll: 1137 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) && 1138 FTy.getParamType(0)->isIntegerTy()); 1139 1140 case LibFunc_isdigit: 1141 case LibFunc_isascii: 1142 case LibFunc_toascii: 1143 case LibFunc_putchar: 1144 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) && 1145 FTy.getReturnType() == FTy.getParamType(0)); 1146 1147 case LibFunc_abs: 1148 case LibFunc_labs: 1149 case LibFunc_llabs: 1150 return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() && 1151 FTy.getReturnType() == FTy.getParamType(0)); 1152 1153 case LibFunc_cxa_atexit: 1154 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() && 1155 FTy.getParamType(0)->isPointerTy() && 1156 FTy.getParamType(1)->isPointerTy() && 1157 FTy.getParamType(2)->isPointerTy()); 1158 1159 case LibFunc_sinpi: 1160 case LibFunc_cospi: 1161 return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() && 1162 FTy.getReturnType() == FTy.getParamType(0)); 1163 1164 case LibFunc_sinpif: 1165 case LibFunc_cospif: 1166 return (NumParams == 1 && FTy.getReturnType()->isFloatTy() && 1167 FTy.getReturnType() == FTy.getParamType(0)); 1168 1169 case LibFunc_strnlen: 1170 return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(1) && 1171 FTy.getParamType(0) == PCharTy && 1172 FTy.getParamType(1) == SizeTTy); 1173 1174 case LibFunc_posix_memalign: 1175 return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) && 1176 FTy.getParamType(0)->isPointerTy() && 1177 FTy.getParamType(1) == SizeTTy && FTy.getParamType(2) == SizeTTy); 1178 1179 case LibFunc::NumLibFuncs: 1180 break; 1181 } 1182 1183 llvm_unreachable("Invalid libfunc"); 1184 } 1185 1186 bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl, 1187 LibFunc &F) const { 1188 const DataLayout *DL = 1189 FDecl.getParent() ? &FDecl.getParent()->getDataLayout() : nullptr; 1190 return getLibFunc(FDecl.getName(), F) && 1191 isValidProtoForLibFunc(*FDecl.getFunctionType(), F, DL); 1192 } 1193 1194 void TargetLibraryInfoImpl::disableAllFunctions() { 1195 memset(AvailableArray, 0, sizeof(AvailableArray)); 1196 } 1197 1198 static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) { 1199 return LHS.ScalarFnName < RHS.ScalarFnName; 1200 } 1201 1202 static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) { 1203 return LHS.VectorFnName < RHS.VectorFnName; 1204 } 1205 1206 static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) { 1207 return LHS.ScalarFnName < S; 1208 } 1209 1210 static bool compareWithVectorFnName(const VecDesc &LHS, StringRef S) { 1211 return LHS.VectorFnName < S; 1212 } 1213 1214 void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) { 1215 VectorDescs.insert(VectorDescs.end(), Fns.begin(), Fns.end()); 1216 std::sort(VectorDescs.begin(), VectorDescs.end(), compareByScalarFnName); 1217 1218 ScalarDescs.insert(ScalarDescs.end(), Fns.begin(), Fns.end()); 1219 std::sort(ScalarDescs.begin(), ScalarDescs.end(), compareByVectorFnName); 1220 } 1221 1222 void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib( 1223 enum VectorLibrary VecLib) { 1224 switch (VecLib) { 1225 case Accelerate: { 1226 const VecDesc VecFuncs[] = { 1227 // Floating-Point Arithmetic and Auxiliary Functions 1228 {"ceilf", "vceilf", 4}, 1229 {"fabsf", "vfabsf", 4}, 1230 {"llvm.fabs.f32", "vfabsf", 4}, 1231 {"floorf", "vfloorf", 4}, 1232 {"sqrtf", "vsqrtf", 4}, 1233 {"llvm.sqrt.f32", "vsqrtf", 4}, 1234 1235 // Exponential and Logarithmic Functions 1236 {"expf", "vexpf", 4}, 1237 {"llvm.exp.f32", "vexpf", 4}, 1238 {"expm1f", "vexpm1f", 4}, 1239 {"logf", "vlogf", 4}, 1240 {"llvm.log.f32", "vlogf", 4}, 1241 {"log1pf", "vlog1pf", 4}, 1242 {"log10f", "vlog10f", 4}, 1243 {"llvm.log10.f32", "vlog10f", 4}, 1244 {"logbf", "vlogbf", 4}, 1245 1246 // Trigonometric Functions 1247 {"sinf", "vsinf", 4}, 1248 {"llvm.sin.f32", "vsinf", 4}, 1249 {"cosf", "vcosf", 4}, 1250 {"llvm.cos.f32", "vcosf", 4}, 1251 {"tanf", "vtanf", 4}, 1252 {"asinf", "vasinf", 4}, 1253 {"acosf", "vacosf", 4}, 1254 {"atanf", "vatanf", 4}, 1255 1256 // Hyperbolic Functions 1257 {"sinhf", "vsinhf", 4}, 1258 {"coshf", "vcoshf", 4}, 1259 {"tanhf", "vtanhf", 4}, 1260 {"asinhf", "vasinhf", 4}, 1261 {"acoshf", "vacoshf", 4}, 1262 {"atanhf", "vatanhf", 4}, 1263 }; 1264 addVectorizableFunctions(VecFuncs); 1265 break; 1266 } 1267 case SVML: { 1268 const VecDesc VecFuncs[] = { 1269 {"sin", "__svml_sin2", 2}, 1270 {"sin", "__svml_sin4", 4}, 1271 {"sin", "__svml_sin8", 8}, 1272 1273 {"sinf", "__svml_sinf4", 4}, 1274 {"sinf", "__svml_sinf8", 8}, 1275 {"sinf", "__svml_sinf16", 16}, 1276 1277 {"cos", "__svml_cos2", 2}, 1278 {"cos", "__svml_cos4", 4}, 1279 {"cos", "__svml_cos8", 8}, 1280 1281 {"cosf", "__svml_cosf4", 4}, 1282 {"cosf", "__svml_cosf8", 8}, 1283 {"cosf", "__svml_cosf16", 16}, 1284 1285 {"pow", "__svml_pow2", 2}, 1286 {"pow", "__svml_pow4", 4}, 1287 {"pow", "__svml_pow8", 8}, 1288 1289 {"powf", "__svml_powf4", 4}, 1290 {"powf", "__svml_powf8", 8}, 1291 {"powf", "__svml_powf16", 16}, 1292 1293 {"llvm.pow.f64", "__svml_pow2", 2}, 1294 {"llvm.pow.f64", "__svml_pow4", 4}, 1295 {"llvm.pow.f64", "__svml_pow8", 8}, 1296 1297 {"llvm.pow.f32", "__svml_powf4", 4}, 1298 {"llvm.pow.f32", "__svml_powf8", 8}, 1299 {"llvm.pow.f32", "__svml_powf16", 16}, 1300 1301 {"exp", "__svml_exp2", 2}, 1302 {"exp", "__svml_exp4", 4}, 1303 {"exp", "__svml_exp8", 8}, 1304 1305 {"expf", "__svml_expf4", 4}, 1306 {"expf", "__svml_expf8", 8}, 1307 {"expf", "__svml_expf16", 16}, 1308 1309 {"llvm.exp.f64", "__svml_exp2", 2}, 1310 {"llvm.exp.f64", "__svml_exp4", 4}, 1311 {"llvm.exp.f64", "__svml_exp8", 8}, 1312 1313 {"llvm.exp.f32", "__svml_expf4", 4}, 1314 {"llvm.exp.f32", "__svml_expf8", 8}, 1315 {"llvm.exp.f32", "__svml_expf16", 16}, 1316 1317 {"log", "__svml_log2", 2}, 1318 {"log", "__svml_log4", 4}, 1319 {"log", "__svml_log8", 8}, 1320 1321 {"logf", "__svml_logf4", 4}, 1322 {"logf", "__svml_logf8", 8}, 1323 {"logf", "__svml_logf16", 16}, 1324 1325 {"llvm.log.f64", "__svml_log2", 2}, 1326 {"llvm.log.f64", "__svml_log4", 4}, 1327 {"llvm.log.f64", "__svml_log8", 8}, 1328 1329 {"llvm.log.f32", "__svml_logf4", 4}, 1330 {"llvm.log.f32", "__svml_logf8", 8}, 1331 {"llvm.log.f32", "__svml_logf16", 16}, 1332 }; 1333 addVectorizableFunctions(VecFuncs); 1334 break; 1335 } 1336 case NoLibrary: 1337 break; 1338 } 1339 } 1340 1341 bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const { 1342 funcName = sanitizeFunctionName(funcName); 1343 if (funcName.empty()) 1344 return false; 1345 1346 std::vector<VecDesc>::const_iterator I = std::lower_bound( 1347 VectorDescs.begin(), VectorDescs.end(), funcName, 1348 compareWithScalarFnName); 1349 return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName; 1350 } 1351 1352 StringRef TargetLibraryInfoImpl::getVectorizedFunction(StringRef F, 1353 unsigned VF) const { 1354 F = sanitizeFunctionName(F); 1355 if (F.empty()) 1356 return F; 1357 std::vector<VecDesc>::const_iterator I = std::lower_bound( 1358 VectorDescs.begin(), VectorDescs.end(), F, compareWithScalarFnName); 1359 while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) { 1360 if (I->VectorizationFactor == VF) 1361 return I->VectorFnName; 1362 ++I; 1363 } 1364 return StringRef(); 1365 } 1366 1367 StringRef TargetLibraryInfoImpl::getScalarizedFunction(StringRef F, 1368 unsigned &VF) const { 1369 F = sanitizeFunctionName(F); 1370 if (F.empty()) 1371 return F; 1372 1373 std::vector<VecDesc>::const_iterator I = std::lower_bound( 1374 ScalarDescs.begin(), ScalarDescs.end(), F, compareWithVectorFnName); 1375 if (I == VectorDescs.end() || StringRef(I->VectorFnName) != F) 1376 return StringRef(); 1377 VF = I->VectorizationFactor; 1378 return I->ScalarFnName; 1379 } 1380 1381 TargetLibraryInfo TargetLibraryAnalysis::run(Module &M, 1382 ModuleAnalysisManager &) { 1383 if (PresetInfoImpl) 1384 return TargetLibraryInfo(*PresetInfoImpl); 1385 1386 return TargetLibraryInfo(lookupInfoImpl(Triple(M.getTargetTriple()))); 1387 } 1388 1389 TargetLibraryInfo TargetLibraryAnalysis::run(Function &F, 1390 FunctionAnalysisManager &) { 1391 if (PresetInfoImpl) 1392 return TargetLibraryInfo(*PresetInfoImpl); 1393 1394 return TargetLibraryInfo( 1395 lookupInfoImpl(Triple(F.getParent()->getTargetTriple()))); 1396 } 1397 1398 TargetLibraryInfoImpl &TargetLibraryAnalysis::lookupInfoImpl(const Triple &T) { 1399 std::unique_ptr<TargetLibraryInfoImpl> &Impl = 1400 Impls[T.normalize()]; 1401 if (!Impl) 1402 Impl.reset(new TargetLibraryInfoImpl(T)); 1403 1404 return *Impl; 1405 } 1406 1407 1408 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass() 1409 : ImmutablePass(ID), TLIImpl(), TLI(TLIImpl) { 1410 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 1411 } 1412 1413 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T) 1414 : ImmutablePass(ID), TLIImpl(T), TLI(TLIImpl) { 1415 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 1416 } 1417 1418 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass( 1419 const TargetLibraryInfoImpl &TLIImpl) 1420 : ImmutablePass(ID), TLIImpl(TLIImpl), TLI(this->TLIImpl) { 1421 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 1422 } 1423 1424 AnalysisKey TargetLibraryAnalysis::Key; 1425 1426 // Register the basic pass. 1427 INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo", 1428 "Target Library Information", false, true) 1429 char TargetLibraryInfoWrapperPass::ID = 0; 1430 1431 void TargetLibraryInfoWrapperPass::anchor() {} 1432