CGCall.cpp 221 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694
  1. //===--- CGCall.cpp - Encapsulate calling convention details --------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // These classes wrap the information about a call or function
  10. // definition used to handle ABI compliancy.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CGCall.h"
  14. #include "ABIInfo.h"
  15. #include "CGBlocks.h"
  16. #include "CGCXXABI.h"
  17. #include "CGCleanup.h"
  18. #include "CGRecordLayout.h"
  19. #include "CodeGenFunction.h"
  20. #include "CodeGenModule.h"
  21. #include "TargetInfo.h"
  22. #include "clang/AST/Attr.h"
  23. #include "clang/AST/Decl.h"
  24. #include "clang/AST/DeclCXX.h"
  25. #include "clang/AST/DeclObjC.h"
  26. #include "clang/Basic/CodeGenOptions.h"
  27. #include "clang/Basic/TargetBuiltins.h"
  28. #include "clang/Basic/TargetInfo.h"
  29. #include "clang/CodeGen/CGFunctionInfo.h"
  30. #include "clang/CodeGen/SwiftCallingConv.h"
  31. #include "llvm/ADT/StringExtras.h"
  32. #include "llvm/Analysis/ValueTracking.h"
  33. #include "llvm/IR/Assumptions.h"
  34. #include "llvm/IR/Attributes.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. #include "llvm/IR/Type.h"
  41. #include "llvm/Transforms/Utils/Local.h"
  42. #include <optional>
  43. using namespace clang;
  44. using namespace CodeGen;
  45. /***/
  46. unsigned CodeGenTypes::ClangCallConvToLLVMCallConv(CallingConv CC) {
  47. switch (CC) {
  48. default: return llvm::CallingConv::C;
  49. case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
  50. case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
  51. case CC_X86RegCall: return llvm::CallingConv::X86_RegCall;
  52. case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
  53. case CC_Win64: return llvm::CallingConv::Win64;
  54. case CC_X86_64SysV: return llvm::CallingConv::X86_64_SysV;
  55. case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
  56. case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
  57. case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI;
  58. // TODO: Add support for __pascal to LLVM.
  59. case CC_X86Pascal: return llvm::CallingConv::C;
  60. // TODO: Add support for __vectorcall to LLVM.
  61. case CC_X86VectorCall: return llvm::CallingConv::X86_VectorCall;
  62. case CC_AArch64VectorCall: return llvm::CallingConv::AArch64_VectorCall;
  63. case CC_AArch64SVEPCS: return llvm::CallingConv::AArch64_SVE_VectorCall;
  64. case CC_AMDGPUKernelCall: return llvm::CallingConv::AMDGPU_KERNEL;
  65. case CC_SpirFunction: return llvm::CallingConv::SPIR_FUNC;
  66. case CC_OpenCLKernel: return CGM.getTargetCodeGenInfo().getOpenCLKernelCallingConv();
  67. case CC_PreserveMost: return llvm::CallingConv::PreserveMost;
  68. case CC_PreserveAll: return llvm::CallingConv::PreserveAll;
  69. case CC_Swift: return llvm::CallingConv::Swift;
  70. case CC_SwiftAsync: return llvm::CallingConv::SwiftTail;
  71. }
  72. }
  73. /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
  74. /// qualification. Either or both of RD and MD may be null. A null RD indicates
  75. /// that there is no meaningful 'this' type, and a null MD can occur when
  76. /// calling a method pointer.
  77. CanQualType CodeGenTypes::DeriveThisType(const CXXRecordDecl *RD,
  78. const CXXMethodDecl *MD) {
  79. QualType RecTy;
  80. if (RD)
  81. RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
  82. else
  83. RecTy = Context.VoidTy;
  84. if (MD)
  85. RecTy = Context.getAddrSpaceQualType(RecTy, MD->getMethodQualifiers().getAddressSpace());
  86. return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
  87. }
  88. /// Returns the canonical formal type of the given C++ method.
  89. static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
  90. return MD->getType()->getCanonicalTypeUnqualified()
  91. .getAs<FunctionProtoType>();
  92. }
  93. /// Returns the "extra-canonicalized" return type, which discards
  94. /// qualifiers on the return type. Codegen doesn't care about them,
  95. /// and it makes ABI code a little easier to be able to assume that
  96. /// all parameter and return types are top-level unqualified.
  97. static CanQualType GetReturnType(QualType RetTy) {
  98. return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
  99. }
  100. /// Arrange the argument and result information for a value of the given
  101. /// unprototyped freestanding function type.
  102. const CGFunctionInfo &
  103. CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) {
  104. // When translating an unprototyped function type, always use a
  105. // variadic type.
  106. return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(),
  107. /*instanceMethod=*/false,
  108. /*chainCall=*/false, std::nullopt,
  109. FTNP->getExtInfo(), {}, RequiredArgs(0));
  110. }
  111. static void addExtParameterInfosForCall(
  112. llvm::SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &paramInfos,
  113. const FunctionProtoType *proto,
  114. unsigned prefixArgs,
  115. unsigned totalArgs) {
  116. assert(proto->hasExtParameterInfos());
  117. assert(paramInfos.size() <= prefixArgs);
  118. assert(proto->getNumParams() + prefixArgs <= totalArgs);
  119. paramInfos.reserve(totalArgs);
  120. // Add default infos for any prefix args that don't already have infos.
  121. paramInfos.resize(prefixArgs);
  122. // Add infos for the prototype.
  123. for (const auto &ParamInfo : proto->getExtParameterInfos()) {
  124. paramInfos.push_back(ParamInfo);
  125. // pass_object_size params have no parameter info.
  126. if (ParamInfo.hasPassObjectSize())
  127. paramInfos.emplace_back();
  128. }
  129. assert(paramInfos.size() <= totalArgs &&
  130. "Did we forget to insert pass_object_size args?");
  131. // Add default infos for the variadic and/or suffix arguments.
  132. paramInfos.resize(totalArgs);
  133. }
  134. /// Adds the formal parameters in FPT to the given prefix. If any parameter in
  135. /// FPT has pass_object_size attrs, then we'll add parameters for those, too.
  136. static void appendParameterTypes(const CodeGenTypes &CGT,
  137. SmallVectorImpl<CanQualType> &prefix,
  138. SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &paramInfos,
  139. CanQual<FunctionProtoType> FPT) {
  140. // Fast path: don't touch param info if we don't need to.
  141. if (!FPT->hasExtParameterInfos()) {
  142. assert(paramInfos.empty() &&
  143. "We have paramInfos, but the prototype doesn't?");
  144. prefix.append(FPT->param_type_begin(), FPT->param_type_end());
  145. return;
  146. }
  147. unsigned PrefixSize = prefix.size();
  148. // In the vast majority of cases, we'll have precisely FPT->getNumParams()
  149. // parameters; the only thing that can change this is the presence of
  150. // pass_object_size. So, we preallocate for the common case.
  151. prefix.reserve(prefix.size() + FPT->getNumParams());
  152. auto ExtInfos = FPT->getExtParameterInfos();
  153. assert(ExtInfos.size() == FPT->getNumParams());
  154. for (unsigned I = 0, E = FPT->getNumParams(); I != E; ++I) {
  155. prefix.push_back(FPT->getParamType(I));
  156. if (ExtInfos[I].hasPassObjectSize())
  157. prefix.push_back(CGT.getContext().getSizeType());
  158. }
  159. addExtParameterInfosForCall(paramInfos, FPT.getTypePtr(), PrefixSize,
  160. prefix.size());
  161. }
  162. /// Arrange the LLVM function layout for a value of the given function
  163. /// type, on top of any implicit parameters already stored.
  164. static const CGFunctionInfo &
  165. arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool instanceMethod,
  166. SmallVectorImpl<CanQualType> &prefix,
  167. CanQual<FunctionProtoType> FTP) {
  168. SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
  169. RequiredArgs Required = RequiredArgs::forPrototypePlus(FTP, prefix.size());
  170. // FIXME: Kill copy.
  171. appendParameterTypes(CGT, prefix, paramInfos, FTP);
  172. CanQualType resultType = FTP->getReturnType().getUnqualifiedType();
  173. return CGT.arrangeLLVMFunctionInfo(resultType, instanceMethod,
  174. /*chainCall=*/false, prefix,
  175. FTP->getExtInfo(), paramInfos,
  176. Required);
  177. }
  178. /// Arrange the argument and result information for a value of the
  179. /// given freestanding function type.
  180. const CGFunctionInfo &
  181. CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) {
  182. SmallVector<CanQualType, 16> argTypes;
  183. return ::arrangeLLVMFunctionInfo(*this, /*instanceMethod=*/false, argTypes,
  184. FTP);
  185. }
  186. static CallingConv getCallingConventionForDecl(const ObjCMethodDecl *D,
  187. bool IsWindows) {
  188. // Set the appropriate calling convention for the Function.
  189. if (D->hasAttr<StdCallAttr>())
  190. return CC_X86StdCall;
  191. if (D->hasAttr<FastCallAttr>())
  192. return CC_X86FastCall;
  193. if (D->hasAttr<RegCallAttr>())
  194. return CC_X86RegCall;
  195. if (D->hasAttr<ThisCallAttr>())
  196. return CC_X86ThisCall;
  197. if (D->hasAttr<VectorCallAttr>())
  198. return CC_X86VectorCall;
  199. if (D->hasAttr<PascalAttr>())
  200. return CC_X86Pascal;
  201. if (PcsAttr *PCS = D->getAttr<PcsAttr>())
  202. return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
  203. if (D->hasAttr<AArch64VectorPcsAttr>())
  204. return CC_AArch64VectorCall;
  205. if (D->hasAttr<AArch64SVEPcsAttr>())
  206. return CC_AArch64SVEPCS;
  207. if (D->hasAttr<AMDGPUKernelCallAttr>())
  208. return CC_AMDGPUKernelCall;
  209. if (D->hasAttr<IntelOclBiccAttr>())
  210. return CC_IntelOclBicc;
  211. if (D->hasAttr<MSABIAttr>())
  212. return IsWindows ? CC_C : CC_Win64;
  213. if (D->hasAttr<SysVABIAttr>())
  214. return IsWindows ? CC_X86_64SysV : CC_C;
  215. if (D->hasAttr<PreserveMostAttr>())
  216. return CC_PreserveMost;
  217. if (D->hasAttr<PreserveAllAttr>())
  218. return CC_PreserveAll;
  219. return CC_C;
  220. }
  221. /// Arrange the argument and result information for a call to an
  222. /// unknown C++ non-static member function of the given abstract type.
  223. /// (A null RD means we don't have any meaningful "this" argument type,
  224. /// so fall back to a generic pointer type).
  225. /// The member function must be an ordinary function, i.e. not a
  226. /// constructor or destructor.
  227. const CGFunctionInfo &
  228. CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD,
  229. const FunctionProtoType *FTP,
  230. const CXXMethodDecl *MD) {
  231. SmallVector<CanQualType, 16> argTypes;
  232. // Add the 'this' pointer.
  233. argTypes.push_back(DeriveThisType(RD, MD));
  234. return ::arrangeLLVMFunctionInfo(
  235. *this, true, argTypes,
  236. FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
  237. }
  238. /// Set calling convention for CUDA/HIP kernel.
  239. static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM,
  240. const FunctionDecl *FD) {
  241. if (FD->hasAttr<CUDAGlobalAttr>()) {
  242. const FunctionType *FT = FTy->getAs<FunctionType>();
  243. CGM.getTargetCodeGenInfo().setCUDAKernelCallingConvention(FT);
  244. FTy = FT->getCanonicalTypeUnqualified();
  245. }
  246. }
  247. /// Arrange the argument and result information for a declaration or
  248. /// definition of the given C++ non-static member function. The
  249. /// member function must be an ordinary function, i.e. not a
  250. /// constructor or destructor.
  251. const CGFunctionInfo &
  252. CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) {
  253. assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!");
  254. assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
  255. CanQualType FT = GetFormalType(MD).getAs<Type>();
  256. setCUDAKernelCallingConvention(FT, CGM, MD);
  257. auto prototype = FT.getAs<FunctionProtoType>();
  258. if (MD->isInstance()) {
  259. // The abstract case is perfectly fine.
  260. const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD);
  261. return arrangeCXXMethodType(ThisType, prototype.getTypePtr(), MD);
  262. }
  263. return arrangeFreeFunctionType(prototype);
  264. }
  265. bool CodeGenTypes::inheritingCtorHasParams(
  266. const InheritedConstructor &Inherited, CXXCtorType Type) {
  267. // Parameters are unnecessary if we're constructing a base class subobject
  268. // and the inherited constructor lives in a virtual base.
  269. return Type == Ctor_Complete ||
  270. !Inherited.getShadowDecl()->constructsVirtualBase() ||
  271. !Target.getCXXABI().hasConstructorVariants();
  272. }
  273. const CGFunctionInfo &
  274. CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
  275. auto *MD = cast<CXXMethodDecl>(GD.getDecl());
  276. SmallVector<CanQualType, 16> argTypes;
  277. SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
  278. const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(GD);
  279. argTypes.push_back(DeriveThisType(ThisType, MD));
  280. bool PassParams = true;
  281. if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
  282. // A base class inheriting constructor doesn't get forwarded arguments
  283. // needed to construct a virtual base (or base class thereof).
  284. if (auto Inherited = CD->getInheritedConstructor())
  285. PassParams = inheritingCtorHasParams(Inherited, GD.getCtorType());
  286. }
  287. CanQual<FunctionProtoType> FTP = GetFormalType(MD);
  288. // Add the formal parameters.
  289. if (PassParams)
  290. appendParameterTypes(*this, argTypes, paramInfos, FTP);
  291. CGCXXABI::AddedStructorArgCounts AddedArgs =
  292. TheCXXABI.buildStructorSignature(GD, argTypes);
  293. if (!paramInfos.empty()) {
  294. // Note: prefix implies after the first param.
  295. if (AddedArgs.Prefix)
  296. paramInfos.insert(paramInfos.begin() + 1, AddedArgs.Prefix,
  297. FunctionProtoType::ExtParameterInfo{});
  298. if (AddedArgs.Suffix)
  299. paramInfos.append(AddedArgs.Suffix,
  300. FunctionProtoType::ExtParameterInfo{});
  301. }
  302. RequiredArgs required =
  303. (PassParams && MD->isVariadic() ? RequiredArgs(argTypes.size())
  304. : RequiredArgs::All);
  305. FunctionType::ExtInfo extInfo = FTP->getExtInfo();
  306. CanQualType resultType = TheCXXABI.HasThisReturn(GD)
  307. ? argTypes.front()
  308. : TheCXXABI.hasMostDerivedReturn(GD)
  309. ? CGM.getContext().VoidPtrTy
  310. : Context.VoidTy;
  311. return arrangeLLVMFunctionInfo(resultType, /*instanceMethod=*/true,
  312. /*chainCall=*/false, argTypes, extInfo,
  313. paramInfos, required);
  314. }
  315. static SmallVector<CanQualType, 16>
  316. getArgTypesForCall(ASTContext &ctx, const CallArgList &args) {
  317. SmallVector<CanQualType, 16> argTypes;
  318. for (auto &arg : args)
  319. argTypes.push_back(ctx.getCanonicalParamType(arg.Ty));
  320. return argTypes;
  321. }
  322. static SmallVector<CanQualType, 16>
  323. getArgTypesForDeclaration(ASTContext &ctx, const FunctionArgList &args) {
  324. SmallVector<CanQualType, 16> argTypes;
  325. for (auto &arg : args)
  326. argTypes.push_back(ctx.getCanonicalParamType(arg->getType()));
  327. return argTypes;
  328. }
  329. static llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16>
  330. getExtParameterInfosForCall(const FunctionProtoType *proto,
  331. unsigned prefixArgs, unsigned totalArgs) {
  332. llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> result;
  333. if (proto->hasExtParameterInfos()) {
  334. addExtParameterInfosForCall(result, proto, prefixArgs, totalArgs);
  335. }
  336. return result;
  337. }
  338. /// Arrange a call to a C++ method, passing the given arguments.
  339. ///
  340. /// ExtraPrefixArgs is the number of ABI-specific args passed after the `this`
  341. /// parameter.
  342. /// ExtraSuffixArgs is the number of ABI-specific args passed at the end of
  343. /// args.
  344. /// PassProtoArgs indicates whether `args` has args for the parameters in the
  345. /// given CXXConstructorDecl.
  346. const CGFunctionInfo &
  347. CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args,
  348. const CXXConstructorDecl *D,
  349. CXXCtorType CtorKind,
  350. unsigned ExtraPrefixArgs,
  351. unsigned ExtraSuffixArgs,
  352. bool PassProtoArgs) {
  353. // FIXME: Kill copy.
  354. SmallVector<CanQualType, 16> ArgTypes;
  355. for (const auto &Arg : args)
  356. ArgTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
  357. // +1 for implicit this, which should always be args[0].
  358. unsigned TotalPrefixArgs = 1 + ExtraPrefixArgs;
  359. CanQual<FunctionProtoType> FPT = GetFormalType(D);
  360. RequiredArgs Required = PassProtoArgs
  361. ? RequiredArgs::forPrototypePlus(
  362. FPT, TotalPrefixArgs + ExtraSuffixArgs)
  363. : RequiredArgs::All;
  364. GlobalDecl GD(D, CtorKind);
  365. CanQualType ResultType = TheCXXABI.HasThisReturn(GD)
  366. ? ArgTypes.front()
  367. : TheCXXABI.hasMostDerivedReturn(GD)
  368. ? CGM.getContext().VoidPtrTy
  369. : Context.VoidTy;
  370. FunctionType::ExtInfo Info = FPT->getExtInfo();
  371. llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> ParamInfos;
  372. // If the prototype args are elided, we should only have ABI-specific args,
  373. // which never have param info.
  374. if (PassProtoArgs && FPT->hasExtParameterInfos()) {
  375. // ABI-specific suffix arguments are treated the same as variadic arguments.
  376. addExtParameterInfosForCall(ParamInfos, FPT.getTypePtr(), TotalPrefixArgs,
  377. ArgTypes.size());
  378. }
  379. return arrangeLLVMFunctionInfo(ResultType, /*instanceMethod=*/true,
  380. /*chainCall=*/false, ArgTypes, Info,
  381. ParamInfos, Required);
  382. }
  383. /// Arrange the argument and result information for the declaration or
  384. /// definition of the given function.
  385. const CGFunctionInfo &
  386. CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) {
  387. if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
  388. if (MD->isInstance())
  389. return arrangeCXXMethodDeclaration(MD);
  390. CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
  391. assert(isa<FunctionType>(FTy));
  392. setCUDAKernelCallingConvention(FTy, CGM, FD);
  393. // When declaring a function without a prototype, always use a
  394. // non-variadic type.
  395. if (CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>()) {
  396. return arrangeLLVMFunctionInfo(
  397. noProto->getReturnType(), /*instanceMethod=*/false,
  398. /*chainCall=*/false, std::nullopt, noProto->getExtInfo(), {},
  399. RequiredArgs::All);
  400. }
  401. return arrangeFreeFunctionType(FTy.castAs<FunctionProtoType>());
  402. }
  403. /// Arrange the argument and result information for the declaration or
  404. /// definition of an Objective-C method.
  405. const CGFunctionInfo &
  406. CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) {
  407. // It happens that this is the same as a call with no optional
  408. // arguments, except also using the formal 'self' type.
  409. return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType());
  410. }
  411. /// Arrange the argument and result information for the function type
  412. /// through which to perform a send to the given Objective-C method,
  413. /// using the given receiver type. The receiver type is not always
  414. /// the 'self' type of the method or even an Objective-C pointer type.
  415. /// This is *not* the right method for actually performing such a
  416. /// message send, due to the possibility of optional arguments.
  417. const CGFunctionInfo &
  418. CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
  419. QualType receiverType) {
  420. SmallVector<CanQualType, 16> argTys;
  421. SmallVector<FunctionProtoType::ExtParameterInfo, 4> extParamInfos(
  422. MD->isDirectMethod() ? 1 : 2);
  423. argTys.push_back(Context.getCanonicalParamType(receiverType));
  424. if (!MD->isDirectMethod())
  425. argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
  426. // FIXME: Kill copy?
  427. for (const auto *I : MD->parameters()) {
  428. argTys.push_back(Context.getCanonicalParamType(I->getType()));
  429. auto extParamInfo = FunctionProtoType::ExtParameterInfo().withIsNoEscape(
  430. I->hasAttr<NoEscapeAttr>());
  431. extParamInfos.push_back(extParamInfo);
  432. }
  433. FunctionType::ExtInfo einfo;
  434. bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows();
  435. einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows));
  436. if (getContext().getLangOpts().ObjCAutoRefCount &&
  437. MD->hasAttr<NSReturnsRetainedAttr>())
  438. einfo = einfo.withProducesResult(true);
  439. RequiredArgs required =
  440. (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All);
  441. return arrangeLLVMFunctionInfo(
  442. GetReturnType(MD->getReturnType()), /*instanceMethod=*/false,
  443. /*chainCall=*/false, argTys, einfo, extParamInfos, required);
  444. }
  445. const CGFunctionInfo &
  446. CodeGenTypes::arrangeUnprototypedObjCMessageSend(QualType returnType,
  447. const CallArgList &args) {
  448. auto argTypes = getArgTypesForCall(Context, args);
  449. FunctionType::ExtInfo einfo;
  450. return arrangeLLVMFunctionInfo(
  451. GetReturnType(returnType), /*instanceMethod=*/false,
  452. /*chainCall=*/false, argTypes, einfo, {}, RequiredArgs::All);
  453. }
  454. const CGFunctionInfo &
  455. CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) {
  456. // FIXME: Do we need to handle ObjCMethodDecl?
  457. const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
  458. if (isa<CXXConstructorDecl>(GD.getDecl()) ||
  459. isa<CXXDestructorDecl>(GD.getDecl()))
  460. return arrangeCXXStructorDeclaration(GD);
  461. return arrangeFunctionDeclaration(FD);
  462. }
  463. /// Arrange a thunk that takes 'this' as the first parameter followed by
  464. /// varargs. Return a void pointer, regardless of the actual return type.
  465. /// The body of the thunk will end in a musttail call to a function of the
  466. /// correct type, and the caller will bitcast the function to the correct
  467. /// prototype.
  468. const CGFunctionInfo &
  469. CodeGenTypes::arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD) {
  470. assert(MD->isVirtual() && "only methods have thunks");
  471. CanQual<FunctionProtoType> FTP = GetFormalType(MD);
  472. CanQualType ArgTys[] = {DeriveThisType(MD->getParent(), MD)};
  473. return arrangeLLVMFunctionInfo(Context.VoidTy, /*instanceMethod=*/false,
  474. /*chainCall=*/false, ArgTys,
  475. FTP->getExtInfo(), {}, RequiredArgs(1));
  476. }
  477. const CGFunctionInfo &
  478. CodeGenTypes::arrangeMSCtorClosure(const CXXConstructorDecl *CD,
  479. CXXCtorType CT) {
  480. assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure);
  481. CanQual<FunctionProtoType> FTP = GetFormalType(CD);
  482. SmallVector<CanQualType, 2> ArgTys;
  483. const CXXRecordDecl *RD = CD->getParent();
  484. ArgTys.push_back(DeriveThisType(RD, CD));
  485. if (CT == Ctor_CopyingClosure)
  486. ArgTys.push_back(*FTP->param_type_begin());
  487. if (RD->getNumVBases() > 0)
  488. ArgTys.push_back(Context.IntTy);
  489. CallingConv CC = Context.getDefaultCallingConvention(
  490. /*IsVariadic=*/false, /*IsCXXMethod=*/true);
  491. return arrangeLLVMFunctionInfo(Context.VoidTy, /*instanceMethod=*/true,
  492. /*chainCall=*/false, ArgTys,
  493. FunctionType::ExtInfo(CC), {},
  494. RequiredArgs::All);
  495. }
  496. /// Arrange a call as unto a free function, except possibly with an
  497. /// additional number of formal parameters considered required.
  498. static const CGFunctionInfo &
  499. arrangeFreeFunctionLikeCall(CodeGenTypes &CGT,
  500. CodeGenModule &CGM,
  501. const CallArgList &args,
  502. const FunctionType *fnType,
  503. unsigned numExtraRequiredArgs,
  504. bool chainCall) {
  505. assert(args.size() >= numExtraRequiredArgs);
  506. llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
  507. // In most cases, there are no optional arguments.
  508. RequiredArgs required = RequiredArgs::All;
  509. // If we have a variadic prototype, the required arguments are the
  510. // extra prefix plus the arguments in the prototype.
  511. if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) {
  512. if (proto->isVariadic())
  513. required = RequiredArgs::forPrototypePlus(proto, numExtraRequiredArgs);
  514. if (proto->hasExtParameterInfos())
  515. addExtParameterInfosForCall(paramInfos, proto, numExtraRequiredArgs,
  516. args.size());
  517. // If we don't have a prototype at all, but we're supposed to
  518. // explicitly use the variadic convention for unprototyped calls,
  519. // treat all of the arguments as required but preserve the nominal
  520. // possibility of variadics.
  521. } else if (CGM.getTargetCodeGenInfo()
  522. .isNoProtoCallVariadic(args,
  523. cast<FunctionNoProtoType>(fnType))) {
  524. required = RequiredArgs(args.size());
  525. }
  526. // FIXME: Kill copy.
  527. SmallVector<CanQualType, 16> argTypes;
  528. for (const auto &arg : args)
  529. argTypes.push_back(CGT.getContext().getCanonicalParamType(arg.Ty));
  530. return CGT.arrangeLLVMFunctionInfo(GetReturnType(fnType->getReturnType()),
  531. /*instanceMethod=*/false, chainCall,
  532. argTypes, fnType->getExtInfo(), paramInfos,
  533. required);
  534. }
  535. /// Figure out the rules for calling a function with the given formal
  536. /// type using the given arguments. The arguments are necessary
  537. /// because the function might be unprototyped, in which case it's
  538. /// target-dependent in crazy ways.
  539. const CGFunctionInfo &
  540. CodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args,
  541. const FunctionType *fnType,
  542. bool chainCall) {
  543. return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType,
  544. chainCall ? 1 : 0, chainCall);
  545. }
  546. /// A block function is essentially a free function with an
  547. /// extra implicit argument.
  548. const CGFunctionInfo &
  549. CodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args,
  550. const FunctionType *fnType) {
  551. return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1,
  552. /*chainCall=*/false);
  553. }
  554. const CGFunctionInfo &
  555. CodeGenTypes::arrangeBlockFunctionDeclaration(const FunctionProtoType *proto,
  556. const FunctionArgList &params) {
  557. auto paramInfos = getExtParameterInfosForCall(proto, 1, params.size());
  558. auto argTypes = getArgTypesForDeclaration(Context, params);
  559. return arrangeLLVMFunctionInfo(GetReturnType(proto->getReturnType()),
  560. /*instanceMethod*/ false, /*chainCall*/ false,
  561. argTypes, proto->getExtInfo(), paramInfos,
  562. RequiredArgs::forPrototypePlus(proto, 1));
  563. }
  564. const CGFunctionInfo &
  565. CodeGenTypes::arrangeBuiltinFunctionCall(QualType resultType,
  566. const CallArgList &args) {
  567. // FIXME: Kill copy.
  568. SmallVector<CanQualType, 16> argTypes;
  569. for (const auto &Arg : args)
  570. argTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
  571. return arrangeLLVMFunctionInfo(
  572. GetReturnType(resultType), /*instanceMethod=*/false,
  573. /*chainCall=*/false, argTypes, FunctionType::ExtInfo(),
  574. /*paramInfos=*/ {}, RequiredArgs::All);
  575. }
  576. const CGFunctionInfo &
  577. CodeGenTypes::arrangeBuiltinFunctionDeclaration(QualType resultType,
  578. const FunctionArgList &args) {
  579. auto argTypes = getArgTypesForDeclaration(Context, args);
  580. return arrangeLLVMFunctionInfo(
  581. GetReturnType(resultType), /*instanceMethod=*/false, /*chainCall=*/false,
  582. argTypes, FunctionType::ExtInfo(), {}, RequiredArgs::All);
  583. }
  584. const CGFunctionInfo &
  585. CodeGenTypes::arrangeBuiltinFunctionDeclaration(CanQualType resultType,
  586. ArrayRef<CanQualType> argTypes) {
  587. return arrangeLLVMFunctionInfo(
  588. resultType, /*instanceMethod=*/false, /*chainCall=*/false,
  589. argTypes, FunctionType::ExtInfo(), {}, RequiredArgs::All);
  590. }
  591. /// Arrange a call to a C++ method, passing the given arguments.
  592. ///
  593. /// numPrefixArgs is the number of ABI-specific prefix arguments we have. It
  594. /// does not count `this`.
  595. const CGFunctionInfo &
  596. CodeGenTypes::arrangeCXXMethodCall(const CallArgList &args,
  597. const FunctionProtoType *proto,
  598. RequiredArgs required,
  599. unsigned numPrefixArgs) {
  600. assert(numPrefixArgs + 1 <= args.size() &&
  601. "Emitting a call with less args than the required prefix?");
  602. // Add one to account for `this`. It's a bit awkward here, but we don't count
  603. // `this` in similar places elsewhere.
  604. auto paramInfos =
  605. getExtParameterInfosForCall(proto, numPrefixArgs + 1, args.size());
  606. // FIXME: Kill copy.
  607. auto argTypes = getArgTypesForCall(Context, args);
  608. FunctionType::ExtInfo info = proto->getExtInfo();
  609. return arrangeLLVMFunctionInfo(
  610. GetReturnType(proto->getReturnType()), /*instanceMethod=*/true,
  611. /*chainCall=*/false, argTypes, info, paramInfos, required);
  612. }
  613. const CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() {
  614. return arrangeLLVMFunctionInfo(
  615. getContext().VoidTy, /*instanceMethod=*/false, /*chainCall=*/false,
  616. std::nullopt, FunctionType::ExtInfo(), {}, RequiredArgs::All);
  617. }
  618. const CGFunctionInfo &
  619. CodeGenTypes::arrangeCall(const CGFunctionInfo &signature,
  620. const CallArgList &args) {
  621. assert(signature.arg_size() <= args.size());
  622. if (signature.arg_size() == args.size())
  623. return signature;
  624. SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
  625. auto sigParamInfos = signature.getExtParameterInfos();
  626. if (!sigParamInfos.empty()) {
  627. paramInfos.append(sigParamInfos.begin(), sigParamInfos.end());
  628. paramInfos.resize(args.size());
  629. }
  630. auto argTypes = getArgTypesForCall(Context, args);
  631. assert(signature.getRequiredArgs().allowsOptionalArgs());
  632. return arrangeLLVMFunctionInfo(signature.getReturnType(),
  633. signature.isInstanceMethod(),
  634. signature.isChainCall(),
  635. argTypes,
  636. signature.getExtInfo(),
  637. paramInfos,
  638. signature.getRequiredArgs());
  639. }
  640. namespace clang {
  641. namespace CodeGen {
  642. void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI);
  643. }
  644. }
  645. /// Arrange the argument and result information for an abstract value
  646. /// of a given function type. This is the method which all of the
  647. /// above functions ultimately defer to.
  648. const CGFunctionInfo &
  649. CodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType,
  650. bool instanceMethod,
  651. bool chainCall,
  652. ArrayRef<CanQualType> argTypes,
  653. FunctionType::ExtInfo info,
  654. ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
  655. RequiredArgs required) {
  656. assert(llvm::all_of(argTypes,
  657. [](CanQualType T) { return T.isCanonicalAsParam(); }));
  658. // Lookup or create unique function info.
  659. llvm::FoldingSetNodeID ID;
  660. CGFunctionInfo::Profile(ID, instanceMethod, chainCall, info, paramInfos,
  661. required, resultType, argTypes);
  662. void *insertPos = nullptr;
  663. CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos);
  664. if (FI)
  665. return *FI;
  666. unsigned CC = ClangCallConvToLLVMCallConv(info.getCC());
  667. // Construct the function info. We co-allocate the ArgInfos.
  668. FI = CGFunctionInfo::create(CC, instanceMethod, chainCall, info,
  669. paramInfos, resultType, argTypes, required);
  670. FunctionInfos.InsertNode(FI, insertPos);
  671. bool inserted = FunctionsBeingProcessed.insert(FI).second;
  672. (void)inserted;
  673. assert(inserted && "Recursively being processed?");
  674. // Compute ABI information.
  675. if (CC == llvm::CallingConv::SPIR_KERNEL) {
  676. // Force target independent argument handling for the host visible
  677. // kernel functions.
  678. computeSPIRKernelABIInfo(CGM, *FI);
  679. } else if (info.getCC() == CC_Swift || info.getCC() == CC_SwiftAsync) {
  680. swiftcall::computeABIInfo(CGM, *FI);
  681. } else {
  682. getABIInfo().computeInfo(*FI);
  683. }
  684. // Loop over all of the computed argument and return value info. If any of
  685. // them are direct or extend without a specified coerce type, specify the
  686. // default now.
  687. ABIArgInfo &retInfo = FI->getReturnInfo();
  688. if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == nullptr)
  689. retInfo.setCoerceToType(ConvertType(FI->getReturnType()));
  690. for (auto &I : FI->arguments())
  691. if (I.info.canHaveCoerceToType() && I.info.getCoerceToType() == nullptr)
  692. I.info.setCoerceToType(ConvertType(I.type));
  693. bool erased = FunctionsBeingProcessed.erase(FI); (void)erased;
  694. assert(erased && "Not in set?");
  695. return *FI;
  696. }
  697. CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC,
  698. bool instanceMethod,
  699. bool chainCall,
  700. const FunctionType::ExtInfo &info,
  701. ArrayRef<ExtParameterInfo> paramInfos,
  702. CanQualType resultType,
  703. ArrayRef<CanQualType> argTypes,
  704. RequiredArgs required) {
  705. assert(paramInfos.empty() || paramInfos.size() == argTypes.size());
  706. assert(!required.allowsOptionalArgs() ||
  707. required.getNumRequiredArgs() <= argTypes.size());
  708. void *buffer =
  709. operator new(totalSizeToAlloc<ArgInfo, ExtParameterInfo>(
  710. argTypes.size() + 1, paramInfos.size()));
  711. CGFunctionInfo *FI = new(buffer) CGFunctionInfo();
  712. FI->CallingConvention = llvmCC;
  713. FI->EffectiveCallingConvention = llvmCC;
  714. FI->ASTCallingConvention = info.getCC();
  715. FI->InstanceMethod = instanceMethod;
  716. FI->ChainCall = chainCall;
  717. FI->CmseNSCall = info.getCmseNSCall();
  718. FI->NoReturn = info.getNoReturn();
  719. FI->ReturnsRetained = info.getProducesResult();
  720. FI->NoCallerSavedRegs = info.getNoCallerSavedRegs();
  721. FI->NoCfCheck = info.getNoCfCheck();
  722. FI->Required = required;
  723. FI->HasRegParm = info.getHasRegParm();
  724. FI->RegParm = info.getRegParm();
  725. FI->ArgStruct = nullptr;
  726. FI->ArgStructAlign = 0;
  727. FI->NumArgs = argTypes.size();
  728. FI->HasExtParameterInfos = !paramInfos.empty();
  729. FI->getArgsBuffer()[0].type = resultType;
  730. FI->MaxVectorWidth = 0;
  731. for (unsigned i = 0, e = argTypes.size(); i != e; ++i)
  732. FI->getArgsBuffer()[i + 1].type = argTypes[i];
  733. for (unsigned i = 0, e = paramInfos.size(); i != e; ++i)
  734. FI->getExtParameterInfosBuffer()[i] = paramInfos[i];
  735. return FI;
  736. }
  737. /***/
  738. namespace {
  739. // ABIArgInfo::Expand implementation.
  740. // Specifies the way QualType passed as ABIArgInfo::Expand is expanded.
  741. struct TypeExpansion {
  742. enum TypeExpansionKind {
  743. // Elements of constant arrays are expanded recursively.
  744. TEK_ConstantArray,
  745. // Record fields are expanded recursively (but if record is a union, only
  746. // the field with the largest size is expanded).
  747. TEK_Record,
  748. // For complex types, real and imaginary parts are expanded recursively.
  749. TEK_Complex,
  750. // All other types are not expandable.
  751. TEK_None
  752. };
  753. const TypeExpansionKind Kind;
  754. TypeExpansion(TypeExpansionKind K) : Kind(K) {}
  755. virtual ~TypeExpansion() {}
  756. };
  757. struct ConstantArrayExpansion : TypeExpansion {
  758. QualType EltTy;
  759. uint64_t NumElts;
  760. ConstantArrayExpansion(QualType EltTy, uint64_t NumElts)
  761. : TypeExpansion(TEK_ConstantArray), EltTy(EltTy), NumElts(NumElts) {}
  762. static bool classof(const TypeExpansion *TE) {
  763. return TE->Kind == TEK_ConstantArray;
  764. }
  765. };
  766. struct RecordExpansion : TypeExpansion {
  767. SmallVector<const CXXBaseSpecifier *, 1> Bases;
  768. SmallVector<const FieldDecl *, 1> Fields;
  769. RecordExpansion(SmallVector<const CXXBaseSpecifier *, 1> &&Bases,
  770. SmallVector<const FieldDecl *, 1> &&Fields)
  771. : TypeExpansion(TEK_Record), Bases(std::move(Bases)),
  772. Fields(std::move(Fields)) {}
  773. static bool classof(const TypeExpansion *TE) {
  774. return TE->Kind == TEK_Record;
  775. }
  776. };
  777. struct ComplexExpansion : TypeExpansion {
  778. QualType EltTy;
  779. ComplexExpansion(QualType EltTy) : TypeExpansion(TEK_Complex), EltTy(EltTy) {}
  780. static bool classof(const TypeExpansion *TE) {
  781. return TE->Kind == TEK_Complex;
  782. }
  783. };
  784. struct NoExpansion : TypeExpansion {
  785. NoExpansion() : TypeExpansion(TEK_None) {}
  786. static bool classof(const TypeExpansion *TE) {
  787. return TE->Kind == TEK_None;
  788. }
  789. };
  790. } // namespace
  791. static std::unique_ptr<TypeExpansion>
  792. getTypeExpansion(QualType Ty, const ASTContext &Context) {
  793. if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
  794. return std::make_unique<ConstantArrayExpansion>(
  795. AT->getElementType(), AT->getSize().getZExtValue());
  796. }
  797. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  798. SmallVector<const CXXBaseSpecifier *, 1> Bases;
  799. SmallVector<const FieldDecl *, 1> Fields;
  800. const RecordDecl *RD = RT->getDecl();
  801. assert(!RD->hasFlexibleArrayMember() &&
  802. "Cannot expand structure with flexible array.");
  803. if (RD->isUnion()) {
  804. // Unions can be here only in degenerative cases - all the fields are same
  805. // after flattening. Thus we have to use the "largest" field.
  806. const FieldDecl *LargestFD = nullptr;
  807. CharUnits UnionSize = CharUnits::Zero();
  808. for (const auto *FD : RD->fields()) {
  809. if (FD->isZeroLengthBitField(Context))
  810. continue;
  811. assert(!FD->isBitField() &&
  812. "Cannot expand structure with bit-field members.");
  813. CharUnits FieldSize = Context.getTypeSizeInChars(FD->getType());
  814. if (UnionSize < FieldSize) {
  815. UnionSize = FieldSize;
  816. LargestFD = FD;
  817. }
  818. }
  819. if (LargestFD)
  820. Fields.push_back(LargestFD);
  821. } else {
  822. if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  823. assert(!CXXRD->isDynamicClass() &&
  824. "cannot expand vtable pointers in dynamic classes");
  825. llvm::append_range(Bases, llvm::make_pointer_range(CXXRD->bases()));
  826. }
  827. for (const auto *FD : RD->fields()) {
  828. if (FD->isZeroLengthBitField(Context))
  829. continue;
  830. assert(!FD->isBitField() &&
  831. "Cannot expand structure with bit-field members.");
  832. Fields.push_back(FD);
  833. }
  834. }
  835. return std::make_unique<RecordExpansion>(std::move(Bases),
  836. std::move(Fields));
  837. }
  838. if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
  839. return std::make_unique<ComplexExpansion>(CT->getElementType());
  840. }
  841. return std::make_unique<NoExpansion>();
  842. }
  843. static int getExpansionSize(QualType Ty, const ASTContext &Context) {
  844. auto Exp = getTypeExpansion(Ty, Context);
  845. if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
  846. return CAExp->NumElts * getExpansionSize(CAExp->EltTy, Context);
  847. }
  848. if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
  849. int Res = 0;
  850. for (auto BS : RExp->Bases)
  851. Res += getExpansionSize(BS->getType(), Context);
  852. for (auto FD : RExp->Fields)
  853. Res += getExpansionSize(FD->getType(), Context);
  854. return Res;
  855. }
  856. if (isa<ComplexExpansion>(Exp.get()))
  857. return 2;
  858. assert(isa<NoExpansion>(Exp.get()));
  859. return 1;
  860. }
  861. void
  862. CodeGenTypes::getExpandedTypes(QualType Ty,
  863. SmallVectorImpl<llvm::Type *>::iterator &TI) {
  864. auto Exp = getTypeExpansion(Ty, Context);
  865. if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
  866. for (int i = 0, n = CAExp->NumElts; i < n; i++) {
  867. getExpandedTypes(CAExp->EltTy, TI);
  868. }
  869. } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
  870. for (auto BS : RExp->Bases)
  871. getExpandedTypes(BS->getType(), TI);
  872. for (auto FD : RExp->Fields)
  873. getExpandedTypes(FD->getType(), TI);
  874. } else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) {
  875. llvm::Type *EltTy = ConvertType(CExp->EltTy);
  876. *TI++ = EltTy;
  877. *TI++ = EltTy;
  878. } else {
  879. assert(isa<NoExpansion>(Exp.get()));
  880. *TI++ = ConvertType(Ty);
  881. }
  882. }
  883. static void forConstantArrayExpansion(CodeGenFunction &CGF,
  884. ConstantArrayExpansion *CAE,
  885. Address BaseAddr,
  886. llvm::function_ref<void(Address)> Fn) {
  887. CharUnits EltSize = CGF.getContext().getTypeSizeInChars(CAE->EltTy);
  888. CharUnits EltAlign =
  889. BaseAddr.getAlignment().alignmentOfArrayElement(EltSize);
  890. llvm::Type *EltTy = CGF.ConvertTypeForMem(CAE->EltTy);
  891. for (int i = 0, n = CAE->NumElts; i < n; i++) {
  892. llvm::Value *EltAddr = CGF.Builder.CreateConstGEP2_32(
  893. BaseAddr.getElementType(), BaseAddr.getPointer(), 0, i);
  894. Fn(Address(EltAddr, EltTy, EltAlign));
  895. }
  896. }
  897. void CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
  898. llvm::Function::arg_iterator &AI) {
  899. assert(LV.isSimple() &&
  900. "Unexpected non-simple lvalue during struct expansion.");
  901. auto Exp = getTypeExpansion(Ty, getContext());
  902. if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
  903. forConstantArrayExpansion(
  904. *this, CAExp, LV.getAddress(*this), [&](Address EltAddr) {
  905. LValue LV = MakeAddrLValue(EltAddr, CAExp->EltTy);
  906. ExpandTypeFromArgs(CAExp->EltTy, LV, AI);
  907. });
  908. } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
  909. Address This = LV.getAddress(*this);
  910. for (const CXXBaseSpecifier *BS : RExp->Bases) {
  911. // Perform a single step derived-to-base conversion.
  912. Address Base =
  913. GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1,
  914. /*NullCheckValue=*/false, SourceLocation());
  915. LValue SubLV = MakeAddrLValue(Base, BS->getType());
  916. // Recurse onto bases.
  917. ExpandTypeFromArgs(BS->getType(), SubLV, AI);
  918. }
  919. for (auto FD : RExp->Fields) {
  920. // FIXME: What are the right qualifiers here?
  921. LValue SubLV = EmitLValueForFieldInitialization(LV, FD);
  922. ExpandTypeFromArgs(FD->getType(), SubLV, AI);
  923. }
  924. } else if (isa<ComplexExpansion>(Exp.get())) {
  925. auto realValue = &*AI++;
  926. auto imagValue = &*AI++;
  927. EmitStoreOfComplex(ComplexPairTy(realValue, imagValue), LV, /*init*/ true);
  928. } else {
  929. // Call EmitStoreOfScalar except when the lvalue is a bitfield to emit a
  930. // primitive store.
  931. assert(isa<NoExpansion>(Exp.get()));
  932. llvm::Value *Arg = &*AI++;
  933. if (LV.isBitField()) {
  934. EmitStoreThroughLValue(RValue::get(Arg), LV);
  935. } else {
  936. // TODO: currently there are some places are inconsistent in what LLVM
  937. // pointer type they use (see D118744). Once clang uses opaque pointers
  938. // all LLVM pointer types will be the same and we can remove this check.
  939. if (Arg->getType()->isPointerTy()) {
  940. Address Addr = LV.getAddress(*this);
  941. Arg = Builder.CreateBitCast(Arg, Addr.getElementType());
  942. }
  943. EmitStoreOfScalar(Arg, LV);
  944. }
  945. }
  946. }
  947. void CodeGenFunction::ExpandTypeToArgs(
  948. QualType Ty, CallArg Arg, llvm::FunctionType *IRFuncTy,
  949. SmallVectorImpl<llvm::Value *> &IRCallArgs, unsigned &IRCallArgPos) {
  950. auto Exp = getTypeExpansion(Ty, getContext());
  951. if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
  952. Address Addr = Arg.hasLValue() ? Arg.getKnownLValue().getAddress(*this)
  953. : Arg.getKnownRValue().getAggregateAddress();
  954. forConstantArrayExpansion(
  955. *this, CAExp, Addr, [&](Address EltAddr) {
  956. CallArg EltArg = CallArg(
  957. convertTempToRValue(EltAddr, CAExp->EltTy, SourceLocation()),
  958. CAExp->EltTy);
  959. ExpandTypeToArgs(CAExp->EltTy, EltArg, IRFuncTy, IRCallArgs,
  960. IRCallArgPos);
  961. });
  962. } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
  963. Address This = Arg.hasLValue() ? Arg.getKnownLValue().getAddress(*this)
  964. : Arg.getKnownRValue().getAggregateAddress();
  965. for (const CXXBaseSpecifier *BS : RExp->Bases) {
  966. // Perform a single step derived-to-base conversion.
  967. Address Base =
  968. GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1,
  969. /*NullCheckValue=*/false, SourceLocation());
  970. CallArg BaseArg = CallArg(RValue::getAggregate(Base), BS->getType());
  971. // Recurse onto bases.
  972. ExpandTypeToArgs(BS->getType(), BaseArg, IRFuncTy, IRCallArgs,
  973. IRCallArgPos);
  974. }
  975. LValue LV = MakeAddrLValue(This, Ty);
  976. for (auto FD : RExp->Fields) {
  977. CallArg FldArg =
  978. CallArg(EmitRValueForField(LV, FD, SourceLocation()), FD->getType());
  979. ExpandTypeToArgs(FD->getType(), FldArg, IRFuncTy, IRCallArgs,
  980. IRCallArgPos);
  981. }
  982. } else if (isa<ComplexExpansion>(Exp.get())) {
  983. ComplexPairTy CV = Arg.getKnownRValue().getComplexVal();
  984. IRCallArgs[IRCallArgPos++] = CV.first;
  985. IRCallArgs[IRCallArgPos++] = CV.second;
  986. } else {
  987. assert(isa<NoExpansion>(Exp.get()));
  988. auto RV = Arg.getKnownRValue();
  989. assert(RV.isScalar() &&
  990. "Unexpected non-scalar rvalue during struct expansion.");
  991. // Insert a bitcast as needed.
  992. llvm::Value *V = RV.getScalarVal();
  993. if (IRCallArgPos < IRFuncTy->getNumParams() &&
  994. V->getType() != IRFuncTy->getParamType(IRCallArgPos))
  995. V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRCallArgPos));
  996. IRCallArgs[IRCallArgPos++] = V;
  997. }
  998. }
  999. /// Create a temporary allocation for the purposes of coercion.
  1000. static Address CreateTempAllocaForCoercion(CodeGenFunction &CGF, llvm::Type *Ty,
  1001. CharUnits MinAlign,
  1002. const Twine &Name = "tmp") {
  1003. // Don't use an alignment that's worse than what LLVM would prefer.
  1004. auto PrefAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(Ty);
  1005. CharUnits Align = std::max(MinAlign, CharUnits::fromQuantity(PrefAlign));
  1006. return CGF.CreateTempAlloca(Ty, Align, Name + ".coerce");
  1007. }
  1008. /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
  1009. /// accessing some number of bytes out of it, try to gep into the struct to get
  1010. /// at its inner goodness. Dive as deep as possible without entering an element
  1011. /// with an in-memory size smaller than DstSize.
  1012. static Address
  1013. EnterStructPointerForCoercedAccess(Address SrcPtr,
  1014. llvm::StructType *SrcSTy,
  1015. uint64_t DstSize, CodeGenFunction &CGF) {
  1016. // We can't dive into a zero-element struct.
  1017. if (SrcSTy->getNumElements() == 0) return SrcPtr;
  1018. llvm::Type *FirstElt = SrcSTy->getElementType(0);
  1019. // If the first elt is at least as large as what we're looking for, or if the
  1020. // first element is the same size as the whole struct, we can enter it. The
  1021. // comparison must be made on the store size and not the alloca size. Using
  1022. // the alloca size may overstate the size of the load.
  1023. uint64_t FirstEltSize =
  1024. CGF.CGM.getDataLayout().getTypeStoreSize(FirstElt);
  1025. if (FirstEltSize < DstSize &&
  1026. FirstEltSize < CGF.CGM.getDataLayout().getTypeStoreSize(SrcSTy))
  1027. return SrcPtr;
  1028. // GEP into the first element.
  1029. SrcPtr = CGF.Builder.CreateStructGEP(SrcPtr, 0, "coerce.dive");
  1030. // If the first element is a struct, recurse.
  1031. llvm::Type *SrcTy = SrcPtr.getElementType();
  1032. if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
  1033. return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
  1034. return SrcPtr;
  1035. }
  1036. /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
  1037. /// are either integers or pointers. This does a truncation of the value if it
  1038. /// is too large or a zero extension if it is too small.
  1039. ///
  1040. /// This behaves as if the value were coerced through memory, so on big-endian
  1041. /// targets the high bits are preserved in a truncation, while little-endian
  1042. /// targets preserve the low bits.
  1043. static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
  1044. llvm::Type *Ty,
  1045. CodeGenFunction &CGF) {
  1046. if (Val->getType() == Ty)
  1047. return Val;
  1048. if (isa<llvm::PointerType>(Val->getType())) {
  1049. // If this is Pointer->Pointer avoid conversion to and from int.
  1050. if (isa<llvm::PointerType>(Ty))
  1051. return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
  1052. // Convert the pointer to an integer so we can play with its width.
  1053. Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
  1054. }
  1055. llvm::Type *DestIntTy = Ty;
  1056. if (isa<llvm::PointerType>(DestIntTy))
  1057. DestIntTy = CGF.IntPtrTy;
  1058. if (Val->getType() != DestIntTy) {
  1059. const llvm::DataLayout &DL = CGF.CGM.getDataLayout();
  1060. if (DL.isBigEndian()) {
  1061. // Preserve the high bits on big-endian targets.
  1062. // That is what memory coercion does.
  1063. uint64_t SrcSize = DL.getTypeSizeInBits(Val->getType());
  1064. uint64_t DstSize = DL.getTypeSizeInBits(DestIntTy);
  1065. if (SrcSize > DstSize) {
  1066. Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits");
  1067. Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii");
  1068. } else {
  1069. Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii");
  1070. Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits");
  1071. }
  1072. } else {
  1073. // Little-endian targets preserve the low bits. No shifts required.
  1074. Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
  1075. }
  1076. }
  1077. if (isa<llvm::PointerType>(Ty))
  1078. Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
  1079. return Val;
  1080. }
  1081. /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
  1082. /// a pointer to an object of type \arg Ty, known to be aligned to
  1083. /// \arg SrcAlign bytes.
  1084. ///
  1085. /// This safely handles the case when the src type is smaller than the
  1086. /// destination type; in this situation the values of bits which not
  1087. /// present in the src are undefined.
  1088. static llvm::Value *CreateCoercedLoad(Address Src, llvm::Type *Ty,
  1089. CodeGenFunction &CGF) {
  1090. llvm::Type *SrcTy = Src.getElementType();
  1091. // If SrcTy and Ty are the same, just do a load.
  1092. if (SrcTy == Ty)
  1093. return CGF.Builder.CreateLoad(Src);
  1094. llvm::TypeSize DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty);
  1095. if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
  1096. Src = EnterStructPointerForCoercedAccess(Src, SrcSTy,
  1097. DstSize.getFixedValue(), CGF);
  1098. SrcTy = Src.getElementType();
  1099. }
  1100. llvm::TypeSize SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
  1101. // If the source and destination are integer or pointer types, just do an
  1102. // extension or truncation to the desired type.
  1103. if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
  1104. (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
  1105. llvm::Value *Load = CGF.Builder.CreateLoad(Src);
  1106. return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
  1107. }
  1108. // If load is legal, just bitcast the src pointer.
  1109. if (!SrcSize.isScalable() && !DstSize.isScalable() &&
  1110. SrcSize.getFixedValue() >= DstSize.getFixedValue()) {
  1111. // Generally SrcSize is never greater than DstSize, since this means we are
  1112. // losing bits. However, this can happen in cases where the structure has
  1113. // additional padding, for example due to a user specified alignment.
  1114. //
  1115. // FIXME: Assert that we aren't truncating non-padding bits when have access
  1116. // to that information.
  1117. Src = CGF.Builder.CreateElementBitCast(Src, Ty);
  1118. return CGF.Builder.CreateLoad(Src);
  1119. }
  1120. // If coercing a fixed vector to a scalable vector for ABI compatibility, and
  1121. // the types match, use the llvm.vector.insert intrinsic to perform the
  1122. // conversion.
  1123. if (auto *ScalableDst = dyn_cast<llvm::ScalableVectorType>(Ty)) {
  1124. if (auto *FixedSrc = dyn_cast<llvm::FixedVectorType>(SrcTy)) {
  1125. // If we are casting a fixed i8 vector to a scalable 16 x i1 predicate
  1126. // vector, use a vector insert and bitcast the result.
  1127. bool NeedsBitcast = false;
  1128. auto PredType =
  1129. llvm::ScalableVectorType::get(CGF.Builder.getInt1Ty(), 16);
  1130. llvm::Type *OrigType = Ty;
  1131. if (ScalableDst == PredType &&
  1132. FixedSrc->getElementType() == CGF.Builder.getInt8Ty()) {
  1133. ScalableDst = llvm::ScalableVectorType::get(CGF.Builder.getInt8Ty(), 2);
  1134. NeedsBitcast = true;
  1135. }
  1136. if (ScalableDst->getElementType() == FixedSrc->getElementType()) {
  1137. auto *Load = CGF.Builder.CreateLoad(Src);
  1138. auto *UndefVec = llvm::UndefValue::get(ScalableDst);
  1139. auto *Zero = llvm::Constant::getNullValue(CGF.CGM.Int64Ty);
  1140. llvm::Value *Result = CGF.Builder.CreateInsertVector(
  1141. ScalableDst, UndefVec, Load, Zero, "castScalableSve");
  1142. if (NeedsBitcast)
  1143. Result = CGF.Builder.CreateBitCast(Result, OrigType);
  1144. return Result;
  1145. }
  1146. }
  1147. }
  1148. // Otherwise do coercion through memory. This is stupid, but simple.
  1149. Address Tmp =
  1150. CreateTempAllocaForCoercion(CGF, Ty, Src.getAlignment(), Src.getName());
  1151. CGF.Builder.CreateMemCpy(
  1152. Tmp.getPointer(), Tmp.getAlignment().getAsAlign(), Src.getPointer(),
  1153. Src.getAlignment().getAsAlign(),
  1154. llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize.getKnownMinValue()));
  1155. return CGF.Builder.CreateLoad(Tmp);
  1156. }
  1157. // Function to store a first-class aggregate into memory. We prefer to
  1158. // store the elements rather than the aggregate to be more friendly to
  1159. // fast-isel.
  1160. // FIXME: Do we need to recurse here?
  1161. void CodeGenFunction::EmitAggregateStore(llvm::Value *Val, Address Dest,
  1162. bool DestIsVolatile) {
  1163. // Prefer scalar stores to first-class aggregate stores.
  1164. if (llvm::StructType *STy = dyn_cast<llvm::StructType>(Val->getType())) {
  1165. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  1166. Address EltPtr = Builder.CreateStructGEP(Dest, i);
  1167. llvm::Value *Elt = Builder.CreateExtractValue(Val, i);
  1168. Builder.CreateStore(Elt, EltPtr, DestIsVolatile);
  1169. }
  1170. } else {
  1171. Builder.CreateStore(Val, Dest, DestIsVolatile);
  1172. }
  1173. }
  1174. /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
  1175. /// where the source and destination may have different types. The
  1176. /// destination is known to be aligned to \arg DstAlign bytes.
  1177. ///
  1178. /// This safely handles the case when the src type is larger than the
  1179. /// destination type; the upper bits of the src will be lost.
  1180. static void CreateCoercedStore(llvm::Value *Src,
  1181. Address Dst,
  1182. bool DstIsVolatile,
  1183. CodeGenFunction &CGF) {
  1184. llvm::Type *SrcTy = Src->getType();
  1185. llvm::Type *DstTy = Dst.getElementType();
  1186. if (SrcTy == DstTy) {
  1187. CGF.Builder.CreateStore(Src, Dst, DstIsVolatile);
  1188. return;
  1189. }
  1190. llvm::TypeSize SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
  1191. if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
  1192. Dst = EnterStructPointerForCoercedAccess(Dst, DstSTy,
  1193. SrcSize.getFixedValue(), CGF);
  1194. DstTy = Dst.getElementType();
  1195. }
  1196. llvm::PointerType *SrcPtrTy = llvm::dyn_cast<llvm::PointerType>(SrcTy);
  1197. llvm::PointerType *DstPtrTy = llvm::dyn_cast<llvm::PointerType>(DstTy);
  1198. if (SrcPtrTy && DstPtrTy &&
  1199. SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) {
  1200. Src = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DstTy);
  1201. CGF.Builder.CreateStore(Src, Dst, DstIsVolatile);
  1202. return;
  1203. }
  1204. // If the source and destination are integer or pointer types, just do an
  1205. // extension or truncation to the desired type.
  1206. if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
  1207. (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
  1208. Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
  1209. CGF.Builder.CreateStore(Src, Dst, DstIsVolatile);
  1210. return;
  1211. }
  1212. llvm::TypeSize DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy);
  1213. // If store is legal, just bitcast the src pointer.
  1214. if (isa<llvm::ScalableVectorType>(SrcTy) ||
  1215. isa<llvm::ScalableVectorType>(DstTy) ||
  1216. SrcSize.getFixedValue() <= DstSize.getFixedValue()) {
  1217. Dst = CGF.Builder.CreateElementBitCast(Dst, SrcTy);
  1218. CGF.EmitAggregateStore(Src, Dst, DstIsVolatile);
  1219. } else {
  1220. // Otherwise do coercion through memory. This is stupid, but
  1221. // simple.
  1222. // Generally SrcSize is never greater than DstSize, since this means we are
  1223. // losing bits. However, this can happen in cases where the structure has
  1224. // additional padding, for example due to a user specified alignment.
  1225. //
  1226. // FIXME: Assert that we aren't truncating non-padding bits when have access
  1227. // to that information.
  1228. Address Tmp = CreateTempAllocaForCoercion(CGF, SrcTy, Dst.getAlignment());
  1229. CGF.Builder.CreateStore(Src, Tmp);
  1230. CGF.Builder.CreateMemCpy(
  1231. Dst.getPointer(), Dst.getAlignment().getAsAlign(), Tmp.getPointer(),
  1232. Tmp.getAlignment().getAsAlign(),
  1233. llvm::ConstantInt::get(CGF.IntPtrTy, DstSize.getFixedValue()));
  1234. }
  1235. }
  1236. static Address emitAddressAtOffset(CodeGenFunction &CGF, Address addr,
  1237. const ABIArgInfo &info) {
  1238. if (unsigned offset = info.getDirectOffset()) {
  1239. addr = CGF.Builder.CreateElementBitCast(addr, CGF.Int8Ty);
  1240. addr = CGF.Builder.CreateConstInBoundsByteGEP(addr,
  1241. CharUnits::fromQuantity(offset));
  1242. addr = CGF.Builder.CreateElementBitCast(addr, info.getCoerceToType());
  1243. }
  1244. return addr;
  1245. }
  1246. namespace {
  1247. /// Encapsulates information about the way function arguments from
  1248. /// CGFunctionInfo should be passed to actual LLVM IR function.
  1249. class ClangToLLVMArgMapping {
  1250. static const unsigned InvalidIndex = ~0U;
  1251. unsigned InallocaArgNo;
  1252. unsigned SRetArgNo;
  1253. unsigned TotalIRArgs;
  1254. /// Arguments of LLVM IR function corresponding to single Clang argument.
  1255. struct IRArgs {
  1256. unsigned PaddingArgIndex;
  1257. // Argument is expanded to IR arguments at positions
  1258. // [FirstArgIndex, FirstArgIndex + NumberOfArgs).
  1259. unsigned FirstArgIndex;
  1260. unsigned NumberOfArgs;
  1261. IRArgs()
  1262. : PaddingArgIndex(InvalidIndex), FirstArgIndex(InvalidIndex),
  1263. NumberOfArgs(0) {}
  1264. };
  1265. SmallVector<IRArgs, 8> ArgInfo;
  1266. public:
  1267. ClangToLLVMArgMapping(const ASTContext &Context, const CGFunctionInfo &FI,
  1268. bool OnlyRequiredArgs = false)
  1269. : InallocaArgNo(InvalidIndex), SRetArgNo(InvalidIndex), TotalIRArgs(0),
  1270. ArgInfo(OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size()) {
  1271. construct(Context, FI, OnlyRequiredArgs);
  1272. }
  1273. bool hasInallocaArg() const { return InallocaArgNo != InvalidIndex; }
  1274. unsigned getInallocaArgNo() const {
  1275. assert(hasInallocaArg());
  1276. return InallocaArgNo;
  1277. }
  1278. bool hasSRetArg() const { return SRetArgNo != InvalidIndex; }
  1279. unsigned getSRetArgNo() const {
  1280. assert(hasSRetArg());
  1281. return SRetArgNo;
  1282. }
  1283. unsigned totalIRArgs() const { return TotalIRArgs; }
  1284. bool hasPaddingArg(unsigned ArgNo) const {
  1285. assert(ArgNo < ArgInfo.size());
  1286. return ArgInfo[ArgNo].PaddingArgIndex != InvalidIndex;
  1287. }
  1288. unsigned getPaddingArgNo(unsigned ArgNo) const {
  1289. assert(hasPaddingArg(ArgNo));
  1290. return ArgInfo[ArgNo].PaddingArgIndex;
  1291. }
  1292. /// Returns index of first IR argument corresponding to ArgNo, and their
  1293. /// quantity.
  1294. std::pair<unsigned, unsigned> getIRArgs(unsigned ArgNo) const {
  1295. assert(ArgNo < ArgInfo.size());
  1296. return std::make_pair(ArgInfo[ArgNo].FirstArgIndex,
  1297. ArgInfo[ArgNo].NumberOfArgs);
  1298. }
  1299. private:
  1300. void construct(const ASTContext &Context, const CGFunctionInfo &FI,
  1301. bool OnlyRequiredArgs);
  1302. };
  1303. void ClangToLLVMArgMapping::construct(const ASTContext &Context,
  1304. const CGFunctionInfo &FI,
  1305. bool OnlyRequiredArgs) {
  1306. unsigned IRArgNo = 0;
  1307. bool SwapThisWithSRet = false;
  1308. const ABIArgInfo &RetAI = FI.getReturnInfo();
  1309. if (RetAI.getKind() == ABIArgInfo::Indirect) {
  1310. SwapThisWithSRet = RetAI.isSRetAfterThis();
  1311. SRetArgNo = SwapThisWithSRet ? 1 : IRArgNo++;
  1312. }
  1313. unsigned ArgNo = 0;
  1314. unsigned NumArgs = OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size();
  1315. for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(); ArgNo < NumArgs;
  1316. ++I, ++ArgNo) {
  1317. assert(I != FI.arg_end());
  1318. QualType ArgType = I->type;
  1319. const ABIArgInfo &AI = I->info;
  1320. // Collect data about IR arguments corresponding to Clang argument ArgNo.
  1321. auto &IRArgs = ArgInfo[ArgNo];
  1322. if (AI.getPaddingType())
  1323. IRArgs.PaddingArgIndex = IRArgNo++;
  1324. switch (AI.getKind()) {
  1325. case ABIArgInfo::Extend:
  1326. case ABIArgInfo::Direct: {
  1327. // FIXME: handle sseregparm someday...
  1328. llvm::StructType *STy = dyn_cast<llvm::StructType>(AI.getCoerceToType());
  1329. if (AI.isDirect() && AI.getCanBeFlattened() && STy) {
  1330. IRArgs.NumberOfArgs = STy->getNumElements();
  1331. } else {
  1332. IRArgs.NumberOfArgs = 1;
  1333. }
  1334. break;
  1335. }
  1336. case ABIArgInfo::Indirect:
  1337. case ABIArgInfo::IndirectAliased:
  1338. IRArgs.NumberOfArgs = 1;
  1339. break;
  1340. case ABIArgInfo::Ignore:
  1341. case ABIArgInfo::InAlloca:
  1342. // ignore and inalloca doesn't have matching LLVM parameters.
  1343. IRArgs.NumberOfArgs = 0;
  1344. break;
  1345. case ABIArgInfo::CoerceAndExpand:
  1346. IRArgs.NumberOfArgs = AI.getCoerceAndExpandTypeSequence().size();
  1347. break;
  1348. case ABIArgInfo::Expand:
  1349. IRArgs.NumberOfArgs = getExpansionSize(ArgType, Context);
  1350. break;
  1351. }
  1352. if (IRArgs.NumberOfArgs > 0) {
  1353. IRArgs.FirstArgIndex = IRArgNo;
  1354. IRArgNo += IRArgs.NumberOfArgs;
  1355. }
  1356. // Skip over the sret parameter when it comes second. We already handled it
  1357. // above.
  1358. if (IRArgNo == 1 && SwapThisWithSRet)
  1359. IRArgNo++;
  1360. }
  1361. assert(ArgNo == ArgInfo.size());
  1362. if (FI.usesInAlloca())
  1363. InallocaArgNo = IRArgNo++;
  1364. TotalIRArgs = IRArgNo;
  1365. }
  1366. } // namespace
  1367. /***/
  1368. bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
  1369. const auto &RI = FI.getReturnInfo();
  1370. return RI.isIndirect() || (RI.isInAlloca() && RI.getInAllocaSRet());
  1371. }
  1372. bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) {
  1373. return ReturnTypeUsesSRet(FI) &&
  1374. getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();
  1375. }
  1376. bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
  1377. if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
  1378. switch (BT->getKind()) {
  1379. default:
  1380. return false;
  1381. case BuiltinType::Float:
  1382. return getTarget().useObjCFPRetForRealType(FloatModeKind::Float);
  1383. case BuiltinType::Double:
  1384. return getTarget().useObjCFPRetForRealType(FloatModeKind::Double);
  1385. case BuiltinType::LongDouble:
  1386. return getTarget().useObjCFPRetForRealType(FloatModeKind::LongDouble);
  1387. }
  1388. }
  1389. return false;
  1390. }
  1391. bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) {
  1392. if (const ComplexType *CT = ResultType->getAs<ComplexType>()) {
  1393. if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) {
  1394. if (BT->getKind() == BuiltinType::LongDouble)
  1395. return getTarget().useObjCFP2RetForComplexLongDouble();
  1396. }
  1397. }
  1398. return false;
  1399. }
  1400. llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
  1401. const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD);
  1402. return GetFunctionType(FI);
  1403. }
  1404. llvm::FunctionType *
  1405. CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
  1406. bool Inserted = FunctionsBeingProcessed.insert(&FI).second;
  1407. (void)Inserted;
  1408. assert(Inserted && "Recursively being processed?");
  1409. llvm::Type *resultType = nullptr;
  1410. const ABIArgInfo &retAI = FI.getReturnInfo();
  1411. switch (retAI.getKind()) {
  1412. case ABIArgInfo::Expand:
  1413. case ABIArgInfo::IndirectAliased:
  1414. llvm_unreachable("Invalid ABI kind for return argument");
  1415. case ABIArgInfo::Extend:
  1416. case ABIArgInfo::Direct:
  1417. resultType = retAI.getCoerceToType();
  1418. break;
  1419. case ABIArgInfo::InAlloca:
  1420. if (retAI.getInAllocaSRet()) {
  1421. // sret things on win32 aren't void, they return the sret pointer.
  1422. QualType ret = FI.getReturnType();
  1423. llvm::Type *ty = ConvertType(ret);
  1424. unsigned addressSpace = CGM.getTypes().getTargetAddressSpace(ret);
  1425. resultType = llvm::PointerType::get(ty, addressSpace);
  1426. } else {
  1427. resultType = llvm::Type::getVoidTy(getLLVMContext());
  1428. }
  1429. break;
  1430. case ABIArgInfo::Indirect:
  1431. case ABIArgInfo::Ignore:
  1432. resultType = llvm::Type::getVoidTy(getLLVMContext());
  1433. break;
  1434. case ABIArgInfo::CoerceAndExpand:
  1435. resultType = retAI.getUnpaddedCoerceAndExpandType();
  1436. break;
  1437. }
  1438. ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI, true);
  1439. SmallVector<llvm::Type*, 8> ArgTypes(IRFunctionArgs.totalIRArgs());
  1440. // Add type for sret argument.
  1441. if (IRFunctionArgs.hasSRetArg()) {
  1442. QualType Ret = FI.getReturnType();
  1443. llvm::Type *Ty = ConvertType(Ret);
  1444. unsigned AddressSpace = CGM.getTypes().getTargetAddressSpace(Ret);
  1445. ArgTypes[IRFunctionArgs.getSRetArgNo()] =
  1446. llvm::PointerType::get(Ty, AddressSpace);
  1447. }
  1448. // Add type for inalloca argument.
  1449. if (IRFunctionArgs.hasInallocaArg()) {
  1450. auto ArgStruct = FI.getArgStruct();
  1451. assert(ArgStruct);
  1452. ArgTypes[IRFunctionArgs.getInallocaArgNo()] = ArgStruct->getPointerTo();
  1453. }
  1454. // Add in all of the required arguments.
  1455. unsigned ArgNo = 0;
  1456. CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
  1457. ie = it + FI.getNumRequiredArgs();
  1458. for (; it != ie; ++it, ++ArgNo) {
  1459. const ABIArgInfo &ArgInfo = it->info;
  1460. // Insert a padding type to ensure proper alignment.
  1461. if (IRFunctionArgs.hasPaddingArg(ArgNo))
  1462. ArgTypes[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
  1463. ArgInfo.getPaddingType();
  1464. unsigned FirstIRArg, NumIRArgs;
  1465. std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
  1466. switch (ArgInfo.getKind()) {
  1467. case ABIArgInfo::Ignore:
  1468. case ABIArgInfo::InAlloca:
  1469. assert(NumIRArgs == 0);
  1470. break;
  1471. case ABIArgInfo::Indirect: {
  1472. assert(NumIRArgs == 1);
  1473. // indirect arguments are always on the stack, which is alloca addr space.
  1474. llvm::Type *LTy = ConvertTypeForMem(it->type);
  1475. ArgTypes[FirstIRArg] = LTy->getPointerTo(
  1476. CGM.getDataLayout().getAllocaAddrSpace());
  1477. break;
  1478. }
  1479. case ABIArgInfo::IndirectAliased: {
  1480. assert(NumIRArgs == 1);
  1481. llvm::Type *LTy = ConvertTypeForMem(it->type);
  1482. ArgTypes[FirstIRArg] = LTy->getPointerTo(ArgInfo.getIndirectAddrSpace());
  1483. break;
  1484. }
  1485. case ABIArgInfo::Extend:
  1486. case ABIArgInfo::Direct: {
  1487. // Fast-isel and the optimizer generally like scalar values better than
  1488. // FCAs, so we flatten them if this is safe to do for this argument.
  1489. llvm::Type *argType = ArgInfo.getCoerceToType();
  1490. llvm::StructType *st = dyn_cast<llvm::StructType>(argType);
  1491. if (st && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
  1492. assert(NumIRArgs == st->getNumElements());
  1493. for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
  1494. ArgTypes[FirstIRArg + i] = st->getElementType(i);
  1495. } else {
  1496. assert(NumIRArgs == 1);
  1497. ArgTypes[FirstIRArg] = argType;
  1498. }
  1499. break;
  1500. }
  1501. case ABIArgInfo::CoerceAndExpand: {
  1502. auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
  1503. for (auto *EltTy : ArgInfo.getCoerceAndExpandTypeSequence()) {
  1504. *ArgTypesIter++ = EltTy;
  1505. }
  1506. assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
  1507. break;
  1508. }
  1509. case ABIArgInfo::Expand:
  1510. auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
  1511. getExpandedTypes(it->type, ArgTypesIter);
  1512. assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
  1513. break;
  1514. }
  1515. }
  1516. bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased;
  1517. assert(Erased && "Not in set?");
  1518. return llvm::FunctionType::get(resultType, ArgTypes, FI.isVariadic());
  1519. }
  1520. llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
  1521. const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
  1522. const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
  1523. if (!isFuncTypeConvertible(FPT))
  1524. return llvm::StructType::get(getLLVMContext());
  1525. return GetFunctionType(GD);
  1526. }
  1527. static void AddAttributesFromFunctionProtoType(ASTContext &Ctx,
  1528. llvm::AttrBuilder &FuncAttrs,
  1529. const FunctionProtoType *FPT) {
  1530. if (!FPT)
  1531. return;
  1532. if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()) &&
  1533. FPT->isNothrow())
  1534. FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
  1535. }
  1536. static void AddAttributesFromAssumes(llvm::AttrBuilder &FuncAttrs,
  1537. const Decl *Callee) {
  1538. if (!Callee)
  1539. return;
  1540. SmallVector<StringRef, 4> Attrs;
  1541. for (const AssumptionAttr *AA : Callee->specific_attrs<AssumptionAttr>())
  1542. AA->getAssumption().split(Attrs, ",");
  1543. if (!Attrs.empty())
  1544. FuncAttrs.addAttribute(llvm::AssumptionAttrKey,
  1545. llvm::join(Attrs.begin(), Attrs.end(), ","));
  1546. }
  1547. bool CodeGenModule::MayDropFunctionReturn(const ASTContext &Context,
  1548. QualType ReturnType) const {
  1549. // We can't just discard the return value for a record type with a
  1550. // complex destructor or a non-trivially copyable type.
  1551. if (const RecordType *RT =
  1552. ReturnType.getCanonicalType()->getAs<RecordType>()) {
  1553. if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl()))
  1554. return ClassDecl->hasTrivialDestructor();
  1555. }
  1556. return ReturnType.isTriviallyCopyableType(Context);
  1557. }
  1558. static bool HasStrictReturn(const CodeGenModule &Module, QualType RetTy,
  1559. const Decl *TargetDecl) {
  1560. // As-is msan can not tolerate noundef mismatch between caller and
  1561. // implementation. Mismatch is possible for e.g. indirect calls from C-caller
  1562. // into C++. Such mismatches lead to confusing false reports. To avoid
  1563. // expensive workaround on msan we enforce initialization event in uncommon
  1564. // cases where it's allowed.
  1565. if (Module.getLangOpts().Sanitize.has(SanitizerKind::Memory))
  1566. return true;
  1567. // C++ explicitly makes returning undefined values UB. C's rule only applies
  1568. // to used values, so we never mark them noundef for now.
  1569. if (!Module.getLangOpts().CPlusPlus)
  1570. return false;
  1571. if (TargetDecl) {
  1572. if (const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(TargetDecl)) {
  1573. if (FDecl->isExternC())
  1574. return false;
  1575. } else if (const VarDecl *VDecl = dyn_cast<VarDecl>(TargetDecl)) {
  1576. // Function pointer.
  1577. if (VDecl->isExternC())
  1578. return false;
  1579. }
  1580. }
  1581. // We don't want to be too aggressive with the return checking, unless
  1582. // it's explicit in the code opts or we're using an appropriate sanitizer.
  1583. // Try to respect what the programmer intended.
  1584. return Module.getCodeGenOpts().StrictReturn ||
  1585. !Module.MayDropFunctionReturn(Module.getContext(), RetTy) ||
  1586. Module.getLangOpts().Sanitize.has(SanitizerKind::Return);
  1587. }
  1588. void CodeGenModule::getDefaultFunctionAttributes(StringRef Name,
  1589. bool HasOptnone,
  1590. bool AttrOnCallSite,
  1591. llvm::AttrBuilder &FuncAttrs) {
  1592. // OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed.
  1593. if (!HasOptnone) {
  1594. if (CodeGenOpts.OptimizeSize)
  1595. FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize);
  1596. if (CodeGenOpts.OptimizeSize == 2)
  1597. FuncAttrs.addAttribute(llvm::Attribute::MinSize);
  1598. }
  1599. if (CodeGenOpts.DisableRedZone)
  1600. FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
  1601. if (CodeGenOpts.IndirectTlsSegRefs)
  1602. FuncAttrs.addAttribute("indirect-tls-seg-refs");
  1603. if (CodeGenOpts.NoImplicitFloat)
  1604. FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
  1605. if (AttrOnCallSite) {
  1606. // Attributes that should go on the call site only.
  1607. // FIXME: Look for 'BuiltinAttr' on the function rather than re-checking
  1608. // the -fno-builtin-foo list.
  1609. if (!CodeGenOpts.SimplifyLibCalls || LangOpts.isNoBuiltinFunc(Name))
  1610. FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin);
  1611. if (!CodeGenOpts.TrapFuncName.empty())
  1612. FuncAttrs.addAttribute("trap-func-name", CodeGenOpts.TrapFuncName);
  1613. } else {
  1614. switch (CodeGenOpts.getFramePointer()) {
  1615. case CodeGenOptions::FramePointerKind::None:
  1616. // This is the default behavior.
  1617. break;
  1618. case CodeGenOptions::FramePointerKind::NonLeaf:
  1619. case CodeGenOptions::FramePointerKind::All:
  1620. FuncAttrs.addAttribute("frame-pointer",
  1621. CodeGenOptions::getFramePointerKindName(
  1622. CodeGenOpts.getFramePointer()));
  1623. }
  1624. if (CodeGenOpts.LessPreciseFPMAD)
  1625. FuncAttrs.addAttribute("less-precise-fpmad", "true");
  1626. if (CodeGenOpts.NullPointerIsValid)
  1627. FuncAttrs.addAttribute(llvm::Attribute::NullPointerIsValid);
  1628. if (CodeGenOpts.FPDenormalMode != llvm::DenormalMode::getIEEE())
  1629. FuncAttrs.addAttribute("denormal-fp-math",
  1630. CodeGenOpts.FPDenormalMode.str());
  1631. if (CodeGenOpts.FP32DenormalMode != CodeGenOpts.FPDenormalMode) {
  1632. FuncAttrs.addAttribute(
  1633. "denormal-fp-math-f32",
  1634. CodeGenOpts.FP32DenormalMode.str());
  1635. }
  1636. if (LangOpts.getDefaultExceptionMode() == LangOptions::FPE_Ignore)
  1637. FuncAttrs.addAttribute("no-trapping-math", "true");
  1638. // TODO: Are these all needed?
  1639. // unsafe/inf/nan/nsz are handled by instruction-level FastMathFlags.
  1640. if (LangOpts.NoHonorInfs)
  1641. FuncAttrs.addAttribute("no-infs-fp-math", "true");
  1642. if (LangOpts.NoHonorNaNs)
  1643. FuncAttrs.addAttribute("no-nans-fp-math", "true");
  1644. if (LangOpts.ApproxFunc)
  1645. FuncAttrs.addAttribute("approx-func-fp-math", "true");
  1646. if (LangOpts.AllowFPReassoc && LangOpts.AllowRecip &&
  1647. LangOpts.NoSignedZero && LangOpts.ApproxFunc &&
  1648. (LangOpts.getDefaultFPContractMode() ==
  1649. LangOptions::FPModeKind::FPM_Fast ||
  1650. LangOpts.getDefaultFPContractMode() ==
  1651. LangOptions::FPModeKind::FPM_FastHonorPragmas))
  1652. FuncAttrs.addAttribute("unsafe-fp-math", "true");
  1653. if (CodeGenOpts.SoftFloat)
  1654. FuncAttrs.addAttribute("use-soft-float", "true");
  1655. FuncAttrs.addAttribute("stack-protector-buffer-size",
  1656. llvm::utostr(CodeGenOpts.SSPBufferSize));
  1657. if (LangOpts.NoSignedZero)
  1658. FuncAttrs.addAttribute("no-signed-zeros-fp-math", "true");
  1659. // TODO: Reciprocal estimate codegen options should apply to instructions?
  1660. const std::vector<std::string> &Recips = CodeGenOpts.Reciprocals;
  1661. if (!Recips.empty())
  1662. FuncAttrs.addAttribute("reciprocal-estimates",
  1663. llvm::join(Recips, ","));
  1664. if (!CodeGenOpts.PreferVectorWidth.empty() &&
  1665. CodeGenOpts.PreferVectorWidth != "none")
  1666. FuncAttrs.addAttribute("prefer-vector-width",
  1667. CodeGenOpts.PreferVectorWidth);
  1668. if (CodeGenOpts.StackRealignment)
  1669. FuncAttrs.addAttribute("stackrealign");
  1670. if (CodeGenOpts.Backchain)
  1671. FuncAttrs.addAttribute("backchain");
  1672. if (CodeGenOpts.EnableSegmentedStacks)
  1673. FuncAttrs.addAttribute("split-stack");
  1674. if (CodeGenOpts.SpeculativeLoadHardening)
  1675. FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);
  1676. // Add zero-call-used-regs attribute.
  1677. switch (CodeGenOpts.getZeroCallUsedRegs()) {
  1678. case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Skip:
  1679. FuncAttrs.removeAttribute("zero-call-used-regs");
  1680. break;
  1681. case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPRArg:
  1682. FuncAttrs.addAttribute("zero-call-used-regs", "used-gpr-arg");
  1683. break;
  1684. case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPR:
  1685. FuncAttrs.addAttribute("zero-call-used-regs", "used-gpr");
  1686. break;
  1687. case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedArg:
  1688. FuncAttrs.addAttribute("zero-call-used-regs", "used-arg");
  1689. break;
  1690. case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Used:
  1691. FuncAttrs.addAttribute("zero-call-used-regs", "used");
  1692. break;
  1693. case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPRArg:
  1694. FuncAttrs.addAttribute("zero-call-used-regs", "all-gpr-arg");
  1695. break;
  1696. case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPR:
  1697. FuncAttrs.addAttribute("zero-call-used-regs", "all-gpr");
  1698. break;
  1699. case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllArg:
  1700. FuncAttrs.addAttribute("zero-call-used-regs", "all-arg");
  1701. break;
  1702. case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::All:
  1703. FuncAttrs.addAttribute("zero-call-used-regs", "all");
  1704. break;
  1705. }
  1706. }
  1707. if (getLangOpts().assumeFunctionsAreConvergent()) {
  1708. // Conservatively, mark all functions and calls in CUDA and OpenCL as
  1709. // convergent (meaning, they may call an intrinsically convergent op, such
  1710. // as __syncthreads() / barrier(), and so can't have certain optimizations
  1711. // applied around them). LLVM will remove this attribute where it safely
  1712. // can.
  1713. FuncAttrs.addAttribute(llvm::Attribute::Convergent);
  1714. }
  1715. // TODO: NoUnwind attribute should be added for other GPU modes HIP,
  1716. // SYCL, OpenMP offload. AFAIK, none of them support exceptions in device
  1717. // code.
  1718. if ((getLangOpts().CUDA && getLangOpts().CUDAIsDevice) ||
  1719. getLangOpts().OpenCL) {
  1720. FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
  1721. }
  1722. for (StringRef Attr : CodeGenOpts.DefaultFunctionAttrs) {
  1723. StringRef Var, Value;
  1724. std::tie(Var, Value) = Attr.split('=');
  1725. FuncAttrs.addAttribute(Var, Value);
  1726. }
  1727. }
  1728. void CodeGenModule::addDefaultFunctionDefinitionAttributes(llvm::Function &F) {
  1729. llvm::AttrBuilder FuncAttrs(F.getContext());
  1730. getDefaultFunctionAttributes(F.getName(), F.hasOptNone(),
  1731. /* AttrOnCallSite = */ false, FuncAttrs);
  1732. // TODO: call GetCPUAndFeaturesAttributes?
  1733. F.addFnAttrs(FuncAttrs);
  1734. }
  1735. void CodeGenModule::addDefaultFunctionDefinitionAttributes(
  1736. llvm::AttrBuilder &attrs) {
  1737. getDefaultFunctionAttributes(/*function name*/ "", /*optnone*/ false,
  1738. /*for call*/ false, attrs);
  1739. GetCPUAndFeaturesAttributes(GlobalDecl(), attrs);
  1740. }
  1741. static void addNoBuiltinAttributes(llvm::AttrBuilder &FuncAttrs,
  1742. const LangOptions &LangOpts,
  1743. const NoBuiltinAttr *NBA = nullptr) {
  1744. auto AddNoBuiltinAttr = [&FuncAttrs](StringRef BuiltinName) {
  1745. SmallString<32> AttributeName;
  1746. AttributeName += "no-builtin-";
  1747. AttributeName += BuiltinName;
  1748. FuncAttrs.addAttribute(AttributeName);
  1749. };
  1750. // First, handle the language options passed through -fno-builtin.
  1751. if (LangOpts.NoBuiltin) {
  1752. // -fno-builtin disables them all.
  1753. FuncAttrs.addAttribute("no-builtins");
  1754. return;
  1755. }
  1756. // Then, add attributes for builtins specified through -fno-builtin-<name>.
  1757. llvm::for_each(LangOpts.NoBuiltinFuncs, AddNoBuiltinAttr);
  1758. // Now, let's check the __attribute__((no_builtin("...")) attribute added to
  1759. // the source.
  1760. if (!NBA)
  1761. return;
  1762. // If there is a wildcard in the builtin names specified through the
  1763. // attribute, disable them all.
  1764. if (llvm::is_contained(NBA->builtinNames(), "*")) {
  1765. FuncAttrs.addAttribute("no-builtins");
  1766. return;
  1767. }
  1768. // And last, add the rest of the builtin names.
  1769. llvm::for_each(NBA->builtinNames(), AddNoBuiltinAttr);
  1770. }
  1771. static bool DetermineNoUndef(QualType QTy, CodeGenTypes &Types,
  1772. const llvm::DataLayout &DL, const ABIArgInfo &AI,
  1773. bool CheckCoerce = true) {
  1774. llvm::Type *Ty = Types.ConvertTypeForMem(QTy);
  1775. if (AI.getKind() == ABIArgInfo::Indirect)
  1776. return true;
  1777. if (AI.getKind() == ABIArgInfo::Extend)
  1778. return true;
  1779. if (!DL.typeSizeEqualsStoreSize(Ty))
  1780. // TODO: This will result in a modest amount of values not marked noundef
  1781. // when they could be. We care about values that *invisibly* contain undef
  1782. // bits from the perspective of LLVM IR.
  1783. return false;
  1784. if (CheckCoerce && AI.canHaveCoerceToType()) {
  1785. llvm::Type *CoerceTy = AI.getCoerceToType();
  1786. if (llvm::TypeSize::isKnownGT(DL.getTypeSizeInBits(CoerceTy),
  1787. DL.getTypeSizeInBits(Ty)))
  1788. // If we're coercing to a type with a greater size than the canonical one,
  1789. // we're introducing new undef bits.
  1790. // Coercing to a type of smaller or equal size is ok, as we know that
  1791. // there's no internal padding (typeSizeEqualsStoreSize).
  1792. return false;
  1793. }
  1794. if (QTy->isBitIntType())
  1795. return true;
  1796. if (QTy->isReferenceType())
  1797. return true;
  1798. if (QTy->isNullPtrType())
  1799. return false;
  1800. if (QTy->isMemberPointerType())
  1801. // TODO: Some member pointers are `noundef`, but it depends on the ABI. For
  1802. // now, never mark them.
  1803. return false;
  1804. if (QTy->isScalarType()) {
  1805. if (const ComplexType *Complex = dyn_cast<ComplexType>(QTy))
  1806. return DetermineNoUndef(Complex->getElementType(), Types, DL, AI, false);
  1807. return true;
  1808. }
  1809. if (const VectorType *Vector = dyn_cast<VectorType>(QTy))
  1810. return DetermineNoUndef(Vector->getElementType(), Types, DL, AI, false);
  1811. if (const MatrixType *Matrix = dyn_cast<MatrixType>(QTy))
  1812. return DetermineNoUndef(Matrix->getElementType(), Types, DL, AI, false);
  1813. if (const ArrayType *Array = dyn_cast<ArrayType>(QTy))
  1814. return DetermineNoUndef(Array->getElementType(), Types, DL, AI, false);
  1815. // TODO: Some structs may be `noundef`, in specific situations.
  1816. return false;
  1817. }
  1818. /// Check if the argument of a function has maybe_undef attribute.
  1819. static bool IsArgumentMaybeUndef(const Decl *TargetDecl,
  1820. unsigned NumRequiredArgs, unsigned ArgNo) {
  1821. const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl);
  1822. if (!FD)
  1823. return false;
  1824. // Assume variadic arguments do not have maybe_undef attribute.
  1825. if (ArgNo >= NumRequiredArgs)
  1826. return false;
  1827. // Check if argument has maybe_undef attribute.
  1828. if (ArgNo < FD->getNumParams()) {
  1829. const ParmVarDecl *Param = FD->getParamDecl(ArgNo);
  1830. if (Param && Param->hasAttr<MaybeUndefAttr>())
  1831. return true;
  1832. }
  1833. return false;
  1834. }
  1835. /// Construct the IR attribute list of a function or call.
  1836. ///
  1837. /// When adding an attribute, please consider where it should be handled:
  1838. ///
  1839. /// - getDefaultFunctionAttributes is for attributes that are essentially
  1840. /// part of the global target configuration (but perhaps can be
  1841. /// overridden on a per-function basis). Adding attributes there
  1842. /// will cause them to also be set in frontends that build on Clang's
  1843. /// target-configuration logic, as well as for code defined in library
  1844. /// modules such as CUDA's libdevice.
  1845. ///
  1846. /// - ConstructAttributeList builds on top of getDefaultFunctionAttributes
  1847. /// and adds declaration-specific, convention-specific, and
  1848. /// frontend-specific logic. The last is of particular importance:
  1849. /// attributes that restrict how the frontend generates code must be
  1850. /// added here rather than getDefaultFunctionAttributes.
  1851. ///
  1852. void CodeGenModule::ConstructAttributeList(StringRef Name,
  1853. const CGFunctionInfo &FI,
  1854. CGCalleeInfo CalleeInfo,
  1855. llvm::AttributeList &AttrList,
  1856. unsigned &CallingConv,
  1857. bool AttrOnCallSite, bool IsThunk) {
  1858. llvm::AttrBuilder FuncAttrs(getLLVMContext());
  1859. llvm::AttrBuilder RetAttrs(getLLVMContext());
  1860. // Collect function IR attributes from the CC lowering.
  1861. // We'll collect the paramete and result attributes later.
  1862. CallingConv = FI.getEffectiveCallingConvention();
  1863. if (FI.isNoReturn())
  1864. FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
  1865. if (FI.isCmseNSCall())
  1866. FuncAttrs.addAttribute("cmse_nonsecure_call");
  1867. // Collect function IR attributes from the callee prototype if we have one.
  1868. AddAttributesFromFunctionProtoType(getContext(), FuncAttrs,
  1869. CalleeInfo.getCalleeFunctionProtoType());
  1870. const Decl *TargetDecl = CalleeInfo.getCalleeDecl().getDecl();
  1871. // Attach assumption attributes to the declaration. If this is a call
  1872. // site, attach assumptions from the caller to the call as well.
  1873. AddAttributesFromAssumes(FuncAttrs, TargetDecl);
  1874. bool HasOptnone = false;
  1875. // The NoBuiltinAttr attached to the target FunctionDecl.
  1876. const NoBuiltinAttr *NBA = nullptr;
  1877. // Some ABIs may result in additional accesses to arguments that may
  1878. // otherwise not be present.
  1879. auto AddPotentialArgAccess = [&]() {
  1880. llvm::Attribute A = FuncAttrs.getAttribute(llvm::Attribute::Memory);
  1881. if (A.isValid())
  1882. FuncAttrs.addMemoryAttr(A.getMemoryEffects() |
  1883. llvm::MemoryEffects::argMemOnly());
  1884. };
  1885. // Collect function IR attributes based on declaration-specific
  1886. // information.
  1887. // FIXME: handle sseregparm someday...
  1888. if (TargetDecl) {
  1889. if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
  1890. FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);
  1891. if (TargetDecl->hasAttr<NoThrowAttr>())
  1892. FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
  1893. if (TargetDecl->hasAttr<NoReturnAttr>())
  1894. FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
  1895. if (TargetDecl->hasAttr<ColdAttr>())
  1896. FuncAttrs.addAttribute(llvm::Attribute::Cold);
  1897. if (TargetDecl->hasAttr<HotAttr>())
  1898. FuncAttrs.addAttribute(llvm::Attribute::Hot);
  1899. if (TargetDecl->hasAttr<NoDuplicateAttr>())
  1900. FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate);
  1901. if (TargetDecl->hasAttr<ConvergentAttr>())
  1902. FuncAttrs.addAttribute(llvm::Attribute::Convergent);
  1903. if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
  1904. AddAttributesFromFunctionProtoType(
  1905. getContext(), FuncAttrs, Fn->getType()->getAs<FunctionProtoType>());
  1906. if (AttrOnCallSite && Fn->isReplaceableGlobalAllocationFunction()) {
  1907. // A sane operator new returns a non-aliasing pointer.
  1908. auto Kind = Fn->getDeclName().getCXXOverloadedOperator();
  1909. if (getCodeGenOpts().AssumeSaneOperatorNew &&
  1910. (Kind == OO_New || Kind == OO_Array_New))
  1911. RetAttrs.addAttribute(llvm::Attribute::NoAlias);
  1912. }
  1913. const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);
  1914. const bool IsVirtualCall = MD && MD->isVirtual();
  1915. // Don't use [[noreturn]], _Noreturn or [[no_builtin]] for a call to a
  1916. // virtual function. These attributes are not inherited by overloads.
  1917. if (!(AttrOnCallSite && IsVirtualCall)) {
  1918. if (Fn->isNoReturn())
  1919. FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
  1920. NBA = Fn->getAttr<NoBuiltinAttr>();
  1921. }
  1922. // Only place nomerge attribute on call sites, never functions. This
  1923. // allows it to work on indirect virtual function calls.
  1924. if (AttrOnCallSite && TargetDecl->hasAttr<NoMergeAttr>())
  1925. FuncAttrs.addAttribute(llvm::Attribute::NoMerge);
  1926. }
  1927. // 'const', 'pure' and 'noalias' attributed functions are also nounwind.
  1928. if (TargetDecl->hasAttr<ConstAttr>()) {
  1929. FuncAttrs.addMemoryAttr(llvm::MemoryEffects::none());
  1930. FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
  1931. // gcc specifies that 'const' functions have greater restrictions than
  1932. // 'pure' functions, so they also cannot have infinite loops.
  1933. FuncAttrs.addAttribute(llvm::Attribute::WillReturn);
  1934. } else if (TargetDecl->hasAttr<PureAttr>()) {
  1935. FuncAttrs.addMemoryAttr(llvm::MemoryEffects::readOnly());
  1936. FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
  1937. // gcc specifies that 'pure' functions cannot have infinite loops.
  1938. FuncAttrs.addAttribute(llvm::Attribute::WillReturn);
  1939. } else if (TargetDecl->hasAttr<NoAliasAttr>()) {
  1940. FuncAttrs.addMemoryAttr(llvm::MemoryEffects::argMemOnly());
  1941. FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
  1942. }
  1943. if (TargetDecl->hasAttr<RestrictAttr>())
  1944. RetAttrs.addAttribute(llvm::Attribute::NoAlias);
  1945. if (TargetDecl->hasAttr<ReturnsNonNullAttr>() &&
  1946. !CodeGenOpts.NullPointerIsValid)
  1947. RetAttrs.addAttribute(llvm::Attribute::NonNull);
  1948. if (TargetDecl->hasAttr<AnyX86NoCallerSavedRegistersAttr>())
  1949. FuncAttrs.addAttribute("no_caller_saved_registers");
  1950. if (TargetDecl->hasAttr<AnyX86NoCfCheckAttr>())
  1951. FuncAttrs.addAttribute(llvm::Attribute::NoCfCheck);
  1952. if (TargetDecl->hasAttr<LeafAttr>())
  1953. FuncAttrs.addAttribute(llvm::Attribute::NoCallback);
  1954. HasOptnone = TargetDecl->hasAttr<OptimizeNoneAttr>();
  1955. if (auto *AllocSize = TargetDecl->getAttr<AllocSizeAttr>()) {
  1956. std::optional<unsigned> NumElemsParam;
  1957. if (AllocSize->getNumElemsParam().isValid())
  1958. NumElemsParam = AllocSize->getNumElemsParam().getLLVMIndex();
  1959. FuncAttrs.addAllocSizeAttr(AllocSize->getElemSizeParam().getLLVMIndex(),
  1960. NumElemsParam);
  1961. }
  1962. if (TargetDecl->hasAttr<OpenCLKernelAttr>()) {
  1963. if (getLangOpts().OpenCLVersion <= 120) {
  1964. // OpenCL v1.2 Work groups are always uniform
  1965. FuncAttrs.addAttribute("uniform-work-group-size", "true");
  1966. } else {
  1967. // OpenCL v2.0 Work groups may be whether uniform or not.
  1968. // '-cl-uniform-work-group-size' compile option gets a hint
  1969. // to the compiler that the global work-size be a multiple of
  1970. // the work-group size specified to clEnqueueNDRangeKernel
  1971. // (i.e. work groups are uniform).
  1972. FuncAttrs.addAttribute("uniform-work-group-size",
  1973. llvm::toStringRef(CodeGenOpts.UniformWGSize));
  1974. }
  1975. }
  1976. }
  1977. // Attach "no-builtins" attributes to:
  1978. // * call sites: both `nobuiltin` and "no-builtins" or "no-builtin-<name>".
  1979. // * definitions: "no-builtins" or "no-builtin-<name>" only.
  1980. // The attributes can come from:
  1981. // * LangOpts: -ffreestanding, -fno-builtin, -fno-builtin-<name>
  1982. // * FunctionDecl attributes: __attribute__((no_builtin(...)))
  1983. addNoBuiltinAttributes(FuncAttrs, getLangOpts(), NBA);
  1984. // Collect function IR attributes based on global settiings.
  1985. getDefaultFunctionAttributes(Name, HasOptnone, AttrOnCallSite, FuncAttrs);
  1986. // Override some default IR attributes based on declaration-specific
  1987. // information.
  1988. if (TargetDecl) {
  1989. if (TargetDecl->hasAttr<NoSpeculativeLoadHardeningAttr>())
  1990. FuncAttrs.removeAttribute(llvm::Attribute::SpeculativeLoadHardening);
  1991. if (TargetDecl->hasAttr<SpeculativeLoadHardeningAttr>())
  1992. FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);
  1993. if (TargetDecl->hasAttr<NoSplitStackAttr>())
  1994. FuncAttrs.removeAttribute("split-stack");
  1995. if (TargetDecl->hasAttr<ZeroCallUsedRegsAttr>()) {
  1996. // A function "__attribute__((...))" overrides the command-line flag.
  1997. auto Kind =
  1998. TargetDecl->getAttr<ZeroCallUsedRegsAttr>()->getZeroCallUsedRegs();
  1999. FuncAttrs.removeAttribute("zero-call-used-regs");
  2000. FuncAttrs.addAttribute(
  2001. "zero-call-used-regs",
  2002. ZeroCallUsedRegsAttr::ConvertZeroCallUsedRegsKindToStr(Kind));
  2003. }
  2004. // Add NonLazyBind attribute to function declarations when -fno-plt
  2005. // is used.
  2006. // FIXME: what if we just haven't processed the function definition
  2007. // yet, or if it's an external definition like C99 inline?
  2008. if (CodeGenOpts.NoPLT) {
  2009. if (auto *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
  2010. if (!Fn->isDefined() && !AttrOnCallSite) {
  2011. FuncAttrs.addAttribute(llvm::Attribute::NonLazyBind);
  2012. }
  2013. }
  2014. }
  2015. }
  2016. // Add "sample-profile-suffix-elision-policy" attribute for internal linkage
  2017. // functions with -funique-internal-linkage-names.
  2018. if (TargetDecl && CodeGenOpts.UniqueInternalLinkageNames) {
  2019. if (const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
  2020. if (!FD->isExternallyVisible())
  2021. FuncAttrs.addAttribute("sample-profile-suffix-elision-policy",
  2022. "selected");
  2023. }
  2024. }
  2025. // Collect non-call-site function IR attributes from declaration-specific
  2026. // information.
  2027. if (!AttrOnCallSite) {
  2028. if (TargetDecl && TargetDecl->hasAttr<CmseNSEntryAttr>())
  2029. FuncAttrs.addAttribute("cmse_nonsecure_entry");
  2030. // Whether tail calls are enabled.
  2031. auto shouldDisableTailCalls = [&] {
  2032. // Should this be honored in getDefaultFunctionAttributes?
  2033. if (CodeGenOpts.DisableTailCalls)
  2034. return true;
  2035. if (!TargetDecl)
  2036. return false;
  2037. if (TargetDecl->hasAttr<DisableTailCallsAttr>() ||
  2038. TargetDecl->hasAttr<AnyX86InterruptAttr>())
  2039. return true;
  2040. if (CodeGenOpts.NoEscapingBlockTailCalls) {
  2041. if (const auto *BD = dyn_cast<BlockDecl>(TargetDecl))
  2042. if (!BD->doesNotEscape())
  2043. return true;
  2044. }
  2045. return false;
  2046. };
  2047. if (shouldDisableTailCalls())
  2048. FuncAttrs.addAttribute("disable-tail-calls", "true");
  2049. // CPU/feature overrides. addDefaultFunctionDefinitionAttributes
  2050. // handles these separately to set them based on the global defaults.
  2051. GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs);
  2052. }
  2053. // Collect attributes from arguments and return values.
  2054. ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI);
  2055. QualType RetTy = FI.getReturnType();
  2056. const ABIArgInfo &RetAI = FI.getReturnInfo();
  2057. const llvm::DataLayout &DL = getDataLayout();
  2058. // Determine if the return type could be partially undef
  2059. if (CodeGenOpts.EnableNoundefAttrs &&
  2060. HasStrictReturn(*this, RetTy, TargetDecl)) {
  2061. if (!RetTy->isVoidType() && RetAI.getKind() != ABIArgInfo::Indirect &&
  2062. DetermineNoUndef(RetTy, getTypes(), DL, RetAI))
  2063. RetAttrs.addAttribute(llvm::Attribute::NoUndef);
  2064. }
  2065. switch (RetAI.getKind()) {
  2066. case ABIArgInfo::Extend:
  2067. if (RetAI.isSignExt())
  2068. RetAttrs.addAttribute(llvm::Attribute::SExt);
  2069. else
  2070. RetAttrs.addAttribute(llvm::Attribute::ZExt);
  2071. [[fallthrough]];
  2072. case ABIArgInfo::Direct:
  2073. if (RetAI.getInReg())
  2074. RetAttrs.addAttribute(llvm::Attribute::InReg);
  2075. break;
  2076. case ABIArgInfo::Ignore:
  2077. break;
  2078. case ABIArgInfo::InAlloca:
  2079. case ABIArgInfo::Indirect: {
  2080. // inalloca and sret disable readnone and readonly
  2081. AddPotentialArgAccess();
  2082. break;
  2083. }
  2084. case ABIArgInfo::CoerceAndExpand:
  2085. break;
  2086. case ABIArgInfo::Expand:
  2087. case ABIArgInfo::IndirectAliased:
  2088. llvm_unreachable("Invalid ABI kind for return argument");
  2089. }
  2090. if (!IsThunk) {
  2091. // FIXME: fix this properly, https://reviews.llvm.org/D100388
  2092. if (const auto *RefTy = RetTy->getAs<ReferenceType>()) {
  2093. QualType PTy = RefTy->getPointeeType();
  2094. if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
  2095. RetAttrs.addDereferenceableAttr(
  2096. getMinimumObjectSize(PTy).getQuantity());
  2097. if (getTypes().getTargetAddressSpace(PTy) == 0 &&
  2098. !CodeGenOpts.NullPointerIsValid)
  2099. RetAttrs.addAttribute(llvm::Attribute::NonNull);
  2100. if (PTy->isObjectType()) {
  2101. llvm::Align Alignment =
  2102. getNaturalPointeeTypeAlignment(RetTy).getAsAlign();
  2103. RetAttrs.addAlignmentAttr(Alignment);
  2104. }
  2105. }
  2106. }
  2107. bool hasUsedSRet = false;
  2108. SmallVector<llvm::AttributeSet, 4> ArgAttrs(IRFunctionArgs.totalIRArgs());
  2109. // Attach attributes to sret.
  2110. if (IRFunctionArgs.hasSRetArg()) {
  2111. llvm::AttrBuilder SRETAttrs(getLLVMContext());
  2112. SRETAttrs.addStructRetAttr(getTypes().ConvertTypeForMem(RetTy));
  2113. hasUsedSRet = true;
  2114. if (RetAI.getInReg())
  2115. SRETAttrs.addAttribute(llvm::Attribute::InReg);
  2116. SRETAttrs.addAlignmentAttr(RetAI.getIndirectAlign().getQuantity());
  2117. ArgAttrs[IRFunctionArgs.getSRetArgNo()] =
  2118. llvm::AttributeSet::get(getLLVMContext(), SRETAttrs);
  2119. }
  2120. // Attach attributes to inalloca argument.
  2121. if (IRFunctionArgs.hasInallocaArg()) {
  2122. llvm::AttrBuilder Attrs(getLLVMContext());
  2123. Attrs.addInAllocaAttr(FI.getArgStruct());
  2124. ArgAttrs[IRFunctionArgs.getInallocaArgNo()] =
  2125. llvm::AttributeSet::get(getLLVMContext(), Attrs);
  2126. }
  2127. // Apply `nonnull`, `dereferencable(N)` and `align N` to the `this` argument,
  2128. // unless this is a thunk function.
  2129. // FIXME: fix this properly, https://reviews.llvm.org/D100388
  2130. if (FI.isInstanceMethod() && !IRFunctionArgs.hasInallocaArg() &&
  2131. !FI.arg_begin()->type->isVoidPointerType() && !IsThunk) {
  2132. auto IRArgs = IRFunctionArgs.getIRArgs(0);
  2133. assert(IRArgs.second == 1 && "Expected only a single `this` pointer.");
  2134. llvm::AttrBuilder Attrs(getLLVMContext());
  2135. QualType ThisTy =
  2136. FI.arg_begin()->type.castAs<PointerType>()->getPointeeType();
  2137. if (!CodeGenOpts.NullPointerIsValid &&
  2138. getTypes().getTargetAddressSpace(FI.arg_begin()->type) == 0) {
  2139. Attrs.addAttribute(llvm::Attribute::NonNull);
  2140. Attrs.addDereferenceableAttr(getMinimumObjectSize(ThisTy).getQuantity());
  2141. } else {
  2142. // FIXME dereferenceable should be correct here, regardless of
  2143. // NullPointerIsValid. However, dereferenceable currently does not always
  2144. // respect NullPointerIsValid and may imply nonnull and break the program.
  2145. // See https://reviews.llvm.org/D66618 for discussions.
  2146. Attrs.addDereferenceableOrNullAttr(
  2147. getMinimumObjectSize(
  2148. FI.arg_begin()->type.castAs<PointerType>()->getPointeeType())
  2149. .getQuantity());
  2150. }
  2151. llvm::Align Alignment =
  2152. getNaturalTypeAlignment(ThisTy, /*BaseInfo=*/nullptr,
  2153. /*TBAAInfo=*/nullptr, /*forPointeeType=*/true)
  2154. .getAsAlign();
  2155. Attrs.addAlignmentAttr(Alignment);
  2156. ArgAttrs[IRArgs.first] = llvm::AttributeSet::get(getLLVMContext(), Attrs);
  2157. }
  2158. unsigned ArgNo = 0;
  2159. for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(),
  2160. E = FI.arg_end();
  2161. I != E; ++I, ++ArgNo) {
  2162. QualType ParamType = I->type;
  2163. const ABIArgInfo &AI = I->info;
  2164. llvm::AttrBuilder Attrs(getLLVMContext());
  2165. // Add attribute for padding argument, if necessary.
  2166. if (IRFunctionArgs.hasPaddingArg(ArgNo)) {
  2167. if (AI.getPaddingInReg()) {
  2168. ArgAttrs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
  2169. llvm::AttributeSet::get(
  2170. getLLVMContext(),
  2171. llvm::AttrBuilder(getLLVMContext()).addAttribute(llvm::Attribute::InReg));
  2172. }
  2173. }
  2174. // Decide whether the argument we're handling could be partially undef
  2175. if (CodeGenOpts.EnableNoundefAttrs &&
  2176. DetermineNoUndef(ParamType, getTypes(), DL, AI)) {
  2177. Attrs.addAttribute(llvm::Attribute::NoUndef);
  2178. }
  2179. // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
  2180. // have the corresponding parameter variable. It doesn't make
  2181. // sense to do it here because parameters are so messed up.
  2182. switch (AI.getKind()) {
  2183. case ABIArgInfo::Extend:
  2184. if (AI.isSignExt())
  2185. Attrs.addAttribute(llvm::Attribute::SExt);
  2186. else
  2187. Attrs.addAttribute(llvm::Attribute::ZExt);
  2188. [[fallthrough]];
  2189. case ABIArgInfo::Direct:
  2190. if (ArgNo == 0 && FI.isChainCall())
  2191. Attrs.addAttribute(llvm::Attribute::Nest);
  2192. else if (AI.getInReg())
  2193. Attrs.addAttribute(llvm::Attribute::InReg);
  2194. Attrs.addStackAlignmentAttr(llvm::MaybeAlign(AI.getDirectAlign()));
  2195. break;
  2196. case ABIArgInfo::Indirect: {
  2197. if (AI.getInReg())
  2198. Attrs.addAttribute(llvm::Attribute::InReg);
  2199. if (AI.getIndirectByVal())
  2200. Attrs.addByValAttr(getTypes().ConvertTypeForMem(ParamType));
  2201. auto *Decl = ParamType->getAsRecordDecl();
  2202. if (CodeGenOpts.PassByValueIsNoAlias && Decl &&
  2203. Decl->getArgPassingRestrictions() == RecordDecl::APK_CanPassInRegs)
  2204. // When calling the function, the pointer passed in will be the only
  2205. // reference to the underlying object. Mark it accordingly.
  2206. Attrs.addAttribute(llvm::Attribute::NoAlias);
  2207. // TODO: We could add the byref attribute if not byval, but it would
  2208. // require updating many testcases.
  2209. CharUnits Align = AI.getIndirectAlign();
  2210. // In a byval argument, it is important that the required
  2211. // alignment of the type is honored, as LLVM might be creating a
  2212. // *new* stack object, and needs to know what alignment to give
  2213. // it. (Sometimes it can deduce a sensible alignment on its own,
  2214. // but not if clang decides it must emit a packed struct, or the
  2215. // user specifies increased alignment requirements.)
  2216. //
  2217. // This is different from indirect *not* byval, where the object
  2218. // exists already, and the align attribute is purely
  2219. // informative.
  2220. assert(!Align.isZero());
  2221. // For now, only add this when we have a byval argument.
  2222. // TODO: be less lazy about updating test cases.
  2223. if (AI.getIndirectByVal())
  2224. Attrs.addAlignmentAttr(Align.getQuantity());
  2225. // byval disables readnone and readonly.
  2226. AddPotentialArgAccess();
  2227. break;
  2228. }
  2229. case ABIArgInfo::IndirectAliased: {
  2230. CharUnits Align = AI.getIndirectAlign();
  2231. Attrs.addByRefAttr(getTypes().ConvertTypeForMem(ParamType));
  2232. Attrs.addAlignmentAttr(Align.getQuantity());
  2233. break;
  2234. }
  2235. case ABIArgInfo::Ignore:
  2236. case ABIArgInfo::Expand:
  2237. case ABIArgInfo::CoerceAndExpand:
  2238. break;
  2239. case ABIArgInfo::InAlloca:
  2240. // inalloca disables readnone and readonly.
  2241. AddPotentialArgAccess();
  2242. continue;
  2243. }
  2244. if (const auto *RefTy = ParamType->getAs<ReferenceType>()) {
  2245. QualType PTy = RefTy->getPointeeType();
  2246. if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
  2247. Attrs.addDereferenceableAttr(
  2248. getMinimumObjectSize(PTy).getQuantity());
  2249. if (getTypes().getTargetAddressSpace(PTy) == 0 &&
  2250. !CodeGenOpts.NullPointerIsValid)
  2251. Attrs.addAttribute(llvm::Attribute::NonNull);
  2252. if (PTy->isObjectType()) {
  2253. llvm::Align Alignment =
  2254. getNaturalPointeeTypeAlignment(ParamType).getAsAlign();
  2255. Attrs.addAlignmentAttr(Alignment);
  2256. }
  2257. }
  2258. // From OpenCL spec v3.0.10 section 6.3.5 Alignment of Types:
  2259. // > For arguments to a __kernel function declared to be a pointer to a
  2260. // > data type, the OpenCL compiler can assume that the pointee is always
  2261. // > appropriately aligned as required by the data type.
  2262. if (TargetDecl && TargetDecl->hasAttr<OpenCLKernelAttr>() &&
  2263. ParamType->isPointerType()) {
  2264. QualType PTy = ParamType->getPointeeType();
  2265. if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {
  2266. llvm::Align Alignment =
  2267. getNaturalPointeeTypeAlignment(ParamType).getAsAlign();
  2268. Attrs.addAlignmentAttr(Alignment);
  2269. }
  2270. }
  2271. switch (FI.getExtParameterInfo(ArgNo).getABI()) {
  2272. case ParameterABI::Ordinary:
  2273. break;
  2274. case ParameterABI::SwiftIndirectResult: {
  2275. // Add 'sret' if we haven't already used it for something, but
  2276. // only if the result is void.
  2277. if (!hasUsedSRet && RetTy->isVoidType()) {
  2278. Attrs.addStructRetAttr(getTypes().ConvertTypeForMem(ParamType));
  2279. hasUsedSRet = true;
  2280. }
  2281. // Add 'noalias' in either case.
  2282. Attrs.addAttribute(llvm::Attribute::NoAlias);
  2283. // Add 'dereferenceable' and 'alignment'.
  2284. auto PTy = ParamType->getPointeeType();
  2285. if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {
  2286. auto info = getContext().getTypeInfoInChars(PTy);
  2287. Attrs.addDereferenceableAttr(info.Width.getQuantity());
  2288. Attrs.addAlignmentAttr(info.Align.getAsAlign());
  2289. }
  2290. break;
  2291. }
  2292. case ParameterABI::SwiftErrorResult:
  2293. Attrs.addAttribute(llvm::Attribute::SwiftError);
  2294. break;
  2295. case ParameterABI::SwiftContext:
  2296. Attrs.addAttribute(llvm::Attribute::SwiftSelf);
  2297. break;
  2298. case ParameterABI::SwiftAsyncContext:
  2299. Attrs.addAttribute(llvm::Attribute::SwiftAsync);
  2300. break;
  2301. }
  2302. if (FI.getExtParameterInfo(ArgNo).isNoEscape())
  2303. Attrs.addAttribute(llvm::Attribute::NoCapture);
  2304. if (Attrs.hasAttributes()) {
  2305. unsigned FirstIRArg, NumIRArgs;
  2306. std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
  2307. for (unsigned i = 0; i < NumIRArgs; i++)
  2308. ArgAttrs[FirstIRArg + i] = ArgAttrs[FirstIRArg + i].addAttributes(
  2309. getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), Attrs));
  2310. }
  2311. }
  2312. assert(ArgNo == FI.arg_size());
  2313. AttrList = llvm::AttributeList::get(
  2314. getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), FuncAttrs),
  2315. llvm::AttributeSet::get(getLLVMContext(), RetAttrs), ArgAttrs);
  2316. }
  2317. /// An argument came in as a promoted argument; demote it back to its
  2318. /// declared type.
  2319. static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
  2320. const VarDecl *var,
  2321. llvm::Value *value) {
  2322. llvm::Type *varType = CGF.ConvertType(var->getType());
  2323. // This can happen with promotions that actually don't change the
  2324. // underlying type, like the enum promotions.
  2325. if (value->getType() == varType) return value;
  2326. assert((varType->isIntegerTy() || varType->isFloatingPointTy())
  2327. && "unexpected promotion type");
  2328. if (isa<llvm::IntegerType>(varType))
  2329. return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
  2330. return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
  2331. }
  2332. /// Returns the attribute (either parameter attribute, or function
  2333. /// attribute), which declares argument ArgNo to be non-null.
  2334. static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD,
  2335. QualType ArgType, unsigned ArgNo) {
  2336. // FIXME: __attribute__((nonnull)) can also be applied to:
  2337. // - references to pointers, where the pointee is known to be
  2338. // nonnull (apparently a Clang extension)
  2339. // - transparent unions containing pointers
  2340. // In the former case, LLVM IR cannot represent the constraint. In
  2341. // the latter case, we have no guarantee that the transparent union
  2342. // is in fact passed as a pointer.
  2343. if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType())
  2344. return nullptr;
  2345. // First, check attribute on parameter itself.
  2346. if (PVD) {
  2347. if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>())
  2348. return ParmNNAttr;
  2349. }
  2350. // Check function attributes.
  2351. if (!FD)
  2352. return nullptr;
  2353. for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) {
  2354. if (NNAttr->isNonNull(ArgNo))
  2355. return NNAttr;
  2356. }
  2357. return nullptr;
  2358. }
  2359. namespace {
  2360. struct CopyBackSwiftError final : EHScopeStack::Cleanup {
  2361. Address Temp;
  2362. Address Arg;
  2363. CopyBackSwiftError(Address temp, Address arg) : Temp(temp), Arg(arg) {}
  2364. void Emit(CodeGenFunction &CGF, Flags flags) override {
  2365. llvm::Value *errorValue = CGF.Builder.CreateLoad(Temp);
  2366. CGF.Builder.CreateStore(errorValue, Arg);
  2367. }
  2368. };
  2369. }
  2370. void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
  2371. llvm::Function *Fn,
  2372. const FunctionArgList &Args) {
  2373. if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>())
  2374. // Naked functions don't have prologues.
  2375. return;
  2376. // If this is an implicit-return-zero function, go ahead and
  2377. // initialize the return value. TODO: it might be nice to have
  2378. // a more general mechanism for this that didn't require synthesized
  2379. // return statements.
  2380. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
  2381. if (FD->hasImplicitReturnZero()) {
  2382. QualType RetTy = FD->getReturnType().getUnqualifiedType();
  2383. llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
  2384. llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
  2385. Builder.CreateStore(Zero, ReturnValue);
  2386. }
  2387. }
  2388. // FIXME: We no longer need the types from FunctionArgList; lift up and
  2389. // simplify.
  2390. ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI);
  2391. assert(Fn->arg_size() == IRFunctionArgs.totalIRArgs());
  2392. // If we're using inalloca, all the memory arguments are GEPs off of the last
  2393. // parameter, which is a pointer to the complete memory area.
  2394. Address ArgStruct = Address::invalid();
  2395. if (IRFunctionArgs.hasInallocaArg()) {
  2396. ArgStruct = Address(Fn->getArg(IRFunctionArgs.getInallocaArgNo()),
  2397. FI.getArgStruct(), FI.getArgStructAlignment());
  2398. assert(ArgStruct.getType() == FI.getArgStruct()->getPointerTo());
  2399. }
  2400. // Name the struct return parameter.
  2401. if (IRFunctionArgs.hasSRetArg()) {
  2402. auto AI = Fn->getArg(IRFunctionArgs.getSRetArgNo());
  2403. AI->setName("agg.result");
  2404. AI->addAttr(llvm::Attribute::NoAlias);
  2405. }
  2406. // Track if we received the parameter as a pointer (indirect, byval, or
  2407. // inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it
  2408. // into a local alloca for us.
  2409. SmallVector<ParamValue, 16> ArgVals;
  2410. ArgVals.reserve(Args.size());
  2411. // Create a pointer value for every parameter declaration. This usually
  2412. // entails copying one or more LLVM IR arguments into an alloca. Don't push
  2413. // any cleanups or do anything that might unwind. We do that separately, so
  2414. // we can push the cleanups in the correct order for the ABI.
  2415. assert(FI.arg_size() == Args.size() &&
  2416. "Mismatch between function signature & arguments.");
  2417. unsigned ArgNo = 0;
  2418. CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
  2419. for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
  2420. i != e; ++i, ++info_it, ++ArgNo) {
  2421. const VarDecl *Arg = *i;
  2422. const ABIArgInfo &ArgI = info_it->info;
  2423. bool isPromoted =
  2424. isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
  2425. // We are converting from ABIArgInfo type to VarDecl type directly, unless
  2426. // the parameter is promoted. In this case we convert to
  2427. // CGFunctionInfo::ArgInfo type with subsequent argument demotion.
  2428. QualType Ty = isPromoted ? info_it->type : Arg->getType();
  2429. assert(hasScalarEvaluationKind(Ty) ==
  2430. hasScalarEvaluationKind(Arg->getType()));
  2431. unsigned FirstIRArg, NumIRArgs;
  2432. std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
  2433. switch (ArgI.getKind()) {
  2434. case ABIArgInfo::InAlloca: {
  2435. assert(NumIRArgs == 0);
  2436. auto FieldIndex = ArgI.getInAllocaFieldIndex();
  2437. Address V =
  2438. Builder.CreateStructGEP(ArgStruct, FieldIndex, Arg->getName());
  2439. if (ArgI.getInAllocaIndirect())
  2440. V = Address(Builder.CreateLoad(V), ConvertTypeForMem(Ty),
  2441. getContext().getTypeAlignInChars(Ty));
  2442. ArgVals.push_back(ParamValue::forIndirect(V));
  2443. break;
  2444. }
  2445. case ABIArgInfo::Indirect:
  2446. case ABIArgInfo::IndirectAliased: {
  2447. assert(NumIRArgs == 1);
  2448. Address ParamAddr = Address(Fn->getArg(FirstIRArg), ConvertTypeForMem(Ty),
  2449. ArgI.getIndirectAlign());
  2450. if (!hasScalarEvaluationKind(Ty)) {
  2451. // Aggregates and complex variables are accessed by reference. All we
  2452. // need to do is realign the value, if requested. Also, if the address
  2453. // may be aliased, copy it to ensure that the parameter variable is
  2454. // mutable and has a unique adress, as C requires.
  2455. Address V = ParamAddr;
  2456. if (ArgI.getIndirectRealign() || ArgI.isIndirectAliased()) {
  2457. Address AlignedTemp = CreateMemTemp(Ty, "coerce");
  2458. // Copy from the incoming argument pointer to the temporary with the
  2459. // appropriate alignment.
  2460. //
  2461. // FIXME: We should have a common utility for generating an aggregate
  2462. // copy.
  2463. CharUnits Size = getContext().getTypeSizeInChars(Ty);
  2464. Builder.CreateMemCpy(
  2465. AlignedTemp.getPointer(), AlignedTemp.getAlignment().getAsAlign(),
  2466. ParamAddr.getPointer(), ParamAddr.getAlignment().getAsAlign(),
  2467. llvm::ConstantInt::get(IntPtrTy, Size.getQuantity()));
  2468. V = AlignedTemp;
  2469. }
  2470. ArgVals.push_back(ParamValue::forIndirect(V));
  2471. } else {
  2472. // Load scalar value from indirect argument.
  2473. llvm::Value *V =
  2474. EmitLoadOfScalar(ParamAddr, false, Ty, Arg->getBeginLoc());
  2475. if (isPromoted)
  2476. V = emitArgumentDemotion(*this, Arg, V);
  2477. ArgVals.push_back(ParamValue::forDirect(V));
  2478. }
  2479. break;
  2480. }
  2481. case ABIArgInfo::Extend:
  2482. case ABIArgInfo::Direct: {
  2483. auto AI = Fn->getArg(FirstIRArg);
  2484. llvm::Type *LTy = ConvertType(Arg->getType());
  2485. // Prepare parameter attributes. So far, only attributes for pointer
  2486. // parameters are prepared. See
  2487. // http://llvm.org/docs/LangRef.html#paramattrs.
  2488. if (ArgI.getDirectOffset() == 0 && LTy->isPointerTy() &&
  2489. ArgI.getCoerceToType()->isPointerTy()) {
  2490. assert(NumIRArgs == 1);
  2491. if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {
  2492. // Set `nonnull` attribute if any.
  2493. if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(),
  2494. PVD->getFunctionScopeIndex()) &&
  2495. !CGM.getCodeGenOpts().NullPointerIsValid)
  2496. AI->addAttr(llvm::Attribute::NonNull);
  2497. QualType OTy = PVD->getOriginalType();
  2498. if (const auto *ArrTy =
  2499. getContext().getAsConstantArrayType(OTy)) {
  2500. // A C99 array parameter declaration with the static keyword also
  2501. // indicates dereferenceability, and if the size is constant we can
  2502. // use the dereferenceable attribute (which requires the size in
  2503. // bytes).
  2504. if (ArrTy->getSizeModifier() == ArrayType::Static) {
  2505. QualType ETy = ArrTy->getElementType();
  2506. llvm::Align Alignment =
  2507. CGM.getNaturalTypeAlignment(ETy).getAsAlign();
  2508. AI->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(Alignment));
  2509. uint64_t ArrSize = ArrTy->getSize().getZExtValue();
  2510. if (!ETy->isIncompleteType() && ETy->isConstantSizeType() &&
  2511. ArrSize) {
  2512. llvm::AttrBuilder Attrs(getLLVMContext());
  2513. Attrs.addDereferenceableAttr(
  2514. getContext().getTypeSizeInChars(ETy).getQuantity() *
  2515. ArrSize);
  2516. AI->addAttrs(Attrs);
  2517. } else if (getContext().getTargetInfo().getNullPointerValue(
  2518. ETy.getAddressSpace()) == 0 &&
  2519. !CGM.getCodeGenOpts().NullPointerIsValid) {
  2520. AI->addAttr(llvm::Attribute::NonNull);
  2521. }
  2522. }
  2523. } else if (const auto *ArrTy =
  2524. getContext().getAsVariableArrayType(OTy)) {
  2525. // For C99 VLAs with the static keyword, we don't know the size so
  2526. // we can't use the dereferenceable attribute, but in addrspace(0)
  2527. // we know that it must be nonnull.
  2528. if (ArrTy->getSizeModifier() == VariableArrayType::Static) {
  2529. QualType ETy = ArrTy->getElementType();
  2530. llvm::Align Alignment =
  2531. CGM.getNaturalTypeAlignment(ETy).getAsAlign();
  2532. AI->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(Alignment));
  2533. if (!getTypes().getTargetAddressSpace(ETy) &&
  2534. !CGM.getCodeGenOpts().NullPointerIsValid)
  2535. AI->addAttr(llvm::Attribute::NonNull);
  2536. }
  2537. }
  2538. // Set `align` attribute if any.
  2539. const auto *AVAttr = PVD->getAttr<AlignValueAttr>();
  2540. if (!AVAttr)
  2541. if (const auto *TOTy = OTy->getAs<TypedefType>())
  2542. AVAttr = TOTy->getDecl()->getAttr<AlignValueAttr>();
  2543. if (AVAttr && !SanOpts.has(SanitizerKind::Alignment)) {
  2544. // If alignment-assumption sanitizer is enabled, we do *not* add
  2545. // alignment attribute here, but emit normal alignment assumption,
  2546. // so the UBSAN check could function.
  2547. llvm::ConstantInt *AlignmentCI =
  2548. cast<llvm::ConstantInt>(EmitScalarExpr(AVAttr->getAlignment()));
  2549. uint64_t AlignmentInt =
  2550. AlignmentCI->getLimitedValue(llvm::Value::MaximumAlignment);
  2551. if (AI->getParamAlign().valueOrOne() < AlignmentInt) {
  2552. AI->removeAttr(llvm::Attribute::AttrKind::Alignment);
  2553. AI->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(
  2554. llvm::Align(AlignmentInt)));
  2555. }
  2556. }
  2557. }
  2558. // Set 'noalias' if an argument type has the `restrict` qualifier.
  2559. if (Arg->getType().isRestrictQualified())
  2560. AI->addAttr(llvm::Attribute::NoAlias);
  2561. }
  2562. // Prepare the argument value. If we have the trivial case, handle it
  2563. // with no muss and fuss.
  2564. if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
  2565. ArgI.getCoerceToType() == ConvertType(Ty) &&
  2566. ArgI.getDirectOffset() == 0) {
  2567. assert(NumIRArgs == 1);
  2568. // LLVM expects swifterror parameters to be used in very restricted
  2569. // ways. Copy the value into a less-restricted temporary.
  2570. llvm::Value *V = AI;
  2571. if (FI.getExtParameterInfo(ArgNo).getABI()
  2572. == ParameterABI::SwiftErrorResult) {
  2573. QualType pointeeTy = Ty->getPointeeType();
  2574. assert(pointeeTy->isPointerType());
  2575. Address temp =
  2576. CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");
  2577. Address arg(V, ConvertTypeForMem(pointeeTy),
  2578. getContext().getTypeAlignInChars(pointeeTy));
  2579. llvm::Value *incomingErrorValue = Builder.CreateLoad(arg);
  2580. Builder.CreateStore(incomingErrorValue, temp);
  2581. V = temp.getPointer();
  2582. // Push a cleanup to copy the value back at the end of the function.
  2583. // The convention does not guarantee that the value will be written
  2584. // back if the function exits with an unwind exception.
  2585. EHStack.pushCleanup<CopyBackSwiftError>(NormalCleanup, temp, arg);
  2586. }
  2587. // Ensure the argument is the correct type.
  2588. if (V->getType() != ArgI.getCoerceToType())
  2589. V = Builder.CreateBitCast(V, ArgI.getCoerceToType());
  2590. if (isPromoted)
  2591. V = emitArgumentDemotion(*this, Arg, V);
  2592. // Because of merging of function types from multiple decls it is
  2593. // possible for the type of an argument to not match the corresponding
  2594. // type in the function type. Since we are codegening the callee
  2595. // in here, add a cast to the argument type.
  2596. llvm::Type *LTy = ConvertType(Arg->getType());
  2597. if (V->getType() != LTy)
  2598. V = Builder.CreateBitCast(V, LTy);
  2599. ArgVals.push_back(ParamValue::forDirect(V));
  2600. break;
  2601. }
  2602. // VLST arguments are coerced to VLATs at the function boundary for
  2603. // ABI consistency. If this is a VLST that was coerced to
  2604. // a VLAT at the function boundary and the types match up, use
  2605. // llvm.vector.extract to convert back to the original VLST.
  2606. if (auto *VecTyTo = dyn_cast<llvm::FixedVectorType>(ConvertType(Ty))) {
  2607. llvm::Value *Coerced = Fn->getArg(FirstIRArg);
  2608. if (auto *VecTyFrom =
  2609. dyn_cast<llvm::ScalableVectorType>(Coerced->getType())) {
  2610. // If we are casting a scalable 16 x i1 predicate vector to a fixed i8
  2611. // vector, bitcast the source and use a vector extract.
  2612. auto PredType =
  2613. llvm::ScalableVectorType::get(Builder.getInt1Ty(), 16);
  2614. if (VecTyFrom == PredType &&
  2615. VecTyTo->getElementType() == Builder.getInt8Ty()) {
  2616. VecTyFrom = llvm::ScalableVectorType::get(Builder.getInt8Ty(), 2);
  2617. Coerced = Builder.CreateBitCast(Coerced, VecTyFrom);
  2618. }
  2619. if (VecTyFrom->getElementType() == VecTyTo->getElementType()) {
  2620. llvm::Value *Zero = llvm::Constant::getNullValue(CGM.Int64Ty);
  2621. assert(NumIRArgs == 1);
  2622. Coerced->setName(Arg->getName() + ".coerce");
  2623. ArgVals.push_back(ParamValue::forDirect(Builder.CreateExtractVector(
  2624. VecTyTo, Coerced, Zero, "castFixedSve")));
  2625. break;
  2626. }
  2627. }
  2628. }
  2629. Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg),
  2630. Arg->getName());
  2631. // Pointer to store into.
  2632. Address Ptr = emitAddressAtOffset(*this, Alloca, ArgI);
  2633. // Fast-isel and the optimizer generally like scalar values better than
  2634. // FCAs, so we flatten them if this is safe to do for this argument.
  2635. llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType());
  2636. if (ArgI.isDirect() && ArgI.getCanBeFlattened() && STy &&
  2637. STy->getNumElements() > 1) {
  2638. uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy);
  2639. llvm::Type *DstTy = Ptr.getElementType();
  2640. uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy);
  2641. Address AddrToStoreInto = Address::invalid();
  2642. if (SrcSize <= DstSize) {
  2643. AddrToStoreInto = Builder.CreateElementBitCast(Ptr, STy);
  2644. } else {
  2645. AddrToStoreInto =
  2646. CreateTempAlloca(STy, Alloca.getAlignment(), "coerce");
  2647. }
  2648. assert(STy->getNumElements() == NumIRArgs);
  2649. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  2650. auto AI = Fn->getArg(FirstIRArg + i);
  2651. AI->setName(Arg->getName() + ".coerce" + Twine(i));
  2652. Address EltPtr = Builder.CreateStructGEP(AddrToStoreInto, i);
  2653. Builder.CreateStore(AI, EltPtr);
  2654. }
  2655. if (SrcSize > DstSize) {
  2656. Builder.CreateMemCpy(Ptr, AddrToStoreInto, DstSize);
  2657. }
  2658. } else {
  2659. // Simple case, just do a coerced store of the argument into the alloca.
  2660. assert(NumIRArgs == 1);
  2661. auto AI = Fn->getArg(FirstIRArg);
  2662. AI->setName(Arg->getName() + ".coerce");
  2663. CreateCoercedStore(AI, Ptr, /*DstIsVolatile=*/false, *this);
  2664. }
  2665. // Match to what EmitParmDecl is expecting for this type.
  2666. if (CodeGenFunction::hasScalarEvaluationKind(Ty)) {
  2667. llvm::Value *V =
  2668. EmitLoadOfScalar(Alloca, false, Ty, Arg->getBeginLoc());
  2669. if (isPromoted)
  2670. V = emitArgumentDemotion(*this, Arg, V);
  2671. ArgVals.push_back(ParamValue::forDirect(V));
  2672. } else {
  2673. ArgVals.push_back(ParamValue::forIndirect(Alloca));
  2674. }
  2675. break;
  2676. }
  2677. case ABIArgInfo::CoerceAndExpand: {
  2678. // Reconstruct into a temporary.
  2679. Address alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));
  2680. ArgVals.push_back(ParamValue::forIndirect(alloca));
  2681. auto coercionType = ArgI.getCoerceAndExpandType();
  2682. alloca = Builder.CreateElementBitCast(alloca, coercionType);
  2683. unsigned argIndex = FirstIRArg;
  2684. for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
  2685. llvm::Type *eltType = coercionType->getElementType(i);
  2686. if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType))
  2687. continue;
  2688. auto eltAddr = Builder.CreateStructGEP(alloca, i);
  2689. auto elt = Fn->getArg(argIndex++);
  2690. Builder.CreateStore(elt, eltAddr);
  2691. }
  2692. assert(argIndex == FirstIRArg + NumIRArgs);
  2693. break;
  2694. }
  2695. case ABIArgInfo::Expand: {
  2696. // If this structure was expanded into multiple arguments then
  2697. // we need to create a temporary and reconstruct it from the
  2698. // arguments.
  2699. Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));
  2700. LValue LV = MakeAddrLValue(Alloca, Ty);
  2701. ArgVals.push_back(ParamValue::forIndirect(Alloca));
  2702. auto FnArgIter = Fn->arg_begin() + FirstIRArg;
  2703. ExpandTypeFromArgs(Ty, LV, FnArgIter);
  2704. assert(FnArgIter == Fn->arg_begin() + FirstIRArg + NumIRArgs);
  2705. for (unsigned i = 0, e = NumIRArgs; i != e; ++i) {
  2706. auto AI = Fn->getArg(FirstIRArg + i);
  2707. AI->setName(Arg->getName() + "." + Twine(i));
  2708. }
  2709. break;
  2710. }
  2711. case ABIArgInfo::Ignore:
  2712. assert(NumIRArgs == 0);
  2713. // Initialize the local variable appropriately.
  2714. if (!hasScalarEvaluationKind(Ty)) {
  2715. ArgVals.push_back(ParamValue::forIndirect(CreateMemTemp(Ty)));
  2716. } else {
  2717. llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType()));
  2718. ArgVals.push_back(ParamValue::forDirect(U));
  2719. }
  2720. break;
  2721. }
  2722. }
  2723. if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
  2724. for (int I = Args.size() - 1; I >= 0; --I)
  2725. EmitParmDecl(*Args[I], ArgVals[I], I + 1);
  2726. } else {
  2727. for (unsigned I = 0, E = Args.size(); I != E; ++I)
  2728. EmitParmDecl(*Args[I], ArgVals[I], I + 1);
  2729. }
  2730. }
  2731. static void eraseUnusedBitCasts(llvm::Instruction *insn) {
  2732. while (insn->use_empty()) {
  2733. llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);
  2734. if (!bitcast) return;
  2735. // This is "safe" because we would have used a ConstantExpr otherwise.
  2736. insn = cast<llvm::Instruction>(bitcast->getOperand(0));
  2737. bitcast->eraseFromParent();
  2738. }
  2739. }
  2740. /// Try to emit a fused autorelease of a return result.
  2741. static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF,
  2742. llvm::Value *result) {
  2743. // We must be immediately followed the cast.
  2744. llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
  2745. if (BB->empty()) return nullptr;
  2746. if (&BB->back() != result) return nullptr;
  2747. llvm::Type *resultType = result->getType();
  2748. // result is in a BasicBlock and is therefore an Instruction.
  2749. llvm::Instruction *generator = cast<llvm::Instruction>(result);
  2750. SmallVector<llvm::Instruction *, 4> InstsToKill;
  2751. // Look for:
  2752. // %generator = bitcast %type1* %generator2 to %type2*
  2753. while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
  2754. // We would have emitted this as a constant if the operand weren't
  2755. // an Instruction.
  2756. generator = cast<llvm::Instruction>(bitcast->getOperand(0));
  2757. // Require the generator to be immediately followed by the cast.
  2758. if (generator->getNextNode() != bitcast)
  2759. return nullptr;
  2760. InstsToKill.push_back(bitcast);
  2761. }
  2762. // Look for:
  2763. // %generator = call i8* @objc_retain(i8* %originalResult)
  2764. // or
  2765. // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
  2766. llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
  2767. if (!call) return nullptr;
  2768. bool doRetainAutorelease;
  2769. if (call->getCalledOperand() == CGF.CGM.getObjCEntrypoints().objc_retain) {
  2770. doRetainAutorelease = true;
  2771. } else if (call->getCalledOperand() ==
  2772. CGF.CGM.getObjCEntrypoints().objc_retainAutoreleasedReturnValue) {
  2773. doRetainAutorelease = false;
  2774. // If we emitted an assembly marker for this call (and the
  2775. // ARCEntrypoints field should have been set if so), go looking
  2776. // for that call. If we can't find it, we can't do this
  2777. // optimization. But it should always be the immediately previous
  2778. // instruction, unless we needed bitcasts around the call.
  2779. if (CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker) {
  2780. llvm::Instruction *prev = call->getPrevNode();
  2781. assert(prev);
  2782. if (isa<llvm::BitCastInst>(prev)) {
  2783. prev = prev->getPrevNode();
  2784. assert(prev);
  2785. }
  2786. assert(isa<llvm::CallInst>(prev));
  2787. assert(cast<llvm::CallInst>(prev)->getCalledOperand() ==
  2788. CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker);
  2789. InstsToKill.push_back(prev);
  2790. }
  2791. } else {
  2792. return nullptr;
  2793. }
  2794. result = call->getArgOperand(0);
  2795. InstsToKill.push_back(call);
  2796. // Keep killing bitcasts, for sanity. Note that we no longer care
  2797. // about precise ordering as long as there's exactly one use.
  2798. while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
  2799. if (!bitcast->hasOneUse()) break;
  2800. InstsToKill.push_back(bitcast);
  2801. result = bitcast->getOperand(0);
  2802. }
  2803. // Delete all the unnecessary instructions, from latest to earliest.
  2804. for (auto *I : InstsToKill)
  2805. I->eraseFromParent();
  2806. // Do the fused retain/autorelease if we were asked to.
  2807. if (doRetainAutorelease)
  2808. result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
  2809. // Cast back to the result type.
  2810. return CGF.Builder.CreateBitCast(result, resultType);
  2811. }
  2812. /// If this is a +1 of the value of an immutable 'self', remove it.
  2813. static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF,
  2814. llvm::Value *result) {
  2815. // This is only applicable to a method with an immutable 'self'.
  2816. const ObjCMethodDecl *method =
  2817. dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl);
  2818. if (!method) return nullptr;
  2819. const VarDecl *self = method->getSelfDecl();
  2820. if (!self->getType().isConstQualified()) return nullptr;
  2821. // Look for a retain call.
  2822. llvm::CallInst *retainCall =
  2823. dyn_cast<llvm::CallInst>(result->stripPointerCasts());
  2824. if (!retainCall || retainCall->getCalledOperand() !=
  2825. CGF.CGM.getObjCEntrypoints().objc_retain)
  2826. return nullptr;
  2827. // Look for an ordinary load of 'self'.
  2828. llvm::Value *retainedValue = retainCall->getArgOperand(0);
  2829. llvm::LoadInst *load =
  2830. dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());
  2831. if (!load || load->isAtomic() || load->isVolatile() ||
  2832. load->getPointerOperand() != CGF.GetAddrOfLocalVar(self).getPointer())
  2833. return nullptr;
  2834. // Okay! Burn it all down. This relies for correctness on the
  2835. // assumption that the retain is emitted as part of the return and
  2836. // that thereafter everything is used "linearly".
  2837. llvm::Type *resultType = result->getType();
  2838. eraseUnusedBitCasts(cast<llvm::Instruction>(result));
  2839. assert(retainCall->use_empty());
  2840. retainCall->eraseFromParent();
  2841. eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue));
  2842. return CGF.Builder.CreateBitCast(load, resultType);
  2843. }
  2844. /// Emit an ARC autorelease of the result of a function.
  2845. ///
  2846. /// \return the value to actually return from the function
  2847. static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF,
  2848. llvm::Value *result) {
  2849. // If we're returning 'self', kill the initial retain. This is a
  2850. // heuristic attempt to "encourage correctness" in the really unfortunate
  2851. // case where we have a return of self during a dealloc and we desperately
  2852. // need to avoid the possible autorelease.
  2853. if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result))
  2854. return self;
  2855. // At -O0, try to emit a fused retain/autorelease.
  2856. if (CGF.shouldUseFusedARCCalls())
  2857. if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
  2858. return fused;
  2859. return CGF.EmitARCAutoreleaseReturnValue(result);
  2860. }
  2861. /// Heuristically search for a dominating store to the return-value slot.
  2862. static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) {
  2863. // Check if a User is a store which pointerOperand is the ReturnValue.
  2864. // We are looking for stores to the ReturnValue, not for stores of the
  2865. // ReturnValue to some other location.
  2866. auto GetStoreIfValid = [&CGF](llvm::User *U) -> llvm::StoreInst * {
  2867. auto *SI = dyn_cast<llvm::StoreInst>(U);
  2868. if (!SI || SI->getPointerOperand() != CGF.ReturnValue.getPointer() ||
  2869. SI->getValueOperand()->getType() != CGF.ReturnValue.getElementType())
  2870. return nullptr;
  2871. // These aren't actually possible for non-coerced returns, and we
  2872. // only care about non-coerced returns on this code path.
  2873. assert(!SI->isAtomic() && !SI->isVolatile());
  2874. return SI;
  2875. };
  2876. // If there are multiple uses of the return-value slot, just check
  2877. // for something immediately preceding the IP. Sometimes this can
  2878. // happen with how we generate implicit-returns; it can also happen
  2879. // with noreturn cleanups.
  2880. if (!CGF.ReturnValue.getPointer()->hasOneUse()) {
  2881. llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
  2882. if (IP->empty()) return nullptr;
  2883. // Look at directly preceding instruction, skipping bitcasts and lifetime
  2884. // markers.
  2885. for (llvm::Instruction &I : make_range(IP->rbegin(), IP->rend())) {
  2886. if (isa<llvm::BitCastInst>(&I))
  2887. continue;
  2888. if (auto *II = dyn_cast<llvm::IntrinsicInst>(&I))
  2889. if (II->getIntrinsicID() == llvm::Intrinsic::lifetime_end)
  2890. continue;
  2891. return GetStoreIfValid(&I);
  2892. }
  2893. return nullptr;
  2894. }
  2895. llvm::StoreInst *store =
  2896. GetStoreIfValid(CGF.ReturnValue.getPointer()->user_back());
  2897. if (!store) return nullptr;
  2898. // Now do a first-and-dirty dominance check: just walk up the
  2899. // single-predecessors chain from the current insertion point.
  2900. llvm::BasicBlock *StoreBB = store->getParent();
  2901. llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
  2902. while (IP != StoreBB) {
  2903. if (!(IP = IP->getSinglePredecessor()))
  2904. return nullptr;
  2905. }
  2906. // Okay, the store's basic block dominates the insertion point; we
  2907. // can do our thing.
  2908. return store;
  2909. }
  2910. // Helper functions for EmitCMSEClearRecord
  2911. // Set the bits corresponding to a field having width `BitWidth` and located at
  2912. // offset `BitOffset` (from the least significant bit) within a storage unit of
  2913. // `Bits.size()` bytes. Each element of `Bits` corresponds to one target byte.
  2914. // Use little-endian layout, i.e.`Bits[0]` is the LSB.
  2915. static void setBitRange(SmallVectorImpl<uint64_t> &Bits, int BitOffset,
  2916. int BitWidth, int CharWidth) {
  2917. assert(CharWidth <= 64);
  2918. assert(static_cast<unsigned>(BitWidth) <= Bits.size() * CharWidth);
  2919. int Pos = 0;
  2920. if (BitOffset >= CharWidth) {
  2921. Pos += BitOffset / CharWidth;
  2922. BitOffset = BitOffset % CharWidth;
  2923. }
  2924. const uint64_t Used = (uint64_t(1) << CharWidth) - 1;
  2925. if (BitOffset + BitWidth >= CharWidth) {
  2926. Bits[Pos++] |= (Used << BitOffset) & Used;
  2927. BitWidth -= CharWidth - BitOffset;
  2928. BitOffset = 0;
  2929. }
  2930. while (BitWidth >= CharWidth) {
  2931. Bits[Pos++] = Used;
  2932. BitWidth -= CharWidth;
  2933. }
  2934. if (BitWidth > 0)
  2935. Bits[Pos++] |= (Used >> (CharWidth - BitWidth)) << BitOffset;
  2936. }
  2937. // Set the bits corresponding to a field having width `BitWidth` and located at
  2938. // offset `BitOffset` (from the least significant bit) within a storage unit of
  2939. // `StorageSize` bytes, located at `StorageOffset` in `Bits`. Each element of
  2940. // `Bits` corresponds to one target byte. Use target endian layout.
  2941. static void setBitRange(SmallVectorImpl<uint64_t> &Bits, int StorageOffset,
  2942. int StorageSize, int BitOffset, int BitWidth,
  2943. int CharWidth, bool BigEndian) {
  2944. SmallVector<uint64_t, 8> TmpBits(StorageSize);
  2945. setBitRange(TmpBits, BitOffset, BitWidth, CharWidth);
  2946. if (BigEndian)
  2947. std::reverse(TmpBits.begin(), TmpBits.end());
  2948. for (uint64_t V : TmpBits)
  2949. Bits[StorageOffset++] |= V;
  2950. }
  2951. static void setUsedBits(CodeGenModule &, QualType, int,
  2952. SmallVectorImpl<uint64_t> &);
  2953. // Set the bits in `Bits`, which correspond to the value representations of
  2954. // the actual members of the record type `RTy`. Note that this function does
  2955. // not handle base classes, virtual tables, etc, since they cannot happen in
  2956. // CMSE function arguments or return. The bit mask corresponds to the target
  2957. // memory layout, i.e. it's endian dependent.
  2958. static void setUsedBits(CodeGenModule &CGM, const RecordType *RTy, int Offset,
  2959. SmallVectorImpl<uint64_t> &Bits) {
  2960. ASTContext &Context = CGM.getContext();
  2961. int CharWidth = Context.getCharWidth();
  2962. const RecordDecl *RD = RTy->getDecl()->getDefinition();
  2963. const ASTRecordLayout &ASTLayout = Context.getASTRecordLayout(RD);
  2964. const CGRecordLayout &Layout = CGM.getTypes().getCGRecordLayout(RD);
  2965. int Idx = 0;
  2966. for (auto I = RD->field_begin(), E = RD->field_end(); I != E; ++I, ++Idx) {
  2967. const FieldDecl *F = *I;
  2968. if (F->isUnnamedBitfield() || F->isZeroLengthBitField(Context) ||
  2969. F->getType()->isIncompleteArrayType())
  2970. continue;
  2971. if (F->isBitField()) {
  2972. const CGBitFieldInfo &BFI = Layout.getBitFieldInfo(F);
  2973. setBitRange(Bits, Offset + BFI.StorageOffset.getQuantity(),
  2974. BFI.StorageSize / CharWidth, BFI.Offset,
  2975. BFI.Size, CharWidth,
  2976. CGM.getDataLayout().isBigEndian());
  2977. continue;
  2978. }
  2979. setUsedBits(CGM, F->getType(),
  2980. Offset + ASTLayout.getFieldOffset(Idx) / CharWidth, Bits);
  2981. }
  2982. }
  2983. // Set the bits in `Bits`, which correspond to the value representations of
  2984. // the elements of an array type `ATy`.
  2985. static void setUsedBits(CodeGenModule &CGM, const ConstantArrayType *ATy,
  2986. int Offset, SmallVectorImpl<uint64_t> &Bits) {
  2987. const ASTContext &Context = CGM.getContext();
  2988. QualType ETy = Context.getBaseElementType(ATy);
  2989. int Size = Context.getTypeSizeInChars(ETy).getQuantity();
  2990. SmallVector<uint64_t, 4> TmpBits(Size);
  2991. setUsedBits(CGM, ETy, 0, TmpBits);
  2992. for (int I = 0, N = Context.getConstantArrayElementCount(ATy); I < N; ++I) {
  2993. auto Src = TmpBits.begin();
  2994. auto Dst = Bits.begin() + Offset + I * Size;
  2995. for (int J = 0; J < Size; ++J)
  2996. *Dst++ |= *Src++;
  2997. }
  2998. }
  2999. // Set the bits in `Bits`, which correspond to the value representations of
  3000. // the type `QTy`.
  3001. static void setUsedBits(CodeGenModule &CGM, QualType QTy, int Offset,
  3002. SmallVectorImpl<uint64_t> &Bits) {
  3003. if (const auto *RTy = QTy->getAs<RecordType>())
  3004. return setUsedBits(CGM, RTy, Offset, Bits);
  3005. ASTContext &Context = CGM.getContext();
  3006. if (const auto *ATy = Context.getAsConstantArrayType(QTy))
  3007. return setUsedBits(CGM, ATy, Offset, Bits);
  3008. int Size = Context.getTypeSizeInChars(QTy).getQuantity();
  3009. if (Size <= 0)
  3010. return;
  3011. std::fill_n(Bits.begin() + Offset, Size,
  3012. (uint64_t(1) << Context.getCharWidth()) - 1);
  3013. }
  3014. static uint64_t buildMultiCharMask(const SmallVectorImpl<uint64_t> &Bits,
  3015. int Pos, int Size, int CharWidth,
  3016. bool BigEndian) {
  3017. assert(Size > 0);
  3018. uint64_t Mask = 0;
  3019. if (BigEndian) {
  3020. for (auto P = Bits.begin() + Pos, E = Bits.begin() + Pos + Size; P != E;
  3021. ++P)
  3022. Mask = (Mask << CharWidth) | *P;
  3023. } else {
  3024. auto P = Bits.begin() + Pos + Size, End = Bits.begin() + Pos;
  3025. do
  3026. Mask = (Mask << CharWidth) | *--P;
  3027. while (P != End);
  3028. }
  3029. return Mask;
  3030. }
  3031. // Emit code to clear the bits in a record, which aren't a part of any user
  3032. // declared member, when the record is a function return.
  3033. llvm::Value *CodeGenFunction::EmitCMSEClearRecord(llvm::Value *Src,
  3034. llvm::IntegerType *ITy,
  3035. QualType QTy) {
  3036. assert(Src->getType() == ITy);
  3037. assert(ITy->getScalarSizeInBits() <= 64);
  3038. const llvm::DataLayout &DataLayout = CGM.getDataLayout();
  3039. int Size = DataLayout.getTypeStoreSize(ITy);
  3040. SmallVector<uint64_t, 4> Bits(Size);
  3041. setUsedBits(CGM, QTy->castAs<RecordType>(), 0, Bits);
  3042. int CharWidth = CGM.getContext().getCharWidth();
  3043. uint64_t Mask =
  3044. buildMultiCharMask(Bits, 0, Size, CharWidth, DataLayout.isBigEndian());
  3045. return Builder.CreateAnd(Src, Mask, "cmse.clear");
  3046. }
  3047. // Emit code to clear the bits in a record, which aren't a part of any user
  3048. // declared member, when the record is a function argument.
  3049. llvm::Value *CodeGenFunction::EmitCMSEClearRecord(llvm::Value *Src,
  3050. llvm::ArrayType *ATy,
  3051. QualType QTy) {
  3052. const llvm::DataLayout &DataLayout = CGM.getDataLayout();
  3053. int Size = DataLayout.getTypeStoreSize(ATy);
  3054. SmallVector<uint64_t, 16> Bits(Size);
  3055. setUsedBits(CGM, QTy->castAs<RecordType>(), 0, Bits);
  3056. // Clear each element of the LLVM array.
  3057. int CharWidth = CGM.getContext().getCharWidth();
  3058. int CharsPerElt =
  3059. ATy->getArrayElementType()->getScalarSizeInBits() / CharWidth;
  3060. int MaskIndex = 0;
  3061. llvm::Value *R = llvm::PoisonValue::get(ATy);
  3062. for (int I = 0, N = ATy->getArrayNumElements(); I != N; ++I) {
  3063. uint64_t Mask = buildMultiCharMask(Bits, MaskIndex, CharsPerElt, CharWidth,
  3064. DataLayout.isBigEndian());
  3065. MaskIndex += CharsPerElt;
  3066. llvm::Value *T0 = Builder.CreateExtractValue(Src, I);
  3067. llvm::Value *T1 = Builder.CreateAnd(T0, Mask, "cmse.clear");
  3068. R = Builder.CreateInsertValue(R, T1, I);
  3069. }
  3070. return R;
  3071. }
  3072. void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
  3073. bool EmitRetDbgLoc,
  3074. SourceLocation EndLoc) {
  3075. if (FI.isNoReturn()) {
  3076. // Noreturn functions don't return.
  3077. EmitUnreachable(EndLoc);
  3078. return;
  3079. }
  3080. if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) {
  3081. // Naked functions don't have epilogues.
  3082. Builder.CreateUnreachable();
  3083. return;
  3084. }
  3085. // Functions with no result always return void.
  3086. if (!ReturnValue.isValid()) {
  3087. Builder.CreateRetVoid();
  3088. return;
  3089. }
  3090. llvm::DebugLoc RetDbgLoc;
  3091. llvm::Value *RV = nullptr;
  3092. QualType RetTy = FI.getReturnType();
  3093. const ABIArgInfo &RetAI = FI.getReturnInfo();
  3094. switch (RetAI.getKind()) {
  3095. case ABIArgInfo::InAlloca:
  3096. // Aggregates get evaluated directly into the destination. Sometimes we
  3097. // need to return the sret value in a register, though.
  3098. assert(hasAggregateEvaluationKind(RetTy));
  3099. if (RetAI.getInAllocaSRet()) {
  3100. llvm::Function::arg_iterator EI = CurFn->arg_end();
  3101. --EI;
  3102. llvm::Value *ArgStruct = &*EI;
  3103. llvm::Value *SRet = Builder.CreateStructGEP(
  3104. FI.getArgStruct(), ArgStruct, RetAI.getInAllocaFieldIndex());
  3105. llvm::Type *Ty =
  3106. cast<llvm::GetElementPtrInst>(SRet)->getResultElementType();
  3107. RV = Builder.CreateAlignedLoad(Ty, SRet, getPointerAlign(), "sret");
  3108. }
  3109. break;
  3110. case ABIArgInfo::Indirect: {
  3111. auto AI = CurFn->arg_begin();
  3112. if (RetAI.isSRetAfterThis())
  3113. ++AI;
  3114. switch (getEvaluationKind(RetTy)) {
  3115. case TEK_Complex: {
  3116. ComplexPairTy RT =
  3117. EmitLoadOfComplex(MakeAddrLValue(ReturnValue, RetTy), EndLoc);
  3118. EmitStoreOfComplex(RT, MakeNaturalAlignAddrLValue(&*AI, RetTy),
  3119. /*isInit*/ true);
  3120. break;
  3121. }
  3122. case TEK_Aggregate:
  3123. // Do nothing; aggregates get evaluated directly into the destination.
  3124. break;
  3125. case TEK_Scalar: {
  3126. LValueBaseInfo BaseInfo;
  3127. TBAAAccessInfo TBAAInfo;
  3128. CharUnits Alignment =
  3129. CGM.getNaturalTypeAlignment(RetTy, &BaseInfo, &TBAAInfo);
  3130. Address ArgAddr(&*AI, ConvertType(RetTy), Alignment);
  3131. LValue ArgVal =
  3132. LValue::MakeAddr(ArgAddr, RetTy, getContext(), BaseInfo, TBAAInfo);
  3133. EmitStoreOfScalar(
  3134. Builder.CreateLoad(ReturnValue), ArgVal, /*isInit*/ true);
  3135. break;
  3136. }
  3137. }
  3138. break;
  3139. }
  3140. case ABIArgInfo::Extend:
  3141. case ABIArgInfo::Direct:
  3142. if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
  3143. RetAI.getDirectOffset() == 0) {
  3144. // The internal return value temp always will have pointer-to-return-type
  3145. // type, just do a load.
  3146. // If there is a dominating store to ReturnValue, we can elide
  3147. // the load, zap the store, and usually zap the alloca.
  3148. if (llvm::StoreInst *SI =
  3149. findDominatingStoreToReturnValue(*this)) {
  3150. // Reuse the debug location from the store unless there is
  3151. // cleanup code to be emitted between the store and return
  3152. // instruction.
  3153. if (EmitRetDbgLoc && !AutoreleaseResult)
  3154. RetDbgLoc = SI->getDebugLoc();
  3155. // Get the stored value and nuke the now-dead store.
  3156. RV = SI->getValueOperand();
  3157. SI->eraseFromParent();
  3158. // Otherwise, we have to do a simple load.
  3159. } else {
  3160. RV = Builder.CreateLoad(ReturnValue);
  3161. }
  3162. } else {
  3163. // If the value is offset in memory, apply the offset now.
  3164. Address V = emitAddressAtOffset(*this, ReturnValue, RetAI);
  3165. RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
  3166. }
  3167. // In ARC, end functions that return a retainable type with a call
  3168. // to objc_autoreleaseReturnValue.
  3169. if (AutoreleaseResult) {
  3170. #ifndef NDEBUG
  3171. // Type::isObjCRetainabletype has to be called on a QualType that hasn't
  3172. // been stripped of the typedefs, so we cannot use RetTy here. Get the
  3173. // original return type of FunctionDecl, CurCodeDecl, and BlockDecl from
  3174. // CurCodeDecl or BlockInfo.
  3175. QualType RT;
  3176. if (auto *FD = dyn_cast<FunctionDecl>(CurCodeDecl))
  3177. RT = FD->getReturnType();
  3178. else if (auto *MD = dyn_cast<ObjCMethodDecl>(CurCodeDecl))
  3179. RT = MD->getReturnType();
  3180. else if (isa<BlockDecl>(CurCodeDecl))
  3181. RT = BlockInfo->BlockExpression->getFunctionType()->getReturnType();
  3182. else
  3183. llvm_unreachable("Unexpected function/method type");
  3184. assert(getLangOpts().ObjCAutoRefCount &&
  3185. !FI.isReturnsRetained() &&
  3186. RT->isObjCRetainableType());
  3187. #endif
  3188. RV = emitAutoreleaseOfResult(*this, RV);
  3189. }
  3190. break;
  3191. case ABIArgInfo::Ignore:
  3192. break;
  3193. case ABIArgInfo::CoerceAndExpand: {
  3194. auto coercionType = RetAI.getCoerceAndExpandType();
  3195. // Load all of the coerced elements out into results.
  3196. llvm::SmallVector<llvm::Value*, 4> results;
  3197. Address addr = Builder.CreateElementBitCast(ReturnValue, coercionType);
  3198. for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
  3199. auto coercedEltType = coercionType->getElementType(i);
  3200. if (ABIArgInfo::isPaddingForCoerceAndExpand(coercedEltType))
  3201. continue;
  3202. auto eltAddr = Builder.CreateStructGEP(addr, i);
  3203. auto elt = Builder.CreateLoad(eltAddr);
  3204. results.push_back(elt);
  3205. }
  3206. // If we have one result, it's the single direct result type.
  3207. if (results.size() == 1) {
  3208. RV = results[0];
  3209. // Otherwise, we need to make a first-class aggregate.
  3210. } else {
  3211. // Construct a return type that lacks padding elements.
  3212. llvm::Type *returnType = RetAI.getUnpaddedCoerceAndExpandType();
  3213. RV = llvm::PoisonValue::get(returnType);
  3214. for (unsigned i = 0, e = results.size(); i != e; ++i) {
  3215. RV = Builder.CreateInsertValue(RV, results[i], i);
  3216. }
  3217. }
  3218. break;
  3219. }
  3220. case ABIArgInfo::Expand:
  3221. case ABIArgInfo::IndirectAliased:
  3222. llvm_unreachable("Invalid ABI kind for return argument");
  3223. }
  3224. llvm::Instruction *Ret;
  3225. if (RV) {
  3226. if (CurFuncDecl && CurFuncDecl->hasAttr<CmseNSEntryAttr>()) {
  3227. // For certain return types, clear padding bits, as they may reveal
  3228. // sensitive information.
  3229. // Small struct/union types are passed as integers.
  3230. auto *ITy = dyn_cast<llvm::IntegerType>(RV->getType());
  3231. if (ITy != nullptr && isa<RecordType>(RetTy.getCanonicalType()))
  3232. RV = EmitCMSEClearRecord(RV, ITy, RetTy);
  3233. }
  3234. EmitReturnValueCheck(RV);
  3235. Ret = Builder.CreateRet(RV);
  3236. } else {
  3237. Ret = Builder.CreateRetVoid();
  3238. }
  3239. if (RetDbgLoc)
  3240. Ret->setDebugLoc(std::move(RetDbgLoc));
  3241. }
  3242. void CodeGenFunction::EmitReturnValueCheck(llvm::Value *RV) {
  3243. // A current decl may not be available when emitting vtable thunks.
  3244. if (!CurCodeDecl)
  3245. return;
  3246. // If the return block isn't reachable, neither is this check, so don't emit
  3247. // it.
  3248. if (ReturnBlock.isValid() && ReturnBlock.getBlock()->use_empty())
  3249. return;
  3250. ReturnsNonNullAttr *RetNNAttr = nullptr;
  3251. if (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute))
  3252. RetNNAttr = CurCodeDecl->getAttr<ReturnsNonNullAttr>();
  3253. if (!RetNNAttr && !requiresReturnValueNullabilityCheck())
  3254. return;
  3255. // Prefer the returns_nonnull attribute if it's present.
  3256. SourceLocation AttrLoc;
  3257. SanitizerMask CheckKind;
  3258. SanitizerHandler Handler;
  3259. if (RetNNAttr) {
  3260. assert(!requiresReturnValueNullabilityCheck() &&
  3261. "Cannot check nullability and the nonnull attribute");
  3262. AttrLoc = RetNNAttr->getLocation();
  3263. CheckKind = SanitizerKind::ReturnsNonnullAttribute;
  3264. Handler = SanitizerHandler::NonnullReturn;
  3265. } else {
  3266. if (auto *DD = dyn_cast<DeclaratorDecl>(CurCodeDecl))
  3267. if (auto *TSI = DD->getTypeSourceInfo())
  3268. if (auto FTL = TSI->getTypeLoc().getAsAdjusted<FunctionTypeLoc>())
  3269. AttrLoc = FTL.getReturnLoc().findNullabilityLoc();
  3270. CheckKind = SanitizerKind::NullabilityReturn;
  3271. Handler = SanitizerHandler::NullabilityReturn;
  3272. }
  3273. SanitizerScope SanScope(this);
  3274. // Make sure the "return" source location is valid. If we're checking a
  3275. // nullability annotation, make sure the preconditions for the check are met.
  3276. llvm::BasicBlock *Check = createBasicBlock("nullcheck");
  3277. llvm::BasicBlock *NoCheck = createBasicBlock("no.nullcheck");
  3278. llvm::Value *SLocPtr = Builder.CreateLoad(ReturnLocation, "return.sloc.load");
  3279. llvm::Value *CanNullCheck = Builder.CreateIsNotNull(SLocPtr);
  3280. if (requiresReturnValueNullabilityCheck())
  3281. CanNullCheck =
  3282. Builder.CreateAnd(CanNullCheck, RetValNullabilityPrecondition);
  3283. Builder.CreateCondBr(CanNullCheck, Check, NoCheck);
  3284. EmitBlock(Check);
  3285. // Now do the null check.
  3286. llvm::Value *Cond = Builder.CreateIsNotNull(RV);
  3287. llvm::Constant *StaticData[] = {EmitCheckSourceLocation(AttrLoc)};
  3288. llvm::Value *DynamicData[] = {SLocPtr};
  3289. EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, DynamicData);
  3290. EmitBlock(NoCheck);
  3291. #ifndef NDEBUG
  3292. // The return location should not be used after the check has been emitted.
  3293. ReturnLocation = Address::invalid();
  3294. #endif
  3295. }
  3296. static bool isInAllocaArgument(CGCXXABI &ABI, QualType type) {
  3297. const CXXRecordDecl *RD = type->getAsCXXRecordDecl();
  3298. return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory;
  3299. }
  3300. static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF,
  3301. QualType Ty) {
  3302. // FIXME: Generate IR in one pass, rather than going back and fixing up these
  3303. // placeholders.
  3304. llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty);
  3305. llvm::Type *IRPtrTy = IRTy->getPointerTo();
  3306. llvm::Value *Placeholder = llvm::PoisonValue::get(IRPtrTy->getPointerTo());
  3307. // FIXME: When we generate this IR in one pass, we shouldn't need
  3308. // this win32-specific alignment hack.
  3309. CharUnits Align = CharUnits::fromQuantity(4);
  3310. Placeholder = CGF.Builder.CreateAlignedLoad(IRPtrTy, Placeholder, Align);
  3311. return AggValueSlot::forAddr(Address(Placeholder, IRTy, Align),
  3312. Ty.getQualifiers(),
  3313. AggValueSlot::IsNotDestructed,
  3314. AggValueSlot::DoesNotNeedGCBarriers,
  3315. AggValueSlot::IsNotAliased,
  3316. AggValueSlot::DoesNotOverlap);
  3317. }
  3318. void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
  3319. const VarDecl *param,
  3320. SourceLocation loc) {
  3321. // StartFunction converted the ABI-lowered parameter(s) into a
  3322. // local alloca. We need to turn that into an r-value suitable
  3323. // for EmitCall.
  3324. Address local = GetAddrOfLocalVar(param);
  3325. QualType type = param->getType();
  3326. if (isInAllocaArgument(CGM.getCXXABI(), type)) {
  3327. CGM.ErrorUnsupported(param, "forwarded non-trivially copyable parameter");
  3328. }
  3329. // GetAddrOfLocalVar returns a pointer-to-pointer for references,
  3330. // but the argument needs to be the original pointer.
  3331. if (type->isReferenceType()) {
  3332. args.add(RValue::get(Builder.CreateLoad(local)), type);
  3333. // In ARC, move out of consumed arguments so that the release cleanup
  3334. // entered by StartFunction doesn't cause an over-release. This isn't
  3335. // optimal -O0 code generation, but it should get cleaned up when
  3336. // optimization is enabled. This also assumes that delegate calls are
  3337. // performed exactly once for a set of arguments, but that should be safe.
  3338. } else if (getLangOpts().ObjCAutoRefCount &&
  3339. param->hasAttr<NSConsumedAttr>() &&
  3340. type->isObjCRetainableType()) {
  3341. llvm::Value *ptr = Builder.CreateLoad(local);
  3342. auto null =
  3343. llvm::ConstantPointerNull::get(cast<llvm::PointerType>(ptr->getType()));
  3344. Builder.CreateStore(null, local);
  3345. args.add(RValue::get(ptr), type);
  3346. // For the most part, we just need to load the alloca, except that
  3347. // aggregate r-values are actually pointers to temporaries.
  3348. } else {
  3349. args.add(convertTempToRValue(local, type, loc), type);
  3350. }
  3351. // Deactivate the cleanup for the callee-destructed param that was pushed.
  3352. if (type->isRecordType() && !CurFuncIsThunk &&
  3353. type->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee() &&
  3354. param->needsDestruction(getContext())) {
  3355. EHScopeStack::stable_iterator cleanup =
  3356. CalleeDestructedParamCleanups.lookup(cast<ParmVarDecl>(param));
  3357. assert(cleanup.isValid() &&
  3358. "cleanup for callee-destructed param not recorded");
  3359. // This unreachable is a temporary marker which will be removed later.
  3360. llvm::Instruction *isActive = Builder.CreateUnreachable();
  3361. args.addArgCleanupDeactivation(cleanup, isActive);
  3362. }
  3363. }
  3364. static bool isProvablyNull(llvm::Value *addr) {
  3365. return isa<llvm::ConstantPointerNull>(addr);
  3366. }
  3367. /// Emit the actual writing-back of a writeback.
  3368. static void emitWriteback(CodeGenFunction &CGF,
  3369. const CallArgList::Writeback &writeback) {
  3370. const LValue &srcLV = writeback.Source;
  3371. Address srcAddr = srcLV.getAddress(CGF);
  3372. assert(!isProvablyNull(srcAddr.getPointer()) &&
  3373. "shouldn't have writeback for provably null argument");
  3374. llvm::BasicBlock *contBB = nullptr;
  3375. // If the argument wasn't provably non-null, we need to null check
  3376. // before doing the store.
  3377. bool provablyNonNull = llvm::isKnownNonZero(srcAddr.getPointer(),
  3378. CGF.CGM.getDataLayout());
  3379. if (!provablyNonNull) {
  3380. llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
  3381. contBB = CGF.createBasicBlock("icr.done");
  3382. llvm::Value *isNull =
  3383. CGF.Builder.CreateIsNull(srcAddr.getPointer(), "icr.isnull");
  3384. CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
  3385. CGF.EmitBlock(writebackBB);
  3386. }
  3387. // Load the value to writeback.
  3388. llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
  3389. // Cast it back, in case we're writing an id to a Foo* or something.
  3390. value = CGF.Builder.CreateBitCast(value, srcAddr.getElementType(),
  3391. "icr.writeback-cast");
  3392. // Perform the writeback.
  3393. // If we have a "to use" value, it's something we need to emit a use
  3394. // of. This has to be carefully threaded in: if it's done after the
  3395. // release it's potentially undefined behavior (and the optimizer
  3396. // will ignore it), and if it happens before the retain then the
  3397. // optimizer could move the release there.
  3398. if (writeback.ToUse) {
  3399. assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong);
  3400. // Retain the new value. No need to block-copy here: the block's
  3401. // being passed up the stack.
  3402. value = CGF.EmitARCRetainNonBlock(value);
  3403. // Emit the intrinsic use here.
  3404. CGF.EmitARCIntrinsicUse(writeback.ToUse);
  3405. // Load the old value (primitively).
  3406. llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation());
  3407. // Put the new value in place (primitively).
  3408. CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false);
  3409. // Release the old value.
  3410. CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime());
  3411. // Otherwise, we can just do a normal lvalue store.
  3412. } else {
  3413. CGF.EmitStoreThroughLValue(RValue::get(value), srcLV);
  3414. }
  3415. // Jump to the continuation block.
  3416. if (!provablyNonNull)
  3417. CGF.EmitBlock(contBB);
  3418. }
  3419. static void emitWritebacks(CodeGenFunction &CGF,
  3420. const CallArgList &args) {
  3421. for (const auto &I : args.writebacks())
  3422. emitWriteback(CGF, I);
  3423. }
  3424. static void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF,
  3425. const CallArgList &CallArgs) {
  3426. ArrayRef<CallArgList::CallArgCleanup> Cleanups =
  3427. CallArgs.getCleanupsToDeactivate();
  3428. // Iterate in reverse to increase the likelihood of popping the cleanup.
  3429. for (const auto &I : llvm::reverse(Cleanups)) {
  3430. CGF.DeactivateCleanupBlock(I.Cleanup, I.IsActiveIP);
  3431. I.IsActiveIP->eraseFromParent();
  3432. }
  3433. }
  3434. static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) {
  3435. if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens()))
  3436. if (uop->getOpcode() == UO_AddrOf)
  3437. return uop->getSubExpr();
  3438. return nullptr;
  3439. }
  3440. /// Emit an argument that's being passed call-by-writeback. That is,
  3441. /// we are passing the address of an __autoreleased temporary; it
  3442. /// might be copy-initialized with the current value of the given
  3443. /// address, but it will definitely be copied out of after the call.
  3444. static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args,
  3445. const ObjCIndirectCopyRestoreExpr *CRE) {
  3446. LValue srcLV;
  3447. // Make an optimistic effort to emit the address as an l-value.
  3448. // This can fail if the argument expression is more complicated.
  3449. if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) {
  3450. srcLV = CGF.EmitLValue(lvExpr);
  3451. // Otherwise, just emit it as a scalar.
  3452. } else {
  3453. Address srcAddr = CGF.EmitPointerWithAlignment(CRE->getSubExpr());
  3454. QualType srcAddrType =
  3455. CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
  3456. srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType);
  3457. }
  3458. Address srcAddr = srcLV.getAddress(CGF);
  3459. // The dest and src types don't necessarily match in LLVM terms
  3460. // because of the crazy ObjC compatibility rules.
  3461. llvm::PointerType *destType =
  3462. cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
  3463. llvm::Type *destElemType =
  3464. CGF.ConvertTypeForMem(CRE->getType()->getPointeeType());
  3465. // If the address is a constant null, just pass the appropriate null.
  3466. if (isProvablyNull(srcAddr.getPointer())) {
  3467. args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
  3468. CRE->getType());
  3469. return;
  3470. }
  3471. // Create the temporary.
  3472. Address temp =
  3473. CGF.CreateTempAlloca(destElemType, CGF.getPointerAlign(), "icr.temp");
  3474. // Loading an l-value can introduce a cleanup if the l-value is __weak,
  3475. // and that cleanup will be conditional if we can't prove that the l-value
  3476. // isn't null, so we need to register a dominating point so that the cleanups
  3477. // system will make valid IR.
  3478. CodeGenFunction::ConditionalEvaluation condEval(CGF);
  3479. // Zero-initialize it if we're not doing a copy-initialization.
  3480. bool shouldCopy = CRE->shouldCopy();
  3481. if (!shouldCopy) {
  3482. llvm::Value *null =
  3483. llvm::ConstantPointerNull::get(cast<llvm::PointerType>(destElemType));
  3484. CGF.Builder.CreateStore(null, temp);
  3485. }
  3486. llvm::BasicBlock *contBB = nullptr;
  3487. llvm::BasicBlock *originBB = nullptr;
  3488. // If the address is *not* known to be non-null, we need to switch.
  3489. llvm::Value *finalArgument;
  3490. bool provablyNonNull = llvm::isKnownNonZero(srcAddr.getPointer(),
  3491. CGF.CGM.getDataLayout());
  3492. if (provablyNonNull) {
  3493. finalArgument = temp.getPointer();
  3494. } else {
  3495. llvm::Value *isNull =
  3496. CGF.Builder.CreateIsNull(srcAddr.getPointer(), "icr.isnull");
  3497. finalArgument = CGF.Builder.CreateSelect(isNull,
  3498. llvm::ConstantPointerNull::get(destType),
  3499. temp.getPointer(), "icr.argument");
  3500. // If we need to copy, then the load has to be conditional, which
  3501. // means we need control flow.
  3502. if (shouldCopy) {
  3503. originBB = CGF.Builder.GetInsertBlock();
  3504. contBB = CGF.createBasicBlock("icr.cont");
  3505. llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
  3506. CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
  3507. CGF.EmitBlock(copyBB);
  3508. condEval.begin(CGF);
  3509. }
  3510. }
  3511. llvm::Value *valueToUse = nullptr;
  3512. // Perform a copy if necessary.
  3513. if (shouldCopy) {
  3514. RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation());
  3515. assert(srcRV.isScalar());
  3516. llvm::Value *src = srcRV.getScalarVal();
  3517. src = CGF.Builder.CreateBitCast(src, destElemType, "icr.cast");
  3518. // Use an ordinary store, not a store-to-lvalue.
  3519. CGF.Builder.CreateStore(src, temp);
  3520. // If optimization is enabled, and the value was held in a
  3521. // __strong variable, we need to tell the optimizer that this
  3522. // value has to stay alive until we're doing the store back.
  3523. // This is because the temporary is effectively unretained,
  3524. // and so otherwise we can violate the high-level semantics.
  3525. if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 &&
  3526. srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) {
  3527. valueToUse = src;
  3528. }
  3529. }
  3530. // Finish the control flow if we needed it.
  3531. if (shouldCopy && !provablyNonNull) {
  3532. llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock();
  3533. CGF.EmitBlock(contBB);
  3534. // Make a phi for the value to intrinsically use.
  3535. if (valueToUse) {
  3536. llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2,
  3537. "icr.to-use");
  3538. phiToUse->addIncoming(valueToUse, copyBB);
  3539. phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()),
  3540. originBB);
  3541. valueToUse = phiToUse;
  3542. }
  3543. condEval.end(CGF);
  3544. }
  3545. args.addWriteback(srcLV, temp, valueToUse);
  3546. args.add(RValue::get(finalArgument), CRE->getType());
  3547. }
  3548. void CallArgList::allocateArgumentMemory(CodeGenFunction &CGF) {
  3549. assert(!StackBase);
  3550. // Save the stack.
  3551. llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stacksave);
  3552. StackBase = CGF.Builder.CreateCall(F, {}, "inalloca.save");
  3553. }
  3554. void CallArgList::freeArgumentMemory(CodeGenFunction &CGF) const {
  3555. if (StackBase) {
  3556. // Restore the stack after the call.
  3557. llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
  3558. CGF.Builder.CreateCall(F, StackBase);
  3559. }
  3560. }
  3561. void CodeGenFunction::EmitNonNullArgCheck(RValue RV, QualType ArgType,
  3562. SourceLocation ArgLoc,
  3563. AbstractCallee AC,
  3564. unsigned ParmNum) {
  3565. if (!AC.getDecl() || !(SanOpts.has(SanitizerKind::NonnullAttribute) ||
  3566. SanOpts.has(SanitizerKind::NullabilityArg)))
  3567. return;
  3568. // The param decl may be missing in a variadic function.
  3569. auto PVD = ParmNum < AC.getNumParams() ? AC.getParamDecl(ParmNum) : nullptr;
  3570. unsigned ArgNo = PVD ? PVD->getFunctionScopeIndex() : ParmNum;
  3571. // Prefer the nonnull attribute if it's present.
  3572. const NonNullAttr *NNAttr = nullptr;
  3573. if (SanOpts.has(SanitizerKind::NonnullAttribute))
  3574. NNAttr = getNonNullAttr(AC.getDecl(), PVD, ArgType, ArgNo);
  3575. bool CanCheckNullability = false;
  3576. if (SanOpts.has(SanitizerKind::NullabilityArg) && !NNAttr && PVD) {
  3577. auto Nullability = PVD->getType()->getNullability();
  3578. CanCheckNullability = Nullability &&
  3579. *Nullability == NullabilityKind::NonNull &&
  3580. PVD->getTypeSourceInfo();
  3581. }
  3582. if (!NNAttr && !CanCheckNullability)
  3583. return;
  3584. SourceLocation AttrLoc;
  3585. SanitizerMask CheckKind;
  3586. SanitizerHandler Handler;
  3587. if (NNAttr) {
  3588. AttrLoc = NNAttr->getLocation();
  3589. CheckKind = SanitizerKind::NonnullAttribute;
  3590. Handler = SanitizerHandler::NonnullArg;
  3591. } else {
  3592. AttrLoc = PVD->getTypeSourceInfo()->getTypeLoc().findNullabilityLoc();
  3593. CheckKind = SanitizerKind::NullabilityArg;
  3594. Handler = SanitizerHandler::NullabilityArg;
  3595. }
  3596. SanitizerScope SanScope(this);
  3597. llvm::Value *Cond = EmitNonNullRValueCheck(RV, ArgType);
  3598. llvm::Constant *StaticData[] = {
  3599. EmitCheckSourceLocation(ArgLoc), EmitCheckSourceLocation(AttrLoc),
  3600. llvm::ConstantInt::get(Int32Ty, ArgNo + 1),
  3601. };
  3602. EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, std::nullopt);
  3603. }
  3604. // Check if the call is going to use the inalloca convention. This needs to
  3605. // agree with CGFunctionInfo::usesInAlloca. The CGFunctionInfo is arranged
  3606. // later, so we can't check it directly.
  3607. static bool hasInAllocaArgs(CodeGenModule &CGM, CallingConv ExplicitCC,
  3608. ArrayRef<QualType> ArgTypes) {
  3609. // The Swift calling conventions don't go through the target-specific
  3610. // argument classification, they never use inalloca.
  3611. // TODO: Consider limiting inalloca use to only calling conventions supported
  3612. // by MSVC.
  3613. if (ExplicitCC == CC_Swift || ExplicitCC == CC_SwiftAsync)
  3614. return false;
  3615. if (!CGM.getTarget().getCXXABI().isMicrosoft())
  3616. return false;
  3617. return llvm::any_of(ArgTypes, [&](QualType Ty) {
  3618. return isInAllocaArgument(CGM.getCXXABI(), Ty);
  3619. });
  3620. }
  3621. #ifndef NDEBUG
  3622. // Determine whether the given argument is an Objective-C method
  3623. // that may have type parameters in its signature.
  3624. static bool isObjCMethodWithTypeParams(const ObjCMethodDecl *method) {
  3625. const DeclContext *dc = method->getDeclContext();
  3626. if (const ObjCInterfaceDecl *classDecl = dyn_cast<ObjCInterfaceDecl>(dc)) {
  3627. return classDecl->getTypeParamListAsWritten();
  3628. }
  3629. if (const ObjCCategoryDecl *catDecl = dyn_cast<ObjCCategoryDecl>(dc)) {
  3630. return catDecl->getTypeParamList();
  3631. }
  3632. return false;
  3633. }
  3634. #endif
  3635. /// EmitCallArgs - Emit call arguments for a function.
  3636. void CodeGenFunction::EmitCallArgs(
  3637. CallArgList &Args, PrototypeWrapper Prototype,
  3638. llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
  3639. AbstractCallee AC, unsigned ParamsToSkip, EvaluationOrder Order) {
  3640. SmallVector<QualType, 16> ArgTypes;
  3641. assert((ParamsToSkip == 0 || Prototype.P) &&
  3642. "Can't skip parameters if type info is not provided");
  3643. // This variable only captures *explicitly* written conventions, not those
  3644. // applied by default via command line flags or target defaults, such as
  3645. // thiscall, aapcs, stdcall via -mrtd, etc. Computing that correctly would
  3646. // require knowing if this is a C++ instance method or being able to see
  3647. // unprototyped FunctionTypes.
  3648. CallingConv ExplicitCC = CC_C;
  3649. // First, if a prototype was provided, use those argument types.
  3650. bool IsVariadic = false;
  3651. if (Prototype.P) {
  3652. const auto *MD = Prototype.P.dyn_cast<const ObjCMethodDecl *>();
  3653. if (MD) {
  3654. IsVariadic = MD->isVariadic();
  3655. ExplicitCC = getCallingConventionForDecl(
  3656. MD, CGM.getTarget().getTriple().isOSWindows());
  3657. ArgTypes.assign(MD->param_type_begin() + ParamsToSkip,
  3658. MD->param_type_end());
  3659. } else {
  3660. const auto *FPT = Prototype.P.get<const FunctionProtoType *>();
  3661. IsVariadic = FPT->isVariadic();
  3662. ExplicitCC = FPT->getExtInfo().getCC();
  3663. ArgTypes.assign(FPT->param_type_begin() + ParamsToSkip,
  3664. FPT->param_type_end());
  3665. }
  3666. #ifndef NDEBUG
  3667. // Check that the prototyped types match the argument expression types.
  3668. bool isGenericMethod = MD && isObjCMethodWithTypeParams(MD);
  3669. CallExpr::const_arg_iterator Arg = ArgRange.begin();
  3670. for (QualType Ty : ArgTypes) {
  3671. assert(Arg != ArgRange.end() && "Running over edge of argument list!");
  3672. assert(
  3673. (isGenericMethod || Ty->isVariablyModifiedType() ||
  3674. Ty.getNonReferenceType()->isObjCRetainableType() ||
  3675. getContext()
  3676. .getCanonicalType(Ty.getNonReferenceType())
  3677. .getTypePtr() ==
  3678. getContext().getCanonicalType((*Arg)->getType()).getTypePtr()) &&
  3679. "type mismatch in call argument!");
  3680. ++Arg;
  3681. }
  3682. // Either we've emitted all the call args, or we have a call to variadic
  3683. // function.
  3684. assert((Arg == ArgRange.end() || IsVariadic) &&
  3685. "Extra arguments in non-variadic function!");
  3686. #endif
  3687. }
  3688. // If we still have any arguments, emit them using the type of the argument.
  3689. for (auto *A : llvm::drop_begin(ArgRange, ArgTypes.size()))
  3690. ArgTypes.push_back(IsVariadic ? getVarArgType(A) : A->getType());
  3691. assert((int)ArgTypes.size() == (ArgRange.end() - ArgRange.begin()));
  3692. // We must evaluate arguments from right to left in the MS C++ ABI,
  3693. // because arguments are destroyed left to right in the callee. As a special
  3694. // case, there are certain language constructs that require left-to-right
  3695. // evaluation, and in those cases we consider the evaluation order requirement
  3696. // to trump the "destruction order is reverse construction order" guarantee.
  3697. bool LeftToRight =
  3698. CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()
  3699. ? Order == EvaluationOrder::ForceLeftToRight
  3700. : Order != EvaluationOrder::ForceRightToLeft;
  3701. auto MaybeEmitImplicitObjectSize = [&](unsigned I, const Expr *Arg,
  3702. RValue EmittedArg) {
  3703. if (!AC.hasFunctionDecl() || I >= AC.getNumParams())
  3704. return;
  3705. auto *PS = AC.getParamDecl(I)->getAttr<PassObjectSizeAttr>();
  3706. if (PS == nullptr)
  3707. return;
  3708. const auto &Context = getContext();
  3709. auto SizeTy = Context.getSizeType();
  3710. auto T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
  3711. assert(EmittedArg.getScalarVal() && "We emitted nothing for the arg?");
  3712. llvm::Value *V = evaluateOrEmitBuiltinObjectSize(Arg, PS->getType(), T,
  3713. EmittedArg.getScalarVal(),
  3714. PS->isDynamic());
  3715. Args.add(RValue::get(V), SizeTy);
  3716. // If we're emitting args in reverse, be sure to do so with
  3717. // pass_object_size, as well.
  3718. if (!LeftToRight)
  3719. std::swap(Args.back(), *(&Args.back() - 1));
  3720. };
  3721. // Insert a stack save if we're going to need any inalloca args.
  3722. if (hasInAllocaArgs(CGM, ExplicitCC, ArgTypes)) {
  3723. assert(getTarget().getTriple().getArch() == llvm::Triple::x86 &&
  3724. "inalloca only supported on x86");
  3725. Args.allocateArgumentMemory(*this);
  3726. }
  3727. // Evaluate each argument in the appropriate order.
  3728. size_t CallArgsStart = Args.size();
  3729. for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) {
  3730. unsigned Idx = LeftToRight ? I : E - I - 1;
  3731. CallExpr::const_arg_iterator Arg = ArgRange.begin() + Idx;
  3732. unsigned InitialArgSize = Args.size();
  3733. // If *Arg is an ObjCIndirectCopyRestoreExpr, check that either the types of
  3734. // the argument and parameter match or the objc method is parameterized.
  3735. assert((!isa<ObjCIndirectCopyRestoreExpr>(*Arg) ||
  3736. getContext().hasSameUnqualifiedType((*Arg)->getType(),
  3737. ArgTypes[Idx]) ||
  3738. (isa<ObjCMethodDecl>(AC.getDecl()) &&
  3739. isObjCMethodWithTypeParams(cast<ObjCMethodDecl>(AC.getDecl())))) &&
  3740. "Argument and parameter types don't match");
  3741. EmitCallArg(Args, *Arg, ArgTypes[Idx]);
  3742. // In particular, we depend on it being the last arg in Args, and the
  3743. // objectsize bits depend on there only being one arg if !LeftToRight.
  3744. assert(InitialArgSize + 1 == Args.size() &&
  3745. "The code below depends on only adding one arg per EmitCallArg");
  3746. (void)InitialArgSize;
  3747. // Since pointer argument are never emitted as LValue, it is safe to emit
  3748. // non-null argument check for r-value only.
  3749. if (!Args.back().hasLValue()) {
  3750. RValue RVArg = Args.back().getKnownRValue();
  3751. EmitNonNullArgCheck(RVArg, ArgTypes[Idx], (*Arg)->getExprLoc(), AC,
  3752. ParamsToSkip + Idx);
  3753. // @llvm.objectsize should never have side-effects and shouldn't need
  3754. // destruction/cleanups, so we can safely "emit" it after its arg,
  3755. // regardless of right-to-leftness
  3756. MaybeEmitImplicitObjectSize(Idx, *Arg, RVArg);
  3757. }
  3758. }
  3759. if (!LeftToRight) {
  3760. // Un-reverse the arguments we just evaluated so they match up with the LLVM
  3761. // IR function.
  3762. std::reverse(Args.begin() + CallArgsStart, Args.end());
  3763. }
  3764. }
  3765. namespace {
  3766. struct DestroyUnpassedArg final : EHScopeStack::Cleanup {
  3767. DestroyUnpassedArg(Address Addr, QualType Ty)
  3768. : Addr(Addr), Ty(Ty) {}
  3769. Address Addr;
  3770. QualType Ty;
  3771. void Emit(CodeGenFunction &CGF, Flags flags) override {
  3772. QualType::DestructionKind DtorKind = Ty.isDestructedType();
  3773. if (DtorKind == QualType::DK_cxx_destructor) {
  3774. const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();
  3775. assert(!Dtor->isTrivial());
  3776. CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false,
  3777. /*Delegating=*/false, Addr, Ty);
  3778. } else {
  3779. CGF.callCStructDestructor(CGF.MakeAddrLValue(Addr, Ty));
  3780. }
  3781. }
  3782. };
  3783. struct DisableDebugLocationUpdates {
  3784. CodeGenFunction &CGF;
  3785. bool disabledDebugInfo;
  3786. DisableDebugLocationUpdates(CodeGenFunction &CGF, const Expr *E) : CGF(CGF) {
  3787. if ((disabledDebugInfo = isa<CXXDefaultArgExpr>(E) && CGF.getDebugInfo()))
  3788. CGF.disableDebugInfo();
  3789. }
  3790. ~DisableDebugLocationUpdates() {
  3791. if (disabledDebugInfo)
  3792. CGF.enableDebugInfo();
  3793. }
  3794. };
  3795. } // end anonymous namespace
  3796. RValue CallArg::getRValue(CodeGenFunction &CGF) const {
  3797. if (!HasLV)
  3798. return RV;
  3799. LValue Copy = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty), Ty);
  3800. CGF.EmitAggregateCopy(Copy, LV, Ty, AggValueSlot::DoesNotOverlap,
  3801. LV.isVolatile());
  3802. IsUsed = true;
  3803. return RValue::getAggregate(Copy.getAddress(CGF));
  3804. }
  3805. void CallArg::copyInto(CodeGenFunction &CGF, Address Addr) const {
  3806. LValue Dst = CGF.MakeAddrLValue(Addr, Ty);
  3807. if (!HasLV && RV.isScalar())
  3808. CGF.EmitStoreOfScalar(RV.getScalarVal(), Dst, /*isInit=*/true);
  3809. else if (!HasLV && RV.isComplex())
  3810. CGF.EmitStoreOfComplex(RV.getComplexVal(), Dst, /*init=*/true);
  3811. else {
  3812. auto Addr = HasLV ? LV.getAddress(CGF) : RV.getAggregateAddress();
  3813. LValue SrcLV = CGF.MakeAddrLValue(Addr, Ty);
  3814. // We assume that call args are never copied into subobjects.
  3815. CGF.EmitAggregateCopy(Dst, SrcLV, Ty, AggValueSlot::DoesNotOverlap,
  3816. HasLV ? LV.isVolatileQualified()
  3817. : RV.isVolatileQualified());
  3818. }
  3819. IsUsed = true;
  3820. }
  3821. void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
  3822. QualType type) {
  3823. DisableDebugLocationUpdates Dis(*this, E);
  3824. if (const ObjCIndirectCopyRestoreExpr *CRE
  3825. = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
  3826. assert(getLangOpts().ObjCAutoRefCount);
  3827. return emitWritebackArg(*this, args, CRE);
  3828. }
  3829. assert(type->isReferenceType() == E->isGLValue() &&
  3830. "reference binding to unmaterialized r-value!");
  3831. if (E->isGLValue()) {
  3832. assert(E->getObjectKind() == OK_Ordinary);
  3833. return args.add(EmitReferenceBindingToExpr(E), type);
  3834. }
  3835. bool HasAggregateEvalKind = hasAggregateEvaluationKind(type);
  3836. // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee.
  3837. // However, we still have to push an EH-only cleanup in case we unwind before
  3838. // we make it to the call.
  3839. if (type->isRecordType() &&
  3840. type->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee()) {
  3841. // If we're using inalloca, use the argument memory. Otherwise, use a
  3842. // temporary.
  3843. AggValueSlot Slot = args.isUsingInAlloca()
  3844. ? createPlaceholderSlot(*this, type) : CreateAggTemp(type, "agg.tmp");
  3845. bool DestroyedInCallee = true, NeedsEHCleanup = true;
  3846. if (const auto *RD = type->getAsCXXRecordDecl())
  3847. DestroyedInCallee = RD->hasNonTrivialDestructor();
  3848. else
  3849. NeedsEHCleanup = needsEHCleanup(type.isDestructedType());
  3850. if (DestroyedInCallee)
  3851. Slot.setExternallyDestructed();
  3852. EmitAggExpr(E, Slot);
  3853. RValue RV = Slot.asRValue();
  3854. args.add(RV, type);
  3855. if (DestroyedInCallee && NeedsEHCleanup) {
  3856. // Create a no-op GEP between the placeholder and the cleanup so we can
  3857. // RAUW it successfully. It also serves as a marker of the first
  3858. // instruction where the cleanup is active.
  3859. pushFullExprCleanup<DestroyUnpassedArg>(EHCleanup, Slot.getAddress(),
  3860. type);
  3861. // This unreachable is a temporary marker which will be removed later.
  3862. llvm::Instruction *IsActive = Builder.CreateUnreachable();
  3863. args.addArgCleanupDeactivation(EHStack.stable_begin(), IsActive);
  3864. }
  3865. return;
  3866. }
  3867. if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) &&
  3868. cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
  3869. LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
  3870. assert(L.isSimple());
  3871. args.addUncopiedAggregate(L, type);
  3872. return;
  3873. }
  3874. args.add(EmitAnyExprToTemp(E), type);
  3875. }
  3876. QualType CodeGenFunction::getVarArgType(const Expr *Arg) {
  3877. // System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC
  3878. // implicitly widens null pointer constants that are arguments to varargs
  3879. // functions to pointer-sized ints.
  3880. if (!getTarget().getTriple().isOSWindows())
  3881. return Arg->getType();
  3882. if (Arg->getType()->isIntegerType() &&
  3883. getContext().getTypeSize(Arg->getType()) <
  3884. getContext().getTargetInfo().getPointerWidth(LangAS::Default) &&
  3885. Arg->isNullPointerConstant(getContext(),
  3886. Expr::NPC_ValueDependentIsNotNull)) {
  3887. return getContext().getIntPtrType();
  3888. }
  3889. return Arg->getType();
  3890. }
  3891. // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
  3892. // optimizer it can aggressively ignore unwind edges.
  3893. void
  3894. CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {
  3895. if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&
  3896. !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
  3897. Inst->setMetadata("clang.arc.no_objc_arc_exceptions",
  3898. CGM.getNoObjCARCExceptionsMetadata());
  3899. }
  3900. /// Emits a call to the given no-arguments nounwind runtime function.
  3901. llvm::CallInst *
  3902. CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee,
  3903. const llvm::Twine &name) {
  3904. return EmitNounwindRuntimeCall(callee, std::nullopt, name);
  3905. }
  3906. /// Emits a call to the given nounwind runtime function.
  3907. llvm::CallInst *
  3908. CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee,
  3909. ArrayRef<llvm::Value *> args,
  3910. const llvm::Twine &name) {
  3911. llvm::CallInst *call = EmitRuntimeCall(callee, args, name);
  3912. call->setDoesNotThrow();
  3913. return call;
  3914. }
  3915. /// Emits a simple call (never an invoke) to the given no-arguments
  3916. /// runtime function.
  3917. llvm::CallInst *CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee,
  3918. const llvm::Twine &name) {
  3919. return EmitRuntimeCall(callee, std::nullopt, name);
  3920. }
  3921. // Calls which may throw must have operand bundles indicating which funclet
  3922. // they are nested within.
  3923. SmallVector<llvm::OperandBundleDef, 1>
  3924. CodeGenFunction::getBundlesForFunclet(llvm::Value *Callee) {
  3925. // There is no need for a funclet operand bundle if we aren't inside a
  3926. // funclet.
  3927. if (!CurrentFuncletPad)
  3928. return (SmallVector<llvm::OperandBundleDef, 1>());
  3929. // Skip intrinsics which cannot throw (as long as they don't lower into
  3930. // regular function calls in the course of IR transformations).
  3931. if (auto *CalleeFn = dyn_cast<llvm::Function>(Callee->stripPointerCasts())) {
  3932. if (CalleeFn->isIntrinsic() && CalleeFn->doesNotThrow()) {
  3933. auto IID = CalleeFn->getIntrinsicID();
  3934. if (!llvm::IntrinsicInst::mayLowerToFunctionCall(IID))
  3935. return (SmallVector<llvm::OperandBundleDef, 1>());
  3936. }
  3937. }
  3938. SmallVector<llvm::OperandBundleDef, 1> BundleList;
  3939. BundleList.emplace_back("funclet", CurrentFuncletPad);
  3940. return BundleList;
  3941. }
  3942. /// Emits a simple call (never an invoke) to the given runtime function.
  3943. llvm::CallInst *CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee,
  3944. ArrayRef<llvm::Value *> args,
  3945. const llvm::Twine &name) {
  3946. llvm::CallInst *call = Builder.CreateCall(
  3947. callee, args, getBundlesForFunclet(callee.getCallee()), name);
  3948. call->setCallingConv(getRuntimeCC());
  3949. return call;
  3950. }
  3951. /// Emits a call or invoke to the given noreturn runtime function.
  3952. void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(
  3953. llvm::FunctionCallee callee, ArrayRef<llvm::Value *> args) {
  3954. SmallVector<llvm::OperandBundleDef, 1> BundleList =
  3955. getBundlesForFunclet(callee.getCallee());
  3956. if (getInvokeDest()) {
  3957. llvm::InvokeInst *invoke =
  3958. Builder.CreateInvoke(callee,
  3959. getUnreachableBlock(),
  3960. getInvokeDest(),
  3961. args,
  3962. BundleList);
  3963. invoke->setDoesNotReturn();
  3964. invoke->setCallingConv(getRuntimeCC());
  3965. } else {
  3966. llvm::CallInst *call = Builder.CreateCall(callee, args, BundleList);
  3967. call->setDoesNotReturn();
  3968. call->setCallingConv(getRuntimeCC());
  3969. Builder.CreateUnreachable();
  3970. }
  3971. }
  3972. /// Emits a call or invoke instruction to the given nullary runtime function.
  3973. llvm::CallBase *
  3974. CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee,
  3975. const Twine &name) {
  3976. return EmitRuntimeCallOrInvoke(callee, std::nullopt, name);
  3977. }
  3978. /// Emits a call or invoke instruction to the given runtime function.
  3979. llvm::CallBase *
  3980. CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee,
  3981. ArrayRef<llvm::Value *> args,
  3982. const Twine &name) {
  3983. llvm::CallBase *call = EmitCallOrInvoke(callee, args, name);
  3984. call->setCallingConv(getRuntimeCC());
  3985. return call;
  3986. }
  3987. /// Emits a call or invoke instruction to the given function, depending
  3988. /// on the current state of the EH stack.
  3989. llvm::CallBase *CodeGenFunction::EmitCallOrInvoke(llvm::FunctionCallee Callee,
  3990. ArrayRef<llvm::Value *> Args,
  3991. const Twine &Name) {
  3992. llvm::BasicBlock *InvokeDest = getInvokeDest();
  3993. SmallVector<llvm::OperandBundleDef, 1> BundleList =
  3994. getBundlesForFunclet(Callee.getCallee());
  3995. llvm::CallBase *Inst;
  3996. if (!InvokeDest)
  3997. Inst = Builder.CreateCall(Callee, Args, BundleList, Name);
  3998. else {
  3999. llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
  4000. Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, BundleList,
  4001. Name);
  4002. EmitBlock(ContBB);
  4003. }
  4004. // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
  4005. // optimizer it can aggressively ignore unwind edges.
  4006. if (CGM.getLangOpts().ObjCAutoRefCount)
  4007. AddObjCARCExceptionMetadata(Inst);
  4008. return Inst;
  4009. }
  4010. void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old,
  4011. llvm::Value *New) {
  4012. DeferredReplacements.push_back(
  4013. std::make_pair(llvm::WeakTrackingVH(Old), New));
  4014. }
  4015. namespace {
  4016. /// Specify given \p NewAlign as the alignment of return value attribute. If
  4017. /// such attribute already exists, re-set it to the maximal one of two options.
  4018. [[nodiscard]] llvm::AttributeList
  4019. maybeRaiseRetAlignmentAttribute(llvm::LLVMContext &Ctx,
  4020. const llvm::AttributeList &Attrs,
  4021. llvm::Align NewAlign) {
  4022. llvm::Align CurAlign = Attrs.getRetAlignment().valueOrOne();
  4023. if (CurAlign >= NewAlign)
  4024. return Attrs;
  4025. llvm::Attribute AlignAttr = llvm::Attribute::getWithAlignment(Ctx, NewAlign);
  4026. return Attrs.removeRetAttribute(Ctx, llvm::Attribute::AttrKind::Alignment)
  4027. .addRetAttribute(Ctx, AlignAttr);
  4028. }
  4029. template <typename AlignedAttrTy> class AbstractAssumeAlignedAttrEmitter {
  4030. protected:
  4031. CodeGenFunction &CGF;
  4032. /// We do nothing if this is, or becomes, nullptr.
  4033. const AlignedAttrTy *AA = nullptr;
  4034. llvm::Value *Alignment = nullptr; // May or may not be a constant.
  4035. llvm::ConstantInt *OffsetCI = nullptr; // Constant, hopefully zero.
  4036. AbstractAssumeAlignedAttrEmitter(CodeGenFunction &CGF_, const Decl *FuncDecl)
  4037. : CGF(CGF_) {
  4038. if (!FuncDecl)
  4039. return;
  4040. AA = FuncDecl->getAttr<AlignedAttrTy>();
  4041. }
  4042. public:
  4043. /// If we can, materialize the alignment as an attribute on return value.
  4044. [[nodiscard]] llvm::AttributeList
  4045. TryEmitAsCallSiteAttribute(const llvm::AttributeList &Attrs) {
  4046. if (!AA || OffsetCI || CGF.SanOpts.has(SanitizerKind::Alignment))
  4047. return Attrs;
  4048. const auto *AlignmentCI = dyn_cast<llvm::ConstantInt>(Alignment);
  4049. if (!AlignmentCI)
  4050. return Attrs;
  4051. // We may legitimately have non-power-of-2 alignment here.
  4052. // If so, this is UB land, emit it via `@llvm.assume` instead.
  4053. if (!AlignmentCI->getValue().isPowerOf2())
  4054. return Attrs;
  4055. llvm::AttributeList NewAttrs = maybeRaiseRetAlignmentAttribute(
  4056. CGF.getLLVMContext(), Attrs,
  4057. llvm::Align(
  4058. AlignmentCI->getLimitedValue(llvm::Value::MaximumAlignment)));
  4059. AA = nullptr; // We're done. Disallow doing anything else.
  4060. return NewAttrs;
  4061. }
  4062. /// Emit alignment assumption.
  4063. /// This is a general fallback that we take if either there is an offset,
  4064. /// or the alignment is variable or we are sanitizing for alignment.
  4065. void EmitAsAnAssumption(SourceLocation Loc, QualType RetTy, RValue &Ret) {
  4066. if (!AA)
  4067. return;
  4068. CGF.emitAlignmentAssumption(Ret.getScalarVal(), RetTy, Loc,
  4069. AA->getLocation(), Alignment, OffsetCI);
  4070. AA = nullptr; // We're done. Disallow doing anything else.
  4071. }
  4072. };
  4073. /// Helper data structure to emit `AssumeAlignedAttr`.
  4074. class AssumeAlignedAttrEmitter final
  4075. : public AbstractAssumeAlignedAttrEmitter<AssumeAlignedAttr> {
  4076. public:
  4077. AssumeAlignedAttrEmitter(CodeGenFunction &CGF_, const Decl *FuncDecl)
  4078. : AbstractAssumeAlignedAttrEmitter(CGF_, FuncDecl) {
  4079. if (!AA)
  4080. return;
  4081. // It is guaranteed that the alignment/offset are constants.
  4082. Alignment = cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AA->getAlignment()));
  4083. if (Expr *Offset = AA->getOffset()) {
  4084. OffsetCI = cast<llvm::ConstantInt>(CGF.EmitScalarExpr(Offset));
  4085. if (OffsetCI->isNullValue()) // Canonicalize zero offset to no offset.
  4086. OffsetCI = nullptr;
  4087. }
  4088. }
  4089. };
  4090. /// Helper data structure to emit `AllocAlignAttr`.
  4091. class AllocAlignAttrEmitter final
  4092. : public AbstractAssumeAlignedAttrEmitter<AllocAlignAttr> {
  4093. public:
  4094. AllocAlignAttrEmitter(CodeGenFunction &CGF_, const Decl *FuncDecl,
  4095. const CallArgList &CallArgs)
  4096. : AbstractAssumeAlignedAttrEmitter(CGF_, FuncDecl) {
  4097. if (!AA)
  4098. return;
  4099. // Alignment may or may not be a constant, and that is okay.
  4100. Alignment = CallArgs[AA->getParamIndex().getLLVMIndex()]
  4101. .getRValue(CGF)
  4102. .getScalarVal();
  4103. }
  4104. };
  4105. } // namespace
  4106. static unsigned getMaxVectorWidth(const llvm::Type *Ty) {
  4107. if (auto *VT = dyn_cast<llvm::VectorType>(Ty))
  4108. return VT->getPrimitiveSizeInBits().getKnownMinValue();
  4109. if (auto *AT = dyn_cast<llvm::ArrayType>(Ty))
  4110. return getMaxVectorWidth(AT->getElementType());
  4111. unsigned MaxVectorWidth = 0;
  4112. if (auto *ST = dyn_cast<llvm::StructType>(Ty))
  4113. for (auto *I : ST->elements())
  4114. MaxVectorWidth = std::max(MaxVectorWidth, getMaxVectorWidth(I));
  4115. return MaxVectorWidth;
  4116. }
  4117. RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
  4118. const CGCallee &Callee,
  4119. ReturnValueSlot ReturnValue,
  4120. const CallArgList &CallArgs,
  4121. llvm::CallBase **callOrInvoke, bool IsMustTail,
  4122. SourceLocation Loc) {
  4123. // FIXME: We no longer need the types from CallArgs; lift up and simplify.
  4124. assert(Callee.isOrdinary() || Callee.isVirtual());
  4125. // Handle struct-return functions by passing a pointer to the
  4126. // location that we would like to return into.
  4127. QualType RetTy = CallInfo.getReturnType();
  4128. const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
  4129. llvm::FunctionType *IRFuncTy = getTypes().GetFunctionType(CallInfo);
  4130. const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl();
  4131. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
  4132. // We can only guarantee that a function is called from the correct
  4133. // context/function based on the appropriate target attributes,
  4134. // so only check in the case where we have both always_inline and target
  4135. // since otherwise we could be making a conditional call after a check for
  4136. // the proper cpu features (and it won't cause code generation issues due to
  4137. // function based code generation).
  4138. if (TargetDecl->hasAttr<AlwaysInlineAttr>() &&
  4139. TargetDecl->hasAttr<TargetAttr>())
  4140. checkTargetFeatures(Loc, FD);
  4141. // Some architectures (such as x86-64) have the ABI changed based on
  4142. // attribute-target/features. Give them a chance to diagnose.
  4143. CGM.getTargetCodeGenInfo().checkFunctionCallABI(
  4144. CGM, Loc, dyn_cast_or_null<FunctionDecl>(CurCodeDecl), FD, CallArgs);
  4145. }
  4146. #ifndef NDEBUG
  4147. if (!(CallInfo.isVariadic() && CallInfo.getArgStruct())) {
  4148. // For an inalloca varargs function, we don't expect CallInfo to match the
  4149. // function pointer's type, because the inalloca struct a will have extra
  4150. // fields in it for the varargs parameters. Code later in this function
  4151. // bitcasts the function pointer to the type derived from CallInfo.
  4152. //
  4153. // In other cases, we assert that the types match up (until pointers stop
  4154. // having pointee types).
  4155. if (Callee.isVirtual())
  4156. assert(IRFuncTy == Callee.getVirtualFunctionType());
  4157. else {
  4158. llvm::PointerType *PtrTy =
  4159. llvm::cast<llvm::PointerType>(Callee.getFunctionPointer()->getType());
  4160. assert(PtrTy->isOpaqueOrPointeeTypeMatches(IRFuncTy));
  4161. }
  4162. }
  4163. #endif
  4164. // 1. Set up the arguments.
  4165. // If we're using inalloca, insert the allocation after the stack save.
  4166. // FIXME: Do this earlier rather than hacking it in here!
  4167. Address ArgMemory = Address::invalid();
  4168. if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) {
  4169. const llvm::DataLayout &DL = CGM.getDataLayout();
  4170. llvm::Instruction *IP = CallArgs.getStackBase();
  4171. llvm::AllocaInst *AI;
  4172. if (IP) {
  4173. IP = IP->getNextNode();
  4174. AI = new llvm::AllocaInst(ArgStruct, DL.getAllocaAddrSpace(),
  4175. "argmem", IP);
  4176. } else {
  4177. AI = CreateTempAlloca(ArgStruct, "argmem");
  4178. }
  4179. auto Align = CallInfo.getArgStructAlignment();
  4180. AI->setAlignment(Align.getAsAlign());
  4181. AI->setUsedWithInAlloca(true);
  4182. assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca());
  4183. ArgMemory = Address(AI, ArgStruct, Align);
  4184. }
  4185. ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), CallInfo);
  4186. SmallVector<llvm::Value *, 16> IRCallArgs(IRFunctionArgs.totalIRArgs());
  4187. // If the call returns a temporary with struct return, create a temporary
  4188. // alloca to hold the result, unless one is given to us.
  4189. Address SRetPtr = Address::invalid();
  4190. Address SRetAlloca = Address::invalid();
  4191. llvm::Value *UnusedReturnSizePtr = nullptr;
  4192. if (RetAI.isIndirect() || RetAI.isInAlloca() || RetAI.isCoerceAndExpand()) {
  4193. if (!ReturnValue.isNull()) {
  4194. SRetPtr = ReturnValue.getValue();
  4195. } else {
  4196. SRetPtr = CreateMemTemp(RetTy, "tmp", &SRetAlloca);
  4197. if (HaveInsertPoint() && ReturnValue.isUnused()) {
  4198. llvm::TypeSize size =
  4199. CGM.getDataLayout().getTypeAllocSize(ConvertTypeForMem(RetTy));
  4200. UnusedReturnSizePtr = EmitLifetimeStart(size, SRetAlloca.getPointer());
  4201. }
  4202. }
  4203. if (IRFunctionArgs.hasSRetArg()) {
  4204. IRCallArgs[IRFunctionArgs.getSRetArgNo()] = SRetPtr.getPointer();
  4205. } else if (RetAI.isInAlloca()) {
  4206. Address Addr =
  4207. Builder.CreateStructGEP(ArgMemory, RetAI.getInAllocaFieldIndex());
  4208. Builder.CreateStore(SRetPtr.getPointer(), Addr);
  4209. }
  4210. }
  4211. Address swiftErrorTemp = Address::invalid();
  4212. Address swiftErrorArg = Address::invalid();
  4213. // When passing arguments using temporary allocas, we need to add the
  4214. // appropriate lifetime markers. This vector keeps track of all the lifetime
  4215. // markers that need to be ended right after the call.
  4216. SmallVector<CallLifetimeEnd, 2> CallLifetimeEndAfterCall;
  4217. // Translate all of the arguments as necessary to match the IR lowering.
  4218. assert(CallInfo.arg_size() == CallArgs.size() &&
  4219. "Mismatch between function signature & arguments.");
  4220. unsigned ArgNo = 0;
  4221. CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
  4222. for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
  4223. I != E; ++I, ++info_it, ++ArgNo) {
  4224. const ABIArgInfo &ArgInfo = info_it->info;
  4225. // Insert a padding argument to ensure proper alignment.
  4226. if (IRFunctionArgs.hasPaddingArg(ArgNo))
  4227. IRCallArgs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
  4228. llvm::UndefValue::get(ArgInfo.getPaddingType());
  4229. unsigned FirstIRArg, NumIRArgs;
  4230. std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
  4231. bool ArgHasMaybeUndefAttr =
  4232. IsArgumentMaybeUndef(TargetDecl, CallInfo.getNumRequiredArgs(), ArgNo);
  4233. switch (ArgInfo.getKind()) {
  4234. case ABIArgInfo::InAlloca: {
  4235. assert(NumIRArgs == 0);
  4236. assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
  4237. if (I->isAggregate()) {
  4238. Address Addr = I->hasLValue()
  4239. ? I->getKnownLValue().getAddress(*this)
  4240. : I->getKnownRValue().getAggregateAddress();
  4241. llvm::Instruction *Placeholder =
  4242. cast<llvm::Instruction>(Addr.getPointer());
  4243. if (!ArgInfo.getInAllocaIndirect()) {
  4244. // Replace the placeholder with the appropriate argument slot GEP.
  4245. CGBuilderTy::InsertPoint IP = Builder.saveIP();
  4246. Builder.SetInsertPoint(Placeholder);
  4247. Addr = Builder.CreateStructGEP(ArgMemory,
  4248. ArgInfo.getInAllocaFieldIndex());
  4249. Builder.restoreIP(IP);
  4250. } else {
  4251. // For indirect things such as overaligned structs, replace the
  4252. // placeholder with a regular aggregate temporary alloca. Store the
  4253. // address of this alloca into the struct.
  4254. Addr = CreateMemTemp(info_it->type, "inalloca.indirect.tmp");
  4255. Address ArgSlot = Builder.CreateStructGEP(
  4256. ArgMemory, ArgInfo.getInAllocaFieldIndex());
  4257. Builder.CreateStore(Addr.getPointer(), ArgSlot);
  4258. }
  4259. deferPlaceholderReplacement(Placeholder, Addr.getPointer());
  4260. } else if (ArgInfo.getInAllocaIndirect()) {
  4261. // Make a temporary alloca and store the address of it into the argument
  4262. // struct.
  4263. Address Addr = CreateMemTempWithoutCast(
  4264. I->Ty, getContext().getTypeAlignInChars(I->Ty),
  4265. "indirect-arg-temp");
  4266. I->copyInto(*this, Addr);
  4267. Address ArgSlot =
  4268. Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex());
  4269. Builder.CreateStore(Addr.getPointer(), ArgSlot);
  4270. } else {
  4271. // Store the RValue into the argument struct.
  4272. Address Addr =
  4273. Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex());
  4274. // There are some cases where a trivial bitcast is not avoidable. The
  4275. // definition of a type later in a translation unit may change it's type
  4276. // from {}* to (%struct.foo*)*.
  4277. Addr = Builder.CreateElementBitCast(Addr, ConvertTypeForMem(I->Ty));
  4278. I->copyInto(*this, Addr);
  4279. }
  4280. break;
  4281. }
  4282. case ABIArgInfo::Indirect:
  4283. case ABIArgInfo::IndirectAliased: {
  4284. assert(NumIRArgs == 1);
  4285. if (!I->isAggregate()) {
  4286. // Make a temporary alloca to pass the argument.
  4287. Address Addr = CreateMemTempWithoutCast(
  4288. I->Ty, ArgInfo.getIndirectAlign(), "indirect-arg-temp");
  4289. llvm::Value *Val = Addr.getPointer();
  4290. if (ArgHasMaybeUndefAttr)
  4291. Val = Builder.CreateFreeze(Addr.getPointer());
  4292. IRCallArgs[FirstIRArg] = Val;
  4293. I->copyInto(*this, Addr);
  4294. } else {
  4295. // We want to avoid creating an unnecessary temporary+copy here;
  4296. // however, we need one in three cases:
  4297. // 1. If the argument is not byval, and we are required to copy the
  4298. // source. (This case doesn't occur on any common architecture.)
  4299. // 2. If the argument is byval, RV is not sufficiently aligned, and
  4300. // we cannot force it to be sufficiently aligned.
  4301. // 3. If the argument is byval, but RV is not located in default
  4302. // or alloca address space.
  4303. Address Addr = I->hasLValue()
  4304. ? I->getKnownLValue().getAddress(*this)
  4305. : I->getKnownRValue().getAggregateAddress();
  4306. llvm::Value *V = Addr.getPointer();
  4307. CharUnits Align = ArgInfo.getIndirectAlign();
  4308. const llvm::DataLayout *TD = &CGM.getDataLayout();
  4309. assert((FirstIRArg >= IRFuncTy->getNumParams() ||
  4310. IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace() ==
  4311. TD->getAllocaAddrSpace()) &&
  4312. "indirect argument must be in alloca address space");
  4313. bool NeedCopy = false;
  4314. if (Addr.getAlignment() < Align &&
  4315. llvm::getOrEnforceKnownAlignment(V, Align.getAsAlign(), *TD) <
  4316. Align.getAsAlign()) {
  4317. NeedCopy = true;
  4318. } else if (I->hasLValue()) {
  4319. auto LV = I->getKnownLValue();
  4320. auto AS = LV.getAddressSpace();
  4321. if (!ArgInfo.getIndirectByVal() ||
  4322. (LV.getAlignment() < getContext().getTypeAlignInChars(I->Ty))) {
  4323. NeedCopy = true;
  4324. }
  4325. if (!getLangOpts().OpenCL) {
  4326. if ((ArgInfo.getIndirectByVal() &&
  4327. (AS != LangAS::Default &&
  4328. AS != CGM.getASTAllocaAddressSpace()))) {
  4329. NeedCopy = true;
  4330. }
  4331. }
  4332. // For OpenCL even if RV is located in default or alloca address space
  4333. // we don't want to perform address space cast for it.
  4334. else if ((ArgInfo.getIndirectByVal() &&
  4335. Addr.getType()->getAddressSpace() != IRFuncTy->
  4336. getParamType(FirstIRArg)->getPointerAddressSpace())) {
  4337. NeedCopy = true;
  4338. }
  4339. }
  4340. if (NeedCopy) {
  4341. // Create an aligned temporary, and copy to it.
  4342. Address AI = CreateMemTempWithoutCast(
  4343. I->Ty, ArgInfo.getIndirectAlign(), "byval-temp");
  4344. llvm::Value *Val = AI.getPointer();
  4345. if (ArgHasMaybeUndefAttr)
  4346. Val = Builder.CreateFreeze(AI.getPointer());
  4347. IRCallArgs[FirstIRArg] = Val;
  4348. // Emit lifetime markers for the temporary alloca.
  4349. llvm::TypeSize ByvalTempElementSize =
  4350. CGM.getDataLayout().getTypeAllocSize(AI.getElementType());
  4351. llvm::Value *LifetimeSize =
  4352. EmitLifetimeStart(ByvalTempElementSize, AI.getPointer());
  4353. // Add cleanup code to emit the end lifetime marker after the call.
  4354. if (LifetimeSize) // In case we disabled lifetime markers.
  4355. CallLifetimeEndAfterCall.emplace_back(AI, LifetimeSize);
  4356. // Generate the copy.
  4357. I->copyInto(*this, AI);
  4358. } else {
  4359. // Skip the extra memcpy call.
  4360. auto *T = llvm::PointerType::getWithSamePointeeType(
  4361. cast<llvm::PointerType>(V->getType()),
  4362. CGM.getDataLayout().getAllocaAddrSpace());
  4363. llvm::Value *Val = getTargetHooks().performAddrSpaceCast(
  4364. *this, V, LangAS::Default, CGM.getASTAllocaAddressSpace(), T,
  4365. true);
  4366. if (ArgHasMaybeUndefAttr)
  4367. Val = Builder.CreateFreeze(Val);
  4368. IRCallArgs[FirstIRArg] = Val;
  4369. }
  4370. }
  4371. break;
  4372. }
  4373. case ABIArgInfo::Ignore:
  4374. assert(NumIRArgs == 0);
  4375. break;
  4376. case ABIArgInfo::Extend:
  4377. case ABIArgInfo::Direct: {
  4378. if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
  4379. ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
  4380. ArgInfo.getDirectOffset() == 0) {
  4381. assert(NumIRArgs == 1);
  4382. llvm::Value *V;
  4383. if (!I->isAggregate())
  4384. V = I->getKnownRValue().getScalarVal();
  4385. else
  4386. V = Builder.CreateLoad(
  4387. I->hasLValue() ? I->getKnownLValue().getAddress(*this)
  4388. : I->getKnownRValue().getAggregateAddress());
  4389. // Implement swifterror by copying into a new swifterror argument.
  4390. // We'll write back in the normal path out of the call.
  4391. if (CallInfo.getExtParameterInfo(ArgNo).getABI()
  4392. == ParameterABI::SwiftErrorResult) {
  4393. assert(!swiftErrorTemp.isValid() && "multiple swifterror args");
  4394. QualType pointeeTy = I->Ty->getPointeeType();
  4395. swiftErrorArg = Address(V, ConvertTypeForMem(pointeeTy),
  4396. getContext().getTypeAlignInChars(pointeeTy));
  4397. swiftErrorTemp =
  4398. CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");
  4399. V = swiftErrorTemp.getPointer();
  4400. cast<llvm::AllocaInst>(V)->setSwiftError(true);
  4401. llvm::Value *errorValue = Builder.CreateLoad(swiftErrorArg);
  4402. Builder.CreateStore(errorValue, swiftErrorTemp);
  4403. }
  4404. // We might have to widen integers, but we should never truncate.
  4405. if (ArgInfo.getCoerceToType() != V->getType() &&
  4406. V->getType()->isIntegerTy())
  4407. V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());
  4408. // If the argument doesn't match, perform a bitcast to coerce it. This
  4409. // can happen due to trivial type mismatches.
  4410. if (FirstIRArg < IRFuncTy->getNumParams() &&
  4411. V->getType() != IRFuncTy->getParamType(FirstIRArg))
  4412. V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
  4413. if (ArgHasMaybeUndefAttr)
  4414. V = Builder.CreateFreeze(V);
  4415. IRCallArgs[FirstIRArg] = V;
  4416. break;
  4417. }
  4418. // FIXME: Avoid the conversion through memory if possible.
  4419. Address Src = Address::invalid();
  4420. if (!I->isAggregate()) {
  4421. Src = CreateMemTemp(I->Ty, "coerce");
  4422. I->copyInto(*this, Src);
  4423. } else {
  4424. Src = I->hasLValue() ? I->getKnownLValue().getAddress(*this)
  4425. : I->getKnownRValue().getAggregateAddress();
  4426. }
  4427. // If the value is offset in memory, apply the offset now.
  4428. Src = emitAddressAtOffset(*this, Src, ArgInfo);
  4429. // Fast-isel and the optimizer generally like scalar values better than
  4430. // FCAs, so we flatten them if this is safe to do for this argument.
  4431. llvm::StructType *STy =
  4432. dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType());
  4433. if (STy && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
  4434. llvm::Type *SrcTy = Src.getElementType();
  4435. uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy);
  4436. uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy);
  4437. // If the source type is smaller than the destination type of the
  4438. // coerce-to logic, copy the source value into a temp alloca the size
  4439. // of the destination type to allow loading all of it. The bits past
  4440. // the source value are left undef.
  4441. if (SrcSize < DstSize) {
  4442. Address TempAlloca
  4443. = CreateTempAlloca(STy, Src.getAlignment(),
  4444. Src.getName() + ".coerce");
  4445. Builder.CreateMemCpy(TempAlloca, Src, SrcSize);
  4446. Src = TempAlloca;
  4447. } else {
  4448. Src = Builder.CreateElementBitCast(Src, STy);
  4449. }
  4450. assert(NumIRArgs == STy->getNumElements());
  4451. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  4452. Address EltPtr = Builder.CreateStructGEP(Src, i);
  4453. llvm::Value *LI = Builder.CreateLoad(EltPtr);
  4454. if (ArgHasMaybeUndefAttr)
  4455. LI = Builder.CreateFreeze(LI);
  4456. IRCallArgs[FirstIRArg + i] = LI;
  4457. }
  4458. } else {
  4459. // In the simple case, just pass the coerced loaded value.
  4460. assert(NumIRArgs == 1);
  4461. llvm::Value *Load =
  4462. CreateCoercedLoad(Src, ArgInfo.getCoerceToType(), *this);
  4463. if (CallInfo.isCmseNSCall()) {
  4464. // For certain parameter types, clear padding bits, as they may reveal
  4465. // sensitive information.
  4466. // Small struct/union types are passed as integer arrays.
  4467. auto *ATy = dyn_cast<llvm::ArrayType>(Load->getType());
  4468. if (ATy != nullptr && isa<RecordType>(I->Ty.getCanonicalType()))
  4469. Load = EmitCMSEClearRecord(Load, ATy, I->Ty);
  4470. }
  4471. if (ArgHasMaybeUndefAttr)
  4472. Load = Builder.CreateFreeze(Load);
  4473. IRCallArgs[FirstIRArg] = Load;
  4474. }
  4475. break;
  4476. }
  4477. case ABIArgInfo::CoerceAndExpand: {
  4478. auto coercionType = ArgInfo.getCoerceAndExpandType();
  4479. auto layout = CGM.getDataLayout().getStructLayout(coercionType);
  4480. llvm::Value *tempSize = nullptr;
  4481. Address addr = Address::invalid();
  4482. Address AllocaAddr = Address::invalid();
  4483. if (I->isAggregate()) {
  4484. addr = I->hasLValue() ? I->getKnownLValue().getAddress(*this)
  4485. : I->getKnownRValue().getAggregateAddress();
  4486. } else {
  4487. RValue RV = I->getKnownRValue();
  4488. assert(RV.isScalar()); // complex should always just be direct
  4489. llvm::Type *scalarType = RV.getScalarVal()->getType();
  4490. auto scalarSize = CGM.getDataLayout().getTypeAllocSize(scalarType);
  4491. auto scalarAlign = CGM.getDataLayout().getPrefTypeAlign(scalarType);
  4492. // Materialize to a temporary.
  4493. addr = CreateTempAlloca(
  4494. RV.getScalarVal()->getType(),
  4495. CharUnits::fromQuantity(std::max(layout->getAlignment(), scalarAlign)),
  4496. "tmp",
  4497. /*ArraySize=*/nullptr, &AllocaAddr);
  4498. tempSize = EmitLifetimeStart(scalarSize, AllocaAddr.getPointer());
  4499. Builder.CreateStore(RV.getScalarVal(), addr);
  4500. }
  4501. addr = Builder.CreateElementBitCast(addr, coercionType);
  4502. unsigned IRArgPos = FirstIRArg;
  4503. for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
  4504. llvm::Type *eltType = coercionType->getElementType(i);
  4505. if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue;
  4506. Address eltAddr = Builder.CreateStructGEP(addr, i);
  4507. llvm::Value *elt = Builder.CreateLoad(eltAddr);
  4508. if (ArgHasMaybeUndefAttr)
  4509. elt = Builder.CreateFreeze(elt);
  4510. IRCallArgs[IRArgPos++] = elt;
  4511. }
  4512. assert(IRArgPos == FirstIRArg + NumIRArgs);
  4513. if (tempSize) {
  4514. EmitLifetimeEnd(tempSize, AllocaAddr.getPointer());
  4515. }
  4516. break;
  4517. }
  4518. case ABIArgInfo::Expand: {
  4519. unsigned IRArgPos = FirstIRArg;
  4520. ExpandTypeToArgs(I->Ty, *I, IRFuncTy, IRCallArgs, IRArgPos);
  4521. assert(IRArgPos == FirstIRArg + NumIRArgs);
  4522. break;
  4523. }
  4524. }
  4525. }
  4526. const CGCallee &ConcreteCallee = Callee.prepareConcreteCallee(*this);
  4527. llvm::Value *CalleePtr = ConcreteCallee.getFunctionPointer();
  4528. // If we're using inalloca, set up that argument.
  4529. if (ArgMemory.isValid()) {
  4530. llvm::Value *Arg = ArgMemory.getPointer();
  4531. if (CallInfo.isVariadic()) {
  4532. // When passing non-POD arguments by value to variadic functions, we will
  4533. // end up with a variadic prototype and an inalloca call site. In such
  4534. // cases, we can't do any parameter mismatch checks. Give up and bitcast
  4535. // the callee.
  4536. unsigned CalleeAS = CalleePtr->getType()->getPointerAddressSpace();
  4537. CalleePtr =
  4538. Builder.CreateBitCast(CalleePtr, IRFuncTy->getPointerTo(CalleeAS));
  4539. } else {
  4540. llvm::Type *LastParamTy =
  4541. IRFuncTy->getParamType(IRFuncTy->getNumParams() - 1);
  4542. if (Arg->getType() != LastParamTy) {
  4543. #ifndef NDEBUG
  4544. // Assert that these structs have equivalent element types.
  4545. llvm::StructType *FullTy = CallInfo.getArgStruct();
  4546. if (!LastParamTy->isOpaquePointerTy()) {
  4547. llvm::StructType *DeclaredTy = cast<llvm::StructType>(
  4548. LastParamTy->getNonOpaquePointerElementType());
  4549. assert(DeclaredTy->getNumElements() == FullTy->getNumElements());
  4550. for (auto DI = DeclaredTy->element_begin(),
  4551. DE = DeclaredTy->element_end(),
  4552. FI = FullTy->element_begin();
  4553. DI != DE; ++DI, ++FI)
  4554. assert(*DI == *FI);
  4555. }
  4556. #endif
  4557. Arg = Builder.CreateBitCast(Arg, LastParamTy);
  4558. }
  4559. }
  4560. assert(IRFunctionArgs.hasInallocaArg());
  4561. IRCallArgs[IRFunctionArgs.getInallocaArgNo()] = Arg;
  4562. }
  4563. // 2. Prepare the function pointer.
  4564. // If the callee is a bitcast of a non-variadic function to have a
  4565. // variadic function pointer type, check to see if we can remove the
  4566. // bitcast. This comes up with unprototyped functions.
  4567. //
  4568. // This makes the IR nicer, but more importantly it ensures that we
  4569. // can inline the function at -O0 if it is marked always_inline.
  4570. auto simplifyVariadicCallee = [](llvm::FunctionType *CalleeFT,
  4571. llvm::Value *Ptr) -> llvm::Function * {
  4572. if (!CalleeFT->isVarArg())
  4573. return nullptr;
  4574. // Get underlying value if it's a bitcast
  4575. if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Ptr)) {
  4576. if (CE->getOpcode() == llvm::Instruction::BitCast)
  4577. Ptr = CE->getOperand(0);
  4578. }
  4579. llvm::Function *OrigFn = dyn_cast<llvm::Function>(Ptr);
  4580. if (!OrigFn)
  4581. return nullptr;
  4582. llvm::FunctionType *OrigFT = OrigFn->getFunctionType();
  4583. // If the original type is variadic, or if any of the component types
  4584. // disagree, we cannot remove the cast.
  4585. if (OrigFT->isVarArg() ||
  4586. OrigFT->getNumParams() != CalleeFT->getNumParams() ||
  4587. OrigFT->getReturnType() != CalleeFT->getReturnType())
  4588. return nullptr;
  4589. for (unsigned i = 0, e = OrigFT->getNumParams(); i != e; ++i)
  4590. if (OrigFT->getParamType(i) != CalleeFT->getParamType(i))
  4591. return nullptr;
  4592. return OrigFn;
  4593. };
  4594. if (llvm::Function *OrigFn = simplifyVariadicCallee(IRFuncTy, CalleePtr)) {
  4595. CalleePtr = OrigFn;
  4596. IRFuncTy = OrigFn->getFunctionType();
  4597. }
  4598. // 3. Perform the actual call.
  4599. // Deactivate any cleanups that we're supposed to do immediately before
  4600. // the call.
  4601. if (!CallArgs.getCleanupsToDeactivate().empty())
  4602. deactivateArgCleanupsBeforeCall(*this, CallArgs);
  4603. // Assert that the arguments we computed match up. The IR verifier
  4604. // will catch this, but this is a common enough source of problems
  4605. // during IRGen changes that it's way better for debugging to catch
  4606. // it ourselves here.
  4607. #ifndef NDEBUG
  4608. assert(IRCallArgs.size() == IRFuncTy->getNumParams() || IRFuncTy->isVarArg());
  4609. for (unsigned i = 0; i < IRCallArgs.size(); ++i) {
  4610. // Inalloca argument can have different type.
  4611. if (IRFunctionArgs.hasInallocaArg() &&
  4612. i == IRFunctionArgs.getInallocaArgNo())
  4613. continue;
  4614. if (i < IRFuncTy->getNumParams())
  4615. assert(IRCallArgs[i]->getType() == IRFuncTy->getParamType(i));
  4616. }
  4617. #endif
  4618. // Update the largest vector width if any arguments have vector types.
  4619. for (unsigned i = 0; i < IRCallArgs.size(); ++i)
  4620. LargestVectorWidth = std::max(LargestVectorWidth,
  4621. getMaxVectorWidth(IRCallArgs[i]->getType()));
  4622. // Compute the calling convention and attributes.
  4623. unsigned CallingConv;
  4624. llvm::AttributeList Attrs;
  4625. CGM.ConstructAttributeList(CalleePtr->getName(), CallInfo,
  4626. Callee.getAbstractInfo(), Attrs, CallingConv,
  4627. /*AttrOnCallSite=*/true,
  4628. /*IsThunk=*/false);
  4629. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl))
  4630. if (FD->hasAttr<StrictFPAttr>())
  4631. // All calls within a strictfp function are marked strictfp
  4632. Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::StrictFP);
  4633. // Add call-site nomerge attribute if exists.
  4634. if (InNoMergeAttributedStmt)
  4635. Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::NoMerge);
  4636. // Add call-site noinline attribute if exists.
  4637. if (InNoInlineAttributedStmt)
  4638. Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::NoInline);
  4639. // Add call-site always_inline attribute if exists.
  4640. if (InAlwaysInlineAttributedStmt)
  4641. Attrs =
  4642. Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::AlwaysInline);
  4643. // Apply some call-site-specific attributes.
  4644. // TODO: work this into building the attribute set.
  4645. // Apply always_inline to all calls within flatten functions.
  4646. // FIXME: should this really take priority over __try, below?
  4647. if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() &&
  4648. !InNoInlineAttributedStmt &&
  4649. !(TargetDecl && TargetDecl->hasAttr<NoInlineAttr>())) {
  4650. Attrs =
  4651. Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::AlwaysInline);
  4652. }
  4653. // Disable inlining inside SEH __try blocks.
  4654. if (isSEHTryScope()) {
  4655. Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::NoInline);
  4656. }
  4657. // Decide whether to use a call or an invoke.
  4658. bool CannotThrow;
  4659. if (currentFunctionUsesSEHTry()) {
  4660. // SEH cares about asynchronous exceptions, so everything can "throw."
  4661. CannotThrow = false;
  4662. } else if (isCleanupPadScope() &&
  4663. EHPersonality::get(*this).isMSVCXXPersonality()) {
  4664. // The MSVC++ personality will implicitly terminate the program if an
  4665. // exception is thrown during a cleanup outside of a try/catch.
  4666. // We don't need to model anything in IR to get this behavior.
  4667. CannotThrow = true;
  4668. } else {
  4669. // Otherwise, nounwind call sites will never throw.
  4670. CannotThrow = Attrs.hasFnAttr(llvm::Attribute::NoUnwind);
  4671. if (auto *FPtr = dyn_cast<llvm::Function>(CalleePtr))
  4672. if (FPtr->hasFnAttribute(llvm::Attribute::NoUnwind))
  4673. CannotThrow = true;
  4674. }
  4675. // If we made a temporary, be sure to clean up after ourselves. Note that we
  4676. // can't depend on being inside of an ExprWithCleanups, so we need to manually
  4677. // pop this cleanup later on. Being eager about this is OK, since this
  4678. // temporary is 'invisible' outside of the callee.
  4679. if (UnusedReturnSizePtr)
  4680. pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, SRetAlloca,
  4681. UnusedReturnSizePtr);
  4682. llvm::BasicBlock *InvokeDest = CannotThrow ? nullptr : getInvokeDest();
  4683. SmallVector<llvm::OperandBundleDef, 1> BundleList =
  4684. getBundlesForFunclet(CalleePtr);
  4685. if (SanOpts.has(SanitizerKind::KCFI) &&
  4686. !isa_and_nonnull<FunctionDecl>(TargetDecl))
  4687. EmitKCFIOperandBundle(ConcreteCallee, BundleList);
  4688. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl))
  4689. if (FD->hasAttr<StrictFPAttr>())
  4690. // All calls within a strictfp function are marked strictfp
  4691. Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::StrictFP);
  4692. AssumeAlignedAttrEmitter AssumeAlignedAttrEmitter(*this, TargetDecl);
  4693. Attrs = AssumeAlignedAttrEmitter.TryEmitAsCallSiteAttribute(Attrs);
  4694. AllocAlignAttrEmitter AllocAlignAttrEmitter(*this, TargetDecl, CallArgs);
  4695. Attrs = AllocAlignAttrEmitter.TryEmitAsCallSiteAttribute(Attrs);
  4696. // Emit the actual call/invoke instruction.
  4697. llvm::CallBase *CI;
  4698. if (!InvokeDest) {
  4699. CI = Builder.CreateCall(IRFuncTy, CalleePtr, IRCallArgs, BundleList);
  4700. } else {
  4701. llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
  4702. CI = Builder.CreateInvoke(IRFuncTy, CalleePtr, Cont, InvokeDest, IRCallArgs,
  4703. BundleList);
  4704. EmitBlock(Cont);
  4705. }
  4706. if (callOrInvoke)
  4707. *callOrInvoke = CI;
  4708. // If this is within a function that has the guard(nocf) attribute and is an
  4709. // indirect call, add the "guard_nocf" attribute to this call to indicate that
  4710. // Control Flow Guard checks should not be added, even if the call is inlined.
  4711. if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
  4712. if (const auto *A = FD->getAttr<CFGuardAttr>()) {
  4713. if (A->getGuard() == CFGuardAttr::GuardArg::nocf && !CI->getCalledFunction())
  4714. Attrs = Attrs.addFnAttribute(getLLVMContext(), "guard_nocf");
  4715. }
  4716. }
  4717. // Apply the attributes and calling convention.
  4718. CI->setAttributes(Attrs);
  4719. CI->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
  4720. // Apply various metadata.
  4721. if (!CI->getType()->isVoidTy())
  4722. CI->setName("call");
  4723. // Update largest vector width from the return type.
  4724. LargestVectorWidth =
  4725. std::max(LargestVectorWidth, getMaxVectorWidth(CI->getType()));
  4726. // Insert instrumentation or attach profile metadata at indirect call sites.
  4727. // For more details, see the comment before the definition of
  4728. // IPVK_IndirectCallTarget in InstrProfData.inc.
  4729. if (!CI->getCalledFunction())
  4730. PGO.valueProfile(Builder, llvm::IPVK_IndirectCallTarget,
  4731. CI, CalleePtr);
  4732. // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
  4733. // optimizer it can aggressively ignore unwind edges.
  4734. if (CGM.getLangOpts().ObjCAutoRefCount)
  4735. AddObjCARCExceptionMetadata(CI);
  4736. // Set tail call kind if necessary.
  4737. if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(CI)) {
  4738. if (TargetDecl && TargetDecl->hasAttr<NotTailCalledAttr>())
  4739. Call->setTailCallKind(llvm::CallInst::TCK_NoTail);
  4740. else if (IsMustTail)
  4741. Call->setTailCallKind(llvm::CallInst::TCK_MustTail);
  4742. }
  4743. // Add metadata for calls to MSAllocator functions
  4744. if (getDebugInfo() && TargetDecl &&
  4745. TargetDecl->hasAttr<MSAllocatorAttr>())
  4746. getDebugInfo()->addHeapAllocSiteMetadata(CI, RetTy->getPointeeType(), Loc);
  4747. // Add metadata if calling an __attribute__((error(""))) or warning fn.
  4748. if (TargetDecl && TargetDecl->hasAttr<ErrorAttr>()) {
  4749. llvm::ConstantInt *Line =
  4750. llvm::ConstantInt::get(Int32Ty, Loc.getRawEncoding());
  4751. llvm::ConstantAsMetadata *MD = llvm::ConstantAsMetadata::get(Line);
  4752. llvm::MDTuple *MDT = llvm::MDNode::get(getLLVMContext(), {MD});
  4753. CI->setMetadata("srcloc", MDT);
  4754. }
  4755. // 4. Finish the call.
  4756. // If the call doesn't return, finish the basic block and clear the
  4757. // insertion point; this allows the rest of IRGen to discard
  4758. // unreachable code.
  4759. if (CI->doesNotReturn()) {
  4760. if (UnusedReturnSizePtr)
  4761. PopCleanupBlock();
  4762. // Strip away the noreturn attribute to better diagnose unreachable UB.
  4763. if (SanOpts.has(SanitizerKind::Unreachable)) {
  4764. // Also remove from function since CallBase::hasFnAttr additionally checks
  4765. // attributes of the called function.
  4766. if (auto *F = CI->getCalledFunction())
  4767. F->removeFnAttr(llvm::Attribute::NoReturn);
  4768. CI->removeFnAttr(llvm::Attribute::NoReturn);
  4769. // Avoid incompatibility with ASan which relies on the `noreturn`
  4770. // attribute to insert handler calls.
  4771. if (SanOpts.hasOneOf(SanitizerKind::Address |
  4772. SanitizerKind::KernelAddress)) {
  4773. SanitizerScope SanScope(this);
  4774. llvm::IRBuilder<>::InsertPointGuard IPGuard(Builder);
  4775. Builder.SetInsertPoint(CI);
  4776. auto *FnType = llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
  4777. llvm::FunctionCallee Fn =
  4778. CGM.CreateRuntimeFunction(FnType, "__asan_handle_no_return");
  4779. EmitNounwindRuntimeCall(Fn);
  4780. }
  4781. }
  4782. EmitUnreachable(Loc);
  4783. Builder.ClearInsertionPoint();
  4784. // FIXME: For now, emit a dummy basic block because expr emitters in
  4785. // generally are not ready to handle emitting expressions at unreachable
  4786. // points.
  4787. EnsureInsertPoint();
  4788. // Return a reasonable RValue.
  4789. return GetUndefRValue(RetTy);
  4790. }
  4791. // If this is a musttail call, return immediately. We do not branch to the
  4792. // epilogue in this case.
  4793. if (IsMustTail) {
  4794. for (auto it = EHStack.find(CurrentCleanupScopeDepth); it != EHStack.end();
  4795. ++it) {
  4796. EHCleanupScope *Cleanup = dyn_cast<EHCleanupScope>(&*it);
  4797. if (!(Cleanup && Cleanup->getCleanup()->isRedundantBeforeReturn()))
  4798. CGM.ErrorUnsupported(MustTailCall, "tail call skipping over cleanups");
  4799. }
  4800. if (CI->getType()->isVoidTy())
  4801. Builder.CreateRetVoid();
  4802. else
  4803. Builder.CreateRet(CI);
  4804. Builder.ClearInsertionPoint();
  4805. EnsureInsertPoint();
  4806. return GetUndefRValue(RetTy);
  4807. }
  4808. // Perform the swifterror writeback.
  4809. if (swiftErrorTemp.isValid()) {
  4810. llvm::Value *errorResult = Builder.CreateLoad(swiftErrorTemp);
  4811. Builder.CreateStore(errorResult, swiftErrorArg);
  4812. }
  4813. // Emit any call-associated writebacks immediately. Arguably this
  4814. // should happen after any return-value munging.
  4815. if (CallArgs.hasWritebacks())
  4816. emitWritebacks(*this, CallArgs);
  4817. // The stack cleanup for inalloca arguments has to run out of the normal
  4818. // lexical order, so deactivate it and run it manually here.
  4819. CallArgs.freeArgumentMemory(*this);
  4820. // Extract the return value.
  4821. RValue Ret = [&] {
  4822. switch (RetAI.getKind()) {
  4823. case ABIArgInfo::CoerceAndExpand: {
  4824. auto coercionType = RetAI.getCoerceAndExpandType();
  4825. Address addr = SRetPtr;
  4826. addr = Builder.CreateElementBitCast(addr, coercionType);
  4827. assert(CI->getType() == RetAI.getUnpaddedCoerceAndExpandType());
  4828. bool requiresExtract = isa<llvm::StructType>(CI->getType());
  4829. unsigned unpaddedIndex = 0;
  4830. for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
  4831. llvm::Type *eltType = coercionType->getElementType(i);
  4832. if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue;
  4833. Address eltAddr = Builder.CreateStructGEP(addr, i);
  4834. llvm::Value *elt = CI;
  4835. if (requiresExtract)
  4836. elt = Builder.CreateExtractValue(elt, unpaddedIndex++);
  4837. else
  4838. assert(unpaddedIndex == 0);
  4839. Builder.CreateStore(elt, eltAddr);
  4840. }
  4841. // FALLTHROUGH
  4842. [[fallthrough]];
  4843. }
  4844. case ABIArgInfo::InAlloca:
  4845. case ABIArgInfo::Indirect: {
  4846. RValue ret = convertTempToRValue(SRetPtr, RetTy, SourceLocation());
  4847. if (UnusedReturnSizePtr)
  4848. PopCleanupBlock();
  4849. return ret;
  4850. }
  4851. case ABIArgInfo::Ignore:
  4852. // If we are ignoring an argument that had a result, make sure to
  4853. // construct the appropriate return value for our caller.
  4854. return GetUndefRValue(RetTy);
  4855. case ABIArgInfo::Extend:
  4856. case ABIArgInfo::Direct: {
  4857. llvm::Type *RetIRTy = ConvertType(RetTy);
  4858. if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) {
  4859. switch (getEvaluationKind(RetTy)) {
  4860. case TEK_Complex: {
  4861. llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
  4862. llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
  4863. return RValue::getComplex(std::make_pair(Real, Imag));
  4864. }
  4865. case TEK_Aggregate: {
  4866. Address DestPtr = ReturnValue.getValue();
  4867. bool DestIsVolatile = ReturnValue.isVolatile();
  4868. if (!DestPtr.isValid()) {
  4869. DestPtr = CreateMemTemp(RetTy, "agg.tmp");
  4870. DestIsVolatile = false;
  4871. }
  4872. EmitAggregateStore(CI, DestPtr, DestIsVolatile);
  4873. return RValue::getAggregate(DestPtr);
  4874. }
  4875. case TEK_Scalar: {
  4876. // If the argument doesn't match, perform a bitcast to coerce it. This
  4877. // can happen due to trivial type mismatches.
  4878. llvm::Value *V = CI;
  4879. if (V->getType() != RetIRTy)
  4880. V = Builder.CreateBitCast(V, RetIRTy);
  4881. return RValue::get(V);
  4882. }
  4883. }
  4884. llvm_unreachable("bad evaluation kind");
  4885. }
  4886. Address DestPtr = ReturnValue.getValue();
  4887. bool DestIsVolatile = ReturnValue.isVolatile();
  4888. if (!DestPtr.isValid()) {
  4889. DestPtr = CreateMemTemp(RetTy, "coerce");
  4890. DestIsVolatile = false;
  4891. }
  4892. // If the value is offset in memory, apply the offset now.
  4893. Address StorePtr = emitAddressAtOffset(*this, DestPtr, RetAI);
  4894. CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
  4895. return convertTempToRValue(DestPtr, RetTy, SourceLocation());
  4896. }
  4897. case ABIArgInfo::Expand:
  4898. case ABIArgInfo::IndirectAliased:
  4899. llvm_unreachable("Invalid ABI kind for return argument");
  4900. }
  4901. llvm_unreachable("Unhandled ABIArgInfo::Kind");
  4902. } ();
  4903. // Emit the assume_aligned check on the return value.
  4904. if (Ret.isScalar() && TargetDecl) {
  4905. AssumeAlignedAttrEmitter.EmitAsAnAssumption(Loc, RetTy, Ret);
  4906. AllocAlignAttrEmitter.EmitAsAnAssumption(Loc, RetTy, Ret);
  4907. }
  4908. // Explicitly call CallLifetimeEnd::Emit just to re-use the code even though
  4909. // we can't use the full cleanup mechanism.
  4910. for (CallLifetimeEnd &LifetimeEnd : CallLifetimeEndAfterCall)
  4911. LifetimeEnd.Emit(*this, /*Flags=*/{});
  4912. if (!ReturnValue.isExternallyDestructed() &&
  4913. RetTy.isDestructedType() == QualType::DK_nontrivial_c_struct)
  4914. pushDestroy(QualType::DK_nontrivial_c_struct, Ret.getAggregateAddress(),
  4915. RetTy);
  4916. return Ret;
  4917. }
  4918. CGCallee CGCallee::prepareConcreteCallee(CodeGenFunction &CGF) const {
  4919. if (isVirtual()) {
  4920. const CallExpr *CE = getVirtualCallExpr();
  4921. return CGF.CGM.getCXXABI().getVirtualFunctionPointer(
  4922. CGF, getVirtualMethodDecl(), getThisAddress(), getVirtualFunctionType(),
  4923. CE ? CE->getBeginLoc() : SourceLocation());
  4924. }
  4925. return *this;
  4926. }
  4927. /* VarArg handling */
  4928. Address CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address &VAListAddr) {
  4929. VAListAddr = VE->isMicrosoftABI()
  4930. ? EmitMSVAListRef(VE->getSubExpr())
  4931. : EmitVAListRef(VE->getSubExpr());
  4932. QualType Ty = VE->getType();
  4933. if (VE->isMicrosoftABI())
  4934. return CGM.getTypes().getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty);
  4935. return CGM.getTypes().getABIInfo().EmitVAArg(*this, VAListAddr, Ty);
  4936. }