AArch64InstrInfo.cpp 271 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810
  1. //===- AArch64InstrInfo.cpp - AArch64 Instruction Information -------------===//
  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. // This file contains the AArch64 implementation of the TargetInstrInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "AArch64InstrInfo.h"
  13. #include "AArch64MachineFunctionInfo.h"
  14. #include "AArch64Subtarget.h"
  15. #include "MCTargetDesc/AArch64AddressingModes.h"
  16. #include "Utils/AArch64BaseInfo.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/CodeGen/MachineBasicBlock.h"
  21. #include "llvm/CodeGen/MachineFrameInfo.h"
  22. #include "llvm/CodeGen/MachineFunction.h"
  23. #include "llvm/CodeGen/MachineInstr.h"
  24. #include "llvm/CodeGen/MachineInstrBuilder.h"
  25. #include "llvm/CodeGen/MachineMemOperand.h"
  26. #include "llvm/CodeGen/MachineModuleInfo.h"
  27. #include "llvm/CodeGen/MachineOperand.h"
  28. #include "llvm/CodeGen/MachineRegisterInfo.h"
  29. #include "llvm/CodeGen/StackMaps.h"
  30. #include "llvm/CodeGen/TargetRegisterInfo.h"
  31. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  32. #include "llvm/IR/DebugInfoMetadata.h"
  33. #include "llvm/IR/DebugLoc.h"
  34. #include "llvm/IR/GlobalValue.h"
  35. #include "llvm/MC/MCAsmInfo.h"
  36. #include "llvm/MC/MCInst.h"
  37. #include "llvm/MC/MCInstBuilder.h"
  38. #include "llvm/MC/MCInstrDesc.h"
  39. #include "llvm/Support/Casting.h"
  40. #include "llvm/Support/CodeGen.h"
  41. #include "llvm/Support/CommandLine.h"
  42. #include "llvm/Support/Compiler.h"
  43. #include "llvm/Support/ErrorHandling.h"
  44. #include "llvm/Support/MathExtras.h"
  45. #include "llvm/Target/TargetMachine.h"
  46. #include "llvm/Target/TargetOptions.h"
  47. #include <cassert>
  48. #include <cstdint>
  49. #include <iterator>
  50. #include <utility>
  51. using namespace llvm;
  52. #define GET_INSTRINFO_CTOR_DTOR
  53. #include "AArch64GenInstrInfo.inc"
  54. static cl::opt<unsigned> TBZDisplacementBits(
  55. "aarch64-tbz-offset-bits", cl::Hidden, cl::init(14),
  56. cl::desc("Restrict range of TB[N]Z instructions (DEBUG)"));
  57. static cl::opt<unsigned> CBZDisplacementBits(
  58. "aarch64-cbz-offset-bits", cl::Hidden, cl::init(19),
  59. cl::desc("Restrict range of CB[N]Z instructions (DEBUG)"));
  60. static cl::opt<unsigned>
  61. BCCDisplacementBits("aarch64-bcc-offset-bits", cl::Hidden, cl::init(19),
  62. cl::desc("Restrict range of Bcc instructions (DEBUG)"));
  63. AArch64InstrInfo::AArch64InstrInfo(const AArch64Subtarget &STI)
  64. : AArch64GenInstrInfo(AArch64::ADJCALLSTACKDOWN, AArch64::ADJCALLSTACKUP,
  65. AArch64::CATCHRET),
  66. RI(STI.getTargetTriple()), Subtarget(STI) {}
  67. /// GetInstSize - Return the number of bytes of code the specified
  68. /// instruction may be. This returns the maximum number of bytes.
  69. unsigned AArch64InstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
  70. const MachineBasicBlock &MBB = *MI.getParent();
  71. const MachineFunction *MF = MBB.getParent();
  72. const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
  73. {
  74. auto Op = MI.getOpcode();
  75. if (Op == AArch64::INLINEASM || Op == AArch64::INLINEASM_BR)
  76. return getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
  77. }
  78. // Meta-instructions emit no code.
  79. if (MI.isMetaInstruction())
  80. return 0;
  81. // FIXME: We currently only handle pseudoinstructions that don't get expanded
  82. // before the assembly printer.
  83. unsigned NumBytes = 0;
  84. const MCInstrDesc &Desc = MI.getDesc();
  85. // Size should be preferably set in
  86. // llvm/lib/Target/AArch64/AArch64InstrInfo.td (default case).
  87. // Specific cases handle instructions of variable sizes
  88. switch (Desc.getOpcode()) {
  89. default:
  90. if (Desc.getSize())
  91. return Desc.getSize();
  92. // Anything not explicitly designated otherwise (i.e. pseudo-instructions
  93. // with fixed constant size but not specified in .td file) is a normal
  94. // 4-byte insn.
  95. NumBytes = 4;
  96. break;
  97. case TargetOpcode::STACKMAP:
  98. // The upper bound for a stackmap intrinsic is the full length of its shadow
  99. NumBytes = StackMapOpers(&MI).getNumPatchBytes();
  100. assert(NumBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
  101. break;
  102. case TargetOpcode::PATCHPOINT:
  103. // The size of the patchpoint intrinsic is the number of bytes requested
  104. NumBytes = PatchPointOpers(&MI).getNumPatchBytes();
  105. assert(NumBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
  106. break;
  107. case TargetOpcode::STATEPOINT:
  108. NumBytes = StatepointOpers(&MI).getNumPatchBytes();
  109. assert(NumBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
  110. // No patch bytes means a normal call inst is emitted
  111. if (NumBytes == 0)
  112. NumBytes = 4;
  113. break;
  114. case AArch64::SPACE:
  115. NumBytes = MI.getOperand(1).getImm();
  116. break;
  117. case TargetOpcode::BUNDLE:
  118. NumBytes = getInstBundleLength(MI);
  119. break;
  120. }
  121. return NumBytes;
  122. }
  123. unsigned AArch64InstrInfo::getInstBundleLength(const MachineInstr &MI) const {
  124. unsigned Size = 0;
  125. MachineBasicBlock::const_instr_iterator I = MI.getIterator();
  126. MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
  127. while (++I != E && I->isInsideBundle()) {
  128. assert(!I->isBundle() && "No nested bundle!");
  129. Size += getInstSizeInBytes(*I);
  130. }
  131. return Size;
  132. }
  133. static void parseCondBranch(MachineInstr *LastInst, MachineBasicBlock *&Target,
  134. SmallVectorImpl<MachineOperand> &Cond) {
  135. // Block ends with fall-through condbranch.
  136. switch (LastInst->getOpcode()) {
  137. default:
  138. llvm_unreachable("Unknown branch instruction?");
  139. case AArch64::Bcc:
  140. Target = LastInst->getOperand(1).getMBB();
  141. Cond.push_back(LastInst->getOperand(0));
  142. break;
  143. case AArch64::CBZW:
  144. case AArch64::CBZX:
  145. case AArch64::CBNZW:
  146. case AArch64::CBNZX:
  147. Target = LastInst->getOperand(1).getMBB();
  148. Cond.push_back(MachineOperand::CreateImm(-1));
  149. Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
  150. Cond.push_back(LastInst->getOperand(0));
  151. break;
  152. case AArch64::TBZW:
  153. case AArch64::TBZX:
  154. case AArch64::TBNZW:
  155. case AArch64::TBNZX:
  156. Target = LastInst->getOperand(2).getMBB();
  157. Cond.push_back(MachineOperand::CreateImm(-1));
  158. Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
  159. Cond.push_back(LastInst->getOperand(0));
  160. Cond.push_back(LastInst->getOperand(1));
  161. }
  162. }
  163. static unsigned getBranchDisplacementBits(unsigned Opc) {
  164. switch (Opc) {
  165. default:
  166. llvm_unreachable("unexpected opcode!");
  167. case AArch64::B:
  168. return 64;
  169. case AArch64::TBNZW:
  170. case AArch64::TBZW:
  171. case AArch64::TBNZX:
  172. case AArch64::TBZX:
  173. return TBZDisplacementBits;
  174. case AArch64::CBNZW:
  175. case AArch64::CBZW:
  176. case AArch64::CBNZX:
  177. case AArch64::CBZX:
  178. return CBZDisplacementBits;
  179. case AArch64::Bcc:
  180. return BCCDisplacementBits;
  181. }
  182. }
  183. bool AArch64InstrInfo::isBranchOffsetInRange(unsigned BranchOp,
  184. int64_t BrOffset) const {
  185. unsigned Bits = getBranchDisplacementBits(BranchOp);
  186. assert(Bits >= 3 && "max branch displacement must be enough to jump"
  187. "over conditional branch expansion");
  188. return isIntN(Bits, BrOffset / 4);
  189. }
  190. MachineBasicBlock *
  191. AArch64InstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
  192. switch (MI.getOpcode()) {
  193. default:
  194. llvm_unreachable("unexpected opcode!");
  195. case AArch64::B:
  196. return MI.getOperand(0).getMBB();
  197. case AArch64::TBZW:
  198. case AArch64::TBNZW:
  199. case AArch64::TBZX:
  200. case AArch64::TBNZX:
  201. return MI.getOperand(2).getMBB();
  202. case AArch64::CBZW:
  203. case AArch64::CBNZW:
  204. case AArch64::CBZX:
  205. case AArch64::CBNZX:
  206. case AArch64::Bcc:
  207. return MI.getOperand(1).getMBB();
  208. }
  209. }
  210. // Branch analysis.
  211. bool AArch64InstrInfo::analyzeBranch(MachineBasicBlock &MBB,
  212. MachineBasicBlock *&TBB,
  213. MachineBasicBlock *&FBB,
  214. SmallVectorImpl<MachineOperand> &Cond,
  215. bool AllowModify) const {
  216. // If the block has no terminators, it just falls into the block after it.
  217. MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
  218. if (I == MBB.end())
  219. return false;
  220. // Skip over SpeculationBarrierEndBB terminators
  221. if (I->getOpcode() == AArch64::SpeculationBarrierISBDSBEndBB ||
  222. I->getOpcode() == AArch64::SpeculationBarrierSBEndBB) {
  223. --I;
  224. }
  225. if (!isUnpredicatedTerminator(*I))
  226. return false;
  227. // Get the last instruction in the block.
  228. MachineInstr *LastInst = &*I;
  229. // If there is only one terminator instruction, process it.
  230. unsigned LastOpc = LastInst->getOpcode();
  231. if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
  232. if (isUncondBranchOpcode(LastOpc)) {
  233. TBB = LastInst->getOperand(0).getMBB();
  234. return false;
  235. }
  236. if (isCondBranchOpcode(LastOpc)) {
  237. // Block ends with fall-through condbranch.
  238. parseCondBranch(LastInst, TBB, Cond);
  239. return false;
  240. }
  241. return true; // Can't handle indirect branch.
  242. }
  243. // Get the instruction before it if it is a terminator.
  244. MachineInstr *SecondLastInst = &*I;
  245. unsigned SecondLastOpc = SecondLastInst->getOpcode();
  246. // If AllowModify is true and the block ends with two or more unconditional
  247. // branches, delete all but the first unconditional branch.
  248. if (AllowModify && isUncondBranchOpcode(LastOpc)) {
  249. while (isUncondBranchOpcode(SecondLastOpc)) {
  250. LastInst->eraseFromParent();
  251. LastInst = SecondLastInst;
  252. LastOpc = LastInst->getOpcode();
  253. if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
  254. // Return now the only terminator is an unconditional branch.
  255. TBB = LastInst->getOperand(0).getMBB();
  256. return false;
  257. } else {
  258. SecondLastInst = &*I;
  259. SecondLastOpc = SecondLastInst->getOpcode();
  260. }
  261. }
  262. }
  263. // If we're allowed to modify and the block ends in a unconditional branch
  264. // which could simply fallthrough, remove the branch. (Note: This case only
  265. // matters when we can't understand the whole sequence, otherwise it's also
  266. // handled by BranchFolding.cpp.)
  267. if (AllowModify && isUncondBranchOpcode(LastOpc) &&
  268. MBB.isLayoutSuccessor(getBranchDestBlock(*LastInst))) {
  269. LastInst->eraseFromParent();
  270. LastInst = SecondLastInst;
  271. LastOpc = LastInst->getOpcode();
  272. if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
  273. assert(!isUncondBranchOpcode(LastOpc) &&
  274. "unreachable unconditional branches removed above");
  275. if (isCondBranchOpcode(LastOpc)) {
  276. // Block ends with fall-through condbranch.
  277. parseCondBranch(LastInst, TBB, Cond);
  278. return false;
  279. }
  280. return true; // Can't handle indirect branch.
  281. } else {
  282. SecondLastInst = &*I;
  283. SecondLastOpc = SecondLastInst->getOpcode();
  284. }
  285. }
  286. // If there are three terminators, we don't know what sort of block this is.
  287. if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(*--I))
  288. return true;
  289. // If the block ends with a B and a Bcc, handle it.
  290. if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
  291. parseCondBranch(SecondLastInst, TBB, Cond);
  292. FBB = LastInst->getOperand(0).getMBB();
  293. return false;
  294. }
  295. // If the block ends with two unconditional branches, handle it. The second
  296. // one is not executed, so remove it.
  297. if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
  298. TBB = SecondLastInst->getOperand(0).getMBB();
  299. I = LastInst;
  300. if (AllowModify)
  301. I->eraseFromParent();
  302. return false;
  303. }
  304. // ...likewise if it ends with an indirect branch followed by an unconditional
  305. // branch.
  306. if (isIndirectBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
  307. I = LastInst;
  308. if (AllowModify)
  309. I->eraseFromParent();
  310. return true;
  311. }
  312. // Otherwise, can't handle this.
  313. return true;
  314. }
  315. bool AArch64InstrInfo::analyzeBranchPredicate(MachineBasicBlock &MBB,
  316. MachineBranchPredicate &MBP,
  317. bool AllowModify) const {
  318. // For the moment, handle only a block which ends with a cb(n)zx followed by
  319. // a fallthrough. Why this? Because it is a common form.
  320. // TODO: Should we handle b.cc?
  321. MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
  322. if (I == MBB.end())
  323. return true;
  324. // Skip over SpeculationBarrierEndBB terminators
  325. if (I->getOpcode() == AArch64::SpeculationBarrierISBDSBEndBB ||
  326. I->getOpcode() == AArch64::SpeculationBarrierSBEndBB) {
  327. --I;
  328. }
  329. if (!isUnpredicatedTerminator(*I))
  330. return true;
  331. // Get the last instruction in the block.
  332. MachineInstr *LastInst = &*I;
  333. unsigned LastOpc = LastInst->getOpcode();
  334. if (!isCondBranchOpcode(LastOpc))
  335. return true;
  336. switch (LastOpc) {
  337. default:
  338. return true;
  339. case AArch64::CBZW:
  340. case AArch64::CBZX:
  341. case AArch64::CBNZW:
  342. case AArch64::CBNZX:
  343. break;
  344. };
  345. MBP.TrueDest = LastInst->getOperand(1).getMBB();
  346. assert(MBP.TrueDest && "expected!");
  347. MBP.FalseDest = MBB.getNextNode();
  348. MBP.ConditionDef = nullptr;
  349. MBP.SingleUseCondition = false;
  350. MBP.LHS = LastInst->getOperand(0);
  351. MBP.RHS = MachineOperand::CreateImm(0);
  352. MBP.Predicate = LastOpc == AArch64::CBNZX ? MachineBranchPredicate::PRED_NE
  353. : MachineBranchPredicate::PRED_EQ;
  354. return false;
  355. }
  356. bool AArch64InstrInfo::reverseBranchCondition(
  357. SmallVectorImpl<MachineOperand> &Cond) const {
  358. if (Cond[0].getImm() != -1) {
  359. // Regular Bcc
  360. AArch64CC::CondCode CC = (AArch64CC::CondCode)(int)Cond[0].getImm();
  361. Cond[0].setImm(AArch64CC::getInvertedCondCode(CC));
  362. } else {
  363. // Folded compare-and-branch
  364. switch (Cond[1].getImm()) {
  365. default:
  366. llvm_unreachable("Unknown conditional branch!");
  367. case AArch64::CBZW:
  368. Cond[1].setImm(AArch64::CBNZW);
  369. break;
  370. case AArch64::CBNZW:
  371. Cond[1].setImm(AArch64::CBZW);
  372. break;
  373. case AArch64::CBZX:
  374. Cond[1].setImm(AArch64::CBNZX);
  375. break;
  376. case AArch64::CBNZX:
  377. Cond[1].setImm(AArch64::CBZX);
  378. break;
  379. case AArch64::TBZW:
  380. Cond[1].setImm(AArch64::TBNZW);
  381. break;
  382. case AArch64::TBNZW:
  383. Cond[1].setImm(AArch64::TBZW);
  384. break;
  385. case AArch64::TBZX:
  386. Cond[1].setImm(AArch64::TBNZX);
  387. break;
  388. case AArch64::TBNZX:
  389. Cond[1].setImm(AArch64::TBZX);
  390. break;
  391. }
  392. }
  393. return false;
  394. }
  395. unsigned AArch64InstrInfo::removeBranch(MachineBasicBlock &MBB,
  396. int *BytesRemoved) const {
  397. MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
  398. if (I == MBB.end())
  399. return 0;
  400. if (!isUncondBranchOpcode(I->getOpcode()) &&
  401. !isCondBranchOpcode(I->getOpcode()))
  402. return 0;
  403. // Remove the branch.
  404. I->eraseFromParent();
  405. I = MBB.end();
  406. if (I == MBB.begin()) {
  407. if (BytesRemoved)
  408. *BytesRemoved = 4;
  409. return 1;
  410. }
  411. --I;
  412. if (!isCondBranchOpcode(I->getOpcode())) {
  413. if (BytesRemoved)
  414. *BytesRemoved = 4;
  415. return 1;
  416. }
  417. // Remove the branch.
  418. I->eraseFromParent();
  419. if (BytesRemoved)
  420. *BytesRemoved = 8;
  421. return 2;
  422. }
  423. void AArch64InstrInfo::instantiateCondBranch(
  424. MachineBasicBlock &MBB, const DebugLoc &DL, MachineBasicBlock *TBB,
  425. ArrayRef<MachineOperand> Cond) const {
  426. if (Cond[0].getImm() != -1) {
  427. // Regular Bcc
  428. BuildMI(&MBB, DL, get(AArch64::Bcc)).addImm(Cond[0].getImm()).addMBB(TBB);
  429. } else {
  430. // Folded compare-and-branch
  431. // Note that we use addOperand instead of addReg to keep the flags.
  432. const MachineInstrBuilder MIB =
  433. BuildMI(&MBB, DL, get(Cond[1].getImm())).add(Cond[2]);
  434. if (Cond.size() > 3)
  435. MIB.addImm(Cond[3].getImm());
  436. MIB.addMBB(TBB);
  437. }
  438. }
  439. unsigned AArch64InstrInfo::insertBranch(
  440. MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
  441. ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
  442. // Shouldn't be a fall through.
  443. assert(TBB && "insertBranch must not be told to insert a fallthrough");
  444. if (!FBB) {
  445. if (Cond.empty()) // Unconditional branch?
  446. BuildMI(&MBB, DL, get(AArch64::B)).addMBB(TBB);
  447. else
  448. instantiateCondBranch(MBB, DL, TBB, Cond);
  449. if (BytesAdded)
  450. *BytesAdded = 4;
  451. return 1;
  452. }
  453. // Two-way conditional branch.
  454. instantiateCondBranch(MBB, DL, TBB, Cond);
  455. BuildMI(&MBB, DL, get(AArch64::B)).addMBB(FBB);
  456. if (BytesAdded)
  457. *BytesAdded = 8;
  458. return 2;
  459. }
  460. // Find the original register that VReg is copied from.
  461. static unsigned removeCopies(const MachineRegisterInfo &MRI, unsigned VReg) {
  462. while (Register::isVirtualRegister(VReg)) {
  463. const MachineInstr *DefMI = MRI.getVRegDef(VReg);
  464. if (!DefMI->isFullCopy())
  465. return VReg;
  466. VReg = DefMI->getOperand(1).getReg();
  467. }
  468. return VReg;
  469. }
  470. // Determine if VReg is defined by an instruction that can be folded into a
  471. // csel instruction. If so, return the folded opcode, and the replacement
  472. // register.
  473. static unsigned canFoldIntoCSel(const MachineRegisterInfo &MRI, unsigned VReg,
  474. unsigned *NewVReg = nullptr) {
  475. VReg = removeCopies(MRI, VReg);
  476. if (!Register::isVirtualRegister(VReg))
  477. return 0;
  478. bool Is64Bit = AArch64::GPR64allRegClass.hasSubClassEq(MRI.getRegClass(VReg));
  479. const MachineInstr *DefMI = MRI.getVRegDef(VReg);
  480. unsigned Opc = 0;
  481. unsigned SrcOpNum = 0;
  482. switch (DefMI->getOpcode()) {
  483. case AArch64::ADDSXri:
  484. case AArch64::ADDSWri:
  485. // if NZCV is used, do not fold.
  486. if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) == -1)
  487. return 0;
  488. // fall-through to ADDXri and ADDWri.
  489. LLVM_FALLTHROUGH;
  490. case AArch64::ADDXri:
  491. case AArch64::ADDWri:
  492. // add x, 1 -> csinc.
  493. if (!DefMI->getOperand(2).isImm() || DefMI->getOperand(2).getImm() != 1 ||
  494. DefMI->getOperand(3).getImm() != 0)
  495. return 0;
  496. SrcOpNum = 1;
  497. Opc = Is64Bit ? AArch64::CSINCXr : AArch64::CSINCWr;
  498. break;
  499. case AArch64::ORNXrr:
  500. case AArch64::ORNWrr: {
  501. // not x -> csinv, represented as orn dst, xzr, src.
  502. unsigned ZReg = removeCopies(MRI, DefMI->getOperand(1).getReg());
  503. if (ZReg != AArch64::XZR && ZReg != AArch64::WZR)
  504. return 0;
  505. SrcOpNum = 2;
  506. Opc = Is64Bit ? AArch64::CSINVXr : AArch64::CSINVWr;
  507. break;
  508. }
  509. case AArch64::SUBSXrr:
  510. case AArch64::SUBSWrr:
  511. // if NZCV is used, do not fold.
  512. if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) == -1)
  513. return 0;
  514. // fall-through to SUBXrr and SUBWrr.
  515. LLVM_FALLTHROUGH;
  516. case AArch64::SUBXrr:
  517. case AArch64::SUBWrr: {
  518. // neg x -> csneg, represented as sub dst, xzr, src.
  519. unsigned ZReg = removeCopies(MRI, DefMI->getOperand(1).getReg());
  520. if (ZReg != AArch64::XZR && ZReg != AArch64::WZR)
  521. return 0;
  522. SrcOpNum = 2;
  523. Opc = Is64Bit ? AArch64::CSNEGXr : AArch64::CSNEGWr;
  524. break;
  525. }
  526. default:
  527. return 0;
  528. }
  529. assert(Opc && SrcOpNum && "Missing parameters");
  530. if (NewVReg)
  531. *NewVReg = DefMI->getOperand(SrcOpNum).getReg();
  532. return Opc;
  533. }
  534. bool AArch64InstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
  535. ArrayRef<MachineOperand> Cond,
  536. Register DstReg, Register TrueReg,
  537. Register FalseReg, int &CondCycles,
  538. int &TrueCycles,
  539. int &FalseCycles) const {
  540. // Check register classes.
  541. const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
  542. const TargetRegisterClass *RC =
  543. RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
  544. if (!RC)
  545. return false;
  546. // Also need to check the dest regclass, in case we're trying to optimize
  547. // something like:
  548. // %1(gpr) = PHI %2(fpr), bb1, %(fpr), bb2
  549. if (!RI.getCommonSubClass(RC, MRI.getRegClass(DstReg)))
  550. return false;
  551. // Expanding cbz/tbz requires an extra cycle of latency on the condition.
  552. unsigned ExtraCondLat = Cond.size() != 1;
  553. // GPRs are handled by csel.
  554. // FIXME: Fold in x+1, -x, and ~x when applicable.
  555. if (AArch64::GPR64allRegClass.hasSubClassEq(RC) ||
  556. AArch64::GPR32allRegClass.hasSubClassEq(RC)) {
  557. // Single-cycle csel, csinc, csinv, and csneg.
  558. CondCycles = 1 + ExtraCondLat;
  559. TrueCycles = FalseCycles = 1;
  560. if (canFoldIntoCSel(MRI, TrueReg))
  561. TrueCycles = 0;
  562. else if (canFoldIntoCSel(MRI, FalseReg))
  563. FalseCycles = 0;
  564. return true;
  565. }
  566. // Scalar floating point is handled by fcsel.
  567. // FIXME: Form fabs, fmin, and fmax when applicable.
  568. if (AArch64::FPR64RegClass.hasSubClassEq(RC) ||
  569. AArch64::FPR32RegClass.hasSubClassEq(RC)) {
  570. CondCycles = 5 + ExtraCondLat;
  571. TrueCycles = FalseCycles = 2;
  572. return true;
  573. }
  574. // Can't do vectors.
  575. return false;
  576. }
  577. void AArch64InstrInfo::insertSelect(MachineBasicBlock &MBB,
  578. MachineBasicBlock::iterator I,
  579. const DebugLoc &DL, Register DstReg,
  580. ArrayRef<MachineOperand> Cond,
  581. Register TrueReg, Register FalseReg) const {
  582. MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
  583. // Parse the condition code, see parseCondBranch() above.
  584. AArch64CC::CondCode CC;
  585. switch (Cond.size()) {
  586. default:
  587. llvm_unreachable("Unknown condition opcode in Cond");
  588. case 1: // b.cc
  589. CC = AArch64CC::CondCode(Cond[0].getImm());
  590. break;
  591. case 3: { // cbz/cbnz
  592. // We must insert a compare against 0.
  593. bool Is64Bit;
  594. switch (Cond[1].getImm()) {
  595. default:
  596. llvm_unreachable("Unknown branch opcode in Cond");
  597. case AArch64::CBZW:
  598. Is64Bit = false;
  599. CC = AArch64CC::EQ;
  600. break;
  601. case AArch64::CBZX:
  602. Is64Bit = true;
  603. CC = AArch64CC::EQ;
  604. break;
  605. case AArch64::CBNZW:
  606. Is64Bit = false;
  607. CC = AArch64CC::NE;
  608. break;
  609. case AArch64::CBNZX:
  610. Is64Bit = true;
  611. CC = AArch64CC::NE;
  612. break;
  613. }
  614. Register SrcReg = Cond[2].getReg();
  615. if (Is64Bit) {
  616. // cmp reg, #0 is actually subs xzr, reg, #0.
  617. MRI.constrainRegClass(SrcReg, &AArch64::GPR64spRegClass);
  618. BuildMI(MBB, I, DL, get(AArch64::SUBSXri), AArch64::XZR)
  619. .addReg(SrcReg)
  620. .addImm(0)
  621. .addImm(0);
  622. } else {
  623. MRI.constrainRegClass(SrcReg, &AArch64::GPR32spRegClass);
  624. BuildMI(MBB, I, DL, get(AArch64::SUBSWri), AArch64::WZR)
  625. .addReg(SrcReg)
  626. .addImm(0)
  627. .addImm(0);
  628. }
  629. break;
  630. }
  631. case 4: { // tbz/tbnz
  632. // We must insert a tst instruction.
  633. switch (Cond[1].getImm()) {
  634. default:
  635. llvm_unreachable("Unknown branch opcode in Cond");
  636. case AArch64::TBZW:
  637. case AArch64::TBZX:
  638. CC = AArch64CC::EQ;
  639. break;
  640. case AArch64::TBNZW:
  641. case AArch64::TBNZX:
  642. CC = AArch64CC::NE;
  643. break;
  644. }
  645. // cmp reg, #foo is actually ands xzr, reg, #1<<foo.
  646. if (Cond[1].getImm() == AArch64::TBZW || Cond[1].getImm() == AArch64::TBNZW)
  647. BuildMI(MBB, I, DL, get(AArch64::ANDSWri), AArch64::WZR)
  648. .addReg(Cond[2].getReg())
  649. .addImm(
  650. AArch64_AM::encodeLogicalImmediate(1ull << Cond[3].getImm(), 32));
  651. else
  652. BuildMI(MBB, I, DL, get(AArch64::ANDSXri), AArch64::XZR)
  653. .addReg(Cond[2].getReg())
  654. .addImm(
  655. AArch64_AM::encodeLogicalImmediate(1ull << Cond[3].getImm(), 64));
  656. break;
  657. }
  658. }
  659. unsigned Opc = 0;
  660. const TargetRegisterClass *RC = nullptr;
  661. bool TryFold = false;
  662. if (MRI.constrainRegClass(DstReg, &AArch64::GPR64RegClass)) {
  663. RC = &AArch64::GPR64RegClass;
  664. Opc = AArch64::CSELXr;
  665. TryFold = true;
  666. } else if (MRI.constrainRegClass(DstReg, &AArch64::GPR32RegClass)) {
  667. RC = &AArch64::GPR32RegClass;
  668. Opc = AArch64::CSELWr;
  669. TryFold = true;
  670. } else if (MRI.constrainRegClass(DstReg, &AArch64::FPR64RegClass)) {
  671. RC = &AArch64::FPR64RegClass;
  672. Opc = AArch64::FCSELDrrr;
  673. } else if (MRI.constrainRegClass(DstReg, &AArch64::FPR32RegClass)) {
  674. RC = &AArch64::FPR32RegClass;
  675. Opc = AArch64::FCSELSrrr;
  676. }
  677. assert(RC && "Unsupported regclass");
  678. // Try folding simple instructions into the csel.
  679. if (TryFold) {
  680. unsigned NewVReg = 0;
  681. unsigned FoldedOpc = canFoldIntoCSel(MRI, TrueReg, &NewVReg);
  682. if (FoldedOpc) {
  683. // The folded opcodes csinc, csinc and csneg apply the operation to
  684. // FalseReg, so we need to invert the condition.
  685. CC = AArch64CC::getInvertedCondCode(CC);
  686. TrueReg = FalseReg;
  687. } else
  688. FoldedOpc = canFoldIntoCSel(MRI, FalseReg, &NewVReg);
  689. // Fold the operation. Leave any dead instructions for DCE to clean up.
  690. if (FoldedOpc) {
  691. FalseReg = NewVReg;
  692. Opc = FoldedOpc;
  693. // The extends the live range of NewVReg.
  694. MRI.clearKillFlags(NewVReg);
  695. }
  696. }
  697. // Pull all virtual register into the appropriate class.
  698. MRI.constrainRegClass(TrueReg, RC);
  699. MRI.constrainRegClass(FalseReg, RC);
  700. // Insert the csel.
  701. BuildMI(MBB, I, DL, get(Opc), DstReg)
  702. .addReg(TrueReg)
  703. .addReg(FalseReg)
  704. .addImm(CC);
  705. }
  706. /// Returns true if a MOVi32imm or MOVi64imm can be expanded to an ORRxx.
  707. static bool canBeExpandedToORR(const MachineInstr &MI, unsigned BitSize) {
  708. uint64_t Imm = MI.getOperand(1).getImm();
  709. uint64_t UImm = Imm << (64 - BitSize) >> (64 - BitSize);
  710. uint64_t Encoding;
  711. return AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding);
  712. }
  713. // FIXME: this implementation should be micro-architecture dependent, so a
  714. // micro-architecture target hook should be introduced here in future.
  715. bool AArch64InstrInfo::isAsCheapAsAMove(const MachineInstr &MI) const {
  716. if (!Subtarget.hasCustomCheapAsMoveHandling())
  717. return MI.isAsCheapAsAMove();
  718. const unsigned Opcode = MI.getOpcode();
  719. // Firstly, check cases gated by features.
  720. if (Subtarget.hasZeroCycleZeroingFP()) {
  721. if (Opcode == AArch64::FMOVH0 ||
  722. Opcode == AArch64::FMOVS0 ||
  723. Opcode == AArch64::FMOVD0)
  724. return true;
  725. }
  726. if (Subtarget.hasZeroCycleZeroingGP()) {
  727. if (Opcode == TargetOpcode::COPY &&
  728. (MI.getOperand(1).getReg() == AArch64::WZR ||
  729. MI.getOperand(1).getReg() == AArch64::XZR))
  730. return true;
  731. }
  732. // Secondly, check cases specific to sub-targets.
  733. if (Subtarget.hasExynosCheapAsMoveHandling()) {
  734. if (isExynosCheapAsMove(MI))
  735. return true;
  736. return MI.isAsCheapAsAMove();
  737. }
  738. // Finally, check generic cases.
  739. switch (Opcode) {
  740. default:
  741. return false;
  742. // add/sub on register without shift
  743. case AArch64::ADDWri:
  744. case AArch64::ADDXri:
  745. case AArch64::SUBWri:
  746. case AArch64::SUBXri:
  747. return (MI.getOperand(3).getImm() == 0);
  748. // logical ops on immediate
  749. case AArch64::ANDWri:
  750. case AArch64::ANDXri:
  751. case AArch64::EORWri:
  752. case AArch64::EORXri:
  753. case AArch64::ORRWri:
  754. case AArch64::ORRXri:
  755. return true;
  756. // logical ops on register without shift
  757. case AArch64::ANDWrr:
  758. case AArch64::ANDXrr:
  759. case AArch64::BICWrr:
  760. case AArch64::BICXrr:
  761. case AArch64::EONWrr:
  762. case AArch64::EONXrr:
  763. case AArch64::EORWrr:
  764. case AArch64::EORXrr:
  765. case AArch64::ORNWrr:
  766. case AArch64::ORNXrr:
  767. case AArch64::ORRWrr:
  768. case AArch64::ORRXrr:
  769. return true;
  770. // If MOVi32imm or MOVi64imm can be expanded into ORRWri or
  771. // ORRXri, it is as cheap as MOV
  772. case AArch64::MOVi32imm:
  773. return canBeExpandedToORR(MI, 32);
  774. case AArch64::MOVi64imm:
  775. return canBeExpandedToORR(MI, 64);
  776. }
  777. llvm_unreachable("Unknown opcode to check as cheap as a move!");
  778. }
  779. bool AArch64InstrInfo::isFalkorShiftExtFast(const MachineInstr &MI) {
  780. switch (MI.getOpcode()) {
  781. default:
  782. return false;
  783. case AArch64::ADDWrs:
  784. case AArch64::ADDXrs:
  785. case AArch64::ADDSWrs:
  786. case AArch64::ADDSXrs: {
  787. unsigned Imm = MI.getOperand(3).getImm();
  788. unsigned ShiftVal = AArch64_AM::getShiftValue(Imm);
  789. if (ShiftVal == 0)
  790. return true;
  791. return AArch64_AM::getShiftType(Imm) == AArch64_AM::LSL && ShiftVal <= 5;
  792. }
  793. case AArch64::ADDWrx:
  794. case AArch64::ADDXrx:
  795. case AArch64::ADDXrx64:
  796. case AArch64::ADDSWrx:
  797. case AArch64::ADDSXrx:
  798. case AArch64::ADDSXrx64: {
  799. unsigned Imm = MI.getOperand(3).getImm();
  800. switch (AArch64_AM::getArithExtendType(Imm)) {
  801. default:
  802. return false;
  803. case AArch64_AM::UXTB:
  804. case AArch64_AM::UXTH:
  805. case AArch64_AM::UXTW:
  806. case AArch64_AM::UXTX:
  807. return AArch64_AM::getArithShiftValue(Imm) <= 4;
  808. }
  809. }
  810. case AArch64::SUBWrs:
  811. case AArch64::SUBSWrs: {
  812. unsigned Imm = MI.getOperand(3).getImm();
  813. unsigned ShiftVal = AArch64_AM::getShiftValue(Imm);
  814. return ShiftVal == 0 ||
  815. (AArch64_AM::getShiftType(Imm) == AArch64_AM::ASR && ShiftVal == 31);
  816. }
  817. case AArch64::SUBXrs:
  818. case AArch64::SUBSXrs: {
  819. unsigned Imm = MI.getOperand(3).getImm();
  820. unsigned ShiftVal = AArch64_AM::getShiftValue(Imm);
  821. return ShiftVal == 0 ||
  822. (AArch64_AM::getShiftType(Imm) == AArch64_AM::ASR && ShiftVal == 63);
  823. }
  824. case AArch64::SUBWrx:
  825. case AArch64::SUBXrx:
  826. case AArch64::SUBXrx64:
  827. case AArch64::SUBSWrx:
  828. case AArch64::SUBSXrx:
  829. case AArch64::SUBSXrx64: {
  830. unsigned Imm = MI.getOperand(3).getImm();
  831. switch (AArch64_AM::getArithExtendType(Imm)) {
  832. default:
  833. return false;
  834. case AArch64_AM::UXTB:
  835. case AArch64_AM::UXTH:
  836. case AArch64_AM::UXTW:
  837. case AArch64_AM::UXTX:
  838. return AArch64_AM::getArithShiftValue(Imm) == 0;
  839. }
  840. }
  841. case AArch64::LDRBBroW:
  842. case AArch64::LDRBBroX:
  843. case AArch64::LDRBroW:
  844. case AArch64::LDRBroX:
  845. case AArch64::LDRDroW:
  846. case AArch64::LDRDroX:
  847. case AArch64::LDRHHroW:
  848. case AArch64::LDRHHroX:
  849. case AArch64::LDRHroW:
  850. case AArch64::LDRHroX:
  851. case AArch64::LDRQroW:
  852. case AArch64::LDRQroX:
  853. case AArch64::LDRSBWroW:
  854. case AArch64::LDRSBWroX:
  855. case AArch64::LDRSBXroW:
  856. case AArch64::LDRSBXroX:
  857. case AArch64::LDRSHWroW:
  858. case AArch64::LDRSHWroX:
  859. case AArch64::LDRSHXroW:
  860. case AArch64::LDRSHXroX:
  861. case AArch64::LDRSWroW:
  862. case AArch64::LDRSWroX:
  863. case AArch64::LDRSroW:
  864. case AArch64::LDRSroX:
  865. case AArch64::LDRWroW:
  866. case AArch64::LDRWroX:
  867. case AArch64::LDRXroW:
  868. case AArch64::LDRXroX:
  869. case AArch64::PRFMroW:
  870. case AArch64::PRFMroX:
  871. case AArch64::STRBBroW:
  872. case AArch64::STRBBroX:
  873. case AArch64::STRBroW:
  874. case AArch64::STRBroX:
  875. case AArch64::STRDroW:
  876. case AArch64::STRDroX:
  877. case AArch64::STRHHroW:
  878. case AArch64::STRHHroX:
  879. case AArch64::STRHroW:
  880. case AArch64::STRHroX:
  881. case AArch64::STRQroW:
  882. case AArch64::STRQroX:
  883. case AArch64::STRSroW:
  884. case AArch64::STRSroX:
  885. case AArch64::STRWroW:
  886. case AArch64::STRWroX:
  887. case AArch64::STRXroW:
  888. case AArch64::STRXroX: {
  889. unsigned IsSigned = MI.getOperand(3).getImm();
  890. return !IsSigned;
  891. }
  892. }
  893. }
  894. bool AArch64InstrInfo::isSEHInstruction(const MachineInstr &MI) {
  895. unsigned Opc = MI.getOpcode();
  896. switch (Opc) {
  897. default:
  898. return false;
  899. case AArch64::SEH_StackAlloc:
  900. case AArch64::SEH_SaveFPLR:
  901. case AArch64::SEH_SaveFPLR_X:
  902. case AArch64::SEH_SaveReg:
  903. case AArch64::SEH_SaveReg_X:
  904. case AArch64::SEH_SaveRegP:
  905. case AArch64::SEH_SaveRegP_X:
  906. case AArch64::SEH_SaveFReg:
  907. case AArch64::SEH_SaveFReg_X:
  908. case AArch64::SEH_SaveFRegP:
  909. case AArch64::SEH_SaveFRegP_X:
  910. case AArch64::SEH_SetFP:
  911. case AArch64::SEH_AddFP:
  912. case AArch64::SEH_Nop:
  913. case AArch64::SEH_PrologEnd:
  914. case AArch64::SEH_EpilogStart:
  915. case AArch64::SEH_EpilogEnd:
  916. return true;
  917. }
  918. }
  919. bool AArch64InstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
  920. Register &SrcReg, Register &DstReg,
  921. unsigned &SubIdx) const {
  922. switch (MI.getOpcode()) {
  923. default:
  924. return false;
  925. case AArch64::SBFMXri: // aka sxtw
  926. case AArch64::UBFMXri: // aka uxtw
  927. // Check for the 32 -> 64 bit extension case, these instructions can do
  928. // much more.
  929. if (MI.getOperand(2).getImm() != 0 || MI.getOperand(3).getImm() != 31)
  930. return false;
  931. // This is a signed or unsigned 32 -> 64 bit extension.
  932. SrcReg = MI.getOperand(1).getReg();
  933. DstReg = MI.getOperand(0).getReg();
  934. SubIdx = AArch64::sub_32;
  935. return true;
  936. }
  937. }
  938. bool AArch64InstrInfo::areMemAccessesTriviallyDisjoint(
  939. const MachineInstr &MIa, const MachineInstr &MIb) const {
  940. const TargetRegisterInfo *TRI = &getRegisterInfo();
  941. const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr;
  942. int64_t OffsetA = 0, OffsetB = 0;
  943. unsigned WidthA = 0, WidthB = 0;
  944. bool OffsetAIsScalable = false, OffsetBIsScalable = false;
  945. assert(MIa.mayLoadOrStore() && "MIa must be a load or store.");
  946. assert(MIb.mayLoadOrStore() && "MIb must be a load or store.");
  947. if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
  948. MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
  949. return false;
  950. // Retrieve the base, offset from the base and width. Width
  951. // is the size of memory that is being loaded/stored (e.g. 1, 2, 4, 8). If
  952. // base are identical, and the offset of a lower memory access +
  953. // the width doesn't overlap the offset of a higher memory access,
  954. // then the memory accesses are different.
  955. // If OffsetAIsScalable and OffsetBIsScalable are both true, they
  956. // are assumed to have the same scale (vscale).
  957. if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, OffsetAIsScalable,
  958. WidthA, TRI) &&
  959. getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, OffsetBIsScalable,
  960. WidthB, TRI)) {
  961. if (BaseOpA->isIdenticalTo(*BaseOpB) &&
  962. OffsetAIsScalable == OffsetBIsScalable) {
  963. int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
  964. int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
  965. int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
  966. if (LowOffset + LowWidth <= HighOffset)
  967. return true;
  968. }
  969. }
  970. return false;
  971. }
  972. bool AArch64InstrInfo::isSchedulingBoundary(const MachineInstr &MI,
  973. const MachineBasicBlock *MBB,
  974. const MachineFunction &MF) const {
  975. if (TargetInstrInfo::isSchedulingBoundary(MI, MBB, MF))
  976. return true;
  977. switch (MI.getOpcode()) {
  978. case AArch64::HINT:
  979. // CSDB hints are scheduling barriers.
  980. if (MI.getOperand(0).getImm() == 0x14)
  981. return true;
  982. break;
  983. case AArch64::DSB:
  984. case AArch64::ISB:
  985. // DSB and ISB also are scheduling barriers.
  986. return true;
  987. default:;
  988. }
  989. return isSEHInstruction(MI);
  990. }
  991. /// analyzeCompare - For a comparison instruction, return the source registers
  992. /// in SrcReg and SrcReg2, and the value it compares against in CmpValue.
  993. /// Return true if the comparison instruction can be analyzed.
  994. bool AArch64InstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg,
  995. Register &SrcReg2, int64_t &CmpMask,
  996. int64_t &CmpValue) const {
  997. // The first operand can be a frame index where we'd normally expect a
  998. // register.
  999. assert(MI.getNumOperands() >= 2 && "All AArch64 cmps should have 2 operands");
  1000. if (!MI.getOperand(1).isReg())
  1001. return false;
  1002. switch (MI.getOpcode()) {
  1003. default:
  1004. break;
  1005. case AArch64::PTEST_PP:
  1006. SrcReg = MI.getOperand(0).getReg();
  1007. SrcReg2 = MI.getOperand(1).getReg();
  1008. // Not sure about the mask and value for now...
  1009. CmpMask = ~0;
  1010. CmpValue = 0;
  1011. return true;
  1012. case AArch64::SUBSWrr:
  1013. case AArch64::SUBSWrs:
  1014. case AArch64::SUBSWrx:
  1015. case AArch64::SUBSXrr:
  1016. case AArch64::SUBSXrs:
  1017. case AArch64::SUBSXrx:
  1018. case AArch64::ADDSWrr:
  1019. case AArch64::ADDSWrs:
  1020. case AArch64::ADDSWrx:
  1021. case AArch64::ADDSXrr:
  1022. case AArch64::ADDSXrs:
  1023. case AArch64::ADDSXrx:
  1024. // Replace SUBSWrr with SUBWrr if NZCV is not used.
  1025. SrcReg = MI.getOperand(1).getReg();
  1026. SrcReg2 = MI.getOperand(2).getReg();
  1027. CmpMask = ~0;
  1028. CmpValue = 0;
  1029. return true;
  1030. case AArch64::SUBSWri:
  1031. case AArch64::ADDSWri:
  1032. case AArch64::SUBSXri:
  1033. case AArch64::ADDSXri:
  1034. SrcReg = MI.getOperand(1).getReg();
  1035. SrcReg2 = 0;
  1036. CmpMask = ~0;
  1037. CmpValue = MI.getOperand(2).getImm();
  1038. return true;
  1039. case AArch64::ANDSWri:
  1040. case AArch64::ANDSXri:
  1041. // ANDS does not use the same encoding scheme as the others xxxS
  1042. // instructions.
  1043. SrcReg = MI.getOperand(1).getReg();
  1044. SrcReg2 = 0;
  1045. CmpMask = ~0;
  1046. CmpValue = AArch64_AM::decodeLogicalImmediate(
  1047. MI.getOperand(2).getImm(),
  1048. MI.getOpcode() == AArch64::ANDSWri ? 32 : 64);
  1049. return true;
  1050. }
  1051. return false;
  1052. }
  1053. static bool UpdateOperandRegClass(MachineInstr &Instr) {
  1054. MachineBasicBlock *MBB = Instr.getParent();
  1055. assert(MBB && "Can't get MachineBasicBlock here");
  1056. MachineFunction *MF = MBB->getParent();
  1057. assert(MF && "Can't get MachineFunction here");
  1058. const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
  1059. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  1060. MachineRegisterInfo *MRI = &MF->getRegInfo();
  1061. for (unsigned OpIdx = 0, EndIdx = Instr.getNumOperands(); OpIdx < EndIdx;
  1062. ++OpIdx) {
  1063. MachineOperand &MO = Instr.getOperand(OpIdx);
  1064. const TargetRegisterClass *OpRegCstraints =
  1065. Instr.getRegClassConstraint(OpIdx, TII, TRI);
  1066. // If there's no constraint, there's nothing to do.
  1067. if (!OpRegCstraints)
  1068. continue;
  1069. // If the operand is a frame index, there's nothing to do here.
  1070. // A frame index operand will resolve correctly during PEI.
  1071. if (MO.isFI())
  1072. continue;
  1073. assert(MO.isReg() &&
  1074. "Operand has register constraints without being a register!");
  1075. Register Reg = MO.getReg();
  1076. if (Register::isPhysicalRegister(Reg)) {
  1077. if (!OpRegCstraints->contains(Reg))
  1078. return false;
  1079. } else if (!OpRegCstraints->hasSubClassEq(MRI->getRegClass(Reg)) &&
  1080. !MRI->constrainRegClass(Reg, OpRegCstraints))
  1081. return false;
  1082. }
  1083. return true;
  1084. }
  1085. /// Return the opcode that does not set flags when possible - otherwise
  1086. /// return the original opcode. The caller is responsible to do the actual
  1087. /// substitution and legality checking.
  1088. static unsigned convertToNonFlagSettingOpc(const MachineInstr &MI) {
  1089. // Don't convert all compare instructions, because for some the zero register
  1090. // encoding becomes the sp register.
  1091. bool MIDefinesZeroReg = false;
  1092. if (MI.definesRegister(AArch64::WZR) || MI.definesRegister(AArch64::XZR))
  1093. MIDefinesZeroReg = true;
  1094. switch (MI.getOpcode()) {
  1095. default:
  1096. return MI.getOpcode();
  1097. case AArch64::ADDSWrr:
  1098. return AArch64::ADDWrr;
  1099. case AArch64::ADDSWri:
  1100. return MIDefinesZeroReg ? AArch64::ADDSWri : AArch64::ADDWri;
  1101. case AArch64::ADDSWrs:
  1102. return MIDefinesZeroReg ? AArch64::ADDSWrs : AArch64::ADDWrs;
  1103. case AArch64::ADDSWrx:
  1104. return AArch64::ADDWrx;
  1105. case AArch64::ADDSXrr:
  1106. return AArch64::ADDXrr;
  1107. case AArch64::ADDSXri:
  1108. return MIDefinesZeroReg ? AArch64::ADDSXri : AArch64::ADDXri;
  1109. case AArch64::ADDSXrs:
  1110. return MIDefinesZeroReg ? AArch64::ADDSXrs : AArch64::ADDXrs;
  1111. case AArch64::ADDSXrx:
  1112. return AArch64::ADDXrx;
  1113. case AArch64::SUBSWrr:
  1114. return AArch64::SUBWrr;
  1115. case AArch64::SUBSWri:
  1116. return MIDefinesZeroReg ? AArch64::SUBSWri : AArch64::SUBWri;
  1117. case AArch64::SUBSWrs:
  1118. return MIDefinesZeroReg ? AArch64::SUBSWrs : AArch64::SUBWrs;
  1119. case AArch64::SUBSWrx:
  1120. return AArch64::SUBWrx;
  1121. case AArch64::SUBSXrr:
  1122. return AArch64::SUBXrr;
  1123. case AArch64::SUBSXri:
  1124. return MIDefinesZeroReg ? AArch64::SUBSXri : AArch64::SUBXri;
  1125. case AArch64::SUBSXrs:
  1126. return MIDefinesZeroReg ? AArch64::SUBSXrs : AArch64::SUBXrs;
  1127. case AArch64::SUBSXrx:
  1128. return AArch64::SUBXrx;
  1129. }
  1130. }
  1131. enum AccessKind { AK_Write = 0x01, AK_Read = 0x10, AK_All = 0x11 };
  1132. /// True when condition flags are accessed (either by writing or reading)
  1133. /// on the instruction trace starting at From and ending at To.
  1134. ///
  1135. /// Note: If From and To are from different blocks it's assumed CC are accessed
  1136. /// on the path.
  1137. static bool areCFlagsAccessedBetweenInstrs(
  1138. MachineBasicBlock::iterator From, MachineBasicBlock::iterator To,
  1139. const TargetRegisterInfo *TRI, const AccessKind AccessToCheck = AK_All) {
  1140. // Early exit if To is at the beginning of the BB.
  1141. if (To == To->getParent()->begin())
  1142. return true;
  1143. // Check whether the instructions are in the same basic block
  1144. // If not, assume the condition flags might get modified somewhere.
  1145. if (To->getParent() != From->getParent())
  1146. return true;
  1147. // From must be above To.
  1148. assert(std::any_of(
  1149. ++To.getReverse(), To->getParent()->rend(),
  1150. [From](MachineInstr &MI) { return MI.getIterator() == From; }));
  1151. // We iterate backward starting at \p To until we hit \p From.
  1152. for (const MachineInstr &Instr :
  1153. instructionsWithoutDebug(++To.getReverse(), From.getReverse())) {
  1154. if (((AccessToCheck & AK_Write) &&
  1155. Instr.modifiesRegister(AArch64::NZCV, TRI)) ||
  1156. ((AccessToCheck & AK_Read) && Instr.readsRegister(AArch64::NZCV, TRI)))
  1157. return true;
  1158. }
  1159. return false;
  1160. }
  1161. /// optimizePTestInstr - Attempt to remove a ptest of a predicate-generating
  1162. /// operation which could set the flags in an identical manner
  1163. bool AArch64InstrInfo::optimizePTestInstr(
  1164. MachineInstr *PTest, unsigned MaskReg, unsigned PredReg,
  1165. const MachineRegisterInfo *MRI) const {
  1166. auto *Mask = MRI->getUniqueVRegDef(MaskReg);
  1167. auto *Pred = MRI->getUniqueVRegDef(PredReg);
  1168. auto NewOp = Pred->getOpcode();
  1169. bool OpChanged = false;
  1170. unsigned MaskOpcode = Mask->getOpcode();
  1171. unsigned PredOpcode = Pred->getOpcode();
  1172. bool PredIsPTestLike = isPTestLikeOpcode(PredOpcode);
  1173. bool PredIsWhileLike = isWhileOpcode(PredOpcode);
  1174. if (isPTrueOpcode(MaskOpcode) && (PredIsPTestLike || PredIsWhileLike)) {
  1175. // For PTEST(PTRUE, OTHER_INST), PTEST is redundant when PTRUE doesn't
  1176. // deactivate any lanes OTHER_INST might set.
  1177. uint64_t MaskElementSize = getElementSizeForOpcode(MaskOpcode);
  1178. uint64_t PredElementSize = getElementSizeForOpcode(PredOpcode);
  1179. // Must be an all active predicate of matching element size.
  1180. if ((PredElementSize != MaskElementSize) ||
  1181. (Mask->getOperand(1).getImm() != 31))
  1182. return false;
  1183. // Fallthough to simply remove the PTEST.
  1184. } else if ((Mask == Pred) && (PredIsPTestLike || PredIsWhileLike)) {
  1185. // For PTEST(PG, PG), PTEST is redundant when PG is the result of an
  1186. // instruction that sets the flags as PTEST would.
  1187. // Fallthough to simply remove the PTEST.
  1188. } else if (PredIsPTestLike) {
  1189. // For PTEST(PG_1, PTEST_LIKE(PG2, ...)), PTEST is redundant when both
  1190. // instructions use the same predicate.
  1191. auto PTestLikeMask = MRI->getUniqueVRegDef(Pred->getOperand(1).getReg());
  1192. if (Mask != PTestLikeMask)
  1193. return false;
  1194. // Fallthough to simply remove the PTEST.
  1195. } else {
  1196. switch (Pred->getOpcode()) {
  1197. case AArch64::BRKB_PPzP:
  1198. case AArch64::BRKPB_PPzPP: {
  1199. // Op 0 is chain, 1 is the mask, 2 the previous predicate to
  1200. // propagate, 3 the new predicate.
  1201. // Check to see if our mask is the same as the brkpb's. If
  1202. // not the resulting flag bits may be different and we
  1203. // can't remove the ptest.
  1204. auto *PredMask = MRI->getUniqueVRegDef(Pred->getOperand(1).getReg());
  1205. if (Mask != PredMask)
  1206. return false;
  1207. // Switch to the new opcode
  1208. NewOp = Pred->getOpcode() == AArch64::BRKB_PPzP ? AArch64::BRKBS_PPzP
  1209. : AArch64::BRKPBS_PPzPP;
  1210. OpChanged = true;
  1211. break;
  1212. }
  1213. case AArch64::BRKN_PPzP: {
  1214. auto *PredMask = MRI->getUniqueVRegDef(Pred->getOperand(1).getReg());
  1215. if (Mask != PredMask)
  1216. return false;
  1217. NewOp = AArch64::BRKNS_PPzP;
  1218. OpChanged = true;
  1219. break;
  1220. }
  1221. case AArch64::RDFFR_PPz: {
  1222. // rdffr p1.b, PredMask=p0/z <--- Definition of Pred
  1223. // ptest Mask=p0, Pred=p1.b <--- If equal masks, remove this and use
  1224. // `rdffrs p1.b, p0/z` above.
  1225. auto *PredMask = MRI->getUniqueVRegDef(Pred->getOperand(1).getReg());
  1226. if (Mask != PredMask)
  1227. return false;
  1228. NewOp = AArch64::RDFFRS_PPz;
  1229. OpChanged = true;
  1230. break;
  1231. }
  1232. default:
  1233. // Bail out if we don't recognize the input
  1234. return false;
  1235. }
  1236. }
  1237. const TargetRegisterInfo *TRI = &getRegisterInfo();
  1238. // If another instruction between Pred and PTest accesses flags, don't remove
  1239. // the ptest or update the earlier instruction to modify them.
  1240. if (areCFlagsAccessedBetweenInstrs(Pred, PTest, TRI))
  1241. return false;
  1242. // If we pass all the checks, it's safe to remove the PTEST and use the flags
  1243. // as they are prior to PTEST. Sometimes this requires the tested PTEST
  1244. // operand to be replaced with an equivalent instruction that also sets the
  1245. // flags.
  1246. Pred->setDesc(get(NewOp));
  1247. PTest->eraseFromParent();
  1248. if (OpChanged) {
  1249. bool succeeded = UpdateOperandRegClass(*Pred);
  1250. (void)succeeded;
  1251. assert(succeeded && "Operands have incompatible register classes!");
  1252. Pred->addRegisterDefined(AArch64::NZCV, TRI);
  1253. }
  1254. // Ensure that the flags def is live.
  1255. if (Pred->registerDefIsDead(AArch64::NZCV, TRI)) {
  1256. unsigned i = 0, e = Pred->getNumOperands();
  1257. for (; i != e; ++i) {
  1258. MachineOperand &MO = Pred->getOperand(i);
  1259. if (MO.isReg() && MO.isDef() && MO.getReg() == AArch64::NZCV) {
  1260. MO.setIsDead(false);
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. return true;
  1266. }
  1267. /// Try to optimize a compare instruction. A compare instruction is an
  1268. /// instruction which produces AArch64::NZCV. It can be truly compare
  1269. /// instruction
  1270. /// when there are no uses of its destination register.
  1271. ///
  1272. /// The following steps are tried in order:
  1273. /// 1. Convert CmpInstr into an unconditional version.
  1274. /// 2. Remove CmpInstr if above there is an instruction producing a needed
  1275. /// condition code or an instruction which can be converted into such an
  1276. /// instruction.
  1277. /// Only comparison with zero is supported.
  1278. bool AArch64InstrInfo::optimizeCompareInstr(
  1279. MachineInstr &CmpInstr, Register SrcReg, Register SrcReg2, int64_t CmpMask,
  1280. int64_t CmpValue, const MachineRegisterInfo *MRI) const {
  1281. assert(CmpInstr.getParent());
  1282. assert(MRI);
  1283. // Replace SUBSWrr with SUBWrr if NZCV is not used.
  1284. int DeadNZCVIdx = CmpInstr.findRegisterDefOperandIdx(AArch64::NZCV, true);
  1285. if (DeadNZCVIdx != -1) {
  1286. if (CmpInstr.definesRegister(AArch64::WZR) ||
  1287. CmpInstr.definesRegister(AArch64::XZR)) {
  1288. CmpInstr.eraseFromParent();
  1289. return true;
  1290. }
  1291. unsigned Opc = CmpInstr.getOpcode();
  1292. unsigned NewOpc = convertToNonFlagSettingOpc(CmpInstr);
  1293. if (NewOpc == Opc)
  1294. return false;
  1295. const MCInstrDesc &MCID = get(NewOpc);
  1296. CmpInstr.setDesc(MCID);
  1297. CmpInstr.RemoveOperand(DeadNZCVIdx);
  1298. bool succeeded = UpdateOperandRegClass(CmpInstr);
  1299. (void)succeeded;
  1300. assert(succeeded && "Some operands reg class are incompatible!");
  1301. return true;
  1302. }
  1303. if (CmpInstr.getOpcode() == AArch64::PTEST_PP)
  1304. return optimizePTestInstr(&CmpInstr, SrcReg, SrcReg2, MRI);
  1305. if (SrcReg2 != 0)
  1306. return false;
  1307. // CmpInstr is a Compare instruction if destination register is not used.
  1308. if (!MRI->use_nodbg_empty(CmpInstr.getOperand(0).getReg()))
  1309. return false;
  1310. if (CmpValue == 0 && substituteCmpToZero(CmpInstr, SrcReg, *MRI))
  1311. return true;
  1312. return (CmpValue == 0 || CmpValue == 1) &&
  1313. removeCmpToZeroOrOne(CmpInstr, SrcReg, CmpValue, *MRI);
  1314. }
  1315. /// Get opcode of S version of Instr.
  1316. /// If Instr is S version its opcode is returned.
  1317. /// AArch64::INSTRUCTION_LIST_END is returned if Instr does not have S version
  1318. /// or we are not interested in it.
  1319. static unsigned sForm(MachineInstr &Instr) {
  1320. switch (Instr.getOpcode()) {
  1321. default:
  1322. return AArch64::INSTRUCTION_LIST_END;
  1323. case AArch64::ADDSWrr:
  1324. case AArch64::ADDSWri:
  1325. case AArch64::ADDSXrr:
  1326. case AArch64::ADDSXri:
  1327. case AArch64::SUBSWrr:
  1328. case AArch64::SUBSWri:
  1329. case AArch64::SUBSXrr:
  1330. case AArch64::SUBSXri:
  1331. return Instr.getOpcode();
  1332. case AArch64::ADDWrr:
  1333. return AArch64::ADDSWrr;
  1334. case AArch64::ADDWri:
  1335. return AArch64::ADDSWri;
  1336. case AArch64::ADDXrr:
  1337. return AArch64::ADDSXrr;
  1338. case AArch64::ADDXri:
  1339. return AArch64::ADDSXri;
  1340. case AArch64::ADCWr:
  1341. return AArch64::ADCSWr;
  1342. case AArch64::ADCXr:
  1343. return AArch64::ADCSXr;
  1344. case AArch64::SUBWrr:
  1345. return AArch64::SUBSWrr;
  1346. case AArch64::SUBWri:
  1347. return AArch64::SUBSWri;
  1348. case AArch64::SUBXrr:
  1349. return AArch64::SUBSXrr;
  1350. case AArch64::SUBXri:
  1351. return AArch64::SUBSXri;
  1352. case AArch64::SBCWr:
  1353. return AArch64::SBCSWr;
  1354. case AArch64::SBCXr:
  1355. return AArch64::SBCSXr;
  1356. case AArch64::ANDWri:
  1357. return AArch64::ANDSWri;
  1358. case AArch64::ANDXri:
  1359. return AArch64::ANDSXri;
  1360. }
  1361. }
  1362. /// Check if AArch64::NZCV should be alive in successors of MBB.
  1363. static bool areCFlagsAliveInSuccessors(const MachineBasicBlock *MBB) {
  1364. for (auto *BB : MBB->successors())
  1365. if (BB->isLiveIn(AArch64::NZCV))
  1366. return true;
  1367. return false;
  1368. }
  1369. /// \returns The condition code operand index for \p Instr if it is a branch
  1370. /// or select and -1 otherwise.
  1371. static int
  1372. findCondCodeUseOperandIdxForBranchOrSelect(const MachineInstr &Instr) {
  1373. switch (Instr.getOpcode()) {
  1374. default:
  1375. return -1;
  1376. case AArch64::Bcc: {
  1377. int Idx = Instr.findRegisterUseOperandIdx(AArch64::NZCV);
  1378. assert(Idx >= 2);
  1379. return Idx - 2;
  1380. }
  1381. case AArch64::CSINVWr:
  1382. case AArch64::CSINVXr:
  1383. case AArch64::CSINCWr:
  1384. case AArch64::CSINCXr:
  1385. case AArch64::CSELWr:
  1386. case AArch64::CSELXr:
  1387. case AArch64::CSNEGWr:
  1388. case AArch64::CSNEGXr:
  1389. case AArch64::FCSELSrrr:
  1390. case AArch64::FCSELDrrr: {
  1391. int Idx = Instr.findRegisterUseOperandIdx(AArch64::NZCV);
  1392. assert(Idx >= 1);
  1393. return Idx - 1;
  1394. }
  1395. }
  1396. }
  1397. namespace {
  1398. struct UsedNZCV {
  1399. bool N = false;
  1400. bool Z = false;
  1401. bool C = false;
  1402. bool V = false;
  1403. UsedNZCV() = default;
  1404. UsedNZCV &operator|=(const UsedNZCV &UsedFlags) {
  1405. this->N |= UsedFlags.N;
  1406. this->Z |= UsedFlags.Z;
  1407. this->C |= UsedFlags.C;
  1408. this->V |= UsedFlags.V;
  1409. return *this;
  1410. }
  1411. };
  1412. } // end anonymous namespace
  1413. /// Find a condition code used by the instruction.
  1414. /// Returns AArch64CC::Invalid if either the instruction does not use condition
  1415. /// codes or we don't optimize CmpInstr in the presence of such instructions.
  1416. static AArch64CC::CondCode findCondCodeUsedByInstr(const MachineInstr &Instr) {
  1417. int CCIdx = findCondCodeUseOperandIdxForBranchOrSelect(Instr);
  1418. return CCIdx >= 0 ? static_cast<AArch64CC::CondCode>(
  1419. Instr.getOperand(CCIdx).getImm())
  1420. : AArch64CC::Invalid;
  1421. }
  1422. static UsedNZCV getUsedNZCV(AArch64CC::CondCode CC) {
  1423. assert(CC != AArch64CC::Invalid);
  1424. UsedNZCV UsedFlags;
  1425. switch (CC) {
  1426. default:
  1427. break;
  1428. case AArch64CC::EQ: // Z set
  1429. case AArch64CC::NE: // Z clear
  1430. UsedFlags.Z = true;
  1431. break;
  1432. case AArch64CC::HI: // Z clear and C set
  1433. case AArch64CC::LS: // Z set or C clear
  1434. UsedFlags.Z = true;
  1435. LLVM_FALLTHROUGH;
  1436. case AArch64CC::HS: // C set
  1437. case AArch64CC::LO: // C clear
  1438. UsedFlags.C = true;
  1439. break;
  1440. case AArch64CC::MI: // N set
  1441. case AArch64CC::PL: // N clear
  1442. UsedFlags.N = true;
  1443. break;
  1444. case AArch64CC::VS: // V set
  1445. case AArch64CC::VC: // V clear
  1446. UsedFlags.V = true;
  1447. break;
  1448. case AArch64CC::GT: // Z clear, N and V the same
  1449. case AArch64CC::LE: // Z set, N and V differ
  1450. UsedFlags.Z = true;
  1451. LLVM_FALLTHROUGH;
  1452. case AArch64CC::GE: // N and V the same
  1453. case AArch64CC::LT: // N and V differ
  1454. UsedFlags.N = true;
  1455. UsedFlags.V = true;
  1456. break;
  1457. }
  1458. return UsedFlags;
  1459. }
  1460. /// \returns Conditions flags used after \p CmpInstr in its MachineBB if they
  1461. /// are not containing C or V flags and NZCV flags are not alive in successors
  1462. /// of the same \p CmpInstr and \p MI parent. \returns None otherwise.
  1463. ///
  1464. /// Collect instructions using that flags in \p CCUseInstrs if provided.
  1465. static Optional<UsedNZCV>
  1466. examineCFlagsUse(MachineInstr &MI, MachineInstr &CmpInstr,
  1467. const TargetRegisterInfo &TRI,
  1468. SmallVectorImpl<MachineInstr *> *CCUseInstrs = nullptr) {
  1469. MachineBasicBlock *CmpParent = CmpInstr.getParent();
  1470. if (MI.getParent() != CmpParent)
  1471. return None;
  1472. if (areCFlagsAliveInSuccessors(CmpParent))
  1473. return None;
  1474. UsedNZCV NZCVUsedAfterCmp;
  1475. for (MachineInstr &Instr : instructionsWithoutDebug(
  1476. std::next(CmpInstr.getIterator()), CmpParent->instr_end())) {
  1477. if (Instr.readsRegister(AArch64::NZCV, &TRI)) {
  1478. AArch64CC::CondCode CC = findCondCodeUsedByInstr(Instr);
  1479. if (CC == AArch64CC::Invalid) // Unsupported conditional instruction
  1480. return None;
  1481. NZCVUsedAfterCmp |= getUsedNZCV(CC);
  1482. if (CCUseInstrs)
  1483. CCUseInstrs->push_back(&Instr);
  1484. }
  1485. if (Instr.modifiesRegister(AArch64::NZCV, &TRI))
  1486. break;
  1487. }
  1488. if (NZCVUsedAfterCmp.C || NZCVUsedAfterCmp.V)
  1489. return None;
  1490. return NZCVUsedAfterCmp;
  1491. }
  1492. static bool isADDSRegImm(unsigned Opcode) {
  1493. return Opcode == AArch64::ADDSWri || Opcode == AArch64::ADDSXri;
  1494. }
  1495. static bool isSUBSRegImm(unsigned Opcode) {
  1496. return Opcode == AArch64::SUBSWri || Opcode == AArch64::SUBSXri;
  1497. }
  1498. /// Check if CmpInstr can be substituted by MI.
  1499. ///
  1500. /// CmpInstr can be substituted:
  1501. /// - CmpInstr is either 'ADDS %vreg, 0' or 'SUBS %vreg, 0'
  1502. /// - and, MI and CmpInstr are from the same MachineBB
  1503. /// - and, condition flags are not alive in successors of the CmpInstr parent
  1504. /// - and, if MI opcode is the S form there must be no defs of flags between
  1505. /// MI and CmpInstr
  1506. /// or if MI opcode is not the S form there must be neither defs of flags
  1507. /// nor uses of flags between MI and CmpInstr.
  1508. /// - and C/V flags are not used after CmpInstr
  1509. static bool canInstrSubstituteCmpInstr(MachineInstr &MI, MachineInstr &CmpInstr,
  1510. const TargetRegisterInfo &TRI) {
  1511. assert(sForm(MI) != AArch64::INSTRUCTION_LIST_END);
  1512. const unsigned CmpOpcode = CmpInstr.getOpcode();
  1513. if (!isADDSRegImm(CmpOpcode) && !isSUBSRegImm(CmpOpcode))
  1514. return false;
  1515. if (!examineCFlagsUse(MI, CmpInstr, TRI))
  1516. return false;
  1517. AccessKind AccessToCheck = AK_Write;
  1518. if (sForm(MI) != MI.getOpcode())
  1519. AccessToCheck = AK_All;
  1520. return !areCFlagsAccessedBetweenInstrs(&MI, &CmpInstr, &TRI, AccessToCheck);
  1521. }
  1522. /// Substitute an instruction comparing to zero with another instruction
  1523. /// which produces needed condition flags.
  1524. ///
  1525. /// Return true on success.
  1526. bool AArch64InstrInfo::substituteCmpToZero(
  1527. MachineInstr &CmpInstr, unsigned SrcReg,
  1528. const MachineRegisterInfo &MRI) const {
  1529. // Get the unique definition of SrcReg.
  1530. MachineInstr *MI = MRI.getUniqueVRegDef(SrcReg);
  1531. if (!MI)
  1532. return false;
  1533. const TargetRegisterInfo &TRI = getRegisterInfo();
  1534. unsigned NewOpc = sForm(*MI);
  1535. if (NewOpc == AArch64::INSTRUCTION_LIST_END)
  1536. return false;
  1537. if (!canInstrSubstituteCmpInstr(*MI, CmpInstr, TRI))
  1538. return false;
  1539. // Update the instruction to set NZCV.
  1540. MI->setDesc(get(NewOpc));
  1541. CmpInstr.eraseFromParent();
  1542. bool succeeded = UpdateOperandRegClass(*MI);
  1543. (void)succeeded;
  1544. assert(succeeded && "Some operands reg class are incompatible!");
  1545. MI->addRegisterDefined(AArch64::NZCV, &TRI);
  1546. return true;
  1547. }
  1548. /// \returns True if \p CmpInstr can be removed.
  1549. ///
  1550. /// \p IsInvertCC is true if, after removing \p CmpInstr, condition
  1551. /// codes used in \p CCUseInstrs must be inverted.
  1552. static bool canCmpInstrBeRemoved(MachineInstr &MI, MachineInstr &CmpInstr,
  1553. int CmpValue, const TargetRegisterInfo &TRI,
  1554. SmallVectorImpl<MachineInstr *> &CCUseInstrs,
  1555. bool &IsInvertCC) {
  1556. assert((CmpValue == 0 || CmpValue == 1) &&
  1557. "Only comparisons to 0 or 1 considered for removal!");
  1558. // MI is 'CSINCWr %vreg, wzr, wzr, <cc>' or 'CSINCXr %vreg, xzr, xzr, <cc>'
  1559. unsigned MIOpc = MI.getOpcode();
  1560. if (MIOpc == AArch64::CSINCWr) {
  1561. if (MI.getOperand(1).getReg() != AArch64::WZR ||
  1562. MI.getOperand(2).getReg() != AArch64::WZR)
  1563. return false;
  1564. } else if (MIOpc == AArch64::CSINCXr) {
  1565. if (MI.getOperand(1).getReg() != AArch64::XZR ||
  1566. MI.getOperand(2).getReg() != AArch64::XZR)
  1567. return false;
  1568. } else {
  1569. return false;
  1570. }
  1571. AArch64CC::CondCode MICC = findCondCodeUsedByInstr(MI);
  1572. if (MICC == AArch64CC::Invalid)
  1573. return false;
  1574. // NZCV needs to be defined
  1575. if (MI.findRegisterDefOperandIdx(AArch64::NZCV, true) != -1)
  1576. return false;
  1577. // CmpInstr is 'ADDS %vreg, 0' or 'SUBS %vreg, 0' or 'SUBS %vreg, 1'
  1578. const unsigned CmpOpcode = CmpInstr.getOpcode();
  1579. bool IsSubsRegImm = isSUBSRegImm(CmpOpcode);
  1580. if (CmpValue && !IsSubsRegImm)
  1581. return false;
  1582. if (!CmpValue && !IsSubsRegImm && !isADDSRegImm(CmpOpcode))
  1583. return false;
  1584. // MI conditions allowed: eq, ne, mi, pl
  1585. UsedNZCV MIUsedNZCV = getUsedNZCV(MICC);
  1586. if (MIUsedNZCV.C || MIUsedNZCV.V)
  1587. return false;
  1588. Optional<UsedNZCV> NZCVUsedAfterCmp =
  1589. examineCFlagsUse(MI, CmpInstr, TRI, &CCUseInstrs);
  1590. // Condition flags are not used in CmpInstr basic block successors and only
  1591. // Z or N flags allowed to be used after CmpInstr within its basic block
  1592. if (!NZCVUsedAfterCmp)
  1593. return false;
  1594. // Z or N flag used after CmpInstr must correspond to the flag used in MI
  1595. if ((MIUsedNZCV.Z && NZCVUsedAfterCmp->N) ||
  1596. (MIUsedNZCV.N && NZCVUsedAfterCmp->Z))
  1597. return false;
  1598. // If CmpInstr is comparison to zero MI conditions are limited to eq, ne
  1599. if (MIUsedNZCV.N && !CmpValue)
  1600. return false;
  1601. // There must be no defs of flags between MI and CmpInstr
  1602. if (areCFlagsAccessedBetweenInstrs(&MI, &CmpInstr, &TRI, AK_Write))
  1603. return false;
  1604. // Condition code is inverted in the following cases:
  1605. // 1. MI condition is ne; CmpInstr is 'ADDS %vreg, 0' or 'SUBS %vreg, 0'
  1606. // 2. MI condition is eq, pl; CmpInstr is 'SUBS %vreg, 1'
  1607. IsInvertCC = (CmpValue && (MICC == AArch64CC::EQ || MICC == AArch64CC::PL)) ||
  1608. (!CmpValue && MICC == AArch64CC::NE);
  1609. return true;
  1610. }
  1611. /// Remove comparision in csinc-cmp sequence
  1612. ///
  1613. /// Examples:
  1614. /// 1. \code
  1615. /// csinc w9, wzr, wzr, ne
  1616. /// cmp w9, #0
  1617. /// b.eq
  1618. /// \endcode
  1619. /// to
  1620. /// \code
  1621. /// csinc w9, wzr, wzr, ne
  1622. /// b.ne
  1623. /// \endcode
  1624. ///
  1625. /// 2. \code
  1626. /// csinc x2, xzr, xzr, mi
  1627. /// cmp x2, #1
  1628. /// b.pl
  1629. /// \endcode
  1630. /// to
  1631. /// \code
  1632. /// csinc x2, xzr, xzr, mi
  1633. /// b.pl
  1634. /// \endcode
  1635. ///
  1636. /// \param CmpInstr comparison instruction
  1637. /// \return True when comparison removed
  1638. bool AArch64InstrInfo::removeCmpToZeroOrOne(
  1639. MachineInstr &CmpInstr, unsigned SrcReg, int CmpValue,
  1640. const MachineRegisterInfo &MRI) const {
  1641. MachineInstr *MI = MRI.getUniqueVRegDef(SrcReg);
  1642. if (!MI)
  1643. return false;
  1644. const TargetRegisterInfo &TRI = getRegisterInfo();
  1645. SmallVector<MachineInstr *, 4> CCUseInstrs;
  1646. bool IsInvertCC = false;
  1647. if (!canCmpInstrBeRemoved(*MI, CmpInstr, CmpValue, TRI, CCUseInstrs,
  1648. IsInvertCC))
  1649. return false;
  1650. // Make transformation
  1651. CmpInstr.eraseFromParent();
  1652. if (IsInvertCC) {
  1653. // Invert condition codes in CmpInstr CC users
  1654. for (MachineInstr *CCUseInstr : CCUseInstrs) {
  1655. int Idx = findCondCodeUseOperandIdxForBranchOrSelect(*CCUseInstr);
  1656. assert(Idx >= 0 && "Unexpected instruction using CC.");
  1657. MachineOperand &CCOperand = CCUseInstr->getOperand(Idx);
  1658. AArch64CC::CondCode CCUse = AArch64CC::getInvertedCondCode(
  1659. static_cast<AArch64CC::CondCode>(CCOperand.getImm()));
  1660. CCOperand.setImm(CCUse);
  1661. }
  1662. }
  1663. return true;
  1664. }
  1665. bool AArch64InstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
  1666. if (MI.getOpcode() != TargetOpcode::LOAD_STACK_GUARD &&
  1667. MI.getOpcode() != AArch64::CATCHRET)
  1668. return false;
  1669. MachineBasicBlock &MBB = *MI.getParent();
  1670. auto &Subtarget = MBB.getParent()->getSubtarget<AArch64Subtarget>();
  1671. auto TRI = Subtarget.getRegisterInfo();
  1672. DebugLoc DL = MI.getDebugLoc();
  1673. if (MI.getOpcode() == AArch64::CATCHRET) {
  1674. // Skip to the first instruction before the epilog.
  1675. const TargetInstrInfo *TII =
  1676. MBB.getParent()->getSubtarget().getInstrInfo();
  1677. MachineBasicBlock *TargetMBB = MI.getOperand(0).getMBB();
  1678. auto MBBI = MachineBasicBlock::iterator(MI);
  1679. MachineBasicBlock::iterator FirstEpilogSEH = std::prev(MBBI);
  1680. while (FirstEpilogSEH->getFlag(MachineInstr::FrameDestroy) &&
  1681. FirstEpilogSEH != MBB.begin())
  1682. FirstEpilogSEH = std::prev(FirstEpilogSEH);
  1683. if (FirstEpilogSEH != MBB.begin())
  1684. FirstEpilogSEH = std::next(FirstEpilogSEH);
  1685. BuildMI(MBB, FirstEpilogSEH, DL, TII->get(AArch64::ADRP))
  1686. .addReg(AArch64::X0, RegState::Define)
  1687. .addMBB(TargetMBB);
  1688. BuildMI(MBB, FirstEpilogSEH, DL, TII->get(AArch64::ADDXri))
  1689. .addReg(AArch64::X0, RegState::Define)
  1690. .addReg(AArch64::X0)
  1691. .addMBB(TargetMBB)
  1692. .addImm(0);
  1693. return true;
  1694. }
  1695. Register Reg = MI.getOperand(0).getReg();
  1696. Module &M = *MBB.getParent()->getFunction().getParent();
  1697. if (M.getStackProtectorGuard() == "sysreg") {
  1698. const AArch64SysReg::SysReg *SrcReg =
  1699. AArch64SysReg::lookupSysRegByName(M.getStackProtectorGuardReg());
  1700. if (!SrcReg)
  1701. report_fatal_error("Unknown SysReg for Stack Protector Guard Register");
  1702. // mrs xN, sysreg
  1703. BuildMI(MBB, MI, DL, get(AArch64::MRS))
  1704. .addDef(Reg, RegState::Renamable)
  1705. .addImm(SrcReg->Encoding);
  1706. int Offset = M.getStackProtectorGuardOffset();
  1707. if (Offset >= 0 && Offset <= 32760 && Offset % 8 == 0) {
  1708. // ldr xN, [xN, #offset]
  1709. BuildMI(MBB, MI, DL, get(AArch64::LDRXui))
  1710. .addDef(Reg)
  1711. .addUse(Reg, RegState::Kill)
  1712. .addImm(Offset / 8);
  1713. } else if (Offset >= -256 && Offset <= 255) {
  1714. // ldur xN, [xN, #offset]
  1715. BuildMI(MBB, MI, DL, get(AArch64::LDURXi))
  1716. .addDef(Reg)
  1717. .addUse(Reg, RegState::Kill)
  1718. .addImm(Offset);
  1719. } else if (Offset >= -4095 && Offset <= 4095) {
  1720. if (Offset > 0) {
  1721. // add xN, xN, #offset
  1722. BuildMI(MBB, MI, DL, get(AArch64::ADDXri))
  1723. .addDef(Reg)
  1724. .addUse(Reg, RegState::Kill)
  1725. .addImm(Offset)
  1726. .addImm(0);
  1727. } else {
  1728. // sub xN, xN, #offset
  1729. BuildMI(MBB, MI, DL, get(AArch64::SUBXri))
  1730. .addDef(Reg)
  1731. .addUse(Reg, RegState::Kill)
  1732. .addImm(-Offset)
  1733. .addImm(0);
  1734. }
  1735. // ldr xN, [xN]
  1736. BuildMI(MBB, MI, DL, get(AArch64::LDRXui))
  1737. .addDef(Reg)
  1738. .addUse(Reg, RegState::Kill)
  1739. .addImm(0);
  1740. } else {
  1741. // Cases that are larger than +/- 4095 and not a multiple of 8, or larger
  1742. // than 23760.
  1743. // It might be nice to use AArch64::MOVi32imm here, which would get
  1744. // expanded in PreSched2 after PostRA, but our lone scratch Reg already
  1745. // contains the MRS result. findScratchNonCalleeSaveRegister() in
  1746. // AArch64FrameLowering might help us find such a scratch register
  1747. // though. If we failed to find a scratch register, we could emit a
  1748. // stream of add instructions to build up the immediate. Or, we could try
  1749. // to insert a AArch64::MOVi32imm before register allocation so that we
  1750. // didn't need to scavenge for a scratch register.
  1751. report_fatal_error("Unable to encode Stack Protector Guard Offset");
  1752. }
  1753. MBB.erase(MI);
  1754. return true;
  1755. }
  1756. const GlobalValue *GV =
  1757. cast<GlobalValue>((*MI.memoperands_begin())->getValue());
  1758. const TargetMachine &TM = MBB.getParent()->getTarget();
  1759. unsigned OpFlags = Subtarget.ClassifyGlobalReference(GV, TM);
  1760. const unsigned char MO_NC = AArch64II::MO_NC;
  1761. if ((OpFlags & AArch64II::MO_GOT) != 0) {
  1762. BuildMI(MBB, MI, DL, get(AArch64::LOADgot), Reg)
  1763. .addGlobalAddress(GV, 0, OpFlags);
  1764. if (Subtarget.isTargetILP32()) {
  1765. unsigned Reg32 = TRI->getSubReg(Reg, AArch64::sub_32);
  1766. BuildMI(MBB, MI, DL, get(AArch64::LDRWui))
  1767. .addDef(Reg32, RegState::Dead)
  1768. .addUse(Reg, RegState::Kill)
  1769. .addImm(0)
  1770. .addMemOperand(*MI.memoperands_begin())
  1771. .addDef(Reg, RegState::Implicit);
  1772. } else {
  1773. BuildMI(MBB, MI, DL, get(AArch64::LDRXui), Reg)
  1774. .addReg(Reg, RegState::Kill)
  1775. .addImm(0)
  1776. .addMemOperand(*MI.memoperands_begin());
  1777. }
  1778. } else if (TM.getCodeModel() == CodeModel::Large) {
  1779. assert(!Subtarget.isTargetILP32() && "how can large exist in ILP32?");
  1780. BuildMI(MBB, MI, DL, get(AArch64::MOVZXi), Reg)
  1781. .addGlobalAddress(GV, 0, AArch64II::MO_G0 | MO_NC)
  1782. .addImm(0);
  1783. BuildMI(MBB, MI, DL, get(AArch64::MOVKXi), Reg)
  1784. .addReg(Reg, RegState::Kill)
  1785. .addGlobalAddress(GV, 0, AArch64II::MO_G1 | MO_NC)
  1786. .addImm(16);
  1787. BuildMI(MBB, MI, DL, get(AArch64::MOVKXi), Reg)
  1788. .addReg(Reg, RegState::Kill)
  1789. .addGlobalAddress(GV, 0, AArch64II::MO_G2 | MO_NC)
  1790. .addImm(32);
  1791. BuildMI(MBB, MI, DL, get(AArch64::MOVKXi), Reg)
  1792. .addReg(Reg, RegState::Kill)
  1793. .addGlobalAddress(GV, 0, AArch64II::MO_G3)
  1794. .addImm(48);
  1795. BuildMI(MBB, MI, DL, get(AArch64::LDRXui), Reg)
  1796. .addReg(Reg, RegState::Kill)
  1797. .addImm(0)
  1798. .addMemOperand(*MI.memoperands_begin());
  1799. } else if (TM.getCodeModel() == CodeModel::Tiny) {
  1800. BuildMI(MBB, MI, DL, get(AArch64::ADR), Reg)
  1801. .addGlobalAddress(GV, 0, OpFlags);
  1802. } else {
  1803. BuildMI(MBB, MI, DL, get(AArch64::ADRP), Reg)
  1804. .addGlobalAddress(GV, 0, OpFlags | AArch64II::MO_PAGE);
  1805. unsigned char LoFlags = OpFlags | AArch64II::MO_PAGEOFF | MO_NC;
  1806. if (Subtarget.isTargetILP32()) {
  1807. unsigned Reg32 = TRI->getSubReg(Reg, AArch64::sub_32);
  1808. BuildMI(MBB, MI, DL, get(AArch64::LDRWui))
  1809. .addDef(Reg32, RegState::Dead)
  1810. .addUse(Reg, RegState::Kill)
  1811. .addGlobalAddress(GV, 0, LoFlags)
  1812. .addMemOperand(*MI.memoperands_begin())
  1813. .addDef(Reg, RegState::Implicit);
  1814. } else {
  1815. BuildMI(MBB, MI, DL, get(AArch64::LDRXui), Reg)
  1816. .addReg(Reg, RegState::Kill)
  1817. .addGlobalAddress(GV, 0, LoFlags)
  1818. .addMemOperand(*MI.memoperands_begin());
  1819. }
  1820. }
  1821. MBB.erase(MI);
  1822. return true;
  1823. }
  1824. // Return true if this instruction simply sets its single destination register
  1825. // to zero. This is equivalent to a register rename of the zero-register.
  1826. bool AArch64InstrInfo::isGPRZero(const MachineInstr &MI) {
  1827. switch (MI.getOpcode()) {
  1828. default:
  1829. break;
  1830. case AArch64::MOVZWi:
  1831. case AArch64::MOVZXi: // movz Rd, #0 (LSL #0)
  1832. if (MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) {
  1833. assert(MI.getDesc().getNumOperands() == 3 &&
  1834. MI.getOperand(2).getImm() == 0 && "invalid MOVZi operands");
  1835. return true;
  1836. }
  1837. break;
  1838. case AArch64::ANDWri: // and Rd, Rzr, #imm
  1839. return MI.getOperand(1).getReg() == AArch64::WZR;
  1840. case AArch64::ANDXri:
  1841. return MI.getOperand(1).getReg() == AArch64::XZR;
  1842. case TargetOpcode::COPY:
  1843. return MI.getOperand(1).getReg() == AArch64::WZR;
  1844. }
  1845. return false;
  1846. }
  1847. // Return true if this instruction simply renames a general register without
  1848. // modifying bits.
  1849. bool AArch64InstrInfo::isGPRCopy(const MachineInstr &MI) {
  1850. switch (MI.getOpcode()) {
  1851. default:
  1852. break;
  1853. case TargetOpcode::COPY: {
  1854. // GPR32 copies will by lowered to ORRXrs
  1855. Register DstReg = MI.getOperand(0).getReg();
  1856. return (AArch64::GPR32RegClass.contains(DstReg) ||
  1857. AArch64::GPR64RegClass.contains(DstReg));
  1858. }
  1859. case AArch64::ORRXrs: // orr Xd, Xzr, Xm (LSL #0)
  1860. if (MI.getOperand(1).getReg() == AArch64::XZR) {
  1861. assert(MI.getDesc().getNumOperands() == 4 &&
  1862. MI.getOperand(3).getImm() == 0 && "invalid ORRrs operands");
  1863. return true;
  1864. }
  1865. break;
  1866. case AArch64::ADDXri: // add Xd, Xn, #0 (LSL #0)
  1867. if (MI.getOperand(2).getImm() == 0) {
  1868. assert(MI.getDesc().getNumOperands() == 4 &&
  1869. MI.getOperand(3).getImm() == 0 && "invalid ADDXri operands");
  1870. return true;
  1871. }
  1872. break;
  1873. }
  1874. return false;
  1875. }
  1876. // Return true if this instruction simply renames a general register without
  1877. // modifying bits.
  1878. bool AArch64InstrInfo::isFPRCopy(const MachineInstr &MI) {
  1879. switch (MI.getOpcode()) {
  1880. default:
  1881. break;
  1882. case TargetOpcode::COPY: {
  1883. Register DstReg = MI.getOperand(0).getReg();
  1884. return AArch64::FPR128RegClass.contains(DstReg);
  1885. }
  1886. case AArch64::ORRv16i8:
  1887. if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
  1888. assert(MI.getDesc().getNumOperands() == 3 && MI.getOperand(0).isReg() &&
  1889. "invalid ORRv16i8 operands");
  1890. return true;
  1891. }
  1892. break;
  1893. }
  1894. return false;
  1895. }
  1896. unsigned AArch64InstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
  1897. int &FrameIndex) const {
  1898. switch (MI.getOpcode()) {
  1899. default:
  1900. break;
  1901. case AArch64::LDRWui:
  1902. case AArch64::LDRXui:
  1903. case AArch64::LDRBui:
  1904. case AArch64::LDRHui:
  1905. case AArch64::LDRSui:
  1906. case AArch64::LDRDui:
  1907. case AArch64::LDRQui:
  1908. if (MI.getOperand(0).getSubReg() == 0 && MI.getOperand(1).isFI() &&
  1909. MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) {
  1910. FrameIndex = MI.getOperand(1).getIndex();
  1911. return MI.getOperand(0).getReg();
  1912. }
  1913. break;
  1914. }
  1915. return 0;
  1916. }
  1917. unsigned AArch64InstrInfo::isStoreToStackSlot(const MachineInstr &MI,
  1918. int &FrameIndex) const {
  1919. switch (MI.getOpcode()) {
  1920. default:
  1921. break;
  1922. case AArch64::STRWui:
  1923. case AArch64::STRXui:
  1924. case AArch64::STRBui:
  1925. case AArch64::STRHui:
  1926. case AArch64::STRSui:
  1927. case AArch64::STRDui:
  1928. case AArch64::STRQui:
  1929. case AArch64::LDR_PXI:
  1930. case AArch64::STR_PXI:
  1931. if (MI.getOperand(0).getSubReg() == 0 && MI.getOperand(1).isFI() &&
  1932. MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) {
  1933. FrameIndex = MI.getOperand(1).getIndex();
  1934. return MI.getOperand(0).getReg();
  1935. }
  1936. break;
  1937. }
  1938. return 0;
  1939. }
  1940. /// Check all MachineMemOperands for a hint to suppress pairing.
  1941. bool AArch64InstrInfo::isLdStPairSuppressed(const MachineInstr &MI) {
  1942. return llvm::any_of(MI.memoperands(), [](MachineMemOperand *MMO) {
  1943. return MMO->getFlags() & MOSuppressPair;
  1944. });
  1945. }
  1946. /// Set a flag on the first MachineMemOperand to suppress pairing.
  1947. void AArch64InstrInfo::suppressLdStPair(MachineInstr &MI) {
  1948. if (MI.memoperands_empty())
  1949. return;
  1950. (*MI.memoperands_begin())->setFlags(MOSuppressPair);
  1951. }
  1952. /// Check all MachineMemOperands for a hint that the load/store is strided.
  1953. bool AArch64InstrInfo::isStridedAccess(const MachineInstr &MI) {
  1954. return llvm::any_of(MI.memoperands(), [](MachineMemOperand *MMO) {
  1955. return MMO->getFlags() & MOStridedAccess;
  1956. });
  1957. }
  1958. bool AArch64InstrInfo::hasUnscaledLdStOffset(unsigned Opc) {
  1959. switch (Opc) {
  1960. default:
  1961. return false;
  1962. case AArch64::STURSi:
  1963. case AArch64::STRSpre:
  1964. case AArch64::STURDi:
  1965. case AArch64::STRDpre:
  1966. case AArch64::STURQi:
  1967. case AArch64::STRQpre:
  1968. case AArch64::STURBBi:
  1969. case AArch64::STURHHi:
  1970. case AArch64::STURWi:
  1971. case AArch64::STRWpre:
  1972. case AArch64::STURXi:
  1973. case AArch64::STRXpre:
  1974. case AArch64::LDURSi:
  1975. case AArch64::LDRSpre:
  1976. case AArch64::LDURDi:
  1977. case AArch64::LDRDpre:
  1978. case AArch64::LDURQi:
  1979. case AArch64::LDRQpre:
  1980. case AArch64::LDURWi:
  1981. case AArch64::LDRWpre:
  1982. case AArch64::LDURXi:
  1983. case AArch64::LDRXpre:
  1984. case AArch64::LDURSWi:
  1985. case AArch64::LDURHHi:
  1986. case AArch64::LDURBBi:
  1987. case AArch64::LDURSBWi:
  1988. case AArch64::LDURSHWi:
  1989. return true;
  1990. }
  1991. }
  1992. Optional<unsigned> AArch64InstrInfo::getUnscaledLdSt(unsigned Opc) {
  1993. switch (Opc) {
  1994. default: return {};
  1995. case AArch64::PRFMui: return AArch64::PRFUMi;
  1996. case AArch64::LDRXui: return AArch64::LDURXi;
  1997. case AArch64::LDRWui: return AArch64::LDURWi;
  1998. case AArch64::LDRBui: return AArch64::LDURBi;
  1999. case AArch64::LDRHui: return AArch64::LDURHi;
  2000. case AArch64::LDRSui: return AArch64::LDURSi;
  2001. case AArch64::LDRDui: return AArch64::LDURDi;
  2002. case AArch64::LDRQui: return AArch64::LDURQi;
  2003. case AArch64::LDRBBui: return AArch64::LDURBBi;
  2004. case AArch64::LDRHHui: return AArch64::LDURHHi;
  2005. case AArch64::LDRSBXui: return AArch64::LDURSBXi;
  2006. case AArch64::LDRSBWui: return AArch64::LDURSBWi;
  2007. case AArch64::LDRSHXui: return AArch64::LDURSHXi;
  2008. case AArch64::LDRSHWui: return AArch64::LDURSHWi;
  2009. case AArch64::LDRSWui: return AArch64::LDURSWi;
  2010. case AArch64::STRXui: return AArch64::STURXi;
  2011. case AArch64::STRWui: return AArch64::STURWi;
  2012. case AArch64::STRBui: return AArch64::STURBi;
  2013. case AArch64::STRHui: return AArch64::STURHi;
  2014. case AArch64::STRSui: return AArch64::STURSi;
  2015. case AArch64::STRDui: return AArch64::STURDi;
  2016. case AArch64::STRQui: return AArch64::STURQi;
  2017. case AArch64::STRBBui: return AArch64::STURBBi;
  2018. case AArch64::STRHHui: return AArch64::STURHHi;
  2019. }
  2020. }
  2021. unsigned AArch64InstrInfo::getLoadStoreImmIdx(unsigned Opc) {
  2022. switch (Opc) {
  2023. default:
  2024. return 2;
  2025. case AArch64::LDPXi:
  2026. case AArch64::LDPDi:
  2027. case AArch64::STPXi:
  2028. case AArch64::STPDi:
  2029. case AArch64::LDNPXi:
  2030. case AArch64::LDNPDi:
  2031. case AArch64::STNPXi:
  2032. case AArch64::STNPDi:
  2033. case AArch64::LDPQi:
  2034. case AArch64::STPQi:
  2035. case AArch64::LDNPQi:
  2036. case AArch64::STNPQi:
  2037. case AArch64::LDPWi:
  2038. case AArch64::LDPSi:
  2039. case AArch64::STPWi:
  2040. case AArch64::STPSi:
  2041. case AArch64::LDNPWi:
  2042. case AArch64::LDNPSi:
  2043. case AArch64::STNPWi:
  2044. case AArch64::STNPSi:
  2045. case AArch64::LDG:
  2046. case AArch64::STGPi:
  2047. case AArch64::LD1B_IMM:
  2048. case AArch64::LD1B_H_IMM:
  2049. case AArch64::LD1B_S_IMM:
  2050. case AArch64::LD1B_D_IMM:
  2051. case AArch64::LD1SB_H_IMM:
  2052. case AArch64::LD1SB_S_IMM:
  2053. case AArch64::LD1SB_D_IMM:
  2054. case AArch64::LD1H_IMM:
  2055. case AArch64::LD1H_S_IMM:
  2056. case AArch64::LD1H_D_IMM:
  2057. case AArch64::LD1SH_S_IMM:
  2058. case AArch64::LD1SH_D_IMM:
  2059. case AArch64::LD1W_IMM:
  2060. case AArch64::LD1W_D_IMM:
  2061. case AArch64::LD1SW_D_IMM:
  2062. case AArch64::LD1D_IMM:
  2063. case AArch64::LD2B_IMM:
  2064. case AArch64::LD2H_IMM:
  2065. case AArch64::LD2W_IMM:
  2066. case AArch64::LD2D_IMM:
  2067. case AArch64::LD3B_IMM:
  2068. case AArch64::LD3H_IMM:
  2069. case AArch64::LD3W_IMM:
  2070. case AArch64::LD3D_IMM:
  2071. case AArch64::LD4B_IMM:
  2072. case AArch64::LD4H_IMM:
  2073. case AArch64::LD4W_IMM:
  2074. case AArch64::LD4D_IMM:
  2075. case AArch64::ST1B_IMM:
  2076. case AArch64::ST1B_H_IMM:
  2077. case AArch64::ST1B_S_IMM:
  2078. case AArch64::ST1B_D_IMM:
  2079. case AArch64::ST1H_IMM:
  2080. case AArch64::ST1H_S_IMM:
  2081. case AArch64::ST1H_D_IMM:
  2082. case AArch64::ST1W_IMM:
  2083. case AArch64::ST1W_D_IMM:
  2084. case AArch64::ST1D_IMM:
  2085. case AArch64::ST2B_IMM:
  2086. case AArch64::ST2H_IMM:
  2087. case AArch64::ST2W_IMM:
  2088. case AArch64::ST2D_IMM:
  2089. case AArch64::ST3B_IMM:
  2090. case AArch64::ST3H_IMM:
  2091. case AArch64::ST3W_IMM:
  2092. case AArch64::ST3D_IMM:
  2093. case AArch64::ST4B_IMM:
  2094. case AArch64::ST4H_IMM:
  2095. case AArch64::ST4W_IMM:
  2096. case AArch64::ST4D_IMM:
  2097. case AArch64::LD1RB_IMM:
  2098. case AArch64::LD1RB_H_IMM:
  2099. case AArch64::LD1RB_S_IMM:
  2100. case AArch64::LD1RB_D_IMM:
  2101. case AArch64::LD1RSB_H_IMM:
  2102. case AArch64::LD1RSB_S_IMM:
  2103. case AArch64::LD1RSB_D_IMM:
  2104. case AArch64::LD1RH_IMM:
  2105. case AArch64::LD1RH_S_IMM:
  2106. case AArch64::LD1RH_D_IMM:
  2107. case AArch64::LD1RSH_S_IMM:
  2108. case AArch64::LD1RSH_D_IMM:
  2109. case AArch64::LD1RW_IMM:
  2110. case AArch64::LD1RW_D_IMM:
  2111. case AArch64::LD1RSW_IMM:
  2112. case AArch64::LD1RD_IMM:
  2113. case AArch64::LDNT1B_ZRI:
  2114. case AArch64::LDNT1H_ZRI:
  2115. case AArch64::LDNT1W_ZRI:
  2116. case AArch64::LDNT1D_ZRI:
  2117. case AArch64::STNT1B_ZRI:
  2118. case AArch64::STNT1H_ZRI:
  2119. case AArch64::STNT1W_ZRI:
  2120. case AArch64::STNT1D_ZRI:
  2121. case AArch64::LDNF1B_IMM:
  2122. case AArch64::LDNF1B_H_IMM:
  2123. case AArch64::LDNF1B_S_IMM:
  2124. case AArch64::LDNF1B_D_IMM:
  2125. case AArch64::LDNF1SB_H_IMM:
  2126. case AArch64::LDNF1SB_S_IMM:
  2127. case AArch64::LDNF1SB_D_IMM:
  2128. case AArch64::LDNF1H_IMM:
  2129. case AArch64::LDNF1H_S_IMM:
  2130. case AArch64::LDNF1H_D_IMM:
  2131. case AArch64::LDNF1SH_S_IMM:
  2132. case AArch64::LDNF1SH_D_IMM:
  2133. case AArch64::LDNF1W_IMM:
  2134. case AArch64::LDNF1W_D_IMM:
  2135. case AArch64::LDNF1SW_D_IMM:
  2136. case AArch64::LDNF1D_IMM:
  2137. return 3;
  2138. case AArch64::ADDG:
  2139. case AArch64::STGOffset:
  2140. case AArch64::LDR_PXI:
  2141. case AArch64::STR_PXI:
  2142. return 2;
  2143. }
  2144. }
  2145. bool AArch64InstrInfo::isPairableLdStInst(const MachineInstr &MI) {
  2146. switch (MI.getOpcode()) {
  2147. default:
  2148. return false;
  2149. // Scaled instructions.
  2150. case AArch64::STRSui:
  2151. case AArch64::STRDui:
  2152. case AArch64::STRQui:
  2153. case AArch64::STRXui:
  2154. case AArch64::STRWui:
  2155. case AArch64::LDRSui:
  2156. case AArch64::LDRDui:
  2157. case AArch64::LDRQui:
  2158. case AArch64::LDRXui:
  2159. case AArch64::LDRWui:
  2160. case AArch64::LDRSWui:
  2161. // Unscaled instructions.
  2162. case AArch64::STURSi:
  2163. case AArch64::STRSpre:
  2164. case AArch64::STURDi:
  2165. case AArch64::STRDpre:
  2166. case AArch64::STURQi:
  2167. case AArch64::STRQpre:
  2168. case AArch64::STURWi:
  2169. case AArch64::STRWpre:
  2170. case AArch64::STURXi:
  2171. case AArch64::STRXpre:
  2172. case AArch64::LDURSi:
  2173. case AArch64::LDRSpre:
  2174. case AArch64::LDURDi:
  2175. case AArch64::LDRDpre:
  2176. case AArch64::LDURQi:
  2177. case AArch64::LDRQpre:
  2178. case AArch64::LDURWi:
  2179. case AArch64::LDRWpre:
  2180. case AArch64::LDURXi:
  2181. case AArch64::LDRXpre:
  2182. case AArch64::LDURSWi:
  2183. return true;
  2184. }
  2185. }
  2186. unsigned AArch64InstrInfo::convertToFlagSettingOpc(unsigned Opc,
  2187. bool &Is64Bit) {
  2188. switch (Opc) {
  2189. default:
  2190. llvm_unreachable("Opcode has no flag setting equivalent!");
  2191. // 32-bit cases:
  2192. case AArch64::ADDWri:
  2193. Is64Bit = false;
  2194. return AArch64::ADDSWri;
  2195. case AArch64::ADDWrr:
  2196. Is64Bit = false;
  2197. return AArch64::ADDSWrr;
  2198. case AArch64::ADDWrs:
  2199. Is64Bit = false;
  2200. return AArch64::ADDSWrs;
  2201. case AArch64::ADDWrx:
  2202. Is64Bit = false;
  2203. return AArch64::ADDSWrx;
  2204. case AArch64::ANDWri:
  2205. Is64Bit = false;
  2206. return AArch64::ANDSWri;
  2207. case AArch64::ANDWrr:
  2208. Is64Bit = false;
  2209. return AArch64::ANDSWrr;
  2210. case AArch64::ANDWrs:
  2211. Is64Bit = false;
  2212. return AArch64::ANDSWrs;
  2213. case AArch64::BICWrr:
  2214. Is64Bit = false;
  2215. return AArch64::BICSWrr;
  2216. case AArch64::BICWrs:
  2217. Is64Bit = false;
  2218. return AArch64::BICSWrs;
  2219. case AArch64::SUBWri:
  2220. Is64Bit = false;
  2221. return AArch64::SUBSWri;
  2222. case AArch64::SUBWrr:
  2223. Is64Bit = false;
  2224. return AArch64::SUBSWrr;
  2225. case AArch64::SUBWrs:
  2226. Is64Bit = false;
  2227. return AArch64::SUBSWrs;
  2228. case AArch64::SUBWrx:
  2229. Is64Bit = false;
  2230. return AArch64::SUBSWrx;
  2231. // 64-bit cases:
  2232. case AArch64::ADDXri:
  2233. Is64Bit = true;
  2234. return AArch64::ADDSXri;
  2235. case AArch64::ADDXrr:
  2236. Is64Bit = true;
  2237. return AArch64::ADDSXrr;
  2238. case AArch64::ADDXrs:
  2239. Is64Bit = true;
  2240. return AArch64::ADDSXrs;
  2241. case AArch64::ADDXrx:
  2242. Is64Bit = true;
  2243. return AArch64::ADDSXrx;
  2244. case AArch64::ANDXri:
  2245. Is64Bit = true;
  2246. return AArch64::ANDSXri;
  2247. case AArch64::ANDXrr:
  2248. Is64Bit = true;
  2249. return AArch64::ANDSXrr;
  2250. case AArch64::ANDXrs:
  2251. Is64Bit = true;
  2252. return AArch64::ANDSXrs;
  2253. case AArch64::BICXrr:
  2254. Is64Bit = true;
  2255. return AArch64::BICSXrr;
  2256. case AArch64::BICXrs:
  2257. Is64Bit = true;
  2258. return AArch64::BICSXrs;
  2259. case AArch64::SUBXri:
  2260. Is64Bit = true;
  2261. return AArch64::SUBSXri;
  2262. case AArch64::SUBXrr:
  2263. Is64Bit = true;
  2264. return AArch64::SUBSXrr;
  2265. case AArch64::SUBXrs:
  2266. Is64Bit = true;
  2267. return AArch64::SUBSXrs;
  2268. case AArch64::SUBXrx:
  2269. Is64Bit = true;
  2270. return AArch64::SUBSXrx;
  2271. }
  2272. }
  2273. // Is this a candidate for ld/st merging or pairing? For example, we don't
  2274. // touch volatiles or load/stores that have a hint to avoid pair formation.
  2275. bool AArch64InstrInfo::isCandidateToMergeOrPair(const MachineInstr &MI) const {
  2276. bool IsPreLdSt = isPreLdSt(MI);
  2277. // If this is a volatile load/store, don't mess with it.
  2278. if (MI.hasOrderedMemoryRef())
  2279. return false;
  2280. // Make sure this is a reg/fi+imm (as opposed to an address reloc).
  2281. // For Pre-inc LD/ST, the operand is shifted by one.
  2282. assert((MI.getOperand(IsPreLdSt ? 2 : 1).isReg() ||
  2283. MI.getOperand(IsPreLdSt ? 2 : 1).isFI()) &&
  2284. "Expected a reg or frame index operand.");
  2285. // For Pre-indexed addressing quadword instructions, the third operand is the
  2286. // immediate value.
  2287. bool IsImmPreLdSt = IsPreLdSt && MI.getOperand(3).isImm();
  2288. if (!MI.getOperand(2).isImm() && !IsImmPreLdSt)
  2289. return false;
  2290. // Can't merge/pair if the instruction modifies the base register.
  2291. // e.g., ldr x0, [x0]
  2292. // This case will never occur with an FI base.
  2293. // However, if the instruction is an LDR/STR<S,D,Q,W,X>pre, it can be merged.
  2294. // For example:
  2295. // ldr q0, [x11, #32]!
  2296. // ldr q1, [x11, #16]
  2297. // to
  2298. // ldp q0, q1, [x11, #32]!
  2299. if (MI.getOperand(1).isReg() && !IsPreLdSt) {
  2300. Register BaseReg = MI.getOperand(1).getReg();
  2301. const TargetRegisterInfo *TRI = &getRegisterInfo();
  2302. if (MI.modifiesRegister(BaseReg, TRI))
  2303. return false;
  2304. }
  2305. // Check if this load/store has a hint to avoid pair formation.
  2306. // MachineMemOperands hints are set by the AArch64StorePairSuppress pass.
  2307. if (isLdStPairSuppressed(MI))
  2308. return false;
  2309. // Do not pair any callee-save store/reload instructions in the
  2310. // prologue/epilogue if the CFI information encoded the operations as separate
  2311. // instructions, as that will cause the size of the actual prologue to mismatch
  2312. // with the prologue size recorded in the Windows CFI.
  2313. const MCAsmInfo *MAI = MI.getMF()->getTarget().getMCAsmInfo();
  2314. bool NeedsWinCFI = MAI->usesWindowsCFI() &&
  2315. MI.getMF()->getFunction().needsUnwindTableEntry();
  2316. if (NeedsWinCFI && (MI.getFlag(MachineInstr::FrameSetup) ||
  2317. MI.getFlag(MachineInstr::FrameDestroy)))
  2318. return false;
  2319. // On some CPUs quad load/store pairs are slower than two single load/stores.
  2320. if (Subtarget.isPaired128Slow()) {
  2321. switch (MI.getOpcode()) {
  2322. default:
  2323. break;
  2324. case AArch64::LDURQi:
  2325. case AArch64::STURQi:
  2326. case AArch64::LDRQui:
  2327. case AArch64::STRQui:
  2328. return false;
  2329. }
  2330. }
  2331. return true;
  2332. }
  2333. bool AArch64InstrInfo::getMemOperandsWithOffsetWidth(
  2334. const MachineInstr &LdSt, SmallVectorImpl<const MachineOperand *> &BaseOps,
  2335. int64_t &Offset, bool &OffsetIsScalable, unsigned &Width,
  2336. const TargetRegisterInfo *TRI) const {
  2337. if (!LdSt.mayLoadOrStore())
  2338. return false;
  2339. const MachineOperand *BaseOp;
  2340. if (!getMemOperandWithOffsetWidth(LdSt, BaseOp, Offset, OffsetIsScalable,
  2341. Width, TRI))
  2342. return false;
  2343. BaseOps.push_back(BaseOp);
  2344. return true;
  2345. }
  2346. Optional<ExtAddrMode>
  2347. AArch64InstrInfo::getAddrModeFromMemoryOp(const MachineInstr &MemI,
  2348. const TargetRegisterInfo *TRI) const {
  2349. const MachineOperand *Base; // Filled with the base operand of MI.
  2350. int64_t Offset; // Filled with the offset of MI.
  2351. bool OffsetIsScalable;
  2352. if (!getMemOperandWithOffset(MemI, Base, Offset, OffsetIsScalable, TRI))
  2353. return None;
  2354. if (!Base->isReg())
  2355. return None;
  2356. ExtAddrMode AM;
  2357. AM.BaseReg = Base->getReg();
  2358. AM.Displacement = Offset;
  2359. AM.ScaledReg = 0;
  2360. AM.Scale = 0;
  2361. return AM;
  2362. }
  2363. bool AArch64InstrInfo::getMemOperandWithOffsetWidth(
  2364. const MachineInstr &LdSt, const MachineOperand *&BaseOp, int64_t &Offset,
  2365. bool &OffsetIsScalable, unsigned &Width,
  2366. const TargetRegisterInfo *TRI) const {
  2367. assert(LdSt.mayLoadOrStore() && "Expected a memory operation.");
  2368. // Handle only loads/stores with base register followed by immediate offset.
  2369. if (LdSt.getNumExplicitOperands() == 3) {
  2370. // Non-paired instruction (e.g., ldr x1, [x0, #8]).
  2371. if ((!LdSt.getOperand(1).isReg() && !LdSt.getOperand(1).isFI()) ||
  2372. !LdSt.getOperand(2).isImm())
  2373. return false;
  2374. } else if (LdSt.getNumExplicitOperands() == 4) {
  2375. // Paired instruction (e.g., ldp x1, x2, [x0, #8]).
  2376. if (!LdSt.getOperand(1).isReg() ||
  2377. (!LdSt.getOperand(2).isReg() && !LdSt.getOperand(2).isFI()) ||
  2378. !LdSt.getOperand(3).isImm())
  2379. return false;
  2380. } else
  2381. return false;
  2382. // Get the scaling factor for the instruction and set the width for the
  2383. // instruction.
  2384. TypeSize Scale(0U, false);
  2385. int64_t Dummy1, Dummy2;
  2386. // If this returns false, then it's an instruction we don't want to handle.
  2387. if (!getMemOpInfo(LdSt.getOpcode(), Scale, Width, Dummy1, Dummy2))
  2388. return false;
  2389. // Compute the offset. Offset is calculated as the immediate operand
  2390. // multiplied by the scaling factor. Unscaled instructions have scaling factor
  2391. // set to 1.
  2392. if (LdSt.getNumExplicitOperands() == 3) {
  2393. BaseOp = &LdSt.getOperand(1);
  2394. Offset = LdSt.getOperand(2).getImm() * Scale.getKnownMinSize();
  2395. } else {
  2396. assert(LdSt.getNumExplicitOperands() == 4 && "invalid number of operands");
  2397. BaseOp = &LdSt.getOperand(2);
  2398. Offset = LdSt.getOperand(3).getImm() * Scale.getKnownMinSize();
  2399. }
  2400. OffsetIsScalable = Scale.isScalable();
  2401. if (!BaseOp->isReg() && !BaseOp->isFI())
  2402. return false;
  2403. return true;
  2404. }
  2405. MachineOperand &
  2406. AArch64InstrInfo::getMemOpBaseRegImmOfsOffsetOperand(MachineInstr &LdSt) const {
  2407. assert(LdSt.mayLoadOrStore() && "Expected a memory operation.");
  2408. MachineOperand &OfsOp = LdSt.getOperand(LdSt.getNumExplicitOperands() - 1);
  2409. assert(OfsOp.isImm() && "Offset operand wasn't immediate.");
  2410. return OfsOp;
  2411. }
  2412. bool AArch64InstrInfo::getMemOpInfo(unsigned Opcode, TypeSize &Scale,
  2413. unsigned &Width, int64_t &MinOffset,
  2414. int64_t &MaxOffset) {
  2415. const unsigned SVEMaxBytesPerVector = AArch64::SVEMaxBitsPerVector / 8;
  2416. switch (Opcode) {
  2417. // Not a memory operation or something we want to handle.
  2418. default:
  2419. Scale = TypeSize::Fixed(0);
  2420. Width = 0;
  2421. MinOffset = MaxOffset = 0;
  2422. return false;
  2423. case AArch64::STRWpost:
  2424. case AArch64::LDRWpost:
  2425. Width = 32;
  2426. Scale = TypeSize::Fixed(4);
  2427. MinOffset = -256;
  2428. MaxOffset = 255;
  2429. break;
  2430. case AArch64::LDURQi:
  2431. case AArch64::STURQi:
  2432. Width = 16;
  2433. Scale = TypeSize::Fixed(1);
  2434. MinOffset = -256;
  2435. MaxOffset = 255;
  2436. break;
  2437. case AArch64::PRFUMi:
  2438. case AArch64::LDURXi:
  2439. case AArch64::LDURDi:
  2440. case AArch64::STURXi:
  2441. case AArch64::STURDi:
  2442. Width = 8;
  2443. Scale = TypeSize::Fixed(1);
  2444. MinOffset = -256;
  2445. MaxOffset = 255;
  2446. break;
  2447. case AArch64::LDURWi:
  2448. case AArch64::LDURSi:
  2449. case AArch64::LDURSWi:
  2450. case AArch64::STURWi:
  2451. case AArch64::STURSi:
  2452. Width = 4;
  2453. Scale = TypeSize::Fixed(1);
  2454. MinOffset = -256;
  2455. MaxOffset = 255;
  2456. break;
  2457. case AArch64::LDURHi:
  2458. case AArch64::LDURHHi:
  2459. case AArch64::LDURSHXi:
  2460. case AArch64::LDURSHWi:
  2461. case AArch64::STURHi:
  2462. case AArch64::STURHHi:
  2463. Width = 2;
  2464. Scale = TypeSize::Fixed(1);
  2465. MinOffset = -256;
  2466. MaxOffset = 255;
  2467. break;
  2468. case AArch64::LDURBi:
  2469. case AArch64::LDURBBi:
  2470. case AArch64::LDURSBXi:
  2471. case AArch64::LDURSBWi:
  2472. case AArch64::STURBi:
  2473. case AArch64::STURBBi:
  2474. Width = 1;
  2475. Scale = TypeSize::Fixed(1);
  2476. MinOffset = -256;
  2477. MaxOffset = 255;
  2478. break;
  2479. case AArch64::LDPQi:
  2480. case AArch64::LDNPQi:
  2481. case AArch64::STPQi:
  2482. case AArch64::STNPQi:
  2483. Scale = TypeSize::Fixed(16);
  2484. Width = 32;
  2485. MinOffset = -64;
  2486. MaxOffset = 63;
  2487. break;
  2488. case AArch64::LDRQui:
  2489. case AArch64::STRQui:
  2490. Scale = TypeSize::Fixed(16);
  2491. Width = 16;
  2492. MinOffset = 0;
  2493. MaxOffset = 4095;
  2494. break;
  2495. case AArch64::LDPXi:
  2496. case AArch64::LDPDi:
  2497. case AArch64::LDNPXi:
  2498. case AArch64::LDNPDi:
  2499. case AArch64::STPXi:
  2500. case AArch64::STPDi:
  2501. case AArch64::STNPXi:
  2502. case AArch64::STNPDi:
  2503. Scale = TypeSize::Fixed(8);
  2504. Width = 16;
  2505. MinOffset = -64;
  2506. MaxOffset = 63;
  2507. break;
  2508. case AArch64::PRFMui:
  2509. case AArch64::LDRXui:
  2510. case AArch64::LDRDui:
  2511. case AArch64::STRXui:
  2512. case AArch64::STRDui:
  2513. Scale = TypeSize::Fixed(8);
  2514. Width = 8;
  2515. MinOffset = 0;
  2516. MaxOffset = 4095;
  2517. break;
  2518. case AArch64::StoreSwiftAsyncContext:
  2519. // Store is an STRXui, but there might be an ADDXri in the expansion too.
  2520. Scale = TypeSize::Fixed(1);
  2521. Width = 8;
  2522. MinOffset = 0;
  2523. MaxOffset = 4095;
  2524. break;
  2525. case AArch64::LDPWi:
  2526. case AArch64::LDPSi:
  2527. case AArch64::LDNPWi:
  2528. case AArch64::LDNPSi:
  2529. case AArch64::STPWi:
  2530. case AArch64::STPSi:
  2531. case AArch64::STNPWi:
  2532. case AArch64::STNPSi:
  2533. Scale = TypeSize::Fixed(4);
  2534. Width = 8;
  2535. MinOffset = -64;
  2536. MaxOffset = 63;
  2537. break;
  2538. case AArch64::LDRWui:
  2539. case AArch64::LDRSui:
  2540. case AArch64::LDRSWui:
  2541. case AArch64::STRWui:
  2542. case AArch64::STRSui:
  2543. Scale = TypeSize::Fixed(4);
  2544. Width = 4;
  2545. MinOffset = 0;
  2546. MaxOffset = 4095;
  2547. break;
  2548. case AArch64::LDRHui:
  2549. case AArch64::LDRHHui:
  2550. case AArch64::LDRSHWui:
  2551. case AArch64::LDRSHXui:
  2552. case AArch64::STRHui:
  2553. case AArch64::STRHHui:
  2554. Scale = TypeSize::Fixed(2);
  2555. Width = 2;
  2556. MinOffset = 0;
  2557. MaxOffset = 4095;
  2558. break;
  2559. case AArch64::LDRBui:
  2560. case AArch64::LDRBBui:
  2561. case AArch64::LDRSBWui:
  2562. case AArch64::LDRSBXui:
  2563. case AArch64::STRBui:
  2564. case AArch64::STRBBui:
  2565. Scale = TypeSize::Fixed(1);
  2566. Width = 1;
  2567. MinOffset = 0;
  2568. MaxOffset = 4095;
  2569. break;
  2570. case AArch64::STPXpre:
  2571. case AArch64::LDPXpost:
  2572. case AArch64::STPDpre:
  2573. case AArch64::LDPDpost:
  2574. Scale = TypeSize::Fixed(8);
  2575. Width = 8;
  2576. MinOffset = -512;
  2577. MaxOffset = 504;
  2578. break;
  2579. case AArch64::STPQpre:
  2580. case AArch64::LDPQpost:
  2581. Scale = TypeSize::Fixed(16);
  2582. Width = 16;
  2583. MinOffset = -1024;
  2584. MaxOffset = 1008;
  2585. break;
  2586. case AArch64::STRXpre:
  2587. case AArch64::STRDpre:
  2588. case AArch64::LDRXpost:
  2589. case AArch64::LDRDpost:
  2590. Scale = TypeSize::Fixed(1);
  2591. Width = 8;
  2592. MinOffset = -256;
  2593. MaxOffset = 255;
  2594. break;
  2595. case AArch64::STRQpre:
  2596. case AArch64::LDRQpost:
  2597. Scale = TypeSize::Fixed(1);
  2598. Width = 16;
  2599. MinOffset = -256;
  2600. MaxOffset = 255;
  2601. break;
  2602. case AArch64::ADDG:
  2603. Scale = TypeSize::Fixed(16);
  2604. Width = 0;
  2605. MinOffset = 0;
  2606. MaxOffset = 63;
  2607. break;
  2608. case AArch64::TAGPstack:
  2609. Scale = TypeSize::Fixed(16);
  2610. Width = 0;
  2611. // TAGP with a negative offset turns into SUBP, which has a maximum offset
  2612. // of 63 (not 64!).
  2613. MinOffset = -63;
  2614. MaxOffset = 63;
  2615. break;
  2616. case AArch64::LDG:
  2617. case AArch64::STGOffset:
  2618. case AArch64::STZGOffset:
  2619. Scale = TypeSize::Fixed(16);
  2620. Width = 16;
  2621. MinOffset = -256;
  2622. MaxOffset = 255;
  2623. break;
  2624. case AArch64::STR_ZZZZXI:
  2625. case AArch64::LDR_ZZZZXI:
  2626. Scale = TypeSize::Scalable(16);
  2627. Width = SVEMaxBytesPerVector * 4;
  2628. MinOffset = -256;
  2629. MaxOffset = 252;
  2630. break;
  2631. case AArch64::STR_ZZZXI:
  2632. case AArch64::LDR_ZZZXI:
  2633. Scale = TypeSize::Scalable(16);
  2634. Width = SVEMaxBytesPerVector * 3;
  2635. MinOffset = -256;
  2636. MaxOffset = 253;
  2637. break;
  2638. case AArch64::STR_ZZXI:
  2639. case AArch64::LDR_ZZXI:
  2640. Scale = TypeSize::Scalable(16);
  2641. Width = SVEMaxBytesPerVector * 2;
  2642. MinOffset = -256;
  2643. MaxOffset = 254;
  2644. break;
  2645. case AArch64::LDR_PXI:
  2646. case AArch64::STR_PXI:
  2647. Scale = TypeSize::Scalable(2);
  2648. Width = SVEMaxBytesPerVector / 8;
  2649. MinOffset = -256;
  2650. MaxOffset = 255;
  2651. break;
  2652. case AArch64::LDR_ZXI:
  2653. case AArch64::STR_ZXI:
  2654. Scale = TypeSize::Scalable(16);
  2655. Width = SVEMaxBytesPerVector;
  2656. MinOffset = -256;
  2657. MaxOffset = 255;
  2658. break;
  2659. case AArch64::LD1B_IMM:
  2660. case AArch64::LD1H_IMM:
  2661. case AArch64::LD1W_IMM:
  2662. case AArch64::LD1D_IMM:
  2663. case AArch64::LDNT1B_ZRI:
  2664. case AArch64::LDNT1H_ZRI:
  2665. case AArch64::LDNT1W_ZRI:
  2666. case AArch64::LDNT1D_ZRI:
  2667. case AArch64::ST1B_IMM:
  2668. case AArch64::ST1H_IMM:
  2669. case AArch64::ST1W_IMM:
  2670. case AArch64::ST1D_IMM:
  2671. case AArch64::STNT1B_ZRI:
  2672. case AArch64::STNT1H_ZRI:
  2673. case AArch64::STNT1W_ZRI:
  2674. case AArch64::STNT1D_ZRI:
  2675. case AArch64::LDNF1B_IMM:
  2676. case AArch64::LDNF1H_IMM:
  2677. case AArch64::LDNF1W_IMM:
  2678. case AArch64::LDNF1D_IMM:
  2679. // A full vectors worth of data
  2680. // Width = mbytes * elements
  2681. Scale = TypeSize::Scalable(16);
  2682. Width = SVEMaxBytesPerVector;
  2683. MinOffset = -8;
  2684. MaxOffset = 7;
  2685. break;
  2686. case AArch64::LD2B_IMM:
  2687. case AArch64::LD2H_IMM:
  2688. case AArch64::LD2W_IMM:
  2689. case AArch64::LD2D_IMM:
  2690. case AArch64::ST2B_IMM:
  2691. case AArch64::ST2H_IMM:
  2692. case AArch64::ST2W_IMM:
  2693. case AArch64::ST2D_IMM:
  2694. Scale = TypeSize::Scalable(32);
  2695. Width = SVEMaxBytesPerVector * 2;
  2696. MinOffset = -8;
  2697. MaxOffset = 7;
  2698. break;
  2699. case AArch64::LD3B_IMM:
  2700. case AArch64::LD3H_IMM:
  2701. case AArch64::LD3W_IMM:
  2702. case AArch64::LD3D_IMM:
  2703. case AArch64::ST3B_IMM:
  2704. case AArch64::ST3H_IMM:
  2705. case AArch64::ST3W_IMM:
  2706. case AArch64::ST3D_IMM:
  2707. Scale = TypeSize::Scalable(48);
  2708. Width = SVEMaxBytesPerVector * 3;
  2709. MinOffset = -8;
  2710. MaxOffset = 7;
  2711. break;
  2712. case AArch64::LD4B_IMM:
  2713. case AArch64::LD4H_IMM:
  2714. case AArch64::LD4W_IMM:
  2715. case AArch64::LD4D_IMM:
  2716. case AArch64::ST4B_IMM:
  2717. case AArch64::ST4H_IMM:
  2718. case AArch64::ST4W_IMM:
  2719. case AArch64::ST4D_IMM:
  2720. Scale = TypeSize::Scalable(64);
  2721. Width = SVEMaxBytesPerVector * 4;
  2722. MinOffset = -8;
  2723. MaxOffset = 7;
  2724. break;
  2725. case AArch64::LD1B_H_IMM:
  2726. case AArch64::LD1SB_H_IMM:
  2727. case AArch64::LD1H_S_IMM:
  2728. case AArch64::LD1SH_S_IMM:
  2729. case AArch64::LD1W_D_IMM:
  2730. case AArch64::LD1SW_D_IMM:
  2731. case AArch64::ST1B_H_IMM:
  2732. case AArch64::ST1H_S_IMM:
  2733. case AArch64::ST1W_D_IMM:
  2734. case AArch64::LDNF1B_H_IMM:
  2735. case AArch64::LDNF1SB_H_IMM:
  2736. case AArch64::LDNF1H_S_IMM:
  2737. case AArch64::LDNF1SH_S_IMM:
  2738. case AArch64::LDNF1W_D_IMM:
  2739. case AArch64::LDNF1SW_D_IMM:
  2740. // A half vector worth of data
  2741. // Width = mbytes * elements
  2742. Scale = TypeSize::Scalable(8);
  2743. Width = SVEMaxBytesPerVector / 2;
  2744. MinOffset = -8;
  2745. MaxOffset = 7;
  2746. break;
  2747. case AArch64::LD1B_S_IMM:
  2748. case AArch64::LD1SB_S_IMM:
  2749. case AArch64::LD1H_D_IMM:
  2750. case AArch64::LD1SH_D_IMM:
  2751. case AArch64::ST1B_S_IMM:
  2752. case AArch64::ST1H_D_IMM:
  2753. case AArch64::LDNF1B_S_IMM:
  2754. case AArch64::LDNF1SB_S_IMM:
  2755. case AArch64::LDNF1H_D_IMM:
  2756. case AArch64::LDNF1SH_D_IMM:
  2757. // A quarter vector worth of data
  2758. // Width = mbytes * elements
  2759. Scale = TypeSize::Scalable(4);
  2760. Width = SVEMaxBytesPerVector / 4;
  2761. MinOffset = -8;
  2762. MaxOffset = 7;
  2763. break;
  2764. case AArch64::LD1B_D_IMM:
  2765. case AArch64::LD1SB_D_IMM:
  2766. case AArch64::ST1B_D_IMM:
  2767. case AArch64::LDNF1B_D_IMM:
  2768. case AArch64::LDNF1SB_D_IMM:
  2769. // A eighth vector worth of data
  2770. // Width = mbytes * elements
  2771. Scale = TypeSize::Scalable(2);
  2772. Width = SVEMaxBytesPerVector / 8;
  2773. MinOffset = -8;
  2774. MaxOffset = 7;
  2775. break;
  2776. case AArch64::ST2GOffset:
  2777. case AArch64::STZ2GOffset:
  2778. Scale = TypeSize::Fixed(16);
  2779. Width = 32;
  2780. MinOffset = -256;
  2781. MaxOffset = 255;
  2782. break;
  2783. case AArch64::STGPi:
  2784. Scale = TypeSize::Fixed(16);
  2785. Width = 16;
  2786. MinOffset = -64;
  2787. MaxOffset = 63;
  2788. break;
  2789. case AArch64::LD1RB_IMM:
  2790. case AArch64::LD1RB_H_IMM:
  2791. case AArch64::LD1RB_S_IMM:
  2792. case AArch64::LD1RB_D_IMM:
  2793. case AArch64::LD1RSB_H_IMM:
  2794. case AArch64::LD1RSB_S_IMM:
  2795. case AArch64::LD1RSB_D_IMM:
  2796. Scale = TypeSize::Fixed(1);
  2797. Width = 1;
  2798. MinOffset = 0;
  2799. MaxOffset = 63;
  2800. break;
  2801. case AArch64::LD1RH_IMM:
  2802. case AArch64::LD1RH_S_IMM:
  2803. case AArch64::LD1RH_D_IMM:
  2804. case AArch64::LD1RSH_S_IMM:
  2805. case AArch64::LD1RSH_D_IMM:
  2806. Scale = TypeSize::Fixed(2);
  2807. Width = 2;
  2808. MinOffset = 0;
  2809. MaxOffset = 63;
  2810. break;
  2811. case AArch64::LD1RW_IMM:
  2812. case AArch64::LD1RW_D_IMM:
  2813. case AArch64::LD1RSW_IMM:
  2814. Scale = TypeSize::Fixed(4);
  2815. Width = 4;
  2816. MinOffset = 0;
  2817. MaxOffset = 63;
  2818. break;
  2819. case AArch64::LD1RD_IMM:
  2820. Scale = TypeSize::Fixed(8);
  2821. Width = 8;
  2822. MinOffset = 0;
  2823. MaxOffset = 63;
  2824. break;
  2825. }
  2826. return true;
  2827. }
  2828. // Scaling factor for unscaled load or store.
  2829. int AArch64InstrInfo::getMemScale(unsigned Opc) {
  2830. switch (Opc) {
  2831. default:
  2832. llvm_unreachable("Opcode has unknown scale!");
  2833. case AArch64::LDRBBui:
  2834. case AArch64::LDURBBi:
  2835. case AArch64::LDRSBWui:
  2836. case AArch64::LDURSBWi:
  2837. case AArch64::STRBBui:
  2838. case AArch64::STURBBi:
  2839. return 1;
  2840. case AArch64::LDRHHui:
  2841. case AArch64::LDURHHi:
  2842. case AArch64::LDRSHWui:
  2843. case AArch64::LDURSHWi:
  2844. case AArch64::STRHHui:
  2845. case AArch64::STURHHi:
  2846. return 2;
  2847. case AArch64::LDRSui:
  2848. case AArch64::LDURSi:
  2849. case AArch64::LDRSpre:
  2850. case AArch64::LDRSWui:
  2851. case AArch64::LDURSWi:
  2852. case AArch64::LDRWpre:
  2853. case AArch64::LDRWui:
  2854. case AArch64::LDURWi:
  2855. case AArch64::STRSui:
  2856. case AArch64::STURSi:
  2857. case AArch64::STRSpre:
  2858. case AArch64::STRWui:
  2859. case AArch64::STURWi:
  2860. case AArch64::STRWpre:
  2861. case AArch64::LDPSi:
  2862. case AArch64::LDPSWi:
  2863. case AArch64::LDPWi:
  2864. case AArch64::STPSi:
  2865. case AArch64::STPWi:
  2866. return 4;
  2867. case AArch64::LDRDui:
  2868. case AArch64::LDURDi:
  2869. case AArch64::LDRDpre:
  2870. case AArch64::LDRXui:
  2871. case AArch64::LDURXi:
  2872. case AArch64::LDRXpre:
  2873. case AArch64::STRDui:
  2874. case AArch64::STURDi:
  2875. case AArch64::STRDpre:
  2876. case AArch64::STRXui:
  2877. case AArch64::STURXi:
  2878. case AArch64::STRXpre:
  2879. case AArch64::LDPDi:
  2880. case AArch64::LDPXi:
  2881. case AArch64::STPDi:
  2882. case AArch64::STPXi:
  2883. return 8;
  2884. case AArch64::LDRQui:
  2885. case AArch64::LDURQi:
  2886. case AArch64::STRQui:
  2887. case AArch64::STURQi:
  2888. case AArch64::STRQpre:
  2889. case AArch64::LDPQi:
  2890. case AArch64::LDRQpre:
  2891. case AArch64::STPQi:
  2892. case AArch64::STGOffset:
  2893. case AArch64::STZGOffset:
  2894. case AArch64::ST2GOffset:
  2895. case AArch64::STZ2GOffset:
  2896. case AArch64::STGPi:
  2897. return 16;
  2898. }
  2899. }
  2900. bool AArch64InstrInfo::isPreLd(const MachineInstr &MI) {
  2901. switch (MI.getOpcode()) {
  2902. default:
  2903. return false;
  2904. case AArch64::LDRWpre:
  2905. case AArch64::LDRXpre:
  2906. case AArch64::LDRSpre:
  2907. case AArch64::LDRDpre:
  2908. case AArch64::LDRQpre:
  2909. return true;
  2910. }
  2911. }
  2912. bool AArch64InstrInfo::isPreSt(const MachineInstr &MI) {
  2913. switch (MI.getOpcode()) {
  2914. default:
  2915. return false;
  2916. case AArch64::STRWpre:
  2917. case AArch64::STRXpre:
  2918. case AArch64::STRSpre:
  2919. case AArch64::STRDpre:
  2920. case AArch64::STRQpre:
  2921. return true;
  2922. }
  2923. }
  2924. bool AArch64InstrInfo::isPreLdSt(const MachineInstr &MI) {
  2925. return isPreLd(MI) || isPreSt(MI);
  2926. }
  2927. // Scale the unscaled offsets. Returns false if the unscaled offset can't be
  2928. // scaled.
  2929. static bool scaleOffset(unsigned Opc, int64_t &Offset) {
  2930. int Scale = AArch64InstrInfo::getMemScale(Opc);
  2931. // If the byte-offset isn't a multiple of the stride, we can't scale this
  2932. // offset.
  2933. if (Offset % Scale != 0)
  2934. return false;
  2935. // Convert the byte-offset used by unscaled into an "element" offset used
  2936. // by the scaled pair load/store instructions.
  2937. Offset /= Scale;
  2938. return true;
  2939. }
  2940. static bool canPairLdStOpc(unsigned FirstOpc, unsigned SecondOpc) {
  2941. if (FirstOpc == SecondOpc)
  2942. return true;
  2943. // We can also pair sign-ext and zero-ext instructions.
  2944. switch (FirstOpc) {
  2945. default:
  2946. return false;
  2947. case AArch64::LDRWui:
  2948. case AArch64::LDURWi:
  2949. return SecondOpc == AArch64::LDRSWui || SecondOpc == AArch64::LDURSWi;
  2950. case AArch64::LDRSWui:
  2951. case AArch64::LDURSWi:
  2952. return SecondOpc == AArch64::LDRWui || SecondOpc == AArch64::LDURWi;
  2953. }
  2954. // These instructions can't be paired based on their opcodes.
  2955. return false;
  2956. }
  2957. static bool shouldClusterFI(const MachineFrameInfo &MFI, int FI1,
  2958. int64_t Offset1, unsigned Opcode1, int FI2,
  2959. int64_t Offset2, unsigned Opcode2) {
  2960. // Accesses through fixed stack object frame indices may access a different
  2961. // fixed stack slot. Check that the object offsets + offsets match.
  2962. if (MFI.isFixedObjectIndex(FI1) && MFI.isFixedObjectIndex(FI2)) {
  2963. int64_t ObjectOffset1 = MFI.getObjectOffset(FI1);
  2964. int64_t ObjectOffset2 = MFI.getObjectOffset(FI2);
  2965. assert(ObjectOffset1 <= ObjectOffset2 && "Object offsets are not ordered.");
  2966. // Convert to scaled object offsets.
  2967. int Scale1 = AArch64InstrInfo::getMemScale(Opcode1);
  2968. if (ObjectOffset1 % Scale1 != 0)
  2969. return false;
  2970. ObjectOffset1 /= Scale1;
  2971. int Scale2 = AArch64InstrInfo::getMemScale(Opcode2);
  2972. if (ObjectOffset2 % Scale2 != 0)
  2973. return false;
  2974. ObjectOffset2 /= Scale2;
  2975. ObjectOffset1 += Offset1;
  2976. ObjectOffset2 += Offset2;
  2977. return ObjectOffset1 + 1 == ObjectOffset2;
  2978. }
  2979. return FI1 == FI2;
  2980. }
  2981. /// Detect opportunities for ldp/stp formation.
  2982. ///
  2983. /// Only called for LdSt for which getMemOperandWithOffset returns true.
  2984. bool AArch64InstrInfo::shouldClusterMemOps(
  2985. ArrayRef<const MachineOperand *> BaseOps1,
  2986. ArrayRef<const MachineOperand *> BaseOps2, unsigned NumLoads,
  2987. unsigned NumBytes) const {
  2988. assert(BaseOps1.size() == 1 && BaseOps2.size() == 1);
  2989. const MachineOperand &BaseOp1 = *BaseOps1.front();
  2990. const MachineOperand &BaseOp2 = *BaseOps2.front();
  2991. const MachineInstr &FirstLdSt = *BaseOp1.getParent();
  2992. const MachineInstr &SecondLdSt = *BaseOp2.getParent();
  2993. if (BaseOp1.getType() != BaseOp2.getType())
  2994. return false;
  2995. assert((BaseOp1.isReg() || BaseOp1.isFI()) &&
  2996. "Only base registers and frame indices are supported.");
  2997. // Check for both base regs and base FI.
  2998. if (BaseOp1.isReg() && BaseOp1.getReg() != BaseOp2.getReg())
  2999. return false;
  3000. // Only cluster up to a single pair.
  3001. if (NumLoads > 2)
  3002. return false;
  3003. if (!isPairableLdStInst(FirstLdSt) || !isPairableLdStInst(SecondLdSt))
  3004. return false;
  3005. // Can we pair these instructions based on their opcodes?
  3006. unsigned FirstOpc = FirstLdSt.getOpcode();
  3007. unsigned SecondOpc = SecondLdSt.getOpcode();
  3008. if (!canPairLdStOpc(FirstOpc, SecondOpc))
  3009. return false;
  3010. // Can't merge volatiles or load/stores that have a hint to avoid pair
  3011. // formation, for example.
  3012. if (!isCandidateToMergeOrPair(FirstLdSt) ||
  3013. !isCandidateToMergeOrPair(SecondLdSt))
  3014. return false;
  3015. // isCandidateToMergeOrPair guarantees that operand 2 is an immediate.
  3016. int64_t Offset1 = FirstLdSt.getOperand(2).getImm();
  3017. if (hasUnscaledLdStOffset(FirstOpc) && !scaleOffset(FirstOpc, Offset1))
  3018. return false;
  3019. int64_t Offset2 = SecondLdSt.getOperand(2).getImm();
  3020. if (hasUnscaledLdStOffset(SecondOpc) && !scaleOffset(SecondOpc, Offset2))
  3021. return false;
  3022. // Pairwise instructions have a 7-bit signed offset field.
  3023. if (Offset1 > 63 || Offset1 < -64)
  3024. return false;
  3025. // The caller should already have ordered First/SecondLdSt by offset.
  3026. // Note: except for non-equal frame index bases
  3027. if (BaseOp1.isFI()) {
  3028. assert((!BaseOp1.isIdenticalTo(BaseOp2) || Offset1 <= Offset2) &&
  3029. "Caller should have ordered offsets.");
  3030. const MachineFrameInfo &MFI =
  3031. FirstLdSt.getParent()->getParent()->getFrameInfo();
  3032. return shouldClusterFI(MFI, BaseOp1.getIndex(), Offset1, FirstOpc,
  3033. BaseOp2.getIndex(), Offset2, SecondOpc);
  3034. }
  3035. assert(Offset1 <= Offset2 && "Caller should have ordered offsets.");
  3036. return Offset1 + 1 == Offset2;
  3037. }
  3038. static const MachineInstrBuilder &AddSubReg(const MachineInstrBuilder &MIB,
  3039. unsigned Reg, unsigned SubIdx,
  3040. unsigned State,
  3041. const TargetRegisterInfo *TRI) {
  3042. if (!SubIdx)
  3043. return MIB.addReg(Reg, State);
  3044. if (Register::isPhysicalRegister(Reg))
  3045. return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
  3046. return MIB.addReg(Reg, State, SubIdx);
  3047. }
  3048. static bool forwardCopyWillClobberTuple(unsigned DestReg, unsigned SrcReg,
  3049. unsigned NumRegs) {
  3050. // We really want the positive remainder mod 32 here, that happens to be
  3051. // easily obtainable with a mask.
  3052. return ((DestReg - SrcReg) & 0x1f) < NumRegs;
  3053. }
  3054. void AArch64InstrInfo::copyPhysRegTuple(MachineBasicBlock &MBB,
  3055. MachineBasicBlock::iterator I,
  3056. const DebugLoc &DL, MCRegister DestReg,
  3057. MCRegister SrcReg, bool KillSrc,
  3058. unsigned Opcode,
  3059. ArrayRef<unsigned> Indices) const {
  3060. assert(Subtarget.hasNEON() && "Unexpected register copy without NEON");
  3061. const TargetRegisterInfo *TRI = &getRegisterInfo();
  3062. uint16_t DestEncoding = TRI->getEncodingValue(DestReg);
  3063. uint16_t SrcEncoding = TRI->getEncodingValue(SrcReg);
  3064. unsigned NumRegs = Indices.size();
  3065. int SubReg = 0, End = NumRegs, Incr = 1;
  3066. if (forwardCopyWillClobberTuple(DestEncoding, SrcEncoding, NumRegs)) {
  3067. SubReg = NumRegs - 1;
  3068. End = -1;
  3069. Incr = -1;
  3070. }
  3071. for (; SubReg != End; SubReg += Incr) {
  3072. const MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opcode));
  3073. AddSubReg(MIB, DestReg, Indices[SubReg], RegState::Define, TRI);
  3074. AddSubReg(MIB, SrcReg, Indices[SubReg], 0, TRI);
  3075. AddSubReg(MIB, SrcReg, Indices[SubReg], getKillRegState(KillSrc), TRI);
  3076. }
  3077. }
  3078. void AArch64InstrInfo::copyGPRRegTuple(MachineBasicBlock &MBB,
  3079. MachineBasicBlock::iterator I,
  3080. DebugLoc DL, unsigned DestReg,
  3081. unsigned SrcReg, bool KillSrc,
  3082. unsigned Opcode, unsigned ZeroReg,
  3083. llvm::ArrayRef<unsigned> Indices) const {
  3084. const TargetRegisterInfo *TRI = &getRegisterInfo();
  3085. unsigned NumRegs = Indices.size();
  3086. #ifndef NDEBUG
  3087. uint16_t DestEncoding = TRI->getEncodingValue(DestReg);
  3088. uint16_t SrcEncoding = TRI->getEncodingValue(SrcReg);
  3089. assert(DestEncoding % NumRegs == 0 && SrcEncoding % NumRegs == 0 &&
  3090. "GPR reg sequences should not be able to overlap");
  3091. #endif
  3092. for (unsigned SubReg = 0; SubReg != NumRegs; ++SubReg) {
  3093. const MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opcode));
  3094. AddSubReg(MIB, DestReg, Indices[SubReg], RegState::Define, TRI);
  3095. MIB.addReg(ZeroReg);
  3096. AddSubReg(MIB, SrcReg, Indices[SubReg], getKillRegState(KillSrc), TRI);
  3097. MIB.addImm(0);
  3098. }
  3099. }
  3100. void AArch64InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
  3101. MachineBasicBlock::iterator I,
  3102. const DebugLoc &DL, MCRegister DestReg,
  3103. MCRegister SrcReg, bool KillSrc) const {
  3104. if (AArch64::GPR32spRegClass.contains(DestReg) &&
  3105. (AArch64::GPR32spRegClass.contains(SrcReg) || SrcReg == AArch64::WZR)) {
  3106. const TargetRegisterInfo *TRI = &getRegisterInfo();
  3107. if (DestReg == AArch64::WSP || SrcReg == AArch64::WSP) {
  3108. // If either operand is WSP, expand to ADD #0.
  3109. if (Subtarget.hasZeroCycleRegMove()) {
  3110. // Cyclone recognizes "ADD Xd, Xn, #0" as a zero-cycle register move.
  3111. MCRegister DestRegX = TRI->getMatchingSuperReg(
  3112. DestReg, AArch64::sub_32, &AArch64::GPR64spRegClass);
  3113. MCRegister SrcRegX = TRI->getMatchingSuperReg(
  3114. SrcReg, AArch64::sub_32, &AArch64::GPR64spRegClass);
  3115. // This instruction is reading and writing X registers. This may upset
  3116. // the register scavenger and machine verifier, so we need to indicate
  3117. // that we are reading an undefined value from SrcRegX, but a proper
  3118. // value from SrcReg.
  3119. BuildMI(MBB, I, DL, get(AArch64::ADDXri), DestRegX)
  3120. .addReg(SrcRegX, RegState::Undef)
  3121. .addImm(0)
  3122. .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0))
  3123. .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
  3124. } else {
  3125. BuildMI(MBB, I, DL, get(AArch64::ADDWri), DestReg)
  3126. .addReg(SrcReg, getKillRegState(KillSrc))
  3127. .addImm(0)
  3128. .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
  3129. }
  3130. } else if (SrcReg == AArch64::WZR && Subtarget.hasZeroCycleZeroingGP()) {
  3131. BuildMI(MBB, I, DL, get(AArch64::MOVZWi), DestReg)
  3132. .addImm(0)
  3133. .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
  3134. } else {
  3135. if (Subtarget.hasZeroCycleRegMove()) {
  3136. // Cyclone recognizes "ORR Xd, XZR, Xm" as a zero-cycle register move.
  3137. MCRegister DestRegX = TRI->getMatchingSuperReg(
  3138. DestReg, AArch64::sub_32, &AArch64::GPR64spRegClass);
  3139. MCRegister SrcRegX = TRI->getMatchingSuperReg(
  3140. SrcReg, AArch64::sub_32, &AArch64::GPR64spRegClass);
  3141. // This instruction is reading and writing X registers. This may upset
  3142. // the register scavenger and machine verifier, so we need to indicate
  3143. // that we are reading an undefined value from SrcRegX, but a proper
  3144. // value from SrcReg.
  3145. BuildMI(MBB, I, DL, get(AArch64::ORRXrr), DestRegX)
  3146. .addReg(AArch64::XZR)
  3147. .addReg(SrcRegX, RegState::Undef)
  3148. .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
  3149. } else {
  3150. // Otherwise, expand to ORR WZR.
  3151. BuildMI(MBB, I, DL, get(AArch64::ORRWrr), DestReg)
  3152. .addReg(AArch64::WZR)
  3153. .addReg(SrcReg, getKillRegState(KillSrc));
  3154. }
  3155. }
  3156. return;
  3157. }
  3158. // Copy a Predicate register by ORRing with itself.
  3159. if (AArch64::PPRRegClass.contains(DestReg) &&
  3160. AArch64::PPRRegClass.contains(SrcReg)) {
  3161. assert(Subtarget.hasSVE() && "Unexpected SVE register.");
  3162. BuildMI(MBB, I, DL, get(AArch64::ORR_PPzPP), DestReg)
  3163. .addReg(SrcReg) // Pg
  3164. .addReg(SrcReg)
  3165. .addReg(SrcReg, getKillRegState(KillSrc));
  3166. return;
  3167. }
  3168. // Copy a Z register by ORRing with itself.
  3169. if (AArch64::ZPRRegClass.contains(DestReg) &&
  3170. AArch64::ZPRRegClass.contains(SrcReg)) {
  3171. assert(Subtarget.hasSVE() && "Unexpected SVE register.");
  3172. BuildMI(MBB, I, DL, get(AArch64::ORR_ZZZ), DestReg)
  3173. .addReg(SrcReg)
  3174. .addReg(SrcReg, getKillRegState(KillSrc));
  3175. return;
  3176. }
  3177. // Copy a Z register pair by copying the individual sub-registers.
  3178. if (AArch64::ZPR2RegClass.contains(DestReg) &&
  3179. AArch64::ZPR2RegClass.contains(SrcReg)) {
  3180. static const unsigned Indices[] = {AArch64::zsub0, AArch64::zsub1};
  3181. copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORR_ZZZ,
  3182. Indices);
  3183. return;
  3184. }
  3185. // Copy a Z register triple by copying the individual sub-registers.
  3186. if (AArch64::ZPR3RegClass.contains(DestReg) &&
  3187. AArch64::ZPR3RegClass.contains(SrcReg)) {
  3188. static const unsigned Indices[] = {AArch64::zsub0, AArch64::zsub1,
  3189. AArch64::zsub2};
  3190. copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORR_ZZZ,
  3191. Indices);
  3192. return;
  3193. }
  3194. // Copy a Z register quad by copying the individual sub-registers.
  3195. if (AArch64::ZPR4RegClass.contains(DestReg) &&
  3196. AArch64::ZPR4RegClass.contains(SrcReg)) {
  3197. static const unsigned Indices[] = {AArch64::zsub0, AArch64::zsub1,
  3198. AArch64::zsub2, AArch64::zsub3};
  3199. copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORR_ZZZ,
  3200. Indices);
  3201. return;
  3202. }
  3203. if (AArch64::GPR64spRegClass.contains(DestReg) &&
  3204. (AArch64::GPR64spRegClass.contains(SrcReg) || SrcReg == AArch64::XZR)) {
  3205. if (DestReg == AArch64::SP || SrcReg == AArch64::SP) {
  3206. // If either operand is SP, expand to ADD #0.
  3207. BuildMI(MBB, I, DL, get(AArch64::ADDXri), DestReg)
  3208. .addReg(SrcReg, getKillRegState(KillSrc))
  3209. .addImm(0)
  3210. .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
  3211. } else if (SrcReg == AArch64::XZR && Subtarget.hasZeroCycleZeroingGP()) {
  3212. BuildMI(MBB, I, DL, get(AArch64::MOVZXi), DestReg)
  3213. .addImm(0)
  3214. .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
  3215. } else {
  3216. // Otherwise, expand to ORR XZR.
  3217. BuildMI(MBB, I, DL, get(AArch64::ORRXrr), DestReg)
  3218. .addReg(AArch64::XZR)
  3219. .addReg(SrcReg, getKillRegState(KillSrc));
  3220. }
  3221. return;
  3222. }
  3223. // Copy a DDDD register quad by copying the individual sub-registers.
  3224. if (AArch64::DDDDRegClass.contains(DestReg) &&
  3225. AArch64::DDDDRegClass.contains(SrcReg)) {
  3226. static const unsigned Indices[] = {AArch64::dsub0, AArch64::dsub1,
  3227. AArch64::dsub2, AArch64::dsub3};
  3228. copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv8i8,
  3229. Indices);
  3230. return;
  3231. }
  3232. // Copy a DDD register triple by copying the individual sub-registers.
  3233. if (AArch64::DDDRegClass.contains(DestReg) &&
  3234. AArch64::DDDRegClass.contains(SrcReg)) {
  3235. static const unsigned Indices[] = {AArch64::dsub0, AArch64::dsub1,
  3236. AArch64::dsub2};
  3237. copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv8i8,
  3238. Indices);
  3239. return;
  3240. }
  3241. // Copy a DD register pair by copying the individual sub-registers.
  3242. if (AArch64::DDRegClass.contains(DestReg) &&
  3243. AArch64::DDRegClass.contains(SrcReg)) {
  3244. static const unsigned Indices[] = {AArch64::dsub0, AArch64::dsub1};
  3245. copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv8i8,
  3246. Indices);
  3247. return;
  3248. }
  3249. // Copy a QQQQ register quad by copying the individual sub-registers.
  3250. if (AArch64::QQQQRegClass.contains(DestReg) &&
  3251. AArch64::QQQQRegClass.contains(SrcReg)) {
  3252. static const unsigned Indices[] = {AArch64::qsub0, AArch64::qsub1,
  3253. AArch64::qsub2, AArch64::qsub3};
  3254. copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv16i8,
  3255. Indices);
  3256. return;
  3257. }
  3258. // Copy a QQQ register triple by copying the individual sub-registers.
  3259. if (AArch64::QQQRegClass.contains(DestReg) &&
  3260. AArch64::QQQRegClass.contains(SrcReg)) {
  3261. static const unsigned Indices[] = {AArch64::qsub0, AArch64::qsub1,
  3262. AArch64::qsub2};
  3263. copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv16i8,
  3264. Indices);
  3265. return;
  3266. }
  3267. // Copy a QQ register pair by copying the individual sub-registers.
  3268. if (AArch64::QQRegClass.contains(DestReg) &&
  3269. AArch64::QQRegClass.contains(SrcReg)) {
  3270. static const unsigned Indices[] = {AArch64::qsub0, AArch64::qsub1};
  3271. copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRv16i8,
  3272. Indices);
  3273. return;
  3274. }
  3275. if (AArch64::XSeqPairsClassRegClass.contains(DestReg) &&
  3276. AArch64::XSeqPairsClassRegClass.contains(SrcReg)) {
  3277. static const unsigned Indices[] = {AArch64::sube64, AArch64::subo64};
  3278. copyGPRRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRXrs,
  3279. AArch64::XZR, Indices);
  3280. return;
  3281. }
  3282. if (AArch64::WSeqPairsClassRegClass.contains(DestReg) &&
  3283. AArch64::WSeqPairsClassRegClass.contains(SrcReg)) {
  3284. static const unsigned Indices[] = {AArch64::sube32, AArch64::subo32};
  3285. copyGPRRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, AArch64::ORRWrs,
  3286. AArch64::WZR, Indices);
  3287. return;
  3288. }
  3289. if (AArch64::FPR128RegClass.contains(DestReg) &&
  3290. AArch64::FPR128RegClass.contains(SrcReg)) {
  3291. if (Subtarget.hasNEON()) {
  3292. BuildMI(MBB, I, DL, get(AArch64::ORRv16i8), DestReg)
  3293. .addReg(SrcReg)
  3294. .addReg(SrcReg, getKillRegState(KillSrc));
  3295. } else {
  3296. BuildMI(MBB, I, DL, get(AArch64::STRQpre))
  3297. .addReg(AArch64::SP, RegState::Define)
  3298. .addReg(SrcReg, getKillRegState(KillSrc))
  3299. .addReg(AArch64::SP)
  3300. .addImm(-16);
  3301. BuildMI(MBB, I, DL, get(AArch64::LDRQpre))
  3302. .addReg(AArch64::SP, RegState::Define)
  3303. .addReg(DestReg, RegState::Define)
  3304. .addReg(AArch64::SP)
  3305. .addImm(16);
  3306. }
  3307. return;
  3308. }
  3309. if (AArch64::FPR64RegClass.contains(DestReg) &&
  3310. AArch64::FPR64RegClass.contains(SrcReg)) {
  3311. BuildMI(MBB, I, DL, get(AArch64::FMOVDr), DestReg)
  3312. .addReg(SrcReg, getKillRegState(KillSrc));
  3313. return;
  3314. }
  3315. if (AArch64::FPR32RegClass.contains(DestReg) &&
  3316. AArch64::FPR32RegClass.contains(SrcReg)) {
  3317. BuildMI(MBB, I, DL, get(AArch64::FMOVSr), DestReg)
  3318. .addReg(SrcReg, getKillRegState(KillSrc));
  3319. return;
  3320. }
  3321. if (AArch64::FPR16RegClass.contains(DestReg) &&
  3322. AArch64::FPR16RegClass.contains(SrcReg)) {
  3323. DestReg =
  3324. RI.getMatchingSuperReg(DestReg, AArch64::hsub, &AArch64::FPR32RegClass);
  3325. SrcReg =
  3326. RI.getMatchingSuperReg(SrcReg, AArch64::hsub, &AArch64::FPR32RegClass);
  3327. BuildMI(MBB, I, DL, get(AArch64::FMOVSr), DestReg)
  3328. .addReg(SrcReg, getKillRegState(KillSrc));
  3329. return;
  3330. }
  3331. if (AArch64::FPR8RegClass.contains(DestReg) &&
  3332. AArch64::FPR8RegClass.contains(SrcReg)) {
  3333. DestReg =
  3334. RI.getMatchingSuperReg(DestReg, AArch64::bsub, &AArch64::FPR32RegClass);
  3335. SrcReg =
  3336. RI.getMatchingSuperReg(SrcReg, AArch64::bsub, &AArch64::FPR32RegClass);
  3337. BuildMI(MBB, I, DL, get(AArch64::FMOVSr), DestReg)
  3338. .addReg(SrcReg, getKillRegState(KillSrc));
  3339. return;
  3340. }
  3341. // Copies between GPR64 and FPR64.
  3342. if (AArch64::FPR64RegClass.contains(DestReg) &&
  3343. AArch64::GPR64RegClass.contains(SrcReg)) {
  3344. BuildMI(MBB, I, DL, get(AArch64::FMOVXDr), DestReg)
  3345. .addReg(SrcReg, getKillRegState(KillSrc));
  3346. return;
  3347. }
  3348. if (AArch64::GPR64RegClass.contains(DestReg) &&
  3349. AArch64::FPR64RegClass.contains(SrcReg)) {
  3350. BuildMI(MBB, I, DL, get(AArch64::FMOVDXr), DestReg)
  3351. .addReg(SrcReg, getKillRegState(KillSrc));
  3352. return;
  3353. }
  3354. // Copies between GPR32 and FPR32.
  3355. if (AArch64::FPR32RegClass.contains(DestReg) &&
  3356. AArch64::GPR32RegClass.contains(SrcReg)) {
  3357. BuildMI(MBB, I, DL, get(AArch64::FMOVWSr), DestReg)
  3358. .addReg(SrcReg, getKillRegState(KillSrc));
  3359. return;
  3360. }
  3361. if (AArch64::GPR32RegClass.contains(DestReg) &&
  3362. AArch64::FPR32RegClass.contains(SrcReg)) {
  3363. BuildMI(MBB, I, DL, get(AArch64::FMOVSWr), DestReg)
  3364. .addReg(SrcReg, getKillRegState(KillSrc));
  3365. return;
  3366. }
  3367. if (DestReg == AArch64::NZCV) {
  3368. assert(AArch64::GPR64RegClass.contains(SrcReg) && "Invalid NZCV copy");
  3369. BuildMI(MBB, I, DL, get(AArch64::MSR))
  3370. .addImm(AArch64SysReg::NZCV)
  3371. .addReg(SrcReg, getKillRegState(KillSrc))
  3372. .addReg(AArch64::NZCV, RegState::Implicit | RegState::Define);
  3373. return;
  3374. }
  3375. if (SrcReg == AArch64::NZCV) {
  3376. assert(AArch64::GPR64RegClass.contains(DestReg) && "Invalid NZCV copy");
  3377. BuildMI(MBB, I, DL, get(AArch64::MRS), DestReg)
  3378. .addImm(AArch64SysReg::NZCV)
  3379. .addReg(AArch64::NZCV, RegState::Implicit | getKillRegState(KillSrc));
  3380. return;
  3381. }
  3382. #ifndef NDEBUG
  3383. const TargetRegisterInfo &TRI = getRegisterInfo();
  3384. errs() << TRI.getRegAsmName(DestReg) << " = COPY "
  3385. << TRI.getRegAsmName(SrcReg) << "\n";
  3386. #endif
  3387. llvm_unreachable("unimplemented reg-to-reg copy");
  3388. }
  3389. static void storeRegPairToStackSlot(const TargetRegisterInfo &TRI,
  3390. MachineBasicBlock &MBB,
  3391. MachineBasicBlock::iterator InsertBefore,
  3392. const MCInstrDesc &MCID,
  3393. Register SrcReg, bool IsKill,
  3394. unsigned SubIdx0, unsigned SubIdx1, int FI,
  3395. MachineMemOperand *MMO) {
  3396. Register SrcReg0 = SrcReg;
  3397. Register SrcReg1 = SrcReg;
  3398. if (Register::isPhysicalRegister(SrcReg)) {
  3399. SrcReg0 = TRI.getSubReg(SrcReg, SubIdx0);
  3400. SubIdx0 = 0;
  3401. SrcReg1 = TRI.getSubReg(SrcReg, SubIdx1);
  3402. SubIdx1 = 0;
  3403. }
  3404. BuildMI(MBB, InsertBefore, DebugLoc(), MCID)
  3405. .addReg(SrcReg0, getKillRegState(IsKill), SubIdx0)
  3406. .addReg(SrcReg1, getKillRegState(IsKill), SubIdx1)
  3407. .addFrameIndex(FI)
  3408. .addImm(0)
  3409. .addMemOperand(MMO);
  3410. }
  3411. void AArch64InstrInfo::storeRegToStackSlot(
  3412. MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg,
  3413. bool isKill, int FI, const TargetRegisterClass *RC,
  3414. const TargetRegisterInfo *TRI) const {
  3415. MachineFunction &MF = *MBB.getParent();
  3416. MachineFrameInfo &MFI = MF.getFrameInfo();
  3417. MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FI);
  3418. MachineMemOperand *MMO =
  3419. MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOStore,
  3420. MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
  3421. unsigned Opc = 0;
  3422. bool Offset = true;
  3423. unsigned StackID = TargetStackID::Default;
  3424. switch (TRI->getSpillSize(*RC)) {
  3425. case 1:
  3426. if (AArch64::FPR8RegClass.hasSubClassEq(RC))
  3427. Opc = AArch64::STRBui;
  3428. break;
  3429. case 2:
  3430. if (AArch64::FPR16RegClass.hasSubClassEq(RC))
  3431. Opc = AArch64::STRHui;
  3432. else if (AArch64::PPRRegClass.hasSubClassEq(RC)) {
  3433. assert(Subtarget.hasSVE() && "Unexpected register store without SVE");
  3434. Opc = AArch64::STR_PXI;
  3435. StackID = TargetStackID::ScalableVector;
  3436. }
  3437. break;
  3438. case 4:
  3439. if (AArch64::GPR32allRegClass.hasSubClassEq(RC)) {
  3440. Opc = AArch64::STRWui;
  3441. if (Register::isVirtualRegister(SrcReg))
  3442. MF.getRegInfo().constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
  3443. else
  3444. assert(SrcReg != AArch64::WSP);
  3445. } else if (AArch64::FPR32RegClass.hasSubClassEq(RC))
  3446. Opc = AArch64::STRSui;
  3447. break;
  3448. case 8:
  3449. if (AArch64::GPR64allRegClass.hasSubClassEq(RC)) {
  3450. Opc = AArch64::STRXui;
  3451. if (Register::isVirtualRegister(SrcReg))
  3452. MF.getRegInfo().constrainRegClass(SrcReg, &AArch64::GPR64RegClass);
  3453. else
  3454. assert(SrcReg != AArch64::SP);
  3455. } else if (AArch64::FPR64RegClass.hasSubClassEq(RC)) {
  3456. Opc = AArch64::STRDui;
  3457. } else if (AArch64::WSeqPairsClassRegClass.hasSubClassEq(RC)) {
  3458. storeRegPairToStackSlot(getRegisterInfo(), MBB, MBBI,
  3459. get(AArch64::STPWi), SrcReg, isKill,
  3460. AArch64::sube32, AArch64::subo32, FI, MMO);
  3461. return;
  3462. }
  3463. break;
  3464. case 16:
  3465. if (AArch64::FPR128RegClass.hasSubClassEq(RC))
  3466. Opc = AArch64::STRQui;
  3467. else if (AArch64::DDRegClass.hasSubClassEq(RC)) {
  3468. assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
  3469. Opc = AArch64::ST1Twov1d;
  3470. Offset = false;
  3471. } else if (AArch64::XSeqPairsClassRegClass.hasSubClassEq(RC)) {
  3472. storeRegPairToStackSlot(getRegisterInfo(), MBB, MBBI,
  3473. get(AArch64::STPXi), SrcReg, isKill,
  3474. AArch64::sube64, AArch64::subo64, FI, MMO);
  3475. return;
  3476. } else if (AArch64::ZPRRegClass.hasSubClassEq(RC)) {
  3477. assert(Subtarget.hasSVE() && "Unexpected register store without SVE");
  3478. Opc = AArch64::STR_ZXI;
  3479. StackID = TargetStackID::ScalableVector;
  3480. }
  3481. break;
  3482. case 24:
  3483. if (AArch64::DDDRegClass.hasSubClassEq(RC)) {
  3484. assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
  3485. Opc = AArch64::ST1Threev1d;
  3486. Offset = false;
  3487. }
  3488. break;
  3489. case 32:
  3490. if (AArch64::DDDDRegClass.hasSubClassEq(RC)) {
  3491. assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
  3492. Opc = AArch64::ST1Fourv1d;
  3493. Offset = false;
  3494. } else if (AArch64::QQRegClass.hasSubClassEq(RC)) {
  3495. assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
  3496. Opc = AArch64::ST1Twov2d;
  3497. Offset = false;
  3498. } else if (AArch64::ZPR2RegClass.hasSubClassEq(RC)) {
  3499. assert(Subtarget.hasSVE() && "Unexpected register store without SVE");
  3500. Opc = AArch64::STR_ZZXI;
  3501. StackID = TargetStackID::ScalableVector;
  3502. }
  3503. break;
  3504. case 48:
  3505. if (AArch64::QQQRegClass.hasSubClassEq(RC)) {
  3506. assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
  3507. Opc = AArch64::ST1Threev2d;
  3508. Offset = false;
  3509. } else if (AArch64::ZPR3RegClass.hasSubClassEq(RC)) {
  3510. assert(Subtarget.hasSVE() && "Unexpected register store without SVE");
  3511. Opc = AArch64::STR_ZZZXI;
  3512. StackID = TargetStackID::ScalableVector;
  3513. }
  3514. break;
  3515. case 64:
  3516. if (AArch64::QQQQRegClass.hasSubClassEq(RC)) {
  3517. assert(Subtarget.hasNEON() && "Unexpected register store without NEON");
  3518. Opc = AArch64::ST1Fourv2d;
  3519. Offset = false;
  3520. } else if (AArch64::ZPR4RegClass.hasSubClassEq(RC)) {
  3521. assert(Subtarget.hasSVE() && "Unexpected register store without SVE");
  3522. Opc = AArch64::STR_ZZZZXI;
  3523. StackID = TargetStackID::ScalableVector;
  3524. }
  3525. break;
  3526. }
  3527. assert(Opc && "Unknown register class");
  3528. MFI.setStackID(FI, StackID);
  3529. const MachineInstrBuilder MI = BuildMI(MBB, MBBI, DebugLoc(), get(Opc))
  3530. .addReg(SrcReg, getKillRegState(isKill))
  3531. .addFrameIndex(FI);
  3532. if (Offset)
  3533. MI.addImm(0);
  3534. MI.addMemOperand(MMO);
  3535. }
  3536. static void loadRegPairFromStackSlot(const TargetRegisterInfo &TRI,
  3537. MachineBasicBlock &MBB,
  3538. MachineBasicBlock::iterator InsertBefore,
  3539. const MCInstrDesc &MCID,
  3540. Register DestReg, unsigned SubIdx0,
  3541. unsigned SubIdx1, int FI,
  3542. MachineMemOperand *MMO) {
  3543. Register DestReg0 = DestReg;
  3544. Register DestReg1 = DestReg;
  3545. bool IsUndef = true;
  3546. if (Register::isPhysicalRegister(DestReg)) {
  3547. DestReg0 = TRI.getSubReg(DestReg, SubIdx0);
  3548. SubIdx0 = 0;
  3549. DestReg1 = TRI.getSubReg(DestReg, SubIdx1);
  3550. SubIdx1 = 0;
  3551. IsUndef = false;
  3552. }
  3553. BuildMI(MBB, InsertBefore, DebugLoc(), MCID)
  3554. .addReg(DestReg0, RegState::Define | getUndefRegState(IsUndef), SubIdx0)
  3555. .addReg(DestReg1, RegState::Define | getUndefRegState(IsUndef), SubIdx1)
  3556. .addFrameIndex(FI)
  3557. .addImm(0)
  3558. .addMemOperand(MMO);
  3559. }
  3560. void AArch64InstrInfo::loadRegFromStackSlot(
  3561. MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg,
  3562. int FI, const TargetRegisterClass *RC,
  3563. const TargetRegisterInfo *TRI) const {
  3564. MachineFunction &MF = *MBB.getParent();
  3565. MachineFrameInfo &MFI = MF.getFrameInfo();
  3566. MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FI);
  3567. MachineMemOperand *MMO =
  3568. MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOLoad,
  3569. MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
  3570. unsigned Opc = 0;
  3571. bool Offset = true;
  3572. unsigned StackID = TargetStackID::Default;
  3573. switch (TRI->getSpillSize(*RC)) {
  3574. case 1:
  3575. if (AArch64::FPR8RegClass.hasSubClassEq(RC))
  3576. Opc = AArch64::LDRBui;
  3577. break;
  3578. case 2:
  3579. if (AArch64::FPR16RegClass.hasSubClassEq(RC))
  3580. Opc = AArch64::LDRHui;
  3581. else if (AArch64::PPRRegClass.hasSubClassEq(RC)) {
  3582. assert(Subtarget.hasSVE() && "Unexpected register load without SVE");
  3583. Opc = AArch64::LDR_PXI;
  3584. StackID = TargetStackID::ScalableVector;
  3585. }
  3586. break;
  3587. case 4:
  3588. if (AArch64::GPR32allRegClass.hasSubClassEq(RC)) {
  3589. Opc = AArch64::LDRWui;
  3590. if (Register::isVirtualRegister(DestReg))
  3591. MF.getRegInfo().constrainRegClass(DestReg, &AArch64::GPR32RegClass);
  3592. else
  3593. assert(DestReg != AArch64::WSP);
  3594. } else if (AArch64::FPR32RegClass.hasSubClassEq(RC))
  3595. Opc = AArch64::LDRSui;
  3596. break;
  3597. case 8:
  3598. if (AArch64::GPR64allRegClass.hasSubClassEq(RC)) {
  3599. Opc = AArch64::LDRXui;
  3600. if (Register::isVirtualRegister(DestReg))
  3601. MF.getRegInfo().constrainRegClass(DestReg, &AArch64::GPR64RegClass);
  3602. else
  3603. assert(DestReg != AArch64::SP);
  3604. } else if (AArch64::FPR64RegClass.hasSubClassEq(RC)) {
  3605. Opc = AArch64::LDRDui;
  3606. } else if (AArch64::WSeqPairsClassRegClass.hasSubClassEq(RC)) {
  3607. loadRegPairFromStackSlot(getRegisterInfo(), MBB, MBBI,
  3608. get(AArch64::LDPWi), DestReg, AArch64::sube32,
  3609. AArch64::subo32, FI, MMO);
  3610. return;
  3611. }
  3612. break;
  3613. case 16:
  3614. if (AArch64::FPR128RegClass.hasSubClassEq(RC))
  3615. Opc = AArch64::LDRQui;
  3616. else if (AArch64::DDRegClass.hasSubClassEq(RC)) {
  3617. assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
  3618. Opc = AArch64::LD1Twov1d;
  3619. Offset = false;
  3620. } else if (AArch64::XSeqPairsClassRegClass.hasSubClassEq(RC)) {
  3621. loadRegPairFromStackSlot(getRegisterInfo(), MBB, MBBI,
  3622. get(AArch64::LDPXi), DestReg, AArch64::sube64,
  3623. AArch64::subo64, FI, MMO);
  3624. return;
  3625. } else if (AArch64::ZPRRegClass.hasSubClassEq(RC)) {
  3626. assert(Subtarget.hasSVE() && "Unexpected register load without SVE");
  3627. Opc = AArch64::LDR_ZXI;
  3628. StackID = TargetStackID::ScalableVector;
  3629. }
  3630. break;
  3631. case 24:
  3632. if (AArch64::DDDRegClass.hasSubClassEq(RC)) {
  3633. assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
  3634. Opc = AArch64::LD1Threev1d;
  3635. Offset = false;
  3636. }
  3637. break;
  3638. case 32:
  3639. if (AArch64::DDDDRegClass.hasSubClassEq(RC)) {
  3640. assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
  3641. Opc = AArch64::LD1Fourv1d;
  3642. Offset = false;
  3643. } else if (AArch64::QQRegClass.hasSubClassEq(RC)) {
  3644. assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
  3645. Opc = AArch64::LD1Twov2d;
  3646. Offset = false;
  3647. } else if (AArch64::ZPR2RegClass.hasSubClassEq(RC)) {
  3648. assert(Subtarget.hasSVE() && "Unexpected register load without SVE");
  3649. Opc = AArch64::LDR_ZZXI;
  3650. StackID = TargetStackID::ScalableVector;
  3651. }
  3652. break;
  3653. case 48:
  3654. if (AArch64::QQQRegClass.hasSubClassEq(RC)) {
  3655. assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
  3656. Opc = AArch64::LD1Threev2d;
  3657. Offset = false;
  3658. } else if (AArch64::ZPR3RegClass.hasSubClassEq(RC)) {
  3659. assert(Subtarget.hasSVE() && "Unexpected register load without SVE");
  3660. Opc = AArch64::LDR_ZZZXI;
  3661. StackID = TargetStackID::ScalableVector;
  3662. }
  3663. break;
  3664. case 64:
  3665. if (AArch64::QQQQRegClass.hasSubClassEq(RC)) {
  3666. assert(Subtarget.hasNEON() && "Unexpected register load without NEON");
  3667. Opc = AArch64::LD1Fourv2d;
  3668. Offset = false;
  3669. } else if (AArch64::ZPR4RegClass.hasSubClassEq(RC)) {
  3670. assert(Subtarget.hasSVE() && "Unexpected register load without SVE");
  3671. Opc = AArch64::LDR_ZZZZXI;
  3672. StackID = TargetStackID::ScalableVector;
  3673. }
  3674. break;
  3675. }
  3676. assert(Opc && "Unknown register class");
  3677. MFI.setStackID(FI, StackID);
  3678. const MachineInstrBuilder MI = BuildMI(MBB, MBBI, DebugLoc(), get(Opc))
  3679. .addReg(DestReg, getDefRegState(true))
  3680. .addFrameIndex(FI);
  3681. if (Offset)
  3682. MI.addImm(0);
  3683. MI.addMemOperand(MMO);
  3684. }
  3685. bool llvm::isNZCVTouchedInInstructionRange(const MachineInstr &DefMI,
  3686. const MachineInstr &UseMI,
  3687. const TargetRegisterInfo *TRI) {
  3688. return any_of(instructionsWithoutDebug(std::next(DefMI.getIterator()),
  3689. UseMI.getIterator()),
  3690. [TRI](const MachineInstr &I) {
  3691. return I.modifiesRegister(AArch64::NZCV, TRI) ||
  3692. I.readsRegister(AArch64::NZCV, TRI);
  3693. });
  3694. }
  3695. void AArch64InstrInfo::decomposeStackOffsetForDwarfOffsets(
  3696. const StackOffset &Offset, int64_t &ByteSized, int64_t &VGSized) {
  3697. // The smallest scalable element supported by scaled SVE addressing
  3698. // modes are predicates, which are 2 scalable bytes in size. So the scalable
  3699. // byte offset must always be a multiple of 2.
  3700. assert(Offset.getScalable() % 2 == 0 && "Invalid frame offset");
  3701. // VGSized offsets are divided by '2', because the VG register is the
  3702. // the number of 64bit granules as opposed to 128bit vector chunks,
  3703. // which is how the 'n' in e.g. MVT::nxv1i8 is modelled.
  3704. // So, for a stack offset of 16 MVT::nxv1i8's, the size is n x 16 bytes.
  3705. // VG = n * 2 and the dwarf offset must be VG * 8 bytes.
  3706. ByteSized = Offset.getFixed();
  3707. VGSized = Offset.getScalable() / 2;
  3708. }
  3709. /// Returns the offset in parts to which this frame offset can be
  3710. /// decomposed for the purpose of describing a frame offset.
  3711. /// For non-scalable offsets this is simply its byte size.
  3712. void AArch64InstrInfo::decomposeStackOffsetForFrameOffsets(
  3713. const StackOffset &Offset, int64_t &NumBytes, int64_t &NumPredicateVectors,
  3714. int64_t &NumDataVectors) {
  3715. // The smallest scalable element supported by scaled SVE addressing
  3716. // modes are predicates, which are 2 scalable bytes in size. So the scalable
  3717. // byte offset must always be a multiple of 2.
  3718. assert(Offset.getScalable() % 2 == 0 && "Invalid frame offset");
  3719. NumBytes = Offset.getFixed();
  3720. NumDataVectors = 0;
  3721. NumPredicateVectors = Offset.getScalable() / 2;
  3722. // This method is used to get the offsets to adjust the frame offset.
  3723. // If the function requires ADDPL to be used and needs more than two ADDPL
  3724. // instructions, part of the offset is folded into NumDataVectors so that it
  3725. // uses ADDVL for part of it, reducing the number of ADDPL instructions.
  3726. if (NumPredicateVectors % 8 == 0 || NumPredicateVectors < -64 ||
  3727. NumPredicateVectors > 62) {
  3728. NumDataVectors = NumPredicateVectors / 8;
  3729. NumPredicateVectors -= NumDataVectors * 8;
  3730. }
  3731. }
  3732. // Helper function to emit a frame offset adjustment from a given
  3733. // pointer (SrcReg), stored into DestReg. This function is explicit
  3734. // in that it requires the opcode.
  3735. static void emitFrameOffsetAdj(MachineBasicBlock &MBB,
  3736. MachineBasicBlock::iterator MBBI,
  3737. const DebugLoc &DL, unsigned DestReg,
  3738. unsigned SrcReg, int64_t Offset, unsigned Opc,
  3739. const TargetInstrInfo *TII,
  3740. MachineInstr::MIFlag Flag, bool NeedsWinCFI,
  3741. bool *HasWinCFI) {
  3742. int Sign = 1;
  3743. unsigned MaxEncoding, ShiftSize;
  3744. switch (Opc) {
  3745. case AArch64::ADDXri:
  3746. case AArch64::ADDSXri:
  3747. case AArch64::SUBXri:
  3748. case AArch64::SUBSXri:
  3749. MaxEncoding = 0xfff;
  3750. ShiftSize = 12;
  3751. break;
  3752. case AArch64::ADDVL_XXI:
  3753. case AArch64::ADDPL_XXI:
  3754. MaxEncoding = 31;
  3755. ShiftSize = 0;
  3756. if (Offset < 0) {
  3757. MaxEncoding = 32;
  3758. Sign = -1;
  3759. Offset = -Offset;
  3760. }
  3761. break;
  3762. default:
  3763. llvm_unreachable("Unsupported opcode");
  3764. }
  3765. // FIXME: If the offset won't fit in 24-bits, compute the offset into a
  3766. // scratch register. If DestReg is a virtual register, use it as the
  3767. // scratch register; otherwise, create a new virtual register (to be
  3768. // replaced by the scavenger at the end of PEI). That case can be optimized
  3769. // slightly if DestReg is SP which is always 16-byte aligned, so the scratch
  3770. // register can be loaded with offset%8 and the add/sub can use an extending
  3771. // instruction with LSL#3.
  3772. // Currently the function handles any offsets but generates a poor sequence
  3773. // of code.
  3774. // assert(Offset < (1 << 24) && "unimplemented reg plus immediate");
  3775. const unsigned MaxEncodableValue = MaxEncoding << ShiftSize;
  3776. Register TmpReg = DestReg;
  3777. if (TmpReg == AArch64::XZR)
  3778. TmpReg = MBB.getParent()->getRegInfo().createVirtualRegister(
  3779. &AArch64::GPR64RegClass);
  3780. do {
  3781. uint64_t ThisVal = std::min<uint64_t>(Offset, MaxEncodableValue);
  3782. unsigned LocalShiftSize = 0;
  3783. if (ThisVal > MaxEncoding) {
  3784. ThisVal = ThisVal >> ShiftSize;
  3785. LocalShiftSize = ShiftSize;
  3786. }
  3787. assert((ThisVal >> ShiftSize) <= MaxEncoding &&
  3788. "Encoding cannot handle value that big");
  3789. Offset -= ThisVal << LocalShiftSize;
  3790. if (Offset == 0)
  3791. TmpReg = DestReg;
  3792. auto MBI = BuildMI(MBB, MBBI, DL, TII->get(Opc), TmpReg)
  3793. .addReg(SrcReg)
  3794. .addImm(Sign * (int)ThisVal);
  3795. if (ShiftSize)
  3796. MBI = MBI.addImm(
  3797. AArch64_AM::getShifterImm(AArch64_AM::LSL, LocalShiftSize));
  3798. MBI = MBI.setMIFlag(Flag);
  3799. if (NeedsWinCFI) {
  3800. assert(Sign == 1 && "SEH directives should always have a positive sign");
  3801. int Imm = (int)(ThisVal << LocalShiftSize);
  3802. if ((DestReg == AArch64::FP && SrcReg == AArch64::SP) ||
  3803. (SrcReg == AArch64::FP && DestReg == AArch64::SP)) {
  3804. if (HasWinCFI)
  3805. *HasWinCFI = true;
  3806. if (Imm == 0)
  3807. BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_SetFP)).setMIFlag(Flag);
  3808. else
  3809. BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_AddFP))
  3810. .addImm(Imm)
  3811. .setMIFlag(Flag);
  3812. assert(Offset == 0 && "Expected remaining offset to be zero to "
  3813. "emit a single SEH directive");
  3814. } else if (DestReg == AArch64::SP) {
  3815. if (HasWinCFI)
  3816. *HasWinCFI = true;
  3817. assert(SrcReg == AArch64::SP && "Unexpected SrcReg for SEH_StackAlloc");
  3818. BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_StackAlloc))
  3819. .addImm(Imm)
  3820. .setMIFlag(Flag);
  3821. }
  3822. if (HasWinCFI)
  3823. *HasWinCFI = true;
  3824. }
  3825. SrcReg = TmpReg;
  3826. } while (Offset);
  3827. }
  3828. void llvm::emitFrameOffset(MachineBasicBlock &MBB,
  3829. MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
  3830. unsigned DestReg, unsigned SrcReg,
  3831. StackOffset Offset, const TargetInstrInfo *TII,
  3832. MachineInstr::MIFlag Flag, bool SetNZCV,
  3833. bool NeedsWinCFI, bool *HasWinCFI) {
  3834. int64_t Bytes, NumPredicateVectors, NumDataVectors;
  3835. AArch64InstrInfo::decomposeStackOffsetForFrameOffsets(
  3836. Offset, Bytes, NumPredicateVectors, NumDataVectors);
  3837. // First emit non-scalable frame offsets, or a simple 'mov'.
  3838. if (Bytes || (!Offset && SrcReg != DestReg)) {
  3839. assert((DestReg != AArch64::SP || Bytes % 8 == 0) &&
  3840. "SP increment/decrement not 8-byte aligned");
  3841. unsigned Opc = SetNZCV ? AArch64::ADDSXri : AArch64::ADDXri;
  3842. if (Bytes < 0) {
  3843. Bytes = -Bytes;
  3844. Opc = SetNZCV ? AArch64::SUBSXri : AArch64::SUBXri;
  3845. }
  3846. emitFrameOffsetAdj(MBB, MBBI, DL, DestReg, SrcReg, Bytes, Opc, TII, Flag,
  3847. NeedsWinCFI, HasWinCFI);
  3848. SrcReg = DestReg;
  3849. }
  3850. assert(!(SetNZCV && (NumPredicateVectors || NumDataVectors)) &&
  3851. "SetNZCV not supported with SVE vectors");
  3852. assert(!(NeedsWinCFI && (NumPredicateVectors || NumDataVectors)) &&
  3853. "WinCFI not supported with SVE vectors");
  3854. if (NumDataVectors) {
  3855. emitFrameOffsetAdj(MBB, MBBI, DL, DestReg, SrcReg, NumDataVectors,
  3856. AArch64::ADDVL_XXI, TII, Flag, NeedsWinCFI, nullptr);
  3857. SrcReg = DestReg;
  3858. }
  3859. if (NumPredicateVectors) {
  3860. assert(DestReg != AArch64::SP && "Unaligned access to SP");
  3861. emitFrameOffsetAdj(MBB, MBBI, DL, DestReg, SrcReg, NumPredicateVectors,
  3862. AArch64::ADDPL_XXI, TII, Flag, NeedsWinCFI, nullptr);
  3863. }
  3864. }
  3865. MachineInstr *AArch64InstrInfo::foldMemoryOperandImpl(
  3866. MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
  3867. MachineBasicBlock::iterator InsertPt, int FrameIndex,
  3868. LiveIntervals *LIS, VirtRegMap *VRM) const {
  3869. // This is a bit of a hack. Consider this instruction:
  3870. //
  3871. // %0 = COPY %sp; GPR64all:%0
  3872. //
  3873. // We explicitly chose GPR64all for the virtual register so such a copy might
  3874. // be eliminated by RegisterCoalescer. However, that may not be possible, and
  3875. // %0 may even spill. We can't spill %sp, and since it is in the GPR64all
  3876. // register class, TargetInstrInfo::foldMemoryOperand() is going to try.
  3877. //
  3878. // To prevent that, we are going to constrain the %0 register class here.
  3879. //
  3880. // <rdar://problem/11522048>
  3881. //
  3882. if (MI.isFullCopy()) {
  3883. Register DstReg = MI.getOperand(0).getReg();
  3884. Register SrcReg = MI.getOperand(1).getReg();
  3885. if (SrcReg == AArch64::SP && Register::isVirtualRegister(DstReg)) {
  3886. MF.getRegInfo().constrainRegClass(DstReg, &AArch64::GPR64RegClass);
  3887. return nullptr;
  3888. }
  3889. if (DstReg == AArch64::SP && Register::isVirtualRegister(SrcReg)) {
  3890. MF.getRegInfo().constrainRegClass(SrcReg, &AArch64::GPR64RegClass);
  3891. return nullptr;
  3892. }
  3893. }
  3894. // Handle the case where a copy is being spilled or filled but the source
  3895. // and destination register class don't match. For example:
  3896. //
  3897. // %0 = COPY %xzr; GPR64common:%0
  3898. //
  3899. // In this case we can still safely fold away the COPY and generate the
  3900. // following spill code:
  3901. //
  3902. // STRXui %xzr, %stack.0
  3903. //
  3904. // This also eliminates spilled cross register class COPYs (e.g. between x and
  3905. // d regs) of the same size. For example:
  3906. //
  3907. // %0 = COPY %1; GPR64:%0, FPR64:%1
  3908. //
  3909. // will be filled as
  3910. //
  3911. // LDRDui %0, fi<#0>
  3912. //
  3913. // instead of
  3914. //
  3915. // LDRXui %Temp, fi<#0>
  3916. // %0 = FMOV %Temp
  3917. //
  3918. if (MI.isCopy() && Ops.size() == 1 &&
  3919. // Make sure we're only folding the explicit COPY defs/uses.
  3920. (Ops[0] == 0 || Ops[0] == 1)) {
  3921. bool IsSpill = Ops[0] == 0;
  3922. bool IsFill = !IsSpill;
  3923. const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
  3924. const MachineRegisterInfo &MRI = MF.getRegInfo();
  3925. MachineBasicBlock &MBB = *MI.getParent();
  3926. const MachineOperand &DstMO = MI.getOperand(0);
  3927. const MachineOperand &SrcMO = MI.getOperand(1);
  3928. Register DstReg = DstMO.getReg();
  3929. Register SrcReg = SrcMO.getReg();
  3930. // This is slightly expensive to compute for physical regs since
  3931. // getMinimalPhysRegClass is slow.
  3932. auto getRegClass = [&](unsigned Reg) {
  3933. return Register::isVirtualRegister(Reg) ? MRI.getRegClass(Reg)
  3934. : TRI.getMinimalPhysRegClass(Reg);
  3935. };
  3936. if (DstMO.getSubReg() == 0 && SrcMO.getSubReg() == 0) {
  3937. assert(TRI.getRegSizeInBits(*getRegClass(DstReg)) ==
  3938. TRI.getRegSizeInBits(*getRegClass(SrcReg)) &&
  3939. "Mismatched register size in non subreg COPY");
  3940. if (IsSpill)
  3941. storeRegToStackSlot(MBB, InsertPt, SrcReg, SrcMO.isKill(), FrameIndex,
  3942. getRegClass(SrcReg), &TRI);
  3943. else
  3944. loadRegFromStackSlot(MBB, InsertPt, DstReg, FrameIndex,
  3945. getRegClass(DstReg), &TRI);
  3946. return &*--InsertPt;
  3947. }
  3948. // Handle cases like spilling def of:
  3949. //
  3950. // %0:sub_32<def,read-undef> = COPY %wzr; GPR64common:%0
  3951. //
  3952. // where the physical register source can be widened and stored to the full
  3953. // virtual reg destination stack slot, in this case producing:
  3954. //
  3955. // STRXui %xzr, %stack.0
  3956. //
  3957. if (IsSpill && DstMO.isUndef() && Register::isPhysicalRegister(SrcReg)) {
  3958. assert(SrcMO.getSubReg() == 0 &&
  3959. "Unexpected subreg on physical register");
  3960. const TargetRegisterClass *SpillRC;
  3961. unsigned SpillSubreg;
  3962. switch (DstMO.getSubReg()) {
  3963. default:
  3964. SpillRC = nullptr;
  3965. break;
  3966. case AArch64::sub_32:
  3967. case AArch64::ssub:
  3968. if (AArch64::GPR32RegClass.contains(SrcReg)) {
  3969. SpillRC = &AArch64::GPR64RegClass;
  3970. SpillSubreg = AArch64::sub_32;
  3971. } else if (AArch64::FPR32RegClass.contains(SrcReg)) {
  3972. SpillRC = &AArch64::FPR64RegClass;
  3973. SpillSubreg = AArch64::ssub;
  3974. } else
  3975. SpillRC = nullptr;
  3976. break;
  3977. case AArch64::dsub:
  3978. if (AArch64::FPR64RegClass.contains(SrcReg)) {
  3979. SpillRC = &AArch64::FPR128RegClass;
  3980. SpillSubreg = AArch64::dsub;
  3981. } else
  3982. SpillRC = nullptr;
  3983. break;
  3984. }
  3985. if (SpillRC)
  3986. if (unsigned WidenedSrcReg =
  3987. TRI.getMatchingSuperReg(SrcReg, SpillSubreg, SpillRC)) {
  3988. storeRegToStackSlot(MBB, InsertPt, WidenedSrcReg, SrcMO.isKill(),
  3989. FrameIndex, SpillRC, &TRI);
  3990. return &*--InsertPt;
  3991. }
  3992. }
  3993. // Handle cases like filling use of:
  3994. //
  3995. // %0:sub_32<def,read-undef> = COPY %1; GPR64:%0, GPR32:%1
  3996. //
  3997. // where we can load the full virtual reg source stack slot, into the subreg
  3998. // destination, in this case producing:
  3999. //
  4000. // LDRWui %0:sub_32<def,read-undef>, %stack.0
  4001. //
  4002. if (IsFill && SrcMO.getSubReg() == 0 && DstMO.isUndef()) {
  4003. const TargetRegisterClass *FillRC;
  4004. switch (DstMO.getSubReg()) {
  4005. default:
  4006. FillRC = nullptr;
  4007. break;
  4008. case AArch64::sub_32:
  4009. FillRC = &AArch64::GPR32RegClass;
  4010. break;
  4011. case AArch64::ssub:
  4012. FillRC = &AArch64::FPR32RegClass;
  4013. break;
  4014. case AArch64::dsub:
  4015. FillRC = &AArch64::FPR64RegClass;
  4016. break;
  4017. }
  4018. if (FillRC) {
  4019. assert(TRI.getRegSizeInBits(*getRegClass(SrcReg)) ==
  4020. TRI.getRegSizeInBits(*FillRC) &&
  4021. "Mismatched regclass size on folded subreg COPY");
  4022. loadRegFromStackSlot(MBB, InsertPt, DstReg, FrameIndex, FillRC, &TRI);
  4023. MachineInstr &LoadMI = *--InsertPt;
  4024. MachineOperand &LoadDst = LoadMI.getOperand(0);
  4025. assert(LoadDst.getSubReg() == 0 && "unexpected subreg on fill load");
  4026. LoadDst.setSubReg(DstMO.getSubReg());
  4027. LoadDst.setIsUndef();
  4028. return &LoadMI;
  4029. }
  4030. }
  4031. }
  4032. // Cannot fold.
  4033. return nullptr;
  4034. }
  4035. int llvm::isAArch64FrameOffsetLegal(const MachineInstr &MI,
  4036. StackOffset &SOffset,
  4037. bool *OutUseUnscaledOp,
  4038. unsigned *OutUnscaledOp,
  4039. int64_t *EmittableOffset) {
  4040. // Set output values in case of early exit.
  4041. if (EmittableOffset)
  4042. *EmittableOffset = 0;
  4043. if (OutUseUnscaledOp)
  4044. *OutUseUnscaledOp = false;
  4045. if (OutUnscaledOp)
  4046. *OutUnscaledOp = 0;
  4047. // Exit early for structured vector spills/fills as they can't take an
  4048. // immediate offset.
  4049. switch (MI.getOpcode()) {
  4050. default:
  4051. break;
  4052. case AArch64::LD1Twov2d:
  4053. case AArch64::LD1Threev2d:
  4054. case AArch64::LD1Fourv2d:
  4055. case AArch64::LD1Twov1d:
  4056. case AArch64::LD1Threev1d:
  4057. case AArch64::LD1Fourv1d:
  4058. case AArch64::ST1Twov2d:
  4059. case AArch64::ST1Threev2d:
  4060. case AArch64::ST1Fourv2d:
  4061. case AArch64::ST1Twov1d:
  4062. case AArch64::ST1Threev1d:
  4063. case AArch64::ST1Fourv1d:
  4064. case AArch64::ST1i8:
  4065. case AArch64::ST1i16:
  4066. case AArch64::ST1i32:
  4067. case AArch64::ST1i64:
  4068. case AArch64::IRG:
  4069. case AArch64::IRGstack:
  4070. case AArch64::STGloop:
  4071. case AArch64::STZGloop:
  4072. return AArch64FrameOffsetCannotUpdate;
  4073. }
  4074. // Get the min/max offset and the scale.
  4075. TypeSize ScaleValue(0U, false);
  4076. unsigned Width;
  4077. int64_t MinOff, MaxOff;
  4078. if (!AArch64InstrInfo::getMemOpInfo(MI.getOpcode(), ScaleValue, Width, MinOff,
  4079. MaxOff))
  4080. llvm_unreachable("unhandled opcode in isAArch64FrameOffsetLegal");
  4081. // Construct the complete offset.
  4082. bool IsMulVL = ScaleValue.isScalable();
  4083. unsigned Scale = ScaleValue.getKnownMinSize();
  4084. int64_t Offset = IsMulVL ? SOffset.getScalable() : SOffset.getFixed();
  4085. const MachineOperand &ImmOpnd =
  4086. MI.getOperand(AArch64InstrInfo::getLoadStoreImmIdx(MI.getOpcode()));
  4087. Offset += ImmOpnd.getImm() * Scale;
  4088. // If the offset doesn't match the scale, we rewrite the instruction to
  4089. // use the unscaled instruction instead. Likewise, if we have a negative
  4090. // offset and there is an unscaled op to use.
  4091. Optional<unsigned> UnscaledOp =
  4092. AArch64InstrInfo::getUnscaledLdSt(MI.getOpcode());
  4093. bool useUnscaledOp = UnscaledOp && (Offset % Scale || Offset < 0);
  4094. if (useUnscaledOp &&
  4095. !AArch64InstrInfo::getMemOpInfo(*UnscaledOp, ScaleValue, Width, MinOff,
  4096. MaxOff))
  4097. llvm_unreachable("unhandled opcode in isAArch64FrameOffsetLegal");
  4098. Scale = ScaleValue.getKnownMinSize();
  4099. assert(IsMulVL == ScaleValue.isScalable() &&
  4100. "Unscaled opcode has different value for scalable");
  4101. int64_t Remainder = Offset % Scale;
  4102. assert(!(Remainder && useUnscaledOp) &&
  4103. "Cannot have remainder when using unscaled op");
  4104. assert(MinOff < MaxOff && "Unexpected Min/Max offsets");
  4105. int64_t NewOffset = Offset / Scale;
  4106. if (MinOff <= NewOffset && NewOffset <= MaxOff)
  4107. Offset = Remainder;
  4108. else {
  4109. NewOffset = NewOffset < 0 ? MinOff : MaxOff;
  4110. Offset = Offset - NewOffset * Scale + Remainder;
  4111. }
  4112. if (EmittableOffset)
  4113. *EmittableOffset = NewOffset;
  4114. if (OutUseUnscaledOp)
  4115. *OutUseUnscaledOp = useUnscaledOp;
  4116. if (OutUnscaledOp && UnscaledOp)
  4117. *OutUnscaledOp = *UnscaledOp;
  4118. if (IsMulVL)
  4119. SOffset = StackOffset::get(SOffset.getFixed(), Offset);
  4120. else
  4121. SOffset = StackOffset::get(Offset, SOffset.getScalable());
  4122. return AArch64FrameOffsetCanUpdate |
  4123. (SOffset ? 0 : AArch64FrameOffsetIsLegal);
  4124. }
  4125. bool llvm::rewriteAArch64FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
  4126. unsigned FrameReg, StackOffset &Offset,
  4127. const AArch64InstrInfo *TII) {
  4128. unsigned Opcode = MI.getOpcode();
  4129. unsigned ImmIdx = FrameRegIdx + 1;
  4130. if (Opcode == AArch64::ADDSXri || Opcode == AArch64::ADDXri) {
  4131. Offset += StackOffset::getFixed(MI.getOperand(ImmIdx).getImm());
  4132. emitFrameOffset(*MI.getParent(), MI, MI.getDebugLoc(),
  4133. MI.getOperand(0).getReg(), FrameReg, Offset, TII,
  4134. MachineInstr::NoFlags, (Opcode == AArch64::ADDSXri));
  4135. MI.eraseFromParent();
  4136. Offset = StackOffset();
  4137. return true;
  4138. }
  4139. int64_t NewOffset;
  4140. unsigned UnscaledOp;
  4141. bool UseUnscaledOp;
  4142. int Status = isAArch64FrameOffsetLegal(MI, Offset, &UseUnscaledOp,
  4143. &UnscaledOp, &NewOffset);
  4144. if (Status & AArch64FrameOffsetCanUpdate) {
  4145. if (Status & AArch64FrameOffsetIsLegal)
  4146. // Replace the FrameIndex with FrameReg.
  4147. MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
  4148. if (UseUnscaledOp)
  4149. MI.setDesc(TII->get(UnscaledOp));
  4150. MI.getOperand(ImmIdx).ChangeToImmediate(NewOffset);
  4151. return !Offset;
  4152. }
  4153. return false;
  4154. }
  4155. MCInst AArch64InstrInfo::getNop() const {
  4156. return MCInstBuilder(AArch64::HINT).addImm(0);
  4157. }
  4158. // AArch64 supports MachineCombiner.
  4159. bool AArch64InstrInfo::useMachineCombiner() const { return true; }
  4160. // True when Opc sets flag
  4161. static bool isCombineInstrSettingFlag(unsigned Opc) {
  4162. switch (Opc) {
  4163. case AArch64::ADDSWrr:
  4164. case AArch64::ADDSWri:
  4165. case AArch64::ADDSXrr:
  4166. case AArch64::ADDSXri:
  4167. case AArch64::SUBSWrr:
  4168. case AArch64::SUBSXrr:
  4169. // Note: MSUB Wd,Wn,Wm,Wi -> Wd = Wi - WnxWm, not Wd=WnxWm - Wi.
  4170. case AArch64::SUBSWri:
  4171. case AArch64::SUBSXri:
  4172. return true;
  4173. default:
  4174. break;
  4175. }
  4176. return false;
  4177. }
  4178. // 32b Opcodes that can be combined with a MUL
  4179. static bool isCombineInstrCandidate32(unsigned Opc) {
  4180. switch (Opc) {
  4181. case AArch64::ADDWrr:
  4182. case AArch64::ADDWri:
  4183. case AArch64::SUBWrr:
  4184. case AArch64::ADDSWrr:
  4185. case AArch64::ADDSWri:
  4186. case AArch64::SUBSWrr:
  4187. // Note: MSUB Wd,Wn,Wm,Wi -> Wd = Wi - WnxWm, not Wd=WnxWm - Wi.
  4188. case AArch64::SUBWri:
  4189. case AArch64::SUBSWri:
  4190. return true;
  4191. default:
  4192. break;
  4193. }
  4194. return false;
  4195. }
  4196. // 64b Opcodes that can be combined with a MUL
  4197. static bool isCombineInstrCandidate64(unsigned Opc) {
  4198. switch (Opc) {
  4199. case AArch64::ADDXrr:
  4200. case AArch64::ADDXri:
  4201. case AArch64::SUBXrr:
  4202. case AArch64::ADDSXrr:
  4203. case AArch64::ADDSXri:
  4204. case AArch64::SUBSXrr:
  4205. // Note: MSUB Wd,Wn,Wm,Wi -> Wd = Wi - WnxWm, not Wd=WnxWm - Wi.
  4206. case AArch64::SUBXri:
  4207. case AArch64::SUBSXri:
  4208. case AArch64::ADDv8i8:
  4209. case AArch64::ADDv16i8:
  4210. case AArch64::ADDv4i16:
  4211. case AArch64::ADDv8i16:
  4212. case AArch64::ADDv2i32:
  4213. case AArch64::ADDv4i32:
  4214. case AArch64::SUBv8i8:
  4215. case AArch64::SUBv16i8:
  4216. case AArch64::SUBv4i16:
  4217. case AArch64::SUBv8i16:
  4218. case AArch64::SUBv2i32:
  4219. case AArch64::SUBv4i32:
  4220. return true;
  4221. default:
  4222. break;
  4223. }
  4224. return false;
  4225. }
  4226. // FP Opcodes that can be combined with a FMUL.
  4227. static bool isCombineInstrCandidateFP(const MachineInstr &Inst) {
  4228. switch (Inst.getOpcode()) {
  4229. default:
  4230. break;
  4231. case AArch64::FADDHrr:
  4232. case AArch64::FADDSrr:
  4233. case AArch64::FADDDrr:
  4234. case AArch64::FADDv4f16:
  4235. case AArch64::FADDv8f16:
  4236. case AArch64::FADDv2f32:
  4237. case AArch64::FADDv2f64:
  4238. case AArch64::FADDv4f32:
  4239. case AArch64::FSUBHrr:
  4240. case AArch64::FSUBSrr:
  4241. case AArch64::FSUBDrr:
  4242. case AArch64::FSUBv4f16:
  4243. case AArch64::FSUBv8f16:
  4244. case AArch64::FSUBv2f32:
  4245. case AArch64::FSUBv2f64:
  4246. case AArch64::FSUBv4f32:
  4247. TargetOptions Options = Inst.getParent()->getParent()->getTarget().Options;
  4248. // We can fuse FADD/FSUB with FMUL, if fusion is either allowed globally by
  4249. // the target options or if FADD/FSUB has the contract fast-math flag.
  4250. return Options.UnsafeFPMath ||
  4251. Options.AllowFPOpFusion == FPOpFusion::Fast ||
  4252. Inst.getFlag(MachineInstr::FmContract);
  4253. return true;
  4254. }
  4255. return false;
  4256. }
  4257. // Opcodes that can be combined with a MUL
  4258. static bool isCombineInstrCandidate(unsigned Opc) {
  4259. return (isCombineInstrCandidate32(Opc) || isCombineInstrCandidate64(Opc));
  4260. }
  4261. //
  4262. // Utility routine that checks if \param MO is defined by an
  4263. // \param CombineOpc instruction in the basic block \param MBB
  4264. static bool canCombine(MachineBasicBlock &MBB, MachineOperand &MO,
  4265. unsigned CombineOpc, unsigned ZeroReg = 0,
  4266. bool CheckZeroReg = false) {
  4267. MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
  4268. MachineInstr *MI = nullptr;
  4269. if (MO.isReg() && Register::isVirtualRegister(MO.getReg()))
  4270. MI = MRI.getUniqueVRegDef(MO.getReg());
  4271. // And it needs to be in the trace (otherwise, it won't have a depth).
  4272. if (!MI || MI->getParent() != &MBB || (unsigned)MI->getOpcode() != CombineOpc)
  4273. return false;
  4274. // Must only used by the user we combine with.
  4275. if (!MRI.hasOneNonDBGUse(MI->getOperand(0).getReg()))
  4276. return false;
  4277. if (CheckZeroReg) {
  4278. assert(MI->getNumOperands() >= 4 && MI->getOperand(0).isReg() &&
  4279. MI->getOperand(1).isReg() && MI->getOperand(2).isReg() &&
  4280. MI->getOperand(3).isReg() && "MAdd/MSub must have a least 4 regs");
  4281. // The third input reg must be zero.
  4282. if (MI->getOperand(3).getReg() != ZeroReg)
  4283. return false;
  4284. }
  4285. return true;
  4286. }
  4287. //
  4288. // Is \param MO defined by an integer multiply and can be combined?
  4289. static bool canCombineWithMUL(MachineBasicBlock &MBB, MachineOperand &MO,
  4290. unsigned MulOpc, unsigned ZeroReg) {
  4291. return canCombine(MBB, MO, MulOpc, ZeroReg, true);
  4292. }
  4293. //
  4294. // Is \param MO defined by a floating-point multiply and can be combined?
  4295. static bool canCombineWithFMUL(MachineBasicBlock &MBB, MachineOperand &MO,
  4296. unsigned MulOpc) {
  4297. return canCombine(MBB, MO, MulOpc);
  4298. }
  4299. // TODO: There are many more machine instruction opcodes to match:
  4300. // 1. Other data types (integer, vectors)
  4301. // 2. Other math / logic operations (xor, or)
  4302. // 3. Other forms of the same operation (intrinsics and other variants)
  4303. bool AArch64InstrInfo::isAssociativeAndCommutative(
  4304. const MachineInstr &Inst) const {
  4305. switch (Inst.getOpcode()) {
  4306. case AArch64::FADDDrr:
  4307. case AArch64::FADDSrr:
  4308. case AArch64::FADDv2f32:
  4309. case AArch64::FADDv2f64:
  4310. case AArch64::FADDv4f32:
  4311. case AArch64::FMULDrr:
  4312. case AArch64::FMULSrr:
  4313. case AArch64::FMULX32:
  4314. case AArch64::FMULX64:
  4315. case AArch64::FMULXv2f32:
  4316. case AArch64::FMULXv2f64:
  4317. case AArch64::FMULXv4f32:
  4318. case AArch64::FMULv2f32:
  4319. case AArch64::FMULv2f64:
  4320. case AArch64::FMULv4f32:
  4321. return Inst.getParent()->getParent()->getTarget().Options.UnsafeFPMath;
  4322. default:
  4323. return false;
  4324. }
  4325. }
  4326. /// Find instructions that can be turned into madd.
  4327. static bool getMaddPatterns(MachineInstr &Root,
  4328. SmallVectorImpl<MachineCombinerPattern> &Patterns) {
  4329. unsigned Opc = Root.getOpcode();
  4330. MachineBasicBlock &MBB = *Root.getParent();
  4331. bool Found = false;
  4332. if (!isCombineInstrCandidate(Opc))
  4333. return false;
  4334. if (isCombineInstrSettingFlag(Opc)) {
  4335. int Cmp_NZCV = Root.findRegisterDefOperandIdx(AArch64::NZCV, true);
  4336. // When NZCV is live bail out.
  4337. if (Cmp_NZCV == -1)
  4338. return false;
  4339. unsigned NewOpc = convertToNonFlagSettingOpc(Root);
  4340. // When opcode can't change bail out.
  4341. // CHECKME: do we miss any cases for opcode conversion?
  4342. if (NewOpc == Opc)
  4343. return false;
  4344. Opc = NewOpc;
  4345. }
  4346. auto setFound = [&](int Opcode, int Operand, unsigned ZeroReg,
  4347. MachineCombinerPattern Pattern) {
  4348. if (canCombineWithMUL(MBB, Root.getOperand(Operand), Opcode, ZeroReg)) {
  4349. Patterns.push_back(Pattern);
  4350. Found = true;
  4351. }
  4352. };
  4353. auto setVFound = [&](int Opcode, int Operand, MachineCombinerPattern Pattern) {
  4354. if (canCombine(MBB, Root.getOperand(Operand), Opcode)) {
  4355. Patterns.push_back(Pattern);
  4356. Found = true;
  4357. }
  4358. };
  4359. typedef MachineCombinerPattern MCP;
  4360. switch (Opc) {
  4361. default:
  4362. break;
  4363. case AArch64::ADDWrr:
  4364. assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
  4365. "ADDWrr does not have register operands");
  4366. setFound(AArch64::MADDWrrr, 1, AArch64::WZR, MCP::MULADDW_OP1);
  4367. setFound(AArch64::MADDWrrr, 2, AArch64::WZR, MCP::MULADDW_OP2);
  4368. break;
  4369. case AArch64::ADDXrr:
  4370. setFound(AArch64::MADDXrrr, 1, AArch64::XZR, MCP::MULADDX_OP1);
  4371. setFound(AArch64::MADDXrrr, 2, AArch64::XZR, MCP::MULADDX_OP2);
  4372. break;
  4373. case AArch64::SUBWrr:
  4374. setFound(AArch64::MADDWrrr, 1, AArch64::WZR, MCP::MULSUBW_OP1);
  4375. setFound(AArch64::MADDWrrr, 2, AArch64::WZR, MCP::MULSUBW_OP2);
  4376. break;
  4377. case AArch64::SUBXrr:
  4378. setFound(AArch64::MADDXrrr, 1, AArch64::XZR, MCP::MULSUBX_OP1);
  4379. setFound(AArch64::MADDXrrr, 2, AArch64::XZR, MCP::MULSUBX_OP2);
  4380. break;
  4381. case AArch64::ADDWri:
  4382. setFound(AArch64::MADDWrrr, 1, AArch64::WZR, MCP::MULADDWI_OP1);
  4383. break;
  4384. case AArch64::ADDXri:
  4385. setFound(AArch64::MADDXrrr, 1, AArch64::XZR, MCP::MULADDXI_OP1);
  4386. break;
  4387. case AArch64::SUBWri:
  4388. setFound(AArch64::MADDWrrr, 1, AArch64::WZR, MCP::MULSUBWI_OP1);
  4389. break;
  4390. case AArch64::SUBXri:
  4391. setFound(AArch64::MADDXrrr, 1, AArch64::XZR, MCP::MULSUBXI_OP1);
  4392. break;
  4393. case AArch64::ADDv8i8:
  4394. setVFound(AArch64::MULv8i8, 1, MCP::MULADDv8i8_OP1);
  4395. setVFound(AArch64::MULv8i8, 2, MCP::MULADDv8i8_OP2);
  4396. break;
  4397. case AArch64::ADDv16i8:
  4398. setVFound(AArch64::MULv16i8, 1, MCP::MULADDv16i8_OP1);
  4399. setVFound(AArch64::MULv16i8, 2, MCP::MULADDv16i8_OP2);
  4400. break;
  4401. case AArch64::ADDv4i16:
  4402. setVFound(AArch64::MULv4i16, 1, MCP::MULADDv4i16_OP1);
  4403. setVFound(AArch64::MULv4i16, 2, MCP::MULADDv4i16_OP2);
  4404. setVFound(AArch64::MULv4i16_indexed, 1, MCP::MULADDv4i16_indexed_OP1);
  4405. setVFound(AArch64::MULv4i16_indexed, 2, MCP::MULADDv4i16_indexed_OP2);
  4406. break;
  4407. case AArch64::ADDv8i16:
  4408. setVFound(AArch64::MULv8i16, 1, MCP::MULADDv8i16_OP1);
  4409. setVFound(AArch64::MULv8i16, 2, MCP::MULADDv8i16_OP2);
  4410. setVFound(AArch64::MULv8i16_indexed, 1, MCP::MULADDv8i16_indexed_OP1);
  4411. setVFound(AArch64::MULv8i16_indexed, 2, MCP::MULADDv8i16_indexed_OP2);
  4412. break;
  4413. case AArch64::ADDv2i32:
  4414. setVFound(AArch64::MULv2i32, 1, MCP::MULADDv2i32_OP1);
  4415. setVFound(AArch64::MULv2i32, 2, MCP::MULADDv2i32_OP2);
  4416. setVFound(AArch64::MULv2i32_indexed, 1, MCP::MULADDv2i32_indexed_OP1);
  4417. setVFound(AArch64::MULv2i32_indexed, 2, MCP::MULADDv2i32_indexed_OP2);
  4418. break;
  4419. case AArch64::ADDv4i32:
  4420. setVFound(AArch64::MULv4i32, 1, MCP::MULADDv4i32_OP1);
  4421. setVFound(AArch64::MULv4i32, 2, MCP::MULADDv4i32_OP2);
  4422. setVFound(AArch64::MULv4i32_indexed, 1, MCP::MULADDv4i32_indexed_OP1);
  4423. setVFound(AArch64::MULv4i32_indexed, 2, MCP::MULADDv4i32_indexed_OP2);
  4424. break;
  4425. case AArch64::SUBv8i8:
  4426. setVFound(AArch64::MULv8i8, 1, MCP::MULSUBv8i8_OP1);
  4427. setVFound(AArch64::MULv8i8, 2, MCP::MULSUBv8i8_OP2);
  4428. break;
  4429. case AArch64::SUBv16i8:
  4430. setVFound(AArch64::MULv16i8, 1, MCP::MULSUBv16i8_OP1);
  4431. setVFound(AArch64::MULv16i8, 2, MCP::MULSUBv16i8_OP2);
  4432. break;
  4433. case AArch64::SUBv4i16:
  4434. setVFound(AArch64::MULv4i16, 1, MCP::MULSUBv4i16_OP1);
  4435. setVFound(AArch64::MULv4i16, 2, MCP::MULSUBv4i16_OP2);
  4436. setVFound(AArch64::MULv4i16_indexed, 1, MCP::MULSUBv4i16_indexed_OP1);
  4437. setVFound(AArch64::MULv4i16_indexed, 2, MCP::MULSUBv4i16_indexed_OP2);
  4438. break;
  4439. case AArch64::SUBv8i16:
  4440. setVFound(AArch64::MULv8i16, 1, MCP::MULSUBv8i16_OP1);
  4441. setVFound(AArch64::MULv8i16, 2, MCP::MULSUBv8i16_OP2);
  4442. setVFound(AArch64::MULv8i16_indexed, 1, MCP::MULSUBv8i16_indexed_OP1);
  4443. setVFound(AArch64::MULv8i16_indexed, 2, MCP::MULSUBv8i16_indexed_OP2);
  4444. break;
  4445. case AArch64::SUBv2i32:
  4446. setVFound(AArch64::MULv2i32, 1, MCP::MULSUBv2i32_OP1);
  4447. setVFound(AArch64::MULv2i32, 2, MCP::MULSUBv2i32_OP2);
  4448. setVFound(AArch64::MULv2i32_indexed, 1, MCP::MULSUBv2i32_indexed_OP1);
  4449. setVFound(AArch64::MULv2i32_indexed, 2, MCP::MULSUBv2i32_indexed_OP2);
  4450. break;
  4451. case AArch64::SUBv4i32:
  4452. setVFound(AArch64::MULv4i32, 1, MCP::MULSUBv4i32_OP1);
  4453. setVFound(AArch64::MULv4i32, 2, MCP::MULSUBv4i32_OP2);
  4454. setVFound(AArch64::MULv4i32_indexed, 1, MCP::MULSUBv4i32_indexed_OP1);
  4455. setVFound(AArch64::MULv4i32_indexed, 2, MCP::MULSUBv4i32_indexed_OP2);
  4456. break;
  4457. }
  4458. return Found;
  4459. }
  4460. /// Floating-Point Support
  4461. /// Find instructions that can be turned into madd.
  4462. static bool getFMAPatterns(MachineInstr &Root,
  4463. SmallVectorImpl<MachineCombinerPattern> &Patterns) {
  4464. if (!isCombineInstrCandidateFP(Root))
  4465. return false;
  4466. MachineBasicBlock &MBB = *Root.getParent();
  4467. bool Found = false;
  4468. auto Match = [&](int Opcode, int Operand,
  4469. MachineCombinerPattern Pattern) -> bool {
  4470. if (canCombineWithFMUL(MBB, Root.getOperand(Operand), Opcode)) {
  4471. Patterns.push_back(Pattern);
  4472. return true;
  4473. }
  4474. return false;
  4475. };
  4476. typedef MachineCombinerPattern MCP;
  4477. switch (Root.getOpcode()) {
  4478. default:
  4479. assert(false && "Unsupported FP instruction in combiner\n");
  4480. break;
  4481. case AArch64::FADDHrr:
  4482. assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
  4483. "FADDHrr does not have register operands");
  4484. Found = Match(AArch64::FMULHrr, 1, MCP::FMULADDH_OP1);
  4485. Found |= Match(AArch64::FMULHrr, 2, MCP::FMULADDH_OP2);
  4486. break;
  4487. case AArch64::FADDSrr:
  4488. assert(Root.getOperand(1).isReg() && Root.getOperand(2).isReg() &&
  4489. "FADDSrr does not have register operands");
  4490. Found |= Match(AArch64::FMULSrr, 1, MCP::FMULADDS_OP1) ||
  4491. Match(AArch64::FMULv1i32_indexed, 1, MCP::FMLAv1i32_indexed_OP1);
  4492. Found |= Match(AArch64::FMULSrr, 2, MCP::FMULADDS_OP2) ||
  4493. Match(AArch64::FMULv1i32_indexed, 2, MCP::FMLAv1i32_indexed_OP2);
  4494. break;
  4495. case AArch64::FADDDrr:
  4496. Found |= Match(AArch64::FMULDrr, 1, MCP::FMULADDD_OP1) ||
  4497. Match(AArch64::FMULv1i64_indexed, 1, MCP::FMLAv1i64_indexed_OP1);
  4498. Found |= Match(AArch64::FMULDrr, 2, MCP::FMULADDD_OP2) ||
  4499. Match(AArch64::FMULv1i64_indexed, 2, MCP::FMLAv1i64_indexed_OP2);
  4500. break;
  4501. case AArch64::FADDv4f16:
  4502. Found |= Match(AArch64::FMULv4i16_indexed, 1, MCP::FMLAv4i16_indexed_OP1) ||
  4503. Match(AArch64::FMULv4f16, 1, MCP::FMLAv4f16_OP1);
  4504. Found |= Match(AArch64::FMULv4i16_indexed, 2, MCP::FMLAv4i16_indexed_OP2) ||
  4505. Match(AArch64::FMULv4f16, 2, MCP::FMLAv4f16_OP2);
  4506. break;
  4507. case AArch64::FADDv8f16:
  4508. Found |= Match(AArch64::FMULv8i16_indexed, 1, MCP::FMLAv8i16_indexed_OP1) ||
  4509. Match(AArch64::FMULv8f16, 1, MCP::FMLAv8f16_OP1);
  4510. Found |= Match(AArch64::FMULv8i16_indexed, 2, MCP::FMLAv8i16_indexed_OP2) ||
  4511. Match(AArch64::FMULv8f16, 2, MCP::FMLAv8f16_OP2);
  4512. break;
  4513. case AArch64::FADDv2f32:
  4514. Found |= Match(AArch64::FMULv2i32_indexed, 1, MCP::FMLAv2i32_indexed_OP1) ||
  4515. Match(AArch64::FMULv2f32, 1, MCP::FMLAv2f32_OP1);
  4516. Found |= Match(AArch64::FMULv2i32_indexed, 2, MCP::FMLAv2i32_indexed_OP2) ||
  4517. Match(AArch64::FMULv2f32, 2, MCP::FMLAv2f32_OP2);
  4518. break;
  4519. case AArch64::FADDv2f64:
  4520. Found |= Match(AArch64::FMULv2i64_indexed, 1, MCP::FMLAv2i64_indexed_OP1) ||
  4521. Match(AArch64::FMULv2f64, 1, MCP::FMLAv2f64_OP1);
  4522. Found |= Match(AArch64::FMULv2i64_indexed, 2, MCP::FMLAv2i64_indexed_OP2) ||
  4523. Match(AArch64::FMULv2f64, 2, MCP::FMLAv2f64_OP2);
  4524. break;
  4525. case AArch64::FADDv4f32:
  4526. Found |= Match(AArch64::FMULv4i32_indexed, 1, MCP::FMLAv4i32_indexed_OP1) ||
  4527. Match(AArch64::FMULv4f32, 1, MCP::FMLAv4f32_OP1);
  4528. Found |= Match(AArch64::FMULv4i32_indexed, 2, MCP::FMLAv4i32_indexed_OP2) ||
  4529. Match(AArch64::FMULv4f32, 2, MCP::FMLAv4f32_OP2);
  4530. break;
  4531. case AArch64::FSUBHrr:
  4532. Found = Match(AArch64::FMULHrr, 1, MCP::FMULSUBH_OP1);
  4533. Found |= Match(AArch64::FMULHrr, 2, MCP::FMULSUBH_OP2);
  4534. Found |= Match(AArch64::FNMULHrr, 1, MCP::FNMULSUBH_OP1);
  4535. break;
  4536. case AArch64::FSUBSrr:
  4537. Found = Match(AArch64::FMULSrr, 1, MCP::FMULSUBS_OP1);
  4538. Found |= Match(AArch64::FMULSrr, 2, MCP::FMULSUBS_OP2) ||
  4539. Match(AArch64::FMULv1i32_indexed, 2, MCP::FMLSv1i32_indexed_OP2);
  4540. Found |= Match(AArch64::FNMULSrr, 1, MCP::FNMULSUBS_OP1);
  4541. break;
  4542. case AArch64::FSUBDrr:
  4543. Found = Match(AArch64::FMULDrr, 1, MCP::FMULSUBD_OP1);
  4544. Found |= Match(AArch64::FMULDrr, 2, MCP::FMULSUBD_OP2) ||
  4545. Match(AArch64::FMULv1i64_indexed, 2, MCP::FMLSv1i64_indexed_OP2);
  4546. Found |= Match(AArch64::FNMULDrr, 1, MCP::FNMULSUBD_OP1);
  4547. break;
  4548. case AArch64::FSUBv4f16:
  4549. Found |= Match(AArch64::FMULv4i16_indexed, 2, MCP::FMLSv4i16_indexed_OP2) ||
  4550. Match(AArch64::FMULv4f16, 2, MCP::FMLSv4f16_OP2);
  4551. Found |= Match(AArch64::FMULv4i16_indexed, 1, MCP::FMLSv4i16_indexed_OP1) ||
  4552. Match(AArch64::FMULv4f16, 1, MCP::FMLSv4f16_OP1);
  4553. break;
  4554. case AArch64::FSUBv8f16:
  4555. Found |= Match(AArch64::FMULv8i16_indexed, 2, MCP::FMLSv8i16_indexed_OP2) ||
  4556. Match(AArch64::FMULv8f16, 2, MCP::FMLSv8f16_OP2);
  4557. Found |= Match(AArch64::FMULv8i16_indexed, 1, MCP::FMLSv8i16_indexed_OP1) ||
  4558. Match(AArch64::FMULv8f16, 1, MCP::FMLSv8f16_OP1);
  4559. break;
  4560. case AArch64::FSUBv2f32:
  4561. Found |= Match(AArch64::FMULv2i32_indexed, 2, MCP::FMLSv2i32_indexed_OP2) ||
  4562. Match(AArch64::FMULv2f32, 2, MCP::FMLSv2f32_OP2);
  4563. Found |= Match(AArch64::FMULv2i32_indexed, 1, MCP::FMLSv2i32_indexed_OP1) ||
  4564. Match(AArch64::FMULv2f32, 1, MCP::FMLSv2f32_OP1);
  4565. break;
  4566. case AArch64::FSUBv2f64:
  4567. Found |= Match(AArch64::FMULv2i64_indexed, 2, MCP::FMLSv2i64_indexed_OP2) ||
  4568. Match(AArch64::FMULv2f64, 2, MCP::FMLSv2f64_OP2);
  4569. Found |= Match(AArch64::FMULv2i64_indexed, 1, MCP::FMLSv2i64_indexed_OP1) ||
  4570. Match(AArch64::FMULv2f64, 1, MCP::FMLSv2f64_OP1);
  4571. break;
  4572. case AArch64::FSUBv4f32:
  4573. Found |= Match(AArch64::FMULv4i32_indexed, 2, MCP::FMLSv4i32_indexed_OP2) ||
  4574. Match(AArch64::FMULv4f32, 2, MCP::FMLSv4f32_OP2);
  4575. Found |= Match(AArch64::FMULv4i32_indexed, 1, MCP::FMLSv4i32_indexed_OP1) ||
  4576. Match(AArch64::FMULv4f32, 1, MCP::FMLSv4f32_OP1);
  4577. break;
  4578. }
  4579. return Found;
  4580. }
  4581. static bool getFMULPatterns(MachineInstr &Root,
  4582. SmallVectorImpl<MachineCombinerPattern> &Patterns) {
  4583. MachineBasicBlock &MBB = *Root.getParent();
  4584. bool Found = false;
  4585. auto Match = [&](unsigned Opcode, int Operand,
  4586. MachineCombinerPattern Pattern) -> bool {
  4587. MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
  4588. MachineOperand &MO = Root.getOperand(Operand);
  4589. MachineInstr *MI = nullptr;
  4590. if (MO.isReg() && Register::isVirtualRegister(MO.getReg()))
  4591. MI = MRI.getUniqueVRegDef(MO.getReg());
  4592. if (MI && MI->getOpcode() == Opcode) {
  4593. Patterns.push_back(Pattern);
  4594. return true;
  4595. }
  4596. return false;
  4597. };
  4598. typedef MachineCombinerPattern MCP;
  4599. switch (Root.getOpcode()) {
  4600. default:
  4601. return false;
  4602. case AArch64::FMULv2f32:
  4603. Found = Match(AArch64::DUPv2i32lane, 1, MCP::FMULv2i32_indexed_OP1);
  4604. Found |= Match(AArch64::DUPv2i32lane, 2, MCP::FMULv2i32_indexed_OP2);
  4605. break;
  4606. case AArch64::FMULv2f64:
  4607. Found = Match(AArch64::DUPv2i64lane, 1, MCP::FMULv2i64_indexed_OP1);
  4608. Found |= Match(AArch64::DUPv2i64lane, 2, MCP::FMULv2i64_indexed_OP2);
  4609. break;
  4610. case AArch64::FMULv4f16:
  4611. Found = Match(AArch64::DUPv4i16lane, 1, MCP::FMULv4i16_indexed_OP1);
  4612. Found |= Match(AArch64::DUPv4i16lane, 2, MCP::FMULv4i16_indexed_OP2);
  4613. break;
  4614. case AArch64::FMULv4f32:
  4615. Found = Match(AArch64::DUPv4i32lane, 1, MCP::FMULv4i32_indexed_OP1);
  4616. Found |= Match(AArch64::DUPv4i32lane, 2, MCP::FMULv4i32_indexed_OP2);
  4617. break;
  4618. case AArch64::FMULv8f16:
  4619. Found = Match(AArch64::DUPv8i16lane, 1, MCP::FMULv8i16_indexed_OP1);
  4620. Found |= Match(AArch64::DUPv8i16lane, 2, MCP::FMULv8i16_indexed_OP2);
  4621. break;
  4622. }
  4623. return Found;
  4624. }
  4625. /// Return true when a code sequence can improve throughput. It
  4626. /// should be called only for instructions in loops.
  4627. /// \param Pattern - combiner pattern
  4628. bool AArch64InstrInfo::isThroughputPattern(
  4629. MachineCombinerPattern Pattern) const {
  4630. switch (Pattern) {
  4631. default:
  4632. break;
  4633. case MachineCombinerPattern::FMULADDH_OP1:
  4634. case MachineCombinerPattern::FMULADDH_OP2:
  4635. case MachineCombinerPattern::FMULSUBH_OP1:
  4636. case MachineCombinerPattern::FMULSUBH_OP2:
  4637. case MachineCombinerPattern::FMULADDS_OP1:
  4638. case MachineCombinerPattern::FMULADDS_OP2:
  4639. case MachineCombinerPattern::FMULSUBS_OP1:
  4640. case MachineCombinerPattern::FMULSUBS_OP2:
  4641. case MachineCombinerPattern::FMULADDD_OP1:
  4642. case MachineCombinerPattern::FMULADDD_OP2:
  4643. case MachineCombinerPattern::FMULSUBD_OP1:
  4644. case MachineCombinerPattern::FMULSUBD_OP2:
  4645. case MachineCombinerPattern::FNMULSUBH_OP1:
  4646. case MachineCombinerPattern::FNMULSUBS_OP1:
  4647. case MachineCombinerPattern::FNMULSUBD_OP1:
  4648. case MachineCombinerPattern::FMLAv4i16_indexed_OP1:
  4649. case MachineCombinerPattern::FMLAv4i16_indexed_OP2:
  4650. case MachineCombinerPattern::FMLAv8i16_indexed_OP1:
  4651. case MachineCombinerPattern::FMLAv8i16_indexed_OP2:
  4652. case MachineCombinerPattern::FMLAv1i32_indexed_OP1:
  4653. case MachineCombinerPattern::FMLAv1i32_indexed_OP2:
  4654. case MachineCombinerPattern::FMLAv1i64_indexed_OP1:
  4655. case MachineCombinerPattern::FMLAv1i64_indexed_OP2:
  4656. case MachineCombinerPattern::FMLAv4f16_OP2:
  4657. case MachineCombinerPattern::FMLAv4f16_OP1:
  4658. case MachineCombinerPattern::FMLAv8f16_OP1:
  4659. case MachineCombinerPattern::FMLAv8f16_OP2:
  4660. case MachineCombinerPattern::FMLAv2f32_OP2:
  4661. case MachineCombinerPattern::FMLAv2f32_OP1:
  4662. case MachineCombinerPattern::FMLAv2f64_OP1:
  4663. case MachineCombinerPattern::FMLAv2f64_OP2:
  4664. case MachineCombinerPattern::FMLAv2i32_indexed_OP1:
  4665. case MachineCombinerPattern::FMLAv2i32_indexed_OP2:
  4666. case MachineCombinerPattern::FMLAv2i64_indexed_OP1:
  4667. case MachineCombinerPattern::FMLAv2i64_indexed_OP2:
  4668. case MachineCombinerPattern::FMLAv4f32_OP1:
  4669. case MachineCombinerPattern::FMLAv4f32_OP2:
  4670. case MachineCombinerPattern::FMLAv4i32_indexed_OP1:
  4671. case MachineCombinerPattern::FMLAv4i32_indexed_OP2:
  4672. case MachineCombinerPattern::FMLSv4i16_indexed_OP1:
  4673. case MachineCombinerPattern::FMLSv4i16_indexed_OP2:
  4674. case MachineCombinerPattern::FMLSv8i16_indexed_OP1:
  4675. case MachineCombinerPattern::FMLSv8i16_indexed_OP2:
  4676. case MachineCombinerPattern::FMLSv1i32_indexed_OP2:
  4677. case MachineCombinerPattern::FMLSv1i64_indexed_OP2:
  4678. case MachineCombinerPattern::FMLSv2i32_indexed_OP2:
  4679. case MachineCombinerPattern::FMLSv2i64_indexed_OP2:
  4680. case MachineCombinerPattern::FMLSv4f16_OP1:
  4681. case MachineCombinerPattern::FMLSv4f16_OP2:
  4682. case MachineCombinerPattern::FMLSv8f16_OP1:
  4683. case MachineCombinerPattern::FMLSv8f16_OP2:
  4684. case MachineCombinerPattern::FMLSv2f32_OP2:
  4685. case MachineCombinerPattern::FMLSv2f64_OP2:
  4686. case MachineCombinerPattern::FMLSv4i32_indexed_OP2:
  4687. case MachineCombinerPattern::FMLSv4f32_OP2:
  4688. case MachineCombinerPattern::FMULv2i32_indexed_OP1:
  4689. case MachineCombinerPattern::FMULv2i32_indexed_OP2:
  4690. case MachineCombinerPattern::FMULv2i64_indexed_OP1:
  4691. case MachineCombinerPattern::FMULv2i64_indexed_OP2:
  4692. case MachineCombinerPattern::FMULv4i16_indexed_OP1:
  4693. case MachineCombinerPattern::FMULv4i16_indexed_OP2:
  4694. case MachineCombinerPattern::FMULv4i32_indexed_OP1:
  4695. case MachineCombinerPattern::FMULv4i32_indexed_OP2:
  4696. case MachineCombinerPattern::FMULv8i16_indexed_OP1:
  4697. case MachineCombinerPattern::FMULv8i16_indexed_OP2:
  4698. case MachineCombinerPattern::MULADDv8i8_OP1:
  4699. case MachineCombinerPattern::MULADDv8i8_OP2:
  4700. case MachineCombinerPattern::MULADDv16i8_OP1:
  4701. case MachineCombinerPattern::MULADDv16i8_OP2:
  4702. case MachineCombinerPattern::MULADDv4i16_OP1:
  4703. case MachineCombinerPattern::MULADDv4i16_OP2:
  4704. case MachineCombinerPattern::MULADDv8i16_OP1:
  4705. case MachineCombinerPattern::MULADDv8i16_OP2:
  4706. case MachineCombinerPattern::MULADDv2i32_OP1:
  4707. case MachineCombinerPattern::MULADDv2i32_OP2:
  4708. case MachineCombinerPattern::MULADDv4i32_OP1:
  4709. case MachineCombinerPattern::MULADDv4i32_OP2:
  4710. case MachineCombinerPattern::MULSUBv8i8_OP1:
  4711. case MachineCombinerPattern::MULSUBv8i8_OP2:
  4712. case MachineCombinerPattern::MULSUBv16i8_OP1:
  4713. case MachineCombinerPattern::MULSUBv16i8_OP2:
  4714. case MachineCombinerPattern::MULSUBv4i16_OP1:
  4715. case MachineCombinerPattern::MULSUBv4i16_OP2:
  4716. case MachineCombinerPattern::MULSUBv8i16_OP1:
  4717. case MachineCombinerPattern::MULSUBv8i16_OP2:
  4718. case MachineCombinerPattern::MULSUBv2i32_OP1:
  4719. case MachineCombinerPattern::MULSUBv2i32_OP2:
  4720. case MachineCombinerPattern::MULSUBv4i32_OP1:
  4721. case MachineCombinerPattern::MULSUBv4i32_OP2:
  4722. case MachineCombinerPattern::MULADDv4i16_indexed_OP1:
  4723. case MachineCombinerPattern::MULADDv4i16_indexed_OP2:
  4724. case MachineCombinerPattern::MULADDv8i16_indexed_OP1:
  4725. case MachineCombinerPattern::MULADDv8i16_indexed_OP2:
  4726. case MachineCombinerPattern::MULADDv2i32_indexed_OP1:
  4727. case MachineCombinerPattern::MULADDv2i32_indexed_OP2:
  4728. case MachineCombinerPattern::MULADDv4i32_indexed_OP1:
  4729. case MachineCombinerPattern::MULADDv4i32_indexed_OP2:
  4730. case MachineCombinerPattern::MULSUBv4i16_indexed_OP1:
  4731. case MachineCombinerPattern::MULSUBv4i16_indexed_OP2:
  4732. case MachineCombinerPattern::MULSUBv8i16_indexed_OP1:
  4733. case MachineCombinerPattern::MULSUBv8i16_indexed_OP2:
  4734. case MachineCombinerPattern::MULSUBv2i32_indexed_OP1:
  4735. case MachineCombinerPattern::MULSUBv2i32_indexed_OP2:
  4736. case MachineCombinerPattern::MULSUBv4i32_indexed_OP1:
  4737. case MachineCombinerPattern::MULSUBv4i32_indexed_OP2:
  4738. return true;
  4739. } // end switch (Pattern)
  4740. return false;
  4741. }
  4742. /// Return true when there is potentially a faster code sequence for an
  4743. /// instruction chain ending in \p Root. All potential patterns are listed in
  4744. /// the \p Pattern vector. Pattern should be sorted in priority order since the
  4745. /// pattern evaluator stops checking as soon as it finds a faster sequence.
  4746. bool AArch64InstrInfo::getMachineCombinerPatterns(
  4747. MachineInstr &Root, SmallVectorImpl<MachineCombinerPattern> &Patterns,
  4748. bool DoRegPressureReduce) const {
  4749. // Integer patterns
  4750. if (getMaddPatterns(Root, Patterns))
  4751. return true;
  4752. // Floating point patterns
  4753. if (getFMULPatterns(Root, Patterns))
  4754. return true;
  4755. if (getFMAPatterns(Root, Patterns))
  4756. return true;
  4757. return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns,
  4758. DoRegPressureReduce);
  4759. }
  4760. enum class FMAInstKind { Default, Indexed, Accumulator };
  4761. /// genFusedMultiply - Generate fused multiply instructions.
  4762. /// This function supports both integer and floating point instructions.
  4763. /// A typical example:
  4764. /// F|MUL I=A,B,0
  4765. /// F|ADD R,I,C
  4766. /// ==> F|MADD R,A,B,C
  4767. /// \param MF Containing MachineFunction
  4768. /// \param MRI Register information
  4769. /// \param TII Target information
  4770. /// \param Root is the F|ADD instruction
  4771. /// \param [out] InsInstrs is a vector of machine instructions and will
  4772. /// contain the generated madd instruction
  4773. /// \param IdxMulOpd is index of operand in Root that is the result of
  4774. /// the F|MUL. In the example above IdxMulOpd is 1.
  4775. /// \param MaddOpc the opcode fo the f|madd instruction
  4776. /// \param RC Register class of operands
  4777. /// \param kind of fma instruction (addressing mode) to be generated
  4778. /// \param ReplacedAddend is the result register from the instruction
  4779. /// replacing the non-combined operand, if any.
  4780. static MachineInstr *
  4781. genFusedMultiply(MachineFunction &MF, MachineRegisterInfo &MRI,
  4782. const TargetInstrInfo *TII, MachineInstr &Root,
  4783. SmallVectorImpl<MachineInstr *> &InsInstrs, unsigned IdxMulOpd,
  4784. unsigned MaddOpc, const TargetRegisterClass *RC,
  4785. FMAInstKind kind = FMAInstKind::Default,
  4786. const Register *ReplacedAddend = nullptr) {
  4787. assert(IdxMulOpd == 1 || IdxMulOpd == 2);
  4788. unsigned IdxOtherOpd = IdxMulOpd == 1 ? 2 : 1;
  4789. MachineInstr *MUL = MRI.getUniqueVRegDef(Root.getOperand(IdxMulOpd).getReg());
  4790. Register ResultReg = Root.getOperand(0).getReg();
  4791. Register SrcReg0 = MUL->getOperand(1).getReg();
  4792. bool Src0IsKill = MUL->getOperand(1).isKill();
  4793. Register SrcReg1 = MUL->getOperand(2).getReg();
  4794. bool Src1IsKill = MUL->getOperand(2).isKill();
  4795. unsigned SrcReg2;
  4796. bool Src2IsKill;
  4797. if (ReplacedAddend) {
  4798. // If we just generated a new addend, we must be it's only use.
  4799. SrcReg2 = *ReplacedAddend;
  4800. Src2IsKill = true;
  4801. } else {
  4802. SrcReg2 = Root.getOperand(IdxOtherOpd).getReg();
  4803. Src2IsKill = Root.getOperand(IdxOtherOpd).isKill();
  4804. }
  4805. if (Register::isVirtualRegister(ResultReg))
  4806. MRI.constrainRegClass(ResultReg, RC);
  4807. if (Register::isVirtualRegister(SrcReg0))
  4808. MRI.constrainRegClass(SrcReg0, RC);
  4809. if (Register::isVirtualRegister(SrcReg1))
  4810. MRI.constrainRegClass(SrcReg1, RC);
  4811. if (Register::isVirtualRegister(SrcReg2))
  4812. MRI.constrainRegClass(SrcReg2, RC);
  4813. MachineInstrBuilder MIB;
  4814. if (kind == FMAInstKind::Default)
  4815. MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
  4816. .addReg(SrcReg0, getKillRegState(Src0IsKill))
  4817. .addReg(SrcReg1, getKillRegState(Src1IsKill))
  4818. .addReg(SrcReg2, getKillRegState(Src2IsKill));
  4819. else if (kind == FMAInstKind::Indexed)
  4820. MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
  4821. .addReg(SrcReg2, getKillRegState(Src2IsKill))
  4822. .addReg(SrcReg0, getKillRegState(Src0IsKill))
  4823. .addReg(SrcReg1, getKillRegState(Src1IsKill))
  4824. .addImm(MUL->getOperand(3).getImm());
  4825. else if (kind == FMAInstKind::Accumulator)
  4826. MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
  4827. .addReg(SrcReg2, getKillRegState(Src2IsKill))
  4828. .addReg(SrcReg0, getKillRegState(Src0IsKill))
  4829. .addReg(SrcReg1, getKillRegState(Src1IsKill));
  4830. else
  4831. assert(false && "Invalid FMA instruction kind \n");
  4832. // Insert the MADD (MADD, FMA, FMS, FMLA, FMSL)
  4833. InsInstrs.push_back(MIB);
  4834. return MUL;
  4835. }
  4836. /// Fold (FMUL x (DUP y lane)) into (FMUL_indexed x y lane)
  4837. static MachineInstr *
  4838. genIndexedMultiply(MachineInstr &Root,
  4839. SmallVectorImpl<MachineInstr *> &InsInstrs,
  4840. unsigned IdxDupOp, unsigned MulOpc,
  4841. const TargetRegisterClass *RC, MachineRegisterInfo &MRI) {
  4842. assert(((IdxDupOp == 1) || (IdxDupOp == 2)) &&
  4843. "Invalid index of FMUL operand");
  4844. MachineFunction &MF = *Root.getMF();
  4845. const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
  4846. MachineInstr *Dup =
  4847. MF.getRegInfo().getUniqueVRegDef(Root.getOperand(IdxDupOp).getReg());
  4848. Register DupSrcReg = Dup->getOperand(1).getReg();
  4849. MRI.clearKillFlags(DupSrcReg);
  4850. MRI.constrainRegClass(DupSrcReg, RC);
  4851. unsigned DupSrcLane = Dup->getOperand(2).getImm();
  4852. unsigned IdxMulOp = IdxDupOp == 1 ? 2 : 1;
  4853. MachineOperand &MulOp = Root.getOperand(IdxMulOp);
  4854. Register ResultReg = Root.getOperand(0).getReg();
  4855. MachineInstrBuilder MIB;
  4856. MIB = BuildMI(MF, Root.getDebugLoc(), TII->get(MulOpc), ResultReg)
  4857. .add(MulOp)
  4858. .addReg(DupSrcReg)
  4859. .addImm(DupSrcLane);
  4860. InsInstrs.push_back(MIB);
  4861. return &Root;
  4862. }
  4863. /// genFusedMultiplyAcc - Helper to generate fused multiply accumulate
  4864. /// instructions.
  4865. ///
  4866. /// \see genFusedMultiply
  4867. static MachineInstr *genFusedMultiplyAcc(
  4868. MachineFunction &MF, MachineRegisterInfo &MRI, const TargetInstrInfo *TII,
  4869. MachineInstr &Root, SmallVectorImpl<MachineInstr *> &InsInstrs,
  4870. unsigned IdxMulOpd, unsigned MaddOpc, const TargetRegisterClass *RC) {
  4871. return genFusedMultiply(MF, MRI, TII, Root, InsInstrs, IdxMulOpd, MaddOpc, RC,
  4872. FMAInstKind::Accumulator);
  4873. }
  4874. /// genNeg - Helper to generate an intermediate negation of the second operand
  4875. /// of Root
  4876. static Register genNeg(MachineFunction &MF, MachineRegisterInfo &MRI,
  4877. const TargetInstrInfo *TII, MachineInstr &Root,
  4878. SmallVectorImpl<MachineInstr *> &InsInstrs,
  4879. DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
  4880. unsigned MnegOpc, const TargetRegisterClass *RC) {
  4881. Register NewVR = MRI.createVirtualRegister(RC);
  4882. MachineInstrBuilder MIB =
  4883. BuildMI(MF, Root.getDebugLoc(), TII->get(MnegOpc), NewVR)
  4884. .add(Root.getOperand(2));
  4885. InsInstrs.push_back(MIB);
  4886. assert(InstrIdxForVirtReg.empty());
  4887. InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
  4888. return NewVR;
  4889. }
  4890. /// genFusedMultiplyAccNeg - Helper to generate fused multiply accumulate
  4891. /// instructions with an additional negation of the accumulator
  4892. static MachineInstr *genFusedMultiplyAccNeg(
  4893. MachineFunction &MF, MachineRegisterInfo &MRI, const TargetInstrInfo *TII,
  4894. MachineInstr &Root, SmallVectorImpl<MachineInstr *> &InsInstrs,
  4895. DenseMap<unsigned, unsigned> &InstrIdxForVirtReg, unsigned IdxMulOpd,
  4896. unsigned MaddOpc, unsigned MnegOpc, const TargetRegisterClass *RC) {
  4897. assert(IdxMulOpd == 1);
  4898. Register NewVR =
  4899. genNeg(MF, MRI, TII, Root, InsInstrs, InstrIdxForVirtReg, MnegOpc, RC);
  4900. return genFusedMultiply(MF, MRI, TII, Root, InsInstrs, IdxMulOpd, MaddOpc, RC,
  4901. FMAInstKind::Accumulator, &NewVR);
  4902. }
  4903. /// genFusedMultiplyIdx - Helper to generate fused multiply accumulate
  4904. /// instructions.
  4905. ///
  4906. /// \see genFusedMultiply
  4907. static MachineInstr *genFusedMultiplyIdx(
  4908. MachineFunction &MF, MachineRegisterInfo &MRI, const TargetInstrInfo *TII,
  4909. MachineInstr &Root, SmallVectorImpl<MachineInstr *> &InsInstrs,
  4910. unsigned IdxMulOpd, unsigned MaddOpc, const TargetRegisterClass *RC) {
  4911. return genFusedMultiply(MF, MRI, TII, Root, InsInstrs, IdxMulOpd, MaddOpc, RC,
  4912. FMAInstKind::Indexed);
  4913. }
  4914. /// genFusedMultiplyAccNeg - Helper to generate fused multiply accumulate
  4915. /// instructions with an additional negation of the accumulator
  4916. static MachineInstr *genFusedMultiplyIdxNeg(
  4917. MachineFunction &MF, MachineRegisterInfo &MRI, const TargetInstrInfo *TII,
  4918. MachineInstr &Root, SmallVectorImpl<MachineInstr *> &InsInstrs,
  4919. DenseMap<unsigned, unsigned> &InstrIdxForVirtReg, unsigned IdxMulOpd,
  4920. unsigned MaddOpc, unsigned MnegOpc, const TargetRegisterClass *RC) {
  4921. assert(IdxMulOpd == 1);
  4922. Register NewVR =
  4923. genNeg(MF, MRI, TII, Root, InsInstrs, InstrIdxForVirtReg, MnegOpc, RC);
  4924. return genFusedMultiply(MF, MRI, TII, Root, InsInstrs, IdxMulOpd, MaddOpc, RC,
  4925. FMAInstKind::Indexed, &NewVR);
  4926. }
  4927. /// genMaddR - Generate madd instruction and combine mul and add using
  4928. /// an extra virtual register
  4929. /// Example - an ADD intermediate needs to be stored in a register:
  4930. /// MUL I=A,B,0
  4931. /// ADD R,I,Imm
  4932. /// ==> ORR V, ZR, Imm
  4933. /// ==> MADD R,A,B,V
  4934. /// \param MF Containing MachineFunction
  4935. /// \param MRI Register information
  4936. /// \param TII Target information
  4937. /// \param Root is the ADD instruction
  4938. /// \param [out] InsInstrs is a vector of machine instructions and will
  4939. /// contain the generated madd instruction
  4940. /// \param IdxMulOpd is index of operand in Root that is the result of
  4941. /// the MUL. In the example above IdxMulOpd is 1.
  4942. /// \param MaddOpc the opcode fo the madd instruction
  4943. /// \param VR is a virtual register that holds the value of an ADD operand
  4944. /// (V in the example above).
  4945. /// \param RC Register class of operands
  4946. static MachineInstr *genMaddR(MachineFunction &MF, MachineRegisterInfo &MRI,
  4947. const TargetInstrInfo *TII, MachineInstr &Root,
  4948. SmallVectorImpl<MachineInstr *> &InsInstrs,
  4949. unsigned IdxMulOpd, unsigned MaddOpc, unsigned VR,
  4950. const TargetRegisterClass *RC) {
  4951. assert(IdxMulOpd == 1 || IdxMulOpd == 2);
  4952. MachineInstr *MUL = MRI.getUniqueVRegDef(Root.getOperand(IdxMulOpd).getReg());
  4953. Register ResultReg = Root.getOperand(0).getReg();
  4954. Register SrcReg0 = MUL->getOperand(1).getReg();
  4955. bool Src0IsKill = MUL->getOperand(1).isKill();
  4956. Register SrcReg1 = MUL->getOperand(2).getReg();
  4957. bool Src1IsKill = MUL->getOperand(2).isKill();
  4958. if (Register::isVirtualRegister(ResultReg))
  4959. MRI.constrainRegClass(ResultReg, RC);
  4960. if (Register::isVirtualRegister(SrcReg0))
  4961. MRI.constrainRegClass(SrcReg0, RC);
  4962. if (Register::isVirtualRegister(SrcReg1))
  4963. MRI.constrainRegClass(SrcReg1, RC);
  4964. if (Register::isVirtualRegister(VR))
  4965. MRI.constrainRegClass(VR, RC);
  4966. MachineInstrBuilder MIB =
  4967. BuildMI(MF, Root.getDebugLoc(), TII->get(MaddOpc), ResultReg)
  4968. .addReg(SrcReg0, getKillRegState(Src0IsKill))
  4969. .addReg(SrcReg1, getKillRegState(Src1IsKill))
  4970. .addReg(VR);
  4971. // Insert the MADD
  4972. InsInstrs.push_back(MIB);
  4973. return MUL;
  4974. }
  4975. /// When getMachineCombinerPatterns() finds potential patterns,
  4976. /// this function generates the instructions that could replace the
  4977. /// original code sequence
  4978. void AArch64InstrInfo::genAlternativeCodeSequence(
  4979. MachineInstr &Root, MachineCombinerPattern Pattern,
  4980. SmallVectorImpl<MachineInstr *> &InsInstrs,
  4981. SmallVectorImpl<MachineInstr *> &DelInstrs,
  4982. DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
  4983. MachineBasicBlock &MBB = *Root.getParent();
  4984. MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
  4985. MachineFunction &MF = *MBB.getParent();
  4986. const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
  4987. MachineInstr *MUL = nullptr;
  4988. const TargetRegisterClass *RC;
  4989. unsigned Opc;
  4990. switch (Pattern) {
  4991. default:
  4992. // Reassociate instructions.
  4993. TargetInstrInfo::genAlternativeCodeSequence(Root, Pattern, InsInstrs,
  4994. DelInstrs, InstrIdxForVirtReg);
  4995. return;
  4996. case MachineCombinerPattern::MULADDW_OP1:
  4997. case MachineCombinerPattern::MULADDX_OP1:
  4998. // MUL I=A,B,0
  4999. // ADD R,I,C
  5000. // ==> MADD R,A,B,C
  5001. // --- Create(MADD);
  5002. if (Pattern == MachineCombinerPattern::MULADDW_OP1) {
  5003. Opc = AArch64::MADDWrrr;
  5004. RC = &AArch64::GPR32RegClass;
  5005. } else {
  5006. Opc = AArch64::MADDXrrr;
  5007. RC = &AArch64::GPR64RegClass;
  5008. }
  5009. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5010. break;
  5011. case MachineCombinerPattern::MULADDW_OP2:
  5012. case MachineCombinerPattern::MULADDX_OP2:
  5013. // MUL I=A,B,0
  5014. // ADD R,C,I
  5015. // ==> MADD R,A,B,C
  5016. // --- Create(MADD);
  5017. if (Pattern == MachineCombinerPattern::MULADDW_OP2) {
  5018. Opc = AArch64::MADDWrrr;
  5019. RC = &AArch64::GPR32RegClass;
  5020. } else {
  5021. Opc = AArch64::MADDXrrr;
  5022. RC = &AArch64::GPR64RegClass;
  5023. }
  5024. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5025. break;
  5026. case MachineCombinerPattern::MULADDWI_OP1:
  5027. case MachineCombinerPattern::MULADDXI_OP1: {
  5028. // MUL I=A,B,0
  5029. // ADD R,I,Imm
  5030. // ==> ORR V, ZR, Imm
  5031. // ==> MADD R,A,B,V
  5032. // --- Create(MADD);
  5033. const TargetRegisterClass *OrrRC;
  5034. unsigned BitSize, OrrOpc, ZeroReg;
  5035. if (Pattern == MachineCombinerPattern::MULADDWI_OP1) {
  5036. OrrOpc = AArch64::ORRWri;
  5037. OrrRC = &AArch64::GPR32spRegClass;
  5038. BitSize = 32;
  5039. ZeroReg = AArch64::WZR;
  5040. Opc = AArch64::MADDWrrr;
  5041. RC = &AArch64::GPR32RegClass;
  5042. } else {
  5043. OrrOpc = AArch64::ORRXri;
  5044. OrrRC = &AArch64::GPR64spRegClass;
  5045. BitSize = 64;
  5046. ZeroReg = AArch64::XZR;
  5047. Opc = AArch64::MADDXrrr;
  5048. RC = &AArch64::GPR64RegClass;
  5049. }
  5050. Register NewVR = MRI.createVirtualRegister(OrrRC);
  5051. uint64_t Imm = Root.getOperand(2).getImm();
  5052. if (Root.getOperand(3).isImm()) {
  5053. unsigned Val = Root.getOperand(3).getImm();
  5054. Imm = Imm << Val;
  5055. }
  5056. uint64_t UImm = SignExtend64(Imm, BitSize);
  5057. uint64_t Encoding;
  5058. if (!AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding))
  5059. return;
  5060. MachineInstrBuilder MIB1 =
  5061. BuildMI(MF, Root.getDebugLoc(), TII->get(OrrOpc), NewVR)
  5062. .addReg(ZeroReg)
  5063. .addImm(Encoding);
  5064. InsInstrs.push_back(MIB1);
  5065. InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
  5066. MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
  5067. break;
  5068. }
  5069. case MachineCombinerPattern::MULSUBW_OP1:
  5070. case MachineCombinerPattern::MULSUBX_OP1: {
  5071. // MUL I=A,B,0
  5072. // SUB R,I, C
  5073. // ==> SUB V, 0, C
  5074. // ==> MADD R,A,B,V // = -C + A*B
  5075. // --- Create(MADD);
  5076. const TargetRegisterClass *SubRC;
  5077. unsigned SubOpc, ZeroReg;
  5078. if (Pattern == MachineCombinerPattern::MULSUBW_OP1) {
  5079. SubOpc = AArch64::SUBWrr;
  5080. SubRC = &AArch64::GPR32spRegClass;
  5081. ZeroReg = AArch64::WZR;
  5082. Opc = AArch64::MADDWrrr;
  5083. RC = &AArch64::GPR32RegClass;
  5084. } else {
  5085. SubOpc = AArch64::SUBXrr;
  5086. SubRC = &AArch64::GPR64spRegClass;
  5087. ZeroReg = AArch64::XZR;
  5088. Opc = AArch64::MADDXrrr;
  5089. RC = &AArch64::GPR64RegClass;
  5090. }
  5091. Register NewVR = MRI.createVirtualRegister(SubRC);
  5092. // SUB NewVR, 0, C
  5093. MachineInstrBuilder MIB1 =
  5094. BuildMI(MF, Root.getDebugLoc(), TII->get(SubOpc), NewVR)
  5095. .addReg(ZeroReg)
  5096. .add(Root.getOperand(2));
  5097. InsInstrs.push_back(MIB1);
  5098. InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
  5099. MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
  5100. break;
  5101. }
  5102. case MachineCombinerPattern::MULSUBW_OP2:
  5103. case MachineCombinerPattern::MULSUBX_OP2:
  5104. // MUL I=A,B,0
  5105. // SUB R,C,I
  5106. // ==> MSUB R,A,B,C (computes C - A*B)
  5107. // --- Create(MSUB);
  5108. if (Pattern == MachineCombinerPattern::MULSUBW_OP2) {
  5109. Opc = AArch64::MSUBWrrr;
  5110. RC = &AArch64::GPR32RegClass;
  5111. } else {
  5112. Opc = AArch64::MSUBXrrr;
  5113. RC = &AArch64::GPR64RegClass;
  5114. }
  5115. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5116. break;
  5117. case MachineCombinerPattern::MULSUBWI_OP1:
  5118. case MachineCombinerPattern::MULSUBXI_OP1: {
  5119. // MUL I=A,B,0
  5120. // SUB R,I, Imm
  5121. // ==> ORR V, ZR, -Imm
  5122. // ==> MADD R,A,B,V // = -Imm + A*B
  5123. // --- Create(MADD);
  5124. const TargetRegisterClass *OrrRC;
  5125. unsigned BitSize, OrrOpc, ZeroReg;
  5126. if (Pattern == MachineCombinerPattern::MULSUBWI_OP1) {
  5127. OrrOpc = AArch64::ORRWri;
  5128. OrrRC = &AArch64::GPR32spRegClass;
  5129. BitSize = 32;
  5130. ZeroReg = AArch64::WZR;
  5131. Opc = AArch64::MADDWrrr;
  5132. RC = &AArch64::GPR32RegClass;
  5133. } else {
  5134. OrrOpc = AArch64::ORRXri;
  5135. OrrRC = &AArch64::GPR64spRegClass;
  5136. BitSize = 64;
  5137. ZeroReg = AArch64::XZR;
  5138. Opc = AArch64::MADDXrrr;
  5139. RC = &AArch64::GPR64RegClass;
  5140. }
  5141. Register NewVR = MRI.createVirtualRegister(OrrRC);
  5142. uint64_t Imm = Root.getOperand(2).getImm();
  5143. if (Root.getOperand(3).isImm()) {
  5144. unsigned Val = Root.getOperand(3).getImm();
  5145. Imm = Imm << Val;
  5146. }
  5147. uint64_t UImm = SignExtend64(-Imm, BitSize);
  5148. uint64_t Encoding;
  5149. if (!AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding))
  5150. return;
  5151. MachineInstrBuilder MIB1 =
  5152. BuildMI(MF, Root.getDebugLoc(), TII->get(OrrOpc), NewVR)
  5153. .addReg(ZeroReg)
  5154. .addImm(Encoding);
  5155. InsInstrs.push_back(MIB1);
  5156. InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
  5157. MUL = genMaddR(MF, MRI, TII, Root, InsInstrs, 1, Opc, NewVR, RC);
  5158. break;
  5159. }
  5160. case MachineCombinerPattern::MULADDv8i8_OP1:
  5161. Opc = AArch64::MLAv8i8;
  5162. RC = &AArch64::FPR64RegClass;
  5163. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5164. break;
  5165. case MachineCombinerPattern::MULADDv8i8_OP2:
  5166. Opc = AArch64::MLAv8i8;
  5167. RC = &AArch64::FPR64RegClass;
  5168. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5169. break;
  5170. case MachineCombinerPattern::MULADDv16i8_OP1:
  5171. Opc = AArch64::MLAv16i8;
  5172. RC = &AArch64::FPR128RegClass;
  5173. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5174. break;
  5175. case MachineCombinerPattern::MULADDv16i8_OP2:
  5176. Opc = AArch64::MLAv16i8;
  5177. RC = &AArch64::FPR128RegClass;
  5178. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5179. break;
  5180. case MachineCombinerPattern::MULADDv4i16_OP1:
  5181. Opc = AArch64::MLAv4i16;
  5182. RC = &AArch64::FPR64RegClass;
  5183. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5184. break;
  5185. case MachineCombinerPattern::MULADDv4i16_OP2:
  5186. Opc = AArch64::MLAv4i16;
  5187. RC = &AArch64::FPR64RegClass;
  5188. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5189. break;
  5190. case MachineCombinerPattern::MULADDv8i16_OP1:
  5191. Opc = AArch64::MLAv8i16;
  5192. RC = &AArch64::FPR128RegClass;
  5193. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5194. break;
  5195. case MachineCombinerPattern::MULADDv8i16_OP2:
  5196. Opc = AArch64::MLAv8i16;
  5197. RC = &AArch64::FPR128RegClass;
  5198. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5199. break;
  5200. case MachineCombinerPattern::MULADDv2i32_OP1:
  5201. Opc = AArch64::MLAv2i32;
  5202. RC = &AArch64::FPR64RegClass;
  5203. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5204. break;
  5205. case MachineCombinerPattern::MULADDv2i32_OP2:
  5206. Opc = AArch64::MLAv2i32;
  5207. RC = &AArch64::FPR64RegClass;
  5208. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5209. break;
  5210. case MachineCombinerPattern::MULADDv4i32_OP1:
  5211. Opc = AArch64::MLAv4i32;
  5212. RC = &AArch64::FPR128RegClass;
  5213. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5214. break;
  5215. case MachineCombinerPattern::MULADDv4i32_OP2:
  5216. Opc = AArch64::MLAv4i32;
  5217. RC = &AArch64::FPR128RegClass;
  5218. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5219. break;
  5220. case MachineCombinerPattern::MULSUBv8i8_OP1:
  5221. Opc = AArch64::MLAv8i8;
  5222. RC = &AArch64::FPR64RegClass;
  5223. MUL = genFusedMultiplyAccNeg(MF, MRI, TII, Root, InsInstrs,
  5224. InstrIdxForVirtReg, 1, Opc, AArch64::NEGv8i8,
  5225. RC);
  5226. break;
  5227. case MachineCombinerPattern::MULSUBv8i8_OP2:
  5228. Opc = AArch64::MLSv8i8;
  5229. RC = &AArch64::FPR64RegClass;
  5230. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5231. break;
  5232. case MachineCombinerPattern::MULSUBv16i8_OP1:
  5233. Opc = AArch64::MLAv16i8;
  5234. RC = &AArch64::FPR128RegClass;
  5235. MUL = genFusedMultiplyAccNeg(MF, MRI, TII, Root, InsInstrs,
  5236. InstrIdxForVirtReg, 1, Opc, AArch64::NEGv16i8,
  5237. RC);
  5238. break;
  5239. case MachineCombinerPattern::MULSUBv16i8_OP2:
  5240. Opc = AArch64::MLSv16i8;
  5241. RC = &AArch64::FPR128RegClass;
  5242. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5243. break;
  5244. case MachineCombinerPattern::MULSUBv4i16_OP1:
  5245. Opc = AArch64::MLAv4i16;
  5246. RC = &AArch64::FPR64RegClass;
  5247. MUL = genFusedMultiplyAccNeg(MF, MRI, TII, Root, InsInstrs,
  5248. InstrIdxForVirtReg, 1, Opc, AArch64::NEGv4i16,
  5249. RC);
  5250. break;
  5251. case MachineCombinerPattern::MULSUBv4i16_OP2:
  5252. Opc = AArch64::MLSv4i16;
  5253. RC = &AArch64::FPR64RegClass;
  5254. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5255. break;
  5256. case MachineCombinerPattern::MULSUBv8i16_OP1:
  5257. Opc = AArch64::MLAv8i16;
  5258. RC = &AArch64::FPR128RegClass;
  5259. MUL = genFusedMultiplyAccNeg(MF, MRI, TII, Root, InsInstrs,
  5260. InstrIdxForVirtReg, 1, Opc, AArch64::NEGv8i16,
  5261. RC);
  5262. break;
  5263. case MachineCombinerPattern::MULSUBv8i16_OP2:
  5264. Opc = AArch64::MLSv8i16;
  5265. RC = &AArch64::FPR128RegClass;
  5266. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5267. break;
  5268. case MachineCombinerPattern::MULSUBv2i32_OP1:
  5269. Opc = AArch64::MLAv2i32;
  5270. RC = &AArch64::FPR64RegClass;
  5271. MUL = genFusedMultiplyAccNeg(MF, MRI, TII, Root, InsInstrs,
  5272. InstrIdxForVirtReg, 1, Opc, AArch64::NEGv2i32,
  5273. RC);
  5274. break;
  5275. case MachineCombinerPattern::MULSUBv2i32_OP2:
  5276. Opc = AArch64::MLSv2i32;
  5277. RC = &AArch64::FPR64RegClass;
  5278. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5279. break;
  5280. case MachineCombinerPattern::MULSUBv4i32_OP1:
  5281. Opc = AArch64::MLAv4i32;
  5282. RC = &AArch64::FPR128RegClass;
  5283. MUL = genFusedMultiplyAccNeg(MF, MRI, TII, Root, InsInstrs,
  5284. InstrIdxForVirtReg, 1, Opc, AArch64::NEGv4i32,
  5285. RC);
  5286. break;
  5287. case MachineCombinerPattern::MULSUBv4i32_OP2:
  5288. Opc = AArch64::MLSv4i32;
  5289. RC = &AArch64::FPR128RegClass;
  5290. MUL = genFusedMultiplyAcc(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5291. break;
  5292. case MachineCombinerPattern::MULADDv4i16_indexed_OP1:
  5293. Opc = AArch64::MLAv4i16_indexed;
  5294. RC = &AArch64::FPR64RegClass;
  5295. MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5296. break;
  5297. case MachineCombinerPattern::MULADDv4i16_indexed_OP2:
  5298. Opc = AArch64::MLAv4i16_indexed;
  5299. RC = &AArch64::FPR64RegClass;
  5300. MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5301. break;
  5302. case MachineCombinerPattern::MULADDv8i16_indexed_OP1:
  5303. Opc = AArch64::MLAv8i16_indexed;
  5304. RC = &AArch64::FPR128RegClass;
  5305. MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5306. break;
  5307. case MachineCombinerPattern::MULADDv8i16_indexed_OP2:
  5308. Opc = AArch64::MLAv8i16_indexed;
  5309. RC = &AArch64::FPR128RegClass;
  5310. MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5311. break;
  5312. case MachineCombinerPattern::MULADDv2i32_indexed_OP1:
  5313. Opc = AArch64::MLAv2i32_indexed;
  5314. RC = &AArch64::FPR64RegClass;
  5315. MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5316. break;
  5317. case MachineCombinerPattern::MULADDv2i32_indexed_OP2:
  5318. Opc = AArch64::MLAv2i32_indexed;
  5319. RC = &AArch64::FPR64RegClass;
  5320. MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5321. break;
  5322. case MachineCombinerPattern::MULADDv4i32_indexed_OP1:
  5323. Opc = AArch64::MLAv4i32_indexed;
  5324. RC = &AArch64::FPR128RegClass;
  5325. MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5326. break;
  5327. case MachineCombinerPattern::MULADDv4i32_indexed_OP2:
  5328. Opc = AArch64::MLAv4i32_indexed;
  5329. RC = &AArch64::FPR128RegClass;
  5330. MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5331. break;
  5332. case MachineCombinerPattern::MULSUBv4i16_indexed_OP1:
  5333. Opc = AArch64::MLAv4i16_indexed;
  5334. RC = &AArch64::FPR64RegClass;
  5335. MUL = genFusedMultiplyIdxNeg(MF, MRI, TII, Root, InsInstrs,
  5336. InstrIdxForVirtReg, 1, Opc, AArch64::NEGv4i16,
  5337. RC);
  5338. break;
  5339. case MachineCombinerPattern::MULSUBv4i16_indexed_OP2:
  5340. Opc = AArch64::MLSv4i16_indexed;
  5341. RC = &AArch64::FPR64RegClass;
  5342. MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5343. break;
  5344. case MachineCombinerPattern::MULSUBv8i16_indexed_OP1:
  5345. Opc = AArch64::MLAv8i16_indexed;
  5346. RC = &AArch64::FPR128RegClass;
  5347. MUL = genFusedMultiplyIdxNeg(MF, MRI, TII, Root, InsInstrs,
  5348. InstrIdxForVirtReg, 1, Opc, AArch64::NEGv8i16,
  5349. RC);
  5350. break;
  5351. case MachineCombinerPattern::MULSUBv8i16_indexed_OP2:
  5352. Opc = AArch64::MLSv8i16_indexed;
  5353. RC = &AArch64::FPR128RegClass;
  5354. MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5355. break;
  5356. case MachineCombinerPattern::MULSUBv2i32_indexed_OP1:
  5357. Opc = AArch64::MLAv2i32_indexed;
  5358. RC = &AArch64::FPR64RegClass;
  5359. MUL = genFusedMultiplyIdxNeg(MF, MRI, TII, Root, InsInstrs,
  5360. InstrIdxForVirtReg, 1, Opc, AArch64::NEGv2i32,
  5361. RC);
  5362. break;
  5363. case MachineCombinerPattern::MULSUBv2i32_indexed_OP2:
  5364. Opc = AArch64::MLSv2i32_indexed;
  5365. RC = &AArch64::FPR64RegClass;
  5366. MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5367. break;
  5368. case MachineCombinerPattern::MULSUBv4i32_indexed_OP1:
  5369. Opc = AArch64::MLAv4i32_indexed;
  5370. RC = &AArch64::FPR128RegClass;
  5371. MUL = genFusedMultiplyIdxNeg(MF, MRI, TII, Root, InsInstrs,
  5372. InstrIdxForVirtReg, 1, Opc, AArch64::NEGv4i32,
  5373. RC);
  5374. break;
  5375. case MachineCombinerPattern::MULSUBv4i32_indexed_OP2:
  5376. Opc = AArch64::MLSv4i32_indexed;
  5377. RC = &AArch64::FPR128RegClass;
  5378. MUL = genFusedMultiplyIdx(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5379. break;
  5380. // Floating Point Support
  5381. case MachineCombinerPattern::FMULADDH_OP1:
  5382. Opc = AArch64::FMADDHrrr;
  5383. RC = &AArch64::FPR16RegClass;
  5384. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5385. break;
  5386. case MachineCombinerPattern::FMULADDS_OP1:
  5387. Opc = AArch64::FMADDSrrr;
  5388. RC = &AArch64::FPR32RegClass;
  5389. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5390. break;
  5391. case MachineCombinerPattern::FMULADDD_OP1:
  5392. Opc = AArch64::FMADDDrrr;
  5393. RC = &AArch64::FPR64RegClass;
  5394. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5395. break;
  5396. case MachineCombinerPattern::FMULADDH_OP2:
  5397. Opc = AArch64::FMADDHrrr;
  5398. RC = &AArch64::FPR16RegClass;
  5399. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5400. break;
  5401. case MachineCombinerPattern::FMULADDS_OP2:
  5402. Opc = AArch64::FMADDSrrr;
  5403. RC = &AArch64::FPR32RegClass;
  5404. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5405. break;
  5406. case MachineCombinerPattern::FMULADDD_OP2:
  5407. Opc = AArch64::FMADDDrrr;
  5408. RC = &AArch64::FPR64RegClass;
  5409. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5410. break;
  5411. case MachineCombinerPattern::FMLAv1i32_indexed_OP1:
  5412. Opc = AArch64::FMLAv1i32_indexed;
  5413. RC = &AArch64::FPR32RegClass;
  5414. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5415. FMAInstKind::Indexed);
  5416. break;
  5417. case MachineCombinerPattern::FMLAv1i32_indexed_OP2:
  5418. Opc = AArch64::FMLAv1i32_indexed;
  5419. RC = &AArch64::FPR32RegClass;
  5420. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5421. FMAInstKind::Indexed);
  5422. break;
  5423. case MachineCombinerPattern::FMLAv1i64_indexed_OP1:
  5424. Opc = AArch64::FMLAv1i64_indexed;
  5425. RC = &AArch64::FPR64RegClass;
  5426. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5427. FMAInstKind::Indexed);
  5428. break;
  5429. case MachineCombinerPattern::FMLAv1i64_indexed_OP2:
  5430. Opc = AArch64::FMLAv1i64_indexed;
  5431. RC = &AArch64::FPR64RegClass;
  5432. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5433. FMAInstKind::Indexed);
  5434. break;
  5435. case MachineCombinerPattern::FMLAv4i16_indexed_OP1:
  5436. RC = &AArch64::FPR64RegClass;
  5437. Opc = AArch64::FMLAv4i16_indexed;
  5438. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5439. FMAInstKind::Indexed);
  5440. break;
  5441. case MachineCombinerPattern::FMLAv4f16_OP1:
  5442. RC = &AArch64::FPR64RegClass;
  5443. Opc = AArch64::FMLAv4f16;
  5444. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5445. FMAInstKind::Accumulator);
  5446. break;
  5447. case MachineCombinerPattern::FMLAv4i16_indexed_OP2:
  5448. RC = &AArch64::FPR64RegClass;
  5449. Opc = AArch64::FMLAv4i16_indexed;
  5450. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5451. FMAInstKind::Indexed);
  5452. break;
  5453. case MachineCombinerPattern::FMLAv4f16_OP2:
  5454. RC = &AArch64::FPR64RegClass;
  5455. Opc = AArch64::FMLAv4f16;
  5456. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5457. FMAInstKind::Accumulator);
  5458. break;
  5459. case MachineCombinerPattern::FMLAv2i32_indexed_OP1:
  5460. case MachineCombinerPattern::FMLAv2f32_OP1:
  5461. RC = &AArch64::FPR64RegClass;
  5462. if (Pattern == MachineCombinerPattern::FMLAv2i32_indexed_OP1) {
  5463. Opc = AArch64::FMLAv2i32_indexed;
  5464. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5465. FMAInstKind::Indexed);
  5466. } else {
  5467. Opc = AArch64::FMLAv2f32;
  5468. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5469. FMAInstKind::Accumulator);
  5470. }
  5471. break;
  5472. case MachineCombinerPattern::FMLAv2i32_indexed_OP2:
  5473. case MachineCombinerPattern::FMLAv2f32_OP2:
  5474. RC = &AArch64::FPR64RegClass;
  5475. if (Pattern == MachineCombinerPattern::FMLAv2i32_indexed_OP2) {
  5476. Opc = AArch64::FMLAv2i32_indexed;
  5477. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5478. FMAInstKind::Indexed);
  5479. } else {
  5480. Opc = AArch64::FMLAv2f32;
  5481. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5482. FMAInstKind::Accumulator);
  5483. }
  5484. break;
  5485. case MachineCombinerPattern::FMLAv8i16_indexed_OP1:
  5486. RC = &AArch64::FPR128RegClass;
  5487. Opc = AArch64::FMLAv8i16_indexed;
  5488. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5489. FMAInstKind::Indexed);
  5490. break;
  5491. case MachineCombinerPattern::FMLAv8f16_OP1:
  5492. RC = &AArch64::FPR128RegClass;
  5493. Opc = AArch64::FMLAv8f16;
  5494. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5495. FMAInstKind::Accumulator);
  5496. break;
  5497. case MachineCombinerPattern::FMLAv8i16_indexed_OP2:
  5498. RC = &AArch64::FPR128RegClass;
  5499. Opc = AArch64::FMLAv8i16_indexed;
  5500. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5501. FMAInstKind::Indexed);
  5502. break;
  5503. case MachineCombinerPattern::FMLAv8f16_OP2:
  5504. RC = &AArch64::FPR128RegClass;
  5505. Opc = AArch64::FMLAv8f16;
  5506. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5507. FMAInstKind::Accumulator);
  5508. break;
  5509. case MachineCombinerPattern::FMLAv2i64_indexed_OP1:
  5510. case MachineCombinerPattern::FMLAv2f64_OP1:
  5511. RC = &AArch64::FPR128RegClass;
  5512. if (Pattern == MachineCombinerPattern::FMLAv2i64_indexed_OP1) {
  5513. Opc = AArch64::FMLAv2i64_indexed;
  5514. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5515. FMAInstKind::Indexed);
  5516. } else {
  5517. Opc = AArch64::FMLAv2f64;
  5518. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5519. FMAInstKind::Accumulator);
  5520. }
  5521. break;
  5522. case MachineCombinerPattern::FMLAv2i64_indexed_OP2:
  5523. case MachineCombinerPattern::FMLAv2f64_OP2:
  5524. RC = &AArch64::FPR128RegClass;
  5525. if (Pattern == MachineCombinerPattern::FMLAv2i64_indexed_OP2) {
  5526. Opc = AArch64::FMLAv2i64_indexed;
  5527. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5528. FMAInstKind::Indexed);
  5529. } else {
  5530. Opc = AArch64::FMLAv2f64;
  5531. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5532. FMAInstKind::Accumulator);
  5533. }
  5534. break;
  5535. case MachineCombinerPattern::FMLAv4i32_indexed_OP1:
  5536. case MachineCombinerPattern::FMLAv4f32_OP1:
  5537. RC = &AArch64::FPR128RegClass;
  5538. if (Pattern == MachineCombinerPattern::FMLAv4i32_indexed_OP1) {
  5539. Opc = AArch64::FMLAv4i32_indexed;
  5540. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5541. FMAInstKind::Indexed);
  5542. } else {
  5543. Opc = AArch64::FMLAv4f32;
  5544. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5545. FMAInstKind::Accumulator);
  5546. }
  5547. break;
  5548. case MachineCombinerPattern::FMLAv4i32_indexed_OP2:
  5549. case MachineCombinerPattern::FMLAv4f32_OP2:
  5550. RC = &AArch64::FPR128RegClass;
  5551. if (Pattern == MachineCombinerPattern::FMLAv4i32_indexed_OP2) {
  5552. Opc = AArch64::FMLAv4i32_indexed;
  5553. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5554. FMAInstKind::Indexed);
  5555. } else {
  5556. Opc = AArch64::FMLAv4f32;
  5557. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5558. FMAInstKind::Accumulator);
  5559. }
  5560. break;
  5561. case MachineCombinerPattern::FMULSUBH_OP1:
  5562. Opc = AArch64::FNMSUBHrrr;
  5563. RC = &AArch64::FPR16RegClass;
  5564. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5565. break;
  5566. case MachineCombinerPattern::FMULSUBS_OP1:
  5567. Opc = AArch64::FNMSUBSrrr;
  5568. RC = &AArch64::FPR32RegClass;
  5569. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5570. break;
  5571. case MachineCombinerPattern::FMULSUBD_OP1:
  5572. Opc = AArch64::FNMSUBDrrr;
  5573. RC = &AArch64::FPR64RegClass;
  5574. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5575. break;
  5576. case MachineCombinerPattern::FNMULSUBH_OP1:
  5577. Opc = AArch64::FNMADDHrrr;
  5578. RC = &AArch64::FPR16RegClass;
  5579. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5580. break;
  5581. case MachineCombinerPattern::FNMULSUBS_OP1:
  5582. Opc = AArch64::FNMADDSrrr;
  5583. RC = &AArch64::FPR32RegClass;
  5584. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5585. break;
  5586. case MachineCombinerPattern::FNMULSUBD_OP1:
  5587. Opc = AArch64::FNMADDDrrr;
  5588. RC = &AArch64::FPR64RegClass;
  5589. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC);
  5590. break;
  5591. case MachineCombinerPattern::FMULSUBH_OP2:
  5592. Opc = AArch64::FMSUBHrrr;
  5593. RC = &AArch64::FPR16RegClass;
  5594. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5595. break;
  5596. case MachineCombinerPattern::FMULSUBS_OP2:
  5597. Opc = AArch64::FMSUBSrrr;
  5598. RC = &AArch64::FPR32RegClass;
  5599. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5600. break;
  5601. case MachineCombinerPattern::FMULSUBD_OP2:
  5602. Opc = AArch64::FMSUBDrrr;
  5603. RC = &AArch64::FPR64RegClass;
  5604. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC);
  5605. break;
  5606. case MachineCombinerPattern::FMLSv1i32_indexed_OP2:
  5607. Opc = AArch64::FMLSv1i32_indexed;
  5608. RC = &AArch64::FPR32RegClass;
  5609. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5610. FMAInstKind::Indexed);
  5611. break;
  5612. case MachineCombinerPattern::FMLSv1i64_indexed_OP2:
  5613. Opc = AArch64::FMLSv1i64_indexed;
  5614. RC = &AArch64::FPR64RegClass;
  5615. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5616. FMAInstKind::Indexed);
  5617. break;
  5618. case MachineCombinerPattern::FMLSv4f16_OP1:
  5619. case MachineCombinerPattern::FMLSv4i16_indexed_OP1: {
  5620. RC = &AArch64::FPR64RegClass;
  5621. Register NewVR = MRI.createVirtualRegister(RC);
  5622. MachineInstrBuilder MIB1 =
  5623. BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv4f16), NewVR)
  5624. .add(Root.getOperand(2));
  5625. InsInstrs.push_back(MIB1);
  5626. InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
  5627. if (Pattern == MachineCombinerPattern::FMLSv4f16_OP1) {
  5628. Opc = AArch64::FMLAv4f16;
  5629. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5630. FMAInstKind::Accumulator, &NewVR);
  5631. } else {
  5632. Opc = AArch64::FMLAv4i16_indexed;
  5633. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5634. FMAInstKind::Indexed, &NewVR);
  5635. }
  5636. break;
  5637. }
  5638. case MachineCombinerPattern::FMLSv4f16_OP2:
  5639. RC = &AArch64::FPR64RegClass;
  5640. Opc = AArch64::FMLSv4f16;
  5641. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5642. FMAInstKind::Accumulator);
  5643. break;
  5644. case MachineCombinerPattern::FMLSv4i16_indexed_OP2:
  5645. RC = &AArch64::FPR64RegClass;
  5646. Opc = AArch64::FMLSv4i16_indexed;
  5647. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5648. FMAInstKind::Indexed);
  5649. break;
  5650. case MachineCombinerPattern::FMLSv2f32_OP2:
  5651. case MachineCombinerPattern::FMLSv2i32_indexed_OP2:
  5652. RC = &AArch64::FPR64RegClass;
  5653. if (Pattern == MachineCombinerPattern::FMLSv2i32_indexed_OP2) {
  5654. Opc = AArch64::FMLSv2i32_indexed;
  5655. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5656. FMAInstKind::Indexed);
  5657. } else {
  5658. Opc = AArch64::FMLSv2f32;
  5659. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5660. FMAInstKind::Accumulator);
  5661. }
  5662. break;
  5663. case MachineCombinerPattern::FMLSv8f16_OP1:
  5664. case MachineCombinerPattern::FMLSv8i16_indexed_OP1: {
  5665. RC = &AArch64::FPR128RegClass;
  5666. Register NewVR = MRI.createVirtualRegister(RC);
  5667. MachineInstrBuilder MIB1 =
  5668. BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv8f16), NewVR)
  5669. .add(Root.getOperand(2));
  5670. InsInstrs.push_back(MIB1);
  5671. InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
  5672. if (Pattern == MachineCombinerPattern::FMLSv8f16_OP1) {
  5673. Opc = AArch64::FMLAv8f16;
  5674. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5675. FMAInstKind::Accumulator, &NewVR);
  5676. } else {
  5677. Opc = AArch64::FMLAv8i16_indexed;
  5678. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5679. FMAInstKind::Indexed, &NewVR);
  5680. }
  5681. break;
  5682. }
  5683. case MachineCombinerPattern::FMLSv8f16_OP2:
  5684. RC = &AArch64::FPR128RegClass;
  5685. Opc = AArch64::FMLSv8f16;
  5686. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5687. FMAInstKind::Accumulator);
  5688. break;
  5689. case MachineCombinerPattern::FMLSv8i16_indexed_OP2:
  5690. RC = &AArch64::FPR128RegClass;
  5691. Opc = AArch64::FMLSv8i16_indexed;
  5692. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5693. FMAInstKind::Indexed);
  5694. break;
  5695. case MachineCombinerPattern::FMLSv2f64_OP2:
  5696. case MachineCombinerPattern::FMLSv2i64_indexed_OP2:
  5697. RC = &AArch64::FPR128RegClass;
  5698. if (Pattern == MachineCombinerPattern::FMLSv2i64_indexed_OP2) {
  5699. Opc = AArch64::FMLSv2i64_indexed;
  5700. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5701. FMAInstKind::Indexed);
  5702. } else {
  5703. Opc = AArch64::FMLSv2f64;
  5704. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5705. FMAInstKind::Accumulator);
  5706. }
  5707. break;
  5708. case MachineCombinerPattern::FMLSv4f32_OP2:
  5709. case MachineCombinerPattern::FMLSv4i32_indexed_OP2:
  5710. RC = &AArch64::FPR128RegClass;
  5711. if (Pattern == MachineCombinerPattern::FMLSv4i32_indexed_OP2) {
  5712. Opc = AArch64::FMLSv4i32_indexed;
  5713. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5714. FMAInstKind::Indexed);
  5715. } else {
  5716. Opc = AArch64::FMLSv4f32;
  5717. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 2, Opc, RC,
  5718. FMAInstKind::Accumulator);
  5719. }
  5720. break;
  5721. case MachineCombinerPattern::FMLSv2f32_OP1:
  5722. case MachineCombinerPattern::FMLSv2i32_indexed_OP1: {
  5723. RC = &AArch64::FPR64RegClass;
  5724. Register NewVR = MRI.createVirtualRegister(RC);
  5725. MachineInstrBuilder MIB1 =
  5726. BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv2f32), NewVR)
  5727. .add(Root.getOperand(2));
  5728. InsInstrs.push_back(MIB1);
  5729. InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
  5730. if (Pattern == MachineCombinerPattern::FMLSv2i32_indexed_OP1) {
  5731. Opc = AArch64::FMLAv2i32_indexed;
  5732. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5733. FMAInstKind::Indexed, &NewVR);
  5734. } else {
  5735. Opc = AArch64::FMLAv2f32;
  5736. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5737. FMAInstKind::Accumulator, &NewVR);
  5738. }
  5739. break;
  5740. }
  5741. case MachineCombinerPattern::FMLSv4f32_OP1:
  5742. case MachineCombinerPattern::FMLSv4i32_indexed_OP1: {
  5743. RC = &AArch64::FPR128RegClass;
  5744. Register NewVR = MRI.createVirtualRegister(RC);
  5745. MachineInstrBuilder MIB1 =
  5746. BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv4f32), NewVR)
  5747. .add(Root.getOperand(2));
  5748. InsInstrs.push_back(MIB1);
  5749. InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
  5750. if (Pattern == MachineCombinerPattern::FMLSv4i32_indexed_OP1) {
  5751. Opc = AArch64::FMLAv4i32_indexed;
  5752. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5753. FMAInstKind::Indexed, &NewVR);
  5754. } else {
  5755. Opc = AArch64::FMLAv4f32;
  5756. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5757. FMAInstKind::Accumulator, &NewVR);
  5758. }
  5759. break;
  5760. }
  5761. case MachineCombinerPattern::FMLSv2f64_OP1:
  5762. case MachineCombinerPattern::FMLSv2i64_indexed_OP1: {
  5763. RC = &AArch64::FPR128RegClass;
  5764. Register NewVR = MRI.createVirtualRegister(RC);
  5765. MachineInstrBuilder MIB1 =
  5766. BuildMI(MF, Root.getDebugLoc(), TII->get(AArch64::FNEGv2f64), NewVR)
  5767. .add(Root.getOperand(2));
  5768. InsInstrs.push_back(MIB1);
  5769. InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
  5770. if (Pattern == MachineCombinerPattern::FMLSv2i64_indexed_OP1) {
  5771. Opc = AArch64::FMLAv2i64_indexed;
  5772. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5773. FMAInstKind::Indexed, &NewVR);
  5774. } else {
  5775. Opc = AArch64::FMLAv2f64;
  5776. MUL = genFusedMultiply(MF, MRI, TII, Root, InsInstrs, 1, Opc, RC,
  5777. FMAInstKind::Accumulator, &NewVR);
  5778. }
  5779. break;
  5780. }
  5781. case MachineCombinerPattern::FMULv2i32_indexed_OP1:
  5782. case MachineCombinerPattern::FMULv2i32_indexed_OP2: {
  5783. unsigned IdxDupOp =
  5784. (Pattern == MachineCombinerPattern::FMULv2i32_indexed_OP1) ? 1 : 2;
  5785. genIndexedMultiply(Root, InsInstrs, IdxDupOp, AArch64::FMULv2i32_indexed,
  5786. &AArch64::FPR128RegClass, MRI);
  5787. break;
  5788. }
  5789. case MachineCombinerPattern::FMULv2i64_indexed_OP1:
  5790. case MachineCombinerPattern::FMULv2i64_indexed_OP2: {
  5791. unsigned IdxDupOp =
  5792. (Pattern == MachineCombinerPattern::FMULv2i64_indexed_OP1) ? 1 : 2;
  5793. genIndexedMultiply(Root, InsInstrs, IdxDupOp, AArch64::FMULv2i64_indexed,
  5794. &AArch64::FPR128RegClass, MRI);
  5795. break;
  5796. }
  5797. case MachineCombinerPattern::FMULv4i16_indexed_OP1:
  5798. case MachineCombinerPattern::FMULv4i16_indexed_OP2: {
  5799. unsigned IdxDupOp =
  5800. (Pattern == MachineCombinerPattern::FMULv4i16_indexed_OP1) ? 1 : 2;
  5801. genIndexedMultiply(Root, InsInstrs, IdxDupOp, AArch64::FMULv4i16_indexed,
  5802. &AArch64::FPR128_loRegClass, MRI);
  5803. break;
  5804. }
  5805. case MachineCombinerPattern::FMULv4i32_indexed_OP1:
  5806. case MachineCombinerPattern::FMULv4i32_indexed_OP2: {
  5807. unsigned IdxDupOp =
  5808. (Pattern == MachineCombinerPattern::FMULv4i32_indexed_OP1) ? 1 : 2;
  5809. genIndexedMultiply(Root, InsInstrs, IdxDupOp, AArch64::FMULv4i32_indexed,
  5810. &AArch64::FPR128RegClass, MRI);
  5811. break;
  5812. }
  5813. case MachineCombinerPattern::FMULv8i16_indexed_OP1:
  5814. case MachineCombinerPattern::FMULv8i16_indexed_OP2: {
  5815. unsigned IdxDupOp =
  5816. (Pattern == MachineCombinerPattern::FMULv8i16_indexed_OP1) ? 1 : 2;
  5817. genIndexedMultiply(Root, InsInstrs, IdxDupOp, AArch64::FMULv8i16_indexed,
  5818. &AArch64::FPR128_loRegClass, MRI);
  5819. break;
  5820. }
  5821. } // end switch (Pattern)
  5822. // Record MUL and ADD/SUB for deletion
  5823. if (MUL)
  5824. DelInstrs.push_back(MUL);
  5825. DelInstrs.push_back(&Root);
  5826. }
  5827. /// Replace csincr-branch sequence by simple conditional branch
  5828. ///
  5829. /// Examples:
  5830. /// 1. \code
  5831. /// csinc w9, wzr, wzr, <condition code>
  5832. /// tbnz w9, #0, 0x44
  5833. /// \endcode
  5834. /// to
  5835. /// \code
  5836. /// b.<inverted condition code>
  5837. /// \endcode
  5838. ///
  5839. /// 2. \code
  5840. /// csinc w9, wzr, wzr, <condition code>
  5841. /// tbz w9, #0, 0x44
  5842. /// \endcode
  5843. /// to
  5844. /// \code
  5845. /// b.<condition code>
  5846. /// \endcode
  5847. ///
  5848. /// Replace compare and branch sequence by TBZ/TBNZ instruction when the
  5849. /// compare's constant operand is power of 2.
  5850. ///
  5851. /// Examples:
  5852. /// \code
  5853. /// and w8, w8, #0x400
  5854. /// cbnz w8, L1
  5855. /// \endcode
  5856. /// to
  5857. /// \code
  5858. /// tbnz w8, #10, L1
  5859. /// \endcode
  5860. ///
  5861. /// \param MI Conditional Branch
  5862. /// \return True when the simple conditional branch is generated
  5863. ///
  5864. bool AArch64InstrInfo::optimizeCondBranch(MachineInstr &MI) const {
  5865. bool IsNegativeBranch = false;
  5866. bool IsTestAndBranch = false;
  5867. unsigned TargetBBInMI = 0;
  5868. switch (MI.getOpcode()) {
  5869. default:
  5870. llvm_unreachable("Unknown branch instruction?");
  5871. case AArch64::Bcc:
  5872. return false;
  5873. case AArch64::CBZW:
  5874. case AArch64::CBZX:
  5875. TargetBBInMI = 1;
  5876. break;
  5877. case AArch64::CBNZW:
  5878. case AArch64::CBNZX:
  5879. TargetBBInMI = 1;
  5880. IsNegativeBranch = true;
  5881. break;
  5882. case AArch64::TBZW:
  5883. case AArch64::TBZX:
  5884. TargetBBInMI = 2;
  5885. IsTestAndBranch = true;
  5886. break;
  5887. case AArch64::TBNZW:
  5888. case AArch64::TBNZX:
  5889. TargetBBInMI = 2;
  5890. IsNegativeBranch = true;
  5891. IsTestAndBranch = true;
  5892. break;
  5893. }
  5894. // So we increment a zero register and test for bits other
  5895. // than bit 0? Conservatively bail out in case the verifier
  5896. // missed this case.
  5897. if (IsTestAndBranch && MI.getOperand(1).getImm())
  5898. return false;
  5899. // Find Definition.
  5900. assert(MI.getParent() && "Incomplete machine instruciton\n");
  5901. MachineBasicBlock *MBB = MI.getParent();
  5902. MachineFunction *MF = MBB->getParent();
  5903. MachineRegisterInfo *MRI = &MF->getRegInfo();
  5904. Register VReg = MI.getOperand(0).getReg();
  5905. if (!Register::isVirtualRegister(VReg))
  5906. return false;
  5907. MachineInstr *DefMI = MRI->getVRegDef(VReg);
  5908. // Look through COPY instructions to find definition.
  5909. while (DefMI->isCopy()) {
  5910. Register CopyVReg = DefMI->getOperand(1).getReg();
  5911. if (!MRI->hasOneNonDBGUse(CopyVReg))
  5912. return false;
  5913. if (!MRI->hasOneDef(CopyVReg))
  5914. return false;
  5915. DefMI = MRI->getVRegDef(CopyVReg);
  5916. }
  5917. switch (DefMI->getOpcode()) {
  5918. default:
  5919. return false;
  5920. // Fold AND into a TBZ/TBNZ if constant operand is power of 2.
  5921. case AArch64::ANDWri:
  5922. case AArch64::ANDXri: {
  5923. if (IsTestAndBranch)
  5924. return false;
  5925. if (DefMI->getParent() != MBB)
  5926. return false;
  5927. if (!MRI->hasOneNonDBGUse(VReg))
  5928. return false;
  5929. bool Is32Bit = (DefMI->getOpcode() == AArch64::ANDWri);
  5930. uint64_t Mask = AArch64_AM::decodeLogicalImmediate(
  5931. DefMI->getOperand(2).getImm(), Is32Bit ? 32 : 64);
  5932. if (!isPowerOf2_64(Mask))
  5933. return false;
  5934. MachineOperand &MO = DefMI->getOperand(1);
  5935. Register NewReg = MO.getReg();
  5936. if (!Register::isVirtualRegister(NewReg))
  5937. return false;
  5938. assert(!MRI->def_empty(NewReg) && "Register must be defined.");
  5939. MachineBasicBlock &RefToMBB = *MBB;
  5940. MachineBasicBlock *TBB = MI.getOperand(1).getMBB();
  5941. DebugLoc DL = MI.getDebugLoc();
  5942. unsigned Imm = Log2_64(Mask);
  5943. unsigned Opc = (Imm < 32)
  5944. ? (IsNegativeBranch ? AArch64::TBNZW : AArch64::TBZW)
  5945. : (IsNegativeBranch ? AArch64::TBNZX : AArch64::TBZX);
  5946. MachineInstr *NewMI = BuildMI(RefToMBB, MI, DL, get(Opc))
  5947. .addReg(NewReg)
  5948. .addImm(Imm)
  5949. .addMBB(TBB);
  5950. // Register lives on to the CBZ now.
  5951. MO.setIsKill(false);
  5952. // For immediate smaller than 32, we need to use the 32-bit
  5953. // variant (W) in all cases. Indeed the 64-bit variant does not
  5954. // allow to encode them.
  5955. // Therefore, if the input register is 64-bit, we need to take the
  5956. // 32-bit sub-part.
  5957. if (!Is32Bit && Imm < 32)
  5958. NewMI->getOperand(0).setSubReg(AArch64::sub_32);
  5959. MI.eraseFromParent();
  5960. return true;
  5961. }
  5962. // Look for CSINC
  5963. case AArch64::CSINCWr:
  5964. case AArch64::CSINCXr: {
  5965. if (!(DefMI->getOperand(1).getReg() == AArch64::WZR &&
  5966. DefMI->getOperand(2).getReg() == AArch64::WZR) &&
  5967. !(DefMI->getOperand(1).getReg() == AArch64::XZR &&
  5968. DefMI->getOperand(2).getReg() == AArch64::XZR))
  5969. return false;
  5970. if (DefMI->findRegisterDefOperandIdx(AArch64::NZCV, true) != -1)
  5971. return false;
  5972. AArch64CC::CondCode CC = (AArch64CC::CondCode)DefMI->getOperand(3).getImm();
  5973. // Convert only when the condition code is not modified between
  5974. // the CSINC and the branch. The CC may be used by other
  5975. // instructions in between.
  5976. if (areCFlagsAccessedBetweenInstrs(DefMI, MI, &getRegisterInfo(), AK_Write))
  5977. return false;
  5978. MachineBasicBlock &RefToMBB = *MBB;
  5979. MachineBasicBlock *TBB = MI.getOperand(TargetBBInMI).getMBB();
  5980. DebugLoc DL = MI.getDebugLoc();
  5981. if (IsNegativeBranch)
  5982. CC = AArch64CC::getInvertedCondCode(CC);
  5983. BuildMI(RefToMBB, MI, DL, get(AArch64::Bcc)).addImm(CC).addMBB(TBB);
  5984. MI.eraseFromParent();
  5985. return true;
  5986. }
  5987. }
  5988. }
  5989. std::pair<unsigned, unsigned>
  5990. AArch64InstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
  5991. const unsigned Mask = AArch64II::MO_FRAGMENT;
  5992. return std::make_pair(TF & Mask, TF & ~Mask);
  5993. }
  5994. ArrayRef<std::pair<unsigned, const char *>>
  5995. AArch64InstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
  5996. using namespace AArch64II;
  5997. static const std::pair<unsigned, const char *> TargetFlags[] = {
  5998. {MO_PAGE, "aarch64-page"}, {MO_PAGEOFF, "aarch64-pageoff"},
  5999. {MO_G3, "aarch64-g3"}, {MO_G2, "aarch64-g2"},
  6000. {MO_G1, "aarch64-g1"}, {MO_G0, "aarch64-g0"},
  6001. {MO_HI12, "aarch64-hi12"}};
  6002. return makeArrayRef(TargetFlags);
  6003. }
  6004. ArrayRef<std::pair<unsigned, const char *>>
  6005. AArch64InstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
  6006. using namespace AArch64II;
  6007. static const std::pair<unsigned, const char *> TargetFlags[] = {
  6008. {MO_COFFSTUB, "aarch64-coffstub"},
  6009. {MO_GOT, "aarch64-got"},
  6010. {MO_NC, "aarch64-nc"},
  6011. {MO_S, "aarch64-s"},
  6012. {MO_TLS, "aarch64-tls"},
  6013. {MO_DLLIMPORT, "aarch64-dllimport"},
  6014. {MO_PREL, "aarch64-prel"},
  6015. {MO_TAGGED, "aarch64-tagged"}};
  6016. return makeArrayRef(TargetFlags);
  6017. }
  6018. ArrayRef<std::pair<MachineMemOperand::Flags, const char *>>
  6019. AArch64InstrInfo::getSerializableMachineMemOperandTargetFlags() const {
  6020. static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] =
  6021. {{MOSuppressPair, "aarch64-suppress-pair"},
  6022. {MOStridedAccess, "aarch64-strided-access"}};
  6023. return makeArrayRef(TargetFlags);
  6024. }
  6025. /// Constants defining how certain sequences should be outlined.
  6026. /// This encompasses how an outlined function should be called, and what kind of
  6027. /// frame should be emitted for that outlined function.
  6028. ///
  6029. /// \p MachineOutlinerDefault implies that the function should be called with
  6030. /// a save and restore of LR to the stack.
  6031. ///
  6032. /// That is,
  6033. ///
  6034. /// I1 Save LR OUTLINED_FUNCTION:
  6035. /// I2 --> BL OUTLINED_FUNCTION I1
  6036. /// I3 Restore LR I2
  6037. /// I3
  6038. /// RET
  6039. ///
  6040. /// * Call construction overhead: 3 (save + BL + restore)
  6041. /// * Frame construction overhead: 1 (ret)
  6042. /// * Requires stack fixups? Yes
  6043. ///
  6044. /// \p MachineOutlinerTailCall implies that the function is being created from
  6045. /// a sequence of instructions ending in a return.
  6046. ///
  6047. /// That is,
  6048. ///
  6049. /// I1 OUTLINED_FUNCTION:
  6050. /// I2 --> B OUTLINED_FUNCTION I1
  6051. /// RET I2
  6052. /// RET
  6053. ///
  6054. /// * Call construction overhead: 1 (B)
  6055. /// * Frame construction overhead: 0 (Return included in sequence)
  6056. /// * Requires stack fixups? No
  6057. ///
  6058. /// \p MachineOutlinerNoLRSave implies that the function should be called using
  6059. /// a BL instruction, but doesn't require LR to be saved and restored. This
  6060. /// happens when LR is known to be dead.
  6061. ///
  6062. /// That is,
  6063. ///
  6064. /// I1 OUTLINED_FUNCTION:
  6065. /// I2 --> BL OUTLINED_FUNCTION I1
  6066. /// I3 I2
  6067. /// I3
  6068. /// RET
  6069. ///
  6070. /// * Call construction overhead: 1 (BL)
  6071. /// * Frame construction overhead: 1 (RET)
  6072. /// * Requires stack fixups? No
  6073. ///
  6074. /// \p MachineOutlinerThunk implies that the function is being created from
  6075. /// a sequence of instructions ending in a call. The outlined function is
  6076. /// called with a BL instruction, and the outlined function tail-calls the
  6077. /// original call destination.
  6078. ///
  6079. /// That is,
  6080. ///
  6081. /// I1 OUTLINED_FUNCTION:
  6082. /// I2 --> BL OUTLINED_FUNCTION I1
  6083. /// BL f I2
  6084. /// B f
  6085. /// * Call construction overhead: 1 (BL)
  6086. /// * Frame construction overhead: 0
  6087. /// * Requires stack fixups? No
  6088. ///
  6089. /// \p MachineOutlinerRegSave implies that the function should be called with a
  6090. /// save and restore of LR to an available register. This allows us to avoid
  6091. /// stack fixups. Note that this outlining variant is compatible with the
  6092. /// NoLRSave case.
  6093. ///
  6094. /// That is,
  6095. ///
  6096. /// I1 Save LR OUTLINED_FUNCTION:
  6097. /// I2 --> BL OUTLINED_FUNCTION I1
  6098. /// I3 Restore LR I2
  6099. /// I3
  6100. /// RET
  6101. ///
  6102. /// * Call construction overhead: 3 (save + BL + restore)
  6103. /// * Frame construction overhead: 1 (ret)
  6104. /// * Requires stack fixups? No
  6105. enum MachineOutlinerClass {
  6106. MachineOutlinerDefault, /// Emit a save, restore, call, and return.
  6107. MachineOutlinerTailCall, /// Only emit a branch.
  6108. MachineOutlinerNoLRSave, /// Emit a call and return.
  6109. MachineOutlinerThunk, /// Emit a call and tail-call.
  6110. MachineOutlinerRegSave /// Same as default, but save to a register.
  6111. };
  6112. enum MachineOutlinerMBBFlags {
  6113. LRUnavailableSomewhere = 0x2,
  6114. HasCalls = 0x4,
  6115. UnsafeRegsDead = 0x8
  6116. };
  6117. unsigned
  6118. AArch64InstrInfo::findRegisterToSaveLRTo(const outliner::Candidate &C) const {
  6119. assert(C.LRUWasSet && "LRU wasn't set?");
  6120. MachineFunction *MF = C.getMF();
  6121. const AArch64RegisterInfo *ARI = static_cast<const AArch64RegisterInfo *>(
  6122. MF->getSubtarget().getRegisterInfo());
  6123. // Check if there is an available register across the sequence that we can
  6124. // use.
  6125. for (unsigned Reg : AArch64::GPR64RegClass) {
  6126. if (!ARI->isReservedReg(*MF, Reg) &&
  6127. Reg != AArch64::LR && // LR is not reserved, but don't use it.
  6128. Reg != AArch64::X16 && // X16 is not guaranteed to be preserved.
  6129. Reg != AArch64::X17 && // Ditto for X17.
  6130. C.LRU.available(Reg) && C.UsedInSequence.available(Reg))
  6131. return Reg;
  6132. }
  6133. // No suitable register. Return 0.
  6134. return 0u;
  6135. }
  6136. static bool
  6137. outliningCandidatesSigningScopeConsensus(const outliner::Candidate &a,
  6138. const outliner::Candidate &b) {
  6139. const auto &MFIa = a.getMF()->getInfo<AArch64FunctionInfo>();
  6140. const auto &MFIb = b.getMF()->getInfo<AArch64FunctionInfo>();
  6141. return MFIa->shouldSignReturnAddress(false) == MFIb->shouldSignReturnAddress(false) &&
  6142. MFIa->shouldSignReturnAddress(true) == MFIb->shouldSignReturnAddress(true);
  6143. }
  6144. static bool
  6145. outliningCandidatesSigningKeyConsensus(const outliner::Candidate &a,
  6146. const outliner::Candidate &b) {
  6147. const auto &MFIa = a.getMF()->getInfo<AArch64FunctionInfo>();
  6148. const auto &MFIb = b.getMF()->getInfo<AArch64FunctionInfo>();
  6149. return MFIa->shouldSignWithBKey() == MFIb->shouldSignWithBKey();
  6150. }
  6151. static bool outliningCandidatesV8_3OpsConsensus(const outliner::Candidate &a,
  6152. const outliner::Candidate &b) {
  6153. const AArch64Subtarget &SubtargetA =
  6154. a.getMF()->getSubtarget<AArch64Subtarget>();
  6155. const AArch64Subtarget &SubtargetB =
  6156. b.getMF()->getSubtarget<AArch64Subtarget>();
  6157. return SubtargetA.hasV8_3aOps() == SubtargetB.hasV8_3aOps();
  6158. }
  6159. outliner::OutlinedFunction AArch64InstrInfo::getOutliningCandidateInfo(
  6160. std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
  6161. outliner::Candidate &FirstCand = RepeatedSequenceLocs[0];
  6162. unsigned SequenceSize =
  6163. std::accumulate(FirstCand.front(), std::next(FirstCand.back()), 0,
  6164. [this](unsigned Sum, const MachineInstr &MI) {
  6165. return Sum + getInstSizeInBytes(MI);
  6166. });
  6167. unsigned NumBytesToCreateFrame = 0;
  6168. // We only allow outlining for functions having exactly matching return
  6169. // address signing attributes, i.e., all share the same value for the
  6170. // attribute "sign-return-address" and all share the same type of key they
  6171. // are signed with.
  6172. // Additionally we require all functions to simultaniously either support
  6173. // v8.3a features or not. Otherwise an outlined function could get signed
  6174. // using dedicated v8.3 instructions and a call from a function that doesn't
  6175. // support v8.3 instructions would therefore be invalid.
  6176. if (std::adjacent_find(
  6177. RepeatedSequenceLocs.begin(), RepeatedSequenceLocs.end(),
  6178. [](const outliner::Candidate &a, const outliner::Candidate &b) {
  6179. // Return true if a and b are non-equal w.r.t. return address
  6180. // signing or support of v8.3a features
  6181. if (outliningCandidatesSigningScopeConsensus(a, b) &&
  6182. outliningCandidatesSigningKeyConsensus(a, b) &&
  6183. outliningCandidatesV8_3OpsConsensus(a, b)) {
  6184. return false;
  6185. }
  6186. return true;
  6187. }) != RepeatedSequenceLocs.end()) {
  6188. return outliner::OutlinedFunction();
  6189. }
  6190. // Since at this point all candidates agree on their return address signing
  6191. // picking just one is fine. If the candidate functions potentially sign their
  6192. // return addresses, the outlined function should do the same. Note that in
  6193. // the case of "sign-return-address"="non-leaf" this is an assumption: It is
  6194. // not certainly true that the outlined function will have to sign its return
  6195. // address but this decision is made later, when the decision to outline
  6196. // has already been made.
  6197. // The same holds for the number of additional instructions we need: On
  6198. // v8.3a RET can be replaced by RETAA/RETAB and no AUT instruction is
  6199. // necessary. However, at this point we don't know if the outlined function
  6200. // will have a RET instruction so we assume the worst.
  6201. const TargetRegisterInfo &TRI = getRegisterInfo();
  6202. if (FirstCand.getMF()
  6203. ->getInfo<AArch64FunctionInfo>()
  6204. ->shouldSignReturnAddress(true)) {
  6205. // One PAC and one AUT instructions
  6206. NumBytesToCreateFrame += 8;
  6207. // We have to check if sp modifying instructions would get outlined.
  6208. // If so we only allow outlining if sp is unchanged overall, so matching
  6209. // sub and add instructions are okay to outline, all other sp modifications
  6210. // are not
  6211. auto hasIllegalSPModification = [&TRI](outliner::Candidate &C) {
  6212. int SPValue = 0;
  6213. MachineBasicBlock::iterator MBBI = C.front();
  6214. for (;;) {
  6215. if (MBBI->modifiesRegister(AArch64::SP, &TRI)) {
  6216. switch (MBBI->getOpcode()) {
  6217. case AArch64::ADDXri:
  6218. case AArch64::ADDWri:
  6219. assert(MBBI->getNumOperands() == 4 && "Wrong number of operands");
  6220. assert(MBBI->getOperand(2).isImm() &&
  6221. "Expected operand to be immediate");
  6222. assert(MBBI->getOperand(1).isReg() &&
  6223. "Expected operand to be a register");
  6224. // Check if the add just increments sp. If so, we search for
  6225. // matching sub instructions that decrement sp. If not, the
  6226. // modification is illegal
  6227. if (MBBI->getOperand(1).getReg() == AArch64::SP)
  6228. SPValue += MBBI->getOperand(2).getImm();
  6229. else
  6230. return true;
  6231. break;
  6232. case AArch64::SUBXri:
  6233. case AArch64::SUBWri:
  6234. assert(MBBI->getNumOperands() == 4 && "Wrong number of operands");
  6235. assert(MBBI->getOperand(2).isImm() &&
  6236. "Expected operand to be immediate");
  6237. assert(MBBI->getOperand(1).isReg() &&
  6238. "Expected operand to be a register");
  6239. // Check if the sub just decrements sp. If so, we search for
  6240. // matching add instructions that increment sp. If not, the
  6241. // modification is illegal
  6242. if (MBBI->getOperand(1).getReg() == AArch64::SP)
  6243. SPValue -= MBBI->getOperand(2).getImm();
  6244. else
  6245. return true;
  6246. break;
  6247. default:
  6248. return true;
  6249. }
  6250. }
  6251. if (MBBI == C.back())
  6252. break;
  6253. ++MBBI;
  6254. }
  6255. if (SPValue)
  6256. return true;
  6257. return false;
  6258. };
  6259. // Remove candidates with illegal stack modifying instructions
  6260. llvm::erase_if(RepeatedSequenceLocs, hasIllegalSPModification);
  6261. // If the sequence doesn't have enough candidates left, then we're done.
  6262. if (RepeatedSequenceLocs.size() < 2)
  6263. return outliner::OutlinedFunction();
  6264. }
  6265. // Properties about candidate MBBs that hold for all of them.
  6266. unsigned FlagsSetInAll = 0xF;
  6267. // Compute liveness information for each candidate, and set FlagsSetInAll.
  6268. std::for_each(RepeatedSequenceLocs.begin(), RepeatedSequenceLocs.end(),
  6269. [&FlagsSetInAll](outliner::Candidate &C) {
  6270. FlagsSetInAll &= C.Flags;
  6271. });
  6272. // According to the AArch64 Procedure Call Standard, the following are
  6273. // undefined on entry/exit from a function call:
  6274. //
  6275. // * Registers x16, x17, (and thus w16, w17)
  6276. // * Condition codes (and thus the NZCV register)
  6277. //
  6278. // Because if this, we can't outline any sequence of instructions where
  6279. // one
  6280. // of these registers is live into/across it. Thus, we need to delete
  6281. // those
  6282. // candidates.
  6283. auto CantGuaranteeValueAcrossCall = [&TRI](outliner::Candidate &C) {
  6284. // If the unsafe registers in this block are all dead, then we don't need
  6285. // to compute liveness here.
  6286. if (C.Flags & UnsafeRegsDead)
  6287. return false;
  6288. C.initLRU(TRI);
  6289. LiveRegUnits LRU = C.LRU;
  6290. return (!LRU.available(AArch64::W16) || !LRU.available(AArch64::W17) ||
  6291. !LRU.available(AArch64::NZCV));
  6292. };
  6293. // Are there any candidates where those registers are live?
  6294. if (!(FlagsSetInAll & UnsafeRegsDead)) {
  6295. // Erase every candidate that violates the restrictions above. (It could be
  6296. // true that we have viable candidates, so it's not worth bailing out in
  6297. // the case that, say, 1 out of 20 candidates violate the restructions.)
  6298. llvm::erase_if(RepeatedSequenceLocs, CantGuaranteeValueAcrossCall);
  6299. // If the sequence doesn't have enough candidates left, then we're done.
  6300. if (RepeatedSequenceLocs.size() < 2)
  6301. return outliner::OutlinedFunction();
  6302. }
  6303. // At this point, we have only "safe" candidates to outline. Figure out
  6304. // frame + call instruction information.
  6305. unsigned LastInstrOpcode = RepeatedSequenceLocs[0].back()->getOpcode();
  6306. // Helper lambda which sets call information for every candidate.
  6307. auto SetCandidateCallInfo =
  6308. [&RepeatedSequenceLocs](unsigned CallID, unsigned NumBytesForCall) {
  6309. for (outliner::Candidate &C : RepeatedSequenceLocs)
  6310. C.setCallInfo(CallID, NumBytesForCall);
  6311. };
  6312. unsigned FrameID = MachineOutlinerDefault;
  6313. NumBytesToCreateFrame += 4;
  6314. bool HasBTI = any_of(RepeatedSequenceLocs, [](outliner::Candidate &C) {
  6315. return C.getMF()->getInfo<AArch64FunctionInfo>()->branchTargetEnforcement();
  6316. });
  6317. // We check to see if CFI Instructions are present, and if they are
  6318. // we find the number of CFI Instructions in the candidates.
  6319. unsigned CFICount = 0;
  6320. MachineBasicBlock::iterator MBBI = RepeatedSequenceLocs[0].front();
  6321. for (unsigned Loc = RepeatedSequenceLocs[0].getStartIdx();
  6322. Loc < RepeatedSequenceLocs[0].getEndIdx() + 1; Loc++) {
  6323. if (MBBI->isCFIInstruction())
  6324. CFICount++;
  6325. MBBI++;
  6326. }
  6327. // We compare the number of found CFI Instructions to the number of CFI
  6328. // instructions in the parent function for each candidate. We must check this
  6329. // since if we outline one of the CFI instructions in a function, we have to
  6330. // outline them all for correctness. If we do not, the address offsets will be
  6331. // incorrect between the two sections of the program.
  6332. for (outliner::Candidate &C : RepeatedSequenceLocs) {
  6333. std::vector<MCCFIInstruction> CFIInstructions =
  6334. C.getMF()->getFrameInstructions();
  6335. if (CFICount > 0 && CFICount != CFIInstructions.size())
  6336. return outliner::OutlinedFunction();
  6337. }
  6338. // Returns true if an instructions is safe to fix up, false otherwise.
  6339. auto IsSafeToFixup = [this, &TRI](MachineInstr &MI) {
  6340. if (MI.isCall())
  6341. return true;
  6342. if (!MI.modifiesRegister(AArch64::SP, &TRI) &&
  6343. !MI.readsRegister(AArch64::SP, &TRI))
  6344. return true;
  6345. // Any modification of SP will break our code to save/restore LR.
  6346. // FIXME: We could handle some instructions which add a constant
  6347. // offset to SP, with a bit more work.
  6348. if (MI.modifiesRegister(AArch64::SP, &TRI))
  6349. return false;
  6350. // At this point, we have a stack instruction that we might need to
  6351. // fix up. We'll handle it if it's a load or store.
  6352. if (MI.mayLoadOrStore()) {
  6353. const MachineOperand *Base; // Filled with the base operand of MI.
  6354. int64_t Offset; // Filled with the offset of MI.
  6355. bool OffsetIsScalable;
  6356. // Does it allow us to offset the base operand and is the base the
  6357. // register SP?
  6358. if (!getMemOperandWithOffset(MI, Base, Offset, OffsetIsScalable, &TRI) ||
  6359. !Base->isReg() || Base->getReg() != AArch64::SP)
  6360. return false;
  6361. // Fixe-up code below assumes bytes.
  6362. if (OffsetIsScalable)
  6363. return false;
  6364. // Find the minimum/maximum offset for this instruction and check
  6365. // if fixing it up would be in range.
  6366. int64_t MinOffset,
  6367. MaxOffset; // Unscaled offsets for the instruction.
  6368. TypeSize Scale(0U, false); // The scale to multiply the offsets by.
  6369. unsigned DummyWidth;
  6370. getMemOpInfo(MI.getOpcode(), Scale, DummyWidth, MinOffset, MaxOffset);
  6371. Offset += 16; // Update the offset to what it would be if we outlined.
  6372. if (Offset < MinOffset * (int64_t)Scale.getFixedSize() ||
  6373. Offset > MaxOffset * (int64_t)Scale.getFixedSize())
  6374. return false;
  6375. // It's in range, so we can outline it.
  6376. return true;
  6377. }
  6378. // FIXME: Add handling for instructions like "add x0, sp, #8".
  6379. // We can't fix it up, so don't outline it.
  6380. return false;
  6381. };
  6382. // True if it's possible to fix up each stack instruction in this sequence.
  6383. // Important for frames/call variants that modify the stack.
  6384. bool AllStackInstrsSafe = std::all_of(
  6385. FirstCand.front(), std::next(FirstCand.back()), IsSafeToFixup);
  6386. // If the last instruction in any candidate is a terminator, then we should
  6387. // tail call all of the candidates.
  6388. if (RepeatedSequenceLocs[0].back()->isTerminator()) {
  6389. FrameID = MachineOutlinerTailCall;
  6390. NumBytesToCreateFrame = 0;
  6391. SetCandidateCallInfo(MachineOutlinerTailCall, 4);
  6392. }
  6393. else if (LastInstrOpcode == AArch64::BL ||
  6394. ((LastInstrOpcode == AArch64::BLR ||
  6395. LastInstrOpcode == AArch64::BLRNoIP) &&
  6396. !HasBTI)) {
  6397. // FIXME: Do we need to check if the code after this uses the value of LR?
  6398. FrameID = MachineOutlinerThunk;
  6399. NumBytesToCreateFrame = 0;
  6400. SetCandidateCallInfo(MachineOutlinerThunk, 4);
  6401. }
  6402. else {
  6403. // We need to decide how to emit calls + frames. We can always emit the same
  6404. // frame if we don't need to save to the stack. If we have to save to the
  6405. // stack, then we need a different frame.
  6406. unsigned NumBytesNoStackCalls = 0;
  6407. std::vector<outliner::Candidate> CandidatesWithoutStackFixups;
  6408. // Check if we have to save LR.
  6409. for (outliner::Candidate &C : RepeatedSequenceLocs) {
  6410. C.initLRU(TRI);
  6411. // If we have a noreturn caller, then we're going to be conservative and
  6412. // say that we have to save LR. If we don't have a ret at the end of the
  6413. // block, then we can't reason about liveness accurately.
  6414. //
  6415. // FIXME: We can probably do better than always disabling this in
  6416. // noreturn functions by fixing up the liveness info.
  6417. bool IsNoReturn =
  6418. C.getMF()->getFunction().hasFnAttribute(Attribute::NoReturn);
  6419. // Is LR available? If so, we don't need a save.
  6420. if (C.LRU.available(AArch64::LR) && !IsNoReturn) {
  6421. NumBytesNoStackCalls += 4;
  6422. C.setCallInfo(MachineOutlinerNoLRSave, 4);
  6423. CandidatesWithoutStackFixups.push_back(C);
  6424. }
  6425. // Is an unused register available? If so, we won't modify the stack, so
  6426. // we can outline with the same frame type as those that don't save LR.
  6427. else if (findRegisterToSaveLRTo(C)) {
  6428. NumBytesNoStackCalls += 12;
  6429. C.setCallInfo(MachineOutlinerRegSave, 12);
  6430. CandidatesWithoutStackFixups.push_back(C);
  6431. }
  6432. // Is SP used in the sequence at all? If not, we don't have to modify
  6433. // the stack, so we are guaranteed to get the same frame.
  6434. else if (C.UsedInSequence.available(AArch64::SP)) {
  6435. NumBytesNoStackCalls += 12;
  6436. C.setCallInfo(MachineOutlinerDefault, 12);
  6437. CandidatesWithoutStackFixups.push_back(C);
  6438. }
  6439. // If we outline this, we need to modify the stack. Pretend we don't
  6440. // outline this by saving all of its bytes.
  6441. else {
  6442. NumBytesNoStackCalls += SequenceSize;
  6443. }
  6444. }
  6445. // If there are no places where we have to save LR, then note that we
  6446. // don't have to update the stack. Otherwise, give every candidate the
  6447. // default call type, as long as it's safe to do so.
  6448. if (!AllStackInstrsSafe ||
  6449. NumBytesNoStackCalls <= RepeatedSequenceLocs.size() * 12) {
  6450. RepeatedSequenceLocs = CandidatesWithoutStackFixups;
  6451. FrameID = MachineOutlinerNoLRSave;
  6452. } else {
  6453. SetCandidateCallInfo(MachineOutlinerDefault, 12);
  6454. // Bugzilla ID: 46767
  6455. // TODO: Check if fixing up the stack more than once is safe so we can
  6456. // outline these.
  6457. //
  6458. // An outline resulting in a caller that requires stack fixups at the
  6459. // callsite to a callee that also requires stack fixups can happen when
  6460. // there are no available registers at the candidate callsite for a
  6461. // candidate that itself also has calls.
  6462. //
  6463. // In other words if function_containing_sequence in the following pseudo
  6464. // assembly requires that we save LR at the point of the call, but there
  6465. // are no available registers: in this case we save using SP and as a
  6466. // result the SP offsets requires stack fixups by multiples of 16.
  6467. //
  6468. // function_containing_sequence:
  6469. // ...
  6470. // save LR to SP <- Requires stack instr fixups in OUTLINED_FUNCTION_N
  6471. // call OUTLINED_FUNCTION_N
  6472. // restore LR from SP
  6473. // ...
  6474. //
  6475. // OUTLINED_FUNCTION_N:
  6476. // save LR to SP <- Requires stack instr fixups in OUTLINED_FUNCTION_N
  6477. // ...
  6478. // bl foo
  6479. // restore LR from SP
  6480. // ret
  6481. //
  6482. // Because the code to handle more than one stack fixup does not
  6483. // currently have the proper checks for legality, these cases will assert
  6484. // in the AArch64 MachineOutliner. This is because the code to do this
  6485. // needs more hardening, testing, better checks that generated code is
  6486. // legal, etc and because it is only verified to handle a single pass of
  6487. // stack fixup.
  6488. //
  6489. // The assert happens in AArch64InstrInfo::buildOutlinedFrame to catch
  6490. // these cases until they are known to be handled. Bugzilla 46767 is
  6491. // referenced in comments at the assert site.
  6492. //
  6493. // To avoid asserting (or generating non-legal code on noassert builds)
  6494. // we remove all candidates which would need more than one stack fixup by
  6495. // pruning the cases where the candidate has calls while also having no
  6496. // available LR and having no available general purpose registers to copy
  6497. // LR to (ie one extra stack save/restore).
  6498. //
  6499. if (FlagsSetInAll & MachineOutlinerMBBFlags::HasCalls) {
  6500. erase_if(RepeatedSequenceLocs, [this](outliner::Candidate &C) {
  6501. return (std::any_of(
  6502. C.front(), std::next(C.back()),
  6503. [](const MachineInstr &MI) { return MI.isCall(); })) &&
  6504. (!C.LRU.available(AArch64::LR) || !findRegisterToSaveLRTo(C));
  6505. });
  6506. }
  6507. }
  6508. // If we dropped all of the candidates, bail out here.
  6509. if (RepeatedSequenceLocs.size() < 2) {
  6510. RepeatedSequenceLocs.clear();
  6511. return outliner::OutlinedFunction();
  6512. }
  6513. }
  6514. // Does every candidate's MBB contain a call? If so, then we might have a call
  6515. // in the range.
  6516. if (FlagsSetInAll & MachineOutlinerMBBFlags::HasCalls) {
  6517. // Check if the range contains a call. These require a save + restore of the
  6518. // link register.
  6519. bool ModStackToSaveLR = false;
  6520. if (std::any_of(FirstCand.front(), FirstCand.back(),
  6521. [](const MachineInstr &MI) { return MI.isCall(); }))
  6522. ModStackToSaveLR = true;
  6523. // Handle the last instruction separately. If this is a tail call, then the
  6524. // last instruction is a call. We don't want to save + restore in this case.
  6525. // However, it could be possible that the last instruction is a call without
  6526. // it being valid to tail call this sequence. We should consider this as
  6527. // well.
  6528. else if (FrameID != MachineOutlinerThunk &&
  6529. FrameID != MachineOutlinerTailCall && FirstCand.back()->isCall())
  6530. ModStackToSaveLR = true;
  6531. if (ModStackToSaveLR) {
  6532. // We can't fix up the stack. Bail out.
  6533. if (!AllStackInstrsSafe) {
  6534. RepeatedSequenceLocs.clear();
  6535. return outliner::OutlinedFunction();
  6536. }
  6537. // Save + restore LR.
  6538. NumBytesToCreateFrame += 8;
  6539. }
  6540. }
  6541. // If we have CFI instructions, we can only outline if the outlined section
  6542. // can be a tail call
  6543. if (FrameID != MachineOutlinerTailCall && CFICount > 0)
  6544. return outliner::OutlinedFunction();
  6545. return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize,
  6546. NumBytesToCreateFrame, FrameID);
  6547. }
  6548. bool AArch64InstrInfo::isFunctionSafeToOutlineFrom(
  6549. MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
  6550. const Function &F = MF.getFunction();
  6551. // Can F be deduplicated by the linker? If it can, don't outline from it.
  6552. if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
  6553. return false;
  6554. // Don't outline from functions with section markings; the program could
  6555. // expect that all the code is in the named section.
  6556. // FIXME: Allow outlining from multiple functions with the same section
  6557. // marking.
  6558. if (F.hasSection())
  6559. return false;
  6560. // Outlining from functions with redzones is unsafe since the outliner may
  6561. // modify the stack. Check if hasRedZone is true or unknown; if yes, don't
  6562. // outline from it.
  6563. AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
  6564. if (!AFI || AFI->hasRedZone().getValueOr(true))
  6565. return false;
  6566. // FIXME: Teach the outliner to generate/handle Windows unwind info.
  6567. if (MF.getTarget().getMCAsmInfo()->usesWindowsCFI())
  6568. return false;
  6569. // It's safe to outline from MF.
  6570. return true;
  6571. }
  6572. bool AArch64InstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
  6573. unsigned &Flags) const {
  6574. if (!TargetInstrInfo::isMBBSafeToOutlineFrom(MBB, Flags))
  6575. return false;
  6576. // Check if LR is available through all of the MBB. If it's not, then set
  6577. // a flag.
  6578. assert(MBB.getParent()->getRegInfo().tracksLiveness() &&
  6579. "Suitable Machine Function for outlining must track liveness");
  6580. LiveRegUnits LRU(getRegisterInfo());
  6581. std::for_each(MBB.rbegin(), MBB.rend(),
  6582. [&LRU](MachineInstr &MI) { LRU.accumulate(MI); });
  6583. // Check if each of the unsafe registers are available...
  6584. bool W16AvailableInBlock = LRU.available(AArch64::W16);
  6585. bool W17AvailableInBlock = LRU.available(AArch64::W17);
  6586. bool NZCVAvailableInBlock = LRU.available(AArch64::NZCV);
  6587. // If all of these are dead (and not live out), we know we don't have to check
  6588. // them later.
  6589. if (W16AvailableInBlock && W17AvailableInBlock && NZCVAvailableInBlock)
  6590. Flags |= MachineOutlinerMBBFlags::UnsafeRegsDead;
  6591. // Now, add the live outs to the set.
  6592. LRU.addLiveOuts(MBB);
  6593. // If any of these registers is available in the MBB, but also a live out of
  6594. // the block, then we know outlining is unsafe.
  6595. if (W16AvailableInBlock && !LRU.available(AArch64::W16))
  6596. return false;
  6597. if (W17AvailableInBlock && !LRU.available(AArch64::W17))
  6598. return false;
  6599. if (NZCVAvailableInBlock && !LRU.available(AArch64::NZCV))
  6600. return false;
  6601. // Check if there's a call inside this MachineBasicBlock. If there is, then
  6602. // set a flag.
  6603. if (any_of(MBB, [](MachineInstr &MI) { return MI.isCall(); }))
  6604. Flags |= MachineOutlinerMBBFlags::HasCalls;
  6605. MachineFunction *MF = MBB.getParent();
  6606. // In the event that we outline, we may have to save LR. If there is an
  6607. // available register in the MBB, then we'll always save LR there. Check if
  6608. // this is true.
  6609. bool CanSaveLR = false;
  6610. const AArch64RegisterInfo *ARI = static_cast<const AArch64RegisterInfo *>(
  6611. MF->getSubtarget().getRegisterInfo());
  6612. // Check if there is an available register across the sequence that we can
  6613. // use.
  6614. for (unsigned Reg : AArch64::GPR64RegClass) {
  6615. if (!ARI->isReservedReg(*MF, Reg) && Reg != AArch64::LR &&
  6616. Reg != AArch64::X16 && Reg != AArch64::X17 && LRU.available(Reg)) {
  6617. CanSaveLR = true;
  6618. break;
  6619. }
  6620. }
  6621. // Check if we have a register we can save LR to, and if LR was used
  6622. // somewhere. If both of those things are true, then we need to evaluate the
  6623. // safety of outlining stack instructions later.
  6624. if (!CanSaveLR && !LRU.available(AArch64::LR))
  6625. Flags |= MachineOutlinerMBBFlags::LRUnavailableSomewhere;
  6626. return true;
  6627. }
  6628. outliner::InstrType
  6629. AArch64InstrInfo::getOutliningType(MachineBasicBlock::iterator &MIT,
  6630. unsigned Flags) const {
  6631. MachineInstr &MI = *MIT;
  6632. MachineBasicBlock *MBB = MI.getParent();
  6633. MachineFunction *MF = MBB->getParent();
  6634. AArch64FunctionInfo *FuncInfo = MF->getInfo<AArch64FunctionInfo>();
  6635. // Don't outline anything used for return address signing. The outlined
  6636. // function will get signed later if needed
  6637. switch (MI.getOpcode()) {
  6638. case AArch64::PACIASP:
  6639. case AArch64::PACIBSP:
  6640. case AArch64::AUTIASP:
  6641. case AArch64::AUTIBSP:
  6642. case AArch64::RETAA:
  6643. case AArch64::RETAB:
  6644. case AArch64::EMITBKEY:
  6645. return outliner::InstrType::Illegal;
  6646. }
  6647. // Don't outline LOHs.
  6648. if (FuncInfo->getLOHRelated().count(&MI))
  6649. return outliner::InstrType::Illegal;
  6650. // We can only outline these if we will tail call the outlined function, or
  6651. // fix up the CFI offsets. Currently, CFI instructions are outlined only if
  6652. // in a tail call.
  6653. //
  6654. // FIXME: If the proper fixups for the offset are implemented, this should be
  6655. // possible.
  6656. if (MI.isCFIInstruction())
  6657. return outliner::InstrType::Legal;
  6658. // Don't allow debug values to impact outlining type.
  6659. if (MI.isDebugInstr() || MI.isIndirectDebugValue())
  6660. return outliner::InstrType::Invisible;
  6661. // At this point, KILL instructions don't really tell us much so we can go
  6662. // ahead and skip over them.
  6663. if (MI.isKill())
  6664. return outliner::InstrType::Invisible;
  6665. // Is this a terminator for a basic block?
  6666. if (MI.isTerminator()) {
  6667. // Is this the end of a function?
  6668. if (MI.getParent()->succ_empty())
  6669. return outliner::InstrType::Legal;
  6670. // It's not, so don't outline it.
  6671. return outliner::InstrType::Illegal;
  6672. }
  6673. // Make sure none of the operands are un-outlinable.
  6674. for (const MachineOperand &MOP : MI.operands()) {
  6675. if (MOP.isCPI() || MOP.isJTI() || MOP.isCFIIndex() || MOP.isFI() ||
  6676. MOP.isTargetIndex())
  6677. return outliner::InstrType::Illegal;
  6678. // If it uses LR or W30 explicitly, then don't touch it.
  6679. if (MOP.isReg() && !MOP.isImplicit() &&
  6680. (MOP.getReg() == AArch64::LR || MOP.getReg() == AArch64::W30))
  6681. return outliner::InstrType::Illegal;
  6682. }
  6683. // Special cases for instructions that can always be outlined, but will fail
  6684. // the later tests. e.g, ADRPs, which are PC-relative use LR, but can always
  6685. // be outlined because they don't require a *specific* value to be in LR.
  6686. if (MI.getOpcode() == AArch64::ADRP)
  6687. return outliner::InstrType::Legal;
  6688. // If MI is a call we might be able to outline it. We don't want to outline
  6689. // any calls that rely on the position of items on the stack. When we outline
  6690. // something containing a call, we have to emit a save and restore of LR in
  6691. // the outlined function. Currently, this always happens by saving LR to the
  6692. // stack. Thus, if we outline, say, half the parameters for a function call
  6693. // plus the call, then we'll break the callee's expectations for the layout
  6694. // of the stack.
  6695. //
  6696. // FIXME: Allow calls to functions which construct a stack frame, as long
  6697. // as they don't access arguments on the stack.
  6698. // FIXME: Figure out some way to analyze functions defined in other modules.
  6699. // We should be able to compute the memory usage based on the IR calling
  6700. // convention, even if we can't see the definition.
  6701. if (MI.isCall()) {
  6702. // Get the function associated with the call. Look at each operand and find
  6703. // the one that represents the callee and get its name.
  6704. const Function *Callee = nullptr;
  6705. for (const MachineOperand &MOP : MI.operands()) {
  6706. if (MOP.isGlobal()) {
  6707. Callee = dyn_cast<Function>(MOP.getGlobal());
  6708. break;
  6709. }
  6710. }
  6711. // Never outline calls to mcount. There isn't any rule that would require
  6712. // this, but the Linux kernel's "ftrace" feature depends on it.
  6713. if (Callee && Callee->getName() == "\01_mcount")
  6714. return outliner::InstrType::Illegal;
  6715. // If we don't know anything about the callee, assume it depends on the
  6716. // stack layout of the caller. In that case, it's only legal to outline
  6717. // as a tail-call. Explicitly list the call instructions we know about so we
  6718. // don't get unexpected results with call pseudo-instructions.
  6719. auto UnknownCallOutlineType = outliner::InstrType::Illegal;
  6720. if (MI.getOpcode() == AArch64::BLR ||
  6721. MI.getOpcode() == AArch64::BLRNoIP || MI.getOpcode() == AArch64::BL)
  6722. UnknownCallOutlineType = outliner::InstrType::LegalTerminator;
  6723. if (!Callee)
  6724. return UnknownCallOutlineType;
  6725. // We have a function we have information about. Check it if it's something
  6726. // can safely outline.
  6727. MachineFunction *CalleeMF = MF->getMMI().getMachineFunction(*Callee);
  6728. // We don't know what's going on with the callee at all. Don't touch it.
  6729. if (!CalleeMF)
  6730. return UnknownCallOutlineType;
  6731. // Check if we know anything about the callee saves on the function. If we
  6732. // don't, then don't touch it, since that implies that we haven't
  6733. // computed anything about its stack frame yet.
  6734. MachineFrameInfo &MFI = CalleeMF->getFrameInfo();
  6735. if (!MFI.isCalleeSavedInfoValid() || MFI.getStackSize() > 0 ||
  6736. MFI.getNumObjects() > 0)
  6737. return UnknownCallOutlineType;
  6738. // At this point, we can say that CalleeMF ought to not pass anything on the
  6739. // stack. Therefore, we can outline it.
  6740. return outliner::InstrType::Legal;
  6741. }
  6742. // Don't outline positions.
  6743. if (MI.isPosition())
  6744. return outliner::InstrType::Illegal;
  6745. // Don't touch the link register or W30.
  6746. if (MI.readsRegister(AArch64::W30, &getRegisterInfo()) ||
  6747. MI.modifiesRegister(AArch64::W30, &getRegisterInfo()))
  6748. return outliner::InstrType::Illegal;
  6749. // Don't outline BTI instructions, because that will prevent the outlining
  6750. // site from being indirectly callable.
  6751. if (MI.getOpcode() == AArch64::HINT) {
  6752. int64_t Imm = MI.getOperand(0).getImm();
  6753. if (Imm == 32 || Imm == 34 || Imm == 36 || Imm == 38)
  6754. return outliner::InstrType::Illegal;
  6755. }
  6756. return outliner::InstrType::Legal;
  6757. }
  6758. void AArch64InstrInfo::fixupPostOutline(MachineBasicBlock &MBB) const {
  6759. for (MachineInstr &MI : MBB) {
  6760. const MachineOperand *Base;
  6761. unsigned Width;
  6762. int64_t Offset;
  6763. bool OffsetIsScalable;
  6764. // Is this a load or store with an immediate offset with SP as the base?
  6765. if (!MI.mayLoadOrStore() ||
  6766. !getMemOperandWithOffsetWidth(MI, Base, Offset, OffsetIsScalable, Width,
  6767. &RI) ||
  6768. (Base->isReg() && Base->getReg() != AArch64::SP))
  6769. continue;
  6770. // It is, so we have to fix it up.
  6771. TypeSize Scale(0U, false);
  6772. int64_t Dummy1, Dummy2;
  6773. MachineOperand &StackOffsetOperand = getMemOpBaseRegImmOfsOffsetOperand(MI);
  6774. assert(StackOffsetOperand.isImm() && "Stack offset wasn't immediate!");
  6775. getMemOpInfo(MI.getOpcode(), Scale, Width, Dummy1, Dummy2);
  6776. assert(Scale != 0 && "Unexpected opcode!");
  6777. assert(!OffsetIsScalable && "Expected offset to be a byte offset");
  6778. // We've pushed the return address to the stack, so add 16 to the offset.
  6779. // This is safe, since we already checked if it would overflow when we
  6780. // checked if this instruction was legal to outline.
  6781. int64_t NewImm = (Offset + 16) / (int64_t)Scale.getFixedSize();
  6782. StackOffsetOperand.setImm(NewImm);
  6783. }
  6784. }
  6785. static void signOutlinedFunction(MachineFunction &MF, MachineBasicBlock &MBB,
  6786. bool ShouldSignReturnAddr,
  6787. bool ShouldSignReturnAddrWithAKey) {
  6788. if (ShouldSignReturnAddr) {
  6789. MachineBasicBlock::iterator MBBPAC = MBB.begin();
  6790. MachineBasicBlock::iterator MBBAUT = MBB.getFirstTerminator();
  6791. const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
  6792. const TargetInstrInfo *TII = Subtarget.getInstrInfo();
  6793. DebugLoc DL;
  6794. if (MBBAUT != MBB.end())
  6795. DL = MBBAUT->getDebugLoc();
  6796. // At the very beginning of the basic block we insert the following
  6797. // depending on the key type
  6798. //
  6799. // a_key: b_key:
  6800. // PACIASP EMITBKEY
  6801. // CFI_INSTRUCTION PACIBSP
  6802. // CFI_INSTRUCTION
  6803. unsigned PACI;
  6804. if (ShouldSignReturnAddrWithAKey) {
  6805. PACI = Subtarget.hasPAuth() ? AArch64::PACIA : AArch64::PACIASP;
  6806. } else {
  6807. BuildMI(MBB, MBBPAC, DebugLoc(), TII->get(AArch64::EMITBKEY))
  6808. .setMIFlag(MachineInstr::FrameSetup);
  6809. PACI = Subtarget.hasPAuth() ? AArch64::PACIB : AArch64::PACIBSP;
  6810. }
  6811. auto MI = BuildMI(MBB, MBBPAC, DebugLoc(), TII->get(PACI));
  6812. if (Subtarget.hasPAuth())
  6813. MI.addReg(AArch64::LR, RegState::Define)
  6814. .addReg(AArch64::LR)
  6815. .addReg(AArch64::SP, RegState::InternalRead);
  6816. MI.setMIFlag(MachineInstr::FrameSetup);
  6817. unsigned CFIIndex =
  6818. MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
  6819. BuildMI(MBB, MBBPAC, DebugLoc(), TII->get(AArch64::CFI_INSTRUCTION))
  6820. .addCFIIndex(CFIIndex)
  6821. .setMIFlags(MachineInstr::FrameSetup);
  6822. // If v8.3a features are available we can replace a RET instruction by
  6823. // RETAA or RETAB and omit the AUT instructions
  6824. if (Subtarget.hasPAuth() && MBBAUT != MBB.end() &&
  6825. MBBAUT->getOpcode() == AArch64::RET) {
  6826. BuildMI(MBB, MBBAUT, DL,
  6827. TII->get(ShouldSignReturnAddrWithAKey ? AArch64::RETAA
  6828. : AArch64::RETAB))
  6829. .copyImplicitOps(*MBBAUT);
  6830. MBB.erase(MBBAUT);
  6831. } else {
  6832. BuildMI(MBB, MBBAUT, DL,
  6833. TII->get(ShouldSignReturnAddrWithAKey ? AArch64::AUTIASP
  6834. : AArch64::AUTIBSP))
  6835. .setMIFlag(MachineInstr::FrameDestroy);
  6836. }
  6837. }
  6838. }
  6839. void AArch64InstrInfo::buildOutlinedFrame(
  6840. MachineBasicBlock &MBB, MachineFunction &MF,
  6841. const outliner::OutlinedFunction &OF) const {
  6842. AArch64FunctionInfo *FI = MF.getInfo<AArch64FunctionInfo>();
  6843. if (OF.FrameConstructionID == MachineOutlinerTailCall)
  6844. FI->setOutliningStyle("Tail Call");
  6845. else if (OF.FrameConstructionID == MachineOutlinerThunk) {
  6846. // For thunk outlining, rewrite the last instruction from a call to a
  6847. // tail-call.
  6848. MachineInstr *Call = &*--MBB.instr_end();
  6849. unsigned TailOpcode;
  6850. if (Call->getOpcode() == AArch64::BL) {
  6851. TailOpcode = AArch64::TCRETURNdi;
  6852. } else {
  6853. assert(Call->getOpcode() == AArch64::BLR ||
  6854. Call->getOpcode() == AArch64::BLRNoIP);
  6855. TailOpcode = AArch64::TCRETURNriALL;
  6856. }
  6857. MachineInstr *TC = BuildMI(MF, DebugLoc(), get(TailOpcode))
  6858. .add(Call->getOperand(0))
  6859. .addImm(0);
  6860. MBB.insert(MBB.end(), TC);
  6861. Call->eraseFromParent();
  6862. FI->setOutliningStyle("Thunk");
  6863. }
  6864. bool IsLeafFunction = true;
  6865. // Is there a call in the outlined range?
  6866. auto IsNonTailCall = [](const MachineInstr &MI) {
  6867. return MI.isCall() && !MI.isReturn();
  6868. };
  6869. if (llvm::any_of(MBB.instrs(), IsNonTailCall)) {
  6870. // Fix up the instructions in the range, since we're going to modify the
  6871. // stack.
  6872. // Bugzilla ID: 46767
  6873. // TODO: Check if fixing up twice is safe so we can outline these.
  6874. assert(OF.FrameConstructionID != MachineOutlinerDefault &&
  6875. "Can only fix up stack references once");
  6876. fixupPostOutline(MBB);
  6877. IsLeafFunction = false;
  6878. // LR has to be a live in so that we can save it.
  6879. if (!MBB.isLiveIn(AArch64::LR))
  6880. MBB.addLiveIn(AArch64::LR);
  6881. MachineBasicBlock::iterator It = MBB.begin();
  6882. MachineBasicBlock::iterator Et = MBB.end();
  6883. if (OF.FrameConstructionID == MachineOutlinerTailCall ||
  6884. OF.FrameConstructionID == MachineOutlinerThunk)
  6885. Et = std::prev(MBB.end());
  6886. // Insert a save before the outlined region
  6887. MachineInstr *STRXpre = BuildMI(MF, DebugLoc(), get(AArch64::STRXpre))
  6888. .addReg(AArch64::SP, RegState::Define)
  6889. .addReg(AArch64::LR)
  6890. .addReg(AArch64::SP)
  6891. .addImm(-16);
  6892. It = MBB.insert(It, STRXpre);
  6893. const TargetSubtargetInfo &STI = MF.getSubtarget();
  6894. const MCRegisterInfo *MRI = STI.getRegisterInfo();
  6895. unsigned DwarfReg = MRI->getDwarfRegNum(AArch64::LR, true);
  6896. // Add a CFI saying the stack was moved 16 B down.
  6897. int64_t StackPosEntry =
  6898. MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, 16));
  6899. BuildMI(MBB, It, DebugLoc(), get(AArch64::CFI_INSTRUCTION))
  6900. .addCFIIndex(StackPosEntry)
  6901. .setMIFlags(MachineInstr::FrameSetup);
  6902. // Add a CFI saying that the LR that we want to find is now 16 B higher than
  6903. // before.
  6904. int64_t LRPosEntry =
  6905. MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, DwarfReg, -16));
  6906. BuildMI(MBB, It, DebugLoc(), get(AArch64::CFI_INSTRUCTION))
  6907. .addCFIIndex(LRPosEntry)
  6908. .setMIFlags(MachineInstr::FrameSetup);
  6909. // Insert a restore before the terminator for the function.
  6910. MachineInstr *LDRXpost = BuildMI(MF, DebugLoc(), get(AArch64::LDRXpost))
  6911. .addReg(AArch64::SP, RegState::Define)
  6912. .addReg(AArch64::LR, RegState::Define)
  6913. .addReg(AArch64::SP)
  6914. .addImm(16);
  6915. Et = MBB.insert(Et, LDRXpost);
  6916. }
  6917. // If a bunch of candidates reach this point they must agree on their return
  6918. // address signing. It is therefore enough to just consider the signing
  6919. // behaviour of one of them
  6920. const auto &MFI = *OF.Candidates.front().getMF()->getInfo<AArch64FunctionInfo>();
  6921. bool ShouldSignReturnAddr = MFI.shouldSignReturnAddress(!IsLeafFunction);
  6922. // a_key is the default
  6923. bool ShouldSignReturnAddrWithAKey = !MFI.shouldSignWithBKey();
  6924. // If this is a tail call outlined function, then there's already a return.
  6925. if (OF.FrameConstructionID == MachineOutlinerTailCall ||
  6926. OF.FrameConstructionID == MachineOutlinerThunk) {
  6927. signOutlinedFunction(MF, MBB, ShouldSignReturnAddr,
  6928. ShouldSignReturnAddrWithAKey);
  6929. return;
  6930. }
  6931. // It's not a tail call, so we have to insert the return ourselves.
  6932. // LR has to be a live in so that we can return to it.
  6933. if (!MBB.isLiveIn(AArch64::LR))
  6934. MBB.addLiveIn(AArch64::LR);
  6935. MachineInstr *ret = BuildMI(MF, DebugLoc(), get(AArch64::RET))
  6936. .addReg(AArch64::LR);
  6937. MBB.insert(MBB.end(), ret);
  6938. signOutlinedFunction(MF, MBB, ShouldSignReturnAddr,
  6939. ShouldSignReturnAddrWithAKey);
  6940. FI->setOutliningStyle("Function");
  6941. // Did we have to modify the stack by saving the link register?
  6942. if (OF.FrameConstructionID != MachineOutlinerDefault)
  6943. return;
  6944. // We modified the stack.
  6945. // Walk over the basic block and fix up all the stack accesses.
  6946. fixupPostOutline(MBB);
  6947. }
  6948. MachineBasicBlock::iterator AArch64InstrInfo::insertOutlinedCall(
  6949. Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It,
  6950. MachineFunction &MF, const outliner::Candidate &C) const {
  6951. // Are we tail calling?
  6952. if (C.CallConstructionID == MachineOutlinerTailCall) {
  6953. // If yes, then we can just branch to the label.
  6954. It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::TCRETURNdi))
  6955. .addGlobalAddress(M.getNamedValue(MF.getName()))
  6956. .addImm(0));
  6957. return It;
  6958. }
  6959. // Are we saving the link register?
  6960. if (C.CallConstructionID == MachineOutlinerNoLRSave ||
  6961. C.CallConstructionID == MachineOutlinerThunk) {
  6962. // No, so just insert the call.
  6963. It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::BL))
  6964. .addGlobalAddress(M.getNamedValue(MF.getName())));
  6965. return It;
  6966. }
  6967. // We want to return the spot where we inserted the call.
  6968. MachineBasicBlock::iterator CallPt;
  6969. // Instructions for saving and restoring LR around the call instruction we're
  6970. // going to insert.
  6971. MachineInstr *Save;
  6972. MachineInstr *Restore;
  6973. // Can we save to a register?
  6974. if (C.CallConstructionID == MachineOutlinerRegSave) {
  6975. // FIXME: This logic should be sunk into a target-specific interface so that
  6976. // we don't have to recompute the register.
  6977. unsigned Reg = findRegisterToSaveLRTo(C);
  6978. assert(Reg != 0 && "No callee-saved register available?");
  6979. // LR has to be a live in so that we can save it.
  6980. if (!MBB.isLiveIn(AArch64::LR))
  6981. MBB.addLiveIn(AArch64::LR);
  6982. // Save and restore LR from Reg.
  6983. Save = BuildMI(MF, DebugLoc(), get(AArch64::ORRXrs), Reg)
  6984. .addReg(AArch64::XZR)
  6985. .addReg(AArch64::LR)
  6986. .addImm(0);
  6987. Restore = BuildMI(MF, DebugLoc(), get(AArch64::ORRXrs), AArch64::LR)
  6988. .addReg(AArch64::XZR)
  6989. .addReg(Reg)
  6990. .addImm(0);
  6991. } else {
  6992. // We have the default case. Save and restore from SP.
  6993. Save = BuildMI(MF, DebugLoc(), get(AArch64::STRXpre))
  6994. .addReg(AArch64::SP, RegState::Define)
  6995. .addReg(AArch64::LR)
  6996. .addReg(AArch64::SP)
  6997. .addImm(-16);
  6998. Restore = BuildMI(MF, DebugLoc(), get(AArch64::LDRXpost))
  6999. .addReg(AArch64::SP, RegState::Define)
  7000. .addReg(AArch64::LR, RegState::Define)
  7001. .addReg(AArch64::SP)
  7002. .addImm(16);
  7003. }
  7004. It = MBB.insert(It, Save);
  7005. It++;
  7006. // Insert the call.
  7007. It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(AArch64::BL))
  7008. .addGlobalAddress(M.getNamedValue(MF.getName())));
  7009. CallPt = It;
  7010. It++;
  7011. It = MBB.insert(It, Restore);
  7012. return CallPt;
  7013. }
  7014. bool AArch64InstrInfo::shouldOutlineFromFunctionByDefault(
  7015. MachineFunction &MF) const {
  7016. return MF.getFunction().hasMinSize();
  7017. }
  7018. Optional<DestSourcePair>
  7019. AArch64InstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
  7020. // AArch64::ORRWrs and AArch64::ORRXrs with WZR/XZR reg
  7021. // and zero immediate operands used as an alias for mov instruction.
  7022. if (MI.getOpcode() == AArch64::ORRWrs &&
  7023. MI.getOperand(1).getReg() == AArch64::WZR &&
  7024. MI.getOperand(3).getImm() == 0x0) {
  7025. return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
  7026. }
  7027. if (MI.getOpcode() == AArch64::ORRXrs &&
  7028. MI.getOperand(1).getReg() == AArch64::XZR &&
  7029. MI.getOperand(3).getImm() == 0x0) {
  7030. return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
  7031. }
  7032. return None;
  7033. }
  7034. Optional<RegImmPair> AArch64InstrInfo::isAddImmediate(const MachineInstr &MI,
  7035. Register Reg) const {
  7036. int Sign = 1;
  7037. int64_t Offset = 0;
  7038. // TODO: Handle cases where Reg is a super- or sub-register of the
  7039. // destination register.
  7040. const MachineOperand &Op0 = MI.getOperand(0);
  7041. if (!Op0.isReg() || Reg != Op0.getReg())
  7042. return None;
  7043. switch (MI.getOpcode()) {
  7044. default:
  7045. return None;
  7046. case AArch64::SUBWri:
  7047. case AArch64::SUBXri:
  7048. case AArch64::SUBSWri:
  7049. case AArch64::SUBSXri:
  7050. Sign *= -1;
  7051. LLVM_FALLTHROUGH;
  7052. case AArch64::ADDSWri:
  7053. case AArch64::ADDSXri:
  7054. case AArch64::ADDWri:
  7055. case AArch64::ADDXri: {
  7056. // TODO: Third operand can be global address (usually some string).
  7057. if (!MI.getOperand(0).isReg() || !MI.getOperand(1).isReg() ||
  7058. !MI.getOperand(2).isImm())
  7059. return None;
  7060. int Shift = MI.getOperand(3).getImm();
  7061. assert((Shift == 0 || Shift == 12) && "Shift can be either 0 or 12");
  7062. Offset = Sign * (MI.getOperand(2).getImm() << Shift);
  7063. }
  7064. }
  7065. return RegImmPair{MI.getOperand(1).getReg(), Offset};
  7066. }
  7067. /// If the given ORR instruction is a copy, and \p DescribedReg overlaps with
  7068. /// the destination register then, if possible, describe the value in terms of
  7069. /// the source register.
  7070. static Optional<ParamLoadedValue>
  7071. describeORRLoadedValue(const MachineInstr &MI, Register DescribedReg,
  7072. const TargetInstrInfo *TII,
  7073. const TargetRegisterInfo *TRI) {
  7074. auto DestSrc = TII->isCopyInstr(MI);
  7075. if (!DestSrc)
  7076. return None;
  7077. Register DestReg = DestSrc->Destination->getReg();
  7078. Register SrcReg = DestSrc->Source->getReg();
  7079. auto Expr = DIExpression::get(MI.getMF()->getFunction().getContext(), {});
  7080. // If the described register is the destination, just return the source.
  7081. if (DestReg == DescribedReg)
  7082. return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr);
  7083. // ORRWrs zero-extends to 64-bits, so we need to consider such cases.
  7084. if (MI.getOpcode() == AArch64::ORRWrs &&
  7085. TRI->isSuperRegister(DestReg, DescribedReg))
  7086. return ParamLoadedValue(MachineOperand::CreateReg(SrcReg, false), Expr);
  7087. // We may need to describe the lower part of a ORRXrs move.
  7088. if (MI.getOpcode() == AArch64::ORRXrs &&
  7089. TRI->isSubRegister(DestReg, DescribedReg)) {
  7090. Register SrcSubReg = TRI->getSubReg(SrcReg, AArch64::sub_32);
  7091. return ParamLoadedValue(MachineOperand::CreateReg(SrcSubReg, false), Expr);
  7092. }
  7093. assert(!TRI->isSuperOrSubRegisterEq(DestReg, DescribedReg) &&
  7094. "Unhandled ORR[XW]rs copy case");
  7095. return None;
  7096. }
  7097. Optional<ParamLoadedValue>
  7098. AArch64InstrInfo::describeLoadedValue(const MachineInstr &MI,
  7099. Register Reg) const {
  7100. const MachineFunction *MF = MI.getMF();
  7101. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  7102. switch (MI.getOpcode()) {
  7103. case AArch64::MOVZWi:
  7104. case AArch64::MOVZXi: {
  7105. // MOVZWi may be used for producing zero-extended 32-bit immediates in
  7106. // 64-bit parameters, so we need to consider super-registers.
  7107. if (!TRI->isSuperRegisterEq(MI.getOperand(0).getReg(), Reg))
  7108. return None;
  7109. if (!MI.getOperand(1).isImm())
  7110. return None;
  7111. int64_t Immediate = MI.getOperand(1).getImm();
  7112. int Shift = MI.getOperand(2).getImm();
  7113. return ParamLoadedValue(MachineOperand::CreateImm(Immediate << Shift),
  7114. nullptr);
  7115. }
  7116. case AArch64::ORRWrs:
  7117. case AArch64::ORRXrs:
  7118. return describeORRLoadedValue(MI, Reg, this, TRI);
  7119. }
  7120. return TargetInstrInfo::describeLoadedValue(MI, Reg);
  7121. }
  7122. bool AArch64InstrInfo::isExtendLikelyToBeFolded(
  7123. MachineInstr &ExtMI, MachineRegisterInfo &MRI) const {
  7124. assert(ExtMI.getOpcode() == TargetOpcode::G_SEXT ||
  7125. ExtMI.getOpcode() == TargetOpcode::G_ZEXT ||
  7126. ExtMI.getOpcode() == TargetOpcode::G_ANYEXT);
  7127. // Anyexts are nops.
  7128. if (ExtMI.getOpcode() == TargetOpcode::G_ANYEXT)
  7129. return true;
  7130. Register DefReg = ExtMI.getOperand(0).getReg();
  7131. if (!MRI.hasOneNonDBGUse(DefReg))
  7132. return false;
  7133. // It's likely that a sext/zext as a G_PTR_ADD offset will be folded into an
  7134. // addressing mode.
  7135. auto *UserMI = &*MRI.use_instr_nodbg_begin(DefReg);
  7136. return UserMI->getOpcode() == TargetOpcode::G_PTR_ADD;
  7137. }
  7138. uint64_t AArch64InstrInfo::getElementSizeForOpcode(unsigned Opc) const {
  7139. return get(Opc).TSFlags & AArch64::ElementSizeMask;
  7140. }
  7141. bool AArch64InstrInfo::isPTestLikeOpcode(unsigned Opc) const {
  7142. return get(Opc).TSFlags & AArch64::InstrFlagIsPTestLike;
  7143. }
  7144. bool AArch64InstrInfo::isWhileOpcode(unsigned Opc) const {
  7145. return get(Opc).TSFlags & AArch64::InstrFlagIsWhile;
  7146. }
  7147. unsigned int
  7148. AArch64InstrInfo::getTailDuplicateSize(CodeGenOpt::Level OptLevel) const {
  7149. return OptLevel >= CodeGenOpt::Aggressive ? 6 : 2;
  7150. }
  7151. unsigned llvm::getBLRCallOpcode(const MachineFunction &MF) {
  7152. if (MF.getSubtarget<AArch64Subtarget>().hardenSlsBlr())
  7153. return AArch64::BLRNoIP;
  7154. else
  7155. return AArch64::BLR;
  7156. }
  7157. #define GET_INSTRINFO_HELPERS
  7158. #define GET_INSTRMAP_INFO
  7159. #include "AArch64GenInstrInfo.inc"