TargetInfo.cpp 360 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978
  1. //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
  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 "TargetInfo.h"
  14. #include "ABIInfo.h"
  15. #include "CGBlocks.h"
  16. #include "CGCXXABI.h"
  17. #include "CGValue.h"
  18. #include "CodeGenFunction.h"
  19. #include "clang/AST/RecordLayout.h"
  20. #include "clang/Basic/CodeGenOptions.h"
  21. #include "clang/CodeGen/CGFunctionInfo.h"
  22. #include "clang/CodeGen/SwiftCallingConv.h"
  23. #include "llvm/ADT/StringExtras.h"
  24. #include "llvm/ADT/StringSwitch.h"
  25. #include "llvm/ADT/Triple.h"
  26. #include "llvm/ADT/Twine.h"
  27. #include "llvm/IR/DataLayout.h"
  28. #include "llvm/IR/Type.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. #include <algorithm> // std::sort
  31. using namespace clang;
  32. using namespace CodeGen;
  33. // Helper for coercing an aggregate argument or return value into an integer
  34. // array of the same size (including padding) and alignment. This alternate
  35. // coercion happens only for the RenderScript ABI and can be removed after
  36. // runtimes that rely on it are no longer supported.
  37. //
  38. // RenderScript assumes that the size of the argument / return value in the IR
  39. // is the same as the size of the corresponding qualified type. This helper
  40. // coerces the aggregate type into an array of the same size (including
  41. // padding). This coercion is used in lieu of expansion of struct members or
  42. // other canonical coercions that return a coerced-type of larger size.
  43. //
  44. // Ty - The argument / return value type
  45. // Context - The associated ASTContext
  46. // LLVMContext - The associated LLVMContext
  47. static ABIArgInfo coerceToIntArray(QualType Ty,
  48. ASTContext &Context,
  49. llvm::LLVMContext &LLVMContext) {
  50. // Alignment and Size are measured in bits.
  51. const uint64_t Size = Context.getTypeSize(Ty);
  52. const uint64_t Alignment = Context.getTypeAlign(Ty);
  53. llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
  54. const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
  55. return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
  56. }
  57. static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
  58. llvm::Value *Array,
  59. llvm::Value *Value,
  60. unsigned FirstIndex,
  61. unsigned LastIndex) {
  62. // Alternatively, we could emit this as a loop in the source.
  63. for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
  64. llvm::Value *Cell =
  65. Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
  66. Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
  67. }
  68. }
  69. static bool isAggregateTypeForABI(QualType T) {
  70. return !CodeGenFunction::hasScalarEvaluationKind(T) ||
  71. T->isMemberFunctionPointerType();
  72. }
  73. ABIArgInfo
  74. ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign,
  75. llvm::Type *Padding) const {
  76. return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
  77. ByRef, Realign, Padding);
  78. }
  79. ABIArgInfo
  80. ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
  81. return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
  82. /*ByRef*/ false, Realign);
  83. }
  84. Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  85. QualType Ty) const {
  86. return Address::invalid();
  87. }
  88. ABIInfo::~ABIInfo() {}
  89. /// Does the given lowering require more than the given number of
  90. /// registers when expanded?
  91. ///
  92. /// This is intended to be the basis of a reasonable basic implementation
  93. /// of should{Pass,Return}IndirectlyForSwift.
  94. ///
  95. /// For most targets, a limit of four total registers is reasonable; this
  96. /// limits the amount of code required in order to move around the value
  97. /// in case it wasn't produced immediately prior to the call by the caller
  98. /// (or wasn't produced in exactly the right registers) or isn't used
  99. /// immediately within the callee. But some targets may need to further
  100. /// limit the register count due to an inability to support that many
  101. /// return registers.
  102. static bool occupiesMoreThan(CodeGenTypes &cgt,
  103. ArrayRef<llvm::Type*> scalarTypes,
  104. unsigned maxAllRegisters) {
  105. unsigned intCount = 0, fpCount = 0;
  106. for (llvm::Type *type : scalarTypes) {
  107. if (type->isPointerTy()) {
  108. intCount++;
  109. } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
  110. auto ptrWidth = cgt.getTarget().getPointerWidth(0);
  111. intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
  112. } else {
  113. assert(type->isVectorTy() || type->isFloatingPointTy());
  114. fpCount++;
  115. }
  116. }
  117. return (intCount + fpCount > maxAllRegisters);
  118. }
  119. bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
  120. llvm::Type *eltTy,
  121. unsigned numElts) const {
  122. // The default implementation of this assumes that the target guarantees
  123. // 128-bit SIMD support but nothing more.
  124. return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16);
  125. }
  126. static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
  127. CGCXXABI &CXXABI) {
  128. const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
  129. if (!RD) {
  130. if (!RT->getDecl()->canPassInRegisters())
  131. return CGCXXABI::RAA_Indirect;
  132. return CGCXXABI::RAA_Default;
  133. }
  134. return CXXABI.getRecordArgABI(RD);
  135. }
  136. static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
  137. CGCXXABI &CXXABI) {
  138. const RecordType *RT = T->getAs<RecordType>();
  139. if (!RT)
  140. return CGCXXABI::RAA_Default;
  141. return getRecordArgABI(RT, CXXABI);
  142. }
  143. static bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI,
  144. const ABIInfo &Info) {
  145. QualType Ty = FI.getReturnType();
  146. if (const auto *RT = Ty->getAs<RecordType>())
  147. if (!isa<CXXRecordDecl>(RT->getDecl()) &&
  148. !RT->getDecl()->canPassInRegisters()) {
  149. FI.getReturnInfo() = Info.getNaturalAlignIndirect(Ty);
  150. return true;
  151. }
  152. return CXXABI.classifyReturnType(FI);
  153. }
  154. /// Pass transparent unions as if they were the type of the first element. Sema
  155. /// should ensure that all elements of the union have the same "machine type".
  156. static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
  157. if (const RecordType *UT = Ty->getAsUnionType()) {
  158. const RecordDecl *UD = UT->getDecl();
  159. if (UD->hasAttr<TransparentUnionAttr>()) {
  160. assert(!UD->field_empty() && "sema created an empty transparent union");
  161. return UD->field_begin()->getType();
  162. }
  163. }
  164. return Ty;
  165. }
  166. CGCXXABI &ABIInfo::getCXXABI() const {
  167. return CGT.getCXXABI();
  168. }
  169. ASTContext &ABIInfo::getContext() const {
  170. return CGT.getContext();
  171. }
  172. llvm::LLVMContext &ABIInfo::getVMContext() const {
  173. return CGT.getLLVMContext();
  174. }
  175. const llvm::DataLayout &ABIInfo::getDataLayout() const {
  176. return CGT.getDataLayout();
  177. }
  178. const TargetInfo &ABIInfo::getTarget() const {
  179. return CGT.getTarget();
  180. }
  181. const CodeGenOptions &ABIInfo::getCodeGenOpts() const {
  182. return CGT.getCodeGenOpts();
  183. }
  184. bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); }
  185. bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  186. return false;
  187. }
  188. bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
  189. uint64_t Members) const {
  190. return false;
  191. }
  192. LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
  193. raw_ostream &OS = llvm::errs();
  194. OS << "(ABIArgInfo Kind=";
  195. switch (TheKind) {
  196. case Direct:
  197. OS << "Direct Type=";
  198. if (llvm::Type *Ty = getCoerceToType())
  199. Ty->print(OS);
  200. else
  201. OS << "null";
  202. break;
  203. case Extend:
  204. OS << "Extend";
  205. break;
  206. case Ignore:
  207. OS << "Ignore";
  208. break;
  209. case InAlloca:
  210. OS << "InAlloca Offset=" << getInAllocaFieldIndex();
  211. break;
  212. case Indirect:
  213. OS << "Indirect Align=" << getIndirectAlign().getQuantity()
  214. << " ByVal=" << getIndirectByVal()
  215. << " Realign=" << getIndirectRealign();
  216. break;
  217. case Expand:
  218. OS << "Expand";
  219. break;
  220. case CoerceAndExpand:
  221. OS << "CoerceAndExpand Type=";
  222. getCoerceAndExpandType()->print(OS);
  223. break;
  224. }
  225. OS << ")\n";
  226. }
  227. // Dynamically round a pointer up to a multiple of the given alignment.
  228. static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
  229. llvm::Value *Ptr,
  230. CharUnits Align) {
  231. llvm::Value *PtrAsInt = Ptr;
  232. // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
  233. PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
  234. PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
  235. llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
  236. PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
  237. llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
  238. PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
  239. Ptr->getType(),
  240. Ptr->getName() + ".aligned");
  241. return PtrAsInt;
  242. }
  243. /// Emit va_arg for a platform using the common void* representation,
  244. /// where arguments are simply emitted in an array of slots on the stack.
  245. ///
  246. /// This version implements the core direct-value passing rules.
  247. ///
  248. /// \param SlotSize - The size and alignment of a stack slot.
  249. /// Each argument will be allocated to a multiple of this number of
  250. /// slots, and all the slots will be aligned to this value.
  251. /// \param AllowHigherAlign - The slot alignment is not a cap;
  252. /// an argument type with an alignment greater than the slot size
  253. /// will be emitted on a higher-alignment address, potentially
  254. /// leaving one or more empty slots behind as padding. If this
  255. /// is false, the returned address might be less-aligned than
  256. /// DirectAlign.
  257. static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
  258. Address VAListAddr,
  259. llvm::Type *DirectTy,
  260. CharUnits DirectSize,
  261. CharUnits DirectAlign,
  262. CharUnits SlotSize,
  263. bool AllowHigherAlign) {
  264. // Cast the element type to i8* if necessary. Some platforms define
  265. // va_list as a struct containing an i8* instead of just an i8*.
  266. if (VAListAddr.getElementType() != CGF.Int8PtrTy)
  267. VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
  268. llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
  269. // If the CC aligns values higher than the slot size, do so if needed.
  270. Address Addr = Address::invalid();
  271. if (AllowHigherAlign && DirectAlign > SlotSize) {
  272. Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
  273. DirectAlign);
  274. } else {
  275. Addr = Address(Ptr, SlotSize);
  276. }
  277. // Advance the pointer past the argument, then store that back.
  278. CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
  279. Address NextPtr =
  280. CGF.Builder.CreateConstInBoundsByteGEP(Addr, FullDirectSize, "argp.next");
  281. CGF.Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
  282. // If the argument is smaller than a slot, and this is a big-endian
  283. // target, the argument will be right-adjusted in its slot.
  284. if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
  285. !DirectTy->isStructTy()) {
  286. Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
  287. }
  288. Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
  289. return Addr;
  290. }
  291. /// Emit va_arg for a platform using the common void* representation,
  292. /// where arguments are simply emitted in an array of slots on the stack.
  293. ///
  294. /// \param IsIndirect - Values of this type are passed indirectly.
  295. /// \param ValueInfo - The size and alignment of this type, generally
  296. /// computed with getContext().getTypeInfoInChars(ValueTy).
  297. /// \param SlotSizeAndAlign - The size and alignment of a stack slot.
  298. /// Each argument will be allocated to a multiple of this number of
  299. /// slots, and all the slots will be aligned to this value.
  300. /// \param AllowHigherAlign - The slot alignment is not a cap;
  301. /// an argument type with an alignment greater than the slot size
  302. /// will be emitted on a higher-alignment address, potentially
  303. /// leaving one or more empty slots behind as padding.
  304. static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
  305. QualType ValueTy, bool IsIndirect,
  306. std::pair<CharUnits, CharUnits> ValueInfo,
  307. CharUnits SlotSizeAndAlign,
  308. bool AllowHigherAlign) {
  309. // The size and alignment of the value that was passed directly.
  310. CharUnits DirectSize, DirectAlign;
  311. if (IsIndirect) {
  312. DirectSize = CGF.getPointerSize();
  313. DirectAlign = CGF.getPointerAlign();
  314. } else {
  315. DirectSize = ValueInfo.first;
  316. DirectAlign = ValueInfo.second;
  317. }
  318. // Cast the address we've calculated to the right type.
  319. llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy);
  320. if (IsIndirect)
  321. DirectTy = DirectTy->getPointerTo(0);
  322. Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy,
  323. DirectSize, DirectAlign,
  324. SlotSizeAndAlign,
  325. AllowHigherAlign);
  326. if (IsIndirect) {
  327. Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second);
  328. }
  329. return Addr;
  330. }
  331. static Address emitMergePHI(CodeGenFunction &CGF,
  332. Address Addr1, llvm::BasicBlock *Block1,
  333. Address Addr2, llvm::BasicBlock *Block2,
  334. const llvm::Twine &Name = "") {
  335. assert(Addr1.getType() == Addr2.getType());
  336. llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
  337. PHI->addIncoming(Addr1.getPointer(), Block1);
  338. PHI->addIncoming(Addr2.getPointer(), Block2);
  339. CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
  340. return Address(PHI, Align);
  341. }
  342. TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
  343. // If someone can figure out a general rule for this, that would be great.
  344. // It's probably just doomed to be platform-dependent, though.
  345. unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
  346. // Verified for:
  347. // x86-64 FreeBSD, Linux, Darwin
  348. // x86-32 FreeBSD, Linux, Darwin
  349. // PowerPC Linux, Darwin
  350. // ARM Darwin (*not* EABI)
  351. // AArch64 Linux
  352. return 32;
  353. }
  354. bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
  355. const FunctionNoProtoType *fnType) const {
  356. // The following conventions are known to require this to be false:
  357. // x86_stdcall
  358. // MIPS
  359. // For everything else, we just prefer false unless we opt out.
  360. return false;
  361. }
  362. void
  363. TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
  364. llvm::SmallString<24> &Opt) const {
  365. // This assumes the user is passing a library name like "rt" instead of a
  366. // filename like "librt.a/so", and that they don't care whether it's static or
  367. // dynamic.
  368. Opt = "-l";
  369. Opt += Lib;
  370. }
  371. unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
  372. // OpenCL kernels are called via an explicit runtime API with arguments
  373. // set with clSetKernelArg(), not as normal sub-functions.
  374. // Return SPIR_KERNEL by default as the kernel calling convention to
  375. // ensure the fingerprint is fixed such way that each OpenCL argument
  376. // gets one matching argument in the produced kernel function argument
  377. // list to enable feasible implementation of clSetKernelArg() with
  378. // aggregates etc. In case we would use the default C calling conv here,
  379. // clSetKernelArg() might break depending on the target-specific
  380. // conventions; different targets might split structs passed as values
  381. // to multiple function arguments etc.
  382. return llvm::CallingConv::SPIR_KERNEL;
  383. }
  384. llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
  385. llvm::PointerType *T, QualType QT) const {
  386. return llvm::ConstantPointerNull::get(T);
  387. }
  388. LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
  389. const VarDecl *D) const {
  390. assert(!CGM.getLangOpts().OpenCL &&
  391. !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
  392. "Address space agnostic languages only");
  393. return D ? D->getType().getAddressSpace() : LangAS::Default;
  394. }
  395. llvm::Value *TargetCodeGenInfo::performAddrSpaceCast(
  396. CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr,
  397. LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const {
  398. // Since target may map different address spaces in AST to the same address
  399. // space, an address space conversion may end up as a bitcast.
  400. if (auto *C = dyn_cast<llvm::Constant>(Src))
  401. return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy);
  402. // Try to preserve the source's name to make IR more readable.
  403. return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
  404. Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : "");
  405. }
  406. llvm::Constant *
  407. TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src,
  408. LangAS SrcAddr, LangAS DestAddr,
  409. llvm::Type *DestTy) const {
  410. // Since target may map different address spaces in AST to the same address
  411. // space, an address space conversion may end up as a bitcast.
  412. return llvm::ConstantExpr::getPointerCast(Src, DestTy);
  413. }
  414. llvm::SyncScope::ID
  415. TargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
  416. SyncScope Scope,
  417. llvm::AtomicOrdering Ordering,
  418. llvm::LLVMContext &Ctx) const {
  419. return Ctx.getOrInsertSyncScopeID(""); /* default sync scope */
  420. }
  421. static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
  422. /// isEmptyField - Return true iff a the field is "empty", that is it
  423. /// is an unnamed bit-field or an (array of) empty record(s).
  424. static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
  425. bool AllowArrays) {
  426. if (FD->isUnnamedBitfield())
  427. return true;
  428. QualType FT = FD->getType();
  429. // Constant arrays of empty records count as empty, strip them off.
  430. // Constant arrays of zero length always count as empty.
  431. if (AllowArrays)
  432. while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
  433. if (AT->getSize() == 0)
  434. return true;
  435. FT = AT->getElementType();
  436. }
  437. const RecordType *RT = FT->getAs<RecordType>();
  438. if (!RT)
  439. return false;
  440. // C++ record fields are never empty, at least in the Itanium ABI.
  441. //
  442. // FIXME: We should use a predicate for whether this behavior is true in the
  443. // current ABI.
  444. if (isa<CXXRecordDecl>(RT->getDecl()))
  445. return false;
  446. return isEmptyRecord(Context, FT, AllowArrays);
  447. }
  448. /// isEmptyRecord - Return true iff a structure contains only empty
  449. /// fields. Note that a structure with a flexible array member is not
  450. /// considered empty.
  451. static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
  452. const RecordType *RT = T->getAs<RecordType>();
  453. if (!RT)
  454. return false;
  455. const RecordDecl *RD = RT->getDecl();
  456. if (RD->hasFlexibleArrayMember())
  457. return false;
  458. // If this is a C++ record, check the bases first.
  459. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  460. for (const auto &I : CXXRD->bases())
  461. if (!isEmptyRecord(Context, I.getType(), true))
  462. return false;
  463. for (const auto *I : RD->fields())
  464. if (!isEmptyField(Context, I, AllowArrays))
  465. return false;
  466. return true;
  467. }
  468. /// isSingleElementStruct - Determine if a structure is a "single
  469. /// element struct", i.e. it has exactly one non-empty field or
  470. /// exactly one field which is itself a single element
  471. /// struct. Structures with flexible array members are never
  472. /// considered single element structs.
  473. ///
  474. /// \return The field declaration for the single non-empty field, if
  475. /// it exists.
  476. static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
  477. const RecordType *RT = T->getAs<RecordType>();
  478. if (!RT)
  479. return nullptr;
  480. const RecordDecl *RD = RT->getDecl();
  481. if (RD->hasFlexibleArrayMember())
  482. return nullptr;
  483. const Type *Found = nullptr;
  484. // If this is a C++ record, check the bases first.
  485. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  486. for (const auto &I : CXXRD->bases()) {
  487. // Ignore empty records.
  488. if (isEmptyRecord(Context, I.getType(), true))
  489. continue;
  490. // If we already found an element then this isn't a single-element struct.
  491. if (Found)
  492. return nullptr;
  493. // If this is non-empty and not a single element struct, the composite
  494. // cannot be a single element struct.
  495. Found = isSingleElementStruct(I.getType(), Context);
  496. if (!Found)
  497. return nullptr;
  498. }
  499. }
  500. // Check for single element.
  501. for (const auto *FD : RD->fields()) {
  502. QualType FT = FD->getType();
  503. // Ignore empty fields.
  504. if (isEmptyField(Context, FD, true))
  505. continue;
  506. // If we already found an element then this isn't a single-element
  507. // struct.
  508. if (Found)
  509. return nullptr;
  510. // Treat single element arrays as the element.
  511. while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
  512. if (AT->getSize().getZExtValue() != 1)
  513. break;
  514. FT = AT->getElementType();
  515. }
  516. if (!isAggregateTypeForABI(FT)) {
  517. Found = FT.getTypePtr();
  518. } else {
  519. Found = isSingleElementStruct(FT, Context);
  520. if (!Found)
  521. return nullptr;
  522. }
  523. }
  524. // We don't consider a struct a single-element struct if it has
  525. // padding beyond the element type.
  526. if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
  527. return nullptr;
  528. return Found;
  529. }
  530. namespace {
  531. Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
  532. const ABIArgInfo &AI) {
  533. // This default implementation defers to the llvm backend's va_arg
  534. // instruction. It can handle only passing arguments directly
  535. // (typically only handled in the backend for primitive types), or
  536. // aggregates passed indirectly by pointer (NOTE: if the "byval"
  537. // flag has ABI impact in the callee, this implementation cannot
  538. // work.)
  539. // Only a few cases are covered here at the moment -- those needed
  540. // by the default abi.
  541. llvm::Value *Val;
  542. if (AI.isIndirect()) {
  543. assert(!AI.getPaddingType() &&
  544. "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
  545. assert(
  546. !AI.getIndirectRealign() &&
  547. "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
  548. auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
  549. CharUnits TyAlignForABI = TyInfo.second;
  550. llvm::Type *BaseTy =
  551. llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
  552. llvm::Value *Addr =
  553. CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
  554. return Address(Addr, TyAlignForABI);
  555. } else {
  556. assert((AI.isDirect() || AI.isExtend()) &&
  557. "Unexpected ArgInfo Kind in generic VAArg emitter!");
  558. assert(!AI.getInReg() &&
  559. "Unexpected InReg seen in arginfo in generic VAArg emitter!");
  560. assert(!AI.getPaddingType() &&
  561. "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
  562. assert(!AI.getDirectOffset() &&
  563. "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
  564. assert(!AI.getCoerceToType() &&
  565. "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
  566. Address Temp = CGF.CreateMemTemp(Ty, "varet");
  567. Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty));
  568. CGF.Builder.CreateStore(Val, Temp);
  569. return Temp;
  570. }
  571. }
  572. /// DefaultABIInfo - The default implementation for ABI specific
  573. /// details. This implementation provides information which results in
  574. /// self-consistent and sensible LLVM IR generation, but does not
  575. /// conform to any particular ABI.
  576. class DefaultABIInfo : public ABIInfo {
  577. public:
  578. DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
  579. ABIArgInfo classifyReturnType(QualType RetTy) const;
  580. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  581. void computeInfo(CGFunctionInfo &FI) const override {
  582. if (!getCXXABI().classifyReturnType(FI))
  583. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  584. for (auto &I : FI.arguments())
  585. I.info = classifyArgumentType(I.type);
  586. }
  587. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  588. QualType Ty) const override {
  589. return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
  590. }
  591. };
  592. class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
  593. public:
  594. DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  595. : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
  596. };
  597. ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
  598. Ty = useFirstFieldIfTransparentUnion(Ty);
  599. if (isAggregateTypeForABI(Ty)) {
  600. // Records with non-trivial destructors/copy-constructors should not be
  601. // passed by value.
  602. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  603. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  604. return getNaturalAlignIndirect(Ty);
  605. }
  606. // Treat an enum type as its underlying type.
  607. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  608. Ty = EnumTy->getDecl()->getIntegerType();
  609. return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
  610. : ABIArgInfo::getDirect());
  611. }
  612. ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
  613. if (RetTy->isVoidType())
  614. return ABIArgInfo::getIgnore();
  615. if (isAggregateTypeForABI(RetTy))
  616. return getNaturalAlignIndirect(RetTy);
  617. // Treat an enum type as its underlying type.
  618. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  619. RetTy = EnumTy->getDecl()->getIntegerType();
  620. return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
  621. : ABIArgInfo::getDirect());
  622. }
  623. //===----------------------------------------------------------------------===//
  624. // WebAssembly ABI Implementation
  625. //
  626. // This is a very simple ABI that relies a lot on DefaultABIInfo.
  627. //===----------------------------------------------------------------------===//
  628. class WebAssemblyABIInfo final : public SwiftABIInfo {
  629. DefaultABIInfo defaultInfo;
  630. public:
  631. explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT)
  632. : SwiftABIInfo(CGT), defaultInfo(CGT) {}
  633. private:
  634. ABIArgInfo classifyReturnType(QualType RetTy) const;
  635. ABIArgInfo classifyArgumentType(QualType Ty) const;
  636. // DefaultABIInfo's classifyReturnType and classifyArgumentType are
  637. // non-virtual, but computeInfo and EmitVAArg are virtual, so we
  638. // overload them.
  639. void computeInfo(CGFunctionInfo &FI) const override {
  640. if (!getCXXABI().classifyReturnType(FI))
  641. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  642. for (auto &Arg : FI.arguments())
  643. Arg.info = classifyArgumentType(Arg.type);
  644. }
  645. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  646. QualType Ty) const override;
  647. bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
  648. bool asReturnValue) const override {
  649. return occupiesMoreThan(CGT, scalars, /*total*/ 4);
  650. }
  651. bool isSwiftErrorInRegister() const override {
  652. return false;
  653. }
  654. };
  655. class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
  656. public:
  657. explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  658. : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {}
  659. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  660. CodeGen::CodeGenModule &CGM) const override {
  661. TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
  662. if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  663. if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) {
  664. llvm::Function *Fn = cast<llvm::Function>(GV);
  665. llvm::AttrBuilder B;
  666. B.addAttribute("wasm-import-module", Attr->getImportModule());
  667. Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
  668. }
  669. if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) {
  670. llvm::Function *Fn = cast<llvm::Function>(GV);
  671. llvm::AttrBuilder B;
  672. B.addAttribute("wasm-import-name", Attr->getImportName());
  673. Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
  674. }
  675. }
  676. if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  677. llvm::Function *Fn = cast<llvm::Function>(GV);
  678. if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype())
  679. Fn->addFnAttr("no-prototype");
  680. }
  681. }
  682. };
  683. /// Classify argument of given type \p Ty.
  684. ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
  685. Ty = useFirstFieldIfTransparentUnion(Ty);
  686. if (isAggregateTypeForABI(Ty)) {
  687. // Records with non-trivial destructors/copy-constructors should not be
  688. // passed by value.
  689. if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
  690. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  691. // Ignore empty structs/unions.
  692. if (isEmptyRecord(getContext(), Ty, true))
  693. return ABIArgInfo::getIgnore();
  694. // Lower single-element structs to just pass a regular value. TODO: We
  695. // could do reasonable-size multiple-element structs too, using getExpand(),
  696. // though watch out for things like bitfields.
  697. if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
  698. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  699. }
  700. // Otherwise just do the default thing.
  701. return defaultInfo.classifyArgumentType(Ty);
  702. }
  703. ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
  704. if (isAggregateTypeForABI(RetTy)) {
  705. // Records with non-trivial destructors/copy-constructors should not be
  706. // returned by value.
  707. if (!getRecordArgABI(RetTy, getCXXABI())) {
  708. // Ignore empty structs/unions.
  709. if (isEmptyRecord(getContext(), RetTy, true))
  710. return ABIArgInfo::getIgnore();
  711. // Lower single-element structs to just return a regular value. TODO: We
  712. // could do reasonable-size multiple-element structs too, using
  713. // ABIArgInfo::getDirect().
  714. if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
  715. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  716. }
  717. }
  718. // Otherwise just do the default thing.
  719. return defaultInfo.classifyReturnType(RetTy);
  720. }
  721. Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  722. QualType Ty) const {
  723. bool IsIndirect = isAggregateTypeForABI(Ty) &&
  724. !isEmptyRecord(getContext(), Ty, true) &&
  725. !isSingleElementStruct(Ty, getContext());
  726. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
  727. getContext().getTypeInfoInChars(Ty),
  728. CharUnits::fromQuantity(4),
  729. /*AllowHigherAlign=*/true);
  730. }
  731. //===----------------------------------------------------------------------===//
  732. // le32/PNaCl bitcode ABI Implementation
  733. //
  734. // This is a simplified version of the x86_32 ABI. Arguments and return values
  735. // are always passed on the stack.
  736. //===----------------------------------------------------------------------===//
  737. class PNaClABIInfo : public ABIInfo {
  738. public:
  739. PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
  740. ABIArgInfo classifyReturnType(QualType RetTy) const;
  741. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  742. void computeInfo(CGFunctionInfo &FI) const override;
  743. Address EmitVAArg(CodeGenFunction &CGF,
  744. Address VAListAddr, QualType Ty) const override;
  745. };
  746. class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
  747. public:
  748. PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  749. : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
  750. };
  751. void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
  752. if (!getCXXABI().classifyReturnType(FI))
  753. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  754. for (auto &I : FI.arguments())
  755. I.info = classifyArgumentType(I.type);
  756. }
  757. Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  758. QualType Ty) const {
  759. // The PNaCL ABI is a bit odd, in that varargs don't use normal
  760. // function classification. Structs get passed directly for varargs
  761. // functions, through a rewriting transform in
  762. // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
  763. // this target to actually support a va_arg instructions with an
  764. // aggregate type, unlike other targets.
  765. return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
  766. }
  767. /// Classify argument of given type \p Ty.
  768. ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
  769. if (isAggregateTypeForABI(Ty)) {
  770. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  771. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  772. return getNaturalAlignIndirect(Ty);
  773. } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
  774. // Treat an enum type as its underlying type.
  775. Ty = EnumTy->getDecl()->getIntegerType();
  776. } else if (Ty->isFloatingType()) {
  777. // Floating-point types don't go inreg.
  778. return ABIArgInfo::getDirect();
  779. }
  780. return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
  781. : ABIArgInfo::getDirect());
  782. }
  783. ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
  784. if (RetTy->isVoidType())
  785. return ABIArgInfo::getIgnore();
  786. // In the PNaCl ABI we always return records/structures on the stack.
  787. if (isAggregateTypeForABI(RetTy))
  788. return getNaturalAlignIndirect(RetTy);
  789. // Treat an enum type as its underlying type.
  790. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  791. RetTy = EnumTy->getDecl()->getIntegerType();
  792. return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
  793. : ABIArgInfo::getDirect());
  794. }
  795. /// IsX86_MMXType - Return true if this is an MMX type.
  796. bool IsX86_MMXType(llvm::Type *IRType) {
  797. // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
  798. return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
  799. cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
  800. IRType->getScalarSizeInBits() != 64;
  801. }
  802. static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  803. StringRef Constraint,
  804. llvm::Type* Ty) {
  805. bool IsMMXCons = llvm::StringSwitch<bool>(Constraint)
  806. .Cases("y", "&y", "^Ym", true)
  807. .Default(false);
  808. if (IsMMXCons && Ty->isVectorTy()) {
  809. if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
  810. // Invalid MMX constraint
  811. return nullptr;
  812. }
  813. return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
  814. }
  815. // No operation needed
  816. return Ty;
  817. }
  818. /// Returns true if this type can be passed in SSE registers with the
  819. /// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
  820. static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
  821. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  822. if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) {
  823. if (BT->getKind() == BuiltinType::LongDouble) {
  824. if (&Context.getTargetInfo().getLongDoubleFormat() ==
  825. &llvm::APFloat::x87DoubleExtended())
  826. return false;
  827. }
  828. return true;
  829. }
  830. } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
  831. // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
  832. // registers specially.
  833. unsigned VecSize = Context.getTypeSize(VT);
  834. if (VecSize == 128 || VecSize == 256 || VecSize == 512)
  835. return true;
  836. }
  837. return false;
  838. }
  839. /// Returns true if this aggregate is small enough to be passed in SSE registers
  840. /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
  841. static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
  842. return NumMembers <= 4;
  843. }
  844. /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86.
  845. static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) {
  846. auto AI = ABIArgInfo::getDirect(T);
  847. AI.setInReg(true);
  848. AI.setCanBeFlattened(false);
  849. return AI;
  850. }
  851. //===----------------------------------------------------------------------===//
  852. // X86-32 ABI Implementation
  853. //===----------------------------------------------------------------------===//
  854. /// Similar to llvm::CCState, but for Clang.
  855. struct CCState {
  856. CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
  857. unsigned CC;
  858. unsigned FreeRegs;
  859. unsigned FreeSSERegs;
  860. };
  861. enum {
  862. // Vectorcall only allows the first 6 parameters to be passed in registers.
  863. VectorcallMaxParamNumAsReg = 6
  864. };
  865. /// X86_32ABIInfo - The X86-32 ABI information.
  866. class X86_32ABIInfo : public SwiftABIInfo {
  867. enum Class {
  868. Integer,
  869. Float
  870. };
  871. static const unsigned MinABIStackAlignInBytes = 4;
  872. bool IsDarwinVectorABI;
  873. bool IsRetSmallStructInRegABI;
  874. bool IsWin32StructABI;
  875. bool IsSoftFloatABI;
  876. bool IsMCUABI;
  877. unsigned DefaultNumRegisterParameters;
  878. static bool isRegisterSize(unsigned Size) {
  879. return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
  880. }
  881. bool isHomogeneousAggregateBaseType(QualType Ty) const override {
  882. // FIXME: Assumes vectorcall is in use.
  883. return isX86VectorTypeForVectorCall(getContext(), Ty);
  884. }
  885. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  886. uint64_t NumMembers) const override {
  887. // FIXME: Assumes vectorcall is in use.
  888. return isX86VectorCallAggregateSmallEnough(NumMembers);
  889. }
  890. bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
  891. /// getIndirectResult - Give a source type \arg Ty, return a suitable result
  892. /// such that the argument will be passed in memory.
  893. ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
  894. ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
  895. /// Return the alignment to use for the given type on the stack.
  896. unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
  897. Class classify(QualType Ty) const;
  898. ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
  899. ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
  900. /// Updates the number of available free registers, returns
  901. /// true if any registers were allocated.
  902. bool updateFreeRegs(QualType Ty, CCState &State) const;
  903. bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
  904. bool &NeedsPadding) const;
  905. bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
  906. bool canExpandIndirectArgument(QualType Ty) const;
  907. /// Rewrite the function info so that all memory arguments use
  908. /// inalloca.
  909. void rewriteWithInAlloca(CGFunctionInfo &FI) const;
  910. void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
  911. CharUnits &StackOffset, ABIArgInfo &Info,
  912. QualType Type) const;
  913. void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
  914. bool &UsedInAlloca) const;
  915. public:
  916. void computeInfo(CGFunctionInfo &FI) const override;
  917. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  918. QualType Ty) const override;
  919. X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
  920. bool RetSmallStructInRegABI, bool Win32StructABI,
  921. unsigned NumRegisterParameters, bool SoftFloatABI)
  922. : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
  923. IsRetSmallStructInRegABI(RetSmallStructInRegABI),
  924. IsWin32StructABI(Win32StructABI),
  925. IsSoftFloatABI(SoftFloatABI),
  926. IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
  927. DefaultNumRegisterParameters(NumRegisterParameters) {}
  928. bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
  929. bool asReturnValue) const override {
  930. // LLVM's x86-32 lowering currently only assigns up to three
  931. // integer registers and three fp registers. Oddly, it'll use up to
  932. // four vector registers for vectors, but those can overlap with the
  933. // scalar registers.
  934. return occupiesMoreThan(CGT, scalars, /*total*/ 3);
  935. }
  936. bool isSwiftErrorInRegister() const override {
  937. // x86-32 lowering does not support passing swifterror in a register.
  938. return false;
  939. }
  940. };
  941. class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
  942. public:
  943. X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
  944. bool RetSmallStructInRegABI, bool Win32StructABI,
  945. unsigned NumRegisterParameters, bool SoftFloatABI)
  946. : TargetCodeGenInfo(new X86_32ABIInfo(
  947. CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
  948. NumRegisterParameters, SoftFloatABI)) {}
  949. static bool isStructReturnInRegABI(
  950. const llvm::Triple &Triple, const CodeGenOptions &Opts);
  951. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  952. CodeGen::CodeGenModule &CGM) const override;
  953. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
  954. // Darwin uses different dwarf register numbers for EH.
  955. if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
  956. return 4;
  957. }
  958. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  959. llvm::Value *Address) const override;
  960. llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  961. StringRef Constraint,
  962. llvm::Type* Ty) const override {
  963. return X86AdjustInlineAsmType(CGF, Constraint, Ty);
  964. }
  965. void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
  966. std::string &Constraints,
  967. std::vector<llvm::Type *> &ResultRegTypes,
  968. std::vector<llvm::Type *> &ResultTruncRegTypes,
  969. std::vector<LValue> &ResultRegDests,
  970. std::string &AsmString,
  971. unsigned NumOutputs) const override;
  972. llvm::Constant *
  973. getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
  974. unsigned Sig = (0xeb << 0) | // jmp rel8
  975. (0x06 << 8) | // .+0x08
  976. ('v' << 16) |
  977. ('2' << 24);
  978. return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
  979. }
  980. StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
  981. return "movl\t%ebp, %ebp"
  982. "\t\t// marker for objc_retainAutoreleaseReturnValue";
  983. }
  984. };
  985. }
  986. /// Rewrite input constraint references after adding some output constraints.
  987. /// In the case where there is one output and one input and we add one output,
  988. /// we need to replace all operand references greater than or equal to 1:
  989. /// mov $0, $1
  990. /// mov eax, $1
  991. /// The result will be:
  992. /// mov $0, $2
  993. /// mov eax, $2
  994. static void rewriteInputConstraintReferences(unsigned FirstIn,
  995. unsigned NumNewOuts,
  996. std::string &AsmString) {
  997. std::string Buf;
  998. llvm::raw_string_ostream OS(Buf);
  999. size_t Pos = 0;
  1000. while (Pos < AsmString.size()) {
  1001. size_t DollarStart = AsmString.find('$', Pos);
  1002. if (DollarStart == std::string::npos)
  1003. DollarStart = AsmString.size();
  1004. size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
  1005. if (DollarEnd == std::string::npos)
  1006. DollarEnd = AsmString.size();
  1007. OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
  1008. Pos = DollarEnd;
  1009. size_t NumDollars = DollarEnd - DollarStart;
  1010. if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
  1011. // We have an operand reference.
  1012. size_t DigitStart = Pos;
  1013. size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
  1014. if (DigitEnd == std::string::npos)
  1015. DigitEnd = AsmString.size();
  1016. StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
  1017. unsigned OperandIndex;
  1018. if (!OperandStr.getAsInteger(10, OperandIndex)) {
  1019. if (OperandIndex >= FirstIn)
  1020. OperandIndex += NumNewOuts;
  1021. OS << OperandIndex;
  1022. } else {
  1023. OS << OperandStr;
  1024. }
  1025. Pos = DigitEnd;
  1026. }
  1027. }
  1028. AsmString = std::move(OS.str());
  1029. }
  1030. /// Add output constraints for EAX:EDX because they are return registers.
  1031. void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
  1032. CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
  1033. std::vector<llvm::Type *> &ResultRegTypes,
  1034. std::vector<llvm::Type *> &ResultTruncRegTypes,
  1035. std::vector<LValue> &ResultRegDests, std::string &AsmString,
  1036. unsigned NumOutputs) const {
  1037. uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
  1038. // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
  1039. // larger.
  1040. if (!Constraints.empty())
  1041. Constraints += ',';
  1042. if (RetWidth <= 32) {
  1043. Constraints += "={eax}";
  1044. ResultRegTypes.push_back(CGF.Int32Ty);
  1045. } else {
  1046. // Use the 'A' constraint for EAX:EDX.
  1047. Constraints += "=A";
  1048. ResultRegTypes.push_back(CGF.Int64Ty);
  1049. }
  1050. // Truncate EAX or EAX:EDX to an integer of the appropriate size.
  1051. llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
  1052. ResultTruncRegTypes.push_back(CoerceTy);
  1053. // Coerce the integer by bitcasting the return slot pointer.
  1054. ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(),
  1055. CoerceTy->getPointerTo()));
  1056. ResultRegDests.push_back(ReturnSlot);
  1057. rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
  1058. }
  1059. /// shouldReturnTypeInRegister - Determine if the given type should be
  1060. /// returned in a register (for the Darwin and MCU ABI).
  1061. bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
  1062. ASTContext &Context) const {
  1063. uint64_t Size = Context.getTypeSize(Ty);
  1064. // For i386, type must be register sized.
  1065. // For the MCU ABI, it only needs to be <= 8-byte
  1066. if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
  1067. return false;
  1068. if (Ty->isVectorType()) {
  1069. // 64- and 128- bit vectors inside structures are not returned in
  1070. // registers.
  1071. if (Size == 64 || Size == 128)
  1072. return false;
  1073. return true;
  1074. }
  1075. // If this is a builtin, pointer, enum, complex type, member pointer, or
  1076. // member function pointer it is ok.
  1077. if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
  1078. Ty->isAnyComplexType() || Ty->isEnumeralType() ||
  1079. Ty->isBlockPointerType() || Ty->isMemberPointerType())
  1080. return true;
  1081. // Arrays are treated like records.
  1082. if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
  1083. return shouldReturnTypeInRegister(AT->getElementType(), Context);
  1084. // Otherwise, it must be a record type.
  1085. const RecordType *RT = Ty->getAs<RecordType>();
  1086. if (!RT) return false;
  1087. // FIXME: Traverse bases here too.
  1088. // Structure types are passed in register if all fields would be
  1089. // passed in a register.
  1090. for (const auto *FD : RT->getDecl()->fields()) {
  1091. // Empty fields are ignored.
  1092. if (isEmptyField(Context, FD, true))
  1093. continue;
  1094. // Check fields recursively.
  1095. if (!shouldReturnTypeInRegister(FD->getType(), Context))
  1096. return false;
  1097. }
  1098. return true;
  1099. }
  1100. static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
  1101. // Treat complex types as the element type.
  1102. if (const ComplexType *CTy = Ty->getAs<ComplexType>())
  1103. Ty = CTy->getElementType();
  1104. // Check for a type which we know has a simple scalar argument-passing
  1105. // convention without any padding. (We're specifically looking for 32
  1106. // and 64-bit integer and integer-equivalents, float, and double.)
  1107. if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
  1108. !Ty->isEnumeralType() && !Ty->isBlockPointerType())
  1109. return false;
  1110. uint64_t Size = Context.getTypeSize(Ty);
  1111. return Size == 32 || Size == 64;
  1112. }
  1113. static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD,
  1114. uint64_t &Size) {
  1115. for (const auto *FD : RD->fields()) {
  1116. // Scalar arguments on the stack get 4 byte alignment on x86. If the
  1117. // argument is smaller than 32-bits, expanding the struct will create
  1118. // alignment padding.
  1119. if (!is32Or64BitBasicType(FD->getType(), Context))
  1120. return false;
  1121. // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
  1122. // how to expand them yet, and the predicate for telling if a bitfield still
  1123. // counts as "basic" is more complicated than what we were doing previously.
  1124. if (FD->isBitField())
  1125. return false;
  1126. Size += Context.getTypeSize(FD->getType());
  1127. }
  1128. return true;
  1129. }
  1130. static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD,
  1131. uint64_t &Size) {
  1132. // Don't do this if there are any non-empty bases.
  1133. for (const CXXBaseSpecifier &Base : RD->bases()) {
  1134. if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(),
  1135. Size))
  1136. return false;
  1137. }
  1138. if (!addFieldSizes(Context, RD, Size))
  1139. return false;
  1140. return true;
  1141. }
  1142. /// Test whether an argument type which is to be passed indirectly (on the
  1143. /// stack) would have the equivalent layout if it was expanded into separate
  1144. /// arguments. If so, we prefer to do the latter to avoid inhibiting
  1145. /// optimizations.
  1146. bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
  1147. // We can only expand structure types.
  1148. const RecordType *RT = Ty->getAs<RecordType>();
  1149. if (!RT)
  1150. return false;
  1151. const RecordDecl *RD = RT->getDecl();
  1152. uint64_t Size = 0;
  1153. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  1154. if (!IsWin32StructABI) {
  1155. // On non-Windows, we have to conservatively match our old bitcode
  1156. // prototypes in order to be ABI-compatible at the bitcode level.
  1157. if (!CXXRD->isCLike())
  1158. return false;
  1159. } else {
  1160. // Don't do this for dynamic classes.
  1161. if (CXXRD->isDynamicClass())
  1162. return false;
  1163. }
  1164. if (!addBaseAndFieldSizes(getContext(), CXXRD, Size))
  1165. return false;
  1166. } else {
  1167. if (!addFieldSizes(getContext(), RD, Size))
  1168. return false;
  1169. }
  1170. // We can do this if there was no alignment padding.
  1171. return Size == getContext().getTypeSize(Ty);
  1172. }
  1173. ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
  1174. // If the return value is indirect, then the hidden argument is consuming one
  1175. // integer register.
  1176. if (State.FreeRegs) {
  1177. --State.FreeRegs;
  1178. if (!IsMCUABI)
  1179. return getNaturalAlignIndirectInReg(RetTy);
  1180. }
  1181. return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
  1182. }
  1183. ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
  1184. CCState &State) const {
  1185. if (RetTy->isVoidType())
  1186. return ABIArgInfo::getIgnore();
  1187. const Type *Base = nullptr;
  1188. uint64_t NumElts = 0;
  1189. if ((State.CC == llvm::CallingConv::X86_VectorCall ||
  1190. State.CC == llvm::CallingConv::X86_RegCall) &&
  1191. isHomogeneousAggregate(RetTy, Base, NumElts)) {
  1192. // The LLVM struct type for such an aggregate should lower properly.
  1193. return ABIArgInfo::getDirect();
  1194. }
  1195. if (const VectorType *VT = RetTy->getAs<VectorType>()) {
  1196. // On Darwin, some vectors are returned in registers.
  1197. if (IsDarwinVectorABI) {
  1198. uint64_t Size = getContext().getTypeSize(RetTy);
  1199. // 128-bit vectors are a special case; they are returned in
  1200. // registers and we need to make sure to pick a type the LLVM
  1201. // backend will like.
  1202. if (Size == 128)
  1203. return ABIArgInfo::getDirect(llvm::VectorType::get(
  1204. llvm::Type::getInt64Ty(getVMContext()), 2));
  1205. // Always return in register if it fits in a general purpose
  1206. // register, or if it is 64 bits and has a single element.
  1207. if ((Size == 8 || Size == 16 || Size == 32) ||
  1208. (Size == 64 && VT->getNumElements() == 1))
  1209. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  1210. Size));
  1211. return getIndirectReturnResult(RetTy, State);
  1212. }
  1213. return ABIArgInfo::getDirect();
  1214. }
  1215. if (isAggregateTypeForABI(RetTy)) {
  1216. if (const RecordType *RT = RetTy->getAs<RecordType>()) {
  1217. // Structures with flexible arrays are always indirect.
  1218. if (RT->getDecl()->hasFlexibleArrayMember())
  1219. return getIndirectReturnResult(RetTy, State);
  1220. }
  1221. // If specified, structs and unions are always indirect.
  1222. if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
  1223. return getIndirectReturnResult(RetTy, State);
  1224. // Ignore empty structs/unions.
  1225. if (isEmptyRecord(getContext(), RetTy, true))
  1226. return ABIArgInfo::getIgnore();
  1227. // Small structures which are register sized are generally returned
  1228. // in a register.
  1229. if (shouldReturnTypeInRegister(RetTy, getContext())) {
  1230. uint64_t Size = getContext().getTypeSize(RetTy);
  1231. // As a special-case, if the struct is a "single-element" struct, and
  1232. // the field is of type "float" or "double", return it in a
  1233. // floating-point register. (MSVC does not apply this special case.)
  1234. // We apply a similar transformation for pointer types to improve the
  1235. // quality of the generated IR.
  1236. if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
  1237. if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
  1238. || SeltTy->hasPointerRepresentation())
  1239. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  1240. // FIXME: We should be able to narrow this integer in cases with dead
  1241. // padding.
  1242. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
  1243. }
  1244. return getIndirectReturnResult(RetTy, State);
  1245. }
  1246. // Treat an enum type as its underlying type.
  1247. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  1248. RetTy = EnumTy->getDecl()->getIntegerType();
  1249. return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
  1250. : ABIArgInfo::getDirect());
  1251. }
  1252. static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
  1253. return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
  1254. }
  1255. static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
  1256. const RecordType *RT = Ty->getAs<RecordType>();
  1257. if (!RT)
  1258. return 0;
  1259. const RecordDecl *RD = RT->getDecl();
  1260. // If this is a C++ record, check the bases first.
  1261. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  1262. for (const auto &I : CXXRD->bases())
  1263. if (!isRecordWithSSEVectorType(Context, I.getType()))
  1264. return false;
  1265. for (const auto *i : RD->fields()) {
  1266. QualType FT = i->getType();
  1267. if (isSSEVectorType(Context, FT))
  1268. return true;
  1269. if (isRecordWithSSEVectorType(Context, FT))
  1270. return true;
  1271. }
  1272. return false;
  1273. }
  1274. unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
  1275. unsigned Align) const {
  1276. // Otherwise, if the alignment is less than or equal to the minimum ABI
  1277. // alignment, just use the default; the backend will handle this.
  1278. if (Align <= MinABIStackAlignInBytes)
  1279. return 0; // Use default alignment.
  1280. // On non-Darwin, the stack type alignment is always 4.
  1281. if (!IsDarwinVectorABI) {
  1282. // Set explicit alignment, since we may need to realign the top.
  1283. return MinABIStackAlignInBytes;
  1284. }
  1285. // Otherwise, if the type contains an SSE vector type, the alignment is 16.
  1286. if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
  1287. isRecordWithSSEVectorType(getContext(), Ty)))
  1288. return 16;
  1289. return MinABIStackAlignInBytes;
  1290. }
  1291. ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
  1292. CCState &State) const {
  1293. if (!ByVal) {
  1294. if (State.FreeRegs) {
  1295. --State.FreeRegs; // Non-byval indirects just use one pointer.
  1296. if (!IsMCUABI)
  1297. return getNaturalAlignIndirectInReg(Ty);
  1298. }
  1299. return getNaturalAlignIndirect(Ty, false);
  1300. }
  1301. // Compute the byval alignment.
  1302. unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
  1303. unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
  1304. if (StackAlign == 0)
  1305. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
  1306. // If the stack alignment is less than the type alignment, realign the
  1307. // argument.
  1308. bool Realign = TypeAlign > StackAlign;
  1309. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
  1310. /*ByVal=*/true, Realign);
  1311. }
  1312. X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
  1313. const Type *T = isSingleElementStruct(Ty, getContext());
  1314. if (!T)
  1315. T = Ty.getTypePtr();
  1316. if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
  1317. BuiltinType::Kind K = BT->getKind();
  1318. if (K == BuiltinType::Float || K == BuiltinType::Double)
  1319. return Float;
  1320. }
  1321. return Integer;
  1322. }
  1323. bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
  1324. if (!IsSoftFloatABI) {
  1325. Class C = classify(Ty);
  1326. if (C == Float)
  1327. return false;
  1328. }
  1329. unsigned Size = getContext().getTypeSize(Ty);
  1330. unsigned SizeInRegs = (Size + 31) / 32;
  1331. if (SizeInRegs == 0)
  1332. return false;
  1333. if (!IsMCUABI) {
  1334. if (SizeInRegs > State.FreeRegs) {
  1335. State.FreeRegs = 0;
  1336. return false;
  1337. }
  1338. } else {
  1339. // The MCU psABI allows passing parameters in-reg even if there are
  1340. // earlier parameters that are passed on the stack. Also,
  1341. // it does not allow passing >8-byte structs in-register,
  1342. // even if there are 3 free registers available.
  1343. if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
  1344. return false;
  1345. }
  1346. State.FreeRegs -= SizeInRegs;
  1347. return true;
  1348. }
  1349. bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
  1350. bool &InReg,
  1351. bool &NeedsPadding) const {
  1352. // On Windows, aggregates other than HFAs are never passed in registers, and
  1353. // they do not consume register slots. Homogenous floating-point aggregates
  1354. // (HFAs) have already been dealt with at this point.
  1355. if (IsWin32StructABI && isAggregateTypeForABI(Ty))
  1356. return false;
  1357. NeedsPadding = false;
  1358. InReg = !IsMCUABI;
  1359. if (!updateFreeRegs(Ty, State))
  1360. return false;
  1361. if (IsMCUABI)
  1362. return true;
  1363. if (State.CC == llvm::CallingConv::X86_FastCall ||
  1364. State.CC == llvm::CallingConv::X86_VectorCall ||
  1365. State.CC == llvm::CallingConv::X86_RegCall) {
  1366. if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
  1367. NeedsPadding = true;
  1368. return false;
  1369. }
  1370. return true;
  1371. }
  1372. bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
  1373. if (!updateFreeRegs(Ty, State))
  1374. return false;
  1375. if (IsMCUABI)
  1376. return false;
  1377. if (State.CC == llvm::CallingConv::X86_FastCall ||
  1378. State.CC == llvm::CallingConv::X86_VectorCall ||
  1379. State.CC == llvm::CallingConv::X86_RegCall) {
  1380. if (getContext().getTypeSize(Ty) > 32)
  1381. return false;
  1382. return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
  1383. Ty->isReferenceType());
  1384. }
  1385. return true;
  1386. }
  1387. ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
  1388. CCState &State) const {
  1389. // FIXME: Set alignment on indirect arguments.
  1390. Ty = useFirstFieldIfTransparentUnion(Ty);
  1391. // Check with the C++ ABI first.
  1392. const RecordType *RT = Ty->getAs<RecordType>();
  1393. if (RT) {
  1394. CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
  1395. if (RAA == CGCXXABI::RAA_Indirect) {
  1396. return getIndirectResult(Ty, false, State);
  1397. } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
  1398. // The field index doesn't matter, we'll fix it up later.
  1399. return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
  1400. }
  1401. }
  1402. // Regcall uses the concept of a homogenous vector aggregate, similar
  1403. // to other targets.
  1404. const Type *Base = nullptr;
  1405. uint64_t NumElts = 0;
  1406. if (State.CC == llvm::CallingConv::X86_RegCall &&
  1407. isHomogeneousAggregate(Ty, Base, NumElts)) {
  1408. if (State.FreeSSERegs >= NumElts) {
  1409. State.FreeSSERegs -= NumElts;
  1410. if (Ty->isBuiltinType() || Ty->isVectorType())
  1411. return ABIArgInfo::getDirect();
  1412. return ABIArgInfo::getExpand();
  1413. }
  1414. return getIndirectResult(Ty, /*ByVal=*/false, State);
  1415. }
  1416. if (isAggregateTypeForABI(Ty)) {
  1417. // Structures with flexible arrays are always indirect.
  1418. // FIXME: This should not be byval!
  1419. if (RT && RT->getDecl()->hasFlexibleArrayMember())
  1420. return getIndirectResult(Ty, true, State);
  1421. // Ignore empty structs/unions on non-Windows.
  1422. if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
  1423. return ABIArgInfo::getIgnore();
  1424. llvm::LLVMContext &LLVMContext = getVMContext();
  1425. llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
  1426. bool NeedsPadding = false;
  1427. bool InReg;
  1428. if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
  1429. unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
  1430. SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
  1431. llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
  1432. if (InReg)
  1433. return ABIArgInfo::getDirectInReg(Result);
  1434. else
  1435. return ABIArgInfo::getDirect(Result);
  1436. }
  1437. llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
  1438. // Expand small (<= 128-bit) record types when we know that the stack layout
  1439. // of those arguments will match the struct. This is important because the
  1440. // LLVM backend isn't smart enough to remove byval, which inhibits many
  1441. // optimizations.
  1442. // Don't do this for the MCU if there are still free integer registers
  1443. // (see X86_64 ABI for full explanation).
  1444. if (getContext().getTypeSize(Ty) <= 4 * 32 &&
  1445. (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty))
  1446. return ABIArgInfo::getExpandWithPadding(
  1447. State.CC == llvm::CallingConv::X86_FastCall ||
  1448. State.CC == llvm::CallingConv::X86_VectorCall ||
  1449. State.CC == llvm::CallingConv::X86_RegCall,
  1450. PaddingType);
  1451. return getIndirectResult(Ty, true, State);
  1452. }
  1453. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  1454. // On Darwin, some vectors are passed in memory, we handle this by passing
  1455. // it as an i8/i16/i32/i64.
  1456. if (IsDarwinVectorABI) {
  1457. uint64_t Size = getContext().getTypeSize(Ty);
  1458. if ((Size == 8 || Size == 16 || Size == 32) ||
  1459. (Size == 64 && VT->getNumElements() == 1))
  1460. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  1461. Size));
  1462. }
  1463. if (IsX86_MMXType(CGT.ConvertType(Ty)))
  1464. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
  1465. return ABIArgInfo::getDirect();
  1466. }
  1467. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  1468. Ty = EnumTy->getDecl()->getIntegerType();
  1469. bool InReg = shouldPrimitiveUseInReg(Ty, State);
  1470. if (Ty->isPromotableIntegerType()) {
  1471. if (InReg)
  1472. return ABIArgInfo::getExtendInReg(Ty);
  1473. return ABIArgInfo::getExtend(Ty);
  1474. }
  1475. if (InReg)
  1476. return ABIArgInfo::getDirectInReg();
  1477. return ABIArgInfo::getDirect();
  1478. }
  1479. void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
  1480. bool &UsedInAlloca) const {
  1481. // Vectorcall x86 works subtly different than in x64, so the format is
  1482. // a bit different than the x64 version. First, all vector types (not HVAs)
  1483. // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers.
  1484. // This differs from the x64 implementation, where the first 6 by INDEX get
  1485. // registers.
  1486. // After that, integers AND HVAs are assigned Left to Right in the same pass.
  1487. // Integers are passed as ECX/EDX if one is available (in order). HVAs will
  1488. // first take up the remaining YMM/XMM registers. If insufficient registers
  1489. // remain but an integer register (ECX/EDX) is available, it will be passed
  1490. // in that, else, on the stack.
  1491. for (auto &I : FI.arguments()) {
  1492. // First pass do all the vector types.
  1493. const Type *Base = nullptr;
  1494. uint64_t NumElts = 0;
  1495. const QualType& Ty = I.type;
  1496. if ((Ty->isVectorType() || Ty->isBuiltinType()) &&
  1497. isHomogeneousAggregate(Ty, Base, NumElts)) {
  1498. if (State.FreeSSERegs >= NumElts) {
  1499. State.FreeSSERegs -= NumElts;
  1500. I.info = ABIArgInfo::getDirect();
  1501. } else {
  1502. I.info = classifyArgumentType(Ty, State);
  1503. }
  1504. UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
  1505. }
  1506. }
  1507. for (auto &I : FI.arguments()) {
  1508. // Second pass, do the rest!
  1509. const Type *Base = nullptr;
  1510. uint64_t NumElts = 0;
  1511. const QualType& Ty = I.type;
  1512. bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts);
  1513. if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) {
  1514. // Assign true HVAs (non vector/native FP types).
  1515. if (State.FreeSSERegs >= NumElts) {
  1516. State.FreeSSERegs -= NumElts;
  1517. I.info = getDirectX86Hva();
  1518. } else {
  1519. I.info = getIndirectResult(Ty, /*ByVal=*/false, State);
  1520. }
  1521. } else if (!IsHva) {
  1522. // Assign all Non-HVAs, so this will exclude Vector/FP args.
  1523. I.info = classifyArgumentType(Ty, State);
  1524. UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
  1525. }
  1526. }
  1527. }
  1528. void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  1529. CCState State(FI.getCallingConvention());
  1530. if (IsMCUABI)
  1531. State.FreeRegs = 3;
  1532. else if (State.CC == llvm::CallingConv::X86_FastCall)
  1533. State.FreeRegs = 2;
  1534. else if (State.CC == llvm::CallingConv::X86_VectorCall) {
  1535. State.FreeRegs = 2;
  1536. State.FreeSSERegs = 6;
  1537. } else if (FI.getHasRegParm())
  1538. State.FreeRegs = FI.getRegParm();
  1539. else if (State.CC == llvm::CallingConv::X86_RegCall) {
  1540. State.FreeRegs = 5;
  1541. State.FreeSSERegs = 8;
  1542. } else
  1543. State.FreeRegs = DefaultNumRegisterParameters;
  1544. if (!::classifyReturnType(getCXXABI(), FI, *this)) {
  1545. FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
  1546. } else if (FI.getReturnInfo().isIndirect()) {
  1547. // The C++ ABI is not aware of register usage, so we have to check if the
  1548. // return value was sret and put it in a register ourselves if appropriate.
  1549. if (State.FreeRegs) {
  1550. --State.FreeRegs; // The sret parameter consumes a register.
  1551. if (!IsMCUABI)
  1552. FI.getReturnInfo().setInReg(true);
  1553. }
  1554. }
  1555. // The chain argument effectively gives us another free register.
  1556. if (FI.isChainCall())
  1557. ++State.FreeRegs;
  1558. bool UsedInAlloca = false;
  1559. if (State.CC == llvm::CallingConv::X86_VectorCall) {
  1560. computeVectorCallArgs(FI, State, UsedInAlloca);
  1561. } else {
  1562. // If not vectorcall, revert to normal behavior.
  1563. for (auto &I : FI.arguments()) {
  1564. I.info = classifyArgumentType(I.type, State);
  1565. UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
  1566. }
  1567. }
  1568. // If we needed to use inalloca for any argument, do a second pass and rewrite
  1569. // all the memory arguments to use inalloca.
  1570. if (UsedInAlloca)
  1571. rewriteWithInAlloca(FI);
  1572. }
  1573. void
  1574. X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
  1575. CharUnits &StackOffset, ABIArgInfo &Info,
  1576. QualType Type) const {
  1577. // Arguments are always 4-byte-aligned.
  1578. CharUnits FieldAlign = CharUnits::fromQuantity(4);
  1579. assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct");
  1580. Info = ABIArgInfo::getInAlloca(FrameFields.size());
  1581. FrameFields.push_back(CGT.ConvertTypeForMem(Type));
  1582. StackOffset += getContext().getTypeSizeInChars(Type);
  1583. // Insert padding bytes to respect alignment.
  1584. CharUnits FieldEnd = StackOffset;
  1585. StackOffset = FieldEnd.alignTo(FieldAlign);
  1586. if (StackOffset != FieldEnd) {
  1587. CharUnits NumBytes = StackOffset - FieldEnd;
  1588. llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
  1589. Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
  1590. FrameFields.push_back(Ty);
  1591. }
  1592. }
  1593. static bool isArgInAlloca(const ABIArgInfo &Info) {
  1594. // Leave ignored and inreg arguments alone.
  1595. switch (Info.getKind()) {
  1596. case ABIArgInfo::InAlloca:
  1597. return true;
  1598. case ABIArgInfo::Indirect:
  1599. assert(Info.getIndirectByVal());
  1600. return true;
  1601. case ABIArgInfo::Ignore:
  1602. return false;
  1603. case ABIArgInfo::Direct:
  1604. case ABIArgInfo::Extend:
  1605. if (Info.getInReg())
  1606. return false;
  1607. return true;
  1608. case ABIArgInfo::Expand:
  1609. case ABIArgInfo::CoerceAndExpand:
  1610. // These are aggregate types which are never passed in registers when
  1611. // inalloca is involved.
  1612. return true;
  1613. }
  1614. llvm_unreachable("invalid enum");
  1615. }
  1616. void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
  1617. assert(IsWin32StructABI && "inalloca only supported on win32");
  1618. // Build a packed struct type for all of the arguments in memory.
  1619. SmallVector<llvm::Type *, 6> FrameFields;
  1620. // The stack alignment is always 4.
  1621. CharUnits StackAlign = CharUnits::fromQuantity(4);
  1622. CharUnits StackOffset;
  1623. CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
  1624. // Put 'this' into the struct before 'sret', if necessary.
  1625. bool IsThisCall =
  1626. FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
  1627. ABIArgInfo &Ret = FI.getReturnInfo();
  1628. if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
  1629. isArgInAlloca(I->info)) {
  1630. addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
  1631. ++I;
  1632. }
  1633. // Put the sret parameter into the inalloca struct if it's in memory.
  1634. if (Ret.isIndirect() && !Ret.getInReg()) {
  1635. CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
  1636. addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
  1637. // On Windows, the hidden sret parameter is always returned in eax.
  1638. Ret.setInAllocaSRet(IsWin32StructABI);
  1639. }
  1640. // Skip the 'this' parameter in ecx.
  1641. if (IsThisCall)
  1642. ++I;
  1643. // Put arguments passed in memory into the struct.
  1644. for (; I != E; ++I) {
  1645. if (isArgInAlloca(I->info))
  1646. addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
  1647. }
  1648. FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
  1649. /*isPacked=*/true),
  1650. StackAlign);
  1651. }
  1652. Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
  1653. Address VAListAddr, QualType Ty) const {
  1654. auto TypeInfo = getContext().getTypeInfoInChars(Ty);
  1655. // x86-32 changes the alignment of certain arguments on the stack.
  1656. //
  1657. // Just messing with TypeInfo like this works because we never pass
  1658. // anything indirectly.
  1659. TypeInfo.second = CharUnits::fromQuantity(
  1660. getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity()));
  1661. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
  1662. TypeInfo, CharUnits::fromQuantity(4),
  1663. /*AllowHigherAlign*/ true);
  1664. }
  1665. bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
  1666. const llvm::Triple &Triple, const CodeGenOptions &Opts) {
  1667. assert(Triple.getArch() == llvm::Triple::x86);
  1668. switch (Opts.getStructReturnConvention()) {
  1669. case CodeGenOptions::SRCK_Default:
  1670. break;
  1671. case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
  1672. return false;
  1673. case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
  1674. return true;
  1675. }
  1676. if (Triple.isOSDarwin() || Triple.isOSIAMCU())
  1677. return true;
  1678. switch (Triple.getOS()) {
  1679. case llvm::Triple::DragonFly:
  1680. case llvm::Triple::FreeBSD:
  1681. case llvm::Triple::OpenBSD:
  1682. case llvm::Triple::Win32:
  1683. return true;
  1684. default:
  1685. return false;
  1686. }
  1687. }
  1688. void X86_32TargetCodeGenInfo::setTargetAttributes(
  1689. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
  1690. if (GV->isDeclaration())
  1691. return;
  1692. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  1693. if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
  1694. llvm::Function *Fn = cast<llvm::Function>(GV);
  1695. Fn->addFnAttr("stackrealign");
  1696. }
  1697. if (FD->hasAttr<AnyX86InterruptAttr>()) {
  1698. llvm::Function *Fn = cast<llvm::Function>(GV);
  1699. Fn->setCallingConv(llvm::CallingConv::X86_INTR);
  1700. }
  1701. }
  1702. }
  1703. bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
  1704. CodeGen::CodeGenFunction &CGF,
  1705. llvm::Value *Address) const {
  1706. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  1707. llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
  1708. // 0-7 are the eight integer registers; the order is different
  1709. // on Darwin (for EH), but the range is the same.
  1710. // 8 is %eip.
  1711. AssignToArrayRange(Builder, Address, Four8, 0, 8);
  1712. if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
  1713. // 12-16 are st(0..4). Not sure why we stop at 4.
  1714. // These have size 16, which is sizeof(long double) on
  1715. // platforms with 8-byte alignment for that type.
  1716. llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
  1717. AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
  1718. } else {
  1719. // 9 is %eflags, which doesn't get a size on Darwin for some
  1720. // reason.
  1721. Builder.CreateAlignedStore(
  1722. Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
  1723. CharUnits::One());
  1724. // 11-16 are st(0..5). Not sure why we stop at 5.
  1725. // These have size 12, which is sizeof(long double) on
  1726. // platforms with 4-byte alignment for that type.
  1727. llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
  1728. AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
  1729. }
  1730. return false;
  1731. }
  1732. //===----------------------------------------------------------------------===//
  1733. // X86-64 ABI Implementation
  1734. //===----------------------------------------------------------------------===//
  1735. namespace {
  1736. /// The AVX ABI level for X86 targets.
  1737. enum class X86AVXABILevel {
  1738. None,
  1739. AVX,
  1740. AVX512
  1741. };
  1742. /// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
  1743. static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
  1744. switch (AVXLevel) {
  1745. case X86AVXABILevel::AVX512:
  1746. return 512;
  1747. case X86AVXABILevel::AVX:
  1748. return 256;
  1749. case X86AVXABILevel::None:
  1750. return 128;
  1751. }
  1752. llvm_unreachable("Unknown AVXLevel");
  1753. }
  1754. /// X86_64ABIInfo - The X86_64 ABI information.
  1755. class X86_64ABIInfo : public SwiftABIInfo {
  1756. enum Class {
  1757. Integer = 0,
  1758. SSE,
  1759. SSEUp,
  1760. X87,
  1761. X87Up,
  1762. ComplexX87,
  1763. NoClass,
  1764. Memory
  1765. };
  1766. /// merge - Implement the X86_64 ABI merging algorithm.
  1767. ///
  1768. /// Merge an accumulating classification \arg Accum with a field
  1769. /// classification \arg Field.
  1770. ///
  1771. /// \param Accum - The accumulating classification. This should
  1772. /// always be either NoClass or the result of a previous merge
  1773. /// call. In addition, this should never be Memory (the caller
  1774. /// should just return Memory for the aggregate).
  1775. static Class merge(Class Accum, Class Field);
  1776. /// postMerge - Implement the X86_64 ABI post merging algorithm.
  1777. ///
  1778. /// Post merger cleanup, reduces a malformed Hi and Lo pair to
  1779. /// final MEMORY or SSE classes when necessary.
  1780. ///
  1781. /// \param AggregateSize - The size of the current aggregate in
  1782. /// the classification process.
  1783. ///
  1784. /// \param Lo - The classification for the parts of the type
  1785. /// residing in the low word of the containing object.
  1786. ///
  1787. /// \param Hi - The classification for the parts of the type
  1788. /// residing in the higher words of the containing object.
  1789. ///
  1790. void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
  1791. /// classify - Determine the x86_64 register classes in which the
  1792. /// given type T should be passed.
  1793. ///
  1794. /// \param Lo - The classification for the parts of the type
  1795. /// residing in the low word of the containing object.
  1796. ///
  1797. /// \param Hi - The classification for the parts of the type
  1798. /// residing in the high word of the containing object.
  1799. ///
  1800. /// \param OffsetBase - The bit offset of this type in the
  1801. /// containing object. Some parameters are classified different
  1802. /// depending on whether they straddle an eightbyte boundary.
  1803. ///
  1804. /// \param isNamedArg - Whether the argument in question is a "named"
  1805. /// argument, as used in AMD64-ABI 3.5.7.
  1806. ///
  1807. /// If a word is unused its result will be NoClass; if a type should
  1808. /// be passed in Memory then at least the classification of \arg Lo
  1809. /// will be Memory.
  1810. ///
  1811. /// The \arg Lo class will be NoClass iff the argument is ignored.
  1812. ///
  1813. /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
  1814. /// also be ComplexX87.
  1815. void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
  1816. bool isNamedArg) const;
  1817. llvm::Type *GetByteVectorType(QualType Ty) const;
  1818. llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
  1819. unsigned IROffset, QualType SourceTy,
  1820. unsigned SourceOffset) const;
  1821. llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
  1822. unsigned IROffset, QualType SourceTy,
  1823. unsigned SourceOffset) const;
  1824. /// getIndirectResult - Give a source type \arg Ty, return a suitable result
  1825. /// such that the argument will be returned in memory.
  1826. ABIArgInfo getIndirectReturnResult(QualType Ty) const;
  1827. /// getIndirectResult - Give a source type \arg Ty, return a suitable result
  1828. /// such that the argument will be passed in memory.
  1829. ///
  1830. /// \param freeIntRegs - The number of free integer registers remaining
  1831. /// available.
  1832. ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
  1833. ABIArgInfo classifyReturnType(QualType RetTy) const;
  1834. ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs,
  1835. unsigned &neededInt, unsigned &neededSSE,
  1836. bool isNamedArg) const;
  1837. ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
  1838. unsigned &NeededSSE) const;
  1839. ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
  1840. unsigned &NeededSSE) const;
  1841. bool IsIllegalVectorType(QualType Ty) const;
  1842. /// The 0.98 ABI revision clarified a lot of ambiguities,
  1843. /// unfortunately in ways that were not always consistent with
  1844. /// certain previous compilers. In particular, platforms which
  1845. /// required strict binary compatibility with older versions of GCC
  1846. /// may need to exempt themselves.
  1847. bool honorsRevision0_98() const {
  1848. return !getTarget().getTriple().isOSDarwin();
  1849. }
  1850. /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to
  1851. /// classify it as INTEGER (for compatibility with older clang compilers).
  1852. bool classifyIntegerMMXAsSSE() const {
  1853. // Clang <= 3.8 did not do this.
  1854. if (getContext().getLangOpts().getClangABICompat() <=
  1855. LangOptions::ClangABI::Ver3_8)
  1856. return false;
  1857. const llvm::Triple &Triple = getTarget().getTriple();
  1858. if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4)
  1859. return false;
  1860. if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
  1861. return false;
  1862. return true;
  1863. }
  1864. // GCC classifies vectors of __int128 as memory.
  1865. bool passInt128VectorsInMem() const {
  1866. // Clang <= 9.0 did not do this.
  1867. if (getContext().getLangOpts().getClangABICompat() <=
  1868. LangOptions::ClangABI::Ver9)
  1869. return false;
  1870. const llvm::Triple &T = getTarget().getTriple();
  1871. return T.isOSLinux() || T.isOSNetBSD();
  1872. }
  1873. X86AVXABILevel AVXLevel;
  1874. // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
  1875. // 64-bit hardware.
  1876. bool Has64BitPointers;
  1877. public:
  1878. X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
  1879. SwiftABIInfo(CGT), AVXLevel(AVXLevel),
  1880. Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
  1881. }
  1882. bool isPassedUsingAVXType(QualType type) const {
  1883. unsigned neededInt, neededSSE;
  1884. // The freeIntRegs argument doesn't matter here.
  1885. ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
  1886. /*isNamedArg*/true);
  1887. if (info.isDirect()) {
  1888. llvm::Type *ty = info.getCoerceToType();
  1889. if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
  1890. return (vectorTy->getBitWidth() > 128);
  1891. }
  1892. return false;
  1893. }
  1894. void computeInfo(CGFunctionInfo &FI) const override;
  1895. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  1896. QualType Ty) const override;
  1897. Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  1898. QualType Ty) const override;
  1899. bool has64BitPointers() const {
  1900. return Has64BitPointers;
  1901. }
  1902. bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
  1903. bool asReturnValue) const override {
  1904. return occupiesMoreThan(CGT, scalars, /*total*/ 4);
  1905. }
  1906. bool isSwiftErrorInRegister() const override {
  1907. return true;
  1908. }
  1909. };
  1910. /// WinX86_64ABIInfo - The Windows X86_64 ABI information.
  1911. class WinX86_64ABIInfo : public SwiftABIInfo {
  1912. public:
  1913. WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
  1914. : SwiftABIInfo(CGT), AVXLevel(AVXLevel),
  1915. IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
  1916. void computeInfo(CGFunctionInfo &FI) const override;
  1917. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  1918. QualType Ty) const override;
  1919. bool isHomogeneousAggregateBaseType(QualType Ty) const override {
  1920. // FIXME: Assumes vectorcall is in use.
  1921. return isX86VectorTypeForVectorCall(getContext(), Ty);
  1922. }
  1923. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  1924. uint64_t NumMembers) const override {
  1925. // FIXME: Assumes vectorcall is in use.
  1926. return isX86VectorCallAggregateSmallEnough(NumMembers);
  1927. }
  1928. bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars,
  1929. bool asReturnValue) const override {
  1930. return occupiesMoreThan(CGT, scalars, /*total*/ 4);
  1931. }
  1932. bool isSwiftErrorInRegister() const override {
  1933. return true;
  1934. }
  1935. private:
  1936. ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType,
  1937. bool IsVectorCall, bool IsRegCall) const;
  1938. ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
  1939. const ABIArgInfo &current) const;
  1940. void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs,
  1941. bool IsVectorCall, bool IsRegCall) const;
  1942. X86AVXABILevel AVXLevel;
  1943. bool IsMingw64;
  1944. };
  1945. class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
  1946. public:
  1947. X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
  1948. : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {}
  1949. const X86_64ABIInfo &getABIInfo() const {
  1950. return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
  1951. }
  1952. /// Disable tail call on x86-64. The epilogue code before the tail jump blocks
  1953. /// the autoreleaseRV/retainRV optimization.
  1954. bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const override {
  1955. return true;
  1956. }
  1957. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
  1958. return 7;
  1959. }
  1960. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  1961. llvm::Value *Address) const override {
  1962. llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
  1963. // 0-15 are the 16 integer registers.
  1964. // 16 is %rip.
  1965. AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
  1966. return false;
  1967. }
  1968. llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  1969. StringRef Constraint,
  1970. llvm::Type* Ty) const override {
  1971. return X86AdjustInlineAsmType(CGF, Constraint, Ty);
  1972. }
  1973. bool isNoProtoCallVariadic(const CallArgList &args,
  1974. const FunctionNoProtoType *fnType) const override {
  1975. // The default CC on x86-64 sets %al to the number of SSA
  1976. // registers used, and GCC sets this when calling an unprototyped
  1977. // function, so we override the default behavior. However, don't do
  1978. // that when AVX types are involved: the ABI explicitly states it is
  1979. // undefined, and it doesn't work in practice because of how the ABI
  1980. // defines varargs anyway.
  1981. if (fnType->getCallConv() == CC_C) {
  1982. bool HasAVXType = false;
  1983. for (CallArgList::const_iterator
  1984. it = args.begin(), ie = args.end(); it != ie; ++it) {
  1985. if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
  1986. HasAVXType = true;
  1987. break;
  1988. }
  1989. }
  1990. if (!HasAVXType)
  1991. return true;
  1992. }
  1993. return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
  1994. }
  1995. llvm::Constant *
  1996. getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
  1997. unsigned Sig = (0xeb << 0) | // jmp rel8
  1998. (0x06 << 8) | // .+0x08
  1999. ('v' << 16) |
  2000. ('2' << 24);
  2001. return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
  2002. }
  2003. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2004. CodeGen::CodeGenModule &CGM) const override {
  2005. if (GV->isDeclaration())
  2006. return;
  2007. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  2008. if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
  2009. llvm::Function *Fn = cast<llvm::Function>(GV);
  2010. Fn->addFnAttr("stackrealign");
  2011. }
  2012. if (FD->hasAttr<AnyX86InterruptAttr>()) {
  2013. llvm::Function *Fn = cast<llvm::Function>(GV);
  2014. Fn->setCallingConv(llvm::CallingConv::X86_INTR);
  2015. }
  2016. }
  2017. }
  2018. };
  2019. static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
  2020. // If the argument does not end in .lib, automatically add the suffix.
  2021. // If the argument contains a space, enclose it in quotes.
  2022. // This matches the behavior of MSVC.
  2023. bool Quote = (Lib.find(" ") != StringRef::npos);
  2024. std::string ArgStr = Quote ? "\"" : "";
  2025. ArgStr += Lib;
  2026. if (!Lib.endswith_lower(".lib") && !Lib.endswith_lower(".a"))
  2027. ArgStr += ".lib";
  2028. ArgStr += Quote ? "\"" : "";
  2029. return ArgStr;
  2030. }
  2031. class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
  2032. public:
  2033. WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
  2034. bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
  2035. unsigned NumRegisterParameters)
  2036. : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
  2037. Win32StructABI, NumRegisterParameters, false) {}
  2038. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2039. CodeGen::CodeGenModule &CGM) const override;
  2040. void getDependentLibraryOption(llvm::StringRef Lib,
  2041. llvm::SmallString<24> &Opt) const override {
  2042. Opt = "/DEFAULTLIB:";
  2043. Opt += qualifyWindowsLibrary(Lib);
  2044. }
  2045. void getDetectMismatchOption(llvm::StringRef Name,
  2046. llvm::StringRef Value,
  2047. llvm::SmallString<32> &Opt) const override {
  2048. Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
  2049. }
  2050. };
  2051. static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2052. CodeGen::CodeGenModule &CGM) {
  2053. if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) {
  2054. if (CGM.getCodeGenOpts().StackProbeSize != 4096)
  2055. Fn->addFnAttr("stack-probe-size",
  2056. llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
  2057. if (CGM.getCodeGenOpts().NoStackArgProbe)
  2058. Fn->addFnAttr("no-stack-arg-probe");
  2059. }
  2060. }
  2061. void WinX86_32TargetCodeGenInfo::setTargetAttributes(
  2062. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
  2063. X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
  2064. if (GV->isDeclaration())
  2065. return;
  2066. addStackProbeTargetAttributes(D, GV, CGM);
  2067. }
  2068. class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
  2069. public:
  2070. WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
  2071. X86AVXABILevel AVXLevel)
  2072. : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT, AVXLevel)) {}
  2073. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2074. CodeGen::CodeGenModule &CGM) const override;
  2075. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
  2076. return 7;
  2077. }
  2078. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  2079. llvm::Value *Address) const override {
  2080. llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
  2081. // 0-15 are the 16 integer registers.
  2082. // 16 is %rip.
  2083. AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
  2084. return false;
  2085. }
  2086. void getDependentLibraryOption(llvm::StringRef Lib,
  2087. llvm::SmallString<24> &Opt) const override {
  2088. Opt = "/DEFAULTLIB:";
  2089. Opt += qualifyWindowsLibrary(Lib);
  2090. }
  2091. void getDetectMismatchOption(llvm::StringRef Name,
  2092. llvm::StringRef Value,
  2093. llvm::SmallString<32> &Opt) const override {
  2094. Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
  2095. }
  2096. };
  2097. void WinX86_64TargetCodeGenInfo::setTargetAttributes(
  2098. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
  2099. TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
  2100. if (GV->isDeclaration())
  2101. return;
  2102. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  2103. if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
  2104. llvm::Function *Fn = cast<llvm::Function>(GV);
  2105. Fn->addFnAttr("stackrealign");
  2106. }
  2107. if (FD->hasAttr<AnyX86InterruptAttr>()) {
  2108. llvm::Function *Fn = cast<llvm::Function>(GV);
  2109. Fn->setCallingConv(llvm::CallingConv::X86_INTR);
  2110. }
  2111. }
  2112. addStackProbeTargetAttributes(D, GV, CGM);
  2113. }
  2114. }
  2115. void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
  2116. Class &Hi) const {
  2117. // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
  2118. //
  2119. // (a) If one of the classes is Memory, the whole argument is passed in
  2120. // memory.
  2121. //
  2122. // (b) If X87UP is not preceded by X87, the whole argument is passed in
  2123. // memory.
  2124. //
  2125. // (c) If the size of the aggregate exceeds two eightbytes and the first
  2126. // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
  2127. // argument is passed in memory. NOTE: This is necessary to keep the
  2128. // ABI working for processors that don't support the __m256 type.
  2129. //
  2130. // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
  2131. //
  2132. // Some of these are enforced by the merging logic. Others can arise
  2133. // only with unions; for example:
  2134. // union { _Complex double; unsigned; }
  2135. //
  2136. // Note that clauses (b) and (c) were added in 0.98.
  2137. //
  2138. if (Hi == Memory)
  2139. Lo = Memory;
  2140. if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
  2141. Lo = Memory;
  2142. if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
  2143. Lo = Memory;
  2144. if (Hi == SSEUp && Lo != SSE)
  2145. Hi = SSE;
  2146. }
  2147. X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
  2148. // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
  2149. // classified recursively so that always two fields are
  2150. // considered. The resulting class is calculated according to
  2151. // the classes of the fields in the eightbyte:
  2152. //
  2153. // (a) If both classes are equal, this is the resulting class.
  2154. //
  2155. // (b) If one of the classes is NO_CLASS, the resulting class is
  2156. // the other class.
  2157. //
  2158. // (c) If one of the classes is MEMORY, the result is the MEMORY
  2159. // class.
  2160. //
  2161. // (d) If one of the classes is INTEGER, the result is the
  2162. // INTEGER.
  2163. //
  2164. // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
  2165. // MEMORY is used as class.
  2166. //
  2167. // (f) Otherwise class SSE is used.
  2168. // Accum should never be memory (we should have returned) or
  2169. // ComplexX87 (because this cannot be passed in a structure).
  2170. assert((Accum != Memory && Accum != ComplexX87) &&
  2171. "Invalid accumulated classification during merge.");
  2172. if (Accum == Field || Field == NoClass)
  2173. return Accum;
  2174. if (Field == Memory)
  2175. return Memory;
  2176. if (Accum == NoClass)
  2177. return Field;
  2178. if (Accum == Integer || Field == Integer)
  2179. return Integer;
  2180. if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
  2181. Accum == X87 || Accum == X87Up)
  2182. return Memory;
  2183. return SSE;
  2184. }
  2185. void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
  2186. Class &Lo, Class &Hi, bool isNamedArg) const {
  2187. // FIXME: This code can be simplified by introducing a simple value class for
  2188. // Class pairs with appropriate constructor methods for the various
  2189. // situations.
  2190. // FIXME: Some of the split computations are wrong; unaligned vectors
  2191. // shouldn't be passed in registers for example, so there is no chance they
  2192. // can straddle an eightbyte. Verify & simplify.
  2193. Lo = Hi = NoClass;
  2194. Class &Current = OffsetBase < 64 ? Lo : Hi;
  2195. Current = Memory;
  2196. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  2197. BuiltinType::Kind k = BT->getKind();
  2198. if (k == BuiltinType::Void) {
  2199. Current = NoClass;
  2200. } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
  2201. Lo = Integer;
  2202. Hi = Integer;
  2203. } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
  2204. Current = Integer;
  2205. } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
  2206. Current = SSE;
  2207. } else if (k == BuiltinType::LongDouble) {
  2208. const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
  2209. if (LDF == &llvm::APFloat::IEEEquad()) {
  2210. Lo = SSE;
  2211. Hi = SSEUp;
  2212. } else if (LDF == &llvm::APFloat::x87DoubleExtended()) {
  2213. Lo = X87;
  2214. Hi = X87Up;
  2215. } else if (LDF == &llvm::APFloat::IEEEdouble()) {
  2216. Current = SSE;
  2217. } else
  2218. llvm_unreachable("unexpected long double representation!");
  2219. }
  2220. // FIXME: _Decimal32 and _Decimal64 are SSE.
  2221. // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
  2222. return;
  2223. }
  2224. if (const EnumType *ET = Ty->getAs<EnumType>()) {
  2225. // Classify the underlying integer type.
  2226. classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
  2227. return;
  2228. }
  2229. if (Ty->hasPointerRepresentation()) {
  2230. Current = Integer;
  2231. return;
  2232. }
  2233. if (Ty->isMemberPointerType()) {
  2234. if (Ty->isMemberFunctionPointerType()) {
  2235. if (Has64BitPointers) {
  2236. // If Has64BitPointers, this is an {i64, i64}, so classify both
  2237. // Lo and Hi now.
  2238. Lo = Hi = Integer;
  2239. } else {
  2240. // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
  2241. // straddles an eightbyte boundary, Hi should be classified as well.
  2242. uint64_t EB_FuncPtr = (OffsetBase) / 64;
  2243. uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
  2244. if (EB_FuncPtr != EB_ThisAdj) {
  2245. Lo = Hi = Integer;
  2246. } else {
  2247. Current = Integer;
  2248. }
  2249. }
  2250. } else {
  2251. Current = Integer;
  2252. }
  2253. return;
  2254. }
  2255. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  2256. uint64_t Size = getContext().getTypeSize(VT);
  2257. if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
  2258. // gcc passes the following as integer:
  2259. // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
  2260. // 2 bytes - <2 x char>, <1 x short>
  2261. // 1 byte - <1 x char>
  2262. Current = Integer;
  2263. // If this type crosses an eightbyte boundary, it should be
  2264. // split.
  2265. uint64_t EB_Lo = (OffsetBase) / 64;
  2266. uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
  2267. if (EB_Lo != EB_Hi)
  2268. Hi = Lo;
  2269. } else if (Size == 64) {
  2270. QualType ElementType = VT->getElementType();
  2271. // gcc passes <1 x double> in memory. :(
  2272. if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
  2273. return;
  2274. // gcc passes <1 x long long> as SSE but clang used to unconditionally
  2275. // pass them as integer. For platforms where clang is the de facto
  2276. // platform compiler, we must continue to use integer.
  2277. if (!classifyIntegerMMXAsSSE() &&
  2278. (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
  2279. ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
  2280. ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
  2281. ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
  2282. Current = Integer;
  2283. else
  2284. Current = SSE;
  2285. // If this type crosses an eightbyte boundary, it should be
  2286. // split.
  2287. if (OffsetBase && OffsetBase != 64)
  2288. Hi = Lo;
  2289. } else if (Size == 128 ||
  2290. (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
  2291. QualType ElementType = VT->getElementType();
  2292. // gcc passes 256 and 512 bit <X x __int128> vectors in memory. :(
  2293. if (passInt128VectorsInMem() && Size != 128 &&
  2294. (ElementType->isSpecificBuiltinType(BuiltinType::Int128) ||
  2295. ElementType->isSpecificBuiltinType(BuiltinType::UInt128)))
  2296. return;
  2297. // Arguments of 256-bits are split into four eightbyte chunks. The
  2298. // least significant one belongs to class SSE and all the others to class
  2299. // SSEUP. The original Lo and Hi design considers that types can't be
  2300. // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
  2301. // This design isn't correct for 256-bits, but since there're no cases
  2302. // where the upper parts would need to be inspected, avoid adding
  2303. // complexity and just consider Hi to match the 64-256 part.
  2304. //
  2305. // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
  2306. // registers if they are "named", i.e. not part of the "..." of a
  2307. // variadic function.
  2308. //
  2309. // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
  2310. // split into eight eightbyte chunks, one SSE and seven SSEUP.
  2311. Lo = SSE;
  2312. Hi = SSEUp;
  2313. }
  2314. return;
  2315. }
  2316. if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
  2317. QualType ET = getContext().getCanonicalType(CT->getElementType());
  2318. uint64_t Size = getContext().getTypeSize(Ty);
  2319. if (ET->isIntegralOrEnumerationType()) {
  2320. if (Size <= 64)
  2321. Current = Integer;
  2322. else if (Size <= 128)
  2323. Lo = Hi = Integer;
  2324. } else if (ET == getContext().FloatTy) {
  2325. Current = SSE;
  2326. } else if (ET == getContext().DoubleTy) {
  2327. Lo = Hi = SSE;
  2328. } else if (ET == getContext().LongDoubleTy) {
  2329. const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
  2330. if (LDF == &llvm::APFloat::IEEEquad())
  2331. Current = Memory;
  2332. else if (LDF == &llvm::APFloat::x87DoubleExtended())
  2333. Current = ComplexX87;
  2334. else if (LDF == &llvm::APFloat::IEEEdouble())
  2335. Lo = Hi = SSE;
  2336. else
  2337. llvm_unreachable("unexpected long double representation!");
  2338. }
  2339. // If this complex type crosses an eightbyte boundary then it
  2340. // should be split.
  2341. uint64_t EB_Real = (OffsetBase) / 64;
  2342. uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
  2343. if (Hi == NoClass && EB_Real != EB_Imag)
  2344. Hi = Lo;
  2345. return;
  2346. }
  2347. if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
  2348. // Arrays are treated like structures.
  2349. uint64_t Size = getContext().getTypeSize(Ty);
  2350. // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
  2351. // than eight eightbytes, ..., it has class MEMORY.
  2352. if (Size > 512)
  2353. return;
  2354. // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
  2355. // fields, it has class MEMORY.
  2356. //
  2357. // Only need to check alignment of array base.
  2358. if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
  2359. return;
  2360. // Otherwise implement simplified merge. We could be smarter about
  2361. // this, but it isn't worth it and would be harder to verify.
  2362. Current = NoClass;
  2363. uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
  2364. uint64_t ArraySize = AT->getSize().getZExtValue();
  2365. // The only case a 256-bit wide vector could be used is when the array
  2366. // contains a single 256-bit element. Since Lo and Hi logic isn't extended
  2367. // to work for sizes wider than 128, early check and fallback to memory.
  2368. //
  2369. if (Size > 128 &&
  2370. (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
  2371. return;
  2372. for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
  2373. Class FieldLo, FieldHi;
  2374. classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
  2375. Lo = merge(Lo, FieldLo);
  2376. Hi = merge(Hi, FieldHi);
  2377. if (Lo == Memory || Hi == Memory)
  2378. break;
  2379. }
  2380. postMerge(Size, Lo, Hi);
  2381. assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
  2382. return;
  2383. }
  2384. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  2385. uint64_t Size = getContext().getTypeSize(Ty);
  2386. // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
  2387. // than eight eightbytes, ..., it has class MEMORY.
  2388. if (Size > 512)
  2389. return;
  2390. // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
  2391. // copy constructor or a non-trivial destructor, it is passed by invisible
  2392. // reference.
  2393. if (getRecordArgABI(RT, getCXXABI()))
  2394. return;
  2395. const RecordDecl *RD = RT->getDecl();
  2396. // Assume variable sized types are passed in memory.
  2397. if (RD->hasFlexibleArrayMember())
  2398. return;
  2399. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  2400. // Reset Lo class, this will be recomputed.
  2401. Current = NoClass;
  2402. // If this is a C++ record, classify the bases first.
  2403. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  2404. for (const auto &I : CXXRD->bases()) {
  2405. assert(!I.isVirtual() && !I.getType()->isDependentType() &&
  2406. "Unexpected base class!");
  2407. const auto *Base =
  2408. cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
  2409. // Classify this field.
  2410. //
  2411. // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
  2412. // single eightbyte, each is classified separately. Each eightbyte gets
  2413. // initialized to class NO_CLASS.
  2414. Class FieldLo, FieldHi;
  2415. uint64_t Offset =
  2416. OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
  2417. classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
  2418. Lo = merge(Lo, FieldLo);
  2419. Hi = merge(Hi, FieldHi);
  2420. if (Lo == Memory || Hi == Memory) {
  2421. postMerge(Size, Lo, Hi);
  2422. return;
  2423. }
  2424. }
  2425. }
  2426. // Classify the fields one at a time, merging the results.
  2427. unsigned idx = 0;
  2428. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  2429. i != e; ++i, ++idx) {
  2430. uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
  2431. bool BitField = i->isBitField();
  2432. // Ignore padding bit-fields.
  2433. if (BitField && i->isUnnamedBitfield())
  2434. continue;
  2435. // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
  2436. // four eightbytes, or it contains unaligned fields, it has class MEMORY.
  2437. //
  2438. // The only case a 256-bit wide vector could be used is when the struct
  2439. // contains a single 256-bit element. Since Lo and Hi logic isn't extended
  2440. // to work for sizes wider than 128, early check and fallback to memory.
  2441. //
  2442. if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) ||
  2443. Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
  2444. Lo = Memory;
  2445. postMerge(Size, Lo, Hi);
  2446. return;
  2447. }
  2448. // Note, skip this test for bit-fields, see below.
  2449. if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
  2450. Lo = Memory;
  2451. postMerge(Size, Lo, Hi);
  2452. return;
  2453. }
  2454. // Classify this field.
  2455. //
  2456. // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
  2457. // exceeds a single eightbyte, each is classified
  2458. // separately. Each eightbyte gets initialized to class
  2459. // NO_CLASS.
  2460. Class FieldLo, FieldHi;
  2461. // Bit-fields require special handling, they do not force the
  2462. // structure to be passed in memory even if unaligned, and
  2463. // therefore they can straddle an eightbyte.
  2464. if (BitField) {
  2465. assert(!i->isUnnamedBitfield());
  2466. uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
  2467. uint64_t Size = i->getBitWidthValue(getContext());
  2468. uint64_t EB_Lo = Offset / 64;
  2469. uint64_t EB_Hi = (Offset + Size - 1) / 64;
  2470. if (EB_Lo) {
  2471. assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
  2472. FieldLo = NoClass;
  2473. FieldHi = Integer;
  2474. } else {
  2475. FieldLo = Integer;
  2476. FieldHi = EB_Hi ? Integer : NoClass;
  2477. }
  2478. } else
  2479. classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
  2480. Lo = merge(Lo, FieldLo);
  2481. Hi = merge(Hi, FieldHi);
  2482. if (Lo == Memory || Hi == Memory)
  2483. break;
  2484. }
  2485. postMerge(Size, Lo, Hi);
  2486. }
  2487. }
  2488. ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
  2489. // If this is a scalar LLVM value then assume LLVM will pass it in the right
  2490. // place naturally.
  2491. if (!isAggregateTypeForABI(Ty)) {
  2492. // Treat an enum type as its underlying type.
  2493. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  2494. Ty = EnumTy->getDecl()->getIntegerType();
  2495. return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
  2496. : ABIArgInfo::getDirect());
  2497. }
  2498. return getNaturalAlignIndirect(Ty);
  2499. }
  2500. bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
  2501. if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
  2502. uint64_t Size = getContext().getTypeSize(VecTy);
  2503. unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
  2504. if (Size <= 64 || Size > LargestVector)
  2505. return true;
  2506. QualType EltTy = VecTy->getElementType();
  2507. if (passInt128VectorsInMem() &&
  2508. (EltTy->isSpecificBuiltinType(BuiltinType::Int128) ||
  2509. EltTy->isSpecificBuiltinType(BuiltinType::UInt128)))
  2510. return true;
  2511. }
  2512. return false;
  2513. }
  2514. ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
  2515. unsigned freeIntRegs) const {
  2516. // If this is a scalar LLVM value then assume LLVM will pass it in the right
  2517. // place naturally.
  2518. //
  2519. // This assumption is optimistic, as there could be free registers available
  2520. // when we need to pass this argument in memory, and LLVM could try to pass
  2521. // the argument in the free register. This does not seem to happen currently,
  2522. // but this code would be much safer if we could mark the argument with
  2523. // 'onstack'. See PR12193.
  2524. if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
  2525. // Treat an enum type as its underlying type.
  2526. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  2527. Ty = EnumTy->getDecl()->getIntegerType();
  2528. return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
  2529. : ABIArgInfo::getDirect());
  2530. }
  2531. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  2532. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  2533. // Compute the byval alignment. We specify the alignment of the byval in all
  2534. // cases so that the mid-level optimizer knows the alignment of the byval.
  2535. unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
  2536. // Attempt to avoid passing indirect results using byval when possible. This
  2537. // is important for good codegen.
  2538. //
  2539. // We do this by coercing the value into a scalar type which the backend can
  2540. // handle naturally (i.e., without using byval).
  2541. //
  2542. // For simplicity, we currently only do this when we have exhausted all of the
  2543. // free integer registers. Doing this when there are free integer registers
  2544. // would require more care, as we would have to ensure that the coerced value
  2545. // did not claim the unused register. That would require either reording the
  2546. // arguments to the function (so that any subsequent inreg values came first),
  2547. // or only doing this optimization when there were no following arguments that
  2548. // might be inreg.
  2549. //
  2550. // We currently expect it to be rare (particularly in well written code) for
  2551. // arguments to be passed on the stack when there are still free integer
  2552. // registers available (this would typically imply large structs being passed
  2553. // by value), so this seems like a fair tradeoff for now.
  2554. //
  2555. // We can revisit this if the backend grows support for 'onstack' parameter
  2556. // attributes. See PR12193.
  2557. if (freeIntRegs == 0) {
  2558. uint64_t Size = getContext().getTypeSize(Ty);
  2559. // If this type fits in an eightbyte, coerce it into the matching integral
  2560. // type, which will end up on the stack (with alignment 8).
  2561. if (Align == 8 && Size <= 64)
  2562. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  2563. Size));
  2564. }
  2565. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
  2566. }
  2567. /// The ABI specifies that a value should be passed in a full vector XMM/YMM
  2568. /// register. Pick an LLVM IR type that will be passed as a vector register.
  2569. llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
  2570. // Wrapper structs/arrays that only contain vectors are passed just like
  2571. // vectors; strip them off if present.
  2572. if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
  2573. Ty = QualType(InnerTy, 0);
  2574. llvm::Type *IRType = CGT.ConvertType(Ty);
  2575. if (isa<llvm::VectorType>(IRType)) {
  2576. // Don't pass vXi128 vectors in their native type, the backend can't
  2577. // legalize them.
  2578. if (passInt128VectorsInMem() &&
  2579. IRType->getVectorElementType()->isIntegerTy(128)) {
  2580. // Use a vXi64 vector.
  2581. uint64_t Size = getContext().getTypeSize(Ty);
  2582. return llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()),
  2583. Size / 64);
  2584. }
  2585. return IRType;
  2586. }
  2587. if (IRType->getTypeID() == llvm::Type::FP128TyID)
  2588. return IRType;
  2589. // We couldn't find the preferred IR vector type for 'Ty'.
  2590. uint64_t Size = getContext().getTypeSize(Ty);
  2591. assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
  2592. // Return a LLVM IR vector type based on the size of 'Ty'.
  2593. return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()),
  2594. Size / 64);
  2595. }
  2596. /// BitsContainNoUserData - Return true if the specified [start,end) bit range
  2597. /// is known to either be off the end of the specified type or being in
  2598. /// alignment padding. The user type specified is known to be at most 128 bits
  2599. /// in size, and have passed through X86_64ABIInfo::classify with a successful
  2600. /// classification that put one of the two halves in the INTEGER class.
  2601. ///
  2602. /// It is conservatively correct to return false.
  2603. static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
  2604. unsigned EndBit, ASTContext &Context) {
  2605. // If the bytes being queried are off the end of the type, there is no user
  2606. // data hiding here. This handles analysis of builtins, vectors and other
  2607. // types that don't contain interesting padding.
  2608. unsigned TySize = (unsigned)Context.getTypeSize(Ty);
  2609. if (TySize <= StartBit)
  2610. return true;
  2611. if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
  2612. unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
  2613. unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
  2614. // Check each element to see if the element overlaps with the queried range.
  2615. for (unsigned i = 0; i != NumElts; ++i) {
  2616. // If the element is after the span we care about, then we're done..
  2617. unsigned EltOffset = i*EltSize;
  2618. if (EltOffset >= EndBit) break;
  2619. unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
  2620. if (!BitsContainNoUserData(AT->getElementType(), EltStart,
  2621. EndBit-EltOffset, Context))
  2622. return false;
  2623. }
  2624. // If it overlaps no elements, then it is safe to process as padding.
  2625. return true;
  2626. }
  2627. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  2628. const RecordDecl *RD = RT->getDecl();
  2629. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  2630. // If this is a C++ record, check the bases first.
  2631. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  2632. for (const auto &I : CXXRD->bases()) {
  2633. assert(!I.isVirtual() && !I.getType()->isDependentType() &&
  2634. "Unexpected base class!");
  2635. const auto *Base =
  2636. cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
  2637. // If the base is after the span we care about, ignore it.
  2638. unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
  2639. if (BaseOffset >= EndBit) continue;
  2640. unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
  2641. if (!BitsContainNoUserData(I.getType(), BaseStart,
  2642. EndBit-BaseOffset, Context))
  2643. return false;
  2644. }
  2645. }
  2646. // Verify that no field has data that overlaps the region of interest. Yes
  2647. // this could be sped up a lot by being smarter about queried fields,
  2648. // however we're only looking at structs up to 16 bytes, so we don't care
  2649. // much.
  2650. unsigned idx = 0;
  2651. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  2652. i != e; ++i, ++idx) {
  2653. unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
  2654. // If we found a field after the region we care about, then we're done.
  2655. if (FieldOffset >= EndBit) break;
  2656. unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
  2657. if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
  2658. Context))
  2659. return false;
  2660. }
  2661. // If nothing in this record overlapped the area of interest, then we're
  2662. // clean.
  2663. return true;
  2664. }
  2665. return false;
  2666. }
  2667. /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
  2668. /// float member at the specified offset. For example, {int,{float}} has a
  2669. /// float at offset 4. It is conservatively correct for this routine to return
  2670. /// false.
  2671. static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
  2672. const llvm::DataLayout &TD) {
  2673. // Base case if we find a float.
  2674. if (IROffset == 0 && IRType->isFloatTy())
  2675. return true;
  2676. // If this is a struct, recurse into the field at the specified offset.
  2677. if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
  2678. const llvm::StructLayout *SL = TD.getStructLayout(STy);
  2679. unsigned Elt = SL->getElementContainingOffset(IROffset);
  2680. IROffset -= SL->getElementOffset(Elt);
  2681. return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
  2682. }
  2683. // If this is an array, recurse into the field at the specified offset.
  2684. if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
  2685. llvm::Type *EltTy = ATy->getElementType();
  2686. unsigned EltSize = TD.getTypeAllocSize(EltTy);
  2687. IROffset -= IROffset/EltSize*EltSize;
  2688. return ContainsFloatAtOffset(EltTy, IROffset, TD);
  2689. }
  2690. return false;
  2691. }
  2692. /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
  2693. /// low 8 bytes of an XMM register, corresponding to the SSE class.
  2694. llvm::Type *X86_64ABIInfo::
  2695. GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
  2696. QualType SourceTy, unsigned SourceOffset) const {
  2697. // The only three choices we have are either double, <2 x float>, or float. We
  2698. // pass as float if the last 4 bytes is just padding. This happens for
  2699. // structs that contain 3 floats.
  2700. if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
  2701. SourceOffset*8+64, getContext()))
  2702. return llvm::Type::getFloatTy(getVMContext());
  2703. // We want to pass as <2 x float> if the LLVM IR type contains a float at
  2704. // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
  2705. // case.
  2706. if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
  2707. ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
  2708. return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
  2709. return llvm::Type::getDoubleTy(getVMContext());
  2710. }
  2711. /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
  2712. /// an 8-byte GPR. This means that we either have a scalar or we are talking
  2713. /// about the high or low part of an up-to-16-byte struct. This routine picks
  2714. /// the best LLVM IR type to represent this, which may be i64 or may be anything
  2715. /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
  2716. /// etc).
  2717. ///
  2718. /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
  2719. /// the source type. IROffset is an offset in bytes into the LLVM IR type that
  2720. /// the 8-byte value references. PrefType may be null.
  2721. ///
  2722. /// SourceTy is the source-level type for the entire argument. SourceOffset is
  2723. /// an offset into this that we're processing (which is always either 0 or 8).
  2724. ///
  2725. llvm::Type *X86_64ABIInfo::
  2726. GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
  2727. QualType SourceTy, unsigned SourceOffset) const {
  2728. // If we're dealing with an un-offset LLVM IR type, then it means that we're
  2729. // returning an 8-byte unit starting with it. See if we can safely use it.
  2730. if (IROffset == 0) {
  2731. // Pointers and int64's always fill the 8-byte unit.
  2732. if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
  2733. IRType->isIntegerTy(64))
  2734. return IRType;
  2735. // If we have a 1/2/4-byte integer, we can use it only if the rest of the
  2736. // goodness in the source type is just tail padding. This is allowed to
  2737. // kick in for struct {double,int} on the int, but not on
  2738. // struct{double,int,int} because we wouldn't return the second int. We
  2739. // have to do this analysis on the source type because we can't depend on
  2740. // unions being lowered a specific way etc.
  2741. if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
  2742. IRType->isIntegerTy(32) ||
  2743. (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
  2744. unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
  2745. cast<llvm::IntegerType>(IRType)->getBitWidth();
  2746. if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
  2747. SourceOffset*8+64, getContext()))
  2748. return IRType;
  2749. }
  2750. }
  2751. if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
  2752. // If this is a struct, recurse into the field at the specified offset.
  2753. const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
  2754. if (IROffset < SL->getSizeInBytes()) {
  2755. unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
  2756. IROffset -= SL->getElementOffset(FieldIdx);
  2757. return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
  2758. SourceTy, SourceOffset);
  2759. }
  2760. }
  2761. if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
  2762. llvm::Type *EltTy = ATy->getElementType();
  2763. unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
  2764. unsigned EltOffset = IROffset/EltSize*EltSize;
  2765. return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
  2766. SourceOffset);
  2767. }
  2768. // Okay, we don't have any better idea of what to pass, so we pass this in an
  2769. // integer register that isn't too big to fit the rest of the struct.
  2770. unsigned TySizeInBytes =
  2771. (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
  2772. assert(TySizeInBytes != SourceOffset && "Empty field?");
  2773. // It is always safe to classify this as an integer type up to i64 that
  2774. // isn't larger than the structure.
  2775. return llvm::IntegerType::get(getVMContext(),
  2776. std::min(TySizeInBytes-SourceOffset, 8U)*8);
  2777. }
  2778. /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
  2779. /// be used as elements of a two register pair to pass or return, return a
  2780. /// first class aggregate to represent them. For example, if the low part of
  2781. /// a by-value argument should be passed as i32* and the high part as float,
  2782. /// return {i32*, float}.
  2783. static llvm::Type *
  2784. GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
  2785. const llvm::DataLayout &TD) {
  2786. // In order to correctly satisfy the ABI, we need to the high part to start
  2787. // at offset 8. If the high and low parts we inferred are both 4-byte types
  2788. // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
  2789. // the second element at offset 8. Check for this:
  2790. unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
  2791. unsigned HiAlign = TD.getABITypeAlignment(Hi);
  2792. unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
  2793. assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
  2794. // To handle this, we have to increase the size of the low part so that the
  2795. // second element will start at an 8 byte offset. We can't increase the size
  2796. // of the second element because it might make us access off the end of the
  2797. // struct.
  2798. if (HiStart != 8) {
  2799. // There are usually two sorts of types the ABI generation code can produce
  2800. // for the low part of a pair that aren't 8 bytes in size: float or
  2801. // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and
  2802. // NaCl).
  2803. // Promote these to a larger type.
  2804. if (Lo->isFloatTy())
  2805. Lo = llvm::Type::getDoubleTy(Lo->getContext());
  2806. else {
  2807. assert((Lo->isIntegerTy() || Lo->isPointerTy())
  2808. && "Invalid/unknown lo type");
  2809. Lo = llvm::Type::getInt64Ty(Lo->getContext());
  2810. }
  2811. }
  2812. llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
  2813. // Verify that the second element is at an 8-byte offset.
  2814. assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
  2815. "Invalid x86-64 argument pair!");
  2816. return Result;
  2817. }
  2818. ABIArgInfo X86_64ABIInfo::
  2819. classifyReturnType(QualType RetTy) const {
  2820. // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
  2821. // classification algorithm.
  2822. X86_64ABIInfo::Class Lo, Hi;
  2823. classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
  2824. // Check some invariants.
  2825. assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
  2826. assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
  2827. llvm::Type *ResType = nullptr;
  2828. switch (Lo) {
  2829. case NoClass:
  2830. if (Hi == NoClass)
  2831. return ABIArgInfo::getIgnore();
  2832. // If the low part is just padding, it takes no register, leave ResType
  2833. // null.
  2834. assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
  2835. "Unknown missing lo part");
  2836. break;
  2837. case SSEUp:
  2838. case X87Up:
  2839. llvm_unreachable("Invalid classification for lo word.");
  2840. // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
  2841. // hidden argument.
  2842. case Memory:
  2843. return getIndirectReturnResult(RetTy);
  2844. // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
  2845. // available register of the sequence %rax, %rdx is used.
  2846. case Integer:
  2847. ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
  2848. // If we have a sign or zero extended integer, make sure to return Extend
  2849. // so that the parameter gets the right LLVM IR attributes.
  2850. if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
  2851. // Treat an enum type as its underlying type.
  2852. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  2853. RetTy = EnumTy->getDecl()->getIntegerType();
  2854. if (RetTy->isIntegralOrEnumerationType() &&
  2855. RetTy->isPromotableIntegerType())
  2856. return ABIArgInfo::getExtend(RetTy);
  2857. }
  2858. break;
  2859. // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
  2860. // available SSE register of the sequence %xmm0, %xmm1 is used.
  2861. case SSE:
  2862. ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
  2863. break;
  2864. // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
  2865. // returned on the X87 stack in %st0 as 80-bit x87 number.
  2866. case X87:
  2867. ResType = llvm::Type::getX86_FP80Ty(getVMContext());
  2868. break;
  2869. // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
  2870. // part of the value is returned in %st0 and the imaginary part in
  2871. // %st1.
  2872. case ComplexX87:
  2873. assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
  2874. ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
  2875. llvm::Type::getX86_FP80Ty(getVMContext()));
  2876. break;
  2877. }
  2878. llvm::Type *HighPart = nullptr;
  2879. switch (Hi) {
  2880. // Memory was handled previously and X87 should
  2881. // never occur as a hi class.
  2882. case Memory:
  2883. case X87:
  2884. llvm_unreachable("Invalid classification for hi word.");
  2885. case ComplexX87: // Previously handled.
  2886. case NoClass:
  2887. break;
  2888. case Integer:
  2889. HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
  2890. if (Lo == NoClass) // Return HighPart at offset 8 in memory.
  2891. return ABIArgInfo::getDirect(HighPart, 8);
  2892. break;
  2893. case SSE:
  2894. HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
  2895. if (Lo == NoClass) // Return HighPart at offset 8 in memory.
  2896. return ABIArgInfo::getDirect(HighPart, 8);
  2897. break;
  2898. // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
  2899. // is passed in the next available eightbyte chunk if the last used
  2900. // vector register.
  2901. //
  2902. // SSEUP should always be preceded by SSE, just widen.
  2903. case SSEUp:
  2904. assert(Lo == SSE && "Unexpected SSEUp classification.");
  2905. ResType = GetByteVectorType(RetTy);
  2906. break;
  2907. // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
  2908. // returned together with the previous X87 value in %st0.
  2909. case X87Up:
  2910. // If X87Up is preceded by X87, we don't need to do
  2911. // anything. However, in some cases with unions it may not be
  2912. // preceded by X87. In such situations we follow gcc and pass the
  2913. // extra bits in an SSE reg.
  2914. if (Lo != X87) {
  2915. HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
  2916. if (Lo == NoClass) // Return HighPart at offset 8 in memory.
  2917. return ABIArgInfo::getDirect(HighPart, 8);
  2918. }
  2919. break;
  2920. }
  2921. // If a high part was specified, merge it together with the low part. It is
  2922. // known to pass in the high eightbyte of the result. We do this by forming a
  2923. // first class struct aggregate with the high and low part: {low, high}
  2924. if (HighPart)
  2925. ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
  2926. return ABIArgInfo::getDirect(ResType);
  2927. }
  2928. ABIArgInfo X86_64ABIInfo::classifyArgumentType(
  2929. QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
  2930. bool isNamedArg)
  2931. const
  2932. {
  2933. Ty = useFirstFieldIfTransparentUnion(Ty);
  2934. X86_64ABIInfo::Class Lo, Hi;
  2935. classify(Ty, 0, Lo, Hi, isNamedArg);
  2936. // Check some invariants.
  2937. // FIXME: Enforce these by construction.
  2938. assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
  2939. assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
  2940. neededInt = 0;
  2941. neededSSE = 0;
  2942. llvm::Type *ResType = nullptr;
  2943. switch (Lo) {
  2944. case NoClass:
  2945. if (Hi == NoClass)
  2946. return ABIArgInfo::getIgnore();
  2947. // If the low part is just padding, it takes no register, leave ResType
  2948. // null.
  2949. assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
  2950. "Unknown missing lo part");
  2951. break;
  2952. // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
  2953. // on the stack.
  2954. case Memory:
  2955. // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
  2956. // COMPLEX_X87, it is passed in memory.
  2957. case X87:
  2958. case ComplexX87:
  2959. if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
  2960. ++neededInt;
  2961. return getIndirectResult(Ty, freeIntRegs);
  2962. case SSEUp:
  2963. case X87Up:
  2964. llvm_unreachable("Invalid classification for lo word.");
  2965. // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
  2966. // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
  2967. // and %r9 is used.
  2968. case Integer:
  2969. ++neededInt;
  2970. // Pick an 8-byte type based on the preferred type.
  2971. ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
  2972. // If we have a sign or zero extended integer, make sure to return Extend
  2973. // so that the parameter gets the right LLVM IR attributes.
  2974. if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
  2975. // Treat an enum type as its underlying type.
  2976. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  2977. Ty = EnumTy->getDecl()->getIntegerType();
  2978. if (Ty->isIntegralOrEnumerationType() &&
  2979. Ty->isPromotableIntegerType())
  2980. return ABIArgInfo::getExtend(Ty);
  2981. }
  2982. break;
  2983. // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
  2984. // available SSE register is used, the registers are taken in the
  2985. // order from %xmm0 to %xmm7.
  2986. case SSE: {
  2987. llvm::Type *IRType = CGT.ConvertType(Ty);
  2988. ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
  2989. ++neededSSE;
  2990. break;
  2991. }
  2992. }
  2993. llvm::Type *HighPart = nullptr;
  2994. switch (Hi) {
  2995. // Memory was handled previously, ComplexX87 and X87 should
  2996. // never occur as hi classes, and X87Up must be preceded by X87,
  2997. // which is passed in memory.
  2998. case Memory:
  2999. case X87:
  3000. case ComplexX87:
  3001. llvm_unreachable("Invalid classification for hi word.");
  3002. case NoClass: break;
  3003. case Integer:
  3004. ++neededInt;
  3005. // Pick an 8-byte type based on the preferred type.
  3006. HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
  3007. if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
  3008. return ABIArgInfo::getDirect(HighPart, 8);
  3009. break;
  3010. // X87Up generally doesn't occur here (long double is passed in
  3011. // memory), except in situations involving unions.
  3012. case X87Up:
  3013. case SSE:
  3014. HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
  3015. if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
  3016. return ABIArgInfo::getDirect(HighPart, 8);
  3017. ++neededSSE;
  3018. break;
  3019. // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
  3020. // eightbyte is passed in the upper half of the last used SSE
  3021. // register. This only happens when 128-bit vectors are passed.
  3022. case SSEUp:
  3023. assert(Lo == SSE && "Unexpected SSEUp classification");
  3024. ResType = GetByteVectorType(Ty);
  3025. break;
  3026. }
  3027. // If a high part was specified, merge it together with the low part. It is
  3028. // known to pass in the high eightbyte of the result. We do this by forming a
  3029. // first class struct aggregate with the high and low part: {low, high}
  3030. if (HighPart)
  3031. ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
  3032. return ABIArgInfo::getDirect(ResType);
  3033. }
  3034. ABIArgInfo
  3035. X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
  3036. unsigned &NeededSSE) const {
  3037. auto RT = Ty->getAs<RecordType>();
  3038. assert(RT && "classifyRegCallStructType only valid with struct types");
  3039. if (RT->getDecl()->hasFlexibleArrayMember())
  3040. return getIndirectReturnResult(Ty);
  3041. // Sum up bases
  3042. if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
  3043. if (CXXRD->isDynamicClass()) {
  3044. NeededInt = NeededSSE = 0;
  3045. return getIndirectReturnResult(Ty);
  3046. }
  3047. for (const auto &I : CXXRD->bases())
  3048. if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE)
  3049. .isIndirect()) {
  3050. NeededInt = NeededSSE = 0;
  3051. return getIndirectReturnResult(Ty);
  3052. }
  3053. }
  3054. // Sum up members
  3055. for (const auto *FD : RT->getDecl()->fields()) {
  3056. if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) {
  3057. if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE)
  3058. .isIndirect()) {
  3059. NeededInt = NeededSSE = 0;
  3060. return getIndirectReturnResult(Ty);
  3061. }
  3062. } else {
  3063. unsigned LocalNeededInt, LocalNeededSSE;
  3064. if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt,
  3065. LocalNeededSSE, true)
  3066. .isIndirect()) {
  3067. NeededInt = NeededSSE = 0;
  3068. return getIndirectReturnResult(Ty);
  3069. }
  3070. NeededInt += LocalNeededInt;
  3071. NeededSSE += LocalNeededSSE;
  3072. }
  3073. }
  3074. return ABIArgInfo::getDirect();
  3075. }
  3076. ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty,
  3077. unsigned &NeededInt,
  3078. unsigned &NeededSSE) const {
  3079. NeededInt = 0;
  3080. NeededSSE = 0;
  3081. return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE);
  3082. }
  3083. void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  3084. const unsigned CallingConv = FI.getCallingConvention();
  3085. // It is possible to force Win64 calling convention on any x86_64 target by
  3086. // using __attribute__((ms_abi)). In such case to correctly emit Win64
  3087. // compatible code delegate this call to WinX86_64ABIInfo::computeInfo.
  3088. if (CallingConv == llvm::CallingConv::Win64) {
  3089. WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel);
  3090. Win64ABIInfo.computeInfo(FI);
  3091. return;
  3092. }
  3093. bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall;
  3094. // Keep track of the number of assigned registers.
  3095. unsigned FreeIntRegs = IsRegCall ? 11 : 6;
  3096. unsigned FreeSSERegs = IsRegCall ? 16 : 8;
  3097. unsigned NeededInt, NeededSSE;
  3098. if (!::classifyReturnType(getCXXABI(), FI, *this)) {
  3099. if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
  3100. !FI.getReturnType()->getTypePtr()->isUnionType()) {
  3101. FI.getReturnInfo() =
  3102. classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE);
  3103. if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
  3104. FreeIntRegs -= NeededInt;
  3105. FreeSSERegs -= NeededSSE;
  3106. } else {
  3107. FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
  3108. }
  3109. } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) {
  3110. // Complex Long Double Type is passed in Memory when Regcall
  3111. // calling convention is used.
  3112. const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>();
  3113. if (getContext().getCanonicalType(CT->getElementType()) ==
  3114. getContext().LongDoubleTy)
  3115. FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
  3116. } else
  3117. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  3118. }
  3119. // If the return value is indirect, then the hidden argument is consuming one
  3120. // integer register.
  3121. if (FI.getReturnInfo().isIndirect())
  3122. --FreeIntRegs;
  3123. // The chain argument effectively gives us another free register.
  3124. if (FI.isChainCall())
  3125. ++FreeIntRegs;
  3126. unsigned NumRequiredArgs = FI.getNumRequiredArgs();
  3127. // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
  3128. // get assigned (in left-to-right order) for passing as follows...
  3129. unsigned ArgNo = 0;
  3130. for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
  3131. it != ie; ++it, ++ArgNo) {
  3132. bool IsNamedArg = ArgNo < NumRequiredArgs;
  3133. if (IsRegCall && it->type->isStructureOrClassType())
  3134. it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE);
  3135. else
  3136. it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
  3137. NeededSSE, IsNamedArg);
  3138. // AMD64-ABI 3.2.3p3: If there are no registers available for any
  3139. // eightbyte of an argument, the whole argument is passed on the
  3140. // stack. If registers have already been assigned for some
  3141. // eightbytes of such an argument, the assignments get reverted.
  3142. if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
  3143. FreeIntRegs -= NeededInt;
  3144. FreeSSERegs -= NeededSSE;
  3145. } else {
  3146. it->info = getIndirectResult(it->type, FreeIntRegs);
  3147. }
  3148. }
  3149. }
  3150. static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
  3151. Address VAListAddr, QualType Ty) {
  3152. Address overflow_arg_area_p =
  3153. CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
  3154. llvm::Value *overflow_arg_area =
  3155. CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
  3156. // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
  3157. // byte boundary if alignment needed by type exceeds 8 byte boundary.
  3158. // It isn't stated explicitly in the standard, but in practice we use
  3159. // alignment greater than 16 where necessary.
  3160. CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
  3161. if (Align > CharUnits::fromQuantity(8)) {
  3162. overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
  3163. Align);
  3164. }
  3165. // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
  3166. llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
  3167. llvm::Value *Res =
  3168. CGF.Builder.CreateBitCast(overflow_arg_area,
  3169. llvm::PointerType::getUnqual(LTy));
  3170. // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
  3171. // l->overflow_arg_area + sizeof(type).
  3172. // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
  3173. // an 8 byte boundary.
  3174. uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
  3175. llvm::Value *Offset =
  3176. llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
  3177. overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
  3178. "overflow_arg_area.next");
  3179. CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
  3180. // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
  3181. return Address(Res, Align);
  3182. }
  3183. Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3184. QualType Ty) const {
  3185. // Assume that va_list type is correct; should be pointer to LLVM type:
  3186. // struct {
  3187. // i32 gp_offset;
  3188. // i32 fp_offset;
  3189. // i8* overflow_arg_area;
  3190. // i8* reg_save_area;
  3191. // };
  3192. unsigned neededInt, neededSSE;
  3193. Ty = getContext().getCanonicalType(Ty);
  3194. ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
  3195. /*isNamedArg*/false);
  3196. // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
  3197. // in the registers. If not go to step 7.
  3198. if (!neededInt && !neededSSE)
  3199. return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
  3200. // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
  3201. // general purpose registers needed to pass type and num_fp to hold
  3202. // the number of floating point registers needed.
  3203. // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
  3204. // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
  3205. // l->fp_offset > 304 - num_fp * 16 go to step 7.
  3206. //
  3207. // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
  3208. // register save space).
  3209. llvm::Value *InRegs = nullptr;
  3210. Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
  3211. llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
  3212. if (neededInt) {
  3213. gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
  3214. gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
  3215. InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
  3216. InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
  3217. }
  3218. if (neededSSE) {
  3219. fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
  3220. fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
  3221. llvm::Value *FitsInFP =
  3222. llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
  3223. FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
  3224. InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
  3225. }
  3226. llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
  3227. llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
  3228. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
  3229. CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
  3230. // Emit code to load the value if it was passed in registers.
  3231. CGF.EmitBlock(InRegBlock);
  3232. // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
  3233. // an offset of l->gp_offset and/or l->fp_offset. This may require
  3234. // copying to a temporary location in case the parameter is passed
  3235. // in different register classes or requires an alignment greater
  3236. // than 8 for general purpose registers and 16 for XMM registers.
  3237. //
  3238. // FIXME: This really results in shameful code when we end up needing to
  3239. // collect arguments from different places; often what should result in a
  3240. // simple assembling of a structure from scattered addresses has many more
  3241. // loads than necessary. Can we clean this up?
  3242. llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
  3243. llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
  3244. CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area");
  3245. Address RegAddr = Address::invalid();
  3246. if (neededInt && neededSSE) {
  3247. // FIXME: Cleanup.
  3248. assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
  3249. llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
  3250. Address Tmp = CGF.CreateMemTemp(Ty);
  3251. Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
  3252. assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
  3253. llvm::Type *TyLo = ST->getElementType(0);
  3254. llvm::Type *TyHi = ST->getElementType(1);
  3255. assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
  3256. "Unexpected ABI info for mixed regs");
  3257. llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
  3258. llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
  3259. llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
  3260. llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
  3261. llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
  3262. llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
  3263. // Copy the first element.
  3264. // FIXME: Our choice of alignment here and below is probably pessimistic.
  3265. llvm::Value *V = CGF.Builder.CreateAlignedLoad(
  3266. TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
  3267. CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo)));
  3268. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
  3269. // Copy the second element.
  3270. V = CGF.Builder.CreateAlignedLoad(
  3271. TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
  3272. CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi)));
  3273. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
  3274. RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
  3275. } else if (neededInt) {
  3276. RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
  3277. CharUnits::fromQuantity(8));
  3278. RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
  3279. // Copy to a temporary if necessary to ensure the appropriate alignment.
  3280. std::pair<CharUnits, CharUnits> SizeAlign =
  3281. getContext().getTypeInfoInChars(Ty);
  3282. uint64_t TySize = SizeAlign.first.getQuantity();
  3283. CharUnits TyAlign = SizeAlign.second;
  3284. // Copy into a temporary if the type is more aligned than the
  3285. // register save area.
  3286. if (TyAlign.getQuantity() > 8) {
  3287. Address Tmp = CGF.CreateMemTemp(Ty);
  3288. CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
  3289. RegAddr = Tmp;
  3290. }
  3291. } else if (neededSSE == 1) {
  3292. RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
  3293. CharUnits::fromQuantity(16));
  3294. RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
  3295. } else {
  3296. assert(neededSSE == 2 && "Invalid number of needed registers!");
  3297. // SSE registers are spaced 16 bytes apart in the register save
  3298. // area, we need to collect the two eightbytes together.
  3299. // The ABI isn't explicit about this, but it seems reasonable
  3300. // to assume that the slots are 16-byte aligned, since the stack is
  3301. // naturally 16-byte aligned and the prologue is expected to store
  3302. // all the SSE registers to the RSA.
  3303. Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
  3304. CharUnits::fromQuantity(16));
  3305. Address RegAddrHi =
  3306. CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
  3307. CharUnits::fromQuantity(16));
  3308. llvm::Type *ST = AI.canHaveCoerceToType()
  3309. ? AI.getCoerceToType()
  3310. : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy);
  3311. llvm::Value *V;
  3312. Address Tmp = CGF.CreateMemTemp(Ty);
  3313. Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
  3314. V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
  3315. RegAddrLo, ST->getStructElementType(0)));
  3316. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
  3317. V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
  3318. RegAddrHi, ST->getStructElementType(1)));
  3319. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
  3320. RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
  3321. }
  3322. // AMD64-ABI 3.5.7p5: Step 5. Set:
  3323. // l->gp_offset = l->gp_offset + num_gp * 8
  3324. // l->fp_offset = l->fp_offset + num_fp * 16.
  3325. if (neededInt) {
  3326. llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
  3327. CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
  3328. gp_offset_p);
  3329. }
  3330. if (neededSSE) {
  3331. llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
  3332. CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
  3333. fp_offset_p);
  3334. }
  3335. CGF.EmitBranch(ContBlock);
  3336. // Emit code to load the value if it was passed in memory.
  3337. CGF.EmitBlock(InMemBlock);
  3338. Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
  3339. // Return the appropriate result.
  3340. CGF.EmitBlock(ContBlock);
  3341. Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
  3342. "vaarg.addr");
  3343. return ResAddr;
  3344. }
  3345. Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3346. QualType Ty) const {
  3347. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
  3348. CGF.getContext().getTypeInfoInChars(Ty),
  3349. CharUnits::fromQuantity(8),
  3350. /*allowHigherAlign*/ false);
  3351. }
  3352. ABIArgInfo
  3353. WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
  3354. const ABIArgInfo &current) const {
  3355. // Assumes vectorCall calling convention.
  3356. const Type *Base = nullptr;
  3357. uint64_t NumElts = 0;
  3358. if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
  3359. isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
  3360. FreeSSERegs -= NumElts;
  3361. return getDirectX86Hva();
  3362. }
  3363. return current;
  3364. }
  3365. ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
  3366. bool IsReturnType, bool IsVectorCall,
  3367. bool IsRegCall) const {
  3368. if (Ty->isVoidType())
  3369. return ABIArgInfo::getIgnore();
  3370. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  3371. Ty = EnumTy->getDecl()->getIntegerType();
  3372. TypeInfo Info = getContext().getTypeInfo(Ty);
  3373. uint64_t Width = Info.Width;
  3374. CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
  3375. const RecordType *RT = Ty->getAs<RecordType>();
  3376. if (RT) {
  3377. if (!IsReturnType) {
  3378. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
  3379. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  3380. }
  3381. if (RT->getDecl()->hasFlexibleArrayMember())
  3382. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  3383. }
  3384. const Type *Base = nullptr;
  3385. uint64_t NumElts = 0;
  3386. // vectorcall adds the concept of a homogenous vector aggregate, similar to
  3387. // other targets.
  3388. if ((IsVectorCall || IsRegCall) &&
  3389. isHomogeneousAggregate(Ty, Base, NumElts)) {
  3390. if (IsRegCall) {
  3391. if (FreeSSERegs >= NumElts) {
  3392. FreeSSERegs -= NumElts;
  3393. if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
  3394. return ABIArgInfo::getDirect();
  3395. return ABIArgInfo::getExpand();
  3396. }
  3397. return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
  3398. } else if (IsVectorCall) {
  3399. if (FreeSSERegs >= NumElts &&
  3400. (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
  3401. FreeSSERegs -= NumElts;
  3402. return ABIArgInfo::getDirect();
  3403. } else if (IsReturnType) {
  3404. return ABIArgInfo::getExpand();
  3405. } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
  3406. // HVAs are delayed and reclassified in the 2nd step.
  3407. return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
  3408. }
  3409. }
  3410. }
  3411. if (Ty->isMemberPointerType()) {
  3412. // If the member pointer is represented by an LLVM int or ptr, pass it
  3413. // directly.
  3414. llvm::Type *LLTy = CGT.ConvertType(Ty);
  3415. if (LLTy->isPointerTy() || LLTy->isIntegerTy())
  3416. return ABIArgInfo::getDirect();
  3417. }
  3418. if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
  3419. // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
  3420. // not 1, 2, 4, or 8 bytes, must be passed by reference."
  3421. if (Width > 64 || !llvm::isPowerOf2_64(Width))
  3422. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  3423. // Otherwise, coerce it to a small integer.
  3424. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
  3425. }
  3426. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  3427. switch (BT->getKind()) {
  3428. case BuiltinType::Bool:
  3429. // Bool type is always extended to the ABI, other builtin types are not
  3430. // extended.
  3431. return ABIArgInfo::getExtend(Ty);
  3432. case BuiltinType::LongDouble:
  3433. // Mingw64 GCC uses the old 80 bit extended precision floating point
  3434. // unit. It passes them indirectly through memory.
  3435. if (IsMingw64) {
  3436. const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
  3437. if (LDF == &llvm::APFloat::x87DoubleExtended())
  3438. return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
  3439. }
  3440. break;
  3441. case BuiltinType::Int128:
  3442. case BuiltinType::UInt128:
  3443. // If it's a parameter type, the normal ABI rule is that arguments larger
  3444. // than 8 bytes are passed indirectly. GCC follows it. We follow it too,
  3445. // even though it isn't particularly efficient.
  3446. if (!IsReturnType)
  3447. return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
  3448. // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that.
  3449. // Clang matches them for compatibility.
  3450. return ABIArgInfo::getDirect(
  3451. llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()), 2));
  3452. default:
  3453. break;
  3454. }
  3455. }
  3456. return ABIArgInfo::getDirect();
  3457. }
  3458. void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI,
  3459. unsigned FreeSSERegs,
  3460. bool IsVectorCall,
  3461. bool IsRegCall) const {
  3462. unsigned Count = 0;
  3463. for (auto &I : FI.arguments()) {
  3464. // Vectorcall in x64 only permits the first 6 arguments to be passed
  3465. // as XMM/YMM registers.
  3466. if (Count < VectorcallMaxParamNumAsReg)
  3467. I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
  3468. else {
  3469. // Since these cannot be passed in registers, pretend no registers
  3470. // are left.
  3471. unsigned ZeroSSERegsAvail = 0;
  3472. I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false,
  3473. IsVectorCall, IsRegCall);
  3474. }
  3475. ++Count;
  3476. }
  3477. for (auto &I : FI.arguments()) {
  3478. I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info);
  3479. }
  3480. }
  3481. void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  3482. const unsigned CC = FI.getCallingConvention();
  3483. bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall;
  3484. bool IsRegCall = CC == llvm::CallingConv::X86_RegCall;
  3485. // If __attribute__((sysv_abi)) is in use, use the SysV argument
  3486. // classification rules.
  3487. if (CC == llvm::CallingConv::X86_64_SysV) {
  3488. X86_64ABIInfo SysVABIInfo(CGT, AVXLevel);
  3489. SysVABIInfo.computeInfo(FI);
  3490. return;
  3491. }
  3492. unsigned FreeSSERegs = 0;
  3493. if (IsVectorCall) {
  3494. // We can use up to 4 SSE return registers with vectorcall.
  3495. FreeSSERegs = 4;
  3496. } else if (IsRegCall) {
  3497. // RegCall gives us 16 SSE registers.
  3498. FreeSSERegs = 16;
  3499. }
  3500. if (!getCXXABI().classifyReturnType(FI))
  3501. FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
  3502. IsVectorCall, IsRegCall);
  3503. if (IsVectorCall) {
  3504. // We can use up to 6 SSE register parameters with vectorcall.
  3505. FreeSSERegs = 6;
  3506. } else if (IsRegCall) {
  3507. // RegCall gives us 16 SSE registers, we can reuse the return registers.
  3508. FreeSSERegs = 16;
  3509. }
  3510. if (IsVectorCall) {
  3511. computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall);
  3512. } else {
  3513. for (auto &I : FI.arguments())
  3514. I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
  3515. }
  3516. }
  3517. Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3518. QualType Ty) const {
  3519. bool IsIndirect = false;
  3520. // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
  3521. // not 1, 2, 4, or 8 bytes, must be passed by reference."
  3522. if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) {
  3523. uint64_t Width = getContext().getTypeSize(Ty);
  3524. IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
  3525. }
  3526. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
  3527. CGF.getContext().getTypeInfoInChars(Ty),
  3528. CharUnits::fromQuantity(8),
  3529. /*allowHigherAlign*/ false);
  3530. }
  3531. // PowerPC-32
  3532. namespace {
  3533. /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
  3534. class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
  3535. bool IsSoftFloatABI;
  3536. CharUnits getParamTypeAlignment(QualType Ty) const;
  3537. public:
  3538. PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI)
  3539. : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {}
  3540. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3541. QualType Ty) const override;
  3542. };
  3543. class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
  3544. public:
  3545. PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI)
  3546. : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {}
  3547. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  3548. // This is recovered from gcc output.
  3549. return 1; // r1 is the dedicated stack pointer
  3550. }
  3551. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  3552. llvm::Value *Address) const override;
  3553. };
  3554. }
  3555. CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
  3556. // Complex types are passed just like their elements
  3557. if (const ComplexType *CTy = Ty->getAs<ComplexType>())
  3558. Ty = CTy->getElementType();
  3559. if (Ty->isVectorType())
  3560. return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16
  3561. : 4);
  3562. // For single-element float/vector structs, we consider the whole type
  3563. // to have the same alignment requirements as its single element.
  3564. const Type *AlignTy = nullptr;
  3565. if (const Type *EltType = isSingleElementStruct(Ty, getContext())) {
  3566. const BuiltinType *BT = EltType->getAs<BuiltinType>();
  3567. if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
  3568. (BT && BT->isFloatingPoint()))
  3569. AlignTy = EltType;
  3570. }
  3571. if (AlignTy)
  3572. return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4);
  3573. return CharUnits::fromQuantity(4);
  3574. }
  3575. // TODO: this implementation is now likely redundant with
  3576. // DefaultABIInfo::EmitVAArg.
  3577. Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
  3578. QualType Ty) const {
  3579. if (getTarget().getTriple().isOSDarwin()) {
  3580. auto TI = getContext().getTypeInfoInChars(Ty);
  3581. TI.second = getParamTypeAlignment(Ty);
  3582. CharUnits SlotSize = CharUnits::fromQuantity(4);
  3583. return emitVoidPtrVAArg(CGF, VAList, Ty,
  3584. classifyArgumentType(Ty).isIndirect(), TI, SlotSize,
  3585. /*AllowHigherAlign=*/true);
  3586. }
  3587. const unsigned OverflowLimit = 8;
  3588. if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
  3589. // TODO: Implement this. For now ignore.
  3590. (void)CTy;
  3591. return Address::invalid(); // FIXME?
  3592. }
  3593. // struct __va_list_tag {
  3594. // unsigned char gpr;
  3595. // unsigned char fpr;
  3596. // unsigned short reserved;
  3597. // void *overflow_arg_area;
  3598. // void *reg_save_area;
  3599. // };
  3600. bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
  3601. bool isInt =
  3602. Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
  3603. bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
  3604. // All aggregates are passed indirectly? That doesn't seem consistent
  3605. // with the argument-lowering code.
  3606. bool isIndirect = Ty->isAggregateType();
  3607. CGBuilderTy &Builder = CGF.Builder;
  3608. // The calling convention either uses 1-2 GPRs or 1 FPR.
  3609. Address NumRegsAddr = Address::invalid();
  3610. if (isInt || IsSoftFloatABI) {
  3611. NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr");
  3612. } else {
  3613. NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr");
  3614. }
  3615. llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
  3616. // "Align" the register count when TY is i64.
  3617. if (isI64 || (isF64 && IsSoftFloatABI)) {
  3618. NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
  3619. NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
  3620. }
  3621. llvm::Value *CC =
  3622. Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
  3623. llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
  3624. llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
  3625. llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
  3626. Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
  3627. llvm::Type *DirectTy = CGF.ConvertType(Ty);
  3628. if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
  3629. // Case 1: consume registers.
  3630. Address RegAddr = Address::invalid();
  3631. {
  3632. CGF.EmitBlock(UsingRegs);
  3633. Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4);
  3634. RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
  3635. CharUnits::fromQuantity(8));
  3636. assert(RegAddr.getElementType() == CGF.Int8Ty);
  3637. // Floating-point registers start after the general-purpose registers.
  3638. if (!(isInt || IsSoftFloatABI)) {
  3639. RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
  3640. CharUnits::fromQuantity(32));
  3641. }
  3642. // Get the address of the saved value by scaling the number of
  3643. // registers we've used by the number of
  3644. CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
  3645. llvm::Value *RegOffset =
  3646. Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
  3647. RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
  3648. RegAddr.getPointer(), RegOffset),
  3649. RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
  3650. RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
  3651. // Increase the used-register count.
  3652. NumRegs =
  3653. Builder.CreateAdd(NumRegs,
  3654. Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
  3655. Builder.CreateStore(NumRegs, NumRegsAddr);
  3656. CGF.EmitBranch(Cont);
  3657. }
  3658. // Case 2: consume space in the overflow area.
  3659. Address MemAddr = Address::invalid();
  3660. {
  3661. CGF.EmitBlock(UsingOverflow);
  3662. Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
  3663. // Everything in the overflow area is rounded up to a size of at least 4.
  3664. CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
  3665. CharUnits Size;
  3666. if (!isIndirect) {
  3667. auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
  3668. Size = TypeInfo.first.alignTo(OverflowAreaAlign);
  3669. } else {
  3670. Size = CGF.getPointerSize();
  3671. }
  3672. Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3);
  3673. Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
  3674. OverflowAreaAlign);
  3675. // Round up address of argument to alignment
  3676. CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
  3677. if (Align > OverflowAreaAlign) {
  3678. llvm::Value *Ptr = OverflowArea.getPointer();
  3679. OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
  3680. Align);
  3681. }
  3682. MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
  3683. // Increase the overflow area.
  3684. OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
  3685. Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
  3686. CGF.EmitBranch(Cont);
  3687. }
  3688. CGF.EmitBlock(Cont);
  3689. // Merge the cases with a phi.
  3690. Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
  3691. "vaarg.addr");
  3692. // Load the pointer if the argument was passed indirectly.
  3693. if (isIndirect) {
  3694. Result = Address(Builder.CreateLoad(Result, "aggr"),
  3695. getContext().getTypeAlignInChars(Ty));
  3696. }
  3697. return Result;
  3698. }
  3699. bool
  3700. PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  3701. llvm::Value *Address) const {
  3702. // This is calculated from the LLVM and GCC tables and verified
  3703. // against gcc output. AFAIK all ABIs use the same encoding.
  3704. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  3705. llvm::IntegerType *i8 = CGF.Int8Ty;
  3706. llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
  3707. llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
  3708. llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
  3709. // 0-31: r0-31, the 4-byte general-purpose registers
  3710. AssignToArrayRange(Builder, Address, Four8, 0, 31);
  3711. // 32-63: fp0-31, the 8-byte floating-point registers
  3712. AssignToArrayRange(Builder, Address, Eight8, 32, 63);
  3713. // 64-76 are various 4-byte special-purpose registers:
  3714. // 64: mq
  3715. // 65: lr
  3716. // 66: ctr
  3717. // 67: ap
  3718. // 68-75 cr0-7
  3719. // 76: xer
  3720. AssignToArrayRange(Builder, Address, Four8, 64, 76);
  3721. // 77-108: v0-31, the 16-byte vector registers
  3722. AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
  3723. // 109: vrsave
  3724. // 110: vscr
  3725. // 111: spe_acc
  3726. // 112: spefscr
  3727. // 113: sfp
  3728. AssignToArrayRange(Builder, Address, Four8, 109, 113);
  3729. return false;
  3730. }
  3731. // PowerPC-64
  3732. namespace {
  3733. /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
  3734. class PPC64_SVR4_ABIInfo : public SwiftABIInfo {
  3735. public:
  3736. enum ABIKind {
  3737. ELFv1 = 0,
  3738. ELFv2
  3739. };
  3740. private:
  3741. static const unsigned GPRBits = 64;
  3742. ABIKind Kind;
  3743. bool HasQPX;
  3744. bool IsSoftFloatABI;
  3745. // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
  3746. // will be passed in a QPX register.
  3747. bool IsQPXVectorTy(const Type *Ty) const {
  3748. if (!HasQPX)
  3749. return false;
  3750. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  3751. unsigned NumElements = VT->getNumElements();
  3752. if (NumElements == 1)
  3753. return false;
  3754. if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
  3755. if (getContext().getTypeSize(Ty) <= 256)
  3756. return true;
  3757. } else if (VT->getElementType()->
  3758. isSpecificBuiltinType(BuiltinType::Float)) {
  3759. if (getContext().getTypeSize(Ty) <= 128)
  3760. return true;
  3761. }
  3762. }
  3763. return false;
  3764. }
  3765. bool IsQPXVectorTy(QualType Ty) const {
  3766. return IsQPXVectorTy(Ty.getTypePtr());
  3767. }
  3768. public:
  3769. PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX,
  3770. bool SoftFloatABI)
  3771. : SwiftABIInfo(CGT), Kind(Kind), HasQPX(HasQPX),
  3772. IsSoftFloatABI(SoftFloatABI) {}
  3773. bool isPromotableTypeForABI(QualType Ty) const;
  3774. CharUnits getParamTypeAlignment(QualType Ty) const;
  3775. ABIArgInfo classifyReturnType(QualType RetTy) const;
  3776. ABIArgInfo classifyArgumentType(QualType Ty) const;
  3777. bool isHomogeneousAggregateBaseType(QualType Ty) const override;
  3778. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  3779. uint64_t Members) const override;
  3780. // TODO: We can add more logic to computeInfo to improve performance.
  3781. // Example: For aggregate arguments that fit in a register, we could
  3782. // use getDirectInReg (as is done below for structs containing a single
  3783. // floating-point value) to avoid pushing them to memory on function
  3784. // entry. This would require changing the logic in PPCISelLowering
  3785. // when lowering the parameters in the caller and args in the callee.
  3786. void computeInfo(CGFunctionInfo &FI) const override {
  3787. if (!getCXXABI().classifyReturnType(FI))
  3788. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  3789. for (auto &I : FI.arguments()) {
  3790. // We rely on the default argument classification for the most part.
  3791. // One exception: An aggregate containing a single floating-point
  3792. // or vector item must be passed in a register if one is available.
  3793. const Type *T = isSingleElementStruct(I.type, getContext());
  3794. if (T) {
  3795. const BuiltinType *BT = T->getAs<BuiltinType>();
  3796. if (IsQPXVectorTy(T) ||
  3797. (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
  3798. (BT && BT->isFloatingPoint())) {
  3799. QualType QT(T, 0);
  3800. I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
  3801. continue;
  3802. }
  3803. }
  3804. I.info = classifyArgumentType(I.type);
  3805. }
  3806. }
  3807. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3808. QualType Ty) const override;
  3809. bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
  3810. bool asReturnValue) const override {
  3811. return occupiesMoreThan(CGT, scalars, /*total*/ 4);
  3812. }
  3813. bool isSwiftErrorInRegister() const override {
  3814. return false;
  3815. }
  3816. };
  3817. class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
  3818. public:
  3819. PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
  3820. PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX,
  3821. bool SoftFloatABI)
  3822. : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX,
  3823. SoftFloatABI)) {}
  3824. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  3825. // This is recovered from gcc output.
  3826. return 1; // r1 is the dedicated stack pointer
  3827. }
  3828. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  3829. llvm::Value *Address) const override;
  3830. };
  3831. class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
  3832. public:
  3833. PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
  3834. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  3835. // This is recovered from gcc output.
  3836. return 1; // r1 is the dedicated stack pointer
  3837. }
  3838. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  3839. llvm::Value *Address) const override;
  3840. };
  3841. }
  3842. // Return true if the ABI requires Ty to be passed sign- or zero-
  3843. // extended to 64 bits.
  3844. bool
  3845. PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
  3846. // Treat an enum type as its underlying type.
  3847. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  3848. Ty = EnumTy->getDecl()->getIntegerType();
  3849. // Promotable integer types are required to be promoted by the ABI.
  3850. if (Ty->isPromotableIntegerType())
  3851. return true;
  3852. // In addition to the usual promotable integer types, we also need to
  3853. // extend all 32-bit types, since the ABI requires promotion to 64 bits.
  3854. if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
  3855. switch (BT->getKind()) {
  3856. case BuiltinType::Int:
  3857. case BuiltinType::UInt:
  3858. return true;
  3859. default:
  3860. break;
  3861. }
  3862. return false;
  3863. }
  3864. /// isAlignedParamType - Determine whether a type requires 16-byte or
  3865. /// higher alignment in the parameter area. Always returns at least 8.
  3866. CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
  3867. // Complex types are passed just like their elements.
  3868. if (const ComplexType *CTy = Ty->getAs<ComplexType>())
  3869. Ty = CTy->getElementType();
  3870. // Only vector types of size 16 bytes need alignment (larger types are
  3871. // passed via reference, smaller types are not aligned).
  3872. if (IsQPXVectorTy(Ty)) {
  3873. if (getContext().getTypeSize(Ty) > 128)
  3874. return CharUnits::fromQuantity(32);
  3875. return CharUnits::fromQuantity(16);
  3876. } else if (Ty->isVectorType()) {
  3877. return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
  3878. }
  3879. // For single-element float/vector structs, we consider the whole type
  3880. // to have the same alignment requirements as its single element.
  3881. const Type *AlignAsType = nullptr;
  3882. const Type *EltType = isSingleElementStruct(Ty, getContext());
  3883. if (EltType) {
  3884. const BuiltinType *BT = EltType->getAs<BuiltinType>();
  3885. if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
  3886. getContext().getTypeSize(EltType) == 128) ||
  3887. (BT && BT->isFloatingPoint()))
  3888. AlignAsType = EltType;
  3889. }
  3890. // Likewise for ELFv2 homogeneous aggregates.
  3891. const Type *Base = nullptr;
  3892. uint64_t Members = 0;
  3893. if (!AlignAsType && Kind == ELFv2 &&
  3894. isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
  3895. AlignAsType = Base;
  3896. // With special case aggregates, only vector base types need alignment.
  3897. if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
  3898. if (getContext().getTypeSize(AlignAsType) > 128)
  3899. return CharUnits::fromQuantity(32);
  3900. return CharUnits::fromQuantity(16);
  3901. } else if (AlignAsType) {
  3902. return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
  3903. }
  3904. // Otherwise, we only need alignment for any aggregate type that
  3905. // has an alignment requirement of >= 16 bytes.
  3906. if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
  3907. if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
  3908. return CharUnits::fromQuantity(32);
  3909. return CharUnits::fromQuantity(16);
  3910. }
  3911. return CharUnits::fromQuantity(8);
  3912. }
  3913. /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
  3914. /// aggregate. Base is set to the base element type, and Members is set
  3915. /// to the number of base elements.
  3916. bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
  3917. uint64_t &Members) const {
  3918. if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
  3919. uint64_t NElements = AT->getSize().getZExtValue();
  3920. if (NElements == 0)
  3921. return false;
  3922. if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
  3923. return false;
  3924. Members *= NElements;
  3925. } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
  3926. const RecordDecl *RD = RT->getDecl();
  3927. if (RD->hasFlexibleArrayMember())
  3928. return false;
  3929. Members = 0;
  3930. // If this is a C++ record, check the bases first.
  3931. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  3932. for (const auto &I : CXXRD->bases()) {
  3933. // Ignore empty records.
  3934. if (isEmptyRecord(getContext(), I.getType(), true))
  3935. continue;
  3936. uint64_t FldMembers;
  3937. if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
  3938. return false;
  3939. Members += FldMembers;
  3940. }
  3941. }
  3942. for (const auto *FD : RD->fields()) {
  3943. // Ignore (non-zero arrays of) empty records.
  3944. QualType FT = FD->getType();
  3945. while (const ConstantArrayType *AT =
  3946. getContext().getAsConstantArrayType(FT)) {
  3947. if (AT->getSize().getZExtValue() == 0)
  3948. return false;
  3949. FT = AT->getElementType();
  3950. }
  3951. if (isEmptyRecord(getContext(), FT, true))
  3952. continue;
  3953. // For compatibility with GCC, ignore empty bitfields in C++ mode.
  3954. if (getContext().getLangOpts().CPlusPlus &&
  3955. FD->isZeroLengthBitField(getContext()))
  3956. continue;
  3957. uint64_t FldMembers;
  3958. if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
  3959. return false;
  3960. Members = (RD->isUnion() ?
  3961. std::max(Members, FldMembers) : Members + FldMembers);
  3962. }
  3963. if (!Base)
  3964. return false;
  3965. // Ensure there is no padding.
  3966. if (getContext().getTypeSize(Base) * Members !=
  3967. getContext().getTypeSize(Ty))
  3968. return false;
  3969. } else {
  3970. Members = 1;
  3971. if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
  3972. Members = 2;
  3973. Ty = CT->getElementType();
  3974. }
  3975. // Most ABIs only support float, double, and some vector type widths.
  3976. if (!isHomogeneousAggregateBaseType(Ty))
  3977. return false;
  3978. // The base type must be the same for all members. Types that
  3979. // agree in both total size and mode (float vs. vector) are
  3980. // treated as being equivalent here.
  3981. const Type *TyPtr = Ty.getTypePtr();
  3982. if (!Base) {
  3983. Base = TyPtr;
  3984. // If it's a non-power-of-2 vector, its size is already a power-of-2,
  3985. // so make sure to widen it explicitly.
  3986. if (const VectorType *VT = Base->getAs<VectorType>()) {
  3987. QualType EltTy = VT->getElementType();
  3988. unsigned NumElements =
  3989. getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
  3990. Base = getContext()
  3991. .getVectorType(EltTy, NumElements, VT->getVectorKind())
  3992. .getTypePtr();
  3993. }
  3994. }
  3995. if (Base->isVectorType() != TyPtr->isVectorType() ||
  3996. getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
  3997. return false;
  3998. }
  3999. return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
  4000. }
  4001. bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  4002. // Homogeneous aggregates for ELFv2 must have base types of float,
  4003. // double, long double, or 128-bit vectors.
  4004. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  4005. if (BT->getKind() == BuiltinType::Float ||
  4006. BT->getKind() == BuiltinType::Double ||
  4007. BT->getKind() == BuiltinType::LongDouble ||
  4008. (getContext().getTargetInfo().hasFloat128Type() &&
  4009. (BT->getKind() == BuiltinType::Float128))) {
  4010. if (IsSoftFloatABI)
  4011. return false;
  4012. return true;
  4013. }
  4014. }
  4015. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  4016. if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
  4017. return true;
  4018. }
  4019. return false;
  4020. }
  4021. bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
  4022. const Type *Base, uint64_t Members) const {
  4023. // Vector and fp128 types require one register, other floating point types
  4024. // require one or two registers depending on their size.
  4025. uint32_t NumRegs =
  4026. ((getContext().getTargetInfo().hasFloat128Type() &&
  4027. Base->isFloat128Type()) ||
  4028. Base->isVectorType()) ? 1
  4029. : (getContext().getTypeSize(Base) + 63) / 64;
  4030. // Homogeneous Aggregates may occupy at most 8 registers.
  4031. return Members * NumRegs <= 8;
  4032. }
  4033. ABIArgInfo
  4034. PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
  4035. Ty = useFirstFieldIfTransparentUnion(Ty);
  4036. if (Ty->isAnyComplexType())
  4037. return ABIArgInfo::getDirect();
  4038. // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
  4039. // or via reference (larger than 16 bytes).
  4040. if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
  4041. uint64_t Size = getContext().getTypeSize(Ty);
  4042. if (Size > 128)
  4043. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  4044. else if (Size < 128) {
  4045. llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
  4046. return ABIArgInfo::getDirect(CoerceTy);
  4047. }
  4048. }
  4049. if (isAggregateTypeForABI(Ty)) {
  4050. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  4051. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  4052. uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
  4053. uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
  4054. // ELFv2 homogeneous aggregates are passed as array types.
  4055. const Type *Base = nullptr;
  4056. uint64_t Members = 0;
  4057. if (Kind == ELFv2 &&
  4058. isHomogeneousAggregate(Ty, Base, Members)) {
  4059. llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
  4060. llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
  4061. return ABIArgInfo::getDirect(CoerceTy);
  4062. }
  4063. // If an aggregate may end up fully in registers, we do not
  4064. // use the ByVal method, but pass the aggregate as array.
  4065. // This is usually beneficial since we avoid forcing the
  4066. // back-end to store the argument to memory.
  4067. uint64_t Bits = getContext().getTypeSize(Ty);
  4068. if (Bits > 0 && Bits <= 8 * GPRBits) {
  4069. llvm::Type *CoerceTy;
  4070. // Types up to 8 bytes are passed as integer type (which will be
  4071. // properly aligned in the argument save area doubleword).
  4072. if (Bits <= GPRBits)
  4073. CoerceTy =
  4074. llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
  4075. // Larger types are passed as arrays, with the base type selected
  4076. // according to the required alignment in the save area.
  4077. else {
  4078. uint64_t RegBits = ABIAlign * 8;
  4079. uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
  4080. llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
  4081. CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
  4082. }
  4083. return ABIArgInfo::getDirect(CoerceTy);
  4084. }
  4085. // All other aggregates are passed ByVal.
  4086. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
  4087. /*ByVal=*/true,
  4088. /*Realign=*/TyAlign > ABIAlign);
  4089. }
  4090. return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
  4091. : ABIArgInfo::getDirect());
  4092. }
  4093. ABIArgInfo
  4094. PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
  4095. if (RetTy->isVoidType())
  4096. return ABIArgInfo::getIgnore();
  4097. if (RetTy->isAnyComplexType())
  4098. return ABIArgInfo::getDirect();
  4099. // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
  4100. // or via reference (larger than 16 bytes).
  4101. if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
  4102. uint64_t Size = getContext().getTypeSize(RetTy);
  4103. if (Size > 128)
  4104. return getNaturalAlignIndirect(RetTy);
  4105. else if (Size < 128) {
  4106. llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
  4107. return ABIArgInfo::getDirect(CoerceTy);
  4108. }
  4109. }
  4110. if (isAggregateTypeForABI(RetTy)) {
  4111. // ELFv2 homogeneous aggregates are returned as array types.
  4112. const Type *Base = nullptr;
  4113. uint64_t Members = 0;
  4114. if (Kind == ELFv2 &&
  4115. isHomogeneousAggregate(RetTy, Base, Members)) {
  4116. llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
  4117. llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
  4118. return ABIArgInfo::getDirect(CoerceTy);
  4119. }
  4120. // ELFv2 small aggregates are returned in up to two registers.
  4121. uint64_t Bits = getContext().getTypeSize(RetTy);
  4122. if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
  4123. if (Bits == 0)
  4124. return ABIArgInfo::getIgnore();
  4125. llvm::Type *CoerceTy;
  4126. if (Bits > GPRBits) {
  4127. CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
  4128. CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
  4129. } else
  4130. CoerceTy =
  4131. llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
  4132. return ABIArgInfo::getDirect(CoerceTy);
  4133. }
  4134. // All other aggregates are returned indirectly.
  4135. return getNaturalAlignIndirect(RetTy);
  4136. }
  4137. return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
  4138. : ABIArgInfo::getDirect());
  4139. }
  4140. // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
  4141. Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4142. QualType Ty) const {
  4143. auto TypeInfo = getContext().getTypeInfoInChars(Ty);
  4144. TypeInfo.second = getParamTypeAlignment(Ty);
  4145. CharUnits SlotSize = CharUnits::fromQuantity(8);
  4146. // If we have a complex type and the base type is smaller than 8 bytes,
  4147. // the ABI calls for the real and imaginary parts to be right-adjusted
  4148. // in separate doublewords. However, Clang expects us to produce a
  4149. // pointer to a structure with the two parts packed tightly. So generate
  4150. // loads of the real and imaginary parts relative to the va_list pointer,
  4151. // and store them to a temporary structure.
  4152. if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
  4153. CharUnits EltSize = TypeInfo.first / 2;
  4154. if (EltSize < SlotSize) {
  4155. Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
  4156. SlotSize * 2, SlotSize,
  4157. SlotSize, /*AllowHigher*/ true);
  4158. Address RealAddr = Addr;
  4159. Address ImagAddr = RealAddr;
  4160. if (CGF.CGM.getDataLayout().isBigEndian()) {
  4161. RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
  4162. SlotSize - EltSize);
  4163. ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
  4164. 2 * SlotSize - EltSize);
  4165. } else {
  4166. ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
  4167. }
  4168. llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
  4169. RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
  4170. ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
  4171. llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
  4172. llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
  4173. Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
  4174. CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
  4175. /*init*/ true);
  4176. return Temp;
  4177. }
  4178. }
  4179. // Otherwise, just use the general rule.
  4180. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
  4181. TypeInfo, SlotSize, /*AllowHigher*/ true);
  4182. }
  4183. static bool
  4184. PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  4185. llvm::Value *Address) {
  4186. // This is calculated from the LLVM and GCC tables and verified
  4187. // against gcc output. AFAIK all ABIs use the same encoding.
  4188. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  4189. llvm::IntegerType *i8 = CGF.Int8Ty;
  4190. llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
  4191. llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
  4192. llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
  4193. // 0-31: r0-31, the 8-byte general-purpose registers
  4194. AssignToArrayRange(Builder, Address, Eight8, 0, 31);
  4195. // 32-63: fp0-31, the 8-byte floating-point registers
  4196. AssignToArrayRange(Builder, Address, Eight8, 32, 63);
  4197. // 64-67 are various 8-byte special-purpose registers:
  4198. // 64: mq
  4199. // 65: lr
  4200. // 66: ctr
  4201. // 67: ap
  4202. AssignToArrayRange(Builder, Address, Eight8, 64, 67);
  4203. // 68-76 are various 4-byte special-purpose registers:
  4204. // 68-75 cr0-7
  4205. // 76: xer
  4206. AssignToArrayRange(Builder, Address, Four8, 68, 76);
  4207. // 77-108: v0-31, the 16-byte vector registers
  4208. AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
  4209. // 109: vrsave
  4210. // 110: vscr
  4211. // 111: spe_acc
  4212. // 112: spefscr
  4213. // 113: sfp
  4214. // 114: tfhar
  4215. // 115: tfiar
  4216. // 116: texasr
  4217. AssignToArrayRange(Builder, Address, Eight8, 109, 116);
  4218. return false;
  4219. }
  4220. bool
  4221. PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
  4222. CodeGen::CodeGenFunction &CGF,
  4223. llvm::Value *Address) const {
  4224. return PPC64_initDwarfEHRegSizeTable(CGF, Address);
  4225. }
  4226. bool
  4227. PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  4228. llvm::Value *Address) const {
  4229. return PPC64_initDwarfEHRegSizeTable(CGF, Address);
  4230. }
  4231. //===----------------------------------------------------------------------===//
  4232. // AArch64 ABI Implementation
  4233. //===----------------------------------------------------------------------===//
  4234. namespace {
  4235. class AArch64ABIInfo : public SwiftABIInfo {
  4236. public:
  4237. enum ABIKind {
  4238. AAPCS = 0,
  4239. DarwinPCS,
  4240. Win64
  4241. };
  4242. private:
  4243. ABIKind Kind;
  4244. public:
  4245. AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
  4246. : SwiftABIInfo(CGT), Kind(Kind) {}
  4247. private:
  4248. ABIKind getABIKind() const { return Kind; }
  4249. bool isDarwinPCS() const { return Kind == DarwinPCS; }
  4250. ABIArgInfo classifyReturnType(QualType RetTy) const;
  4251. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  4252. bool isHomogeneousAggregateBaseType(QualType Ty) const override;
  4253. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  4254. uint64_t Members) const override;
  4255. bool isIllegalVectorType(QualType Ty) const;
  4256. void computeInfo(CGFunctionInfo &FI) const override {
  4257. if (!::classifyReturnType(getCXXABI(), FI, *this))
  4258. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  4259. for (auto &it : FI.arguments())
  4260. it.info = classifyArgumentType(it.type);
  4261. }
  4262. Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
  4263. CodeGenFunction &CGF) const;
  4264. Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
  4265. CodeGenFunction &CGF) const;
  4266. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4267. QualType Ty) const override {
  4268. return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty)
  4269. : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
  4270. : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
  4271. }
  4272. Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4273. QualType Ty) const override;
  4274. bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
  4275. bool asReturnValue) const override {
  4276. return occupiesMoreThan(CGT, scalars, /*total*/ 4);
  4277. }
  4278. bool isSwiftErrorInRegister() const override {
  4279. return true;
  4280. }
  4281. bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
  4282. unsigned elts) const override;
  4283. };
  4284. class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
  4285. public:
  4286. AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
  4287. : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
  4288. StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
  4289. return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue";
  4290. }
  4291. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  4292. return 31;
  4293. }
  4294. bool doesReturnSlotInterfereWithArgs() const override { return false; }
  4295. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  4296. CodeGen::CodeGenModule &CGM) const override {
  4297. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  4298. if (!FD)
  4299. return;
  4300. llvm::Function *Fn = cast<llvm::Function>(GV);
  4301. auto Kind = CGM.getCodeGenOpts().getSignReturnAddress();
  4302. if (Kind != CodeGenOptions::SignReturnAddressScope::None) {
  4303. Fn->addFnAttr("sign-return-address",
  4304. Kind == CodeGenOptions::SignReturnAddressScope::All
  4305. ? "all"
  4306. : "non-leaf");
  4307. auto Key = CGM.getCodeGenOpts().getSignReturnAddressKey();
  4308. Fn->addFnAttr("sign-return-address-key",
  4309. Key == CodeGenOptions::SignReturnAddressKeyValue::AKey
  4310. ? "a_key"
  4311. : "b_key");
  4312. }
  4313. if (CGM.getCodeGenOpts().BranchTargetEnforcement)
  4314. Fn->addFnAttr("branch-target-enforcement");
  4315. }
  4316. };
  4317. class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo {
  4318. public:
  4319. WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K)
  4320. : AArch64TargetCodeGenInfo(CGT, K) {}
  4321. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  4322. CodeGen::CodeGenModule &CGM) const override;
  4323. void getDependentLibraryOption(llvm::StringRef Lib,
  4324. llvm::SmallString<24> &Opt) const override {
  4325. Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
  4326. }
  4327. void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
  4328. llvm::SmallString<32> &Opt) const override {
  4329. Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
  4330. }
  4331. };
  4332. void WindowsAArch64TargetCodeGenInfo::setTargetAttributes(
  4333. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
  4334. AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
  4335. if (GV->isDeclaration())
  4336. return;
  4337. addStackProbeTargetAttributes(D, GV, CGM);
  4338. }
  4339. }
  4340. ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
  4341. Ty = useFirstFieldIfTransparentUnion(Ty);
  4342. // Handle illegal vector types here.
  4343. if (isIllegalVectorType(Ty)) {
  4344. uint64_t Size = getContext().getTypeSize(Ty);
  4345. // Android promotes <2 x i8> to i16, not i32
  4346. if (isAndroid() && (Size <= 16)) {
  4347. llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
  4348. return ABIArgInfo::getDirect(ResType);
  4349. }
  4350. if (Size <= 32) {
  4351. llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
  4352. return ABIArgInfo::getDirect(ResType);
  4353. }
  4354. if (Size == 64) {
  4355. llvm::Type *ResType =
  4356. llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
  4357. return ABIArgInfo::getDirect(ResType);
  4358. }
  4359. if (Size == 128) {
  4360. llvm::Type *ResType =
  4361. llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
  4362. return ABIArgInfo::getDirect(ResType);
  4363. }
  4364. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  4365. }
  4366. if (!isAggregateTypeForABI(Ty)) {
  4367. // Treat an enum type as its underlying type.
  4368. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  4369. Ty = EnumTy->getDecl()->getIntegerType();
  4370. return (Ty->isPromotableIntegerType() && isDarwinPCS()
  4371. ? ABIArgInfo::getExtend(Ty)
  4372. : ABIArgInfo::getDirect());
  4373. }
  4374. // Structures with either a non-trivial destructor or a non-trivial
  4375. // copy constructor are always indirect.
  4376. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  4377. return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
  4378. CGCXXABI::RAA_DirectInMemory);
  4379. }
  4380. // Empty records are always ignored on Darwin, but actually passed in C++ mode
  4381. // elsewhere for GNU compatibility.
  4382. uint64_t Size = getContext().getTypeSize(Ty);
  4383. bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
  4384. if (IsEmpty || Size == 0) {
  4385. if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
  4386. return ABIArgInfo::getIgnore();
  4387. // GNU C mode. The only argument that gets ignored is an empty one with size
  4388. // 0.
  4389. if (IsEmpty && Size == 0)
  4390. return ABIArgInfo::getIgnore();
  4391. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  4392. }
  4393. // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
  4394. const Type *Base = nullptr;
  4395. uint64_t Members = 0;
  4396. if (isHomogeneousAggregate(Ty, Base, Members)) {
  4397. return ABIArgInfo::getDirect(
  4398. llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
  4399. }
  4400. // Aggregates <= 16 bytes are passed directly in registers or on the stack.
  4401. if (Size <= 128) {
  4402. // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
  4403. // same size and alignment.
  4404. if (getTarget().isRenderScriptTarget()) {
  4405. return coerceToIntArray(Ty, getContext(), getVMContext());
  4406. }
  4407. unsigned Alignment;
  4408. if (Kind == AArch64ABIInfo::AAPCS) {
  4409. Alignment = getContext().getTypeUnadjustedAlign(Ty);
  4410. Alignment = Alignment < 128 ? 64 : 128;
  4411. } else {
  4412. Alignment = getContext().getTypeAlign(Ty);
  4413. }
  4414. Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
  4415. // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
  4416. // For aggregates with 16-byte alignment, we use i128.
  4417. if (Alignment < 128 && Size == 128) {
  4418. llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
  4419. return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
  4420. }
  4421. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
  4422. }
  4423. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  4424. }
  4425. ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const {
  4426. if (RetTy->isVoidType())
  4427. return ABIArgInfo::getIgnore();
  4428. // Large vector types should be returned via memory.
  4429. if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
  4430. return getNaturalAlignIndirect(RetTy);
  4431. if (!isAggregateTypeForABI(RetTy)) {
  4432. // Treat an enum type as its underlying type.
  4433. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  4434. RetTy = EnumTy->getDecl()->getIntegerType();
  4435. return (RetTy->isPromotableIntegerType() && isDarwinPCS()
  4436. ? ABIArgInfo::getExtend(RetTy)
  4437. : ABIArgInfo::getDirect());
  4438. }
  4439. uint64_t Size = getContext().getTypeSize(RetTy);
  4440. if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
  4441. return ABIArgInfo::getIgnore();
  4442. const Type *Base = nullptr;
  4443. uint64_t Members = 0;
  4444. if (isHomogeneousAggregate(RetTy, Base, Members))
  4445. // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
  4446. return ABIArgInfo::getDirect();
  4447. // Aggregates <= 16 bytes are returned directly in registers or on the stack.
  4448. if (Size <= 128) {
  4449. // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
  4450. // same size and alignment.
  4451. if (getTarget().isRenderScriptTarget()) {
  4452. return coerceToIntArray(RetTy, getContext(), getVMContext());
  4453. }
  4454. unsigned Alignment = getContext().getTypeAlign(RetTy);
  4455. Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
  4456. // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
  4457. // For aggregates with 16-byte alignment, we use i128.
  4458. if (Alignment < 128 && Size == 128) {
  4459. llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
  4460. return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
  4461. }
  4462. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
  4463. }
  4464. return getNaturalAlignIndirect(RetTy);
  4465. }
  4466. /// isIllegalVectorType - check whether the vector type is legal for AArch64.
  4467. bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
  4468. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  4469. // Check whether VT is legal.
  4470. unsigned NumElements = VT->getNumElements();
  4471. uint64_t Size = getContext().getTypeSize(VT);
  4472. // NumElements should be power of 2.
  4473. if (!llvm::isPowerOf2_32(NumElements))
  4474. return true;
  4475. return Size != 64 && (Size != 128 || NumElements == 1);
  4476. }
  4477. return false;
  4478. }
  4479. bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize,
  4480. llvm::Type *eltTy,
  4481. unsigned elts) const {
  4482. if (!llvm::isPowerOf2_32(elts))
  4483. return false;
  4484. if (totalSize.getQuantity() != 8 &&
  4485. (totalSize.getQuantity() != 16 || elts == 1))
  4486. return false;
  4487. return true;
  4488. }
  4489. bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  4490. // Homogeneous aggregates for AAPCS64 must have base types of a floating
  4491. // point type or a short-vector type. This is the same as the 32-bit ABI,
  4492. // but with the difference that any floating-point type is allowed,
  4493. // including __fp16.
  4494. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  4495. if (BT->isFloatingPoint())
  4496. return true;
  4497. } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
  4498. unsigned VecSize = getContext().getTypeSize(VT);
  4499. if (VecSize == 64 || VecSize == 128)
  4500. return true;
  4501. }
  4502. return false;
  4503. }
  4504. bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
  4505. uint64_t Members) const {
  4506. return Members <= 4;
  4507. }
  4508. Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr,
  4509. QualType Ty,
  4510. CodeGenFunction &CGF) const {
  4511. ABIArgInfo AI = classifyArgumentType(Ty);
  4512. bool IsIndirect = AI.isIndirect();
  4513. llvm::Type *BaseTy = CGF.ConvertType(Ty);
  4514. if (IsIndirect)
  4515. BaseTy = llvm::PointerType::getUnqual(BaseTy);
  4516. else if (AI.getCoerceToType())
  4517. BaseTy = AI.getCoerceToType();
  4518. unsigned NumRegs = 1;
  4519. if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
  4520. BaseTy = ArrTy->getElementType();
  4521. NumRegs = ArrTy->getNumElements();
  4522. }
  4523. bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
  4524. // The AArch64 va_list type and handling is specified in the Procedure Call
  4525. // Standard, section B.4:
  4526. //
  4527. // struct {
  4528. // void *__stack;
  4529. // void *__gr_top;
  4530. // void *__vr_top;
  4531. // int __gr_offs;
  4532. // int __vr_offs;
  4533. // };
  4534. llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
  4535. llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
  4536. llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
  4537. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
  4538. CharUnits TySize = getContext().getTypeSizeInChars(Ty);
  4539. CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty);
  4540. Address reg_offs_p = Address::invalid();
  4541. llvm::Value *reg_offs = nullptr;
  4542. int reg_top_index;
  4543. int RegSize = IsIndirect ? 8 : TySize.getQuantity();
  4544. if (!IsFPR) {
  4545. // 3 is the field number of __gr_offs
  4546. reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
  4547. reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
  4548. reg_top_index = 1; // field number for __gr_top
  4549. RegSize = llvm::alignTo(RegSize, 8);
  4550. } else {
  4551. // 4 is the field number of __vr_offs.
  4552. reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
  4553. reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
  4554. reg_top_index = 2; // field number for __vr_top
  4555. RegSize = 16 * NumRegs;
  4556. }
  4557. //=======================================
  4558. // Find out where argument was passed
  4559. //=======================================
  4560. // If reg_offs >= 0 we're already using the stack for this type of
  4561. // argument. We don't want to keep updating reg_offs (in case it overflows,
  4562. // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
  4563. // whatever they get).
  4564. llvm::Value *UsingStack = nullptr;
  4565. UsingStack = CGF.Builder.CreateICmpSGE(
  4566. reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
  4567. CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
  4568. // Otherwise, at least some kind of argument could go in these registers, the
  4569. // question is whether this particular type is too big.
  4570. CGF.EmitBlock(MaybeRegBlock);
  4571. // Integer arguments may need to correct register alignment (for example a
  4572. // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
  4573. // align __gr_offs to calculate the potential address.
  4574. if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
  4575. int Align = TyAlign.getQuantity();
  4576. reg_offs = CGF.Builder.CreateAdd(
  4577. reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
  4578. "align_regoffs");
  4579. reg_offs = CGF.Builder.CreateAnd(
  4580. reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
  4581. "aligned_regoffs");
  4582. }
  4583. // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
  4584. // The fact that this is done unconditionally reflects the fact that
  4585. // allocating an argument to the stack also uses up all the remaining
  4586. // registers of the appropriate kind.
  4587. llvm::Value *NewOffset = nullptr;
  4588. NewOffset = CGF.Builder.CreateAdd(
  4589. reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
  4590. CGF.Builder.CreateStore(NewOffset, reg_offs_p);
  4591. // Now we're in a position to decide whether this argument really was in
  4592. // registers or not.
  4593. llvm::Value *InRegs = nullptr;
  4594. InRegs = CGF.Builder.CreateICmpSLE(
  4595. NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
  4596. CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
  4597. //=======================================
  4598. // Argument was in registers
  4599. //=======================================
  4600. // Now we emit the code for if the argument was originally passed in
  4601. // registers. First start the appropriate block:
  4602. CGF.EmitBlock(InRegBlock);
  4603. llvm::Value *reg_top = nullptr;
  4604. Address reg_top_p =
  4605. CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
  4606. reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
  4607. Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs),
  4608. CharUnits::fromQuantity(IsFPR ? 16 : 8));
  4609. Address RegAddr = Address::invalid();
  4610. llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
  4611. if (IsIndirect) {
  4612. // If it's been passed indirectly (actually a struct), whatever we find from
  4613. // stored registers or on the stack will actually be a struct **.
  4614. MemTy = llvm::PointerType::getUnqual(MemTy);
  4615. }
  4616. const Type *Base = nullptr;
  4617. uint64_t NumMembers = 0;
  4618. bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
  4619. if (IsHFA && NumMembers > 1) {
  4620. // Homogeneous aggregates passed in registers will have their elements split
  4621. // and stored 16-bytes apart regardless of size (they're notionally in qN,
  4622. // qN+1, ...). We reload and store into a temporary local variable
  4623. // contiguously.
  4624. assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
  4625. auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
  4626. llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
  4627. llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
  4628. Address Tmp = CGF.CreateTempAlloca(HFATy,
  4629. std::max(TyAlign, BaseTyInfo.second));
  4630. // On big-endian platforms, the value will be right-aligned in its slot.
  4631. int Offset = 0;
  4632. if (CGF.CGM.getDataLayout().isBigEndian() &&
  4633. BaseTyInfo.first.getQuantity() < 16)
  4634. Offset = 16 - BaseTyInfo.first.getQuantity();
  4635. for (unsigned i = 0; i < NumMembers; ++i) {
  4636. CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
  4637. Address LoadAddr =
  4638. CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
  4639. LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
  4640. Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i);
  4641. llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
  4642. CGF.Builder.CreateStore(Elem, StoreAddr);
  4643. }
  4644. RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
  4645. } else {
  4646. // Otherwise the object is contiguous in memory.
  4647. // It might be right-aligned in its slot.
  4648. CharUnits SlotSize = BaseAddr.getAlignment();
  4649. if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
  4650. (IsHFA || !isAggregateTypeForABI(Ty)) &&
  4651. TySize < SlotSize) {
  4652. CharUnits Offset = SlotSize - TySize;
  4653. BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
  4654. }
  4655. RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
  4656. }
  4657. CGF.EmitBranch(ContBlock);
  4658. //=======================================
  4659. // Argument was on the stack
  4660. //=======================================
  4661. CGF.EmitBlock(OnStackBlock);
  4662. Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
  4663. llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
  4664. // Again, stack arguments may need realignment. In this case both integer and
  4665. // floating-point ones might be affected.
  4666. if (!IsIndirect && TyAlign.getQuantity() > 8) {
  4667. int Align = TyAlign.getQuantity();
  4668. OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
  4669. OnStackPtr = CGF.Builder.CreateAdd(
  4670. OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
  4671. "align_stack");
  4672. OnStackPtr = CGF.Builder.CreateAnd(
  4673. OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
  4674. "align_stack");
  4675. OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
  4676. }
  4677. Address OnStackAddr(OnStackPtr,
  4678. std::max(CharUnits::fromQuantity(8), TyAlign));
  4679. // All stack slots are multiples of 8 bytes.
  4680. CharUnits StackSlotSize = CharUnits::fromQuantity(8);
  4681. CharUnits StackSize;
  4682. if (IsIndirect)
  4683. StackSize = StackSlotSize;
  4684. else
  4685. StackSize = TySize.alignTo(StackSlotSize);
  4686. llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
  4687. llvm::Value *NewStack =
  4688. CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack");
  4689. // Write the new value of __stack for the next call to va_arg
  4690. CGF.Builder.CreateStore(NewStack, stack_p);
  4691. if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
  4692. TySize < StackSlotSize) {
  4693. CharUnits Offset = StackSlotSize - TySize;
  4694. OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
  4695. }
  4696. OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
  4697. CGF.EmitBranch(ContBlock);
  4698. //=======================================
  4699. // Tidy up
  4700. //=======================================
  4701. CGF.EmitBlock(ContBlock);
  4702. Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
  4703. OnStackAddr, OnStackBlock, "vaargs.addr");
  4704. if (IsIndirect)
  4705. return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
  4706. TyAlign);
  4707. return ResAddr;
  4708. }
  4709. Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
  4710. CodeGenFunction &CGF) const {
  4711. // The backend's lowering doesn't support va_arg for aggregates or
  4712. // illegal vector types. Lower VAArg here for these cases and use
  4713. // the LLVM va_arg instruction for everything else.
  4714. if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
  4715. return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
  4716. CharUnits SlotSize = CharUnits::fromQuantity(8);
  4717. // Empty records are ignored for parameter passing purposes.
  4718. if (isEmptyRecord(getContext(), Ty, true)) {
  4719. Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
  4720. Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
  4721. return Addr;
  4722. }
  4723. // The size of the actual thing passed, which might end up just
  4724. // being a pointer for indirect types.
  4725. auto TyInfo = getContext().getTypeInfoInChars(Ty);
  4726. // Arguments bigger than 16 bytes which aren't homogeneous
  4727. // aggregates should be passed indirectly.
  4728. bool IsIndirect = false;
  4729. if (TyInfo.first.getQuantity() > 16) {
  4730. const Type *Base = nullptr;
  4731. uint64_t Members = 0;
  4732. IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
  4733. }
  4734. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
  4735. TyInfo, SlotSize, /*AllowHigherAlign*/ true);
  4736. }
  4737. Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4738. QualType Ty) const {
  4739. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
  4740. CGF.getContext().getTypeInfoInChars(Ty),
  4741. CharUnits::fromQuantity(8),
  4742. /*allowHigherAlign*/ false);
  4743. }
  4744. //===----------------------------------------------------------------------===//
  4745. // ARM ABI Implementation
  4746. //===----------------------------------------------------------------------===//
  4747. namespace {
  4748. class ARMABIInfo : public SwiftABIInfo {
  4749. public:
  4750. enum ABIKind {
  4751. APCS = 0,
  4752. AAPCS = 1,
  4753. AAPCS_VFP = 2,
  4754. AAPCS16_VFP = 3,
  4755. };
  4756. private:
  4757. ABIKind Kind;
  4758. public:
  4759. ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
  4760. : SwiftABIInfo(CGT), Kind(_Kind) {
  4761. setCCs();
  4762. }
  4763. bool isEABI() const {
  4764. switch (getTarget().getTriple().getEnvironment()) {
  4765. case llvm::Triple::Android:
  4766. case llvm::Triple::EABI:
  4767. case llvm::Triple::EABIHF:
  4768. case llvm::Triple::GNUEABI:
  4769. case llvm::Triple::GNUEABIHF:
  4770. case llvm::Triple::MuslEABI:
  4771. case llvm::Triple::MuslEABIHF:
  4772. return true;
  4773. default:
  4774. return false;
  4775. }
  4776. }
  4777. bool isEABIHF() const {
  4778. switch (getTarget().getTriple().getEnvironment()) {
  4779. case llvm::Triple::EABIHF:
  4780. case llvm::Triple::GNUEABIHF:
  4781. case llvm::Triple::MuslEABIHF:
  4782. return true;
  4783. default:
  4784. return false;
  4785. }
  4786. }
  4787. ABIKind getABIKind() const { return Kind; }
  4788. private:
  4789. ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic,
  4790. unsigned functionCallConv) const;
  4791. ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic,
  4792. unsigned functionCallConv) const;
  4793. ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base,
  4794. uint64_t Members) const;
  4795. ABIArgInfo coerceIllegalVector(QualType Ty) const;
  4796. bool isIllegalVectorType(QualType Ty) const;
  4797. bool containsAnyFP16Vectors(QualType Ty) const;
  4798. bool isHomogeneousAggregateBaseType(QualType Ty) const override;
  4799. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  4800. uint64_t Members) const override;
  4801. bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const;
  4802. void computeInfo(CGFunctionInfo &FI) const override;
  4803. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4804. QualType Ty) const override;
  4805. llvm::CallingConv::ID getLLVMDefaultCC() const;
  4806. llvm::CallingConv::ID getABIDefaultCC() const;
  4807. void setCCs();
  4808. bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
  4809. bool asReturnValue) const override {
  4810. return occupiesMoreThan(CGT, scalars, /*total*/ 4);
  4811. }
  4812. bool isSwiftErrorInRegister() const override {
  4813. return true;
  4814. }
  4815. bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
  4816. unsigned elts) const override;
  4817. };
  4818. class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
  4819. public:
  4820. ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
  4821. :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
  4822. const ARMABIInfo &getABIInfo() const {
  4823. return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
  4824. }
  4825. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  4826. return 13;
  4827. }
  4828. StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
  4829. return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
  4830. }
  4831. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  4832. llvm::Value *Address) const override {
  4833. llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
  4834. // 0-15 are the 16 integer registers.
  4835. AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
  4836. return false;
  4837. }
  4838. unsigned getSizeOfUnwindException() const override {
  4839. if (getABIInfo().isEABI()) return 88;
  4840. return TargetCodeGenInfo::getSizeOfUnwindException();
  4841. }
  4842. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  4843. CodeGen::CodeGenModule &CGM) const override {
  4844. if (GV->isDeclaration())
  4845. return;
  4846. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  4847. if (!FD)
  4848. return;
  4849. const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
  4850. if (!Attr)
  4851. return;
  4852. const char *Kind;
  4853. switch (Attr->getInterrupt()) {
  4854. case ARMInterruptAttr::Generic: Kind = ""; break;
  4855. case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
  4856. case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
  4857. case ARMInterruptAttr::SWI: Kind = "SWI"; break;
  4858. case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
  4859. case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
  4860. }
  4861. llvm::Function *Fn = cast<llvm::Function>(GV);
  4862. Fn->addFnAttr("interrupt", Kind);
  4863. ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
  4864. if (ABI == ARMABIInfo::APCS)
  4865. return;
  4866. // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
  4867. // however this is not necessarily true on taking any interrupt. Instruct
  4868. // the backend to perform a realignment as part of the function prologue.
  4869. llvm::AttrBuilder B;
  4870. B.addStackAlignmentAttr(8);
  4871. Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
  4872. }
  4873. };
  4874. class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
  4875. public:
  4876. WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
  4877. : ARMTargetCodeGenInfo(CGT, K) {}
  4878. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  4879. CodeGen::CodeGenModule &CGM) const override;
  4880. void getDependentLibraryOption(llvm::StringRef Lib,
  4881. llvm::SmallString<24> &Opt) const override {
  4882. Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
  4883. }
  4884. void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
  4885. llvm::SmallString<32> &Opt) const override {
  4886. Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
  4887. }
  4888. };
  4889. void WindowsARMTargetCodeGenInfo::setTargetAttributes(
  4890. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
  4891. ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
  4892. if (GV->isDeclaration())
  4893. return;
  4894. addStackProbeTargetAttributes(D, GV, CGM);
  4895. }
  4896. }
  4897. void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
  4898. if (!::classifyReturnType(getCXXABI(), FI, *this))
  4899. FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(),
  4900. FI.getCallingConvention());
  4901. for (auto &I : FI.arguments())
  4902. I.info = classifyArgumentType(I.type, FI.isVariadic(),
  4903. FI.getCallingConvention());
  4904. // Always honor user-specified calling convention.
  4905. if (FI.getCallingConvention() != llvm::CallingConv::C)
  4906. return;
  4907. llvm::CallingConv::ID cc = getRuntimeCC();
  4908. if (cc != llvm::CallingConv::C)
  4909. FI.setEffectiveCallingConvention(cc);
  4910. }
  4911. /// Return the default calling convention that LLVM will use.
  4912. llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
  4913. // The default calling convention that LLVM will infer.
  4914. if (isEABIHF() || getTarget().getTriple().isWatchABI())
  4915. return llvm::CallingConv::ARM_AAPCS_VFP;
  4916. else if (isEABI())
  4917. return llvm::CallingConv::ARM_AAPCS;
  4918. else
  4919. return llvm::CallingConv::ARM_APCS;
  4920. }
  4921. /// Return the calling convention that our ABI would like us to use
  4922. /// as the C calling convention.
  4923. llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
  4924. switch (getABIKind()) {
  4925. case APCS: return llvm::CallingConv::ARM_APCS;
  4926. case AAPCS: return llvm::CallingConv::ARM_AAPCS;
  4927. case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
  4928. case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
  4929. }
  4930. llvm_unreachable("bad ABI kind");
  4931. }
  4932. void ARMABIInfo::setCCs() {
  4933. assert(getRuntimeCC() == llvm::CallingConv::C);
  4934. // Don't muddy up the IR with a ton of explicit annotations if
  4935. // they'd just match what LLVM will infer from the triple.
  4936. llvm::CallingConv::ID abiCC = getABIDefaultCC();
  4937. if (abiCC != getLLVMDefaultCC())
  4938. RuntimeCC = abiCC;
  4939. }
  4940. ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const {
  4941. uint64_t Size = getContext().getTypeSize(Ty);
  4942. if (Size <= 32) {
  4943. llvm::Type *ResType =
  4944. llvm::Type::getInt32Ty(getVMContext());
  4945. return ABIArgInfo::getDirect(ResType);
  4946. }
  4947. if (Size == 64 || Size == 128) {
  4948. llvm::Type *ResType = llvm::VectorType::get(
  4949. llvm::Type::getInt32Ty(getVMContext()), Size / 32);
  4950. return ABIArgInfo::getDirect(ResType);
  4951. }
  4952. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  4953. }
  4954. ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty,
  4955. const Type *Base,
  4956. uint64_t Members) const {
  4957. assert(Base && "Base class should be set for homogeneous aggregate");
  4958. // Base can be a floating-point or a vector.
  4959. if (const VectorType *VT = Base->getAs<VectorType>()) {
  4960. // FP16 vectors should be converted to integer vectors
  4961. if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) {
  4962. uint64_t Size = getContext().getTypeSize(VT);
  4963. llvm::Type *NewVecTy = llvm::VectorType::get(
  4964. llvm::Type::getInt32Ty(getVMContext()), Size / 32);
  4965. llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members);
  4966. return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
  4967. }
  4968. }
  4969. return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
  4970. }
  4971. ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic,
  4972. unsigned functionCallConv) const {
  4973. // 6.1.2.1 The following argument types are VFP CPRCs:
  4974. // A single-precision floating-point type (including promoted
  4975. // half-precision types); A double-precision floating-point type;
  4976. // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
  4977. // with a Base Type of a single- or double-precision floating-point type,
  4978. // 64-bit containerized vectors or 128-bit containerized vectors with one
  4979. // to four Elements.
  4980. // Variadic functions should always marshal to the base standard.
  4981. bool IsAAPCS_VFP =
  4982. !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false);
  4983. Ty = useFirstFieldIfTransparentUnion(Ty);
  4984. // Handle illegal vector types here.
  4985. if (isIllegalVectorType(Ty))
  4986. return coerceIllegalVector(Ty);
  4987. // _Float16 and __fp16 get passed as if it were an int or float, but with
  4988. // the top 16 bits unspecified. This is not done for OpenCL as it handles the
  4989. // half type natively, and does not need to interwork with AAPCS code.
  4990. if ((Ty->isFloat16Type() || Ty->isHalfType()) &&
  4991. !getContext().getLangOpts().NativeHalfArgsAndReturns) {
  4992. llvm::Type *ResType = IsAAPCS_VFP ?
  4993. llvm::Type::getFloatTy(getVMContext()) :
  4994. llvm::Type::getInt32Ty(getVMContext());
  4995. return ABIArgInfo::getDirect(ResType);
  4996. }
  4997. if (!isAggregateTypeForABI(Ty)) {
  4998. // Treat an enum type as its underlying type.
  4999. if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
  5000. Ty = EnumTy->getDecl()->getIntegerType();
  5001. }
  5002. return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
  5003. : ABIArgInfo::getDirect());
  5004. }
  5005. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  5006. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  5007. }
  5008. // Ignore empty records.
  5009. if (isEmptyRecord(getContext(), Ty, true))
  5010. return ABIArgInfo::getIgnore();
  5011. if (IsAAPCS_VFP) {
  5012. // Homogeneous Aggregates need to be expanded when we can fit the aggregate
  5013. // into VFP registers.
  5014. const Type *Base = nullptr;
  5015. uint64_t Members = 0;
  5016. if (isHomogeneousAggregate(Ty, Base, Members))
  5017. return classifyHomogeneousAggregate(Ty, Base, Members);
  5018. } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
  5019. // WatchOS does have homogeneous aggregates. Note that we intentionally use
  5020. // this convention even for a variadic function: the backend will use GPRs
  5021. // if needed.
  5022. const Type *Base = nullptr;
  5023. uint64_t Members = 0;
  5024. if (isHomogeneousAggregate(Ty, Base, Members)) {
  5025. assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
  5026. llvm::Type *Ty =
  5027. llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
  5028. return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
  5029. }
  5030. }
  5031. if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
  5032. getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
  5033. // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
  5034. // bigger than 128-bits, they get placed in space allocated by the caller,
  5035. // and a pointer is passed.
  5036. return ABIArgInfo::getIndirect(
  5037. CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
  5038. }
  5039. // Support byval for ARM.
  5040. // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
  5041. // most 8-byte. We realign the indirect argument if type alignment is bigger
  5042. // than ABI alignment.
  5043. uint64_t ABIAlign = 4;
  5044. uint64_t TyAlign;
  5045. if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
  5046. getABIKind() == ARMABIInfo::AAPCS) {
  5047. TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
  5048. ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
  5049. } else {
  5050. TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
  5051. }
  5052. if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
  5053. assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
  5054. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
  5055. /*ByVal=*/true,
  5056. /*Realign=*/TyAlign > ABIAlign);
  5057. }
  5058. // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
  5059. // same size and alignment.
  5060. if (getTarget().isRenderScriptTarget()) {
  5061. return coerceToIntArray(Ty, getContext(), getVMContext());
  5062. }
  5063. // Otherwise, pass by coercing to a structure of the appropriate size.
  5064. llvm::Type* ElemTy;
  5065. unsigned SizeRegs;
  5066. // FIXME: Try to match the types of the arguments more accurately where
  5067. // we can.
  5068. if (TyAlign <= 4) {
  5069. ElemTy = llvm::Type::getInt32Ty(getVMContext());
  5070. SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
  5071. } else {
  5072. ElemTy = llvm::Type::getInt64Ty(getVMContext());
  5073. SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
  5074. }
  5075. return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
  5076. }
  5077. static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
  5078. llvm::LLVMContext &VMContext) {
  5079. // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
  5080. // is called integer-like if its size is less than or equal to one word, and
  5081. // the offset of each of its addressable sub-fields is zero.
  5082. uint64_t Size = Context.getTypeSize(Ty);
  5083. // Check that the type fits in a word.
  5084. if (Size > 32)
  5085. return false;
  5086. // FIXME: Handle vector types!
  5087. if (Ty->isVectorType())
  5088. return false;
  5089. // Float types are never treated as "integer like".
  5090. if (Ty->isRealFloatingType())
  5091. return false;
  5092. // If this is a builtin or pointer type then it is ok.
  5093. if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
  5094. return true;
  5095. // Small complex integer types are "integer like".
  5096. if (const ComplexType *CT = Ty->getAs<ComplexType>())
  5097. return isIntegerLikeType(CT->getElementType(), Context, VMContext);
  5098. // Single element and zero sized arrays should be allowed, by the definition
  5099. // above, but they are not.
  5100. // Otherwise, it must be a record type.
  5101. const RecordType *RT = Ty->getAs<RecordType>();
  5102. if (!RT) return false;
  5103. // Ignore records with flexible arrays.
  5104. const RecordDecl *RD = RT->getDecl();
  5105. if (RD->hasFlexibleArrayMember())
  5106. return false;
  5107. // Check that all sub-fields are at offset 0, and are themselves "integer
  5108. // like".
  5109. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  5110. bool HadField = false;
  5111. unsigned idx = 0;
  5112. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  5113. i != e; ++i, ++idx) {
  5114. const FieldDecl *FD = *i;
  5115. // Bit-fields are not addressable, we only need to verify they are "integer
  5116. // like". We still have to disallow a subsequent non-bitfield, for example:
  5117. // struct { int : 0; int x }
  5118. // is non-integer like according to gcc.
  5119. if (FD->isBitField()) {
  5120. if (!RD->isUnion())
  5121. HadField = true;
  5122. if (!isIntegerLikeType(FD->getType(), Context, VMContext))
  5123. return false;
  5124. continue;
  5125. }
  5126. // Check if this field is at offset 0.
  5127. if (Layout.getFieldOffset(idx) != 0)
  5128. return false;
  5129. if (!isIntegerLikeType(FD->getType(), Context, VMContext))
  5130. return false;
  5131. // Only allow at most one field in a structure. This doesn't match the
  5132. // wording above, but follows gcc in situations with a field following an
  5133. // empty structure.
  5134. if (!RD->isUnion()) {
  5135. if (HadField)
  5136. return false;
  5137. HadField = true;
  5138. }
  5139. }
  5140. return true;
  5141. }
  5142. ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic,
  5143. unsigned functionCallConv) const {
  5144. // Variadic functions should always marshal to the base standard.
  5145. bool IsAAPCS_VFP =
  5146. !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true);
  5147. if (RetTy->isVoidType())
  5148. return ABIArgInfo::getIgnore();
  5149. if (const VectorType *VT = RetTy->getAs<VectorType>()) {
  5150. // Large vector types should be returned via memory.
  5151. if (getContext().getTypeSize(RetTy) > 128)
  5152. return getNaturalAlignIndirect(RetTy);
  5153. // FP16 vectors should be converted to integer vectors
  5154. if (!getTarget().hasLegalHalfType() &&
  5155. (VT->getElementType()->isFloat16Type() ||
  5156. VT->getElementType()->isHalfType()))
  5157. return coerceIllegalVector(RetTy);
  5158. }
  5159. // _Float16 and __fp16 get returned as if it were an int or float, but with
  5160. // the top 16 bits unspecified. This is not done for OpenCL as it handles the
  5161. // half type natively, and does not need to interwork with AAPCS code.
  5162. if ((RetTy->isFloat16Type() || RetTy->isHalfType()) &&
  5163. !getContext().getLangOpts().NativeHalfArgsAndReturns) {
  5164. llvm::Type *ResType = IsAAPCS_VFP ?
  5165. llvm::Type::getFloatTy(getVMContext()) :
  5166. llvm::Type::getInt32Ty(getVMContext());
  5167. return ABIArgInfo::getDirect(ResType);
  5168. }
  5169. if (!isAggregateTypeForABI(RetTy)) {
  5170. // Treat an enum type as its underlying type.
  5171. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  5172. RetTy = EnumTy->getDecl()->getIntegerType();
  5173. return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
  5174. : ABIArgInfo::getDirect();
  5175. }
  5176. // Are we following APCS?
  5177. if (getABIKind() == APCS) {
  5178. if (isEmptyRecord(getContext(), RetTy, false))
  5179. return ABIArgInfo::getIgnore();
  5180. // Complex types are all returned as packed integers.
  5181. //
  5182. // FIXME: Consider using 2 x vector types if the back end handles them
  5183. // correctly.
  5184. if (RetTy->isAnyComplexType())
  5185. return ABIArgInfo::getDirect(llvm::IntegerType::get(
  5186. getVMContext(), getContext().getTypeSize(RetTy)));
  5187. // Integer like structures are returned in r0.
  5188. if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
  5189. // Return in the smallest viable integer type.
  5190. uint64_t Size = getContext().getTypeSize(RetTy);
  5191. if (Size <= 8)
  5192. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  5193. if (Size <= 16)
  5194. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  5195. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  5196. }
  5197. // Otherwise return in memory.
  5198. return getNaturalAlignIndirect(RetTy);
  5199. }
  5200. // Otherwise this is an AAPCS variant.
  5201. if (isEmptyRecord(getContext(), RetTy, true))
  5202. return ABIArgInfo::getIgnore();
  5203. // Check for homogeneous aggregates with AAPCS-VFP.
  5204. if (IsAAPCS_VFP) {
  5205. const Type *Base = nullptr;
  5206. uint64_t Members = 0;
  5207. if (isHomogeneousAggregate(RetTy, Base, Members))
  5208. return classifyHomogeneousAggregate(RetTy, Base, Members);
  5209. }
  5210. // Aggregates <= 4 bytes are returned in r0; other aggregates
  5211. // are returned indirectly.
  5212. uint64_t Size = getContext().getTypeSize(RetTy);
  5213. if (Size <= 32) {
  5214. // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
  5215. // same size and alignment.
  5216. if (getTarget().isRenderScriptTarget()) {
  5217. return coerceToIntArray(RetTy, getContext(), getVMContext());
  5218. }
  5219. if (getDataLayout().isBigEndian())
  5220. // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
  5221. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  5222. // Return in the smallest viable integer type.
  5223. if (Size <= 8)
  5224. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  5225. if (Size <= 16)
  5226. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  5227. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  5228. } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
  5229. llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
  5230. llvm::Type *CoerceTy =
  5231. llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
  5232. return ABIArgInfo::getDirect(CoerceTy);
  5233. }
  5234. return getNaturalAlignIndirect(RetTy);
  5235. }
  5236. /// isIllegalVector - check whether Ty is an illegal vector type.
  5237. bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
  5238. if (const VectorType *VT = Ty->getAs<VectorType> ()) {
  5239. // On targets that don't support FP16, FP16 is expanded into float, and we
  5240. // don't want the ABI to depend on whether or not FP16 is supported in
  5241. // hardware. Thus return false to coerce FP16 vectors into integer vectors.
  5242. if (!getTarget().hasLegalHalfType() &&
  5243. (VT->getElementType()->isFloat16Type() ||
  5244. VT->getElementType()->isHalfType()))
  5245. return true;
  5246. if (isAndroid()) {
  5247. // Android shipped using Clang 3.1, which supported a slightly different
  5248. // vector ABI. The primary differences were that 3-element vector types
  5249. // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
  5250. // accepts that legacy behavior for Android only.
  5251. // Check whether VT is legal.
  5252. unsigned NumElements = VT->getNumElements();
  5253. // NumElements should be power of 2 or equal to 3.
  5254. if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
  5255. return true;
  5256. } else {
  5257. // Check whether VT is legal.
  5258. unsigned NumElements = VT->getNumElements();
  5259. uint64_t Size = getContext().getTypeSize(VT);
  5260. // NumElements should be power of 2.
  5261. if (!llvm::isPowerOf2_32(NumElements))
  5262. return true;
  5263. // Size should be greater than 32 bits.
  5264. return Size <= 32;
  5265. }
  5266. }
  5267. return false;
  5268. }
  5269. /// Return true if a type contains any 16-bit floating point vectors
  5270. bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const {
  5271. if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
  5272. uint64_t NElements = AT->getSize().getZExtValue();
  5273. if (NElements == 0)
  5274. return false;
  5275. return containsAnyFP16Vectors(AT->getElementType());
  5276. } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
  5277. const RecordDecl *RD = RT->getDecl();
  5278. // If this is a C++ record, check the bases first.
  5279. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  5280. if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) {
  5281. return containsAnyFP16Vectors(B.getType());
  5282. }))
  5283. return true;
  5284. if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) {
  5285. return FD && containsAnyFP16Vectors(FD->getType());
  5286. }))
  5287. return true;
  5288. return false;
  5289. } else {
  5290. if (const VectorType *VT = Ty->getAs<VectorType>())
  5291. return (VT->getElementType()->isFloat16Type() ||
  5292. VT->getElementType()->isHalfType());
  5293. return false;
  5294. }
  5295. }
  5296. bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
  5297. llvm::Type *eltTy,
  5298. unsigned numElts) const {
  5299. if (!llvm::isPowerOf2_32(numElts))
  5300. return false;
  5301. unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy);
  5302. if (size > 64)
  5303. return false;
  5304. if (vectorSize.getQuantity() != 8 &&
  5305. (vectorSize.getQuantity() != 16 || numElts == 1))
  5306. return false;
  5307. return true;
  5308. }
  5309. bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  5310. // Homogeneous aggregates for AAPCS-VFP must have base types of float,
  5311. // double, or 64-bit or 128-bit vectors.
  5312. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  5313. if (BT->getKind() == BuiltinType::Float ||
  5314. BT->getKind() == BuiltinType::Double ||
  5315. BT->getKind() == BuiltinType::LongDouble)
  5316. return true;
  5317. } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
  5318. unsigned VecSize = getContext().getTypeSize(VT);
  5319. if (VecSize == 64 || VecSize == 128)
  5320. return true;
  5321. }
  5322. return false;
  5323. }
  5324. bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
  5325. uint64_t Members) const {
  5326. return Members <= 4;
  5327. }
  5328. bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention,
  5329. bool acceptHalf) const {
  5330. // Give precedence to user-specified calling conventions.
  5331. if (callConvention != llvm::CallingConv::C)
  5332. return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP);
  5333. else
  5334. return (getABIKind() == AAPCS_VFP) ||
  5335. (acceptHalf && (getABIKind() == AAPCS16_VFP));
  5336. }
  5337. Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5338. QualType Ty) const {
  5339. CharUnits SlotSize = CharUnits::fromQuantity(4);
  5340. // Empty records are ignored for parameter passing purposes.
  5341. if (isEmptyRecord(getContext(), Ty, true)) {
  5342. Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
  5343. Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
  5344. return Addr;
  5345. }
  5346. CharUnits TySize = getContext().getTypeSizeInChars(Ty);
  5347. CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty);
  5348. // Use indirect if size of the illegal vector is bigger than 16 bytes.
  5349. bool IsIndirect = false;
  5350. const Type *Base = nullptr;
  5351. uint64_t Members = 0;
  5352. if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
  5353. IsIndirect = true;
  5354. // ARMv7k passes structs bigger than 16 bytes indirectly, in space
  5355. // allocated by the caller.
  5356. } else if (TySize > CharUnits::fromQuantity(16) &&
  5357. getABIKind() == ARMABIInfo::AAPCS16_VFP &&
  5358. !isHomogeneousAggregate(Ty, Base, Members)) {
  5359. IsIndirect = true;
  5360. // Otherwise, bound the type's ABI alignment.
  5361. // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
  5362. // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
  5363. // Our callers should be prepared to handle an under-aligned address.
  5364. } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
  5365. getABIKind() == ARMABIInfo::AAPCS) {
  5366. TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
  5367. TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
  5368. } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
  5369. // ARMv7k allows type alignment up to 16 bytes.
  5370. TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
  5371. TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
  5372. } else {
  5373. TyAlignForABI = CharUnits::fromQuantity(4);
  5374. }
  5375. std::pair<CharUnits, CharUnits> TyInfo = { TySize, TyAlignForABI };
  5376. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
  5377. SlotSize, /*AllowHigherAlign*/ true);
  5378. }
  5379. //===----------------------------------------------------------------------===//
  5380. // NVPTX ABI Implementation
  5381. //===----------------------------------------------------------------------===//
  5382. namespace {
  5383. class NVPTXABIInfo : public ABIInfo {
  5384. public:
  5385. NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
  5386. ABIArgInfo classifyReturnType(QualType RetTy) const;
  5387. ABIArgInfo classifyArgumentType(QualType Ty) const;
  5388. void computeInfo(CGFunctionInfo &FI) const override;
  5389. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5390. QualType Ty) const override;
  5391. };
  5392. class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
  5393. public:
  5394. NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
  5395. : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
  5396. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  5397. CodeGen::CodeGenModule &M) const override;
  5398. bool shouldEmitStaticExternCAliases() const override;
  5399. private:
  5400. // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
  5401. // resulting MDNode to the nvvm.annotations MDNode.
  5402. static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
  5403. };
  5404. /// Checks if the type is unsupported directly by the current target.
  5405. static bool isUnsupportedType(ASTContext &Context, QualType T) {
  5406. if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type())
  5407. return true;
  5408. if (!Context.getTargetInfo().hasFloat128Type() &&
  5409. (T->isFloat128Type() ||
  5410. (T->isRealFloatingType() && Context.getTypeSize(T) == 128)))
  5411. return true;
  5412. if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() &&
  5413. Context.getTypeSize(T) > 64)
  5414. return true;
  5415. if (const auto *AT = T->getAsArrayTypeUnsafe())
  5416. return isUnsupportedType(Context, AT->getElementType());
  5417. const auto *RT = T->getAs<RecordType>();
  5418. if (!RT)
  5419. return false;
  5420. const RecordDecl *RD = RT->getDecl();
  5421. // If this is a C++ record, check the bases first.
  5422. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  5423. for (const CXXBaseSpecifier &I : CXXRD->bases())
  5424. if (isUnsupportedType(Context, I.getType()))
  5425. return true;
  5426. for (const FieldDecl *I : RD->fields())
  5427. if (isUnsupportedType(Context, I->getType()))
  5428. return true;
  5429. return false;
  5430. }
  5431. /// Coerce the given type into an array with maximum allowed size of elements.
  5432. static ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, ASTContext &Context,
  5433. llvm::LLVMContext &LLVMContext,
  5434. unsigned MaxSize) {
  5435. // Alignment and Size are measured in bits.
  5436. const uint64_t Size = Context.getTypeSize(Ty);
  5437. const uint64_t Alignment = Context.getTypeAlign(Ty);
  5438. const unsigned Div = std::min<unsigned>(MaxSize, Alignment);
  5439. llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Div);
  5440. const uint64_t NumElements = (Size + Div - 1) / Div;
  5441. return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
  5442. }
  5443. ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
  5444. if (RetTy->isVoidType())
  5445. return ABIArgInfo::getIgnore();
  5446. if (getContext().getLangOpts().OpenMP &&
  5447. getContext().getLangOpts().OpenMPIsDevice &&
  5448. isUnsupportedType(getContext(), RetTy))
  5449. return coerceToIntArrayWithLimit(RetTy, getContext(), getVMContext(), 64);
  5450. // note: this is different from default ABI
  5451. if (!RetTy->isScalarType())
  5452. return ABIArgInfo::getDirect();
  5453. // Treat an enum type as its underlying type.
  5454. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  5455. RetTy = EnumTy->getDecl()->getIntegerType();
  5456. return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
  5457. : ABIArgInfo::getDirect());
  5458. }
  5459. ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
  5460. // Treat an enum type as its underlying type.
  5461. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  5462. Ty = EnumTy->getDecl()->getIntegerType();
  5463. // Return aggregates type as indirect by value
  5464. if (isAggregateTypeForABI(Ty))
  5465. return getNaturalAlignIndirect(Ty, /* byval */ true);
  5466. return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
  5467. : ABIArgInfo::getDirect());
  5468. }
  5469. void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
  5470. if (!getCXXABI().classifyReturnType(FI))
  5471. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  5472. for (auto &I : FI.arguments())
  5473. I.info = classifyArgumentType(I.type);
  5474. // Always honor user-specified calling convention.
  5475. if (FI.getCallingConvention() != llvm::CallingConv::C)
  5476. return;
  5477. FI.setEffectiveCallingConvention(getRuntimeCC());
  5478. }
  5479. Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5480. QualType Ty) const {
  5481. llvm_unreachable("NVPTX does not support varargs");
  5482. }
  5483. void NVPTXTargetCodeGenInfo::setTargetAttributes(
  5484. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
  5485. if (GV->isDeclaration())
  5486. return;
  5487. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  5488. if (!FD) return;
  5489. llvm::Function *F = cast<llvm::Function>(GV);
  5490. // Perform special handling in OpenCL mode
  5491. if (M.getLangOpts().OpenCL) {
  5492. // Use OpenCL function attributes to check for kernel functions
  5493. // By default, all functions are device functions
  5494. if (FD->hasAttr<OpenCLKernelAttr>()) {
  5495. // OpenCL __kernel functions get kernel metadata
  5496. // Create !{<func-ref>, metadata !"kernel", i32 1} node
  5497. addNVVMMetadata(F, "kernel", 1);
  5498. // And kernel functions are not subject to inlining
  5499. F->addFnAttr(llvm::Attribute::NoInline);
  5500. }
  5501. }
  5502. // Perform special handling in CUDA mode.
  5503. if (M.getLangOpts().CUDA) {
  5504. // CUDA __global__ functions get a kernel metadata entry. Since
  5505. // __global__ functions cannot be called from the device, we do not
  5506. // need to set the noinline attribute.
  5507. if (FD->hasAttr<CUDAGlobalAttr>()) {
  5508. // Create !{<func-ref>, metadata !"kernel", i32 1} node
  5509. addNVVMMetadata(F, "kernel", 1);
  5510. }
  5511. if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
  5512. // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
  5513. llvm::APSInt MaxThreads(32);
  5514. MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
  5515. if (MaxThreads > 0)
  5516. addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
  5517. // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
  5518. // not specified in __launch_bounds__ or if the user specified a 0 value,
  5519. // we don't have to add a PTX directive.
  5520. if (Attr->getMinBlocks()) {
  5521. llvm::APSInt MinBlocks(32);
  5522. MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
  5523. if (MinBlocks > 0)
  5524. // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
  5525. addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
  5526. }
  5527. }
  5528. }
  5529. }
  5530. void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
  5531. int Operand) {
  5532. llvm::Module *M = F->getParent();
  5533. llvm::LLVMContext &Ctx = M->getContext();
  5534. // Get "nvvm.annotations" metadata node
  5535. llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
  5536. llvm::Metadata *MDVals[] = {
  5537. llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
  5538. llvm::ConstantAsMetadata::get(
  5539. llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
  5540. // Append metadata to nvvm.annotations
  5541. MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
  5542. }
  5543. bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
  5544. return false;
  5545. }
  5546. }
  5547. //===----------------------------------------------------------------------===//
  5548. // SystemZ ABI Implementation
  5549. //===----------------------------------------------------------------------===//
  5550. namespace {
  5551. class SystemZABIInfo : public SwiftABIInfo {
  5552. bool HasVector;
  5553. public:
  5554. SystemZABIInfo(CodeGenTypes &CGT, bool HV)
  5555. : SwiftABIInfo(CGT), HasVector(HV) {}
  5556. bool isPromotableIntegerType(QualType Ty) const;
  5557. bool isCompoundType(QualType Ty) const;
  5558. bool isVectorArgumentType(QualType Ty) const;
  5559. bool isFPArgumentType(QualType Ty) const;
  5560. QualType GetSingleElementType(QualType Ty) const;
  5561. ABIArgInfo classifyReturnType(QualType RetTy) const;
  5562. ABIArgInfo classifyArgumentType(QualType ArgTy) const;
  5563. void computeInfo(CGFunctionInfo &FI) const override {
  5564. if (!getCXXABI().classifyReturnType(FI))
  5565. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  5566. for (auto &I : FI.arguments())
  5567. I.info = classifyArgumentType(I.type);
  5568. }
  5569. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5570. QualType Ty) const override;
  5571. bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
  5572. bool asReturnValue) const override {
  5573. return occupiesMoreThan(CGT, scalars, /*total*/ 4);
  5574. }
  5575. bool isSwiftErrorInRegister() const override {
  5576. return false;
  5577. }
  5578. };
  5579. class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
  5580. public:
  5581. SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
  5582. : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {}
  5583. };
  5584. }
  5585. bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
  5586. // Treat an enum type as its underlying type.
  5587. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  5588. Ty = EnumTy->getDecl()->getIntegerType();
  5589. // Promotable integer types are required to be promoted by the ABI.
  5590. if (Ty->isPromotableIntegerType())
  5591. return true;
  5592. // 32-bit values must also be promoted.
  5593. if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
  5594. switch (BT->getKind()) {
  5595. case BuiltinType::Int:
  5596. case BuiltinType::UInt:
  5597. return true;
  5598. default:
  5599. return false;
  5600. }
  5601. return false;
  5602. }
  5603. bool SystemZABIInfo::isCompoundType(QualType Ty) const {
  5604. return (Ty->isAnyComplexType() ||
  5605. Ty->isVectorType() ||
  5606. isAggregateTypeForABI(Ty));
  5607. }
  5608. bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
  5609. return (HasVector &&
  5610. Ty->isVectorType() &&
  5611. getContext().getTypeSize(Ty) <= 128);
  5612. }
  5613. bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
  5614. if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
  5615. switch (BT->getKind()) {
  5616. case BuiltinType::Float:
  5617. case BuiltinType::Double:
  5618. return true;
  5619. default:
  5620. return false;
  5621. }
  5622. return false;
  5623. }
  5624. QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
  5625. if (const RecordType *RT = Ty->getAsStructureType()) {
  5626. const RecordDecl *RD = RT->getDecl();
  5627. QualType Found;
  5628. // If this is a C++ record, check the bases first.
  5629. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  5630. for (const auto &I : CXXRD->bases()) {
  5631. QualType Base = I.getType();
  5632. // Empty bases don't affect things either way.
  5633. if (isEmptyRecord(getContext(), Base, true))
  5634. continue;
  5635. if (!Found.isNull())
  5636. return Ty;
  5637. Found = GetSingleElementType(Base);
  5638. }
  5639. // Check the fields.
  5640. for (const auto *FD : RD->fields()) {
  5641. // For compatibility with GCC, ignore empty bitfields in C++ mode.
  5642. // Unlike isSingleElementStruct(), empty structure and array fields
  5643. // do count. So do anonymous bitfields that aren't zero-sized.
  5644. if (getContext().getLangOpts().CPlusPlus &&
  5645. FD->isZeroLengthBitField(getContext()))
  5646. continue;
  5647. // Unlike isSingleElementStruct(), arrays do not count.
  5648. // Nested structures still do though.
  5649. if (!Found.isNull())
  5650. return Ty;
  5651. Found = GetSingleElementType(FD->getType());
  5652. }
  5653. // Unlike isSingleElementStruct(), trailing padding is allowed.
  5654. // An 8-byte aligned struct s { float f; } is passed as a double.
  5655. if (!Found.isNull())
  5656. return Found;
  5657. }
  5658. return Ty;
  5659. }
  5660. Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5661. QualType Ty) const {
  5662. // Assume that va_list type is correct; should be pointer to LLVM type:
  5663. // struct {
  5664. // i64 __gpr;
  5665. // i64 __fpr;
  5666. // i8 *__overflow_arg_area;
  5667. // i8 *__reg_save_area;
  5668. // };
  5669. // Every non-vector argument occupies 8 bytes and is passed by preference
  5670. // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are
  5671. // always passed on the stack.
  5672. Ty = getContext().getCanonicalType(Ty);
  5673. auto TyInfo = getContext().getTypeInfoInChars(Ty);
  5674. llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
  5675. llvm::Type *DirectTy = ArgTy;
  5676. ABIArgInfo AI = classifyArgumentType(Ty);
  5677. bool IsIndirect = AI.isIndirect();
  5678. bool InFPRs = false;
  5679. bool IsVector = false;
  5680. CharUnits UnpaddedSize;
  5681. CharUnits DirectAlign;
  5682. if (IsIndirect) {
  5683. DirectTy = llvm::PointerType::getUnqual(DirectTy);
  5684. UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
  5685. } else {
  5686. if (AI.getCoerceToType())
  5687. ArgTy = AI.getCoerceToType();
  5688. InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy();
  5689. IsVector = ArgTy->isVectorTy();
  5690. UnpaddedSize = TyInfo.first;
  5691. DirectAlign = TyInfo.second;
  5692. }
  5693. CharUnits PaddedSize = CharUnits::fromQuantity(8);
  5694. if (IsVector && UnpaddedSize > PaddedSize)
  5695. PaddedSize = CharUnits::fromQuantity(16);
  5696. assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
  5697. CharUnits Padding = (PaddedSize - UnpaddedSize);
  5698. llvm::Type *IndexTy = CGF.Int64Ty;
  5699. llvm::Value *PaddedSizeV =
  5700. llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
  5701. if (IsVector) {
  5702. // Work out the address of a vector argument on the stack.
  5703. // Vector arguments are always passed in the high bits of a
  5704. // single (8 byte) or double (16 byte) stack slot.
  5705. Address OverflowArgAreaPtr =
  5706. CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
  5707. Address OverflowArgArea =
  5708. Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
  5709. TyInfo.second);
  5710. Address MemAddr =
  5711. CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
  5712. // Update overflow_arg_area_ptr pointer
  5713. llvm::Value *NewOverflowArgArea =
  5714. CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
  5715. "overflow_arg_area");
  5716. CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
  5717. return MemAddr;
  5718. }
  5719. assert(PaddedSize.getQuantity() == 8);
  5720. unsigned MaxRegs, RegCountField, RegSaveIndex;
  5721. CharUnits RegPadding;
  5722. if (InFPRs) {
  5723. MaxRegs = 4; // Maximum of 4 FPR arguments
  5724. RegCountField = 1; // __fpr
  5725. RegSaveIndex = 16; // save offset for f0
  5726. RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
  5727. } else {
  5728. MaxRegs = 5; // Maximum of 5 GPR arguments
  5729. RegCountField = 0; // __gpr
  5730. RegSaveIndex = 2; // save offset for r2
  5731. RegPadding = Padding; // values are passed in the low bits of a GPR
  5732. }
  5733. Address RegCountPtr =
  5734. CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
  5735. llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
  5736. llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
  5737. llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
  5738. "fits_in_regs");
  5739. llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
  5740. llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
  5741. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
  5742. CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
  5743. // Emit code to load the value if it was passed in registers.
  5744. CGF.EmitBlock(InRegBlock);
  5745. // Work out the address of an argument register.
  5746. llvm::Value *ScaledRegCount =
  5747. CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
  5748. llvm::Value *RegBase =
  5749. llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
  5750. + RegPadding.getQuantity());
  5751. llvm::Value *RegOffset =
  5752. CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
  5753. Address RegSaveAreaPtr =
  5754. CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
  5755. llvm::Value *RegSaveArea =
  5756. CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
  5757. Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
  5758. "raw_reg_addr"),
  5759. PaddedSize);
  5760. Address RegAddr =
  5761. CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
  5762. // Update the register count
  5763. llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
  5764. llvm::Value *NewRegCount =
  5765. CGF.Builder.CreateAdd(RegCount, One, "reg_count");
  5766. CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
  5767. CGF.EmitBranch(ContBlock);
  5768. // Emit code to load the value if it was passed in memory.
  5769. CGF.EmitBlock(InMemBlock);
  5770. // Work out the address of a stack argument.
  5771. Address OverflowArgAreaPtr =
  5772. CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
  5773. Address OverflowArgArea =
  5774. Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
  5775. PaddedSize);
  5776. Address RawMemAddr =
  5777. CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
  5778. Address MemAddr =
  5779. CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
  5780. // Update overflow_arg_area_ptr pointer
  5781. llvm::Value *NewOverflowArgArea =
  5782. CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
  5783. "overflow_arg_area");
  5784. CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
  5785. CGF.EmitBranch(ContBlock);
  5786. // Return the appropriate result.
  5787. CGF.EmitBlock(ContBlock);
  5788. Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
  5789. MemAddr, InMemBlock, "va_arg.addr");
  5790. if (IsIndirect)
  5791. ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
  5792. TyInfo.second);
  5793. return ResAddr;
  5794. }
  5795. ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
  5796. if (RetTy->isVoidType())
  5797. return ABIArgInfo::getIgnore();
  5798. if (isVectorArgumentType(RetTy))
  5799. return ABIArgInfo::getDirect();
  5800. if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
  5801. return getNaturalAlignIndirect(RetTy);
  5802. return (isPromotableIntegerType(RetTy) ? ABIArgInfo::getExtend(RetTy)
  5803. : ABIArgInfo::getDirect());
  5804. }
  5805. ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
  5806. // Handle the generic C++ ABI.
  5807. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  5808. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  5809. // Integers and enums are extended to full register width.
  5810. if (isPromotableIntegerType(Ty))
  5811. return ABIArgInfo::getExtend(Ty);
  5812. // Handle vector types and vector-like structure types. Note that
  5813. // as opposed to float-like structure types, we do not allow any
  5814. // padding for vector-like structures, so verify the sizes match.
  5815. uint64_t Size = getContext().getTypeSize(Ty);
  5816. QualType SingleElementTy = GetSingleElementType(Ty);
  5817. if (isVectorArgumentType(SingleElementTy) &&
  5818. getContext().getTypeSize(SingleElementTy) == Size)
  5819. return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
  5820. // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
  5821. if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
  5822. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  5823. // Handle small structures.
  5824. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  5825. // Structures with flexible arrays have variable length, so really
  5826. // fail the size test above.
  5827. const RecordDecl *RD = RT->getDecl();
  5828. if (RD->hasFlexibleArrayMember())
  5829. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  5830. // The structure is passed as an unextended integer, a float, or a double.
  5831. llvm::Type *PassTy;
  5832. if (isFPArgumentType(SingleElementTy)) {
  5833. assert(Size == 32 || Size == 64);
  5834. if (Size == 32)
  5835. PassTy = llvm::Type::getFloatTy(getVMContext());
  5836. else
  5837. PassTy = llvm::Type::getDoubleTy(getVMContext());
  5838. } else
  5839. PassTy = llvm::IntegerType::get(getVMContext(), Size);
  5840. return ABIArgInfo::getDirect(PassTy);
  5841. }
  5842. // Non-structure compounds are passed indirectly.
  5843. if (isCompoundType(Ty))
  5844. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  5845. return ABIArgInfo::getDirect(nullptr);
  5846. }
  5847. //===----------------------------------------------------------------------===//
  5848. // MSP430 ABI Implementation
  5849. //===----------------------------------------------------------------------===//
  5850. namespace {
  5851. class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
  5852. public:
  5853. MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
  5854. : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
  5855. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  5856. CodeGen::CodeGenModule &M) const override;
  5857. };
  5858. }
  5859. void MSP430TargetCodeGenInfo::setTargetAttributes(
  5860. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
  5861. if (GV->isDeclaration())
  5862. return;
  5863. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  5864. const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>();
  5865. if (!InterruptAttr)
  5866. return;
  5867. // Handle 'interrupt' attribute:
  5868. llvm::Function *F = cast<llvm::Function>(GV);
  5869. // Step 1: Set ISR calling convention.
  5870. F->setCallingConv(llvm::CallingConv::MSP430_INTR);
  5871. // Step 2: Add attributes goodness.
  5872. F->addFnAttr(llvm::Attribute::NoInline);
  5873. F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber()));
  5874. }
  5875. }
  5876. //===----------------------------------------------------------------------===//
  5877. // MIPS ABI Implementation. This works for both little-endian and
  5878. // big-endian variants.
  5879. //===----------------------------------------------------------------------===//
  5880. namespace {
  5881. class MipsABIInfo : public ABIInfo {
  5882. bool IsO32;
  5883. unsigned MinABIStackAlignInBytes, StackAlignInBytes;
  5884. void CoerceToIntArgs(uint64_t TySize,
  5885. SmallVectorImpl<llvm::Type *> &ArgList) const;
  5886. llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
  5887. llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
  5888. llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
  5889. public:
  5890. MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
  5891. ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
  5892. StackAlignInBytes(IsO32 ? 8 : 16) {}
  5893. ABIArgInfo classifyReturnType(QualType RetTy) const;
  5894. ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
  5895. void computeInfo(CGFunctionInfo &FI) const override;
  5896. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5897. QualType Ty) const override;
  5898. ABIArgInfo extendType(QualType Ty) const;
  5899. };
  5900. class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
  5901. unsigned SizeOfUnwindException;
  5902. public:
  5903. MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
  5904. : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
  5905. SizeOfUnwindException(IsO32 ? 24 : 32) {}
  5906. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
  5907. return 29;
  5908. }
  5909. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  5910. CodeGen::CodeGenModule &CGM) const override {
  5911. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  5912. if (!FD) return;
  5913. llvm::Function *Fn = cast<llvm::Function>(GV);
  5914. if (FD->hasAttr<MipsLongCallAttr>())
  5915. Fn->addFnAttr("long-call");
  5916. else if (FD->hasAttr<MipsShortCallAttr>())
  5917. Fn->addFnAttr("short-call");
  5918. // Other attributes do not have a meaning for declarations.
  5919. if (GV->isDeclaration())
  5920. return;
  5921. if (FD->hasAttr<Mips16Attr>()) {
  5922. Fn->addFnAttr("mips16");
  5923. }
  5924. else if (FD->hasAttr<NoMips16Attr>()) {
  5925. Fn->addFnAttr("nomips16");
  5926. }
  5927. if (FD->hasAttr<MicroMipsAttr>())
  5928. Fn->addFnAttr("micromips");
  5929. else if (FD->hasAttr<NoMicroMipsAttr>())
  5930. Fn->addFnAttr("nomicromips");
  5931. const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
  5932. if (!Attr)
  5933. return;
  5934. const char *Kind;
  5935. switch (Attr->getInterrupt()) {
  5936. case MipsInterruptAttr::eic: Kind = "eic"; break;
  5937. case MipsInterruptAttr::sw0: Kind = "sw0"; break;
  5938. case MipsInterruptAttr::sw1: Kind = "sw1"; break;
  5939. case MipsInterruptAttr::hw0: Kind = "hw0"; break;
  5940. case MipsInterruptAttr::hw1: Kind = "hw1"; break;
  5941. case MipsInterruptAttr::hw2: Kind = "hw2"; break;
  5942. case MipsInterruptAttr::hw3: Kind = "hw3"; break;
  5943. case MipsInterruptAttr::hw4: Kind = "hw4"; break;
  5944. case MipsInterruptAttr::hw5: Kind = "hw5"; break;
  5945. }
  5946. Fn->addFnAttr("interrupt", Kind);
  5947. }
  5948. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  5949. llvm::Value *Address) const override;
  5950. unsigned getSizeOfUnwindException() const override {
  5951. return SizeOfUnwindException;
  5952. }
  5953. };
  5954. }
  5955. void MipsABIInfo::CoerceToIntArgs(
  5956. uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
  5957. llvm::IntegerType *IntTy =
  5958. llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
  5959. // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
  5960. for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
  5961. ArgList.push_back(IntTy);
  5962. // If necessary, add one more integer type to ArgList.
  5963. unsigned R = TySize % (MinABIStackAlignInBytes * 8);
  5964. if (R)
  5965. ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
  5966. }
  5967. // In N32/64, an aligned double precision floating point field is passed in
  5968. // a register.
  5969. llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
  5970. SmallVector<llvm::Type*, 8> ArgList, IntArgList;
  5971. if (IsO32) {
  5972. CoerceToIntArgs(TySize, ArgList);
  5973. return llvm::StructType::get(getVMContext(), ArgList);
  5974. }
  5975. if (Ty->isComplexType())
  5976. return CGT.ConvertType(Ty);
  5977. const RecordType *RT = Ty->getAs<RecordType>();
  5978. // Unions/vectors are passed in integer registers.
  5979. if (!RT || !RT->isStructureOrClassType()) {
  5980. CoerceToIntArgs(TySize, ArgList);
  5981. return llvm::StructType::get(getVMContext(), ArgList);
  5982. }
  5983. const RecordDecl *RD = RT->getDecl();
  5984. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  5985. assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
  5986. uint64_t LastOffset = 0;
  5987. unsigned idx = 0;
  5988. llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
  5989. // Iterate over fields in the struct/class and check if there are any aligned
  5990. // double fields.
  5991. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  5992. i != e; ++i, ++idx) {
  5993. const QualType Ty = i->getType();
  5994. const BuiltinType *BT = Ty->getAs<BuiltinType>();
  5995. if (!BT || BT->getKind() != BuiltinType::Double)
  5996. continue;
  5997. uint64_t Offset = Layout.getFieldOffset(idx);
  5998. if (Offset % 64) // Ignore doubles that are not aligned.
  5999. continue;
  6000. // Add ((Offset - LastOffset) / 64) args of type i64.
  6001. for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
  6002. ArgList.push_back(I64);
  6003. // Add double type.
  6004. ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
  6005. LastOffset = Offset + 64;
  6006. }
  6007. CoerceToIntArgs(TySize - LastOffset, IntArgList);
  6008. ArgList.append(IntArgList.begin(), IntArgList.end());
  6009. return llvm::StructType::get(getVMContext(), ArgList);
  6010. }
  6011. llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
  6012. uint64_t Offset) const {
  6013. if (OrigOffset + MinABIStackAlignInBytes > Offset)
  6014. return nullptr;
  6015. return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
  6016. }
  6017. ABIArgInfo
  6018. MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
  6019. Ty = useFirstFieldIfTransparentUnion(Ty);
  6020. uint64_t OrigOffset = Offset;
  6021. uint64_t TySize = getContext().getTypeSize(Ty);
  6022. uint64_t Align = getContext().getTypeAlign(Ty) / 8;
  6023. Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
  6024. (uint64_t)StackAlignInBytes);
  6025. unsigned CurrOffset = llvm::alignTo(Offset, Align);
  6026. Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
  6027. if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
  6028. // Ignore empty aggregates.
  6029. if (TySize == 0)
  6030. return ABIArgInfo::getIgnore();
  6031. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  6032. Offset = OrigOffset + MinABIStackAlignInBytes;
  6033. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  6034. }
  6035. // If we have reached here, aggregates are passed directly by coercing to
  6036. // another structure type. Padding is inserted if the offset of the
  6037. // aggregate is unaligned.
  6038. ABIArgInfo ArgInfo =
  6039. ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
  6040. getPaddingType(OrigOffset, CurrOffset));
  6041. ArgInfo.setInReg(true);
  6042. return ArgInfo;
  6043. }
  6044. // Treat an enum type as its underlying type.
  6045. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  6046. Ty = EnumTy->getDecl()->getIntegerType();
  6047. // All integral types are promoted to the GPR width.
  6048. if (Ty->isIntegralOrEnumerationType())
  6049. return extendType(Ty);
  6050. return ABIArgInfo::getDirect(
  6051. nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
  6052. }
  6053. llvm::Type*
  6054. MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
  6055. const RecordType *RT = RetTy->getAs<RecordType>();
  6056. SmallVector<llvm::Type*, 8> RTList;
  6057. if (RT && RT->isStructureOrClassType()) {
  6058. const RecordDecl *RD = RT->getDecl();
  6059. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  6060. unsigned FieldCnt = Layout.getFieldCount();
  6061. // N32/64 returns struct/classes in floating point registers if the
  6062. // following conditions are met:
  6063. // 1. The size of the struct/class is no larger than 128-bit.
  6064. // 2. The struct/class has one or two fields all of which are floating
  6065. // point types.
  6066. // 3. The offset of the first field is zero (this follows what gcc does).
  6067. //
  6068. // Any other composite results are returned in integer registers.
  6069. //
  6070. if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
  6071. RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
  6072. for (; b != e; ++b) {
  6073. const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
  6074. if (!BT || !BT->isFloatingPoint())
  6075. break;
  6076. RTList.push_back(CGT.ConvertType(b->getType()));
  6077. }
  6078. if (b == e)
  6079. return llvm::StructType::get(getVMContext(), RTList,
  6080. RD->hasAttr<PackedAttr>());
  6081. RTList.clear();
  6082. }
  6083. }
  6084. CoerceToIntArgs(Size, RTList);
  6085. return llvm::StructType::get(getVMContext(), RTList);
  6086. }
  6087. ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
  6088. uint64_t Size = getContext().getTypeSize(RetTy);
  6089. if (RetTy->isVoidType())
  6090. return ABIArgInfo::getIgnore();
  6091. // O32 doesn't treat zero-sized structs differently from other structs.
  6092. // However, N32/N64 ignores zero sized return values.
  6093. if (!IsO32 && Size == 0)
  6094. return ABIArgInfo::getIgnore();
  6095. if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
  6096. if (Size <= 128) {
  6097. if (RetTy->isAnyComplexType())
  6098. return ABIArgInfo::getDirect();
  6099. // O32 returns integer vectors in registers and N32/N64 returns all small
  6100. // aggregates in registers.
  6101. if (!IsO32 ||
  6102. (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
  6103. ABIArgInfo ArgInfo =
  6104. ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
  6105. ArgInfo.setInReg(true);
  6106. return ArgInfo;
  6107. }
  6108. }
  6109. return getNaturalAlignIndirect(RetTy);
  6110. }
  6111. // Treat an enum type as its underlying type.
  6112. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  6113. RetTy = EnumTy->getDecl()->getIntegerType();
  6114. if (RetTy->isPromotableIntegerType())
  6115. return ABIArgInfo::getExtend(RetTy);
  6116. if ((RetTy->isUnsignedIntegerOrEnumerationType() ||
  6117. RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32)
  6118. return ABIArgInfo::getSignExtend(RetTy);
  6119. return ABIArgInfo::getDirect();
  6120. }
  6121. void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
  6122. ABIArgInfo &RetInfo = FI.getReturnInfo();
  6123. if (!getCXXABI().classifyReturnType(FI))
  6124. RetInfo = classifyReturnType(FI.getReturnType());
  6125. // Check if a pointer to an aggregate is passed as a hidden argument.
  6126. uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
  6127. for (auto &I : FI.arguments())
  6128. I.info = classifyArgumentType(I.type, Offset);
  6129. }
  6130. Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6131. QualType OrigTy) const {
  6132. QualType Ty = OrigTy;
  6133. // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
  6134. // Pointers are also promoted in the same way but this only matters for N32.
  6135. unsigned SlotSizeInBits = IsO32 ? 32 : 64;
  6136. unsigned PtrWidth = getTarget().getPointerWidth(0);
  6137. bool DidPromote = false;
  6138. if ((Ty->isIntegerType() &&
  6139. getContext().getIntWidth(Ty) < SlotSizeInBits) ||
  6140. (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
  6141. DidPromote = true;
  6142. Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
  6143. Ty->isSignedIntegerType());
  6144. }
  6145. auto TyInfo = getContext().getTypeInfoInChars(Ty);
  6146. // The alignment of things in the argument area is never larger than
  6147. // StackAlignInBytes.
  6148. TyInfo.second =
  6149. std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
  6150. // MinABIStackAlignInBytes is the size of argument slots on the stack.
  6151. CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
  6152. Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
  6153. TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
  6154. // If there was a promotion, "unpromote" into a temporary.
  6155. // TODO: can we just use a pointer into a subset of the original slot?
  6156. if (DidPromote) {
  6157. Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
  6158. llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
  6159. // Truncate down to the right width.
  6160. llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
  6161. : CGF.IntPtrTy);
  6162. llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
  6163. if (OrigTy->isPointerType())
  6164. V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
  6165. CGF.Builder.CreateStore(V, Temp);
  6166. Addr = Temp;
  6167. }
  6168. return Addr;
  6169. }
  6170. ABIArgInfo MipsABIInfo::extendType(QualType Ty) const {
  6171. int TySize = getContext().getTypeSize(Ty);
  6172. // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
  6173. if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
  6174. return ABIArgInfo::getSignExtend(Ty);
  6175. return ABIArgInfo::getExtend(Ty);
  6176. }
  6177. bool
  6178. MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  6179. llvm::Value *Address) const {
  6180. // This information comes from gcc's implementation, which seems to
  6181. // as canonical as it gets.
  6182. // Everything on MIPS is 4 bytes. Double-precision FP registers
  6183. // are aliased to pairs of single-precision FP registers.
  6184. llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
  6185. // 0-31 are the general purpose registers, $0 - $31.
  6186. // 32-63 are the floating-point registers, $f0 - $f31.
  6187. // 64 and 65 are the multiply/divide registers, $hi and $lo.
  6188. // 66 is the (notional, I think) register for signal-handler return.
  6189. AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
  6190. // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
  6191. // They are one bit wide and ignored here.
  6192. // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
  6193. // (coprocessor 1 is the FP unit)
  6194. // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
  6195. // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
  6196. // 176-181 are the DSP accumulator registers.
  6197. AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
  6198. return false;
  6199. }
  6200. //===----------------------------------------------------------------------===//
  6201. // AVR ABI Implementation.
  6202. //===----------------------------------------------------------------------===//
  6203. namespace {
  6204. class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
  6205. public:
  6206. AVRTargetCodeGenInfo(CodeGenTypes &CGT)
  6207. : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { }
  6208. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  6209. CodeGen::CodeGenModule &CGM) const override {
  6210. if (GV->isDeclaration())
  6211. return;
  6212. const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
  6213. if (!FD) return;
  6214. auto *Fn = cast<llvm::Function>(GV);
  6215. if (FD->getAttr<AVRInterruptAttr>())
  6216. Fn->addFnAttr("interrupt");
  6217. if (FD->getAttr<AVRSignalAttr>())
  6218. Fn->addFnAttr("signal");
  6219. }
  6220. };
  6221. }
  6222. //===----------------------------------------------------------------------===//
  6223. // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
  6224. // Currently subclassed only to implement custom OpenCL C function attribute
  6225. // handling.
  6226. //===----------------------------------------------------------------------===//
  6227. namespace {
  6228. class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
  6229. public:
  6230. TCETargetCodeGenInfo(CodeGenTypes &CGT)
  6231. : DefaultTargetCodeGenInfo(CGT) {}
  6232. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  6233. CodeGen::CodeGenModule &M) const override;
  6234. };
  6235. void TCETargetCodeGenInfo::setTargetAttributes(
  6236. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
  6237. if (GV->isDeclaration())
  6238. return;
  6239. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  6240. if (!FD) return;
  6241. llvm::Function *F = cast<llvm::Function>(GV);
  6242. if (M.getLangOpts().OpenCL) {
  6243. if (FD->hasAttr<OpenCLKernelAttr>()) {
  6244. // OpenCL C Kernel functions are not subject to inlining
  6245. F->addFnAttr(llvm::Attribute::NoInline);
  6246. const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
  6247. if (Attr) {
  6248. // Convert the reqd_work_group_size() attributes to metadata.
  6249. llvm::LLVMContext &Context = F->getContext();
  6250. llvm::NamedMDNode *OpenCLMetadata =
  6251. M.getModule().getOrInsertNamedMetadata(
  6252. "opencl.kernel_wg_size_info");
  6253. SmallVector<llvm::Metadata *, 5> Operands;
  6254. Operands.push_back(llvm::ConstantAsMetadata::get(F));
  6255. Operands.push_back(
  6256. llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
  6257. M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
  6258. Operands.push_back(
  6259. llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
  6260. M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
  6261. Operands.push_back(
  6262. llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
  6263. M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
  6264. // Add a boolean constant operand for "required" (true) or "hint"
  6265. // (false) for implementing the work_group_size_hint attr later.
  6266. // Currently always true as the hint is not yet implemented.
  6267. Operands.push_back(
  6268. llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
  6269. OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
  6270. }
  6271. }
  6272. }
  6273. }
  6274. }
  6275. //===----------------------------------------------------------------------===//
  6276. // Hexagon ABI Implementation
  6277. //===----------------------------------------------------------------------===//
  6278. namespace {
  6279. class HexagonABIInfo : public ABIInfo {
  6280. public:
  6281. HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
  6282. private:
  6283. ABIArgInfo classifyReturnType(QualType RetTy) const;
  6284. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  6285. void computeInfo(CGFunctionInfo &FI) const override;
  6286. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6287. QualType Ty) const override;
  6288. };
  6289. class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
  6290. public:
  6291. HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
  6292. :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
  6293. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  6294. return 29;
  6295. }
  6296. };
  6297. }
  6298. void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
  6299. if (!getCXXABI().classifyReturnType(FI))
  6300. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  6301. for (auto &I : FI.arguments())
  6302. I.info = classifyArgumentType(I.type);
  6303. }
  6304. ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
  6305. if (!isAggregateTypeForABI(Ty)) {
  6306. // Treat an enum type as its underlying type.
  6307. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  6308. Ty = EnumTy->getDecl()->getIntegerType();
  6309. return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
  6310. : ABIArgInfo::getDirect());
  6311. }
  6312. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  6313. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  6314. // Ignore empty records.
  6315. if (isEmptyRecord(getContext(), Ty, true))
  6316. return ABIArgInfo::getIgnore();
  6317. uint64_t Size = getContext().getTypeSize(Ty);
  6318. if (Size > 64)
  6319. return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
  6320. // Pass in the smallest viable integer type.
  6321. else if (Size > 32)
  6322. return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
  6323. else if (Size > 16)
  6324. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  6325. else if (Size > 8)
  6326. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  6327. else
  6328. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  6329. }
  6330. ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
  6331. if (RetTy->isVoidType())
  6332. return ABIArgInfo::getIgnore();
  6333. // Large vector types should be returned via memory.
  6334. if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
  6335. return getNaturalAlignIndirect(RetTy);
  6336. if (!isAggregateTypeForABI(RetTy)) {
  6337. // Treat an enum type as its underlying type.
  6338. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  6339. RetTy = EnumTy->getDecl()->getIntegerType();
  6340. return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
  6341. : ABIArgInfo::getDirect());
  6342. }
  6343. if (isEmptyRecord(getContext(), RetTy, true))
  6344. return ABIArgInfo::getIgnore();
  6345. // Aggregates <= 8 bytes are returned in r0; other aggregates
  6346. // are returned indirectly.
  6347. uint64_t Size = getContext().getTypeSize(RetTy);
  6348. if (Size <= 64) {
  6349. // Return in the smallest viable integer type.
  6350. if (Size <= 8)
  6351. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  6352. if (Size <= 16)
  6353. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  6354. if (Size <= 32)
  6355. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  6356. return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
  6357. }
  6358. return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
  6359. }
  6360. Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6361. QualType Ty) const {
  6362. // FIXME: Someone needs to audit that this handle alignment correctly.
  6363. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
  6364. getContext().getTypeInfoInChars(Ty),
  6365. CharUnits::fromQuantity(4),
  6366. /*AllowHigherAlign*/ true);
  6367. }
  6368. //===----------------------------------------------------------------------===//
  6369. // Lanai ABI Implementation
  6370. //===----------------------------------------------------------------------===//
  6371. namespace {
  6372. class LanaiABIInfo : public DefaultABIInfo {
  6373. public:
  6374. LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  6375. bool shouldUseInReg(QualType Ty, CCState &State) const;
  6376. void computeInfo(CGFunctionInfo &FI) const override {
  6377. CCState State(FI.getCallingConvention());
  6378. // Lanai uses 4 registers to pass arguments unless the function has the
  6379. // regparm attribute set.
  6380. if (FI.getHasRegParm()) {
  6381. State.FreeRegs = FI.getRegParm();
  6382. } else {
  6383. State.FreeRegs = 4;
  6384. }
  6385. if (!getCXXABI().classifyReturnType(FI))
  6386. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  6387. for (auto &I : FI.arguments())
  6388. I.info = classifyArgumentType(I.type, State);
  6389. }
  6390. ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
  6391. ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
  6392. };
  6393. } // end anonymous namespace
  6394. bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
  6395. unsigned Size = getContext().getTypeSize(Ty);
  6396. unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
  6397. if (SizeInRegs == 0)
  6398. return false;
  6399. if (SizeInRegs > State.FreeRegs) {
  6400. State.FreeRegs = 0;
  6401. return false;
  6402. }
  6403. State.FreeRegs -= SizeInRegs;
  6404. return true;
  6405. }
  6406. ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
  6407. CCState &State) const {
  6408. if (!ByVal) {
  6409. if (State.FreeRegs) {
  6410. --State.FreeRegs; // Non-byval indirects just use one pointer.
  6411. return getNaturalAlignIndirectInReg(Ty);
  6412. }
  6413. return getNaturalAlignIndirect(Ty, false);
  6414. }
  6415. // Compute the byval alignment.
  6416. const unsigned MinABIStackAlignInBytes = 4;
  6417. unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
  6418. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
  6419. /*Realign=*/TypeAlign >
  6420. MinABIStackAlignInBytes);
  6421. }
  6422. ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
  6423. CCState &State) const {
  6424. // Check with the C++ ABI first.
  6425. const RecordType *RT = Ty->getAs<RecordType>();
  6426. if (RT) {
  6427. CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
  6428. if (RAA == CGCXXABI::RAA_Indirect) {
  6429. return getIndirectResult(Ty, /*ByVal=*/false, State);
  6430. } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
  6431. return getNaturalAlignIndirect(Ty, /*ByRef=*/true);
  6432. }
  6433. }
  6434. if (isAggregateTypeForABI(Ty)) {
  6435. // Structures with flexible arrays are always indirect.
  6436. if (RT && RT->getDecl()->hasFlexibleArrayMember())
  6437. return getIndirectResult(Ty, /*ByVal=*/true, State);
  6438. // Ignore empty structs/unions.
  6439. if (isEmptyRecord(getContext(), Ty, true))
  6440. return ABIArgInfo::getIgnore();
  6441. llvm::LLVMContext &LLVMContext = getVMContext();
  6442. unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
  6443. if (SizeInRegs <= State.FreeRegs) {
  6444. llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
  6445. SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
  6446. llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
  6447. State.FreeRegs -= SizeInRegs;
  6448. return ABIArgInfo::getDirectInReg(Result);
  6449. } else {
  6450. State.FreeRegs = 0;
  6451. }
  6452. return getIndirectResult(Ty, true, State);
  6453. }
  6454. // Treat an enum type as its underlying type.
  6455. if (const auto *EnumTy = Ty->getAs<EnumType>())
  6456. Ty = EnumTy->getDecl()->getIntegerType();
  6457. bool InReg = shouldUseInReg(Ty, State);
  6458. if (Ty->isPromotableIntegerType()) {
  6459. if (InReg)
  6460. return ABIArgInfo::getDirectInReg();
  6461. return ABIArgInfo::getExtend(Ty);
  6462. }
  6463. if (InReg)
  6464. return ABIArgInfo::getDirectInReg();
  6465. return ABIArgInfo::getDirect();
  6466. }
  6467. namespace {
  6468. class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
  6469. public:
  6470. LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  6471. : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {}
  6472. };
  6473. }
  6474. //===----------------------------------------------------------------------===//
  6475. // AMDGPU ABI Implementation
  6476. //===----------------------------------------------------------------------===//
  6477. namespace {
  6478. class AMDGPUABIInfo final : public DefaultABIInfo {
  6479. private:
  6480. static const unsigned MaxNumRegsForArgsRet = 16;
  6481. unsigned numRegsForType(QualType Ty) const;
  6482. bool isHomogeneousAggregateBaseType(QualType Ty) const override;
  6483. bool isHomogeneousAggregateSmallEnough(const Type *Base,
  6484. uint64_t Members) const override;
  6485. public:
  6486. explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
  6487. DefaultABIInfo(CGT) {}
  6488. ABIArgInfo classifyReturnType(QualType RetTy) const;
  6489. ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
  6490. ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
  6491. void computeInfo(CGFunctionInfo &FI) const override;
  6492. };
  6493. bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  6494. return true;
  6495. }
  6496. bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
  6497. const Type *Base, uint64_t Members) const {
  6498. uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
  6499. // Homogeneous Aggregates may occupy at most 16 registers.
  6500. return Members * NumRegs <= MaxNumRegsForArgsRet;
  6501. }
  6502. /// Estimate number of registers the type will use when passed in registers.
  6503. unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
  6504. unsigned NumRegs = 0;
  6505. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  6506. // Compute from the number of elements. The reported size is based on the
  6507. // in-memory size, which includes the padding 4th element for 3-vectors.
  6508. QualType EltTy = VT->getElementType();
  6509. unsigned EltSize = getContext().getTypeSize(EltTy);
  6510. // 16-bit element vectors should be passed as packed.
  6511. if (EltSize == 16)
  6512. return (VT->getNumElements() + 1) / 2;
  6513. unsigned EltNumRegs = (EltSize + 31) / 32;
  6514. return EltNumRegs * VT->getNumElements();
  6515. }
  6516. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  6517. const RecordDecl *RD = RT->getDecl();
  6518. assert(!RD->hasFlexibleArrayMember());
  6519. for (const FieldDecl *Field : RD->fields()) {
  6520. QualType FieldTy = Field->getType();
  6521. NumRegs += numRegsForType(FieldTy);
  6522. }
  6523. return NumRegs;
  6524. }
  6525. return (getContext().getTypeSize(Ty) + 31) / 32;
  6526. }
  6527. void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
  6528. llvm::CallingConv::ID CC = FI.getCallingConvention();
  6529. if (!getCXXABI().classifyReturnType(FI))
  6530. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  6531. unsigned NumRegsLeft = MaxNumRegsForArgsRet;
  6532. for (auto &Arg : FI.arguments()) {
  6533. if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
  6534. Arg.info = classifyKernelArgumentType(Arg.type);
  6535. } else {
  6536. Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
  6537. }
  6538. }
  6539. }
  6540. ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
  6541. if (isAggregateTypeForABI(RetTy)) {
  6542. // Records with non-trivial destructors/copy-constructors should not be
  6543. // returned by value.
  6544. if (!getRecordArgABI(RetTy, getCXXABI())) {
  6545. // Ignore empty structs/unions.
  6546. if (isEmptyRecord(getContext(), RetTy, true))
  6547. return ABIArgInfo::getIgnore();
  6548. // Lower single-element structs to just return a regular value.
  6549. if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
  6550. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  6551. if (const RecordType *RT = RetTy->getAs<RecordType>()) {
  6552. const RecordDecl *RD = RT->getDecl();
  6553. if (RD->hasFlexibleArrayMember())
  6554. return DefaultABIInfo::classifyReturnType(RetTy);
  6555. }
  6556. // Pack aggregates <= 4 bytes into single VGPR or pair.
  6557. uint64_t Size = getContext().getTypeSize(RetTy);
  6558. if (Size <= 16)
  6559. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  6560. if (Size <= 32)
  6561. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  6562. if (Size <= 64) {
  6563. llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
  6564. return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
  6565. }
  6566. if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
  6567. return ABIArgInfo::getDirect();
  6568. }
  6569. }
  6570. // Otherwise just do the default thing.
  6571. return DefaultABIInfo::classifyReturnType(RetTy);
  6572. }
  6573. /// For kernels all parameters are really passed in a special buffer. It doesn't
  6574. /// make sense to pass anything byval, so everything must be direct.
  6575. ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
  6576. Ty = useFirstFieldIfTransparentUnion(Ty);
  6577. // TODO: Can we omit empty structs?
  6578. // Coerce single element structs to its element.
  6579. if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
  6580. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  6581. // If we set CanBeFlattened to true, CodeGen will expand the struct to its
  6582. // individual elements, which confuses the Clover OpenCL backend; therefore we
  6583. // have to set it to false here. Other args of getDirect() are just defaults.
  6584. return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
  6585. }
  6586. ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
  6587. unsigned &NumRegsLeft) const {
  6588. assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
  6589. Ty = useFirstFieldIfTransparentUnion(Ty);
  6590. if (isAggregateTypeForABI(Ty)) {
  6591. // Records with non-trivial destructors/copy-constructors should not be
  6592. // passed by value.
  6593. if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
  6594. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  6595. // Ignore empty structs/unions.
  6596. if (isEmptyRecord(getContext(), Ty, true))
  6597. return ABIArgInfo::getIgnore();
  6598. // Lower single-element structs to just pass a regular value. TODO: We
  6599. // could do reasonable-size multiple-element structs too, using getExpand(),
  6600. // though watch out for things like bitfields.
  6601. if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
  6602. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  6603. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  6604. const RecordDecl *RD = RT->getDecl();
  6605. if (RD->hasFlexibleArrayMember())
  6606. return DefaultABIInfo::classifyArgumentType(Ty);
  6607. }
  6608. // Pack aggregates <= 8 bytes into single VGPR or pair.
  6609. uint64_t Size = getContext().getTypeSize(Ty);
  6610. if (Size <= 64) {
  6611. unsigned NumRegs = (Size + 31) / 32;
  6612. NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
  6613. if (Size <= 16)
  6614. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  6615. if (Size <= 32)
  6616. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  6617. // XXX: Should this be i64 instead, and should the limit increase?
  6618. llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
  6619. return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
  6620. }
  6621. if (NumRegsLeft > 0) {
  6622. unsigned NumRegs = numRegsForType(Ty);
  6623. if (NumRegsLeft >= NumRegs) {
  6624. NumRegsLeft -= NumRegs;
  6625. return ABIArgInfo::getDirect();
  6626. }
  6627. }
  6628. }
  6629. // Otherwise just do the default thing.
  6630. ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
  6631. if (!ArgInfo.isIndirect()) {
  6632. unsigned NumRegs = numRegsForType(Ty);
  6633. NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
  6634. }
  6635. return ArgInfo;
  6636. }
  6637. class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
  6638. public:
  6639. AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
  6640. : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {}
  6641. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  6642. CodeGen::CodeGenModule &M) const override;
  6643. unsigned getOpenCLKernelCallingConv() const override;
  6644. llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
  6645. llvm::PointerType *T, QualType QT) const override;
  6646. LangAS getASTAllocaAddressSpace() const override {
  6647. return getLangASFromTargetAS(
  6648. getABIInfo().getDataLayout().getAllocaAddrSpace());
  6649. }
  6650. LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
  6651. const VarDecl *D) const override;
  6652. llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
  6653. SyncScope Scope,
  6654. llvm::AtomicOrdering Ordering,
  6655. llvm::LLVMContext &Ctx) const override;
  6656. llvm::Function *
  6657. createEnqueuedBlockKernel(CodeGenFunction &CGF,
  6658. llvm::Function *BlockInvokeFunc,
  6659. llvm::Value *BlockLiteral) const override;
  6660. bool shouldEmitStaticExternCAliases() const override;
  6661. void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
  6662. };
  6663. }
  6664. static bool requiresAMDGPUProtectedVisibility(const Decl *D,
  6665. llvm::GlobalValue *GV) {
  6666. if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
  6667. return false;
  6668. return D->hasAttr<OpenCLKernelAttr>() ||
  6669. (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
  6670. (isa<VarDecl>(D) &&
  6671. (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
  6672. D->hasAttr<HIPPinnedShadowAttr>()));
  6673. }
  6674. static bool requiresAMDGPUDefaultVisibility(const Decl *D,
  6675. llvm::GlobalValue *GV) {
  6676. if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
  6677. return false;
  6678. return isa<VarDecl>(D) && D->hasAttr<HIPPinnedShadowAttr>();
  6679. }
  6680. void AMDGPUTargetCodeGenInfo::setTargetAttributes(
  6681. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
  6682. if (requiresAMDGPUDefaultVisibility(D, GV)) {
  6683. GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
  6684. GV->setDSOLocal(false);
  6685. } else if (requiresAMDGPUProtectedVisibility(D, GV)) {
  6686. GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
  6687. GV->setDSOLocal(true);
  6688. }
  6689. if (GV->isDeclaration())
  6690. return;
  6691. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  6692. if (!FD)
  6693. return;
  6694. llvm::Function *F = cast<llvm::Function>(GV);
  6695. const auto *ReqdWGS = M.getLangOpts().OpenCL ?
  6696. FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
  6697. const bool IsOpenCLKernel = M.getLangOpts().OpenCL &&
  6698. FD->hasAttr<OpenCLKernelAttr>();
  6699. const bool IsHIPKernel = M.getLangOpts().HIP &&
  6700. FD->hasAttr<CUDAGlobalAttr>();
  6701. if ((IsOpenCLKernel || IsHIPKernel) &&
  6702. (M.getTriple().getOS() == llvm::Triple::AMDHSA))
  6703. F->addFnAttr("amdgpu-implicitarg-num-bytes", "56");
  6704. const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
  6705. if (ReqdWGS || FlatWGS) {
  6706. unsigned Min = 0;
  6707. unsigned Max = 0;
  6708. if (FlatWGS) {
  6709. Min = FlatWGS->getMin()
  6710. ->EvaluateKnownConstInt(M.getContext())
  6711. .getExtValue();
  6712. Max = FlatWGS->getMax()
  6713. ->EvaluateKnownConstInt(M.getContext())
  6714. .getExtValue();
  6715. }
  6716. if (ReqdWGS && Min == 0 && Max == 0)
  6717. Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
  6718. if (Min != 0) {
  6719. assert(Min <= Max && "Min must be less than or equal Max");
  6720. std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
  6721. F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
  6722. } else
  6723. assert(Max == 0 && "Max must be zero");
  6724. } else if (IsOpenCLKernel || IsHIPKernel) {
  6725. // By default, restrict the maximum size to 256.
  6726. F->addFnAttr("amdgpu-flat-work-group-size", "1,256");
  6727. }
  6728. if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
  6729. unsigned Min =
  6730. Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue();
  6731. unsigned Max = Attr->getMax() ? Attr->getMax()
  6732. ->EvaluateKnownConstInt(M.getContext())
  6733. .getExtValue()
  6734. : 0;
  6735. if (Min != 0) {
  6736. assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
  6737. std::string AttrVal = llvm::utostr(Min);
  6738. if (Max != 0)
  6739. AttrVal = AttrVal + "," + llvm::utostr(Max);
  6740. F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
  6741. } else
  6742. assert(Max == 0 && "Max must be zero");
  6743. }
  6744. if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
  6745. unsigned NumSGPR = Attr->getNumSGPR();
  6746. if (NumSGPR != 0)
  6747. F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
  6748. }
  6749. if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
  6750. uint32_t NumVGPR = Attr->getNumVGPR();
  6751. if (NumVGPR != 0)
  6752. F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
  6753. }
  6754. }
  6755. unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
  6756. return llvm::CallingConv::AMDGPU_KERNEL;
  6757. }
  6758. // Currently LLVM assumes null pointers always have value 0,
  6759. // which results in incorrectly transformed IR. Therefore, instead of
  6760. // emitting null pointers in private and local address spaces, a null
  6761. // pointer in generic address space is emitted which is casted to a
  6762. // pointer in local or private address space.
  6763. llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
  6764. const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
  6765. QualType QT) const {
  6766. if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
  6767. return llvm::ConstantPointerNull::get(PT);
  6768. auto &Ctx = CGM.getContext();
  6769. auto NPT = llvm::PointerType::get(PT->getElementType(),
  6770. Ctx.getTargetAddressSpace(LangAS::opencl_generic));
  6771. return llvm::ConstantExpr::getAddrSpaceCast(
  6772. llvm::ConstantPointerNull::get(NPT), PT);
  6773. }
  6774. LangAS
  6775. AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
  6776. const VarDecl *D) const {
  6777. assert(!CGM.getLangOpts().OpenCL &&
  6778. !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
  6779. "Address space agnostic languages only");
  6780. LangAS DefaultGlobalAS = getLangASFromTargetAS(
  6781. CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
  6782. if (!D)
  6783. return DefaultGlobalAS;
  6784. LangAS AddrSpace = D->getType().getAddressSpace();
  6785. assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace));
  6786. if (AddrSpace != LangAS::Default)
  6787. return AddrSpace;
  6788. if (CGM.isTypeConstant(D->getType(), false)) {
  6789. if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
  6790. return ConstAS.getValue();
  6791. }
  6792. return DefaultGlobalAS;
  6793. }
  6794. llvm::SyncScope::ID
  6795. AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
  6796. SyncScope Scope,
  6797. llvm::AtomicOrdering Ordering,
  6798. llvm::LLVMContext &Ctx) const {
  6799. std::string Name;
  6800. switch (Scope) {
  6801. case SyncScope::OpenCLWorkGroup:
  6802. Name = "workgroup";
  6803. break;
  6804. case SyncScope::OpenCLDevice:
  6805. Name = "agent";
  6806. break;
  6807. case SyncScope::OpenCLAllSVMDevices:
  6808. Name = "";
  6809. break;
  6810. case SyncScope::OpenCLSubGroup:
  6811. Name = "wavefront";
  6812. }
  6813. if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) {
  6814. if (!Name.empty())
  6815. Name = Twine(Twine(Name) + Twine("-")).str();
  6816. Name = Twine(Twine(Name) + Twine("one-as")).str();
  6817. }
  6818. return Ctx.getOrInsertSyncScopeID(Name);
  6819. }
  6820. bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
  6821. return false;
  6822. }
  6823. void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention(
  6824. const FunctionType *&FT) const {
  6825. FT = getABIInfo().getContext().adjustFunctionType(
  6826. FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
  6827. }
  6828. //===----------------------------------------------------------------------===//
  6829. // SPARC v8 ABI Implementation.
  6830. // Based on the SPARC Compliance Definition version 2.4.1.
  6831. //
  6832. // Ensures that complex values are passed in registers.
  6833. //
  6834. namespace {
  6835. class SparcV8ABIInfo : public DefaultABIInfo {
  6836. public:
  6837. SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  6838. private:
  6839. ABIArgInfo classifyReturnType(QualType RetTy) const;
  6840. void computeInfo(CGFunctionInfo &FI) const override;
  6841. };
  6842. } // end anonymous namespace
  6843. ABIArgInfo
  6844. SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
  6845. if (Ty->isAnyComplexType()) {
  6846. return ABIArgInfo::getDirect();
  6847. }
  6848. else {
  6849. return DefaultABIInfo::classifyReturnType(Ty);
  6850. }
  6851. }
  6852. void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  6853. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  6854. for (auto &Arg : FI.arguments())
  6855. Arg.info = classifyArgumentType(Arg.type);
  6856. }
  6857. namespace {
  6858. class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
  6859. public:
  6860. SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
  6861. : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {}
  6862. };
  6863. } // end anonymous namespace
  6864. //===----------------------------------------------------------------------===//
  6865. // SPARC v9 ABI Implementation.
  6866. // Based on the SPARC Compliance Definition version 2.4.1.
  6867. //
  6868. // Function arguments a mapped to a nominal "parameter array" and promoted to
  6869. // registers depending on their type. Each argument occupies 8 or 16 bytes in
  6870. // the array, structs larger than 16 bytes are passed indirectly.
  6871. //
  6872. // One case requires special care:
  6873. //
  6874. // struct mixed {
  6875. // int i;
  6876. // float f;
  6877. // };
  6878. //
  6879. // When a struct mixed is passed by value, it only occupies 8 bytes in the
  6880. // parameter array, but the int is passed in an integer register, and the float
  6881. // is passed in a floating point register. This is represented as two arguments
  6882. // with the LLVM IR inreg attribute:
  6883. //
  6884. // declare void f(i32 inreg %i, float inreg %f)
  6885. //
  6886. // The code generator will only allocate 4 bytes from the parameter array for
  6887. // the inreg arguments. All other arguments are allocated a multiple of 8
  6888. // bytes.
  6889. //
  6890. namespace {
  6891. class SparcV9ABIInfo : public ABIInfo {
  6892. public:
  6893. SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
  6894. private:
  6895. ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
  6896. void computeInfo(CGFunctionInfo &FI) const override;
  6897. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6898. QualType Ty) const override;
  6899. // Coercion type builder for structs passed in registers. The coercion type
  6900. // serves two purposes:
  6901. //
  6902. // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
  6903. // in registers.
  6904. // 2. Expose aligned floating point elements as first-level elements, so the
  6905. // code generator knows to pass them in floating point registers.
  6906. //
  6907. // We also compute the InReg flag which indicates that the struct contains
  6908. // aligned 32-bit floats.
  6909. //
  6910. struct CoerceBuilder {
  6911. llvm::LLVMContext &Context;
  6912. const llvm::DataLayout &DL;
  6913. SmallVector<llvm::Type*, 8> Elems;
  6914. uint64_t Size;
  6915. bool InReg;
  6916. CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
  6917. : Context(c), DL(dl), Size(0), InReg(false) {}
  6918. // Pad Elems with integers until Size is ToSize.
  6919. void pad(uint64_t ToSize) {
  6920. assert(ToSize >= Size && "Cannot remove elements");
  6921. if (ToSize == Size)
  6922. return;
  6923. // Finish the current 64-bit word.
  6924. uint64_t Aligned = llvm::alignTo(Size, 64);
  6925. if (Aligned > Size && Aligned <= ToSize) {
  6926. Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
  6927. Size = Aligned;
  6928. }
  6929. // Add whole 64-bit words.
  6930. while (Size + 64 <= ToSize) {
  6931. Elems.push_back(llvm::Type::getInt64Ty(Context));
  6932. Size += 64;
  6933. }
  6934. // Final in-word padding.
  6935. if (Size < ToSize) {
  6936. Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
  6937. Size = ToSize;
  6938. }
  6939. }
  6940. // Add a floating point element at Offset.
  6941. void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
  6942. // Unaligned floats are treated as integers.
  6943. if (Offset % Bits)
  6944. return;
  6945. // The InReg flag is only required if there are any floats < 64 bits.
  6946. if (Bits < 64)
  6947. InReg = true;
  6948. pad(Offset);
  6949. Elems.push_back(Ty);
  6950. Size = Offset + Bits;
  6951. }
  6952. // Add a struct type to the coercion type, starting at Offset (in bits).
  6953. void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
  6954. const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
  6955. for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
  6956. llvm::Type *ElemTy = StrTy->getElementType(i);
  6957. uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
  6958. switch (ElemTy->getTypeID()) {
  6959. case llvm::Type::StructTyID:
  6960. addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
  6961. break;
  6962. case llvm::Type::FloatTyID:
  6963. addFloat(ElemOffset, ElemTy, 32);
  6964. break;
  6965. case llvm::Type::DoubleTyID:
  6966. addFloat(ElemOffset, ElemTy, 64);
  6967. break;
  6968. case llvm::Type::FP128TyID:
  6969. addFloat(ElemOffset, ElemTy, 128);
  6970. break;
  6971. case llvm::Type::PointerTyID:
  6972. if (ElemOffset % 64 == 0) {
  6973. pad(ElemOffset);
  6974. Elems.push_back(ElemTy);
  6975. Size += 64;
  6976. }
  6977. break;
  6978. default:
  6979. break;
  6980. }
  6981. }
  6982. }
  6983. // Check if Ty is a usable substitute for the coercion type.
  6984. bool isUsableType(llvm::StructType *Ty) const {
  6985. return llvm::makeArrayRef(Elems) == Ty->elements();
  6986. }
  6987. // Get the coercion type as a literal struct type.
  6988. llvm::Type *getType() const {
  6989. if (Elems.size() == 1)
  6990. return Elems.front();
  6991. else
  6992. return llvm::StructType::get(Context, Elems);
  6993. }
  6994. };
  6995. };
  6996. } // end anonymous namespace
  6997. ABIArgInfo
  6998. SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
  6999. if (Ty->isVoidType())
  7000. return ABIArgInfo::getIgnore();
  7001. uint64_t Size = getContext().getTypeSize(Ty);
  7002. // Anything too big to fit in registers is passed with an explicit indirect
  7003. // pointer / sret pointer.
  7004. if (Size > SizeLimit)
  7005. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  7006. // Treat an enum type as its underlying type.
  7007. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  7008. Ty = EnumTy->getDecl()->getIntegerType();
  7009. // Integer types smaller than a register are extended.
  7010. if (Size < 64 && Ty->isIntegerType())
  7011. return ABIArgInfo::getExtend(Ty);
  7012. // Other non-aggregates go in registers.
  7013. if (!isAggregateTypeForABI(Ty))
  7014. return ABIArgInfo::getDirect();
  7015. // If a C++ object has either a non-trivial copy constructor or a non-trivial
  7016. // destructor, it is passed with an explicit indirect pointer / sret pointer.
  7017. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  7018. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  7019. // This is a small aggregate type that should be passed in registers.
  7020. // Build a coercion type from the LLVM struct type.
  7021. llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
  7022. if (!StrTy)
  7023. return ABIArgInfo::getDirect();
  7024. CoerceBuilder CB(getVMContext(), getDataLayout());
  7025. CB.addStruct(0, StrTy);
  7026. CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
  7027. // Try to use the original type for coercion.
  7028. llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
  7029. if (CB.InReg)
  7030. return ABIArgInfo::getDirectInReg(CoerceTy);
  7031. else
  7032. return ABIArgInfo::getDirect(CoerceTy);
  7033. }
  7034. Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  7035. QualType Ty) const {
  7036. ABIArgInfo AI = classifyType(Ty, 16 * 8);
  7037. llvm::Type *ArgTy = CGT.ConvertType(Ty);
  7038. if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
  7039. AI.setCoerceToType(ArgTy);
  7040. CharUnits SlotSize = CharUnits::fromQuantity(8);
  7041. CGBuilderTy &Builder = CGF.Builder;
  7042. Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
  7043. llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
  7044. auto TypeInfo = getContext().getTypeInfoInChars(Ty);
  7045. Address ArgAddr = Address::invalid();
  7046. CharUnits Stride;
  7047. switch (AI.getKind()) {
  7048. case ABIArgInfo::Expand:
  7049. case ABIArgInfo::CoerceAndExpand:
  7050. case ABIArgInfo::InAlloca:
  7051. llvm_unreachable("Unsupported ABI kind for va_arg");
  7052. case ABIArgInfo::Extend: {
  7053. Stride = SlotSize;
  7054. CharUnits Offset = SlotSize - TypeInfo.first;
  7055. ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
  7056. break;
  7057. }
  7058. case ABIArgInfo::Direct: {
  7059. auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
  7060. Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
  7061. ArgAddr = Addr;
  7062. break;
  7063. }
  7064. case ABIArgInfo::Indirect:
  7065. Stride = SlotSize;
  7066. ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
  7067. ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
  7068. TypeInfo.second);
  7069. break;
  7070. case ABIArgInfo::Ignore:
  7071. return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
  7072. }
  7073. // Update VAList.
  7074. Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next");
  7075. Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
  7076. return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
  7077. }
  7078. void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  7079. FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
  7080. for (auto &I : FI.arguments())
  7081. I.info = classifyType(I.type, 16 * 8);
  7082. }
  7083. namespace {
  7084. class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
  7085. public:
  7086. SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
  7087. : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
  7088. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  7089. return 14;
  7090. }
  7091. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  7092. llvm::Value *Address) const override;
  7093. };
  7094. } // end anonymous namespace
  7095. bool
  7096. SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  7097. llvm::Value *Address) const {
  7098. // This is calculated from the LLVM and GCC tables and verified
  7099. // against gcc output. AFAIK all ABIs use the same encoding.
  7100. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  7101. llvm::IntegerType *i8 = CGF.Int8Ty;
  7102. llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
  7103. llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
  7104. // 0-31: the 8-byte general-purpose registers
  7105. AssignToArrayRange(Builder, Address, Eight8, 0, 31);
  7106. // 32-63: f0-31, the 4-byte floating-point registers
  7107. AssignToArrayRange(Builder, Address, Four8, 32, 63);
  7108. // Y = 64
  7109. // PSR = 65
  7110. // WIM = 66
  7111. // TBR = 67
  7112. // PC = 68
  7113. // NPC = 69
  7114. // FSR = 70
  7115. // CSR = 71
  7116. AssignToArrayRange(Builder, Address, Eight8, 64, 71);
  7117. // 72-87: d0-15, the 8-byte floating-point registers
  7118. AssignToArrayRange(Builder, Address, Eight8, 72, 87);
  7119. return false;
  7120. }
  7121. // ARC ABI implementation.
  7122. namespace {
  7123. class ARCABIInfo : public DefaultABIInfo {
  7124. public:
  7125. using DefaultABIInfo::DefaultABIInfo;
  7126. private:
  7127. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  7128. QualType Ty) const override;
  7129. void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const {
  7130. if (!State.FreeRegs)
  7131. return;
  7132. if (Info.isIndirect() && Info.getInReg())
  7133. State.FreeRegs--;
  7134. else if (Info.isDirect() && Info.getInReg()) {
  7135. unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32;
  7136. if (sz < State.FreeRegs)
  7137. State.FreeRegs -= sz;
  7138. else
  7139. State.FreeRegs = 0;
  7140. }
  7141. }
  7142. void computeInfo(CGFunctionInfo &FI) const override {
  7143. CCState State(FI.getCallingConvention());
  7144. // ARC uses 8 registers to pass arguments.
  7145. State.FreeRegs = 8;
  7146. if (!getCXXABI().classifyReturnType(FI))
  7147. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  7148. updateState(FI.getReturnInfo(), FI.getReturnType(), State);
  7149. for (auto &I : FI.arguments()) {
  7150. I.info = classifyArgumentType(I.type, State.FreeRegs);
  7151. updateState(I.info, I.type, State);
  7152. }
  7153. }
  7154. ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const;
  7155. ABIArgInfo getIndirectByValue(QualType Ty) const;
  7156. ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const;
  7157. ABIArgInfo classifyReturnType(QualType RetTy) const;
  7158. };
  7159. class ARCTargetCodeGenInfo : public TargetCodeGenInfo {
  7160. public:
  7161. ARCTargetCodeGenInfo(CodeGenTypes &CGT)
  7162. : TargetCodeGenInfo(new ARCABIInfo(CGT)) {}
  7163. };
  7164. ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const {
  7165. return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) :
  7166. getNaturalAlignIndirect(Ty, false);
  7167. }
  7168. ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const {
  7169. // Compute the byval alignment.
  7170. const unsigned MinABIStackAlignInBytes = 4;
  7171. unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
  7172. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
  7173. TypeAlign > MinABIStackAlignInBytes);
  7174. }
  7175. Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  7176. QualType Ty) const {
  7177. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
  7178. getContext().getTypeInfoInChars(Ty),
  7179. CharUnits::fromQuantity(4), true);
  7180. }
  7181. ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty,
  7182. uint8_t FreeRegs) const {
  7183. // Handle the generic C++ ABI.
  7184. const RecordType *RT = Ty->getAs<RecordType>();
  7185. if (RT) {
  7186. CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
  7187. if (RAA == CGCXXABI::RAA_Indirect)
  7188. return getIndirectByRef(Ty, FreeRegs > 0);
  7189. if (RAA == CGCXXABI::RAA_DirectInMemory)
  7190. return getIndirectByValue(Ty);
  7191. }
  7192. // Treat an enum type as its underlying type.
  7193. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  7194. Ty = EnumTy->getDecl()->getIntegerType();
  7195. auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32;
  7196. if (isAggregateTypeForABI(Ty)) {
  7197. // Structures with flexible arrays are always indirect.
  7198. if (RT && RT->getDecl()->hasFlexibleArrayMember())
  7199. return getIndirectByValue(Ty);
  7200. // Ignore empty structs/unions.
  7201. if (isEmptyRecord(getContext(), Ty, true))
  7202. return ABIArgInfo::getIgnore();
  7203. llvm::LLVMContext &LLVMContext = getVMContext();
  7204. llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
  7205. SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
  7206. llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
  7207. return FreeRegs >= SizeInRegs ?
  7208. ABIArgInfo::getDirectInReg(Result) :
  7209. ABIArgInfo::getDirect(Result, 0, nullptr, false);
  7210. }
  7211. return Ty->isPromotableIntegerType() ?
  7212. (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty) :
  7213. ABIArgInfo::getExtend(Ty)) :
  7214. (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg() :
  7215. ABIArgInfo::getDirect());
  7216. }
  7217. ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const {
  7218. if (RetTy->isAnyComplexType())
  7219. return ABIArgInfo::getDirectInReg();
  7220. // Arguments of size > 4 registers are indirect.
  7221. auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32;
  7222. if (RetSize > 4)
  7223. return getIndirectByRef(RetTy, /*HasFreeRegs*/ true);
  7224. return DefaultABIInfo::classifyReturnType(RetTy);
  7225. }
  7226. } // End anonymous namespace.
  7227. //===----------------------------------------------------------------------===//
  7228. // XCore ABI Implementation
  7229. //===----------------------------------------------------------------------===//
  7230. namespace {
  7231. /// A SmallStringEnc instance is used to build up the TypeString by passing
  7232. /// it by reference between functions that append to it.
  7233. typedef llvm::SmallString<128> SmallStringEnc;
  7234. /// TypeStringCache caches the meta encodings of Types.
  7235. ///
  7236. /// The reason for caching TypeStrings is two fold:
  7237. /// 1. To cache a type's encoding for later uses;
  7238. /// 2. As a means to break recursive member type inclusion.
  7239. ///
  7240. /// A cache Entry can have a Status of:
  7241. /// NonRecursive: The type encoding is not recursive;
  7242. /// Recursive: The type encoding is recursive;
  7243. /// Incomplete: An incomplete TypeString;
  7244. /// IncompleteUsed: An incomplete TypeString that has been used in a
  7245. /// Recursive type encoding.
  7246. ///
  7247. /// A NonRecursive entry will have all of its sub-members expanded as fully
  7248. /// as possible. Whilst it may contain types which are recursive, the type
  7249. /// itself is not recursive and thus its encoding may be safely used whenever
  7250. /// the type is encountered.
  7251. ///
  7252. /// A Recursive entry will have all of its sub-members expanded as fully as
  7253. /// possible. The type itself is recursive and it may contain other types which
  7254. /// are recursive. The Recursive encoding must not be used during the expansion
  7255. /// of a recursive type's recursive branch. For simplicity the code uses
  7256. /// IncompleteCount to reject all usage of Recursive encodings for member types.
  7257. ///
  7258. /// An Incomplete entry is always a RecordType and only encodes its
  7259. /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
  7260. /// are placed into the cache during type expansion as a means to identify and
  7261. /// handle recursive inclusion of types as sub-members. If there is recursion
  7262. /// the entry becomes IncompleteUsed.
  7263. ///
  7264. /// During the expansion of a RecordType's members:
  7265. ///
  7266. /// If the cache contains a NonRecursive encoding for the member type, the
  7267. /// cached encoding is used;
  7268. ///
  7269. /// If the cache contains a Recursive encoding for the member type, the
  7270. /// cached encoding is 'Swapped' out, as it may be incorrect, and...
  7271. ///
  7272. /// If the member is a RecordType, an Incomplete encoding is placed into the
  7273. /// cache to break potential recursive inclusion of itself as a sub-member;
  7274. ///
  7275. /// Once a member RecordType has been expanded, its temporary incomplete
  7276. /// entry is removed from the cache. If a Recursive encoding was swapped out
  7277. /// it is swapped back in;
  7278. ///
  7279. /// If an incomplete entry is used to expand a sub-member, the incomplete
  7280. /// entry is marked as IncompleteUsed. The cache keeps count of how many
  7281. /// IncompleteUsed entries it currently contains in IncompleteUsedCount;
  7282. ///
  7283. /// If a member's encoding is found to be a NonRecursive or Recursive viz:
  7284. /// IncompleteUsedCount==0, the member's encoding is added to the cache.
  7285. /// Else the member is part of a recursive type and thus the recursion has
  7286. /// been exited too soon for the encoding to be correct for the member.
  7287. ///
  7288. class TypeStringCache {
  7289. enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
  7290. struct Entry {
  7291. std::string Str; // The encoded TypeString for the type.
  7292. enum Status State; // Information about the encoding in 'Str'.
  7293. std::string Swapped; // A temporary place holder for a Recursive encoding
  7294. // during the expansion of RecordType's members.
  7295. };
  7296. std::map<const IdentifierInfo *, struct Entry> Map;
  7297. unsigned IncompleteCount; // Number of Incomplete entries in the Map.
  7298. unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
  7299. public:
  7300. TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
  7301. void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
  7302. bool removeIncomplete(const IdentifierInfo *ID);
  7303. void addIfComplete(const IdentifierInfo *ID, StringRef Str,
  7304. bool IsRecursive);
  7305. StringRef lookupStr(const IdentifierInfo *ID);
  7306. };
  7307. /// TypeString encodings for enum & union fields must be order.
  7308. /// FieldEncoding is a helper for this ordering process.
  7309. class FieldEncoding {
  7310. bool HasName;
  7311. std::string Enc;
  7312. public:
  7313. FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
  7314. StringRef str() { return Enc; }
  7315. bool operator<(const FieldEncoding &rhs) const {
  7316. if (HasName != rhs.HasName) return HasName;
  7317. return Enc < rhs.Enc;
  7318. }
  7319. };
  7320. class XCoreABIInfo : public DefaultABIInfo {
  7321. public:
  7322. XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  7323. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  7324. QualType Ty) const override;
  7325. };
  7326. class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
  7327. mutable TypeStringCache TSC;
  7328. public:
  7329. XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
  7330. :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
  7331. void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
  7332. CodeGen::CodeGenModule &M) const override;
  7333. };
  7334. } // End anonymous namespace.
  7335. // TODO: this implementation is likely now redundant with the default
  7336. // EmitVAArg.
  7337. Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  7338. QualType Ty) const {
  7339. CGBuilderTy &Builder = CGF.Builder;
  7340. // Get the VAList.
  7341. CharUnits SlotSize = CharUnits::fromQuantity(4);
  7342. Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
  7343. // Handle the argument.
  7344. ABIArgInfo AI = classifyArgumentType(Ty);
  7345. CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
  7346. llvm::Type *ArgTy = CGT.ConvertType(Ty);
  7347. if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
  7348. AI.setCoerceToType(ArgTy);
  7349. llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
  7350. Address Val = Address::invalid();
  7351. CharUnits ArgSize = CharUnits::Zero();
  7352. switch (AI.getKind()) {
  7353. case ABIArgInfo::Expand:
  7354. case ABIArgInfo::CoerceAndExpand:
  7355. case ABIArgInfo::InAlloca:
  7356. llvm_unreachable("Unsupported ABI kind for va_arg");
  7357. case ABIArgInfo::Ignore:
  7358. Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
  7359. ArgSize = CharUnits::Zero();
  7360. break;
  7361. case ABIArgInfo::Extend:
  7362. case ABIArgInfo::Direct:
  7363. Val = Builder.CreateBitCast(AP, ArgPtrTy);
  7364. ArgSize = CharUnits::fromQuantity(
  7365. getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
  7366. ArgSize = ArgSize.alignTo(SlotSize);
  7367. break;
  7368. case ABIArgInfo::Indirect:
  7369. Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
  7370. Val = Address(Builder.CreateLoad(Val), TypeAlign);
  7371. ArgSize = SlotSize;
  7372. break;
  7373. }
  7374. // Increment the VAList.
  7375. if (!ArgSize.isZero()) {
  7376. Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize);
  7377. Builder.CreateStore(APN.getPointer(), VAListAddr);
  7378. }
  7379. return Val;
  7380. }
  7381. /// During the expansion of a RecordType, an incomplete TypeString is placed
  7382. /// into the cache as a means to identify and break recursion.
  7383. /// If there is a Recursive encoding in the cache, it is swapped out and will
  7384. /// be reinserted by removeIncomplete().
  7385. /// All other types of encoding should have been used rather than arriving here.
  7386. void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
  7387. std::string StubEnc) {
  7388. if (!ID)
  7389. return;
  7390. Entry &E = Map[ID];
  7391. assert( (E.Str.empty() || E.State == Recursive) &&
  7392. "Incorrectly use of addIncomplete");
  7393. assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
  7394. E.Swapped.swap(E.Str); // swap out the Recursive
  7395. E.Str.swap(StubEnc);
  7396. E.State = Incomplete;
  7397. ++IncompleteCount;
  7398. }
  7399. /// Once the RecordType has been expanded, the temporary incomplete TypeString
  7400. /// must be removed from the cache.
  7401. /// If a Recursive was swapped out by addIncomplete(), it will be replaced.
  7402. /// Returns true if the RecordType was defined recursively.
  7403. bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
  7404. if (!ID)
  7405. return false;
  7406. auto I = Map.find(ID);
  7407. assert(I != Map.end() && "Entry not present");
  7408. Entry &E = I->second;
  7409. assert( (E.State == Incomplete ||
  7410. E.State == IncompleteUsed) &&
  7411. "Entry must be an incomplete type");
  7412. bool IsRecursive = false;
  7413. if (E.State == IncompleteUsed) {
  7414. // We made use of our Incomplete encoding, thus we are recursive.
  7415. IsRecursive = true;
  7416. --IncompleteUsedCount;
  7417. }
  7418. if (E.Swapped.empty())
  7419. Map.erase(I);
  7420. else {
  7421. // Swap the Recursive back.
  7422. E.Swapped.swap(E.Str);
  7423. E.Swapped.clear();
  7424. E.State = Recursive;
  7425. }
  7426. --IncompleteCount;
  7427. return IsRecursive;
  7428. }
  7429. /// Add the encoded TypeString to the cache only if it is NonRecursive or
  7430. /// Recursive (viz: all sub-members were expanded as fully as possible).
  7431. void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
  7432. bool IsRecursive) {
  7433. if (!ID || IncompleteUsedCount)
  7434. return; // No key or it is is an incomplete sub-type so don't add.
  7435. Entry &E = Map[ID];
  7436. if (IsRecursive && !E.Str.empty()) {
  7437. assert(E.State==Recursive && E.Str.size() == Str.size() &&
  7438. "This is not the same Recursive entry");
  7439. // The parent container was not recursive after all, so we could have used
  7440. // this Recursive sub-member entry after all, but we assumed the worse when
  7441. // we started viz: IncompleteCount!=0.
  7442. return;
  7443. }
  7444. assert(E.Str.empty() && "Entry already present");
  7445. E.Str = Str.str();
  7446. E.State = IsRecursive? Recursive : NonRecursive;
  7447. }
  7448. /// Return a cached TypeString encoding for the ID. If there isn't one, or we
  7449. /// are recursively expanding a type (IncompleteCount != 0) and the cached
  7450. /// encoding is Recursive, return an empty StringRef.
  7451. StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
  7452. if (!ID)
  7453. return StringRef(); // We have no key.
  7454. auto I = Map.find(ID);
  7455. if (I == Map.end())
  7456. return StringRef(); // We have no encoding.
  7457. Entry &E = I->second;
  7458. if (E.State == Recursive && IncompleteCount)
  7459. return StringRef(); // We don't use Recursive encodings for member types.
  7460. if (E.State == Incomplete) {
  7461. // The incomplete type is being used to break out of recursion.
  7462. E.State = IncompleteUsed;
  7463. ++IncompleteUsedCount;
  7464. }
  7465. return E.Str;
  7466. }
  7467. /// The XCore ABI includes a type information section that communicates symbol
  7468. /// type information to the linker. The linker uses this information to verify
  7469. /// safety/correctness of things such as array bound and pointers et al.
  7470. /// The ABI only requires C (and XC) language modules to emit TypeStrings.
  7471. /// This type information (TypeString) is emitted into meta data for all global
  7472. /// symbols: definitions, declarations, functions & variables.
  7473. ///
  7474. /// The TypeString carries type, qualifier, name, size & value details.
  7475. /// Please see 'Tools Development Guide' section 2.16.2 for format details:
  7476. /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
  7477. /// The output is tested by test/CodeGen/xcore-stringtype.c.
  7478. ///
  7479. static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
  7480. CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
  7481. /// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
  7482. void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
  7483. CodeGen::CodeGenModule &CGM) const {
  7484. SmallStringEnc Enc;
  7485. if (getTypeString(Enc, D, CGM, TSC)) {
  7486. llvm::LLVMContext &Ctx = CGM.getModule().getContext();
  7487. llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
  7488. llvm::MDString::get(Ctx, Enc.str())};
  7489. llvm::NamedMDNode *MD =
  7490. CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
  7491. MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
  7492. }
  7493. }
  7494. //===----------------------------------------------------------------------===//
  7495. // SPIR ABI Implementation
  7496. //===----------------------------------------------------------------------===//
  7497. namespace {
  7498. class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
  7499. public:
  7500. SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  7501. : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
  7502. unsigned getOpenCLKernelCallingConv() const override;
  7503. };
  7504. } // End anonymous namespace.
  7505. namespace clang {
  7506. namespace CodeGen {
  7507. void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
  7508. DefaultABIInfo SPIRABI(CGM.getTypes());
  7509. SPIRABI.computeInfo(FI);
  7510. }
  7511. }
  7512. }
  7513. unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
  7514. return llvm::CallingConv::SPIR_KERNEL;
  7515. }
  7516. static bool appendType(SmallStringEnc &Enc, QualType QType,
  7517. const CodeGen::CodeGenModule &CGM,
  7518. TypeStringCache &TSC);
  7519. /// Helper function for appendRecordType().
  7520. /// Builds a SmallVector containing the encoded field types in declaration
  7521. /// order.
  7522. static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
  7523. const RecordDecl *RD,
  7524. const CodeGen::CodeGenModule &CGM,
  7525. TypeStringCache &TSC) {
  7526. for (const auto *Field : RD->fields()) {
  7527. SmallStringEnc Enc;
  7528. Enc += "m(";
  7529. Enc += Field->getName();
  7530. Enc += "){";
  7531. if (Field->isBitField()) {
  7532. Enc += "b(";
  7533. llvm::raw_svector_ostream OS(Enc);
  7534. OS << Field->getBitWidthValue(CGM.getContext());
  7535. Enc += ':';
  7536. }
  7537. if (!appendType(Enc, Field->getType(), CGM, TSC))
  7538. return false;
  7539. if (Field->isBitField())
  7540. Enc += ')';
  7541. Enc += '}';
  7542. FE.emplace_back(!Field->getName().empty(), Enc);
  7543. }
  7544. return true;
  7545. }
  7546. /// Appends structure and union types to Enc and adds encoding to cache.
  7547. /// Recursively calls appendType (via extractFieldType) for each field.
  7548. /// Union types have their fields ordered according to the ABI.
  7549. static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
  7550. const CodeGen::CodeGenModule &CGM,
  7551. TypeStringCache &TSC, const IdentifierInfo *ID) {
  7552. // Append the cached TypeString if we have one.
  7553. StringRef TypeString = TSC.lookupStr(ID);
  7554. if (!TypeString.empty()) {
  7555. Enc += TypeString;
  7556. return true;
  7557. }
  7558. // Start to emit an incomplete TypeString.
  7559. size_t Start = Enc.size();
  7560. Enc += (RT->isUnionType()? 'u' : 's');
  7561. Enc += '(';
  7562. if (ID)
  7563. Enc += ID->getName();
  7564. Enc += "){";
  7565. // We collect all encoded fields and order as necessary.
  7566. bool IsRecursive = false;
  7567. const RecordDecl *RD = RT->getDecl()->getDefinition();
  7568. if (RD && !RD->field_empty()) {
  7569. // An incomplete TypeString stub is placed in the cache for this RecordType
  7570. // so that recursive calls to this RecordType will use it whilst building a
  7571. // complete TypeString for this RecordType.
  7572. SmallVector<FieldEncoding, 16> FE;
  7573. std::string StubEnc(Enc.substr(Start).str());
  7574. StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString.
  7575. TSC.addIncomplete(ID, std::move(StubEnc));
  7576. if (!extractFieldType(FE, RD, CGM, TSC)) {
  7577. (void) TSC.removeIncomplete(ID);
  7578. return false;
  7579. }
  7580. IsRecursive = TSC.removeIncomplete(ID);
  7581. // The ABI requires unions to be sorted but not structures.
  7582. // See FieldEncoding::operator< for sort algorithm.
  7583. if (RT->isUnionType())
  7584. llvm::sort(FE);
  7585. // We can now complete the TypeString.
  7586. unsigned E = FE.size();
  7587. for (unsigned I = 0; I != E; ++I) {
  7588. if (I)
  7589. Enc += ',';
  7590. Enc += FE[I].str();
  7591. }
  7592. }
  7593. Enc += '}';
  7594. TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
  7595. return true;
  7596. }
  7597. /// Appends enum types to Enc and adds the encoding to the cache.
  7598. static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
  7599. TypeStringCache &TSC,
  7600. const IdentifierInfo *ID) {
  7601. // Append the cached TypeString if we have one.
  7602. StringRef TypeString = TSC.lookupStr(ID);
  7603. if (!TypeString.empty()) {
  7604. Enc += TypeString;
  7605. return true;
  7606. }
  7607. size_t Start = Enc.size();
  7608. Enc += "e(";
  7609. if (ID)
  7610. Enc += ID->getName();
  7611. Enc += "){";
  7612. // We collect all encoded enumerations and order them alphanumerically.
  7613. if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
  7614. SmallVector<FieldEncoding, 16> FE;
  7615. for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
  7616. ++I) {
  7617. SmallStringEnc EnumEnc;
  7618. EnumEnc += "m(";
  7619. EnumEnc += I->getName();
  7620. EnumEnc += "){";
  7621. I->getInitVal().toString(EnumEnc);
  7622. EnumEnc += '}';
  7623. FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
  7624. }
  7625. llvm::sort(FE);
  7626. unsigned E = FE.size();
  7627. for (unsigned I = 0; I != E; ++I) {
  7628. if (I)
  7629. Enc += ',';
  7630. Enc += FE[I].str();
  7631. }
  7632. }
  7633. Enc += '}';
  7634. TSC.addIfComplete(ID, Enc.substr(Start), false);
  7635. return true;
  7636. }
  7637. /// Appends type's qualifier to Enc.
  7638. /// This is done prior to appending the type's encoding.
  7639. static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
  7640. // Qualifiers are emitted in alphabetical order.
  7641. static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
  7642. int Lookup = 0;
  7643. if (QT.isConstQualified())
  7644. Lookup += 1<<0;
  7645. if (QT.isRestrictQualified())
  7646. Lookup += 1<<1;
  7647. if (QT.isVolatileQualified())
  7648. Lookup += 1<<2;
  7649. Enc += Table[Lookup];
  7650. }
  7651. /// Appends built-in types to Enc.
  7652. static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
  7653. const char *EncType;
  7654. switch (BT->getKind()) {
  7655. case BuiltinType::Void:
  7656. EncType = "0";
  7657. break;
  7658. case BuiltinType::Bool:
  7659. EncType = "b";
  7660. break;
  7661. case BuiltinType::Char_U:
  7662. EncType = "uc";
  7663. break;
  7664. case BuiltinType::UChar:
  7665. EncType = "uc";
  7666. break;
  7667. case BuiltinType::SChar:
  7668. EncType = "sc";
  7669. break;
  7670. case BuiltinType::UShort:
  7671. EncType = "us";
  7672. break;
  7673. case BuiltinType::Short:
  7674. EncType = "ss";
  7675. break;
  7676. case BuiltinType::UInt:
  7677. EncType = "ui";
  7678. break;
  7679. case BuiltinType::Int:
  7680. EncType = "si";
  7681. break;
  7682. case BuiltinType::ULong:
  7683. EncType = "ul";
  7684. break;
  7685. case BuiltinType::Long:
  7686. EncType = "sl";
  7687. break;
  7688. case BuiltinType::ULongLong:
  7689. EncType = "ull";
  7690. break;
  7691. case BuiltinType::LongLong:
  7692. EncType = "sll";
  7693. break;
  7694. case BuiltinType::Float:
  7695. EncType = "ft";
  7696. break;
  7697. case BuiltinType::Double:
  7698. EncType = "d";
  7699. break;
  7700. case BuiltinType::LongDouble:
  7701. EncType = "ld";
  7702. break;
  7703. default:
  7704. return false;
  7705. }
  7706. Enc += EncType;
  7707. return true;
  7708. }
  7709. /// Appends a pointer encoding to Enc before calling appendType for the pointee.
  7710. static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
  7711. const CodeGen::CodeGenModule &CGM,
  7712. TypeStringCache &TSC) {
  7713. Enc += "p(";
  7714. if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
  7715. return false;
  7716. Enc += ')';
  7717. return true;
  7718. }
  7719. /// Appends array encoding to Enc before calling appendType for the element.
  7720. static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
  7721. const ArrayType *AT,
  7722. const CodeGen::CodeGenModule &CGM,
  7723. TypeStringCache &TSC, StringRef NoSizeEnc) {
  7724. if (AT->getSizeModifier() != ArrayType::Normal)
  7725. return false;
  7726. Enc += "a(";
  7727. if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
  7728. CAT->getSize().toStringUnsigned(Enc);
  7729. else
  7730. Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
  7731. Enc += ':';
  7732. // The Qualifiers should be attached to the type rather than the array.
  7733. appendQualifier(Enc, QT);
  7734. if (!appendType(Enc, AT->getElementType(), CGM, TSC))
  7735. return false;
  7736. Enc += ')';
  7737. return true;
  7738. }
  7739. /// Appends a function encoding to Enc, calling appendType for the return type
  7740. /// and the arguments.
  7741. static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
  7742. const CodeGen::CodeGenModule &CGM,
  7743. TypeStringCache &TSC) {
  7744. Enc += "f{";
  7745. if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
  7746. return false;
  7747. Enc += "}(";
  7748. if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
  7749. // N.B. we are only interested in the adjusted param types.
  7750. auto I = FPT->param_type_begin();
  7751. auto E = FPT->param_type_end();
  7752. if (I != E) {
  7753. do {
  7754. if (!appendType(Enc, *I, CGM, TSC))
  7755. return false;
  7756. ++I;
  7757. if (I != E)
  7758. Enc += ',';
  7759. } while (I != E);
  7760. if (FPT->isVariadic())
  7761. Enc += ",va";
  7762. } else {
  7763. if (FPT->isVariadic())
  7764. Enc += "va";
  7765. else
  7766. Enc += '0';
  7767. }
  7768. }
  7769. Enc += ')';
  7770. return true;
  7771. }
  7772. /// Handles the type's qualifier before dispatching a call to handle specific
  7773. /// type encodings.
  7774. static bool appendType(SmallStringEnc &Enc, QualType QType,
  7775. const CodeGen::CodeGenModule &CGM,
  7776. TypeStringCache &TSC) {
  7777. QualType QT = QType.getCanonicalType();
  7778. if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
  7779. // The Qualifiers should be attached to the type rather than the array.
  7780. // Thus we don't call appendQualifier() here.
  7781. return appendArrayType(Enc, QT, AT, CGM, TSC, "");
  7782. appendQualifier(Enc, QT);
  7783. if (const BuiltinType *BT = QT->getAs<BuiltinType>())
  7784. return appendBuiltinType(Enc, BT);
  7785. if (const PointerType *PT = QT->getAs<PointerType>())
  7786. return appendPointerType(Enc, PT, CGM, TSC);
  7787. if (const EnumType *ET = QT->getAs<EnumType>())
  7788. return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
  7789. if (const RecordType *RT = QT->getAsStructureType())
  7790. return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
  7791. if (const RecordType *RT = QT->getAsUnionType())
  7792. return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
  7793. if (const FunctionType *FT = QT->getAs<FunctionType>())
  7794. return appendFunctionType(Enc, FT, CGM, TSC);
  7795. return false;
  7796. }
  7797. static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
  7798. CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
  7799. if (!D)
  7800. return false;
  7801. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  7802. if (FD->getLanguageLinkage() != CLanguageLinkage)
  7803. return false;
  7804. return appendType(Enc, FD->getType(), CGM, TSC);
  7805. }
  7806. if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
  7807. if (VD->getLanguageLinkage() != CLanguageLinkage)
  7808. return false;
  7809. QualType QT = VD->getType().getCanonicalType();
  7810. if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
  7811. // Global ArrayTypes are given a size of '*' if the size is unknown.
  7812. // The Qualifiers should be attached to the type rather than the array.
  7813. // Thus we don't call appendQualifier() here.
  7814. return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
  7815. }
  7816. return appendType(Enc, QT, CGM, TSC);
  7817. }
  7818. return false;
  7819. }
  7820. //===----------------------------------------------------------------------===//
  7821. // RISCV ABI Implementation
  7822. //===----------------------------------------------------------------------===//
  7823. namespace {
  7824. class RISCVABIInfo : public DefaultABIInfo {
  7825. private:
  7826. // Size of the integer ('x') registers in bits.
  7827. unsigned XLen;
  7828. // Size of the floating point ('f') registers in bits. Note that the target
  7829. // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target
  7830. // with soft float ABI has FLen==0).
  7831. unsigned FLen;
  7832. static const int NumArgGPRs = 8;
  7833. static const int NumArgFPRs = 8;
  7834. bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
  7835. llvm::Type *&Field1Ty,
  7836. CharUnits &Field1Off,
  7837. llvm::Type *&Field2Ty,
  7838. CharUnits &Field2Off) const;
  7839. public:
  7840. RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen)
  7841. : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {}
  7842. // DefaultABIInfo's classifyReturnType and classifyArgumentType are
  7843. // non-virtual, but computeInfo is virtual, so we overload it.
  7844. void computeInfo(CGFunctionInfo &FI) const override;
  7845. ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft,
  7846. int &ArgFPRsLeft) const;
  7847. ABIArgInfo classifyReturnType(QualType RetTy) const;
  7848. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  7849. QualType Ty) const override;
  7850. ABIArgInfo extendType(QualType Ty) const;
  7851. bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
  7852. CharUnits &Field1Off, llvm::Type *&Field2Ty,
  7853. CharUnits &Field2Off, int &NeededArgGPRs,
  7854. int &NeededArgFPRs) const;
  7855. ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty,
  7856. CharUnits Field1Off,
  7857. llvm::Type *Field2Ty,
  7858. CharUnits Field2Off) const;
  7859. };
  7860. } // end anonymous namespace
  7861. void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
  7862. QualType RetTy = FI.getReturnType();
  7863. if (!getCXXABI().classifyReturnType(FI))
  7864. FI.getReturnInfo() = classifyReturnType(RetTy);
  7865. // IsRetIndirect is true if classifyArgumentType indicated the value should
  7866. // be passed indirect or if the type size is greater than 2*xlen. e.g. fp128
  7867. // is passed direct in LLVM IR, relying on the backend lowering code to
  7868. // rewrite the argument list and pass indirectly on RV32.
  7869. bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect ||
  7870. getContext().getTypeSize(RetTy) > (2 * XLen);
  7871. // We must track the number of GPRs used in order to conform to the RISC-V
  7872. // ABI, as integer scalars passed in registers should have signext/zeroext
  7873. // when promoted, but are anyext if passed on the stack. As GPR usage is
  7874. // different for variadic arguments, we must also track whether we are
  7875. // examining a vararg or not.
  7876. int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
  7877. int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
  7878. int NumFixedArgs = FI.getNumRequiredArgs();
  7879. int ArgNum = 0;
  7880. for (auto &ArgInfo : FI.arguments()) {
  7881. bool IsFixed = ArgNum < NumFixedArgs;
  7882. ArgInfo.info =
  7883. classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft);
  7884. ArgNum++;
  7885. }
  7886. }
  7887. // Returns true if the struct is a potential candidate for the floating point
  7888. // calling convention. If this function returns true, the caller is
  7889. // responsible for checking that if there is only a single field then that
  7890. // field is a float.
  7891. bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
  7892. llvm::Type *&Field1Ty,
  7893. CharUnits &Field1Off,
  7894. llvm::Type *&Field2Ty,
  7895. CharUnits &Field2Off) const {
  7896. bool IsInt = Ty->isIntegralOrEnumerationType();
  7897. bool IsFloat = Ty->isRealFloatingType();
  7898. if (IsInt || IsFloat) {
  7899. uint64_t Size = getContext().getTypeSize(Ty);
  7900. if (IsInt && Size > XLen)
  7901. return false;
  7902. // Can't be eligible if larger than the FP registers. Half precision isn't
  7903. // currently supported on RISC-V and the ABI hasn't been confirmed, so
  7904. // default to the integer ABI in that case.
  7905. if (IsFloat && (Size > FLen || Size < 32))
  7906. return false;
  7907. // Can't be eligible if an integer type was already found (int+int pairs
  7908. // are not eligible).
  7909. if (IsInt && Field1Ty && Field1Ty->isIntegerTy())
  7910. return false;
  7911. if (!Field1Ty) {
  7912. Field1Ty = CGT.ConvertType(Ty);
  7913. Field1Off = CurOff;
  7914. return true;
  7915. }
  7916. if (!Field2Ty) {
  7917. Field2Ty = CGT.ConvertType(Ty);
  7918. Field2Off = CurOff;
  7919. return true;
  7920. }
  7921. return false;
  7922. }
  7923. if (auto CTy = Ty->getAs<ComplexType>()) {
  7924. if (Field1Ty)
  7925. return false;
  7926. QualType EltTy = CTy->getElementType();
  7927. if (getContext().getTypeSize(EltTy) > FLen)
  7928. return false;
  7929. Field1Ty = CGT.ConvertType(EltTy);
  7930. Field1Off = CurOff;
  7931. assert(CurOff.isZero() && "Unexpected offset for first field");
  7932. Field2Ty = Field1Ty;
  7933. Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy);
  7934. return true;
  7935. }
  7936. if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) {
  7937. uint64_t ArraySize = ATy->getSize().getZExtValue();
  7938. QualType EltTy = ATy->getElementType();
  7939. CharUnits EltSize = getContext().getTypeSizeInChars(EltTy);
  7940. for (uint64_t i = 0; i < ArraySize; ++i) {
  7941. bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty,
  7942. Field1Off, Field2Ty, Field2Off);
  7943. if (!Ret)
  7944. return false;
  7945. CurOff += EltSize;
  7946. }
  7947. return true;
  7948. }
  7949. if (const auto *RTy = Ty->getAs<RecordType>()) {
  7950. // Structures with either a non-trivial destructor or a non-trivial
  7951. // copy constructor are not eligible for the FP calling convention.
  7952. if (getRecordArgABI(Ty, CGT.getCXXABI()))
  7953. return false;
  7954. if (isEmptyRecord(getContext(), Ty, true))
  7955. return true;
  7956. const RecordDecl *RD = RTy->getDecl();
  7957. // Unions aren't eligible unless they're empty (which is caught above).
  7958. if (RD->isUnion())
  7959. return false;
  7960. int ZeroWidthBitFieldCount = 0;
  7961. for (const FieldDecl *FD : RD->fields()) {
  7962. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  7963. uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex());
  7964. QualType QTy = FD->getType();
  7965. if (FD->isBitField()) {
  7966. unsigned BitWidth = FD->getBitWidthValue(getContext());
  7967. // Allow a bitfield with a type greater than XLen as long as the
  7968. // bitwidth is XLen or less.
  7969. if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen)
  7970. QTy = getContext().getIntTypeForBitwidth(XLen, false);
  7971. if (BitWidth == 0) {
  7972. ZeroWidthBitFieldCount++;
  7973. continue;
  7974. }
  7975. }
  7976. bool Ret = detectFPCCEligibleStructHelper(
  7977. QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits),
  7978. Field1Ty, Field1Off, Field2Ty, Field2Off);
  7979. if (!Ret)
  7980. return false;
  7981. // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp
  7982. // or int+fp structs, but are ignored for a struct with an fp field and
  7983. // any number of zero-width bitfields.
  7984. if (Field2Ty && ZeroWidthBitFieldCount > 0)
  7985. return false;
  7986. }
  7987. return Field1Ty != nullptr;
  7988. }
  7989. return false;
  7990. }
  7991. // Determine if a struct is eligible for passing according to the floating
  7992. // point calling convention (i.e., when flattened it contains a single fp
  7993. // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and
  7994. // NeededArgGPRs are incremented appropriately.
  7995. bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
  7996. CharUnits &Field1Off,
  7997. llvm::Type *&Field2Ty,
  7998. CharUnits &Field2Off,
  7999. int &NeededArgGPRs,
  8000. int &NeededArgFPRs) const {
  8001. Field1Ty = nullptr;
  8002. Field2Ty = nullptr;
  8003. NeededArgGPRs = 0;
  8004. NeededArgFPRs = 0;
  8005. bool IsCandidate = detectFPCCEligibleStructHelper(
  8006. Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off);
  8007. // Not really a candidate if we have a single int but no float.
  8008. if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy())
  8009. return IsCandidate = false;
  8010. if (!IsCandidate)
  8011. return false;
  8012. if (Field1Ty && Field1Ty->isFloatingPointTy())
  8013. NeededArgFPRs++;
  8014. else if (Field1Ty)
  8015. NeededArgGPRs++;
  8016. if (Field2Ty && Field2Ty->isFloatingPointTy())
  8017. NeededArgFPRs++;
  8018. else if (Field2Ty)
  8019. NeededArgGPRs++;
  8020. return IsCandidate;
  8021. }
  8022. // Call getCoerceAndExpand for the two-element flattened struct described by
  8023. // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an
  8024. // appropriate coerceToType and unpaddedCoerceToType.
  8025. ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct(
  8026. llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty,
  8027. CharUnits Field2Off) const {
  8028. SmallVector<llvm::Type *, 3> CoerceElts;
  8029. SmallVector<llvm::Type *, 2> UnpaddedCoerceElts;
  8030. if (!Field1Off.isZero())
  8031. CoerceElts.push_back(llvm::ArrayType::get(
  8032. llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity()));
  8033. CoerceElts.push_back(Field1Ty);
  8034. UnpaddedCoerceElts.push_back(Field1Ty);
  8035. if (!Field2Ty) {
  8036. return ABIArgInfo::getCoerceAndExpand(
  8037. llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()),
  8038. UnpaddedCoerceElts[0]);
  8039. }
  8040. CharUnits Field2Align =
  8041. CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(Field2Ty));
  8042. CharUnits Field1Size =
  8043. CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty));
  8044. CharUnits Field2OffNoPadNoPack = Field1Size.alignTo(Field2Align);
  8045. CharUnits Padding = CharUnits::Zero();
  8046. if (Field2Off > Field2OffNoPadNoPack)
  8047. Padding = Field2Off - Field2OffNoPadNoPack;
  8048. else if (Field2Off != Field2Align && Field2Off > Field1Size)
  8049. Padding = Field2Off - Field1Size;
  8050. bool IsPacked = !Field2Off.isMultipleOf(Field2Align);
  8051. if (!Padding.isZero())
  8052. CoerceElts.push_back(llvm::ArrayType::get(
  8053. llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity()));
  8054. CoerceElts.push_back(Field2Ty);
  8055. UnpaddedCoerceElts.push_back(Field2Ty);
  8056. auto CoerceToType =
  8057. llvm::StructType::get(getVMContext(), CoerceElts, IsPacked);
  8058. auto UnpaddedCoerceToType =
  8059. llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked);
  8060. return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType);
  8061. }
  8062. ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
  8063. int &ArgGPRsLeft,
  8064. int &ArgFPRsLeft) const {
  8065. assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
  8066. Ty = useFirstFieldIfTransparentUnion(Ty);
  8067. // Structures with either a non-trivial destructor or a non-trivial
  8068. // copy constructor are always passed indirectly.
  8069. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  8070. if (ArgGPRsLeft)
  8071. ArgGPRsLeft -= 1;
  8072. return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
  8073. CGCXXABI::RAA_DirectInMemory);
  8074. }
  8075. // Ignore empty structs/unions.
  8076. if (isEmptyRecord(getContext(), Ty, true))
  8077. return ABIArgInfo::getIgnore();
  8078. uint64_t Size = getContext().getTypeSize(Ty);
  8079. // Pass floating point values via FPRs if possible.
  8080. if (IsFixed && Ty->isFloatingType() && FLen >= Size && ArgFPRsLeft) {
  8081. ArgFPRsLeft--;
  8082. return ABIArgInfo::getDirect();
  8083. }
  8084. // Complex types for the hard float ABI must be passed direct rather than
  8085. // using CoerceAndExpand.
  8086. if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) {
  8087. QualType EltTy = Ty->getAs<ComplexType>()->getElementType();
  8088. if (getContext().getTypeSize(EltTy) <= FLen) {
  8089. ArgFPRsLeft -= 2;
  8090. return ABIArgInfo::getDirect();
  8091. }
  8092. }
  8093. if (IsFixed && FLen && Ty->isStructureOrClassType()) {
  8094. llvm::Type *Field1Ty = nullptr;
  8095. llvm::Type *Field2Ty = nullptr;
  8096. CharUnits Field1Off = CharUnits::Zero();
  8097. CharUnits Field2Off = CharUnits::Zero();
  8098. int NeededArgGPRs;
  8099. int NeededArgFPRs;
  8100. bool IsCandidate =
  8101. detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off,
  8102. NeededArgGPRs, NeededArgFPRs);
  8103. if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft &&
  8104. NeededArgFPRs <= ArgFPRsLeft) {
  8105. ArgGPRsLeft -= NeededArgGPRs;
  8106. ArgFPRsLeft -= NeededArgFPRs;
  8107. return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty,
  8108. Field2Off);
  8109. }
  8110. }
  8111. uint64_t NeededAlign = getContext().getTypeAlign(Ty);
  8112. bool MustUseStack = false;
  8113. // Determine the number of GPRs needed to pass the current argument
  8114. // according to the ABI. 2*XLen-aligned varargs are passed in "aligned"
  8115. // register pairs, so may consume 3 registers.
  8116. int NeededArgGPRs = 1;
  8117. if (!IsFixed && NeededAlign == 2 * XLen)
  8118. NeededArgGPRs = 2 + (ArgGPRsLeft % 2);
  8119. else if (Size > XLen && Size <= 2 * XLen)
  8120. NeededArgGPRs = 2;
  8121. if (NeededArgGPRs > ArgGPRsLeft) {
  8122. MustUseStack = true;
  8123. NeededArgGPRs = ArgGPRsLeft;
  8124. }
  8125. ArgGPRsLeft -= NeededArgGPRs;
  8126. if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
  8127. // Treat an enum type as its underlying type.
  8128. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  8129. Ty = EnumTy->getDecl()->getIntegerType();
  8130. // All integral types are promoted to XLen width, unless passed on the
  8131. // stack.
  8132. if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) {
  8133. return extendType(Ty);
  8134. }
  8135. return ABIArgInfo::getDirect();
  8136. }
  8137. // Aggregates which are <= 2*XLen will be passed in registers if possible,
  8138. // so coerce to integers.
  8139. if (Size <= 2 * XLen) {
  8140. unsigned Alignment = getContext().getTypeAlign(Ty);
  8141. // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is
  8142. // required, and a 2-element XLen array if only XLen alignment is required.
  8143. if (Size <= XLen) {
  8144. return ABIArgInfo::getDirect(
  8145. llvm::IntegerType::get(getVMContext(), XLen));
  8146. } else if (Alignment == 2 * XLen) {
  8147. return ABIArgInfo::getDirect(
  8148. llvm::IntegerType::get(getVMContext(), 2 * XLen));
  8149. } else {
  8150. return ABIArgInfo::getDirect(llvm::ArrayType::get(
  8151. llvm::IntegerType::get(getVMContext(), XLen), 2));
  8152. }
  8153. }
  8154. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  8155. }
  8156. ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const {
  8157. if (RetTy->isVoidType())
  8158. return ABIArgInfo::getIgnore();
  8159. int ArgGPRsLeft = 2;
  8160. int ArgFPRsLeft = FLen ? 2 : 0;
  8161. // The rules for return and argument types are the same, so defer to
  8162. // classifyArgumentType.
  8163. return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft,
  8164. ArgFPRsLeft);
  8165. }
  8166. Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  8167. QualType Ty) const {
  8168. CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
  8169. // Empty records are ignored for parameter passing purposes.
  8170. if (isEmptyRecord(getContext(), Ty, true)) {
  8171. Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
  8172. Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
  8173. return Addr;
  8174. }
  8175. std::pair<CharUnits, CharUnits> SizeAndAlign =
  8176. getContext().getTypeInfoInChars(Ty);
  8177. // Arguments bigger than 2*Xlen bytes are passed indirectly.
  8178. bool IsIndirect = SizeAndAlign.first > 2 * SlotSize;
  8179. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign,
  8180. SlotSize, /*AllowHigherAlign=*/true);
  8181. }
  8182. ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const {
  8183. int TySize = getContext().getTypeSize(Ty);
  8184. // RV64 ABI requires unsigned 32 bit integers to be sign extended.
  8185. if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
  8186. return ABIArgInfo::getSignExtend(Ty);
  8187. return ABIArgInfo::getExtend(Ty);
  8188. }
  8189. namespace {
  8190. class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
  8191. public:
  8192. RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen,
  8193. unsigned FLen)
  8194. : TargetCodeGenInfo(new RISCVABIInfo(CGT, XLen, FLen)) {}
  8195. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  8196. CodeGen::CodeGenModule &CGM) const override {
  8197. const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
  8198. if (!FD) return;
  8199. const auto *Attr = FD->getAttr<RISCVInterruptAttr>();
  8200. if (!Attr)
  8201. return;
  8202. const char *Kind;
  8203. switch (Attr->getInterrupt()) {
  8204. case RISCVInterruptAttr::user: Kind = "user"; break;
  8205. case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break;
  8206. case RISCVInterruptAttr::machine: Kind = "machine"; break;
  8207. }
  8208. auto *Fn = cast<llvm::Function>(GV);
  8209. Fn->addFnAttr("interrupt", Kind);
  8210. }
  8211. };
  8212. } // namespace
  8213. //===----------------------------------------------------------------------===//
  8214. // Driver code
  8215. //===----------------------------------------------------------------------===//
  8216. bool CodeGenModule::supportsCOMDAT() const {
  8217. return getTriple().supportsCOMDAT();
  8218. }
  8219. const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
  8220. if (TheTargetCodeGenInfo)
  8221. return *TheTargetCodeGenInfo;
  8222. // Helper to set the unique_ptr while still keeping the return value.
  8223. auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
  8224. this->TheTargetCodeGenInfo.reset(P);
  8225. return *P;
  8226. };
  8227. const llvm::Triple &Triple = getTarget().getTriple();
  8228. switch (Triple.getArch()) {
  8229. default:
  8230. return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
  8231. case llvm::Triple::le32:
  8232. return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
  8233. case llvm::Triple::mips:
  8234. case llvm::Triple::mipsel:
  8235. if (Triple.getOS() == llvm::Triple::NaCl)
  8236. return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
  8237. return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
  8238. case llvm::Triple::mips64:
  8239. case llvm::Triple::mips64el:
  8240. return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
  8241. case llvm::Triple::avr:
  8242. return SetCGInfo(new AVRTargetCodeGenInfo(Types));
  8243. case llvm::Triple::aarch64:
  8244. case llvm::Triple::aarch64_be: {
  8245. AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
  8246. if (getTarget().getABI() == "darwinpcs")
  8247. Kind = AArch64ABIInfo::DarwinPCS;
  8248. else if (Triple.isOSWindows())
  8249. return SetCGInfo(
  8250. new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
  8251. return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
  8252. }
  8253. case llvm::Triple::wasm32:
  8254. case llvm::Triple::wasm64:
  8255. return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types));
  8256. case llvm::Triple::arm:
  8257. case llvm::Triple::armeb:
  8258. case llvm::Triple::thumb:
  8259. case llvm::Triple::thumbeb: {
  8260. if (Triple.getOS() == llvm::Triple::Win32) {
  8261. return SetCGInfo(
  8262. new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
  8263. }
  8264. ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
  8265. StringRef ABIStr = getTarget().getABI();
  8266. if (ABIStr == "apcs-gnu")
  8267. Kind = ARMABIInfo::APCS;
  8268. else if (ABIStr == "aapcs16")
  8269. Kind = ARMABIInfo::AAPCS16_VFP;
  8270. else if (CodeGenOpts.FloatABI == "hard" ||
  8271. (CodeGenOpts.FloatABI != "soft" &&
  8272. (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
  8273. Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
  8274. Triple.getEnvironment() == llvm::Triple::EABIHF)))
  8275. Kind = ARMABIInfo::AAPCS_VFP;
  8276. return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
  8277. }
  8278. case llvm::Triple::ppc:
  8279. return SetCGInfo(
  8280. new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft" ||
  8281. getTarget().hasFeature("spe")));
  8282. case llvm::Triple::ppc64:
  8283. if (Triple.isOSBinFormatELF()) {
  8284. PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
  8285. if (getTarget().getABI() == "elfv2")
  8286. Kind = PPC64_SVR4_ABIInfo::ELFv2;
  8287. bool HasQPX = getTarget().getABI() == "elfv1-qpx";
  8288. bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
  8289. return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
  8290. IsSoftFloat));
  8291. } else
  8292. return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
  8293. case llvm::Triple::ppc64le: {
  8294. assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
  8295. PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
  8296. if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
  8297. Kind = PPC64_SVR4_ABIInfo::ELFv1;
  8298. bool HasQPX = getTarget().getABI() == "elfv1-qpx";
  8299. bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
  8300. return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
  8301. IsSoftFloat));
  8302. }
  8303. case llvm::Triple::nvptx:
  8304. case llvm::Triple::nvptx64:
  8305. return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
  8306. case llvm::Triple::msp430:
  8307. return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
  8308. case llvm::Triple::riscv32:
  8309. case llvm::Triple::riscv64: {
  8310. StringRef ABIStr = getTarget().getABI();
  8311. unsigned XLen = getTarget().getPointerWidth(0);
  8312. unsigned ABIFLen = 0;
  8313. if (ABIStr.endswith("f"))
  8314. ABIFLen = 32;
  8315. else if (ABIStr.endswith("d"))
  8316. ABIFLen = 64;
  8317. return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen));
  8318. }
  8319. case llvm::Triple::systemz: {
  8320. bool HasVector = getTarget().getABI() == "vector";
  8321. return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector));
  8322. }
  8323. case llvm::Triple::tce:
  8324. case llvm::Triple::tcele:
  8325. return SetCGInfo(new TCETargetCodeGenInfo(Types));
  8326. case llvm::Triple::x86: {
  8327. bool IsDarwinVectorABI = Triple.isOSDarwin();
  8328. bool RetSmallStructInRegABI =
  8329. X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
  8330. bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
  8331. if (Triple.getOS() == llvm::Triple::Win32) {
  8332. return SetCGInfo(new WinX86_32TargetCodeGenInfo(
  8333. Types, IsDarwinVectorABI, RetSmallStructInRegABI,
  8334. IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
  8335. } else {
  8336. return SetCGInfo(new X86_32TargetCodeGenInfo(
  8337. Types, IsDarwinVectorABI, RetSmallStructInRegABI,
  8338. IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
  8339. CodeGenOpts.FloatABI == "soft"));
  8340. }
  8341. }
  8342. case llvm::Triple::x86_64: {
  8343. StringRef ABI = getTarget().getABI();
  8344. X86AVXABILevel AVXLevel =
  8345. (ABI == "avx512"
  8346. ? X86AVXABILevel::AVX512
  8347. : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
  8348. switch (Triple.getOS()) {
  8349. case llvm::Triple::Win32:
  8350. return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
  8351. default:
  8352. return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
  8353. }
  8354. }
  8355. case llvm::Triple::hexagon:
  8356. return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
  8357. case llvm::Triple::lanai:
  8358. return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
  8359. case llvm::Triple::r600:
  8360. return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
  8361. case llvm::Triple::amdgcn:
  8362. return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
  8363. case llvm::Triple::sparc:
  8364. return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
  8365. case llvm::Triple::sparcv9:
  8366. return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
  8367. case llvm::Triple::xcore:
  8368. return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
  8369. case llvm::Triple::arc:
  8370. return SetCGInfo(new ARCTargetCodeGenInfo(Types));
  8371. case llvm::Triple::spir:
  8372. case llvm::Triple::spir64:
  8373. return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
  8374. }
  8375. }
  8376. /// Create an OpenCL kernel for an enqueued block.
  8377. ///
  8378. /// The kernel has the same function type as the block invoke function. Its
  8379. /// name is the name of the block invoke function postfixed with "_kernel".
  8380. /// It simply calls the block invoke function then returns.
  8381. llvm::Function *
  8382. TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
  8383. llvm::Function *Invoke,
  8384. llvm::Value *BlockLiteral) const {
  8385. auto *InvokeFT = Invoke->getFunctionType();
  8386. llvm::SmallVector<llvm::Type *, 2> ArgTys;
  8387. for (auto &P : InvokeFT->params())
  8388. ArgTys.push_back(P);
  8389. auto &C = CGF.getLLVMContext();
  8390. std::string Name = Invoke->getName().str() + "_kernel";
  8391. auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
  8392. auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
  8393. &CGF.CGM.getModule());
  8394. auto IP = CGF.Builder.saveIP();
  8395. auto *BB = llvm::BasicBlock::Create(C, "entry", F);
  8396. auto &Builder = CGF.Builder;
  8397. Builder.SetInsertPoint(BB);
  8398. llvm::SmallVector<llvm::Value *, 2> Args;
  8399. for (auto &A : F->args())
  8400. Args.push_back(&A);
  8401. Builder.CreateCall(Invoke, Args);
  8402. Builder.CreateRetVoid();
  8403. Builder.restoreIP(IP);
  8404. return F;
  8405. }
  8406. /// Create an OpenCL kernel for an enqueued block.
  8407. ///
  8408. /// The type of the first argument (the block literal) is the struct type
  8409. /// of the block literal instead of a pointer type. The first argument
  8410. /// (block literal) is passed directly by value to the kernel. The kernel
  8411. /// allocates the same type of struct on stack and stores the block literal
  8412. /// to it and passes its pointer to the block invoke function. The kernel
  8413. /// has "enqueued-block" function attribute and kernel argument metadata.
  8414. llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
  8415. CodeGenFunction &CGF, llvm::Function *Invoke,
  8416. llvm::Value *BlockLiteral) const {
  8417. auto &Builder = CGF.Builder;
  8418. auto &C = CGF.getLLVMContext();
  8419. auto *BlockTy = BlockLiteral->getType()->getPointerElementType();
  8420. auto *InvokeFT = Invoke->getFunctionType();
  8421. llvm::SmallVector<llvm::Type *, 2> ArgTys;
  8422. llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
  8423. llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
  8424. llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
  8425. llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
  8426. llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
  8427. llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
  8428. ArgTys.push_back(BlockTy);
  8429. ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
  8430. AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
  8431. ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
  8432. ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
  8433. AccessQuals.push_back(llvm::MDString::get(C, "none"));
  8434. ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
  8435. for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
  8436. ArgTys.push_back(InvokeFT->getParamType(I));
  8437. ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
  8438. AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
  8439. AccessQuals.push_back(llvm::MDString::get(C, "none"));
  8440. ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
  8441. ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
  8442. ArgNames.push_back(
  8443. llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
  8444. }
  8445. std::string Name = Invoke->getName().str() + "_kernel";
  8446. auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
  8447. auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
  8448. &CGF.CGM.getModule());
  8449. F->addFnAttr("enqueued-block");
  8450. auto IP = CGF.Builder.saveIP();
  8451. auto *BB = llvm::BasicBlock::Create(C, "entry", F);
  8452. Builder.SetInsertPoint(BB);
  8453. unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy);
  8454. auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
  8455. BlockPtr->setAlignment(llvm::MaybeAlign(BlockAlign));
  8456. Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
  8457. auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
  8458. llvm::SmallVector<llvm::Value *, 2> Args;
  8459. Args.push_back(Cast);
  8460. for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I)
  8461. Args.push_back(I);
  8462. Builder.CreateCall(Invoke, Args);
  8463. Builder.CreateRetVoid();
  8464. Builder.restoreIP(IP);
  8465. F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
  8466. F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
  8467. F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
  8468. F->setMetadata("kernel_arg_base_type",
  8469. llvm::MDNode::get(C, ArgBaseTypeNames));
  8470. F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
  8471. if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
  8472. F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
  8473. return F;
  8474. }