CGCall.cpp 175 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563
  1. //===--- CGCall.cpp - Encapsulate calling convention details --------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // These classes wrap the information about a call or function
  11. // definition used to handle ABI compliancy.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "CGCall.h"
  15. #include "ABIInfo.h"
  16. #include "CGBlocks.h"
  17. #include "CGCXXABI.h"
  18. #include "CGCleanup.h"
  19. #include "CodeGenFunction.h"
  20. #include "CodeGenModule.h"
  21. #include "TargetInfo.h"
  22. #include "clang/AST/Decl.h"
  23. #include "clang/AST/DeclCXX.h"
  24. #include "clang/AST/DeclObjC.h"
  25. #include "clang/Basic/TargetBuiltins.h"
  26. #include "clang/Basic/TargetInfo.h"
  27. #include "clang/CodeGen/CGFunctionInfo.h"
  28. #include "clang/CodeGen/SwiftCallingConv.h"
  29. #include "clang/Frontend/CodeGenOptions.h"
  30. #include "llvm/ADT/StringExtras.h"
  31. #include "llvm/Transforms/Utils/Local.h"
  32. #include "llvm/Analysis/ValueTracking.h"
  33. #include "llvm/IR/Attributes.h"
  34. #include "llvm/IR/CallSite.h"
  35. #include "llvm/IR/CallingConv.h"
  36. #include "llvm/IR/DataLayout.h"
  37. #include "llvm/IR/InlineAsm.h"
  38. #include "llvm/IR/IntrinsicInst.h"
  39. #include "llvm/IR/Intrinsics.h"
  40. using namespace clang;
  41. using namespace CodeGen;
  42. /***/
  43. unsigned CodeGenTypes::ClangCallConvToLLVMCallConv(CallingConv CC) {
  44. switch (CC) {
  45. default: return llvm::CallingConv::C;
  46. case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
  47. case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
  48. case CC_X86RegCall: return llvm::CallingConv::X86_RegCall;
  49. case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
  50. case CC_Win64: return llvm::CallingConv::Win64;
  51. case CC_X86_64SysV: return llvm::CallingConv::X86_64_SysV;
  52. case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
  53. case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
  54. case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI;
  55. // TODO: Add support for __pascal to LLVM.
  56. case CC_X86Pascal: return llvm::CallingConv::C;
  57. // TODO: Add support for __vectorcall to LLVM.
  58. case CC_X86VectorCall: return llvm::CallingConv::X86_VectorCall;
  59. case CC_AArch64VectorCall: return llvm::CallingConv::AArch64_VectorCall;
  60. case CC_SpirFunction: return llvm::CallingConv::SPIR_FUNC;
  61. case CC_OpenCLKernel: return CGM.getTargetCodeGenInfo().getOpenCLKernelCallingConv();
  62. case CC_PreserveMost: return llvm::CallingConv::PreserveMost;
  63. case CC_PreserveAll: return llvm::CallingConv::PreserveAll;
  64. case CC_Swift: return llvm::CallingConv::Swift;
  65. }
  66. }
  67. /// Derives the 'this' type for codegen purposes, i.e. ignoring method
  68. /// qualification.
  69. /// FIXME: address space qualification?
  70. static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
  71. QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
  72. return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
  73. }
  74. /// Returns the canonical formal type of the given C++ method.
  75. static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
  76. return MD->getType()->getCanonicalTypeUnqualified()
  77. .getAs<FunctionProtoType>();
  78. }
  79. /// Returns the "extra-canonicalized" return type, which discards
  80. /// qualifiers on the return type. Codegen doesn't care about them,
  81. /// and it makes ABI code a little easier to be able to assume that
  82. /// all parameter and return types are top-level unqualified.
  83. static CanQualType GetReturnType(QualType RetTy) {
  84. return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
  85. }
  86. /// Arrange the argument and result information for a value of the given
  87. /// unprototyped freestanding function type.
  88. const CGFunctionInfo &
  89. CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) {
  90. // When translating an unprototyped function type, always use a
  91. // variadic type.
  92. return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(),
  93. /*instanceMethod=*/false,
  94. /*chainCall=*/false, None,
  95. FTNP->getExtInfo(), {}, RequiredArgs(0));
  96. }
  97. static void addExtParameterInfosForCall(
  98. llvm::SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &paramInfos,
  99. const FunctionProtoType *proto,
  100. unsigned prefixArgs,
  101. unsigned totalArgs) {
  102. assert(proto->hasExtParameterInfos());
  103. assert(paramInfos.size() <= prefixArgs);
  104. assert(proto->getNumParams() + prefixArgs <= totalArgs);
  105. paramInfos.reserve(totalArgs);
  106. // Add default infos for any prefix args that don't already have infos.
  107. paramInfos.resize(prefixArgs);
  108. // Add infos for the prototype.
  109. for (const auto &ParamInfo : proto->getExtParameterInfos()) {
  110. paramInfos.push_back(ParamInfo);
  111. // pass_object_size params have no parameter info.
  112. if (ParamInfo.hasPassObjectSize())
  113. paramInfos.emplace_back();
  114. }
  115. assert(paramInfos.size() <= totalArgs &&
  116. "Did we forget to insert pass_object_size args?");
  117. // Add default infos for the variadic and/or suffix arguments.
  118. paramInfos.resize(totalArgs);
  119. }
  120. /// Adds the formal parameters in FPT to the given prefix. If any parameter in
  121. /// FPT has pass_object_size attrs, then we'll add parameters for those, too.
  122. static void appendParameterTypes(const CodeGenTypes &CGT,
  123. SmallVectorImpl<CanQualType> &prefix,
  124. SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &paramInfos,
  125. CanQual<FunctionProtoType> FPT) {
  126. // Fast path: don't touch param info if we don't need to.
  127. if (!FPT->hasExtParameterInfos()) {
  128. assert(paramInfos.empty() &&
  129. "We have paramInfos, but the prototype doesn't?");
  130. prefix.append(FPT->param_type_begin(), FPT->param_type_end());
  131. return;
  132. }
  133. unsigned PrefixSize = prefix.size();
  134. // In the vast majority of cases, we'll have precisely FPT->getNumParams()
  135. // parameters; the only thing that can change this is the presence of
  136. // pass_object_size. So, we preallocate for the common case.
  137. prefix.reserve(prefix.size() + FPT->getNumParams());
  138. auto ExtInfos = FPT->getExtParameterInfos();
  139. assert(ExtInfos.size() == FPT->getNumParams());
  140. for (unsigned I = 0, E = FPT->getNumParams(); I != E; ++I) {
  141. prefix.push_back(FPT->getParamType(I));
  142. if (ExtInfos[I].hasPassObjectSize())
  143. prefix.push_back(CGT.getContext().getSizeType());
  144. }
  145. addExtParameterInfosForCall(paramInfos, FPT.getTypePtr(), PrefixSize,
  146. prefix.size());
  147. }
  148. /// Arrange the LLVM function layout for a value of the given function
  149. /// type, on top of any implicit parameters already stored.
  150. static const CGFunctionInfo &
  151. arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool instanceMethod,
  152. SmallVectorImpl<CanQualType> &prefix,
  153. CanQual<FunctionProtoType> FTP,
  154. const FunctionDecl *FD) {
  155. SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
  156. RequiredArgs Required =
  157. RequiredArgs::forPrototypePlus(FTP, prefix.size(), FD);
  158. // FIXME: Kill copy.
  159. appendParameterTypes(CGT, prefix, paramInfos, FTP);
  160. CanQualType resultType = FTP->getReturnType().getUnqualifiedType();
  161. return CGT.arrangeLLVMFunctionInfo(resultType, instanceMethod,
  162. /*chainCall=*/false, prefix,
  163. FTP->getExtInfo(), paramInfos,
  164. Required);
  165. }
  166. /// Arrange the argument and result information for a value of the
  167. /// given freestanding function type.
  168. const CGFunctionInfo &
  169. CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP,
  170. const FunctionDecl *FD) {
  171. SmallVector<CanQualType, 16> argTypes;
  172. return ::arrangeLLVMFunctionInfo(*this, /*instanceMethod=*/false, argTypes,
  173. FTP, FD);
  174. }
  175. static CallingConv getCallingConventionForDecl(const Decl *D, bool IsWindows) {
  176. // Set the appropriate calling convention for the Function.
  177. if (D->hasAttr<StdCallAttr>())
  178. return CC_X86StdCall;
  179. if (D->hasAttr<FastCallAttr>())
  180. return CC_X86FastCall;
  181. if (D->hasAttr<RegCallAttr>())
  182. return CC_X86RegCall;
  183. if (D->hasAttr<ThisCallAttr>())
  184. return CC_X86ThisCall;
  185. if (D->hasAttr<VectorCallAttr>())
  186. return CC_X86VectorCall;
  187. if (D->hasAttr<PascalAttr>())
  188. return CC_X86Pascal;
  189. if (PcsAttr *PCS = D->getAttr<PcsAttr>())
  190. return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
  191. if (D->hasAttr<AArch64VectorPcsAttr>())
  192. return CC_AArch64VectorCall;
  193. if (D->hasAttr<IntelOclBiccAttr>())
  194. return CC_IntelOclBicc;
  195. if (D->hasAttr<MSABIAttr>())
  196. return IsWindows ? CC_C : CC_Win64;
  197. if (D->hasAttr<SysVABIAttr>())
  198. return IsWindows ? CC_X86_64SysV : CC_C;
  199. if (D->hasAttr<PreserveMostAttr>())
  200. return CC_PreserveMost;
  201. if (D->hasAttr<PreserveAllAttr>())
  202. return CC_PreserveAll;
  203. return CC_C;
  204. }
  205. /// Arrange the argument and result information for a call to an
  206. /// unknown C++ non-static member function of the given abstract type.
  207. /// (Zero value of RD means we don't have any meaningful "this" argument type,
  208. /// so fall back to a generic pointer type).
  209. /// The member function must be an ordinary function, i.e. not a
  210. /// constructor or destructor.
  211. const CGFunctionInfo &
  212. CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD,
  213. const FunctionProtoType *FTP,
  214. const CXXMethodDecl *MD) {
  215. SmallVector<CanQualType, 16> argTypes;
  216. // Add the 'this' pointer.
  217. if (RD)
  218. argTypes.push_back(GetThisType(Context, RD));
  219. else
  220. argTypes.push_back(Context.VoidPtrTy);
  221. return ::arrangeLLVMFunctionInfo(
  222. *this, true, argTypes,
  223. FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>(), MD);
  224. }
  225. /// Set calling convention for CUDA/HIP kernel.
  226. static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM,
  227. const FunctionDecl *FD) {
  228. if (FD->hasAttr<CUDAGlobalAttr>()) {
  229. const FunctionType *FT = FTy->getAs<FunctionType>();
  230. CGM.getTargetCodeGenInfo().setCUDAKernelCallingConvention(FT);
  231. FTy = FT->getCanonicalTypeUnqualified();
  232. }
  233. }
  234. /// Arrange the argument and result information for a declaration or
  235. /// definition of the given C++ non-static member function. The
  236. /// member function must be an ordinary function, i.e. not a
  237. /// constructor or destructor.
  238. const CGFunctionInfo &
  239. CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) {
  240. assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!");
  241. assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
  242. CanQualType FT = GetFormalType(MD).getAs<Type>();
  243. setCUDAKernelCallingConvention(FT, CGM, MD);
  244. auto prototype = FT.getAs<FunctionProtoType>();
  245. if (MD->isInstance()) {
  246. // The abstract case is perfectly fine.
  247. const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD);
  248. return arrangeCXXMethodType(ThisType, prototype.getTypePtr(), MD);
  249. }
  250. return arrangeFreeFunctionType(prototype, MD);
  251. }
  252. bool CodeGenTypes::inheritingCtorHasParams(
  253. const InheritedConstructor &Inherited, CXXCtorType Type) {
  254. // Parameters are unnecessary if we're constructing a base class subobject
  255. // and the inherited constructor lives in a virtual base.
  256. return Type == Ctor_Complete ||
  257. !Inherited.getShadowDecl()->constructsVirtualBase() ||
  258. !Target.getCXXABI().hasConstructorVariants();
  259. }
  260. const CGFunctionInfo &
  261. CodeGenTypes::arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
  262. StructorType Type) {
  263. SmallVector<CanQualType, 16> argTypes;
  264. SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
  265. argTypes.push_back(GetThisType(Context, MD->getParent()));
  266. bool PassParams = true;
  267. GlobalDecl GD;
  268. if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
  269. GD = GlobalDecl(CD, toCXXCtorType(Type));
  270. // A base class inheriting constructor doesn't get forwarded arguments
  271. // needed to construct a virtual base (or base class thereof).
  272. if (auto Inherited = CD->getInheritedConstructor())
  273. PassParams = inheritingCtorHasParams(Inherited, toCXXCtorType(Type));
  274. } else {
  275. auto *DD = dyn_cast<CXXDestructorDecl>(MD);
  276. GD = GlobalDecl(DD, toCXXDtorType(Type));
  277. }
  278. CanQual<FunctionProtoType> FTP = GetFormalType(MD);
  279. // Add the formal parameters.
  280. if (PassParams)
  281. appendParameterTypes(*this, argTypes, paramInfos, FTP);
  282. CGCXXABI::AddedStructorArgs AddedArgs =
  283. TheCXXABI.buildStructorSignature(MD, Type, argTypes);
  284. if (!paramInfos.empty()) {
  285. // Note: prefix implies after the first param.
  286. if (AddedArgs.Prefix)
  287. paramInfos.insert(paramInfos.begin() + 1, AddedArgs.Prefix,
  288. FunctionProtoType::ExtParameterInfo{});
  289. if (AddedArgs.Suffix)
  290. paramInfos.append(AddedArgs.Suffix,
  291. FunctionProtoType::ExtParameterInfo{});
  292. }
  293. RequiredArgs required =
  294. (PassParams && MD->isVariadic() ? RequiredArgs(argTypes.size())
  295. : RequiredArgs::All);
  296. FunctionType::ExtInfo extInfo = FTP->getExtInfo();
  297. CanQualType resultType = TheCXXABI.HasThisReturn(GD)
  298. ? argTypes.front()
  299. : TheCXXABI.hasMostDerivedReturn(GD)
  300. ? CGM.getContext().VoidPtrTy
  301. : Context.VoidTy;
  302. return arrangeLLVMFunctionInfo(resultType, /*instanceMethod=*/true,
  303. /*chainCall=*/false, argTypes, extInfo,
  304. paramInfos, required);
  305. }
  306. static SmallVector<CanQualType, 16>
  307. getArgTypesForCall(ASTContext &ctx, const CallArgList &args) {
  308. SmallVector<CanQualType, 16> argTypes;
  309. for (auto &arg : args)
  310. argTypes.push_back(ctx.getCanonicalParamType(arg.Ty));
  311. return argTypes;
  312. }
  313. static SmallVector<CanQualType, 16>
  314. getArgTypesForDeclaration(ASTContext &ctx, const FunctionArgList &args) {
  315. SmallVector<CanQualType, 16> argTypes;
  316. for (auto &arg : args)
  317. argTypes.push_back(ctx.getCanonicalParamType(arg->getType()));
  318. return argTypes;
  319. }
  320. static llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16>
  321. getExtParameterInfosForCall(const FunctionProtoType *proto,
  322. unsigned prefixArgs, unsigned totalArgs) {
  323. llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> result;
  324. if (proto->hasExtParameterInfos()) {
  325. addExtParameterInfosForCall(result, proto, prefixArgs, totalArgs);
  326. }
  327. return result;
  328. }
  329. /// Arrange a call to a C++ method, passing the given arguments.
  330. ///
  331. /// ExtraPrefixArgs is the number of ABI-specific args passed after the `this`
  332. /// parameter.
  333. /// ExtraSuffixArgs is the number of ABI-specific args passed at the end of
  334. /// args.
  335. /// PassProtoArgs indicates whether `args` has args for the parameters in the
  336. /// given CXXConstructorDecl.
  337. const CGFunctionInfo &
  338. CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args,
  339. const CXXConstructorDecl *D,
  340. CXXCtorType CtorKind,
  341. unsigned ExtraPrefixArgs,
  342. unsigned ExtraSuffixArgs,
  343. bool PassProtoArgs) {
  344. // FIXME: Kill copy.
  345. SmallVector<CanQualType, 16> ArgTypes;
  346. for (const auto &Arg : args)
  347. ArgTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
  348. // +1 for implicit this, which should always be args[0].
  349. unsigned TotalPrefixArgs = 1 + ExtraPrefixArgs;
  350. CanQual<FunctionProtoType> FPT = GetFormalType(D);
  351. RequiredArgs Required =
  352. RequiredArgs::forPrototypePlus(FPT, TotalPrefixArgs + ExtraSuffixArgs, D);
  353. GlobalDecl GD(D, CtorKind);
  354. CanQualType ResultType = TheCXXABI.HasThisReturn(GD)
  355. ? ArgTypes.front()
  356. : TheCXXABI.hasMostDerivedReturn(GD)
  357. ? CGM.getContext().VoidPtrTy
  358. : Context.VoidTy;
  359. FunctionType::ExtInfo Info = FPT->getExtInfo();
  360. llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> ParamInfos;
  361. // If the prototype args are elided, we should only have ABI-specific args,
  362. // which never have param info.
  363. if (PassProtoArgs && FPT->hasExtParameterInfos()) {
  364. // ABI-specific suffix arguments are treated the same as variadic arguments.
  365. addExtParameterInfosForCall(ParamInfos, FPT.getTypePtr(), TotalPrefixArgs,
  366. ArgTypes.size());
  367. }
  368. return arrangeLLVMFunctionInfo(ResultType, /*instanceMethod=*/true,
  369. /*chainCall=*/false, ArgTypes, Info,
  370. ParamInfos, Required);
  371. }
  372. /// Arrange the argument and result information for the declaration or
  373. /// definition of the given function.
  374. const CGFunctionInfo &
  375. CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) {
  376. if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
  377. if (MD->isInstance())
  378. return arrangeCXXMethodDeclaration(MD);
  379. CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
  380. assert(isa<FunctionType>(FTy));
  381. setCUDAKernelCallingConvention(FTy, CGM, FD);
  382. // When declaring a function without a prototype, always use a
  383. // non-variadic type.
  384. if (CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>()) {
  385. return arrangeLLVMFunctionInfo(
  386. noProto->getReturnType(), /*instanceMethod=*/false,
  387. /*chainCall=*/false, None, noProto->getExtInfo(), {},RequiredArgs::All);
  388. }
  389. return arrangeFreeFunctionType(FTy.castAs<FunctionProtoType>(), FD);
  390. }
  391. /// Arrange the argument and result information for the declaration or
  392. /// definition of an Objective-C method.
  393. const CGFunctionInfo &
  394. CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) {
  395. // It happens that this is the same as a call with no optional
  396. // arguments, except also using the formal 'self' type.
  397. return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType());
  398. }
  399. /// Arrange the argument and result information for the function type
  400. /// through which to perform a send to the given Objective-C method,
  401. /// using the given receiver type. The receiver type is not always
  402. /// the 'self' type of the method or even an Objective-C pointer type.
  403. /// This is *not* the right method for actually performing such a
  404. /// message send, due to the possibility of optional arguments.
  405. const CGFunctionInfo &
  406. CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
  407. QualType receiverType) {
  408. SmallVector<CanQualType, 16> argTys;
  409. SmallVector<FunctionProtoType::ExtParameterInfo, 4> extParamInfos(2);
  410. argTys.push_back(Context.getCanonicalParamType(receiverType));
  411. argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
  412. // FIXME: Kill copy?
  413. for (const auto *I : MD->parameters()) {
  414. argTys.push_back(Context.getCanonicalParamType(I->getType()));
  415. auto extParamInfo = FunctionProtoType::ExtParameterInfo().withIsNoEscape(
  416. I->hasAttr<NoEscapeAttr>());
  417. extParamInfos.push_back(extParamInfo);
  418. }
  419. FunctionType::ExtInfo einfo;
  420. bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows();
  421. einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows));
  422. if (getContext().getLangOpts().ObjCAutoRefCount &&
  423. MD->hasAttr<NSReturnsRetainedAttr>())
  424. einfo = einfo.withProducesResult(true);
  425. RequiredArgs required =
  426. (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All);
  427. return arrangeLLVMFunctionInfo(
  428. GetReturnType(MD->getReturnType()), /*instanceMethod=*/false,
  429. /*chainCall=*/false, argTys, einfo, extParamInfos, required);
  430. }
  431. const CGFunctionInfo &
  432. CodeGenTypes::arrangeUnprototypedObjCMessageSend(QualType returnType,
  433. const CallArgList &args) {
  434. auto argTypes = getArgTypesForCall(Context, args);
  435. FunctionType::ExtInfo einfo;
  436. return arrangeLLVMFunctionInfo(
  437. GetReturnType(returnType), /*instanceMethod=*/false,
  438. /*chainCall=*/false, argTypes, einfo, {}, RequiredArgs::All);
  439. }
  440. const CGFunctionInfo &
  441. CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) {
  442. // FIXME: Do we need to handle ObjCMethodDecl?
  443. const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
  444. if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
  445. return arrangeCXXStructorDeclaration(CD, getFromCtorType(GD.getCtorType()));
  446. if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
  447. return arrangeCXXStructorDeclaration(DD, getFromDtorType(GD.getDtorType()));
  448. return arrangeFunctionDeclaration(FD);
  449. }
  450. /// Arrange a thunk that takes 'this' as the first parameter followed by
  451. /// varargs. Return a void pointer, regardless of the actual return type.
  452. /// The body of the thunk will end in a musttail call to a function of the
  453. /// correct type, and the caller will bitcast the function to the correct
  454. /// prototype.
  455. const CGFunctionInfo &
  456. CodeGenTypes::arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD) {
  457. assert(MD->isVirtual() && "only methods have thunks");
  458. CanQual<FunctionProtoType> FTP = GetFormalType(MD);
  459. CanQualType ArgTys[] = { GetThisType(Context, MD->getParent()) };
  460. return arrangeLLVMFunctionInfo(Context.VoidTy, /*instanceMethod=*/false,
  461. /*chainCall=*/false, ArgTys,
  462. FTP->getExtInfo(), {}, RequiredArgs(1));
  463. }
  464. const CGFunctionInfo &
  465. CodeGenTypes::arrangeMSCtorClosure(const CXXConstructorDecl *CD,
  466. CXXCtorType CT) {
  467. assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure);
  468. CanQual<FunctionProtoType> FTP = GetFormalType(CD);
  469. SmallVector<CanQualType, 2> ArgTys;
  470. const CXXRecordDecl *RD = CD->getParent();
  471. ArgTys.push_back(GetThisType(Context, RD));
  472. if (CT == Ctor_CopyingClosure)
  473. ArgTys.push_back(*FTP->param_type_begin());
  474. if (RD->getNumVBases() > 0)
  475. ArgTys.push_back(Context.IntTy);
  476. CallingConv CC = Context.getDefaultCallingConvention(
  477. /*IsVariadic=*/false, /*IsCXXMethod=*/true);
  478. return arrangeLLVMFunctionInfo(Context.VoidTy, /*instanceMethod=*/true,
  479. /*chainCall=*/false, ArgTys,
  480. FunctionType::ExtInfo(CC), {},
  481. RequiredArgs::All);
  482. }
  483. /// Arrange a call as unto a free function, except possibly with an
  484. /// additional number of formal parameters considered required.
  485. static const CGFunctionInfo &
  486. arrangeFreeFunctionLikeCall(CodeGenTypes &CGT,
  487. CodeGenModule &CGM,
  488. const CallArgList &args,
  489. const FunctionType *fnType,
  490. unsigned numExtraRequiredArgs,
  491. bool chainCall) {
  492. assert(args.size() >= numExtraRequiredArgs);
  493. llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
  494. // In most cases, there are no optional arguments.
  495. RequiredArgs required = RequiredArgs::All;
  496. // If we have a variadic prototype, the required arguments are the
  497. // extra prefix plus the arguments in the prototype.
  498. if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) {
  499. if (proto->isVariadic())
  500. required = RequiredArgs(proto->getNumParams() + numExtraRequiredArgs);
  501. if (proto->hasExtParameterInfos())
  502. addExtParameterInfosForCall(paramInfos, proto, numExtraRequiredArgs,
  503. args.size());
  504. // If we don't have a prototype at all, but we're supposed to
  505. // explicitly use the variadic convention for unprototyped calls,
  506. // treat all of the arguments as required but preserve the nominal
  507. // possibility of variadics.
  508. } else if (CGM.getTargetCodeGenInfo()
  509. .isNoProtoCallVariadic(args,
  510. cast<FunctionNoProtoType>(fnType))) {
  511. required = RequiredArgs(args.size());
  512. }
  513. // FIXME: Kill copy.
  514. SmallVector<CanQualType, 16> argTypes;
  515. for (const auto &arg : args)
  516. argTypes.push_back(CGT.getContext().getCanonicalParamType(arg.Ty));
  517. return CGT.arrangeLLVMFunctionInfo(GetReturnType(fnType->getReturnType()),
  518. /*instanceMethod=*/false, chainCall,
  519. argTypes, fnType->getExtInfo(), paramInfos,
  520. required);
  521. }
  522. /// Figure out the rules for calling a function with the given formal
  523. /// type using the given arguments. The arguments are necessary
  524. /// because the function might be unprototyped, in which case it's
  525. /// target-dependent in crazy ways.
  526. const CGFunctionInfo &
  527. CodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args,
  528. const FunctionType *fnType,
  529. bool chainCall) {
  530. return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType,
  531. chainCall ? 1 : 0, chainCall);
  532. }
  533. /// A block function is essentially a free function with an
  534. /// extra implicit argument.
  535. const CGFunctionInfo &
  536. CodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args,
  537. const FunctionType *fnType) {
  538. return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1,
  539. /*chainCall=*/false);
  540. }
  541. const CGFunctionInfo &
  542. CodeGenTypes::arrangeBlockFunctionDeclaration(const FunctionProtoType *proto,
  543. const FunctionArgList &params) {
  544. auto paramInfos = getExtParameterInfosForCall(proto, 1, params.size());
  545. auto argTypes = getArgTypesForDeclaration(Context, params);
  546. return arrangeLLVMFunctionInfo(
  547. GetReturnType(proto->getReturnType()),
  548. /*instanceMethod*/ false, /*chainCall*/ false, argTypes,
  549. proto->getExtInfo(), paramInfos,
  550. RequiredArgs::forPrototypePlus(proto, 1, nullptr));
  551. }
  552. const CGFunctionInfo &
  553. CodeGenTypes::arrangeBuiltinFunctionCall(QualType resultType,
  554. const CallArgList &args) {
  555. // FIXME: Kill copy.
  556. SmallVector<CanQualType, 16> argTypes;
  557. for (const auto &Arg : args)
  558. argTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
  559. return arrangeLLVMFunctionInfo(
  560. GetReturnType(resultType), /*instanceMethod=*/false,
  561. /*chainCall=*/false, argTypes, FunctionType::ExtInfo(),
  562. /*paramInfos=*/ {}, RequiredArgs::All);
  563. }
  564. const CGFunctionInfo &
  565. CodeGenTypes::arrangeBuiltinFunctionDeclaration(QualType resultType,
  566. const FunctionArgList &args) {
  567. auto argTypes = getArgTypesForDeclaration(Context, args);
  568. return arrangeLLVMFunctionInfo(
  569. GetReturnType(resultType), /*instanceMethod=*/false, /*chainCall=*/false,
  570. argTypes, FunctionType::ExtInfo(), {}, RequiredArgs::All);
  571. }
  572. const CGFunctionInfo &
  573. CodeGenTypes::arrangeBuiltinFunctionDeclaration(CanQualType resultType,
  574. ArrayRef<CanQualType> argTypes) {
  575. return arrangeLLVMFunctionInfo(
  576. resultType, /*instanceMethod=*/false, /*chainCall=*/false,
  577. argTypes, FunctionType::ExtInfo(), {}, RequiredArgs::All);
  578. }
  579. /// Arrange a call to a C++ method, passing the given arguments.
  580. ///
  581. /// numPrefixArgs is the number of ABI-specific prefix arguments we have. It
  582. /// does not count `this`.
  583. const CGFunctionInfo &
  584. CodeGenTypes::arrangeCXXMethodCall(const CallArgList &args,
  585. const FunctionProtoType *proto,
  586. RequiredArgs required,
  587. unsigned numPrefixArgs) {
  588. assert(numPrefixArgs + 1 <= args.size() &&
  589. "Emitting a call with less args than the required prefix?");
  590. // Add one to account for `this`. It's a bit awkward here, but we don't count
  591. // `this` in similar places elsewhere.
  592. auto paramInfos =
  593. getExtParameterInfosForCall(proto, numPrefixArgs + 1, args.size());
  594. // FIXME: Kill copy.
  595. auto argTypes = getArgTypesForCall(Context, args);
  596. FunctionType::ExtInfo info = proto->getExtInfo();
  597. return arrangeLLVMFunctionInfo(
  598. GetReturnType(proto->getReturnType()), /*instanceMethod=*/true,
  599. /*chainCall=*/false, argTypes, info, paramInfos, required);
  600. }
  601. const CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() {
  602. return arrangeLLVMFunctionInfo(
  603. getContext().VoidTy, /*instanceMethod=*/false, /*chainCall=*/false,
  604. None, FunctionType::ExtInfo(), {}, RequiredArgs::All);
  605. }
  606. const CGFunctionInfo &
  607. CodeGenTypes::arrangeCall(const CGFunctionInfo &signature,
  608. const CallArgList &args) {
  609. assert(signature.arg_size() <= args.size());
  610. if (signature.arg_size() == args.size())
  611. return signature;
  612. SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
  613. auto sigParamInfos = signature.getExtParameterInfos();
  614. if (!sigParamInfos.empty()) {
  615. paramInfos.append(sigParamInfos.begin(), sigParamInfos.end());
  616. paramInfos.resize(args.size());
  617. }
  618. auto argTypes = getArgTypesForCall(Context, args);
  619. assert(signature.getRequiredArgs().allowsOptionalArgs());
  620. return arrangeLLVMFunctionInfo(signature.getReturnType(),
  621. signature.isInstanceMethod(),
  622. signature.isChainCall(),
  623. argTypes,
  624. signature.getExtInfo(),
  625. paramInfos,
  626. signature.getRequiredArgs());
  627. }
  628. namespace clang {
  629. namespace CodeGen {
  630. void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI);
  631. }
  632. }
  633. /// Arrange the argument and result information for an abstract value
  634. /// of a given function type. This is the method which all of the
  635. /// above functions ultimately defer to.
  636. const CGFunctionInfo &
  637. CodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType,
  638. bool instanceMethod,
  639. bool chainCall,
  640. ArrayRef<CanQualType> argTypes,
  641. FunctionType::ExtInfo info,
  642. ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
  643. RequiredArgs required) {
  644. assert(llvm::all_of(argTypes,
  645. [](CanQualType T) { return T.isCanonicalAsParam(); }));
  646. // Lookup or create unique function info.
  647. llvm::FoldingSetNodeID ID;
  648. CGFunctionInfo::Profile(ID, instanceMethod, chainCall, info, paramInfos,
  649. required, resultType, argTypes);
  650. void *insertPos = nullptr;
  651. CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos);
  652. if (FI)
  653. return *FI;
  654. unsigned CC = ClangCallConvToLLVMCallConv(info.getCC());
  655. // Construct the function info. We co-allocate the ArgInfos.
  656. FI = CGFunctionInfo::create(CC, instanceMethod, chainCall, info,
  657. paramInfos, resultType, argTypes, required);
  658. FunctionInfos.InsertNode(FI, insertPos);
  659. bool inserted = FunctionsBeingProcessed.insert(FI).second;
  660. (void)inserted;
  661. assert(inserted && "Recursively being processed?");
  662. // Compute ABI information.
  663. if (CC == llvm::CallingConv::SPIR_KERNEL) {
  664. // Force target independent argument handling for the host visible
  665. // kernel functions.
  666. computeSPIRKernelABIInfo(CGM, *FI);
  667. } else if (info.getCC() == CC_Swift) {
  668. swiftcall::computeABIInfo(CGM, *FI);
  669. } else {
  670. getABIInfo().computeInfo(*FI);
  671. }
  672. // Loop over all of the computed argument and return value info. If any of
  673. // them are direct or extend without a specified coerce type, specify the
  674. // default now.
  675. ABIArgInfo &retInfo = FI->getReturnInfo();
  676. if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == nullptr)
  677. retInfo.setCoerceToType(ConvertType(FI->getReturnType()));
  678. for (auto &I : FI->arguments())
  679. if (I.info.canHaveCoerceToType() && I.info.getCoerceToType() == nullptr)
  680. I.info.setCoerceToType(ConvertType(I.type));
  681. bool erased = FunctionsBeingProcessed.erase(FI); (void)erased;
  682. assert(erased && "Not in set?");
  683. return *FI;
  684. }
  685. CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC,
  686. bool instanceMethod,
  687. bool chainCall,
  688. const FunctionType::ExtInfo &info,
  689. ArrayRef<ExtParameterInfo> paramInfos,
  690. CanQualType resultType,
  691. ArrayRef<CanQualType> argTypes,
  692. RequiredArgs required) {
  693. assert(paramInfos.empty() || paramInfos.size() == argTypes.size());
  694. void *buffer =
  695. operator new(totalSizeToAlloc<ArgInfo, ExtParameterInfo>(
  696. argTypes.size() + 1, paramInfos.size()));
  697. CGFunctionInfo *FI = new(buffer) CGFunctionInfo();
  698. FI->CallingConvention = llvmCC;
  699. FI->EffectiveCallingConvention = llvmCC;
  700. FI->ASTCallingConvention = info.getCC();
  701. FI->InstanceMethod = instanceMethod;
  702. FI->ChainCall = chainCall;
  703. FI->NoReturn = info.getNoReturn();
  704. FI->ReturnsRetained = info.getProducesResult();
  705. FI->NoCallerSavedRegs = info.getNoCallerSavedRegs();
  706. FI->NoCfCheck = info.getNoCfCheck();
  707. FI->Required = required;
  708. FI->HasRegParm = info.getHasRegParm();
  709. FI->RegParm = info.getRegParm();
  710. FI->ArgStruct = nullptr;
  711. FI->ArgStructAlign = 0;
  712. FI->NumArgs = argTypes.size();
  713. FI->HasExtParameterInfos = !paramInfos.empty();
  714. FI->getArgsBuffer()[0].type = resultType;
  715. for (unsigned i = 0, e = argTypes.size(); i != e; ++i)
  716. FI->getArgsBuffer()[i + 1].type = argTypes[i];
  717. for (unsigned i = 0, e = paramInfos.size(); i != e; ++i)
  718. FI->getExtParameterInfosBuffer()[i] = paramInfos[i];
  719. return FI;
  720. }
  721. /***/
  722. namespace {
  723. // ABIArgInfo::Expand implementation.
  724. // Specifies the way QualType passed as ABIArgInfo::Expand is expanded.
  725. struct TypeExpansion {
  726. enum TypeExpansionKind {
  727. // Elements of constant arrays are expanded recursively.
  728. TEK_ConstantArray,
  729. // Record fields are expanded recursively (but if record is a union, only
  730. // the field with the largest size is expanded).
  731. TEK_Record,
  732. // For complex types, real and imaginary parts are expanded recursively.
  733. TEK_Complex,
  734. // All other types are not expandable.
  735. TEK_None
  736. };
  737. const TypeExpansionKind Kind;
  738. TypeExpansion(TypeExpansionKind K) : Kind(K) {}
  739. virtual ~TypeExpansion() {}
  740. };
  741. struct ConstantArrayExpansion : TypeExpansion {
  742. QualType EltTy;
  743. uint64_t NumElts;
  744. ConstantArrayExpansion(QualType EltTy, uint64_t NumElts)
  745. : TypeExpansion(TEK_ConstantArray), EltTy(EltTy), NumElts(NumElts) {}
  746. static bool classof(const TypeExpansion *TE) {
  747. return TE->Kind == TEK_ConstantArray;
  748. }
  749. };
  750. struct RecordExpansion : TypeExpansion {
  751. SmallVector<const CXXBaseSpecifier *, 1> Bases;
  752. SmallVector<const FieldDecl *, 1> Fields;
  753. RecordExpansion(SmallVector<const CXXBaseSpecifier *, 1> &&Bases,
  754. SmallVector<const FieldDecl *, 1> &&Fields)
  755. : TypeExpansion(TEK_Record), Bases(std::move(Bases)),
  756. Fields(std::move(Fields)) {}
  757. static bool classof(const TypeExpansion *TE) {
  758. return TE->Kind == TEK_Record;
  759. }
  760. };
  761. struct ComplexExpansion : TypeExpansion {
  762. QualType EltTy;
  763. ComplexExpansion(QualType EltTy) : TypeExpansion(TEK_Complex), EltTy(EltTy) {}
  764. static bool classof(const TypeExpansion *TE) {
  765. return TE->Kind == TEK_Complex;
  766. }
  767. };
  768. struct NoExpansion : TypeExpansion {
  769. NoExpansion() : TypeExpansion(TEK_None) {}
  770. static bool classof(const TypeExpansion *TE) {
  771. return TE->Kind == TEK_None;
  772. }
  773. };
  774. } // namespace
  775. static std::unique_ptr<TypeExpansion>
  776. getTypeExpansion(QualType Ty, const ASTContext &Context) {
  777. if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
  778. return llvm::make_unique<ConstantArrayExpansion>(
  779. AT->getElementType(), AT->getSize().getZExtValue());
  780. }
  781. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  782. SmallVector<const CXXBaseSpecifier *, 1> Bases;
  783. SmallVector<const FieldDecl *, 1> Fields;
  784. const RecordDecl *RD = RT->getDecl();
  785. assert(!RD->hasFlexibleArrayMember() &&
  786. "Cannot expand structure with flexible array.");
  787. if (RD->isUnion()) {
  788. // Unions can be here only in degenerative cases - all the fields are same
  789. // after flattening. Thus we have to use the "largest" field.
  790. const FieldDecl *LargestFD = nullptr;
  791. CharUnits UnionSize = CharUnits::Zero();
  792. for (const auto *FD : RD->fields()) {
  793. if (FD->isZeroLengthBitField(Context))
  794. continue;
  795. assert(!FD->isBitField() &&
  796. "Cannot expand structure with bit-field members.");
  797. CharUnits FieldSize = Context.getTypeSizeInChars(FD->getType());
  798. if (UnionSize < FieldSize) {
  799. UnionSize = FieldSize;
  800. LargestFD = FD;
  801. }
  802. }
  803. if (LargestFD)
  804. Fields.push_back(LargestFD);
  805. } else {
  806. if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  807. assert(!CXXRD->isDynamicClass() &&
  808. "cannot expand vtable pointers in dynamic classes");
  809. for (const CXXBaseSpecifier &BS : CXXRD->bases())
  810. Bases.push_back(&BS);
  811. }
  812. for (const auto *FD : RD->fields()) {
  813. if (FD->isZeroLengthBitField(Context))
  814. continue;
  815. assert(!FD->isBitField() &&
  816. "Cannot expand structure with bit-field members.");
  817. Fields.push_back(FD);
  818. }
  819. }
  820. return llvm::make_unique<RecordExpansion>(std::move(Bases),
  821. std::move(Fields));
  822. }
  823. if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
  824. return llvm::make_unique<ComplexExpansion>(CT->getElementType());
  825. }
  826. return llvm::make_unique<NoExpansion>();
  827. }
  828. static int getExpansionSize(QualType Ty, const ASTContext &Context) {
  829. auto Exp = getTypeExpansion(Ty, Context);
  830. if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
  831. return CAExp->NumElts * getExpansionSize(CAExp->EltTy, Context);
  832. }
  833. if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
  834. int Res = 0;
  835. for (auto BS : RExp->Bases)
  836. Res += getExpansionSize(BS->getType(), Context);
  837. for (auto FD : RExp->Fields)
  838. Res += getExpansionSize(FD->getType(), Context);
  839. return Res;
  840. }
  841. if (isa<ComplexExpansion>(Exp.get()))
  842. return 2;
  843. assert(isa<NoExpansion>(Exp.get()));
  844. return 1;
  845. }
  846. void
  847. CodeGenTypes::getExpandedTypes(QualType Ty,
  848. SmallVectorImpl<llvm::Type *>::iterator &TI) {
  849. auto Exp = getTypeExpansion(Ty, Context);
  850. if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
  851. for (int i = 0, n = CAExp->NumElts; i < n; i++) {
  852. getExpandedTypes(CAExp->EltTy, TI);
  853. }
  854. } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
  855. for (auto BS : RExp->Bases)
  856. getExpandedTypes(BS->getType(), TI);
  857. for (auto FD : RExp->Fields)
  858. getExpandedTypes(FD->getType(), TI);
  859. } else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) {
  860. llvm::Type *EltTy = ConvertType(CExp->EltTy);
  861. *TI++ = EltTy;
  862. *TI++ = EltTy;
  863. } else {
  864. assert(isa<NoExpansion>(Exp.get()));
  865. *TI++ = ConvertType(Ty);
  866. }
  867. }
  868. static void forConstantArrayExpansion(CodeGenFunction &CGF,
  869. ConstantArrayExpansion *CAE,
  870. Address BaseAddr,
  871. llvm::function_ref<void(Address)> Fn) {
  872. CharUnits EltSize = CGF.getContext().getTypeSizeInChars(CAE->EltTy);
  873. CharUnits EltAlign =
  874. BaseAddr.getAlignment().alignmentOfArrayElement(EltSize);
  875. for (int i = 0, n = CAE->NumElts; i < n; i++) {
  876. llvm::Value *EltAddr =
  877. CGF.Builder.CreateConstGEP2_32(nullptr, BaseAddr.getPointer(), 0, i);
  878. Fn(Address(EltAddr, EltAlign));
  879. }
  880. }
  881. void CodeGenFunction::ExpandTypeFromArgs(
  882. QualType Ty, LValue LV, SmallVectorImpl<llvm::Value *>::iterator &AI) {
  883. assert(LV.isSimple() &&
  884. "Unexpected non-simple lvalue during struct expansion.");
  885. auto Exp = getTypeExpansion(Ty, getContext());
  886. if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
  887. forConstantArrayExpansion(*this, CAExp, LV.getAddress(),
  888. [&](Address EltAddr) {
  889. LValue LV = MakeAddrLValue(EltAddr, CAExp->EltTy);
  890. ExpandTypeFromArgs(CAExp->EltTy, LV, AI);
  891. });
  892. } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
  893. Address This = LV.getAddress();
  894. for (const CXXBaseSpecifier *BS : RExp->Bases) {
  895. // Perform a single step derived-to-base conversion.
  896. Address Base =
  897. GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1,
  898. /*NullCheckValue=*/false, SourceLocation());
  899. LValue SubLV = MakeAddrLValue(Base, BS->getType());
  900. // Recurse onto bases.
  901. ExpandTypeFromArgs(BS->getType(), SubLV, AI);
  902. }
  903. for (auto FD : RExp->Fields) {
  904. // FIXME: What are the right qualifiers here?
  905. LValue SubLV = EmitLValueForFieldInitialization(LV, FD);
  906. ExpandTypeFromArgs(FD->getType(), SubLV, AI);
  907. }
  908. } else if (isa<ComplexExpansion>(Exp.get())) {
  909. auto realValue = *AI++;
  910. auto imagValue = *AI++;
  911. EmitStoreOfComplex(ComplexPairTy(realValue, imagValue), LV, /*init*/ true);
  912. } else {
  913. assert(isa<NoExpansion>(Exp.get()));
  914. EmitStoreThroughLValue(RValue::get(*AI++), LV);
  915. }
  916. }
  917. void CodeGenFunction::ExpandTypeToArgs(
  918. QualType Ty, CallArg Arg, llvm::FunctionType *IRFuncTy,
  919. SmallVectorImpl<llvm::Value *> &IRCallArgs, unsigned &IRCallArgPos) {
  920. auto Exp = getTypeExpansion(Ty, getContext());
  921. if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
  922. Address Addr = Arg.hasLValue() ? Arg.getKnownLValue().getAddress()
  923. : Arg.getKnownRValue().getAggregateAddress();
  924. forConstantArrayExpansion(
  925. *this, CAExp, Addr, [&](Address EltAddr) {
  926. CallArg EltArg = CallArg(
  927. convertTempToRValue(EltAddr, CAExp->EltTy, SourceLocation()),
  928. CAExp->EltTy);
  929. ExpandTypeToArgs(CAExp->EltTy, EltArg, IRFuncTy, IRCallArgs,
  930. IRCallArgPos);
  931. });
  932. } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
  933. Address This = Arg.hasLValue() ? Arg.getKnownLValue().getAddress()
  934. : Arg.getKnownRValue().getAggregateAddress();
  935. for (const CXXBaseSpecifier *BS : RExp->Bases) {
  936. // Perform a single step derived-to-base conversion.
  937. Address Base =
  938. GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1,
  939. /*NullCheckValue=*/false, SourceLocation());
  940. CallArg BaseArg = CallArg(RValue::getAggregate(Base), BS->getType());
  941. // Recurse onto bases.
  942. ExpandTypeToArgs(BS->getType(), BaseArg, IRFuncTy, IRCallArgs,
  943. IRCallArgPos);
  944. }
  945. LValue LV = MakeAddrLValue(This, Ty);
  946. for (auto FD : RExp->Fields) {
  947. CallArg FldArg =
  948. CallArg(EmitRValueForField(LV, FD, SourceLocation()), FD->getType());
  949. ExpandTypeToArgs(FD->getType(), FldArg, IRFuncTy, IRCallArgs,
  950. IRCallArgPos);
  951. }
  952. } else if (isa<ComplexExpansion>(Exp.get())) {
  953. ComplexPairTy CV = Arg.getKnownRValue().getComplexVal();
  954. IRCallArgs[IRCallArgPos++] = CV.first;
  955. IRCallArgs[IRCallArgPos++] = CV.second;
  956. } else {
  957. assert(isa<NoExpansion>(Exp.get()));
  958. auto RV = Arg.getKnownRValue();
  959. assert(RV.isScalar() &&
  960. "Unexpected non-scalar rvalue during struct expansion.");
  961. // Insert a bitcast as needed.
  962. llvm::Value *V = RV.getScalarVal();
  963. if (IRCallArgPos < IRFuncTy->getNumParams() &&
  964. V->getType() != IRFuncTy->getParamType(IRCallArgPos))
  965. V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRCallArgPos));
  966. IRCallArgs[IRCallArgPos++] = V;
  967. }
  968. }
  969. /// Create a temporary allocation for the purposes of coercion.
  970. static Address CreateTempAllocaForCoercion(CodeGenFunction &CGF, llvm::Type *Ty,
  971. CharUnits MinAlign) {
  972. // Don't use an alignment that's worse than what LLVM would prefer.
  973. auto PrefAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(Ty);
  974. CharUnits Align = std::max(MinAlign, CharUnits::fromQuantity(PrefAlign));
  975. return CGF.CreateTempAlloca(Ty, Align);
  976. }
  977. /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
  978. /// accessing some number of bytes out of it, try to gep into the struct to get
  979. /// at its inner goodness. Dive as deep as possible without entering an element
  980. /// with an in-memory size smaller than DstSize.
  981. static Address
  982. EnterStructPointerForCoercedAccess(Address SrcPtr,
  983. llvm::StructType *SrcSTy,
  984. uint64_t DstSize, CodeGenFunction &CGF) {
  985. // We can't dive into a zero-element struct.
  986. if (SrcSTy->getNumElements() == 0) return SrcPtr;
  987. llvm::Type *FirstElt = SrcSTy->getElementType(0);
  988. // If the first elt is at least as large as what we're looking for, or if the
  989. // first element is the same size as the whole struct, we can enter it. The
  990. // comparison must be made on the store size and not the alloca size. Using
  991. // the alloca size may overstate the size of the load.
  992. uint64_t FirstEltSize =
  993. CGF.CGM.getDataLayout().getTypeStoreSize(FirstElt);
  994. if (FirstEltSize < DstSize &&
  995. FirstEltSize < CGF.CGM.getDataLayout().getTypeStoreSize(SrcSTy))
  996. return SrcPtr;
  997. // GEP into the first element.
  998. SrcPtr = CGF.Builder.CreateStructGEP(SrcPtr, 0, CharUnits(), "coerce.dive");
  999. // If the first element is a struct, recurse.
  1000. llvm::Type *SrcTy = SrcPtr.getElementType();
  1001. if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
  1002. return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
  1003. return SrcPtr;
  1004. }
  1005. /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
  1006. /// are either integers or pointers. This does a truncation of the value if it
  1007. /// is too large or a zero extension if it is too small.
  1008. ///
  1009. /// This behaves as if the value were coerced through memory, so on big-endian
  1010. /// targets the high bits are preserved in a truncation, while little-endian
  1011. /// targets preserve the low bits.
  1012. static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
  1013. llvm::Type *Ty,
  1014. CodeGenFunction &CGF) {
  1015. if (Val->getType() == Ty)
  1016. return Val;
  1017. if (isa<llvm::PointerType>(Val->getType())) {
  1018. // If this is Pointer->Pointer avoid conversion to and from int.
  1019. if (isa<llvm::PointerType>(Ty))
  1020. return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
  1021. // Convert the pointer to an integer so we can play with its width.
  1022. Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
  1023. }
  1024. llvm::Type *DestIntTy = Ty;
  1025. if (isa<llvm::PointerType>(DestIntTy))
  1026. DestIntTy = CGF.IntPtrTy;
  1027. if (Val->getType() != DestIntTy) {
  1028. const llvm::DataLayout &DL = CGF.CGM.getDataLayout();
  1029. if (DL.isBigEndian()) {
  1030. // Preserve the high bits on big-endian targets.
  1031. // That is what memory coercion does.
  1032. uint64_t SrcSize = DL.getTypeSizeInBits(Val->getType());
  1033. uint64_t DstSize = DL.getTypeSizeInBits(DestIntTy);
  1034. if (SrcSize > DstSize) {
  1035. Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits");
  1036. Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii");
  1037. } else {
  1038. Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii");
  1039. Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits");
  1040. }
  1041. } else {
  1042. // Little-endian targets preserve the low bits. No shifts required.
  1043. Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
  1044. }
  1045. }
  1046. if (isa<llvm::PointerType>(Ty))
  1047. Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
  1048. return Val;
  1049. }
  1050. /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
  1051. /// a pointer to an object of type \arg Ty, known to be aligned to
  1052. /// \arg SrcAlign bytes.
  1053. ///
  1054. /// This safely handles the case when the src type is smaller than the
  1055. /// destination type; in this situation the values of bits which not
  1056. /// present in the src are undefined.
  1057. static llvm::Value *CreateCoercedLoad(Address Src, llvm::Type *Ty,
  1058. CodeGenFunction &CGF) {
  1059. llvm::Type *SrcTy = Src.getElementType();
  1060. // If SrcTy and Ty are the same, just do a load.
  1061. if (SrcTy == Ty)
  1062. return CGF.Builder.CreateLoad(Src);
  1063. uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty);
  1064. if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
  1065. Src = EnterStructPointerForCoercedAccess(Src, SrcSTy, DstSize, CGF);
  1066. SrcTy = Src.getType()->getElementType();
  1067. }
  1068. uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
  1069. // If the source and destination are integer or pointer types, just do an
  1070. // extension or truncation to the desired type.
  1071. if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
  1072. (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
  1073. llvm::Value *Load = CGF.Builder.CreateLoad(Src);
  1074. return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
  1075. }
  1076. // If load is legal, just bitcast the src pointer.
  1077. if (SrcSize >= DstSize) {
  1078. // Generally SrcSize is never greater than DstSize, since this means we are
  1079. // losing bits. However, this can happen in cases where the structure has
  1080. // additional padding, for example due to a user specified alignment.
  1081. //
  1082. // FIXME: Assert that we aren't truncating non-padding bits when have access
  1083. // to that information.
  1084. Src = CGF.Builder.CreateBitCast(Src,
  1085. Ty->getPointerTo(Src.getAddressSpace()));
  1086. return CGF.Builder.CreateLoad(Src);
  1087. }
  1088. // Otherwise do coercion through memory. This is stupid, but simple.
  1089. Address Tmp = CreateTempAllocaForCoercion(CGF, Ty, Src.getAlignment());
  1090. Address Casted = CGF.Builder.CreateElementBitCast(Tmp,CGF.Int8Ty);
  1091. Address SrcCasted = CGF.Builder.CreateElementBitCast(Src,CGF.Int8Ty);
  1092. CGF.Builder.CreateMemCpy(Casted, SrcCasted,
  1093. llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize),
  1094. false);
  1095. return CGF.Builder.CreateLoad(Tmp);
  1096. }
  1097. // Function to store a first-class aggregate into memory. We prefer to
  1098. // store the elements rather than the aggregate to be more friendly to
  1099. // fast-isel.
  1100. // FIXME: Do we need to recurse here?
  1101. static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val,
  1102. Address Dest, bool DestIsVolatile) {
  1103. // Prefer scalar stores to first-class aggregate stores.
  1104. if (llvm::StructType *STy =
  1105. dyn_cast<llvm::StructType>(Val->getType())) {
  1106. const llvm::StructLayout *Layout =
  1107. CGF.CGM.getDataLayout().getStructLayout(STy);
  1108. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  1109. auto EltOffset = CharUnits::fromQuantity(Layout->getElementOffset(i));
  1110. Address EltPtr = CGF.Builder.CreateStructGEP(Dest, i, EltOffset);
  1111. llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
  1112. CGF.Builder.CreateStore(Elt, EltPtr, DestIsVolatile);
  1113. }
  1114. } else {
  1115. CGF.Builder.CreateStore(Val, Dest, DestIsVolatile);
  1116. }
  1117. }
  1118. /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
  1119. /// where the source and destination may have different types. The
  1120. /// destination is known to be aligned to \arg DstAlign bytes.
  1121. ///
  1122. /// This safely handles the case when the src type is larger than the
  1123. /// destination type; the upper bits of the src will be lost.
  1124. static void CreateCoercedStore(llvm::Value *Src,
  1125. Address Dst,
  1126. bool DstIsVolatile,
  1127. CodeGenFunction &CGF) {
  1128. llvm::Type *SrcTy = Src->getType();
  1129. llvm::Type *DstTy = Dst.getType()->getElementType();
  1130. if (SrcTy == DstTy) {
  1131. CGF.Builder.CreateStore(Src, Dst, DstIsVolatile);
  1132. return;
  1133. }
  1134. uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
  1135. if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
  1136. Dst = EnterStructPointerForCoercedAccess(Dst, DstSTy, SrcSize, CGF);
  1137. DstTy = Dst.getType()->getElementType();
  1138. }
  1139. // If the source and destination are integer or pointer types, just do an
  1140. // extension or truncation to the desired type.
  1141. if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
  1142. (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
  1143. Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
  1144. CGF.Builder.CreateStore(Src, Dst, DstIsVolatile);
  1145. return;
  1146. }
  1147. uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy);
  1148. // If store is legal, just bitcast the src pointer.
  1149. if (SrcSize <= DstSize) {
  1150. Dst = CGF.Builder.CreateElementBitCast(Dst, SrcTy);
  1151. BuildAggStore(CGF, Src, Dst, DstIsVolatile);
  1152. } else {
  1153. // Otherwise do coercion through memory. This is stupid, but
  1154. // simple.
  1155. // Generally SrcSize is never greater than DstSize, since this means we are
  1156. // losing bits. However, this can happen in cases where the structure has
  1157. // additional padding, for example due to a user specified alignment.
  1158. //
  1159. // FIXME: Assert that we aren't truncating non-padding bits when have access
  1160. // to that information.
  1161. Address Tmp = CreateTempAllocaForCoercion(CGF, SrcTy, Dst.getAlignment());
  1162. CGF.Builder.CreateStore(Src, Tmp);
  1163. Address Casted = CGF.Builder.CreateElementBitCast(Tmp,CGF.Int8Ty);
  1164. Address DstCasted = CGF.Builder.CreateElementBitCast(Dst,CGF.Int8Ty);
  1165. CGF.Builder.CreateMemCpy(DstCasted, Casted,
  1166. llvm::ConstantInt::get(CGF.IntPtrTy, DstSize),
  1167. false);
  1168. }
  1169. }
  1170. static Address emitAddressAtOffset(CodeGenFunction &CGF, Address addr,
  1171. const ABIArgInfo &info) {
  1172. if (unsigned offset = info.getDirectOffset()) {
  1173. addr = CGF.Builder.CreateElementBitCast(addr, CGF.Int8Ty);
  1174. addr = CGF.Builder.CreateConstInBoundsByteGEP(addr,
  1175. CharUnits::fromQuantity(offset));
  1176. addr = CGF.Builder.CreateElementBitCast(addr, info.getCoerceToType());
  1177. }
  1178. return addr;
  1179. }
  1180. namespace {
  1181. /// Encapsulates information about the way function arguments from
  1182. /// CGFunctionInfo should be passed to actual LLVM IR function.
  1183. class ClangToLLVMArgMapping {
  1184. static const unsigned InvalidIndex = ~0U;
  1185. unsigned InallocaArgNo;
  1186. unsigned SRetArgNo;
  1187. unsigned TotalIRArgs;
  1188. /// Arguments of LLVM IR function corresponding to single Clang argument.
  1189. struct IRArgs {
  1190. unsigned PaddingArgIndex;
  1191. // Argument is expanded to IR arguments at positions
  1192. // [FirstArgIndex, FirstArgIndex + NumberOfArgs).
  1193. unsigned FirstArgIndex;
  1194. unsigned NumberOfArgs;
  1195. IRArgs()
  1196. : PaddingArgIndex(InvalidIndex), FirstArgIndex(InvalidIndex),
  1197. NumberOfArgs(0) {}
  1198. };
  1199. SmallVector<IRArgs, 8> ArgInfo;
  1200. public:
  1201. ClangToLLVMArgMapping(const ASTContext &Context, const CGFunctionInfo &FI,
  1202. bool OnlyRequiredArgs = false)
  1203. : InallocaArgNo(InvalidIndex), SRetArgNo(InvalidIndex), TotalIRArgs(0),
  1204. ArgInfo(OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size()) {
  1205. construct(Context, FI, OnlyRequiredArgs);
  1206. }
  1207. bool hasInallocaArg() const { return InallocaArgNo != InvalidIndex; }
  1208. unsigned getInallocaArgNo() const {
  1209. assert(hasInallocaArg());
  1210. return InallocaArgNo;
  1211. }
  1212. bool hasSRetArg() const { return SRetArgNo != InvalidIndex; }
  1213. unsigned getSRetArgNo() const {
  1214. assert(hasSRetArg());
  1215. return SRetArgNo;
  1216. }
  1217. unsigned totalIRArgs() const { return TotalIRArgs; }
  1218. bool hasPaddingArg(unsigned ArgNo) const {
  1219. assert(ArgNo < ArgInfo.size());
  1220. return ArgInfo[ArgNo].PaddingArgIndex != InvalidIndex;
  1221. }
  1222. unsigned getPaddingArgNo(unsigned ArgNo) const {
  1223. assert(hasPaddingArg(ArgNo));
  1224. return ArgInfo[ArgNo].PaddingArgIndex;
  1225. }
  1226. /// Returns index of first IR argument corresponding to ArgNo, and their
  1227. /// quantity.
  1228. std::pair<unsigned, unsigned> getIRArgs(unsigned ArgNo) const {
  1229. assert(ArgNo < ArgInfo.size());
  1230. return std::make_pair(ArgInfo[ArgNo].FirstArgIndex,
  1231. ArgInfo[ArgNo].NumberOfArgs);
  1232. }
  1233. private:
  1234. void construct(const ASTContext &Context, const CGFunctionInfo &FI,
  1235. bool OnlyRequiredArgs);
  1236. };
  1237. void ClangToLLVMArgMapping::construct(const ASTContext &Context,
  1238. const CGFunctionInfo &FI,
  1239. bool OnlyRequiredArgs) {
  1240. unsigned IRArgNo = 0;
  1241. bool SwapThisWithSRet = false;
  1242. const ABIArgInfo &RetAI = FI.getReturnInfo();
  1243. if (RetAI.getKind() == ABIArgInfo::Indirect) {
  1244. SwapThisWithSRet = RetAI.isSRetAfterThis();
  1245. SRetArgNo = SwapThisWithSRet ? 1 : IRArgNo++;
  1246. }
  1247. unsigned ArgNo = 0;
  1248. unsigned NumArgs = OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size();
  1249. for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(); ArgNo < NumArgs;
  1250. ++I, ++ArgNo) {
  1251. assert(I != FI.arg_end());
  1252. QualType ArgType = I->type;
  1253. const ABIArgInfo &AI = I->info;
  1254. // Collect data about IR arguments corresponding to Clang argument ArgNo.
  1255. auto &IRArgs = ArgInfo[ArgNo];
  1256. if (AI.getPaddingType())
  1257. IRArgs.PaddingArgIndex = IRArgNo++;
  1258. switch (AI.getKind()) {
  1259. case ABIArgInfo::Extend:
  1260. case ABIArgInfo::Direct: {
  1261. // FIXME: handle sseregparm someday...
  1262. llvm::StructType *STy = dyn_cast<llvm::StructType>(AI.getCoerceToType());
  1263. if (AI.isDirect() && AI.getCanBeFlattened() && STy) {
  1264. IRArgs.NumberOfArgs = STy->getNumElements();
  1265. } else {
  1266. IRArgs.NumberOfArgs = 1;
  1267. }
  1268. break;
  1269. }
  1270. case ABIArgInfo::Indirect:
  1271. IRArgs.NumberOfArgs = 1;
  1272. break;
  1273. case ABIArgInfo::Ignore:
  1274. case ABIArgInfo::InAlloca:
  1275. // ignore and inalloca doesn't have matching LLVM parameters.
  1276. IRArgs.NumberOfArgs = 0;
  1277. break;
  1278. case ABIArgInfo::CoerceAndExpand:
  1279. IRArgs.NumberOfArgs = AI.getCoerceAndExpandTypeSequence().size();
  1280. break;
  1281. case ABIArgInfo::Expand:
  1282. IRArgs.NumberOfArgs = getExpansionSize(ArgType, Context);
  1283. break;
  1284. }
  1285. if (IRArgs.NumberOfArgs > 0) {
  1286. IRArgs.FirstArgIndex = IRArgNo;
  1287. IRArgNo += IRArgs.NumberOfArgs;
  1288. }
  1289. // Skip over the sret parameter when it comes second. We already handled it
  1290. // above.
  1291. if (IRArgNo == 1 && SwapThisWithSRet)
  1292. IRArgNo++;
  1293. }
  1294. assert(ArgNo == ArgInfo.size());
  1295. if (FI.usesInAlloca())
  1296. InallocaArgNo = IRArgNo++;
  1297. TotalIRArgs = IRArgNo;
  1298. }
  1299. } // namespace
  1300. /***/
  1301. bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
  1302. const auto &RI = FI.getReturnInfo();
  1303. return RI.isIndirect() || (RI.isInAlloca() && RI.getInAllocaSRet());
  1304. }
  1305. bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) {
  1306. return ReturnTypeUsesSRet(FI) &&
  1307. getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();
  1308. }
  1309. bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
  1310. if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
  1311. switch (BT->getKind()) {
  1312. default:
  1313. return false;
  1314. case BuiltinType::Float:
  1315. return getTarget().useObjCFPRetForRealType(TargetInfo::Float);
  1316. case BuiltinType::Double:
  1317. return getTarget().useObjCFPRetForRealType(TargetInfo::Double);
  1318. case BuiltinType::LongDouble:
  1319. return getTarget().useObjCFPRetForRealType(TargetInfo::LongDouble);
  1320. }
  1321. }
  1322. return false;
  1323. }
  1324. bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) {
  1325. if (const ComplexType *CT = ResultType->getAs<ComplexType>()) {
  1326. if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) {
  1327. if (BT->getKind() == BuiltinType::LongDouble)
  1328. return getTarget().useObjCFP2RetForComplexLongDouble();
  1329. }
  1330. }
  1331. return false;
  1332. }
  1333. llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
  1334. const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD);
  1335. return GetFunctionType(FI);
  1336. }
  1337. llvm::FunctionType *
  1338. CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
  1339. bool Inserted = FunctionsBeingProcessed.insert(&FI).second;
  1340. (void)Inserted;
  1341. assert(Inserted && "Recursively being processed?");
  1342. llvm::Type *resultType = nullptr;
  1343. const ABIArgInfo &retAI = FI.getReturnInfo();
  1344. switch (retAI.getKind()) {
  1345. case ABIArgInfo::Expand:
  1346. llvm_unreachable("Invalid ABI kind for return argument");
  1347. case ABIArgInfo::Extend:
  1348. case ABIArgInfo::Direct:
  1349. resultType = retAI.getCoerceToType();
  1350. break;
  1351. case ABIArgInfo::InAlloca:
  1352. if (retAI.getInAllocaSRet()) {
  1353. // sret things on win32 aren't void, they return the sret pointer.
  1354. QualType ret = FI.getReturnType();
  1355. llvm::Type *ty = ConvertType(ret);
  1356. unsigned addressSpace = Context.getTargetAddressSpace(ret);
  1357. resultType = llvm::PointerType::get(ty, addressSpace);
  1358. } else {
  1359. resultType = llvm::Type::getVoidTy(getLLVMContext());
  1360. }
  1361. break;
  1362. case ABIArgInfo::Indirect:
  1363. case ABIArgInfo::Ignore:
  1364. resultType = llvm::Type::getVoidTy(getLLVMContext());
  1365. break;
  1366. case ABIArgInfo::CoerceAndExpand:
  1367. resultType = retAI.getUnpaddedCoerceAndExpandType();
  1368. break;
  1369. }
  1370. ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI, true);
  1371. SmallVector<llvm::Type*, 8> ArgTypes(IRFunctionArgs.totalIRArgs());
  1372. // Add type for sret argument.
  1373. if (IRFunctionArgs.hasSRetArg()) {
  1374. QualType Ret = FI.getReturnType();
  1375. llvm::Type *Ty = ConvertType(Ret);
  1376. unsigned AddressSpace = Context.getTargetAddressSpace(Ret);
  1377. ArgTypes[IRFunctionArgs.getSRetArgNo()] =
  1378. llvm::PointerType::get(Ty, AddressSpace);
  1379. }
  1380. // Add type for inalloca argument.
  1381. if (IRFunctionArgs.hasInallocaArg()) {
  1382. auto ArgStruct = FI.getArgStruct();
  1383. assert(ArgStruct);
  1384. ArgTypes[IRFunctionArgs.getInallocaArgNo()] = ArgStruct->getPointerTo();
  1385. }
  1386. // Add in all of the required arguments.
  1387. unsigned ArgNo = 0;
  1388. CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
  1389. ie = it + FI.getNumRequiredArgs();
  1390. for (; it != ie; ++it, ++ArgNo) {
  1391. const ABIArgInfo &ArgInfo = it->info;
  1392. // Insert a padding type to ensure proper alignment.
  1393. if (IRFunctionArgs.hasPaddingArg(ArgNo))
  1394. ArgTypes[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
  1395. ArgInfo.getPaddingType();
  1396. unsigned FirstIRArg, NumIRArgs;
  1397. std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
  1398. switch (ArgInfo.getKind()) {
  1399. case ABIArgInfo::Ignore:
  1400. case ABIArgInfo::InAlloca:
  1401. assert(NumIRArgs == 0);
  1402. break;
  1403. case ABIArgInfo::Indirect: {
  1404. assert(NumIRArgs == 1);
  1405. // indirect arguments are always on the stack, which is alloca addr space.
  1406. llvm::Type *LTy = ConvertTypeForMem(it->type);
  1407. ArgTypes[FirstIRArg] = LTy->getPointerTo(
  1408. CGM.getDataLayout().getAllocaAddrSpace());
  1409. break;
  1410. }
  1411. case ABIArgInfo::Extend:
  1412. case ABIArgInfo::Direct: {
  1413. // Fast-isel and the optimizer generally like scalar values better than
  1414. // FCAs, so we flatten them if this is safe to do for this argument.
  1415. llvm::Type *argType = ArgInfo.getCoerceToType();
  1416. llvm::StructType *st = dyn_cast<llvm::StructType>(argType);
  1417. if (st && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
  1418. assert(NumIRArgs == st->getNumElements());
  1419. for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
  1420. ArgTypes[FirstIRArg + i] = st->getElementType(i);
  1421. } else {
  1422. assert(NumIRArgs == 1);
  1423. ArgTypes[FirstIRArg] = argType;
  1424. }
  1425. break;
  1426. }
  1427. case ABIArgInfo::CoerceAndExpand: {
  1428. auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
  1429. for (auto EltTy : ArgInfo.getCoerceAndExpandTypeSequence()) {
  1430. *ArgTypesIter++ = EltTy;
  1431. }
  1432. assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
  1433. break;
  1434. }
  1435. case ABIArgInfo::Expand:
  1436. auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
  1437. getExpandedTypes(it->type, ArgTypesIter);
  1438. assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
  1439. break;
  1440. }
  1441. }
  1442. bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased;
  1443. assert(Erased && "Not in set?");
  1444. return llvm::FunctionType::get(resultType, ArgTypes, FI.isVariadic());
  1445. }
  1446. llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
  1447. const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
  1448. const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
  1449. if (!isFuncTypeConvertible(FPT))
  1450. return llvm::StructType::get(getLLVMContext());
  1451. const CGFunctionInfo *Info;
  1452. if (isa<CXXDestructorDecl>(MD))
  1453. Info =
  1454. &arrangeCXXStructorDeclaration(MD, getFromDtorType(GD.getDtorType()));
  1455. else
  1456. Info = &arrangeCXXMethodDeclaration(MD);
  1457. return GetFunctionType(*Info);
  1458. }
  1459. static void AddAttributesFromFunctionProtoType(ASTContext &Ctx,
  1460. llvm::AttrBuilder &FuncAttrs,
  1461. const FunctionProtoType *FPT) {
  1462. if (!FPT)
  1463. return;
  1464. if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()) &&
  1465. FPT->isNothrow())
  1466. FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
  1467. }
  1468. void CodeGenModule::ConstructDefaultFnAttrList(StringRef Name, bool HasOptnone,
  1469. bool AttrOnCallSite,
  1470. llvm::AttrBuilder &FuncAttrs) {
  1471. // OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed.
  1472. if (!HasOptnone) {
  1473. if (CodeGenOpts.OptimizeSize)
  1474. FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize);
  1475. if (CodeGenOpts.OptimizeSize == 2)
  1476. FuncAttrs.addAttribute(llvm::Attribute::MinSize);
  1477. }
  1478. if (CodeGenOpts.DisableRedZone)
  1479. FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
  1480. if (CodeGenOpts.IndirectTlsSegRefs)
  1481. FuncAttrs.addAttribute("indirect-tls-seg-refs");
  1482. if (CodeGenOpts.NoImplicitFloat)
  1483. FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
  1484. if (AttrOnCallSite) {
  1485. // Attributes that should go on the call site only.
  1486. if (!CodeGenOpts.SimplifyLibCalls ||
  1487. CodeGenOpts.isNoBuiltinFunc(Name.data()))
  1488. FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin);
  1489. if (!CodeGenOpts.TrapFuncName.empty())
  1490. FuncAttrs.addAttribute("trap-func-name", CodeGenOpts.TrapFuncName);
  1491. } else {
  1492. // Attributes that should go on the function, but not the call site.
  1493. if (!CodeGenOpts.DisableFPElim) {
  1494. FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
  1495. } else if (CodeGenOpts.OmitLeafFramePointer) {
  1496. FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
  1497. FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf");
  1498. } else {
  1499. FuncAttrs.addAttribute("no-frame-pointer-elim", "true");
  1500. FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf");
  1501. }
  1502. FuncAttrs.addAttribute("less-precise-fpmad",
  1503. llvm::toStringRef(CodeGenOpts.LessPreciseFPMAD));
  1504. if (CodeGenOpts.NullPointerIsValid)
  1505. FuncAttrs.addAttribute("null-pointer-is-valid", "true");
  1506. if (!CodeGenOpts.FPDenormalMode.empty())
  1507. FuncAttrs.addAttribute("denormal-fp-math", CodeGenOpts.FPDenormalMode);
  1508. FuncAttrs.addAttribute("no-trapping-math",
  1509. llvm::toStringRef(CodeGenOpts.NoTrappingMath));
  1510. // Strict (compliant) code is the default, so only add this attribute to
  1511. // indicate that we are trying to workaround a problem case.
  1512. if (!CodeGenOpts.StrictFloatCastOverflow)
  1513. FuncAttrs.addAttribute("strict-float-cast-overflow", "false");
  1514. // TODO: Are these all needed?
  1515. // unsafe/inf/nan/nsz are handled by instruction-level FastMathFlags.
  1516. FuncAttrs.addAttribute("no-infs-fp-math",
  1517. llvm::toStringRef(CodeGenOpts.NoInfsFPMath));
  1518. FuncAttrs.addAttribute("no-nans-fp-math",
  1519. llvm::toStringRef(CodeGenOpts.NoNaNsFPMath));
  1520. FuncAttrs.addAttribute("unsafe-fp-math",
  1521. llvm::toStringRef(CodeGenOpts.UnsafeFPMath));
  1522. FuncAttrs.addAttribute("use-soft-float",
  1523. llvm::toStringRef(CodeGenOpts.SoftFloat));
  1524. FuncAttrs.addAttribute("stack-protector-buffer-size",
  1525. llvm::utostr(CodeGenOpts.SSPBufferSize));
  1526. FuncAttrs.addAttribute("no-signed-zeros-fp-math",
  1527. llvm::toStringRef(CodeGenOpts.NoSignedZeros));
  1528. FuncAttrs.addAttribute(
  1529. "correctly-rounded-divide-sqrt-fp-math",
  1530. llvm::toStringRef(CodeGenOpts.CorrectlyRoundedDivSqrt));
  1531. if (getLangOpts().OpenCL)
  1532. FuncAttrs.addAttribute("denorms-are-zero",
  1533. llvm::toStringRef(CodeGenOpts.FlushDenorm));
  1534. // TODO: Reciprocal estimate codegen options should apply to instructions?
  1535. const std::vector<std::string> &Recips = CodeGenOpts.Reciprocals;
  1536. if (!Recips.empty())
  1537. FuncAttrs.addAttribute("reciprocal-estimates",
  1538. llvm::join(Recips, ","));
  1539. if (!CodeGenOpts.PreferVectorWidth.empty() &&
  1540. CodeGenOpts.PreferVectorWidth != "none")
  1541. FuncAttrs.addAttribute("prefer-vector-width",
  1542. CodeGenOpts.PreferVectorWidth);
  1543. if (CodeGenOpts.StackRealignment)
  1544. FuncAttrs.addAttribute("stackrealign");
  1545. if (CodeGenOpts.Backchain)
  1546. FuncAttrs.addAttribute("backchain");
  1547. // FIXME: The interaction of this attribute with the SLH command line flag
  1548. // has not been determined.
  1549. if (CodeGenOpts.SpeculativeLoadHardening)
  1550. FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);
  1551. }
  1552. if (getLangOpts().assumeFunctionsAreConvergent()) {
  1553. // Conservatively, mark all functions and calls in CUDA and OpenCL as
  1554. // convergent (meaning, they may call an intrinsically convergent op, such
  1555. // as __syncthreads() / barrier(), and so can't have certain optimizations
  1556. // applied around them). LLVM will remove this attribute where it safely
  1557. // can.
  1558. FuncAttrs.addAttribute(llvm::Attribute::Convergent);
  1559. }
  1560. if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) {
  1561. // Exceptions aren't supported in CUDA device code.
  1562. FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
  1563. // Respect -fcuda-flush-denormals-to-zero.
  1564. if (CodeGenOpts.FlushDenorm)
  1565. FuncAttrs.addAttribute("nvptx-f32ftz", "true");
  1566. }
  1567. }
  1568. void CodeGenModule::AddDefaultFnAttrs(llvm::Function &F) {
  1569. llvm::AttrBuilder FuncAttrs;
  1570. ConstructDefaultFnAttrList(F.getName(),
  1571. F.hasFnAttribute(llvm::Attribute::OptimizeNone),
  1572. /* AttrOnCallsite = */ false, FuncAttrs);
  1573. F.addAttributes(llvm::AttributeList::FunctionIndex, FuncAttrs);
  1574. }
  1575. void CodeGenModule::ConstructAttributeList(
  1576. StringRef Name, const CGFunctionInfo &FI, CGCalleeInfo CalleeInfo,
  1577. llvm::AttributeList &AttrList, unsigned &CallingConv, bool AttrOnCallSite) {
  1578. llvm::AttrBuilder FuncAttrs;
  1579. llvm::AttrBuilder RetAttrs;
  1580. CallingConv = FI.getEffectiveCallingConvention();
  1581. if (FI.isNoReturn())
  1582. FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
  1583. // If we have information about the function prototype, we can learn
  1584. // attributes from there.
  1585. AddAttributesFromFunctionProtoType(getContext(), FuncAttrs,
  1586. CalleeInfo.getCalleeFunctionProtoType());
  1587. const Decl *TargetDecl = CalleeInfo.getCalleeDecl().getDecl();
  1588. bool HasOptnone = false;
  1589. // FIXME: handle sseregparm someday...
  1590. if (TargetDecl) {
  1591. if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
  1592. FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);
  1593. if (TargetDecl->hasAttr<NoThrowAttr>())
  1594. FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
  1595. if (TargetDecl->hasAttr<NoReturnAttr>())
  1596. FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
  1597. if (TargetDecl->hasAttr<ColdAttr>())
  1598. FuncAttrs.addAttribute(llvm::Attribute::Cold);
  1599. if (TargetDecl->hasAttr<NoDuplicateAttr>())
  1600. FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate);
  1601. if (TargetDecl->hasAttr<ConvergentAttr>())
  1602. FuncAttrs.addAttribute(llvm::Attribute::Convergent);
  1603. if (TargetDecl->hasAttr<SpeculativeLoadHardeningAttr>())
  1604. FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);
  1605. if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
  1606. AddAttributesFromFunctionProtoType(
  1607. getContext(), FuncAttrs, Fn->getType()->getAs<FunctionProtoType>());
  1608. // Don't use [[noreturn]] or _Noreturn for a call to a virtual function.
  1609. // These attributes are not inherited by overloads.
  1610. const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);
  1611. if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual()))
  1612. FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
  1613. }
  1614. // 'const', 'pure' and 'noalias' attributed functions are also nounwind.
  1615. if (TargetDecl->hasAttr<ConstAttr>()) {
  1616. FuncAttrs.addAttribute(llvm::Attribute::ReadNone);
  1617. FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
  1618. } else if (TargetDecl->hasAttr<PureAttr>()) {
  1619. FuncAttrs.addAttribute(llvm::Attribute::ReadOnly);
  1620. FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
  1621. } else if (TargetDecl->hasAttr<NoAliasAttr>()) {
  1622. FuncAttrs.addAttribute(llvm::Attribute::ArgMemOnly);
  1623. FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
  1624. }
  1625. if (TargetDecl->hasAttr<RestrictAttr>())
  1626. RetAttrs.addAttribute(llvm::Attribute::NoAlias);
  1627. if (TargetDecl->hasAttr<ReturnsNonNullAttr>() &&
  1628. !CodeGenOpts.NullPointerIsValid)
  1629. RetAttrs.addAttribute(llvm::Attribute::NonNull);
  1630. if (TargetDecl->hasAttr<AnyX86NoCallerSavedRegistersAttr>())
  1631. FuncAttrs.addAttribute("no_caller_saved_registers");
  1632. if (TargetDecl->hasAttr<AnyX86NoCfCheckAttr>())
  1633. FuncAttrs.addAttribute(llvm::Attribute::NoCfCheck);
  1634. HasOptnone = TargetDecl->hasAttr<OptimizeNoneAttr>();
  1635. if (auto *AllocSize = TargetDecl->getAttr<AllocSizeAttr>()) {
  1636. Optional<unsigned> NumElemsParam;
  1637. if (AllocSize->getNumElemsParam().isValid())
  1638. NumElemsParam = AllocSize->getNumElemsParam().getLLVMIndex();
  1639. FuncAttrs.addAllocSizeAttr(AllocSize->getElemSizeParam().getLLVMIndex(),
  1640. NumElemsParam);
  1641. }
  1642. }
  1643. ConstructDefaultFnAttrList(Name, HasOptnone, AttrOnCallSite, FuncAttrs);
  1644. if (CodeGenOpts.EnableSegmentedStacks &&
  1645. !(TargetDecl && TargetDecl->hasAttr<NoSplitStackAttr>()))
  1646. FuncAttrs.addAttribute("split-stack");
  1647. // Add NonLazyBind attribute to function declarations when -fno-plt
  1648. // is used.
  1649. if (TargetDecl && CodeGenOpts.NoPLT) {
  1650. if (auto *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
  1651. if (!Fn->isDefined() && !AttrOnCallSite) {
  1652. FuncAttrs.addAttribute(llvm::Attribute::NonLazyBind);
  1653. }
  1654. }
  1655. }
  1656. if (TargetDecl && TargetDecl->hasAttr<OpenCLKernelAttr>()) {
  1657. if (getLangOpts().OpenCLVersion <= 120) {
  1658. // OpenCL v1.2 Work groups are always uniform
  1659. FuncAttrs.addAttribute("uniform-work-group-size", "true");
  1660. } else {
  1661. // OpenCL v2.0 Work groups may be whether uniform or not.
  1662. // '-cl-uniform-work-group-size' compile option gets a hint
  1663. // to the compiler that the global work-size be a multiple of
  1664. // the work-group size specified to clEnqueueNDRangeKernel
  1665. // (i.e. work groups are uniform).
  1666. FuncAttrs.addAttribute("uniform-work-group-size",
  1667. llvm::toStringRef(CodeGenOpts.UniformWGSize));
  1668. }
  1669. }
  1670. if (!AttrOnCallSite) {
  1671. bool DisableTailCalls = false;
  1672. if (CodeGenOpts.DisableTailCalls)
  1673. DisableTailCalls = true;
  1674. else if (TargetDecl) {
  1675. if (TargetDecl->hasAttr<DisableTailCallsAttr>() ||
  1676. TargetDecl->hasAttr<AnyX86InterruptAttr>())
  1677. DisableTailCalls = true;
  1678. else if (CodeGenOpts.NoEscapingBlockTailCalls) {
  1679. if (const auto *BD = dyn_cast<BlockDecl>(TargetDecl))
  1680. if (!BD->doesNotEscape())
  1681. DisableTailCalls = true;
  1682. }
  1683. }
  1684. FuncAttrs.addAttribute("disable-tail-calls",
  1685. llvm::toStringRef(DisableTailCalls));
  1686. GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs);
  1687. }
  1688. ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI);
  1689. QualType RetTy = FI.getReturnType();
  1690. const ABIArgInfo &RetAI = FI.getReturnInfo();
  1691. switch (RetAI.getKind()) {
  1692. case ABIArgInfo::Extend:
  1693. if (RetAI.isSignExt())
  1694. RetAttrs.addAttribute(llvm::Attribute::SExt);
  1695. else
  1696. RetAttrs.addAttribute(llvm::Attribute::ZExt);
  1697. LLVM_FALLTHROUGH;
  1698. case ABIArgInfo::Direct:
  1699. if (RetAI.getInReg())
  1700. RetAttrs.addAttribute(llvm::Attribute::InReg);
  1701. break;
  1702. case ABIArgInfo::Ignore:
  1703. break;
  1704. case ABIArgInfo::InAlloca:
  1705. case ABIArgInfo::Indirect: {
  1706. // inalloca and sret disable readnone and readonly
  1707. FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
  1708. .removeAttribute(llvm::Attribute::ReadNone);
  1709. break;
  1710. }
  1711. case ABIArgInfo::CoerceAndExpand:
  1712. break;
  1713. case ABIArgInfo::Expand:
  1714. llvm_unreachable("Invalid ABI kind for return argument");
  1715. }
  1716. if (const auto *RefTy = RetTy->getAs<ReferenceType>()) {
  1717. QualType PTy = RefTy->getPointeeType();
  1718. if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
  1719. RetAttrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy)
  1720. .getQuantity());
  1721. else if (getContext().getTargetAddressSpace(PTy) == 0 &&
  1722. !CodeGenOpts.NullPointerIsValid)
  1723. RetAttrs.addAttribute(llvm::Attribute::NonNull);
  1724. }
  1725. bool hasUsedSRet = false;
  1726. SmallVector<llvm::AttributeSet, 4> ArgAttrs(IRFunctionArgs.totalIRArgs());
  1727. // Attach attributes to sret.
  1728. if (IRFunctionArgs.hasSRetArg()) {
  1729. llvm::AttrBuilder SRETAttrs;
  1730. if (!RetAI.getSuppressSRet())
  1731. SRETAttrs.addAttribute(llvm::Attribute::StructRet);
  1732. hasUsedSRet = true;
  1733. if (RetAI.getInReg())
  1734. SRETAttrs.addAttribute(llvm::Attribute::InReg);
  1735. ArgAttrs[IRFunctionArgs.getSRetArgNo()] =
  1736. llvm::AttributeSet::get(getLLVMContext(), SRETAttrs);
  1737. }
  1738. // Attach attributes to inalloca argument.
  1739. if (IRFunctionArgs.hasInallocaArg()) {
  1740. llvm::AttrBuilder Attrs;
  1741. Attrs.addAttribute(llvm::Attribute::InAlloca);
  1742. ArgAttrs[IRFunctionArgs.getInallocaArgNo()] =
  1743. llvm::AttributeSet::get(getLLVMContext(), Attrs);
  1744. }
  1745. unsigned ArgNo = 0;
  1746. for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(),
  1747. E = FI.arg_end();
  1748. I != E; ++I, ++ArgNo) {
  1749. QualType ParamType = I->type;
  1750. const ABIArgInfo &AI = I->info;
  1751. llvm::AttrBuilder Attrs;
  1752. // Add attribute for padding argument, if necessary.
  1753. if (IRFunctionArgs.hasPaddingArg(ArgNo)) {
  1754. if (AI.getPaddingInReg()) {
  1755. ArgAttrs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
  1756. llvm::AttributeSet::get(
  1757. getLLVMContext(),
  1758. llvm::AttrBuilder().addAttribute(llvm::Attribute::InReg));
  1759. }
  1760. }
  1761. // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
  1762. // have the corresponding parameter variable. It doesn't make
  1763. // sense to do it here because parameters are so messed up.
  1764. switch (AI.getKind()) {
  1765. case ABIArgInfo::Extend:
  1766. if (AI.isSignExt())
  1767. Attrs.addAttribute(llvm::Attribute::SExt);
  1768. else
  1769. Attrs.addAttribute(llvm::Attribute::ZExt);
  1770. LLVM_FALLTHROUGH;
  1771. case ABIArgInfo::Direct:
  1772. if (ArgNo == 0 && FI.isChainCall())
  1773. Attrs.addAttribute(llvm::Attribute::Nest);
  1774. else if (AI.getInReg())
  1775. Attrs.addAttribute(llvm::Attribute::InReg);
  1776. break;
  1777. case ABIArgInfo::Indirect: {
  1778. if (AI.getInReg())
  1779. Attrs.addAttribute(llvm::Attribute::InReg);
  1780. if (AI.getIndirectByVal())
  1781. Attrs.addAttribute(llvm::Attribute::ByVal);
  1782. CharUnits Align = AI.getIndirectAlign();
  1783. // In a byval argument, it is important that the required
  1784. // alignment of the type is honored, as LLVM might be creating a
  1785. // *new* stack object, and needs to know what alignment to give
  1786. // it. (Sometimes it can deduce a sensible alignment on its own,
  1787. // but not if clang decides it must emit a packed struct, or the
  1788. // user specifies increased alignment requirements.)
  1789. //
  1790. // This is different from indirect *not* byval, where the object
  1791. // exists already, and the align attribute is purely
  1792. // informative.
  1793. assert(!Align.isZero());
  1794. // For now, only add this when we have a byval argument.
  1795. // TODO: be less lazy about updating test cases.
  1796. if (AI.getIndirectByVal())
  1797. Attrs.addAlignmentAttr(Align.getQuantity());
  1798. // byval disables readnone and readonly.
  1799. FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
  1800. .removeAttribute(llvm::Attribute::ReadNone);
  1801. break;
  1802. }
  1803. case ABIArgInfo::Ignore:
  1804. case ABIArgInfo::Expand:
  1805. case ABIArgInfo::CoerceAndExpand:
  1806. break;
  1807. case ABIArgInfo::InAlloca:
  1808. // inalloca disables readnone and readonly.
  1809. FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
  1810. .removeAttribute(llvm::Attribute::ReadNone);
  1811. continue;
  1812. }
  1813. if (const auto *RefTy = ParamType->getAs<ReferenceType>()) {
  1814. QualType PTy = RefTy->getPointeeType();
  1815. if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
  1816. Attrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy)
  1817. .getQuantity());
  1818. else if (getContext().getTargetAddressSpace(PTy) == 0 &&
  1819. !CodeGenOpts.NullPointerIsValid)
  1820. Attrs.addAttribute(llvm::Attribute::NonNull);
  1821. }
  1822. switch (FI.getExtParameterInfo(ArgNo).getABI()) {
  1823. case ParameterABI::Ordinary:
  1824. break;
  1825. case ParameterABI::SwiftIndirectResult: {
  1826. // Add 'sret' if we haven't already used it for something, but
  1827. // only if the result is void.
  1828. if (!hasUsedSRet && RetTy->isVoidType()) {
  1829. Attrs.addAttribute(llvm::Attribute::StructRet);
  1830. hasUsedSRet = true;
  1831. }
  1832. // Add 'noalias' in either case.
  1833. Attrs.addAttribute(llvm::Attribute::NoAlias);
  1834. // Add 'dereferenceable' and 'alignment'.
  1835. auto PTy = ParamType->getPointeeType();
  1836. if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {
  1837. auto info = getContext().getTypeInfoInChars(PTy);
  1838. Attrs.addDereferenceableAttr(info.first.getQuantity());
  1839. Attrs.addAttribute(llvm::Attribute::getWithAlignment(getLLVMContext(),
  1840. info.second.getQuantity()));
  1841. }
  1842. break;
  1843. }
  1844. case ParameterABI::SwiftErrorResult:
  1845. Attrs.addAttribute(llvm::Attribute::SwiftError);
  1846. break;
  1847. case ParameterABI::SwiftContext:
  1848. Attrs.addAttribute(llvm::Attribute::SwiftSelf);
  1849. break;
  1850. }
  1851. if (FI.getExtParameterInfo(ArgNo).isNoEscape())
  1852. Attrs.addAttribute(llvm::Attribute::NoCapture);
  1853. if (Attrs.hasAttributes()) {
  1854. unsigned FirstIRArg, NumIRArgs;
  1855. std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
  1856. for (unsigned i = 0; i < NumIRArgs; i++)
  1857. ArgAttrs[FirstIRArg + i] =
  1858. llvm::AttributeSet::get(getLLVMContext(), Attrs);
  1859. }
  1860. }
  1861. assert(ArgNo == FI.arg_size());
  1862. AttrList = llvm::AttributeList::get(
  1863. getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), FuncAttrs),
  1864. llvm::AttributeSet::get(getLLVMContext(), RetAttrs), ArgAttrs);
  1865. }
  1866. /// An argument came in as a promoted argument; demote it back to its
  1867. /// declared type.
  1868. static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
  1869. const VarDecl *var,
  1870. llvm::Value *value) {
  1871. llvm::Type *varType = CGF.ConvertType(var->getType());
  1872. // This can happen with promotions that actually don't change the
  1873. // underlying type, like the enum promotions.
  1874. if (value->getType() == varType) return value;
  1875. assert((varType->isIntegerTy() || varType->isFloatingPointTy())
  1876. && "unexpected promotion type");
  1877. if (isa<llvm::IntegerType>(varType))
  1878. return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
  1879. return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
  1880. }
  1881. /// Returns the attribute (either parameter attribute, or function
  1882. /// attribute), which declares argument ArgNo to be non-null.
  1883. static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD,
  1884. QualType ArgType, unsigned ArgNo) {
  1885. // FIXME: __attribute__((nonnull)) can also be applied to:
  1886. // - references to pointers, where the pointee is known to be
  1887. // nonnull (apparently a Clang extension)
  1888. // - transparent unions containing pointers
  1889. // In the former case, LLVM IR cannot represent the constraint. In
  1890. // the latter case, we have no guarantee that the transparent union
  1891. // is in fact passed as a pointer.
  1892. if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType())
  1893. return nullptr;
  1894. // First, check attribute on parameter itself.
  1895. if (PVD) {
  1896. if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>())
  1897. return ParmNNAttr;
  1898. }
  1899. // Check function attributes.
  1900. if (!FD)
  1901. return nullptr;
  1902. for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) {
  1903. if (NNAttr->isNonNull(ArgNo))
  1904. return NNAttr;
  1905. }
  1906. return nullptr;
  1907. }
  1908. namespace {
  1909. struct CopyBackSwiftError final : EHScopeStack::Cleanup {
  1910. Address Temp;
  1911. Address Arg;
  1912. CopyBackSwiftError(Address temp, Address arg) : Temp(temp), Arg(arg) {}
  1913. void Emit(CodeGenFunction &CGF, Flags flags) override {
  1914. llvm::Value *errorValue = CGF.Builder.CreateLoad(Temp);
  1915. CGF.Builder.CreateStore(errorValue, Arg);
  1916. }
  1917. };
  1918. }
  1919. void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
  1920. llvm::Function *Fn,
  1921. const FunctionArgList &Args) {
  1922. if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>())
  1923. // Naked functions don't have prologues.
  1924. return;
  1925. // If this is an implicit-return-zero function, go ahead and
  1926. // initialize the return value. TODO: it might be nice to have
  1927. // a more general mechanism for this that didn't require synthesized
  1928. // return statements.
  1929. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
  1930. if (FD->hasImplicitReturnZero()) {
  1931. QualType RetTy = FD->getReturnType().getUnqualifiedType();
  1932. llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
  1933. llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
  1934. Builder.CreateStore(Zero, ReturnValue);
  1935. }
  1936. }
  1937. // FIXME: We no longer need the types from FunctionArgList; lift up and
  1938. // simplify.
  1939. ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI);
  1940. // Flattened function arguments.
  1941. SmallVector<llvm::Value *, 16> FnArgs;
  1942. FnArgs.reserve(IRFunctionArgs.totalIRArgs());
  1943. for (auto &Arg : Fn->args()) {
  1944. FnArgs.push_back(&Arg);
  1945. }
  1946. assert(FnArgs.size() == IRFunctionArgs.totalIRArgs());
  1947. // If we're using inalloca, all the memory arguments are GEPs off of the last
  1948. // parameter, which is a pointer to the complete memory area.
  1949. Address ArgStruct = Address::invalid();
  1950. const llvm::StructLayout *ArgStructLayout = nullptr;
  1951. if (IRFunctionArgs.hasInallocaArg()) {
  1952. ArgStructLayout = CGM.getDataLayout().getStructLayout(FI.getArgStruct());
  1953. ArgStruct = Address(FnArgs[IRFunctionArgs.getInallocaArgNo()],
  1954. FI.getArgStructAlignment());
  1955. assert(ArgStruct.getType() == FI.getArgStruct()->getPointerTo());
  1956. }
  1957. // Name the struct return parameter.
  1958. if (IRFunctionArgs.hasSRetArg()) {
  1959. auto AI = cast<llvm::Argument>(FnArgs[IRFunctionArgs.getSRetArgNo()]);
  1960. AI->setName("agg.result");
  1961. AI->addAttr(llvm::Attribute::NoAlias);
  1962. }
  1963. // Track if we received the parameter as a pointer (indirect, byval, or
  1964. // inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it
  1965. // into a local alloca for us.
  1966. SmallVector<ParamValue, 16> ArgVals;
  1967. ArgVals.reserve(Args.size());
  1968. // Create a pointer value for every parameter declaration. This usually
  1969. // entails copying one or more LLVM IR arguments into an alloca. Don't push
  1970. // any cleanups or do anything that might unwind. We do that separately, so
  1971. // we can push the cleanups in the correct order for the ABI.
  1972. assert(FI.arg_size() == Args.size() &&
  1973. "Mismatch between function signature & arguments.");
  1974. unsigned ArgNo = 0;
  1975. CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
  1976. for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
  1977. i != e; ++i, ++info_it, ++ArgNo) {
  1978. const VarDecl *Arg = *i;
  1979. const ABIArgInfo &ArgI = info_it->info;
  1980. bool isPromoted =
  1981. isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
  1982. // We are converting from ABIArgInfo type to VarDecl type directly, unless
  1983. // the parameter is promoted. In this case we convert to
  1984. // CGFunctionInfo::ArgInfo type with subsequent argument demotion.
  1985. QualType Ty = isPromoted ? info_it->type : Arg->getType();
  1986. assert(hasScalarEvaluationKind(Ty) ==
  1987. hasScalarEvaluationKind(Arg->getType()));
  1988. unsigned FirstIRArg, NumIRArgs;
  1989. std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
  1990. switch (ArgI.getKind()) {
  1991. case ABIArgInfo::InAlloca: {
  1992. assert(NumIRArgs == 0);
  1993. auto FieldIndex = ArgI.getInAllocaFieldIndex();
  1994. CharUnits FieldOffset =
  1995. CharUnits::fromQuantity(ArgStructLayout->getElementOffset(FieldIndex));
  1996. Address V = Builder.CreateStructGEP(ArgStruct, FieldIndex, FieldOffset,
  1997. Arg->getName());
  1998. ArgVals.push_back(ParamValue::forIndirect(V));
  1999. break;
  2000. }
  2001. case ABIArgInfo::Indirect: {
  2002. assert(NumIRArgs == 1);
  2003. Address ParamAddr = Address(FnArgs[FirstIRArg], ArgI.getIndirectAlign());
  2004. if (!hasScalarEvaluationKind(Ty)) {
  2005. // Aggregates and complex variables are accessed by reference. All we
  2006. // need to do is realign the value, if requested.
  2007. Address V = ParamAddr;
  2008. if (ArgI.getIndirectRealign()) {
  2009. Address AlignedTemp = CreateMemTemp(Ty, "coerce");
  2010. // Copy from the incoming argument pointer to the temporary with the
  2011. // appropriate alignment.
  2012. //
  2013. // FIXME: We should have a common utility for generating an aggregate
  2014. // copy.
  2015. CharUnits Size = getContext().getTypeSizeInChars(Ty);
  2016. auto SizeVal = llvm::ConstantInt::get(IntPtrTy, Size.getQuantity());
  2017. Address Dst = Builder.CreateBitCast(AlignedTemp, Int8PtrTy);
  2018. Address Src = Builder.CreateBitCast(ParamAddr, Int8PtrTy);
  2019. Builder.CreateMemCpy(Dst, Src, SizeVal, false);
  2020. V = AlignedTemp;
  2021. }
  2022. ArgVals.push_back(ParamValue::forIndirect(V));
  2023. } else {
  2024. // Load scalar value from indirect argument.
  2025. llvm::Value *V =
  2026. EmitLoadOfScalar(ParamAddr, false, Ty, Arg->getBeginLoc());
  2027. if (isPromoted)
  2028. V = emitArgumentDemotion(*this, Arg, V);
  2029. ArgVals.push_back(ParamValue::forDirect(V));
  2030. }
  2031. break;
  2032. }
  2033. case ABIArgInfo::Extend:
  2034. case ABIArgInfo::Direct: {
  2035. // If we have the trivial case, handle it with no muss and fuss.
  2036. if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
  2037. ArgI.getCoerceToType() == ConvertType(Ty) &&
  2038. ArgI.getDirectOffset() == 0) {
  2039. assert(NumIRArgs == 1);
  2040. llvm::Value *V = FnArgs[FirstIRArg];
  2041. auto AI = cast<llvm::Argument>(V);
  2042. if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {
  2043. if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(),
  2044. PVD->getFunctionScopeIndex()) &&
  2045. !CGM.getCodeGenOpts().NullPointerIsValid)
  2046. AI->addAttr(llvm::Attribute::NonNull);
  2047. QualType OTy = PVD->getOriginalType();
  2048. if (const auto *ArrTy =
  2049. getContext().getAsConstantArrayType(OTy)) {
  2050. // A C99 array parameter declaration with the static keyword also
  2051. // indicates dereferenceability, and if the size is constant we can
  2052. // use the dereferenceable attribute (which requires the size in
  2053. // bytes).
  2054. if (ArrTy->getSizeModifier() == ArrayType::Static) {
  2055. QualType ETy = ArrTy->getElementType();
  2056. uint64_t ArrSize = ArrTy->getSize().getZExtValue();
  2057. if (!ETy->isIncompleteType() && ETy->isConstantSizeType() &&
  2058. ArrSize) {
  2059. llvm::AttrBuilder Attrs;
  2060. Attrs.addDereferenceableAttr(
  2061. getContext().getTypeSizeInChars(ETy).getQuantity()*ArrSize);
  2062. AI->addAttrs(Attrs);
  2063. } else if (getContext().getTargetAddressSpace(ETy) == 0 &&
  2064. !CGM.getCodeGenOpts().NullPointerIsValid) {
  2065. AI->addAttr(llvm::Attribute::NonNull);
  2066. }
  2067. }
  2068. } else if (const auto *ArrTy =
  2069. getContext().getAsVariableArrayType(OTy)) {
  2070. // For C99 VLAs with the static keyword, we don't know the size so
  2071. // we can't use the dereferenceable attribute, but in addrspace(0)
  2072. // we know that it must be nonnull.
  2073. if (ArrTy->getSizeModifier() == VariableArrayType::Static &&
  2074. !getContext().getTargetAddressSpace(ArrTy->getElementType()) &&
  2075. !CGM.getCodeGenOpts().NullPointerIsValid)
  2076. AI->addAttr(llvm::Attribute::NonNull);
  2077. }
  2078. const auto *AVAttr = PVD->getAttr<AlignValueAttr>();
  2079. if (!AVAttr)
  2080. if (const auto *TOTy = dyn_cast<TypedefType>(OTy))
  2081. AVAttr = TOTy->getDecl()->getAttr<AlignValueAttr>();
  2082. if (AVAttr) {
  2083. llvm::Value *AlignmentValue =
  2084. EmitScalarExpr(AVAttr->getAlignment());
  2085. llvm::ConstantInt *AlignmentCI =
  2086. cast<llvm::ConstantInt>(AlignmentValue);
  2087. unsigned Alignment = std::min((unsigned)AlignmentCI->getZExtValue(),
  2088. +llvm::Value::MaximumAlignment);
  2089. AI->addAttrs(llvm::AttrBuilder().addAlignmentAttr(Alignment));
  2090. }
  2091. }
  2092. if (Arg->getType().isRestrictQualified())
  2093. AI->addAttr(llvm::Attribute::NoAlias);
  2094. // LLVM expects swifterror parameters to be used in very restricted
  2095. // ways. Copy the value into a less-restricted temporary.
  2096. if (FI.getExtParameterInfo(ArgNo).getABI()
  2097. == ParameterABI::SwiftErrorResult) {
  2098. QualType pointeeTy = Ty->getPointeeType();
  2099. assert(pointeeTy->isPointerType());
  2100. Address temp =
  2101. CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");
  2102. Address arg = Address(V, getContext().getTypeAlignInChars(pointeeTy));
  2103. llvm::Value *incomingErrorValue = Builder.CreateLoad(arg);
  2104. Builder.CreateStore(incomingErrorValue, temp);
  2105. V = temp.getPointer();
  2106. // Push a cleanup to copy the value back at the end of the function.
  2107. // The convention does not guarantee that the value will be written
  2108. // back if the function exits with an unwind exception.
  2109. EHStack.pushCleanup<CopyBackSwiftError>(NormalCleanup, temp, arg);
  2110. }
  2111. // Ensure the argument is the correct type.
  2112. if (V->getType() != ArgI.getCoerceToType())
  2113. V = Builder.CreateBitCast(V, ArgI.getCoerceToType());
  2114. if (isPromoted)
  2115. V = emitArgumentDemotion(*this, Arg, V);
  2116. // Because of merging of function types from multiple decls it is
  2117. // possible for the type of an argument to not match the corresponding
  2118. // type in the function type. Since we are codegening the callee
  2119. // in here, add a cast to the argument type.
  2120. llvm::Type *LTy = ConvertType(Arg->getType());
  2121. if (V->getType() != LTy)
  2122. V = Builder.CreateBitCast(V, LTy);
  2123. ArgVals.push_back(ParamValue::forDirect(V));
  2124. break;
  2125. }
  2126. Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg),
  2127. Arg->getName());
  2128. // Pointer to store into.
  2129. Address Ptr = emitAddressAtOffset(*this, Alloca, ArgI);
  2130. // Fast-isel and the optimizer generally like scalar values better than
  2131. // FCAs, so we flatten them if this is safe to do for this argument.
  2132. llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType());
  2133. if (ArgI.isDirect() && ArgI.getCanBeFlattened() && STy &&
  2134. STy->getNumElements() > 1) {
  2135. auto SrcLayout = CGM.getDataLayout().getStructLayout(STy);
  2136. uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy);
  2137. llvm::Type *DstTy = Ptr.getElementType();
  2138. uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy);
  2139. Address AddrToStoreInto = Address::invalid();
  2140. if (SrcSize <= DstSize) {
  2141. AddrToStoreInto = Builder.CreateElementBitCast(Ptr, STy);
  2142. } else {
  2143. AddrToStoreInto =
  2144. CreateTempAlloca(STy, Alloca.getAlignment(), "coerce");
  2145. }
  2146. assert(STy->getNumElements() == NumIRArgs);
  2147. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  2148. auto AI = FnArgs[FirstIRArg + i];
  2149. AI->setName(Arg->getName() + ".coerce" + Twine(i));
  2150. auto Offset = CharUnits::fromQuantity(SrcLayout->getElementOffset(i));
  2151. Address EltPtr =
  2152. Builder.CreateStructGEP(AddrToStoreInto, i, Offset);
  2153. Builder.CreateStore(AI, EltPtr);
  2154. }
  2155. if (SrcSize > DstSize) {
  2156. Builder.CreateMemCpy(Ptr, AddrToStoreInto, DstSize);
  2157. }
  2158. } else {
  2159. // Simple case, just do a coerced store of the argument into the alloca.
  2160. assert(NumIRArgs == 1);
  2161. auto AI = FnArgs[FirstIRArg];
  2162. AI->setName(Arg->getName() + ".coerce");
  2163. CreateCoercedStore(AI, Ptr, /*DestIsVolatile=*/false, *this);
  2164. }
  2165. // Match to what EmitParmDecl is expecting for this type.
  2166. if (CodeGenFunction::hasScalarEvaluationKind(Ty)) {
  2167. llvm::Value *V =
  2168. EmitLoadOfScalar(Alloca, false, Ty, Arg->getBeginLoc());
  2169. if (isPromoted)
  2170. V = emitArgumentDemotion(*this, Arg, V);
  2171. ArgVals.push_back(ParamValue::forDirect(V));
  2172. } else {
  2173. ArgVals.push_back(ParamValue::forIndirect(Alloca));
  2174. }
  2175. break;
  2176. }
  2177. case ABIArgInfo::CoerceAndExpand: {
  2178. // Reconstruct into a temporary.
  2179. Address alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));
  2180. ArgVals.push_back(ParamValue::forIndirect(alloca));
  2181. auto coercionType = ArgI.getCoerceAndExpandType();
  2182. alloca = Builder.CreateElementBitCast(alloca, coercionType);
  2183. auto layout = CGM.getDataLayout().getStructLayout(coercionType);
  2184. unsigned argIndex = FirstIRArg;
  2185. for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
  2186. llvm::Type *eltType = coercionType->getElementType(i);
  2187. if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType))
  2188. continue;
  2189. auto eltAddr = Builder.CreateStructGEP(alloca, i, layout);
  2190. auto elt = FnArgs[argIndex++];
  2191. Builder.CreateStore(elt, eltAddr);
  2192. }
  2193. assert(argIndex == FirstIRArg + NumIRArgs);
  2194. break;
  2195. }
  2196. case ABIArgInfo::Expand: {
  2197. // If this structure was expanded into multiple arguments then
  2198. // we need to create a temporary and reconstruct it from the
  2199. // arguments.
  2200. Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));
  2201. LValue LV = MakeAddrLValue(Alloca, Ty);
  2202. ArgVals.push_back(ParamValue::forIndirect(Alloca));
  2203. auto FnArgIter = FnArgs.begin() + FirstIRArg;
  2204. ExpandTypeFromArgs(Ty, LV, FnArgIter);
  2205. assert(FnArgIter == FnArgs.begin() + FirstIRArg + NumIRArgs);
  2206. for (unsigned i = 0, e = NumIRArgs; i != e; ++i) {
  2207. auto AI = FnArgs[FirstIRArg + i];
  2208. AI->setName(Arg->getName() + "." + Twine(i));
  2209. }
  2210. break;
  2211. }
  2212. case ABIArgInfo::Ignore:
  2213. assert(NumIRArgs == 0);
  2214. // Initialize the local variable appropriately.
  2215. if (!hasScalarEvaluationKind(Ty)) {
  2216. ArgVals.push_back(ParamValue::forIndirect(CreateMemTemp(Ty)));
  2217. } else {
  2218. llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType()));
  2219. ArgVals.push_back(ParamValue::forDirect(U));
  2220. }
  2221. break;
  2222. }
  2223. }
  2224. if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
  2225. for (int I = Args.size() - 1; I >= 0; --I)
  2226. EmitParmDecl(*Args[I], ArgVals[I], I + 1);
  2227. } else {
  2228. for (unsigned I = 0, E = Args.size(); I != E; ++I)
  2229. EmitParmDecl(*Args[I], ArgVals[I], I + 1);
  2230. }
  2231. }
  2232. static void eraseUnusedBitCasts(llvm::Instruction *insn) {
  2233. while (insn->use_empty()) {
  2234. llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);
  2235. if (!bitcast) return;
  2236. // This is "safe" because we would have used a ConstantExpr otherwise.
  2237. insn = cast<llvm::Instruction>(bitcast->getOperand(0));
  2238. bitcast->eraseFromParent();
  2239. }
  2240. }
  2241. /// Try to emit a fused autorelease of a return result.
  2242. static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF,
  2243. llvm::Value *result) {
  2244. // We must be immediately followed the cast.
  2245. llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
  2246. if (BB->empty()) return nullptr;
  2247. if (&BB->back() != result) return nullptr;
  2248. llvm::Type *resultType = result->getType();
  2249. // result is in a BasicBlock and is therefore an Instruction.
  2250. llvm::Instruction *generator = cast<llvm::Instruction>(result);
  2251. SmallVector<llvm::Instruction *, 4> InstsToKill;
  2252. // Look for:
  2253. // %generator = bitcast %type1* %generator2 to %type2*
  2254. while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
  2255. // We would have emitted this as a constant if the operand weren't
  2256. // an Instruction.
  2257. generator = cast<llvm::Instruction>(bitcast->getOperand(0));
  2258. // Require the generator to be immediately followed by the cast.
  2259. if (generator->getNextNode() != bitcast)
  2260. return nullptr;
  2261. InstsToKill.push_back(bitcast);
  2262. }
  2263. // Look for:
  2264. // %generator = call i8* @objc_retain(i8* %originalResult)
  2265. // or
  2266. // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
  2267. llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
  2268. if (!call) return nullptr;
  2269. bool doRetainAutorelease;
  2270. if (call->getCalledValue() == CGF.CGM.getObjCEntrypoints().objc_retain) {
  2271. doRetainAutorelease = true;
  2272. } else if (call->getCalledValue() == CGF.CGM.getObjCEntrypoints()
  2273. .objc_retainAutoreleasedReturnValue) {
  2274. doRetainAutorelease = false;
  2275. // If we emitted an assembly marker for this call (and the
  2276. // ARCEntrypoints field should have been set if so), go looking
  2277. // for that call. If we can't find it, we can't do this
  2278. // optimization. But it should always be the immediately previous
  2279. // instruction, unless we needed bitcasts around the call.
  2280. if (CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker) {
  2281. llvm::Instruction *prev = call->getPrevNode();
  2282. assert(prev);
  2283. if (isa<llvm::BitCastInst>(prev)) {
  2284. prev = prev->getPrevNode();
  2285. assert(prev);
  2286. }
  2287. assert(isa<llvm::CallInst>(prev));
  2288. assert(cast<llvm::CallInst>(prev)->getCalledValue() ==
  2289. CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker);
  2290. InstsToKill.push_back(prev);
  2291. }
  2292. } else {
  2293. return nullptr;
  2294. }
  2295. result = call->getArgOperand(0);
  2296. InstsToKill.push_back(call);
  2297. // Keep killing bitcasts, for sanity. Note that we no longer care
  2298. // about precise ordering as long as there's exactly one use.
  2299. while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
  2300. if (!bitcast->hasOneUse()) break;
  2301. InstsToKill.push_back(bitcast);
  2302. result = bitcast->getOperand(0);
  2303. }
  2304. // Delete all the unnecessary instructions, from latest to earliest.
  2305. for (auto *I : InstsToKill)
  2306. I->eraseFromParent();
  2307. // Do the fused retain/autorelease if we were asked to.
  2308. if (doRetainAutorelease)
  2309. result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
  2310. // Cast back to the result type.
  2311. return CGF.Builder.CreateBitCast(result, resultType);
  2312. }
  2313. /// If this is a +1 of the value of an immutable 'self', remove it.
  2314. static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF,
  2315. llvm::Value *result) {
  2316. // This is only applicable to a method with an immutable 'self'.
  2317. const ObjCMethodDecl *method =
  2318. dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl);
  2319. if (!method) return nullptr;
  2320. const VarDecl *self = method->getSelfDecl();
  2321. if (!self->getType().isConstQualified()) return nullptr;
  2322. // Look for a retain call.
  2323. llvm::CallInst *retainCall =
  2324. dyn_cast<llvm::CallInst>(result->stripPointerCasts());
  2325. if (!retainCall ||
  2326. retainCall->getCalledValue() != CGF.CGM.getObjCEntrypoints().objc_retain)
  2327. return nullptr;
  2328. // Look for an ordinary load of 'self'.
  2329. llvm::Value *retainedValue = retainCall->getArgOperand(0);
  2330. llvm::LoadInst *load =
  2331. dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());
  2332. if (!load || load->isAtomic() || load->isVolatile() ||
  2333. load->getPointerOperand() != CGF.GetAddrOfLocalVar(self).getPointer())
  2334. return nullptr;
  2335. // Okay! Burn it all down. This relies for correctness on the
  2336. // assumption that the retain is emitted as part of the return and
  2337. // that thereafter everything is used "linearly".
  2338. llvm::Type *resultType = result->getType();
  2339. eraseUnusedBitCasts(cast<llvm::Instruction>(result));
  2340. assert(retainCall->use_empty());
  2341. retainCall->eraseFromParent();
  2342. eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue));
  2343. return CGF.Builder.CreateBitCast(load, resultType);
  2344. }
  2345. /// Emit an ARC autorelease of the result of a function.
  2346. ///
  2347. /// \return the value to actually return from the function
  2348. static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF,
  2349. llvm::Value *result) {
  2350. // If we're returning 'self', kill the initial retain. This is a
  2351. // heuristic attempt to "encourage correctness" in the really unfortunate
  2352. // case where we have a return of self during a dealloc and we desperately
  2353. // need to avoid the possible autorelease.
  2354. if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result))
  2355. return self;
  2356. // At -O0, try to emit a fused retain/autorelease.
  2357. if (CGF.shouldUseFusedARCCalls())
  2358. if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
  2359. return fused;
  2360. return CGF.EmitARCAutoreleaseReturnValue(result);
  2361. }
  2362. /// Heuristically search for a dominating store to the return-value slot.
  2363. static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) {
  2364. // Check if a User is a store which pointerOperand is the ReturnValue.
  2365. // We are looking for stores to the ReturnValue, not for stores of the
  2366. // ReturnValue to some other location.
  2367. auto GetStoreIfValid = [&CGF](llvm::User *U) -> llvm::StoreInst * {
  2368. auto *SI = dyn_cast<llvm::StoreInst>(U);
  2369. if (!SI || SI->getPointerOperand() != CGF.ReturnValue.getPointer())
  2370. return nullptr;
  2371. // These aren't actually possible for non-coerced returns, and we
  2372. // only care about non-coerced returns on this code path.
  2373. assert(!SI->isAtomic() && !SI->isVolatile());
  2374. return SI;
  2375. };
  2376. // If there are multiple uses of the return-value slot, just check
  2377. // for something immediately preceding the IP. Sometimes this can
  2378. // happen with how we generate implicit-returns; it can also happen
  2379. // with noreturn cleanups.
  2380. if (!CGF.ReturnValue.getPointer()->hasOneUse()) {
  2381. llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
  2382. if (IP->empty()) return nullptr;
  2383. llvm::Instruction *I = &IP->back();
  2384. // Skip lifetime markers
  2385. for (llvm::BasicBlock::reverse_iterator II = IP->rbegin(),
  2386. IE = IP->rend();
  2387. II != IE; ++II) {
  2388. if (llvm::IntrinsicInst *Intrinsic =
  2389. dyn_cast<llvm::IntrinsicInst>(&*II)) {
  2390. if (Intrinsic->getIntrinsicID() == llvm::Intrinsic::lifetime_end) {
  2391. const llvm::Value *CastAddr = Intrinsic->getArgOperand(1);
  2392. ++II;
  2393. if (II == IE)
  2394. break;
  2395. if (isa<llvm::BitCastInst>(&*II) && (CastAddr == &*II))
  2396. continue;
  2397. }
  2398. }
  2399. I = &*II;
  2400. break;
  2401. }
  2402. return GetStoreIfValid(I);
  2403. }
  2404. llvm::StoreInst *store =
  2405. GetStoreIfValid(CGF.ReturnValue.getPointer()->user_back());
  2406. if (!store) return nullptr;
  2407. // Now do a first-and-dirty dominance check: just walk up the
  2408. // single-predecessors chain from the current insertion point.
  2409. llvm::BasicBlock *StoreBB = store->getParent();
  2410. llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
  2411. while (IP != StoreBB) {
  2412. if (!(IP = IP->getSinglePredecessor()))
  2413. return nullptr;
  2414. }
  2415. // Okay, the store's basic block dominates the insertion point; we
  2416. // can do our thing.
  2417. return store;
  2418. }
  2419. void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
  2420. bool EmitRetDbgLoc,
  2421. SourceLocation EndLoc) {
  2422. if (FI.isNoReturn()) {
  2423. // Noreturn functions don't return.
  2424. EmitUnreachable(EndLoc);
  2425. return;
  2426. }
  2427. if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) {
  2428. // Naked functions don't have epilogues.
  2429. Builder.CreateUnreachable();
  2430. return;
  2431. }
  2432. // Functions with no result always return void.
  2433. if (!ReturnValue.isValid()) {
  2434. Builder.CreateRetVoid();
  2435. return;
  2436. }
  2437. llvm::DebugLoc RetDbgLoc;
  2438. llvm::Value *RV = nullptr;
  2439. QualType RetTy = FI.getReturnType();
  2440. const ABIArgInfo &RetAI = FI.getReturnInfo();
  2441. switch (RetAI.getKind()) {
  2442. case ABIArgInfo::InAlloca:
  2443. // Aggregrates get evaluated directly into the destination. Sometimes we
  2444. // need to return the sret value in a register, though.
  2445. assert(hasAggregateEvaluationKind(RetTy));
  2446. if (RetAI.getInAllocaSRet()) {
  2447. llvm::Function::arg_iterator EI = CurFn->arg_end();
  2448. --EI;
  2449. llvm::Value *ArgStruct = &*EI;
  2450. llvm::Value *SRet = Builder.CreateStructGEP(
  2451. nullptr, ArgStruct, RetAI.getInAllocaFieldIndex());
  2452. RV = Builder.CreateAlignedLoad(SRet, getPointerAlign(), "sret");
  2453. }
  2454. break;
  2455. case ABIArgInfo::Indirect: {
  2456. auto AI = CurFn->arg_begin();
  2457. if (RetAI.isSRetAfterThis())
  2458. ++AI;
  2459. switch (getEvaluationKind(RetTy)) {
  2460. case TEK_Complex: {
  2461. ComplexPairTy RT =
  2462. EmitLoadOfComplex(MakeAddrLValue(ReturnValue, RetTy), EndLoc);
  2463. EmitStoreOfComplex(RT, MakeNaturalAlignAddrLValue(&*AI, RetTy),
  2464. /*isInit*/ true);
  2465. break;
  2466. }
  2467. case TEK_Aggregate:
  2468. // Do nothing; aggregrates get evaluated directly into the destination.
  2469. break;
  2470. case TEK_Scalar:
  2471. EmitStoreOfScalar(Builder.CreateLoad(ReturnValue),
  2472. MakeNaturalAlignAddrLValue(&*AI, RetTy),
  2473. /*isInit*/ true);
  2474. break;
  2475. }
  2476. break;
  2477. }
  2478. case ABIArgInfo::Extend:
  2479. case ABIArgInfo::Direct:
  2480. if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
  2481. RetAI.getDirectOffset() == 0) {
  2482. // The internal return value temp always will have pointer-to-return-type
  2483. // type, just do a load.
  2484. // If there is a dominating store to ReturnValue, we can elide
  2485. // the load, zap the store, and usually zap the alloca.
  2486. if (llvm::StoreInst *SI =
  2487. findDominatingStoreToReturnValue(*this)) {
  2488. // Reuse the debug location from the store unless there is
  2489. // cleanup code to be emitted between the store and return
  2490. // instruction.
  2491. if (EmitRetDbgLoc && !AutoreleaseResult)
  2492. RetDbgLoc = SI->getDebugLoc();
  2493. // Get the stored value and nuke the now-dead store.
  2494. RV = SI->getValueOperand();
  2495. SI->eraseFromParent();
  2496. // If that was the only use of the return value, nuke it as well now.
  2497. auto returnValueInst = ReturnValue.getPointer();
  2498. if (returnValueInst->use_empty()) {
  2499. if (auto alloca = dyn_cast<llvm::AllocaInst>(returnValueInst)) {
  2500. alloca->eraseFromParent();
  2501. ReturnValue = Address::invalid();
  2502. }
  2503. }
  2504. // Otherwise, we have to do a simple load.
  2505. } else {
  2506. RV = Builder.CreateLoad(ReturnValue);
  2507. }
  2508. } else {
  2509. // If the value is offset in memory, apply the offset now.
  2510. Address V = emitAddressAtOffset(*this, ReturnValue, RetAI);
  2511. RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
  2512. }
  2513. // In ARC, end functions that return a retainable type with a call
  2514. // to objc_autoreleaseReturnValue.
  2515. if (AutoreleaseResult) {
  2516. #ifndef NDEBUG
  2517. // Type::isObjCRetainabletype has to be called on a QualType that hasn't
  2518. // been stripped of the typedefs, so we cannot use RetTy here. Get the
  2519. // original return type of FunctionDecl, CurCodeDecl, and BlockDecl from
  2520. // CurCodeDecl or BlockInfo.
  2521. QualType RT;
  2522. if (auto *FD = dyn_cast<FunctionDecl>(CurCodeDecl))
  2523. RT = FD->getReturnType();
  2524. else if (auto *MD = dyn_cast<ObjCMethodDecl>(CurCodeDecl))
  2525. RT = MD->getReturnType();
  2526. else if (isa<BlockDecl>(CurCodeDecl))
  2527. RT = BlockInfo->BlockExpression->getFunctionType()->getReturnType();
  2528. else
  2529. llvm_unreachable("Unexpected function/method type");
  2530. assert(getLangOpts().ObjCAutoRefCount &&
  2531. !FI.isReturnsRetained() &&
  2532. RT->isObjCRetainableType());
  2533. #endif
  2534. RV = emitAutoreleaseOfResult(*this, RV);
  2535. }
  2536. break;
  2537. case ABIArgInfo::Ignore:
  2538. break;
  2539. case ABIArgInfo::CoerceAndExpand: {
  2540. auto coercionType = RetAI.getCoerceAndExpandType();
  2541. auto layout = CGM.getDataLayout().getStructLayout(coercionType);
  2542. // Load all of the coerced elements out into results.
  2543. llvm::SmallVector<llvm::Value*, 4> results;
  2544. Address addr = Builder.CreateElementBitCast(ReturnValue, coercionType);
  2545. for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
  2546. auto coercedEltType = coercionType->getElementType(i);
  2547. if (ABIArgInfo::isPaddingForCoerceAndExpand(coercedEltType))
  2548. continue;
  2549. auto eltAddr = Builder.CreateStructGEP(addr, i, layout);
  2550. auto elt = Builder.CreateLoad(eltAddr);
  2551. results.push_back(elt);
  2552. }
  2553. // If we have one result, it's the single direct result type.
  2554. if (results.size() == 1) {
  2555. RV = results[0];
  2556. // Otherwise, we need to make a first-class aggregate.
  2557. } else {
  2558. // Construct a return type that lacks padding elements.
  2559. llvm::Type *returnType = RetAI.getUnpaddedCoerceAndExpandType();
  2560. RV = llvm::UndefValue::get(returnType);
  2561. for (unsigned i = 0, e = results.size(); i != e; ++i) {
  2562. RV = Builder.CreateInsertValue(RV, results[i], i);
  2563. }
  2564. }
  2565. break;
  2566. }
  2567. case ABIArgInfo::Expand:
  2568. llvm_unreachable("Invalid ABI kind for return argument");
  2569. }
  2570. llvm::Instruction *Ret;
  2571. if (RV) {
  2572. EmitReturnValueCheck(RV);
  2573. Ret = Builder.CreateRet(RV);
  2574. } else {
  2575. Ret = Builder.CreateRetVoid();
  2576. }
  2577. if (RetDbgLoc)
  2578. Ret->setDebugLoc(std::move(RetDbgLoc));
  2579. }
  2580. void CodeGenFunction::EmitReturnValueCheck(llvm::Value *RV) {
  2581. // A current decl may not be available when emitting vtable thunks.
  2582. if (!CurCodeDecl)
  2583. return;
  2584. ReturnsNonNullAttr *RetNNAttr = nullptr;
  2585. if (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute))
  2586. RetNNAttr = CurCodeDecl->getAttr<ReturnsNonNullAttr>();
  2587. if (!RetNNAttr && !requiresReturnValueNullabilityCheck())
  2588. return;
  2589. // Prefer the returns_nonnull attribute if it's present.
  2590. SourceLocation AttrLoc;
  2591. SanitizerMask CheckKind;
  2592. SanitizerHandler Handler;
  2593. if (RetNNAttr) {
  2594. assert(!requiresReturnValueNullabilityCheck() &&
  2595. "Cannot check nullability and the nonnull attribute");
  2596. AttrLoc = RetNNAttr->getLocation();
  2597. CheckKind = SanitizerKind::ReturnsNonnullAttribute;
  2598. Handler = SanitizerHandler::NonnullReturn;
  2599. } else {
  2600. if (auto *DD = dyn_cast<DeclaratorDecl>(CurCodeDecl))
  2601. if (auto *TSI = DD->getTypeSourceInfo())
  2602. if (auto FTL = TSI->getTypeLoc().castAs<FunctionTypeLoc>())
  2603. AttrLoc = FTL.getReturnLoc().findNullabilityLoc();
  2604. CheckKind = SanitizerKind::NullabilityReturn;
  2605. Handler = SanitizerHandler::NullabilityReturn;
  2606. }
  2607. SanitizerScope SanScope(this);
  2608. // Make sure the "return" source location is valid. If we're checking a
  2609. // nullability annotation, make sure the preconditions for the check are met.
  2610. llvm::BasicBlock *Check = createBasicBlock("nullcheck");
  2611. llvm::BasicBlock *NoCheck = createBasicBlock("no.nullcheck");
  2612. llvm::Value *SLocPtr = Builder.CreateLoad(ReturnLocation, "return.sloc.load");
  2613. llvm::Value *CanNullCheck = Builder.CreateIsNotNull(SLocPtr);
  2614. if (requiresReturnValueNullabilityCheck())
  2615. CanNullCheck =
  2616. Builder.CreateAnd(CanNullCheck, RetValNullabilityPrecondition);
  2617. Builder.CreateCondBr(CanNullCheck, Check, NoCheck);
  2618. EmitBlock(Check);
  2619. // Now do the null check.
  2620. llvm::Value *Cond = Builder.CreateIsNotNull(RV);
  2621. llvm::Constant *StaticData[] = {EmitCheckSourceLocation(AttrLoc)};
  2622. llvm::Value *DynamicData[] = {SLocPtr};
  2623. EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, DynamicData);
  2624. EmitBlock(NoCheck);
  2625. #ifndef NDEBUG
  2626. // The return location should not be used after the check has been emitted.
  2627. ReturnLocation = Address::invalid();
  2628. #endif
  2629. }
  2630. static bool isInAllocaArgument(CGCXXABI &ABI, QualType type) {
  2631. const CXXRecordDecl *RD = type->getAsCXXRecordDecl();
  2632. return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory;
  2633. }
  2634. static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF,
  2635. QualType Ty) {
  2636. // FIXME: Generate IR in one pass, rather than going back and fixing up these
  2637. // placeholders.
  2638. llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty);
  2639. llvm::Type *IRPtrTy = IRTy->getPointerTo();
  2640. llvm::Value *Placeholder = llvm::UndefValue::get(IRPtrTy->getPointerTo());
  2641. // FIXME: When we generate this IR in one pass, we shouldn't need
  2642. // this win32-specific alignment hack.
  2643. CharUnits Align = CharUnits::fromQuantity(4);
  2644. Placeholder = CGF.Builder.CreateAlignedLoad(IRPtrTy, Placeholder, Align);
  2645. return AggValueSlot::forAddr(Address(Placeholder, Align),
  2646. Ty.getQualifiers(),
  2647. AggValueSlot::IsNotDestructed,
  2648. AggValueSlot::DoesNotNeedGCBarriers,
  2649. AggValueSlot::IsNotAliased,
  2650. AggValueSlot::DoesNotOverlap);
  2651. }
  2652. void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
  2653. const VarDecl *param,
  2654. SourceLocation loc) {
  2655. // StartFunction converted the ABI-lowered parameter(s) into a
  2656. // local alloca. We need to turn that into an r-value suitable
  2657. // for EmitCall.
  2658. Address local = GetAddrOfLocalVar(param);
  2659. QualType type = param->getType();
  2660. assert(!isInAllocaArgument(CGM.getCXXABI(), type) &&
  2661. "cannot emit delegate call arguments for inalloca arguments!");
  2662. // GetAddrOfLocalVar returns a pointer-to-pointer for references,
  2663. // but the argument needs to be the original pointer.
  2664. if (type->isReferenceType()) {
  2665. args.add(RValue::get(Builder.CreateLoad(local)), type);
  2666. // In ARC, move out of consumed arguments so that the release cleanup
  2667. // entered by StartFunction doesn't cause an over-release. This isn't
  2668. // optimal -O0 code generation, but it should get cleaned up when
  2669. // optimization is enabled. This also assumes that delegate calls are
  2670. // performed exactly once for a set of arguments, but that should be safe.
  2671. } else if (getLangOpts().ObjCAutoRefCount &&
  2672. param->hasAttr<NSConsumedAttr>() &&
  2673. type->isObjCRetainableType()) {
  2674. llvm::Value *ptr = Builder.CreateLoad(local);
  2675. auto null =
  2676. llvm::ConstantPointerNull::get(cast<llvm::PointerType>(ptr->getType()));
  2677. Builder.CreateStore(null, local);
  2678. args.add(RValue::get(ptr), type);
  2679. // For the most part, we just need to load the alloca, except that
  2680. // aggregate r-values are actually pointers to temporaries.
  2681. } else {
  2682. args.add(convertTempToRValue(local, type, loc), type);
  2683. }
  2684. // Deactivate the cleanup for the callee-destructed param that was pushed.
  2685. if (hasAggregateEvaluationKind(type) && !CurFuncIsThunk &&
  2686. type->getAs<RecordType>()->getDecl()->isParamDestroyedInCallee() &&
  2687. type.isDestructedType()) {
  2688. EHScopeStack::stable_iterator cleanup =
  2689. CalleeDestructedParamCleanups.lookup(cast<ParmVarDecl>(param));
  2690. assert(cleanup.isValid() &&
  2691. "cleanup for callee-destructed param not recorded");
  2692. // This unreachable is a temporary marker which will be removed later.
  2693. llvm::Instruction *isActive = Builder.CreateUnreachable();
  2694. args.addArgCleanupDeactivation(cleanup, isActive);
  2695. }
  2696. }
  2697. static bool isProvablyNull(llvm::Value *addr) {
  2698. return isa<llvm::ConstantPointerNull>(addr);
  2699. }
  2700. /// Emit the actual writing-back of a writeback.
  2701. static void emitWriteback(CodeGenFunction &CGF,
  2702. const CallArgList::Writeback &writeback) {
  2703. const LValue &srcLV = writeback.Source;
  2704. Address srcAddr = srcLV.getAddress();
  2705. assert(!isProvablyNull(srcAddr.getPointer()) &&
  2706. "shouldn't have writeback for provably null argument");
  2707. llvm::BasicBlock *contBB = nullptr;
  2708. // If the argument wasn't provably non-null, we need to null check
  2709. // before doing the store.
  2710. bool provablyNonNull = llvm::isKnownNonZero(srcAddr.getPointer(),
  2711. CGF.CGM.getDataLayout());
  2712. if (!provablyNonNull) {
  2713. llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
  2714. contBB = CGF.createBasicBlock("icr.done");
  2715. llvm::Value *isNull =
  2716. CGF.Builder.CreateIsNull(srcAddr.getPointer(), "icr.isnull");
  2717. CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
  2718. CGF.EmitBlock(writebackBB);
  2719. }
  2720. // Load the value to writeback.
  2721. llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
  2722. // Cast it back, in case we're writing an id to a Foo* or something.
  2723. value = CGF.Builder.CreateBitCast(value, srcAddr.getElementType(),
  2724. "icr.writeback-cast");
  2725. // Perform the writeback.
  2726. // If we have a "to use" value, it's something we need to emit a use
  2727. // of. This has to be carefully threaded in: if it's done after the
  2728. // release it's potentially undefined behavior (and the optimizer
  2729. // will ignore it), and if it happens before the retain then the
  2730. // optimizer could move the release there.
  2731. if (writeback.ToUse) {
  2732. assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong);
  2733. // Retain the new value. No need to block-copy here: the block's
  2734. // being passed up the stack.
  2735. value = CGF.EmitARCRetainNonBlock(value);
  2736. // Emit the intrinsic use here.
  2737. CGF.EmitARCIntrinsicUse(writeback.ToUse);
  2738. // Load the old value (primitively).
  2739. llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation());
  2740. // Put the new value in place (primitively).
  2741. CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false);
  2742. // Release the old value.
  2743. CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime());
  2744. // Otherwise, we can just do a normal lvalue store.
  2745. } else {
  2746. CGF.EmitStoreThroughLValue(RValue::get(value), srcLV);
  2747. }
  2748. // Jump to the continuation block.
  2749. if (!provablyNonNull)
  2750. CGF.EmitBlock(contBB);
  2751. }
  2752. static void emitWritebacks(CodeGenFunction &CGF,
  2753. const CallArgList &args) {
  2754. for (const auto &I : args.writebacks())
  2755. emitWriteback(CGF, I);
  2756. }
  2757. static void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF,
  2758. const CallArgList &CallArgs) {
  2759. ArrayRef<CallArgList::CallArgCleanup> Cleanups =
  2760. CallArgs.getCleanupsToDeactivate();
  2761. // Iterate in reverse to increase the likelihood of popping the cleanup.
  2762. for (const auto &I : llvm::reverse(Cleanups)) {
  2763. CGF.DeactivateCleanupBlock(I.Cleanup, I.IsActiveIP);
  2764. I.IsActiveIP->eraseFromParent();
  2765. }
  2766. }
  2767. static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) {
  2768. if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens()))
  2769. if (uop->getOpcode() == UO_AddrOf)
  2770. return uop->getSubExpr();
  2771. return nullptr;
  2772. }
  2773. /// Emit an argument that's being passed call-by-writeback. That is,
  2774. /// we are passing the address of an __autoreleased temporary; it
  2775. /// might be copy-initialized with the current value of the given
  2776. /// address, but it will definitely be copied out of after the call.
  2777. static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args,
  2778. const ObjCIndirectCopyRestoreExpr *CRE) {
  2779. LValue srcLV;
  2780. // Make an optimistic effort to emit the address as an l-value.
  2781. // This can fail if the argument expression is more complicated.
  2782. if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) {
  2783. srcLV = CGF.EmitLValue(lvExpr);
  2784. // Otherwise, just emit it as a scalar.
  2785. } else {
  2786. Address srcAddr = CGF.EmitPointerWithAlignment(CRE->getSubExpr());
  2787. QualType srcAddrType =
  2788. CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
  2789. srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType);
  2790. }
  2791. Address srcAddr = srcLV.getAddress();
  2792. // The dest and src types don't necessarily match in LLVM terms
  2793. // because of the crazy ObjC compatibility rules.
  2794. llvm::PointerType *destType =
  2795. cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
  2796. // If the address is a constant null, just pass the appropriate null.
  2797. if (isProvablyNull(srcAddr.getPointer())) {
  2798. args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
  2799. CRE->getType());
  2800. return;
  2801. }
  2802. // Create the temporary.
  2803. Address temp = CGF.CreateTempAlloca(destType->getElementType(),
  2804. CGF.getPointerAlign(),
  2805. "icr.temp");
  2806. // Loading an l-value can introduce a cleanup if the l-value is __weak,
  2807. // and that cleanup will be conditional if we can't prove that the l-value
  2808. // isn't null, so we need to register a dominating point so that the cleanups
  2809. // system will make valid IR.
  2810. CodeGenFunction::ConditionalEvaluation condEval(CGF);
  2811. // Zero-initialize it if we're not doing a copy-initialization.
  2812. bool shouldCopy = CRE->shouldCopy();
  2813. if (!shouldCopy) {
  2814. llvm::Value *null =
  2815. llvm::ConstantPointerNull::get(
  2816. cast<llvm::PointerType>(destType->getElementType()));
  2817. CGF.Builder.CreateStore(null, temp);
  2818. }
  2819. llvm::BasicBlock *contBB = nullptr;
  2820. llvm::BasicBlock *originBB = nullptr;
  2821. // If the address is *not* known to be non-null, we need to switch.
  2822. llvm::Value *finalArgument;
  2823. bool provablyNonNull = llvm::isKnownNonZero(srcAddr.getPointer(),
  2824. CGF.CGM.getDataLayout());
  2825. if (provablyNonNull) {
  2826. finalArgument = temp.getPointer();
  2827. } else {
  2828. llvm::Value *isNull =
  2829. CGF.Builder.CreateIsNull(srcAddr.getPointer(), "icr.isnull");
  2830. finalArgument = CGF.Builder.CreateSelect(isNull,
  2831. llvm::ConstantPointerNull::get(destType),
  2832. temp.getPointer(), "icr.argument");
  2833. // If we need to copy, then the load has to be conditional, which
  2834. // means we need control flow.
  2835. if (shouldCopy) {
  2836. originBB = CGF.Builder.GetInsertBlock();
  2837. contBB = CGF.createBasicBlock("icr.cont");
  2838. llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
  2839. CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
  2840. CGF.EmitBlock(copyBB);
  2841. condEval.begin(CGF);
  2842. }
  2843. }
  2844. llvm::Value *valueToUse = nullptr;
  2845. // Perform a copy if necessary.
  2846. if (shouldCopy) {
  2847. RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation());
  2848. assert(srcRV.isScalar());
  2849. llvm::Value *src = srcRV.getScalarVal();
  2850. src = CGF.Builder.CreateBitCast(src, destType->getElementType(),
  2851. "icr.cast");
  2852. // Use an ordinary store, not a store-to-lvalue.
  2853. CGF.Builder.CreateStore(src, temp);
  2854. // If optimization is enabled, and the value was held in a
  2855. // __strong variable, we need to tell the optimizer that this
  2856. // value has to stay alive until we're doing the store back.
  2857. // This is because the temporary is effectively unretained,
  2858. // and so otherwise we can violate the high-level semantics.
  2859. if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 &&
  2860. srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) {
  2861. valueToUse = src;
  2862. }
  2863. }
  2864. // Finish the control flow if we needed it.
  2865. if (shouldCopy && !provablyNonNull) {
  2866. llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock();
  2867. CGF.EmitBlock(contBB);
  2868. // Make a phi for the value to intrinsically use.
  2869. if (valueToUse) {
  2870. llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2,
  2871. "icr.to-use");
  2872. phiToUse->addIncoming(valueToUse, copyBB);
  2873. phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()),
  2874. originBB);
  2875. valueToUse = phiToUse;
  2876. }
  2877. condEval.end(CGF);
  2878. }
  2879. args.addWriteback(srcLV, temp, valueToUse);
  2880. args.add(RValue::get(finalArgument), CRE->getType());
  2881. }
  2882. void CallArgList::allocateArgumentMemory(CodeGenFunction &CGF) {
  2883. assert(!StackBase);
  2884. // Save the stack.
  2885. llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stacksave);
  2886. StackBase = CGF.Builder.CreateCall(F, {}, "inalloca.save");
  2887. }
  2888. void CallArgList::freeArgumentMemory(CodeGenFunction &CGF) const {
  2889. if (StackBase) {
  2890. // Restore the stack after the call.
  2891. llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
  2892. CGF.Builder.CreateCall(F, StackBase);
  2893. }
  2894. }
  2895. void CodeGenFunction::EmitNonNullArgCheck(RValue RV, QualType ArgType,
  2896. SourceLocation ArgLoc,
  2897. AbstractCallee AC,
  2898. unsigned ParmNum) {
  2899. if (!AC.getDecl() || !(SanOpts.has(SanitizerKind::NonnullAttribute) ||
  2900. SanOpts.has(SanitizerKind::NullabilityArg)))
  2901. return;
  2902. // The param decl may be missing in a variadic function.
  2903. auto PVD = ParmNum < AC.getNumParams() ? AC.getParamDecl(ParmNum) : nullptr;
  2904. unsigned ArgNo = PVD ? PVD->getFunctionScopeIndex() : ParmNum;
  2905. // Prefer the nonnull attribute if it's present.
  2906. const NonNullAttr *NNAttr = nullptr;
  2907. if (SanOpts.has(SanitizerKind::NonnullAttribute))
  2908. NNAttr = getNonNullAttr(AC.getDecl(), PVD, ArgType, ArgNo);
  2909. bool CanCheckNullability = false;
  2910. if (SanOpts.has(SanitizerKind::NullabilityArg) && !NNAttr && PVD) {
  2911. auto Nullability = PVD->getType()->getNullability(getContext());
  2912. CanCheckNullability = Nullability &&
  2913. *Nullability == NullabilityKind::NonNull &&
  2914. PVD->getTypeSourceInfo();
  2915. }
  2916. if (!NNAttr && !CanCheckNullability)
  2917. return;
  2918. SourceLocation AttrLoc;
  2919. SanitizerMask CheckKind;
  2920. SanitizerHandler Handler;
  2921. if (NNAttr) {
  2922. AttrLoc = NNAttr->getLocation();
  2923. CheckKind = SanitizerKind::NonnullAttribute;
  2924. Handler = SanitizerHandler::NonnullArg;
  2925. } else {
  2926. AttrLoc = PVD->getTypeSourceInfo()->getTypeLoc().findNullabilityLoc();
  2927. CheckKind = SanitizerKind::NullabilityArg;
  2928. Handler = SanitizerHandler::NullabilityArg;
  2929. }
  2930. SanitizerScope SanScope(this);
  2931. assert(RV.isScalar());
  2932. llvm::Value *V = RV.getScalarVal();
  2933. llvm::Value *Cond =
  2934. Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType()));
  2935. llvm::Constant *StaticData[] = {
  2936. EmitCheckSourceLocation(ArgLoc), EmitCheckSourceLocation(AttrLoc),
  2937. llvm::ConstantInt::get(Int32Ty, ArgNo + 1),
  2938. };
  2939. EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, None);
  2940. }
  2941. void CodeGenFunction::EmitCallArgs(
  2942. CallArgList &Args, ArrayRef<QualType> ArgTypes,
  2943. llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
  2944. AbstractCallee AC, unsigned ParamsToSkip, EvaluationOrder Order) {
  2945. assert((int)ArgTypes.size() == (ArgRange.end() - ArgRange.begin()));
  2946. // We *have* to evaluate arguments from right to left in the MS C++ ABI,
  2947. // because arguments are destroyed left to right in the callee. As a special
  2948. // case, there are certain language constructs that require left-to-right
  2949. // evaluation, and in those cases we consider the evaluation order requirement
  2950. // to trump the "destruction order is reverse construction order" guarantee.
  2951. bool LeftToRight =
  2952. CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()
  2953. ? Order == EvaluationOrder::ForceLeftToRight
  2954. : Order != EvaluationOrder::ForceRightToLeft;
  2955. auto MaybeEmitImplicitObjectSize = [&](unsigned I, const Expr *Arg,
  2956. RValue EmittedArg) {
  2957. if (!AC.hasFunctionDecl() || I >= AC.getNumParams())
  2958. return;
  2959. auto *PS = AC.getParamDecl(I)->getAttr<PassObjectSizeAttr>();
  2960. if (PS == nullptr)
  2961. return;
  2962. const auto &Context = getContext();
  2963. auto SizeTy = Context.getSizeType();
  2964. auto T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
  2965. assert(EmittedArg.getScalarVal() && "We emitted nothing for the arg?");
  2966. llvm::Value *V = evaluateOrEmitBuiltinObjectSize(Arg, PS->getType(), T,
  2967. EmittedArg.getScalarVal());
  2968. Args.add(RValue::get(V), SizeTy);
  2969. // If we're emitting args in reverse, be sure to do so with
  2970. // pass_object_size, as well.
  2971. if (!LeftToRight)
  2972. std::swap(Args.back(), *(&Args.back() - 1));
  2973. };
  2974. // Insert a stack save if we're going to need any inalloca args.
  2975. bool HasInAllocaArgs = false;
  2976. if (CGM.getTarget().getCXXABI().isMicrosoft()) {
  2977. for (ArrayRef<QualType>::iterator I = ArgTypes.begin(), E = ArgTypes.end();
  2978. I != E && !HasInAllocaArgs; ++I)
  2979. HasInAllocaArgs = isInAllocaArgument(CGM.getCXXABI(), *I);
  2980. if (HasInAllocaArgs) {
  2981. assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
  2982. Args.allocateArgumentMemory(*this);
  2983. }
  2984. }
  2985. // Evaluate each argument in the appropriate order.
  2986. size_t CallArgsStart = Args.size();
  2987. for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) {
  2988. unsigned Idx = LeftToRight ? I : E - I - 1;
  2989. CallExpr::const_arg_iterator Arg = ArgRange.begin() + Idx;
  2990. unsigned InitialArgSize = Args.size();
  2991. // If *Arg is an ObjCIndirectCopyRestoreExpr, check that either the types of
  2992. // the argument and parameter match or the objc method is parameterized.
  2993. assert((!isa<ObjCIndirectCopyRestoreExpr>(*Arg) ||
  2994. getContext().hasSameUnqualifiedType((*Arg)->getType(),
  2995. ArgTypes[Idx]) ||
  2996. (isa<ObjCMethodDecl>(AC.getDecl()) &&
  2997. isObjCMethodWithTypeParams(cast<ObjCMethodDecl>(AC.getDecl())))) &&
  2998. "Argument and parameter types don't match");
  2999. EmitCallArg(Args, *Arg, ArgTypes[Idx]);
  3000. // In particular, we depend on it being the last arg in Args, and the
  3001. // objectsize bits depend on there only being one arg if !LeftToRight.
  3002. assert(InitialArgSize + 1 == Args.size() &&
  3003. "The code below depends on only adding one arg per EmitCallArg");
  3004. (void)InitialArgSize;
  3005. // Since pointer argument are never emitted as LValue, it is safe to emit
  3006. // non-null argument check for r-value only.
  3007. if (!Args.back().hasLValue()) {
  3008. RValue RVArg = Args.back().getKnownRValue();
  3009. EmitNonNullArgCheck(RVArg, ArgTypes[Idx], (*Arg)->getExprLoc(), AC,
  3010. ParamsToSkip + Idx);
  3011. // @llvm.objectsize should never have side-effects and shouldn't need
  3012. // destruction/cleanups, so we can safely "emit" it after its arg,
  3013. // regardless of right-to-leftness
  3014. MaybeEmitImplicitObjectSize(Idx, *Arg, RVArg);
  3015. }
  3016. }
  3017. if (!LeftToRight) {
  3018. // Un-reverse the arguments we just evaluated so they match up with the LLVM
  3019. // IR function.
  3020. std::reverse(Args.begin() + CallArgsStart, Args.end());
  3021. }
  3022. }
  3023. namespace {
  3024. struct DestroyUnpassedArg final : EHScopeStack::Cleanup {
  3025. DestroyUnpassedArg(Address Addr, QualType Ty)
  3026. : Addr(Addr), Ty(Ty) {}
  3027. Address Addr;
  3028. QualType Ty;
  3029. void Emit(CodeGenFunction &CGF, Flags flags) override {
  3030. QualType::DestructionKind DtorKind = Ty.isDestructedType();
  3031. if (DtorKind == QualType::DK_cxx_destructor) {
  3032. const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();
  3033. assert(!Dtor->isTrivial());
  3034. CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false,
  3035. /*Delegating=*/false, Addr);
  3036. } else {
  3037. CGF.callCStructDestructor(CGF.MakeAddrLValue(Addr, Ty));
  3038. }
  3039. }
  3040. };
  3041. struct DisableDebugLocationUpdates {
  3042. CodeGenFunction &CGF;
  3043. bool disabledDebugInfo;
  3044. DisableDebugLocationUpdates(CodeGenFunction &CGF, const Expr *E) : CGF(CGF) {
  3045. if ((disabledDebugInfo = isa<CXXDefaultArgExpr>(E) && CGF.getDebugInfo()))
  3046. CGF.disableDebugInfo();
  3047. }
  3048. ~DisableDebugLocationUpdates() {
  3049. if (disabledDebugInfo)
  3050. CGF.enableDebugInfo();
  3051. }
  3052. };
  3053. } // end anonymous namespace
  3054. RValue CallArg::getRValue(CodeGenFunction &CGF) const {
  3055. if (!HasLV)
  3056. return RV;
  3057. LValue Copy = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty), Ty);
  3058. CGF.EmitAggregateCopy(Copy, LV, Ty, AggValueSlot::DoesNotOverlap,
  3059. LV.isVolatile());
  3060. IsUsed = true;
  3061. return RValue::getAggregate(Copy.getAddress());
  3062. }
  3063. void CallArg::copyInto(CodeGenFunction &CGF, Address Addr) const {
  3064. LValue Dst = CGF.MakeAddrLValue(Addr, Ty);
  3065. if (!HasLV && RV.isScalar())
  3066. CGF.EmitStoreOfScalar(RV.getScalarVal(), Dst, /*init=*/true);
  3067. else if (!HasLV && RV.isComplex())
  3068. CGF.EmitStoreOfComplex(RV.getComplexVal(), Dst, /*init=*/true);
  3069. else {
  3070. auto Addr = HasLV ? LV.getAddress() : RV.getAggregateAddress();
  3071. LValue SrcLV = CGF.MakeAddrLValue(Addr, Ty);
  3072. // We assume that call args are never copied into subobjects.
  3073. CGF.EmitAggregateCopy(Dst, SrcLV, Ty, AggValueSlot::DoesNotOverlap,
  3074. HasLV ? LV.isVolatileQualified()
  3075. : RV.isVolatileQualified());
  3076. }
  3077. IsUsed = true;
  3078. }
  3079. void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
  3080. QualType type) {
  3081. DisableDebugLocationUpdates Dis(*this, E);
  3082. if (const ObjCIndirectCopyRestoreExpr *CRE
  3083. = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
  3084. assert(getLangOpts().ObjCAutoRefCount);
  3085. return emitWritebackArg(*this, args, CRE);
  3086. }
  3087. assert(type->isReferenceType() == E->isGLValue() &&
  3088. "reference binding to unmaterialized r-value!");
  3089. if (E->isGLValue()) {
  3090. assert(E->getObjectKind() == OK_Ordinary);
  3091. return args.add(EmitReferenceBindingToExpr(E), type);
  3092. }
  3093. bool HasAggregateEvalKind = hasAggregateEvaluationKind(type);
  3094. // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee.
  3095. // However, we still have to push an EH-only cleanup in case we unwind before
  3096. // we make it to the call.
  3097. if (HasAggregateEvalKind &&
  3098. type->getAs<RecordType>()->getDecl()->isParamDestroyedInCallee()) {
  3099. // If we're using inalloca, use the argument memory. Otherwise, use a
  3100. // temporary.
  3101. AggValueSlot Slot;
  3102. if (args.isUsingInAlloca())
  3103. Slot = createPlaceholderSlot(*this, type);
  3104. else
  3105. Slot = CreateAggTemp(type, "agg.tmp");
  3106. bool DestroyedInCallee = true, NeedsEHCleanup = true;
  3107. if (const auto *RD = type->getAsCXXRecordDecl())
  3108. DestroyedInCallee = RD->hasNonTrivialDestructor();
  3109. else
  3110. NeedsEHCleanup = needsEHCleanup(type.isDestructedType());
  3111. if (DestroyedInCallee)
  3112. Slot.setExternallyDestructed();
  3113. EmitAggExpr(E, Slot);
  3114. RValue RV = Slot.asRValue();
  3115. args.add(RV, type);
  3116. if (DestroyedInCallee && NeedsEHCleanup) {
  3117. // Create a no-op GEP between the placeholder and the cleanup so we can
  3118. // RAUW it successfully. It also serves as a marker of the first
  3119. // instruction where the cleanup is active.
  3120. pushFullExprCleanup<DestroyUnpassedArg>(EHCleanup, Slot.getAddress(),
  3121. type);
  3122. // This unreachable is a temporary marker which will be removed later.
  3123. llvm::Instruction *IsActive = Builder.CreateUnreachable();
  3124. args.addArgCleanupDeactivation(EHStack.getInnermostEHScope(), IsActive);
  3125. }
  3126. return;
  3127. }
  3128. if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) &&
  3129. cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
  3130. LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
  3131. assert(L.isSimple());
  3132. args.addUncopiedAggregate(L, type);
  3133. return;
  3134. }
  3135. args.add(EmitAnyExprToTemp(E), type);
  3136. }
  3137. QualType CodeGenFunction::getVarArgType(const Expr *Arg) {
  3138. // System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC
  3139. // implicitly widens null pointer constants that are arguments to varargs
  3140. // functions to pointer-sized ints.
  3141. if (!getTarget().getTriple().isOSWindows())
  3142. return Arg->getType();
  3143. if (Arg->getType()->isIntegerType() &&
  3144. getContext().getTypeSize(Arg->getType()) <
  3145. getContext().getTargetInfo().getPointerWidth(0) &&
  3146. Arg->isNullPointerConstant(getContext(),
  3147. Expr::NPC_ValueDependentIsNotNull)) {
  3148. return getContext().getIntPtrType();
  3149. }
  3150. return Arg->getType();
  3151. }
  3152. // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
  3153. // optimizer it can aggressively ignore unwind edges.
  3154. void
  3155. CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {
  3156. if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&
  3157. !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
  3158. Inst->setMetadata("clang.arc.no_objc_arc_exceptions",
  3159. CGM.getNoObjCARCExceptionsMetadata());
  3160. }
  3161. /// Emits a call to the given no-arguments nounwind runtime function.
  3162. llvm::CallInst *
  3163. CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee,
  3164. const llvm::Twine &name) {
  3165. return EmitNounwindRuntimeCall(callee, None, name);
  3166. }
  3167. /// Emits a call to the given nounwind runtime function.
  3168. llvm::CallInst *
  3169. CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee,
  3170. ArrayRef<llvm::Value*> args,
  3171. const llvm::Twine &name) {
  3172. llvm::CallInst *call = EmitRuntimeCall(callee, args, name);
  3173. call->setDoesNotThrow();
  3174. return call;
  3175. }
  3176. /// Emits a simple call (never an invoke) to the given no-arguments
  3177. /// runtime function.
  3178. llvm::CallInst *
  3179. CodeGenFunction::EmitRuntimeCall(llvm::Value *callee,
  3180. const llvm::Twine &name) {
  3181. return EmitRuntimeCall(callee, None, name);
  3182. }
  3183. // Calls which may throw must have operand bundles indicating which funclet
  3184. // they are nested within.
  3185. SmallVector<llvm::OperandBundleDef, 1>
  3186. CodeGenFunction::getBundlesForFunclet(llvm::Value *Callee) {
  3187. SmallVector<llvm::OperandBundleDef, 1> BundleList;
  3188. // There is no need for a funclet operand bundle if we aren't inside a
  3189. // funclet.
  3190. if (!CurrentFuncletPad)
  3191. return BundleList;
  3192. // Skip intrinsics which cannot throw.
  3193. auto *CalleeFn = dyn_cast<llvm::Function>(Callee->stripPointerCasts());
  3194. if (CalleeFn && CalleeFn->isIntrinsic() && CalleeFn->doesNotThrow())
  3195. return BundleList;
  3196. BundleList.emplace_back("funclet", CurrentFuncletPad);
  3197. return BundleList;
  3198. }
  3199. /// Emits a simple call (never an invoke) to the given runtime function.
  3200. llvm::CallInst *
  3201. CodeGenFunction::EmitRuntimeCall(llvm::Value *callee,
  3202. ArrayRef<llvm::Value*> args,
  3203. const llvm::Twine &name) {
  3204. llvm::CallInst *call =
  3205. Builder.CreateCall(callee, args, getBundlesForFunclet(callee), name);
  3206. call->setCallingConv(getRuntimeCC());
  3207. return call;
  3208. }
  3209. /// Emits a call or invoke to the given noreturn runtime function.
  3210. void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
  3211. ArrayRef<llvm::Value*> args) {
  3212. SmallVector<llvm::OperandBundleDef, 1> BundleList =
  3213. getBundlesForFunclet(callee);
  3214. if (getInvokeDest()) {
  3215. llvm::InvokeInst *invoke =
  3216. Builder.CreateInvoke(callee,
  3217. getUnreachableBlock(),
  3218. getInvokeDest(),
  3219. args,
  3220. BundleList);
  3221. invoke->setDoesNotReturn();
  3222. invoke->setCallingConv(getRuntimeCC());
  3223. } else {
  3224. llvm::CallInst *call = Builder.CreateCall(callee, args, BundleList);
  3225. call->setDoesNotReturn();
  3226. call->setCallingConv(getRuntimeCC());
  3227. Builder.CreateUnreachable();
  3228. }
  3229. }
  3230. /// Emits a call or invoke instruction to the given nullary runtime function.
  3231. llvm::CallSite
  3232. CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee,
  3233. const Twine &name) {
  3234. return EmitRuntimeCallOrInvoke(callee, None, name);
  3235. }
  3236. /// Emits a call or invoke instruction to the given runtime function.
  3237. llvm::CallSite
  3238. CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee,
  3239. ArrayRef<llvm::Value*> args,
  3240. const Twine &name) {
  3241. llvm::CallSite callSite = EmitCallOrInvoke(callee, args, name);
  3242. callSite.setCallingConv(getRuntimeCC());
  3243. return callSite;
  3244. }
  3245. /// Emits a call or invoke instruction to the given function, depending
  3246. /// on the current state of the EH stack.
  3247. llvm::CallSite
  3248. CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
  3249. ArrayRef<llvm::Value *> Args,
  3250. const Twine &Name) {
  3251. llvm::BasicBlock *InvokeDest = getInvokeDest();
  3252. SmallVector<llvm::OperandBundleDef, 1> BundleList =
  3253. getBundlesForFunclet(Callee);
  3254. llvm::Instruction *Inst;
  3255. if (!InvokeDest)
  3256. Inst = Builder.CreateCall(Callee, Args, BundleList, Name);
  3257. else {
  3258. llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
  3259. Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, BundleList,
  3260. Name);
  3261. EmitBlock(ContBB);
  3262. }
  3263. // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
  3264. // optimizer it can aggressively ignore unwind edges.
  3265. if (CGM.getLangOpts().ObjCAutoRefCount)
  3266. AddObjCARCExceptionMetadata(Inst);
  3267. return llvm::CallSite(Inst);
  3268. }
  3269. void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old,
  3270. llvm::Value *New) {
  3271. DeferredReplacements.push_back(std::make_pair(Old, New));
  3272. }
  3273. RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
  3274. const CGCallee &Callee,
  3275. ReturnValueSlot ReturnValue,
  3276. const CallArgList &CallArgs,
  3277. llvm::Instruction **callOrInvoke,
  3278. SourceLocation Loc) {
  3279. // FIXME: We no longer need the types from CallArgs; lift up and simplify.
  3280. assert(Callee.isOrdinary() || Callee.isVirtual());
  3281. // Handle struct-return functions by passing a pointer to the
  3282. // location that we would like to return into.
  3283. QualType RetTy = CallInfo.getReturnType();
  3284. const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
  3285. llvm::FunctionType *IRFuncTy = Callee.getFunctionType();
  3286. // 1. Set up the arguments.
  3287. // If we're using inalloca, insert the allocation after the stack save.
  3288. // FIXME: Do this earlier rather than hacking it in here!
  3289. Address ArgMemory = Address::invalid();
  3290. const llvm::StructLayout *ArgMemoryLayout = nullptr;
  3291. if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) {
  3292. const llvm::DataLayout &DL = CGM.getDataLayout();
  3293. ArgMemoryLayout = DL.getStructLayout(ArgStruct);
  3294. llvm::Instruction *IP = CallArgs.getStackBase();
  3295. llvm::AllocaInst *AI;
  3296. if (IP) {
  3297. IP = IP->getNextNode();
  3298. AI = new llvm::AllocaInst(ArgStruct, DL.getAllocaAddrSpace(),
  3299. "argmem", IP);
  3300. } else {
  3301. AI = CreateTempAlloca(ArgStruct, "argmem");
  3302. }
  3303. auto Align = CallInfo.getArgStructAlignment();
  3304. AI->setAlignment(Align.getQuantity());
  3305. AI->setUsedWithInAlloca(true);
  3306. assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca());
  3307. ArgMemory = Address(AI, Align);
  3308. }
  3309. // Helper function to drill into the inalloca allocation.
  3310. auto createInAllocaStructGEP = [&](unsigned FieldIndex) -> Address {
  3311. auto FieldOffset =
  3312. CharUnits::fromQuantity(ArgMemoryLayout->getElementOffset(FieldIndex));
  3313. return Builder.CreateStructGEP(ArgMemory, FieldIndex, FieldOffset);
  3314. };
  3315. ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), CallInfo);
  3316. SmallVector<llvm::Value *, 16> IRCallArgs(IRFunctionArgs.totalIRArgs());
  3317. // If the call returns a temporary with struct return, create a temporary
  3318. // alloca to hold the result, unless one is given to us.
  3319. Address SRetPtr = Address::invalid();
  3320. Address SRetAlloca = Address::invalid();
  3321. llvm::Value *UnusedReturnSizePtr = nullptr;
  3322. if (RetAI.isIndirect() || RetAI.isInAlloca() || RetAI.isCoerceAndExpand()) {
  3323. if (!ReturnValue.isNull()) {
  3324. SRetPtr = ReturnValue.getValue();
  3325. } else {
  3326. SRetPtr = CreateMemTemp(RetTy, "tmp", &SRetAlloca);
  3327. if (HaveInsertPoint() && ReturnValue.isUnused()) {
  3328. uint64_t size =
  3329. CGM.getDataLayout().getTypeAllocSize(ConvertTypeForMem(RetTy));
  3330. UnusedReturnSizePtr = EmitLifetimeStart(size, SRetAlloca.getPointer());
  3331. }
  3332. }
  3333. if (IRFunctionArgs.hasSRetArg()) {
  3334. IRCallArgs[IRFunctionArgs.getSRetArgNo()] = SRetPtr.getPointer();
  3335. } else if (RetAI.isInAlloca()) {
  3336. Address Addr = createInAllocaStructGEP(RetAI.getInAllocaFieldIndex());
  3337. Builder.CreateStore(SRetPtr.getPointer(), Addr);
  3338. }
  3339. }
  3340. Address swiftErrorTemp = Address::invalid();
  3341. Address swiftErrorArg = Address::invalid();
  3342. // Translate all of the arguments as necessary to match the IR lowering.
  3343. assert(CallInfo.arg_size() == CallArgs.size() &&
  3344. "Mismatch between function signature & arguments.");
  3345. unsigned ArgNo = 0;
  3346. CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
  3347. for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
  3348. I != E; ++I, ++info_it, ++ArgNo) {
  3349. const ABIArgInfo &ArgInfo = info_it->info;
  3350. // Insert a padding argument to ensure proper alignment.
  3351. if (IRFunctionArgs.hasPaddingArg(ArgNo))
  3352. IRCallArgs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
  3353. llvm::UndefValue::get(ArgInfo.getPaddingType());
  3354. unsigned FirstIRArg, NumIRArgs;
  3355. std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
  3356. switch (ArgInfo.getKind()) {
  3357. case ABIArgInfo::InAlloca: {
  3358. assert(NumIRArgs == 0);
  3359. assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
  3360. if (I->isAggregate()) {
  3361. // Replace the placeholder with the appropriate argument slot GEP.
  3362. Address Addr = I->hasLValue()
  3363. ? I->getKnownLValue().getAddress()
  3364. : I->getKnownRValue().getAggregateAddress();
  3365. llvm::Instruction *Placeholder =
  3366. cast<llvm::Instruction>(Addr.getPointer());
  3367. CGBuilderTy::InsertPoint IP = Builder.saveIP();
  3368. Builder.SetInsertPoint(Placeholder);
  3369. Addr = createInAllocaStructGEP(ArgInfo.getInAllocaFieldIndex());
  3370. Builder.restoreIP(IP);
  3371. deferPlaceholderReplacement(Placeholder, Addr.getPointer());
  3372. } else {
  3373. // Store the RValue into the argument struct.
  3374. Address Addr = createInAllocaStructGEP(ArgInfo.getInAllocaFieldIndex());
  3375. unsigned AS = Addr.getType()->getPointerAddressSpace();
  3376. llvm::Type *MemType = ConvertTypeForMem(I->Ty)->getPointerTo(AS);
  3377. // There are some cases where a trivial bitcast is not avoidable. The
  3378. // definition of a type later in a translation unit may change it's type
  3379. // from {}* to (%struct.foo*)*.
  3380. if (Addr.getType() != MemType)
  3381. Addr = Builder.CreateBitCast(Addr, MemType);
  3382. I->copyInto(*this, Addr);
  3383. }
  3384. break;
  3385. }
  3386. case ABIArgInfo::Indirect: {
  3387. assert(NumIRArgs == 1);
  3388. if (!I->isAggregate()) {
  3389. // Make a temporary alloca to pass the argument.
  3390. Address Addr = CreateMemTempWithoutCast(
  3391. I->Ty, ArgInfo.getIndirectAlign(), "indirect-arg-temp");
  3392. IRCallArgs[FirstIRArg] = Addr.getPointer();
  3393. I->copyInto(*this, Addr);
  3394. } else {
  3395. // We want to avoid creating an unnecessary temporary+copy here;
  3396. // however, we need one in three cases:
  3397. // 1. If the argument is not byval, and we are required to copy the
  3398. // source. (This case doesn't occur on any common architecture.)
  3399. // 2. If the argument is byval, RV is not sufficiently aligned, and
  3400. // we cannot force it to be sufficiently aligned.
  3401. // 3. If the argument is byval, but RV is not located in default
  3402. // or alloca address space.
  3403. Address Addr = I->hasLValue()
  3404. ? I->getKnownLValue().getAddress()
  3405. : I->getKnownRValue().getAggregateAddress();
  3406. llvm::Value *V = Addr.getPointer();
  3407. CharUnits Align = ArgInfo.getIndirectAlign();
  3408. const llvm::DataLayout *TD = &CGM.getDataLayout();
  3409. assert((FirstIRArg >= IRFuncTy->getNumParams() ||
  3410. IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace() ==
  3411. TD->getAllocaAddrSpace()) &&
  3412. "indirect argument must be in alloca address space");
  3413. bool NeedCopy = false;
  3414. if (Addr.getAlignment() < Align &&
  3415. llvm::getOrEnforceKnownAlignment(V, Align.getQuantity(), *TD) <
  3416. Align.getQuantity()) {
  3417. NeedCopy = true;
  3418. } else if (I->hasLValue()) {
  3419. auto LV = I->getKnownLValue();
  3420. auto AS = LV.getAddressSpace();
  3421. if ((!ArgInfo.getIndirectByVal() &&
  3422. (LV.getAlignment() >=
  3423. getContext().getTypeAlignInChars(I->Ty)))) {
  3424. NeedCopy = true;
  3425. }
  3426. if (!getLangOpts().OpenCL) {
  3427. if ((ArgInfo.getIndirectByVal() &&
  3428. (AS != LangAS::Default &&
  3429. AS != CGM.getASTAllocaAddressSpace()))) {
  3430. NeedCopy = true;
  3431. }
  3432. }
  3433. // For OpenCL even if RV is located in default or alloca address space
  3434. // we don't want to perform address space cast for it.
  3435. else if ((ArgInfo.getIndirectByVal() &&
  3436. Addr.getType()->getAddressSpace() != IRFuncTy->
  3437. getParamType(FirstIRArg)->getPointerAddressSpace())) {
  3438. NeedCopy = true;
  3439. }
  3440. }
  3441. if (NeedCopy) {
  3442. // Create an aligned temporary, and copy to it.
  3443. Address AI = CreateMemTempWithoutCast(
  3444. I->Ty, ArgInfo.getIndirectAlign(), "byval-temp");
  3445. IRCallArgs[FirstIRArg] = AI.getPointer();
  3446. I->copyInto(*this, AI);
  3447. } else {
  3448. // Skip the extra memcpy call.
  3449. auto *T = V->getType()->getPointerElementType()->getPointerTo(
  3450. CGM.getDataLayout().getAllocaAddrSpace());
  3451. IRCallArgs[FirstIRArg] = getTargetHooks().performAddrSpaceCast(
  3452. *this, V, LangAS::Default, CGM.getASTAllocaAddressSpace(), T,
  3453. true);
  3454. }
  3455. }
  3456. break;
  3457. }
  3458. case ABIArgInfo::Ignore:
  3459. assert(NumIRArgs == 0);
  3460. break;
  3461. case ABIArgInfo::Extend:
  3462. case ABIArgInfo::Direct: {
  3463. if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
  3464. ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
  3465. ArgInfo.getDirectOffset() == 0) {
  3466. assert(NumIRArgs == 1);
  3467. llvm::Value *V;
  3468. if (!I->isAggregate())
  3469. V = I->getKnownRValue().getScalarVal();
  3470. else
  3471. V = Builder.CreateLoad(
  3472. I->hasLValue() ? I->getKnownLValue().getAddress()
  3473. : I->getKnownRValue().getAggregateAddress());
  3474. // Implement swifterror by copying into a new swifterror argument.
  3475. // We'll write back in the normal path out of the call.
  3476. if (CallInfo.getExtParameterInfo(ArgNo).getABI()
  3477. == ParameterABI::SwiftErrorResult) {
  3478. assert(!swiftErrorTemp.isValid() && "multiple swifterror args");
  3479. QualType pointeeTy = I->Ty->getPointeeType();
  3480. swiftErrorArg =
  3481. Address(V, getContext().getTypeAlignInChars(pointeeTy));
  3482. swiftErrorTemp =
  3483. CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");
  3484. V = swiftErrorTemp.getPointer();
  3485. cast<llvm::AllocaInst>(V)->setSwiftError(true);
  3486. llvm::Value *errorValue = Builder.CreateLoad(swiftErrorArg);
  3487. Builder.CreateStore(errorValue, swiftErrorTemp);
  3488. }
  3489. // We might have to widen integers, but we should never truncate.
  3490. if (ArgInfo.getCoerceToType() != V->getType() &&
  3491. V->getType()->isIntegerTy())
  3492. V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());
  3493. // If the argument doesn't match, perform a bitcast to coerce it. This
  3494. // can happen due to trivial type mismatches.
  3495. if (FirstIRArg < IRFuncTy->getNumParams() &&
  3496. V->getType() != IRFuncTy->getParamType(FirstIRArg))
  3497. V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
  3498. IRCallArgs[FirstIRArg] = V;
  3499. break;
  3500. }
  3501. // FIXME: Avoid the conversion through memory if possible.
  3502. Address Src = Address::invalid();
  3503. if (!I->isAggregate()) {
  3504. Src = CreateMemTemp(I->Ty, "coerce");
  3505. I->copyInto(*this, Src);
  3506. } else {
  3507. Src = I->hasLValue() ? I->getKnownLValue().getAddress()
  3508. : I->getKnownRValue().getAggregateAddress();
  3509. }
  3510. // If the value is offset in memory, apply the offset now.
  3511. Src = emitAddressAtOffset(*this, Src, ArgInfo);
  3512. // Fast-isel and the optimizer generally like scalar values better than
  3513. // FCAs, so we flatten them if this is safe to do for this argument.
  3514. llvm::StructType *STy =
  3515. dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType());
  3516. if (STy && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
  3517. llvm::Type *SrcTy = Src.getType()->getElementType();
  3518. uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy);
  3519. uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy);
  3520. // If the source type is smaller than the destination type of the
  3521. // coerce-to logic, copy the source value into a temp alloca the size
  3522. // of the destination type to allow loading all of it. The bits past
  3523. // the source value are left undef.
  3524. if (SrcSize < DstSize) {
  3525. Address TempAlloca
  3526. = CreateTempAlloca(STy, Src.getAlignment(),
  3527. Src.getName() + ".coerce");
  3528. Builder.CreateMemCpy(TempAlloca, Src, SrcSize);
  3529. Src = TempAlloca;
  3530. } else {
  3531. Src = Builder.CreateBitCast(Src,
  3532. STy->getPointerTo(Src.getAddressSpace()));
  3533. }
  3534. auto SrcLayout = CGM.getDataLayout().getStructLayout(STy);
  3535. assert(NumIRArgs == STy->getNumElements());
  3536. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  3537. auto Offset = CharUnits::fromQuantity(SrcLayout->getElementOffset(i));
  3538. Address EltPtr = Builder.CreateStructGEP(Src, i, Offset);
  3539. llvm::Value *LI = Builder.CreateLoad(EltPtr);
  3540. IRCallArgs[FirstIRArg + i] = LI;
  3541. }
  3542. } else {
  3543. // In the simple case, just pass the coerced loaded value.
  3544. assert(NumIRArgs == 1);
  3545. IRCallArgs[FirstIRArg] =
  3546. CreateCoercedLoad(Src, ArgInfo.getCoerceToType(), *this);
  3547. }
  3548. break;
  3549. }
  3550. case ABIArgInfo::CoerceAndExpand: {
  3551. auto coercionType = ArgInfo.getCoerceAndExpandType();
  3552. auto layout = CGM.getDataLayout().getStructLayout(coercionType);
  3553. llvm::Value *tempSize = nullptr;
  3554. Address addr = Address::invalid();
  3555. Address AllocaAddr = Address::invalid();
  3556. if (I->isAggregate()) {
  3557. addr = I->hasLValue() ? I->getKnownLValue().getAddress()
  3558. : I->getKnownRValue().getAggregateAddress();
  3559. } else {
  3560. RValue RV = I->getKnownRValue();
  3561. assert(RV.isScalar()); // complex should always just be direct
  3562. llvm::Type *scalarType = RV.getScalarVal()->getType();
  3563. auto scalarSize = CGM.getDataLayout().getTypeAllocSize(scalarType);
  3564. auto scalarAlign = CGM.getDataLayout().getPrefTypeAlignment(scalarType);
  3565. // Materialize to a temporary.
  3566. addr = CreateTempAlloca(RV.getScalarVal()->getType(),
  3567. CharUnits::fromQuantity(std::max(
  3568. layout->getAlignment(), scalarAlign)),
  3569. "tmp",
  3570. /*ArraySize=*/nullptr, &AllocaAddr);
  3571. tempSize = EmitLifetimeStart(scalarSize, AllocaAddr.getPointer());
  3572. Builder.CreateStore(RV.getScalarVal(), addr);
  3573. }
  3574. addr = Builder.CreateElementBitCast(addr, coercionType);
  3575. unsigned IRArgPos = FirstIRArg;
  3576. for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
  3577. llvm::Type *eltType = coercionType->getElementType(i);
  3578. if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue;
  3579. Address eltAddr = Builder.CreateStructGEP(addr, i, layout);
  3580. llvm::Value *elt = Builder.CreateLoad(eltAddr);
  3581. IRCallArgs[IRArgPos++] = elt;
  3582. }
  3583. assert(IRArgPos == FirstIRArg + NumIRArgs);
  3584. if (tempSize) {
  3585. EmitLifetimeEnd(tempSize, AllocaAddr.getPointer());
  3586. }
  3587. break;
  3588. }
  3589. case ABIArgInfo::Expand:
  3590. unsigned IRArgPos = FirstIRArg;
  3591. ExpandTypeToArgs(I->Ty, *I, IRFuncTy, IRCallArgs, IRArgPos);
  3592. assert(IRArgPos == FirstIRArg + NumIRArgs);
  3593. break;
  3594. }
  3595. }
  3596. const CGCallee &ConcreteCallee = Callee.prepareConcreteCallee(*this);
  3597. llvm::Value *CalleePtr = ConcreteCallee.getFunctionPointer();
  3598. // If we're using inalloca, set up that argument.
  3599. if (ArgMemory.isValid()) {
  3600. llvm::Value *Arg = ArgMemory.getPointer();
  3601. if (CallInfo.isVariadic()) {
  3602. // When passing non-POD arguments by value to variadic functions, we will
  3603. // end up with a variadic prototype and an inalloca call site. In such
  3604. // cases, we can't do any parameter mismatch checks. Give up and bitcast
  3605. // the callee.
  3606. unsigned CalleeAS = CalleePtr->getType()->getPointerAddressSpace();
  3607. auto FnTy = getTypes().GetFunctionType(CallInfo)->getPointerTo(CalleeAS);
  3608. CalleePtr = Builder.CreateBitCast(CalleePtr, FnTy);
  3609. } else {
  3610. llvm::Type *LastParamTy =
  3611. IRFuncTy->getParamType(IRFuncTy->getNumParams() - 1);
  3612. if (Arg->getType() != LastParamTy) {
  3613. #ifndef NDEBUG
  3614. // Assert that these structs have equivalent element types.
  3615. llvm::StructType *FullTy = CallInfo.getArgStruct();
  3616. llvm::StructType *DeclaredTy = cast<llvm::StructType>(
  3617. cast<llvm::PointerType>(LastParamTy)->getElementType());
  3618. assert(DeclaredTy->getNumElements() == FullTy->getNumElements());
  3619. for (llvm::StructType::element_iterator DI = DeclaredTy->element_begin(),
  3620. DE = DeclaredTy->element_end(),
  3621. FI = FullTy->element_begin();
  3622. DI != DE; ++DI, ++FI)
  3623. assert(*DI == *FI);
  3624. #endif
  3625. Arg = Builder.CreateBitCast(Arg, LastParamTy);
  3626. }
  3627. }
  3628. assert(IRFunctionArgs.hasInallocaArg());
  3629. IRCallArgs[IRFunctionArgs.getInallocaArgNo()] = Arg;
  3630. }
  3631. // 2. Prepare the function pointer.
  3632. // If the callee is a bitcast of a non-variadic function to have a
  3633. // variadic function pointer type, check to see if we can remove the
  3634. // bitcast. This comes up with unprototyped functions.
  3635. //
  3636. // This makes the IR nicer, but more importantly it ensures that we
  3637. // can inline the function at -O0 if it is marked always_inline.
  3638. auto simplifyVariadicCallee = [](llvm::Value *Ptr) -> llvm::Value* {
  3639. llvm::FunctionType *CalleeFT =
  3640. cast<llvm::FunctionType>(Ptr->getType()->getPointerElementType());
  3641. if (!CalleeFT->isVarArg())
  3642. return Ptr;
  3643. llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Ptr);
  3644. if (!CE || CE->getOpcode() != llvm::Instruction::BitCast)
  3645. return Ptr;
  3646. llvm::Function *OrigFn = dyn_cast<llvm::Function>(CE->getOperand(0));
  3647. if (!OrigFn)
  3648. return Ptr;
  3649. llvm::FunctionType *OrigFT = OrigFn->getFunctionType();
  3650. // If the original type is variadic, or if any of the component types
  3651. // disagree, we cannot remove the cast.
  3652. if (OrigFT->isVarArg() ||
  3653. OrigFT->getNumParams() != CalleeFT->getNumParams() ||
  3654. OrigFT->getReturnType() != CalleeFT->getReturnType())
  3655. return Ptr;
  3656. for (unsigned i = 0, e = OrigFT->getNumParams(); i != e; ++i)
  3657. if (OrigFT->getParamType(i) != CalleeFT->getParamType(i))
  3658. return Ptr;
  3659. return OrigFn;
  3660. };
  3661. CalleePtr = simplifyVariadicCallee(CalleePtr);
  3662. // 3. Perform the actual call.
  3663. // Deactivate any cleanups that we're supposed to do immediately before
  3664. // the call.
  3665. if (!CallArgs.getCleanupsToDeactivate().empty())
  3666. deactivateArgCleanupsBeforeCall(*this, CallArgs);
  3667. // Assert that the arguments we computed match up. The IR verifier
  3668. // will catch this, but this is a common enough source of problems
  3669. // during IRGen changes that it's way better for debugging to catch
  3670. // it ourselves here.
  3671. #ifndef NDEBUG
  3672. assert(IRCallArgs.size() == IRFuncTy->getNumParams() || IRFuncTy->isVarArg());
  3673. for (unsigned i = 0; i < IRCallArgs.size(); ++i) {
  3674. // Inalloca argument can have different type.
  3675. if (IRFunctionArgs.hasInallocaArg() &&
  3676. i == IRFunctionArgs.getInallocaArgNo())
  3677. continue;
  3678. if (i < IRFuncTy->getNumParams())
  3679. assert(IRCallArgs[i]->getType() == IRFuncTy->getParamType(i));
  3680. }
  3681. #endif
  3682. // Update the largest vector width if any arguments have vector types.
  3683. for (unsigned i = 0; i < IRCallArgs.size(); ++i) {
  3684. if (auto *VT = dyn_cast<llvm::VectorType>(IRCallArgs[i]->getType()))
  3685. LargestVectorWidth = std::max(LargestVectorWidth,
  3686. VT->getPrimitiveSizeInBits());
  3687. }
  3688. // Compute the calling convention and attributes.
  3689. unsigned CallingConv;
  3690. llvm::AttributeList Attrs;
  3691. CGM.ConstructAttributeList(CalleePtr->getName(), CallInfo,
  3692. Callee.getAbstractInfo(), Attrs, CallingConv,
  3693. /*AttrOnCallSite=*/true);
  3694. // Apply some call-site-specific attributes.
  3695. // TODO: work this into building the attribute set.
  3696. // Apply always_inline to all calls within flatten functions.
  3697. // FIXME: should this really take priority over __try, below?
  3698. if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() &&
  3699. !(Callee.getAbstractInfo().getCalleeDecl().getDecl() &&
  3700. Callee.getAbstractInfo()
  3701. .getCalleeDecl()
  3702. .getDecl()
  3703. ->hasAttr<NoInlineAttr>())) {
  3704. Attrs =
  3705. Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex,
  3706. llvm::Attribute::AlwaysInline);
  3707. }
  3708. // Disable inlining inside SEH __try blocks.
  3709. if (isSEHTryScope()) {
  3710. Attrs =
  3711. Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex,
  3712. llvm::Attribute::NoInline);
  3713. }
  3714. // Decide whether to use a call or an invoke.
  3715. bool CannotThrow;
  3716. if (currentFunctionUsesSEHTry()) {
  3717. // SEH cares about asynchronous exceptions, so everything can "throw."
  3718. CannotThrow = false;
  3719. } else if (isCleanupPadScope() &&
  3720. EHPersonality::get(*this).isMSVCXXPersonality()) {
  3721. // The MSVC++ personality will implicitly terminate the program if an
  3722. // exception is thrown during a cleanup outside of a try/catch.
  3723. // We don't need to model anything in IR to get this behavior.
  3724. CannotThrow = true;
  3725. } else {
  3726. // Otherwise, nounwind call sites will never throw.
  3727. CannotThrow = Attrs.hasAttribute(llvm::AttributeList::FunctionIndex,
  3728. llvm::Attribute::NoUnwind);
  3729. }
  3730. // If we made a temporary, be sure to clean up after ourselves. Note that we
  3731. // can't depend on being inside of an ExprWithCleanups, so we need to manually
  3732. // pop this cleanup later on. Being eager about this is OK, since this
  3733. // temporary is 'invisible' outside of the callee.
  3734. if (UnusedReturnSizePtr)
  3735. pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, SRetAlloca,
  3736. UnusedReturnSizePtr);
  3737. llvm::BasicBlock *InvokeDest = CannotThrow ? nullptr : getInvokeDest();
  3738. SmallVector<llvm::OperandBundleDef, 1> BundleList =
  3739. getBundlesForFunclet(CalleePtr);
  3740. // Emit the actual call/invoke instruction.
  3741. llvm::CallSite CS;
  3742. if (!InvokeDest) {
  3743. CS = Builder.CreateCall(CalleePtr, IRCallArgs, BundleList);
  3744. } else {
  3745. llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
  3746. CS = Builder.CreateInvoke(CalleePtr, Cont, InvokeDest, IRCallArgs,
  3747. BundleList);
  3748. EmitBlock(Cont);
  3749. }
  3750. llvm::Instruction *CI = CS.getInstruction();
  3751. if (callOrInvoke)
  3752. *callOrInvoke = CI;
  3753. // Apply the attributes and calling convention.
  3754. CS.setAttributes(Attrs);
  3755. CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
  3756. // Apply various metadata.
  3757. if (!CI->getType()->isVoidTy())
  3758. CI->setName("call");
  3759. // Update largest vector width from the return type.
  3760. if (auto *VT = dyn_cast<llvm::VectorType>(CI->getType()))
  3761. LargestVectorWidth = std::max(LargestVectorWidth,
  3762. VT->getPrimitiveSizeInBits());
  3763. // Insert instrumentation or attach profile metadata at indirect call sites.
  3764. // For more details, see the comment before the definition of
  3765. // IPVK_IndirectCallTarget in InstrProfData.inc.
  3766. if (!CS.getCalledFunction())
  3767. PGO.valueProfile(Builder, llvm::IPVK_IndirectCallTarget,
  3768. CI, CalleePtr);
  3769. // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
  3770. // optimizer it can aggressively ignore unwind edges.
  3771. if (CGM.getLangOpts().ObjCAutoRefCount)
  3772. AddObjCARCExceptionMetadata(CI);
  3773. // Suppress tail calls if requested.
  3774. if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(CI)) {
  3775. const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl();
  3776. if (TargetDecl && TargetDecl->hasAttr<NotTailCalledAttr>())
  3777. Call->setTailCallKind(llvm::CallInst::TCK_NoTail);
  3778. }
  3779. // 4. Finish the call.
  3780. // If the call doesn't return, finish the basic block and clear the
  3781. // insertion point; this allows the rest of IRGen to discard
  3782. // unreachable code.
  3783. if (CS.doesNotReturn()) {
  3784. if (UnusedReturnSizePtr)
  3785. PopCleanupBlock();
  3786. // Strip away the noreturn attribute to better diagnose unreachable UB.
  3787. if (SanOpts.has(SanitizerKind::Unreachable)) {
  3788. if (auto *F = CS.getCalledFunction())
  3789. F->removeFnAttr(llvm::Attribute::NoReturn);
  3790. CS.removeAttribute(llvm::AttributeList::FunctionIndex,
  3791. llvm::Attribute::NoReturn);
  3792. }
  3793. EmitUnreachable(Loc);
  3794. Builder.ClearInsertionPoint();
  3795. // FIXME: For now, emit a dummy basic block because expr emitters in
  3796. // generally are not ready to handle emitting expressions at unreachable
  3797. // points.
  3798. EnsureInsertPoint();
  3799. // Return a reasonable RValue.
  3800. return GetUndefRValue(RetTy);
  3801. }
  3802. // Perform the swifterror writeback.
  3803. if (swiftErrorTemp.isValid()) {
  3804. llvm::Value *errorResult = Builder.CreateLoad(swiftErrorTemp);
  3805. Builder.CreateStore(errorResult, swiftErrorArg);
  3806. }
  3807. // Emit any call-associated writebacks immediately. Arguably this
  3808. // should happen after any return-value munging.
  3809. if (CallArgs.hasWritebacks())
  3810. emitWritebacks(*this, CallArgs);
  3811. // The stack cleanup for inalloca arguments has to run out of the normal
  3812. // lexical order, so deactivate it and run it manually here.
  3813. CallArgs.freeArgumentMemory(*this);
  3814. // Extract the return value.
  3815. RValue Ret = [&] {
  3816. switch (RetAI.getKind()) {
  3817. case ABIArgInfo::CoerceAndExpand: {
  3818. auto coercionType = RetAI.getCoerceAndExpandType();
  3819. auto layout = CGM.getDataLayout().getStructLayout(coercionType);
  3820. Address addr = SRetPtr;
  3821. addr = Builder.CreateElementBitCast(addr, coercionType);
  3822. assert(CI->getType() == RetAI.getUnpaddedCoerceAndExpandType());
  3823. bool requiresExtract = isa<llvm::StructType>(CI->getType());
  3824. unsigned unpaddedIndex = 0;
  3825. for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
  3826. llvm::Type *eltType = coercionType->getElementType(i);
  3827. if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue;
  3828. Address eltAddr = Builder.CreateStructGEP(addr, i, layout);
  3829. llvm::Value *elt = CI;
  3830. if (requiresExtract)
  3831. elt = Builder.CreateExtractValue(elt, unpaddedIndex++);
  3832. else
  3833. assert(unpaddedIndex == 0);
  3834. Builder.CreateStore(elt, eltAddr);
  3835. }
  3836. // FALLTHROUGH
  3837. LLVM_FALLTHROUGH;
  3838. }
  3839. case ABIArgInfo::InAlloca:
  3840. case ABIArgInfo::Indirect: {
  3841. RValue ret = convertTempToRValue(SRetPtr, RetTy, SourceLocation());
  3842. if (UnusedReturnSizePtr)
  3843. PopCleanupBlock();
  3844. return ret;
  3845. }
  3846. case ABIArgInfo::Ignore:
  3847. // If we are ignoring an argument that had a result, make sure to
  3848. // construct the appropriate return value for our caller.
  3849. return GetUndefRValue(RetTy);
  3850. case ABIArgInfo::Extend:
  3851. case ABIArgInfo::Direct: {
  3852. llvm::Type *RetIRTy = ConvertType(RetTy);
  3853. if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) {
  3854. switch (getEvaluationKind(RetTy)) {
  3855. case TEK_Complex: {
  3856. llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
  3857. llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
  3858. return RValue::getComplex(std::make_pair(Real, Imag));
  3859. }
  3860. case TEK_Aggregate: {
  3861. Address DestPtr = ReturnValue.getValue();
  3862. bool DestIsVolatile = ReturnValue.isVolatile();
  3863. if (!DestPtr.isValid()) {
  3864. DestPtr = CreateMemTemp(RetTy, "agg.tmp");
  3865. DestIsVolatile = false;
  3866. }
  3867. BuildAggStore(*this, CI, DestPtr, DestIsVolatile);
  3868. return RValue::getAggregate(DestPtr);
  3869. }
  3870. case TEK_Scalar: {
  3871. // If the argument doesn't match, perform a bitcast to coerce it. This
  3872. // can happen due to trivial type mismatches.
  3873. llvm::Value *V = CI;
  3874. if (V->getType() != RetIRTy)
  3875. V = Builder.CreateBitCast(V, RetIRTy);
  3876. return RValue::get(V);
  3877. }
  3878. }
  3879. llvm_unreachable("bad evaluation kind");
  3880. }
  3881. Address DestPtr = ReturnValue.getValue();
  3882. bool DestIsVolatile = ReturnValue.isVolatile();
  3883. if (!DestPtr.isValid()) {
  3884. DestPtr = CreateMemTemp(RetTy, "coerce");
  3885. DestIsVolatile = false;
  3886. }
  3887. // If the value is offset in memory, apply the offset now.
  3888. Address StorePtr = emitAddressAtOffset(*this, DestPtr, RetAI);
  3889. CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
  3890. return convertTempToRValue(DestPtr, RetTy, SourceLocation());
  3891. }
  3892. case ABIArgInfo::Expand:
  3893. llvm_unreachable("Invalid ABI kind for return argument");
  3894. }
  3895. llvm_unreachable("Unhandled ABIArgInfo::Kind");
  3896. } ();
  3897. // Emit the assume_aligned check on the return value.
  3898. const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl();
  3899. if (Ret.isScalar() && TargetDecl) {
  3900. if (const auto *AA = TargetDecl->getAttr<AssumeAlignedAttr>()) {
  3901. llvm::Value *OffsetValue = nullptr;
  3902. if (const auto *Offset = AA->getOffset())
  3903. OffsetValue = EmitScalarExpr(Offset);
  3904. llvm::Value *Alignment = EmitScalarExpr(AA->getAlignment());
  3905. llvm::ConstantInt *AlignmentCI = cast<llvm::ConstantInt>(Alignment);
  3906. EmitAlignmentAssumption(Ret.getScalarVal(), AlignmentCI->getZExtValue(),
  3907. OffsetValue);
  3908. } else if (const auto *AA = TargetDecl->getAttr<AllocAlignAttr>()) {
  3909. llvm::Value *ParamVal =
  3910. CallArgs[AA->getParamIndex().getLLVMIndex()].getRValue(
  3911. *this).getScalarVal();
  3912. EmitAlignmentAssumption(Ret.getScalarVal(), ParamVal);
  3913. }
  3914. }
  3915. return Ret;
  3916. }
  3917. CGCallee CGCallee::prepareConcreteCallee(CodeGenFunction &CGF) const {
  3918. if (isVirtual()) {
  3919. const CallExpr *CE = getVirtualCallExpr();
  3920. return CGF.CGM.getCXXABI().getVirtualFunctionPointer(
  3921. CGF, getVirtualMethodDecl(), getThisAddress(), getFunctionType(),
  3922. CE ? CE->getBeginLoc() : SourceLocation());
  3923. }
  3924. return *this;
  3925. }
  3926. /* VarArg handling */
  3927. Address CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address &VAListAddr) {
  3928. VAListAddr = VE->isMicrosoftABI()
  3929. ? EmitMSVAListRef(VE->getSubExpr())
  3930. : EmitVAListRef(VE->getSubExpr());
  3931. QualType Ty = VE->getType();
  3932. if (VE->isMicrosoftABI())
  3933. return CGM.getTypes().getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty);
  3934. return CGM.getTypes().getABIInfo().EmitVAArg(*this, VAListAddr, Ty);
  3935. }