TargetInfo.cpp 348 KB

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