TargetInfo.cpp 460 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527
  1. //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // These classes wrap the information about a call or function
  10. // definition used to handle ABI compliancy.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "TargetInfo.h"
  14. #include "ABIInfo.h"
  15. #include "CGBlocks.h"
  16. #include "CGCXXABI.h"
  17. #include "CGValue.h"
  18. #include "CodeGenFunction.h"
  19. #include "clang/AST/Attr.h"
  20. #include "clang/AST/RecordLayout.h"
  21. #include "clang/Basic/Builtins.h"
  22. #include "clang/Basic/CodeGenOptions.h"
  23. #include "clang/Basic/DiagnosticFrontend.h"
  24. #include "clang/CodeGen/CGFunctionInfo.h"
  25. #include "llvm/ADT/SmallBitVector.h"
  26. #include "llvm/ADT/StringExtras.h"
  27. #include "llvm/ADT/StringSwitch.h"
  28. #include "llvm/ADT/Triple.h"
  29. #include "llvm/ADT/Twine.h"
  30. #include "llvm/IR/DataLayout.h"
  31. #include "llvm/IR/IntrinsicsNVPTX.h"
  32. #include "llvm/IR/IntrinsicsS390.h"
  33. #include "llvm/IR/Type.h"
  34. #include "llvm/Support/MathExtras.h"
  35. #include "llvm/Support/raw_ostream.h"
  36. #include <algorithm>
  37. using namespace clang;
  38. using namespace CodeGen;
  39. // Helper for coercing an aggregate argument or return value into an integer
  40. // array of the same size (including padding) and alignment. This alternate
  41. // coercion happens only for the RenderScript ABI and can be removed after
  42. // runtimes that rely on it are no longer supported.
  43. //
  44. // RenderScript assumes that the size of the argument / return value in the IR
  45. // is the same as the size of the corresponding qualified type. This helper
  46. // coerces the aggregate type into an array of the same size (including
  47. // padding). This coercion is used in lieu of expansion of struct members or
  48. // other canonical coercions that return a coerced-type of larger size.
  49. //
  50. // Ty - The argument / return value type
  51. // Context - The associated ASTContext
  52. // LLVMContext - The associated LLVMContext
  53. static ABIArgInfo coerceToIntArray(QualType Ty,
  54. ASTContext &Context,
  55. llvm::LLVMContext &LLVMContext) {
  56. // Alignment and Size are measured in bits.
  57. const uint64_t Size = Context.getTypeSize(Ty);
  58. const uint64_t Alignment = Context.getTypeAlign(Ty);
  59. llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
  60. const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
  61. return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
  62. }
  63. static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
  64. llvm::Value *Array,
  65. llvm::Value *Value,
  66. unsigned FirstIndex,
  67. unsigned LastIndex) {
  68. // Alternatively, we could emit this as a loop in the source.
  69. for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
  70. llvm::Value *Cell =
  71. Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
  72. Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
  73. }
  74. }
  75. static bool isAggregateTypeForABI(QualType T) {
  76. return !CodeGenFunction::hasScalarEvaluationKind(T) ||
  77. T->isMemberFunctionPointerType();
  78. }
  79. ABIArgInfo ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByVal,
  80. bool Realign,
  81. llvm::Type *Padding) const {
  82. return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), ByVal,
  83. Realign, Padding);
  84. }
  85. ABIArgInfo
  86. ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
  87. return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
  88. /*ByVal*/ false, Realign);
  89. }
  90. Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  91. QualType Ty) const {
  92. return Address::invalid();
  93. }
  94. static llvm::Type *getVAListElementType(CodeGenFunction &CGF) {
  95. return CGF.ConvertTypeForMem(
  96. CGF.getContext().getBuiltinVaListType()->getPointeeType());
  97. }
  98. bool ABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
  99. if (getContext().isPromotableIntegerType(Ty))
  100. return true;
  101. if (const auto *EIT = Ty->getAs<BitIntType>())
  102. if (EIT->getNumBits() < getContext().getTypeSize(getContext().IntTy))
  103. return true;
  104. return false;
  105. }
  106. ABIInfo::~ABIInfo() = default;
  107. SwiftABIInfo::~SwiftABIInfo() = default;
  108. /// Does the given lowering require more than the given number of
  109. /// registers when expanded?
  110. ///
  111. /// This is intended to be the basis of a reasonable basic implementation
  112. /// of should{Pass,Return}IndirectlyForSwift.
  113. ///
  114. /// For most targets, a limit of four total registers is reasonable; this
  115. /// limits the amount of code required in order to move around the value
  116. /// in case it wasn't produced immediately prior to the call by the caller
  117. /// (or wasn't produced in exactly the right registers) or isn't used
  118. /// immediately within the callee. But some targets may need to further
  119. /// limit the register count due to an inability to support that many
  120. /// return registers.
  121. static bool occupiesMoreThan(CodeGenTypes &cgt,
  122. ArrayRef<llvm::Type*> scalarTypes,
  123. unsigned maxAllRegisters) {
  124. unsigned intCount = 0, fpCount = 0;
  125. for (llvm::Type *type : scalarTypes) {
  126. if (type->isPointerTy()) {
  127. intCount++;
  128. } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
  129. auto ptrWidth = cgt.getTarget().getPointerWidth(LangAS::Default);
  130. intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
  131. } else {
  132. assert(type->isVectorTy() || type->isFloatingPointTy());
  133. fpCount++;
  134. }
  135. }
  136. return (intCount + fpCount > maxAllRegisters);
  137. }
  138. bool SwiftABIInfo::shouldPassIndirectly(ArrayRef<llvm::Type *> ComponentTys,
  139. bool AsReturnValue) const {
  140. return occupiesMoreThan(CGT, ComponentTys, /*total=*/4);
  141. }
  142. bool SwiftABIInfo::isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy,
  143. unsigned NumElts) const {
  144. // The default implementation of this assumes that the target guarantees
  145. // 128-bit SIMD support but nothing more.
  146. return (VectorSize.getQuantity() > 8 && VectorSize.getQuantity() <= 16);
  147. }
  148. static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
  149. CGCXXABI &CXXABI) {
  150. const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
  151. if (!RD) {
  152. if (!RT->getDecl()->canPassInRegisters())
  153. return CGCXXABI::RAA_Indirect;
  154. return CGCXXABI::RAA_Default;
  155. }
  156. return CXXABI.getRecordArgABI(RD);
  157. }
  158. static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
  159. CGCXXABI &CXXABI) {
  160. const RecordType *RT = T->getAs<RecordType>();
  161. if (!RT)
  162. return CGCXXABI::RAA_Default;
  163. return getRecordArgABI(RT, CXXABI);
  164. }
  165. static bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI,
  166. const ABIInfo &Info) {
  167. QualType Ty = FI.getReturnType();
  168. if (const auto *RT = Ty->getAs<RecordType>())
  169. if (!isa<CXXRecordDecl>(RT->getDecl()) &&
  170. !RT->getDecl()->canPassInRegisters()) {
  171. FI.getReturnInfo() = Info.getNaturalAlignIndirect(Ty);
  172. return true;
  173. }
  174. return CXXABI.classifyReturnType(FI);
  175. }
  176. /// Pass transparent unions as if they were the type of the first element. Sema
  177. /// should ensure that all elements of the union have the same "machine type".
  178. static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
  179. if (const RecordType *UT = Ty->getAsUnionType()) {
  180. const RecordDecl *UD = UT->getDecl();
  181. if (UD->hasAttr<TransparentUnionAttr>()) {
  182. assert(!UD->field_empty() && "sema created an empty transparent union");
  183. return UD->field_begin()->getType();
  184. }
  185. }
  186. return Ty;
  187. }
  188. CGCXXABI &ABIInfo::getCXXABI() const {
  189. return CGT.getCXXABI();
  190. }
  191. ASTContext &ABIInfo::getContext() const {
  192. return CGT.getContext();
  193. }
  194. llvm::LLVMContext &ABIInfo::getVMContext() const {
  195. return CGT.getLLVMContext();
  196. }
  197. const llvm::DataLayout &ABIInfo::getDataLayout() const {
  198. return CGT.getDataLayout();
  199. }
  200. const TargetInfo &ABIInfo::getTarget() const {
  201. return CGT.getTarget();
  202. }
  203. const CodeGenOptions &ABIInfo::getCodeGenOpts() const {
  204. return CGT.getCodeGenOpts();
  205. }
  206. bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); }
  207. bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  208. return false;
  209. }
  210. bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
  211. uint64_t Members) const {
  212. return false;
  213. }
  214. bool ABIInfo::isZeroLengthBitfieldPermittedInHomogeneousAggregate() const {
  215. // For compatibility with GCC, ignore empty bitfields in C++ mode.
  216. return getContext().getLangOpts().CPlusPlus;
  217. }
  218. LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
  219. raw_ostream &OS = llvm::errs();
  220. OS << "(ABIArgInfo Kind=";
  221. switch (TheKind) {
  222. case Direct:
  223. OS << "Direct Type=";
  224. if (llvm::Type *Ty = getCoerceToType())
  225. Ty->print(OS);
  226. else
  227. OS << "null";
  228. break;
  229. case Extend:
  230. OS << "Extend";
  231. break;
  232. case Ignore:
  233. OS << "Ignore";
  234. break;
  235. case InAlloca:
  236. OS << "InAlloca Offset=" << getInAllocaFieldIndex();
  237. break;
  238. case Indirect:
  239. OS << "Indirect Align=" << getIndirectAlign().getQuantity()
  240. << " ByVal=" << getIndirectByVal()
  241. << " Realign=" << getIndirectRealign();
  242. break;
  243. case IndirectAliased:
  244. OS << "Indirect Align=" << getIndirectAlign().getQuantity()
  245. << " AadrSpace=" << getIndirectAddrSpace()
  246. << " Realign=" << getIndirectRealign();
  247. break;
  248. case Expand:
  249. OS << "Expand";
  250. break;
  251. case CoerceAndExpand:
  252. OS << "CoerceAndExpand Type=";
  253. getCoerceAndExpandType()->print(OS);
  254. break;
  255. }
  256. OS << ")\n";
  257. }
  258. // Dynamically round a pointer up to a multiple of the given alignment.
  259. static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
  260. llvm::Value *Ptr,
  261. CharUnits Align) {
  262. llvm::Value *PtrAsInt = Ptr;
  263. // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
  264. PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
  265. PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
  266. llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
  267. PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
  268. llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
  269. PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
  270. Ptr->getType(),
  271. Ptr->getName() + ".aligned");
  272. return PtrAsInt;
  273. }
  274. /// Emit va_arg for a platform using the common void* representation,
  275. /// where arguments are simply emitted in an array of slots on the stack.
  276. ///
  277. /// This version implements the core direct-value passing rules.
  278. ///
  279. /// \param SlotSize - The size and alignment of a stack slot.
  280. /// Each argument will be allocated to a multiple of this number of
  281. /// slots, and all the slots will be aligned to this value.
  282. /// \param AllowHigherAlign - The slot alignment is not a cap;
  283. /// an argument type with an alignment greater than the slot size
  284. /// will be emitted on a higher-alignment address, potentially
  285. /// leaving one or more empty slots behind as padding. If this
  286. /// is false, the returned address might be less-aligned than
  287. /// DirectAlign.
  288. /// \param ForceRightAdjust - Default is false. On big-endian platform and
  289. /// if the argument is smaller than a slot, set this flag will force
  290. /// right-adjust the argument in its slot irrespective of the type.
  291. static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
  292. Address VAListAddr,
  293. llvm::Type *DirectTy,
  294. CharUnits DirectSize,
  295. CharUnits DirectAlign,
  296. CharUnits SlotSize,
  297. bool AllowHigherAlign,
  298. bool ForceRightAdjust = false) {
  299. // Cast the element type to i8* if necessary. Some platforms define
  300. // va_list as a struct containing an i8* instead of just an i8*.
  301. if (VAListAddr.getElementType() != CGF.Int8PtrTy)
  302. VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
  303. llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
  304. // If the CC aligns values higher than the slot size, do so if needed.
  305. Address Addr = Address::invalid();
  306. if (AllowHigherAlign && DirectAlign > SlotSize) {
  307. Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
  308. CGF.Int8Ty, DirectAlign);
  309. } else {
  310. Addr = Address(Ptr, CGF.Int8Ty, SlotSize);
  311. }
  312. // Advance the pointer past the argument, then store that back.
  313. CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
  314. Address NextPtr =
  315. CGF.Builder.CreateConstInBoundsByteGEP(Addr, FullDirectSize, "argp.next");
  316. CGF.Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
  317. // If the argument is smaller than a slot, and this is a big-endian
  318. // target, the argument will be right-adjusted in its slot.
  319. if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
  320. (!DirectTy->isStructTy() || ForceRightAdjust)) {
  321. Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
  322. }
  323. Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
  324. return Addr;
  325. }
  326. /// Emit va_arg for a platform using the common void* representation,
  327. /// where arguments are simply emitted in an array of slots on the stack.
  328. ///
  329. /// \param IsIndirect - Values of this type are passed indirectly.
  330. /// \param ValueInfo - The size and alignment of this type, generally
  331. /// computed with getContext().getTypeInfoInChars(ValueTy).
  332. /// \param SlotSizeAndAlign - The size and alignment of a stack slot.
  333. /// Each argument will be allocated to a multiple of this number of
  334. /// slots, and all the slots will be aligned to this value.
  335. /// \param AllowHigherAlign - The slot alignment is not a cap;
  336. /// an argument type with an alignment greater than the slot size
  337. /// will be emitted on a higher-alignment address, potentially
  338. /// leaving one or more empty slots behind as padding.
  339. /// \param ForceRightAdjust - Default is false. On big-endian platform and
  340. /// if the argument is smaller than a slot, set this flag will force
  341. /// right-adjust the argument in its slot irrespective of the type.
  342. static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
  343. QualType ValueTy, bool IsIndirect,
  344. TypeInfoChars ValueInfo,
  345. CharUnits SlotSizeAndAlign,
  346. bool AllowHigherAlign,
  347. bool ForceRightAdjust = false) {
  348. // The size and alignment of the value that was passed directly.
  349. CharUnits DirectSize, DirectAlign;
  350. if (IsIndirect) {
  351. DirectSize = CGF.getPointerSize();
  352. DirectAlign = CGF.getPointerAlign();
  353. } else {
  354. DirectSize = ValueInfo.Width;
  355. DirectAlign = ValueInfo.Align;
  356. }
  357. // Cast the address we've calculated to the right type.
  358. llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy), *ElementTy = DirectTy;
  359. if (IsIndirect)
  360. DirectTy = DirectTy->getPointerTo(0);
  361. Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, DirectSize,
  362. DirectAlign, SlotSizeAndAlign,
  363. AllowHigherAlign, ForceRightAdjust);
  364. if (IsIndirect) {
  365. Addr = Address(CGF.Builder.CreateLoad(Addr), ElementTy, ValueInfo.Align);
  366. }
  367. return Addr;
  368. }
  369. static Address complexTempStructure(CodeGenFunction &CGF, Address VAListAddr,
  370. QualType Ty, CharUnits SlotSize,
  371. CharUnits EltSize, const ComplexType *CTy) {
  372. Address Addr =
  373. emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, SlotSize * 2,
  374. SlotSize, SlotSize, /*AllowHigher*/ true);
  375. Address RealAddr = Addr;
  376. Address ImagAddr = RealAddr;
  377. if (CGF.CGM.getDataLayout().isBigEndian()) {
  378. RealAddr =
  379. CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize - EltSize);
  380. ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
  381. 2 * SlotSize - EltSize);
  382. } else {
  383. ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
  384. }
  385. llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
  386. RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
  387. ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
  388. llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
  389. llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
  390. Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
  391. CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
  392. /*init*/ true);
  393. return Temp;
  394. }
  395. static Address emitMergePHI(CodeGenFunction &CGF,
  396. Address Addr1, llvm::BasicBlock *Block1,
  397. Address Addr2, llvm::BasicBlock *Block2,
  398. const llvm::Twine &Name = "") {
  399. assert(Addr1.getType() == Addr2.getType());
  400. llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
  401. PHI->addIncoming(Addr1.getPointer(), Block1);
  402. PHI->addIncoming(Addr2.getPointer(), Block2);
  403. CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
  404. return Address(PHI, Addr1.getElementType(), Align);
  405. }
  406. TargetCodeGenInfo::TargetCodeGenInfo(std::unique_ptr<ABIInfo> Info)
  407. : Info(std::move(Info)) {}
  408. TargetCodeGenInfo::~TargetCodeGenInfo() = default;
  409. // If someone can figure out a general rule for this, that would be great.
  410. // It's probably just doomed to be platform-dependent, though.
  411. unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
  412. // Verified for:
  413. // x86-64 FreeBSD, Linux, Darwin
  414. // x86-32 FreeBSD, Linux, Darwin
  415. // PowerPC Linux, Darwin
  416. // ARM Darwin (*not* EABI)
  417. // AArch64 Linux
  418. return 32;
  419. }
  420. bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
  421. const FunctionNoProtoType *fnType) const {
  422. // The following conventions are known to require this to be false:
  423. // x86_stdcall
  424. // MIPS
  425. // For everything else, we just prefer false unless we opt out.
  426. return false;
  427. }
  428. void
  429. TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
  430. llvm::SmallString<24> &Opt) const {
  431. // This assumes the user is passing a library name like "rt" instead of a
  432. // filename like "librt.a/so", and that they don't care whether it's static or
  433. // dynamic.
  434. Opt = "-l";
  435. Opt += Lib;
  436. }
  437. unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
  438. // OpenCL kernels are called via an explicit runtime API with arguments
  439. // set with clSetKernelArg(), not as normal sub-functions.
  440. // Return SPIR_KERNEL by default as the kernel calling convention to
  441. // ensure the fingerprint is fixed such way that each OpenCL argument
  442. // gets one matching argument in the produced kernel function argument
  443. // list to enable feasible implementation of clSetKernelArg() with
  444. // aggregates etc. In case we would use the default C calling conv here,
  445. // clSetKernelArg() might break depending on the target-specific
  446. // conventions; different targets might split structs passed as values
  447. // to multiple function arguments etc.
  448. return llvm::CallingConv::SPIR_KERNEL;
  449. }
  450. llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
  451. llvm::PointerType *T, QualType QT) const {
  452. return llvm::ConstantPointerNull::get(T);
  453. }
  454. LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
  455. const VarDecl *D) const {
  456. assert(!CGM.getLangOpts().OpenCL &&
  457. !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
  458. "Address space agnostic languages only");
  459. return D ? D->getType().getAddressSpace() : LangAS::Default;
  460. }
  461. llvm::Value *TargetCodeGenInfo::performAddrSpaceCast(
  462. CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr,
  463. LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const {
  464. // Since target may map different address spaces in AST to the same address
  465. // space, an address space conversion may end up as a bitcast.
  466. if (auto *C = dyn_cast<llvm::Constant>(Src))
  467. return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy);
  468. // Try to preserve the source's name to make IR more readable.
  469. return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
  470. Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : "");
  471. }
  472. llvm::Constant *
  473. TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src,
  474. LangAS SrcAddr, LangAS DestAddr,
  475. llvm::Type *DestTy) const {
  476. // Since target may map different address spaces in AST to the same address
  477. // space, an address space conversion may end up as a bitcast.
  478. return llvm::ConstantExpr::getPointerCast(Src, DestTy);
  479. }
  480. llvm::SyncScope::ID
  481. TargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
  482. SyncScope Scope,
  483. llvm::AtomicOrdering Ordering,
  484. llvm::LLVMContext &Ctx) const {
  485. return Ctx.getOrInsertSyncScopeID(""); /* default sync scope */
  486. }
  487. static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
  488. /// isEmptyField - Return true iff a the field is "empty", that is it
  489. /// is an unnamed bit-field or an (array of) empty record(s).
  490. static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
  491. bool AllowArrays) {
  492. if (FD->isUnnamedBitfield())
  493. return true;
  494. QualType FT = FD->getType();
  495. // Constant arrays of empty records count as empty, strip them off.
  496. // Constant arrays of zero length always count as empty.
  497. bool WasArray = false;
  498. if (AllowArrays)
  499. while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
  500. if (AT->getSize() == 0)
  501. return true;
  502. FT = AT->getElementType();
  503. // The [[no_unique_address]] special case below does not apply to
  504. // arrays of C++ empty records, so we need to remember this fact.
  505. WasArray = true;
  506. }
  507. const RecordType *RT = FT->getAs<RecordType>();
  508. if (!RT)
  509. return false;
  510. // C++ record fields are never empty, at least in the Itanium ABI.
  511. //
  512. // FIXME: We should use a predicate for whether this behavior is true in the
  513. // current ABI.
  514. //
  515. // The exception to the above rule are fields marked with the
  516. // [[no_unique_address]] attribute (since C++20). Those do count as empty
  517. // according to the Itanium ABI. The exception applies only to records,
  518. // not arrays of records, so we must also check whether we stripped off an
  519. // array type above.
  520. if (isa<CXXRecordDecl>(RT->getDecl()) &&
  521. (WasArray || !FD->hasAttr<NoUniqueAddressAttr>()))
  522. return false;
  523. return isEmptyRecord(Context, FT, AllowArrays);
  524. }
  525. /// isEmptyRecord - Return true iff a structure contains only empty
  526. /// fields. Note that a structure with a flexible array member is not
  527. /// considered empty.
  528. static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
  529. const RecordType *RT = T->getAs<RecordType>();
  530. if (!RT)
  531. return false;
  532. const RecordDecl *RD = RT->getDecl();
  533. if (RD->hasFlexibleArrayMember())
  534. return false;
  535. // If this is a C++ record, check the bases first.
  536. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  537. for (const auto &I : CXXRD->bases())
  538. if (!isEmptyRecord(Context, I.getType(), true))
  539. return false;
  540. for (const auto *I : RD->fields())
  541. if (!isEmptyField(Context, I, AllowArrays))
  542. return false;
  543. return true;
  544. }
  545. /// isSingleElementStruct - Determine if a structure is a "single
  546. /// element struct", i.e. it has exactly one non-empty field or
  547. /// exactly one field which is itself a single element
  548. /// struct. Structures with flexible array members are never
  549. /// considered single element structs.
  550. ///
  551. /// \return The field declaration for the single non-empty field, if
  552. /// it exists.
  553. static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
  554. const RecordType *RT = T->getAs<RecordType>();
  555. if (!RT)
  556. return nullptr;
  557. const RecordDecl *RD = RT->getDecl();
  558. if (RD->hasFlexibleArrayMember())
  559. return nullptr;
  560. const Type *Found = nullptr;
  561. // If this is a C++ record, check the bases first.
  562. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  563. for (const auto &I : CXXRD->bases()) {
  564. // Ignore empty records.
  565. if (isEmptyRecord(Context, I.getType(), true))
  566. continue;
  567. // If we already found an element then this isn't a single-element struct.
  568. if (Found)
  569. return nullptr;
  570. // If this is non-empty and not a single element struct, the composite
  571. // cannot be a single element struct.
  572. Found = isSingleElementStruct(I.getType(), Context);
  573. if (!Found)
  574. return nullptr;
  575. }
  576. }
  577. // Check for single element.
  578. for (const auto *FD : RD->fields()) {
  579. QualType FT = FD->getType();
  580. // Ignore empty fields.
  581. if (isEmptyField(Context, FD, true))
  582. continue;
  583. // If we already found an element then this isn't a single-element
  584. // struct.
  585. if (Found)
  586. return nullptr;
  587. // Treat single element arrays as the element.
  588. while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
  589. if (AT->getSize().getZExtValue() != 1)
  590. break;
  591. FT = AT->getElementType();
  592. }
  593. if (!isAggregateTypeForABI(FT)) {
  594. Found = FT.getTypePtr();
  595. } else {
  596. Found = isSingleElementStruct(FT, Context);
  597. if (!Found)
  598. return nullptr;
  599. }
  600. }
  601. // We don't consider a struct a single-element struct if it has
  602. // padding beyond the element type.
  603. if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
  604. return nullptr;
  605. return Found;
  606. }
  607. namespace {
  608. Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
  609. const ABIArgInfo &AI) {
  610. // This default implementation defers to the llvm backend's va_arg
  611. // instruction. It can handle only passing arguments directly
  612. // (typically only handled in the backend for primitive types), or
  613. // aggregates passed indirectly by pointer (NOTE: if the "byval"
  614. // flag has ABI impact in the callee, this implementation cannot
  615. // work.)
  616. // Only a few cases are covered here at the moment -- those needed
  617. // by the default abi.
  618. llvm::Value *Val;
  619. if (AI.isIndirect()) {
  620. assert(!AI.getPaddingType() &&
  621. "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
  622. assert(
  623. !AI.getIndirectRealign() &&
  624. "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
  625. auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
  626. CharUnits TyAlignForABI = TyInfo.Align;
  627. llvm::Type *ElementTy = CGF.ConvertTypeForMem(Ty);
  628. llvm::Type *BaseTy = llvm::PointerType::getUnqual(ElementTy);
  629. llvm::Value *Addr =
  630. CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
  631. return Address(Addr, ElementTy, TyAlignForABI);
  632. } else {
  633. assert((AI.isDirect() || AI.isExtend()) &&
  634. "Unexpected ArgInfo Kind in generic VAArg emitter!");
  635. assert(!AI.getInReg() &&
  636. "Unexpected InReg seen in arginfo in generic VAArg emitter!");
  637. assert(!AI.getPaddingType() &&
  638. "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
  639. assert(!AI.getDirectOffset() &&
  640. "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
  641. assert(!AI.getCoerceToType() &&
  642. "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
  643. Address Temp = CGF.CreateMemTemp(Ty, "varet");
  644. Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(),
  645. CGF.ConvertTypeForMem(Ty));
  646. CGF.Builder.CreateStore(Val, Temp);
  647. return Temp;
  648. }
  649. }
  650. /// DefaultABIInfo - The default implementation for ABI specific
  651. /// details. This implementation provides information which results in
  652. /// self-consistent and sensible LLVM IR generation, but does not
  653. /// conform to any particular ABI.
  654. class DefaultABIInfo : public ABIInfo {
  655. public:
  656. DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
  657. ABIArgInfo classifyReturnType(QualType RetTy) const;
  658. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  659. void computeInfo(CGFunctionInfo &FI) const override {
  660. if (!getCXXABI().classifyReturnType(FI))
  661. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  662. for (auto &I : FI.arguments())
  663. I.info = classifyArgumentType(I.type);
  664. }
  665. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  666. QualType Ty) const override {
  667. return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
  668. }
  669. };
  670. class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
  671. public:
  672. DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  673. : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
  674. };
  675. ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
  676. Ty = useFirstFieldIfTransparentUnion(Ty);
  677. if (isAggregateTypeForABI(Ty)) {
  678. // Records with non-trivial destructors/copy-constructors should not be
  679. // passed by value.
  680. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  681. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  682. return getNaturalAlignIndirect(Ty);
  683. }
  684. // Treat an enum type as its underlying type.
  685. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  686. Ty = EnumTy->getDecl()->getIntegerType();
  687. ASTContext &Context = getContext();
  688. if (const auto *EIT = Ty->getAs<BitIntType>())
  689. if (EIT->getNumBits() >
  690. Context.getTypeSize(Context.getTargetInfo().hasInt128Type()
  691. ? Context.Int128Ty
  692. : Context.LongLongTy))
  693. return getNaturalAlignIndirect(Ty);
  694. return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
  695. : ABIArgInfo::getDirect());
  696. }
  697. ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
  698. if (RetTy->isVoidType())
  699. return ABIArgInfo::getIgnore();
  700. if (isAggregateTypeForABI(RetTy))
  701. return getNaturalAlignIndirect(RetTy);
  702. // Treat an enum type as its underlying type.
  703. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  704. RetTy = EnumTy->getDecl()->getIntegerType();
  705. if (const auto *EIT = RetTy->getAs<BitIntType>())
  706. if (EIT->getNumBits() >
  707. getContext().getTypeSize(getContext().getTargetInfo().hasInt128Type()
  708. ? getContext().Int128Ty
  709. : getContext().LongLongTy))
  710. return getNaturalAlignIndirect(RetTy);
  711. return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
  712. : ABIArgInfo::getDirect());
  713. }
  714. //===----------------------------------------------------------------------===//
  715. // WebAssembly ABI Implementation
  716. //
  717. // This is a very simple ABI that relies a lot on DefaultABIInfo.
  718. //===----------------------------------------------------------------------===//
  719. class WebAssemblyABIInfo final : public ABIInfo {
  720. public:
  721. enum ABIKind {
  722. MVP = 0,
  723. ExperimentalMV = 1,
  724. };
  725. private:
  726. DefaultABIInfo defaultInfo;
  727. ABIKind Kind;
  728. public:
  729. explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind)
  730. : ABIInfo(CGT), defaultInfo(CGT), Kind(Kind) {}
  731. private:
  732. ABIArgInfo classifyReturnType(QualType RetTy) const;
  733. ABIArgInfo classifyArgumentType(QualType Ty) const;
  734. // DefaultABIInfo's classifyReturnType and classifyArgumentType are
  735. // non-virtual, but computeInfo and EmitVAArg are virtual, so we
  736. // overload them.
  737. void computeInfo(CGFunctionInfo &FI) const override {
  738. if (!getCXXABI().classifyReturnType(FI))
  739. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  740. for (auto &Arg : FI.arguments())
  741. Arg.info = classifyArgumentType(Arg.type);
  742. }
  743. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  744. QualType Ty) const override;
  745. };
  746. class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
  747. public:
  748. explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
  749. WebAssemblyABIInfo::ABIKind K)
  750. : TargetCodeGenInfo(std::make_unique<WebAssemblyABIInfo>(CGT, K)) {
  751. SwiftInfo =
  752. std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false);
  753. }
  754. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  755. CodeGen::CodeGenModule &CGM) const override {
  756. TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
  757. if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  758. if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) {
  759. llvm::Function *Fn = cast<llvm::Function>(GV);
  760. llvm::AttrBuilder B(GV->getContext());
  761. B.addAttribute("wasm-import-module", Attr->getImportModule());
  762. Fn->addFnAttrs(B);
  763. }
  764. if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) {
  765. llvm::Function *Fn = cast<llvm::Function>(GV);
  766. llvm::AttrBuilder B(GV->getContext());
  767. B.addAttribute("wasm-import-name", Attr->getImportName());
  768. Fn->addFnAttrs(B);
  769. }
  770. if (const auto *Attr = FD->getAttr<WebAssemblyExportNameAttr>()) {
  771. llvm::Function *Fn = cast<llvm::Function>(GV);
  772. llvm::AttrBuilder B(GV->getContext());
  773. B.addAttribute("wasm-export-name", Attr->getExportName());
  774. Fn->addFnAttrs(B);
  775. }
  776. }
  777. if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  778. llvm::Function *Fn = cast<llvm::Function>(GV);
  779. if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype())
  780. Fn->addFnAttr("no-prototype");
  781. }
  782. }
  783. };
  784. /// Classify argument of given type \p Ty.
  785. ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
  786. Ty = useFirstFieldIfTransparentUnion(Ty);
  787. if (isAggregateTypeForABI(Ty)) {
  788. // Records with non-trivial destructors/copy-constructors should not be
  789. // passed by value.
  790. if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
  791. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  792. // Ignore empty structs/unions.
  793. if (isEmptyRecord(getContext(), Ty, true))
  794. return ABIArgInfo::getIgnore();
  795. // Lower single-element structs to just pass a regular value. TODO: We
  796. // could do reasonable-size multiple-element structs too, using getExpand(),
  797. // though watch out for things like bitfields.
  798. if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
  799. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  800. // For the experimental multivalue ABI, fully expand all other aggregates
  801. if (Kind == ABIKind::ExperimentalMV) {
  802. const RecordType *RT = Ty->getAs<RecordType>();
  803. assert(RT);
  804. bool HasBitField = false;
  805. for (auto *Field : RT->getDecl()->fields()) {
  806. if (Field->isBitField()) {
  807. HasBitField = true;
  808. break;
  809. }
  810. }
  811. if (!HasBitField)
  812. return ABIArgInfo::getExpand();
  813. }
  814. }
  815. // Otherwise just do the default thing.
  816. return defaultInfo.classifyArgumentType(Ty);
  817. }
  818. ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
  819. if (isAggregateTypeForABI(RetTy)) {
  820. // Records with non-trivial destructors/copy-constructors should not be
  821. // returned by value.
  822. if (!getRecordArgABI(RetTy, getCXXABI())) {
  823. // Ignore empty structs/unions.
  824. if (isEmptyRecord(getContext(), RetTy, true))
  825. return ABIArgInfo::getIgnore();
  826. // Lower single-element structs to just return a regular value. TODO: We
  827. // could do reasonable-size multiple-element structs too, using
  828. // ABIArgInfo::getDirect().
  829. if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
  830. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  831. // For the experimental multivalue ABI, return all other aggregates
  832. if (Kind == ABIKind::ExperimentalMV)
  833. return ABIArgInfo::getDirect();
  834. }
  835. }
  836. // Otherwise just do the default thing.
  837. return defaultInfo.classifyReturnType(RetTy);
  838. }
  839. Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  840. QualType Ty) const {
  841. bool IsIndirect = isAggregateTypeForABI(Ty) &&
  842. !isEmptyRecord(getContext(), Ty, true) &&
  843. !isSingleElementStruct(Ty, getContext());
  844. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
  845. getContext().getTypeInfoInChars(Ty),
  846. CharUnits::fromQuantity(4),
  847. /*AllowHigherAlign=*/true);
  848. }
  849. //===----------------------------------------------------------------------===//
  850. // le32/PNaCl bitcode ABI Implementation
  851. //
  852. // This is a simplified version of the x86_32 ABI. Arguments and return values
  853. // are always passed on the stack.
  854. //===----------------------------------------------------------------------===//
  855. class PNaClABIInfo : public ABIInfo {
  856. public:
  857. PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
  858. ABIArgInfo classifyReturnType(QualType RetTy) const;
  859. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  860. void computeInfo(CGFunctionInfo &FI) const override;
  861. Address EmitVAArg(CodeGenFunction &CGF,
  862. Address VAListAddr, QualType Ty) const override;
  863. };
  864. class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
  865. public:
  866. PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  867. : TargetCodeGenInfo(std::make_unique<PNaClABIInfo>(CGT)) {}
  868. };
  869. void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
  870. if (!getCXXABI().classifyReturnType(FI))
  871. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  872. for (auto &I : FI.arguments())
  873. I.info = classifyArgumentType(I.type);
  874. }
  875. Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  876. QualType Ty) const {
  877. // The PNaCL ABI is a bit odd, in that varargs don't use normal
  878. // function classification. Structs get passed directly for varargs
  879. // functions, through a rewriting transform in
  880. // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
  881. // this target to actually support a va_arg instructions with an
  882. // aggregate type, unlike other targets.
  883. return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
  884. }
  885. /// Classify argument of given type \p Ty.
  886. ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
  887. if (isAggregateTypeForABI(Ty)) {
  888. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  889. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  890. return getNaturalAlignIndirect(Ty);
  891. } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
  892. // Treat an enum type as its underlying type.
  893. Ty = EnumTy->getDecl()->getIntegerType();
  894. } else if (Ty->isFloatingType()) {
  895. // Floating-point types don't go inreg.
  896. return ABIArgInfo::getDirect();
  897. } else if (const auto *EIT = Ty->getAs<BitIntType>()) {
  898. // Treat bit-precise integers as integers if <= 64, otherwise pass
  899. // indirectly.
  900. if (EIT->getNumBits() > 64)
  901. return getNaturalAlignIndirect(Ty);
  902. return ABIArgInfo::getDirect();
  903. }
  904. return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
  905. : ABIArgInfo::getDirect());
  906. }
  907. ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
  908. if (RetTy->isVoidType())
  909. return ABIArgInfo::getIgnore();
  910. // In the PNaCl ABI we always return records/structures on the stack.
  911. if (isAggregateTypeForABI(RetTy))
  912. return getNaturalAlignIndirect(RetTy);
  913. // Treat bit-precise integers as integers if <= 64, otherwise pass indirectly.
  914. if (const auto *EIT = RetTy->getAs<BitIntType>()) {
  915. if (EIT->getNumBits() > 64)
  916. return getNaturalAlignIndirect(RetTy);
  917. return ABIArgInfo::getDirect();
  918. }
  919. // Treat an enum type as its underlying type.
  920. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  921. RetTy = EnumTy->getDecl()->getIntegerType();
  922. return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
  923. : ABIArgInfo::getDirect());
  924. }
  925. /// IsX86_MMXType - Return true if this is an MMX type.
  926. bool IsX86_MMXType(llvm::Type *IRType) {
  927. // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
  928. return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
  929. cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
  930. IRType->getScalarSizeInBits() != 64;
  931. }
  932. static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  933. StringRef Constraint,
  934. llvm::Type* Ty) {
  935. bool IsMMXCons = llvm::StringSwitch<bool>(Constraint)
  936. .Cases("y", "&y", "^Ym", true)
  937. .Default(false);
  938. if (IsMMXCons && Ty->isVectorTy()) {
  939. if (cast<llvm::VectorType>(Ty)->getPrimitiveSizeInBits().getFixedValue() !=
  940. 64) {
  941. // Invalid MMX constraint
  942. return nullptr;
  943. }
  944. return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
  945. }
  946. // No operation needed
  947. return Ty;
  948. }
  949. /// Returns true if this type can be passed in SSE registers with the
  950. /// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
  951. static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
  952. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  953. if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) {
  954. if (BT->getKind() == BuiltinType::LongDouble) {
  955. if (&Context.getTargetInfo().getLongDoubleFormat() ==
  956. &llvm::APFloat::x87DoubleExtended())
  957. return false;
  958. }
  959. return true;
  960. }
  961. } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
  962. // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
  963. // registers specially.
  964. unsigned VecSize = Context.getTypeSize(VT);
  965. if (VecSize == 128 || VecSize == 256 || VecSize == 512)
  966. return true;
  967. }
  968. return false;
  969. }
  970. /// Returns true if this aggregate is small enough to be passed in SSE registers
  971. /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
  972. static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
  973. return NumMembers <= 4;
  974. }
  975. /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86.
  976. static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) {
  977. auto AI = ABIArgInfo::getDirect(T);
  978. AI.setInReg(true);
  979. AI.setCanBeFlattened(false);
  980. return AI;
  981. }
  982. //===----------------------------------------------------------------------===//
  983. // X86-32 ABI Implementation
  984. //===----------------------------------------------------------------------===//
  985. /// Similar to llvm::CCState, but for Clang.
  986. struct CCState {
  987. CCState(CGFunctionInfo &FI)
  988. : IsPreassigned(FI.arg_size()), CC(FI.getCallingConvention()) {}
  989. llvm::SmallBitVector IsPreassigned;
  990. unsigned CC = CallingConv::CC_C;
  991. unsigned FreeRegs = 0;
  992. unsigned FreeSSERegs = 0;
  993. };
  994. /// X86_32ABIInfo - The X86-32 ABI information.
  995. class X86_32ABIInfo : public ABIInfo {
  996. enum Class {
  997. Integer,
  998. Float
  999. };
  1000. static const unsigned MinABIStackAlignInBytes = 4;
  1001. bool IsDarwinVectorABI;
  1002. bool IsRetSmallStructInRegABI;
  1003. bool IsWin32StructABI;
  1004. bool IsSoftFloatABI;
  1005. bool IsMCUABI;
  1006. bool IsLinuxABI;
  1007. unsigned DefaultNumRegisterParameters;
  1008. static bool isRegisterSize(unsigned Size) {
  1009. return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
  1010. }
  1011. bool isHomogeneousAggregateBaseType(QualType Ty) const override {
  1012. // FIXME: Assumes vectorcall is in use.
  1013. return isX86VectorTypeForVectorCall(getContext(), Ty);
  1014. }
  1015. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  1016. uint64_t NumMembers) const override {
  1017. // FIXME: Assumes vectorcall is in use.
  1018. return isX86VectorCallAggregateSmallEnough(NumMembers);
  1019. }
  1020. bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
  1021. /// getIndirectResult - Give a source type \arg Ty, return a suitable result
  1022. /// such that the argument will be passed in memory.
  1023. ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
  1024. ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
  1025. /// Return the alignment to use for the given type on the stack.
  1026. unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
  1027. Class classify(QualType Ty) const;
  1028. ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
  1029. ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
  1030. /// Updates the number of available free registers, returns
  1031. /// true if any registers were allocated.
  1032. bool updateFreeRegs(QualType Ty, CCState &State) const;
  1033. bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
  1034. bool &NeedsPadding) const;
  1035. bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
  1036. bool canExpandIndirectArgument(QualType Ty) const;
  1037. /// Rewrite the function info so that all memory arguments use
  1038. /// inalloca.
  1039. void rewriteWithInAlloca(CGFunctionInfo &FI) const;
  1040. void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
  1041. CharUnits &StackOffset, ABIArgInfo &Info,
  1042. QualType Type) const;
  1043. void runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const;
  1044. public:
  1045. void computeInfo(CGFunctionInfo &FI) const override;
  1046. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  1047. QualType Ty) const override;
  1048. X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
  1049. bool RetSmallStructInRegABI, bool Win32StructABI,
  1050. unsigned NumRegisterParameters, bool SoftFloatABI)
  1051. : ABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
  1052. IsRetSmallStructInRegABI(RetSmallStructInRegABI),
  1053. IsWin32StructABI(Win32StructABI), IsSoftFloatABI(SoftFloatABI),
  1054. IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
  1055. IsLinuxABI(CGT.getTarget().getTriple().isOSLinux() ||
  1056. CGT.getTarget().getTriple().isOSCygMing()),
  1057. DefaultNumRegisterParameters(NumRegisterParameters) {}
  1058. };
  1059. class X86_32SwiftABIInfo : public SwiftABIInfo {
  1060. public:
  1061. explicit X86_32SwiftABIInfo(CodeGenTypes &CGT)
  1062. : SwiftABIInfo(CGT, /*SwiftErrorInRegister=*/false) {}
  1063. bool shouldPassIndirectly(ArrayRef<llvm::Type *> ComponentTys,
  1064. bool AsReturnValue) const override {
  1065. // LLVM's x86-32 lowering currently only assigns up to three
  1066. // integer registers and three fp registers. Oddly, it'll use up to
  1067. // four vector registers for vectors, but those can overlap with the
  1068. // scalar registers.
  1069. return occupiesMoreThan(CGT, ComponentTys, /*total=*/3);
  1070. }
  1071. };
  1072. class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
  1073. public:
  1074. X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
  1075. bool RetSmallStructInRegABI, bool Win32StructABI,
  1076. unsigned NumRegisterParameters, bool SoftFloatABI)
  1077. : TargetCodeGenInfo(std::make_unique<X86_32ABIInfo>(
  1078. CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
  1079. NumRegisterParameters, SoftFloatABI)) {
  1080. SwiftInfo = std::make_unique<X86_32SwiftABIInfo>(CGT);
  1081. }
  1082. static bool isStructReturnInRegABI(
  1083. const llvm::Triple &Triple, const CodeGenOptions &Opts);
  1084. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  1085. CodeGen::CodeGenModule &CGM) const override;
  1086. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
  1087. // Darwin uses different dwarf register numbers for EH.
  1088. if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
  1089. return 4;
  1090. }
  1091. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  1092. llvm::Value *Address) const override;
  1093. llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  1094. StringRef Constraint,
  1095. llvm::Type* Ty) const override {
  1096. return X86AdjustInlineAsmType(CGF, Constraint, Ty);
  1097. }
  1098. void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
  1099. std::string &Constraints,
  1100. std::vector<llvm::Type *> &ResultRegTypes,
  1101. std::vector<llvm::Type *> &ResultTruncRegTypes,
  1102. std::vector<LValue> &ResultRegDests,
  1103. std::string &AsmString,
  1104. unsigned NumOutputs) const override;
  1105. llvm::Constant *
  1106. getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
  1107. unsigned Sig = (0xeb << 0) | // jmp rel8
  1108. (0x06 << 8) | // .+0x08
  1109. ('v' << 16) |
  1110. ('2' << 24);
  1111. return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
  1112. }
  1113. StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
  1114. return "movl\t%ebp, %ebp"
  1115. "\t\t// marker for objc_retainAutoreleaseReturnValue";
  1116. }
  1117. };
  1118. }
  1119. /// Rewrite input constraint references after adding some output constraints.
  1120. /// In the case where there is one output and one input and we add one output,
  1121. /// we need to replace all operand references greater than or equal to 1:
  1122. /// mov $0, $1
  1123. /// mov eax, $1
  1124. /// The result will be:
  1125. /// mov $0, $2
  1126. /// mov eax, $2
  1127. static void rewriteInputConstraintReferences(unsigned FirstIn,
  1128. unsigned NumNewOuts,
  1129. std::string &AsmString) {
  1130. std::string Buf;
  1131. llvm::raw_string_ostream OS(Buf);
  1132. size_t Pos = 0;
  1133. while (Pos < AsmString.size()) {
  1134. size_t DollarStart = AsmString.find('$', Pos);
  1135. if (DollarStart == std::string::npos)
  1136. DollarStart = AsmString.size();
  1137. size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
  1138. if (DollarEnd == std::string::npos)
  1139. DollarEnd = AsmString.size();
  1140. OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
  1141. Pos = DollarEnd;
  1142. size_t NumDollars = DollarEnd - DollarStart;
  1143. if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
  1144. // We have an operand reference.
  1145. size_t DigitStart = Pos;
  1146. if (AsmString[DigitStart] == '{') {
  1147. OS << '{';
  1148. ++DigitStart;
  1149. }
  1150. size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
  1151. if (DigitEnd == std::string::npos)
  1152. DigitEnd = AsmString.size();
  1153. StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
  1154. unsigned OperandIndex;
  1155. if (!OperandStr.getAsInteger(10, OperandIndex)) {
  1156. if (OperandIndex >= FirstIn)
  1157. OperandIndex += NumNewOuts;
  1158. OS << OperandIndex;
  1159. } else {
  1160. OS << OperandStr;
  1161. }
  1162. Pos = DigitEnd;
  1163. }
  1164. }
  1165. AsmString = std::move(OS.str());
  1166. }
  1167. /// Add output constraints for EAX:EDX because they are return registers.
  1168. void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
  1169. CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
  1170. std::vector<llvm::Type *> &ResultRegTypes,
  1171. std::vector<llvm::Type *> &ResultTruncRegTypes,
  1172. std::vector<LValue> &ResultRegDests, std::string &AsmString,
  1173. unsigned NumOutputs) const {
  1174. uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
  1175. // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
  1176. // larger.
  1177. if (!Constraints.empty())
  1178. Constraints += ',';
  1179. if (RetWidth <= 32) {
  1180. Constraints += "={eax}";
  1181. ResultRegTypes.push_back(CGF.Int32Ty);
  1182. } else {
  1183. // Use the 'A' constraint for EAX:EDX.
  1184. Constraints += "=A";
  1185. ResultRegTypes.push_back(CGF.Int64Ty);
  1186. }
  1187. // Truncate EAX or EAX:EDX to an integer of the appropriate size.
  1188. llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
  1189. ResultTruncRegTypes.push_back(CoerceTy);
  1190. // Coerce the integer by bitcasting the return slot pointer.
  1191. ReturnSlot.setAddress(
  1192. CGF.Builder.CreateElementBitCast(ReturnSlot.getAddress(CGF), CoerceTy));
  1193. ResultRegDests.push_back(ReturnSlot);
  1194. rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
  1195. }
  1196. /// shouldReturnTypeInRegister - Determine if the given type should be
  1197. /// returned in a register (for the Darwin and MCU ABI).
  1198. bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
  1199. ASTContext &Context) const {
  1200. uint64_t Size = Context.getTypeSize(Ty);
  1201. // For i386, type must be register sized.
  1202. // For the MCU ABI, it only needs to be <= 8-byte
  1203. if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
  1204. return false;
  1205. if (Ty->isVectorType()) {
  1206. // 64- and 128- bit vectors inside structures are not returned in
  1207. // registers.
  1208. if (Size == 64 || Size == 128)
  1209. return false;
  1210. return true;
  1211. }
  1212. // If this is a builtin, pointer, enum, complex type, member pointer, or
  1213. // member function pointer it is ok.
  1214. if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
  1215. Ty->isAnyComplexType() || Ty->isEnumeralType() ||
  1216. Ty->isBlockPointerType() || Ty->isMemberPointerType())
  1217. return true;
  1218. // Arrays are treated like records.
  1219. if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
  1220. return shouldReturnTypeInRegister(AT->getElementType(), Context);
  1221. // Otherwise, it must be a record type.
  1222. const RecordType *RT = Ty->getAs<RecordType>();
  1223. if (!RT) return false;
  1224. // FIXME: Traverse bases here too.
  1225. // Structure types are passed in register if all fields would be
  1226. // passed in a register.
  1227. for (const auto *FD : RT->getDecl()->fields()) {
  1228. // Empty fields are ignored.
  1229. if (isEmptyField(Context, FD, true))
  1230. continue;
  1231. // Check fields recursively.
  1232. if (!shouldReturnTypeInRegister(FD->getType(), Context))
  1233. return false;
  1234. }
  1235. return true;
  1236. }
  1237. static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
  1238. // Treat complex types as the element type.
  1239. if (const ComplexType *CTy = Ty->getAs<ComplexType>())
  1240. Ty = CTy->getElementType();
  1241. // Check for a type which we know has a simple scalar argument-passing
  1242. // convention without any padding. (We're specifically looking for 32
  1243. // and 64-bit integer and integer-equivalents, float, and double.)
  1244. if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
  1245. !Ty->isEnumeralType() && !Ty->isBlockPointerType())
  1246. return false;
  1247. uint64_t Size = Context.getTypeSize(Ty);
  1248. return Size == 32 || Size == 64;
  1249. }
  1250. static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD,
  1251. uint64_t &Size) {
  1252. for (const auto *FD : RD->fields()) {
  1253. // Scalar arguments on the stack get 4 byte alignment on x86. If the
  1254. // argument is smaller than 32-bits, expanding the struct will create
  1255. // alignment padding.
  1256. if (!is32Or64BitBasicType(FD->getType(), Context))
  1257. return false;
  1258. // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
  1259. // how to expand them yet, and the predicate for telling if a bitfield still
  1260. // counts as "basic" is more complicated than what we were doing previously.
  1261. if (FD->isBitField())
  1262. return false;
  1263. Size += Context.getTypeSize(FD->getType());
  1264. }
  1265. return true;
  1266. }
  1267. static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD,
  1268. uint64_t &Size) {
  1269. // Don't do this if there are any non-empty bases.
  1270. for (const CXXBaseSpecifier &Base : RD->bases()) {
  1271. if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(),
  1272. Size))
  1273. return false;
  1274. }
  1275. if (!addFieldSizes(Context, RD, Size))
  1276. return false;
  1277. return true;
  1278. }
  1279. /// Test whether an argument type which is to be passed indirectly (on the
  1280. /// stack) would have the equivalent layout if it was expanded into separate
  1281. /// arguments. If so, we prefer to do the latter to avoid inhibiting
  1282. /// optimizations.
  1283. bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
  1284. // We can only expand structure types.
  1285. const RecordType *RT = Ty->getAs<RecordType>();
  1286. if (!RT)
  1287. return false;
  1288. const RecordDecl *RD = RT->getDecl();
  1289. uint64_t Size = 0;
  1290. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  1291. if (!IsWin32StructABI) {
  1292. // On non-Windows, we have to conservatively match our old bitcode
  1293. // prototypes in order to be ABI-compatible at the bitcode level.
  1294. if (!CXXRD->isCLike())
  1295. return false;
  1296. } else {
  1297. // Don't do this for dynamic classes.
  1298. if (CXXRD->isDynamicClass())
  1299. return false;
  1300. }
  1301. if (!addBaseAndFieldSizes(getContext(), CXXRD, Size))
  1302. return false;
  1303. } else {
  1304. if (!addFieldSizes(getContext(), RD, Size))
  1305. return false;
  1306. }
  1307. // We can do this if there was no alignment padding.
  1308. return Size == getContext().getTypeSize(Ty);
  1309. }
  1310. ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
  1311. // If the return value is indirect, then the hidden argument is consuming one
  1312. // integer register.
  1313. if (State.FreeRegs) {
  1314. --State.FreeRegs;
  1315. if (!IsMCUABI)
  1316. return getNaturalAlignIndirectInReg(RetTy);
  1317. }
  1318. return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
  1319. }
  1320. ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
  1321. CCState &State) const {
  1322. if (RetTy->isVoidType())
  1323. return ABIArgInfo::getIgnore();
  1324. const Type *Base = nullptr;
  1325. uint64_t NumElts = 0;
  1326. if ((State.CC == llvm::CallingConv::X86_VectorCall ||
  1327. State.CC == llvm::CallingConv::X86_RegCall) &&
  1328. isHomogeneousAggregate(RetTy, Base, NumElts)) {
  1329. // The LLVM struct type for such an aggregate should lower properly.
  1330. return ABIArgInfo::getDirect();
  1331. }
  1332. if (const VectorType *VT = RetTy->getAs<VectorType>()) {
  1333. // On Darwin, some vectors are returned in registers.
  1334. if (IsDarwinVectorABI) {
  1335. uint64_t Size = getContext().getTypeSize(RetTy);
  1336. // 128-bit vectors are a special case; they are returned in
  1337. // registers and we need to make sure to pick a type the LLVM
  1338. // backend will like.
  1339. if (Size == 128)
  1340. return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
  1341. llvm::Type::getInt64Ty(getVMContext()), 2));
  1342. // Always return in register if it fits in a general purpose
  1343. // register, or if it is 64 bits and has a single element.
  1344. if ((Size == 8 || Size == 16 || Size == 32) ||
  1345. (Size == 64 && VT->getNumElements() == 1))
  1346. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  1347. Size));
  1348. return getIndirectReturnResult(RetTy, State);
  1349. }
  1350. return ABIArgInfo::getDirect();
  1351. }
  1352. if (isAggregateTypeForABI(RetTy)) {
  1353. if (const RecordType *RT = RetTy->getAs<RecordType>()) {
  1354. // Structures with flexible arrays are always indirect.
  1355. if (RT->getDecl()->hasFlexibleArrayMember())
  1356. return getIndirectReturnResult(RetTy, State);
  1357. }
  1358. // If specified, structs and unions are always indirect.
  1359. if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
  1360. return getIndirectReturnResult(RetTy, State);
  1361. // Ignore empty structs/unions.
  1362. if (isEmptyRecord(getContext(), RetTy, true))
  1363. return ABIArgInfo::getIgnore();
  1364. // Return complex of _Float16 as <2 x half> so the backend will use xmm0.
  1365. if (const ComplexType *CT = RetTy->getAs<ComplexType>()) {
  1366. QualType ET = getContext().getCanonicalType(CT->getElementType());
  1367. if (ET->isFloat16Type())
  1368. return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
  1369. llvm::Type::getHalfTy(getVMContext()), 2));
  1370. }
  1371. // Small structures which are register sized are generally returned
  1372. // in a register.
  1373. if (shouldReturnTypeInRegister(RetTy, getContext())) {
  1374. uint64_t Size = getContext().getTypeSize(RetTy);
  1375. // As a special-case, if the struct is a "single-element" struct, and
  1376. // the field is of type "float" or "double", return it in a
  1377. // floating-point register. (MSVC does not apply this special case.)
  1378. // We apply a similar transformation for pointer types to improve the
  1379. // quality of the generated IR.
  1380. if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
  1381. if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
  1382. || SeltTy->hasPointerRepresentation())
  1383. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  1384. // FIXME: We should be able to narrow this integer in cases with dead
  1385. // padding.
  1386. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
  1387. }
  1388. return getIndirectReturnResult(RetTy, State);
  1389. }
  1390. // Treat an enum type as its underlying type.
  1391. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  1392. RetTy = EnumTy->getDecl()->getIntegerType();
  1393. if (const auto *EIT = RetTy->getAs<BitIntType>())
  1394. if (EIT->getNumBits() > 64)
  1395. return getIndirectReturnResult(RetTy, State);
  1396. return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
  1397. : ABIArgInfo::getDirect());
  1398. }
  1399. static bool isSIMDVectorType(ASTContext &Context, QualType Ty) {
  1400. return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
  1401. }
  1402. static bool isRecordWithSIMDVectorType(ASTContext &Context, QualType Ty) {
  1403. const RecordType *RT = Ty->getAs<RecordType>();
  1404. if (!RT)
  1405. return false;
  1406. const RecordDecl *RD = RT->getDecl();
  1407. // If this is a C++ record, check the bases first.
  1408. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  1409. for (const auto &I : CXXRD->bases())
  1410. if (!isRecordWithSIMDVectorType(Context, I.getType()))
  1411. return false;
  1412. for (const auto *i : RD->fields()) {
  1413. QualType FT = i->getType();
  1414. if (isSIMDVectorType(Context, FT))
  1415. return true;
  1416. if (isRecordWithSIMDVectorType(Context, FT))
  1417. return true;
  1418. }
  1419. return false;
  1420. }
  1421. unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
  1422. unsigned Align) const {
  1423. // Otherwise, if the alignment is less than or equal to the minimum ABI
  1424. // alignment, just use the default; the backend will handle this.
  1425. if (Align <= MinABIStackAlignInBytes)
  1426. return 0; // Use default alignment.
  1427. if (IsLinuxABI) {
  1428. // Exclude other System V OS (e.g Darwin, PS4 and FreeBSD) since we don't
  1429. // want to spend any effort dealing with the ramifications of ABI breaks.
  1430. //
  1431. // If the vector type is __m128/__m256/__m512, return the default alignment.
  1432. if (Ty->isVectorType() && (Align == 16 || Align == 32 || Align == 64))
  1433. return Align;
  1434. }
  1435. // On non-Darwin, the stack type alignment is always 4.
  1436. if (!IsDarwinVectorABI) {
  1437. // Set explicit alignment, since we may need to realign the top.
  1438. return MinABIStackAlignInBytes;
  1439. }
  1440. // Otherwise, if the type contains an SSE vector type, the alignment is 16.
  1441. if (Align >= 16 && (isSIMDVectorType(getContext(), Ty) ||
  1442. isRecordWithSIMDVectorType(getContext(), Ty)))
  1443. return 16;
  1444. return MinABIStackAlignInBytes;
  1445. }
  1446. ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
  1447. CCState &State) const {
  1448. if (!ByVal) {
  1449. if (State.FreeRegs) {
  1450. --State.FreeRegs; // Non-byval indirects just use one pointer.
  1451. if (!IsMCUABI)
  1452. return getNaturalAlignIndirectInReg(Ty);
  1453. }
  1454. return getNaturalAlignIndirect(Ty, false);
  1455. }
  1456. // Compute the byval alignment.
  1457. unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
  1458. unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
  1459. if (StackAlign == 0)
  1460. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
  1461. // If the stack alignment is less than the type alignment, realign the
  1462. // argument.
  1463. bool Realign = TypeAlign > StackAlign;
  1464. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
  1465. /*ByVal=*/true, Realign);
  1466. }
  1467. X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
  1468. const Type *T = isSingleElementStruct(Ty, getContext());
  1469. if (!T)
  1470. T = Ty.getTypePtr();
  1471. if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
  1472. BuiltinType::Kind K = BT->getKind();
  1473. if (K == BuiltinType::Float || K == BuiltinType::Double)
  1474. return Float;
  1475. }
  1476. return Integer;
  1477. }
  1478. bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
  1479. if (!IsSoftFloatABI) {
  1480. Class C = classify(Ty);
  1481. if (C == Float)
  1482. return false;
  1483. }
  1484. unsigned Size = getContext().getTypeSize(Ty);
  1485. unsigned SizeInRegs = (Size + 31) / 32;
  1486. if (SizeInRegs == 0)
  1487. return false;
  1488. if (!IsMCUABI) {
  1489. if (SizeInRegs > State.FreeRegs) {
  1490. State.FreeRegs = 0;
  1491. return false;
  1492. }
  1493. } else {
  1494. // The MCU psABI allows passing parameters in-reg even if there are
  1495. // earlier parameters that are passed on the stack. Also,
  1496. // it does not allow passing >8-byte structs in-register,
  1497. // even if there are 3 free registers available.
  1498. if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
  1499. return false;
  1500. }
  1501. State.FreeRegs -= SizeInRegs;
  1502. return true;
  1503. }
  1504. bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
  1505. bool &InReg,
  1506. bool &NeedsPadding) const {
  1507. // On Windows, aggregates other than HFAs are never passed in registers, and
  1508. // they do not consume register slots. Homogenous floating-point aggregates
  1509. // (HFAs) have already been dealt with at this point.
  1510. if (IsWin32StructABI && isAggregateTypeForABI(Ty))
  1511. return false;
  1512. NeedsPadding = false;
  1513. InReg = !IsMCUABI;
  1514. if (!updateFreeRegs(Ty, State))
  1515. return false;
  1516. if (IsMCUABI)
  1517. return true;
  1518. if (State.CC == llvm::CallingConv::X86_FastCall ||
  1519. State.CC == llvm::CallingConv::X86_VectorCall ||
  1520. State.CC == llvm::CallingConv::X86_RegCall) {
  1521. if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
  1522. NeedsPadding = true;
  1523. return false;
  1524. }
  1525. return true;
  1526. }
  1527. bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
  1528. bool IsPtrOrInt = (getContext().getTypeSize(Ty) <= 32) &&
  1529. (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
  1530. Ty->isReferenceType());
  1531. if (!IsPtrOrInt && (State.CC == llvm::CallingConv::X86_FastCall ||
  1532. State.CC == llvm::CallingConv::X86_VectorCall))
  1533. return false;
  1534. if (!updateFreeRegs(Ty, State))
  1535. return false;
  1536. if (!IsPtrOrInt && State.CC == llvm::CallingConv::X86_RegCall)
  1537. return false;
  1538. // Return true to apply inreg to all legal parameters except for MCU targets.
  1539. return !IsMCUABI;
  1540. }
  1541. void X86_32ABIInfo::runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const {
  1542. // Vectorcall x86 works subtly different than in x64, so the format is
  1543. // a bit different than the x64 version. First, all vector types (not HVAs)
  1544. // are assigned, with the first 6 ending up in the [XYZ]MM0-5 registers.
  1545. // This differs from the x64 implementation, where the first 6 by INDEX get
  1546. // registers.
  1547. // In the second pass over the arguments, HVAs are passed in the remaining
  1548. // vector registers if possible, or indirectly by address. The address will be
  1549. // passed in ECX/EDX if available. Any other arguments are passed according to
  1550. // the usual fastcall rules.
  1551. MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments();
  1552. for (int I = 0, E = Args.size(); I < E; ++I) {
  1553. const Type *Base = nullptr;
  1554. uint64_t NumElts = 0;
  1555. const QualType &Ty = Args[I].type;
  1556. if ((Ty->isVectorType() || Ty->isBuiltinType()) &&
  1557. isHomogeneousAggregate(Ty, Base, NumElts)) {
  1558. if (State.FreeSSERegs >= NumElts) {
  1559. State.FreeSSERegs -= NumElts;
  1560. Args[I].info = ABIArgInfo::getDirectInReg();
  1561. State.IsPreassigned.set(I);
  1562. }
  1563. }
  1564. }
  1565. }
  1566. ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
  1567. CCState &State) const {
  1568. // FIXME: Set alignment on indirect arguments.
  1569. bool IsFastCall = State.CC == llvm::CallingConv::X86_FastCall;
  1570. bool IsRegCall = State.CC == llvm::CallingConv::X86_RegCall;
  1571. bool IsVectorCall = State.CC == llvm::CallingConv::X86_VectorCall;
  1572. Ty = useFirstFieldIfTransparentUnion(Ty);
  1573. TypeInfo TI = getContext().getTypeInfo(Ty);
  1574. // Check with the C++ ABI first.
  1575. const RecordType *RT = Ty->getAs<RecordType>();
  1576. if (RT) {
  1577. CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
  1578. if (RAA == CGCXXABI::RAA_Indirect) {
  1579. return getIndirectResult(Ty, false, State);
  1580. } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
  1581. // The field index doesn't matter, we'll fix it up later.
  1582. return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
  1583. }
  1584. }
  1585. // Regcall uses the concept of a homogenous vector aggregate, similar
  1586. // to other targets.
  1587. const Type *Base = nullptr;
  1588. uint64_t NumElts = 0;
  1589. if ((IsRegCall || IsVectorCall) &&
  1590. isHomogeneousAggregate(Ty, Base, NumElts)) {
  1591. if (State.FreeSSERegs >= NumElts) {
  1592. State.FreeSSERegs -= NumElts;
  1593. // Vectorcall passes HVAs directly and does not flatten them, but regcall
  1594. // does.
  1595. if (IsVectorCall)
  1596. return getDirectX86Hva();
  1597. if (Ty->isBuiltinType() || Ty->isVectorType())
  1598. return ABIArgInfo::getDirect();
  1599. return ABIArgInfo::getExpand();
  1600. }
  1601. return getIndirectResult(Ty, /*ByVal=*/false, State);
  1602. }
  1603. if (isAggregateTypeForABI(Ty)) {
  1604. // Structures with flexible arrays are always indirect.
  1605. // FIXME: This should not be byval!
  1606. if (RT && RT->getDecl()->hasFlexibleArrayMember())
  1607. return getIndirectResult(Ty, true, State);
  1608. // Ignore empty structs/unions on non-Windows.
  1609. if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
  1610. return ABIArgInfo::getIgnore();
  1611. llvm::LLVMContext &LLVMContext = getVMContext();
  1612. llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
  1613. bool NeedsPadding = false;
  1614. bool InReg;
  1615. if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
  1616. unsigned SizeInRegs = (TI.Width + 31) / 32;
  1617. SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
  1618. llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
  1619. if (InReg)
  1620. return ABIArgInfo::getDirectInReg(Result);
  1621. else
  1622. return ABIArgInfo::getDirect(Result);
  1623. }
  1624. llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
  1625. // Pass over-aligned aggregates on Windows indirectly. This behavior was
  1626. // added in MSVC 2015.
  1627. if (IsWin32StructABI && TI.isAlignRequired() && TI.Align > 32)
  1628. return getIndirectResult(Ty, /*ByVal=*/false, State);
  1629. // Expand small (<= 128-bit) record types when we know that the stack layout
  1630. // of those arguments will match the struct. This is important because the
  1631. // LLVM backend isn't smart enough to remove byval, which inhibits many
  1632. // optimizations.
  1633. // Don't do this for the MCU if there are still free integer registers
  1634. // (see X86_64 ABI for full explanation).
  1635. if (TI.Width <= 4 * 32 && (!IsMCUABI || State.FreeRegs == 0) &&
  1636. canExpandIndirectArgument(Ty))
  1637. return ABIArgInfo::getExpandWithPadding(
  1638. IsFastCall || IsVectorCall || IsRegCall, PaddingType);
  1639. return getIndirectResult(Ty, true, State);
  1640. }
  1641. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  1642. // On Windows, vectors are passed directly if registers are available, or
  1643. // indirectly if not. This avoids the need to align argument memory. Pass
  1644. // user-defined vector types larger than 512 bits indirectly for simplicity.
  1645. if (IsWin32StructABI) {
  1646. if (TI.Width <= 512 && State.FreeSSERegs > 0) {
  1647. --State.FreeSSERegs;
  1648. return ABIArgInfo::getDirectInReg();
  1649. }
  1650. return getIndirectResult(Ty, /*ByVal=*/false, State);
  1651. }
  1652. // On Darwin, some vectors are passed in memory, we handle this by passing
  1653. // it as an i8/i16/i32/i64.
  1654. if (IsDarwinVectorABI) {
  1655. if ((TI.Width == 8 || TI.Width == 16 || TI.Width == 32) ||
  1656. (TI.Width == 64 && VT->getNumElements() == 1))
  1657. return ABIArgInfo::getDirect(
  1658. llvm::IntegerType::get(getVMContext(), TI.Width));
  1659. }
  1660. if (IsX86_MMXType(CGT.ConvertType(Ty)))
  1661. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
  1662. return ABIArgInfo::getDirect();
  1663. }
  1664. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  1665. Ty = EnumTy->getDecl()->getIntegerType();
  1666. bool InReg = shouldPrimitiveUseInReg(Ty, State);
  1667. if (isPromotableIntegerTypeForABI(Ty)) {
  1668. if (InReg)
  1669. return ABIArgInfo::getExtendInReg(Ty);
  1670. return ABIArgInfo::getExtend(Ty);
  1671. }
  1672. if (const auto *EIT = Ty->getAs<BitIntType>()) {
  1673. if (EIT->getNumBits() <= 64) {
  1674. if (InReg)
  1675. return ABIArgInfo::getDirectInReg();
  1676. return ABIArgInfo::getDirect();
  1677. }
  1678. return getIndirectResult(Ty, /*ByVal=*/false, State);
  1679. }
  1680. if (InReg)
  1681. return ABIArgInfo::getDirectInReg();
  1682. return ABIArgInfo::getDirect();
  1683. }
  1684. void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  1685. CCState State(FI);
  1686. if (IsMCUABI)
  1687. State.FreeRegs = 3;
  1688. else if (State.CC == llvm::CallingConv::X86_FastCall) {
  1689. State.FreeRegs = 2;
  1690. State.FreeSSERegs = 3;
  1691. } else if (State.CC == llvm::CallingConv::X86_VectorCall) {
  1692. State.FreeRegs = 2;
  1693. State.FreeSSERegs = 6;
  1694. } else if (FI.getHasRegParm())
  1695. State.FreeRegs = FI.getRegParm();
  1696. else if (State.CC == llvm::CallingConv::X86_RegCall) {
  1697. State.FreeRegs = 5;
  1698. State.FreeSSERegs = 8;
  1699. } else if (IsWin32StructABI) {
  1700. // Since MSVC 2015, the first three SSE vectors have been passed in
  1701. // registers. The rest are passed indirectly.
  1702. State.FreeRegs = DefaultNumRegisterParameters;
  1703. State.FreeSSERegs = 3;
  1704. } else
  1705. State.FreeRegs = DefaultNumRegisterParameters;
  1706. if (!::classifyReturnType(getCXXABI(), FI, *this)) {
  1707. FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
  1708. } else if (FI.getReturnInfo().isIndirect()) {
  1709. // The C++ ABI is not aware of register usage, so we have to check if the
  1710. // return value was sret and put it in a register ourselves if appropriate.
  1711. if (State.FreeRegs) {
  1712. --State.FreeRegs; // The sret parameter consumes a register.
  1713. if (!IsMCUABI)
  1714. FI.getReturnInfo().setInReg(true);
  1715. }
  1716. }
  1717. // The chain argument effectively gives us another free register.
  1718. if (FI.isChainCall())
  1719. ++State.FreeRegs;
  1720. // For vectorcall, do a first pass over the arguments, assigning FP and vector
  1721. // arguments to XMM registers as available.
  1722. if (State.CC == llvm::CallingConv::X86_VectorCall)
  1723. runVectorCallFirstPass(FI, State);
  1724. bool UsedInAlloca = false;
  1725. MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments();
  1726. for (int I = 0, E = Args.size(); I < E; ++I) {
  1727. // Skip arguments that have already been assigned.
  1728. if (State.IsPreassigned.test(I))
  1729. continue;
  1730. Args[I].info = classifyArgumentType(Args[I].type, State);
  1731. UsedInAlloca |= (Args[I].info.getKind() == ABIArgInfo::InAlloca);
  1732. }
  1733. // If we needed to use inalloca for any argument, do a second pass and rewrite
  1734. // all the memory arguments to use inalloca.
  1735. if (UsedInAlloca)
  1736. rewriteWithInAlloca(FI);
  1737. }
  1738. void
  1739. X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
  1740. CharUnits &StackOffset, ABIArgInfo &Info,
  1741. QualType Type) const {
  1742. // Arguments are always 4-byte-aligned.
  1743. CharUnits WordSize = CharUnits::fromQuantity(4);
  1744. assert(StackOffset.isMultipleOf(WordSize) && "unaligned inalloca struct");
  1745. // sret pointers and indirect things will require an extra pointer
  1746. // indirection, unless they are byval. Most things are byval, and will not
  1747. // require this indirection.
  1748. bool IsIndirect = false;
  1749. if (Info.isIndirect() && !Info.getIndirectByVal())
  1750. IsIndirect = true;
  1751. Info = ABIArgInfo::getInAlloca(FrameFields.size(), IsIndirect);
  1752. llvm::Type *LLTy = CGT.ConvertTypeForMem(Type);
  1753. if (IsIndirect)
  1754. LLTy = LLTy->getPointerTo(0);
  1755. FrameFields.push_back(LLTy);
  1756. StackOffset += IsIndirect ? WordSize : getContext().getTypeSizeInChars(Type);
  1757. // Insert padding bytes to respect alignment.
  1758. CharUnits FieldEnd = StackOffset;
  1759. StackOffset = FieldEnd.alignTo(WordSize);
  1760. if (StackOffset != FieldEnd) {
  1761. CharUnits NumBytes = StackOffset - FieldEnd;
  1762. llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
  1763. Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
  1764. FrameFields.push_back(Ty);
  1765. }
  1766. }
  1767. static bool isArgInAlloca(const ABIArgInfo &Info) {
  1768. // Leave ignored and inreg arguments alone.
  1769. switch (Info.getKind()) {
  1770. case ABIArgInfo::InAlloca:
  1771. return true;
  1772. case ABIArgInfo::Ignore:
  1773. case ABIArgInfo::IndirectAliased:
  1774. return false;
  1775. case ABIArgInfo::Indirect:
  1776. case ABIArgInfo::Direct:
  1777. case ABIArgInfo::Extend:
  1778. return !Info.getInReg();
  1779. case ABIArgInfo::Expand:
  1780. case ABIArgInfo::CoerceAndExpand:
  1781. // These are aggregate types which are never passed in registers when
  1782. // inalloca is involved.
  1783. return true;
  1784. }
  1785. llvm_unreachable("invalid enum");
  1786. }
  1787. void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
  1788. assert(IsWin32StructABI && "inalloca only supported on win32");
  1789. // Build a packed struct type for all of the arguments in memory.
  1790. SmallVector<llvm::Type *, 6> FrameFields;
  1791. // The stack alignment is always 4.
  1792. CharUnits StackAlign = CharUnits::fromQuantity(4);
  1793. CharUnits StackOffset;
  1794. CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
  1795. // Put 'this' into the struct before 'sret', if necessary.
  1796. bool IsThisCall =
  1797. FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
  1798. ABIArgInfo &Ret = FI.getReturnInfo();
  1799. if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
  1800. isArgInAlloca(I->info)) {
  1801. addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
  1802. ++I;
  1803. }
  1804. // Put the sret parameter into the inalloca struct if it's in memory.
  1805. if (Ret.isIndirect() && !Ret.getInReg()) {
  1806. addFieldToArgStruct(FrameFields, StackOffset, Ret, FI.getReturnType());
  1807. // On Windows, the hidden sret parameter is always returned in eax.
  1808. Ret.setInAllocaSRet(IsWin32StructABI);
  1809. }
  1810. // Skip the 'this' parameter in ecx.
  1811. if (IsThisCall)
  1812. ++I;
  1813. // Put arguments passed in memory into the struct.
  1814. for (; I != E; ++I) {
  1815. if (isArgInAlloca(I->info))
  1816. addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
  1817. }
  1818. FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
  1819. /*isPacked=*/true),
  1820. StackAlign);
  1821. }
  1822. Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
  1823. Address VAListAddr, QualType Ty) const {
  1824. auto TypeInfo = getContext().getTypeInfoInChars(Ty);
  1825. // x86-32 changes the alignment of certain arguments on the stack.
  1826. //
  1827. // Just messing with TypeInfo like this works because we never pass
  1828. // anything indirectly.
  1829. TypeInfo.Align = CharUnits::fromQuantity(
  1830. getTypeStackAlignInBytes(Ty, TypeInfo.Align.getQuantity()));
  1831. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
  1832. TypeInfo, CharUnits::fromQuantity(4),
  1833. /*AllowHigherAlign*/ true);
  1834. }
  1835. bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
  1836. const llvm::Triple &Triple, const CodeGenOptions &Opts) {
  1837. assert(Triple.getArch() == llvm::Triple::x86);
  1838. switch (Opts.getStructReturnConvention()) {
  1839. case CodeGenOptions::SRCK_Default:
  1840. break;
  1841. case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
  1842. return false;
  1843. case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
  1844. return true;
  1845. }
  1846. if (Triple.isOSDarwin() || Triple.isOSIAMCU())
  1847. return true;
  1848. switch (Triple.getOS()) {
  1849. case llvm::Triple::DragonFly:
  1850. case llvm::Triple::FreeBSD:
  1851. case llvm::Triple::OpenBSD:
  1852. case llvm::Triple::Win32:
  1853. return true;
  1854. default:
  1855. return false;
  1856. }
  1857. }
  1858. static void addX86InterruptAttrs(const FunctionDecl *FD, llvm::GlobalValue *GV,
  1859. CodeGen::CodeGenModule &CGM) {
  1860. if (!FD->hasAttr<AnyX86InterruptAttr>())
  1861. return;
  1862. llvm::Function *Fn = cast<llvm::Function>(GV);
  1863. Fn->setCallingConv(llvm::CallingConv::X86_INTR);
  1864. if (FD->getNumParams() == 0)
  1865. return;
  1866. auto PtrTy = cast<PointerType>(FD->getParamDecl(0)->getType());
  1867. llvm::Type *ByValTy = CGM.getTypes().ConvertType(PtrTy->getPointeeType());
  1868. llvm::Attribute NewAttr = llvm::Attribute::getWithByValType(
  1869. Fn->getContext(), ByValTy);
  1870. Fn->addParamAttr(0, NewAttr);
  1871. }
  1872. void X86_32TargetCodeGenInfo::setTargetAttributes(
  1873. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
  1874. if (GV->isDeclaration())
  1875. return;
  1876. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  1877. if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
  1878. llvm::Function *Fn = cast<llvm::Function>(GV);
  1879. Fn->addFnAttr("stackrealign");
  1880. }
  1881. addX86InterruptAttrs(FD, GV, CGM);
  1882. }
  1883. }
  1884. bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
  1885. CodeGen::CodeGenFunction &CGF,
  1886. llvm::Value *Address) const {
  1887. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  1888. llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
  1889. // 0-7 are the eight integer registers; the order is different
  1890. // on Darwin (for EH), but the range is the same.
  1891. // 8 is %eip.
  1892. AssignToArrayRange(Builder, Address, Four8, 0, 8);
  1893. if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
  1894. // 12-16 are st(0..4). Not sure why we stop at 4.
  1895. // These have size 16, which is sizeof(long double) on
  1896. // platforms with 8-byte alignment for that type.
  1897. llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
  1898. AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
  1899. } else {
  1900. // 9 is %eflags, which doesn't get a size on Darwin for some
  1901. // reason.
  1902. Builder.CreateAlignedStore(
  1903. Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
  1904. CharUnits::One());
  1905. // 11-16 are st(0..5). Not sure why we stop at 5.
  1906. // These have size 12, which is sizeof(long double) on
  1907. // platforms with 4-byte alignment for that type.
  1908. llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
  1909. AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
  1910. }
  1911. return false;
  1912. }
  1913. //===----------------------------------------------------------------------===//
  1914. // X86-64 ABI Implementation
  1915. //===----------------------------------------------------------------------===//
  1916. namespace {
  1917. /// The AVX ABI level for X86 targets.
  1918. enum class X86AVXABILevel {
  1919. None,
  1920. AVX,
  1921. AVX512
  1922. };
  1923. /// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
  1924. static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
  1925. switch (AVXLevel) {
  1926. case X86AVXABILevel::AVX512:
  1927. return 512;
  1928. case X86AVXABILevel::AVX:
  1929. return 256;
  1930. case X86AVXABILevel::None:
  1931. return 128;
  1932. }
  1933. llvm_unreachable("Unknown AVXLevel");
  1934. }
  1935. /// X86_64ABIInfo - The X86_64 ABI information.
  1936. class X86_64ABIInfo : public ABIInfo {
  1937. enum Class {
  1938. Integer = 0,
  1939. SSE,
  1940. SSEUp,
  1941. X87,
  1942. X87Up,
  1943. ComplexX87,
  1944. NoClass,
  1945. Memory
  1946. };
  1947. /// merge - Implement the X86_64 ABI merging algorithm.
  1948. ///
  1949. /// Merge an accumulating classification \arg Accum with a field
  1950. /// classification \arg Field.
  1951. ///
  1952. /// \param Accum - The accumulating classification. This should
  1953. /// always be either NoClass or the result of a previous merge
  1954. /// call. In addition, this should never be Memory (the caller
  1955. /// should just return Memory for the aggregate).
  1956. static Class merge(Class Accum, Class Field);
  1957. /// postMerge - Implement the X86_64 ABI post merging algorithm.
  1958. ///
  1959. /// Post merger cleanup, reduces a malformed Hi and Lo pair to
  1960. /// final MEMORY or SSE classes when necessary.
  1961. ///
  1962. /// \param AggregateSize - The size of the current aggregate in
  1963. /// the classification process.
  1964. ///
  1965. /// \param Lo - The classification for the parts of the type
  1966. /// residing in the low word of the containing object.
  1967. ///
  1968. /// \param Hi - The classification for the parts of the type
  1969. /// residing in the higher words of the containing object.
  1970. ///
  1971. void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
  1972. /// classify - Determine the x86_64 register classes in which the
  1973. /// given type T should be passed.
  1974. ///
  1975. /// \param Lo - The classification for the parts of the type
  1976. /// residing in the low word of the containing object.
  1977. ///
  1978. /// \param Hi - The classification for the parts of the type
  1979. /// residing in the high word of the containing object.
  1980. ///
  1981. /// \param OffsetBase - The bit offset of this type in the
  1982. /// containing object. Some parameters are classified different
  1983. /// depending on whether they straddle an eightbyte boundary.
  1984. ///
  1985. /// \param isNamedArg - Whether the argument in question is a "named"
  1986. /// argument, as used in AMD64-ABI 3.5.7.
  1987. ///
  1988. /// \param IsRegCall - Whether the calling conversion is regcall.
  1989. ///
  1990. /// If a word is unused its result will be NoClass; if a type should
  1991. /// be passed in Memory then at least the classification of \arg Lo
  1992. /// will be Memory.
  1993. ///
  1994. /// The \arg Lo class will be NoClass iff the argument is ignored.
  1995. ///
  1996. /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
  1997. /// also be ComplexX87.
  1998. void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
  1999. bool isNamedArg, bool IsRegCall = false) const;
  2000. llvm::Type *GetByteVectorType(QualType Ty) const;
  2001. llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
  2002. unsigned IROffset, QualType SourceTy,
  2003. unsigned SourceOffset) const;
  2004. llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
  2005. unsigned IROffset, QualType SourceTy,
  2006. unsigned SourceOffset) const;
  2007. /// getIndirectResult - Give a source type \arg Ty, return a suitable result
  2008. /// such that the argument will be returned in memory.
  2009. ABIArgInfo getIndirectReturnResult(QualType Ty) const;
  2010. /// getIndirectResult - Give a source type \arg Ty, return a suitable result
  2011. /// such that the argument will be passed in memory.
  2012. ///
  2013. /// \param freeIntRegs - The number of free integer registers remaining
  2014. /// available.
  2015. ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
  2016. ABIArgInfo classifyReturnType(QualType RetTy) const;
  2017. ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs,
  2018. unsigned &neededInt, unsigned &neededSSE,
  2019. bool isNamedArg,
  2020. bool IsRegCall = false) const;
  2021. ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
  2022. unsigned &NeededSSE,
  2023. unsigned &MaxVectorWidth) const;
  2024. ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
  2025. unsigned &NeededSSE,
  2026. unsigned &MaxVectorWidth) const;
  2027. bool IsIllegalVectorType(QualType Ty) const;
  2028. /// The 0.98 ABI revision clarified a lot of ambiguities,
  2029. /// unfortunately in ways that were not always consistent with
  2030. /// certain previous compilers. In particular, platforms which
  2031. /// required strict binary compatibility with older versions of GCC
  2032. /// may need to exempt themselves.
  2033. bool honorsRevision0_98() const {
  2034. return !getTarget().getTriple().isOSDarwin();
  2035. }
  2036. /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to
  2037. /// classify it as INTEGER (for compatibility with older clang compilers).
  2038. bool classifyIntegerMMXAsSSE() const {
  2039. // Clang <= 3.8 did not do this.
  2040. if (getContext().getLangOpts().getClangABICompat() <=
  2041. LangOptions::ClangABI::Ver3_8)
  2042. return false;
  2043. const llvm::Triple &Triple = getTarget().getTriple();
  2044. if (Triple.isOSDarwin() || Triple.isPS() || Triple.isOSFreeBSD())
  2045. return false;
  2046. return true;
  2047. }
  2048. // GCC classifies vectors of __int128 as memory.
  2049. bool passInt128VectorsInMem() const {
  2050. // Clang <= 9.0 did not do this.
  2051. if (getContext().getLangOpts().getClangABICompat() <=
  2052. LangOptions::ClangABI::Ver9)
  2053. return false;
  2054. const llvm::Triple &T = getTarget().getTriple();
  2055. return T.isOSLinux() || T.isOSNetBSD();
  2056. }
  2057. X86AVXABILevel AVXLevel;
  2058. // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
  2059. // 64-bit hardware.
  2060. bool Has64BitPointers;
  2061. public:
  2062. X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
  2063. : ABIInfo(CGT), AVXLevel(AVXLevel),
  2064. Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {}
  2065. bool isPassedUsingAVXType(QualType type) const {
  2066. unsigned neededInt, neededSSE;
  2067. // The freeIntRegs argument doesn't matter here.
  2068. ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
  2069. /*isNamedArg*/true);
  2070. if (info.isDirect()) {
  2071. llvm::Type *ty = info.getCoerceToType();
  2072. if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
  2073. return vectorTy->getPrimitiveSizeInBits().getFixedValue() > 128;
  2074. }
  2075. return false;
  2076. }
  2077. void computeInfo(CGFunctionInfo &FI) const override;
  2078. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  2079. QualType Ty) const override;
  2080. Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  2081. QualType Ty) const override;
  2082. bool has64BitPointers() const {
  2083. return Has64BitPointers;
  2084. }
  2085. };
  2086. /// WinX86_64ABIInfo - The Windows X86_64 ABI information.
  2087. class WinX86_64ABIInfo : public ABIInfo {
  2088. public:
  2089. WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
  2090. : ABIInfo(CGT), AVXLevel(AVXLevel),
  2091. IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
  2092. void computeInfo(CGFunctionInfo &FI) const override;
  2093. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  2094. QualType Ty) const override;
  2095. bool isHomogeneousAggregateBaseType(QualType Ty) const override {
  2096. // FIXME: Assumes vectorcall is in use.
  2097. return isX86VectorTypeForVectorCall(getContext(), Ty);
  2098. }
  2099. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  2100. uint64_t NumMembers) const override {
  2101. // FIXME: Assumes vectorcall is in use.
  2102. return isX86VectorCallAggregateSmallEnough(NumMembers);
  2103. }
  2104. private:
  2105. ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType,
  2106. bool IsVectorCall, bool IsRegCall) const;
  2107. ABIArgInfo reclassifyHvaArgForVectorCall(QualType Ty, unsigned &FreeSSERegs,
  2108. const ABIArgInfo &current) const;
  2109. X86AVXABILevel AVXLevel;
  2110. bool IsMingw64;
  2111. };
  2112. class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
  2113. public:
  2114. X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
  2115. : TargetCodeGenInfo(std::make_unique<X86_64ABIInfo>(CGT, AVXLevel)) {
  2116. SwiftInfo =
  2117. std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/true);
  2118. }
  2119. const X86_64ABIInfo &getABIInfo() const {
  2120. return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
  2121. }
  2122. /// Disable tail call on x86-64. The epilogue code before the tail jump blocks
  2123. /// autoreleaseRV/retainRV and autoreleaseRV/unsafeClaimRV optimizations.
  2124. bool markARCOptimizedReturnCallsAsNoTail() const override { return true; }
  2125. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
  2126. return 7;
  2127. }
  2128. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  2129. llvm::Value *Address) const override {
  2130. llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
  2131. // 0-15 are the 16 integer registers.
  2132. // 16 is %rip.
  2133. AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
  2134. return false;
  2135. }
  2136. llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  2137. StringRef Constraint,
  2138. llvm::Type* Ty) const override {
  2139. return X86AdjustInlineAsmType(CGF, Constraint, Ty);
  2140. }
  2141. bool isNoProtoCallVariadic(const CallArgList &args,
  2142. const FunctionNoProtoType *fnType) const override {
  2143. // The default CC on x86-64 sets %al to the number of SSA
  2144. // registers used, and GCC sets this when calling an unprototyped
  2145. // function, so we override the default behavior. However, don't do
  2146. // that when AVX types are involved: the ABI explicitly states it is
  2147. // undefined, and it doesn't work in practice because of how the ABI
  2148. // defines varargs anyway.
  2149. if (fnType->getCallConv() == CC_C) {
  2150. bool HasAVXType = false;
  2151. for (CallArgList::const_iterator
  2152. it = args.begin(), ie = args.end(); it != ie; ++it) {
  2153. if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
  2154. HasAVXType = true;
  2155. break;
  2156. }
  2157. }
  2158. if (!HasAVXType)
  2159. return true;
  2160. }
  2161. return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
  2162. }
  2163. llvm::Constant *
  2164. getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
  2165. unsigned Sig = (0xeb << 0) | // jmp rel8
  2166. (0x06 << 8) | // .+0x08
  2167. ('v' << 16) |
  2168. ('2' << 24);
  2169. return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
  2170. }
  2171. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2172. CodeGen::CodeGenModule &CGM) const override {
  2173. if (GV->isDeclaration())
  2174. return;
  2175. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  2176. if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
  2177. llvm::Function *Fn = cast<llvm::Function>(GV);
  2178. Fn->addFnAttr("stackrealign");
  2179. }
  2180. addX86InterruptAttrs(FD, GV, CGM);
  2181. }
  2182. }
  2183. void checkFunctionCallABI(CodeGenModule &CGM, SourceLocation CallLoc,
  2184. const FunctionDecl *Caller,
  2185. const FunctionDecl *Callee,
  2186. const CallArgList &Args) const override;
  2187. };
  2188. static void initFeatureMaps(const ASTContext &Ctx,
  2189. llvm::StringMap<bool> &CallerMap,
  2190. const FunctionDecl *Caller,
  2191. llvm::StringMap<bool> &CalleeMap,
  2192. const FunctionDecl *Callee) {
  2193. if (CalleeMap.empty() && CallerMap.empty()) {
  2194. // The caller is potentially nullptr in the case where the call isn't in a
  2195. // function. In this case, the getFunctionFeatureMap ensures we just get
  2196. // the TU level setting (since it cannot be modified by 'target'..
  2197. Ctx.getFunctionFeatureMap(CallerMap, Caller);
  2198. Ctx.getFunctionFeatureMap(CalleeMap, Callee);
  2199. }
  2200. }
  2201. static bool checkAVXParamFeature(DiagnosticsEngine &Diag,
  2202. SourceLocation CallLoc,
  2203. const llvm::StringMap<bool> &CallerMap,
  2204. const llvm::StringMap<bool> &CalleeMap,
  2205. QualType Ty, StringRef Feature,
  2206. bool IsArgument) {
  2207. bool CallerHasFeat = CallerMap.lookup(Feature);
  2208. bool CalleeHasFeat = CalleeMap.lookup(Feature);
  2209. if (!CallerHasFeat && !CalleeHasFeat)
  2210. return Diag.Report(CallLoc, diag::warn_avx_calling_convention)
  2211. << IsArgument << Ty << Feature;
  2212. // Mixing calling conventions here is very clearly an error.
  2213. if (!CallerHasFeat || !CalleeHasFeat)
  2214. return Diag.Report(CallLoc, diag::err_avx_calling_convention)
  2215. << IsArgument << Ty << Feature;
  2216. // Else, both caller and callee have the required feature, so there is no need
  2217. // to diagnose.
  2218. return false;
  2219. }
  2220. static bool checkAVXParam(DiagnosticsEngine &Diag, ASTContext &Ctx,
  2221. SourceLocation CallLoc,
  2222. const llvm::StringMap<bool> &CallerMap,
  2223. const llvm::StringMap<bool> &CalleeMap, QualType Ty,
  2224. bool IsArgument) {
  2225. uint64_t Size = Ctx.getTypeSize(Ty);
  2226. if (Size > 256)
  2227. return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty,
  2228. "avx512f", IsArgument);
  2229. if (Size > 128)
  2230. return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty, "avx",
  2231. IsArgument);
  2232. return false;
  2233. }
  2234. void X86_64TargetCodeGenInfo::checkFunctionCallABI(
  2235. CodeGenModule &CGM, SourceLocation CallLoc, const FunctionDecl *Caller,
  2236. const FunctionDecl *Callee, const CallArgList &Args) const {
  2237. llvm::StringMap<bool> CallerMap;
  2238. llvm::StringMap<bool> CalleeMap;
  2239. unsigned ArgIndex = 0;
  2240. // We need to loop through the actual call arguments rather than the
  2241. // function's parameters, in case this variadic.
  2242. for (const CallArg &Arg : Args) {
  2243. // The "avx" feature changes how vectors >128 in size are passed. "avx512f"
  2244. // additionally changes how vectors >256 in size are passed. Like GCC, we
  2245. // warn when a function is called with an argument where this will change.
  2246. // Unlike GCC, we also error when it is an obvious ABI mismatch, that is,
  2247. // the caller and callee features are mismatched.
  2248. // Unfortunately, we cannot do this diagnostic in SEMA, since the callee can
  2249. // change its ABI with attribute-target after this call.
  2250. if (Arg.getType()->isVectorType() &&
  2251. CGM.getContext().getTypeSize(Arg.getType()) > 128) {
  2252. initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee);
  2253. QualType Ty = Arg.getType();
  2254. // The CallArg seems to have desugared the type already, so for clearer
  2255. // diagnostics, replace it with the type in the FunctionDecl if possible.
  2256. if (ArgIndex < Callee->getNumParams())
  2257. Ty = Callee->getParamDecl(ArgIndex)->getType();
  2258. if (checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap,
  2259. CalleeMap, Ty, /*IsArgument*/ true))
  2260. return;
  2261. }
  2262. ++ArgIndex;
  2263. }
  2264. // Check return always, as we don't have a good way of knowing in codegen
  2265. // whether this value is used, tail-called, etc.
  2266. if (Callee->getReturnType()->isVectorType() &&
  2267. CGM.getContext().getTypeSize(Callee->getReturnType()) > 128) {
  2268. initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee);
  2269. checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap,
  2270. CalleeMap, Callee->getReturnType(),
  2271. /*IsArgument*/ false);
  2272. }
  2273. }
  2274. static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
  2275. // If the argument does not end in .lib, automatically add the suffix.
  2276. // If the argument contains a space, enclose it in quotes.
  2277. // This matches the behavior of MSVC.
  2278. bool Quote = Lib.contains(' ');
  2279. std::string ArgStr = Quote ? "\"" : "";
  2280. ArgStr += Lib;
  2281. if (!Lib.endswith_insensitive(".lib") && !Lib.endswith_insensitive(".a"))
  2282. ArgStr += ".lib";
  2283. ArgStr += Quote ? "\"" : "";
  2284. return ArgStr;
  2285. }
  2286. class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
  2287. public:
  2288. WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
  2289. bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
  2290. unsigned NumRegisterParameters)
  2291. : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
  2292. Win32StructABI, NumRegisterParameters, false) {}
  2293. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2294. CodeGen::CodeGenModule &CGM) const override;
  2295. void getDependentLibraryOption(llvm::StringRef Lib,
  2296. llvm::SmallString<24> &Opt) const override {
  2297. Opt = "/DEFAULTLIB:";
  2298. Opt += qualifyWindowsLibrary(Lib);
  2299. }
  2300. void getDetectMismatchOption(llvm::StringRef Name,
  2301. llvm::StringRef Value,
  2302. llvm::SmallString<32> &Opt) const override {
  2303. Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
  2304. }
  2305. };
  2306. static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2307. CodeGen::CodeGenModule &CGM) {
  2308. if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) {
  2309. if (CGM.getCodeGenOpts().StackProbeSize != 4096)
  2310. Fn->addFnAttr("stack-probe-size",
  2311. llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
  2312. if (CGM.getCodeGenOpts().NoStackArgProbe)
  2313. Fn->addFnAttr("no-stack-arg-probe");
  2314. }
  2315. }
  2316. void WinX86_32TargetCodeGenInfo::setTargetAttributes(
  2317. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
  2318. X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
  2319. if (GV->isDeclaration())
  2320. return;
  2321. addStackProbeTargetAttributes(D, GV, CGM);
  2322. }
  2323. class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
  2324. public:
  2325. WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
  2326. X86AVXABILevel AVXLevel)
  2327. : TargetCodeGenInfo(std::make_unique<WinX86_64ABIInfo>(CGT, AVXLevel)) {
  2328. SwiftInfo =
  2329. std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/true);
  2330. }
  2331. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2332. CodeGen::CodeGenModule &CGM) const override;
  2333. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
  2334. return 7;
  2335. }
  2336. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  2337. llvm::Value *Address) const override {
  2338. llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
  2339. // 0-15 are the 16 integer registers.
  2340. // 16 is %rip.
  2341. AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
  2342. return false;
  2343. }
  2344. void getDependentLibraryOption(llvm::StringRef Lib,
  2345. llvm::SmallString<24> &Opt) const override {
  2346. Opt = "/DEFAULTLIB:";
  2347. Opt += qualifyWindowsLibrary(Lib);
  2348. }
  2349. void getDetectMismatchOption(llvm::StringRef Name,
  2350. llvm::StringRef Value,
  2351. llvm::SmallString<32> &Opt) const override {
  2352. Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
  2353. }
  2354. };
  2355. void WinX86_64TargetCodeGenInfo::setTargetAttributes(
  2356. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
  2357. TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
  2358. if (GV->isDeclaration())
  2359. return;
  2360. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  2361. if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
  2362. llvm::Function *Fn = cast<llvm::Function>(GV);
  2363. Fn->addFnAttr("stackrealign");
  2364. }
  2365. addX86InterruptAttrs(FD, GV, CGM);
  2366. }
  2367. addStackProbeTargetAttributes(D, GV, CGM);
  2368. }
  2369. }
  2370. void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
  2371. Class &Hi) const {
  2372. // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
  2373. //
  2374. // (a) If one of the classes is Memory, the whole argument is passed in
  2375. // memory.
  2376. //
  2377. // (b) If X87UP is not preceded by X87, the whole argument is passed in
  2378. // memory.
  2379. //
  2380. // (c) If the size of the aggregate exceeds two eightbytes and the first
  2381. // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
  2382. // argument is passed in memory. NOTE: This is necessary to keep the
  2383. // ABI working for processors that don't support the __m256 type.
  2384. //
  2385. // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
  2386. //
  2387. // Some of these are enforced by the merging logic. Others can arise
  2388. // only with unions; for example:
  2389. // union { _Complex double; unsigned; }
  2390. //
  2391. // Note that clauses (b) and (c) were added in 0.98.
  2392. //
  2393. if (Hi == Memory)
  2394. Lo = Memory;
  2395. if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
  2396. Lo = Memory;
  2397. if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
  2398. Lo = Memory;
  2399. if (Hi == SSEUp && Lo != SSE)
  2400. Hi = SSE;
  2401. }
  2402. X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
  2403. // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
  2404. // classified recursively so that always two fields are
  2405. // considered. The resulting class is calculated according to
  2406. // the classes of the fields in the eightbyte:
  2407. //
  2408. // (a) If both classes are equal, this is the resulting class.
  2409. //
  2410. // (b) If one of the classes is NO_CLASS, the resulting class is
  2411. // the other class.
  2412. //
  2413. // (c) If one of the classes is MEMORY, the result is the MEMORY
  2414. // class.
  2415. //
  2416. // (d) If one of the classes is INTEGER, the result is the
  2417. // INTEGER.
  2418. //
  2419. // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
  2420. // MEMORY is used as class.
  2421. //
  2422. // (f) Otherwise class SSE is used.
  2423. // Accum should never be memory (we should have returned) or
  2424. // ComplexX87 (because this cannot be passed in a structure).
  2425. assert((Accum != Memory && Accum != ComplexX87) &&
  2426. "Invalid accumulated classification during merge.");
  2427. if (Accum == Field || Field == NoClass)
  2428. return Accum;
  2429. if (Field == Memory)
  2430. return Memory;
  2431. if (Accum == NoClass)
  2432. return Field;
  2433. if (Accum == Integer || Field == Integer)
  2434. return Integer;
  2435. if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
  2436. Accum == X87 || Accum == X87Up)
  2437. return Memory;
  2438. return SSE;
  2439. }
  2440. void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, Class &Lo,
  2441. Class &Hi, bool isNamedArg, bool IsRegCall) const {
  2442. // FIXME: This code can be simplified by introducing a simple value class for
  2443. // Class pairs with appropriate constructor methods for the various
  2444. // situations.
  2445. // FIXME: Some of the split computations are wrong; unaligned vectors
  2446. // shouldn't be passed in registers for example, so there is no chance they
  2447. // can straddle an eightbyte. Verify & simplify.
  2448. Lo = Hi = NoClass;
  2449. Class &Current = OffsetBase < 64 ? Lo : Hi;
  2450. Current = Memory;
  2451. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  2452. BuiltinType::Kind k = BT->getKind();
  2453. if (k == BuiltinType::Void) {
  2454. Current = NoClass;
  2455. } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
  2456. Lo = Integer;
  2457. Hi = Integer;
  2458. } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
  2459. Current = Integer;
  2460. } else if (k == BuiltinType::Float || k == BuiltinType::Double ||
  2461. k == BuiltinType::Float16 || k == BuiltinType::BFloat16) {
  2462. Current = SSE;
  2463. } else if (k == BuiltinType::LongDouble) {
  2464. const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
  2465. if (LDF == &llvm::APFloat::IEEEquad()) {
  2466. Lo = SSE;
  2467. Hi = SSEUp;
  2468. } else if (LDF == &llvm::APFloat::x87DoubleExtended()) {
  2469. Lo = X87;
  2470. Hi = X87Up;
  2471. } else if (LDF == &llvm::APFloat::IEEEdouble()) {
  2472. Current = SSE;
  2473. } else
  2474. llvm_unreachable("unexpected long double representation!");
  2475. }
  2476. // FIXME: _Decimal32 and _Decimal64 are SSE.
  2477. // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
  2478. return;
  2479. }
  2480. if (const EnumType *ET = Ty->getAs<EnumType>()) {
  2481. // Classify the underlying integer type.
  2482. classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
  2483. return;
  2484. }
  2485. if (Ty->hasPointerRepresentation()) {
  2486. Current = Integer;
  2487. return;
  2488. }
  2489. if (Ty->isMemberPointerType()) {
  2490. if (Ty->isMemberFunctionPointerType()) {
  2491. if (Has64BitPointers) {
  2492. // If Has64BitPointers, this is an {i64, i64}, so classify both
  2493. // Lo and Hi now.
  2494. Lo = Hi = Integer;
  2495. } else {
  2496. // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
  2497. // straddles an eightbyte boundary, Hi should be classified as well.
  2498. uint64_t EB_FuncPtr = (OffsetBase) / 64;
  2499. uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
  2500. if (EB_FuncPtr != EB_ThisAdj) {
  2501. Lo = Hi = Integer;
  2502. } else {
  2503. Current = Integer;
  2504. }
  2505. }
  2506. } else {
  2507. Current = Integer;
  2508. }
  2509. return;
  2510. }
  2511. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  2512. uint64_t Size = getContext().getTypeSize(VT);
  2513. if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
  2514. // gcc passes the following as integer:
  2515. // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
  2516. // 2 bytes - <2 x char>, <1 x short>
  2517. // 1 byte - <1 x char>
  2518. Current = Integer;
  2519. // If this type crosses an eightbyte boundary, it should be
  2520. // split.
  2521. uint64_t EB_Lo = (OffsetBase) / 64;
  2522. uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
  2523. if (EB_Lo != EB_Hi)
  2524. Hi = Lo;
  2525. } else if (Size == 64) {
  2526. QualType ElementType = VT->getElementType();
  2527. // gcc passes <1 x double> in memory. :(
  2528. if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
  2529. return;
  2530. // gcc passes <1 x long long> as SSE but clang used to unconditionally
  2531. // pass them as integer. For platforms where clang is the de facto
  2532. // platform compiler, we must continue to use integer.
  2533. if (!classifyIntegerMMXAsSSE() &&
  2534. (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
  2535. ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
  2536. ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
  2537. ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
  2538. Current = Integer;
  2539. else
  2540. Current = SSE;
  2541. // If this type crosses an eightbyte boundary, it should be
  2542. // split.
  2543. if (OffsetBase && OffsetBase != 64)
  2544. Hi = Lo;
  2545. } else if (Size == 128 ||
  2546. (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
  2547. QualType ElementType = VT->getElementType();
  2548. // gcc passes 256 and 512 bit <X x __int128> vectors in memory. :(
  2549. if (passInt128VectorsInMem() && Size != 128 &&
  2550. (ElementType->isSpecificBuiltinType(BuiltinType::Int128) ||
  2551. ElementType->isSpecificBuiltinType(BuiltinType::UInt128)))
  2552. return;
  2553. // Arguments of 256-bits are split into four eightbyte chunks. The
  2554. // least significant one belongs to class SSE and all the others to class
  2555. // SSEUP. The original Lo and Hi design considers that types can't be
  2556. // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
  2557. // This design isn't correct for 256-bits, but since there're no cases
  2558. // where the upper parts would need to be inspected, avoid adding
  2559. // complexity and just consider Hi to match the 64-256 part.
  2560. //
  2561. // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
  2562. // registers if they are "named", i.e. not part of the "..." of a
  2563. // variadic function.
  2564. //
  2565. // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
  2566. // split into eight eightbyte chunks, one SSE and seven SSEUP.
  2567. Lo = SSE;
  2568. Hi = SSEUp;
  2569. }
  2570. return;
  2571. }
  2572. if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
  2573. QualType ET = getContext().getCanonicalType(CT->getElementType());
  2574. uint64_t Size = getContext().getTypeSize(Ty);
  2575. if (ET->isIntegralOrEnumerationType()) {
  2576. if (Size <= 64)
  2577. Current = Integer;
  2578. else if (Size <= 128)
  2579. Lo = Hi = Integer;
  2580. } else if (ET->isFloat16Type() || ET == getContext().FloatTy ||
  2581. ET->isBFloat16Type()) {
  2582. Current = SSE;
  2583. } else if (ET == getContext().DoubleTy) {
  2584. Lo = Hi = SSE;
  2585. } else if (ET == getContext().LongDoubleTy) {
  2586. const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
  2587. if (LDF == &llvm::APFloat::IEEEquad())
  2588. Current = Memory;
  2589. else if (LDF == &llvm::APFloat::x87DoubleExtended())
  2590. Current = ComplexX87;
  2591. else if (LDF == &llvm::APFloat::IEEEdouble())
  2592. Lo = Hi = SSE;
  2593. else
  2594. llvm_unreachable("unexpected long double representation!");
  2595. }
  2596. // If this complex type crosses an eightbyte boundary then it
  2597. // should be split.
  2598. uint64_t EB_Real = (OffsetBase) / 64;
  2599. uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
  2600. if (Hi == NoClass && EB_Real != EB_Imag)
  2601. Hi = Lo;
  2602. return;
  2603. }
  2604. if (const auto *EITy = Ty->getAs<BitIntType>()) {
  2605. if (EITy->getNumBits() <= 64)
  2606. Current = Integer;
  2607. else if (EITy->getNumBits() <= 128)
  2608. Lo = Hi = Integer;
  2609. // Larger values need to get passed in memory.
  2610. return;
  2611. }
  2612. if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
  2613. // Arrays are treated like structures.
  2614. uint64_t Size = getContext().getTypeSize(Ty);
  2615. // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
  2616. // than eight eightbytes, ..., it has class MEMORY.
  2617. // regcall ABI doesn't have limitation to an object. The only limitation
  2618. // is the free registers, which will be checked in computeInfo.
  2619. if (!IsRegCall && Size > 512)
  2620. return;
  2621. // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
  2622. // fields, it has class MEMORY.
  2623. //
  2624. // Only need to check alignment of array base.
  2625. if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
  2626. return;
  2627. // Otherwise implement simplified merge. We could be smarter about
  2628. // this, but it isn't worth it and would be harder to verify.
  2629. Current = NoClass;
  2630. uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
  2631. uint64_t ArraySize = AT->getSize().getZExtValue();
  2632. // The only case a 256-bit wide vector could be used is when the array
  2633. // contains a single 256-bit element. Since Lo and Hi logic isn't extended
  2634. // to work for sizes wider than 128, early check and fallback to memory.
  2635. //
  2636. if (Size > 128 &&
  2637. (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
  2638. return;
  2639. for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
  2640. Class FieldLo, FieldHi;
  2641. classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
  2642. Lo = merge(Lo, FieldLo);
  2643. Hi = merge(Hi, FieldHi);
  2644. if (Lo == Memory || Hi == Memory)
  2645. break;
  2646. }
  2647. postMerge(Size, Lo, Hi);
  2648. assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
  2649. return;
  2650. }
  2651. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  2652. uint64_t Size = getContext().getTypeSize(Ty);
  2653. // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
  2654. // than eight eightbytes, ..., it has class MEMORY.
  2655. if (Size > 512)
  2656. return;
  2657. // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
  2658. // copy constructor or a non-trivial destructor, it is passed by invisible
  2659. // reference.
  2660. if (getRecordArgABI(RT, getCXXABI()))
  2661. return;
  2662. const RecordDecl *RD = RT->getDecl();
  2663. // Assume variable sized types are passed in memory.
  2664. if (RD->hasFlexibleArrayMember())
  2665. return;
  2666. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  2667. // Reset Lo class, this will be recomputed.
  2668. Current = NoClass;
  2669. // If this is a C++ record, classify the bases first.
  2670. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  2671. for (const auto &I : CXXRD->bases()) {
  2672. assert(!I.isVirtual() && !I.getType()->isDependentType() &&
  2673. "Unexpected base class!");
  2674. const auto *Base =
  2675. cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
  2676. // Classify this field.
  2677. //
  2678. // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
  2679. // single eightbyte, each is classified separately. Each eightbyte gets
  2680. // initialized to class NO_CLASS.
  2681. Class FieldLo, FieldHi;
  2682. uint64_t Offset =
  2683. OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
  2684. classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
  2685. Lo = merge(Lo, FieldLo);
  2686. Hi = merge(Hi, FieldHi);
  2687. if (Lo == Memory || Hi == Memory) {
  2688. postMerge(Size, Lo, Hi);
  2689. return;
  2690. }
  2691. }
  2692. }
  2693. // Classify the fields one at a time, merging the results.
  2694. unsigned idx = 0;
  2695. bool UseClang11Compat = getContext().getLangOpts().getClangABICompat() <=
  2696. LangOptions::ClangABI::Ver11 ||
  2697. getContext().getTargetInfo().getTriple().isPS();
  2698. bool IsUnion = RT->isUnionType() && !UseClang11Compat;
  2699. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  2700. i != e; ++i, ++idx) {
  2701. uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
  2702. bool BitField = i->isBitField();
  2703. // Ignore padding bit-fields.
  2704. if (BitField && i->isUnnamedBitfield())
  2705. continue;
  2706. // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
  2707. // eight eightbytes, or it contains unaligned fields, it has class MEMORY.
  2708. //
  2709. // The only case a 256-bit or a 512-bit wide vector could be used is when
  2710. // the struct contains a single 256-bit or 512-bit element. Early check
  2711. // and fallback to memory.
  2712. //
  2713. // FIXME: Extended the Lo and Hi logic properly to work for size wider
  2714. // than 128.
  2715. if (Size > 128 &&
  2716. ((!IsUnion && Size != getContext().getTypeSize(i->getType())) ||
  2717. Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
  2718. Lo = Memory;
  2719. postMerge(Size, Lo, Hi);
  2720. return;
  2721. }
  2722. // Note, skip this test for bit-fields, see below.
  2723. if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
  2724. Lo = Memory;
  2725. postMerge(Size, Lo, Hi);
  2726. return;
  2727. }
  2728. // Classify this field.
  2729. //
  2730. // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
  2731. // exceeds a single eightbyte, each is classified
  2732. // separately. Each eightbyte gets initialized to class
  2733. // NO_CLASS.
  2734. Class FieldLo, FieldHi;
  2735. // Bit-fields require special handling, they do not force the
  2736. // structure to be passed in memory even if unaligned, and
  2737. // therefore they can straddle an eightbyte.
  2738. if (BitField) {
  2739. assert(!i->isUnnamedBitfield());
  2740. uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
  2741. uint64_t Size = i->getBitWidthValue(getContext());
  2742. uint64_t EB_Lo = Offset / 64;
  2743. uint64_t EB_Hi = (Offset + Size - 1) / 64;
  2744. if (EB_Lo) {
  2745. assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
  2746. FieldLo = NoClass;
  2747. FieldHi = Integer;
  2748. } else {
  2749. FieldLo = Integer;
  2750. FieldHi = EB_Hi ? Integer : NoClass;
  2751. }
  2752. } else
  2753. classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
  2754. Lo = merge(Lo, FieldLo);
  2755. Hi = merge(Hi, FieldHi);
  2756. if (Lo == Memory || Hi == Memory)
  2757. break;
  2758. }
  2759. postMerge(Size, Lo, Hi);
  2760. }
  2761. }
  2762. ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
  2763. // If this is a scalar LLVM value then assume LLVM will pass it in the right
  2764. // place naturally.
  2765. if (!isAggregateTypeForABI(Ty)) {
  2766. // Treat an enum type as its underlying type.
  2767. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  2768. Ty = EnumTy->getDecl()->getIntegerType();
  2769. if (Ty->isBitIntType())
  2770. return getNaturalAlignIndirect(Ty);
  2771. return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
  2772. : ABIArgInfo::getDirect());
  2773. }
  2774. return getNaturalAlignIndirect(Ty);
  2775. }
  2776. bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
  2777. if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
  2778. uint64_t Size = getContext().getTypeSize(VecTy);
  2779. unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
  2780. if (Size <= 64 || Size > LargestVector)
  2781. return true;
  2782. QualType EltTy = VecTy->getElementType();
  2783. if (passInt128VectorsInMem() &&
  2784. (EltTy->isSpecificBuiltinType(BuiltinType::Int128) ||
  2785. EltTy->isSpecificBuiltinType(BuiltinType::UInt128)))
  2786. return true;
  2787. }
  2788. return false;
  2789. }
  2790. ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
  2791. unsigned freeIntRegs) const {
  2792. // If this is a scalar LLVM value then assume LLVM will pass it in the right
  2793. // place naturally.
  2794. //
  2795. // This assumption is optimistic, as there could be free registers available
  2796. // when we need to pass this argument in memory, and LLVM could try to pass
  2797. // the argument in the free register. This does not seem to happen currently,
  2798. // but this code would be much safer if we could mark the argument with
  2799. // 'onstack'. See PR12193.
  2800. if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty) &&
  2801. !Ty->isBitIntType()) {
  2802. // Treat an enum type as its underlying type.
  2803. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  2804. Ty = EnumTy->getDecl()->getIntegerType();
  2805. return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
  2806. : ABIArgInfo::getDirect());
  2807. }
  2808. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  2809. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  2810. // Compute the byval alignment. We specify the alignment of the byval in all
  2811. // cases so that the mid-level optimizer knows the alignment of the byval.
  2812. unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
  2813. // Attempt to avoid passing indirect results using byval when possible. This
  2814. // is important for good codegen.
  2815. //
  2816. // We do this by coercing the value into a scalar type which the backend can
  2817. // handle naturally (i.e., without using byval).
  2818. //
  2819. // For simplicity, we currently only do this when we have exhausted all of the
  2820. // free integer registers. Doing this when there are free integer registers
  2821. // would require more care, as we would have to ensure that the coerced value
  2822. // did not claim the unused register. That would require either reording the
  2823. // arguments to the function (so that any subsequent inreg values came first),
  2824. // or only doing this optimization when there were no following arguments that
  2825. // might be inreg.
  2826. //
  2827. // We currently expect it to be rare (particularly in well written code) for
  2828. // arguments to be passed on the stack when there are still free integer
  2829. // registers available (this would typically imply large structs being passed
  2830. // by value), so this seems like a fair tradeoff for now.
  2831. //
  2832. // We can revisit this if the backend grows support for 'onstack' parameter
  2833. // attributes. See PR12193.
  2834. if (freeIntRegs == 0) {
  2835. uint64_t Size = getContext().getTypeSize(Ty);
  2836. // If this type fits in an eightbyte, coerce it into the matching integral
  2837. // type, which will end up on the stack (with alignment 8).
  2838. if (Align == 8 && Size <= 64)
  2839. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  2840. Size));
  2841. }
  2842. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
  2843. }
  2844. /// The ABI specifies that a value should be passed in a full vector XMM/YMM
  2845. /// register. Pick an LLVM IR type that will be passed as a vector register.
  2846. llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
  2847. // Wrapper structs/arrays that only contain vectors are passed just like
  2848. // vectors; strip them off if present.
  2849. if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
  2850. Ty = QualType(InnerTy, 0);
  2851. llvm::Type *IRType = CGT.ConvertType(Ty);
  2852. if (isa<llvm::VectorType>(IRType)) {
  2853. // Don't pass vXi128 vectors in their native type, the backend can't
  2854. // legalize them.
  2855. if (passInt128VectorsInMem() &&
  2856. cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy(128)) {
  2857. // Use a vXi64 vector.
  2858. uint64_t Size = getContext().getTypeSize(Ty);
  2859. return llvm::FixedVectorType::get(llvm::Type::getInt64Ty(getVMContext()),
  2860. Size / 64);
  2861. }
  2862. return IRType;
  2863. }
  2864. if (IRType->getTypeID() == llvm::Type::FP128TyID)
  2865. return IRType;
  2866. // We couldn't find the preferred IR vector type for 'Ty'.
  2867. uint64_t Size = getContext().getTypeSize(Ty);
  2868. assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
  2869. // Return a LLVM IR vector type based on the size of 'Ty'.
  2870. return llvm::FixedVectorType::get(llvm::Type::getDoubleTy(getVMContext()),
  2871. Size / 64);
  2872. }
  2873. /// BitsContainNoUserData - Return true if the specified [start,end) bit range
  2874. /// is known to either be off the end of the specified type or being in
  2875. /// alignment padding. The user type specified is known to be at most 128 bits
  2876. /// in size, and have passed through X86_64ABIInfo::classify with a successful
  2877. /// classification that put one of the two halves in the INTEGER class.
  2878. ///
  2879. /// It is conservatively correct to return false.
  2880. static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
  2881. unsigned EndBit, ASTContext &Context) {
  2882. // If the bytes being queried are off the end of the type, there is no user
  2883. // data hiding here. This handles analysis of builtins, vectors and other
  2884. // types that don't contain interesting padding.
  2885. unsigned TySize = (unsigned)Context.getTypeSize(Ty);
  2886. if (TySize <= StartBit)
  2887. return true;
  2888. if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
  2889. unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
  2890. unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
  2891. // Check each element to see if the element overlaps with the queried range.
  2892. for (unsigned i = 0; i != NumElts; ++i) {
  2893. // If the element is after the span we care about, then we're done..
  2894. unsigned EltOffset = i*EltSize;
  2895. if (EltOffset >= EndBit) break;
  2896. unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
  2897. if (!BitsContainNoUserData(AT->getElementType(), EltStart,
  2898. EndBit-EltOffset, Context))
  2899. return false;
  2900. }
  2901. // If it overlaps no elements, then it is safe to process as padding.
  2902. return true;
  2903. }
  2904. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  2905. const RecordDecl *RD = RT->getDecl();
  2906. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  2907. // If this is a C++ record, check the bases first.
  2908. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  2909. for (const auto &I : CXXRD->bases()) {
  2910. assert(!I.isVirtual() && !I.getType()->isDependentType() &&
  2911. "Unexpected base class!");
  2912. const auto *Base =
  2913. cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
  2914. // If the base is after the span we care about, ignore it.
  2915. unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
  2916. if (BaseOffset >= EndBit) continue;
  2917. unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
  2918. if (!BitsContainNoUserData(I.getType(), BaseStart,
  2919. EndBit-BaseOffset, Context))
  2920. return false;
  2921. }
  2922. }
  2923. // Verify that no field has data that overlaps the region of interest. Yes
  2924. // this could be sped up a lot by being smarter about queried fields,
  2925. // however we're only looking at structs up to 16 bytes, so we don't care
  2926. // much.
  2927. unsigned idx = 0;
  2928. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  2929. i != e; ++i, ++idx) {
  2930. unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
  2931. // If we found a field after the region we care about, then we're done.
  2932. if (FieldOffset >= EndBit) break;
  2933. unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
  2934. if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
  2935. Context))
  2936. return false;
  2937. }
  2938. // If nothing in this record overlapped the area of interest, then we're
  2939. // clean.
  2940. return true;
  2941. }
  2942. return false;
  2943. }
  2944. /// getFPTypeAtOffset - Return a floating point type at the specified offset.
  2945. static llvm::Type *getFPTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
  2946. const llvm::DataLayout &TD) {
  2947. if (IROffset == 0 && IRType->isFloatingPointTy())
  2948. return IRType;
  2949. // If this is a struct, recurse into the field at the specified offset.
  2950. if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
  2951. if (!STy->getNumContainedTypes())
  2952. return nullptr;
  2953. const llvm::StructLayout *SL = TD.getStructLayout(STy);
  2954. unsigned Elt = SL->getElementContainingOffset(IROffset);
  2955. IROffset -= SL->getElementOffset(Elt);
  2956. return getFPTypeAtOffset(STy->getElementType(Elt), IROffset, TD);
  2957. }
  2958. // If this is an array, recurse into the field at the specified offset.
  2959. if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
  2960. llvm::Type *EltTy = ATy->getElementType();
  2961. unsigned EltSize = TD.getTypeAllocSize(EltTy);
  2962. IROffset -= IROffset / EltSize * EltSize;
  2963. return getFPTypeAtOffset(EltTy, IROffset, TD);
  2964. }
  2965. return nullptr;
  2966. }
  2967. /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
  2968. /// low 8 bytes of an XMM register, corresponding to the SSE class.
  2969. llvm::Type *X86_64ABIInfo::
  2970. GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
  2971. QualType SourceTy, unsigned SourceOffset) const {
  2972. const llvm::DataLayout &TD = getDataLayout();
  2973. unsigned SourceSize =
  2974. (unsigned)getContext().getTypeSize(SourceTy) / 8 - SourceOffset;
  2975. llvm::Type *T0 = getFPTypeAtOffset(IRType, IROffset, TD);
  2976. if (!T0 || T0->isDoubleTy())
  2977. return llvm::Type::getDoubleTy(getVMContext());
  2978. // Get the adjacent FP type.
  2979. llvm::Type *T1 = nullptr;
  2980. unsigned T0Size = TD.getTypeAllocSize(T0);
  2981. if (SourceSize > T0Size)
  2982. T1 = getFPTypeAtOffset(IRType, IROffset + T0Size, TD);
  2983. if (T1 == nullptr) {
  2984. // Check if IRType is a half/bfloat + float. float type will be in IROffset+4 due
  2985. // to its alignment.
  2986. if (T0->is16bitFPTy() && SourceSize > 4)
  2987. T1 = getFPTypeAtOffset(IRType, IROffset + 4, TD);
  2988. // If we can't get a second FP type, return a simple half or float.
  2989. // avx512fp16-abi.c:pr51813_2 shows it works to return float for
  2990. // {float, i8} too.
  2991. if (T1 == nullptr)
  2992. return T0;
  2993. }
  2994. if (T0->isFloatTy() && T1->isFloatTy())
  2995. return llvm::FixedVectorType::get(T0, 2);
  2996. if (T0->is16bitFPTy() && T1->is16bitFPTy()) {
  2997. llvm::Type *T2 = nullptr;
  2998. if (SourceSize > 4)
  2999. T2 = getFPTypeAtOffset(IRType, IROffset + 4, TD);
  3000. if (T2 == nullptr)
  3001. return llvm::FixedVectorType::get(T0, 2);
  3002. return llvm::FixedVectorType::get(T0, 4);
  3003. }
  3004. if (T0->is16bitFPTy() || T1->is16bitFPTy())
  3005. return llvm::FixedVectorType::get(llvm::Type::getHalfTy(getVMContext()), 4);
  3006. return llvm::Type::getDoubleTy(getVMContext());
  3007. }
  3008. /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
  3009. /// an 8-byte GPR. This means that we either have a scalar or we are talking
  3010. /// about the high or low part of an up-to-16-byte struct. This routine picks
  3011. /// the best LLVM IR type to represent this, which may be i64 or may be anything
  3012. /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
  3013. /// etc).
  3014. ///
  3015. /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
  3016. /// the source type. IROffset is an offset in bytes into the LLVM IR type that
  3017. /// the 8-byte value references. PrefType may be null.
  3018. ///
  3019. /// SourceTy is the source-level type for the entire argument. SourceOffset is
  3020. /// an offset into this that we're processing (which is always either 0 or 8).
  3021. ///
  3022. llvm::Type *X86_64ABIInfo::
  3023. GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
  3024. QualType SourceTy, unsigned SourceOffset) const {
  3025. // If we're dealing with an un-offset LLVM IR type, then it means that we're
  3026. // returning an 8-byte unit starting with it. See if we can safely use it.
  3027. if (IROffset == 0) {
  3028. // Pointers and int64's always fill the 8-byte unit.
  3029. if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
  3030. IRType->isIntegerTy(64))
  3031. return IRType;
  3032. // If we have a 1/2/4-byte integer, we can use it only if the rest of the
  3033. // goodness in the source type is just tail padding. This is allowed to
  3034. // kick in for struct {double,int} on the int, but not on
  3035. // struct{double,int,int} because we wouldn't return the second int. We
  3036. // have to do this analysis on the source type because we can't depend on
  3037. // unions being lowered a specific way etc.
  3038. if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
  3039. IRType->isIntegerTy(32) ||
  3040. (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
  3041. unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
  3042. cast<llvm::IntegerType>(IRType)->getBitWidth();
  3043. if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
  3044. SourceOffset*8+64, getContext()))
  3045. return IRType;
  3046. }
  3047. }
  3048. if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
  3049. // If this is a struct, recurse into the field at the specified offset.
  3050. const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
  3051. if (IROffset < SL->getSizeInBytes()) {
  3052. unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
  3053. IROffset -= SL->getElementOffset(FieldIdx);
  3054. return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
  3055. SourceTy, SourceOffset);
  3056. }
  3057. }
  3058. if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
  3059. llvm::Type *EltTy = ATy->getElementType();
  3060. unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
  3061. unsigned EltOffset = IROffset/EltSize*EltSize;
  3062. return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
  3063. SourceOffset);
  3064. }
  3065. // Okay, we don't have any better idea of what to pass, so we pass this in an
  3066. // integer register that isn't too big to fit the rest of the struct.
  3067. unsigned TySizeInBytes =
  3068. (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
  3069. assert(TySizeInBytes != SourceOffset && "Empty field?");
  3070. // It is always safe to classify this as an integer type up to i64 that
  3071. // isn't larger than the structure.
  3072. return llvm::IntegerType::get(getVMContext(),
  3073. std::min(TySizeInBytes-SourceOffset, 8U)*8);
  3074. }
  3075. /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
  3076. /// be used as elements of a two register pair to pass or return, return a
  3077. /// first class aggregate to represent them. For example, if the low part of
  3078. /// a by-value argument should be passed as i32* and the high part as float,
  3079. /// return {i32*, float}.
  3080. static llvm::Type *
  3081. GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
  3082. const llvm::DataLayout &TD) {
  3083. // In order to correctly satisfy the ABI, we need to the high part to start
  3084. // at offset 8. If the high and low parts we inferred are both 4-byte types
  3085. // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
  3086. // the second element at offset 8. Check for this:
  3087. unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
  3088. llvm::Align HiAlign = TD.getABITypeAlign(Hi);
  3089. unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
  3090. assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
  3091. // To handle this, we have to increase the size of the low part so that the
  3092. // second element will start at an 8 byte offset. We can't increase the size
  3093. // of the second element because it might make us access off the end of the
  3094. // struct.
  3095. if (HiStart != 8) {
  3096. // There are usually two sorts of types the ABI generation code can produce
  3097. // for the low part of a pair that aren't 8 bytes in size: half, float or
  3098. // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and
  3099. // NaCl).
  3100. // Promote these to a larger type.
  3101. if (Lo->isHalfTy() || Lo->isFloatTy())
  3102. Lo = llvm::Type::getDoubleTy(Lo->getContext());
  3103. else {
  3104. assert((Lo->isIntegerTy() || Lo->isPointerTy())
  3105. && "Invalid/unknown lo type");
  3106. Lo = llvm::Type::getInt64Ty(Lo->getContext());
  3107. }
  3108. }
  3109. llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
  3110. // Verify that the second element is at an 8-byte offset.
  3111. assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
  3112. "Invalid x86-64 argument pair!");
  3113. return Result;
  3114. }
  3115. ABIArgInfo X86_64ABIInfo::
  3116. classifyReturnType(QualType RetTy) const {
  3117. // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
  3118. // classification algorithm.
  3119. X86_64ABIInfo::Class Lo, Hi;
  3120. classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
  3121. // Check some invariants.
  3122. assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
  3123. assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
  3124. llvm::Type *ResType = nullptr;
  3125. switch (Lo) {
  3126. case NoClass:
  3127. if (Hi == NoClass)
  3128. return ABIArgInfo::getIgnore();
  3129. // If the low part is just padding, it takes no register, leave ResType
  3130. // null.
  3131. assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
  3132. "Unknown missing lo part");
  3133. break;
  3134. case SSEUp:
  3135. case X87Up:
  3136. llvm_unreachable("Invalid classification for lo word.");
  3137. // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
  3138. // hidden argument.
  3139. case Memory:
  3140. return getIndirectReturnResult(RetTy);
  3141. // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
  3142. // available register of the sequence %rax, %rdx is used.
  3143. case Integer:
  3144. ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
  3145. // If we have a sign or zero extended integer, make sure to return Extend
  3146. // so that the parameter gets the right LLVM IR attributes.
  3147. if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
  3148. // Treat an enum type as its underlying type.
  3149. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  3150. RetTy = EnumTy->getDecl()->getIntegerType();
  3151. if (RetTy->isIntegralOrEnumerationType() &&
  3152. isPromotableIntegerTypeForABI(RetTy))
  3153. return ABIArgInfo::getExtend(RetTy);
  3154. }
  3155. break;
  3156. // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
  3157. // available SSE register of the sequence %xmm0, %xmm1 is used.
  3158. case SSE:
  3159. ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
  3160. break;
  3161. // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
  3162. // returned on the X87 stack in %st0 as 80-bit x87 number.
  3163. case X87:
  3164. ResType = llvm::Type::getX86_FP80Ty(getVMContext());
  3165. break;
  3166. // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
  3167. // part of the value is returned in %st0 and the imaginary part in
  3168. // %st1.
  3169. case ComplexX87:
  3170. assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
  3171. ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
  3172. llvm::Type::getX86_FP80Ty(getVMContext()));
  3173. break;
  3174. }
  3175. llvm::Type *HighPart = nullptr;
  3176. switch (Hi) {
  3177. // Memory was handled previously and X87 should
  3178. // never occur as a hi class.
  3179. case Memory:
  3180. case X87:
  3181. llvm_unreachable("Invalid classification for hi word.");
  3182. case ComplexX87: // Previously handled.
  3183. case NoClass:
  3184. break;
  3185. case Integer:
  3186. HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
  3187. if (Lo == NoClass) // Return HighPart at offset 8 in memory.
  3188. return ABIArgInfo::getDirect(HighPart, 8);
  3189. break;
  3190. case SSE:
  3191. HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
  3192. if (Lo == NoClass) // Return HighPart at offset 8 in memory.
  3193. return ABIArgInfo::getDirect(HighPart, 8);
  3194. break;
  3195. // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
  3196. // is passed in the next available eightbyte chunk if the last used
  3197. // vector register.
  3198. //
  3199. // SSEUP should always be preceded by SSE, just widen.
  3200. case SSEUp:
  3201. assert(Lo == SSE && "Unexpected SSEUp classification.");
  3202. ResType = GetByteVectorType(RetTy);
  3203. break;
  3204. // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
  3205. // returned together with the previous X87 value in %st0.
  3206. case X87Up:
  3207. // If X87Up is preceded by X87, we don't need to do
  3208. // anything. However, in some cases with unions it may not be
  3209. // preceded by X87. In such situations we follow gcc and pass the
  3210. // extra bits in an SSE reg.
  3211. if (Lo != X87) {
  3212. HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
  3213. if (Lo == NoClass) // Return HighPart at offset 8 in memory.
  3214. return ABIArgInfo::getDirect(HighPart, 8);
  3215. }
  3216. break;
  3217. }
  3218. // If a high part was specified, merge it together with the low part. It is
  3219. // known to pass in the high eightbyte of the result. We do this by forming a
  3220. // first class struct aggregate with the high and low part: {low, high}
  3221. if (HighPart)
  3222. ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
  3223. return ABIArgInfo::getDirect(ResType);
  3224. }
  3225. ABIArgInfo
  3226. X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned freeIntRegs,
  3227. unsigned &neededInt, unsigned &neededSSE,
  3228. bool isNamedArg, bool IsRegCall) const {
  3229. Ty = useFirstFieldIfTransparentUnion(Ty);
  3230. X86_64ABIInfo::Class Lo, Hi;
  3231. classify(Ty, 0, Lo, Hi, isNamedArg, IsRegCall);
  3232. // Check some invariants.
  3233. // FIXME: Enforce these by construction.
  3234. assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
  3235. assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
  3236. neededInt = 0;
  3237. neededSSE = 0;
  3238. llvm::Type *ResType = nullptr;
  3239. switch (Lo) {
  3240. case NoClass:
  3241. if (Hi == NoClass)
  3242. return ABIArgInfo::getIgnore();
  3243. // If the low part is just padding, it takes no register, leave ResType
  3244. // null.
  3245. assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
  3246. "Unknown missing lo part");
  3247. break;
  3248. // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
  3249. // on the stack.
  3250. case Memory:
  3251. // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
  3252. // COMPLEX_X87, it is passed in memory.
  3253. case X87:
  3254. case ComplexX87:
  3255. if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
  3256. ++neededInt;
  3257. return getIndirectResult(Ty, freeIntRegs);
  3258. case SSEUp:
  3259. case X87Up:
  3260. llvm_unreachable("Invalid classification for lo word.");
  3261. // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
  3262. // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
  3263. // and %r9 is used.
  3264. case Integer:
  3265. ++neededInt;
  3266. // Pick an 8-byte type based on the preferred type.
  3267. ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
  3268. // If we have a sign or zero extended integer, make sure to return Extend
  3269. // so that the parameter gets the right LLVM IR attributes.
  3270. if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
  3271. // Treat an enum type as its underlying type.
  3272. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  3273. Ty = EnumTy->getDecl()->getIntegerType();
  3274. if (Ty->isIntegralOrEnumerationType() &&
  3275. isPromotableIntegerTypeForABI(Ty))
  3276. return ABIArgInfo::getExtend(Ty);
  3277. }
  3278. break;
  3279. // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
  3280. // available SSE register is used, the registers are taken in the
  3281. // order from %xmm0 to %xmm7.
  3282. case SSE: {
  3283. llvm::Type *IRType = CGT.ConvertType(Ty);
  3284. ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
  3285. ++neededSSE;
  3286. break;
  3287. }
  3288. }
  3289. llvm::Type *HighPart = nullptr;
  3290. switch (Hi) {
  3291. // Memory was handled previously, ComplexX87 and X87 should
  3292. // never occur as hi classes, and X87Up must be preceded by X87,
  3293. // which is passed in memory.
  3294. case Memory:
  3295. case X87:
  3296. case ComplexX87:
  3297. llvm_unreachable("Invalid classification for hi word.");
  3298. case NoClass: break;
  3299. case Integer:
  3300. ++neededInt;
  3301. // Pick an 8-byte type based on the preferred type.
  3302. HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
  3303. if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
  3304. return ABIArgInfo::getDirect(HighPart, 8);
  3305. break;
  3306. // X87Up generally doesn't occur here (long double is passed in
  3307. // memory), except in situations involving unions.
  3308. case X87Up:
  3309. case SSE:
  3310. HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
  3311. if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
  3312. return ABIArgInfo::getDirect(HighPart, 8);
  3313. ++neededSSE;
  3314. break;
  3315. // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
  3316. // eightbyte is passed in the upper half of the last used SSE
  3317. // register. This only happens when 128-bit vectors are passed.
  3318. case SSEUp:
  3319. assert(Lo == SSE && "Unexpected SSEUp classification");
  3320. ResType = GetByteVectorType(Ty);
  3321. break;
  3322. }
  3323. // If a high part was specified, merge it together with the low part. It is
  3324. // known to pass in the high eightbyte of the result. We do this by forming a
  3325. // first class struct aggregate with the high and low part: {low, high}
  3326. if (HighPart)
  3327. ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
  3328. return ABIArgInfo::getDirect(ResType);
  3329. }
  3330. ABIArgInfo
  3331. X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
  3332. unsigned &NeededSSE,
  3333. unsigned &MaxVectorWidth) const {
  3334. auto RT = Ty->getAs<RecordType>();
  3335. assert(RT && "classifyRegCallStructType only valid with struct types");
  3336. if (RT->getDecl()->hasFlexibleArrayMember())
  3337. return getIndirectReturnResult(Ty);
  3338. // Sum up bases
  3339. if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
  3340. if (CXXRD->isDynamicClass()) {
  3341. NeededInt = NeededSSE = 0;
  3342. return getIndirectReturnResult(Ty);
  3343. }
  3344. for (const auto &I : CXXRD->bases())
  3345. if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE,
  3346. MaxVectorWidth)
  3347. .isIndirect()) {
  3348. NeededInt = NeededSSE = 0;
  3349. return getIndirectReturnResult(Ty);
  3350. }
  3351. }
  3352. // Sum up members
  3353. for (const auto *FD : RT->getDecl()->fields()) {
  3354. QualType MTy = FD->getType();
  3355. if (MTy->isRecordType() && !MTy->isUnionType()) {
  3356. if (classifyRegCallStructTypeImpl(MTy, NeededInt, NeededSSE,
  3357. MaxVectorWidth)
  3358. .isIndirect()) {
  3359. NeededInt = NeededSSE = 0;
  3360. return getIndirectReturnResult(Ty);
  3361. }
  3362. } else {
  3363. unsigned LocalNeededInt, LocalNeededSSE;
  3364. if (classifyArgumentType(MTy, UINT_MAX, LocalNeededInt, LocalNeededSSE,
  3365. true, true)
  3366. .isIndirect()) {
  3367. NeededInt = NeededSSE = 0;
  3368. return getIndirectReturnResult(Ty);
  3369. }
  3370. if (const auto *AT = getContext().getAsConstantArrayType(MTy))
  3371. MTy = AT->getElementType();
  3372. if (const auto *VT = MTy->getAs<VectorType>())
  3373. if (getContext().getTypeSize(VT) > MaxVectorWidth)
  3374. MaxVectorWidth = getContext().getTypeSize(VT);
  3375. NeededInt += LocalNeededInt;
  3376. NeededSSE += LocalNeededSSE;
  3377. }
  3378. }
  3379. return ABIArgInfo::getDirect();
  3380. }
  3381. ABIArgInfo
  3382. X86_64ABIInfo::classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
  3383. unsigned &NeededSSE,
  3384. unsigned &MaxVectorWidth) const {
  3385. NeededInt = 0;
  3386. NeededSSE = 0;
  3387. MaxVectorWidth = 0;
  3388. return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE,
  3389. MaxVectorWidth);
  3390. }
  3391. void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  3392. const unsigned CallingConv = FI.getCallingConvention();
  3393. // It is possible to force Win64 calling convention on any x86_64 target by
  3394. // using __attribute__((ms_abi)). In such case to correctly emit Win64
  3395. // compatible code delegate this call to WinX86_64ABIInfo::computeInfo.
  3396. if (CallingConv == llvm::CallingConv::Win64) {
  3397. WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel);
  3398. Win64ABIInfo.computeInfo(FI);
  3399. return;
  3400. }
  3401. bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall;
  3402. // Keep track of the number of assigned registers.
  3403. unsigned FreeIntRegs = IsRegCall ? 11 : 6;
  3404. unsigned FreeSSERegs = IsRegCall ? 16 : 8;
  3405. unsigned NeededInt = 0, NeededSSE = 0, MaxVectorWidth = 0;
  3406. if (!::classifyReturnType(getCXXABI(), FI, *this)) {
  3407. if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
  3408. !FI.getReturnType()->getTypePtr()->isUnionType()) {
  3409. FI.getReturnInfo() = classifyRegCallStructType(
  3410. FI.getReturnType(), NeededInt, NeededSSE, MaxVectorWidth);
  3411. if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
  3412. FreeIntRegs -= NeededInt;
  3413. FreeSSERegs -= NeededSSE;
  3414. } else {
  3415. FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
  3416. }
  3417. } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>() &&
  3418. getContext().getCanonicalType(FI.getReturnType()
  3419. ->getAs<ComplexType>()
  3420. ->getElementType()) ==
  3421. getContext().LongDoubleTy)
  3422. // Complex Long Double Type is passed in Memory when Regcall
  3423. // calling convention is used.
  3424. FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
  3425. else
  3426. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  3427. }
  3428. // If the return value is indirect, then the hidden argument is consuming one
  3429. // integer register.
  3430. if (FI.getReturnInfo().isIndirect())
  3431. --FreeIntRegs;
  3432. else if (NeededSSE && MaxVectorWidth > 0)
  3433. FI.setMaxVectorWidth(MaxVectorWidth);
  3434. // The chain argument effectively gives us another free register.
  3435. if (FI.isChainCall())
  3436. ++FreeIntRegs;
  3437. unsigned NumRequiredArgs = FI.getNumRequiredArgs();
  3438. // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
  3439. // get assigned (in left-to-right order) for passing as follows...
  3440. unsigned ArgNo = 0;
  3441. for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
  3442. it != ie; ++it, ++ArgNo) {
  3443. bool IsNamedArg = ArgNo < NumRequiredArgs;
  3444. if (IsRegCall && it->type->isStructureOrClassType())
  3445. it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE,
  3446. MaxVectorWidth);
  3447. else
  3448. it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
  3449. NeededSSE, IsNamedArg);
  3450. // AMD64-ABI 3.2.3p3: If there are no registers available for any
  3451. // eightbyte of an argument, the whole argument is passed on the
  3452. // stack. If registers have already been assigned for some
  3453. // eightbytes of such an argument, the assignments get reverted.
  3454. if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
  3455. FreeIntRegs -= NeededInt;
  3456. FreeSSERegs -= NeededSSE;
  3457. if (MaxVectorWidth > FI.getMaxVectorWidth())
  3458. FI.setMaxVectorWidth(MaxVectorWidth);
  3459. } else {
  3460. it->info = getIndirectResult(it->type, FreeIntRegs);
  3461. }
  3462. }
  3463. }
  3464. static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
  3465. Address VAListAddr, QualType Ty) {
  3466. Address overflow_arg_area_p =
  3467. CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
  3468. llvm::Value *overflow_arg_area =
  3469. CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
  3470. // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
  3471. // byte boundary if alignment needed by type exceeds 8 byte boundary.
  3472. // It isn't stated explicitly in the standard, but in practice we use
  3473. // alignment greater than 16 where necessary.
  3474. CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
  3475. if (Align > CharUnits::fromQuantity(8)) {
  3476. overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
  3477. Align);
  3478. }
  3479. // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
  3480. llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
  3481. llvm::Value *Res =
  3482. CGF.Builder.CreateBitCast(overflow_arg_area,
  3483. llvm::PointerType::getUnqual(LTy));
  3484. // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
  3485. // l->overflow_arg_area + sizeof(type).
  3486. // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
  3487. // an 8 byte boundary.
  3488. uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
  3489. llvm::Value *Offset =
  3490. llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
  3491. overflow_arg_area = CGF.Builder.CreateGEP(CGF.Int8Ty, overflow_arg_area,
  3492. Offset, "overflow_arg_area.next");
  3493. CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
  3494. // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
  3495. return Address(Res, LTy, Align);
  3496. }
  3497. Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3498. QualType Ty) const {
  3499. // Assume that va_list type is correct; should be pointer to LLVM type:
  3500. // struct {
  3501. // i32 gp_offset;
  3502. // i32 fp_offset;
  3503. // i8* overflow_arg_area;
  3504. // i8* reg_save_area;
  3505. // };
  3506. unsigned neededInt, neededSSE;
  3507. Ty = getContext().getCanonicalType(Ty);
  3508. ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
  3509. /*isNamedArg*/false);
  3510. // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
  3511. // in the registers. If not go to step 7.
  3512. if (!neededInt && !neededSSE)
  3513. return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
  3514. // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
  3515. // general purpose registers needed to pass type and num_fp to hold
  3516. // the number of floating point registers needed.
  3517. // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
  3518. // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
  3519. // l->fp_offset > 304 - num_fp * 16 go to step 7.
  3520. //
  3521. // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
  3522. // register save space).
  3523. llvm::Value *InRegs = nullptr;
  3524. Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
  3525. llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
  3526. if (neededInt) {
  3527. gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
  3528. gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
  3529. InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
  3530. InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
  3531. }
  3532. if (neededSSE) {
  3533. fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
  3534. fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
  3535. llvm::Value *FitsInFP =
  3536. llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
  3537. FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
  3538. InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
  3539. }
  3540. llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
  3541. llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
  3542. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
  3543. CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
  3544. // Emit code to load the value if it was passed in registers.
  3545. CGF.EmitBlock(InRegBlock);
  3546. // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
  3547. // an offset of l->gp_offset and/or l->fp_offset. This may require
  3548. // copying to a temporary location in case the parameter is passed
  3549. // in different register classes or requires an alignment greater
  3550. // than 8 for general purpose registers and 16 for XMM registers.
  3551. //
  3552. // FIXME: This really results in shameful code when we end up needing to
  3553. // collect arguments from different places; often what should result in a
  3554. // simple assembling of a structure from scattered addresses has many more
  3555. // loads than necessary. Can we clean this up?
  3556. llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
  3557. llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
  3558. CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area");
  3559. Address RegAddr = Address::invalid();
  3560. if (neededInt && neededSSE) {
  3561. // FIXME: Cleanup.
  3562. assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
  3563. llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
  3564. Address Tmp = CGF.CreateMemTemp(Ty);
  3565. Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
  3566. assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
  3567. llvm::Type *TyLo = ST->getElementType(0);
  3568. llvm::Type *TyHi = ST->getElementType(1);
  3569. assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
  3570. "Unexpected ABI info for mixed regs");
  3571. llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
  3572. llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
  3573. llvm::Value *GPAddr =
  3574. CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, gp_offset);
  3575. llvm::Value *FPAddr =
  3576. CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, fp_offset);
  3577. llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
  3578. llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
  3579. // Copy the first element.
  3580. // FIXME: Our choice of alignment here and below is probably pessimistic.
  3581. llvm::Value *V = CGF.Builder.CreateAlignedLoad(
  3582. TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
  3583. CharUnits::fromQuantity(getDataLayout().getABITypeAlign(TyLo)));
  3584. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
  3585. // Copy the second element.
  3586. V = CGF.Builder.CreateAlignedLoad(
  3587. TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
  3588. CharUnits::fromQuantity(getDataLayout().getABITypeAlign(TyHi)));
  3589. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
  3590. RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
  3591. } else if (neededInt) {
  3592. RegAddr = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, gp_offset),
  3593. CGF.Int8Ty, CharUnits::fromQuantity(8));
  3594. RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
  3595. // Copy to a temporary if necessary to ensure the appropriate alignment.
  3596. auto TInfo = getContext().getTypeInfoInChars(Ty);
  3597. uint64_t TySize = TInfo.Width.getQuantity();
  3598. CharUnits TyAlign = TInfo.Align;
  3599. // Copy into a temporary if the type is more aligned than the
  3600. // register save area.
  3601. if (TyAlign.getQuantity() > 8) {
  3602. Address Tmp = CGF.CreateMemTemp(Ty);
  3603. CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
  3604. RegAddr = Tmp;
  3605. }
  3606. } else if (neededSSE == 1) {
  3607. RegAddr = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, fp_offset),
  3608. CGF.Int8Ty, CharUnits::fromQuantity(16));
  3609. RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
  3610. } else {
  3611. assert(neededSSE == 2 && "Invalid number of needed registers!");
  3612. // SSE registers are spaced 16 bytes apart in the register save
  3613. // area, we need to collect the two eightbytes together.
  3614. // The ABI isn't explicit about this, but it seems reasonable
  3615. // to assume that the slots are 16-byte aligned, since the stack is
  3616. // naturally 16-byte aligned and the prologue is expected to store
  3617. // all the SSE registers to the RSA.
  3618. Address RegAddrLo = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea,
  3619. fp_offset),
  3620. CGF.Int8Ty, CharUnits::fromQuantity(16));
  3621. Address RegAddrHi =
  3622. CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
  3623. CharUnits::fromQuantity(16));
  3624. llvm::Type *ST = AI.canHaveCoerceToType()
  3625. ? AI.getCoerceToType()
  3626. : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy);
  3627. llvm::Value *V;
  3628. Address Tmp = CGF.CreateMemTemp(Ty);
  3629. Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
  3630. V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
  3631. RegAddrLo, ST->getStructElementType(0)));
  3632. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
  3633. V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
  3634. RegAddrHi, ST->getStructElementType(1)));
  3635. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
  3636. RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
  3637. }
  3638. // AMD64-ABI 3.5.7p5: Step 5. Set:
  3639. // l->gp_offset = l->gp_offset + num_gp * 8
  3640. // l->fp_offset = l->fp_offset + num_fp * 16.
  3641. if (neededInt) {
  3642. llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
  3643. CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
  3644. gp_offset_p);
  3645. }
  3646. if (neededSSE) {
  3647. llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
  3648. CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
  3649. fp_offset_p);
  3650. }
  3651. CGF.EmitBranch(ContBlock);
  3652. // Emit code to load the value if it was passed in memory.
  3653. CGF.EmitBlock(InMemBlock);
  3654. Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
  3655. // Return the appropriate result.
  3656. CGF.EmitBlock(ContBlock);
  3657. Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
  3658. "vaarg.addr");
  3659. return ResAddr;
  3660. }
  3661. Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3662. QualType Ty) const {
  3663. // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
  3664. // not 1, 2, 4, or 8 bytes, must be passed by reference."
  3665. uint64_t Width = getContext().getTypeSize(Ty);
  3666. bool IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
  3667. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
  3668. CGF.getContext().getTypeInfoInChars(Ty),
  3669. CharUnits::fromQuantity(8),
  3670. /*allowHigherAlign*/ false);
  3671. }
  3672. ABIArgInfo WinX86_64ABIInfo::reclassifyHvaArgForVectorCall(
  3673. QualType Ty, unsigned &FreeSSERegs, const ABIArgInfo &current) const {
  3674. const Type *Base = nullptr;
  3675. uint64_t NumElts = 0;
  3676. if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
  3677. isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
  3678. FreeSSERegs -= NumElts;
  3679. return getDirectX86Hva();
  3680. }
  3681. return current;
  3682. }
  3683. ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
  3684. bool IsReturnType, bool IsVectorCall,
  3685. bool IsRegCall) const {
  3686. if (Ty->isVoidType())
  3687. return ABIArgInfo::getIgnore();
  3688. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  3689. Ty = EnumTy->getDecl()->getIntegerType();
  3690. TypeInfo Info = getContext().getTypeInfo(Ty);
  3691. uint64_t Width = Info.Width;
  3692. CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
  3693. const RecordType *RT = Ty->getAs<RecordType>();
  3694. if (RT) {
  3695. if (!IsReturnType) {
  3696. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
  3697. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  3698. }
  3699. if (RT->getDecl()->hasFlexibleArrayMember())
  3700. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  3701. }
  3702. const Type *Base = nullptr;
  3703. uint64_t NumElts = 0;
  3704. // vectorcall adds the concept of a homogenous vector aggregate, similar to
  3705. // other targets.
  3706. if ((IsVectorCall || IsRegCall) &&
  3707. isHomogeneousAggregate(Ty, Base, NumElts)) {
  3708. if (IsRegCall) {
  3709. if (FreeSSERegs >= NumElts) {
  3710. FreeSSERegs -= NumElts;
  3711. if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
  3712. return ABIArgInfo::getDirect();
  3713. return ABIArgInfo::getExpand();
  3714. }
  3715. return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
  3716. } else if (IsVectorCall) {
  3717. if (FreeSSERegs >= NumElts &&
  3718. (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
  3719. FreeSSERegs -= NumElts;
  3720. return ABIArgInfo::getDirect();
  3721. } else if (IsReturnType) {
  3722. return ABIArgInfo::getExpand();
  3723. } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
  3724. // HVAs are delayed and reclassified in the 2nd step.
  3725. return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
  3726. }
  3727. }
  3728. }
  3729. if (Ty->isMemberPointerType()) {
  3730. // If the member pointer is represented by an LLVM int or ptr, pass it
  3731. // directly.
  3732. llvm::Type *LLTy = CGT.ConvertType(Ty);
  3733. if (LLTy->isPointerTy() || LLTy->isIntegerTy())
  3734. return ABIArgInfo::getDirect();
  3735. }
  3736. if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
  3737. // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
  3738. // not 1, 2, 4, or 8 bytes, must be passed by reference."
  3739. if (Width > 64 || !llvm::isPowerOf2_64(Width))
  3740. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  3741. // Otherwise, coerce it to a small integer.
  3742. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
  3743. }
  3744. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  3745. switch (BT->getKind()) {
  3746. case BuiltinType::Bool:
  3747. // Bool type is always extended to the ABI, other builtin types are not
  3748. // extended.
  3749. return ABIArgInfo::getExtend(Ty);
  3750. case BuiltinType::LongDouble:
  3751. // Mingw64 GCC uses the old 80 bit extended precision floating point
  3752. // unit. It passes them indirectly through memory.
  3753. if (IsMingw64) {
  3754. const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
  3755. if (LDF == &llvm::APFloat::x87DoubleExtended())
  3756. return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
  3757. }
  3758. break;
  3759. case BuiltinType::Int128:
  3760. case BuiltinType::UInt128:
  3761. // If it's a parameter type, the normal ABI rule is that arguments larger
  3762. // than 8 bytes are passed indirectly. GCC follows it. We follow it too,
  3763. // even though it isn't particularly efficient.
  3764. if (!IsReturnType)
  3765. return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
  3766. // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that.
  3767. // Clang matches them for compatibility.
  3768. return ABIArgInfo::getDirect(llvm::FixedVectorType::get(
  3769. llvm::Type::getInt64Ty(getVMContext()), 2));
  3770. default:
  3771. break;
  3772. }
  3773. }
  3774. if (Ty->isBitIntType()) {
  3775. // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
  3776. // not 1, 2, 4, or 8 bytes, must be passed by reference."
  3777. // However, non-power-of-two bit-precise integers will be passed as 1, 2, 4,
  3778. // or 8 bytes anyway as long is it fits in them, so we don't have to check
  3779. // the power of 2.
  3780. if (Width <= 64)
  3781. return ABIArgInfo::getDirect();
  3782. return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
  3783. }
  3784. return ABIArgInfo::getDirect();
  3785. }
  3786. void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  3787. const unsigned CC = FI.getCallingConvention();
  3788. bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall;
  3789. bool IsRegCall = CC == llvm::CallingConv::X86_RegCall;
  3790. // If __attribute__((sysv_abi)) is in use, use the SysV argument
  3791. // classification rules.
  3792. if (CC == llvm::CallingConv::X86_64_SysV) {
  3793. X86_64ABIInfo SysVABIInfo(CGT, AVXLevel);
  3794. SysVABIInfo.computeInfo(FI);
  3795. return;
  3796. }
  3797. unsigned FreeSSERegs = 0;
  3798. if (IsVectorCall) {
  3799. // We can use up to 4 SSE return registers with vectorcall.
  3800. FreeSSERegs = 4;
  3801. } else if (IsRegCall) {
  3802. // RegCall gives us 16 SSE registers.
  3803. FreeSSERegs = 16;
  3804. }
  3805. if (!getCXXABI().classifyReturnType(FI))
  3806. FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
  3807. IsVectorCall, IsRegCall);
  3808. if (IsVectorCall) {
  3809. // We can use up to 6 SSE register parameters with vectorcall.
  3810. FreeSSERegs = 6;
  3811. } else if (IsRegCall) {
  3812. // RegCall gives us 16 SSE registers, we can reuse the return registers.
  3813. FreeSSERegs = 16;
  3814. }
  3815. unsigned ArgNum = 0;
  3816. unsigned ZeroSSERegs = 0;
  3817. for (auto &I : FI.arguments()) {
  3818. // Vectorcall in x64 only permits the first 6 arguments to be passed as
  3819. // XMM/YMM registers. After the sixth argument, pretend no vector
  3820. // registers are left.
  3821. unsigned *MaybeFreeSSERegs =
  3822. (IsVectorCall && ArgNum >= 6) ? &ZeroSSERegs : &FreeSSERegs;
  3823. I.info =
  3824. classify(I.type, *MaybeFreeSSERegs, false, IsVectorCall, IsRegCall);
  3825. ++ArgNum;
  3826. }
  3827. if (IsVectorCall) {
  3828. // For vectorcall, assign aggregate HVAs to any free vector registers in a
  3829. // second pass.
  3830. for (auto &I : FI.arguments())
  3831. I.info = reclassifyHvaArgForVectorCall(I.type, FreeSSERegs, I.info);
  3832. }
  3833. }
  3834. Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3835. QualType Ty) const {
  3836. // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
  3837. // not 1, 2, 4, or 8 bytes, must be passed by reference."
  3838. uint64_t Width = getContext().getTypeSize(Ty);
  3839. bool IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
  3840. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
  3841. CGF.getContext().getTypeInfoInChars(Ty),
  3842. CharUnits::fromQuantity(8),
  3843. /*allowHigherAlign*/ false);
  3844. }
  3845. static bool PPC_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  3846. llvm::Value *Address, bool Is64Bit,
  3847. bool IsAIX) {
  3848. // This is calculated from the LLVM and GCC tables and verified
  3849. // against gcc output. AFAIK all PPC ABIs use the same encoding.
  3850. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  3851. llvm::IntegerType *i8 = CGF.Int8Ty;
  3852. llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
  3853. llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
  3854. llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
  3855. // 0-31: r0-31, the 4-byte or 8-byte general-purpose registers
  3856. AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 0, 31);
  3857. // 32-63: fp0-31, the 8-byte floating-point registers
  3858. AssignToArrayRange(Builder, Address, Eight8, 32, 63);
  3859. // 64-67 are various 4-byte or 8-byte special-purpose registers:
  3860. // 64: mq
  3861. // 65: lr
  3862. // 66: ctr
  3863. // 67: ap
  3864. AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 64, 67);
  3865. // 68-76 are various 4-byte special-purpose registers:
  3866. // 68-75 cr0-7
  3867. // 76: xer
  3868. AssignToArrayRange(Builder, Address, Four8, 68, 76);
  3869. // 77-108: v0-31, the 16-byte vector registers
  3870. AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
  3871. // 109: vrsave
  3872. // 110: vscr
  3873. AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 109, 110);
  3874. // AIX does not utilize the rest of the registers.
  3875. if (IsAIX)
  3876. return false;
  3877. // 111: spe_acc
  3878. // 112: spefscr
  3879. // 113: sfp
  3880. AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 111, 113);
  3881. if (!Is64Bit)
  3882. return false;
  3883. // TODO: Need to verify if these registers are used on 64 bit AIX with Power8
  3884. // or above CPU.
  3885. // 64-bit only registers:
  3886. // 114: tfhar
  3887. // 115: tfiar
  3888. // 116: texasr
  3889. AssignToArrayRange(Builder, Address, Eight8, 114, 116);
  3890. return false;
  3891. }
  3892. // AIX
  3893. namespace {
  3894. /// AIXABIInfo - The AIX XCOFF ABI information.
  3895. class AIXABIInfo : public ABIInfo {
  3896. const bool Is64Bit;
  3897. const unsigned PtrByteSize;
  3898. CharUnits getParamTypeAlignment(QualType Ty) const;
  3899. public:
  3900. AIXABIInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit)
  3901. : ABIInfo(CGT), Is64Bit(Is64Bit), PtrByteSize(Is64Bit ? 8 : 4) {}
  3902. bool isPromotableTypeForABI(QualType Ty) const;
  3903. ABIArgInfo classifyReturnType(QualType RetTy) const;
  3904. ABIArgInfo classifyArgumentType(QualType Ty) const;
  3905. void computeInfo(CGFunctionInfo &FI) const override {
  3906. if (!getCXXABI().classifyReturnType(FI))
  3907. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  3908. for (auto &I : FI.arguments())
  3909. I.info = classifyArgumentType(I.type);
  3910. }
  3911. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3912. QualType Ty) const override;
  3913. };
  3914. class AIXTargetCodeGenInfo : public TargetCodeGenInfo {
  3915. const bool Is64Bit;
  3916. public:
  3917. AIXTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit)
  3918. : TargetCodeGenInfo(std::make_unique<AIXABIInfo>(CGT, Is64Bit)),
  3919. Is64Bit(Is64Bit) {}
  3920. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  3921. return 1; // r1 is the dedicated stack pointer
  3922. }
  3923. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  3924. llvm::Value *Address) const override;
  3925. };
  3926. } // namespace
  3927. // Return true if the ABI requires Ty to be passed sign- or zero-
  3928. // extended to 32/64 bits.
  3929. bool AIXABIInfo::isPromotableTypeForABI(QualType Ty) const {
  3930. // Treat an enum type as its underlying type.
  3931. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  3932. Ty = EnumTy->getDecl()->getIntegerType();
  3933. // Promotable integer types are required to be promoted by the ABI.
  3934. if (getContext().isPromotableIntegerType(Ty))
  3935. return true;
  3936. if (!Is64Bit)
  3937. return false;
  3938. // For 64 bit mode, in addition to the usual promotable integer types, we also
  3939. // need to extend all 32-bit types, since the ABI requires promotion to 64
  3940. // bits.
  3941. if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
  3942. switch (BT->getKind()) {
  3943. case BuiltinType::Int:
  3944. case BuiltinType::UInt:
  3945. return true;
  3946. default:
  3947. break;
  3948. }
  3949. return false;
  3950. }
  3951. ABIArgInfo AIXABIInfo::classifyReturnType(QualType RetTy) const {
  3952. if (RetTy->isAnyComplexType())
  3953. return ABIArgInfo::getDirect();
  3954. if (RetTy->isVectorType())
  3955. return ABIArgInfo::getDirect();
  3956. if (RetTy->isVoidType())
  3957. return ABIArgInfo::getIgnore();
  3958. if (isAggregateTypeForABI(RetTy))
  3959. return getNaturalAlignIndirect(RetTy);
  3960. return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
  3961. : ABIArgInfo::getDirect());
  3962. }
  3963. ABIArgInfo AIXABIInfo::classifyArgumentType(QualType Ty) const {
  3964. Ty = useFirstFieldIfTransparentUnion(Ty);
  3965. if (Ty->isAnyComplexType())
  3966. return ABIArgInfo::getDirect();
  3967. if (Ty->isVectorType())
  3968. return ABIArgInfo::getDirect();
  3969. if (isAggregateTypeForABI(Ty)) {
  3970. // Records with non-trivial destructors/copy-constructors should not be
  3971. // passed by value.
  3972. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  3973. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  3974. CharUnits CCAlign = getParamTypeAlignment(Ty);
  3975. CharUnits TyAlign = getContext().getTypeAlignInChars(Ty);
  3976. return ABIArgInfo::getIndirect(CCAlign, /*ByVal*/ true,
  3977. /*Realign*/ TyAlign > CCAlign);
  3978. }
  3979. return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
  3980. : ABIArgInfo::getDirect());
  3981. }
  3982. CharUnits AIXABIInfo::getParamTypeAlignment(QualType Ty) const {
  3983. // Complex types are passed just like their elements.
  3984. if (const ComplexType *CTy = Ty->getAs<ComplexType>())
  3985. Ty = CTy->getElementType();
  3986. if (Ty->isVectorType())
  3987. return CharUnits::fromQuantity(16);
  3988. // If the structure contains a vector type, the alignment is 16.
  3989. if (isRecordWithSIMDVectorType(getContext(), Ty))
  3990. return CharUnits::fromQuantity(16);
  3991. return CharUnits::fromQuantity(PtrByteSize);
  3992. }
  3993. Address AIXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  3994. QualType Ty) const {
  3995. auto TypeInfo = getContext().getTypeInfoInChars(Ty);
  3996. TypeInfo.Align = getParamTypeAlignment(Ty);
  3997. CharUnits SlotSize = CharUnits::fromQuantity(PtrByteSize);
  3998. // If we have a complex type and the base type is smaller than the register
  3999. // size, the ABI calls for the real and imaginary parts to be right-adjusted
  4000. // in separate words in 32bit mode or doublewords in 64bit mode. However,
  4001. // Clang expects us to produce a pointer to a structure with the two parts
  4002. // packed tightly. So generate loads of the real and imaginary parts relative
  4003. // to the va_list pointer, and store them to a temporary structure. We do the
  4004. // same as the PPC64ABI here.
  4005. if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
  4006. CharUnits EltSize = TypeInfo.Width / 2;
  4007. if (EltSize < SlotSize)
  4008. return complexTempStructure(CGF, VAListAddr, Ty, SlotSize, EltSize, CTy);
  4009. }
  4010. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, TypeInfo,
  4011. SlotSize, /*AllowHigher*/ true);
  4012. }
  4013. bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
  4014. CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const {
  4015. return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
  4016. }
  4017. // PowerPC-32
  4018. namespace {
  4019. /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
  4020. class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
  4021. bool IsSoftFloatABI;
  4022. bool IsRetSmallStructInRegABI;
  4023. CharUnits getParamTypeAlignment(QualType Ty) const;
  4024. public:
  4025. PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI,
  4026. bool RetSmallStructInRegABI)
  4027. : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI),
  4028. IsRetSmallStructInRegABI(RetSmallStructInRegABI) {}
  4029. ABIArgInfo classifyReturnType(QualType RetTy) const;
  4030. void computeInfo(CGFunctionInfo &FI) const override {
  4031. if (!getCXXABI().classifyReturnType(FI))
  4032. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  4033. for (auto &I : FI.arguments())
  4034. I.info = classifyArgumentType(I.type);
  4035. }
  4036. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4037. QualType Ty) const override;
  4038. };
  4039. class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
  4040. public:
  4041. PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI,
  4042. bool RetSmallStructInRegABI)
  4043. : TargetCodeGenInfo(std::make_unique<PPC32_SVR4_ABIInfo>(
  4044. CGT, SoftFloatABI, RetSmallStructInRegABI)) {}
  4045. static bool isStructReturnInRegABI(const llvm::Triple &Triple,
  4046. const CodeGenOptions &Opts);
  4047. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  4048. // This is recovered from gcc output.
  4049. return 1; // r1 is the dedicated stack pointer
  4050. }
  4051. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  4052. llvm::Value *Address) const override;
  4053. };
  4054. }
  4055. CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
  4056. // Complex types are passed just like their elements.
  4057. if (const ComplexType *CTy = Ty->getAs<ComplexType>())
  4058. Ty = CTy->getElementType();
  4059. if (Ty->isVectorType())
  4060. return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16
  4061. : 4);
  4062. // For single-element float/vector structs, we consider the whole type
  4063. // to have the same alignment requirements as its single element.
  4064. const Type *AlignTy = nullptr;
  4065. if (const Type *EltType = isSingleElementStruct(Ty, getContext())) {
  4066. const BuiltinType *BT = EltType->getAs<BuiltinType>();
  4067. if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
  4068. (BT && BT->isFloatingPoint()))
  4069. AlignTy = EltType;
  4070. }
  4071. if (AlignTy)
  4072. return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4);
  4073. return CharUnits::fromQuantity(4);
  4074. }
  4075. ABIArgInfo PPC32_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
  4076. uint64_t Size;
  4077. // -msvr4-struct-return puts small aggregates in GPR3 and GPR4.
  4078. if (isAggregateTypeForABI(RetTy) && IsRetSmallStructInRegABI &&
  4079. (Size = getContext().getTypeSize(RetTy)) <= 64) {
  4080. // System V ABI (1995), page 3-22, specified:
  4081. // > A structure or union whose size is less than or equal to 8 bytes
  4082. // > shall be returned in r3 and r4, as if it were first stored in the
  4083. // > 8-byte aligned memory area and then the low addressed word were
  4084. // > loaded into r3 and the high-addressed word into r4. Bits beyond
  4085. // > the last member of the structure or union are not defined.
  4086. //
  4087. // GCC for big-endian PPC32 inserts the pad before the first member,
  4088. // not "beyond the last member" of the struct. To stay compatible
  4089. // with GCC, we coerce the struct to an integer of the same size.
  4090. // LLVM will extend it and return i32 in r3, or i64 in r3:r4.
  4091. if (Size == 0)
  4092. return ABIArgInfo::getIgnore();
  4093. else {
  4094. llvm::Type *CoerceTy = llvm::Type::getIntNTy(getVMContext(), Size);
  4095. return ABIArgInfo::getDirect(CoerceTy);
  4096. }
  4097. }
  4098. return DefaultABIInfo::classifyReturnType(RetTy);
  4099. }
  4100. // TODO: this implementation is now likely redundant with
  4101. // DefaultABIInfo::EmitVAArg.
  4102. Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
  4103. QualType Ty) const {
  4104. if (getTarget().getTriple().isOSDarwin()) {
  4105. auto TI = getContext().getTypeInfoInChars(Ty);
  4106. TI.Align = getParamTypeAlignment(Ty);
  4107. CharUnits SlotSize = CharUnits::fromQuantity(4);
  4108. return emitVoidPtrVAArg(CGF, VAList, Ty,
  4109. classifyArgumentType(Ty).isIndirect(), TI, SlotSize,
  4110. /*AllowHigherAlign=*/true);
  4111. }
  4112. const unsigned OverflowLimit = 8;
  4113. if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
  4114. // TODO: Implement this. For now ignore.
  4115. (void)CTy;
  4116. return Address::invalid(); // FIXME?
  4117. }
  4118. // struct __va_list_tag {
  4119. // unsigned char gpr;
  4120. // unsigned char fpr;
  4121. // unsigned short reserved;
  4122. // void *overflow_arg_area;
  4123. // void *reg_save_area;
  4124. // };
  4125. bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
  4126. bool isInt = !Ty->isFloatingType();
  4127. bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
  4128. // All aggregates are passed indirectly? That doesn't seem consistent
  4129. // with the argument-lowering code.
  4130. bool isIndirect = isAggregateTypeForABI(Ty);
  4131. CGBuilderTy &Builder = CGF.Builder;
  4132. // The calling convention either uses 1-2 GPRs or 1 FPR.
  4133. Address NumRegsAddr = Address::invalid();
  4134. if (isInt || IsSoftFloatABI) {
  4135. NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr");
  4136. } else {
  4137. NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr");
  4138. }
  4139. llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
  4140. // "Align" the register count when TY is i64.
  4141. if (isI64 || (isF64 && IsSoftFloatABI)) {
  4142. NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
  4143. NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
  4144. }
  4145. llvm::Value *CC =
  4146. Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
  4147. llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
  4148. llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
  4149. llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
  4150. Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
  4151. llvm::Type *DirectTy = CGF.ConvertType(Ty), *ElementTy = DirectTy;
  4152. if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
  4153. // Case 1: consume registers.
  4154. Address RegAddr = Address::invalid();
  4155. {
  4156. CGF.EmitBlock(UsingRegs);
  4157. Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4);
  4158. RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), CGF.Int8Ty,
  4159. CharUnits::fromQuantity(8));
  4160. assert(RegAddr.getElementType() == CGF.Int8Ty);
  4161. // Floating-point registers start after the general-purpose registers.
  4162. if (!(isInt || IsSoftFloatABI)) {
  4163. RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
  4164. CharUnits::fromQuantity(32));
  4165. }
  4166. // Get the address of the saved value by scaling the number of
  4167. // registers we've used by the number of
  4168. CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
  4169. llvm::Value *RegOffset =
  4170. Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
  4171. RegAddr = Address(
  4172. Builder.CreateInBoundsGEP(CGF.Int8Ty, RegAddr.getPointer(), RegOffset),
  4173. CGF.Int8Ty, RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
  4174. RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
  4175. // Increase the used-register count.
  4176. NumRegs =
  4177. Builder.CreateAdd(NumRegs,
  4178. Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
  4179. Builder.CreateStore(NumRegs, NumRegsAddr);
  4180. CGF.EmitBranch(Cont);
  4181. }
  4182. // Case 2: consume space in the overflow area.
  4183. Address MemAddr = Address::invalid();
  4184. {
  4185. CGF.EmitBlock(UsingOverflow);
  4186. Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
  4187. // Everything in the overflow area is rounded up to a size of at least 4.
  4188. CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
  4189. CharUnits Size;
  4190. if (!isIndirect) {
  4191. auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
  4192. Size = TypeInfo.Width.alignTo(OverflowAreaAlign);
  4193. } else {
  4194. Size = CGF.getPointerSize();
  4195. }
  4196. Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3);
  4197. Address OverflowArea =
  4198. Address(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), CGF.Int8Ty,
  4199. OverflowAreaAlign);
  4200. // Round up address of argument to alignment
  4201. CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
  4202. if (Align > OverflowAreaAlign) {
  4203. llvm::Value *Ptr = OverflowArea.getPointer();
  4204. OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
  4205. OverflowArea.getElementType(), Align);
  4206. }
  4207. MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
  4208. // Increase the overflow area.
  4209. OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
  4210. Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
  4211. CGF.EmitBranch(Cont);
  4212. }
  4213. CGF.EmitBlock(Cont);
  4214. // Merge the cases with a phi.
  4215. Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
  4216. "vaarg.addr");
  4217. // Load the pointer if the argument was passed indirectly.
  4218. if (isIndirect) {
  4219. Result = Address(Builder.CreateLoad(Result, "aggr"), ElementTy,
  4220. getContext().getTypeAlignInChars(Ty));
  4221. }
  4222. return Result;
  4223. }
  4224. bool PPC32TargetCodeGenInfo::isStructReturnInRegABI(
  4225. const llvm::Triple &Triple, const CodeGenOptions &Opts) {
  4226. assert(Triple.isPPC32());
  4227. switch (Opts.getStructReturnConvention()) {
  4228. case CodeGenOptions::SRCK_Default:
  4229. break;
  4230. case CodeGenOptions::SRCK_OnStack: // -maix-struct-return
  4231. return false;
  4232. case CodeGenOptions::SRCK_InRegs: // -msvr4-struct-return
  4233. return true;
  4234. }
  4235. if (Triple.isOSBinFormatELF() && !Triple.isOSLinux())
  4236. return true;
  4237. return false;
  4238. }
  4239. bool
  4240. PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  4241. llvm::Value *Address) const {
  4242. return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ false,
  4243. /*IsAIX*/ false);
  4244. }
  4245. // PowerPC-64
  4246. namespace {
  4247. /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
  4248. class PPC64_SVR4_ABIInfo : public ABIInfo {
  4249. public:
  4250. enum ABIKind {
  4251. ELFv1 = 0,
  4252. ELFv2
  4253. };
  4254. private:
  4255. static const unsigned GPRBits = 64;
  4256. ABIKind Kind;
  4257. bool IsSoftFloatABI;
  4258. public:
  4259. PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind,
  4260. bool SoftFloatABI)
  4261. : ABIInfo(CGT), Kind(Kind), IsSoftFloatABI(SoftFloatABI) {}
  4262. bool isPromotableTypeForABI(QualType Ty) const;
  4263. CharUnits getParamTypeAlignment(QualType Ty) const;
  4264. ABIArgInfo classifyReturnType(QualType RetTy) const;
  4265. ABIArgInfo classifyArgumentType(QualType Ty) const;
  4266. bool isHomogeneousAggregateBaseType(QualType Ty) const override;
  4267. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  4268. uint64_t Members) const override;
  4269. // TODO: We can add more logic to computeInfo to improve performance.
  4270. // Example: For aggregate arguments that fit in a register, we could
  4271. // use getDirectInReg (as is done below for structs containing a single
  4272. // floating-point value) to avoid pushing them to memory on function
  4273. // entry. This would require changing the logic in PPCISelLowering
  4274. // when lowering the parameters in the caller and args in the callee.
  4275. void computeInfo(CGFunctionInfo &FI) const override {
  4276. if (!getCXXABI().classifyReturnType(FI))
  4277. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  4278. for (auto &I : FI.arguments()) {
  4279. // We rely on the default argument classification for the most part.
  4280. // One exception: An aggregate containing a single floating-point
  4281. // or vector item must be passed in a register if one is available.
  4282. const Type *T = isSingleElementStruct(I.type, getContext());
  4283. if (T) {
  4284. const BuiltinType *BT = T->getAs<BuiltinType>();
  4285. if ((T->isVectorType() && getContext().getTypeSize(T) == 128) ||
  4286. (BT && BT->isFloatingPoint())) {
  4287. QualType QT(T, 0);
  4288. I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
  4289. continue;
  4290. }
  4291. }
  4292. I.info = classifyArgumentType(I.type);
  4293. }
  4294. }
  4295. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4296. QualType Ty) const override;
  4297. };
  4298. class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
  4299. public:
  4300. PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
  4301. PPC64_SVR4_ABIInfo::ABIKind Kind,
  4302. bool SoftFloatABI)
  4303. : TargetCodeGenInfo(
  4304. std::make_unique<PPC64_SVR4_ABIInfo>(CGT, Kind, SoftFloatABI)) {
  4305. SwiftInfo =
  4306. std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false);
  4307. }
  4308. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  4309. // This is recovered from gcc output.
  4310. return 1; // r1 is the dedicated stack pointer
  4311. }
  4312. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  4313. llvm::Value *Address) const override;
  4314. };
  4315. class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
  4316. public:
  4317. PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
  4318. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  4319. // This is recovered from gcc output.
  4320. return 1; // r1 is the dedicated stack pointer
  4321. }
  4322. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  4323. llvm::Value *Address) const override;
  4324. };
  4325. }
  4326. // Return true if the ABI requires Ty to be passed sign- or zero-
  4327. // extended to 64 bits.
  4328. bool
  4329. PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
  4330. // Treat an enum type as its underlying type.
  4331. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  4332. Ty = EnumTy->getDecl()->getIntegerType();
  4333. // Promotable integer types are required to be promoted by the ABI.
  4334. if (isPromotableIntegerTypeForABI(Ty))
  4335. return true;
  4336. // In addition to the usual promotable integer types, we also need to
  4337. // extend all 32-bit types, since the ABI requires promotion to 64 bits.
  4338. if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
  4339. switch (BT->getKind()) {
  4340. case BuiltinType::Int:
  4341. case BuiltinType::UInt:
  4342. return true;
  4343. default:
  4344. break;
  4345. }
  4346. if (const auto *EIT = Ty->getAs<BitIntType>())
  4347. if (EIT->getNumBits() < 64)
  4348. return true;
  4349. return false;
  4350. }
  4351. /// isAlignedParamType - Determine whether a type requires 16-byte or
  4352. /// higher alignment in the parameter area. Always returns at least 8.
  4353. CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
  4354. // Complex types are passed just like their elements.
  4355. if (const ComplexType *CTy = Ty->getAs<ComplexType>())
  4356. Ty = CTy->getElementType();
  4357. auto FloatUsesVector = [this](QualType Ty){
  4358. return Ty->isRealFloatingType() && &getContext().getFloatTypeSemantics(
  4359. Ty) == &llvm::APFloat::IEEEquad();
  4360. };
  4361. // Only vector types of size 16 bytes need alignment (larger types are
  4362. // passed via reference, smaller types are not aligned).
  4363. if (Ty->isVectorType()) {
  4364. return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
  4365. } else if (FloatUsesVector(Ty)) {
  4366. // According to ABI document section 'Optional Save Areas': If extended
  4367. // precision floating-point values in IEEE BINARY 128 QUADRUPLE PRECISION
  4368. // format are supported, map them to a single quadword, quadword aligned.
  4369. return CharUnits::fromQuantity(16);
  4370. }
  4371. // For single-element float/vector structs, we consider the whole type
  4372. // to have the same alignment requirements as its single element.
  4373. const Type *AlignAsType = nullptr;
  4374. const Type *EltType = isSingleElementStruct(Ty, getContext());
  4375. if (EltType) {
  4376. const BuiltinType *BT = EltType->getAs<BuiltinType>();
  4377. if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
  4378. (BT && BT->isFloatingPoint()))
  4379. AlignAsType = EltType;
  4380. }
  4381. // Likewise for ELFv2 homogeneous aggregates.
  4382. const Type *Base = nullptr;
  4383. uint64_t Members = 0;
  4384. if (!AlignAsType && Kind == ELFv2 &&
  4385. isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
  4386. AlignAsType = Base;
  4387. // With special case aggregates, only vector base types need alignment.
  4388. if (AlignAsType) {
  4389. bool UsesVector = AlignAsType->isVectorType() ||
  4390. FloatUsesVector(QualType(AlignAsType, 0));
  4391. return CharUnits::fromQuantity(UsesVector ? 16 : 8);
  4392. }
  4393. // Otherwise, we only need alignment for any aggregate type that
  4394. // has an alignment requirement of >= 16 bytes.
  4395. if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
  4396. return CharUnits::fromQuantity(16);
  4397. }
  4398. return CharUnits::fromQuantity(8);
  4399. }
  4400. /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
  4401. /// aggregate. Base is set to the base element type, and Members is set
  4402. /// to the number of base elements.
  4403. bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
  4404. uint64_t &Members) const {
  4405. if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
  4406. uint64_t NElements = AT->getSize().getZExtValue();
  4407. if (NElements == 0)
  4408. return false;
  4409. if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
  4410. return false;
  4411. Members *= NElements;
  4412. } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
  4413. const RecordDecl *RD = RT->getDecl();
  4414. if (RD->hasFlexibleArrayMember())
  4415. return false;
  4416. Members = 0;
  4417. // If this is a C++ record, check the properties of the record such as
  4418. // bases and ABI specific restrictions
  4419. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  4420. if (!getCXXABI().isPermittedToBeHomogeneousAggregate(CXXRD))
  4421. return false;
  4422. for (const auto &I : CXXRD->bases()) {
  4423. // Ignore empty records.
  4424. if (isEmptyRecord(getContext(), I.getType(), true))
  4425. continue;
  4426. uint64_t FldMembers;
  4427. if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
  4428. return false;
  4429. Members += FldMembers;
  4430. }
  4431. }
  4432. for (const auto *FD : RD->fields()) {
  4433. // Ignore (non-zero arrays of) empty records.
  4434. QualType FT = FD->getType();
  4435. while (const ConstantArrayType *AT =
  4436. getContext().getAsConstantArrayType(FT)) {
  4437. if (AT->getSize().getZExtValue() == 0)
  4438. return false;
  4439. FT = AT->getElementType();
  4440. }
  4441. if (isEmptyRecord(getContext(), FT, true))
  4442. continue;
  4443. if (isZeroLengthBitfieldPermittedInHomogeneousAggregate() &&
  4444. FD->isZeroLengthBitField(getContext()))
  4445. continue;
  4446. uint64_t FldMembers;
  4447. if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
  4448. return false;
  4449. Members = (RD->isUnion() ?
  4450. std::max(Members, FldMembers) : Members + FldMembers);
  4451. }
  4452. if (!Base)
  4453. return false;
  4454. // Ensure there is no padding.
  4455. if (getContext().getTypeSize(Base) * Members !=
  4456. getContext().getTypeSize(Ty))
  4457. return false;
  4458. } else {
  4459. Members = 1;
  4460. if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
  4461. Members = 2;
  4462. Ty = CT->getElementType();
  4463. }
  4464. // Most ABIs only support float, double, and some vector type widths.
  4465. if (!isHomogeneousAggregateBaseType(Ty))
  4466. return false;
  4467. // The base type must be the same for all members. Types that
  4468. // agree in both total size and mode (float vs. vector) are
  4469. // treated as being equivalent here.
  4470. const Type *TyPtr = Ty.getTypePtr();
  4471. if (!Base) {
  4472. Base = TyPtr;
  4473. // If it's a non-power-of-2 vector, its size is already a power-of-2,
  4474. // so make sure to widen it explicitly.
  4475. if (const VectorType *VT = Base->getAs<VectorType>()) {
  4476. QualType EltTy = VT->getElementType();
  4477. unsigned NumElements =
  4478. getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
  4479. Base = getContext()
  4480. .getVectorType(EltTy, NumElements, VT->getVectorKind())
  4481. .getTypePtr();
  4482. }
  4483. }
  4484. if (Base->isVectorType() != TyPtr->isVectorType() ||
  4485. getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
  4486. return false;
  4487. }
  4488. return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
  4489. }
  4490. bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  4491. // Homogeneous aggregates for ELFv2 must have base types of float,
  4492. // double, long double, or 128-bit vectors.
  4493. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  4494. if (BT->getKind() == BuiltinType::Float ||
  4495. BT->getKind() == BuiltinType::Double ||
  4496. BT->getKind() == BuiltinType::LongDouble ||
  4497. BT->getKind() == BuiltinType::Ibm128 ||
  4498. (getContext().getTargetInfo().hasFloat128Type() &&
  4499. (BT->getKind() == BuiltinType::Float128))) {
  4500. if (IsSoftFloatABI)
  4501. return false;
  4502. return true;
  4503. }
  4504. }
  4505. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  4506. if (getContext().getTypeSize(VT) == 128)
  4507. return true;
  4508. }
  4509. return false;
  4510. }
  4511. bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
  4512. const Type *Base, uint64_t Members) const {
  4513. // Vector and fp128 types require one register, other floating point types
  4514. // require one or two registers depending on their size.
  4515. uint32_t NumRegs =
  4516. ((getContext().getTargetInfo().hasFloat128Type() &&
  4517. Base->isFloat128Type()) ||
  4518. Base->isVectorType()) ? 1
  4519. : (getContext().getTypeSize(Base) + 63) / 64;
  4520. // Homogeneous Aggregates may occupy at most 8 registers.
  4521. return Members * NumRegs <= 8;
  4522. }
  4523. ABIArgInfo
  4524. PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
  4525. Ty = useFirstFieldIfTransparentUnion(Ty);
  4526. if (Ty->isAnyComplexType())
  4527. return ABIArgInfo::getDirect();
  4528. // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
  4529. // or via reference (larger than 16 bytes).
  4530. if (Ty->isVectorType()) {
  4531. uint64_t Size = getContext().getTypeSize(Ty);
  4532. if (Size > 128)
  4533. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  4534. else if (Size < 128) {
  4535. llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
  4536. return ABIArgInfo::getDirect(CoerceTy);
  4537. }
  4538. }
  4539. if (const auto *EIT = Ty->getAs<BitIntType>())
  4540. if (EIT->getNumBits() > 128)
  4541. return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
  4542. if (isAggregateTypeForABI(Ty)) {
  4543. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  4544. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  4545. uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
  4546. uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
  4547. // ELFv2 homogeneous aggregates are passed as array types.
  4548. const Type *Base = nullptr;
  4549. uint64_t Members = 0;
  4550. if (Kind == ELFv2 &&
  4551. isHomogeneousAggregate(Ty, Base, Members)) {
  4552. llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
  4553. llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
  4554. return ABIArgInfo::getDirect(CoerceTy);
  4555. }
  4556. // If an aggregate may end up fully in registers, we do not
  4557. // use the ByVal method, but pass the aggregate as array.
  4558. // This is usually beneficial since we avoid forcing the
  4559. // back-end to store the argument to memory.
  4560. uint64_t Bits = getContext().getTypeSize(Ty);
  4561. if (Bits > 0 && Bits <= 8 * GPRBits) {
  4562. llvm::Type *CoerceTy;
  4563. // Types up to 8 bytes are passed as integer type (which will be
  4564. // properly aligned in the argument save area doubleword).
  4565. if (Bits <= GPRBits)
  4566. CoerceTy =
  4567. llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
  4568. // Larger types are passed as arrays, with the base type selected
  4569. // according to the required alignment in the save area.
  4570. else {
  4571. uint64_t RegBits = ABIAlign * 8;
  4572. uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
  4573. llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
  4574. CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
  4575. }
  4576. return ABIArgInfo::getDirect(CoerceTy);
  4577. }
  4578. // All other aggregates are passed ByVal.
  4579. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
  4580. /*ByVal=*/true,
  4581. /*Realign=*/TyAlign > ABIAlign);
  4582. }
  4583. return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
  4584. : ABIArgInfo::getDirect());
  4585. }
  4586. ABIArgInfo
  4587. PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
  4588. if (RetTy->isVoidType())
  4589. return ABIArgInfo::getIgnore();
  4590. if (RetTy->isAnyComplexType())
  4591. return ABIArgInfo::getDirect();
  4592. // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
  4593. // or via reference (larger than 16 bytes).
  4594. if (RetTy->isVectorType()) {
  4595. uint64_t Size = getContext().getTypeSize(RetTy);
  4596. if (Size > 128)
  4597. return getNaturalAlignIndirect(RetTy);
  4598. else if (Size < 128) {
  4599. llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
  4600. return ABIArgInfo::getDirect(CoerceTy);
  4601. }
  4602. }
  4603. if (const auto *EIT = RetTy->getAs<BitIntType>())
  4604. if (EIT->getNumBits() > 128)
  4605. return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
  4606. if (isAggregateTypeForABI(RetTy)) {
  4607. // ELFv2 homogeneous aggregates are returned as array types.
  4608. const Type *Base = nullptr;
  4609. uint64_t Members = 0;
  4610. if (Kind == ELFv2 &&
  4611. isHomogeneousAggregate(RetTy, Base, Members)) {
  4612. llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
  4613. llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
  4614. return ABIArgInfo::getDirect(CoerceTy);
  4615. }
  4616. // ELFv2 small aggregates are returned in up to two registers.
  4617. uint64_t Bits = getContext().getTypeSize(RetTy);
  4618. if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
  4619. if (Bits == 0)
  4620. return ABIArgInfo::getIgnore();
  4621. llvm::Type *CoerceTy;
  4622. if (Bits > GPRBits) {
  4623. CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
  4624. CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
  4625. } else
  4626. CoerceTy =
  4627. llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
  4628. return ABIArgInfo::getDirect(CoerceTy);
  4629. }
  4630. // All other aggregates are returned indirectly.
  4631. return getNaturalAlignIndirect(RetTy);
  4632. }
  4633. return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
  4634. : ABIArgInfo::getDirect());
  4635. }
  4636. // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
  4637. Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4638. QualType Ty) const {
  4639. auto TypeInfo = getContext().getTypeInfoInChars(Ty);
  4640. TypeInfo.Align = getParamTypeAlignment(Ty);
  4641. CharUnits SlotSize = CharUnits::fromQuantity(8);
  4642. // If we have a complex type and the base type is smaller than 8 bytes,
  4643. // the ABI calls for the real and imaginary parts to be right-adjusted
  4644. // in separate doublewords. However, Clang expects us to produce a
  4645. // pointer to a structure with the two parts packed tightly. So generate
  4646. // loads of the real and imaginary parts relative to the va_list pointer,
  4647. // and store them to a temporary structure.
  4648. if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
  4649. CharUnits EltSize = TypeInfo.Width / 2;
  4650. if (EltSize < SlotSize)
  4651. return complexTempStructure(CGF, VAListAddr, Ty, SlotSize, EltSize, CTy);
  4652. }
  4653. // Otherwise, just use the general rule.
  4654. //
  4655. // The PPC64 ABI passes some arguments in integer registers, even to variadic
  4656. // functions. To allow va_list to use the simple "void*" representation,
  4657. // variadic calls allocate space in the argument area for the integer argument
  4658. // registers, and variadic functions spill their integer argument registers to
  4659. // this area in their prologues. When aggregates smaller than a register are
  4660. // passed this way, they are passed in the least significant bits of the
  4661. // register, which means that after spilling on big-endian targets they will
  4662. // be right-aligned in their argument slot. This is uncommon; for a variety of
  4663. // reasons, other big-endian targets don't end up right-aligning aggregate
  4664. // types this way, and so right-alignment only applies to fundamental types.
  4665. // So on PPC64, we must force the use of right-alignment even for aggregates.
  4666. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, TypeInfo,
  4667. SlotSize, /*AllowHigher*/ true,
  4668. /*ForceRightAdjust*/ true);
  4669. }
  4670. bool
  4671. PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
  4672. CodeGen::CodeGenFunction &CGF,
  4673. llvm::Value *Address) const {
  4674. return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true,
  4675. /*IsAIX*/ false);
  4676. }
  4677. bool
  4678. PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  4679. llvm::Value *Address) const {
  4680. return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true,
  4681. /*IsAIX*/ false);
  4682. }
  4683. //===----------------------------------------------------------------------===//
  4684. // AArch64 ABI Implementation
  4685. //===----------------------------------------------------------------------===//
  4686. namespace {
  4687. class AArch64ABIInfo : public ABIInfo {
  4688. public:
  4689. enum ABIKind {
  4690. AAPCS = 0,
  4691. DarwinPCS,
  4692. Win64
  4693. };
  4694. private:
  4695. ABIKind Kind;
  4696. public:
  4697. AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {}
  4698. private:
  4699. ABIKind getABIKind() const { return Kind; }
  4700. bool isDarwinPCS() const { return Kind == DarwinPCS; }
  4701. ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const;
  4702. ABIArgInfo classifyArgumentType(QualType RetTy, bool IsVariadic,
  4703. unsigned CallingConvention) const;
  4704. ABIArgInfo coerceIllegalVector(QualType Ty) const;
  4705. bool isHomogeneousAggregateBaseType(QualType Ty) const override;
  4706. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  4707. uint64_t Members) const override;
  4708. bool isZeroLengthBitfieldPermittedInHomogeneousAggregate() const override;
  4709. bool isIllegalVectorType(QualType Ty) const;
  4710. void computeInfo(CGFunctionInfo &FI) const override {
  4711. if (!::classifyReturnType(getCXXABI(), FI, *this))
  4712. FI.getReturnInfo() =
  4713. classifyReturnType(FI.getReturnType(), FI.isVariadic());
  4714. for (auto &it : FI.arguments())
  4715. it.info = classifyArgumentType(it.type, FI.isVariadic(),
  4716. FI.getCallingConvention());
  4717. }
  4718. Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
  4719. CodeGenFunction &CGF) const;
  4720. Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
  4721. CodeGenFunction &CGF) const;
  4722. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4723. QualType Ty) const override {
  4724. llvm::Type *BaseTy = CGF.ConvertType(Ty);
  4725. if (isa<llvm::ScalableVectorType>(BaseTy))
  4726. llvm::report_fatal_error("Passing SVE types to variadic functions is "
  4727. "currently not supported");
  4728. return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty)
  4729. : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
  4730. : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
  4731. }
  4732. Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  4733. QualType Ty) const override;
  4734. bool allowBFloatArgsAndRet() const override {
  4735. return getTarget().hasBFloat16Type();
  4736. }
  4737. };
  4738. class AArch64SwiftABIInfo : public SwiftABIInfo {
  4739. public:
  4740. explicit AArch64SwiftABIInfo(CodeGenTypes &CGT)
  4741. : SwiftABIInfo(CGT, /*SwiftErrorInRegister=*/true) {}
  4742. bool isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy,
  4743. unsigned NumElts) const override;
  4744. };
  4745. class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
  4746. public:
  4747. AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
  4748. : TargetCodeGenInfo(std::make_unique<AArch64ABIInfo>(CGT, Kind)) {
  4749. SwiftInfo = std::make_unique<AArch64SwiftABIInfo>(CGT);
  4750. }
  4751. StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
  4752. return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue";
  4753. }
  4754. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  4755. return 31;
  4756. }
  4757. bool doesReturnSlotInterfereWithArgs() const override { return false; }
  4758. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  4759. CodeGen::CodeGenModule &CGM) const override {
  4760. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  4761. if (!FD)
  4762. return;
  4763. const auto *TA = FD->getAttr<TargetAttr>();
  4764. if (TA == nullptr)
  4765. return;
  4766. ParsedTargetAttr Attr =
  4767. CGM.getTarget().parseTargetAttr(TA->getFeaturesStr());
  4768. if (Attr.BranchProtection.empty())
  4769. return;
  4770. TargetInfo::BranchProtectionInfo BPI;
  4771. StringRef Error;
  4772. (void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
  4773. Attr.CPU, BPI, Error);
  4774. assert(Error.empty());
  4775. auto *Fn = cast<llvm::Function>(GV);
  4776. static const char *SignReturnAddrStr[] = {"none", "non-leaf", "all"};
  4777. Fn->addFnAttr("sign-return-address", SignReturnAddrStr[static_cast<int>(BPI.SignReturnAddr)]);
  4778. if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
  4779. Fn->addFnAttr("sign-return-address-key",
  4780. BPI.SignKey == LangOptions::SignReturnAddressKeyKind::AKey
  4781. ? "a_key"
  4782. : "b_key");
  4783. }
  4784. Fn->addFnAttr("branch-target-enforcement",
  4785. BPI.BranchTargetEnforcement ? "true" : "false");
  4786. }
  4787. bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,
  4788. llvm::Type *Ty) const override {
  4789. if (CGF.getTarget().hasFeature("ls64")) {
  4790. auto *ST = dyn_cast<llvm::StructType>(Ty);
  4791. if (ST && ST->getNumElements() == 1) {
  4792. auto *AT = dyn_cast<llvm::ArrayType>(ST->getElementType(0));
  4793. if (AT && AT->getNumElements() == 8 &&
  4794. AT->getElementType()->isIntegerTy(64))
  4795. return true;
  4796. }
  4797. }
  4798. return TargetCodeGenInfo::isScalarizableAsmOperand(CGF, Ty);
  4799. }
  4800. };
  4801. class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo {
  4802. public:
  4803. WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K)
  4804. : AArch64TargetCodeGenInfo(CGT, K) {}
  4805. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  4806. CodeGen::CodeGenModule &CGM) const override;
  4807. void getDependentLibraryOption(llvm::StringRef Lib,
  4808. llvm::SmallString<24> &Opt) const override {
  4809. Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
  4810. }
  4811. void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
  4812. llvm::SmallString<32> &Opt) const override {
  4813. Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
  4814. }
  4815. };
  4816. void WindowsAArch64TargetCodeGenInfo::setTargetAttributes(
  4817. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
  4818. AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
  4819. if (GV->isDeclaration())
  4820. return;
  4821. addStackProbeTargetAttributes(D, GV, CGM);
  4822. }
  4823. }
  4824. ABIArgInfo AArch64ABIInfo::coerceIllegalVector(QualType Ty) const {
  4825. assert(Ty->isVectorType() && "expected vector type!");
  4826. const auto *VT = Ty->castAs<VectorType>();
  4827. if (VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector) {
  4828. assert(VT->getElementType()->isBuiltinType() && "expected builtin type!");
  4829. assert(VT->getElementType()->castAs<BuiltinType>()->getKind() ==
  4830. BuiltinType::UChar &&
  4831. "unexpected builtin type for SVE predicate!");
  4832. return ABIArgInfo::getDirect(llvm::ScalableVectorType::get(
  4833. llvm::Type::getInt1Ty(getVMContext()), 16));
  4834. }
  4835. if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector) {
  4836. assert(VT->getElementType()->isBuiltinType() && "expected builtin type!");
  4837. const auto *BT = VT->getElementType()->castAs<BuiltinType>();
  4838. llvm::ScalableVectorType *ResType = nullptr;
  4839. switch (BT->getKind()) {
  4840. default:
  4841. llvm_unreachable("unexpected builtin type for SVE vector!");
  4842. case BuiltinType::SChar:
  4843. case BuiltinType::UChar:
  4844. ResType = llvm::ScalableVectorType::get(
  4845. llvm::Type::getInt8Ty(getVMContext()), 16);
  4846. break;
  4847. case BuiltinType::Short:
  4848. case BuiltinType::UShort:
  4849. ResType = llvm::ScalableVectorType::get(
  4850. llvm::Type::getInt16Ty(getVMContext()), 8);
  4851. break;
  4852. case BuiltinType::Int:
  4853. case BuiltinType::UInt:
  4854. ResType = llvm::ScalableVectorType::get(
  4855. llvm::Type::getInt32Ty(getVMContext()), 4);
  4856. break;
  4857. case BuiltinType::Long:
  4858. case BuiltinType::ULong:
  4859. ResType = llvm::ScalableVectorType::get(
  4860. llvm::Type::getInt64Ty(getVMContext()), 2);
  4861. break;
  4862. case BuiltinType::Half:
  4863. ResType = llvm::ScalableVectorType::get(
  4864. llvm::Type::getHalfTy(getVMContext()), 8);
  4865. break;
  4866. case BuiltinType::Float:
  4867. ResType = llvm::ScalableVectorType::get(
  4868. llvm::Type::getFloatTy(getVMContext()), 4);
  4869. break;
  4870. case BuiltinType::Double:
  4871. ResType = llvm::ScalableVectorType::get(
  4872. llvm::Type::getDoubleTy(getVMContext()), 2);
  4873. break;
  4874. case BuiltinType::BFloat16:
  4875. ResType = llvm::ScalableVectorType::get(
  4876. llvm::Type::getBFloatTy(getVMContext()), 8);
  4877. break;
  4878. }
  4879. return ABIArgInfo::getDirect(ResType);
  4880. }
  4881. uint64_t Size = getContext().getTypeSize(Ty);
  4882. // Android promotes <2 x i8> to i16, not i32
  4883. if (isAndroid() && (Size <= 16)) {
  4884. llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
  4885. return ABIArgInfo::getDirect(ResType);
  4886. }
  4887. if (Size <= 32) {
  4888. llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
  4889. return ABIArgInfo::getDirect(ResType);
  4890. }
  4891. if (Size == 64) {
  4892. auto *ResType =
  4893. llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
  4894. return ABIArgInfo::getDirect(ResType);
  4895. }
  4896. if (Size == 128) {
  4897. auto *ResType =
  4898. llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
  4899. return ABIArgInfo::getDirect(ResType);
  4900. }
  4901. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  4902. }
  4903. ABIArgInfo
  4904. AArch64ABIInfo::classifyArgumentType(QualType Ty, bool IsVariadic,
  4905. unsigned CallingConvention) const {
  4906. Ty = useFirstFieldIfTransparentUnion(Ty);
  4907. // Handle illegal vector types here.
  4908. if (isIllegalVectorType(Ty))
  4909. return coerceIllegalVector(Ty);
  4910. if (!isAggregateTypeForABI(Ty)) {
  4911. // Treat an enum type as its underlying type.
  4912. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  4913. Ty = EnumTy->getDecl()->getIntegerType();
  4914. if (const auto *EIT = Ty->getAs<BitIntType>())
  4915. if (EIT->getNumBits() > 128)
  4916. return getNaturalAlignIndirect(Ty);
  4917. return (isPromotableIntegerTypeForABI(Ty) && isDarwinPCS()
  4918. ? ABIArgInfo::getExtend(Ty)
  4919. : ABIArgInfo::getDirect());
  4920. }
  4921. // Structures with either a non-trivial destructor or a non-trivial
  4922. // copy constructor are always indirect.
  4923. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  4924. return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
  4925. CGCXXABI::RAA_DirectInMemory);
  4926. }
  4927. // Empty records are always ignored on Darwin, but actually passed in C++ mode
  4928. // elsewhere for GNU compatibility.
  4929. uint64_t Size = getContext().getTypeSize(Ty);
  4930. bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
  4931. if (IsEmpty || Size == 0) {
  4932. if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
  4933. return ABIArgInfo::getIgnore();
  4934. // GNU C mode. The only argument that gets ignored is an empty one with size
  4935. // 0.
  4936. if (IsEmpty && Size == 0)
  4937. return ABIArgInfo::getIgnore();
  4938. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  4939. }
  4940. // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
  4941. const Type *Base = nullptr;
  4942. uint64_t Members = 0;
  4943. bool IsWin64 = Kind == Win64 || CallingConvention == llvm::CallingConv::Win64;
  4944. bool IsWinVariadic = IsWin64 && IsVariadic;
  4945. // In variadic functions on Windows, all composite types are treated alike,
  4946. // no special handling of HFAs/HVAs.
  4947. if (!IsWinVariadic && isHomogeneousAggregate(Ty, Base, Members)) {
  4948. if (Kind != AArch64ABIInfo::AAPCS)
  4949. return ABIArgInfo::getDirect(
  4950. llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
  4951. // For alignment adjusted HFAs, cap the argument alignment to 16, leave it
  4952. // default otherwise.
  4953. unsigned Align =
  4954. getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
  4955. unsigned BaseAlign = getContext().getTypeAlignInChars(Base).getQuantity();
  4956. Align = (Align > BaseAlign && Align >= 16) ? 16 : 0;
  4957. return ABIArgInfo::getDirect(
  4958. llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members), 0,
  4959. nullptr, true, Align);
  4960. }
  4961. // Aggregates <= 16 bytes are passed directly in registers or on the stack.
  4962. if (Size <= 128) {
  4963. // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
  4964. // same size and alignment.
  4965. if (getTarget().isRenderScriptTarget()) {
  4966. return coerceToIntArray(Ty, getContext(), getVMContext());
  4967. }
  4968. unsigned Alignment;
  4969. if (Kind == AArch64ABIInfo::AAPCS) {
  4970. Alignment = getContext().getTypeUnadjustedAlign(Ty);
  4971. Alignment = Alignment < 128 ? 64 : 128;
  4972. } else {
  4973. Alignment =
  4974. std::max(getContext().getTypeAlign(Ty),
  4975. (unsigned)getTarget().getPointerWidth(LangAS::Default));
  4976. }
  4977. Size = llvm::alignTo(Size, Alignment);
  4978. // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
  4979. // For aggregates with 16-byte alignment, we use i128.
  4980. llvm::Type *BaseTy = llvm::Type::getIntNTy(getVMContext(), Alignment);
  4981. return ABIArgInfo::getDirect(
  4982. Size == Alignment ? BaseTy
  4983. : llvm::ArrayType::get(BaseTy, Size / Alignment));
  4984. }
  4985. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  4986. }
  4987. ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy,
  4988. bool IsVariadic) const {
  4989. if (RetTy->isVoidType())
  4990. return ABIArgInfo::getIgnore();
  4991. if (const auto *VT = RetTy->getAs<VectorType>()) {
  4992. if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector ||
  4993. VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
  4994. return coerceIllegalVector(RetTy);
  4995. }
  4996. // Large vector types should be returned via memory.
  4997. if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
  4998. return getNaturalAlignIndirect(RetTy);
  4999. if (!isAggregateTypeForABI(RetTy)) {
  5000. // Treat an enum type as its underlying type.
  5001. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  5002. RetTy = EnumTy->getDecl()->getIntegerType();
  5003. if (const auto *EIT = RetTy->getAs<BitIntType>())
  5004. if (EIT->getNumBits() > 128)
  5005. return getNaturalAlignIndirect(RetTy);
  5006. return (isPromotableIntegerTypeForABI(RetTy) && isDarwinPCS()
  5007. ? ABIArgInfo::getExtend(RetTy)
  5008. : ABIArgInfo::getDirect());
  5009. }
  5010. uint64_t Size = getContext().getTypeSize(RetTy);
  5011. if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
  5012. return ABIArgInfo::getIgnore();
  5013. const Type *Base = nullptr;
  5014. uint64_t Members = 0;
  5015. if (isHomogeneousAggregate(RetTy, Base, Members) &&
  5016. !(getTarget().getTriple().getArch() == llvm::Triple::aarch64_32 &&
  5017. IsVariadic))
  5018. // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
  5019. return ABIArgInfo::getDirect();
  5020. // Aggregates <= 16 bytes are returned directly in registers or on the stack.
  5021. if (Size <= 128) {
  5022. // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
  5023. // same size and alignment.
  5024. if (getTarget().isRenderScriptTarget()) {
  5025. return coerceToIntArray(RetTy, getContext(), getVMContext());
  5026. }
  5027. if (Size <= 64 && getDataLayout().isLittleEndian()) {
  5028. // Composite types are returned in lower bits of a 64-bit register for LE,
  5029. // and in higher bits for BE. However, integer types are always returned
  5030. // in lower bits for both LE and BE, and they are not rounded up to
  5031. // 64-bits. We can skip rounding up of composite types for LE, but not for
  5032. // BE, otherwise composite types will be indistinguishable from integer
  5033. // types.
  5034. return ABIArgInfo::getDirect(
  5035. llvm::IntegerType::get(getVMContext(), Size));
  5036. }
  5037. unsigned Alignment = getContext().getTypeAlign(RetTy);
  5038. Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
  5039. // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
  5040. // For aggregates with 16-byte alignment, we use i128.
  5041. if (Alignment < 128 && Size == 128) {
  5042. llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
  5043. return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
  5044. }
  5045. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
  5046. }
  5047. return getNaturalAlignIndirect(RetTy);
  5048. }
  5049. /// isIllegalVectorType - check whether the vector type is legal for AArch64.
  5050. bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
  5051. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  5052. // Check whether VT is a fixed-length SVE vector. These types are
  5053. // represented as scalable vectors in function args/return and must be
  5054. // coerced from fixed vectors.
  5055. if (VT->getVectorKind() == VectorType::SveFixedLengthDataVector ||
  5056. VT->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
  5057. return true;
  5058. // Check whether VT is legal.
  5059. unsigned NumElements = VT->getNumElements();
  5060. uint64_t Size = getContext().getTypeSize(VT);
  5061. // NumElements should be power of 2.
  5062. if (!llvm::isPowerOf2_32(NumElements))
  5063. return true;
  5064. // arm64_32 has to be compatible with the ARM logic here, which allows huge
  5065. // vectors for some reason.
  5066. llvm::Triple Triple = getTarget().getTriple();
  5067. if (Triple.getArch() == llvm::Triple::aarch64_32 &&
  5068. Triple.isOSBinFormatMachO())
  5069. return Size <= 32;
  5070. return Size != 64 && (Size != 128 || NumElements == 1);
  5071. }
  5072. return false;
  5073. }
  5074. bool AArch64SwiftABIInfo::isLegalVectorType(CharUnits VectorSize,
  5075. llvm::Type *EltTy,
  5076. unsigned NumElts) const {
  5077. if (!llvm::isPowerOf2_32(NumElts))
  5078. return false;
  5079. if (VectorSize.getQuantity() != 8 &&
  5080. (VectorSize.getQuantity() != 16 || NumElts == 1))
  5081. return false;
  5082. return true;
  5083. }
  5084. bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  5085. // Homogeneous aggregates for AAPCS64 must have base types of a floating
  5086. // point type or a short-vector type. This is the same as the 32-bit ABI,
  5087. // but with the difference that any floating-point type is allowed,
  5088. // including __fp16.
  5089. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  5090. if (BT->isFloatingPoint())
  5091. return true;
  5092. } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
  5093. unsigned VecSize = getContext().getTypeSize(VT);
  5094. if (VecSize == 64 || VecSize == 128)
  5095. return true;
  5096. }
  5097. return false;
  5098. }
  5099. bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
  5100. uint64_t Members) const {
  5101. return Members <= 4;
  5102. }
  5103. bool AArch64ABIInfo::isZeroLengthBitfieldPermittedInHomogeneousAggregate()
  5104. const {
  5105. // AAPCS64 says that the rule for whether something is a homogeneous
  5106. // aggregate is applied to the output of the data layout decision. So
  5107. // anything that doesn't affect the data layout also does not affect
  5108. // homogeneity. In particular, zero-length bitfields don't stop a struct
  5109. // being homogeneous.
  5110. return true;
  5111. }
  5112. Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
  5113. CodeGenFunction &CGF) const {
  5114. ABIArgInfo AI = classifyArgumentType(Ty, /*IsVariadic=*/true,
  5115. CGF.CurFnInfo->getCallingConvention());
  5116. // Empty records are ignored for parameter passing purposes.
  5117. if (AI.isIgnore()) {
  5118. uint64_t PointerSize = getTarget().getPointerWidth(LangAS::Default) / 8;
  5119. CharUnits SlotSize = CharUnits::fromQuantity(PointerSize);
  5120. VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
  5121. auto *Load = CGF.Builder.CreateLoad(VAListAddr);
  5122. Address Addr = Address(Load, CGF.Int8Ty, SlotSize);
  5123. return CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
  5124. }
  5125. bool IsIndirect = AI.isIndirect();
  5126. llvm::Type *BaseTy = CGF.ConvertType(Ty);
  5127. if (IsIndirect)
  5128. BaseTy = llvm::PointerType::getUnqual(BaseTy);
  5129. else if (AI.getCoerceToType())
  5130. BaseTy = AI.getCoerceToType();
  5131. unsigned NumRegs = 1;
  5132. if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
  5133. BaseTy = ArrTy->getElementType();
  5134. NumRegs = ArrTy->getNumElements();
  5135. }
  5136. bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
  5137. // The AArch64 va_list type and handling is specified in the Procedure Call
  5138. // Standard, section B.4:
  5139. //
  5140. // struct {
  5141. // void *__stack;
  5142. // void *__gr_top;
  5143. // void *__vr_top;
  5144. // int __gr_offs;
  5145. // int __vr_offs;
  5146. // };
  5147. llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
  5148. llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
  5149. llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
  5150. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
  5151. CharUnits TySize = getContext().getTypeSizeInChars(Ty);
  5152. CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty);
  5153. Address reg_offs_p = Address::invalid();
  5154. llvm::Value *reg_offs = nullptr;
  5155. int reg_top_index;
  5156. int RegSize = IsIndirect ? 8 : TySize.getQuantity();
  5157. if (!IsFPR) {
  5158. // 3 is the field number of __gr_offs
  5159. reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
  5160. reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
  5161. reg_top_index = 1; // field number for __gr_top
  5162. RegSize = llvm::alignTo(RegSize, 8);
  5163. } else {
  5164. // 4 is the field number of __vr_offs.
  5165. reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
  5166. reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
  5167. reg_top_index = 2; // field number for __vr_top
  5168. RegSize = 16 * NumRegs;
  5169. }
  5170. //=======================================
  5171. // Find out where argument was passed
  5172. //=======================================
  5173. // If reg_offs >= 0 we're already using the stack for this type of
  5174. // argument. We don't want to keep updating reg_offs (in case it overflows,
  5175. // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
  5176. // whatever they get).
  5177. llvm::Value *UsingStack = nullptr;
  5178. UsingStack = CGF.Builder.CreateICmpSGE(
  5179. reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
  5180. CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
  5181. // Otherwise, at least some kind of argument could go in these registers, the
  5182. // question is whether this particular type is too big.
  5183. CGF.EmitBlock(MaybeRegBlock);
  5184. // Integer arguments may need to correct register alignment (for example a
  5185. // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
  5186. // align __gr_offs to calculate the potential address.
  5187. if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
  5188. int Align = TyAlign.getQuantity();
  5189. reg_offs = CGF.Builder.CreateAdd(
  5190. reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
  5191. "align_regoffs");
  5192. reg_offs = CGF.Builder.CreateAnd(
  5193. reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
  5194. "aligned_regoffs");
  5195. }
  5196. // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
  5197. // The fact that this is done unconditionally reflects the fact that
  5198. // allocating an argument to the stack also uses up all the remaining
  5199. // registers of the appropriate kind.
  5200. llvm::Value *NewOffset = nullptr;
  5201. NewOffset = CGF.Builder.CreateAdd(
  5202. reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
  5203. CGF.Builder.CreateStore(NewOffset, reg_offs_p);
  5204. // Now we're in a position to decide whether this argument really was in
  5205. // registers or not.
  5206. llvm::Value *InRegs = nullptr;
  5207. InRegs = CGF.Builder.CreateICmpSLE(
  5208. NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
  5209. CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
  5210. //=======================================
  5211. // Argument was in registers
  5212. //=======================================
  5213. // Now we emit the code for if the argument was originally passed in
  5214. // registers. First start the appropriate block:
  5215. CGF.EmitBlock(InRegBlock);
  5216. llvm::Value *reg_top = nullptr;
  5217. Address reg_top_p =
  5218. CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
  5219. reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
  5220. Address BaseAddr(CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, reg_top, reg_offs),
  5221. CGF.Int8Ty, CharUnits::fromQuantity(IsFPR ? 16 : 8));
  5222. Address RegAddr = Address::invalid();
  5223. llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty), *ElementTy = MemTy;
  5224. if (IsIndirect) {
  5225. // If it's been passed indirectly (actually a struct), whatever we find from
  5226. // stored registers or on the stack will actually be a struct **.
  5227. MemTy = llvm::PointerType::getUnqual(MemTy);
  5228. }
  5229. const Type *Base = nullptr;
  5230. uint64_t NumMembers = 0;
  5231. bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
  5232. if (IsHFA && NumMembers > 1) {
  5233. // Homogeneous aggregates passed in registers will have their elements split
  5234. // and stored 16-bytes apart regardless of size (they're notionally in qN,
  5235. // qN+1, ...). We reload and store into a temporary local variable
  5236. // contiguously.
  5237. assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
  5238. auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
  5239. llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
  5240. llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
  5241. Address Tmp = CGF.CreateTempAlloca(HFATy,
  5242. std::max(TyAlign, BaseTyInfo.Align));
  5243. // On big-endian platforms, the value will be right-aligned in its slot.
  5244. int Offset = 0;
  5245. if (CGF.CGM.getDataLayout().isBigEndian() &&
  5246. BaseTyInfo.Width.getQuantity() < 16)
  5247. Offset = 16 - BaseTyInfo.Width.getQuantity();
  5248. for (unsigned i = 0; i < NumMembers; ++i) {
  5249. CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
  5250. Address LoadAddr =
  5251. CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
  5252. LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
  5253. Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i);
  5254. llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
  5255. CGF.Builder.CreateStore(Elem, StoreAddr);
  5256. }
  5257. RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
  5258. } else {
  5259. // Otherwise the object is contiguous in memory.
  5260. // It might be right-aligned in its slot.
  5261. CharUnits SlotSize = BaseAddr.getAlignment();
  5262. if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
  5263. (IsHFA || !isAggregateTypeForABI(Ty)) &&
  5264. TySize < SlotSize) {
  5265. CharUnits Offset = SlotSize - TySize;
  5266. BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
  5267. }
  5268. RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
  5269. }
  5270. CGF.EmitBranch(ContBlock);
  5271. //=======================================
  5272. // Argument was on the stack
  5273. //=======================================
  5274. CGF.EmitBlock(OnStackBlock);
  5275. Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
  5276. llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
  5277. // Again, stack arguments may need realignment. In this case both integer and
  5278. // floating-point ones might be affected.
  5279. if (!IsIndirect && TyAlign.getQuantity() > 8) {
  5280. int Align = TyAlign.getQuantity();
  5281. OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
  5282. OnStackPtr = CGF.Builder.CreateAdd(
  5283. OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
  5284. "align_stack");
  5285. OnStackPtr = CGF.Builder.CreateAnd(
  5286. OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
  5287. "align_stack");
  5288. OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
  5289. }
  5290. Address OnStackAddr = Address(OnStackPtr, CGF.Int8Ty,
  5291. std::max(CharUnits::fromQuantity(8), TyAlign));
  5292. // All stack slots are multiples of 8 bytes.
  5293. CharUnits StackSlotSize = CharUnits::fromQuantity(8);
  5294. CharUnits StackSize;
  5295. if (IsIndirect)
  5296. StackSize = StackSlotSize;
  5297. else
  5298. StackSize = TySize.alignTo(StackSlotSize);
  5299. llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
  5300. llvm::Value *NewStack = CGF.Builder.CreateInBoundsGEP(
  5301. CGF.Int8Ty, OnStackPtr, StackSizeC, "new_stack");
  5302. // Write the new value of __stack for the next call to va_arg
  5303. CGF.Builder.CreateStore(NewStack, stack_p);
  5304. if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
  5305. TySize < StackSlotSize) {
  5306. CharUnits Offset = StackSlotSize - TySize;
  5307. OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
  5308. }
  5309. OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
  5310. CGF.EmitBranch(ContBlock);
  5311. //=======================================
  5312. // Tidy up
  5313. //=======================================
  5314. CGF.EmitBlock(ContBlock);
  5315. Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, OnStackAddr,
  5316. OnStackBlock, "vaargs.addr");
  5317. if (IsIndirect)
  5318. return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), ElementTy,
  5319. TyAlign);
  5320. return ResAddr;
  5321. }
  5322. Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
  5323. CodeGenFunction &CGF) const {
  5324. // The backend's lowering doesn't support va_arg for aggregates or
  5325. // illegal vector types. Lower VAArg here for these cases and use
  5326. // the LLVM va_arg instruction for everything else.
  5327. if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
  5328. return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
  5329. uint64_t PointerSize = getTarget().getPointerWidth(LangAS::Default) / 8;
  5330. CharUnits SlotSize = CharUnits::fromQuantity(PointerSize);
  5331. // Empty records are ignored for parameter passing purposes.
  5332. if (isEmptyRecord(getContext(), Ty, true)) {
  5333. Address Addr = Address(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"),
  5334. getVAListElementType(CGF), SlotSize);
  5335. Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
  5336. return Addr;
  5337. }
  5338. // The size of the actual thing passed, which might end up just
  5339. // being a pointer for indirect types.
  5340. auto TyInfo = getContext().getTypeInfoInChars(Ty);
  5341. // Arguments bigger than 16 bytes which aren't homogeneous
  5342. // aggregates should be passed indirectly.
  5343. bool IsIndirect = false;
  5344. if (TyInfo.Width.getQuantity() > 16) {
  5345. const Type *Base = nullptr;
  5346. uint64_t Members = 0;
  5347. IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
  5348. }
  5349. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
  5350. TyInfo, SlotSize, /*AllowHigherAlign*/ true);
  5351. }
  5352. Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5353. QualType Ty) const {
  5354. bool IsIndirect = false;
  5355. // Composites larger than 16 bytes are passed by reference.
  5356. if (isAggregateTypeForABI(Ty) && getContext().getTypeSize(Ty) > 128)
  5357. IsIndirect = true;
  5358. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
  5359. CGF.getContext().getTypeInfoInChars(Ty),
  5360. CharUnits::fromQuantity(8),
  5361. /*allowHigherAlign*/ false);
  5362. }
  5363. //===----------------------------------------------------------------------===//
  5364. // ARM ABI Implementation
  5365. //===----------------------------------------------------------------------===//
  5366. namespace {
  5367. class ARMABIInfo : public ABIInfo {
  5368. public:
  5369. enum ABIKind {
  5370. APCS = 0,
  5371. AAPCS = 1,
  5372. AAPCS_VFP = 2,
  5373. AAPCS16_VFP = 3,
  5374. };
  5375. private:
  5376. ABIKind Kind;
  5377. bool IsFloatABISoftFP;
  5378. public:
  5379. ARMABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {
  5380. setCCs();
  5381. IsFloatABISoftFP = CGT.getCodeGenOpts().FloatABI == "softfp" ||
  5382. CGT.getCodeGenOpts().FloatABI == ""; // default
  5383. }
  5384. bool isEABI() const {
  5385. switch (getTarget().getTriple().getEnvironment()) {
  5386. case llvm::Triple::Android:
  5387. case llvm::Triple::EABI:
  5388. case llvm::Triple::EABIHF:
  5389. case llvm::Triple::GNUEABI:
  5390. case llvm::Triple::GNUEABIHF:
  5391. case llvm::Triple::MuslEABI:
  5392. case llvm::Triple::MuslEABIHF:
  5393. return true;
  5394. default:
  5395. return false;
  5396. }
  5397. }
  5398. bool isEABIHF() const {
  5399. switch (getTarget().getTriple().getEnvironment()) {
  5400. case llvm::Triple::EABIHF:
  5401. case llvm::Triple::GNUEABIHF:
  5402. case llvm::Triple::MuslEABIHF:
  5403. return true;
  5404. default:
  5405. return false;
  5406. }
  5407. }
  5408. ABIKind getABIKind() const { return Kind; }
  5409. bool allowBFloatArgsAndRet() const override {
  5410. return !IsFloatABISoftFP && getTarget().hasBFloat16Type();
  5411. }
  5412. private:
  5413. ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic,
  5414. unsigned functionCallConv) const;
  5415. ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic,
  5416. unsigned functionCallConv) const;
  5417. ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base,
  5418. uint64_t Members) const;
  5419. ABIArgInfo coerceIllegalVector(QualType Ty) const;
  5420. bool isIllegalVectorType(QualType Ty) const;
  5421. bool containsAnyFP16Vectors(QualType Ty) const;
  5422. bool isHomogeneousAggregateBaseType(QualType Ty) const override;
  5423. bool isHomogeneousAggregateSmallEnough(const Type *Ty,
  5424. uint64_t Members) const override;
  5425. bool isZeroLengthBitfieldPermittedInHomogeneousAggregate() const override;
  5426. bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const;
  5427. void computeInfo(CGFunctionInfo &FI) const override;
  5428. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  5429. QualType Ty) const override;
  5430. llvm::CallingConv::ID getLLVMDefaultCC() const;
  5431. llvm::CallingConv::ID getABIDefaultCC() const;
  5432. void setCCs();
  5433. };
  5434. class ARMSwiftABIInfo : public SwiftABIInfo {
  5435. public:
  5436. explicit ARMSwiftABIInfo(CodeGenTypes &CGT)
  5437. : SwiftABIInfo(CGT, /*SwiftErrorInRegister=*/true) {}
  5438. bool isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy,
  5439. unsigned NumElts) const override;
  5440. };
  5441. class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
  5442. public:
  5443. ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
  5444. : TargetCodeGenInfo(std::make_unique<ARMABIInfo>(CGT, K)) {
  5445. SwiftInfo = std::make_unique<ARMSwiftABIInfo>(CGT);
  5446. }
  5447. const ARMABIInfo &getABIInfo() const {
  5448. return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
  5449. }
  5450. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  5451. return 13;
  5452. }
  5453. StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
  5454. return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
  5455. }
  5456. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  5457. llvm::Value *Address) const override {
  5458. llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
  5459. // 0-15 are the 16 integer registers.
  5460. AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
  5461. return false;
  5462. }
  5463. unsigned getSizeOfUnwindException() const override {
  5464. if (getABIInfo().isEABI()) return 88;
  5465. return TargetCodeGenInfo::getSizeOfUnwindException();
  5466. }
  5467. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  5468. CodeGen::CodeGenModule &CGM) const override {
  5469. if (GV->isDeclaration())
  5470. return;
  5471. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  5472. if (!FD)
  5473. return;
  5474. auto *Fn = cast<llvm::Function>(GV);
  5475. if (const auto *TA = FD->getAttr<TargetAttr>()) {
  5476. ParsedTargetAttr Attr =
  5477. CGM.getTarget().parseTargetAttr(TA->getFeaturesStr());
  5478. if (!Attr.BranchProtection.empty()) {
  5479. TargetInfo::BranchProtectionInfo BPI;
  5480. StringRef DiagMsg;
  5481. StringRef Arch =
  5482. Attr.CPU.empty() ? CGM.getTarget().getTargetOpts().CPU : Attr.CPU;
  5483. if (!CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
  5484. Arch, BPI, DiagMsg)) {
  5485. CGM.getDiags().Report(
  5486. D->getLocation(),
  5487. diag::warn_target_unsupported_branch_protection_attribute)
  5488. << Arch;
  5489. } else {
  5490. static const char *SignReturnAddrStr[] = {"none", "non-leaf", "all"};
  5491. assert(static_cast<unsigned>(BPI.SignReturnAddr) <= 2 &&
  5492. "Unexpected SignReturnAddressScopeKind");
  5493. Fn->addFnAttr(
  5494. "sign-return-address",
  5495. SignReturnAddrStr[static_cast<int>(BPI.SignReturnAddr)]);
  5496. Fn->addFnAttr("branch-target-enforcement",
  5497. BPI.BranchTargetEnforcement ? "true" : "false");
  5498. }
  5499. } else if (CGM.getLangOpts().BranchTargetEnforcement ||
  5500. CGM.getLangOpts().hasSignReturnAddress()) {
  5501. // If the Branch Protection attribute is missing, validate the target
  5502. // Architecture attribute against Branch Protection command line
  5503. // settings.
  5504. if (!CGM.getTarget().isBranchProtectionSupportedArch(Attr.CPU))
  5505. CGM.getDiags().Report(
  5506. D->getLocation(),
  5507. diag::warn_target_unsupported_branch_protection_attribute)
  5508. << Attr.CPU;
  5509. }
  5510. }
  5511. const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
  5512. if (!Attr)
  5513. return;
  5514. const char *Kind;
  5515. switch (Attr->getInterrupt()) {
  5516. case ARMInterruptAttr::Generic: Kind = ""; break;
  5517. case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
  5518. case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
  5519. case ARMInterruptAttr::SWI: Kind = "SWI"; break;
  5520. case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
  5521. case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
  5522. }
  5523. Fn->addFnAttr("interrupt", Kind);
  5524. ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
  5525. if (ABI == ARMABIInfo::APCS)
  5526. return;
  5527. // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
  5528. // however this is not necessarily true on taking any interrupt. Instruct
  5529. // the backend to perform a realignment as part of the function prologue.
  5530. llvm::AttrBuilder B(Fn->getContext());
  5531. B.addStackAlignmentAttr(8);
  5532. Fn->addFnAttrs(B);
  5533. }
  5534. };
  5535. class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
  5536. public:
  5537. WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
  5538. : ARMTargetCodeGenInfo(CGT, K) {}
  5539. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  5540. CodeGen::CodeGenModule &CGM) const override;
  5541. void getDependentLibraryOption(llvm::StringRef Lib,
  5542. llvm::SmallString<24> &Opt) const override {
  5543. Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
  5544. }
  5545. void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
  5546. llvm::SmallString<32> &Opt) const override {
  5547. Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
  5548. }
  5549. };
  5550. void WindowsARMTargetCodeGenInfo::setTargetAttributes(
  5551. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
  5552. ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
  5553. if (GV->isDeclaration())
  5554. return;
  5555. addStackProbeTargetAttributes(D, GV, CGM);
  5556. }
  5557. }
  5558. void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
  5559. if (!::classifyReturnType(getCXXABI(), FI, *this))
  5560. FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(),
  5561. FI.getCallingConvention());
  5562. for (auto &I : FI.arguments())
  5563. I.info = classifyArgumentType(I.type, FI.isVariadic(),
  5564. FI.getCallingConvention());
  5565. // Always honor user-specified calling convention.
  5566. if (FI.getCallingConvention() != llvm::CallingConv::C)
  5567. return;
  5568. llvm::CallingConv::ID cc = getRuntimeCC();
  5569. if (cc != llvm::CallingConv::C)
  5570. FI.setEffectiveCallingConvention(cc);
  5571. }
  5572. /// Return the default calling convention that LLVM will use.
  5573. llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
  5574. // The default calling convention that LLVM will infer.
  5575. if (isEABIHF() || getTarget().getTriple().isWatchABI())
  5576. return llvm::CallingConv::ARM_AAPCS_VFP;
  5577. else if (isEABI())
  5578. return llvm::CallingConv::ARM_AAPCS;
  5579. else
  5580. return llvm::CallingConv::ARM_APCS;
  5581. }
  5582. /// Return the calling convention that our ABI would like us to use
  5583. /// as the C calling convention.
  5584. llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
  5585. switch (getABIKind()) {
  5586. case APCS: return llvm::CallingConv::ARM_APCS;
  5587. case AAPCS: return llvm::CallingConv::ARM_AAPCS;
  5588. case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
  5589. case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
  5590. }
  5591. llvm_unreachable("bad ABI kind");
  5592. }
  5593. void ARMABIInfo::setCCs() {
  5594. assert(getRuntimeCC() == llvm::CallingConv::C);
  5595. // Don't muddy up the IR with a ton of explicit annotations if
  5596. // they'd just match what LLVM will infer from the triple.
  5597. llvm::CallingConv::ID abiCC = getABIDefaultCC();
  5598. if (abiCC != getLLVMDefaultCC())
  5599. RuntimeCC = abiCC;
  5600. }
  5601. ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const {
  5602. uint64_t Size = getContext().getTypeSize(Ty);
  5603. if (Size <= 32) {
  5604. llvm::Type *ResType =
  5605. llvm::Type::getInt32Ty(getVMContext());
  5606. return ABIArgInfo::getDirect(ResType);
  5607. }
  5608. if (Size == 64 || Size == 128) {
  5609. auto *ResType = llvm::FixedVectorType::get(
  5610. llvm::Type::getInt32Ty(getVMContext()), Size / 32);
  5611. return ABIArgInfo::getDirect(ResType);
  5612. }
  5613. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  5614. }
  5615. ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty,
  5616. const Type *Base,
  5617. uint64_t Members) const {
  5618. assert(Base && "Base class should be set for homogeneous aggregate");
  5619. // Base can be a floating-point or a vector.
  5620. if (const VectorType *VT = Base->getAs<VectorType>()) {
  5621. // FP16 vectors should be converted to integer vectors
  5622. if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) {
  5623. uint64_t Size = getContext().getTypeSize(VT);
  5624. auto *NewVecTy = llvm::FixedVectorType::get(
  5625. llvm::Type::getInt32Ty(getVMContext()), Size / 32);
  5626. llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members);
  5627. return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
  5628. }
  5629. }
  5630. unsigned Align = 0;
  5631. if (getABIKind() == ARMABIInfo::AAPCS ||
  5632. getABIKind() == ARMABIInfo::AAPCS_VFP) {
  5633. // For alignment adjusted HFAs, cap the argument alignment to 8, leave it
  5634. // default otherwise.
  5635. Align = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
  5636. unsigned BaseAlign = getContext().getTypeAlignInChars(Base).getQuantity();
  5637. Align = (Align > BaseAlign && Align >= 8) ? 8 : 0;
  5638. }
  5639. return ABIArgInfo::getDirect(nullptr, 0, nullptr, false, Align);
  5640. }
  5641. ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic,
  5642. unsigned functionCallConv) const {
  5643. // 6.1.2.1 The following argument types are VFP CPRCs:
  5644. // A single-precision floating-point type (including promoted
  5645. // half-precision types); A double-precision floating-point type;
  5646. // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
  5647. // with a Base Type of a single- or double-precision floating-point type,
  5648. // 64-bit containerized vectors or 128-bit containerized vectors with one
  5649. // to four Elements.
  5650. // Variadic functions should always marshal to the base standard.
  5651. bool IsAAPCS_VFP =
  5652. !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false);
  5653. Ty = useFirstFieldIfTransparentUnion(Ty);
  5654. // Handle illegal vector types here.
  5655. if (isIllegalVectorType(Ty))
  5656. return coerceIllegalVector(Ty);
  5657. if (!isAggregateTypeForABI(Ty)) {
  5658. // Treat an enum type as its underlying type.
  5659. if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
  5660. Ty = EnumTy->getDecl()->getIntegerType();
  5661. }
  5662. if (const auto *EIT = Ty->getAs<BitIntType>())
  5663. if (EIT->getNumBits() > 64)
  5664. return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
  5665. return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
  5666. : ABIArgInfo::getDirect());
  5667. }
  5668. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  5669. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  5670. }
  5671. // Ignore empty records.
  5672. if (isEmptyRecord(getContext(), Ty, true))
  5673. return ABIArgInfo::getIgnore();
  5674. if (IsAAPCS_VFP) {
  5675. // Homogeneous Aggregates need to be expanded when we can fit the aggregate
  5676. // into VFP registers.
  5677. const Type *Base = nullptr;
  5678. uint64_t Members = 0;
  5679. if (isHomogeneousAggregate(Ty, Base, Members))
  5680. return classifyHomogeneousAggregate(Ty, Base, Members);
  5681. } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
  5682. // WatchOS does have homogeneous aggregates. Note that we intentionally use
  5683. // this convention even for a variadic function: the backend will use GPRs
  5684. // if needed.
  5685. const Type *Base = nullptr;
  5686. uint64_t Members = 0;
  5687. if (isHomogeneousAggregate(Ty, Base, Members)) {
  5688. assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
  5689. llvm::Type *Ty =
  5690. llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
  5691. return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
  5692. }
  5693. }
  5694. if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
  5695. getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
  5696. // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
  5697. // bigger than 128-bits, they get placed in space allocated by the caller,
  5698. // and a pointer is passed.
  5699. return ABIArgInfo::getIndirect(
  5700. CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
  5701. }
  5702. // Support byval for ARM.
  5703. // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
  5704. // most 8-byte. We realign the indirect argument if type alignment is bigger
  5705. // than ABI alignment.
  5706. uint64_t ABIAlign = 4;
  5707. uint64_t TyAlign;
  5708. if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
  5709. getABIKind() == ARMABIInfo::AAPCS) {
  5710. TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
  5711. ABIAlign = std::clamp(TyAlign, (uint64_t)4, (uint64_t)8);
  5712. } else {
  5713. TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
  5714. }
  5715. if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
  5716. assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
  5717. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
  5718. /*ByVal=*/true,
  5719. /*Realign=*/TyAlign > ABIAlign);
  5720. }
  5721. // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
  5722. // same size and alignment.
  5723. if (getTarget().isRenderScriptTarget()) {
  5724. return coerceToIntArray(Ty, getContext(), getVMContext());
  5725. }
  5726. // Otherwise, pass by coercing to a structure of the appropriate size.
  5727. llvm::Type* ElemTy;
  5728. unsigned SizeRegs;
  5729. // FIXME: Try to match the types of the arguments more accurately where
  5730. // we can.
  5731. if (TyAlign <= 4) {
  5732. ElemTy = llvm::Type::getInt32Ty(getVMContext());
  5733. SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
  5734. } else {
  5735. ElemTy = llvm::Type::getInt64Ty(getVMContext());
  5736. SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
  5737. }
  5738. return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
  5739. }
  5740. static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
  5741. llvm::LLVMContext &VMContext) {
  5742. // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
  5743. // is called integer-like if its size is less than or equal to one word, and
  5744. // the offset of each of its addressable sub-fields is zero.
  5745. uint64_t Size = Context.getTypeSize(Ty);
  5746. // Check that the type fits in a word.
  5747. if (Size > 32)
  5748. return false;
  5749. // FIXME: Handle vector types!
  5750. if (Ty->isVectorType())
  5751. return false;
  5752. // Float types are never treated as "integer like".
  5753. if (Ty->isRealFloatingType())
  5754. return false;
  5755. // If this is a builtin or pointer type then it is ok.
  5756. if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
  5757. return true;
  5758. // Small complex integer types are "integer like".
  5759. if (const ComplexType *CT = Ty->getAs<ComplexType>())
  5760. return isIntegerLikeType(CT->getElementType(), Context, VMContext);
  5761. // Single element and zero sized arrays should be allowed, by the definition
  5762. // above, but they are not.
  5763. // Otherwise, it must be a record type.
  5764. const RecordType *RT = Ty->getAs<RecordType>();
  5765. if (!RT) return false;
  5766. // Ignore records with flexible arrays.
  5767. const RecordDecl *RD = RT->getDecl();
  5768. if (RD->hasFlexibleArrayMember())
  5769. return false;
  5770. // Check that all sub-fields are at offset 0, and are themselves "integer
  5771. // like".
  5772. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  5773. bool HadField = false;
  5774. unsigned idx = 0;
  5775. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  5776. i != e; ++i, ++idx) {
  5777. const FieldDecl *FD = *i;
  5778. // Bit-fields are not addressable, we only need to verify they are "integer
  5779. // like". We still have to disallow a subsequent non-bitfield, for example:
  5780. // struct { int : 0; int x }
  5781. // is non-integer like according to gcc.
  5782. if (FD->isBitField()) {
  5783. if (!RD->isUnion())
  5784. HadField = true;
  5785. if (!isIntegerLikeType(FD->getType(), Context, VMContext))
  5786. return false;
  5787. continue;
  5788. }
  5789. // Check if this field is at offset 0.
  5790. if (Layout.getFieldOffset(idx) != 0)
  5791. return false;
  5792. if (!isIntegerLikeType(FD->getType(), Context, VMContext))
  5793. return false;
  5794. // Only allow at most one field in a structure. This doesn't match the
  5795. // wording above, but follows gcc in situations with a field following an
  5796. // empty structure.
  5797. if (!RD->isUnion()) {
  5798. if (HadField)
  5799. return false;
  5800. HadField = true;
  5801. }
  5802. }
  5803. return true;
  5804. }
  5805. ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic,
  5806. unsigned functionCallConv) const {
  5807. // Variadic functions should always marshal to the base standard.
  5808. bool IsAAPCS_VFP =
  5809. !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true);
  5810. if (RetTy->isVoidType())
  5811. return ABIArgInfo::getIgnore();
  5812. if (const VectorType *VT = RetTy->getAs<VectorType>()) {
  5813. // Large vector types should be returned via memory.
  5814. if (getContext().getTypeSize(RetTy) > 128)
  5815. return getNaturalAlignIndirect(RetTy);
  5816. // TODO: FP16/BF16 vectors should be converted to integer vectors
  5817. // This check is similar to isIllegalVectorType - refactor?
  5818. if ((!getTarget().hasLegalHalfType() &&
  5819. (VT->getElementType()->isFloat16Type() ||
  5820. VT->getElementType()->isHalfType())) ||
  5821. (IsFloatABISoftFP &&
  5822. VT->getElementType()->isBFloat16Type()))
  5823. return coerceIllegalVector(RetTy);
  5824. }
  5825. if (!isAggregateTypeForABI(RetTy)) {
  5826. // Treat an enum type as its underlying type.
  5827. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  5828. RetTy = EnumTy->getDecl()->getIntegerType();
  5829. if (const auto *EIT = RetTy->getAs<BitIntType>())
  5830. if (EIT->getNumBits() > 64)
  5831. return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
  5832. return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
  5833. : ABIArgInfo::getDirect();
  5834. }
  5835. // Are we following APCS?
  5836. if (getABIKind() == APCS) {
  5837. if (isEmptyRecord(getContext(), RetTy, false))
  5838. return ABIArgInfo::getIgnore();
  5839. // Complex types are all returned as packed integers.
  5840. //
  5841. // FIXME: Consider using 2 x vector types if the back end handles them
  5842. // correctly.
  5843. if (RetTy->isAnyComplexType())
  5844. return ABIArgInfo::getDirect(llvm::IntegerType::get(
  5845. getVMContext(), getContext().getTypeSize(RetTy)));
  5846. // Integer like structures are returned in r0.
  5847. if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
  5848. // Return in the smallest viable integer type.
  5849. uint64_t Size = getContext().getTypeSize(RetTy);
  5850. if (Size <= 8)
  5851. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  5852. if (Size <= 16)
  5853. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  5854. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  5855. }
  5856. // Otherwise return in memory.
  5857. return getNaturalAlignIndirect(RetTy);
  5858. }
  5859. // Otherwise this is an AAPCS variant.
  5860. if (isEmptyRecord(getContext(), RetTy, true))
  5861. return ABIArgInfo::getIgnore();
  5862. // Check for homogeneous aggregates with AAPCS-VFP.
  5863. if (IsAAPCS_VFP) {
  5864. const Type *Base = nullptr;
  5865. uint64_t Members = 0;
  5866. if (isHomogeneousAggregate(RetTy, Base, Members))
  5867. return classifyHomogeneousAggregate(RetTy, Base, Members);
  5868. }
  5869. // Aggregates <= 4 bytes are returned in r0; other aggregates
  5870. // are returned indirectly.
  5871. uint64_t Size = getContext().getTypeSize(RetTy);
  5872. if (Size <= 32) {
  5873. // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
  5874. // same size and alignment.
  5875. if (getTarget().isRenderScriptTarget()) {
  5876. return coerceToIntArray(RetTy, getContext(), getVMContext());
  5877. }
  5878. if (getDataLayout().isBigEndian())
  5879. // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
  5880. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  5881. // Return in the smallest viable integer type.
  5882. if (Size <= 8)
  5883. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  5884. if (Size <= 16)
  5885. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  5886. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  5887. } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
  5888. llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
  5889. llvm::Type *CoerceTy =
  5890. llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
  5891. return ABIArgInfo::getDirect(CoerceTy);
  5892. }
  5893. return getNaturalAlignIndirect(RetTy);
  5894. }
  5895. /// isIllegalVector - check whether Ty is an illegal vector type.
  5896. bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
  5897. if (const VectorType *VT = Ty->getAs<VectorType> ()) {
  5898. // On targets that don't support half, fp16 or bfloat, they are expanded
  5899. // into float, and we don't want the ABI to depend on whether or not they
  5900. // are supported in hardware. Thus return false to coerce vectors of these
  5901. // types into integer vectors.
  5902. // We do not depend on hasLegalHalfType for bfloat as it is a
  5903. // separate IR type.
  5904. if ((!getTarget().hasLegalHalfType() &&
  5905. (VT->getElementType()->isFloat16Type() ||
  5906. VT->getElementType()->isHalfType())) ||
  5907. (IsFloatABISoftFP &&
  5908. VT->getElementType()->isBFloat16Type()))
  5909. return true;
  5910. if (isAndroid()) {
  5911. // Android shipped using Clang 3.1, which supported a slightly different
  5912. // vector ABI. The primary differences were that 3-element vector types
  5913. // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
  5914. // accepts that legacy behavior for Android only.
  5915. // Check whether VT is legal.
  5916. unsigned NumElements = VT->getNumElements();
  5917. // NumElements should be power of 2 or equal to 3.
  5918. if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
  5919. return true;
  5920. } else {
  5921. // Check whether VT is legal.
  5922. unsigned NumElements = VT->getNumElements();
  5923. uint64_t Size = getContext().getTypeSize(VT);
  5924. // NumElements should be power of 2.
  5925. if (!llvm::isPowerOf2_32(NumElements))
  5926. return true;
  5927. // Size should be greater than 32 bits.
  5928. return Size <= 32;
  5929. }
  5930. }
  5931. return false;
  5932. }
  5933. /// Return true if a type contains any 16-bit floating point vectors
  5934. bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const {
  5935. if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
  5936. uint64_t NElements = AT->getSize().getZExtValue();
  5937. if (NElements == 0)
  5938. return false;
  5939. return containsAnyFP16Vectors(AT->getElementType());
  5940. } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
  5941. const RecordDecl *RD = RT->getDecl();
  5942. // If this is a C++ record, check the bases first.
  5943. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  5944. if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) {
  5945. return containsAnyFP16Vectors(B.getType());
  5946. }))
  5947. return true;
  5948. if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) {
  5949. return FD && containsAnyFP16Vectors(FD->getType());
  5950. }))
  5951. return true;
  5952. return false;
  5953. } else {
  5954. if (const VectorType *VT = Ty->getAs<VectorType>())
  5955. return (VT->getElementType()->isFloat16Type() ||
  5956. VT->getElementType()->isBFloat16Type() ||
  5957. VT->getElementType()->isHalfType());
  5958. return false;
  5959. }
  5960. }
  5961. bool ARMSwiftABIInfo::isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy,
  5962. unsigned NumElts) const {
  5963. if (!llvm::isPowerOf2_32(NumElts))
  5964. return false;
  5965. unsigned size = CGT.getDataLayout().getTypeStoreSizeInBits(EltTy);
  5966. if (size > 64)
  5967. return false;
  5968. if (VectorSize.getQuantity() != 8 &&
  5969. (VectorSize.getQuantity() != 16 || NumElts == 1))
  5970. return false;
  5971. return true;
  5972. }
  5973. bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  5974. // Homogeneous aggregates for AAPCS-VFP must have base types of float,
  5975. // double, or 64-bit or 128-bit vectors.
  5976. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  5977. if (BT->getKind() == BuiltinType::Float ||
  5978. BT->getKind() == BuiltinType::Double ||
  5979. BT->getKind() == BuiltinType::LongDouble)
  5980. return true;
  5981. } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
  5982. unsigned VecSize = getContext().getTypeSize(VT);
  5983. if (VecSize == 64 || VecSize == 128)
  5984. return true;
  5985. }
  5986. return false;
  5987. }
  5988. bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
  5989. uint64_t Members) const {
  5990. return Members <= 4;
  5991. }
  5992. bool ARMABIInfo::isZeroLengthBitfieldPermittedInHomogeneousAggregate() const {
  5993. // AAPCS32 says that the rule for whether something is a homogeneous
  5994. // aggregate is applied to the output of the data layout decision. So
  5995. // anything that doesn't affect the data layout also does not affect
  5996. // homogeneity. In particular, zero-length bitfields don't stop a struct
  5997. // being homogeneous.
  5998. return true;
  5999. }
  6000. bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention,
  6001. bool acceptHalf) const {
  6002. // Give precedence to user-specified calling conventions.
  6003. if (callConvention != llvm::CallingConv::C)
  6004. return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP);
  6005. else
  6006. return (getABIKind() == AAPCS_VFP) ||
  6007. (acceptHalf && (getABIKind() == AAPCS16_VFP));
  6008. }
  6009. Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6010. QualType Ty) const {
  6011. CharUnits SlotSize = CharUnits::fromQuantity(4);
  6012. // Empty records are ignored for parameter passing purposes.
  6013. if (isEmptyRecord(getContext(), Ty, true)) {
  6014. VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
  6015. auto *Load = CGF.Builder.CreateLoad(VAListAddr);
  6016. Address Addr = Address(Load, CGF.Int8Ty, SlotSize);
  6017. return CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
  6018. }
  6019. CharUnits TySize = getContext().getTypeSizeInChars(Ty);
  6020. CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty);
  6021. // Use indirect if size of the illegal vector is bigger than 16 bytes.
  6022. bool IsIndirect = false;
  6023. const Type *Base = nullptr;
  6024. uint64_t Members = 0;
  6025. if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
  6026. IsIndirect = true;
  6027. // ARMv7k passes structs bigger than 16 bytes indirectly, in space
  6028. // allocated by the caller.
  6029. } else if (TySize > CharUnits::fromQuantity(16) &&
  6030. getABIKind() == ARMABIInfo::AAPCS16_VFP &&
  6031. !isHomogeneousAggregate(Ty, Base, Members)) {
  6032. IsIndirect = true;
  6033. // Otherwise, bound the type's ABI alignment.
  6034. // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
  6035. // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
  6036. // Our callers should be prepared to handle an under-aligned address.
  6037. } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
  6038. getABIKind() == ARMABIInfo::AAPCS) {
  6039. TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
  6040. TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
  6041. } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
  6042. // ARMv7k allows type alignment up to 16 bytes.
  6043. TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
  6044. TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
  6045. } else {
  6046. TyAlignForABI = CharUnits::fromQuantity(4);
  6047. }
  6048. TypeInfoChars TyInfo(TySize, TyAlignForABI, AlignRequirementKind::None);
  6049. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
  6050. SlotSize, /*AllowHigherAlign*/ true);
  6051. }
  6052. //===----------------------------------------------------------------------===//
  6053. // NVPTX ABI Implementation
  6054. //===----------------------------------------------------------------------===//
  6055. namespace {
  6056. class NVPTXTargetCodeGenInfo;
  6057. class NVPTXABIInfo : public ABIInfo {
  6058. NVPTXTargetCodeGenInfo &CGInfo;
  6059. public:
  6060. NVPTXABIInfo(CodeGenTypes &CGT, NVPTXTargetCodeGenInfo &Info)
  6061. : ABIInfo(CGT), CGInfo(Info) {}
  6062. ABIArgInfo classifyReturnType(QualType RetTy) const;
  6063. ABIArgInfo classifyArgumentType(QualType Ty) const;
  6064. void computeInfo(CGFunctionInfo &FI) const override;
  6065. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6066. QualType Ty) const override;
  6067. bool isUnsupportedType(QualType T) const;
  6068. ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, unsigned MaxSize) const;
  6069. };
  6070. class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
  6071. public:
  6072. NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
  6073. : TargetCodeGenInfo(std::make_unique<NVPTXABIInfo>(CGT, *this)) {}
  6074. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  6075. CodeGen::CodeGenModule &M) const override;
  6076. bool shouldEmitStaticExternCAliases() const override;
  6077. llvm::Type *getCUDADeviceBuiltinSurfaceDeviceType() const override {
  6078. // On the device side, surface reference is represented as an object handle
  6079. // in 64-bit integer.
  6080. return llvm::Type::getInt64Ty(getABIInfo().getVMContext());
  6081. }
  6082. llvm::Type *getCUDADeviceBuiltinTextureDeviceType() const override {
  6083. // On the device side, texture reference is represented as an object handle
  6084. // in 64-bit integer.
  6085. return llvm::Type::getInt64Ty(getABIInfo().getVMContext());
  6086. }
  6087. bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF, LValue Dst,
  6088. LValue Src) const override {
  6089. emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
  6090. return true;
  6091. }
  6092. bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF, LValue Dst,
  6093. LValue Src) const override {
  6094. emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
  6095. return true;
  6096. }
  6097. private:
  6098. // Adds a NamedMDNode with GV, Name, and Operand as operands, and adds the
  6099. // resulting MDNode to the nvvm.annotations MDNode.
  6100. static void addNVVMMetadata(llvm::GlobalValue *GV, StringRef Name,
  6101. int Operand);
  6102. static void emitBuiltinSurfTexDeviceCopy(CodeGenFunction &CGF, LValue Dst,
  6103. LValue Src) {
  6104. llvm::Value *Handle = nullptr;
  6105. llvm::Constant *C =
  6106. llvm::dyn_cast<llvm::Constant>(Src.getAddress(CGF).getPointer());
  6107. // Lookup `addrspacecast` through the constant pointer if any.
  6108. if (auto *ASC = llvm::dyn_cast_or_null<llvm::AddrSpaceCastOperator>(C))
  6109. C = llvm::cast<llvm::Constant>(ASC->getPointerOperand());
  6110. if (auto *GV = llvm::dyn_cast_or_null<llvm::GlobalVariable>(C)) {
  6111. // Load the handle from the specific global variable using
  6112. // `nvvm.texsurf.handle.internal` intrinsic.
  6113. Handle = CGF.EmitRuntimeCall(
  6114. CGF.CGM.getIntrinsic(llvm::Intrinsic::nvvm_texsurf_handle_internal,
  6115. {GV->getType()}),
  6116. {GV}, "texsurf_handle");
  6117. } else
  6118. Handle = CGF.EmitLoadOfScalar(Src, SourceLocation());
  6119. CGF.EmitStoreOfScalar(Handle, Dst);
  6120. }
  6121. };
  6122. /// Checks if the type is unsupported directly by the current target.
  6123. bool NVPTXABIInfo::isUnsupportedType(QualType T) const {
  6124. ASTContext &Context = getContext();
  6125. if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type())
  6126. return true;
  6127. if (!Context.getTargetInfo().hasFloat128Type() &&
  6128. (T->isFloat128Type() ||
  6129. (T->isRealFloatingType() && Context.getTypeSize(T) == 128)))
  6130. return true;
  6131. if (const auto *EIT = T->getAs<BitIntType>())
  6132. return EIT->getNumBits() >
  6133. (Context.getTargetInfo().hasInt128Type() ? 128U : 64U);
  6134. if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() &&
  6135. Context.getTypeSize(T) > 64U)
  6136. return true;
  6137. if (const auto *AT = T->getAsArrayTypeUnsafe())
  6138. return isUnsupportedType(AT->getElementType());
  6139. const auto *RT = T->getAs<RecordType>();
  6140. if (!RT)
  6141. return false;
  6142. const RecordDecl *RD = RT->getDecl();
  6143. // If this is a C++ record, check the bases first.
  6144. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  6145. for (const CXXBaseSpecifier &I : CXXRD->bases())
  6146. if (isUnsupportedType(I.getType()))
  6147. return true;
  6148. for (const FieldDecl *I : RD->fields())
  6149. if (isUnsupportedType(I->getType()))
  6150. return true;
  6151. return false;
  6152. }
  6153. /// Coerce the given type into an array with maximum allowed size of elements.
  6154. ABIArgInfo NVPTXABIInfo::coerceToIntArrayWithLimit(QualType Ty,
  6155. unsigned MaxSize) const {
  6156. // Alignment and Size are measured in bits.
  6157. const uint64_t Size = getContext().getTypeSize(Ty);
  6158. const uint64_t Alignment = getContext().getTypeAlign(Ty);
  6159. const unsigned Div = std::min<unsigned>(MaxSize, Alignment);
  6160. llvm::Type *IntType = llvm::Type::getIntNTy(getVMContext(), Div);
  6161. const uint64_t NumElements = (Size + Div - 1) / Div;
  6162. return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
  6163. }
  6164. ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
  6165. if (RetTy->isVoidType())
  6166. return ABIArgInfo::getIgnore();
  6167. if (getContext().getLangOpts().OpenMP &&
  6168. getContext().getLangOpts().OpenMPIsDevice && isUnsupportedType(RetTy))
  6169. return coerceToIntArrayWithLimit(RetTy, 64);
  6170. // note: this is different from default ABI
  6171. if (!RetTy->isScalarType())
  6172. return ABIArgInfo::getDirect();
  6173. // Treat an enum type as its underlying type.
  6174. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  6175. RetTy = EnumTy->getDecl()->getIntegerType();
  6176. return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
  6177. : ABIArgInfo::getDirect());
  6178. }
  6179. ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
  6180. // Treat an enum type as its underlying type.
  6181. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  6182. Ty = EnumTy->getDecl()->getIntegerType();
  6183. // Return aggregates type as indirect by value
  6184. if (isAggregateTypeForABI(Ty)) {
  6185. // Under CUDA device compilation, tex/surf builtin types are replaced with
  6186. // object types and passed directly.
  6187. if (getContext().getLangOpts().CUDAIsDevice) {
  6188. if (Ty->isCUDADeviceBuiltinSurfaceType())
  6189. return ABIArgInfo::getDirect(
  6190. CGInfo.getCUDADeviceBuiltinSurfaceDeviceType());
  6191. if (Ty->isCUDADeviceBuiltinTextureType())
  6192. return ABIArgInfo::getDirect(
  6193. CGInfo.getCUDADeviceBuiltinTextureDeviceType());
  6194. }
  6195. return getNaturalAlignIndirect(Ty, /* byval */ true);
  6196. }
  6197. if (const auto *EIT = Ty->getAs<BitIntType>()) {
  6198. if ((EIT->getNumBits() > 128) ||
  6199. (!getContext().getTargetInfo().hasInt128Type() &&
  6200. EIT->getNumBits() > 64))
  6201. return getNaturalAlignIndirect(Ty, /* byval */ true);
  6202. }
  6203. return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
  6204. : ABIArgInfo::getDirect());
  6205. }
  6206. void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
  6207. if (!getCXXABI().classifyReturnType(FI))
  6208. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  6209. for (auto &I : FI.arguments())
  6210. I.info = classifyArgumentType(I.type);
  6211. // Always honor user-specified calling convention.
  6212. if (FI.getCallingConvention() != llvm::CallingConv::C)
  6213. return;
  6214. FI.setEffectiveCallingConvention(getRuntimeCC());
  6215. }
  6216. Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6217. QualType Ty) const {
  6218. llvm_unreachable("NVPTX does not support varargs");
  6219. }
  6220. void NVPTXTargetCodeGenInfo::setTargetAttributes(
  6221. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
  6222. if (GV->isDeclaration())
  6223. return;
  6224. const VarDecl *VD = dyn_cast_or_null<VarDecl>(D);
  6225. if (VD) {
  6226. if (M.getLangOpts().CUDA) {
  6227. if (VD->getType()->isCUDADeviceBuiltinSurfaceType())
  6228. addNVVMMetadata(GV, "surface", 1);
  6229. else if (VD->getType()->isCUDADeviceBuiltinTextureType())
  6230. addNVVMMetadata(GV, "texture", 1);
  6231. return;
  6232. }
  6233. }
  6234. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  6235. if (!FD) return;
  6236. llvm::Function *F = cast<llvm::Function>(GV);
  6237. // Perform special handling in OpenCL mode
  6238. if (M.getLangOpts().OpenCL) {
  6239. // Use OpenCL function attributes to check for kernel functions
  6240. // By default, all functions are device functions
  6241. if (FD->hasAttr<OpenCLKernelAttr>()) {
  6242. // OpenCL __kernel functions get kernel metadata
  6243. // Create !{<func-ref>, metadata !"kernel", i32 1} node
  6244. addNVVMMetadata(F, "kernel", 1);
  6245. // And kernel functions are not subject to inlining
  6246. F->addFnAttr(llvm::Attribute::NoInline);
  6247. }
  6248. }
  6249. // Perform special handling in CUDA mode.
  6250. if (M.getLangOpts().CUDA) {
  6251. // CUDA __global__ functions get a kernel metadata entry. Since
  6252. // __global__ functions cannot be called from the device, we do not
  6253. // need to set the noinline attribute.
  6254. if (FD->hasAttr<CUDAGlobalAttr>()) {
  6255. // Create !{<func-ref>, metadata !"kernel", i32 1} node
  6256. addNVVMMetadata(F, "kernel", 1);
  6257. }
  6258. if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
  6259. // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
  6260. llvm::APSInt MaxThreads(32);
  6261. MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
  6262. if (MaxThreads > 0)
  6263. addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
  6264. // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
  6265. // not specified in __launch_bounds__ or if the user specified a 0 value,
  6266. // we don't have to add a PTX directive.
  6267. if (Attr->getMinBlocks()) {
  6268. llvm::APSInt MinBlocks(32);
  6269. MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
  6270. if (MinBlocks > 0)
  6271. // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
  6272. addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
  6273. }
  6274. }
  6275. }
  6276. }
  6277. void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::GlobalValue *GV,
  6278. StringRef Name, int Operand) {
  6279. llvm::Module *M = GV->getParent();
  6280. llvm::LLVMContext &Ctx = M->getContext();
  6281. // Get "nvvm.annotations" metadata node
  6282. llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
  6283. llvm::Metadata *MDVals[] = {
  6284. llvm::ConstantAsMetadata::get(GV), llvm::MDString::get(Ctx, Name),
  6285. llvm::ConstantAsMetadata::get(
  6286. llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
  6287. // Append metadata to nvvm.annotations
  6288. MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
  6289. }
  6290. bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
  6291. return false;
  6292. }
  6293. }
  6294. //===----------------------------------------------------------------------===//
  6295. // SystemZ ABI Implementation
  6296. //===----------------------------------------------------------------------===//
  6297. namespace {
  6298. class SystemZABIInfo : public ABIInfo {
  6299. bool HasVector;
  6300. bool IsSoftFloatABI;
  6301. public:
  6302. SystemZABIInfo(CodeGenTypes &CGT, bool HV, bool SF)
  6303. : ABIInfo(CGT), HasVector(HV), IsSoftFloatABI(SF) {}
  6304. bool isPromotableIntegerTypeForABI(QualType Ty) const;
  6305. bool isCompoundType(QualType Ty) const;
  6306. bool isVectorArgumentType(QualType Ty) const;
  6307. bool isFPArgumentType(QualType Ty) const;
  6308. QualType GetSingleElementType(QualType Ty) const;
  6309. ABIArgInfo classifyReturnType(QualType RetTy) const;
  6310. ABIArgInfo classifyArgumentType(QualType ArgTy) const;
  6311. void computeInfo(CGFunctionInfo &FI) const override;
  6312. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6313. QualType Ty) const override;
  6314. };
  6315. class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
  6316. ASTContext &Ctx;
  6317. const SystemZABIInfo &getABIInfo() const {
  6318. return static_cast<const SystemZABIInfo&>(TargetCodeGenInfo::getABIInfo());
  6319. }
  6320. // These are used for speeding up the search for a visible vector ABI.
  6321. mutable bool HasVisibleVecABIFlag = false;
  6322. mutable std::set<const Type *> SeenTypes;
  6323. // Returns true (the first time) if Ty is, or is found to include, a vector
  6324. // type that exposes the vector ABI. This is any vector >=16 bytes which
  6325. // with vector support are aligned to only 8 bytes. When IsParam is true,
  6326. // the type belongs to a value as passed between functions. If it is a
  6327. // vector <=16 bytes it will be passed in a vector register (if supported).
  6328. bool isVectorTypeBased(const Type *Ty, bool IsParam) const;
  6329. public:
  6330. SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector, bool SoftFloatABI)
  6331. : TargetCodeGenInfo(
  6332. std::make_unique<SystemZABIInfo>(CGT, HasVector, SoftFloatABI)),
  6333. Ctx(CGT.getContext()) {
  6334. SwiftInfo =
  6335. std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false);
  6336. }
  6337. // The vector ABI is different when the vector facility is present and when
  6338. // a module e.g. defines an externally visible vector variable, a flag
  6339. // indicating a visible vector ABI is added. Eventually this will result in
  6340. // a GNU attribute indicating the vector ABI of the module. Ty is the type
  6341. // of a variable or function parameter that is globally visible.
  6342. void handleExternallyVisibleObjABI(const Type *Ty, CodeGen::CodeGenModule &M,
  6343. bool IsParam) const {
  6344. if (!HasVisibleVecABIFlag && isVectorTypeBased(Ty, IsParam)) {
  6345. M.getModule().addModuleFlag(llvm::Module::Warning,
  6346. "s390x-visible-vector-ABI", 1);
  6347. HasVisibleVecABIFlag = true;
  6348. }
  6349. }
  6350. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  6351. CodeGen::CodeGenModule &M) const override {
  6352. if (!D)
  6353. return;
  6354. // Check if the vector ABI becomes visible by an externally visible
  6355. // variable or function.
  6356. if (const auto *VD = dyn_cast<VarDecl>(D)) {
  6357. if (VD->isExternallyVisible())
  6358. handleExternallyVisibleObjABI(VD->getType().getTypePtr(), M,
  6359. /*IsParam*/false);
  6360. }
  6361. else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  6362. if (FD->isExternallyVisible())
  6363. handleExternallyVisibleObjABI(FD->getType().getTypePtr(), M,
  6364. /*IsParam*/false);
  6365. }
  6366. }
  6367. llvm::Value *testFPKind(llvm::Value *V, unsigned BuiltinID,
  6368. CGBuilderTy &Builder,
  6369. CodeGenModule &CGM) const override {
  6370. assert(V->getType()->isFloatingPointTy() && "V should have an FP type.");
  6371. // Only use TDC in constrained FP mode.
  6372. if (!Builder.getIsFPConstrained())
  6373. return nullptr;
  6374. llvm::Type *Ty = V->getType();
  6375. if (Ty->isFloatTy() || Ty->isDoubleTy() || Ty->isFP128Ty()) {
  6376. llvm::Module &M = CGM.getModule();
  6377. auto &Ctx = M.getContext();
  6378. llvm::Function *TDCFunc =
  6379. llvm::Intrinsic::getDeclaration(&M, llvm::Intrinsic::s390_tdc, Ty);
  6380. unsigned TDCBits = 0;
  6381. switch (BuiltinID) {
  6382. case Builtin::BI__builtin_isnan:
  6383. TDCBits = 0xf;
  6384. break;
  6385. case Builtin::BIfinite:
  6386. case Builtin::BI__finite:
  6387. case Builtin::BIfinitef:
  6388. case Builtin::BI__finitef:
  6389. case Builtin::BIfinitel:
  6390. case Builtin::BI__finitel:
  6391. case Builtin::BI__builtin_isfinite:
  6392. TDCBits = 0xfc0;
  6393. break;
  6394. case Builtin::BI__builtin_isinf:
  6395. TDCBits = 0x30;
  6396. break;
  6397. default:
  6398. break;
  6399. }
  6400. if (TDCBits)
  6401. return Builder.CreateCall(
  6402. TDCFunc,
  6403. {V, llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), TDCBits)});
  6404. }
  6405. return nullptr;
  6406. }
  6407. };
  6408. }
  6409. bool SystemZABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
  6410. // Treat an enum type as its underlying type.
  6411. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  6412. Ty = EnumTy->getDecl()->getIntegerType();
  6413. // Promotable integer types are required to be promoted by the ABI.
  6414. if (ABIInfo::isPromotableIntegerTypeForABI(Ty))
  6415. return true;
  6416. if (const auto *EIT = Ty->getAs<BitIntType>())
  6417. if (EIT->getNumBits() < 64)
  6418. return true;
  6419. // 32-bit values must also be promoted.
  6420. if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
  6421. switch (BT->getKind()) {
  6422. case BuiltinType::Int:
  6423. case BuiltinType::UInt:
  6424. return true;
  6425. default:
  6426. return false;
  6427. }
  6428. return false;
  6429. }
  6430. bool SystemZABIInfo::isCompoundType(QualType Ty) const {
  6431. return (Ty->isAnyComplexType() ||
  6432. Ty->isVectorType() ||
  6433. isAggregateTypeForABI(Ty));
  6434. }
  6435. bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
  6436. return (HasVector &&
  6437. Ty->isVectorType() &&
  6438. getContext().getTypeSize(Ty) <= 128);
  6439. }
  6440. bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
  6441. if (IsSoftFloatABI)
  6442. return false;
  6443. if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
  6444. switch (BT->getKind()) {
  6445. case BuiltinType::Float:
  6446. case BuiltinType::Double:
  6447. return true;
  6448. default:
  6449. return false;
  6450. }
  6451. return false;
  6452. }
  6453. QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
  6454. const RecordType *RT = Ty->getAs<RecordType>();
  6455. if (RT && RT->isStructureOrClassType()) {
  6456. const RecordDecl *RD = RT->getDecl();
  6457. QualType Found;
  6458. // If this is a C++ record, check the bases first.
  6459. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  6460. if (CXXRD->hasDefinition())
  6461. for (const auto &I : CXXRD->bases()) {
  6462. QualType Base = I.getType();
  6463. // Empty bases don't affect things either way.
  6464. if (isEmptyRecord(getContext(), Base, true))
  6465. continue;
  6466. if (!Found.isNull())
  6467. return Ty;
  6468. Found = GetSingleElementType(Base);
  6469. }
  6470. // Check the fields.
  6471. for (const auto *FD : RD->fields()) {
  6472. // Unlike isSingleElementStruct(), empty structure and array fields
  6473. // do count. So do anonymous bitfields that aren't zero-sized.
  6474. // Like isSingleElementStruct(), ignore C++20 empty data members.
  6475. if (FD->hasAttr<NoUniqueAddressAttr>() &&
  6476. isEmptyRecord(getContext(), FD->getType(), true))
  6477. continue;
  6478. // Unlike isSingleElementStruct(), arrays do not count.
  6479. // Nested structures still do though.
  6480. if (!Found.isNull())
  6481. return Ty;
  6482. Found = GetSingleElementType(FD->getType());
  6483. }
  6484. // Unlike isSingleElementStruct(), trailing padding is allowed.
  6485. // An 8-byte aligned struct s { float f; } is passed as a double.
  6486. if (!Found.isNull())
  6487. return Found;
  6488. }
  6489. return Ty;
  6490. }
  6491. Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6492. QualType Ty) const {
  6493. // Assume that va_list type is correct; should be pointer to LLVM type:
  6494. // struct {
  6495. // i64 __gpr;
  6496. // i64 __fpr;
  6497. // i8 *__overflow_arg_area;
  6498. // i8 *__reg_save_area;
  6499. // };
  6500. // Every non-vector argument occupies 8 bytes and is passed by preference
  6501. // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are
  6502. // always passed on the stack.
  6503. const SystemZTargetCodeGenInfo &SZCGI =
  6504. static_cast<const SystemZTargetCodeGenInfo &>(
  6505. CGT.getCGM().getTargetCodeGenInfo());
  6506. Ty = getContext().getCanonicalType(Ty);
  6507. auto TyInfo = getContext().getTypeInfoInChars(Ty);
  6508. llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
  6509. llvm::Type *DirectTy = ArgTy;
  6510. ABIArgInfo AI = classifyArgumentType(Ty);
  6511. bool IsIndirect = AI.isIndirect();
  6512. bool InFPRs = false;
  6513. bool IsVector = false;
  6514. CharUnits UnpaddedSize;
  6515. CharUnits DirectAlign;
  6516. SZCGI.handleExternallyVisibleObjABI(Ty.getTypePtr(), CGT.getCGM(),
  6517. /*IsParam*/true);
  6518. if (IsIndirect) {
  6519. DirectTy = llvm::PointerType::getUnqual(DirectTy);
  6520. UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
  6521. } else {
  6522. if (AI.getCoerceToType())
  6523. ArgTy = AI.getCoerceToType();
  6524. InFPRs = (!IsSoftFloatABI && (ArgTy->isFloatTy() || ArgTy->isDoubleTy()));
  6525. IsVector = ArgTy->isVectorTy();
  6526. UnpaddedSize = TyInfo.Width;
  6527. DirectAlign = TyInfo.Align;
  6528. }
  6529. CharUnits PaddedSize = CharUnits::fromQuantity(8);
  6530. if (IsVector && UnpaddedSize > PaddedSize)
  6531. PaddedSize = CharUnits::fromQuantity(16);
  6532. assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
  6533. CharUnits Padding = (PaddedSize - UnpaddedSize);
  6534. llvm::Type *IndexTy = CGF.Int64Ty;
  6535. llvm::Value *PaddedSizeV =
  6536. llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
  6537. if (IsVector) {
  6538. // Work out the address of a vector argument on the stack.
  6539. // Vector arguments are always passed in the high bits of a
  6540. // single (8 byte) or double (16 byte) stack slot.
  6541. Address OverflowArgAreaPtr =
  6542. CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
  6543. Address OverflowArgArea =
  6544. Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
  6545. CGF.Int8Ty, TyInfo.Align);
  6546. Address MemAddr =
  6547. CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
  6548. // Update overflow_arg_area_ptr pointer
  6549. llvm::Value *NewOverflowArgArea = CGF.Builder.CreateGEP(
  6550. OverflowArgArea.getElementType(), OverflowArgArea.getPointer(),
  6551. PaddedSizeV, "overflow_arg_area");
  6552. CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
  6553. return MemAddr;
  6554. }
  6555. assert(PaddedSize.getQuantity() == 8);
  6556. unsigned MaxRegs, RegCountField, RegSaveIndex;
  6557. CharUnits RegPadding;
  6558. if (InFPRs) {
  6559. MaxRegs = 4; // Maximum of 4 FPR arguments
  6560. RegCountField = 1; // __fpr
  6561. RegSaveIndex = 16; // save offset for f0
  6562. RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
  6563. } else {
  6564. MaxRegs = 5; // Maximum of 5 GPR arguments
  6565. RegCountField = 0; // __gpr
  6566. RegSaveIndex = 2; // save offset for r2
  6567. RegPadding = Padding; // values are passed in the low bits of a GPR
  6568. }
  6569. Address RegCountPtr =
  6570. CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
  6571. llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
  6572. llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
  6573. llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
  6574. "fits_in_regs");
  6575. llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
  6576. llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
  6577. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
  6578. CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
  6579. // Emit code to load the value if it was passed in registers.
  6580. CGF.EmitBlock(InRegBlock);
  6581. // Work out the address of an argument register.
  6582. llvm::Value *ScaledRegCount =
  6583. CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
  6584. llvm::Value *RegBase =
  6585. llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
  6586. + RegPadding.getQuantity());
  6587. llvm::Value *RegOffset =
  6588. CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
  6589. Address RegSaveAreaPtr =
  6590. CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
  6591. llvm::Value *RegSaveArea =
  6592. CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
  6593. Address RawRegAddr(
  6594. CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, RegOffset, "raw_reg_addr"),
  6595. CGF.Int8Ty, PaddedSize);
  6596. Address RegAddr =
  6597. CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
  6598. // Update the register count
  6599. llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
  6600. llvm::Value *NewRegCount =
  6601. CGF.Builder.CreateAdd(RegCount, One, "reg_count");
  6602. CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
  6603. CGF.EmitBranch(ContBlock);
  6604. // Emit code to load the value if it was passed in memory.
  6605. CGF.EmitBlock(InMemBlock);
  6606. // Work out the address of a stack argument.
  6607. Address OverflowArgAreaPtr =
  6608. CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
  6609. Address OverflowArgArea =
  6610. Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
  6611. CGF.Int8Ty, PaddedSize);
  6612. Address RawMemAddr =
  6613. CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
  6614. Address MemAddr =
  6615. CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
  6616. // Update overflow_arg_area_ptr pointer
  6617. llvm::Value *NewOverflowArgArea =
  6618. CGF.Builder.CreateGEP(OverflowArgArea.getElementType(),
  6619. OverflowArgArea.getPointer(), PaddedSizeV,
  6620. "overflow_arg_area");
  6621. CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
  6622. CGF.EmitBranch(ContBlock);
  6623. // Return the appropriate result.
  6624. CGF.EmitBlock(ContBlock);
  6625. Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
  6626. "va_arg.addr");
  6627. if (IsIndirect)
  6628. ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), ArgTy,
  6629. TyInfo.Align);
  6630. return ResAddr;
  6631. }
  6632. ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
  6633. if (RetTy->isVoidType())
  6634. return ABIArgInfo::getIgnore();
  6635. if (isVectorArgumentType(RetTy))
  6636. return ABIArgInfo::getDirect();
  6637. if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
  6638. return getNaturalAlignIndirect(RetTy);
  6639. return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
  6640. : ABIArgInfo::getDirect());
  6641. }
  6642. ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
  6643. // Handle the generic C++ ABI.
  6644. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  6645. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  6646. // Integers and enums are extended to full register width.
  6647. if (isPromotableIntegerTypeForABI(Ty))
  6648. return ABIArgInfo::getExtend(Ty);
  6649. // Handle vector types and vector-like structure types. Note that
  6650. // as opposed to float-like structure types, we do not allow any
  6651. // padding for vector-like structures, so verify the sizes match.
  6652. uint64_t Size = getContext().getTypeSize(Ty);
  6653. QualType SingleElementTy = GetSingleElementType(Ty);
  6654. if (isVectorArgumentType(SingleElementTy) &&
  6655. getContext().getTypeSize(SingleElementTy) == Size)
  6656. return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
  6657. // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
  6658. if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
  6659. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  6660. // Handle small structures.
  6661. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  6662. // Structures with flexible arrays have variable length, so really
  6663. // fail the size test above.
  6664. const RecordDecl *RD = RT->getDecl();
  6665. if (RD->hasFlexibleArrayMember())
  6666. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  6667. // The structure is passed as an unextended integer, a float, or a double.
  6668. llvm::Type *PassTy;
  6669. if (isFPArgumentType(SingleElementTy)) {
  6670. assert(Size == 32 || Size == 64);
  6671. if (Size == 32)
  6672. PassTy = llvm::Type::getFloatTy(getVMContext());
  6673. else
  6674. PassTy = llvm::Type::getDoubleTy(getVMContext());
  6675. } else
  6676. PassTy = llvm::IntegerType::get(getVMContext(), Size);
  6677. return ABIArgInfo::getDirect(PassTy);
  6678. }
  6679. // Non-structure compounds are passed indirectly.
  6680. if (isCompoundType(Ty))
  6681. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  6682. return ABIArgInfo::getDirect(nullptr);
  6683. }
  6684. void SystemZABIInfo::computeInfo(CGFunctionInfo &FI) const {
  6685. const SystemZTargetCodeGenInfo &SZCGI =
  6686. static_cast<const SystemZTargetCodeGenInfo &>(
  6687. CGT.getCGM().getTargetCodeGenInfo());
  6688. if (!getCXXABI().classifyReturnType(FI))
  6689. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  6690. unsigned Idx = 0;
  6691. for (auto &I : FI.arguments()) {
  6692. I.info = classifyArgumentType(I.type);
  6693. if (FI.isVariadic() && Idx++ >= FI.getNumRequiredArgs())
  6694. // Check if a vararg vector argument is passed, in which case the
  6695. // vector ABI becomes visible as the va_list could be passed on to
  6696. // other functions.
  6697. SZCGI.handleExternallyVisibleObjABI(I.type.getTypePtr(), CGT.getCGM(),
  6698. /*IsParam*/true);
  6699. }
  6700. }
  6701. bool SystemZTargetCodeGenInfo::isVectorTypeBased(const Type *Ty,
  6702. bool IsParam) const {
  6703. if (!SeenTypes.insert(Ty).second)
  6704. return false;
  6705. if (IsParam) {
  6706. // A narrow (<16 bytes) vector will as a parameter also expose the ABI as
  6707. // it will be passed in a vector register. A wide (>16 bytes) vector will
  6708. // be passed via "hidden" pointer where any extra alignment is not
  6709. // required (per GCC).
  6710. const Type *SingleEltTy =
  6711. getABIInfo().GetSingleElementType(QualType(Ty, 0)).getTypePtr();
  6712. bool SingleVecEltStruct = SingleEltTy != Ty && SingleEltTy->isVectorType() &&
  6713. Ctx.getTypeSize(SingleEltTy) == Ctx.getTypeSize(Ty);
  6714. if (Ty->isVectorType() || SingleVecEltStruct)
  6715. return Ctx.getTypeSize(Ty) / 8 <= 16;
  6716. }
  6717. // Assume pointers are dereferenced.
  6718. while (Ty->isPointerType() || Ty->isArrayType())
  6719. Ty = Ty->getPointeeOrArrayElementType();
  6720. // Vectors >= 16 bytes expose the ABI through alignment requirements.
  6721. if (Ty->isVectorType() && Ctx.getTypeSize(Ty) / 8 >= 16)
  6722. return true;
  6723. if (const auto *RecordTy = Ty->getAs<RecordType>()) {
  6724. const RecordDecl *RD = RecordTy->getDecl();
  6725. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  6726. if (CXXRD->hasDefinition())
  6727. for (const auto &I : CXXRD->bases())
  6728. if (isVectorTypeBased(I.getType().getTypePtr(), /*IsParam*/false))
  6729. return true;
  6730. for (const auto *FD : RD->fields())
  6731. if (isVectorTypeBased(FD->getType().getTypePtr(), /*IsParam*/false))
  6732. return true;
  6733. }
  6734. if (const auto *FT = Ty->getAs<FunctionType>())
  6735. if (isVectorTypeBased(FT->getReturnType().getTypePtr(), /*IsParam*/true))
  6736. return true;
  6737. if (const FunctionProtoType *Proto = Ty->getAs<FunctionProtoType>())
  6738. for (auto ParamType : Proto->getParamTypes())
  6739. if (isVectorTypeBased(ParamType.getTypePtr(), /*IsParam*/true))
  6740. return true;
  6741. return false;
  6742. }
  6743. //===----------------------------------------------------------------------===//
  6744. // MSP430 ABI Implementation
  6745. //===----------------------------------------------------------------------===//
  6746. namespace {
  6747. class MSP430ABIInfo : public DefaultABIInfo {
  6748. static ABIArgInfo complexArgInfo() {
  6749. ABIArgInfo Info = ABIArgInfo::getDirect();
  6750. Info.setCanBeFlattened(false);
  6751. return Info;
  6752. }
  6753. public:
  6754. MSP430ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  6755. ABIArgInfo classifyReturnType(QualType RetTy) const {
  6756. if (RetTy->isAnyComplexType())
  6757. return complexArgInfo();
  6758. return DefaultABIInfo::classifyReturnType(RetTy);
  6759. }
  6760. ABIArgInfo classifyArgumentType(QualType RetTy) const {
  6761. if (RetTy->isAnyComplexType())
  6762. return complexArgInfo();
  6763. return DefaultABIInfo::classifyArgumentType(RetTy);
  6764. }
  6765. // Just copy the original implementations because
  6766. // DefaultABIInfo::classify{Return,Argument}Type() are not virtual
  6767. void computeInfo(CGFunctionInfo &FI) const override {
  6768. if (!getCXXABI().classifyReturnType(FI))
  6769. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  6770. for (auto &I : FI.arguments())
  6771. I.info = classifyArgumentType(I.type);
  6772. }
  6773. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6774. QualType Ty) const override {
  6775. return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
  6776. }
  6777. };
  6778. class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
  6779. public:
  6780. MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
  6781. : TargetCodeGenInfo(std::make_unique<MSP430ABIInfo>(CGT)) {}
  6782. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  6783. CodeGen::CodeGenModule &M) const override;
  6784. };
  6785. }
  6786. void MSP430TargetCodeGenInfo::setTargetAttributes(
  6787. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
  6788. if (GV->isDeclaration())
  6789. return;
  6790. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  6791. const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>();
  6792. if (!InterruptAttr)
  6793. return;
  6794. // Handle 'interrupt' attribute:
  6795. llvm::Function *F = cast<llvm::Function>(GV);
  6796. // Step 1: Set ISR calling convention.
  6797. F->setCallingConv(llvm::CallingConv::MSP430_INTR);
  6798. // Step 2: Add attributes goodness.
  6799. F->addFnAttr(llvm::Attribute::NoInline);
  6800. F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber()));
  6801. }
  6802. }
  6803. //===----------------------------------------------------------------------===//
  6804. // MIPS ABI Implementation. This works for both little-endian and
  6805. // big-endian variants.
  6806. //===----------------------------------------------------------------------===//
  6807. namespace {
  6808. class MipsABIInfo : public ABIInfo {
  6809. bool IsO32;
  6810. const unsigned MinABIStackAlignInBytes, StackAlignInBytes;
  6811. void CoerceToIntArgs(uint64_t TySize,
  6812. SmallVectorImpl<llvm::Type *> &ArgList) const;
  6813. llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
  6814. llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
  6815. llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
  6816. public:
  6817. MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
  6818. ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
  6819. StackAlignInBytes(IsO32 ? 8 : 16) {}
  6820. ABIArgInfo classifyReturnType(QualType RetTy) const;
  6821. ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
  6822. void computeInfo(CGFunctionInfo &FI) const override;
  6823. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  6824. QualType Ty) const override;
  6825. ABIArgInfo extendType(QualType Ty) const;
  6826. };
  6827. class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
  6828. unsigned SizeOfUnwindException;
  6829. public:
  6830. MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
  6831. : TargetCodeGenInfo(std::make_unique<MipsABIInfo>(CGT, IsO32)),
  6832. SizeOfUnwindException(IsO32 ? 24 : 32) {}
  6833. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
  6834. return 29;
  6835. }
  6836. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  6837. CodeGen::CodeGenModule &CGM) const override {
  6838. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  6839. if (!FD) return;
  6840. llvm::Function *Fn = cast<llvm::Function>(GV);
  6841. if (FD->hasAttr<MipsLongCallAttr>())
  6842. Fn->addFnAttr("long-call");
  6843. else if (FD->hasAttr<MipsShortCallAttr>())
  6844. Fn->addFnAttr("short-call");
  6845. // Other attributes do not have a meaning for declarations.
  6846. if (GV->isDeclaration())
  6847. return;
  6848. if (FD->hasAttr<Mips16Attr>()) {
  6849. Fn->addFnAttr("mips16");
  6850. }
  6851. else if (FD->hasAttr<NoMips16Attr>()) {
  6852. Fn->addFnAttr("nomips16");
  6853. }
  6854. if (FD->hasAttr<MicroMipsAttr>())
  6855. Fn->addFnAttr("micromips");
  6856. else if (FD->hasAttr<NoMicroMipsAttr>())
  6857. Fn->addFnAttr("nomicromips");
  6858. const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
  6859. if (!Attr)
  6860. return;
  6861. const char *Kind;
  6862. switch (Attr->getInterrupt()) {
  6863. case MipsInterruptAttr::eic: Kind = "eic"; break;
  6864. case MipsInterruptAttr::sw0: Kind = "sw0"; break;
  6865. case MipsInterruptAttr::sw1: Kind = "sw1"; break;
  6866. case MipsInterruptAttr::hw0: Kind = "hw0"; break;
  6867. case MipsInterruptAttr::hw1: Kind = "hw1"; break;
  6868. case MipsInterruptAttr::hw2: Kind = "hw2"; break;
  6869. case MipsInterruptAttr::hw3: Kind = "hw3"; break;
  6870. case MipsInterruptAttr::hw4: Kind = "hw4"; break;
  6871. case MipsInterruptAttr::hw5: Kind = "hw5"; break;
  6872. }
  6873. Fn->addFnAttr("interrupt", Kind);
  6874. }
  6875. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  6876. llvm::Value *Address) const override;
  6877. unsigned getSizeOfUnwindException() const override {
  6878. return SizeOfUnwindException;
  6879. }
  6880. };
  6881. }
  6882. void MipsABIInfo::CoerceToIntArgs(
  6883. uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
  6884. llvm::IntegerType *IntTy =
  6885. llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
  6886. // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
  6887. for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
  6888. ArgList.push_back(IntTy);
  6889. // If necessary, add one more integer type to ArgList.
  6890. unsigned R = TySize % (MinABIStackAlignInBytes * 8);
  6891. if (R)
  6892. ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
  6893. }
  6894. // In N32/64, an aligned double precision floating point field is passed in
  6895. // a register.
  6896. llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
  6897. SmallVector<llvm::Type*, 8> ArgList, IntArgList;
  6898. if (IsO32) {
  6899. CoerceToIntArgs(TySize, ArgList);
  6900. return llvm::StructType::get(getVMContext(), ArgList);
  6901. }
  6902. if (Ty->isComplexType())
  6903. return CGT.ConvertType(Ty);
  6904. const RecordType *RT = Ty->getAs<RecordType>();
  6905. // Unions/vectors are passed in integer registers.
  6906. if (!RT || !RT->isStructureOrClassType()) {
  6907. CoerceToIntArgs(TySize, ArgList);
  6908. return llvm::StructType::get(getVMContext(), ArgList);
  6909. }
  6910. const RecordDecl *RD = RT->getDecl();
  6911. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  6912. assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
  6913. uint64_t LastOffset = 0;
  6914. unsigned idx = 0;
  6915. llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
  6916. // Iterate over fields in the struct/class and check if there are any aligned
  6917. // double fields.
  6918. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  6919. i != e; ++i, ++idx) {
  6920. const QualType Ty = i->getType();
  6921. const BuiltinType *BT = Ty->getAs<BuiltinType>();
  6922. if (!BT || BT->getKind() != BuiltinType::Double)
  6923. continue;
  6924. uint64_t Offset = Layout.getFieldOffset(idx);
  6925. if (Offset % 64) // Ignore doubles that are not aligned.
  6926. continue;
  6927. // Add ((Offset - LastOffset) / 64) args of type i64.
  6928. for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
  6929. ArgList.push_back(I64);
  6930. // Add double type.
  6931. ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
  6932. LastOffset = Offset + 64;
  6933. }
  6934. CoerceToIntArgs(TySize - LastOffset, IntArgList);
  6935. ArgList.append(IntArgList.begin(), IntArgList.end());
  6936. return llvm::StructType::get(getVMContext(), ArgList);
  6937. }
  6938. llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
  6939. uint64_t Offset) const {
  6940. if (OrigOffset + MinABIStackAlignInBytes > Offset)
  6941. return nullptr;
  6942. return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
  6943. }
  6944. ABIArgInfo
  6945. MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
  6946. Ty = useFirstFieldIfTransparentUnion(Ty);
  6947. uint64_t OrigOffset = Offset;
  6948. uint64_t TySize = getContext().getTypeSize(Ty);
  6949. uint64_t Align = getContext().getTypeAlign(Ty) / 8;
  6950. Align = std::clamp(Align, (uint64_t)MinABIStackAlignInBytes,
  6951. (uint64_t)StackAlignInBytes);
  6952. unsigned CurrOffset = llvm::alignTo(Offset, Align);
  6953. Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
  6954. if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
  6955. // Ignore empty aggregates.
  6956. if (TySize == 0)
  6957. return ABIArgInfo::getIgnore();
  6958. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  6959. Offset = OrigOffset + MinABIStackAlignInBytes;
  6960. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  6961. }
  6962. // If we have reached here, aggregates are passed directly by coercing to
  6963. // another structure type. Padding is inserted if the offset of the
  6964. // aggregate is unaligned.
  6965. ABIArgInfo ArgInfo =
  6966. ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
  6967. getPaddingType(OrigOffset, CurrOffset));
  6968. ArgInfo.setInReg(true);
  6969. return ArgInfo;
  6970. }
  6971. // Treat an enum type as its underlying type.
  6972. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  6973. Ty = EnumTy->getDecl()->getIntegerType();
  6974. // Make sure we pass indirectly things that are too large.
  6975. if (const auto *EIT = Ty->getAs<BitIntType>())
  6976. if (EIT->getNumBits() > 128 ||
  6977. (EIT->getNumBits() > 64 &&
  6978. !getContext().getTargetInfo().hasInt128Type()))
  6979. return getNaturalAlignIndirect(Ty);
  6980. // All integral types are promoted to the GPR width.
  6981. if (Ty->isIntegralOrEnumerationType())
  6982. return extendType(Ty);
  6983. return ABIArgInfo::getDirect(
  6984. nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
  6985. }
  6986. llvm::Type*
  6987. MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
  6988. const RecordType *RT = RetTy->getAs<RecordType>();
  6989. SmallVector<llvm::Type*, 8> RTList;
  6990. if (RT && RT->isStructureOrClassType()) {
  6991. const RecordDecl *RD = RT->getDecl();
  6992. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  6993. unsigned FieldCnt = Layout.getFieldCount();
  6994. // N32/64 returns struct/classes in floating point registers if the
  6995. // following conditions are met:
  6996. // 1. The size of the struct/class is no larger than 128-bit.
  6997. // 2. The struct/class has one or two fields all of which are floating
  6998. // point types.
  6999. // 3. The offset of the first field is zero (this follows what gcc does).
  7000. //
  7001. // Any other composite results are returned in integer registers.
  7002. //
  7003. if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
  7004. RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
  7005. for (; b != e; ++b) {
  7006. const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
  7007. if (!BT || !BT->isFloatingPoint())
  7008. break;
  7009. RTList.push_back(CGT.ConvertType(b->getType()));
  7010. }
  7011. if (b == e)
  7012. return llvm::StructType::get(getVMContext(), RTList,
  7013. RD->hasAttr<PackedAttr>());
  7014. RTList.clear();
  7015. }
  7016. }
  7017. CoerceToIntArgs(Size, RTList);
  7018. return llvm::StructType::get(getVMContext(), RTList);
  7019. }
  7020. ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
  7021. uint64_t Size = getContext().getTypeSize(RetTy);
  7022. if (RetTy->isVoidType())
  7023. return ABIArgInfo::getIgnore();
  7024. // O32 doesn't treat zero-sized structs differently from other structs.
  7025. // However, N32/N64 ignores zero sized return values.
  7026. if (!IsO32 && Size == 0)
  7027. return ABIArgInfo::getIgnore();
  7028. if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
  7029. if (Size <= 128) {
  7030. if (RetTy->isAnyComplexType())
  7031. return ABIArgInfo::getDirect();
  7032. // O32 returns integer vectors in registers and N32/N64 returns all small
  7033. // aggregates in registers.
  7034. if (!IsO32 ||
  7035. (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
  7036. ABIArgInfo ArgInfo =
  7037. ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
  7038. ArgInfo.setInReg(true);
  7039. return ArgInfo;
  7040. }
  7041. }
  7042. return getNaturalAlignIndirect(RetTy);
  7043. }
  7044. // Treat an enum type as its underlying type.
  7045. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  7046. RetTy = EnumTy->getDecl()->getIntegerType();
  7047. // Make sure we pass indirectly things that are too large.
  7048. if (const auto *EIT = RetTy->getAs<BitIntType>())
  7049. if (EIT->getNumBits() > 128 ||
  7050. (EIT->getNumBits() > 64 &&
  7051. !getContext().getTargetInfo().hasInt128Type()))
  7052. return getNaturalAlignIndirect(RetTy);
  7053. if (isPromotableIntegerTypeForABI(RetTy))
  7054. return ABIArgInfo::getExtend(RetTy);
  7055. if ((RetTy->isUnsignedIntegerOrEnumerationType() ||
  7056. RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32)
  7057. return ABIArgInfo::getSignExtend(RetTy);
  7058. return ABIArgInfo::getDirect();
  7059. }
  7060. void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
  7061. ABIArgInfo &RetInfo = FI.getReturnInfo();
  7062. if (!getCXXABI().classifyReturnType(FI))
  7063. RetInfo = classifyReturnType(FI.getReturnType());
  7064. // Check if a pointer to an aggregate is passed as a hidden argument.
  7065. uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
  7066. for (auto &I : FI.arguments())
  7067. I.info = classifyArgumentType(I.type, Offset);
  7068. }
  7069. Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  7070. QualType OrigTy) const {
  7071. QualType Ty = OrigTy;
  7072. // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
  7073. // Pointers are also promoted in the same way but this only matters for N32.
  7074. unsigned SlotSizeInBits = IsO32 ? 32 : 64;
  7075. unsigned PtrWidth = getTarget().getPointerWidth(LangAS::Default);
  7076. bool DidPromote = false;
  7077. if ((Ty->isIntegerType() &&
  7078. getContext().getIntWidth(Ty) < SlotSizeInBits) ||
  7079. (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
  7080. DidPromote = true;
  7081. Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
  7082. Ty->isSignedIntegerType());
  7083. }
  7084. auto TyInfo = getContext().getTypeInfoInChars(Ty);
  7085. // The alignment of things in the argument area is never larger than
  7086. // StackAlignInBytes.
  7087. TyInfo.Align =
  7088. std::min(TyInfo.Align, CharUnits::fromQuantity(StackAlignInBytes));
  7089. // MinABIStackAlignInBytes is the size of argument slots on the stack.
  7090. CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
  7091. Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
  7092. TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
  7093. // If there was a promotion, "unpromote" into a temporary.
  7094. // TODO: can we just use a pointer into a subset of the original slot?
  7095. if (DidPromote) {
  7096. Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
  7097. llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
  7098. // Truncate down to the right width.
  7099. llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
  7100. : CGF.IntPtrTy);
  7101. llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
  7102. if (OrigTy->isPointerType())
  7103. V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
  7104. CGF.Builder.CreateStore(V, Temp);
  7105. Addr = Temp;
  7106. }
  7107. return Addr;
  7108. }
  7109. ABIArgInfo MipsABIInfo::extendType(QualType Ty) const {
  7110. int TySize = getContext().getTypeSize(Ty);
  7111. // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
  7112. if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
  7113. return ABIArgInfo::getSignExtend(Ty);
  7114. return ABIArgInfo::getExtend(Ty);
  7115. }
  7116. bool
  7117. MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  7118. llvm::Value *Address) const {
  7119. // This information comes from gcc's implementation, which seems to
  7120. // as canonical as it gets.
  7121. // Everything on MIPS is 4 bytes. Double-precision FP registers
  7122. // are aliased to pairs of single-precision FP registers.
  7123. llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
  7124. // 0-31 are the general purpose registers, $0 - $31.
  7125. // 32-63 are the floating-point registers, $f0 - $f31.
  7126. // 64 and 65 are the multiply/divide registers, $hi and $lo.
  7127. // 66 is the (notional, I think) register for signal-handler return.
  7128. AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
  7129. // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
  7130. // They are one bit wide and ignored here.
  7131. // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
  7132. // (coprocessor 1 is the FP unit)
  7133. // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
  7134. // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
  7135. // 176-181 are the DSP accumulator registers.
  7136. AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
  7137. return false;
  7138. }
  7139. //===----------------------------------------------------------------------===//
  7140. // M68k ABI Implementation
  7141. //===----------------------------------------------------------------------===//
  7142. namespace {
  7143. class M68kTargetCodeGenInfo : public TargetCodeGenInfo {
  7144. public:
  7145. M68kTargetCodeGenInfo(CodeGenTypes &CGT)
  7146. : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
  7147. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  7148. CodeGen::CodeGenModule &M) const override;
  7149. };
  7150. } // namespace
  7151. void M68kTargetCodeGenInfo::setTargetAttributes(
  7152. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
  7153. if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  7154. if (const auto *attr = FD->getAttr<M68kInterruptAttr>()) {
  7155. // Handle 'interrupt' attribute:
  7156. llvm::Function *F = cast<llvm::Function>(GV);
  7157. // Step 1: Set ISR calling convention.
  7158. F->setCallingConv(llvm::CallingConv::M68k_INTR);
  7159. // Step 2: Add attributes goodness.
  7160. F->addFnAttr(llvm::Attribute::NoInline);
  7161. // Step 3: Emit ISR vector alias.
  7162. unsigned Num = attr->getNumber() / 2;
  7163. llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
  7164. "__isr_" + Twine(Num), F);
  7165. }
  7166. }
  7167. }
  7168. //===----------------------------------------------------------------------===//
  7169. // AVR ABI Implementation. Documented at
  7170. // https://gcc.gnu.org/wiki/avr-gcc#Calling_Convention
  7171. // https://gcc.gnu.org/wiki/avr-gcc#Reduced_Tiny
  7172. //===----------------------------------------------------------------------===//
  7173. namespace {
  7174. class AVRABIInfo : public DefaultABIInfo {
  7175. private:
  7176. // The total amount of registers can be used to pass parameters. It is 18 on
  7177. // AVR, or 6 on AVRTiny.
  7178. const unsigned ParamRegs;
  7179. // The total amount of registers can be used to pass return value. It is 8 on
  7180. // AVR, or 4 on AVRTiny.
  7181. const unsigned RetRegs;
  7182. public:
  7183. AVRABIInfo(CodeGenTypes &CGT, unsigned NPR, unsigned NRR)
  7184. : DefaultABIInfo(CGT), ParamRegs(NPR), RetRegs(NRR) {}
  7185. ABIArgInfo classifyReturnType(QualType Ty, bool &LargeRet) const {
  7186. // On AVR, a return struct with size less than or equals to 8 bytes is
  7187. // returned directly via registers R18-R25. On AVRTiny, a return struct
  7188. // with size less than or equals to 4 bytes is returned directly via
  7189. // registers R22-R25.
  7190. if (isAggregateTypeForABI(Ty) &&
  7191. getContext().getTypeSize(Ty) <= RetRegs * 8)
  7192. return ABIArgInfo::getDirect();
  7193. // A return value (struct or scalar) with larger size is returned via a
  7194. // stack slot, along with a pointer as the function's implicit argument.
  7195. if (getContext().getTypeSize(Ty) > RetRegs * 8) {
  7196. LargeRet = true;
  7197. return getNaturalAlignIndirect(Ty);
  7198. }
  7199. // An i8 return value should not be extended to i16, since AVR has 8-bit
  7200. // registers.
  7201. if (Ty->isIntegralOrEnumerationType() && getContext().getTypeSize(Ty) <= 8)
  7202. return ABIArgInfo::getDirect();
  7203. // Otherwise we follow the default way which is compatible.
  7204. return DefaultABIInfo::classifyReturnType(Ty);
  7205. }
  7206. ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegs) const {
  7207. unsigned TySize = getContext().getTypeSize(Ty);
  7208. // An int8 type argument always costs two registers like an int16.
  7209. if (TySize == 8 && NumRegs >= 2) {
  7210. NumRegs -= 2;
  7211. return ABIArgInfo::getExtend(Ty);
  7212. }
  7213. // If the argument size is an odd number of bytes, round up the size
  7214. // to the next even number.
  7215. TySize = llvm::alignTo(TySize, 16);
  7216. // Any type including an array/struct type can be passed in rgisters,
  7217. // if there are enough registers left.
  7218. if (TySize <= NumRegs * 8) {
  7219. NumRegs -= TySize / 8;
  7220. return ABIArgInfo::getDirect();
  7221. }
  7222. // An argument is passed either completely in registers or completely in
  7223. // memory. Since there are not enough registers left, current argument
  7224. // and all other unprocessed arguments should be passed in memory.
  7225. // However we still need to return `ABIArgInfo::getDirect()` other than
  7226. // `ABIInfo::getNaturalAlignIndirect(Ty)`, otherwise an extra stack slot
  7227. // will be allocated, so the stack frame layout will be incompatible with
  7228. // avr-gcc.
  7229. NumRegs = 0;
  7230. return ABIArgInfo::getDirect();
  7231. }
  7232. void computeInfo(CGFunctionInfo &FI) const override {
  7233. // Decide the return type.
  7234. bool LargeRet = false;
  7235. if (!getCXXABI().classifyReturnType(FI))
  7236. FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), LargeRet);
  7237. // Decide each argument type. The total number of registers can be used for
  7238. // arguments depends on several factors:
  7239. // 1. Arguments of varargs functions are passed on the stack. This applies
  7240. // even to the named arguments. So no register can be used.
  7241. // 2. Total 18 registers can be used on avr and 6 ones on avrtiny.
  7242. // 3. If the return type is a struct with too large size, two registers
  7243. // (out of 18/6) will be cost as an implicit pointer argument.
  7244. unsigned NumRegs = ParamRegs;
  7245. if (FI.isVariadic())
  7246. NumRegs = 0;
  7247. else if (LargeRet)
  7248. NumRegs -= 2;
  7249. for (auto &I : FI.arguments())
  7250. I.info = classifyArgumentType(I.type, NumRegs);
  7251. }
  7252. };
  7253. class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
  7254. public:
  7255. AVRTargetCodeGenInfo(CodeGenTypes &CGT, unsigned NPR, unsigned NRR)
  7256. : TargetCodeGenInfo(std::make_unique<AVRABIInfo>(CGT, NPR, NRR)) {}
  7257. LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
  7258. const VarDecl *D) const override {
  7259. // Check if global/static variable is defined in address space
  7260. // 1~6 (__flash, __flash1, __flash2, __flash3, __flash4, __flash5)
  7261. // but not constant.
  7262. if (D) {
  7263. LangAS AS = D->getType().getAddressSpace();
  7264. if (isTargetAddressSpace(AS) && 1 <= toTargetAddressSpace(AS) &&
  7265. toTargetAddressSpace(AS) <= 6 && !D->getType().isConstQualified())
  7266. CGM.getDiags().Report(D->getLocation(),
  7267. diag::err_verify_nonconst_addrspace)
  7268. << "__flash*";
  7269. }
  7270. return TargetCodeGenInfo::getGlobalVarAddressSpace(CGM, D);
  7271. }
  7272. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  7273. CodeGen::CodeGenModule &CGM) const override {
  7274. if (GV->isDeclaration())
  7275. return;
  7276. const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
  7277. if (!FD) return;
  7278. auto *Fn = cast<llvm::Function>(GV);
  7279. if (FD->getAttr<AVRInterruptAttr>())
  7280. Fn->addFnAttr("interrupt");
  7281. if (FD->getAttr<AVRSignalAttr>())
  7282. Fn->addFnAttr("signal");
  7283. }
  7284. };
  7285. }
  7286. //===----------------------------------------------------------------------===//
  7287. // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
  7288. // Currently subclassed only to implement custom OpenCL C function attribute
  7289. // handling.
  7290. //===----------------------------------------------------------------------===//
  7291. namespace {
  7292. class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
  7293. public:
  7294. TCETargetCodeGenInfo(CodeGenTypes &CGT)
  7295. : DefaultTargetCodeGenInfo(CGT) {}
  7296. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  7297. CodeGen::CodeGenModule &M) const override;
  7298. };
  7299. void TCETargetCodeGenInfo::setTargetAttributes(
  7300. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
  7301. if (GV->isDeclaration())
  7302. return;
  7303. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  7304. if (!FD) return;
  7305. llvm::Function *F = cast<llvm::Function>(GV);
  7306. if (M.getLangOpts().OpenCL) {
  7307. if (FD->hasAttr<OpenCLKernelAttr>()) {
  7308. // OpenCL C Kernel functions are not subject to inlining
  7309. F->addFnAttr(llvm::Attribute::NoInline);
  7310. const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
  7311. if (Attr) {
  7312. // Convert the reqd_work_group_size() attributes to metadata.
  7313. llvm::LLVMContext &Context = F->getContext();
  7314. llvm::NamedMDNode *OpenCLMetadata =
  7315. M.getModule().getOrInsertNamedMetadata(
  7316. "opencl.kernel_wg_size_info");
  7317. SmallVector<llvm::Metadata *, 5> Operands;
  7318. Operands.push_back(llvm::ConstantAsMetadata::get(F));
  7319. Operands.push_back(
  7320. llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
  7321. M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
  7322. Operands.push_back(
  7323. llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
  7324. M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
  7325. Operands.push_back(
  7326. llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
  7327. M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
  7328. // Add a boolean constant operand for "required" (true) or "hint"
  7329. // (false) for implementing the work_group_size_hint attr later.
  7330. // Currently always true as the hint is not yet implemented.
  7331. Operands.push_back(
  7332. llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
  7333. OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
  7334. }
  7335. }
  7336. }
  7337. }
  7338. }
  7339. //===----------------------------------------------------------------------===//
  7340. // Hexagon ABI Implementation
  7341. //===----------------------------------------------------------------------===//
  7342. namespace {
  7343. class HexagonABIInfo : public DefaultABIInfo {
  7344. public:
  7345. HexagonABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  7346. private:
  7347. ABIArgInfo classifyReturnType(QualType RetTy) const;
  7348. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  7349. ABIArgInfo classifyArgumentType(QualType RetTy, unsigned *RegsLeft) const;
  7350. void computeInfo(CGFunctionInfo &FI) const override;
  7351. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  7352. QualType Ty) const override;
  7353. Address EmitVAArgFromMemory(CodeGenFunction &CFG, Address VAListAddr,
  7354. QualType Ty) const;
  7355. Address EmitVAArgForHexagon(CodeGenFunction &CFG, Address VAListAddr,
  7356. QualType Ty) const;
  7357. Address EmitVAArgForHexagonLinux(CodeGenFunction &CFG, Address VAListAddr,
  7358. QualType Ty) const;
  7359. };
  7360. class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
  7361. public:
  7362. HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
  7363. : TargetCodeGenInfo(std::make_unique<HexagonABIInfo>(CGT)) {}
  7364. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  7365. return 29;
  7366. }
  7367. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  7368. CodeGen::CodeGenModule &GCM) const override {
  7369. if (GV->isDeclaration())
  7370. return;
  7371. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  7372. if (!FD)
  7373. return;
  7374. }
  7375. };
  7376. } // namespace
  7377. void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
  7378. unsigned RegsLeft = 6;
  7379. if (!getCXXABI().classifyReturnType(FI))
  7380. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  7381. for (auto &I : FI.arguments())
  7382. I.info = classifyArgumentType(I.type, &RegsLeft);
  7383. }
  7384. static bool HexagonAdjustRegsLeft(uint64_t Size, unsigned *RegsLeft) {
  7385. assert(Size <= 64 && "Not expecting to pass arguments larger than 64 bits"
  7386. " through registers");
  7387. if (*RegsLeft == 0)
  7388. return false;
  7389. if (Size <= 32) {
  7390. (*RegsLeft)--;
  7391. return true;
  7392. }
  7393. if (2 <= (*RegsLeft & (~1U))) {
  7394. *RegsLeft = (*RegsLeft & (~1U)) - 2;
  7395. return true;
  7396. }
  7397. // Next available register was r5 but candidate was greater than 32-bits so it
  7398. // has to go on the stack. However we still consume r5
  7399. if (*RegsLeft == 1)
  7400. *RegsLeft = 0;
  7401. return false;
  7402. }
  7403. ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty,
  7404. unsigned *RegsLeft) const {
  7405. if (!isAggregateTypeForABI(Ty)) {
  7406. // Treat an enum type as its underlying type.
  7407. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  7408. Ty = EnumTy->getDecl()->getIntegerType();
  7409. uint64_t Size = getContext().getTypeSize(Ty);
  7410. if (Size <= 64)
  7411. HexagonAdjustRegsLeft(Size, RegsLeft);
  7412. if (Size > 64 && Ty->isBitIntType())
  7413. return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
  7414. return isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
  7415. : ABIArgInfo::getDirect();
  7416. }
  7417. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  7418. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  7419. // Ignore empty records.
  7420. if (isEmptyRecord(getContext(), Ty, true))
  7421. return ABIArgInfo::getIgnore();
  7422. uint64_t Size = getContext().getTypeSize(Ty);
  7423. unsigned Align = getContext().getTypeAlign(Ty);
  7424. if (Size > 64)
  7425. return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
  7426. if (HexagonAdjustRegsLeft(Size, RegsLeft))
  7427. Align = Size <= 32 ? 32 : 64;
  7428. if (Size <= Align) {
  7429. // Pass in the smallest viable integer type.
  7430. if (!llvm::isPowerOf2_64(Size))
  7431. Size = llvm::NextPowerOf2(Size);
  7432. return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
  7433. }
  7434. return DefaultABIInfo::classifyArgumentType(Ty);
  7435. }
  7436. ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
  7437. if (RetTy->isVoidType())
  7438. return ABIArgInfo::getIgnore();
  7439. const TargetInfo &T = CGT.getTarget();
  7440. uint64_t Size = getContext().getTypeSize(RetTy);
  7441. if (RetTy->getAs<VectorType>()) {
  7442. // HVX vectors are returned in vector registers or register pairs.
  7443. if (T.hasFeature("hvx")) {
  7444. assert(T.hasFeature("hvx-length64b") || T.hasFeature("hvx-length128b"));
  7445. uint64_t VecSize = T.hasFeature("hvx-length64b") ? 64*8 : 128*8;
  7446. if (Size == VecSize || Size == 2*VecSize)
  7447. return ABIArgInfo::getDirectInReg();
  7448. }
  7449. // Large vector types should be returned via memory.
  7450. if (Size > 64)
  7451. return getNaturalAlignIndirect(RetTy);
  7452. }
  7453. if (!isAggregateTypeForABI(RetTy)) {
  7454. // Treat an enum type as its underlying type.
  7455. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  7456. RetTy = EnumTy->getDecl()->getIntegerType();
  7457. if (Size > 64 && RetTy->isBitIntType())
  7458. return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
  7459. return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
  7460. : ABIArgInfo::getDirect();
  7461. }
  7462. if (isEmptyRecord(getContext(), RetTy, true))
  7463. return ABIArgInfo::getIgnore();
  7464. // Aggregates <= 8 bytes are returned in registers, other aggregates
  7465. // are returned indirectly.
  7466. if (Size <= 64) {
  7467. // Return in the smallest viable integer type.
  7468. if (!llvm::isPowerOf2_64(Size))
  7469. Size = llvm::NextPowerOf2(Size);
  7470. return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size));
  7471. }
  7472. return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
  7473. }
  7474. Address HexagonABIInfo::EmitVAArgFromMemory(CodeGenFunction &CGF,
  7475. Address VAListAddr,
  7476. QualType Ty) const {
  7477. // Load the overflow area pointer.
  7478. Address __overflow_area_pointer_p =
  7479. CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
  7480. llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
  7481. __overflow_area_pointer_p, "__overflow_area_pointer");
  7482. uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
  7483. if (Align > 4) {
  7484. // Alignment should be a power of 2.
  7485. assert((Align & (Align - 1)) == 0 && "Alignment is not power of 2!");
  7486. // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
  7487. llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
  7488. // Add offset to the current pointer to access the argument.
  7489. __overflow_area_pointer =
  7490. CGF.Builder.CreateGEP(CGF.Int8Ty, __overflow_area_pointer, Offset);
  7491. llvm::Value *AsInt =
  7492. CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
  7493. // Create a mask which should be "AND"ed
  7494. // with (overflow_arg_area + align - 1)
  7495. llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -(int)Align);
  7496. __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
  7497. CGF.Builder.CreateAnd(AsInt, Mask), __overflow_area_pointer->getType(),
  7498. "__overflow_area_pointer.align");
  7499. }
  7500. // Get the type of the argument from memory and bitcast
  7501. // overflow area pointer to the argument type.
  7502. llvm::Type *PTy = CGF.ConvertTypeForMem(Ty);
  7503. Address AddrTyped = CGF.Builder.CreateElementBitCast(
  7504. Address(__overflow_area_pointer, CGF.Int8Ty,
  7505. CharUnits::fromQuantity(Align)),
  7506. PTy);
  7507. // Round up to the minimum stack alignment for varargs which is 4 bytes.
  7508. uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
  7509. __overflow_area_pointer = CGF.Builder.CreateGEP(
  7510. CGF.Int8Ty, __overflow_area_pointer,
  7511. llvm::ConstantInt::get(CGF.Int32Ty, Offset),
  7512. "__overflow_area_pointer.next");
  7513. CGF.Builder.CreateStore(__overflow_area_pointer, __overflow_area_pointer_p);
  7514. return AddrTyped;
  7515. }
  7516. Address HexagonABIInfo::EmitVAArgForHexagon(CodeGenFunction &CGF,
  7517. Address VAListAddr,
  7518. QualType Ty) const {
  7519. // FIXME: Need to handle alignment
  7520. llvm::Type *BP = CGF.Int8PtrTy;
  7521. CGBuilderTy &Builder = CGF.Builder;
  7522. Address VAListAddrAsBPP = Builder.CreateElementBitCast(VAListAddr, BP, "ap");
  7523. llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
  7524. // Handle address alignment for type alignment > 32 bits
  7525. uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
  7526. if (TyAlign > 4) {
  7527. assert((TyAlign & (TyAlign - 1)) == 0 && "Alignment is not power of 2!");
  7528. llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
  7529. AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
  7530. AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
  7531. Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
  7532. }
  7533. Address AddrTyped = Builder.CreateElementBitCast(
  7534. Address(Addr, CGF.Int8Ty, CharUnits::fromQuantity(TyAlign)),
  7535. CGF.ConvertType(Ty));
  7536. uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4);
  7537. llvm::Value *NextAddr = Builder.CreateGEP(
  7538. CGF.Int8Ty, Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next");
  7539. Builder.CreateStore(NextAddr, VAListAddrAsBPP);
  7540. return AddrTyped;
  7541. }
  7542. Address HexagonABIInfo::EmitVAArgForHexagonLinux(CodeGenFunction &CGF,
  7543. Address VAListAddr,
  7544. QualType Ty) const {
  7545. int ArgSize = CGF.getContext().getTypeSize(Ty) / 8;
  7546. if (ArgSize > 8)
  7547. return EmitVAArgFromMemory(CGF, VAListAddr, Ty);
  7548. // Here we have check if the argument is in register area or
  7549. // in overflow area.
  7550. // If the saved register area pointer + argsize rounded up to alignment >
  7551. // saved register area end pointer, argument is in overflow area.
  7552. unsigned RegsLeft = 6;
  7553. Ty = CGF.getContext().getCanonicalType(Ty);
  7554. (void)classifyArgumentType(Ty, &RegsLeft);
  7555. llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
  7556. llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
  7557. llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
  7558. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
  7559. // Get rounded size of the argument.GCC does not allow vararg of
  7560. // size < 4 bytes. We follow the same logic here.
  7561. ArgSize = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
  7562. int ArgAlign = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8;
  7563. // Argument may be in saved register area
  7564. CGF.EmitBlock(MaybeRegBlock);
  7565. // Load the current saved register area pointer.
  7566. Address __current_saved_reg_area_pointer_p = CGF.Builder.CreateStructGEP(
  7567. VAListAddr, 0, "__current_saved_reg_area_pointer_p");
  7568. llvm::Value *__current_saved_reg_area_pointer = CGF.Builder.CreateLoad(
  7569. __current_saved_reg_area_pointer_p, "__current_saved_reg_area_pointer");
  7570. // Load the saved register area end pointer.
  7571. Address __saved_reg_area_end_pointer_p = CGF.Builder.CreateStructGEP(
  7572. VAListAddr, 1, "__saved_reg_area_end_pointer_p");
  7573. llvm::Value *__saved_reg_area_end_pointer = CGF.Builder.CreateLoad(
  7574. __saved_reg_area_end_pointer_p, "__saved_reg_area_end_pointer");
  7575. // If the size of argument is > 4 bytes, check if the stack
  7576. // location is aligned to 8 bytes
  7577. if (ArgAlign > 4) {
  7578. llvm::Value *__current_saved_reg_area_pointer_int =
  7579. CGF.Builder.CreatePtrToInt(__current_saved_reg_area_pointer,
  7580. CGF.Int32Ty);
  7581. __current_saved_reg_area_pointer_int = CGF.Builder.CreateAdd(
  7582. __current_saved_reg_area_pointer_int,
  7583. llvm::ConstantInt::get(CGF.Int32Ty, (ArgAlign - 1)),
  7584. "align_current_saved_reg_area_pointer");
  7585. __current_saved_reg_area_pointer_int =
  7586. CGF.Builder.CreateAnd(__current_saved_reg_area_pointer_int,
  7587. llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
  7588. "align_current_saved_reg_area_pointer");
  7589. __current_saved_reg_area_pointer =
  7590. CGF.Builder.CreateIntToPtr(__current_saved_reg_area_pointer_int,
  7591. __current_saved_reg_area_pointer->getType(),
  7592. "align_current_saved_reg_area_pointer");
  7593. }
  7594. llvm::Value *__new_saved_reg_area_pointer =
  7595. CGF.Builder.CreateGEP(CGF.Int8Ty, __current_saved_reg_area_pointer,
  7596. llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
  7597. "__new_saved_reg_area_pointer");
  7598. llvm::Value *UsingStack = nullptr;
  7599. UsingStack = CGF.Builder.CreateICmpSGT(__new_saved_reg_area_pointer,
  7600. __saved_reg_area_end_pointer);
  7601. CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, InRegBlock);
  7602. // Argument in saved register area
  7603. // Implement the block where argument is in register saved area
  7604. CGF.EmitBlock(InRegBlock);
  7605. llvm::Type *PTy = CGF.ConvertType(Ty);
  7606. llvm::Value *__saved_reg_area_p = CGF.Builder.CreateBitCast(
  7607. __current_saved_reg_area_pointer, llvm::PointerType::getUnqual(PTy));
  7608. CGF.Builder.CreateStore(__new_saved_reg_area_pointer,
  7609. __current_saved_reg_area_pointer_p);
  7610. CGF.EmitBranch(ContBlock);
  7611. // Argument in overflow area
  7612. // Implement the block where the argument is in overflow area.
  7613. CGF.EmitBlock(OnStackBlock);
  7614. // Load the overflow area pointer
  7615. Address __overflow_area_pointer_p =
  7616. CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p");
  7617. llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad(
  7618. __overflow_area_pointer_p, "__overflow_area_pointer");
  7619. // Align the overflow area pointer according to the alignment of the argument
  7620. if (ArgAlign > 4) {
  7621. llvm::Value *__overflow_area_pointer_int =
  7622. CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty);
  7623. __overflow_area_pointer_int =
  7624. CGF.Builder.CreateAdd(__overflow_area_pointer_int,
  7625. llvm::ConstantInt::get(CGF.Int32Ty, ArgAlign - 1),
  7626. "align_overflow_area_pointer");
  7627. __overflow_area_pointer_int =
  7628. CGF.Builder.CreateAnd(__overflow_area_pointer_int,
  7629. llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign),
  7630. "align_overflow_area_pointer");
  7631. __overflow_area_pointer = CGF.Builder.CreateIntToPtr(
  7632. __overflow_area_pointer_int, __overflow_area_pointer->getType(),
  7633. "align_overflow_area_pointer");
  7634. }
  7635. // Get the pointer for next argument in overflow area and store it
  7636. // to overflow area pointer.
  7637. llvm::Value *__new_overflow_area_pointer = CGF.Builder.CreateGEP(
  7638. CGF.Int8Ty, __overflow_area_pointer,
  7639. llvm::ConstantInt::get(CGF.Int32Ty, ArgSize),
  7640. "__overflow_area_pointer.next");
  7641. CGF.Builder.CreateStore(__new_overflow_area_pointer,
  7642. __overflow_area_pointer_p);
  7643. CGF.Builder.CreateStore(__new_overflow_area_pointer,
  7644. __current_saved_reg_area_pointer_p);
  7645. // Bitcast the overflow area pointer to the type of argument.
  7646. llvm::Type *OverflowPTy = CGF.ConvertTypeForMem(Ty);
  7647. llvm::Value *__overflow_area_p = CGF.Builder.CreateBitCast(
  7648. __overflow_area_pointer, llvm::PointerType::getUnqual(OverflowPTy));
  7649. CGF.EmitBranch(ContBlock);
  7650. // Get the correct pointer to load the variable argument
  7651. // Implement the ContBlock
  7652. CGF.EmitBlock(ContBlock);
  7653. llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
  7654. llvm::Type *MemPTy = llvm::PointerType::getUnqual(MemTy);
  7655. llvm::PHINode *ArgAddr = CGF.Builder.CreatePHI(MemPTy, 2, "vaarg.addr");
  7656. ArgAddr->addIncoming(__saved_reg_area_p, InRegBlock);
  7657. ArgAddr->addIncoming(__overflow_area_p, OnStackBlock);
  7658. return Address(ArgAddr, MemTy, CharUnits::fromQuantity(ArgAlign));
  7659. }
  7660. Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  7661. QualType Ty) const {
  7662. if (getTarget().getTriple().isMusl())
  7663. return EmitVAArgForHexagonLinux(CGF, VAListAddr, Ty);
  7664. return EmitVAArgForHexagon(CGF, VAListAddr, Ty);
  7665. }
  7666. //===----------------------------------------------------------------------===//
  7667. // Lanai ABI Implementation
  7668. //===----------------------------------------------------------------------===//
  7669. namespace {
  7670. class LanaiABIInfo : public DefaultABIInfo {
  7671. public:
  7672. LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  7673. bool shouldUseInReg(QualType Ty, CCState &State) const;
  7674. void computeInfo(CGFunctionInfo &FI) const override {
  7675. CCState State(FI);
  7676. // Lanai uses 4 registers to pass arguments unless the function has the
  7677. // regparm attribute set.
  7678. if (FI.getHasRegParm()) {
  7679. State.FreeRegs = FI.getRegParm();
  7680. } else {
  7681. State.FreeRegs = 4;
  7682. }
  7683. if (!getCXXABI().classifyReturnType(FI))
  7684. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  7685. for (auto &I : FI.arguments())
  7686. I.info = classifyArgumentType(I.type, State);
  7687. }
  7688. ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
  7689. ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
  7690. };
  7691. } // end anonymous namespace
  7692. bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
  7693. unsigned Size = getContext().getTypeSize(Ty);
  7694. unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
  7695. if (SizeInRegs == 0)
  7696. return false;
  7697. if (SizeInRegs > State.FreeRegs) {
  7698. State.FreeRegs = 0;
  7699. return false;
  7700. }
  7701. State.FreeRegs -= SizeInRegs;
  7702. return true;
  7703. }
  7704. ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
  7705. CCState &State) const {
  7706. if (!ByVal) {
  7707. if (State.FreeRegs) {
  7708. --State.FreeRegs; // Non-byval indirects just use one pointer.
  7709. return getNaturalAlignIndirectInReg(Ty);
  7710. }
  7711. return getNaturalAlignIndirect(Ty, false);
  7712. }
  7713. // Compute the byval alignment.
  7714. const unsigned MinABIStackAlignInBytes = 4;
  7715. unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
  7716. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
  7717. /*Realign=*/TypeAlign >
  7718. MinABIStackAlignInBytes);
  7719. }
  7720. ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
  7721. CCState &State) const {
  7722. // Check with the C++ ABI first.
  7723. const RecordType *RT = Ty->getAs<RecordType>();
  7724. if (RT) {
  7725. CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
  7726. if (RAA == CGCXXABI::RAA_Indirect) {
  7727. return getIndirectResult(Ty, /*ByVal=*/false, State);
  7728. } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
  7729. return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
  7730. }
  7731. }
  7732. if (isAggregateTypeForABI(Ty)) {
  7733. // Structures with flexible arrays are always indirect.
  7734. if (RT && RT->getDecl()->hasFlexibleArrayMember())
  7735. return getIndirectResult(Ty, /*ByVal=*/true, State);
  7736. // Ignore empty structs/unions.
  7737. if (isEmptyRecord(getContext(), Ty, true))
  7738. return ABIArgInfo::getIgnore();
  7739. llvm::LLVMContext &LLVMContext = getVMContext();
  7740. unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
  7741. if (SizeInRegs <= State.FreeRegs) {
  7742. llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
  7743. SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
  7744. llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
  7745. State.FreeRegs -= SizeInRegs;
  7746. return ABIArgInfo::getDirectInReg(Result);
  7747. } else {
  7748. State.FreeRegs = 0;
  7749. }
  7750. return getIndirectResult(Ty, true, State);
  7751. }
  7752. // Treat an enum type as its underlying type.
  7753. if (const auto *EnumTy = Ty->getAs<EnumType>())
  7754. Ty = EnumTy->getDecl()->getIntegerType();
  7755. bool InReg = shouldUseInReg(Ty, State);
  7756. // Don't pass >64 bit integers in registers.
  7757. if (const auto *EIT = Ty->getAs<BitIntType>())
  7758. if (EIT->getNumBits() > 64)
  7759. return getIndirectResult(Ty, /*ByVal=*/true, State);
  7760. if (isPromotableIntegerTypeForABI(Ty)) {
  7761. if (InReg)
  7762. return ABIArgInfo::getDirectInReg();
  7763. return ABIArgInfo::getExtend(Ty);
  7764. }
  7765. if (InReg)
  7766. return ABIArgInfo::getDirectInReg();
  7767. return ABIArgInfo::getDirect();
  7768. }
  7769. namespace {
  7770. class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
  7771. public:
  7772. LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  7773. : TargetCodeGenInfo(std::make_unique<LanaiABIInfo>(CGT)) {}
  7774. };
  7775. }
  7776. //===----------------------------------------------------------------------===//
  7777. // AMDGPU ABI Implementation
  7778. //===----------------------------------------------------------------------===//
  7779. namespace {
  7780. class AMDGPUABIInfo final : public DefaultABIInfo {
  7781. private:
  7782. static const unsigned MaxNumRegsForArgsRet = 16;
  7783. unsigned numRegsForType(QualType Ty) const;
  7784. bool isHomogeneousAggregateBaseType(QualType Ty) const override;
  7785. bool isHomogeneousAggregateSmallEnough(const Type *Base,
  7786. uint64_t Members) const override;
  7787. // Coerce HIP scalar pointer arguments from generic pointers to global ones.
  7788. llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS,
  7789. unsigned ToAS) const {
  7790. // Single value types.
  7791. auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(Ty);
  7792. if (PtrTy && PtrTy->getAddressSpace() == FromAS)
  7793. return llvm::PointerType::getWithSamePointeeType(PtrTy, ToAS);
  7794. return Ty;
  7795. }
  7796. public:
  7797. explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
  7798. DefaultABIInfo(CGT) {}
  7799. ABIArgInfo classifyReturnType(QualType RetTy) const;
  7800. ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
  7801. ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
  7802. void computeInfo(CGFunctionInfo &FI) const override;
  7803. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  7804. QualType Ty) const override;
  7805. };
  7806. bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
  7807. return true;
  7808. }
  7809. bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
  7810. const Type *Base, uint64_t Members) const {
  7811. uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
  7812. // Homogeneous Aggregates may occupy at most 16 registers.
  7813. return Members * NumRegs <= MaxNumRegsForArgsRet;
  7814. }
  7815. /// Estimate number of registers the type will use when passed in registers.
  7816. unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
  7817. unsigned NumRegs = 0;
  7818. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  7819. // Compute from the number of elements. The reported size is based on the
  7820. // in-memory size, which includes the padding 4th element for 3-vectors.
  7821. QualType EltTy = VT->getElementType();
  7822. unsigned EltSize = getContext().getTypeSize(EltTy);
  7823. // 16-bit element vectors should be passed as packed.
  7824. if (EltSize == 16)
  7825. return (VT->getNumElements() + 1) / 2;
  7826. unsigned EltNumRegs = (EltSize + 31) / 32;
  7827. return EltNumRegs * VT->getNumElements();
  7828. }
  7829. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  7830. const RecordDecl *RD = RT->getDecl();
  7831. assert(!RD->hasFlexibleArrayMember());
  7832. for (const FieldDecl *Field : RD->fields()) {
  7833. QualType FieldTy = Field->getType();
  7834. NumRegs += numRegsForType(FieldTy);
  7835. }
  7836. return NumRegs;
  7837. }
  7838. return (getContext().getTypeSize(Ty) + 31) / 32;
  7839. }
  7840. void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
  7841. llvm::CallingConv::ID CC = FI.getCallingConvention();
  7842. if (!getCXXABI().classifyReturnType(FI))
  7843. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  7844. unsigned NumRegsLeft = MaxNumRegsForArgsRet;
  7845. for (auto &Arg : FI.arguments()) {
  7846. if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
  7847. Arg.info = classifyKernelArgumentType(Arg.type);
  7848. } else {
  7849. Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
  7850. }
  7851. }
  7852. }
  7853. Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  7854. QualType Ty) const {
  7855. llvm_unreachable("AMDGPU does not support varargs");
  7856. }
  7857. ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
  7858. if (isAggregateTypeForABI(RetTy)) {
  7859. // Records with non-trivial destructors/copy-constructors should not be
  7860. // returned by value.
  7861. if (!getRecordArgABI(RetTy, getCXXABI())) {
  7862. // Ignore empty structs/unions.
  7863. if (isEmptyRecord(getContext(), RetTy, true))
  7864. return ABIArgInfo::getIgnore();
  7865. // Lower single-element structs to just return a regular value.
  7866. if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
  7867. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  7868. if (const RecordType *RT = RetTy->getAs<RecordType>()) {
  7869. const RecordDecl *RD = RT->getDecl();
  7870. if (RD->hasFlexibleArrayMember())
  7871. return DefaultABIInfo::classifyReturnType(RetTy);
  7872. }
  7873. // Pack aggregates <= 4 bytes into single VGPR or pair.
  7874. uint64_t Size = getContext().getTypeSize(RetTy);
  7875. if (Size <= 16)
  7876. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  7877. if (Size <= 32)
  7878. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  7879. if (Size <= 64) {
  7880. llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
  7881. return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
  7882. }
  7883. if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
  7884. return ABIArgInfo::getDirect();
  7885. }
  7886. }
  7887. // Otherwise just do the default thing.
  7888. return DefaultABIInfo::classifyReturnType(RetTy);
  7889. }
  7890. /// For kernels all parameters are really passed in a special buffer. It doesn't
  7891. /// make sense to pass anything byval, so everything must be direct.
  7892. ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
  7893. Ty = useFirstFieldIfTransparentUnion(Ty);
  7894. // TODO: Can we omit empty structs?
  7895. if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
  7896. Ty = QualType(SeltTy, 0);
  7897. llvm::Type *OrigLTy = CGT.ConvertType(Ty);
  7898. llvm::Type *LTy = OrigLTy;
  7899. if (getContext().getLangOpts().HIP) {
  7900. LTy = coerceKernelArgumentType(
  7901. OrigLTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default),
  7902. /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device));
  7903. }
  7904. // FIXME: Should also use this for OpenCL, but it requires addressing the
  7905. // problem of kernels being called.
  7906. //
  7907. // FIXME: This doesn't apply the optimization of coercing pointers in structs
  7908. // to global address space when using byref. This would require implementing a
  7909. // new kind of coercion of the in-memory type when for indirect arguments.
  7910. if (!getContext().getLangOpts().OpenCL && LTy == OrigLTy &&
  7911. isAggregateTypeForABI(Ty)) {
  7912. return ABIArgInfo::getIndirectAliased(
  7913. getContext().getTypeAlignInChars(Ty),
  7914. getContext().getTargetAddressSpace(LangAS::opencl_constant),
  7915. false /*Realign*/, nullptr /*Padding*/);
  7916. }
  7917. // If we set CanBeFlattened to true, CodeGen will expand the struct to its
  7918. // individual elements, which confuses the Clover OpenCL backend; therefore we
  7919. // have to set it to false here. Other args of getDirect() are just defaults.
  7920. return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
  7921. }
  7922. ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
  7923. unsigned &NumRegsLeft) const {
  7924. assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
  7925. Ty = useFirstFieldIfTransparentUnion(Ty);
  7926. if (isAggregateTypeForABI(Ty)) {
  7927. // Records with non-trivial destructors/copy-constructors should not be
  7928. // passed by value.
  7929. if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
  7930. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  7931. // Ignore empty structs/unions.
  7932. if (isEmptyRecord(getContext(), Ty, true))
  7933. return ABIArgInfo::getIgnore();
  7934. // Lower single-element structs to just pass a regular value. TODO: We
  7935. // could do reasonable-size multiple-element structs too, using getExpand(),
  7936. // though watch out for things like bitfields.
  7937. if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
  7938. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  7939. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  7940. const RecordDecl *RD = RT->getDecl();
  7941. if (RD->hasFlexibleArrayMember())
  7942. return DefaultABIInfo::classifyArgumentType(Ty);
  7943. }
  7944. // Pack aggregates <= 8 bytes into single VGPR or pair.
  7945. uint64_t Size = getContext().getTypeSize(Ty);
  7946. if (Size <= 64) {
  7947. unsigned NumRegs = (Size + 31) / 32;
  7948. NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
  7949. if (Size <= 16)
  7950. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  7951. if (Size <= 32)
  7952. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  7953. // XXX: Should this be i64 instead, and should the limit increase?
  7954. llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
  7955. return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
  7956. }
  7957. if (NumRegsLeft > 0) {
  7958. unsigned NumRegs = numRegsForType(Ty);
  7959. if (NumRegsLeft >= NumRegs) {
  7960. NumRegsLeft -= NumRegs;
  7961. return ABIArgInfo::getDirect();
  7962. }
  7963. }
  7964. }
  7965. // Otherwise just do the default thing.
  7966. ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
  7967. if (!ArgInfo.isIndirect()) {
  7968. unsigned NumRegs = numRegsForType(Ty);
  7969. NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
  7970. }
  7971. return ArgInfo;
  7972. }
  7973. class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
  7974. public:
  7975. AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
  7976. : TargetCodeGenInfo(std::make_unique<AMDGPUABIInfo>(CGT)) {}
  7977. void setFunctionDeclAttributes(const FunctionDecl *FD, llvm::Function *F,
  7978. CodeGenModule &CGM) const;
  7979. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  7980. CodeGen::CodeGenModule &M) const override;
  7981. unsigned getOpenCLKernelCallingConv() const override;
  7982. llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
  7983. llvm::PointerType *T, QualType QT) const override;
  7984. LangAS getASTAllocaAddressSpace() const override {
  7985. return getLangASFromTargetAS(
  7986. getABIInfo().getDataLayout().getAllocaAddrSpace());
  7987. }
  7988. LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
  7989. const VarDecl *D) const override;
  7990. llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
  7991. SyncScope Scope,
  7992. llvm::AtomicOrdering Ordering,
  7993. llvm::LLVMContext &Ctx) const override;
  7994. llvm::Function *
  7995. createEnqueuedBlockKernel(CodeGenFunction &CGF,
  7996. llvm::Function *BlockInvokeFunc,
  7997. llvm::Type *BlockTy) const override;
  7998. bool shouldEmitStaticExternCAliases() const override;
  7999. void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
  8000. };
  8001. }
  8002. static bool requiresAMDGPUProtectedVisibility(const Decl *D,
  8003. llvm::GlobalValue *GV) {
  8004. if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
  8005. return false;
  8006. return D->hasAttr<OpenCLKernelAttr>() ||
  8007. (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
  8008. (isa<VarDecl>(D) &&
  8009. (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
  8010. cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
  8011. cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType()));
  8012. }
  8013. void AMDGPUTargetCodeGenInfo::setFunctionDeclAttributes(
  8014. const FunctionDecl *FD, llvm::Function *F, CodeGenModule &M) const {
  8015. const auto *ReqdWGS =
  8016. M.getLangOpts().OpenCL ? FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
  8017. const bool IsOpenCLKernel =
  8018. M.getLangOpts().OpenCL && FD->hasAttr<OpenCLKernelAttr>();
  8019. const bool IsHIPKernel = M.getLangOpts().HIP && FD->hasAttr<CUDAGlobalAttr>();
  8020. const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
  8021. if (ReqdWGS || FlatWGS) {
  8022. unsigned Min = 0;
  8023. unsigned Max = 0;
  8024. if (FlatWGS) {
  8025. Min = FlatWGS->getMin()
  8026. ->EvaluateKnownConstInt(M.getContext())
  8027. .getExtValue();
  8028. Max = FlatWGS->getMax()
  8029. ->EvaluateKnownConstInt(M.getContext())
  8030. .getExtValue();
  8031. }
  8032. if (ReqdWGS && Min == 0 && Max == 0)
  8033. Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
  8034. if (Min != 0) {
  8035. assert(Min <= Max && "Min must be less than or equal Max");
  8036. std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
  8037. F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
  8038. } else
  8039. assert(Max == 0 && "Max must be zero");
  8040. } else if (IsOpenCLKernel || IsHIPKernel) {
  8041. // By default, restrict the maximum size to a value specified by
  8042. // --gpu-max-threads-per-block=n or its default value for HIP.
  8043. const unsigned OpenCLDefaultMaxWorkGroupSize = 256;
  8044. const unsigned DefaultMaxWorkGroupSize =
  8045. IsOpenCLKernel ? OpenCLDefaultMaxWorkGroupSize
  8046. : M.getLangOpts().GPUMaxThreadsPerBlock;
  8047. std::string AttrVal =
  8048. std::string("1,") + llvm::utostr(DefaultMaxWorkGroupSize);
  8049. F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
  8050. }
  8051. if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
  8052. unsigned Min =
  8053. Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue();
  8054. unsigned Max = Attr->getMax() ? Attr->getMax()
  8055. ->EvaluateKnownConstInt(M.getContext())
  8056. .getExtValue()
  8057. : 0;
  8058. if (Min != 0) {
  8059. assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
  8060. std::string AttrVal = llvm::utostr(Min);
  8061. if (Max != 0)
  8062. AttrVal = AttrVal + "," + llvm::utostr(Max);
  8063. F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
  8064. } else
  8065. assert(Max == 0 && "Max must be zero");
  8066. }
  8067. if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
  8068. unsigned NumSGPR = Attr->getNumSGPR();
  8069. if (NumSGPR != 0)
  8070. F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
  8071. }
  8072. if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
  8073. uint32_t NumVGPR = Attr->getNumVGPR();
  8074. if (NumVGPR != 0)
  8075. F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
  8076. }
  8077. }
  8078. void AMDGPUTargetCodeGenInfo::setTargetAttributes(
  8079. const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
  8080. if (requiresAMDGPUProtectedVisibility(D, GV)) {
  8081. GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
  8082. GV->setDSOLocal(true);
  8083. }
  8084. if (GV->isDeclaration())
  8085. return;
  8086. llvm::Function *F = dyn_cast<llvm::Function>(GV);
  8087. if (!F)
  8088. return;
  8089. const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
  8090. if (FD)
  8091. setFunctionDeclAttributes(FD, F, M);
  8092. const bool IsHIPKernel =
  8093. M.getLangOpts().HIP && FD && FD->hasAttr<CUDAGlobalAttr>();
  8094. const bool IsOpenMPkernel =
  8095. M.getLangOpts().OpenMPIsDevice &&
  8096. (F->getCallingConv() == llvm::CallingConv::AMDGPU_KERNEL);
  8097. // TODO: This should be moved to language specific attributes instead.
  8098. if (IsHIPKernel || IsOpenMPkernel)
  8099. F->addFnAttr("uniform-work-group-size", "true");
  8100. if (M.getContext().getTargetInfo().allowAMDGPUUnsafeFPAtomics())
  8101. F->addFnAttr("amdgpu-unsafe-fp-atomics", "true");
  8102. if (!getABIInfo().getCodeGenOpts().EmitIEEENaNCompliantInsts)
  8103. F->addFnAttr("amdgpu-ieee", "false");
  8104. }
  8105. unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
  8106. return llvm::CallingConv::AMDGPU_KERNEL;
  8107. }
  8108. // Currently LLVM assumes null pointers always have value 0,
  8109. // which results in incorrectly transformed IR. Therefore, instead of
  8110. // emitting null pointers in private and local address spaces, a null
  8111. // pointer in generic address space is emitted which is casted to a
  8112. // pointer in local or private address space.
  8113. llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
  8114. const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
  8115. QualType QT) const {
  8116. if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
  8117. return llvm::ConstantPointerNull::get(PT);
  8118. auto &Ctx = CGM.getContext();
  8119. auto NPT = llvm::PointerType::getWithSamePointeeType(
  8120. PT, Ctx.getTargetAddressSpace(LangAS::opencl_generic));
  8121. return llvm::ConstantExpr::getAddrSpaceCast(
  8122. llvm::ConstantPointerNull::get(NPT), PT);
  8123. }
  8124. LangAS
  8125. AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
  8126. const VarDecl *D) const {
  8127. assert(!CGM.getLangOpts().OpenCL &&
  8128. !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
  8129. "Address space agnostic languages only");
  8130. LangAS DefaultGlobalAS = getLangASFromTargetAS(
  8131. CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
  8132. if (!D)
  8133. return DefaultGlobalAS;
  8134. LangAS AddrSpace = D->getType().getAddressSpace();
  8135. assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace));
  8136. if (AddrSpace != LangAS::Default)
  8137. return AddrSpace;
  8138. // Only promote to address space 4 if VarDecl has constant initialization.
  8139. if (CGM.isTypeConstant(D->getType(), false) &&
  8140. D->hasConstantInitialization()) {
  8141. if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
  8142. return *ConstAS;
  8143. }
  8144. return DefaultGlobalAS;
  8145. }
  8146. llvm::SyncScope::ID
  8147. AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
  8148. SyncScope Scope,
  8149. llvm::AtomicOrdering Ordering,
  8150. llvm::LLVMContext &Ctx) const {
  8151. std::string Name;
  8152. switch (Scope) {
  8153. case SyncScope::HIPSingleThread:
  8154. Name = "singlethread";
  8155. break;
  8156. case SyncScope::HIPWavefront:
  8157. case SyncScope::OpenCLSubGroup:
  8158. Name = "wavefront";
  8159. break;
  8160. case SyncScope::HIPWorkgroup:
  8161. case SyncScope::OpenCLWorkGroup:
  8162. Name = "workgroup";
  8163. break;
  8164. case SyncScope::HIPAgent:
  8165. case SyncScope::OpenCLDevice:
  8166. Name = "agent";
  8167. break;
  8168. case SyncScope::HIPSystem:
  8169. case SyncScope::OpenCLAllSVMDevices:
  8170. Name = "";
  8171. break;
  8172. }
  8173. if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) {
  8174. if (!Name.empty())
  8175. Name = Twine(Twine(Name) + Twine("-")).str();
  8176. Name = Twine(Twine(Name) + Twine("one-as")).str();
  8177. }
  8178. return Ctx.getOrInsertSyncScopeID(Name);
  8179. }
  8180. bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
  8181. return false;
  8182. }
  8183. void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention(
  8184. const FunctionType *&FT) const {
  8185. FT = getABIInfo().getContext().adjustFunctionType(
  8186. FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
  8187. }
  8188. //===----------------------------------------------------------------------===//
  8189. // SPARC v8 ABI Implementation.
  8190. // Based on the SPARC Compliance Definition version 2.4.1.
  8191. //
  8192. // Ensures that complex values are passed in registers.
  8193. //
  8194. namespace {
  8195. class SparcV8ABIInfo : public DefaultABIInfo {
  8196. public:
  8197. SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  8198. private:
  8199. ABIArgInfo classifyReturnType(QualType RetTy) const;
  8200. void computeInfo(CGFunctionInfo &FI) const override;
  8201. };
  8202. } // end anonymous namespace
  8203. ABIArgInfo
  8204. SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
  8205. if (Ty->isAnyComplexType()) {
  8206. return ABIArgInfo::getDirect();
  8207. }
  8208. else {
  8209. return DefaultABIInfo::classifyReturnType(Ty);
  8210. }
  8211. }
  8212. void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  8213. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  8214. for (auto &Arg : FI.arguments())
  8215. Arg.info = classifyArgumentType(Arg.type);
  8216. }
  8217. namespace {
  8218. class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
  8219. public:
  8220. SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
  8221. : TargetCodeGenInfo(std::make_unique<SparcV8ABIInfo>(CGT)) {}
  8222. llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
  8223. llvm::Value *Address) const override {
  8224. int Offset;
  8225. if (isAggregateTypeForABI(CGF.CurFnInfo->getReturnType()))
  8226. Offset = 12;
  8227. else
  8228. Offset = 8;
  8229. return CGF.Builder.CreateGEP(CGF.Int8Ty, Address,
  8230. llvm::ConstantInt::get(CGF.Int32Ty, Offset));
  8231. }
  8232. llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
  8233. llvm::Value *Address) const override {
  8234. int Offset;
  8235. if (isAggregateTypeForABI(CGF.CurFnInfo->getReturnType()))
  8236. Offset = -12;
  8237. else
  8238. Offset = -8;
  8239. return CGF.Builder.CreateGEP(CGF.Int8Ty, Address,
  8240. llvm::ConstantInt::get(CGF.Int32Ty, Offset));
  8241. }
  8242. };
  8243. } // end anonymous namespace
  8244. //===----------------------------------------------------------------------===//
  8245. // SPARC v9 ABI Implementation.
  8246. // Based on the SPARC Compliance Definition version 2.4.1.
  8247. //
  8248. // Function arguments a mapped to a nominal "parameter array" and promoted to
  8249. // registers depending on their type. Each argument occupies 8 or 16 bytes in
  8250. // the array, structs larger than 16 bytes are passed indirectly.
  8251. //
  8252. // One case requires special care:
  8253. //
  8254. // struct mixed {
  8255. // int i;
  8256. // float f;
  8257. // };
  8258. //
  8259. // When a struct mixed is passed by value, it only occupies 8 bytes in the
  8260. // parameter array, but the int is passed in an integer register, and the float
  8261. // is passed in a floating point register. This is represented as two arguments
  8262. // with the LLVM IR inreg attribute:
  8263. //
  8264. // declare void f(i32 inreg %i, float inreg %f)
  8265. //
  8266. // The code generator will only allocate 4 bytes from the parameter array for
  8267. // the inreg arguments. All other arguments are allocated a multiple of 8
  8268. // bytes.
  8269. //
  8270. namespace {
  8271. class SparcV9ABIInfo : public ABIInfo {
  8272. public:
  8273. SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
  8274. private:
  8275. ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
  8276. void computeInfo(CGFunctionInfo &FI) const override;
  8277. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  8278. QualType Ty) const override;
  8279. // Coercion type builder for structs passed in registers. The coercion type
  8280. // serves two purposes:
  8281. //
  8282. // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
  8283. // in registers.
  8284. // 2. Expose aligned floating point elements as first-level elements, so the
  8285. // code generator knows to pass them in floating point registers.
  8286. //
  8287. // We also compute the InReg flag which indicates that the struct contains
  8288. // aligned 32-bit floats.
  8289. //
  8290. struct CoerceBuilder {
  8291. llvm::LLVMContext &Context;
  8292. const llvm::DataLayout &DL;
  8293. SmallVector<llvm::Type*, 8> Elems;
  8294. uint64_t Size;
  8295. bool InReg;
  8296. CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
  8297. : Context(c), DL(dl), Size(0), InReg(false) {}
  8298. // Pad Elems with integers until Size is ToSize.
  8299. void pad(uint64_t ToSize) {
  8300. assert(ToSize >= Size && "Cannot remove elements");
  8301. if (ToSize == Size)
  8302. return;
  8303. // Finish the current 64-bit word.
  8304. uint64_t Aligned = llvm::alignTo(Size, 64);
  8305. if (Aligned > Size && Aligned <= ToSize) {
  8306. Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
  8307. Size = Aligned;
  8308. }
  8309. // Add whole 64-bit words.
  8310. while (Size + 64 <= ToSize) {
  8311. Elems.push_back(llvm::Type::getInt64Ty(Context));
  8312. Size += 64;
  8313. }
  8314. // Final in-word padding.
  8315. if (Size < ToSize) {
  8316. Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
  8317. Size = ToSize;
  8318. }
  8319. }
  8320. // Add a floating point element at Offset.
  8321. void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
  8322. // Unaligned floats are treated as integers.
  8323. if (Offset % Bits)
  8324. return;
  8325. // The InReg flag is only required if there are any floats < 64 bits.
  8326. if (Bits < 64)
  8327. InReg = true;
  8328. pad(Offset);
  8329. Elems.push_back(Ty);
  8330. Size = Offset + Bits;
  8331. }
  8332. // Add a struct type to the coercion type, starting at Offset (in bits).
  8333. void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
  8334. const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
  8335. for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
  8336. llvm::Type *ElemTy = StrTy->getElementType(i);
  8337. uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
  8338. switch (ElemTy->getTypeID()) {
  8339. case llvm::Type::StructTyID:
  8340. addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
  8341. break;
  8342. case llvm::Type::FloatTyID:
  8343. addFloat(ElemOffset, ElemTy, 32);
  8344. break;
  8345. case llvm::Type::DoubleTyID:
  8346. addFloat(ElemOffset, ElemTy, 64);
  8347. break;
  8348. case llvm::Type::FP128TyID:
  8349. addFloat(ElemOffset, ElemTy, 128);
  8350. break;
  8351. case llvm::Type::PointerTyID:
  8352. if (ElemOffset % 64 == 0) {
  8353. pad(ElemOffset);
  8354. Elems.push_back(ElemTy);
  8355. Size += 64;
  8356. }
  8357. break;
  8358. default:
  8359. break;
  8360. }
  8361. }
  8362. }
  8363. // Check if Ty is a usable substitute for the coercion type.
  8364. bool isUsableType(llvm::StructType *Ty) const {
  8365. return llvm::ArrayRef(Elems) == Ty->elements();
  8366. }
  8367. // Get the coercion type as a literal struct type.
  8368. llvm::Type *getType() const {
  8369. if (Elems.size() == 1)
  8370. return Elems.front();
  8371. else
  8372. return llvm::StructType::get(Context, Elems);
  8373. }
  8374. };
  8375. };
  8376. } // end anonymous namespace
  8377. ABIArgInfo
  8378. SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
  8379. if (Ty->isVoidType())
  8380. return ABIArgInfo::getIgnore();
  8381. uint64_t Size = getContext().getTypeSize(Ty);
  8382. // Anything too big to fit in registers is passed with an explicit indirect
  8383. // pointer / sret pointer.
  8384. if (Size > SizeLimit)
  8385. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  8386. // Treat an enum type as its underlying type.
  8387. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  8388. Ty = EnumTy->getDecl()->getIntegerType();
  8389. // Integer types smaller than a register are extended.
  8390. if (Size < 64 && Ty->isIntegerType())
  8391. return ABIArgInfo::getExtend(Ty);
  8392. if (const auto *EIT = Ty->getAs<BitIntType>())
  8393. if (EIT->getNumBits() < 64)
  8394. return ABIArgInfo::getExtend(Ty);
  8395. // Other non-aggregates go in registers.
  8396. if (!isAggregateTypeForABI(Ty))
  8397. return ABIArgInfo::getDirect();
  8398. // If a C++ object has either a non-trivial copy constructor or a non-trivial
  8399. // destructor, it is passed with an explicit indirect pointer / sret pointer.
  8400. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
  8401. return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
  8402. // This is a small aggregate type that should be passed in registers.
  8403. // Build a coercion type from the LLVM struct type.
  8404. llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
  8405. if (!StrTy)
  8406. return ABIArgInfo::getDirect();
  8407. CoerceBuilder CB(getVMContext(), getDataLayout());
  8408. CB.addStruct(0, StrTy);
  8409. CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
  8410. // Try to use the original type for coercion.
  8411. llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
  8412. if (CB.InReg)
  8413. return ABIArgInfo::getDirectInReg(CoerceTy);
  8414. else
  8415. return ABIArgInfo::getDirect(CoerceTy);
  8416. }
  8417. Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  8418. QualType Ty) const {
  8419. ABIArgInfo AI = classifyType(Ty, 16 * 8);
  8420. llvm::Type *ArgTy = CGT.ConvertType(Ty);
  8421. if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
  8422. AI.setCoerceToType(ArgTy);
  8423. CharUnits SlotSize = CharUnits::fromQuantity(8);
  8424. CGBuilderTy &Builder = CGF.Builder;
  8425. Address Addr = Address(Builder.CreateLoad(VAListAddr, "ap.cur"),
  8426. getVAListElementType(CGF), SlotSize);
  8427. llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
  8428. auto TypeInfo = getContext().getTypeInfoInChars(Ty);
  8429. Address ArgAddr = Address::invalid();
  8430. CharUnits Stride;
  8431. switch (AI.getKind()) {
  8432. case ABIArgInfo::Expand:
  8433. case ABIArgInfo::CoerceAndExpand:
  8434. case ABIArgInfo::InAlloca:
  8435. llvm_unreachable("Unsupported ABI kind for va_arg");
  8436. case ABIArgInfo::Extend: {
  8437. Stride = SlotSize;
  8438. CharUnits Offset = SlotSize - TypeInfo.Width;
  8439. ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
  8440. break;
  8441. }
  8442. case ABIArgInfo::Direct: {
  8443. auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
  8444. Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
  8445. ArgAddr = Addr;
  8446. break;
  8447. }
  8448. case ABIArgInfo::Indirect:
  8449. case ABIArgInfo::IndirectAliased:
  8450. Stride = SlotSize;
  8451. ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
  8452. ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), ArgTy,
  8453. TypeInfo.Align);
  8454. break;
  8455. case ABIArgInfo::Ignore:
  8456. return Address(llvm::UndefValue::get(ArgPtrTy), ArgTy, TypeInfo.Align);
  8457. }
  8458. // Update VAList.
  8459. Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next");
  8460. Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
  8461. return Builder.CreateElementBitCast(ArgAddr, ArgTy, "arg.addr");
  8462. }
  8463. void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  8464. FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
  8465. for (auto &I : FI.arguments())
  8466. I.info = classifyType(I.type, 16 * 8);
  8467. }
  8468. namespace {
  8469. class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
  8470. public:
  8471. SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
  8472. : TargetCodeGenInfo(std::make_unique<SparcV9ABIInfo>(CGT)) {}
  8473. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
  8474. return 14;
  8475. }
  8476. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  8477. llvm::Value *Address) const override;
  8478. llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
  8479. llvm::Value *Address) const override {
  8480. return CGF.Builder.CreateGEP(CGF.Int8Ty, Address,
  8481. llvm::ConstantInt::get(CGF.Int32Ty, 8));
  8482. }
  8483. llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
  8484. llvm::Value *Address) const override {
  8485. return CGF.Builder.CreateGEP(CGF.Int8Ty, Address,
  8486. llvm::ConstantInt::get(CGF.Int32Ty, -8));
  8487. }
  8488. };
  8489. } // end anonymous namespace
  8490. bool
  8491. SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  8492. llvm::Value *Address) const {
  8493. // This is calculated from the LLVM and GCC tables and verified
  8494. // against gcc output. AFAIK all ABIs use the same encoding.
  8495. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  8496. llvm::IntegerType *i8 = CGF.Int8Ty;
  8497. llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
  8498. llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
  8499. // 0-31: the 8-byte general-purpose registers
  8500. AssignToArrayRange(Builder, Address, Eight8, 0, 31);
  8501. // 32-63: f0-31, the 4-byte floating-point registers
  8502. AssignToArrayRange(Builder, Address, Four8, 32, 63);
  8503. // Y = 64
  8504. // PSR = 65
  8505. // WIM = 66
  8506. // TBR = 67
  8507. // PC = 68
  8508. // NPC = 69
  8509. // FSR = 70
  8510. // CSR = 71
  8511. AssignToArrayRange(Builder, Address, Eight8, 64, 71);
  8512. // 72-87: d0-15, the 8-byte floating-point registers
  8513. AssignToArrayRange(Builder, Address, Eight8, 72, 87);
  8514. return false;
  8515. }
  8516. // ARC ABI implementation.
  8517. namespace {
  8518. class ARCABIInfo : public DefaultABIInfo {
  8519. public:
  8520. using DefaultABIInfo::DefaultABIInfo;
  8521. private:
  8522. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  8523. QualType Ty) const override;
  8524. void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const {
  8525. if (!State.FreeRegs)
  8526. return;
  8527. if (Info.isIndirect() && Info.getInReg())
  8528. State.FreeRegs--;
  8529. else if (Info.isDirect() && Info.getInReg()) {
  8530. unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32;
  8531. if (sz < State.FreeRegs)
  8532. State.FreeRegs -= sz;
  8533. else
  8534. State.FreeRegs = 0;
  8535. }
  8536. }
  8537. void computeInfo(CGFunctionInfo &FI) const override {
  8538. CCState State(FI);
  8539. // ARC uses 8 registers to pass arguments.
  8540. State.FreeRegs = 8;
  8541. if (!getCXXABI().classifyReturnType(FI))
  8542. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  8543. updateState(FI.getReturnInfo(), FI.getReturnType(), State);
  8544. for (auto &I : FI.arguments()) {
  8545. I.info = classifyArgumentType(I.type, State.FreeRegs);
  8546. updateState(I.info, I.type, State);
  8547. }
  8548. }
  8549. ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const;
  8550. ABIArgInfo getIndirectByValue(QualType Ty) const;
  8551. ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const;
  8552. ABIArgInfo classifyReturnType(QualType RetTy) const;
  8553. };
  8554. class ARCTargetCodeGenInfo : public TargetCodeGenInfo {
  8555. public:
  8556. ARCTargetCodeGenInfo(CodeGenTypes &CGT)
  8557. : TargetCodeGenInfo(std::make_unique<ARCABIInfo>(CGT)) {}
  8558. };
  8559. ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const {
  8560. return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) :
  8561. getNaturalAlignIndirect(Ty, false);
  8562. }
  8563. ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const {
  8564. // Compute the byval alignment.
  8565. const unsigned MinABIStackAlignInBytes = 4;
  8566. unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
  8567. return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
  8568. TypeAlign > MinABIStackAlignInBytes);
  8569. }
  8570. Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  8571. QualType Ty) const {
  8572. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
  8573. getContext().getTypeInfoInChars(Ty),
  8574. CharUnits::fromQuantity(4), true);
  8575. }
  8576. ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty,
  8577. uint8_t FreeRegs) const {
  8578. // Handle the generic C++ ABI.
  8579. const RecordType *RT = Ty->getAs<RecordType>();
  8580. if (RT) {
  8581. CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
  8582. if (RAA == CGCXXABI::RAA_Indirect)
  8583. return getIndirectByRef(Ty, FreeRegs > 0);
  8584. if (RAA == CGCXXABI::RAA_DirectInMemory)
  8585. return getIndirectByValue(Ty);
  8586. }
  8587. // Treat an enum type as its underlying type.
  8588. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  8589. Ty = EnumTy->getDecl()->getIntegerType();
  8590. auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32;
  8591. if (isAggregateTypeForABI(Ty)) {
  8592. // Structures with flexible arrays are always indirect.
  8593. if (RT && RT->getDecl()->hasFlexibleArrayMember())
  8594. return getIndirectByValue(Ty);
  8595. // Ignore empty structs/unions.
  8596. if (isEmptyRecord(getContext(), Ty, true))
  8597. return ABIArgInfo::getIgnore();
  8598. llvm::LLVMContext &LLVMContext = getVMContext();
  8599. llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
  8600. SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
  8601. llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
  8602. return FreeRegs >= SizeInRegs ?
  8603. ABIArgInfo::getDirectInReg(Result) :
  8604. ABIArgInfo::getDirect(Result, 0, nullptr, false);
  8605. }
  8606. if (const auto *EIT = Ty->getAs<BitIntType>())
  8607. if (EIT->getNumBits() > 64)
  8608. return getIndirectByValue(Ty);
  8609. return isPromotableIntegerTypeForABI(Ty)
  8610. ? (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty)
  8611. : ABIArgInfo::getExtend(Ty))
  8612. : (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg()
  8613. : ABIArgInfo::getDirect());
  8614. }
  8615. ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const {
  8616. if (RetTy->isAnyComplexType())
  8617. return ABIArgInfo::getDirectInReg();
  8618. // Arguments of size > 4 registers are indirect.
  8619. auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32;
  8620. if (RetSize > 4)
  8621. return getIndirectByRef(RetTy, /*HasFreeRegs*/ true);
  8622. return DefaultABIInfo::classifyReturnType(RetTy);
  8623. }
  8624. } // End anonymous namespace.
  8625. //===----------------------------------------------------------------------===//
  8626. // XCore ABI Implementation
  8627. //===----------------------------------------------------------------------===//
  8628. namespace {
  8629. /// A SmallStringEnc instance is used to build up the TypeString by passing
  8630. /// it by reference between functions that append to it.
  8631. typedef llvm::SmallString<128> SmallStringEnc;
  8632. /// TypeStringCache caches the meta encodings of Types.
  8633. ///
  8634. /// The reason for caching TypeStrings is two fold:
  8635. /// 1. To cache a type's encoding for later uses;
  8636. /// 2. As a means to break recursive member type inclusion.
  8637. ///
  8638. /// A cache Entry can have a Status of:
  8639. /// NonRecursive: The type encoding is not recursive;
  8640. /// Recursive: The type encoding is recursive;
  8641. /// Incomplete: An incomplete TypeString;
  8642. /// IncompleteUsed: An incomplete TypeString that has been used in a
  8643. /// Recursive type encoding.
  8644. ///
  8645. /// A NonRecursive entry will have all of its sub-members expanded as fully
  8646. /// as possible. Whilst it may contain types which are recursive, the type
  8647. /// itself is not recursive and thus its encoding may be safely used whenever
  8648. /// the type is encountered.
  8649. ///
  8650. /// A Recursive entry will have all of its sub-members expanded as fully as
  8651. /// possible. The type itself is recursive and it may contain other types which
  8652. /// are recursive. The Recursive encoding must not be used during the expansion
  8653. /// of a recursive type's recursive branch. For simplicity the code uses
  8654. /// IncompleteCount to reject all usage of Recursive encodings for member types.
  8655. ///
  8656. /// An Incomplete entry is always a RecordType and only encodes its
  8657. /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
  8658. /// are placed into the cache during type expansion as a means to identify and
  8659. /// handle recursive inclusion of types as sub-members. If there is recursion
  8660. /// the entry becomes IncompleteUsed.
  8661. ///
  8662. /// During the expansion of a RecordType's members:
  8663. ///
  8664. /// If the cache contains a NonRecursive encoding for the member type, the
  8665. /// cached encoding is used;
  8666. ///
  8667. /// If the cache contains a Recursive encoding for the member type, the
  8668. /// cached encoding is 'Swapped' out, as it may be incorrect, and...
  8669. ///
  8670. /// If the member is a RecordType, an Incomplete encoding is placed into the
  8671. /// cache to break potential recursive inclusion of itself as a sub-member;
  8672. ///
  8673. /// Once a member RecordType has been expanded, its temporary incomplete
  8674. /// entry is removed from the cache. If a Recursive encoding was swapped out
  8675. /// it is swapped back in;
  8676. ///
  8677. /// If an incomplete entry is used to expand a sub-member, the incomplete
  8678. /// entry is marked as IncompleteUsed. The cache keeps count of how many
  8679. /// IncompleteUsed entries it currently contains in IncompleteUsedCount;
  8680. ///
  8681. /// If a member's encoding is found to be a NonRecursive or Recursive viz:
  8682. /// IncompleteUsedCount==0, the member's encoding is added to the cache.
  8683. /// Else the member is part of a recursive type and thus the recursion has
  8684. /// been exited too soon for the encoding to be correct for the member.
  8685. ///
  8686. class TypeStringCache {
  8687. enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
  8688. struct Entry {
  8689. std::string Str; // The encoded TypeString for the type.
  8690. enum Status State; // Information about the encoding in 'Str'.
  8691. std::string Swapped; // A temporary place holder for a Recursive encoding
  8692. // during the expansion of RecordType's members.
  8693. };
  8694. std::map<const IdentifierInfo *, struct Entry> Map;
  8695. unsigned IncompleteCount; // Number of Incomplete entries in the Map.
  8696. unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
  8697. public:
  8698. TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
  8699. void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
  8700. bool removeIncomplete(const IdentifierInfo *ID);
  8701. void addIfComplete(const IdentifierInfo *ID, StringRef Str,
  8702. bool IsRecursive);
  8703. StringRef lookupStr(const IdentifierInfo *ID);
  8704. };
  8705. /// TypeString encodings for enum & union fields must be order.
  8706. /// FieldEncoding is a helper for this ordering process.
  8707. class FieldEncoding {
  8708. bool HasName;
  8709. std::string Enc;
  8710. public:
  8711. FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
  8712. StringRef str() { return Enc; }
  8713. bool operator<(const FieldEncoding &rhs) const {
  8714. if (HasName != rhs.HasName) return HasName;
  8715. return Enc < rhs.Enc;
  8716. }
  8717. };
  8718. class XCoreABIInfo : public DefaultABIInfo {
  8719. public:
  8720. XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  8721. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  8722. QualType Ty) const override;
  8723. };
  8724. class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
  8725. mutable TypeStringCache TSC;
  8726. void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
  8727. const CodeGen::CodeGenModule &M) const;
  8728. public:
  8729. XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
  8730. : TargetCodeGenInfo(std::make_unique<XCoreABIInfo>(CGT)) {}
  8731. void emitTargetMetadata(CodeGen::CodeGenModule &CGM,
  8732. const llvm::MapVector<GlobalDecl, StringRef>
  8733. &MangledDeclNames) const override;
  8734. };
  8735. } // End anonymous namespace.
  8736. // TODO: this implementation is likely now redundant with the default
  8737. // EmitVAArg.
  8738. Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  8739. QualType Ty) const {
  8740. CGBuilderTy &Builder = CGF.Builder;
  8741. // Get the VAList.
  8742. CharUnits SlotSize = CharUnits::fromQuantity(4);
  8743. Address AP = Address(Builder.CreateLoad(VAListAddr),
  8744. getVAListElementType(CGF), SlotSize);
  8745. // Handle the argument.
  8746. ABIArgInfo AI = classifyArgumentType(Ty);
  8747. CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
  8748. llvm::Type *ArgTy = CGT.ConvertType(Ty);
  8749. if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
  8750. AI.setCoerceToType(ArgTy);
  8751. llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
  8752. Address Val = Address::invalid();
  8753. CharUnits ArgSize = CharUnits::Zero();
  8754. switch (AI.getKind()) {
  8755. case ABIArgInfo::Expand:
  8756. case ABIArgInfo::CoerceAndExpand:
  8757. case ABIArgInfo::InAlloca:
  8758. llvm_unreachable("Unsupported ABI kind for va_arg");
  8759. case ABIArgInfo::Ignore:
  8760. Val = Address(llvm::UndefValue::get(ArgPtrTy), ArgTy, TypeAlign);
  8761. ArgSize = CharUnits::Zero();
  8762. break;
  8763. case ABIArgInfo::Extend:
  8764. case ABIArgInfo::Direct:
  8765. Val = Builder.CreateElementBitCast(AP, ArgTy);
  8766. ArgSize = CharUnits::fromQuantity(
  8767. getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
  8768. ArgSize = ArgSize.alignTo(SlotSize);
  8769. break;
  8770. case ABIArgInfo::Indirect:
  8771. case ABIArgInfo::IndirectAliased:
  8772. Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
  8773. Val = Address(Builder.CreateLoad(Val), ArgTy, TypeAlign);
  8774. ArgSize = SlotSize;
  8775. break;
  8776. }
  8777. // Increment the VAList.
  8778. if (!ArgSize.isZero()) {
  8779. Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize);
  8780. Builder.CreateStore(APN.getPointer(), VAListAddr);
  8781. }
  8782. return Val;
  8783. }
  8784. /// During the expansion of a RecordType, an incomplete TypeString is placed
  8785. /// into the cache as a means to identify and break recursion.
  8786. /// If there is a Recursive encoding in the cache, it is swapped out and will
  8787. /// be reinserted by removeIncomplete().
  8788. /// All other types of encoding should have been used rather than arriving here.
  8789. void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
  8790. std::string StubEnc) {
  8791. if (!ID)
  8792. return;
  8793. Entry &E = Map[ID];
  8794. assert( (E.Str.empty() || E.State == Recursive) &&
  8795. "Incorrectly use of addIncomplete");
  8796. assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
  8797. E.Swapped.swap(E.Str); // swap out the Recursive
  8798. E.Str.swap(StubEnc);
  8799. E.State = Incomplete;
  8800. ++IncompleteCount;
  8801. }
  8802. /// Once the RecordType has been expanded, the temporary incomplete TypeString
  8803. /// must be removed from the cache.
  8804. /// If a Recursive was swapped out by addIncomplete(), it will be replaced.
  8805. /// Returns true if the RecordType was defined recursively.
  8806. bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
  8807. if (!ID)
  8808. return false;
  8809. auto I = Map.find(ID);
  8810. assert(I != Map.end() && "Entry not present");
  8811. Entry &E = I->second;
  8812. assert( (E.State == Incomplete ||
  8813. E.State == IncompleteUsed) &&
  8814. "Entry must be an incomplete type");
  8815. bool IsRecursive = false;
  8816. if (E.State == IncompleteUsed) {
  8817. // We made use of our Incomplete encoding, thus we are recursive.
  8818. IsRecursive = true;
  8819. --IncompleteUsedCount;
  8820. }
  8821. if (E.Swapped.empty())
  8822. Map.erase(I);
  8823. else {
  8824. // Swap the Recursive back.
  8825. E.Swapped.swap(E.Str);
  8826. E.Swapped.clear();
  8827. E.State = Recursive;
  8828. }
  8829. --IncompleteCount;
  8830. return IsRecursive;
  8831. }
  8832. /// Add the encoded TypeString to the cache only if it is NonRecursive or
  8833. /// Recursive (viz: all sub-members were expanded as fully as possible).
  8834. void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
  8835. bool IsRecursive) {
  8836. if (!ID || IncompleteUsedCount)
  8837. return; // No key or it is an incomplete sub-type so don't add.
  8838. Entry &E = Map[ID];
  8839. if (IsRecursive && !E.Str.empty()) {
  8840. assert(E.State==Recursive && E.Str.size() == Str.size() &&
  8841. "This is not the same Recursive entry");
  8842. // The parent container was not recursive after all, so we could have used
  8843. // this Recursive sub-member entry after all, but we assumed the worse when
  8844. // we started viz: IncompleteCount!=0.
  8845. return;
  8846. }
  8847. assert(E.Str.empty() && "Entry already present");
  8848. E.Str = Str.str();
  8849. E.State = IsRecursive? Recursive : NonRecursive;
  8850. }
  8851. /// Return a cached TypeString encoding for the ID. If there isn't one, or we
  8852. /// are recursively expanding a type (IncompleteCount != 0) and the cached
  8853. /// encoding is Recursive, return an empty StringRef.
  8854. StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
  8855. if (!ID)
  8856. return StringRef(); // We have no key.
  8857. auto I = Map.find(ID);
  8858. if (I == Map.end())
  8859. return StringRef(); // We have no encoding.
  8860. Entry &E = I->second;
  8861. if (E.State == Recursive && IncompleteCount)
  8862. return StringRef(); // We don't use Recursive encodings for member types.
  8863. if (E.State == Incomplete) {
  8864. // The incomplete type is being used to break out of recursion.
  8865. E.State = IncompleteUsed;
  8866. ++IncompleteUsedCount;
  8867. }
  8868. return E.Str;
  8869. }
  8870. /// The XCore ABI includes a type information section that communicates symbol
  8871. /// type information to the linker. The linker uses this information to verify
  8872. /// safety/correctness of things such as array bound and pointers et al.
  8873. /// The ABI only requires C (and XC) language modules to emit TypeStrings.
  8874. /// This type information (TypeString) is emitted into meta data for all global
  8875. /// symbols: definitions, declarations, functions & variables.
  8876. ///
  8877. /// The TypeString carries type, qualifier, name, size & value details.
  8878. /// Please see 'Tools Development Guide' section 2.16.2 for format details:
  8879. /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
  8880. /// The output is tested by test/CodeGen/xcore-stringtype.c.
  8881. ///
  8882. static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
  8883. const CodeGen::CodeGenModule &CGM,
  8884. TypeStringCache &TSC);
  8885. /// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
  8886. void XCoreTargetCodeGenInfo::emitTargetMD(
  8887. const Decl *D, llvm::GlobalValue *GV,
  8888. const CodeGen::CodeGenModule &CGM) const {
  8889. SmallStringEnc Enc;
  8890. if (getTypeString(Enc, D, CGM, TSC)) {
  8891. llvm::LLVMContext &Ctx = CGM.getModule().getContext();
  8892. llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
  8893. llvm::MDString::get(Ctx, Enc.str())};
  8894. llvm::NamedMDNode *MD =
  8895. CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
  8896. MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
  8897. }
  8898. }
  8899. void XCoreTargetCodeGenInfo::emitTargetMetadata(
  8900. CodeGen::CodeGenModule &CGM,
  8901. const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const {
  8902. // Warning, new MangledDeclNames may be appended within this loop.
  8903. // We rely on MapVector insertions adding new elements to the end
  8904. // of the container.
  8905. for (unsigned I = 0; I != MangledDeclNames.size(); ++I) {
  8906. auto Val = *(MangledDeclNames.begin() + I);
  8907. llvm::GlobalValue *GV = CGM.GetGlobalValue(Val.second);
  8908. if (GV) {
  8909. const Decl *D = Val.first.getDecl()->getMostRecentDecl();
  8910. emitTargetMD(D, GV, CGM);
  8911. }
  8912. }
  8913. }
  8914. //===----------------------------------------------------------------------===//
  8915. // Base ABI and target codegen info implementation common between SPIR and
  8916. // SPIR-V.
  8917. //===----------------------------------------------------------------------===//
  8918. namespace {
  8919. class CommonSPIRABIInfo : public DefaultABIInfo {
  8920. public:
  8921. CommonSPIRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) { setCCs(); }
  8922. private:
  8923. void setCCs();
  8924. };
  8925. class SPIRVABIInfo : public CommonSPIRABIInfo {
  8926. public:
  8927. SPIRVABIInfo(CodeGenTypes &CGT) : CommonSPIRABIInfo(CGT) {}
  8928. void computeInfo(CGFunctionInfo &FI) const override;
  8929. private:
  8930. ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
  8931. };
  8932. } // end anonymous namespace
  8933. namespace {
  8934. class CommonSPIRTargetCodeGenInfo : public TargetCodeGenInfo {
  8935. public:
  8936. CommonSPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  8937. : TargetCodeGenInfo(std::make_unique<CommonSPIRABIInfo>(CGT)) {}
  8938. CommonSPIRTargetCodeGenInfo(std::unique_ptr<ABIInfo> ABIInfo)
  8939. : TargetCodeGenInfo(std::move(ABIInfo)) {}
  8940. LangAS getASTAllocaAddressSpace() const override {
  8941. return getLangASFromTargetAS(
  8942. getABIInfo().getDataLayout().getAllocaAddrSpace());
  8943. }
  8944. unsigned getOpenCLKernelCallingConv() const override;
  8945. };
  8946. class SPIRVTargetCodeGenInfo : public CommonSPIRTargetCodeGenInfo {
  8947. public:
  8948. SPIRVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  8949. : CommonSPIRTargetCodeGenInfo(std::make_unique<SPIRVABIInfo>(CGT)) {}
  8950. void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
  8951. };
  8952. } // End anonymous namespace.
  8953. void CommonSPIRABIInfo::setCCs() {
  8954. assert(getRuntimeCC() == llvm::CallingConv::C);
  8955. RuntimeCC = llvm::CallingConv::SPIR_FUNC;
  8956. }
  8957. ABIArgInfo SPIRVABIInfo::classifyKernelArgumentType(QualType Ty) const {
  8958. if (getContext().getLangOpts().CUDAIsDevice) {
  8959. // Coerce pointer arguments with default address space to CrossWorkGroup
  8960. // pointers for HIPSPV/CUDASPV. When the language mode is HIP/CUDA, the
  8961. // SPIRTargetInfo maps cuda_device to SPIR-V's CrossWorkGroup address space.
  8962. llvm::Type *LTy = CGT.ConvertType(Ty);
  8963. auto DefaultAS = getContext().getTargetAddressSpace(LangAS::Default);
  8964. auto GlobalAS = getContext().getTargetAddressSpace(LangAS::cuda_device);
  8965. auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(LTy);
  8966. if (PtrTy && PtrTy->getAddressSpace() == DefaultAS) {
  8967. LTy = llvm::PointerType::getWithSamePointeeType(PtrTy, GlobalAS);
  8968. return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
  8969. }
  8970. // Force copying aggregate type in kernel arguments by value when
  8971. // compiling CUDA targeting SPIR-V. This is required for the object
  8972. // copied to be valid on the device.
  8973. // This behavior follows the CUDA spec
  8974. // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#global-function-argument-processing,
  8975. // and matches the NVPTX implementation.
  8976. if (isAggregateTypeForABI(Ty))
  8977. return getNaturalAlignIndirect(Ty, /* byval */ true);
  8978. }
  8979. return classifyArgumentType(Ty);
  8980. }
  8981. void SPIRVABIInfo::computeInfo(CGFunctionInfo &FI) const {
  8982. // The logic is same as in DefaultABIInfo with an exception on the kernel
  8983. // arguments handling.
  8984. llvm::CallingConv::ID CC = FI.getCallingConvention();
  8985. if (!getCXXABI().classifyReturnType(FI))
  8986. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  8987. for (auto &I : FI.arguments()) {
  8988. if (CC == llvm::CallingConv::SPIR_KERNEL) {
  8989. I.info = classifyKernelArgumentType(I.type);
  8990. } else {
  8991. I.info = classifyArgumentType(I.type);
  8992. }
  8993. }
  8994. }
  8995. namespace clang {
  8996. namespace CodeGen {
  8997. void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
  8998. if (CGM.getTarget().getTriple().isSPIRV())
  8999. SPIRVABIInfo(CGM.getTypes()).computeInfo(FI);
  9000. else
  9001. CommonSPIRABIInfo(CGM.getTypes()).computeInfo(FI);
  9002. }
  9003. }
  9004. }
  9005. unsigned CommonSPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
  9006. return llvm::CallingConv::SPIR_KERNEL;
  9007. }
  9008. void SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention(
  9009. const FunctionType *&FT) const {
  9010. // Convert HIP kernels to SPIR-V kernels.
  9011. if (getABIInfo().getContext().getLangOpts().HIP) {
  9012. FT = getABIInfo().getContext().adjustFunctionType(
  9013. FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
  9014. return;
  9015. }
  9016. }
  9017. static bool appendType(SmallStringEnc &Enc, QualType QType,
  9018. const CodeGen::CodeGenModule &CGM,
  9019. TypeStringCache &TSC);
  9020. /// Helper function for appendRecordType().
  9021. /// Builds a SmallVector containing the encoded field types in declaration
  9022. /// order.
  9023. static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
  9024. const RecordDecl *RD,
  9025. const CodeGen::CodeGenModule &CGM,
  9026. TypeStringCache &TSC) {
  9027. for (const auto *Field : RD->fields()) {
  9028. SmallStringEnc Enc;
  9029. Enc += "m(";
  9030. Enc += Field->getName();
  9031. Enc += "){";
  9032. if (Field->isBitField()) {
  9033. Enc += "b(";
  9034. llvm::raw_svector_ostream OS(Enc);
  9035. OS << Field->getBitWidthValue(CGM.getContext());
  9036. Enc += ':';
  9037. }
  9038. if (!appendType(Enc, Field->getType(), CGM, TSC))
  9039. return false;
  9040. if (Field->isBitField())
  9041. Enc += ')';
  9042. Enc += '}';
  9043. FE.emplace_back(!Field->getName().empty(), Enc);
  9044. }
  9045. return true;
  9046. }
  9047. /// Appends structure and union types to Enc and adds encoding to cache.
  9048. /// Recursively calls appendType (via extractFieldType) for each field.
  9049. /// Union types have their fields ordered according to the ABI.
  9050. static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
  9051. const CodeGen::CodeGenModule &CGM,
  9052. TypeStringCache &TSC, const IdentifierInfo *ID) {
  9053. // Append the cached TypeString if we have one.
  9054. StringRef TypeString = TSC.lookupStr(ID);
  9055. if (!TypeString.empty()) {
  9056. Enc += TypeString;
  9057. return true;
  9058. }
  9059. // Start to emit an incomplete TypeString.
  9060. size_t Start = Enc.size();
  9061. Enc += (RT->isUnionType()? 'u' : 's');
  9062. Enc += '(';
  9063. if (ID)
  9064. Enc += ID->getName();
  9065. Enc += "){";
  9066. // We collect all encoded fields and order as necessary.
  9067. bool IsRecursive = false;
  9068. const RecordDecl *RD = RT->getDecl()->getDefinition();
  9069. if (RD && !RD->field_empty()) {
  9070. // An incomplete TypeString stub is placed in the cache for this RecordType
  9071. // so that recursive calls to this RecordType will use it whilst building a
  9072. // complete TypeString for this RecordType.
  9073. SmallVector<FieldEncoding, 16> FE;
  9074. std::string StubEnc(Enc.substr(Start).str());
  9075. StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString.
  9076. TSC.addIncomplete(ID, std::move(StubEnc));
  9077. if (!extractFieldType(FE, RD, CGM, TSC)) {
  9078. (void) TSC.removeIncomplete(ID);
  9079. return false;
  9080. }
  9081. IsRecursive = TSC.removeIncomplete(ID);
  9082. // The ABI requires unions to be sorted but not structures.
  9083. // See FieldEncoding::operator< for sort algorithm.
  9084. if (RT->isUnionType())
  9085. llvm::sort(FE);
  9086. // We can now complete the TypeString.
  9087. unsigned E = FE.size();
  9088. for (unsigned I = 0; I != E; ++I) {
  9089. if (I)
  9090. Enc += ',';
  9091. Enc += FE[I].str();
  9092. }
  9093. }
  9094. Enc += '}';
  9095. TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
  9096. return true;
  9097. }
  9098. /// Appends enum types to Enc and adds the encoding to the cache.
  9099. static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
  9100. TypeStringCache &TSC,
  9101. const IdentifierInfo *ID) {
  9102. // Append the cached TypeString if we have one.
  9103. StringRef TypeString = TSC.lookupStr(ID);
  9104. if (!TypeString.empty()) {
  9105. Enc += TypeString;
  9106. return true;
  9107. }
  9108. size_t Start = Enc.size();
  9109. Enc += "e(";
  9110. if (ID)
  9111. Enc += ID->getName();
  9112. Enc += "){";
  9113. // We collect all encoded enumerations and order them alphanumerically.
  9114. if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
  9115. SmallVector<FieldEncoding, 16> FE;
  9116. for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
  9117. ++I) {
  9118. SmallStringEnc EnumEnc;
  9119. EnumEnc += "m(";
  9120. EnumEnc += I->getName();
  9121. EnumEnc += "){";
  9122. I->getInitVal().toString(EnumEnc);
  9123. EnumEnc += '}';
  9124. FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
  9125. }
  9126. llvm::sort(FE);
  9127. unsigned E = FE.size();
  9128. for (unsigned I = 0; I != E; ++I) {
  9129. if (I)
  9130. Enc += ',';
  9131. Enc += FE[I].str();
  9132. }
  9133. }
  9134. Enc += '}';
  9135. TSC.addIfComplete(ID, Enc.substr(Start), false);
  9136. return true;
  9137. }
  9138. /// Appends type's qualifier to Enc.
  9139. /// This is done prior to appending the type's encoding.
  9140. static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
  9141. // Qualifiers are emitted in alphabetical order.
  9142. static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
  9143. int Lookup = 0;
  9144. if (QT.isConstQualified())
  9145. Lookup += 1<<0;
  9146. if (QT.isRestrictQualified())
  9147. Lookup += 1<<1;
  9148. if (QT.isVolatileQualified())
  9149. Lookup += 1<<2;
  9150. Enc += Table[Lookup];
  9151. }
  9152. /// Appends built-in types to Enc.
  9153. static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
  9154. const char *EncType;
  9155. switch (BT->getKind()) {
  9156. case BuiltinType::Void:
  9157. EncType = "0";
  9158. break;
  9159. case BuiltinType::Bool:
  9160. EncType = "b";
  9161. break;
  9162. case BuiltinType::Char_U:
  9163. EncType = "uc";
  9164. break;
  9165. case BuiltinType::UChar:
  9166. EncType = "uc";
  9167. break;
  9168. case BuiltinType::SChar:
  9169. EncType = "sc";
  9170. break;
  9171. case BuiltinType::UShort:
  9172. EncType = "us";
  9173. break;
  9174. case BuiltinType::Short:
  9175. EncType = "ss";
  9176. break;
  9177. case BuiltinType::UInt:
  9178. EncType = "ui";
  9179. break;
  9180. case BuiltinType::Int:
  9181. EncType = "si";
  9182. break;
  9183. case BuiltinType::ULong:
  9184. EncType = "ul";
  9185. break;
  9186. case BuiltinType::Long:
  9187. EncType = "sl";
  9188. break;
  9189. case BuiltinType::ULongLong:
  9190. EncType = "ull";
  9191. break;
  9192. case BuiltinType::LongLong:
  9193. EncType = "sll";
  9194. break;
  9195. case BuiltinType::Float:
  9196. EncType = "ft";
  9197. break;
  9198. case BuiltinType::Double:
  9199. EncType = "d";
  9200. break;
  9201. case BuiltinType::LongDouble:
  9202. EncType = "ld";
  9203. break;
  9204. default:
  9205. return false;
  9206. }
  9207. Enc += EncType;
  9208. return true;
  9209. }
  9210. /// Appends a pointer encoding to Enc before calling appendType for the pointee.
  9211. static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
  9212. const CodeGen::CodeGenModule &CGM,
  9213. TypeStringCache &TSC) {
  9214. Enc += "p(";
  9215. if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
  9216. return false;
  9217. Enc += ')';
  9218. return true;
  9219. }
  9220. /// Appends array encoding to Enc before calling appendType for the element.
  9221. static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
  9222. const ArrayType *AT,
  9223. const CodeGen::CodeGenModule &CGM,
  9224. TypeStringCache &TSC, StringRef NoSizeEnc) {
  9225. if (AT->getSizeModifier() != ArrayType::Normal)
  9226. return false;
  9227. Enc += "a(";
  9228. if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
  9229. CAT->getSize().toStringUnsigned(Enc);
  9230. else
  9231. Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
  9232. Enc += ':';
  9233. // The Qualifiers should be attached to the type rather than the array.
  9234. appendQualifier(Enc, QT);
  9235. if (!appendType(Enc, AT->getElementType(), CGM, TSC))
  9236. return false;
  9237. Enc += ')';
  9238. return true;
  9239. }
  9240. /// Appends a function encoding to Enc, calling appendType for the return type
  9241. /// and the arguments.
  9242. static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
  9243. const CodeGen::CodeGenModule &CGM,
  9244. TypeStringCache &TSC) {
  9245. Enc += "f{";
  9246. if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
  9247. return false;
  9248. Enc += "}(";
  9249. if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
  9250. // N.B. we are only interested in the adjusted param types.
  9251. auto I = FPT->param_type_begin();
  9252. auto E = FPT->param_type_end();
  9253. if (I != E) {
  9254. do {
  9255. if (!appendType(Enc, *I, CGM, TSC))
  9256. return false;
  9257. ++I;
  9258. if (I != E)
  9259. Enc += ',';
  9260. } while (I != E);
  9261. if (FPT->isVariadic())
  9262. Enc += ",va";
  9263. } else {
  9264. if (FPT->isVariadic())
  9265. Enc += "va";
  9266. else
  9267. Enc += '0';
  9268. }
  9269. }
  9270. Enc += ')';
  9271. return true;
  9272. }
  9273. /// Handles the type's qualifier before dispatching a call to handle specific
  9274. /// type encodings.
  9275. static bool appendType(SmallStringEnc &Enc, QualType QType,
  9276. const CodeGen::CodeGenModule &CGM,
  9277. TypeStringCache &TSC) {
  9278. QualType QT = QType.getCanonicalType();
  9279. if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
  9280. // The Qualifiers should be attached to the type rather than the array.
  9281. // Thus we don't call appendQualifier() here.
  9282. return appendArrayType(Enc, QT, AT, CGM, TSC, "");
  9283. appendQualifier(Enc, QT);
  9284. if (const BuiltinType *BT = QT->getAs<BuiltinType>())
  9285. return appendBuiltinType(Enc, BT);
  9286. if (const PointerType *PT = QT->getAs<PointerType>())
  9287. return appendPointerType(Enc, PT, CGM, TSC);
  9288. if (const EnumType *ET = QT->getAs<EnumType>())
  9289. return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
  9290. if (const RecordType *RT = QT->getAsStructureType())
  9291. return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
  9292. if (const RecordType *RT = QT->getAsUnionType())
  9293. return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
  9294. if (const FunctionType *FT = QT->getAs<FunctionType>())
  9295. return appendFunctionType(Enc, FT, CGM, TSC);
  9296. return false;
  9297. }
  9298. static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
  9299. const CodeGen::CodeGenModule &CGM,
  9300. TypeStringCache &TSC) {
  9301. if (!D)
  9302. return false;
  9303. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  9304. if (FD->getLanguageLinkage() != CLanguageLinkage)
  9305. return false;
  9306. return appendType(Enc, FD->getType(), CGM, TSC);
  9307. }
  9308. if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
  9309. if (VD->getLanguageLinkage() != CLanguageLinkage)
  9310. return false;
  9311. QualType QT = VD->getType().getCanonicalType();
  9312. if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
  9313. // Global ArrayTypes are given a size of '*' if the size is unknown.
  9314. // The Qualifiers should be attached to the type rather than the array.
  9315. // Thus we don't call appendQualifier() here.
  9316. return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
  9317. }
  9318. return appendType(Enc, QT, CGM, TSC);
  9319. }
  9320. return false;
  9321. }
  9322. //===----------------------------------------------------------------------===//
  9323. // RISCV ABI Implementation
  9324. //===----------------------------------------------------------------------===//
  9325. namespace {
  9326. class RISCVABIInfo : public DefaultABIInfo {
  9327. private:
  9328. // Size of the integer ('x') registers in bits.
  9329. unsigned XLen;
  9330. // Size of the floating point ('f') registers in bits. Note that the target
  9331. // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target
  9332. // with soft float ABI has FLen==0).
  9333. unsigned FLen;
  9334. static const int NumArgGPRs = 8;
  9335. static const int NumArgFPRs = 8;
  9336. bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
  9337. llvm::Type *&Field1Ty,
  9338. CharUnits &Field1Off,
  9339. llvm::Type *&Field2Ty,
  9340. CharUnits &Field2Off) const;
  9341. public:
  9342. RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen)
  9343. : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {}
  9344. // DefaultABIInfo's classifyReturnType and classifyArgumentType are
  9345. // non-virtual, but computeInfo is virtual, so we overload it.
  9346. void computeInfo(CGFunctionInfo &FI) const override;
  9347. ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft,
  9348. int &ArgFPRsLeft) const;
  9349. ABIArgInfo classifyReturnType(QualType RetTy) const;
  9350. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  9351. QualType Ty) const override;
  9352. ABIArgInfo extendType(QualType Ty) const;
  9353. bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
  9354. CharUnits &Field1Off, llvm::Type *&Field2Ty,
  9355. CharUnits &Field2Off, int &NeededArgGPRs,
  9356. int &NeededArgFPRs) const;
  9357. ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty,
  9358. CharUnits Field1Off,
  9359. llvm::Type *Field2Ty,
  9360. CharUnits Field2Off) const;
  9361. };
  9362. } // end anonymous namespace
  9363. void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
  9364. QualType RetTy = FI.getReturnType();
  9365. if (!getCXXABI().classifyReturnType(FI))
  9366. FI.getReturnInfo() = classifyReturnType(RetTy);
  9367. // IsRetIndirect is true if classifyArgumentType indicated the value should
  9368. // be passed indirect, or if the type size is a scalar greater than 2*XLen
  9369. // and not a complex type with elements <= FLen. e.g. fp128 is passed direct
  9370. // in LLVM IR, relying on the backend lowering code to rewrite the argument
  9371. // list and pass indirectly on RV32.
  9372. bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
  9373. if (!IsRetIndirect && RetTy->isScalarType() &&
  9374. getContext().getTypeSize(RetTy) > (2 * XLen)) {
  9375. if (RetTy->isComplexType() && FLen) {
  9376. QualType EltTy = RetTy->castAs<ComplexType>()->getElementType();
  9377. IsRetIndirect = getContext().getTypeSize(EltTy) > FLen;
  9378. } else {
  9379. // This is a normal scalar > 2*XLen, such as fp128 on RV32.
  9380. IsRetIndirect = true;
  9381. }
  9382. }
  9383. int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
  9384. int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
  9385. int NumFixedArgs = FI.getNumRequiredArgs();
  9386. int ArgNum = 0;
  9387. for (auto &ArgInfo : FI.arguments()) {
  9388. bool IsFixed = ArgNum < NumFixedArgs;
  9389. ArgInfo.info =
  9390. classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft);
  9391. ArgNum++;
  9392. }
  9393. }
  9394. // Returns true if the struct is a potential candidate for the floating point
  9395. // calling convention. If this function returns true, the caller is
  9396. // responsible for checking that if there is only a single field then that
  9397. // field is a float.
  9398. bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
  9399. llvm::Type *&Field1Ty,
  9400. CharUnits &Field1Off,
  9401. llvm::Type *&Field2Ty,
  9402. CharUnits &Field2Off) const {
  9403. bool IsInt = Ty->isIntegralOrEnumerationType();
  9404. bool IsFloat = Ty->isRealFloatingType();
  9405. if (IsInt || IsFloat) {
  9406. uint64_t Size = getContext().getTypeSize(Ty);
  9407. if (IsInt && Size > XLen)
  9408. return false;
  9409. // Can't be eligible if larger than the FP registers. Half precision isn't
  9410. // currently supported on RISC-V and the ABI hasn't been confirmed, so
  9411. // default to the integer ABI in that case.
  9412. if (IsFloat && (Size > FLen || Size < 32))
  9413. return false;
  9414. // Can't be eligible if an integer type was already found (int+int pairs
  9415. // are not eligible).
  9416. if (IsInt && Field1Ty && Field1Ty->isIntegerTy())
  9417. return false;
  9418. if (!Field1Ty) {
  9419. Field1Ty = CGT.ConvertType(Ty);
  9420. Field1Off = CurOff;
  9421. return true;
  9422. }
  9423. if (!Field2Ty) {
  9424. Field2Ty = CGT.ConvertType(Ty);
  9425. Field2Off = CurOff;
  9426. return true;
  9427. }
  9428. return false;
  9429. }
  9430. if (auto CTy = Ty->getAs<ComplexType>()) {
  9431. if (Field1Ty)
  9432. return false;
  9433. QualType EltTy = CTy->getElementType();
  9434. if (getContext().getTypeSize(EltTy) > FLen)
  9435. return false;
  9436. Field1Ty = CGT.ConvertType(EltTy);
  9437. Field1Off = CurOff;
  9438. Field2Ty = Field1Ty;
  9439. Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy);
  9440. return true;
  9441. }
  9442. if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) {
  9443. uint64_t ArraySize = ATy->getSize().getZExtValue();
  9444. QualType EltTy = ATy->getElementType();
  9445. CharUnits EltSize = getContext().getTypeSizeInChars(EltTy);
  9446. for (uint64_t i = 0; i < ArraySize; ++i) {
  9447. bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty,
  9448. Field1Off, Field2Ty, Field2Off);
  9449. if (!Ret)
  9450. return false;
  9451. CurOff += EltSize;
  9452. }
  9453. return true;
  9454. }
  9455. if (const auto *RTy = Ty->getAs<RecordType>()) {
  9456. // Structures with either a non-trivial destructor or a non-trivial
  9457. // copy constructor are not eligible for the FP calling convention.
  9458. if (getRecordArgABI(Ty, CGT.getCXXABI()))
  9459. return false;
  9460. if (isEmptyRecord(getContext(), Ty, true))
  9461. return true;
  9462. const RecordDecl *RD = RTy->getDecl();
  9463. // Unions aren't eligible unless they're empty (which is caught above).
  9464. if (RD->isUnion())
  9465. return false;
  9466. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  9467. // If this is a C++ record, check the bases first.
  9468. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  9469. for (const CXXBaseSpecifier &B : CXXRD->bases()) {
  9470. const auto *BDecl =
  9471. cast<CXXRecordDecl>(B.getType()->castAs<RecordType>()->getDecl());
  9472. CharUnits BaseOff = Layout.getBaseClassOffset(BDecl);
  9473. bool Ret = detectFPCCEligibleStructHelper(B.getType(), CurOff + BaseOff,
  9474. Field1Ty, Field1Off, Field2Ty,
  9475. Field2Off);
  9476. if (!Ret)
  9477. return false;
  9478. }
  9479. }
  9480. int ZeroWidthBitFieldCount = 0;
  9481. for (const FieldDecl *FD : RD->fields()) {
  9482. uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex());
  9483. QualType QTy = FD->getType();
  9484. if (FD->isBitField()) {
  9485. unsigned BitWidth = FD->getBitWidthValue(getContext());
  9486. // Allow a bitfield with a type greater than XLen as long as the
  9487. // bitwidth is XLen or less.
  9488. if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen)
  9489. QTy = getContext().getIntTypeForBitwidth(XLen, false);
  9490. if (BitWidth == 0) {
  9491. ZeroWidthBitFieldCount++;
  9492. continue;
  9493. }
  9494. }
  9495. bool Ret = detectFPCCEligibleStructHelper(
  9496. QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits),
  9497. Field1Ty, Field1Off, Field2Ty, Field2Off);
  9498. if (!Ret)
  9499. return false;
  9500. // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp
  9501. // or int+fp structs, but are ignored for a struct with an fp field and
  9502. // any number of zero-width bitfields.
  9503. if (Field2Ty && ZeroWidthBitFieldCount > 0)
  9504. return false;
  9505. }
  9506. return Field1Ty != nullptr;
  9507. }
  9508. return false;
  9509. }
  9510. // Determine if a struct is eligible for passing according to the floating
  9511. // point calling convention (i.e., when flattened it contains a single fp
  9512. // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and
  9513. // NeededArgGPRs are incremented appropriately.
  9514. bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
  9515. CharUnits &Field1Off,
  9516. llvm::Type *&Field2Ty,
  9517. CharUnits &Field2Off,
  9518. int &NeededArgGPRs,
  9519. int &NeededArgFPRs) const {
  9520. Field1Ty = nullptr;
  9521. Field2Ty = nullptr;
  9522. NeededArgGPRs = 0;
  9523. NeededArgFPRs = 0;
  9524. bool IsCandidate = detectFPCCEligibleStructHelper(
  9525. Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off);
  9526. // Not really a candidate if we have a single int but no float.
  9527. if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy())
  9528. return false;
  9529. if (!IsCandidate)
  9530. return false;
  9531. if (Field1Ty && Field1Ty->isFloatingPointTy())
  9532. NeededArgFPRs++;
  9533. else if (Field1Ty)
  9534. NeededArgGPRs++;
  9535. if (Field2Ty && Field2Ty->isFloatingPointTy())
  9536. NeededArgFPRs++;
  9537. else if (Field2Ty)
  9538. NeededArgGPRs++;
  9539. return true;
  9540. }
  9541. // Call getCoerceAndExpand for the two-element flattened struct described by
  9542. // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an
  9543. // appropriate coerceToType and unpaddedCoerceToType.
  9544. ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct(
  9545. llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty,
  9546. CharUnits Field2Off) const {
  9547. SmallVector<llvm::Type *, 3> CoerceElts;
  9548. SmallVector<llvm::Type *, 2> UnpaddedCoerceElts;
  9549. if (!Field1Off.isZero())
  9550. CoerceElts.push_back(llvm::ArrayType::get(
  9551. llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity()));
  9552. CoerceElts.push_back(Field1Ty);
  9553. UnpaddedCoerceElts.push_back(Field1Ty);
  9554. if (!Field2Ty) {
  9555. return ABIArgInfo::getCoerceAndExpand(
  9556. llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()),
  9557. UnpaddedCoerceElts[0]);
  9558. }
  9559. CharUnits Field2Align =
  9560. CharUnits::fromQuantity(getDataLayout().getABITypeAlign(Field2Ty));
  9561. CharUnits Field1End = Field1Off +
  9562. CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty));
  9563. CharUnits Field2OffNoPadNoPack = Field1End.alignTo(Field2Align);
  9564. CharUnits Padding = CharUnits::Zero();
  9565. if (Field2Off > Field2OffNoPadNoPack)
  9566. Padding = Field2Off - Field2OffNoPadNoPack;
  9567. else if (Field2Off != Field2Align && Field2Off > Field1End)
  9568. Padding = Field2Off - Field1End;
  9569. bool IsPacked = !Field2Off.isMultipleOf(Field2Align);
  9570. if (!Padding.isZero())
  9571. CoerceElts.push_back(llvm::ArrayType::get(
  9572. llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity()));
  9573. CoerceElts.push_back(Field2Ty);
  9574. UnpaddedCoerceElts.push_back(Field2Ty);
  9575. auto CoerceToType =
  9576. llvm::StructType::get(getVMContext(), CoerceElts, IsPacked);
  9577. auto UnpaddedCoerceToType =
  9578. llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked);
  9579. return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType);
  9580. }
  9581. ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
  9582. int &ArgGPRsLeft,
  9583. int &ArgFPRsLeft) const {
  9584. assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
  9585. Ty = useFirstFieldIfTransparentUnion(Ty);
  9586. // Structures with either a non-trivial destructor or a non-trivial
  9587. // copy constructor are always passed indirectly.
  9588. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  9589. if (ArgGPRsLeft)
  9590. ArgGPRsLeft -= 1;
  9591. return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
  9592. CGCXXABI::RAA_DirectInMemory);
  9593. }
  9594. // Ignore empty structs/unions.
  9595. if (isEmptyRecord(getContext(), Ty, true))
  9596. return ABIArgInfo::getIgnore();
  9597. uint64_t Size = getContext().getTypeSize(Ty);
  9598. // Pass floating point values via FPRs if possible.
  9599. if (IsFixed && Ty->isFloatingType() && !Ty->isComplexType() &&
  9600. FLen >= Size && ArgFPRsLeft) {
  9601. ArgFPRsLeft--;
  9602. return ABIArgInfo::getDirect();
  9603. }
  9604. // Complex types for the hard float ABI must be passed direct rather than
  9605. // using CoerceAndExpand.
  9606. if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) {
  9607. QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
  9608. if (getContext().getTypeSize(EltTy) <= FLen) {
  9609. ArgFPRsLeft -= 2;
  9610. return ABIArgInfo::getDirect();
  9611. }
  9612. }
  9613. if (IsFixed && FLen && Ty->isStructureOrClassType()) {
  9614. llvm::Type *Field1Ty = nullptr;
  9615. llvm::Type *Field2Ty = nullptr;
  9616. CharUnits Field1Off = CharUnits::Zero();
  9617. CharUnits Field2Off = CharUnits::Zero();
  9618. int NeededArgGPRs = 0;
  9619. int NeededArgFPRs = 0;
  9620. bool IsCandidate =
  9621. detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off,
  9622. NeededArgGPRs, NeededArgFPRs);
  9623. if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft &&
  9624. NeededArgFPRs <= ArgFPRsLeft) {
  9625. ArgGPRsLeft -= NeededArgGPRs;
  9626. ArgFPRsLeft -= NeededArgFPRs;
  9627. return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty,
  9628. Field2Off);
  9629. }
  9630. }
  9631. uint64_t NeededAlign = getContext().getTypeAlign(Ty);
  9632. // Determine the number of GPRs needed to pass the current argument
  9633. // according to the ABI. 2*XLen-aligned varargs are passed in "aligned"
  9634. // register pairs, so may consume 3 registers.
  9635. int NeededArgGPRs = 1;
  9636. if (!IsFixed && NeededAlign == 2 * XLen)
  9637. NeededArgGPRs = 2 + (ArgGPRsLeft % 2);
  9638. else if (Size > XLen && Size <= 2 * XLen)
  9639. NeededArgGPRs = 2;
  9640. if (NeededArgGPRs > ArgGPRsLeft) {
  9641. NeededArgGPRs = ArgGPRsLeft;
  9642. }
  9643. ArgGPRsLeft -= NeededArgGPRs;
  9644. if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
  9645. // Treat an enum type as its underlying type.
  9646. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  9647. Ty = EnumTy->getDecl()->getIntegerType();
  9648. // All integral types are promoted to XLen width
  9649. if (Size < XLen && Ty->isIntegralOrEnumerationType()) {
  9650. return extendType(Ty);
  9651. }
  9652. if (const auto *EIT = Ty->getAs<BitIntType>()) {
  9653. if (EIT->getNumBits() < XLen)
  9654. return extendType(Ty);
  9655. if (EIT->getNumBits() > 128 ||
  9656. (!getContext().getTargetInfo().hasInt128Type() &&
  9657. EIT->getNumBits() > 64))
  9658. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  9659. }
  9660. return ABIArgInfo::getDirect();
  9661. }
  9662. // Aggregates which are <= 2*XLen will be passed in registers if possible,
  9663. // so coerce to integers.
  9664. if (Size <= 2 * XLen) {
  9665. unsigned Alignment = getContext().getTypeAlign(Ty);
  9666. // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is
  9667. // required, and a 2-element XLen array if only XLen alignment is required.
  9668. if (Size <= XLen) {
  9669. return ABIArgInfo::getDirect(
  9670. llvm::IntegerType::get(getVMContext(), XLen));
  9671. } else if (Alignment == 2 * XLen) {
  9672. return ABIArgInfo::getDirect(
  9673. llvm::IntegerType::get(getVMContext(), 2 * XLen));
  9674. } else {
  9675. return ABIArgInfo::getDirect(llvm::ArrayType::get(
  9676. llvm::IntegerType::get(getVMContext(), XLen), 2));
  9677. }
  9678. }
  9679. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  9680. }
  9681. ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const {
  9682. if (RetTy->isVoidType())
  9683. return ABIArgInfo::getIgnore();
  9684. int ArgGPRsLeft = 2;
  9685. int ArgFPRsLeft = FLen ? 2 : 0;
  9686. // The rules for return and argument types are the same, so defer to
  9687. // classifyArgumentType.
  9688. return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft,
  9689. ArgFPRsLeft);
  9690. }
  9691. Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  9692. QualType Ty) const {
  9693. CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
  9694. // Empty records are ignored for parameter passing purposes.
  9695. if (isEmptyRecord(getContext(), Ty, true)) {
  9696. Address Addr = Address(CGF.Builder.CreateLoad(VAListAddr),
  9697. getVAListElementType(CGF), SlotSize);
  9698. Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
  9699. return Addr;
  9700. }
  9701. auto TInfo = getContext().getTypeInfoInChars(Ty);
  9702. // Arguments bigger than 2*Xlen bytes are passed indirectly.
  9703. bool IsIndirect = TInfo.Width > 2 * SlotSize;
  9704. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TInfo,
  9705. SlotSize, /*AllowHigherAlign=*/true);
  9706. }
  9707. ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const {
  9708. int TySize = getContext().getTypeSize(Ty);
  9709. // RV64 ABI requires unsigned 32 bit integers to be sign extended.
  9710. if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
  9711. return ABIArgInfo::getSignExtend(Ty);
  9712. return ABIArgInfo::getExtend(Ty);
  9713. }
  9714. namespace {
  9715. class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
  9716. public:
  9717. RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen,
  9718. unsigned FLen)
  9719. : TargetCodeGenInfo(std::make_unique<RISCVABIInfo>(CGT, XLen, FLen)) {}
  9720. void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  9721. CodeGen::CodeGenModule &CGM) const override {
  9722. const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
  9723. if (!FD) return;
  9724. const auto *Attr = FD->getAttr<RISCVInterruptAttr>();
  9725. if (!Attr)
  9726. return;
  9727. const char *Kind;
  9728. switch (Attr->getInterrupt()) {
  9729. case RISCVInterruptAttr::user: Kind = "user"; break;
  9730. case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break;
  9731. case RISCVInterruptAttr::machine: Kind = "machine"; break;
  9732. }
  9733. auto *Fn = cast<llvm::Function>(GV);
  9734. Fn->addFnAttr("interrupt", Kind);
  9735. }
  9736. };
  9737. } // namespace
  9738. //===----------------------------------------------------------------------===//
  9739. // VE ABI Implementation.
  9740. //
  9741. namespace {
  9742. class VEABIInfo : public DefaultABIInfo {
  9743. public:
  9744. VEABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  9745. private:
  9746. ABIArgInfo classifyReturnType(QualType RetTy) const;
  9747. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  9748. void computeInfo(CGFunctionInfo &FI) const override;
  9749. };
  9750. } // end anonymous namespace
  9751. ABIArgInfo VEABIInfo::classifyReturnType(QualType Ty) const {
  9752. if (Ty->isAnyComplexType())
  9753. return ABIArgInfo::getDirect();
  9754. uint64_t Size = getContext().getTypeSize(Ty);
  9755. if (Size < 64 && Ty->isIntegerType())
  9756. return ABIArgInfo::getExtend(Ty);
  9757. return DefaultABIInfo::classifyReturnType(Ty);
  9758. }
  9759. ABIArgInfo VEABIInfo::classifyArgumentType(QualType Ty) const {
  9760. if (Ty->isAnyComplexType())
  9761. return ABIArgInfo::getDirect();
  9762. uint64_t Size = getContext().getTypeSize(Ty);
  9763. if (Size < 64 && Ty->isIntegerType())
  9764. return ABIArgInfo::getExtend(Ty);
  9765. return DefaultABIInfo::classifyArgumentType(Ty);
  9766. }
  9767. void VEABIInfo::computeInfo(CGFunctionInfo &FI) const {
  9768. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  9769. for (auto &Arg : FI.arguments())
  9770. Arg.info = classifyArgumentType(Arg.type);
  9771. }
  9772. namespace {
  9773. class VETargetCodeGenInfo : public TargetCodeGenInfo {
  9774. public:
  9775. VETargetCodeGenInfo(CodeGenTypes &CGT)
  9776. : TargetCodeGenInfo(std::make_unique<VEABIInfo>(CGT)) {}
  9777. // VE ABI requires the arguments of variadic and prototype-less functions
  9778. // are passed in both registers and memory.
  9779. bool isNoProtoCallVariadic(const CallArgList &args,
  9780. const FunctionNoProtoType *fnType) const override {
  9781. return true;
  9782. }
  9783. };
  9784. } // end anonymous namespace
  9785. //===----------------------------------------------------------------------===//
  9786. // CSKY ABI Implementation
  9787. //===----------------------------------------------------------------------===//
  9788. namespace {
  9789. class CSKYABIInfo : public DefaultABIInfo {
  9790. static const int NumArgGPRs = 4;
  9791. static const int NumArgFPRs = 4;
  9792. static const unsigned XLen = 32;
  9793. unsigned FLen;
  9794. public:
  9795. CSKYABIInfo(CodeGen::CodeGenTypes &CGT, unsigned FLen)
  9796. : DefaultABIInfo(CGT), FLen(FLen) {}
  9797. void computeInfo(CGFunctionInfo &FI) const override;
  9798. ABIArgInfo classifyArgumentType(QualType Ty, int &ArgGPRsLeft,
  9799. int &ArgFPRsLeft,
  9800. bool isReturnType = false) const;
  9801. ABIArgInfo classifyReturnType(QualType RetTy) const;
  9802. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  9803. QualType Ty) const override;
  9804. };
  9805. } // end anonymous namespace
  9806. void CSKYABIInfo::computeInfo(CGFunctionInfo &FI) const {
  9807. QualType RetTy = FI.getReturnType();
  9808. if (!getCXXABI().classifyReturnType(FI))
  9809. FI.getReturnInfo() = classifyReturnType(RetTy);
  9810. bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
  9811. // We must track the number of GPRs used in order to conform to the CSKY
  9812. // ABI, as integer scalars passed in registers should have signext/zeroext
  9813. // when promoted.
  9814. int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
  9815. int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
  9816. for (auto &ArgInfo : FI.arguments()) {
  9817. ArgInfo.info = classifyArgumentType(ArgInfo.type, ArgGPRsLeft, ArgFPRsLeft);
  9818. }
  9819. }
  9820. Address CSKYABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  9821. QualType Ty) const {
  9822. CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
  9823. // Empty records are ignored for parameter passing purposes.
  9824. if (isEmptyRecord(getContext(), Ty, true)) {
  9825. Address Addr = Address(CGF.Builder.CreateLoad(VAListAddr),
  9826. getVAListElementType(CGF), SlotSize);
  9827. Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
  9828. return Addr;
  9829. }
  9830. auto TInfo = getContext().getTypeInfoInChars(Ty);
  9831. return emitVoidPtrVAArg(CGF, VAListAddr, Ty, false, TInfo, SlotSize,
  9832. /*AllowHigherAlign=*/true);
  9833. }
  9834. ABIArgInfo CSKYABIInfo::classifyArgumentType(QualType Ty, int &ArgGPRsLeft,
  9835. int &ArgFPRsLeft,
  9836. bool isReturnType) const {
  9837. assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
  9838. Ty = useFirstFieldIfTransparentUnion(Ty);
  9839. // Structures with either a non-trivial destructor or a non-trivial
  9840. // copy constructor are always passed indirectly.
  9841. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  9842. if (ArgGPRsLeft)
  9843. ArgGPRsLeft -= 1;
  9844. return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
  9845. CGCXXABI::RAA_DirectInMemory);
  9846. }
  9847. // Ignore empty structs/unions.
  9848. if (isEmptyRecord(getContext(), Ty, true))
  9849. return ABIArgInfo::getIgnore();
  9850. if (!Ty->getAsUnionType())
  9851. if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
  9852. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  9853. uint64_t Size = getContext().getTypeSize(Ty);
  9854. // Pass floating point values via FPRs if possible.
  9855. if (Ty->isFloatingType() && !Ty->isComplexType() && FLen >= Size &&
  9856. ArgFPRsLeft) {
  9857. ArgFPRsLeft--;
  9858. return ABIArgInfo::getDirect();
  9859. }
  9860. // Complex types for the hard float ABI must be passed direct rather than
  9861. // using CoerceAndExpand.
  9862. if (Ty->isComplexType() && FLen && !isReturnType) {
  9863. QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
  9864. if (getContext().getTypeSize(EltTy) <= FLen) {
  9865. ArgFPRsLeft -= 2;
  9866. return ABIArgInfo::getDirect();
  9867. }
  9868. }
  9869. if (!isAggregateTypeForABI(Ty)) {
  9870. // Treat an enum type as its underlying type.
  9871. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  9872. Ty = EnumTy->getDecl()->getIntegerType();
  9873. // All integral types are promoted to XLen width, unless passed on the
  9874. // stack.
  9875. if (Size < XLen && Ty->isIntegralOrEnumerationType())
  9876. return ABIArgInfo::getExtend(Ty);
  9877. if (const auto *EIT = Ty->getAs<BitIntType>()) {
  9878. if (EIT->getNumBits() < XLen)
  9879. return ABIArgInfo::getExtend(Ty);
  9880. }
  9881. return ABIArgInfo::getDirect();
  9882. }
  9883. // For argument type, the first 4*XLen parts of aggregate will be passed
  9884. // in registers, and the rest will be passed in stack.
  9885. // So we can coerce to integers directly and let backend handle it correctly.
  9886. // For return type, aggregate which <= 2*XLen will be returned in registers.
  9887. // Otherwise, aggregate will be returned indirectly.
  9888. if (!isReturnType || (isReturnType && Size <= 2 * XLen)) {
  9889. if (Size <= XLen) {
  9890. return ABIArgInfo::getDirect(
  9891. llvm::IntegerType::get(getVMContext(), XLen));
  9892. } else {
  9893. return ABIArgInfo::getDirect(llvm::ArrayType::get(
  9894. llvm::IntegerType::get(getVMContext(), XLen), (Size + 31) / XLen));
  9895. }
  9896. }
  9897. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  9898. }
  9899. ABIArgInfo CSKYABIInfo::classifyReturnType(QualType RetTy) const {
  9900. if (RetTy->isVoidType())
  9901. return ABIArgInfo::getIgnore();
  9902. int ArgGPRsLeft = 2;
  9903. int ArgFPRsLeft = FLen ? 1 : 0;
  9904. // The rules for return and argument types are the same, so defer to
  9905. // classifyArgumentType.
  9906. return classifyArgumentType(RetTy, ArgGPRsLeft, ArgFPRsLeft, true);
  9907. }
  9908. namespace {
  9909. class CSKYTargetCodeGenInfo : public TargetCodeGenInfo {
  9910. public:
  9911. CSKYTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned FLen)
  9912. : TargetCodeGenInfo(std::make_unique<CSKYABIInfo>(CGT, FLen)) {}
  9913. };
  9914. } // end anonymous namespace
  9915. //===----------------------------------------------------------------------===//
  9916. // BPF ABI Implementation
  9917. //===----------------------------------------------------------------------===//
  9918. namespace {
  9919. class BPFABIInfo : public DefaultABIInfo {
  9920. public:
  9921. BPFABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
  9922. ABIArgInfo classifyArgumentType(QualType Ty) const {
  9923. Ty = useFirstFieldIfTransparentUnion(Ty);
  9924. if (isAggregateTypeForABI(Ty)) {
  9925. uint64_t Bits = getContext().getTypeSize(Ty);
  9926. if (Bits == 0)
  9927. return ABIArgInfo::getIgnore();
  9928. // If the aggregate needs 1 or 2 registers, do not use reference.
  9929. if (Bits <= 128) {
  9930. llvm::Type *CoerceTy;
  9931. if (Bits <= 64) {
  9932. CoerceTy =
  9933. llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
  9934. } else {
  9935. llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), 64);
  9936. CoerceTy = llvm::ArrayType::get(RegTy, 2);
  9937. }
  9938. return ABIArgInfo::getDirect(CoerceTy);
  9939. } else {
  9940. return getNaturalAlignIndirect(Ty);
  9941. }
  9942. }
  9943. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  9944. Ty = EnumTy->getDecl()->getIntegerType();
  9945. ASTContext &Context = getContext();
  9946. if (const auto *EIT = Ty->getAs<BitIntType>())
  9947. if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
  9948. return getNaturalAlignIndirect(Ty);
  9949. return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
  9950. : ABIArgInfo::getDirect());
  9951. }
  9952. ABIArgInfo classifyReturnType(QualType RetTy) const {
  9953. if (RetTy->isVoidType())
  9954. return ABIArgInfo::getIgnore();
  9955. if (isAggregateTypeForABI(RetTy))
  9956. return getNaturalAlignIndirect(RetTy);
  9957. // Treat an enum type as its underlying type.
  9958. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  9959. RetTy = EnumTy->getDecl()->getIntegerType();
  9960. ASTContext &Context = getContext();
  9961. if (const auto *EIT = RetTy->getAs<BitIntType>())
  9962. if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
  9963. return getNaturalAlignIndirect(RetTy);
  9964. // Caller will do necessary sign/zero extension.
  9965. return ABIArgInfo::getDirect();
  9966. }
  9967. void computeInfo(CGFunctionInfo &FI) const override {
  9968. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  9969. for (auto &I : FI.arguments())
  9970. I.info = classifyArgumentType(I.type);
  9971. }
  9972. };
  9973. class BPFTargetCodeGenInfo : public TargetCodeGenInfo {
  9974. public:
  9975. BPFTargetCodeGenInfo(CodeGenTypes &CGT)
  9976. : TargetCodeGenInfo(std::make_unique<BPFABIInfo>(CGT)) {}
  9977. const BPFABIInfo &getABIInfo() const {
  9978. return static_cast<const BPFABIInfo&>(TargetCodeGenInfo::getABIInfo());
  9979. }
  9980. };
  9981. }
  9982. // LoongArch ABI Implementation. Documented at
  9983. // https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html
  9984. //
  9985. //===----------------------------------------------------------------------===//
  9986. namespace {
  9987. class LoongArchABIInfo : public DefaultABIInfo {
  9988. private:
  9989. // Size of the integer ('r') registers in bits.
  9990. unsigned GRLen;
  9991. // Size of the floating point ('f') registers in bits.
  9992. unsigned FRLen;
  9993. // Number of general-purpose argument registers.
  9994. static const int NumGARs = 8;
  9995. // Number of floating-point argument registers.
  9996. static const int NumFARs = 8;
  9997. bool detectFARsEligibleStructHelper(QualType Ty, CharUnits CurOff,
  9998. llvm::Type *&Field1Ty,
  9999. CharUnits &Field1Off,
  10000. llvm::Type *&Field2Ty,
  10001. CharUnits &Field2Off) const;
  10002. public:
  10003. LoongArchABIInfo(CodeGen::CodeGenTypes &CGT, unsigned GRLen, unsigned FRLen)
  10004. : DefaultABIInfo(CGT), GRLen(GRLen), FRLen(FRLen) {}
  10005. void computeInfo(CGFunctionInfo &FI) const override;
  10006. ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &GARsLeft,
  10007. int &FARsLeft) const;
  10008. ABIArgInfo classifyReturnType(QualType RetTy) const;
  10009. Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  10010. QualType Ty) const override;
  10011. ABIArgInfo extendType(QualType Ty) const;
  10012. bool detectFARsEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
  10013. CharUnits &Field1Off, llvm::Type *&Field2Ty,
  10014. CharUnits &Field2Off, int &NeededArgGPRs,
  10015. int &NeededArgFPRs) const;
  10016. ABIArgInfo coerceAndExpandFARsEligibleStruct(llvm::Type *Field1Ty,
  10017. CharUnits Field1Off,
  10018. llvm::Type *Field2Ty,
  10019. CharUnits Field2Off) const;
  10020. };
  10021. } // end anonymous namespace
  10022. void LoongArchABIInfo::computeInfo(CGFunctionInfo &FI) const {
  10023. QualType RetTy = FI.getReturnType();
  10024. if (!getCXXABI().classifyReturnType(FI))
  10025. FI.getReturnInfo() = classifyReturnType(RetTy);
  10026. // IsRetIndirect is true if classifyArgumentType indicated the value should
  10027. // be passed indirect, or if the type size is a scalar greater than 2*GRLen
  10028. // and not a complex type with elements <= FRLen. e.g. fp128 is passed direct
  10029. // in LLVM IR, relying on the backend lowering code to rewrite the argument
  10030. // list and pass indirectly on LA32.
  10031. bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
  10032. if (!IsRetIndirect && RetTy->isScalarType() &&
  10033. getContext().getTypeSize(RetTy) > (2 * GRLen)) {
  10034. if (RetTy->isComplexType() && FRLen) {
  10035. QualType EltTy = RetTy->castAs<ComplexType>()->getElementType();
  10036. IsRetIndirect = getContext().getTypeSize(EltTy) > FRLen;
  10037. } else {
  10038. // This is a normal scalar > 2*GRLen, such as fp128 on LA32.
  10039. IsRetIndirect = true;
  10040. }
  10041. }
  10042. // We must track the number of GARs and FARs used in order to conform to the
  10043. // LoongArch ABI. As GAR usage is different for variadic arguments, we must
  10044. // also track whether we are examining a vararg or not.
  10045. int GARsLeft = IsRetIndirect ? NumGARs - 1 : NumGARs;
  10046. int FARsLeft = FRLen ? NumFARs : 0;
  10047. int NumFixedArgs = FI.getNumRequiredArgs();
  10048. int ArgNum = 0;
  10049. for (auto &ArgInfo : FI.arguments()) {
  10050. ArgInfo.info = classifyArgumentType(
  10051. ArgInfo.type, /*IsFixed=*/ArgNum < NumFixedArgs, GARsLeft, FARsLeft);
  10052. ArgNum++;
  10053. }
  10054. }
  10055. // Returns true if the struct is a potential candidate to be passed in FARs (and
  10056. // GARs). If this function returns true, the caller is responsible for checking
  10057. // that if there is only a single field then that field is a float.
  10058. bool LoongArchABIInfo::detectFARsEligibleStructHelper(
  10059. QualType Ty, CharUnits CurOff, llvm::Type *&Field1Ty, CharUnits &Field1Off,
  10060. llvm::Type *&Field2Ty, CharUnits &Field2Off) const {
  10061. bool IsInt = Ty->isIntegralOrEnumerationType();
  10062. bool IsFloat = Ty->isRealFloatingType();
  10063. if (IsInt || IsFloat) {
  10064. uint64_t Size = getContext().getTypeSize(Ty);
  10065. if (IsInt && Size > GRLen)
  10066. return false;
  10067. // Can't be eligible if larger than the FP registers. Half precision isn't
  10068. // currently supported on LoongArch and the ABI hasn't been confirmed, so
  10069. // default to the integer ABI in that case.
  10070. if (IsFloat && (Size > FRLen || Size < 32))
  10071. return false;
  10072. // Can't be eligible if an integer type was already found (int+int pairs
  10073. // are not eligible).
  10074. if (IsInt && Field1Ty && Field1Ty->isIntegerTy())
  10075. return false;
  10076. if (!Field1Ty) {
  10077. Field1Ty = CGT.ConvertType(Ty);
  10078. Field1Off = CurOff;
  10079. return true;
  10080. }
  10081. if (!Field2Ty) {
  10082. Field2Ty = CGT.ConvertType(Ty);
  10083. Field2Off = CurOff;
  10084. return true;
  10085. }
  10086. return false;
  10087. }
  10088. if (auto CTy = Ty->getAs<ComplexType>()) {
  10089. if (Field1Ty)
  10090. return false;
  10091. QualType EltTy = CTy->getElementType();
  10092. if (getContext().getTypeSize(EltTy) > FRLen)
  10093. return false;
  10094. Field1Ty = CGT.ConvertType(EltTy);
  10095. Field1Off = CurOff;
  10096. Field2Ty = Field1Ty;
  10097. Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy);
  10098. return true;
  10099. }
  10100. if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) {
  10101. uint64_t ArraySize = ATy->getSize().getZExtValue();
  10102. QualType EltTy = ATy->getElementType();
  10103. CharUnits EltSize = getContext().getTypeSizeInChars(EltTy);
  10104. for (uint64_t i = 0; i < ArraySize; ++i) {
  10105. if (!detectFARsEligibleStructHelper(EltTy, CurOff, Field1Ty, Field1Off,
  10106. Field2Ty, Field2Off))
  10107. return false;
  10108. CurOff += EltSize;
  10109. }
  10110. return true;
  10111. }
  10112. if (const auto *RTy = Ty->getAs<RecordType>()) {
  10113. // Structures with either a non-trivial destructor or a non-trivial
  10114. // copy constructor are not eligible for the FP calling convention.
  10115. if (getRecordArgABI(Ty, CGT.getCXXABI()))
  10116. return false;
  10117. if (isEmptyRecord(getContext(), Ty, true))
  10118. return true;
  10119. const RecordDecl *RD = RTy->getDecl();
  10120. // Unions aren't eligible unless they're empty (which is caught above).
  10121. if (RD->isUnion())
  10122. return false;
  10123. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  10124. // If this is a C++ record, check the bases first.
  10125. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  10126. for (const CXXBaseSpecifier &B : CXXRD->bases()) {
  10127. const auto *BDecl =
  10128. cast<CXXRecordDecl>(B.getType()->castAs<RecordType>()->getDecl());
  10129. if (!detectFARsEligibleStructHelper(
  10130. B.getType(), CurOff + Layout.getBaseClassOffset(BDecl),
  10131. Field1Ty, Field1Off, Field2Ty, Field2Off))
  10132. return false;
  10133. }
  10134. }
  10135. for (const FieldDecl *FD : RD->fields()) {
  10136. QualType QTy = FD->getType();
  10137. if (FD->isBitField()) {
  10138. unsigned BitWidth = FD->getBitWidthValue(getContext());
  10139. // Zero-width bitfields are ignored.
  10140. if (BitWidth == 0)
  10141. continue;
  10142. // Allow a bitfield with a type greater than GRLen as long as the
  10143. // bitwidth is GRLen or less.
  10144. if (getContext().getTypeSize(QTy) > GRLen && BitWidth <= GRLen) {
  10145. QTy = getContext().getIntTypeForBitwidth(GRLen, false);
  10146. }
  10147. }
  10148. if (!detectFARsEligibleStructHelper(
  10149. QTy,
  10150. CurOff + getContext().toCharUnitsFromBits(
  10151. Layout.getFieldOffset(FD->getFieldIndex())),
  10152. Field1Ty, Field1Off, Field2Ty, Field2Off))
  10153. return false;
  10154. }
  10155. return Field1Ty != nullptr;
  10156. }
  10157. return false;
  10158. }
  10159. // Determine if a struct is eligible to be passed in FARs (and GARs) (i.e., when
  10160. // flattened it contains a single fp value, fp+fp, or int+fp of appropriate
  10161. // size). If so, NeededFARs and NeededGARs are incremented appropriately.
  10162. bool LoongArchABIInfo::detectFARsEligibleStruct(
  10163. QualType Ty, llvm::Type *&Field1Ty, CharUnits &Field1Off,
  10164. llvm::Type *&Field2Ty, CharUnits &Field2Off, int &NeededGARs,
  10165. int &NeededFARs) const {
  10166. Field1Ty = nullptr;
  10167. Field2Ty = nullptr;
  10168. NeededGARs = 0;
  10169. NeededFARs = 0;
  10170. if (!detectFARsEligibleStructHelper(Ty, CharUnits::Zero(), Field1Ty,
  10171. Field1Off, Field2Ty, Field2Off))
  10172. return false;
  10173. // Not really a candidate if we have a single int but no float.
  10174. if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy())
  10175. return false;
  10176. if (Field1Ty && Field1Ty->isFloatingPointTy())
  10177. NeededFARs++;
  10178. else if (Field1Ty)
  10179. NeededGARs++;
  10180. if (Field2Ty && Field2Ty->isFloatingPointTy())
  10181. NeededFARs++;
  10182. else if (Field2Ty)
  10183. NeededGARs++;
  10184. return true;
  10185. }
  10186. // Call getCoerceAndExpand for the two-element flattened struct described by
  10187. // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an
  10188. // appropriate coerceToType and unpaddedCoerceToType.
  10189. ABIArgInfo LoongArchABIInfo::coerceAndExpandFARsEligibleStruct(
  10190. llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty,
  10191. CharUnits Field2Off) const {
  10192. SmallVector<llvm::Type *, 3> CoerceElts;
  10193. SmallVector<llvm::Type *, 2> UnpaddedCoerceElts;
  10194. if (!Field1Off.isZero())
  10195. CoerceElts.push_back(llvm::ArrayType::get(
  10196. llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity()));
  10197. CoerceElts.push_back(Field1Ty);
  10198. UnpaddedCoerceElts.push_back(Field1Ty);
  10199. if (!Field2Ty) {
  10200. return ABIArgInfo::getCoerceAndExpand(
  10201. llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()),
  10202. UnpaddedCoerceElts[0]);
  10203. }
  10204. CharUnits Field2Align =
  10205. CharUnits::fromQuantity(getDataLayout().getABITypeAlign(Field2Ty));
  10206. CharUnits Field1End =
  10207. Field1Off +
  10208. CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty));
  10209. CharUnits Field2OffNoPadNoPack = Field1End.alignTo(Field2Align);
  10210. CharUnits Padding = CharUnits::Zero();
  10211. if (Field2Off > Field2OffNoPadNoPack)
  10212. Padding = Field2Off - Field2OffNoPadNoPack;
  10213. else if (Field2Off != Field2Align && Field2Off > Field1End)
  10214. Padding = Field2Off - Field1End;
  10215. bool IsPacked = !Field2Off.isMultipleOf(Field2Align);
  10216. if (!Padding.isZero())
  10217. CoerceElts.push_back(llvm::ArrayType::get(
  10218. llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity()));
  10219. CoerceElts.push_back(Field2Ty);
  10220. UnpaddedCoerceElts.push_back(Field2Ty);
  10221. return ABIArgInfo::getCoerceAndExpand(
  10222. llvm::StructType::get(getVMContext(), CoerceElts, IsPacked),
  10223. llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked));
  10224. }
  10225. ABIArgInfo LoongArchABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
  10226. int &GARsLeft,
  10227. int &FARsLeft) const {
  10228. assert(GARsLeft <= NumGARs && "GAR tracking underflow");
  10229. Ty = useFirstFieldIfTransparentUnion(Ty);
  10230. // Structures with either a non-trivial destructor or a non-trivial
  10231. // copy constructor are always passed indirectly.
  10232. if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
  10233. if (GARsLeft)
  10234. GARsLeft -= 1;
  10235. return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
  10236. CGCXXABI::RAA_DirectInMemory);
  10237. }
  10238. // Ignore empty structs/unions.
  10239. if (isEmptyRecord(getContext(), Ty, true))
  10240. return ABIArgInfo::getIgnore();
  10241. uint64_t Size = getContext().getTypeSize(Ty);
  10242. // Pass floating point values via FARs if possible.
  10243. if (IsFixed && Ty->isFloatingType() && !Ty->isComplexType() &&
  10244. FRLen >= Size && FARsLeft) {
  10245. FARsLeft--;
  10246. return ABIArgInfo::getDirect();
  10247. }
  10248. // Complex types for the *f or *d ABI must be passed directly rather than
  10249. // using CoerceAndExpand.
  10250. if (IsFixed && Ty->isComplexType() && FRLen && FARsLeft >= 2) {
  10251. QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
  10252. if (getContext().getTypeSize(EltTy) <= FRLen) {
  10253. FARsLeft -= 2;
  10254. return ABIArgInfo::getDirect();
  10255. }
  10256. }
  10257. if (IsFixed && FRLen && Ty->isStructureOrClassType()) {
  10258. llvm::Type *Field1Ty = nullptr;
  10259. llvm::Type *Field2Ty = nullptr;
  10260. CharUnits Field1Off = CharUnits::Zero();
  10261. CharUnits Field2Off = CharUnits::Zero();
  10262. int NeededGARs = 0;
  10263. int NeededFARs = 0;
  10264. bool IsCandidate = detectFARsEligibleStruct(
  10265. Ty, Field1Ty, Field1Off, Field2Ty, Field2Off, NeededGARs, NeededFARs);
  10266. if (IsCandidate && NeededGARs <= GARsLeft && NeededFARs <= FARsLeft) {
  10267. GARsLeft -= NeededGARs;
  10268. FARsLeft -= NeededFARs;
  10269. return coerceAndExpandFARsEligibleStruct(Field1Ty, Field1Off, Field2Ty,
  10270. Field2Off);
  10271. }
  10272. }
  10273. uint64_t NeededAlign = getContext().getTypeAlign(Ty);
  10274. // Determine the number of GARs needed to pass the current argument
  10275. // according to the ABI. 2*GRLen-aligned varargs are passed in "aligned"
  10276. // register pairs, so may consume 3 registers.
  10277. int NeededGARs = 1;
  10278. if (!IsFixed && NeededAlign == 2 * GRLen)
  10279. NeededGARs = 2 + (GARsLeft % 2);
  10280. else if (Size > GRLen && Size <= 2 * GRLen)
  10281. NeededGARs = 2;
  10282. if (NeededGARs > GARsLeft)
  10283. NeededGARs = GARsLeft;
  10284. GARsLeft -= NeededGARs;
  10285. if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
  10286. // Treat an enum type as its underlying type.
  10287. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  10288. Ty = EnumTy->getDecl()->getIntegerType();
  10289. // All integral types are promoted to GRLen width.
  10290. if (Size < GRLen && Ty->isIntegralOrEnumerationType())
  10291. return extendType(Ty);
  10292. if (const auto *EIT = Ty->getAs<BitIntType>()) {
  10293. if (EIT->getNumBits() < GRLen)
  10294. return extendType(Ty);
  10295. if (EIT->getNumBits() > 128 ||
  10296. (!getContext().getTargetInfo().hasInt128Type() &&
  10297. EIT->getNumBits() > 64))
  10298. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  10299. }
  10300. return ABIArgInfo::getDirect();
  10301. }
  10302. // Aggregates which are <= 2*GRLen will be passed in registers if possible,
  10303. // so coerce to integers.
  10304. if (Size <= 2 * GRLen) {
  10305. // Use a single GRLen int if possible, 2*GRLen if 2*GRLen alignment is
  10306. // required, and a 2-element GRLen array if only GRLen alignment is
  10307. // required.
  10308. if (Size <= GRLen) {
  10309. return ABIArgInfo::getDirect(
  10310. llvm::IntegerType::get(getVMContext(), GRLen));
  10311. }
  10312. if (getContext().getTypeAlign(Ty) == 2 * GRLen) {
  10313. return ABIArgInfo::getDirect(
  10314. llvm::IntegerType::get(getVMContext(), 2 * GRLen));
  10315. }
  10316. return ABIArgInfo::getDirect(
  10317. llvm::ArrayType::get(llvm::IntegerType::get(getVMContext(), GRLen), 2));
  10318. }
  10319. return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
  10320. }
  10321. ABIArgInfo LoongArchABIInfo::classifyReturnType(QualType RetTy) const {
  10322. if (RetTy->isVoidType())
  10323. return ABIArgInfo::getIgnore();
  10324. // The rules for return and argument types are the same, so defer to
  10325. // classifyArgumentType.
  10326. int GARsLeft = 2;
  10327. int FARsLeft = FRLen ? 2 : 0;
  10328. return classifyArgumentType(RetTy, /*IsFixed=*/true, GARsLeft, FARsLeft);
  10329. }
  10330. Address LoongArchABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
  10331. QualType Ty) const {
  10332. CharUnits SlotSize = CharUnits::fromQuantity(GRLen / 8);
  10333. // Empty records are ignored for parameter passing purposes.
  10334. if (isEmptyRecord(getContext(), Ty, true)) {
  10335. Address Addr = Address(CGF.Builder.CreateLoad(VAListAddr),
  10336. getVAListElementType(CGF), SlotSize);
  10337. Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
  10338. return Addr;
  10339. }
  10340. auto TInfo = getContext().getTypeInfoInChars(Ty);
  10341. // Arguments bigger than 2*GRLen bytes are passed indirectly.
  10342. return emitVoidPtrVAArg(CGF, VAListAddr, Ty,
  10343. /*IsIndirect=*/TInfo.Width > 2 * SlotSize, TInfo,
  10344. SlotSize,
  10345. /*AllowHigherAlign=*/true);
  10346. }
  10347. ABIArgInfo LoongArchABIInfo::extendType(QualType Ty) const {
  10348. int TySize = getContext().getTypeSize(Ty);
  10349. // LA64 ABI requires unsigned 32 bit integers to be sign extended.
  10350. if (GRLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
  10351. return ABIArgInfo::getSignExtend(Ty);
  10352. return ABIArgInfo::getExtend(Ty);
  10353. }
  10354. namespace {
  10355. class LoongArchTargetCodeGenInfo : public TargetCodeGenInfo {
  10356. public:
  10357. LoongArchTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned GRLen,
  10358. unsigned FRLen)
  10359. : TargetCodeGenInfo(
  10360. std::make_unique<LoongArchABIInfo>(CGT, GRLen, FRLen)) {}
  10361. };
  10362. } // namespace
  10363. //===----------------------------------------------------------------------===//
  10364. // Driver code
  10365. //===----------------------------------------------------------------------===//
  10366. bool CodeGenModule::supportsCOMDAT() const {
  10367. return getTriple().supportsCOMDAT();
  10368. }
  10369. const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
  10370. if (TheTargetCodeGenInfo)
  10371. return *TheTargetCodeGenInfo;
  10372. // Helper to set the unique_ptr while still keeping the return value.
  10373. auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
  10374. this->TheTargetCodeGenInfo.reset(P);
  10375. return *P;
  10376. };
  10377. const llvm::Triple &Triple = getTarget().getTriple();
  10378. switch (Triple.getArch()) {
  10379. default:
  10380. return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
  10381. case llvm::Triple::le32:
  10382. return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
  10383. case llvm::Triple::m68k:
  10384. return SetCGInfo(new M68kTargetCodeGenInfo(Types));
  10385. case llvm::Triple::mips:
  10386. case llvm::Triple::mipsel:
  10387. if (Triple.getOS() == llvm::Triple::NaCl)
  10388. return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
  10389. return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
  10390. case llvm::Triple::mips64:
  10391. case llvm::Triple::mips64el:
  10392. return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
  10393. case llvm::Triple::avr: {
  10394. // For passing parameters, R8~R25 are used on avr, and R18~R25 are used
  10395. // on avrtiny. For passing return value, R18~R25 are used on avr, and
  10396. // R22~R25 are used on avrtiny.
  10397. unsigned NPR = getTarget().getABI() == "avrtiny" ? 6 : 18;
  10398. unsigned NRR = getTarget().getABI() == "avrtiny" ? 4 : 8;
  10399. return SetCGInfo(new AVRTargetCodeGenInfo(Types, NPR, NRR));
  10400. }
  10401. case llvm::Triple::aarch64:
  10402. case llvm::Triple::aarch64_32:
  10403. case llvm::Triple::aarch64_be: {
  10404. AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
  10405. if (getTarget().getABI() == "darwinpcs")
  10406. Kind = AArch64ABIInfo::DarwinPCS;
  10407. else if (Triple.isOSWindows())
  10408. return SetCGInfo(
  10409. new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
  10410. return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
  10411. }
  10412. case llvm::Triple::wasm32:
  10413. case llvm::Triple::wasm64: {
  10414. WebAssemblyABIInfo::ABIKind Kind = WebAssemblyABIInfo::MVP;
  10415. if (getTarget().getABI() == "experimental-mv")
  10416. Kind = WebAssemblyABIInfo::ExperimentalMV;
  10417. return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types, Kind));
  10418. }
  10419. case llvm::Triple::arm:
  10420. case llvm::Triple::armeb:
  10421. case llvm::Triple::thumb:
  10422. case llvm::Triple::thumbeb: {
  10423. if (Triple.getOS() == llvm::Triple::Win32) {
  10424. return SetCGInfo(
  10425. new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
  10426. }
  10427. ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
  10428. StringRef ABIStr = getTarget().getABI();
  10429. if (ABIStr == "apcs-gnu")
  10430. Kind = ARMABIInfo::APCS;
  10431. else if (ABIStr == "aapcs16")
  10432. Kind = ARMABIInfo::AAPCS16_VFP;
  10433. else if (CodeGenOpts.FloatABI == "hard" ||
  10434. (CodeGenOpts.FloatABI != "soft" &&
  10435. (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
  10436. Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
  10437. Triple.getEnvironment() == llvm::Triple::EABIHF)))
  10438. Kind = ARMABIInfo::AAPCS_VFP;
  10439. return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
  10440. }
  10441. case llvm::Triple::ppc: {
  10442. if (Triple.isOSAIX())
  10443. return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ false));
  10444. bool IsSoftFloat =
  10445. CodeGenOpts.FloatABI == "soft" || getTarget().hasFeature("spe");
  10446. bool RetSmallStructInRegABI =
  10447. PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
  10448. return SetCGInfo(
  10449. new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI));
  10450. }
  10451. case llvm::Triple::ppcle: {
  10452. bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
  10453. bool RetSmallStructInRegABI =
  10454. PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
  10455. return SetCGInfo(
  10456. new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI));
  10457. }
  10458. case llvm::Triple::ppc64:
  10459. if (Triple.isOSAIX())
  10460. return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ true));
  10461. if (Triple.isOSBinFormatELF()) {
  10462. PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
  10463. if (getTarget().getABI() == "elfv2")
  10464. Kind = PPC64_SVR4_ABIInfo::ELFv2;
  10465. bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
  10466. return SetCGInfo(
  10467. new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, IsSoftFloat));
  10468. }
  10469. return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
  10470. case llvm::Triple::ppc64le: {
  10471. assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
  10472. PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
  10473. if (getTarget().getABI() == "elfv1")
  10474. Kind = PPC64_SVR4_ABIInfo::ELFv1;
  10475. bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
  10476. return SetCGInfo(
  10477. new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, IsSoftFloat));
  10478. }
  10479. case llvm::Triple::nvptx:
  10480. case llvm::Triple::nvptx64:
  10481. return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
  10482. case llvm::Triple::msp430:
  10483. return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
  10484. case llvm::Triple::riscv32:
  10485. case llvm::Triple::riscv64: {
  10486. StringRef ABIStr = getTarget().getABI();
  10487. unsigned XLen = getTarget().getPointerWidth(LangAS::Default);
  10488. unsigned ABIFLen = 0;
  10489. if (ABIStr.endswith("f"))
  10490. ABIFLen = 32;
  10491. else if (ABIStr.endswith("d"))
  10492. ABIFLen = 64;
  10493. return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen));
  10494. }
  10495. case llvm::Triple::systemz: {
  10496. bool SoftFloat = CodeGenOpts.FloatABI == "soft";
  10497. bool HasVector = !SoftFloat && getTarget().getABI() == "vector";
  10498. return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector, SoftFloat));
  10499. }
  10500. case llvm::Triple::tce:
  10501. case llvm::Triple::tcele:
  10502. return SetCGInfo(new TCETargetCodeGenInfo(Types));
  10503. case llvm::Triple::x86: {
  10504. bool IsDarwinVectorABI = Triple.isOSDarwin();
  10505. bool RetSmallStructInRegABI =
  10506. X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
  10507. bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
  10508. if (Triple.getOS() == llvm::Triple::Win32) {
  10509. return SetCGInfo(new WinX86_32TargetCodeGenInfo(
  10510. Types, IsDarwinVectorABI, RetSmallStructInRegABI,
  10511. IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
  10512. } else {
  10513. return SetCGInfo(new X86_32TargetCodeGenInfo(
  10514. Types, IsDarwinVectorABI, RetSmallStructInRegABI,
  10515. IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
  10516. CodeGenOpts.FloatABI == "soft"));
  10517. }
  10518. }
  10519. case llvm::Triple::x86_64: {
  10520. StringRef ABI = getTarget().getABI();
  10521. X86AVXABILevel AVXLevel =
  10522. (ABI == "avx512"
  10523. ? X86AVXABILevel::AVX512
  10524. : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
  10525. switch (Triple.getOS()) {
  10526. case llvm::Triple::Win32:
  10527. return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
  10528. default:
  10529. return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
  10530. }
  10531. }
  10532. case llvm::Triple::hexagon:
  10533. return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
  10534. case llvm::Triple::lanai:
  10535. return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
  10536. case llvm::Triple::r600:
  10537. return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
  10538. case llvm::Triple::amdgcn:
  10539. return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
  10540. case llvm::Triple::sparc:
  10541. return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
  10542. case llvm::Triple::sparcv9:
  10543. return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
  10544. case llvm::Triple::xcore:
  10545. return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
  10546. case llvm::Triple::arc:
  10547. return SetCGInfo(new ARCTargetCodeGenInfo(Types));
  10548. case llvm::Triple::spir:
  10549. case llvm::Triple::spir64:
  10550. return SetCGInfo(new CommonSPIRTargetCodeGenInfo(Types));
  10551. case llvm::Triple::spirv32:
  10552. case llvm::Triple::spirv64:
  10553. return SetCGInfo(new SPIRVTargetCodeGenInfo(Types));
  10554. case llvm::Triple::ve:
  10555. return SetCGInfo(new VETargetCodeGenInfo(Types));
  10556. case llvm::Triple::csky: {
  10557. bool IsSoftFloat = !getTarget().hasFeature("hard-float-abi");
  10558. bool hasFP64 = getTarget().hasFeature("fpuv2_df") ||
  10559. getTarget().hasFeature("fpuv3_df");
  10560. return SetCGInfo(new CSKYTargetCodeGenInfo(Types, IsSoftFloat ? 0
  10561. : hasFP64 ? 64
  10562. : 32));
  10563. }
  10564. case llvm::Triple::bpfeb:
  10565. case llvm::Triple::bpfel:
  10566. return SetCGInfo(new BPFTargetCodeGenInfo(Types));
  10567. case llvm::Triple::loongarch32:
  10568. case llvm::Triple::loongarch64: {
  10569. StringRef ABIStr = getTarget().getABI();
  10570. unsigned ABIFRLen = 0;
  10571. if (ABIStr.endswith("f"))
  10572. ABIFRLen = 32;
  10573. else if (ABIStr.endswith("d"))
  10574. ABIFRLen = 64;
  10575. return SetCGInfo(new LoongArchTargetCodeGenInfo(
  10576. Types, getTarget().getPointerWidth(LangAS::Default), ABIFRLen));
  10577. }
  10578. }
  10579. }
  10580. /// Create an OpenCL kernel for an enqueued block.
  10581. ///
  10582. /// The kernel has the same function type as the block invoke function. Its
  10583. /// name is the name of the block invoke function postfixed with "_kernel".
  10584. /// It simply calls the block invoke function then returns.
  10585. llvm::Function *
  10586. TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
  10587. llvm::Function *Invoke,
  10588. llvm::Type *BlockTy) const {
  10589. auto *InvokeFT = Invoke->getFunctionType();
  10590. auto &C = CGF.getLLVMContext();
  10591. std::string Name = Invoke->getName().str() + "_kernel";
  10592. auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C),
  10593. InvokeFT->params(), false);
  10594. auto *F = llvm::Function::Create(FT, llvm::GlobalValue::ExternalLinkage, Name,
  10595. &CGF.CGM.getModule());
  10596. auto IP = CGF.Builder.saveIP();
  10597. auto *BB = llvm::BasicBlock::Create(C, "entry", F);
  10598. auto &Builder = CGF.Builder;
  10599. Builder.SetInsertPoint(BB);
  10600. llvm::SmallVector<llvm::Value *, 2> Args(llvm::make_pointer_range(F->args()));
  10601. llvm::CallInst *call = Builder.CreateCall(Invoke, Args);
  10602. call->setCallingConv(Invoke->getCallingConv());
  10603. Builder.CreateRetVoid();
  10604. Builder.restoreIP(IP);
  10605. return F;
  10606. }
  10607. /// Create an OpenCL kernel for an enqueued block.
  10608. ///
  10609. /// The type of the first argument (the block literal) is the struct type
  10610. /// of the block literal instead of a pointer type. The first argument
  10611. /// (block literal) is passed directly by value to the kernel. The kernel
  10612. /// allocates the same type of struct on stack and stores the block literal
  10613. /// to it and passes its pointer to the block invoke function. The kernel
  10614. /// has "enqueued-block" function attribute and kernel argument metadata.
  10615. llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
  10616. CodeGenFunction &CGF, llvm::Function *Invoke,
  10617. llvm::Type *BlockTy) const {
  10618. auto &Builder = CGF.Builder;
  10619. auto &C = CGF.getLLVMContext();
  10620. auto *InvokeFT = Invoke->getFunctionType();
  10621. llvm::SmallVector<llvm::Type *, 2> ArgTys;
  10622. llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
  10623. llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
  10624. llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
  10625. llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
  10626. llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
  10627. llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
  10628. ArgTys.push_back(BlockTy);
  10629. ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
  10630. AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
  10631. ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
  10632. ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
  10633. AccessQuals.push_back(llvm::MDString::get(C, "none"));
  10634. ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
  10635. for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
  10636. ArgTys.push_back(InvokeFT->getParamType(I));
  10637. ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
  10638. AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
  10639. AccessQuals.push_back(llvm::MDString::get(C, "none"));
  10640. ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
  10641. ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
  10642. ArgNames.push_back(
  10643. llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
  10644. }
  10645. std::string Name = Invoke->getName().str() + "_kernel";
  10646. auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
  10647. auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
  10648. &CGF.CGM.getModule());
  10649. F->addFnAttr("enqueued-block");
  10650. auto IP = CGF.Builder.saveIP();
  10651. auto *BB = llvm::BasicBlock::Create(C, "entry", F);
  10652. Builder.SetInsertPoint(BB);
  10653. const auto BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(BlockTy);
  10654. auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
  10655. BlockPtr->setAlignment(BlockAlign);
  10656. Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
  10657. auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
  10658. llvm::SmallVector<llvm::Value *, 2> Args;
  10659. Args.push_back(Cast);
  10660. for (llvm::Argument &A : llvm::drop_begin(F->args()))
  10661. Args.push_back(&A);
  10662. llvm::CallInst *call = Builder.CreateCall(Invoke, Args);
  10663. call->setCallingConv(Invoke->getCallingConv());
  10664. Builder.CreateRetVoid();
  10665. Builder.restoreIP(IP);
  10666. F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
  10667. F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
  10668. F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
  10669. F->setMetadata("kernel_arg_base_type",
  10670. llvm::MDNode::get(C, ArgBaseTypeNames));
  10671. F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
  10672. if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
  10673. F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
  10674. return F;
  10675. }