TargetInfo.cpp 347 KB

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