TargetInfo.cpp 308 KB

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