TargetLibraryInfo.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. //===-- TargetLibraryInfo.cpp - Runtime library information ----------------==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the TargetLibraryInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Analysis/TargetLibraryInfo.h"
  13. #include "llvm/ADT/Triple.h"
  14. #include "llvm/IR/Constants.h"
  15. #include "llvm/InitializePasses.h"
  16. #include "llvm/Support/CommandLine.h"
  17. using namespace llvm;
  18. static cl::opt<TargetLibraryInfoImpl::VectorLibrary> ClVectorLibrary(
  19. "vector-library", cl::Hidden, cl::desc("Vector functions library"),
  20. cl::init(TargetLibraryInfoImpl::NoLibrary),
  21. cl::values(clEnumValN(TargetLibraryInfoImpl::NoLibrary, "none",
  22. "No vector functions library"),
  23. clEnumValN(TargetLibraryInfoImpl::Accelerate, "Accelerate",
  24. "Accelerate framework"),
  25. clEnumValN(TargetLibraryInfoImpl::DarwinLibSystemM,
  26. "Darwin_libsystem_m", "Darwin libsystem_m"),
  27. clEnumValN(TargetLibraryInfoImpl::LIBMVEC_X86, "LIBMVEC-X86",
  28. "GLIBC Vector Math library"),
  29. clEnumValN(TargetLibraryInfoImpl::MASSV, "MASSV",
  30. "IBM MASS vector library"),
  31. clEnumValN(TargetLibraryInfoImpl::SVML, "SVML",
  32. "Intel SVML library"),
  33. clEnumValN(TargetLibraryInfoImpl::SLEEFGNUABI, "sleefgnuabi",
  34. "SIMD Library for Evaluating Elementary Functions")));
  35. StringLiteral const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] =
  36. {
  37. #define TLI_DEFINE_STRING
  38. #include "llvm/Analysis/TargetLibraryInfo.def"
  39. };
  40. // Recognized types of library function arguments and return types.
  41. enum FuncArgTypeID : char {
  42. Void = 0, // Must be zero.
  43. Bool, // 8 bits on all targets
  44. Int16,
  45. Int32,
  46. Int,
  47. IntPlus, // Int or bigger.
  48. Long, // Either 32 or 64 bits.
  49. IntX, // Any integer type.
  50. Int64,
  51. LLong, // 64 bits on all targets.
  52. SizeT, // size_t.
  53. SSizeT, // POSIX ssize_t.
  54. Flt, // IEEE float.
  55. Dbl, // IEEE double.
  56. LDbl, // Any floating type (TODO: tighten this up).
  57. Floating, // Any floating type.
  58. Ptr, // Any pointer type.
  59. Struct, // Any struct type.
  60. Ellip, // The ellipsis (...).
  61. Same, // Same argument type as the previous one.
  62. };
  63. typedef std::array<FuncArgTypeID, 8> FuncProtoTy;
  64. static const FuncProtoTy Signatures[] = {
  65. #define TLI_DEFINE_SIG
  66. #include "llvm/Analysis/TargetLibraryInfo.def"
  67. };
  68. static_assert(sizeof Signatures / sizeof *Signatures == LibFunc::NumLibFuncs,
  69. "Missing library function signatures");
  70. static bool hasSinCosPiStret(const Triple &T) {
  71. // Only Darwin variants have _stret versions of combined trig functions.
  72. if (!T.isOSDarwin())
  73. return false;
  74. // The ABI is rather complicated on x86, so don't do anything special there.
  75. if (T.getArch() == Triple::x86)
  76. return false;
  77. if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
  78. return false;
  79. if (T.isiOS() && T.isOSVersionLT(7, 0))
  80. return false;
  81. return true;
  82. }
  83. static bool hasBcmp(const Triple &TT) {
  84. // Posix removed support from bcmp() in 2001, but the glibc and several
  85. // implementations of the libc still have it.
  86. if (TT.isOSLinux())
  87. return TT.isGNUEnvironment() || TT.isMusl();
  88. // Both NetBSD and OpenBSD are planning to remove the function. Windows does
  89. // not have it.
  90. return TT.isOSFreeBSD() || TT.isOSSolaris();
  91. }
  92. static bool isCallingConvCCompatible(CallingConv::ID CC, StringRef TT,
  93. FunctionType *FuncTy) {
  94. switch (CC) {
  95. default:
  96. return false;
  97. case llvm::CallingConv::C:
  98. return true;
  99. case llvm::CallingConv::ARM_APCS:
  100. case llvm::CallingConv::ARM_AAPCS:
  101. case llvm::CallingConv::ARM_AAPCS_VFP: {
  102. // The iOS ABI diverges from the standard in some cases, so for now don't
  103. // try to simplify those calls.
  104. if (Triple(TT).isiOS())
  105. return false;
  106. if (!FuncTy->getReturnType()->isPointerTy() &&
  107. !FuncTy->getReturnType()->isIntegerTy() &&
  108. !FuncTy->getReturnType()->isVoidTy())
  109. return false;
  110. for (auto *Param : FuncTy->params()) {
  111. if (!Param->isPointerTy() && !Param->isIntegerTy())
  112. return false;
  113. }
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. bool TargetLibraryInfoImpl::isCallingConvCCompatible(CallBase *CI) {
  120. return ::isCallingConvCCompatible(CI->getCallingConv(),
  121. CI->getModule()->getTargetTriple(),
  122. CI->getFunctionType());
  123. }
  124. bool TargetLibraryInfoImpl::isCallingConvCCompatible(Function *F) {
  125. return ::isCallingConvCCompatible(F->getCallingConv(),
  126. F->getParent()->getTargetTriple(),
  127. F->getFunctionType());
  128. }
  129. /// Initialize the set of available library functions based on the specified
  130. /// target triple. This should be carefully written so that a missing target
  131. /// triple gets a sane set of defaults.
  132. static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
  133. ArrayRef<StringLiteral> StandardNames) {
  134. // Verify that the StandardNames array is in alphabetical order.
  135. assert(
  136. llvm::is_sorted(StandardNames,
  137. [](StringRef LHS, StringRef RHS) { return LHS < RHS; }) &&
  138. "TargetLibraryInfoImpl function names must be sorted");
  139. // Set IO unlocked variants as unavailable
  140. // Set them as available per system below
  141. TLI.setUnavailable(LibFunc_getc_unlocked);
  142. TLI.setUnavailable(LibFunc_getchar_unlocked);
  143. TLI.setUnavailable(LibFunc_putc_unlocked);
  144. TLI.setUnavailable(LibFunc_putchar_unlocked);
  145. TLI.setUnavailable(LibFunc_fputc_unlocked);
  146. TLI.setUnavailable(LibFunc_fgetc_unlocked);
  147. TLI.setUnavailable(LibFunc_fread_unlocked);
  148. TLI.setUnavailable(LibFunc_fwrite_unlocked);
  149. TLI.setUnavailable(LibFunc_fputs_unlocked);
  150. TLI.setUnavailable(LibFunc_fgets_unlocked);
  151. bool ShouldExtI32Param, ShouldExtI32Return;
  152. bool ShouldSignExtI32Param, ShouldSignExtI32Return;
  153. TargetLibraryInfo::initExtensionsForTriple(ShouldExtI32Param,
  154. ShouldExtI32Return, ShouldSignExtI32Param, ShouldSignExtI32Return, T);
  155. TLI.setShouldExtI32Param(ShouldExtI32Param);
  156. TLI.setShouldExtI32Return(ShouldExtI32Return);
  157. TLI.setShouldSignExtI32Param(ShouldSignExtI32Param);
  158. TLI.setShouldSignExtI32Return(ShouldSignExtI32Return);
  159. // Let's assume by default that the size of int is 32 bits, unless the target
  160. // is a 16-bit architecture because then it most likely is 16 bits. If that
  161. // isn't true for a target those defaults should be overridden below.
  162. TLI.setIntSize(T.isArch16Bit() ? 16 : 32);
  163. // There is really no runtime library on AMDGPU, apart from
  164. // __kmpc_alloc/free_shared.
  165. if (T.isAMDGPU()) {
  166. TLI.disableAllFunctions();
  167. TLI.setAvailable(llvm::LibFunc___kmpc_alloc_shared);
  168. TLI.setAvailable(llvm::LibFunc___kmpc_free_shared);
  169. return;
  170. }
  171. // memset_pattern{4,8,16} is only available on iOS 3.0 and Mac OS X 10.5 and
  172. // later. All versions of watchOS support it.
  173. if (T.isMacOSX()) {
  174. // available IO unlocked variants on Mac OS X
  175. TLI.setAvailable(LibFunc_getc_unlocked);
  176. TLI.setAvailable(LibFunc_getchar_unlocked);
  177. TLI.setAvailable(LibFunc_putc_unlocked);
  178. TLI.setAvailable(LibFunc_putchar_unlocked);
  179. if (T.isMacOSXVersionLT(10, 5)) {
  180. TLI.setUnavailable(LibFunc_memset_pattern4);
  181. TLI.setUnavailable(LibFunc_memset_pattern8);
  182. TLI.setUnavailable(LibFunc_memset_pattern16);
  183. }
  184. } else if (T.isiOS()) {
  185. if (T.isOSVersionLT(3, 0)) {
  186. TLI.setUnavailable(LibFunc_memset_pattern4);
  187. TLI.setUnavailable(LibFunc_memset_pattern8);
  188. TLI.setUnavailable(LibFunc_memset_pattern16);
  189. }
  190. } else if (!T.isWatchOS()) {
  191. TLI.setUnavailable(LibFunc_memset_pattern4);
  192. TLI.setUnavailable(LibFunc_memset_pattern8);
  193. TLI.setUnavailable(LibFunc_memset_pattern16);
  194. }
  195. if (!hasSinCosPiStret(T)) {
  196. TLI.setUnavailable(LibFunc_sinpi);
  197. TLI.setUnavailable(LibFunc_sinpif);
  198. TLI.setUnavailable(LibFunc_cospi);
  199. TLI.setUnavailable(LibFunc_cospif);
  200. TLI.setUnavailable(LibFunc_sincospi_stret);
  201. TLI.setUnavailable(LibFunc_sincospif_stret);
  202. }
  203. if (!hasBcmp(T))
  204. TLI.setUnavailable(LibFunc_bcmp);
  205. if (T.isMacOSX() && T.getArch() == Triple::x86 &&
  206. !T.isMacOSXVersionLT(10, 7)) {
  207. // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
  208. // we don't care about) have two versions; on recent OSX, the one we want
  209. // has a $UNIX2003 suffix. The two implementations are identical except
  210. // for the return value in some edge cases. However, we don't want to
  211. // generate code that depends on the old symbols.
  212. TLI.setAvailableWithName(LibFunc_fwrite, "fwrite$UNIX2003");
  213. TLI.setAvailableWithName(LibFunc_fputs, "fputs$UNIX2003");
  214. }
  215. // iprintf and friends are only available on XCore, TCE, and Emscripten.
  216. if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce &&
  217. T.getOS() != Triple::Emscripten) {
  218. TLI.setUnavailable(LibFunc_iprintf);
  219. TLI.setUnavailable(LibFunc_siprintf);
  220. TLI.setUnavailable(LibFunc_fiprintf);
  221. }
  222. // __small_printf and friends are only available on Emscripten.
  223. if (T.getOS() != Triple::Emscripten) {
  224. TLI.setUnavailable(LibFunc_small_printf);
  225. TLI.setUnavailable(LibFunc_small_sprintf);
  226. TLI.setUnavailable(LibFunc_small_fprintf);
  227. }
  228. if (T.isOSWindows() && !T.isOSCygMing()) {
  229. // XXX: The earliest documentation available at the moment is for VS2015/VC19:
  230. // https://docs.microsoft.com/en-us/cpp/c-runtime-library/floating-point-support?view=vs-2015
  231. // XXX: In order to use an MSVCRT older than VC19,
  232. // the specific library version must be explicit in the target triple,
  233. // e.g., x86_64-pc-windows-msvc18.
  234. bool hasPartialC99 = true;
  235. if (T.isKnownWindowsMSVCEnvironment()) {
  236. VersionTuple Version = T.getEnvironmentVersion();
  237. hasPartialC99 = (Version.getMajor() == 0 || Version.getMajor() >= 19);
  238. }
  239. // Latest targets support C89 math functions, in part.
  240. bool isARM = (T.getArch() == Triple::aarch64 ||
  241. T.getArch() == Triple::arm);
  242. bool hasPartialFloat = (isARM ||
  243. T.getArch() == Triple::x86_64);
  244. // Win32 does not support float C89 math functions, in general.
  245. if (!hasPartialFloat) {
  246. TLI.setUnavailable(LibFunc_acosf);
  247. TLI.setUnavailable(LibFunc_asinf);
  248. TLI.setUnavailable(LibFunc_atan2f);
  249. TLI.setUnavailable(LibFunc_atanf);
  250. TLI.setUnavailable(LibFunc_ceilf);
  251. TLI.setUnavailable(LibFunc_cosf);
  252. TLI.setUnavailable(LibFunc_coshf);
  253. TLI.setUnavailable(LibFunc_expf);
  254. TLI.setUnavailable(LibFunc_floorf);
  255. TLI.setUnavailable(LibFunc_fmodf);
  256. TLI.setUnavailable(LibFunc_log10f);
  257. TLI.setUnavailable(LibFunc_logf);
  258. TLI.setUnavailable(LibFunc_modff);
  259. TLI.setUnavailable(LibFunc_powf);
  260. TLI.setUnavailable(LibFunc_remainderf);
  261. TLI.setUnavailable(LibFunc_sinf);
  262. TLI.setUnavailable(LibFunc_sinhf);
  263. TLI.setUnavailable(LibFunc_sqrtf);
  264. TLI.setUnavailable(LibFunc_tanf);
  265. TLI.setUnavailable(LibFunc_tanhf);
  266. }
  267. if (!isARM)
  268. TLI.setUnavailable(LibFunc_fabsf);
  269. TLI.setUnavailable(LibFunc_frexpf);
  270. TLI.setUnavailable(LibFunc_ldexpf);
  271. // Win32 does not support long double C89 math functions.
  272. TLI.setUnavailable(LibFunc_acosl);
  273. TLI.setUnavailable(LibFunc_asinl);
  274. TLI.setUnavailable(LibFunc_atan2l);
  275. TLI.setUnavailable(LibFunc_atanl);
  276. TLI.setUnavailable(LibFunc_ceill);
  277. TLI.setUnavailable(LibFunc_cosl);
  278. TLI.setUnavailable(LibFunc_coshl);
  279. TLI.setUnavailable(LibFunc_expl);
  280. TLI.setUnavailable(LibFunc_fabsl);
  281. TLI.setUnavailable(LibFunc_floorl);
  282. TLI.setUnavailable(LibFunc_fmodl);
  283. TLI.setUnavailable(LibFunc_frexpl);
  284. TLI.setUnavailable(LibFunc_ldexpl);
  285. TLI.setUnavailable(LibFunc_log10l);
  286. TLI.setUnavailable(LibFunc_logl);
  287. TLI.setUnavailable(LibFunc_modfl);
  288. TLI.setUnavailable(LibFunc_powl);
  289. TLI.setUnavailable(LibFunc_remainderl);
  290. TLI.setUnavailable(LibFunc_sinl);
  291. TLI.setUnavailable(LibFunc_sinhl);
  292. TLI.setUnavailable(LibFunc_sqrtl);
  293. TLI.setUnavailable(LibFunc_tanl);
  294. TLI.setUnavailable(LibFunc_tanhl);
  295. // Win32 does not fully support C99 math functions.
  296. if (!hasPartialC99) {
  297. TLI.setUnavailable(LibFunc_acosh);
  298. TLI.setUnavailable(LibFunc_acoshf);
  299. TLI.setUnavailable(LibFunc_asinh);
  300. TLI.setUnavailable(LibFunc_asinhf);
  301. TLI.setUnavailable(LibFunc_atanh);
  302. TLI.setUnavailable(LibFunc_atanhf);
  303. TLI.setAvailableWithName(LibFunc_cabs, "_cabs");
  304. TLI.setUnavailable(LibFunc_cabsf);
  305. TLI.setUnavailable(LibFunc_cbrt);
  306. TLI.setUnavailable(LibFunc_cbrtf);
  307. TLI.setAvailableWithName(LibFunc_copysign, "_copysign");
  308. TLI.setAvailableWithName(LibFunc_copysignf, "_copysignf");
  309. TLI.setUnavailable(LibFunc_exp2);
  310. TLI.setUnavailable(LibFunc_exp2f);
  311. TLI.setUnavailable(LibFunc_expm1);
  312. TLI.setUnavailable(LibFunc_expm1f);
  313. TLI.setUnavailable(LibFunc_fmax);
  314. TLI.setUnavailable(LibFunc_fmaxf);
  315. TLI.setUnavailable(LibFunc_fmin);
  316. TLI.setUnavailable(LibFunc_fminf);
  317. TLI.setUnavailable(LibFunc_log1p);
  318. TLI.setUnavailable(LibFunc_log1pf);
  319. TLI.setUnavailable(LibFunc_log2);
  320. TLI.setUnavailable(LibFunc_log2f);
  321. TLI.setAvailableWithName(LibFunc_logb, "_logb");
  322. if (hasPartialFloat)
  323. TLI.setAvailableWithName(LibFunc_logbf, "_logbf");
  324. else
  325. TLI.setUnavailable(LibFunc_logbf);
  326. TLI.setUnavailable(LibFunc_rint);
  327. TLI.setUnavailable(LibFunc_rintf);
  328. TLI.setUnavailable(LibFunc_round);
  329. TLI.setUnavailable(LibFunc_roundf);
  330. TLI.setUnavailable(LibFunc_trunc);
  331. TLI.setUnavailable(LibFunc_truncf);
  332. }
  333. // Win32 does not support long double C99 math functions.
  334. TLI.setUnavailable(LibFunc_acoshl);
  335. TLI.setUnavailable(LibFunc_asinhl);
  336. TLI.setUnavailable(LibFunc_atanhl);
  337. TLI.setUnavailable(LibFunc_cabsl);
  338. TLI.setUnavailable(LibFunc_cbrtl);
  339. TLI.setUnavailable(LibFunc_copysignl);
  340. TLI.setUnavailable(LibFunc_exp2l);
  341. TLI.setUnavailable(LibFunc_expm1l);
  342. TLI.setUnavailable(LibFunc_fmaxl);
  343. TLI.setUnavailable(LibFunc_fminl);
  344. TLI.setUnavailable(LibFunc_log1pl);
  345. TLI.setUnavailable(LibFunc_log2l);
  346. TLI.setUnavailable(LibFunc_logbl);
  347. TLI.setUnavailable(LibFunc_nearbyintl);
  348. TLI.setUnavailable(LibFunc_rintl);
  349. TLI.setUnavailable(LibFunc_roundl);
  350. TLI.setUnavailable(LibFunc_truncl);
  351. // Win32 does not support these functions, but
  352. // they are generally available on POSIX-compliant systems.
  353. TLI.setUnavailable(LibFunc_access);
  354. TLI.setUnavailable(LibFunc_chmod);
  355. TLI.setUnavailable(LibFunc_closedir);
  356. TLI.setUnavailable(LibFunc_fdopen);
  357. TLI.setUnavailable(LibFunc_fileno);
  358. TLI.setUnavailable(LibFunc_fseeko);
  359. TLI.setUnavailable(LibFunc_fstat);
  360. TLI.setUnavailable(LibFunc_ftello);
  361. TLI.setUnavailable(LibFunc_gettimeofday);
  362. TLI.setUnavailable(LibFunc_memccpy);
  363. TLI.setUnavailable(LibFunc_mkdir);
  364. TLI.setUnavailable(LibFunc_open);
  365. TLI.setUnavailable(LibFunc_opendir);
  366. TLI.setUnavailable(LibFunc_pclose);
  367. TLI.setUnavailable(LibFunc_popen);
  368. TLI.setUnavailable(LibFunc_read);
  369. TLI.setUnavailable(LibFunc_rmdir);
  370. TLI.setUnavailable(LibFunc_stat);
  371. TLI.setUnavailable(LibFunc_strcasecmp);
  372. TLI.setUnavailable(LibFunc_strncasecmp);
  373. TLI.setUnavailable(LibFunc_unlink);
  374. TLI.setUnavailable(LibFunc_utime);
  375. TLI.setUnavailable(LibFunc_write);
  376. }
  377. if (T.isOSWindows() && !T.isWindowsCygwinEnvironment()) {
  378. // These functions aren't available in either MSVC or MinGW environments.
  379. TLI.setUnavailable(LibFunc_bcmp);
  380. TLI.setUnavailable(LibFunc_bcopy);
  381. TLI.setUnavailable(LibFunc_bzero);
  382. TLI.setUnavailable(LibFunc_chown);
  383. TLI.setUnavailable(LibFunc_ctermid);
  384. TLI.setUnavailable(LibFunc_ffs);
  385. TLI.setUnavailable(LibFunc_flockfile);
  386. TLI.setUnavailable(LibFunc_fstatvfs);
  387. TLI.setUnavailable(LibFunc_ftrylockfile);
  388. TLI.setUnavailable(LibFunc_funlockfile);
  389. TLI.setUnavailable(LibFunc_getitimer);
  390. TLI.setUnavailable(LibFunc_getlogin_r);
  391. TLI.setUnavailable(LibFunc_getpwnam);
  392. TLI.setUnavailable(LibFunc_htonl);
  393. TLI.setUnavailable(LibFunc_htons);
  394. TLI.setUnavailable(LibFunc_lchown);
  395. TLI.setUnavailable(LibFunc_lstat);
  396. TLI.setUnavailable(LibFunc_memrchr);
  397. TLI.setUnavailable(LibFunc_ntohl);
  398. TLI.setUnavailable(LibFunc_ntohs);
  399. TLI.setUnavailable(LibFunc_pread);
  400. TLI.setUnavailable(LibFunc_pwrite);
  401. TLI.setUnavailable(LibFunc_readlink);
  402. TLI.setUnavailable(LibFunc_realpath);
  403. TLI.setUnavailable(LibFunc_setitimer);
  404. TLI.setUnavailable(LibFunc_statvfs);
  405. TLI.setUnavailable(LibFunc_stpcpy);
  406. TLI.setUnavailable(LibFunc_stpncpy);
  407. TLI.setUnavailable(LibFunc_times);
  408. TLI.setUnavailable(LibFunc_uname);
  409. TLI.setUnavailable(LibFunc_unsetenv);
  410. TLI.setUnavailable(LibFunc_utimes);
  411. }
  412. // Pick just one set of new/delete variants.
  413. if (T.isOSMSVCRT()) {
  414. // MSVC, doesn't have the Itanium new/delete.
  415. TLI.setUnavailable(LibFunc_ZdaPv);
  416. TLI.setUnavailable(LibFunc_ZdaPvRKSt9nothrow_t);
  417. TLI.setUnavailable(LibFunc_ZdaPvSt11align_val_t);
  418. TLI.setUnavailable(LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t);
  419. TLI.setUnavailable(LibFunc_ZdaPvj);
  420. TLI.setUnavailable(LibFunc_ZdaPvjSt11align_val_t);
  421. TLI.setUnavailable(LibFunc_ZdaPvm);
  422. TLI.setUnavailable(LibFunc_ZdaPvmSt11align_val_t);
  423. TLI.setUnavailable(LibFunc_ZdlPv);
  424. TLI.setUnavailable(LibFunc_ZdlPvRKSt9nothrow_t);
  425. TLI.setUnavailable(LibFunc_ZdlPvSt11align_val_t);
  426. TLI.setUnavailable(LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t);
  427. TLI.setUnavailable(LibFunc_ZdlPvj);
  428. TLI.setUnavailable(LibFunc_ZdlPvjSt11align_val_t);
  429. TLI.setUnavailable(LibFunc_ZdlPvm);
  430. TLI.setUnavailable(LibFunc_ZdlPvmSt11align_val_t);
  431. TLI.setUnavailable(LibFunc_Znaj);
  432. TLI.setUnavailable(LibFunc_ZnajRKSt9nothrow_t);
  433. TLI.setUnavailable(LibFunc_ZnajSt11align_val_t);
  434. TLI.setUnavailable(LibFunc_ZnajSt11align_val_tRKSt9nothrow_t);
  435. TLI.setUnavailable(LibFunc_Znam);
  436. TLI.setUnavailable(LibFunc_ZnamRKSt9nothrow_t);
  437. TLI.setUnavailable(LibFunc_ZnamSt11align_val_t);
  438. TLI.setUnavailable(LibFunc_ZnamSt11align_val_tRKSt9nothrow_t);
  439. TLI.setUnavailable(LibFunc_Znwj);
  440. TLI.setUnavailable(LibFunc_ZnwjRKSt9nothrow_t);
  441. TLI.setUnavailable(LibFunc_ZnwjSt11align_val_t);
  442. TLI.setUnavailable(LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t);
  443. TLI.setUnavailable(LibFunc_Znwm);
  444. TLI.setUnavailable(LibFunc_ZnwmRKSt9nothrow_t);
  445. TLI.setUnavailable(LibFunc_ZnwmSt11align_val_t);
  446. TLI.setUnavailable(LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t);
  447. } else {
  448. // Not MSVC, assume it's Itanium.
  449. TLI.setUnavailable(LibFunc_msvc_new_int);
  450. TLI.setUnavailable(LibFunc_msvc_new_int_nothrow);
  451. TLI.setUnavailable(LibFunc_msvc_new_longlong);
  452. TLI.setUnavailable(LibFunc_msvc_new_longlong_nothrow);
  453. TLI.setUnavailable(LibFunc_msvc_delete_ptr32);
  454. TLI.setUnavailable(LibFunc_msvc_delete_ptr32_nothrow);
  455. TLI.setUnavailable(LibFunc_msvc_delete_ptr32_int);
  456. TLI.setUnavailable(LibFunc_msvc_delete_ptr64);
  457. TLI.setUnavailable(LibFunc_msvc_delete_ptr64_nothrow);
  458. TLI.setUnavailable(LibFunc_msvc_delete_ptr64_longlong);
  459. TLI.setUnavailable(LibFunc_msvc_new_array_int);
  460. TLI.setUnavailable(LibFunc_msvc_new_array_int_nothrow);
  461. TLI.setUnavailable(LibFunc_msvc_new_array_longlong);
  462. TLI.setUnavailable(LibFunc_msvc_new_array_longlong_nothrow);
  463. TLI.setUnavailable(LibFunc_msvc_delete_array_ptr32);
  464. TLI.setUnavailable(LibFunc_msvc_delete_array_ptr32_nothrow);
  465. TLI.setUnavailable(LibFunc_msvc_delete_array_ptr32_int);
  466. TLI.setUnavailable(LibFunc_msvc_delete_array_ptr64);
  467. TLI.setUnavailable(LibFunc_msvc_delete_array_ptr64_nothrow);
  468. TLI.setUnavailable(LibFunc_msvc_delete_array_ptr64_longlong);
  469. }
  470. switch (T.getOS()) {
  471. case Triple::MacOSX:
  472. // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
  473. // and their names are __exp10 and __exp10f. exp10l is not available on
  474. // OS X or iOS.
  475. TLI.setUnavailable(LibFunc_exp10l);
  476. if (T.isMacOSXVersionLT(10, 9)) {
  477. TLI.setUnavailable(LibFunc_exp10);
  478. TLI.setUnavailable(LibFunc_exp10f);
  479. } else {
  480. TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
  481. TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
  482. }
  483. break;
  484. case Triple::IOS:
  485. case Triple::TvOS:
  486. case Triple::WatchOS:
  487. TLI.setUnavailable(LibFunc_exp10l);
  488. if (!T.isWatchOS() &&
  489. (T.isOSVersionLT(7, 0) || (T.isOSVersionLT(9, 0) && T.isX86()))) {
  490. TLI.setUnavailable(LibFunc_exp10);
  491. TLI.setUnavailable(LibFunc_exp10f);
  492. } else {
  493. TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
  494. TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
  495. }
  496. break;
  497. case Triple::Linux:
  498. // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
  499. // buggy prior to glibc version 2.18. Until this version is widely deployed
  500. // or we have a reasonable detection strategy, we cannot use exp10 reliably
  501. // on Linux.
  502. //
  503. // Fall through to disable all of them.
  504. [[fallthrough]];
  505. default:
  506. TLI.setUnavailable(LibFunc_exp10);
  507. TLI.setUnavailable(LibFunc_exp10f);
  508. TLI.setUnavailable(LibFunc_exp10l);
  509. }
  510. // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
  511. // Linux (GLIBC):
  512. // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
  513. // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
  514. // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
  515. switch (T.getOS()) {
  516. case Triple::Darwin:
  517. case Triple::MacOSX:
  518. case Triple::IOS:
  519. case Triple::TvOS:
  520. case Triple::WatchOS:
  521. case Triple::FreeBSD:
  522. case Triple::Linux:
  523. break;
  524. default:
  525. TLI.setUnavailable(LibFunc_ffsl);
  526. }
  527. // ffsll is available on at least FreeBSD and Linux (GLIBC):
  528. // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
  529. // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
  530. switch (T.getOS()) {
  531. case Triple::Darwin:
  532. case Triple::MacOSX:
  533. case Triple::IOS:
  534. case Triple::TvOS:
  535. case Triple::WatchOS:
  536. case Triple::FreeBSD:
  537. case Triple::Linux:
  538. break;
  539. default:
  540. TLI.setUnavailable(LibFunc_ffsll);
  541. }
  542. // The following functions are available on at least FreeBSD:
  543. // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
  544. // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
  545. // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
  546. if (!T.isOSFreeBSD()) {
  547. TLI.setUnavailable(LibFunc_fls);
  548. TLI.setUnavailable(LibFunc_flsl);
  549. TLI.setUnavailable(LibFunc_flsll);
  550. }
  551. // The following functions are only available on GNU/Linux (using glibc).
  552. // Linux variants without glibc (eg: bionic, musl) may have some subset.
  553. if (!T.isOSLinux() || !T.isGNUEnvironment()) {
  554. TLI.setUnavailable(LibFunc_dunder_strdup);
  555. TLI.setUnavailable(LibFunc_dunder_strtok_r);
  556. TLI.setUnavailable(LibFunc_dunder_isoc99_scanf);
  557. TLI.setUnavailable(LibFunc_dunder_isoc99_sscanf);
  558. TLI.setUnavailable(LibFunc_under_IO_getc);
  559. TLI.setUnavailable(LibFunc_under_IO_putc);
  560. // But, Android and musl have memalign.
  561. if (!T.isAndroid() && !T.isMusl())
  562. TLI.setUnavailable(LibFunc_memalign);
  563. TLI.setUnavailable(LibFunc_fopen64);
  564. TLI.setUnavailable(LibFunc_fseeko64);
  565. TLI.setUnavailable(LibFunc_fstat64);
  566. TLI.setUnavailable(LibFunc_fstatvfs64);
  567. TLI.setUnavailable(LibFunc_ftello64);
  568. TLI.setUnavailable(LibFunc_lstat64);
  569. TLI.setUnavailable(LibFunc_open64);
  570. TLI.setUnavailable(LibFunc_stat64);
  571. TLI.setUnavailable(LibFunc_statvfs64);
  572. TLI.setUnavailable(LibFunc_tmpfile64);
  573. // Relaxed math functions are included in math-finite.h on Linux (GLIBC).
  574. // Note that math-finite.h is no longer supported by top-of-tree GLIBC,
  575. // so we keep these functions around just so that they're recognized by
  576. // the ConstantFolder.
  577. TLI.setUnavailable(LibFunc_acos_finite);
  578. TLI.setUnavailable(LibFunc_acosf_finite);
  579. TLI.setUnavailable(LibFunc_acosl_finite);
  580. TLI.setUnavailable(LibFunc_acosh_finite);
  581. TLI.setUnavailable(LibFunc_acoshf_finite);
  582. TLI.setUnavailable(LibFunc_acoshl_finite);
  583. TLI.setUnavailable(LibFunc_asin_finite);
  584. TLI.setUnavailable(LibFunc_asinf_finite);
  585. TLI.setUnavailable(LibFunc_asinl_finite);
  586. TLI.setUnavailable(LibFunc_atan2_finite);
  587. TLI.setUnavailable(LibFunc_atan2f_finite);
  588. TLI.setUnavailable(LibFunc_atan2l_finite);
  589. TLI.setUnavailable(LibFunc_atanh_finite);
  590. TLI.setUnavailable(LibFunc_atanhf_finite);
  591. TLI.setUnavailable(LibFunc_atanhl_finite);
  592. TLI.setUnavailable(LibFunc_cosh_finite);
  593. TLI.setUnavailable(LibFunc_coshf_finite);
  594. TLI.setUnavailable(LibFunc_coshl_finite);
  595. TLI.setUnavailable(LibFunc_exp10_finite);
  596. TLI.setUnavailable(LibFunc_exp10f_finite);
  597. TLI.setUnavailable(LibFunc_exp10l_finite);
  598. TLI.setUnavailable(LibFunc_exp2_finite);
  599. TLI.setUnavailable(LibFunc_exp2f_finite);
  600. TLI.setUnavailable(LibFunc_exp2l_finite);
  601. TLI.setUnavailable(LibFunc_exp_finite);
  602. TLI.setUnavailable(LibFunc_expf_finite);
  603. TLI.setUnavailable(LibFunc_expl_finite);
  604. TLI.setUnavailable(LibFunc_log10_finite);
  605. TLI.setUnavailable(LibFunc_log10f_finite);
  606. TLI.setUnavailable(LibFunc_log10l_finite);
  607. TLI.setUnavailable(LibFunc_log2_finite);
  608. TLI.setUnavailable(LibFunc_log2f_finite);
  609. TLI.setUnavailable(LibFunc_log2l_finite);
  610. TLI.setUnavailable(LibFunc_log_finite);
  611. TLI.setUnavailable(LibFunc_logf_finite);
  612. TLI.setUnavailable(LibFunc_logl_finite);
  613. TLI.setUnavailable(LibFunc_pow_finite);
  614. TLI.setUnavailable(LibFunc_powf_finite);
  615. TLI.setUnavailable(LibFunc_powl_finite);
  616. TLI.setUnavailable(LibFunc_sinh_finite);
  617. TLI.setUnavailable(LibFunc_sinhf_finite);
  618. TLI.setUnavailable(LibFunc_sinhl_finite);
  619. TLI.setUnavailable(LibFunc_sqrt_finite);
  620. TLI.setUnavailable(LibFunc_sqrtf_finite);
  621. TLI.setUnavailable(LibFunc_sqrtl_finite);
  622. }
  623. if ((T.isOSLinux() && T.isGNUEnvironment()) ||
  624. (T.isAndroid() && !T.isAndroidVersionLT(28))) {
  625. // available IO unlocked variants on GNU/Linux and Android P or later
  626. TLI.setAvailable(LibFunc_getc_unlocked);
  627. TLI.setAvailable(LibFunc_getchar_unlocked);
  628. TLI.setAvailable(LibFunc_putc_unlocked);
  629. TLI.setAvailable(LibFunc_putchar_unlocked);
  630. TLI.setAvailable(LibFunc_fputc_unlocked);
  631. TLI.setAvailable(LibFunc_fgetc_unlocked);
  632. TLI.setAvailable(LibFunc_fread_unlocked);
  633. TLI.setAvailable(LibFunc_fwrite_unlocked);
  634. TLI.setAvailable(LibFunc_fputs_unlocked);
  635. TLI.setAvailable(LibFunc_fgets_unlocked);
  636. }
  637. if (T.isAndroid() && T.isAndroidVersionLT(21)) {
  638. TLI.setUnavailable(LibFunc_stpcpy);
  639. TLI.setUnavailable(LibFunc_stpncpy);
  640. }
  641. if (T.isPS()) {
  642. // PS4/PS5 do have memalign.
  643. TLI.setAvailable(LibFunc_memalign);
  644. // PS4/PS5 do not have new/delete with "unsigned int" size parameter;
  645. // they only have the "unsigned long" versions.
  646. TLI.setUnavailable(LibFunc_ZdaPvj);
  647. TLI.setUnavailable(LibFunc_ZdaPvjSt11align_val_t);
  648. TLI.setUnavailable(LibFunc_ZdlPvj);
  649. TLI.setUnavailable(LibFunc_ZdlPvjSt11align_val_t);
  650. TLI.setUnavailable(LibFunc_Znaj);
  651. TLI.setUnavailable(LibFunc_ZnajRKSt9nothrow_t);
  652. TLI.setUnavailable(LibFunc_ZnajSt11align_val_t);
  653. TLI.setUnavailable(LibFunc_ZnajSt11align_val_tRKSt9nothrow_t);
  654. TLI.setUnavailable(LibFunc_Znwj);
  655. TLI.setUnavailable(LibFunc_ZnwjRKSt9nothrow_t);
  656. TLI.setUnavailable(LibFunc_ZnwjSt11align_val_t);
  657. TLI.setUnavailable(LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t);
  658. // None of the *_chk functions.
  659. TLI.setUnavailable(LibFunc_memccpy_chk);
  660. TLI.setUnavailable(LibFunc_memcpy_chk);
  661. TLI.setUnavailable(LibFunc_memmove_chk);
  662. TLI.setUnavailable(LibFunc_mempcpy_chk);
  663. TLI.setUnavailable(LibFunc_memset_chk);
  664. TLI.setUnavailable(LibFunc_snprintf_chk);
  665. TLI.setUnavailable(LibFunc_sprintf_chk);
  666. TLI.setUnavailable(LibFunc_stpcpy_chk);
  667. TLI.setUnavailable(LibFunc_stpncpy_chk);
  668. TLI.setUnavailable(LibFunc_strcat_chk);
  669. TLI.setUnavailable(LibFunc_strcpy_chk);
  670. TLI.setUnavailable(LibFunc_strlcat_chk);
  671. TLI.setUnavailable(LibFunc_strlcpy_chk);
  672. TLI.setUnavailable(LibFunc_strlen_chk);
  673. TLI.setUnavailable(LibFunc_strncat_chk);
  674. TLI.setUnavailable(LibFunc_strncpy_chk);
  675. TLI.setUnavailable(LibFunc_vsnprintf_chk);
  676. TLI.setUnavailable(LibFunc_vsprintf_chk);
  677. // Various Posix system functions.
  678. TLI.setUnavailable(LibFunc_access);
  679. TLI.setUnavailable(LibFunc_chmod);
  680. TLI.setUnavailable(LibFunc_chown);
  681. TLI.setUnavailable(LibFunc_closedir);
  682. TLI.setUnavailable(LibFunc_ctermid);
  683. TLI.setUnavailable(LibFunc_execl);
  684. TLI.setUnavailable(LibFunc_execle);
  685. TLI.setUnavailable(LibFunc_execlp);
  686. TLI.setUnavailable(LibFunc_execv);
  687. TLI.setUnavailable(LibFunc_execvP);
  688. TLI.setUnavailable(LibFunc_execve);
  689. TLI.setUnavailable(LibFunc_execvp);
  690. TLI.setUnavailable(LibFunc_execvpe);
  691. TLI.setUnavailable(LibFunc_fork);
  692. TLI.setUnavailable(LibFunc_fstat);
  693. TLI.setUnavailable(LibFunc_fstatvfs);
  694. TLI.setUnavailable(LibFunc_getenv);
  695. TLI.setUnavailable(LibFunc_getitimer);
  696. TLI.setUnavailable(LibFunc_getlogin_r);
  697. TLI.setUnavailable(LibFunc_getpwnam);
  698. TLI.setUnavailable(LibFunc_gettimeofday);
  699. TLI.setUnavailable(LibFunc_lchown);
  700. TLI.setUnavailable(LibFunc_lstat);
  701. TLI.setUnavailable(LibFunc_mkdir);
  702. TLI.setUnavailable(LibFunc_open);
  703. TLI.setUnavailable(LibFunc_opendir);
  704. TLI.setUnavailable(LibFunc_pclose);
  705. TLI.setUnavailable(LibFunc_popen);
  706. TLI.setUnavailable(LibFunc_pread);
  707. TLI.setUnavailable(LibFunc_pwrite);
  708. TLI.setUnavailable(LibFunc_read);
  709. TLI.setUnavailable(LibFunc_readlink);
  710. TLI.setUnavailable(LibFunc_realpath);
  711. TLI.setUnavailable(LibFunc_rename);
  712. TLI.setUnavailable(LibFunc_rmdir);
  713. TLI.setUnavailable(LibFunc_setitimer);
  714. TLI.setUnavailable(LibFunc_stat);
  715. TLI.setUnavailable(LibFunc_statvfs);
  716. TLI.setUnavailable(LibFunc_system);
  717. TLI.setUnavailable(LibFunc_times);
  718. TLI.setUnavailable(LibFunc_tmpfile);
  719. TLI.setUnavailable(LibFunc_unlink);
  720. TLI.setUnavailable(LibFunc_uname);
  721. TLI.setUnavailable(LibFunc_unsetenv);
  722. TLI.setUnavailable(LibFunc_utime);
  723. TLI.setUnavailable(LibFunc_utimes);
  724. TLI.setUnavailable(LibFunc_valloc);
  725. TLI.setUnavailable(LibFunc_write);
  726. // Miscellaneous other functions not provided.
  727. TLI.setUnavailable(LibFunc_atomic_load);
  728. TLI.setUnavailable(LibFunc_atomic_store);
  729. TLI.setUnavailable(LibFunc___kmpc_alloc_shared);
  730. TLI.setUnavailable(LibFunc___kmpc_free_shared);
  731. TLI.setUnavailable(LibFunc_dunder_strndup);
  732. TLI.setUnavailable(LibFunc_bcmp);
  733. TLI.setUnavailable(LibFunc_bcopy);
  734. TLI.setUnavailable(LibFunc_bzero);
  735. TLI.setUnavailable(LibFunc_cabs);
  736. TLI.setUnavailable(LibFunc_cabsf);
  737. TLI.setUnavailable(LibFunc_cabsl);
  738. TLI.setUnavailable(LibFunc_ffs);
  739. TLI.setUnavailable(LibFunc_flockfile);
  740. TLI.setUnavailable(LibFunc_fseeko);
  741. TLI.setUnavailable(LibFunc_ftello);
  742. TLI.setUnavailable(LibFunc_ftrylockfile);
  743. TLI.setUnavailable(LibFunc_funlockfile);
  744. TLI.setUnavailable(LibFunc_htonl);
  745. TLI.setUnavailable(LibFunc_htons);
  746. TLI.setUnavailable(LibFunc_isascii);
  747. TLI.setUnavailable(LibFunc_memccpy);
  748. TLI.setUnavailable(LibFunc_mempcpy);
  749. TLI.setUnavailable(LibFunc_memrchr);
  750. TLI.setUnavailable(LibFunc_ntohl);
  751. TLI.setUnavailable(LibFunc_ntohs);
  752. TLI.setUnavailable(LibFunc_reallocf);
  753. TLI.setUnavailable(LibFunc_roundeven);
  754. TLI.setUnavailable(LibFunc_roundevenf);
  755. TLI.setUnavailable(LibFunc_roundevenl);
  756. TLI.setUnavailable(LibFunc_stpcpy);
  757. TLI.setUnavailable(LibFunc_stpncpy);
  758. TLI.setUnavailable(LibFunc_strlcat);
  759. TLI.setUnavailable(LibFunc_strlcpy);
  760. TLI.setUnavailable(LibFunc_strndup);
  761. TLI.setUnavailable(LibFunc_strnlen);
  762. TLI.setUnavailable(LibFunc_toascii);
  763. }
  764. // As currently implemented in clang, NVPTX code has no standard library to
  765. // speak of. Headers provide a standard-ish library implementation, but many
  766. // of the signatures are wrong -- for example, many libm functions are not
  767. // extern "C".
  768. //
  769. // libdevice, an IR library provided by nvidia, is linked in by the front-end,
  770. // but only used functions are provided to llvm. Moreover, most of the
  771. // functions in libdevice don't map precisely to standard library functions.
  772. //
  773. // FIXME: Having no standard library prevents e.g. many fastmath
  774. // optimizations, so this situation should be fixed.
  775. if (T.isNVPTX()) {
  776. TLI.disableAllFunctions();
  777. TLI.setAvailable(LibFunc_nvvm_reflect);
  778. TLI.setAvailable(llvm::LibFunc_malloc);
  779. TLI.setAvailable(llvm::LibFunc_free);
  780. // TODO: We could enable the following two according to [0] but we haven't
  781. // done an evaluation wrt. the performance implications.
  782. // [0]
  783. // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#dynamic-global-memory-allocation-and-operations
  784. //
  785. // TLI.setAvailable(llvm::LibFunc_memcpy);
  786. // TLI.setAvailable(llvm::LibFunc_memset);
  787. TLI.setAvailable(llvm::LibFunc___kmpc_alloc_shared);
  788. TLI.setAvailable(llvm::LibFunc___kmpc_free_shared);
  789. } else {
  790. TLI.setUnavailable(LibFunc_nvvm_reflect);
  791. }
  792. // These vec_malloc/free routines are only available on AIX.
  793. if (!T.isOSAIX()) {
  794. TLI.setUnavailable(LibFunc_vec_calloc);
  795. TLI.setUnavailable(LibFunc_vec_malloc);
  796. TLI.setUnavailable(LibFunc_vec_realloc);
  797. TLI.setUnavailable(LibFunc_vec_free);
  798. }
  799. TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary, T);
  800. }
  801. TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
  802. // Default to everything being available.
  803. memset(AvailableArray, -1, sizeof(AvailableArray));
  804. initialize(*this, Triple(), StandardNames);
  805. }
  806. TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
  807. // Default to everything being available.
  808. memset(AvailableArray, -1, sizeof(AvailableArray));
  809. initialize(*this, T, StandardNames);
  810. }
  811. TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
  812. : CustomNames(TLI.CustomNames), ShouldExtI32Param(TLI.ShouldExtI32Param),
  813. ShouldExtI32Return(TLI.ShouldExtI32Return),
  814. ShouldSignExtI32Param(TLI.ShouldSignExtI32Param),
  815. ShouldSignExtI32Return(TLI.ShouldSignExtI32Return),
  816. SizeOfInt(TLI.SizeOfInt) {
  817. memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
  818. VectorDescs = TLI.VectorDescs;
  819. ScalarDescs = TLI.ScalarDescs;
  820. }
  821. TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
  822. : CustomNames(std::move(TLI.CustomNames)),
  823. ShouldExtI32Param(TLI.ShouldExtI32Param),
  824. ShouldExtI32Return(TLI.ShouldExtI32Return),
  825. ShouldSignExtI32Param(TLI.ShouldSignExtI32Param),
  826. ShouldSignExtI32Return(TLI.ShouldSignExtI32Return),
  827. SizeOfInt(TLI.SizeOfInt) {
  828. std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
  829. AvailableArray);
  830. VectorDescs = TLI.VectorDescs;
  831. ScalarDescs = TLI.ScalarDescs;
  832. }
  833. TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
  834. CustomNames = TLI.CustomNames;
  835. ShouldExtI32Param = TLI.ShouldExtI32Param;
  836. ShouldExtI32Return = TLI.ShouldExtI32Return;
  837. ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
  838. ShouldSignExtI32Return = TLI.ShouldSignExtI32Return;
  839. SizeOfInt = TLI.SizeOfInt;
  840. memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
  841. return *this;
  842. }
  843. TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
  844. CustomNames = std::move(TLI.CustomNames);
  845. ShouldExtI32Param = TLI.ShouldExtI32Param;
  846. ShouldExtI32Return = TLI.ShouldExtI32Return;
  847. ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
  848. ShouldSignExtI32Return = TLI.ShouldSignExtI32Return;
  849. SizeOfInt = TLI.SizeOfInt;
  850. std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
  851. AvailableArray);
  852. return *this;
  853. }
  854. static StringRef sanitizeFunctionName(StringRef funcName) {
  855. // Filter out empty names and names containing null bytes, those can't be in
  856. // our table.
  857. if (funcName.empty() || funcName.contains('\0'))
  858. return StringRef();
  859. // Check for \01 prefix that is used to mangle __asm declarations and
  860. // strip it if present.
  861. return GlobalValue::dropLLVMManglingEscape(funcName);
  862. }
  863. bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName, LibFunc &F) const {
  864. funcName = sanitizeFunctionName(funcName);
  865. if (funcName.empty())
  866. return false;
  867. const auto *Start = std::begin(StandardNames);
  868. const auto *End = std::end(StandardNames);
  869. const auto *I = std::lower_bound(Start, End, funcName);
  870. if (I != End && *I == funcName) {
  871. F = (LibFunc)(I - Start);
  872. return true;
  873. }
  874. return false;
  875. }
  876. // Return true if ArgTy matches Ty.
  877. static bool matchType(FuncArgTypeID ArgTy, const Type *Ty, unsigned IntBits,
  878. unsigned SizeTBits) {
  879. switch (ArgTy) {
  880. case Void:
  881. return Ty->isVoidTy();
  882. case Bool:
  883. return Ty->isIntegerTy(8);
  884. case Int16:
  885. return Ty->isIntegerTy(16);
  886. case Int32:
  887. return Ty->isIntegerTy(32);
  888. case Int:
  889. return Ty->isIntegerTy(IntBits);
  890. case IntPlus:
  891. return Ty->isIntegerTy() && Ty->getPrimitiveSizeInBits() >= IntBits;
  892. case IntX:
  893. return Ty->isIntegerTy();
  894. case Long:
  895. // TODO: Figure out and use long size.
  896. return Ty->isIntegerTy() && Ty->getPrimitiveSizeInBits() >= IntBits;
  897. case Int64:
  898. return Ty->isIntegerTy(64);
  899. case LLong:
  900. return Ty->isIntegerTy(64);
  901. case SizeT:
  902. case SSizeT:
  903. return Ty->isIntegerTy(SizeTBits);
  904. case Flt:
  905. return Ty->isFloatTy();
  906. case Dbl:
  907. return Ty->isDoubleTy();
  908. // TODO: Tighten this up.
  909. case LDbl:
  910. return Ty->isFloatingPointTy();
  911. case Floating:
  912. return Ty->isFloatingPointTy();
  913. case Ptr:
  914. return Ty->isPointerTy();
  915. case Struct:
  916. return Ty->isStructTy();
  917. default:
  918. break;
  919. }
  920. llvm_unreachable("Invalid type");
  921. }
  922. bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
  923. LibFunc F,
  924. const Module &M) const {
  925. unsigned NumParams = FTy.getNumParams();
  926. switch (F) {
  927. // Special handling for <complex.h> functions:
  928. case LibFunc_cabs:
  929. case LibFunc_cabsf:
  930. case LibFunc_cabsl: {
  931. Type *RetTy = FTy.getReturnType();
  932. if (!RetTy->isFloatingPointTy())
  933. return false;
  934. Type *ParamTy = FTy.getParamType(0);
  935. // NOTE: These prototypes are target specific and currently support
  936. // "complex" passed as an array or discrete real & imaginary parameters.
  937. // Add other calling conventions to enable libcall optimizations.
  938. if (NumParams == 1)
  939. return (ParamTy->isArrayTy() && ParamTy->getArrayNumElements() == 2 &&
  940. ParamTy->getArrayElementType() == RetTy);
  941. else if (NumParams == 2)
  942. return ParamTy == RetTy && FTy.getParamType(1) == RetTy;
  943. return false;
  944. }
  945. // Special handling for the sincospi functions that return either
  946. // a struct or vector:
  947. case LibFunc_sincospi_stret:
  948. case LibFunc_sincospif_stret: {
  949. if (NumParams != 1)
  950. return false;
  951. Type *RetTy = FTy.getReturnType();
  952. Type *ParamTy = FTy.getParamType(0);
  953. if (auto *Ty = dyn_cast<StructType>(RetTy)) {
  954. if (Ty->getNumElements() != 2)
  955. return false;
  956. return (Ty->getElementType(0) == ParamTy &&
  957. Ty->getElementType(1) == ParamTy);
  958. }
  959. if (auto *Ty = dyn_cast<FixedVectorType>(RetTy)) {
  960. if (Ty->getNumElements() != 2)
  961. return false;
  962. return Ty->getElementType() == ParamTy;
  963. }
  964. return false;
  965. }
  966. default:
  967. break;
  968. }
  969. unsigned IntBits = getIntSize();
  970. unsigned SizeTBits = getSizeTSize(M);
  971. unsigned Idx = 0;
  972. // Iterate over the type ids in the function prototype, matching each
  973. // against the function's type FTy, starting with its return type.
  974. // Return true if both match in number and kind, inclduing the ellipsis.
  975. Type *Ty = FTy.getReturnType(), *LastTy = Ty;
  976. const auto &ProtoTypes = Signatures[F];
  977. for (auto TyID : ProtoTypes) {
  978. if (Idx && TyID == Void)
  979. // Except in the first position where it designates the function's
  980. // return type Void ends the argument list.
  981. break;
  982. if (TyID == Ellip) {
  983. // The ellipsis ends the protoype list but is not a part of FTy's
  984. // argument list. Except when it's last it must be followed by
  985. // Void.
  986. assert(Idx == ProtoTypes.size() - 1 || ProtoTypes[Idx + 1] == Void);
  987. return FTy.isFunctionVarArg();
  988. }
  989. if (TyID == Same) {
  990. assert(Idx != 0 && "Type ID 'Same' must not be first!");
  991. if (Ty != LastTy)
  992. return false;
  993. } else {
  994. if (!Ty || !matchType(TyID, Ty, IntBits, SizeTBits))
  995. return false;
  996. LastTy = Ty;
  997. }
  998. if (Idx == NumParams) {
  999. // There's at least one and at most two more type ids than there are
  1000. // arguments in FTy's argument list.
  1001. Ty = nullptr;
  1002. ++Idx;
  1003. continue;
  1004. }
  1005. Ty = FTy.getParamType(Idx++);
  1006. }
  1007. // Return success only if all entries on both lists have been processed
  1008. // and the function is not a variadic one.
  1009. return Idx == NumParams + 1 && !FTy.isFunctionVarArg();
  1010. }
  1011. bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
  1012. LibFunc &F) const {
  1013. // Intrinsics don't overlap w/libcalls; if our module has a large number of
  1014. // intrinsics, this ends up being an interesting compile time win since we
  1015. // avoid string normalization and comparison.
  1016. if (FDecl.isIntrinsic()) return false;
  1017. const Module *M = FDecl.getParent();
  1018. assert(M && "Expecting FDecl to be connected to a Module.");
  1019. return getLibFunc(FDecl.getName(), F) &&
  1020. isValidProtoForLibFunc(*FDecl.getFunctionType(), F, *M);
  1021. }
  1022. void TargetLibraryInfoImpl::disableAllFunctions() {
  1023. memset(AvailableArray, 0, sizeof(AvailableArray));
  1024. }
  1025. static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
  1026. return LHS.ScalarFnName < RHS.ScalarFnName;
  1027. }
  1028. static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
  1029. return LHS.VectorFnName < RHS.VectorFnName;
  1030. }
  1031. static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
  1032. return LHS.ScalarFnName < S;
  1033. }
  1034. void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
  1035. llvm::append_range(VectorDescs, Fns);
  1036. llvm::sort(VectorDescs, compareByScalarFnName);
  1037. llvm::append_range(ScalarDescs, Fns);
  1038. llvm::sort(ScalarDescs, compareByVectorFnName);
  1039. }
  1040. void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
  1041. enum VectorLibrary VecLib, const llvm::Triple &TargetTriple) {
  1042. switch (VecLib) {
  1043. case Accelerate: {
  1044. const VecDesc VecFuncs[] = {
  1045. #define TLI_DEFINE_ACCELERATE_VECFUNCS
  1046. #include "llvm/Analysis/VecFuncs.def"
  1047. };
  1048. addVectorizableFunctions(VecFuncs);
  1049. break;
  1050. }
  1051. case DarwinLibSystemM: {
  1052. const VecDesc VecFuncs[] = {
  1053. #define TLI_DEFINE_DARWIN_LIBSYSTEM_M_VECFUNCS
  1054. #include "llvm/Analysis/VecFuncs.def"
  1055. };
  1056. addVectorizableFunctions(VecFuncs);
  1057. break;
  1058. }
  1059. case LIBMVEC_X86: {
  1060. const VecDesc VecFuncs[] = {
  1061. #define TLI_DEFINE_LIBMVEC_X86_VECFUNCS
  1062. #include "llvm/Analysis/VecFuncs.def"
  1063. };
  1064. addVectorizableFunctions(VecFuncs);
  1065. break;
  1066. }
  1067. case MASSV: {
  1068. const VecDesc VecFuncs[] = {
  1069. #define TLI_DEFINE_MASSV_VECFUNCS
  1070. #include "llvm/Analysis/VecFuncs.def"
  1071. };
  1072. addVectorizableFunctions(VecFuncs);
  1073. break;
  1074. }
  1075. case SVML: {
  1076. const VecDesc VecFuncs[] = {
  1077. #define TLI_DEFINE_SVML_VECFUNCS
  1078. #include "llvm/Analysis/VecFuncs.def"
  1079. };
  1080. addVectorizableFunctions(VecFuncs);
  1081. break;
  1082. }
  1083. case SLEEFGNUABI: {
  1084. const VecDesc VecFuncs_VF2[] = {
  1085. #define TLI_DEFINE_SLEEFGNUABI_VF2_VECFUNCS
  1086. #include "llvm/Analysis/VecFuncs.def"
  1087. };
  1088. const VecDesc VecFuncs_VF4[] = {
  1089. #define TLI_DEFINE_SLEEFGNUABI_VF4_VECFUNCS
  1090. #include "llvm/Analysis/VecFuncs.def"
  1091. };
  1092. switch (TargetTriple.getArch()) {
  1093. default:
  1094. break;
  1095. case llvm::Triple::aarch64:
  1096. case llvm::Triple::aarch64_be:
  1097. addVectorizableFunctions(VecFuncs_VF2);
  1098. addVectorizableFunctions(VecFuncs_VF4);
  1099. break;
  1100. }
  1101. break;
  1102. }
  1103. case NoLibrary:
  1104. break;
  1105. }
  1106. }
  1107. bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
  1108. funcName = sanitizeFunctionName(funcName);
  1109. if (funcName.empty())
  1110. return false;
  1111. std::vector<VecDesc>::const_iterator I =
  1112. llvm::lower_bound(VectorDescs, funcName, compareWithScalarFnName);
  1113. return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
  1114. }
  1115. StringRef
  1116. TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
  1117. const ElementCount &VF) const {
  1118. F = sanitizeFunctionName(F);
  1119. if (F.empty())
  1120. return F;
  1121. std::vector<VecDesc>::const_iterator I =
  1122. llvm::lower_bound(VectorDescs, F, compareWithScalarFnName);
  1123. while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
  1124. if (I->VectorizationFactor == VF)
  1125. return I->VectorFnName;
  1126. ++I;
  1127. }
  1128. return StringRef();
  1129. }
  1130. TargetLibraryInfo TargetLibraryAnalysis::run(const Function &F,
  1131. FunctionAnalysisManager &) {
  1132. if (!BaselineInfoImpl)
  1133. BaselineInfoImpl =
  1134. TargetLibraryInfoImpl(Triple(F.getParent()->getTargetTriple()));
  1135. return TargetLibraryInfo(*BaselineInfoImpl, &F);
  1136. }
  1137. unsigned TargetLibraryInfoImpl::getWCharSize(const Module &M) const {
  1138. if (auto *ShortWChar = cast_or_null<ConstantAsMetadata>(
  1139. M.getModuleFlag("wchar_size")))
  1140. return cast<ConstantInt>(ShortWChar->getValue())->getZExtValue();
  1141. return 0;
  1142. }
  1143. unsigned TargetLibraryInfoImpl::getSizeTSize(const Module &M) const {
  1144. // There is really no guarantee that sizeof(size_t) is equal to sizeof(int*).
  1145. // If that isn't true then it should be possible to derive the SizeTTy from
  1146. // the target triple here instead and do an early return.
  1147. // Historically LLVM assume that size_t has same size as intptr_t (hence
  1148. // deriving the size from sizeof(int*) in address space zero). This should
  1149. // work for most targets. For future consideration: DataLayout also implement
  1150. // getIndexSizeInBits which might map better to size_t compared to
  1151. // getPointerSizeInBits. Hard coding address space zero here might be
  1152. // unfortunate as well. Maybe getDefaultGlobalsAddressSpace() or
  1153. // getAllocaAddrSpace() is better.
  1154. unsigned AddressSpace = 0;
  1155. return M.getDataLayout().getPointerSizeInBits(AddressSpace);
  1156. }
  1157. TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
  1158. : ImmutablePass(ID), TLA(TargetLibraryInfoImpl()) {
  1159. initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  1160. }
  1161. TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
  1162. : ImmutablePass(ID), TLA(TargetLibraryInfoImpl(T)) {
  1163. initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  1164. }
  1165. TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
  1166. const TargetLibraryInfoImpl &TLIImpl)
  1167. : ImmutablePass(ID), TLA(TLIImpl) {
  1168. initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  1169. }
  1170. AnalysisKey TargetLibraryAnalysis::Key;
  1171. // Register the basic pass.
  1172. INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
  1173. "Target Library Information", false, true)
  1174. char TargetLibraryInfoWrapperPass::ID = 0;
  1175. void TargetLibraryInfoWrapperPass::anchor() {}
  1176. void TargetLibraryInfoImpl::getWidestVF(StringRef ScalarF,
  1177. ElementCount &FixedVF,
  1178. ElementCount &ScalableVF) const {
  1179. ScalarF = sanitizeFunctionName(ScalarF);
  1180. // Use '0' here because a type of the form <vscale x 1 x ElTy> is not the
  1181. // same as a scalar.
  1182. ScalableVF = ElementCount::getScalable(0);
  1183. FixedVF = ElementCount::getFixed(1);
  1184. if (ScalarF.empty())
  1185. return;
  1186. std::vector<VecDesc>::const_iterator I =
  1187. llvm::lower_bound(VectorDescs, ScalarF, compareWithScalarFnName);
  1188. while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == ScalarF) {
  1189. ElementCount *VF =
  1190. I->VectorizationFactor.isScalable() ? &ScalableVF : &FixedVF;
  1191. if (ElementCount::isKnownGT(I->VectorizationFactor, *VF))
  1192. *VF = I->VectorizationFactor;
  1193. ++I;
  1194. }
  1195. }