BuildLibCalls.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
  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 some functions that will create standard C libcalls.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Transforms/Utils/BuildLibCalls.h"
  13. #include "llvm/ADT/SmallString.h"
  14. #include "llvm/ADT/Statistic.h"
  15. #include "llvm/Analysis/TargetLibraryInfo.h"
  16. #include "llvm/IR/Constants.h"
  17. #include "llvm/IR/DataLayout.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/IRBuilder.h"
  20. #include "llvm/IR/Intrinsics.h"
  21. #include "llvm/IR/LLVMContext.h"
  22. #include "llvm/IR/Module.h"
  23. #include "llvm/IR/Type.h"
  24. #include "llvm/Analysis/MemoryBuiltins.h"
  25. using namespace llvm;
  26. #define DEBUG_TYPE "build-libcalls"
  27. //- Infer Attributes ---------------------------------------------------------//
  28. STATISTIC(NumReadNone, "Number of functions inferred as readnone");
  29. STATISTIC(NumInaccessibleMemOnly,
  30. "Number of functions inferred as inaccessiblememonly");
  31. STATISTIC(NumReadOnly, "Number of functions inferred as readonly");
  32. STATISTIC(NumWriteOnly, "Number of functions inferred as writeonly");
  33. STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly");
  34. STATISTIC(NumInaccessibleMemOrArgMemOnly,
  35. "Number of functions inferred as inaccessiblemem_or_argmemonly");
  36. STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind");
  37. STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture");
  38. STATISTIC(NumWriteOnlyArg, "Number of arguments inferred as writeonly");
  39. STATISTIC(NumSExtArg, "Number of arguments inferred as signext");
  40. STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly");
  41. STATISTIC(NumNoAlias, "Number of function returns inferred as noalias");
  42. STATISTIC(NumNoUndef, "Number of function returns inferred as noundef returns");
  43. STATISTIC(NumReturnedArg, "Number of arguments inferred as returned");
  44. STATISTIC(NumWillReturn, "Number of functions inferred as willreturn");
  45. static bool setDoesNotAccessMemory(Function &F) {
  46. if (F.doesNotAccessMemory())
  47. return false;
  48. F.setDoesNotAccessMemory();
  49. ++NumReadNone;
  50. return true;
  51. }
  52. static bool setOnlyAccessesInaccessibleMemory(Function &F) {
  53. if (F.onlyAccessesInaccessibleMemory())
  54. return false;
  55. F.setOnlyAccessesInaccessibleMemory();
  56. ++NumInaccessibleMemOnly;
  57. return true;
  58. }
  59. static bool setOnlyReadsMemory(Function &F) {
  60. if (F.onlyReadsMemory())
  61. return false;
  62. F.setOnlyReadsMemory();
  63. ++NumReadOnly;
  64. return true;
  65. }
  66. static bool setOnlyWritesMemory(Function &F) {
  67. if (F.onlyWritesMemory()) // writeonly or readnone
  68. return false;
  69. // Turn readonly and writeonly into readnone.
  70. if (F.hasFnAttribute(Attribute::ReadOnly)) {
  71. F.removeFnAttr(Attribute::ReadOnly);
  72. return setDoesNotAccessMemory(F);
  73. }
  74. ++NumWriteOnly;
  75. F.setOnlyWritesMemory();
  76. return true;
  77. }
  78. static bool setOnlyAccessesArgMemory(Function &F) {
  79. if (F.onlyAccessesArgMemory())
  80. return false;
  81. F.setOnlyAccessesArgMemory();
  82. ++NumArgMemOnly;
  83. return true;
  84. }
  85. static bool setOnlyAccessesInaccessibleMemOrArgMem(Function &F) {
  86. if (F.onlyAccessesInaccessibleMemOrArgMem())
  87. return false;
  88. F.setOnlyAccessesInaccessibleMemOrArgMem();
  89. ++NumInaccessibleMemOrArgMemOnly;
  90. return true;
  91. }
  92. static bool setDoesNotThrow(Function &F) {
  93. if (F.doesNotThrow())
  94. return false;
  95. F.setDoesNotThrow();
  96. ++NumNoUnwind;
  97. return true;
  98. }
  99. static bool setRetDoesNotAlias(Function &F) {
  100. if (F.hasRetAttribute(Attribute::NoAlias))
  101. return false;
  102. F.addRetAttr(Attribute::NoAlias);
  103. ++NumNoAlias;
  104. return true;
  105. }
  106. static bool setDoesNotCapture(Function &F, unsigned ArgNo) {
  107. if (F.hasParamAttribute(ArgNo, Attribute::NoCapture))
  108. return false;
  109. F.addParamAttr(ArgNo, Attribute::NoCapture);
  110. ++NumNoCapture;
  111. return true;
  112. }
  113. static bool setDoesNotAlias(Function &F, unsigned ArgNo) {
  114. if (F.hasParamAttribute(ArgNo, Attribute::NoAlias))
  115. return false;
  116. F.addParamAttr(ArgNo, Attribute::NoAlias);
  117. ++NumNoAlias;
  118. return true;
  119. }
  120. static bool setOnlyReadsMemory(Function &F, unsigned ArgNo) {
  121. if (F.hasParamAttribute(ArgNo, Attribute::ReadOnly))
  122. return false;
  123. F.addParamAttr(ArgNo, Attribute::ReadOnly);
  124. ++NumReadOnlyArg;
  125. return true;
  126. }
  127. static bool setOnlyWritesMemory(Function &F, unsigned ArgNo) {
  128. if (F.hasParamAttribute(ArgNo, Attribute::WriteOnly))
  129. return false;
  130. F.addParamAttr(ArgNo, Attribute::WriteOnly);
  131. ++NumWriteOnlyArg;
  132. return true;
  133. }
  134. static bool setSignExtendedArg(Function &F, unsigned ArgNo) {
  135. if (F.hasParamAttribute(ArgNo, Attribute::SExt))
  136. return false;
  137. F.addParamAttr(ArgNo, Attribute::SExt);
  138. ++NumSExtArg;
  139. return true;
  140. }
  141. static bool setRetNoUndef(Function &F) {
  142. if (!F.getReturnType()->isVoidTy() &&
  143. !F.hasRetAttribute(Attribute::NoUndef)) {
  144. F.addRetAttr(Attribute::NoUndef);
  145. ++NumNoUndef;
  146. return true;
  147. }
  148. return false;
  149. }
  150. static bool setArgsNoUndef(Function &F) {
  151. bool Changed = false;
  152. for (unsigned ArgNo = 0; ArgNo < F.arg_size(); ++ArgNo) {
  153. if (!F.hasParamAttribute(ArgNo, Attribute::NoUndef)) {
  154. F.addParamAttr(ArgNo, Attribute::NoUndef);
  155. ++NumNoUndef;
  156. Changed = true;
  157. }
  158. }
  159. return Changed;
  160. }
  161. static bool setArgNoUndef(Function &F, unsigned ArgNo) {
  162. if (F.hasParamAttribute(ArgNo, Attribute::NoUndef))
  163. return false;
  164. F.addParamAttr(ArgNo, Attribute::NoUndef);
  165. ++NumNoUndef;
  166. return true;
  167. }
  168. static bool setRetAndArgsNoUndef(Function &F) {
  169. bool UndefAdded = false;
  170. UndefAdded |= setRetNoUndef(F);
  171. UndefAdded |= setArgsNoUndef(F);
  172. return UndefAdded;
  173. }
  174. static bool setReturnedArg(Function &F, unsigned ArgNo) {
  175. if (F.hasParamAttribute(ArgNo, Attribute::Returned))
  176. return false;
  177. F.addParamAttr(ArgNo, Attribute::Returned);
  178. ++NumReturnedArg;
  179. return true;
  180. }
  181. static bool setNonLazyBind(Function &F) {
  182. if (F.hasFnAttribute(Attribute::NonLazyBind))
  183. return false;
  184. F.addFnAttr(Attribute::NonLazyBind);
  185. return true;
  186. }
  187. static bool setDoesNotFreeMemory(Function &F) {
  188. if (F.hasFnAttribute(Attribute::NoFree))
  189. return false;
  190. F.addFnAttr(Attribute::NoFree);
  191. return true;
  192. }
  193. static bool setWillReturn(Function &F) {
  194. if (F.hasFnAttribute(Attribute::WillReturn))
  195. return false;
  196. F.addFnAttr(Attribute::WillReturn);
  197. ++NumWillReturn;
  198. return true;
  199. }
  200. bool llvm::inferLibFuncAttributes(Module *M, StringRef Name,
  201. const TargetLibraryInfo &TLI) {
  202. Function *F = M->getFunction(Name);
  203. if (!F)
  204. return false;
  205. return inferLibFuncAttributes(*F, TLI);
  206. }
  207. bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
  208. LibFunc TheLibFunc;
  209. if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc)))
  210. return false;
  211. bool Changed = false;
  212. if(!isLibFreeFunction(&F, TheLibFunc) && !isReallocLikeFn(&F, &TLI))
  213. Changed |= setDoesNotFreeMemory(F);
  214. if (F.getParent() != nullptr && F.getParent()->getRtLibUseGOT())
  215. Changed |= setNonLazyBind(F);
  216. switch (TheLibFunc) {
  217. case LibFunc_strlen:
  218. case LibFunc_strnlen:
  219. case LibFunc_wcslen:
  220. Changed |= setOnlyReadsMemory(F);
  221. Changed |= setDoesNotThrow(F);
  222. Changed |= setOnlyAccessesArgMemory(F);
  223. Changed |= setWillReturn(F);
  224. Changed |= setDoesNotCapture(F, 0);
  225. return Changed;
  226. case LibFunc_strchr:
  227. case LibFunc_strrchr:
  228. Changed |= setOnlyAccessesArgMemory(F);
  229. Changed |= setOnlyReadsMemory(F);
  230. Changed |= setDoesNotThrow(F);
  231. Changed |= setWillReturn(F);
  232. return Changed;
  233. case LibFunc_strtol:
  234. case LibFunc_strtod:
  235. case LibFunc_strtof:
  236. case LibFunc_strtoul:
  237. case LibFunc_strtoll:
  238. case LibFunc_strtold:
  239. case LibFunc_strtoull:
  240. Changed |= setDoesNotThrow(F);
  241. Changed |= setWillReturn(F);
  242. Changed |= setDoesNotCapture(F, 1);
  243. Changed |= setOnlyReadsMemory(F, 0);
  244. return Changed;
  245. case LibFunc_strcat:
  246. case LibFunc_strncat:
  247. Changed |= setOnlyAccessesArgMemory(F);
  248. Changed |= setDoesNotThrow(F);
  249. Changed |= setWillReturn(F);
  250. Changed |= setReturnedArg(F, 0);
  251. Changed |= setDoesNotCapture(F, 1);
  252. Changed |= setOnlyReadsMemory(F, 1);
  253. Changed |= setDoesNotAlias(F, 0);
  254. Changed |= setDoesNotAlias(F, 1);
  255. return Changed;
  256. case LibFunc_strcpy:
  257. case LibFunc_strncpy:
  258. Changed |= setReturnedArg(F, 0);
  259. LLVM_FALLTHROUGH;
  260. case LibFunc_stpcpy:
  261. case LibFunc_stpncpy:
  262. Changed |= setOnlyAccessesArgMemory(F);
  263. Changed |= setDoesNotThrow(F);
  264. Changed |= setWillReturn(F);
  265. Changed |= setDoesNotCapture(F, 1);
  266. Changed |= setOnlyWritesMemory(F, 0);
  267. Changed |= setOnlyReadsMemory(F, 1);
  268. Changed |= setDoesNotAlias(F, 0);
  269. Changed |= setDoesNotAlias(F, 1);
  270. return Changed;
  271. case LibFunc_strxfrm:
  272. Changed |= setDoesNotThrow(F);
  273. Changed |= setWillReturn(F);
  274. Changed |= setDoesNotCapture(F, 0);
  275. Changed |= setDoesNotCapture(F, 1);
  276. Changed |= setOnlyReadsMemory(F, 1);
  277. return Changed;
  278. case LibFunc_strcmp: // 0,1
  279. case LibFunc_strspn: // 0,1
  280. case LibFunc_strncmp: // 0,1
  281. case LibFunc_strcspn: // 0,1
  282. Changed |= setDoesNotThrow(F);
  283. Changed |= setOnlyAccessesArgMemory(F);
  284. Changed |= setWillReturn(F);
  285. Changed |= setOnlyReadsMemory(F);
  286. Changed |= setDoesNotCapture(F, 0);
  287. Changed |= setDoesNotCapture(F, 1);
  288. return Changed;
  289. case LibFunc_strcoll:
  290. case LibFunc_strcasecmp: // 0,1
  291. case LibFunc_strncasecmp: //
  292. // Those functions may depend on the locale, which may be accessed through
  293. // global memory.
  294. Changed |= setOnlyReadsMemory(F);
  295. Changed |= setDoesNotThrow(F);
  296. Changed |= setWillReturn(F);
  297. Changed |= setDoesNotCapture(F, 0);
  298. Changed |= setDoesNotCapture(F, 1);
  299. return Changed;
  300. case LibFunc_strstr:
  301. case LibFunc_strpbrk:
  302. Changed |= setOnlyAccessesArgMemory(F);
  303. Changed |= setOnlyReadsMemory(F);
  304. Changed |= setDoesNotThrow(F);
  305. Changed |= setWillReturn(F);
  306. Changed |= setDoesNotCapture(F, 1);
  307. return Changed;
  308. case LibFunc_strtok:
  309. case LibFunc_strtok_r:
  310. Changed |= setDoesNotThrow(F);
  311. Changed |= setWillReturn(F);
  312. Changed |= setDoesNotCapture(F, 1);
  313. Changed |= setOnlyReadsMemory(F, 1);
  314. return Changed;
  315. case LibFunc_scanf:
  316. Changed |= setRetAndArgsNoUndef(F);
  317. Changed |= setDoesNotThrow(F);
  318. Changed |= setDoesNotCapture(F, 0);
  319. Changed |= setOnlyReadsMemory(F, 0);
  320. return Changed;
  321. case LibFunc_setbuf:
  322. case LibFunc_setvbuf:
  323. Changed |= setRetAndArgsNoUndef(F);
  324. Changed |= setDoesNotThrow(F);
  325. Changed |= setDoesNotCapture(F, 0);
  326. return Changed;
  327. case LibFunc_strndup:
  328. Changed |= setArgNoUndef(F, 1);
  329. LLVM_FALLTHROUGH;
  330. case LibFunc_strdup:
  331. Changed |= setOnlyAccessesInaccessibleMemOrArgMem(F);
  332. Changed |= setDoesNotThrow(F);
  333. Changed |= setRetDoesNotAlias(F);
  334. Changed |= setWillReturn(F);
  335. Changed |= setDoesNotCapture(F, 0);
  336. Changed |= setOnlyReadsMemory(F, 0);
  337. return Changed;
  338. case LibFunc_stat:
  339. case LibFunc_statvfs:
  340. Changed |= setRetAndArgsNoUndef(F);
  341. Changed |= setDoesNotThrow(F);
  342. Changed |= setDoesNotCapture(F, 0);
  343. Changed |= setDoesNotCapture(F, 1);
  344. Changed |= setOnlyReadsMemory(F, 0);
  345. return Changed;
  346. case LibFunc_sscanf:
  347. Changed |= setRetAndArgsNoUndef(F);
  348. Changed |= setDoesNotThrow(F);
  349. Changed |= setDoesNotCapture(F, 0);
  350. Changed |= setDoesNotCapture(F, 1);
  351. Changed |= setOnlyReadsMemory(F, 0);
  352. Changed |= setOnlyReadsMemory(F, 1);
  353. return Changed;
  354. case LibFunc_sprintf:
  355. Changed |= setRetAndArgsNoUndef(F);
  356. Changed |= setDoesNotThrow(F);
  357. Changed |= setDoesNotCapture(F, 0);
  358. Changed |= setDoesNotAlias(F, 0);
  359. Changed |= setOnlyWritesMemory(F, 0);
  360. Changed |= setDoesNotCapture(F, 1);
  361. Changed |= setOnlyReadsMemory(F, 1);
  362. return Changed;
  363. case LibFunc_snprintf:
  364. Changed |= setRetAndArgsNoUndef(F);
  365. Changed |= setDoesNotThrow(F);
  366. Changed |= setDoesNotCapture(F, 0);
  367. Changed |= setDoesNotAlias(F, 0);
  368. Changed |= setOnlyWritesMemory(F, 0);
  369. Changed |= setDoesNotCapture(F, 2);
  370. Changed |= setOnlyReadsMemory(F, 2);
  371. return Changed;
  372. case LibFunc_setitimer:
  373. Changed |= setRetAndArgsNoUndef(F);
  374. Changed |= setDoesNotThrow(F);
  375. Changed |= setWillReturn(F);
  376. Changed |= setDoesNotCapture(F, 1);
  377. Changed |= setDoesNotCapture(F, 2);
  378. Changed |= setOnlyReadsMemory(F, 1);
  379. return Changed;
  380. case LibFunc_system:
  381. // May throw; "system" is a valid pthread cancellation point.
  382. Changed |= setRetAndArgsNoUndef(F);
  383. Changed |= setDoesNotCapture(F, 0);
  384. Changed |= setOnlyReadsMemory(F, 0);
  385. return Changed;
  386. case LibFunc_aligned_alloc:
  387. case LibFunc_valloc:
  388. case LibFunc_malloc:
  389. case LibFunc_vec_malloc:
  390. Changed |= setOnlyAccessesInaccessibleMemory(F);
  391. Changed |= setRetAndArgsNoUndef(F);
  392. Changed |= setDoesNotThrow(F);
  393. Changed |= setRetDoesNotAlias(F);
  394. Changed |= setWillReturn(F);
  395. return Changed;
  396. case LibFunc_memcmp:
  397. Changed |= setOnlyAccessesArgMemory(F);
  398. Changed |= setOnlyReadsMemory(F);
  399. Changed |= setDoesNotThrow(F);
  400. Changed |= setWillReturn(F);
  401. Changed |= setDoesNotCapture(F, 0);
  402. Changed |= setDoesNotCapture(F, 1);
  403. return Changed;
  404. case LibFunc_memchr:
  405. case LibFunc_memrchr:
  406. Changed |= setDoesNotThrow(F);
  407. Changed |= setOnlyAccessesArgMemory(F);
  408. Changed |= setOnlyReadsMemory(F);
  409. Changed |= setWillReturn(F);
  410. return Changed;
  411. case LibFunc_modf:
  412. case LibFunc_modff:
  413. case LibFunc_modfl:
  414. Changed |= setDoesNotThrow(F);
  415. Changed |= setWillReturn(F);
  416. Changed |= setDoesNotCapture(F, 1);
  417. return Changed;
  418. case LibFunc_memcpy:
  419. Changed |= setDoesNotThrow(F);
  420. Changed |= setOnlyAccessesArgMemory(F);
  421. Changed |= setWillReturn(F);
  422. Changed |= setDoesNotAlias(F, 0);
  423. Changed |= setReturnedArg(F, 0);
  424. Changed |= setOnlyWritesMemory(F, 0);
  425. Changed |= setDoesNotAlias(F, 1);
  426. Changed |= setDoesNotCapture(F, 1);
  427. Changed |= setOnlyReadsMemory(F, 1);
  428. return Changed;
  429. case LibFunc_memmove:
  430. Changed |= setDoesNotThrow(F);
  431. Changed |= setOnlyAccessesArgMemory(F);
  432. Changed |= setWillReturn(F);
  433. Changed |= setReturnedArg(F, 0);
  434. Changed |= setOnlyWritesMemory(F, 0);
  435. Changed |= setDoesNotCapture(F, 1);
  436. Changed |= setOnlyReadsMemory(F, 1);
  437. return Changed;
  438. case LibFunc_mempcpy:
  439. case LibFunc_memccpy:
  440. Changed |= setWillReturn(F);
  441. LLVM_FALLTHROUGH;
  442. case LibFunc_memcpy_chk:
  443. Changed |= setDoesNotThrow(F);
  444. Changed |= setOnlyAccessesArgMemory(F);
  445. Changed |= setDoesNotAlias(F, 0);
  446. Changed |= setOnlyWritesMemory(F, 0);
  447. Changed |= setDoesNotAlias(F, 1);
  448. Changed |= setDoesNotCapture(F, 1);
  449. Changed |= setOnlyReadsMemory(F, 1);
  450. return Changed;
  451. case LibFunc_memalign:
  452. Changed |= setOnlyAccessesInaccessibleMemory(F);
  453. Changed |= setRetNoUndef(F);
  454. Changed |= setDoesNotThrow(F);
  455. Changed |= setRetDoesNotAlias(F);
  456. Changed |= setWillReturn(F);
  457. return Changed;
  458. case LibFunc_mkdir:
  459. Changed |= setRetAndArgsNoUndef(F);
  460. Changed |= setDoesNotThrow(F);
  461. Changed |= setDoesNotCapture(F, 0);
  462. Changed |= setOnlyReadsMemory(F, 0);
  463. return Changed;
  464. case LibFunc_mktime:
  465. Changed |= setRetAndArgsNoUndef(F);
  466. Changed |= setDoesNotThrow(F);
  467. Changed |= setWillReturn(F);
  468. Changed |= setDoesNotCapture(F, 0);
  469. return Changed;
  470. case LibFunc_realloc:
  471. case LibFunc_vec_realloc:
  472. case LibFunc_reallocf:
  473. Changed |= setOnlyAccessesInaccessibleMemOrArgMem(F);
  474. Changed |= setRetNoUndef(F);
  475. Changed |= setDoesNotThrow(F);
  476. Changed |= setRetDoesNotAlias(F);
  477. Changed |= setWillReturn(F);
  478. Changed |= setDoesNotCapture(F, 0);
  479. Changed |= setArgNoUndef(F, 1);
  480. return Changed;
  481. case LibFunc_read:
  482. // May throw; "read" is a valid pthread cancellation point.
  483. Changed |= setRetAndArgsNoUndef(F);
  484. Changed |= setDoesNotCapture(F, 1);
  485. return Changed;
  486. case LibFunc_rewind:
  487. Changed |= setRetAndArgsNoUndef(F);
  488. Changed |= setDoesNotThrow(F);
  489. Changed |= setDoesNotCapture(F, 0);
  490. return Changed;
  491. case LibFunc_rmdir:
  492. case LibFunc_remove:
  493. case LibFunc_realpath:
  494. Changed |= setRetAndArgsNoUndef(F);
  495. Changed |= setDoesNotThrow(F);
  496. Changed |= setDoesNotCapture(F, 0);
  497. Changed |= setOnlyReadsMemory(F, 0);
  498. return Changed;
  499. case LibFunc_rename:
  500. Changed |= setRetAndArgsNoUndef(F);
  501. Changed |= setDoesNotThrow(F);
  502. Changed |= setDoesNotCapture(F, 0);
  503. Changed |= setDoesNotCapture(F, 1);
  504. Changed |= setOnlyReadsMemory(F, 0);
  505. Changed |= setOnlyReadsMemory(F, 1);
  506. return Changed;
  507. case LibFunc_readlink:
  508. Changed |= setRetAndArgsNoUndef(F);
  509. Changed |= setDoesNotThrow(F);
  510. Changed |= setDoesNotCapture(F, 0);
  511. Changed |= setDoesNotCapture(F, 1);
  512. Changed |= setOnlyReadsMemory(F, 0);
  513. return Changed;
  514. case LibFunc_write:
  515. // May throw; "write" is a valid pthread cancellation point.
  516. Changed |= setRetAndArgsNoUndef(F);
  517. Changed |= setDoesNotCapture(F, 1);
  518. Changed |= setOnlyReadsMemory(F, 1);
  519. return Changed;
  520. case LibFunc_bcopy:
  521. Changed |= setDoesNotThrow(F);
  522. Changed |= setOnlyAccessesArgMemory(F);
  523. Changed |= setWillReturn(F);
  524. Changed |= setDoesNotCapture(F, 0);
  525. Changed |= setOnlyReadsMemory(F, 0);
  526. Changed |= setOnlyWritesMemory(F, 1);
  527. Changed |= setDoesNotCapture(F, 1);
  528. return Changed;
  529. case LibFunc_bcmp:
  530. Changed |= setDoesNotThrow(F);
  531. Changed |= setOnlyAccessesArgMemory(F);
  532. Changed |= setOnlyReadsMemory(F);
  533. Changed |= setWillReturn(F);
  534. Changed |= setDoesNotCapture(F, 0);
  535. Changed |= setDoesNotCapture(F, 1);
  536. return Changed;
  537. case LibFunc_bzero:
  538. Changed |= setDoesNotThrow(F);
  539. Changed |= setOnlyAccessesArgMemory(F);
  540. Changed |= setWillReturn(F);
  541. Changed |= setDoesNotCapture(F, 0);
  542. Changed |= setOnlyWritesMemory(F, 0);
  543. return Changed;
  544. case LibFunc_calloc:
  545. case LibFunc_vec_calloc:
  546. Changed |= setOnlyAccessesInaccessibleMemory(F);
  547. Changed |= setRetAndArgsNoUndef(F);
  548. Changed |= setDoesNotThrow(F);
  549. Changed |= setRetDoesNotAlias(F);
  550. Changed |= setWillReturn(F);
  551. return Changed;
  552. case LibFunc_chmod:
  553. case LibFunc_chown:
  554. Changed |= setRetAndArgsNoUndef(F);
  555. Changed |= setDoesNotThrow(F);
  556. Changed |= setDoesNotCapture(F, 0);
  557. Changed |= setOnlyReadsMemory(F, 0);
  558. return Changed;
  559. case LibFunc_ctermid:
  560. case LibFunc_clearerr:
  561. case LibFunc_closedir:
  562. Changed |= setRetAndArgsNoUndef(F);
  563. Changed |= setDoesNotThrow(F);
  564. Changed |= setDoesNotCapture(F, 0);
  565. return Changed;
  566. case LibFunc_atoi:
  567. case LibFunc_atol:
  568. case LibFunc_atof:
  569. case LibFunc_atoll:
  570. Changed |= setDoesNotThrow(F);
  571. Changed |= setOnlyReadsMemory(F);
  572. Changed |= setWillReturn(F);
  573. Changed |= setDoesNotCapture(F, 0);
  574. return Changed;
  575. case LibFunc_access:
  576. Changed |= setRetAndArgsNoUndef(F);
  577. Changed |= setDoesNotThrow(F);
  578. Changed |= setDoesNotCapture(F, 0);
  579. Changed |= setOnlyReadsMemory(F, 0);
  580. return Changed;
  581. case LibFunc_fopen:
  582. Changed |= setRetAndArgsNoUndef(F);
  583. Changed |= setDoesNotThrow(F);
  584. Changed |= setRetDoesNotAlias(F);
  585. Changed |= setDoesNotCapture(F, 0);
  586. Changed |= setDoesNotCapture(F, 1);
  587. Changed |= setOnlyReadsMemory(F, 0);
  588. Changed |= setOnlyReadsMemory(F, 1);
  589. return Changed;
  590. case LibFunc_fdopen:
  591. Changed |= setRetAndArgsNoUndef(F);
  592. Changed |= setDoesNotThrow(F);
  593. Changed |= setRetDoesNotAlias(F);
  594. Changed |= setDoesNotCapture(F, 1);
  595. Changed |= setOnlyReadsMemory(F, 1);
  596. return Changed;
  597. case LibFunc_feof:
  598. Changed |= setRetAndArgsNoUndef(F);
  599. Changed |= setDoesNotThrow(F);
  600. Changed |= setDoesNotCapture(F, 0);
  601. return Changed;
  602. case LibFunc_free:
  603. case LibFunc_vec_free:
  604. Changed |= setOnlyAccessesInaccessibleMemOrArgMem(F);
  605. Changed |= setArgsNoUndef(F);
  606. Changed |= setDoesNotThrow(F);
  607. Changed |= setWillReturn(F);
  608. Changed |= setDoesNotCapture(F, 0);
  609. return Changed;
  610. case LibFunc_fseek:
  611. case LibFunc_ftell:
  612. case LibFunc_fgetc:
  613. case LibFunc_fgetc_unlocked:
  614. case LibFunc_fseeko:
  615. case LibFunc_ftello:
  616. case LibFunc_fileno:
  617. case LibFunc_fflush:
  618. case LibFunc_fclose:
  619. case LibFunc_fsetpos:
  620. case LibFunc_flockfile:
  621. case LibFunc_funlockfile:
  622. case LibFunc_ftrylockfile:
  623. Changed |= setRetAndArgsNoUndef(F);
  624. Changed |= setDoesNotThrow(F);
  625. Changed |= setDoesNotCapture(F, 0);
  626. return Changed;
  627. case LibFunc_ferror:
  628. Changed |= setRetAndArgsNoUndef(F);
  629. Changed |= setDoesNotThrow(F);
  630. Changed |= setDoesNotCapture(F, 0);
  631. Changed |= setOnlyReadsMemory(F);
  632. return Changed;
  633. case LibFunc_fputc:
  634. case LibFunc_fputc_unlocked:
  635. case LibFunc_fstat:
  636. Changed |= setRetAndArgsNoUndef(F);
  637. Changed |= setDoesNotThrow(F);
  638. Changed |= setDoesNotCapture(F, 1);
  639. return Changed;
  640. case LibFunc_frexp:
  641. case LibFunc_frexpf:
  642. case LibFunc_frexpl:
  643. Changed |= setDoesNotThrow(F);
  644. Changed |= setWillReturn(F);
  645. Changed |= setDoesNotCapture(F, 1);
  646. return Changed;
  647. case LibFunc_fstatvfs:
  648. Changed |= setRetAndArgsNoUndef(F);
  649. Changed |= setDoesNotThrow(F);
  650. Changed |= setDoesNotCapture(F, 1);
  651. return Changed;
  652. case LibFunc_fgets:
  653. case LibFunc_fgets_unlocked:
  654. Changed |= setRetAndArgsNoUndef(F);
  655. Changed |= setDoesNotThrow(F);
  656. Changed |= setDoesNotCapture(F, 2);
  657. return Changed;
  658. case LibFunc_fread:
  659. case LibFunc_fread_unlocked:
  660. Changed |= setRetAndArgsNoUndef(F);
  661. Changed |= setDoesNotThrow(F);
  662. Changed |= setDoesNotCapture(F, 0);
  663. Changed |= setDoesNotCapture(F, 3);
  664. return Changed;
  665. case LibFunc_fwrite:
  666. case LibFunc_fwrite_unlocked:
  667. Changed |= setRetAndArgsNoUndef(F);
  668. Changed |= setDoesNotThrow(F);
  669. Changed |= setDoesNotCapture(F, 0);
  670. Changed |= setDoesNotCapture(F, 3);
  671. // FIXME: readonly #1?
  672. return Changed;
  673. case LibFunc_fputs:
  674. case LibFunc_fputs_unlocked:
  675. Changed |= setRetAndArgsNoUndef(F);
  676. Changed |= setDoesNotThrow(F);
  677. Changed |= setDoesNotCapture(F, 0);
  678. Changed |= setDoesNotCapture(F, 1);
  679. Changed |= setOnlyReadsMemory(F, 0);
  680. return Changed;
  681. case LibFunc_fscanf:
  682. case LibFunc_fprintf:
  683. Changed |= setRetAndArgsNoUndef(F);
  684. Changed |= setDoesNotThrow(F);
  685. Changed |= setDoesNotCapture(F, 0);
  686. Changed |= setDoesNotCapture(F, 1);
  687. Changed |= setOnlyReadsMemory(F, 1);
  688. return Changed;
  689. case LibFunc_fgetpos:
  690. Changed |= setRetAndArgsNoUndef(F);
  691. Changed |= setDoesNotThrow(F);
  692. Changed |= setDoesNotCapture(F, 0);
  693. Changed |= setDoesNotCapture(F, 1);
  694. return Changed;
  695. case LibFunc_getc:
  696. Changed |= setRetAndArgsNoUndef(F);
  697. Changed |= setDoesNotThrow(F);
  698. Changed |= setDoesNotCapture(F, 0);
  699. return Changed;
  700. case LibFunc_getlogin_r:
  701. Changed |= setRetAndArgsNoUndef(F);
  702. Changed |= setDoesNotThrow(F);
  703. Changed |= setDoesNotCapture(F, 0);
  704. return Changed;
  705. case LibFunc_getc_unlocked:
  706. Changed |= setRetAndArgsNoUndef(F);
  707. Changed |= setDoesNotThrow(F);
  708. Changed |= setDoesNotCapture(F, 0);
  709. return Changed;
  710. case LibFunc_getenv:
  711. Changed |= setRetAndArgsNoUndef(F);
  712. Changed |= setDoesNotThrow(F);
  713. Changed |= setOnlyReadsMemory(F);
  714. Changed |= setDoesNotCapture(F, 0);
  715. return Changed;
  716. case LibFunc_gets:
  717. case LibFunc_getchar:
  718. case LibFunc_getchar_unlocked:
  719. Changed |= setRetAndArgsNoUndef(F);
  720. Changed |= setDoesNotThrow(F);
  721. return Changed;
  722. case LibFunc_getitimer:
  723. Changed |= setRetAndArgsNoUndef(F);
  724. Changed |= setDoesNotThrow(F);
  725. Changed |= setDoesNotCapture(F, 1);
  726. return Changed;
  727. case LibFunc_getpwnam:
  728. Changed |= setRetAndArgsNoUndef(F);
  729. Changed |= setDoesNotThrow(F);
  730. Changed |= setDoesNotCapture(F, 0);
  731. Changed |= setOnlyReadsMemory(F, 0);
  732. return Changed;
  733. case LibFunc_ungetc:
  734. Changed |= setRetAndArgsNoUndef(F);
  735. Changed |= setDoesNotThrow(F);
  736. Changed |= setDoesNotCapture(F, 1);
  737. return Changed;
  738. case LibFunc_uname:
  739. Changed |= setRetAndArgsNoUndef(F);
  740. Changed |= setDoesNotThrow(F);
  741. Changed |= setDoesNotCapture(F, 0);
  742. return Changed;
  743. case LibFunc_unlink:
  744. Changed |= setRetAndArgsNoUndef(F);
  745. Changed |= setDoesNotThrow(F);
  746. Changed |= setDoesNotCapture(F, 0);
  747. Changed |= setOnlyReadsMemory(F, 0);
  748. return Changed;
  749. case LibFunc_unsetenv:
  750. Changed |= setRetAndArgsNoUndef(F);
  751. Changed |= setDoesNotThrow(F);
  752. Changed |= setDoesNotCapture(F, 0);
  753. Changed |= setOnlyReadsMemory(F, 0);
  754. return Changed;
  755. case LibFunc_utime:
  756. case LibFunc_utimes:
  757. Changed |= setRetAndArgsNoUndef(F);
  758. Changed |= setDoesNotThrow(F);
  759. Changed |= setDoesNotCapture(F, 0);
  760. Changed |= setDoesNotCapture(F, 1);
  761. Changed |= setOnlyReadsMemory(F, 0);
  762. Changed |= setOnlyReadsMemory(F, 1);
  763. return Changed;
  764. case LibFunc_putc:
  765. case LibFunc_putc_unlocked:
  766. Changed |= setRetAndArgsNoUndef(F);
  767. Changed |= setDoesNotThrow(F);
  768. Changed |= setDoesNotCapture(F, 1);
  769. return Changed;
  770. case LibFunc_puts:
  771. case LibFunc_printf:
  772. case LibFunc_perror:
  773. Changed |= setRetAndArgsNoUndef(F);
  774. Changed |= setDoesNotThrow(F);
  775. Changed |= setDoesNotCapture(F, 0);
  776. Changed |= setOnlyReadsMemory(F, 0);
  777. return Changed;
  778. case LibFunc_pread:
  779. // May throw; "pread" is a valid pthread cancellation point.
  780. Changed |= setRetAndArgsNoUndef(F);
  781. Changed |= setDoesNotCapture(F, 1);
  782. return Changed;
  783. case LibFunc_pwrite:
  784. // May throw; "pwrite" is a valid pthread cancellation point.
  785. Changed |= setRetAndArgsNoUndef(F);
  786. Changed |= setDoesNotCapture(F, 1);
  787. Changed |= setOnlyReadsMemory(F, 1);
  788. return Changed;
  789. case LibFunc_putchar:
  790. case LibFunc_putchar_unlocked:
  791. Changed |= setRetAndArgsNoUndef(F);
  792. Changed |= setDoesNotThrow(F);
  793. return Changed;
  794. case LibFunc_popen:
  795. Changed |= setRetAndArgsNoUndef(F);
  796. Changed |= setDoesNotThrow(F);
  797. Changed |= setRetDoesNotAlias(F);
  798. Changed |= setDoesNotCapture(F, 0);
  799. Changed |= setDoesNotCapture(F, 1);
  800. Changed |= setOnlyReadsMemory(F, 0);
  801. Changed |= setOnlyReadsMemory(F, 1);
  802. return Changed;
  803. case LibFunc_pclose:
  804. Changed |= setRetAndArgsNoUndef(F);
  805. Changed |= setDoesNotThrow(F);
  806. Changed |= setDoesNotCapture(F, 0);
  807. return Changed;
  808. case LibFunc_vscanf:
  809. Changed |= setRetAndArgsNoUndef(F);
  810. Changed |= setDoesNotThrow(F);
  811. Changed |= setDoesNotCapture(F, 0);
  812. Changed |= setOnlyReadsMemory(F, 0);
  813. return Changed;
  814. case LibFunc_vsscanf:
  815. Changed |= setRetAndArgsNoUndef(F);
  816. Changed |= setDoesNotThrow(F);
  817. Changed |= setDoesNotCapture(F, 0);
  818. Changed |= setDoesNotCapture(F, 1);
  819. Changed |= setOnlyReadsMemory(F, 0);
  820. Changed |= setOnlyReadsMemory(F, 1);
  821. return Changed;
  822. case LibFunc_vfscanf:
  823. Changed |= setRetAndArgsNoUndef(F);
  824. Changed |= setDoesNotThrow(F);
  825. Changed |= setDoesNotCapture(F, 0);
  826. Changed |= setDoesNotCapture(F, 1);
  827. Changed |= setOnlyReadsMemory(F, 1);
  828. return Changed;
  829. case LibFunc_vprintf:
  830. Changed |= setRetAndArgsNoUndef(F);
  831. Changed |= setDoesNotThrow(F);
  832. Changed |= setDoesNotCapture(F, 0);
  833. Changed |= setOnlyReadsMemory(F, 0);
  834. return Changed;
  835. case LibFunc_vfprintf:
  836. case LibFunc_vsprintf:
  837. Changed |= setRetAndArgsNoUndef(F);
  838. Changed |= setDoesNotThrow(F);
  839. Changed |= setDoesNotCapture(F, 0);
  840. Changed |= setDoesNotCapture(F, 1);
  841. Changed |= setOnlyReadsMemory(F, 1);
  842. return Changed;
  843. case LibFunc_vsnprintf:
  844. Changed |= setRetAndArgsNoUndef(F);
  845. Changed |= setDoesNotThrow(F);
  846. Changed |= setDoesNotCapture(F, 0);
  847. Changed |= setDoesNotCapture(F, 2);
  848. Changed |= setOnlyReadsMemory(F, 2);
  849. return Changed;
  850. case LibFunc_open:
  851. // May throw; "open" is a valid pthread cancellation point.
  852. Changed |= setRetAndArgsNoUndef(F);
  853. Changed |= setDoesNotCapture(F, 0);
  854. Changed |= setOnlyReadsMemory(F, 0);
  855. return Changed;
  856. case LibFunc_opendir:
  857. Changed |= setRetAndArgsNoUndef(F);
  858. Changed |= setDoesNotThrow(F);
  859. Changed |= setRetDoesNotAlias(F);
  860. Changed |= setDoesNotCapture(F, 0);
  861. Changed |= setOnlyReadsMemory(F, 0);
  862. return Changed;
  863. case LibFunc_tmpfile:
  864. Changed |= setRetAndArgsNoUndef(F);
  865. Changed |= setDoesNotThrow(F);
  866. Changed |= setRetDoesNotAlias(F);
  867. return Changed;
  868. case LibFunc_times:
  869. Changed |= setRetAndArgsNoUndef(F);
  870. Changed |= setDoesNotThrow(F);
  871. Changed |= setDoesNotCapture(F, 0);
  872. return Changed;
  873. case LibFunc_htonl:
  874. case LibFunc_htons:
  875. case LibFunc_ntohl:
  876. case LibFunc_ntohs:
  877. Changed |= setDoesNotThrow(F);
  878. Changed |= setDoesNotAccessMemory(F);
  879. return Changed;
  880. case LibFunc_lstat:
  881. Changed |= setRetAndArgsNoUndef(F);
  882. Changed |= setDoesNotThrow(F);
  883. Changed |= setDoesNotCapture(F, 0);
  884. Changed |= setDoesNotCapture(F, 1);
  885. Changed |= setOnlyReadsMemory(F, 0);
  886. return Changed;
  887. case LibFunc_lchown:
  888. Changed |= setRetAndArgsNoUndef(F);
  889. Changed |= setDoesNotThrow(F);
  890. Changed |= setDoesNotCapture(F, 0);
  891. Changed |= setOnlyReadsMemory(F, 0);
  892. return Changed;
  893. case LibFunc_qsort:
  894. // May throw; places call through function pointer.
  895. // Cannot give undef pointer/size
  896. Changed |= setRetAndArgsNoUndef(F);
  897. Changed |= setDoesNotCapture(F, 3);
  898. return Changed;
  899. case LibFunc_dunder_strndup:
  900. Changed |= setArgNoUndef(F, 1);
  901. LLVM_FALLTHROUGH;
  902. case LibFunc_dunder_strdup:
  903. Changed |= setDoesNotThrow(F);
  904. Changed |= setRetDoesNotAlias(F);
  905. Changed |= setWillReturn(F);
  906. Changed |= setDoesNotCapture(F, 0);
  907. Changed |= setOnlyReadsMemory(F, 0);
  908. return Changed;
  909. case LibFunc_dunder_strtok_r:
  910. Changed |= setDoesNotThrow(F);
  911. Changed |= setDoesNotCapture(F, 1);
  912. Changed |= setOnlyReadsMemory(F, 1);
  913. return Changed;
  914. case LibFunc_under_IO_getc:
  915. Changed |= setRetAndArgsNoUndef(F);
  916. Changed |= setDoesNotThrow(F);
  917. Changed |= setDoesNotCapture(F, 0);
  918. return Changed;
  919. case LibFunc_under_IO_putc:
  920. Changed |= setRetAndArgsNoUndef(F);
  921. Changed |= setDoesNotThrow(F);
  922. Changed |= setDoesNotCapture(F, 1);
  923. return Changed;
  924. case LibFunc_dunder_isoc99_scanf:
  925. Changed |= setRetAndArgsNoUndef(F);
  926. Changed |= setDoesNotThrow(F);
  927. Changed |= setDoesNotCapture(F, 0);
  928. Changed |= setOnlyReadsMemory(F, 0);
  929. return Changed;
  930. case LibFunc_stat64:
  931. case LibFunc_lstat64:
  932. case LibFunc_statvfs64:
  933. Changed |= setRetAndArgsNoUndef(F);
  934. Changed |= setDoesNotThrow(F);
  935. Changed |= setDoesNotCapture(F, 0);
  936. Changed |= setDoesNotCapture(F, 1);
  937. Changed |= setOnlyReadsMemory(F, 0);
  938. return Changed;
  939. case LibFunc_dunder_isoc99_sscanf:
  940. Changed |= setRetAndArgsNoUndef(F);
  941. Changed |= setDoesNotThrow(F);
  942. Changed |= setDoesNotCapture(F, 0);
  943. Changed |= setDoesNotCapture(F, 1);
  944. Changed |= setOnlyReadsMemory(F, 0);
  945. Changed |= setOnlyReadsMemory(F, 1);
  946. return Changed;
  947. case LibFunc_fopen64:
  948. Changed |= setRetAndArgsNoUndef(F);
  949. Changed |= setDoesNotThrow(F);
  950. Changed |= setRetDoesNotAlias(F);
  951. Changed |= setDoesNotCapture(F, 0);
  952. Changed |= setDoesNotCapture(F, 1);
  953. Changed |= setOnlyReadsMemory(F, 0);
  954. Changed |= setOnlyReadsMemory(F, 1);
  955. return Changed;
  956. case LibFunc_fseeko64:
  957. case LibFunc_ftello64:
  958. Changed |= setRetAndArgsNoUndef(F);
  959. Changed |= setDoesNotThrow(F);
  960. Changed |= setDoesNotCapture(F, 0);
  961. return Changed;
  962. case LibFunc_tmpfile64:
  963. Changed |= setRetAndArgsNoUndef(F);
  964. Changed |= setDoesNotThrow(F);
  965. Changed |= setRetDoesNotAlias(F);
  966. return Changed;
  967. case LibFunc_fstat64:
  968. case LibFunc_fstatvfs64:
  969. Changed |= setRetAndArgsNoUndef(F);
  970. Changed |= setDoesNotThrow(F);
  971. Changed |= setDoesNotCapture(F, 1);
  972. return Changed;
  973. case LibFunc_open64:
  974. // May throw; "open" is a valid pthread cancellation point.
  975. Changed |= setRetAndArgsNoUndef(F);
  976. Changed |= setDoesNotCapture(F, 0);
  977. Changed |= setOnlyReadsMemory(F, 0);
  978. return Changed;
  979. case LibFunc_gettimeofday:
  980. // Currently some platforms have the restrict keyword on the arguments to
  981. // gettimeofday. To be conservative, do not add noalias to gettimeofday's
  982. // arguments.
  983. Changed |= setRetAndArgsNoUndef(F);
  984. Changed |= setDoesNotThrow(F);
  985. Changed |= setDoesNotCapture(F, 0);
  986. Changed |= setDoesNotCapture(F, 1);
  987. return Changed;
  988. case LibFunc_memset_pattern4:
  989. case LibFunc_memset_pattern8:
  990. case LibFunc_memset_pattern16:
  991. Changed |= setDoesNotCapture(F, 0);
  992. Changed |= setDoesNotCapture(F, 1);
  993. Changed |= setOnlyReadsMemory(F, 1);
  994. LLVM_FALLTHROUGH;
  995. case LibFunc_memset:
  996. Changed |= setWillReturn(F);
  997. LLVM_FALLTHROUGH;
  998. case LibFunc_memset_chk:
  999. Changed |= setOnlyAccessesArgMemory(F);
  1000. Changed |= setOnlyWritesMemory(F, 0);
  1001. Changed |= setDoesNotThrow(F);
  1002. return Changed;
  1003. // int __nvvm_reflect(const char *)
  1004. case LibFunc_nvvm_reflect:
  1005. Changed |= setRetAndArgsNoUndef(F);
  1006. Changed |= setDoesNotAccessMemory(F);
  1007. Changed |= setDoesNotThrow(F);
  1008. return Changed;
  1009. case LibFunc_ldexp:
  1010. case LibFunc_ldexpf:
  1011. case LibFunc_ldexpl:
  1012. Changed |= setSignExtendedArg(F, 1);
  1013. Changed |= setWillReturn(F);
  1014. return Changed;
  1015. case LibFunc_abs:
  1016. case LibFunc_acos:
  1017. case LibFunc_acosf:
  1018. case LibFunc_acosh:
  1019. case LibFunc_acoshf:
  1020. case LibFunc_acoshl:
  1021. case LibFunc_acosl:
  1022. case LibFunc_asin:
  1023. case LibFunc_asinf:
  1024. case LibFunc_asinh:
  1025. case LibFunc_asinhf:
  1026. case LibFunc_asinhl:
  1027. case LibFunc_asinl:
  1028. case LibFunc_atan:
  1029. case LibFunc_atan2:
  1030. case LibFunc_atan2f:
  1031. case LibFunc_atan2l:
  1032. case LibFunc_atanf:
  1033. case LibFunc_atanh:
  1034. case LibFunc_atanhf:
  1035. case LibFunc_atanhl:
  1036. case LibFunc_atanl:
  1037. case LibFunc_cbrt:
  1038. case LibFunc_cbrtf:
  1039. case LibFunc_cbrtl:
  1040. case LibFunc_ceil:
  1041. case LibFunc_ceilf:
  1042. case LibFunc_ceill:
  1043. case LibFunc_copysign:
  1044. case LibFunc_copysignf:
  1045. case LibFunc_copysignl:
  1046. case LibFunc_cos:
  1047. case LibFunc_cosh:
  1048. case LibFunc_coshf:
  1049. case LibFunc_coshl:
  1050. case LibFunc_cosf:
  1051. case LibFunc_cosl:
  1052. case LibFunc_cospi:
  1053. case LibFunc_cospif:
  1054. case LibFunc_exp:
  1055. case LibFunc_expf:
  1056. case LibFunc_expl:
  1057. case LibFunc_exp2:
  1058. case LibFunc_exp2f:
  1059. case LibFunc_exp2l:
  1060. case LibFunc_expm1:
  1061. case LibFunc_expm1f:
  1062. case LibFunc_expm1l:
  1063. case LibFunc_fabs:
  1064. case LibFunc_fabsf:
  1065. case LibFunc_fabsl:
  1066. case LibFunc_ffs:
  1067. case LibFunc_ffsl:
  1068. case LibFunc_ffsll:
  1069. case LibFunc_floor:
  1070. case LibFunc_floorf:
  1071. case LibFunc_floorl:
  1072. case LibFunc_fls:
  1073. case LibFunc_flsl:
  1074. case LibFunc_flsll:
  1075. case LibFunc_fmax:
  1076. case LibFunc_fmaxf:
  1077. case LibFunc_fmaxl:
  1078. case LibFunc_fmin:
  1079. case LibFunc_fminf:
  1080. case LibFunc_fminl:
  1081. case LibFunc_fmod:
  1082. case LibFunc_fmodf:
  1083. case LibFunc_fmodl:
  1084. case LibFunc_isascii:
  1085. case LibFunc_isdigit:
  1086. case LibFunc_labs:
  1087. case LibFunc_llabs:
  1088. case LibFunc_log:
  1089. case LibFunc_log10:
  1090. case LibFunc_log10f:
  1091. case LibFunc_log10l:
  1092. case LibFunc_log1p:
  1093. case LibFunc_log1pf:
  1094. case LibFunc_log1pl:
  1095. case LibFunc_log2:
  1096. case LibFunc_log2f:
  1097. case LibFunc_log2l:
  1098. case LibFunc_logb:
  1099. case LibFunc_logbf:
  1100. case LibFunc_logbl:
  1101. case LibFunc_logf:
  1102. case LibFunc_logl:
  1103. case LibFunc_nearbyint:
  1104. case LibFunc_nearbyintf:
  1105. case LibFunc_nearbyintl:
  1106. case LibFunc_pow:
  1107. case LibFunc_powf:
  1108. case LibFunc_powl:
  1109. case LibFunc_rint:
  1110. case LibFunc_rintf:
  1111. case LibFunc_rintl:
  1112. case LibFunc_round:
  1113. case LibFunc_roundf:
  1114. case LibFunc_roundl:
  1115. case LibFunc_sin:
  1116. case LibFunc_sincospif_stret:
  1117. case LibFunc_sinf:
  1118. case LibFunc_sinh:
  1119. case LibFunc_sinhf:
  1120. case LibFunc_sinhl:
  1121. case LibFunc_sinl:
  1122. case LibFunc_sinpi:
  1123. case LibFunc_sinpif:
  1124. case LibFunc_sqrt:
  1125. case LibFunc_sqrtf:
  1126. case LibFunc_sqrtl:
  1127. case LibFunc_tan:
  1128. case LibFunc_tanf:
  1129. case LibFunc_tanh:
  1130. case LibFunc_tanhf:
  1131. case LibFunc_tanhl:
  1132. case LibFunc_tanl:
  1133. case LibFunc_toascii:
  1134. case LibFunc_trunc:
  1135. case LibFunc_truncf:
  1136. case LibFunc_truncl:
  1137. Changed |= setDoesNotThrow(F);
  1138. Changed |= setDoesNotFreeMemory(F);
  1139. Changed |= setOnlyWritesMemory(F);
  1140. Changed |= setWillReturn(F);
  1141. return Changed;
  1142. default:
  1143. // FIXME: It'd be really nice to cover all the library functions we're
  1144. // aware of here.
  1145. return false;
  1146. }
  1147. }
  1148. bool llvm::hasFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
  1149. LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn) {
  1150. switch (Ty->getTypeID()) {
  1151. case Type::HalfTyID:
  1152. return false;
  1153. case Type::FloatTyID:
  1154. return TLI->has(FloatFn);
  1155. case Type::DoubleTyID:
  1156. return TLI->has(DoubleFn);
  1157. default:
  1158. return TLI->has(LongDoubleFn);
  1159. }
  1160. }
  1161. StringRef llvm::getFloatFnName(const TargetLibraryInfo *TLI, Type *Ty,
  1162. LibFunc DoubleFn, LibFunc FloatFn,
  1163. LibFunc LongDoubleFn) {
  1164. assert(hasFloatFn(TLI, Ty, DoubleFn, FloatFn, LongDoubleFn) &&
  1165. "Cannot get name for unavailable function!");
  1166. switch (Ty->getTypeID()) {
  1167. case Type::HalfTyID:
  1168. llvm_unreachable("No name for HalfTy!");
  1169. case Type::FloatTyID:
  1170. return TLI->getName(FloatFn);
  1171. case Type::DoubleTyID:
  1172. return TLI->getName(DoubleFn);
  1173. default:
  1174. return TLI->getName(LongDoubleFn);
  1175. }
  1176. }
  1177. //- Emit LibCalls ------------------------------------------------------------//
  1178. Value *llvm::castToCStr(Value *V, IRBuilderBase &B) {
  1179. unsigned AS = V->getType()->getPointerAddressSpace();
  1180. return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
  1181. }
  1182. static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType,
  1183. ArrayRef<Type *> ParamTypes,
  1184. ArrayRef<Value *> Operands, IRBuilderBase &B,
  1185. const TargetLibraryInfo *TLI,
  1186. bool IsVaArgs = false) {
  1187. if (!TLI->has(TheLibFunc))
  1188. return nullptr;
  1189. Module *M = B.GetInsertBlock()->getModule();
  1190. StringRef FuncName = TLI->getName(TheLibFunc);
  1191. FunctionType *FuncType = FunctionType::get(ReturnType, ParamTypes, IsVaArgs);
  1192. FunctionCallee Callee = M->getOrInsertFunction(FuncName, FuncType);
  1193. inferLibFuncAttributes(M, FuncName, *TLI);
  1194. CallInst *CI = B.CreateCall(Callee, Operands, FuncName);
  1195. if (const Function *F =
  1196. dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
  1197. CI->setCallingConv(F->getCallingConv());
  1198. return CI;
  1199. }
  1200. Value *llvm::emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL,
  1201. const TargetLibraryInfo *TLI) {
  1202. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1203. return emitLibCall(LibFunc_strlen, DL.getIntPtrType(Context),
  1204. B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI);
  1205. }
  1206. Value *llvm::emitStrDup(Value *Ptr, IRBuilderBase &B,
  1207. const TargetLibraryInfo *TLI) {
  1208. return emitLibCall(LibFunc_strdup, B.getInt8PtrTy(), B.getInt8PtrTy(),
  1209. castToCStr(Ptr, B), B, TLI);
  1210. }
  1211. Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilderBase &B,
  1212. const TargetLibraryInfo *TLI) {
  1213. Type *I8Ptr = B.getInt8PtrTy();
  1214. Type *I32Ty = B.getInt32Ty();
  1215. return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, I32Ty},
  1216. {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, B, TLI);
  1217. }
  1218. Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
  1219. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  1220. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1221. return emitLibCall(
  1222. LibFunc_strncmp, B.getInt32Ty(),
  1223. {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
  1224. {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
  1225. }
  1226. Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B,
  1227. const TargetLibraryInfo *TLI) {
  1228. Type *I8Ptr = Dst->getType();
  1229. return emitLibCall(LibFunc_strcpy, I8Ptr, {I8Ptr, I8Ptr},
  1230. {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI);
  1231. }
  1232. Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B,
  1233. const TargetLibraryInfo *TLI) {
  1234. Type *I8Ptr = B.getInt8PtrTy();
  1235. return emitLibCall(LibFunc_stpcpy, I8Ptr, {I8Ptr, I8Ptr},
  1236. {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI);
  1237. }
  1238. Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
  1239. const TargetLibraryInfo *TLI) {
  1240. Type *I8Ptr = B.getInt8PtrTy();
  1241. return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()},
  1242. {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI);
  1243. }
  1244. Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
  1245. const TargetLibraryInfo *TLI) {
  1246. Type *I8Ptr = B.getInt8PtrTy();
  1247. return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()},
  1248. {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI);
  1249. }
  1250. Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
  1251. IRBuilderBase &B, const DataLayout &DL,
  1252. const TargetLibraryInfo *TLI) {
  1253. if (!TLI->has(LibFunc_memcpy_chk))
  1254. return nullptr;
  1255. Module *M = B.GetInsertBlock()->getModule();
  1256. AttributeList AS;
  1257. AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex,
  1258. Attribute::NoUnwind);
  1259. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1260. FunctionCallee MemCpy = M->getOrInsertFunction(
  1261. "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
  1262. B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
  1263. DL.getIntPtrType(Context));
  1264. Dst = castToCStr(Dst, B);
  1265. Src = castToCStr(Src, B);
  1266. CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
  1267. if (const Function *F =
  1268. dyn_cast<Function>(MemCpy.getCallee()->stripPointerCasts()))
  1269. CI->setCallingConv(F->getCallingConv());
  1270. return CI;
  1271. }
  1272. Value *llvm::emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
  1273. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  1274. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1275. return emitLibCall(
  1276. LibFunc_mempcpy, B.getInt8PtrTy(),
  1277. {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
  1278. {Dst, Src, Len}, B, TLI);
  1279. }
  1280. Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B,
  1281. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  1282. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1283. return emitLibCall(
  1284. LibFunc_memchr, B.getInt8PtrTy(),
  1285. {B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context)},
  1286. {castToCStr(Ptr, B), Val, Len}, B, TLI);
  1287. }
  1288. Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
  1289. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  1290. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1291. return emitLibCall(
  1292. LibFunc_memcmp, B.getInt32Ty(),
  1293. {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
  1294. {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
  1295. }
  1296. Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
  1297. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  1298. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1299. return emitLibCall(
  1300. LibFunc_bcmp, B.getInt32Ty(),
  1301. {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
  1302. {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
  1303. }
  1304. Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
  1305. IRBuilderBase &B, const TargetLibraryInfo *TLI) {
  1306. return emitLibCall(
  1307. LibFunc_memccpy, B.getInt8PtrTy(),
  1308. {B.getInt8PtrTy(), B.getInt8PtrTy(), B.getInt32Ty(), Len->getType()},
  1309. {Ptr1, Ptr2, Val, Len}, B, TLI);
  1310. }
  1311. Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
  1312. ArrayRef<Value *> VariadicArgs, IRBuilderBase &B,
  1313. const TargetLibraryInfo *TLI) {
  1314. SmallVector<Value *, 8> Args{castToCStr(Dest, B), Size, castToCStr(Fmt, B)};
  1315. llvm::append_range(Args, VariadicArgs);
  1316. return emitLibCall(LibFunc_snprintf, B.getInt32Ty(),
  1317. {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy()},
  1318. Args, B, TLI, /*IsVaArgs=*/true);
  1319. }
  1320. Value *llvm::emitSPrintf(Value *Dest, Value *Fmt,
  1321. ArrayRef<Value *> VariadicArgs, IRBuilderBase &B,
  1322. const TargetLibraryInfo *TLI) {
  1323. SmallVector<Value *, 8> Args{castToCStr(Dest, B), castToCStr(Fmt, B)};
  1324. llvm::append_range(Args, VariadicArgs);
  1325. return emitLibCall(LibFunc_sprintf, B.getInt32Ty(),
  1326. {B.getInt8PtrTy(), B.getInt8PtrTy()}, Args, B, TLI,
  1327. /*IsVaArgs=*/true);
  1328. }
  1329. Value *llvm::emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B,
  1330. const TargetLibraryInfo *TLI) {
  1331. return emitLibCall(LibFunc_strcat, B.getInt8PtrTy(),
  1332. {B.getInt8PtrTy(), B.getInt8PtrTy()},
  1333. {castToCStr(Dest, B), castToCStr(Src, B)}, B, TLI);
  1334. }
  1335. Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
  1336. const TargetLibraryInfo *TLI) {
  1337. return emitLibCall(LibFunc_strlcpy, Size->getType(),
  1338. {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()},
  1339. {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI);
  1340. }
  1341. Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
  1342. const TargetLibraryInfo *TLI) {
  1343. return emitLibCall(LibFunc_strlcat, Size->getType(),
  1344. {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()},
  1345. {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI);
  1346. }
  1347. Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
  1348. const TargetLibraryInfo *TLI) {
  1349. return emitLibCall(LibFunc_strncat, B.getInt8PtrTy(),
  1350. {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()},
  1351. {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI);
  1352. }
  1353. Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
  1354. IRBuilderBase &B, const TargetLibraryInfo *TLI) {
  1355. return emitLibCall(
  1356. LibFunc_vsnprintf, B.getInt32Ty(),
  1357. {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy(), VAList->getType()},
  1358. {castToCStr(Dest, B), Size, castToCStr(Fmt, B), VAList}, B, TLI);
  1359. }
  1360. Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList,
  1361. IRBuilderBase &B, const TargetLibraryInfo *TLI) {
  1362. return emitLibCall(LibFunc_vsprintf, B.getInt32Ty(),
  1363. {B.getInt8PtrTy(), B.getInt8PtrTy(), VAList->getType()},
  1364. {castToCStr(Dest, B), castToCStr(Fmt, B), VAList}, B, TLI);
  1365. }
  1366. /// Append a suffix to the function name according to the type of 'Op'.
  1367. static void appendTypeSuffix(Value *Op, StringRef &Name,
  1368. SmallString<20> &NameBuffer) {
  1369. if (!Op->getType()->isDoubleTy()) {
  1370. NameBuffer += Name;
  1371. if (Op->getType()->isFloatTy())
  1372. NameBuffer += 'f';
  1373. else
  1374. NameBuffer += 'l';
  1375. Name = NameBuffer;
  1376. }
  1377. }
  1378. static Value *emitUnaryFloatFnCallHelper(Value *Op, StringRef Name,
  1379. IRBuilderBase &B,
  1380. const AttributeList &Attrs) {
  1381. assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall");
  1382. Module *M = B.GetInsertBlock()->getModule();
  1383. FunctionCallee Callee =
  1384. M->getOrInsertFunction(Name, Op->getType(), Op->getType());
  1385. CallInst *CI = B.CreateCall(Callee, Op, Name);
  1386. // The incoming attribute set may have come from a speculatable intrinsic, but
  1387. // is being replaced with a library call which is not allowed to be
  1388. // speculatable.
  1389. CI->setAttributes(
  1390. Attrs.removeFnAttribute(B.getContext(), Attribute::Speculatable));
  1391. if (const Function *F =
  1392. dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
  1393. CI->setCallingConv(F->getCallingConv());
  1394. return CI;
  1395. }
  1396. Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilderBase &B,
  1397. const AttributeList &Attrs) {
  1398. SmallString<20> NameBuffer;
  1399. appendTypeSuffix(Op, Name, NameBuffer);
  1400. return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs);
  1401. }
  1402. Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
  1403. LibFunc DoubleFn, LibFunc FloatFn,
  1404. LibFunc LongDoubleFn, IRBuilderBase &B,
  1405. const AttributeList &Attrs) {
  1406. // Get the name of the function according to TLI.
  1407. StringRef Name = getFloatFnName(TLI, Op->getType(),
  1408. DoubleFn, FloatFn, LongDoubleFn);
  1409. return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs);
  1410. }
  1411. static Value *emitBinaryFloatFnCallHelper(Value *Op1, Value *Op2,
  1412. StringRef Name, IRBuilderBase &B,
  1413. const AttributeList &Attrs,
  1414. const TargetLibraryInfo *TLI = nullptr) {
  1415. assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
  1416. Module *M = B.GetInsertBlock()->getModule();
  1417. FunctionCallee Callee = M->getOrInsertFunction(Name, Op1->getType(),
  1418. Op1->getType(), Op2->getType());
  1419. if (TLI != nullptr)
  1420. inferLibFuncAttributes(M, Name, *TLI);
  1421. CallInst *CI = B.CreateCall(Callee, { Op1, Op2 }, Name);
  1422. // The incoming attribute set may have come from a speculatable intrinsic, but
  1423. // is being replaced with a library call which is not allowed to be
  1424. // speculatable.
  1425. CI->setAttributes(
  1426. Attrs.removeFnAttribute(B.getContext(), Attribute::Speculatable));
  1427. if (const Function *F =
  1428. dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
  1429. CI->setCallingConv(F->getCallingConv());
  1430. return CI;
  1431. }
  1432. Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
  1433. IRBuilderBase &B,
  1434. const AttributeList &Attrs) {
  1435. assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
  1436. SmallString<20> NameBuffer;
  1437. appendTypeSuffix(Op1, Name, NameBuffer);
  1438. return emitBinaryFloatFnCallHelper(Op1, Op2, Name, B, Attrs);
  1439. }
  1440. Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2,
  1441. const TargetLibraryInfo *TLI,
  1442. LibFunc DoubleFn, LibFunc FloatFn,
  1443. LibFunc LongDoubleFn, IRBuilderBase &B,
  1444. const AttributeList &Attrs) {
  1445. // Get the name of the function according to TLI.
  1446. StringRef Name = getFloatFnName(TLI, Op1->getType(),
  1447. DoubleFn, FloatFn, LongDoubleFn);
  1448. return emitBinaryFloatFnCallHelper(Op1, Op2, Name, B, Attrs, TLI);
  1449. }
  1450. Value *llvm::emitPutChar(Value *Char, IRBuilderBase &B,
  1451. const TargetLibraryInfo *TLI) {
  1452. if (!TLI->has(LibFunc_putchar))
  1453. return nullptr;
  1454. Module *M = B.GetInsertBlock()->getModule();
  1455. StringRef PutCharName = TLI->getName(LibFunc_putchar);
  1456. FunctionCallee PutChar =
  1457. M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
  1458. inferLibFuncAttributes(M, PutCharName, *TLI);
  1459. CallInst *CI = B.CreateCall(PutChar,
  1460. B.CreateIntCast(Char,
  1461. B.getInt32Ty(),
  1462. /*isSigned*/true,
  1463. "chari"),
  1464. PutCharName);
  1465. if (const Function *F =
  1466. dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts()))
  1467. CI->setCallingConv(F->getCallingConv());
  1468. return CI;
  1469. }
  1470. Value *llvm::emitPutS(Value *Str, IRBuilderBase &B,
  1471. const TargetLibraryInfo *TLI) {
  1472. if (!TLI->has(LibFunc_puts))
  1473. return nullptr;
  1474. Module *M = B.GetInsertBlock()->getModule();
  1475. StringRef PutsName = TLI->getName(LibFunc_puts);
  1476. FunctionCallee PutS =
  1477. M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy());
  1478. inferLibFuncAttributes(M, PutsName, *TLI);
  1479. CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName);
  1480. if (const Function *F =
  1481. dyn_cast<Function>(PutS.getCallee()->stripPointerCasts()))
  1482. CI->setCallingConv(F->getCallingConv());
  1483. return CI;
  1484. }
  1485. Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilderBase &B,
  1486. const TargetLibraryInfo *TLI) {
  1487. if (!TLI->has(LibFunc_fputc))
  1488. return nullptr;
  1489. Module *M = B.GetInsertBlock()->getModule();
  1490. StringRef FPutcName = TLI->getName(LibFunc_fputc);
  1491. FunctionCallee F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(),
  1492. B.getInt32Ty(), File->getType());
  1493. if (File->getType()->isPointerTy())
  1494. inferLibFuncAttributes(M, FPutcName, *TLI);
  1495. Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
  1496. "chari");
  1497. CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
  1498. if (const Function *Fn =
  1499. dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
  1500. CI->setCallingConv(Fn->getCallingConv());
  1501. return CI;
  1502. }
  1503. Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilderBase &B,
  1504. const TargetLibraryInfo *TLI) {
  1505. if (!TLI->has(LibFunc_fputs))
  1506. return nullptr;
  1507. Module *M = B.GetInsertBlock()->getModule();
  1508. StringRef FPutsName = TLI->getName(LibFunc_fputs);
  1509. FunctionCallee F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
  1510. B.getInt8PtrTy(), File->getType());
  1511. if (File->getType()->isPointerTy())
  1512. inferLibFuncAttributes(M, FPutsName, *TLI);
  1513. CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName);
  1514. if (const Function *Fn =
  1515. dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
  1516. CI->setCallingConv(Fn->getCallingConv());
  1517. return CI;
  1518. }
  1519. Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B,
  1520. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  1521. if (!TLI->has(LibFunc_fwrite))
  1522. return nullptr;
  1523. Module *M = B.GetInsertBlock()->getModule();
  1524. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1525. StringRef FWriteName = TLI->getName(LibFunc_fwrite);
  1526. FunctionCallee F = M->getOrInsertFunction(
  1527. FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
  1528. DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
  1529. if (File->getType()->isPointerTy())
  1530. inferLibFuncAttributes(M, FWriteName, *TLI);
  1531. CallInst *CI =
  1532. B.CreateCall(F, {castToCStr(Ptr, B), Size,
  1533. ConstantInt::get(DL.getIntPtrType(Context), 1), File});
  1534. if (const Function *Fn =
  1535. dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
  1536. CI->setCallingConv(Fn->getCallingConv());
  1537. return CI;
  1538. }
  1539. Value *llvm::emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL,
  1540. const TargetLibraryInfo *TLI) {
  1541. if (!TLI->has(LibFunc_malloc))
  1542. return nullptr;
  1543. Module *M = B.GetInsertBlock()->getModule();
  1544. StringRef MallocName = TLI->getName(LibFunc_malloc);
  1545. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1546. FunctionCallee Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(),
  1547. DL.getIntPtrType(Context));
  1548. inferLibFuncAttributes(M, MallocName, *TLI);
  1549. CallInst *CI = B.CreateCall(Malloc, Num, MallocName);
  1550. if (const Function *F =
  1551. dyn_cast<Function>(Malloc.getCallee()->stripPointerCasts()))
  1552. CI->setCallingConv(F->getCallingConv());
  1553. return CI;
  1554. }
  1555. Value *llvm::emitCalloc(Value *Num, Value *Size, IRBuilderBase &B,
  1556. const TargetLibraryInfo &TLI) {
  1557. if (!TLI.has(LibFunc_calloc))
  1558. return nullptr;
  1559. Module *M = B.GetInsertBlock()->getModule();
  1560. StringRef CallocName = TLI.getName(LibFunc_calloc);
  1561. const DataLayout &DL = M->getDataLayout();
  1562. IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext()));
  1563. FunctionCallee Calloc =
  1564. M->getOrInsertFunction(CallocName, B.getInt8PtrTy(), PtrType, PtrType);
  1565. inferLibFuncAttributes(M, CallocName, TLI);
  1566. CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
  1567. if (const auto *F =
  1568. dyn_cast<Function>(Calloc.getCallee()->stripPointerCasts()))
  1569. CI->setCallingConv(F->getCallingConv());
  1570. return CI;
  1571. }