SimplifyLibCalls.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  1. //===------ SimplifyLibCalls.cpp - Library calls simplifier ---------------===//
  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 is a utility pass used for testing the InstructionSimplify analysis.
  11. // The analysis is applied to every instruction, and if it simplifies then the
  12. // instruction is replaced by the simplification. If you are looking for a pass
  13. // that performs serious instruction folding, use the instcombine pass instead.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Transforms/Utils/SimplifyLibCalls.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/Analysis/ValueTracking.h"
  19. #include "llvm/DataLayout.h"
  20. #include "llvm/Function.h"
  21. #include "llvm/IRBuilder.h"
  22. #include "llvm/Intrinsics.h"
  23. #include "llvm/LLVMContext.h"
  24. #include "llvm/Module.h"
  25. #include "llvm/Target/TargetLibraryInfo.h"
  26. #include "llvm/Transforms/Utils/BuildLibCalls.h"
  27. using namespace llvm;
  28. /// This class is the abstract base class for the set of optimizations that
  29. /// corresponds to one library call.
  30. namespace {
  31. class LibCallOptimization {
  32. protected:
  33. Function *Caller;
  34. const DataLayout *TD;
  35. const TargetLibraryInfo *TLI;
  36. const LibCallSimplifier *LCS;
  37. LLVMContext* Context;
  38. public:
  39. LibCallOptimization() { }
  40. virtual ~LibCallOptimization() {}
  41. /// callOptimizer - This pure virtual method is implemented by base classes to
  42. /// do various optimizations. If this returns null then no transformation was
  43. /// performed. If it returns CI, then it transformed the call and CI is to be
  44. /// deleted. If it returns something else, replace CI with the new value and
  45. /// delete CI.
  46. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
  47. =0;
  48. Value *optimizeCall(CallInst *CI, const DataLayout *TD,
  49. const TargetLibraryInfo *TLI,
  50. const LibCallSimplifier *LCS, IRBuilder<> &B) {
  51. Caller = CI->getParent()->getParent();
  52. this->TD = TD;
  53. this->TLI = TLI;
  54. this->LCS = LCS;
  55. if (CI->getCalledFunction())
  56. Context = &CI->getCalledFunction()->getContext();
  57. // We never change the calling convention.
  58. if (CI->getCallingConv() != llvm::CallingConv::C)
  59. return NULL;
  60. return callOptimizer(CI->getCalledFunction(), CI, B);
  61. }
  62. };
  63. //===----------------------------------------------------------------------===//
  64. // Helper Functions
  65. //===----------------------------------------------------------------------===//
  66. /// isOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
  67. /// value is equal or not-equal to zero.
  68. static bool isOnlyUsedInZeroEqualityComparison(Value *V) {
  69. for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
  70. UI != E; ++UI) {
  71. if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
  72. if (IC->isEquality())
  73. if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
  74. if (C->isNullValue())
  75. continue;
  76. // Unknown instruction.
  77. return false;
  78. }
  79. return true;
  80. }
  81. /// isOnlyUsedInEqualityComparison - Return true if it is only used in equality
  82. /// comparisons with With.
  83. static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
  84. for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
  85. UI != E; ++UI) {
  86. if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
  87. if (IC->isEquality() && IC->getOperand(1) == With)
  88. continue;
  89. // Unknown instruction.
  90. return false;
  91. }
  92. return true;
  93. }
  94. static bool callHasFloatingPointArgument(const CallInst *CI) {
  95. for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
  96. it != e; ++it) {
  97. if ((*it)->getType()->isFloatingPointTy())
  98. return true;
  99. }
  100. return false;
  101. }
  102. //===----------------------------------------------------------------------===//
  103. // Fortified Library Call Optimizations
  104. //===----------------------------------------------------------------------===//
  105. struct FortifiedLibCallOptimization : public LibCallOptimization {
  106. protected:
  107. virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
  108. bool isString) const = 0;
  109. };
  110. struct InstFortifiedLibCallOptimization : public FortifiedLibCallOptimization {
  111. CallInst *CI;
  112. bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, bool isString) const {
  113. if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))
  114. return true;
  115. if (ConstantInt *SizeCI =
  116. dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {
  117. if (SizeCI->isAllOnesValue())
  118. return true;
  119. if (isString) {
  120. uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));
  121. // If the length is 0 we don't know how long it is and so we can't
  122. // remove the check.
  123. if (Len == 0) return false;
  124. return SizeCI->getZExtValue() >= Len;
  125. }
  126. if (ConstantInt *Arg = dyn_cast<ConstantInt>(
  127. CI->getArgOperand(SizeArgOp)))
  128. return SizeCI->getZExtValue() >= Arg->getZExtValue();
  129. }
  130. return false;
  131. }
  132. };
  133. struct MemCpyChkOpt : public InstFortifiedLibCallOptimization {
  134. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  135. this->CI = CI;
  136. FunctionType *FT = Callee->getFunctionType();
  137. LLVMContext &Context = CI->getParent()->getContext();
  138. // Check if this has the right signature.
  139. if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
  140. !FT->getParamType(0)->isPointerTy() ||
  141. !FT->getParamType(1)->isPointerTy() ||
  142. FT->getParamType(2) != TD->getIntPtrType(Context) ||
  143. FT->getParamType(3) != TD->getIntPtrType(Context))
  144. return 0;
  145. if (isFoldable(3, 2, false)) {
  146. B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
  147. CI->getArgOperand(2), 1);
  148. return CI->getArgOperand(0);
  149. }
  150. return 0;
  151. }
  152. };
  153. struct MemMoveChkOpt : public InstFortifiedLibCallOptimization {
  154. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  155. this->CI = CI;
  156. FunctionType *FT = Callee->getFunctionType();
  157. LLVMContext &Context = CI->getParent()->getContext();
  158. // Check if this has the right signature.
  159. if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
  160. !FT->getParamType(0)->isPointerTy() ||
  161. !FT->getParamType(1)->isPointerTy() ||
  162. FT->getParamType(2) != TD->getIntPtrType(Context) ||
  163. FT->getParamType(3) != TD->getIntPtrType(Context))
  164. return 0;
  165. if (isFoldable(3, 2, false)) {
  166. B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
  167. CI->getArgOperand(2), 1);
  168. return CI->getArgOperand(0);
  169. }
  170. return 0;
  171. }
  172. };
  173. struct MemSetChkOpt : public InstFortifiedLibCallOptimization {
  174. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  175. this->CI = CI;
  176. FunctionType *FT = Callee->getFunctionType();
  177. LLVMContext &Context = CI->getParent()->getContext();
  178. // Check if this has the right signature.
  179. if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
  180. !FT->getParamType(0)->isPointerTy() ||
  181. !FT->getParamType(1)->isIntegerTy() ||
  182. FT->getParamType(2) != TD->getIntPtrType(Context) ||
  183. FT->getParamType(3) != TD->getIntPtrType(Context))
  184. return 0;
  185. if (isFoldable(3, 2, false)) {
  186. Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
  187. false);
  188. B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
  189. return CI->getArgOperand(0);
  190. }
  191. return 0;
  192. }
  193. };
  194. struct StrCpyChkOpt : public InstFortifiedLibCallOptimization {
  195. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  196. this->CI = CI;
  197. StringRef Name = Callee->getName();
  198. FunctionType *FT = Callee->getFunctionType();
  199. LLVMContext &Context = CI->getParent()->getContext();
  200. // Check if this has the right signature.
  201. if (FT->getNumParams() != 3 ||
  202. FT->getReturnType() != FT->getParamType(0) ||
  203. FT->getParamType(0) != FT->getParamType(1) ||
  204. FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
  205. FT->getParamType(2) != TD->getIntPtrType(Context))
  206. return 0;
  207. Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
  208. if (Dst == Src) // __strcpy_chk(x,x) -> x
  209. return Src;
  210. // If a) we don't have any length information, or b) we know this will
  211. // fit then just lower to a plain strcpy. Otherwise we'll keep our
  212. // strcpy_chk call which may fail at runtime if the size is too long.
  213. // TODO: It might be nice to get a maximum length out of the possible
  214. // string lengths for varying.
  215. if (isFoldable(2, 1, true)) {
  216. Value *Ret = EmitStrCpy(Dst, Src, B, TD, TLI, Name.substr(2, 6));
  217. return Ret;
  218. } else {
  219. // Maybe we can stil fold __strcpy_chk to __memcpy_chk.
  220. uint64_t Len = GetStringLength(Src);
  221. if (Len == 0) return 0;
  222. // This optimization require DataLayout.
  223. if (!TD) return 0;
  224. Value *Ret =
  225. EmitMemCpyChk(Dst, Src,
  226. ConstantInt::get(TD->getIntPtrType(Context), Len),
  227. CI->getArgOperand(2), B, TD, TLI);
  228. return Ret;
  229. }
  230. return 0;
  231. }
  232. };
  233. struct StpCpyChkOpt : public InstFortifiedLibCallOptimization {
  234. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  235. this->CI = CI;
  236. StringRef Name = Callee->getName();
  237. FunctionType *FT = Callee->getFunctionType();
  238. LLVMContext &Context = CI->getParent()->getContext();
  239. // Check if this has the right signature.
  240. if (FT->getNumParams() != 3 ||
  241. FT->getReturnType() != FT->getParamType(0) ||
  242. FT->getParamType(0) != FT->getParamType(1) ||
  243. FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
  244. FT->getParamType(2) != TD->getIntPtrType(FT->getParamType(0)))
  245. return 0;
  246. Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
  247. if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
  248. Value *StrLen = EmitStrLen(Src, B, TD, TLI);
  249. return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
  250. }
  251. // If a) we don't have any length information, or b) we know this will
  252. // fit then just lower to a plain stpcpy. Otherwise we'll keep our
  253. // stpcpy_chk call which may fail at runtime if the size is too long.
  254. // TODO: It might be nice to get a maximum length out of the possible
  255. // string lengths for varying.
  256. if (isFoldable(2, 1, true)) {
  257. Value *Ret = EmitStrCpy(Dst, Src, B, TD, TLI, Name.substr(2, 6));
  258. return Ret;
  259. } else {
  260. // Maybe we can stil fold __stpcpy_chk to __memcpy_chk.
  261. uint64_t Len = GetStringLength(Src);
  262. if (Len == 0) return 0;
  263. // This optimization require DataLayout.
  264. if (!TD) return 0;
  265. Type *PT = FT->getParamType(0);
  266. Value *LenV = ConstantInt::get(TD->getIntPtrType(PT), Len);
  267. Value *DstEnd = B.CreateGEP(Dst,
  268. ConstantInt::get(TD->getIntPtrType(PT),
  269. Len - 1));
  270. if (!EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, TD, TLI))
  271. return 0;
  272. return DstEnd;
  273. }
  274. return 0;
  275. }
  276. };
  277. struct StrNCpyChkOpt : public InstFortifiedLibCallOptimization {
  278. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  279. this->CI = CI;
  280. StringRef Name = Callee->getName();
  281. FunctionType *FT = Callee->getFunctionType();
  282. LLVMContext &Context = CI->getParent()->getContext();
  283. // Check if this has the right signature.
  284. if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
  285. FT->getParamType(0) != FT->getParamType(1) ||
  286. FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
  287. !FT->getParamType(2)->isIntegerTy() ||
  288. FT->getParamType(3) != TD->getIntPtrType(Context))
  289. return 0;
  290. if (isFoldable(3, 2, false)) {
  291. Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
  292. CI->getArgOperand(2), B, TD, TLI,
  293. Name.substr(2, 7));
  294. return Ret;
  295. }
  296. return 0;
  297. }
  298. };
  299. //===----------------------------------------------------------------------===//
  300. // String and Memory Library Call Optimizations
  301. //===----------------------------------------------------------------------===//
  302. struct StrCatOpt : public LibCallOptimization {
  303. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  304. // Verify the "strcat" function prototype.
  305. FunctionType *FT = Callee->getFunctionType();
  306. if (FT->getNumParams() != 2 ||
  307. FT->getReturnType() != B.getInt8PtrTy() ||
  308. FT->getParamType(0) != FT->getReturnType() ||
  309. FT->getParamType(1) != FT->getReturnType())
  310. return 0;
  311. // Extract some information from the instruction
  312. Value *Dst = CI->getArgOperand(0);
  313. Value *Src = CI->getArgOperand(1);
  314. // See if we can get the length of the input string.
  315. uint64_t Len = GetStringLength(Src);
  316. if (Len == 0) return 0;
  317. --Len; // Unbias length.
  318. // Handle the simple, do-nothing case: strcat(x, "") -> x
  319. if (Len == 0)
  320. return Dst;
  321. // These optimizations require DataLayout.
  322. if (!TD) return 0;
  323. return emitStrLenMemCpy(Src, Dst, Len, B);
  324. }
  325. Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
  326. IRBuilder<> &B) {
  327. // We need to find the end of the destination string. That's where the
  328. // memory is to be moved to. We just generate a call to strlen.
  329. Value *DstLen = EmitStrLen(Dst, B, TD, TLI);
  330. if (!DstLen)
  331. return 0;
  332. // Now that we have the destination's length, we must index into the
  333. // destination's pointer to get the actual memcpy destination (end of
  334. // the string .. we're concatenating).
  335. Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
  336. // We have enough information to now generate the memcpy call to do the
  337. // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
  338. B.CreateMemCpy(CpyDst, Src,
  339. ConstantInt::get(TD->getIntPtrType(*Context), Len + 1), 1);
  340. return Dst;
  341. }
  342. };
  343. struct StrNCatOpt : public StrCatOpt {
  344. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  345. // Verify the "strncat" function prototype.
  346. FunctionType *FT = Callee->getFunctionType();
  347. if (FT->getNumParams() != 3 ||
  348. FT->getReturnType() != B.getInt8PtrTy() ||
  349. FT->getParamType(0) != FT->getReturnType() ||
  350. FT->getParamType(1) != FT->getReturnType() ||
  351. !FT->getParamType(2)->isIntegerTy())
  352. return 0;
  353. // Extract some information from the instruction
  354. Value *Dst = CI->getArgOperand(0);
  355. Value *Src = CI->getArgOperand(1);
  356. uint64_t Len;
  357. // We don't do anything if length is not constant
  358. if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
  359. Len = LengthArg->getZExtValue();
  360. else
  361. return 0;
  362. // See if we can get the length of the input string.
  363. uint64_t SrcLen = GetStringLength(Src);
  364. if (SrcLen == 0) return 0;
  365. --SrcLen; // Unbias length.
  366. // Handle the simple, do-nothing cases:
  367. // strncat(x, "", c) -> x
  368. // strncat(x, c, 0) -> x
  369. if (SrcLen == 0 || Len == 0) return Dst;
  370. // These optimizations require DataLayout.
  371. if (!TD) return 0;
  372. // We don't optimize this case
  373. if (Len < SrcLen) return 0;
  374. // strncat(x, s, c) -> strcat(x, s)
  375. // s is constant so the strcat can be optimized further
  376. return emitStrLenMemCpy(Src, Dst, SrcLen, B);
  377. }
  378. };
  379. struct StrChrOpt : public LibCallOptimization {
  380. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  381. // Verify the "strchr" function prototype.
  382. FunctionType *FT = Callee->getFunctionType();
  383. if (FT->getNumParams() != 2 ||
  384. FT->getReturnType() != B.getInt8PtrTy() ||
  385. FT->getParamType(0) != FT->getReturnType() ||
  386. !FT->getParamType(1)->isIntegerTy(32))
  387. return 0;
  388. Value *SrcStr = CI->getArgOperand(0);
  389. // If the second operand is non-constant, see if we can compute the length
  390. // of the input string and turn this into memchr.
  391. ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  392. if (CharC == 0) {
  393. // These optimizations require DataLayout.
  394. if (!TD) return 0;
  395. uint64_t Len = GetStringLength(SrcStr);
  396. if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
  397. return 0;
  398. return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
  399. ConstantInt::get(TD->getIntPtrType(*Context), Len),
  400. B, TD, TLI);
  401. }
  402. // Otherwise, the character is a constant, see if the first argument is
  403. // a string literal. If so, we can constant fold.
  404. StringRef Str;
  405. if (!getConstantStringInfo(SrcStr, Str))
  406. return 0;
  407. // Compute the offset, make sure to handle the case when we're searching for
  408. // zero (a weird way to spell strlen).
  409. size_t I = CharC->getSExtValue() == 0 ?
  410. Str.size() : Str.find(CharC->getSExtValue());
  411. if (I == StringRef::npos) // Didn't find the char. strchr returns null.
  412. return Constant::getNullValue(CI->getType());
  413. // strchr(s+n,c) -> gep(s+n+i,c)
  414. return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");
  415. }
  416. };
  417. struct StrRChrOpt : public LibCallOptimization {
  418. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  419. // Verify the "strrchr" function prototype.
  420. FunctionType *FT = Callee->getFunctionType();
  421. if (FT->getNumParams() != 2 ||
  422. FT->getReturnType() != B.getInt8PtrTy() ||
  423. FT->getParamType(0) != FT->getReturnType() ||
  424. !FT->getParamType(1)->isIntegerTy(32))
  425. return 0;
  426. Value *SrcStr = CI->getArgOperand(0);
  427. ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  428. // Cannot fold anything if we're not looking for a constant.
  429. if (!CharC)
  430. return 0;
  431. StringRef Str;
  432. if (!getConstantStringInfo(SrcStr, Str)) {
  433. // strrchr(s, 0) -> strchr(s, 0)
  434. if (TD && CharC->isZero())
  435. return EmitStrChr(SrcStr, '\0', B, TD, TLI);
  436. return 0;
  437. }
  438. // Compute the offset.
  439. size_t I = CharC->getSExtValue() == 0 ?
  440. Str.size() : Str.rfind(CharC->getSExtValue());
  441. if (I == StringRef::npos) // Didn't find the char. Return null.
  442. return Constant::getNullValue(CI->getType());
  443. // strrchr(s+n,c) -> gep(s+n+i,c)
  444. return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");
  445. }
  446. };
  447. struct StrCmpOpt : public LibCallOptimization {
  448. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  449. // Verify the "strcmp" function prototype.
  450. FunctionType *FT = Callee->getFunctionType();
  451. if (FT->getNumParams() != 2 ||
  452. !FT->getReturnType()->isIntegerTy(32) ||
  453. FT->getParamType(0) != FT->getParamType(1) ||
  454. FT->getParamType(0) != B.getInt8PtrTy())
  455. return 0;
  456. Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
  457. if (Str1P == Str2P) // strcmp(x,x) -> 0
  458. return ConstantInt::get(CI->getType(), 0);
  459. StringRef Str1, Str2;
  460. bool HasStr1 = getConstantStringInfo(Str1P, Str1);
  461. bool HasStr2 = getConstantStringInfo(Str2P, Str2);
  462. // strcmp(x, y) -> cnst (if both x and y are constant strings)
  463. if (HasStr1 && HasStr2)
  464. return ConstantInt::get(CI->getType(), Str1.compare(Str2));
  465. if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
  466. return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
  467. CI->getType()));
  468. if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
  469. return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
  470. // strcmp(P, "x") -> memcmp(P, "x", 2)
  471. uint64_t Len1 = GetStringLength(Str1P);
  472. uint64_t Len2 = GetStringLength(Str2P);
  473. if (Len1 && Len2) {
  474. // These optimizations require DataLayout.
  475. if (!TD) return 0;
  476. return EmitMemCmp(Str1P, Str2P,
  477. ConstantInt::get(TD->getIntPtrType(*Context),
  478. std::min(Len1, Len2)), B, TD, TLI);
  479. }
  480. return 0;
  481. }
  482. };
  483. struct StrNCmpOpt : public LibCallOptimization {
  484. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  485. // Verify the "strncmp" function prototype.
  486. FunctionType *FT = Callee->getFunctionType();
  487. if (FT->getNumParams() != 3 ||
  488. !FT->getReturnType()->isIntegerTy(32) ||
  489. FT->getParamType(0) != FT->getParamType(1) ||
  490. FT->getParamType(0) != B.getInt8PtrTy() ||
  491. !FT->getParamType(2)->isIntegerTy())
  492. return 0;
  493. Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
  494. if (Str1P == Str2P) // strncmp(x,x,n) -> 0
  495. return ConstantInt::get(CI->getType(), 0);
  496. // Get the length argument if it is constant.
  497. uint64_t Length;
  498. if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
  499. Length = LengthArg->getZExtValue();
  500. else
  501. return 0;
  502. if (Length == 0) // strncmp(x,y,0) -> 0
  503. return ConstantInt::get(CI->getType(), 0);
  504. if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
  505. return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD, TLI);
  506. StringRef Str1, Str2;
  507. bool HasStr1 = getConstantStringInfo(Str1P, Str1);
  508. bool HasStr2 = getConstantStringInfo(Str2P, Str2);
  509. // strncmp(x, y) -> cnst (if both x and y are constant strings)
  510. if (HasStr1 && HasStr2) {
  511. StringRef SubStr1 = Str1.substr(0, Length);
  512. StringRef SubStr2 = Str2.substr(0, Length);
  513. return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
  514. }
  515. if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
  516. return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
  517. CI->getType()));
  518. if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
  519. return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
  520. return 0;
  521. }
  522. };
  523. struct StrCpyOpt : public LibCallOptimization {
  524. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  525. // Verify the "strcpy" function prototype.
  526. FunctionType *FT = Callee->getFunctionType();
  527. if (FT->getNumParams() != 2 ||
  528. FT->getReturnType() != FT->getParamType(0) ||
  529. FT->getParamType(0) != FT->getParamType(1) ||
  530. FT->getParamType(0) != B.getInt8PtrTy())
  531. return 0;
  532. Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
  533. if (Dst == Src) // strcpy(x,x) -> x
  534. return Src;
  535. // These optimizations require DataLayout.
  536. if (!TD) return 0;
  537. // See if we can get the length of the input string.
  538. uint64_t Len = GetStringLength(Src);
  539. if (Len == 0) return 0;
  540. // We have enough information to now generate the memcpy call to do the
  541. // copy for us. Make a memcpy to copy the nul byte with align = 1.
  542. B.CreateMemCpy(Dst, Src,
  543. ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
  544. return Dst;
  545. }
  546. };
  547. struct StpCpyOpt: public LibCallOptimization {
  548. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  549. // Verify the "stpcpy" function prototype.
  550. FunctionType *FT = Callee->getFunctionType();
  551. if (FT->getNumParams() != 2 ||
  552. FT->getReturnType() != FT->getParamType(0) ||
  553. FT->getParamType(0) != FT->getParamType(1) ||
  554. FT->getParamType(0) != B.getInt8PtrTy())
  555. return 0;
  556. // These optimizations require DataLayout.
  557. if (!TD) return 0;
  558. Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
  559. if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
  560. Value *StrLen = EmitStrLen(Src, B, TD, TLI);
  561. return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
  562. }
  563. // See if we can get the length of the input string.
  564. uint64_t Len = GetStringLength(Src);
  565. if (Len == 0) return 0;
  566. Type *PT = FT->getParamType(0);
  567. Value *LenV = ConstantInt::get(TD->getIntPtrType(PT), Len);
  568. Value *DstEnd = B.CreateGEP(Dst,
  569. ConstantInt::get(TD->getIntPtrType(PT),
  570. Len - 1));
  571. // We have enough information to now generate the memcpy call to do the
  572. // copy for us. Make a memcpy to copy the nul byte with align = 1.
  573. B.CreateMemCpy(Dst, Src, LenV, 1);
  574. return DstEnd;
  575. }
  576. };
  577. struct StrNCpyOpt : public LibCallOptimization {
  578. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  579. FunctionType *FT = Callee->getFunctionType();
  580. if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
  581. FT->getParamType(0) != FT->getParamType(1) ||
  582. FT->getParamType(0) != B.getInt8PtrTy() ||
  583. !FT->getParamType(2)->isIntegerTy())
  584. return 0;
  585. Value *Dst = CI->getArgOperand(0);
  586. Value *Src = CI->getArgOperand(1);
  587. Value *LenOp = CI->getArgOperand(2);
  588. // See if we can get the length of the input string.
  589. uint64_t SrcLen = GetStringLength(Src);
  590. if (SrcLen == 0) return 0;
  591. --SrcLen;
  592. if (SrcLen == 0) {
  593. // strncpy(x, "", y) -> memset(x, '\0', y, 1)
  594. B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
  595. return Dst;
  596. }
  597. uint64_t Len;
  598. if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
  599. Len = LengthArg->getZExtValue();
  600. else
  601. return 0;
  602. if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
  603. // These optimizations require DataLayout.
  604. if (!TD) return 0;
  605. // Let strncpy handle the zero padding
  606. if (Len > SrcLen+1) return 0;
  607. Type *PT = FT->getParamType(0);
  608. // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
  609. B.CreateMemCpy(Dst, Src,
  610. ConstantInt::get(TD->getIntPtrType(PT), Len), 1);
  611. return Dst;
  612. }
  613. };
  614. struct StrLenOpt : public LibCallOptimization {
  615. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  616. FunctionType *FT = Callee->getFunctionType();
  617. if (FT->getNumParams() != 1 ||
  618. FT->getParamType(0) != B.getInt8PtrTy() ||
  619. !FT->getReturnType()->isIntegerTy())
  620. return 0;
  621. Value *Src = CI->getArgOperand(0);
  622. // Constant folding: strlen("xyz") -> 3
  623. if (uint64_t Len = GetStringLength(Src))
  624. return ConstantInt::get(CI->getType(), Len-1);
  625. // strlen(x) != 0 --> *x != 0
  626. // strlen(x) == 0 --> *x == 0
  627. if (isOnlyUsedInZeroEqualityComparison(CI))
  628. return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
  629. return 0;
  630. }
  631. };
  632. struct StrPBrkOpt : public LibCallOptimization {
  633. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  634. FunctionType *FT = Callee->getFunctionType();
  635. if (FT->getNumParams() != 2 ||
  636. FT->getParamType(0) != B.getInt8PtrTy() ||
  637. FT->getParamType(1) != FT->getParamType(0) ||
  638. FT->getReturnType() != FT->getParamType(0))
  639. return 0;
  640. StringRef S1, S2;
  641. bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
  642. bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
  643. // strpbrk(s, "") -> NULL
  644. // strpbrk("", s) -> NULL
  645. if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
  646. return Constant::getNullValue(CI->getType());
  647. // Constant folding.
  648. if (HasS1 && HasS2) {
  649. size_t I = S1.find_first_of(S2);
  650. if (I == std::string::npos) // No match.
  651. return Constant::getNullValue(CI->getType());
  652. return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");
  653. }
  654. // strpbrk(s, "a") -> strchr(s, 'a')
  655. if (TD && HasS2 && S2.size() == 1)
  656. return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD, TLI);
  657. return 0;
  658. }
  659. };
  660. struct StrToOpt : public LibCallOptimization {
  661. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  662. FunctionType *FT = Callee->getFunctionType();
  663. if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
  664. !FT->getParamType(0)->isPointerTy() ||
  665. !FT->getParamType(1)->isPointerTy())
  666. return 0;
  667. Value *EndPtr = CI->getArgOperand(1);
  668. if (isa<ConstantPointerNull>(EndPtr)) {
  669. // With a null EndPtr, this function won't capture the main argument.
  670. // It would be readonly too, except that it still may write to errno.
  671. CI->addAttribute(1, Attribute::get(Callee->getContext(),
  672. Attribute::NoCapture));
  673. }
  674. return 0;
  675. }
  676. };
  677. struct StrSpnOpt : public LibCallOptimization {
  678. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  679. FunctionType *FT = Callee->getFunctionType();
  680. if (FT->getNumParams() != 2 ||
  681. FT->getParamType(0) != B.getInt8PtrTy() ||
  682. FT->getParamType(1) != FT->getParamType(0) ||
  683. !FT->getReturnType()->isIntegerTy())
  684. return 0;
  685. StringRef S1, S2;
  686. bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
  687. bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
  688. // strspn(s, "") -> 0
  689. // strspn("", s) -> 0
  690. if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
  691. return Constant::getNullValue(CI->getType());
  692. // Constant folding.
  693. if (HasS1 && HasS2) {
  694. size_t Pos = S1.find_first_not_of(S2);
  695. if (Pos == StringRef::npos) Pos = S1.size();
  696. return ConstantInt::get(CI->getType(), Pos);
  697. }
  698. return 0;
  699. }
  700. };
  701. struct StrCSpnOpt : public LibCallOptimization {
  702. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  703. FunctionType *FT = Callee->getFunctionType();
  704. if (FT->getNumParams() != 2 ||
  705. FT->getParamType(0) != B.getInt8PtrTy() ||
  706. FT->getParamType(1) != FT->getParamType(0) ||
  707. !FT->getReturnType()->isIntegerTy())
  708. return 0;
  709. StringRef S1, S2;
  710. bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
  711. bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
  712. // strcspn("", s) -> 0
  713. if (HasS1 && S1.empty())
  714. return Constant::getNullValue(CI->getType());
  715. // Constant folding.
  716. if (HasS1 && HasS2) {
  717. size_t Pos = S1.find_first_of(S2);
  718. if (Pos == StringRef::npos) Pos = S1.size();
  719. return ConstantInt::get(CI->getType(), Pos);
  720. }
  721. // strcspn(s, "") -> strlen(s)
  722. if (TD && HasS2 && S2.empty())
  723. return EmitStrLen(CI->getArgOperand(0), B, TD, TLI);
  724. return 0;
  725. }
  726. };
  727. struct StrStrOpt : public LibCallOptimization {
  728. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  729. FunctionType *FT = Callee->getFunctionType();
  730. if (FT->getNumParams() != 2 ||
  731. !FT->getParamType(0)->isPointerTy() ||
  732. !FT->getParamType(1)->isPointerTy() ||
  733. !FT->getReturnType()->isPointerTy())
  734. return 0;
  735. // fold strstr(x, x) -> x.
  736. if (CI->getArgOperand(0) == CI->getArgOperand(1))
  737. return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
  738. // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
  739. if (TD && isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
  740. Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD, TLI);
  741. if (!StrLen)
  742. return 0;
  743. Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
  744. StrLen, B, TD, TLI);
  745. if (!StrNCmp)
  746. return 0;
  747. for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
  748. UI != UE; ) {
  749. ICmpInst *Old = cast<ICmpInst>(*UI++);
  750. Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
  751. ConstantInt::getNullValue(StrNCmp->getType()),
  752. "cmp");
  753. LCS->replaceAllUsesWith(Old, Cmp);
  754. }
  755. return CI;
  756. }
  757. // See if either input string is a constant string.
  758. StringRef SearchStr, ToFindStr;
  759. bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
  760. bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
  761. // fold strstr(x, "") -> x.
  762. if (HasStr2 && ToFindStr.empty())
  763. return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
  764. // If both strings are known, constant fold it.
  765. if (HasStr1 && HasStr2) {
  766. std::string::size_type Offset = SearchStr.find(ToFindStr);
  767. if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
  768. return Constant::getNullValue(CI->getType());
  769. // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
  770. Value *Result = CastToCStr(CI->getArgOperand(0), B);
  771. Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
  772. return B.CreateBitCast(Result, CI->getType());
  773. }
  774. // fold strstr(x, "y") -> strchr(x, 'y').
  775. if (HasStr2 && ToFindStr.size() == 1) {
  776. Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TD, TLI);
  777. return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : 0;
  778. }
  779. return 0;
  780. }
  781. };
  782. struct MemCmpOpt : public LibCallOptimization {
  783. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  784. FunctionType *FT = Callee->getFunctionType();
  785. if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
  786. !FT->getParamType(1)->isPointerTy() ||
  787. !FT->getReturnType()->isIntegerTy(32))
  788. return 0;
  789. Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
  790. if (LHS == RHS) // memcmp(s,s,x) -> 0
  791. return Constant::getNullValue(CI->getType());
  792. // Make sure we have a constant length.
  793. ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
  794. if (!LenC) return 0;
  795. uint64_t Len = LenC->getZExtValue();
  796. if (Len == 0) // memcmp(s1,s2,0) -> 0
  797. return Constant::getNullValue(CI->getType());
  798. // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
  799. if (Len == 1) {
  800. Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
  801. CI->getType(), "lhsv");
  802. Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
  803. CI->getType(), "rhsv");
  804. return B.CreateSub(LHSV, RHSV, "chardiff");
  805. }
  806. // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
  807. StringRef LHSStr, RHSStr;
  808. if (getConstantStringInfo(LHS, LHSStr) &&
  809. getConstantStringInfo(RHS, RHSStr)) {
  810. // Make sure we're not reading out-of-bounds memory.
  811. if (Len > LHSStr.size() || Len > RHSStr.size())
  812. return 0;
  813. // Fold the memcmp and normalize the result. This way we get consistent
  814. // results across multiple platforms.
  815. uint64_t Ret = 0;
  816. int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);
  817. if (Cmp < 0)
  818. Ret = -1;
  819. else if (Cmp > 0)
  820. Ret = 1;
  821. return ConstantInt::get(CI->getType(), Ret);
  822. }
  823. return 0;
  824. }
  825. };
  826. struct MemCpyOpt : public LibCallOptimization {
  827. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  828. // These optimizations require DataLayout.
  829. if (!TD) return 0;
  830. FunctionType *FT = Callee->getFunctionType();
  831. if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
  832. !FT->getParamType(0)->isPointerTy() ||
  833. !FT->getParamType(1)->isPointerTy() ||
  834. FT->getParamType(2) != TD->getIntPtrType(*Context))
  835. return 0;
  836. // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
  837. B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
  838. CI->getArgOperand(2), 1);
  839. return CI->getArgOperand(0);
  840. }
  841. };
  842. struct MemMoveOpt : public LibCallOptimization {
  843. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  844. // These optimizations require DataLayout.
  845. if (!TD) return 0;
  846. FunctionType *FT = Callee->getFunctionType();
  847. if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
  848. !FT->getParamType(0)->isPointerTy() ||
  849. !FT->getParamType(1)->isPointerTy() ||
  850. FT->getParamType(2) != TD->getIntPtrType(*Context))
  851. return 0;
  852. // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
  853. B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
  854. CI->getArgOperand(2), 1);
  855. return CI->getArgOperand(0);
  856. }
  857. };
  858. struct MemSetOpt : public LibCallOptimization {
  859. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  860. // These optimizations require DataLayout.
  861. if (!TD) return 0;
  862. FunctionType *FT = Callee->getFunctionType();
  863. if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
  864. !FT->getParamType(0)->isPointerTy() ||
  865. !FT->getParamType(1)->isIntegerTy() ||
  866. FT->getParamType(2) != TD->getIntPtrType(*Context))
  867. return 0;
  868. // memset(p, v, n) -> llvm.memset(p, v, n, 1)
  869. Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
  870. B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
  871. return CI->getArgOperand(0);
  872. }
  873. };
  874. //===----------------------------------------------------------------------===//
  875. // Math Library Optimizations
  876. //===----------------------------------------------------------------------===//
  877. //===----------------------------------------------------------------------===//
  878. // Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
  879. struct UnaryDoubleFPOpt : public LibCallOptimization {
  880. bool CheckRetType;
  881. UnaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {}
  882. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  883. FunctionType *FT = Callee->getFunctionType();
  884. if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
  885. !FT->getParamType(0)->isDoubleTy())
  886. return 0;
  887. if (CheckRetType) {
  888. // Check if all the uses for function like 'sin' are converted to float.
  889. for (Value::use_iterator UseI = CI->use_begin(); UseI != CI->use_end();
  890. ++UseI) {
  891. FPTruncInst *Cast = dyn_cast<FPTruncInst>(*UseI);
  892. if (Cast == 0 || !Cast->getType()->isFloatTy())
  893. return 0;
  894. }
  895. }
  896. // If this is something like 'floor((double)floatval)', convert to floorf.
  897. FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
  898. if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
  899. return 0;
  900. // floor((double)floatval) -> (double)floorf(floatval)
  901. Value *V = Cast->getOperand(0);
  902. V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
  903. return B.CreateFPExt(V, B.getDoubleTy());
  904. }
  905. };
  906. struct UnsafeFPLibCallOptimization : public LibCallOptimization {
  907. bool UnsafeFPShrink;
  908. UnsafeFPLibCallOptimization(bool UnsafeFPShrink) {
  909. this->UnsafeFPShrink = UnsafeFPShrink;
  910. }
  911. };
  912. struct CosOpt : public UnsafeFPLibCallOptimization {
  913. CosOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
  914. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  915. Value *Ret = NULL;
  916. if (UnsafeFPShrink && Callee->getName() == "cos" &&
  917. TLI->has(LibFunc::cosf)) {
  918. UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
  919. Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
  920. }
  921. FunctionType *FT = Callee->getFunctionType();
  922. // Just make sure this has 1 argument of FP type, which matches the
  923. // result type.
  924. if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
  925. !FT->getParamType(0)->isFloatingPointTy())
  926. return Ret;
  927. // cos(-x) -> cos(x)
  928. Value *Op1 = CI->getArgOperand(0);
  929. if (BinaryOperator::isFNeg(Op1)) {
  930. BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);
  931. return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");
  932. }
  933. return Ret;
  934. }
  935. };
  936. struct PowOpt : public UnsafeFPLibCallOptimization {
  937. PowOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
  938. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  939. Value *Ret = NULL;
  940. if (UnsafeFPShrink && Callee->getName() == "pow" &&
  941. TLI->has(LibFunc::powf)) {
  942. UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
  943. Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
  944. }
  945. FunctionType *FT = Callee->getFunctionType();
  946. // Just make sure this has 2 arguments of the same FP type, which match the
  947. // result type.
  948. if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
  949. FT->getParamType(0) != FT->getParamType(1) ||
  950. !FT->getParamType(0)->isFloatingPointTy())
  951. return Ret;
  952. Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
  953. if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
  954. if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
  955. return Op1C;
  956. if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
  957. return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
  958. }
  959. ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
  960. if (Op2C == 0) return Ret;
  961. if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
  962. return ConstantFP::get(CI->getType(), 1.0);
  963. if (Op2C->isExactlyValue(0.5)) {
  964. // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
  965. // This is faster than calling pow, and still handles negative zero
  966. // and negative infinity correctly.
  967. // TODO: In fast-math mode, this could be just sqrt(x).
  968. // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
  969. Value *Inf = ConstantFP::getInfinity(CI->getType());
  970. Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
  971. Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
  972. Callee->getAttributes());
  973. Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
  974. Callee->getAttributes());
  975. Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
  976. Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
  977. return Sel;
  978. }
  979. if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
  980. return Op1;
  981. if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
  982. return B.CreateFMul(Op1, Op1, "pow2");
  983. if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
  984. return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
  985. Op1, "powrecip");
  986. return 0;
  987. }
  988. };
  989. struct Exp2Opt : public UnsafeFPLibCallOptimization {
  990. Exp2Opt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
  991. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  992. Value *Ret = NULL;
  993. if (UnsafeFPShrink && Callee->getName() == "exp2" &&
  994. TLI->has(LibFunc::exp2)) {
  995. UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
  996. Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
  997. }
  998. FunctionType *FT = Callee->getFunctionType();
  999. // Just make sure this has 1 argument of FP type, which matches the
  1000. // result type.
  1001. if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
  1002. !FT->getParamType(0)->isFloatingPointTy())
  1003. return Ret;
  1004. Value *Op = CI->getArgOperand(0);
  1005. // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
  1006. // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
  1007. Value *LdExpArg = 0;
  1008. if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
  1009. if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
  1010. LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
  1011. } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
  1012. if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
  1013. LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
  1014. }
  1015. if (LdExpArg) {
  1016. const char *Name;
  1017. if (Op->getType()->isFloatTy())
  1018. Name = "ldexpf";
  1019. else if (Op->getType()->isDoubleTy())
  1020. Name = "ldexp";
  1021. else
  1022. Name = "ldexpl";
  1023. Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
  1024. if (!Op->getType()->isFloatTy())
  1025. One = ConstantExpr::getFPExtend(One, Op->getType());
  1026. Module *M = Caller->getParent();
  1027. Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
  1028. Op->getType(),
  1029. B.getInt32Ty(), NULL);
  1030. CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
  1031. if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
  1032. CI->setCallingConv(F->getCallingConv());
  1033. return CI;
  1034. }
  1035. return Ret;
  1036. }
  1037. };
  1038. //===----------------------------------------------------------------------===//
  1039. // Integer Library Call Optimizations
  1040. //===----------------------------------------------------------------------===//
  1041. struct FFSOpt : public LibCallOptimization {
  1042. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  1043. FunctionType *FT = Callee->getFunctionType();
  1044. // Just make sure this has 2 arguments of the same FP type, which match the
  1045. // result type.
  1046. if (FT->getNumParams() != 1 ||
  1047. !FT->getReturnType()->isIntegerTy(32) ||
  1048. !FT->getParamType(0)->isIntegerTy())
  1049. return 0;
  1050. Value *Op = CI->getArgOperand(0);
  1051. // Constant fold.
  1052. if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
  1053. if (CI->isZero()) // ffs(0) -> 0.
  1054. return B.getInt32(0);
  1055. // ffs(c) -> cttz(c)+1
  1056. return B.getInt32(CI->getValue().countTrailingZeros() + 1);
  1057. }
  1058. // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
  1059. Type *ArgType = Op->getType();
  1060. Value *F = Intrinsic::getDeclaration(Callee->getParent(),
  1061. Intrinsic::cttz, ArgType);
  1062. Value *V = B.CreateCall2(F, Op, B.getFalse(), "cttz");
  1063. V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
  1064. V = B.CreateIntCast(V, B.getInt32Ty(), false);
  1065. Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
  1066. return B.CreateSelect(Cond, V, B.getInt32(0));
  1067. }
  1068. };
  1069. struct AbsOpt : public LibCallOptimization {
  1070. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  1071. FunctionType *FT = Callee->getFunctionType();
  1072. // We require integer(integer) where the types agree.
  1073. if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
  1074. FT->getParamType(0) != FT->getReturnType())
  1075. return 0;
  1076. // abs(x) -> x >s -1 ? x : -x
  1077. Value *Op = CI->getArgOperand(0);
  1078. Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),
  1079. "ispos");
  1080. Value *Neg = B.CreateNeg(Op, "neg");
  1081. return B.CreateSelect(Pos, Op, Neg);
  1082. }
  1083. };
  1084. struct IsDigitOpt : public LibCallOptimization {
  1085. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  1086. FunctionType *FT = Callee->getFunctionType();
  1087. // We require integer(i32)
  1088. if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
  1089. !FT->getParamType(0)->isIntegerTy(32))
  1090. return 0;
  1091. // isdigit(c) -> (c-'0') <u 10
  1092. Value *Op = CI->getArgOperand(0);
  1093. Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
  1094. Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
  1095. return B.CreateZExt(Op, CI->getType());
  1096. }
  1097. };
  1098. struct IsAsciiOpt : public LibCallOptimization {
  1099. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  1100. FunctionType *FT = Callee->getFunctionType();
  1101. // We require integer(i32)
  1102. if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
  1103. !FT->getParamType(0)->isIntegerTy(32))
  1104. return 0;
  1105. // isascii(c) -> c <u 128
  1106. Value *Op = CI->getArgOperand(0);
  1107. Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
  1108. return B.CreateZExt(Op, CI->getType());
  1109. }
  1110. };
  1111. struct ToAsciiOpt : public LibCallOptimization {
  1112. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  1113. FunctionType *FT = Callee->getFunctionType();
  1114. // We require i32(i32)
  1115. if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
  1116. !FT->getParamType(0)->isIntegerTy(32))
  1117. return 0;
  1118. // toascii(c) -> c & 0x7f
  1119. return B.CreateAnd(CI->getArgOperand(0),
  1120. ConstantInt::get(CI->getType(),0x7F));
  1121. }
  1122. };
  1123. //===----------------------------------------------------------------------===//
  1124. // Formatting and IO Library Call Optimizations
  1125. //===----------------------------------------------------------------------===//
  1126. struct PrintFOpt : public LibCallOptimization {
  1127. Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,
  1128. IRBuilder<> &B) {
  1129. // Check for a fixed format string.
  1130. StringRef FormatStr;
  1131. if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
  1132. return 0;
  1133. // Empty format string -> noop.
  1134. if (FormatStr.empty()) // Tolerate printf's declared void.
  1135. return CI->use_empty() ? (Value*)CI :
  1136. ConstantInt::get(CI->getType(), 0);
  1137. // Do not do any of the following transformations if the printf return value
  1138. // is used, in general the printf return value is not compatible with either
  1139. // putchar() or puts().
  1140. if (!CI->use_empty())
  1141. return 0;
  1142. // printf("x") -> putchar('x'), even for '%'.
  1143. if (FormatStr.size() == 1) {
  1144. Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI);
  1145. if (CI->use_empty() || !Res) return Res;
  1146. return B.CreateIntCast(Res, CI->getType(), true);
  1147. }
  1148. // printf("foo\n") --> puts("foo")
  1149. if (FormatStr[FormatStr.size()-1] == '\n' &&
  1150. FormatStr.find('%') == std::string::npos) { // no format characters.
  1151. // Create a string literal with no \n on it. We expect the constant merge
  1152. // pass to be run after this pass, to merge duplicate strings.
  1153. FormatStr = FormatStr.drop_back();
  1154. Value *GV = B.CreateGlobalString(FormatStr, "str");
  1155. Value *NewCI = EmitPutS(GV, B, TD, TLI);
  1156. return (CI->use_empty() || !NewCI) ?
  1157. NewCI :
  1158. ConstantInt::get(CI->getType(), FormatStr.size()+1);
  1159. }
  1160. // Optimize specific format strings.
  1161. // printf("%c", chr) --> putchar(chr)
  1162. if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
  1163. CI->getArgOperand(1)->getType()->isIntegerTy()) {
  1164. Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI);
  1165. if (CI->use_empty() || !Res) return Res;
  1166. return B.CreateIntCast(Res, CI->getType(), true);
  1167. }
  1168. // printf("%s\n", str) --> puts(str)
  1169. if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
  1170. CI->getArgOperand(1)->getType()->isPointerTy()) {
  1171. return EmitPutS(CI->getArgOperand(1), B, TD, TLI);
  1172. }
  1173. return 0;
  1174. }
  1175. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  1176. // Require one fixed pointer argument and an integer/void result.
  1177. FunctionType *FT = Callee->getFunctionType();
  1178. if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
  1179. !(FT->getReturnType()->isIntegerTy() ||
  1180. FT->getReturnType()->isVoidTy()))
  1181. return 0;
  1182. if (Value *V = optimizeFixedFormatString(Callee, CI, B)) {
  1183. return V;
  1184. }
  1185. // printf(format, ...) -> iprintf(format, ...) if no floating point
  1186. // arguments.
  1187. if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) {
  1188. Module *M = B.GetInsertBlock()->getParent()->getParent();
  1189. Constant *IPrintFFn =
  1190. M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
  1191. CallInst *New = cast<CallInst>(CI->clone());
  1192. New->setCalledFunction(IPrintFFn);
  1193. B.Insert(New);
  1194. return New;
  1195. }
  1196. return 0;
  1197. }
  1198. };
  1199. struct SPrintFOpt : public LibCallOptimization {
  1200. Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
  1201. IRBuilder<> &B) {
  1202. // Check for a fixed format string.
  1203. StringRef FormatStr;
  1204. if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
  1205. return 0;
  1206. // If we just have a format string (nothing else crazy) transform it.
  1207. if (CI->getNumArgOperands() == 2) {
  1208. // Make sure there's no % in the constant array. We could try to handle
  1209. // %% -> % in the future if we cared.
  1210. for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
  1211. if (FormatStr[i] == '%')
  1212. return 0; // we found a format specifier, bail out.
  1213. // These optimizations require DataLayout.
  1214. if (!TD) return 0;
  1215. // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
  1216. B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
  1217. ConstantInt::get(TD->getIntPtrType(*Context), // Copy the
  1218. FormatStr.size() + 1), 1); // nul byte.
  1219. return ConstantInt::get(CI->getType(), FormatStr.size());
  1220. }
  1221. // The remaining optimizations require the format string to be "%s" or "%c"
  1222. // and have an extra operand.
  1223. if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
  1224. CI->getNumArgOperands() < 3)
  1225. return 0;
  1226. // Decode the second character of the format string.
  1227. if (FormatStr[1] == 'c') {
  1228. // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
  1229. if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
  1230. Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
  1231. Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
  1232. B.CreateStore(V, Ptr);
  1233. Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
  1234. B.CreateStore(B.getInt8(0), Ptr);
  1235. return ConstantInt::get(CI->getType(), 1);
  1236. }
  1237. if (FormatStr[1] == 's') {
  1238. // These optimizations require DataLayout.
  1239. if (!TD) return 0;
  1240. // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
  1241. if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
  1242. Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD, TLI);
  1243. if (!Len)
  1244. return 0;
  1245. Value *IncLen = B.CreateAdd(Len,
  1246. ConstantInt::get(Len->getType(), 1),
  1247. "leninc");
  1248. B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
  1249. // The sprintf result is the unincremented number of bytes in the string.
  1250. return B.CreateIntCast(Len, CI->getType(), false);
  1251. }
  1252. return 0;
  1253. }
  1254. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  1255. // Require two fixed pointer arguments and an integer result.
  1256. FunctionType *FT = Callee->getFunctionType();
  1257. if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
  1258. !FT->getParamType(1)->isPointerTy() ||
  1259. !FT->getReturnType()->isIntegerTy())
  1260. return 0;
  1261. if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
  1262. return V;
  1263. }
  1264. // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
  1265. // point arguments.
  1266. if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) {
  1267. Module *M = B.GetInsertBlock()->getParent()->getParent();
  1268. Constant *SIPrintFFn =
  1269. M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
  1270. CallInst *New = cast<CallInst>(CI->clone());
  1271. New->setCalledFunction(SIPrintFFn);
  1272. B.Insert(New);
  1273. return New;
  1274. }
  1275. return 0;
  1276. }
  1277. };
  1278. struct FPrintFOpt : public LibCallOptimization {
  1279. Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,
  1280. IRBuilder<> &B) {
  1281. // All the optimizations depend on the format string.
  1282. StringRef FormatStr;
  1283. if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
  1284. return 0;
  1285. // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
  1286. if (CI->getNumArgOperands() == 2) {
  1287. for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
  1288. if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
  1289. return 0; // We found a format specifier.
  1290. // These optimizations require DataLayout.
  1291. if (!TD) return 0;
  1292. Value *NewCI = EmitFWrite(CI->getArgOperand(1),
  1293. ConstantInt::get(TD->getIntPtrType(*Context),
  1294. FormatStr.size()),
  1295. CI->getArgOperand(0), B, TD, TLI);
  1296. return NewCI ? ConstantInt::get(CI->getType(), FormatStr.size()) : 0;
  1297. }
  1298. // The remaining optimizations require the format string to be "%s" or "%c"
  1299. // and have an extra operand.
  1300. if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
  1301. CI->getNumArgOperands() < 3)
  1302. return 0;
  1303. // Decode the second character of the format string.
  1304. if (FormatStr[1] == 'c') {
  1305. // fprintf(F, "%c", chr) --> fputc(chr, F)
  1306. if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
  1307. Value *NewCI = EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B,
  1308. TD, TLI);
  1309. return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
  1310. }
  1311. if (FormatStr[1] == 's') {
  1312. // fprintf(F, "%s", str) --> fputs(str, F)
  1313. if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
  1314. return 0;
  1315. return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD, TLI);
  1316. }
  1317. return 0;
  1318. }
  1319. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  1320. // Require two fixed paramters as pointers and integer result.
  1321. FunctionType *FT = Callee->getFunctionType();
  1322. if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
  1323. !FT->getParamType(1)->isPointerTy() ||
  1324. !FT->getReturnType()->isIntegerTy())
  1325. return 0;
  1326. if (Value *V = optimizeFixedFormatString(Callee, CI, B)) {
  1327. return V;
  1328. }
  1329. // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
  1330. // floating point arguments.
  1331. if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) {
  1332. Module *M = B.GetInsertBlock()->getParent()->getParent();
  1333. Constant *FIPrintFFn =
  1334. M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
  1335. CallInst *New = cast<CallInst>(CI->clone());
  1336. New->setCalledFunction(FIPrintFFn);
  1337. B.Insert(New);
  1338. return New;
  1339. }
  1340. return 0;
  1341. }
  1342. };
  1343. struct FWriteOpt : public LibCallOptimization {
  1344. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  1345. // Require a pointer, an integer, an integer, a pointer, returning integer.
  1346. FunctionType *FT = Callee->getFunctionType();
  1347. if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
  1348. !FT->getParamType(1)->isIntegerTy() ||
  1349. !FT->getParamType(2)->isIntegerTy() ||
  1350. !FT->getParamType(3)->isPointerTy() ||
  1351. !FT->getReturnType()->isIntegerTy())
  1352. return 0;
  1353. // Get the element size and count.
  1354. ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  1355. ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
  1356. if (!SizeC || !CountC) return 0;
  1357. uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
  1358. // If this is writing zero records, remove the call (it's a noop).
  1359. if (Bytes == 0)
  1360. return ConstantInt::get(CI->getType(), 0);
  1361. // If this is writing one byte, turn it into fputc.
  1362. // This optimisation is only valid, if the return value is unused.
  1363. if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
  1364. Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
  1365. Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, TD, TLI);
  1366. return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
  1367. }
  1368. return 0;
  1369. }
  1370. };
  1371. struct FPutsOpt : public LibCallOptimization {
  1372. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  1373. // These optimizations require DataLayout.
  1374. if (!TD) return 0;
  1375. // Require two pointers. Also, we can't optimize if return value is used.
  1376. FunctionType *FT = Callee->getFunctionType();
  1377. if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
  1378. !FT->getParamType(1)->isPointerTy() ||
  1379. !CI->use_empty())
  1380. return 0;
  1381. // fputs(s,F) --> fwrite(s,1,strlen(s),F)
  1382. uint64_t Len = GetStringLength(CI->getArgOperand(0));
  1383. if (!Len) return 0;
  1384. // Known to have no uses (see above).
  1385. return EmitFWrite(CI->getArgOperand(0),
  1386. ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
  1387. CI->getArgOperand(1), B, TD, TLI);
  1388. }
  1389. };
  1390. struct PutsOpt : public LibCallOptimization {
  1391. virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
  1392. // Require one fixed pointer argument and an integer/void result.
  1393. FunctionType *FT = Callee->getFunctionType();
  1394. if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
  1395. !(FT->getReturnType()->isIntegerTy() ||
  1396. FT->getReturnType()->isVoidTy()))
  1397. return 0;
  1398. // Check for a constant string.
  1399. StringRef Str;
  1400. if (!getConstantStringInfo(CI->getArgOperand(0), Str))
  1401. return 0;
  1402. if (Str.empty() && CI->use_empty()) {
  1403. // puts("") -> putchar('\n')
  1404. Value *Res = EmitPutChar(B.getInt32('\n'), B, TD, TLI);
  1405. if (CI->use_empty() || !Res) return Res;
  1406. return B.CreateIntCast(Res, CI->getType(), true);
  1407. }
  1408. return 0;
  1409. }
  1410. };
  1411. } // End anonymous namespace.
  1412. namespace llvm {
  1413. class LibCallSimplifierImpl {
  1414. const DataLayout *TD;
  1415. const TargetLibraryInfo *TLI;
  1416. const LibCallSimplifier *LCS;
  1417. bool UnsafeFPShrink;
  1418. StringMap<LibCallOptimization*> Optimizations;
  1419. // Fortified library call optimizations.
  1420. MemCpyChkOpt MemCpyChk;
  1421. MemMoveChkOpt MemMoveChk;
  1422. MemSetChkOpt MemSetChk;
  1423. StrCpyChkOpt StrCpyChk;
  1424. StpCpyChkOpt StpCpyChk;
  1425. StrNCpyChkOpt StrNCpyChk;
  1426. // String library call optimizations.
  1427. StrCatOpt StrCat;
  1428. StrNCatOpt StrNCat;
  1429. StrChrOpt StrChr;
  1430. StrRChrOpt StrRChr;
  1431. StrCmpOpt StrCmp;
  1432. StrNCmpOpt StrNCmp;
  1433. StrCpyOpt StrCpy;
  1434. StpCpyOpt StpCpy;
  1435. StrNCpyOpt StrNCpy;
  1436. StrLenOpt StrLen;
  1437. StrPBrkOpt StrPBrk;
  1438. StrToOpt StrTo;
  1439. StrSpnOpt StrSpn;
  1440. StrCSpnOpt StrCSpn;
  1441. StrStrOpt StrStr;
  1442. // Memory library call optimizations.
  1443. MemCmpOpt MemCmp;
  1444. MemCpyOpt MemCpy;
  1445. MemMoveOpt MemMove;
  1446. MemSetOpt MemSet;
  1447. // Math library call optimizations.
  1448. UnaryDoubleFPOpt UnaryDoubleFP, UnsafeUnaryDoubleFP;
  1449. CosOpt Cos; PowOpt Pow; Exp2Opt Exp2;
  1450. // Integer library call optimizations.
  1451. FFSOpt FFS;
  1452. AbsOpt Abs;
  1453. IsDigitOpt IsDigit;
  1454. IsAsciiOpt IsAscii;
  1455. ToAsciiOpt ToAscii;
  1456. // Formatting and IO library call optimizations.
  1457. PrintFOpt PrintF;
  1458. SPrintFOpt SPrintF;
  1459. FPrintFOpt FPrintF;
  1460. FWriteOpt FWrite;
  1461. FPutsOpt FPuts;
  1462. PutsOpt Puts;
  1463. void initOptimizations();
  1464. void addOpt(LibFunc::Func F, LibCallOptimization* Opt);
  1465. void addOpt(LibFunc::Func F1, LibFunc::Func F2, LibCallOptimization* Opt);
  1466. public:
  1467. LibCallSimplifierImpl(const DataLayout *TD, const TargetLibraryInfo *TLI,
  1468. const LibCallSimplifier *LCS,
  1469. bool UnsafeFPShrink = false)
  1470. : UnaryDoubleFP(false), UnsafeUnaryDoubleFP(true),
  1471. Cos(UnsafeFPShrink), Pow(UnsafeFPShrink), Exp2(UnsafeFPShrink) {
  1472. this->TD = TD;
  1473. this->TLI = TLI;
  1474. this->LCS = LCS;
  1475. this->UnsafeFPShrink = UnsafeFPShrink;
  1476. }
  1477. Value *optimizeCall(CallInst *CI);
  1478. };
  1479. void LibCallSimplifierImpl::initOptimizations() {
  1480. // Fortified library call optimizations.
  1481. Optimizations["__memcpy_chk"] = &MemCpyChk;
  1482. Optimizations["__memmove_chk"] = &MemMoveChk;
  1483. Optimizations["__memset_chk"] = &MemSetChk;
  1484. Optimizations["__strcpy_chk"] = &StrCpyChk;
  1485. Optimizations["__stpcpy_chk"] = &StpCpyChk;
  1486. Optimizations["__strncpy_chk"] = &StrNCpyChk;
  1487. Optimizations["__stpncpy_chk"] = &StrNCpyChk;
  1488. // String library call optimizations.
  1489. addOpt(LibFunc::strcat, &StrCat);
  1490. addOpt(LibFunc::strncat, &StrNCat);
  1491. addOpt(LibFunc::strchr, &StrChr);
  1492. addOpt(LibFunc::strrchr, &StrRChr);
  1493. addOpt(LibFunc::strcmp, &StrCmp);
  1494. addOpt(LibFunc::strncmp, &StrNCmp);
  1495. addOpt(LibFunc::strcpy, &StrCpy);
  1496. addOpt(LibFunc::stpcpy, &StpCpy);
  1497. addOpt(LibFunc::strncpy, &StrNCpy);
  1498. addOpt(LibFunc::strlen, &StrLen);
  1499. addOpt(LibFunc::strpbrk, &StrPBrk);
  1500. addOpt(LibFunc::strtol, &StrTo);
  1501. addOpt(LibFunc::strtod, &StrTo);
  1502. addOpt(LibFunc::strtof, &StrTo);
  1503. addOpt(LibFunc::strtoul, &StrTo);
  1504. addOpt(LibFunc::strtoll, &StrTo);
  1505. addOpt(LibFunc::strtold, &StrTo);
  1506. addOpt(LibFunc::strtoull, &StrTo);
  1507. addOpt(LibFunc::strspn, &StrSpn);
  1508. addOpt(LibFunc::strcspn, &StrCSpn);
  1509. addOpt(LibFunc::strstr, &StrStr);
  1510. // Memory library call optimizations.
  1511. addOpt(LibFunc::memcmp, &MemCmp);
  1512. addOpt(LibFunc::memcpy, &MemCpy);
  1513. addOpt(LibFunc::memmove, &MemMove);
  1514. addOpt(LibFunc::memset, &MemSet);
  1515. // Math library call optimizations.
  1516. addOpt(LibFunc::ceil, LibFunc::ceilf, &UnaryDoubleFP);
  1517. addOpt(LibFunc::fabs, LibFunc::fabsf, &UnaryDoubleFP);
  1518. addOpt(LibFunc::floor, LibFunc::floorf, &UnaryDoubleFP);
  1519. addOpt(LibFunc::rint, LibFunc::rintf, &UnaryDoubleFP);
  1520. addOpt(LibFunc::round, LibFunc::roundf, &UnaryDoubleFP);
  1521. addOpt(LibFunc::nearbyint, LibFunc::nearbyintf, &UnaryDoubleFP);
  1522. addOpt(LibFunc::trunc, LibFunc::truncf, &UnaryDoubleFP);
  1523. if(UnsafeFPShrink) {
  1524. addOpt(LibFunc::acos, LibFunc::acosf, &UnsafeUnaryDoubleFP);
  1525. addOpt(LibFunc::acosh, LibFunc::acoshf, &UnsafeUnaryDoubleFP);
  1526. addOpt(LibFunc::asin, LibFunc::asinf, &UnsafeUnaryDoubleFP);
  1527. addOpt(LibFunc::asinh, LibFunc::asinhf, &UnsafeUnaryDoubleFP);
  1528. addOpt(LibFunc::atan, LibFunc::atanf, &UnsafeUnaryDoubleFP);
  1529. addOpt(LibFunc::atanh, LibFunc::atanhf, &UnsafeUnaryDoubleFP);
  1530. addOpt(LibFunc::cbrt, LibFunc::cbrtf, &UnsafeUnaryDoubleFP);
  1531. addOpt(LibFunc::cosh, LibFunc::coshf, &UnsafeUnaryDoubleFP);
  1532. addOpt(LibFunc::exp, LibFunc::expf, &UnsafeUnaryDoubleFP);
  1533. addOpt(LibFunc::exp10, LibFunc::exp10f, &UnsafeUnaryDoubleFP);
  1534. addOpt(LibFunc::expm1, LibFunc::expm1f, &UnsafeUnaryDoubleFP);
  1535. addOpt(LibFunc::log, LibFunc::logf, &UnsafeUnaryDoubleFP);
  1536. addOpt(LibFunc::log10, LibFunc::log10f, &UnsafeUnaryDoubleFP);
  1537. addOpt(LibFunc::log1p, LibFunc::log1pf, &UnsafeUnaryDoubleFP);
  1538. addOpt(LibFunc::log2, LibFunc::log2f, &UnsafeUnaryDoubleFP);
  1539. addOpt(LibFunc::logb, LibFunc::logbf, &UnsafeUnaryDoubleFP);
  1540. addOpt(LibFunc::sin, LibFunc::sinf, &UnsafeUnaryDoubleFP);
  1541. addOpt(LibFunc::sinh, LibFunc::sinhf, &UnsafeUnaryDoubleFP);
  1542. addOpt(LibFunc::sqrt, LibFunc::sqrtf, &UnsafeUnaryDoubleFP);
  1543. addOpt(LibFunc::tan, LibFunc::tanf, &UnsafeUnaryDoubleFP);
  1544. addOpt(LibFunc::tanh, LibFunc::tanhf, &UnsafeUnaryDoubleFP);
  1545. }
  1546. addOpt(LibFunc::cosf, &Cos);
  1547. addOpt(LibFunc::cos, &Cos);
  1548. addOpt(LibFunc::cosl, &Cos);
  1549. addOpt(LibFunc::powf, &Pow);
  1550. addOpt(LibFunc::pow, &Pow);
  1551. addOpt(LibFunc::powl, &Pow);
  1552. Optimizations["llvm.pow.f32"] = &Pow;
  1553. Optimizations["llvm.pow.f64"] = &Pow;
  1554. Optimizations["llvm.pow.f80"] = &Pow;
  1555. Optimizations["llvm.pow.f128"] = &Pow;
  1556. Optimizations["llvm.pow.ppcf128"] = &Pow;
  1557. addOpt(LibFunc::exp2l, &Exp2);
  1558. addOpt(LibFunc::exp2, &Exp2);
  1559. addOpt(LibFunc::exp2f, &Exp2);
  1560. Optimizations["llvm.exp2.ppcf128"] = &Exp2;
  1561. Optimizations["llvm.exp2.f128"] = &Exp2;
  1562. Optimizations["llvm.exp2.f80"] = &Exp2;
  1563. Optimizations["llvm.exp2.f64"] = &Exp2;
  1564. Optimizations["llvm.exp2.f32"] = &Exp2;
  1565. // Integer library call optimizations.
  1566. addOpt(LibFunc::ffs, &FFS);
  1567. addOpt(LibFunc::ffsl, &FFS);
  1568. addOpt(LibFunc::ffsll, &FFS);
  1569. addOpt(LibFunc::abs, &Abs);
  1570. addOpt(LibFunc::labs, &Abs);
  1571. addOpt(LibFunc::llabs, &Abs);
  1572. addOpt(LibFunc::isdigit, &IsDigit);
  1573. addOpt(LibFunc::isascii, &IsAscii);
  1574. addOpt(LibFunc::toascii, &ToAscii);
  1575. // Formatting and IO library call optimizations.
  1576. addOpt(LibFunc::printf, &PrintF);
  1577. addOpt(LibFunc::sprintf, &SPrintF);
  1578. addOpt(LibFunc::fprintf, &FPrintF);
  1579. addOpt(LibFunc::fwrite, &FWrite);
  1580. addOpt(LibFunc::fputs, &FPuts);
  1581. addOpt(LibFunc::puts, &Puts);
  1582. }
  1583. Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) {
  1584. if (Optimizations.empty())
  1585. initOptimizations();
  1586. Function *Callee = CI->getCalledFunction();
  1587. LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
  1588. if (LCO) {
  1589. IRBuilder<> Builder(CI);
  1590. return LCO->optimizeCall(CI, TD, TLI, LCS, Builder);
  1591. }
  1592. return 0;
  1593. }
  1594. void LibCallSimplifierImpl::addOpt(LibFunc::Func F, LibCallOptimization* Opt) {
  1595. if (TLI->has(F))
  1596. Optimizations[TLI->getName(F)] = Opt;
  1597. }
  1598. void LibCallSimplifierImpl::addOpt(LibFunc::Func F1, LibFunc::Func F2,
  1599. LibCallOptimization* Opt) {
  1600. if (TLI->has(F1) && TLI->has(F2))
  1601. Optimizations[TLI->getName(F1)] = Opt;
  1602. }
  1603. LibCallSimplifier::LibCallSimplifier(const DataLayout *TD,
  1604. const TargetLibraryInfo *TLI,
  1605. bool UnsafeFPShrink) {
  1606. Impl = new LibCallSimplifierImpl(TD, TLI, this, UnsafeFPShrink);
  1607. }
  1608. LibCallSimplifier::~LibCallSimplifier() {
  1609. delete Impl;
  1610. }
  1611. Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
  1612. return Impl->optimizeCall(CI);
  1613. }
  1614. void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) const {
  1615. I->replaceAllUsesWith(With);
  1616. I->eraseFromParent();
  1617. }
  1618. }