BuildLibCalls.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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. using namespace llvm;
  25. #define DEBUG_TYPE "build-libcalls"
  26. //- Infer Attributes ---------------------------------------------------------//
  27. STATISTIC(NumReadNone, "Number of functions inferred as readnone");
  28. STATISTIC(NumReadOnly, "Number of functions inferred as readonly");
  29. STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly");
  30. STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind");
  31. STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture");
  32. STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly");
  33. STATISTIC(NumNoAlias, "Number of function returns inferred as noalias");
  34. STATISTIC(NumNonNull, "Number of function returns inferred as nonnull returns");
  35. STATISTIC(NumReturnedArg, "Number of arguments inferred as returned");
  36. static bool setDoesNotAccessMemory(Function &F) {
  37. if (F.doesNotAccessMemory())
  38. return false;
  39. F.setDoesNotAccessMemory();
  40. ++NumReadNone;
  41. return true;
  42. }
  43. static bool setOnlyReadsMemory(Function &F) {
  44. if (F.onlyReadsMemory())
  45. return false;
  46. F.setOnlyReadsMemory();
  47. ++NumReadOnly;
  48. return true;
  49. }
  50. static bool setOnlyAccessesArgMemory(Function &F) {
  51. if (F.onlyAccessesArgMemory())
  52. return false;
  53. F.setOnlyAccessesArgMemory();
  54. ++NumArgMemOnly;
  55. return true;
  56. }
  57. static bool setDoesNotThrow(Function &F) {
  58. if (F.doesNotThrow())
  59. return false;
  60. F.setDoesNotThrow();
  61. ++NumNoUnwind;
  62. return true;
  63. }
  64. static bool setRetDoesNotAlias(Function &F) {
  65. if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias))
  66. return false;
  67. F.addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
  68. ++NumNoAlias;
  69. return true;
  70. }
  71. static bool setDoesNotCapture(Function &F, unsigned ArgNo) {
  72. if (F.hasParamAttribute(ArgNo, Attribute::NoCapture))
  73. return false;
  74. F.addParamAttr(ArgNo, Attribute::NoCapture);
  75. ++NumNoCapture;
  76. return true;
  77. }
  78. static bool setOnlyReadsMemory(Function &F, unsigned ArgNo) {
  79. if (F.hasParamAttribute(ArgNo, Attribute::ReadOnly))
  80. return false;
  81. F.addParamAttr(ArgNo, Attribute::ReadOnly);
  82. ++NumReadOnlyArg;
  83. return true;
  84. }
  85. static bool setRetNonNull(Function &F) {
  86. assert(F.getReturnType()->isPointerTy() &&
  87. "nonnull applies only to pointers");
  88. if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NonNull))
  89. return false;
  90. F.addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
  91. ++NumNonNull;
  92. return true;
  93. }
  94. static bool setReturnedArg(Function &F, unsigned ArgNo) {
  95. if (F.hasParamAttribute(ArgNo, Attribute::Returned))
  96. return false;
  97. F.addParamAttr(ArgNo, Attribute::Returned);
  98. ++NumReturnedArg;
  99. return true;
  100. }
  101. static bool setNonLazyBind(Function &F) {
  102. if (F.hasFnAttribute(Attribute::NonLazyBind))
  103. return false;
  104. F.addFnAttr(Attribute::NonLazyBind);
  105. return true;
  106. }
  107. bool llvm::inferLibFuncAttributes(Module *M, StringRef Name,
  108. const TargetLibraryInfo &TLI) {
  109. Function *F = M->getFunction(Name);
  110. if (!F)
  111. return false;
  112. return inferLibFuncAttributes(*F, TLI);
  113. }
  114. bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
  115. LibFunc TheLibFunc;
  116. if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc)))
  117. return false;
  118. bool Changed = false;
  119. if (F.getParent() != nullptr && F.getParent()->getRtLibUseGOT())
  120. Changed |= setNonLazyBind(F);
  121. switch (TheLibFunc) {
  122. case LibFunc_strlen:
  123. case LibFunc_wcslen:
  124. Changed |= setOnlyReadsMemory(F);
  125. Changed |= setDoesNotThrow(F);
  126. Changed |= setOnlyAccessesArgMemory(F);
  127. Changed |= setDoesNotCapture(F, 0);
  128. return Changed;
  129. case LibFunc_strchr:
  130. case LibFunc_strrchr:
  131. Changed |= setOnlyReadsMemory(F);
  132. Changed |= setDoesNotThrow(F);
  133. return Changed;
  134. case LibFunc_strtol:
  135. case LibFunc_strtod:
  136. case LibFunc_strtof:
  137. case LibFunc_strtoul:
  138. case LibFunc_strtoll:
  139. case LibFunc_strtold:
  140. case LibFunc_strtoull:
  141. Changed |= setDoesNotThrow(F);
  142. Changed |= setDoesNotCapture(F, 1);
  143. Changed |= setOnlyReadsMemory(F, 0);
  144. return Changed;
  145. case LibFunc_strcpy:
  146. case LibFunc_strncpy:
  147. case LibFunc_strcat:
  148. case LibFunc_strncat:
  149. Changed |= setReturnedArg(F, 0);
  150. LLVM_FALLTHROUGH;
  151. case LibFunc_stpcpy:
  152. case LibFunc_stpncpy:
  153. Changed |= setDoesNotThrow(F);
  154. Changed |= setDoesNotCapture(F, 1);
  155. Changed |= setOnlyReadsMemory(F, 1);
  156. return Changed;
  157. case LibFunc_strxfrm:
  158. Changed |= setDoesNotThrow(F);
  159. Changed |= setDoesNotCapture(F, 0);
  160. Changed |= setDoesNotCapture(F, 1);
  161. Changed |= setOnlyReadsMemory(F, 1);
  162. return Changed;
  163. case LibFunc_strcmp: // 0,1
  164. case LibFunc_strspn: // 0,1
  165. case LibFunc_strncmp: // 0,1
  166. case LibFunc_strcspn: // 0,1
  167. case LibFunc_strcoll: // 0,1
  168. case LibFunc_strcasecmp: // 0,1
  169. case LibFunc_strncasecmp: //
  170. Changed |= setOnlyReadsMemory(F);
  171. Changed |= setDoesNotThrow(F);
  172. Changed |= setDoesNotCapture(F, 0);
  173. Changed |= setDoesNotCapture(F, 1);
  174. return Changed;
  175. case LibFunc_strstr:
  176. case LibFunc_strpbrk:
  177. Changed |= setOnlyReadsMemory(F);
  178. Changed |= setDoesNotThrow(F);
  179. Changed |= setDoesNotCapture(F, 1);
  180. return Changed;
  181. case LibFunc_strtok:
  182. case LibFunc_strtok_r:
  183. Changed |= setDoesNotThrow(F);
  184. Changed |= setDoesNotCapture(F, 1);
  185. Changed |= setOnlyReadsMemory(F, 1);
  186. return Changed;
  187. case LibFunc_scanf:
  188. Changed |= setDoesNotThrow(F);
  189. Changed |= setDoesNotCapture(F, 0);
  190. Changed |= setOnlyReadsMemory(F, 0);
  191. return Changed;
  192. case LibFunc_setbuf:
  193. case LibFunc_setvbuf:
  194. Changed |= setDoesNotThrow(F);
  195. Changed |= setDoesNotCapture(F, 0);
  196. return Changed;
  197. case LibFunc_strdup:
  198. case LibFunc_strndup:
  199. Changed |= setDoesNotThrow(F);
  200. Changed |= setRetDoesNotAlias(F);
  201. Changed |= setDoesNotCapture(F, 0);
  202. Changed |= setOnlyReadsMemory(F, 0);
  203. return Changed;
  204. case LibFunc_stat:
  205. case LibFunc_statvfs:
  206. Changed |= setDoesNotThrow(F);
  207. Changed |= setDoesNotCapture(F, 0);
  208. Changed |= setDoesNotCapture(F, 1);
  209. Changed |= setOnlyReadsMemory(F, 0);
  210. return Changed;
  211. case LibFunc_sscanf:
  212. Changed |= setDoesNotThrow(F);
  213. Changed |= setDoesNotCapture(F, 0);
  214. Changed |= setDoesNotCapture(F, 1);
  215. Changed |= setOnlyReadsMemory(F, 0);
  216. Changed |= setOnlyReadsMemory(F, 1);
  217. return Changed;
  218. case LibFunc_sprintf:
  219. Changed |= setDoesNotThrow(F);
  220. Changed |= setDoesNotCapture(F, 0);
  221. Changed |= setDoesNotCapture(F, 1);
  222. Changed |= setOnlyReadsMemory(F, 1);
  223. return Changed;
  224. case LibFunc_snprintf:
  225. Changed |= setDoesNotThrow(F);
  226. Changed |= setDoesNotCapture(F, 0);
  227. Changed |= setDoesNotCapture(F, 2);
  228. Changed |= setOnlyReadsMemory(F, 2);
  229. return Changed;
  230. case LibFunc_setitimer:
  231. Changed |= setDoesNotThrow(F);
  232. Changed |= setDoesNotCapture(F, 1);
  233. Changed |= setDoesNotCapture(F, 2);
  234. Changed |= setOnlyReadsMemory(F, 1);
  235. return Changed;
  236. case LibFunc_system:
  237. // May throw; "system" is a valid pthread cancellation point.
  238. Changed |= setDoesNotCapture(F, 0);
  239. Changed |= setOnlyReadsMemory(F, 0);
  240. return Changed;
  241. case LibFunc_malloc:
  242. Changed |= setDoesNotThrow(F);
  243. Changed |= setRetDoesNotAlias(F);
  244. return Changed;
  245. case LibFunc_memcmp:
  246. Changed |= setOnlyReadsMemory(F);
  247. Changed |= setDoesNotThrow(F);
  248. Changed |= setDoesNotCapture(F, 0);
  249. Changed |= setDoesNotCapture(F, 1);
  250. return Changed;
  251. case LibFunc_memchr:
  252. case LibFunc_memrchr:
  253. Changed |= setOnlyReadsMemory(F);
  254. Changed |= setDoesNotThrow(F);
  255. return Changed;
  256. case LibFunc_modf:
  257. case LibFunc_modff:
  258. case LibFunc_modfl:
  259. Changed |= setDoesNotThrow(F);
  260. Changed |= setDoesNotCapture(F, 1);
  261. return Changed;
  262. case LibFunc_memcpy:
  263. case LibFunc_memmove:
  264. Changed |= setReturnedArg(F, 0);
  265. LLVM_FALLTHROUGH;
  266. case LibFunc_mempcpy:
  267. case LibFunc_memccpy:
  268. Changed |= setDoesNotThrow(F);
  269. Changed |= setDoesNotCapture(F, 1);
  270. Changed |= setOnlyReadsMemory(F, 1);
  271. return Changed;
  272. case LibFunc_memcpy_chk:
  273. Changed |= setDoesNotThrow(F);
  274. return Changed;
  275. case LibFunc_memalign:
  276. Changed |= setRetDoesNotAlias(F);
  277. return Changed;
  278. case LibFunc_mkdir:
  279. Changed |= setDoesNotThrow(F);
  280. Changed |= setDoesNotCapture(F, 0);
  281. Changed |= setOnlyReadsMemory(F, 0);
  282. return Changed;
  283. case LibFunc_mktime:
  284. Changed |= setDoesNotThrow(F);
  285. Changed |= setDoesNotCapture(F, 0);
  286. return Changed;
  287. case LibFunc_realloc:
  288. Changed |= setDoesNotThrow(F);
  289. Changed |= setRetDoesNotAlias(F);
  290. Changed |= setDoesNotCapture(F, 0);
  291. return Changed;
  292. case LibFunc_read:
  293. // May throw; "read" is a valid pthread cancellation point.
  294. Changed |= setDoesNotCapture(F, 1);
  295. return Changed;
  296. case LibFunc_rewind:
  297. Changed |= setDoesNotThrow(F);
  298. Changed |= setDoesNotCapture(F, 0);
  299. return Changed;
  300. case LibFunc_rmdir:
  301. case LibFunc_remove:
  302. case LibFunc_realpath:
  303. Changed |= setDoesNotThrow(F);
  304. Changed |= setDoesNotCapture(F, 0);
  305. Changed |= setOnlyReadsMemory(F, 0);
  306. return Changed;
  307. case LibFunc_rename:
  308. Changed |= setDoesNotThrow(F);
  309. Changed |= setDoesNotCapture(F, 0);
  310. Changed |= setDoesNotCapture(F, 1);
  311. Changed |= setOnlyReadsMemory(F, 0);
  312. Changed |= setOnlyReadsMemory(F, 1);
  313. return Changed;
  314. case LibFunc_readlink:
  315. Changed |= setDoesNotThrow(F);
  316. Changed |= setDoesNotCapture(F, 0);
  317. Changed |= setDoesNotCapture(F, 1);
  318. Changed |= setOnlyReadsMemory(F, 0);
  319. return Changed;
  320. case LibFunc_write:
  321. // May throw; "write" is a valid pthread cancellation point.
  322. Changed |= setDoesNotCapture(F, 1);
  323. Changed |= setOnlyReadsMemory(F, 1);
  324. return Changed;
  325. case LibFunc_bcopy:
  326. Changed |= setDoesNotThrow(F);
  327. Changed |= setDoesNotCapture(F, 0);
  328. Changed |= setDoesNotCapture(F, 1);
  329. Changed |= setOnlyReadsMemory(F, 0);
  330. return Changed;
  331. case LibFunc_bcmp:
  332. Changed |= setDoesNotThrow(F);
  333. Changed |= setOnlyReadsMemory(F);
  334. Changed |= setDoesNotCapture(F, 0);
  335. Changed |= setDoesNotCapture(F, 1);
  336. return Changed;
  337. case LibFunc_bzero:
  338. Changed |= setDoesNotThrow(F);
  339. Changed |= setDoesNotCapture(F, 0);
  340. return Changed;
  341. case LibFunc_calloc:
  342. Changed |= setDoesNotThrow(F);
  343. Changed |= setRetDoesNotAlias(F);
  344. return Changed;
  345. case LibFunc_chmod:
  346. case LibFunc_chown:
  347. Changed |= setDoesNotThrow(F);
  348. Changed |= setDoesNotCapture(F, 0);
  349. Changed |= setOnlyReadsMemory(F, 0);
  350. return Changed;
  351. case LibFunc_ctermid:
  352. case LibFunc_clearerr:
  353. case LibFunc_closedir:
  354. Changed |= setDoesNotThrow(F);
  355. Changed |= setDoesNotCapture(F, 0);
  356. return Changed;
  357. case LibFunc_atoi:
  358. case LibFunc_atol:
  359. case LibFunc_atof:
  360. case LibFunc_atoll:
  361. Changed |= setDoesNotThrow(F);
  362. Changed |= setOnlyReadsMemory(F);
  363. Changed |= setDoesNotCapture(F, 0);
  364. return Changed;
  365. case LibFunc_access:
  366. Changed |= setDoesNotThrow(F);
  367. Changed |= setDoesNotCapture(F, 0);
  368. Changed |= setOnlyReadsMemory(F, 0);
  369. return Changed;
  370. case LibFunc_fopen:
  371. Changed |= setDoesNotThrow(F);
  372. Changed |= setRetDoesNotAlias(F);
  373. Changed |= setDoesNotCapture(F, 0);
  374. Changed |= setDoesNotCapture(F, 1);
  375. Changed |= setOnlyReadsMemory(F, 0);
  376. Changed |= setOnlyReadsMemory(F, 1);
  377. return Changed;
  378. case LibFunc_fdopen:
  379. Changed |= setDoesNotThrow(F);
  380. Changed |= setRetDoesNotAlias(F);
  381. Changed |= setDoesNotCapture(F, 1);
  382. Changed |= setOnlyReadsMemory(F, 1);
  383. return Changed;
  384. case LibFunc_feof:
  385. case LibFunc_free:
  386. case LibFunc_fseek:
  387. case LibFunc_ftell:
  388. case LibFunc_fgetc:
  389. case LibFunc_fgetc_unlocked:
  390. case LibFunc_fseeko:
  391. case LibFunc_ftello:
  392. case LibFunc_fileno:
  393. case LibFunc_fflush:
  394. case LibFunc_fclose:
  395. case LibFunc_fsetpos:
  396. case LibFunc_flockfile:
  397. case LibFunc_funlockfile:
  398. case LibFunc_ftrylockfile:
  399. Changed |= setDoesNotThrow(F);
  400. Changed |= setDoesNotCapture(F, 0);
  401. return Changed;
  402. case LibFunc_ferror:
  403. Changed |= setDoesNotThrow(F);
  404. Changed |= setDoesNotCapture(F, 0);
  405. Changed |= setOnlyReadsMemory(F);
  406. return Changed;
  407. case LibFunc_fputc:
  408. case LibFunc_fputc_unlocked:
  409. case LibFunc_fstat:
  410. case LibFunc_frexp:
  411. case LibFunc_frexpf:
  412. case LibFunc_frexpl:
  413. case LibFunc_fstatvfs:
  414. Changed |= setDoesNotThrow(F);
  415. Changed |= setDoesNotCapture(F, 1);
  416. return Changed;
  417. case LibFunc_fgets:
  418. case LibFunc_fgets_unlocked:
  419. Changed |= setDoesNotThrow(F);
  420. Changed |= setDoesNotCapture(F, 2);
  421. return Changed;
  422. case LibFunc_fread:
  423. case LibFunc_fread_unlocked:
  424. Changed |= setDoesNotThrow(F);
  425. Changed |= setDoesNotCapture(F, 0);
  426. Changed |= setDoesNotCapture(F, 3);
  427. return Changed;
  428. case LibFunc_fwrite:
  429. case LibFunc_fwrite_unlocked:
  430. Changed |= setDoesNotThrow(F);
  431. Changed |= setDoesNotCapture(F, 0);
  432. Changed |= setDoesNotCapture(F, 3);
  433. // FIXME: readonly #1?
  434. return Changed;
  435. case LibFunc_fputs:
  436. case LibFunc_fputs_unlocked:
  437. Changed |= setDoesNotThrow(F);
  438. Changed |= setDoesNotCapture(F, 0);
  439. Changed |= setDoesNotCapture(F, 1);
  440. Changed |= setOnlyReadsMemory(F, 0);
  441. return Changed;
  442. case LibFunc_fscanf:
  443. case LibFunc_fprintf:
  444. Changed |= setDoesNotThrow(F);
  445. Changed |= setDoesNotCapture(F, 0);
  446. Changed |= setDoesNotCapture(F, 1);
  447. Changed |= setOnlyReadsMemory(F, 1);
  448. return Changed;
  449. case LibFunc_fgetpos:
  450. Changed |= setDoesNotThrow(F);
  451. Changed |= setDoesNotCapture(F, 0);
  452. Changed |= setDoesNotCapture(F, 1);
  453. return Changed;
  454. case LibFunc_getc:
  455. case LibFunc_getlogin_r:
  456. case LibFunc_getc_unlocked:
  457. Changed |= setDoesNotThrow(F);
  458. Changed |= setDoesNotCapture(F, 0);
  459. return Changed;
  460. case LibFunc_getenv:
  461. Changed |= setDoesNotThrow(F);
  462. Changed |= setOnlyReadsMemory(F);
  463. Changed |= setDoesNotCapture(F, 0);
  464. return Changed;
  465. case LibFunc_gets:
  466. case LibFunc_getchar:
  467. case LibFunc_getchar_unlocked:
  468. Changed |= setDoesNotThrow(F);
  469. return Changed;
  470. case LibFunc_getitimer:
  471. Changed |= setDoesNotThrow(F);
  472. Changed |= setDoesNotCapture(F, 1);
  473. return Changed;
  474. case LibFunc_getpwnam:
  475. Changed |= setDoesNotThrow(F);
  476. Changed |= setDoesNotCapture(F, 0);
  477. Changed |= setOnlyReadsMemory(F, 0);
  478. return Changed;
  479. case LibFunc_ungetc:
  480. Changed |= setDoesNotThrow(F);
  481. Changed |= setDoesNotCapture(F, 1);
  482. return Changed;
  483. case LibFunc_uname:
  484. Changed |= setDoesNotThrow(F);
  485. Changed |= setDoesNotCapture(F, 0);
  486. return Changed;
  487. case LibFunc_unlink:
  488. Changed |= setDoesNotThrow(F);
  489. Changed |= setDoesNotCapture(F, 0);
  490. Changed |= setOnlyReadsMemory(F, 0);
  491. return Changed;
  492. case LibFunc_unsetenv:
  493. Changed |= setDoesNotThrow(F);
  494. Changed |= setDoesNotCapture(F, 0);
  495. Changed |= setOnlyReadsMemory(F, 0);
  496. return Changed;
  497. case LibFunc_utime:
  498. case LibFunc_utimes:
  499. Changed |= setDoesNotThrow(F);
  500. Changed |= setDoesNotCapture(F, 0);
  501. Changed |= setDoesNotCapture(F, 1);
  502. Changed |= setOnlyReadsMemory(F, 0);
  503. Changed |= setOnlyReadsMemory(F, 1);
  504. return Changed;
  505. case LibFunc_putc:
  506. case LibFunc_putc_unlocked:
  507. Changed |= setDoesNotThrow(F);
  508. Changed |= setDoesNotCapture(F, 1);
  509. return Changed;
  510. case LibFunc_puts:
  511. case LibFunc_printf:
  512. case LibFunc_perror:
  513. Changed |= setDoesNotThrow(F);
  514. Changed |= setDoesNotCapture(F, 0);
  515. Changed |= setOnlyReadsMemory(F, 0);
  516. return Changed;
  517. case LibFunc_pread:
  518. // May throw; "pread" is a valid pthread cancellation point.
  519. Changed |= setDoesNotCapture(F, 1);
  520. return Changed;
  521. case LibFunc_pwrite:
  522. // May throw; "pwrite" is a valid pthread cancellation point.
  523. Changed |= setDoesNotCapture(F, 1);
  524. Changed |= setOnlyReadsMemory(F, 1);
  525. return Changed;
  526. case LibFunc_putchar:
  527. case LibFunc_putchar_unlocked:
  528. Changed |= setDoesNotThrow(F);
  529. return Changed;
  530. case LibFunc_popen:
  531. Changed |= setDoesNotThrow(F);
  532. Changed |= setRetDoesNotAlias(F);
  533. Changed |= setDoesNotCapture(F, 0);
  534. Changed |= setDoesNotCapture(F, 1);
  535. Changed |= setOnlyReadsMemory(F, 0);
  536. Changed |= setOnlyReadsMemory(F, 1);
  537. return Changed;
  538. case LibFunc_pclose:
  539. Changed |= setDoesNotThrow(F);
  540. Changed |= setDoesNotCapture(F, 0);
  541. return Changed;
  542. case LibFunc_vscanf:
  543. Changed |= setDoesNotThrow(F);
  544. Changed |= setDoesNotCapture(F, 0);
  545. Changed |= setOnlyReadsMemory(F, 0);
  546. return Changed;
  547. case LibFunc_vsscanf:
  548. Changed |= setDoesNotThrow(F);
  549. Changed |= setDoesNotCapture(F, 0);
  550. Changed |= setDoesNotCapture(F, 1);
  551. Changed |= setOnlyReadsMemory(F, 0);
  552. Changed |= setOnlyReadsMemory(F, 1);
  553. return Changed;
  554. case LibFunc_vfscanf:
  555. Changed |= setDoesNotThrow(F);
  556. Changed |= setDoesNotCapture(F, 0);
  557. Changed |= setDoesNotCapture(F, 1);
  558. Changed |= setOnlyReadsMemory(F, 1);
  559. return Changed;
  560. case LibFunc_valloc:
  561. Changed |= setDoesNotThrow(F);
  562. Changed |= setRetDoesNotAlias(F);
  563. return Changed;
  564. case LibFunc_vprintf:
  565. Changed |= setDoesNotThrow(F);
  566. Changed |= setDoesNotCapture(F, 0);
  567. Changed |= setOnlyReadsMemory(F, 0);
  568. return Changed;
  569. case LibFunc_vfprintf:
  570. case LibFunc_vsprintf:
  571. Changed |= setDoesNotThrow(F);
  572. Changed |= setDoesNotCapture(F, 0);
  573. Changed |= setDoesNotCapture(F, 1);
  574. Changed |= setOnlyReadsMemory(F, 1);
  575. return Changed;
  576. case LibFunc_vsnprintf:
  577. Changed |= setDoesNotThrow(F);
  578. Changed |= setDoesNotCapture(F, 0);
  579. Changed |= setDoesNotCapture(F, 2);
  580. Changed |= setOnlyReadsMemory(F, 2);
  581. return Changed;
  582. case LibFunc_open:
  583. // May throw; "open" is a valid pthread cancellation point.
  584. Changed |= setDoesNotCapture(F, 0);
  585. Changed |= setOnlyReadsMemory(F, 0);
  586. return Changed;
  587. case LibFunc_opendir:
  588. Changed |= setDoesNotThrow(F);
  589. Changed |= setRetDoesNotAlias(F);
  590. Changed |= setDoesNotCapture(F, 0);
  591. Changed |= setOnlyReadsMemory(F, 0);
  592. return Changed;
  593. case LibFunc_tmpfile:
  594. Changed |= setDoesNotThrow(F);
  595. Changed |= setRetDoesNotAlias(F);
  596. return Changed;
  597. case LibFunc_times:
  598. Changed |= setDoesNotThrow(F);
  599. Changed |= setDoesNotCapture(F, 0);
  600. return Changed;
  601. case LibFunc_htonl:
  602. case LibFunc_htons:
  603. case LibFunc_ntohl:
  604. case LibFunc_ntohs:
  605. Changed |= setDoesNotThrow(F);
  606. Changed |= setDoesNotAccessMemory(F);
  607. return Changed;
  608. case LibFunc_lstat:
  609. Changed |= setDoesNotThrow(F);
  610. Changed |= setDoesNotCapture(F, 0);
  611. Changed |= setDoesNotCapture(F, 1);
  612. Changed |= setOnlyReadsMemory(F, 0);
  613. return Changed;
  614. case LibFunc_lchown:
  615. Changed |= setDoesNotThrow(F);
  616. Changed |= setDoesNotCapture(F, 0);
  617. Changed |= setOnlyReadsMemory(F, 0);
  618. return Changed;
  619. case LibFunc_qsort:
  620. // May throw; places call through function pointer.
  621. Changed |= setDoesNotCapture(F, 3);
  622. return Changed;
  623. case LibFunc_dunder_strdup:
  624. case LibFunc_dunder_strndup:
  625. Changed |= setDoesNotThrow(F);
  626. Changed |= setRetDoesNotAlias(F);
  627. Changed |= setDoesNotCapture(F, 0);
  628. Changed |= setOnlyReadsMemory(F, 0);
  629. return Changed;
  630. case LibFunc_dunder_strtok_r:
  631. Changed |= setDoesNotThrow(F);
  632. Changed |= setDoesNotCapture(F, 1);
  633. Changed |= setOnlyReadsMemory(F, 1);
  634. return Changed;
  635. case LibFunc_under_IO_getc:
  636. Changed |= setDoesNotThrow(F);
  637. Changed |= setDoesNotCapture(F, 0);
  638. return Changed;
  639. case LibFunc_under_IO_putc:
  640. Changed |= setDoesNotThrow(F);
  641. Changed |= setDoesNotCapture(F, 1);
  642. return Changed;
  643. case LibFunc_dunder_isoc99_scanf:
  644. Changed |= setDoesNotThrow(F);
  645. Changed |= setDoesNotCapture(F, 0);
  646. Changed |= setOnlyReadsMemory(F, 0);
  647. return Changed;
  648. case LibFunc_stat64:
  649. case LibFunc_lstat64:
  650. case LibFunc_statvfs64:
  651. Changed |= setDoesNotThrow(F);
  652. Changed |= setDoesNotCapture(F, 0);
  653. Changed |= setDoesNotCapture(F, 1);
  654. Changed |= setOnlyReadsMemory(F, 0);
  655. return Changed;
  656. case LibFunc_dunder_isoc99_sscanf:
  657. Changed |= setDoesNotThrow(F);
  658. Changed |= setDoesNotCapture(F, 0);
  659. Changed |= setDoesNotCapture(F, 1);
  660. Changed |= setOnlyReadsMemory(F, 0);
  661. Changed |= setOnlyReadsMemory(F, 1);
  662. return Changed;
  663. case LibFunc_fopen64:
  664. Changed |= setDoesNotThrow(F);
  665. Changed |= setRetDoesNotAlias(F);
  666. Changed |= setDoesNotCapture(F, 0);
  667. Changed |= setDoesNotCapture(F, 1);
  668. Changed |= setOnlyReadsMemory(F, 0);
  669. Changed |= setOnlyReadsMemory(F, 1);
  670. return Changed;
  671. case LibFunc_fseeko64:
  672. case LibFunc_ftello64:
  673. Changed |= setDoesNotThrow(F);
  674. Changed |= setDoesNotCapture(F, 0);
  675. return Changed;
  676. case LibFunc_tmpfile64:
  677. Changed |= setDoesNotThrow(F);
  678. Changed |= setRetDoesNotAlias(F);
  679. return Changed;
  680. case LibFunc_fstat64:
  681. case LibFunc_fstatvfs64:
  682. Changed |= setDoesNotThrow(F);
  683. Changed |= setDoesNotCapture(F, 1);
  684. return Changed;
  685. case LibFunc_open64:
  686. // May throw; "open" is a valid pthread cancellation point.
  687. Changed |= setDoesNotCapture(F, 0);
  688. Changed |= setOnlyReadsMemory(F, 0);
  689. return Changed;
  690. case LibFunc_gettimeofday:
  691. // Currently some platforms have the restrict keyword on the arguments to
  692. // gettimeofday. To be conservative, do not add noalias to gettimeofday's
  693. // arguments.
  694. Changed |= setDoesNotThrow(F);
  695. Changed |= setDoesNotCapture(F, 0);
  696. Changed |= setDoesNotCapture(F, 1);
  697. return Changed;
  698. case LibFunc_Znwj: // new(unsigned int)
  699. case LibFunc_Znwm: // new(unsigned long)
  700. case LibFunc_Znaj: // new[](unsigned int)
  701. case LibFunc_Znam: // new[](unsigned long)
  702. case LibFunc_msvc_new_int: // new(unsigned int)
  703. case LibFunc_msvc_new_longlong: // new(unsigned long long)
  704. case LibFunc_msvc_new_array_int: // new[](unsigned int)
  705. case LibFunc_msvc_new_array_longlong: // new[](unsigned long long)
  706. // Operator new always returns a nonnull noalias pointer
  707. Changed |= setRetNonNull(F);
  708. Changed |= setRetDoesNotAlias(F);
  709. return Changed;
  710. // TODO: add LibFunc entries for:
  711. // case LibFunc_memset_pattern4:
  712. // case LibFunc_memset_pattern8:
  713. case LibFunc_memset_pattern16:
  714. Changed |= setOnlyAccessesArgMemory(F);
  715. Changed |= setDoesNotCapture(F, 0);
  716. Changed |= setDoesNotCapture(F, 1);
  717. Changed |= setOnlyReadsMemory(F, 1);
  718. return Changed;
  719. // int __nvvm_reflect(const char *)
  720. case LibFunc_nvvm_reflect:
  721. Changed |= setDoesNotAccessMemory(F);
  722. Changed |= setDoesNotThrow(F);
  723. return Changed;
  724. default:
  725. // FIXME: It'd be really nice to cover all the library functions we're
  726. // aware of here.
  727. return false;
  728. }
  729. }
  730. bool llvm::hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
  731. LibFunc DoubleFn, LibFunc FloatFn,
  732. LibFunc LongDoubleFn) {
  733. switch (Ty->getTypeID()) {
  734. case Type::HalfTyID:
  735. return false;
  736. case Type::FloatTyID:
  737. return TLI->has(FloatFn);
  738. case Type::DoubleTyID:
  739. return TLI->has(DoubleFn);
  740. default:
  741. return TLI->has(LongDoubleFn);
  742. }
  743. }
  744. StringRef llvm::getUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
  745. LibFunc DoubleFn, LibFunc FloatFn,
  746. LibFunc LongDoubleFn) {
  747. assert(hasUnaryFloatFn(TLI, Ty, DoubleFn, FloatFn, LongDoubleFn) &&
  748. "Cannot get name for unavailable function!");
  749. switch (Ty->getTypeID()) {
  750. case Type::HalfTyID:
  751. llvm_unreachable("No name for HalfTy!");
  752. case Type::FloatTyID:
  753. return TLI->getName(FloatFn);
  754. case Type::DoubleTyID:
  755. return TLI->getName(DoubleFn);
  756. default:
  757. return TLI->getName(LongDoubleFn);
  758. }
  759. }
  760. //- Emit LibCalls ------------------------------------------------------------//
  761. Value *llvm::castToCStr(Value *V, IRBuilder<> &B) {
  762. unsigned AS = V->getType()->getPointerAddressSpace();
  763. return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
  764. }
  765. Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
  766. const TargetLibraryInfo *TLI) {
  767. if (!TLI->has(LibFunc_strlen))
  768. return nullptr;
  769. Module *M = B.GetInsertBlock()->getModule();
  770. StringRef StrlenName = TLI->getName(LibFunc_strlen);
  771. LLVMContext &Context = B.GetInsertBlock()->getContext();
  772. FunctionCallee StrLen = M->getOrInsertFunction(
  773. StrlenName, DL.getIntPtrType(Context), B.getInt8PtrTy());
  774. inferLibFuncAttributes(M, StrlenName, *TLI);
  775. CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), StrlenName);
  776. if (const Function *F =
  777. dyn_cast<Function>(StrLen.getCallee()->stripPointerCasts()))
  778. CI->setCallingConv(F->getCallingConv());
  779. return CI;
  780. }
  781. Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
  782. const TargetLibraryInfo *TLI) {
  783. if (!TLI->has(LibFunc_strchr))
  784. return nullptr;
  785. Module *M = B.GetInsertBlock()->getModule();
  786. StringRef StrChrName = TLI->getName(LibFunc_strchr);
  787. Type *I8Ptr = B.getInt8PtrTy();
  788. Type *I32Ty = B.getInt32Ty();
  789. FunctionCallee StrChr =
  790. M->getOrInsertFunction(StrChrName, I8Ptr, I8Ptr, I32Ty);
  791. inferLibFuncAttributes(M, StrChrName, *TLI);
  792. CallInst *CI = B.CreateCall(
  793. StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, StrChrName);
  794. if (const Function *F =
  795. dyn_cast<Function>(StrChr.getCallee()->stripPointerCasts()))
  796. CI->setCallingConv(F->getCallingConv());
  797. return CI;
  798. }
  799. Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
  800. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  801. if (!TLI->has(LibFunc_strncmp))
  802. return nullptr;
  803. Module *M = B.GetInsertBlock()->getModule();
  804. StringRef StrNCmpName = TLI->getName(LibFunc_strncmp);
  805. LLVMContext &Context = B.GetInsertBlock()->getContext();
  806. FunctionCallee StrNCmp =
  807. M->getOrInsertFunction(StrNCmpName, B.getInt32Ty(), B.getInt8PtrTy(),
  808. B.getInt8PtrTy(), DL.getIntPtrType(Context));
  809. inferLibFuncAttributes(M, StrNCmpName, *TLI);
  810. CallInst *CI = B.CreateCall(
  811. StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, StrNCmpName);
  812. if (const Function *F =
  813. dyn_cast<Function>(StrNCmp.getCallee()->stripPointerCasts()))
  814. CI->setCallingConv(F->getCallingConv());
  815. return CI;
  816. }
  817. Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
  818. const TargetLibraryInfo *TLI, StringRef Name) {
  819. if (!TLI->has(LibFunc_strcpy))
  820. return nullptr;
  821. Module *M = B.GetInsertBlock()->getModule();
  822. Type *I8Ptr = B.getInt8PtrTy();
  823. FunctionCallee StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
  824. inferLibFuncAttributes(M, Name, *TLI);
  825. CallInst *CI =
  826. B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
  827. if (const Function *F =
  828. dyn_cast<Function>(StrCpy.getCallee()->stripPointerCasts()))
  829. CI->setCallingConv(F->getCallingConv());
  830. return CI;
  831. }
  832. Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
  833. const TargetLibraryInfo *TLI, StringRef Name) {
  834. if (!TLI->has(LibFunc_strncpy))
  835. return nullptr;
  836. Module *M = B.GetInsertBlock()->getModule();
  837. Type *I8Ptr = B.getInt8PtrTy();
  838. FunctionCallee StrNCpy =
  839. M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, Len->getType());
  840. inferLibFuncAttributes(M, Name, *TLI);
  841. CallInst *CI = B.CreateCall(
  842. StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, Name);
  843. if (const Function *F =
  844. dyn_cast<Function>(StrNCpy.getCallee()->stripPointerCasts()))
  845. CI->setCallingConv(F->getCallingConv());
  846. return CI;
  847. }
  848. Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
  849. IRBuilder<> &B, const DataLayout &DL,
  850. const TargetLibraryInfo *TLI) {
  851. if (!TLI->has(LibFunc_memcpy_chk))
  852. return nullptr;
  853. Module *M = B.GetInsertBlock()->getModule();
  854. AttributeList AS;
  855. AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex,
  856. Attribute::NoUnwind);
  857. LLVMContext &Context = B.GetInsertBlock()->getContext();
  858. FunctionCallee MemCpy = M->getOrInsertFunction(
  859. "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
  860. B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
  861. DL.getIntPtrType(Context));
  862. Dst = castToCStr(Dst, B);
  863. Src = castToCStr(Src, B);
  864. CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
  865. if (const Function *F =
  866. dyn_cast<Function>(MemCpy.getCallee()->stripPointerCasts()))
  867. CI->setCallingConv(F->getCallingConv());
  868. return CI;
  869. }
  870. Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
  871. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  872. if (!TLI->has(LibFunc_memchr))
  873. return nullptr;
  874. Module *M = B.GetInsertBlock()->getModule();
  875. StringRef MemChrName = TLI->getName(LibFunc_memchr);
  876. LLVMContext &Context = B.GetInsertBlock()->getContext();
  877. FunctionCallee MemChr =
  878. M->getOrInsertFunction(MemChrName, B.getInt8PtrTy(), B.getInt8PtrTy(),
  879. B.getInt32Ty(), DL.getIntPtrType(Context));
  880. inferLibFuncAttributes(M, MemChrName, *TLI);
  881. CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, MemChrName);
  882. if (const Function *F =
  883. dyn_cast<Function>(MemChr.getCallee()->stripPointerCasts()))
  884. CI->setCallingConv(F->getCallingConv());
  885. return CI;
  886. }
  887. Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
  888. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  889. if (!TLI->has(LibFunc_memcmp))
  890. return nullptr;
  891. Module *M = B.GetInsertBlock()->getModule();
  892. StringRef MemCmpName = TLI->getName(LibFunc_memcmp);
  893. LLVMContext &Context = B.GetInsertBlock()->getContext();
  894. FunctionCallee MemCmp =
  895. M->getOrInsertFunction(MemCmpName, B.getInt32Ty(), B.getInt8PtrTy(),
  896. B.getInt8PtrTy(), DL.getIntPtrType(Context));
  897. inferLibFuncAttributes(M, MemCmpName, *TLI);
  898. CallInst *CI = B.CreateCall(
  899. MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, MemCmpName);
  900. if (const Function *F =
  901. dyn_cast<Function>(MemCmp.getCallee()->stripPointerCasts()))
  902. CI->setCallingConv(F->getCallingConv());
  903. return CI;
  904. }
  905. /// Append a suffix to the function name according to the type of 'Op'.
  906. static void appendTypeSuffix(Value *Op, StringRef &Name,
  907. SmallString<20> &NameBuffer) {
  908. if (!Op->getType()->isDoubleTy()) {
  909. NameBuffer += Name;
  910. if (Op->getType()->isFloatTy())
  911. NameBuffer += 'f';
  912. else
  913. NameBuffer += 'l';
  914. Name = NameBuffer;
  915. }
  916. }
  917. static Value *emitUnaryFloatFnCallHelper(Value *Op, StringRef Name,
  918. IRBuilder<> &B,
  919. const AttributeList &Attrs) {
  920. assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall");
  921. Module *M = B.GetInsertBlock()->getModule();
  922. FunctionCallee Callee =
  923. M->getOrInsertFunction(Name, Op->getType(), Op->getType());
  924. CallInst *CI = B.CreateCall(Callee, Op, Name);
  925. // The incoming attribute set may have come from a speculatable intrinsic, but
  926. // is being replaced with a library call which is not allowed to be
  927. // speculatable.
  928. CI->setAttributes(Attrs.removeAttribute(B.getContext(),
  929. AttributeList::FunctionIndex,
  930. Attribute::Speculatable));
  931. if (const Function *F =
  932. dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
  933. CI->setCallingConv(F->getCallingConv());
  934. return CI;
  935. }
  936. Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
  937. const AttributeList &Attrs) {
  938. SmallString<20> NameBuffer;
  939. appendTypeSuffix(Op, Name, NameBuffer);
  940. return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs);
  941. }
  942. Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
  943. LibFunc DoubleFn, LibFunc FloatFn,
  944. LibFunc LongDoubleFn, IRBuilder<> &B,
  945. const AttributeList &Attrs) {
  946. // Get the name of the function according to TLI.
  947. StringRef Name = getUnaryFloatFn(TLI, Op->getType(),
  948. DoubleFn, FloatFn, LongDoubleFn);
  949. return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs);
  950. }
  951. Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
  952. IRBuilder<> &B, const AttributeList &Attrs) {
  953. assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
  954. SmallString<20> NameBuffer;
  955. appendTypeSuffix(Op1, Name, NameBuffer);
  956. Module *M = B.GetInsertBlock()->getModule();
  957. FunctionCallee Callee = M->getOrInsertFunction(
  958. Name, Op1->getType(), Op1->getType(), Op2->getType());
  959. CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
  960. CI->setAttributes(Attrs);
  961. if (const Function *F =
  962. dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
  963. CI->setCallingConv(F->getCallingConv());
  964. return CI;
  965. }
  966. Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B,
  967. const TargetLibraryInfo *TLI) {
  968. if (!TLI->has(LibFunc_putchar))
  969. return nullptr;
  970. Module *M = B.GetInsertBlock()->getModule();
  971. StringRef PutCharName = TLI->getName(LibFunc_putchar);
  972. FunctionCallee PutChar =
  973. M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
  974. inferLibFuncAttributes(M, PutCharName, *TLI);
  975. CallInst *CI = B.CreateCall(PutChar,
  976. B.CreateIntCast(Char,
  977. B.getInt32Ty(),
  978. /*isSigned*/true,
  979. "chari"),
  980. PutCharName);
  981. if (const Function *F =
  982. dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts()))
  983. CI->setCallingConv(F->getCallingConv());
  984. return CI;
  985. }
  986. Value *llvm::emitPutS(Value *Str, IRBuilder<> &B,
  987. const TargetLibraryInfo *TLI) {
  988. if (!TLI->has(LibFunc_puts))
  989. return nullptr;
  990. Module *M = B.GetInsertBlock()->getModule();
  991. StringRef PutsName = TLI->getName(LibFunc_puts);
  992. FunctionCallee PutS =
  993. M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy());
  994. inferLibFuncAttributes(M, PutsName, *TLI);
  995. CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName);
  996. if (const Function *F =
  997. dyn_cast<Function>(PutS.getCallee()->stripPointerCasts()))
  998. CI->setCallingConv(F->getCallingConv());
  999. return CI;
  1000. }
  1001. Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
  1002. const TargetLibraryInfo *TLI) {
  1003. if (!TLI->has(LibFunc_fputc))
  1004. return nullptr;
  1005. Module *M = B.GetInsertBlock()->getModule();
  1006. StringRef FPutcName = TLI->getName(LibFunc_fputc);
  1007. FunctionCallee F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(),
  1008. B.getInt32Ty(), File->getType());
  1009. if (File->getType()->isPointerTy())
  1010. inferLibFuncAttributes(M, FPutcName, *TLI);
  1011. Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
  1012. "chari");
  1013. CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
  1014. if (const Function *Fn =
  1015. dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
  1016. CI->setCallingConv(Fn->getCallingConv());
  1017. return CI;
  1018. }
  1019. Value *llvm::emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
  1020. const TargetLibraryInfo *TLI) {
  1021. if (!TLI->has(LibFunc_fputc_unlocked))
  1022. return nullptr;
  1023. Module *M = B.GetInsertBlock()->getModule();
  1024. StringRef FPutcUnlockedName = TLI->getName(LibFunc_fputc_unlocked);
  1025. FunctionCallee F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(),
  1026. B.getInt32Ty(), File->getType());
  1027. if (File->getType()->isPointerTy())
  1028. inferLibFuncAttributes(M, FPutcUnlockedName, *TLI);
  1029. Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari");
  1030. CallInst *CI = B.CreateCall(F, {Char, File}, FPutcUnlockedName);
  1031. if (const Function *Fn =
  1032. dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
  1033. CI->setCallingConv(Fn->getCallingConv());
  1034. return CI;
  1035. }
  1036. Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
  1037. const TargetLibraryInfo *TLI) {
  1038. if (!TLI->has(LibFunc_fputs))
  1039. return nullptr;
  1040. Module *M = B.GetInsertBlock()->getModule();
  1041. StringRef FPutsName = TLI->getName(LibFunc_fputs);
  1042. FunctionCallee F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
  1043. B.getInt8PtrTy(), File->getType());
  1044. if (File->getType()->isPointerTy())
  1045. inferLibFuncAttributes(M, FPutsName, *TLI);
  1046. CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName);
  1047. if (const Function *Fn =
  1048. dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
  1049. CI->setCallingConv(Fn->getCallingConv());
  1050. return CI;
  1051. }
  1052. Value *llvm::emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
  1053. const TargetLibraryInfo *TLI) {
  1054. if (!TLI->has(LibFunc_fputs_unlocked))
  1055. return nullptr;
  1056. Module *M = B.GetInsertBlock()->getModule();
  1057. StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked);
  1058. FunctionCallee F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(),
  1059. B.getInt8PtrTy(), File->getType());
  1060. if (File->getType()->isPointerTy())
  1061. inferLibFuncAttributes(M, FPutsUnlockedName, *TLI);
  1062. CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsUnlockedName);
  1063. if (const Function *Fn =
  1064. dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
  1065. CI->setCallingConv(Fn->getCallingConv());
  1066. return CI;
  1067. }
  1068. Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
  1069. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  1070. if (!TLI->has(LibFunc_fwrite))
  1071. return nullptr;
  1072. Module *M = B.GetInsertBlock()->getModule();
  1073. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1074. StringRef FWriteName = TLI->getName(LibFunc_fwrite);
  1075. FunctionCallee F = M->getOrInsertFunction(
  1076. FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
  1077. DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
  1078. if (File->getType()->isPointerTy())
  1079. inferLibFuncAttributes(M, FWriteName, *TLI);
  1080. CallInst *CI =
  1081. B.CreateCall(F, {castToCStr(Ptr, B), Size,
  1082. ConstantInt::get(DL.getIntPtrType(Context), 1), File});
  1083. if (const Function *Fn =
  1084. dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
  1085. CI->setCallingConv(Fn->getCallingConv());
  1086. return CI;
  1087. }
  1088. Value *llvm::emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
  1089. const TargetLibraryInfo *TLI) {
  1090. if (!TLI->has(LibFunc_malloc))
  1091. return nullptr;
  1092. Module *M = B.GetInsertBlock()->getModule();
  1093. StringRef MallocName = TLI->getName(LibFunc_malloc);
  1094. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1095. FunctionCallee Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(),
  1096. DL.getIntPtrType(Context));
  1097. inferLibFuncAttributes(M, MallocName, *TLI);
  1098. CallInst *CI = B.CreateCall(Malloc, Num, MallocName);
  1099. if (const Function *F =
  1100. dyn_cast<Function>(Malloc.getCallee()->stripPointerCasts()))
  1101. CI->setCallingConv(F->getCallingConv());
  1102. return CI;
  1103. }
  1104. Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
  1105. IRBuilder<> &B, const TargetLibraryInfo &TLI) {
  1106. if (!TLI.has(LibFunc_calloc))
  1107. return nullptr;
  1108. Module *M = B.GetInsertBlock()->getModule();
  1109. StringRef CallocName = TLI.getName(LibFunc_calloc);
  1110. const DataLayout &DL = M->getDataLayout();
  1111. IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext()));
  1112. FunctionCallee Calloc = M->getOrInsertFunction(
  1113. CallocName, Attrs, B.getInt8PtrTy(), PtrType, PtrType);
  1114. inferLibFuncAttributes(M, CallocName, TLI);
  1115. CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
  1116. if (const auto *F =
  1117. dyn_cast<Function>(Calloc.getCallee()->stripPointerCasts()))
  1118. CI->setCallingConv(F->getCallingConv());
  1119. return CI;
  1120. }
  1121. Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
  1122. IRBuilder<> &B, const DataLayout &DL,
  1123. const TargetLibraryInfo *TLI) {
  1124. if (!TLI->has(LibFunc_fwrite_unlocked))
  1125. return nullptr;
  1126. Module *M = B.GetInsertBlock()->getModule();
  1127. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1128. StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked);
  1129. FunctionCallee F = M->getOrInsertFunction(
  1130. FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
  1131. DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
  1132. if (File->getType()->isPointerTy())
  1133. inferLibFuncAttributes(M, FWriteUnlockedName, *TLI);
  1134. CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
  1135. if (const Function *Fn =
  1136. dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
  1137. CI->setCallingConv(Fn->getCallingConv());
  1138. return CI;
  1139. }
  1140. Value *llvm::emitFGetCUnlocked(Value *File, IRBuilder<> &B,
  1141. const TargetLibraryInfo *TLI) {
  1142. if (!TLI->has(LibFunc_fgetc_unlocked))
  1143. return nullptr;
  1144. Module *M = B.GetInsertBlock()->getModule();
  1145. StringRef FGetCUnlockedName = TLI->getName(LibFunc_fgetc_unlocked);
  1146. FunctionCallee F = M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(),
  1147. File->getType());
  1148. if (File->getType()->isPointerTy())
  1149. inferLibFuncAttributes(M, FGetCUnlockedName, *TLI);
  1150. CallInst *CI = B.CreateCall(F, File, FGetCUnlockedName);
  1151. if (const Function *Fn =
  1152. dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
  1153. CI->setCallingConv(Fn->getCallingConv());
  1154. return CI;
  1155. }
  1156. Value *llvm::emitFGetSUnlocked(Value *Str, Value *Size, Value *File,
  1157. IRBuilder<> &B, const TargetLibraryInfo *TLI) {
  1158. if (!TLI->has(LibFunc_fgets_unlocked))
  1159. return nullptr;
  1160. Module *M = B.GetInsertBlock()->getModule();
  1161. StringRef FGetSUnlockedName = TLI->getName(LibFunc_fgets_unlocked);
  1162. FunctionCallee F =
  1163. M->getOrInsertFunction(FGetSUnlockedName, B.getInt8PtrTy(),
  1164. B.getInt8PtrTy(), B.getInt32Ty(), File->getType());
  1165. inferLibFuncAttributes(M, FGetSUnlockedName, *TLI);
  1166. CallInst *CI =
  1167. B.CreateCall(F, {castToCStr(Str, B), Size, File}, FGetSUnlockedName);
  1168. if (const Function *Fn =
  1169. dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
  1170. CI->setCallingConv(Fn->getCallingConv());
  1171. return CI;
  1172. }
  1173. Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
  1174. IRBuilder<> &B, const DataLayout &DL,
  1175. const TargetLibraryInfo *TLI) {
  1176. if (!TLI->has(LibFunc_fread_unlocked))
  1177. return nullptr;
  1178. Module *M = B.GetInsertBlock()->getModule();
  1179. LLVMContext &Context = B.GetInsertBlock()->getContext();
  1180. StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked);
  1181. FunctionCallee F = M->getOrInsertFunction(
  1182. FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
  1183. DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
  1184. if (File->getType()->isPointerTy())
  1185. inferLibFuncAttributes(M, FReadUnlockedName, *TLI);
  1186. CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
  1187. if (const Function *Fn =
  1188. dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
  1189. CI->setCallingConv(Fn->getCallingConv());
  1190. return CI;
  1191. }