TargetInfo.cpp 326 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063
  1. //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // These classes wrap the information about a call or function
  11. // definition used to handle ABI compliancy.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "TargetInfo.h"
  15. #include "ABIInfo.h"
  16. #include "CGBlocks.h"
  17. #include "CGCXXABI.h"
  18. #include "CGValue.h"
  19. #include "CodeGenFunction.h"
  20. #include "clang/AST/RecordLayout.h"
  21. #include "clang/CodeGen/CGFunctionInfo.h"
  22. #include "clang/CodeGen/SwiftCallingConv.h"
  23. #include "clang/Frontend/CodeGenOptions.h"
  24. #include "llvm/ADT/StringExtras.h"
  25. #include "llvm/ADT/StringSwitch.h"
  26. #include "llvm/ADT/Triple.h"
  27. #include "llvm/ADT/Twine.h"
  28. #include "llvm/IR/DataLayout.h"
  29. #include "llvm/IR/Type.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include <algorithm> // std::sort
  32. using namespace clang;
  33. using namespace CodeGen;
  34. // Helper for coercing an aggregate argument or return value into an integer
  35. // array of the same size (including padding) and alignment. This alternate
  36. // coercion happens only for the RenderScript ABI and can be removed after
  37. // runtimes that rely on it are no longer supported.
  38. //
  39. // RenderScript assumes that the size of the argument / return value in the IR
  40. // is the same as the size of the corresponding qualified type. This helper
  41. // coerces the aggregate type into an array of the same size (including
  42. // padding). This coercion is used in lieu of expansion of struct members or
  43. // other canonical coercions that return a coerced-type of larger size.
  44. //
  45. // Ty - The argument / return value type
  46. // Context - The associated ASTContext
  47. // LLVMContext - The associated LLVMContext
  48. static ABIArgInfo coerceToIntArray(QualType Ty,
  49. ASTContext &Context,
  50. llvm::LLVMContext &LLVMContext) {
  51. // Alignment and Size are measured in bits.
  52. const uint64_t Size = Context.getTypeSize(Ty);
  53. const uint64_t Alignment = Context.getTypeAlign(Ty);
  54. llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
  55. const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
  56. return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
  57. }
  58. static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
  59. llvm::Value *Array,
  60. llvm::Value *Value,
  61. unsigned FirstIndex,
  62. unsigned LastIndex) {
  63. // Alternatively, we could emit this as a loop in the source.
  64. for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
  65. llvm::Value *Cell =
  66. Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
  67. Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
  68. }
  69. }
  70. static bool isAggregateTypeForABI(QualType T) {
  71. return !CodeGenFunction::hasScalarEvaluationKind(T) ||
  72. T->isMemberFunctionPointerType();
  73. }
  74. ABIArgInfo
  75. ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign,
  76. llvm::Type *Padding) const {
  77. return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
  78. ByRef, Realign, Padding);
  79. }
  80. ABIArgInfo
  81. ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
  82. return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
  83. /*ByRef*/ false, Realign);
  84. }
  85. Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  86. QualType Ty) const {
  87. return Address::invalid();
  88. }
  89. ABIInfo::~ABIInfo() {}
  90. /// Does the given lowering require more than the given number of
  91. /// registers when expanded?
  92. ///
  93. /// This is intended to be the basis of a reasonable basic implementation
  94. /// of should{Pass,Return}IndirectlyForSwift.
  95. ///
  96. /// For most targets, a limit of four total registers is reasonable; this
  97. /// limits the amount of code required in order to move around the value
  98. /// in case it wasn't produced immediately prior to the call by the caller
  99. /// (or wasn't produced in exactly the right registers) or isn't used
  100. /// immediately within the callee. But some targets may need to further
  101. /// limit the register count due to an inability to support that many
  102. /// return registers.
  103. static bool occupiesMoreThan(CodeGenTypes &cgt,
  104. ArrayRef<llvm::Type*> scalarTypes,
  105. unsigned maxAllRegisters) {
  106. unsigned intCount = 0, fpCount = 0;
  107. for (llvm::Type *type : scalarTypes) {
  108. if (type->isPointerTy()) {
  109. intCount++;
  110. } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
  111. auto ptrWidth = cgt.getTarget().getPointerWidth(0);
  112. intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
  113. } else {
  114. assert(type->isVectorTy() || type->isFloatingPointTy());
  115. fpCount++;
  116. }
  117. }
  118. return (intCount + fpCount > maxAllRegisters);
  119. }
  120. bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
  121. llvm::Type *eltTy,
  122. unsigned numElts) const {
  123. // The default implementation of this assumes that the target guarantees
  124. // 128-bit SIMD support but nothing more.
  125. return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16);
  126. }
  127. static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
  128. CGCXXABI &CXXABI) {
  129. const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
  130. if (!RD)
  131. return CGCXXABI::RAA_Default;
  132. return CXXABI.getRecordArgABI(RD);
  133. }
  134. static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
  135. CGCXXABI &CXXABI) {
  136. const RecordType *RT = T->getAs<RecordType>();
  137. if (!RT)
  138. return CGCXXABI::RAA_Default;
  139. return getRecordArgABI(RT, CXXABI);
  140. }
  141. /// Pass transparent unions as if they were the type of the first element. Sema
  142. /// should ensure that all elements of the union have the same "machine type".
  143. static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
  144. if (const RecordType *UT = Ty->getAsUnionType()) {
  145. const RecordDecl *UD = UT->getDecl();
  146. if (UD->hasAttr<TransparentUnionAttr>()) {
  147. assert(!UD->field_empty() && "sema created an empty transparent union");
  148. return UD->field_begin()->getType();
  149. }
  150. }
  151. return Ty;
  152. }
  153. CGCXXABI &ABIInfo::getCXXABI() const {
  154. return CGT.getCXXABI();
  155. }
  156. ASTContext &ABIInfo::getContext() const {
  157. return CGT.getContext();
  158. }
  159. llvm::LLVMContext &ABIInfo::getVMContext() const {
  160. return CGT.getLLVMContext();
  161. }
  162. const llvm::DataLayout &ABIInfo::getDataLayout() const {
  163. return CGT.getDataLayout();
  164. }
  165. const TargetInfo &ABIInfo::getTarget() const {
  166. return CGT.getTarget();
  167. }
  168. const CodeGenOptions &ABIInfo::getCodeGenOpts() const {
  169. return CGT.getCodeGenOpts();
  170. }
  171. bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); }
  172. bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  173. return false;
  174. }
  175. bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
  176. uint64_t Members) const {
  177. return false;
  178. }
  179. bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
  180. return false;
  181. }
  182. LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
  183. raw_ostream &OS = llvm::errs();
  184. OS << "(ABIArgInfo Kind=";
  185. switch (TheKind) {
  186. case Direct:
  187. OS << "Direct Type=";
  188. if (llvm::Type *Ty = getCoerceToType())
  189. Ty->print(OS);
  190. else
  191. OS << "null";
  192. break;
  193. case Extend:
  194. OS << "Extend";
  195. break;
  196. case Ignore:
  197. OS << "Ignore";
  198. break;
  199. case InAlloca:
  200. OS << "InAlloca Offset=" << getInAllocaFieldIndex();
  201. break;
  202. case Indirect:
  203. OS << "Indirect Align=" << getIndirectAlign().getQuantity()
  204. << " ByVal=" << getIndirectByVal()
  205. << " Realign=" << getIndirectRealign();
  206. break;
  207. case Expand:
  208. OS << "Expand";
  209. break;
  210. case CoerceAndExpand:
  211. OS << "CoerceAndExpand Type=";
  212. getCoerceAndExpandType()->print(OS);
  213. break;
  214. }
  215. OS << ")\n";
  216. }
  217. // Dynamically round a pointer up to a multiple of the given alignment.
  218. static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
  219. llvm::Value *Ptr,
  220. CharUnits Align) {
  221. llvm::Value *PtrAsInt = Ptr;
  222. // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
  223. PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
  224. PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
  225. llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
  226. PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
  227. llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
  228. PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
  229. Ptr->getType(),
  230. Ptr->getName() + ".aligned");
  231. return PtrAsInt;
  232. }
  233. /// Emit va_arg for a platform using the common void* representation,
  234. /// where arguments are simply emitted in an array of slots on the stack.
  235. ///
  236. /// This version implements the core direct-value passing rules.
  237. ///
  238. /// \param SlotSize - The size and alignment of a stack slot.
  239. /// Each argument will be allocated to a multiple of this number of
  240. /// slots, and all the slots will be aligned to this value.
  241. /// \param AllowHigherAlign - The slot alignment is not a cap;
  242. /// an argument type with an alignment greater than the slot size
  243. /// will be emitted on a higher-alignment address, potentially
  244. /// leaving one or more empty slots behind as padding. If this
  245. /// is false, the returned address might be less-aligned than
  246. /// DirectAlign.
  247. static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
  248. Address VAListAddr,
  249. llvm::Type *DirectTy,
  250. CharUnits DirectSize,
  251. CharUnits DirectAlign,
  252. CharUnits SlotSize,
  253. bool AllowHigherAlign) {
  254. // Cast the element type to i8* if necessary. Some platforms define
  255. // va_list as a struct containing an i8* instead of just an i8*.
  256. if (VAListAddr.getElementType() != CGF.Int8PtrTy)
  257. VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
  258. llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
  259. // If the CC aligns values higher than the slot size, do so if needed.
  260. Address Addr = Address::invalid();
  261. if (AllowHigherAlign && DirectAlign > SlotSize) {
  262. Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
  263. DirectAlign);
  264. } else {
  265. Addr = Address(Ptr, SlotSize);
  266. }
  267. // Advance the pointer past the argument, then store that back.
  268. CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
  269. llvm::Value *NextPtr =
  270. CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize,
  271. "argp.next");
  272. CGF.Builder.CreateStore(NextPtr, VAListAddr);
  273. // If the argument is smaller than a slot, and this is a big-endian
  274. // target, the argument will be right-adjusted in its slot.
  275. if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
  276. !DirectTy->isStructTy()) {
  277. Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
  278. }
  279. Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
  280. return Addr;
  281. }
  282. /// Emit va_arg for a platform using the common void* representation,
  283. /// where arguments are simply emitted in an array of slots on the stack.
  284. ///
  285. /// \param IsIndirect - Values of this type are passed indirectly.
  286. /// \param ValueInfo - The size and alignment of this type, generally
  287. /// computed with getContext().getTypeInfoInChars(ValueTy).
  288. /// \param SlotSizeAndAlign - The size and alignment of a stack slot.
  289. /// Each argument will be allocated to a multiple of this number of
  290. /// slots, and all the slots will be aligned to this value.
  291. /// \param AllowHigherAlign - The slot alignment is not a cap;
  292. /// an argument type with an alignment greater than the slot size
  293. /// will be emitted on a higher-alignment address, potentially
  294. /// leaving one or more empty slots behind as padding.
  295. static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
  296. QualType ValueTy, bool IsIndirect,
  297. std::pair<CharUnits, CharUnits> ValueInfo,
  298. CharUnits SlotSizeAndAlign,
  299. bool AllowHigherAlign) {
  300. // The size and alignment of the value that was passed directly.
  301. CharUnits DirectSize, DirectAlign;
  302. if (IsIndirect) {
  303. DirectSize = CGF.getPointerSize();
  304. DirectAlign = CGF.getPointerAlign();
  305. } else {
  306. DirectSize = ValueInfo.first;
  307. DirectAlign = ValueInfo.second;
  308. }
  309. // Cast the address we've calculated to the right type.
  310. llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy);
  311. if (IsIndirect)
  312. DirectTy = DirectTy->getPointerTo(0);
  313. Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy,
  314. DirectSize, DirectAlign,
  315. SlotSizeAndAlign,
  316. AllowHigherAlign);
  317. if (IsIndirect) {
  318. Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second);
  319. }
  320. return Addr;
  321. }
  322. static Address emitMergePHI(CodeGenFunction &CGF,
  323. Address Addr1, llvm::BasicBlock *Block1,
  324. Address Addr2, llvm::BasicBlock *Block2,
  325. const llvm::Twine &Name = "") {
  326. assert(Addr1.getType() == Addr2.getType());
  327. llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
  328. PHI->addIncoming(Addr1.getPointer(), Block1);
  329. PHI->addIncoming(Addr2.getPointer(), Block2);
  330. CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
  331. return Address(PHI, Align);
  332. }
  333. TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
  334. // If someone can figure out a general rule for this, that would be great.
  335. // It's probably just doomed to be platform-dependent, though.
  336. unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
  337. // Verified for:
  338. // x86-64 FreeBSD, Linux, Darwin
  339. // x86-32 FreeBSD, Linux, Darwin
  340. // PowerPC Linux, Darwin
  341. // ARM Darwin (*not* EABI)
  342. // AArch64 Linux
  343. return 32;
  344. }
  345. bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
  346. const FunctionNoProtoType *fnType) const {
  347. // The following conventions are known to require this to be false:
  348. // x86_stdcall
  349. // MIPS
  350. // For everything else, we just prefer false unless we opt out.
  351. return false;
  352. }
  353. void
  354. TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
  355. llvm::SmallString<24> &Opt) const {
  356. // This assumes the user is passing a library name like "rt" instead of a
  357. // filename like "librt.a/so", and that they don't care whether it's static or
  358. // dynamic.
  359. Opt = "-l";
  360. Opt += Lib;
  361. }
  362. unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
  363. // OpenCL kernels are called via an explicit runtime API with arguments
  364. // set with clSetKernelArg(), not as normal sub-functions.
  365. // Return SPIR_KERNEL by default as the kernel calling convention to
  366. // ensure the fingerprint is fixed such way that each OpenCL argument
  367. // gets one matching argument in the produced kernel function argument
  368. // list to enable feasible implementation of clSetKernelArg() with
  369. // aggregates etc. In case we would use the default C calling conv here,
  370. // clSetKernelArg() might break depending on the target-specific
  371. // conventions; different targets might split structs passed as values
  372. // to multiple function arguments etc.
  373. return llvm::CallingConv::SPIR_KERNEL;
  374. }
  375. llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
  376. llvm::PointerType *T, QualType QT) const {
  377. return llvm::ConstantPointerNull::get(T);
  378. }
  379. LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
  380. const VarDecl *D) const {
  381. assert(!CGM.getLangOpts().OpenCL &&
  382. !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
  383. "Address space agnostic languages only");
  384. return D ? D->getType().getAddressSpace() : LangAS::Default;
  385. }
  386. llvm::Value *TargetCodeGenInfo::performAddrSpaceCast(
  387. CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr,
  388. LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const {
  389. // Since target may map different address spaces in AST to the same address
  390. // space, an address space conversion may end up as a bitcast.
  391. if (auto *C = dyn_cast<llvm::Constant>(Src))
  392. return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy);
  393. return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DestTy);
  394. }
  395. llvm::Constant *
  396. TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src,
  397. LangAS SrcAddr, LangAS DestAddr,
  398. llvm::Type *DestTy) const {
  399. // Since target may map different address spaces in AST to the same address
  400. // space, an address space conversion may end up as a bitcast.
  401. return llvm::ConstantExpr::getPointerCast(Src, DestTy);
  402. }
  403. llvm::SyncScope::ID
  404. TargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, llvm::LLVMContext &C) const {
  405. return C.getOrInsertSyncScopeID(""); /* default sync scope */
  406. }
  407. static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
  408. /// isEmptyField - Return true iff a the field is "empty", that is it
  409. /// is an unnamed bit-field or an (array of) empty record(s).
  410. static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
  411. bool AllowArrays) {
  412. if (FD->isUnnamedBitfield())
  413. return true;
  414. QualType FT = FD->getType();
  415. // Constant arrays of empty records count as empty, strip them off.
  416. // Constant arrays of zero length always count as empty.
  417. if (AllowArrays)
  418. while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
  419. if (AT->getSize() == 0)
  420. return true;
  421. FT = AT->getElementType();
  422. }
  423. const RecordType *RT = FT->getAs<RecordType>();
  424. if (!RT)
  425. return false;
  426. // C++ record fields are never empty, at least in the Itanium ABI.
  427. //
  428. // FIXME: We should use a predicate for whether this behavior is true in the
  429. // current ABI.
  430. if (isa<CXXRecordDecl>(RT->getDecl()))
  431. return false;
  432. return isEmptyRecord(Context, FT, AllowArrays);
  433. }
  434. /// isEmptyRecord - Return true iff a structure contains only empty
  435. /// fields. Note that a structure with a flexible array member is not
  436. /// considered empty.
  437. static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
  438. const RecordType *RT = T->getAs<RecordType>();
  439. if (!RT)
  440. return false;
  441. const RecordDecl *RD = RT->getDecl();
  442. if (RD->hasFlexibleArrayMember())
  443. return false;
  444. // If this is a C++ record, check the bases first.
  445. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  446. for (const auto &I : CXXRD->bases())
  447. if (!isEmptyRecord(Context, I.getType(), true))
  448. return false;
  449. for (const auto *I : RD->fields())
  450. if (!isEmptyField(Context, I, AllowArrays))
  451. return false;
  452. return true;
  453. }
  454. /// isSingleElementStruct - Determine if a structure is a "single
  455. /// element struct", i.e. it has exactly one non-empty field or
  456. /// exactly one field which is itself a single element
  457. /// struct. Structures with flexible array members are never
  458. /// considered single element structs.
  459. ///
  460. /// \return The field declaration for the single non-empty field, if
  461. /// it exists.
  462. static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
  463. const RecordType *RT = T->getAs<RecordType>();
  464. if (!RT)
  465. return nullptr;
  466. const RecordDecl *RD = RT->getDecl();
  467. if (RD->hasFlexibleArrayMember())
  468. return nullptr;
  469. const Type *Found = nullptr;
  470. // If this is a C++ record, check the bases first.
  471. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  472. for (const auto &I : CXXRD->bases()) {
  473. // Ignore empty records.
  474. if (isEmptyRecord(Context, I.getType(), true))
  475. continue;
  476. // If we already found an element then this isn't a single-element struct.
  477. if (Found)
  478. return nullptr;
  479. // If this is non-empty and not a single element struct, the composite
  480. // cannot be a single element struct.
  481. Found = isSingleElementStruct(I.getType(), Context);
  482. if (!Found)
  483. return nullptr;
  484. }
  485. }
  486. // Check for single element.
  487. for (const auto *FD : RD->fields()) {
  488. QualType FT = FD->getType();
  489. // Ignore empty fields.
  490. if (isEmptyField(Context, FD, true))
  491. continue;
  492. // If we already found an element then this isn't a single-element
  493. // struct.
  494. if (Found)
  495. return nullptr;
  496. // Treat single element arrays as the element.
  497. while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
  498. if (AT->getSize().getZExtValue() != 1)
  499. break;
  500. FT = AT->getElementType();
  501. }
  502. if (!isAggregateTypeForABI(FT)) {
  503. Found = FT.getTypePtr();
  504. } else {
  505. Found = isSingleElementStruct(FT, Context);
  506. if (!Found)
  507. return nullptr;
  508. }
  509. }
  510. // We don't consider a struct a single-element struct if it has
  511. // padding beyond the element type.
  512. if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
  513. return nullptr;
  514. return Found;
  515. }
  516. namespace {
  517. Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
  518. const ABIArgInfo &AI) {
  519. // This default implementation defers to the llvm backend's va_arg
  520. // instruction. It can handle only passing arguments directly
  521. // (typically only handled in the backend for primitive types), or
  522. // aggregates passed indirectly by pointer (NOTE: if the "byval"
  523. // flag has ABI impact in the callee, this implementation cannot
  524. // work.)
  525. // Only a few cases are covered here at the moment -- those needed
  526. // by the default abi.
  527. llvm::Value *Val;
  528. if (AI.isIndirect()) {
  529. assert(!AI.getPaddingType() &&
  530. "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
  531. assert(
  532. !AI.getIndirectRealign() &&
  533. "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
  534. auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
  535. CharUnits TyAlignForABI = TyInfo.second;
  536. llvm::Type *BaseTy =
  537. llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
  538. llvm::Value *Addr =
  539. CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
  540. return Address(Addr, TyAlignForABI);
  541. } else {
  542. assert((AI.isDirect() || AI.isExtend()) &&
  543. "Unexpected ArgInfo Kind in generic VAArg emitter!");
  544. assert(!AI.getInReg() &&
  545. "Unexpected InReg seen in arginfo in generic VAArg emitter!");
  546. assert(!AI.getPaddingType() &&
  547. "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
  548. assert(!AI.getDirectOffset() &&
  549. "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
  550. assert(!AI.getCoerceToType() &&
  551. "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
  552. Address Temp = CGF.CreateMemTemp(Ty, "varet");
  553. Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty));
  554. CGF.Builder.CreateStore(Val, Temp);
  555. return Temp;
  556. }
  557. }
  558. /// DefaultABIInfo - The default implementation for ABI specific
  559. /// details. This implementation provides information which results in
  560. /// self-consistent and sensible LLVM IR generation, but does not
  561. /// conform to any particular ABI.
  562. class DefaultABIInfo : public ABIInfo {
  563. public:
  564. DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
  565. ABIArgInfo classifyReturnType(QualType RetTy) const;
  566. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  567. void computeInfo(CGFunctionInfo &FI) const override {
  568. if (!getCXXABI().classifyReturnType(FI))
  569. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  570. for (auto &I : FI.arguments())
  571. I.info = classifyArgumentType(I.type);
  572. }
  573. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  574. QualType Ty) const override {
  575. return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
  576. }
  577. };
  578. class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
  579. public:
  580. DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  581. : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
  582. };
  583. ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
  584. Ty = useFirstFieldIfTransparentUnion(Ty);
  585. if (isAggregateTypeForABI(Ty)) {
  586. // Records with non-trivial destructors/copy-constructors should not be
  587. // passed by value.
  588. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  589. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  590. return getNaturalAlignIndirect(Ty);
  591. }
  592. // Treat an enum type as its underlying type.
  593. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  594. Ty = EnumTy->getDecl()->getIntegerType();
  595. return (Ty->isPromotableIntegerType() ?
  596. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  597. }
  598. ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
  599. if (RetTy->isVoidType())
  600. return ABIArgInfo::getIgnore();
  601. if (isAggregateTypeForABI(RetTy))
  602. return getNaturalAlignIndirect(RetTy);
  603. // Treat an enum type as its underlying type.
  604. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  605. RetTy = EnumTy->getDecl()->getIntegerType();
  606. return (RetTy->isPromotableIntegerType() ?
  607. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  608. }
  609. //===----------------------------------------------------------------------===//
  610. // WebAssembly ABI Implementation
  611. //
  612. // This is a very simple ABI that relies a lot on DefaultABIInfo.
  613. //===----------------------------------------------------------------------===//
  614. class WebAssemblyABIInfo final : public DefaultABIInfo {
  615. public:
  616. explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT)
  617. : DefaultABIInfo(CGT) {}
  618. private:
  619. ABIArgInfo classifyReturnType(QualType RetTy) const;
  620. ABIArgInfo classifyArgumentType(QualType Ty) const;
  621. // DefaultABIInfo's classifyReturnType and classifyArgumentType are
  622. // non-virtual, but computeInfo and EmitVAArg are virtual, so we
  623. // overload them.
  624. void computeInfo(CGFunctionInfo &FI) const override {
  625. if (!getCXXABI().classifyReturnType(FI))
  626. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  627. for (auto &Arg : FI.arguments())
  628. Arg.info = classifyArgumentType(Arg.type);
  629. }
  630. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  631. QualType Ty) const override;
  632. };
  633. class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
  634. public:
  635. explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  636. : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {}
  637. };
  638. /// \brief Classify argument of given type \p Ty.
  639. ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
  640. Ty = useFirstFieldIfTransparentUnion(Ty);
  641. if (isAggregateTypeForABI(Ty)) {
  642. // Records with non-trivial destructors/copy-constructors should not be
  643. // passed by value.
  644. if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
  645. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  646. // Ignore empty structs/unions.
  647. if (isEmptyRecord(getContext(), Ty, true))
  648. return ABIArgInfo::getIgnore();
  649. // Lower single-element structs to just pass a regular value. TODO: We
  650. // could do reasonable-size multiple-element structs too, using getExpand(),
  651. // though watch out for things like bitfields.
  652. if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
  653. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  654. }
  655. // Otherwise just do the default thing.
  656. return DefaultABIInfo::classifyArgumentType(Ty);
  657. }
  658. ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
  659. if (isAggregateTypeForABI(RetTy)) {
  660. // Records with non-trivial destructors/copy-constructors should not be
  661. // returned by value.
  662. if (!getRecordArgABI(RetTy, getCXXABI())) {
  663. // Ignore empty structs/unions.
  664. if (isEmptyRecord(getContext(), RetTy, true))
  665. return ABIArgInfo::getIgnore();
  666. // Lower single-element structs to just return a regular value. TODO: We
  667. // could do reasonable-size multiple-element structs too, using
  668. // ABIArgInfo::getDirect().
  669. if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
  670. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  671. }
  672. }
  673. // Otherwise just do the default thing.
  674. return DefaultABIInfo::classifyReturnType(RetTy);
  675. }
  676. Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  677. QualType Ty) const {
  678. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false,
  679. getContext().getTypeInfoInChars(Ty),
  680. CharUnits::fromQuantity(4),
  681. /*AllowHigherAlign=*/ true);
  682. }
  683. //===----------------------------------------------------------------------===//
  684. // le32/PNaCl bitcode ABI Implementation
  685. //
  686. // This is a simplified version of the x86_32 ABI. Arguments and return values
  687. // are always passed on the stack.
  688. //===----------------------------------------------------------------------===//
  689. class PNaClABIInfo : public ABIInfo {
  690. public:
  691. PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
  692. ABIArgInfo classifyReturnType(QualType RetTy) const;
  693. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  694. void computeInfo(CGFunctionInfo &FI) const override;
  695. Address EmitVAArg(CodeGenFunction &CGF,
  696. Address VAListAddr, QualType Ty) const override;
  697. };
  698. class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
  699. public:
  700. PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  701. : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
  702. };
  703. void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
  704. if (!getCXXABI().classifyReturnType(FI))
  705. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  706. for (auto &I : FI.arguments())
  707. I.info = classifyArgumentType(I.type);
  708. }
  709. Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  710. QualType Ty) const {
  711. // The PNaCL ABI is a bit odd, in that varargs don't use normal
  712. // function classification. Structs get passed directly for varargs
  713. // functions, through a rewriting transform in
  714. // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
  715. // this target to actually support a va_arg instructions with an
  716. // aggregate type, unlike other targets.
  717. return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
  718. }
  719. /// \brief Classify argument of given type \p Ty.
  720. ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
  721. if (isAggregateTypeForABI(Ty)) {
  722. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  723. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  724. return getNaturalAlignIndirect(Ty);
  725. } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
  726. // Treat an enum type as its underlying type.
  727. Ty = EnumTy->getDecl()->getIntegerType();
  728. } else if (Ty->isFloatingType()) {
  729. // Floating-point types don't go inreg.
  730. return ABIArgInfo::getDirect();
  731. }
  732. return (Ty->isPromotableIntegerType() ?
  733. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  734. }
  735. ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
  736. if (RetTy->isVoidType())
  737. return ABIArgInfo::getIgnore();
  738. // In the PNaCl ABI we always return records/structures on the stack.
  739. if (isAggregateTypeForABI(RetTy))
  740. return getNaturalAlignIndirect(RetTy);
  741. // Treat an enum type as its underlying type.
  742. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  743. RetTy = EnumTy->getDecl()->getIntegerType();
  744. return (RetTy->isPromotableIntegerType() ?
  745. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  746. }
  747. /// IsX86_MMXType - Return true if this is an MMX type.
  748. bool IsX86_MMXType(llvm::Type *IRType) {
  749. // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
  750. return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
  751. cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
  752. IRType->getScalarSizeInBits() != 64;
  753. }
  754. static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  755. StringRef Constraint,
  756. llvm::Type* Ty) {
  757. bool IsMMXCons = llvm::StringSwitch<bool>(Constraint)
  758. .Cases("y", "&y", "^Ym", true)
  759. .Default(false);
  760. if (IsMMXCons && Ty->isVectorTy()) {
  761. if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
  762. // Invalid MMX constraint
  763. return nullptr;
  764. }
  765. return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
  766. }
  767. // No operation needed
  768. return Ty;
  769. }
  770. /// Returns true if this type can be passed in SSE registers with the
  771. /// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
  772. static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
  773. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  774. if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) {
  775. if (BT->getKind() == BuiltinType::LongDouble) {
  776. if (&Context.getTargetInfo().getLongDoubleFormat() ==
  777. &llvm::APFloat::x87DoubleExtended())
  778. return false;
  779. }
  780. return true;
  781. }
  782. } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
  783. // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
  784. // registers specially.
  785. unsigned VecSize = Context.getTypeSize(VT);
  786. if (VecSize == 128 || VecSize == 256 || VecSize == 512)
  787. return true;
  788. }
  789. return false;
  790. }
  791. /// Returns true if this aggregate is small enough to be passed in SSE registers
  792. /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
  793. static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
  794. return NumMembers <= 4;
  795. }
  796. /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86.
  797. static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) {
  798. auto AI = ABIArgInfo::getDirect(T);
  799. AI.setInReg(true);
  800. AI.setCanBeFlattened(false);
  801. return AI;
  802. }
  803. //===----------------------------------------------------------------------===//
  804. // X86-32 ABI Implementation
  805. //===----------------------------------------------------------------------===//
  806. /// \brief Similar to llvm::CCState, but for Clang.
  807. struct CCState {
  808. CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
  809. unsigned CC;
  810. unsigned FreeRegs;
  811. unsigned FreeSSERegs;
  812. };
  813. enum {
  814. // Vectorcall only allows the first 6 parameters to be passed in registers.
  815. VectorcallMaxParamNumAsReg = 6
  816. };
  817. /// X86_32ABIInfo - The X86-32 ABI information.
  818. class X86_32ABIInfo : public SwiftABIInfo {
  819. enum Class {
  820. Integer,
  821. Float
  822. };
  823. static const unsigned MinABIStackAlignInBytes = 4;
  824. bool IsDarwinVectorABI;
  825. bool IsRetSmallStructInRegABI;
  826. bool IsWin32StructABI;
  827. bool IsSoftFloatABI;
  828. bool IsMCUABI;
  829. unsigned DefaultNumRegisterParameters;
  830. static bool isRegisterSize(unsigned Size) {
  831. return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
  832. }
  833. bool isHomogeneousAggregateBaseType(QualType Ty) const override {
  834. // FIXME: Assumes vectorcall is in use.
  835. return isX86VectorTypeForVectorCall(getContext(), Ty);
  836. }
  837. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  838. uint64_t NumMembers) const override {
  839. // FIXME: Assumes vectorcall is in use.
  840. return isX86VectorCallAggregateSmallEnough(NumMembers);
  841. }
  842. bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
  843. /// getIndirectResult - Give a source type \arg Ty, return a suitable result
  844. /// such that the argument will be passed in memory.
  845. ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
  846. ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
  847. /// \brief Return the alignment to use for the given type on the stack.
  848. unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
  849. Class classify(QualType Ty) const;
  850. ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
  851. ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
  852. /// \brief Updates the number of available free registers, returns
  853. /// true if any registers were allocated.
  854. bool updateFreeRegs(QualType Ty, CCState &State) const;
  855. bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
  856. bool &NeedsPadding) const;
  857. bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
  858. bool canExpandIndirectArgument(QualType Ty) const;
  859. /// \brief Rewrite the function info so that all memory arguments use
  860. /// inalloca.
  861. void rewriteWithInAlloca(CGFunctionInfo &FI) const;
  862. void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
  863. CharUnits &StackOffset, ABIArgInfo &Info,
  864. QualType Type) const;
  865. void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
  866. bool &UsedInAlloca) const;
  867. public:
  868. void computeInfo(CGFunctionInfo &FI) const override;
  869. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  870. QualType Ty) const override;
  871. X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
  872. bool RetSmallStructInRegABI, bool Win32StructABI,
  873. unsigned NumRegisterParameters, bool SoftFloatABI)
  874. : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
  875. IsRetSmallStructInRegABI(RetSmallStructInRegABI),
  876. IsWin32StructABI(Win32StructABI),
  877. IsSoftFloatABI(SoftFloatABI),
  878. IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
  879. DefaultNumRegisterParameters(NumRegisterParameters) {}
  880. bool shouldPassIndirectlyForSwift(CharUnits totalSize,
  881. ArrayRef<llvm::Type*> scalars,
  882. bool asReturnValue) const override {
  883. // LLVM's x86-32 lowering currently only assigns up to three
  884. // integer registers and three fp registers. Oddly, it'll use up to
  885. // four vector registers for vectors, but those can overlap with the
  886. // scalar registers.
  887. return occupiesMoreThan(CGT, scalars, /*total*/ 3);
  888. }
  889. bool isSwiftErrorInRegister() const override {
  890. // x86-32 lowering does not support passing swifterror in a register.
  891. return false;
  892. }
  893. };
  894. class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
  895. public:
  896. X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
  897. bool RetSmallStructInRegABI, bool Win32StructABI,
  898. unsigned NumRegisterParameters, bool SoftFloatABI)
  899. : TargetCodeGenInfo(new X86_32ABIInfo(
  900. CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
  901. NumRegisterParameters, SoftFloatABI)) {}
  902. static bool isStructReturnInRegABI(
  903. const llvm::Triple &Triple, const CodeGenOptions &Opts);
  904. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  905. CodeGen::CodeGenModule &CGM,
  906. ForDefinition_t IsForDefinition) const override;
  907. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
  908. // Darwin uses different dwarf register numbers for EH.
  909. if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
  910. return 4;
  911. }
  912. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  913. llvm::Value *Address) const override;
  914. llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  915. StringRef Constraint,
  916. llvm::Type* Ty) const override {
  917. return X86AdjustInlineAsmType(CGF, Constraint, Ty);
  918. }
  919. void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
  920. std::string &Constraints,
  921. std::vector<llvm::Type *> &ResultRegTypes,
  922. std::vector<llvm::Type *> &ResultTruncRegTypes,
  923. std::vector<LValue> &ResultRegDests,
  924. std::string &AsmString,
  925. unsigned NumOutputs) const override;
  926. llvm::Constant *
  927. getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
  928. unsigned Sig = (0xeb << 0) | // jmp rel8
  929. (0x06 << 8) | // .+0x08
  930. ('v' << 16) |
  931. ('2' << 24);
  932. return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
  933. }
  934. StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
  935. return "movl\t%ebp, %ebp"
  936. "\t\t// marker for objc_retainAutoreleaseReturnValue";
  937. }
  938. };
  939. }
  940. /// Rewrite input constraint references after adding some output constraints.
  941. /// In the case where there is one output and one input and we add one output,
  942. /// we need to replace all operand references greater than or equal to 1:
  943. /// mov $0, $1
  944. /// mov eax, $1
  945. /// The result will be:
  946. /// mov $0, $2
  947. /// mov eax, $2
  948. static void rewriteInputConstraintReferences(unsigned FirstIn,
  949. unsigned NumNewOuts,
  950. std::string &AsmString) {
  951. std::string Buf;
  952. llvm::raw_string_ostream OS(Buf);
  953. size_t Pos = 0;
  954. while (Pos < AsmString.size()) {
  955. size_t DollarStart = AsmString.find('$', Pos);
  956. if (DollarStart == std::string::npos)
  957. DollarStart = AsmString.size();
  958. size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
  959. if (DollarEnd == std::string::npos)
  960. DollarEnd = AsmString.size();
  961. OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
  962. Pos = DollarEnd;
  963. size_t NumDollars = DollarEnd - DollarStart;
  964. if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
  965. // We have an operand reference.
  966. size_t DigitStart = Pos;
  967. size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
  968. if (DigitEnd == std::string::npos)
  969. DigitEnd = AsmString.size();
  970. StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
  971. unsigned OperandIndex;
  972. if (!OperandStr.getAsInteger(10, OperandIndex)) {
  973. if (OperandIndex >= FirstIn)
  974. OperandIndex += NumNewOuts;
  975. OS << OperandIndex;
  976. } else {
  977. OS << OperandStr;
  978. }
  979. Pos = DigitEnd;
  980. }
  981. }
  982. AsmString = std::move(OS.str());
  983. }
  984. /// Add output constraints for EAX:EDX because they are return registers.
  985. void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
  986. CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
  987. std::vector<llvm::Type *> &ResultRegTypes,
  988. std::vector<llvm::Type *> &ResultTruncRegTypes,
  989. std::vector<LValue> &ResultRegDests, std::string &AsmString,
  990. unsigned NumOutputs) const {
  991. uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
  992. // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
  993. // larger.
  994. if (!Constraints.empty())
  995. Constraints += ',';
  996. if (RetWidth <= 32) {
  997. Constraints += "={eax}";
  998. ResultRegTypes.push_back(CGF.Int32Ty);
  999. } else {
  1000. // Use the 'A' constraint for EAX:EDX.
  1001. Constraints += "=A";
  1002. ResultRegTypes.push_back(CGF.Int64Ty);
  1003. }
  1004. // Truncate EAX or EAX:EDX to an integer of the appropriate size.
  1005. llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
  1006. ResultTruncRegTypes.push_back(CoerceTy);
  1007. // Coerce the integer by bitcasting the return slot pointer.
  1008. ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(),
  1009. CoerceTy->getPointerTo()));
  1010. ResultRegDests.push_back(ReturnSlot);
  1011. rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
  1012. }
  1013. /// shouldReturnTypeInRegister - Determine if the given type should be
  1014. /// returned in a register (for the Darwin and MCU ABI).
  1015. bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
  1016. ASTContext &Context) const {
  1017. uint64_t Size = Context.getTypeSize(Ty);
  1018. // For i386, type must be register sized.
  1019. // For the MCU ABI, it only needs to be <= 8-byte
  1020. if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
  1021. return false;
  1022. if (Ty->isVectorType()) {
  1023. // 64- and 128- bit vectors inside structures are not returned in
  1024. // registers.
  1025. if (Size == 64 || Size == 128)
  1026. return false;
  1027. return true;
  1028. }
  1029. // If this is a builtin, pointer, enum, complex type, member pointer, or
  1030. // member function pointer it is ok.
  1031. if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
  1032. Ty->isAnyComplexType() || Ty->isEnumeralType() ||
  1033. Ty->isBlockPointerType() || Ty->isMemberPointerType())
  1034. return true;
  1035. // Arrays are treated like records.
  1036. if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
  1037. return shouldReturnTypeInRegister(AT->getElementType(), Context);
  1038. // Otherwise, it must be a record type.
  1039. const RecordType *RT = Ty->getAs<RecordType>();
  1040. if (!RT) return false;
  1041. // FIXME: Traverse bases here too.
  1042. // Structure types are passed in register if all fields would be
  1043. // passed in a register.
  1044. for (const auto *FD : RT->getDecl()->fields()) {
  1045. // Empty fields are ignored.
  1046. if (isEmptyField(Context, FD, true))
  1047. continue;
  1048. // Check fields recursively.
  1049. if (!shouldReturnTypeInRegister(FD->getType(), Context))
  1050. return false;
  1051. }
  1052. return true;
  1053. }
  1054. static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
  1055. // Treat complex types as the element type.
  1056. if (const ComplexType *CTy = Ty->getAs<ComplexType>())
  1057. Ty = CTy->getElementType();
  1058. // Check for a type which we know has a simple scalar argument-passing
  1059. // convention without any padding. (We're specifically looking for 32
  1060. // and 64-bit integer and integer-equivalents, float, and double.)
  1061. if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
  1062. !Ty->isEnumeralType() && !Ty->isBlockPointerType())
  1063. return false;
  1064. uint64_t Size = Context.getTypeSize(Ty);
  1065. return Size == 32 || Size == 64;
  1066. }
  1067. static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD,
  1068. uint64_t &Size) {
  1069. for (const auto *FD : RD->fields()) {
  1070. // Scalar arguments on the stack get 4 byte alignment on x86. If the
  1071. // argument is smaller than 32-bits, expanding the struct will create
  1072. // alignment padding.
  1073. if (!is32Or64BitBasicType(FD->getType(), Context))
  1074. return false;
  1075. // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
  1076. // how to expand them yet, and the predicate for telling if a bitfield still
  1077. // counts as "basic" is more complicated than what we were doing previously.
  1078. if (FD->isBitField())
  1079. return false;
  1080. Size += Context.getTypeSize(FD->getType());
  1081. }
  1082. return true;
  1083. }
  1084. static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD,
  1085. uint64_t &Size) {
  1086. // Don't do this if there are any non-empty bases.
  1087. for (const CXXBaseSpecifier &Base : RD->bases()) {
  1088. if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(),
  1089. Size))
  1090. return false;
  1091. }
  1092. if (!addFieldSizes(Context, RD, Size))
  1093. return false;
  1094. return true;
  1095. }
  1096. /// Test whether an argument type which is to be passed indirectly (on the
  1097. /// stack) would have the equivalent layout if it was expanded into separate
  1098. /// arguments. If so, we prefer to do the latter to avoid inhibiting
  1099. /// optimizations.
  1100. bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
  1101. // We can only expand structure types.
  1102. const RecordType *RT = Ty->getAs<RecordType>();
  1103. if (!RT)
  1104. return false;
  1105. const RecordDecl *RD = RT->getDecl();
  1106. uint64_t Size = 0;
  1107. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  1108. if (!IsWin32StructABI) {
  1109. // On non-Windows, we have to conservatively match our old bitcode
  1110. // prototypes in order to be ABI-compatible at the bitcode level.
  1111. if (!CXXRD->isCLike())
  1112. return false;
  1113. } else {
  1114. // Don't do this for dynamic classes.
  1115. if (CXXRD->isDynamicClass())
  1116. return false;
  1117. }
  1118. if (!addBaseAndFieldSizes(getContext(), CXXRD, Size))
  1119. return false;
  1120. } else {
  1121. if (!addFieldSizes(getContext(), RD, Size))
  1122. return false;
  1123. }
  1124. // We can do this if there was no alignment padding.
  1125. return Size == getContext().getTypeSize(Ty);
  1126. }
  1127. ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
  1128. // If the return value is indirect, then the hidden argument is consuming one
  1129. // integer register.
  1130. if (State.FreeRegs) {
  1131. --State.FreeRegs;
  1132. if (!IsMCUABI)
  1133. return getNaturalAlignIndirectInReg(RetTy);
  1134. }
  1135. return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
  1136. }
  1137. ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
  1138. CCState &State) const {
  1139. if (RetTy->isVoidType())
  1140. return ABIArgInfo::getIgnore();
  1141. const Type *Base = nullptr;
  1142. uint64_t NumElts = 0;
  1143. if ((State.CC == llvm::CallingConv::X86_VectorCall ||
  1144. State.CC == llvm::CallingConv::X86_RegCall) &&
  1145. isHomogeneousAggregate(RetTy, Base, NumElts)) {
  1146. // The LLVM struct type for such an aggregate should lower properly.
  1147. return ABIArgInfo::getDirect();
  1148. }
  1149. if (const VectorType *VT = RetTy->getAs<VectorType>()) {
  1150. // On Darwin, some vectors are returned in registers.
  1151. if (IsDarwinVectorABI) {
  1152. uint64_t Size = getContext().getTypeSize(RetTy);
  1153. // 128-bit vectors are a special case; they are returned in
  1154. // registers and we need to make sure to pick a type the LLVM
  1155. // backend will like.
  1156. if (Size == 128)
  1157. return ABIArgInfo::getDirect(llvm::VectorType::get(
  1158. llvm::Type::getInt64Ty(getVMContext()), 2));
  1159. // Always return in register if it fits in a general purpose
  1160. // register, or if it is 64 bits and has a single element.
  1161. if ((Size == 8 || Size == 16 || Size == 32) ||
  1162. (Size == 64 && VT->getNumElements() == 1))
  1163. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  1164. Size));
  1165. return getIndirectReturnResult(RetTy, State);
  1166. }
  1167. return ABIArgInfo::getDirect();
  1168. }
  1169. if (isAggregateTypeForABI(RetTy)) {
  1170. if (const RecordType *RT = RetTy->getAs<RecordType>()) {
  1171. // Structures with flexible arrays are always indirect.
  1172. if (RT->getDecl()->hasFlexibleArrayMember())
  1173. return getIndirectReturnResult(RetTy, State);
  1174. }
  1175. // If specified, structs and unions are always indirect.
  1176. if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
  1177. return getIndirectReturnResult(RetTy, State);
  1178. // Ignore empty structs/unions.
  1179. if (isEmptyRecord(getContext(), RetTy, true))
  1180. return ABIArgInfo::getIgnore();
  1181. // Small structures which are register sized are generally returned
  1182. // in a register.
  1183. if (shouldReturnTypeInRegister(RetTy, getContext())) {
  1184. uint64_t Size = getContext().getTypeSize(RetTy);
  1185. // As a special-case, if the struct is a "single-element" struct, and
  1186. // the field is of type "float" or "double", return it in a
  1187. // floating-point register. (MSVC does not apply this special case.)
  1188. // We apply a similar transformation for pointer types to improve the
  1189. // quality of the generated IR.
  1190. if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
  1191. if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
  1192. || SeltTy->hasPointerRepresentation())
  1193. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  1194. // FIXME: We should be able to narrow this integer in cases with dead
  1195. // padding.
  1196. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
  1197. }
  1198. return getIndirectReturnResult(RetTy, State);
  1199. }
  1200. // Treat an enum type as its underlying type.
  1201. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  1202. RetTy = EnumTy->getDecl()->getIntegerType();
  1203. return (RetTy->isPromotableIntegerType() ?
  1204. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  1205. }
  1206. static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
  1207. return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
  1208. }
  1209. static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
  1210. const RecordType *RT = Ty->getAs<RecordType>();
  1211. if (!RT)
  1212. return 0;
  1213. const RecordDecl *RD = RT->getDecl();
  1214. // If this is a C++ record, check the bases first.
  1215. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  1216. for (const auto &I : CXXRD->bases())
  1217. if (!isRecordWithSSEVectorType(Context, I.getType()))
  1218. return false;
  1219. for (const auto *i : RD->fields()) {
  1220. QualType FT = i->getType();
  1221. if (isSSEVectorType(Context, FT))
  1222. return true;
  1223. if (isRecordWithSSEVectorType(Context, FT))
  1224. return true;
  1225. }
  1226. return false;
  1227. }
  1228. unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
  1229. unsigned Align) const {
  1230. // Otherwise, if the alignment is less than or equal to the minimum ABI
  1231. // alignment, just use the default; the backend will handle this.
  1232. if (Align <= MinABIStackAlignInBytes)
  1233. return 0; // Use default alignment.
  1234. // On non-Darwin, the stack type alignment is always 4.
  1235. if (!IsDarwinVectorABI) {
  1236. // Set explicit alignment, since we may need to realign the top.
  1237. return MinABIStackAlignInBytes;
  1238. }
  1239. // Otherwise, if the type contains an SSE vector type, the alignment is 16.
  1240. if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
  1241. isRecordWithSSEVectorType(getContext(), Ty)))
  1242. return 16;
  1243. return MinABIStackAlignInBytes;
  1244. }
  1245. ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
  1246. CCState &State) const {
  1247. if (!ByVal) {
  1248. if (State.FreeRegs) {
  1249. --State.FreeRegs; // Non-byval indirects just use one pointer.
  1250. if (!IsMCUABI)
  1251. return getNaturalAlignIndirectInReg(Ty);
  1252. }
  1253. return getNaturalAlignIndirect(Ty, false);
  1254. }
  1255. // Compute the byval alignment.
  1256. unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
  1257. unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
  1258. if (StackAlign == 0)
  1259. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
  1260. // If the stack alignment is less than the type alignment, realign the
  1261. // argument.
  1262. bool Realign = TypeAlign > StackAlign;
  1263. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
  1264. /*ByVal=*/true, Realign);
  1265. }
  1266. X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
  1267. const Type *T = isSingleElementStruct(Ty, getContext());
  1268. if (!T)
  1269. T = Ty.getTypePtr();
  1270. if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
  1271. BuiltinType::Kind K = BT->getKind();
  1272. if (K == BuiltinType::Float || K == BuiltinType::Double)
  1273. return Float;
  1274. }
  1275. return Integer;
  1276. }
  1277. bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
  1278. if (!IsSoftFloatABI) {
  1279. Class C = classify(Ty);
  1280. if (C == Float)
  1281. return false;
  1282. }
  1283. unsigned Size = getContext().getTypeSize(Ty);
  1284. unsigned SizeInRegs = (Size + 31) / 32;
  1285. if (SizeInRegs == 0)
  1286. return false;
  1287. if (!IsMCUABI) {
  1288. if (SizeInRegs > State.FreeRegs) {
  1289. State.FreeRegs = 0;
  1290. return false;
  1291. }
  1292. } else {
  1293. // The MCU psABI allows passing parameters in-reg even if there are
  1294. // earlier parameters that are passed on the stack. Also,
  1295. // it does not allow passing >8-byte structs in-register,
  1296. // even if there are 3 free registers available.
  1297. if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
  1298. return false;
  1299. }
  1300. State.FreeRegs -= SizeInRegs;
  1301. return true;
  1302. }
  1303. bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
  1304. bool &InReg,
  1305. bool &NeedsPadding) const {
  1306. // On Windows, aggregates other than HFAs are never passed in registers, and
  1307. // they do not consume register slots. Homogenous floating-point aggregates
  1308. // (HFAs) have already been dealt with at this point.
  1309. if (IsWin32StructABI && isAggregateTypeForABI(Ty))
  1310. return false;
  1311. NeedsPadding = false;
  1312. InReg = !IsMCUABI;
  1313. if (!updateFreeRegs(Ty, State))
  1314. return false;
  1315. if (IsMCUABI)
  1316. return true;
  1317. if (State.CC == llvm::CallingConv::X86_FastCall ||
  1318. State.CC == llvm::CallingConv::X86_VectorCall ||
  1319. State.CC == llvm::CallingConv::X86_RegCall) {
  1320. if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
  1321. NeedsPadding = true;
  1322. return false;
  1323. }
  1324. return true;
  1325. }
  1326. bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
  1327. if (!updateFreeRegs(Ty, State))
  1328. return false;
  1329. if (IsMCUABI)
  1330. return false;
  1331. if (State.CC == llvm::CallingConv::X86_FastCall ||
  1332. State.CC == llvm::CallingConv::X86_VectorCall ||
  1333. State.CC == llvm::CallingConv::X86_RegCall) {
  1334. if (getContext().getTypeSize(Ty) > 32)
  1335. return false;
  1336. return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
  1337. Ty->isReferenceType());
  1338. }
  1339. return true;
  1340. }
  1341. ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
  1342. CCState &State) const {
  1343. // FIXME: Set alignment on indirect arguments.
  1344. Ty = useFirstFieldIfTransparentUnion(Ty);
  1345. // Check with the C++ ABI first.
  1346. const RecordType *RT = Ty->getAs<RecordType>();
  1347. if (RT) {
  1348. CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
  1349. if (RAA == CGCXXABI::RAA_Indirect) {
  1350. return getIndirectResult(Ty, false, State);
  1351. } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
  1352. // The field index doesn't matter, we'll fix it up later.
  1353. return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
  1354. }
  1355. }
  1356. // Regcall uses the concept of a homogenous vector aggregate, similar
  1357. // to other targets.
  1358. const Type *Base = nullptr;
  1359. uint64_t NumElts = 0;
  1360. if (State.CC == llvm::CallingConv::X86_RegCall &&
  1361. isHomogeneousAggregate(Ty, Base, NumElts)) {
  1362. if (State.FreeSSERegs >= NumElts) {
  1363. State.FreeSSERegs -= NumElts;
  1364. if (Ty->isBuiltinType() || Ty->isVectorType())
  1365. return ABIArgInfo::getDirect();
  1366. return ABIArgInfo::getExpand();
  1367. }
  1368. return getIndirectResult(Ty, /*ByVal=*/false, State);
  1369. }
  1370. if (isAggregateTypeForABI(Ty)) {
  1371. // Structures with flexible arrays are always indirect.
  1372. // FIXME: This should not be byval!
  1373. if (RT && RT->getDecl()->hasFlexibleArrayMember())
  1374. return getIndirectResult(Ty, true, State);
  1375. // Ignore empty structs/unions on non-Windows.
  1376. if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
  1377. return ABIArgInfo::getIgnore();
  1378. llvm::LLVMContext &LLVMContext = getVMContext();
  1379. llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
  1380. bool NeedsPadding = false;
  1381. bool InReg;
  1382. if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
  1383. unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
  1384. SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
  1385. llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
  1386. if (InReg)
  1387. return ABIArgInfo::getDirectInReg(Result);
  1388. else
  1389. return ABIArgInfo::getDirect(Result);
  1390. }
  1391. llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
  1392. // Expand small (<= 128-bit) record types when we know that the stack layout
  1393. // of those arguments will match the struct. This is important because the
  1394. // LLVM backend isn't smart enough to remove byval, which inhibits many
  1395. // optimizations.
  1396. // Don't do this for the MCU if there are still free integer registers
  1397. // (see X86_64 ABI for full explanation).
  1398. if (getContext().getTypeSize(Ty) <= 4 * 32 &&
  1399. (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty))
  1400. return ABIArgInfo::getExpandWithPadding(
  1401. State.CC == llvm::CallingConv::X86_FastCall ||
  1402. State.CC == llvm::CallingConv::X86_VectorCall ||
  1403. State.CC == llvm::CallingConv::X86_RegCall,
  1404. PaddingType);
  1405. return getIndirectResult(Ty, true, State);
  1406. }
  1407. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  1408. // On Darwin, some vectors are passed in memory, we handle this by passing
  1409. // it as an i8/i16/i32/i64.
  1410. if (IsDarwinVectorABI) {
  1411. uint64_t Size = getContext().getTypeSize(Ty);
  1412. if ((Size == 8 || Size == 16 || Size == 32) ||
  1413. (Size == 64 && VT->getNumElements() == 1))
  1414. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  1415. Size));
  1416. }
  1417. if (IsX86_MMXType(CGT.ConvertType(Ty)))
  1418. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
  1419. return ABIArgInfo::getDirect();
  1420. }
  1421. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  1422. Ty = EnumTy->getDecl()->getIntegerType();
  1423. bool InReg = shouldPrimitiveUseInReg(Ty, State);
  1424. if (Ty->isPromotableIntegerType()) {
  1425. if (InReg)
  1426. return ABIArgInfo::getExtendInReg();
  1427. return ABIArgInfo::getExtend();
  1428. }
  1429. if (InReg)
  1430. return ABIArgInfo::getDirectInReg();
  1431. return ABIArgInfo::getDirect();
  1432. }
  1433. void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
  1434. bool &UsedInAlloca) const {
  1435. // Vectorcall x86 works subtly different than in x64, so the format is
  1436. // a bit different than the x64 version. First, all vector types (not HVAs)
  1437. // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers.
  1438. // This differs from the x64 implementation, where the first 6 by INDEX get
  1439. // registers.
  1440. // After that, integers AND HVAs are assigned Left to Right in the same pass.
  1441. // Integers are passed as ECX/EDX if one is available (in order). HVAs will
  1442. // first take up the remaining YMM/XMM registers. If insufficient registers
  1443. // remain but an integer register (ECX/EDX) is available, it will be passed
  1444. // in that, else, on the stack.
  1445. for (auto &I : FI.arguments()) {
  1446. // First pass do all the vector types.
  1447. const Type *Base = nullptr;
  1448. uint64_t NumElts = 0;
  1449. const QualType& Ty = I.type;
  1450. if ((Ty->isVectorType() || Ty->isBuiltinType()) &&
  1451. isHomogeneousAggregate(Ty, Base, NumElts)) {
  1452. if (State.FreeSSERegs >= NumElts) {
  1453. State.FreeSSERegs -= NumElts;
  1454. I.info = ABIArgInfo::getDirect();
  1455. } else {
  1456. I.info = classifyArgumentType(Ty, State);
  1457. }
  1458. UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
  1459. }
  1460. }
  1461. for (auto &I : FI.arguments()) {
  1462. // Second pass, do the rest!
  1463. const Type *Base = nullptr;
  1464. uint64_t NumElts = 0;
  1465. const QualType& Ty = I.type;
  1466. bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts);
  1467. if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) {
  1468. // Assign true HVAs (non vector/native FP types).
  1469. if (State.FreeSSERegs >= NumElts) {
  1470. State.FreeSSERegs -= NumElts;
  1471. I.info = getDirectX86Hva();
  1472. } else {
  1473. I.info = getIndirectResult(Ty, /*ByVal=*/false, State);
  1474. }
  1475. } else if (!IsHva) {
  1476. // Assign all Non-HVAs, so this will exclude Vector/FP args.
  1477. I.info = classifyArgumentType(Ty, State);
  1478. UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
  1479. }
  1480. }
  1481. }
  1482. void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  1483. CCState State(FI.getCallingConvention());
  1484. if (IsMCUABI)
  1485. State.FreeRegs = 3;
  1486. else if (State.CC == llvm::CallingConv::X86_FastCall)
  1487. State.FreeRegs = 2;
  1488. else if (State.CC == llvm::CallingConv::X86_VectorCall) {
  1489. State.FreeRegs = 2;
  1490. State.FreeSSERegs = 6;
  1491. } else if (FI.getHasRegParm())
  1492. State.FreeRegs = FI.getRegParm();
  1493. else if (State.CC == llvm::CallingConv::X86_RegCall) {
  1494. State.FreeRegs = 5;
  1495. State.FreeSSERegs = 8;
  1496. } else
  1497. State.FreeRegs = DefaultNumRegisterParameters;
  1498. if (!getCXXABI().classifyReturnType(FI)) {
  1499. FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
  1500. } else if (FI.getReturnInfo().isIndirect()) {
  1501. // The C++ ABI is not aware of register usage, so we have to check if the
  1502. // return value was sret and put it in a register ourselves if appropriate.
  1503. if (State.FreeRegs) {
  1504. --State.FreeRegs; // The sret parameter consumes a register.
  1505. if (!IsMCUABI)
  1506. FI.getReturnInfo().setInReg(true);
  1507. }
  1508. }
  1509. // The chain argument effectively gives us another free register.
  1510. if (FI.isChainCall())
  1511. ++State.FreeRegs;
  1512. bool UsedInAlloca = false;
  1513. if (State.CC == llvm::CallingConv::X86_VectorCall) {
  1514. computeVectorCallArgs(FI, State, UsedInAlloca);
  1515. } else {
  1516. // If not vectorcall, revert to normal behavior.
  1517. for (auto &I : FI.arguments()) {
  1518. I.info = classifyArgumentType(I.type, State);
  1519. UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
  1520. }
  1521. }
  1522. // If we needed to use inalloca for any argument, do a second pass and rewrite
  1523. // all the memory arguments to use inalloca.
  1524. if (UsedInAlloca)
  1525. rewriteWithInAlloca(FI);
  1526. }
  1527. void
  1528. X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
  1529. CharUnits &StackOffset, ABIArgInfo &Info,
  1530. QualType Type) const {
  1531. // Arguments are always 4-byte-aligned.
  1532. CharUnits FieldAlign = CharUnits::fromQuantity(4);
  1533. assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct");
  1534. Info = ABIArgInfo::getInAlloca(FrameFields.size());
  1535. FrameFields.push_back(CGT.ConvertTypeForMem(Type));
  1536. StackOffset += getContext().getTypeSizeInChars(Type);
  1537. // Insert padding bytes to respect alignment.
  1538. CharUnits FieldEnd = StackOffset;
  1539. StackOffset = FieldEnd.alignTo(FieldAlign);
  1540. if (StackOffset != FieldEnd) {
  1541. CharUnits NumBytes = StackOffset - FieldEnd;
  1542. llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
  1543. Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
  1544. FrameFields.push_back(Ty);
  1545. }
  1546. }
  1547. static bool isArgInAlloca(const ABIArgInfo &Info) {
  1548. // Leave ignored and inreg arguments alone.
  1549. switch (Info.getKind()) {
  1550. case ABIArgInfo::InAlloca:
  1551. return true;
  1552. case ABIArgInfo::Indirect:
  1553. assert(Info.getIndirectByVal());
  1554. return true;
  1555. case ABIArgInfo::Ignore:
  1556. return false;
  1557. case ABIArgInfo::Direct:
  1558. case ABIArgInfo::Extend:
  1559. if (Info.getInReg())
  1560. return false;
  1561. return true;
  1562. case ABIArgInfo::Expand:
  1563. case ABIArgInfo::CoerceAndExpand:
  1564. // These are aggregate types which are never passed in registers when
  1565. // inalloca is involved.
  1566. return true;
  1567. }
  1568. llvm_unreachable("invalid enum");
  1569. }
  1570. void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
  1571. assert(IsWin32StructABI && "inalloca only supported on win32");
  1572. // Build a packed struct type for all of the arguments in memory.
  1573. SmallVector<llvm::Type *, 6> FrameFields;
  1574. // The stack alignment is always 4.
  1575. CharUnits StackAlign = CharUnits::fromQuantity(4);
  1576. CharUnits StackOffset;
  1577. CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
  1578. // Put 'this' into the struct before 'sret', if necessary.
  1579. bool IsThisCall =
  1580. FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
  1581. ABIArgInfo &Ret = FI.getReturnInfo();
  1582. if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
  1583. isArgInAlloca(I->info)) {
  1584. addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
  1585. ++I;
  1586. }
  1587. // Put the sret parameter into the inalloca struct if it's in memory.
  1588. if (Ret.isIndirect() && !Ret.getInReg()) {
  1589. CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
  1590. addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
  1591. // On Windows, the hidden sret parameter is always returned in eax.
  1592. Ret.setInAllocaSRet(IsWin32StructABI);
  1593. }
  1594. // Skip the 'this' parameter in ecx.
  1595. if (IsThisCall)
  1596. ++I;
  1597. // Put arguments passed in memory into the struct.
  1598. for (; I != E; ++I) {
  1599. if (isArgInAlloca(I->info))
  1600. addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
  1601. }
  1602. FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
  1603. /*isPacked=*/true),
  1604. StackAlign);
  1605. }
  1606. Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
  1607. Address VAListAddr, QualType Ty) const {
  1608. auto TypeInfo = getContext().getTypeInfoInChars(Ty);
  1609. // x86-32 changes the alignment of certain arguments on the stack.
  1610. //
  1611. // Just messing with TypeInfo like this works because we never pass
  1612. // anything indirectly.
  1613. TypeInfo.second = CharUnits::fromQuantity(
  1614. getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity()));
  1615. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
  1616. TypeInfo, CharUnits::fromQuantity(4),
  1617. /*AllowHigherAlign*/ true);
  1618. }
  1619. bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
  1620. const llvm::Triple &Triple, const CodeGenOptions &Opts) {
  1621. assert(Triple.getArch() == llvm::Triple::x86);
  1622. switch (Opts.getStructReturnConvention()) {
  1623. case CodeGenOptions::SRCK_Default:
  1624. break;
  1625. case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
  1626. return false;
  1627. case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
  1628. return true;
  1629. }
  1630. if (Triple.isOSDarwin() || Triple.isOSIAMCU())
  1631. return true;
  1632. switch (Triple.getOS()) {
  1633. case llvm::Triple::DragonFly:
  1634. case llvm::Triple::FreeBSD:
  1635. case llvm::Triple::OpenBSD:
  1636. case llvm::Triple::Win32:
  1637. return true;
  1638. default:
  1639. return false;
  1640. }
  1641. }
  1642. void X86_32TargetCodeGenInfo::setTargetAttributes(
  1643. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM,
  1644. ForDefinition_t IsForDefinition) const {
  1645. if (!IsForDefinition)
  1646. return;
  1647. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  1648. if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
  1649. // Get the LLVM function.
  1650. llvm::Function *Fn = cast<llvm::Function>(GV);
  1651. // Now add the 'alignstack' attribute with a value of 16.
  1652. llvm::AttrBuilder B;
  1653. B.addStackAlignmentAttr(16);
  1654. Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
  1655. }
  1656. if (FD->hasAttr<AnyX86InterruptAttr>()) {
  1657. llvm::Function *Fn = cast<llvm::Function>(GV);
  1658. Fn->setCallingConv(llvm::CallingConv::X86_INTR);
  1659. }
  1660. }
  1661. }
  1662. bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
  1663. CodeGen::CodeGenFunction &CGF,
  1664. llvm::Value *Address) const {
  1665. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  1666. llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
  1667. // 0-7 are the eight integer registers; the order is different
  1668. // on Darwin (for EH), but the range is the same.
  1669. // 8 is %eip.
  1670. AssignToArrayRange(Builder, Address, Four8, 0, 8);
  1671. if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
  1672. // 12-16 are st(0..4). Not sure why we stop at 4.
  1673. // These have size 16, which is sizeof(long double) on
  1674. // platforms with 8-byte alignment for that type.
  1675. llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
  1676. AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
  1677. } else {
  1678. // 9 is %eflags, which doesn't get a size on Darwin for some
  1679. // reason.
  1680. Builder.CreateAlignedStore(
  1681. Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
  1682. CharUnits::One());
  1683. // 11-16 are st(0..5). Not sure why we stop at 5.
  1684. // These have size 12, which is sizeof(long double) on
  1685. // platforms with 4-byte alignment for that type.
  1686. llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
  1687. AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
  1688. }
  1689. return false;
  1690. }
  1691. //===----------------------------------------------------------------------===//
  1692. // X86-64 ABI Implementation
  1693. //===----------------------------------------------------------------------===//
  1694. namespace {
  1695. /// The AVX ABI level for X86 targets.
  1696. enum class X86AVXABILevel {
  1697. None,
  1698. AVX,
  1699. AVX512
  1700. };
  1701. /// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
  1702. static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
  1703. switch (AVXLevel) {
  1704. case X86AVXABILevel::AVX512:
  1705. return 512;
  1706. case X86AVXABILevel::AVX:
  1707. return 256;
  1708. case X86AVXABILevel::None:
  1709. return 128;
  1710. }
  1711. llvm_unreachable("Unknown AVXLevel");
  1712. }
  1713. /// X86_64ABIInfo - The X86_64 ABI information.
  1714. class X86_64ABIInfo : public SwiftABIInfo {
  1715. enum Class {
  1716. Integer = 0,
  1717. SSE,
  1718. SSEUp,
  1719. X87,
  1720. X87Up,
  1721. ComplexX87,
  1722. NoClass,
  1723. Memory
  1724. };
  1725. /// merge - Implement the X86_64 ABI merging algorithm.
  1726. ///
  1727. /// Merge an accumulating classification \arg Accum with a field
  1728. /// classification \arg Field.
  1729. ///
  1730. /// \param Accum - The accumulating classification. This should
  1731. /// always be either NoClass or the result of a previous merge
  1732. /// call. In addition, this should never be Memory (the caller
  1733. /// should just return Memory for the aggregate).
  1734. static Class merge(Class Accum, Class Field);
  1735. /// postMerge - Implement the X86_64 ABI post merging algorithm.
  1736. ///
  1737. /// Post merger cleanup, reduces a malformed Hi and Lo pair to
  1738. /// final MEMORY or SSE classes when necessary.
  1739. ///
  1740. /// \param AggregateSize - The size of the current aggregate in
  1741. /// the classification process.
  1742. ///
  1743. /// \param Lo - The classification for the parts of the type
  1744. /// residing in the low word of the containing object.
  1745. ///
  1746. /// \param Hi - The classification for the parts of the type
  1747. /// residing in the higher words of the containing object.
  1748. ///
  1749. void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
  1750. /// classify - Determine the x86_64 register classes in which the
  1751. /// given type T should be passed.
  1752. ///
  1753. /// \param Lo - The classification for the parts of the type
  1754. /// residing in the low word of the containing object.
  1755. ///
  1756. /// \param Hi - The classification for the parts of the type
  1757. /// residing in the high word of the containing object.
  1758. ///
  1759. /// \param OffsetBase - The bit offset of this type in the
  1760. /// containing object. Some parameters are classified different
  1761. /// depending on whether they straddle an eightbyte boundary.
  1762. ///
  1763. /// \param isNamedArg - Whether the argument in question is a "named"
  1764. /// argument, as used in AMD64-ABI 3.5.7.
  1765. ///
  1766. /// If a word is unused its result will be NoClass; if a type should
  1767. /// be passed in Memory then at least the classification of \arg Lo
  1768. /// will be Memory.
  1769. ///
  1770. /// The \arg Lo class will be NoClass iff the argument is ignored.
  1771. ///
  1772. /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
  1773. /// also be ComplexX87.
  1774. void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
  1775. bool isNamedArg) const;
  1776. llvm::Type *GetByteVectorType(QualType Ty) const;
  1777. llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
  1778. unsigned IROffset, QualType SourceTy,
  1779. unsigned SourceOffset) const;
  1780. llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
  1781. unsigned IROffset, QualType SourceTy,
  1782. unsigned SourceOffset) const;
  1783. /// getIndirectResult - Give a source type \arg Ty, return a suitable result
  1784. /// such that the argument will be returned in memory.
  1785. ABIArgInfo getIndirectReturnResult(QualType Ty) const;
  1786. /// getIndirectResult - Give a source type \arg Ty, return a suitable result
  1787. /// such that the argument will be passed in memory.
  1788. ///
  1789. /// \param freeIntRegs - The number of free integer registers remaining
  1790. /// available.
  1791. ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
  1792. ABIArgInfo classifyReturnType(QualType RetTy) const;
  1793. ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs,
  1794. unsigned &neededInt, unsigned &neededSSE,
  1795. bool isNamedArg) const;
  1796. ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
  1797. unsigned &NeededSSE) const;
  1798. ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
  1799. unsigned &NeededSSE) const;
  1800. bool IsIllegalVectorType(QualType Ty) const;
  1801. /// The 0.98 ABI revision clarified a lot of ambiguities,
  1802. /// unfortunately in ways that were not always consistent with
  1803. /// certain previous compilers. In particular, platforms which
  1804. /// required strict binary compatibility with older versions of GCC
  1805. /// may need to exempt themselves.
  1806. bool honorsRevision0_98() const {
  1807. return !getTarget().getTriple().isOSDarwin();
  1808. }
  1809. /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to
  1810. /// classify it as INTEGER (for compatibility with older clang compilers).
  1811. bool classifyIntegerMMXAsSSE() const {
  1812. // Clang <= 3.8 did not do this.
  1813. if (getCodeGenOpts().getClangABICompat() <=
  1814. CodeGenOptions::ClangABI::Ver3_8)
  1815. return false;
  1816. const llvm::Triple &Triple = getTarget().getTriple();
  1817. if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4)
  1818. return false;
  1819. if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
  1820. return false;
  1821. return true;
  1822. }
  1823. X86AVXABILevel AVXLevel;
  1824. // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
  1825. // 64-bit hardware.
  1826. bool Has64BitPointers;
  1827. public:
  1828. X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
  1829. SwiftABIInfo(CGT), AVXLevel(AVXLevel),
  1830. Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
  1831. }
  1832. bool isPassedUsingAVXType(QualType type) const {
  1833. unsigned neededInt, neededSSE;
  1834. // The freeIntRegs argument doesn't matter here.
  1835. ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
  1836. /*isNamedArg*/true);
  1837. if (info.isDirect()) {
  1838. llvm::Type *ty = info.getCoerceToType();
  1839. if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
  1840. return (vectorTy->getBitWidth() > 128);
  1841. }
  1842. return false;
  1843. }
  1844. void computeInfo(CGFunctionInfo &FI) const override;
  1845. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  1846. QualType Ty) const override;
  1847. Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  1848. QualType Ty) const override;
  1849. bool has64BitPointers() const {
  1850. return Has64BitPointers;
  1851. }
  1852. bool shouldPassIndirectlyForSwift(CharUnits totalSize,
  1853. ArrayRef<llvm::Type*> scalars,
  1854. bool asReturnValue) const override {
  1855. return occupiesMoreThan(CGT, scalars, /*total*/ 4);
  1856. }
  1857. bool isSwiftErrorInRegister() const override {
  1858. return true;
  1859. }
  1860. };
  1861. /// WinX86_64ABIInfo - The Windows X86_64 ABI information.
  1862. class WinX86_64ABIInfo : public SwiftABIInfo {
  1863. public:
  1864. WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT)
  1865. : SwiftABIInfo(CGT),
  1866. IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
  1867. void computeInfo(CGFunctionInfo &FI) const override;
  1868. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  1869. QualType Ty) const override;
  1870. bool isHomogeneousAggregateBaseType(QualType Ty) const override {
  1871. // FIXME: Assumes vectorcall is in use.
  1872. return isX86VectorTypeForVectorCall(getContext(), Ty);
  1873. }
  1874. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  1875. uint64_t NumMembers) const override {
  1876. // FIXME: Assumes vectorcall is in use.
  1877. return isX86VectorCallAggregateSmallEnough(NumMembers);
  1878. }
  1879. bool shouldPassIndirectlyForSwift(CharUnits totalSize,
  1880. ArrayRef<llvm::Type *> scalars,
  1881. bool asReturnValue) const override {
  1882. return occupiesMoreThan(CGT, scalars, /*total*/ 4);
  1883. }
  1884. bool isSwiftErrorInRegister() const override {
  1885. return true;
  1886. }
  1887. private:
  1888. ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType,
  1889. bool IsVectorCall, bool IsRegCall) const;
  1890. ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
  1891. const ABIArgInfo &current) const;
  1892. void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs,
  1893. bool IsVectorCall, bool IsRegCall) const;
  1894. bool IsMingw64;
  1895. };
  1896. class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
  1897. public:
  1898. X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
  1899. : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {}
  1900. const X86_64ABIInfo &getABIInfo() const {
  1901. return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
  1902. }
  1903. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
  1904. return 7;
  1905. }
  1906. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  1907. llvm::Value *Address) const override {
  1908. llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
  1909. // 0-15 are the 16 integer registers.
  1910. // 16 is %rip.
  1911. AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
  1912. return false;
  1913. }
  1914. llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  1915. StringRef Constraint,
  1916. llvm::Type* Ty) const override {
  1917. return X86AdjustInlineAsmType(CGF, Constraint, Ty);
  1918. }
  1919. bool isNoProtoCallVariadic(const CallArgList &args,
  1920. const FunctionNoProtoType *fnType) const override {
  1921. // The default CC on x86-64 sets %al to the number of SSA
  1922. // registers used, and GCC sets this when calling an unprototyped
  1923. // function, so we override the default behavior. However, don't do
  1924. // that when AVX types are involved: the ABI explicitly states it is
  1925. // undefined, and it doesn't work in practice because of how the ABI
  1926. // defines varargs anyway.
  1927. if (fnType->getCallConv() == CC_C) {
  1928. bool HasAVXType = false;
  1929. for (CallArgList::const_iterator
  1930. it = args.begin(), ie = args.end(); it != ie; ++it) {
  1931. if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
  1932. HasAVXType = true;
  1933. break;
  1934. }
  1935. }
  1936. if (!HasAVXType)
  1937. return true;
  1938. }
  1939. return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
  1940. }
  1941. llvm::Constant *
  1942. getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
  1943. unsigned Sig = (0xeb << 0) | // jmp rel8
  1944. (0x06 << 8) | // .+0x08
  1945. ('v' << 16) |
  1946. ('2' << 24);
  1947. return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
  1948. }
  1949. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  1950. CodeGen::CodeGenModule &CGM,
  1951. ForDefinition_t IsForDefinition) const override {
  1952. if (!IsForDefinition)
  1953. return;
  1954. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  1955. if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
  1956. // Get the LLVM function.
  1957. auto *Fn = cast<llvm::Function>(GV);
  1958. // Now add the 'alignstack' attribute with a value of 16.
  1959. llvm::AttrBuilder B;
  1960. B.addStackAlignmentAttr(16);
  1961. Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
  1962. }
  1963. if (FD->hasAttr<AnyX86InterruptAttr>()) {
  1964. llvm::Function *Fn = cast<llvm::Function>(GV);
  1965. Fn->setCallingConv(llvm::CallingConv::X86_INTR);
  1966. }
  1967. }
  1968. }
  1969. };
  1970. class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo {
  1971. public:
  1972. PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
  1973. : X86_64TargetCodeGenInfo(CGT, AVXLevel) {}
  1974. void getDependentLibraryOption(llvm::StringRef Lib,
  1975. llvm::SmallString<24> &Opt) const override {
  1976. Opt = "\01";
  1977. // If the argument contains a space, enclose it in quotes.
  1978. if (Lib.find(" ") != StringRef::npos)
  1979. Opt += "\"" + Lib.str() + "\"";
  1980. else
  1981. Opt += Lib;
  1982. }
  1983. };
  1984. static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
  1985. // If the argument does not end in .lib, automatically add the suffix.
  1986. // If the argument contains a space, enclose it in quotes.
  1987. // This matches the behavior of MSVC.
  1988. bool Quote = (Lib.find(" ") != StringRef::npos);
  1989. std::string ArgStr = Quote ? "\"" : "";
  1990. ArgStr += Lib;
  1991. if (!Lib.endswith_lower(".lib"))
  1992. ArgStr += ".lib";
  1993. ArgStr += Quote ? "\"" : "";
  1994. return ArgStr;
  1995. }
  1996. class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
  1997. public:
  1998. WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
  1999. bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
  2000. unsigned NumRegisterParameters)
  2001. : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
  2002. Win32StructABI, NumRegisterParameters, false) {}
  2003. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2004. CodeGen::CodeGenModule &CGM,
  2005. ForDefinition_t IsForDefinition) const override;
  2006. void getDependentLibraryOption(llvm::StringRef Lib,
  2007. llvm::SmallString<24> &Opt) const override {
  2008. Opt = "/DEFAULTLIB:";
  2009. Opt += qualifyWindowsLibrary(Lib);
  2010. }
  2011. void getDetectMismatchOption(llvm::StringRef Name,
  2012. llvm::StringRef Value,
  2013. llvm::SmallString<32> &Opt) const override {
  2014. Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
  2015. }
  2016. };
  2017. static void addStackProbeSizeTargetAttribute(const Decl *D,
  2018. llvm::GlobalValue *GV,
  2019. CodeGen::CodeGenModule &CGM) {
  2020. if (D && isa<FunctionDecl>(D)) {
  2021. if (CGM.getCodeGenOpts().StackProbeSize != 4096) {
  2022. llvm::Function *Fn = cast<llvm::Function>(GV);
  2023. Fn->addFnAttr("stack-probe-size",
  2024. llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
  2025. }
  2026. }
  2027. }
  2028. void WinX86_32TargetCodeGenInfo::setTargetAttributes(
  2029. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM,
  2030. ForDefinition_t IsForDefinition) const {
  2031. X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition);
  2032. if (!IsForDefinition)
  2033. return;
  2034. addStackProbeSizeTargetAttribute(D, GV, CGM);
  2035. }
  2036. class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
  2037. public:
  2038. WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
  2039. X86AVXABILevel AVXLevel)
  2040. : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
  2041. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2042. CodeGen::CodeGenModule &CGM,
  2043. ForDefinition_t IsForDefinition) const override;
  2044. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
  2045. return 7;
  2046. }
  2047. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  2048. llvm::Value *Address) const override {
  2049. llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
  2050. // 0-15 are the 16 integer registers.
  2051. // 16 is %rip.
  2052. AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
  2053. return false;
  2054. }
  2055. void getDependentLibraryOption(llvm::StringRef Lib,
  2056. llvm::SmallString<24> &Opt) const override {
  2057. Opt = "/DEFAULTLIB:";
  2058. Opt += qualifyWindowsLibrary(Lib);
  2059. }
  2060. void getDetectMismatchOption(llvm::StringRef Name,
  2061. llvm::StringRef Value,
  2062. llvm::SmallString<32> &Opt) const override {
  2063. Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
  2064. }
  2065. };
  2066. void WinX86_64TargetCodeGenInfo::setTargetAttributes(
  2067. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM,
  2068. ForDefinition_t IsForDefinition) const {
  2069. TargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition);
  2070. if (!IsForDefinition)
  2071. return;
  2072. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  2073. if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
  2074. // Get the LLVM function.
  2075. auto *Fn = cast<llvm::Function>(GV);
  2076. // Now add the 'alignstack' attribute with a value of 16.
  2077. llvm::AttrBuilder B;
  2078. B.addStackAlignmentAttr(16);
  2079. Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
  2080. }
  2081. if (FD->hasAttr<AnyX86InterruptAttr>()) {
  2082. llvm::Function *Fn = cast<llvm::Function>(GV);
  2083. Fn->setCallingConv(llvm::CallingConv::X86_INTR);
  2084. }
  2085. }
  2086. addStackProbeSizeTargetAttribute(D, GV, CGM);
  2087. }
  2088. }
  2089. void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
  2090. Class &Hi) const {
  2091. // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
  2092. //
  2093. // (a) If one of the classes is Memory, the whole argument is passed in
  2094. // memory.
  2095. //
  2096. // (b) If X87UP is not preceded by X87, the whole argument is passed in
  2097. // memory.
  2098. //
  2099. // (c) If the size of the aggregate exceeds two eightbytes and the first
  2100. // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
  2101. // argument is passed in memory. NOTE: This is necessary to keep the
  2102. // ABI working for processors that don't support the __m256 type.
  2103. //
  2104. // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
  2105. //
  2106. // Some of these are enforced by the merging logic. Others can arise
  2107. // only with unions; for example:
  2108. // union { _Complex double; unsigned; }
  2109. //
  2110. // Note that clauses (b) and (c) were added in 0.98.
  2111. //
  2112. if (Hi == Memory)
  2113. Lo = Memory;
  2114. if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
  2115. Lo = Memory;
  2116. if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
  2117. Lo = Memory;
  2118. if (Hi == SSEUp && Lo != SSE)
  2119. Hi = SSE;
  2120. }
  2121. X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
  2122. // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
  2123. // classified recursively so that always two fields are
  2124. // considered. The resulting class is calculated according to
  2125. // the classes of the fields in the eightbyte:
  2126. //
  2127. // (a) If both classes are equal, this is the resulting class.
  2128. //
  2129. // (b) If one of the classes is NO_CLASS, the resulting class is
  2130. // the other class.
  2131. //
  2132. // (c) If one of the classes is MEMORY, the result is the MEMORY
  2133. // class.
  2134. //
  2135. // (d) If one of the classes is INTEGER, the result is the
  2136. // INTEGER.
  2137. //
  2138. // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
  2139. // MEMORY is used as class.
  2140. //
  2141. // (f) Otherwise class SSE is used.
  2142. // Accum should never be memory (we should have returned) or
  2143. // ComplexX87 (because this cannot be passed in a structure).
  2144. assert((Accum != Memory && Accum != ComplexX87) &&
  2145. "Invalid accumulated classification during merge.");
  2146. if (Accum == Field || Field == NoClass)
  2147. return Accum;
  2148. if (Field == Memory)
  2149. return Memory;
  2150. if (Accum == NoClass)
  2151. return Field;
  2152. if (Accum == Integer || Field == Integer)
  2153. return Integer;
  2154. if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
  2155. Accum == X87 || Accum == X87Up)
  2156. return Memory;
  2157. return SSE;
  2158. }
  2159. void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
  2160. Class &Lo, Class &Hi, bool isNamedArg) const {
  2161. // FIXME: This code can be simplified by introducing a simple value class for
  2162. // Class pairs with appropriate constructor methods for the various
  2163. // situations.
  2164. // FIXME: Some of the split computations are wrong; unaligned vectors
  2165. // shouldn't be passed in registers for example, so there is no chance they
  2166. // can straddle an eightbyte. Verify & simplify.
  2167. Lo = Hi = NoClass;
  2168. Class &Current = OffsetBase < 64 ? Lo : Hi;
  2169. Current = Memory;
  2170. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  2171. BuiltinType::Kind k = BT->getKind();
  2172. if (k == BuiltinType::Void) {
  2173. Current = NoClass;
  2174. } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
  2175. Lo = Integer;
  2176. Hi = Integer;
  2177. } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
  2178. Current = Integer;
  2179. } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
  2180. Current = SSE;
  2181. } else if (k == BuiltinType::LongDouble) {
  2182. const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
  2183. if (LDF == &llvm::APFloat::IEEEquad()) {
  2184. Lo = SSE;
  2185. Hi = SSEUp;
  2186. } else if (LDF == &llvm::APFloat::x87DoubleExtended()) {
  2187. Lo = X87;
  2188. Hi = X87Up;
  2189. } else if (LDF == &llvm::APFloat::IEEEdouble()) {
  2190. Current = SSE;
  2191. } else
  2192. llvm_unreachable("unexpected long double representation!");
  2193. }
  2194. // FIXME: _Decimal32 and _Decimal64 are SSE.
  2195. // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
  2196. return;
  2197. }
  2198. if (const EnumType *ET = Ty->getAs<EnumType>()) {
  2199. // Classify the underlying integer type.
  2200. classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
  2201. return;
  2202. }
  2203. if (Ty->hasPointerRepresentation()) {
  2204. Current = Integer;
  2205. return;
  2206. }
  2207. if (Ty->isMemberPointerType()) {
  2208. if (Ty->isMemberFunctionPointerType()) {
  2209. if (Has64BitPointers) {
  2210. // If Has64BitPointers, this is an {i64, i64}, so classify both
  2211. // Lo and Hi now.
  2212. Lo = Hi = Integer;
  2213. } else {
  2214. // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
  2215. // straddles an eightbyte boundary, Hi should be classified as well.
  2216. uint64_t EB_FuncPtr = (OffsetBase) / 64;
  2217. uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
  2218. if (EB_FuncPtr != EB_ThisAdj) {
  2219. Lo = Hi = Integer;
  2220. } else {
  2221. Current = Integer;
  2222. }
  2223. }
  2224. } else {
  2225. Current = Integer;
  2226. }
  2227. return;
  2228. }
  2229. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  2230. uint64_t Size = getContext().getTypeSize(VT);
  2231. if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
  2232. // gcc passes the following as integer:
  2233. // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
  2234. // 2 bytes - <2 x char>, <1 x short>
  2235. // 1 byte - <1 x char>
  2236. Current = Integer;
  2237. // If this type crosses an eightbyte boundary, it should be
  2238. // split.
  2239. uint64_t EB_Lo = (OffsetBase) / 64;
  2240. uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
  2241. if (EB_Lo != EB_Hi)
  2242. Hi = Lo;
  2243. } else if (Size == 64) {
  2244. QualType ElementType = VT->getElementType();
  2245. // gcc passes <1 x double> in memory. :(
  2246. if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
  2247. return;
  2248. // gcc passes <1 x long long> as SSE but clang used to unconditionally
  2249. // pass them as integer. For platforms where clang is the de facto
  2250. // platform compiler, we must continue to use integer.
  2251. if (!classifyIntegerMMXAsSSE() &&
  2252. (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
  2253. ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
  2254. ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
  2255. ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
  2256. Current = Integer;
  2257. else
  2258. Current = SSE;
  2259. // If this type crosses an eightbyte boundary, it should be
  2260. // split.
  2261. if (OffsetBase && OffsetBase != 64)
  2262. Hi = Lo;
  2263. } else if (Size == 128 ||
  2264. (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
  2265. // Arguments of 256-bits are split into four eightbyte chunks. The
  2266. // least significant one belongs to class SSE and all the others to class
  2267. // SSEUP. The original Lo and Hi design considers that types can't be
  2268. // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
  2269. // This design isn't correct for 256-bits, but since there're no cases
  2270. // where the upper parts would need to be inspected, avoid adding
  2271. // complexity and just consider Hi to match the 64-256 part.
  2272. //
  2273. // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
  2274. // registers if they are "named", i.e. not part of the "..." of a
  2275. // variadic function.
  2276. //
  2277. // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
  2278. // split into eight eightbyte chunks, one SSE and seven SSEUP.
  2279. Lo = SSE;
  2280. Hi = SSEUp;
  2281. }
  2282. return;
  2283. }
  2284. if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
  2285. QualType ET = getContext().getCanonicalType(CT->getElementType());
  2286. uint64_t Size = getContext().getTypeSize(Ty);
  2287. if (ET->isIntegralOrEnumerationType()) {
  2288. if (Size <= 64)
  2289. Current = Integer;
  2290. else if (Size <= 128)
  2291. Lo = Hi = Integer;
  2292. } else if (ET == getContext().FloatTy) {
  2293. Current = SSE;
  2294. } else if (ET == getContext().DoubleTy) {
  2295. Lo = Hi = SSE;
  2296. } else if (ET == getContext().LongDoubleTy) {
  2297. const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
  2298. if (LDF == &llvm::APFloat::IEEEquad())
  2299. Current = Memory;
  2300. else if (LDF == &llvm::APFloat::x87DoubleExtended())
  2301. Current = ComplexX87;
  2302. else if (LDF == &llvm::APFloat::IEEEdouble())
  2303. Lo = Hi = SSE;
  2304. else
  2305. llvm_unreachable("unexpected long double representation!");
  2306. }
  2307. // If this complex type crosses an eightbyte boundary then it
  2308. // should be split.
  2309. uint64_t EB_Real = (OffsetBase) / 64;
  2310. uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
  2311. if (Hi == NoClass && EB_Real != EB_Imag)
  2312. Hi = Lo;
  2313. return;
  2314. }
  2315. if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
  2316. // Arrays are treated like structures.
  2317. uint64_t Size = getContext().getTypeSize(Ty);
  2318. // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
  2319. // than eight eightbytes, ..., it has class MEMORY.
  2320. if (Size > 512)
  2321. return;
  2322. // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
  2323. // fields, it has class MEMORY.
  2324. //
  2325. // Only need to check alignment of array base.
  2326. if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
  2327. return;
  2328. // Otherwise implement simplified merge. We could be smarter about
  2329. // this, but it isn't worth it and would be harder to verify.
  2330. Current = NoClass;
  2331. uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
  2332. uint64_t ArraySize = AT->getSize().getZExtValue();
  2333. // The only case a 256-bit wide vector could be used is when the array
  2334. // contains a single 256-bit element. Since Lo and Hi logic isn't extended
  2335. // to work for sizes wider than 128, early check and fallback to memory.
  2336. //
  2337. if (Size > 128 &&
  2338. (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
  2339. return;
  2340. for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
  2341. Class FieldLo, FieldHi;
  2342. classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
  2343. Lo = merge(Lo, FieldLo);
  2344. Hi = merge(Hi, FieldHi);
  2345. if (Lo == Memory || Hi == Memory)
  2346. break;
  2347. }
  2348. postMerge(Size, Lo, Hi);
  2349. assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
  2350. return;
  2351. }
  2352. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  2353. uint64_t Size = getContext().getTypeSize(Ty);
  2354. // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
  2355. // than eight eightbytes, ..., it has class MEMORY.
  2356. if (Size > 512)
  2357. return;
  2358. // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
  2359. // copy constructor or a non-trivial destructor, it is passed by invisible
  2360. // reference.
  2361. if (getRecordArgABI(RT, getCXXABI()))
  2362. return;
  2363. const RecordDecl *RD = RT->getDecl();
  2364. // Assume variable sized types are passed in memory.
  2365. if (RD->hasFlexibleArrayMember())
  2366. return;
  2367. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  2368. // Reset Lo class, this will be recomputed.
  2369. Current = NoClass;
  2370. // If this is a C++ record, classify the bases first.
  2371. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  2372. for (const auto &I : CXXRD->bases()) {
  2373. assert(!I.isVirtual() && !I.getType()->isDependentType() &&
  2374. "Unexpected base class!");
  2375. const CXXRecordDecl *Base =
  2376. cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
  2377. // Classify this field.
  2378. //
  2379. // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
  2380. // single eightbyte, each is classified separately. Each eightbyte gets
  2381. // initialized to class NO_CLASS.
  2382. Class FieldLo, FieldHi;
  2383. uint64_t Offset =
  2384. OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
  2385. classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
  2386. Lo = merge(Lo, FieldLo);
  2387. Hi = merge(Hi, FieldHi);
  2388. if (Lo == Memory || Hi == Memory) {
  2389. postMerge(Size, Lo, Hi);
  2390. return;
  2391. }
  2392. }
  2393. }
  2394. // Classify the fields one at a time, merging the results.
  2395. unsigned idx = 0;
  2396. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  2397. i != e; ++i, ++idx) {
  2398. uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
  2399. bool BitField = i->isBitField();
  2400. // Ignore padding bit-fields.
  2401. if (BitField && i->isUnnamedBitfield())
  2402. continue;
  2403. // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
  2404. // four eightbytes, or it contains unaligned fields, it has class MEMORY.
  2405. //
  2406. // The only case a 256-bit wide vector could be used is when the struct
  2407. // contains a single 256-bit element. Since Lo and Hi logic isn't extended
  2408. // to work for sizes wider than 128, early check and fallback to memory.
  2409. //
  2410. if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) ||
  2411. Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
  2412. Lo = Memory;
  2413. postMerge(Size, Lo, Hi);
  2414. return;
  2415. }
  2416. // Note, skip this test for bit-fields, see below.
  2417. if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
  2418. Lo = Memory;
  2419. postMerge(Size, Lo, Hi);
  2420. return;
  2421. }
  2422. // Classify this field.
  2423. //
  2424. // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
  2425. // exceeds a single eightbyte, each is classified
  2426. // separately. Each eightbyte gets initialized to class
  2427. // NO_CLASS.
  2428. Class FieldLo, FieldHi;
  2429. // Bit-fields require special handling, they do not force the
  2430. // structure to be passed in memory even if unaligned, and
  2431. // therefore they can straddle an eightbyte.
  2432. if (BitField) {
  2433. assert(!i->isUnnamedBitfield());
  2434. uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
  2435. uint64_t Size = i->getBitWidthValue(getContext());
  2436. uint64_t EB_Lo = Offset / 64;
  2437. uint64_t EB_Hi = (Offset + Size - 1) / 64;
  2438. if (EB_Lo) {
  2439. assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
  2440. FieldLo = NoClass;
  2441. FieldHi = Integer;
  2442. } else {
  2443. FieldLo = Integer;
  2444. FieldHi = EB_Hi ? Integer : NoClass;
  2445. }
  2446. } else
  2447. classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
  2448. Lo = merge(Lo, FieldLo);
  2449. Hi = merge(Hi, FieldHi);
  2450. if (Lo == Memory || Hi == Memory)
  2451. break;
  2452. }
  2453. postMerge(Size, Lo, Hi);
  2454. }
  2455. }
  2456. ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
  2457. // If this is a scalar LLVM value then assume LLVM will pass it in the right
  2458. // place naturally.
  2459. if (!isAggregateTypeForABI(Ty)) {
  2460. // Treat an enum type as its underlying type.
  2461. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  2462. Ty = EnumTy->getDecl()->getIntegerType();
  2463. return (Ty->isPromotableIntegerType() ?
  2464. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  2465. }
  2466. return getNaturalAlignIndirect(Ty);
  2467. }
  2468. bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
  2469. if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
  2470. uint64_t Size = getContext().getTypeSize(VecTy);
  2471. unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
  2472. if (Size <= 64 || Size > LargestVector)
  2473. return true;
  2474. }
  2475. return false;
  2476. }
  2477. ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
  2478. unsigned freeIntRegs) const {
  2479. // If this is a scalar LLVM value then assume LLVM will pass it in the right
  2480. // place naturally.
  2481. //
  2482. // This assumption is optimistic, as there could be free registers available
  2483. // when we need to pass this argument in memory, and LLVM could try to pass
  2484. // the argument in the free register. This does not seem to happen currently,
  2485. // but this code would be much safer if we could mark the argument with
  2486. // 'onstack'. See PR12193.
  2487. if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
  2488. // Treat an enum type as its underlying type.
  2489. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  2490. Ty = EnumTy->getDecl()->getIntegerType();
  2491. return (Ty->isPromotableIntegerType() ?
  2492. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  2493. }
  2494. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  2495. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  2496. // Compute the byval alignment. We specify the alignment of the byval in all
  2497. // cases so that the mid-level optimizer knows the alignment of the byval.
  2498. unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
  2499. // Attempt to avoid passing indirect results using byval when possible. This
  2500. // is important for good codegen.
  2501. //
  2502. // We do this by coercing the value into a scalar type which the backend can
  2503. // handle naturally (i.e., without using byval).
  2504. //
  2505. // For simplicity, we currently only do this when we have exhausted all of the
  2506. // free integer registers. Doing this when there are free integer registers
  2507. // would require more care, as we would have to ensure that the coerced value
  2508. // did not claim the unused register. That would require either reording the
  2509. // arguments to the function (so that any subsequent inreg values came first),
  2510. // or only doing this optimization when there were no following arguments that
  2511. // might be inreg.
  2512. //
  2513. // We currently expect it to be rare (particularly in well written code) for
  2514. // arguments to be passed on the stack when there are still free integer
  2515. // registers available (this would typically imply large structs being passed
  2516. // by value), so this seems like a fair tradeoff for now.
  2517. //
  2518. // We can revisit this if the backend grows support for 'onstack' parameter
  2519. // attributes. See PR12193.
  2520. if (freeIntRegs == 0) {
  2521. uint64_t Size = getContext().getTypeSize(Ty);
  2522. // If this type fits in an eightbyte, coerce it into the matching integral
  2523. // type, which will end up on the stack (with alignment 8).
  2524. if (Align == 8 && Size <= 64)
  2525. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  2526. Size));
  2527. }
  2528. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
  2529. }
  2530. /// The ABI specifies that a value should be passed in a full vector XMM/YMM
  2531. /// register. Pick an LLVM IR type that will be passed as a vector register.
  2532. llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
  2533. // Wrapper structs/arrays that only contain vectors are passed just like
  2534. // vectors; strip them off if present.
  2535. if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
  2536. Ty = QualType(InnerTy, 0);
  2537. llvm::Type *IRType = CGT.ConvertType(Ty);
  2538. if (isa<llvm::VectorType>(IRType) ||
  2539. IRType->getTypeID() == llvm::Type::FP128TyID)
  2540. return IRType;
  2541. // We couldn't find the preferred IR vector type for 'Ty'.
  2542. uint64_t Size = getContext().getTypeSize(Ty);
  2543. assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
  2544. // Return a LLVM IR vector type based on the size of 'Ty'.
  2545. return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()),
  2546. Size / 64);
  2547. }
  2548. /// BitsContainNoUserData - Return true if the specified [start,end) bit range
  2549. /// is known to either be off the end of the specified type or being in
  2550. /// alignment padding. The user type specified is known to be at most 128 bits
  2551. /// in size, and have passed through X86_64ABIInfo::classify with a successful
  2552. /// classification that put one of the two halves in the INTEGER class.
  2553. ///
  2554. /// It is conservatively correct to return false.
  2555. static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
  2556. unsigned EndBit, ASTContext &Context) {
  2557. // If the bytes being queried are off the end of the type, there is no user
  2558. // data hiding here. This handles analysis of builtins, vectors and other
  2559. // types that don't contain interesting padding.
  2560. unsigned TySize = (unsigned)Context.getTypeSize(Ty);
  2561. if (TySize <= StartBit)
  2562. return true;
  2563. if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
  2564. unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
  2565. unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
  2566. // Check each element to see if the element overlaps with the queried range.
  2567. for (unsigned i = 0; i != NumElts; ++i) {
  2568. // If the element is after the span we care about, then we're done..
  2569. unsigned EltOffset = i*EltSize;
  2570. if (EltOffset >= EndBit) break;
  2571. unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
  2572. if (!BitsContainNoUserData(AT->getElementType(), EltStart,
  2573. EndBit-EltOffset, Context))
  2574. return false;
  2575. }
  2576. // If it overlaps no elements, then it is safe to process as padding.
  2577. return true;
  2578. }
  2579. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  2580. const RecordDecl *RD = RT->getDecl();
  2581. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  2582. // If this is a C++ record, check the bases first.
  2583. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  2584. for (const auto &I : CXXRD->bases()) {
  2585. assert(!I.isVirtual() && !I.getType()->isDependentType() &&
  2586. "Unexpected base class!");
  2587. const CXXRecordDecl *Base =
  2588. cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
  2589. // If the base is after the span we care about, ignore it.
  2590. unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
  2591. if (BaseOffset >= EndBit) continue;
  2592. unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
  2593. if (!BitsContainNoUserData(I.getType(), BaseStart,
  2594. EndBit-BaseOffset, Context))
  2595. return false;
  2596. }
  2597. }
  2598. // Verify that no field has data that overlaps the region of interest. Yes
  2599. // this could be sped up a lot by being smarter about queried fields,
  2600. // however we're only looking at structs up to 16 bytes, so we don't care
  2601. // much.
  2602. unsigned idx = 0;
  2603. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  2604. i != e; ++i, ++idx) {
  2605. unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
  2606. // If we found a field after the region we care about, then we're done.
  2607. if (FieldOffset >= EndBit) break;
  2608. unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
  2609. if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
  2610. Context))
  2611. return false;
  2612. }
  2613. // If nothing in this record overlapped the area of interest, then we're
  2614. // clean.
  2615. return true;
  2616. }
  2617. return false;
  2618. }
  2619. /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
  2620. /// float member at the specified offset. For example, {int,{float}} has a
  2621. /// float at offset 4. It is conservatively correct for this routine to return
  2622. /// false.
  2623. static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
  2624. const llvm::DataLayout &TD) {
  2625. // Base case if we find a float.
  2626. if (IROffset == 0 && IRType->isFloatTy())
  2627. return true;
  2628. // If this is a struct, recurse into the field at the specified offset.
  2629. if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
  2630. const llvm::StructLayout *SL = TD.getStructLayout(STy);
  2631. unsigned Elt = SL->getElementContainingOffset(IROffset);
  2632. IROffset -= SL->getElementOffset(Elt);
  2633. return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
  2634. }
  2635. // If this is an array, recurse into the field at the specified offset.
  2636. if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
  2637. llvm::Type *EltTy = ATy->getElementType();
  2638. unsigned EltSize = TD.getTypeAllocSize(EltTy);
  2639. IROffset -= IROffset/EltSize*EltSize;
  2640. return ContainsFloatAtOffset(EltTy, IROffset, TD);
  2641. }
  2642. return false;
  2643. }
  2644. /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
  2645. /// low 8 bytes of an XMM register, corresponding to the SSE class.
  2646. llvm::Type *X86_64ABIInfo::
  2647. GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
  2648. QualType SourceTy, unsigned SourceOffset) const {
  2649. // The only three choices we have are either double, <2 x float>, or float. We
  2650. // pass as float if the last 4 bytes is just padding. This happens for
  2651. // structs that contain 3 floats.
  2652. if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
  2653. SourceOffset*8+64, getContext()))
  2654. return llvm::Type::getFloatTy(getVMContext());
  2655. // We want to pass as <2 x float> if the LLVM IR type contains a float at
  2656. // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
  2657. // case.
  2658. if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
  2659. ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
  2660. return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
  2661. return llvm::Type::getDoubleTy(getVMContext());
  2662. }
  2663. /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
  2664. /// an 8-byte GPR. This means that we either have a scalar or we are talking
  2665. /// about the high or low part of an up-to-16-byte struct. This routine picks
  2666. /// the best LLVM IR type to represent this, which may be i64 or may be anything
  2667. /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
  2668. /// etc).
  2669. ///
  2670. /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
  2671. /// the source type. IROffset is an offset in bytes into the LLVM IR type that
  2672. /// the 8-byte value references. PrefType may be null.
  2673. ///
  2674. /// SourceTy is the source-level type for the entire argument. SourceOffset is
  2675. /// an offset into this that we're processing (which is always either 0 or 8).
  2676. ///
  2677. llvm::Type *X86_64ABIInfo::
  2678. GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
  2679. QualType SourceTy, unsigned SourceOffset) const {
  2680. // If we're dealing with an un-offset LLVM IR type, then it means that we're
  2681. // returning an 8-byte unit starting with it. See if we can safely use it.
  2682. if (IROffset == 0) {
  2683. // Pointers and int64's always fill the 8-byte unit.
  2684. if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
  2685. IRType->isIntegerTy(64))
  2686. return IRType;
  2687. // If we have a 1/2/4-byte integer, we can use it only if the rest of the
  2688. // goodness in the source type is just tail padding. This is allowed to
  2689. // kick in for struct {double,int} on the int, but not on
  2690. // struct{double,int,int} because we wouldn't return the second int. We
  2691. // have to do this analysis on the source type because we can't depend on
  2692. // unions being lowered a specific way etc.
  2693. if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
  2694. IRType->isIntegerTy(32) ||
  2695. (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
  2696. unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
  2697. cast<llvm::IntegerType>(IRType)->getBitWidth();
  2698. if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
  2699. SourceOffset*8+64, getContext()))
  2700. return IRType;
  2701. }
  2702. }
  2703. if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
  2704. // If this is a struct, recurse into the field at the specified offset.
  2705. const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
  2706. if (IROffset < SL->getSizeInBytes()) {
  2707. unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
  2708. IROffset -= SL->getElementOffset(FieldIdx);
  2709. return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
  2710. SourceTy, SourceOffset);
  2711. }
  2712. }
  2713. if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
  2714. llvm::Type *EltTy = ATy->getElementType();
  2715. unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
  2716. unsigned EltOffset = IROffset/EltSize*EltSize;
  2717. return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
  2718. SourceOffset);
  2719. }
  2720. // Okay, we don't have any better idea of what to pass, so we pass this in an
  2721. // integer register that isn't too big to fit the rest of the struct.
  2722. unsigned TySizeInBytes =
  2723. (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
  2724. assert(TySizeInBytes != SourceOffset && "Empty field?");
  2725. // It is always safe to classify this as an integer type up to i64 that
  2726. // isn't larger than the structure.
  2727. return llvm::IntegerType::get(getVMContext(),
  2728. std::min(TySizeInBytes-SourceOffset, 8U)*8);
  2729. }
  2730. /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
  2731. /// be used as elements of a two register pair to pass or return, return a
  2732. /// first class aggregate to represent them. For example, if the low part of
  2733. /// a by-value argument should be passed as i32* and the high part as float,
  2734. /// return {i32*, float}.
  2735. static llvm::Type *
  2736. GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
  2737. const llvm::DataLayout &TD) {
  2738. // In order to correctly satisfy the ABI, we need to the high part to start
  2739. // at offset 8. If the high and low parts we inferred are both 4-byte types
  2740. // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
  2741. // the second element at offset 8. Check for this:
  2742. unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
  2743. unsigned HiAlign = TD.getABITypeAlignment(Hi);
  2744. unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
  2745. assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
  2746. // To handle this, we have to increase the size of the low part so that the
  2747. // second element will start at an 8 byte offset. We can't increase the size
  2748. // of the second element because it might make us access off the end of the
  2749. // struct.
  2750. if (HiStart != 8) {
  2751. // There are usually two sorts of types the ABI generation code can produce
  2752. // for the low part of a pair that aren't 8 bytes in size: float or
  2753. // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and
  2754. // NaCl).
  2755. // Promote these to a larger type.
  2756. if (Lo->isFloatTy())
  2757. Lo = llvm::Type::getDoubleTy(Lo->getContext());
  2758. else {
  2759. assert((Lo->isIntegerTy() || Lo->isPointerTy())
  2760. && "Invalid/unknown lo type");
  2761. Lo = llvm::Type::getInt64Ty(Lo->getContext());
  2762. }
  2763. }
  2764. llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
  2765. // Verify that the second element is at an 8-byte offset.
  2766. assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
  2767. "Invalid x86-64 argument pair!");
  2768. return Result;
  2769. }
  2770. ABIArgInfo X86_64ABIInfo::
  2771. classifyReturnType(QualType RetTy) const {
  2772. // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
  2773. // classification algorithm.
  2774. X86_64ABIInfo::Class Lo, Hi;
  2775. classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
  2776. // Check some invariants.
  2777. assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
  2778. assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
  2779. llvm::Type *ResType = nullptr;
  2780. switch (Lo) {
  2781. case NoClass:
  2782. if (Hi == NoClass)
  2783. return ABIArgInfo::getIgnore();
  2784. // If the low part is just padding, it takes no register, leave ResType
  2785. // null.
  2786. assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
  2787. "Unknown missing lo part");
  2788. break;
  2789. case SSEUp:
  2790. case X87Up:
  2791. llvm_unreachable("Invalid classification for lo word.");
  2792. // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
  2793. // hidden argument.
  2794. case Memory:
  2795. return getIndirectReturnResult(RetTy);
  2796. // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
  2797. // available register of the sequence %rax, %rdx is used.
  2798. case Integer:
  2799. ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
  2800. // If we have a sign or zero extended integer, make sure to return Extend
  2801. // so that the parameter gets the right LLVM IR attributes.
  2802. if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
  2803. // Treat an enum type as its underlying type.
  2804. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  2805. RetTy = EnumTy->getDecl()->getIntegerType();
  2806. if (RetTy->isIntegralOrEnumerationType() &&
  2807. RetTy->isPromotableIntegerType())
  2808. return ABIArgInfo::getExtend();
  2809. }
  2810. break;
  2811. // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
  2812. // available SSE register of the sequence %xmm0, %xmm1 is used.
  2813. case SSE:
  2814. ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
  2815. break;
  2816. // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
  2817. // returned on the X87 stack in %st0 as 80-bit x87 number.
  2818. case X87:
  2819. ResType = llvm::Type::getX86_FP80Ty(getVMContext());
  2820. break;
  2821. // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
  2822. // part of the value is returned in %st0 and the imaginary part in
  2823. // %st1.
  2824. case ComplexX87:
  2825. assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
  2826. ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
  2827. llvm::Type::getX86_FP80Ty(getVMContext()));
  2828. break;
  2829. }
  2830. llvm::Type *HighPart = nullptr;
  2831. switch (Hi) {
  2832. // Memory was handled previously and X87 should
  2833. // never occur as a hi class.
  2834. case Memory:
  2835. case X87:
  2836. llvm_unreachable("Invalid classification for hi word.");
  2837. case ComplexX87: // Previously handled.
  2838. case NoClass:
  2839. break;
  2840. case Integer:
  2841. HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
  2842. if (Lo == NoClass) // Return HighPart at offset 8 in memory.
  2843. return ABIArgInfo::getDirect(HighPart, 8);
  2844. break;
  2845. case SSE:
  2846. HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
  2847. if (Lo == NoClass) // Return HighPart at offset 8 in memory.
  2848. return ABIArgInfo::getDirect(HighPart, 8);
  2849. break;
  2850. // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
  2851. // is passed in the next available eightbyte chunk if the last used
  2852. // vector register.
  2853. //
  2854. // SSEUP should always be preceded by SSE, just widen.
  2855. case SSEUp:
  2856. assert(Lo == SSE && "Unexpected SSEUp classification.");
  2857. ResType = GetByteVectorType(RetTy);
  2858. break;
  2859. // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
  2860. // returned together with the previous X87 value in %st0.
  2861. case X87Up:
  2862. // If X87Up is preceded by X87, we don't need to do
  2863. // anything. However, in some cases with unions it may not be
  2864. // preceded by X87. In such situations we follow gcc and pass the
  2865. // extra bits in an SSE reg.
  2866. if (Lo != X87) {
  2867. HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
  2868. if (Lo == NoClass) // Return HighPart at offset 8 in memory.
  2869. return ABIArgInfo::getDirect(HighPart, 8);
  2870. }
  2871. break;
  2872. }
  2873. // If a high part was specified, merge it together with the low part. It is
  2874. // known to pass in the high eightbyte of the result. We do this by forming a
  2875. // first class struct aggregate with the high and low part: {low, high}
  2876. if (HighPart)
  2877. ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
  2878. return ABIArgInfo::getDirect(ResType);
  2879. }
  2880. ABIArgInfo X86_64ABIInfo::classifyArgumentType(
  2881. QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
  2882. bool isNamedArg)
  2883. const
  2884. {
  2885. Ty = useFirstFieldIfTransparentUnion(Ty);
  2886. X86_64ABIInfo::Class Lo, Hi;
  2887. classify(Ty, 0, Lo, Hi, isNamedArg);
  2888. // Check some invariants.
  2889. // FIXME: Enforce these by construction.
  2890. assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
  2891. assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
  2892. neededInt = 0;
  2893. neededSSE = 0;
  2894. llvm::Type *ResType = nullptr;
  2895. switch (Lo) {
  2896. case NoClass:
  2897. if (Hi == NoClass)
  2898. return ABIArgInfo::getIgnore();
  2899. // If the low part is just padding, it takes no register, leave ResType
  2900. // null.
  2901. assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
  2902. "Unknown missing lo part");
  2903. break;
  2904. // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
  2905. // on the stack.
  2906. case Memory:
  2907. // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
  2908. // COMPLEX_X87, it is passed in memory.
  2909. case X87:
  2910. case ComplexX87:
  2911. if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
  2912. ++neededInt;
  2913. return getIndirectResult(Ty, freeIntRegs);
  2914. case SSEUp:
  2915. case X87Up:
  2916. llvm_unreachable("Invalid classification for lo word.");
  2917. // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
  2918. // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
  2919. // and %r9 is used.
  2920. case Integer:
  2921. ++neededInt;
  2922. // Pick an 8-byte type based on the preferred type.
  2923. ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
  2924. // If we have a sign or zero extended integer, make sure to return Extend
  2925. // so that the parameter gets the right LLVM IR attributes.
  2926. if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
  2927. // Treat an enum type as its underlying type.
  2928. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  2929. Ty = EnumTy->getDecl()->getIntegerType();
  2930. if (Ty->isIntegralOrEnumerationType() &&
  2931. Ty->isPromotableIntegerType())
  2932. return ABIArgInfo::getExtend();
  2933. }
  2934. break;
  2935. // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
  2936. // available SSE register is used, the registers are taken in the
  2937. // order from %xmm0 to %xmm7.
  2938. case SSE: {
  2939. llvm::Type *IRType = CGT.ConvertType(Ty);
  2940. ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
  2941. ++neededSSE;
  2942. break;
  2943. }
  2944. }
  2945. llvm::Type *HighPart = nullptr;
  2946. switch (Hi) {
  2947. // Memory was handled previously, ComplexX87 and X87 should
  2948. // never occur as hi classes, and X87Up must be preceded by X87,
  2949. // which is passed in memory.
  2950. case Memory:
  2951. case X87:
  2952. case ComplexX87:
  2953. llvm_unreachable("Invalid classification for hi word.");
  2954. case NoClass: break;
  2955. case Integer:
  2956. ++neededInt;
  2957. // Pick an 8-byte type based on the preferred type.
  2958. HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
  2959. if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
  2960. return ABIArgInfo::getDirect(HighPart, 8);
  2961. break;
  2962. // X87Up generally doesn't occur here (long double is passed in
  2963. // memory), except in situations involving unions.
  2964. case X87Up:
  2965. case SSE:
  2966. HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
  2967. if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
  2968. return ABIArgInfo::getDirect(HighPart, 8);
  2969. ++neededSSE;
  2970. break;
  2971. // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
  2972. // eightbyte is passed in the upper half of the last used SSE
  2973. // register. This only happens when 128-bit vectors are passed.
  2974. case SSEUp:
  2975. assert(Lo == SSE && "Unexpected SSEUp classification");
  2976. ResType = GetByteVectorType(Ty);
  2977. break;
  2978. }
  2979. // If a high part was specified, merge it together with the low part. It is
  2980. // known to pass in the high eightbyte of the result. We do this by forming a
  2981. // first class struct aggregate with the high and low part: {low, high}
  2982. if (HighPart)
  2983. ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
  2984. return ABIArgInfo::getDirect(ResType);
  2985. }
  2986. ABIArgInfo
  2987. X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
  2988. unsigned &NeededSSE) const {
  2989. auto RT = Ty->getAs<RecordType>();
  2990. assert(RT && "classifyRegCallStructType only valid with struct types");
  2991. if (RT->getDecl()->hasFlexibleArrayMember())
  2992. return getIndirectReturnResult(Ty);
  2993. // Sum up bases
  2994. if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
  2995. if (CXXRD->isDynamicClass()) {
  2996. NeededInt = NeededSSE = 0;
  2997. return getIndirectReturnResult(Ty);
  2998. }
  2999. for (const auto &I : CXXRD->bases())
  3000. if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE)
  3001. .isIndirect()) {
  3002. NeededInt = NeededSSE = 0;
  3003. return getIndirectReturnResult(Ty);
  3004. }
  3005. }
  3006. // Sum up members
  3007. for (const auto *FD : RT->getDecl()->fields()) {
  3008. if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) {
  3009. if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE)
  3010. .isIndirect()) {
  3011. NeededInt = NeededSSE = 0;
  3012. return getIndirectReturnResult(Ty);
  3013. }
  3014. } else {
  3015. unsigned LocalNeededInt, LocalNeededSSE;
  3016. if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt,
  3017. LocalNeededSSE, true)
  3018. .isIndirect()) {
  3019. NeededInt = NeededSSE = 0;
  3020. return getIndirectReturnResult(Ty);
  3021. }
  3022. NeededInt += LocalNeededInt;
  3023. NeededSSE += LocalNeededSSE;
  3024. }
  3025. }
  3026. return ABIArgInfo::getDirect();
  3027. }
  3028. ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty,
  3029. unsigned &NeededInt,
  3030. unsigned &NeededSSE) const {
  3031. NeededInt = 0;
  3032. NeededSSE = 0;
  3033. return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE);
  3034. }
  3035. void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  3036. bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall;
  3037. // Keep track of the number of assigned registers.
  3038. unsigned FreeIntRegs = IsRegCall ? 11 : 6;
  3039. unsigned FreeSSERegs = IsRegCall ? 16 : 8;
  3040. unsigned NeededInt, NeededSSE;
  3041. if (!getCXXABI().classifyReturnType(FI)) {
  3042. if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
  3043. !FI.getReturnType()->getTypePtr()->isUnionType()) {
  3044. FI.getReturnInfo() =
  3045. classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE);
  3046. if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
  3047. FreeIntRegs -= NeededInt;
  3048. FreeSSERegs -= NeededSSE;
  3049. } else {
  3050. FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
  3051. }
  3052. } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) {
  3053. // Complex Long Double Type is passed in Memory when Regcall
  3054. // calling convention is used.
  3055. const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>();
  3056. if (getContext().getCanonicalType(CT->getElementType()) ==
  3057. getContext().LongDoubleTy)
  3058. FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
  3059. } else
  3060. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  3061. }
  3062. // If the return value is indirect, then the hidden argument is consuming one
  3063. // integer register.
  3064. if (FI.getReturnInfo().isIndirect())
  3065. --FreeIntRegs;
  3066. // The chain argument effectively gives us another free register.
  3067. if (FI.isChainCall())
  3068. ++FreeIntRegs;
  3069. unsigned NumRequiredArgs = FI.getNumRequiredArgs();
  3070. // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
  3071. // get assigned (in left-to-right order) for passing as follows...
  3072. unsigned ArgNo = 0;
  3073. for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
  3074. it != ie; ++it, ++ArgNo) {
  3075. bool IsNamedArg = ArgNo < NumRequiredArgs;
  3076. if (IsRegCall && it->type->isStructureOrClassType())
  3077. it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE);
  3078. else
  3079. it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
  3080. NeededSSE, IsNamedArg);
  3081. // AMD64-ABI 3.2.3p3: If there are no registers available for any
  3082. // eightbyte of an argument, the whole argument is passed on the
  3083. // stack. If registers have already been assigned for some
  3084. // eightbytes of such an argument, the assignments get reverted.
  3085. if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
  3086. FreeIntRegs -= NeededInt;
  3087. FreeSSERegs -= NeededSSE;
  3088. } else {
  3089. it->info = getIndirectResult(it->type, FreeIntRegs);
  3090. }
  3091. }
  3092. }
  3093. static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
  3094. Address VAListAddr, QualType Ty) {
  3095. Address overflow_arg_area_p = CGF.Builder.CreateStructGEP(
  3096. VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p");
  3097. llvm::Value *overflow_arg_area =
  3098. CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
  3099. // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
  3100. // byte boundary if alignment needed by type exceeds 8 byte boundary.
  3101. // It isn't stated explicitly in the standard, but in practice we use
  3102. // alignment greater than 16 where necessary.
  3103. CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
  3104. if (Align > CharUnits::fromQuantity(8)) {
  3105. overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
  3106. Align);
  3107. }
  3108. // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
  3109. llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
  3110. llvm::Value *Res =
  3111. CGF.Builder.CreateBitCast(overflow_arg_area,
  3112. llvm::PointerType::getUnqual(LTy));
  3113. // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
  3114. // l->overflow_arg_area + sizeof(type).
  3115. // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
  3116. // an 8 byte boundary.
  3117. uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
  3118. llvm::Value *Offset =
  3119. llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
  3120. overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
  3121. "overflow_arg_area.next");
  3122. CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
  3123. // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
  3124. return Address(Res, Align);
  3125. }
  3126. Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3127. QualType Ty) const {
  3128. // Assume that va_list type is correct; should be pointer to LLVM type:
  3129. // struct {
  3130. // i32 gp_offset;
  3131. // i32 fp_offset;
  3132. // i8* overflow_arg_area;
  3133. // i8* reg_save_area;
  3134. // };
  3135. unsigned neededInt, neededSSE;
  3136. Ty = getContext().getCanonicalType(Ty);
  3137. ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
  3138. /*isNamedArg*/false);
  3139. // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
  3140. // in the registers. If not go to step 7.
  3141. if (!neededInt && !neededSSE)
  3142. return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
  3143. // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
  3144. // general purpose registers needed to pass type and num_fp to hold
  3145. // the number of floating point registers needed.
  3146. // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
  3147. // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
  3148. // l->fp_offset > 304 - num_fp * 16 go to step 7.
  3149. //
  3150. // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
  3151. // register save space).
  3152. llvm::Value *InRegs = nullptr;
  3153. Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
  3154. llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
  3155. if (neededInt) {
  3156. gp_offset_p =
  3157. CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(),
  3158. "gp_offset_p");
  3159. gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
  3160. InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
  3161. InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
  3162. }
  3163. if (neededSSE) {
  3164. fp_offset_p =
  3165. CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4),
  3166. "fp_offset_p");
  3167. fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
  3168. llvm::Value *FitsInFP =
  3169. llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
  3170. FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
  3171. InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
  3172. }
  3173. llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
  3174. llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
  3175. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
  3176. CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
  3177. // Emit code to load the value if it was passed in registers.
  3178. CGF.EmitBlock(InRegBlock);
  3179. // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
  3180. // an offset of l->gp_offset and/or l->fp_offset. This may require
  3181. // copying to a temporary location in case the parameter is passed
  3182. // in different register classes or requires an alignment greater
  3183. // than 8 for general purpose registers and 16 for XMM registers.
  3184. //
  3185. // FIXME: This really results in shameful code when we end up needing to
  3186. // collect arguments from different places; often what should result in a
  3187. // simple assembling of a structure from scattered addresses has many more
  3188. // loads than necessary. Can we clean this up?
  3189. llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
  3190. llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
  3191. CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)),
  3192. "reg_save_area");
  3193. Address RegAddr = Address::invalid();
  3194. if (neededInt && neededSSE) {
  3195. // FIXME: Cleanup.
  3196. assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
  3197. llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
  3198. Address Tmp = CGF.CreateMemTemp(Ty);
  3199. Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
  3200. assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
  3201. llvm::Type *TyLo = ST->getElementType(0);
  3202. llvm::Type *TyHi = ST->getElementType(1);
  3203. assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
  3204. "Unexpected ABI info for mixed regs");
  3205. llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
  3206. llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
  3207. llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
  3208. llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
  3209. llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
  3210. llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
  3211. // Copy the first element.
  3212. // FIXME: Our choice of alignment here and below is probably pessimistic.
  3213. llvm::Value *V = CGF.Builder.CreateAlignedLoad(
  3214. TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
  3215. CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo)));
  3216. CGF.Builder.CreateStore(V,
  3217. CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
  3218. // Copy the second element.
  3219. V = CGF.Builder.CreateAlignedLoad(
  3220. TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
  3221. CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi)));
  3222. CharUnits Offset = CharUnits::fromQuantity(
  3223. getDataLayout().getStructLayout(ST)->getElementOffset(1));
  3224. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset));
  3225. RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
  3226. } else if (neededInt) {
  3227. RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
  3228. CharUnits::fromQuantity(8));
  3229. RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
  3230. // Copy to a temporary if necessary to ensure the appropriate alignment.
  3231. std::pair<CharUnits, CharUnits> SizeAlign =
  3232. getContext().getTypeInfoInChars(Ty);
  3233. uint64_t TySize = SizeAlign.first.getQuantity();
  3234. CharUnits TyAlign = SizeAlign.second;
  3235. // Copy into a temporary if the type is more aligned than the
  3236. // register save area.
  3237. if (TyAlign.getQuantity() > 8) {
  3238. Address Tmp = CGF.CreateMemTemp(Ty);
  3239. CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
  3240. RegAddr = Tmp;
  3241. }
  3242. } else if (neededSSE == 1) {
  3243. RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
  3244. CharUnits::fromQuantity(16));
  3245. RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
  3246. } else {
  3247. assert(neededSSE == 2 && "Invalid number of needed registers!");
  3248. // SSE registers are spaced 16 bytes apart in the register save
  3249. // area, we need to collect the two eightbytes together.
  3250. // The ABI isn't explicit about this, but it seems reasonable
  3251. // to assume that the slots are 16-byte aligned, since the stack is
  3252. // naturally 16-byte aligned and the prologue is expected to store
  3253. // all the SSE registers to the RSA.
  3254. Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
  3255. CharUnits::fromQuantity(16));
  3256. Address RegAddrHi =
  3257. CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
  3258. CharUnits::fromQuantity(16));
  3259. llvm::Type *DoubleTy = CGF.DoubleTy;
  3260. llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy);
  3261. llvm::Value *V;
  3262. Address Tmp = CGF.CreateMemTemp(Ty);
  3263. Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
  3264. V = CGF.Builder.CreateLoad(
  3265. CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy));
  3266. CGF.Builder.CreateStore(V,
  3267. CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
  3268. V = CGF.Builder.CreateLoad(
  3269. CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy));
  3270. CGF.Builder.CreateStore(V,
  3271. CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8)));
  3272. RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
  3273. }
  3274. // AMD64-ABI 3.5.7p5: Step 5. Set:
  3275. // l->gp_offset = l->gp_offset + num_gp * 8
  3276. // l->fp_offset = l->fp_offset + num_fp * 16.
  3277. if (neededInt) {
  3278. llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
  3279. CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
  3280. gp_offset_p);
  3281. }
  3282. if (neededSSE) {
  3283. llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
  3284. CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
  3285. fp_offset_p);
  3286. }
  3287. CGF.EmitBranch(ContBlock);
  3288. // Emit code to load the value if it was passed in memory.
  3289. CGF.EmitBlock(InMemBlock);
  3290. Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
  3291. // Return the appropriate result.
  3292. CGF.EmitBlock(ContBlock);
  3293. Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
  3294. "vaarg.addr");
  3295. return ResAddr;
  3296. }
  3297. Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3298. QualType Ty) const {
  3299. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
  3300. CGF.getContext().getTypeInfoInChars(Ty),
  3301. CharUnits::fromQuantity(8),
  3302. /*allowHigherAlign*/ false);
  3303. }
  3304. ABIArgInfo
  3305. WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
  3306. const ABIArgInfo &current) const {
  3307. // Assumes vectorCall calling convention.
  3308. const Type *Base = nullptr;
  3309. uint64_t NumElts = 0;
  3310. if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
  3311. isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
  3312. FreeSSERegs -= NumElts;
  3313. return getDirectX86Hva();
  3314. }
  3315. return current;
  3316. }
  3317. ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
  3318. bool IsReturnType, bool IsVectorCall,
  3319. bool IsRegCall) const {
  3320. if (Ty->isVoidType())
  3321. return ABIArgInfo::getIgnore();
  3322. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  3323. Ty = EnumTy->getDecl()->getIntegerType();
  3324. TypeInfo Info = getContext().getTypeInfo(Ty);
  3325. uint64_t Width = Info.Width;
  3326. CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
  3327. const RecordType *RT = Ty->getAs<RecordType>();
  3328. if (RT) {
  3329. if (!IsReturnType) {
  3330. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
  3331. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  3332. }
  3333. if (RT->getDecl()->hasFlexibleArrayMember())
  3334. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  3335. }
  3336. const Type *Base = nullptr;
  3337. uint64_t NumElts = 0;
  3338. // vectorcall adds the concept of a homogenous vector aggregate, similar to
  3339. // other targets.
  3340. if ((IsVectorCall || IsRegCall) &&
  3341. isHomogeneousAggregate(Ty, Base, NumElts)) {
  3342. if (IsRegCall) {
  3343. if (FreeSSERegs >= NumElts) {
  3344. FreeSSERegs -= NumElts;
  3345. if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
  3346. return ABIArgInfo::getDirect();
  3347. return ABIArgInfo::getExpand();
  3348. }
  3349. return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
  3350. } else if (IsVectorCall) {
  3351. if (FreeSSERegs >= NumElts &&
  3352. (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
  3353. FreeSSERegs -= NumElts;
  3354. return ABIArgInfo::getDirect();
  3355. } else if (IsReturnType) {
  3356. return ABIArgInfo::getExpand();
  3357. } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
  3358. // HVAs are delayed and reclassified in the 2nd step.
  3359. return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
  3360. }
  3361. }
  3362. }
  3363. if (Ty->isMemberPointerType()) {
  3364. // If the member pointer is represented by an LLVM int or ptr, pass it
  3365. // directly.
  3366. llvm::Type *LLTy = CGT.ConvertType(Ty);
  3367. if (LLTy->isPointerTy() || LLTy->isIntegerTy())
  3368. return ABIArgInfo::getDirect();
  3369. }
  3370. if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
  3371. // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
  3372. // not 1, 2, 4, or 8 bytes, must be passed by reference."
  3373. if (Width > 64 || !llvm::isPowerOf2_64(Width))
  3374. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  3375. // Otherwise, coerce it to a small integer.
  3376. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
  3377. }
  3378. // Bool type is always extended to the ABI, other builtin types are not
  3379. // extended.
  3380. const BuiltinType *BT = Ty->getAs<BuiltinType>();
  3381. if (BT && BT->getKind() == BuiltinType::Bool)
  3382. return ABIArgInfo::getExtend();
  3383. // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It
  3384. // passes them indirectly through memory.
  3385. if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) {
  3386. const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
  3387. if (LDF == &llvm::APFloat::x87DoubleExtended())
  3388. return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
  3389. }
  3390. return ABIArgInfo::getDirect();
  3391. }
  3392. void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI,
  3393. unsigned FreeSSERegs,
  3394. bool IsVectorCall,
  3395. bool IsRegCall) const {
  3396. unsigned Count = 0;
  3397. for (auto &I : FI.arguments()) {
  3398. // Vectorcall in x64 only permits the first 6 arguments to be passed
  3399. // as XMM/YMM registers.
  3400. if (Count < VectorcallMaxParamNumAsReg)
  3401. I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
  3402. else {
  3403. // Since these cannot be passed in registers, pretend no registers
  3404. // are left.
  3405. unsigned ZeroSSERegsAvail = 0;
  3406. I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false,
  3407. IsVectorCall, IsRegCall);
  3408. }
  3409. ++Count;
  3410. }
  3411. for (auto &I : FI.arguments()) {
  3412. I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info);
  3413. }
  3414. }
  3415. void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  3416. bool IsVectorCall =
  3417. FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall;
  3418. bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall;
  3419. unsigned FreeSSERegs = 0;
  3420. if (IsVectorCall) {
  3421. // We can use up to 4 SSE return registers with vectorcall.
  3422. FreeSSERegs = 4;
  3423. } else if (IsRegCall) {
  3424. // RegCall gives us 16 SSE registers.
  3425. FreeSSERegs = 16;
  3426. }
  3427. if (!getCXXABI().classifyReturnType(FI))
  3428. FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
  3429. IsVectorCall, IsRegCall);
  3430. if (IsVectorCall) {
  3431. // We can use up to 6 SSE register parameters with vectorcall.
  3432. FreeSSERegs = 6;
  3433. } else if (IsRegCall) {
  3434. // RegCall gives us 16 SSE registers, we can reuse the return registers.
  3435. FreeSSERegs = 16;
  3436. }
  3437. if (IsVectorCall) {
  3438. computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall);
  3439. } else {
  3440. for (auto &I : FI.arguments())
  3441. I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
  3442. }
  3443. }
  3444. Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3445. QualType Ty) const {
  3446. bool IsIndirect = false;
  3447. // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
  3448. // not 1, 2, 4, or 8 bytes, must be passed by reference."
  3449. if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) {
  3450. uint64_t Width = getContext().getTypeSize(Ty);
  3451. IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
  3452. }
  3453. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
  3454. CGF.getContext().getTypeInfoInChars(Ty),
  3455. CharUnits::fromQuantity(8),
  3456. /*allowHigherAlign*/ false);
  3457. }
  3458. // PowerPC-32
  3459. namespace {
  3460. /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
  3461. class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
  3462. bool IsSoftFloatABI;
  3463. CharUnits getParamTypeAlignment(QualType Ty) const;
  3464. public:
  3465. PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI)
  3466. : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {}
  3467. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3468. QualType Ty) const override;
  3469. };
  3470. class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
  3471. public:
  3472. PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI)
  3473. : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {}
  3474. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  3475. // This is recovered from gcc output.
  3476. return 1; // r1 is the dedicated stack pointer
  3477. }
  3478. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  3479. llvm::Value *Address) const override;
  3480. };
  3481. }
  3482. CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
  3483. // Complex types are passed just like their elements
  3484. if (const ComplexType *CTy = Ty->getAs<ComplexType>())
  3485. Ty = CTy->getElementType();
  3486. if (Ty->isVectorType())
  3487. return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16
  3488. : 4);
  3489. // For single-element float/vector structs, we consider the whole type
  3490. // to have the same alignment requirements as its single element.
  3491. const Type *AlignTy = nullptr;
  3492. if (const Type *EltType = isSingleElementStruct(Ty, getContext())) {
  3493. const BuiltinType *BT = EltType->getAs<BuiltinType>();
  3494. if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
  3495. (BT && BT->isFloatingPoint()))
  3496. AlignTy = EltType;
  3497. }
  3498. if (AlignTy)
  3499. return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4);
  3500. return CharUnits::fromQuantity(4);
  3501. }
  3502. // TODO: this implementation is now likely redundant with
  3503. // DefaultABIInfo::EmitVAArg.
  3504. Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
  3505. QualType Ty) const {
  3506. if (getTarget().getTriple().isOSDarwin()) {
  3507. auto TI = getContext().getTypeInfoInChars(Ty);
  3508. TI.second = getParamTypeAlignment(Ty);
  3509. CharUnits SlotSize = CharUnits::fromQuantity(4);
  3510. return emitVoidPtrVAArg(CGF, VAList, Ty,
  3511. classifyArgumentType(Ty).isIndirect(), TI, SlotSize,
  3512. /*AllowHigherAlign=*/true);
  3513. }
  3514. const unsigned OverflowLimit = 8;
  3515. if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
  3516. // TODO: Implement this. For now ignore.
  3517. (void)CTy;
  3518. return Address::invalid(); // FIXME?
  3519. }
  3520. // struct __va_list_tag {
  3521. // unsigned char gpr;
  3522. // unsigned char fpr;
  3523. // unsigned short reserved;
  3524. // void *overflow_arg_area;
  3525. // void *reg_save_area;
  3526. // };
  3527. bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
  3528. bool isInt =
  3529. Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
  3530. bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
  3531. // All aggregates are passed indirectly? That doesn't seem consistent
  3532. // with the argument-lowering code.
  3533. bool isIndirect = Ty->isAggregateType();
  3534. CGBuilderTy &Builder = CGF.Builder;
  3535. // The calling convention either uses 1-2 GPRs or 1 FPR.
  3536. Address NumRegsAddr = Address::invalid();
  3537. if (isInt || IsSoftFloatABI) {
  3538. NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr");
  3539. } else {
  3540. NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr");
  3541. }
  3542. llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
  3543. // "Align" the register count when TY is i64.
  3544. if (isI64 || (isF64 && IsSoftFloatABI)) {
  3545. NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
  3546. NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
  3547. }
  3548. llvm::Value *CC =
  3549. Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
  3550. llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
  3551. llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
  3552. llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
  3553. Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
  3554. llvm::Type *DirectTy = CGF.ConvertType(Ty);
  3555. if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
  3556. // Case 1: consume registers.
  3557. Address RegAddr = Address::invalid();
  3558. {
  3559. CGF.EmitBlock(UsingRegs);
  3560. Address RegSaveAreaPtr =
  3561. Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8));
  3562. RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
  3563. CharUnits::fromQuantity(8));
  3564. assert(RegAddr.getElementType() == CGF.Int8Ty);
  3565. // Floating-point registers start after the general-purpose registers.
  3566. if (!(isInt || IsSoftFloatABI)) {
  3567. RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
  3568. CharUnits::fromQuantity(32));
  3569. }
  3570. // Get the address of the saved value by scaling the number of
  3571. // registers we've used by the number of
  3572. CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
  3573. llvm::Value *RegOffset =
  3574. Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
  3575. RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
  3576. RegAddr.getPointer(), RegOffset),
  3577. RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
  3578. RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
  3579. // Increase the used-register count.
  3580. NumRegs =
  3581. Builder.CreateAdd(NumRegs,
  3582. Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
  3583. Builder.CreateStore(NumRegs, NumRegsAddr);
  3584. CGF.EmitBranch(Cont);
  3585. }
  3586. // Case 2: consume space in the overflow area.
  3587. Address MemAddr = Address::invalid();
  3588. {
  3589. CGF.EmitBlock(UsingOverflow);
  3590. Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
  3591. // Everything in the overflow area is rounded up to a size of at least 4.
  3592. CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
  3593. CharUnits Size;
  3594. if (!isIndirect) {
  3595. auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
  3596. Size = TypeInfo.first.alignTo(OverflowAreaAlign);
  3597. } else {
  3598. Size = CGF.getPointerSize();
  3599. }
  3600. Address OverflowAreaAddr =
  3601. Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4));
  3602. Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
  3603. OverflowAreaAlign);
  3604. // Round up address of argument to alignment
  3605. CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
  3606. if (Align > OverflowAreaAlign) {
  3607. llvm::Value *Ptr = OverflowArea.getPointer();
  3608. OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
  3609. Align);
  3610. }
  3611. MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
  3612. // Increase the overflow area.
  3613. OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
  3614. Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
  3615. CGF.EmitBranch(Cont);
  3616. }
  3617. CGF.EmitBlock(Cont);
  3618. // Merge the cases with a phi.
  3619. Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
  3620. "vaarg.addr");
  3621. // Load the pointer if the argument was passed indirectly.
  3622. if (isIndirect) {
  3623. Result = Address(Builder.CreateLoad(Result, "aggr"),
  3624. getContext().getTypeAlignInChars(Ty));
  3625. }
  3626. return Result;
  3627. }
  3628. bool
  3629. PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  3630. llvm::Value *Address) const {
  3631. // This is calculated from the LLVM and GCC tables and verified
  3632. // against gcc output. AFAIK all ABIs use the same encoding.
  3633. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  3634. llvm::IntegerType *i8 = CGF.Int8Ty;
  3635. llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
  3636. llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
  3637. llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
  3638. // 0-31: r0-31, the 4-byte general-purpose registers
  3639. AssignToArrayRange(Builder, Address, Four8, 0, 31);
  3640. // 32-63: fp0-31, the 8-byte floating-point registers
  3641. AssignToArrayRange(Builder, Address, Eight8, 32, 63);
  3642. // 64-76 are various 4-byte special-purpose registers:
  3643. // 64: mq
  3644. // 65: lr
  3645. // 66: ctr
  3646. // 67: ap
  3647. // 68-75 cr0-7
  3648. // 76: xer
  3649. AssignToArrayRange(Builder, Address, Four8, 64, 76);
  3650. // 77-108: v0-31, the 16-byte vector registers
  3651. AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
  3652. // 109: vrsave
  3653. // 110: vscr
  3654. // 111: spe_acc
  3655. // 112: spefscr
  3656. // 113: sfp
  3657. AssignToArrayRange(Builder, Address, Four8, 109, 113);
  3658. return false;
  3659. }
  3660. // PowerPC-64
  3661. namespace {
  3662. /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
  3663. class PPC64_SVR4_ABIInfo : public ABIInfo {
  3664. public:
  3665. enum ABIKind {
  3666. ELFv1 = 0,
  3667. ELFv2
  3668. };
  3669. private:
  3670. static const unsigned GPRBits = 64;
  3671. ABIKind Kind;
  3672. bool HasQPX;
  3673. bool IsSoftFloatABI;
  3674. // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
  3675. // will be passed in a QPX register.
  3676. bool IsQPXVectorTy(const Type *Ty) const {
  3677. if (!HasQPX)
  3678. return false;
  3679. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  3680. unsigned NumElements = VT->getNumElements();
  3681. if (NumElements == 1)
  3682. return false;
  3683. if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
  3684. if (getContext().getTypeSize(Ty) <= 256)
  3685. return true;
  3686. } else if (VT->getElementType()->
  3687. isSpecificBuiltinType(BuiltinType::Float)) {
  3688. if (getContext().getTypeSize(Ty) <= 128)
  3689. return true;
  3690. }
  3691. }
  3692. return false;
  3693. }
  3694. bool IsQPXVectorTy(QualType Ty) const {
  3695. return IsQPXVectorTy(Ty.getTypePtr());
  3696. }
  3697. public:
  3698. PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX,
  3699. bool SoftFloatABI)
  3700. : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX),
  3701. IsSoftFloatABI(SoftFloatABI) {}
  3702. bool isPromotableTypeForABI(QualType Ty) const;
  3703. CharUnits getParamTypeAlignment(QualType Ty) const;
  3704. ABIArgInfo classifyReturnType(QualType RetTy) const;
  3705. ABIArgInfo classifyArgumentType(QualType Ty) const;
  3706. bool isHomogeneousAggregateBaseType(QualType Ty) const override;
  3707. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  3708. uint64_t Members) const override;
  3709. // TODO: We can add more logic to computeInfo to improve performance.
  3710. // Example: For aggregate arguments that fit in a register, we could
  3711. // use getDirectInReg (as is done below for structs containing a single
  3712. // floating-point value) to avoid pushing them to memory on function
  3713. // entry. This would require changing the logic in PPCISelLowering
  3714. // when lowering the parameters in the caller and args in the callee.
  3715. void computeInfo(CGFunctionInfo &FI) const override {
  3716. if (!getCXXABI().classifyReturnType(FI))
  3717. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  3718. for (auto &I : FI.arguments()) {
  3719. // We rely on the default argument classification for the most part.
  3720. // One exception: An aggregate containing a single floating-point
  3721. // or vector item must be passed in a register if one is available.
  3722. const Type *T = isSingleElementStruct(I.type, getContext());
  3723. if (T) {
  3724. const BuiltinType *BT = T->getAs<BuiltinType>();
  3725. if (IsQPXVectorTy(T) ||
  3726. (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
  3727. (BT && BT->isFloatingPoint())) {
  3728. QualType QT(T, 0);
  3729. I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
  3730. continue;
  3731. }
  3732. }
  3733. I.info = classifyArgumentType(I.type);
  3734. }
  3735. }
  3736. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3737. QualType Ty) const override;
  3738. };
  3739. class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
  3740. public:
  3741. PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
  3742. PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX,
  3743. bool SoftFloatABI)
  3744. : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX,
  3745. SoftFloatABI)) {}
  3746. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  3747. // This is recovered from gcc output.
  3748. return 1; // r1 is the dedicated stack pointer
  3749. }
  3750. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  3751. llvm::Value *Address) const override;
  3752. };
  3753. class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
  3754. public:
  3755. PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
  3756. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  3757. // This is recovered from gcc output.
  3758. return 1; // r1 is the dedicated stack pointer
  3759. }
  3760. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  3761. llvm::Value *Address) const override;
  3762. };
  3763. }
  3764. // Return true if the ABI requires Ty to be passed sign- or zero-
  3765. // extended to 64 bits.
  3766. bool
  3767. PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
  3768. // Treat an enum type as its underlying type.
  3769. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  3770. Ty = EnumTy->getDecl()->getIntegerType();
  3771. // Promotable integer types are required to be promoted by the ABI.
  3772. if (Ty->isPromotableIntegerType())
  3773. return true;
  3774. // In addition to the usual promotable integer types, we also need to
  3775. // extend all 32-bit types, since the ABI requires promotion to 64 bits.
  3776. if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
  3777. switch (BT->getKind()) {
  3778. case BuiltinType::Int:
  3779. case BuiltinType::UInt:
  3780. return true;
  3781. default:
  3782. break;
  3783. }
  3784. return false;
  3785. }
  3786. /// isAlignedParamType - Determine whether a type requires 16-byte or
  3787. /// higher alignment in the parameter area. Always returns at least 8.
  3788. CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
  3789. // Complex types are passed just like their elements.
  3790. if (const ComplexType *CTy = Ty->getAs<ComplexType>())
  3791. Ty = CTy->getElementType();
  3792. // Only vector types of size 16 bytes need alignment (larger types are
  3793. // passed via reference, smaller types are not aligned).
  3794. if (IsQPXVectorTy(Ty)) {
  3795. if (getContext().getTypeSize(Ty) > 128)
  3796. return CharUnits::fromQuantity(32);
  3797. return CharUnits::fromQuantity(16);
  3798. } else if (Ty->isVectorType()) {
  3799. return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
  3800. }
  3801. // For single-element float/vector structs, we consider the whole type
  3802. // to have the same alignment requirements as its single element.
  3803. const Type *AlignAsType = nullptr;
  3804. const Type *EltType = isSingleElementStruct(Ty, getContext());
  3805. if (EltType) {
  3806. const BuiltinType *BT = EltType->getAs<BuiltinType>();
  3807. if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
  3808. getContext().getTypeSize(EltType) == 128) ||
  3809. (BT && BT->isFloatingPoint()))
  3810. AlignAsType = EltType;
  3811. }
  3812. // Likewise for ELFv2 homogeneous aggregates.
  3813. const Type *Base = nullptr;
  3814. uint64_t Members = 0;
  3815. if (!AlignAsType && Kind == ELFv2 &&
  3816. isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
  3817. AlignAsType = Base;
  3818. // With special case aggregates, only vector base types need alignment.
  3819. if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
  3820. if (getContext().getTypeSize(AlignAsType) > 128)
  3821. return CharUnits::fromQuantity(32);
  3822. return CharUnits::fromQuantity(16);
  3823. } else if (AlignAsType) {
  3824. return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
  3825. }
  3826. // Otherwise, we only need alignment for any aggregate type that
  3827. // has an alignment requirement of >= 16 bytes.
  3828. if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
  3829. if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
  3830. return CharUnits::fromQuantity(32);
  3831. return CharUnits::fromQuantity(16);
  3832. }
  3833. return CharUnits::fromQuantity(8);
  3834. }
  3835. /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
  3836. /// aggregate. Base is set to the base element type, and Members is set
  3837. /// to the number of base elements.
  3838. bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
  3839. uint64_t &Members) const {
  3840. if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
  3841. uint64_t NElements = AT->getSize().getZExtValue();
  3842. if (NElements == 0)
  3843. return false;
  3844. if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
  3845. return false;
  3846. Members *= NElements;
  3847. } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
  3848. const RecordDecl *RD = RT->getDecl();
  3849. if (RD->hasFlexibleArrayMember())
  3850. return false;
  3851. Members = 0;
  3852. // If this is a C++ record, check the bases first.
  3853. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  3854. for (const auto &I : CXXRD->bases()) {
  3855. // Ignore empty records.
  3856. if (isEmptyRecord(getContext(), I.getType(), true))
  3857. continue;
  3858. uint64_t FldMembers;
  3859. if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
  3860. return false;
  3861. Members += FldMembers;
  3862. }
  3863. }
  3864. for (const auto *FD : RD->fields()) {
  3865. // Ignore (non-zero arrays of) empty records.
  3866. QualType FT = FD->getType();
  3867. while (const ConstantArrayType *AT =
  3868. getContext().getAsConstantArrayType(FT)) {
  3869. if (AT->getSize().getZExtValue() == 0)
  3870. return false;
  3871. FT = AT->getElementType();
  3872. }
  3873. if (isEmptyRecord(getContext(), FT, true))
  3874. continue;
  3875. // For compatibility with GCC, ignore empty bitfields in C++ mode.
  3876. if (getContext().getLangOpts().CPlusPlus &&
  3877. FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
  3878. continue;
  3879. uint64_t FldMembers;
  3880. if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
  3881. return false;
  3882. Members = (RD->isUnion() ?
  3883. std::max(Members, FldMembers) : Members + FldMembers);
  3884. }
  3885. if (!Base)
  3886. return false;
  3887. // Ensure there is no padding.
  3888. if (getContext().getTypeSize(Base) * Members !=
  3889. getContext().getTypeSize(Ty))
  3890. return false;
  3891. } else {
  3892. Members = 1;
  3893. if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
  3894. Members = 2;
  3895. Ty = CT->getElementType();
  3896. }
  3897. // Most ABIs only support float, double, and some vector type widths.
  3898. if (!isHomogeneousAggregateBaseType(Ty))
  3899. return false;
  3900. // The base type must be the same for all members. Types that
  3901. // agree in both total size and mode (float vs. vector) are
  3902. // treated as being equivalent here.
  3903. const Type *TyPtr = Ty.getTypePtr();
  3904. if (!Base) {
  3905. Base = TyPtr;
  3906. // If it's a non-power-of-2 vector, its size is already a power-of-2,
  3907. // so make sure to widen it explicitly.
  3908. if (const VectorType *VT = Base->getAs<VectorType>()) {
  3909. QualType EltTy = VT->getElementType();
  3910. unsigned NumElements =
  3911. getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
  3912. Base = getContext()
  3913. .getVectorType(EltTy, NumElements, VT->getVectorKind())
  3914. .getTypePtr();
  3915. }
  3916. }
  3917. if (Base->isVectorType() != TyPtr->isVectorType() ||
  3918. getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
  3919. return false;
  3920. }
  3921. return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
  3922. }
  3923. bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  3924. // Homogeneous aggregates for ELFv2 must have base types of float,
  3925. // double, long double, or 128-bit vectors.
  3926. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  3927. if (BT->getKind() == BuiltinType::Float ||
  3928. BT->getKind() == BuiltinType::Double ||
  3929. BT->getKind() == BuiltinType::LongDouble) {
  3930. if (IsSoftFloatABI)
  3931. return false;
  3932. return true;
  3933. }
  3934. }
  3935. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  3936. if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
  3937. return true;
  3938. }
  3939. return false;
  3940. }
  3941. bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
  3942. const Type *Base, uint64_t Members) const {
  3943. // Vector types require one register, floating point types require one
  3944. // or two registers depending on their size.
  3945. uint32_t NumRegs =
  3946. Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64;
  3947. // Homogeneous Aggregates may occupy at most 8 registers.
  3948. return Members * NumRegs <= 8;
  3949. }
  3950. ABIArgInfo
  3951. PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
  3952. Ty = useFirstFieldIfTransparentUnion(Ty);
  3953. if (Ty->isAnyComplexType())
  3954. return ABIArgInfo::getDirect();
  3955. // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
  3956. // or via reference (larger than 16 bytes).
  3957. if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
  3958. uint64_t Size = getContext().getTypeSize(Ty);
  3959. if (Size > 128)
  3960. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  3961. else if (Size < 128) {
  3962. llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
  3963. return ABIArgInfo::getDirect(CoerceTy);
  3964. }
  3965. }
  3966. if (isAggregateTypeForABI(Ty)) {
  3967. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  3968. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  3969. uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
  3970. uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
  3971. // ELFv2 homogeneous aggregates are passed as array types.
  3972. const Type *Base = nullptr;
  3973. uint64_t Members = 0;
  3974. if (Kind == ELFv2 &&
  3975. isHomogeneousAggregate(Ty, Base, Members)) {
  3976. llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
  3977. llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
  3978. return ABIArgInfo::getDirect(CoerceTy);
  3979. }
  3980. // If an aggregate may end up fully in registers, we do not
  3981. // use the ByVal method, but pass the aggregate as array.
  3982. // This is usually beneficial since we avoid forcing the
  3983. // back-end to store the argument to memory.
  3984. uint64_t Bits = getContext().getTypeSize(Ty);
  3985. if (Bits > 0 && Bits <= 8 * GPRBits) {
  3986. llvm::Type *CoerceTy;
  3987. // Types up to 8 bytes are passed as integer type (which will be
  3988. // properly aligned in the argument save area doubleword).
  3989. if (Bits <= GPRBits)
  3990. CoerceTy =
  3991. llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
  3992. // Larger types are passed as arrays, with the base type selected
  3993. // according to the required alignment in the save area.
  3994. else {
  3995. uint64_t RegBits = ABIAlign * 8;
  3996. uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
  3997. llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
  3998. CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
  3999. }
  4000. return ABIArgInfo::getDirect(CoerceTy);
  4001. }
  4002. // All other aggregates are passed ByVal.
  4003. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
  4004. /*ByVal=*/true,
  4005. /*Realign=*/TyAlign > ABIAlign);
  4006. }
  4007. return (isPromotableTypeForABI(Ty) ?
  4008. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  4009. }
  4010. ABIArgInfo
  4011. PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
  4012. if (RetTy->isVoidType())
  4013. return ABIArgInfo::getIgnore();
  4014. if (RetTy->isAnyComplexType())
  4015. return ABIArgInfo::getDirect();
  4016. // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
  4017. // or via reference (larger than 16 bytes).
  4018. if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
  4019. uint64_t Size = getContext().getTypeSize(RetTy);
  4020. if (Size > 128)
  4021. return getNaturalAlignIndirect(RetTy);
  4022. else if (Size < 128) {
  4023. llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
  4024. return ABIArgInfo::getDirect(CoerceTy);
  4025. }
  4026. }
  4027. if (isAggregateTypeForABI(RetTy)) {
  4028. // ELFv2 homogeneous aggregates are returned as array types.
  4029. const Type *Base = nullptr;
  4030. uint64_t Members = 0;
  4031. if (Kind == ELFv2 &&
  4032. isHomogeneousAggregate(RetTy, Base, Members)) {
  4033. llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
  4034. llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
  4035. return ABIArgInfo::getDirect(CoerceTy);
  4036. }
  4037. // ELFv2 small aggregates are returned in up to two registers.
  4038. uint64_t Bits = getContext().getTypeSize(RetTy);
  4039. if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
  4040. if (Bits == 0)
  4041. return ABIArgInfo::getIgnore();
  4042. llvm::Type *CoerceTy;
  4043. if (Bits > GPRBits) {
  4044. CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
  4045. CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
  4046. } else
  4047. CoerceTy =
  4048. llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
  4049. return ABIArgInfo::getDirect(CoerceTy);
  4050. }
  4051. // All other aggregates are returned indirectly.
  4052. return getNaturalAlignIndirect(RetTy);
  4053. }
  4054. return (isPromotableTypeForABI(RetTy) ?
  4055. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  4056. }
  4057. // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
  4058. Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4059. QualType Ty) const {
  4060. auto TypeInfo = getContext().getTypeInfoInChars(Ty);
  4061. TypeInfo.second = getParamTypeAlignment(Ty);
  4062. CharUnits SlotSize = CharUnits::fromQuantity(8);
  4063. // If we have a complex type and the base type is smaller than 8 bytes,
  4064. // the ABI calls for the real and imaginary parts to be right-adjusted
  4065. // in separate doublewords. However, Clang expects us to produce a
  4066. // pointer to a structure with the two parts packed tightly. So generate
  4067. // loads of the real and imaginary parts relative to the va_list pointer,
  4068. // and store them to a temporary structure.
  4069. if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
  4070. CharUnits EltSize = TypeInfo.first / 2;
  4071. if (EltSize < SlotSize) {
  4072. Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
  4073. SlotSize * 2, SlotSize,
  4074. SlotSize, /*AllowHigher*/ true);
  4075. Address RealAddr = Addr;
  4076. Address ImagAddr = RealAddr;
  4077. if (CGF.CGM.getDataLayout().isBigEndian()) {
  4078. RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
  4079. SlotSize - EltSize);
  4080. ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
  4081. 2 * SlotSize - EltSize);
  4082. } else {
  4083. ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
  4084. }
  4085. llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
  4086. RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
  4087. ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
  4088. llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
  4089. llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
  4090. Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
  4091. CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
  4092. /*init*/ true);
  4093. return Temp;
  4094. }
  4095. }
  4096. // Otherwise, just use the general rule.
  4097. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
  4098. TypeInfo, SlotSize, /*AllowHigher*/ true);
  4099. }
  4100. static bool
  4101. PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  4102. llvm::Value *Address) {
  4103. // This is calculated from the LLVM and GCC tables and verified
  4104. // against gcc output. AFAIK all ABIs use the same encoding.
  4105. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  4106. llvm::IntegerType *i8 = CGF.Int8Ty;
  4107. llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
  4108. llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
  4109. llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
  4110. // 0-31: r0-31, the 8-byte general-purpose registers
  4111. AssignToArrayRange(Builder, Address, Eight8, 0, 31);
  4112. // 32-63: fp0-31, the 8-byte floating-point registers
  4113. AssignToArrayRange(Builder, Address, Eight8, 32, 63);
  4114. // 64-67 are various 8-byte special-purpose registers:
  4115. // 64: mq
  4116. // 65: lr
  4117. // 66: ctr
  4118. // 67: ap
  4119. AssignToArrayRange(Builder, Address, Eight8, 64, 67);
  4120. // 68-76 are various 4-byte special-purpose registers:
  4121. // 68-75 cr0-7
  4122. // 76: xer
  4123. AssignToArrayRange(Builder, Address, Four8, 68, 76);
  4124. // 77-108: v0-31, the 16-byte vector registers
  4125. AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
  4126. // 109: vrsave
  4127. // 110: vscr
  4128. // 111: spe_acc
  4129. // 112: spefscr
  4130. // 113: sfp
  4131. // 114: tfhar
  4132. // 115: tfiar
  4133. // 116: texasr
  4134. AssignToArrayRange(Builder, Address, Eight8, 109, 116);
  4135. return false;
  4136. }
  4137. bool
  4138. PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
  4139. CodeGen::CodeGenFunction &CGF,
  4140. llvm::Value *Address) const {
  4141. return PPC64_initDwarfEHRegSizeTable(CGF, Address);
  4142. }
  4143. bool
  4144. PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  4145. llvm::Value *Address) const {
  4146. return PPC64_initDwarfEHRegSizeTable(CGF, Address);
  4147. }
  4148. //===----------------------------------------------------------------------===//
  4149. // AArch64 ABI Implementation
  4150. //===----------------------------------------------------------------------===//
  4151. namespace {
  4152. class AArch64ABIInfo : public SwiftABIInfo {
  4153. public:
  4154. enum ABIKind {
  4155. AAPCS = 0,
  4156. DarwinPCS,
  4157. Win64
  4158. };
  4159. private:
  4160. ABIKind Kind;
  4161. public:
  4162. AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
  4163. : SwiftABIInfo(CGT), Kind(Kind) {}
  4164. private:
  4165. ABIKind getABIKind() const { return Kind; }
  4166. bool isDarwinPCS() const { return Kind == DarwinPCS; }
  4167. ABIArgInfo classifyReturnType(QualType RetTy) const;
  4168. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  4169. bool isHomogeneousAggregateBaseType(QualType Ty) const override;
  4170. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  4171. uint64_t Members) const override;
  4172. bool isIllegalVectorType(QualType Ty) const;
  4173. void computeInfo(CGFunctionInfo &FI) const override {
  4174. if (!getCXXABI().classifyReturnType(FI))
  4175. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  4176. for (auto &it : FI.arguments())
  4177. it.info = classifyArgumentType(it.type);
  4178. }
  4179. Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
  4180. CodeGenFunction &CGF) const;
  4181. Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
  4182. CodeGenFunction &CGF) const;
  4183. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4184. QualType Ty) const override {
  4185. return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty)
  4186. : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
  4187. : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
  4188. }
  4189. Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4190. QualType Ty) const override;
  4191. bool shouldPassIndirectlyForSwift(CharUnits totalSize,
  4192. ArrayRef<llvm::Type*> scalars,
  4193. bool asReturnValue) const override {
  4194. return occupiesMoreThan(CGT, scalars, /*total*/ 4);
  4195. }
  4196. bool isSwiftErrorInRegister() const override {
  4197. return true;
  4198. }
  4199. bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
  4200. unsigned elts) const override;
  4201. };
  4202. class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
  4203. public:
  4204. AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
  4205. : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
  4206. StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
  4207. return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue";
  4208. }
  4209. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  4210. return 31;
  4211. }
  4212. bool doesReturnSlotInterfereWithArgs() const override { return false; }
  4213. };
  4214. class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo {
  4215. public:
  4216. WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K)
  4217. : AArch64TargetCodeGenInfo(CGT, K) {}
  4218. void getDependentLibraryOption(llvm::StringRef Lib,
  4219. llvm::SmallString<24> &Opt) const override {
  4220. Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
  4221. }
  4222. void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
  4223. llvm::SmallString<32> &Opt) const override {
  4224. Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
  4225. }
  4226. };
  4227. }
  4228. ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
  4229. Ty = useFirstFieldIfTransparentUnion(Ty);
  4230. // Handle illegal vector types here.
  4231. if (isIllegalVectorType(Ty)) {
  4232. uint64_t Size = getContext().getTypeSize(Ty);
  4233. // Android promotes <2 x i8> to i16, not i32
  4234. if (isAndroid() && (Size <= 16)) {
  4235. llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
  4236. return ABIArgInfo::getDirect(ResType);
  4237. }
  4238. if (Size <= 32) {
  4239. llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
  4240. return ABIArgInfo::getDirect(ResType);
  4241. }
  4242. if (Size == 64) {
  4243. llvm::Type *ResType =
  4244. llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
  4245. return ABIArgInfo::getDirect(ResType);
  4246. }
  4247. if (Size == 128) {
  4248. llvm::Type *ResType =
  4249. llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
  4250. return ABIArgInfo::getDirect(ResType);
  4251. }
  4252. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  4253. }
  4254. if (!isAggregateTypeForABI(Ty)) {
  4255. // Treat an enum type as its underlying type.
  4256. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  4257. Ty = EnumTy->getDecl()->getIntegerType();
  4258. return (Ty->isPromotableIntegerType() && isDarwinPCS()
  4259. ? ABIArgInfo::getExtend()
  4260. : ABIArgInfo::getDirect());
  4261. }
  4262. // Structures with either a non-trivial destructor or a non-trivial
  4263. // copy constructor are always indirect.
  4264. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  4265. return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
  4266. CGCXXABI::RAA_DirectInMemory);
  4267. }
  4268. // Empty records are always ignored on Darwin, but actually passed in C++ mode
  4269. // elsewhere for GNU compatibility.
  4270. uint64_t Size = getContext().getTypeSize(Ty);
  4271. bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
  4272. if (IsEmpty || Size == 0) {
  4273. if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
  4274. return ABIArgInfo::getIgnore();
  4275. // GNU C mode. The only argument that gets ignored is an empty one with size
  4276. // 0.
  4277. if (IsEmpty && Size == 0)
  4278. return ABIArgInfo::getIgnore();
  4279. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  4280. }
  4281. // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
  4282. const Type *Base = nullptr;
  4283. uint64_t Members = 0;
  4284. if (isHomogeneousAggregate(Ty, Base, Members)) {
  4285. return ABIArgInfo::getDirect(
  4286. llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
  4287. }
  4288. // Aggregates <= 16 bytes are passed directly in registers or on the stack.
  4289. if (Size <= 128) {
  4290. // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
  4291. // same size and alignment.
  4292. if (getTarget().isRenderScriptTarget()) {
  4293. return coerceToIntArray(Ty, getContext(), getVMContext());
  4294. }
  4295. unsigned Alignment = getContext().getTypeAlign(Ty);
  4296. Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
  4297. // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
  4298. // For aggregates with 16-byte alignment, we use i128.
  4299. if (Alignment < 128 && Size == 128) {
  4300. llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
  4301. return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
  4302. }
  4303. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
  4304. }
  4305. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  4306. }
  4307. ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const {
  4308. if (RetTy->isVoidType())
  4309. return ABIArgInfo::getIgnore();
  4310. // Large vector types should be returned via memory.
  4311. if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
  4312. return getNaturalAlignIndirect(RetTy);
  4313. if (!isAggregateTypeForABI(RetTy)) {
  4314. // Treat an enum type as its underlying type.
  4315. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  4316. RetTy = EnumTy->getDecl()->getIntegerType();
  4317. return (RetTy->isPromotableIntegerType() && isDarwinPCS()
  4318. ? ABIArgInfo::getExtend()
  4319. : ABIArgInfo::getDirect());
  4320. }
  4321. uint64_t Size = getContext().getTypeSize(RetTy);
  4322. if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
  4323. return ABIArgInfo::getIgnore();
  4324. const Type *Base = nullptr;
  4325. uint64_t Members = 0;
  4326. if (isHomogeneousAggregate(RetTy, Base, Members))
  4327. // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
  4328. return ABIArgInfo::getDirect();
  4329. // Aggregates <= 16 bytes are returned directly in registers or on the stack.
  4330. if (Size <= 128) {
  4331. // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
  4332. // same size and alignment.
  4333. if (getTarget().isRenderScriptTarget()) {
  4334. return coerceToIntArray(RetTy, getContext(), getVMContext());
  4335. }
  4336. unsigned Alignment = getContext().getTypeAlign(RetTy);
  4337. Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
  4338. // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
  4339. // For aggregates with 16-byte alignment, we use i128.
  4340. if (Alignment < 128 && Size == 128) {
  4341. llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
  4342. return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
  4343. }
  4344. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
  4345. }
  4346. return getNaturalAlignIndirect(RetTy);
  4347. }
  4348. /// isIllegalVectorType - check whether the vector type is legal for AArch64.
  4349. bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
  4350. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  4351. // Check whether VT is legal.
  4352. unsigned NumElements = VT->getNumElements();
  4353. uint64_t Size = getContext().getTypeSize(VT);
  4354. // NumElements should be power of 2.
  4355. if (!llvm::isPowerOf2_32(NumElements))
  4356. return true;
  4357. return Size != 64 && (Size != 128 || NumElements == 1);
  4358. }
  4359. return false;
  4360. }
  4361. bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize,
  4362. llvm::Type *eltTy,
  4363. unsigned elts) const {
  4364. if (!llvm::isPowerOf2_32(elts))
  4365. return false;
  4366. if (totalSize.getQuantity() != 8 &&
  4367. (totalSize.getQuantity() != 16 || elts == 1))
  4368. return false;
  4369. return true;
  4370. }
  4371. bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  4372. // Homogeneous aggregates for AAPCS64 must have base types of a floating
  4373. // point type or a short-vector type. This is the same as the 32-bit ABI,
  4374. // but with the difference that any floating-point type is allowed,
  4375. // including __fp16.
  4376. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  4377. if (BT->isFloatingPoint())
  4378. return true;
  4379. } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
  4380. unsigned VecSize = getContext().getTypeSize(VT);
  4381. if (VecSize == 64 || VecSize == 128)
  4382. return true;
  4383. }
  4384. return false;
  4385. }
  4386. bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
  4387. uint64_t Members) const {
  4388. return Members <= 4;
  4389. }
  4390. Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr,
  4391. QualType Ty,
  4392. CodeGenFunction &CGF) const {
  4393. ABIArgInfo AI = classifyArgumentType(Ty);
  4394. bool IsIndirect = AI.isIndirect();
  4395. llvm::Type *BaseTy = CGF.ConvertType(Ty);
  4396. if (IsIndirect)
  4397. BaseTy = llvm::PointerType::getUnqual(BaseTy);
  4398. else if (AI.getCoerceToType())
  4399. BaseTy = AI.getCoerceToType();
  4400. unsigned NumRegs = 1;
  4401. if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
  4402. BaseTy = ArrTy->getElementType();
  4403. NumRegs = ArrTy->getNumElements();
  4404. }
  4405. bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
  4406. // The AArch64 va_list type and handling is specified in the Procedure Call
  4407. // Standard, section B.4:
  4408. //
  4409. // struct {
  4410. // void *__stack;
  4411. // void *__gr_top;
  4412. // void *__vr_top;
  4413. // int __gr_offs;
  4414. // int __vr_offs;
  4415. // };
  4416. llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
  4417. llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
  4418. llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
  4419. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
  4420. auto TyInfo = getContext().getTypeInfoInChars(Ty);
  4421. CharUnits TyAlign = TyInfo.second;
  4422. Address reg_offs_p = Address::invalid();
  4423. llvm::Value *reg_offs = nullptr;
  4424. int reg_top_index;
  4425. CharUnits reg_top_offset;
  4426. int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity();
  4427. if (!IsFPR) {
  4428. // 3 is the field number of __gr_offs
  4429. reg_offs_p =
  4430. CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
  4431. "gr_offs_p");
  4432. reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
  4433. reg_top_index = 1; // field number for __gr_top
  4434. reg_top_offset = CharUnits::fromQuantity(8);
  4435. RegSize = llvm::alignTo(RegSize, 8);
  4436. } else {
  4437. // 4 is the field number of __vr_offs.
  4438. reg_offs_p =
  4439. CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28),
  4440. "vr_offs_p");
  4441. reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
  4442. reg_top_index = 2; // field number for __vr_top
  4443. reg_top_offset = CharUnits::fromQuantity(16);
  4444. RegSize = 16 * NumRegs;
  4445. }
  4446. //=======================================
  4447. // Find out where argument was passed
  4448. //=======================================
  4449. // If reg_offs >= 0 we're already using the stack for this type of
  4450. // argument. We don't want to keep updating reg_offs (in case it overflows,
  4451. // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
  4452. // whatever they get).
  4453. llvm::Value *UsingStack = nullptr;
  4454. UsingStack = CGF.Builder.CreateICmpSGE(
  4455. reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
  4456. CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
  4457. // Otherwise, at least some kind of argument could go in these registers, the
  4458. // question is whether this particular type is too big.
  4459. CGF.EmitBlock(MaybeRegBlock);
  4460. // Integer arguments may need to correct register alignment (for example a
  4461. // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
  4462. // align __gr_offs to calculate the potential address.
  4463. if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
  4464. int Align = TyAlign.getQuantity();
  4465. reg_offs = CGF.Builder.CreateAdd(
  4466. reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
  4467. "align_regoffs");
  4468. reg_offs = CGF.Builder.CreateAnd(
  4469. reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
  4470. "aligned_regoffs");
  4471. }
  4472. // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
  4473. // The fact that this is done unconditionally reflects the fact that
  4474. // allocating an argument to the stack also uses up all the remaining
  4475. // registers of the appropriate kind.
  4476. llvm::Value *NewOffset = nullptr;
  4477. NewOffset = CGF.Builder.CreateAdd(
  4478. reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
  4479. CGF.Builder.CreateStore(NewOffset, reg_offs_p);
  4480. // Now we're in a position to decide whether this argument really was in
  4481. // registers or not.
  4482. llvm::Value *InRegs = nullptr;
  4483. InRegs = CGF.Builder.CreateICmpSLE(
  4484. NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
  4485. CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
  4486. //=======================================
  4487. // Argument was in registers
  4488. //=======================================
  4489. // Now we emit the code for if the argument was originally passed in
  4490. // registers. First start the appropriate block:
  4491. CGF.EmitBlock(InRegBlock);
  4492. llvm::Value *reg_top = nullptr;
  4493. Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index,
  4494. reg_top_offset, "reg_top_p");
  4495. reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
  4496. Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs),
  4497. CharUnits::fromQuantity(IsFPR ? 16 : 8));
  4498. Address RegAddr = Address::invalid();
  4499. llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
  4500. if (IsIndirect) {
  4501. // If it's been passed indirectly (actually a struct), whatever we find from
  4502. // stored registers or on the stack will actually be a struct **.
  4503. MemTy = llvm::PointerType::getUnqual(MemTy);
  4504. }
  4505. const Type *Base = nullptr;
  4506. uint64_t NumMembers = 0;
  4507. bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
  4508. if (IsHFA && NumMembers > 1) {
  4509. // Homogeneous aggregates passed in registers will have their elements split
  4510. // and stored 16-bytes apart regardless of size (they're notionally in qN,
  4511. // qN+1, ...). We reload and store into a temporary local variable
  4512. // contiguously.
  4513. assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
  4514. auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
  4515. llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
  4516. llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
  4517. Address Tmp = CGF.CreateTempAlloca(HFATy,
  4518. std::max(TyAlign, BaseTyInfo.second));
  4519. // On big-endian platforms, the value will be right-aligned in its slot.
  4520. int Offset = 0;
  4521. if (CGF.CGM.getDataLayout().isBigEndian() &&
  4522. BaseTyInfo.first.getQuantity() < 16)
  4523. Offset = 16 - BaseTyInfo.first.getQuantity();
  4524. for (unsigned i = 0; i < NumMembers; ++i) {
  4525. CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
  4526. Address LoadAddr =
  4527. CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
  4528. LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
  4529. Address StoreAddr =
  4530. CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first);
  4531. llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
  4532. CGF.Builder.CreateStore(Elem, StoreAddr);
  4533. }
  4534. RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
  4535. } else {
  4536. // Otherwise the object is contiguous in memory.
  4537. // It might be right-aligned in its slot.
  4538. CharUnits SlotSize = BaseAddr.getAlignment();
  4539. if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
  4540. (IsHFA || !isAggregateTypeForABI(Ty)) &&
  4541. TyInfo.first < SlotSize) {
  4542. CharUnits Offset = SlotSize - TyInfo.first;
  4543. BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
  4544. }
  4545. RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
  4546. }
  4547. CGF.EmitBranch(ContBlock);
  4548. //=======================================
  4549. // Argument was on the stack
  4550. //=======================================
  4551. CGF.EmitBlock(OnStackBlock);
  4552. Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0,
  4553. CharUnits::Zero(), "stack_p");
  4554. llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
  4555. // Again, stack arguments may need realignment. In this case both integer and
  4556. // floating-point ones might be affected.
  4557. if (!IsIndirect && TyAlign.getQuantity() > 8) {
  4558. int Align = TyAlign.getQuantity();
  4559. OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
  4560. OnStackPtr = CGF.Builder.CreateAdd(
  4561. OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
  4562. "align_stack");
  4563. OnStackPtr = CGF.Builder.CreateAnd(
  4564. OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
  4565. "align_stack");
  4566. OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
  4567. }
  4568. Address OnStackAddr(OnStackPtr,
  4569. std::max(CharUnits::fromQuantity(8), TyAlign));
  4570. // All stack slots are multiples of 8 bytes.
  4571. CharUnits StackSlotSize = CharUnits::fromQuantity(8);
  4572. CharUnits StackSize;
  4573. if (IsIndirect)
  4574. StackSize = StackSlotSize;
  4575. else
  4576. StackSize = TyInfo.first.alignTo(StackSlotSize);
  4577. llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
  4578. llvm::Value *NewStack =
  4579. CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack");
  4580. // Write the new value of __stack for the next call to va_arg
  4581. CGF.Builder.CreateStore(NewStack, stack_p);
  4582. if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
  4583. TyInfo.first < StackSlotSize) {
  4584. CharUnits Offset = StackSlotSize - TyInfo.first;
  4585. OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
  4586. }
  4587. OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
  4588. CGF.EmitBranch(ContBlock);
  4589. //=======================================
  4590. // Tidy up
  4591. //=======================================
  4592. CGF.EmitBlock(ContBlock);
  4593. Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
  4594. OnStackAddr, OnStackBlock, "vaargs.addr");
  4595. if (IsIndirect)
  4596. return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
  4597. TyInfo.second);
  4598. return ResAddr;
  4599. }
  4600. Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
  4601. CodeGenFunction &CGF) const {
  4602. // The backend's lowering doesn't support va_arg for aggregates or
  4603. // illegal vector types. Lower VAArg here for these cases and use
  4604. // the LLVM va_arg instruction for everything else.
  4605. if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
  4606. return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
  4607. CharUnits SlotSize = CharUnits::fromQuantity(8);
  4608. // Empty records are ignored for parameter passing purposes.
  4609. if (isEmptyRecord(getContext(), Ty, true)) {
  4610. Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
  4611. Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
  4612. return Addr;
  4613. }
  4614. // The size of the actual thing passed, which might end up just
  4615. // being a pointer for indirect types.
  4616. auto TyInfo = getContext().getTypeInfoInChars(Ty);
  4617. // Arguments bigger than 16 bytes which aren't homogeneous
  4618. // aggregates should be passed indirectly.
  4619. bool IsIndirect = false;
  4620. if (TyInfo.first.getQuantity() > 16) {
  4621. const Type *Base = nullptr;
  4622. uint64_t Members = 0;
  4623. IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
  4624. }
  4625. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
  4626. TyInfo, SlotSize, /*AllowHigherAlign*/ true);
  4627. }
  4628. Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4629. QualType Ty) const {
  4630. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
  4631. CGF.getContext().getTypeInfoInChars(Ty),
  4632. CharUnits::fromQuantity(8),
  4633. /*allowHigherAlign*/ false);
  4634. }
  4635. //===----------------------------------------------------------------------===//
  4636. // ARM ABI Implementation
  4637. //===----------------------------------------------------------------------===//
  4638. namespace {
  4639. class ARMABIInfo : public SwiftABIInfo {
  4640. public:
  4641. enum ABIKind {
  4642. APCS = 0,
  4643. AAPCS = 1,
  4644. AAPCS_VFP = 2,
  4645. AAPCS16_VFP = 3,
  4646. };
  4647. private:
  4648. ABIKind Kind;
  4649. public:
  4650. ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
  4651. : SwiftABIInfo(CGT), Kind(_Kind) {
  4652. setCCs();
  4653. }
  4654. bool isEABI() const {
  4655. switch (getTarget().getTriple().getEnvironment()) {
  4656. case llvm::Triple::Android:
  4657. case llvm::Triple::EABI:
  4658. case llvm::Triple::EABIHF:
  4659. case llvm::Triple::GNUEABI:
  4660. case llvm::Triple::GNUEABIHF:
  4661. case llvm::Triple::MuslEABI:
  4662. case llvm::Triple::MuslEABIHF:
  4663. return true;
  4664. default:
  4665. return false;
  4666. }
  4667. }
  4668. bool isEABIHF() const {
  4669. switch (getTarget().getTriple().getEnvironment()) {
  4670. case llvm::Triple::EABIHF:
  4671. case llvm::Triple::GNUEABIHF:
  4672. case llvm::Triple::MuslEABIHF:
  4673. return true;
  4674. default:
  4675. return false;
  4676. }
  4677. }
  4678. ABIKind getABIKind() const { return Kind; }
  4679. private:
  4680. ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const;
  4681. ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const;
  4682. bool isIllegalVectorType(QualType Ty) const;
  4683. bool isHomogeneousAggregateBaseType(QualType Ty) const override;
  4684. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  4685. uint64_t Members) const override;
  4686. void computeInfo(CGFunctionInfo &FI) const override;
  4687. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4688. QualType Ty) const override;
  4689. llvm::CallingConv::ID getLLVMDefaultCC() const;
  4690. llvm::CallingConv::ID getABIDefaultCC() const;
  4691. void setCCs();
  4692. bool shouldPassIndirectlyForSwift(CharUnits totalSize,
  4693. ArrayRef<llvm::Type*> scalars,
  4694. bool asReturnValue) const override {
  4695. return occupiesMoreThan(CGT, scalars, /*total*/ 4);
  4696. }
  4697. bool isSwiftErrorInRegister() const override {
  4698. return true;
  4699. }
  4700. bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
  4701. unsigned elts) const override;
  4702. };
  4703. class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
  4704. public:
  4705. ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
  4706. :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
  4707. const ARMABIInfo &getABIInfo() const {
  4708. return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
  4709. }
  4710. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  4711. return 13;
  4712. }
  4713. StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
  4714. return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
  4715. }
  4716. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  4717. llvm::Value *Address) const override {
  4718. llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
  4719. // 0-15 are the 16 integer registers.
  4720. AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
  4721. return false;
  4722. }
  4723. unsigned getSizeOfUnwindException() const override {
  4724. if (getABIInfo().isEABI()) return 88;
  4725. return TargetCodeGenInfo::getSizeOfUnwindException();
  4726. }
  4727. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  4728. CodeGen::CodeGenModule &CGM,
  4729. ForDefinition_t IsForDefinition) const override {
  4730. if (!IsForDefinition)
  4731. return;
  4732. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  4733. if (!FD)
  4734. return;
  4735. const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
  4736. if (!Attr)
  4737. return;
  4738. const char *Kind;
  4739. switch (Attr->getInterrupt()) {
  4740. case ARMInterruptAttr::Generic: Kind = ""; break;
  4741. case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
  4742. case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
  4743. case ARMInterruptAttr::SWI: Kind = "SWI"; break;
  4744. case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
  4745. case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
  4746. }
  4747. llvm::Function *Fn = cast<llvm::Function>(GV);
  4748. Fn->addFnAttr("interrupt", Kind);
  4749. ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
  4750. if (ABI == ARMABIInfo::APCS)
  4751. return;
  4752. // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
  4753. // however this is not necessarily true on taking any interrupt. Instruct
  4754. // the backend to perform a realignment as part of the function prologue.
  4755. llvm::AttrBuilder B;
  4756. B.addStackAlignmentAttr(8);
  4757. Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
  4758. }
  4759. };
  4760. class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
  4761. public:
  4762. WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
  4763. : ARMTargetCodeGenInfo(CGT, K) {}
  4764. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  4765. CodeGen::CodeGenModule &CGM,
  4766. ForDefinition_t IsForDefinition) const override;
  4767. void getDependentLibraryOption(llvm::StringRef Lib,
  4768. llvm::SmallString<24> &Opt) const override {
  4769. Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
  4770. }
  4771. void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
  4772. llvm::SmallString<32> &Opt) const override {
  4773. Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
  4774. }
  4775. };
  4776. void WindowsARMTargetCodeGenInfo::setTargetAttributes(
  4777. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM,
  4778. ForDefinition_t IsForDefinition) const {
  4779. ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition);
  4780. if (!IsForDefinition)
  4781. return;
  4782. addStackProbeSizeTargetAttribute(D, GV, CGM);
  4783. }
  4784. }
  4785. void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
  4786. if (!getCXXABI().classifyReturnType(FI))
  4787. FI.getReturnInfo() =
  4788. classifyReturnType(FI.getReturnType(), FI.isVariadic());
  4789. for (auto &I : FI.arguments())
  4790. I.info = classifyArgumentType(I.type, FI.isVariadic());
  4791. // Always honor user-specified calling convention.
  4792. if (FI.getCallingConvention() != llvm::CallingConv::C)
  4793. return;
  4794. llvm::CallingConv::ID cc = getRuntimeCC();
  4795. if (cc != llvm::CallingConv::C)
  4796. FI.setEffectiveCallingConvention(cc);
  4797. }
  4798. /// Return the default calling convention that LLVM will use.
  4799. llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
  4800. // The default calling convention that LLVM will infer.
  4801. if (isEABIHF() || getTarget().getTriple().isWatchABI())
  4802. return llvm::CallingConv::ARM_AAPCS_VFP;
  4803. else if (isEABI())
  4804. return llvm::CallingConv::ARM_AAPCS;
  4805. else
  4806. return llvm::CallingConv::ARM_APCS;
  4807. }
  4808. /// Return the calling convention that our ABI would like us to use
  4809. /// as the C calling convention.
  4810. llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
  4811. switch (getABIKind()) {
  4812. case APCS: return llvm::CallingConv::ARM_APCS;
  4813. case AAPCS: return llvm::CallingConv::ARM_AAPCS;
  4814. case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
  4815. case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
  4816. }
  4817. llvm_unreachable("bad ABI kind");
  4818. }
  4819. void ARMABIInfo::setCCs() {
  4820. assert(getRuntimeCC() == llvm::CallingConv::C);
  4821. // Don't muddy up the IR with a ton of explicit annotations if
  4822. // they'd just match what LLVM will infer from the triple.
  4823. llvm::CallingConv::ID abiCC = getABIDefaultCC();
  4824. if (abiCC != getLLVMDefaultCC())
  4825. RuntimeCC = abiCC;
  4826. // AAPCS apparently requires runtime support functions to be soft-float, but
  4827. // that's almost certainly for historic reasons (Thumb1 not supporting VFP
  4828. // most likely). It's more convenient for AAPCS16_VFP to be hard-float.
  4829. // The Run-time ABI for the ARM Architecture section 4.1.2 requires
  4830. // AEABI-complying FP helper functions to use the base AAPCS.
  4831. // These AEABI functions are expanded in the ARM llvm backend, all the builtin
  4832. // support functions emitted by clang such as the _Complex helpers follow the
  4833. // abiCC.
  4834. if (abiCC != getLLVMDefaultCC())
  4835. BuiltinCC = abiCC;
  4836. }
  4837. ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
  4838. bool isVariadic) const {
  4839. // 6.1.2.1 The following argument types are VFP CPRCs:
  4840. // A single-precision floating-point type (including promoted
  4841. // half-precision types); A double-precision floating-point type;
  4842. // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
  4843. // with a Base Type of a single- or double-precision floating-point type,
  4844. // 64-bit containerized vectors or 128-bit containerized vectors with one
  4845. // to four Elements.
  4846. bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
  4847. Ty = useFirstFieldIfTransparentUnion(Ty);
  4848. // Handle illegal vector types here.
  4849. if (isIllegalVectorType(Ty)) {
  4850. uint64_t Size = getContext().getTypeSize(Ty);
  4851. if (Size <= 32) {
  4852. llvm::Type *ResType =
  4853. llvm::Type::getInt32Ty(getVMContext());
  4854. return ABIArgInfo::getDirect(ResType);
  4855. }
  4856. if (Size == 64) {
  4857. llvm::Type *ResType = llvm::VectorType::get(
  4858. llvm::Type::getInt32Ty(getVMContext()), 2);
  4859. return ABIArgInfo::getDirect(ResType);
  4860. }
  4861. if (Size == 128) {
  4862. llvm::Type *ResType = llvm::VectorType::get(
  4863. llvm::Type::getInt32Ty(getVMContext()), 4);
  4864. return ABIArgInfo::getDirect(ResType);
  4865. }
  4866. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  4867. }
  4868. // __fp16 gets passed as if it were an int or float, but with the top 16 bits
  4869. // unspecified. This is not done for OpenCL as it handles the half type
  4870. // natively, and does not need to interwork with AAPCS code.
  4871. if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
  4872. llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
  4873. llvm::Type::getFloatTy(getVMContext()) :
  4874. llvm::Type::getInt32Ty(getVMContext());
  4875. return ABIArgInfo::getDirect(ResType);
  4876. }
  4877. if (!isAggregateTypeForABI(Ty)) {
  4878. // Treat an enum type as its underlying type.
  4879. if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
  4880. Ty = EnumTy->getDecl()->getIntegerType();
  4881. }
  4882. return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend()
  4883. : ABIArgInfo::getDirect());
  4884. }
  4885. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  4886. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  4887. }
  4888. // Ignore empty records.
  4889. if (isEmptyRecord(getContext(), Ty, true))
  4890. return ABIArgInfo::getIgnore();
  4891. if (IsEffectivelyAAPCS_VFP) {
  4892. // Homogeneous Aggregates need to be expanded when we can fit the aggregate
  4893. // into VFP registers.
  4894. const Type *Base = nullptr;
  4895. uint64_t Members = 0;
  4896. if (isHomogeneousAggregate(Ty, Base, Members)) {
  4897. assert(Base && "Base class should be set for homogeneous aggregate");
  4898. // Base can be a floating-point or a vector.
  4899. return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
  4900. }
  4901. } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
  4902. // WatchOS does have homogeneous aggregates. Note that we intentionally use
  4903. // this convention even for a variadic function: the backend will use GPRs
  4904. // if needed.
  4905. const Type *Base = nullptr;
  4906. uint64_t Members = 0;
  4907. if (isHomogeneousAggregate(Ty, Base, Members)) {
  4908. assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
  4909. llvm::Type *Ty =
  4910. llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
  4911. return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
  4912. }
  4913. }
  4914. if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
  4915. getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
  4916. // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
  4917. // bigger than 128-bits, they get placed in space allocated by the caller,
  4918. // and a pointer is passed.
  4919. return ABIArgInfo::getIndirect(
  4920. CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
  4921. }
  4922. // Support byval for ARM.
  4923. // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
  4924. // most 8-byte. We realign the indirect argument if type alignment is bigger
  4925. // than ABI alignment.
  4926. uint64_t ABIAlign = 4;
  4927. uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
  4928. if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
  4929. getABIKind() == ARMABIInfo::AAPCS)
  4930. ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
  4931. if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
  4932. assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
  4933. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
  4934. /*ByVal=*/true,
  4935. /*Realign=*/TyAlign > ABIAlign);
  4936. }
  4937. // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
  4938. // same size and alignment.
  4939. if (getTarget().isRenderScriptTarget()) {
  4940. return coerceToIntArray(Ty, getContext(), getVMContext());
  4941. }
  4942. // Otherwise, pass by coercing to a structure of the appropriate size.
  4943. llvm::Type* ElemTy;
  4944. unsigned SizeRegs;
  4945. // FIXME: Try to match the types of the arguments more accurately where
  4946. // we can.
  4947. if (getContext().getTypeAlign(Ty) <= 32) {
  4948. ElemTy = llvm::Type::getInt32Ty(getVMContext());
  4949. SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
  4950. } else {
  4951. ElemTy = llvm::Type::getInt64Ty(getVMContext());
  4952. SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
  4953. }
  4954. return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
  4955. }
  4956. static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
  4957. llvm::LLVMContext &VMContext) {
  4958. // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
  4959. // is called integer-like if its size is less than or equal to one word, and
  4960. // the offset of each of its addressable sub-fields is zero.
  4961. uint64_t Size = Context.getTypeSize(Ty);
  4962. // Check that the type fits in a word.
  4963. if (Size > 32)
  4964. return false;
  4965. // FIXME: Handle vector types!
  4966. if (Ty->isVectorType())
  4967. return false;
  4968. // Float types are never treated as "integer like".
  4969. if (Ty->isRealFloatingType())
  4970. return false;
  4971. // If this is a builtin or pointer type then it is ok.
  4972. if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
  4973. return true;
  4974. // Small complex integer types are "integer like".
  4975. if (const ComplexType *CT = Ty->getAs<ComplexType>())
  4976. return isIntegerLikeType(CT->getElementType(), Context, VMContext);
  4977. // Single element and zero sized arrays should be allowed, by the definition
  4978. // above, but they are not.
  4979. // Otherwise, it must be a record type.
  4980. const RecordType *RT = Ty->getAs<RecordType>();
  4981. if (!RT) return false;
  4982. // Ignore records with flexible arrays.
  4983. const RecordDecl *RD = RT->getDecl();
  4984. if (RD->hasFlexibleArrayMember())
  4985. return false;
  4986. // Check that all sub-fields are at offset 0, and are themselves "integer
  4987. // like".
  4988. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  4989. bool HadField = false;
  4990. unsigned idx = 0;
  4991. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  4992. i != e; ++i, ++idx) {
  4993. const FieldDecl *FD = *i;
  4994. // Bit-fields are not addressable, we only need to verify they are "integer
  4995. // like". We still have to disallow a subsequent non-bitfield, for example:
  4996. // struct { int : 0; int x }
  4997. // is non-integer like according to gcc.
  4998. if (FD->isBitField()) {
  4999. if (!RD->isUnion())
  5000. HadField = true;
  5001. if (!isIntegerLikeType(FD->getType(), Context, VMContext))
  5002. return false;
  5003. continue;
  5004. }
  5005. // Check if this field is at offset 0.
  5006. if (Layout.getFieldOffset(idx) != 0)
  5007. return false;
  5008. if (!isIntegerLikeType(FD->getType(), Context, VMContext))
  5009. return false;
  5010. // Only allow at most one field in a structure. This doesn't match the
  5011. // wording above, but follows gcc in situations with a field following an
  5012. // empty structure.
  5013. if (!RD->isUnion()) {
  5014. if (HadField)
  5015. return false;
  5016. HadField = true;
  5017. }
  5018. }
  5019. return true;
  5020. }
  5021. ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
  5022. bool isVariadic) const {
  5023. bool IsEffectivelyAAPCS_VFP =
  5024. (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic;
  5025. if (RetTy->isVoidType())
  5026. return ABIArgInfo::getIgnore();
  5027. // Large vector types should be returned via memory.
  5028. if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) {
  5029. return getNaturalAlignIndirect(RetTy);
  5030. }
  5031. // __fp16 gets returned as if it were an int or float, but with the top 16
  5032. // bits unspecified. This is not done for OpenCL as it handles the half type
  5033. // natively, and does not need to interwork with AAPCS code.
  5034. if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
  5035. llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
  5036. llvm::Type::getFloatTy(getVMContext()) :
  5037. llvm::Type::getInt32Ty(getVMContext());
  5038. return ABIArgInfo::getDirect(ResType);
  5039. }
  5040. if (!isAggregateTypeForABI(RetTy)) {
  5041. // Treat an enum type as its underlying type.
  5042. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  5043. RetTy = EnumTy->getDecl()->getIntegerType();
  5044. return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend()
  5045. : ABIArgInfo::getDirect();
  5046. }
  5047. // Are we following APCS?
  5048. if (getABIKind() == APCS) {
  5049. if (isEmptyRecord(getContext(), RetTy, false))
  5050. return ABIArgInfo::getIgnore();
  5051. // Complex types are all returned as packed integers.
  5052. //
  5053. // FIXME: Consider using 2 x vector types if the back end handles them
  5054. // correctly.
  5055. if (RetTy->isAnyComplexType())
  5056. return ABIArgInfo::getDirect(llvm::IntegerType::get(
  5057. getVMContext(), getContext().getTypeSize(RetTy)));
  5058. // Integer like structures are returned in r0.
  5059. if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
  5060. // Return in the smallest viable integer type.
  5061. uint64_t Size = getContext().getTypeSize(RetTy);
  5062. if (Size <= 8)
  5063. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  5064. if (Size <= 16)
  5065. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  5066. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  5067. }
  5068. // Otherwise return in memory.
  5069. return getNaturalAlignIndirect(RetTy);
  5070. }
  5071. // Otherwise this is an AAPCS variant.
  5072. if (isEmptyRecord(getContext(), RetTy, true))
  5073. return ABIArgInfo::getIgnore();
  5074. // Check for homogeneous aggregates with AAPCS-VFP.
  5075. if (IsEffectivelyAAPCS_VFP) {
  5076. const Type *Base = nullptr;
  5077. uint64_t Members = 0;
  5078. if (isHomogeneousAggregate(RetTy, Base, Members)) {
  5079. assert(Base && "Base class should be set for homogeneous aggregate");
  5080. // Homogeneous Aggregates are returned directly.
  5081. return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
  5082. }
  5083. }
  5084. // Aggregates <= 4 bytes are returned in r0; other aggregates
  5085. // are returned indirectly.
  5086. uint64_t Size = getContext().getTypeSize(RetTy);
  5087. if (Size <= 32) {
  5088. // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
  5089. // same size and alignment.
  5090. if (getTarget().isRenderScriptTarget()) {
  5091. return coerceToIntArray(RetTy, getContext(), getVMContext());
  5092. }
  5093. if (getDataLayout().isBigEndian())
  5094. // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
  5095. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  5096. // Return in the smallest viable integer type.
  5097. if (Size <= 8)
  5098. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  5099. if (Size <= 16)
  5100. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  5101. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  5102. } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
  5103. llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
  5104. llvm::Type *CoerceTy =
  5105. llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
  5106. return ABIArgInfo::getDirect(CoerceTy);
  5107. }
  5108. return getNaturalAlignIndirect(RetTy);
  5109. }
  5110. /// isIllegalVector - check whether Ty is an illegal vector type.
  5111. bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
  5112. if (const VectorType *VT = Ty->getAs<VectorType> ()) {
  5113. if (isAndroid()) {
  5114. // Android shipped using Clang 3.1, which supported a slightly different
  5115. // vector ABI. The primary differences were that 3-element vector types
  5116. // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
  5117. // accepts that legacy behavior for Android only.
  5118. // Check whether VT is legal.
  5119. unsigned NumElements = VT->getNumElements();
  5120. // NumElements should be power of 2 or equal to 3.
  5121. if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
  5122. return true;
  5123. } else {
  5124. // Check whether VT is legal.
  5125. unsigned NumElements = VT->getNumElements();
  5126. uint64_t Size = getContext().getTypeSize(VT);
  5127. // NumElements should be power of 2.
  5128. if (!llvm::isPowerOf2_32(NumElements))
  5129. return true;
  5130. // Size should be greater than 32 bits.
  5131. return Size <= 32;
  5132. }
  5133. }
  5134. return false;
  5135. }
  5136. bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
  5137. llvm::Type *eltTy,
  5138. unsigned numElts) const {
  5139. if (!llvm::isPowerOf2_32(numElts))
  5140. return false;
  5141. unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy);
  5142. if (size > 64)
  5143. return false;
  5144. if (vectorSize.getQuantity() != 8 &&
  5145. (vectorSize.getQuantity() != 16 || numElts == 1))
  5146. return false;
  5147. return true;
  5148. }
  5149. bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  5150. // Homogeneous aggregates for AAPCS-VFP must have base types of float,
  5151. // double, or 64-bit or 128-bit vectors.
  5152. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  5153. if (BT->getKind() == BuiltinType::Float ||
  5154. BT->getKind() == BuiltinType::Double ||
  5155. BT->getKind() == BuiltinType::LongDouble)
  5156. return true;
  5157. } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
  5158. unsigned VecSize = getContext().getTypeSize(VT);
  5159. if (VecSize == 64 || VecSize == 128)
  5160. return true;
  5161. }
  5162. return false;
  5163. }
  5164. bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
  5165. uint64_t Members) const {
  5166. return Members <= 4;
  5167. }
  5168. Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5169. QualType Ty) const {
  5170. CharUnits SlotSize = CharUnits::fromQuantity(4);
  5171. // Empty records are ignored for parameter passing purposes.
  5172. if (isEmptyRecord(getContext(), Ty, true)) {
  5173. Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
  5174. Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
  5175. return Addr;
  5176. }
  5177. auto TyInfo = getContext().getTypeInfoInChars(Ty);
  5178. CharUnits TyAlignForABI = TyInfo.second;
  5179. // Use indirect if size of the illegal vector is bigger than 16 bytes.
  5180. bool IsIndirect = false;
  5181. const Type *Base = nullptr;
  5182. uint64_t Members = 0;
  5183. if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
  5184. IsIndirect = true;
  5185. // ARMv7k passes structs bigger than 16 bytes indirectly, in space
  5186. // allocated by the caller.
  5187. } else if (TyInfo.first > CharUnits::fromQuantity(16) &&
  5188. getABIKind() == ARMABIInfo::AAPCS16_VFP &&
  5189. !isHomogeneousAggregate(Ty, Base, Members)) {
  5190. IsIndirect = true;
  5191. // Otherwise, bound the type's ABI alignment.
  5192. // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
  5193. // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
  5194. // Our callers should be prepared to handle an under-aligned address.
  5195. } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
  5196. getABIKind() == ARMABIInfo::AAPCS) {
  5197. TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
  5198. TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
  5199. } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
  5200. // ARMv7k allows type alignment up to 16 bytes.
  5201. TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
  5202. TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
  5203. } else {
  5204. TyAlignForABI = CharUnits::fromQuantity(4);
  5205. }
  5206. TyInfo.second = TyAlignForABI;
  5207. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
  5208. SlotSize, /*AllowHigherAlign*/ true);
  5209. }
  5210. //===----------------------------------------------------------------------===//
  5211. // NVPTX ABI Implementation
  5212. //===----------------------------------------------------------------------===//
  5213. namespace {
  5214. class NVPTXABIInfo : public ABIInfo {
  5215. public:
  5216. NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
  5217. ABIArgInfo classifyReturnType(QualType RetTy) const;
  5218. ABIArgInfo classifyArgumentType(QualType Ty) const;
  5219. void computeInfo(CGFunctionInfo &FI) const override;
  5220. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5221. QualType Ty) const override;
  5222. };
  5223. class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
  5224. public:
  5225. NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
  5226. : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
  5227. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  5228. CodeGen::CodeGenModule &M,
  5229. ForDefinition_t IsForDefinition) const override;
  5230. private:
  5231. // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
  5232. // resulting MDNode to the nvvm.annotations MDNode.
  5233. static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
  5234. };
  5235. ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
  5236. if (RetTy->isVoidType())
  5237. return ABIArgInfo::getIgnore();
  5238. // note: this is different from default ABI
  5239. if (!RetTy->isScalarType())
  5240. return ABIArgInfo::getDirect();
  5241. // Treat an enum type as its underlying type.
  5242. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  5243. RetTy = EnumTy->getDecl()->getIntegerType();
  5244. return (RetTy->isPromotableIntegerType() ?
  5245. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  5246. }
  5247. ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
  5248. // Treat an enum type as its underlying type.
  5249. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  5250. Ty = EnumTy->getDecl()->getIntegerType();
  5251. // Return aggregates type as indirect by value
  5252. if (isAggregateTypeForABI(Ty))
  5253. return getNaturalAlignIndirect(Ty, /* byval */ true);
  5254. return (Ty->isPromotableIntegerType() ?
  5255. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  5256. }
  5257. void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
  5258. if (!getCXXABI().classifyReturnType(FI))
  5259. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  5260. for (auto &I : FI.arguments())
  5261. I.info = classifyArgumentType(I.type);
  5262. // Always honor user-specified calling convention.
  5263. if (FI.getCallingConvention() != llvm::CallingConv::C)
  5264. return;
  5265. FI.setEffectiveCallingConvention(getRuntimeCC());
  5266. }
  5267. Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5268. QualType Ty) const {
  5269. llvm_unreachable("NVPTX does not support varargs");
  5270. }
  5271. void NVPTXTargetCodeGenInfo::setTargetAttributes(
  5272. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M,
  5273. ForDefinition_t IsForDefinition) const {
  5274. if (!IsForDefinition)
  5275. return;
  5276. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  5277. if (!FD) return;
  5278. llvm::Function *F = cast<llvm::Function>(GV);
  5279. // Perform special handling in OpenCL mode
  5280. if (M.getLangOpts().OpenCL) {
  5281. // Use OpenCL function attributes to check for kernel functions
  5282. // By default, all functions are device functions
  5283. if (FD->hasAttr<OpenCLKernelAttr>()) {
  5284. // OpenCL __kernel functions get kernel metadata
  5285. // Create !{<func-ref>, metadata !"kernel", i32 1} node
  5286. addNVVMMetadata(F, "kernel", 1);
  5287. // And kernel functions are not subject to inlining
  5288. F->addFnAttr(llvm::Attribute::NoInline);
  5289. }
  5290. }
  5291. // Perform special handling in CUDA mode.
  5292. if (M.getLangOpts().CUDA) {
  5293. // CUDA __global__ functions get a kernel metadata entry. Since
  5294. // __global__ functions cannot be called from the device, we do not
  5295. // need to set the noinline attribute.
  5296. if (FD->hasAttr<CUDAGlobalAttr>()) {
  5297. // Create !{<func-ref>, metadata !"kernel", i32 1} node
  5298. addNVVMMetadata(F, "kernel", 1);
  5299. }
  5300. if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
  5301. // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
  5302. llvm::APSInt MaxThreads(32);
  5303. MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
  5304. if (MaxThreads > 0)
  5305. addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
  5306. // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
  5307. // not specified in __launch_bounds__ or if the user specified a 0 value,
  5308. // we don't have to add a PTX directive.
  5309. if (Attr->getMinBlocks()) {
  5310. llvm::APSInt MinBlocks(32);
  5311. MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
  5312. if (MinBlocks > 0)
  5313. // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
  5314. addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
  5315. }
  5316. }
  5317. }
  5318. }
  5319. void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
  5320. int Operand) {
  5321. llvm::Module *M = F->getParent();
  5322. llvm::LLVMContext &Ctx = M->getContext();
  5323. // Get "nvvm.annotations" metadata node
  5324. llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
  5325. llvm::Metadata *MDVals[] = {
  5326. llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
  5327. llvm::ConstantAsMetadata::get(
  5328. llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
  5329. // Append metadata to nvvm.annotations
  5330. MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
  5331. }
  5332. }
  5333. //===----------------------------------------------------------------------===//
  5334. // SystemZ ABI Implementation
  5335. //===----------------------------------------------------------------------===//
  5336. namespace {
  5337. class SystemZABIInfo : public SwiftABIInfo {
  5338. bool HasVector;
  5339. public:
  5340. SystemZABIInfo(CodeGenTypes &CGT, bool HV)
  5341. : SwiftABIInfo(CGT), HasVector(HV) {}
  5342. bool isPromotableIntegerType(QualType Ty) const;
  5343. bool isCompoundType(QualType Ty) const;
  5344. bool isVectorArgumentType(QualType Ty) const;
  5345. bool isFPArgumentType(QualType Ty) const;
  5346. QualType GetSingleElementType(QualType Ty) const;
  5347. ABIArgInfo classifyReturnType(QualType RetTy) const;
  5348. ABIArgInfo classifyArgumentType(QualType ArgTy) const;
  5349. void computeInfo(CGFunctionInfo &FI) const override {
  5350. if (!getCXXABI().classifyReturnType(FI))
  5351. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  5352. for (auto &I : FI.arguments())
  5353. I.info = classifyArgumentType(I.type);
  5354. }
  5355. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5356. QualType Ty) const override;
  5357. bool shouldPassIndirectlyForSwift(CharUnits totalSize,
  5358. ArrayRef<llvm::Type*> scalars,
  5359. bool asReturnValue) const override {
  5360. return occupiesMoreThan(CGT, scalars, /*total*/ 4);
  5361. }
  5362. bool isSwiftErrorInRegister() const override {
  5363. return false;
  5364. }
  5365. };
  5366. class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
  5367. public:
  5368. SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
  5369. : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {}
  5370. };
  5371. }
  5372. bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
  5373. // Treat an enum type as its underlying type.
  5374. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  5375. Ty = EnumTy->getDecl()->getIntegerType();
  5376. // Promotable integer types are required to be promoted by the ABI.
  5377. if (Ty->isPromotableIntegerType())
  5378. return true;
  5379. // 32-bit values must also be promoted.
  5380. if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
  5381. switch (BT->getKind()) {
  5382. case BuiltinType::Int:
  5383. case BuiltinType::UInt:
  5384. return true;
  5385. default:
  5386. return false;
  5387. }
  5388. return false;
  5389. }
  5390. bool SystemZABIInfo::isCompoundType(QualType Ty) const {
  5391. return (Ty->isAnyComplexType() ||
  5392. Ty->isVectorType() ||
  5393. isAggregateTypeForABI(Ty));
  5394. }
  5395. bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
  5396. return (HasVector &&
  5397. Ty->isVectorType() &&
  5398. getContext().getTypeSize(Ty) <= 128);
  5399. }
  5400. bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
  5401. if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
  5402. switch (BT->getKind()) {
  5403. case BuiltinType::Float:
  5404. case BuiltinType::Double:
  5405. return true;
  5406. default:
  5407. return false;
  5408. }
  5409. return false;
  5410. }
  5411. QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
  5412. if (const RecordType *RT = Ty->getAsStructureType()) {
  5413. const RecordDecl *RD = RT->getDecl();
  5414. QualType Found;
  5415. // If this is a C++ record, check the bases first.
  5416. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  5417. for (const auto &I : CXXRD->bases()) {
  5418. QualType Base = I.getType();
  5419. // Empty bases don't affect things either way.
  5420. if (isEmptyRecord(getContext(), Base, true))
  5421. continue;
  5422. if (!Found.isNull())
  5423. return Ty;
  5424. Found = GetSingleElementType(Base);
  5425. }
  5426. // Check the fields.
  5427. for (const auto *FD : RD->fields()) {
  5428. // For compatibility with GCC, ignore empty bitfields in C++ mode.
  5429. // Unlike isSingleElementStruct(), empty structure and array fields
  5430. // do count. So do anonymous bitfields that aren't zero-sized.
  5431. if (getContext().getLangOpts().CPlusPlus &&
  5432. FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
  5433. continue;
  5434. // Unlike isSingleElementStruct(), arrays do not count.
  5435. // Nested structures still do though.
  5436. if (!Found.isNull())
  5437. return Ty;
  5438. Found = GetSingleElementType(FD->getType());
  5439. }
  5440. // Unlike isSingleElementStruct(), trailing padding is allowed.
  5441. // An 8-byte aligned struct s { float f; } is passed as a double.
  5442. if (!Found.isNull())
  5443. return Found;
  5444. }
  5445. return Ty;
  5446. }
  5447. Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5448. QualType Ty) const {
  5449. // Assume that va_list type is correct; should be pointer to LLVM type:
  5450. // struct {
  5451. // i64 __gpr;
  5452. // i64 __fpr;
  5453. // i8 *__overflow_arg_area;
  5454. // i8 *__reg_save_area;
  5455. // };
  5456. // Every non-vector argument occupies 8 bytes and is passed by preference
  5457. // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are
  5458. // always passed on the stack.
  5459. Ty = getContext().getCanonicalType(Ty);
  5460. auto TyInfo = getContext().getTypeInfoInChars(Ty);
  5461. llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
  5462. llvm::Type *DirectTy = ArgTy;
  5463. ABIArgInfo AI = classifyArgumentType(Ty);
  5464. bool IsIndirect = AI.isIndirect();
  5465. bool InFPRs = false;
  5466. bool IsVector = false;
  5467. CharUnits UnpaddedSize;
  5468. CharUnits DirectAlign;
  5469. if (IsIndirect) {
  5470. DirectTy = llvm::PointerType::getUnqual(DirectTy);
  5471. UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
  5472. } else {
  5473. if (AI.getCoerceToType())
  5474. ArgTy = AI.getCoerceToType();
  5475. InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy();
  5476. IsVector = ArgTy->isVectorTy();
  5477. UnpaddedSize = TyInfo.first;
  5478. DirectAlign = TyInfo.second;
  5479. }
  5480. CharUnits PaddedSize = CharUnits::fromQuantity(8);
  5481. if (IsVector && UnpaddedSize > PaddedSize)
  5482. PaddedSize = CharUnits::fromQuantity(16);
  5483. assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
  5484. CharUnits Padding = (PaddedSize - UnpaddedSize);
  5485. llvm::Type *IndexTy = CGF.Int64Ty;
  5486. llvm::Value *PaddedSizeV =
  5487. llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
  5488. if (IsVector) {
  5489. // Work out the address of a vector argument on the stack.
  5490. // Vector arguments are always passed in the high bits of a
  5491. // single (8 byte) or double (16 byte) stack slot.
  5492. Address OverflowArgAreaPtr =
  5493. CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16),
  5494. "overflow_arg_area_ptr");
  5495. Address OverflowArgArea =
  5496. Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
  5497. TyInfo.second);
  5498. Address MemAddr =
  5499. CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
  5500. // Update overflow_arg_area_ptr pointer
  5501. llvm::Value *NewOverflowArgArea =
  5502. CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
  5503. "overflow_arg_area");
  5504. CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
  5505. return MemAddr;
  5506. }
  5507. assert(PaddedSize.getQuantity() == 8);
  5508. unsigned MaxRegs, RegCountField, RegSaveIndex;
  5509. CharUnits RegPadding;
  5510. if (InFPRs) {
  5511. MaxRegs = 4; // Maximum of 4 FPR arguments
  5512. RegCountField = 1; // __fpr
  5513. RegSaveIndex = 16; // save offset for f0
  5514. RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
  5515. } else {
  5516. MaxRegs = 5; // Maximum of 5 GPR arguments
  5517. RegCountField = 0; // __gpr
  5518. RegSaveIndex = 2; // save offset for r2
  5519. RegPadding = Padding; // values are passed in the low bits of a GPR
  5520. }
  5521. Address RegCountPtr = CGF.Builder.CreateStructGEP(
  5522. VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8),
  5523. "reg_count_ptr");
  5524. llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
  5525. llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
  5526. llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
  5527. "fits_in_regs");
  5528. llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
  5529. llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
  5530. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
  5531. CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
  5532. // Emit code to load the value if it was passed in registers.
  5533. CGF.EmitBlock(InRegBlock);
  5534. // Work out the address of an argument register.
  5535. llvm::Value *ScaledRegCount =
  5536. CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
  5537. llvm::Value *RegBase =
  5538. llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
  5539. + RegPadding.getQuantity());
  5540. llvm::Value *RegOffset =
  5541. CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
  5542. Address RegSaveAreaPtr =
  5543. CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
  5544. "reg_save_area_ptr");
  5545. llvm::Value *RegSaveArea =
  5546. CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
  5547. Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
  5548. "raw_reg_addr"),
  5549. PaddedSize);
  5550. Address RegAddr =
  5551. CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
  5552. // Update the register count
  5553. llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
  5554. llvm::Value *NewRegCount =
  5555. CGF.Builder.CreateAdd(RegCount, One, "reg_count");
  5556. CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
  5557. CGF.EmitBranch(ContBlock);
  5558. // Emit code to load the value if it was passed in memory.
  5559. CGF.EmitBlock(InMemBlock);
  5560. // Work out the address of a stack argument.
  5561. Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP(
  5562. VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr");
  5563. Address OverflowArgArea =
  5564. Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
  5565. PaddedSize);
  5566. Address RawMemAddr =
  5567. CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
  5568. Address MemAddr =
  5569. CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
  5570. // Update overflow_arg_area_ptr pointer
  5571. llvm::Value *NewOverflowArgArea =
  5572. CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
  5573. "overflow_arg_area");
  5574. CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
  5575. CGF.EmitBranch(ContBlock);
  5576. // Return the appropriate result.
  5577. CGF.EmitBlock(ContBlock);
  5578. Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
  5579. MemAddr, InMemBlock, "va_arg.addr");
  5580. if (IsIndirect)
  5581. ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
  5582. TyInfo.second);
  5583. return ResAddr;
  5584. }
  5585. ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
  5586. if (RetTy->isVoidType())
  5587. return ABIArgInfo::getIgnore();
  5588. if (isVectorArgumentType(RetTy))
  5589. return ABIArgInfo::getDirect();
  5590. if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
  5591. return getNaturalAlignIndirect(RetTy);
  5592. return (isPromotableIntegerType(RetTy) ?
  5593. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  5594. }
  5595. ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
  5596. // Handle the generic C++ ABI.
  5597. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  5598. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  5599. // Integers and enums are extended to full register width.
  5600. if (isPromotableIntegerType(Ty))
  5601. return ABIArgInfo::getExtend();
  5602. // Handle vector types and vector-like structure types. Note that
  5603. // as opposed to float-like structure types, we do not allow any
  5604. // padding for vector-like structures, so verify the sizes match.
  5605. uint64_t Size = getContext().getTypeSize(Ty);
  5606. QualType SingleElementTy = GetSingleElementType(Ty);
  5607. if (isVectorArgumentType(SingleElementTy) &&
  5608. getContext().getTypeSize(SingleElementTy) == Size)
  5609. return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
  5610. // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
  5611. if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
  5612. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  5613. // Handle small structures.
  5614. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  5615. // Structures with flexible arrays have variable length, so really
  5616. // fail the size test above.
  5617. const RecordDecl *RD = RT->getDecl();
  5618. if (RD->hasFlexibleArrayMember())
  5619. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  5620. // The structure is passed as an unextended integer, a float, or a double.
  5621. llvm::Type *PassTy;
  5622. if (isFPArgumentType(SingleElementTy)) {
  5623. assert(Size == 32 || Size == 64);
  5624. if (Size == 32)
  5625. PassTy = llvm::Type::getFloatTy(getVMContext());
  5626. else
  5627. PassTy = llvm::Type::getDoubleTy(getVMContext());
  5628. } else
  5629. PassTy = llvm::IntegerType::get(getVMContext(), Size);
  5630. return ABIArgInfo::getDirect(PassTy);
  5631. }
  5632. // Non-structure compounds are passed indirectly.
  5633. if (isCompoundType(Ty))
  5634. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  5635. return ABIArgInfo::getDirect(nullptr);
  5636. }
  5637. //===----------------------------------------------------------------------===//
  5638. // MSP430 ABI Implementation
  5639. //===----------------------------------------------------------------------===//
  5640. namespace {
  5641. class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
  5642. public:
  5643. MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
  5644. : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
  5645. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  5646. CodeGen::CodeGenModule &M,
  5647. ForDefinition_t IsForDefinition) const override;
  5648. };
  5649. }
  5650. void MSP430TargetCodeGenInfo::setTargetAttributes(
  5651. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M,
  5652. ForDefinition_t IsForDefinition) const {
  5653. if (!IsForDefinition)
  5654. return;
  5655. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  5656. if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
  5657. // Handle 'interrupt' attribute:
  5658. llvm::Function *F = cast<llvm::Function>(GV);
  5659. // Step 1: Set ISR calling convention.
  5660. F->setCallingConv(llvm::CallingConv::MSP430_INTR);
  5661. // Step 2: Add attributes goodness.
  5662. F->addFnAttr(llvm::Attribute::NoInline);
  5663. // Step 3: Emit ISR vector alias.
  5664. unsigned Num = attr->getNumber() / 2;
  5665. llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
  5666. "__isr_" + Twine(Num), F);
  5667. }
  5668. }
  5669. }
  5670. //===----------------------------------------------------------------------===//
  5671. // MIPS ABI Implementation. This works for both little-endian and
  5672. // big-endian variants.
  5673. //===----------------------------------------------------------------------===//
  5674. namespace {
  5675. class MipsABIInfo : public ABIInfo {
  5676. bool IsO32;
  5677. unsigned MinABIStackAlignInBytes, StackAlignInBytes;
  5678. void CoerceToIntArgs(uint64_t TySize,
  5679. SmallVectorImpl<llvm::Type *> &ArgList) const;
  5680. llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
  5681. llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
  5682. llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
  5683. public:
  5684. MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
  5685. ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
  5686. StackAlignInBytes(IsO32 ? 8 : 16) {}
  5687. ABIArgInfo classifyReturnType(QualType RetTy) const;
  5688. ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
  5689. void computeInfo(CGFunctionInfo &FI) const override;
  5690. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5691. QualType Ty) const override;
  5692. bool shouldSignExtUnsignedType(QualType Ty) const override;
  5693. };
  5694. class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
  5695. unsigned SizeOfUnwindException;
  5696. public:
  5697. MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
  5698. : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
  5699. SizeOfUnwindException(IsO32 ? 24 : 32) {}
  5700. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
  5701. return 29;
  5702. }
  5703. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  5704. CodeGen::CodeGenModule &CGM,
  5705. ForDefinition_t IsForDefinition) const override {
  5706. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  5707. if (!FD) return;
  5708. llvm::Function *Fn = cast<llvm::Function>(GV);
  5709. if (FD->hasAttr<MipsLongCallAttr>())
  5710. Fn->addFnAttr("long-call");
  5711. else if (FD->hasAttr<MipsShortCallAttr>())
  5712. Fn->addFnAttr("short-call");
  5713. // Other attributes do not have a meaning for declarations.
  5714. if (!IsForDefinition)
  5715. return;
  5716. if (FD->hasAttr<Mips16Attr>()) {
  5717. Fn->addFnAttr("mips16");
  5718. }
  5719. else if (FD->hasAttr<NoMips16Attr>()) {
  5720. Fn->addFnAttr("nomips16");
  5721. }
  5722. if (FD->hasAttr<MicroMipsAttr>())
  5723. Fn->addFnAttr("micromips");
  5724. else if (FD->hasAttr<NoMicroMipsAttr>())
  5725. Fn->addFnAttr("nomicromips");
  5726. const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
  5727. if (!Attr)
  5728. return;
  5729. const char *Kind;
  5730. switch (Attr->getInterrupt()) {
  5731. case MipsInterruptAttr::eic: Kind = "eic"; break;
  5732. case MipsInterruptAttr::sw0: Kind = "sw0"; break;
  5733. case MipsInterruptAttr::sw1: Kind = "sw1"; break;
  5734. case MipsInterruptAttr::hw0: Kind = "hw0"; break;
  5735. case MipsInterruptAttr::hw1: Kind = "hw1"; break;
  5736. case MipsInterruptAttr::hw2: Kind = "hw2"; break;
  5737. case MipsInterruptAttr::hw3: Kind = "hw3"; break;
  5738. case MipsInterruptAttr::hw4: Kind = "hw4"; break;
  5739. case MipsInterruptAttr::hw5: Kind = "hw5"; break;
  5740. }
  5741. Fn->addFnAttr("interrupt", Kind);
  5742. }
  5743. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  5744. llvm::Value *Address) const override;
  5745. unsigned getSizeOfUnwindException() const override {
  5746. return SizeOfUnwindException;
  5747. }
  5748. };
  5749. }
  5750. void MipsABIInfo::CoerceToIntArgs(
  5751. uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
  5752. llvm::IntegerType *IntTy =
  5753. llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
  5754. // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
  5755. for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
  5756. ArgList.push_back(IntTy);
  5757. // If necessary, add one more integer type to ArgList.
  5758. unsigned R = TySize % (MinABIStackAlignInBytes * 8);
  5759. if (R)
  5760. ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
  5761. }
  5762. // In N32/64, an aligned double precision floating point field is passed in
  5763. // a register.
  5764. llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
  5765. SmallVector<llvm::Type*, 8> ArgList, IntArgList;
  5766. if (IsO32) {
  5767. CoerceToIntArgs(TySize, ArgList);
  5768. return llvm::StructType::get(getVMContext(), ArgList);
  5769. }
  5770. if (Ty->isComplexType())
  5771. return CGT.ConvertType(Ty);
  5772. const RecordType *RT = Ty->getAs<RecordType>();
  5773. // Unions/vectors are passed in integer registers.
  5774. if (!RT || !RT->isStructureOrClassType()) {
  5775. CoerceToIntArgs(TySize, ArgList);
  5776. return llvm::StructType::get(getVMContext(), ArgList);
  5777. }
  5778. const RecordDecl *RD = RT->getDecl();
  5779. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  5780. assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
  5781. uint64_t LastOffset = 0;
  5782. unsigned idx = 0;
  5783. llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
  5784. // Iterate over fields in the struct/class and check if there are any aligned
  5785. // double fields.
  5786. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  5787. i != e; ++i, ++idx) {
  5788. const QualType Ty = i->getType();
  5789. const BuiltinType *BT = Ty->getAs<BuiltinType>();
  5790. if (!BT || BT->getKind() != BuiltinType::Double)
  5791. continue;
  5792. uint64_t Offset = Layout.getFieldOffset(idx);
  5793. if (Offset % 64) // Ignore doubles that are not aligned.
  5794. continue;
  5795. // Add ((Offset - LastOffset) / 64) args of type i64.
  5796. for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
  5797. ArgList.push_back(I64);
  5798. // Add double type.
  5799. ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
  5800. LastOffset = Offset + 64;
  5801. }
  5802. CoerceToIntArgs(TySize - LastOffset, IntArgList);
  5803. ArgList.append(IntArgList.begin(), IntArgList.end());
  5804. return llvm::StructType::get(getVMContext(), ArgList);
  5805. }
  5806. llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
  5807. uint64_t Offset) const {
  5808. if (OrigOffset + MinABIStackAlignInBytes > Offset)
  5809. return nullptr;
  5810. return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
  5811. }
  5812. ABIArgInfo
  5813. MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
  5814. Ty = useFirstFieldIfTransparentUnion(Ty);
  5815. uint64_t OrigOffset = Offset;
  5816. uint64_t TySize = getContext().getTypeSize(Ty);
  5817. uint64_t Align = getContext().getTypeAlign(Ty) / 8;
  5818. Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
  5819. (uint64_t)StackAlignInBytes);
  5820. unsigned CurrOffset = llvm::alignTo(Offset, Align);
  5821. Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
  5822. if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
  5823. // Ignore empty aggregates.
  5824. if (TySize == 0)
  5825. return ABIArgInfo::getIgnore();
  5826. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  5827. Offset = OrigOffset + MinABIStackAlignInBytes;
  5828. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  5829. }
  5830. // If we have reached here, aggregates are passed directly by coercing to
  5831. // another structure type. Padding is inserted if the offset of the
  5832. // aggregate is unaligned.
  5833. ABIArgInfo ArgInfo =
  5834. ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
  5835. getPaddingType(OrigOffset, CurrOffset));
  5836. ArgInfo.setInReg(true);
  5837. return ArgInfo;
  5838. }
  5839. // Treat an enum type as its underlying type.
  5840. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  5841. Ty = EnumTy->getDecl()->getIntegerType();
  5842. // All integral types are promoted to the GPR width.
  5843. if (Ty->isIntegralOrEnumerationType())
  5844. return ABIArgInfo::getExtend();
  5845. return ABIArgInfo::getDirect(
  5846. nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
  5847. }
  5848. llvm::Type*
  5849. MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
  5850. const RecordType *RT = RetTy->getAs<RecordType>();
  5851. SmallVector<llvm::Type*, 8> RTList;
  5852. if (RT && RT->isStructureOrClassType()) {
  5853. const RecordDecl *RD = RT->getDecl();
  5854. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  5855. unsigned FieldCnt = Layout.getFieldCount();
  5856. // N32/64 returns struct/classes in floating point registers if the
  5857. // following conditions are met:
  5858. // 1. The size of the struct/class is no larger than 128-bit.
  5859. // 2. The struct/class has one or two fields all of which are floating
  5860. // point types.
  5861. // 3. The offset of the first field is zero (this follows what gcc does).
  5862. //
  5863. // Any other composite results are returned in integer registers.
  5864. //
  5865. if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
  5866. RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
  5867. for (; b != e; ++b) {
  5868. const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
  5869. if (!BT || !BT->isFloatingPoint())
  5870. break;
  5871. RTList.push_back(CGT.ConvertType(b->getType()));
  5872. }
  5873. if (b == e)
  5874. return llvm::StructType::get(getVMContext(), RTList,
  5875. RD->hasAttr<PackedAttr>());
  5876. RTList.clear();
  5877. }
  5878. }
  5879. CoerceToIntArgs(Size, RTList);
  5880. return llvm::StructType::get(getVMContext(), RTList);
  5881. }
  5882. ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
  5883. uint64_t Size = getContext().getTypeSize(RetTy);
  5884. if (RetTy->isVoidType())
  5885. return ABIArgInfo::getIgnore();
  5886. // O32 doesn't treat zero-sized structs differently from other structs.
  5887. // However, N32/N64 ignores zero sized return values.
  5888. if (!IsO32 && Size == 0)
  5889. return ABIArgInfo::getIgnore();
  5890. if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
  5891. if (Size <= 128) {
  5892. if (RetTy->isAnyComplexType())
  5893. return ABIArgInfo::getDirect();
  5894. // O32 returns integer vectors in registers and N32/N64 returns all small
  5895. // aggregates in registers.
  5896. if (!IsO32 ||
  5897. (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
  5898. ABIArgInfo ArgInfo =
  5899. ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
  5900. ArgInfo.setInReg(true);
  5901. return ArgInfo;
  5902. }
  5903. }
  5904. return getNaturalAlignIndirect(RetTy);
  5905. }
  5906. // Treat an enum type as its underlying type.
  5907. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  5908. RetTy = EnumTy->getDecl()->getIntegerType();
  5909. return (RetTy->isPromotableIntegerType() ?
  5910. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  5911. }
  5912. void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
  5913. ABIArgInfo &RetInfo = FI.getReturnInfo();
  5914. if (!getCXXABI().classifyReturnType(FI))
  5915. RetInfo = classifyReturnType(FI.getReturnType());
  5916. // Check if a pointer to an aggregate is passed as a hidden argument.
  5917. uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
  5918. for (auto &I : FI.arguments())
  5919. I.info = classifyArgumentType(I.type, Offset);
  5920. }
  5921. Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5922. QualType OrigTy) const {
  5923. QualType Ty = OrigTy;
  5924. // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
  5925. // Pointers are also promoted in the same way but this only matters for N32.
  5926. unsigned SlotSizeInBits = IsO32 ? 32 : 64;
  5927. unsigned PtrWidth = getTarget().getPointerWidth(0);
  5928. bool DidPromote = false;
  5929. if ((Ty->isIntegerType() &&
  5930. getContext().getIntWidth(Ty) < SlotSizeInBits) ||
  5931. (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
  5932. DidPromote = true;
  5933. Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
  5934. Ty->isSignedIntegerType());
  5935. }
  5936. auto TyInfo = getContext().getTypeInfoInChars(Ty);
  5937. // The alignment of things in the argument area is never larger than
  5938. // StackAlignInBytes.
  5939. TyInfo.second =
  5940. std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
  5941. // MinABIStackAlignInBytes is the size of argument slots on the stack.
  5942. CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
  5943. Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
  5944. TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
  5945. // If there was a promotion, "unpromote" into a temporary.
  5946. // TODO: can we just use a pointer into a subset of the original slot?
  5947. if (DidPromote) {
  5948. Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
  5949. llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
  5950. // Truncate down to the right width.
  5951. llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
  5952. : CGF.IntPtrTy);
  5953. llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
  5954. if (OrigTy->isPointerType())
  5955. V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
  5956. CGF.Builder.CreateStore(V, Temp);
  5957. Addr = Temp;
  5958. }
  5959. return Addr;
  5960. }
  5961. bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
  5962. int TySize = getContext().getTypeSize(Ty);
  5963. // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
  5964. if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
  5965. return true;
  5966. return false;
  5967. }
  5968. bool
  5969. MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  5970. llvm::Value *Address) const {
  5971. // This information comes from gcc's implementation, which seems to
  5972. // as canonical as it gets.
  5973. // Everything on MIPS is 4 bytes. Double-precision FP registers
  5974. // are aliased to pairs of single-precision FP registers.
  5975. llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
  5976. // 0-31 are the general purpose registers, $0 - $31.
  5977. // 32-63 are the floating-point registers, $f0 - $f31.
  5978. // 64 and 65 are the multiply/divide registers, $hi and $lo.
  5979. // 66 is the (notional, I think) register for signal-handler return.
  5980. AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
  5981. // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
  5982. // They are one bit wide and ignored here.
  5983. // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
  5984. // (coprocessor 1 is the FP unit)
  5985. // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
  5986. // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
  5987. // 176-181 are the DSP accumulator registers.
  5988. AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
  5989. return false;
  5990. }
  5991. //===----------------------------------------------------------------------===//
  5992. // AVR ABI Implementation.
  5993. //===----------------------------------------------------------------------===//
  5994. namespace {
  5995. class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
  5996. public:
  5997. AVRTargetCodeGenInfo(CodeGenTypes &CGT)
  5998. : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { }
  5999. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  6000. CodeGen::CodeGenModule &CGM,
  6001. ForDefinition_t IsForDefinition) const override {
  6002. if (!IsForDefinition)
  6003. return;
  6004. const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
  6005. if (!FD) return;
  6006. auto *Fn = cast<llvm::Function>(GV);
  6007. if (FD->getAttr<AVRInterruptAttr>())
  6008. Fn->addFnAttr("interrupt");
  6009. if (FD->getAttr<AVRSignalAttr>())
  6010. Fn->addFnAttr("signal");
  6011. }
  6012. };
  6013. }
  6014. //===----------------------------------------------------------------------===//
  6015. // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
  6016. // Currently subclassed only to implement custom OpenCL C function attribute
  6017. // handling.
  6018. //===----------------------------------------------------------------------===//
  6019. namespace {
  6020. class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
  6021. public:
  6022. TCETargetCodeGenInfo(CodeGenTypes &CGT)
  6023. : DefaultTargetCodeGenInfo(CGT) {}
  6024. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  6025. CodeGen::CodeGenModule &M,
  6026. ForDefinition_t IsForDefinition) const override;
  6027. };
  6028. void TCETargetCodeGenInfo::setTargetAttributes(
  6029. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M,
  6030. ForDefinition_t IsForDefinition) const {
  6031. if (!IsForDefinition)
  6032. return;
  6033. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  6034. if (!FD) return;
  6035. llvm::Function *F = cast<llvm::Function>(GV);
  6036. if (M.getLangOpts().OpenCL) {
  6037. if (FD->hasAttr<OpenCLKernelAttr>()) {
  6038. // OpenCL C Kernel functions are not subject to inlining
  6039. F->addFnAttr(llvm::Attribute::NoInline);
  6040. const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
  6041. if (Attr) {
  6042. // Convert the reqd_work_group_size() attributes to metadata.
  6043. llvm::LLVMContext &Context = F->getContext();
  6044. llvm::NamedMDNode *OpenCLMetadata =
  6045. M.getModule().getOrInsertNamedMetadata(
  6046. "opencl.kernel_wg_size_info");
  6047. SmallVector<llvm::Metadata *, 5> Operands;
  6048. Operands.push_back(llvm::ConstantAsMetadata::get(F));
  6049. Operands.push_back(
  6050. llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
  6051. M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
  6052. Operands.push_back(
  6053. llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
  6054. M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
  6055. Operands.push_back(
  6056. llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
  6057. M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
  6058. // Add a boolean constant operand for "required" (true) or "hint"
  6059. // (false) for implementing the work_group_size_hint attr later.
  6060. // Currently always true as the hint is not yet implemented.
  6061. Operands.push_back(
  6062. llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
  6063. OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
  6064. }
  6065. }
  6066. }
  6067. }
  6068. }
  6069. //===----------------------------------------------------------------------===//
  6070. // Hexagon ABI Implementation
  6071. //===----------------------------------------------------------------------===//
  6072. namespace {
  6073. class HexagonABIInfo : public ABIInfo {
  6074. public:
  6075. HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
  6076. private:
  6077. ABIArgInfo classifyReturnType(QualType RetTy) const;
  6078. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  6079. void computeInfo(CGFunctionInfo &FI) const override;
  6080. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6081. QualType Ty) const override;
  6082. };
  6083. class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
  6084. public:
  6085. HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
  6086. :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
  6087. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  6088. return 29;
  6089. }
  6090. };
  6091. }
  6092. void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
  6093. if (!getCXXABI().classifyReturnType(FI))
  6094. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  6095. for (auto &I : FI.arguments())
  6096. I.info = classifyArgumentType(I.type);
  6097. }
  6098. ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
  6099. if (!isAggregateTypeForABI(Ty)) {
  6100. // Treat an enum type as its underlying type.
  6101. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  6102. Ty = EnumTy->getDecl()->getIntegerType();
  6103. return (Ty->isPromotableIntegerType() ?
  6104. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  6105. }
  6106. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  6107. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  6108. // Ignore empty records.
  6109. if (isEmptyRecord(getContext(), Ty, true))
  6110. return ABIArgInfo::getIgnore();
  6111. uint64_t Size = getContext().getTypeSize(Ty);
  6112. if (Size > 64)
  6113. return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
  6114. // Pass in the smallest viable integer type.
  6115. else if (Size > 32)
  6116. return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
  6117. else if (Size > 16)
  6118. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  6119. else if (Size > 8)
  6120. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  6121. else
  6122. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  6123. }
  6124. ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
  6125. if (RetTy->isVoidType())
  6126. return ABIArgInfo::getIgnore();
  6127. // Large vector types should be returned via memory.
  6128. if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
  6129. return getNaturalAlignIndirect(RetTy);
  6130. if (!isAggregateTypeForABI(RetTy)) {
  6131. // Treat an enum type as its underlying type.
  6132. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  6133. RetTy = EnumTy->getDecl()->getIntegerType();
  6134. return (RetTy->isPromotableIntegerType() ?
  6135. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  6136. }
  6137. if (isEmptyRecord(getContext(), RetTy, true))
  6138. return ABIArgInfo::getIgnore();
  6139. // Aggregates <= 8 bytes are returned in r0; other aggregates
  6140. // are returned indirectly.
  6141. uint64_t Size = getContext().getTypeSize(RetTy);
  6142. if (Size <= 64) {
  6143. // Return in the smallest viable integer type.
  6144. if (Size <= 8)
  6145. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  6146. if (Size <= 16)
  6147. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  6148. if (Size <= 32)
  6149. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  6150. return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
  6151. }
  6152. return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
  6153. }
  6154. Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6155. QualType Ty) const {
  6156. // FIXME: Someone needs to audit that this handle alignment correctly.
  6157. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
  6158. getContext().getTypeInfoInChars(Ty),
  6159. CharUnits::fromQuantity(4),
  6160. /*AllowHigherAlign*/ true);
  6161. }
  6162. //===----------------------------------------------------------------------===//
  6163. // Lanai ABI Implementation
  6164. //===----------------------------------------------------------------------===//
  6165. namespace {
  6166. class LanaiABIInfo : public DefaultABIInfo {
  6167. public:
  6168. LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  6169. bool shouldUseInReg(QualType Ty, CCState &State) const;
  6170. void computeInfo(CGFunctionInfo &FI) const override {
  6171. CCState State(FI.getCallingConvention());
  6172. // Lanai uses 4 registers to pass arguments unless the function has the
  6173. // regparm attribute set.
  6174. if (FI.getHasRegParm()) {
  6175. State.FreeRegs = FI.getRegParm();
  6176. } else {
  6177. State.FreeRegs = 4;
  6178. }
  6179. if (!getCXXABI().classifyReturnType(FI))
  6180. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  6181. for (auto &I : FI.arguments())
  6182. I.info = classifyArgumentType(I.type, State);
  6183. }
  6184. ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
  6185. ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
  6186. };
  6187. } // end anonymous namespace
  6188. bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
  6189. unsigned Size = getContext().getTypeSize(Ty);
  6190. unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
  6191. if (SizeInRegs == 0)
  6192. return false;
  6193. if (SizeInRegs > State.FreeRegs) {
  6194. State.FreeRegs = 0;
  6195. return false;
  6196. }
  6197. State.FreeRegs -= SizeInRegs;
  6198. return true;
  6199. }
  6200. ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
  6201. CCState &State) const {
  6202. if (!ByVal) {
  6203. if (State.FreeRegs) {
  6204. --State.FreeRegs; // Non-byval indirects just use one pointer.
  6205. return getNaturalAlignIndirectInReg(Ty);
  6206. }
  6207. return getNaturalAlignIndirect(Ty, false);
  6208. }
  6209. // Compute the byval alignment.
  6210. const unsigned MinABIStackAlignInBytes = 4;
  6211. unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
  6212. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
  6213. /*Realign=*/TypeAlign >
  6214. MinABIStackAlignInBytes);
  6215. }
  6216. ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
  6217. CCState &State) const {
  6218. // Check with the C++ ABI first.
  6219. const RecordType *RT = Ty->getAs<RecordType>();
  6220. if (RT) {
  6221. CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
  6222. if (RAA == CGCXXABI::RAA_Indirect) {
  6223. return getIndirectResult(Ty, /*ByVal=*/false, State);
  6224. } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
  6225. return getNaturalAlignIndirect(Ty, /*ByRef=*/true);
  6226. }
  6227. }
  6228. if (isAggregateTypeForABI(Ty)) {
  6229. // Structures with flexible arrays are always indirect.
  6230. if (RT && RT->getDecl()->hasFlexibleArrayMember())
  6231. return getIndirectResult(Ty, /*ByVal=*/true, State);
  6232. // Ignore empty structs/unions.
  6233. if (isEmptyRecord(getContext(), Ty, true))
  6234. return ABIArgInfo::getIgnore();
  6235. llvm::LLVMContext &LLVMContext = getVMContext();
  6236. unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
  6237. if (SizeInRegs <= State.FreeRegs) {
  6238. llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
  6239. SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
  6240. llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
  6241. State.FreeRegs -= SizeInRegs;
  6242. return ABIArgInfo::getDirectInReg(Result);
  6243. } else {
  6244. State.FreeRegs = 0;
  6245. }
  6246. return getIndirectResult(Ty, true, State);
  6247. }
  6248. // Treat an enum type as its underlying type.
  6249. if (const auto *EnumTy = Ty->getAs<EnumType>())
  6250. Ty = EnumTy->getDecl()->getIntegerType();
  6251. bool InReg = shouldUseInReg(Ty, State);
  6252. if (Ty->isPromotableIntegerType()) {
  6253. if (InReg)
  6254. return ABIArgInfo::getDirectInReg();
  6255. return ABIArgInfo::getExtend();
  6256. }
  6257. if (InReg)
  6258. return ABIArgInfo::getDirectInReg();
  6259. return ABIArgInfo::getDirect();
  6260. }
  6261. namespace {
  6262. class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
  6263. public:
  6264. LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  6265. : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {}
  6266. };
  6267. }
  6268. //===----------------------------------------------------------------------===//
  6269. // AMDGPU ABI Implementation
  6270. //===----------------------------------------------------------------------===//
  6271. namespace {
  6272. class AMDGPUABIInfo final : public DefaultABIInfo {
  6273. private:
  6274. static const unsigned MaxNumRegsForArgsRet = 16;
  6275. unsigned numRegsForType(QualType Ty) const;
  6276. bool isHomogeneousAggregateBaseType(QualType Ty) const override;
  6277. bool isHomogeneousAggregateSmallEnough(const Type *Base,
  6278. uint64_t Members) const override;
  6279. public:
  6280. explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
  6281. DefaultABIInfo(CGT) {}
  6282. ABIArgInfo classifyReturnType(QualType RetTy) const;
  6283. ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
  6284. ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
  6285. void computeInfo(CGFunctionInfo &FI) const override;
  6286. };
  6287. bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  6288. return true;
  6289. }
  6290. bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
  6291. const Type *Base, uint64_t Members) const {
  6292. uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
  6293. // Homogeneous Aggregates may occupy at most 16 registers.
  6294. return Members * NumRegs <= MaxNumRegsForArgsRet;
  6295. }
  6296. /// Estimate number of registers the type will use when passed in registers.
  6297. unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
  6298. unsigned NumRegs = 0;
  6299. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  6300. // Compute from the number of elements. The reported size is based on the
  6301. // in-memory size, which includes the padding 4th element for 3-vectors.
  6302. QualType EltTy = VT->getElementType();
  6303. unsigned EltSize = getContext().getTypeSize(EltTy);
  6304. // 16-bit element vectors should be passed as packed.
  6305. if (EltSize == 16)
  6306. return (VT->getNumElements() + 1) / 2;
  6307. unsigned EltNumRegs = (EltSize + 31) / 32;
  6308. return EltNumRegs * VT->getNumElements();
  6309. }
  6310. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  6311. const RecordDecl *RD = RT->getDecl();
  6312. assert(!RD->hasFlexibleArrayMember());
  6313. for (const FieldDecl *Field : RD->fields()) {
  6314. QualType FieldTy = Field->getType();
  6315. NumRegs += numRegsForType(FieldTy);
  6316. }
  6317. return NumRegs;
  6318. }
  6319. return (getContext().getTypeSize(Ty) + 31) / 32;
  6320. }
  6321. void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
  6322. llvm::CallingConv::ID CC = FI.getCallingConvention();
  6323. if (!getCXXABI().classifyReturnType(FI))
  6324. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  6325. unsigned NumRegsLeft = MaxNumRegsForArgsRet;
  6326. for (auto &Arg : FI.arguments()) {
  6327. if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
  6328. Arg.info = classifyKernelArgumentType(Arg.type);
  6329. } else {
  6330. Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
  6331. }
  6332. }
  6333. }
  6334. ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
  6335. if (isAggregateTypeForABI(RetTy)) {
  6336. // Records with non-trivial destructors/copy-constructors should not be
  6337. // returned by value.
  6338. if (!getRecordArgABI(RetTy, getCXXABI())) {
  6339. // Ignore empty structs/unions.
  6340. if (isEmptyRecord(getContext(), RetTy, true))
  6341. return ABIArgInfo::getIgnore();
  6342. // Lower single-element structs to just return a regular value.
  6343. if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
  6344. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  6345. if (const RecordType *RT = RetTy->getAs<RecordType>()) {
  6346. const RecordDecl *RD = RT->getDecl();
  6347. if (RD->hasFlexibleArrayMember())
  6348. return DefaultABIInfo::classifyReturnType(RetTy);
  6349. }
  6350. // Pack aggregates <= 4 bytes into single VGPR or pair.
  6351. uint64_t Size = getContext().getTypeSize(RetTy);
  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. if (Size <= 64) {
  6357. llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
  6358. return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
  6359. }
  6360. if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
  6361. return ABIArgInfo::getDirect();
  6362. }
  6363. }
  6364. // Otherwise just do the default thing.
  6365. return DefaultABIInfo::classifyReturnType(RetTy);
  6366. }
  6367. /// For kernels all parameters are really passed in a special buffer. It doesn't
  6368. /// make sense to pass anything byval, so everything must be direct.
  6369. ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
  6370. Ty = useFirstFieldIfTransparentUnion(Ty);
  6371. // TODO: Can we omit empty structs?
  6372. // Coerce single element structs to its element.
  6373. if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
  6374. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  6375. // If we set CanBeFlattened to true, CodeGen will expand the struct to its
  6376. // individual elements, which confuses the Clover OpenCL backend; therefore we
  6377. // have to set it to false here. Other args of getDirect() are just defaults.
  6378. return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
  6379. }
  6380. ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
  6381. unsigned &NumRegsLeft) const {
  6382. assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
  6383. Ty = useFirstFieldIfTransparentUnion(Ty);
  6384. if (isAggregateTypeForABI(Ty)) {
  6385. // Records with non-trivial destructors/copy-constructors should not be
  6386. // passed by value.
  6387. if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
  6388. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  6389. // Ignore empty structs/unions.
  6390. if (isEmptyRecord(getContext(), Ty, true))
  6391. return ABIArgInfo::getIgnore();
  6392. // Lower single-element structs to just pass a regular value. TODO: We
  6393. // could do reasonable-size multiple-element structs too, using getExpand(),
  6394. // though watch out for things like bitfields.
  6395. if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
  6396. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  6397. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  6398. const RecordDecl *RD = RT->getDecl();
  6399. if (RD->hasFlexibleArrayMember())
  6400. return DefaultABIInfo::classifyArgumentType(Ty);
  6401. }
  6402. // Pack aggregates <= 8 bytes into single VGPR or pair.
  6403. uint64_t Size = getContext().getTypeSize(Ty);
  6404. if (Size <= 64) {
  6405. unsigned NumRegs = (Size + 31) / 32;
  6406. NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
  6407. if (Size <= 16)
  6408. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  6409. if (Size <= 32)
  6410. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  6411. // XXX: Should this be i64 instead, and should the limit increase?
  6412. llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
  6413. return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
  6414. }
  6415. if (NumRegsLeft > 0) {
  6416. unsigned NumRegs = numRegsForType(Ty);
  6417. if (NumRegsLeft >= NumRegs) {
  6418. NumRegsLeft -= NumRegs;
  6419. return ABIArgInfo::getDirect();
  6420. }
  6421. }
  6422. }
  6423. // Otherwise just do the default thing.
  6424. ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
  6425. if (!ArgInfo.isIndirect()) {
  6426. unsigned NumRegs = numRegsForType(Ty);
  6427. NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
  6428. }
  6429. return ArgInfo;
  6430. }
  6431. class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
  6432. public:
  6433. AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
  6434. : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {}
  6435. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  6436. CodeGen::CodeGenModule &M,
  6437. ForDefinition_t IsForDefinition) const override;
  6438. unsigned getOpenCLKernelCallingConv() const override;
  6439. llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
  6440. llvm::PointerType *T, QualType QT) const override;
  6441. LangAS getASTAllocaAddressSpace() const override {
  6442. return getLangASFromTargetAS(
  6443. getABIInfo().getDataLayout().getAllocaAddrSpace());
  6444. }
  6445. LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
  6446. const VarDecl *D) const override;
  6447. llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S,
  6448. llvm::LLVMContext &C) const override;
  6449. llvm::Function *
  6450. createEnqueuedBlockKernel(CodeGenFunction &CGF,
  6451. llvm::Function *BlockInvokeFunc,
  6452. llvm::Value *BlockLiteral) const override;
  6453. };
  6454. }
  6455. void AMDGPUTargetCodeGenInfo::setTargetAttributes(
  6456. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M,
  6457. ForDefinition_t IsForDefinition) const {
  6458. if (!IsForDefinition)
  6459. return;
  6460. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  6461. if (!FD)
  6462. return;
  6463. llvm::Function *F = cast<llvm::Function>(GV);
  6464. const auto *ReqdWGS = M.getLangOpts().OpenCL ?
  6465. FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
  6466. const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
  6467. if (ReqdWGS || FlatWGS) {
  6468. unsigned Min = FlatWGS ? FlatWGS->getMin() : 0;
  6469. unsigned Max = FlatWGS ? FlatWGS->getMax() : 0;
  6470. if (ReqdWGS && Min == 0 && Max == 0)
  6471. Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
  6472. if (Min != 0) {
  6473. assert(Min <= Max && "Min must be less than or equal Max");
  6474. std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
  6475. F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
  6476. } else
  6477. assert(Max == 0 && "Max must be zero");
  6478. }
  6479. if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
  6480. unsigned Min = Attr->getMin();
  6481. unsigned Max = Attr->getMax();
  6482. if (Min != 0) {
  6483. assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
  6484. std::string AttrVal = llvm::utostr(Min);
  6485. if (Max != 0)
  6486. AttrVal = AttrVal + "," + llvm::utostr(Max);
  6487. F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
  6488. } else
  6489. assert(Max == 0 && "Max must be zero");
  6490. }
  6491. if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
  6492. unsigned NumSGPR = Attr->getNumSGPR();
  6493. if (NumSGPR != 0)
  6494. F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
  6495. }
  6496. if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
  6497. uint32_t NumVGPR = Attr->getNumVGPR();
  6498. if (NumVGPR != 0)
  6499. F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
  6500. }
  6501. }
  6502. unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
  6503. return llvm::CallingConv::AMDGPU_KERNEL;
  6504. }
  6505. // Currently LLVM assumes null pointers always have value 0,
  6506. // which results in incorrectly transformed IR. Therefore, instead of
  6507. // emitting null pointers in private and local address spaces, a null
  6508. // pointer in generic address space is emitted which is casted to a
  6509. // pointer in local or private address space.
  6510. llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
  6511. const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
  6512. QualType QT) const {
  6513. if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
  6514. return llvm::ConstantPointerNull::get(PT);
  6515. auto &Ctx = CGM.getContext();
  6516. auto NPT = llvm::PointerType::get(PT->getElementType(),
  6517. Ctx.getTargetAddressSpace(LangAS::opencl_generic));
  6518. return llvm::ConstantExpr::getAddrSpaceCast(
  6519. llvm::ConstantPointerNull::get(NPT), PT);
  6520. }
  6521. LangAS
  6522. AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
  6523. const VarDecl *D) const {
  6524. assert(!CGM.getLangOpts().OpenCL &&
  6525. !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
  6526. "Address space agnostic languages only");
  6527. LangAS DefaultGlobalAS = getLangASFromTargetAS(
  6528. CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
  6529. if (!D)
  6530. return DefaultGlobalAS;
  6531. LangAS AddrSpace = D->getType().getAddressSpace();
  6532. assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace));
  6533. if (AddrSpace != LangAS::Default)
  6534. return AddrSpace;
  6535. if (CGM.isTypeConstant(D->getType(), false)) {
  6536. if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
  6537. return ConstAS.getValue();
  6538. }
  6539. return DefaultGlobalAS;
  6540. }
  6541. llvm::SyncScope::ID
  6542. AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S,
  6543. llvm::LLVMContext &C) const {
  6544. StringRef Name;
  6545. switch (S) {
  6546. case SyncScope::OpenCLWorkGroup:
  6547. Name = "workgroup";
  6548. break;
  6549. case SyncScope::OpenCLDevice:
  6550. Name = "agent";
  6551. break;
  6552. case SyncScope::OpenCLAllSVMDevices:
  6553. Name = "";
  6554. break;
  6555. case SyncScope::OpenCLSubGroup:
  6556. Name = "subgroup";
  6557. }
  6558. return C.getOrInsertSyncScopeID(Name);
  6559. }
  6560. //===----------------------------------------------------------------------===//
  6561. // SPARC v8 ABI Implementation.
  6562. // Based on the SPARC Compliance Definition version 2.4.1.
  6563. //
  6564. // Ensures that complex values are passed in registers.
  6565. //
  6566. namespace {
  6567. class SparcV8ABIInfo : public DefaultABIInfo {
  6568. public:
  6569. SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  6570. private:
  6571. ABIArgInfo classifyReturnType(QualType RetTy) const;
  6572. void computeInfo(CGFunctionInfo &FI) const override;
  6573. };
  6574. } // end anonymous namespace
  6575. ABIArgInfo
  6576. SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
  6577. if (Ty->isAnyComplexType()) {
  6578. return ABIArgInfo::getDirect();
  6579. }
  6580. else {
  6581. return DefaultABIInfo::classifyReturnType(Ty);
  6582. }
  6583. }
  6584. void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  6585. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  6586. for (auto &Arg : FI.arguments())
  6587. Arg.info = classifyArgumentType(Arg.type);
  6588. }
  6589. namespace {
  6590. class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
  6591. public:
  6592. SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
  6593. : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {}
  6594. };
  6595. } // end anonymous namespace
  6596. //===----------------------------------------------------------------------===//
  6597. // SPARC v9 ABI Implementation.
  6598. // Based on the SPARC Compliance Definition version 2.4.1.
  6599. //
  6600. // Function arguments a mapped to a nominal "parameter array" and promoted to
  6601. // registers depending on their type. Each argument occupies 8 or 16 bytes in
  6602. // the array, structs larger than 16 bytes are passed indirectly.
  6603. //
  6604. // One case requires special care:
  6605. //
  6606. // struct mixed {
  6607. // int i;
  6608. // float f;
  6609. // };
  6610. //
  6611. // When a struct mixed is passed by value, it only occupies 8 bytes in the
  6612. // parameter array, but the int is passed in an integer register, and the float
  6613. // is passed in a floating point register. This is represented as two arguments
  6614. // with the LLVM IR inreg attribute:
  6615. //
  6616. // declare void f(i32 inreg %i, float inreg %f)
  6617. //
  6618. // The code generator will only allocate 4 bytes from the parameter array for
  6619. // the inreg arguments. All other arguments are allocated a multiple of 8
  6620. // bytes.
  6621. //
  6622. namespace {
  6623. class SparcV9ABIInfo : public ABIInfo {
  6624. public:
  6625. SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
  6626. private:
  6627. ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
  6628. void computeInfo(CGFunctionInfo &FI) const override;
  6629. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6630. QualType Ty) const override;
  6631. // Coercion type builder for structs passed in registers. The coercion type
  6632. // serves two purposes:
  6633. //
  6634. // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
  6635. // in registers.
  6636. // 2. Expose aligned floating point elements as first-level elements, so the
  6637. // code generator knows to pass them in floating point registers.
  6638. //
  6639. // We also compute the InReg flag which indicates that the struct contains
  6640. // aligned 32-bit floats.
  6641. //
  6642. struct CoerceBuilder {
  6643. llvm::LLVMContext &Context;
  6644. const llvm::DataLayout &DL;
  6645. SmallVector<llvm::Type*, 8> Elems;
  6646. uint64_t Size;
  6647. bool InReg;
  6648. CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
  6649. : Context(c), DL(dl), Size(0), InReg(false) {}
  6650. // Pad Elems with integers until Size is ToSize.
  6651. void pad(uint64_t ToSize) {
  6652. assert(ToSize >= Size && "Cannot remove elements");
  6653. if (ToSize == Size)
  6654. return;
  6655. // Finish the current 64-bit word.
  6656. uint64_t Aligned = llvm::alignTo(Size, 64);
  6657. if (Aligned > Size && Aligned <= ToSize) {
  6658. Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
  6659. Size = Aligned;
  6660. }
  6661. // Add whole 64-bit words.
  6662. while (Size + 64 <= ToSize) {
  6663. Elems.push_back(llvm::Type::getInt64Ty(Context));
  6664. Size += 64;
  6665. }
  6666. // Final in-word padding.
  6667. if (Size < ToSize) {
  6668. Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
  6669. Size = ToSize;
  6670. }
  6671. }
  6672. // Add a floating point element at Offset.
  6673. void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
  6674. // Unaligned floats are treated as integers.
  6675. if (Offset % Bits)
  6676. return;
  6677. // The InReg flag is only required if there are any floats < 64 bits.
  6678. if (Bits < 64)
  6679. InReg = true;
  6680. pad(Offset);
  6681. Elems.push_back(Ty);
  6682. Size = Offset + Bits;
  6683. }
  6684. // Add a struct type to the coercion type, starting at Offset (in bits).
  6685. void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
  6686. const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
  6687. for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
  6688. llvm::Type *ElemTy = StrTy->getElementType(i);
  6689. uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
  6690. switch (ElemTy->getTypeID()) {
  6691. case llvm::Type::StructTyID:
  6692. addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
  6693. break;
  6694. case llvm::Type::FloatTyID:
  6695. addFloat(ElemOffset, ElemTy, 32);
  6696. break;
  6697. case llvm::Type::DoubleTyID:
  6698. addFloat(ElemOffset, ElemTy, 64);
  6699. break;
  6700. case llvm::Type::FP128TyID:
  6701. addFloat(ElemOffset, ElemTy, 128);
  6702. break;
  6703. case llvm::Type::PointerTyID:
  6704. if (ElemOffset % 64 == 0) {
  6705. pad(ElemOffset);
  6706. Elems.push_back(ElemTy);
  6707. Size += 64;
  6708. }
  6709. break;
  6710. default:
  6711. break;
  6712. }
  6713. }
  6714. }
  6715. // Check if Ty is a usable substitute for the coercion type.
  6716. bool isUsableType(llvm::StructType *Ty) const {
  6717. return llvm::makeArrayRef(Elems) == Ty->elements();
  6718. }
  6719. // Get the coercion type as a literal struct type.
  6720. llvm::Type *getType() const {
  6721. if (Elems.size() == 1)
  6722. return Elems.front();
  6723. else
  6724. return llvm::StructType::get(Context, Elems);
  6725. }
  6726. };
  6727. };
  6728. } // end anonymous namespace
  6729. ABIArgInfo
  6730. SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
  6731. if (Ty->isVoidType())
  6732. return ABIArgInfo::getIgnore();
  6733. uint64_t Size = getContext().getTypeSize(Ty);
  6734. // Anything too big to fit in registers is passed with an explicit indirect
  6735. // pointer / sret pointer.
  6736. if (Size > SizeLimit)
  6737. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  6738. // Treat an enum type as its underlying type.
  6739. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  6740. Ty = EnumTy->getDecl()->getIntegerType();
  6741. // Integer types smaller than a register are extended.
  6742. if (Size < 64 && Ty->isIntegerType())
  6743. return ABIArgInfo::getExtend();
  6744. // Other non-aggregates go in registers.
  6745. if (!isAggregateTypeForABI(Ty))
  6746. return ABIArgInfo::getDirect();
  6747. // If a C++ object has either a non-trivial copy constructor or a non-trivial
  6748. // destructor, it is passed with an explicit indirect pointer / sret pointer.
  6749. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  6750. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  6751. // This is a small aggregate type that should be passed in registers.
  6752. // Build a coercion type from the LLVM struct type.
  6753. llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
  6754. if (!StrTy)
  6755. return ABIArgInfo::getDirect();
  6756. CoerceBuilder CB(getVMContext(), getDataLayout());
  6757. CB.addStruct(0, StrTy);
  6758. CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
  6759. // Try to use the original type for coercion.
  6760. llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
  6761. if (CB.InReg)
  6762. return ABIArgInfo::getDirectInReg(CoerceTy);
  6763. else
  6764. return ABIArgInfo::getDirect(CoerceTy);
  6765. }
  6766. Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6767. QualType Ty) const {
  6768. ABIArgInfo AI = classifyType(Ty, 16 * 8);
  6769. llvm::Type *ArgTy = CGT.ConvertType(Ty);
  6770. if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
  6771. AI.setCoerceToType(ArgTy);
  6772. CharUnits SlotSize = CharUnits::fromQuantity(8);
  6773. CGBuilderTy &Builder = CGF.Builder;
  6774. Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
  6775. llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
  6776. auto TypeInfo = getContext().getTypeInfoInChars(Ty);
  6777. Address ArgAddr = Address::invalid();
  6778. CharUnits Stride;
  6779. switch (AI.getKind()) {
  6780. case ABIArgInfo::Expand:
  6781. case ABIArgInfo::CoerceAndExpand:
  6782. case ABIArgInfo::InAlloca:
  6783. llvm_unreachable("Unsupported ABI kind for va_arg");
  6784. case ABIArgInfo::Extend: {
  6785. Stride = SlotSize;
  6786. CharUnits Offset = SlotSize - TypeInfo.first;
  6787. ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
  6788. break;
  6789. }
  6790. case ABIArgInfo::Direct: {
  6791. auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
  6792. Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
  6793. ArgAddr = Addr;
  6794. break;
  6795. }
  6796. case ABIArgInfo::Indirect:
  6797. Stride = SlotSize;
  6798. ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
  6799. ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
  6800. TypeInfo.second);
  6801. break;
  6802. case ABIArgInfo::Ignore:
  6803. return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
  6804. }
  6805. // Update VAList.
  6806. llvm::Value *NextPtr =
  6807. Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next");
  6808. Builder.CreateStore(NextPtr, VAListAddr);
  6809. return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
  6810. }
  6811. void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  6812. FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
  6813. for (auto &I : FI.arguments())
  6814. I.info = classifyType(I.type, 16 * 8);
  6815. }
  6816. namespace {
  6817. class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
  6818. public:
  6819. SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
  6820. : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
  6821. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  6822. return 14;
  6823. }
  6824. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  6825. llvm::Value *Address) const override;
  6826. };
  6827. } // end anonymous namespace
  6828. bool
  6829. SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  6830. llvm::Value *Address) const {
  6831. // This is calculated from the LLVM and GCC tables and verified
  6832. // against gcc output. AFAIK all ABIs use the same encoding.
  6833. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  6834. llvm::IntegerType *i8 = CGF.Int8Ty;
  6835. llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
  6836. llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
  6837. // 0-31: the 8-byte general-purpose registers
  6838. AssignToArrayRange(Builder, Address, Eight8, 0, 31);
  6839. // 32-63: f0-31, the 4-byte floating-point registers
  6840. AssignToArrayRange(Builder, Address, Four8, 32, 63);
  6841. // Y = 64
  6842. // PSR = 65
  6843. // WIM = 66
  6844. // TBR = 67
  6845. // PC = 68
  6846. // NPC = 69
  6847. // FSR = 70
  6848. // CSR = 71
  6849. AssignToArrayRange(Builder, Address, Eight8, 64, 71);
  6850. // 72-87: d0-15, the 8-byte floating-point registers
  6851. AssignToArrayRange(Builder, Address, Eight8, 72, 87);
  6852. return false;
  6853. }
  6854. //===----------------------------------------------------------------------===//
  6855. // XCore ABI Implementation
  6856. //===----------------------------------------------------------------------===//
  6857. namespace {
  6858. /// A SmallStringEnc instance is used to build up the TypeString by passing
  6859. /// it by reference between functions that append to it.
  6860. typedef llvm::SmallString<128> SmallStringEnc;
  6861. /// TypeStringCache caches the meta encodings of Types.
  6862. ///
  6863. /// The reason for caching TypeStrings is two fold:
  6864. /// 1. To cache a type's encoding for later uses;
  6865. /// 2. As a means to break recursive member type inclusion.
  6866. ///
  6867. /// A cache Entry can have a Status of:
  6868. /// NonRecursive: The type encoding is not recursive;
  6869. /// Recursive: The type encoding is recursive;
  6870. /// Incomplete: An incomplete TypeString;
  6871. /// IncompleteUsed: An incomplete TypeString that has been used in a
  6872. /// Recursive type encoding.
  6873. ///
  6874. /// A NonRecursive entry will have all of its sub-members expanded as fully
  6875. /// as possible. Whilst it may contain types which are recursive, the type
  6876. /// itself is not recursive and thus its encoding may be safely used whenever
  6877. /// the type is encountered.
  6878. ///
  6879. /// A Recursive entry will have all of its sub-members expanded as fully as
  6880. /// possible. The type itself is recursive and it may contain other types which
  6881. /// are recursive. The Recursive encoding must not be used during the expansion
  6882. /// of a recursive type's recursive branch. For simplicity the code uses
  6883. /// IncompleteCount to reject all usage of Recursive encodings for member types.
  6884. ///
  6885. /// An Incomplete entry is always a RecordType and only encodes its
  6886. /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
  6887. /// are placed into the cache during type expansion as a means to identify and
  6888. /// handle recursive inclusion of types as sub-members. If there is recursion
  6889. /// the entry becomes IncompleteUsed.
  6890. ///
  6891. /// During the expansion of a RecordType's members:
  6892. ///
  6893. /// If the cache contains a NonRecursive encoding for the member type, the
  6894. /// cached encoding is used;
  6895. ///
  6896. /// If the cache contains a Recursive encoding for the member type, the
  6897. /// cached encoding is 'Swapped' out, as it may be incorrect, and...
  6898. ///
  6899. /// If the member is a RecordType, an Incomplete encoding is placed into the
  6900. /// cache to break potential recursive inclusion of itself as a sub-member;
  6901. ///
  6902. /// Once a member RecordType has been expanded, its temporary incomplete
  6903. /// entry is removed from the cache. If a Recursive encoding was swapped out
  6904. /// it is swapped back in;
  6905. ///
  6906. /// If an incomplete entry is used to expand a sub-member, the incomplete
  6907. /// entry is marked as IncompleteUsed. The cache keeps count of how many
  6908. /// IncompleteUsed entries it currently contains in IncompleteUsedCount;
  6909. ///
  6910. /// If a member's encoding is found to be a NonRecursive or Recursive viz:
  6911. /// IncompleteUsedCount==0, the member's encoding is added to the cache.
  6912. /// Else the member is part of a recursive type and thus the recursion has
  6913. /// been exited too soon for the encoding to be correct for the member.
  6914. ///
  6915. class TypeStringCache {
  6916. enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
  6917. struct Entry {
  6918. std::string Str; // The encoded TypeString for the type.
  6919. enum Status State; // Information about the encoding in 'Str'.
  6920. std::string Swapped; // A temporary place holder for a Recursive encoding
  6921. // during the expansion of RecordType's members.
  6922. };
  6923. std::map<const IdentifierInfo *, struct Entry> Map;
  6924. unsigned IncompleteCount; // Number of Incomplete entries in the Map.
  6925. unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
  6926. public:
  6927. TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
  6928. void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
  6929. bool removeIncomplete(const IdentifierInfo *ID);
  6930. void addIfComplete(const IdentifierInfo *ID, StringRef Str,
  6931. bool IsRecursive);
  6932. StringRef lookupStr(const IdentifierInfo *ID);
  6933. };
  6934. /// TypeString encodings for enum & union fields must be order.
  6935. /// FieldEncoding is a helper for this ordering process.
  6936. class FieldEncoding {
  6937. bool HasName;
  6938. std::string Enc;
  6939. public:
  6940. FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
  6941. StringRef str() { return Enc; }
  6942. bool operator<(const FieldEncoding &rhs) const {
  6943. if (HasName != rhs.HasName) return HasName;
  6944. return Enc < rhs.Enc;
  6945. }
  6946. };
  6947. class XCoreABIInfo : public DefaultABIInfo {
  6948. public:
  6949. XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  6950. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6951. QualType Ty) const override;
  6952. };
  6953. class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
  6954. mutable TypeStringCache TSC;
  6955. public:
  6956. XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
  6957. :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
  6958. void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
  6959. CodeGen::CodeGenModule &M) const override;
  6960. };
  6961. } // End anonymous namespace.
  6962. // TODO: this implementation is likely now redundant with the default
  6963. // EmitVAArg.
  6964. Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6965. QualType Ty) const {
  6966. CGBuilderTy &Builder = CGF.Builder;
  6967. // Get the VAList.
  6968. CharUnits SlotSize = CharUnits::fromQuantity(4);
  6969. Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
  6970. // Handle the argument.
  6971. ABIArgInfo AI = classifyArgumentType(Ty);
  6972. CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
  6973. llvm::Type *ArgTy = CGT.ConvertType(Ty);
  6974. if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
  6975. AI.setCoerceToType(ArgTy);
  6976. llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
  6977. Address Val = Address::invalid();
  6978. CharUnits ArgSize = CharUnits::Zero();
  6979. switch (AI.getKind()) {
  6980. case ABIArgInfo::Expand:
  6981. case ABIArgInfo::CoerceAndExpand:
  6982. case ABIArgInfo::InAlloca:
  6983. llvm_unreachable("Unsupported ABI kind for va_arg");
  6984. case ABIArgInfo::Ignore:
  6985. Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
  6986. ArgSize = CharUnits::Zero();
  6987. break;
  6988. case ABIArgInfo::Extend:
  6989. case ABIArgInfo::Direct:
  6990. Val = Builder.CreateBitCast(AP, ArgPtrTy);
  6991. ArgSize = CharUnits::fromQuantity(
  6992. getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
  6993. ArgSize = ArgSize.alignTo(SlotSize);
  6994. break;
  6995. case ABIArgInfo::Indirect:
  6996. Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
  6997. Val = Address(Builder.CreateLoad(Val), TypeAlign);
  6998. ArgSize = SlotSize;
  6999. break;
  7000. }
  7001. // Increment the VAList.
  7002. if (!ArgSize.isZero()) {
  7003. llvm::Value *APN =
  7004. Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize);
  7005. Builder.CreateStore(APN, VAListAddr);
  7006. }
  7007. return Val;
  7008. }
  7009. /// During the expansion of a RecordType, an incomplete TypeString is placed
  7010. /// into the cache as a means to identify and break recursion.
  7011. /// If there is a Recursive encoding in the cache, it is swapped out and will
  7012. /// be reinserted by removeIncomplete().
  7013. /// All other types of encoding should have been used rather than arriving here.
  7014. void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
  7015. std::string StubEnc) {
  7016. if (!ID)
  7017. return;
  7018. Entry &E = Map[ID];
  7019. assert( (E.Str.empty() || E.State == Recursive) &&
  7020. "Incorrectly use of addIncomplete");
  7021. assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
  7022. E.Swapped.swap(E.Str); // swap out the Recursive
  7023. E.Str.swap(StubEnc);
  7024. E.State = Incomplete;
  7025. ++IncompleteCount;
  7026. }
  7027. /// Once the RecordType has been expanded, the temporary incomplete TypeString
  7028. /// must be removed from the cache.
  7029. /// If a Recursive was swapped out by addIncomplete(), it will be replaced.
  7030. /// Returns true if the RecordType was defined recursively.
  7031. bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
  7032. if (!ID)
  7033. return false;
  7034. auto I = Map.find(ID);
  7035. assert(I != Map.end() && "Entry not present");
  7036. Entry &E = I->second;
  7037. assert( (E.State == Incomplete ||
  7038. E.State == IncompleteUsed) &&
  7039. "Entry must be an incomplete type");
  7040. bool IsRecursive = false;
  7041. if (E.State == IncompleteUsed) {
  7042. // We made use of our Incomplete encoding, thus we are recursive.
  7043. IsRecursive = true;
  7044. --IncompleteUsedCount;
  7045. }
  7046. if (E.Swapped.empty())
  7047. Map.erase(I);
  7048. else {
  7049. // Swap the Recursive back.
  7050. E.Swapped.swap(E.Str);
  7051. E.Swapped.clear();
  7052. E.State = Recursive;
  7053. }
  7054. --IncompleteCount;
  7055. return IsRecursive;
  7056. }
  7057. /// Add the encoded TypeString to the cache only if it is NonRecursive or
  7058. /// Recursive (viz: all sub-members were expanded as fully as possible).
  7059. void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
  7060. bool IsRecursive) {
  7061. if (!ID || IncompleteUsedCount)
  7062. return; // No key or it is is an incomplete sub-type so don't add.
  7063. Entry &E = Map[ID];
  7064. if (IsRecursive && !E.Str.empty()) {
  7065. assert(E.State==Recursive && E.Str.size() == Str.size() &&
  7066. "This is not the same Recursive entry");
  7067. // The parent container was not recursive after all, so we could have used
  7068. // this Recursive sub-member entry after all, but we assumed the worse when
  7069. // we started viz: IncompleteCount!=0.
  7070. return;
  7071. }
  7072. assert(E.Str.empty() && "Entry already present");
  7073. E.Str = Str.str();
  7074. E.State = IsRecursive? Recursive : NonRecursive;
  7075. }
  7076. /// Return a cached TypeString encoding for the ID. If there isn't one, or we
  7077. /// are recursively expanding a type (IncompleteCount != 0) and the cached
  7078. /// encoding is Recursive, return an empty StringRef.
  7079. StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
  7080. if (!ID)
  7081. return StringRef(); // We have no key.
  7082. auto I = Map.find(ID);
  7083. if (I == Map.end())
  7084. return StringRef(); // We have no encoding.
  7085. Entry &E = I->second;
  7086. if (E.State == Recursive && IncompleteCount)
  7087. return StringRef(); // We don't use Recursive encodings for member types.
  7088. if (E.State == Incomplete) {
  7089. // The incomplete type is being used to break out of recursion.
  7090. E.State = IncompleteUsed;
  7091. ++IncompleteUsedCount;
  7092. }
  7093. return E.Str;
  7094. }
  7095. /// The XCore ABI includes a type information section that communicates symbol
  7096. /// type information to the linker. The linker uses this information to verify
  7097. /// safety/correctness of things such as array bound and pointers et al.
  7098. /// The ABI only requires C (and XC) language modules to emit TypeStrings.
  7099. /// This type information (TypeString) is emitted into meta data for all global
  7100. /// symbols: definitions, declarations, functions & variables.
  7101. ///
  7102. /// The TypeString carries type, qualifier, name, size & value details.
  7103. /// Please see 'Tools Development Guide' section 2.16.2 for format details:
  7104. /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
  7105. /// The output is tested by test/CodeGen/xcore-stringtype.c.
  7106. ///
  7107. static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
  7108. CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
  7109. /// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
  7110. void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
  7111. CodeGen::CodeGenModule &CGM) const {
  7112. SmallStringEnc Enc;
  7113. if (getTypeString(Enc, D, CGM, TSC)) {
  7114. llvm::LLVMContext &Ctx = CGM.getModule().getContext();
  7115. llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
  7116. llvm::MDString::get(Ctx, Enc.str())};
  7117. llvm::NamedMDNode *MD =
  7118. CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
  7119. MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
  7120. }
  7121. }
  7122. //===----------------------------------------------------------------------===//
  7123. // SPIR ABI Implementation
  7124. //===----------------------------------------------------------------------===//
  7125. namespace {
  7126. class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
  7127. public:
  7128. SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  7129. : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
  7130. unsigned getOpenCLKernelCallingConv() const override;
  7131. };
  7132. } // End anonymous namespace.
  7133. namespace clang {
  7134. namespace CodeGen {
  7135. void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
  7136. DefaultABIInfo SPIRABI(CGM.getTypes());
  7137. SPIRABI.computeInfo(FI);
  7138. }
  7139. }
  7140. }
  7141. unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
  7142. return llvm::CallingConv::SPIR_KERNEL;
  7143. }
  7144. static bool appendType(SmallStringEnc &Enc, QualType QType,
  7145. const CodeGen::CodeGenModule &CGM,
  7146. TypeStringCache &TSC);
  7147. /// Helper function for appendRecordType().
  7148. /// Builds a SmallVector containing the encoded field types in declaration
  7149. /// order.
  7150. static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
  7151. const RecordDecl *RD,
  7152. const CodeGen::CodeGenModule &CGM,
  7153. TypeStringCache &TSC) {
  7154. for (const auto *Field : RD->fields()) {
  7155. SmallStringEnc Enc;
  7156. Enc += "m(";
  7157. Enc += Field->getName();
  7158. Enc += "){";
  7159. if (Field->isBitField()) {
  7160. Enc += "b(";
  7161. llvm::raw_svector_ostream OS(Enc);
  7162. OS << Field->getBitWidthValue(CGM.getContext());
  7163. Enc += ':';
  7164. }
  7165. if (!appendType(Enc, Field->getType(), CGM, TSC))
  7166. return false;
  7167. if (Field->isBitField())
  7168. Enc += ')';
  7169. Enc += '}';
  7170. FE.emplace_back(!Field->getName().empty(), Enc);
  7171. }
  7172. return true;
  7173. }
  7174. /// Appends structure and union types to Enc and adds encoding to cache.
  7175. /// Recursively calls appendType (via extractFieldType) for each field.
  7176. /// Union types have their fields ordered according to the ABI.
  7177. static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
  7178. const CodeGen::CodeGenModule &CGM,
  7179. TypeStringCache &TSC, const IdentifierInfo *ID) {
  7180. // Append the cached TypeString if we have one.
  7181. StringRef TypeString = TSC.lookupStr(ID);
  7182. if (!TypeString.empty()) {
  7183. Enc += TypeString;
  7184. return true;
  7185. }
  7186. // Start to emit an incomplete TypeString.
  7187. size_t Start = Enc.size();
  7188. Enc += (RT->isUnionType()? 'u' : 's');
  7189. Enc += '(';
  7190. if (ID)
  7191. Enc += ID->getName();
  7192. Enc += "){";
  7193. // We collect all encoded fields and order as necessary.
  7194. bool IsRecursive = false;
  7195. const RecordDecl *RD = RT->getDecl()->getDefinition();
  7196. if (RD && !RD->field_empty()) {
  7197. // An incomplete TypeString stub is placed in the cache for this RecordType
  7198. // so that recursive calls to this RecordType will use it whilst building a
  7199. // complete TypeString for this RecordType.
  7200. SmallVector<FieldEncoding, 16> FE;
  7201. std::string StubEnc(Enc.substr(Start).str());
  7202. StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString.
  7203. TSC.addIncomplete(ID, std::move(StubEnc));
  7204. if (!extractFieldType(FE, RD, CGM, TSC)) {
  7205. (void) TSC.removeIncomplete(ID);
  7206. return false;
  7207. }
  7208. IsRecursive = TSC.removeIncomplete(ID);
  7209. // The ABI requires unions to be sorted but not structures.
  7210. // See FieldEncoding::operator< for sort algorithm.
  7211. if (RT->isUnionType())
  7212. std::sort(FE.begin(), FE.end());
  7213. // We can now complete the TypeString.
  7214. unsigned E = FE.size();
  7215. for (unsigned I = 0; I != E; ++I) {
  7216. if (I)
  7217. Enc += ',';
  7218. Enc += FE[I].str();
  7219. }
  7220. }
  7221. Enc += '}';
  7222. TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
  7223. return true;
  7224. }
  7225. /// Appends enum types to Enc and adds the encoding to the cache.
  7226. static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
  7227. TypeStringCache &TSC,
  7228. const IdentifierInfo *ID) {
  7229. // Append the cached TypeString if we have one.
  7230. StringRef TypeString = TSC.lookupStr(ID);
  7231. if (!TypeString.empty()) {
  7232. Enc += TypeString;
  7233. return true;
  7234. }
  7235. size_t Start = Enc.size();
  7236. Enc += "e(";
  7237. if (ID)
  7238. Enc += ID->getName();
  7239. Enc += "){";
  7240. // We collect all encoded enumerations and order them alphanumerically.
  7241. if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
  7242. SmallVector<FieldEncoding, 16> FE;
  7243. for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
  7244. ++I) {
  7245. SmallStringEnc EnumEnc;
  7246. EnumEnc += "m(";
  7247. EnumEnc += I->getName();
  7248. EnumEnc += "){";
  7249. I->getInitVal().toString(EnumEnc);
  7250. EnumEnc += '}';
  7251. FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
  7252. }
  7253. std::sort(FE.begin(), FE.end());
  7254. unsigned E = FE.size();
  7255. for (unsigned I = 0; I != E; ++I) {
  7256. if (I)
  7257. Enc += ',';
  7258. Enc += FE[I].str();
  7259. }
  7260. }
  7261. Enc += '}';
  7262. TSC.addIfComplete(ID, Enc.substr(Start), false);
  7263. return true;
  7264. }
  7265. /// Appends type's qualifier to Enc.
  7266. /// This is done prior to appending the type's encoding.
  7267. static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
  7268. // Qualifiers are emitted in alphabetical order.
  7269. static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
  7270. int Lookup = 0;
  7271. if (QT.isConstQualified())
  7272. Lookup += 1<<0;
  7273. if (QT.isRestrictQualified())
  7274. Lookup += 1<<1;
  7275. if (QT.isVolatileQualified())
  7276. Lookup += 1<<2;
  7277. Enc += Table[Lookup];
  7278. }
  7279. /// Appends built-in types to Enc.
  7280. static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
  7281. const char *EncType;
  7282. switch (BT->getKind()) {
  7283. case BuiltinType::Void:
  7284. EncType = "0";
  7285. break;
  7286. case BuiltinType::Bool:
  7287. EncType = "b";
  7288. break;
  7289. case BuiltinType::Char_U:
  7290. EncType = "uc";
  7291. break;
  7292. case BuiltinType::UChar:
  7293. EncType = "uc";
  7294. break;
  7295. case BuiltinType::SChar:
  7296. EncType = "sc";
  7297. break;
  7298. case BuiltinType::UShort:
  7299. EncType = "us";
  7300. break;
  7301. case BuiltinType::Short:
  7302. EncType = "ss";
  7303. break;
  7304. case BuiltinType::UInt:
  7305. EncType = "ui";
  7306. break;
  7307. case BuiltinType::Int:
  7308. EncType = "si";
  7309. break;
  7310. case BuiltinType::ULong:
  7311. EncType = "ul";
  7312. break;
  7313. case BuiltinType::Long:
  7314. EncType = "sl";
  7315. break;
  7316. case BuiltinType::ULongLong:
  7317. EncType = "ull";
  7318. break;
  7319. case BuiltinType::LongLong:
  7320. EncType = "sll";
  7321. break;
  7322. case BuiltinType::Float:
  7323. EncType = "ft";
  7324. break;
  7325. case BuiltinType::Double:
  7326. EncType = "d";
  7327. break;
  7328. case BuiltinType::LongDouble:
  7329. EncType = "ld";
  7330. break;
  7331. default:
  7332. return false;
  7333. }
  7334. Enc += EncType;
  7335. return true;
  7336. }
  7337. /// Appends a pointer encoding to Enc before calling appendType for the pointee.
  7338. static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
  7339. const CodeGen::CodeGenModule &CGM,
  7340. TypeStringCache &TSC) {
  7341. Enc += "p(";
  7342. if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
  7343. return false;
  7344. Enc += ')';
  7345. return true;
  7346. }
  7347. /// Appends array encoding to Enc before calling appendType for the element.
  7348. static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
  7349. const ArrayType *AT,
  7350. const CodeGen::CodeGenModule &CGM,
  7351. TypeStringCache &TSC, StringRef NoSizeEnc) {
  7352. if (AT->getSizeModifier() != ArrayType::Normal)
  7353. return false;
  7354. Enc += "a(";
  7355. if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
  7356. CAT->getSize().toStringUnsigned(Enc);
  7357. else
  7358. Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
  7359. Enc += ':';
  7360. // The Qualifiers should be attached to the type rather than the array.
  7361. appendQualifier(Enc, QT);
  7362. if (!appendType(Enc, AT->getElementType(), CGM, TSC))
  7363. return false;
  7364. Enc += ')';
  7365. return true;
  7366. }
  7367. /// Appends a function encoding to Enc, calling appendType for the return type
  7368. /// and the arguments.
  7369. static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
  7370. const CodeGen::CodeGenModule &CGM,
  7371. TypeStringCache &TSC) {
  7372. Enc += "f{";
  7373. if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
  7374. return false;
  7375. Enc += "}(";
  7376. if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
  7377. // N.B. we are only interested in the adjusted param types.
  7378. auto I = FPT->param_type_begin();
  7379. auto E = FPT->param_type_end();
  7380. if (I != E) {
  7381. do {
  7382. if (!appendType(Enc, *I, CGM, TSC))
  7383. return false;
  7384. ++I;
  7385. if (I != E)
  7386. Enc += ',';
  7387. } while (I != E);
  7388. if (FPT->isVariadic())
  7389. Enc += ",va";
  7390. } else {
  7391. if (FPT->isVariadic())
  7392. Enc += "va";
  7393. else
  7394. Enc += '0';
  7395. }
  7396. }
  7397. Enc += ')';
  7398. return true;
  7399. }
  7400. /// Handles the type's qualifier before dispatching a call to handle specific
  7401. /// type encodings.
  7402. static bool appendType(SmallStringEnc &Enc, QualType QType,
  7403. const CodeGen::CodeGenModule &CGM,
  7404. TypeStringCache &TSC) {
  7405. QualType QT = QType.getCanonicalType();
  7406. if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
  7407. // The Qualifiers should be attached to the type rather than the array.
  7408. // Thus we don't call appendQualifier() here.
  7409. return appendArrayType(Enc, QT, AT, CGM, TSC, "");
  7410. appendQualifier(Enc, QT);
  7411. if (const BuiltinType *BT = QT->getAs<BuiltinType>())
  7412. return appendBuiltinType(Enc, BT);
  7413. if (const PointerType *PT = QT->getAs<PointerType>())
  7414. return appendPointerType(Enc, PT, CGM, TSC);
  7415. if (const EnumType *ET = QT->getAs<EnumType>())
  7416. return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
  7417. if (const RecordType *RT = QT->getAsStructureType())
  7418. return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
  7419. if (const RecordType *RT = QT->getAsUnionType())
  7420. return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
  7421. if (const FunctionType *FT = QT->getAs<FunctionType>())
  7422. return appendFunctionType(Enc, FT, CGM, TSC);
  7423. return false;
  7424. }
  7425. static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
  7426. CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
  7427. if (!D)
  7428. return false;
  7429. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  7430. if (FD->getLanguageLinkage() != CLanguageLinkage)
  7431. return false;
  7432. return appendType(Enc, FD->getType(), CGM, TSC);
  7433. }
  7434. if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
  7435. if (VD->getLanguageLinkage() != CLanguageLinkage)
  7436. return false;
  7437. QualType QT = VD->getType().getCanonicalType();
  7438. if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
  7439. // Global ArrayTypes are given a size of '*' if the size is unknown.
  7440. // The Qualifiers should be attached to the type rather than the array.
  7441. // Thus we don't call appendQualifier() here.
  7442. return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
  7443. }
  7444. return appendType(Enc, QT, CGM, TSC);
  7445. }
  7446. return false;
  7447. }
  7448. //===----------------------------------------------------------------------===//
  7449. // Driver code
  7450. //===----------------------------------------------------------------------===//
  7451. bool CodeGenModule::supportsCOMDAT() const {
  7452. return getTriple().supportsCOMDAT();
  7453. }
  7454. const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
  7455. if (TheTargetCodeGenInfo)
  7456. return *TheTargetCodeGenInfo;
  7457. // Helper to set the unique_ptr while still keeping the return value.
  7458. auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
  7459. this->TheTargetCodeGenInfo.reset(P);
  7460. return *P;
  7461. };
  7462. const llvm::Triple &Triple = getTarget().getTriple();
  7463. switch (Triple.getArch()) {
  7464. default:
  7465. return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
  7466. case llvm::Triple::le32:
  7467. return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
  7468. case llvm::Triple::mips:
  7469. case llvm::Triple::mipsel:
  7470. if (Triple.getOS() == llvm::Triple::NaCl)
  7471. return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
  7472. return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
  7473. case llvm::Triple::mips64:
  7474. case llvm::Triple::mips64el:
  7475. return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
  7476. case llvm::Triple::avr:
  7477. return SetCGInfo(new AVRTargetCodeGenInfo(Types));
  7478. case llvm::Triple::aarch64:
  7479. case llvm::Triple::aarch64_be: {
  7480. AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
  7481. if (getTarget().getABI() == "darwinpcs")
  7482. Kind = AArch64ABIInfo::DarwinPCS;
  7483. else if (Triple.isOSWindows())
  7484. return SetCGInfo(
  7485. new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
  7486. return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
  7487. }
  7488. case llvm::Triple::wasm32:
  7489. case llvm::Triple::wasm64:
  7490. return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types));
  7491. case llvm::Triple::arm:
  7492. case llvm::Triple::armeb:
  7493. case llvm::Triple::thumb:
  7494. case llvm::Triple::thumbeb: {
  7495. if (Triple.getOS() == llvm::Triple::Win32) {
  7496. return SetCGInfo(
  7497. new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
  7498. }
  7499. ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
  7500. StringRef ABIStr = getTarget().getABI();
  7501. if (ABIStr == "apcs-gnu")
  7502. Kind = ARMABIInfo::APCS;
  7503. else if (ABIStr == "aapcs16")
  7504. Kind = ARMABIInfo::AAPCS16_VFP;
  7505. else if (CodeGenOpts.FloatABI == "hard" ||
  7506. (CodeGenOpts.FloatABI != "soft" &&
  7507. (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
  7508. Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
  7509. Triple.getEnvironment() == llvm::Triple::EABIHF)))
  7510. Kind = ARMABIInfo::AAPCS_VFP;
  7511. return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
  7512. }
  7513. case llvm::Triple::ppc:
  7514. return SetCGInfo(
  7515. new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft"));
  7516. case llvm::Triple::ppc64:
  7517. if (Triple.isOSBinFormatELF()) {
  7518. PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
  7519. if (getTarget().getABI() == "elfv2")
  7520. Kind = PPC64_SVR4_ABIInfo::ELFv2;
  7521. bool HasQPX = getTarget().getABI() == "elfv1-qpx";
  7522. bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
  7523. return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
  7524. IsSoftFloat));
  7525. } else
  7526. return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
  7527. case llvm::Triple::ppc64le: {
  7528. assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
  7529. PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
  7530. if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
  7531. Kind = PPC64_SVR4_ABIInfo::ELFv1;
  7532. bool HasQPX = getTarget().getABI() == "elfv1-qpx";
  7533. bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
  7534. return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
  7535. IsSoftFloat));
  7536. }
  7537. case llvm::Triple::nvptx:
  7538. case llvm::Triple::nvptx64:
  7539. return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
  7540. case llvm::Triple::msp430:
  7541. return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
  7542. case llvm::Triple::systemz: {
  7543. bool HasVector = getTarget().getABI() == "vector";
  7544. return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector));
  7545. }
  7546. case llvm::Triple::tce:
  7547. case llvm::Triple::tcele:
  7548. return SetCGInfo(new TCETargetCodeGenInfo(Types));
  7549. case llvm::Triple::x86: {
  7550. bool IsDarwinVectorABI = Triple.isOSDarwin();
  7551. bool RetSmallStructInRegABI =
  7552. X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
  7553. bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
  7554. if (Triple.getOS() == llvm::Triple::Win32) {
  7555. return SetCGInfo(new WinX86_32TargetCodeGenInfo(
  7556. Types, IsDarwinVectorABI, RetSmallStructInRegABI,
  7557. IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
  7558. } else {
  7559. return SetCGInfo(new X86_32TargetCodeGenInfo(
  7560. Types, IsDarwinVectorABI, RetSmallStructInRegABI,
  7561. IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
  7562. CodeGenOpts.FloatABI == "soft"));
  7563. }
  7564. }
  7565. case llvm::Triple::x86_64: {
  7566. StringRef ABI = getTarget().getABI();
  7567. X86AVXABILevel AVXLevel =
  7568. (ABI == "avx512"
  7569. ? X86AVXABILevel::AVX512
  7570. : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
  7571. switch (Triple.getOS()) {
  7572. case llvm::Triple::Win32:
  7573. return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
  7574. case llvm::Triple::PS4:
  7575. return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel));
  7576. default:
  7577. return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
  7578. }
  7579. }
  7580. case llvm::Triple::hexagon:
  7581. return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
  7582. case llvm::Triple::lanai:
  7583. return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
  7584. case llvm::Triple::r600:
  7585. return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
  7586. case llvm::Triple::amdgcn:
  7587. return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
  7588. case llvm::Triple::sparc:
  7589. return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
  7590. case llvm::Triple::sparcv9:
  7591. return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
  7592. case llvm::Triple::xcore:
  7593. return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
  7594. case llvm::Triple::spir:
  7595. case llvm::Triple::spir64:
  7596. return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
  7597. }
  7598. }
  7599. /// Create an OpenCL kernel for an enqueued block.
  7600. ///
  7601. /// The kernel has the same function type as the block invoke function. Its
  7602. /// name is the name of the block invoke function postfixed with "_kernel".
  7603. /// It simply calls the block invoke function then returns.
  7604. llvm::Function *
  7605. TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
  7606. llvm::Function *Invoke,
  7607. llvm::Value *BlockLiteral) const {
  7608. auto *InvokeFT = Invoke->getFunctionType();
  7609. llvm::SmallVector<llvm::Type *, 2> ArgTys;
  7610. for (auto &P : InvokeFT->params())
  7611. ArgTys.push_back(P);
  7612. auto &C = CGF.getLLVMContext();
  7613. std::string Name = Invoke->getName().str() + "_kernel";
  7614. auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
  7615. auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
  7616. &CGF.CGM.getModule());
  7617. auto IP = CGF.Builder.saveIP();
  7618. auto *BB = llvm::BasicBlock::Create(C, "entry", F);
  7619. auto &Builder = CGF.Builder;
  7620. Builder.SetInsertPoint(BB);
  7621. llvm::SmallVector<llvm::Value *, 2> Args;
  7622. for (auto &A : F->args())
  7623. Args.push_back(&A);
  7624. Builder.CreateCall(Invoke, Args);
  7625. Builder.CreateRetVoid();
  7626. Builder.restoreIP(IP);
  7627. return F;
  7628. }
  7629. /// Create an OpenCL kernel for an enqueued block.
  7630. ///
  7631. /// The type of the first argument (the block literal) is the struct type
  7632. /// of the block literal instead of a pointer type. The first argument
  7633. /// (block literal) is passed directly by value to the kernel. The kernel
  7634. /// allocates the same type of struct on stack and stores the block literal
  7635. /// to it and passes its pointer to the block invoke function. The kernel
  7636. /// has "enqueued-block" function attribute and kernel argument metadata.
  7637. llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
  7638. CodeGenFunction &CGF, llvm::Function *Invoke,
  7639. llvm::Value *BlockLiteral) const {
  7640. auto &Builder = CGF.Builder;
  7641. auto &C = CGF.getLLVMContext();
  7642. auto *BlockTy = BlockLiteral->getType()->getPointerElementType();
  7643. auto *InvokeFT = Invoke->getFunctionType();
  7644. llvm::SmallVector<llvm::Type *, 2> ArgTys;
  7645. llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
  7646. llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
  7647. llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
  7648. llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
  7649. llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
  7650. llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
  7651. ArgTys.push_back(BlockTy);
  7652. ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
  7653. AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
  7654. ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
  7655. ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
  7656. AccessQuals.push_back(llvm::MDString::get(C, "none"));
  7657. ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
  7658. for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
  7659. ArgTys.push_back(InvokeFT->getParamType(I));
  7660. ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
  7661. AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
  7662. AccessQuals.push_back(llvm::MDString::get(C, "none"));
  7663. ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
  7664. ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
  7665. ArgNames.push_back(
  7666. llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
  7667. }
  7668. std::string Name = Invoke->getName().str() + "_kernel";
  7669. auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
  7670. auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
  7671. &CGF.CGM.getModule());
  7672. F->addFnAttr("enqueued-block");
  7673. auto IP = CGF.Builder.saveIP();
  7674. auto *BB = llvm::BasicBlock::Create(C, "entry", F);
  7675. Builder.SetInsertPoint(BB);
  7676. unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy);
  7677. auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
  7678. BlockPtr->setAlignment(BlockAlign);
  7679. Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
  7680. auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
  7681. llvm::SmallVector<llvm::Value *, 2> Args;
  7682. Args.push_back(Cast);
  7683. for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I)
  7684. Args.push_back(I);
  7685. Builder.CreateCall(Invoke, Args);
  7686. Builder.CreateRetVoid();
  7687. Builder.restoreIP(IP);
  7688. F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
  7689. F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
  7690. F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
  7691. F->setMetadata("kernel_arg_base_type",
  7692. llvm::MDNode::get(C, ArgBaseTypeNames));
  7693. F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
  7694. if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
  7695. F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
  7696. return F;
  7697. }