SimplifyLibCalls.cpp 146 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081
  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/Triple.h"
  17. #include "llvm/Analysis/ConstantFolding.h"
  18. #include "llvm/Analysis/Loads.h"
  19. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  20. #include "llvm/Analysis/ValueTracking.h"
  21. #include "llvm/IR/DataLayout.h"
  22. #include "llvm/IR/Function.h"
  23. #include "llvm/IR/IRBuilder.h"
  24. #include "llvm/IR/IntrinsicInst.h"
  25. #include "llvm/IR/Intrinsics.h"
  26. #include "llvm/IR/Module.h"
  27. #include "llvm/IR/PatternMatch.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/KnownBits.h"
  30. #include "llvm/Support/MathExtras.h"
  31. #include "llvm/Transforms/Utils/BuildLibCalls.h"
  32. #include "llvm/Transforms/Utils/Local.h"
  33. #include "llvm/Transforms/Utils/SizeOpts.h"
  34. #include <cmath>
  35. using namespace llvm;
  36. using namespace PatternMatch;
  37. static cl::opt<bool>
  38. EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden,
  39. cl::init(false),
  40. cl::desc("Enable unsafe double to float "
  41. "shrinking for math lib calls"));
  42. //===----------------------------------------------------------------------===//
  43. // Helper Functions
  44. //===----------------------------------------------------------------------===//
  45. static bool ignoreCallingConv(LibFunc Func) {
  46. return Func == LibFunc_abs || Func == LibFunc_labs ||
  47. Func == LibFunc_llabs || Func == LibFunc_strlen;
  48. }
  49. /// Return true if it is only used in equality comparisons with With.
  50. static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
  51. for (User *U : V->users()) {
  52. if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
  53. if (IC->isEquality() && IC->getOperand(1) == With)
  54. continue;
  55. // Unknown instruction.
  56. return false;
  57. }
  58. return true;
  59. }
  60. static bool callHasFloatingPointArgument(const CallInst *CI) {
  61. return any_of(CI->operands(), [](const Use &OI) {
  62. return OI->getType()->isFloatingPointTy();
  63. });
  64. }
  65. static bool callHasFP128Argument(const CallInst *CI) {
  66. return any_of(CI->operands(), [](const Use &OI) {
  67. return OI->getType()->isFP128Ty();
  68. });
  69. }
  70. // Convert the entire string Str representing an integer in Base, up to
  71. // the terminating nul if present, to a constant according to the rules
  72. // of strtoul[l] or, when AsSigned is set, of strtol[l]. On success
  73. // return the result, otherwise null.
  74. // The function assumes the string is encoded in ASCII and carefully
  75. // avoids converting sequences (including "") that the corresponding
  76. // library call might fail and set errno for.
  77. static Value *convertStrToInt(CallInst *CI, StringRef &Str, Value *EndPtr,
  78. uint64_t Base, bool AsSigned, IRBuilderBase &B) {
  79. if (Base < 2 || Base > 36)
  80. if (Base != 0)
  81. // Fail for an invalid base (required by POSIX).
  82. return nullptr;
  83. // Current offset into the original string to reflect in EndPtr.
  84. size_t Offset = 0;
  85. // Strip leading whitespace.
  86. for ( ; Offset != Str.size(); ++Offset)
  87. if (!isSpace((unsigned char)Str[Offset])) {
  88. Str = Str.substr(Offset);
  89. break;
  90. }
  91. if (Str.empty())
  92. // Fail for empty subject sequences (POSIX allows but doesn't require
  93. // strtol[l]/strtoul[l] to fail with EINVAL).
  94. return nullptr;
  95. // Strip but remember the sign.
  96. bool Negate = Str[0] == '-';
  97. if (Str[0] == '-' || Str[0] == '+') {
  98. Str = Str.drop_front();
  99. if (Str.empty())
  100. // Fail for a sign with nothing after it.
  101. return nullptr;
  102. ++Offset;
  103. }
  104. // Set Max to the absolute value of the minimum (for signed), or
  105. // to the maximum (for unsigned) value representable in the type.
  106. Type *RetTy = CI->getType();
  107. unsigned NBits = RetTy->getPrimitiveSizeInBits();
  108. uint64_t Max = AsSigned && Negate ? 1 : 0;
  109. Max += AsSigned ? maxIntN(NBits) : maxUIntN(NBits);
  110. // Autodetect Base if it's zero and consume the "0x" prefix.
  111. if (Str.size() > 1) {
  112. if (Str[0] == '0') {
  113. if (toUpper((unsigned char)Str[1]) == 'X') {
  114. if (Str.size() == 2 || (Base && Base != 16))
  115. // Fail if Base doesn't allow the "0x" prefix or for the prefix
  116. // alone that implementations like BSD set errno to EINVAL for.
  117. return nullptr;
  118. Str = Str.drop_front(2);
  119. Offset += 2;
  120. Base = 16;
  121. }
  122. else if (Base == 0)
  123. Base = 8;
  124. } else if (Base == 0)
  125. Base = 10;
  126. }
  127. else if (Base == 0)
  128. Base = 10;
  129. // Convert the rest of the subject sequence, not including the sign,
  130. // to its uint64_t representation (this assumes the source character
  131. // set is ASCII).
  132. uint64_t Result = 0;
  133. for (unsigned i = 0; i != Str.size(); ++i) {
  134. unsigned char DigVal = Str[i];
  135. if (isDigit(DigVal))
  136. DigVal = DigVal - '0';
  137. else {
  138. DigVal = toUpper(DigVal);
  139. if (isAlpha(DigVal))
  140. DigVal = DigVal - 'A' + 10;
  141. else
  142. return nullptr;
  143. }
  144. if (DigVal >= Base)
  145. // Fail if the digit is not valid in the Base.
  146. return nullptr;
  147. // Add the digit and fail if the result is not representable in
  148. // the (unsigned form of the) destination type.
  149. bool VFlow;
  150. Result = SaturatingMultiplyAdd(Result, Base, (uint64_t)DigVal, &VFlow);
  151. if (VFlow || Result > Max)
  152. return nullptr;
  153. }
  154. if (EndPtr) {
  155. // Store the pointer to the end.
  156. Value *Off = B.getInt64(Offset + Str.size());
  157. Value *StrBeg = CI->getArgOperand(0);
  158. Value *StrEnd = B.CreateInBoundsGEP(B.getInt8Ty(), StrBeg, Off, "endptr");
  159. B.CreateStore(StrEnd, EndPtr);
  160. }
  161. if (Negate)
  162. // Unsigned negation doesn't overflow.
  163. Result = -Result;
  164. return ConstantInt::get(RetTy, Result);
  165. }
  166. static bool isOnlyUsedInComparisonWithZero(Value *V) {
  167. for (User *U : V->users()) {
  168. if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
  169. if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
  170. if (C->isNullValue())
  171. continue;
  172. // Unknown instruction.
  173. return false;
  174. }
  175. return true;
  176. }
  177. static bool canTransformToMemCmp(CallInst *CI, Value *Str, uint64_t Len,
  178. const DataLayout &DL) {
  179. if (!isOnlyUsedInComparisonWithZero(CI))
  180. return false;
  181. if (!isDereferenceableAndAlignedPointer(Str, Align(1), APInt(64, Len), DL))
  182. return false;
  183. if (CI->getFunction()->hasFnAttribute(Attribute::SanitizeMemory))
  184. return false;
  185. return true;
  186. }
  187. static void annotateDereferenceableBytes(CallInst *CI,
  188. ArrayRef<unsigned> ArgNos,
  189. uint64_t DereferenceableBytes) {
  190. const Function *F = CI->getCaller();
  191. if (!F)
  192. return;
  193. for (unsigned ArgNo : ArgNos) {
  194. uint64_t DerefBytes = DereferenceableBytes;
  195. unsigned AS = CI->getArgOperand(ArgNo)->getType()->getPointerAddressSpace();
  196. if (!llvm::NullPointerIsDefined(F, AS) ||
  197. CI->paramHasAttr(ArgNo, Attribute::NonNull))
  198. DerefBytes = std::max(CI->getParamDereferenceableOrNullBytes(ArgNo),
  199. DereferenceableBytes);
  200. if (CI->getParamDereferenceableBytes(ArgNo) < DerefBytes) {
  201. CI->removeParamAttr(ArgNo, Attribute::Dereferenceable);
  202. if (!llvm::NullPointerIsDefined(F, AS) ||
  203. CI->paramHasAttr(ArgNo, Attribute::NonNull))
  204. CI->removeParamAttr(ArgNo, Attribute::DereferenceableOrNull);
  205. CI->addParamAttr(ArgNo, Attribute::getWithDereferenceableBytes(
  206. CI->getContext(), DerefBytes));
  207. }
  208. }
  209. }
  210. static void annotateNonNullNoUndefBasedOnAccess(CallInst *CI,
  211. ArrayRef<unsigned> ArgNos) {
  212. Function *F = CI->getCaller();
  213. if (!F)
  214. return;
  215. for (unsigned ArgNo : ArgNos) {
  216. if (!CI->paramHasAttr(ArgNo, Attribute::NoUndef))
  217. CI->addParamAttr(ArgNo, Attribute::NoUndef);
  218. if (!CI->paramHasAttr(ArgNo, Attribute::NonNull)) {
  219. unsigned AS =
  220. CI->getArgOperand(ArgNo)->getType()->getPointerAddressSpace();
  221. if (llvm::NullPointerIsDefined(F, AS))
  222. continue;
  223. CI->addParamAttr(ArgNo, Attribute::NonNull);
  224. }
  225. annotateDereferenceableBytes(CI, ArgNo, 1);
  226. }
  227. }
  228. static void annotateNonNullAndDereferenceable(CallInst *CI, ArrayRef<unsigned> ArgNos,
  229. Value *Size, const DataLayout &DL) {
  230. if (ConstantInt *LenC = dyn_cast<ConstantInt>(Size)) {
  231. annotateNonNullNoUndefBasedOnAccess(CI, ArgNos);
  232. annotateDereferenceableBytes(CI, ArgNos, LenC->getZExtValue());
  233. } else if (isKnownNonZero(Size, DL)) {
  234. annotateNonNullNoUndefBasedOnAccess(CI, ArgNos);
  235. const APInt *X, *Y;
  236. uint64_t DerefMin = 1;
  237. if (match(Size, m_Select(m_Value(), m_APInt(X), m_APInt(Y)))) {
  238. DerefMin = std::min(X->getZExtValue(), Y->getZExtValue());
  239. annotateDereferenceableBytes(CI, ArgNos, DerefMin);
  240. }
  241. }
  242. }
  243. // Copy CallInst "flags" like musttail, notail, and tail. Return New param for
  244. // easier chaining. Calls to emit* and B.createCall should probably be wrapped
  245. // in this function when New is created to replace Old. Callers should take
  246. // care to check Old.isMustTailCall() if they aren't replacing Old directly
  247. // with New.
  248. static Value *copyFlags(const CallInst &Old, Value *New) {
  249. assert(!Old.isMustTailCall() && "do not copy musttail call flags");
  250. assert(!Old.isNoTailCall() && "do not copy notail call flags");
  251. if (auto *NewCI = dyn_cast_or_null<CallInst>(New))
  252. NewCI->setTailCallKind(Old.getTailCallKind());
  253. return New;
  254. }
  255. static Value *mergeAttributesAndFlags(CallInst *NewCI, const CallInst &Old) {
  256. NewCI->setAttributes(AttributeList::get(
  257. NewCI->getContext(), {NewCI->getAttributes(), Old.getAttributes()}));
  258. NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
  259. return copyFlags(Old, NewCI);
  260. }
  261. // Helper to avoid truncating the length if size_t is 32-bits.
  262. static StringRef substr(StringRef Str, uint64_t Len) {
  263. return Len >= Str.size() ? Str : Str.substr(0, Len);
  264. }
  265. //===----------------------------------------------------------------------===//
  266. // String and Memory Library Call Optimizations
  267. //===----------------------------------------------------------------------===//
  268. Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilderBase &B) {
  269. // Extract some information from the instruction
  270. Value *Dst = CI->getArgOperand(0);
  271. Value *Src = CI->getArgOperand(1);
  272. annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
  273. // See if we can get the length of the input string.
  274. uint64_t Len = GetStringLength(Src);
  275. if (Len)
  276. annotateDereferenceableBytes(CI, 1, Len);
  277. else
  278. return nullptr;
  279. --Len; // Unbias length.
  280. // Handle the simple, do-nothing case: strcat(x, "") -> x
  281. if (Len == 0)
  282. return Dst;
  283. return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, Len, B));
  284. }
  285. Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
  286. IRBuilderBase &B) {
  287. // We need to find the end of the destination string. That's where the
  288. // memory is to be moved to. We just generate a call to strlen.
  289. Value *DstLen = emitStrLen(Dst, B, DL, TLI);
  290. if (!DstLen)
  291. return nullptr;
  292. // Now that we have the destination's length, we must index into the
  293. // destination's pointer to get the actual memcpy destination (end of
  294. // the string .. we're concatenating).
  295. Value *CpyDst = B.CreateInBoundsGEP(B.getInt8Ty(), Dst, DstLen, "endptr");
  296. // We have enough information to now generate the memcpy call to do the
  297. // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
  298. B.CreateMemCpy(
  299. CpyDst, Align(1), Src, Align(1),
  300. ConstantInt::get(DL.getIntPtrType(Src->getContext()), Len + 1));
  301. return Dst;
  302. }
  303. Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilderBase &B) {
  304. // Extract some information from the instruction.
  305. Value *Dst = CI->getArgOperand(0);
  306. Value *Src = CI->getArgOperand(1);
  307. Value *Size = CI->getArgOperand(2);
  308. uint64_t Len;
  309. annotateNonNullNoUndefBasedOnAccess(CI, 0);
  310. if (isKnownNonZero(Size, DL))
  311. annotateNonNullNoUndefBasedOnAccess(CI, 1);
  312. // We don't do anything if length is not constant.
  313. ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size);
  314. if (LengthArg) {
  315. Len = LengthArg->getZExtValue();
  316. // strncat(x, c, 0) -> x
  317. if (!Len)
  318. return Dst;
  319. } else {
  320. return nullptr;
  321. }
  322. // See if we can get the length of the input string.
  323. uint64_t SrcLen = GetStringLength(Src);
  324. if (SrcLen) {
  325. annotateDereferenceableBytes(CI, 1, SrcLen);
  326. --SrcLen; // Unbias length.
  327. } else {
  328. return nullptr;
  329. }
  330. // strncat(x, "", c) -> x
  331. if (SrcLen == 0)
  332. return Dst;
  333. // We don't optimize this case.
  334. if (Len < SrcLen)
  335. return nullptr;
  336. // strncat(x, s, c) -> strcat(x, s)
  337. // s is constant so the strcat can be optimized further.
  338. return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, SrcLen, B));
  339. }
  340. // Helper to transform memchr(S, C, N) == S to N && *S == C and, when
  341. // NBytes is null, strchr(S, C) to *S == C. A precondition of the function
  342. // is that either S is dereferenceable or the value of N is nonzero.
  343. static Value* memChrToCharCompare(CallInst *CI, Value *NBytes,
  344. IRBuilderBase &B, const DataLayout &DL)
  345. {
  346. Value *Src = CI->getArgOperand(0);
  347. Value *CharVal = CI->getArgOperand(1);
  348. // Fold memchr(A, C, N) == A to N && *A == C.
  349. Type *CharTy = B.getInt8Ty();
  350. Value *Char0 = B.CreateLoad(CharTy, Src);
  351. CharVal = B.CreateTrunc(CharVal, CharTy);
  352. Value *Cmp = B.CreateICmpEQ(Char0, CharVal, "char0cmp");
  353. if (NBytes) {
  354. Value *Zero = ConstantInt::get(NBytes->getType(), 0);
  355. Value *And = B.CreateICmpNE(NBytes, Zero);
  356. Cmp = B.CreateLogicalAnd(And, Cmp);
  357. }
  358. Value *NullPtr = Constant::getNullValue(CI->getType());
  359. return B.CreateSelect(Cmp, Src, NullPtr);
  360. }
  361. Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilderBase &B) {
  362. Value *SrcStr = CI->getArgOperand(0);
  363. Value *CharVal = CI->getArgOperand(1);
  364. annotateNonNullNoUndefBasedOnAccess(CI, 0);
  365. if (isOnlyUsedInEqualityComparison(CI, SrcStr))
  366. return memChrToCharCompare(CI, nullptr, B, DL);
  367. // If the second operand is non-constant, see if we can compute the length
  368. // of the input string and turn this into memchr.
  369. ConstantInt *CharC = dyn_cast<ConstantInt>(CharVal);
  370. if (!CharC) {
  371. uint64_t Len = GetStringLength(SrcStr);
  372. if (Len)
  373. annotateDereferenceableBytes(CI, 0, Len);
  374. else
  375. return nullptr;
  376. Function *Callee = CI->getCalledFunction();
  377. FunctionType *FT = Callee->getFunctionType();
  378. unsigned IntBits = TLI->getIntSize();
  379. if (!FT->getParamType(1)->isIntegerTy(IntBits)) // memchr needs 'int'.
  380. return nullptr;
  381. unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
  382. Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
  383. return copyFlags(*CI,
  384. emitMemChr(SrcStr, CharVal, // include nul.
  385. ConstantInt::get(SizeTTy, Len), B,
  386. DL, TLI));
  387. }
  388. if (CharC->isZero()) {
  389. Value *NullPtr = Constant::getNullValue(CI->getType());
  390. if (isOnlyUsedInEqualityComparison(CI, NullPtr))
  391. // Pre-empt the transformation to strlen below and fold
  392. // strchr(A, '\0') == null to false.
  393. return B.CreateIntToPtr(B.getTrue(), CI->getType());
  394. }
  395. // Otherwise, the character is a constant, see if the first argument is
  396. // a string literal. If so, we can constant fold.
  397. StringRef Str;
  398. if (!getConstantStringInfo(SrcStr, Str)) {
  399. if (CharC->isZero()) // strchr(p, 0) -> p + strlen(p)
  400. if (Value *StrLen = emitStrLen(SrcStr, B, DL, TLI))
  401. return B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, StrLen, "strchr");
  402. return nullptr;
  403. }
  404. // Compute the offset, make sure to handle the case when we're searching for
  405. // zero (a weird way to spell strlen).
  406. size_t I = (0xFF & CharC->getSExtValue()) == 0
  407. ? Str.size()
  408. : Str.find(CharC->getSExtValue());
  409. if (I == StringRef::npos) // Didn't find the char. strchr returns null.
  410. return Constant::getNullValue(CI->getType());
  411. // strchr(s+n,c) -> gep(s+n+i,c)
  412. return B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strchr");
  413. }
  414. Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilderBase &B) {
  415. Value *SrcStr = CI->getArgOperand(0);
  416. Value *CharVal = CI->getArgOperand(1);
  417. ConstantInt *CharC = dyn_cast<ConstantInt>(CharVal);
  418. annotateNonNullNoUndefBasedOnAccess(CI, 0);
  419. StringRef Str;
  420. if (!getConstantStringInfo(SrcStr, Str)) {
  421. // strrchr(s, 0) -> strchr(s, 0)
  422. if (CharC && CharC->isZero())
  423. return copyFlags(*CI, emitStrChr(SrcStr, '\0', B, TLI));
  424. return nullptr;
  425. }
  426. unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
  427. Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
  428. // Try to expand strrchr to the memrchr nonstandard extension if it's
  429. // available, or simply fail otherwise.
  430. uint64_t NBytes = Str.size() + 1; // Include the terminating nul.
  431. Value *Size = ConstantInt::get(SizeTTy, NBytes);
  432. return copyFlags(*CI, emitMemRChr(SrcStr, CharVal, Size, B, DL, TLI));
  433. }
  434. Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilderBase &B) {
  435. Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
  436. if (Str1P == Str2P) // strcmp(x,x) -> 0
  437. return ConstantInt::get(CI->getType(), 0);
  438. StringRef Str1, Str2;
  439. bool HasStr1 = getConstantStringInfo(Str1P, Str1);
  440. bool HasStr2 = getConstantStringInfo(Str2P, Str2);
  441. // strcmp(x, y) -> cnst (if both x and y are constant strings)
  442. if (HasStr1 && HasStr2)
  443. return ConstantInt::get(CI->getType(),
  444. std::clamp(Str1.compare(Str2), -1, 1));
  445. if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
  446. return B.CreateNeg(B.CreateZExt(
  447. B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
  448. if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
  449. return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"),
  450. CI->getType());
  451. // strcmp(P, "x") -> memcmp(P, "x", 2)
  452. uint64_t Len1 = GetStringLength(Str1P);
  453. if (Len1)
  454. annotateDereferenceableBytes(CI, 0, Len1);
  455. uint64_t Len2 = GetStringLength(Str2P);
  456. if (Len2)
  457. annotateDereferenceableBytes(CI, 1, Len2);
  458. if (Len1 && Len2) {
  459. return copyFlags(
  460. *CI, emitMemCmp(Str1P, Str2P,
  461. ConstantInt::get(DL.getIntPtrType(CI->getContext()),
  462. std::min(Len1, Len2)),
  463. B, DL, TLI));
  464. }
  465. // strcmp to memcmp
  466. if (!HasStr1 && HasStr2) {
  467. if (canTransformToMemCmp(CI, Str1P, Len2, DL))
  468. return copyFlags(
  469. *CI,
  470. emitMemCmp(Str1P, Str2P,
  471. ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len2),
  472. B, DL, TLI));
  473. } else if (HasStr1 && !HasStr2) {
  474. if (canTransformToMemCmp(CI, Str2P, Len1, DL))
  475. return copyFlags(
  476. *CI,
  477. emitMemCmp(Str1P, Str2P,
  478. ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len1),
  479. B, DL, TLI));
  480. }
  481. annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
  482. return nullptr;
  483. }
  484. // Optimize a memcmp or, when StrNCmp is true, strncmp call CI with constant
  485. // arrays LHS and RHS and nonconstant Size.
  486. static Value *optimizeMemCmpVarSize(CallInst *CI, Value *LHS, Value *RHS,
  487. Value *Size, bool StrNCmp,
  488. IRBuilderBase &B, const DataLayout &DL);
  489. Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilderBase &B) {
  490. Value *Str1P = CI->getArgOperand(0);
  491. Value *Str2P = CI->getArgOperand(1);
  492. Value *Size = CI->getArgOperand(2);
  493. if (Str1P == Str2P) // strncmp(x,x,n) -> 0
  494. return ConstantInt::get(CI->getType(), 0);
  495. if (isKnownNonZero(Size, DL))
  496. annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
  497. // Get the length argument if it is constant.
  498. uint64_t Length;
  499. if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size))
  500. Length = LengthArg->getZExtValue();
  501. else
  502. return optimizeMemCmpVarSize(CI, Str1P, Str2P, Size, true, B, DL);
  503. if (Length == 0) // strncmp(x,y,0) -> 0
  504. return ConstantInt::get(CI->getType(), 0);
  505. if (Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
  506. return copyFlags(*CI, emitMemCmp(Str1P, Str2P, Size, B, DL, TLI));
  507. StringRef Str1, Str2;
  508. bool HasStr1 = getConstantStringInfo(Str1P, Str1);
  509. bool HasStr2 = getConstantStringInfo(Str2P, Str2);
  510. // strncmp(x, y) -> cnst (if both x and y are constant strings)
  511. if (HasStr1 && HasStr2) {
  512. // Avoid truncating the 64-bit Length to 32 bits in ILP32.
  513. StringRef SubStr1 = substr(Str1, Length);
  514. StringRef SubStr2 = substr(Str2, Length);
  515. return ConstantInt::get(CI->getType(),
  516. std::clamp(SubStr1.compare(SubStr2), -1, 1));
  517. }
  518. if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
  519. return B.CreateNeg(B.CreateZExt(
  520. B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
  521. if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
  522. return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"),
  523. CI->getType());
  524. uint64_t Len1 = GetStringLength(Str1P);
  525. if (Len1)
  526. annotateDereferenceableBytes(CI, 0, Len1);
  527. uint64_t Len2 = GetStringLength(Str2P);
  528. if (Len2)
  529. annotateDereferenceableBytes(CI, 1, Len2);
  530. // strncmp to memcmp
  531. if (!HasStr1 && HasStr2) {
  532. Len2 = std::min(Len2, Length);
  533. if (canTransformToMemCmp(CI, Str1P, Len2, DL))
  534. return copyFlags(
  535. *CI,
  536. emitMemCmp(Str1P, Str2P,
  537. ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len2),
  538. B, DL, TLI));
  539. } else if (HasStr1 && !HasStr2) {
  540. Len1 = std::min(Len1, Length);
  541. if (canTransformToMemCmp(CI, Str2P, Len1, DL))
  542. return copyFlags(
  543. *CI,
  544. emitMemCmp(Str1P, Str2P,
  545. ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len1),
  546. B, DL, TLI));
  547. }
  548. return nullptr;
  549. }
  550. Value *LibCallSimplifier::optimizeStrNDup(CallInst *CI, IRBuilderBase &B) {
  551. Value *Src = CI->getArgOperand(0);
  552. ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  553. uint64_t SrcLen = GetStringLength(Src);
  554. if (SrcLen && Size) {
  555. annotateDereferenceableBytes(CI, 0, SrcLen);
  556. if (SrcLen <= Size->getZExtValue() + 1)
  557. return copyFlags(*CI, emitStrDup(Src, B, TLI));
  558. }
  559. return nullptr;
  560. }
  561. Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilderBase &B) {
  562. Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
  563. if (Dst == Src) // strcpy(x,x) -> x
  564. return Src;
  565. annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
  566. // See if we can get the length of the input string.
  567. uint64_t Len = GetStringLength(Src);
  568. if (Len)
  569. annotateDereferenceableBytes(CI, 1, Len);
  570. else
  571. return nullptr;
  572. // We have enough information to now generate the memcpy call to do the
  573. // copy for us. Make a memcpy to copy the nul byte with align = 1.
  574. CallInst *NewCI =
  575. B.CreateMemCpy(Dst, Align(1), Src, Align(1),
  576. ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len));
  577. mergeAttributesAndFlags(NewCI, *CI);
  578. return Dst;
  579. }
  580. Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilderBase &B) {
  581. Function *Callee = CI->getCalledFunction();
  582. Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
  583. // stpcpy(d,s) -> strcpy(d,s) if the result is not used.
  584. if (CI->use_empty())
  585. return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
  586. if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
  587. Value *StrLen = emitStrLen(Src, B, DL, TLI);
  588. return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
  589. }
  590. // See if we can get the length of the input string.
  591. uint64_t Len = GetStringLength(Src);
  592. if (Len)
  593. annotateDereferenceableBytes(CI, 1, Len);
  594. else
  595. return nullptr;
  596. Type *PT = Callee->getFunctionType()->getParamType(0);
  597. Value *LenV = ConstantInt::get(DL.getIntPtrType(PT), Len);
  598. Value *DstEnd = B.CreateInBoundsGEP(
  599. B.getInt8Ty(), Dst, ConstantInt::get(DL.getIntPtrType(PT), Len - 1));
  600. // We have enough information to now generate the memcpy call to do the
  601. // copy for us. Make a memcpy to copy the nul byte with align = 1.
  602. CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1), LenV);
  603. mergeAttributesAndFlags(NewCI, *CI);
  604. return DstEnd;
  605. }
  606. // Optimize a call to size_t strlcpy(char*, const char*, size_t).
  607. Value *LibCallSimplifier::optimizeStrLCpy(CallInst *CI, IRBuilderBase &B) {
  608. Value *Size = CI->getArgOperand(2);
  609. if (isKnownNonZero(Size, DL))
  610. // Like snprintf, the function stores into the destination only when
  611. // the size argument is nonzero.
  612. annotateNonNullNoUndefBasedOnAccess(CI, 0);
  613. // The function reads the source argument regardless of Size (it returns
  614. // its length).
  615. annotateNonNullNoUndefBasedOnAccess(CI, 1);
  616. uint64_t NBytes;
  617. if (ConstantInt *SizeC = dyn_cast<ConstantInt>(Size))
  618. NBytes = SizeC->getZExtValue();
  619. else
  620. return nullptr;
  621. Value *Dst = CI->getArgOperand(0);
  622. Value *Src = CI->getArgOperand(1);
  623. if (NBytes <= 1) {
  624. if (NBytes == 1)
  625. // For a call to strlcpy(D, S, 1) first store a nul in *D.
  626. B.CreateStore(B.getInt8(0), Dst);
  627. // Transform strlcpy(D, S, 0) to a call to strlen(S).
  628. return copyFlags(*CI, emitStrLen(Src, B, DL, TLI));
  629. }
  630. // Try to determine the length of the source, substituting its size
  631. // when it's not nul-terminated (as it's required to be) to avoid
  632. // reading past its end.
  633. StringRef Str;
  634. if (!getConstantStringInfo(Src, Str, /*TrimAtNul=*/false))
  635. return nullptr;
  636. uint64_t SrcLen = Str.find('\0');
  637. // Set if the terminating nul should be copied by the call to memcpy
  638. // below.
  639. bool NulTerm = SrcLen < NBytes;
  640. if (NulTerm)
  641. // Overwrite NBytes with the number of bytes to copy, including
  642. // the terminating nul.
  643. NBytes = SrcLen + 1;
  644. else {
  645. // Set the length of the source for the function to return to its
  646. // size, and cap NBytes at the same.
  647. SrcLen = std::min(SrcLen, uint64_t(Str.size()));
  648. NBytes = std::min(NBytes - 1, SrcLen);
  649. }
  650. if (SrcLen == 0) {
  651. // Transform strlcpy(D, "", N) to (*D = '\0, 0).
  652. B.CreateStore(B.getInt8(0), Dst);
  653. return ConstantInt::get(CI->getType(), 0);
  654. }
  655. Function *Callee = CI->getCalledFunction();
  656. Type *PT = Callee->getFunctionType()->getParamType(0);
  657. // Transform strlcpy(D, S, N) to memcpy(D, S, N') where N' is the lower
  658. // bound on strlen(S) + 1 and N, optionally followed by a nul store to
  659. // D[N' - 1] if necessary.
  660. CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1),
  661. ConstantInt::get(DL.getIntPtrType(PT), NBytes));
  662. mergeAttributesAndFlags(NewCI, *CI);
  663. if (!NulTerm) {
  664. Value *EndOff = ConstantInt::get(CI->getType(), NBytes);
  665. Value *EndPtr = B.CreateInBoundsGEP(B.getInt8Ty(), Dst, EndOff);
  666. B.CreateStore(B.getInt8(0), EndPtr);
  667. }
  668. // Like snprintf, strlcpy returns the number of nonzero bytes that would
  669. // have been copied if the bound had been sufficiently big (which in this
  670. // case is strlen(Src)).
  671. return ConstantInt::get(CI->getType(), SrcLen);
  672. }
  673. // Optimize a call CI to either stpncpy when RetEnd is true, or to strncpy
  674. // otherwise.
  675. Value *LibCallSimplifier::optimizeStringNCpy(CallInst *CI, bool RetEnd,
  676. IRBuilderBase &B) {
  677. Function *Callee = CI->getCalledFunction();
  678. Value *Dst = CI->getArgOperand(0);
  679. Value *Src = CI->getArgOperand(1);
  680. Value *Size = CI->getArgOperand(2);
  681. if (isKnownNonZero(Size, DL)) {
  682. // Both st{p,r}ncpy(D, S, N) access the source and destination arrays
  683. // only when N is nonzero.
  684. annotateNonNullNoUndefBasedOnAccess(CI, 0);
  685. annotateNonNullNoUndefBasedOnAccess(CI, 1);
  686. }
  687. // If the "bound" argument is known set N to it. Otherwise set it to
  688. // UINT64_MAX and handle it later.
  689. uint64_t N = UINT64_MAX;
  690. if (ConstantInt *SizeC = dyn_cast<ConstantInt>(Size))
  691. N = SizeC->getZExtValue();
  692. if (N == 0)
  693. // Fold st{p,r}ncpy(D, S, 0) to D.
  694. return Dst;
  695. if (N == 1) {
  696. Type *CharTy = B.getInt8Ty();
  697. Value *CharVal = B.CreateLoad(CharTy, Src, "stxncpy.char0");
  698. B.CreateStore(CharVal, Dst);
  699. if (!RetEnd)
  700. // Transform strncpy(D, S, 1) to return (*D = *S), D.
  701. return Dst;
  702. // Transform stpncpy(D, S, 1) to return (*D = *S) ? D + 1 : D.
  703. Value *ZeroChar = ConstantInt::get(CharTy, 0);
  704. Value *Cmp = B.CreateICmpEQ(CharVal, ZeroChar, "stpncpy.char0cmp");
  705. Value *Off1 = B.getInt32(1);
  706. Value *EndPtr = B.CreateInBoundsGEP(CharTy, Dst, Off1, "stpncpy.end");
  707. return B.CreateSelect(Cmp, Dst, EndPtr, "stpncpy.sel");
  708. }
  709. // If the length of the input string is known set SrcLen to it.
  710. uint64_t SrcLen = GetStringLength(Src);
  711. if (SrcLen)
  712. annotateDereferenceableBytes(CI, 1, SrcLen);
  713. else
  714. return nullptr;
  715. --SrcLen; // Unbias length.
  716. if (SrcLen == 0) {
  717. // Transform st{p,r}ncpy(D, "", N) to memset(D, '\0', N) for any N.
  718. Align MemSetAlign =
  719. CI->getAttributes().getParamAttrs(0).getAlignment().valueOrOne();
  720. CallInst *NewCI = B.CreateMemSet(Dst, B.getInt8('\0'), Size, MemSetAlign);
  721. AttrBuilder ArgAttrs(CI->getContext(), CI->getAttributes().getParamAttrs(0));
  722. NewCI->setAttributes(NewCI->getAttributes().addParamAttributes(
  723. CI->getContext(), 0, ArgAttrs));
  724. copyFlags(*CI, NewCI);
  725. return Dst;
  726. }
  727. if (N > SrcLen + 1) {
  728. if (N > 128)
  729. // Bail if N is large or unknown.
  730. return nullptr;
  731. // st{p,r}ncpy(D, "a", N) -> memcpy(D, "a\0\0\0", N) for N <= 128.
  732. StringRef Str;
  733. if (!getConstantStringInfo(Src, Str))
  734. return nullptr;
  735. std::string SrcStr = Str.str();
  736. // Create a bigger, nul-padded array with the same length, SrcLen,
  737. // as the original string.
  738. SrcStr.resize(N, '\0');
  739. Src = B.CreateGlobalString(SrcStr, "str");
  740. }
  741. Type *PT = Callee->getFunctionType()->getParamType(0);
  742. // st{p,r}ncpy(D, S, N) -> memcpy(align 1 D, align 1 S, N) when both
  743. // S and N are constant.
  744. CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1),
  745. ConstantInt::get(DL.getIntPtrType(PT), N));
  746. mergeAttributesAndFlags(NewCI, *CI);
  747. if (!RetEnd)
  748. return Dst;
  749. // stpncpy(D, S, N) returns the address of the first null in D if it writes
  750. // one, otherwise D + N.
  751. Value *Off = B.getInt64(std::min(SrcLen, N));
  752. return B.CreateInBoundsGEP(B.getInt8Ty(), Dst, Off, "endptr");
  753. }
  754. Value *LibCallSimplifier::optimizeStringLength(CallInst *CI, IRBuilderBase &B,
  755. unsigned CharSize,
  756. Value *Bound) {
  757. Value *Src = CI->getArgOperand(0);
  758. Type *CharTy = B.getIntNTy(CharSize);
  759. if (isOnlyUsedInZeroEqualityComparison(CI) &&
  760. (!Bound || isKnownNonZero(Bound, DL))) {
  761. // Fold strlen:
  762. // strlen(x) != 0 --> *x != 0
  763. // strlen(x) == 0 --> *x == 0
  764. // and likewise strnlen with constant N > 0:
  765. // strnlen(x, N) != 0 --> *x != 0
  766. // strnlen(x, N) == 0 --> *x == 0
  767. return B.CreateZExt(B.CreateLoad(CharTy, Src, "char0"),
  768. CI->getType());
  769. }
  770. if (Bound) {
  771. if (ConstantInt *BoundCst = dyn_cast<ConstantInt>(Bound)) {
  772. if (BoundCst->isZero())
  773. // Fold strnlen(s, 0) -> 0 for any s, constant or otherwise.
  774. return ConstantInt::get(CI->getType(), 0);
  775. if (BoundCst->isOne()) {
  776. // Fold strnlen(s, 1) -> *s ? 1 : 0 for any s.
  777. Value *CharVal = B.CreateLoad(CharTy, Src, "strnlen.char0");
  778. Value *ZeroChar = ConstantInt::get(CharTy, 0);
  779. Value *Cmp = B.CreateICmpNE(CharVal, ZeroChar, "strnlen.char0cmp");
  780. return B.CreateZExt(Cmp, CI->getType());
  781. }
  782. }
  783. }
  784. if (uint64_t Len = GetStringLength(Src, CharSize)) {
  785. Value *LenC = ConstantInt::get(CI->getType(), Len - 1);
  786. // Fold strlen("xyz") -> 3 and strnlen("xyz", 2) -> 2
  787. // and strnlen("xyz", Bound) -> min(3, Bound) for nonconstant Bound.
  788. if (Bound)
  789. return B.CreateBinaryIntrinsic(Intrinsic::umin, LenC, Bound);
  790. return LenC;
  791. }
  792. if (Bound)
  793. // Punt for strnlen for now.
  794. return nullptr;
  795. // If s is a constant pointer pointing to a string literal, we can fold
  796. // strlen(s + x) to strlen(s) - x, when x is known to be in the range
  797. // [0, strlen(s)] or the string has a single null terminator '\0' at the end.
  798. // We only try to simplify strlen when the pointer s points to an array
  799. // of CharSize elements. Otherwise, we would need to scale the offset x before
  800. // doing the subtraction. This will make the optimization more complex, and
  801. // it's not very useful because calling strlen for a pointer of other types is
  802. // very uncommon.
  803. if (GEPOperator *GEP = dyn_cast<GEPOperator>(Src)) {
  804. // TODO: Handle subobjects.
  805. if (!isGEPBasedOnPointerToString(GEP, CharSize))
  806. return nullptr;
  807. ConstantDataArraySlice Slice;
  808. if (getConstantDataArrayInfo(GEP->getOperand(0), Slice, CharSize)) {
  809. uint64_t NullTermIdx;
  810. if (Slice.Array == nullptr) {
  811. NullTermIdx = 0;
  812. } else {
  813. NullTermIdx = ~((uint64_t)0);
  814. for (uint64_t I = 0, E = Slice.Length; I < E; ++I) {
  815. if (Slice.Array->getElementAsInteger(I + Slice.Offset) == 0) {
  816. NullTermIdx = I;
  817. break;
  818. }
  819. }
  820. // If the string does not have '\0', leave it to strlen to compute
  821. // its length.
  822. if (NullTermIdx == ~((uint64_t)0))
  823. return nullptr;
  824. }
  825. Value *Offset = GEP->getOperand(2);
  826. KnownBits Known = computeKnownBits(Offset, DL, 0, nullptr, CI, nullptr);
  827. uint64_t ArrSize =
  828. cast<ArrayType>(GEP->getSourceElementType())->getNumElements();
  829. // If Offset is not provably in the range [0, NullTermIdx], we can still
  830. // optimize if we can prove that the program has undefined behavior when
  831. // Offset is outside that range. That is the case when GEP->getOperand(0)
  832. // is a pointer to an object whose memory extent is NullTermIdx+1.
  833. if ((Known.isNonNegative() && Known.getMaxValue().ule(NullTermIdx)) ||
  834. (isa<GlobalVariable>(GEP->getOperand(0)) &&
  835. NullTermIdx == ArrSize - 1)) {
  836. Offset = B.CreateSExtOrTrunc(Offset, CI->getType());
  837. return B.CreateSub(ConstantInt::get(CI->getType(), NullTermIdx),
  838. Offset);
  839. }
  840. }
  841. }
  842. // strlen(x?"foo":"bars") --> x ? 3 : 4
  843. if (SelectInst *SI = dyn_cast<SelectInst>(Src)) {
  844. uint64_t LenTrue = GetStringLength(SI->getTrueValue(), CharSize);
  845. uint64_t LenFalse = GetStringLength(SI->getFalseValue(), CharSize);
  846. if (LenTrue && LenFalse) {
  847. ORE.emit([&]() {
  848. return OptimizationRemark("instcombine", "simplify-libcalls", CI)
  849. << "folded strlen(select) to select of constants";
  850. });
  851. return B.CreateSelect(SI->getCondition(),
  852. ConstantInt::get(CI->getType(), LenTrue - 1),
  853. ConstantInt::get(CI->getType(), LenFalse - 1));
  854. }
  855. }
  856. return nullptr;
  857. }
  858. Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilderBase &B) {
  859. if (Value *V = optimizeStringLength(CI, B, 8))
  860. return V;
  861. annotateNonNullNoUndefBasedOnAccess(CI, 0);
  862. return nullptr;
  863. }
  864. Value *LibCallSimplifier::optimizeStrNLen(CallInst *CI, IRBuilderBase &B) {
  865. Value *Bound = CI->getArgOperand(1);
  866. if (Value *V = optimizeStringLength(CI, B, 8, Bound))
  867. return V;
  868. if (isKnownNonZero(Bound, DL))
  869. annotateNonNullNoUndefBasedOnAccess(CI, 0);
  870. return nullptr;
  871. }
  872. Value *LibCallSimplifier::optimizeWcslen(CallInst *CI, IRBuilderBase &B) {
  873. Module &M = *CI->getModule();
  874. unsigned WCharSize = TLI->getWCharSize(M) * 8;
  875. // We cannot perform this optimization without wchar_size metadata.
  876. if (WCharSize == 0)
  877. return nullptr;
  878. return optimizeStringLength(CI, B, WCharSize);
  879. }
  880. Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilderBase &B) {
  881. StringRef S1, S2;
  882. bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
  883. bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
  884. // strpbrk(s, "") -> nullptr
  885. // strpbrk("", s) -> nullptr
  886. if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
  887. return Constant::getNullValue(CI->getType());
  888. // Constant folding.
  889. if (HasS1 && HasS2) {
  890. size_t I = S1.find_first_of(S2);
  891. if (I == StringRef::npos) // No match.
  892. return Constant::getNullValue(CI->getType());
  893. return B.CreateInBoundsGEP(B.getInt8Ty(), CI->getArgOperand(0),
  894. B.getInt64(I), "strpbrk");
  895. }
  896. // strpbrk(s, "a") -> strchr(s, 'a')
  897. if (HasS2 && S2.size() == 1)
  898. return copyFlags(*CI, emitStrChr(CI->getArgOperand(0), S2[0], B, TLI));
  899. return nullptr;
  900. }
  901. Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilderBase &B) {
  902. Value *EndPtr = CI->getArgOperand(1);
  903. if (isa<ConstantPointerNull>(EndPtr)) {
  904. // With a null EndPtr, this function won't capture the main argument.
  905. // It would be readonly too, except that it still may write to errno.
  906. CI->addParamAttr(0, Attribute::NoCapture);
  907. }
  908. return nullptr;
  909. }
  910. Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilderBase &B) {
  911. StringRef S1, S2;
  912. bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
  913. bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
  914. // strspn(s, "") -> 0
  915. // strspn("", s) -> 0
  916. if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
  917. return Constant::getNullValue(CI->getType());
  918. // Constant folding.
  919. if (HasS1 && HasS2) {
  920. size_t Pos = S1.find_first_not_of(S2);
  921. if (Pos == StringRef::npos)
  922. Pos = S1.size();
  923. return ConstantInt::get(CI->getType(), Pos);
  924. }
  925. return nullptr;
  926. }
  927. Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilderBase &B) {
  928. StringRef S1, S2;
  929. bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
  930. bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
  931. // strcspn("", s) -> 0
  932. if (HasS1 && S1.empty())
  933. return Constant::getNullValue(CI->getType());
  934. // Constant folding.
  935. if (HasS1 && HasS2) {
  936. size_t Pos = S1.find_first_of(S2);
  937. if (Pos == StringRef::npos)
  938. Pos = S1.size();
  939. return ConstantInt::get(CI->getType(), Pos);
  940. }
  941. // strcspn(s, "") -> strlen(s)
  942. if (HasS2 && S2.empty())
  943. return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B, DL, TLI));
  944. return nullptr;
  945. }
  946. Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilderBase &B) {
  947. // fold strstr(x, x) -> x.
  948. if (CI->getArgOperand(0) == CI->getArgOperand(1))
  949. return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
  950. // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
  951. if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
  952. Value *StrLen = emitStrLen(CI->getArgOperand(1), B, DL, TLI);
  953. if (!StrLen)
  954. return nullptr;
  955. Value *StrNCmp = emitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
  956. StrLen, B, DL, TLI);
  957. if (!StrNCmp)
  958. return nullptr;
  959. for (User *U : llvm::make_early_inc_range(CI->users())) {
  960. ICmpInst *Old = cast<ICmpInst>(U);
  961. Value *Cmp =
  962. B.CreateICmp(Old->getPredicate(), StrNCmp,
  963. ConstantInt::getNullValue(StrNCmp->getType()), "cmp");
  964. replaceAllUsesWith(Old, Cmp);
  965. }
  966. return CI;
  967. }
  968. // See if either input string is a constant string.
  969. StringRef SearchStr, ToFindStr;
  970. bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
  971. bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
  972. // fold strstr(x, "") -> x.
  973. if (HasStr2 && ToFindStr.empty())
  974. return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
  975. // If both strings are known, constant fold it.
  976. if (HasStr1 && HasStr2) {
  977. size_t Offset = SearchStr.find(ToFindStr);
  978. if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
  979. return Constant::getNullValue(CI->getType());
  980. // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
  981. Value *Result = castToCStr(CI->getArgOperand(0), B);
  982. Result =
  983. B.CreateConstInBoundsGEP1_64(B.getInt8Ty(), Result, Offset, "strstr");
  984. return B.CreateBitCast(Result, CI->getType());
  985. }
  986. // fold strstr(x, "y") -> strchr(x, 'y').
  987. if (HasStr2 && ToFindStr.size() == 1) {
  988. Value *StrChr = emitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI);
  989. return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr;
  990. }
  991. annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
  992. return nullptr;
  993. }
  994. Value *LibCallSimplifier::optimizeMemRChr(CallInst *CI, IRBuilderBase &B) {
  995. Value *SrcStr = CI->getArgOperand(0);
  996. Value *Size = CI->getArgOperand(2);
  997. annotateNonNullAndDereferenceable(CI, 0, Size, DL);
  998. Value *CharVal = CI->getArgOperand(1);
  999. ConstantInt *LenC = dyn_cast<ConstantInt>(Size);
  1000. Value *NullPtr = Constant::getNullValue(CI->getType());
  1001. if (LenC) {
  1002. if (LenC->isZero())
  1003. // Fold memrchr(x, y, 0) --> null.
  1004. return NullPtr;
  1005. if (LenC->isOne()) {
  1006. // Fold memrchr(x, y, 1) --> *x == y ? x : null for any x and y,
  1007. // constant or otherwise.
  1008. Value *Val = B.CreateLoad(B.getInt8Ty(), SrcStr, "memrchr.char0");
  1009. // Slice off the character's high end bits.
  1010. CharVal = B.CreateTrunc(CharVal, B.getInt8Ty());
  1011. Value *Cmp = B.CreateICmpEQ(Val, CharVal, "memrchr.char0cmp");
  1012. return B.CreateSelect(Cmp, SrcStr, NullPtr, "memrchr.sel");
  1013. }
  1014. }
  1015. StringRef Str;
  1016. if (!getConstantStringInfo(SrcStr, Str, /*TrimAtNul=*/false))
  1017. return nullptr;
  1018. if (Str.size() == 0)
  1019. // If the array is empty fold memrchr(A, C, N) to null for any value
  1020. // of C and N on the basis that the only valid value of N is zero
  1021. // (otherwise the call is undefined).
  1022. return NullPtr;
  1023. uint64_t EndOff = UINT64_MAX;
  1024. if (LenC) {
  1025. EndOff = LenC->getZExtValue();
  1026. if (Str.size() < EndOff)
  1027. // Punt out-of-bounds accesses to sanitizers and/or libc.
  1028. return nullptr;
  1029. }
  1030. if (ConstantInt *CharC = dyn_cast<ConstantInt>(CharVal)) {
  1031. // Fold memrchr(S, C, N) for a constant C.
  1032. size_t Pos = Str.rfind(CharC->getZExtValue(), EndOff);
  1033. if (Pos == StringRef::npos)
  1034. // When the character is not in the source array fold the result
  1035. // to null regardless of Size.
  1036. return NullPtr;
  1037. if (LenC)
  1038. // Fold memrchr(s, c, N) --> s + Pos for constant N > Pos.
  1039. return B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, B.getInt64(Pos));
  1040. if (Str.find(Str[Pos]) == Pos) {
  1041. // When there is just a single occurrence of C in S, i.e., the one
  1042. // in Str[Pos], fold
  1043. // memrchr(s, c, N) --> N <= Pos ? null : s + Pos
  1044. // for nonconstant N.
  1045. Value *Cmp = B.CreateICmpULE(Size, ConstantInt::get(Size->getType(), Pos),
  1046. "memrchr.cmp");
  1047. Value *SrcPlus = B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr,
  1048. B.getInt64(Pos), "memrchr.ptr_plus");
  1049. return B.CreateSelect(Cmp, NullPtr, SrcPlus, "memrchr.sel");
  1050. }
  1051. }
  1052. // Truncate the string to search at most EndOff characters.
  1053. Str = Str.substr(0, EndOff);
  1054. if (Str.find_first_not_of(Str[0]) != StringRef::npos)
  1055. return nullptr;
  1056. // If the source array consists of all equal characters, then for any
  1057. // C and N (whether in bounds or not), fold memrchr(S, C, N) to
  1058. // N != 0 && *S == C ? S + N - 1 : null
  1059. Type *SizeTy = Size->getType();
  1060. Type *Int8Ty = B.getInt8Ty();
  1061. Value *NNeZ = B.CreateICmpNE(Size, ConstantInt::get(SizeTy, 0));
  1062. // Slice off the sought character's high end bits.
  1063. CharVal = B.CreateTrunc(CharVal, Int8Ty);
  1064. Value *CEqS0 = B.CreateICmpEQ(ConstantInt::get(Int8Ty, Str[0]), CharVal);
  1065. Value *And = B.CreateLogicalAnd(NNeZ, CEqS0);
  1066. Value *SizeM1 = B.CreateSub(Size, ConstantInt::get(SizeTy, 1));
  1067. Value *SrcPlus =
  1068. B.CreateInBoundsGEP(Int8Ty, SrcStr, SizeM1, "memrchr.ptr_plus");
  1069. return B.CreateSelect(And, SrcPlus, NullPtr, "memrchr.sel");
  1070. }
  1071. Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) {
  1072. Value *SrcStr = CI->getArgOperand(0);
  1073. Value *Size = CI->getArgOperand(2);
  1074. if (isKnownNonZero(Size, DL)) {
  1075. annotateNonNullNoUndefBasedOnAccess(CI, 0);
  1076. if (isOnlyUsedInEqualityComparison(CI, SrcStr))
  1077. return memChrToCharCompare(CI, Size, B, DL);
  1078. }
  1079. Value *CharVal = CI->getArgOperand(1);
  1080. ConstantInt *CharC = dyn_cast<ConstantInt>(CharVal);
  1081. ConstantInt *LenC = dyn_cast<ConstantInt>(Size);
  1082. Value *NullPtr = Constant::getNullValue(CI->getType());
  1083. // memchr(x, y, 0) -> null
  1084. if (LenC) {
  1085. if (LenC->isZero())
  1086. return NullPtr;
  1087. if (LenC->isOne()) {
  1088. // Fold memchr(x, y, 1) --> *x == y ? x : null for any x and y,
  1089. // constant or otherwise.
  1090. Value *Val = B.CreateLoad(B.getInt8Ty(), SrcStr, "memchr.char0");
  1091. // Slice off the character's high end bits.
  1092. CharVal = B.CreateTrunc(CharVal, B.getInt8Ty());
  1093. Value *Cmp = B.CreateICmpEQ(Val, CharVal, "memchr.char0cmp");
  1094. return B.CreateSelect(Cmp, SrcStr, NullPtr, "memchr.sel");
  1095. }
  1096. }
  1097. StringRef Str;
  1098. if (!getConstantStringInfo(SrcStr, Str, /*TrimAtNul=*/false))
  1099. return nullptr;
  1100. if (CharC) {
  1101. size_t Pos = Str.find(CharC->getZExtValue());
  1102. if (Pos == StringRef::npos)
  1103. // When the character is not in the source array fold the result
  1104. // to null regardless of Size.
  1105. return NullPtr;
  1106. // Fold memchr(s, c, n) -> n <= Pos ? null : s + Pos
  1107. // When the constant Size is less than or equal to the character
  1108. // position also fold the result to null.
  1109. Value *Cmp = B.CreateICmpULE(Size, ConstantInt::get(Size->getType(), Pos),
  1110. "memchr.cmp");
  1111. Value *SrcPlus = B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, B.getInt64(Pos),
  1112. "memchr.ptr");
  1113. return B.CreateSelect(Cmp, NullPtr, SrcPlus);
  1114. }
  1115. if (Str.size() == 0)
  1116. // If the array is empty fold memchr(A, C, N) to null for any value
  1117. // of C and N on the basis that the only valid value of N is zero
  1118. // (otherwise the call is undefined).
  1119. return NullPtr;
  1120. if (LenC)
  1121. Str = substr(Str, LenC->getZExtValue());
  1122. size_t Pos = Str.find_first_not_of(Str[0]);
  1123. if (Pos == StringRef::npos
  1124. || Str.find_first_not_of(Str[Pos], Pos) == StringRef::npos) {
  1125. // If the source array consists of at most two consecutive sequences
  1126. // of the same characters, then for any C and N (whether in bounds or
  1127. // not), fold memchr(S, C, N) to
  1128. // N != 0 && *S == C ? S : null
  1129. // or for the two sequences to:
  1130. // N != 0 && *S == C ? S : (N > Pos && S[Pos] == C ? S + Pos : null)
  1131. // ^Sel2 ^Sel1 are denoted above.
  1132. // The latter makes it also possible to fold strchr() calls with strings
  1133. // of the same characters.
  1134. Type *SizeTy = Size->getType();
  1135. Type *Int8Ty = B.getInt8Ty();
  1136. // Slice off the sought character's high end bits.
  1137. CharVal = B.CreateTrunc(CharVal, Int8Ty);
  1138. Value *Sel1 = NullPtr;
  1139. if (Pos != StringRef::npos) {
  1140. // Handle two consecutive sequences of the same characters.
  1141. Value *PosVal = ConstantInt::get(SizeTy, Pos);
  1142. Value *StrPos = ConstantInt::get(Int8Ty, Str[Pos]);
  1143. Value *CEqSPos = B.CreateICmpEQ(CharVal, StrPos);
  1144. Value *NGtPos = B.CreateICmp(ICmpInst::ICMP_UGT, Size, PosVal);
  1145. Value *And = B.CreateAnd(CEqSPos, NGtPos);
  1146. Value *SrcPlus = B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, PosVal);
  1147. Sel1 = B.CreateSelect(And, SrcPlus, NullPtr, "memchr.sel1");
  1148. }
  1149. Value *Str0 = ConstantInt::get(Int8Ty, Str[0]);
  1150. Value *CEqS0 = B.CreateICmpEQ(Str0, CharVal);
  1151. Value *NNeZ = B.CreateICmpNE(Size, ConstantInt::get(SizeTy, 0));
  1152. Value *And = B.CreateAnd(NNeZ, CEqS0);
  1153. return B.CreateSelect(And, SrcStr, Sel1, "memchr.sel2");
  1154. }
  1155. if (!LenC) {
  1156. if (isOnlyUsedInEqualityComparison(CI, SrcStr))
  1157. // S is dereferenceable so it's safe to load from it and fold
  1158. // memchr(S, C, N) == S to N && *S == C for any C and N.
  1159. // TODO: This is safe even even for nonconstant S.
  1160. return memChrToCharCompare(CI, Size, B, DL);
  1161. // From now on we need a constant length and constant array.
  1162. return nullptr;
  1163. }
  1164. // If the char is variable but the input str and length are not we can turn
  1165. // this memchr call into a simple bit field test. Of course this only works
  1166. // when the return value is only checked against null.
  1167. //
  1168. // It would be really nice to reuse switch lowering here but we can't change
  1169. // the CFG at this point.
  1170. //
  1171. // memchr("\r\n", C, 2) != nullptr -> (1 << C & ((1 << '\r') | (1 << '\n')))
  1172. // != 0
  1173. // after bounds check.
  1174. if (Str.empty() || !isOnlyUsedInZeroEqualityComparison(CI))
  1175. return nullptr;
  1176. unsigned char Max =
  1177. *std::max_element(reinterpret_cast<const unsigned char *>(Str.begin()),
  1178. reinterpret_cast<const unsigned char *>(Str.end()));
  1179. // Make sure the bit field we're about to create fits in a register on the
  1180. // target.
  1181. // FIXME: On a 64 bit architecture this prevents us from using the
  1182. // interesting range of alpha ascii chars. We could do better by emitting
  1183. // two bitfields or shifting the range by 64 if no lower chars are used.
  1184. if (!DL.fitsInLegalInteger(Max + 1))
  1185. return nullptr;
  1186. // For the bit field use a power-of-2 type with at least 8 bits to avoid
  1187. // creating unnecessary illegal types.
  1188. unsigned char Width = NextPowerOf2(std::max((unsigned char)7, Max));
  1189. // Now build the bit field.
  1190. APInt Bitfield(Width, 0);
  1191. for (char C : Str)
  1192. Bitfield.setBit((unsigned char)C);
  1193. Value *BitfieldC = B.getInt(Bitfield);
  1194. // Adjust width of "C" to the bitfield width, then mask off the high bits.
  1195. Value *C = B.CreateZExtOrTrunc(CharVal, BitfieldC->getType());
  1196. C = B.CreateAnd(C, B.getIntN(Width, 0xFF));
  1197. // First check that the bit field access is within bounds.
  1198. Value *Bounds = B.CreateICmp(ICmpInst::ICMP_ULT, C, B.getIntN(Width, Width),
  1199. "memchr.bounds");
  1200. // Create code that checks if the given bit is set in the field.
  1201. Value *Shl = B.CreateShl(B.getIntN(Width, 1ULL), C);
  1202. Value *Bits = B.CreateIsNotNull(B.CreateAnd(Shl, BitfieldC), "memchr.bits");
  1203. // Finally merge both checks and cast to pointer type. The inttoptr
  1204. // implicitly zexts the i1 to intptr type.
  1205. return B.CreateIntToPtr(B.CreateLogicalAnd(Bounds, Bits, "memchr"),
  1206. CI->getType());
  1207. }
  1208. // Optimize a memcmp or, when StrNCmp is true, strncmp call CI with constant
  1209. // arrays LHS and RHS and nonconstant Size.
  1210. static Value *optimizeMemCmpVarSize(CallInst *CI, Value *LHS, Value *RHS,
  1211. Value *Size, bool StrNCmp,
  1212. IRBuilderBase &B, const DataLayout &DL) {
  1213. if (LHS == RHS) // memcmp(s,s,x) -> 0
  1214. return Constant::getNullValue(CI->getType());
  1215. StringRef LStr, RStr;
  1216. if (!getConstantStringInfo(LHS, LStr, /*TrimAtNul=*/false) ||
  1217. !getConstantStringInfo(RHS, RStr, /*TrimAtNul=*/false))
  1218. return nullptr;
  1219. // If the contents of both constant arrays are known, fold a call to
  1220. // memcmp(A, B, N) to
  1221. // N <= Pos ? 0 : (A < B ? -1 : B < A ? +1 : 0)
  1222. // where Pos is the first mismatch between A and B, determined below.
  1223. uint64_t Pos = 0;
  1224. Value *Zero = ConstantInt::get(CI->getType(), 0);
  1225. for (uint64_t MinSize = std::min(LStr.size(), RStr.size()); ; ++Pos) {
  1226. if (Pos == MinSize ||
  1227. (StrNCmp && (LStr[Pos] == '\0' && RStr[Pos] == '\0'))) {
  1228. // One array is a leading part of the other of equal or greater
  1229. // size, or for strncmp, the arrays are equal strings.
  1230. // Fold the result to zero. Size is assumed to be in bounds, since
  1231. // otherwise the call would be undefined.
  1232. return Zero;
  1233. }
  1234. if (LStr[Pos] != RStr[Pos])
  1235. break;
  1236. }
  1237. // Normalize the result.
  1238. typedef unsigned char UChar;
  1239. int IRes = UChar(LStr[Pos]) < UChar(RStr[Pos]) ? -1 : 1;
  1240. Value *MaxSize = ConstantInt::get(Size->getType(), Pos);
  1241. Value *Cmp = B.CreateICmp(ICmpInst::ICMP_ULE, Size, MaxSize);
  1242. Value *Res = ConstantInt::get(CI->getType(), IRes);
  1243. return B.CreateSelect(Cmp, Zero, Res);
  1244. }
  1245. // Optimize a memcmp call CI with constant size Len.
  1246. static Value *optimizeMemCmpConstantSize(CallInst *CI, Value *LHS, Value *RHS,
  1247. uint64_t Len, IRBuilderBase &B,
  1248. const DataLayout &DL) {
  1249. if (Len == 0) // memcmp(s1,s2,0) -> 0
  1250. return Constant::getNullValue(CI->getType());
  1251. // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
  1252. if (Len == 1) {
  1253. Value *LHSV =
  1254. B.CreateZExt(B.CreateLoad(B.getInt8Ty(), castToCStr(LHS, B), "lhsc"),
  1255. CI->getType(), "lhsv");
  1256. Value *RHSV =
  1257. B.CreateZExt(B.CreateLoad(B.getInt8Ty(), castToCStr(RHS, B), "rhsc"),
  1258. CI->getType(), "rhsv");
  1259. return B.CreateSub(LHSV, RHSV, "chardiff");
  1260. }
  1261. // memcmp(S1,S2,N/8)==0 -> (*(intN_t*)S1 != *(intN_t*)S2)==0
  1262. // TODO: The case where both inputs are constants does not need to be limited
  1263. // to legal integers or equality comparison. See block below this.
  1264. if (DL.isLegalInteger(Len * 8) && isOnlyUsedInZeroEqualityComparison(CI)) {
  1265. IntegerType *IntType = IntegerType::get(CI->getContext(), Len * 8);
  1266. Align PrefAlignment = DL.getPrefTypeAlign(IntType);
  1267. // First, see if we can fold either argument to a constant.
  1268. Value *LHSV = nullptr;
  1269. if (auto *LHSC = dyn_cast<Constant>(LHS)) {
  1270. LHSC = ConstantExpr::getBitCast(LHSC, IntType->getPointerTo());
  1271. LHSV = ConstantFoldLoadFromConstPtr(LHSC, IntType, DL);
  1272. }
  1273. Value *RHSV = nullptr;
  1274. if (auto *RHSC = dyn_cast<Constant>(RHS)) {
  1275. RHSC = ConstantExpr::getBitCast(RHSC, IntType->getPointerTo());
  1276. RHSV = ConstantFoldLoadFromConstPtr(RHSC, IntType, DL);
  1277. }
  1278. // Don't generate unaligned loads. If either source is constant data,
  1279. // alignment doesn't matter for that source because there is no load.
  1280. if ((LHSV || getKnownAlignment(LHS, DL, CI) >= PrefAlignment) &&
  1281. (RHSV || getKnownAlignment(RHS, DL, CI) >= PrefAlignment)) {
  1282. if (!LHSV) {
  1283. Type *LHSPtrTy =
  1284. IntType->getPointerTo(LHS->getType()->getPointerAddressSpace());
  1285. LHSV = B.CreateLoad(IntType, B.CreateBitCast(LHS, LHSPtrTy), "lhsv");
  1286. }
  1287. if (!RHSV) {
  1288. Type *RHSPtrTy =
  1289. IntType->getPointerTo(RHS->getType()->getPointerAddressSpace());
  1290. RHSV = B.CreateLoad(IntType, B.CreateBitCast(RHS, RHSPtrTy), "rhsv");
  1291. }
  1292. return B.CreateZExt(B.CreateICmpNE(LHSV, RHSV), CI->getType(), "memcmp");
  1293. }
  1294. }
  1295. return nullptr;
  1296. }
  1297. // Most simplifications for memcmp also apply to bcmp.
  1298. Value *LibCallSimplifier::optimizeMemCmpBCmpCommon(CallInst *CI,
  1299. IRBuilderBase &B) {
  1300. Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
  1301. Value *Size = CI->getArgOperand(2);
  1302. annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
  1303. if (Value *Res = optimizeMemCmpVarSize(CI, LHS, RHS, Size, false, B, DL))
  1304. return Res;
  1305. // Handle constant Size.
  1306. ConstantInt *LenC = dyn_cast<ConstantInt>(Size);
  1307. if (!LenC)
  1308. return nullptr;
  1309. return optimizeMemCmpConstantSize(CI, LHS, RHS, LenC->getZExtValue(), B, DL);
  1310. }
  1311. Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilderBase &B) {
  1312. Module *M = CI->getModule();
  1313. if (Value *V = optimizeMemCmpBCmpCommon(CI, B))
  1314. return V;
  1315. // memcmp(x, y, Len) == 0 -> bcmp(x, y, Len) == 0
  1316. // bcmp can be more efficient than memcmp because it only has to know that
  1317. // there is a difference, not how different one is to the other.
  1318. if (isLibFuncEmittable(M, TLI, LibFunc_bcmp) &&
  1319. isOnlyUsedInZeroEqualityComparison(CI)) {
  1320. Value *LHS = CI->getArgOperand(0);
  1321. Value *RHS = CI->getArgOperand(1);
  1322. Value *Size = CI->getArgOperand(2);
  1323. return copyFlags(*CI, emitBCmp(LHS, RHS, Size, B, DL, TLI));
  1324. }
  1325. return nullptr;
  1326. }
  1327. Value *LibCallSimplifier::optimizeBCmp(CallInst *CI, IRBuilderBase &B) {
  1328. return optimizeMemCmpBCmpCommon(CI, B);
  1329. }
  1330. Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilderBase &B) {
  1331. Value *Size = CI->getArgOperand(2);
  1332. annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
  1333. if (isa<IntrinsicInst>(CI))
  1334. return nullptr;
  1335. // memcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n)
  1336. CallInst *NewCI = B.CreateMemCpy(CI->getArgOperand(0), Align(1),
  1337. CI->getArgOperand(1), Align(1), Size);
  1338. mergeAttributesAndFlags(NewCI, *CI);
  1339. return CI->getArgOperand(0);
  1340. }
  1341. Value *LibCallSimplifier::optimizeMemCCpy(CallInst *CI, IRBuilderBase &B) {
  1342. Value *Dst = CI->getArgOperand(0);
  1343. Value *Src = CI->getArgOperand(1);
  1344. ConstantInt *StopChar = dyn_cast<ConstantInt>(CI->getArgOperand(2));
  1345. ConstantInt *N = dyn_cast<ConstantInt>(CI->getArgOperand(3));
  1346. StringRef SrcStr;
  1347. if (CI->use_empty() && Dst == Src)
  1348. return Dst;
  1349. // memccpy(d, s, c, 0) -> nullptr
  1350. if (N) {
  1351. if (N->isNullValue())
  1352. return Constant::getNullValue(CI->getType());
  1353. if (!getConstantStringInfo(Src, SrcStr, /*TrimAtNul=*/false) ||
  1354. // TODO: Handle zeroinitializer.
  1355. !StopChar)
  1356. return nullptr;
  1357. } else {
  1358. return nullptr;
  1359. }
  1360. // Wrap arg 'c' of type int to char
  1361. size_t Pos = SrcStr.find(StopChar->getSExtValue() & 0xFF);
  1362. if (Pos == StringRef::npos) {
  1363. if (N->getZExtValue() <= SrcStr.size()) {
  1364. copyFlags(*CI, B.CreateMemCpy(Dst, Align(1), Src, Align(1),
  1365. CI->getArgOperand(3)));
  1366. return Constant::getNullValue(CI->getType());
  1367. }
  1368. return nullptr;
  1369. }
  1370. Value *NewN =
  1371. ConstantInt::get(N->getType(), std::min(uint64_t(Pos + 1), N->getZExtValue()));
  1372. // memccpy -> llvm.memcpy
  1373. copyFlags(*CI, B.CreateMemCpy(Dst, Align(1), Src, Align(1), NewN));
  1374. return Pos + 1 <= N->getZExtValue()
  1375. ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, NewN)
  1376. : Constant::getNullValue(CI->getType());
  1377. }
  1378. Value *LibCallSimplifier::optimizeMemPCpy(CallInst *CI, IRBuilderBase &B) {
  1379. Value *Dst = CI->getArgOperand(0);
  1380. Value *N = CI->getArgOperand(2);
  1381. // mempcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n), x + n
  1382. CallInst *NewCI =
  1383. B.CreateMemCpy(Dst, Align(1), CI->getArgOperand(1), Align(1), N);
  1384. // Propagate attributes, but memcpy has no return value, so make sure that
  1385. // any return attributes are compliant.
  1386. // TODO: Attach return value attributes to the 1st operand to preserve them?
  1387. mergeAttributesAndFlags(NewCI, *CI);
  1388. return B.CreateInBoundsGEP(B.getInt8Ty(), Dst, N);
  1389. }
  1390. Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilderBase &B) {
  1391. Value *Size = CI->getArgOperand(2);
  1392. annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
  1393. if (isa<IntrinsicInst>(CI))
  1394. return nullptr;
  1395. // memmove(x, y, n) -> llvm.memmove(align 1 x, align 1 y, n)
  1396. CallInst *NewCI = B.CreateMemMove(CI->getArgOperand(0), Align(1),
  1397. CI->getArgOperand(1), Align(1), Size);
  1398. mergeAttributesAndFlags(NewCI, *CI);
  1399. return CI->getArgOperand(0);
  1400. }
  1401. Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilderBase &B) {
  1402. Value *Size = CI->getArgOperand(2);
  1403. annotateNonNullAndDereferenceable(CI, 0, Size, DL);
  1404. if (isa<IntrinsicInst>(CI))
  1405. return nullptr;
  1406. // memset(p, v, n) -> llvm.memset(align 1 p, v, n)
  1407. Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
  1408. CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val, Size, Align(1));
  1409. mergeAttributesAndFlags(NewCI, *CI);
  1410. return CI->getArgOperand(0);
  1411. }
  1412. Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilderBase &B) {
  1413. if (isa<ConstantPointerNull>(CI->getArgOperand(0)))
  1414. return copyFlags(*CI, emitMalloc(CI->getArgOperand(1), B, DL, TLI));
  1415. return nullptr;
  1416. }
  1417. //===----------------------------------------------------------------------===//
  1418. // Math Library Optimizations
  1419. //===----------------------------------------------------------------------===//
  1420. // Replace a libcall \p CI with a call to intrinsic \p IID
  1421. static Value *replaceUnaryCall(CallInst *CI, IRBuilderBase &B,
  1422. Intrinsic::ID IID) {
  1423. // Propagate fast-math flags from the existing call to the new call.
  1424. IRBuilderBase::FastMathFlagGuard Guard(B);
  1425. B.setFastMathFlags(CI->getFastMathFlags());
  1426. Module *M = CI->getModule();
  1427. Value *V = CI->getArgOperand(0);
  1428. Function *F = Intrinsic::getDeclaration(M, IID, CI->getType());
  1429. CallInst *NewCall = B.CreateCall(F, V);
  1430. NewCall->takeName(CI);
  1431. return copyFlags(*CI, NewCall);
  1432. }
  1433. /// Return a variant of Val with float type.
  1434. /// Currently this works in two cases: If Val is an FPExtension of a float
  1435. /// value to something bigger, simply return the operand.
  1436. /// If Val is a ConstantFP but can be converted to a float ConstantFP without
  1437. /// loss of precision do so.
  1438. static Value *valueHasFloatPrecision(Value *Val) {
  1439. if (FPExtInst *Cast = dyn_cast<FPExtInst>(Val)) {
  1440. Value *Op = Cast->getOperand(0);
  1441. if (Op->getType()->isFloatTy())
  1442. return Op;
  1443. }
  1444. if (ConstantFP *Const = dyn_cast<ConstantFP>(Val)) {
  1445. APFloat F = Const->getValueAPF();
  1446. bool losesInfo;
  1447. (void)F.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
  1448. &losesInfo);
  1449. if (!losesInfo)
  1450. return ConstantFP::get(Const->getContext(), F);
  1451. }
  1452. return nullptr;
  1453. }
  1454. /// Shrink double -> float functions.
  1455. static Value *optimizeDoubleFP(CallInst *CI, IRBuilderBase &B,
  1456. bool isBinary, const TargetLibraryInfo *TLI,
  1457. bool isPrecise = false) {
  1458. Function *CalleeFn = CI->getCalledFunction();
  1459. if (!CI->getType()->isDoubleTy() || !CalleeFn)
  1460. return nullptr;
  1461. // If not all the uses of the function are converted to float, then bail out.
  1462. // This matters if the precision of the result is more important than the
  1463. // precision of the arguments.
  1464. if (isPrecise)
  1465. for (User *U : CI->users()) {
  1466. FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);
  1467. if (!Cast || !Cast->getType()->isFloatTy())
  1468. return nullptr;
  1469. }
  1470. // If this is something like 'g((double) float)', convert to 'gf(float)'.
  1471. Value *V[2];
  1472. V[0] = valueHasFloatPrecision(CI->getArgOperand(0));
  1473. V[1] = isBinary ? valueHasFloatPrecision(CI->getArgOperand(1)) : nullptr;
  1474. if (!V[0] || (isBinary && !V[1]))
  1475. return nullptr;
  1476. // If call isn't an intrinsic, check that it isn't within a function with the
  1477. // same name as the float version of this call, otherwise the result is an
  1478. // infinite loop. For example, from MinGW-w64:
  1479. //
  1480. // float expf(float val) { return (float) exp((double) val); }
  1481. StringRef CalleeName = CalleeFn->getName();
  1482. bool IsIntrinsic = CalleeFn->isIntrinsic();
  1483. if (!IsIntrinsic) {
  1484. StringRef CallerName = CI->getFunction()->getName();
  1485. if (!CallerName.empty() && CallerName.back() == 'f' &&
  1486. CallerName.size() == (CalleeName.size() + 1) &&
  1487. CallerName.startswith(CalleeName))
  1488. return nullptr;
  1489. }
  1490. // Propagate the math semantics from the current function to the new function.
  1491. IRBuilderBase::FastMathFlagGuard Guard(B);
  1492. B.setFastMathFlags(CI->getFastMathFlags());
  1493. // g((double) float) -> (double) gf(float)
  1494. Value *R;
  1495. if (IsIntrinsic) {
  1496. Module *M = CI->getModule();
  1497. Intrinsic::ID IID = CalleeFn->getIntrinsicID();
  1498. Function *Fn = Intrinsic::getDeclaration(M, IID, B.getFloatTy());
  1499. R = isBinary ? B.CreateCall(Fn, V) : B.CreateCall(Fn, V[0]);
  1500. } else {
  1501. AttributeList CalleeAttrs = CalleeFn->getAttributes();
  1502. R = isBinary ? emitBinaryFloatFnCall(V[0], V[1], TLI, CalleeName, B,
  1503. CalleeAttrs)
  1504. : emitUnaryFloatFnCall(V[0], TLI, CalleeName, B, CalleeAttrs);
  1505. }
  1506. return B.CreateFPExt(R, B.getDoubleTy());
  1507. }
  1508. /// Shrink double -> float for unary functions.
  1509. static Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilderBase &B,
  1510. const TargetLibraryInfo *TLI,
  1511. bool isPrecise = false) {
  1512. return optimizeDoubleFP(CI, B, false, TLI, isPrecise);
  1513. }
  1514. /// Shrink double -> float for binary functions.
  1515. static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilderBase &B,
  1516. const TargetLibraryInfo *TLI,
  1517. bool isPrecise = false) {
  1518. return optimizeDoubleFP(CI, B, true, TLI, isPrecise);
  1519. }
  1520. // cabs(z) -> sqrt((creal(z)*creal(z)) + (cimag(z)*cimag(z)))
  1521. Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilderBase &B) {
  1522. if (!CI->isFast())
  1523. return nullptr;
  1524. // Propagate fast-math flags from the existing call to new instructions.
  1525. IRBuilderBase::FastMathFlagGuard Guard(B);
  1526. B.setFastMathFlags(CI->getFastMathFlags());
  1527. Value *Real, *Imag;
  1528. if (CI->arg_size() == 1) {
  1529. Value *Op = CI->getArgOperand(0);
  1530. assert(Op->getType()->isArrayTy() && "Unexpected signature for cabs!");
  1531. Real = B.CreateExtractValue(Op, 0, "real");
  1532. Imag = B.CreateExtractValue(Op, 1, "imag");
  1533. } else {
  1534. assert(CI->arg_size() == 2 && "Unexpected signature for cabs!");
  1535. Real = CI->getArgOperand(0);
  1536. Imag = CI->getArgOperand(1);
  1537. }
  1538. Value *RealReal = B.CreateFMul(Real, Real);
  1539. Value *ImagImag = B.CreateFMul(Imag, Imag);
  1540. Function *FSqrt = Intrinsic::getDeclaration(CI->getModule(), Intrinsic::sqrt,
  1541. CI->getType());
  1542. return copyFlags(
  1543. *CI, B.CreateCall(FSqrt, B.CreateFAdd(RealReal, ImagImag), "cabs"));
  1544. }
  1545. static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func,
  1546. IRBuilderBase &B) {
  1547. if (!isa<FPMathOperator>(Call))
  1548. return nullptr;
  1549. IRBuilderBase::FastMathFlagGuard Guard(B);
  1550. B.setFastMathFlags(Call->getFastMathFlags());
  1551. // TODO: Can this be shared to also handle LLVM intrinsics?
  1552. Value *X;
  1553. switch (Func) {
  1554. case LibFunc_sin:
  1555. case LibFunc_sinf:
  1556. case LibFunc_sinl:
  1557. case LibFunc_tan:
  1558. case LibFunc_tanf:
  1559. case LibFunc_tanl:
  1560. // sin(-X) --> -sin(X)
  1561. // tan(-X) --> -tan(X)
  1562. if (match(Call->getArgOperand(0), m_OneUse(m_FNeg(m_Value(X)))))
  1563. return B.CreateFNeg(
  1564. copyFlags(*Call, B.CreateCall(Call->getCalledFunction(), X)));
  1565. break;
  1566. case LibFunc_cos:
  1567. case LibFunc_cosf:
  1568. case LibFunc_cosl:
  1569. // cos(-X) --> cos(X)
  1570. if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))))
  1571. return copyFlags(*Call,
  1572. B.CreateCall(Call->getCalledFunction(), X, "cos"));
  1573. break;
  1574. default:
  1575. break;
  1576. }
  1577. return nullptr;
  1578. }
  1579. // Return a properly extended integer (DstWidth bits wide) if the operation is
  1580. // an itofp.
  1581. static Value *getIntToFPVal(Value *I2F, IRBuilderBase &B, unsigned DstWidth) {
  1582. if (isa<SIToFPInst>(I2F) || isa<UIToFPInst>(I2F)) {
  1583. Value *Op = cast<Instruction>(I2F)->getOperand(0);
  1584. // Make sure that the exponent fits inside an "int" of size DstWidth,
  1585. // thus avoiding any range issues that FP has not.
  1586. unsigned BitWidth = Op->getType()->getPrimitiveSizeInBits();
  1587. if (BitWidth < DstWidth ||
  1588. (BitWidth == DstWidth && isa<SIToFPInst>(I2F)))
  1589. return isa<SIToFPInst>(I2F) ? B.CreateSExt(Op, B.getIntNTy(DstWidth))
  1590. : B.CreateZExt(Op, B.getIntNTy(DstWidth));
  1591. }
  1592. return nullptr;
  1593. }
  1594. /// Use exp{,2}(x * y) for pow(exp{,2}(x), y);
  1595. /// ldexp(1.0, x) for pow(2.0, itofp(x)); exp2(n * x) for pow(2.0 ** n, x);
  1596. /// exp10(x) for pow(10.0, x); exp2(log2(n) * x) for pow(n, x).
  1597. Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
  1598. Module *M = Pow->getModule();
  1599. Value *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
  1600. Module *Mod = Pow->getModule();
  1601. Type *Ty = Pow->getType();
  1602. bool Ignored;
  1603. // Evaluate special cases related to a nested function as the base.
  1604. // pow(exp(x), y) -> exp(x * y)
  1605. // pow(exp2(x), y) -> exp2(x * y)
  1606. // If exp{,2}() is used only once, it is better to fold two transcendental
  1607. // math functions into one. If used again, exp{,2}() would still have to be
  1608. // called with the original argument, then keep both original transcendental
  1609. // functions. However, this transformation is only safe with fully relaxed
  1610. // math semantics, since, besides rounding differences, it changes overflow
  1611. // and underflow behavior quite dramatically. For example:
  1612. // pow(exp(1000), 0.001) = pow(inf, 0.001) = inf
  1613. // Whereas:
  1614. // exp(1000 * 0.001) = exp(1)
  1615. // TODO: Loosen the requirement for fully relaxed math semantics.
  1616. // TODO: Handle exp10() when more targets have it available.
  1617. CallInst *BaseFn = dyn_cast<CallInst>(Base);
  1618. if (BaseFn && BaseFn->hasOneUse() && BaseFn->isFast() && Pow->isFast()) {
  1619. LibFunc LibFn;
  1620. Function *CalleeFn = BaseFn->getCalledFunction();
  1621. if (CalleeFn && TLI->getLibFunc(CalleeFn->getName(), LibFn) &&
  1622. isLibFuncEmittable(M, TLI, LibFn)) {
  1623. StringRef ExpName;
  1624. Intrinsic::ID ID;
  1625. Value *ExpFn;
  1626. LibFunc LibFnFloat, LibFnDouble, LibFnLongDouble;
  1627. switch (LibFn) {
  1628. default:
  1629. return nullptr;
  1630. case LibFunc_expf:
  1631. case LibFunc_exp:
  1632. case LibFunc_expl:
  1633. ExpName = TLI->getName(LibFunc_exp);
  1634. ID = Intrinsic::exp;
  1635. LibFnFloat = LibFunc_expf;
  1636. LibFnDouble = LibFunc_exp;
  1637. LibFnLongDouble = LibFunc_expl;
  1638. break;
  1639. case LibFunc_exp2f:
  1640. case LibFunc_exp2:
  1641. case LibFunc_exp2l:
  1642. ExpName = TLI->getName(LibFunc_exp2);
  1643. ID = Intrinsic::exp2;
  1644. LibFnFloat = LibFunc_exp2f;
  1645. LibFnDouble = LibFunc_exp2;
  1646. LibFnLongDouble = LibFunc_exp2l;
  1647. break;
  1648. }
  1649. // Create new exp{,2}() with the product as its argument.
  1650. Value *FMul = B.CreateFMul(BaseFn->getArgOperand(0), Expo, "mul");
  1651. ExpFn = BaseFn->doesNotAccessMemory()
  1652. ? B.CreateCall(Intrinsic::getDeclaration(Mod, ID, Ty),
  1653. FMul, ExpName)
  1654. : emitUnaryFloatFnCall(FMul, TLI, LibFnDouble, LibFnFloat,
  1655. LibFnLongDouble, B,
  1656. BaseFn->getAttributes());
  1657. // Since the new exp{,2}() is different from the original one, dead code
  1658. // elimination cannot be trusted to remove it, since it may have side
  1659. // effects (e.g., errno). When the only consumer for the original
  1660. // exp{,2}() is pow(), then it has to be explicitly erased.
  1661. substituteInParent(BaseFn, ExpFn);
  1662. return ExpFn;
  1663. }
  1664. }
  1665. // Evaluate special cases related to a constant base.
  1666. const APFloat *BaseF;
  1667. if (!match(Pow->getArgOperand(0), m_APFloat(BaseF)))
  1668. return nullptr;
  1669. AttributeList NoAttrs; // Attributes are only meaningful on the original call
  1670. // pow(2.0, itofp(x)) -> ldexp(1.0, x)
  1671. if (match(Base, m_SpecificFP(2.0)) &&
  1672. (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo)) &&
  1673. hasFloatFn(M, TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl)) {
  1674. if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize()))
  1675. return copyFlags(*Pow,
  1676. emitBinaryFloatFnCall(ConstantFP::get(Ty, 1.0), ExpoI,
  1677. TLI, LibFunc_ldexp, LibFunc_ldexpf,
  1678. LibFunc_ldexpl, B, NoAttrs));
  1679. }
  1680. // pow(2.0 ** n, x) -> exp2(n * x)
  1681. if (hasFloatFn(M, TLI, Ty, LibFunc_exp2, LibFunc_exp2f, LibFunc_exp2l)) {
  1682. APFloat BaseR = APFloat(1.0);
  1683. BaseR.convert(BaseF->getSemantics(), APFloat::rmTowardZero, &Ignored);
  1684. BaseR = BaseR / *BaseF;
  1685. bool IsInteger = BaseF->isInteger(), IsReciprocal = BaseR.isInteger();
  1686. const APFloat *NF = IsReciprocal ? &BaseR : BaseF;
  1687. APSInt NI(64, false);
  1688. if ((IsInteger || IsReciprocal) &&
  1689. NF->convertToInteger(NI, APFloat::rmTowardZero, &Ignored) ==
  1690. APFloat::opOK &&
  1691. NI > 1 && NI.isPowerOf2()) {
  1692. double N = NI.logBase2() * (IsReciprocal ? -1.0 : 1.0);
  1693. Value *FMul = B.CreateFMul(Expo, ConstantFP::get(Ty, N), "mul");
  1694. if (Pow->doesNotAccessMemory())
  1695. return copyFlags(*Pow, B.CreateCall(Intrinsic::getDeclaration(
  1696. Mod, Intrinsic::exp2, Ty),
  1697. FMul, "exp2"));
  1698. else
  1699. return copyFlags(*Pow, emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2,
  1700. LibFunc_exp2f,
  1701. LibFunc_exp2l, B, NoAttrs));
  1702. }
  1703. }
  1704. // pow(10.0, x) -> exp10(x)
  1705. // TODO: There is no exp10() intrinsic yet, but some day there shall be one.
  1706. if (match(Base, m_SpecificFP(10.0)) &&
  1707. hasFloatFn(M, TLI, Ty, LibFunc_exp10, LibFunc_exp10f, LibFunc_exp10l))
  1708. return copyFlags(*Pow, emitUnaryFloatFnCall(Expo, TLI, LibFunc_exp10,
  1709. LibFunc_exp10f, LibFunc_exp10l,
  1710. B, NoAttrs));
  1711. // pow(x, y) -> exp2(log2(x) * y)
  1712. if (Pow->hasApproxFunc() && Pow->hasNoNaNs() && BaseF->isFiniteNonZero() &&
  1713. !BaseF->isNegative()) {
  1714. // pow(1, inf) is defined to be 1 but exp2(log2(1) * inf) evaluates to NaN.
  1715. // Luckily optimizePow has already handled the x == 1 case.
  1716. assert(!match(Base, m_FPOne()) &&
  1717. "pow(1.0, y) should have been simplified earlier!");
  1718. Value *Log = nullptr;
  1719. if (Ty->isFloatTy())
  1720. Log = ConstantFP::get(Ty, std::log2(BaseF->convertToFloat()));
  1721. else if (Ty->isDoubleTy())
  1722. Log = ConstantFP::get(Ty, std::log2(BaseF->convertToDouble()));
  1723. if (Log) {
  1724. Value *FMul = B.CreateFMul(Log, Expo, "mul");
  1725. if (Pow->doesNotAccessMemory())
  1726. return copyFlags(*Pow, B.CreateCall(Intrinsic::getDeclaration(
  1727. Mod, Intrinsic::exp2, Ty),
  1728. FMul, "exp2"));
  1729. else if (hasFloatFn(M, TLI, Ty, LibFunc_exp2, LibFunc_exp2f,
  1730. LibFunc_exp2l))
  1731. return copyFlags(*Pow, emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2,
  1732. LibFunc_exp2f,
  1733. LibFunc_exp2l, B, NoAttrs));
  1734. }
  1735. }
  1736. return nullptr;
  1737. }
  1738. static Value *getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno,
  1739. Module *M, IRBuilderBase &B,
  1740. const TargetLibraryInfo *TLI) {
  1741. // If errno is never set, then use the intrinsic for sqrt().
  1742. if (NoErrno) {
  1743. Function *SqrtFn =
  1744. Intrinsic::getDeclaration(M, Intrinsic::sqrt, V->getType());
  1745. return B.CreateCall(SqrtFn, V, "sqrt");
  1746. }
  1747. // Otherwise, use the libcall for sqrt().
  1748. if (hasFloatFn(M, TLI, V->getType(), LibFunc_sqrt, LibFunc_sqrtf,
  1749. LibFunc_sqrtl))
  1750. // TODO: We also should check that the target can in fact lower the sqrt()
  1751. // libcall. We currently have no way to ask this question, so we ask if
  1752. // the target has a sqrt() libcall, which is not exactly the same.
  1753. return emitUnaryFloatFnCall(V, TLI, LibFunc_sqrt, LibFunc_sqrtf,
  1754. LibFunc_sqrtl, B, Attrs);
  1755. return nullptr;
  1756. }
  1757. /// Use square root in place of pow(x, +/-0.5).
  1758. Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) {
  1759. Value *Sqrt, *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
  1760. Module *Mod = Pow->getModule();
  1761. Type *Ty = Pow->getType();
  1762. const APFloat *ExpoF;
  1763. if (!match(Expo, m_APFloat(ExpoF)) ||
  1764. (!ExpoF->isExactlyValue(0.5) && !ExpoF->isExactlyValue(-0.5)))
  1765. return nullptr;
  1766. // Converting pow(X, -0.5) to 1/sqrt(X) may introduce an extra rounding step,
  1767. // so that requires fast-math-flags (afn or reassoc).
  1768. if (ExpoF->isNegative() && (!Pow->hasApproxFunc() && !Pow->hasAllowReassoc()))
  1769. return nullptr;
  1770. // If we have a pow() library call (accesses memory) and we can't guarantee
  1771. // that the base is not an infinity, give up:
  1772. // pow(-Inf, 0.5) is optionally required to have a result of +Inf (not setting
  1773. // errno), but sqrt(-Inf) is required by various standards to set errno.
  1774. if (!Pow->doesNotAccessMemory() && !Pow->hasNoInfs() &&
  1775. !isKnownNeverInfinity(Base, TLI))
  1776. return nullptr;
  1777. Sqrt = getSqrtCall(Base, AttributeList(), Pow->doesNotAccessMemory(), Mod, B,
  1778. TLI);
  1779. if (!Sqrt)
  1780. return nullptr;
  1781. // Handle signed zero base by expanding to fabs(sqrt(x)).
  1782. if (!Pow->hasNoSignedZeros()) {
  1783. Function *FAbsFn = Intrinsic::getDeclaration(Mod, Intrinsic::fabs, Ty);
  1784. Sqrt = B.CreateCall(FAbsFn, Sqrt, "abs");
  1785. }
  1786. Sqrt = copyFlags(*Pow, Sqrt);
  1787. // Handle non finite base by expanding to
  1788. // (x == -infinity ? +infinity : sqrt(x)).
  1789. if (!Pow->hasNoInfs()) {
  1790. Value *PosInf = ConstantFP::getInfinity(Ty),
  1791. *NegInf = ConstantFP::getInfinity(Ty, true);
  1792. Value *FCmp = B.CreateFCmpOEQ(Base, NegInf, "isinf");
  1793. Sqrt = B.CreateSelect(FCmp, PosInf, Sqrt);
  1794. }
  1795. // If the exponent is negative, then get the reciprocal.
  1796. if (ExpoF->isNegative())
  1797. Sqrt = B.CreateFDiv(ConstantFP::get(Ty, 1.0), Sqrt, "reciprocal");
  1798. return Sqrt;
  1799. }
  1800. static Value *createPowWithIntegerExponent(Value *Base, Value *Expo, Module *M,
  1801. IRBuilderBase &B) {
  1802. Value *Args[] = {Base, Expo};
  1803. Type *Types[] = {Base->getType(), Expo->getType()};
  1804. Function *F = Intrinsic::getDeclaration(M, Intrinsic::powi, Types);
  1805. return B.CreateCall(F, Args);
  1806. }
  1807. Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) {
  1808. Value *Base = Pow->getArgOperand(0);
  1809. Value *Expo = Pow->getArgOperand(1);
  1810. Function *Callee = Pow->getCalledFunction();
  1811. StringRef Name = Callee->getName();
  1812. Type *Ty = Pow->getType();
  1813. Module *M = Pow->getModule();
  1814. bool AllowApprox = Pow->hasApproxFunc();
  1815. bool Ignored;
  1816. // Propagate the math semantics from the call to any created instructions.
  1817. IRBuilderBase::FastMathFlagGuard Guard(B);
  1818. B.setFastMathFlags(Pow->getFastMathFlags());
  1819. // Evaluate special cases related to the base.
  1820. // pow(1.0, x) -> 1.0
  1821. if (match(Base, m_FPOne()))
  1822. return Base;
  1823. if (Value *Exp = replacePowWithExp(Pow, B))
  1824. return Exp;
  1825. // Evaluate special cases related to the exponent.
  1826. // pow(x, -1.0) -> 1.0 / x
  1827. if (match(Expo, m_SpecificFP(-1.0)))
  1828. return B.CreateFDiv(ConstantFP::get(Ty, 1.0), Base, "reciprocal");
  1829. // pow(x, +/-0.0) -> 1.0
  1830. if (match(Expo, m_AnyZeroFP()))
  1831. return ConstantFP::get(Ty, 1.0);
  1832. // pow(x, 1.0) -> x
  1833. if (match(Expo, m_FPOne()))
  1834. return Base;
  1835. // pow(x, 2.0) -> x * x
  1836. if (match(Expo, m_SpecificFP(2.0)))
  1837. return B.CreateFMul(Base, Base, "square");
  1838. if (Value *Sqrt = replacePowWithSqrt(Pow, B))
  1839. return Sqrt;
  1840. // If we can approximate pow:
  1841. // pow(x, n) -> powi(x, n) * sqrt(x) if n has exactly a 0.5 fraction
  1842. // pow(x, n) -> powi(x, n) if n is a constant signed integer value
  1843. const APFloat *ExpoF;
  1844. if (AllowApprox && match(Expo, m_APFloat(ExpoF)) &&
  1845. !ExpoF->isExactlyValue(0.5) && !ExpoF->isExactlyValue(-0.5)) {
  1846. APFloat ExpoA(abs(*ExpoF));
  1847. APFloat ExpoI(*ExpoF);
  1848. Value *Sqrt = nullptr;
  1849. if (!ExpoA.isInteger()) {
  1850. APFloat Expo2 = ExpoA;
  1851. // To check if ExpoA is an integer + 0.5, we add it to itself. If there
  1852. // is no floating point exception and the result is an integer, then
  1853. // ExpoA == integer + 0.5
  1854. if (Expo2.add(ExpoA, APFloat::rmNearestTiesToEven) != APFloat::opOK)
  1855. return nullptr;
  1856. if (!Expo2.isInteger())
  1857. return nullptr;
  1858. if (ExpoI.roundToIntegral(APFloat::rmTowardNegative) !=
  1859. APFloat::opInexact)
  1860. return nullptr;
  1861. if (!ExpoI.isInteger())
  1862. return nullptr;
  1863. ExpoF = &ExpoI;
  1864. Sqrt = getSqrtCall(Base, AttributeList(), Pow->doesNotAccessMemory(), M,
  1865. B, TLI);
  1866. if (!Sqrt)
  1867. return nullptr;
  1868. }
  1869. // 0.5 fraction is now optionally handled.
  1870. // Do pow -> powi for remaining integer exponent
  1871. APSInt IntExpo(TLI->getIntSize(), /*isUnsigned=*/false);
  1872. if (ExpoF->isInteger() &&
  1873. ExpoF->convertToInteger(IntExpo, APFloat::rmTowardZero, &Ignored) ==
  1874. APFloat::opOK) {
  1875. Value *PowI = copyFlags(
  1876. *Pow,
  1877. createPowWithIntegerExponent(
  1878. Base, ConstantInt::get(B.getIntNTy(TLI->getIntSize()), IntExpo),
  1879. M, B));
  1880. if (PowI && Sqrt)
  1881. return B.CreateFMul(PowI, Sqrt);
  1882. return PowI;
  1883. }
  1884. }
  1885. // powf(x, itofp(y)) -> powi(x, y)
  1886. if (AllowApprox && (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo))) {
  1887. if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize()))
  1888. return copyFlags(*Pow, createPowWithIntegerExponent(Base, ExpoI, M, B));
  1889. }
  1890. // Shrink pow() to powf() if the arguments are single precision,
  1891. // unless the result is expected to be double precision.
  1892. if (UnsafeFPShrink && Name == TLI->getName(LibFunc_pow) &&
  1893. hasFloatVersion(M, Name)) {
  1894. if (Value *Shrunk = optimizeBinaryDoubleFP(Pow, B, TLI, true))
  1895. return Shrunk;
  1896. }
  1897. return nullptr;
  1898. }
  1899. Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
  1900. Module *M = CI->getModule();
  1901. Function *Callee = CI->getCalledFunction();
  1902. StringRef Name = Callee->getName();
  1903. Value *Ret = nullptr;
  1904. if (UnsafeFPShrink && Name == TLI->getName(LibFunc_exp2) &&
  1905. hasFloatVersion(M, Name))
  1906. Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
  1907. Type *Ty = CI->getType();
  1908. Value *Op = CI->getArgOperand(0);
  1909. // exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= IntSize
  1910. // exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < IntSize
  1911. if ((isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op)) &&
  1912. hasFloatFn(M, TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl)) {
  1913. if (Value *Exp = getIntToFPVal(Op, B, TLI->getIntSize()))
  1914. return emitBinaryFloatFnCall(ConstantFP::get(Ty, 1.0), Exp, TLI,
  1915. LibFunc_ldexp, LibFunc_ldexpf,
  1916. LibFunc_ldexpl, B, AttributeList());
  1917. }
  1918. return Ret;
  1919. }
  1920. Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) {
  1921. Module *M = CI->getModule();
  1922. // If we can shrink the call to a float function rather than a double
  1923. // function, do that first.
  1924. Function *Callee = CI->getCalledFunction();
  1925. StringRef Name = Callee->getName();
  1926. if ((Name == "fmin" || Name == "fmax") && hasFloatVersion(M, Name))
  1927. if (Value *Ret = optimizeBinaryDoubleFP(CI, B, TLI))
  1928. return Ret;
  1929. // The LLVM intrinsics minnum/maxnum correspond to fmin/fmax. Canonicalize to
  1930. // the intrinsics for improved optimization (for example, vectorization).
  1931. // No-signed-zeros is implied by the definitions of fmax/fmin themselves.
  1932. // From the C standard draft WG14/N1256:
  1933. // "Ideally, fmax would be sensitive to the sign of zero, for example
  1934. // fmax(-0.0, +0.0) would return +0; however, implementation in software
  1935. // might be impractical."
  1936. IRBuilderBase::FastMathFlagGuard Guard(B);
  1937. FastMathFlags FMF = CI->getFastMathFlags();
  1938. FMF.setNoSignedZeros();
  1939. B.setFastMathFlags(FMF);
  1940. Intrinsic::ID IID = Callee->getName().startswith("fmin") ? Intrinsic::minnum
  1941. : Intrinsic::maxnum;
  1942. Function *F = Intrinsic::getDeclaration(CI->getModule(), IID, CI->getType());
  1943. return copyFlags(
  1944. *CI, B.CreateCall(F, {CI->getArgOperand(0), CI->getArgOperand(1)}));
  1945. }
  1946. Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
  1947. Function *LogFn = Log->getCalledFunction();
  1948. StringRef LogNm = LogFn->getName();
  1949. Intrinsic::ID LogID = LogFn->getIntrinsicID();
  1950. Module *Mod = Log->getModule();
  1951. Type *Ty = Log->getType();
  1952. Value *Ret = nullptr;
  1953. if (UnsafeFPShrink && hasFloatVersion(Mod, LogNm))
  1954. Ret = optimizeUnaryDoubleFP(Log, B, TLI, true);
  1955. // The earlier call must also be 'fast' in order to do these transforms.
  1956. CallInst *Arg = dyn_cast<CallInst>(Log->getArgOperand(0));
  1957. if (!Log->isFast() || !Arg || !Arg->isFast() || !Arg->hasOneUse())
  1958. return Ret;
  1959. LibFunc LogLb, ExpLb, Exp2Lb, Exp10Lb, PowLb;
  1960. // This is only applicable to log(), log2(), log10().
  1961. if (TLI->getLibFunc(LogNm, LogLb))
  1962. switch (LogLb) {
  1963. case LibFunc_logf:
  1964. LogID = Intrinsic::log;
  1965. ExpLb = LibFunc_expf;
  1966. Exp2Lb = LibFunc_exp2f;
  1967. Exp10Lb = LibFunc_exp10f;
  1968. PowLb = LibFunc_powf;
  1969. break;
  1970. case LibFunc_log:
  1971. LogID = Intrinsic::log;
  1972. ExpLb = LibFunc_exp;
  1973. Exp2Lb = LibFunc_exp2;
  1974. Exp10Lb = LibFunc_exp10;
  1975. PowLb = LibFunc_pow;
  1976. break;
  1977. case LibFunc_logl:
  1978. LogID = Intrinsic::log;
  1979. ExpLb = LibFunc_expl;
  1980. Exp2Lb = LibFunc_exp2l;
  1981. Exp10Lb = LibFunc_exp10l;
  1982. PowLb = LibFunc_powl;
  1983. break;
  1984. case LibFunc_log2f:
  1985. LogID = Intrinsic::log2;
  1986. ExpLb = LibFunc_expf;
  1987. Exp2Lb = LibFunc_exp2f;
  1988. Exp10Lb = LibFunc_exp10f;
  1989. PowLb = LibFunc_powf;
  1990. break;
  1991. case LibFunc_log2:
  1992. LogID = Intrinsic::log2;
  1993. ExpLb = LibFunc_exp;
  1994. Exp2Lb = LibFunc_exp2;
  1995. Exp10Lb = LibFunc_exp10;
  1996. PowLb = LibFunc_pow;
  1997. break;
  1998. case LibFunc_log2l:
  1999. LogID = Intrinsic::log2;
  2000. ExpLb = LibFunc_expl;
  2001. Exp2Lb = LibFunc_exp2l;
  2002. Exp10Lb = LibFunc_exp10l;
  2003. PowLb = LibFunc_powl;
  2004. break;
  2005. case LibFunc_log10f:
  2006. LogID = Intrinsic::log10;
  2007. ExpLb = LibFunc_expf;
  2008. Exp2Lb = LibFunc_exp2f;
  2009. Exp10Lb = LibFunc_exp10f;
  2010. PowLb = LibFunc_powf;
  2011. break;
  2012. case LibFunc_log10:
  2013. LogID = Intrinsic::log10;
  2014. ExpLb = LibFunc_exp;
  2015. Exp2Lb = LibFunc_exp2;
  2016. Exp10Lb = LibFunc_exp10;
  2017. PowLb = LibFunc_pow;
  2018. break;
  2019. case LibFunc_log10l:
  2020. LogID = Intrinsic::log10;
  2021. ExpLb = LibFunc_expl;
  2022. Exp2Lb = LibFunc_exp2l;
  2023. Exp10Lb = LibFunc_exp10l;
  2024. PowLb = LibFunc_powl;
  2025. break;
  2026. default:
  2027. return Ret;
  2028. }
  2029. else if (LogID == Intrinsic::log || LogID == Intrinsic::log2 ||
  2030. LogID == Intrinsic::log10) {
  2031. if (Ty->getScalarType()->isFloatTy()) {
  2032. ExpLb = LibFunc_expf;
  2033. Exp2Lb = LibFunc_exp2f;
  2034. Exp10Lb = LibFunc_exp10f;
  2035. PowLb = LibFunc_powf;
  2036. } else if (Ty->getScalarType()->isDoubleTy()) {
  2037. ExpLb = LibFunc_exp;
  2038. Exp2Lb = LibFunc_exp2;
  2039. Exp10Lb = LibFunc_exp10;
  2040. PowLb = LibFunc_pow;
  2041. } else
  2042. return Ret;
  2043. } else
  2044. return Ret;
  2045. IRBuilderBase::FastMathFlagGuard Guard(B);
  2046. B.setFastMathFlags(FastMathFlags::getFast());
  2047. Intrinsic::ID ArgID = Arg->getIntrinsicID();
  2048. LibFunc ArgLb = NotLibFunc;
  2049. TLI->getLibFunc(*Arg, ArgLb);
  2050. // log(pow(x,y)) -> y*log(x)
  2051. AttributeList NoAttrs;
  2052. if (ArgLb == PowLb || ArgID == Intrinsic::pow) {
  2053. Value *LogX =
  2054. Log->doesNotAccessMemory()
  2055. ? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty),
  2056. Arg->getOperand(0), "log")
  2057. : emitUnaryFloatFnCall(Arg->getOperand(0), TLI, LogNm, B, NoAttrs);
  2058. Value *MulY = B.CreateFMul(Arg->getArgOperand(1), LogX, "mul");
  2059. // Since pow() may have side effects, e.g. errno,
  2060. // dead code elimination may not be trusted to remove it.
  2061. substituteInParent(Arg, MulY);
  2062. return MulY;
  2063. }
  2064. // log(exp{,2,10}(y)) -> y*log({e,2,10})
  2065. // TODO: There is no exp10() intrinsic yet.
  2066. if (ArgLb == ExpLb || ArgLb == Exp2Lb || ArgLb == Exp10Lb ||
  2067. ArgID == Intrinsic::exp || ArgID == Intrinsic::exp2) {
  2068. Constant *Eul;
  2069. if (ArgLb == ExpLb || ArgID == Intrinsic::exp)
  2070. // FIXME: Add more precise value of e for long double.
  2071. Eul = ConstantFP::get(Log->getType(), numbers::e);
  2072. else if (ArgLb == Exp2Lb || ArgID == Intrinsic::exp2)
  2073. Eul = ConstantFP::get(Log->getType(), 2.0);
  2074. else
  2075. Eul = ConstantFP::get(Log->getType(), 10.0);
  2076. Value *LogE = Log->doesNotAccessMemory()
  2077. ? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty),
  2078. Eul, "log")
  2079. : emitUnaryFloatFnCall(Eul, TLI, LogNm, B, NoAttrs);
  2080. Value *MulY = B.CreateFMul(Arg->getArgOperand(0), LogE, "mul");
  2081. // Since exp() may have side effects, e.g. errno,
  2082. // dead code elimination may not be trusted to remove it.
  2083. substituteInParent(Arg, MulY);
  2084. return MulY;
  2085. }
  2086. return Ret;
  2087. }
  2088. Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
  2089. Module *M = CI->getModule();
  2090. Function *Callee = CI->getCalledFunction();
  2091. Value *Ret = nullptr;
  2092. // TODO: Once we have a way (other than checking for the existince of the
  2093. // libcall) to tell whether our target can lower @llvm.sqrt, relax the
  2094. // condition below.
  2095. if (isLibFuncEmittable(M, TLI, LibFunc_sqrtf) &&
  2096. (Callee->getName() == "sqrt" ||
  2097. Callee->getIntrinsicID() == Intrinsic::sqrt))
  2098. Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
  2099. if (!CI->isFast())
  2100. return Ret;
  2101. Instruction *I = dyn_cast<Instruction>(CI->getArgOperand(0));
  2102. if (!I || I->getOpcode() != Instruction::FMul || !I->isFast())
  2103. return Ret;
  2104. // We're looking for a repeated factor in a multiplication tree,
  2105. // so we can do this fold: sqrt(x * x) -> fabs(x);
  2106. // or this fold: sqrt((x * x) * y) -> fabs(x) * sqrt(y).
  2107. Value *Op0 = I->getOperand(0);
  2108. Value *Op1 = I->getOperand(1);
  2109. Value *RepeatOp = nullptr;
  2110. Value *OtherOp = nullptr;
  2111. if (Op0 == Op1) {
  2112. // Simple match: the operands of the multiply are identical.
  2113. RepeatOp = Op0;
  2114. } else {
  2115. // Look for a more complicated pattern: one of the operands is itself
  2116. // a multiply, so search for a common factor in that multiply.
  2117. // Note: We don't bother looking any deeper than this first level or for
  2118. // variations of this pattern because instcombine's visitFMUL and/or the
  2119. // reassociation pass should give us this form.
  2120. Value *OtherMul0, *OtherMul1;
  2121. if (match(Op0, m_FMul(m_Value(OtherMul0), m_Value(OtherMul1)))) {
  2122. // Pattern: sqrt((x * y) * z)
  2123. if (OtherMul0 == OtherMul1 && cast<Instruction>(Op0)->isFast()) {
  2124. // Matched: sqrt((x * x) * z)
  2125. RepeatOp = OtherMul0;
  2126. OtherOp = Op1;
  2127. }
  2128. }
  2129. }
  2130. if (!RepeatOp)
  2131. return Ret;
  2132. // Fast math flags for any created instructions should match the sqrt
  2133. // and multiply.
  2134. IRBuilderBase::FastMathFlagGuard Guard(B);
  2135. B.setFastMathFlags(I->getFastMathFlags());
  2136. // If we found a repeated factor, hoist it out of the square root and
  2137. // replace it with the fabs of that factor.
  2138. Type *ArgType = I->getType();
  2139. Function *Fabs = Intrinsic::getDeclaration(M, Intrinsic::fabs, ArgType);
  2140. Value *FabsCall = B.CreateCall(Fabs, RepeatOp, "fabs");
  2141. if (OtherOp) {
  2142. // If we found a non-repeated factor, we still need to get its square
  2143. // root. We then multiply that by the value that was simplified out
  2144. // of the square root calculation.
  2145. Function *Sqrt = Intrinsic::getDeclaration(M, Intrinsic::sqrt, ArgType);
  2146. Value *SqrtCall = B.CreateCall(Sqrt, OtherOp, "sqrt");
  2147. return copyFlags(*CI, B.CreateFMul(FabsCall, SqrtCall));
  2148. }
  2149. return copyFlags(*CI, FabsCall);
  2150. }
  2151. // TODO: Generalize to handle any trig function and its inverse.
  2152. Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilderBase &B) {
  2153. Module *M = CI->getModule();
  2154. Function *Callee = CI->getCalledFunction();
  2155. Value *Ret = nullptr;
  2156. StringRef Name = Callee->getName();
  2157. if (UnsafeFPShrink && Name == "tan" && hasFloatVersion(M, Name))
  2158. Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
  2159. Value *Op1 = CI->getArgOperand(0);
  2160. auto *OpC = dyn_cast<CallInst>(Op1);
  2161. if (!OpC)
  2162. return Ret;
  2163. // Both calls must be 'fast' in order to remove them.
  2164. if (!CI->isFast() || !OpC->isFast())
  2165. return Ret;
  2166. // tan(atan(x)) -> x
  2167. // tanf(atanf(x)) -> x
  2168. // tanl(atanl(x)) -> x
  2169. LibFunc Func;
  2170. Function *F = OpC->getCalledFunction();
  2171. if (F && TLI->getLibFunc(F->getName(), Func) &&
  2172. isLibFuncEmittable(M, TLI, Func) &&
  2173. ((Func == LibFunc_atan && Callee->getName() == "tan") ||
  2174. (Func == LibFunc_atanf && Callee->getName() == "tanf") ||
  2175. (Func == LibFunc_atanl && Callee->getName() == "tanl")))
  2176. Ret = OpC->getArgOperand(0);
  2177. return Ret;
  2178. }
  2179. static bool isTrigLibCall(CallInst *CI) {
  2180. // We can only hope to do anything useful if we can ignore things like errno
  2181. // and floating-point exceptions.
  2182. // We already checked the prototype.
  2183. return CI->doesNotThrow() && CI->doesNotAccessMemory();
  2184. }
  2185. static bool insertSinCosCall(IRBuilderBase &B, Function *OrigCallee, Value *Arg,
  2186. bool UseFloat, Value *&Sin, Value *&Cos,
  2187. Value *&SinCos, const TargetLibraryInfo *TLI) {
  2188. Module *M = OrigCallee->getParent();
  2189. Type *ArgTy = Arg->getType();
  2190. Type *ResTy;
  2191. StringRef Name;
  2192. Triple T(OrigCallee->getParent()->getTargetTriple());
  2193. if (UseFloat) {
  2194. Name = "__sincospif_stret";
  2195. assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");
  2196. // x86_64 can't use {float, float} since that would be returned in both
  2197. // xmm0 and xmm1, which isn't what a real struct would do.
  2198. ResTy = T.getArch() == Triple::x86_64
  2199. ? static_cast<Type *>(FixedVectorType::get(ArgTy, 2))
  2200. : static_cast<Type *>(StructType::get(ArgTy, ArgTy));
  2201. } else {
  2202. Name = "__sincospi_stret";
  2203. ResTy = StructType::get(ArgTy, ArgTy);
  2204. }
  2205. if (!isLibFuncEmittable(M, TLI, Name))
  2206. return false;
  2207. LibFunc TheLibFunc;
  2208. TLI->getLibFunc(Name, TheLibFunc);
  2209. FunctionCallee Callee = getOrInsertLibFunc(
  2210. M, *TLI, TheLibFunc, OrigCallee->getAttributes(), ResTy, ArgTy);
  2211. if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
  2212. // If the argument is an instruction, it must dominate all uses so put our
  2213. // sincos call there.
  2214. B.SetInsertPoint(ArgInst->getParent(), ++ArgInst->getIterator());
  2215. } else {
  2216. // Otherwise (e.g. for a constant) the beginning of the function is as
  2217. // good a place as any.
  2218. BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
  2219. B.SetInsertPoint(&EntryBB, EntryBB.begin());
  2220. }
  2221. SinCos = B.CreateCall(Callee, Arg, "sincospi");
  2222. if (SinCos->getType()->isStructTy()) {
  2223. Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
  2224. Cos = B.CreateExtractValue(SinCos, 1, "cospi");
  2225. } else {
  2226. Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
  2227. "sinpi");
  2228. Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
  2229. "cospi");
  2230. }
  2231. return true;
  2232. }
  2233. Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, IRBuilderBase &B) {
  2234. // Make sure the prototype is as expected, otherwise the rest of the
  2235. // function is probably invalid and likely to abort.
  2236. if (!isTrigLibCall(CI))
  2237. return nullptr;
  2238. Value *Arg = CI->getArgOperand(0);
  2239. SmallVector<CallInst *, 1> SinCalls;
  2240. SmallVector<CallInst *, 1> CosCalls;
  2241. SmallVector<CallInst *, 1> SinCosCalls;
  2242. bool IsFloat = Arg->getType()->isFloatTy();
  2243. // Look for all compatible sinpi, cospi and sincospi calls with the same
  2244. // argument. If there are enough (in some sense) we can make the
  2245. // substitution.
  2246. Function *F = CI->getFunction();
  2247. for (User *U : Arg->users())
  2248. classifyArgUse(U, F, IsFloat, SinCalls, CosCalls, SinCosCalls);
  2249. // It's only worthwhile if both sinpi and cospi are actually used.
  2250. if (SinCalls.empty() || CosCalls.empty())
  2251. return nullptr;
  2252. Value *Sin, *Cos, *SinCos;
  2253. if (!insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos,
  2254. SinCos, TLI))
  2255. return nullptr;
  2256. auto replaceTrigInsts = [this](SmallVectorImpl<CallInst *> &Calls,
  2257. Value *Res) {
  2258. for (CallInst *C : Calls)
  2259. replaceAllUsesWith(C, Res);
  2260. };
  2261. replaceTrigInsts(SinCalls, Sin);
  2262. replaceTrigInsts(CosCalls, Cos);
  2263. replaceTrigInsts(SinCosCalls, SinCos);
  2264. return nullptr;
  2265. }
  2266. void LibCallSimplifier::classifyArgUse(
  2267. Value *Val, Function *F, bool IsFloat,
  2268. SmallVectorImpl<CallInst *> &SinCalls,
  2269. SmallVectorImpl<CallInst *> &CosCalls,
  2270. SmallVectorImpl<CallInst *> &SinCosCalls) {
  2271. auto *CI = dyn_cast<CallInst>(Val);
  2272. if (!CI || CI->use_empty())
  2273. return;
  2274. // Don't consider calls in other functions.
  2275. if (CI->getFunction() != F)
  2276. return;
  2277. Module *M = CI->getModule();
  2278. Function *Callee = CI->getCalledFunction();
  2279. LibFunc Func;
  2280. if (!Callee || !TLI->getLibFunc(*Callee, Func) ||
  2281. !isLibFuncEmittable(M, TLI, Func) ||
  2282. !isTrigLibCall(CI))
  2283. return;
  2284. if (IsFloat) {
  2285. if (Func == LibFunc_sinpif)
  2286. SinCalls.push_back(CI);
  2287. else if (Func == LibFunc_cospif)
  2288. CosCalls.push_back(CI);
  2289. else if (Func == LibFunc_sincospif_stret)
  2290. SinCosCalls.push_back(CI);
  2291. } else {
  2292. if (Func == LibFunc_sinpi)
  2293. SinCalls.push_back(CI);
  2294. else if (Func == LibFunc_cospi)
  2295. CosCalls.push_back(CI);
  2296. else if (Func == LibFunc_sincospi_stret)
  2297. SinCosCalls.push_back(CI);
  2298. }
  2299. }
  2300. //===----------------------------------------------------------------------===//
  2301. // Integer Library Call Optimizations
  2302. //===----------------------------------------------------------------------===//
  2303. Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilderBase &B) {
  2304. // All variants of ffs return int which need not be 32 bits wide.
  2305. // ffs{,l,ll}(x) -> x != 0 ? (int)llvm.cttz(x)+1 : 0
  2306. Type *RetType = CI->getType();
  2307. Value *Op = CI->getArgOperand(0);
  2308. Type *ArgType = Op->getType();
  2309. Function *F = Intrinsic::getDeclaration(CI->getCalledFunction()->getParent(),
  2310. Intrinsic::cttz, ArgType);
  2311. Value *V = B.CreateCall(F, {Op, B.getTrue()}, "cttz");
  2312. V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
  2313. V = B.CreateIntCast(V, RetType, false);
  2314. Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
  2315. return B.CreateSelect(Cond, V, ConstantInt::get(RetType, 0));
  2316. }
  2317. Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilderBase &B) {
  2318. // All variants of fls return int which need not be 32 bits wide.
  2319. // fls{,l,ll}(x) -> (int)(sizeInBits(x) - llvm.ctlz(x, false))
  2320. Value *Op = CI->getArgOperand(0);
  2321. Type *ArgType = Op->getType();
  2322. Function *F = Intrinsic::getDeclaration(CI->getCalledFunction()->getParent(),
  2323. Intrinsic::ctlz, ArgType);
  2324. Value *V = B.CreateCall(F, {Op, B.getFalse()}, "ctlz");
  2325. V = B.CreateSub(ConstantInt::get(V->getType(), ArgType->getIntegerBitWidth()),
  2326. V);
  2327. return B.CreateIntCast(V, CI->getType(), false);
  2328. }
  2329. Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilderBase &B) {
  2330. // abs(x) -> x <s 0 ? -x : x
  2331. // The negation has 'nsw' because abs of INT_MIN is undefined.
  2332. Value *X = CI->getArgOperand(0);
  2333. Value *IsNeg = B.CreateIsNeg(X);
  2334. Value *NegX = B.CreateNSWNeg(X, "neg");
  2335. return B.CreateSelect(IsNeg, NegX, X);
  2336. }
  2337. Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilderBase &B) {
  2338. // isdigit(c) -> (c-'0') <u 10
  2339. Value *Op = CI->getArgOperand(0);
  2340. Type *ArgType = Op->getType();
  2341. Op = B.CreateSub(Op, ConstantInt::get(ArgType, '0'), "isdigittmp");
  2342. Op = B.CreateICmpULT(Op, ConstantInt::get(ArgType, 10), "isdigit");
  2343. return B.CreateZExt(Op, CI->getType());
  2344. }
  2345. Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilderBase &B) {
  2346. // isascii(c) -> c <u 128
  2347. Value *Op = CI->getArgOperand(0);
  2348. Type *ArgType = Op->getType();
  2349. Op = B.CreateICmpULT(Op, ConstantInt::get(ArgType, 128), "isascii");
  2350. return B.CreateZExt(Op, CI->getType());
  2351. }
  2352. Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilderBase &B) {
  2353. // toascii(c) -> c & 0x7f
  2354. return B.CreateAnd(CI->getArgOperand(0),
  2355. ConstantInt::get(CI->getType(), 0x7F));
  2356. }
  2357. // Fold calls to atoi, atol, and atoll.
  2358. Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilderBase &B) {
  2359. CI->addParamAttr(0, Attribute::NoCapture);
  2360. StringRef Str;
  2361. if (!getConstantStringInfo(CI->getArgOperand(0), Str))
  2362. return nullptr;
  2363. return convertStrToInt(CI, Str, nullptr, 10, /*AsSigned=*/true, B);
  2364. }
  2365. // Fold calls to strtol, strtoll, strtoul, and strtoull.
  2366. Value *LibCallSimplifier::optimizeStrToInt(CallInst *CI, IRBuilderBase &B,
  2367. bool AsSigned) {
  2368. Value *EndPtr = CI->getArgOperand(1);
  2369. if (isa<ConstantPointerNull>(EndPtr)) {
  2370. // With a null EndPtr, this function won't capture the main argument.
  2371. // It would be readonly too, except that it still may write to errno.
  2372. CI->addParamAttr(0, Attribute::NoCapture);
  2373. EndPtr = nullptr;
  2374. } else if (!isKnownNonZero(EndPtr, DL))
  2375. return nullptr;
  2376. StringRef Str;
  2377. if (!getConstantStringInfo(CI->getArgOperand(0), Str))
  2378. return nullptr;
  2379. if (ConstantInt *CInt = dyn_cast<ConstantInt>(CI->getArgOperand(2))) {
  2380. return convertStrToInt(CI, Str, EndPtr, CInt->getSExtValue(), AsSigned, B);
  2381. }
  2382. return nullptr;
  2383. }
  2384. //===----------------------------------------------------------------------===//
  2385. // Formatting and IO Library Call Optimizations
  2386. //===----------------------------------------------------------------------===//
  2387. static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
  2388. Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilderBase &B,
  2389. int StreamArg) {
  2390. Function *Callee = CI->getCalledFunction();
  2391. // Error reporting calls should be cold, mark them as such.
  2392. // This applies even to non-builtin calls: it is only a hint and applies to
  2393. // functions that the frontend might not understand as builtins.
  2394. // This heuristic was suggested in:
  2395. // Improving Static Branch Prediction in a Compiler
  2396. // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu
  2397. // Proceedings of PACT'98, Oct. 1998, IEEE
  2398. if (!CI->hasFnAttr(Attribute::Cold) &&
  2399. isReportingError(Callee, CI, StreamArg)) {
  2400. CI->addFnAttr(Attribute::Cold);
  2401. }
  2402. return nullptr;
  2403. }
  2404. static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {
  2405. if (!Callee || !Callee->isDeclaration())
  2406. return false;
  2407. if (StreamArg < 0)
  2408. return true;
  2409. // These functions might be considered cold, but only if their stream
  2410. // argument is stderr.
  2411. if (StreamArg >= (int)CI->arg_size())
  2412. return false;
  2413. LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
  2414. if (!LI)
  2415. return false;
  2416. GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
  2417. if (!GV || !GV->isDeclaration())
  2418. return false;
  2419. return GV->getName() == "stderr";
  2420. }
  2421. Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
  2422. // Check for a fixed format string.
  2423. StringRef FormatStr;
  2424. if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
  2425. return nullptr;
  2426. // Empty format string -> noop.
  2427. if (FormatStr.empty()) // Tolerate printf's declared void.
  2428. return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);
  2429. // Do not do any of the following transformations if the printf return value
  2430. // is used, in general the printf return value is not compatible with either
  2431. // putchar() or puts().
  2432. if (!CI->use_empty())
  2433. return nullptr;
  2434. Type *IntTy = CI->getType();
  2435. // printf("x") -> putchar('x'), even for "%" and "%%".
  2436. if (FormatStr.size() == 1 || FormatStr == "%%") {
  2437. // Convert the character to unsigned char before passing it to putchar
  2438. // to avoid host-specific sign extension in the IR. Putchar converts
  2439. // it to unsigned char regardless.
  2440. Value *IntChar = ConstantInt::get(IntTy, (unsigned char)FormatStr[0]);
  2441. return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
  2442. }
  2443. // Try to remove call or emit putchar/puts.
  2444. if (FormatStr == "%s" && CI->arg_size() > 1) {
  2445. StringRef OperandStr;
  2446. if (!getConstantStringInfo(CI->getOperand(1), OperandStr))
  2447. return nullptr;
  2448. // printf("%s", "") --> NOP
  2449. if (OperandStr.empty())
  2450. return (Value *)CI;
  2451. // printf("%s", "a") --> putchar('a')
  2452. if (OperandStr.size() == 1) {
  2453. // Convert the character to unsigned char before passing it to putchar
  2454. // to avoid host-specific sign extension in the IR. Putchar converts
  2455. // it to unsigned char regardless.
  2456. Value *IntChar = ConstantInt::get(IntTy, (unsigned char)OperandStr[0]);
  2457. return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
  2458. }
  2459. // printf("%s", str"\n") --> puts(str)
  2460. if (OperandStr.back() == '\n') {
  2461. OperandStr = OperandStr.drop_back();
  2462. Value *GV = B.CreateGlobalString(OperandStr, "str");
  2463. return copyFlags(*CI, emitPutS(GV, B, TLI));
  2464. }
  2465. return nullptr;
  2466. }
  2467. // printf("foo\n") --> puts("foo")
  2468. if (FormatStr.back() == '\n' &&
  2469. !FormatStr.contains('%')) { // No format characters.
  2470. // Create a string literal with no \n on it. We expect the constant merge
  2471. // pass to be run after this pass, to merge duplicate strings.
  2472. FormatStr = FormatStr.drop_back();
  2473. Value *GV = B.CreateGlobalString(FormatStr, "str");
  2474. return copyFlags(*CI, emitPutS(GV, B, TLI));
  2475. }
  2476. // Optimize specific format strings.
  2477. // printf("%c", chr) --> putchar(chr)
  2478. if (FormatStr == "%c" && CI->arg_size() > 1 &&
  2479. CI->getArgOperand(1)->getType()->isIntegerTy()) {
  2480. // Convert the argument to the type expected by putchar, i.e., int, which
  2481. // need not be 32 bits wide but which is the same as printf's return type.
  2482. Value *IntChar = B.CreateIntCast(CI->getArgOperand(1), IntTy, false);
  2483. return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
  2484. }
  2485. // printf("%s\n", str) --> puts(str)
  2486. if (FormatStr == "%s\n" && CI->arg_size() > 1 &&
  2487. CI->getArgOperand(1)->getType()->isPointerTy())
  2488. return copyFlags(*CI, emitPutS(CI->getArgOperand(1), B, TLI));
  2489. return nullptr;
  2490. }
  2491. Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilderBase &B) {
  2492. Module *M = CI->getModule();
  2493. Function *Callee = CI->getCalledFunction();
  2494. FunctionType *FT = Callee->getFunctionType();
  2495. if (Value *V = optimizePrintFString(CI, B)) {
  2496. return V;
  2497. }
  2498. annotateNonNullNoUndefBasedOnAccess(CI, 0);
  2499. // printf(format, ...) -> iprintf(format, ...) if no floating point
  2500. // arguments.
  2501. if (isLibFuncEmittable(M, TLI, LibFunc_iprintf) &&
  2502. !callHasFloatingPointArgument(CI)) {
  2503. FunctionCallee IPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_iprintf, FT,
  2504. Callee->getAttributes());
  2505. CallInst *New = cast<CallInst>(CI->clone());
  2506. New->setCalledFunction(IPrintFFn);
  2507. B.Insert(New);
  2508. return New;
  2509. }
  2510. // printf(format, ...) -> __small_printf(format, ...) if no 128-bit floating point
  2511. // arguments.
  2512. if (isLibFuncEmittable(M, TLI, LibFunc_small_printf) &&
  2513. !callHasFP128Argument(CI)) {
  2514. auto SmallPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_small_printf, FT,
  2515. Callee->getAttributes());
  2516. CallInst *New = cast<CallInst>(CI->clone());
  2517. New->setCalledFunction(SmallPrintFFn);
  2518. B.Insert(New);
  2519. return New;
  2520. }
  2521. return nullptr;
  2522. }
  2523. Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI,
  2524. IRBuilderBase &B) {
  2525. // Check for a fixed format string.
  2526. StringRef FormatStr;
  2527. if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
  2528. return nullptr;
  2529. // If we just have a format string (nothing else crazy) transform it.
  2530. Value *Dest = CI->getArgOperand(0);
  2531. if (CI->arg_size() == 2) {
  2532. // Make sure there's no % in the constant array. We could try to handle
  2533. // %% -> % in the future if we cared.
  2534. if (FormatStr.contains('%'))
  2535. return nullptr; // we found a format specifier, bail out.
  2536. // sprintf(str, fmt) -> llvm.memcpy(align 1 str, align 1 fmt, strlen(fmt)+1)
  2537. B.CreateMemCpy(
  2538. Dest, Align(1), CI->getArgOperand(1), Align(1),
  2539. ConstantInt::get(DL.getIntPtrType(CI->getContext()),
  2540. FormatStr.size() + 1)); // Copy the null byte.
  2541. return ConstantInt::get(CI->getType(), FormatStr.size());
  2542. }
  2543. // The remaining optimizations require the format string to be "%s" or "%c"
  2544. // and have an extra operand.
  2545. if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
  2546. return nullptr;
  2547. // Decode the second character of the format string.
  2548. if (FormatStr[1] == 'c') {
  2549. // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
  2550. if (!CI->getArgOperand(2)->getType()->isIntegerTy())
  2551. return nullptr;
  2552. Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
  2553. Value *Ptr = castToCStr(Dest, B);
  2554. B.CreateStore(V, Ptr);
  2555. Ptr = B.CreateInBoundsGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
  2556. B.CreateStore(B.getInt8(0), Ptr);
  2557. return ConstantInt::get(CI->getType(), 1);
  2558. }
  2559. if (FormatStr[1] == 's') {
  2560. // sprintf(dest, "%s", str) -> llvm.memcpy(align 1 dest, align 1 str,
  2561. // strlen(str)+1)
  2562. if (!CI->getArgOperand(2)->getType()->isPointerTy())
  2563. return nullptr;
  2564. if (CI->use_empty())
  2565. // sprintf(dest, "%s", str) -> strcpy(dest, str)
  2566. return copyFlags(*CI, emitStrCpy(Dest, CI->getArgOperand(2), B, TLI));
  2567. uint64_t SrcLen = GetStringLength(CI->getArgOperand(2));
  2568. if (SrcLen) {
  2569. B.CreateMemCpy(
  2570. Dest, Align(1), CI->getArgOperand(2), Align(1),
  2571. ConstantInt::get(DL.getIntPtrType(CI->getContext()), SrcLen));
  2572. // Returns total number of characters written without null-character.
  2573. return ConstantInt::get(CI->getType(), SrcLen - 1);
  2574. } else if (Value *V = emitStpCpy(Dest, CI->getArgOperand(2), B, TLI)) {
  2575. // sprintf(dest, "%s", str) -> stpcpy(dest, str) - dest
  2576. // Handle mismatched pointer types (goes away with typeless pointers?).
  2577. V = B.CreatePointerCast(V, B.getInt8PtrTy());
  2578. Dest = B.CreatePointerCast(Dest, B.getInt8PtrTy());
  2579. Value *PtrDiff = B.CreatePtrDiff(B.getInt8Ty(), V, Dest);
  2580. return B.CreateIntCast(PtrDiff, CI->getType(), false);
  2581. }
  2582. bool OptForSize = CI->getFunction()->hasOptSize() ||
  2583. llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
  2584. PGSOQueryType::IRPass);
  2585. if (OptForSize)
  2586. return nullptr;
  2587. Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
  2588. if (!Len)
  2589. return nullptr;
  2590. Value *IncLen =
  2591. B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
  2592. B.CreateMemCpy(Dest, Align(1), CI->getArgOperand(2), Align(1), IncLen);
  2593. // The sprintf result is the unincremented number of bytes in the string.
  2594. return B.CreateIntCast(Len, CI->getType(), false);
  2595. }
  2596. return nullptr;
  2597. }
  2598. Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilderBase &B) {
  2599. Module *M = CI->getModule();
  2600. Function *Callee = CI->getCalledFunction();
  2601. FunctionType *FT = Callee->getFunctionType();
  2602. if (Value *V = optimizeSPrintFString(CI, B)) {
  2603. return V;
  2604. }
  2605. annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
  2606. // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
  2607. // point arguments.
  2608. if (isLibFuncEmittable(M, TLI, LibFunc_siprintf) &&
  2609. !callHasFloatingPointArgument(CI)) {
  2610. FunctionCallee SIPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_siprintf,
  2611. FT, Callee->getAttributes());
  2612. CallInst *New = cast<CallInst>(CI->clone());
  2613. New->setCalledFunction(SIPrintFFn);
  2614. B.Insert(New);
  2615. return New;
  2616. }
  2617. // sprintf(str, format, ...) -> __small_sprintf(str, format, ...) if no 128-bit
  2618. // floating point arguments.
  2619. if (isLibFuncEmittable(M, TLI, LibFunc_small_sprintf) &&
  2620. !callHasFP128Argument(CI)) {
  2621. auto SmallSPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_small_sprintf, FT,
  2622. Callee->getAttributes());
  2623. CallInst *New = cast<CallInst>(CI->clone());
  2624. New->setCalledFunction(SmallSPrintFFn);
  2625. B.Insert(New);
  2626. return New;
  2627. }
  2628. return nullptr;
  2629. }
  2630. // Transform an snprintf call CI with the bound N to format the string Str
  2631. // either to a call to memcpy, or to single character a store, or to nothing,
  2632. // and fold the result to a constant. A nonnull StrArg refers to the string
  2633. // argument being formatted. Otherwise the call is one with N < 2 and
  2634. // the "%c" directive to format a single character.
  2635. Value *LibCallSimplifier::emitSnPrintfMemCpy(CallInst *CI, Value *StrArg,
  2636. StringRef Str, uint64_t N,
  2637. IRBuilderBase &B) {
  2638. assert(StrArg || (N < 2 && Str.size() == 1));
  2639. unsigned IntBits = TLI->getIntSize();
  2640. uint64_t IntMax = maxIntN(IntBits);
  2641. if (Str.size() > IntMax)
  2642. // Bail if the string is longer than INT_MAX. POSIX requires
  2643. // implementations to set errno to EOVERFLOW in this case, in
  2644. // addition to when N is larger than that (checked by the caller).
  2645. return nullptr;
  2646. Value *StrLen = ConstantInt::get(CI->getType(), Str.size());
  2647. if (N == 0)
  2648. return StrLen;
  2649. // Set to the number of bytes to copy fron StrArg which is also
  2650. // the offset of the terinating nul.
  2651. uint64_t NCopy;
  2652. if (N > Str.size())
  2653. // Copy the full string, including the terminating nul (which must
  2654. // be present regardless of the bound).
  2655. NCopy = Str.size() + 1;
  2656. else
  2657. NCopy = N - 1;
  2658. Value *DstArg = CI->getArgOperand(0);
  2659. if (NCopy && StrArg)
  2660. // Transform the call to lvm.memcpy(dst, fmt, N).
  2661. copyFlags(
  2662. *CI,
  2663. B.CreateMemCpy(
  2664. DstArg, Align(1), StrArg, Align(1),
  2665. ConstantInt::get(DL.getIntPtrType(CI->getContext()), NCopy)));
  2666. if (N > Str.size())
  2667. // Return early when the whole format string, including the final nul,
  2668. // has been copied.
  2669. return StrLen;
  2670. // Otherwise, when truncating the string append a terminating nul.
  2671. Type *Int8Ty = B.getInt8Ty();
  2672. Value *NulOff = B.getIntN(IntBits, NCopy);
  2673. Value *DstEnd = B.CreateInBoundsGEP(Int8Ty, DstArg, NulOff, "endptr");
  2674. B.CreateStore(ConstantInt::get(Int8Ty, 0), DstEnd);
  2675. return StrLen;
  2676. }
  2677. Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI,
  2678. IRBuilderBase &B) {
  2679. // Check for size
  2680. ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  2681. if (!Size)
  2682. return nullptr;
  2683. uint64_t N = Size->getZExtValue();
  2684. uint64_t IntMax = maxIntN(TLI->getIntSize());
  2685. if (N > IntMax)
  2686. // Bail if the bound exceeds INT_MAX. POSIX requires implementations
  2687. // to set errno to EOVERFLOW in this case.
  2688. return nullptr;
  2689. Value *DstArg = CI->getArgOperand(0);
  2690. Value *FmtArg = CI->getArgOperand(2);
  2691. // Check for a fixed format string.
  2692. StringRef FormatStr;
  2693. if (!getConstantStringInfo(FmtArg, FormatStr))
  2694. return nullptr;
  2695. // If we just have a format string (nothing else crazy) transform it.
  2696. if (CI->arg_size() == 3) {
  2697. if (FormatStr.contains('%'))
  2698. // Bail if the format string contains a directive and there are
  2699. // no arguments. We could handle "%%" in the future.
  2700. return nullptr;
  2701. return emitSnPrintfMemCpy(CI, FmtArg, FormatStr, N, B);
  2702. }
  2703. // The remaining optimizations require the format string to be "%s" or "%c"
  2704. // and have an extra operand.
  2705. if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() != 4)
  2706. return nullptr;
  2707. // Decode the second character of the format string.
  2708. if (FormatStr[1] == 'c') {
  2709. if (N <= 1) {
  2710. // Use an arbitary string of length 1 to transform the call into
  2711. // either a nul store (N == 1) or a no-op (N == 0) and fold it
  2712. // to one.
  2713. StringRef CharStr("*");
  2714. return emitSnPrintfMemCpy(CI, nullptr, CharStr, N, B);
  2715. }
  2716. // snprintf(dst, size, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
  2717. if (!CI->getArgOperand(3)->getType()->isIntegerTy())
  2718. return nullptr;
  2719. Value *V = B.CreateTrunc(CI->getArgOperand(3), B.getInt8Ty(), "char");
  2720. Value *Ptr = castToCStr(DstArg, B);
  2721. B.CreateStore(V, Ptr);
  2722. Ptr = B.CreateInBoundsGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
  2723. B.CreateStore(B.getInt8(0), Ptr);
  2724. return ConstantInt::get(CI->getType(), 1);
  2725. }
  2726. if (FormatStr[1] != 's')
  2727. return nullptr;
  2728. Value *StrArg = CI->getArgOperand(3);
  2729. // snprintf(dest, size, "%s", str) to llvm.memcpy(dest, str, len+1, 1)
  2730. StringRef Str;
  2731. if (!getConstantStringInfo(StrArg, Str))
  2732. return nullptr;
  2733. return emitSnPrintfMemCpy(CI, StrArg, Str, N, B);
  2734. }
  2735. Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilderBase &B) {
  2736. if (Value *V = optimizeSnPrintFString(CI, B)) {
  2737. return V;
  2738. }
  2739. if (isKnownNonZero(CI->getOperand(1), DL))
  2740. annotateNonNullNoUndefBasedOnAccess(CI, 0);
  2741. return nullptr;
  2742. }
  2743. Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI,
  2744. IRBuilderBase &B) {
  2745. optimizeErrorReporting(CI, B, 0);
  2746. // All the optimizations depend on the format string.
  2747. StringRef FormatStr;
  2748. if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
  2749. return nullptr;
  2750. // Do not do any of the following transformations if the fprintf return
  2751. // value is used, in general the fprintf return value is not compatible
  2752. // with fwrite(), fputc() or fputs().
  2753. if (!CI->use_empty())
  2754. return nullptr;
  2755. // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
  2756. if (CI->arg_size() == 2) {
  2757. // Could handle %% -> % if we cared.
  2758. if (FormatStr.contains('%'))
  2759. return nullptr; // We found a format specifier.
  2760. unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
  2761. Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
  2762. return copyFlags(
  2763. *CI, emitFWrite(CI->getArgOperand(1),
  2764. ConstantInt::get(SizeTTy, FormatStr.size()),
  2765. CI->getArgOperand(0), B, DL, TLI));
  2766. }
  2767. // The remaining optimizations require the format string to be "%s" or "%c"
  2768. // and have an extra operand.
  2769. if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
  2770. return nullptr;
  2771. // Decode the second character of the format string.
  2772. if (FormatStr[1] == 'c') {
  2773. // fprintf(F, "%c", chr) --> fputc((int)chr, F)
  2774. if (!CI->getArgOperand(2)->getType()->isIntegerTy())
  2775. return nullptr;
  2776. Type *IntTy = B.getIntNTy(TLI->getIntSize());
  2777. Value *V = B.CreateIntCast(CI->getArgOperand(2), IntTy, /*isSigned*/ true,
  2778. "chari");
  2779. return copyFlags(*CI, emitFPutC(V, CI->getArgOperand(0), B, TLI));
  2780. }
  2781. if (FormatStr[1] == 's') {
  2782. // fprintf(F, "%s", str) --> fputs(str, F)
  2783. if (!CI->getArgOperand(2)->getType()->isPointerTy())
  2784. return nullptr;
  2785. return copyFlags(
  2786. *CI, emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI));
  2787. }
  2788. return nullptr;
  2789. }
  2790. Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilderBase &B) {
  2791. Module *M = CI->getModule();
  2792. Function *Callee = CI->getCalledFunction();
  2793. FunctionType *FT = Callee->getFunctionType();
  2794. if (Value *V = optimizeFPrintFString(CI, B)) {
  2795. return V;
  2796. }
  2797. // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
  2798. // floating point arguments.
  2799. if (isLibFuncEmittable(M, TLI, LibFunc_fiprintf) &&
  2800. !callHasFloatingPointArgument(CI)) {
  2801. FunctionCallee FIPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_fiprintf,
  2802. FT, Callee->getAttributes());
  2803. CallInst *New = cast<CallInst>(CI->clone());
  2804. New->setCalledFunction(FIPrintFFn);
  2805. B.Insert(New);
  2806. return New;
  2807. }
  2808. // fprintf(stream, format, ...) -> __small_fprintf(stream, format, ...) if no
  2809. // 128-bit floating point arguments.
  2810. if (isLibFuncEmittable(M, TLI, LibFunc_small_fprintf) &&
  2811. !callHasFP128Argument(CI)) {
  2812. auto SmallFPrintFFn =
  2813. getOrInsertLibFunc(M, *TLI, LibFunc_small_fprintf, FT,
  2814. Callee->getAttributes());
  2815. CallInst *New = cast<CallInst>(CI->clone());
  2816. New->setCalledFunction(SmallFPrintFFn);
  2817. B.Insert(New);
  2818. return New;
  2819. }
  2820. return nullptr;
  2821. }
  2822. Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilderBase &B) {
  2823. optimizeErrorReporting(CI, B, 3);
  2824. // Get the element size and count.
  2825. ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  2826. ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
  2827. if (SizeC && CountC) {
  2828. uint64_t Bytes = SizeC->getZExtValue() * CountC->getZExtValue();
  2829. // If this is writing zero records, remove the call (it's a noop).
  2830. if (Bytes == 0)
  2831. return ConstantInt::get(CI->getType(), 0);
  2832. // If this is writing one byte, turn it into fputc.
  2833. // This optimisation is only valid, if the return value is unused.
  2834. if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
  2835. Value *Char = B.CreateLoad(B.getInt8Ty(),
  2836. castToCStr(CI->getArgOperand(0), B), "char");
  2837. Type *IntTy = B.getIntNTy(TLI->getIntSize());
  2838. Value *Cast = B.CreateIntCast(Char, IntTy, /*isSigned*/ true, "chari");
  2839. Value *NewCI = emitFPutC(Cast, CI->getArgOperand(3), B, TLI);
  2840. return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
  2841. }
  2842. }
  2843. return nullptr;
  2844. }
  2845. Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) {
  2846. optimizeErrorReporting(CI, B, 1);
  2847. // Don't rewrite fputs to fwrite when optimising for size because fwrite
  2848. // requires more arguments and thus extra MOVs are required.
  2849. bool OptForSize = CI->getFunction()->hasOptSize() ||
  2850. llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
  2851. PGSOQueryType::IRPass);
  2852. if (OptForSize)
  2853. return nullptr;
  2854. // We can't optimize if return value is used.
  2855. if (!CI->use_empty())
  2856. return nullptr;
  2857. // fputs(s,F) --> fwrite(s,strlen(s),1,F)
  2858. uint64_t Len = GetStringLength(CI->getArgOperand(0));
  2859. if (!Len)
  2860. return nullptr;
  2861. // Known to have no uses (see above).
  2862. unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
  2863. Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
  2864. return copyFlags(
  2865. *CI,
  2866. emitFWrite(CI->getArgOperand(0),
  2867. ConstantInt::get(SizeTTy, Len - 1),
  2868. CI->getArgOperand(1), B, DL, TLI));
  2869. }
  2870. Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilderBase &B) {
  2871. annotateNonNullNoUndefBasedOnAccess(CI, 0);
  2872. if (!CI->use_empty())
  2873. return nullptr;
  2874. // Check for a constant string.
  2875. // puts("") -> putchar('\n')
  2876. StringRef Str;
  2877. if (getConstantStringInfo(CI->getArgOperand(0), Str) && Str.empty()) {
  2878. // putchar takes an argument of the same type as puts returns, i.e.,
  2879. // int, which need not be 32 bits wide.
  2880. Type *IntTy = CI->getType();
  2881. return copyFlags(*CI, emitPutChar(ConstantInt::get(IntTy, '\n'), B, TLI));
  2882. }
  2883. return nullptr;
  2884. }
  2885. Value *LibCallSimplifier::optimizeBCopy(CallInst *CI, IRBuilderBase &B) {
  2886. // bcopy(src, dst, n) -> llvm.memmove(dst, src, n)
  2887. return copyFlags(*CI, B.CreateMemMove(CI->getArgOperand(1), Align(1),
  2888. CI->getArgOperand(0), Align(1),
  2889. CI->getArgOperand(2)));
  2890. }
  2891. bool LibCallSimplifier::hasFloatVersion(const Module *M, StringRef FuncName) {
  2892. SmallString<20> FloatFuncName = FuncName;
  2893. FloatFuncName += 'f';
  2894. return isLibFuncEmittable(M, TLI, FloatFuncName);
  2895. }
  2896. Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI,
  2897. IRBuilderBase &Builder) {
  2898. Module *M = CI->getModule();
  2899. LibFunc Func;
  2900. Function *Callee = CI->getCalledFunction();
  2901. // Check for string/memory library functions.
  2902. if (TLI->getLibFunc(*Callee, Func) && isLibFuncEmittable(M, TLI, Func)) {
  2903. // Make sure we never change the calling convention.
  2904. assert(
  2905. (ignoreCallingConv(Func) ||
  2906. TargetLibraryInfoImpl::isCallingConvCCompatible(CI)) &&
  2907. "Optimizing string/memory libcall would change the calling convention");
  2908. switch (Func) {
  2909. case LibFunc_strcat:
  2910. return optimizeStrCat(CI, Builder);
  2911. case LibFunc_strncat:
  2912. return optimizeStrNCat(CI, Builder);
  2913. case LibFunc_strchr:
  2914. return optimizeStrChr(CI, Builder);
  2915. case LibFunc_strrchr:
  2916. return optimizeStrRChr(CI, Builder);
  2917. case LibFunc_strcmp:
  2918. return optimizeStrCmp(CI, Builder);
  2919. case LibFunc_strncmp:
  2920. return optimizeStrNCmp(CI, Builder);
  2921. case LibFunc_strcpy:
  2922. return optimizeStrCpy(CI, Builder);
  2923. case LibFunc_stpcpy:
  2924. return optimizeStpCpy(CI, Builder);
  2925. case LibFunc_strlcpy:
  2926. return optimizeStrLCpy(CI, Builder);
  2927. case LibFunc_stpncpy:
  2928. return optimizeStringNCpy(CI, /*RetEnd=*/true, Builder);
  2929. case LibFunc_strncpy:
  2930. return optimizeStringNCpy(CI, /*RetEnd=*/false, Builder);
  2931. case LibFunc_strlen:
  2932. return optimizeStrLen(CI, Builder);
  2933. case LibFunc_strnlen:
  2934. return optimizeStrNLen(CI, Builder);
  2935. case LibFunc_strpbrk:
  2936. return optimizeStrPBrk(CI, Builder);
  2937. case LibFunc_strndup:
  2938. return optimizeStrNDup(CI, Builder);
  2939. case LibFunc_strtol:
  2940. case LibFunc_strtod:
  2941. case LibFunc_strtof:
  2942. case LibFunc_strtoul:
  2943. case LibFunc_strtoll:
  2944. case LibFunc_strtold:
  2945. case LibFunc_strtoull:
  2946. return optimizeStrTo(CI, Builder);
  2947. case LibFunc_strspn:
  2948. return optimizeStrSpn(CI, Builder);
  2949. case LibFunc_strcspn:
  2950. return optimizeStrCSpn(CI, Builder);
  2951. case LibFunc_strstr:
  2952. return optimizeStrStr(CI, Builder);
  2953. case LibFunc_memchr:
  2954. return optimizeMemChr(CI, Builder);
  2955. case LibFunc_memrchr:
  2956. return optimizeMemRChr(CI, Builder);
  2957. case LibFunc_bcmp:
  2958. return optimizeBCmp(CI, Builder);
  2959. case LibFunc_memcmp:
  2960. return optimizeMemCmp(CI, Builder);
  2961. case LibFunc_memcpy:
  2962. return optimizeMemCpy(CI, Builder);
  2963. case LibFunc_memccpy:
  2964. return optimizeMemCCpy(CI, Builder);
  2965. case LibFunc_mempcpy:
  2966. return optimizeMemPCpy(CI, Builder);
  2967. case LibFunc_memmove:
  2968. return optimizeMemMove(CI, Builder);
  2969. case LibFunc_memset:
  2970. return optimizeMemSet(CI, Builder);
  2971. case LibFunc_realloc:
  2972. return optimizeRealloc(CI, Builder);
  2973. case LibFunc_wcslen:
  2974. return optimizeWcslen(CI, Builder);
  2975. case LibFunc_bcopy:
  2976. return optimizeBCopy(CI, Builder);
  2977. default:
  2978. break;
  2979. }
  2980. }
  2981. return nullptr;
  2982. }
  2983. Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
  2984. LibFunc Func,
  2985. IRBuilderBase &Builder) {
  2986. const Module *M = CI->getModule();
  2987. // Don't optimize calls that require strict floating point semantics.
  2988. if (CI->isStrictFP())
  2989. return nullptr;
  2990. if (Value *V = optimizeTrigReflections(CI, Func, Builder))
  2991. return V;
  2992. switch (Func) {
  2993. case LibFunc_sinpif:
  2994. case LibFunc_sinpi:
  2995. case LibFunc_cospif:
  2996. case LibFunc_cospi:
  2997. return optimizeSinCosPi(CI, Builder);
  2998. case LibFunc_powf:
  2999. case LibFunc_pow:
  3000. case LibFunc_powl:
  3001. return optimizePow(CI, Builder);
  3002. case LibFunc_exp2l:
  3003. case LibFunc_exp2:
  3004. case LibFunc_exp2f:
  3005. return optimizeExp2(CI, Builder);
  3006. case LibFunc_fabsf:
  3007. case LibFunc_fabs:
  3008. case LibFunc_fabsl:
  3009. return replaceUnaryCall(CI, Builder, Intrinsic::fabs);
  3010. case LibFunc_sqrtf:
  3011. case LibFunc_sqrt:
  3012. case LibFunc_sqrtl:
  3013. return optimizeSqrt(CI, Builder);
  3014. case LibFunc_logf:
  3015. case LibFunc_log:
  3016. case LibFunc_logl:
  3017. case LibFunc_log10f:
  3018. case LibFunc_log10:
  3019. case LibFunc_log10l:
  3020. case LibFunc_log1pf:
  3021. case LibFunc_log1p:
  3022. case LibFunc_log1pl:
  3023. case LibFunc_log2f:
  3024. case LibFunc_log2:
  3025. case LibFunc_log2l:
  3026. case LibFunc_logbf:
  3027. case LibFunc_logb:
  3028. case LibFunc_logbl:
  3029. return optimizeLog(CI, Builder);
  3030. case LibFunc_tan:
  3031. case LibFunc_tanf:
  3032. case LibFunc_tanl:
  3033. return optimizeTan(CI, Builder);
  3034. case LibFunc_ceil:
  3035. return replaceUnaryCall(CI, Builder, Intrinsic::ceil);
  3036. case LibFunc_floor:
  3037. return replaceUnaryCall(CI, Builder, Intrinsic::floor);
  3038. case LibFunc_round:
  3039. return replaceUnaryCall(CI, Builder, Intrinsic::round);
  3040. case LibFunc_roundeven:
  3041. return replaceUnaryCall(CI, Builder, Intrinsic::roundeven);
  3042. case LibFunc_nearbyint:
  3043. return replaceUnaryCall(CI, Builder, Intrinsic::nearbyint);
  3044. case LibFunc_rint:
  3045. return replaceUnaryCall(CI, Builder, Intrinsic::rint);
  3046. case LibFunc_trunc:
  3047. return replaceUnaryCall(CI, Builder, Intrinsic::trunc);
  3048. case LibFunc_acos:
  3049. case LibFunc_acosh:
  3050. case LibFunc_asin:
  3051. case LibFunc_asinh:
  3052. case LibFunc_atan:
  3053. case LibFunc_atanh:
  3054. case LibFunc_cbrt:
  3055. case LibFunc_cosh:
  3056. case LibFunc_exp:
  3057. case LibFunc_exp10:
  3058. case LibFunc_expm1:
  3059. case LibFunc_cos:
  3060. case LibFunc_sin:
  3061. case LibFunc_sinh:
  3062. case LibFunc_tanh:
  3063. if (UnsafeFPShrink && hasFloatVersion(M, CI->getCalledFunction()->getName()))
  3064. return optimizeUnaryDoubleFP(CI, Builder, TLI, true);
  3065. return nullptr;
  3066. case LibFunc_copysign:
  3067. if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
  3068. return optimizeBinaryDoubleFP(CI, Builder, TLI);
  3069. return nullptr;
  3070. case LibFunc_fminf:
  3071. case LibFunc_fmin:
  3072. case LibFunc_fminl:
  3073. case LibFunc_fmaxf:
  3074. case LibFunc_fmax:
  3075. case LibFunc_fmaxl:
  3076. return optimizeFMinFMax(CI, Builder);
  3077. case LibFunc_cabs:
  3078. case LibFunc_cabsf:
  3079. case LibFunc_cabsl:
  3080. return optimizeCAbs(CI, Builder);
  3081. default:
  3082. return nullptr;
  3083. }
  3084. }
  3085. Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) {
  3086. Module *M = CI->getModule();
  3087. assert(!CI->isMustTailCall() && "These transforms aren't musttail safe.");
  3088. // TODO: Split out the code below that operates on FP calls so that
  3089. // we can all non-FP calls with the StrictFP attribute to be
  3090. // optimized.
  3091. if (CI->isNoBuiltin())
  3092. return nullptr;
  3093. LibFunc Func;
  3094. Function *Callee = CI->getCalledFunction();
  3095. bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
  3096. SmallVector<OperandBundleDef, 2> OpBundles;
  3097. CI->getOperandBundlesAsDefs(OpBundles);
  3098. IRBuilderBase::OperandBundlesGuard Guard(Builder);
  3099. Builder.setDefaultOperandBundles(OpBundles);
  3100. // Command-line parameter overrides instruction attribute.
  3101. // This can't be moved to optimizeFloatingPointLibCall() because it may be
  3102. // used by the intrinsic optimizations.
  3103. if (EnableUnsafeFPShrink.getNumOccurrences() > 0)
  3104. UnsafeFPShrink = EnableUnsafeFPShrink;
  3105. else if (isa<FPMathOperator>(CI) && CI->isFast())
  3106. UnsafeFPShrink = true;
  3107. // First, check for intrinsics.
  3108. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
  3109. if (!IsCallingConvC)
  3110. return nullptr;
  3111. // The FP intrinsics have corresponding constrained versions so we don't
  3112. // need to check for the StrictFP attribute here.
  3113. switch (II->getIntrinsicID()) {
  3114. case Intrinsic::pow:
  3115. return optimizePow(CI, Builder);
  3116. case Intrinsic::exp2:
  3117. return optimizeExp2(CI, Builder);
  3118. case Intrinsic::log:
  3119. case Intrinsic::log2:
  3120. case Intrinsic::log10:
  3121. return optimizeLog(CI, Builder);
  3122. case Intrinsic::sqrt:
  3123. return optimizeSqrt(CI, Builder);
  3124. case Intrinsic::memset:
  3125. return optimizeMemSet(CI, Builder);
  3126. case Intrinsic::memcpy:
  3127. return optimizeMemCpy(CI, Builder);
  3128. case Intrinsic::memmove:
  3129. return optimizeMemMove(CI, Builder);
  3130. default:
  3131. return nullptr;
  3132. }
  3133. }
  3134. // Also try to simplify calls to fortified library functions.
  3135. if (Value *SimplifiedFortifiedCI =
  3136. FortifiedSimplifier.optimizeCall(CI, Builder)) {
  3137. // Try to further simplify the result.
  3138. CallInst *SimplifiedCI = dyn_cast<CallInst>(SimplifiedFortifiedCI);
  3139. if (SimplifiedCI && SimplifiedCI->getCalledFunction()) {
  3140. // Ensure that SimplifiedCI's uses are complete, since some calls have
  3141. // their uses analyzed.
  3142. replaceAllUsesWith(CI, SimplifiedCI);
  3143. // Set insertion point to SimplifiedCI to guarantee we reach all uses
  3144. // we might replace later on.
  3145. IRBuilderBase::InsertPointGuard Guard(Builder);
  3146. Builder.SetInsertPoint(SimplifiedCI);
  3147. if (Value *V = optimizeStringMemoryLibCall(SimplifiedCI, Builder)) {
  3148. // If we were able to further simplify, remove the now redundant call.
  3149. substituteInParent(SimplifiedCI, V);
  3150. return V;
  3151. }
  3152. }
  3153. return SimplifiedFortifiedCI;
  3154. }
  3155. // Then check for known library functions.
  3156. if (TLI->getLibFunc(*Callee, Func) && isLibFuncEmittable(M, TLI, Func)) {
  3157. // We never change the calling convention.
  3158. if (!ignoreCallingConv(Func) && !IsCallingConvC)
  3159. return nullptr;
  3160. if (Value *V = optimizeStringMemoryLibCall(CI, Builder))
  3161. return V;
  3162. if (Value *V = optimizeFloatingPointLibCall(CI, Func, Builder))
  3163. return V;
  3164. switch (Func) {
  3165. case LibFunc_ffs:
  3166. case LibFunc_ffsl:
  3167. case LibFunc_ffsll:
  3168. return optimizeFFS(CI, Builder);
  3169. case LibFunc_fls:
  3170. case LibFunc_flsl:
  3171. case LibFunc_flsll:
  3172. return optimizeFls(CI, Builder);
  3173. case LibFunc_abs:
  3174. case LibFunc_labs:
  3175. case LibFunc_llabs:
  3176. return optimizeAbs(CI, Builder);
  3177. case LibFunc_isdigit:
  3178. return optimizeIsDigit(CI, Builder);
  3179. case LibFunc_isascii:
  3180. return optimizeIsAscii(CI, Builder);
  3181. case LibFunc_toascii:
  3182. return optimizeToAscii(CI, Builder);
  3183. case LibFunc_atoi:
  3184. case LibFunc_atol:
  3185. case LibFunc_atoll:
  3186. return optimizeAtoi(CI, Builder);
  3187. case LibFunc_strtol:
  3188. case LibFunc_strtoll:
  3189. return optimizeStrToInt(CI, Builder, /*AsSigned=*/true);
  3190. case LibFunc_strtoul:
  3191. case LibFunc_strtoull:
  3192. return optimizeStrToInt(CI, Builder, /*AsSigned=*/false);
  3193. case LibFunc_printf:
  3194. return optimizePrintF(CI, Builder);
  3195. case LibFunc_sprintf:
  3196. return optimizeSPrintF(CI, Builder);
  3197. case LibFunc_snprintf:
  3198. return optimizeSnPrintF(CI, Builder);
  3199. case LibFunc_fprintf:
  3200. return optimizeFPrintF(CI, Builder);
  3201. case LibFunc_fwrite:
  3202. return optimizeFWrite(CI, Builder);
  3203. case LibFunc_fputs:
  3204. return optimizeFPuts(CI, Builder);
  3205. case LibFunc_puts:
  3206. return optimizePuts(CI, Builder);
  3207. case LibFunc_perror:
  3208. return optimizeErrorReporting(CI, Builder);
  3209. case LibFunc_vfprintf:
  3210. case LibFunc_fiprintf:
  3211. return optimizeErrorReporting(CI, Builder, 0);
  3212. default:
  3213. return nullptr;
  3214. }
  3215. }
  3216. return nullptr;
  3217. }
  3218. LibCallSimplifier::LibCallSimplifier(
  3219. const DataLayout &DL, const TargetLibraryInfo *TLI,
  3220. OptimizationRemarkEmitter &ORE,
  3221. BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
  3222. function_ref<void(Instruction *, Value *)> Replacer,
  3223. function_ref<void(Instruction *)> Eraser)
  3224. : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), ORE(ORE), BFI(BFI), PSI(PSI),
  3225. Replacer(Replacer), Eraser(Eraser) {}
  3226. void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) {
  3227. // Indirect through the replacer used in this instance.
  3228. Replacer(I, With);
  3229. }
  3230. void LibCallSimplifier::eraseFromParent(Instruction *I) {
  3231. Eraser(I);
  3232. }
  3233. // TODO:
  3234. // Additional cases that we need to add to this file:
  3235. //
  3236. // cbrt:
  3237. // * cbrt(expN(X)) -> expN(x/3)
  3238. // * cbrt(sqrt(x)) -> pow(x,1/6)
  3239. // * cbrt(cbrt(x)) -> pow(x,1/9)
  3240. //
  3241. // exp, expf, expl:
  3242. // * exp(log(x)) -> x
  3243. //
  3244. // log, logf, logl:
  3245. // * log(exp(x)) -> x
  3246. // * log(exp(y)) -> y*log(e)
  3247. // * log(exp10(y)) -> y*log(10)
  3248. // * log(sqrt(x)) -> 0.5*log(x)
  3249. //
  3250. // pow, powf, powl:
  3251. // * pow(sqrt(x),y) -> pow(x,y*0.5)
  3252. // * pow(pow(x,y),z)-> pow(x,y*z)
  3253. //
  3254. // signbit:
  3255. // * signbit(cnst) -> cnst'
  3256. // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
  3257. //
  3258. // sqrt, sqrtf, sqrtl:
  3259. // * sqrt(expN(x)) -> expN(x*0.5)
  3260. // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
  3261. // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
  3262. //
  3263. //===----------------------------------------------------------------------===//
  3264. // Fortified Library Call Optimizations
  3265. //===----------------------------------------------------------------------===//
  3266. bool FortifiedLibCallSimplifier::isFortifiedCallFoldable(
  3267. CallInst *CI, unsigned ObjSizeOp, std::optional<unsigned> SizeOp,
  3268. std::optional<unsigned> StrOp, std::optional<unsigned> FlagOp) {
  3269. // If this function takes a flag argument, the implementation may use it to
  3270. // perform extra checks. Don't fold into the non-checking variant.
  3271. if (FlagOp) {
  3272. ConstantInt *Flag = dyn_cast<ConstantInt>(CI->getArgOperand(*FlagOp));
  3273. if (!Flag || !Flag->isZero())
  3274. return false;
  3275. }
  3276. if (SizeOp && CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(*SizeOp))
  3277. return true;
  3278. if (ConstantInt *ObjSizeCI =
  3279. dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) {
  3280. if (ObjSizeCI->isMinusOne())
  3281. return true;
  3282. // If the object size wasn't -1 (unknown), bail out if we were asked to.
  3283. if (OnlyLowerUnknownSize)
  3284. return false;
  3285. if (StrOp) {
  3286. uint64_t Len = GetStringLength(CI->getArgOperand(*StrOp));
  3287. // If the length is 0 we don't know how long it is and so we can't
  3288. // remove the check.
  3289. if (Len)
  3290. annotateDereferenceableBytes(CI, *StrOp, Len);
  3291. else
  3292. return false;
  3293. return ObjSizeCI->getZExtValue() >= Len;
  3294. }
  3295. if (SizeOp) {
  3296. if (ConstantInt *SizeCI =
  3297. dyn_cast<ConstantInt>(CI->getArgOperand(*SizeOp)))
  3298. return ObjSizeCI->getZExtValue() >= SizeCI->getZExtValue();
  3299. }
  3300. }
  3301. return false;
  3302. }
  3303. Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI,
  3304. IRBuilderBase &B) {
  3305. if (isFortifiedCallFoldable(CI, 3, 2)) {
  3306. CallInst *NewCI =
  3307. B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
  3308. Align(1), CI->getArgOperand(2));
  3309. mergeAttributesAndFlags(NewCI, *CI);
  3310. return CI->getArgOperand(0);
  3311. }
  3312. return nullptr;
  3313. }
  3314. Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI,
  3315. IRBuilderBase &B) {
  3316. if (isFortifiedCallFoldable(CI, 3, 2)) {
  3317. CallInst *NewCI =
  3318. B.CreateMemMove(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
  3319. Align(1), CI->getArgOperand(2));
  3320. mergeAttributesAndFlags(NewCI, *CI);
  3321. return CI->getArgOperand(0);
  3322. }
  3323. return nullptr;
  3324. }
  3325. Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI,
  3326. IRBuilderBase &B) {
  3327. if (isFortifiedCallFoldable(CI, 3, 2)) {
  3328. Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
  3329. CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val,
  3330. CI->getArgOperand(2), Align(1));
  3331. mergeAttributesAndFlags(NewCI, *CI);
  3332. return CI->getArgOperand(0);
  3333. }
  3334. return nullptr;
  3335. }
  3336. Value *FortifiedLibCallSimplifier::optimizeMemPCpyChk(CallInst *CI,
  3337. IRBuilderBase &B) {
  3338. const DataLayout &DL = CI->getModule()->getDataLayout();
  3339. if (isFortifiedCallFoldable(CI, 3, 2))
  3340. if (Value *Call = emitMemPCpy(CI->getArgOperand(0), CI->getArgOperand(1),
  3341. CI->getArgOperand(2), B, DL, TLI)) {
  3342. return mergeAttributesAndFlags(cast<CallInst>(Call), *CI);
  3343. }
  3344. return nullptr;
  3345. }
  3346. Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
  3347. IRBuilderBase &B,
  3348. LibFunc Func) {
  3349. const DataLayout &DL = CI->getModule()->getDataLayout();
  3350. Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
  3351. *ObjSize = CI->getArgOperand(2);
  3352. // __stpcpy_chk(x,x,...) -> x+strlen(x)
  3353. if (Func == LibFunc_stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) {
  3354. Value *StrLen = emitStrLen(Src, B, DL, TLI);
  3355. return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
  3356. }
  3357. // If a) we don't have any length information, or b) we know this will
  3358. // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
  3359. // st[rp]cpy_chk call which may fail at runtime if the size is too long.
  3360. // TODO: It might be nice to get a maximum length out of the possible
  3361. // string lengths for varying.
  3362. if (isFortifiedCallFoldable(CI, 2, std::nullopt, 1)) {
  3363. if (Func == LibFunc_strcpy_chk)
  3364. return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
  3365. else
  3366. return copyFlags(*CI, emitStpCpy(Dst, Src, B, TLI));
  3367. }
  3368. if (OnlyLowerUnknownSize)
  3369. return nullptr;
  3370. // Maybe we can stil fold __st[rp]cpy_chk to __memcpy_chk.
  3371. uint64_t Len = GetStringLength(Src);
  3372. if (Len)
  3373. annotateDereferenceableBytes(CI, 1, Len);
  3374. else
  3375. return nullptr;
  3376. unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
  3377. Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
  3378. Value *LenV = ConstantInt::get(SizeTTy, Len);
  3379. Value *Ret = emitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI);
  3380. // If the function was an __stpcpy_chk, and we were able to fold it into
  3381. // a __memcpy_chk, we still need to return the correct end pointer.
  3382. if (Ret && Func == LibFunc_stpcpy_chk)
  3383. return B.CreateInBoundsGEP(B.getInt8Ty(), Dst,
  3384. ConstantInt::get(SizeTTy, Len - 1));
  3385. return copyFlags(*CI, cast<CallInst>(Ret));
  3386. }
  3387. Value *FortifiedLibCallSimplifier::optimizeStrLenChk(CallInst *CI,
  3388. IRBuilderBase &B) {
  3389. if (isFortifiedCallFoldable(CI, 1, std::nullopt, 0))
  3390. return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B,
  3391. CI->getModule()->getDataLayout(), TLI));
  3392. return nullptr;
  3393. }
  3394. Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
  3395. IRBuilderBase &B,
  3396. LibFunc Func) {
  3397. if (isFortifiedCallFoldable(CI, 3, 2)) {
  3398. if (Func == LibFunc_strncpy_chk)
  3399. return copyFlags(*CI,
  3400. emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
  3401. CI->getArgOperand(2), B, TLI));
  3402. else
  3403. return copyFlags(*CI,
  3404. emitStpNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
  3405. CI->getArgOperand(2), B, TLI));
  3406. }
  3407. return nullptr;
  3408. }
  3409. Value *FortifiedLibCallSimplifier::optimizeMemCCpyChk(CallInst *CI,
  3410. IRBuilderBase &B) {
  3411. if (isFortifiedCallFoldable(CI, 4, 3))
  3412. return copyFlags(
  3413. *CI, emitMemCCpy(CI->getArgOperand(0), CI->getArgOperand(1),
  3414. CI->getArgOperand(2), CI->getArgOperand(3), B, TLI));
  3415. return nullptr;
  3416. }
  3417. Value *FortifiedLibCallSimplifier::optimizeSNPrintfChk(CallInst *CI,
  3418. IRBuilderBase &B) {
  3419. if (isFortifiedCallFoldable(CI, 3, 1, std::nullopt, 2)) {
  3420. SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 5));
  3421. return copyFlags(*CI,
  3422. emitSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1),
  3423. CI->getArgOperand(4), VariadicArgs, B, TLI));
  3424. }
  3425. return nullptr;
  3426. }
  3427. Value *FortifiedLibCallSimplifier::optimizeSPrintfChk(CallInst *CI,
  3428. IRBuilderBase &B) {
  3429. if (isFortifiedCallFoldable(CI, 2, std::nullopt, std::nullopt, 1)) {
  3430. SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 4));
  3431. return copyFlags(*CI,
  3432. emitSPrintf(CI->getArgOperand(0), CI->getArgOperand(3),
  3433. VariadicArgs, B, TLI));
  3434. }
  3435. return nullptr;
  3436. }
  3437. Value *FortifiedLibCallSimplifier::optimizeStrCatChk(CallInst *CI,
  3438. IRBuilderBase &B) {
  3439. if (isFortifiedCallFoldable(CI, 2))
  3440. return copyFlags(
  3441. *CI, emitStrCat(CI->getArgOperand(0), CI->getArgOperand(1), B, TLI));
  3442. return nullptr;
  3443. }
  3444. Value *FortifiedLibCallSimplifier::optimizeStrLCat(CallInst *CI,
  3445. IRBuilderBase &B) {
  3446. if (isFortifiedCallFoldable(CI, 3))
  3447. return copyFlags(*CI,
  3448. emitStrLCat(CI->getArgOperand(0), CI->getArgOperand(1),
  3449. CI->getArgOperand(2), B, TLI));
  3450. return nullptr;
  3451. }
  3452. Value *FortifiedLibCallSimplifier::optimizeStrNCatChk(CallInst *CI,
  3453. IRBuilderBase &B) {
  3454. if (isFortifiedCallFoldable(CI, 3))
  3455. return copyFlags(*CI,
  3456. emitStrNCat(CI->getArgOperand(0), CI->getArgOperand(1),
  3457. CI->getArgOperand(2), B, TLI));
  3458. return nullptr;
  3459. }
  3460. Value *FortifiedLibCallSimplifier::optimizeStrLCpyChk(CallInst *CI,
  3461. IRBuilderBase &B) {
  3462. if (isFortifiedCallFoldable(CI, 3))
  3463. return copyFlags(*CI,
  3464. emitStrLCpy(CI->getArgOperand(0), CI->getArgOperand(1),
  3465. CI->getArgOperand(2), B, TLI));
  3466. return nullptr;
  3467. }
  3468. Value *FortifiedLibCallSimplifier::optimizeVSNPrintfChk(CallInst *CI,
  3469. IRBuilderBase &B) {
  3470. if (isFortifiedCallFoldable(CI, 3, 1, std::nullopt, 2))
  3471. return copyFlags(
  3472. *CI, emitVSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1),
  3473. CI->getArgOperand(4), CI->getArgOperand(5), B, TLI));
  3474. return nullptr;
  3475. }
  3476. Value *FortifiedLibCallSimplifier::optimizeVSPrintfChk(CallInst *CI,
  3477. IRBuilderBase &B) {
  3478. if (isFortifiedCallFoldable(CI, 2, std::nullopt, std::nullopt, 1))
  3479. return copyFlags(*CI,
  3480. emitVSPrintf(CI->getArgOperand(0), CI->getArgOperand(3),
  3481. CI->getArgOperand(4), B, TLI));
  3482. return nullptr;
  3483. }
  3484. Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI,
  3485. IRBuilderBase &Builder) {
  3486. // FIXME: We shouldn't be changing "nobuiltin" or TLI unavailable calls here.
  3487. // Some clang users checked for _chk libcall availability using:
  3488. // __has_builtin(__builtin___memcpy_chk)
  3489. // When compiling with -fno-builtin, this is always true.
  3490. // When passing -ffreestanding/-mkernel, which both imply -fno-builtin, we
  3491. // end up with fortified libcalls, which isn't acceptable in a freestanding
  3492. // environment which only provides their non-fortified counterparts.
  3493. //
  3494. // Until we change clang and/or teach external users to check for availability
  3495. // differently, disregard the "nobuiltin" attribute and TLI::has.
  3496. //
  3497. // PR23093.
  3498. LibFunc Func;
  3499. Function *Callee = CI->getCalledFunction();
  3500. bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
  3501. SmallVector<OperandBundleDef, 2> OpBundles;
  3502. CI->getOperandBundlesAsDefs(OpBundles);
  3503. IRBuilderBase::OperandBundlesGuard Guard(Builder);
  3504. Builder.setDefaultOperandBundles(OpBundles);
  3505. // First, check that this is a known library functions and that the prototype
  3506. // is correct.
  3507. if (!TLI->getLibFunc(*Callee, Func))
  3508. return nullptr;
  3509. // We never change the calling convention.
  3510. if (!ignoreCallingConv(Func) && !IsCallingConvC)
  3511. return nullptr;
  3512. switch (Func) {
  3513. case LibFunc_memcpy_chk:
  3514. return optimizeMemCpyChk(CI, Builder);
  3515. case LibFunc_mempcpy_chk:
  3516. return optimizeMemPCpyChk(CI, Builder);
  3517. case LibFunc_memmove_chk:
  3518. return optimizeMemMoveChk(CI, Builder);
  3519. case LibFunc_memset_chk:
  3520. return optimizeMemSetChk(CI, Builder);
  3521. case LibFunc_stpcpy_chk:
  3522. case LibFunc_strcpy_chk:
  3523. return optimizeStrpCpyChk(CI, Builder, Func);
  3524. case LibFunc_strlen_chk:
  3525. return optimizeStrLenChk(CI, Builder);
  3526. case LibFunc_stpncpy_chk:
  3527. case LibFunc_strncpy_chk:
  3528. return optimizeStrpNCpyChk(CI, Builder, Func);
  3529. case LibFunc_memccpy_chk:
  3530. return optimizeMemCCpyChk(CI, Builder);
  3531. case LibFunc_snprintf_chk:
  3532. return optimizeSNPrintfChk(CI, Builder);
  3533. case LibFunc_sprintf_chk:
  3534. return optimizeSPrintfChk(CI, Builder);
  3535. case LibFunc_strcat_chk:
  3536. return optimizeStrCatChk(CI, Builder);
  3537. case LibFunc_strlcat_chk:
  3538. return optimizeStrLCat(CI, Builder);
  3539. case LibFunc_strncat_chk:
  3540. return optimizeStrNCatChk(CI, Builder);
  3541. case LibFunc_strlcpy_chk:
  3542. return optimizeStrLCpyChk(CI, Builder);
  3543. case LibFunc_vsnprintf_chk:
  3544. return optimizeVSNPrintfChk(CI, Builder);
  3545. case LibFunc_vsprintf_chk:
  3546. return optimizeVSPrintfChk(CI, Builder);
  3547. default:
  3548. break;
  3549. }
  3550. return nullptr;
  3551. }
  3552. FortifiedLibCallSimplifier::FortifiedLibCallSimplifier(
  3553. const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
  3554. : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}