BuildLibCalls.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements some functions that will create standard C libcalls.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/BuildLibCalls.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/Analysis/TargetLibraryInfo.h"
  17. #include "llvm/IR/Constants.h"
  18. #include "llvm/IR/DataLayout.h"
  19. #include "llvm/IR/Function.h"
  20. #include "llvm/IR/IRBuilder.h"
  21. #include "llvm/IR/Intrinsics.h"
  22. #include "llvm/IR/LLVMContext.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/IR/Type.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(NumReadOnly, "Number of functions inferred as readonly");
  30. STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly");
  31. STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind");
  32. STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture");
  33. STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly");
  34. STATISTIC(NumNoAlias, "Number of function returns inferred as noalias");
  35. STATISTIC(NumNonNull, "Number of function returns inferred as nonnull returns");
  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 setDoesNotCapture(Function &F, unsigned n) {
  65. if (F.doesNotCapture(n))
  66. return false;
  67. F.setDoesNotCapture(n);
  68. ++NumNoCapture;
  69. return true;
  70. }
  71. static bool setOnlyReadsMemory(Function &F, unsigned n) {
  72. if (F.onlyReadsMemory(n))
  73. return false;
  74. F.setOnlyReadsMemory(n);
  75. ++NumReadOnlyArg;
  76. return true;
  77. }
  78. static bool setDoesNotAlias(Function &F, unsigned n) {
  79. if (F.doesNotAlias(n))
  80. return false;
  81. F.setDoesNotAlias(n);
  82. ++NumNoAlias;
  83. return true;
  84. }
  85. static bool setNonNull(Function &F, unsigned n) {
  86. assert(
  87. (n != AttributeList::ReturnIndex || F.getReturnType()->isPointerTy()) &&
  88. "nonnull applies only to pointers");
  89. if (F.getAttributes().hasAttribute(n, Attribute::NonNull))
  90. return false;
  91. F.addAttribute(n, Attribute::NonNull);
  92. ++NumNonNull;
  93. return true;
  94. }
  95. bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
  96. LibFunc TheLibFunc;
  97. if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc)))
  98. return false;
  99. bool Changed = false;
  100. switch (TheLibFunc) {
  101. case LibFunc_strlen:
  102. Changed |= setOnlyReadsMemory(F);
  103. Changed |= setDoesNotThrow(F);
  104. Changed |= setDoesNotCapture(F, 1);
  105. return Changed;
  106. case LibFunc_strchr:
  107. case LibFunc_strrchr:
  108. Changed |= setOnlyReadsMemory(F);
  109. Changed |= setDoesNotThrow(F);
  110. return Changed;
  111. case LibFunc_strtol:
  112. case LibFunc_strtod:
  113. case LibFunc_strtof:
  114. case LibFunc_strtoul:
  115. case LibFunc_strtoll:
  116. case LibFunc_strtold:
  117. case LibFunc_strtoull:
  118. Changed |= setDoesNotThrow(F);
  119. Changed |= setDoesNotCapture(F, 2);
  120. Changed |= setOnlyReadsMemory(F, 1);
  121. return Changed;
  122. case LibFunc_strcpy:
  123. case LibFunc_stpcpy:
  124. case LibFunc_strcat:
  125. case LibFunc_strncat:
  126. case LibFunc_strncpy:
  127. case LibFunc_stpncpy:
  128. Changed |= setDoesNotThrow(F);
  129. Changed |= setDoesNotCapture(F, 2);
  130. Changed |= setOnlyReadsMemory(F, 2);
  131. return Changed;
  132. case LibFunc_strxfrm:
  133. Changed |= setDoesNotThrow(F);
  134. Changed |= setDoesNotCapture(F, 1);
  135. Changed |= setDoesNotCapture(F, 2);
  136. Changed |= setOnlyReadsMemory(F, 2);
  137. return Changed;
  138. case LibFunc_strcmp: // 0,1
  139. case LibFunc_strspn: // 0,1
  140. case LibFunc_strncmp: // 0,1
  141. case LibFunc_strcspn: // 0,1
  142. case LibFunc_strcoll: // 0,1
  143. case LibFunc_strcasecmp: // 0,1
  144. case LibFunc_strncasecmp: //
  145. Changed |= setOnlyReadsMemory(F);
  146. Changed |= setDoesNotThrow(F);
  147. Changed |= setDoesNotCapture(F, 1);
  148. Changed |= setDoesNotCapture(F, 2);
  149. return Changed;
  150. case LibFunc_strstr:
  151. case LibFunc_strpbrk:
  152. Changed |= setOnlyReadsMemory(F);
  153. Changed |= setDoesNotThrow(F);
  154. Changed |= setDoesNotCapture(F, 2);
  155. return Changed;
  156. case LibFunc_strtok:
  157. case LibFunc_strtok_r:
  158. Changed |= setDoesNotThrow(F);
  159. Changed |= setDoesNotCapture(F, 2);
  160. Changed |= setOnlyReadsMemory(F, 2);
  161. return Changed;
  162. case LibFunc_scanf:
  163. Changed |= setDoesNotThrow(F);
  164. Changed |= setDoesNotCapture(F, 1);
  165. Changed |= setOnlyReadsMemory(F, 1);
  166. return Changed;
  167. case LibFunc_setbuf:
  168. case LibFunc_setvbuf:
  169. Changed |= setDoesNotThrow(F);
  170. Changed |= setDoesNotCapture(F, 1);
  171. return Changed;
  172. case LibFunc_strdup:
  173. case LibFunc_strndup:
  174. Changed |= setDoesNotThrow(F);
  175. Changed |= setDoesNotAlias(F, 0);
  176. Changed |= setDoesNotCapture(F, 1);
  177. Changed |= setOnlyReadsMemory(F, 1);
  178. return Changed;
  179. case LibFunc_stat:
  180. case LibFunc_statvfs:
  181. Changed |= setDoesNotThrow(F);
  182. Changed |= setDoesNotCapture(F, 1);
  183. Changed |= setDoesNotCapture(F, 2);
  184. Changed |= setOnlyReadsMemory(F, 1);
  185. return Changed;
  186. case LibFunc_sscanf:
  187. Changed |= setDoesNotThrow(F);
  188. Changed |= setDoesNotCapture(F, 1);
  189. Changed |= setDoesNotCapture(F, 2);
  190. Changed |= setOnlyReadsMemory(F, 1);
  191. Changed |= setOnlyReadsMemory(F, 2);
  192. return Changed;
  193. case LibFunc_sprintf:
  194. Changed |= setDoesNotThrow(F);
  195. Changed |= setDoesNotCapture(F, 1);
  196. Changed |= setDoesNotCapture(F, 2);
  197. Changed |= setOnlyReadsMemory(F, 2);
  198. return Changed;
  199. case LibFunc_snprintf:
  200. Changed |= setDoesNotThrow(F);
  201. Changed |= setDoesNotCapture(F, 1);
  202. Changed |= setDoesNotCapture(F, 3);
  203. Changed |= setOnlyReadsMemory(F, 3);
  204. return Changed;
  205. case LibFunc_setitimer:
  206. Changed |= setDoesNotThrow(F);
  207. Changed |= setDoesNotCapture(F, 2);
  208. Changed |= setDoesNotCapture(F, 3);
  209. Changed |= setOnlyReadsMemory(F, 2);
  210. return Changed;
  211. case LibFunc_system:
  212. // May throw; "system" is a valid pthread cancellation point.
  213. Changed |= setDoesNotCapture(F, 1);
  214. Changed |= setOnlyReadsMemory(F, 1);
  215. return Changed;
  216. case LibFunc_malloc:
  217. Changed |= setDoesNotThrow(F);
  218. Changed |= setDoesNotAlias(F, 0);
  219. return Changed;
  220. case LibFunc_memcmp:
  221. Changed |= setOnlyReadsMemory(F);
  222. Changed |= setDoesNotThrow(F);
  223. Changed |= setDoesNotCapture(F, 1);
  224. Changed |= setDoesNotCapture(F, 2);
  225. return Changed;
  226. case LibFunc_memchr:
  227. case LibFunc_memrchr:
  228. Changed |= setOnlyReadsMemory(F);
  229. Changed |= setDoesNotThrow(F);
  230. return Changed;
  231. case LibFunc_modf:
  232. case LibFunc_modff:
  233. case LibFunc_modfl:
  234. Changed |= setDoesNotThrow(F);
  235. Changed |= setDoesNotCapture(F, 2);
  236. return Changed;
  237. case LibFunc_memcpy:
  238. case LibFunc_mempcpy:
  239. case LibFunc_memccpy:
  240. case LibFunc_memmove:
  241. Changed |= setDoesNotThrow(F);
  242. Changed |= setDoesNotCapture(F, 2);
  243. Changed |= setOnlyReadsMemory(F, 2);
  244. return Changed;
  245. case LibFunc_memcpy_chk:
  246. Changed |= setDoesNotThrow(F);
  247. return Changed;
  248. case LibFunc_memalign:
  249. Changed |= setDoesNotAlias(F, 0);
  250. return Changed;
  251. case LibFunc_mkdir:
  252. Changed |= setDoesNotThrow(F);
  253. Changed |= setDoesNotCapture(F, 1);
  254. Changed |= setOnlyReadsMemory(F, 1);
  255. return Changed;
  256. case LibFunc_mktime:
  257. Changed |= setDoesNotThrow(F);
  258. Changed |= setDoesNotCapture(F, 1);
  259. return Changed;
  260. case LibFunc_realloc:
  261. Changed |= setDoesNotThrow(F);
  262. Changed |= setDoesNotAlias(F, 0);
  263. Changed |= setDoesNotCapture(F, 1);
  264. return Changed;
  265. case LibFunc_read:
  266. // May throw; "read" is a valid pthread cancellation point.
  267. Changed |= setDoesNotCapture(F, 2);
  268. return Changed;
  269. case LibFunc_rewind:
  270. Changed |= setDoesNotThrow(F);
  271. Changed |= setDoesNotCapture(F, 1);
  272. return Changed;
  273. case LibFunc_rmdir:
  274. case LibFunc_remove:
  275. case LibFunc_realpath:
  276. Changed |= setDoesNotThrow(F);
  277. Changed |= setDoesNotCapture(F, 1);
  278. Changed |= setOnlyReadsMemory(F, 1);
  279. return Changed;
  280. case LibFunc_rename:
  281. Changed |= setDoesNotThrow(F);
  282. Changed |= setDoesNotCapture(F, 1);
  283. Changed |= setDoesNotCapture(F, 2);
  284. Changed |= setOnlyReadsMemory(F, 1);
  285. Changed |= setOnlyReadsMemory(F, 2);
  286. return Changed;
  287. case LibFunc_readlink:
  288. Changed |= setDoesNotThrow(F);
  289. Changed |= setDoesNotCapture(F, 1);
  290. Changed |= setDoesNotCapture(F, 2);
  291. Changed |= setOnlyReadsMemory(F, 1);
  292. return Changed;
  293. case LibFunc_write:
  294. // May throw; "write" is a valid pthread cancellation point.
  295. Changed |= setDoesNotCapture(F, 2);
  296. Changed |= setOnlyReadsMemory(F, 2);
  297. return Changed;
  298. case LibFunc_bcopy:
  299. Changed |= setDoesNotThrow(F);
  300. Changed |= setDoesNotCapture(F, 1);
  301. Changed |= setDoesNotCapture(F, 2);
  302. Changed |= setOnlyReadsMemory(F, 1);
  303. return Changed;
  304. case LibFunc_bcmp:
  305. Changed |= setDoesNotThrow(F);
  306. Changed |= setOnlyReadsMemory(F);
  307. Changed |= setDoesNotCapture(F, 1);
  308. Changed |= setDoesNotCapture(F, 2);
  309. return Changed;
  310. case LibFunc_bzero:
  311. Changed |= setDoesNotThrow(F);
  312. Changed |= setDoesNotCapture(F, 1);
  313. return Changed;
  314. case LibFunc_calloc:
  315. Changed |= setDoesNotThrow(F);
  316. Changed |= setDoesNotAlias(F, 0);
  317. return Changed;
  318. case LibFunc_chmod:
  319. case LibFunc_chown:
  320. Changed |= setDoesNotThrow(F);
  321. Changed |= setDoesNotCapture(F, 1);
  322. Changed |= setOnlyReadsMemory(F, 1);
  323. return Changed;
  324. case LibFunc_ctermid:
  325. case LibFunc_clearerr:
  326. case LibFunc_closedir:
  327. Changed |= setDoesNotThrow(F);
  328. Changed |= setDoesNotCapture(F, 1);
  329. return Changed;
  330. case LibFunc_atoi:
  331. case LibFunc_atol:
  332. case LibFunc_atof:
  333. case LibFunc_atoll:
  334. Changed |= setDoesNotThrow(F);
  335. Changed |= setOnlyReadsMemory(F);
  336. Changed |= setDoesNotCapture(F, 1);
  337. return Changed;
  338. case LibFunc_access:
  339. Changed |= setDoesNotThrow(F);
  340. Changed |= setDoesNotCapture(F, 1);
  341. Changed |= setOnlyReadsMemory(F, 1);
  342. return Changed;
  343. case LibFunc_fopen:
  344. Changed |= setDoesNotThrow(F);
  345. Changed |= setDoesNotAlias(F, 0);
  346. Changed |= setDoesNotCapture(F, 1);
  347. Changed |= setDoesNotCapture(F, 2);
  348. Changed |= setOnlyReadsMemory(F, 1);
  349. Changed |= setOnlyReadsMemory(F, 2);
  350. return Changed;
  351. case LibFunc_fdopen:
  352. Changed |= setDoesNotThrow(F);
  353. Changed |= setDoesNotAlias(F, 0);
  354. Changed |= setDoesNotCapture(F, 2);
  355. Changed |= setOnlyReadsMemory(F, 2);
  356. return Changed;
  357. case LibFunc_feof:
  358. case LibFunc_free:
  359. case LibFunc_fseek:
  360. case LibFunc_ftell:
  361. case LibFunc_fgetc:
  362. case LibFunc_fseeko:
  363. case LibFunc_ftello:
  364. case LibFunc_fileno:
  365. case LibFunc_fflush:
  366. case LibFunc_fclose:
  367. case LibFunc_fsetpos:
  368. case LibFunc_flockfile:
  369. case LibFunc_funlockfile:
  370. case LibFunc_ftrylockfile:
  371. Changed |= setDoesNotThrow(F);
  372. Changed |= setDoesNotCapture(F, 1);
  373. return Changed;
  374. case LibFunc_ferror:
  375. Changed |= setDoesNotThrow(F);
  376. Changed |= setDoesNotCapture(F, 1);
  377. Changed |= setOnlyReadsMemory(F);
  378. return Changed;
  379. case LibFunc_fputc:
  380. case LibFunc_fstat:
  381. case LibFunc_frexp:
  382. case LibFunc_frexpf:
  383. case LibFunc_frexpl:
  384. case LibFunc_fstatvfs:
  385. Changed |= setDoesNotThrow(F);
  386. Changed |= setDoesNotCapture(F, 2);
  387. return Changed;
  388. case LibFunc_fgets:
  389. Changed |= setDoesNotThrow(F);
  390. Changed |= setDoesNotCapture(F, 3);
  391. return Changed;
  392. case LibFunc_fread:
  393. Changed |= setDoesNotThrow(F);
  394. Changed |= setDoesNotCapture(F, 1);
  395. Changed |= setDoesNotCapture(F, 4);
  396. return Changed;
  397. case LibFunc_fwrite:
  398. Changed |= setDoesNotThrow(F);
  399. Changed |= setDoesNotCapture(F, 1);
  400. Changed |= setDoesNotCapture(F, 4);
  401. // FIXME: readonly #1?
  402. return Changed;
  403. case LibFunc_fputs:
  404. Changed |= setDoesNotThrow(F);
  405. Changed |= setDoesNotCapture(F, 1);
  406. Changed |= setDoesNotCapture(F, 2);
  407. Changed |= setOnlyReadsMemory(F, 1);
  408. return Changed;
  409. case LibFunc_fscanf:
  410. case LibFunc_fprintf:
  411. Changed |= setDoesNotThrow(F);
  412. Changed |= setDoesNotCapture(F, 1);
  413. Changed |= setDoesNotCapture(F, 2);
  414. Changed |= setOnlyReadsMemory(F, 2);
  415. return Changed;
  416. case LibFunc_fgetpos:
  417. Changed |= setDoesNotThrow(F);
  418. Changed |= setDoesNotCapture(F, 1);
  419. Changed |= setDoesNotCapture(F, 2);
  420. return Changed;
  421. case LibFunc_getc:
  422. case LibFunc_getlogin_r:
  423. case LibFunc_getc_unlocked:
  424. Changed |= setDoesNotThrow(F);
  425. Changed |= setDoesNotCapture(F, 1);
  426. return Changed;
  427. case LibFunc_getenv:
  428. Changed |= setDoesNotThrow(F);
  429. Changed |= setOnlyReadsMemory(F);
  430. Changed |= setDoesNotCapture(F, 1);
  431. return Changed;
  432. case LibFunc_gets:
  433. case LibFunc_getchar:
  434. Changed |= setDoesNotThrow(F);
  435. return Changed;
  436. case LibFunc_getitimer:
  437. Changed |= setDoesNotThrow(F);
  438. Changed |= setDoesNotCapture(F, 2);
  439. return Changed;
  440. case LibFunc_getpwnam:
  441. Changed |= setDoesNotThrow(F);
  442. Changed |= setDoesNotCapture(F, 1);
  443. Changed |= setOnlyReadsMemory(F, 1);
  444. return Changed;
  445. case LibFunc_ungetc:
  446. Changed |= setDoesNotThrow(F);
  447. Changed |= setDoesNotCapture(F, 2);
  448. return Changed;
  449. case LibFunc_uname:
  450. Changed |= setDoesNotThrow(F);
  451. Changed |= setDoesNotCapture(F, 1);
  452. return Changed;
  453. case LibFunc_unlink:
  454. Changed |= setDoesNotThrow(F);
  455. Changed |= setDoesNotCapture(F, 1);
  456. Changed |= setOnlyReadsMemory(F, 1);
  457. return Changed;
  458. case LibFunc_unsetenv:
  459. Changed |= setDoesNotThrow(F);
  460. Changed |= setDoesNotCapture(F, 1);
  461. Changed |= setOnlyReadsMemory(F, 1);
  462. return Changed;
  463. case LibFunc_utime:
  464. case LibFunc_utimes:
  465. Changed |= setDoesNotThrow(F);
  466. Changed |= setDoesNotCapture(F, 1);
  467. Changed |= setDoesNotCapture(F, 2);
  468. Changed |= setOnlyReadsMemory(F, 1);
  469. Changed |= setOnlyReadsMemory(F, 2);
  470. return Changed;
  471. case LibFunc_putc:
  472. Changed |= setDoesNotThrow(F);
  473. Changed |= setDoesNotCapture(F, 2);
  474. return Changed;
  475. case LibFunc_puts:
  476. case LibFunc_printf:
  477. case LibFunc_perror:
  478. Changed |= setDoesNotThrow(F);
  479. Changed |= setDoesNotCapture(F, 1);
  480. Changed |= setOnlyReadsMemory(F, 1);
  481. return Changed;
  482. case LibFunc_pread:
  483. // May throw; "pread" is a valid pthread cancellation point.
  484. Changed |= setDoesNotCapture(F, 2);
  485. return Changed;
  486. case LibFunc_pwrite:
  487. // May throw; "pwrite" is a valid pthread cancellation point.
  488. Changed |= setDoesNotCapture(F, 2);
  489. Changed |= setOnlyReadsMemory(F, 2);
  490. return Changed;
  491. case LibFunc_putchar:
  492. Changed |= setDoesNotThrow(F);
  493. return Changed;
  494. case LibFunc_popen:
  495. Changed |= setDoesNotThrow(F);
  496. Changed |= setDoesNotAlias(F, 0);
  497. Changed |= setDoesNotCapture(F, 1);
  498. Changed |= setDoesNotCapture(F, 2);
  499. Changed |= setOnlyReadsMemory(F, 1);
  500. Changed |= setOnlyReadsMemory(F, 2);
  501. return Changed;
  502. case LibFunc_pclose:
  503. Changed |= setDoesNotThrow(F);
  504. Changed |= setDoesNotCapture(F, 1);
  505. return Changed;
  506. case LibFunc_vscanf:
  507. Changed |= setDoesNotThrow(F);
  508. Changed |= setDoesNotCapture(F, 1);
  509. Changed |= setOnlyReadsMemory(F, 1);
  510. return Changed;
  511. case LibFunc_vsscanf:
  512. Changed |= setDoesNotThrow(F);
  513. Changed |= setDoesNotCapture(F, 1);
  514. Changed |= setDoesNotCapture(F, 2);
  515. Changed |= setOnlyReadsMemory(F, 1);
  516. Changed |= setOnlyReadsMemory(F, 2);
  517. return Changed;
  518. case LibFunc_vfscanf:
  519. Changed |= setDoesNotThrow(F);
  520. Changed |= setDoesNotCapture(F, 1);
  521. Changed |= setDoesNotCapture(F, 2);
  522. Changed |= setOnlyReadsMemory(F, 2);
  523. return Changed;
  524. case LibFunc_valloc:
  525. Changed |= setDoesNotThrow(F);
  526. Changed |= setDoesNotAlias(F, 0);
  527. return Changed;
  528. case LibFunc_vprintf:
  529. Changed |= setDoesNotThrow(F);
  530. Changed |= setDoesNotCapture(F, 1);
  531. Changed |= setOnlyReadsMemory(F, 1);
  532. return Changed;
  533. case LibFunc_vfprintf:
  534. case LibFunc_vsprintf:
  535. Changed |= setDoesNotThrow(F);
  536. Changed |= setDoesNotCapture(F, 1);
  537. Changed |= setDoesNotCapture(F, 2);
  538. Changed |= setOnlyReadsMemory(F, 2);
  539. return Changed;
  540. case LibFunc_vsnprintf:
  541. Changed |= setDoesNotThrow(F);
  542. Changed |= setDoesNotCapture(F, 1);
  543. Changed |= setDoesNotCapture(F, 3);
  544. Changed |= setOnlyReadsMemory(F, 3);
  545. return Changed;
  546. case LibFunc_open:
  547. // May throw; "open" is a valid pthread cancellation point.
  548. Changed |= setDoesNotCapture(F, 1);
  549. Changed |= setOnlyReadsMemory(F, 1);
  550. return Changed;
  551. case LibFunc_opendir:
  552. Changed |= setDoesNotThrow(F);
  553. Changed |= setDoesNotAlias(F, 0);
  554. Changed |= setDoesNotCapture(F, 1);
  555. Changed |= setOnlyReadsMemory(F, 1);
  556. return Changed;
  557. case LibFunc_tmpfile:
  558. Changed |= setDoesNotThrow(F);
  559. Changed |= setDoesNotAlias(F, 0);
  560. return Changed;
  561. case LibFunc_times:
  562. Changed |= setDoesNotThrow(F);
  563. Changed |= setDoesNotCapture(F, 1);
  564. return Changed;
  565. case LibFunc_htonl:
  566. case LibFunc_htons:
  567. case LibFunc_ntohl:
  568. case LibFunc_ntohs:
  569. Changed |= setDoesNotThrow(F);
  570. Changed |= setDoesNotAccessMemory(F);
  571. return Changed;
  572. case LibFunc_lstat:
  573. Changed |= setDoesNotThrow(F);
  574. Changed |= setDoesNotCapture(F, 1);
  575. Changed |= setDoesNotCapture(F, 2);
  576. Changed |= setOnlyReadsMemory(F, 1);
  577. return Changed;
  578. case LibFunc_lchown:
  579. Changed |= setDoesNotThrow(F);
  580. Changed |= setDoesNotCapture(F, 1);
  581. Changed |= setOnlyReadsMemory(F, 1);
  582. return Changed;
  583. case LibFunc_qsort:
  584. // May throw; places call through function pointer.
  585. Changed |= setDoesNotCapture(F, 4);
  586. return Changed;
  587. case LibFunc_dunder_strdup:
  588. case LibFunc_dunder_strndup:
  589. Changed |= setDoesNotThrow(F);
  590. Changed |= setDoesNotAlias(F, 0);
  591. Changed |= setDoesNotCapture(F, 1);
  592. Changed |= setOnlyReadsMemory(F, 1);
  593. return Changed;
  594. case LibFunc_dunder_strtok_r:
  595. Changed |= setDoesNotThrow(F);
  596. Changed |= setDoesNotCapture(F, 2);
  597. Changed |= setOnlyReadsMemory(F, 2);
  598. return Changed;
  599. case LibFunc_under_IO_getc:
  600. Changed |= setDoesNotThrow(F);
  601. Changed |= setDoesNotCapture(F, 1);
  602. return Changed;
  603. case LibFunc_under_IO_putc:
  604. Changed |= setDoesNotThrow(F);
  605. Changed |= setDoesNotCapture(F, 2);
  606. return Changed;
  607. case LibFunc_dunder_isoc99_scanf:
  608. Changed |= setDoesNotThrow(F);
  609. Changed |= setDoesNotCapture(F, 1);
  610. Changed |= setOnlyReadsMemory(F, 1);
  611. return Changed;
  612. case LibFunc_stat64:
  613. case LibFunc_lstat64:
  614. case LibFunc_statvfs64:
  615. Changed |= setDoesNotThrow(F);
  616. Changed |= setDoesNotCapture(F, 1);
  617. Changed |= setDoesNotCapture(F, 2);
  618. Changed |= setOnlyReadsMemory(F, 1);
  619. return Changed;
  620. case LibFunc_dunder_isoc99_sscanf:
  621. Changed |= setDoesNotThrow(F);
  622. Changed |= setDoesNotCapture(F, 1);
  623. Changed |= setDoesNotCapture(F, 2);
  624. Changed |= setOnlyReadsMemory(F, 1);
  625. Changed |= setOnlyReadsMemory(F, 2);
  626. return Changed;
  627. case LibFunc_fopen64:
  628. Changed |= setDoesNotThrow(F);
  629. Changed |= setDoesNotAlias(F, 0);
  630. Changed |= setDoesNotCapture(F, 1);
  631. Changed |= setDoesNotCapture(F, 2);
  632. Changed |= setOnlyReadsMemory(F, 1);
  633. Changed |= setOnlyReadsMemory(F, 2);
  634. return Changed;
  635. case LibFunc_fseeko64:
  636. case LibFunc_ftello64:
  637. Changed |= setDoesNotThrow(F);
  638. Changed |= setDoesNotCapture(F, 1);
  639. return Changed;
  640. case LibFunc_tmpfile64:
  641. Changed |= setDoesNotThrow(F);
  642. Changed |= setDoesNotAlias(F, 0);
  643. return Changed;
  644. case LibFunc_fstat64:
  645. case LibFunc_fstatvfs64:
  646. Changed |= setDoesNotThrow(F);
  647. Changed |= setDoesNotCapture(F, 2);
  648. return Changed;
  649. case LibFunc_open64:
  650. // May throw; "open" is a valid pthread cancellation point.
  651. Changed |= setDoesNotCapture(F, 1);
  652. Changed |= setOnlyReadsMemory(F, 1);
  653. return Changed;
  654. case LibFunc_gettimeofday:
  655. // Currently some platforms have the restrict keyword on the arguments to
  656. // gettimeofday. To be conservative, do not add noalias to gettimeofday's
  657. // arguments.
  658. Changed |= setDoesNotThrow(F);
  659. Changed |= setDoesNotCapture(F, 1);
  660. Changed |= setDoesNotCapture(F, 2);
  661. return Changed;
  662. case LibFunc_Znwj: // new(unsigned int)
  663. case LibFunc_Znwm: // new(unsigned long)
  664. case LibFunc_Znaj: // new[](unsigned int)
  665. case LibFunc_Znam: // new[](unsigned long)
  666. case LibFunc_msvc_new_int: // new(unsigned int)
  667. case LibFunc_msvc_new_longlong: // new(unsigned long long)
  668. case LibFunc_msvc_new_array_int: // new[](unsigned int)
  669. case LibFunc_msvc_new_array_longlong: // new[](unsigned long long)
  670. // Operator new always returns a nonnull noalias pointer
  671. Changed |= setNonNull(F, AttributeList::ReturnIndex);
  672. Changed |= setDoesNotAlias(F, AttributeList::ReturnIndex);
  673. return Changed;
  674. //TODO: add LibFunc entries for:
  675. //case LibFunc_memset_pattern4:
  676. //case LibFunc_memset_pattern8:
  677. case LibFunc_memset_pattern16:
  678. Changed |= setOnlyAccessesArgMemory(F);
  679. Changed |= setDoesNotCapture(F, 1);
  680. Changed |= setDoesNotCapture(F, 2);
  681. Changed |= setOnlyReadsMemory(F, 2);
  682. return Changed;
  683. // int __nvvm_reflect(const char *)
  684. case LibFunc_nvvm_reflect:
  685. Changed |= setDoesNotAccessMemory(F);
  686. Changed |= setDoesNotThrow(F);
  687. return Changed;
  688. default:
  689. // FIXME: It'd be really nice to cover all the library functions we're
  690. // aware of here.
  691. return false;
  692. }
  693. }
  694. //- Emit LibCalls ------------------------------------------------------------//
  695. Value *llvm::castToCStr(Value *V, IRBuilder<> &B) {
  696. unsigned AS = V->getType()->getPointerAddressSpace();
  697. return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
  698. }
  699. Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
  700. const TargetLibraryInfo *TLI) {
  701. if (!TLI->has(LibFunc_strlen))
  702. return nullptr;
  703. Module *M = B.GetInsertBlock()->getModule();
  704. LLVMContext &Context = B.GetInsertBlock()->getContext();
  705. Constant *StrLen = M->getOrInsertFunction("strlen", DL.getIntPtrType(Context),
  706. B.getInt8PtrTy());
  707. inferLibFuncAttributes(*M->getFunction("strlen"), *TLI);
  708. CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), "strlen");
  709. if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
  710. CI->setCallingConv(F->getCallingConv());
  711. return CI;
  712. }
  713. Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
  714. const TargetLibraryInfo *TLI) {
  715. if (!TLI->has(LibFunc_strchr))
  716. return nullptr;
  717. Module *M = B.GetInsertBlock()->getModule();
  718. Type *I8Ptr = B.getInt8PtrTy();
  719. Type *I32Ty = B.getInt32Ty();
  720. Constant *StrChr =
  721. M->getOrInsertFunction("strchr", I8Ptr, I8Ptr, I32Ty);
  722. inferLibFuncAttributes(*M->getFunction("strchr"), *TLI);
  723. CallInst *CI = B.CreateCall(
  724. StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
  725. if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
  726. CI->setCallingConv(F->getCallingConv());
  727. return CI;
  728. }
  729. Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
  730. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  731. if (!TLI->has(LibFunc_strncmp))
  732. return nullptr;
  733. Module *M = B.GetInsertBlock()->getModule();
  734. LLVMContext &Context = B.GetInsertBlock()->getContext();
  735. Value *StrNCmp = M->getOrInsertFunction("strncmp", B.getInt32Ty(),
  736. B.getInt8PtrTy(), B.getInt8PtrTy(),
  737. DL.getIntPtrType(Context));
  738. inferLibFuncAttributes(*M->getFunction("strncmp"), *TLI);
  739. CallInst *CI = B.CreateCall(
  740. StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "strncmp");
  741. if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
  742. CI->setCallingConv(F->getCallingConv());
  743. return CI;
  744. }
  745. Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
  746. const TargetLibraryInfo *TLI, StringRef Name) {
  747. if (!TLI->has(LibFunc_strcpy))
  748. return nullptr;
  749. Module *M = B.GetInsertBlock()->getModule();
  750. Type *I8Ptr = B.getInt8PtrTy();
  751. Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
  752. inferLibFuncAttributes(*M->getFunction(Name), *TLI);
  753. CallInst *CI =
  754. B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
  755. if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
  756. CI->setCallingConv(F->getCallingConv());
  757. return CI;
  758. }
  759. Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
  760. const TargetLibraryInfo *TLI, StringRef Name) {
  761. if (!TLI->has(LibFunc_strncpy))
  762. return nullptr;
  763. Module *M = B.GetInsertBlock()->getModule();
  764. Type *I8Ptr = B.getInt8PtrTy();
  765. Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr,
  766. Len->getType());
  767. inferLibFuncAttributes(*M->getFunction(Name), *TLI);
  768. CallInst *CI = B.CreateCall(
  769. StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, "strncpy");
  770. if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
  771. CI->setCallingConv(F->getCallingConv());
  772. return CI;
  773. }
  774. Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
  775. IRBuilder<> &B, const DataLayout &DL,
  776. const TargetLibraryInfo *TLI) {
  777. if (!TLI->has(LibFunc_memcpy_chk))
  778. return nullptr;
  779. Module *M = B.GetInsertBlock()->getModule();
  780. AttributeList AS;
  781. AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex,
  782. Attribute::NoUnwind);
  783. LLVMContext &Context = B.GetInsertBlock()->getContext();
  784. Value *MemCpy = M->getOrInsertFunction(
  785. "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
  786. B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
  787. DL.getIntPtrType(Context));
  788. Dst = castToCStr(Dst, B);
  789. Src = castToCStr(Src, B);
  790. CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
  791. if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
  792. CI->setCallingConv(F->getCallingConv());
  793. return CI;
  794. }
  795. Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
  796. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  797. if (!TLI->has(LibFunc_memchr))
  798. return nullptr;
  799. Module *M = B.GetInsertBlock()->getModule();
  800. LLVMContext &Context = B.GetInsertBlock()->getContext();
  801. Value *MemChr = M->getOrInsertFunction("memchr", B.getInt8PtrTy(),
  802. B.getInt8PtrTy(), B.getInt32Ty(),
  803. DL.getIntPtrType(Context));
  804. inferLibFuncAttributes(*M->getFunction("memchr"), *TLI);
  805. CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, "memchr");
  806. if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
  807. CI->setCallingConv(F->getCallingConv());
  808. return CI;
  809. }
  810. Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
  811. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  812. if (!TLI->has(LibFunc_memcmp))
  813. return nullptr;
  814. Module *M = B.GetInsertBlock()->getModule();
  815. LLVMContext &Context = B.GetInsertBlock()->getContext();
  816. Value *MemCmp = M->getOrInsertFunction("memcmp", B.getInt32Ty(),
  817. B.getInt8PtrTy(), B.getInt8PtrTy(),
  818. DL.getIntPtrType(Context));
  819. inferLibFuncAttributes(*M->getFunction("memcmp"), *TLI);
  820. CallInst *CI = B.CreateCall(
  821. MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "memcmp");
  822. if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
  823. CI->setCallingConv(F->getCallingConv());
  824. return CI;
  825. }
  826. /// Append a suffix to the function name according to the type of 'Op'.
  827. static void appendTypeSuffix(Value *Op, StringRef &Name,
  828. SmallString<20> &NameBuffer) {
  829. if (!Op->getType()->isDoubleTy()) {
  830. NameBuffer += Name;
  831. if (Op->getType()->isFloatTy())
  832. NameBuffer += 'f';
  833. else
  834. NameBuffer += 'l';
  835. Name = NameBuffer;
  836. }
  837. }
  838. Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
  839. const AttributeList &Attrs) {
  840. SmallString<20> NameBuffer;
  841. appendTypeSuffix(Op, Name, NameBuffer);
  842. Module *M = B.GetInsertBlock()->getModule();
  843. Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
  844. Op->getType());
  845. CallInst *CI = B.CreateCall(Callee, Op, Name);
  846. CI->setAttributes(Attrs);
  847. if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
  848. CI->setCallingConv(F->getCallingConv());
  849. return CI;
  850. }
  851. Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
  852. IRBuilder<> &B, const AttributeList &Attrs) {
  853. SmallString<20> NameBuffer;
  854. appendTypeSuffix(Op1, Name, NameBuffer);
  855. Module *M = B.GetInsertBlock()->getModule();
  856. Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(),
  857. Op2->getType());
  858. CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
  859. CI->setAttributes(Attrs);
  860. if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
  861. CI->setCallingConv(F->getCallingConv());
  862. return CI;
  863. }
  864. Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B,
  865. const TargetLibraryInfo *TLI) {
  866. if (!TLI->has(LibFunc_putchar))
  867. return nullptr;
  868. Module *M = B.GetInsertBlock()->getModule();
  869. Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(), B.getInt32Ty());
  870. inferLibFuncAttributes(*M->getFunction("putchar"), *TLI);
  871. CallInst *CI = B.CreateCall(PutChar,
  872. B.CreateIntCast(Char,
  873. B.getInt32Ty(),
  874. /*isSigned*/true,
  875. "chari"),
  876. "putchar");
  877. if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
  878. CI->setCallingConv(F->getCallingConv());
  879. return CI;
  880. }
  881. Value *llvm::emitPutS(Value *Str, IRBuilder<> &B,
  882. const TargetLibraryInfo *TLI) {
  883. if (!TLI->has(LibFunc_puts))
  884. return nullptr;
  885. Module *M = B.GetInsertBlock()->getModule();
  886. Value *PutS =
  887. M->getOrInsertFunction("puts", B.getInt32Ty(), B.getInt8PtrTy());
  888. inferLibFuncAttributes(*M->getFunction("puts"), *TLI);
  889. CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), "puts");
  890. if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
  891. CI->setCallingConv(F->getCallingConv());
  892. return CI;
  893. }
  894. Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
  895. const TargetLibraryInfo *TLI) {
  896. if (!TLI->has(LibFunc_fputc))
  897. return nullptr;
  898. Module *M = B.GetInsertBlock()->getModule();
  899. Constant *F = M->getOrInsertFunction("fputc", B.getInt32Ty(), B.getInt32Ty(),
  900. File->getType());
  901. if (File->getType()->isPointerTy())
  902. inferLibFuncAttributes(*M->getFunction("fputc"), *TLI);
  903. Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
  904. "chari");
  905. CallInst *CI = B.CreateCall(F, {Char, File}, "fputc");
  906. if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
  907. CI->setCallingConv(Fn->getCallingConv());
  908. return CI;
  909. }
  910. Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
  911. const TargetLibraryInfo *TLI) {
  912. if (!TLI->has(LibFunc_fputs))
  913. return nullptr;
  914. Module *M = B.GetInsertBlock()->getModule();
  915. StringRef FPutsName = TLI->getName(LibFunc_fputs);
  916. Constant *F = M->getOrInsertFunction(
  917. FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType());
  918. if (File->getType()->isPointerTy())
  919. inferLibFuncAttributes(*M->getFunction(FPutsName), *TLI);
  920. CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs");
  921. if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
  922. CI->setCallingConv(Fn->getCallingConv());
  923. return CI;
  924. }
  925. Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
  926. const DataLayout &DL, const TargetLibraryInfo *TLI) {
  927. if (!TLI->has(LibFunc_fwrite))
  928. return nullptr;
  929. Module *M = B.GetInsertBlock()->getModule();
  930. LLVMContext &Context = B.GetInsertBlock()->getContext();
  931. StringRef FWriteName = TLI->getName(LibFunc_fwrite);
  932. Constant *F = M->getOrInsertFunction(
  933. FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
  934. DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
  935. if (File->getType()->isPointerTy())
  936. inferLibFuncAttributes(*M->getFunction(FWriteName), *TLI);
  937. CallInst *CI =
  938. B.CreateCall(F, {castToCStr(Ptr, B), Size,
  939. ConstantInt::get(DL.getIntPtrType(Context), 1), File});
  940. if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
  941. CI->setCallingConv(Fn->getCallingConv());
  942. return CI;
  943. }