TargetLibraryInfo.cpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  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. StringLiteral const TargetLibraryInfoImpl::StandardNames[LibFunc::NumLibFuncs] =
  34. {
  35. #define TLI_DEFINE_STRING
  36. #include "llvm/Analysis/TargetLibraryInfo.def"
  37. };
  38. static bool hasSinCosPiStret(const Triple &T) {
  39. // Only Darwin variants have _stret versions of combined trig functions.
  40. if (!T.isOSDarwin())
  41. return false;
  42. // The ABI is rather complicated on x86, so don't do anything special there.
  43. if (T.getArch() == Triple::x86)
  44. return false;
  45. if (T.isMacOSX() && T.isMacOSXVersionLT(10, 9))
  46. return false;
  47. if (T.isiOS() && T.isOSVersionLT(7, 0))
  48. return false;
  49. return true;
  50. }
  51. static bool hasBcmp(const Triple &TT) {
  52. // Posix removed support from bcmp() in 2001, but the glibc and several
  53. // implementations of the libc still have it.
  54. if (TT.isOSLinux())
  55. return TT.isGNUEnvironment() || TT.isMusl();
  56. // Both NetBSD and OpenBSD are planning to remove the function. Windows does
  57. // not have it.
  58. return TT.isOSFreeBSD() || TT.isOSSolaris();
  59. }
  60. static bool isCallingConvCCompatible(CallingConv::ID CC, StringRef TT,
  61. FunctionType *FuncTy) {
  62. switch (CC) {
  63. default:
  64. return false;
  65. case llvm::CallingConv::C:
  66. return true;
  67. case llvm::CallingConv::ARM_APCS:
  68. case llvm::CallingConv::ARM_AAPCS:
  69. case llvm::CallingConv::ARM_AAPCS_VFP: {
  70. // The iOS ABI diverges from the standard in some cases, so for now don't
  71. // try to simplify those calls.
  72. if (Triple(TT).isiOS())
  73. return false;
  74. if (!FuncTy->getReturnType()->isPointerTy() &&
  75. !FuncTy->getReturnType()->isIntegerTy() &&
  76. !FuncTy->getReturnType()->isVoidTy())
  77. return false;
  78. for (auto *Param : FuncTy->params()) {
  79. if (!Param->isPointerTy() && !Param->isIntegerTy())
  80. return false;
  81. }
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. bool TargetLibraryInfoImpl::isCallingConvCCompatible(CallBase *CI) {
  88. return ::isCallingConvCCompatible(CI->getCallingConv(),
  89. CI->getModule()->getTargetTriple(),
  90. CI->getFunctionType());
  91. }
  92. bool TargetLibraryInfoImpl::isCallingConvCCompatible(Function *F) {
  93. return ::isCallingConvCCompatible(F->getCallingConv(),
  94. F->getParent()->getTargetTriple(),
  95. F->getFunctionType());
  96. }
  97. /// Initialize the set of available library functions based on the specified
  98. /// target triple. This should be carefully written so that a missing target
  99. /// triple gets a sane set of defaults.
  100. static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T,
  101. ArrayRef<StringLiteral> StandardNames) {
  102. // Verify that the StandardNames array is in alphabetical order.
  103. assert(
  104. llvm::is_sorted(StandardNames,
  105. [](StringRef LHS, StringRef RHS) { return LHS < RHS; }) &&
  106. "TargetLibraryInfoImpl function names must be sorted");
  107. // Set IO unlocked variants as unavailable
  108. // Set them as available per system below
  109. TLI.setUnavailable(LibFunc_getc_unlocked);
  110. TLI.setUnavailable(LibFunc_getchar_unlocked);
  111. TLI.setUnavailable(LibFunc_putc_unlocked);
  112. TLI.setUnavailable(LibFunc_putchar_unlocked);
  113. TLI.setUnavailable(LibFunc_fputc_unlocked);
  114. TLI.setUnavailable(LibFunc_fgetc_unlocked);
  115. TLI.setUnavailable(LibFunc_fread_unlocked);
  116. TLI.setUnavailable(LibFunc_fwrite_unlocked);
  117. TLI.setUnavailable(LibFunc_fputs_unlocked);
  118. TLI.setUnavailable(LibFunc_fgets_unlocked);
  119. bool ShouldExtI32Param = false, ShouldExtI32Return = false,
  120. ShouldSignExtI32Param = false;
  121. // PowerPC64, Sparc64, SystemZ need signext/zeroext on i32 parameters and
  122. // returns corresponding to C-level ints and unsigned ints.
  123. if (T.isPPC64() || T.getArch() == Triple::sparcv9 ||
  124. T.getArch() == Triple::systemz) {
  125. ShouldExtI32Param = true;
  126. ShouldExtI32Return = true;
  127. }
  128. // Mips, on the other hand, needs signext on i32 parameters corresponding
  129. // to both signed and unsigned ints.
  130. if (T.isMIPS()) {
  131. ShouldSignExtI32Param = true;
  132. }
  133. TLI.setShouldExtI32Param(ShouldExtI32Param);
  134. TLI.setShouldExtI32Return(ShouldExtI32Return);
  135. TLI.setShouldSignExtI32Param(ShouldSignExtI32Param);
  136. // Let's assume by default that the size of int is 32 bits, unless the target
  137. // is a 16-bit architecture because then it most likely is 16 bits. If that
  138. // isn't true for a target those defaults should be overridden below.
  139. TLI.setIntSize(T.isArch16Bit() ? 16 : 32);
  140. // There is really no runtime library on AMDGPU, apart from
  141. // __kmpc_alloc/free_shared.
  142. if (T.isAMDGPU()) {
  143. TLI.disableAllFunctions();
  144. TLI.setAvailable(llvm::LibFunc___kmpc_alloc_shared);
  145. TLI.setAvailable(llvm::LibFunc___kmpc_free_shared);
  146. return;
  147. }
  148. // memset_pattern{4,8,16} is only available on iOS 3.0 and Mac OS X 10.5 and
  149. // later. All versions of watchOS support it.
  150. if (T.isMacOSX()) {
  151. // available IO unlocked variants on Mac OS X
  152. TLI.setAvailable(LibFunc_getc_unlocked);
  153. TLI.setAvailable(LibFunc_getchar_unlocked);
  154. TLI.setAvailable(LibFunc_putc_unlocked);
  155. TLI.setAvailable(LibFunc_putchar_unlocked);
  156. if (T.isMacOSXVersionLT(10, 5)) {
  157. TLI.setUnavailable(LibFunc_memset_pattern4);
  158. TLI.setUnavailable(LibFunc_memset_pattern8);
  159. TLI.setUnavailable(LibFunc_memset_pattern16);
  160. }
  161. } else if (T.isiOS()) {
  162. if (T.isOSVersionLT(3, 0)) {
  163. TLI.setUnavailable(LibFunc_memset_pattern4);
  164. TLI.setUnavailable(LibFunc_memset_pattern8);
  165. TLI.setUnavailable(LibFunc_memset_pattern16);
  166. }
  167. } else if (!T.isWatchOS()) {
  168. TLI.setUnavailable(LibFunc_memset_pattern4);
  169. TLI.setUnavailable(LibFunc_memset_pattern8);
  170. TLI.setUnavailable(LibFunc_memset_pattern16);
  171. }
  172. if (!hasSinCosPiStret(T)) {
  173. TLI.setUnavailable(LibFunc_sinpi);
  174. TLI.setUnavailable(LibFunc_sinpif);
  175. TLI.setUnavailable(LibFunc_cospi);
  176. TLI.setUnavailable(LibFunc_cospif);
  177. TLI.setUnavailable(LibFunc_sincospi_stret);
  178. TLI.setUnavailable(LibFunc_sincospif_stret);
  179. }
  180. if (!hasBcmp(T))
  181. TLI.setUnavailable(LibFunc_bcmp);
  182. if (T.isMacOSX() && T.getArch() == Triple::x86 &&
  183. !T.isMacOSXVersionLT(10, 7)) {
  184. // x86-32 OSX has a scheme where fwrite and fputs (and some other functions
  185. // we don't care about) have two versions; on recent OSX, the one we want
  186. // has a $UNIX2003 suffix. The two implementations are identical except
  187. // for the return value in some edge cases. However, we don't want to
  188. // generate code that depends on the old symbols.
  189. TLI.setAvailableWithName(LibFunc_fwrite, "fwrite$UNIX2003");
  190. TLI.setAvailableWithName(LibFunc_fputs, "fputs$UNIX2003");
  191. }
  192. // iprintf and friends are only available on XCore, TCE, and Emscripten.
  193. if (T.getArch() != Triple::xcore && T.getArch() != Triple::tce &&
  194. T.getOS() != Triple::Emscripten) {
  195. TLI.setUnavailable(LibFunc_iprintf);
  196. TLI.setUnavailable(LibFunc_siprintf);
  197. TLI.setUnavailable(LibFunc_fiprintf);
  198. }
  199. // __small_printf and friends are only available on Emscripten.
  200. if (T.getOS() != Triple::Emscripten) {
  201. TLI.setUnavailable(LibFunc_small_printf);
  202. TLI.setUnavailable(LibFunc_small_sprintf);
  203. TLI.setUnavailable(LibFunc_small_fprintf);
  204. }
  205. if (T.isOSWindows() && !T.isOSCygMing()) {
  206. // XXX: The earliest documentation available at the moment is for VS2015/VC19:
  207. // https://docs.microsoft.com/en-us/cpp/c-runtime-library/floating-point-support?view=vs-2015
  208. // XXX: In order to use an MSVCRT older than VC19,
  209. // the specific library version must be explicit in the target triple,
  210. // e.g., x86_64-pc-windows-msvc18.
  211. bool hasPartialC99 = true;
  212. if (T.isKnownWindowsMSVCEnvironment()) {
  213. VersionTuple Version = T.getEnvironmentVersion();
  214. hasPartialC99 = (Version.getMajor() == 0 || Version.getMajor() >= 19);
  215. }
  216. // Latest targets support C89 math functions, in part.
  217. bool isARM = (T.getArch() == Triple::aarch64 ||
  218. T.getArch() == Triple::arm);
  219. bool hasPartialFloat = (isARM ||
  220. T.getArch() == Triple::x86_64);
  221. // Win32 does not support float C89 math functions, in general.
  222. if (!hasPartialFloat) {
  223. TLI.setUnavailable(LibFunc_acosf);
  224. TLI.setUnavailable(LibFunc_asinf);
  225. TLI.setUnavailable(LibFunc_atan2f);
  226. TLI.setUnavailable(LibFunc_atanf);
  227. TLI.setUnavailable(LibFunc_ceilf);
  228. TLI.setUnavailable(LibFunc_cosf);
  229. TLI.setUnavailable(LibFunc_coshf);
  230. TLI.setUnavailable(LibFunc_expf);
  231. TLI.setUnavailable(LibFunc_floorf);
  232. TLI.setUnavailable(LibFunc_fmodf);
  233. TLI.setUnavailable(LibFunc_log10f);
  234. TLI.setUnavailable(LibFunc_logf);
  235. TLI.setUnavailable(LibFunc_modff);
  236. TLI.setUnavailable(LibFunc_powf);
  237. TLI.setUnavailable(LibFunc_remainderf);
  238. TLI.setUnavailable(LibFunc_sinf);
  239. TLI.setUnavailable(LibFunc_sinhf);
  240. TLI.setUnavailable(LibFunc_sqrtf);
  241. TLI.setUnavailable(LibFunc_tanf);
  242. TLI.setUnavailable(LibFunc_tanhf);
  243. }
  244. if (!isARM)
  245. TLI.setUnavailable(LibFunc_fabsf);
  246. TLI.setUnavailable(LibFunc_frexpf);
  247. TLI.setUnavailable(LibFunc_ldexpf);
  248. // Win32 does not support long double C89 math functions.
  249. TLI.setUnavailable(LibFunc_acosl);
  250. TLI.setUnavailable(LibFunc_asinl);
  251. TLI.setUnavailable(LibFunc_atan2l);
  252. TLI.setUnavailable(LibFunc_atanl);
  253. TLI.setUnavailable(LibFunc_ceill);
  254. TLI.setUnavailable(LibFunc_cosl);
  255. TLI.setUnavailable(LibFunc_coshl);
  256. TLI.setUnavailable(LibFunc_expl);
  257. TLI.setUnavailable(LibFunc_fabsl);
  258. TLI.setUnavailable(LibFunc_floorl);
  259. TLI.setUnavailable(LibFunc_fmodl);
  260. TLI.setUnavailable(LibFunc_frexpl);
  261. TLI.setUnavailable(LibFunc_ldexpl);
  262. TLI.setUnavailable(LibFunc_log10l);
  263. TLI.setUnavailable(LibFunc_logl);
  264. TLI.setUnavailable(LibFunc_modfl);
  265. TLI.setUnavailable(LibFunc_powl);
  266. TLI.setUnavailable(LibFunc_remainderl);
  267. TLI.setUnavailable(LibFunc_sinl);
  268. TLI.setUnavailable(LibFunc_sinhl);
  269. TLI.setUnavailable(LibFunc_sqrtl);
  270. TLI.setUnavailable(LibFunc_tanl);
  271. TLI.setUnavailable(LibFunc_tanhl);
  272. // Win32 does not fully support C99 math functions.
  273. if (!hasPartialC99) {
  274. TLI.setUnavailable(LibFunc_acosh);
  275. TLI.setUnavailable(LibFunc_acoshf);
  276. TLI.setUnavailable(LibFunc_asinh);
  277. TLI.setUnavailable(LibFunc_asinhf);
  278. TLI.setUnavailable(LibFunc_atanh);
  279. TLI.setUnavailable(LibFunc_atanhf);
  280. TLI.setAvailableWithName(LibFunc_cabs, "_cabs");
  281. TLI.setUnavailable(LibFunc_cabsf);
  282. TLI.setUnavailable(LibFunc_cbrt);
  283. TLI.setUnavailable(LibFunc_cbrtf);
  284. TLI.setAvailableWithName(LibFunc_copysign, "_copysign");
  285. TLI.setAvailableWithName(LibFunc_copysignf, "_copysignf");
  286. TLI.setUnavailable(LibFunc_exp2);
  287. TLI.setUnavailable(LibFunc_exp2f);
  288. TLI.setUnavailable(LibFunc_expm1);
  289. TLI.setUnavailable(LibFunc_expm1f);
  290. TLI.setUnavailable(LibFunc_fmax);
  291. TLI.setUnavailable(LibFunc_fmaxf);
  292. TLI.setUnavailable(LibFunc_fmin);
  293. TLI.setUnavailable(LibFunc_fminf);
  294. TLI.setUnavailable(LibFunc_log1p);
  295. TLI.setUnavailable(LibFunc_log1pf);
  296. TLI.setUnavailable(LibFunc_log2);
  297. TLI.setUnavailable(LibFunc_log2f);
  298. TLI.setAvailableWithName(LibFunc_logb, "_logb");
  299. if (hasPartialFloat)
  300. TLI.setAvailableWithName(LibFunc_logbf, "_logbf");
  301. else
  302. TLI.setUnavailable(LibFunc_logbf);
  303. TLI.setUnavailable(LibFunc_rint);
  304. TLI.setUnavailable(LibFunc_rintf);
  305. TLI.setUnavailable(LibFunc_round);
  306. TLI.setUnavailable(LibFunc_roundf);
  307. TLI.setUnavailable(LibFunc_trunc);
  308. TLI.setUnavailable(LibFunc_truncf);
  309. }
  310. // Win32 does not support long double C99 math functions.
  311. TLI.setUnavailable(LibFunc_acoshl);
  312. TLI.setUnavailable(LibFunc_asinhl);
  313. TLI.setUnavailable(LibFunc_atanhl);
  314. TLI.setUnavailable(LibFunc_cabsl);
  315. TLI.setUnavailable(LibFunc_cbrtl);
  316. TLI.setUnavailable(LibFunc_copysignl);
  317. TLI.setUnavailable(LibFunc_exp2l);
  318. TLI.setUnavailable(LibFunc_expm1l);
  319. TLI.setUnavailable(LibFunc_fmaxl);
  320. TLI.setUnavailable(LibFunc_fminl);
  321. TLI.setUnavailable(LibFunc_log1pl);
  322. TLI.setUnavailable(LibFunc_log2l);
  323. TLI.setUnavailable(LibFunc_logbl);
  324. TLI.setUnavailable(LibFunc_nearbyintl);
  325. TLI.setUnavailable(LibFunc_rintl);
  326. TLI.setUnavailable(LibFunc_roundl);
  327. TLI.setUnavailable(LibFunc_truncl);
  328. // Win32 does not support these functions, but
  329. // they are generally available on POSIX-compliant systems.
  330. TLI.setUnavailable(LibFunc_access);
  331. TLI.setUnavailable(LibFunc_chmod);
  332. TLI.setUnavailable(LibFunc_closedir);
  333. TLI.setUnavailable(LibFunc_fdopen);
  334. TLI.setUnavailable(LibFunc_fileno);
  335. TLI.setUnavailable(LibFunc_fseeko);
  336. TLI.setUnavailable(LibFunc_fstat);
  337. TLI.setUnavailable(LibFunc_ftello);
  338. TLI.setUnavailable(LibFunc_gettimeofday);
  339. TLI.setUnavailable(LibFunc_memccpy);
  340. TLI.setUnavailable(LibFunc_mkdir);
  341. TLI.setUnavailable(LibFunc_open);
  342. TLI.setUnavailable(LibFunc_opendir);
  343. TLI.setUnavailable(LibFunc_pclose);
  344. TLI.setUnavailable(LibFunc_popen);
  345. TLI.setUnavailable(LibFunc_read);
  346. TLI.setUnavailable(LibFunc_rmdir);
  347. TLI.setUnavailable(LibFunc_stat);
  348. TLI.setUnavailable(LibFunc_strcasecmp);
  349. TLI.setUnavailable(LibFunc_strncasecmp);
  350. TLI.setUnavailable(LibFunc_unlink);
  351. TLI.setUnavailable(LibFunc_utime);
  352. TLI.setUnavailable(LibFunc_write);
  353. }
  354. if (T.isOSWindows() && !T.isWindowsCygwinEnvironment()) {
  355. // These functions aren't available in either MSVC or MinGW environments.
  356. TLI.setUnavailable(LibFunc_bcmp);
  357. TLI.setUnavailable(LibFunc_bcopy);
  358. TLI.setUnavailable(LibFunc_bzero);
  359. TLI.setUnavailable(LibFunc_chown);
  360. TLI.setUnavailable(LibFunc_ctermid);
  361. TLI.setUnavailable(LibFunc_ffs);
  362. TLI.setUnavailable(LibFunc_flockfile);
  363. TLI.setUnavailable(LibFunc_fstatvfs);
  364. TLI.setUnavailable(LibFunc_ftrylockfile);
  365. TLI.setUnavailable(LibFunc_funlockfile);
  366. TLI.setUnavailable(LibFunc_getitimer);
  367. TLI.setUnavailable(LibFunc_getlogin_r);
  368. TLI.setUnavailable(LibFunc_getpwnam);
  369. TLI.setUnavailable(LibFunc_htonl);
  370. TLI.setUnavailable(LibFunc_htons);
  371. TLI.setUnavailable(LibFunc_lchown);
  372. TLI.setUnavailable(LibFunc_lstat);
  373. TLI.setUnavailable(LibFunc_ntohl);
  374. TLI.setUnavailable(LibFunc_ntohs);
  375. TLI.setUnavailable(LibFunc_pread);
  376. TLI.setUnavailable(LibFunc_pwrite);
  377. TLI.setUnavailable(LibFunc_readlink);
  378. TLI.setUnavailable(LibFunc_realpath);
  379. TLI.setUnavailable(LibFunc_setitimer);
  380. TLI.setUnavailable(LibFunc_statvfs);
  381. TLI.setUnavailable(LibFunc_stpcpy);
  382. TLI.setUnavailable(LibFunc_stpncpy);
  383. TLI.setUnavailable(LibFunc_times);
  384. TLI.setUnavailable(LibFunc_uname);
  385. TLI.setUnavailable(LibFunc_unsetenv);
  386. TLI.setUnavailable(LibFunc_utimes);
  387. }
  388. // Pick just one set of new/delete variants.
  389. if (T.isOSMSVCRT()) {
  390. // MSVC, doesn't have the Itanium new/delete.
  391. TLI.setUnavailable(LibFunc_ZdaPv);
  392. TLI.setUnavailable(LibFunc_ZdaPvRKSt9nothrow_t);
  393. TLI.setUnavailable(LibFunc_ZdaPvSt11align_val_t);
  394. TLI.setUnavailable(LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t);
  395. TLI.setUnavailable(LibFunc_ZdaPvj);
  396. TLI.setUnavailable(LibFunc_ZdaPvjSt11align_val_t);
  397. TLI.setUnavailable(LibFunc_ZdaPvm);
  398. TLI.setUnavailable(LibFunc_ZdaPvmSt11align_val_t);
  399. TLI.setUnavailable(LibFunc_ZdlPv);
  400. TLI.setUnavailable(LibFunc_ZdlPvRKSt9nothrow_t);
  401. TLI.setUnavailable(LibFunc_ZdlPvSt11align_val_t);
  402. TLI.setUnavailable(LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t);
  403. TLI.setUnavailable(LibFunc_ZdlPvj);
  404. TLI.setUnavailable(LibFunc_ZdlPvjSt11align_val_t);
  405. TLI.setUnavailable(LibFunc_ZdlPvm);
  406. TLI.setUnavailable(LibFunc_ZdlPvmSt11align_val_t);
  407. TLI.setUnavailable(LibFunc_Znaj);
  408. TLI.setUnavailable(LibFunc_ZnajRKSt9nothrow_t);
  409. TLI.setUnavailable(LibFunc_ZnajSt11align_val_t);
  410. TLI.setUnavailable(LibFunc_ZnajSt11align_val_tRKSt9nothrow_t);
  411. TLI.setUnavailable(LibFunc_Znam);
  412. TLI.setUnavailable(LibFunc_ZnamRKSt9nothrow_t);
  413. TLI.setUnavailable(LibFunc_ZnamSt11align_val_t);
  414. TLI.setUnavailable(LibFunc_ZnamSt11align_val_tRKSt9nothrow_t);
  415. TLI.setUnavailable(LibFunc_Znwj);
  416. TLI.setUnavailable(LibFunc_ZnwjRKSt9nothrow_t);
  417. TLI.setUnavailable(LibFunc_ZnwjSt11align_val_t);
  418. TLI.setUnavailable(LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t);
  419. TLI.setUnavailable(LibFunc_Znwm);
  420. TLI.setUnavailable(LibFunc_ZnwmRKSt9nothrow_t);
  421. TLI.setUnavailable(LibFunc_ZnwmSt11align_val_t);
  422. TLI.setUnavailable(LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t);
  423. } else {
  424. // Not MSVC, assume it's Itanium.
  425. TLI.setUnavailable(LibFunc_msvc_new_int);
  426. TLI.setUnavailable(LibFunc_msvc_new_int_nothrow);
  427. TLI.setUnavailable(LibFunc_msvc_new_longlong);
  428. TLI.setUnavailable(LibFunc_msvc_new_longlong_nothrow);
  429. TLI.setUnavailable(LibFunc_msvc_delete_ptr32);
  430. TLI.setUnavailable(LibFunc_msvc_delete_ptr32_nothrow);
  431. TLI.setUnavailable(LibFunc_msvc_delete_ptr32_int);
  432. TLI.setUnavailable(LibFunc_msvc_delete_ptr64);
  433. TLI.setUnavailable(LibFunc_msvc_delete_ptr64_nothrow);
  434. TLI.setUnavailable(LibFunc_msvc_delete_ptr64_longlong);
  435. TLI.setUnavailable(LibFunc_msvc_new_array_int);
  436. TLI.setUnavailable(LibFunc_msvc_new_array_int_nothrow);
  437. TLI.setUnavailable(LibFunc_msvc_new_array_longlong);
  438. TLI.setUnavailable(LibFunc_msvc_new_array_longlong_nothrow);
  439. TLI.setUnavailable(LibFunc_msvc_delete_array_ptr32);
  440. TLI.setUnavailable(LibFunc_msvc_delete_array_ptr32_nothrow);
  441. TLI.setUnavailable(LibFunc_msvc_delete_array_ptr32_int);
  442. TLI.setUnavailable(LibFunc_msvc_delete_array_ptr64);
  443. TLI.setUnavailable(LibFunc_msvc_delete_array_ptr64_nothrow);
  444. TLI.setUnavailable(LibFunc_msvc_delete_array_ptr64_longlong);
  445. }
  446. switch (T.getOS()) {
  447. case Triple::MacOSX:
  448. // exp10 and exp10f are not available on OS X until 10.9 and iOS until 7.0
  449. // and their names are __exp10 and __exp10f. exp10l is not available on
  450. // OS X or iOS.
  451. TLI.setUnavailable(LibFunc_exp10l);
  452. if (T.isMacOSXVersionLT(10, 9)) {
  453. TLI.setUnavailable(LibFunc_exp10);
  454. TLI.setUnavailable(LibFunc_exp10f);
  455. } else {
  456. TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
  457. TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
  458. }
  459. break;
  460. case Triple::IOS:
  461. case Triple::TvOS:
  462. case Triple::WatchOS:
  463. TLI.setUnavailable(LibFunc_exp10l);
  464. if (!T.isWatchOS() &&
  465. (T.isOSVersionLT(7, 0) || (T.isOSVersionLT(9, 0) && T.isX86()))) {
  466. TLI.setUnavailable(LibFunc_exp10);
  467. TLI.setUnavailable(LibFunc_exp10f);
  468. } else {
  469. TLI.setAvailableWithName(LibFunc_exp10, "__exp10");
  470. TLI.setAvailableWithName(LibFunc_exp10f, "__exp10f");
  471. }
  472. break;
  473. case Triple::Linux:
  474. // exp10, exp10f, exp10l is available on Linux (GLIBC) but are extremely
  475. // buggy prior to glibc version 2.18. Until this version is widely deployed
  476. // or we have a reasonable detection strategy, we cannot use exp10 reliably
  477. // on Linux.
  478. //
  479. // Fall through to disable all of them.
  480. LLVM_FALLTHROUGH;
  481. default:
  482. TLI.setUnavailable(LibFunc_exp10);
  483. TLI.setUnavailable(LibFunc_exp10f);
  484. TLI.setUnavailable(LibFunc_exp10l);
  485. }
  486. // ffsl is available on at least Darwin, Mac OS X, iOS, FreeBSD, and
  487. // Linux (GLIBC):
  488. // http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ffsl.3.html
  489. // http://svn.freebsd.org/base/head/lib/libc/string/ffsl.c
  490. // http://www.gnu.org/software/gnulib/manual/html_node/ffsl.html
  491. switch (T.getOS()) {
  492. case Triple::Darwin:
  493. case Triple::MacOSX:
  494. case Triple::IOS:
  495. case Triple::TvOS:
  496. case Triple::WatchOS:
  497. case Triple::FreeBSD:
  498. case Triple::Linux:
  499. break;
  500. default:
  501. TLI.setUnavailable(LibFunc_ffsl);
  502. }
  503. // ffsll is available on at least FreeBSD and Linux (GLIBC):
  504. // http://svn.freebsd.org/base/head/lib/libc/string/ffsll.c
  505. // http://www.gnu.org/software/gnulib/manual/html_node/ffsll.html
  506. switch (T.getOS()) {
  507. case Triple::Darwin:
  508. case Triple::MacOSX:
  509. case Triple::IOS:
  510. case Triple::TvOS:
  511. case Triple::WatchOS:
  512. case Triple::FreeBSD:
  513. case Triple::Linux:
  514. break;
  515. default:
  516. TLI.setUnavailable(LibFunc_ffsll);
  517. }
  518. // The following functions are available on at least FreeBSD:
  519. // http://svn.freebsd.org/base/head/lib/libc/string/fls.c
  520. // http://svn.freebsd.org/base/head/lib/libc/string/flsl.c
  521. // http://svn.freebsd.org/base/head/lib/libc/string/flsll.c
  522. if (!T.isOSFreeBSD()) {
  523. TLI.setUnavailable(LibFunc_fls);
  524. TLI.setUnavailable(LibFunc_flsl);
  525. TLI.setUnavailable(LibFunc_flsll);
  526. }
  527. // The following functions are only available on GNU/Linux (using glibc).
  528. // Linux variants without glibc (eg: bionic, musl) may have some subset.
  529. if (!T.isOSLinux() || !T.isGNUEnvironment()) {
  530. TLI.setUnavailable(LibFunc_dunder_strdup);
  531. TLI.setUnavailable(LibFunc_dunder_strtok_r);
  532. TLI.setUnavailable(LibFunc_dunder_isoc99_scanf);
  533. TLI.setUnavailable(LibFunc_dunder_isoc99_sscanf);
  534. TLI.setUnavailable(LibFunc_under_IO_getc);
  535. TLI.setUnavailable(LibFunc_under_IO_putc);
  536. // But, Android and musl have memalign.
  537. if (!T.isAndroid() && !T.isMusl())
  538. TLI.setUnavailable(LibFunc_memalign);
  539. TLI.setUnavailable(LibFunc_fopen64);
  540. TLI.setUnavailable(LibFunc_fseeko64);
  541. TLI.setUnavailable(LibFunc_fstat64);
  542. TLI.setUnavailable(LibFunc_fstatvfs64);
  543. TLI.setUnavailable(LibFunc_ftello64);
  544. TLI.setUnavailable(LibFunc_lstat64);
  545. TLI.setUnavailable(LibFunc_open64);
  546. TLI.setUnavailable(LibFunc_stat64);
  547. TLI.setUnavailable(LibFunc_statvfs64);
  548. TLI.setUnavailable(LibFunc_tmpfile64);
  549. // Relaxed math functions are included in math-finite.h on Linux (GLIBC).
  550. // Note that math-finite.h is no longer supported by top-of-tree GLIBC,
  551. // so we keep these functions around just so that they're recognized by
  552. // the ConstantFolder.
  553. TLI.setUnavailable(LibFunc_acos_finite);
  554. TLI.setUnavailable(LibFunc_acosf_finite);
  555. TLI.setUnavailable(LibFunc_acosl_finite);
  556. TLI.setUnavailable(LibFunc_acosh_finite);
  557. TLI.setUnavailable(LibFunc_acoshf_finite);
  558. TLI.setUnavailable(LibFunc_acoshl_finite);
  559. TLI.setUnavailable(LibFunc_asin_finite);
  560. TLI.setUnavailable(LibFunc_asinf_finite);
  561. TLI.setUnavailable(LibFunc_asinl_finite);
  562. TLI.setUnavailable(LibFunc_atan2_finite);
  563. TLI.setUnavailable(LibFunc_atan2f_finite);
  564. TLI.setUnavailable(LibFunc_atan2l_finite);
  565. TLI.setUnavailable(LibFunc_atanh_finite);
  566. TLI.setUnavailable(LibFunc_atanhf_finite);
  567. TLI.setUnavailable(LibFunc_atanhl_finite);
  568. TLI.setUnavailable(LibFunc_cosh_finite);
  569. TLI.setUnavailable(LibFunc_coshf_finite);
  570. TLI.setUnavailable(LibFunc_coshl_finite);
  571. TLI.setUnavailable(LibFunc_exp10_finite);
  572. TLI.setUnavailable(LibFunc_exp10f_finite);
  573. TLI.setUnavailable(LibFunc_exp10l_finite);
  574. TLI.setUnavailable(LibFunc_exp2_finite);
  575. TLI.setUnavailable(LibFunc_exp2f_finite);
  576. TLI.setUnavailable(LibFunc_exp2l_finite);
  577. TLI.setUnavailable(LibFunc_exp_finite);
  578. TLI.setUnavailable(LibFunc_expf_finite);
  579. TLI.setUnavailable(LibFunc_expl_finite);
  580. TLI.setUnavailable(LibFunc_log10_finite);
  581. TLI.setUnavailable(LibFunc_log10f_finite);
  582. TLI.setUnavailable(LibFunc_log10l_finite);
  583. TLI.setUnavailable(LibFunc_log2_finite);
  584. TLI.setUnavailable(LibFunc_log2f_finite);
  585. TLI.setUnavailable(LibFunc_log2l_finite);
  586. TLI.setUnavailable(LibFunc_log_finite);
  587. TLI.setUnavailable(LibFunc_logf_finite);
  588. TLI.setUnavailable(LibFunc_logl_finite);
  589. TLI.setUnavailable(LibFunc_pow_finite);
  590. TLI.setUnavailable(LibFunc_powf_finite);
  591. TLI.setUnavailable(LibFunc_powl_finite);
  592. TLI.setUnavailable(LibFunc_sinh_finite);
  593. TLI.setUnavailable(LibFunc_sinhf_finite);
  594. TLI.setUnavailable(LibFunc_sinhl_finite);
  595. TLI.setUnavailable(LibFunc_sqrt_finite);
  596. TLI.setUnavailable(LibFunc_sqrtf_finite);
  597. TLI.setUnavailable(LibFunc_sqrtl_finite);
  598. }
  599. if ((T.isOSLinux() && T.isGNUEnvironment()) ||
  600. (T.isAndroid() && !T.isAndroidVersionLT(28))) {
  601. // available IO unlocked variants on GNU/Linux and Android P or later
  602. TLI.setAvailable(LibFunc_getc_unlocked);
  603. TLI.setAvailable(LibFunc_getchar_unlocked);
  604. TLI.setAvailable(LibFunc_putc_unlocked);
  605. TLI.setAvailable(LibFunc_putchar_unlocked);
  606. TLI.setAvailable(LibFunc_fputc_unlocked);
  607. TLI.setAvailable(LibFunc_fgetc_unlocked);
  608. TLI.setAvailable(LibFunc_fread_unlocked);
  609. TLI.setAvailable(LibFunc_fwrite_unlocked);
  610. TLI.setAvailable(LibFunc_fputs_unlocked);
  611. TLI.setAvailable(LibFunc_fgets_unlocked);
  612. }
  613. if (T.isAndroid() && T.isAndroidVersionLT(21)) {
  614. TLI.setUnavailable(LibFunc_stpcpy);
  615. TLI.setUnavailable(LibFunc_stpncpy);
  616. }
  617. if (T.isPS4()) {
  618. // PS4 does have memalign.
  619. TLI.setAvailable(LibFunc_memalign);
  620. // PS4 does not have new/delete with "unsigned int" size parameter;
  621. // it only has the "unsigned long" versions.
  622. TLI.setUnavailable(LibFunc_ZdaPvj);
  623. TLI.setUnavailable(LibFunc_ZdaPvjSt11align_val_t);
  624. TLI.setUnavailable(LibFunc_ZdlPvj);
  625. TLI.setUnavailable(LibFunc_ZdlPvjSt11align_val_t);
  626. TLI.setUnavailable(LibFunc_Znaj);
  627. TLI.setUnavailable(LibFunc_ZnajRKSt9nothrow_t);
  628. TLI.setUnavailable(LibFunc_ZnajSt11align_val_t);
  629. TLI.setUnavailable(LibFunc_ZnajSt11align_val_tRKSt9nothrow_t);
  630. TLI.setUnavailable(LibFunc_Znwj);
  631. TLI.setUnavailable(LibFunc_ZnwjRKSt9nothrow_t);
  632. TLI.setUnavailable(LibFunc_ZnwjSt11align_val_t);
  633. TLI.setUnavailable(LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t);
  634. // None of the *_chk functions.
  635. TLI.setUnavailable(LibFunc_memccpy_chk);
  636. TLI.setUnavailable(LibFunc_memcpy_chk);
  637. TLI.setUnavailable(LibFunc_memmove_chk);
  638. TLI.setUnavailable(LibFunc_mempcpy_chk);
  639. TLI.setUnavailable(LibFunc_memset_chk);
  640. TLI.setUnavailable(LibFunc_snprintf_chk);
  641. TLI.setUnavailable(LibFunc_sprintf_chk);
  642. TLI.setUnavailable(LibFunc_stpcpy_chk);
  643. TLI.setUnavailable(LibFunc_stpncpy_chk);
  644. TLI.setUnavailable(LibFunc_strcat_chk);
  645. TLI.setUnavailable(LibFunc_strcpy_chk);
  646. TLI.setUnavailable(LibFunc_strlcat_chk);
  647. TLI.setUnavailable(LibFunc_strlcpy_chk);
  648. TLI.setUnavailable(LibFunc_strlen_chk);
  649. TLI.setUnavailable(LibFunc_strncat_chk);
  650. TLI.setUnavailable(LibFunc_strncpy_chk);
  651. TLI.setUnavailable(LibFunc_vsnprintf_chk);
  652. TLI.setUnavailable(LibFunc_vsprintf_chk);
  653. // Various Posix system functions.
  654. TLI.setUnavailable(LibFunc_access);
  655. TLI.setUnavailable(LibFunc_chmod);
  656. TLI.setUnavailable(LibFunc_chown);
  657. TLI.setUnavailable(LibFunc_closedir);
  658. TLI.setUnavailable(LibFunc_ctermid);
  659. TLI.setUnavailable(LibFunc_execl);
  660. TLI.setUnavailable(LibFunc_execle);
  661. TLI.setUnavailable(LibFunc_execlp);
  662. TLI.setUnavailable(LibFunc_execv);
  663. TLI.setUnavailable(LibFunc_execvP);
  664. TLI.setUnavailable(LibFunc_execve);
  665. TLI.setUnavailable(LibFunc_execvp);
  666. TLI.setUnavailable(LibFunc_execvpe);
  667. TLI.setUnavailable(LibFunc_fork);
  668. TLI.setUnavailable(LibFunc_fstat);
  669. TLI.setUnavailable(LibFunc_fstatvfs);
  670. TLI.setUnavailable(LibFunc_getenv);
  671. TLI.setUnavailable(LibFunc_getitimer);
  672. TLI.setUnavailable(LibFunc_getlogin_r);
  673. TLI.setUnavailable(LibFunc_getpwnam);
  674. TLI.setUnavailable(LibFunc_gettimeofday);
  675. TLI.setUnavailable(LibFunc_lchown);
  676. TLI.setUnavailable(LibFunc_lstat);
  677. TLI.setUnavailable(LibFunc_mkdir);
  678. TLI.setUnavailable(LibFunc_open);
  679. TLI.setUnavailable(LibFunc_opendir);
  680. TLI.setUnavailable(LibFunc_pclose);
  681. TLI.setUnavailable(LibFunc_popen);
  682. TLI.setUnavailable(LibFunc_pread);
  683. TLI.setUnavailable(LibFunc_pwrite);
  684. TLI.setUnavailable(LibFunc_read);
  685. TLI.setUnavailable(LibFunc_readlink);
  686. TLI.setUnavailable(LibFunc_realpath);
  687. TLI.setUnavailable(LibFunc_rename);
  688. TLI.setUnavailable(LibFunc_rmdir);
  689. TLI.setUnavailable(LibFunc_setitimer);
  690. TLI.setUnavailable(LibFunc_stat);
  691. TLI.setUnavailable(LibFunc_statvfs);
  692. TLI.setUnavailable(LibFunc_system);
  693. TLI.setUnavailable(LibFunc_times);
  694. TLI.setUnavailable(LibFunc_tmpfile);
  695. TLI.setUnavailable(LibFunc_unlink);
  696. TLI.setUnavailable(LibFunc_uname);
  697. TLI.setUnavailable(LibFunc_unsetenv);
  698. TLI.setUnavailable(LibFunc_utime);
  699. TLI.setUnavailable(LibFunc_utimes);
  700. TLI.setUnavailable(LibFunc_valloc);
  701. TLI.setUnavailable(LibFunc_write);
  702. // Miscellaneous other functions not provided.
  703. TLI.setUnavailable(LibFunc_atomic_load);
  704. TLI.setUnavailable(LibFunc_atomic_store);
  705. TLI.setUnavailable(LibFunc___kmpc_alloc_shared);
  706. TLI.setUnavailable(LibFunc___kmpc_free_shared);
  707. TLI.setUnavailable(LibFunc_dunder_strndup);
  708. TLI.setUnavailable(LibFunc_bcmp);
  709. TLI.setUnavailable(LibFunc_bcopy);
  710. TLI.setUnavailable(LibFunc_bzero);
  711. TLI.setUnavailable(LibFunc_cabs);
  712. TLI.setUnavailable(LibFunc_cabsf);
  713. TLI.setUnavailable(LibFunc_cabsl);
  714. TLI.setUnavailable(LibFunc_ffs);
  715. TLI.setUnavailable(LibFunc_flockfile);
  716. TLI.setUnavailable(LibFunc_fseeko);
  717. TLI.setUnavailable(LibFunc_ftello);
  718. TLI.setUnavailable(LibFunc_ftrylockfile);
  719. TLI.setUnavailable(LibFunc_funlockfile);
  720. TLI.setUnavailable(LibFunc_htonl);
  721. TLI.setUnavailable(LibFunc_htons);
  722. TLI.setUnavailable(LibFunc_isascii);
  723. TLI.setUnavailable(LibFunc_memccpy);
  724. TLI.setUnavailable(LibFunc_mempcpy);
  725. TLI.setUnavailable(LibFunc_memrchr);
  726. TLI.setUnavailable(LibFunc_ntohl);
  727. TLI.setUnavailable(LibFunc_ntohs);
  728. TLI.setUnavailable(LibFunc_reallocf);
  729. TLI.setUnavailable(LibFunc_roundeven);
  730. TLI.setUnavailable(LibFunc_roundevenf);
  731. TLI.setUnavailable(LibFunc_roundevenl);
  732. TLI.setUnavailable(LibFunc_stpcpy);
  733. TLI.setUnavailable(LibFunc_stpncpy);
  734. TLI.setUnavailable(LibFunc_strlcat);
  735. TLI.setUnavailable(LibFunc_strlcpy);
  736. TLI.setUnavailable(LibFunc_strndup);
  737. TLI.setUnavailable(LibFunc_strnlen);
  738. TLI.setUnavailable(LibFunc_toascii);
  739. }
  740. // As currently implemented in clang, NVPTX code has no standard library to
  741. // speak of. Headers provide a standard-ish library implementation, but many
  742. // of the signatures are wrong -- for example, many libm functions are not
  743. // extern "C".
  744. //
  745. // libdevice, an IR library provided by nvidia, is linked in by the front-end,
  746. // but only used functions are provided to llvm. Moreover, most of the
  747. // functions in libdevice don't map precisely to standard library functions.
  748. //
  749. // FIXME: Having no standard library prevents e.g. many fastmath
  750. // optimizations, so this situation should be fixed.
  751. if (T.isNVPTX()) {
  752. TLI.disableAllFunctions();
  753. TLI.setAvailable(LibFunc_nvvm_reflect);
  754. TLI.setAvailable(llvm::LibFunc_malloc);
  755. TLI.setAvailable(llvm::LibFunc_free);
  756. // TODO: We could enable the following two according to [0] but we haven't
  757. // done an evaluation wrt. the performance implications.
  758. // [0]
  759. // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#dynamic-global-memory-allocation-and-operations
  760. //
  761. // TLI.setAvailable(llvm::LibFunc_memcpy);
  762. // TLI.setAvailable(llvm::LibFunc_memset);
  763. TLI.setAvailable(llvm::LibFunc___kmpc_alloc_shared);
  764. TLI.setAvailable(llvm::LibFunc___kmpc_free_shared);
  765. } else {
  766. TLI.setUnavailable(LibFunc_nvvm_reflect);
  767. }
  768. // These vec_malloc/free routines are only available on AIX.
  769. if (!T.isOSAIX()) {
  770. TLI.setUnavailable(LibFunc_vec_calloc);
  771. TLI.setUnavailable(LibFunc_vec_malloc);
  772. TLI.setUnavailable(LibFunc_vec_realloc);
  773. TLI.setUnavailable(LibFunc_vec_free);
  774. }
  775. TLI.addVectorizableFunctionsFromVecLib(ClVectorLibrary);
  776. }
  777. TargetLibraryInfoImpl::TargetLibraryInfoImpl() {
  778. // Default to everything being available.
  779. memset(AvailableArray, -1, sizeof(AvailableArray));
  780. initialize(*this, Triple(), StandardNames);
  781. }
  782. TargetLibraryInfoImpl::TargetLibraryInfoImpl(const Triple &T) {
  783. // Default to everything being available.
  784. memset(AvailableArray, -1, sizeof(AvailableArray));
  785. initialize(*this, T, StandardNames);
  786. }
  787. TargetLibraryInfoImpl::TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI)
  788. : CustomNames(TLI.CustomNames), ShouldExtI32Param(TLI.ShouldExtI32Param),
  789. ShouldExtI32Return(TLI.ShouldExtI32Return),
  790. ShouldSignExtI32Param(TLI.ShouldSignExtI32Param),
  791. SizeOfInt(TLI.SizeOfInt) {
  792. memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
  793. VectorDescs = TLI.VectorDescs;
  794. ScalarDescs = TLI.ScalarDescs;
  795. }
  796. TargetLibraryInfoImpl::TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI)
  797. : CustomNames(std::move(TLI.CustomNames)),
  798. ShouldExtI32Param(TLI.ShouldExtI32Param),
  799. ShouldExtI32Return(TLI.ShouldExtI32Return),
  800. ShouldSignExtI32Param(TLI.ShouldSignExtI32Param),
  801. SizeOfInt(TLI.SizeOfInt) {
  802. std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
  803. AvailableArray);
  804. VectorDescs = TLI.VectorDescs;
  805. ScalarDescs = TLI.ScalarDescs;
  806. }
  807. TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(const TargetLibraryInfoImpl &TLI) {
  808. CustomNames = TLI.CustomNames;
  809. ShouldExtI32Param = TLI.ShouldExtI32Param;
  810. ShouldExtI32Return = TLI.ShouldExtI32Return;
  811. ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
  812. SizeOfInt = TLI.SizeOfInt;
  813. memcpy(AvailableArray, TLI.AvailableArray, sizeof(AvailableArray));
  814. return *this;
  815. }
  816. TargetLibraryInfoImpl &TargetLibraryInfoImpl::operator=(TargetLibraryInfoImpl &&TLI) {
  817. CustomNames = std::move(TLI.CustomNames);
  818. ShouldExtI32Param = TLI.ShouldExtI32Param;
  819. ShouldExtI32Return = TLI.ShouldExtI32Return;
  820. ShouldSignExtI32Param = TLI.ShouldSignExtI32Param;
  821. SizeOfInt = TLI.SizeOfInt;
  822. std::move(std::begin(TLI.AvailableArray), std::end(TLI.AvailableArray),
  823. AvailableArray);
  824. return *this;
  825. }
  826. static StringRef sanitizeFunctionName(StringRef funcName) {
  827. // Filter out empty names and names containing null bytes, those can't be in
  828. // our table.
  829. if (funcName.empty() || funcName.contains('\0'))
  830. return StringRef();
  831. // Check for \01 prefix that is used to mangle __asm declarations and
  832. // strip it if present.
  833. return GlobalValue::dropLLVMManglingEscape(funcName);
  834. }
  835. bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName, LibFunc &F) const {
  836. funcName = sanitizeFunctionName(funcName);
  837. if (funcName.empty())
  838. return false;
  839. const auto *Start = std::begin(StandardNames);
  840. const auto *End = std::end(StandardNames);
  841. const auto *I = std::lower_bound(Start, End, funcName);
  842. if (I != End && *I == funcName) {
  843. F = (LibFunc)(I - Start);
  844. return true;
  845. }
  846. return false;
  847. }
  848. bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
  849. LibFunc F,
  850. const Module &M) const {
  851. // FIXME: There is really no guarantee that sizeof(size_t) is equal to
  852. // sizeof(int*) for every target. So the assumption used here to derive the
  853. // SizeTBits based on the size of an integer pointer in address space zero
  854. // isn't always valid.
  855. unsigned SizeTBits = M.getDataLayout().getPointerSizeInBits(/*AddrSpace=*/0);
  856. unsigned NumParams = FTy.getNumParams();
  857. switch (F) {
  858. case LibFunc_execl:
  859. case LibFunc_execlp:
  860. case LibFunc_execle:
  861. return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
  862. FTy.getParamType(1)->isPointerTy() &&
  863. FTy.getReturnType()->isIntegerTy(32));
  864. case LibFunc_execv:
  865. case LibFunc_execvp:
  866. return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
  867. FTy.getParamType(1)->isPointerTy() &&
  868. FTy.getReturnType()->isIntegerTy(32));
  869. case LibFunc_execvP:
  870. case LibFunc_execvpe:
  871. case LibFunc_execve:
  872. return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
  873. FTy.getParamType(1)->isPointerTy() &&
  874. FTy.getParamType(2)->isPointerTy() &&
  875. FTy.getReturnType()->isIntegerTy(32));
  876. case LibFunc_strlen_chk:
  877. --NumParams;
  878. if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
  879. return false;
  880. LLVM_FALLTHROUGH;
  881. case LibFunc_strlen:
  882. return NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
  883. FTy.getReturnType()->isIntegerTy(SizeTBits);
  884. case LibFunc_strchr:
  885. case LibFunc_strrchr:
  886. return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
  887. FTy.getParamType(0) == FTy.getReturnType() &&
  888. FTy.getParamType(1)->isIntegerTy());
  889. case LibFunc_strtol:
  890. case LibFunc_strtod:
  891. case LibFunc_strtof:
  892. case LibFunc_strtoul:
  893. case LibFunc_strtoll:
  894. case LibFunc_strtold:
  895. case LibFunc_strtoull:
  896. return ((NumParams == 2 || NumParams == 3) &&
  897. FTy.getParamType(0)->isPointerTy() &&
  898. FTy.getParamType(1)->isPointerTy());
  899. case LibFunc_strcat_chk:
  900. --NumParams;
  901. if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
  902. return false;
  903. LLVM_FALLTHROUGH;
  904. case LibFunc_strcat:
  905. return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
  906. FTy.getParamType(0) == FTy.getReturnType() &&
  907. FTy.getParamType(1) == FTy.getReturnType());
  908. case LibFunc_strncat_chk:
  909. --NumParams;
  910. if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
  911. return false;
  912. LLVM_FALLTHROUGH;
  913. case LibFunc_strncat:
  914. return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
  915. FTy.getParamType(0) == FTy.getReturnType() &&
  916. FTy.getParamType(1) == FTy.getReturnType() &&
  917. FTy.getParamType(2)->isIntegerTy(SizeTBits));
  918. case LibFunc_strcpy_chk:
  919. case LibFunc_stpcpy_chk:
  920. --NumParams;
  921. if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
  922. return false;
  923. LLVM_FALLTHROUGH;
  924. case LibFunc_strcpy:
  925. case LibFunc_stpcpy:
  926. return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(0) &&
  927. FTy.getParamType(0) == FTy.getParamType(1) &&
  928. FTy.getParamType(0)->isPointerTy());
  929. case LibFunc_strlcat_chk:
  930. case LibFunc_strlcpy_chk:
  931. --NumParams;
  932. if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
  933. return false;
  934. LLVM_FALLTHROUGH;
  935. case LibFunc_strlcat:
  936. case LibFunc_strlcpy:
  937. return NumParams == 3 && FTy.getReturnType()->isIntegerTy(SizeTBits) &&
  938. FTy.getParamType(0)->isPointerTy() &&
  939. FTy.getParamType(1)->isPointerTy() &&
  940. FTy.getParamType(2)->isIntegerTy(SizeTBits);
  941. case LibFunc_strncpy_chk:
  942. case LibFunc_stpncpy_chk:
  943. --NumParams;
  944. if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
  945. return false;
  946. LLVM_FALLTHROUGH;
  947. case LibFunc_strncpy:
  948. case LibFunc_stpncpy:
  949. return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
  950. FTy.getParamType(0) == FTy.getParamType(1) &&
  951. FTy.getParamType(0)->isPointerTy() &&
  952. FTy.getParamType(2)->isIntegerTy(SizeTBits));
  953. case LibFunc_strxfrm:
  954. return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
  955. FTy.getParamType(1)->isPointerTy());
  956. case LibFunc_strcmp:
  957. return (NumParams == 2 && FTy.getReturnType()->isIntegerTy(32) &&
  958. FTy.getParamType(0)->isPointerTy() &&
  959. FTy.getParamType(0) == FTy.getParamType(1));
  960. case LibFunc_strncmp:
  961. return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
  962. FTy.getParamType(0)->isPointerTy() &&
  963. FTy.getParamType(0) == FTy.getParamType(1) &&
  964. FTy.getParamType(2)->isIntegerTy(SizeTBits));
  965. case LibFunc_strspn:
  966. case LibFunc_strcspn:
  967. return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
  968. FTy.getParamType(0) == FTy.getParamType(1) &&
  969. FTy.getReturnType()->isIntegerTy());
  970. case LibFunc_strcoll:
  971. case LibFunc_strcasecmp:
  972. case LibFunc_strncasecmp:
  973. return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
  974. FTy.getParamType(1)->isPointerTy());
  975. case LibFunc_strstr:
  976. return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
  977. FTy.getParamType(0)->isPointerTy() &&
  978. FTy.getParamType(1)->isPointerTy());
  979. case LibFunc_strpbrk:
  980. return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
  981. FTy.getReturnType() == FTy.getParamType(0) &&
  982. FTy.getParamType(0) == FTy.getParamType(1));
  983. case LibFunc_strtok:
  984. case LibFunc_strtok_r:
  985. return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
  986. case LibFunc_scanf:
  987. case LibFunc_setbuf:
  988. case LibFunc_setvbuf:
  989. return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
  990. case LibFunc_strdup:
  991. case LibFunc_strndup:
  992. return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
  993. FTy.getParamType(0)->isPointerTy());
  994. case LibFunc_sscanf:
  995. case LibFunc_stat:
  996. case LibFunc_statvfs:
  997. case LibFunc_siprintf:
  998. case LibFunc_small_sprintf:
  999. case LibFunc_sprintf:
  1000. return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
  1001. FTy.getParamType(1)->isPointerTy() &&
  1002. FTy.getReturnType()->isIntegerTy(32));
  1003. case LibFunc_sprintf_chk:
  1004. return NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
  1005. FTy.getParamType(1)->isIntegerTy(32) &&
  1006. FTy.getParamType(2)->isIntegerTy(SizeTBits) &&
  1007. FTy.getParamType(3)->isPointerTy() &&
  1008. FTy.getReturnType()->isIntegerTy(32);
  1009. case LibFunc_snprintf:
  1010. return NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
  1011. FTy.getParamType(1)->isIntegerTy(SizeTBits) &&
  1012. FTy.getParamType(2)->isPointerTy() &&
  1013. FTy.getReturnType()->isIntegerTy(32);
  1014. case LibFunc_snprintf_chk:
  1015. return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
  1016. FTy.getParamType(1)->isIntegerTy(SizeTBits) &&
  1017. FTy.getParamType(2)->isIntegerTy(32) &&
  1018. FTy.getParamType(3)->isIntegerTy(SizeTBits) &&
  1019. FTy.getParamType(4)->isPointerTy() &&
  1020. FTy.getReturnType()->isIntegerTy(32);
  1021. case LibFunc_setitimer:
  1022. return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
  1023. FTy.getParamType(2)->isPointerTy());
  1024. case LibFunc_system:
  1025. return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
  1026. case LibFunc___kmpc_alloc_shared:
  1027. case LibFunc_malloc:
  1028. case LibFunc_vec_malloc:
  1029. return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
  1030. case LibFunc_memcmp:
  1031. return NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
  1032. FTy.getParamType(0)->isPointerTy() &&
  1033. FTy.getParamType(1)->isPointerTy() &&
  1034. FTy.getParamType(2)->isIntegerTy(SizeTBits);
  1035. case LibFunc_memchr:
  1036. case LibFunc_memrchr:
  1037. return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
  1038. FTy.getReturnType() == FTy.getParamType(0) &&
  1039. FTy.getParamType(1)->isIntegerTy(32) &&
  1040. FTy.getParamType(2)->isIntegerTy(SizeTBits));
  1041. case LibFunc_modf:
  1042. case LibFunc_modff:
  1043. case LibFunc_modfl:
  1044. return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
  1045. case LibFunc_memcpy_chk:
  1046. case LibFunc_mempcpy_chk:
  1047. case LibFunc_memmove_chk:
  1048. --NumParams;
  1049. if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
  1050. return false;
  1051. LLVM_FALLTHROUGH;
  1052. case LibFunc_memcpy:
  1053. case LibFunc_mempcpy:
  1054. case LibFunc_memmove:
  1055. return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
  1056. FTy.getParamType(0)->isPointerTy() &&
  1057. FTy.getParamType(1)->isPointerTy() &&
  1058. FTy.getParamType(2)->isIntegerTy(SizeTBits));
  1059. case LibFunc_memset_chk:
  1060. --NumParams;
  1061. if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
  1062. return false;
  1063. LLVM_FALLTHROUGH;
  1064. case LibFunc_memset:
  1065. return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
  1066. FTy.getParamType(0)->isPointerTy() &&
  1067. FTy.getParamType(1)->isIntegerTy() &&
  1068. FTy.getParamType(2)->isIntegerTy(SizeTBits));
  1069. case LibFunc_memccpy_chk:
  1070. --NumParams;
  1071. if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
  1072. return false;
  1073. LLVM_FALLTHROUGH;
  1074. case LibFunc_memccpy:
  1075. return (NumParams >= 2 && FTy.getParamType(1)->isPointerTy());
  1076. case LibFunc_memalign:
  1077. return (FTy.getReturnType()->isPointerTy());
  1078. case LibFunc_realloc:
  1079. case LibFunc_reallocf:
  1080. case LibFunc_vec_realloc:
  1081. return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
  1082. FTy.getParamType(0) == FTy.getReturnType() &&
  1083. FTy.getParamType(1)->isIntegerTy(SizeTBits));
  1084. case LibFunc_read:
  1085. return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
  1086. case LibFunc_rewind:
  1087. case LibFunc_rmdir:
  1088. case LibFunc_remove:
  1089. case LibFunc_realpath:
  1090. return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
  1091. case LibFunc_rename:
  1092. return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
  1093. FTy.getParamType(1)->isPointerTy());
  1094. case LibFunc_readlink:
  1095. return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
  1096. FTy.getParamType(1)->isPointerTy());
  1097. case LibFunc_write:
  1098. return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
  1099. case LibFunc_aligned_alloc:
  1100. return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
  1101. case LibFunc_bcopy:
  1102. case LibFunc_bcmp:
  1103. return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
  1104. FTy.getParamType(1)->isPointerTy());
  1105. case LibFunc_bzero:
  1106. return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
  1107. case LibFunc_calloc:
  1108. case LibFunc_vec_calloc:
  1109. return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
  1110. FTy.getParamType(0) == FTy.getParamType(1));
  1111. case LibFunc_atof:
  1112. case LibFunc_atoi:
  1113. case LibFunc_atol:
  1114. case LibFunc_atoll:
  1115. case LibFunc_ferror:
  1116. case LibFunc_getenv:
  1117. case LibFunc_getpwnam:
  1118. case LibFunc_iprintf:
  1119. case LibFunc_small_printf:
  1120. case LibFunc_pclose:
  1121. case LibFunc_perror:
  1122. case LibFunc_printf:
  1123. case LibFunc_puts:
  1124. case LibFunc_uname:
  1125. case LibFunc_under_IO_getc:
  1126. case LibFunc_unlink:
  1127. case LibFunc_unsetenv:
  1128. return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
  1129. case LibFunc_access:
  1130. case LibFunc_chmod:
  1131. case LibFunc_chown:
  1132. case LibFunc_clearerr:
  1133. case LibFunc_closedir:
  1134. case LibFunc_ctermid:
  1135. case LibFunc_fclose:
  1136. case LibFunc_feof:
  1137. case LibFunc_fflush:
  1138. case LibFunc_fgetc:
  1139. case LibFunc_fgetc_unlocked:
  1140. case LibFunc_fileno:
  1141. case LibFunc_flockfile:
  1142. case LibFunc_free:
  1143. case LibFunc_fseek:
  1144. case LibFunc_fseeko64:
  1145. case LibFunc_fseeko:
  1146. case LibFunc_fsetpos:
  1147. case LibFunc_ftell:
  1148. case LibFunc_ftello64:
  1149. case LibFunc_ftello:
  1150. case LibFunc_ftrylockfile:
  1151. case LibFunc_funlockfile:
  1152. case LibFunc_getc:
  1153. case LibFunc_getc_unlocked:
  1154. case LibFunc_getlogin_r:
  1155. case LibFunc_mkdir:
  1156. case LibFunc_mktime:
  1157. case LibFunc_times:
  1158. case LibFunc_vec_free:
  1159. return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
  1160. case LibFunc___kmpc_free_shared:
  1161. return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
  1162. FTy.getParamType(1)->isIntegerTy(SizeTBits));
  1163. case LibFunc_fopen:
  1164. return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
  1165. FTy.getParamType(0)->isPointerTy() &&
  1166. FTy.getParamType(1)->isPointerTy());
  1167. case LibFunc_fork:
  1168. return (NumParams == 0 && FTy.getReturnType()->isIntegerTy(32));
  1169. case LibFunc_fdopen:
  1170. return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
  1171. FTy.getParamType(1)->isPointerTy());
  1172. case LibFunc_fputc:
  1173. case LibFunc_fputc_unlocked:
  1174. case LibFunc_fstat:
  1175. case LibFunc_frexp:
  1176. case LibFunc_frexpf:
  1177. case LibFunc_frexpl:
  1178. case LibFunc_fstatvfs:
  1179. return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
  1180. case LibFunc_fgets:
  1181. case LibFunc_fgets_unlocked:
  1182. return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
  1183. FTy.getParamType(2)->isPointerTy());
  1184. case LibFunc_fread:
  1185. case LibFunc_fread_unlocked:
  1186. return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
  1187. FTy.getParamType(3)->isPointerTy());
  1188. case LibFunc_fwrite:
  1189. case LibFunc_fwrite_unlocked:
  1190. return (NumParams == 4 && FTy.getReturnType()->isIntegerTy() &&
  1191. FTy.getParamType(0)->isPointerTy() &&
  1192. FTy.getParamType(1)->isIntegerTy() &&
  1193. FTy.getParamType(2)->isIntegerTy() &&
  1194. FTy.getParamType(3)->isPointerTy());
  1195. case LibFunc_fputs:
  1196. case LibFunc_fputs_unlocked:
  1197. return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
  1198. FTy.getParamType(1)->isPointerTy());
  1199. case LibFunc_fscanf:
  1200. case LibFunc_fiprintf:
  1201. case LibFunc_small_fprintf:
  1202. case LibFunc_fprintf:
  1203. return (NumParams >= 2 && FTy.getReturnType()->isIntegerTy() &&
  1204. FTy.getParamType(0)->isPointerTy() &&
  1205. FTy.getParamType(1)->isPointerTy());
  1206. case LibFunc_fgetpos:
  1207. return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
  1208. FTy.getParamType(1)->isPointerTy());
  1209. case LibFunc_getchar:
  1210. case LibFunc_getchar_unlocked:
  1211. return (NumParams == 0 && FTy.getReturnType()->isIntegerTy());
  1212. case LibFunc_gets:
  1213. return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
  1214. case LibFunc_getitimer:
  1215. return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
  1216. case LibFunc_ungetc:
  1217. return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
  1218. case LibFunc_utime:
  1219. case LibFunc_utimes:
  1220. return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
  1221. FTy.getParamType(1)->isPointerTy());
  1222. case LibFunc_putc:
  1223. case LibFunc_putc_unlocked:
  1224. return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
  1225. case LibFunc_pread:
  1226. case LibFunc_pwrite:
  1227. return (NumParams == 4 && FTy.getParamType(1)->isPointerTy());
  1228. case LibFunc_popen:
  1229. return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
  1230. FTy.getParamType(0)->isPointerTy() &&
  1231. FTy.getParamType(1)->isPointerTy());
  1232. case LibFunc_vscanf:
  1233. return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
  1234. case LibFunc_vsscanf:
  1235. return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
  1236. FTy.getParamType(2)->isPointerTy());
  1237. case LibFunc_vfscanf:
  1238. return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
  1239. FTy.getParamType(2)->isPointerTy());
  1240. case LibFunc_valloc:
  1241. return (FTy.getReturnType()->isPointerTy());
  1242. case LibFunc_vprintf:
  1243. return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
  1244. case LibFunc_vfprintf:
  1245. case LibFunc_vsprintf:
  1246. return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
  1247. FTy.getParamType(1)->isPointerTy());
  1248. case LibFunc_vsprintf_chk:
  1249. return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
  1250. FTy.getParamType(1)->isIntegerTy(32) &&
  1251. FTy.getParamType(2)->isIntegerTy(SizeTBits) && FTy.getParamType(3)->isPointerTy();
  1252. case LibFunc_vsnprintf:
  1253. return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
  1254. FTy.getParamType(2)->isPointerTy());
  1255. case LibFunc_vsnprintf_chk:
  1256. return NumParams == 6 && FTy.getParamType(0)->isPointerTy() &&
  1257. FTy.getParamType(2)->isIntegerTy(32) &&
  1258. FTy.getParamType(3)->isIntegerTy(SizeTBits) && FTy.getParamType(4)->isPointerTy();
  1259. case LibFunc_open:
  1260. return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
  1261. case LibFunc_opendir:
  1262. return (NumParams == 1 && FTy.getReturnType()->isPointerTy() &&
  1263. FTy.getParamType(0)->isPointerTy());
  1264. case LibFunc_tmpfile:
  1265. return (FTy.getReturnType()->isPointerTy());
  1266. case LibFunc_htonl:
  1267. case LibFunc_ntohl:
  1268. return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
  1269. FTy.getReturnType() == FTy.getParamType(0));
  1270. case LibFunc_htons:
  1271. case LibFunc_ntohs:
  1272. return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(16) &&
  1273. FTy.getReturnType() == FTy.getParamType(0));
  1274. case LibFunc_lstat:
  1275. return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
  1276. FTy.getParamType(1)->isPointerTy());
  1277. case LibFunc_lchown:
  1278. return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
  1279. case LibFunc_qsort:
  1280. return (NumParams == 4 && FTy.getParamType(3)->isPointerTy());
  1281. case LibFunc_dunder_strdup:
  1282. case LibFunc_dunder_strndup:
  1283. return (NumParams >= 1 && FTy.getReturnType()->isPointerTy() &&
  1284. FTy.getParamType(0)->isPointerTy());
  1285. case LibFunc_dunder_strtok_r:
  1286. return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
  1287. case LibFunc_under_IO_putc:
  1288. return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
  1289. case LibFunc_dunder_isoc99_scanf:
  1290. return (NumParams >= 1 && FTy.getParamType(0)->isPointerTy());
  1291. case LibFunc_stat64:
  1292. case LibFunc_lstat64:
  1293. case LibFunc_statvfs64:
  1294. return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
  1295. FTy.getParamType(1)->isPointerTy());
  1296. case LibFunc_dunder_isoc99_sscanf:
  1297. return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
  1298. FTy.getParamType(1)->isPointerTy());
  1299. case LibFunc_fopen64:
  1300. return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
  1301. FTy.getParamType(0)->isPointerTy() &&
  1302. FTy.getParamType(1)->isPointerTy());
  1303. case LibFunc_tmpfile64:
  1304. return (FTy.getReturnType()->isPointerTy());
  1305. case LibFunc_fstat64:
  1306. case LibFunc_fstatvfs64:
  1307. return (NumParams == 2 && FTy.getParamType(1)->isPointerTy());
  1308. case LibFunc_open64:
  1309. return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
  1310. case LibFunc_gettimeofday:
  1311. return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
  1312. FTy.getParamType(1)->isPointerTy());
  1313. // new(unsigned int);
  1314. case LibFunc_Znwj:
  1315. // new(unsigned long);
  1316. case LibFunc_Znwm:
  1317. // new[](unsigned int);
  1318. case LibFunc_Znaj:
  1319. // new[](unsigned long);
  1320. case LibFunc_Znam:
  1321. // new(unsigned int);
  1322. case LibFunc_msvc_new_int:
  1323. // new(unsigned long long);
  1324. case LibFunc_msvc_new_longlong:
  1325. // new[](unsigned int);
  1326. case LibFunc_msvc_new_array_int:
  1327. // new[](unsigned long long);
  1328. case LibFunc_msvc_new_array_longlong:
  1329. return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
  1330. // new(unsigned int, nothrow);
  1331. case LibFunc_ZnwjRKSt9nothrow_t:
  1332. // new(unsigned long, nothrow);
  1333. case LibFunc_ZnwmRKSt9nothrow_t:
  1334. // new[](unsigned int, nothrow);
  1335. case LibFunc_ZnajRKSt9nothrow_t:
  1336. // new[](unsigned long, nothrow);
  1337. case LibFunc_ZnamRKSt9nothrow_t:
  1338. // new(unsigned int, nothrow);
  1339. case LibFunc_msvc_new_int_nothrow:
  1340. // new(unsigned long long, nothrow);
  1341. case LibFunc_msvc_new_longlong_nothrow:
  1342. // new[](unsigned int, nothrow);
  1343. case LibFunc_msvc_new_array_int_nothrow:
  1344. // new[](unsigned long long, nothrow);
  1345. case LibFunc_msvc_new_array_longlong_nothrow:
  1346. // new(unsigned int, align_val_t)
  1347. case LibFunc_ZnwjSt11align_val_t:
  1348. // new(unsigned long, align_val_t)
  1349. case LibFunc_ZnwmSt11align_val_t:
  1350. // new[](unsigned int, align_val_t)
  1351. case LibFunc_ZnajSt11align_val_t:
  1352. // new[](unsigned long, align_val_t)
  1353. case LibFunc_ZnamSt11align_val_t:
  1354. return (NumParams == 2 && FTy.getReturnType()->isPointerTy());
  1355. // new(unsigned int, align_val_t, nothrow)
  1356. case LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t:
  1357. // new(unsigned long, align_val_t, nothrow)
  1358. case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t:
  1359. // new[](unsigned int, align_val_t, nothrow)
  1360. case LibFunc_ZnajSt11align_val_tRKSt9nothrow_t:
  1361. // new[](unsigned long, align_val_t, nothrow)
  1362. case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t:
  1363. return (NumParams == 3 && FTy.getReturnType()->isPointerTy());
  1364. // void operator delete[](void*);
  1365. case LibFunc_ZdaPv:
  1366. // void operator delete(void*);
  1367. case LibFunc_ZdlPv:
  1368. // void operator delete[](void*);
  1369. case LibFunc_msvc_delete_array_ptr32:
  1370. // void operator delete[](void*);
  1371. case LibFunc_msvc_delete_array_ptr64:
  1372. // void operator delete(void*);
  1373. case LibFunc_msvc_delete_ptr32:
  1374. // void operator delete(void*);
  1375. case LibFunc_msvc_delete_ptr64:
  1376. return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
  1377. // void operator delete[](void*, nothrow);
  1378. case LibFunc_ZdaPvRKSt9nothrow_t:
  1379. // void operator delete[](void*, unsigned int);
  1380. case LibFunc_ZdaPvj:
  1381. // void operator delete[](void*, unsigned long);
  1382. case LibFunc_ZdaPvm:
  1383. // void operator delete(void*, nothrow);
  1384. case LibFunc_ZdlPvRKSt9nothrow_t:
  1385. // void operator delete(void*, unsigned int);
  1386. case LibFunc_ZdlPvj:
  1387. // void operator delete(void*, unsigned long);
  1388. case LibFunc_ZdlPvm:
  1389. // void operator delete(void*, align_val_t)
  1390. case LibFunc_ZdlPvSt11align_val_t:
  1391. // void operator delete[](void*, align_val_t)
  1392. case LibFunc_ZdaPvSt11align_val_t:
  1393. // void operator delete[](void*, unsigned int);
  1394. case LibFunc_msvc_delete_array_ptr32_int:
  1395. // void operator delete[](void*, nothrow);
  1396. case LibFunc_msvc_delete_array_ptr32_nothrow:
  1397. // void operator delete[](void*, unsigned long long);
  1398. case LibFunc_msvc_delete_array_ptr64_longlong:
  1399. // void operator delete[](void*, nothrow);
  1400. case LibFunc_msvc_delete_array_ptr64_nothrow:
  1401. // void operator delete(void*, unsigned int);
  1402. case LibFunc_msvc_delete_ptr32_int:
  1403. // void operator delete(void*, nothrow);
  1404. case LibFunc_msvc_delete_ptr32_nothrow:
  1405. // void operator delete(void*, unsigned long long);
  1406. case LibFunc_msvc_delete_ptr64_longlong:
  1407. // void operator delete(void*, nothrow);
  1408. case LibFunc_msvc_delete_ptr64_nothrow:
  1409. return (NumParams == 2 && FTy.getParamType(0)->isPointerTy());
  1410. // void operator delete(void*, align_val_t, nothrow)
  1411. case LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t:
  1412. // void operator delete[](void*, align_val_t, nothrow)
  1413. case LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t:
  1414. // void operator delete(void*, unsigned int, align_val_t)
  1415. case LibFunc_ZdlPvjSt11align_val_t:
  1416. // void operator delete(void*, unsigned long, align_val_t)
  1417. case LibFunc_ZdlPvmSt11align_val_t:
  1418. // void operator delete[](void*, unsigned int, align_val_t);
  1419. case LibFunc_ZdaPvjSt11align_val_t:
  1420. // void operator delete[](void*, unsigned long, align_val_t);
  1421. case LibFunc_ZdaPvmSt11align_val_t:
  1422. return (NumParams == 3 && FTy.getParamType(0)->isPointerTy());
  1423. // void __atomic_load(size_t, void *, void *, int)
  1424. case LibFunc_atomic_load:
  1425. // void __atomic_store(size_t, void *, void *, int)
  1426. case LibFunc_atomic_store:
  1427. return (NumParams == 4 && FTy.getParamType(0)->isIntegerTy() &&
  1428. FTy.getParamType(1)->isPointerTy() &&
  1429. FTy.getParamType(2)->isPointerTy() &&
  1430. FTy.getParamType(3)->isIntegerTy());
  1431. case LibFunc_memset_pattern4:
  1432. case LibFunc_memset_pattern8:
  1433. case LibFunc_memset_pattern16:
  1434. return (!FTy.isVarArg() && NumParams == 3 &&
  1435. FTy.getParamType(0)->isPointerTy() &&
  1436. FTy.getParamType(1)->isPointerTy() &&
  1437. FTy.getParamType(2)->isIntegerTy());
  1438. case LibFunc_cxa_guard_abort:
  1439. case LibFunc_cxa_guard_acquire:
  1440. case LibFunc_cxa_guard_release:
  1441. case LibFunc_nvvm_reflect:
  1442. return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
  1443. case LibFunc_sincospi_stret:
  1444. case LibFunc_sincospif_stret:
  1445. return (NumParams == 1 && FTy.getParamType(0)->isFloatingPointTy());
  1446. case LibFunc_acos:
  1447. case LibFunc_acos_finite:
  1448. case LibFunc_acosf:
  1449. case LibFunc_acosf_finite:
  1450. case LibFunc_acosh:
  1451. case LibFunc_acosh_finite:
  1452. case LibFunc_acoshf:
  1453. case LibFunc_acoshf_finite:
  1454. case LibFunc_acoshl:
  1455. case LibFunc_acoshl_finite:
  1456. case LibFunc_acosl:
  1457. case LibFunc_acosl_finite:
  1458. case LibFunc_asin:
  1459. case LibFunc_asin_finite:
  1460. case LibFunc_asinf:
  1461. case LibFunc_asinf_finite:
  1462. case LibFunc_asinh:
  1463. case LibFunc_asinhf:
  1464. case LibFunc_asinhl:
  1465. case LibFunc_asinl:
  1466. case LibFunc_asinl_finite:
  1467. case LibFunc_atan:
  1468. case LibFunc_atanf:
  1469. case LibFunc_atanh:
  1470. case LibFunc_atanh_finite:
  1471. case LibFunc_atanhf:
  1472. case LibFunc_atanhf_finite:
  1473. case LibFunc_atanhl:
  1474. case LibFunc_atanhl_finite:
  1475. case LibFunc_atanl:
  1476. case LibFunc_cbrt:
  1477. case LibFunc_cbrtf:
  1478. case LibFunc_cbrtl:
  1479. case LibFunc_ceil:
  1480. case LibFunc_ceilf:
  1481. case LibFunc_ceill:
  1482. case LibFunc_cos:
  1483. case LibFunc_cosf:
  1484. case LibFunc_cosh:
  1485. case LibFunc_cosh_finite:
  1486. case LibFunc_coshf:
  1487. case LibFunc_coshf_finite:
  1488. case LibFunc_coshl:
  1489. case LibFunc_coshl_finite:
  1490. case LibFunc_cosl:
  1491. case LibFunc_exp10:
  1492. case LibFunc_exp10_finite:
  1493. case LibFunc_exp10f:
  1494. case LibFunc_exp10f_finite:
  1495. case LibFunc_exp10l:
  1496. case LibFunc_exp10l_finite:
  1497. case LibFunc_exp2:
  1498. case LibFunc_exp2_finite:
  1499. case LibFunc_exp2f:
  1500. case LibFunc_exp2f_finite:
  1501. case LibFunc_exp2l:
  1502. case LibFunc_exp2l_finite:
  1503. case LibFunc_exp:
  1504. case LibFunc_exp_finite:
  1505. case LibFunc_expf:
  1506. case LibFunc_expf_finite:
  1507. case LibFunc_expl:
  1508. case LibFunc_expl_finite:
  1509. case LibFunc_expm1:
  1510. case LibFunc_expm1f:
  1511. case LibFunc_expm1l:
  1512. case LibFunc_fabs:
  1513. case LibFunc_fabsf:
  1514. case LibFunc_fabsl:
  1515. case LibFunc_floor:
  1516. case LibFunc_floorf:
  1517. case LibFunc_floorl:
  1518. case LibFunc_log10:
  1519. case LibFunc_log10_finite:
  1520. case LibFunc_log10f:
  1521. case LibFunc_log10f_finite:
  1522. case LibFunc_log10l:
  1523. case LibFunc_log10l_finite:
  1524. case LibFunc_log1p:
  1525. case LibFunc_log1pf:
  1526. case LibFunc_log1pl:
  1527. case LibFunc_log2:
  1528. case LibFunc_log2_finite:
  1529. case LibFunc_log2f:
  1530. case LibFunc_log2f_finite:
  1531. case LibFunc_log2l:
  1532. case LibFunc_log2l_finite:
  1533. case LibFunc_log:
  1534. case LibFunc_log_finite:
  1535. case LibFunc_logb:
  1536. case LibFunc_logbf:
  1537. case LibFunc_logbl:
  1538. case LibFunc_logf:
  1539. case LibFunc_logf_finite:
  1540. case LibFunc_logl:
  1541. case LibFunc_logl_finite:
  1542. case LibFunc_nearbyint:
  1543. case LibFunc_nearbyintf:
  1544. case LibFunc_nearbyintl:
  1545. case LibFunc_rint:
  1546. case LibFunc_rintf:
  1547. case LibFunc_rintl:
  1548. case LibFunc_round:
  1549. case LibFunc_roundf:
  1550. case LibFunc_roundl:
  1551. case LibFunc_roundeven:
  1552. case LibFunc_roundevenf:
  1553. case LibFunc_roundevenl:
  1554. case LibFunc_sin:
  1555. case LibFunc_sinf:
  1556. case LibFunc_sinh:
  1557. case LibFunc_sinh_finite:
  1558. case LibFunc_sinhf:
  1559. case LibFunc_sinhf_finite:
  1560. case LibFunc_sinhl:
  1561. case LibFunc_sinhl_finite:
  1562. case LibFunc_sinl:
  1563. case LibFunc_sqrt:
  1564. case LibFunc_sqrt_finite:
  1565. case LibFunc_sqrtf:
  1566. case LibFunc_sqrtf_finite:
  1567. case LibFunc_sqrtl:
  1568. case LibFunc_sqrtl_finite:
  1569. case LibFunc_tan:
  1570. case LibFunc_tanf:
  1571. case LibFunc_tanh:
  1572. case LibFunc_tanhf:
  1573. case LibFunc_tanhl:
  1574. case LibFunc_tanl:
  1575. case LibFunc_trunc:
  1576. case LibFunc_truncf:
  1577. case LibFunc_truncl:
  1578. return (NumParams == 1 && FTy.getReturnType()->isFloatingPointTy() &&
  1579. FTy.getReturnType() == FTy.getParamType(0));
  1580. case LibFunc_atan2:
  1581. case LibFunc_atan2_finite:
  1582. case LibFunc_atan2f:
  1583. case LibFunc_atan2f_finite:
  1584. case LibFunc_atan2l:
  1585. case LibFunc_atan2l_finite:
  1586. case LibFunc_fmin:
  1587. case LibFunc_fminf:
  1588. case LibFunc_fminl:
  1589. case LibFunc_fmax:
  1590. case LibFunc_fmaxf:
  1591. case LibFunc_fmaxl:
  1592. case LibFunc_fmod:
  1593. case LibFunc_fmodf:
  1594. case LibFunc_fmodl:
  1595. case LibFunc_remainder:
  1596. case LibFunc_remainderf:
  1597. case LibFunc_remainderl:
  1598. case LibFunc_copysign:
  1599. case LibFunc_copysignf:
  1600. case LibFunc_copysignl:
  1601. case LibFunc_pow:
  1602. case LibFunc_pow_finite:
  1603. case LibFunc_powf:
  1604. case LibFunc_powf_finite:
  1605. case LibFunc_powl:
  1606. case LibFunc_powl_finite:
  1607. return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
  1608. FTy.getReturnType() == FTy.getParamType(0) &&
  1609. FTy.getReturnType() == FTy.getParamType(1));
  1610. case LibFunc_ldexp:
  1611. case LibFunc_ldexpf:
  1612. case LibFunc_ldexpl:
  1613. return (NumParams == 2 && FTy.getReturnType()->isFloatingPointTy() &&
  1614. FTy.getReturnType() == FTy.getParamType(0) &&
  1615. FTy.getParamType(1)->isIntegerTy(getIntSize()));
  1616. case LibFunc_ffs:
  1617. case LibFunc_ffsl:
  1618. case LibFunc_ffsll:
  1619. case LibFunc_fls:
  1620. case LibFunc_flsl:
  1621. case LibFunc_flsll:
  1622. return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
  1623. FTy.getParamType(0)->isIntegerTy());
  1624. case LibFunc_isdigit:
  1625. case LibFunc_isascii:
  1626. case LibFunc_toascii:
  1627. case LibFunc_putchar:
  1628. case LibFunc_putchar_unlocked:
  1629. return (NumParams == 1 && FTy.getReturnType()->isIntegerTy(32) &&
  1630. FTy.getReturnType() == FTy.getParamType(0));
  1631. case LibFunc_abs:
  1632. case LibFunc_labs:
  1633. case LibFunc_llabs:
  1634. return (NumParams == 1 && FTy.getReturnType()->isIntegerTy() &&
  1635. FTy.getReturnType() == FTy.getParamType(0));
  1636. case LibFunc_cxa_atexit:
  1637. return (NumParams == 3 && FTy.getReturnType()->isIntegerTy() &&
  1638. FTy.getParamType(0)->isPointerTy() &&
  1639. FTy.getParamType(1)->isPointerTy() &&
  1640. FTy.getParamType(2)->isPointerTy());
  1641. case LibFunc_sinpi:
  1642. case LibFunc_cospi:
  1643. return (NumParams == 1 && FTy.getReturnType()->isDoubleTy() &&
  1644. FTy.getReturnType() == FTy.getParamType(0));
  1645. case LibFunc_sinpif:
  1646. case LibFunc_cospif:
  1647. return (NumParams == 1 && FTy.getReturnType()->isFloatTy() &&
  1648. FTy.getReturnType() == FTy.getParamType(0));
  1649. case LibFunc_strnlen:
  1650. return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(1) &&
  1651. FTy.getParamType(0)->isPointerTy() &&
  1652. FTy.getParamType(1)->isIntegerTy(SizeTBits));
  1653. case LibFunc_posix_memalign:
  1654. return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
  1655. FTy.getParamType(0)->isPointerTy() &&
  1656. FTy.getParamType(1)->isIntegerTy(SizeTBits) &&
  1657. FTy.getParamType(2)->isIntegerTy(SizeTBits));
  1658. case LibFunc_wcslen:
  1659. return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
  1660. FTy.getReturnType()->isIntegerTy());
  1661. case LibFunc_cabs:
  1662. case LibFunc_cabsf:
  1663. case LibFunc_cabsl: {
  1664. Type* RetTy = FTy.getReturnType();
  1665. if (!RetTy->isFloatingPointTy())
  1666. return false;
  1667. // NOTE: These prototypes are target specific and currently support
  1668. // "complex" passed as an array or discrete real & imaginary parameters.
  1669. // Add other calling conventions to enable libcall optimizations.
  1670. if (NumParams == 1)
  1671. return (FTy.getParamType(0)->isArrayTy() &&
  1672. FTy.getParamType(0)->getArrayNumElements() == 2 &&
  1673. FTy.getParamType(0)->getArrayElementType() == RetTy);
  1674. else if (NumParams == 2)
  1675. return (FTy.getParamType(0) == RetTy && FTy.getParamType(1) == RetTy);
  1676. else
  1677. return false;
  1678. }
  1679. case LibFunc::NumLibFuncs:
  1680. case LibFunc::NotLibFunc:
  1681. break;
  1682. }
  1683. llvm_unreachable("Invalid libfunc");
  1684. }
  1685. bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
  1686. LibFunc &F) const {
  1687. // Intrinsics don't overlap w/libcalls; if our module has a large number of
  1688. // intrinsics, this ends up being an interesting compile time win since we
  1689. // avoid string normalization and comparison.
  1690. if (FDecl.isIntrinsic()) return false;
  1691. const Module *M = FDecl.getParent();
  1692. assert(M && "Expecting FDecl to be connected to a Module.");
  1693. return getLibFunc(FDecl.getName(), F) &&
  1694. isValidProtoForLibFunc(*FDecl.getFunctionType(), F, *M);
  1695. }
  1696. void TargetLibraryInfoImpl::disableAllFunctions() {
  1697. memset(AvailableArray, 0, sizeof(AvailableArray));
  1698. }
  1699. static bool compareByScalarFnName(const VecDesc &LHS, const VecDesc &RHS) {
  1700. return LHS.ScalarFnName < RHS.ScalarFnName;
  1701. }
  1702. static bool compareByVectorFnName(const VecDesc &LHS, const VecDesc &RHS) {
  1703. return LHS.VectorFnName < RHS.VectorFnName;
  1704. }
  1705. static bool compareWithScalarFnName(const VecDesc &LHS, StringRef S) {
  1706. return LHS.ScalarFnName < S;
  1707. }
  1708. void TargetLibraryInfoImpl::addVectorizableFunctions(ArrayRef<VecDesc> Fns) {
  1709. llvm::append_range(VectorDescs, Fns);
  1710. llvm::sort(VectorDescs, compareByScalarFnName);
  1711. llvm::append_range(ScalarDescs, Fns);
  1712. llvm::sort(ScalarDescs, compareByVectorFnName);
  1713. }
  1714. void TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib(
  1715. enum VectorLibrary VecLib) {
  1716. switch (VecLib) {
  1717. case Accelerate: {
  1718. const VecDesc VecFuncs[] = {
  1719. #define TLI_DEFINE_ACCELERATE_VECFUNCS
  1720. #include "llvm/Analysis/VecFuncs.def"
  1721. };
  1722. addVectorizableFunctions(VecFuncs);
  1723. break;
  1724. }
  1725. case DarwinLibSystemM: {
  1726. const VecDesc VecFuncs[] = {
  1727. #define TLI_DEFINE_DARWIN_LIBSYSTEM_M_VECFUNCS
  1728. #include "llvm/Analysis/VecFuncs.def"
  1729. };
  1730. addVectorizableFunctions(VecFuncs);
  1731. break;
  1732. }
  1733. case LIBMVEC_X86: {
  1734. const VecDesc VecFuncs[] = {
  1735. #define TLI_DEFINE_LIBMVEC_X86_VECFUNCS
  1736. #include "llvm/Analysis/VecFuncs.def"
  1737. };
  1738. addVectorizableFunctions(VecFuncs);
  1739. break;
  1740. }
  1741. case MASSV: {
  1742. const VecDesc VecFuncs[] = {
  1743. #define TLI_DEFINE_MASSV_VECFUNCS
  1744. #include "llvm/Analysis/VecFuncs.def"
  1745. };
  1746. addVectorizableFunctions(VecFuncs);
  1747. break;
  1748. }
  1749. case SVML: {
  1750. const VecDesc VecFuncs[] = {
  1751. #define TLI_DEFINE_SVML_VECFUNCS
  1752. #include "llvm/Analysis/VecFuncs.def"
  1753. };
  1754. addVectorizableFunctions(VecFuncs);
  1755. break;
  1756. }
  1757. case NoLibrary:
  1758. break;
  1759. }
  1760. }
  1761. bool TargetLibraryInfoImpl::isFunctionVectorizable(StringRef funcName) const {
  1762. funcName = sanitizeFunctionName(funcName);
  1763. if (funcName.empty())
  1764. return false;
  1765. std::vector<VecDesc>::const_iterator I =
  1766. llvm::lower_bound(VectorDescs, funcName, compareWithScalarFnName);
  1767. return I != VectorDescs.end() && StringRef(I->ScalarFnName) == funcName;
  1768. }
  1769. StringRef
  1770. TargetLibraryInfoImpl::getVectorizedFunction(StringRef F,
  1771. const ElementCount &VF) const {
  1772. F = sanitizeFunctionName(F);
  1773. if (F.empty())
  1774. return F;
  1775. std::vector<VecDesc>::const_iterator I =
  1776. llvm::lower_bound(VectorDescs, F, compareWithScalarFnName);
  1777. while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == F) {
  1778. if (I->VectorizationFactor == VF)
  1779. return I->VectorFnName;
  1780. ++I;
  1781. }
  1782. return StringRef();
  1783. }
  1784. TargetLibraryInfo TargetLibraryAnalysis::run(const Function &F,
  1785. FunctionAnalysisManager &) {
  1786. if (!BaselineInfoImpl)
  1787. BaselineInfoImpl =
  1788. TargetLibraryInfoImpl(Triple(F.getParent()->getTargetTriple()));
  1789. return TargetLibraryInfo(*BaselineInfoImpl, &F);
  1790. }
  1791. unsigned TargetLibraryInfoImpl::getWCharSize(const Module &M) const {
  1792. if (auto *ShortWChar = cast_or_null<ConstantAsMetadata>(
  1793. M.getModuleFlag("wchar_size")))
  1794. return cast<ConstantInt>(ShortWChar->getValue())->getZExtValue();
  1795. return 0;
  1796. }
  1797. TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()
  1798. : ImmutablePass(ID), TLA(TargetLibraryInfoImpl()) {
  1799. initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  1800. }
  1801. TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(const Triple &T)
  1802. : ImmutablePass(ID), TLA(TargetLibraryInfoImpl(T)) {
  1803. initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  1804. }
  1805. TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass(
  1806. const TargetLibraryInfoImpl &TLIImpl)
  1807. : ImmutablePass(ID), TLA(TLIImpl) {
  1808. initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  1809. }
  1810. AnalysisKey TargetLibraryAnalysis::Key;
  1811. // Register the basic pass.
  1812. INITIALIZE_PASS(TargetLibraryInfoWrapperPass, "targetlibinfo",
  1813. "Target Library Information", false, true)
  1814. char TargetLibraryInfoWrapperPass::ID = 0;
  1815. void TargetLibraryInfoWrapperPass::anchor() {}
  1816. void TargetLibraryInfoImpl::getWidestVF(StringRef ScalarF,
  1817. ElementCount &FixedVF,
  1818. ElementCount &ScalableVF) const {
  1819. ScalarF = sanitizeFunctionName(ScalarF);
  1820. // Use '0' here because a type of the form <vscale x 1 x ElTy> is not the
  1821. // same as a scalar.
  1822. ScalableVF = ElementCount::getScalable(0);
  1823. FixedVF = ElementCount::getFixed(1);
  1824. if (ScalarF.empty())
  1825. return;
  1826. std::vector<VecDesc>::const_iterator I =
  1827. llvm::lower_bound(VectorDescs, ScalarF, compareWithScalarFnName);
  1828. while (I != VectorDescs.end() && StringRef(I->ScalarFnName) == ScalarF) {
  1829. ElementCount *VF =
  1830. I->VectorizationFactor.isScalable() ? &ScalableVF : &FixedVF;
  1831. if (ElementCount::isKnownGT(I->VectorizationFactor, *VF))
  1832. *VF = I->VectorizationFactor;
  1833. ++I;
  1834. }
  1835. }