SimplifyLibCalls.cpp 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  1. //===------ SimplifyLibCalls.cpp - Library calls simplifier ---------------===//
  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 the library calls simplifier. It does not implement
  10. // any pass, but can't be used by other passes to do simplifications.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/SimplifyLibCalls.h"
  14. #include "llvm/ADT/APSInt.h"
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/ADT/StringMap.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/Analysis/ConstantFolding.h"
  19. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  20. #include "llvm/Analysis/TargetLibraryInfo.h"
  21. #include "llvm/Transforms/Utils/Local.h"
  22. #include "llvm/Analysis/ValueTracking.h"
  23. #include "llvm/Analysis/CaptureTracking.h"
  24. #include "llvm/Analysis/Loads.h"
  25. #include "llvm/IR/DataLayout.h"
  26. #include "llvm/IR/Function.h"
  27. #include "llvm/IR/IRBuilder.h"
  28. #include "llvm/IR/IntrinsicInst.h"
  29. #include "llvm/IR/Intrinsics.h"
  30. #include "llvm/IR/LLVMContext.h"
  31. #include "llvm/IR/Module.h"
  32. #include "llvm/IR/PatternMatch.h"
  33. #include "llvm/Support/CommandLine.h"
  34. #include "llvm/Support/KnownBits.h"
  35. #include "llvm/Transforms/Utils/BuildLibCalls.h"
  36. using namespace llvm;
  37. using namespace PatternMatch;
  38. static cl::opt<bool>
  39. EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden,
  40. cl::init(false),
  41. cl::desc("Enable unsafe double to float "
  42. "shrinking for math lib calls"));
  43. //===----------------------------------------------------------------------===//
  44. // Helper Functions
  45. //===----------------------------------------------------------------------===//
  46. static bool ignoreCallingConv(LibFunc Func) {
  47. return Func == LibFunc_abs || Func == LibFunc_labs ||
  48. Func == LibFunc_llabs || Func == LibFunc_strlen;
  49. }
  50. static bool isCallingConvCCompatible(CallInst *CI) {
  51. switch(CI->getCallingConv()) {
  52. default:
  53. return false;
  54. case llvm::CallingConv::C:
  55. return true;
  56. case llvm::CallingConv::ARM_APCS:
  57. case llvm::CallingConv::ARM_AAPCS:
  58. case llvm::CallingConv::ARM_AAPCS_VFP: {
  59. // The iOS ABI diverges from the standard in some cases, so for now don't
  60. // try to simplify those calls.
  61. if (Triple(CI->getModule()->getTargetTriple()).isiOS())
  62. return false;
  63. auto *FuncTy = CI->getFunctionType();
  64. if (!FuncTy->getReturnType()->isPointerTy() &&
  65. !FuncTy->getReturnType()->isIntegerTy() &&
  66. !FuncTy->getReturnType()->isVoidTy())
  67. return false;
  68. for (auto Param : FuncTy->params()) {
  69. if (!Param->isPointerTy() && !Param->isIntegerTy())
  70. return false;
  71. }
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. /// Return true if it is only used in equality comparisons with With.
  78. static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
  79. for (User *U : V->users()) {
  80. if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
  81. if (IC->isEquality() && IC->getOperand(1) == With)
  82. continue;
  83. // Unknown instruction.
  84. return false;
  85. }
  86. return true;
  87. }
  88. static bool callHasFloatingPointArgument(const CallInst *CI) {
  89. return any_of(CI->operands(), [](const Use &OI) {
  90. return OI->getType()->isFloatingPointTy();
  91. });
  92. }
  93. static bool callHasFP128Argument(const CallInst *CI) {
  94. return any_of(CI->operands(), [](const Use &OI) {
  95. return OI->getType()->isFP128Ty();
  96. });
  97. }
  98. static Value *convertStrToNumber(CallInst *CI, StringRef &Str, int64_t Base) {
  99. if (Base < 2 || Base > 36)
  100. // handle special zero base
  101. if (Base != 0)
  102. return nullptr;
  103. char *End;
  104. std::string nptr = Str.str();
  105. errno = 0;
  106. long long int Result = strtoll(nptr.c_str(), &End, Base);
  107. if (errno)
  108. return nullptr;
  109. // if we assume all possible target locales are ASCII supersets,
  110. // then if strtoll successfully parses a number on the host,
  111. // it will also successfully parse the same way on the target
  112. if (*End != '\0')
  113. return nullptr;
  114. if (!isIntN(CI->getType()->getPrimitiveSizeInBits(), Result))
  115. return nullptr;
  116. return ConstantInt::get(CI->getType(), Result);
  117. }
  118. static bool isLocallyOpenedFile(Value *File, CallInst *CI, IRBuilder<> &B,
  119. const TargetLibraryInfo *TLI) {
  120. CallInst *FOpen = dyn_cast<CallInst>(File);
  121. if (!FOpen)
  122. return false;
  123. Function *InnerCallee = FOpen->getCalledFunction();
  124. if (!InnerCallee)
  125. return false;
  126. LibFunc Func;
  127. if (!TLI->getLibFunc(*InnerCallee, Func) || !TLI->has(Func) ||
  128. Func != LibFunc_fopen)
  129. return false;
  130. inferLibFuncAttributes(*CI->getCalledFunction(), *TLI);
  131. if (PointerMayBeCaptured(File, true, true))
  132. return false;
  133. return true;
  134. }
  135. static bool isOnlyUsedInComparisonWithZero(Value *V) {
  136. for (User *U : V->users()) {
  137. if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
  138. if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
  139. if (C->isNullValue())
  140. continue;
  141. // Unknown instruction.
  142. return false;
  143. }
  144. return true;
  145. }
  146. static bool canTransformToMemCmp(CallInst *CI, Value *Str, uint64_t Len,
  147. const DataLayout &DL) {
  148. if (!isOnlyUsedInComparisonWithZero(CI))
  149. return false;
  150. if (!isDereferenceableAndAlignedPointer(Str, 1, APInt(64, Len), DL))
  151. return false;
  152. if (CI->getFunction()->hasFnAttribute(Attribute::SanitizeMemory))
  153. return false;
  154. return true;
  155. }
  156. //===----------------------------------------------------------------------===//
  157. // String and Memory Library Call Optimizations
  158. //===----------------------------------------------------------------------===//
  159. Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilder<> &B) {
  160. // Extract some information from the instruction
  161. Value *Dst = CI->getArgOperand(0);
  162. Value *Src = CI->getArgOperand(1);
  163. // See if we can get the length of the input string.
  164. uint64_t Len = GetStringLength(Src);
  165. if (Len == 0)
  166. return nullptr;
  167. --Len; // Unbias length.
  168. // Handle the simple, do-nothing case: strcat(x, "") -> x
  169. if (Len == 0)
  170. return Dst;
  171. return emitStrLenMemCpy(Src, Dst, Len, B);
  172. }
  173. Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
  174. IRBuilder<> &B) {
  175. // We need to find the end of the destination string. That's where the
  176. // memory is to be moved to. We just generate a call to strlen.
  177. Value *DstLen = emitStrLen(Dst, B, DL, TLI);
  178. if (!DstLen)
  179. return nullptr;
  180. // Now that we have the destination's length, we must index into the
  181. // destination's pointer to get the actual memcpy destination (end of
  182. // the string .. we're concatenating).
  183. Value *CpyDst = B.CreateGEP(B.getInt8Ty(), Dst, DstLen, "endptr");
  184. // We have enough information to now generate the memcpy call to do the
  185. // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
  186. B.CreateMemCpy(CpyDst, 1, Src, 1,
  187. ConstantInt::get(DL.getIntPtrType(Src->getContext()), Len + 1));
  188. return Dst;
  189. }
  190. Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilder<> &B) {
  191. // Extract some information from the instruction.
  192. Value *Dst = CI->getArgOperand(0);
  193. Value *Src = CI->getArgOperand(1);
  194. uint64_t Len;
  195. // We don't do anything if length is not constant.
  196. if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
  197. Len = LengthArg->getZExtValue();
  198. else
  199. return nullptr;
  200. // See if we can get the length of the input string.
  201. uint64_t SrcLen = GetStringLength(Src);
  202. if (SrcLen == 0)
  203. return nullptr;
  204. --SrcLen; // Unbias length.
  205. // Handle the simple, do-nothing cases:
  206. // strncat(x, "", c) -> x
  207. // strncat(x, c, 0) -> x
  208. if (SrcLen == 0 || Len == 0)
  209. return Dst;
  210. // We don't optimize this case.
  211. if (Len < SrcLen)
  212. return nullptr;
  213. // strncat(x, s, c) -> strcat(x, s)
  214. // s is constant so the strcat can be optimized further.
  215. return emitStrLenMemCpy(Src, Dst, SrcLen, B);
  216. }
  217. Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilder<> &B) {
  218. Function *Callee = CI->getCalledFunction();
  219. FunctionType *FT = Callee->getFunctionType();
  220. Value *SrcStr = CI->getArgOperand(0);
  221. // If the second operand is non-constant, see if we can compute the length
  222. // of the input string and turn this into memchr.
  223. ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  224. if (!CharC) {
  225. uint64_t Len = GetStringLength(SrcStr);
  226. if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32)) // memchr needs i32.
  227. return nullptr;
  228. return emitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
  229. ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len),
  230. B, DL, TLI);
  231. }
  232. // Otherwise, the character is a constant, see if the first argument is
  233. // a string literal. If so, we can constant fold.
  234. StringRef Str;
  235. if (!getConstantStringInfo(SrcStr, Str)) {
  236. if (CharC->isZero()) // strchr(p, 0) -> p + strlen(p)
  237. return B.CreateGEP(B.getInt8Ty(), SrcStr, emitStrLen(SrcStr, B, DL, TLI),
  238. "strchr");
  239. return nullptr;
  240. }
  241. // Compute the offset, make sure to handle the case when we're searching for
  242. // zero (a weird way to spell strlen).
  243. size_t I = (0xFF & CharC->getSExtValue()) == 0
  244. ? Str.size()
  245. : Str.find(CharC->getSExtValue());
  246. if (I == StringRef::npos) // Didn't find the char. strchr returns null.
  247. return Constant::getNullValue(CI->getType());
  248. // strchr(s+n,c) -> gep(s+n+i,c)
  249. return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strchr");
  250. }
  251. Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilder<> &B) {
  252. Value *SrcStr = CI->getArgOperand(0);
  253. ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  254. // Cannot fold anything if we're not looking for a constant.
  255. if (!CharC)
  256. return nullptr;
  257. StringRef Str;
  258. if (!getConstantStringInfo(SrcStr, Str)) {
  259. // strrchr(s, 0) -> strchr(s, 0)
  260. if (CharC->isZero())
  261. return emitStrChr(SrcStr, '\0', B, TLI);
  262. return nullptr;
  263. }
  264. // Compute the offset.
  265. size_t I = (0xFF & CharC->getSExtValue()) == 0
  266. ? Str.size()
  267. : Str.rfind(CharC->getSExtValue());
  268. if (I == StringRef::npos) // Didn't find the char. Return null.
  269. return Constant::getNullValue(CI->getType());
  270. // strrchr(s+n,c) -> gep(s+n+i,c)
  271. return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strrchr");
  272. }
  273. Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilder<> &B) {
  274. Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
  275. if (Str1P == Str2P) // strcmp(x,x) -> 0
  276. return ConstantInt::get(CI->getType(), 0);
  277. StringRef Str1, Str2;
  278. bool HasStr1 = getConstantStringInfo(Str1P, Str1);
  279. bool HasStr2 = getConstantStringInfo(Str2P, Str2);
  280. // strcmp(x, y) -> cnst (if both x and y are constant strings)
  281. if (HasStr1 && HasStr2)
  282. return ConstantInt::get(CI->getType(), Str1.compare(Str2));
  283. if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
  284. return B.CreateNeg(B.CreateZExt(
  285. B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
  286. if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
  287. return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"),
  288. CI->getType());
  289. // strcmp(P, "x") -> memcmp(P, "x", 2)
  290. uint64_t Len1 = GetStringLength(Str1P);
  291. uint64_t Len2 = GetStringLength(Str2P);
  292. if (Len1 && Len2) {
  293. return emitMemCmp(Str1P, Str2P,
  294. ConstantInt::get(DL.getIntPtrType(CI->getContext()),
  295. std::min(Len1, Len2)),
  296. B, DL, TLI);
  297. }
  298. // strcmp to memcmp
  299. if (!HasStr1 && HasStr2) {
  300. if (canTransformToMemCmp(CI, Str1P, Len2, DL))
  301. return emitMemCmp(
  302. Str1P, Str2P,
  303. ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len2), B, DL,
  304. TLI);
  305. } else if (HasStr1 && !HasStr2) {
  306. if (canTransformToMemCmp(CI, Str2P, Len1, DL))
  307. return emitMemCmp(
  308. Str1P, Str2P,
  309. ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len1), B, DL,
  310. TLI);
  311. }
  312. return nullptr;
  313. }
  314. Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilder<> &B) {
  315. Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
  316. if (Str1P == Str2P) // strncmp(x,x,n) -> 0
  317. return ConstantInt::get(CI->getType(), 0);
  318. // Get the length argument if it is constant.
  319. uint64_t Length;
  320. if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
  321. Length = LengthArg->getZExtValue();
  322. else
  323. return nullptr;
  324. if (Length == 0) // strncmp(x,y,0) -> 0
  325. return ConstantInt::get(CI->getType(), 0);
  326. if (Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
  327. return emitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI);
  328. StringRef Str1, Str2;
  329. bool HasStr1 = getConstantStringInfo(Str1P, Str1);
  330. bool HasStr2 = getConstantStringInfo(Str2P, Str2);
  331. // strncmp(x, y) -> cnst (if both x and y are constant strings)
  332. if (HasStr1 && HasStr2) {
  333. StringRef SubStr1 = Str1.substr(0, Length);
  334. StringRef SubStr2 = Str2.substr(0, Length);
  335. return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
  336. }
  337. if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
  338. return B.CreateNeg(B.CreateZExt(
  339. B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
  340. if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
  341. return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"),
  342. CI->getType());
  343. uint64_t Len1 = GetStringLength(Str1P);
  344. uint64_t Len2 = GetStringLength(Str2P);
  345. // strncmp to memcmp
  346. if (!HasStr1 && HasStr2) {
  347. Len2 = std::min(Len2, Length);
  348. if (canTransformToMemCmp(CI, Str1P, Len2, DL))
  349. return emitMemCmp(
  350. Str1P, Str2P,
  351. ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len2), B, DL,
  352. TLI);
  353. } else if (HasStr1 && !HasStr2) {
  354. Len1 = std::min(Len1, Length);
  355. if (canTransformToMemCmp(CI, Str2P, Len1, DL))
  356. return emitMemCmp(
  357. Str1P, Str2P,
  358. ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len1), B, DL,
  359. TLI);
  360. }
  361. return nullptr;
  362. }
  363. Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilder<> &B) {
  364. Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
  365. if (Dst == Src) // strcpy(x,x) -> x
  366. return Src;
  367. // See if we can get the length of the input string.
  368. uint64_t Len = GetStringLength(Src);
  369. if (Len == 0)
  370. return nullptr;
  371. // We have enough information to now generate the memcpy call to do the
  372. // copy for us. Make a memcpy to copy the nul byte with align = 1.
  373. B.CreateMemCpy(Dst, 1, Src, 1,
  374. ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len));
  375. return Dst;
  376. }
  377. Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilder<> &B) {
  378. Function *Callee = CI->getCalledFunction();
  379. Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
  380. if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
  381. Value *StrLen = emitStrLen(Src, B, DL, TLI);
  382. return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
  383. }
  384. // See if we can get the length of the input string.
  385. uint64_t Len = GetStringLength(Src);
  386. if (Len == 0)
  387. return nullptr;
  388. Type *PT = Callee->getFunctionType()->getParamType(0);
  389. Value *LenV = ConstantInt::get(DL.getIntPtrType(PT), Len);
  390. Value *DstEnd = B.CreateGEP(B.getInt8Ty(), Dst,
  391. ConstantInt::get(DL.getIntPtrType(PT), Len - 1));
  392. // We have enough information to now generate the memcpy call to do the
  393. // copy for us. Make a memcpy to copy the nul byte with align = 1.
  394. B.CreateMemCpy(Dst, 1, Src, 1, LenV);
  395. return DstEnd;
  396. }
  397. Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilder<> &B) {
  398. Function *Callee = CI->getCalledFunction();
  399. Value *Dst = CI->getArgOperand(0);
  400. Value *Src = CI->getArgOperand(1);
  401. Value *LenOp = CI->getArgOperand(2);
  402. // See if we can get the length of the input string.
  403. uint64_t SrcLen = GetStringLength(Src);
  404. if (SrcLen == 0)
  405. return nullptr;
  406. --SrcLen;
  407. if (SrcLen == 0) {
  408. // strncpy(x, "", y) -> memset(align 1 x, '\0', y)
  409. B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
  410. return Dst;
  411. }
  412. uint64_t Len;
  413. if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
  414. Len = LengthArg->getZExtValue();
  415. else
  416. return nullptr;
  417. if (Len == 0)
  418. return Dst; // strncpy(x, y, 0) -> x
  419. // Let strncpy handle the zero padding
  420. if (Len > SrcLen + 1)
  421. return nullptr;
  422. Type *PT = Callee->getFunctionType()->getParamType(0);
  423. // strncpy(x, s, c) -> memcpy(align 1 x, align 1 s, c) [s and c are constant]
  424. B.CreateMemCpy(Dst, 1, Src, 1, ConstantInt::get(DL.getIntPtrType(PT), Len));
  425. return Dst;
  426. }
  427. Value *LibCallSimplifier::optimizeStringLength(CallInst *CI, IRBuilder<> &B,
  428. unsigned CharSize) {
  429. Value *Src = CI->getArgOperand(0);
  430. // Constant folding: strlen("xyz") -> 3
  431. if (uint64_t Len = GetStringLength(Src, CharSize))
  432. return ConstantInt::get(CI->getType(), Len - 1);
  433. // If s is a constant pointer pointing to a string literal, we can fold
  434. // strlen(s + x) to strlen(s) - x, when x is known to be in the range
  435. // [0, strlen(s)] or the string has a single null terminator '\0' at the end.
  436. // We only try to simplify strlen when the pointer s points to an array
  437. // of i8. Otherwise, we would need to scale the offset x before doing the
  438. // subtraction. This will make the optimization more complex, and it's not
  439. // very useful because calling strlen for a pointer of other types is
  440. // very uncommon.
  441. if (GEPOperator *GEP = dyn_cast<GEPOperator>(Src)) {
  442. if (!isGEPBasedOnPointerToString(GEP, CharSize))
  443. return nullptr;
  444. ConstantDataArraySlice Slice;
  445. if (getConstantDataArrayInfo(GEP->getOperand(0), Slice, CharSize)) {
  446. uint64_t NullTermIdx;
  447. if (Slice.Array == nullptr) {
  448. NullTermIdx = 0;
  449. } else {
  450. NullTermIdx = ~((uint64_t)0);
  451. for (uint64_t I = 0, E = Slice.Length; I < E; ++I) {
  452. if (Slice.Array->getElementAsInteger(I + Slice.Offset) == 0) {
  453. NullTermIdx = I;
  454. break;
  455. }
  456. }
  457. // If the string does not have '\0', leave it to strlen to compute
  458. // its length.
  459. if (NullTermIdx == ~((uint64_t)0))
  460. return nullptr;
  461. }
  462. Value *Offset = GEP->getOperand(2);
  463. KnownBits Known = computeKnownBits(Offset, DL, 0, nullptr, CI, nullptr);
  464. Known.Zero.flipAllBits();
  465. uint64_t ArrSize =
  466. cast<ArrayType>(GEP->getSourceElementType())->getNumElements();
  467. // KnownZero's bits are flipped, so zeros in KnownZero now represent
  468. // bits known to be zeros in Offset, and ones in KnowZero represent
  469. // bits unknown in Offset. Therefore, Offset is known to be in range
  470. // [0, NullTermIdx] when the flipped KnownZero is non-negative and
  471. // unsigned-less-than NullTermIdx.
  472. //
  473. // If Offset is not provably in the range [0, NullTermIdx], we can still
  474. // optimize if we can prove that the program has undefined behavior when
  475. // Offset is outside that range. That is the case when GEP->getOperand(0)
  476. // is a pointer to an object whose memory extent is NullTermIdx+1.
  477. if ((Known.Zero.isNonNegative() && Known.Zero.ule(NullTermIdx)) ||
  478. (GEP->isInBounds() && isa<GlobalVariable>(GEP->getOperand(0)) &&
  479. NullTermIdx == ArrSize - 1)) {
  480. Offset = B.CreateSExtOrTrunc(Offset, CI->getType());
  481. return B.CreateSub(ConstantInt::get(CI->getType(), NullTermIdx),
  482. Offset);
  483. }
  484. }
  485. return nullptr;
  486. }
  487. // strlen(x?"foo":"bars") --> x ? 3 : 4
  488. if (SelectInst *SI = dyn_cast<SelectInst>(Src)) {
  489. uint64_t LenTrue = GetStringLength(SI->getTrueValue(), CharSize);
  490. uint64_t LenFalse = GetStringLength(SI->getFalseValue(), CharSize);
  491. if (LenTrue && LenFalse) {
  492. ORE.emit([&]() {
  493. return OptimizationRemark("instcombine", "simplify-libcalls", CI)
  494. << "folded strlen(select) to select of constants";
  495. });
  496. return B.CreateSelect(SI->getCondition(),
  497. ConstantInt::get(CI->getType(), LenTrue - 1),
  498. ConstantInt::get(CI->getType(), LenFalse - 1));
  499. }
  500. }
  501. // strlen(x) != 0 --> *x != 0
  502. // strlen(x) == 0 --> *x == 0
  503. if (isOnlyUsedInZeroEqualityComparison(CI))
  504. return B.CreateZExt(B.CreateLoad(B.getIntNTy(CharSize), Src, "strlenfirst"),
  505. CI->getType());
  506. return nullptr;
  507. }
  508. Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilder<> &B) {
  509. return optimizeStringLength(CI, B, 8);
  510. }
  511. Value *LibCallSimplifier::optimizeWcslen(CallInst *CI, IRBuilder<> &B) {
  512. Module &M = *CI->getModule();
  513. unsigned WCharSize = TLI->getWCharSize(M) * 8;
  514. // We cannot perform this optimization without wchar_size metadata.
  515. if (WCharSize == 0)
  516. return nullptr;
  517. return optimizeStringLength(CI, B, WCharSize);
  518. }
  519. Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilder<> &B) {
  520. StringRef S1, S2;
  521. bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
  522. bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
  523. // strpbrk(s, "") -> nullptr
  524. // strpbrk("", s) -> nullptr
  525. if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
  526. return Constant::getNullValue(CI->getType());
  527. // Constant folding.
  528. if (HasS1 && HasS2) {
  529. size_t I = S1.find_first_of(S2);
  530. if (I == StringRef::npos) // No match.
  531. return Constant::getNullValue(CI->getType());
  532. return B.CreateGEP(B.getInt8Ty(), CI->getArgOperand(0), B.getInt64(I),
  533. "strpbrk");
  534. }
  535. // strpbrk(s, "a") -> strchr(s, 'a')
  536. if (HasS2 && S2.size() == 1)
  537. return emitStrChr(CI->getArgOperand(0), S2[0], B, TLI);
  538. return nullptr;
  539. }
  540. Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilder<> &B) {
  541. Value *EndPtr = CI->getArgOperand(1);
  542. if (isa<ConstantPointerNull>(EndPtr)) {
  543. // With a null EndPtr, this function won't capture the main argument.
  544. // It would be readonly too, except that it still may write to errno.
  545. CI->addParamAttr(0, Attribute::NoCapture);
  546. }
  547. return nullptr;
  548. }
  549. Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilder<> &B) {
  550. StringRef S1, S2;
  551. bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
  552. bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
  553. // strspn(s, "") -> 0
  554. // strspn("", s) -> 0
  555. if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
  556. return Constant::getNullValue(CI->getType());
  557. // Constant folding.
  558. if (HasS1 && HasS2) {
  559. size_t Pos = S1.find_first_not_of(S2);
  560. if (Pos == StringRef::npos)
  561. Pos = S1.size();
  562. return ConstantInt::get(CI->getType(), Pos);
  563. }
  564. return nullptr;
  565. }
  566. Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilder<> &B) {
  567. StringRef S1, S2;
  568. bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
  569. bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
  570. // strcspn("", s) -> 0
  571. if (HasS1 && S1.empty())
  572. return Constant::getNullValue(CI->getType());
  573. // Constant folding.
  574. if (HasS1 && HasS2) {
  575. size_t Pos = S1.find_first_of(S2);
  576. if (Pos == StringRef::npos)
  577. Pos = S1.size();
  578. return ConstantInt::get(CI->getType(), Pos);
  579. }
  580. // strcspn(s, "") -> strlen(s)
  581. if (HasS2 && S2.empty())
  582. return emitStrLen(CI->getArgOperand(0), B, DL, TLI);
  583. return nullptr;
  584. }
  585. Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilder<> &B) {
  586. // fold strstr(x, x) -> x.
  587. if (CI->getArgOperand(0) == CI->getArgOperand(1))
  588. return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
  589. // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
  590. if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
  591. Value *StrLen = emitStrLen(CI->getArgOperand(1), B, DL, TLI);
  592. if (!StrLen)
  593. return nullptr;
  594. Value *StrNCmp = emitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
  595. StrLen, B, DL, TLI);
  596. if (!StrNCmp)
  597. return nullptr;
  598. for (auto UI = CI->user_begin(), UE = CI->user_end(); UI != UE;) {
  599. ICmpInst *Old = cast<ICmpInst>(*UI++);
  600. Value *Cmp =
  601. B.CreateICmp(Old->getPredicate(), StrNCmp,
  602. ConstantInt::getNullValue(StrNCmp->getType()), "cmp");
  603. replaceAllUsesWith(Old, Cmp);
  604. }
  605. return CI;
  606. }
  607. // See if either input string is a constant string.
  608. StringRef SearchStr, ToFindStr;
  609. bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
  610. bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
  611. // fold strstr(x, "") -> x.
  612. if (HasStr2 && ToFindStr.empty())
  613. return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
  614. // If both strings are known, constant fold it.
  615. if (HasStr1 && HasStr2) {
  616. size_t Offset = SearchStr.find(ToFindStr);
  617. if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
  618. return Constant::getNullValue(CI->getType());
  619. // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
  620. Value *Result = castToCStr(CI->getArgOperand(0), B);
  621. Result =
  622. B.CreateConstInBoundsGEP1_64(B.getInt8Ty(), Result, Offset, "strstr");
  623. return B.CreateBitCast(Result, CI->getType());
  624. }
  625. // fold strstr(x, "y") -> strchr(x, 'y').
  626. if (HasStr2 && ToFindStr.size() == 1) {
  627. Value *StrChr = emitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI);
  628. return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr;
  629. }
  630. return nullptr;
  631. }
  632. Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilder<> &B) {
  633. Value *SrcStr = CI->getArgOperand(0);
  634. ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  635. ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
  636. // memchr(x, y, 0) -> null
  637. if (LenC && LenC->isZero())
  638. return Constant::getNullValue(CI->getType());
  639. // From now on we need at least constant length and string.
  640. StringRef Str;
  641. if (!LenC || !getConstantStringInfo(SrcStr, Str, 0, /*TrimAtNul=*/false))
  642. return nullptr;
  643. // Truncate the string to LenC. If Str is smaller than LenC we will still only
  644. // scan the string, as reading past the end of it is undefined and we can just
  645. // return null if we don't find the char.
  646. Str = Str.substr(0, LenC->getZExtValue());
  647. // If the char is variable but the input str and length are not we can turn
  648. // this memchr call into a simple bit field test. Of course this only works
  649. // when the return value is only checked against null.
  650. //
  651. // It would be really nice to reuse switch lowering here but we can't change
  652. // the CFG at this point.
  653. //
  654. // memchr("\r\n", C, 2) != nullptr -> (1 << C & ((1 << '\r') | (1 << '\n')))
  655. // != 0
  656. // after bounds check.
  657. if (!CharC && !Str.empty() && isOnlyUsedInZeroEqualityComparison(CI)) {
  658. unsigned char Max =
  659. *std::max_element(reinterpret_cast<const unsigned char *>(Str.begin()),
  660. reinterpret_cast<const unsigned char *>(Str.end()));
  661. // Make sure the bit field we're about to create fits in a register on the
  662. // target.
  663. // FIXME: On a 64 bit architecture this prevents us from using the
  664. // interesting range of alpha ascii chars. We could do better by emitting
  665. // two bitfields or shifting the range by 64 if no lower chars are used.
  666. if (!DL.fitsInLegalInteger(Max + 1))
  667. return nullptr;
  668. // For the bit field use a power-of-2 type with at least 8 bits to avoid
  669. // creating unnecessary illegal types.
  670. unsigned char Width = NextPowerOf2(std::max((unsigned char)7, Max));
  671. // Now build the bit field.
  672. APInt Bitfield(Width, 0);
  673. for (char C : Str)
  674. Bitfield.setBit((unsigned char)C);
  675. Value *BitfieldC = B.getInt(Bitfield);
  676. // Adjust width of "C" to the bitfield width, then mask off the high bits.
  677. Value *C = B.CreateZExtOrTrunc(CI->getArgOperand(1), BitfieldC->getType());
  678. C = B.CreateAnd(C, B.getIntN(Width, 0xFF));
  679. // First check that the bit field access is within bounds.
  680. Value *Bounds = B.CreateICmp(ICmpInst::ICMP_ULT, C, B.getIntN(Width, Width),
  681. "memchr.bounds");
  682. // Create code that checks if the given bit is set in the field.
  683. Value *Shl = B.CreateShl(B.getIntN(Width, 1ULL), C);
  684. Value *Bits = B.CreateIsNotNull(B.CreateAnd(Shl, BitfieldC), "memchr.bits");
  685. // Finally merge both checks and cast to pointer type. The inttoptr
  686. // implicitly zexts the i1 to intptr type.
  687. return B.CreateIntToPtr(B.CreateAnd(Bounds, Bits, "memchr"), CI->getType());
  688. }
  689. // Check if all arguments are constants. If so, we can constant fold.
  690. if (!CharC)
  691. return nullptr;
  692. // Compute the offset.
  693. size_t I = Str.find(CharC->getSExtValue() & 0xFF);
  694. if (I == StringRef::npos) // Didn't find the char. memchr returns null.
  695. return Constant::getNullValue(CI->getType());
  696. // memchr(s+n,c,l) -> gep(s+n+i,c)
  697. return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "memchr");
  698. }
  699. static Value *optimizeMemCmpConstantSize(CallInst *CI, Value *LHS, Value *RHS,
  700. uint64_t Len, IRBuilder<> &B,
  701. const DataLayout &DL) {
  702. if (Len == 0) // memcmp(s1,s2,0) -> 0
  703. return Constant::getNullValue(CI->getType());
  704. // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
  705. if (Len == 1) {
  706. Value *LHSV =
  707. B.CreateZExt(B.CreateLoad(B.getInt8Ty(), castToCStr(LHS, B), "lhsc"),
  708. CI->getType(), "lhsv");
  709. Value *RHSV =
  710. B.CreateZExt(B.CreateLoad(B.getInt8Ty(), castToCStr(RHS, B), "rhsc"),
  711. CI->getType(), "rhsv");
  712. return B.CreateSub(LHSV, RHSV, "chardiff");
  713. }
  714. // memcmp(S1,S2,N/8)==0 -> (*(intN_t*)S1 != *(intN_t*)S2)==0
  715. // TODO: The case where both inputs are constants does not need to be limited
  716. // to legal integers or equality comparison. See block below this.
  717. if (DL.isLegalInteger(Len * 8) && isOnlyUsedInZeroEqualityComparison(CI)) {
  718. IntegerType *IntType = IntegerType::get(CI->getContext(), Len * 8);
  719. unsigned PrefAlignment = DL.getPrefTypeAlignment(IntType);
  720. // First, see if we can fold either argument to a constant.
  721. Value *LHSV = nullptr;
  722. if (auto *LHSC = dyn_cast<Constant>(LHS)) {
  723. LHSC = ConstantExpr::getBitCast(LHSC, IntType->getPointerTo());
  724. LHSV = ConstantFoldLoadFromConstPtr(LHSC, IntType, DL);
  725. }
  726. Value *RHSV = nullptr;
  727. if (auto *RHSC = dyn_cast<Constant>(RHS)) {
  728. RHSC = ConstantExpr::getBitCast(RHSC, IntType->getPointerTo());
  729. RHSV = ConstantFoldLoadFromConstPtr(RHSC, IntType, DL);
  730. }
  731. // Don't generate unaligned loads. If either source is constant data,
  732. // alignment doesn't matter for that source because there is no load.
  733. if ((LHSV || getKnownAlignment(LHS, DL, CI) >= PrefAlignment) &&
  734. (RHSV || getKnownAlignment(RHS, DL, CI) >= PrefAlignment)) {
  735. if (!LHSV) {
  736. Type *LHSPtrTy =
  737. IntType->getPointerTo(LHS->getType()->getPointerAddressSpace());
  738. LHSV = B.CreateLoad(IntType, B.CreateBitCast(LHS, LHSPtrTy), "lhsv");
  739. }
  740. if (!RHSV) {
  741. Type *RHSPtrTy =
  742. IntType->getPointerTo(RHS->getType()->getPointerAddressSpace());
  743. RHSV = B.CreateLoad(IntType, B.CreateBitCast(RHS, RHSPtrTy), "rhsv");
  744. }
  745. return B.CreateZExt(B.CreateICmpNE(LHSV, RHSV), CI->getType(), "memcmp");
  746. }
  747. }
  748. // Constant folding: memcmp(x, y, Len) -> constant (all arguments are const).
  749. // TODO: This is limited to i8 arrays.
  750. StringRef LHSStr, RHSStr;
  751. if (getConstantStringInfo(LHS, LHSStr) &&
  752. getConstantStringInfo(RHS, RHSStr)) {
  753. // Make sure we're not reading out-of-bounds memory.
  754. if (Len > LHSStr.size() || Len > RHSStr.size())
  755. return nullptr;
  756. // Fold the memcmp and normalize the result. This way we get consistent
  757. // results across multiple platforms.
  758. uint64_t Ret = 0;
  759. int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);
  760. if (Cmp < 0)
  761. Ret = -1;
  762. else if (Cmp > 0)
  763. Ret = 1;
  764. return ConstantInt::get(CI->getType(), Ret);
  765. }
  766. return nullptr;
  767. }
  768. Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilder<> &B) {
  769. Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
  770. Value *Size = CI->getArgOperand(2);
  771. if (LHS == RHS) // memcmp(s,s,x) -> 0
  772. return Constant::getNullValue(CI->getType());
  773. // Handle constant lengths.
  774. if (ConstantInt *LenC = dyn_cast<ConstantInt>(Size))
  775. if (Value *Res = optimizeMemCmpConstantSize(CI, LHS, RHS,
  776. LenC->getZExtValue(), B, DL))
  777. return Res;
  778. // memcmp(x, y, Len) == 0 -> bcmp(x, y, Len) == 0
  779. // `bcmp` can be more efficient than memcmp because it only has to know that
  780. // there is a difference, not where it is.
  781. if (isOnlyUsedInZeroEqualityComparison(CI) && TLI->has(LibFunc_bcmp)) {
  782. return emitBCmp(LHS, RHS, Size, B, DL, TLI);
  783. }
  784. return nullptr;
  785. }
  786. Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilder<> &B) {
  787. // memcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n)
  788. B.CreateMemCpy(CI->getArgOperand(0), 1, CI->getArgOperand(1), 1,
  789. CI->getArgOperand(2));
  790. return CI->getArgOperand(0);
  791. }
  792. Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilder<> &B) {
  793. // memmove(x, y, n) -> llvm.memmove(align 1 x, align 1 y, n)
  794. B.CreateMemMove(CI->getArgOperand(0), 1, CI->getArgOperand(1), 1,
  795. CI->getArgOperand(2));
  796. return CI->getArgOperand(0);
  797. }
  798. /// Fold memset[_chk](malloc(n), 0, n) --> calloc(1, n).
  799. Value *LibCallSimplifier::foldMallocMemset(CallInst *Memset, IRBuilder<> &B) {
  800. // This has to be a memset of zeros (bzero).
  801. auto *FillValue = dyn_cast<ConstantInt>(Memset->getArgOperand(1));
  802. if (!FillValue || FillValue->getZExtValue() != 0)
  803. return nullptr;
  804. // TODO: We should handle the case where the malloc has more than one use.
  805. // This is necessary to optimize common patterns such as when the result of
  806. // the malloc is checked against null or when a memset intrinsic is used in
  807. // place of a memset library call.
  808. auto *Malloc = dyn_cast<CallInst>(Memset->getArgOperand(0));
  809. if (!Malloc || !Malloc->hasOneUse())
  810. return nullptr;
  811. // Is the inner call really malloc()?
  812. Function *InnerCallee = Malloc->getCalledFunction();
  813. if (!InnerCallee)
  814. return nullptr;
  815. LibFunc Func;
  816. if (!TLI->getLibFunc(*InnerCallee, Func) || !TLI->has(Func) ||
  817. Func != LibFunc_malloc)
  818. return nullptr;
  819. // The memset must cover the same number of bytes that are malloc'd.
  820. if (Memset->getArgOperand(2) != Malloc->getArgOperand(0))
  821. return nullptr;
  822. // Replace the malloc with a calloc. We need the data layout to know what the
  823. // actual size of a 'size_t' parameter is.
  824. B.SetInsertPoint(Malloc->getParent(), ++Malloc->getIterator());
  825. const DataLayout &DL = Malloc->getModule()->getDataLayout();
  826. IntegerType *SizeType = DL.getIntPtrType(B.GetInsertBlock()->getContext());
  827. Value *Calloc = emitCalloc(ConstantInt::get(SizeType, 1),
  828. Malloc->getArgOperand(0), Malloc->getAttributes(),
  829. B, *TLI);
  830. if (!Calloc)
  831. return nullptr;
  832. Malloc->replaceAllUsesWith(Calloc);
  833. eraseFromParent(Malloc);
  834. return Calloc;
  835. }
  836. Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B) {
  837. if (auto *Calloc = foldMallocMemset(CI, B))
  838. return Calloc;
  839. // memset(p, v, n) -> llvm.memset(align 1 p, v, n)
  840. Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
  841. B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
  842. return CI->getArgOperand(0);
  843. }
  844. Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilder<> &B) {
  845. if (isa<ConstantPointerNull>(CI->getArgOperand(0)))
  846. return emitMalloc(CI->getArgOperand(1), B, DL, TLI);
  847. return nullptr;
  848. }
  849. //===----------------------------------------------------------------------===//
  850. // Math Library Optimizations
  851. //===----------------------------------------------------------------------===//
  852. // Replace a libcall \p CI with a call to intrinsic \p IID
  853. static Value *replaceUnaryCall(CallInst *CI, IRBuilder<> &B, Intrinsic::ID IID) {
  854. // Propagate fast-math flags from the existing call to the new call.
  855. IRBuilder<>::FastMathFlagGuard Guard(B);
  856. B.setFastMathFlags(CI->getFastMathFlags());
  857. Module *M = CI->getModule();
  858. Value *V = CI->getArgOperand(0);
  859. Function *F = Intrinsic::getDeclaration(M, IID, CI->getType());
  860. CallInst *NewCall = B.CreateCall(F, V);
  861. NewCall->takeName(CI);
  862. return NewCall;
  863. }
  864. /// Return a variant of Val with float type.
  865. /// Currently this works in two cases: If Val is an FPExtension of a float
  866. /// value to something bigger, simply return the operand.
  867. /// If Val is a ConstantFP but can be converted to a float ConstantFP without
  868. /// loss of precision do so.
  869. static Value *valueHasFloatPrecision(Value *Val) {
  870. if (FPExtInst *Cast = dyn_cast<FPExtInst>(Val)) {
  871. Value *Op = Cast->getOperand(0);
  872. if (Op->getType()->isFloatTy())
  873. return Op;
  874. }
  875. if (ConstantFP *Const = dyn_cast<ConstantFP>(Val)) {
  876. APFloat F = Const->getValueAPF();
  877. bool losesInfo;
  878. (void)F.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
  879. &losesInfo);
  880. if (!losesInfo)
  881. return ConstantFP::get(Const->getContext(), F);
  882. }
  883. return nullptr;
  884. }
  885. /// Shrink double -> float functions.
  886. static Value *optimizeDoubleFP(CallInst *CI, IRBuilder<> &B,
  887. bool isBinary, bool isPrecise = false) {
  888. if (!CI->getType()->isDoubleTy())
  889. return nullptr;
  890. // If not all the uses of the function are converted to float, then bail out.
  891. // This matters if the precision of the result is more important than the
  892. // precision of the arguments.
  893. if (isPrecise)
  894. for (User *U : CI->users()) {
  895. FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);
  896. if (!Cast || !Cast->getType()->isFloatTy())
  897. return nullptr;
  898. }
  899. // If this is something like 'g((double) float)', convert to 'gf(float)'.
  900. Value *V[2];
  901. V[0] = valueHasFloatPrecision(CI->getArgOperand(0));
  902. V[1] = isBinary ? valueHasFloatPrecision(CI->getArgOperand(1)) : nullptr;
  903. if (!V[0] || (isBinary && !V[1]))
  904. return nullptr;
  905. // If call isn't an intrinsic, check that it isn't within a function with the
  906. // same name as the float version of this call, otherwise the result is an
  907. // infinite loop. For example, from MinGW-w64:
  908. //
  909. // float expf(float val) { return (float) exp((double) val); }
  910. Function *CalleeFn = CI->getCalledFunction();
  911. StringRef CalleeNm = CalleeFn->getName();
  912. AttributeList CalleeAt = CalleeFn->getAttributes();
  913. if (CalleeFn && !CalleeFn->isIntrinsic()) {
  914. const Function *Fn = CI->getFunction();
  915. StringRef FnName = Fn->getName();
  916. if (FnName.back() == 'f' &&
  917. FnName.size() == (CalleeNm.size() + 1) &&
  918. FnName.startswith(CalleeNm))
  919. return nullptr;
  920. }
  921. // Propagate the math semantics from the current function to the new function.
  922. IRBuilder<>::FastMathFlagGuard Guard(B);
  923. B.setFastMathFlags(CI->getFastMathFlags());
  924. // g((double) float) -> (double) gf(float)
  925. Value *R;
  926. if (CalleeFn->isIntrinsic()) {
  927. Module *M = CI->getModule();
  928. Intrinsic::ID IID = CalleeFn->getIntrinsicID();
  929. Function *Fn = Intrinsic::getDeclaration(M, IID, B.getFloatTy());
  930. R = isBinary ? B.CreateCall(Fn, V) : B.CreateCall(Fn, V[0]);
  931. }
  932. else
  933. R = isBinary ? emitBinaryFloatFnCall(V[0], V[1], CalleeNm, B, CalleeAt)
  934. : emitUnaryFloatFnCall(V[0], CalleeNm, B, CalleeAt);
  935. return B.CreateFPExt(R, B.getDoubleTy());
  936. }
  937. /// Shrink double -> float for unary functions.
  938. static Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B,
  939. bool isPrecise = false) {
  940. return optimizeDoubleFP(CI, B, false, isPrecise);
  941. }
  942. /// Shrink double -> float for binary functions.
  943. static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B,
  944. bool isPrecise = false) {
  945. return optimizeDoubleFP(CI, B, true, isPrecise);
  946. }
  947. // cabs(z) -> sqrt((creal(z)*creal(z)) + (cimag(z)*cimag(z)))
  948. Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilder<> &B) {
  949. if (!CI->isFast())
  950. return nullptr;
  951. // Propagate fast-math flags from the existing call to new instructions.
  952. IRBuilder<>::FastMathFlagGuard Guard(B);
  953. B.setFastMathFlags(CI->getFastMathFlags());
  954. Value *Real, *Imag;
  955. if (CI->getNumArgOperands() == 1) {
  956. Value *Op = CI->getArgOperand(0);
  957. assert(Op->getType()->isArrayTy() && "Unexpected signature for cabs!");
  958. Real = B.CreateExtractValue(Op, 0, "real");
  959. Imag = B.CreateExtractValue(Op, 1, "imag");
  960. } else {
  961. assert(CI->getNumArgOperands() == 2 && "Unexpected signature for cabs!");
  962. Real = CI->getArgOperand(0);
  963. Imag = CI->getArgOperand(1);
  964. }
  965. Value *RealReal = B.CreateFMul(Real, Real);
  966. Value *ImagImag = B.CreateFMul(Imag, Imag);
  967. Function *FSqrt = Intrinsic::getDeclaration(CI->getModule(), Intrinsic::sqrt,
  968. CI->getType());
  969. return B.CreateCall(FSqrt, B.CreateFAdd(RealReal, ImagImag), "cabs");
  970. }
  971. static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func,
  972. IRBuilder<> &B) {
  973. if (!isa<FPMathOperator>(Call))
  974. return nullptr;
  975. IRBuilder<>::FastMathFlagGuard Guard(B);
  976. B.setFastMathFlags(Call->getFastMathFlags());
  977. // TODO: Can this be shared to also handle LLVM intrinsics?
  978. Value *X;
  979. switch (Func) {
  980. case LibFunc_sin:
  981. case LibFunc_sinf:
  982. case LibFunc_sinl:
  983. case LibFunc_tan:
  984. case LibFunc_tanf:
  985. case LibFunc_tanl:
  986. // sin(-X) --> -sin(X)
  987. // tan(-X) --> -tan(X)
  988. if (match(Call->getArgOperand(0), m_OneUse(m_FNeg(m_Value(X)))))
  989. return B.CreateFNeg(B.CreateCall(Call->getCalledFunction(), X));
  990. break;
  991. case LibFunc_cos:
  992. case LibFunc_cosf:
  993. case LibFunc_cosl:
  994. // cos(-X) --> cos(X)
  995. if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))))
  996. return B.CreateCall(Call->getCalledFunction(), X, "cos");
  997. break;
  998. default:
  999. break;
  1000. }
  1001. return nullptr;
  1002. }
  1003. static Value *getPow(Value *InnerChain[33], unsigned Exp, IRBuilder<> &B) {
  1004. // Multiplications calculated using Addition Chains.
  1005. // Refer: http://wwwhomes.uni-bielefeld.de/achim/addition_chain.html
  1006. assert(Exp != 0 && "Incorrect exponent 0 not handled");
  1007. if (InnerChain[Exp])
  1008. return InnerChain[Exp];
  1009. static const unsigned AddChain[33][2] = {
  1010. {0, 0}, // Unused.
  1011. {0, 0}, // Unused (base case = pow1).
  1012. {1, 1}, // Unused (pre-computed).
  1013. {1, 2}, {2, 2}, {2, 3}, {3, 3}, {2, 5}, {4, 4},
  1014. {1, 8}, {5, 5}, {1, 10}, {6, 6}, {4, 9}, {7, 7},
  1015. {3, 12}, {8, 8}, {8, 9}, {2, 16}, {1, 18}, {10, 10},
  1016. {6, 15}, {11, 11}, {3, 20}, {12, 12}, {8, 17}, {13, 13},
  1017. {3, 24}, {14, 14}, {4, 25}, {15, 15}, {3, 28}, {16, 16},
  1018. };
  1019. InnerChain[Exp] = B.CreateFMul(getPow(InnerChain, AddChain[Exp][0], B),
  1020. getPow(InnerChain, AddChain[Exp][1], B));
  1021. return InnerChain[Exp];
  1022. }
  1023. /// Use exp{,2}(x * y) for pow(exp{,2}(x), y);
  1024. /// exp2(n * x) for pow(2.0 ** n, x); exp10(x) for pow(10.0, x).
  1025. Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilder<> &B) {
  1026. Value *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
  1027. AttributeList Attrs = Pow->getCalledFunction()->getAttributes();
  1028. Module *Mod = Pow->getModule();
  1029. Type *Ty = Pow->getType();
  1030. bool Ignored;
  1031. // Evaluate special cases related to a nested function as the base.
  1032. // pow(exp(x), y) -> exp(x * y)
  1033. // pow(exp2(x), y) -> exp2(x * y)
  1034. // If exp{,2}() is used only once, it is better to fold two transcendental
  1035. // math functions into one. If used again, exp{,2}() would still have to be
  1036. // called with the original argument, then keep both original transcendental
  1037. // functions. However, this transformation is only safe with fully relaxed
  1038. // math semantics, since, besides rounding differences, it changes overflow
  1039. // and underflow behavior quite dramatically. For example:
  1040. // pow(exp(1000), 0.001) = pow(inf, 0.001) = inf
  1041. // Whereas:
  1042. // exp(1000 * 0.001) = exp(1)
  1043. // TODO: Loosen the requirement for fully relaxed math semantics.
  1044. // TODO: Handle exp10() when more targets have it available.
  1045. CallInst *BaseFn = dyn_cast<CallInst>(Base);
  1046. if (BaseFn && BaseFn->hasOneUse() && BaseFn->isFast() && Pow->isFast()) {
  1047. LibFunc LibFn;
  1048. Function *CalleeFn = BaseFn->getCalledFunction();
  1049. if (CalleeFn &&
  1050. TLI->getLibFunc(CalleeFn->getName(), LibFn) && TLI->has(LibFn)) {
  1051. StringRef ExpName;
  1052. Intrinsic::ID ID;
  1053. Value *ExpFn;
  1054. LibFunc LibFnFloat;
  1055. LibFunc LibFnDouble;
  1056. LibFunc LibFnLongDouble;
  1057. switch (LibFn) {
  1058. default:
  1059. return nullptr;
  1060. case LibFunc_expf: case LibFunc_exp: case LibFunc_expl:
  1061. ExpName = TLI->getName(LibFunc_exp);
  1062. ID = Intrinsic::exp;
  1063. LibFnFloat = LibFunc_expf;
  1064. LibFnDouble = LibFunc_exp;
  1065. LibFnLongDouble = LibFunc_expl;
  1066. break;
  1067. case LibFunc_exp2f: case LibFunc_exp2: case LibFunc_exp2l:
  1068. ExpName = TLI->getName(LibFunc_exp2);
  1069. ID = Intrinsic::exp2;
  1070. LibFnFloat = LibFunc_exp2f;
  1071. LibFnDouble = LibFunc_exp2;
  1072. LibFnLongDouble = LibFunc_exp2l;
  1073. break;
  1074. }
  1075. // Create new exp{,2}() with the product as its argument.
  1076. Value *FMul = B.CreateFMul(BaseFn->getArgOperand(0), Expo, "mul");
  1077. ExpFn = BaseFn->doesNotAccessMemory()
  1078. ? B.CreateCall(Intrinsic::getDeclaration(Mod, ID, Ty),
  1079. FMul, ExpName)
  1080. : emitUnaryFloatFnCall(FMul, TLI, LibFnDouble, LibFnFloat,
  1081. LibFnLongDouble, B,
  1082. BaseFn->getAttributes());
  1083. // Since the new exp{,2}() is different from the original one, dead code
  1084. // elimination cannot be trusted to remove it, since it may have side
  1085. // effects (e.g., errno). When the only consumer for the original
  1086. // exp{,2}() is pow(), then it has to be explicitly erased.
  1087. BaseFn->replaceAllUsesWith(ExpFn);
  1088. eraseFromParent(BaseFn);
  1089. return ExpFn;
  1090. }
  1091. }
  1092. // Evaluate special cases related to a constant base.
  1093. const APFloat *BaseF;
  1094. if (!match(Pow->getArgOperand(0), m_APFloat(BaseF)))
  1095. return nullptr;
  1096. // pow(2.0 ** n, x) -> exp2(n * x)
  1097. if (hasUnaryFloatFn(TLI, Ty, LibFunc_exp2, LibFunc_exp2f, LibFunc_exp2l)) {
  1098. APFloat BaseR = APFloat(1.0);
  1099. BaseR.convert(BaseF->getSemantics(), APFloat::rmTowardZero, &Ignored);
  1100. BaseR = BaseR / *BaseF;
  1101. bool IsInteger = BaseF->isInteger(),
  1102. IsReciprocal = BaseR.isInteger();
  1103. const APFloat *NF = IsReciprocal ? &BaseR : BaseF;
  1104. APSInt NI(64, false);
  1105. if ((IsInteger || IsReciprocal) &&
  1106. !NF->convertToInteger(NI, APFloat::rmTowardZero, &Ignored) &&
  1107. NI > 1 && NI.isPowerOf2()) {
  1108. double N = NI.logBase2() * (IsReciprocal ? -1.0 : 1.0);
  1109. Value *FMul = B.CreateFMul(Expo, ConstantFP::get(Ty, N), "mul");
  1110. if (Pow->doesNotAccessMemory())
  1111. return B.CreateCall(Intrinsic::getDeclaration(Mod, Intrinsic::exp2, Ty),
  1112. FMul, "exp2");
  1113. else
  1114. return emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2, LibFunc_exp2f,
  1115. LibFunc_exp2l, B, Attrs);
  1116. }
  1117. }
  1118. // pow(10.0, x) -> exp10(x)
  1119. // TODO: There is no exp10() intrinsic yet, but some day there shall be one.
  1120. if (match(Base, m_SpecificFP(10.0)) &&
  1121. hasUnaryFloatFn(TLI, Ty, LibFunc_exp10, LibFunc_exp10f, LibFunc_exp10l))
  1122. return emitUnaryFloatFnCall(Expo, TLI, LibFunc_exp10, LibFunc_exp10f,
  1123. LibFunc_exp10l, B, Attrs);
  1124. return nullptr;
  1125. }
  1126. static Value *getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno,
  1127. Module *M, IRBuilder<> &B,
  1128. const TargetLibraryInfo *TLI) {
  1129. // If errno is never set, then use the intrinsic for sqrt().
  1130. if (NoErrno) {
  1131. Function *SqrtFn =
  1132. Intrinsic::getDeclaration(M, Intrinsic::sqrt, V->getType());
  1133. return B.CreateCall(SqrtFn, V, "sqrt");
  1134. }
  1135. // Otherwise, use the libcall for sqrt().
  1136. if (hasUnaryFloatFn(TLI, V->getType(), LibFunc_sqrt, LibFunc_sqrtf,
  1137. LibFunc_sqrtl))
  1138. // TODO: We also should check that the target can in fact lower the sqrt()
  1139. // libcall. We currently have no way to ask this question, so we ask if
  1140. // the target has a sqrt() libcall, which is not exactly the same.
  1141. return emitUnaryFloatFnCall(V, TLI, LibFunc_sqrt, LibFunc_sqrtf,
  1142. LibFunc_sqrtl, B, Attrs);
  1143. return nullptr;
  1144. }
  1145. /// Use square root in place of pow(x, +/-0.5).
  1146. Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilder<> &B) {
  1147. Value *Sqrt, *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
  1148. AttributeList Attrs = Pow->getCalledFunction()->getAttributes();
  1149. Module *Mod = Pow->getModule();
  1150. Type *Ty = Pow->getType();
  1151. const APFloat *ExpoF;
  1152. if (!match(Expo, m_APFloat(ExpoF)) ||
  1153. (!ExpoF->isExactlyValue(0.5) && !ExpoF->isExactlyValue(-0.5)))
  1154. return nullptr;
  1155. Sqrt = getSqrtCall(Base, Attrs, Pow->doesNotAccessMemory(), Mod, B, TLI);
  1156. if (!Sqrt)
  1157. return nullptr;
  1158. // Handle signed zero base by expanding to fabs(sqrt(x)).
  1159. if (!Pow->hasNoSignedZeros()) {
  1160. Function *FAbsFn = Intrinsic::getDeclaration(Mod, Intrinsic::fabs, Ty);
  1161. Sqrt = B.CreateCall(FAbsFn, Sqrt, "abs");
  1162. }
  1163. // Handle non finite base by expanding to
  1164. // (x == -infinity ? +infinity : sqrt(x)).
  1165. if (!Pow->hasNoInfs()) {
  1166. Value *PosInf = ConstantFP::getInfinity(Ty),
  1167. *NegInf = ConstantFP::getInfinity(Ty, true);
  1168. Value *FCmp = B.CreateFCmpOEQ(Base, NegInf, "isinf");
  1169. Sqrt = B.CreateSelect(FCmp, PosInf, Sqrt);
  1170. }
  1171. // If the exponent is negative, then get the reciprocal.
  1172. if (ExpoF->isNegative())
  1173. Sqrt = B.CreateFDiv(ConstantFP::get(Ty, 1.0), Sqrt, "reciprocal");
  1174. return Sqrt;
  1175. }
  1176. Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilder<> &B) {
  1177. Value *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
  1178. Function *Callee = Pow->getCalledFunction();
  1179. StringRef Name = Callee->getName();
  1180. Type *Ty = Pow->getType();
  1181. Value *Shrunk = nullptr;
  1182. bool Ignored;
  1183. // Bail out if simplifying libcalls to pow() is disabled.
  1184. if (!hasUnaryFloatFn(TLI, Ty, LibFunc_pow, LibFunc_powf, LibFunc_powl))
  1185. return nullptr;
  1186. // Propagate the math semantics from the call to any created instructions.
  1187. IRBuilder<>::FastMathFlagGuard Guard(B);
  1188. B.setFastMathFlags(Pow->getFastMathFlags());
  1189. // Shrink pow() to powf() if the arguments are single precision,
  1190. // unless the result is expected to be double precision.
  1191. if (UnsafeFPShrink &&
  1192. Name == TLI->getName(LibFunc_pow) && hasFloatVersion(Name))
  1193. Shrunk = optimizeBinaryDoubleFP(Pow, B, true);
  1194. // Evaluate special cases related to the base.
  1195. // pow(1.0, x) -> 1.0
  1196. if (match(Base, m_FPOne()))
  1197. return Base;
  1198. if (Value *Exp = replacePowWithExp(Pow, B))
  1199. return Exp;
  1200. // Evaluate special cases related to the exponent.
  1201. // pow(x, -1.0) -> 1.0 / x
  1202. if (match(Expo, m_SpecificFP(-1.0)))
  1203. return B.CreateFDiv(ConstantFP::get(Ty, 1.0), Base, "reciprocal");
  1204. // pow(x, 0.0) -> 1.0
  1205. if (match(Expo, m_SpecificFP(0.0)))
  1206. return ConstantFP::get(Ty, 1.0);
  1207. // pow(x, 1.0) -> x
  1208. if (match(Expo, m_FPOne()))
  1209. return Base;
  1210. // pow(x, 2.0) -> x * x
  1211. if (match(Expo, m_SpecificFP(2.0)))
  1212. return B.CreateFMul(Base, Base, "square");
  1213. if (Value *Sqrt = replacePowWithSqrt(Pow, B))
  1214. return Sqrt;
  1215. // pow(x, n) -> x * x * x * ...
  1216. const APFloat *ExpoF;
  1217. if (Pow->isFast() && match(Expo, m_APFloat(ExpoF))) {
  1218. // We limit to a max of 7 multiplications, thus the maximum exponent is 32.
  1219. // If the exponent is an integer+0.5 we generate a call to sqrt and an
  1220. // additional fmul.
  1221. // TODO: This whole transformation should be backend specific (e.g. some
  1222. // backends might prefer libcalls or the limit for the exponent might
  1223. // be different) and it should also consider optimizing for size.
  1224. APFloat LimF(ExpoF->getSemantics(), 33.0),
  1225. ExpoA(abs(*ExpoF));
  1226. if (ExpoA.compare(LimF) == APFloat::cmpLessThan) {
  1227. // This transformation applies to integer or integer+0.5 exponents only.
  1228. // For integer+0.5, we create a sqrt(Base) call.
  1229. Value *Sqrt = nullptr;
  1230. if (!ExpoA.isInteger()) {
  1231. APFloat Expo2 = ExpoA;
  1232. // To check if ExpoA is an integer + 0.5, we add it to itself. If there
  1233. // is no floating point exception and the result is an integer, then
  1234. // ExpoA == integer + 0.5
  1235. if (Expo2.add(ExpoA, APFloat::rmNearestTiesToEven) != APFloat::opOK)
  1236. return nullptr;
  1237. if (!Expo2.isInteger())
  1238. return nullptr;
  1239. Sqrt =
  1240. getSqrtCall(Base, Pow->getCalledFunction()->getAttributes(),
  1241. Pow->doesNotAccessMemory(), Pow->getModule(), B, TLI);
  1242. }
  1243. // We will memoize intermediate products of the Addition Chain.
  1244. Value *InnerChain[33] = {nullptr};
  1245. InnerChain[1] = Base;
  1246. InnerChain[2] = B.CreateFMul(Base, Base, "square");
  1247. // We cannot readily convert a non-double type (like float) to a double.
  1248. // So we first convert it to something which could be converted to double.
  1249. ExpoA.convert(APFloat::IEEEdouble(), APFloat::rmTowardZero, &Ignored);
  1250. Value *FMul = getPow(InnerChain, ExpoA.convertToDouble(), B);
  1251. // Expand pow(x, y+0.5) to pow(x, y) * sqrt(x).
  1252. if (Sqrt)
  1253. FMul = B.CreateFMul(FMul, Sqrt);
  1254. // If the exponent is negative, then get the reciprocal.
  1255. if (ExpoF->isNegative())
  1256. FMul = B.CreateFDiv(ConstantFP::get(Ty, 1.0), FMul, "reciprocal");
  1257. return FMul;
  1258. }
  1259. }
  1260. return Shrunk;
  1261. }
  1262. Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilder<> &B) {
  1263. Function *Callee = CI->getCalledFunction();
  1264. Value *Ret = nullptr;
  1265. StringRef Name = Callee->getName();
  1266. if (UnsafeFPShrink && Name == "exp2" && hasFloatVersion(Name))
  1267. Ret = optimizeUnaryDoubleFP(CI, B, true);
  1268. Value *Op = CI->getArgOperand(0);
  1269. // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
  1270. // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
  1271. LibFunc LdExp = LibFunc_ldexpl;
  1272. if (Op->getType()->isFloatTy())
  1273. LdExp = LibFunc_ldexpf;
  1274. else if (Op->getType()->isDoubleTy())
  1275. LdExp = LibFunc_ldexp;
  1276. if (TLI->has(LdExp)) {
  1277. Value *LdExpArg = nullptr;
  1278. if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
  1279. if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
  1280. LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
  1281. } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
  1282. if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
  1283. LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
  1284. }
  1285. if (LdExpArg) {
  1286. Constant *One = ConstantFP::get(CI->getContext(), APFloat(1.0f));
  1287. if (!Op->getType()->isFloatTy())
  1288. One = ConstantExpr::getFPExtend(One, Op->getType());
  1289. Module *M = CI->getModule();
  1290. FunctionCallee NewCallee = M->getOrInsertFunction(
  1291. TLI->getName(LdExp), Op->getType(), Op->getType(), B.getInt32Ty());
  1292. CallInst *CI = B.CreateCall(NewCallee, {One, LdExpArg});
  1293. if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
  1294. CI->setCallingConv(F->getCallingConv());
  1295. return CI;
  1296. }
  1297. }
  1298. return Ret;
  1299. }
  1300. Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilder<> &B) {
  1301. Function *Callee = CI->getCalledFunction();
  1302. // If we can shrink the call to a float function rather than a double
  1303. // function, do that first.
  1304. StringRef Name = Callee->getName();
  1305. if ((Name == "fmin" || Name == "fmax") && hasFloatVersion(Name))
  1306. if (Value *Ret = optimizeBinaryDoubleFP(CI, B))
  1307. return Ret;
  1308. IRBuilder<>::FastMathFlagGuard Guard(B);
  1309. FastMathFlags FMF;
  1310. if (CI->isFast()) {
  1311. // If the call is 'fast', then anything we create here will also be 'fast'.
  1312. FMF.setFast();
  1313. } else {
  1314. // At a minimum, no-nans-fp-math must be true.
  1315. if (!CI->hasNoNaNs())
  1316. return nullptr;
  1317. // No-signed-zeros is implied by the definitions of fmax/fmin themselves:
  1318. // "Ideally, fmax would be sensitive to the sign of zero, for example
  1319. // fmax(-0. 0, +0. 0) would return +0; however, implementation in software
  1320. // might be impractical."
  1321. FMF.setNoSignedZeros();
  1322. FMF.setNoNaNs();
  1323. }
  1324. B.setFastMathFlags(FMF);
  1325. // We have a relaxed floating-point environment. We can ignore NaN-handling
  1326. // and transform to a compare and select. We do not have to consider errno or
  1327. // exceptions, because fmin/fmax do not have those.
  1328. Value *Op0 = CI->getArgOperand(0);
  1329. Value *Op1 = CI->getArgOperand(1);
  1330. Value *Cmp = Callee->getName().startswith("fmin") ?
  1331. B.CreateFCmpOLT(Op0, Op1) : B.CreateFCmpOGT(Op0, Op1);
  1332. return B.CreateSelect(Cmp, Op0, Op1);
  1333. }
  1334. Value *LibCallSimplifier::optimizeLog(CallInst *CI, IRBuilder<> &B) {
  1335. Function *Callee = CI->getCalledFunction();
  1336. Value *Ret = nullptr;
  1337. StringRef Name = Callee->getName();
  1338. if (UnsafeFPShrink && hasFloatVersion(Name))
  1339. Ret = optimizeUnaryDoubleFP(CI, B, true);
  1340. if (!CI->isFast())
  1341. return Ret;
  1342. Value *Op1 = CI->getArgOperand(0);
  1343. auto *OpC = dyn_cast<CallInst>(Op1);
  1344. // The earlier call must also be 'fast' in order to do these transforms.
  1345. if (!OpC || !OpC->isFast())
  1346. return Ret;
  1347. // log(pow(x,y)) -> y*log(x)
  1348. // This is only applicable to log, log2, log10.
  1349. if (Name != "log" && Name != "log2" && Name != "log10")
  1350. return Ret;
  1351. IRBuilder<>::FastMathFlagGuard Guard(B);
  1352. FastMathFlags FMF;
  1353. FMF.setFast();
  1354. B.setFastMathFlags(FMF);
  1355. LibFunc Func;
  1356. Function *F = OpC->getCalledFunction();
  1357. if (F && ((TLI->getLibFunc(F->getName(), Func) && TLI->has(Func) &&
  1358. Func == LibFunc_pow) || F->getIntrinsicID() == Intrinsic::pow))
  1359. return B.CreateFMul(OpC->getArgOperand(1),
  1360. emitUnaryFloatFnCall(OpC->getOperand(0), Callee->getName(), B,
  1361. Callee->getAttributes()), "mul");
  1362. // log(exp2(y)) -> y*log(2)
  1363. if (F && Name == "log" && TLI->getLibFunc(F->getName(), Func) &&
  1364. TLI->has(Func) && Func == LibFunc_exp2)
  1365. return B.CreateFMul(
  1366. OpC->getArgOperand(0),
  1367. emitUnaryFloatFnCall(ConstantFP::get(CI->getType(), 2.0),
  1368. Callee->getName(), B, Callee->getAttributes()),
  1369. "logmul");
  1370. return Ret;
  1371. }
  1372. Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilder<> &B) {
  1373. Function *Callee = CI->getCalledFunction();
  1374. Value *Ret = nullptr;
  1375. // TODO: Once we have a way (other than checking for the existince of the
  1376. // libcall) to tell whether our target can lower @llvm.sqrt, relax the
  1377. // condition below.
  1378. if (TLI->has(LibFunc_sqrtf) && (Callee->getName() == "sqrt" ||
  1379. Callee->getIntrinsicID() == Intrinsic::sqrt))
  1380. Ret = optimizeUnaryDoubleFP(CI, B, true);
  1381. if (!CI->isFast())
  1382. return Ret;
  1383. Instruction *I = dyn_cast<Instruction>(CI->getArgOperand(0));
  1384. if (!I || I->getOpcode() != Instruction::FMul || !I->isFast())
  1385. return Ret;
  1386. // We're looking for a repeated factor in a multiplication tree,
  1387. // so we can do this fold: sqrt(x * x) -> fabs(x);
  1388. // or this fold: sqrt((x * x) * y) -> fabs(x) * sqrt(y).
  1389. Value *Op0 = I->getOperand(0);
  1390. Value *Op1 = I->getOperand(1);
  1391. Value *RepeatOp = nullptr;
  1392. Value *OtherOp = nullptr;
  1393. if (Op0 == Op1) {
  1394. // Simple match: the operands of the multiply are identical.
  1395. RepeatOp = Op0;
  1396. } else {
  1397. // Look for a more complicated pattern: one of the operands is itself
  1398. // a multiply, so search for a common factor in that multiply.
  1399. // Note: We don't bother looking any deeper than this first level or for
  1400. // variations of this pattern because instcombine's visitFMUL and/or the
  1401. // reassociation pass should give us this form.
  1402. Value *OtherMul0, *OtherMul1;
  1403. if (match(Op0, m_FMul(m_Value(OtherMul0), m_Value(OtherMul1)))) {
  1404. // Pattern: sqrt((x * y) * z)
  1405. if (OtherMul0 == OtherMul1 && cast<Instruction>(Op0)->isFast()) {
  1406. // Matched: sqrt((x * x) * z)
  1407. RepeatOp = OtherMul0;
  1408. OtherOp = Op1;
  1409. }
  1410. }
  1411. }
  1412. if (!RepeatOp)
  1413. return Ret;
  1414. // Fast math flags for any created instructions should match the sqrt
  1415. // and multiply.
  1416. IRBuilder<>::FastMathFlagGuard Guard(B);
  1417. B.setFastMathFlags(I->getFastMathFlags());
  1418. // If we found a repeated factor, hoist it out of the square root and
  1419. // replace it with the fabs of that factor.
  1420. Module *M = Callee->getParent();
  1421. Type *ArgType = I->getType();
  1422. Function *Fabs = Intrinsic::getDeclaration(M, Intrinsic::fabs, ArgType);
  1423. Value *FabsCall = B.CreateCall(Fabs, RepeatOp, "fabs");
  1424. if (OtherOp) {
  1425. // If we found a non-repeated factor, we still need to get its square
  1426. // root. We then multiply that by the value that was simplified out
  1427. // of the square root calculation.
  1428. Function *Sqrt = Intrinsic::getDeclaration(M, Intrinsic::sqrt, ArgType);
  1429. Value *SqrtCall = B.CreateCall(Sqrt, OtherOp, "sqrt");
  1430. return B.CreateFMul(FabsCall, SqrtCall);
  1431. }
  1432. return FabsCall;
  1433. }
  1434. // TODO: Generalize to handle any trig function and its inverse.
  1435. Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilder<> &B) {
  1436. Function *Callee = CI->getCalledFunction();
  1437. Value *Ret = nullptr;
  1438. StringRef Name = Callee->getName();
  1439. if (UnsafeFPShrink && Name == "tan" && hasFloatVersion(Name))
  1440. Ret = optimizeUnaryDoubleFP(CI, B, true);
  1441. Value *Op1 = CI->getArgOperand(0);
  1442. auto *OpC = dyn_cast<CallInst>(Op1);
  1443. if (!OpC)
  1444. return Ret;
  1445. // Both calls must be 'fast' in order to remove them.
  1446. if (!CI->isFast() || !OpC->isFast())
  1447. return Ret;
  1448. // tan(atan(x)) -> x
  1449. // tanf(atanf(x)) -> x
  1450. // tanl(atanl(x)) -> x
  1451. LibFunc Func;
  1452. Function *F = OpC->getCalledFunction();
  1453. if (F && TLI->getLibFunc(F->getName(), Func) && TLI->has(Func) &&
  1454. ((Func == LibFunc_atan && Callee->getName() == "tan") ||
  1455. (Func == LibFunc_atanf && Callee->getName() == "tanf") ||
  1456. (Func == LibFunc_atanl && Callee->getName() == "tanl")))
  1457. Ret = OpC->getArgOperand(0);
  1458. return Ret;
  1459. }
  1460. static bool isTrigLibCall(CallInst *CI) {
  1461. // We can only hope to do anything useful if we can ignore things like errno
  1462. // and floating-point exceptions.
  1463. // We already checked the prototype.
  1464. return CI->hasFnAttr(Attribute::NoUnwind) &&
  1465. CI->hasFnAttr(Attribute::ReadNone);
  1466. }
  1467. static void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,
  1468. bool UseFloat, Value *&Sin, Value *&Cos,
  1469. Value *&SinCos) {
  1470. Type *ArgTy = Arg->getType();
  1471. Type *ResTy;
  1472. StringRef Name;
  1473. Triple T(OrigCallee->getParent()->getTargetTriple());
  1474. if (UseFloat) {
  1475. Name = "__sincospif_stret";
  1476. assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");
  1477. // x86_64 can't use {float, float} since that would be returned in both
  1478. // xmm0 and xmm1, which isn't what a real struct would do.
  1479. ResTy = T.getArch() == Triple::x86_64
  1480. ? static_cast<Type *>(VectorType::get(ArgTy, 2))
  1481. : static_cast<Type *>(StructType::get(ArgTy, ArgTy));
  1482. } else {
  1483. Name = "__sincospi_stret";
  1484. ResTy = StructType::get(ArgTy, ArgTy);
  1485. }
  1486. Module *M = OrigCallee->getParent();
  1487. FunctionCallee Callee =
  1488. M->getOrInsertFunction(Name, OrigCallee->getAttributes(), ResTy, ArgTy);
  1489. if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
  1490. // If the argument is an instruction, it must dominate all uses so put our
  1491. // sincos call there.
  1492. B.SetInsertPoint(ArgInst->getParent(), ++ArgInst->getIterator());
  1493. } else {
  1494. // Otherwise (e.g. for a constant) the beginning of the function is as
  1495. // good a place as any.
  1496. BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
  1497. B.SetInsertPoint(&EntryBB, EntryBB.begin());
  1498. }
  1499. SinCos = B.CreateCall(Callee, Arg, "sincospi");
  1500. if (SinCos->getType()->isStructTy()) {
  1501. Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
  1502. Cos = B.CreateExtractValue(SinCos, 1, "cospi");
  1503. } else {
  1504. Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
  1505. "sinpi");
  1506. Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
  1507. "cospi");
  1508. }
  1509. }
  1510. Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, IRBuilder<> &B) {
  1511. // Make sure the prototype is as expected, otherwise the rest of the
  1512. // function is probably invalid and likely to abort.
  1513. if (!isTrigLibCall(CI))
  1514. return nullptr;
  1515. Value *Arg = CI->getArgOperand(0);
  1516. SmallVector<CallInst *, 1> SinCalls;
  1517. SmallVector<CallInst *, 1> CosCalls;
  1518. SmallVector<CallInst *, 1> SinCosCalls;
  1519. bool IsFloat = Arg->getType()->isFloatTy();
  1520. // Look for all compatible sinpi, cospi and sincospi calls with the same
  1521. // argument. If there are enough (in some sense) we can make the
  1522. // substitution.
  1523. Function *F = CI->getFunction();
  1524. for (User *U : Arg->users())
  1525. classifyArgUse(U, F, IsFloat, SinCalls, CosCalls, SinCosCalls);
  1526. // It's only worthwhile if both sinpi and cospi are actually used.
  1527. if (SinCosCalls.empty() && (SinCalls.empty() || CosCalls.empty()))
  1528. return nullptr;
  1529. Value *Sin, *Cos, *SinCos;
  1530. insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos, SinCos);
  1531. auto replaceTrigInsts = [this](SmallVectorImpl<CallInst *> &Calls,
  1532. Value *Res) {
  1533. for (CallInst *C : Calls)
  1534. replaceAllUsesWith(C, Res);
  1535. };
  1536. replaceTrigInsts(SinCalls, Sin);
  1537. replaceTrigInsts(CosCalls, Cos);
  1538. replaceTrigInsts(SinCosCalls, SinCos);
  1539. return nullptr;
  1540. }
  1541. void LibCallSimplifier::classifyArgUse(
  1542. Value *Val, Function *F, bool IsFloat,
  1543. SmallVectorImpl<CallInst *> &SinCalls,
  1544. SmallVectorImpl<CallInst *> &CosCalls,
  1545. SmallVectorImpl<CallInst *> &SinCosCalls) {
  1546. CallInst *CI = dyn_cast<CallInst>(Val);
  1547. if (!CI)
  1548. return;
  1549. // Don't consider calls in other functions.
  1550. if (CI->getFunction() != F)
  1551. return;
  1552. Function *Callee = CI->getCalledFunction();
  1553. LibFunc Func;
  1554. if (!Callee || !TLI->getLibFunc(*Callee, Func) || !TLI->has(Func) ||
  1555. !isTrigLibCall(CI))
  1556. return;
  1557. if (IsFloat) {
  1558. if (Func == LibFunc_sinpif)
  1559. SinCalls.push_back(CI);
  1560. else if (Func == LibFunc_cospif)
  1561. CosCalls.push_back(CI);
  1562. else if (Func == LibFunc_sincospif_stret)
  1563. SinCosCalls.push_back(CI);
  1564. } else {
  1565. if (Func == LibFunc_sinpi)
  1566. SinCalls.push_back(CI);
  1567. else if (Func == LibFunc_cospi)
  1568. CosCalls.push_back(CI);
  1569. else if (Func == LibFunc_sincospi_stret)
  1570. SinCosCalls.push_back(CI);
  1571. }
  1572. }
  1573. //===----------------------------------------------------------------------===//
  1574. // Integer Library Call Optimizations
  1575. //===----------------------------------------------------------------------===//
  1576. Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilder<> &B) {
  1577. // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
  1578. Value *Op = CI->getArgOperand(0);
  1579. Type *ArgType = Op->getType();
  1580. Function *F = Intrinsic::getDeclaration(CI->getCalledFunction()->getParent(),
  1581. Intrinsic::cttz, ArgType);
  1582. Value *V = B.CreateCall(F, {Op, B.getTrue()}, "cttz");
  1583. V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
  1584. V = B.CreateIntCast(V, B.getInt32Ty(), false);
  1585. Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
  1586. return B.CreateSelect(Cond, V, B.getInt32(0));
  1587. }
  1588. Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilder<> &B) {
  1589. // fls(x) -> (i32)(sizeInBits(x) - llvm.ctlz(x, false))
  1590. Value *Op = CI->getArgOperand(0);
  1591. Type *ArgType = Op->getType();
  1592. Function *F = Intrinsic::getDeclaration(CI->getCalledFunction()->getParent(),
  1593. Intrinsic::ctlz, ArgType);
  1594. Value *V = B.CreateCall(F, {Op, B.getFalse()}, "ctlz");
  1595. V = B.CreateSub(ConstantInt::get(V->getType(), ArgType->getIntegerBitWidth()),
  1596. V);
  1597. return B.CreateIntCast(V, CI->getType(), false);
  1598. }
  1599. Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilder<> &B) {
  1600. // abs(x) -> x <s 0 ? -x : x
  1601. // The negation has 'nsw' because abs of INT_MIN is undefined.
  1602. Value *X = CI->getArgOperand(0);
  1603. Value *IsNeg = B.CreateICmpSLT(X, Constant::getNullValue(X->getType()));
  1604. Value *NegX = B.CreateNSWNeg(X, "neg");
  1605. return B.CreateSelect(IsNeg, NegX, X);
  1606. }
  1607. Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilder<> &B) {
  1608. // isdigit(c) -> (c-'0') <u 10
  1609. Value *Op = CI->getArgOperand(0);
  1610. Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
  1611. Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
  1612. return B.CreateZExt(Op, CI->getType());
  1613. }
  1614. Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilder<> &B) {
  1615. // isascii(c) -> c <u 128
  1616. Value *Op = CI->getArgOperand(0);
  1617. Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
  1618. return B.CreateZExt(Op, CI->getType());
  1619. }
  1620. Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilder<> &B) {
  1621. // toascii(c) -> c & 0x7f
  1622. return B.CreateAnd(CI->getArgOperand(0),
  1623. ConstantInt::get(CI->getType(), 0x7F));
  1624. }
  1625. Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilder<> &B) {
  1626. StringRef Str;
  1627. if (!getConstantStringInfo(CI->getArgOperand(0), Str))
  1628. return nullptr;
  1629. return convertStrToNumber(CI, Str, 10);
  1630. }
  1631. Value *LibCallSimplifier::optimizeStrtol(CallInst *CI, IRBuilder<> &B) {
  1632. StringRef Str;
  1633. if (!getConstantStringInfo(CI->getArgOperand(0), Str))
  1634. return nullptr;
  1635. if (!isa<ConstantPointerNull>(CI->getArgOperand(1)))
  1636. return nullptr;
  1637. if (ConstantInt *CInt = dyn_cast<ConstantInt>(CI->getArgOperand(2))) {
  1638. return convertStrToNumber(CI, Str, CInt->getSExtValue());
  1639. }
  1640. return nullptr;
  1641. }
  1642. //===----------------------------------------------------------------------===//
  1643. // Formatting and IO Library Call Optimizations
  1644. //===----------------------------------------------------------------------===//
  1645. static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
  1646. Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
  1647. int StreamArg) {
  1648. Function *Callee = CI->getCalledFunction();
  1649. // Error reporting calls should be cold, mark them as such.
  1650. // This applies even to non-builtin calls: it is only a hint and applies to
  1651. // functions that the frontend might not understand as builtins.
  1652. // This heuristic was suggested in:
  1653. // Improving Static Branch Prediction in a Compiler
  1654. // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu
  1655. // Proceedings of PACT'98, Oct. 1998, IEEE
  1656. if (!CI->hasFnAttr(Attribute::Cold) &&
  1657. isReportingError(Callee, CI, StreamArg)) {
  1658. CI->addAttribute(AttributeList::FunctionIndex, Attribute::Cold);
  1659. }
  1660. return nullptr;
  1661. }
  1662. static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {
  1663. if (!Callee || !Callee->isDeclaration())
  1664. return false;
  1665. if (StreamArg < 0)
  1666. return true;
  1667. // These functions might be considered cold, but only if their stream
  1668. // argument is stderr.
  1669. if (StreamArg >= (int)CI->getNumArgOperands())
  1670. return false;
  1671. LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
  1672. if (!LI)
  1673. return false;
  1674. GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
  1675. if (!GV || !GV->isDeclaration())
  1676. return false;
  1677. return GV->getName() == "stderr";
  1678. }
  1679. Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {
  1680. // Check for a fixed format string.
  1681. StringRef FormatStr;
  1682. if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
  1683. return nullptr;
  1684. // Empty format string -> noop.
  1685. if (FormatStr.empty()) // Tolerate printf's declared void.
  1686. return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);
  1687. // Do not do any of the following transformations if the printf return value
  1688. // is used, in general the printf return value is not compatible with either
  1689. // putchar() or puts().
  1690. if (!CI->use_empty())
  1691. return nullptr;
  1692. // printf("x") -> putchar('x'), even for "%" and "%%".
  1693. if (FormatStr.size() == 1 || FormatStr == "%%")
  1694. return emitPutChar(B.getInt32(FormatStr[0]), B, TLI);
  1695. // printf("%s", "a") --> putchar('a')
  1696. if (FormatStr == "%s" && CI->getNumArgOperands() > 1) {
  1697. StringRef ChrStr;
  1698. if (!getConstantStringInfo(CI->getOperand(1), ChrStr))
  1699. return nullptr;
  1700. if (ChrStr.size() != 1)
  1701. return nullptr;
  1702. return emitPutChar(B.getInt32(ChrStr[0]), B, TLI);
  1703. }
  1704. // printf("foo\n") --> puts("foo")
  1705. if (FormatStr[FormatStr.size() - 1] == '\n' &&
  1706. FormatStr.find('%') == StringRef::npos) { // No format characters.
  1707. // Create a string literal with no \n on it. We expect the constant merge
  1708. // pass to be run after this pass, to merge duplicate strings.
  1709. FormatStr = FormatStr.drop_back();
  1710. Value *GV = B.CreateGlobalString(FormatStr, "str");
  1711. return emitPutS(GV, B, TLI);
  1712. }
  1713. // Optimize specific format strings.
  1714. // printf("%c", chr) --> putchar(chr)
  1715. if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
  1716. CI->getArgOperand(1)->getType()->isIntegerTy())
  1717. return emitPutChar(CI->getArgOperand(1), B, TLI);
  1718. // printf("%s\n", str) --> puts(str)
  1719. if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
  1720. CI->getArgOperand(1)->getType()->isPointerTy())
  1721. return emitPutS(CI->getArgOperand(1), B, TLI);
  1722. return nullptr;
  1723. }
  1724. Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilder<> &B) {
  1725. Function *Callee = CI->getCalledFunction();
  1726. FunctionType *FT = Callee->getFunctionType();
  1727. if (Value *V = optimizePrintFString(CI, B)) {
  1728. return V;
  1729. }
  1730. // printf(format, ...) -> iprintf(format, ...) if no floating point
  1731. // arguments.
  1732. if (TLI->has(LibFunc_iprintf) && !callHasFloatingPointArgument(CI)) {
  1733. Module *M = B.GetInsertBlock()->getParent()->getParent();
  1734. FunctionCallee IPrintFFn =
  1735. M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
  1736. CallInst *New = cast<CallInst>(CI->clone());
  1737. New->setCalledFunction(IPrintFFn);
  1738. B.Insert(New);
  1739. return New;
  1740. }
  1741. // printf(format, ...) -> __small_printf(format, ...) if no 128-bit floating point
  1742. // arguments.
  1743. if (TLI->has(LibFunc_small_printf) && !callHasFP128Argument(CI)) {
  1744. Module *M = B.GetInsertBlock()->getParent()->getParent();
  1745. auto SmallPrintFFn =
  1746. M->getOrInsertFunction(TLI->getName(LibFunc_small_printf),
  1747. FT, Callee->getAttributes());
  1748. CallInst *New = cast<CallInst>(CI->clone());
  1749. New->setCalledFunction(SmallPrintFFn);
  1750. B.Insert(New);
  1751. return New;
  1752. }
  1753. return nullptr;
  1754. }
  1755. Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {
  1756. // Check for a fixed format string.
  1757. StringRef FormatStr;
  1758. if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
  1759. return nullptr;
  1760. // If we just have a format string (nothing else crazy) transform it.
  1761. if (CI->getNumArgOperands() == 2) {
  1762. // Make sure there's no % in the constant array. We could try to handle
  1763. // %% -> % in the future if we cared.
  1764. if (FormatStr.find('%') != StringRef::npos)
  1765. return nullptr; // we found a format specifier, bail out.
  1766. // sprintf(str, fmt) -> llvm.memcpy(align 1 str, align 1 fmt, strlen(fmt)+1)
  1767. B.CreateMemCpy(CI->getArgOperand(0), 1, CI->getArgOperand(1), 1,
  1768. ConstantInt::get(DL.getIntPtrType(CI->getContext()),
  1769. FormatStr.size() + 1)); // Copy the null byte.
  1770. return ConstantInt::get(CI->getType(), FormatStr.size());
  1771. }
  1772. // The remaining optimizations require the format string to be "%s" or "%c"
  1773. // and have an extra operand.
  1774. if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
  1775. CI->getNumArgOperands() < 3)
  1776. return nullptr;
  1777. // Decode the second character of the format string.
  1778. if (FormatStr[1] == 'c') {
  1779. // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
  1780. if (!CI->getArgOperand(2)->getType()->isIntegerTy())
  1781. return nullptr;
  1782. Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
  1783. Value *Ptr = castToCStr(CI->getArgOperand(0), B);
  1784. B.CreateStore(V, Ptr);
  1785. Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
  1786. B.CreateStore(B.getInt8(0), Ptr);
  1787. return ConstantInt::get(CI->getType(), 1);
  1788. }
  1789. if (FormatStr[1] == 's') {
  1790. // sprintf(dest, "%s", str) -> llvm.memcpy(align 1 dest, align 1 str,
  1791. // strlen(str)+1)
  1792. if (!CI->getArgOperand(2)->getType()->isPointerTy())
  1793. return nullptr;
  1794. Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
  1795. if (!Len)
  1796. return nullptr;
  1797. Value *IncLen =
  1798. B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
  1799. B.CreateMemCpy(CI->getArgOperand(0), 1, CI->getArgOperand(2), 1, IncLen);
  1800. // The sprintf result is the unincremented number of bytes in the string.
  1801. return B.CreateIntCast(Len, CI->getType(), false);
  1802. }
  1803. return nullptr;
  1804. }
  1805. Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilder<> &B) {
  1806. Function *Callee = CI->getCalledFunction();
  1807. FunctionType *FT = Callee->getFunctionType();
  1808. if (Value *V = optimizeSPrintFString(CI, B)) {
  1809. return V;
  1810. }
  1811. // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
  1812. // point arguments.
  1813. if (TLI->has(LibFunc_siprintf) && !callHasFloatingPointArgument(CI)) {
  1814. Module *M = B.GetInsertBlock()->getParent()->getParent();
  1815. FunctionCallee SIPrintFFn =
  1816. M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
  1817. CallInst *New = cast<CallInst>(CI->clone());
  1818. New->setCalledFunction(SIPrintFFn);
  1819. B.Insert(New);
  1820. return New;
  1821. }
  1822. // sprintf(str, format, ...) -> __small_sprintf(str, format, ...) if no 128-bit
  1823. // floating point arguments.
  1824. if (TLI->has(LibFunc_small_sprintf) && !callHasFP128Argument(CI)) {
  1825. Module *M = B.GetInsertBlock()->getParent()->getParent();
  1826. auto SmallSPrintFFn =
  1827. M->getOrInsertFunction(TLI->getName(LibFunc_small_sprintf),
  1828. FT, Callee->getAttributes());
  1829. CallInst *New = cast<CallInst>(CI->clone());
  1830. New->setCalledFunction(SmallSPrintFFn);
  1831. B.Insert(New);
  1832. return New;
  1833. }
  1834. return nullptr;
  1835. }
  1836. Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI, IRBuilder<> &B) {
  1837. // Check for a fixed format string.
  1838. StringRef FormatStr;
  1839. if (!getConstantStringInfo(CI->getArgOperand(2), FormatStr))
  1840. return nullptr;
  1841. // Check for size
  1842. ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  1843. if (!Size)
  1844. return nullptr;
  1845. uint64_t N = Size->getZExtValue();
  1846. // If we just have a format string (nothing else crazy) transform it.
  1847. if (CI->getNumArgOperands() == 3) {
  1848. // Make sure there's no % in the constant array. We could try to handle
  1849. // %% -> % in the future if we cared.
  1850. if (FormatStr.find('%') != StringRef::npos)
  1851. return nullptr; // we found a format specifier, bail out.
  1852. if (N == 0)
  1853. return ConstantInt::get(CI->getType(), FormatStr.size());
  1854. else if (N < FormatStr.size() + 1)
  1855. return nullptr;
  1856. // snprintf(dst, size, fmt) -> llvm.memcpy(align 1 dst, align 1 fmt,
  1857. // strlen(fmt)+1)
  1858. B.CreateMemCpy(
  1859. CI->getArgOperand(0), 1, CI->getArgOperand(2), 1,
  1860. ConstantInt::get(DL.getIntPtrType(CI->getContext()),
  1861. FormatStr.size() + 1)); // Copy the null byte.
  1862. return ConstantInt::get(CI->getType(), FormatStr.size());
  1863. }
  1864. // The remaining optimizations require the format string to be "%s" or "%c"
  1865. // and have an extra operand.
  1866. if (FormatStr.size() == 2 && FormatStr[0] == '%' &&
  1867. CI->getNumArgOperands() == 4) {
  1868. // Decode the second character of the format string.
  1869. if (FormatStr[1] == 'c') {
  1870. if (N == 0)
  1871. return ConstantInt::get(CI->getType(), 1);
  1872. else if (N == 1)
  1873. return nullptr;
  1874. // snprintf(dst, size, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
  1875. if (!CI->getArgOperand(3)->getType()->isIntegerTy())
  1876. return nullptr;
  1877. Value *V = B.CreateTrunc(CI->getArgOperand(3), B.getInt8Ty(), "char");
  1878. Value *Ptr = castToCStr(CI->getArgOperand(0), B);
  1879. B.CreateStore(V, Ptr);
  1880. Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
  1881. B.CreateStore(B.getInt8(0), Ptr);
  1882. return ConstantInt::get(CI->getType(), 1);
  1883. }
  1884. if (FormatStr[1] == 's') {
  1885. // snprintf(dest, size, "%s", str) to llvm.memcpy(dest, str, len+1, 1)
  1886. StringRef Str;
  1887. if (!getConstantStringInfo(CI->getArgOperand(3), Str))
  1888. return nullptr;
  1889. if (N == 0)
  1890. return ConstantInt::get(CI->getType(), Str.size());
  1891. else if (N < Str.size() + 1)
  1892. return nullptr;
  1893. B.CreateMemCpy(CI->getArgOperand(0), 1, CI->getArgOperand(3), 1,
  1894. ConstantInt::get(CI->getType(), Str.size() + 1));
  1895. // The snprintf result is the unincremented number of bytes in the string.
  1896. return ConstantInt::get(CI->getType(), Str.size());
  1897. }
  1898. }
  1899. return nullptr;
  1900. }
  1901. Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilder<> &B) {
  1902. if (Value *V = optimizeSnPrintFString(CI, B)) {
  1903. return V;
  1904. }
  1905. return nullptr;
  1906. }
  1907. Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, IRBuilder<> &B) {
  1908. optimizeErrorReporting(CI, B, 0);
  1909. // All the optimizations depend on the format string.
  1910. StringRef FormatStr;
  1911. if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
  1912. return nullptr;
  1913. // Do not do any of the following transformations if the fprintf return
  1914. // value is used, in general the fprintf return value is not compatible
  1915. // with fwrite(), fputc() or fputs().
  1916. if (!CI->use_empty())
  1917. return nullptr;
  1918. // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
  1919. if (CI->getNumArgOperands() == 2) {
  1920. // Could handle %% -> % if we cared.
  1921. if (FormatStr.find('%') != StringRef::npos)
  1922. return nullptr; // We found a format specifier.
  1923. return emitFWrite(
  1924. CI->getArgOperand(1),
  1925. ConstantInt::get(DL.getIntPtrType(CI->getContext()), FormatStr.size()),
  1926. CI->getArgOperand(0), B, DL, TLI);
  1927. }
  1928. // The remaining optimizations require the format string to be "%s" or "%c"
  1929. // and have an extra operand.
  1930. if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
  1931. CI->getNumArgOperands() < 3)
  1932. return nullptr;
  1933. // Decode the second character of the format string.
  1934. if (FormatStr[1] == 'c') {
  1935. // fprintf(F, "%c", chr) --> fputc(chr, F)
  1936. if (!CI->getArgOperand(2)->getType()->isIntegerTy())
  1937. return nullptr;
  1938. return emitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI);
  1939. }
  1940. if (FormatStr[1] == 's') {
  1941. // fprintf(F, "%s", str) --> fputs(str, F)
  1942. if (!CI->getArgOperand(2)->getType()->isPointerTy())
  1943. return nullptr;
  1944. return emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI);
  1945. }
  1946. return nullptr;
  1947. }
  1948. Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilder<> &B) {
  1949. Function *Callee = CI->getCalledFunction();
  1950. FunctionType *FT = Callee->getFunctionType();
  1951. if (Value *V = optimizeFPrintFString(CI, B)) {
  1952. return V;
  1953. }
  1954. // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
  1955. // floating point arguments.
  1956. if (TLI->has(LibFunc_fiprintf) && !callHasFloatingPointArgument(CI)) {
  1957. Module *M = B.GetInsertBlock()->getParent()->getParent();
  1958. FunctionCallee FIPrintFFn =
  1959. M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
  1960. CallInst *New = cast<CallInst>(CI->clone());
  1961. New->setCalledFunction(FIPrintFFn);
  1962. B.Insert(New);
  1963. return New;
  1964. }
  1965. // fprintf(stream, format, ...) -> __small_fprintf(stream, format, ...) if no
  1966. // 128-bit floating point arguments.
  1967. if (TLI->has(LibFunc_small_fprintf) && !callHasFP128Argument(CI)) {
  1968. Module *M = B.GetInsertBlock()->getParent()->getParent();
  1969. auto SmallFPrintFFn =
  1970. M->getOrInsertFunction(TLI->getName(LibFunc_small_fprintf),
  1971. FT, Callee->getAttributes());
  1972. CallInst *New = cast<CallInst>(CI->clone());
  1973. New->setCalledFunction(SmallFPrintFFn);
  1974. B.Insert(New);
  1975. return New;
  1976. }
  1977. return nullptr;
  1978. }
  1979. Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilder<> &B) {
  1980. optimizeErrorReporting(CI, B, 3);
  1981. // Get the element size and count.
  1982. ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  1983. ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
  1984. if (SizeC && CountC) {
  1985. uint64_t Bytes = SizeC->getZExtValue() * CountC->getZExtValue();
  1986. // If this is writing zero records, remove the call (it's a noop).
  1987. if (Bytes == 0)
  1988. return ConstantInt::get(CI->getType(), 0);
  1989. // If this is writing one byte, turn it into fputc.
  1990. // This optimisation is only valid, if the return value is unused.
  1991. if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
  1992. Value *Char = B.CreateLoad(B.getInt8Ty(),
  1993. castToCStr(CI->getArgOperand(0), B), "char");
  1994. Value *NewCI = emitFPutC(Char, CI->getArgOperand(3), B, TLI);
  1995. return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
  1996. }
  1997. }
  1998. if (isLocallyOpenedFile(CI->getArgOperand(3), CI, B, TLI))
  1999. return emitFWriteUnlocked(CI->getArgOperand(0), CI->getArgOperand(1),
  2000. CI->getArgOperand(2), CI->getArgOperand(3), B, DL,
  2001. TLI);
  2002. return nullptr;
  2003. }
  2004. Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilder<> &B) {
  2005. optimizeErrorReporting(CI, B, 1);
  2006. // Don't rewrite fputs to fwrite when optimising for size because fwrite
  2007. // requires more arguments and thus extra MOVs are required.
  2008. if (CI->getFunction()->hasOptSize())
  2009. return nullptr;
  2010. // Check if has any use
  2011. if (!CI->use_empty()) {
  2012. if (isLocallyOpenedFile(CI->getArgOperand(1), CI, B, TLI))
  2013. return emitFPutSUnlocked(CI->getArgOperand(0), CI->getArgOperand(1), B,
  2014. TLI);
  2015. else
  2016. // We can't optimize if return value is used.
  2017. return nullptr;
  2018. }
  2019. // fputs(s,F) --> fwrite(s,strlen(s),1,F)
  2020. uint64_t Len = GetStringLength(CI->getArgOperand(0));
  2021. if (!Len)
  2022. return nullptr;
  2023. // Known to have no uses (see above).
  2024. return emitFWrite(
  2025. CI->getArgOperand(0),
  2026. ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len - 1),
  2027. CI->getArgOperand(1), B, DL, TLI);
  2028. }
  2029. Value *LibCallSimplifier::optimizeFPutc(CallInst *CI, IRBuilder<> &B) {
  2030. optimizeErrorReporting(CI, B, 1);
  2031. if (isLocallyOpenedFile(CI->getArgOperand(1), CI, B, TLI))
  2032. return emitFPutCUnlocked(CI->getArgOperand(0), CI->getArgOperand(1), B,
  2033. TLI);
  2034. return nullptr;
  2035. }
  2036. Value *LibCallSimplifier::optimizeFGetc(CallInst *CI, IRBuilder<> &B) {
  2037. if (isLocallyOpenedFile(CI->getArgOperand(0), CI, B, TLI))
  2038. return emitFGetCUnlocked(CI->getArgOperand(0), B, TLI);
  2039. return nullptr;
  2040. }
  2041. Value *LibCallSimplifier::optimizeFGets(CallInst *CI, IRBuilder<> &B) {
  2042. if (isLocallyOpenedFile(CI->getArgOperand(2), CI, B, TLI))
  2043. return emitFGetSUnlocked(CI->getArgOperand(0), CI->getArgOperand(1),
  2044. CI->getArgOperand(2), B, TLI);
  2045. return nullptr;
  2046. }
  2047. Value *LibCallSimplifier::optimizeFRead(CallInst *CI, IRBuilder<> &B) {
  2048. if (isLocallyOpenedFile(CI->getArgOperand(3), CI, B, TLI))
  2049. return emitFReadUnlocked(CI->getArgOperand(0), CI->getArgOperand(1),
  2050. CI->getArgOperand(2), CI->getArgOperand(3), B, DL,
  2051. TLI);
  2052. return nullptr;
  2053. }
  2054. Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilder<> &B) {
  2055. if (!CI->use_empty())
  2056. return nullptr;
  2057. // Check for a constant string.
  2058. // puts("") -> putchar('\n')
  2059. StringRef Str;
  2060. if (getConstantStringInfo(CI->getArgOperand(0), Str) && Str.empty())
  2061. return emitPutChar(B.getInt32('\n'), B, TLI);
  2062. return nullptr;
  2063. }
  2064. bool LibCallSimplifier::hasFloatVersion(StringRef FuncName) {
  2065. LibFunc Func;
  2066. SmallString<20> FloatFuncName = FuncName;
  2067. FloatFuncName += 'f';
  2068. if (TLI->getLibFunc(FloatFuncName, Func))
  2069. return TLI->has(Func);
  2070. return false;
  2071. }
  2072. Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI,
  2073. IRBuilder<> &Builder) {
  2074. LibFunc Func;
  2075. Function *Callee = CI->getCalledFunction();
  2076. // Check for string/memory library functions.
  2077. if (TLI->getLibFunc(*Callee, Func) && TLI->has(Func)) {
  2078. // Make sure we never change the calling convention.
  2079. assert((ignoreCallingConv(Func) ||
  2080. isCallingConvCCompatible(CI)) &&
  2081. "Optimizing string/memory libcall would change the calling convention");
  2082. switch (Func) {
  2083. case LibFunc_strcat:
  2084. return optimizeStrCat(CI, Builder);
  2085. case LibFunc_strncat:
  2086. return optimizeStrNCat(CI, Builder);
  2087. case LibFunc_strchr:
  2088. return optimizeStrChr(CI, Builder);
  2089. case LibFunc_strrchr:
  2090. return optimizeStrRChr(CI, Builder);
  2091. case LibFunc_strcmp:
  2092. return optimizeStrCmp(CI, Builder);
  2093. case LibFunc_strncmp:
  2094. return optimizeStrNCmp(CI, Builder);
  2095. case LibFunc_strcpy:
  2096. return optimizeStrCpy(CI, Builder);
  2097. case LibFunc_stpcpy:
  2098. return optimizeStpCpy(CI, Builder);
  2099. case LibFunc_strncpy:
  2100. return optimizeStrNCpy(CI, Builder);
  2101. case LibFunc_strlen:
  2102. return optimizeStrLen(CI, Builder);
  2103. case LibFunc_strpbrk:
  2104. return optimizeStrPBrk(CI, Builder);
  2105. case LibFunc_strtol:
  2106. case LibFunc_strtod:
  2107. case LibFunc_strtof:
  2108. case LibFunc_strtoul:
  2109. case LibFunc_strtoll:
  2110. case LibFunc_strtold:
  2111. case LibFunc_strtoull:
  2112. return optimizeStrTo(CI, Builder);
  2113. case LibFunc_strspn:
  2114. return optimizeStrSpn(CI, Builder);
  2115. case LibFunc_strcspn:
  2116. return optimizeStrCSpn(CI, Builder);
  2117. case LibFunc_strstr:
  2118. return optimizeStrStr(CI, Builder);
  2119. case LibFunc_memchr:
  2120. return optimizeMemChr(CI, Builder);
  2121. case LibFunc_memcmp:
  2122. return optimizeMemCmp(CI, Builder);
  2123. case LibFunc_memcpy:
  2124. return optimizeMemCpy(CI, Builder);
  2125. case LibFunc_memmove:
  2126. return optimizeMemMove(CI, Builder);
  2127. case LibFunc_memset:
  2128. return optimizeMemSet(CI, Builder);
  2129. case LibFunc_realloc:
  2130. return optimizeRealloc(CI, Builder);
  2131. case LibFunc_wcslen:
  2132. return optimizeWcslen(CI, Builder);
  2133. default:
  2134. break;
  2135. }
  2136. }
  2137. return nullptr;
  2138. }
  2139. Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
  2140. LibFunc Func,
  2141. IRBuilder<> &Builder) {
  2142. // Don't optimize calls that require strict floating point semantics.
  2143. if (CI->isStrictFP())
  2144. return nullptr;
  2145. if (Value *V = optimizeTrigReflections(CI, Func, Builder))
  2146. return V;
  2147. switch (Func) {
  2148. case LibFunc_sinpif:
  2149. case LibFunc_sinpi:
  2150. case LibFunc_cospif:
  2151. case LibFunc_cospi:
  2152. return optimizeSinCosPi(CI, Builder);
  2153. case LibFunc_powf:
  2154. case LibFunc_pow:
  2155. case LibFunc_powl:
  2156. return optimizePow(CI, Builder);
  2157. case LibFunc_exp2l:
  2158. case LibFunc_exp2:
  2159. case LibFunc_exp2f:
  2160. return optimizeExp2(CI, Builder);
  2161. case LibFunc_fabsf:
  2162. case LibFunc_fabs:
  2163. case LibFunc_fabsl:
  2164. return replaceUnaryCall(CI, Builder, Intrinsic::fabs);
  2165. case LibFunc_sqrtf:
  2166. case LibFunc_sqrt:
  2167. case LibFunc_sqrtl:
  2168. return optimizeSqrt(CI, Builder);
  2169. case LibFunc_log:
  2170. case LibFunc_log10:
  2171. case LibFunc_log1p:
  2172. case LibFunc_log2:
  2173. case LibFunc_logb:
  2174. return optimizeLog(CI, Builder);
  2175. case LibFunc_tan:
  2176. case LibFunc_tanf:
  2177. case LibFunc_tanl:
  2178. return optimizeTan(CI, Builder);
  2179. case LibFunc_ceil:
  2180. return replaceUnaryCall(CI, Builder, Intrinsic::ceil);
  2181. case LibFunc_floor:
  2182. return replaceUnaryCall(CI, Builder, Intrinsic::floor);
  2183. case LibFunc_round:
  2184. return replaceUnaryCall(CI, Builder, Intrinsic::round);
  2185. case LibFunc_nearbyint:
  2186. return replaceUnaryCall(CI, Builder, Intrinsic::nearbyint);
  2187. case LibFunc_rint:
  2188. return replaceUnaryCall(CI, Builder, Intrinsic::rint);
  2189. case LibFunc_trunc:
  2190. return replaceUnaryCall(CI, Builder, Intrinsic::trunc);
  2191. case LibFunc_acos:
  2192. case LibFunc_acosh:
  2193. case LibFunc_asin:
  2194. case LibFunc_asinh:
  2195. case LibFunc_atan:
  2196. case LibFunc_atanh:
  2197. case LibFunc_cbrt:
  2198. case LibFunc_cosh:
  2199. case LibFunc_exp:
  2200. case LibFunc_exp10:
  2201. case LibFunc_expm1:
  2202. case LibFunc_cos:
  2203. case LibFunc_sin:
  2204. case LibFunc_sinh:
  2205. case LibFunc_tanh:
  2206. if (UnsafeFPShrink && hasFloatVersion(CI->getCalledFunction()->getName()))
  2207. return optimizeUnaryDoubleFP(CI, Builder, true);
  2208. return nullptr;
  2209. case LibFunc_copysign:
  2210. if (hasFloatVersion(CI->getCalledFunction()->getName()))
  2211. return optimizeBinaryDoubleFP(CI, Builder);
  2212. return nullptr;
  2213. case LibFunc_fminf:
  2214. case LibFunc_fmin:
  2215. case LibFunc_fminl:
  2216. case LibFunc_fmaxf:
  2217. case LibFunc_fmax:
  2218. case LibFunc_fmaxl:
  2219. return optimizeFMinFMax(CI, Builder);
  2220. case LibFunc_cabs:
  2221. case LibFunc_cabsf:
  2222. case LibFunc_cabsl:
  2223. return optimizeCAbs(CI, Builder);
  2224. default:
  2225. return nullptr;
  2226. }
  2227. }
  2228. Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
  2229. // TODO: Split out the code below that operates on FP calls so that
  2230. // we can all non-FP calls with the StrictFP attribute to be
  2231. // optimized.
  2232. if (CI->isNoBuiltin())
  2233. return nullptr;
  2234. LibFunc Func;
  2235. Function *Callee = CI->getCalledFunction();
  2236. SmallVector<OperandBundleDef, 2> OpBundles;
  2237. CI->getOperandBundlesAsDefs(OpBundles);
  2238. IRBuilder<> Builder(CI, /*FPMathTag=*/nullptr, OpBundles);
  2239. bool isCallingConvC = isCallingConvCCompatible(CI);
  2240. // Command-line parameter overrides instruction attribute.
  2241. // This can't be moved to optimizeFloatingPointLibCall() because it may be
  2242. // used by the intrinsic optimizations.
  2243. if (EnableUnsafeFPShrink.getNumOccurrences() > 0)
  2244. UnsafeFPShrink = EnableUnsafeFPShrink;
  2245. else if (isa<FPMathOperator>(CI) && CI->isFast())
  2246. UnsafeFPShrink = true;
  2247. // First, check for intrinsics.
  2248. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
  2249. if (!isCallingConvC)
  2250. return nullptr;
  2251. // The FP intrinsics have corresponding constrained versions so we don't
  2252. // need to check for the StrictFP attribute here.
  2253. switch (II->getIntrinsicID()) {
  2254. case Intrinsic::pow:
  2255. return optimizePow(CI, Builder);
  2256. case Intrinsic::exp2:
  2257. return optimizeExp2(CI, Builder);
  2258. case Intrinsic::log:
  2259. return optimizeLog(CI, Builder);
  2260. case Intrinsic::sqrt:
  2261. return optimizeSqrt(CI, Builder);
  2262. // TODO: Use foldMallocMemset() with memset intrinsic.
  2263. default:
  2264. return nullptr;
  2265. }
  2266. }
  2267. // Also try to simplify calls to fortified library functions.
  2268. if (Value *SimplifiedFortifiedCI = FortifiedSimplifier.optimizeCall(CI)) {
  2269. // Try to further simplify the result.
  2270. CallInst *SimplifiedCI = dyn_cast<CallInst>(SimplifiedFortifiedCI);
  2271. if (SimplifiedCI && SimplifiedCI->getCalledFunction()) {
  2272. // Use an IR Builder from SimplifiedCI if available instead of CI
  2273. // to guarantee we reach all uses we might replace later on.
  2274. IRBuilder<> TmpBuilder(SimplifiedCI);
  2275. if (Value *V = optimizeStringMemoryLibCall(SimplifiedCI, TmpBuilder)) {
  2276. // If we were able to further simplify, remove the now redundant call.
  2277. SimplifiedCI->replaceAllUsesWith(V);
  2278. eraseFromParent(SimplifiedCI);
  2279. return V;
  2280. }
  2281. }
  2282. return SimplifiedFortifiedCI;
  2283. }
  2284. // Then check for known library functions.
  2285. if (TLI->getLibFunc(*Callee, Func) && TLI->has(Func)) {
  2286. // We never change the calling convention.
  2287. if (!ignoreCallingConv(Func) && !isCallingConvC)
  2288. return nullptr;
  2289. if (Value *V = optimizeStringMemoryLibCall(CI, Builder))
  2290. return V;
  2291. if (Value *V = optimizeFloatingPointLibCall(CI, Func, Builder))
  2292. return V;
  2293. switch (Func) {
  2294. case LibFunc_ffs:
  2295. case LibFunc_ffsl:
  2296. case LibFunc_ffsll:
  2297. return optimizeFFS(CI, Builder);
  2298. case LibFunc_fls:
  2299. case LibFunc_flsl:
  2300. case LibFunc_flsll:
  2301. return optimizeFls(CI, Builder);
  2302. case LibFunc_abs:
  2303. case LibFunc_labs:
  2304. case LibFunc_llabs:
  2305. return optimizeAbs(CI, Builder);
  2306. case LibFunc_isdigit:
  2307. return optimizeIsDigit(CI, Builder);
  2308. case LibFunc_isascii:
  2309. return optimizeIsAscii(CI, Builder);
  2310. case LibFunc_toascii:
  2311. return optimizeToAscii(CI, Builder);
  2312. case LibFunc_atoi:
  2313. case LibFunc_atol:
  2314. case LibFunc_atoll:
  2315. return optimizeAtoi(CI, Builder);
  2316. case LibFunc_strtol:
  2317. case LibFunc_strtoll:
  2318. return optimizeStrtol(CI, Builder);
  2319. case LibFunc_printf:
  2320. return optimizePrintF(CI, Builder);
  2321. case LibFunc_sprintf:
  2322. return optimizeSPrintF(CI, Builder);
  2323. case LibFunc_snprintf:
  2324. return optimizeSnPrintF(CI, Builder);
  2325. case LibFunc_fprintf:
  2326. return optimizeFPrintF(CI, Builder);
  2327. case LibFunc_fwrite:
  2328. return optimizeFWrite(CI, Builder);
  2329. case LibFunc_fread:
  2330. return optimizeFRead(CI, Builder);
  2331. case LibFunc_fputs:
  2332. return optimizeFPuts(CI, Builder);
  2333. case LibFunc_fgets:
  2334. return optimizeFGets(CI, Builder);
  2335. case LibFunc_fputc:
  2336. return optimizeFPutc(CI, Builder);
  2337. case LibFunc_fgetc:
  2338. return optimizeFGetc(CI, Builder);
  2339. case LibFunc_puts:
  2340. return optimizePuts(CI, Builder);
  2341. case LibFunc_perror:
  2342. return optimizeErrorReporting(CI, Builder);
  2343. case LibFunc_vfprintf:
  2344. case LibFunc_fiprintf:
  2345. return optimizeErrorReporting(CI, Builder, 0);
  2346. default:
  2347. return nullptr;
  2348. }
  2349. }
  2350. return nullptr;
  2351. }
  2352. LibCallSimplifier::LibCallSimplifier(
  2353. const DataLayout &DL, const TargetLibraryInfo *TLI,
  2354. OptimizationRemarkEmitter &ORE,
  2355. function_ref<void(Instruction *, Value *)> Replacer,
  2356. function_ref<void(Instruction *)> Eraser)
  2357. : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), ORE(ORE),
  2358. UnsafeFPShrink(false), Replacer(Replacer), Eraser(Eraser) {}
  2359. void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) {
  2360. // Indirect through the replacer used in this instance.
  2361. Replacer(I, With);
  2362. }
  2363. void LibCallSimplifier::eraseFromParent(Instruction *I) {
  2364. Eraser(I);
  2365. }
  2366. // TODO:
  2367. // Additional cases that we need to add to this file:
  2368. //
  2369. // cbrt:
  2370. // * cbrt(expN(X)) -> expN(x/3)
  2371. // * cbrt(sqrt(x)) -> pow(x,1/6)
  2372. // * cbrt(cbrt(x)) -> pow(x,1/9)
  2373. //
  2374. // exp, expf, expl:
  2375. // * exp(log(x)) -> x
  2376. //
  2377. // log, logf, logl:
  2378. // * log(exp(x)) -> x
  2379. // * log(exp(y)) -> y*log(e)
  2380. // * log(exp10(y)) -> y*log(10)
  2381. // * log(sqrt(x)) -> 0.5*log(x)
  2382. //
  2383. // pow, powf, powl:
  2384. // * pow(sqrt(x),y) -> pow(x,y*0.5)
  2385. // * pow(pow(x,y),z)-> pow(x,y*z)
  2386. //
  2387. // signbit:
  2388. // * signbit(cnst) -> cnst'
  2389. // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
  2390. //
  2391. // sqrt, sqrtf, sqrtl:
  2392. // * sqrt(expN(x)) -> expN(x*0.5)
  2393. // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
  2394. // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
  2395. //
  2396. //===----------------------------------------------------------------------===//
  2397. // Fortified Library Call Optimizations
  2398. //===----------------------------------------------------------------------===//
  2399. bool FortifiedLibCallSimplifier::isFortifiedCallFoldable(CallInst *CI,
  2400. unsigned ObjSizeOp,
  2401. unsigned SizeOp,
  2402. bool isString) {
  2403. if (CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(SizeOp))
  2404. return true;
  2405. if (ConstantInt *ObjSizeCI =
  2406. dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) {
  2407. if (ObjSizeCI->isMinusOne())
  2408. return true;
  2409. // If the object size wasn't -1 (unknown), bail out if we were asked to.
  2410. if (OnlyLowerUnknownSize)
  2411. return false;
  2412. if (isString) {
  2413. uint64_t Len = GetStringLength(CI->getArgOperand(SizeOp));
  2414. // If the length is 0 we don't know how long it is and so we can't
  2415. // remove the check.
  2416. if (Len == 0)
  2417. return false;
  2418. return ObjSizeCI->getZExtValue() >= Len;
  2419. }
  2420. if (ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getArgOperand(SizeOp)))
  2421. return ObjSizeCI->getZExtValue() >= SizeCI->getZExtValue();
  2422. }
  2423. return false;
  2424. }
  2425. Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI,
  2426. IRBuilder<> &B) {
  2427. if (isFortifiedCallFoldable(CI, 3, 2, false)) {
  2428. B.CreateMemCpy(CI->getArgOperand(0), 1, CI->getArgOperand(1), 1,
  2429. CI->getArgOperand(2));
  2430. return CI->getArgOperand(0);
  2431. }
  2432. return nullptr;
  2433. }
  2434. Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI,
  2435. IRBuilder<> &B) {
  2436. if (isFortifiedCallFoldable(CI, 3, 2, false)) {
  2437. B.CreateMemMove(CI->getArgOperand(0), 1, CI->getArgOperand(1), 1,
  2438. CI->getArgOperand(2));
  2439. return CI->getArgOperand(0);
  2440. }
  2441. return nullptr;
  2442. }
  2443. Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI,
  2444. IRBuilder<> &B) {
  2445. // TODO: Try foldMallocMemset() here.
  2446. if (isFortifiedCallFoldable(CI, 3, 2, false)) {
  2447. Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
  2448. B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
  2449. return CI->getArgOperand(0);
  2450. }
  2451. return nullptr;
  2452. }
  2453. Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
  2454. IRBuilder<> &B,
  2455. LibFunc Func) {
  2456. Function *Callee = CI->getCalledFunction();
  2457. StringRef Name = Callee->getName();
  2458. const DataLayout &DL = CI->getModule()->getDataLayout();
  2459. Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
  2460. *ObjSize = CI->getArgOperand(2);
  2461. // __stpcpy_chk(x,x,...) -> x+strlen(x)
  2462. if (Func == LibFunc_stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) {
  2463. Value *StrLen = emitStrLen(Src, B, DL, TLI);
  2464. return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
  2465. }
  2466. // If a) we don't have any length information, or b) we know this will
  2467. // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
  2468. // st[rp]cpy_chk call which may fail at runtime if the size is too long.
  2469. // TODO: It might be nice to get a maximum length out of the possible
  2470. // string lengths for varying.
  2471. if (isFortifiedCallFoldable(CI, 2, 1, true))
  2472. return emitStrCpy(Dst, Src, B, TLI, Name.substr(2, 6));
  2473. if (OnlyLowerUnknownSize)
  2474. return nullptr;
  2475. // Maybe we can stil fold __st[rp]cpy_chk to __memcpy_chk.
  2476. uint64_t Len = GetStringLength(Src);
  2477. if (Len == 0)
  2478. return nullptr;
  2479. Type *SizeTTy = DL.getIntPtrType(CI->getContext());
  2480. Value *LenV = ConstantInt::get(SizeTTy, Len);
  2481. Value *Ret = emitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI);
  2482. // If the function was an __stpcpy_chk, and we were able to fold it into
  2483. // a __memcpy_chk, we still need to return the correct end pointer.
  2484. if (Ret && Func == LibFunc_stpcpy_chk)
  2485. return B.CreateGEP(B.getInt8Ty(), Dst, ConstantInt::get(SizeTTy, Len - 1));
  2486. return Ret;
  2487. }
  2488. Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
  2489. IRBuilder<> &B,
  2490. LibFunc Func) {
  2491. Function *Callee = CI->getCalledFunction();
  2492. StringRef Name = Callee->getName();
  2493. if (isFortifiedCallFoldable(CI, 3, 2, false)) {
  2494. Value *Ret = emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
  2495. CI->getArgOperand(2), B, TLI, Name.substr(2, 7));
  2496. return Ret;
  2497. }
  2498. return nullptr;
  2499. }
  2500. Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI) {
  2501. // FIXME: We shouldn't be changing "nobuiltin" or TLI unavailable calls here.
  2502. // Some clang users checked for _chk libcall availability using:
  2503. // __has_builtin(__builtin___memcpy_chk)
  2504. // When compiling with -fno-builtin, this is always true.
  2505. // When passing -ffreestanding/-mkernel, which both imply -fno-builtin, we
  2506. // end up with fortified libcalls, which isn't acceptable in a freestanding
  2507. // environment which only provides their non-fortified counterparts.
  2508. //
  2509. // Until we change clang and/or teach external users to check for availability
  2510. // differently, disregard the "nobuiltin" attribute and TLI::has.
  2511. //
  2512. // PR23093.
  2513. LibFunc Func;
  2514. Function *Callee = CI->getCalledFunction();
  2515. SmallVector<OperandBundleDef, 2> OpBundles;
  2516. CI->getOperandBundlesAsDefs(OpBundles);
  2517. IRBuilder<> Builder(CI, /*FPMathTag=*/nullptr, OpBundles);
  2518. bool isCallingConvC = isCallingConvCCompatible(CI);
  2519. // First, check that this is a known library functions and that the prototype
  2520. // is correct.
  2521. if (!TLI->getLibFunc(*Callee, Func))
  2522. return nullptr;
  2523. // We never change the calling convention.
  2524. if (!ignoreCallingConv(Func) && !isCallingConvC)
  2525. return nullptr;
  2526. switch (Func) {
  2527. case LibFunc_memcpy_chk:
  2528. return optimizeMemCpyChk(CI, Builder);
  2529. case LibFunc_memmove_chk:
  2530. return optimizeMemMoveChk(CI, Builder);
  2531. case LibFunc_memset_chk:
  2532. return optimizeMemSetChk(CI, Builder);
  2533. case LibFunc_stpcpy_chk:
  2534. case LibFunc_strcpy_chk:
  2535. return optimizeStrpCpyChk(CI, Builder, Func);
  2536. case LibFunc_stpncpy_chk:
  2537. case LibFunc_strncpy_chk:
  2538. return optimizeStrpNCpyChk(CI, Builder, Func);
  2539. default:
  2540. break;
  2541. }
  2542. return nullptr;
  2543. }
  2544. FortifiedLibCallSimplifier::FortifiedLibCallSimplifier(
  2545. const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
  2546. : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}