AArch64ISelDAGToDAG.cpp 217 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958
  1. //===-- AArch64ISelDAGToDAG.cpp - A dag to dag inst selector for AArch64 --===//
  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 defines an instruction selector for the AArch64 target.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "AArch64MachineFunctionInfo.h"
  13. #include "AArch64TargetMachine.h"
  14. #include "MCTargetDesc/AArch64AddressingModes.h"
  15. #include "llvm/ADT/APSInt.h"
  16. #include "llvm/CodeGen/ISDOpcodes.h"
  17. #include "llvm/CodeGen/SelectionDAGISel.h"
  18. #include "llvm/IR/Function.h" // To access function attributes.
  19. #include "llvm/IR/GlobalValue.h"
  20. #include "llvm/IR/Intrinsics.h"
  21. #include "llvm/IR/IntrinsicsAArch64.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Support/ErrorHandling.h"
  24. #include "llvm/Support/KnownBits.h"
  25. #include "llvm/Support/MathExtras.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. using namespace llvm;
  28. #define DEBUG_TYPE "aarch64-isel"
  29. #define PASS_NAME "AArch64 Instruction Selection"
  30. //===--------------------------------------------------------------------===//
  31. /// AArch64DAGToDAGISel - AArch64 specific code to select AArch64 machine
  32. /// instructions for SelectionDAG operations.
  33. ///
  34. namespace {
  35. class AArch64DAGToDAGISel : public SelectionDAGISel {
  36. /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
  37. /// make the right decision when generating code for different targets.
  38. const AArch64Subtarget *Subtarget;
  39. public:
  40. static char ID;
  41. AArch64DAGToDAGISel() = delete;
  42. explicit AArch64DAGToDAGISel(AArch64TargetMachine &tm,
  43. CodeGenOpt::Level OptLevel)
  44. : SelectionDAGISel(ID, tm, OptLevel), Subtarget(nullptr) {}
  45. bool runOnMachineFunction(MachineFunction &MF) override {
  46. Subtarget = &MF.getSubtarget<AArch64Subtarget>();
  47. return SelectionDAGISel::runOnMachineFunction(MF);
  48. }
  49. void Select(SDNode *Node) override;
  50. /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
  51. /// inline asm expressions.
  52. bool SelectInlineAsmMemoryOperand(const SDValue &Op,
  53. unsigned ConstraintID,
  54. std::vector<SDValue> &OutOps) override;
  55. template <signed Low, signed High, signed Scale>
  56. bool SelectRDVLImm(SDValue N, SDValue &Imm);
  57. bool tryMLAV64LaneV128(SDNode *N);
  58. bool tryMULLV64LaneV128(unsigned IntNo, SDNode *N);
  59. bool SelectArithExtendedRegister(SDValue N, SDValue &Reg, SDValue &Shift);
  60. bool SelectArithUXTXRegister(SDValue N, SDValue &Reg, SDValue &Shift);
  61. bool SelectArithImmed(SDValue N, SDValue &Val, SDValue &Shift);
  62. bool SelectNegArithImmed(SDValue N, SDValue &Val, SDValue &Shift);
  63. bool SelectArithShiftedRegister(SDValue N, SDValue &Reg, SDValue &Shift) {
  64. return SelectShiftedRegister(N, false, Reg, Shift);
  65. }
  66. bool SelectLogicalShiftedRegister(SDValue N, SDValue &Reg, SDValue &Shift) {
  67. return SelectShiftedRegister(N, true, Reg, Shift);
  68. }
  69. bool SelectAddrModeIndexed7S8(SDValue N, SDValue &Base, SDValue &OffImm) {
  70. return SelectAddrModeIndexed7S(N, 1, Base, OffImm);
  71. }
  72. bool SelectAddrModeIndexed7S16(SDValue N, SDValue &Base, SDValue &OffImm) {
  73. return SelectAddrModeIndexed7S(N, 2, Base, OffImm);
  74. }
  75. bool SelectAddrModeIndexed7S32(SDValue N, SDValue &Base, SDValue &OffImm) {
  76. return SelectAddrModeIndexed7S(N, 4, Base, OffImm);
  77. }
  78. bool SelectAddrModeIndexed7S64(SDValue N, SDValue &Base, SDValue &OffImm) {
  79. return SelectAddrModeIndexed7S(N, 8, Base, OffImm);
  80. }
  81. bool SelectAddrModeIndexed7S128(SDValue N, SDValue &Base, SDValue &OffImm) {
  82. return SelectAddrModeIndexed7S(N, 16, Base, OffImm);
  83. }
  84. bool SelectAddrModeIndexedS9S128(SDValue N, SDValue &Base, SDValue &OffImm) {
  85. return SelectAddrModeIndexedBitWidth(N, true, 9, 16, Base, OffImm);
  86. }
  87. bool SelectAddrModeIndexedU6S128(SDValue N, SDValue &Base, SDValue &OffImm) {
  88. return SelectAddrModeIndexedBitWidth(N, false, 6, 16, Base, OffImm);
  89. }
  90. bool SelectAddrModeIndexed8(SDValue N, SDValue &Base, SDValue &OffImm) {
  91. return SelectAddrModeIndexed(N, 1, Base, OffImm);
  92. }
  93. bool SelectAddrModeIndexed16(SDValue N, SDValue &Base, SDValue &OffImm) {
  94. return SelectAddrModeIndexed(N, 2, Base, OffImm);
  95. }
  96. bool SelectAddrModeIndexed32(SDValue N, SDValue &Base, SDValue &OffImm) {
  97. return SelectAddrModeIndexed(N, 4, Base, OffImm);
  98. }
  99. bool SelectAddrModeIndexed64(SDValue N, SDValue &Base, SDValue &OffImm) {
  100. return SelectAddrModeIndexed(N, 8, Base, OffImm);
  101. }
  102. bool SelectAddrModeIndexed128(SDValue N, SDValue &Base, SDValue &OffImm) {
  103. return SelectAddrModeIndexed(N, 16, Base, OffImm);
  104. }
  105. bool SelectAddrModeUnscaled8(SDValue N, SDValue &Base, SDValue &OffImm) {
  106. return SelectAddrModeUnscaled(N, 1, Base, OffImm);
  107. }
  108. bool SelectAddrModeUnscaled16(SDValue N, SDValue &Base, SDValue &OffImm) {
  109. return SelectAddrModeUnscaled(N, 2, Base, OffImm);
  110. }
  111. bool SelectAddrModeUnscaled32(SDValue N, SDValue &Base, SDValue &OffImm) {
  112. return SelectAddrModeUnscaled(N, 4, Base, OffImm);
  113. }
  114. bool SelectAddrModeUnscaled64(SDValue N, SDValue &Base, SDValue &OffImm) {
  115. return SelectAddrModeUnscaled(N, 8, Base, OffImm);
  116. }
  117. bool SelectAddrModeUnscaled128(SDValue N, SDValue &Base, SDValue &OffImm) {
  118. return SelectAddrModeUnscaled(N, 16, Base, OffImm);
  119. }
  120. template <unsigned Size, unsigned Max>
  121. bool SelectAddrModeIndexedUImm(SDValue N, SDValue &Base, SDValue &OffImm) {
  122. // Test if there is an appropriate addressing mode and check if the
  123. // immediate fits.
  124. bool Found = SelectAddrModeIndexed(N, Size, Base, OffImm);
  125. if (Found) {
  126. if (auto *CI = dyn_cast<ConstantSDNode>(OffImm)) {
  127. int64_t C = CI->getSExtValue();
  128. if (C <= Max)
  129. return true;
  130. }
  131. }
  132. // Otherwise, base only, materialize address in register.
  133. Base = N;
  134. OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i64);
  135. return true;
  136. }
  137. template<int Width>
  138. bool SelectAddrModeWRO(SDValue N, SDValue &Base, SDValue &Offset,
  139. SDValue &SignExtend, SDValue &DoShift) {
  140. return SelectAddrModeWRO(N, Width / 8, Base, Offset, SignExtend, DoShift);
  141. }
  142. template<int Width>
  143. bool SelectAddrModeXRO(SDValue N, SDValue &Base, SDValue &Offset,
  144. SDValue &SignExtend, SDValue &DoShift) {
  145. return SelectAddrModeXRO(N, Width / 8, Base, Offset, SignExtend, DoShift);
  146. }
  147. bool SelectExtractHigh(SDValue N, SDValue &Res) {
  148. if (Subtarget->isLittleEndian() && N->getOpcode() == ISD::BITCAST)
  149. N = N->getOperand(0);
  150. if (N->getOpcode() != ISD::EXTRACT_SUBVECTOR ||
  151. !isa<ConstantSDNode>(N->getOperand(1)))
  152. return false;
  153. EVT VT = N->getValueType(0);
  154. EVT LVT = N->getOperand(0).getValueType();
  155. unsigned Index = N->getConstantOperandVal(1);
  156. if (!VT.is64BitVector() || !LVT.is128BitVector() ||
  157. Index != VT.getVectorNumElements())
  158. return false;
  159. Res = N->getOperand(0);
  160. return true;
  161. }
  162. bool SelectRoundingVLShr(SDValue N, SDValue &Res1, SDValue &Res2) {
  163. if (N.getOpcode() != AArch64ISD::VLSHR)
  164. return false;
  165. SDValue Op = N->getOperand(0);
  166. EVT VT = Op.getValueType();
  167. unsigned ShtAmt = N->getConstantOperandVal(1);
  168. if (ShtAmt > VT.getScalarSizeInBits() / 2 || Op.getOpcode() != ISD::ADD)
  169. return false;
  170. APInt Imm;
  171. if (Op.getOperand(1).getOpcode() == AArch64ISD::MOVIshift)
  172. Imm = APInt(VT.getScalarSizeInBits(),
  173. Op.getOperand(1).getConstantOperandVal(0)
  174. << Op.getOperand(1).getConstantOperandVal(1));
  175. else if (Op.getOperand(1).getOpcode() == AArch64ISD::DUP &&
  176. isa<ConstantSDNode>(Op.getOperand(1).getOperand(0)))
  177. Imm = APInt(VT.getScalarSizeInBits(),
  178. Op.getOperand(1).getConstantOperandVal(0));
  179. else
  180. return false;
  181. if (Imm != 1ULL << (ShtAmt - 1))
  182. return false;
  183. Res1 = Op.getOperand(0);
  184. Res2 = CurDAG->getTargetConstant(ShtAmt, SDLoc(N), MVT::i32);
  185. return true;
  186. }
  187. bool SelectDupZeroOrUndef(SDValue N) {
  188. switch(N->getOpcode()) {
  189. case ISD::UNDEF:
  190. return true;
  191. case AArch64ISD::DUP:
  192. case ISD::SPLAT_VECTOR: {
  193. auto Opnd0 = N->getOperand(0);
  194. if (isNullConstant(Opnd0))
  195. return true;
  196. if (isNullFPConstant(Opnd0))
  197. return true;
  198. break;
  199. }
  200. default:
  201. break;
  202. }
  203. return false;
  204. }
  205. bool SelectDupZero(SDValue N) {
  206. switch(N->getOpcode()) {
  207. case AArch64ISD::DUP:
  208. case ISD::SPLAT_VECTOR: {
  209. auto Opnd0 = N->getOperand(0);
  210. if (isNullConstant(Opnd0))
  211. return true;
  212. if (isNullFPConstant(Opnd0))
  213. return true;
  214. break;
  215. }
  216. }
  217. return false;
  218. }
  219. template<MVT::SimpleValueType VT>
  220. bool SelectSVEAddSubImm(SDValue N, SDValue &Imm, SDValue &Shift) {
  221. return SelectSVEAddSubImm(N, VT, Imm, Shift);
  222. }
  223. template <MVT::SimpleValueType VT>
  224. bool SelectSVECpyDupImm(SDValue N, SDValue &Imm, SDValue &Shift) {
  225. return SelectSVECpyDupImm(N, VT, Imm, Shift);
  226. }
  227. template <MVT::SimpleValueType VT, bool Invert = false>
  228. bool SelectSVELogicalImm(SDValue N, SDValue &Imm) {
  229. return SelectSVELogicalImm(N, VT, Imm, Invert);
  230. }
  231. template <MVT::SimpleValueType VT>
  232. bool SelectSVEArithImm(SDValue N, SDValue &Imm) {
  233. return SelectSVEArithImm(N, VT, Imm);
  234. }
  235. template <unsigned Low, unsigned High, bool AllowSaturation = false>
  236. bool SelectSVEShiftImm(SDValue N, SDValue &Imm) {
  237. return SelectSVEShiftImm(N, Low, High, AllowSaturation, Imm);
  238. }
  239. bool SelectSVEShiftSplatImmR(SDValue N, SDValue &Imm) {
  240. if (N->getOpcode() != ISD::SPLAT_VECTOR)
  241. return false;
  242. EVT EltVT = N->getValueType(0).getVectorElementType();
  243. return SelectSVEShiftImm(N->getOperand(0), /* Low */ 1,
  244. /* High */ EltVT.getFixedSizeInBits(),
  245. /* AllowSaturation */ true, Imm);
  246. }
  247. // Returns a suitable CNT/INC/DEC/RDVL multiplier to calculate VSCALE*N.
  248. template<signed Min, signed Max, signed Scale, bool Shift>
  249. bool SelectCntImm(SDValue N, SDValue &Imm) {
  250. if (!isa<ConstantSDNode>(N))
  251. return false;
  252. int64_t MulImm = cast<ConstantSDNode>(N)->getSExtValue();
  253. if (Shift)
  254. MulImm = 1LL << MulImm;
  255. if ((MulImm % std::abs(Scale)) != 0)
  256. return false;
  257. MulImm /= Scale;
  258. if ((MulImm >= Min) && (MulImm <= Max)) {
  259. Imm = CurDAG->getTargetConstant(MulImm, SDLoc(N), MVT::i32);
  260. return true;
  261. }
  262. return false;
  263. }
  264. template <signed Max, signed Scale>
  265. bool SelectEXTImm(SDValue N, SDValue &Imm) {
  266. if (!isa<ConstantSDNode>(N))
  267. return false;
  268. int64_t MulImm = cast<ConstantSDNode>(N)->getSExtValue();
  269. if (MulImm >= 0 && MulImm <= Max) {
  270. MulImm *= Scale;
  271. Imm = CurDAG->getTargetConstant(MulImm, SDLoc(N), MVT::i32);
  272. return true;
  273. }
  274. return false;
  275. }
  276. template <unsigned BaseReg> bool ImmToTile(SDValue N, SDValue &Imm) {
  277. if (auto *CI = dyn_cast<ConstantSDNode>(N)) {
  278. uint64_t C = CI->getZExtValue();
  279. Imm = CurDAG->getRegister(BaseReg + C, MVT::Other);
  280. return true;
  281. }
  282. return false;
  283. }
  284. /// Form sequences of consecutive 64/128-bit registers for use in NEON
  285. /// instructions making use of a vector-list (e.g. ldN, tbl). Vecs must have
  286. /// between 1 and 4 elements. If it contains a single element that is returned
  287. /// unchanged; otherwise a REG_SEQUENCE value is returned.
  288. SDValue createDTuple(ArrayRef<SDValue> Vecs);
  289. SDValue createQTuple(ArrayRef<SDValue> Vecs);
  290. // Form a sequence of SVE registers for instructions using list of vectors,
  291. // e.g. structured loads and stores (ldN, stN).
  292. SDValue createZTuple(ArrayRef<SDValue> Vecs);
  293. /// Generic helper for the createDTuple/createQTuple
  294. /// functions. Those should almost always be called instead.
  295. SDValue createTuple(ArrayRef<SDValue> Vecs, const unsigned RegClassIDs[],
  296. const unsigned SubRegs[]);
  297. void SelectTable(SDNode *N, unsigned NumVecs, unsigned Opc, bool isExt);
  298. bool tryIndexedLoad(SDNode *N);
  299. bool trySelectStackSlotTagP(SDNode *N);
  300. void SelectTagP(SDNode *N);
  301. void SelectLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
  302. unsigned SubRegIdx);
  303. void SelectPostLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
  304. unsigned SubRegIdx);
  305. void SelectLoadLane(SDNode *N, unsigned NumVecs, unsigned Opc);
  306. void SelectPostLoadLane(SDNode *N, unsigned NumVecs, unsigned Opc);
  307. void SelectPredicatedLoad(SDNode *N, unsigned NumVecs, unsigned Scale,
  308. unsigned Opc_rr, unsigned Opc_ri,
  309. bool IsIntr = false);
  310. void SelectWhilePair(SDNode *N, unsigned Opc);
  311. void SelectCVTIntrinsic(SDNode *N, unsigned NumVecs, unsigned Opcode);
  312. bool SelectAddrModeFrameIndexSVE(SDValue N, SDValue &Base, SDValue &OffImm);
  313. /// SVE Reg+Imm addressing mode.
  314. template <int64_t Min, int64_t Max>
  315. bool SelectAddrModeIndexedSVE(SDNode *Root, SDValue N, SDValue &Base,
  316. SDValue &OffImm);
  317. /// SVE Reg+Reg address mode.
  318. template <unsigned Scale>
  319. bool SelectSVERegRegAddrMode(SDValue N, SDValue &Base, SDValue &Offset) {
  320. return SelectSVERegRegAddrMode(N, Scale, Base, Offset);
  321. }
  322. template <unsigned MaxIdx, unsigned Scale>
  323. bool SelectSMETileSlice(SDValue N, SDValue &Vector, SDValue &Offset) {
  324. return SelectSMETileSlice(N, MaxIdx, Vector, Offset, Scale);
  325. }
  326. void SelectStore(SDNode *N, unsigned NumVecs, unsigned Opc);
  327. void SelectPostStore(SDNode *N, unsigned NumVecs, unsigned Opc);
  328. void SelectStoreLane(SDNode *N, unsigned NumVecs, unsigned Opc);
  329. void SelectPostStoreLane(SDNode *N, unsigned NumVecs, unsigned Opc);
  330. void SelectPredicatedStore(SDNode *N, unsigned NumVecs, unsigned Scale,
  331. unsigned Opc_rr, unsigned Opc_ri);
  332. std::tuple<unsigned, SDValue, SDValue>
  333. findAddrModeSVELoadStore(SDNode *N, unsigned Opc_rr, unsigned Opc_ri,
  334. const SDValue &OldBase, const SDValue &OldOffset,
  335. unsigned Scale);
  336. bool tryBitfieldExtractOp(SDNode *N);
  337. bool tryBitfieldExtractOpFromSExt(SDNode *N);
  338. bool tryBitfieldInsertOp(SDNode *N);
  339. bool tryBitfieldInsertInZeroOp(SDNode *N);
  340. bool tryShiftAmountMod(SDNode *N);
  341. bool tryHighFPExt(SDNode *N);
  342. bool tryReadRegister(SDNode *N);
  343. bool tryWriteRegister(SDNode *N);
  344. // Include the pieces autogenerated from the target description.
  345. #include "AArch64GenDAGISel.inc"
  346. private:
  347. bool SelectShiftedRegister(SDValue N, bool AllowROR, SDValue &Reg,
  348. SDValue &Shift);
  349. bool SelectShiftedRegisterFromAnd(SDValue N, SDValue &Reg, SDValue &Shift);
  350. bool SelectAddrModeIndexed7S(SDValue N, unsigned Size, SDValue &Base,
  351. SDValue &OffImm) {
  352. return SelectAddrModeIndexedBitWidth(N, true, 7, Size, Base, OffImm);
  353. }
  354. bool SelectAddrModeIndexedBitWidth(SDValue N, bool IsSignedImm, unsigned BW,
  355. unsigned Size, SDValue &Base,
  356. SDValue &OffImm);
  357. bool SelectAddrModeIndexed(SDValue N, unsigned Size, SDValue &Base,
  358. SDValue &OffImm);
  359. bool SelectAddrModeUnscaled(SDValue N, unsigned Size, SDValue &Base,
  360. SDValue &OffImm);
  361. bool SelectAddrModeWRO(SDValue N, unsigned Size, SDValue &Base,
  362. SDValue &Offset, SDValue &SignExtend,
  363. SDValue &DoShift);
  364. bool SelectAddrModeXRO(SDValue N, unsigned Size, SDValue &Base,
  365. SDValue &Offset, SDValue &SignExtend,
  366. SDValue &DoShift);
  367. bool isWorthFolding(SDValue V) const;
  368. bool SelectExtendedSHL(SDValue N, unsigned Size, bool WantExtend,
  369. SDValue &Offset, SDValue &SignExtend);
  370. template<unsigned RegWidth>
  371. bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos) {
  372. return SelectCVTFixedPosOperand(N, FixedPos, RegWidth);
  373. }
  374. bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos, unsigned Width);
  375. bool SelectCMP_SWAP(SDNode *N);
  376. bool SelectSVEAddSubImm(SDValue N, MVT VT, SDValue &Imm, SDValue &Shift);
  377. bool SelectSVECpyDupImm(SDValue N, MVT VT, SDValue &Imm, SDValue &Shift);
  378. bool SelectSVELogicalImm(SDValue N, MVT VT, SDValue &Imm, bool Invert);
  379. bool SelectSVESignedArithImm(SDValue N, SDValue &Imm);
  380. bool SelectSVEShiftImm(SDValue N, uint64_t Low, uint64_t High,
  381. bool AllowSaturation, SDValue &Imm);
  382. bool SelectSVEArithImm(SDValue N, MVT VT, SDValue &Imm);
  383. bool SelectSVERegRegAddrMode(SDValue N, unsigned Scale, SDValue &Base,
  384. SDValue &Offset);
  385. bool SelectSMETileSlice(SDValue N, unsigned MaxSize, SDValue &Vector,
  386. SDValue &Offset, unsigned Scale = 1);
  387. bool SelectAllActivePredicate(SDValue N);
  388. };
  389. } // end anonymous namespace
  390. char AArch64DAGToDAGISel::ID = 0;
  391. INITIALIZE_PASS(AArch64DAGToDAGISel, DEBUG_TYPE, PASS_NAME, false, false)
  392. /// isIntImmediate - This method tests to see if the node is a constant
  393. /// operand. If so Imm will receive the 32-bit value.
  394. static bool isIntImmediate(const SDNode *N, uint64_t &Imm) {
  395. if (const ConstantSDNode *C = dyn_cast<const ConstantSDNode>(N)) {
  396. Imm = C->getZExtValue();
  397. return true;
  398. }
  399. return false;
  400. }
  401. // isIntImmediate - This method tests to see if a constant operand.
  402. // If so Imm will receive the value.
  403. static bool isIntImmediate(SDValue N, uint64_t &Imm) {
  404. return isIntImmediate(N.getNode(), Imm);
  405. }
  406. // isOpcWithIntImmediate - This method tests to see if the node is a specific
  407. // opcode and that it has a immediate integer right operand.
  408. // If so Imm will receive the 32 bit value.
  409. static bool isOpcWithIntImmediate(const SDNode *N, unsigned Opc,
  410. uint64_t &Imm) {
  411. return N->getOpcode() == Opc &&
  412. isIntImmediate(N->getOperand(1).getNode(), Imm);
  413. }
  414. // isIntImmediateEq - This method tests to see if N is a constant operand that
  415. // is equivalent to 'ImmExpected'.
  416. #ifndef NDEBUG
  417. static bool isIntImmediateEq(SDValue N, const uint64_t ImmExpected) {
  418. uint64_t Imm;
  419. if (!isIntImmediate(N.getNode(), Imm))
  420. return false;
  421. return Imm == ImmExpected;
  422. }
  423. #endif
  424. bool AArch64DAGToDAGISel::SelectInlineAsmMemoryOperand(
  425. const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) {
  426. switch(ConstraintID) {
  427. default:
  428. llvm_unreachable("Unexpected asm memory constraint");
  429. case InlineAsm::Constraint_m:
  430. case InlineAsm::Constraint_o:
  431. case InlineAsm::Constraint_Q:
  432. // We need to make sure that this one operand does not end up in XZR, thus
  433. // require the address to be in a PointerRegClass register.
  434. const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
  435. const TargetRegisterClass *TRC = TRI->getPointerRegClass(*MF);
  436. SDLoc dl(Op);
  437. SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i64);
  438. SDValue NewOp =
  439. SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
  440. dl, Op.getValueType(),
  441. Op, RC), 0);
  442. OutOps.push_back(NewOp);
  443. return false;
  444. }
  445. return true;
  446. }
  447. /// SelectArithImmed - Select an immediate value that can be represented as
  448. /// a 12-bit value shifted left by either 0 or 12. If so, return true with
  449. /// Val set to the 12-bit value and Shift set to the shifter operand.
  450. bool AArch64DAGToDAGISel::SelectArithImmed(SDValue N, SDValue &Val,
  451. SDValue &Shift) {
  452. // This function is called from the addsub_shifted_imm ComplexPattern,
  453. // which lists [imm] as the list of opcode it's interested in, however
  454. // we still need to check whether the operand is actually an immediate
  455. // here because the ComplexPattern opcode list is only used in
  456. // root-level opcode matching.
  457. if (!isa<ConstantSDNode>(N.getNode()))
  458. return false;
  459. uint64_t Immed = cast<ConstantSDNode>(N.getNode())->getZExtValue();
  460. unsigned ShiftAmt;
  461. if (Immed >> 12 == 0) {
  462. ShiftAmt = 0;
  463. } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
  464. ShiftAmt = 12;
  465. Immed = Immed >> 12;
  466. } else
  467. return false;
  468. unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
  469. SDLoc dl(N);
  470. Val = CurDAG->getTargetConstant(Immed, dl, MVT::i32);
  471. Shift = CurDAG->getTargetConstant(ShVal, dl, MVT::i32);
  472. return true;
  473. }
  474. /// SelectNegArithImmed - As above, but negates the value before trying to
  475. /// select it.
  476. bool AArch64DAGToDAGISel::SelectNegArithImmed(SDValue N, SDValue &Val,
  477. SDValue &Shift) {
  478. // This function is called from the addsub_shifted_imm ComplexPattern,
  479. // which lists [imm] as the list of opcode it's interested in, however
  480. // we still need to check whether the operand is actually an immediate
  481. // here because the ComplexPattern opcode list is only used in
  482. // root-level opcode matching.
  483. if (!isa<ConstantSDNode>(N.getNode()))
  484. return false;
  485. // The immediate operand must be a 24-bit zero-extended immediate.
  486. uint64_t Immed = cast<ConstantSDNode>(N.getNode())->getZExtValue();
  487. // This negation is almost always valid, but "cmp wN, #0" and "cmn wN, #0"
  488. // have the opposite effect on the C flag, so this pattern mustn't match under
  489. // those circumstances.
  490. if (Immed == 0)
  491. return false;
  492. if (N.getValueType() == MVT::i32)
  493. Immed = ~((uint32_t)Immed) + 1;
  494. else
  495. Immed = ~Immed + 1ULL;
  496. if (Immed & 0xFFFFFFFFFF000000ULL)
  497. return false;
  498. Immed &= 0xFFFFFFULL;
  499. return SelectArithImmed(CurDAG->getConstant(Immed, SDLoc(N), MVT::i32), Val,
  500. Shift);
  501. }
  502. /// getShiftTypeForNode - Translate a shift node to the corresponding
  503. /// ShiftType value.
  504. static AArch64_AM::ShiftExtendType getShiftTypeForNode(SDValue N) {
  505. switch (N.getOpcode()) {
  506. default:
  507. return AArch64_AM::InvalidShiftExtend;
  508. case ISD::SHL:
  509. return AArch64_AM::LSL;
  510. case ISD::SRL:
  511. return AArch64_AM::LSR;
  512. case ISD::SRA:
  513. return AArch64_AM::ASR;
  514. case ISD::ROTR:
  515. return AArch64_AM::ROR;
  516. }
  517. }
  518. /// Determine whether it is worth it to fold SHL into the addressing
  519. /// mode.
  520. static bool isWorthFoldingSHL(SDValue V) {
  521. assert(V.getOpcode() == ISD::SHL && "invalid opcode");
  522. // It is worth folding logical shift of up to three places.
  523. auto *CSD = dyn_cast<ConstantSDNode>(V.getOperand(1));
  524. if (!CSD)
  525. return false;
  526. unsigned ShiftVal = CSD->getZExtValue();
  527. if (ShiftVal > 3)
  528. return false;
  529. // Check if this particular node is reused in any non-memory related
  530. // operation. If yes, do not try to fold this node into the address
  531. // computation, since the computation will be kept.
  532. const SDNode *Node = V.getNode();
  533. for (SDNode *UI : Node->uses())
  534. if (!isa<MemSDNode>(*UI))
  535. for (SDNode *UII : UI->uses())
  536. if (!isa<MemSDNode>(*UII))
  537. return false;
  538. return true;
  539. }
  540. /// Determine whether it is worth to fold V into an extended register.
  541. bool AArch64DAGToDAGISel::isWorthFolding(SDValue V) const {
  542. // Trivial if we are optimizing for code size or if there is only
  543. // one use of the value.
  544. if (CurDAG->shouldOptForSize() || V.hasOneUse())
  545. return true;
  546. // If a subtarget has a fastpath LSL we can fold a logical shift into
  547. // the addressing mode and save a cycle.
  548. if (Subtarget->hasLSLFast() && V.getOpcode() == ISD::SHL &&
  549. isWorthFoldingSHL(V))
  550. return true;
  551. if (Subtarget->hasLSLFast() && V.getOpcode() == ISD::ADD) {
  552. const SDValue LHS = V.getOperand(0);
  553. const SDValue RHS = V.getOperand(1);
  554. if (LHS.getOpcode() == ISD::SHL && isWorthFoldingSHL(LHS))
  555. return true;
  556. if (RHS.getOpcode() == ISD::SHL && isWorthFoldingSHL(RHS))
  557. return true;
  558. }
  559. // It hurts otherwise, since the value will be reused.
  560. return false;
  561. }
  562. /// and (shl/srl/sra, x, c), mask --> shl (srl/sra, x, c1), c2
  563. /// to select more shifted register
  564. bool AArch64DAGToDAGISel::SelectShiftedRegisterFromAnd(SDValue N, SDValue &Reg,
  565. SDValue &Shift) {
  566. EVT VT = N.getValueType();
  567. if (VT != MVT::i32 && VT != MVT::i64)
  568. return false;
  569. if (N->getOpcode() != ISD::AND || !N->hasOneUse())
  570. return false;
  571. SDValue LHS = N.getOperand(0);
  572. if (!LHS->hasOneUse())
  573. return false;
  574. unsigned LHSOpcode = LHS->getOpcode();
  575. if (LHSOpcode != ISD::SHL && LHSOpcode != ISD::SRL && LHSOpcode != ISD::SRA)
  576. return false;
  577. ConstantSDNode *ShiftAmtNode = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
  578. if (!ShiftAmtNode)
  579. return false;
  580. uint64_t ShiftAmtC = ShiftAmtNode->getZExtValue();
  581. ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N.getOperand(1));
  582. if (!RHSC)
  583. return false;
  584. APInt AndMask = RHSC->getAPIntValue();
  585. unsigned LowZBits, MaskLen;
  586. if (!AndMask.isShiftedMask(LowZBits, MaskLen))
  587. return false;
  588. unsigned BitWidth = N.getValueSizeInBits();
  589. SDLoc DL(LHS);
  590. uint64_t NewShiftC;
  591. unsigned NewShiftOp;
  592. if (LHSOpcode == ISD::SHL) {
  593. // LowZBits <= ShiftAmtC will fall into isBitfieldPositioningOp
  594. // BitWidth != LowZBits + MaskLen doesn't match the pattern
  595. if (LowZBits <= ShiftAmtC || (BitWidth != LowZBits + MaskLen))
  596. return false;
  597. NewShiftC = LowZBits - ShiftAmtC;
  598. NewShiftOp = VT == MVT::i64 ? AArch64::UBFMXri : AArch64::UBFMWri;
  599. } else {
  600. if (LowZBits == 0)
  601. return false;
  602. // NewShiftC >= BitWidth will fall into isBitfieldExtractOp
  603. NewShiftC = LowZBits + ShiftAmtC;
  604. if (NewShiftC >= BitWidth)
  605. return false;
  606. // SRA need all high bits
  607. if (LHSOpcode == ISD::SRA && (BitWidth != (LowZBits + MaskLen)))
  608. return false;
  609. // SRL high bits can be 0 or 1
  610. if (LHSOpcode == ISD::SRL && (BitWidth > (NewShiftC + MaskLen)))
  611. return false;
  612. if (LHSOpcode == ISD::SRL)
  613. NewShiftOp = VT == MVT::i64 ? AArch64::UBFMXri : AArch64::UBFMWri;
  614. else
  615. NewShiftOp = VT == MVT::i64 ? AArch64::SBFMXri : AArch64::SBFMWri;
  616. }
  617. assert(NewShiftC < BitWidth && "Invalid shift amount");
  618. SDValue NewShiftAmt = CurDAG->getTargetConstant(NewShiftC, DL, VT);
  619. SDValue BitWidthMinus1 = CurDAG->getTargetConstant(BitWidth - 1, DL, VT);
  620. Reg = SDValue(CurDAG->getMachineNode(NewShiftOp, DL, VT, LHS->getOperand(0),
  621. NewShiftAmt, BitWidthMinus1),
  622. 0);
  623. unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, LowZBits);
  624. Shift = CurDAG->getTargetConstant(ShVal, DL, MVT::i32);
  625. return true;
  626. }
  627. /// SelectShiftedRegister - Select a "shifted register" operand. If the value
  628. /// is not shifted, set the Shift operand to default of "LSL 0". The logical
  629. /// instructions allow the shifted register to be rotated, but the arithmetic
  630. /// instructions do not. The AllowROR parameter specifies whether ROR is
  631. /// supported.
  632. bool AArch64DAGToDAGISel::SelectShiftedRegister(SDValue N, bool AllowROR,
  633. SDValue &Reg, SDValue &Shift) {
  634. if (SelectShiftedRegisterFromAnd(N, Reg, Shift))
  635. return true;
  636. AArch64_AM::ShiftExtendType ShType = getShiftTypeForNode(N);
  637. if (ShType == AArch64_AM::InvalidShiftExtend)
  638. return false;
  639. if (!AllowROR && ShType == AArch64_AM::ROR)
  640. return false;
  641. if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
  642. unsigned BitSize = N.getValueSizeInBits();
  643. unsigned Val = RHS->getZExtValue() & (BitSize - 1);
  644. unsigned ShVal = AArch64_AM::getShifterImm(ShType, Val);
  645. Reg = N.getOperand(0);
  646. Shift = CurDAG->getTargetConstant(ShVal, SDLoc(N), MVT::i32);
  647. return isWorthFolding(N);
  648. }
  649. return false;
  650. }
  651. /// getExtendTypeForNode - Translate an extend node to the corresponding
  652. /// ExtendType value.
  653. static AArch64_AM::ShiftExtendType
  654. getExtendTypeForNode(SDValue N, bool IsLoadStore = false) {
  655. if (N.getOpcode() == ISD::SIGN_EXTEND ||
  656. N.getOpcode() == ISD::SIGN_EXTEND_INREG) {
  657. EVT SrcVT;
  658. if (N.getOpcode() == ISD::SIGN_EXTEND_INREG)
  659. SrcVT = cast<VTSDNode>(N.getOperand(1))->getVT();
  660. else
  661. SrcVT = N.getOperand(0).getValueType();
  662. if (!IsLoadStore && SrcVT == MVT::i8)
  663. return AArch64_AM::SXTB;
  664. else if (!IsLoadStore && SrcVT == MVT::i16)
  665. return AArch64_AM::SXTH;
  666. else if (SrcVT == MVT::i32)
  667. return AArch64_AM::SXTW;
  668. assert(SrcVT != MVT::i64 && "extend from 64-bits?");
  669. return AArch64_AM::InvalidShiftExtend;
  670. } else if (N.getOpcode() == ISD::ZERO_EXTEND ||
  671. N.getOpcode() == ISD::ANY_EXTEND) {
  672. EVT SrcVT = N.getOperand(0).getValueType();
  673. if (!IsLoadStore && SrcVT == MVT::i8)
  674. return AArch64_AM::UXTB;
  675. else if (!IsLoadStore && SrcVT == MVT::i16)
  676. return AArch64_AM::UXTH;
  677. else if (SrcVT == MVT::i32)
  678. return AArch64_AM::UXTW;
  679. assert(SrcVT != MVT::i64 && "extend from 64-bits?");
  680. return AArch64_AM::InvalidShiftExtend;
  681. } else if (N.getOpcode() == ISD::AND) {
  682. ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
  683. if (!CSD)
  684. return AArch64_AM::InvalidShiftExtend;
  685. uint64_t AndMask = CSD->getZExtValue();
  686. switch (AndMask) {
  687. default:
  688. return AArch64_AM::InvalidShiftExtend;
  689. case 0xFF:
  690. return !IsLoadStore ? AArch64_AM::UXTB : AArch64_AM::InvalidShiftExtend;
  691. case 0xFFFF:
  692. return !IsLoadStore ? AArch64_AM::UXTH : AArch64_AM::InvalidShiftExtend;
  693. case 0xFFFFFFFF:
  694. return AArch64_AM::UXTW;
  695. }
  696. }
  697. return AArch64_AM::InvalidShiftExtend;
  698. }
  699. // Helper for SelectMLAV64LaneV128 - Recognize high lane extracts.
  700. static bool checkHighLaneIndex(SDNode *DL, SDValue &LaneOp, int &LaneIdx) {
  701. if (DL->getOpcode() != AArch64ISD::DUPLANE16 &&
  702. DL->getOpcode() != AArch64ISD::DUPLANE32)
  703. return false;
  704. SDValue SV = DL->getOperand(0);
  705. if (SV.getOpcode() != ISD::INSERT_SUBVECTOR)
  706. return false;
  707. SDValue EV = SV.getOperand(1);
  708. if (EV.getOpcode() != ISD::EXTRACT_SUBVECTOR)
  709. return false;
  710. ConstantSDNode *DLidx = cast<ConstantSDNode>(DL->getOperand(1).getNode());
  711. ConstantSDNode *EVidx = cast<ConstantSDNode>(EV.getOperand(1).getNode());
  712. LaneIdx = DLidx->getSExtValue() + EVidx->getSExtValue();
  713. LaneOp = EV.getOperand(0);
  714. return true;
  715. }
  716. // Helper for SelectOpcV64LaneV128 - Recognize operations where one operand is a
  717. // high lane extract.
  718. static bool checkV64LaneV128(SDValue Op0, SDValue Op1, SDValue &StdOp,
  719. SDValue &LaneOp, int &LaneIdx) {
  720. if (!checkHighLaneIndex(Op0.getNode(), LaneOp, LaneIdx)) {
  721. std::swap(Op0, Op1);
  722. if (!checkHighLaneIndex(Op0.getNode(), LaneOp, LaneIdx))
  723. return false;
  724. }
  725. StdOp = Op1;
  726. return true;
  727. }
  728. /// SelectMLAV64LaneV128 - AArch64 supports vector MLAs where one multiplicand
  729. /// is a lane in the upper half of a 128-bit vector. Recognize and select this
  730. /// so that we don't emit unnecessary lane extracts.
  731. bool AArch64DAGToDAGISel::tryMLAV64LaneV128(SDNode *N) {
  732. SDLoc dl(N);
  733. SDValue Op0 = N->getOperand(0);
  734. SDValue Op1 = N->getOperand(1);
  735. SDValue MLAOp1; // Will hold ordinary multiplicand for MLA.
  736. SDValue MLAOp2; // Will hold lane-accessed multiplicand for MLA.
  737. int LaneIdx = -1; // Will hold the lane index.
  738. if (Op1.getOpcode() != ISD::MUL ||
  739. !checkV64LaneV128(Op1.getOperand(0), Op1.getOperand(1), MLAOp1, MLAOp2,
  740. LaneIdx)) {
  741. std::swap(Op0, Op1);
  742. if (Op1.getOpcode() != ISD::MUL ||
  743. !checkV64LaneV128(Op1.getOperand(0), Op1.getOperand(1), MLAOp1, MLAOp2,
  744. LaneIdx))
  745. return false;
  746. }
  747. SDValue LaneIdxVal = CurDAG->getTargetConstant(LaneIdx, dl, MVT::i64);
  748. SDValue Ops[] = { Op0, MLAOp1, MLAOp2, LaneIdxVal };
  749. unsigned MLAOpc = ~0U;
  750. switch (N->getSimpleValueType(0).SimpleTy) {
  751. default:
  752. llvm_unreachable("Unrecognized MLA.");
  753. case MVT::v4i16:
  754. MLAOpc = AArch64::MLAv4i16_indexed;
  755. break;
  756. case MVT::v8i16:
  757. MLAOpc = AArch64::MLAv8i16_indexed;
  758. break;
  759. case MVT::v2i32:
  760. MLAOpc = AArch64::MLAv2i32_indexed;
  761. break;
  762. case MVT::v4i32:
  763. MLAOpc = AArch64::MLAv4i32_indexed;
  764. break;
  765. }
  766. ReplaceNode(N, CurDAG->getMachineNode(MLAOpc, dl, N->getValueType(0), Ops));
  767. return true;
  768. }
  769. bool AArch64DAGToDAGISel::tryMULLV64LaneV128(unsigned IntNo, SDNode *N) {
  770. SDLoc dl(N);
  771. SDValue SMULLOp0;
  772. SDValue SMULLOp1;
  773. int LaneIdx;
  774. if (!checkV64LaneV128(N->getOperand(1), N->getOperand(2), SMULLOp0, SMULLOp1,
  775. LaneIdx))
  776. return false;
  777. SDValue LaneIdxVal = CurDAG->getTargetConstant(LaneIdx, dl, MVT::i64);
  778. SDValue Ops[] = { SMULLOp0, SMULLOp1, LaneIdxVal };
  779. unsigned SMULLOpc = ~0U;
  780. if (IntNo == Intrinsic::aarch64_neon_smull) {
  781. switch (N->getSimpleValueType(0).SimpleTy) {
  782. default:
  783. llvm_unreachable("Unrecognized SMULL.");
  784. case MVT::v4i32:
  785. SMULLOpc = AArch64::SMULLv4i16_indexed;
  786. break;
  787. case MVT::v2i64:
  788. SMULLOpc = AArch64::SMULLv2i32_indexed;
  789. break;
  790. }
  791. } else if (IntNo == Intrinsic::aarch64_neon_umull) {
  792. switch (N->getSimpleValueType(0).SimpleTy) {
  793. default:
  794. llvm_unreachable("Unrecognized SMULL.");
  795. case MVT::v4i32:
  796. SMULLOpc = AArch64::UMULLv4i16_indexed;
  797. break;
  798. case MVT::v2i64:
  799. SMULLOpc = AArch64::UMULLv2i32_indexed;
  800. break;
  801. }
  802. } else
  803. llvm_unreachable("Unrecognized intrinsic.");
  804. ReplaceNode(N, CurDAG->getMachineNode(SMULLOpc, dl, N->getValueType(0), Ops));
  805. return true;
  806. }
  807. /// Instructions that accept extend modifiers like UXTW expect the register
  808. /// being extended to be a GPR32, but the incoming DAG might be acting on a
  809. /// GPR64 (either via SEXT_INREG or AND). Extract the appropriate low bits if
  810. /// this is the case.
  811. static SDValue narrowIfNeeded(SelectionDAG *CurDAG, SDValue N) {
  812. if (N.getValueType() == MVT::i32)
  813. return N;
  814. SDLoc dl(N);
  815. SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
  816. MachineSDNode *Node = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
  817. dl, MVT::i32, N, SubReg);
  818. return SDValue(Node, 0);
  819. }
  820. // Returns a suitable CNT/INC/DEC/RDVL multiplier to calculate VSCALE*N.
  821. template<signed Low, signed High, signed Scale>
  822. bool AArch64DAGToDAGISel::SelectRDVLImm(SDValue N, SDValue &Imm) {
  823. if (!isa<ConstantSDNode>(N))
  824. return false;
  825. int64_t MulImm = cast<ConstantSDNode>(N)->getSExtValue();
  826. if ((MulImm % std::abs(Scale)) == 0) {
  827. int64_t RDVLImm = MulImm / Scale;
  828. if ((RDVLImm >= Low) && (RDVLImm <= High)) {
  829. Imm = CurDAG->getTargetConstant(RDVLImm, SDLoc(N), MVT::i32);
  830. return true;
  831. }
  832. }
  833. return false;
  834. }
  835. /// SelectArithExtendedRegister - Select a "extended register" operand. This
  836. /// operand folds in an extend followed by an optional left shift.
  837. bool AArch64DAGToDAGISel::SelectArithExtendedRegister(SDValue N, SDValue &Reg,
  838. SDValue &Shift) {
  839. unsigned ShiftVal = 0;
  840. AArch64_AM::ShiftExtendType Ext;
  841. if (N.getOpcode() == ISD::SHL) {
  842. ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
  843. if (!CSD)
  844. return false;
  845. ShiftVal = CSD->getZExtValue();
  846. if (ShiftVal > 4)
  847. return false;
  848. Ext = getExtendTypeForNode(N.getOperand(0));
  849. if (Ext == AArch64_AM::InvalidShiftExtend)
  850. return false;
  851. Reg = N.getOperand(0).getOperand(0);
  852. } else {
  853. Ext = getExtendTypeForNode(N);
  854. if (Ext == AArch64_AM::InvalidShiftExtend)
  855. return false;
  856. Reg = N.getOperand(0);
  857. // Don't match if free 32-bit -> 64-bit zext can be used instead. Use the
  858. // isDef32 as a heuristic for when the operand is likely to be a 32bit def.
  859. auto isDef32 = [](SDValue N) {
  860. unsigned Opc = N.getOpcode();
  861. return Opc != ISD::TRUNCATE && Opc != TargetOpcode::EXTRACT_SUBREG &&
  862. Opc != ISD::CopyFromReg && Opc != ISD::AssertSext &&
  863. Opc != ISD::AssertZext && Opc != ISD::AssertAlign &&
  864. Opc != ISD::FREEZE;
  865. };
  866. if (Ext == AArch64_AM::UXTW && Reg->getValueType(0).getSizeInBits() == 32 &&
  867. isDef32(Reg))
  868. return false;
  869. }
  870. // AArch64 mandates that the RHS of the operation must use the smallest
  871. // register class that could contain the size being extended from. Thus,
  872. // if we're folding a (sext i8), we need the RHS to be a GPR32, even though
  873. // there might not be an actual 32-bit value in the program. We can
  874. // (harmlessly) synthesize one by injected an EXTRACT_SUBREG here.
  875. assert(Ext != AArch64_AM::UXTX && Ext != AArch64_AM::SXTX);
  876. Reg = narrowIfNeeded(CurDAG, Reg);
  877. Shift = CurDAG->getTargetConstant(getArithExtendImm(Ext, ShiftVal), SDLoc(N),
  878. MVT::i32);
  879. return isWorthFolding(N);
  880. }
  881. /// SelectArithUXTXRegister - Select a "UXTX register" operand. This
  882. /// operand is refered by the instructions have SP operand
  883. bool AArch64DAGToDAGISel::SelectArithUXTXRegister(SDValue N, SDValue &Reg,
  884. SDValue &Shift) {
  885. unsigned ShiftVal = 0;
  886. AArch64_AM::ShiftExtendType Ext;
  887. if (N.getOpcode() != ISD::SHL)
  888. return false;
  889. ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
  890. if (!CSD)
  891. return false;
  892. ShiftVal = CSD->getZExtValue();
  893. if (ShiftVal > 4)
  894. return false;
  895. Ext = AArch64_AM::UXTX;
  896. Reg = N.getOperand(0);
  897. Shift = CurDAG->getTargetConstant(getArithExtendImm(Ext, ShiftVal), SDLoc(N),
  898. MVT::i32);
  899. return isWorthFolding(N);
  900. }
  901. /// If there's a use of this ADDlow that's not itself a load/store then we'll
  902. /// need to create a real ADD instruction from it anyway and there's no point in
  903. /// folding it into the mem op. Theoretically, it shouldn't matter, but there's
  904. /// a single pseudo-instruction for an ADRP/ADD pair so over-aggressive folding
  905. /// leads to duplicated ADRP instructions.
  906. static bool isWorthFoldingADDlow(SDValue N) {
  907. for (auto *Use : N->uses()) {
  908. if (Use->getOpcode() != ISD::LOAD && Use->getOpcode() != ISD::STORE &&
  909. Use->getOpcode() != ISD::ATOMIC_LOAD &&
  910. Use->getOpcode() != ISD::ATOMIC_STORE)
  911. return false;
  912. // ldar and stlr have much more restrictive addressing modes (just a
  913. // register).
  914. if (isStrongerThanMonotonic(cast<MemSDNode>(Use)->getSuccessOrdering()))
  915. return false;
  916. }
  917. return true;
  918. }
  919. /// SelectAddrModeIndexedBitWidth - Select a "register plus scaled (un)signed BW-bit
  920. /// immediate" address. The "Size" argument is the size in bytes of the memory
  921. /// reference, which determines the scale.
  922. bool AArch64DAGToDAGISel::SelectAddrModeIndexedBitWidth(SDValue N, bool IsSignedImm,
  923. unsigned BW, unsigned Size,
  924. SDValue &Base,
  925. SDValue &OffImm) {
  926. SDLoc dl(N);
  927. const DataLayout &DL = CurDAG->getDataLayout();
  928. const TargetLowering *TLI = getTargetLowering();
  929. if (N.getOpcode() == ISD::FrameIndex) {
  930. int FI = cast<FrameIndexSDNode>(N)->getIndex();
  931. Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
  932. OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
  933. return true;
  934. }
  935. // As opposed to the (12-bit) Indexed addressing mode below, the 7/9-bit signed
  936. // selected here doesn't support labels/immediates, only base+offset.
  937. if (CurDAG->isBaseWithConstantOffset(N)) {
  938. if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
  939. if (IsSignedImm) {
  940. int64_t RHSC = RHS->getSExtValue();
  941. unsigned Scale = Log2_32(Size);
  942. int64_t Range = 0x1LL << (BW - 1);
  943. if ((RHSC & (Size - 1)) == 0 && RHSC >= -(Range << Scale) &&
  944. RHSC < (Range << Scale)) {
  945. Base = N.getOperand(0);
  946. if (Base.getOpcode() == ISD::FrameIndex) {
  947. int FI = cast<FrameIndexSDNode>(Base)->getIndex();
  948. Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
  949. }
  950. OffImm = CurDAG->getTargetConstant(RHSC >> Scale, dl, MVT::i64);
  951. return true;
  952. }
  953. } else {
  954. // unsigned Immediate
  955. uint64_t RHSC = RHS->getZExtValue();
  956. unsigned Scale = Log2_32(Size);
  957. uint64_t Range = 0x1ULL << BW;
  958. if ((RHSC & (Size - 1)) == 0 && RHSC < (Range << Scale)) {
  959. Base = N.getOperand(0);
  960. if (Base.getOpcode() == ISD::FrameIndex) {
  961. int FI = cast<FrameIndexSDNode>(Base)->getIndex();
  962. Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
  963. }
  964. OffImm = CurDAG->getTargetConstant(RHSC >> Scale, dl, MVT::i64);
  965. return true;
  966. }
  967. }
  968. }
  969. }
  970. // Base only. The address will be materialized into a register before
  971. // the memory is accessed.
  972. // add x0, Xbase, #offset
  973. // stp x1, x2, [x0]
  974. Base = N;
  975. OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
  976. return true;
  977. }
  978. /// SelectAddrModeIndexed - Select a "register plus scaled unsigned 12-bit
  979. /// immediate" address. The "Size" argument is the size in bytes of the memory
  980. /// reference, which determines the scale.
  981. bool AArch64DAGToDAGISel::SelectAddrModeIndexed(SDValue N, unsigned Size,
  982. SDValue &Base, SDValue &OffImm) {
  983. SDLoc dl(N);
  984. const DataLayout &DL = CurDAG->getDataLayout();
  985. const TargetLowering *TLI = getTargetLowering();
  986. if (N.getOpcode() == ISD::FrameIndex) {
  987. int FI = cast<FrameIndexSDNode>(N)->getIndex();
  988. Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
  989. OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
  990. return true;
  991. }
  992. if (N.getOpcode() == AArch64ISD::ADDlow && isWorthFoldingADDlow(N)) {
  993. GlobalAddressSDNode *GAN =
  994. dyn_cast<GlobalAddressSDNode>(N.getOperand(1).getNode());
  995. Base = N.getOperand(0);
  996. OffImm = N.getOperand(1);
  997. if (!GAN)
  998. return true;
  999. if (GAN->getOffset() % Size == 0 &&
  1000. GAN->getGlobal()->getPointerAlignment(DL) >= Size)
  1001. return true;
  1002. }
  1003. if (CurDAG->isBaseWithConstantOffset(N)) {
  1004. if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
  1005. int64_t RHSC = (int64_t)RHS->getZExtValue();
  1006. unsigned Scale = Log2_32(Size);
  1007. if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
  1008. Base = N.getOperand(0);
  1009. if (Base.getOpcode() == ISD::FrameIndex) {
  1010. int FI = cast<FrameIndexSDNode>(Base)->getIndex();
  1011. Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
  1012. }
  1013. OffImm = CurDAG->getTargetConstant(RHSC >> Scale, dl, MVT::i64);
  1014. return true;
  1015. }
  1016. }
  1017. }
  1018. // Before falling back to our general case, check if the unscaled
  1019. // instructions can handle this. If so, that's preferable.
  1020. if (SelectAddrModeUnscaled(N, Size, Base, OffImm))
  1021. return false;
  1022. // Base only. The address will be materialized into a register before
  1023. // the memory is accessed.
  1024. // add x0, Xbase, #offset
  1025. // ldr x0, [x0]
  1026. Base = N;
  1027. OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
  1028. return true;
  1029. }
  1030. /// SelectAddrModeUnscaled - Select a "register plus unscaled signed 9-bit
  1031. /// immediate" address. This should only match when there is an offset that
  1032. /// is not valid for a scaled immediate addressing mode. The "Size" argument
  1033. /// is the size in bytes of the memory reference, which is needed here to know
  1034. /// what is valid for a scaled immediate.
  1035. bool AArch64DAGToDAGISel::SelectAddrModeUnscaled(SDValue N, unsigned Size,
  1036. SDValue &Base,
  1037. SDValue &OffImm) {
  1038. if (!CurDAG->isBaseWithConstantOffset(N))
  1039. return false;
  1040. if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
  1041. int64_t RHSC = RHS->getSExtValue();
  1042. // If the offset is valid as a scaled immediate, don't match here.
  1043. if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 &&
  1044. RHSC < (0x1000 << Log2_32(Size)))
  1045. return false;
  1046. if (RHSC >= -256 && RHSC < 256) {
  1047. Base = N.getOperand(0);
  1048. if (Base.getOpcode() == ISD::FrameIndex) {
  1049. int FI = cast<FrameIndexSDNode>(Base)->getIndex();
  1050. const TargetLowering *TLI = getTargetLowering();
  1051. Base = CurDAG->getTargetFrameIndex(
  1052. FI, TLI->getPointerTy(CurDAG->getDataLayout()));
  1053. }
  1054. OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i64);
  1055. return true;
  1056. }
  1057. }
  1058. return false;
  1059. }
  1060. static SDValue Widen(SelectionDAG *CurDAG, SDValue N) {
  1061. SDLoc dl(N);
  1062. SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
  1063. SDValue ImpDef = SDValue(
  1064. CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, MVT::i64), 0);
  1065. MachineSDNode *Node = CurDAG->getMachineNode(
  1066. TargetOpcode::INSERT_SUBREG, dl, MVT::i64, ImpDef, N, SubReg);
  1067. return SDValue(Node, 0);
  1068. }
  1069. /// Check if the given SHL node (\p N), can be used to form an
  1070. /// extended register for an addressing mode.
  1071. bool AArch64DAGToDAGISel::SelectExtendedSHL(SDValue N, unsigned Size,
  1072. bool WantExtend, SDValue &Offset,
  1073. SDValue &SignExtend) {
  1074. assert(N.getOpcode() == ISD::SHL && "Invalid opcode.");
  1075. ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
  1076. if (!CSD || (CSD->getZExtValue() & 0x7) != CSD->getZExtValue())
  1077. return false;
  1078. SDLoc dl(N);
  1079. if (WantExtend) {
  1080. AArch64_AM::ShiftExtendType Ext =
  1081. getExtendTypeForNode(N.getOperand(0), true);
  1082. if (Ext == AArch64_AM::InvalidShiftExtend)
  1083. return false;
  1084. Offset = narrowIfNeeded(CurDAG, N.getOperand(0).getOperand(0));
  1085. SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
  1086. MVT::i32);
  1087. } else {
  1088. Offset = N.getOperand(0);
  1089. SignExtend = CurDAG->getTargetConstant(0, dl, MVT::i32);
  1090. }
  1091. unsigned LegalShiftVal = Log2_32(Size);
  1092. unsigned ShiftVal = CSD->getZExtValue();
  1093. if (ShiftVal != 0 && ShiftVal != LegalShiftVal)
  1094. return false;
  1095. return isWorthFolding(N);
  1096. }
  1097. bool AArch64DAGToDAGISel::SelectAddrModeWRO(SDValue N, unsigned Size,
  1098. SDValue &Base, SDValue &Offset,
  1099. SDValue &SignExtend,
  1100. SDValue &DoShift) {
  1101. if (N.getOpcode() != ISD::ADD)
  1102. return false;
  1103. SDValue LHS = N.getOperand(0);
  1104. SDValue RHS = N.getOperand(1);
  1105. SDLoc dl(N);
  1106. // We don't want to match immediate adds here, because they are better lowered
  1107. // to the register-immediate addressing modes.
  1108. if (isa<ConstantSDNode>(LHS) || isa<ConstantSDNode>(RHS))
  1109. return false;
  1110. // Check if this particular node is reused in any non-memory related
  1111. // operation. If yes, do not try to fold this node into the address
  1112. // computation, since the computation will be kept.
  1113. const SDNode *Node = N.getNode();
  1114. for (SDNode *UI : Node->uses()) {
  1115. if (!isa<MemSDNode>(*UI))
  1116. return false;
  1117. }
  1118. // Remember if it is worth folding N when it produces extended register.
  1119. bool IsExtendedRegisterWorthFolding = isWorthFolding(N);
  1120. // Try to match a shifted extend on the RHS.
  1121. if (IsExtendedRegisterWorthFolding && RHS.getOpcode() == ISD::SHL &&
  1122. SelectExtendedSHL(RHS, Size, true, Offset, SignExtend)) {
  1123. Base = LHS;
  1124. DoShift = CurDAG->getTargetConstant(true, dl, MVT::i32);
  1125. return true;
  1126. }
  1127. // Try to match a shifted extend on the LHS.
  1128. if (IsExtendedRegisterWorthFolding && LHS.getOpcode() == ISD::SHL &&
  1129. SelectExtendedSHL(LHS, Size, true, Offset, SignExtend)) {
  1130. Base = RHS;
  1131. DoShift = CurDAG->getTargetConstant(true, dl, MVT::i32);
  1132. return true;
  1133. }
  1134. // There was no shift, whatever else we find.
  1135. DoShift = CurDAG->getTargetConstant(false, dl, MVT::i32);
  1136. AArch64_AM::ShiftExtendType Ext = AArch64_AM::InvalidShiftExtend;
  1137. // Try to match an unshifted extend on the LHS.
  1138. if (IsExtendedRegisterWorthFolding &&
  1139. (Ext = getExtendTypeForNode(LHS, true)) !=
  1140. AArch64_AM::InvalidShiftExtend) {
  1141. Base = RHS;
  1142. Offset = narrowIfNeeded(CurDAG, LHS.getOperand(0));
  1143. SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
  1144. MVT::i32);
  1145. if (isWorthFolding(LHS))
  1146. return true;
  1147. }
  1148. // Try to match an unshifted extend on the RHS.
  1149. if (IsExtendedRegisterWorthFolding &&
  1150. (Ext = getExtendTypeForNode(RHS, true)) !=
  1151. AArch64_AM::InvalidShiftExtend) {
  1152. Base = LHS;
  1153. Offset = narrowIfNeeded(CurDAG, RHS.getOperand(0));
  1154. SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
  1155. MVT::i32);
  1156. if (isWorthFolding(RHS))
  1157. return true;
  1158. }
  1159. return false;
  1160. }
  1161. // Check if the given immediate is preferred by ADD. If an immediate can be
  1162. // encoded in an ADD, or it can be encoded in an "ADD LSL #12" and can not be
  1163. // encoded by one MOVZ, return true.
  1164. static bool isPreferredADD(int64_t ImmOff) {
  1165. // Constant in [0x0, 0xfff] can be encoded in ADD.
  1166. if ((ImmOff & 0xfffffffffffff000LL) == 0x0LL)
  1167. return true;
  1168. // Check if it can be encoded in an "ADD LSL #12".
  1169. if ((ImmOff & 0xffffffffff000fffLL) == 0x0LL)
  1170. // As a single MOVZ is faster than a "ADD of LSL #12", ignore such constant.
  1171. return (ImmOff & 0xffffffffff00ffffLL) != 0x0LL &&
  1172. (ImmOff & 0xffffffffffff0fffLL) != 0x0LL;
  1173. return false;
  1174. }
  1175. bool AArch64DAGToDAGISel::SelectAddrModeXRO(SDValue N, unsigned Size,
  1176. SDValue &Base, SDValue &Offset,
  1177. SDValue &SignExtend,
  1178. SDValue &DoShift) {
  1179. if (N.getOpcode() != ISD::ADD)
  1180. return false;
  1181. SDValue LHS = N.getOperand(0);
  1182. SDValue RHS = N.getOperand(1);
  1183. SDLoc DL(N);
  1184. // Check if this particular node is reused in any non-memory related
  1185. // operation. If yes, do not try to fold this node into the address
  1186. // computation, since the computation will be kept.
  1187. const SDNode *Node = N.getNode();
  1188. for (SDNode *UI : Node->uses()) {
  1189. if (!isa<MemSDNode>(*UI))
  1190. return false;
  1191. }
  1192. // Watch out if RHS is a wide immediate, it can not be selected into
  1193. // [BaseReg+Imm] addressing mode. Also it may not be able to be encoded into
  1194. // ADD/SUB. Instead it will use [BaseReg + 0] address mode and generate
  1195. // instructions like:
  1196. // MOV X0, WideImmediate
  1197. // ADD X1, BaseReg, X0
  1198. // LDR X2, [X1, 0]
  1199. // For such situation, using [BaseReg, XReg] addressing mode can save one
  1200. // ADD/SUB:
  1201. // MOV X0, WideImmediate
  1202. // LDR X2, [BaseReg, X0]
  1203. if (isa<ConstantSDNode>(RHS)) {
  1204. int64_t ImmOff = (int64_t)cast<ConstantSDNode>(RHS)->getZExtValue();
  1205. unsigned Scale = Log2_32(Size);
  1206. // Skip the immediate can be selected by load/store addressing mode.
  1207. // Also skip the immediate can be encoded by a single ADD (SUB is also
  1208. // checked by using -ImmOff).
  1209. if ((ImmOff % Size == 0 && ImmOff >= 0 && ImmOff < (0x1000 << Scale)) ||
  1210. isPreferredADD(ImmOff) || isPreferredADD(-ImmOff))
  1211. return false;
  1212. SDValue Ops[] = { RHS };
  1213. SDNode *MOVI =
  1214. CurDAG->getMachineNode(AArch64::MOVi64imm, DL, MVT::i64, Ops);
  1215. SDValue MOVIV = SDValue(MOVI, 0);
  1216. // This ADD of two X register will be selected into [Reg+Reg] mode.
  1217. N = CurDAG->getNode(ISD::ADD, DL, MVT::i64, LHS, MOVIV);
  1218. }
  1219. // Remember if it is worth folding N when it produces extended register.
  1220. bool IsExtendedRegisterWorthFolding = isWorthFolding(N);
  1221. // Try to match a shifted extend on the RHS.
  1222. if (IsExtendedRegisterWorthFolding && RHS.getOpcode() == ISD::SHL &&
  1223. SelectExtendedSHL(RHS, Size, false, Offset, SignExtend)) {
  1224. Base = LHS;
  1225. DoShift = CurDAG->getTargetConstant(true, DL, MVT::i32);
  1226. return true;
  1227. }
  1228. // Try to match a shifted extend on the LHS.
  1229. if (IsExtendedRegisterWorthFolding && LHS.getOpcode() == ISD::SHL &&
  1230. SelectExtendedSHL(LHS, Size, false, Offset, SignExtend)) {
  1231. Base = RHS;
  1232. DoShift = CurDAG->getTargetConstant(true, DL, MVT::i32);
  1233. return true;
  1234. }
  1235. // Match any non-shifted, non-extend, non-immediate add expression.
  1236. Base = LHS;
  1237. Offset = RHS;
  1238. SignExtend = CurDAG->getTargetConstant(false, DL, MVT::i32);
  1239. DoShift = CurDAG->getTargetConstant(false, DL, MVT::i32);
  1240. // Reg1 + Reg2 is free: no check needed.
  1241. return true;
  1242. }
  1243. SDValue AArch64DAGToDAGISel::createDTuple(ArrayRef<SDValue> Regs) {
  1244. static const unsigned RegClassIDs[] = {
  1245. AArch64::DDRegClassID, AArch64::DDDRegClassID, AArch64::DDDDRegClassID};
  1246. static const unsigned SubRegs[] = {AArch64::dsub0, AArch64::dsub1,
  1247. AArch64::dsub2, AArch64::dsub3};
  1248. return createTuple(Regs, RegClassIDs, SubRegs);
  1249. }
  1250. SDValue AArch64DAGToDAGISel::createQTuple(ArrayRef<SDValue> Regs) {
  1251. static const unsigned RegClassIDs[] = {
  1252. AArch64::QQRegClassID, AArch64::QQQRegClassID, AArch64::QQQQRegClassID};
  1253. static const unsigned SubRegs[] = {AArch64::qsub0, AArch64::qsub1,
  1254. AArch64::qsub2, AArch64::qsub3};
  1255. return createTuple(Regs, RegClassIDs, SubRegs);
  1256. }
  1257. SDValue AArch64DAGToDAGISel::createZTuple(ArrayRef<SDValue> Regs) {
  1258. static const unsigned RegClassIDs[] = {AArch64::ZPR2RegClassID,
  1259. AArch64::ZPR3RegClassID,
  1260. AArch64::ZPR4RegClassID};
  1261. static const unsigned SubRegs[] = {AArch64::zsub0, AArch64::zsub1,
  1262. AArch64::zsub2, AArch64::zsub3};
  1263. return createTuple(Regs, RegClassIDs, SubRegs);
  1264. }
  1265. SDValue AArch64DAGToDAGISel::createTuple(ArrayRef<SDValue> Regs,
  1266. const unsigned RegClassIDs[],
  1267. const unsigned SubRegs[]) {
  1268. // There's no special register-class for a vector-list of 1 element: it's just
  1269. // a vector.
  1270. if (Regs.size() == 1)
  1271. return Regs[0];
  1272. assert(Regs.size() >= 2 && Regs.size() <= 4);
  1273. SDLoc DL(Regs[0]);
  1274. SmallVector<SDValue, 4> Ops;
  1275. // First operand of REG_SEQUENCE is the desired RegClass.
  1276. Ops.push_back(
  1277. CurDAG->getTargetConstant(RegClassIDs[Regs.size() - 2], DL, MVT::i32));
  1278. // Then we get pairs of source & subregister-position for the components.
  1279. for (unsigned i = 0; i < Regs.size(); ++i) {
  1280. Ops.push_back(Regs[i]);
  1281. Ops.push_back(CurDAG->getTargetConstant(SubRegs[i], DL, MVT::i32));
  1282. }
  1283. SDNode *N =
  1284. CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, MVT::Untyped, Ops);
  1285. return SDValue(N, 0);
  1286. }
  1287. void AArch64DAGToDAGISel::SelectTable(SDNode *N, unsigned NumVecs, unsigned Opc,
  1288. bool isExt) {
  1289. SDLoc dl(N);
  1290. EVT VT = N->getValueType(0);
  1291. unsigned ExtOff = isExt;
  1292. // Form a REG_SEQUENCE to force register allocation.
  1293. unsigned Vec0Off = ExtOff + 1;
  1294. SmallVector<SDValue, 4> Regs(N->op_begin() + Vec0Off,
  1295. N->op_begin() + Vec0Off + NumVecs);
  1296. SDValue RegSeq = createQTuple(Regs);
  1297. SmallVector<SDValue, 6> Ops;
  1298. if (isExt)
  1299. Ops.push_back(N->getOperand(1));
  1300. Ops.push_back(RegSeq);
  1301. Ops.push_back(N->getOperand(NumVecs + ExtOff + 1));
  1302. ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops));
  1303. }
  1304. bool AArch64DAGToDAGISel::tryIndexedLoad(SDNode *N) {
  1305. LoadSDNode *LD = cast<LoadSDNode>(N);
  1306. if (LD->isUnindexed())
  1307. return false;
  1308. EVT VT = LD->getMemoryVT();
  1309. EVT DstVT = N->getValueType(0);
  1310. ISD::MemIndexedMode AM = LD->getAddressingMode();
  1311. bool IsPre = AM == ISD::PRE_INC || AM == ISD::PRE_DEC;
  1312. // We're not doing validity checking here. That was done when checking
  1313. // if we should mark the load as indexed or not. We're just selecting
  1314. // the right instruction.
  1315. unsigned Opcode = 0;
  1316. ISD::LoadExtType ExtType = LD->getExtensionType();
  1317. bool InsertTo64 = false;
  1318. if (VT == MVT::i64)
  1319. Opcode = IsPre ? AArch64::LDRXpre : AArch64::LDRXpost;
  1320. else if (VT == MVT::i32) {
  1321. if (ExtType == ISD::NON_EXTLOAD)
  1322. Opcode = IsPre ? AArch64::LDRWpre : AArch64::LDRWpost;
  1323. else if (ExtType == ISD::SEXTLOAD)
  1324. Opcode = IsPre ? AArch64::LDRSWpre : AArch64::LDRSWpost;
  1325. else {
  1326. Opcode = IsPre ? AArch64::LDRWpre : AArch64::LDRWpost;
  1327. InsertTo64 = true;
  1328. // The result of the load is only i32. It's the subreg_to_reg that makes
  1329. // it into an i64.
  1330. DstVT = MVT::i32;
  1331. }
  1332. } else if (VT == MVT::i16) {
  1333. if (ExtType == ISD::SEXTLOAD) {
  1334. if (DstVT == MVT::i64)
  1335. Opcode = IsPre ? AArch64::LDRSHXpre : AArch64::LDRSHXpost;
  1336. else
  1337. Opcode = IsPre ? AArch64::LDRSHWpre : AArch64::LDRSHWpost;
  1338. } else {
  1339. Opcode = IsPre ? AArch64::LDRHHpre : AArch64::LDRHHpost;
  1340. InsertTo64 = DstVT == MVT::i64;
  1341. // The result of the load is only i32. It's the subreg_to_reg that makes
  1342. // it into an i64.
  1343. DstVT = MVT::i32;
  1344. }
  1345. } else if (VT == MVT::i8) {
  1346. if (ExtType == ISD::SEXTLOAD) {
  1347. if (DstVT == MVT::i64)
  1348. Opcode = IsPre ? AArch64::LDRSBXpre : AArch64::LDRSBXpost;
  1349. else
  1350. Opcode = IsPre ? AArch64::LDRSBWpre : AArch64::LDRSBWpost;
  1351. } else {
  1352. Opcode = IsPre ? AArch64::LDRBBpre : AArch64::LDRBBpost;
  1353. InsertTo64 = DstVT == MVT::i64;
  1354. // The result of the load is only i32. It's the subreg_to_reg that makes
  1355. // it into an i64.
  1356. DstVT = MVT::i32;
  1357. }
  1358. } else if (VT == MVT::f16) {
  1359. Opcode = IsPre ? AArch64::LDRHpre : AArch64::LDRHpost;
  1360. } else if (VT == MVT::bf16) {
  1361. Opcode = IsPre ? AArch64::LDRHpre : AArch64::LDRHpost;
  1362. } else if (VT == MVT::f32) {
  1363. Opcode = IsPre ? AArch64::LDRSpre : AArch64::LDRSpost;
  1364. } else if (VT == MVT::f64 || VT.is64BitVector()) {
  1365. Opcode = IsPre ? AArch64::LDRDpre : AArch64::LDRDpost;
  1366. } else if (VT.is128BitVector()) {
  1367. Opcode = IsPre ? AArch64::LDRQpre : AArch64::LDRQpost;
  1368. } else
  1369. return false;
  1370. SDValue Chain = LD->getChain();
  1371. SDValue Base = LD->getBasePtr();
  1372. ConstantSDNode *OffsetOp = cast<ConstantSDNode>(LD->getOffset());
  1373. int OffsetVal = (int)OffsetOp->getZExtValue();
  1374. SDLoc dl(N);
  1375. SDValue Offset = CurDAG->getTargetConstant(OffsetVal, dl, MVT::i64);
  1376. SDValue Ops[] = { Base, Offset, Chain };
  1377. SDNode *Res = CurDAG->getMachineNode(Opcode, dl, MVT::i64, DstVT,
  1378. MVT::Other, Ops);
  1379. // Transfer memoperands.
  1380. MachineMemOperand *MemOp = cast<MemSDNode>(N)->getMemOperand();
  1381. CurDAG->setNodeMemRefs(cast<MachineSDNode>(Res), {MemOp});
  1382. // Either way, we're replacing the node, so tell the caller that.
  1383. SDValue LoadedVal = SDValue(Res, 1);
  1384. if (InsertTo64) {
  1385. SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
  1386. LoadedVal =
  1387. SDValue(CurDAG->getMachineNode(
  1388. AArch64::SUBREG_TO_REG, dl, MVT::i64,
  1389. CurDAG->getTargetConstant(0, dl, MVT::i64), LoadedVal,
  1390. SubReg),
  1391. 0);
  1392. }
  1393. ReplaceUses(SDValue(N, 0), LoadedVal);
  1394. ReplaceUses(SDValue(N, 1), SDValue(Res, 0));
  1395. ReplaceUses(SDValue(N, 2), SDValue(Res, 2));
  1396. CurDAG->RemoveDeadNode(N);
  1397. return true;
  1398. }
  1399. void AArch64DAGToDAGISel::SelectLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
  1400. unsigned SubRegIdx) {
  1401. SDLoc dl(N);
  1402. EVT VT = N->getValueType(0);
  1403. SDValue Chain = N->getOperand(0);
  1404. SDValue Ops[] = {N->getOperand(2), // Mem operand;
  1405. Chain};
  1406. const EVT ResTys[] = {MVT::Untyped, MVT::Other};
  1407. SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
  1408. SDValue SuperReg = SDValue(Ld, 0);
  1409. for (unsigned i = 0; i < NumVecs; ++i)
  1410. ReplaceUses(SDValue(N, i),
  1411. CurDAG->getTargetExtractSubreg(SubRegIdx + i, dl, VT, SuperReg));
  1412. ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 1));
  1413. // Transfer memoperands. In the case of AArch64::LD64B, there won't be one,
  1414. // because it's too simple to have needed special treatment during lowering.
  1415. if (auto *MemIntr = dyn_cast<MemIntrinsicSDNode>(N)) {
  1416. MachineMemOperand *MemOp = MemIntr->getMemOperand();
  1417. CurDAG->setNodeMemRefs(cast<MachineSDNode>(Ld), {MemOp});
  1418. }
  1419. CurDAG->RemoveDeadNode(N);
  1420. }
  1421. void AArch64DAGToDAGISel::SelectPostLoad(SDNode *N, unsigned NumVecs,
  1422. unsigned Opc, unsigned SubRegIdx) {
  1423. SDLoc dl(N);
  1424. EVT VT = N->getValueType(0);
  1425. SDValue Chain = N->getOperand(0);
  1426. SDValue Ops[] = {N->getOperand(1), // Mem operand
  1427. N->getOperand(2), // Incremental
  1428. Chain};
  1429. const EVT ResTys[] = {MVT::i64, // Type of the write back register
  1430. MVT::Untyped, MVT::Other};
  1431. SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
  1432. // Update uses of write back register
  1433. ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 0));
  1434. // Update uses of vector list
  1435. SDValue SuperReg = SDValue(Ld, 1);
  1436. if (NumVecs == 1)
  1437. ReplaceUses(SDValue(N, 0), SuperReg);
  1438. else
  1439. for (unsigned i = 0; i < NumVecs; ++i)
  1440. ReplaceUses(SDValue(N, i),
  1441. CurDAG->getTargetExtractSubreg(SubRegIdx + i, dl, VT, SuperReg));
  1442. // Update the chain
  1443. ReplaceUses(SDValue(N, NumVecs + 1), SDValue(Ld, 2));
  1444. CurDAG->RemoveDeadNode(N);
  1445. }
  1446. /// Optimize \param OldBase and \param OldOffset selecting the best addressing
  1447. /// mode. Returns a tuple consisting of an Opcode, an SDValue representing the
  1448. /// new Base and an SDValue representing the new offset.
  1449. std::tuple<unsigned, SDValue, SDValue>
  1450. AArch64DAGToDAGISel::findAddrModeSVELoadStore(SDNode *N, unsigned Opc_rr,
  1451. unsigned Opc_ri,
  1452. const SDValue &OldBase,
  1453. const SDValue &OldOffset,
  1454. unsigned Scale) {
  1455. SDValue NewBase = OldBase;
  1456. SDValue NewOffset = OldOffset;
  1457. // Detect a possible Reg+Imm addressing mode.
  1458. const bool IsRegImm = SelectAddrModeIndexedSVE</*Min=*/-8, /*Max=*/7>(
  1459. N, OldBase, NewBase, NewOffset);
  1460. // Detect a possible reg+reg addressing mode, but only if we haven't already
  1461. // detected a Reg+Imm one.
  1462. const bool IsRegReg =
  1463. !IsRegImm && SelectSVERegRegAddrMode(OldBase, Scale, NewBase, NewOffset);
  1464. // Select the instruction.
  1465. return std::make_tuple(IsRegReg ? Opc_rr : Opc_ri, NewBase, NewOffset);
  1466. }
  1467. enum class SelectTypeKind {
  1468. Int1 = 0,
  1469. };
  1470. /// This function selects an opcode from a list of opcodes, which is
  1471. /// expected to be the opcode for { 8-bit, 16-bit, 32-bit, 64-bit }
  1472. /// element types, in this order.
  1473. template <SelectTypeKind Kind>
  1474. static unsigned SelectOpcodeFromVT(EVT VT, ArrayRef<unsigned> Opcodes) {
  1475. // Only match scalable vector VTs
  1476. if (!VT.isScalableVector())
  1477. return 0;
  1478. EVT EltVT = VT.getVectorElementType();
  1479. switch (Kind) {
  1480. case SelectTypeKind::Int1:
  1481. if (EltVT != MVT::i1)
  1482. return 0;
  1483. break;
  1484. }
  1485. unsigned Offset;
  1486. switch (VT.getVectorMinNumElements()) {
  1487. case 16: // 8-bit
  1488. Offset = 0;
  1489. break;
  1490. case 8: // 16-bit
  1491. Offset = 1;
  1492. break;
  1493. case 4: // 32-bit
  1494. Offset = 2;
  1495. break;
  1496. case 2: // 64-bit
  1497. Offset = 3;
  1498. break;
  1499. default:
  1500. return 0;
  1501. }
  1502. return (Opcodes.size() <= Offset) ? 0 : Opcodes[Offset];
  1503. }
  1504. void AArch64DAGToDAGISel::SelectWhilePair(SDNode *N, unsigned Opc) {
  1505. SDLoc DL(N);
  1506. EVT VT = N->getValueType(0);
  1507. SDValue Ops[] = {N->getOperand(1), N->getOperand(2)};
  1508. SDNode *WhilePair = CurDAG->getMachineNode(Opc, DL, MVT::Untyped, Ops);
  1509. SDValue SuperReg = SDValue(WhilePair, 0);
  1510. for (unsigned I = 0; I < 2; ++I)
  1511. ReplaceUses(SDValue(N, I), CurDAG->getTargetExtractSubreg(
  1512. AArch64::psub0 + I, DL, VT, SuperReg));
  1513. CurDAG->RemoveDeadNode(N);
  1514. }
  1515. void AArch64DAGToDAGISel::SelectCVTIntrinsic(SDNode *N, unsigned NumVecs,
  1516. unsigned Opcode) {
  1517. EVT VT = N->getValueType(0);
  1518. SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
  1519. SDValue Ops = createZTuple(Regs);
  1520. SDLoc DL(N);
  1521. SDNode *Intrinsic = CurDAG->getMachineNode(Opcode, DL, MVT::Untyped, Ops);
  1522. SDValue SuperReg = SDValue(Intrinsic, 0);
  1523. for (unsigned i = 0; i < NumVecs; ++i)
  1524. ReplaceUses(SDValue(N, i), CurDAG->getTargetExtractSubreg(
  1525. AArch64::zsub0 + i, DL, VT, SuperReg));
  1526. CurDAG->RemoveDeadNode(N);
  1527. return;
  1528. }
  1529. void AArch64DAGToDAGISel::SelectPredicatedLoad(SDNode *N, unsigned NumVecs,
  1530. unsigned Scale, unsigned Opc_ri,
  1531. unsigned Opc_rr, bool IsIntr) {
  1532. assert(Scale < 4 && "Invalid scaling value.");
  1533. SDLoc DL(N);
  1534. EVT VT = N->getValueType(0);
  1535. SDValue Chain = N->getOperand(0);
  1536. // Optimize addressing mode.
  1537. SDValue Base, Offset;
  1538. unsigned Opc;
  1539. std::tie(Opc, Base, Offset) = findAddrModeSVELoadStore(
  1540. N, Opc_rr, Opc_ri, N->getOperand(IsIntr ? 3 : 2),
  1541. CurDAG->getTargetConstant(0, DL, MVT::i64), Scale);
  1542. SDValue Ops[] = {N->getOperand(IsIntr ? 2 : 1), // Predicate
  1543. Base, // Memory operand
  1544. Offset, Chain};
  1545. const EVT ResTys[] = {MVT::Untyped, MVT::Other};
  1546. SDNode *Load = CurDAG->getMachineNode(Opc, DL, ResTys, Ops);
  1547. SDValue SuperReg = SDValue(Load, 0);
  1548. for (unsigned i = 0; i < NumVecs; ++i)
  1549. ReplaceUses(SDValue(N, i), CurDAG->getTargetExtractSubreg(
  1550. AArch64::zsub0 + i, DL, VT, SuperReg));
  1551. // Copy chain
  1552. unsigned ChainIdx = NumVecs;
  1553. ReplaceUses(SDValue(N, ChainIdx), SDValue(Load, 1));
  1554. CurDAG->RemoveDeadNode(N);
  1555. }
  1556. void AArch64DAGToDAGISel::SelectStore(SDNode *N, unsigned NumVecs,
  1557. unsigned Opc) {
  1558. SDLoc dl(N);
  1559. EVT VT = N->getOperand(2)->getValueType(0);
  1560. // Form a REG_SEQUENCE to force register allocation.
  1561. bool Is128Bit = VT.getSizeInBits() == 128;
  1562. SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
  1563. SDValue RegSeq = Is128Bit ? createQTuple(Regs) : createDTuple(Regs);
  1564. SDValue Ops[] = {RegSeq, N->getOperand(NumVecs + 2), N->getOperand(0)};
  1565. SDNode *St = CurDAG->getMachineNode(Opc, dl, N->getValueType(0), Ops);
  1566. // Transfer memoperands.
  1567. MachineMemOperand *MemOp = cast<MemIntrinsicSDNode>(N)->getMemOperand();
  1568. CurDAG->setNodeMemRefs(cast<MachineSDNode>(St), {MemOp});
  1569. ReplaceNode(N, St);
  1570. }
  1571. void AArch64DAGToDAGISel::SelectPredicatedStore(SDNode *N, unsigned NumVecs,
  1572. unsigned Scale, unsigned Opc_rr,
  1573. unsigned Opc_ri) {
  1574. SDLoc dl(N);
  1575. // Form a REG_SEQUENCE to force register allocation.
  1576. SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
  1577. SDValue RegSeq = createZTuple(Regs);
  1578. // Optimize addressing mode.
  1579. unsigned Opc;
  1580. SDValue Offset, Base;
  1581. std::tie(Opc, Base, Offset) = findAddrModeSVELoadStore(
  1582. N, Opc_rr, Opc_ri, N->getOperand(NumVecs + 3),
  1583. CurDAG->getTargetConstant(0, dl, MVT::i64), Scale);
  1584. SDValue Ops[] = {RegSeq, N->getOperand(NumVecs + 2), // predicate
  1585. Base, // address
  1586. Offset, // offset
  1587. N->getOperand(0)}; // chain
  1588. SDNode *St = CurDAG->getMachineNode(Opc, dl, N->getValueType(0), Ops);
  1589. ReplaceNode(N, St);
  1590. }
  1591. bool AArch64DAGToDAGISel::SelectAddrModeFrameIndexSVE(SDValue N, SDValue &Base,
  1592. SDValue &OffImm) {
  1593. SDLoc dl(N);
  1594. const DataLayout &DL = CurDAG->getDataLayout();
  1595. const TargetLowering *TLI = getTargetLowering();
  1596. // Try to match it for the frame address
  1597. if (auto FINode = dyn_cast<FrameIndexSDNode>(N)) {
  1598. int FI = FINode->getIndex();
  1599. Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
  1600. OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
  1601. return true;
  1602. }
  1603. return false;
  1604. }
  1605. void AArch64DAGToDAGISel::SelectPostStore(SDNode *N, unsigned NumVecs,
  1606. unsigned Opc) {
  1607. SDLoc dl(N);
  1608. EVT VT = N->getOperand(2)->getValueType(0);
  1609. const EVT ResTys[] = {MVT::i64, // Type of the write back register
  1610. MVT::Other}; // Type for the Chain
  1611. // Form a REG_SEQUENCE to force register allocation.
  1612. bool Is128Bit = VT.getSizeInBits() == 128;
  1613. SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
  1614. SDValue RegSeq = Is128Bit ? createQTuple(Regs) : createDTuple(Regs);
  1615. SDValue Ops[] = {RegSeq,
  1616. N->getOperand(NumVecs + 1), // base register
  1617. N->getOperand(NumVecs + 2), // Incremental
  1618. N->getOperand(0)}; // Chain
  1619. SDNode *St = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
  1620. ReplaceNode(N, St);
  1621. }
  1622. namespace {
  1623. /// WidenVector - Given a value in the V64 register class, produce the
  1624. /// equivalent value in the V128 register class.
  1625. class WidenVector {
  1626. SelectionDAG &DAG;
  1627. public:
  1628. WidenVector(SelectionDAG &DAG) : DAG(DAG) {}
  1629. SDValue operator()(SDValue V64Reg) {
  1630. EVT VT = V64Reg.getValueType();
  1631. unsigned NarrowSize = VT.getVectorNumElements();
  1632. MVT EltTy = VT.getVectorElementType().getSimpleVT();
  1633. MVT WideTy = MVT::getVectorVT(EltTy, 2 * NarrowSize);
  1634. SDLoc DL(V64Reg);
  1635. SDValue Undef =
  1636. SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, WideTy), 0);
  1637. return DAG.getTargetInsertSubreg(AArch64::dsub, DL, WideTy, Undef, V64Reg);
  1638. }
  1639. };
  1640. } // namespace
  1641. /// NarrowVector - Given a value in the V128 register class, produce the
  1642. /// equivalent value in the V64 register class.
  1643. static SDValue NarrowVector(SDValue V128Reg, SelectionDAG &DAG) {
  1644. EVT VT = V128Reg.getValueType();
  1645. unsigned WideSize = VT.getVectorNumElements();
  1646. MVT EltTy = VT.getVectorElementType().getSimpleVT();
  1647. MVT NarrowTy = MVT::getVectorVT(EltTy, WideSize / 2);
  1648. return DAG.getTargetExtractSubreg(AArch64::dsub, SDLoc(V128Reg), NarrowTy,
  1649. V128Reg);
  1650. }
  1651. void AArch64DAGToDAGISel::SelectLoadLane(SDNode *N, unsigned NumVecs,
  1652. unsigned Opc) {
  1653. SDLoc dl(N);
  1654. EVT VT = N->getValueType(0);
  1655. bool Narrow = VT.getSizeInBits() == 64;
  1656. // Form a REG_SEQUENCE to force register allocation.
  1657. SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
  1658. if (Narrow)
  1659. transform(Regs, Regs.begin(),
  1660. WidenVector(*CurDAG));
  1661. SDValue RegSeq = createQTuple(Regs);
  1662. const EVT ResTys[] = {MVT::Untyped, MVT::Other};
  1663. unsigned LaneNo =
  1664. cast<ConstantSDNode>(N->getOperand(NumVecs + 2))->getZExtValue();
  1665. SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
  1666. N->getOperand(NumVecs + 3), N->getOperand(0)};
  1667. SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
  1668. SDValue SuperReg = SDValue(Ld, 0);
  1669. EVT WideVT = RegSeq.getOperand(1)->getValueType(0);
  1670. static const unsigned QSubs[] = { AArch64::qsub0, AArch64::qsub1,
  1671. AArch64::qsub2, AArch64::qsub3 };
  1672. for (unsigned i = 0; i < NumVecs; ++i) {
  1673. SDValue NV = CurDAG->getTargetExtractSubreg(QSubs[i], dl, WideVT, SuperReg);
  1674. if (Narrow)
  1675. NV = NarrowVector(NV, *CurDAG);
  1676. ReplaceUses(SDValue(N, i), NV);
  1677. }
  1678. ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 1));
  1679. CurDAG->RemoveDeadNode(N);
  1680. }
  1681. void AArch64DAGToDAGISel::SelectPostLoadLane(SDNode *N, unsigned NumVecs,
  1682. unsigned Opc) {
  1683. SDLoc dl(N);
  1684. EVT VT = N->getValueType(0);
  1685. bool Narrow = VT.getSizeInBits() == 64;
  1686. // Form a REG_SEQUENCE to force register allocation.
  1687. SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
  1688. if (Narrow)
  1689. transform(Regs, Regs.begin(),
  1690. WidenVector(*CurDAG));
  1691. SDValue RegSeq = createQTuple(Regs);
  1692. const EVT ResTys[] = {MVT::i64, // Type of the write back register
  1693. RegSeq->getValueType(0), MVT::Other};
  1694. unsigned LaneNo =
  1695. cast<ConstantSDNode>(N->getOperand(NumVecs + 1))->getZExtValue();
  1696. SDValue Ops[] = {RegSeq,
  1697. CurDAG->getTargetConstant(LaneNo, dl,
  1698. MVT::i64), // Lane Number
  1699. N->getOperand(NumVecs + 2), // Base register
  1700. N->getOperand(NumVecs + 3), // Incremental
  1701. N->getOperand(0)};
  1702. SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
  1703. // Update uses of the write back register
  1704. ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 0));
  1705. // Update uses of the vector list
  1706. SDValue SuperReg = SDValue(Ld, 1);
  1707. if (NumVecs == 1) {
  1708. ReplaceUses(SDValue(N, 0),
  1709. Narrow ? NarrowVector(SuperReg, *CurDAG) : SuperReg);
  1710. } else {
  1711. EVT WideVT = RegSeq.getOperand(1)->getValueType(0);
  1712. static const unsigned QSubs[] = { AArch64::qsub0, AArch64::qsub1,
  1713. AArch64::qsub2, AArch64::qsub3 };
  1714. for (unsigned i = 0; i < NumVecs; ++i) {
  1715. SDValue NV = CurDAG->getTargetExtractSubreg(QSubs[i], dl, WideVT,
  1716. SuperReg);
  1717. if (Narrow)
  1718. NV = NarrowVector(NV, *CurDAG);
  1719. ReplaceUses(SDValue(N, i), NV);
  1720. }
  1721. }
  1722. // Update the Chain
  1723. ReplaceUses(SDValue(N, NumVecs + 1), SDValue(Ld, 2));
  1724. CurDAG->RemoveDeadNode(N);
  1725. }
  1726. void AArch64DAGToDAGISel::SelectStoreLane(SDNode *N, unsigned NumVecs,
  1727. unsigned Opc) {
  1728. SDLoc dl(N);
  1729. EVT VT = N->getOperand(2)->getValueType(0);
  1730. bool Narrow = VT.getSizeInBits() == 64;
  1731. // Form a REG_SEQUENCE to force register allocation.
  1732. SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
  1733. if (Narrow)
  1734. transform(Regs, Regs.begin(),
  1735. WidenVector(*CurDAG));
  1736. SDValue RegSeq = createQTuple(Regs);
  1737. unsigned LaneNo =
  1738. cast<ConstantSDNode>(N->getOperand(NumVecs + 2))->getZExtValue();
  1739. SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
  1740. N->getOperand(NumVecs + 3), N->getOperand(0)};
  1741. SDNode *St = CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops);
  1742. // Transfer memoperands.
  1743. MachineMemOperand *MemOp = cast<MemIntrinsicSDNode>(N)->getMemOperand();
  1744. CurDAG->setNodeMemRefs(cast<MachineSDNode>(St), {MemOp});
  1745. ReplaceNode(N, St);
  1746. }
  1747. void AArch64DAGToDAGISel::SelectPostStoreLane(SDNode *N, unsigned NumVecs,
  1748. unsigned Opc) {
  1749. SDLoc dl(N);
  1750. EVT VT = N->getOperand(2)->getValueType(0);
  1751. bool Narrow = VT.getSizeInBits() == 64;
  1752. // Form a REG_SEQUENCE to force register allocation.
  1753. SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
  1754. if (Narrow)
  1755. transform(Regs, Regs.begin(),
  1756. WidenVector(*CurDAG));
  1757. SDValue RegSeq = createQTuple(Regs);
  1758. const EVT ResTys[] = {MVT::i64, // Type of the write back register
  1759. MVT::Other};
  1760. unsigned LaneNo =
  1761. cast<ConstantSDNode>(N->getOperand(NumVecs + 1))->getZExtValue();
  1762. SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
  1763. N->getOperand(NumVecs + 2), // Base Register
  1764. N->getOperand(NumVecs + 3), // Incremental
  1765. N->getOperand(0)};
  1766. SDNode *St = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
  1767. // Transfer memoperands.
  1768. MachineMemOperand *MemOp = cast<MemIntrinsicSDNode>(N)->getMemOperand();
  1769. CurDAG->setNodeMemRefs(cast<MachineSDNode>(St), {MemOp});
  1770. ReplaceNode(N, St);
  1771. }
  1772. static bool isBitfieldExtractOpFromAnd(SelectionDAG *CurDAG, SDNode *N,
  1773. unsigned &Opc, SDValue &Opd0,
  1774. unsigned &LSB, unsigned &MSB,
  1775. unsigned NumberOfIgnoredLowBits,
  1776. bool BiggerPattern) {
  1777. assert(N->getOpcode() == ISD::AND &&
  1778. "N must be a AND operation to call this function");
  1779. EVT VT = N->getValueType(0);
  1780. // Here we can test the type of VT and return false when the type does not
  1781. // match, but since it is done prior to that call in the current context
  1782. // we turned that into an assert to avoid redundant code.
  1783. assert((VT == MVT::i32 || VT == MVT::i64) &&
  1784. "Type checking must have been done before calling this function");
  1785. // FIXME: simplify-demanded-bits in DAGCombine will probably have
  1786. // changed the AND node to a 32-bit mask operation. We'll have to
  1787. // undo that as part of the transform here if we want to catch all
  1788. // the opportunities.
  1789. // Currently the NumberOfIgnoredLowBits argument helps to recover
  1790. // from these situations when matching bigger pattern (bitfield insert).
  1791. // For unsigned extracts, check for a shift right and mask
  1792. uint64_t AndImm = 0;
  1793. if (!isOpcWithIntImmediate(N, ISD::AND, AndImm))
  1794. return false;
  1795. const SDNode *Op0 = N->getOperand(0).getNode();
  1796. // Because of simplify-demanded-bits in DAGCombine, the mask may have been
  1797. // simplified. Try to undo that
  1798. AndImm |= maskTrailingOnes<uint64_t>(NumberOfIgnoredLowBits);
  1799. // The immediate is a mask of the low bits iff imm & (imm+1) == 0
  1800. if (AndImm & (AndImm + 1))
  1801. return false;
  1802. bool ClampMSB = false;
  1803. uint64_t SrlImm = 0;
  1804. // Handle the SRL + ANY_EXTEND case.
  1805. if (VT == MVT::i64 && Op0->getOpcode() == ISD::ANY_EXTEND &&
  1806. isOpcWithIntImmediate(Op0->getOperand(0).getNode(), ISD::SRL, SrlImm)) {
  1807. // Extend the incoming operand of the SRL to 64-bit.
  1808. Opd0 = Widen(CurDAG, Op0->getOperand(0).getOperand(0));
  1809. // Make sure to clamp the MSB so that we preserve the semantics of the
  1810. // original operations.
  1811. ClampMSB = true;
  1812. } else if (VT == MVT::i32 && Op0->getOpcode() == ISD::TRUNCATE &&
  1813. isOpcWithIntImmediate(Op0->getOperand(0).getNode(), ISD::SRL,
  1814. SrlImm)) {
  1815. // If the shift result was truncated, we can still combine them.
  1816. Opd0 = Op0->getOperand(0).getOperand(0);
  1817. // Use the type of SRL node.
  1818. VT = Opd0->getValueType(0);
  1819. } else if (isOpcWithIntImmediate(Op0, ISD::SRL, SrlImm)) {
  1820. Opd0 = Op0->getOperand(0);
  1821. ClampMSB = (VT == MVT::i32);
  1822. } else if (BiggerPattern) {
  1823. // Let's pretend a 0 shift right has been performed.
  1824. // The resulting code will be at least as good as the original one
  1825. // plus it may expose more opportunities for bitfield insert pattern.
  1826. // FIXME: Currently we limit this to the bigger pattern, because
  1827. // some optimizations expect AND and not UBFM.
  1828. Opd0 = N->getOperand(0);
  1829. } else
  1830. return false;
  1831. // Bail out on large immediates. This happens when no proper
  1832. // combining/constant folding was performed.
  1833. if (!BiggerPattern && (SrlImm <= 0 || SrlImm >= VT.getSizeInBits())) {
  1834. LLVM_DEBUG(
  1835. (dbgs() << N
  1836. << ": Found large shift immediate, this should not happen\n"));
  1837. return false;
  1838. }
  1839. LSB = SrlImm;
  1840. MSB = SrlImm + (VT == MVT::i32 ? countTrailingOnes<uint32_t>(AndImm)
  1841. : countTrailingOnes<uint64_t>(AndImm)) -
  1842. 1;
  1843. if (ClampMSB)
  1844. // Since we're moving the extend before the right shift operation, we need
  1845. // to clamp the MSB to make sure we don't shift in undefined bits instead of
  1846. // the zeros which would get shifted in with the original right shift
  1847. // operation.
  1848. MSB = MSB > 31 ? 31 : MSB;
  1849. Opc = VT == MVT::i32 ? AArch64::UBFMWri : AArch64::UBFMXri;
  1850. return true;
  1851. }
  1852. static bool isBitfieldExtractOpFromSExtInReg(SDNode *N, unsigned &Opc,
  1853. SDValue &Opd0, unsigned &Immr,
  1854. unsigned &Imms) {
  1855. assert(N->getOpcode() == ISD::SIGN_EXTEND_INREG);
  1856. EVT VT = N->getValueType(0);
  1857. unsigned BitWidth = VT.getSizeInBits();
  1858. assert((VT == MVT::i32 || VT == MVT::i64) &&
  1859. "Type checking must have been done before calling this function");
  1860. SDValue Op = N->getOperand(0);
  1861. if (Op->getOpcode() == ISD::TRUNCATE) {
  1862. Op = Op->getOperand(0);
  1863. VT = Op->getValueType(0);
  1864. BitWidth = VT.getSizeInBits();
  1865. }
  1866. uint64_t ShiftImm;
  1867. if (!isOpcWithIntImmediate(Op.getNode(), ISD::SRL, ShiftImm) &&
  1868. !isOpcWithIntImmediate(Op.getNode(), ISD::SRA, ShiftImm))
  1869. return false;
  1870. unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
  1871. if (ShiftImm + Width > BitWidth)
  1872. return false;
  1873. Opc = (VT == MVT::i32) ? AArch64::SBFMWri : AArch64::SBFMXri;
  1874. Opd0 = Op.getOperand(0);
  1875. Immr = ShiftImm;
  1876. Imms = ShiftImm + Width - 1;
  1877. return true;
  1878. }
  1879. static bool isSeveralBitsExtractOpFromShr(SDNode *N, unsigned &Opc,
  1880. SDValue &Opd0, unsigned &LSB,
  1881. unsigned &MSB) {
  1882. // We are looking for the following pattern which basically extracts several
  1883. // continuous bits from the source value and places it from the LSB of the
  1884. // destination value, all other bits of the destination value or set to zero:
  1885. //
  1886. // Value2 = AND Value, MaskImm
  1887. // SRL Value2, ShiftImm
  1888. //
  1889. // with MaskImm >> ShiftImm to search for the bit width.
  1890. //
  1891. // This gets selected into a single UBFM:
  1892. //
  1893. // UBFM Value, ShiftImm, findLastSet(MaskImm)
  1894. //
  1895. if (N->getOpcode() != ISD::SRL)
  1896. return false;
  1897. uint64_t AndMask = 0;
  1898. if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, AndMask))
  1899. return false;
  1900. Opd0 = N->getOperand(0).getOperand(0);
  1901. uint64_t SrlImm = 0;
  1902. if (!isIntImmediate(N->getOperand(1), SrlImm))
  1903. return false;
  1904. // Check whether we really have several bits extract here.
  1905. if (!isMask_64(AndMask >> SrlImm))
  1906. return false;
  1907. Opc = N->getValueType(0) == MVT::i32 ? AArch64::UBFMWri : AArch64::UBFMXri;
  1908. LSB = SrlImm;
  1909. MSB = findLastSet(AndMask, ZB_Undefined);
  1910. return true;
  1911. }
  1912. static bool isBitfieldExtractOpFromShr(SDNode *N, unsigned &Opc, SDValue &Opd0,
  1913. unsigned &Immr, unsigned &Imms,
  1914. bool BiggerPattern) {
  1915. assert((N->getOpcode() == ISD::SRA || N->getOpcode() == ISD::SRL) &&
  1916. "N must be a SHR/SRA operation to call this function");
  1917. EVT VT = N->getValueType(0);
  1918. // Here we can test the type of VT and return false when the type does not
  1919. // match, but since it is done prior to that call in the current context
  1920. // we turned that into an assert to avoid redundant code.
  1921. assert((VT == MVT::i32 || VT == MVT::i64) &&
  1922. "Type checking must have been done before calling this function");
  1923. // Check for AND + SRL doing several bits extract.
  1924. if (isSeveralBitsExtractOpFromShr(N, Opc, Opd0, Immr, Imms))
  1925. return true;
  1926. // We're looking for a shift of a shift.
  1927. uint64_t ShlImm = 0;
  1928. uint64_t TruncBits = 0;
  1929. if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, ShlImm)) {
  1930. Opd0 = N->getOperand(0).getOperand(0);
  1931. } else if (VT == MVT::i32 && N->getOpcode() == ISD::SRL &&
  1932. N->getOperand(0).getNode()->getOpcode() == ISD::TRUNCATE) {
  1933. // We are looking for a shift of truncate. Truncate from i64 to i32 could
  1934. // be considered as setting high 32 bits as zero. Our strategy here is to
  1935. // always generate 64bit UBFM. This consistency will help the CSE pass
  1936. // later find more redundancy.
  1937. Opd0 = N->getOperand(0).getOperand(0);
  1938. TruncBits = Opd0->getValueType(0).getSizeInBits() - VT.getSizeInBits();
  1939. VT = Opd0.getValueType();
  1940. assert(VT == MVT::i64 && "the promoted type should be i64");
  1941. } else if (BiggerPattern) {
  1942. // Let's pretend a 0 shift left has been performed.
  1943. // FIXME: Currently we limit this to the bigger pattern case,
  1944. // because some optimizations expect AND and not UBFM
  1945. Opd0 = N->getOperand(0);
  1946. } else
  1947. return false;
  1948. // Missing combines/constant folding may have left us with strange
  1949. // constants.
  1950. if (ShlImm >= VT.getSizeInBits()) {
  1951. LLVM_DEBUG(
  1952. (dbgs() << N
  1953. << ": Found large shift immediate, this should not happen\n"));
  1954. return false;
  1955. }
  1956. uint64_t SrlImm = 0;
  1957. if (!isIntImmediate(N->getOperand(1), SrlImm))
  1958. return false;
  1959. assert(SrlImm > 0 && SrlImm < VT.getSizeInBits() &&
  1960. "bad amount in shift node!");
  1961. int immr = SrlImm - ShlImm;
  1962. Immr = immr < 0 ? immr + VT.getSizeInBits() : immr;
  1963. Imms = VT.getSizeInBits() - ShlImm - TruncBits - 1;
  1964. // SRA requires a signed extraction
  1965. if (VT == MVT::i32)
  1966. Opc = N->getOpcode() == ISD::SRA ? AArch64::SBFMWri : AArch64::UBFMWri;
  1967. else
  1968. Opc = N->getOpcode() == ISD::SRA ? AArch64::SBFMXri : AArch64::UBFMXri;
  1969. return true;
  1970. }
  1971. bool AArch64DAGToDAGISel::tryBitfieldExtractOpFromSExt(SDNode *N) {
  1972. assert(N->getOpcode() == ISD::SIGN_EXTEND);
  1973. EVT VT = N->getValueType(0);
  1974. EVT NarrowVT = N->getOperand(0)->getValueType(0);
  1975. if (VT != MVT::i64 || NarrowVT != MVT::i32)
  1976. return false;
  1977. uint64_t ShiftImm;
  1978. SDValue Op = N->getOperand(0);
  1979. if (!isOpcWithIntImmediate(Op.getNode(), ISD::SRA, ShiftImm))
  1980. return false;
  1981. SDLoc dl(N);
  1982. // Extend the incoming operand of the shift to 64-bits.
  1983. SDValue Opd0 = Widen(CurDAG, Op.getOperand(0));
  1984. unsigned Immr = ShiftImm;
  1985. unsigned Imms = NarrowVT.getSizeInBits() - 1;
  1986. SDValue Ops[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, VT),
  1987. CurDAG->getTargetConstant(Imms, dl, VT)};
  1988. CurDAG->SelectNodeTo(N, AArch64::SBFMXri, VT, Ops);
  1989. return true;
  1990. }
  1991. /// Try to form fcvtl2 instructions from a floating-point extend of a high-half
  1992. /// extract of a subvector.
  1993. bool AArch64DAGToDAGISel::tryHighFPExt(SDNode *N) {
  1994. assert(N->getOpcode() == ISD::FP_EXTEND);
  1995. // There are 2 forms of fcvtl2 - extend to double or extend to float.
  1996. SDValue Extract = N->getOperand(0);
  1997. EVT VT = N->getValueType(0);
  1998. EVT NarrowVT = Extract.getValueType();
  1999. if ((VT != MVT::v2f64 || NarrowVT != MVT::v2f32) &&
  2000. (VT != MVT::v4f32 || NarrowVT != MVT::v4f16))
  2001. return false;
  2002. // Optionally look past a bitcast.
  2003. Extract = peekThroughBitcasts(Extract);
  2004. if (Extract.getOpcode() != ISD::EXTRACT_SUBVECTOR)
  2005. return false;
  2006. // Match extract from start of high half index.
  2007. // Example: v8i16 -> v4i16 means the extract must begin at index 4.
  2008. unsigned ExtractIndex = Extract.getConstantOperandVal(1);
  2009. if (ExtractIndex != Extract.getValueType().getVectorNumElements())
  2010. return false;
  2011. auto Opcode = VT == MVT::v2f64 ? AArch64::FCVTLv4i32 : AArch64::FCVTLv8i16;
  2012. CurDAG->SelectNodeTo(N, Opcode, VT, Extract.getOperand(0));
  2013. return true;
  2014. }
  2015. static bool isBitfieldExtractOp(SelectionDAG *CurDAG, SDNode *N, unsigned &Opc,
  2016. SDValue &Opd0, unsigned &Immr, unsigned &Imms,
  2017. unsigned NumberOfIgnoredLowBits = 0,
  2018. bool BiggerPattern = false) {
  2019. if (N->getValueType(0) != MVT::i32 && N->getValueType(0) != MVT::i64)
  2020. return false;
  2021. switch (N->getOpcode()) {
  2022. default:
  2023. if (!N->isMachineOpcode())
  2024. return false;
  2025. break;
  2026. case ISD::AND:
  2027. return isBitfieldExtractOpFromAnd(CurDAG, N, Opc, Opd0, Immr, Imms,
  2028. NumberOfIgnoredLowBits, BiggerPattern);
  2029. case ISD::SRL:
  2030. case ISD::SRA:
  2031. return isBitfieldExtractOpFromShr(N, Opc, Opd0, Immr, Imms, BiggerPattern);
  2032. case ISD::SIGN_EXTEND_INREG:
  2033. return isBitfieldExtractOpFromSExtInReg(N, Opc, Opd0, Immr, Imms);
  2034. }
  2035. unsigned NOpc = N->getMachineOpcode();
  2036. switch (NOpc) {
  2037. default:
  2038. return false;
  2039. case AArch64::SBFMWri:
  2040. case AArch64::UBFMWri:
  2041. case AArch64::SBFMXri:
  2042. case AArch64::UBFMXri:
  2043. Opc = NOpc;
  2044. Opd0 = N->getOperand(0);
  2045. Immr = cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
  2046. Imms = cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
  2047. return true;
  2048. }
  2049. // Unreachable
  2050. return false;
  2051. }
  2052. bool AArch64DAGToDAGISel::tryBitfieldExtractOp(SDNode *N) {
  2053. unsigned Opc, Immr, Imms;
  2054. SDValue Opd0;
  2055. if (!isBitfieldExtractOp(CurDAG, N, Opc, Opd0, Immr, Imms))
  2056. return false;
  2057. EVT VT = N->getValueType(0);
  2058. SDLoc dl(N);
  2059. // If the bit extract operation is 64bit but the original type is 32bit, we
  2060. // need to add one EXTRACT_SUBREG.
  2061. if ((Opc == AArch64::SBFMXri || Opc == AArch64::UBFMXri) && VT == MVT::i32) {
  2062. SDValue Ops64[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, MVT::i64),
  2063. CurDAG->getTargetConstant(Imms, dl, MVT::i64)};
  2064. SDNode *BFM = CurDAG->getMachineNode(Opc, dl, MVT::i64, Ops64);
  2065. SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
  2066. ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl,
  2067. MVT::i32, SDValue(BFM, 0), SubReg));
  2068. return true;
  2069. }
  2070. SDValue Ops[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, VT),
  2071. CurDAG->getTargetConstant(Imms, dl, VT)};
  2072. CurDAG->SelectNodeTo(N, Opc, VT, Ops);
  2073. return true;
  2074. }
  2075. /// Does DstMask form a complementary pair with the mask provided by
  2076. /// BitsToBeInserted, suitable for use in a BFI instruction. Roughly speaking,
  2077. /// this asks whether DstMask zeroes precisely those bits that will be set by
  2078. /// the other half.
  2079. static bool isBitfieldDstMask(uint64_t DstMask, const APInt &BitsToBeInserted,
  2080. unsigned NumberOfIgnoredHighBits, EVT VT) {
  2081. assert((VT == MVT::i32 || VT == MVT::i64) &&
  2082. "i32 or i64 mask type expected!");
  2083. unsigned BitWidth = VT.getSizeInBits() - NumberOfIgnoredHighBits;
  2084. APInt SignificantDstMask = APInt(BitWidth, DstMask);
  2085. APInt SignificantBitsToBeInserted = BitsToBeInserted.zextOrTrunc(BitWidth);
  2086. return (SignificantDstMask & SignificantBitsToBeInserted) == 0 &&
  2087. (SignificantDstMask | SignificantBitsToBeInserted).isAllOnes();
  2088. }
  2089. // Look for bits that will be useful for later uses.
  2090. // A bit is consider useless as soon as it is dropped and never used
  2091. // before it as been dropped.
  2092. // E.g., looking for useful bit of x
  2093. // 1. y = x & 0x7
  2094. // 2. z = y >> 2
  2095. // After #1, x useful bits are 0x7, then the useful bits of x, live through
  2096. // y.
  2097. // After #2, the useful bits of x are 0x4.
  2098. // However, if x is used on an unpredicatable instruction, then all its bits
  2099. // are useful.
  2100. // E.g.
  2101. // 1. y = x & 0x7
  2102. // 2. z = y >> 2
  2103. // 3. str x, [@x]
  2104. static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth = 0);
  2105. static void getUsefulBitsFromAndWithImmediate(SDValue Op, APInt &UsefulBits,
  2106. unsigned Depth) {
  2107. uint64_t Imm =
  2108. cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
  2109. Imm = AArch64_AM::decodeLogicalImmediate(Imm, UsefulBits.getBitWidth());
  2110. UsefulBits &= APInt(UsefulBits.getBitWidth(), Imm);
  2111. getUsefulBits(Op, UsefulBits, Depth + 1);
  2112. }
  2113. static void getUsefulBitsFromBitfieldMoveOpd(SDValue Op, APInt &UsefulBits,
  2114. uint64_t Imm, uint64_t MSB,
  2115. unsigned Depth) {
  2116. // inherit the bitwidth value
  2117. APInt OpUsefulBits(UsefulBits);
  2118. OpUsefulBits = 1;
  2119. if (MSB >= Imm) {
  2120. OpUsefulBits <<= MSB - Imm + 1;
  2121. --OpUsefulBits;
  2122. // The interesting part will be in the lower part of the result
  2123. getUsefulBits(Op, OpUsefulBits, Depth + 1);
  2124. // The interesting part was starting at Imm in the argument
  2125. OpUsefulBits <<= Imm;
  2126. } else {
  2127. OpUsefulBits <<= MSB + 1;
  2128. --OpUsefulBits;
  2129. // The interesting part will be shifted in the result
  2130. OpUsefulBits <<= OpUsefulBits.getBitWidth() - Imm;
  2131. getUsefulBits(Op, OpUsefulBits, Depth + 1);
  2132. // The interesting part was at zero in the argument
  2133. OpUsefulBits.lshrInPlace(OpUsefulBits.getBitWidth() - Imm);
  2134. }
  2135. UsefulBits &= OpUsefulBits;
  2136. }
  2137. static void getUsefulBitsFromUBFM(SDValue Op, APInt &UsefulBits,
  2138. unsigned Depth) {
  2139. uint64_t Imm =
  2140. cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
  2141. uint64_t MSB =
  2142. cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
  2143. getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
  2144. }
  2145. static void getUsefulBitsFromOrWithShiftedReg(SDValue Op, APInt &UsefulBits,
  2146. unsigned Depth) {
  2147. uint64_t ShiftTypeAndValue =
  2148. cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
  2149. APInt Mask(UsefulBits);
  2150. Mask.clearAllBits();
  2151. Mask.flipAllBits();
  2152. if (AArch64_AM::getShiftType(ShiftTypeAndValue) == AArch64_AM::LSL) {
  2153. // Shift Left
  2154. uint64_t ShiftAmt = AArch64_AM::getShiftValue(ShiftTypeAndValue);
  2155. Mask <<= ShiftAmt;
  2156. getUsefulBits(Op, Mask, Depth + 1);
  2157. Mask.lshrInPlace(ShiftAmt);
  2158. } else if (AArch64_AM::getShiftType(ShiftTypeAndValue) == AArch64_AM::LSR) {
  2159. // Shift Right
  2160. // We do not handle AArch64_AM::ASR, because the sign will change the
  2161. // number of useful bits
  2162. uint64_t ShiftAmt = AArch64_AM::getShiftValue(ShiftTypeAndValue);
  2163. Mask.lshrInPlace(ShiftAmt);
  2164. getUsefulBits(Op, Mask, Depth + 1);
  2165. Mask <<= ShiftAmt;
  2166. } else
  2167. return;
  2168. UsefulBits &= Mask;
  2169. }
  2170. static void getUsefulBitsFromBFM(SDValue Op, SDValue Orig, APInt &UsefulBits,
  2171. unsigned Depth) {
  2172. uint64_t Imm =
  2173. cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
  2174. uint64_t MSB =
  2175. cast<const ConstantSDNode>(Op.getOperand(3).getNode())->getZExtValue();
  2176. APInt OpUsefulBits(UsefulBits);
  2177. OpUsefulBits = 1;
  2178. APInt ResultUsefulBits(UsefulBits.getBitWidth(), 0);
  2179. ResultUsefulBits.flipAllBits();
  2180. APInt Mask(UsefulBits.getBitWidth(), 0);
  2181. getUsefulBits(Op, ResultUsefulBits, Depth + 1);
  2182. if (MSB >= Imm) {
  2183. // The instruction is a BFXIL.
  2184. uint64_t Width = MSB - Imm + 1;
  2185. uint64_t LSB = Imm;
  2186. OpUsefulBits <<= Width;
  2187. --OpUsefulBits;
  2188. if (Op.getOperand(1) == Orig) {
  2189. // Copy the low bits from the result to bits starting from LSB.
  2190. Mask = ResultUsefulBits & OpUsefulBits;
  2191. Mask <<= LSB;
  2192. }
  2193. if (Op.getOperand(0) == Orig)
  2194. // Bits starting from LSB in the input contribute to the result.
  2195. Mask |= (ResultUsefulBits & ~OpUsefulBits);
  2196. } else {
  2197. // The instruction is a BFI.
  2198. uint64_t Width = MSB + 1;
  2199. uint64_t LSB = UsefulBits.getBitWidth() - Imm;
  2200. OpUsefulBits <<= Width;
  2201. --OpUsefulBits;
  2202. OpUsefulBits <<= LSB;
  2203. if (Op.getOperand(1) == Orig) {
  2204. // Copy the bits from the result to the zero bits.
  2205. Mask = ResultUsefulBits & OpUsefulBits;
  2206. Mask.lshrInPlace(LSB);
  2207. }
  2208. if (Op.getOperand(0) == Orig)
  2209. Mask |= (ResultUsefulBits & ~OpUsefulBits);
  2210. }
  2211. UsefulBits &= Mask;
  2212. }
  2213. static void getUsefulBitsForUse(SDNode *UserNode, APInt &UsefulBits,
  2214. SDValue Orig, unsigned Depth) {
  2215. // Users of this node should have already been instruction selected
  2216. // FIXME: Can we turn that into an assert?
  2217. if (!UserNode->isMachineOpcode())
  2218. return;
  2219. switch (UserNode->getMachineOpcode()) {
  2220. default:
  2221. return;
  2222. case AArch64::ANDSWri:
  2223. case AArch64::ANDSXri:
  2224. case AArch64::ANDWri:
  2225. case AArch64::ANDXri:
  2226. // We increment Depth only when we call the getUsefulBits
  2227. return getUsefulBitsFromAndWithImmediate(SDValue(UserNode, 0), UsefulBits,
  2228. Depth);
  2229. case AArch64::UBFMWri:
  2230. case AArch64::UBFMXri:
  2231. return getUsefulBitsFromUBFM(SDValue(UserNode, 0), UsefulBits, Depth);
  2232. case AArch64::ORRWrs:
  2233. case AArch64::ORRXrs:
  2234. if (UserNode->getOperand(0) != Orig && UserNode->getOperand(1) == Orig)
  2235. getUsefulBitsFromOrWithShiftedReg(SDValue(UserNode, 0), UsefulBits,
  2236. Depth);
  2237. return;
  2238. case AArch64::BFMWri:
  2239. case AArch64::BFMXri:
  2240. return getUsefulBitsFromBFM(SDValue(UserNode, 0), Orig, UsefulBits, Depth);
  2241. case AArch64::STRBBui:
  2242. case AArch64::STURBBi:
  2243. if (UserNode->getOperand(0) != Orig)
  2244. return;
  2245. UsefulBits &= APInt(UsefulBits.getBitWidth(), 0xff);
  2246. return;
  2247. case AArch64::STRHHui:
  2248. case AArch64::STURHHi:
  2249. if (UserNode->getOperand(0) != Orig)
  2250. return;
  2251. UsefulBits &= APInt(UsefulBits.getBitWidth(), 0xffff);
  2252. return;
  2253. }
  2254. }
  2255. static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth) {
  2256. if (Depth >= SelectionDAG::MaxRecursionDepth)
  2257. return;
  2258. // Initialize UsefulBits
  2259. if (!Depth) {
  2260. unsigned Bitwidth = Op.getScalarValueSizeInBits();
  2261. // At the beginning, assume every produced bits is useful
  2262. UsefulBits = APInt(Bitwidth, 0);
  2263. UsefulBits.flipAllBits();
  2264. }
  2265. APInt UsersUsefulBits(UsefulBits.getBitWidth(), 0);
  2266. for (SDNode *Node : Op.getNode()->uses()) {
  2267. // A use cannot produce useful bits
  2268. APInt UsefulBitsForUse = APInt(UsefulBits);
  2269. getUsefulBitsForUse(Node, UsefulBitsForUse, Op, Depth);
  2270. UsersUsefulBits |= UsefulBitsForUse;
  2271. }
  2272. // UsefulBits contains the produced bits that are meaningful for the
  2273. // current definition, thus a user cannot make a bit meaningful at
  2274. // this point
  2275. UsefulBits &= UsersUsefulBits;
  2276. }
  2277. /// Create a machine node performing a notional SHL of Op by ShlAmount. If
  2278. /// ShlAmount is negative, do a (logical) right-shift instead. If ShlAmount is
  2279. /// 0, return Op unchanged.
  2280. static SDValue getLeftShift(SelectionDAG *CurDAG, SDValue Op, int ShlAmount) {
  2281. if (ShlAmount == 0)
  2282. return Op;
  2283. EVT VT = Op.getValueType();
  2284. SDLoc dl(Op);
  2285. unsigned BitWidth = VT.getSizeInBits();
  2286. unsigned UBFMOpc = BitWidth == 32 ? AArch64::UBFMWri : AArch64::UBFMXri;
  2287. SDNode *ShiftNode;
  2288. if (ShlAmount > 0) {
  2289. // LSL wD, wN, #Amt == UBFM wD, wN, #32-Amt, #31-Amt
  2290. ShiftNode = CurDAG->getMachineNode(
  2291. UBFMOpc, dl, VT, Op,
  2292. CurDAG->getTargetConstant(BitWidth - ShlAmount, dl, VT),
  2293. CurDAG->getTargetConstant(BitWidth - 1 - ShlAmount, dl, VT));
  2294. } else {
  2295. // LSR wD, wN, #Amt == UBFM wD, wN, #Amt, #32-1
  2296. assert(ShlAmount < 0 && "expected right shift");
  2297. int ShrAmount = -ShlAmount;
  2298. ShiftNode = CurDAG->getMachineNode(
  2299. UBFMOpc, dl, VT, Op, CurDAG->getTargetConstant(ShrAmount, dl, VT),
  2300. CurDAG->getTargetConstant(BitWidth - 1, dl, VT));
  2301. }
  2302. return SDValue(ShiftNode, 0);
  2303. }
  2304. // For bit-field-positioning pattern "(and (shl VAL, N), ShiftedMask)".
  2305. static bool isBitfieldPositioningOpFromAnd(SelectionDAG *CurDAG, SDValue Op,
  2306. bool BiggerPattern,
  2307. const uint64_t NonZeroBits,
  2308. SDValue &Src, int &DstLSB,
  2309. int &Width);
  2310. // For bit-field-positioning pattern "shl VAL, N)".
  2311. static bool isBitfieldPositioningOpFromShl(SelectionDAG *CurDAG, SDValue Op,
  2312. bool BiggerPattern,
  2313. const uint64_t NonZeroBits,
  2314. SDValue &Src, int &DstLSB,
  2315. int &Width);
  2316. /// Does this tree qualify as an attempt to move a bitfield into position,
  2317. /// essentially "(and (shl VAL, N), Mask)" or (shl VAL, N).
  2318. static bool isBitfieldPositioningOp(SelectionDAG *CurDAG, SDValue Op,
  2319. bool BiggerPattern, SDValue &Src,
  2320. int &DstLSB, int &Width) {
  2321. EVT VT = Op.getValueType();
  2322. unsigned BitWidth = VT.getSizeInBits();
  2323. (void)BitWidth;
  2324. assert(BitWidth == 32 || BitWidth == 64);
  2325. KnownBits Known = CurDAG->computeKnownBits(Op);
  2326. // Non-zero in the sense that they're not provably zero, which is the key
  2327. // point if we want to use this value
  2328. const uint64_t NonZeroBits = (~Known.Zero).getZExtValue();
  2329. if (!isShiftedMask_64(NonZeroBits))
  2330. return false;
  2331. switch (Op.getOpcode()) {
  2332. default:
  2333. break;
  2334. case ISD::AND:
  2335. return isBitfieldPositioningOpFromAnd(CurDAG, Op, BiggerPattern,
  2336. NonZeroBits, Src, DstLSB, Width);
  2337. case ISD::SHL:
  2338. return isBitfieldPositioningOpFromShl(CurDAG, Op, BiggerPattern,
  2339. NonZeroBits, Src, DstLSB, Width);
  2340. }
  2341. return false;
  2342. }
  2343. static bool isBitfieldPositioningOpFromAnd(SelectionDAG *CurDAG, SDValue Op,
  2344. bool BiggerPattern,
  2345. const uint64_t NonZeroBits,
  2346. SDValue &Src, int &DstLSB,
  2347. int &Width) {
  2348. assert(isShiftedMask_64(NonZeroBits) && "Caller guaranteed");
  2349. EVT VT = Op.getValueType();
  2350. assert((VT == MVT::i32 || VT == MVT::i64) &&
  2351. "Caller guarantees VT is one of i32 or i64");
  2352. (void)VT;
  2353. uint64_t AndImm;
  2354. if (!isOpcWithIntImmediate(Op.getNode(), ISD::AND, AndImm))
  2355. return false;
  2356. // If (~AndImm & NonZeroBits) is not zero at POS, we know that
  2357. // 1) (AndImm & (1 << POS) == 0)
  2358. // 2) the result of AND is not zero at POS bit (according to NonZeroBits)
  2359. //
  2360. // 1) and 2) don't agree so something must be wrong (e.g., in
  2361. // 'SelectionDAG::computeKnownBits')
  2362. assert((~AndImm & NonZeroBits) == 0 &&
  2363. "Something must be wrong (e.g., in SelectionDAG::computeKnownBits)");
  2364. SDValue AndOp0 = Op.getOperand(0);
  2365. uint64_t ShlImm;
  2366. SDValue ShlOp0;
  2367. if (isOpcWithIntImmediate(AndOp0.getNode(), ISD::SHL, ShlImm)) {
  2368. // For pattern "and(shl(val, N), shifted-mask)", 'ShlOp0' is set to 'val'.
  2369. ShlOp0 = AndOp0.getOperand(0);
  2370. } else if (VT == MVT::i64 && AndOp0.getOpcode() == ISD::ANY_EXTEND &&
  2371. isOpcWithIntImmediate(AndOp0.getOperand(0).getNode(), ISD::SHL,
  2372. ShlImm)) {
  2373. // For pattern "and(any_extend(shl(val, N)), shifted-mask)"
  2374. // ShlVal == shl(val, N), which is a left shift on a smaller type.
  2375. SDValue ShlVal = AndOp0.getOperand(0);
  2376. // Since this is after type legalization and ShlVal is extended to MVT::i64,
  2377. // expect VT to be MVT::i32.
  2378. assert((ShlVal.getValueType() == MVT::i32) && "Expect VT to be MVT::i32.");
  2379. // Widens 'val' to MVT::i64 as the source of bit field positioning.
  2380. ShlOp0 = Widen(CurDAG, ShlVal.getOperand(0));
  2381. } else
  2382. return false;
  2383. // For !BiggerPattern, bail out if the AndOp0 has more than one use, since
  2384. // then we'll end up generating AndOp0+UBFIZ instead of just keeping
  2385. // AndOp0+AND.
  2386. if (!BiggerPattern && !AndOp0.hasOneUse())
  2387. return false;
  2388. DstLSB = countTrailingZeros(NonZeroBits);
  2389. Width = countTrailingOnes(NonZeroBits >> DstLSB);
  2390. // Bail out on large Width. This happens when no proper combining / constant
  2391. // folding was performed.
  2392. if (Width >= (int)VT.getSizeInBits()) {
  2393. // If VT is i64, Width > 64 is insensible since NonZeroBits is uint64_t, and
  2394. // Width == 64 indicates a missed dag-combine from "(and val, AllOnes)" to
  2395. // "val".
  2396. // If VT is i32, what Width >= 32 means:
  2397. // - For "(and (any_extend(shl val, N)), shifted-mask)", the`and` Op
  2398. // demands at least 'Width' bits (after dag-combiner). This together with
  2399. // `any_extend` Op (undefined higher bits) indicates missed combination
  2400. // when lowering the 'and' IR instruction to an machine IR instruction.
  2401. LLVM_DEBUG(
  2402. dbgs()
  2403. << "Found large Width in bit-field-positioning -- this indicates no "
  2404. "proper combining / constant folding was performed\n");
  2405. return false;
  2406. }
  2407. // BFI encompasses sufficiently many nodes that it's worth inserting an extra
  2408. // LSL/LSR if the mask in NonZeroBits doesn't quite match up with the ISD::SHL
  2409. // amount. BiggerPattern is true when this pattern is being matched for BFI,
  2410. // BiggerPattern is false when this pattern is being matched for UBFIZ, in
  2411. // which case it is not profitable to insert an extra shift.
  2412. if (ShlImm != uint64_t(DstLSB) && !BiggerPattern)
  2413. return false;
  2414. Src = getLeftShift(CurDAG, ShlOp0, ShlImm - DstLSB);
  2415. return true;
  2416. }
  2417. // For node (shl (and val, mask), N)), returns true if the node is equivalent to
  2418. // UBFIZ.
  2419. static bool isSeveralBitsPositioningOpFromShl(const uint64_t ShlImm, SDValue Op,
  2420. SDValue &Src, int &DstLSB,
  2421. int &Width) {
  2422. // Caller should have verified that N is a left shift with constant shift
  2423. // amount; asserts that.
  2424. assert(Op.getOpcode() == ISD::SHL &&
  2425. "Op.getNode() should be a SHL node to call this function");
  2426. assert(isIntImmediateEq(Op.getOperand(1), ShlImm) &&
  2427. "Op.getNode() should shift ShlImm to call this function");
  2428. uint64_t AndImm = 0;
  2429. SDValue Op0 = Op.getOperand(0);
  2430. if (!isOpcWithIntImmediate(Op0.getNode(), ISD::AND, AndImm))
  2431. return false;
  2432. const uint64_t ShiftedAndImm = ((AndImm << ShlImm) >> ShlImm);
  2433. if (isMask_64(ShiftedAndImm)) {
  2434. // AndImm is a superset of (AllOnes >> ShlImm); in other words, AndImm
  2435. // should end with Mask, and could be prefixed with random bits if those
  2436. // bits are shifted out.
  2437. //
  2438. // For example, xyz11111 (with {x,y,z} being 0 or 1) is fine if ShlImm >= 3;
  2439. // the AND result corresponding to those bits are shifted out, so it's fine
  2440. // to not extract them.
  2441. Width = countTrailingOnes(ShiftedAndImm);
  2442. DstLSB = ShlImm;
  2443. Src = Op0.getOperand(0);
  2444. return true;
  2445. }
  2446. return false;
  2447. }
  2448. static bool isBitfieldPositioningOpFromShl(SelectionDAG *CurDAG, SDValue Op,
  2449. bool BiggerPattern,
  2450. const uint64_t NonZeroBits,
  2451. SDValue &Src, int &DstLSB,
  2452. int &Width) {
  2453. assert(isShiftedMask_64(NonZeroBits) && "Caller guaranteed");
  2454. EVT VT = Op.getValueType();
  2455. assert((VT == MVT::i32 || VT == MVT::i64) &&
  2456. "Caller guarantees that type is i32 or i64");
  2457. (void)VT;
  2458. uint64_t ShlImm;
  2459. if (!isOpcWithIntImmediate(Op.getNode(), ISD::SHL, ShlImm))
  2460. return false;
  2461. if (!BiggerPattern && !Op.hasOneUse())
  2462. return false;
  2463. if (isSeveralBitsPositioningOpFromShl(ShlImm, Op, Src, DstLSB, Width))
  2464. return true;
  2465. DstLSB = countTrailingZeros(NonZeroBits);
  2466. Width = countTrailingOnes(NonZeroBits >> DstLSB);
  2467. if (ShlImm != uint64_t(DstLSB) && !BiggerPattern)
  2468. return false;
  2469. Src = getLeftShift(CurDAG, Op.getOperand(0), ShlImm - DstLSB);
  2470. return true;
  2471. }
  2472. static bool isShiftedMask(uint64_t Mask, EVT VT) {
  2473. assert(VT == MVT::i32 || VT == MVT::i64);
  2474. if (VT == MVT::i32)
  2475. return isShiftedMask_32(Mask);
  2476. return isShiftedMask_64(Mask);
  2477. }
  2478. // Generate a BFI/BFXIL from 'or (and X, MaskImm), OrImm' iff the value being
  2479. // inserted only sets known zero bits.
  2480. static bool tryBitfieldInsertOpFromOrAndImm(SDNode *N, SelectionDAG *CurDAG) {
  2481. assert(N->getOpcode() == ISD::OR && "Expect a OR operation");
  2482. EVT VT = N->getValueType(0);
  2483. if (VT != MVT::i32 && VT != MVT::i64)
  2484. return false;
  2485. unsigned BitWidth = VT.getSizeInBits();
  2486. uint64_t OrImm;
  2487. if (!isOpcWithIntImmediate(N, ISD::OR, OrImm))
  2488. return false;
  2489. // Skip this transformation if the ORR immediate can be encoded in the ORR.
  2490. // Otherwise, we'll trade an AND+ORR for ORR+BFI/BFXIL, which is most likely
  2491. // performance neutral.
  2492. if (AArch64_AM::isLogicalImmediate(OrImm, BitWidth))
  2493. return false;
  2494. uint64_t MaskImm;
  2495. SDValue And = N->getOperand(0);
  2496. // Must be a single use AND with an immediate operand.
  2497. if (!And.hasOneUse() ||
  2498. !isOpcWithIntImmediate(And.getNode(), ISD::AND, MaskImm))
  2499. return false;
  2500. // Compute the Known Zero for the AND as this allows us to catch more general
  2501. // cases than just looking for AND with imm.
  2502. KnownBits Known = CurDAG->computeKnownBits(And);
  2503. // Non-zero in the sense that they're not provably zero, which is the key
  2504. // point if we want to use this value.
  2505. uint64_t NotKnownZero = (~Known.Zero).getZExtValue();
  2506. // The KnownZero mask must be a shifted mask (e.g., 1110..011, 11100..00).
  2507. if (!isShiftedMask(Known.Zero.getZExtValue(), VT))
  2508. return false;
  2509. // The bits being inserted must only set those bits that are known to be zero.
  2510. if ((OrImm & NotKnownZero) != 0) {
  2511. // FIXME: It's okay if the OrImm sets NotKnownZero bits to 1, but we don't
  2512. // currently handle this case.
  2513. return false;
  2514. }
  2515. // BFI/BFXIL dst, src, #lsb, #width.
  2516. int LSB = countTrailingOnes(NotKnownZero);
  2517. int Width = BitWidth - APInt(BitWidth, NotKnownZero).countPopulation();
  2518. // BFI/BFXIL is an alias of BFM, so translate to BFM operands.
  2519. unsigned ImmR = (BitWidth - LSB) % BitWidth;
  2520. unsigned ImmS = Width - 1;
  2521. // If we're creating a BFI instruction avoid cases where we need more
  2522. // instructions to materialize the BFI constant as compared to the original
  2523. // ORR. A BFXIL will use the same constant as the original ORR, so the code
  2524. // should be no worse in this case.
  2525. bool IsBFI = LSB != 0;
  2526. uint64_t BFIImm = OrImm >> LSB;
  2527. if (IsBFI && !AArch64_AM::isLogicalImmediate(BFIImm, BitWidth)) {
  2528. // We have a BFI instruction and we know the constant can't be materialized
  2529. // with a ORR-immediate with the zero register.
  2530. unsigned OrChunks = 0, BFIChunks = 0;
  2531. for (unsigned Shift = 0; Shift < BitWidth; Shift += 16) {
  2532. if (((OrImm >> Shift) & 0xFFFF) != 0)
  2533. ++OrChunks;
  2534. if (((BFIImm >> Shift) & 0xFFFF) != 0)
  2535. ++BFIChunks;
  2536. }
  2537. if (BFIChunks > OrChunks)
  2538. return false;
  2539. }
  2540. // Materialize the constant to be inserted.
  2541. SDLoc DL(N);
  2542. unsigned MOVIOpc = VT == MVT::i32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
  2543. SDNode *MOVI = CurDAG->getMachineNode(
  2544. MOVIOpc, DL, VT, CurDAG->getTargetConstant(BFIImm, DL, VT));
  2545. // Create the BFI/BFXIL instruction.
  2546. SDValue Ops[] = {And.getOperand(0), SDValue(MOVI, 0),
  2547. CurDAG->getTargetConstant(ImmR, DL, VT),
  2548. CurDAG->getTargetConstant(ImmS, DL, VT)};
  2549. unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
  2550. CurDAG->SelectNodeTo(N, Opc, VT, Ops);
  2551. return true;
  2552. }
  2553. static bool isWorthFoldingIntoOrrWithShift(SDValue Dst, SelectionDAG *CurDAG,
  2554. SDValue &ShiftedOperand,
  2555. uint64_t &EncodedShiftImm) {
  2556. // Avoid folding Dst into ORR-with-shift if Dst has other uses than ORR.
  2557. if (!Dst.hasOneUse())
  2558. return false;
  2559. EVT VT = Dst.getValueType();
  2560. assert((VT == MVT::i32 || VT == MVT::i64) &&
  2561. "Caller should guarantee that VT is one of i32 or i64");
  2562. const unsigned SizeInBits = VT.getSizeInBits();
  2563. SDLoc DL(Dst.getNode());
  2564. uint64_t AndImm, ShlImm;
  2565. if (isOpcWithIntImmediate(Dst.getNode(), ISD::AND, AndImm) &&
  2566. isShiftedMask_64(AndImm)) {
  2567. // Avoid transforming 'DstOp0' if it has other uses than the AND node.
  2568. SDValue DstOp0 = Dst.getOperand(0);
  2569. if (!DstOp0.hasOneUse())
  2570. return false;
  2571. // An example to illustrate the transformation
  2572. // From:
  2573. // lsr x8, x1, #1
  2574. // and x8, x8, #0x3f80
  2575. // bfxil x8, x1, #0, #7
  2576. // To:
  2577. // and x8, x23, #0x7f
  2578. // ubfx x9, x23, #8, #7
  2579. // orr x23, x8, x9, lsl #7
  2580. //
  2581. // The number of instructions remains the same, but ORR is faster than BFXIL
  2582. // on many AArch64 processors (or as good as BFXIL if not faster). Besides,
  2583. // the dependency chain is improved after the transformation.
  2584. uint64_t SrlImm;
  2585. if (isOpcWithIntImmediate(DstOp0.getNode(), ISD::SRL, SrlImm)) {
  2586. uint64_t NumTrailingZeroInShiftedMask = countTrailingZeros(AndImm);
  2587. if ((SrlImm + NumTrailingZeroInShiftedMask) < SizeInBits) {
  2588. unsigned MaskWidth =
  2589. countTrailingOnes(AndImm >> NumTrailingZeroInShiftedMask);
  2590. unsigned UBFMOpc =
  2591. (VT == MVT::i32) ? AArch64::UBFMWri : AArch64::UBFMXri;
  2592. SDNode *UBFMNode = CurDAG->getMachineNode(
  2593. UBFMOpc, DL, VT, DstOp0.getOperand(0),
  2594. CurDAG->getTargetConstant(SrlImm + NumTrailingZeroInShiftedMask, DL,
  2595. VT),
  2596. CurDAG->getTargetConstant(
  2597. SrlImm + NumTrailingZeroInShiftedMask + MaskWidth - 1, DL, VT));
  2598. ShiftedOperand = SDValue(UBFMNode, 0);
  2599. EncodedShiftImm = AArch64_AM::getShifterImm(
  2600. AArch64_AM::LSL, NumTrailingZeroInShiftedMask);
  2601. return true;
  2602. }
  2603. }
  2604. return false;
  2605. }
  2606. if (isOpcWithIntImmediate(Dst.getNode(), ISD::SHL, ShlImm)) {
  2607. ShiftedOperand = Dst.getOperand(0);
  2608. EncodedShiftImm = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShlImm);
  2609. return true;
  2610. }
  2611. uint64_t SrlImm;
  2612. if (isOpcWithIntImmediate(Dst.getNode(), ISD::SRL, SrlImm)) {
  2613. ShiftedOperand = Dst.getOperand(0);
  2614. EncodedShiftImm = AArch64_AM::getShifterImm(AArch64_AM::LSR, SrlImm);
  2615. return true;
  2616. }
  2617. return false;
  2618. }
  2619. // Given an 'ISD::OR' node that is going to be selected as BFM, analyze
  2620. // the operands and select it to AArch64::ORR with shifted registers if
  2621. // that's more efficient. Returns true iff selection to AArch64::ORR happens.
  2622. static bool tryOrrWithShift(SDNode *N, SDValue OrOpd0, SDValue OrOpd1,
  2623. SDValue Src, SDValue Dst, SelectionDAG *CurDAG,
  2624. const bool BiggerPattern) {
  2625. EVT VT = N->getValueType(0);
  2626. assert(N->getOpcode() == ISD::OR && "Expect N to be an OR node");
  2627. assert(((N->getOperand(0) == OrOpd0 && N->getOperand(1) == OrOpd1) ||
  2628. (N->getOperand(1) == OrOpd0 && N->getOperand(0) == OrOpd1)) &&
  2629. "Expect OrOpd0 and OrOpd1 to be operands of ISD::OR");
  2630. assert((VT == MVT::i32 || VT == MVT::i64) &&
  2631. "Expect result type to be i32 or i64 since N is combinable to BFM");
  2632. SDLoc DL(N);
  2633. // Bail out if BFM simplifies away one node in BFM Dst.
  2634. if (OrOpd1 != Dst)
  2635. return false;
  2636. const unsigned OrrOpc = (VT == MVT::i32) ? AArch64::ORRWrs : AArch64::ORRXrs;
  2637. // For "BFM Rd, Rn, #immr, #imms", it's known that BFM simplifies away fewer
  2638. // nodes from Rn (or inserts additional shift node) if BiggerPattern is true.
  2639. if (BiggerPattern) {
  2640. uint64_t SrcAndImm;
  2641. if (isOpcWithIntImmediate(OrOpd0.getNode(), ISD::AND, SrcAndImm) &&
  2642. isMask_64(SrcAndImm) && OrOpd0.getOperand(0) == Src) {
  2643. // OrOpd0 = AND Src, #Mask
  2644. // So BFM simplifies away one AND node from Src and doesn't simplify away
  2645. // nodes from Dst. If ORR with left-shifted operand also simplifies away
  2646. // one node (from Rd), ORR is better since it has higher throughput and
  2647. // smaller latency than BFM on many AArch64 processors (and for the rest
  2648. // ORR is at least as good as BFM).
  2649. SDValue ShiftedOperand;
  2650. uint64_t EncodedShiftImm;
  2651. if (isWorthFoldingIntoOrrWithShift(Dst, CurDAG, ShiftedOperand,
  2652. EncodedShiftImm)) {
  2653. SDValue Ops[] = {OrOpd0, ShiftedOperand,
  2654. CurDAG->getTargetConstant(EncodedShiftImm, DL, VT)};
  2655. CurDAG->SelectNodeTo(N, OrrOpc, VT, Ops);
  2656. return true;
  2657. }
  2658. }
  2659. return false;
  2660. }
  2661. assert((!BiggerPattern) && "BiggerPattern should be handled above");
  2662. uint64_t ShlImm;
  2663. if (isOpcWithIntImmediate(OrOpd0.getNode(), ISD::SHL, ShlImm)) {
  2664. if (OrOpd0.getOperand(0) == Src && OrOpd0.hasOneUse()) {
  2665. SDValue Ops[] = {
  2666. Dst, Src,
  2667. CurDAG->getTargetConstant(
  2668. AArch64_AM::getShifterImm(AArch64_AM::LSL, ShlImm), DL, VT)};
  2669. CurDAG->SelectNodeTo(N, OrrOpc, VT, Ops);
  2670. return true;
  2671. }
  2672. // Select the following pattern to left-shifted operand rather than BFI.
  2673. // %val1 = op ..
  2674. // %val2 = shl %val1, #imm
  2675. // %res = or %val1, %val2
  2676. //
  2677. // If N is selected to be BFI, we know that
  2678. // 1) OrOpd0 would be the operand from which extract bits (i.e., folded into
  2679. // BFI) 2) OrOpd1 would be the destination operand (i.e., preserved)
  2680. //
  2681. // Instead of selecting N to BFI, fold OrOpd0 as a left shift directly.
  2682. if (OrOpd0.getOperand(0) == OrOpd1) {
  2683. SDValue Ops[] = {
  2684. OrOpd1, OrOpd1,
  2685. CurDAG->getTargetConstant(
  2686. AArch64_AM::getShifterImm(AArch64_AM::LSL, ShlImm), DL, VT)};
  2687. CurDAG->SelectNodeTo(N, OrrOpc, VT, Ops);
  2688. return true;
  2689. }
  2690. }
  2691. uint64_t SrlImm;
  2692. if (isOpcWithIntImmediate(OrOpd0.getNode(), ISD::SRL, SrlImm)) {
  2693. // Select the following pattern to right-shifted operand rather than BFXIL.
  2694. // %val1 = op ..
  2695. // %val2 = lshr %val1, #imm
  2696. // %res = or %val1, %val2
  2697. //
  2698. // If N is selected to be BFXIL, we know that
  2699. // 1) OrOpd0 would be the operand from which extract bits (i.e., folded into
  2700. // BFXIL) 2) OrOpd1 would be the destination operand (i.e., preserved)
  2701. //
  2702. // Instead of selecting N to BFXIL, fold OrOpd0 as a right shift directly.
  2703. if (OrOpd0.getOperand(0) == OrOpd1) {
  2704. SDValue Ops[] = {
  2705. OrOpd1, OrOpd1,
  2706. CurDAG->getTargetConstant(
  2707. AArch64_AM::getShifterImm(AArch64_AM::LSR, SrlImm), DL, VT)};
  2708. CurDAG->SelectNodeTo(N, OrrOpc, VT, Ops);
  2709. return true;
  2710. }
  2711. }
  2712. return false;
  2713. }
  2714. static bool tryBitfieldInsertOpFromOr(SDNode *N, const APInt &UsefulBits,
  2715. SelectionDAG *CurDAG) {
  2716. assert(N->getOpcode() == ISD::OR && "Expect a OR operation");
  2717. EVT VT = N->getValueType(0);
  2718. if (VT != MVT::i32 && VT != MVT::i64)
  2719. return false;
  2720. unsigned BitWidth = VT.getSizeInBits();
  2721. // Because of simplify-demanded-bits in DAGCombine, involved masks may not
  2722. // have the expected shape. Try to undo that.
  2723. unsigned NumberOfIgnoredLowBits = UsefulBits.countTrailingZeros();
  2724. unsigned NumberOfIgnoredHighBits = UsefulBits.countLeadingZeros();
  2725. // Given a OR operation, check if we have the following pattern
  2726. // ubfm c, b, imm, imm2 (or something that does the same jobs, see
  2727. // isBitfieldExtractOp)
  2728. // d = e & mask2 ; where mask is a binary sequence of 1..10..0 and
  2729. // countTrailingZeros(mask2) == imm2 - imm + 1
  2730. // f = d | c
  2731. // if yes, replace the OR instruction with:
  2732. // f = BFM Opd0, Opd1, LSB, MSB ; where LSB = imm, and MSB = imm2
  2733. // OR is commutative, check all combinations of operand order and values of
  2734. // BiggerPattern, i.e.
  2735. // Opd0, Opd1, BiggerPattern=false
  2736. // Opd1, Opd0, BiggerPattern=false
  2737. // Opd0, Opd1, BiggerPattern=true
  2738. // Opd1, Opd0, BiggerPattern=true
  2739. // Several of these combinations may match, so check with BiggerPattern=false
  2740. // first since that will produce better results by matching more instructions
  2741. // and/or inserting fewer extra instructions.
  2742. for (int I = 0; I < 4; ++I) {
  2743. SDValue Dst, Src;
  2744. unsigned ImmR, ImmS;
  2745. bool BiggerPattern = I / 2;
  2746. SDValue OrOpd0Val = N->getOperand(I % 2);
  2747. SDNode *OrOpd0 = OrOpd0Val.getNode();
  2748. SDValue OrOpd1Val = N->getOperand((I + 1) % 2);
  2749. SDNode *OrOpd1 = OrOpd1Val.getNode();
  2750. unsigned BFXOpc;
  2751. int DstLSB, Width;
  2752. if (isBitfieldExtractOp(CurDAG, OrOpd0, BFXOpc, Src, ImmR, ImmS,
  2753. NumberOfIgnoredLowBits, BiggerPattern)) {
  2754. // Check that the returned opcode is compatible with the pattern,
  2755. // i.e., same type and zero extended (U and not S)
  2756. if ((BFXOpc != AArch64::UBFMXri && VT == MVT::i64) ||
  2757. (BFXOpc != AArch64::UBFMWri && VT == MVT::i32))
  2758. continue;
  2759. // Compute the width of the bitfield insertion
  2760. DstLSB = 0;
  2761. Width = ImmS - ImmR + 1;
  2762. // FIXME: This constraint is to catch bitfield insertion we may
  2763. // want to widen the pattern if we want to grab general bitfied
  2764. // move case
  2765. if (Width <= 0)
  2766. continue;
  2767. // If the mask on the insertee is correct, we have a BFXIL operation. We
  2768. // can share the ImmR and ImmS values from the already-computed UBFM.
  2769. } else if (isBitfieldPositioningOp(CurDAG, OrOpd0Val,
  2770. BiggerPattern,
  2771. Src, DstLSB, Width)) {
  2772. ImmR = (BitWidth - DstLSB) % BitWidth;
  2773. ImmS = Width - 1;
  2774. } else
  2775. continue;
  2776. // Check the second part of the pattern
  2777. EVT VT = OrOpd1Val.getValueType();
  2778. assert((VT == MVT::i32 || VT == MVT::i64) && "unexpected OR operand");
  2779. // Compute the Known Zero for the candidate of the first operand.
  2780. // This allows to catch more general case than just looking for
  2781. // AND with imm. Indeed, simplify-demanded-bits may have removed
  2782. // the AND instruction because it proves it was useless.
  2783. KnownBits Known = CurDAG->computeKnownBits(OrOpd1Val);
  2784. // Check if there is enough room for the second operand to appear
  2785. // in the first one
  2786. APInt BitsToBeInserted =
  2787. APInt::getBitsSet(Known.getBitWidth(), DstLSB, DstLSB + Width);
  2788. if ((BitsToBeInserted & ~Known.Zero) != 0)
  2789. continue;
  2790. // Set the first operand
  2791. uint64_t Imm;
  2792. if (isOpcWithIntImmediate(OrOpd1, ISD::AND, Imm) &&
  2793. isBitfieldDstMask(Imm, BitsToBeInserted, NumberOfIgnoredHighBits, VT))
  2794. // In that case, we can eliminate the AND
  2795. Dst = OrOpd1->getOperand(0);
  2796. else
  2797. // Maybe the AND has been removed by simplify-demanded-bits
  2798. // or is useful because it discards more bits
  2799. Dst = OrOpd1Val;
  2800. // Before selecting ISD::OR node to AArch64::BFM, see if an AArch64::ORR
  2801. // with shifted operand is more efficient.
  2802. if (tryOrrWithShift(N, OrOpd0Val, OrOpd1Val, Src, Dst, CurDAG,
  2803. BiggerPattern))
  2804. return true;
  2805. // both parts match
  2806. SDLoc DL(N);
  2807. SDValue Ops[] = {Dst, Src, CurDAG->getTargetConstant(ImmR, DL, VT),
  2808. CurDAG->getTargetConstant(ImmS, DL, VT)};
  2809. unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
  2810. CurDAG->SelectNodeTo(N, Opc, VT, Ops);
  2811. return true;
  2812. }
  2813. // Generate a BFXIL from 'or (and X, Mask0Imm), (and Y, Mask1Imm)' iff
  2814. // Mask0Imm and ~Mask1Imm are equivalent and one of the MaskImms is a shifted
  2815. // mask (e.g., 0x000ffff0).
  2816. uint64_t Mask0Imm, Mask1Imm;
  2817. SDValue And0 = N->getOperand(0);
  2818. SDValue And1 = N->getOperand(1);
  2819. if (And0.hasOneUse() && And1.hasOneUse() &&
  2820. isOpcWithIntImmediate(And0.getNode(), ISD::AND, Mask0Imm) &&
  2821. isOpcWithIntImmediate(And1.getNode(), ISD::AND, Mask1Imm) &&
  2822. APInt(BitWidth, Mask0Imm) == ~APInt(BitWidth, Mask1Imm) &&
  2823. (isShiftedMask(Mask0Imm, VT) || isShiftedMask(Mask1Imm, VT))) {
  2824. // ORR is commutative, so canonicalize to the form 'or (and X, Mask0Imm),
  2825. // (and Y, Mask1Imm)' where Mask1Imm is the shifted mask masking off the
  2826. // bits to be inserted.
  2827. if (isShiftedMask(Mask0Imm, VT)) {
  2828. std::swap(And0, And1);
  2829. std::swap(Mask0Imm, Mask1Imm);
  2830. }
  2831. SDValue Src = And1->getOperand(0);
  2832. SDValue Dst = And0->getOperand(0);
  2833. unsigned LSB = countTrailingZeros(Mask1Imm);
  2834. int Width = BitWidth - APInt(BitWidth, Mask0Imm).countPopulation();
  2835. // The BFXIL inserts the low-order bits from a source register, so right
  2836. // shift the needed bits into place.
  2837. SDLoc DL(N);
  2838. unsigned ShiftOpc = (VT == MVT::i32) ? AArch64::UBFMWri : AArch64::UBFMXri;
  2839. uint64_t LsrImm = LSB;
  2840. if (Src->hasOneUse() &&
  2841. isOpcWithIntImmediate(Src.getNode(), ISD::SRL, LsrImm) &&
  2842. (LsrImm + LSB) < BitWidth) {
  2843. Src = Src->getOperand(0);
  2844. LsrImm += LSB;
  2845. }
  2846. SDNode *LSR = CurDAG->getMachineNode(
  2847. ShiftOpc, DL, VT, Src, CurDAG->getTargetConstant(LsrImm, DL, VT),
  2848. CurDAG->getTargetConstant(BitWidth - 1, DL, VT));
  2849. // BFXIL is an alias of BFM, so translate to BFM operands.
  2850. unsigned ImmR = (BitWidth - LSB) % BitWidth;
  2851. unsigned ImmS = Width - 1;
  2852. // Create the BFXIL instruction.
  2853. SDValue Ops[] = {Dst, SDValue(LSR, 0),
  2854. CurDAG->getTargetConstant(ImmR, DL, VT),
  2855. CurDAG->getTargetConstant(ImmS, DL, VT)};
  2856. unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
  2857. CurDAG->SelectNodeTo(N, Opc, VT, Ops);
  2858. return true;
  2859. }
  2860. return false;
  2861. }
  2862. bool AArch64DAGToDAGISel::tryBitfieldInsertOp(SDNode *N) {
  2863. if (N->getOpcode() != ISD::OR)
  2864. return false;
  2865. APInt NUsefulBits;
  2866. getUsefulBits(SDValue(N, 0), NUsefulBits);
  2867. // If all bits are not useful, just return UNDEF.
  2868. if (!NUsefulBits) {
  2869. CurDAG->SelectNodeTo(N, TargetOpcode::IMPLICIT_DEF, N->getValueType(0));
  2870. return true;
  2871. }
  2872. if (tryBitfieldInsertOpFromOr(N, NUsefulBits, CurDAG))
  2873. return true;
  2874. return tryBitfieldInsertOpFromOrAndImm(N, CurDAG);
  2875. }
  2876. /// SelectBitfieldInsertInZeroOp - Match a UBFIZ instruction that is the
  2877. /// equivalent of a left shift by a constant amount followed by an and masking
  2878. /// out a contiguous set of bits.
  2879. bool AArch64DAGToDAGISel::tryBitfieldInsertInZeroOp(SDNode *N) {
  2880. if (N->getOpcode() != ISD::AND)
  2881. return false;
  2882. EVT VT = N->getValueType(0);
  2883. if (VT != MVT::i32 && VT != MVT::i64)
  2884. return false;
  2885. SDValue Op0;
  2886. int DstLSB, Width;
  2887. if (!isBitfieldPositioningOp(CurDAG, SDValue(N, 0), /*BiggerPattern=*/false,
  2888. Op0, DstLSB, Width))
  2889. return false;
  2890. // ImmR is the rotate right amount.
  2891. unsigned ImmR = (VT.getSizeInBits() - DstLSB) % VT.getSizeInBits();
  2892. // ImmS is the most significant bit of the source to be moved.
  2893. unsigned ImmS = Width - 1;
  2894. SDLoc DL(N);
  2895. SDValue Ops[] = {Op0, CurDAG->getTargetConstant(ImmR, DL, VT),
  2896. CurDAG->getTargetConstant(ImmS, DL, VT)};
  2897. unsigned Opc = (VT == MVT::i32) ? AArch64::UBFMWri : AArch64::UBFMXri;
  2898. CurDAG->SelectNodeTo(N, Opc, VT, Ops);
  2899. return true;
  2900. }
  2901. /// tryShiftAmountMod - Take advantage of built-in mod of shift amount in
  2902. /// variable shift/rotate instructions.
  2903. bool AArch64DAGToDAGISel::tryShiftAmountMod(SDNode *N) {
  2904. EVT VT = N->getValueType(0);
  2905. unsigned Opc;
  2906. switch (N->getOpcode()) {
  2907. case ISD::ROTR:
  2908. Opc = (VT == MVT::i32) ? AArch64::RORVWr : AArch64::RORVXr;
  2909. break;
  2910. case ISD::SHL:
  2911. Opc = (VT == MVT::i32) ? AArch64::LSLVWr : AArch64::LSLVXr;
  2912. break;
  2913. case ISD::SRL:
  2914. Opc = (VT == MVT::i32) ? AArch64::LSRVWr : AArch64::LSRVXr;
  2915. break;
  2916. case ISD::SRA:
  2917. Opc = (VT == MVT::i32) ? AArch64::ASRVWr : AArch64::ASRVXr;
  2918. break;
  2919. default:
  2920. return false;
  2921. }
  2922. uint64_t Size;
  2923. uint64_t Bits;
  2924. if (VT == MVT::i32) {
  2925. Bits = 5;
  2926. Size = 32;
  2927. } else if (VT == MVT::i64) {
  2928. Bits = 6;
  2929. Size = 64;
  2930. } else
  2931. return false;
  2932. SDValue ShiftAmt = N->getOperand(1);
  2933. SDLoc DL(N);
  2934. SDValue NewShiftAmt;
  2935. // Skip over an extend of the shift amount.
  2936. if (ShiftAmt->getOpcode() == ISD::ZERO_EXTEND ||
  2937. ShiftAmt->getOpcode() == ISD::ANY_EXTEND)
  2938. ShiftAmt = ShiftAmt->getOperand(0);
  2939. if (ShiftAmt->getOpcode() == ISD::ADD || ShiftAmt->getOpcode() == ISD::SUB) {
  2940. SDValue Add0 = ShiftAmt->getOperand(0);
  2941. SDValue Add1 = ShiftAmt->getOperand(1);
  2942. uint64_t Add0Imm;
  2943. uint64_t Add1Imm;
  2944. if (isIntImmediate(Add1, Add1Imm) && (Add1Imm % Size == 0)) {
  2945. // If we are shifting by X+/-N where N == 0 mod Size, then just shift by X
  2946. // to avoid the ADD/SUB.
  2947. NewShiftAmt = Add0;
  2948. } else if (ShiftAmt->getOpcode() == ISD::SUB &&
  2949. isIntImmediate(Add0, Add0Imm) && Add0Imm != 0 &&
  2950. (Add0Imm % Size == 0)) {
  2951. // If we are shifting by N-X where N == 0 mod Size, then just shift by -X
  2952. // to generate a NEG instead of a SUB from a constant.
  2953. unsigned NegOpc;
  2954. unsigned ZeroReg;
  2955. EVT SubVT = ShiftAmt->getValueType(0);
  2956. if (SubVT == MVT::i32) {
  2957. NegOpc = AArch64::SUBWrr;
  2958. ZeroReg = AArch64::WZR;
  2959. } else {
  2960. assert(SubVT == MVT::i64);
  2961. NegOpc = AArch64::SUBXrr;
  2962. ZeroReg = AArch64::XZR;
  2963. }
  2964. SDValue Zero =
  2965. CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, ZeroReg, SubVT);
  2966. MachineSDNode *Neg =
  2967. CurDAG->getMachineNode(NegOpc, DL, SubVT, Zero, Add1);
  2968. NewShiftAmt = SDValue(Neg, 0);
  2969. } else if (ShiftAmt->getOpcode() == ISD::SUB &&
  2970. isIntImmediate(Add0, Add0Imm) && (Add0Imm % Size == Size - 1)) {
  2971. // If we are shifting by N-X where N == -1 mod Size, then just shift by ~X
  2972. // to generate a NOT instead of a SUB from a constant.
  2973. unsigned NotOpc;
  2974. unsigned ZeroReg;
  2975. EVT SubVT = ShiftAmt->getValueType(0);
  2976. if (SubVT == MVT::i32) {
  2977. NotOpc = AArch64::ORNWrr;
  2978. ZeroReg = AArch64::WZR;
  2979. } else {
  2980. assert(SubVT == MVT::i64);
  2981. NotOpc = AArch64::ORNXrr;
  2982. ZeroReg = AArch64::XZR;
  2983. }
  2984. SDValue Zero =
  2985. CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, ZeroReg, SubVT);
  2986. MachineSDNode *Not =
  2987. CurDAG->getMachineNode(NotOpc, DL, SubVT, Zero, Add1);
  2988. NewShiftAmt = SDValue(Not, 0);
  2989. } else
  2990. return false;
  2991. } else {
  2992. // If the shift amount is masked with an AND, check that the mask covers the
  2993. // bits that are implicitly ANDed off by the above opcodes and if so, skip
  2994. // the AND.
  2995. uint64_t MaskImm;
  2996. if (!isOpcWithIntImmediate(ShiftAmt.getNode(), ISD::AND, MaskImm) &&
  2997. !isOpcWithIntImmediate(ShiftAmt.getNode(), AArch64ISD::ANDS, MaskImm))
  2998. return false;
  2999. if (countTrailingOnes(MaskImm) < Bits)
  3000. return false;
  3001. NewShiftAmt = ShiftAmt->getOperand(0);
  3002. }
  3003. // Narrow/widen the shift amount to match the size of the shift operation.
  3004. if (VT == MVT::i32)
  3005. NewShiftAmt = narrowIfNeeded(CurDAG, NewShiftAmt);
  3006. else if (VT == MVT::i64 && NewShiftAmt->getValueType(0) == MVT::i32) {
  3007. SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, DL, MVT::i32);
  3008. MachineSDNode *Ext = CurDAG->getMachineNode(
  3009. AArch64::SUBREG_TO_REG, DL, VT,
  3010. CurDAG->getTargetConstant(0, DL, MVT::i64), NewShiftAmt, SubReg);
  3011. NewShiftAmt = SDValue(Ext, 0);
  3012. }
  3013. SDValue Ops[] = {N->getOperand(0), NewShiftAmt};
  3014. CurDAG->SelectNodeTo(N, Opc, VT, Ops);
  3015. return true;
  3016. }
  3017. bool
  3018. AArch64DAGToDAGISel::SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos,
  3019. unsigned RegWidth) {
  3020. APFloat FVal(0.0);
  3021. if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
  3022. FVal = CN->getValueAPF();
  3023. else if (LoadSDNode *LN = dyn_cast<LoadSDNode>(N)) {
  3024. // Some otherwise illegal constants are allowed in this case.
  3025. if (LN->getOperand(1).getOpcode() != AArch64ISD::ADDlow ||
  3026. !isa<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1)))
  3027. return false;
  3028. ConstantPoolSDNode *CN =
  3029. dyn_cast<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1));
  3030. FVal = cast<ConstantFP>(CN->getConstVal())->getValueAPF();
  3031. } else
  3032. return false;
  3033. // An FCVT[SU] instruction performs: convertToInt(Val * 2^fbits) where fbits
  3034. // is between 1 and 32 for a destination w-register, or 1 and 64 for an
  3035. // x-register.
  3036. //
  3037. // By this stage, we've detected (fp_to_[su]int (fmul Val, THIS_NODE)) so we
  3038. // want THIS_NODE to be 2^fbits. This is much easier to deal with using
  3039. // integers.
  3040. bool IsExact;
  3041. // fbits is between 1 and 64 in the worst-case, which means the fmul
  3042. // could have 2^64 as an actual operand. Need 65 bits of precision.
  3043. APSInt IntVal(65, true);
  3044. FVal.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact);
  3045. // N.b. isPowerOf2 also checks for > 0.
  3046. if (!IsExact || !IntVal.isPowerOf2()) return false;
  3047. unsigned FBits = IntVal.logBase2();
  3048. // Checks above should have guaranteed that we haven't lost information in
  3049. // finding FBits, but it must still be in range.
  3050. if (FBits == 0 || FBits > RegWidth) return false;
  3051. FixedPos = CurDAG->getTargetConstant(FBits, SDLoc(N), MVT::i32);
  3052. return true;
  3053. }
  3054. // Inspects a register string of the form o0:op1:CRn:CRm:op2 gets the fields
  3055. // of the string and obtains the integer values from them and combines these
  3056. // into a single value to be used in the MRS/MSR instruction.
  3057. static int getIntOperandFromRegisterString(StringRef RegString) {
  3058. SmallVector<StringRef, 5> Fields;
  3059. RegString.split(Fields, ':');
  3060. if (Fields.size() == 1)
  3061. return -1;
  3062. assert(Fields.size() == 5
  3063. && "Invalid number of fields in read register string");
  3064. SmallVector<int, 5> Ops;
  3065. bool AllIntFields = true;
  3066. for (StringRef Field : Fields) {
  3067. unsigned IntField;
  3068. AllIntFields &= !Field.getAsInteger(10, IntField);
  3069. Ops.push_back(IntField);
  3070. }
  3071. assert(AllIntFields &&
  3072. "Unexpected non-integer value in special register string.");
  3073. (void)AllIntFields;
  3074. // Need to combine the integer fields of the string into a single value
  3075. // based on the bit encoding of MRS/MSR instruction.
  3076. return (Ops[0] << 14) | (Ops[1] << 11) | (Ops[2] << 7) |
  3077. (Ops[3] << 3) | (Ops[4]);
  3078. }
  3079. // Lower the read_register intrinsic to an MRS instruction node if the special
  3080. // register string argument is either of the form detailed in the ALCE (the
  3081. // form described in getIntOperandsFromRegsterString) or is a named register
  3082. // known by the MRS SysReg mapper.
  3083. bool AArch64DAGToDAGISel::tryReadRegister(SDNode *N) {
  3084. const auto *MD = cast<MDNodeSDNode>(N->getOperand(1));
  3085. const auto *RegString = cast<MDString>(MD->getMD()->getOperand(0));
  3086. SDLoc DL(N);
  3087. bool ReadIs128Bit = N->getOpcode() == AArch64ISD::MRRS;
  3088. unsigned Opcode64Bit = AArch64::MRS;
  3089. int Imm = getIntOperandFromRegisterString(RegString->getString());
  3090. if (Imm == -1) {
  3091. // No match, Use the sysreg mapper to map the remaining possible strings to
  3092. // the value for the register to be used for the instruction operand.
  3093. const auto *TheReg =
  3094. AArch64SysReg::lookupSysRegByName(RegString->getString());
  3095. if (TheReg && TheReg->Readable &&
  3096. TheReg->haveFeatures(Subtarget->getFeatureBits()))
  3097. Imm = TheReg->Encoding;
  3098. else
  3099. Imm = AArch64SysReg::parseGenericRegister(RegString->getString());
  3100. if (Imm == -1) {
  3101. // Still no match, see if this is "pc" or give up.
  3102. if (!ReadIs128Bit && RegString->getString() == "pc") {
  3103. Opcode64Bit = AArch64::ADR;
  3104. Imm = 0;
  3105. } else {
  3106. return false;
  3107. }
  3108. }
  3109. }
  3110. SDValue InChain = N->getOperand(0);
  3111. SDValue SysRegImm = CurDAG->getTargetConstant(Imm, DL, MVT::i32);
  3112. if (!ReadIs128Bit) {
  3113. CurDAG->SelectNodeTo(N, Opcode64Bit, MVT::i64, MVT::Other /* Chain */,
  3114. {SysRegImm, InChain});
  3115. } else {
  3116. SDNode *MRRS = CurDAG->getMachineNode(
  3117. AArch64::MRRS, DL,
  3118. {MVT::Untyped /* XSeqPair */, MVT::Other /* Chain */},
  3119. {SysRegImm, InChain});
  3120. // Sysregs are not endian. The even register always contains the low half
  3121. // of the register.
  3122. SDValue Lo = CurDAG->getTargetExtractSubreg(AArch64::sube64, DL, MVT::i64,
  3123. SDValue(MRRS, 0));
  3124. SDValue Hi = CurDAG->getTargetExtractSubreg(AArch64::subo64, DL, MVT::i64,
  3125. SDValue(MRRS, 0));
  3126. SDValue OutChain = SDValue(MRRS, 1);
  3127. ReplaceUses(SDValue(N, 0), Lo);
  3128. ReplaceUses(SDValue(N, 1), Hi);
  3129. ReplaceUses(SDValue(N, 2), OutChain);
  3130. };
  3131. return true;
  3132. }
  3133. // Lower the write_register intrinsic to an MSR instruction node if the special
  3134. // register string argument is either of the form detailed in the ALCE (the
  3135. // form described in getIntOperandsFromRegsterString) or is a named register
  3136. // known by the MSR SysReg mapper.
  3137. bool AArch64DAGToDAGISel::tryWriteRegister(SDNode *N) {
  3138. const auto *MD = cast<MDNodeSDNode>(N->getOperand(1));
  3139. const auto *RegString = cast<MDString>(MD->getMD()->getOperand(0));
  3140. SDLoc DL(N);
  3141. bool WriteIs128Bit = N->getOpcode() == AArch64ISD::MSRR;
  3142. if (!WriteIs128Bit) {
  3143. // Check if the register was one of those allowed as the pstatefield value
  3144. // in the MSR (immediate) instruction. To accept the values allowed in the
  3145. // pstatefield for the MSR (immediate) instruction, we also require that an
  3146. // immediate value has been provided as an argument, we know that this is
  3147. // the case as it has been ensured by semantic checking.
  3148. auto trySelectPState = [&](auto PMapper, unsigned State) {
  3149. if (PMapper) {
  3150. assert(isa<ConstantSDNode>(N->getOperand(2)) &&
  3151. "Expected a constant integer expression.");
  3152. unsigned Reg = PMapper->Encoding;
  3153. uint64_t Immed = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
  3154. CurDAG->SelectNodeTo(
  3155. N, State, MVT::Other, CurDAG->getTargetConstant(Reg, DL, MVT::i32),
  3156. CurDAG->getTargetConstant(Immed, DL, MVT::i16), N->getOperand(0));
  3157. return true;
  3158. }
  3159. return false;
  3160. };
  3161. if (trySelectPState(
  3162. AArch64PState::lookupPStateImm0_15ByName(RegString->getString()),
  3163. AArch64::MSRpstateImm4))
  3164. return true;
  3165. if (trySelectPState(
  3166. AArch64PState::lookupPStateImm0_1ByName(RegString->getString()),
  3167. AArch64::MSRpstateImm1))
  3168. return true;
  3169. }
  3170. int Imm = getIntOperandFromRegisterString(RegString->getString());
  3171. if (Imm == -1) {
  3172. // Use the sysreg mapper to attempt to map the remaining possible strings
  3173. // to the value for the register to be used for the MSR (register)
  3174. // instruction operand.
  3175. auto TheReg = AArch64SysReg::lookupSysRegByName(RegString->getString());
  3176. if (TheReg && TheReg->Writeable &&
  3177. TheReg->haveFeatures(Subtarget->getFeatureBits()))
  3178. Imm = TheReg->Encoding;
  3179. else
  3180. Imm = AArch64SysReg::parseGenericRegister(RegString->getString());
  3181. if (Imm == -1)
  3182. return false;
  3183. }
  3184. SDValue InChain = N->getOperand(0);
  3185. if (!WriteIs128Bit) {
  3186. CurDAG->SelectNodeTo(N, AArch64::MSR, MVT::Other,
  3187. CurDAG->getTargetConstant(Imm, DL, MVT::i32),
  3188. N->getOperand(2), InChain);
  3189. } else {
  3190. // No endian swap. The lower half always goes into the even subreg, and the
  3191. // higher half always into the odd supreg.
  3192. SDNode *Pair = CurDAG->getMachineNode(
  3193. TargetOpcode::REG_SEQUENCE, DL, MVT::Untyped /* XSeqPair */,
  3194. {CurDAG->getTargetConstant(AArch64::XSeqPairsClassRegClass.getID(), DL,
  3195. MVT::i32),
  3196. N->getOperand(2),
  3197. CurDAG->getTargetConstant(AArch64::sube64, DL, MVT::i32),
  3198. N->getOperand(3),
  3199. CurDAG->getTargetConstant(AArch64::subo64, DL, MVT::i32)});
  3200. CurDAG->SelectNodeTo(N, AArch64::MSRR, MVT::Other,
  3201. CurDAG->getTargetConstant(Imm, DL, MVT::i32),
  3202. SDValue(Pair, 0), InChain);
  3203. }
  3204. return true;
  3205. }
  3206. /// We've got special pseudo-instructions for these
  3207. bool AArch64DAGToDAGISel::SelectCMP_SWAP(SDNode *N) {
  3208. unsigned Opcode;
  3209. EVT MemTy = cast<MemSDNode>(N)->getMemoryVT();
  3210. // Leave IR for LSE if subtarget supports it.
  3211. if (Subtarget->hasLSE()) return false;
  3212. if (MemTy == MVT::i8)
  3213. Opcode = AArch64::CMP_SWAP_8;
  3214. else if (MemTy == MVT::i16)
  3215. Opcode = AArch64::CMP_SWAP_16;
  3216. else if (MemTy == MVT::i32)
  3217. Opcode = AArch64::CMP_SWAP_32;
  3218. else if (MemTy == MVT::i64)
  3219. Opcode = AArch64::CMP_SWAP_64;
  3220. else
  3221. llvm_unreachable("Unknown AtomicCmpSwap type");
  3222. MVT RegTy = MemTy == MVT::i64 ? MVT::i64 : MVT::i32;
  3223. SDValue Ops[] = {N->getOperand(1), N->getOperand(2), N->getOperand(3),
  3224. N->getOperand(0)};
  3225. SDNode *CmpSwap = CurDAG->getMachineNode(
  3226. Opcode, SDLoc(N),
  3227. CurDAG->getVTList(RegTy, MVT::i32, MVT::Other), Ops);
  3228. MachineMemOperand *MemOp = cast<MemSDNode>(N)->getMemOperand();
  3229. CurDAG->setNodeMemRefs(cast<MachineSDNode>(CmpSwap), {MemOp});
  3230. ReplaceUses(SDValue(N, 0), SDValue(CmpSwap, 0));
  3231. ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 2));
  3232. CurDAG->RemoveDeadNode(N);
  3233. return true;
  3234. }
  3235. bool AArch64DAGToDAGISel::SelectSVEAddSubImm(SDValue N, MVT VT, SDValue &Imm,
  3236. SDValue &Shift) {
  3237. if (!isa<ConstantSDNode>(N))
  3238. return false;
  3239. SDLoc DL(N);
  3240. uint64_t Val = cast<ConstantSDNode>(N)
  3241. ->getAPIntValue()
  3242. .trunc(VT.getFixedSizeInBits())
  3243. .getZExtValue();
  3244. switch (VT.SimpleTy) {
  3245. case MVT::i8:
  3246. // All immediates are supported.
  3247. Shift = CurDAG->getTargetConstant(0, DL, MVT::i32);
  3248. Imm = CurDAG->getTargetConstant(Val, DL, MVT::i32);
  3249. return true;
  3250. case MVT::i16:
  3251. case MVT::i32:
  3252. case MVT::i64:
  3253. // Support 8bit unsigned immediates.
  3254. if (Val <= 255) {
  3255. Shift = CurDAG->getTargetConstant(0, DL, MVT::i32);
  3256. Imm = CurDAG->getTargetConstant(Val, DL, MVT::i32);
  3257. return true;
  3258. }
  3259. // Support 16bit unsigned immediates that are a multiple of 256.
  3260. if (Val <= 65280 && Val % 256 == 0) {
  3261. Shift = CurDAG->getTargetConstant(8, DL, MVT::i32);
  3262. Imm = CurDAG->getTargetConstant(Val >> 8, DL, MVT::i32);
  3263. return true;
  3264. }
  3265. break;
  3266. default:
  3267. break;
  3268. }
  3269. return false;
  3270. }
  3271. bool AArch64DAGToDAGISel::SelectSVECpyDupImm(SDValue N, MVT VT, SDValue &Imm,
  3272. SDValue &Shift) {
  3273. if (!isa<ConstantSDNode>(N))
  3274. return false;
  3275. SDLoc DL(N);
  3276. int64_t Val = cast<ConstantSDNode>(N)
  3277. ->getAPIntValue()
  3278. .trunc(VT.getFixedSizeInBits())
  3279. .getSExtValue();
  3280. switch (VT.SimpleTy) {
  3281. case MVT::i8:
  3282. // All immediates are supported.
  3283. Shift = CurDAG->getTargetConstant(0, DL, MVT::i32);
  3284. Imm = CurDAG->getTargetConstant(Val & 0xFF, DL, MVT::i32);
  3285. return true;
  3286. case MVT::i16:
  3287. case MVT::i32:
  3288. case MVT::i64:
  3289. // Support 8bit signed immediates.
  3290. if (Val >= -128 && Val <= 127) {
  3291. Shift = CurDAG->getTargetConstant(0, DL, MVT::i32);
  3292. Imm = CurDAG->getTargetConstant(Val & 0xFF, DL, MVT::i32);
  3293. return true;
  3294. }
  3295. // Support 16bit signed immediates that are a multiple of 256.
  3296. if (Val >= -32768 && Val <= 32512 && Val % 256 == 0) {
  3297. Shift = CurDAG->getTargetConstant(8, DL, MVT::i32);
  3298. Imm = CurDAG->getTargetConstant((Val >> 8) & 0xFF, DL, MVT::i32);
  3299. return true;
  3300. }
  3301. break;
  3302. default:
  3303. break;
  3304. }
  3305. return false;
  3306. }
  3307. bool AArch64DAGToDAGISel::SelectSVESignedArithImm(SDValue N, SDValue &Imm) {
  3308. if (auto CNode = dyn_cast<ConstantSDNode>(N)) {
  3309. int64_t ImmVal = CNode->getSExtValue();
  3310. SDLoc DL(N);
  3311. if (ImmVal >= -128 && ImmVal < 128) {
  3312. Imm = CurDAG->getTargetConstant(ImmVal, DL, MVT::i32);
  3313. return true;
  3314. }
  3315. }
  3316. return false;
  3317. }
  3318. bool AArch64DAGToDAGISel::SelectSVEArithImm(SDValue N, MVT VT, SDValue &Imm) {
  3319. if (auto CNode = dyn_cast<ConstantSDNode>(N)) {
  3320. uint64_t ImmVal = CNode->getZExtValue();
  3321. switch (VT.SimpleTy) {
  3322. case MVT::i8:
  3323. ImmVal &= 0xFF;
  3324. break;
  3325. case MVT::i16:
  3326. ImmVal &= 0xFFFF;
  3327. break;
  3328. case MVT::i32:
  3329. ImmVal &= 0xFFFFFFFF;
  3330. break;
  3331. case MVT::i64:
  3332. break;
  3333. default:
  3334. llvm_unreachable("Unexpected type");
  3335. }
  3336. if (ImmVal < 256) {
  3337. Imm = CurDAG->getTargetConstant(ImmVal, SDLoc(N), MVT::i32);
  3338. return true;
  3339. }
  3340. }
  3341. return false;
  3342. }
  3343. bool AArch64DAGToDAGISel::SelectSVELogicalImm(SDValue N, MVT VT, SDValue &Imm,
  3344. bool Invert) {
  3345. if (auto CNode = dyn_cast<ConstantSDNode>(N)) {
  3346. uint64_t ImmVal = CNode->getZExtValue();
  3347. SDLoc DL(N);
  3348. if (Invert)
  3349. ImmVal = ~ImmVal;
  3350. // Shift mask depending on type size.
  3351. switch (VT.SimpleTy) {
  3352. case MVT::i8:
  3353. ImmVal &= 0xFF;
  3354. ImmVal |= ImmVal << 8;
  3355. ImmVal |= ImmVal << 16;
  3356. ImmVal |= ImmVal << 32;
  3357. break;
  3358. case MVT::i16:
  3359. ImmVal &= 0xFFFF;
  3360. ImmVal |= ImmVal << 16;
  3361. ImmVal |= ImmVal << 32;
  3362. break;
  3363. case MVT::i32:
  3364. ImmVal &= 0xFFFFFFFF;
  3365. ImmVal |= ImmVal << 32;
  3366. break;
  3367. case MVT::i64:
  3368. break;
  3369. default:
  3370. llvm_unreachable("Unexpected type");
  3371. }
  3372. uint64_t encoding;
  3373. if (AArch64_AM::processLogicalImmediate(ImmVal, 64, encoding)) {
  3374. Imm = CurDAG->getTargetConstant(encoding, DL, MVT::i64);
  3375. return true;
  3376. }
  3377. }
  3378. return false;
  3379. }
  3380. // SVE shift intrinsics allow shift amounts larger than the element's bitwidth.
  3381. // Rather than attempt to normalise everything we can sometimes saturate the
  3382. // shift amount during selection. This function also allows for consistent
  3383. // isel patterns by ensuring the resulting "Imm" node is of the i32 type
  3384. // required by the instructions.
  3385. bool AArch64DAGToDAGISel::SelectSVEShiftImm(SDValue N, uint64_t Low,
  3386. uint64_t High, bool AllowSaturation,
  3387. SDValue &Imm) {
  3388. if (auto *CN = dyn_cast<ConstantSDNode>(N)) {
  3389. uint64_t ImmVal = CN->getZExtValue();
  3390. // Reject shift amounts that are too small.
  3391. if (ImmVal < Low)
  3392. return false;
  3393. // Reject or saturate shift amounts that are too big.
  3394. if (ImmVal > High) {
  3395. if (!AllowSaturation)
  3396. return false;
  3397. ImmVal = High;
  3398. }
  3399. Imm = CurDAG->getTargetConstant(ImmVal, SDLoc(N), MVT::i32);
  3400. return true;
  3401. }
  3402. return false;
  3403. }
  3404. bool AArch64DAGToDAGISel::trySelectStackSlotTagP(SDNode *N) {
  3405. // tagp(FrameIndex, IRGstack, tag_offset):
  3406. // since the offset between FrameIndex and IRGstack is a compile-time
  3407. // constant, this can be lowered to a single ADDG instruction.
  3408. if (!(isa<FrameIndexSDNode>(N->getOperand(1)))) {
  3409. return false;
  3410. }
  3411. SDValue IRG_SP = N->getOperand(2);
  3412. if (IRG_SP->getOpcode() != ISD::INTRINSIC_W_CHAIN ||
  3413. cast<ConstantSDNode>(IRG_SP->getOperand(1))->getZExtValue() !=
  3414. Intrinsic::aarch64_irg_sp) {
  3415. return false;
  3416. }
  3417. const TargetLowering *TLI = getTargetLowering();
  3418. SDLoc DL(N);
  3419. int FI = cast<FrameIndexSDNode>(N->getOperand(1))->getIndex();
  3420. SDValue FiOp = CurDAG->getTargetFrameIndex(
  3421. FI, TLI->getPointerTy(CurDAG->getDataLayout()));
  3422. int TagOffset = cast<ConstantSDNode>(N->getOperand(3))->getZExtValue();
  3423. SDNode *Out = CurDAG->getMachineNode(
  3424. AArch64::TAGPstack, DL, MVT::i64,
  3425. {FiOp, CurDAG->getTargetConstant(0, DL, MVT::i64), N->getOperand(2),
  3426. CurDAG->getTargetConstant(TagOffset, DL, MVT::i64)});
  3427. ReplaceNode(N, Out);
  3428. return true;
  3429. }
  3430. void AArch64DAGToDAGISel::SelectTagP(SDNode *N) {
  3431. assert(isa<ConstantSDNode>(N->getOperand(3)) &&
  3432. "llvm.aarch64.tagp third argument must be an immediate");
  3433. if (trySelectStackSlotTagP(N))
  3434. return;
  3435. // FIXME: above applies in any case when offset between Op1 and Op2 is a
  3436. // compile-time constant, not just for stack allocations.
  3437. // General case for unrelated pointers in Op1 and Op2.
  3438. SDLoc DL(N);
  3439. int TagOffset = cast<ConstantSDNode>(N->getOperand(3))->getZExtValue();
  3440. SDNode *N1 = CurDAG->getMachineNode(AArch64::SUBP, DL, MVT::i64,
  3441. {N->getOperand(1), N->getOperand(2)});
  3442. SDNode *N2 = CurDAG->getMachineNode(AArch64::ADDXrr, DL, MVT::i64,
  3443. {SDValue(N1, 0), N->getOperand(2)});
  3444. SDNode *N3 = CurDAG->getMachineNode(
  3445. AArch64::ADDG, DL, MVT::i64,
  3446. {SDValue(N2, 0), CurDAG->getTargetConstant(0, DL, MVT::i64),
  3447. CurDAG->getTargetConstant(TagOffset, DL, MVT::i64)});
  3448. ReplaceNode(N, N3);
  3449. }
  3450. // NOTE: We cannot use EXTRACT_SUBREG in all cases because the fixed length
  3451. // vector types larger than NEON don't have a matching SubRegIndex.
  3452. static SDNode *extractSubReg(SelectionDAG *DAG, EVT VT, SDValue V) {
  3453. assert(V.getValueType().isScalableVector() &&
  3454. V.getValueType().getSizeInBits().getKnownMinValue() ==
  3455. AArch64::SVEBitsPerBlock &&
  3456. "Expected to extract from a packed scalable vector!");
  3457. assert(VT.isFixedLengthVector() &&
  3458. "Expected to extract a fixed length vector!");
  3459. SDLoc DL(V);
  3460. switch (VT.getSizeInBits()) {
  3461. case 64: {
  3462. auto SubReg = DAG->getTargetConstant(AArch64::dsub, DL, MVT::i32);
  3463. return DAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL, VT, V, SubReg);
  3464. }
  3465. case 128: {
  3466. auto SubReg = DAG->getTargetConstant(AArch64::zsub, DL, MVT::i32);
  3467. return DAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL, VT, V, SubReg);
  3468. }
  3469. default: {
  3470. auto RC = DAG->getTargetConstant(AArch64::ZPRRegClassID, DL, MVT::i64);
  3471. return DAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, DL, VT, V, RC);
  3472. }
  3473. }
  3474. }
  3475. // NOTE: We cannot use INSERT_SUBREG in all cases because the fixed length
  3476. // vector types larger than NEON don't have a matching SubRegIndex.
  3477. static SDNode *insertSubReg(SelectionDAG *DAG, EVT VT, SDValue V) {
  3478. assert(VT.isScalableVector() &&
  3479. VT.getSizeInBits().getKnownMinValue() == AArch64::SVEBitsPerBlock &&
  3480. "Expected to insert into a packed scalable vector!");
  3481. assert(V.getValueType().isFixedLengthVector() &&
  3482. "Expected to insert a fixed length vector!");
  3483. SDLoc DL(V);
  3484. switch (V.getValueType().getSizeInBits()) {
  3485. case 64: {
  3486. auto SubReg = DAG->getTargetConstant(AArch64::dsub, DL, MVT::i32);
  3487. auto Container = DAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
  3488. return DAG->getMachineNode(TargetOpcode::INSERT_SUBREG, DL, VT,
  3489. SDValue(Container, 0), V, SubReg);
  3490. }
  3491. case 128: {
  3492. auto SubReg = DAG->getTargetConstant(AArch64::zsub, DL, MVT::i32);
  3493. auto Container = DAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
  3494. return DAG->getMachineNode(TargetOpcode::INSERT_SUBREG, DL, VT,
  3495. SDValue(Container, 0), V, SubReg);
  3496. }
  3497. default: {
  3498. auto RC = DAG->getTargetConstant(AArch64::ZPRRegClassID, DL, MVT::i64);
  3499. return DAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, DL, VT, V, RC);
  3500. }
  3501. }
  3502. }
  3503. void AArch64DAGToDAGISel::Select(SDNode *Node) {
  3504. // If we have a custom node, we already have selected!
  3505. if (Node->isMachineOpcode()) {
  3506. LLVM_DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
  3507. Node->setNodeId(-1);
  3508. return;
  3509. }
  3510. // Few custom selection stuff.
  3511. EVT VT = Node->getValueType(0);
  3512. switch (Node->getOpcode()) {
  3513. default:
  3514. break;
  3515. case ISD::ATOMIC_CMP_SWAP:
  3516. if (SelectCMP_SWAP(Node))
  3517. return;
  3518. break;
  3519. case ISD::READ_REGISTER:
  3520. case AArch64ISD::MRRS:
  3521. if (tryReadRegister(Node))
  3522. return;
  3523. break;
  3524. case ISD::WRITE_REGISTER:
  3525. case AArch64ISD::MSRR:
  3526. if (tryWriteRegister(Node))
  3527. return;
  3528. break;
  3529. case ISD::ADD:
  3530. if (tryMLAV64LaneV128(Node))
  3531. return;
  3532. break;
  3533. case ISD::LOAD: {
  3534. // Try to select as an indexed load. Fall through to normal processing
  3535. // if we can't.
  3536. if (tryIndexedLoad(Node))
  3537. return;
  3538. break;
  3539. }
  3540. case ISD::SRL:
  3541. case ISD::AND:
  3542. case ISD::SRA:
  3543. case ISD::SIGN_EXTEND_INREG:
  3544. if (tryBitfieldExtractOp(Node))
  3545. return;
  3546. if (tryBitfieldInsertInZeroOp(Node))
  3547. return;
  3548. [[fallthrough]];
  3549. case ISD::ROTR:
  3550. case ISD::SHL:
  3551. if (tryShiftAmountMod(Node))
  3552. return;
  3553. break;
  3554. case ISD::SIGN_EXTEND:
  3555. if (tryBitfieldExtractOpFromSExt(Node))
  3556. return;
  3557. break;
  3558. case ISD::FP_EXTEND:
  3559. if (tryHighFPExt(Node))
  3560. return;
  3561. break;
  3562. case ISD::OR:
  3563. if (tryBitfieldInsertOp(Node))
  3564. return;
  3565. break;
  3566. case ISD::EXTRACT_SUBVECTOR: {
  3567. // Bail when not a "cast" like extract_subvector.
  3568. if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue() != 0)
  3569. break;
  3570. // Bail when normal isel can do the job.
  3571. EVT InVT = Node->getOperand(0).getValueType();
  3572. if (VT.isScalableVector() || InVT.isFixedLengthVector())
  3573. break;
  3574. // NOTE: We can only get here when doing fixed length SVE code generation.
  3575. // We do manual selection because the types involved are not linked to real
  3576. // registers (despite being legal) and must be coerced into SVE registers.
  3577. //
  3578. // NOTE: If the above changes, be aware that selection will still not work
  3579. // because the td definition of extract_vector does not support extracting
  3580. // a fixed length vector from a scalable vector.
  3581. ReplaceNode(Node, extractSubReg(CurDAG, VT, Node->getOperand(0)));
  3582. return;
  3583. }
  3584. case ISD::INSERT_SUBVECTOR: {
  3585. // Bail when not a "cast" like insert_subvector.
  3586. if (cast<ConstantSDNode>(Node->getOperand(2))->getZExtValue() != 0)
  3587. break;
  3588. if (!Node->getOperand(0).isUndef())
  3589. break;
  3590. // Bail when normal isel should do the job.
  3591. EVT InVT = Node->getOperand(1).getValueType();
  3592. if (VT.isFixedLengthVector() || InVT.isScalableVector())
  3593. break;
  3594. // NOTE: We can only get here when doing fixed length SVE code generation.
  3595. // We do manual selection because the types involved are not linked to real
  3596. // registers (despite being legal) and must be coerced into SVE registers.
  3597. //
  3598. // NOTE: If the above changes, be aware that selection will still not work
  3599. // because the td definition of insert_vector does not support inserting a
  3600. // fixed length vector into a scalable vector.
  3601. ReplaceNode(Node, insertSubReg(CurDAG, VT, Node->getOperand(1)));
  3602. return;
  3603. }
  3604. case ISD::Constant: {
  3605. // Materialize zero constants as copies from WZR/XZR. This allows
  3606. // the coalescer to propagate these into other instructions.
  3607. ConstantSDNode *ConstNode = cast<ConstantSDNode>(Node);
  3608. if (ConstNode->isZero()) {
  3609. if (VT == MVT::i32) {
  3610. SDValue New = CurDAG->getCopyFromReg(
  3611. CurDAG->getEntryNode(), SDLoc(Node), AArch64::WZR, MVT::i32);
  3612. ReplaceNode(Node, New.getNode());
  3613. return;
  3614. } else if (VT == MVT::i64) {
  3615. SDValue New = CurDAG->getCopyFromReg(
  3616. CurDAG->getEntryNode(), SDLoc(Node), AArch64::XZR, MVT::i64);
  3617. ReplaceNode(Node, New.getNode());
  3618. return;
  3619. }
  3620. }
  3621. break;
  3622. }
  3623. case ISD::FrameIndex: {
  3624. // Selects to ADDXri FI, 0 which in turn will become ADDXri SP, imm.
  3625. int FI = cast<FrameIndexSDNode>(Node)->getIndex();
  3626. unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
  3627. const TargetLowering *TLI = getTargetLowering();
  3628. SDValue TFI = CurDAG->getTargetFrameIndex(
  3629. FI, TLI->getPointerTy(CurDAG->getDataLayout()));
  3630. SDLoc DL(Node);
  3631. SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, DL, MVT::i32),
  3632. CurDAG->getTargetConstant(Shifter, DL, MVT::i32) };
  3633. CurDAG->SelectNodeTo(Node, AArch64::ADDXri, MVT::i64, Ops);
  3634. return;
  3635. }
  3636. case ISD::INTRINSIC_W_CHAIN: {
  3637. unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
  3638. switch (IntNo) {
  3639. default:
  3640. break;
  3641. case Intrinsic::aarch64_ldaxp:
  3642. case Intrinsic::aarch64_ldxp: {
  3643. unsigned Op =
  3644. IntNo == Intrinsic::aarch64_ldaxp ? AArch64::LDAXPX : AArch64::LDXPX;
  3645. SDValue MemAddr = Node->getOperand(2);
  3646. SDLoc DL(Node);
  3647. SDValue Chain = Node->getOperand(0);
  3648. SDNode *Ld = CurDAG->getMachineNode(Op, DL, MVT::i64, MVT::i64,
  3649. MVT::Other, MemAddr, Chain);
  3650. // Transfer memoperands.
  3651. MachineMemOperand *MemOp =
  3652. cast<MemIntrinsicSDNode>(Node)->getMemOperand();
  3653. CurDAG->setNodeMemRefs(cast<MachineSDNode>(Ld), {MemOp});
  3654. ReplaceNode(Node, Ld);
  3655. return;
  3656. }
  3657. case Intrinsic::aarch64_stlxp:
  3658. case Intrinsic::aarch64_stxp: {
  3659. unsigned Op =
  3660. IntNo == Intrinsic::aarch64_stlxp ? AArch64::STLXPX : AArch64::STXPX;
  3661. SDLoc DL(Node);
  3662. SDValue Chain = Node->getOperand(0);
  3663. SDValue ValLo = Node->getOperand(2);
  3664. SDValue ValHi = Node->getOperand(3);
  3665. SDValue MemAddr = Node->getOperand(4);
  3666. // Place arguments in the right order.
  3667. SDValue Ops[] = {ValLo, ValHi, MemAddr, Chain};
  3668. SDNode *St = CurDAG->getMachineNode(Op, DL, MVT::i32, MVT::Other, Ops);
  3669. // Transfer memoperands.
  3670. MachineMemOperand *MemOp =
  3671. cast<MemIntrinsicSDNode>(Node)->getMemOperand();
  3672. CurDAG->setNodeMemRefs(cast<MachineSDNode>(St), {MemOp});
  3673. ReplaceNode(Node, St);
  3674. return;
  3675. }
  3676. case Intrinsic::aarch64_neon_ld1x2:
  3677. if (VT == MVT::v8i8) {
  3678. SelectLoad(Node, 2, AArch64::LD1Twov8b, AArch64::dsub0);
  3679. return;
  3680. } else if (VT == MVT::v16i8) {
  3681. SelectLoad(Node, 2, AArch64::LD1Twov16b, AArch64::qsub0);
  3682. return;
  3683. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  3684. SelectLoad(Node, 2, AArch64::LD1Twov4h, AArch64::dsub0);
  3685. return;
  3686. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  3687. SelectLoad(Node, 2, AArch64::LD1Twov8h, AArch64::qsub0);
  3688. return;
  3689. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  3690. SelectLoad(Node, 2, AArch64::LD1Twov2s, AArch64::dsub0);
  3691. return;
  3692. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  3693. SelectLoad(Node, 2, AArch64::LD1Twov4s, AArch64::qsub0);
  3694. return;
  3695. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  3696. SelectLoad(Node, 2, AArch64::LD1Twov1d, AArch64::dsub0);
  3697. return;
  3698. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  3699. SelectLoad(Node, 2, AArch64::LD1Twov2d, AArch64::qsub0);
  3700. return;
  3701. }
  3702. break;
  3703. case Intrinsic::aarch64_neon_ld1x3:
  3704. if (VT == MVT::v8i8) {
  3705. SelectLoad(Node, 3, AArch64::LD1Threev8b, AArch64::dsub0);
  3706. return;
  3707. } else if (VT == MVT::v16i8) {
  3708. SelectLoad(Node, 3, AArch64::LD1Threev16b, AArch64::qsub0);
  3709. return;
  3710. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  3711. SelectLoad(Node, 3, AArch64::LD1Threev4h, AArch64::dsub0);
  3712. return;
  3713. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  3714. SelectLoad(Node, 3, AArch64::LD1Threev8h, AArch64::qsub0);
  3715. return;
  3716. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  3717. SelectLoad(Node, 3, AArch64::LD1Threev2s, AArch64::dsub0);
  3718. return;
  3719. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  3720. SelectLoad(Node, 3, AArch64::LD1Threev4s, AArch64::qsub0);
  3721. return;
  3722. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  3723. SelectLoad(Node, 3, AArch64::LD1Threev1d, AArch64::dsub0);
  3724. return;
  3725. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  3726. SelectLoad(Node, 3, AArch64::LD1Threev2d, AArch64::qsub0);
  3727. return;
  3728. }
  3729. break;
  3730. case Intrinsic::aarch64_neon_ld1x4:
  3731. if (VT == MVT::v8i8) {
  3732. SelectLoad(Node, 4, AArch64::LD1Fourv8b, AArch64::dsub0);
  3733. return;
  3734. } else if (VT == MVT::v16i8) {
  3735. SelectLoad(Node, 4, AArch64::LD1Fourv16b, AArch64::qsub0);
  3736. return;
  3737. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  3738. SelectLoad(Node, 4, AArch64::LD1Fourv4h, AArch64::dsub0);
  3739. return;
  3740. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  3741. SelectLoad(Node, 4, AArch64::LD1Fourv8h, AArch64::qsub0);
  3742. return;
  3743. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  3744. SelectLoad(Node, 4, AArch64::LD1Fourv2s, AArch64::dsub0);
  3745. return;
  3746. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  3747. SelectLoad(Node, 4, AArch64::LD1Fourv4s, AArch64::qsub0);
  3748. return;
  3749. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  3750. SelectLoad(Node, 4, AArch64::LD1Fourv1d, AArch64::dsub0);
  3751. return;
  3752. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  3753. SelectLoad(Node, 4, AArch64::LD1Fourv2d, AArch64::qsub0);
  3754. return;
  3755. }
  3756. break;
  3757. case Intrinsic::aarch64_neon_ld2:
  3758. if (VT == MVT::v8i8) {
  3759. SelectLoad(Node, 2, AArch64::LD2Twov8b, AArch64::dsub0);
  3760. return;
  3761. } else if (VT == MVT::v16i8) {
  3762. SelectLoad(Node, 2, AArch64::LD2Twov16b, AArch64::qsub0);
  3763. return;
  3764. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  3765. SelectLoad(Node, 2, AArch64::LD2Twov4h, AArch64::dsub0);
  3766. return;
  3767. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  3768. SelectLoad(Node, 2, AArch64::LD2Twov8h, AArch64::qsub0);
  3769. return;
  3770. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  3771. SelectLoad(Node, 2, AArch64::LD2Twov2s, AArch64::dsub0);
  3772. return;
  3773. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  3774. SelectLoad(Node, 2, AArch64::LD2Twov4s, AArch64::qsub0);
  3775. return;
  3776. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  3777. SelectLoad(Node, 2, AArch64::LD1Twov1d, AArch64::dsub0);
  3778. return;
  3779. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  3780. SelectLoad(Node, 2, AArch64::LD2Twov2d, AArch64::qsub0);
  3781. return;
  3782. }
  3783. break;
  3784. case Intrinsic::aarch64_neon_ld3:
  3785. if (VT == MVT::v8i8) {
  3786. SelectLoad(Node, 3, AArch64::LD3Threev8b, AArch64::dsub0);
  3787. return;
  3788. } else if (VT == MVT::v16i8) {
  3789. SelectLoad(Node, 3, AArch64::LD3Threev16b, AArch64::qsub0);
  3790. return;
  3791. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  3792. SelectLoad(Node, 3, AArch64::LD3Threev4h, AArch64::dsub0);
  3793. return;
  3794. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  3795. SelectLoad(Node, 3, AArch64::LD3Threev8h, AArch64::qsub0);
  3796. return;
  3797. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  3798. SelectLoad(Node, 3, AArch64::LD3Threev2s, AArch64::dsub0);
  3799. return;
  3800. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  3801. SelectLoad(Node, 3, AArch64::LD3Threev4s, AArch64::qsub0);
  3802. return;
  3803. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  3804. SelectLoad(Node, 3, AArch64::LD1Threev1d, AArch64::dsub0);
  3805. return;
  3806. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  3807. SelectLoad(Node, 3, AArch64::LD3Threev2d, AArch64::qsub0);
  3808. return;
  3809. }
  3810. break;
  3811. case Intrinsic::aarch64_neon_ld4:
  3812. if (VT == MVT::v8i8) {
  3813. SelectLoad(Node, 4, AArch64::LD4Fourv8b, AArch64::dsub0);
  3814. return;
  3815. } else if (VT == MVT::v16i8) {
  3816. SelectLoad(Node, 4, AArch64::LD4Fourv16b, AArch64::qsub0);
  3817. return;
  3818. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  3819. SelectLoad(Node, 4, AArch64::LD4Fourv4h, AArch64::dsub0);
  3820. return;
  3821. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  3822. SelectLoad(Node, 4, AArch64::LD4Fourv8h, AArch64::qsub0);
  3823. return;
  3824. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  3825. SelectLoad(Node, 4, AArch64::LD4Fourv2s, AArch64::dsub0);
  3826. return;
  3827. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  3828. SelectLoad(Node, 4, AArch64::LD4Fourv4s, AArch64::qsub0);
  3829. return;
  3830. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  3831. SelectLoad(Node, 4, AArch64::LD1Fourv1d, AArch64::dsub0);
  3832. return;
  3833. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  3834. SelectLoad(Node, 4, AArch64::LD4Fourv2d, AArch64::qsub0);
  3835. return;
  3836. }
  3837. break;
  3838. case Intrinsic::aarch64_neon_ld2r:
  3839. if (VT == MVT::v8i8) {
  3840. SelectLoad(Node, 2, AArch64::LD2Rv8b, AArch64::dsub0);
  3841. return;
  3842. } else if (VT == MVT::v16i8) {
  3843. SelectLoad(Node, 2, AArch64::LD2Rv16b, AArch64::qsub0);
  3844. return;
  3845. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  3846. SelectLoad(Node, 2, AArch64::LD2Rv4h, AArch64::dsub0);
  3847. return;
  3848. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  3849. SelectLoad(Node, 2, AArch64::LD2Rv8h, AArch64::qsub0);
  3850. return;
  3851. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  3852. SelectLoad(Node, 2, AArch64::LD2Rv2s, AArch64::dsub0);
  3853. return;
  3854. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  3855. SelectLoad(Node, 2, AArch64::LD2Rv4s, AArch64::qsub0);
  3856. return;
  3857. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  3858. SelectLoad(Node, 2, AArch64::LD2Rv1d, AArch64::dsub0);
  3859. return;
  3860. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  3861. SelectLoad(Node, 2, AArch64::LD2Rv2d, AArch64::qsub0);
  3862. return;
  3863. }
  3864. break;
  3865. case Intrinsic::aarch64_neon_ld3r:
  3866. if (VT == MVT::v8i8) {
  3867. SelectLoad(Node, 3, AArch64::LD3Rv8b, AArch64::dsub0);
  3868. return;
  3869. } else if (VT == MVT::v16i8) {
  3870. SelectLoad(Node, 3, AArch64::LD3Rv16b, AArch64::qsub0);
  3871. return;
  3872. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  3873. SelectLoad(Node, 3, AArch64::LD3Rv4h, AArch64::dsub0);
  3874. return;
  3875. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  3876. SelectLoad(Node, 3, AArch64::LD3Rv8h, AArch64::qsub0);
  3877. return;
  3878. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  3879. SelectLoad(Node, 3, AArch64::LD3Rv2s, AArch64::dsub0);
  3880. return;
  3881. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  3882. SelectLoad(Node, 3, AArch64::LD3Rv4s, AArch64::qsub0);
  3883. return;
  3884. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  3885. SelectLoad(Node, 3, AArch64::LD3Rv1d, AArch64::dsub0);
  3886. return;
  3887. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  3888. SelectLoad(Node, 3, AArch64::LD3Rv2d, AArch64::qsub0);
  3889. return;
  3890. }
  3891. break;
  3892. case Intrinsic::aarch64_neon_ld4r:
  3893. if (VT == MVT::v8i8) {
  3894. SelectLoad(Node, 4, AArch64::LD4Rv8b, AArch64::dsub0);
  3895. return;
  3896. } else if (VT == MVT::v16i8) {
  3897. SelectLoad(Node, 4, AArch64::LD4Rv16b, AArch64::qsub0);
  3898. return;
  3899. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  3900. SelectLoad(Node, 4, AArch64::LD4Rv4h, AArch64::dsub0);
  3901. return;
  3902. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  3903. SelectLoad(Node, 4, AArch64::LD4Rv8h, AArch64::qsub0);
  3904. return;
  3905. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  3906. SelectLoad(Node, 4, AArch64::LD4Rv2s, AArch64::dsub0);
  3907. return;
  3908. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  3909. SelectLoad(Node, 4, AArch64::LD4Rv4s, AArch64::qsub0);
  3910. return;
  3911. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  3912. SelectLoad(Node, 4, AArch64::LD4Rv1d, AArch64::dsub0);
  3913. return;
  3914. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  3915. SelectLoad(Node, 4, AArch64::LD4Rv2d, AArch64::qsub0);
  3916. return;
  3917. }
  3918. break;
  3919. case Intrinsic::aarch64_neon_ld2lane:
  3920. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  3921. SelectLoadLane(Node, 2, AArch64::LD2i8);
  3922. return;
  3923. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  3924. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  3925. SelectLoadLane(Node, 2, AArch64::LD2i16);
  3926. return;
  3927. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  3928. VT == MVT::v2f32) {
  3929. SelectLoadLane(Node, 2, AArch64::LD2i32);
  3930. return;
  3931. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  3932. VT == MVT::v1f64) {
  3933. SelectLoadLane(Node, 2, AArch64::LD2i64);
  3934. return;
  3935. }
  3936. break;
  3937. case Intrinsic::aarch64_neon_ld3lane:
  3938. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  3939. SelectLoadLane(Node, 3, AArch64::LD3i8);
  3940. return;
  3941. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  3942. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  3943. SelectLoadLane(Node, 3, AArch64::LD3i16);
  3944. return;
  3945. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  3946. VT == MVT::v2f32) {
  3947. SelectLoadLane(Node, 3, AArch64::LD3i32);
  3948. return;
  3949. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  3950. VT == MVT::v1f64) {
  3951. SelectLoadLane(Node, 3, AArch64::LD3i64);
  3952. return;
  3953. }
  3954. break;
  3955. case Intrinsic::aarch64_neon_ld4lane:
  3956. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  3957. SelectLoadLane(Node, 4, AArch64::LD4i8);
  3958. return;
  3959. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  3960. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  3961. SelectLoadLane(Node, 4, AArch64::LD4i16);
  3962. return;
  3963. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  3964. VT == MVT::v2f32) {
  3965. SelectLoadLane(Node, 4, AArch64::LD4i32);
  3966. return;
  3967. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  3968. VT == MVT::v1f64) {
  3969. SelectLoadLane(Node, 4, AArch64::LD4i64);
  3970. return;
  3971. }
  3972. break;
  3973. case Intrinsic::aarch64_ld64b:
  3974. SelectLoad(Node, 8, AArch64::LD64B, AArch64::x8sub_0);
  3975. return;
  3976. case Intrinsic::aarch64_sve_ld2_sret: {
  3977. if (VT == MVT::nxv16i8) {
  3978. SelectPredicatedLoad(Node, 2, 0, AArch64::LD2B_IMM, AArch64::LD2B,
  3979. true);
  3980. return;
  3981. } else if (VT == MVT::nxv8i16 || VT == MVT::nxv8f16 ||
  3982. VT == MVT::nxv8bf16) {
  3983. SelectPredicatedLoad(Node, 2, 1, AArch64::LD2H_IMM, AArch64::LD2H,
  3984. true);
  3985. return;
  3986. } else if (VT == MVT::nxv4i32 || VT == MVT::nxv4f32) {
  3987. SelectPredicatedLoad(Node, 2, 2, AArch64::LD2W_IMM, AArch64::LD2W,
  3988. true);
  3989. return;
  3990. } else if (VT == MVT::nxv2i64 || VT == MVT::nxv2f64) {
  3991. SelectPredicatedLoad(Node, 2, 3, AArch64::LD2D_IMM, AArch64::LD2D,
  3992. true);
  3993. return;
  3994. }
  3995. break;
  3996. }
  3997. case Intrinsic::aarch64_sve_ld3_sret: {
  3998. if (VT == MVT::nxv16i8) {
  3999. SelectPredicatedLoad(Node, 3, 0, AArch64::LD3B_IMM, AArch64::LD3B,
  4000. true);
  4001. return;
  4002. } else if (VT == MVT::nxv8i16 || VT == MVT::nxv8f16 ||
  4003. VT == MVT::nxv8bf16) {
  4004. SelectPredicatedLoad(Node, 3, 1, AArch64::LD3H_IMM, AArch64::LD3H,
  4005. true);
  4006. return;
  4007. } else if (VT == MVT::nxv4i32 || VT == MVT::nxv4f32) {
  4008. SelectPredicatedLoad(Node, 3, 2, AArch64::LD3W_IMM, AArch64::LD3W,
  4009. true);
  4010. return;
  4011. } else if (VT == MVT::nxv2i64 || VT == MVT::nxv2f64) {
  4012. SelectPredicatedLoad(Node, 3, 3, AArch64::LD3D_IMM, AArch64::LD3D,
  4013. true);
  4014. return;
  4015. }
  4016. break;
  4017. }
  4018. case Intrinsic::aarch64_sve_ld4_sret: {
  4019. if (VT == MVT::nxv16i8) {
  4020. SelectPredicatedLoad(Node, 4, 0, AArch64::LD4B_IMM, AArch64::LD4B,
  4021. true);
  4022. return;
  4023. } else if (VT == MVT::nxv8i16 || VT == MVT::nxv8f16 ||
  4024. VT == MVT::nxv8bf16) {
  4025. SelectPredicatedLoad(Node, 4, 1, AArch64::LD4H_IMM, AArch64::LD4H,
  4026. true);
  4027. return;
  4028. } else if (VT == MVT::nxv4i32 || VT == MVT::nxv4f32) {
  4029. SelectPredicatedLoad(Node, 4, 2, AArch64::LD4W_IMM, AArch64::LD4W,
  4030. true);
  4031. return;
  4032. } else if (VT == MVT::nxv2i64 || VT == MVT::nxv2f64) {
  4033. SelectPredicatedLoad(Node, 4, 3, AArch64::LD4D_IMM, AArch64::LD4D,
  4034. true);
  4035. return;
  4036. }
  4037. break;
  4038. }
  4039. case Intrinsic::swift_async_context_addr: {
  4040. SDLoc DL(Node);
  4041. SDValue Chain = Node->getOperand(0);
  4042. SDValue CopyFP = CurDAG->getCopyFromReg(Chain, DL, AArch64::FP, MVT::i64);
  4043. SDValue Res = SDValue(
  4044. CurDAG->getMachineNode(AArch64::SUBXri, DL, MVT::i64, CopyFP,
  4045. CurDAG->getTargetConstant(8, DL, MVT::i32),
  4046. CurDAG->getTargetConstant(0, DL, MVT::i32)),
  4047. 0);
  4048. ReplaceUses(SDValue(Node, 0), Res);
  4049. ReplaceUses(SDValue(Node, 1), CopyFP.getValue(1));
  4050. CurDAG->RemoveDeadNode(Node);
  4051. auto &MF = CurDAG->getMachineFunction();
  4052. MF.getFrameInfo().setFrameAddressIsTaken(true);
  4053. MF.getInfo<AArch64FunctionInfo>()->setHasSwiftAsyncContext(true);
  4054. return;
  4055. }
  4056. }
  4057. } break;
  4058. case ISD::INTRINSIC_WO_CHAIN: {
  4059. unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
  4060. switch (IntNo) {
  4061. default:
  4062. break;
  4063. case Intrinsic::aarch64_tagp:
  4064. SelectTagP(Node);
  4065. return;
  4066. case Intrinsic::aarch64_neon_tbl2:
  4067. SelectTable(Node, 2,
  4068. VT == MVT::v8i8 ? AArch64::TBLv8i8Two : AArch64::TBLv16i8Two,
  4069. false);
  4070. return;
  4071. case Intrinsic::aarch64_neon_tbl3:
  4072. SelectTable(Node, 3, VT == MVT::v8i8 ? AArch64::TBLv8i8Three
  4073. : AArch64::TBLv16i8Three,
  4074. false);
  4075. return;
  4076. case Intrinsic::aarch64_neon_tbl4:
  4077. SelectTable(Node, 4, VT == MVT::v8i8 ? AArch64::TBLv8i8Four
  4078. : AArch64::TBLv16i8Four,
  4079. false);
  4080. return;
  4081. case Intrinsic::aarch64_neon_tbx2:
  4082. SelectTable(Node, 2,
  4083. VT == MVT::v8i8 ? AArch64::TBXv8i8Two : AArch64::TBXv16i8Two,
  4084. true);
  4085. return;
  4086. case Intrinsic::aarch64_neon_tbx3:
  4087. SelectTable(Node, 3, VT == MVT::v8i8 ? AArch64::TBXv8i8Three
  4088. : AArch64::TBXv16i8Three,
  4089. true);
  4090. return;
  4091. case Intrinsic::aarch64_neon_tbx4:
  4092. SelectTable(Node, 4, VT == MVT::v8i8 ? AArch64::TBXv8i8Four
  4093. : AArch64::TBXv16i8Four,
  4094. true);
  4095. return;
  4096. case Intrinsic::aarch64_neon_smull:
  4097. case Intrinsic::aarch64_neon_umull:
  4098. if (tryMULLV64LaneV128(IntNo, Node))
  4099. return;
  4100. break;
  4101. case Intrinsic::aarch64_sve_whilege_x2:
  4102. if (auto Op = SelectOpcodeFromVT<SelectTypeKind::Int1>(
  4103. Node->getValueType(0),
  4104. {AArch64::WHILEGE_2PXX_B, AArch64::WHILEGE_2PXX_H,
  4105. AArch64::WHILEGE_2PXX_S, AArch64::WHILEGE_2PXX_D}))
  4106. SelectWhilePair(Node, Op);
  4107. return;
  4108. case Intrinsic::aarch64_sve_whilegt_x2:
  4109. if (auto Op = SelectOpcodeFromVT<SelectTypeKind::Int1>(
  4110. Node->getValueType(0),
  4111. {AArch64::WHILEGT_2PXX_B, AArch64::WHILEGT_2PXX_H,
  4112. AArch64::WHILEGT_2PXX_S, AArch64::WHILEGT_2PXX_D}))
  4113. SelectWhilePair(Node, Op);
  4114. return;
  4115. case Intrinsic::aarch64_sve_whilehi_x2:
  4116. if (auto Op = SelectOpcodeFromVT<SelectTypeKind::Int1>(
  4117. Node->getValueType(0),
  4118. {AArch64::WHILEHI_2PXX_B, AArch64::WHILEHI_2PXX_H,
  4119. AArch64::WHILEHI_2PXX_S, AArch64::WHILEHI_2PXX_D}))
  4120. SelectWhilePair(Node, Op);
  4121. return;
  4122. case Intrinsic::aarch64_sve_whilehs_x2:
  4123. if (auto Op = SelectOpcodeFromVT<SelectTypeKind::Int1>(
  4124. Node->getValueType(0),
  4125. {AArch64::WHILEHS_2PXX_B, AArch64::WHILEHS_2PXX_H,
  4126. AArch64::WHILEHS_2PXX_S, AArch64::WHILEHS_2PXX_D}))
  4127. SelectWhilePair(Node, Op);
  4128. return;
  4129. case Intrinsic::aarch64_sve_whilele_x2:
  4130. if (auto Op = SelectOpcodeFromVT<SelectTypeKind::Int1>(
  4131. Node->getValueType(0),
  4132. {AArch64::WHILELE_2PXX_B, AArch64::WHILELE_2PXX_H,
  4133. AArch64::WHILELE_2PXX_S, AArch64::WHILELE_2PXX_D}))
  4134. SelectWhilePair(Node, Op);
  4135. return;
  4136. case Intrinsic::aarch64_sve_whilelo_x2:
  4137. if (auto Op = SelectOpcodeFromVT<SelectTypeKind::Int1>(
  4138. Node->getValueType(0),
  4139. {AArch64::WHILELO_2PXX_B, AArch64::WHILELO_2PXX_H,
  4140. AArch64::WHILELO_2PXX_S, AArch64::WHILELO_2PXX_D}))
  4141. SelectWhilePair(Node, Op);
  4142. return;
  4143. case Intrinsic::aarch64_sve_whilels_x2:
  4144. if (auto Op = SelectOpcodeFromVT<SelectTypeKind::Int1>(
  4145. Node->getValueType(0),
  4146. {AArch64::WHILELS_2PXX_B, AArch64::WHILELS_2PXX_H,
  4147. AArch64::WHILELS_2PXX_S, AArch64::WHILELS_2PXX_D}))
  4148. SelectWhilePair(Node, Op);
  4149. return;
  4150. case Intrinsic::aarch64_sve_whilelt_x2:
  4151. if (auto Op = SelectOpcodeFromVT<SelectTypeKind::Int1>(
  4152. Node->getValueType(0),
  4153. {AArch64::WHILELT_2PXX_B, AArch64::WHILELT_2PXX_H,
  4154. AArch64::WHILELT_2PXX_S, AArch64::WHILELT_2PXX_D}))
  4155. SelectWhilePair(Node, Op);
  4156. return;
  4157. case Intrinsic::aarch64_sve_fcvts_x2:
  4158. SelectCVTIntrinsic(Node, 2, AArch64::FCVTZS_2Z2Z_StoS);
  4159. return;
  4160. case Intrinsic::aarch64_sve_scvtf_x2:
  4161. SelectCVTIntrinsic(Node, 2, AArch64::SCVTF_2Z2Z_StoS);
  4162. return;
  4163. case Intrinsic::aarch64_sve_fcvtu_x2:
  4164. SelectCVTIntrinsic(Node, 2, AArch64::FCVTZU_2Z2Z_StoS);
  4165. return;
  4166. case Intrinsic::aarch64_sve_ucvtf_x2:
  4167. SelectCVTIntrinsic(Node, 2, AArch64::UCVTF_2Z2Z_StoS);
  4168. return;
  4169. case Intrinsic::aarch64_sve_fcvts_x4:
  4170. SelectCVTIntrinsic(Node, 4, AArch64::FCVTZS_4Z4Z_StoS);
  4171. return;
  4172. case Intrinsic::aarch64_sve_scvtf_x4:
  4173. SelectCVTIntrinsic(Node, 4, AArch64::SCVTF_4Z4Z_StoS);
  4174. return;
  4175. case Intrinsic::aarch64_sve_fcvtu_x4:
  4176. SelectCVTIntrinsic(Node, 4, AArch64::FCVTZU_4Z4Z_StoS);
  4177. return;
  4178. case Intrinsic::aarch64_sve_ucvtf_x4:
  4179. SelectCVTIntrinsic(Node, 4, AArch64::UCVTF_4Z4Z_StoS);
  4180. return;
  4181. }
  4182. break;
  4183. }
  4184. case ISD::INTRINSIC_VOID: {
  4185. unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
  4186. if (Node->getNumOperands() >= 3)
  4187. VT = Node->getOperand(2)->getValueType(0);
  4188. switch (IntNo) {
  4189. default:
  4190. break;
  4191. case Intrinsic::aarch64_neon_st1x2: {
  4192. if (VT == MVT::v8i8) {
  4193. SelectStore(Node, 2, AArch64::ST1Twov8b);
  4194. return;
  4195. } else if (VT == MVT::v16i8) {
  4196. SelectStore(Node, 2, AArch64::ST1Twov16b);
  4197. return;
  4198. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4199. VT == MVT::v4bf16) {
  4200. SelectStore(Node, 2, AArch64::ST1Twov4h);
  4201. return;
  4202. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 ||
  4203. VT == MVT::v8bf16) {
  4204. SelectStore(Node, 2, AArch64::ST1Twov8h);
  4205. return;
  4206. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4207. SelectStore(Node, 2, AArch64::ST1Twov2s);
  4208. return;
  4209. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4210. SelectStore(Node, 2, AArch64::ST1Twov4s);
  4211. return;
  4212. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4213. SelectStore(Node, 2, AArch64::ST1Twov2d);
  4214. return;
  4215. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4216. SelectStore(Node, 2, AArch64::ST1Twov1d);
  4217. return;
  4218. }
  4219. break;
  4220. }
  4221. case Intrinsic::aarch64_neon_st1x3: {
  4222. if (VT == MVT::v8i8) {
  4223. SelectStore(Node, 3, AArch64::ST1Threev8b);
  4224. return;
  4225. } else if (VT == MVT::v16i8) {
  4226. SelectStore(Node, 3, AArch64::ST1Threev16b);
  4227. return;
  4228. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4229. VT == MVT::v4bf16) {
  4230. SelectStore(Node, 3, AArch64::ST1Threev4h);
  4231. return;
  4232. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 ||
  4233. VT == MVT::v8bf16) {
  4234. SelectStore(Node, 3, AArch64::ST1Threev8h);
  4235. return;
  4236. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4237. SelectStore(Node, 3, AArch64::ST1Threev2s);
  4238. return;
  4239. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4240. SelectStore(Node, 3, AArch64::ST1Threev4s);
  4241. return;
  4242. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4243. SelectStore(Node, 3, AArch64::ST1Threev2d);
  4244. return;
  4245. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4246. SelectStore(Node, 3, AArch64::ST1Threev1d);
  4247. return;
  4248. }
  4249. break;
  4250. }
  4251. case Intrinsic::aarch64_neon_st1x4: {
  4252. if (VT == MVT::v8i8) {
  4253. SelectStore(Node, 4, AArch64::ST1Fourv8b);
  4254. return;
  4255. } else if (VT == MVT::v16i8) {
  4256. SelectStore(Node, 4, AArch64::ST1Fourv16b);
  4257. return;
  4258. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4259. VT == MVT::v4bf16) {
  4260. SelectStore(Node, 4, AArch64::ST1Fourv4h);
  4261. return;
  4262. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 ||
  4263. VT == MVT::v8bf16) {
  4264. SelectStore(Node, 4, AArch64::ST1Fourv8h);
  4265. return;
  4266. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4267. SelectStore(Node, 4, AArch64::ST1Fourv2s);
  4268. return;
  4269. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4270. SelectStore(Node, 4, AArch64::ST1Fourv4s);
  4271. return;
  4272. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4273. SelectStore(Node, 4, AArch64::ST1Fourv2d);
  4274. return;
  4275. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4276. SelectStore(Node, 4, AArch64::ST1Fourv1d);
  4277. return;
  4278. }
  4279. break;
  4280. }
  4281. case Intrinsic::aarch64_neon_st2: {
  4282. if (VT == MVT::v8i8) {
  4283. SelectStore(Node, 2, AArch64::ST2Twov8b);
  4284. return;
  4285. } else if (VT == MVT::v16i8) {
  4286. SelectStore(Node, 2, AArch64::ST2Twov16b);
  4287. return;
  4288. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4289. VT == MVT::v4bf16) {
  4290. SelectStore(Node, 2, AArch64::ST2Twov4h);
  4291. return;
  4292. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 ||
  4293. VT == MVT::v8bf16) {
  4294. SelectStore(Node, 2, AArch64::ST2Twov8h);
  4295. return;
  4296. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4297. SelectStore(Node, 2, AArch64::ST2Twov2s);
  4298. return;
  4299. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4300. SelectStore(Node, 2, AArch64::ST2Twov4s);
  4301. return;
  4302. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4303. SelectStore(Node, 2, AArch64::ST2Twov2d);
  4304. return;
  4305. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4306. SelectStore(Node, 2, AArch64::ST1Twov1d);
  4307. return;
  4308. }
  4309. break;
  4310. }
  4311. case Intrinsic::aarch64_neon_st3: {
  4312. if (VT == MVT::v8i8) {
  4313. SelectStore(Node, 3, AArch64::ST3Threev8b);
  4314. return;
  4315. } else if (VT == MVT::v16i8) {
  4316. SelectStore(Node, 3, AArch64::ST3Threev16b);
  4317. return;
  4318. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4319. VT == MVT::v4bf16) {
  4320. SelectStore(Node, 3, AArch64::ST3Threev4h);
  4321. return;
  4322. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 ||
  4323. VT == MVT::v8bf16) {
  4324. SelectStore(Node, 3, AArch64::ST3Threev8h);
  4325. return;
  4326. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4327. SelectStore(Node, 3, AArch64::ST3Threev2s);
  4328. return;
  4329. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4330. SelectStore(Node, 3, AArch64::ST3Threev4s);
  4331. return;
  4332. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4333. SelectStore(Node, 3, AArch64::ST3Threev2d);
  4334. return;
  4335. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4336. SelectStore(Node, 3, AArch64::ST1Threev1d);
  4337. return;
  4338. }
  4339. break;
  4340. }
  4341. case Intrinsic::aarch64_neon_st4: {
  4342. if (VT == MVT::v8i8) {
  4343. SelectStore(Node, 4, AArch64::ST4Fourv8b);
  4344. return;
  4345. } else if (VT == MVT::v16i8) {
  4346. SelectStore(Node, 4, AArch64::ST4Fourv16b);
  4347. return;
  4348. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4349. VT == MVT::v4bf16) {
  4350. SelectStore(Node, 4, AArch64::ST4Fourv4h);
  4351. return;
  4352. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 ||
  4353. VT == MVT::v8bf16) {
  4354. SelectStore(Node, 4, AArch64::ST4Fourv8h);
  4355. return;
  4356. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4357. SelectStore(Node, 4, AArch64::ST4Fourv2s);
  4358. return;
  4359. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4360. SelectStore(Node, 4, AArch64::ST4Fourv4s);
  4361. return;
  4362. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4363. SelectStore(Node, 4, AArch64::ST4Fourv2d);
  4364. return;
  4365. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4366. SelectStore(Node, 4, AArch64::ST1Fourv1d);
  4367. return;
  4368. }
  4369. break;
  4370. }
  4371. case Intrinsic::aarch64_neon_st2lane: {
  4372. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  4373. SelectStoreLane(Node, 2, AArch64::ST2i8);
  4374. return;
  4375. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4376. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  4377. SelectStoreLane(Node, 2, AArch64::ST2i16);
  4378. return;
  4379. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  4380. VT == MVT::v2f32) {
  4381. SelectStoreLane(Node, 2, AArch64::ST2i32);
  4382. return;
  4383. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  4384. VT == MVT::v1f64) {
  4385. SelectStoreLane(Node, 2, AArch64::ST2i64);
  4386. return;
  4387. }
  4388. break;
  4389. }
  4390. case Intrinsic::aarch64_neon_st3lane: {
  4391. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  4392. SelectStoreLane(Node, 3, AArch64::ST3i8);
  4393. return;
  4394. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4395. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  4396. SelectStoreLane(Node, 3, AArch64::ST3i16);
  4397. return;
  4398. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  4399. VT == MVT::v2f32) {
  4400. SelectStoreLane(Node, 3, AArch64::ST3i32);
  4401. return;
  4402. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  4403. VT == MVT::v1f64) {
  4404. SelectStoreLane(Node, 3, AArch64::ST3i64);
  4405. return;
  4406. }
  4407. break;
  4408. }
  4409. case Intrinsic::aarch64_neon_st4lane: {
  4410. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  4411. SelectStoreLane(Node, 4, AArch64::ST4i8);
  4412. return;
  4413. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4414. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  4415. SelectStoreLane(Node, 4, AArch64::ST4i16);
  4416. return;
  4417. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  4418. VT == MVT::v2f32) {
  4419. SelectStoreLane(Node, 4, AArch64::ST4i32);
  4420. return;
  4421. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  4422. VT == MVT::v1f64) {
  4423. SelectStoreLane(Node, 4, AArch64::ST4i64);
  4424. return;
  4425. }
  4426. break;
  4427. }
  4428. case Intrinsic::aarch64_sve_st2: {
  4429. if (VT == MVT::nxv16i8) {
  4430. SelectPredicatedStore(Node, 2, 0, AArch64::ST2B, AArch64::ST2B_IMM);
  4431. return;
  4432. } else if (VT == MVT::nxv8i16 || VT == MVT::nxv8f16 ||
  4433. VT == MVT::nxv8bf16) {
  4434. SelectPredicatedStore(Node, 2, 1, AArch64::ST2H, AArch64::ST2H_IMM);
  4435. return;
  4436. } else if (VT == MVT::nxv4i32 || VT == MVT::nxv4f32) {
  4437. SelectPredicatedStore(Node, 2, 2, AArch64::ST2W, AArch64::ST2W_IMM);
  4438. return;
  4439. } else if (VT == MVT::nxv2i64 || VT == MVT::nxv2f64) {
  4440. SelectPredicatedStore(Node, 2, 3, AArch64::ST2D, AArch64::ST2D_IMM);
  4441. return;
  4442. }
  4443. break;
  4444. }
  4445. case Intrinsic::aarch64_sve_st3: {
  4446. if (VT == MVT::nxv16i8) {
  4447. SelectPredicatedStore(Node, 3, 0, AArch64::ST3B, AArch64::ST3B_IMM);
  4448. return;
  4449. } else if (VT == MVT::nxv8i16 || VT == MVT::nxv8f16 ||
  4450. VT == MVT::nxv8bf16) {
  4451. SelectPredicatedStore(Node, 3, 1, AArch64::ST3H, AArch64::ST3H_IMM);
  4452. return;
  4453. } else if (VT == MVT::nxv4i32 || VT == MVT::nxv4f32) {
  4454. SelectPredicatedStore(Node, 3, 2, AArch64::ST3W, AArch64::ST3W_IMM);
  4455. return;
  4456. } else if (VT == MVT::nxv2i64 || VT == MVT::nxv2f64) {
  4457. SelectPredicatedStore(Node, 3, 3, AArch64::ST3D, AArch64::ST3D_IMM);
  4458. return;
  4459. }
  4460. break;
  4461. }
  4462. case Intrinsic::aarch64_sve_st4: {
  4463. if (VT == MVT::nxv16i8) {
  4464. SelectPredicatedStore(Node, 4, 0, AArch64::ST4B, AArch64::ST4B_IMM);
  4465. return;
  4466. } else if (VT == MVT::nxv8i16 || VT == MVT::nxv8f16 ||
  4467. VT == MVT::nxv8bf16) {
  4468. SelectPredicatedStore(Node, 4, 1, AArch64::ST4H, AArch64::ST4H_IMM);
  4469. return;
  4470. } else if (VT == MVT::nxv4i32 || VT == MVT::nxv4f32) {
  4471. SelectPredicatedStore(Node, 4, 2, AArch64::ST4W, AArch64::ST4W_IMM);
  4472. return;
  4473. } else if (VT == MVT::nxv2i64 || VT == MVT::nxv2f64) {
  4474. SelectPredicatedStore(Node, 4, 3, AArch64::ST4D, AArch64::ST4D_IMM);
  4475. return;
  4476. }
  4477. break;
  4478. }
  4479. }
  4480. break;
  4481. }
  4482. case AArch64ISD::LD2post: {
  4483. if (VT == MVT::v8i8) {
  4484. SelectPostLoad(Node, 2, AArch64::LD2Twov8b_POST, AArch64::dsub0);
  4485. return;
  4486. } else if (VT == MVT::v16i8) {
  4487. SelectPostLoad(Node, 2, AArch64::LD2Twov16b_POST, AArch64::qsub0);
  4488. return;
  4489. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4490. SelectPostLoad(Node, 2, AArch64::LD2Twov4h_POST, AArch64::dsub0);
  4491. return;
  4492. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4493. SelectPostLoad(Node, 2, AArch64::LD2Twov8h_POST, AArch64::qsub0);
  4494. return;
  4495. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4496. SelectPostLoad(Node, 2, AArch64::LD2Twov2s_POST, AArch64::dsub0);
  4497. return;
  4498. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4499. SelectPostLoad(Node, 2, AArch64::LD2Twov4s_POST, AArch64::qsub0);
  4500. return;
  4501. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4502. SelectPostLoad(Node, 2, AArch64::LD1Twov1d_POST, AArch64::dsub0);
  4503. return;
  4504. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4505. SelectPostLoad(Node, 2, AArch64::LD2Twov2d_POST, AArch64::qsub0);
  4506. return;
  4507. }
  4508. break;
  4509. }
  4510. case AArch64ISD::LD3post: {
  4511. if (VT == MVT::v8i8) {
  4512. SelectPostLoad(Node, 3, AArch64::LD3Threev8b_POST, AArch64::dsub0);
  4513. return;
  4514. } else if (VT == MVT::v16i8) {
  4515. SelectPostLoad(Node, 3, AArch64::LD3Threev16b_POST, AArch64::qsub0);
  4516. return;
  4517. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4518. SelectPostLoad(Node, 3, AArch64::LD3Threev4h_POST, AArch64::dsub0);
  4519. return;
  4520. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4521. SelectPostLoad(Node, 3, AArch64::LD3Threev8h_POST, AArch64::qsub0);
  4522. return;
  4523. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4524. SelectPostLoad(Node, 3, AArch64::LD3Threev2s_POST, AArch64::dsub0);
  4525. return;
  4526. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4527. SelectPostLoad(Node, 3, AArch64::LD3Threev4s_POST, AArch64::qsub0);
  4528. return;
  4529. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4530. SelectPostLoad(Node, 3, AArch64::LD1Threev1d_POST, AArch64::dsub0);
  4531. return;
  4532. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4533. SelectPostLoad(Node, 3, AArch64::LD3Threev2d_POST, AArch64::qsub0);
  4534. return;
  4535. }
  4536. break;
  4537. }
  4538. case AArch64ISD::LD4post: {
  4539. if (VT == MVT::v8i8) {
  4540. SelectPostLoad(Node, 4, AArch64::LD4Fourv8b_POST, AArch64::dsub0);
  4541. return;
  4542. } else if (VT == MVT::v16i8) {
  4543. SelectPostLoad(Node, 4, AArch64::LD4Fourv16b_POST, AArch64::qsub0);
  4544. return;
  4545. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4546. SelectPostLoad(Node, 4, AArch64::LD4Fourv4h_POST, AArch64::dsub0);
  4547. return;
  4548. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4549. SelectPostLoad(Node, 4, AArch64::LD4Fourv8h_POST, AArch64::qsub0);
  4550. return;
  4551. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4552. SelectPostLoad(Node, 4, AArch64::LD4Fourv2s_POST, AArch64::dsub0);
  4553. return;
  4554. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4555. SelectPostLoad(Node, 4, AArch64::LD4Fourv4s_POST, AArch64::qsub0);
  4556. return;
  4557. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4558. SelectPostLoad(Node, 4, AArch64::LD1Fourv1d_POST, AArch64::dsub0);
  4559. return;
  4560. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4561. SelectPostLoad(Node, 4, AArch64::LD4Fourv2d_POST, AArch64::qsub0);
  4562. return;
  4563. }
  4564. break;
  4565. }
  4566. case AArch64ISD::LD1x2post: {
  4567. if (VT == MVT::v8i8) {
  4568. SelectPostLoad(Node, 2, AArch64::LD1Twov8b_POST, AArch64::dsub0);
  4569. return;
  4570. } else if (VT == MVT::v16i8) {
  4571. SelectPostLoad(Node, 2, AArch64::LD1Twov16b_POST, AArch64::qsub0);
  4572. return;
  4573. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4574. SelectPostLoad(Node, 2, AArch64::LD1Twov4h_POST, AArch64::dsub0);
  4575. return;
  4576. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4577. SelectPostLoad(Node, 2, AArch64::LD1Twov8h_POST, AArch64::qsub0);
  4578. return;
  4579. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4580. SelectPostLoad(Node, 2, AArch64::LD1Twov2s_POST, AArch64::dsub0);
  4581. return;
  4582. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4583. SelectPostLoad(Node, 2, AArch64::LD1Twov4s_POST, AArch64::qsub0);
  4584. return;
  4585. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4586. SelectPostLoad(Node, 2, AArch64::LD1Twov1d_POST, AArch64::dsub0);
  4587. return;
  4588. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4589. SelectPostLoad(Node, 2, AArch64::LD1Twov2d_POST, AArch64::qsub0);
  4590. return;
  4591. }
  4592. break;
  4593. }
  4594. case AArch64ISD::LD1x3post: {
  4595. if (VT == MVT::v8i8) {
  4596. SelectPostLoad(Node, 3, AArch64::LD1Threev8b_POST, AArch64::dsub0);
  4597. return;
  4598. } else if (VT == MVT::v16i8) {
  4599. SelectPostLoad(Node, 3, AArch64::LD1Threev16b_POST, AArch64::qsub0);
  4600. return;
  4601. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4602. SelectPostLoad(Node, 3, AArch64::LD1Threev4h_POST, AArch64::dsub0);
  4603. return;
  4604. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4605. SelectPostLoad(Node, 3, AArch64::LD1Threev8h_POST, AArch64::qsub0);
  4606. return;
  4607. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4608. SelectPostLoad(Node, 3, AArch64::LD1Threev2s_POST, AArch64::dsub0);
  4609. return;
  4610. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4611. SelectPostLoad(Node, 3, AArch64::LD1Threev4s_POST, AArch64::qsub0);
  4612. return;
  4613. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4614. SelectPostLoad(Node, 3, AArch64::LD1Threev1d_POST, AArch64::dsub0);
  4615. return;
  4616. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4617. SelectPostLoad(Node, 3, AArch64::LD1Threev2d_POST, AArch64::qsub0);
  4618. return;
  4619. }
  4620. break;
  4621. }
  4622. case AArch64ISD::LD1x4post: {
  4623. if (VT == MVT::v8i8) {
  4624. SelectPostLoad(Node, 4, AArch64::LD1Fourv8b_POST, AArch64::dsub0);
  4625. return;
  4626. } else if (VT == MVT::v16i8) {
  4627. SelectPostLoad(Node, 4, AArch64::LD1Fourv16b_POST, AArch64::qsub0);
  4628. return;
  4629. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4630. SelectPostLoad(Node, 4, AArch64::LD1Fourv4h_POST, AArch64::dsub0);
  4631. return;
  4632. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4633. SelectPostLoad(Node, 4, AArch64::LD1Fourv8h_POST, AArch64::qsub0);
  4634. return;
  4635. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4636. SelectPostLoad(Node, 4, AArch64::LD1Fourv2s_POST, AArch64::dsub0);
  4637. return;
  4638. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4639. SelectPostLoad(Node, 4, AArch64::LD1Fourv4s_POST, AArch64::qsub0);
  4640. return;
  4641. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4642. SelectPostLoad(Node, 4, AArch64::LD1Fourv1d_POST, AArch64::dsub0);
  4643. return;
  4644. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4645. SelectPostLoad(Node, 4, AArch64::LD1Fourv2d_POST, AArch64::qsub0);
  4646. return;
  4647. }
  4648. break;
  4649. }
  4650. case AArch64ISD::LD1DUPpost: {
  4651. if (VT == MVT::v8i8) {
  4652. SelectPostLoad(Node, 1, AArch64::LD1Rv8b_POST, AArch64::dsub0);
  4653. return;
  4654. } else if (VT == MVT::v16i8) {
  4655. SelectPostLoad(Node, 1, AArch64::LD1Rv16b_POST, AArch64::qsub0);
  4656. return;
  4657. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4658. SelectPostLoad(Node, 1, AArch64::LD1Rv4h_POST, AArch64::dsub0);
  4659. return;
  4660. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4661. SelectPostLoad(Node, 1, AArch64::LD1Rv8h_POST, AArch64::qsub0);
  4662. return;
  4663. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4664. SelectPostLoad(Node, 1, AArch64::LD1Rv2s_POST, AArch64::dsub0);
  4665. return;
  4666. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4667. SelectPostLoad(Node, 1, AArch64::LD1Rv4s_POST, AArch64::qsub0);
  4668. return;
  4669. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4670. SelectPostLoad(Node, 1, AArch64::LD1Rv1d_POST, AArch64::dsub0);
  4671. return;
  4672. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4673. SelectPostLoad(Node, 1, AArch64::LD1Rv2d_POST, AArch64::qsub0);
  4674. return;
  4675. }
  4676. break;
  4677. }
  4678. case AArch64ISD::LD2DUPpost: {
  4679. if (VT == MVT::v8i8) {
  4680. SelectPostLoad(Node, 2, AArch64::LD2Rv8b_POST, AArch64::dsub0);
  4681. return;
  4682. } else if (VT == MVT::v16i8) {
  4683. SelectPostLoad(Node, 2, AArch64::LD2Rv16b_POST, AArch64::qsub0);
  4684. return;
  4685. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4686. SelectPostLoad(Node, 2, AArch64::LD2Rv4h_POST, AArch64::dsub0);
  4687. return;
  4688. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4689. SelectPostLoad(Node, 2, AArch64::LD2Rv8h_POST, AArch64::qsub0);
  4690. return;
  4691. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4692. SelectPostLoad(Node, 2, AArch64::LD2Rv2s_POST, AArch64::dsub0);
  4693. return;
  4694. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4695. SelectPostLoad(Node, 2, AArch64::LD2Rv4s_POST, AArch64::qsub0);
  4696. return;
  4697. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4698. SelectPostLoad(Node, 2, AArch64::LD2Rv1d_POST, AArch64::dsub0);
  4699. return;
  4700. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4701. SelectPostLoad(Node, 2, AArch64::LD2Rv2d_POST, AArch64::qsub0);
  4702. return;
  4703. }
  4704. break;
  4705. }
  4706. case AArch64ISD::LD3DUPpost: {
  4707. if (VT == MVT::v8i8) {
  4708. SelectPostLoad(Node, 3, AArch64::LD3Rv8b_POST, AArch64::dsub0);
  4709. return;
  4710. } else if (VT == MVT::v16i8) {
  4711. SelectPostLoad(Node, 3, AArch64::LD3Rv16b_POST, AArch64::qsub0);
  4712. return;
  4713. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4714. SelectPostLoad(Node, 3, AArch64::LD3Rv4h_POST, AArch64::dsub0);
  4715. return;
  4716. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4717. SelectPostLoad(Node, 3, AArch64::LD3Rv8h_POST, AArch64::qsub0);
  4718. return;
  4719. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4720. SelectPostLoad(Node, 3, AArch64::LD3Rv2s_POST, AArch64::dsub0);
  4721. return;
  4722. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4723. SelectPostLoad(Node, 3, AArch64::LD3Rv4s_POST, AArch64::qsub0);
  4724. return;
  4725. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4726. SelectPostLoad(Node, 3, AArch64::LD3Rv1d_POST, AArch64::dsub0);
  4727. return;
  4728. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4729. SelectPostLoad(Node, 3, AArch64::LD3Rv2d_POST, AArch64::qsub0);
  4730. return;
  4731. }
  4732. break;
  4733. }
  4734. case AArch64ISD::LD4DUPpost: {
  4735. if (VT == MVT::v8i8) {
  4736. SelectPostLoad(Node, 4, AArch64::LD4Rv8b_POST, AArch64::dsub0);
  4737. return;
  4738. } else if (VT == MVT::v16i8) {
  4739. SelectPostLoad(Node, 4, AArch64::LD4Rv16b_POST, AArch64::qsub0);
  4740. return;
  4741. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4742. SelectPostLoad(Node, 4, AArch64::LD4Rv4h_POST, AArch64::dsub0);
  4743. return;
  4744. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4745. SelectPostLoad(Node, 4, AArch64::LD4Rv8h_POST, AArch64::qsub0);
  4746. return;
  4747. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4748. SelectPostLoad(Node, 4, AArch64::LD4Rv2s_POST, AArch64::dsub0);
  4749. return;
  4750. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4751. SelectPostLoad(Node, 4, AArch64::LD4Rv4s_POST, AArch64::qsub0);
  4752. return;
  4753. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4754. SelectPostLoad(Node, 4, AArch64::LD4Rv1d_POST, AArch64::dsub0);
  4755. return;
  4756. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4757. SelectPostLoad(Node, 4, AArch64::LD4Rv2d_POST, AArch64::qsub0);
  4758. return;
  4759. }
  4760. break;
  4761. }
  4762. case AArch64ISD::LD1LANEpost: {
  4763. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  4764. SelectPostLoadLane(Node, 1, AArch64::LD1i8_POST);
  4765. return;
  4766. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4767. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  4768. SelectPostLoadLane(Node, 1, AArch64::LD1i16_POST);
  4769. return;
  4770. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  4771. VT == MVT::v2f32) {
  4772. SelectPostLoadLane(Node, 1, AArch64::LD1i32_POST);
  4773. return;
  4774. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  4775. VT == MVT::v1f64) {
  4776. SelectPostLoadLane(Node, 1, AArch64::LD1i64_POST);
  4777. return;
  4778. }
  4779. break;
  4780. }
  4781. case AArch64ISD::LD2LANEpost: {
  4782. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  4783. SelectPostLoadLane(Node, 2, AArch64::LD2i8_POST);
  4784. return;
  4785. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4786. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  4787. SelectPostLoadLane(Node, 2, AArch64::LD2i16_POST);
  4788. return;
  4789. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  4790. VT == MVT::v2f32) {
  4791. SelectPostLoadLane(Node, 2, AArch64::LD2i32_POST);
  4792. return;
  4793. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  4794. VT == MVT::v1f64) {
  4795. SelectPostLoadLane(Node, 2, AArch64::LD2i64_POST);
  4796. return;
  4797. }
  4798. break;
  4799. }
  4800. case AArch64ISD::LD3LANEpost: {
  4801. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  4802. SelectPostLoadLane(Node, 3, AArch64::LD3i8_POST);
  4803. return;
  4804. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4805. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  4806. SelectPostLoadLane(Node, 3, AArch64::LD3i16_POST);
  4807. return;
  4808. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  4809. VT == MVT::v2f32) {
  4810. SelectPostLoadLane(Node, 3, AArch64::LD3i32_POST);
  4811. return;
  4812. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  4813. VT == MVT::v1f64) {
  4814. SelectPostLoadLane(Node, 3, AArch64::LD3i64_POST);
  4815. return;
  4816. }
  4817. break;
  4818. }
  4819. case AArch64ISD::LD4LANEpost: {
  4820. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  4821. SelectPostLoadLane(Node, 4, AArch64::LD4i8_POST);
  4822. return;
  4823. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  4824. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  4825. SelectPostLoadLane(Node, 4, AArch64::LD4i16_POST);
  4826. return;
  4827. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  4828. VT == MVT::v2f32) {
  4829. SelectPostLoadLane(Node, 4, AArch64::LD4i32_POST);
  4830. return;
  4831. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  4832. VT == MVT::v1f64) {
  4833. SelectPostLoadLane(Node, 4, AArch64::LD4i64_POST);
  4834. return;
  4835. }
  4836. break;
  4837. }
  4838. case AArch64ISD::ST2post: {
  4839. VT = Node->getOperand(1).getValueType();
  4840. if (VT == MVT::v8i8) {
  4841. SelectPostStore(Node, 2, AArch64::ST2Twov8b_POST);
  4842. return;
  4843. } else if (VT == MVT::v16i8) {
  4844. SelectPostStore(Node, 2, AArch64::ST2Twov16b_POST);
  4845. return;
  4846. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4847. SelectPostStore(Node, 2, AArch64::ST2Twov4h_POST);
  4848. return;
  4849. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4850. SelectPostStore(Node, 2, AArch64::ST2Twov8h_POST);
  4851. return;
  4852. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4853. SelectPostStore(Node, 2, AArch64::ST2Twov2s_POST);
  4854. return;
  4855. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4856. SelectPostStore(Node, 2, AArch64::ST2Twov4s_POST);
  4857. return;
  4858. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4859. SelectPostStore(Node, 2, AArch64::ST2Twov2d_POST);
  4860. return;
  4861. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4862. SelectPostStore(Node, 2, AArch64::ST1Twov1d_POST);
  4863. return;
  4864. }
  4865. break;
  4866. }
  4867. case AArch64ISD::ST3post: {
  4868. VT = Node->getOperand(1).getValueType();
  4869. if (VT == MVT::v8i8) {
  4870. SelectPostStore(Node, 3, AArch64::ST3Threev8b_POST);
  4871. return;
  4872. } else if (VT == MVT::v16i8) {
  4873. SelectPostStore(Node, 3, AArch64::ST3Threev16b_POST);
  4874. return;
  4875. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4876. SelectPostStore(Node, 3, AArch64::ST3Threev4h_POST);
  4877. return;
  4878. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4879. SelectPostStore(Node, 3, AArch64::ST3Threev8h_POST);
  4880. return;
  4881. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4882. SelectPostStore(Node, 3, AArch64::ST3Threev2s_POST);
  4883. return;
  4884. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4885. SelectPostStore(Node, 3, AArch64::ST3Threev4s_POST);
  4886. return;
  4887. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4888. SelectPostStore(Node, 3, AArch64::ST3Threev2d_POST);
  4889. return;
  4890. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4891. SelectPostStore(Node, 3, AArch64::ST1Threev1d_POST);
  4892. return;
  4893. }
  4894. break;
  4895. }
  4896. case AArch64ISD::ST4post: {
  4897. VT = Node->getOperand(1).getValueType();
  4898. if (VT == MVT::v8i8) {
  4899. SelectPostStore(Node, 4, AArch64::ST4Fourv8b_POST);
  4900. return;
  4901. } else if (VT == MVT::v16i8) {
  4902. SelectPostStore(Node, 4, AArch64::ST4Fourv16b_POST);
  4903. return;
  4904. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4905. SelectPostStore(Node, 4, AArch64::ST4Fourv4h_POST);
  4906. return;
  4907. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4908. SelectPostStore(Node, 4, AArch64::ST4Fourv8h_POST);
  4909. return;
  4910. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4911. SelectPostStore(Node, 4, AArch64::ST4Fourv2s_POST);
  4912. return;
  4913. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4914. SelectPostStore(Node, 4, AArch64::ST4Fourv4s_POST);
  4915. return;
  4916. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4917. SelectPostStore(Node, 4, AArch64::ST4Fourv2d_POST);
  4918. return;
  4919. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4920. SelectPostStore(Node, 4, AArch64::ST1Fourv1d_POST);
  4921. return;
  4922. }
  4923. break;
  4924. }
  4925. case AArch64ISD::ST1x2post: {
  4926. VT = Node->getOperand(1).getValueType();
  4927. if (VT == MVT::v8i8) {
  4928. SelectPostStore(Node, 2, AArch64::ST1Twov8b_POST);
  4929. return;
  4930. } else if (VT == MVT::v16i8) {
  4931. SelectPostStore(Node, 2, AArch64::ST1Twov16b_POST);
  4932. return;
  4933. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4934. SelectPostStore(Node, 2, AArch64::ST1Twov4h_POST);
  4935. return;
  4936. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4937. SelectPostStore(Node, 2, AArch64::ST1Twov8h_POST);
  4938. return;
  4939. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4940. SelectPostStore(Node, 2, AArch64::ST1Twov2s_POST);
  4941. return;
  4942. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4943. SelectPostStore(Node, 2, AArch64::ST1Twov4s_POST);
  4944. return;
  4945. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4946. SelectPostStore(Node, 2, AArch64::ST1Twov1d_POST);
  4947. return;
  4948. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4949. SelectPostStore(Node, 2, AArch64::ST1Twov2d_POST);
  4950. return;
  4951. }
  4952. break;
  4953. }
  4954. case AArch64ISD::ST1x3post: {
  4955. VT = Node->getOperand(1).getValueType();
  4956. if (VT == MVT::v8i8) {
  4957. SelectPostStore(Node, 3, AArch64::ST1Threev8b_POST);
  4958. return;
  4959. } else if (VT == MVT::v16i8) {
  4960. SelectPostStore(Node, 3, AArch64::ST1Threev16b_POST);
  4961. return;
  4962. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4963. SelectPostStore(Node, 3, AArch64::ST1Threev4h_POST);
  4964. return;
  4965. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16 ) {
  4966. SelectPostStore(Node, 3, AArch64::ST1Threev8h_POST);
  4967. return;
  4968. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4969. SelectPostStore(Node, 3, AArch64::ST1Threev2s_POST);
  4970. return;
  4971. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  4972. SelectPostStore(Node, 3, AArch64::ST1Threev4s_POST);
  4973. return;
  4974. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  4975. SelectPostStore(Node, 3, AArch64::ST1Threev1d_POST);
  4976. return;
  4977. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  4978. SelectPostStore(Node, 3, AArch64::ST1Threev2d_POST);
  4979. return;
  4980. }
  4981. break;
  4982. }
  4983. case AArch64ISD::ST1x4post: {
  4984. VT = Node->getOperand(1).getValueType();
  4985. if (VT == MVT::v8i8) {
  4986. SelectPostStore(Node, 4, AArch64::ST1Fourv8b_POST);
  4987. return;
  4988. } else if (VT == MVT::v16i8) {
  4989. SelectPostStore(Node, 4, AArch64::ST1Fourv16b_POST);
  4990. return;
  4991. } else if (VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16) {
  4992. SelectPostStore(Node, 4, AArch64::ST1Fourv4h_POST);
  4993. return;
  4994. } else if (VT == MVT::v8i16 || VT == MVT::v8f16 || VT == MVT::v8bf16) {
  4995. SelectPostStore(Node, 4, AArch64::ST1Fourv8h_POST);
  4996. return;
  4997. } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
  4998. SelectPostStore(Node, 4, AArch64::ST1Fourv2s_POST);
  4999. return;
  5000. } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
  5001. SelectPostStore(Node, 4, AArch64::ST1Fourv4s_POST);
  5002. return;
  5003. } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
  5004. SelectPostStore(Node, 4, AArch64::ST1Fourv1d_POST);
  5005. return;
  5006. } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
  5007. SelectPostStore(Node, 4, AArch64::ST1Fourv2d_POST);
  5008. return;
  5009. }
  5010. break;
  5011. }
  5012. case AArch64ISD::ST2LANEpost: {
  5013. VT = Node->getOperand(1).getValueType();
  5014. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  5015. SelectPostStoreLane(Node, 2, AArch64::ST2i8_POST);
  5016. return;
  5017. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  5018. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  5019. SelectPostStoreLane(Node, 2, AArch64::ST2i16_POST);
  5020. return;
  5021. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  5022. VT == MVT::v2f32) {
  5023. SelectPostStoreLane(Node, 2, AArch64::ST2i32_POST);
  5024. return;
  5025. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  5026. VT == MVT::v1f64) {
  5027. SelectPostStoreLane(Node, 2, AArch64::ST2i64_POST);
  5028. return;
  5029. }
  5030. break;
  5031. }
  5032. case AArch64ISD::ST3LANEpost: {
  5033. VT = Node->getOperand(1).getValueType();
  5034. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  5035. SelectPostStoreLane(Node, 3, AArch64::ST3i8_POST);
  5036. return;
  5037. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  5038. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  5039. SelectPostStoreLane(Node, 3, AArch64::ST3i16_POST);
  5040. return;
  5041. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  5042. VT == MVT::v2f32) {
  5043. SelectPostStoreLane(Node, 3, AArch64::ST3i32_POST);
  5044. return;
  5045. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  5046. VT == MVT::v1f64) {
  5047. SelectPostStoreLane(Node, 3, AArch64::ST3i64_POST);
  5048. return;
  5049. }
  5050. break;
  5051. }
  5052. case AArch64ISD::ST4LANEpost: {
  5053. VT = Node->getOperand(1).getValueType();
  5054. if (VT == MVT::v16i8 || VT == MVT::v8i8) {
  5055. SelectPostStoreLane(Node, 4, AArch64::ST4i8_POST);
  5056. return;
  5057. } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
  5058. VT == MVT::v8f16 || VT == MVT::v4bf16 || VT == MVT::v8bf16) {
  5059. SelectPostStoreLane(Node, 4, AArch64::ST4i16_POST);
  5060. return;
  5061. } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
  5062. VT == MVT::v2f32) {
  5063. SelectPostStoreLane(Node, 4, AArch64::ST4i32_POST);
  5064. return;
  5065. } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
  5066. VT == MVT::v1f64) {
  5067. SelectPostStoreLane(Node, 4, AArch64::ST4i64_POST);
  5068. return;
  5069. }
  5070. break;
  5071. }
  5072. case AArch64ISD::SVE_LD2_MERGE_ZERO: {
  5073. if (VT == MVT::nxv16i8) {
  5074. SelectPredicatedLoad(Node, 2, 0, AArch64::LD2B_IMM, AArch64::LD2B);
  5075. return;
  5076. } else if (VT == MVT::nxv8i16 || VT == MVT::nxv8f16 ||
  5077. VT == MVT::nxv8bf16) {
  5078. SelectPredicatedLoad(Node, 2, 1, AArch64::LD2H_IMM, AArch64::LD2H);
  5079. return;
  5080. } else if (VT == MVT::nxv4i32 || VT == MVT::nxv4f32) {
  5081. SelectPredicatedLoad(Node, 2, 2, AArch64::LD2W_IMM, AArch64::LD2W);
  5082. return;
  5083. } else if (VT == MVT::nxv2i64 || VT == MVT::nxv2f64) {
  5084. SelectPredicatedLoad(Node, 2, 3, AArch64::LD2D_IMM, AArch64::LD2D);
  5085. return;
  5086. }
  5087. break;
  5088. }
  5089. case AArch64ISD::SVE_LD3_MERGE_ZERO: {
  5090. if (VT == MVT::nxv16i8) {
  5091. SelectPredicatedLoad(Node, 3, 0, AArch64::LD3B_IMM, AArch64::LD3B);
  5092. return;
  5093. } else if (VT == MVT::nxv8i16 || VT == MVT::nxv8f16 ||
  5094. VT == MVT::nxv8bf16) {
  5095. SelectPredicatedLoad(Node, 3, 1, AArch64::LD3H_IMM, AArch64::LD3H);
  5096. return;
  5097. } else if (VT == MVT::nxv4i32 || VT == MVT::nxv4f32) {
  5098. SelectPredicatedLoad(Node, 3, 2, AArch64::LD3W_IMM, AArch64::LD3W);
  5099. return;
  5100. } else if (VT == MVT::nxv2i64 || VT == MVT::nxv2f64) {
  5101. SelectPredicatedLoad(Node, 3, 3, AArch64::LD3D_IMM, AArch64::LD3D);
  5102. return;
  5103. }
  5104. break;
  5105. }
  5106. case AArch64ISD::SVE_LD4_MERGE_ZERO: {
  5107. if (VT == MVT::nxv16i8) {
  5108. SelectPredicatedLoad(Node, 4, 0, AArch64::LD4B_IMM, AArch64::LD4B);
  5109. return;
  5110. } else if (VT == MVT::nxv8i16 || VT == MVT::nxv8f16 ||
  5111. VT == MVT::nxv8bf16) {
  5112. SelectPredicatedLoad(Node, 4, 1, AArch64::LD4H_IMM, AArch64::LD4H);
  5113. return;
  5114. } else if (VT == MVT::nxv4i32 || VT == MVT::nxv4f32) {
  5115. SelectPredicatedLoad(Node, 4, 2, AArch64::LD4W_IMM, AArch64::LD4W);
  5116. return;
  5117. } else if (VT == MVT::nxv2i64 || VT == MVT::nxv2f64) {
  5118. SelectPredicatedLoad(Node, 4, 3, AArch64::LD4D_IMM, AArch64::LD4D);
  5119. return;
  5120. }
  5121. break;
  5122. }
  5123. }
  5124. // Select the default instruction
  5125. SelectCode(Node);
  5126. }
  5127. /// createAArch64ISelDag - This pass converts a legalized DAG into a
  5128. /// AArch64-specific DAG, ready for instruction scheduling.
  5129. FunctionPass *llvm::createAArch64ISelDag(AArch64TargetMachine &TM,
  5130. CodeGenOpt::Level OptLevel) {
  5131. return new AArch64DAGToDAGISel(TM, OptLevel);
  5132. }
  5133. /// When \p PredVT is a scalable vector predicate in the form
  5134. /// MVT::nx<M>xi1, it builds the correspondent scalable vector of
  5135. /// integers MVT::nx<M>xi<bits> s.t. M x bits = 128. When targeting
  5136. /// structured vectors (NumVec >1), the output data type is
  5137. /// MVT::nx<M*NumVec>xi<bits> s.t. M x bits = 128. If the input
  5138. /// PredVT is not in the form MVT::nx<M>xi1, it returns an invalid
  5139. /// EVT.
  5140. static EVT getPackedVectorTypeFromPredicateType(LLVMContext &Ctx, EVT PredVT,
  5141. unsigned NumVec) {
  5142. assert(NumVec > 0 && NumVec < 5 && "Invalid number of vectors.");
  5143. if (!PredVT.isScalableVector() || PredVT.getVectorElementType() != MVT::i1)
  5144. return EVT();
  5145. if (PredVT != MVT::nxv16i1 && PredVT != MVT::nxv8i1 &&
  5146. PredVT != MVT::nxv4i1 && PredVT != MVT::nxv2i1)
  5147. return EVT();
  5148. ElementCount EC = PredVT.getVectorElementCount();
  5149. EVT ScalarVT =
  5150. EVT::getIntegerVT(Ctx, AArch64::SVEBitsPerBlock / EC.getKnownMinValue());
  5151. EVT MemVT = EVT::getVectorVT(Ctx, ScalarVT, EC * NumVec);
  5152. return MemVT;
  5153. }
  5154. /// Return the EVT of the data associated to a memory operation in \p
  5155. /// Root. If such EVT cannot be retrived, it returns an invalid EVT.
  5156. static EVT getMemVTFromNode(LLVMContext &Ctx, SDNode *Root) {
  5157. if (isa<MemSDNode>(Root))
  5158. return cast<MemSDNode>(Root)->getMemoryVT();
  5159. if (isa<MemIntrinsicSDNode>(Root))
  5160. return cast<MemIntrinsicSDNode>(Root)->getMemoryVT();
  5161. const unsigned Opcode = Root->getOpcode();
  5162. // For custom ISD nodes, we have to look at them individually to extract the
  5163. // type of the data moved to/from memory.
  5164. switch (Opcode) {
  5165. case AArch64ISD::LD1_MERGE_ZERO:
  5166. case AArch64ISD::LD1S_MERGE_ZERO:
  5167. case AArch64ISD::LDNF1_MERGE_ZERO:
  5168. case AArch64ISD::LDNF1S_MERGE_ZERO:
  5169. return cast<VTSDNode>(Root->getOperand(3))->getVT();
  5170. case AArch64ISD::ST1_PRED:
  5171. return cast<VTSDNode>(Root->getOperand(4))->getVT();
  5172. case AArch64ISD::SVE_LD2_MERGE_ZERO:
  5173. return getPackedVectorTypeFromPredicateType(
  5174. Ctx, Root->getOperand(1)->getValueType(0), /*NumVec=*/2);
  5175. case AArch64ISD::SVE_LD3_MERGE_ZERO:
  5176. return getPackedVectorTypeFromPredicateType(
  5177. Ctx, Root->getOperand(1)->getValueType(0), /*NumVec=*/3);
  5178. case AArch64ISD::SVE_LD4_MERGE_ZERO:
  5179. return getPackedVectorTypeFromPredicateType(
  5180. Ctx, Root->getOperand(1)->getValueType(0), /*NumVec=*/4);
  5181. default:
  5182. break;
  5183. }
  5184. if (Opcode != ISD::INTRINSIC_VOID && Opcode != ISD::INTRINSIC_W_CHAIN)
  5185. return EVT();
  5186. switch (cast<ConstantSDNode>(Root->getOperand(1))->getZExtValue()) {
  5187. default:
  5188. return EVT();
  5189. case Intrinsic::aarch64_sme_ldr:
  5190. case Intrinsic::aarch64_sme_str:
  5191. return MVT::nxv16i8;
  5192. case Intrinsic::aarch64_sve_prf:
  5193. // We are using an SVE prefetch intrinsic. Type must be inferred from the
  5194. // width of the predicate.
  5195. return getPackedVectorTypeFromPredicateType(
  5196. Ctx, Root->getOperand(2)->getValueType(0), /*NumVec=*/1);
  5197. case Intrinsic::aarch64_sve_ld2_sret:
  5198. return getPackedVectorTypeFromPredicateType(
  5199. Ctx, Root->getOperand(2)->getValueType(0), /*NumVec=*/2);
  5200. case Intrinsic::aarch64_sve_ld3_sret:
  5201. return getPackedVectorTypeFromPredicateType(
  5202. Ctx, Root->getOperand(2)->getValueType(0), /*NumVec=*/3);
  5203. case Intrinsic::aarch64_sve_ld4_sret:
  5204. return getPackedVectorTypeFromPredicateType(
  5205. Ctx, Root->getOperand(2)->getValueType(0), /*NumVec=*/4);
  5206. }
  5207. }
  5208. /// SelectAddrModeIndexedSVE - Attempt selection of the addressing mode:
  5209. /// Base + OffImm * sizeof(MemVT) for Min >= OffImm <= Max
  5210. /// where Root is the memory access using N for its address.
  5211. template <int64_t Min, int64_t Max>
  5212. bool AArch64DAGToDAGISel::SelectAddrModeIndexedSVE(SDNode *Root, SDValue N,
  5213. SDValue &Base,
  5214. SDValue &OffImm) {
  5215. const EVT MemVT = getMemVTFromNode(*(CurDAG->getContext()), Root);
  5216. const DataLayout &DL = CurDAG->getDataLayout();
  5217. const MachineFrameInfo &MFI = MF->getFrameInfo();
  5218. if (N.getOpcode() == ISD::FrameIndex) {
  5219. int FI = cast<FrameIndexSDNode>(N)->getIndex();
  5220. // We can only encode VL scaled offsets, so only fold in frame indexes
  5221. // referencing SVE objects.
  5222. if (FI == 0 || MFI.getStackID(FI) == TargetStackID::ScalableVector) {
  5223. Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
  5224. OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i64);
  5225. return true;
  5226. }
  5227. return false;
  5228. }
  5229. if (MemVT == EVT())
  5230. return false;
  5231. if (N.getOpcode() != ISD::ADD)
  5232. return false;
  5233. SDValue VScale = N.getOperand(1);
  5234. if (VScale.getOpcode() != ISD::VSCALE)
  5235. return false;
  5236. TypeSize TS = MemVT.getSizeInBits();
  5237. int64_t MemWidthBytes = static_cast<int64_t>(TS.getKnownMinValue()) / 8;
  5238. int64_t MulImm = cast<ConstantSDNode>(VScale.getOperand(0))->getSExtValue();
  5239. if ((MulImm % MemWidthBytes) != 0)
  5240. return false;
  5241. int64_t Offset = MulImm / MemWidthBytes;
  5242. if (Offset < Min || Offset > Max)
  5243. return false;
  5244. Base = N.getOperand(0);
  5245. if (Base.getOpcode() == ISD::FrameIndex) {
  5246. int FI = cast<FrameIndexSDNode>(Base)->getIndex();
  5247. // We can only encode VL scaled offsets, so only fold in frame indexes
  5248. // referencing SVE objects.
  5249. if (FI == 0 || MFI.getStackID(FI) == TargetStackID::ScalableVector)
  5250. Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
  5251. }
  5252. OffImm = CurDAG->getTargetConstant(Offset, SDLoc(N), MVT::i64);
  5253. return true;
  5254. }
  5255. /// Select register plus register addressing mode for SVE, with scaled
  5256. /// offset.
  5257. bool AArch64DAGToDAGISel::SelectSVERegRegAddrMode(SDValue N, unsigned Scale,
  5258. SDValue &Base,
  5259. SDValue &Offset) {
  5260. if (N.getOpcode() != ISD::ADD)
  5261. return false;
  5262. // Process an ADD node.
  5263. const SDValue LHS = N.getOperand(0);
  5264. const SDValue RHS = N.getOperand(1);
  5265. // 8 bit data does not come with the SHL node, so it is treated
  5266. // separately.
  5267. if (Scale == 0) {
  5268. Base = LHS;
  5269. Offset = RHS;
  5270. return true;
  5271. }
  5272. if (auto C = dyn_cast<ConstantSDNode>(RHS)) {
  5273. int64_t ImmOff = C->getSExtValue();
  5274. unsigned Size = 1 << Scale;
  5275. // To use the reg+reg addressing mode, the immediate must be a multiple of
  5276. // the vector element's byte size.
  5277. if (ImmOff % Size)
  5278. return false;
  5279. SDLoc DL(N);
  5280. Base = LHS;
  5281. Offset = CurDAG->getTargetConstant(ImmOff >> Scale, DL, MVT::i64);
  5282. SDValue Ops[] = {Offset};
  5283. SDNode *MI = CurDAG->getMachineNode(AArch64::MOVi64imm, DL, MVT::i64, Ops);
  5284. Offset = SDValue(MI, 0);
  5285. return true;
  5286. }
  5287. // Check if the RHS is a shift node with a constant.
  5288. if (RHS.getOpcode() != ISD::SHL)
  5289. return false;
  5290. const SDValue ShiftRHS = RHS.getOperand(1);
  5291. if (auto *C = dyn_cast<ConstantSDNode>(ShiftRHS))
  5292. if (C->getZExtValue() == Scale) {
  5293. Base = LHS;
  5294. Offset = RHS.getOperand(0);
  5295. return true;
  5296. }
  5297. return false;
  5298. }
  5299. bool AArch64DAGToDAGISel::SelectAllActivePredicate(SDValue N) {
  5300. const AArch64TargetLowering *TLI =
  5301. static_cast<const AArch64TargetLowering *>(getTargetLowering());
  5302. return TLI->isAllActivePredicate(*CurDAG, N);
  5303. }
  5304. bool AArch64DAGToDAGISel::SelectSMETileSlice(SDValue N, unsigned MaxSize,
  5305. SDValue &Base, SDValue &Offset,
  5306. unsigned Scale) {
  5307. if (N.getOpcode() != ISD::ADD) {
  5308. Base = N;
  5309. Offset = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i64);
  5310. return true;
  5311. }
  5312. // Process an ADD node.
  5313. const SDValue LHS = N.getOperand(0);
  5314. const SDValue RHS = N.getOperand(1);
  5315. if (auto C = dyn_cast<ConstantSDNode>(RHS)) {
  5316. int64_t ImmOff = C->getSExtValue();
  5317. if ((ImmOff < 0 || ImmOff > MaxSize) || (ImmOff % Scale != 0))
  5318. return false;
  5319. Base = LHS;
  5320. Offset = CurDAG->getTargetConstant(ImmOff / Scale, SDLoc(N), MVT::i64);
  5321. return true;
  5322. }
  5323. return false;
  5324. }