BuildLibCalls.cpp 64 KB

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