SelectionDAG.cpp 350 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621
  1. //===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
  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 implements the SelectionDAG class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/SelectionDAG.h"
  13. #include "SDNodeDbgValue.h"
  14. #include "llvm/ADT/APFloat.h"
  15. #include "llvm/ADT/APInt.h"
  16. #include "llvm/ADT/APSInt.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/BitVector.h"
  19. #include "llvm/ADT/FoldingSet.h"
  20. #include "llvm/ADT/None.h"
  21. #include "llvm/ADT/STLExtras.h"
  22. #include "llvm/ADT/SmallPtrSet.h"
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/ADT/Triple.h"
  25. #include "llvm/ADT/Twine.h"
  26. #include "llvm/Analysis/ValueTracking.h"
  27. #include "llvm/CodeGen/ISDOpcodes.h"
  28. #include "llvm/CodeGen/MachineBasicBlock.h"
  29. #include "llvm/CodeGen/MachineConstantPool.h"
  30. #include "llvm/CodeGen/MachineFrameInfo.h"
  31. #include "llvm/CodeGen/MachineFunction.h"
  32. #include "llvm/CodeGen/MachineMemOperand.h"
  33. #include "llvm/CodeGen/RuntimeLibcalls.h"
  34. #include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
  35. #include "llvm/CodeGen/SelectionDAGNodes.h"
  36. #include "llvm/CodeGen/SelectionDAGTargetInfo.h"
  37. #include "llvm/CodeGen/TargetLowering.h"
  38. #include "llvm/CodeGen/TargetRegisterInfo.h"
  39. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  40. #include "llvm/CodeGen/ValueTypes.h"
  41. #include "llvm/IR/Constant.h"
  42. #include "llvm/IR/Constants.h"
  43. #include "llvm/IR/DataLayout.h"
  44. #include "llvm/IR/DebugInfoMetadata.h"
  45. #include "llvm/IR/DebugLoc.h"
  46. #include "llvm/IR/DerivedTypes.h"
  47. #include "llvm/IR/Function.h"
  48. #include "llvm/IR/GlobalValue.h"
  49. #include "llvm/IR/Metadata.h"
  50. #include "llvm/IR/Type.h"
  51. #include "llvm/IR/Value.h"
  52. #include "llvm/Support/Casting.h"
  53. #include "llvm/Support/CodeGen.h"
  54. #include "llvm/Support/Compiler.h"
  55. #include "llvm/Support/Debug.h"
  56. #include "llvm/Support/ErrorHandling.h"
  57. #include "llvm/Support/KnownBits.h"
  58. #include "llvm/Support/MachineValueType.h"
  59. #include "llvm/Support/ManagedStatic.h"
  60. #include "llvm/Support/MathExtras.h"
  61. #include "llvm/Support/Mutex.h"
  62. #include "llvm/Support/raw_ostream.h"
  63. #include "llvm/Target/TargetMachine.h"
  64. #include "llvm/Target/TargetOptions.h"
  65. #include <algorithm>
  66. #include <cassert>
  67. #include <cstdint>
  68. #include <cstdlib>
  69. #include <limits>
  70. #include <set>
  71. #include <string>
  72. #include <utility>
  73. #include <vector>
  74. using namespace llvm;
  75. /// makeVTList - Return an instance of the SDVTList struct initialized with the
  76. /// specified members.
  77. static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
  78. SDVTList Res = {VTs, NumVTs};
  79. return Res;
  80. }
  81. // Default null implementations of the callbacks.
  82. void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
  83. void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
  84. void SelectionDAG::DAGUpdateListener::NodeInserted(SDNode *) {}
  85. void SelectionDAG::DAGNodeDeletedListener::anchor() {}
  86. #define DEBUG_TYPE "selectiondag"
  87. static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
  88. cl::Hidden, cl::init(true),
  89. cl::desc("Gang up loads and stores generated by inlining of memcpy"));
  90. static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
  91. cl::desc("Number limit for gluing ld/st of memcpy."),
  92. cl::Hidden, cl::init(0));
  93. static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G) {
  94. LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
  95. }
  96. //===----------------------------------------------------------------------===//
  97. // ConstantFPSDNode Class
  98. //===----------------------------------------------------------------------===//
  99. /// isExactlyValue - We don't rely on operator== working on double values, as
  100. /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
  101. /// As such, this method can be used to do an exact bit-for-bit comparison of
  102. /// two floating point values.
  103. bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
  104. return getValueAPF().bitwiseIsEqual(V);
  105. }
  106. bool ConstantFPSDNode::isValueValidForType(EVT VT,
  107. const APFloat& Val) {
  108. assert(VT.isFloatingPoint() && "Can only convert between FP types");
  109. // convert modifies in place, so make a copy.
  110. APFloat Val2 = APFloat(Val);
  111. bool losesInfo;
  112. (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
  113. APFloat::rmNearestTiesToEven,
  114. &losesInfo);
  115. return !losesInfo;
  116. }
  117. //===----------------------------------------------------------------------===//
  118. // ISD Namespace
  119. //===----------------------------------------------------------------------===//
  120. bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
  121. auto *BV = dyn_cast<BuildVectorSDNode>(N);
  122. if (!BV)
  123. return false;
  124. APInt SplatUndef;
  125. unsigned SplatBitSize;
  126. bool HasUndefs;
  127. unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
  128. return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
  129. EltSize) &&
  130. EltSize == SplatBitSize;
  131. }
  132. // FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
  133. // specializations of the more general isConstantSplatVector()?
  134. bool ISD::isBuildVectorAllOnes(const SDNode *N) {
  135. // Look through a bit convert.
  136. while (N->getOpcode() == ISD::BITCAST)
  137. N = N->getOperand(0).getNode();
  138. if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
  139. unsigned i = 0, e = N->getNumOperands();
  140. // Skip over all of the undef values.
  141. while (i != e && N->getOperand(i).isUndef())
  142. ++i;
  143. // Do not accept an all-undef vector.
  144. if (i == e) return false;
  145. // Do not accept build_vectors that aren't all constants or which have non-~0
  146. // elements. We have to be a bit careful here, as the type of the constant
  147. // may not be the same as the type of the vector elements due to type
  148. // legalization (the elements are promoted to a legal type for the target and
  149. // a vector of a type may be legal when the base element type is not).
  150. // We only want to check enough bits to cover the vector elements, because
  151. // we care if the resultant vector is all ones, not whether the individual
  152. // constants are.
  153. SDValue NotZero = N->getOperand(i);
  154. unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
  155. if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
  156. if (CN->getAPIntValue().countTrailingOnes() < EltSize)
  157. return false;
  158. } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
  159. if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
  160. return false;
  161. } else
  162. return false;
  163. // Okay, we have at least one ~0 value, check to see if the rest match or are
  164. // undefs. Even with the above element type twiddling, this should be OK, as
  165. // the same type legalization should have applied to all the elements.
  166. for (++i; i != e; ++i)
  167. if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
  168. return false;
  169. return true;
  170. }
  171. bool ISD::isBuildVectorAllZeros(const SDNode *N) {
  172. // Look through a bit convert.
  173. while (N->getOpcode() == ISD::BITCAST)
  174. N = N->getOperand(0).getNode();
  175. if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
  176. bool IsAllUndef = true;
  177. for (const SDValue &Op : N->op_values()) {
  178. if (Op.isUndef())
  179. continue;
  180. IsAllUndef = false;
  181. // Do not accept build_vectors that aren't all constants or which have non-0
  182. // elements. We have to be a bit careful here, as the type of the constant
  183. // may not be the same as the type of the vector elements due to type
  184. // legalization (the elements are promoted to a legal type for the target
  185. // and a vector of a type may be legal when the base element type is not).
  186. // We only want to check enough bits to cover the vector elements, because
  187. // we care if the resultant vector is all zeros, not whether the individual
  188. // constants are.
  189. unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
  190. if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
  191. if (CN->getAPIntValue().countTrailingZeros() < EltSize)
  192. return false;
  193. } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
  194. if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
  195. return false;
  196. } else
  197. return false;
  198. }
  199. // Do not accept an all-undef vector.
  200. if (IsAllUndef)
  201. return false;
  202. return true;
  203. }
  204. bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
  205. if (N->getOpcode() != ISD::BUILD_VECTOR)
  206. return false;
  207. for (const SDValue &Op : N->op_values()) {
  208. if (Op.isUndef())
  209. continue;
  210. if (!isa<ConstantSDNode>(Op))
  211. return false;
  212. }
  213. return true;
  214. }
  215. bool ISD::isBuildVectorOfConstantFPSDNodes(const SDNode *N) {
  216. if (N->getOpcode() != ISD::BUILD_VECTOR)
  217. return false;
  218. for (const SDValue &Op : N->op_values()) {
  219. if (Op.isUndef())
  220. continue;
  221. if (!isa<ConstantFPSDNode>(Op))
  222. return false;
  223. }
  224. return true;
  225. }
  226. bool ISD::allOperandsUndef(const SDNode *N) {
  227. // Return false if the node has no operands.
  228. // This is "logically inconsistent" with the definition of "all" but
  229. // is probably the desired behavior.
  230. if (N->getNumOperands() == 0)
  231. return false;
  232. return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
  233. }
  234. bool ISD::matchUnaryPredicate(SDValue Op,
  235. std::function<bool(ConstantSDNode *)> Match,
  236. bool AllowUndefs) {
  237. // FIXME: Add support for scalar UNDEF cases?
  238. if (auto *Cst = dyn_cast<ConstantSDNode>(Op))
  239. return Match(Cst);
  240. // FIXME: Add support for vector UNDEF cases?
  241. if (ISD::BUILD_VECTOR != Op.getOpcode())
  242. return false;
  243. EVT SVT = Op.getValueType().getScalarType();
  244. for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
  245. if (AllowUndefs && Op.getOperand(i).isUndef()) {
  246. if (!Match(nullptr))
  247. return false;
  248. continue;
  249. }
  250. auto *Cst = dyn_cast<ConstantSDNode>(Op.getOperand(i));
  251. if (!Cst || Cst->getValueType(0) != SVT || !Match(Cst))
  252. return false;
  253. }
  254. return true;
  255. }
  256. bool ISD::matchBinaryPredicate(
  257. SDValue LHS, SDValue RHS,
  258. std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
  259. bool AllowUndefs, bool AllowTypeMismatch) {
  260. if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
  261. return false;
  262. // TODO: Add support for scalar UNDEF cases?
  263. if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
  264. if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
  265. return Match(LHSCst, RHSCst);
  266. // TODO: Add support for vector UNDEF cases?
  267. if (ISD::BUILD_VECTOR != LHS.getOpcode() ||
  268. ISD::BUILD_VECTOR != RHS.getOpcode())
  269. return false;
  270. EVT SVT = LHS.getValueType().getScalarType();
  271. for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
  272. SDValue LHSOp = LHS.getOperand(i);
  273. SDValue RHSOp = RHS.getOperand(i);
  274. bool LHSUndef = AllowUndefs && LHSOp.isUndef();
  275. bool RHSUndef = AllowUndefs && RHSOp.isUndef();
  276. auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
  277. auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
  278. if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
  279. return false;
  280. if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
  281. LHSOp.getValueType() != RHSOp.getValueType()))
  282. return false;
  283. if (!Match(LHSCst, RHSCst))
  284. return false;
  285. }
  286. return true;
  287. }
  288. ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) {
  289. switch (ExtType) {
  290. case ISD::EXTLOAD:
  291. return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
  292. case ISD::SEXTLOAD:
  293. return ISD::SIGN_EXTEND;
  294. case ISD::ZEXTLOAD:
  295. return ISD::ZERO_EXTEND;
  296. default:
  297. break;
  298. }
  299. llvm_unreachable("Invalid LoadExtType");
  300. }
  301. ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
  302. // To perform this operation, we just need to swap the L and G bits of the
  303. // operation.
  304. unsigned OldL = (Operation >> 2) & 1;
  305. unsigned OldG = (Operation >> 1) & 1;
  306. return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
  307. (OldL << 1) | // New G bit
  308. (OldG << 2)); // New L bit.
  309. }
  310. ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
  311. unsigned Operation = Op;
  312. if (isInteger)
  313. Operation ^= 7; // Flip L, G, E bits, but not U.
  314. else
  315. Operation ^= 15; // Flip all of the condition bits.
  316. if (Operation > ISD::SETTRUE2)
  317. Operation &= ~8; // Don't let N and U bits get set.
  318. return ISD::CondCode(Operation);
  319. }
  320. /// For an integer comparison, return 1 if the comparison is a signed operation
  321. /// and 2 if the result is an unsigned comparison. Return zero if the operation
  322. /// does not depend on the sign of the input (setne and seteq).
  323. static int isSignedOp(ISD::CondCode Opcode) {
  324. switch (Opcode) {
  325. default: llvm_unreachable("Illegal integer setcc operation!");
  326. case ISD::SETEQ:
  327. case ISD::SETNE: return 0;
  328. case ISD::SETLT:
  329. case ISD::SETLE:
  330. case ISD::SETGT:
  331. case ISD::SETGE: return 1;
  332. case ISD::SETULT:
  333. case ISD::SETULE:
  334. case ISD::SETUGT:
  335. case ISD::SETUGE: return 2;
  336. }
  337. }
  338. ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
  339. bool IsInteger) {
  340. if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
  341. // Cannot fold a signed integer setcc with an unsigned integer setcc.
  342. return ISD::SETCC_INVALID;
  343. unsigned Op = Op1 | Op2; // Combine all of the condition bits.
  344. // If the N and U bits get set, then the resultant comparison DOES suddenly
  345. // care about orderedness, and it is true when ordered.
  346. if (Op > ISD::SETTRUE2)
  347. Op &= ~16; // Clear the U bit if the N bit is set.
  348. // Canonicalize illegal integer setcc's.
  349. if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
  350. Op = ISD::SETNE;
  351. return ISD::CondCode(Op);
  352. }
  353. ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
  354. bool IsInteger) {
  355. if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
  356. // Cannot fold a signed setcc with an unsigned setcc.
  357. return ISD::SETCC_INVALID;
  358. // Combine all of the condition bits.
  359. ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
  360. // Canonicalize illegal integer setcc's.
  361. if (IsInteger) {
  362. switch (Result) {
  363. default: break;
  364. case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
  365. case ISD::SETOEQ: // SETEQ & SETU[LG]E
  366. case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
  367. case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
  368. case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
  369. }
  370. }
  371. return Result;
  372. }
  373. //===----------------------------------------------------------------------===//
  374. // SDNode Profile Support
  375. //===----------------------------------------------------------------------===//
  376. /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
  377. static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
  378. ID.AddInteger(OpC);
  379. }
  380. /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
  381. /// solely with their pointer.
  382. static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
  383. ID.AddPointer(VTList.VTs);
  384. }
  385. /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
  386. static void AddNodeIDOperands(FoldingSetNodeID &ID,
  387. ArrayRef<SDValue> Ops) {
  388. for (auto& Op : Ops) {
  389. ID.AddPointer(Op.getNode());
  390. ID.AddInteger(Op.getResNo());
  391. }
  392. }
  393. /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
  394. static void AddNodeIDOperands(FoldingSetNodeID &ID,
  395. ArrayRef<SDUse> Ops) {
  396. for (auto& Op : Ops) {
  397. ID.AddPointer(Op.getNode());
  398. ID.AddInteger(Op.getResNo());
  399. }
  400. }
  401. static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
  402. SDVTList VTList, ArrayRef<SDValue> OpList) {
  403. AddNodeIDOpcode(ID, OpC);
  404. AddNodeIDValueTypes(ID, VTList);
  405. AddNodeIDOperands(ID, OpList);
  406. }
  407. /// If this is an SDNode with special info, add this info to the NodeID data.
  408. static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
  409. switch (N->getOpcode()) {
  410. case ISD::TargetExternalSymbol:
  411. case ISD::ExternalSymbol:
  412. case ISD::MCSymbol:
  413. llvm_unreachable("Should only be used on nodes with operands");
  414. default: break; // Normal nodes don't need extra info.
  415. case ISD::TargetConstant:
  416. case ISD::Constant: {
  417. const ConstantSDNode *C = cast<ConstantSDNode>(N);
  418. ID.AddPointer(C->getConstantIntValue());
  419. ID.AddBoolean(C->isOpaque());
  420. break;
  421. }
  422. case ISD::TargetConstantFP:
  423. case ISD::ConstantFP:
  424. ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
  425. break;
  426. case ISD::TargetGlobalAddress:
  427. case ISD::GlobalAddress:
  428. case ISD::TargetGlobalTLSAddress:
  429. case ISD::GlobalTLSAddress: {
  430. const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
  431. ID.AddPointer(GA->getGlobal());
  432. ID.AddInteger(GA->getOffset());
  433. ID.AddInteger(GA->getTargetFlags());
  434. break;
  435. }
  436. case ISD::BasicBlock:
  437. ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
  438. break;
  439. case ISD::Register:
  440. ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
  441. break;
  442. case ISD::RegisterMask:
  443. ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
  444. break;
  445. case ISD::SRCVALUE:
  446. ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
  447. break;
  448. case ISD::FrameIndex:
  449. case ISD::TargetFrameIndex:
  450. ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
  451. break;
  452. case ISD::LIFETIME_START:
  453. case ISD::LIFETIME_END:
  454. if (cast<LifetimeSDNode>(N)->hasOffset()) {
  455. ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
  456. ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
  457. }
  458. break;
  459. case ISD::JumpTable:
  460. case ISD::TargetJumpTable:
  461. ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
  462. ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
  463. break;
  464. case ISD::ConstantPool:
  465. case ISD::TargetConstantPool: {
  466. const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
  467. ID.AddInteger(CP->getAlignment());
  468. ID.AddInteger(CP->getOffset());
  469. if (CP->isMachineConstantPoolEntry())
  470. CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
  471. else
  472. ID.AddPointer(CP->getConstVal());
  473. ID.AddInteger(CP->getTargetFlags());
  474. break;
  475. }
  476. case ISD::TargetIndex: {
  477. const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
  478. ID.AddInteger(TI->getIndex());
  479. ID.AddInteger(TI->getOffset());
  480. ID.AddInteger(TI->getTargetFlags());
  481. break;
  482. }
  483. case ISD::LOAD: {
  484. const LoadSDNode *LD = cast<LoadSDNode>(N);
  485. ID.AddInteger(LD->getMemoryVT().getRawBits());
  486. ID.AddInteger(LD->getRawSubclassData());
  487. ID.AddInteger(LD->getPointerInfo().getAddrSpace());
  488. break;
  489. }
  490. case ISD::STORE: {
  491. const StoreSDNode *ST = cast<StoreSDNode>(N);
  492. ID.AddInteger(ST->getMemoryVT().getRawBits());
  493. ID.AddInteger(ST->getRawSubclassData());
  494. ID.AddInteger(ST->getPointerInfo().getAddrSpace());
  495. break;
  496. }
  497. case ISD::MLOAD: {
  498. const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
  499. ID.AddInteger(MLD->getMemoryVT().getRawBits());
  500. ID.AddInteger(MLD->getRawSubclassData());
  501. ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
  502. break;
  503. }
  504. case ISD::MSTORE: {
  505. const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
  506. ID.AddInteger(MST->getMemoryVT().getRawBits());
  507. ID.AddInteger(MST->getRawSubclassData());
  508. ID.AddInteger(MST->getPointerInfo().getAddrSpace());
  509. break;
  510. }
  511. case ISD::MGATHER: {
  512. const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
  513. ID.AddInteger(MG->getMemoryVT().getRawBits());
  514. ID.AddInteger(MG->getRawSubclassData());
  515. ID.AddInteger(MG->getPointerInfo().getAddrSpace());
  516. break;
  517. }
  518. case ISD::MSCATTER: {
  519. const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
  520. ID.AddInteger(MS->getMemoryVT().getRawBits());
  521. ID.AddInteger(MS->getRawSubclassData());
  522. ID.AddInteger(MS->getPointerInfo().getAddrSpace());
  523. break;
  524. }
  525. case ISD::ATOMIC_CMP_SWAP:
  526. case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
  527. case ISD::ATOMIC_SWAP:
  528. case ISD::ATOMIC_LOAD_ADD:
  529. case ISD::ATOMIC_LOAD_SUB:
  530. case ISD::ATOMIC_LOAD_AND:
  531. case ISD::ATOMIC_LOAD_CLR:
  532. case ISD::ATOMIC_LOAD_OR:
  533. case ISD::ATOMIC_LOAD_XOR:
  534. case ISD::ATOMIC_LOAD_NAND:
  535. case ISD::ATOMIC_LOAD_MIN:
  536. case ISD::ATOMIC_LOAD_MAX:
  537. case ISD::ATOMIC_LOAD_UMIN:
  538. case ISD::ATOMIC_LOAD_UMAX:
  539. case ISD::ATOMIC_LOAD:
  540. case ISD::ATOMIC_STORE: {
  541. const AtomicSDNode *AT = cast<AtomicSDNode>(N);
  542. ID.AddInteger(AT->getMemoryVT().getRawBits());
  543. ID.AddInteger(AT->getRawSubclassData());
  544. ID.AddInteger(AT->getPointerInfo().getAddrSpace());
  545. break;
  546. }
  547. case ISD::PREFETCH: {
  548. const MemSDNode *PF = cast<MemSDNode>(N);
  549. ID.AddInteger(PF->getPointerInfo().getAddrSpace());
  550. break;
  551. }
  552. case ISD::VECTOR_SHUFFLE: {
  553. const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
  554. for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
  555. i != e; ++i)
  556. ID.AddInteger(SVN->getMaskElt(i));
  557. break;
  558. }
  559. case ISD::TargetBlockAddress:
  560. case ISD::BlockAddress: {
  561. const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
  562. ID.AddPointer(BA->getBlockAddress());
  563. ID.AddInteger(BA->getOffset());
  564. ID.AddInteger(BA->getTargetFlags());
  565. break;
  566. }
  567. } // end switch (N->getOpcode())
  568. // Target specific memory nodes could also have address spaces to check.
  569. if (N->isTargetMemoryOpcode())
  570. ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
  571. }
  572. /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
  573. /// data.
  574. static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
  575. AddNodeIDOpcode(ID, N->getOpcode());
  576. // Add the return value info.
  577. AddNodeIDValueTypes(ID, N->getVTList());
  578. // Add the operand info.
  579. AddNodeIDOperands(ID, N->ops());
  580. // Handle SDNode leafs with special info.
  581. AddNodeIDCustom(ID, N);
  582. }
  583. //===----------------------------------------------------------------------===//
  584. // SelectionDAG Class
  585. //===----------------------------------------------------------------------===//
  586. /// doNotCSE - Return true if CSE should not be performed for this node.
  587. static bool doNotCSE(SDNode *N) {
  588. if (N->getValueType(0) == MVT::Glue)
  589. return true; // Never CSE anything that produces a flag.
  590. switch (N->getOpcode()) {
  591. default: break;
  592. case ISD::HANDLENODE:
  593. case ISD::EH_LABEL:
  594. return true; // Never CSE these nodes.
  595. }
  596. // Check that remaining values produced are not flags.
  597. for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
  598. if (N->getValueType(i) == MVT::Glue)
  599. return true; // Never CSE anything that produces a flag.
  600. return false;
  601. }
  602. /// RemoveDeadNodes - This method deletes all unreachable nodes in the
  603. /// SelectionDAG.
  604. void SelectionDAG::RemoveDeadNodes() {
  605. // Create a dummy node (which is not added to allnodes), that adds a reference
  606. // to the root node, preventing it from being deleted.
  607. HandleSDNode Dummy(getRoot());
  608. SmallVector<SDNode*, 128> DeadNodes;
  609. // Add all obviously-dead nodes to the DeadNodes worklist.
  610. for (SDNode &Node : allnodes())
  611. if (Node.use_empty())
  612. DeadNodes.push_back(&Node);
  613. RemoveDeadNodes(DeadNodes);
  614. // If the root changed (e.g. it was a dead load, update the root).
  615. setRoot(Dummy.getValue());
  616. }
  617. /// RemoveDeadNodes - This method deletes the unreachable nodes in the
  618. /// given list, and any nodes that become unreachable as a result.
  619. void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
  620. // Process the worklist, deleting the nodes and adding their uses to the
  621. // worklist.
  622. while (!DeadNodes.empty()) {
  623. SDNode *N = DeadNodes.pop_back_val();
  624. // Skip to next node if we've already managed to delete the node. This could
  625. // happen if replacing a node causes a node previously added to the node to
  626. // be deleted.
  627. if (N->getOpcode() == ISD::DELETED_NODE)
  628. continue;
  629. for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
  630. DUL->NodeDeleted(N, nullptr);
  631. // Take the node out of the appropriate CSE map.
  632. RemoveNodeFromCSEMaps(N);
  633. // Next, brutally remove the operand list. This is safe to do, as there are
  634. // no cycles in the graph.
  635. for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
  636. SDUse &Use = *I++;
  637. SDNode *Operand = Use.getNode();
  638. Use.set(SDValue());
  639. // Now that we removed this operand, see if there are no uses of it left.
  640. if (Operand->use_empty())
  641. DeadNodes.push_back(Operand);
  642. }
  643. DeallocateNode(N);
  644. }
  645. }
  646. void SelectionDAG::RemoveDeadNode(SDNode *N){
  647. SmallVector<SDNode*, 16> DeadNodes(1, N);
  648. // Create a dummy node that adds a reference to the root node, preventing
  649. // it from being deleted. (This matters if the root is an operand of the
  650. // dead node.)
  651. HandleSDNode Dummy(getRoot());
  652. RemoveDeadNodes(DeadNodes);
  653. }
  654. void SelectionDAG::DeleteNode(SDNode *N) {
  655. // First take this out of the appropriate CSE map.
  656. RemoveNodeFromCSEMaps(N);
  657. // Finally, remove uses due to operands of this node, remove from the
  658. // AllNodes list, and delete the node.
  659. DeleteNodeNotInCSEMaps(N);
  660. }
  661. void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
  662. assert(N->getIterator() != AllNodes.begin() &&
  663. "Cannot delete the entry node!");
  664. assert(N->use_empty() && "Cannot delete a node that is not dead!");
  665. // Drop all of the operands and decrement used node's use counts.
  666. N->DropOperands();
  667. DeallocateNode(N);
  668. }
  669. void SDDbgInfo::erase(const SDNode *Node) {
  670. DbgValMapType::iterator I = DbgValMap.find(Node);
  671. if (I == DbgValMap.end())
  672. return;
  673. for (auto &Val: I->second)
  674. Val->setIsInvalidated();
  675. DbgValMap.erase(I);
  676. }
  677. void SelectionDAG::DeallocateNode(SDNode *N) {
  678. // If we have operands, deallocate them.
  679. removeOperands(N);
  680. NodeAllocator.Deallocate(AllNodes.remove(N));
  681. // Set the opcode to DELETED_NODE to help catch bugs when node
  682. // memory is reallocated.
  683. // FIXME: There are places in SDag that have grown a dependency on the opcode
  684. // value in the released node.
  685. __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
  686. N->NodeType = ISD::DELETED_NODE;
  687. // If any of the SDDbgValue nodes refer to this SDNode, invalidate
  688. // them and forget about that node.
  689. DbgInfo->erase(N);
  690. }
  691. #ifndef NDEBUG
  692. /// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
  693. static void VerifySDNode(SDNode *N) {
  694. switch (N->getOpcode()) {
  695. default:
  696. break;
  697. case ISD::BUILD_PAIR: {
  698. EVT VT = N->getValueType(0);
  699. assert(N->getNumValues() == 1 && "Too many results!");
  700. assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
  701. "Wrong return type!");
  702. assert(N->getNumOperands() == 2 && "Wrong number of operands!");
  703. assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
  704. "Mismatched operand types!");
  705. assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
  706. "Wrong operand type!");
  707. assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
  708. "Wrong return type size");
  709. break;
  710. }
  711. case ISD::BUILD_VECTOR: {
  712. assert(N->getNumValues() == 1 && "Too many results!");
  713. assert(N->getValueType(0).isVector() && "Wrong return type!");
  714. assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
  715. "Wrong number of operands!");
  716. EVT EltVT = N->getValueType(0).getVectorElementType();
  717. for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
  718. assert((I->getValueType() == EltVT ||
  719. (EltVT.isInteger() && I->getValueType().isInteger() &&
  720. EltVT.bitsLE(I->getValueType()))) &&
  721. "Wrong operand type!");
  722. assert(I->getValueType() == N->getOperand(0).getValueType() &&
  723. "Operands must all have the same type");
  724. }
  725. break;
  726. }
  727. }
  728. }
  729. #endif // NDEBUG
  730. /// Insert a newly allocated node into the DAG.
  731. ///
  732. /// Handles insertion into the all nodes list and CSE map, as well as
  733. /// verification and other common operations when a new node is allocated.
  734. void SelectionDAG::InsertNode(SDNode *N) {
  735. AllNodes.push_back(N);
  736. #ifndef NDEBUG
  737. N->PersistentId = NextPersistentId++;
  738. VerifySDNode(N);
  739. #endif
  740. for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
  741. DUL->NodeInserted(N);
  742. }
  743. /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
  744. /// correspond to it. This is useful when we're about to delete or repurpose
  745. /// the node. We don't want future request for structurally identical nodes
  746. /// to return N anymore.
  747. bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
  748. bool Erased = false;
  749. switch (N->getOpcode()) {
  750. case ISD::HANDLENODE: return false; // noop.
  751. case ISD::CONDCODE:
  752. assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
  753. "Cond code doesn't exist!");
  754. Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
  755. CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
  756. break;
  757. case ISD::ExternalSymbol:
  758. Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
  759. break;
  760. case ISD::TargetExternalSymbol: {
  761. ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
  762. Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
  763. ESN->getSymbol(), ESN->getTargetFlags()));
  764. break;
  765. }
  766. case ISD::MCSymbol: {
  767. auto *MCSN = cast<MCSymbolSDNode>(N);
  768. Erased = MCSymbols.erase(MCSN->getMCSymbol());
  769. break;
  770. }
  771. case ISD::VALUETYPE: {
  772. EVT VT = cast<VTSDNode>(N)->getVT();
  773. if (VT.isExtended()) {
  774. Erased = ExtendedValueTypeNodes.erase(VT);
  775. } else {
  776. Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
  777. ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
  778. }
  779. break;
  780. }
  781. default:
  782. // Remove it from the CSE Map.
  783. assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
  784. assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
  785. Erased = CSEMap.RemoveNode(N);
  786. break;
  787. }
  788. #ifndef NDEBUG
  789. // Verify that the node was actually in one of the CSE maps, unless it has a
  790. // flag result (which cannot be CSE'd) or is one of the special cases that are
  791. // not subject to CSE.
  792. if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
  793. !N->isMachineOpcode() && !doNotCSE(N)) {
  794. N->dump(this);
  795. dbgs() << "\n";
  796. llvm_unreachable("Node is not in map!");
  797. }
  798. #endif
  799. return Erased;
  800. }
  801. /// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
  802. /// maps and modified in place. Add it back to the CSE maps, unless an identical
  803. /// node already exists, in which case transfer all its users to the existing
  804. /// node. This transfer can potentially trigger recursive merging.
  805. void
  806. SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
  807. // For node types that aren't CSE'd, just act as if no identical node
  808. // already exists.
  809. if (!doNotCSE(N)) {
  810. SDNode *Existing = CSEMap.GetOrInsertNode(N);
  811. if (Existing != N) {
  812. // If there was already an existing matching node, use ReplaceAllUsesWith
  813. // to replace the dead one with the existing one. This can cause
  814. // recursive merging of other unrelated nodes down the line.
  815. ReplaceAllUsesWith(N, Existing);
  816. // N is now dead. Inform the listeners and delete it.
  817. for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
  818. DUL->NodeDeleted(N, Existing);
  819. DeleteNodeNotInCSEMaps(N);
  820. return;
  821. }
  822. }
  823. // If the node doesn't already exist, we updated it. Inform listeners.
  824. for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
  825. DUL->NodeUpdated(N);
  826. }
  827. /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
  828. /// were replaced with those specified. If this node is never memoized,
  829. /// return null, otherwise return a pointer to the slot it would take. If a
  830. /// node already exists with these operands, the slot will be non-null.
  831. SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
  832. void *&InsertPos) {
  833. if (doNotCSE(N))
  834. return nullptr;
  835. SDValue Ops[] = { Op };
  836. FoldingSetNodeID ID;
  837. AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
  838. AddNodeIDCustom(ID, N);
  839. SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
  840. if (Node)
  841. Node->intersectFlagsWith(N->getFlags());
  842. return Node;
  843. }
  844. /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
  845. /// were replaced with those specified. If this node is never memoized,
  846. /// return null, otherwise return a pointer to the slot it would take. If a
  847. /// node already exists with these operands, the slot will be non-null.
  848. SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
  849. SDValue Op1, SDValue Op2,
  850. void *&InsertPos) {
  851. if (doNotCSE(N))
  852. return nullptr;
  853. SDValue Ops[] = { Op1, Op2 };
  854. FoldingSetNodeID ID;
  855. AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
  856. AddNodeIDCustom(ID, N);
  857. SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
  858. if (Node)
  859. Node->intersectFlagsWith(N->getFlags());
  860. return Node;
  861. }
  862. /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
  863. /// were replaced with those specified. If this node is never memoized,
  864. /// return null, otherwise return a pointer to the slot it would take. If a
  865. /// node already exists with these operands, the slot will be non-null.
  866. SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
  867. void *&InsertPos) {
  868. if (doNotCSE(N))
  869. return nullptr;
  870. FoldingSetNodeID ID;
  871. AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
  872. AddNodeIDCustom(ID, N);
  873. SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
  874. if (Node)
  875. Node->intersectFlagsWith(N->getFlags());
  876. return Node;
  877. }
  878. unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
  879. Type *Ty = VT == MVT::iPTR ?
  880. PointerType::get(Type::getInt8Ty(*getContext()), 0) :
  881. VT.getTypeForEVT(*getContext());
  882. return getDataLayout().getABITypeAlignment(Ty);
  883. }
  884. // EntryNode could meaningfully have debug info if we can find it...
  885. SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
  886. : TM(tm), OptLevel(OL),
  887. EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
  888. Root(getEntryNode()) {
  889. InsertNode(&EntryNode);
  890. DbgInfo = new SDDbgInfo();
  891. }
  892. void SelectionDAG::init(MachineFunction &NewMF,
  893. OptimizationRemarkEmitter &NewORE,
  894. Pass *PassPtr, const TargetLibraryInfo *LibraryInfo,
  895. LegacyDivergenceAnalysis * Divergence) {
  896. MF = &NewMF;
  897. SDAGISelPass = PassPtr;
  898. ORE = &NewORE;
  899. TLI = getSubtarget().getTargetLowering();
  900. TSI = getSubtarget().getSelectionDAGInfo();
  901. LibInfo = LibraryInfo;
  902. Context = &MF->getFunction().getContext();
  903. DA = Divergence;
  904. }
  905. SelectionDAG::~SelectionDAG() {
  906. assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
  907. allnodes_clear();
  908. OperandRecycler.clear(OperandAllocator);
  909. delete DbgInfo;
  910. }
  911. void SelectionDAG::allnodes_clear() {
  912. assert(&*AllNodes.begin() == &EntryNode);
  913. AllNodes.remove(AllNodes.begin());
  914. while (!AllNodes.empty())
  915. DeallocateNode(&AllNodes.front());
  916. #ifndef NDEBUG
  917. NextPersistentId = 0;
  918. #endif
  919. }
  920. SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
  921. void *&InsertPos) {
  922. SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
  923. if (N) {
  924. switch (N->getOpcode()) {
  925. default: break;
  926. case ISD::Constant:
  927. case ISD::ConstantFP:
  928. llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
  929. "debug location. Use another overload.");
  930. }
  931. }
  932. return N;
  933. }
  934. SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
  935. const SDLoc &DL, void *&InsertPos) {
  936. SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
  937. if (N) {
  938. switch (N->getOpcode()) {
  939. case ISD::Constant:
  940. case ISD::ConstantFP:
  941. // Erase debug location from the node if the node is used at several
  942. // different places. Do not propagate one location to all uses as it
  943. // will cause a worse single stepping debugging experience.
  944. if (N->getDebugLoc() != DL.getDebugLoc())
  945. N->setDebugLoc(DebugLoc());
  946. break;
  947. default:
  948. // When the node's point of use is located earlier in the instruction
  949. // sequence than its prior point of use, update its debug info to the
  950. // earlier location.
  951. if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
  952. N->setDebugLoc(DL.getDebugLoc());
  953. break;
  954. }
  955. }
  956. return N;
  957. }
  958. void SelectionDAG::clear() {
  959. allnodes_clear();
  960. OperandRecycler.clear(OperandAllocator);
  961. OperandAllocator.Reset();
  962. CSEMap.clear();
  963. ExtendedValueTypeNodes.clear();
  964. ExternalSymbols.clear();
  965. TargetExternalSymbols.clear();
  966. MCSymbols.clear();
  967. SDCallSiteDbgInfo.clear();
  968. std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
  969. static_cast<CondCodeSDNode*>(nullptr));
  970. std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
  971. static_cast<SDNode*>(nullptr));
  972. EntryNode.UseList = nullptr;
  973. InsertNode(&EntryNode);
  974. Root = getEntryNode();
  975. DbgInfo->clear();
  976. }
  977. SDValue SelectionDAG::getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT) {
  978. return VT.bitsGT(Op.getValueType())
  979. ? getNode(ISD::FP_EXTEND, DL, VT, Op)
  980. : getNode(ISD::FP_ROUND, DL, VT, Op, getIntPtrConstant(0, DL));
  981. }
  982. SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
  983. return VT.bitsGT(Op.getValueType()) ?
  984. getNode(ISD::ANY_EXTEND, DL, VT, Op) :
  985. getNode(ISD::TRUNCATE, DL, VT, Op);
  986. }
  987. SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
  988. return VT.bitsGT(Op.getValueType()) ?
  989. getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
  990. getNode(ISD::TRUNCATE, DL, VT, Op);
  991. }
  992. SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
  993. return VT.bitsGT(Op.getValueType()) ?
  994. getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
  995. getNode(ISD::TRUNCATE, DL, VT, Op);
  996. }
  997. SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT,
  998. EVT OpVT) {
  999. if (VT.bitsLE(Op.getValueType()))
  1000. return getNode(ISD::TRUNCATE, SL, VT, Op);
  1001. TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
  1002. return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
  1003. }
  1004. SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT) {
  1005. assert(!VT.isVector() &&
  1006. "getZeroExtendInReg should use the vector element type instead of "
  1007. "the vector type!");
  1008. if (Op.getValueType().getScalarType() == VT) return Op;
  1009. unsigned BitWidth = Op.getScalarValueSizeInBits();
  1010. APInt Imm = APInt::getLowBitsSet(BitWidth,
  1011. VT.getSizeInBits());
  1012. return getNode(ISD::AND, DL, Op.getValueType(), Op,
  1013. getConstant(Imm, DL, Op.getValueType()));
  1014. }
  1015. SDValue SelectionDAG::getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
  1016. // Only unsigned pointer semantics are supported right now. In the future this
  1017. // might delegate to TLI to check pointer signedness.
  1018. return getZExtOrTrunc(Op, DL, VT);
  1019. }
  1020. SDValue SelectionDAG::getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT) {
  1021. // Only unsigned pointer semantics are supported right now. In the future this
  1022. // might delegate to TLI to check pointer signedness.
  1023. return getZeroExtendInReg(Op, DL, VT);
  1024. }
  1025. /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
  1026. SDValue SelectionDAG::getNOT(const SDLoc &DL, SDValue Val, EVT VT) {
  1027. EVT EltVT = VT.getScalarType();
  1028. SDValue NegOne =
  1029. getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, VT);
  1030. return getNode(ISD::XOR, DL, VT, Val, NegOne);
  1031. }
  1032. SDValue SelectionDAG::getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT) {
  1033. SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
  1034. return getNode(ISD::XOR, DL, VT, Val, TrueValue);
  1035. }
  1036. SDValue SelectionDAG::getBoolConstant(bool V, const SDLoc &DL, EVT VT,
  1037. EVT OpVT) {
  1038. if (!V)
  1039. return getConstant(0, DL, VT);
  1040. switch (TLI->getBooleanContents(OpVT)) {
  1041. case TargetLowering::ZeroOrOneBooleanContent:
  1042. case TargetLowering::UndefinedBooleanContent:
  1043. return getConstant(1, DL, VT);
  1044. case TargetLowering::ZeroOrNegativeOneBooleanContent:
  1045. return getAllOnesConstant(DL, VT);
  1046. }
  1047. llvm_unreachable("Unexpected boolean content enum!");
  1048. }
  1049. SDValue SelectionDAG::getConstant(uint64_t Val, const SDLoc &DL, EVT VT,
  1050. bool isT, bool isO) {
  1051. EVT EltVT = VT.getScalarType();
  1052. assert((EltVT.getSizeInBits() >= 64 ||
  1053. (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
  1054. "getConstant with a uint64_t value that doesn't fit in the type!");
  1055. return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
  1056. }
  1057. SDValue SelectionDAG::getConstant(const APInt &Val, const SDLoc &DL, EVT VT,
  1058. bool isT, bool isO) {
  1059. return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
  1060. }
  1061. SDValue SelectionDAG::getConstant(const ConstantInt &Val, const SDLoc &DL,
  1062. EVT VT, bool isT, bool isO) {
  1063. assert(VT.isInteger() && "Cannot create FP integer constant!");
  1064. EVT EltVT = VT.getScalarType();
  1065. const ConstantInt *Elt = &Val;
  1066. // In some cases the vector type is legal but the element type is illegal and
  1067. // needs to be promoted, for example v8i8 on ARM. In this case, promote the
  1068. // inserted value (the type does not need to match the vector element type).
  1069. // Any extra bits introduced will be truncated away.
  1070. if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
  1071. TargetLowering::TypePromoteInteger) {
  1072. EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
  1073. APInt NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
  1074. Elt = ConstantInt::get(*getContext(), NewVal);
  1075. }
  1076. // In other cases the element type is illegal and needs to be expanded, for
  1077. // example v2i64 on MIPS32. In this case, find the nearest legal type, split
  1078. // the value into n parts and use a vector type with n-times the elements.
  1079. // Then bitcast to the type requested.
  1080. // Legalizing constants too early makes the DAGCombiner's job harder so we
  1081. // only legalize if the DAG tells us we must produce legal types.
  1082. else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
  1083. TLI->getTypeAction(*getContext(), EltVT) ==
  1084. TargetLowering::TypeExpandInteger) {
  1085. const APInt &NewVal = Elt->getValue();
  1086. EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
  1087. unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
  1088. unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
  1089. EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
  1090. // Check the temporary vector is the correct size. If this fails then
  1091. // getTypeToTransformTo() probably returned a type whose size (in bits)
  1092. // isn't a power-of-2 factor of the requested type size.
  1093. assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
  1094. SmallVector<SDValue, 2> EltParts;
  1095. for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
  1096. EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
  1097. .zextOrTrunc(ViaEltSizeInBits), DL,
  1098. ViaEltVT, isT, isO));
  1099. }
  1100. // EltParts is currently in little endian order. If we actually want
  1101. // big-endian order then reverse it now.
  1102. if (getDataLayout().isBigEndian())
  1103. std::reverse(EltParts.begin(), EltParts.end());
  1104. // The elements must be reversed when the element order is different
  1105. // to the endianness of the elements (because the BITCAST is itself a
  1106. // vector shuffle in this situation). However, we do not need any code to
  1107. // perform this reversal because getConstant() is producing a vector
  1108. // splat.
  1109. // This situation occurs in MIPS MSA.
  1110. SmallVector<SDValue, 8> Ops;
  1111. for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
  1112. Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
  1113. SDValue V = getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
  1114. return V;
  1115. }
  1116. assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
  1117. "APInt size does not match type size!");
  1118. unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
  1119. FoldingSetNodeID ID;
  1120. AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
  1121. ID.AddPointer(Elt);
  1122. ID.AddBoolean(isO);
  1123. void *IP = nullptr;
  1124. SDNode *N = nullptr;
  1125. if ((N = FindNodeOrInsertPos(ID, DL, IP)))
  1126. if (!VT.isVector())
  1127. return SDValue(N, 0);
  1128. if (!N) {
  1129. N = newSDNode<ConstantSDNode>(isT, isO, Elt, EltVT);
  1130. CSEMap.InsertNode(N, IP);
  1131. InsertNode(N);
  1132. NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
  1133. }
  1134. SDValue Result(N, 0);
  1135. if (VT.isVector())
  1136. Result = getSplatBuildVector(VT, DL, Result);
  1137. return Result;
  1138. }
  1139. SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, const SDLoc &DL,
  1140. bool isTarget) {
  1141. return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
  1142. }
  1143. SDValue SelectionDAG::getShiftAmountConstant(uint64_t Val, EVT VT,
  1144. const SDLoc &DL, bool LegalTypes) {
  1145. EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout(), LegalTypes);
  1146. return getConstant(Val, DL, ShiftVT);
  1147. }
  1148. SDValue SelectionDAG::getConstantFP(const APFloat &V, const SDLoc &DL, EVT VT,
  1149. bool isTarget) {
  1150. return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
  1151. }
  1152. SDValue SelectionDAG::getConstantFP(const ConstantFP &V, const SDLoc &DL,
  1153. EVT VT, bool isTarget) {
  1154. assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
  1155. EVT EltVT = VT.getScalarType();
  1156. // Do the map lookup using the actual bit pattern for the floating point
  1157. // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
  1158. // we don't have issues with SNANs.
  1159. unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
  1160. FoldingSetNodeID ID;
  1161. AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
  1162. ID.AddPointer(&V);
  1163. void *IP = nullptr;
  1164. SDNode *N = nullptr;
  1165. if ((N = FindNodeOrInsertPos(ID, DL, IP)))
  1166. if (!VT.isVector())
  1167. return SDValue(N, 0);
  1168. if (!N) {
  1169. N = newSDNode<ConstantFPSDNode>(isTarget, &V, EltVT);
  1170. CSEMap.InsertNode(N, IP);
  1171. InsertNode(N);
  1172. }
  1173. SDValue Result(N, 0);
  1174. if (VT.isVector())
  1175. Result = getSplatBuildVector(VT, DL, Result);
  1176. NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
  1177. return Result;
  1178. }
  1179. SDValue SelectionDAG::getConstantFP(double Val, const SDLoc &DL, EVT VT,
  1180. bool isTarget) {
  1181. EVT EltVT = VT.getScalarType();
  1182. if (EltVT == MVT::f32)
  1183. return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
  1184. else if (EltVT == MVT::f64)
  1185. return getConstantFP(APFloat(Val), DL, VT, isTarget);
  1186. else if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
  1187. EltVT == MVT::f16) {
  1188. bool Ignored;
  1189. APFloat APF = APFloat(Val);
  1190. APF.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
  1191. &Ignored);
  1192. return getConstantFP(APF, DL, VT, isTarget);
  1193. } else
  1194. llvm_unreachable("Unsupported type in getConstantFP");
  1195. }
  1196. SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, const SDLoc &DL,
  1197. EVT VT, int64_t Offset, bool isTargetGA,
  1198. unsigned TargetFlags) {
  1199. assert((TargetFlags == 0 || isTargetGA) &&
  1200. "Cannot set target flags on target-independent globals");
  1201. // Truncate (with sign-extension) the offset value to the pointer size.
  1202. unsigned BitWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
  1203. if (BitWidth < 64)
  1204. Offset = SignExtend64(Offset, BitWidth);
  1205. unsigned Opc;
  1206. if (GV->isThreadLocal())
  1207. Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
  1208. else
  1209. Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
  1210. FoldingSetNodeID ID;
  1211. AddNodeIDNode(ID, Opc, getVTList(VT), None);
  1212. ID.AddPointer(GV);
  1213. ID.AddInteger(Offset);
  1214. ID.AddInteger(TargetFlags);
  1215. void *IP = nullptr;
  1216. if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
  1217. return SDValue(E, 0);
  1218. auto *N = newSDNode<GlobalAddressSDNode>(
  1219. Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VT, Offset, TargetFlags);
  1220. CSEMap.InsertNode(N, IP);
  1221. InsertNode(N);
  1222. return SDValue(N, 0);
  1223. }
  1224. SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
  1225. unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
  1226. FoldingSetNodeID ID;
  1227. AddNodeIDNode(ID, Opc, getVTList(VT), None);
  1228. ID.AddInteger(FI);
  1229. void *IP = nullptr;
  1230. if (SDNode *E = FindNodeOrInsertPos(ID, IP))
  1231. return SDValue(E, 0);
  1232. auto *N = newSDNode<FrameIndexSDNode>(FI, VT, isTarget);
  1233. CSEMap.InsertNode(N, IP);
  1234. InsertNode(N);
  1235. return SDValue(N, 0);
  1236. }
  1237. SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
  1238. unsigned TargetFlags) {
  1239. assert((TargetFlags == 0 || isTarget) &&
  1240. "Cannot set target flags on target-independent jump tables");
  1241. unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
  1242. FoldingSetNodeID ID;
  1243. AddNodeIDNode(ID, Opc, getVTList(VT), None);
  1244. ID.AddInteger(JTI);
  1245. ID.AddInteger(TargetFlags);
  1246. void *IP = nullptr;
  1247. if (SDNode *E = FindNodeOrInsertPos(ID, IP))
  1248. return SDValue(E, 0);
  1249. auto *N = newSDNode<JumpTableSDNode>(JTI, VT, isTarget, TargetFlags);
  1250. CSEMap.InsertNode(N, IP);
  1251. InsertNode(N);
  1252. return SDValue(N, 0);
  1253. }
  1254. SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
  1255. unsigned Alignment, int Offset,
  1256. bool isTarget,
  1257. unsigned TargetFlags) {
  1258. assert((TargetFlags == 0 || isTarget) &&
  1259. "Cannot set target flags on target-independent globals");
  1260. if (Alignment == 0)
  1261. Alignment = MF->getFunction().hasOptSize()
  1262. ? getDataLayout().getABITypeAlignment(C->getType())
  1263. : getDataLayout().getPrefTypeAlignment(C->getType());
  1264. unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
  1265. FoldingSetNodeID ID;
  1266. AddNodeIDNode(ID, Opc, getVTList(VT), None);
  1267. ID.AddInteger(Alignment);
  1268. ID.AddInteger(Offset);
  1269. ID.AddPointer(C);
  1270. ID.AddInteger(TargetFlags);
  1271. void *IP = nullptr;
  1272. if (SDNode *E = FindNodeOrInsertPos(ID, IP))
  1273. return SDValue(E, 0);
  1274. auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VT, Offset, Alignment,
  1275. TargetFlags);
  1276. CSEMap.InsertNode(N, IP);
  1277. InsertNode(N);
  1278. return SDValue(N, 0);
  1279. }
  1280. SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
  1281. unsigned Alignment, int Offset,
  1282. bool isTarget,
  1283. unsigned TargetFlags) {
  1284. assert((TargetFlags == 0 || isTarget) &&
  1285. "Cannot set target flags on target-independent globals");
  1286. if (Alignment == 0)
  1287. Alignment = getDataLayout().getPrefTypeAlignment(C->getType());
  1288. unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
  1289. FoldingSetNodeID ID;
  1290. AddNodeIDNode(ID, Opc, getVTList(VT), None);
  1291. ID.AddInteger(Alignment);
  1292. ID.AddInteger(Offset);
  1293. C->addSelectionDAGCSEId(ID);
  1294. ID.AddInteger(TargetFlags);
  1295. void *IP = nullptr;
  1296. if (SDNode *E = FindNodeOrInsertPos(ID, IP))
  1297. return SDValue(E, 0);
  1298. auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VT, Offset, Alignment,
  1299. TargetFlags);
  1300. CSEMap.InsertNode(N, IP);
  1301. InsertNode(N);
  1302. return SDValue(N, 0);
  1303. }
  1304. SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
  1305. unsigned TargetFlags) {
  1306. FoldingSetNodeID ID;
  1307. AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
  1308. ID.AddInteger(Index);
  1309. ID.AddInteger(Offset);
  1310. ID.AddInteger(TargetFlags);
  1311. void *IP = nullptr;
  1312. if (SDNode *E = FindNodeOrInsertPos(ID, IP))
  1313. return SDValue(E, 0);
  1314. auto *N = newSDNode<TargetIndexSDNode>(Index, VT, Offset, TargetFlags);
  1315. CSEMap.InsertNode(N, IP);
  1316. InsertNode(N);
  1317. return SDValue(N, 0);
  1318. }
  1319. SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
  1320. FoldingSetNodeID ID;
  1321. AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
  1322. ID.AddPointer(MBB);
  1323. void *IP = nullptr;
  1324. if (SDNode *E = FindNodeOrInsertPos(ID, IP))
  1325. return SDValue(E, 0);
  1326. auto *N = newSDNode<BasicBlockSDNode>(MBB);
  1327. CSEMap.InsertNode(N, IP);
  1328. InsertNode(N);
  1329. return SDValue(N, 0);
  1330. }
  1331. SDValue SelectionDAG::getValueType(EVT VT) {
  1332. if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
  1333. ValueTypeNodes.size())
  1334. ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
  1335. SDNode *&N = VT.isExtended() ?
  1336. ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
  1337. if (N) return SDValue(N, 0);
  1338. N = newSDNode<VTSDNode>(VT);
  1339. InsertNode(N);
  1340. return SDValue(N, 0);
  1341. }
  1342. SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
  1343. SDNode *&N = ExternalSymbols[Sym];
  1344. if (N) return SDValue(N, 0);
  1345. N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, VT);
  1346. InsertNode(N);
  1347. return SDValue(N, 0);
  1348. }
  1349. SDValue SelectionDAG::getMCSymbol(MCSymbol *Sym, EVT VT) {
  1350. SDNode *&N = MCSymbols[Sym];
  1351. if (N)
  1352. return SDValue(N, 0);
  1353. N = newSDNode<MCSymbolSDNode>(Sym, VT);
  1354. InsertNode(N);
  1355. return SDValue(N, 0);
  1356. }
  1357. SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
  1358. unsigned TargetFlags) {
  1359. SDNode *&N =
  1360. TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
  1361. if (N) return SDValue(N, 0);
  1362. N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, VT);
  1363. InsertNode(N);
  1364. return SDValue(N, 0);
  1365. }
  1366. SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
  1367. if ((unsigned)Cond >= CondCodeNodes.size())
  1368. CondCodeNodes.resize(Cond+1);
  1369. if (!CondCodeNodes[Cond]) {
  1370. auto *N = newSDNode<CondCodeSDNode>(Cond);
  1371. CondCodeNodes[Cond] = N;
  1372. InsertNode(N);
  1373. }
  1374. return SDValue(CondCodeNodes[Cond], 0);
  1375. }
  1376. /// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
  1377. /// point at N1 to point at N2 and indices that point at N2 to point at N1.
  1378. static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef<int> M) {
  1379. std::swap(N1, N2);
  1380. ShuffleVectorSDNode::commuteMask(M);
  1381. }
  1382. SDValue SelectionDAG::getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1,
  1383. SDValue N2, ArrayRef<int> Mask) {
  1384. assert(VT.getVectorNumElements() == Mask.size() &&
  1385. "Must have the same number of vector elements as mask elements!");
  1386. assert(VT == N1.getValueType() && VT == N2.getValueType() &&
  1387. "Invalid VECTOR_SHUFFLE");
  1388. // Canonicalize shuffle undef, undef -> undef
  1389. if (N1.isUndef() && N2.isUndef())
  1390. return getUNDEF(VT);
  1391. // Validate that all indices in Mask are within the range of the elements
  1392. // input to the shuffle.
  1393. int NElts = Mask.size();
  1394. assert(llvm::all_of(Mask,
  1395. [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
  1396. "Index out of range");
  1397. // Copy the mask so we can do any needed cleanup.
  1398. SmallVector<int, 8> MaskVec(Mask.begin(), Mask.end());
  1399. // Canonicalize shuffle v, v -> v, undef
  1400. if (N1 == N2) {
  1401. N2 = getUNDEF(VT);
  1402. for (int i = 0; i != NElts; ++i)
  1403. if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
  1404. }
  1405. // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
  1406. if (N1.isUndef())
  1407. commuteShuffle(N1, N2, MaskVec);
  1408. if (TLI->hasVectorBlend()) {
  1409. // If shuffling a splat, try to blend the splat instead. We do this here so
  1410. // that even when this arises during lowering we don't have to re-handle it.
  1411. auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
  1412. BitVector UndefElements;
  1413. SDValue Splat = BV->getSplatValue(&UndefElements);
  1414. if (!Splat)
  1415. return;
  1416. for (int i = 0; i < NElts; ++i) {
  1417. if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
  1418. continue;
  1419. // If this input comes from undef, mark it as such.
  1420. if (UndefElements[MaskVec[i] - Offset]) {
  1421. MaskVec[i] = -1;
  1422. continue;
  1423. }
  1424. // If we can blend a non-undef lane, use that instead.
  1425. if (!UndefElements[i])
  1426. MaskVec[i] = i + Offset;
  1427. }
  1428. };
  1429. if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
  1430. BlendSplat(N1BV, 0);
  1431. if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
  1432. BlendSplat(N2BV, NElts);
  1433. }
  1434. // Canonicalize all index into lhs, -> shuffle lhs, undef
  1435. // Canonicalize all index into rhs, -> shuffle rhs, undef
  1436. bool AllLHS = true, AllRHS = true;
  1437. bool N2Undef = N2.isUndef();
  1438. for (int i = 0; i != NElts; ++i) {
  1439. if (MaskVec[i] >= NElts) {
  1440. if (N2Undef)
  1441. MaskVec[i] = -1;
  1442. else
  1443. AllLHS = false;
  1444. } else if (MaskVec[i] >= 0) {
  1445. AllRHS = false;
  1446. }
  1447. }
  1448. if (AllLHS && AllRHS)
  1449. return getUNDEF(VT);
  1450. if (AllLHS && !N2Undef)
  1451. N2 = getUNDEF(VT);
  1452. if (AllRHS) {
  1453. N1 = getUNDEF(VT);
  1454. commuteShuffle(N1, N2, MaskVec);
  1455. }
  1456. // Reset our undef status after accounting for the mask.
  1457. N2Undef = N2.isUndef();
  1458. // Re-check whether both sides ended up undef.
  1459. if (N1.isUndef() && N2Undef)
  1460. return getUNDEF(VT);
  1461. // If Identity shuffle return that node.
  1462. bool Identity = true, AllSame = true;
  1463. for (int i = 0; i != NElts; ++i) {
  1464. if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
  1465. if (MaskVec[i] != MaskVec[0]) AllSame = false;
  1466. }
  1467. if (Identity && NElts)
  1468. return N1;
  1469. // Shuffling a constant splat doesn't change the result.
  1470. if (N2Undef) {
  1471. SDValue V = N1;
  1472. // Look through any bitcasts. We check that these don't change the number
  1473. // (and size) of elements and just changes their types.
  1474. while (V.getOpcode() == ISD::BITCAST)
  1475. V = V->getOperand(0);
  1476. // A splat should always show up as a build vector node.
  1477. if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
  1478. BitVector UndefElements;
  1479. SDValue Splat = BV->getSplatValue(&UndefElements);
  1480. // If this is a splat of an undef, shuffling it is also undef.
  1481. if (Splat && Splat.isUndef())
  1482. return getUNDEF(VT);
  1483. bool SameNumElts =
  1484. V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
  1485. // We only have a splat which can skip shuffles if there is a splatted
  1486. // value and no undef lanes rearranged by the shuffle.
  1487. if (Splat && UndefElements.none()) {
  1488. // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
  1489. // number of elements match or the value splatted is a zero constant.
  1490. if (SameNumElts)
  1491. return N1;
  1492. if (auto *C = dyn_cast<ConstantSDNode>(Splat))
  1493. if (C->isNullValue())
  1494. return N1;
  1495. }
  1496. // If the shuffle itself creates a splat, build the vector directly.
  1497. if (AllSame && SameNumElts) {
  1498. EVT BuildVT = BV->getValueType(0);
  1499. const SDValue &Splatted = BV->getOperand(MaskVec[0]);
  1500. SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
  1501. // We may have jumped through bitcasts, so the type of the
  1502. // BUILD_VECTOR may not match the type of the shuffle.
  1503. if (BuildVT != VT)
  1504. NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
  1505. return NewBV;
  1506. }
  1507. }
  1508. }
  1509. FoldingSetNodeID ID;
  1510. SDValue Ops[2] = { N1, N2 };
  1511. AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
  1512. for (int i = 0; i != NElts; ++i)
  1513. ID.AddInteger(MaskVec[i]);
  1514. void* IP = nullptr;
  1515. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
  1516. return SDValue(E, 0);
  1517. // Allocate the mask array for the node out of the BumpPtrAllocator, since
  1518. // SDNode doesn't have access to it. This memory will be "leaked" when
  1519. // the node is deallocated, but recovered when the NodeAllocator is released.
  1520. int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
  1521. llvm::copy(MaskVec, MaskAlloc);
  1522. auto *N = newSDNode<ShuffleVectorSDNode>(VT, dl.getIROrder(),
  1523. dl.getDebugLoc(), MaskAlloc);
  1524. createOperands(N, Ops);
  1525. CSEMap.InsertNode(N, IP);
  1526. InsertNode(N);
  1527. SDValue V = SDValue(N, 0);
  1528. NewSDValueDbgMsg(V, "Creating new node: ", this);
  1529. return V;
  1530. }
  1531. SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
  1532. EVT VT = SV.getValueType(0);
  1533. SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end());
  1534. ShuffleVectorSDNode::commuteMask(MaskVec);
  1535. SDValue Op0 = SV.getOperand(0);
  1536. SDValue Op1 = SV.getOperand(1);
  1537. return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
  1538. }
  1539. SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
  1540. FoldingSetNodeID ID;
  1541. AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
  1542. ID.AddInteger(RegNo);
  1543. void *IP = nullptr;
  1544. if (SDNode *E = FindNodeOrInsertPos(ID, IP))
  1545. return SDValue(E, 0);
  1546. auto *N = newSDNode<RegisterSDNode>(RegNo, VT);
  1547. N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, DA);
  1548. CSEMap.InsertNode(N, IP);
  1549. InsertNode(N);
  1550. return SDValue(N, 0);
  1551. }
  1552. SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
  1553. FoldingSetNodeID ID;
  1554. AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
  1555. ID.AddPointer(RegMask);
  1556. void *IP = nullptr;
  1557. if (SDNode *E = FindNodeOrInsertPos(ID, IP))
  1558. return SDValue(E, 0);
  1559. auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
  1560. CSEMap.InsertNode(N, IP);
  1561. InsertNode(N);
  1562. return SDValue(N, 0);
  1563. }
  1564. SDValue SelectionDAG::getEHLabel(const SDLoc &dl, SDValue Root,
  1565. MCSymbol *Label) {
  1566. return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
  1567. }
  1568. SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
  1569. SDValue Root, MCSymbol *Label) {
  1570. FoldingSetNodeID ID;
  1571. SDValue Ops[] = { Root };
  1572. AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
  1573. ID.AddPointer(Label);
  1574. void *IP = nullptr;
  1575. if (SDNode *E = FindNodeOrInsertPos(ID, IP))
  1576. return SDValue(E, 0);
  1577. auto *N =
  1578. newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
  1579. createOperands(N, Ops);
  1580. CSEMap.InsertNode(N, IP);
  1581. InsertNode(N);
  1582. return SDValue(N, 0);
  1583. }
  1584. SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
  1585. int64_t Offset, bool isTarget,
  1586. unsigned TargetFlags) {
  1587. unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
  1588. FoldingSetNodeID ID;
  1589. AddNodeIDNode(ID, Opc, getVTList(VT), None);
  1590. ID.AddPointer(BA);
  1591. ID.AddInteger(Offset);
  1592. ID.AddInteger(TargetFlags);
  1593. void *IP = nullptr;
  1594. if (SDNode *E = FindNodeOrInsertPos(ID, IP))
  1595. return SDValue(E, 0);
  1596. auto *N = newSDNode<BlockAddressSDNode>(Opc, VT, BA, Offset, TargetFlags);
  1597. CSEMap.InsertNode(N, IP);
  1598. InsertNode(N);
  1599. return SDValue(N, 0);
  1600. }
  1601. SDValue SelectionDAG::getSrcValue(const Value *V) {
  1602. assert((!V || V->getType()->isPointerTy()) &&
  1603. "SrcValue is not a pointer?");
  1604. FoldingSetNodeID ID;
  1605. AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
  1606. ID.AddPointer(V);
  1607. void *IP = nullptr;
  1608. if (SDNode *E = FindNodeOrInsertPos(ID, IP))
  1609. return SDValue(E, 0);
  1610. auto *N = newSDNode<SrcValueSDNode>(V);
  1611. CSEMap.InsertNode(N, IP);
  1612. InsertNode(N);
  1613. return SDValue(N, 0);
  1614. }
  1615. SDValue SelectionDAG::getMDNode(const MDNode *MD) {
  1616. FoldingSetNodeID ID;
  1617. AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
  1618. ID.AddPointer(MD);
  1619. void *IP = nullptr;
  1620. if (SDNode *E = FindNodeOrInsertPos(ID, IP))
  1621. return SDValue(E, 0);
  1622. auto *N = newSDNode<MDNodeSDNode>(MD);
  1623. CSEMap.InsertNode(N, IP);
  1624. InsertNode(N);
  1625. return SDValue(N, 0);
  1626. }
  1627. SDValue SelectionDAG::getBitcast(EVT VT, SDValue V) {
  1628. if (VT == V.getValueType())
  1629. return V;
  1630. return getNode(ISD::BITCAST, SDLoc(V), VT, V);
  1631. }
  1632. SDValue SelectionDAG::getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr,
  1633. unsigned SrcAS, unsigned DestAS) {
  1634. SDValue Ops[] = {Ptr};
  1635. FoldingSetNodeID ID;
  1636. AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
  1637. ID.AddInteger(SrcAS);
  1638. ID.AddInteger(DestAS);
  1639. void *IP = nullptr;
  1640. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
  1641. return SDValue(E, 0);
  1642. auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
  1643. VT, SrcAS, DestAS);
  1644. createOperands(N, Ops);
  1645. CSEMap.InsertNode(N, IP);
  1646. InsertNode(N);
  1647. return SDValue(N, 0);
  1648. }
  1649. /// getShiftAmountOperand - Return the specified value casted to
  1650. /// the target's desired shift amount type.
  1651. SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
  1652. EVT OpTy = Op.getValueType();
  1653. EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
  1654. if (OpTy == ShTy || OpTy.isVector()) return Op;
  1655. return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
  1656. }
  1657. SDValue SelectionDAG::expandVAArg(SDNode *Node) {
  1658. SDLoc dl(Node);
  1659. const TargetLowering &TLI = getTargetLoweringInfo();
  1660. const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
  1661. EVT VT = Node->getValueType(0);
  1662. SDValue Tmp1 = Node->getOperand(0);
  1663. SDValue Tmp2 = Node->getOperand(1);
  1664. const llvm::MaybeAlign MA(Node->getConstantOperandVal(3));
  1665. SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
  1666. Tmp2, MachinePointerInfo(V));
  1667. SDValue VAList = VAListLoad;
  1668. if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
  1669. VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
  1670. getConstant(MA->value() - 1, dl, VAList.getValueType()));
  1671. VAList =
  1672. getNode(ISD::AND, dl, VAList.getValueType(), VAList,
  1673. getConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
  1674. }
  1675. // Increment the pointer, VAList, to the next vaarg
  1676. Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
  1677. getConstant(getDataLayout().getTypeAllocSize(
  1678. VT.getTypeForEVT(*getContext())),
  1679. dl, VAList.getValueType()));
  1680. // Store the incremented VAList to the legalized pointer
  1681. Tmp1 =
  1682. getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
  1683. // Load the actual argument out of the pointer VAList
  1684. return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
  1685. }
  1686. SDValue SelectionDAG::expandVACopy(SDNode *Node) {
  1687. SDLoc dl(Node);
  1688. const TargetLowering &TLI = getTargetLoweringInfo();
  1689. // This defaults to loading a pointer from the input and storing it to the
  1690. // output, returning the chain.
  1691. const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
  1692. const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
  1693. SDValue Tmp1 =
  1694. getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
  1695. Node->getOperand(2), MachinePointerInfo(VS));
  1696. return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
  1697. MachinePointerInfo(VD));
  1698. }
  1699. SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
  1700. MachineFrameInfo &MFI = getMachineFunction().getFrameInfo();
  1701. unsigned ByteSize = VT.getStoreSize();
  1702. Type *Ty = VT.getTypeForEVT(*getContext());
  1703. unsigned StackAlign =
  1704. std::max((unsigned)getDataLayout().getPrefTypeAlignment(Ty), minAlign);
  1705. int FrameIdx = MFI.CreateStackObject(ByteSize, StackAlign, false);
  1706. return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
  1707. }
  1708. SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
  1709. unsigned Bytes = std::max(VT1.getStoreSize(), VT2.getStoreSize());
  1710. Type *Ty1 = VT1.getTypeForEVT(*getContext());
  1711. Type *Ty2 = VT2.getTypeForEVT(*getContext());
  1712. const DataLayout &DL = getDataLayout();
  1713. unsigned Align =
  1714. std::max(DL.getPrefTypeAlignment(Ty1), DL.getPrefTypeAlignment(Ty2));
  1715. MachineFrameInfo &MFI = getMachineFunction().getFrameInfo();
  1716. int FrameIdx = MFI.CreateStackObject(Bytes, Align, false);
  1717. return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
  1718. }
  1719. SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1, SDValue N2,
  1720. ISD::CondCode Cond, const SDLoc &dl) {
  1721. EVT OpVT = N1.getValueType();
  1722. // These setcc operations always fold.
  1723. switch (Cond) {
  1724. default: break;
  1725. case ISD::SETFALSE:
  1726. case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
  1727. case ISD::SETTRUE:
  1728. case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
  1729. case ISD::SETOEQ:
  1730. case ISD::SETOGT:
  1731. case ISD::SETOGE:
  1732. case ISD::SETOLT:
  1733. case ISD::SETOLE:
  1734. case ISD::SETONE:
  1735. case ISD::SETO:
  1736. case ISD::SETUO:
  1737. case ISD::SETUEQ:
  1738. case ISD::SETUNE:
  1739. assert(!OpVT.isInteger() && "Illegal setcc for integer!");
  1740. break;
  1741. }
  1742. if (OpVT.isInteger()) {
  1743. // For EQ and NE, we can always pick a value for the undef to make the
  1744. // predicate pass or fail, so we can return undef.
  1745. // Matches behavior in llvm::ConstantFoldCompareInstruction.
  1746. // icmp eq/ne X, undef -> undef.
  1747. if ((N1.isUndef() || N2.isUndef()) &&
  1748. (Cond == ISD::SETEQ || Cond == ISD::SETNE))
  1749. return getUNDEF(VT);
  1750. // If both operands are undef, we can return undef for int comparison.
  1751. // icmp undef, undef -> undef.
  1752. if (N1.isUndef() && N2.isUndef())
  1753. return getUNDEF(VT);
  1754. // icmp X, X -> true/false
  1755. // icmp X, undef -> true/false because undef could be X.
  1756. if (N1 == N2)
  1757. return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
  1758. }
  1759. if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
  1760. const APInt &C2 = N2C->getAPIntValue();
  1761. if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
  1762. const APInt &C1 = N1C->getAPIntValue();
  1763. switch (Cond) {
  1764. default: llvm_unreachable("Unknown integer setcc!");
  1765. case ISD::SETEQ: return getBoolConstant(C1 == C2, dl, VT, OpVT);
  1766. case ISD::SETNE: return getBoolConstant(C1 != C2, dl, VT, OpVT);
  1767. case ISD::SETULT: return getBoolConstant(C1.ult(C2), dl, VT, OpVT);
  1768. case ISD::SETUGT: return getBoolConstant(C1.ugt(C2), dl, VT, OpVT);
  1769. case ISD::SETULE: return getBoolConstant(C1.ule(C2), dl, VT, OpVT);
  1770. case ISD::SETUGE: return getBoolConstant(C1.uge(C2), dl, VT, OpVT);
  1771. case ISD::SETLT: return getBoolConstant(C1.slt(C2), dl, VT, OpVT);
  1772. case ISD::SETGT: return getBoolConstant(C1.sgt(C2), dl, VT, OpVT);
  1773. case ISD::SETLE: return getBoolConstant(C1.sle(C2), dl, VT, OpVT);
  1774. case ISD::SETGE: return getBoolConstant(C1.sge(C2), dl, VT, OpVT);
  1775. }
  1776. }
  1777. }
  1778. auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
  1779. auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
  1780. if (N1CFP && N2CFP) {
  1781. APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
  1782. switch (Cond) {
  1783. default: break;
  1784. case ISD::SETEQ: if (R==APFloat::cmpUnordered)
  1785. return getUNDEF(VT);
  1786. LLVM_FALLTHROUGH;
  1787. case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
  1788. OpVT);
  1789. case ISD::SETNE: if (R==APFloat::cmpUnordered)
  1790. return getUNDEF(VT);
  1791. LLVM_FALLTHROUGH;
  1792. case ISD::SETONE: return getBoolConstant(R==APFloat::cmpGreaterThan ||
  1793. R==APFloat::cmpLessThan, dl, VT,
  1794. OpVT);
  1795. case ISD::SETLT: if (R==APFloat::cmpUnordered)
  1796. return getUNDEF(VT);
  1797. LLVM_FALLTHROUGH;
  1798. case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
  1799. OpVT);
  1800. case ISD::SETGT: if (R==APFloat::cmpUnordered)
  1801. return getUNDEF(VT);
  1802. LLVM_FALLTHROUGH;
  1803. case ISD::SETOGT: return getBoolConstant(R==APFloat::cmpGreaterThan, dl,
  1804. VT, OpVT);
  1805. case ISD::SETLE: if (R==APFloat::cmpUnordered)
  1806. return getUNDEF(VT);
  1807. LLVM_FALLTHROUGH;
  1808. case ISD::SETOLE: return getBoolConstant(R==APFloat::cmpLessThan ||
  1809. R==APFloat::cmpEqual, dl, VT,
  1810. OpVT);
  1811. case ISD::SETGE: if (R==APFloat::cmpUnordered)
  1812. return getUNDEF(VT);
  1813. LLVM_FALLTHROUGH;
  1814. case ISD::SETOGE: return getBoolConstant(R==APFloat::cmpGreaterThan ||
  1815. R==APFloat::cmpEqual, dl, VT, OpVT);
  1816. case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
  1817. OpVT);
  1818. case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
  1819. OpVT);
  1820. case ISD::SETUEQ: return getBoolConstant(R==APFloat::cmpUnordered ||
  1821. R==APFloat::cmpEqual, dl, VT,
  1822. OpVT);
  1823. case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
  1824. OpVT);
  1825. case ISD::SETULT: return getBoolConstant(R==APFloat::cmpUnordered ||
  1826. R==APFloat::cmpLessThan, dl, VT,
  1827. OpVT);
  1828. case ISD::SETUGT: return getBoolConstant(R==APFloat::cmpGreaterThan ||
  1829. R==APFloat::cmpUnordered, dl, VT,
  1830. OpVT);
  1831. case ISD::SETULE: return getBoolConstant(R!=APFloat::cmpGreaterThan, dl,
  1832. VT, OpVT);
  1833. case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
  1834. OpVT);
  1835. }
  1836. } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
  1837. // Ensure that the constant occurs on the RHS.
  1838. ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
  1839. if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
  1840. return SDValue();
  1841. return getSetCC(dl, VT, N2, N1, SwappedCond);
  1842. } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
  1843. (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
  1844. // If an operand is known to be a nan (or undef that could be a nan), we can
  1845. // fold it.
  1846. // Choosing NaN for the undef will always make unordered comparison succeed
  1847. // and ordered comparison fails.
  1848. // Matches behavior in llvm::ConstantFoldCompareInstruction.
  1849. switch (ISD::getUnorderedFlavor(Cond)) {
  1850. default:
  1851. llvm_unreachable("Unknown flavor!");
  1852. case 0: // Known false.
  1853. return getBoolConstant(false, dl, VT, OpVT);
  1854. case 1: // Known true.
  1855. return getBoolConstant(true, dl, VT, OpVT);
  1856. case 2: // Undefined.
  1857. return getUNDEF(VT);
  1858. }
  1859. }
  1860. // Could not fold it.
  1861. return SDValue();
  1862. }
  1863. /// See if the specified operand can be simplified with the knowledge that only
  1864. /// the bits specified by DemandedBits are used.
  1865. /// TODO: really we should be making this into the DAG equivalent of
  1866. /// SimplifyMultipleUseDemandedBits and not generate any new nodes.
  1867. SDValue SelectionDAG::GetDemandedBits(SDValue V, const APInt &DemandedBits) {
  1868. EVT VT = V.getValueType();
  1869. APInt DemandedElts = VT.isVector()
  1870. ? APInt::getAllOnesValue(VT.getVectorNumElements())
  1871. : APInt(1, 1);
  1872. return GetDemandedBits(V, DemandedBits, DemandedElts);
  1873. }
  1874. /// See if the specified operand can be simplified with the knowledge that only
  1875. /// the bits specified by DemandedBits are used in the elements specified by
  1876. /// DemandedElts.
  1877. /// TODO: really we should be making this into the DAG equivalent of
  1878. /// SimplifyMultipleUseDemandedBits and not generate any new nodes.
  1879. SDValue SelectionDAG::GetDemandedBits(SDValue V, const APInt &DemandedBits,
  1880. const APInt &DemandedElts) {
  1881. switch (V.getOpcode()) {
  1882. default:
  1883. break;
  1884. case ISD::Constant: {
  1885. auto *CV = cast<ConstantSDNode>(V.getNode());
  1886. assert(CV && "Const value should be ConstSDNode.");
  1887. const APInt &CVal = CV->getAPIntValue();
  1888. APInt NewVal = CVal & DemandedBits;
  1889. if (NewVal != CVal)
  1890. return getConstant(NewVal, SDLoc(V), V.getValueType());
  1891. break;
  1892. }
  1893. case ISD::OR:
  1894. case ISD::XOR:
  1895. case ISD::SIGN_EXTEND_INREG:
  1896. return TLI->SimplifyMultipleUseDemandedBits(V, DemandedBits, DemandedElts,
  1897. *this, 0);
  1898. case ISD::SRL:
  1899. // Only look at single-use SRLs.
  1900. if (!V.getNode()->hasOneUse())
  1901. break;
  1902. if (auto *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
  1903. // See if we can recursively simplify the LHS.
  1904. unsigned Amt = RHSC->getZExtValue();
  1905. // Watch out for shift count overflow though.
  1906. if (Amt >= DemandedBits.getBitWidth())
  1907. break;
  1908. APInt SrcDemandedBits = DemandedBits << Amt;
  1909. if (SDValue SimplifyLHS =
  1910. GetDemandedBits(V.getOperand(0), SrcDemandedBits))
  1911. return getNode(ISD::SRL, SDLoc(V), V.getValueType(), SimplifyLHS,
  1912. V.getOperand(1));
  1913. }
  1914. break;
  1915. case ISD::AND: {
  1916. // X & -1 -> X (ignoring bits which aren't demanded).
  1917. // Also handle the case where masked out bits in X are known to be zero.
  1918. if (ConstantSDNode *RHSC = isConstOrConstSplat(V.getOperand(1))) {
  1919. const APInt &AndVal = RHSC->getAPIntValue();
  1920. if (DemandedBits.isSubsetOf(AndVal) ||
  1921. DemandedBits.isSubsetOf(computeKnownBits(V.getOperand(0)).Zero |
  1922. AndVal))
  1923. return V.getOperand(0);
  1924. }
  1925. break;
  1926. }
  1927. case ISD::ANY_EXTEND: {
  1928. SDValue Src = V.getOperand(0);
  1929. unsigned SrcBitWidth = Src.getScalarValueSizeInBits();
  1930. // Being conservative here - only peek through if we only demand bits in the
  1931. // non-extended source (even though the extended bits are technically
  1932. // undef).
  1933. if (DemandedBits.getActiveBits() > SrcBitWidth)
  1934. break;
  1935. APInt SrcDemandedBits = DemandedBits.trunc(SrcBitWidth);
  1936. if (SDValue DemandedSrc = GetDemandedBits(Src, SrcDemandedBits))
  1937. return getNode(ISD::ANY_EXTEND, SDLoc(V), V.getValueType(), DemandedSrc);
  1938. break;
  1939. }
  1940. }
  1941. return SDValue();
  1942. }
  1943. /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
  1944. /// use this predicate to simplify operations downstream.
  1945. bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
  1946. unsigned BitWidth = Op.getScalarValueSizeInBits();
  1947. return MaskedValueIsZero(Op, APInt::getSignMask(BitWidth), Depth);
  1948. }
  1949. /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
  1950. /// this predicate to simplify operations downstream. Mask is known to be zero
  1951. /// for bits that V cannot have.
  1952. bool SelectionDAG::MaskedValueIsZero(SDValue V, const APInt &Mask,
  1953. unsigned Depth) const {
  1954. EVT VT = V.getValueType();
  1955. APInt DemandedElts = VT.isVector()
  1956. ? APInt::getAllOnesValue(VT.getVectorNumElements())
  1957. : APInt(1, 1);
  1958. return MaskedValueIsZero(V, Mask, DemandedElts, Depth);
  1959. }
  1960. /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
  1961. /// DemandedElts. We use this predicate to simplify operations downstream.
  1962. /// Mask is known to be zero for bits that V cannot have.
  1963. bool SelectionDAG::MaskedValueIsZero(SDValue V, const APInt &Mask,
  1964. const APInt &DemandedElts,
  1965. unsigned Depth) const {
  1966. return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
  1967. }
  1968. /// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
  1969. bool SelectionDAG::MaskedValueIsAllOnes(SDValue V, const APInt &Mask,
  1970. unsigned Depth) const {
  1971. return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
  1972. }
  1973. /// isSplatValue - Return true if the vector V has the same value
  1974. /// across all DemandedElts.
  1975. bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
  1976. APInt &UndefElts) {
  1977. if (!DemandedElts)
  1978. return false; // No demanded elts, better to assume we don't know anything.
  1979. EVT VT = V.getValueType();
  1980. assert(VT.isVector() && "Vector type expected");
  1981. unsigned NumElts = VT.getVectorNumElements();
  1982. assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
  1983. UndefElts = APInt::getNullValue(NumElts);
  1984. switch (V.getOpcode()) {
  1985. case ISD::BUILD_VECTOR: {
  1986. SDValue Scl;
  1987. for (unsigned i = 0; i != NumElts; ++i) {
  1988. SDValue Op = V.getOperand(i);
  1989. if (Op.isUndef()) {
  1990. UndefElts.setBit(i);
  1991. continue;
  1992. }
  1993. if (!DemandedElts[i])
  1994. continue;
  1995. if (Scl && Scl != Op)
  1996. return false;
  1997. Scl = Op;
  1998. }
  1999. return true;
  2000. }
  2001. case ISD::VECTOR_SHUFFLE: {
  2002. // Check if this is a shuffle node doing a splat.
  2003. // TODO: Do we need to handle shuffle(splat, undef, mask)?
  2004. int SplatIndex = -1;
  2005. ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
  2006. for (int i = 0; i != (int)NumElts; ++i) {
  2007. int M = Mask[i];
  2008. if (M < 0) {
  2009. UndefElts.setBit(i);
  2010. continue;
  2011. }
  2012. if (!DemandedElts[i])
  2013. continue;
  2014. if (0 <= SplatIndex && SplatIndex != M)
  2015. return false;
  2016. SplatIndex = M;
  2017. }
  2018. return true;
  2019. }
  2020. case ISD::EXTRACT_SUBVECTOR: {
  2021. SDValue Src = V.getOperand(0);
  2022. ConstantSDNode *SubIdx = dyn_cast<ConstantSDNode>(V.getOperand(1));
  2023. unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
  2024. if (SubIdx && SubIdx->getAPIntValue().ule(NumSrcElts - NumElts)) {
  2025. // Offset the demanded elts by the subvector index.
  2026. uint64_t Idx = SubIdx->getZExtValue();
  2027. APInt UndefSrcElts;
  2028. APInt DemandedSrc = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
  2029. if (isSplatValue(Src, DemandedSrc, UndefSrcElts)) {
  2030. UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
  2031. return true;
  2032. }
  2033. }
  2034. break;
  2035. }
  2036. case ISD::ADD:
  2037. case ISD::SUB:
  2038. case ISD::AND: {
  2039. APInt UndefLHS, UndefRHS;
  2040. SDValue LHS = V.getOperand(0);
  2041. SDValue RHS = V.getOperand(1);
  2042. if (isSplatValue(LHS, DemandedElts, UndefLHS) &&
  2043. isSplatValue(RHS, DemandedElts, UndefRHS)) {
  2044. UndefElts = UndefLHS | UndefRHS;
  2045. return true;
  2046. }
  2047. break;
  2048. }
  2049. }
  2050. return false;
  2051. }
  2052. /// Helper wrapper to main isSplatValue function.
  2053. bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) {
  2054. EVT VT = V.getValueType();
  2055. assert(VT.isVector() && "Vector type expected");
  2056. unsigned NumElts = VT.getVectorNumElements();
  2057. APInt UndefElts;
  2058. APInt DemandedElts = APInt::getAllOnesValue(NumElts);
  2059. return isSplatValue(V, DemandedElts, UndefElts) &&
  2060. (AllowUndefs || !UndefElts);
  2061. }
  2062. SDValue SelectionDAG::getSplatSourceVector(SDValue V, int &SplatIdx) {
  2063. V = peekThroughExtractSubvectors(V);
  2064. EVT VT = V.getValueType();
  2065. unsigned Opcode = V.getOpcode();
  2066. switch (Opcode) {
  2067. default: {
  2068. APInt UndefElts;
  2069. APInt DemandedElts = APInt::getAllOnesValue(VT.getVectorNumElements());
  2070. if (isSplatValue(V, DemandedElts, UndefElts)) {
  2071. // Handle case where all demanded elements are UNDEF.
  2072. if (DemandedElts.isSubsetOf(UndefElts)) {
  2073. SplatIdx = 0;
  2074. return getUNDEF(VT);
  2075. }
  2076. SplatIdx = (UndefElts & DemandedElts).countTrailingOnes();
  2077. return V;
  2078. }
  2079. break;
  2080. }
  2081. case ISD::VECTOR_SHUFFLE: {
  2082. // Check if this is a shuffle node doing a splat.
  2083. // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
  2084. // getTargetVShiftNode currently struggles without the splat source.
  2085. auto *SVN = cast<ShuffleVectorSDNode>(V);
  2086. if (!SVN->isSplat())
  2087. break;
  2088. int Idx = SVN->getSplatIndex();
  2089. int NumElts = V.getValueType().getVectorNumElements();
  2090. SplatIdx = Idx % NumElts;
  2091. return V.getOperand(Idx / NumElts);
  2092. }
  2093. }
  2094. return SDValue();
  2095. }
  2096. SDValue SelectionDAG::getSplatValue(SDValue V) {
  2097. int SplatIdx;
  2098. if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx))
  2099. return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V),
  2100. SrcVector.getValueType().getScalarType(), SrcVector,
  2101. getIntPtrConstant(SplatIdx, SDLoc(V)));
  2102. return SDValue();
  2103. }
  2104. /// If a SHL/SRA/SRL node has a constant or splat constant shift amount that
  2105. /// is less than the element bit-width of the shift node, return it.
  2106. static const APInt *getValidShiftAmountConstant(SDValue V) {
  2107. if (ConstantSDNode *SA = isConstOrConstSplat(V.getOperand(1))) {
  2108. // Shifting more than the bitwidth is not valid.
  2109. const APInt &ShAmt = SA->getAPIntValue();
  2110. if (ShAmt.ult(V.getScalarValueSizeInBits()))
  2111. return &ShAmt;
  2112. }
  2113. return nullptr;
  2114. }
  2115. /// Determine which bits of Op are known to be either zero or one and return
  2116. /// them in Known. For vectors, the known bits are those that are shared by
  2117. /// every vector element.
  2118. KnownBits SelectionDAG::computeKnownBits(SDValue Op, unsigned Depth) const {
  2119. EVT VT = Op.getValueType();
  2120. APInt DemandedElts = VT.isVector()
  2121. ? APInt::getAllOnesValue(VT.getVectorNumElements())
  2122. : APInt(1, 1);
  2123. return computeKnownBits(Op, DemandedElts, Depth);
  2124. }
  2125. /// Determine which bits of Op are known to be either zero or one and return
  2126. /// them in Known. The DemandedElts argument allows us to only collect the known
  2127. /// bits that are shared by the requested vector elements.
  2128. KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
  2129. unsigned Depth) const {
  2130. unsigned BitWidth = Op.getScalarValueSizeInBits();
  2131. KnownBits Known(BitWidth); // Don't know anything.
  2132. if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
  2133. // We know all of the bits for a constant!
  2134. Known.One = C->getAPIntValue();
  2135. Known.Zero = ~Known.One;
  2136. return Known;
  2137. }
  2138. if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
  2139. // We know all of the bits for a constant fp!
  2140. Known.One = C->getValueAPF().bitcastToAPInt();
  2141. Known.Zero = ~Known.One;
  2142. return Known;
  2143. }
  2144. if (Depth >= 6)
  2145. return Known; // Limit search depth.
  2146. KnownBits Known2;
  2147. unsigned NumElts = DemandedElts.getBitWidth();
  2148. assert((!Op.getValueType().isVector() ||
  2149. NumElts == Op.getValueType().getVectorNumElements()) &&
  2150. "Unexpected vector size");
  2151. if (!DemandedElts)
  2152. return Known; // No demanded elts, better to assume we don't know anything.
  2153. unsigned Opcode = Op.getOpcode();
  2154. switch (Opcode) {
  2155. case ISD::BUILD_VECTOR:
  2156. // Collect the known bits that are shared by every demanded vector element.
  2157. Known.Zero.setAllBits(); Known.One.setAllBits();
  2158. for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
  2159. if (!DemandedElts[i])
  2160. continue;
  2161. SDValue SrcOp = Op.getOperand(i);
  2162. Known2 = computeKnownBits(SrcOp, Depth + 1);
  2163. // BUILD_VECTOR can implicitly truncate sources, we must handle this.
  2164. if (SrcOp.getValueSizeInBits() != BitWidth) {
  2165. assert(SrcOp.getValueSizeInBits() > BitWidth &&
  2166. "Expected BUILD_VECTOR implicit truncation");
  2167. Known2 = Known2.trunc(BitWidth);
  2168. }
  2169. // Known bits are the values that are shared by every demanded element.
  2170. Known.One &= Known2.One;
  2171. Known.Zero &= Known2.Zero;
  2172. // If we don't know any bits, early out.
  2173. if (Known.isUnknown())
  2174. break;
  2175. }
  2176. break;
  2177. case ISD::VECTOR_SHUFFLE: {
  2178. // Collect the known bits that are shared by every vector element referenced
  2179. // by the shuffle.
  2180. APInt DemandedLHS(NumElts, 0), DemandedRHS(NumElts, 0);
  2181. Known.Zero.setAllBits(); Known.One.setAllBits();
  2182. const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
  2183. assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
  2184. for (unsigned i = 0; i != NumElts; ++i) {
  2185. if (!DemandedElts[i])
  2186. continue;
  2187. int M = SVN->getMaskElt(i);
  2188. if (M < 0) {
  2189. // For UNDEF elements, we don't know anything about the common state of
  2190. // the shuffle result.
  2191. Known.resetAll();
  2192. DemandedLHS.clearAllBits();
  2193. DemandedRHS.clearAllBits();
  2194. break;
  2195. }
  2196. if ((unsigned)M < NumElts)
  2197. DemandedLHS.setBit((unsigned)M % NumElts);
  2198. else
  2199. DemandedRHS.setBit((unsigned)M % NumElts);
  2200. }
  2201. // Known bits are the values that are shared by every demanded element.
  2202. if (!!DemandedLHS) {
  2203. SDValue LHS = Op.getOperand(0);
  2204. Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
  2205. Known.One &= Known2.One;
  2206. Known.Zero &= Known2.Zero;
  2207. }
  2208. // If we don't know any bits, early out.
  2209. if (Known.isUnknown())
  2210. break;
  2211. if (!!DemandedRHS) {
  2212. SDValue RHS = Op.getOperand(1);
  2213. Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
  2214. Known.One &= Known2.One;
  2215. Known.Zero &= Known2.Zero;
  2216. }
  2217. break;
  2218. }
  2219. case ISD::CONCAT_VECTORS: {
  2220. // Split DemandedElts and test each of the demanded subvectors.
  2221. Known.Zero.setAllBits(); Known.One.setAllBits();
  2222. EVT SubVectorVT = Op.getOperand(0).getValueType();
  2223. unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
  2224. unsigned NumSubVectors = Op.getNumOperands();
  2225. for (unsigned i = 0; i != NumSubVectors; ++i) {
  2226. APInt DemandedSub = DemandedElts.lshr(i * NumSubVectorElts);
  2227. DemandedSub = DemandedSub.trunc(NumSubVectorElts);
  2228. if (!!DemandedSub) {
  2229. SDValue Sub = Op.getOperand(i);
  2230. Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
  2231. Known.One &= Known2.One;
  2232. Known.Zero &= Known2.Zero;
  2233. }
  2234. // If we don't know any bits, early out.
  2235. if (Known.isUnknown())
  2236. break;
  2237. }
  2238. break;
  2239. }
  2240. case ISD::INSERT_SUBVECTOR: {
  2241. // If we know the element index, demand any elements from the subvector and
  2242. // the remainder from the src its inserted into, otherwise demand them all.
  2243. SDValue Src = Op.getOperand(0);
  2244. SDValue Sub = Op.getOperand(1);
  2245. ConstantSDNode *SubIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2));
  2246. unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
  2247. if (SubIdx && SubIdx->getAPIntValue().ule(NumElts - NumSubElts)) {
  2248. Known.One.setAllBits();
  2249. Known.Zero.setAllBits();
  2250. uint64_t Idx = SubIdx->getZExtValue();
  2251. APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
  2252. if (!!DemandedSubElts) {
  2253. Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
  2254. if (Known.isUnknown())
  2255. break; // early-out.
  2256. }
  2257. APInt SubMask = APInt::getBitsSet(NumElts, Idx, Idx + NumSubElts);
  2258. APInt DemandedSrcElts = DemandedElts & ~SubMask;
  2259. if (!!DemandedSrcElts) {
  2260. Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
  2261. Known.One &= Known2.One;
  2262. Known.Zero &= Known2.Zero;
  2263. }
  2264. } else {
  2265. Known = computeKnownBits(Sub, Depth + 1);
  2266. if (Known.isUnknown())
  2267. break; // early-out.
  2268. Known2 = computeKnownBits(Src, Depth + 1);
  2269. Known.One &= Known2.One;
  2270. Known.Zero &= Known2.Zero;
  2271. }
  2272. break;
  2273. }
  2274. case ISD::EXTRACT_SUBVECTOR: {
  2275. // If we know the element index, just demand that subvector elements,
  2276. // otherwise demand them all.
  2277. SDValue Src = Op.getOperand(0);
  2278. ConstantSDNode *SubIdx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
  2279. unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
  2280. if (SubIdx && SubIdx->getAPIntValue().ule(NumSrcElts - NumElts)) {
  2281. // Offset the demanded elts by the subvector index.
  2282. uint64_t Idx = SubIdx->getZExtValue();
  2283. APInt DemandedSrc = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
  2284. Known = computeKnownBits(Src, DemandedSrc, Depth + 1);
  2285. } else {
  2286. Known = computeKnownBits(Src, Depth + 1);
  2287. }
  2288. break;
  2289. }
  2290. case ISD::SCALAR_TO_VECTOR: {
  2291. // We know about scalar_to_vector as much as we know about it source,
  2292. // which becomes the first element of otherwise unknown vector.
  2293. if (DemandedElts != 1)
  2294. break;
  2295. SDValue N0 = Op.getOperand(0);
  2296. Known = computeKnownBits(N0, Depth + 1);
  2297. if (N0.getValueSizeInBits() != BitWidth)
  2298. Known = Known.trunc(BitWidth);
  2299. break;
  2300. }
  2301. case ISD::BITCAST: {
  2302. SDValue N0 = Op.getOperand(0);
  2303. EVT SubVT = N0.getValueType();
  2304. unsigned SubBitWidth = SubVT.getScalarSizeInBits();
  2305. // Ignore bitcasts from unsupported types.
  2306. if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
  2307. break;
  2308. // Fast handling of 'identity' bitcasts.
  2309. if (BitWidth == SubBitWidth) {
  2310. Known = computeKnownBits(N0, DemandedElts, Depth + 1);
  2311. break;
  2312. }
  2313. bool IsLE = getDataLayout().isLittleEndian();
  2314. // Bitcast 'small element' vector to 'large element' scalar/vector.
  2315. if ((BitWidth % SubBitWidth) == 0) {
  2316. assert(N0.getValueType().isVector() && "Expected bitcast from vector");
  2317. // Collect known bits for the (larger) output by collecting the known
  2318. // bits from each set of sub elements and shift these into place.
  2319. // We need to separately call computeKnownBits for each set of
  2320. // sub elements as the knownbits for each is likely to be different.
  2321. unsigned SubScale = BitWidth / SubBitWidth;
  2322. APInt SubDemandedElts(NumElts * SubScale, 0);
  2323. for (unsigned i = 0; i != NumElts; ++i)
  2324. if (DemandedElts[i])
  2325. SubDemandedElts.setBit(i * SubScale);
  2326. for (unsigned i = 0; i != SubScale; ++i) {
  2327. Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
  2328. Depth + 1);
  2329. unsigned Shifts = IsLE ? i : SubScale - 1 - i;
  2330. Known.One |= Known2.One.zext(BitWidth).shl(SubBitWidth * Shifts);
  2331. Known.Zero |= Known2.Zero.zext(BitWidth).shl(SubBitWidth * Shifts);
  2332. }
  2333. }
  2334. // Bitcast 'large element' scalar/vector to 'small element' vector.
  2335. if ((SubBitWidth % BitWidth) == 0) {
  2336. assert(Op.getValueType().isVector() && "Expected bitcast to vector");
  2337. // Collect known bits for the (smaller) output by collecting the known
  2338. // bits from the overlapping larger input elements and extracting the
  2339. // sub sections we actually care about.
  2340. unsigned SubScale = SubBitWidth / BitWidth;
  2341. APInt SubDemandedElts(NumElts / SubScale, 0);
  2342. for (unsigned i = 0; i != NumElts; ++i)
  2343. if (DemandedElts[i])
  2344. SubDemandedElts.setBit(i / SubScale);
  2345. Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
  2346. Known.Zero.setAllBits(); Known.One.setAllBits();
  2347. for (unsigned i = 0; i != NumElts; ++i)
  2348. if (DemandedElts[i]) {
  2349. unsigned Shifts = IsLE ? i : NumElts - 1 - i;
  2350. unsigned Offset = (Shifts % SubScale) * BitWidth;
  2351. Known.One &= Known2.One.lshr(Offset).trunc(BitWidth);
  2352. Known.Zero &= Known2.Zero.lshr(Offset).trunc(BitWidth);
  2353. // If we don't know any bits, early out.
  2354. if (Known.isUnknown())
  2355. break;
  2356. }
  2357. }
  2358. break;
  2359. }
  2360. case ISD::AND:
  2361. // If either the LHS or the RHS are Zero, the result is zero.
  2362. Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
  2363. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2364. // Output known-1 bits are only known if set in both the LHS & RHS.
  2365. Known.One &= Known2.One;
  2366. // Output known-0 are known to be clear if zero in either the LHS | RHS.
  2367. Known.Zero |= Known2.Zero;
  2368. break;
  2369. case ISD::OR:
  2370. Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
  2371. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2372. // Output known-0 bits are only known if clear in both the LHS & RHS.
  2373. Known.Zero &= Known2.Zero;
  2374. // Output known-1 are known to be set if set in either the LHS | RHS.
  2375. Known.One |= Known2.One;
  2376. break;
  2377. case ISD::XOR: {
  2378. Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
  2379. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2380. // Output known-0 bits are known if clear or set in both the LHS & RHS.
  2381. APInt KnownZeroOut = (Known.Zero & Known2.Zero) | (Known.One & Known2.One);
  2382. // Output known-1 are known to be set if set in only one of the LHS, RHS.
  2383. Known.One = (Known.Zero & Known2.One) | (Known.One & Known2.Zero);
  2384. Known.Zero = KnownZeroOut;
  2385. break;
  2386. }
  2387. case ISD::MUL: {
  2388. Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
  2389. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2390. // If low bits are zero in either operand, output low known-0 bits.
  2391. // Also compute a conservative estimate for high known-0 bits.
  2392. // More trickiness is possible, but this is sufficient for the
  2393. // interesting case of alignment computation.
  2394. unsigned TrailZ = Known.countMinTrailingZeros() +
  2395. Known2.countMinTrailingZeros();
  2396. unsigned LeadZ = std::max(Known.countMinLeadingZeros() +
  2397. Known2.countMinLeadingZeros(),
  2398. BitWidth) - BitWidth;
  2399. Known.resetAll();
  2400. Known.Zero.setLowBits(std::min(TrailZ, BitWidth));
  2401. Known.Zero.setHighBits(std::min(LeadZ, BitWidth));
  2402. break;
  2403. }
  2404. case ISD::UDIV: {
  2405. // For the purposes of computing leading zeros we can conservatively
  2406. // treat a udiv as a logical right shift by the power of 2 known to
  2407. // be less than the denominator.
  2408. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2409. unsigned LeadZ = Known2.countMinLeadingZeros();
  2410. Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
  2411. unsigned RHSMaxLeadingZeros = Known2.countMaxLeadingZeros();
  2412. if (RHSMaxLeadingZeros != BitWidth)
  2413. LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1);
  2414. Known.Zero.setHighBits(LeadZ);
  2415. break;
  2416. }
  2417. case ISD::SELECT:
  2418. case ISD::VSELECT:
  2419. Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
  2420. // If we don't know any bits, early out.
  2421. if (Known.isUnknown())
  2422. break;
  2423. Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
  2424. // Only known if known in both the LHS and RHS.
  2425. Known.One &= Known2.One;
  2426. Known.Zero &= Known2.Zero;
  2427. break;
  2428. case ISD::SELECT_CC:
  2429. Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
  2430. // If we don't know any bits, early out.
  2431. if (Known.isUnknown())
  2432. break;
  2433. Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
  2434. // Only known if known in both the LHS and RHS.
  2435. Known.One &= Known2.One;
  2436. Known.Zero &= Known2.Zero;
  2437. break;
  2438. case ISD::SMULO:
  2439. case ISD::UMULO:
  2440. case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
  2441. if (Op.getResNo() != 1)
  2442. break;
  2443. // The boolean result conforms to getBooleanContents.
  2444. // If we know the result of a setcc has the top bits zero, use this info.
  2445. // We know that we have an integer-based boolean since these operations
  2446. // are only available for integer.
  2447. if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
  2448. TargetLowering::ZeroOrOneBooleanContent &&
  2449. BitWidth > 1)
  2450. Known.Zero.setBitsFrom(1);
  2451. break;
  2452. case ISD::SETCC:
  2453. // If we know the result of a setcc has the top bits zero, use this info.
  2454. if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
  2455. TargetLowering::ZeroOrOneBooleanContent &&
  2456. BitWidth > 1)
  2457. Known.Zero.setBitsFrom(1);
  2458. break;
  2459. case ISD::SHL:
  2460. if (const APInt *ShAmt = getValidShiftAmountConstant(Op)) {
  2461. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2462. unsigned Shift = ShAmt->getZExtValue();
  2463. Known.Zero <<= Shift;
  2464. Known.One <<= Shift;
  2465. // Low bits are known zero.
  2466. Known.Zero.setLowBits(Shift);
  2467. }
  2468. break;
  2469. case ISD::SRL:
  2470. if (const APInt *ShAmt = getValidShiftAmountConstant(Op)) {
  2471. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2472. unsigned Shift = ShAmt->getZExtValue();
  2473. Known.Zero.lshrInPlace(Shift);
  2474. Known.One.lshrInPlace(Shift);
  2475. // High bits are known zero.
  2476. Known.Zero.setHighBits(Shift);
  2477. } else if (auto *BV = dyn_cast<BuildVectorSDNode>(Op.getOperand(1))) {
  2478. // If the shift amount is a vector of constants see if we can bound
  2479. // the number of upper zero bits.
  2480. unsigned ShiftAmountMin = BitWidth;
  2481. for (unsigned i = 0; i != BV->getNumOperands(); ++i) {
  2482. if (auto *C = dyn_cast<ConstantSDNode>(BV->getOperand(i))) {
  2483. const APInt &ShAmt = C->getAPIntValue();
  2484. if (ShAmt.ult(BitWidth)) {
  2485. ShiftAmountMin = std::min<unsigned>(ShiftAmountMin,
  2486. ShAmt.getZExtValue());
  2487. continue;
  2488. }
  2489. }
  2490. // Don't know anything.
  2491. ShiftAmountMin = 0;
  2492. break;
  2493. }
  2494. Known.Zero.setHighBits(ShiftAmountMin);
  2495. }
  2496. break;
  2497. case ISD::SRA:
  2498. if (const APInt *ShAmt = getValidShiftAmountConstant(Op)) {
  2499. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2500. unsigned Shift = ShAmt->getZExtValue();
  2501. // Sign extend known zero/one bit (else is unknown).
  2502. Known.Zero.ashrInPlace(Shift);
  2503. Known.One.ashrInPlace(Shift);
  2504. }
  2505. break;
  2506. case ISD::FSHL:
  2507. case ISD::FSHR:
  2508. if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
  2509. unsigned Amt = C->getAPIntValue().urem(BitWidth);
  2510. // For fshl, 0-shift returns the 1st arg.
  2511. // For fshr, 0-shift returns the 2nd arg.
  2512. if (Amt == 0) {
  2513. Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
  2514. DemandedElts, Depth + 1);
  2515. break;
  2516. }
  2517. // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
  2518. // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
  2519. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2520. Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
  2521. if (Opcode == ISD::FSHL) {
  2522. Known.One <<= Amt;
  2523. Known.Zero <<= Amt;
  2524. Known2.One.lshrInPlace(BitWidth - Amt);
  2525. Known2.Zero.lshrInPlace(BitWidth - Amt);
  2526. } else {
  2527. Known.One <<= BitWidth - Amt;
  2528. Known.Zero <<= BitWidth - Amt;
  2529. Known2.One.lshrInPlace(Amt);
  2530. Known2.Zero.lshrInPlace(Amt);
  2531. }
  2532. Known.One |= Known2.One;
  2533. Known.Zero |= Known2.Zero;
  2534. }
  2535. break;
  2536. case ISD::SIGN_EXTEND_INREG: {
  2537. EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
  2538. unsigned EBits = EVT.getScalarSizeInBits();
  2539. // Sign extension. Compute the demanded bits in the result that are not
  2540. // present in the input.
  2541. APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
  2542. APInt InSignMask = APInt::getSignMask(EBits);
  2543. APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
  2544. // If the sign extended bits are demanded, we know that the sign
  2545. // bit is demanded.
  2546. InSignMask = InSignMask.zext(BitWidth);
  2547. if (NewBits.getBoolValue())
  2548. InputDemandedBits |= InSignMask;
  2549. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2550. Known.One &= InputDemandedBits;
  2551. Known.Zero &= InputDemandedBits;
  2552. // If the sign bit of the input is known set or clear, then we know the
  2553. // top bits of the result.
  2554. if (Known.Zero.intersects(InSignMask)) { // Input sign bit known clear
  2555. Known.Zero |= NewBits;
  2556. Known.One &= ~NewBits;
  2557. } else if (Known.One.intersects(InSignMask)) { // Input sign bit known set
  2558. Known.One |= NewBits;
  2559. Known.Zero &= ~NewBits;
  2560. } else { // Input sign bit unknown
  2561. Known.Zero &= ~NewBits;
  2562. Known.One &= ~NewBits;
  2563. }
  2564. break;
  2565. }
  2566. case ISD::CTTZ:
  2567. case ISD::CTTZ_ZERO_UNDEF: {
  2568. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2569. // If we have a known 1, its position is our upper bound.
  2570. unsigned PossibleTZ = Known2.countMaxTrailingZeros();
  2571. unsigned LowBits = Log2_32(PossibleTZ) + 1;
  2572. Known.Zero.setBitsFrom(LowBits);
  2573. break;
  2574. }
  2575. case ISD::CTLZ:
  2576. case ISD::CTLZ_ZERO_UNDEF: {
  2577. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2578. // If we have a known 1, its position is our upper bound.
  2579. unsigned PossibleLZ = Known2.countMaxLeadingZeros();
  2580. unsigned LowBits = Log2_32(PossibleLZ) + 1;
  2581. Known.Zero.setBitsFrom(LowBits);
  2582. break;
  2583. }
  2584. case ISD::CTPOP: {
  2585. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2586. // If we know some of the bits are zero, they can't be one.
  2587. unsigned PossibleOnes = Known2.countMaxPopulation();
  2588. Known.Zero.setBitsFrom(Log2_32(PossibleOnes) + 1);
  2589. break;
  2590. }
  2591. case ISD::LOAD: {
  2592. LoadSDNode *LD = cast<LoadSDNode>(Op);
  2593. const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
  2594. if (ISD::isNON_EXTLoad(LD) && Cst) {
  2595. // Determine any common known bits from the loaded constant pool value.
  2596. Type *CstTy = Cst->getType();
  2597. if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits()) {
  2598. // If its a vector splat, then we can (quickly) reuse the scalar path.
  2599. // NOTE: We assume all elements match and none are UNDEF.
  2600. if (CstTy->isVectorTy()) {
  2601. if (const Constant *Splat = Cst->getSplatValue()) {
  2602. Cst = Splat;
  2603. CstTy = Cst->getType();
  2604. }
  2605. }
  2606. // TODO - do we need to handle different bitwidths?
  2607. if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
  2608. // Iterate across all vector elements finding common known bits.
  2609. Known.One.setAllBits();
  2610. Known.Zero.setAllBits();
  2611. for (unsigned i = 0; i != NumElts; ++i) {
  2612. if (!DemandedElts[i])
  2613. continue;
  2614. if (Constant *Elt = Cst->getAggregateElement(i)) {
  2615. if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
  2616. const APInt &Value = CInt->getValue();
  2617. Known.One &= Value;
  2618. Known.Zero &= ~Value;
  2619. continue;
  2620. }
  2621. if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
  2622. APInt Value = CFP->getValueAPF().bitcastToAPInt();
  2623. Known.One &= Value;
  2624. Known.Zero &= ~Value;
  2625. continue;
  2626. }
  2627. }
  2628. Known.One.clearAllBits();
  2629. Known.Zero.clearAllBits();
  2630. break;
  2631. }
  2632. } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
  2633. if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
  2634. const APInt &Value = CInt->getValue();
  2635. Known.One = Value;
  2636. Known.Zero = ~Value;
  2637. } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
  2638. APInt Value = CFP->getValueAPF().bitcastToAPInt();
  2639. Known.One = Value;
  2640. Known.Zero = ~Value;
  2641. }
  2642. }
  2643. }
  2644. } else if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
  2645. // If this is a ZEXTLoad and we are looking at the loaded value.
  2646. EVT VT = LD->getMemoryVT();
  2647. unsigned MemBits = VT.getScalarSizeInBits();
  2648. Known.Zero.setBitsFrom(MemBits);
  2649. } else if (const MDNode *Ranges = LD->getRanges()) {
  2650. if (LD->getExtensionType() == ISD::NON_EXTLOAD)
  2651. computeKnownBitsFromRangeMetadata(*Ranges, Known);
  2652. }
  2653. break;
  2654. }
  2655. case ISD::ZERO_EXTEND_VECTOR_INREG: {
  2656. EVT InVT = Op.getOperand(0).getValueType();
  2657. APInt InDemandedElts = DemandedElts.zextOrSelf(InVT.getVectorNumElements());
  2658. Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
  2659. Known = Known.zext(BitWidth, true /* ExtendedBitsAreKnownZero */);
  2660. break;
  2661. }
  2662. case ISD::ZERO_EXTEND: {
  2663. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2664. Known = Known.zext(BitWidth, true /* ExtendedBitsAreKnownZero */);
  2665. break;
  2666. }
  2667. case ISD::SIGN_EXTEND_VECTOR_INREG: {
  2668. EVT InVT = Op.getOperand(0).getValueType();
  2669. APInt InDemandedElts = DemandedElts.zextOrSelf(InVT.getVectorNumElements());
  2670. Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
  2671. // If the sign bit is known to be zero or one, then sext will extend
  2672. // it to the top bits, else it will just zext.
  2673. Known = Known.sext(BitWidth);
  2674. break;
  2675. }
  2676. case ISD::SIGN_EXTEND: {
  2677. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2678. // If the sign bit is known to be zero or one, then sext will extend
  2679. // it to the top bits, else it will just zext.
  2680. Known = Known.sext(BitWidth);
  2681. break;
  2682. }
  2683. case ISD::ANY_EXTEND: {
  2684. Known = computeKnownBits(Op.getOperand(0), Depth+1);
  2685. Known = Known.zext(BitWidth, false /* ExtendedBitsAreKnownZero */);
  2686. break;
  2687. }
  2688. case ISD::TRUNCATE: {
  2689. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2690. Known = Known.trunc(BitWidth);
  2691. break;
  2692. }
  2693. case ISD::AssertZext: {
  2694. EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
  2695. APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
  2696. Known = computeKnownBits(Op.getOperand(0), Depth+1);
  2697. Known.Zero |= (~InMask);
  2698. Known.One &= (~Known.Zero);
  2699. break;
  2700. }
  2701. case ISD::FGETSIGN:
  2702. // All bits are zero except the low bit.
  2703. Known.Zero.setBitsFrom(1);
  2704. break;
  2705. case ISD::USUBO:
  2706. case ISD::SSUBO:
  2707. if (Op.getResNo() == 1) {
  2708. // If we know the result of a setcc has the top bits zero, use this info.
  2709. if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
  2710. TargetLowering::ZeroOrOneBooleanContent &&
  2711. BitWidth > 1)
  2712. Known.Zero.setBitsFrom(1);
  2713. break;
  2714. }
  2715. LLVM_FALLTHROUGH;
  2716. case ISD::SUB:
  2717. case ISD::SUBC: {
  2718. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2719. Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
  2720. Known = KnownBits::computeForAddSub(/* Add */ false, /* NSW */ false,
  2721. Known, Known2);
  2722. break;
  2723. }
  2724. case ISD::UADDO:
  2725. case ISD::SADDO:
  2726. case ISD::ADDCARRY:
  2727. if (Op.getResNo() == 1) {
  2728. // If we know the result of a setcc has the top bits zero, use this info.
  2729. if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
  2730. TargetLowering::ZeroOrOneBooleanContent &&
  2731. BitWidth > 1)
  2732. Known.Zero.setBitsFrom(1);
  2733. break;
  2734. }
  2735. LLVM_FALLTHROUGH;
  2736. case ISD::ADD:
  2737. case ISD::ADDC:
  2738. case ISD::ADDE: {
  2739. assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
  2740. // With ADDE and ADDCARRY, a carry bit may be added in.
  2741. KnownBits Carry(1);
  2742. if (Opcode == ISD::ADDE)
  2743. // Can't track carry from glue, set carry to unknown.
  2744. Carry.resetAll();
  2745. else if (Opcode == ISD::ADDCARRY)
  2746. // TODO: Compute known bits for the carry operand. Not sure if it is worth
  2747. // the trouble (how often will we find a known carry bit). And I haven't
  2748. // tested this very much yet, but something like this might work:
  2749. // Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
  2750. // Carry = Carry.zextOrTrunc(1, false);
  2751. Carry.resetAll();
  2752. else
  2753. Carry.setAllZero();
  2754. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2755. Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
  2756. Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
  2757. break;
  2758. }
  2759. case ISD::SREM:
  2760. if (ConstantSDNode *Rem = isConstOrConstSplat(Op.getOperand(1))) {
  2761. const APInt &RA = Rem->getAPIntValue().abs();
  2762. if (RA.isPowerOf2()) {
  2763. APInt LowBits = RA - 1;
  2764. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2765. // The low bits of the first operand are unchanged by the srem.
  2766. Known.Zero = Known2.Zero & LowBits;
  2767. Known.One = Known2.One & LowBits;
  2768. // If the first operand is non-negative or has all low bits zero, then
  2769. // the upper bits are all zero.
  2770. if (Known2.isNonNegative() || LowBits.isSubsetOf(Known2.Zero))
  2771. Known.Zero |= ~LowBits;
  2772. // If the first operand is negative and not all low bits are zero, then
  2773. // the upper bits are all one.
  2774. if (Known2.isNegative() && LowBits.intersects(Known2.One))
  2775. Known.One |= ~LowBits;
  2776. assert((Known.Zero & Known.One) == 0&&"Bits known to be one AND zero?");
  2777. }
  2778. }
  2779. break;
  2780. case ISD::UREM: {
  2781. if (ConstantSDNode *Rem = isConstOrConstSplat(Op.getOperand(1))) {
  2782. const APInt &RA = Rem->getAPIntValue();
  2783. if (RA.isPowerOf2()) {
  2784. APInt LowBits = (RA - 1);
  2785. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2786. // The upper bits are all zero, the lower ones are unchanged.
  2787. Known.Zero = Known2.Zero | ~LowBits;
  2788. Known.One = Known2.One & LowBits;
  2789. break;
  2790. }
  2791. }
  2792. // Since the result is less than or equal to either operand, any leading
  2793. // zero bits in either operand must also exist in the result.
  2794. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2795. Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
  2796. uint32_t Leaders =
  2797. std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros());
  2798. Known.resetAll();
  2799. Known.Zero.setHighBits(Leaders);
  2800. break;
  2801. }
  2802. case ISD::EXTRACT_ELEMENT: {
  2803. Known = computeKnownBits(Op.getOperand(0), Depth+1);
  2804. const unsigned Index = Op.getConstantOperandVal(1);
  2805. const unsigned EltBitWidth = Op.getValueSizeInBits();
  2806. // Remove low part of known bits mask
  2807. Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
  2808. Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
  2809. // Remove high part of known bit mask
  2810. Known = Known.trunc(EltBitWidth);
  2811. break;
  2812. }
  2813. case ISD::EXTRACT_VECTOR_ELT: {
  2814. SDValue InVec = Op.getOperand(0);
  2815. SDValue EltNo = Op.getOperand(1);
  2816. EVT VecVT = InVec.getValueType();
  2817. const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
  2818. const unsigned NumSrcElts = VecVT.getVectorNumElements();
  2819. // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
  2820. // anything about the extended bits.
  2821. if (BitWidth > EltBitWidth)
  2822. Known = Known.trunc(EltBitWidth);
  2823. ConstantSDNode *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
  2824. if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts)) {
  2825. // If we know the element index, just demand that vector element.
  2826. unsigned Idx = ConstEltNo->getZExtValue();
  2827. APInt DemandedElt = APInt::getOneBitSet(NumSrcElts, Idx);
  2828. Known = computeKnownBits(InVec, DemandedElt, Depth + 1);
  2829. } else {
  2830. // Unknown element index, so ignore DemandedElts and demand them all.
  2831. Known = computeKnownBits(InVec, Depth + 1);
  2832. }
  2833. if (BitWidth > EltBitWidth)
  2834. Known = Known.zext(BitWidth, false /* => any extend */);
  2835. break;
  2836. }
  2837. case ISD::INSERT_VECTOR_ELT: {
  2838. SDValue InVec = Op.getOperand(0);
  2839. SDValue InVal = Op.getOperand(1);
  2840. SDValue EltNo = Op.getOperand(2);
  2841. ConstantSDNode *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
  2842. if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
  2843. // If we know the element index, split the demand between the
  2844. // source vector and the inserted element.
  2845. Known.Zero = Known.One = APInt::getAllOnesValue(BitWidth);
  2846. unsigned EltIdx = CEltNo->getZExtValue();
  2847. // If we demand the inserted element then add its common known bits.
  2848. if (DemandedElts[EltIdx]) {
  2849. Known2 = computeKnownBits(InVal, Depth + 1);
  2850. Known.One &= Known2.One.zextOrTrunc(Known.One.getBitWidth());
  2851. Known.Zero &= Known2.Zero.zextOrTrunc(Known.Zero.getBitWidth());
  2852. }
  2853. // If we demand the source vector then add its common known bits, ensuring
  2854. // that we don't demand the inserted element.
  2855. APInt VectorElts = DemandedElts & ~(APInt::getOneBitSet(NumElts, EltIdx));
  2856. if (!!VectorElts) {
  2857. Known2 = computeKnownBits(InVec, VectorElts, Depth + 1);
  2858. Known.One &= Known2.One;
  2859. Known.Zero &= Known2.Zero;
  2860. }
  2861. } else {
  2862. // Unknown element index, so ignore DemandedElts and demand them all.
  2863. Known = computeKnownBits(InVec, Depth + 1);
  2864. Known2 = computeKnownBits(InVal, Depth + 1);
  2865. Known.One &= Known2.One.zextOrTrunc(Known.One.getBitWidth());
  2866. Known.Zero &= Known2.Zero.zextOrTrunc(Known.Zero.getBitWidth());
  2867. }
  2868. break;
  2869. }
  2870. case ISD::BITREVERSE: {
  2871. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2872. Known.Zero = Known2.Zero.reverseBits();
  2873. Known.One = Known2.One.reverseBits();
  2874. break;
  2875. }
  2876. case ISD::BSWAP: {
  2877. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2878. Known.Zero = Known2.Zero.byteSwap();
  2879. Known.One = Known2.One.byteSwap();
  2880. break;
  2881. }
  2882. case ISD::ABS: {
  2883. Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2884. // If the source's MSB is zero then we know the rest of the bits already.
  2885. if (Known2.isNonNegative()) {
  2886. Known.Zero = Known2.Zero;
  2887. Known.One = Known2.One;
  2888. break;
  2889. }
  2890. // We only know that the absolute values's MSB will be zero iff there is
  2891. // a set bit that isn't the sign bit (otherwise it could be INT_MIN).
  2892. Known2.One.clearSignBit();
  2893. if (Known2.One.getBoolValue()) {
  2894. Known.Zero = APInt::getSignMask(BitWidth);
  2895. break;
  2896. }
  2897. break;
  2898. }
  2899. case ISD::UMIN: {
  2900. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2901. Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
  2902. // UMIN - we know that the result will have the maximum of the
  2903. // known zero leading bits of the inputs.
  2904. unsigned LeadZero = Known.countMinLeadingZeros();
  2905. LeadZero = std::max(LeadZero, Known2.countMinLeadingZeros());
  2906. Known.Zero &= Known2.Zero;
  2907. Known.One &= Known2.One;
  2908. Known.Zero.setHighBits(LeadZero);
  2909. break;
  2910. }
  2911. case ISD::UMAX: {
  2912. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2913. Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
  2914. // UMAX - we know that the result will have the maximum of the
  2915. // known one leading bits of the inputs.
  2916. unsigned LeadOne = Known.countMinLeadingOnes();
  2917. LeadOne = std::max(LeadOne, Known2.countMinLeadingOnes());
  2918. Known.Zero &= Known2.Zero;
  2919. Known.One &= Known2.One;
  2920. Known.One.setHighBits(LeadOne);
  2921. break;
  2922. }
  2923. case ISD::SMIN:
  2924. case ISD::SMAX: {
  2925. // If we have a clamp pattern, we know that the number of sign bits will be
  2926. // the minimum of the clamp min/max range.
  2927. bool IsMax = (Opcode == ISD::SMAX);
  2928. ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
  2929. if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
  2930. if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
  2931. CstHigh =
  2932. isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
  2933. if (CstLow && CstHigh) {
  2934. if (!IsMax)
  2935. std::swap(CstLow, CstHigh);
  2936. const APInt &ValueLow = CstLow->getAPIntValue();
  2937. const APInt &ValueHigh = CstHigh->getAPIntValue();
  2938. if (ValueLow.sle(ValueHigh)) {
  2939. unsigned LowSignBits = ValueLow.getNumSignBits();
  2940. unsigned HighSignBits = ValueHigh.getNumSignBits();
  2941. unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
  2942. if (ValueLow.isNegative() && ValueHigh.isNegative()) {
  2943. Known.One.setHighBits(MinSignBits);
  2944. break;
  2945. }
  2946. if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
  2947. Known.Zero.setHighBits(MinSignBits);
  2948. break;
  2949. }
  2950. }
  2951. }
  2952. // Fallback - just get the shared known bits of the operands.
  2953. Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
  2954. if (Known.isUnknown()) break; // Early-out
  2955. Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
  2956. Known.Zero &= Known2.Zero;
  2957. Known.One &= Known2.One;
  2958. break;
  2959. }
  2960. case ISD::FrameIndex:
  2961. case ISD::TargetFrameIndex:
  2962. TLI->computeKnownBitsForFrameIndex(Op, Known, DemandedElts, *this, Depth);
  2963. break;
  2964. default:
  2965. if (Opcode < ISD::BUILTIN_OP_END)
  2966. break;
  2967. LLVM_FALLTHROUGH;
  2968. case ISD::INTRINSIC_WO_CHAIN:
  2969. case ISD::INTRINSIC_W_CHAIN:
  2970. case ISD::INTRINSIC_VOID:
  2971. // Allow the target to implement this method for its nodes.
  2972. TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
  2973. break;
  2974. }
  2975. assert(!Known.hasConflict() && "Bits known to be one AND zero?");
  2976. return Known;
  2977. }
  2978. SelectionDAG::OverflowKind SelectionDAG::computeOverflowKind(SDValue N0,
  2979. SDValue N1) const {
  2980. // X + 0 never overflow
  2981. if (isNullConstant(N1))
  2982. return OFK_Never;
  2983. KnownBits N1Known = computeKnownBits(N1);
  2984. if (N1Known.Zero.getBoolValue()) {
  2985. KnownBits N0Known = computeKnownBits(N0);
  2986. bool overflow;
  2987. (void)(~N0Known.Zero).uadd_ov(~N1Known.Zero, overflow);
  2988. if (!overflow)
  2989. return OFK_Never;
  2990. }
  2991. // mulhi + 1 never overflow
  2992. if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
  2993. (~N1Known.Zero & 0x01) == ~N1Known.Zero)
  2994. return OFK_Never;
  2995. if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1) {
  2996. KnownBits N0Known = computeKnownBits(N0);
  2997. if ((~N0Known.Zero & 0x01) == ~N0Known.Zero)
  2998. return OFK_Never;
  2999. }
  3000. return OFK_Sometime;
  3001. }
  3002. bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val) const {
  3003. EVT OpVT = Val.getValueType();
  3004. unsigned BitWidth = OpVT.getScalarSizeInBits();
  3005. // Is the constant a known power of 2?
  3006. if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(Val))
  3007. return Const->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
  3008. // A left-shift of a constant one will have exactly one bit set because
  3009. // shifting the bit off the end is undefined.
  3010. if (Val.getOpcode() == ISD::SHL) {
  3011. auto *C = isConstOrConstSplat(Val.getOperand(0));
  3012. if (C && C->getAPIntValue() == 1)
  3013. return true;
  3014. }
  3015. // Similarly, a logical right-shift of a constant sign-bit will have exactly
  3016. // one bit set.
  3017. if (Val.getOpcode() == ISD::SRL) {
  3018. auto *C = isConstOrConstSplat(Val.getOperand(0));
  3019. if (C && C->getAPIntValue().isSignMask())
  3020. return true;
  3021. }
  3022. // Are all operands of a build vector constant powers of two?
  3023. if (Val.getOpcode() == ISD::BUILD_VECTOR)
  3024. if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
  3025. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
  3026. return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
  3027. return false;
  3028. }))
  3029. return true;
  3030. // More could be done here, though the above checks are enough
  3031. // to handle some common cases.
  3032. // Fall back to computeKnownBits to catch other known cases.
  3033. KnownBits Known = computeKnownBits(Val);
  3034. return (Known.countMaxPopulation() == 1) && (Known.countMinPopulation() == 1);
  3035. }
  3036. unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const {
  3037. EVT VT = Op.getValueType();
  3038. APInt DemandedElts = VT.isVector()
  3039. ? APInt::getAllOnesValue(VT.getVectorNumElements())
  3040. : APInt(1, 1);
  3041. return ComputeNumSignBits(Op, DemandedElts, Depth);
  3042. }
  3043. unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
  3044. unsigned Depth) const {
  3045. EVT VT = Op.getValueType();
  3046. assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
  3047. unsigned VTBits = VT.getScalarSizeInBits();
  3048. unsigned NumElts = DemandedElts.getBitWidth();
  3049. unsigned Tmp, Tmp2;
  3050. unsigned FirstAnswer = 1;
  3051. if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
  3052. const APInt &Val = C->getAPIntValue();
  3053. return Val.getNumSignBits();
  3054. }
  3055. if (Depth >= 6)
  3056. return 1; // Limit search depth.
  3057. if (!DemandedElts)
  3058. return 1; // No demanded elts, better to assume we don't know anything.
  3059. unsigned Opcode = Op.getOpcode();
  3060. switch (Opcode) {
  3061. default: break;
  3062. case ISD::AssertSext:
  3063. Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
  3064. return VTBits-Tmp+1;
  3065. case ISD::AssertZext:
  3066. Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
  3067. return VTBits-Tmp;
  3068. case ISD::BUILD_VECTOR:
  3069. Tmp = VTBits;
  3070. for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
  3071. if (!DemandedElts[i])
  3072. continue;
  3073. SDValue SrcOp = Op.getOperand(i);
  3074. Tmp2 = ComputeNumSignBits(Op.getOperand(i), Depth + 1);
  3075. // BUILD_VECTOR can implicitly truncate sources, we must handle this.
  3076. if (SrcOp.getValueSizeInBits() != VTBits) {
  3077. assert(SrcOp.getValueSizeInBits() > VTBits &&
  3078. "Expected BUILD_VECTOR implicit truncation");
  3079. unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
  3080. Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
  3081. }
  3082. Tmp = std::min(Tmp, Tmp2);
  3083. }
  3084. return Tmp;
  3085. case ISD::VECTOR_SHUFFLE: {
  3086. // Collect the minimum number of sign bits that are shared by every vector
  3087. // element referenced by the shuffle.
  3088. APInt DemandedLHS(NumElts, 0), DemandedRHS(NumElts, 0);
  3089. const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
  3090. assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
  3091. for (unsigned i = 0; i != NumElts; ++i) {
  3092. int M = SVN->getMaskElt(i);
  3093. if (!DemandedElts[i])
  3094. continue;
  3095. // For UNDEF elements, we don't know anything about the common state of
  3096. // the shuffle result.
  3097. if (M < 0)
  3098. return 1;
  3099. if ((unsigned)M < NumElts)
  3100. DemandedLHS.setBit((unsigned)M % NumElts);
  3101. else
  3102. DemandedRHS.setBit((unsigned)M % NumElts);
  3103. }
  3104. Tmp = std::numeric_limits<unsigned>::max();
  3105. if (!!DemandedLHS)
  3106. Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
  3107. if (!!DemandedRHS) {
  3108. Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
  3109. Tmp = std::min(Tmp, Tmp2);
  3110. }
  3111. // If we don't know anything, early out and try computeKnownBits fall-back.
  3112. if (Tmp == 1)
  3113. break;
  3114. assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
  3115. return Tmp;
  3116. }
  3117. case ISD::BITCAST: {
  3118. SDValue N0 = Op.getOperand(0);
  3119. EVT SrcVT = N0.getValueType();
  3120. unsigned SrcBits = SrcVT.getScalarSizeInBits();
  3121. // Ignore bitcasts from unsupported types..
  3122. if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
  3123. break;
  3124. // Fast handling of 'identity' bitcasts.
  3125. if (VTBits == SrcBits)
  3126. return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
  3127. bool IsLE = getDataLayout().isLittleEndian();
  3128. // Bitcast 'large element' scalar/vector to 'small element' vector.
  3129. if ((SrcBits % VTBits) == 0) {
  3130. assert(VT.isVector() && "Expected bitcast to vector");
  3131. unsigned Scale = SrcBits / VTBits;
  3132. APInt SrcDemandedElts(NumElts / Scale, 0);
  3133. for (unsigned i = 0; i != NumElts; ++i)
  3134. if (DemandedElts[i])
  3135. SrcDemandedElts.setBit(i / Scale);
  3136. // Fast case - sign splat can be simply split across the small elements.
  3137. Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
  3138. if (Tmp == SrcBits)
  3139. return VTBits;
  3140. // Slow case - determine how far the sign extends into each sub-element.
  3141. Tmp2 = VTBits;
  3142. for (unsigned i = 0; i != NumElts; ++i)
  3143. if (DemandedElts[i]) {
  3144. unsigned SubOffset = i % Scale;
  3145. SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
  3146. SubOffset = SubOffset * VTBits;
  3147. if (Tmp <= SubOffset)
  3148. return 1;
  3149. Tmp2 = std::min(Tmp2, Tmp - SubOffset);
  3150. }
  3151. return Tmp2;
  3152. }
  3153. break;
  3154. }
  3155. case ISD::SIGN_EXTEND:
  3156. Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
  3157. return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
  3158. case ISD::SIGN_EXTEND_INREG:
  3159. // Max of the input and what this extends.
  3160. Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
  3161. Tmp = VTBits-Tmp+1;
  3162. Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
  3163. return std::max(Tmp, Tmp2);
  3164. case ISD::SIGN_EXTEND_VECTOR_INREG: {
  3165. SDValue Src = Op.getOperand(0);
  3166. EVT SrcVT = Src.getValueType();
  3167. APInt DemandedSrcElts = DemandedElts.zextOrSelf(SrcVT.getVectorNumElements());
  3168. Tmp = VTBits - SrcVT.getScalarSizeInBits();
  3169. return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
  3170. }
  3171. case ISD::SRA:
  3172. Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
  3173. // SRA X, C -> adds C sign bits.
  3174. if (ConstantSDNode *C =
  3175. isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
  3176. APInt ShiftVal = C->getAPIntValue();
  3177. ShiftVal += Tmp;
  3178. Tmp = ShiftVal.uge(VTBits) ? VTBits : ShiftVal.getZExtValue();
  3179. }
  3180. return Tmp;
  3181. case ISD::SHL:
  3182. if (ConstantSDNode *C =
  3183. isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
  3184. // shl destroys sign bits.
  3185. Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
  3186. if (C->getAPIntValue().uge(VTBits) || // Bad shift.
  3187. C->getAPIntValue().uge(Tmp)) break; // Shifted all sign bits out.
  3188. return Tmp - C->getZExtValue();
  3189. }
  3190. break;
  3191. case ISD::AND:
  3192. case ISD::OR:
  3193. case ISD::XOR: // NOT is handled here.
  3194. // Logical binary ops preserve the number of sign bits at the worst.
  3195. Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
  3196. if (Tmp != 1) {
  3197. Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
  3198. FirstAnswer = std::min(Tmp, Tmp2);
  3199. // We computed what we know about the sign bits as our first
  3200. // answer. Now proceed to the generic code that uses
  3201. // computeKnownBits, and pick whichever answer is better.
  3202. }
  3203. break;
  3204. case ISD::SELECT:
  3205. case ISD::VSELECT:
  3206. Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
  3207. if (Tmp == 1) return 1; // Early out.
  3208. Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
  3209. return std::min(Tmp, Tmp2);
  3210. case ISD::SELECT_CC:
  3211. Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
  3212. if (Tmp == 1) return 1; // Early out.
  3213. Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
  3214. return std::min(Tmp, Tmp2);
  3215. case ISD::SMIN:
  3216. case ISD::SMAX: {
  3217. // If we have a clamp pattern, we know that the number of sign bits will be
  3218. // the minimum of the clamp min/max range.
  3219. bool IsMax = (Opcode == ISD::SMAX);
  3220. ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
  3221. if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
  3222. if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
  3223. CstHigh =
  3224. isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
  3225. if (CstLow && CstHigh) {
  3226. if (!IsMax)
  3227. std::swap(CstLow, CstHigh);
  3228. if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
  3229. Tmp = CstLow->getAPIntValue().getNumSignBits();
  3230. Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
  3231. return std::min(Tmp, Tmp2);
  3232. }
  3233. }
  3234. // Fallback - just get the minimum number of sign bits of the operands.
  3235. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
  3236. if (Tmp == 1)
  3237. return 1; // Early out.
  3238. Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
  3239. return std::min(Tmp, Tmp2);
  3240. }
  3241. case ISD::UMIN:
  3242. case ISD::UMAX:
  3243. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
  3244. if (Tmp == 1)
  3245. return 1; // Early out.
  3246. Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
  3247. return std::min(Tmp, Tmp2);
  3248. case ISD::SADDO:
  3249. case ISD::UADDO:
  3250. case ISD::SSUBO:
  3251. case ISD::USUBO:
  3252. case ISD::SMULO:
  3253. case ISD::UMULO:
  3254. if (Op.getResNo() != 1)
  3255. break;
  3256. // The boolean result conforms to getBooleanContents. Fall through.
  3257. // If setcc returns 0/-1, all bits are sign bits.
  3258. // We know that we have an integer-based boolean since these operations
  3259. // are only available for integer.
  3260. if (TLI->getBooleanContents(VT.isVector(), false) ==
  3261. TargetLowering::ZeroOrNegativeOneBooleanContent)
  3262. return VTBits;
  3263. break;
  3264. case ISD::SETCC:
  3265. // If setcc returns 0/-1, all bits are sign bits.
  3266. if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
  3267. TargetLowering::ZeroOrNegativeOneBooleanContent)
  3268. return VTBits;
  3269. break;
  3270. case ISD::ROTL:
  3271. case ISD::ROTR:
  3272. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  3273. unsigned RotAmt = C->getAPIntValue().urem(VTBits);
  3274. // Handle rotate right by N like a rotate left by 32-N.
  3275. if (Opcode == ISD::ROTR)
  3276. RotAmt = (VTBits - RotAmt) % VTBits;
  3277. // If we aren't rotating out all of the known-in sign bits, return the
  3278. // number that are left. This handles rotl(sext(x), 1) for example.
  3279. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  3280. if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
  3281. }
  3282. break;
  3283. case ISD::ADD:
  3284. case ISD::ADDC:
  3285. // Add can have at most one carry bit. Thus we know that the output
  3286. // is, at worst, one more bit than the inputs.
  3287. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  3288. if (Tmp == 1) return 1; // Early out.
  3289. // Special case decrementing a value (ADD X, -1):
  3290. if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
  3291. if (CRHS->isAllOnesValue()) {
  3292. KnownBits Known = computeKnownBits(Op.getOperand(0), Depth+1);
  3293. // If the input is known to be 0 or 1, the output is 0/-1, which is all
  3294. // sign bits set.
  3295. if ((Known.Zero | 1).isAllOnesValue())
  3296. return VTBits;
  3297. // If we are subtracting one from a positive number, there is no carry
  3298. // out of the result.
  3299. if (Known.isNonNegative())
  3300. return Tmp;
  3301. }
  3302. Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
  3303. if (Tmp2 == 1) return 1;
  3304. return std::min(Tmp, Tmp2)-1;
  3305. case ISD::SUB:
  3306. Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
  3307. if (Tmp2 == 1) return 1;
  3308. // Handle NEG.
  3309. if (ConstantSDNode *CLHS = isConstOrConstSplat(Op.getOperand(0)))
  3310. if (CLHS->isNullValue()) {
  3311. KnownBits Known = computeKnownBits(Op.getOperand(1), Depth+1);
  3312. // If the input is known to be 0 or 1, the output is 0/-1, which is all
  3313. // sign bits set.
  3314. if ((Known.Zero | 1).isAllOnesValue())
  3315. return VTBits;
  3316. // If the input is known to be positive (the sign bit is known clear),
  3317. // the output of the NEG has the same number of sign bits as the input.
  3318. if (Known.isNonNegative())
  3319. return Tmp2;
  3320. // Otherwise, we treat this like a SUB.
  3321. }
  3322. // Sub can have at most one carry bit. Thus we know that the output
  3323. // is, at worst, one more bit than the inputs.
  3324. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  3325. if (Tmp == 1) return 1; // Early out.
  3326. return std::min(Tmp, Tmp2)-1;
  3327. case ISD::MUL: {
  3328. // The output of the Mul can be at most twice the valid bits in the inputs.
  3329. unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
  3330. if (SignBitsOp0 == 1)
  3331. break;
  3332. unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
  3333. if (SignBitsOp1 == 1)
  3334. break;
  3335. unsigned OutValidBits =
  3336. (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
  3337. return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
  3338. }
  3339. case ISD::TRUNCATE: {
  3340. // Check if the sign bits of source go down as far as the truncated value.
  3341. unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
  3342. unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
  3343. if (NumSrcSignBits > (NumSrcBits - VTBits))
  3344. return NumSrcSignBits - (NumSrcBits - VTBits);
  3345. break;
  3346. }
  3347. case ISD::EXTRACT_ELEMENT: {
  3348. const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  3349. const int BitWidth = Op.getValueSizeInBits();
  3350. const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
  3351. // Get reverse index (starting from 1), Op1 value indexes elements from
  3352. // little end. Sign starts at big end.
  3353. const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
  3354. // If the sign portion ends in our element the subtraction gives correct
  3355. // result. Otherwise it gives either negative or > bitwidth result
  3356. return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
  3357. }
  3358. case ISD::INSERT_VECTOR_ELT: {
  3359. SDValue InVec = Op.getOperand(0);
  3360. SDValue InVal = Op.getOperand(1);
  3361. SDValue EltNo = Op.getOperand(2);
  3362. ConstantSDNode *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
  3363. if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
  3364. // If we know the element index, split the demand between the
  3365. // source vector and the inserted element.
  3366. unsigned EltIdx = CEltNo->getZExtValue();
  3367. // If we demand the inserted element then get its sign bits.
  3368. Tmp = std::numeric_limits<unsigned>::max();
  3369. if (DemandedElts[EltIdx]) {
  3370. // TODO - handle implicit truncation of inserted elements.
  3371. if (InVal.getScalarValueSizeInBits() != VTBits)
  3372. break;
  3373. Tmp = ComputeNumSignBits(InVal, Depth + 1);
  3374. }
  3375. // If we demand the source vector then get its sign bits, and determine
  3376. // the minimum.
  3377. APInt VectorElts = DemandedElts;
  3378. VectorElts.clearBit(EltIdx);
  3379. if (!!VectorElts) {
  3380. Tmp2 = ComputeNumSignBits(InVec, VectorElts, Depth + 1);
  3381. Tmp = std::min(Tmp, Tmp2);
  3382. }
  3383. } else {
  3384. // Unknown element index, so ignore DemandedElts and demand them all.
  3385. Tmp = ComputeNumSignBits(InVec, Depth + 1);
  3386. Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
  3387. Tmp = std::min(Tmp, Tmp2);
  3388. }
  3389. assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
  3390. return Tmp;
  3391. }
  3392. case ISD::EXTRACT_VECTOR_ELT: {
  3393. SDValue InVec = Op.getOperand(0);
  3394. SDValue EltNo = Op.getOperand(1);
  3395. EVT VecVT = InVec.getValueType();
  3396. const unsigned BitWidth = Op.getValueSizeInBits();
  3397. const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
  3398. const unsigned NumSrcElts = VecVT.getVectorNumElements();
  3399. // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
  3400. // anything about sign bits. But if the sizes match we can derive knowledge
  3401. // about sign bits from the vector operand.
  3402. if (BitWidth != EltBitWidth)
  3403. break;
  3404. // If we know the element index, just demand that vector element, else for
  3405. // an unknown element index, ignore DemandedElts and demand them all.
  3406. APInt DemandedSrcElts = APInt::getAllOnesValue(NumSrcElts);
  3407. ConstantSDNode *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
  3408. if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
  3409. DemandedSrcElts =
  3410. APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
  3411. return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
  3412. }
  3413. case ISD::EXTRACT_SUBVECTOR: {
  3414. // If we know the element index, just demand that subvector elements,
  3415. // otherwise demand them all.
  3416. SDValue Src = Op.getOperand(0);
  3417. ConstantSDNode *SubIdx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
  3418. unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
  3419. if (SubIdx && SubIdx->getAPIntValue().ule(NumSrcElts - NumElts)) {
  3420. // Offset the demanded elts by the subvector index.
  3421. uint64_t Idx = SubIdx->getZExtValue();
  3422. APInt DemandedSrc = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
  3423. return ComputeNumSignBits(Src, DemandedSrc, Depth + 1);
  3424. }
  3425. return ComputeNumSignBits(Src, Depth + 1);
  3426. }
  3427. case ISD::CONCAT_VECTORS: {
  3428. // Determine the minimum number of sign bits across all demanded
  3429. // elts of the input vectors. Early out if the result is already 1.
  3430. Tmp = std::numeric_limits<unsigned>::max();
  3431. EVT SubVectorVT = Op.getOperand(0).getValueType();
  3432. unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
  3433. unsigned NumSubVectors = Op.getNumOperands();
  3434. for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
  3435. APInt DemandedSub = DemandedElts.lshr(i * NumSubVectorElts);
  3436. DemandedSub = DemandedSub.trunc(NumSubVectorElts);
  3437. if (!DemandedSub)
  3438. continue;
  3439. Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
  3440. Tmp = std::min(Tmp, Tmp2);
  3441. }
  3442. assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
  3443. return Tmp;
  3444. }
  3445. case ISD::INSERT_SUBVECTOR: {
  3446. // If we know the element index, demand any elements from the subvector and
  3447. // the remainder from the src its inserted into, otherwise demand them all.
  3448. SDValue Src = Op.getOperand(0);
  3449. SDValue Sub = Op.getOperand(1);
  3450. auto *SubIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2));
  3451. unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
  3452. if (SubIdx && SubIdx->getAPIntValue().ule(NumElts - NumSubElts)) {
  3453. Tmp = std::numeric_limits<unsigned>::max();
  3454. uint64_t Idx = SubIdx->getZExtValue();
  3455. APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
  3456. if (!!DemandedSubElts) {
  3457. Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
  3458. if (Tmp == 1) return 1; // early-out
  3459. }
  3460. APInt SubMask = APInt::getBitsSet(NumElts, Idx, Idx + NumSubElts);
  3461. APInt DemandedSrcElts = DemandedElts & ~SubMask;
  3462. if (!!DemandedSrcElts) {
  3463. Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
  3464. Tmp = std::min(Tmp, Tmp2);
  3465. }
  3466. assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
  3467. return Tmp;
  3468. }
  3469. // Not able to determine the index so just assume worst case.
  3470. Tmp = ComputeNumSignBits(Sub, Depth + 1);
  3471. if (Tmp == 1) return 1; // early-out
  3472. Tmp2 = ComputeNumSignBits(Src, Depth + 1);
  3473. Tmp = std::min(Tmp, Tmp2);
  3474. assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
  3475. return Tmp;
  3476. }
  3477. }
  3478. // If we are looking at the loaded value of the SDNode.
  3479. if (Op.getResNo() == 0) {
  3480. // Handle LOADX separately here. EXTLOAD case will fallthrough.
  3481. if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
  3482. unsigned ExtType = LD->getExtensionType();
  3483. switch (ExtType) {
  3484. default: break;
  3485. case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
  3486. Tmp = LD->getMemoryVT().getScalarSizeInBits();
  3487. return VTBits - Tmp + 1;
  3488. case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
  3489. Tmp = LD->getMemoryVT().getScalarSizeInBits();
  3490. return VTBits - Tmp;
  3491. case ISD::NON_EXTLOAD:
  3492. if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
  3493. // We only need to handle vectors - computeKnownBits should handle
  3494. // scalar cases.
  3495. Type *CstTy = Cst->getType();
  3496. if (CstTy->isVectorTy() &&
  3497. (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits()) {
  3498. Tmp = VTBits;
  3499. for (unsigned i = 0; i != NumElts; ++i) {
  3500. if (!DemandedElts[i])
  3501. continue;
  3502. if (Constant *Elt = Cst->getAggregateElement(i)) {
  3503. if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
  3504. const APInt &Value = CInt->getValue();
  3505. Tmp = std::min(Tmp, Value.getNumSignBits());
  3506. continue;
  3507. }
  3508. if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
  3509. APInt Value = CFP->getValueAPF().bitcastToAPInt();
  3510. Tmp = std::min(Tmp, Value.getNumSignBits());
  3511. continue;
  3512. }
  3513. }
  3514. // Unknown type. Conservatively assume no bits match sign bit.
  3515. return 1;
  3516. }
  3517. return Tmp;
  3518. }
  3519. }
  3520. break;
  3521. }
  3522. }
  3523. }
  3524. // Allow the target to implement this method for its nodes.
  3525. if (Opcode >= ISD::BUILTIN_OP_END ||
  3526. Opcode == ISD::INTRINSIC_WO_CHAIN ||
  3527. Opcode == ISD::INTRINSIC_W_CHAIN ||
  3528. Opcode == ISD::INTRINSIC_VOID) {
  3529. unsigned NumBits =
  3530. TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
  3531. if (NumBits > 1)
  3532. FirstAnswer = std::max(FirstAnswer, NumBits);
  3533. }
  3534. // Finally, if we can prove that the top bits of the result are 0's or 1's,
  3535. // use this information.
  3536. KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
  3537. APInt Mask;
  3538. if (Known.isNonNegative()) { // sign bit is 0
  3539. Mask = Known.Zero;
  3540. } else if (Known.isNegative()) { // sign bit is 1;
  3541. Mask = Known.One;
  3542. } else {
  3543. // Nothing known.
  3544. return FirstAnswer;
  3545. }
  3546. // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
  3547. // the number of identical bits in the top of the input value.
  3548. Mask = ~Mask;
  3549. Mask <<= Mask.getBitWidth()-VTBits;
  3550. // Return # leading zeros. We use 'min' here in case Val was zero before
  3551. // shifting. We don't want to return '64' as for an i32 "0".
  3552. return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
  3553. }
  3554. bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
  3555. if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
  3556. !isa<ConstantSDNode>(Op.getOperand(1)))
  3557. return false;
  3558. if (Op.getOpcode() == ISD::OR &&
  3559. !MaskedValueIsZero(Op.getOperand(0), Op.getConstantOperandAPInt(1)))
  3560. return false;
  3561. return true;
  3562. }
  3563. bool SelectionDAG::isKnownNeverNaN(SDValue Op, bool SNaN, unsigned Depth) const {
  3564. // If we're told that NaNs won't happen, assume they won't.
  3565. if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
  3566. return true;
  3567. if (Depth >= 6)
  3568. return false; // Limit search depth.
  3569. // TODO: Handle vectors.
  3570. // If the value is a constant, we can obviously see if it is a NaN or not.
  3571. if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
  3572. return !C->getValueAPF().isNaN() ||
  3573. (SNaN && !C->getValueAPF().isSignaling());
  3574. }
  3575. unsigned Opcode = Op.getOpcode();
  3576. switch (Opcode) {
  3577. case ISD::FADD:
  3578. case ISD::FSUB:
  3579. case ISD::FMUL:
  3580. case ISD::FDIV:
  3581. case ISD::FREM:
  3582. case ISD::FSIN:
  3583. case ISD::FCOS: {
  3584. if (SNaN)
  3585. return true;
  3586. // TODO: Need isKnownNeverInfinity
  3587. return false;
  3588. }
  3589. case ISD::FCANONICALIZE:
  3590. case ISD::FEXP:
  3591. case ISD::FEXP2:
  3592. case ISD::FTRUNC:
  3593. case ISD::FFLOOR:
  3594. case ISD::FCEIL:
  3595. case ISD::FROUND:
  3596. case ISD::FRINT:
  3597. case ISD::FNEARBYINT: {
  3598. if (SNaN)
  3599. return true;
  3600. return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
  3601. }
  3602. case ISD::FABS:
  3603. case ISD::FNEG:
  3604. case ISD::FCOPYSIGN: {
  3605. return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
  3606. }
  3607. case ISD::SELECT:
  3608. return isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
  3609. isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
  3610. case ISD::FP_EXTEND:
  3611. case ISD::FP_ROUND: {
  3612. if (SNaN)
  3613. return true;
  3614. return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
  3615. }
  3616. case ISD::SINT_TO_FP:
  3617. case ISD::UINT_TO_FP:
  3618. return true;
  3619. case ISD::FMA:
  3620. case ISD::FMAD: {
  3621. if (SNaN)
  3622. return true;
  3623. return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
  3624. isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
  3625. isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
  3626. }
  3627. case ISD::FSQRT: // Need is known positive
  3628. case ISD::FLOG:
  3629. case ISD::FLOG2:
  3630. case ISD::FLOG10:
  3631. case ISD::FPOWI:
  3632. case ISD::FPOW: {
  3633. if (SNaN)
  3634. return true;
  3635. // TODO: Refine on operand
  3636. return false;
  3637. }
  3638. case ISD::FMINNUM:
  3639. case ISD::FMAXNUM: {
  3640. // Only one needs to be known not-nan, since it will be returned if the
  3641. // other ends up being one.
  3642. return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) ||
  3643. isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
  3644. }
  3645. case ISD::FMINNUM_IEEE:
  3646. case ISD::FMAXNUM_IEEE: {
  3647. if (SNaN)
  3648. return true;
  3649. // This can return a NaN if either operand is an sNaN, or if both operands
  3650. // are NaN.
  3651. return (isKnownNeverNaN(Op.getOperand(0), false, Depth + 1) &&
  3652. isKnownNeverSNaN(Op.getOperand(1), Depth + 1)) ||
  3653. (isKnownNeverNaN(Op.getOperand(1), false, Depth + 1) &&
  3654. isKnownNeverSNaN(Op.getOperand(0), Depth + 1));
  3655. }
  3656. case ISD::FMINIMUM:
  3657. case ISD::FMAXIMUM: {
  3658. // TODO: Does this quiet or return the origina NaN as-is?
  3659. return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
  3660. isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
  3661. }
  3662. case ISD::EXTRACT_VECTOR_ELT: {
  3663. return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
  3664. }
  3665. default:
  3666. if (Opcode >= ISD::BUILTIN_OP_END ||
  3667. Opcode == ISD::INTRINSIC_WO_CHAIN ||
  3668. Opcode == ISD::INTRINSIC_W_CHAIN ||
  3669. Opcode == ISD::INTRINSIC_VOID) {
  3670. return TLI->isKnownNeverNaNForTargetNode(Op, *this, SNaN, Depth);
  3671. }
  3672. return false;
  3673. }
  3674. }
  3675. bool SelectionDAG::isKnownNeverZeroFloat(SDValue Op) const {
  3676. assert(Op.getValueType().isFloatingPoint() &&
  3677. "Floating point type expected");
  3678. // If the value is a constant, we can obviously see if it is a zero or not.
  3679. // TODO: Add BuildVector support.
  3680. if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
  3681. return !C->isZero();
  3682. return false;
  3683. }
  3684. bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
  3685. assert(!Op.getValueType().isFloatingPoint() &&
  3686. "Floating point types unsupported - use isKnownNeverZeroFloat");
  3687. // If the value is a constant, we can obviously see if it is a zero or not.
  3688. if (ISD::matchUnaryPredicate(
  3689. Op, [](ConstantSDNode *C) { return !C->isNullValue(); }))
  3690. return true;
  3691. // TODO: Recognize more cases here.
  3692. switch (Op.getOpcode()) {
  3693. default: break;
  3694. case ISD::OR:
  3695. if (isKnownNeverZero(Op.getOperand(1)) ||
  3696. isKnownNeverZero(Op.getOperand(0)))
  3697. return true;
  3698. break;
  3699. }
  3700. return false;
  3701. }
  3702. bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
  3703. // Check the obvious case.
  3704. if (A == B) return true;
  3705. // For for negative and positive zero.
  3706. if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
  3707. if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
  3708. if (CA->isZero() && CB->isZero()) return true;
  3709. // Otherwise they may not be equal.
  3710. return false;
  3711. }
  3712. // FIXME: unify with llvm::haveNoCommonBitsSet.
  3713. // FIXME: could also handle masked merge pattern (X & ~M) op (Y & M)
  3714. bool SelectionDAG::haveNoCommonBitsSet(SDValue A, SDValue B) const {
  3715. assert(A.getValueType() == B.getValueType() &&
  3716. "Values must have the same type");
  3717. return (computeKnownBits(A).Zero | computeKnownBits(B).Zero).isAllOnesValue();
  3718. }
  3719. static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT,
  3720. ArrayRef<SDValue> Ops,
  3721. SelectionDAG &DAG) {
  3722. int NumOps = Ops.size();
  3723. assert(NumOps != 0 && "Can't build an empty vector!");
  3724. assert(VT.getVectorNumElements() == (unsigned)NumOps &&
  3725. "Incorrect element count in BUILD_VECTOR!");
  3726. // BUILD_VECTOR of UNDEFs is UNDEF.
  3727. if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
  3728. return DAG.getUNDEF(VT);
  3729. // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
  3730. SDValue IdentitySrc;
  3731. bool IsIdentity = true;
  3732. for (int i = 0; i != NumOps; ++i) {
  3733. if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
  3734. Ops[i].getOperand(0).getValueType() != VT ||
  3735. (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
  3736. !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
  3737. cast<ConstantSDNode>(Ops[i].getOperand(1))->getAPIntValue() != i) {
  3738. IsIdentity = false;
  3739. break;
  3740. }
  3741. IdentitySrc = Ops[i].getOperand(0);
  3742. }
  3743. if (IsIdentity)
  3744. return IdentitySrc;
  3745. return SDValue();
  3746. }
  3747. /// Try to simplify vector concatenation to an input value, undef, or build
  3748. /// vector.
  3749. static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT,
  3750. ArrayRef<SDValue> Ops,
  3751. SelectionDAG &DAG) {
  3752. assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
  3753. assert(llvm::all_of(Ops,
  3754. [Ops](SDValue Op) {
  3755. return Ops[0].getValueType() == Op.getValueType();
  3756. }) &&
  3757. "Concatenation of vectors with inconsistent value types!");
  3758. assert((Ops.size() * Ops[0].getValueType().getVectorNumElements()) ==
  3759. VT.getVectorNumElements() &&
  3760. "Incorrect element count in vector concatenation!");
  3761. if (Ops.size() == 1)
  3762. return Ops[0];
  3763. // Concat of UNDEFs is UNDEF.
  3764. if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
  3765. return DAG.getUNDEF(VT);
  3766. // Scan the operands and look for extract operations from a single source
  3767. // that correspond to insertion at the same location via this concatenation:
  3768. // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
  3769. SDValue IdentitySrc;
  3770. bool IsIdentity = true;
  3771. for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
  3772. SDValue Op = Ops[i];
  3773. unsigned IdentityIndex = i * Op.getValueType().getVectorNumElements();
  3774. if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
  3775. Op.getOperand(0).getValueType() != VT ||
  3776. (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
  3777. !isa<ConstantSDNode>(Op.getOperand(1)) ||
  3778. Op.getConstantOperandVal(1) != IdentityIndex) {
  3779. IsIdentity = false;
  3780. break;
  3781. }
  3782. assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
  3783. "Unexpected identity source vector for concat of extracts");
  3784. IdentitySrc = Op.getOperand(0);
  3785. }
  3786. if (IsIdentity) {
  3787. assert(IdentitySrc && "Failed to set source vector of extracts");
  3788. return IdentitySrc;
  3789. }
  3790. // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
  3791. // simplified to one big BUILD_VECTOR.
  3792. // FIXME: Add support for SCALAR_TO_VECTOR as well.
  3793. EVT SVT = VT.getScalarType();
  3794. SmallVector<SDValue, 16> Elts;
  3795. for (SDValue Op : Ops) {
  3796. EVT OpVT = Op.getValueType();
  3797. if (Op.isUndef())
  3798. Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
  3799. else if (Op.getOpcode() == ISD::BUILD_VECTOR)
  3800. Elts.append(Op->op_begin(), Op->op_end());
  3801. else
  3802. return SDValue();
  3803. }
  3804. // BUILD_VECTOR requires all inputs to be of the same type, find the
  3805. // maximum type and extend them all.
  3806. for (SDValue Op : Elts)
  3807. SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
  3808. if (SVT.bitsGT(VT.getScalarType()))
  3809. for (SDValue &Op : Elts)
  3810. Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
  3811. ? DAG.getZExtOrTrunc(Op, DL, SVT)
  3812. : DAG.getSExtOrTrunc(Op, DL, SVT);
  3813. SDValue V = DAG.getBuildVector(VT, DL, Elts);
  3814. NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
  3815. return V;
  3816. }
  3817. /// Gets or creates the specified node.
  3818. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
  3819. FoldingSetNodeID ID;
  3820. AddNodeIDNode(ID, Opcode, getVTList(VT), None);
  3821. void *IP = nullptr;
  3822. if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
  3823. return SDValue(E, 0);
  3824. auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(),
  3825. getVTList(VT));
  3826. CSEMap.InsertNode(N, IP);
  3827. InsertNode(N);
  3828. SDValue V = SDValue(N, 0);
  3829. NewSDValueDbgMsg(V, "Creating new node: ", this);
  3830. return V;
  3831. }
  3832. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
  3833. SDValue Operand, const SDNodeFlags Flags) {
  3834. // Constant fold unary operations with an integer constant operand. Even
  3835. // opaque constant will be folded, because the folding of unary operations
  3836. // doesn't create new constants with different values. Nevertheless, the
  3837. // opaque flag is preserved during folding to prevent future folding with
  3838. // other constants.
  3839. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand)) {
  3840. const APInt &Val = C->getAPIntValue();
  3841. switch (Opcode) {
  3842. default: break;
  3843. case ISD::SIGN_EXTEND:
  3844. return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
  3845. C->isTargetOpcode(), C->isOpaque());
  3846. case ISD::TRUNCATE:
  3847. if (C->isOpaque())
  3848. break;
  3849. LLVM_FALLTHROUGH;
  3850. case ISD::ANY_EXTEND:
  3851. case ISD::ZERO_EXTEND:
  3852. return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
  3853. C->isTargetOpcode(), C->isOpaque());
  3854. case ISD::UINT_TO_FP:
  3855. case ISD::SINT_TO_FP: {
  3856. APFloat apf(EVTToAPFloatSemantics(VT),
  3857. APInt::getNullValue(VT.getSizeInBits()));
  3858. (void)apf.convertFromAPInt(Val,
  3859. Opcode==ISD::SINT_TO_FP,
  3860. APFloat::rmNearestTiesToEven);
  3861. return getConstantFP(apf, DL, VT);
  3862. }
  3863. case ISD::BITCAST:
  3864. if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
  3865. return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
  3866. if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
  3867. return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
  3868. if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
  3869. return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
  3870. if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
  3871. return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
  3872. break;
  3873. case ISD::ABS:
  3874. return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
  3875. C->isOpaque());
  3876. case ISD::BITREVERSE:
  3877. return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
  3878. C->isOpaque());
  3879. case ISD::BSWAP:
  3880. return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
  3881. C->isOpaque());
  3882. case ISD::CTPOP:
  3883. return getConstant(Val.countPopulation(), DL, VT, C->isTargetOpcode(),
  3884. C->isOpaque());
  3885. case ISD::CTLZ:
  3886. case ISD::CTLZ_ZERO_UNDEF:
  3887. return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(),
  3888. C->isOpaque());
  3889. case ISD::CTTZ:
  3890. case ISD::CTTZ_ZERO_UNDEF:
  3891. return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(),
  3892. C->isOpaque());
  3893. case ISD::FP16_TO_FP: {
  3894. bool Ignored;
  3895. APFloat FPV(APFloat::IEEEhalf(),
  3896. (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
  3897. // This can return overflow, underflow, or inexact; we don't care.
  3898. // FIXME need to be more flexible about rounding mode.
  3899. (void)FPV.convert(EVTToAPFloatSemantics(VT),
  3900. APFloat::rmNearestTiesToEven, &Ignored);
  3901. return getConstantFP(FPV, DL, VT);
  3902. }
  3903. }
  3904. }
  3905. // Constant fold unary operations with a floating point constant operand.
  3906. if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand)) {
  3907. APFloat V = C->getValueAPF(); // make copy
  3908. switch (Opcode) {
  3909. case ISD::FNEG:
  3910. V.changeSign();
  3911. return getConstantFP(V, DL, VT);
  3912. case ISD::FABS:
  3913. V.clearSign();
  3914. return getConstantFP(V, DL, VT);
  3915. case ISD::FCEIL: {
  3916. APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
  3917. if (fs == APFloat::opOK || fs == APFloat::opInexact)
  3918. return getConstantFP(V, DL, VT);
  3919. break;
  3920. }
  3921. case ISD::FTRUNC: {
  3922. APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
  3923. if (fs == APFloat::opOK || fs == APFloat::opInexact)
  3924. return getConstantFP(V, DL, VT);
  3925. break;
  3926. }
  3927. case ISD::FFLOOR: {
  3928. APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
  3929. if (fs == APFloat::opOK || fs == APFloat::opInexact)
  3930. return getConstantFP(V, DL, VT);
  3931. break;
  3932. }
  3933. case ISD::FP_EXTEND: {
  3934. bool ignored;
  3935. // This can return overflow, underflow, or inexact; we don't care.
  3936. // FIXME need to be more flexible about rounding mode.
  3937. (void)V.convert(EVTToAPFloatSemantics(VT),
  3938. APFloat::rmNearestTiesToEven, &ignored);
  3939. return getConstantFP(V, DL, VT);
  3940. }
  3941. case ISD::FP_TO_SINT:
  3942. case ISD::FP_TO_UINT: {
  3943. bool ignored;
  3944. APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
  3945. // FIXME need to be more flexible about rounding mode.
  3946. APFloat::opStatus s =
  3947. V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
  3948. if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
  3949. break;
  3950. return getConstant(IntVal, DL, VT);
  3951. }
  3952. case ISD::BITCAST:
  3953. if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
  3954. return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
  3955. else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
  3956. return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
  3957. else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
  3958. return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
  3959. break;
  3960. case ISD::FP_TO_FP16: {
  3961. bool Ignored;
  3962. // This can return overflow, underflow, or inexact; we don't care.
  3963. // FIXME need to be more flexible about rounding mode.
  3964. (void)V.convert(APFloat::IEEEhalf(),
  3965. APFloat::rmNearestTiesToEven, &Ignored);
  3966. return getConstant(V.bitcastToAPInt(), DL, VT);
  3967. }
  3968. }
  3969. }
  3970. // Constant fold unary operations with a vector integer or float operand.
  3971. if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand)) {
  3972. if (BV->isConstant()) {
  3973. switch (Opcode) {
  3974. default:
  3975. // FIXME: Entirely reasonable to perform folding of other unary
  3976. // operations here as the need arises.
  3977. break;
  3978. case ISD::FNEG:
  3979. case ISD::FABS:
  3980. case ISD::FCEIL:
  3981. case ISD::FTRUNC:
  3982. case ISD::FFLOOR:
  3983. case ISD::FP_EXTEND:
  3984. case ISD::FP_TO_SINT:
  3985. case ISD::FP_TO_UINT:
  3986. case ISD::TRUNCATE:
  3987. case ISD::ANY_EXTEND:
  3988. case ISD::ZERO_EXTEND:
  3989. case ISD::SIGN_EXTEND:
  3990. case ISD::UINT_TO_FP:
  3991. case ISD::SINT_TO_FP:
  3992. case ISD::ABS:
  3993. case ISD::BITREVERSE:
  3994. case ISD::BSWAP:
  3995. case ISD::CTLZ:
  3996. case ISD::CTLZ_ZERO_UNDEF:
  3997. case ISD::CTTZ:
  3998. case ISD::CTTZ_ZERO_UNDEF:
  3999. case ISD::CTPOP: {
  4000. SDValue Ops = { Operand };
  4001. if (SDValue Fold = FoldConstantVectorArithmetic(Opcode, DL, VT, Ops))
  4002. return Fold;
  4003. }
  4004. }
  4005. }
  4006. }
  4007. unsigned OpOpcode = Operand.getNode()->getOpcode();
  4008. switch (Opcode) {
  4009. case ISD::TokenFactor:
  4010. case ISD::MERGE_VALUES:
  4011. case ISD::CONCAT_VECTORS:
  4012. return Operand; // Factor, merge or concat of one node? No need.
  4013. case ISD::BUILD_VECTOR: {
  4014. // Attempt to simplify BUILD_VECTOR.
  4015. SDValue Ops[] = {Operand};
  4016. if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
  4017. return V;
  4018. break;
  4019. }
  4020. case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
  4021. case ISD::FP_EXTEND:
  4022. assert(VT.isFloatingPoint() &&
  4023. Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
  4024. if (Operand.getValueType() == VT) return Operand; // noop conversion.
  4025. assert((!VT.isVector() ||
  4026. VT.getVectorNumElements() ==
  4027. Operand.getValueType().getVectorNumElements()) &&
  4028. "Vector element count mismatch!");
  4029. assert(Operand.getValueType().bitsLT(VT) &&
  4030. "Invalid fpext node, dst < src!");
  4031. if (Operand.isUndef())
  4032. return getUNDEF(VT);
  4033. break;
  4034. case ISD::FP_TO_SINT:
  4035. case ISD::FP_TO_UINT:
  4036. if (Operand.isUndef())
  4037. return getUNDEF(VT);
  4038. break;
  4039. case ISD::SINT_TO_FP:
  4040. case ISD::UINT_TO_FP:
  4041. // [us]itofp(undef) = 0, because the result value is bounded.
  4042. if (Operand.isUndef())
  4043. return getConstantFP(0.0, DL, VT);
  4044. break;
  4045. case ISD::SIGN_EXTEND:
  4046. assert(VT.isInteger() && Operand.getValueType().isInteger() &&
  4047. "Invalid SIGN_EXTEND!");
  4048. assert(VT.isVector() == Operand.getValueType().isVector() &&
  4049. "SIGN_EXTEND result type type should be vector iff the operand "
  4050. "type is vector!");
  4051. if (Operand.getValueType() == VT) return Operand; // noop extension
  4052. assert((!VT.isVector() ||
  4053. VT.getVectorNumElements() ==
  4054. Operand.getValueType().getVectorNumElements()) &&
  4055. "Vector element count mismatch!");
  4056. assert(Operand.getValueType().bitsLT(VT) &&
  4057. "Invalid sext node, dst < src!");
  4058. if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
  4059. return getNode(OpOpcode, DL, VT, Operand.getOperand(0));
  4060. else if (OpOpcode == ISD::UNDEF)
  4061. // sext(undef) = 0, because the top bits will all be the same.
  4062. return getConstant(0, DL, VT);
  4063. break;
  4064. case ISD::ZERO_EXTEND:
  4065. assert(VT.isInteger() && Operand.getValueType().isInteger() &&
  4066. "Invalid ZERO_EXTEND!");
  4067. assert(VT.isVector() == Operand.getValueType().isVector() &&
  4068. "ZERO_EXTEND result type type should be vector iff the operand "
  4069. "type is vector!");
  4070. if (Operand.getValueType() == VT) return Operand; // noop extension
  4071. assert((!VT.isVector() ||
  4072. VT.getVectorNumElements() ==
  4073. Operand.getValueType().getVectorNumElements()) &&
  4074. "Vector element count mismatch!");
  4075. assert(Operand.getValueType().bitsLT(VT) &&
  4076. "Invalid zext node, dst < src!");
  4077. if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
  4078. return getNode(ISD::ZERO_EXTEND, DL, VT, Operand.getOperand(0));
  4079. else if (OpOpcode == ISD::UNDEF)
  4080. // zext(undef) = 0, because the top bits will be zero.
  4081. return getConstant(0, DL, VT);
  4082. break;
  4083. case ISD::ANY_EXTEND:
  4084. assert(VT.isInteger() && Operand.getValueType().isInteger() &&
  4085. "Invalid ANY_EXTEND!");
  4086. assert(VT.isVector() == Operand.getValueType().isVector() &&
  4087. "ANY_EXTEND result type type should be vector iff the operand "
  4088. "type is vector!");
  4089. if (Operand.getValueType() == VT) return Operand; // noop extension
  4090. assert((!VT.isVector() ||
  4091. VT.getVectorNumElements() ==
  4092. Operand.getValueType().getVectorNumElements()) &&
  4093. "Vector element count mismatch!");
  4094. assert(Operand.getValueType().bitsLT(VT) &&
  4095. "Invalid anyext node, dst < src!");
  4096. if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
  4097. OpOpcode == ISD::ANY_EXTEND)
  4098. // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
  4099. return getNode(OpOpcode, DL, VT, Operand.getOperand(0));
  4100. else if (OpOpcode == ISD::UNDEF)
  4101. return getUNDEF(VT);
  4102. // (ext (trunc x)) -> x
  4103. if (OpOpcode == ISD::TRUNCATE) {
  4104. SDValue OpOp = Operand.getOperand(0);
  4105. if (OpOp.getValueType() == VT) {
  4106. transferDbgValues(Operand, OpOp);
  4107. return OpOp;
  4108. }
  4109. }
  4110. break;
  4111. case ISD::TRUNCATE:
  4112. assert(VT.isInteger() && Operand.getValueType().isInteger() &&
  4113. "Invalid TRUNCATE!");
  4114. assert(VT.isVector() == Operand.getValueType().isVector() &&
  4115. "TRUNCATE result type type should be vector iff the operand "
  4116. "type is vector!");
  4117. if (Operand.getValueType() == VT) return Operand; // noop truncate
  4118. assert((!VT.isVector() ||
  4119. VT.getVectorNumElements() ==
  4120. Operand.getValueType().getVectorNumElements()) &&
  4121. "Vector element count mismatch!");
  4122. assert(Operand.getValueType().bitsGT(VT) &&
  4123. "Invalid truncate node, src < dst!");
  4124. if (OpOpcode == ISD::TRUNCATE)
  4125. return getNode(ISD::TRUNCATE, DL, VT, Operand.getOperand(0));
  4126. if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
  4127. OpOpcode == ISD::ANY_EXTEND) {
  4128. // If the source is smaller than the dest, we still need an extend.
  4129. if (Operand.getOperand(0).getValueType().getScalarType()
  4130. .bitsLT(VT.getScalarType()))
  4131. return getNode(OpOpcode, DL, VT, Operand.getOperand(0));
  4132. if (Operand.getOperand(0).getValueType().bitsGT(VT))
  4133. return getNode(ISD::TRUNCATE, DL, VT, Operand.getOperand(0));
  4134. return Operand.getOperand(0);
  4135. }
  4136. if (OpOpcode == ISD::UNDEF)
  4137. return getUNDEF(VT);
  4138. break;
  4139. case ISD::ANY_EXTEND_VECTOR_INREG:
  4140. case ISD::ZERO_EXTEND_VECTOR_INREG:
  4141. case ISD::SIGN_EXTEND_VECTOR_INREG:
  4142. assert(VT.isVector() && "This DAG node is restricted to vector types.");
  4143. assert(Operand.getValueType().bitsLE(VT) &&
  4144. "The input must be the same size or smaller than the result.");
  4145. assert(VT.getVectorNumElements() <
  4146. Operand.getValueType().getVectorNumElements() &&
  4147. "The destination vector type must have fewer lanes than the input.");
  4148. break;
  4149. case ISD::ABS:
  4150. assert(VT.isInteger() && VT == Operand.getValueType() &&
  4151. "Invalid ABS!");
  4152. if (OpOpcode == ISD::UNDEF)
  4153. return getUNDEF(VT);
  4154. break;
  4155. case ISD::BSWAP:
  4156. assert(VT.isInteger() && VT == Operand.getValueType() &&
  4157. "Invalid BSWAP!");
  4158. assert((VT.getScalarSizeInBits() % 16 == 0) &&
  4159. "BSWAP types must be a multiple of 16 bits!");
  4160. if (OpOpcode == ISD::UNDEF)
  4161. return getUNDEF(VT);
  4162. break;
  4163. case ISD::BITREVERSE:
  4164. assert(VT.isInteger() && VT == Operand.getValueType() &&
  4165. "Invalid BITREVERSE!");
  4166. if (OpOpcode == ISD::UNDEF)
  4167. return getUNDEF(VT);
  4168. break;
  4169. case ISD::BITCAST:
  4170. // Basic sanity checking.
  4171. assert(VT.getSizeInBits() == Operand.getValueSizeInBits() &&
  4172. "Cannot BITCAST between types of different sizes!");
  4173. if (VT == Operand.getValueType()) return Operand; // noop conversion.
  4174. if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
  4175. return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
  4176. if (OpOpcode == ISD::UNDEF)
  4177. return getUNDEF(VT);
  4178. break;
  4179. case ISD::SCALAR_TO_VECTOR:
  4180. assert(VT.isVector() && !Operand.getValueType().isVector() &&
  4181. (VT.getVectorElementType() == Operand.getValueType() ||
  4182. (VT.getVectorElementType().isInteger() &&
  4183. Operand.getValueType().isInteger() &&
  4184. VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
  4185. "Illegal SCALAR_TO_VECTOR node!");
  4186. if (OpOpcode == ISD::UNDEF)
  4187. return getUNDEF(VT);
  4188. // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
  4189. if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
  4190. isa<ConstantSDNode>(Operand.getOperand(1)) &&
  4191. Operand.getConstantOperandVal(1) == 0 &&
  4192. Operand.getOperand(0).getValueType() == VT)
  4193. return Operand.getOperand(0);
  4194. break;
  4195. case ISD::FNEG:
  4196. // Negation of an unknown bag of bits is still completely undefined.
  4197. if (OpOpcode == ISD::UNDEF)
  4198. return getUNDEF(VT);
  4199. // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
  4200. if ((getTarget().Options.NoSignedZerosFPMath || Flags.hasNoSignedZeros()) &&
  4201. OpOpcode == ISD::FSUB)
  4202. return getNode(ISD::FSUB, DL, VT, Operand.getOperand(1),
  4203. Operand.getOperand(0), Flags);
  4204. if (OpOpcode == ISD::FNEG) // --X -> X
  4205. return Operand.getOperand(0);
  4206. break;
  4207. case ISD::FABS:
  4208. if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
  4209. return getNode(ISD::FABS, DL, VT, Operand.getOperand(0));
  4210. break;
  4211. }
  4212. SDNode *N;
  4213. SDVTList VTs = getVTList(VT);
  4214. SDValue Ops[] = {Operand};
  4215. if (VT != MVT::Glue) { // Don't CSE flag producing nodes
  4216. FoldingSetNodeID ID;
  4217. AddNodeIDNode(ID, Opcode, VTs, Ops);
  4218. void *IP = nullptr;
  4219. if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
  4220. E->intersectFlagsWith(Flags);
  4221. return SDValue(E, 0);
  4222. }
  4223. N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
  4224. N->setFlags(Flags);
  4225. createOperands(N, Ops);
  4226. CSEMap.InsertNode(N, IP);
  4227. } else {
  4228. N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
  4229. createOperands(N, Ops);
  4230. }
  4231. InsertNode(N);
  4232. SDValue V = SDValue(N, 0);
  4233. NewSDValueDbgMsg(V, "Creating new node: ", this);
  4234. return V;
  4235. }
  4236. static std::pair<APInt, bool> FoldValue(unsigned Opcode, const APInt &C1,
  4237. const APInt &C2) {
  4238. switch (Opcode) {
  4239. case ISD::ADD: return std::make_pair(C1 + C2, true);
  4240. case ISD::SUB: return std::make_pair(C1 - C2, true);
  4241. case ISD::MUL: return std::make_pair(C1 * C2, true);
  4242. case ISD::AND: return std::make_pair(C1 & C2, true);
  4243. case ISD::OR: return std::make_pair(C1 | C2, true);
  4244. case ISD::XOR: return std::make_pair(C1 ^ C2, true);
  4245. case ISD::SHL: return std::make_pair(C1 << C2, true);
  4246. case ISD::SRL: return std::make_pair(C1.lshr(C2), true);
  4247. case ISD::SRA: return std::make_pair(C1.ashr(C2), true);
  4248. case ISD::ROTL: return std::make_pair(C1.rotl(C2), true);
  4249. case ISD::ROTR: return std::make_pair(C1.rotr(C2), true);
  4250. case ISD::SMIN: return std::make_pair(C1.sle(C2) ? C1 : C2, true);
  4251. case ISD::SMAX: return std::make_pair(C1.sge(C2) ? C1 : C2, true);
  4252. case ISD::UMIN: return std::make_pair(C1.ule(C2) ? C1 : C2, true);
  4253. case ISD::UMAX: return std::make_pair(C1.uge(C2) ? C1 : C2, true);
  4254. case ISD::SADDSAT: return std::make_pair(C1.sadd_sat(C2), true);
  4255. case ISD::UADDSAT: return std::make_pair(C1.uadd_sat(C2), true);
  4256. case ISD::SSUBSAT: return std::make_pair(C1.ssub_sat(C2), true);
  4257. case ISD::USUBSAT: return std::make_pair(C1.usub_sat(C2), true);
  4258. case ISD::UDIV:
  4259. if (!C2.getBoolValue())
  4260. break;
  4261. return std::make_pair(C1.udiv(C2), true);
  4262. case ISD::UREM:
  4263. if (!C2.getBoolValue())
  4264. break;
  4265. return std::make_pair(C1.urem(C2), true);
  4266. case ISD::SDIV:
  4267. if (!C2.getBoolValue())
  4268. break;
  4269. return std::make_pair(C1.sdiv(C2), true);
  4270. case ISD::SREM:
  4271. if (!C2.getBoolValue())
  4272. break;
  4273. return std::make_pair(C1.srem(C2), true);
  4274. }
  4275. return std::make_pair(APInt(1, 0), false);
  4276. }
  4277. SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL,
  4278. EVT VT, const ConstantSDNode *C1,
  4279. const ConstantSDNode *C2) {
  4280. if (C1->isOpaque() || C2->isOpaque())
  4281. return SDValue();
  4282. std::pair<APInt, bool> Folded = FoldValue(Opcode, C1->getAPIntValue(),
  4283. C2->getAPIntValue());
  4284. if (!Folded.second)
  4285. return SDValue();
  4286. return getConstant(Folded.first, DL, VT);
  4287. }
  4288. SDValue SelectionDAG::FoldSymbolOffset(unsigned Opcode, EVT VT,
  4289. const GlobalAddressSDNode *GA,
  4290. const SDNode *N2) {
  4291. if (GA->getOpcode() != ISD::GlobalAddress)
  4292. return SDValue();
  4293. if (!TLI->isOffsetFoldingLegal(GA))
  4294. return SDValue();
  4295. auto *C2 = dyn_cast<ConstantSDNode>(N2);
  4296. if (!C2)
  4297. return SDValue();
  4298. int64_t Offset = C2->getSExtValue();
  4299. switch (Opcode) {
  4300. case ISD::ADD: break;
  4301. case ISD::SUB: Offset = -uint64_t(Offset); break;
  4302. default: return SDValue();
  4303. }
  4304. return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
  4305. GA->getOffset() + uint64_t(Offset));
  4306. }
  4307. bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
  4308. switch (Opcode) {
  4309. case ISD::SDIV:
  4310. case ISD::UDIV:
  4311. case ISD::SREM:
  4312. case ISD::UREM: {
  4313. // If a divisor is zero/undef or any element of a divisor vector is
  4314. // zero/undef, the whole op is undef.
  4315. assert(Ops.size() == 2 && "Div/rem should have 2 operands");
  4316. SDValue Divisor = Ops[1];
  4317. if (Divisor.isUndef() || isNullConstant(Divisor))
  4318. return true;
  4319. return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
  4320. llvm::any_of(Divisor->op_values(),
  4321. [](SDValue V) { return V.isUndef() ||
  4322. isNullConstant(V); });
  4323. // TODO: Handle signed overflow.
  4324. }
  4325. // TODO: Handle oversized shifts.
  4326. default:
  4327. return false;
  4328. }
  4329. }
  4330. SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL,
  4331. EVT VT, SDNode *N1, SDNode *N2) {
  4332. // If the opcode is a target-specific ISD node, there's nothing we can
  4333. // do here and the operand rules may not line up with the below, so
  4334. // bail early.
  4335. if (Opcode >= ISD::BUILTIN_OP_END)
  4336. return SDValue();
  4337. if (isUndef(Opcode, {SDValue(N1, 0), SDValue(N2, 0)}))
  4338. return getUNDEF(VT);
  4339. // Handle the case of two scalars.
  4340. if (auto *C1 = dyn_cast<ConstantSDNode>(N1)) {
  4341. if (auto *C2 = dyn_cast<ConstantSDNode>(N2)) {
  4342. SDValue Folded = FoldConstantArithmetic(Opcode, DL, VT, C1, C2);
  4343. assert((!Folded || !VT.isVector()) &&
  4344. "Can't fold vectors ops with scalar operands");
  4345. return Folded;
  4346. }
  4347. }
  4348. // fold (add Sym, c) -> Sym+c
  4349. if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N1))
  4350. return FoldSymbolOffset(Opcode, VT, GA, N2);
  4351. if (TLI->isCommutativeBinOp(Opcode))
  4352. if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N2))
  4353. return FoldSymbolOffset(Opcode, VT, GA, N1);
  4354. // For vectors, extract each constant element and fold them individually.
  4355. // Either input may be an undef value.
  4356. auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
  4357. if (!BV1 && !N1->isUndef())
  4358. return SDValue();
  4359. auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
  4360. if (!BV2 && !N2->isUndef())
  4361. return SDValue();
  4362. // If both operands are undef, that's handled the same way as scalars.
  4363. if (!BV1 && !BV2)
  4364. return SDValue();
  4365. assert((!BV1 || !BV2 || BV1->getNumOperands() == BV2->getNumOperands()) &&
  4366. "Vector binop with different number of elements in operands?");
  4367. EVT SVT = VT.getScalarType();
  4368. EVT LegalSVT = SVT;
  4369. if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
  4370. LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
  4371. if (LegalSVT.bitsLT(SVT))
  4372. return SDValue();
  4373. }
  4374. SmallVector<SDValue, 4> Outputs;
  4375. unsigned NumOps = BV1 ? BV1->getNumOperands() : BV2->getNumOperands();
  4376. for (unsigned I = 0; I != NumOps; ++I) {
  4377. SDValue V1 = BV1 ? BV1->getOperand(I) : getUNDEF(SVT);
  4378. SDValue V2 = BV2 ? BV2->getOperand(I) : getUNDEF(SVT);
  4379. if (SVT.isInteger()) {
  4380. if (V1->getValueType(0).bitsGT(SVT))
  4381. V1 = getNode(ISD::TRUNCATE, DL, SVT, V1);
  4382. if (V2->getValueType(0).bitsGT(SVT))
  4383. V2 = getNode(ISD::TRUNCATE, DL, SVT, V2);
  4384. }
  4385. if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
  4386. return SDValue();
  4387. // Fold one vector element.
  4388. SDValue ScalarResult = getNode(Opcode, DL, SVT, V1, V2);
  4389. if (LegalSVT != SVT)
  4390. ScalarResult = getNode(ISD::SIGN_EXTEND, DL, LegalSVT, ScalarResult);
  4391. // Scalar folding only succeeded if the result is a constant or UNDEF.
  4392. if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
  4393. ScalarResult.getOpcode() != ISD::ConstantFP)
  4394. return SDValue();
  4395. Outputs.push_back(ScalarResult);
  4396. }
  4397. assert(VT.getVectorNumElements() == Outputs.size() &&
  4398. "Vector size mismatch!");
  4399. // We may have a vector type but a scalar result. Create a splat.
  4400. Outputs.resize(VT.getVectorNumElements(), Outputs.back());
  4401. // Build a big vector out of the scalar elements we generated.
  4402. return getBuildVector(VT, SDLoc(), Outputs);
  4403. }
  4404. // TODO: Merge with FoldConstantArithmetic
  4405. SDValue SelectionDAG::FoldConstantVectorArithmetic(unsigned Opcode,
  4406. const SDLoc &DL, EVT VT,
  4407. ArrayRef<SDValue> Ops,
  4408. const SDNodeFlags Flags) {
  4409. // If the opcode is a target-specific ISD node, there's nothing we can
  4410. // do here and the operand rules may not line up with the below, so
  4411. // bail early.
  4412. if (Opcode >= ISD::BUILTIN_OP_END)
  4413. return SDValue();
  4414. if (isUndef(Opcode, Ops))
  4415. return getUNDEF(VT);
  4416. // We can only fold vectors - maybe merge with FoldConstantArithmetic someday?
  4417. if (!VT.isVector())
  4418. return SDValue();
  4419. unsigned NumElts = VT.getVectorNumElements();
  4420. auto IsScalarOrSameVectorSize = [&](const SDValue &Op) {
  4421. return !Op.getValueType().isVector() ||
  4422. Op.getValueType().getVectorNumElements() == NumElts;
  4423. };
  4424. auto IsConstantBuildVectorOrUndef = [&](const SDValue &Op) {
  4425. BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Op);
  4426. return (Op.isUndef()) || (Op.getOpcode() == ISD::CONDCODE) ||
  4427. (BV && BV->isConstant());
  4428. };
  4429. // All operands must be vector types with the same number of elements as
  4430. // the result type and must be either UNDEF or a build vector of constant
  4431. // or UNDEF scalars.
  4432. if (!llvm::all_of(Ops, IsConstantBuildVectorOrUndef) ||
  4433. !llvm::all_of(Ops, IsScalarOrSameVectorSize))
  4434. return SDValue();
  4435. // If we are comparing vectors, then the result needs to be a i1 boolean
  4436. // that is then sign-extended back to the legal result type.
  4437. EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
  4438. // Find legal integer scalar type for constant promotion and
  4439. // ensure that its scalar size is at least as large as source.
  4440. EVT LegalSVT = VT.getScalarType();
  4441. if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
  4442. LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
  4443. if (LegalSVT.bitsLT(VT.getScalarType()))
  4444. return SDValue();
  4445. }
  4446. // Constant fold each scalar lane separately.
  4447. SmallVector<SDValue, 4> ScalarResults;
  4448. for (unsigned i = 0; i != NumElts; i++) {
  4449. SmallVector<SDValue, 4> ScalarOps;
  4450. for (SDValue Op : Ops) {
  4451. EVT InSVT = Op.getValueType().getScalarType();
  4452. BuildVectorSDNode *InBV = dyn_cast<BuildVectorSDNode>(Op);
  4453. if (!InBV) {
  4454. // We've checked that this is UNDEF or a constant of some kind.
  4455. if (Op.isUndef())
  4456. ScalarOps.push_back(getUNDEF(InSVT));
  4457. else
  4458. ScalarOps.push_back(Op);
  4459. continue;
  4460. }
  4461. SDValue ScalarOp = InBV->getOperand(i);
  4462. EVT ScalarVT = ScalarOp.getValueType();
  4463. // Build vector (integer) scalar operands may need implicit
  4464. // truncation - do this before constant folding.
  4465. if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT))
  4466. ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
  4467. ScalarOps.push_back(ScalarOp);
  4468. }
  4469. // Constant fold the scalar operands.
  4470. SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
  4471. // Legalize the (integer) scalar constant if necessary.
  4472. if (LegalSVT != SVT)
  4473. ScalarResult = getNode(ISD::SIGN_EXTEND, DL, LegalSVT, ScalarResult);
  4474. // Scalar folding only succeeded if the result is a constant or UNDEF.
  4475. if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
  4476. ScalarResult.getOpcode() != ISD::ConstantFP)
  4477. return SDValue();
  4478. ScalarResults.push_back(ScalarResult);
  4479. }
  4480. SDValue V = getBuildVector(VT, DL, ScalarResults);
  4481. NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
  4482. return V;
  4483. }
  4484. SDValue SelectionDAG::foldConstantFPMath(unsigned Opcode, const SDLoc &DL,
  4485. EVT VT, SDValue N1, SDValue N2) {
  4486. // TODO: We don't do any constant folding for strict FP opcodes here, but we
  4487. // should. That will require dealing with a potentially non-default
  4488. // rounding mode, checking the "opStatus" return value from the APFloat
  4489. // math calculations, and possibly other variations.
  4490. auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
  4491. auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
  4492. if (N1CFP && N2CFP) {
  4493. APFloat C1 = N1CFP->getValueAPF(), C2 = N2CFP->getValueAPF();
  4494. switch (Opcode) {
  4495. case ISD::FADD:
  4496. C1.add(C2, APFloat::rmNearestTiesToEven);
  4497. return getConstantFP(C1, DL, VT);
  4498. case ISD::FSUB:
  4499. C1.subtract(C2, APFloat::rmNearestTiesToEven);
  4500. return getConstantFP(C1, DL, VT);
  4501. case ISD::FMUL:
  4502. C1.multiply(C2, APFloat::rmNearestTiesToEven);
  4503. return getConstantFP(C1, DL, VT);
  4504. case ISD::FDIV:
  4505. C1.divide(C2, APFloat::rmNearestTiesToEven);
  4506. return getConstantFP(C1, DL, VT);
  4507. case ISD::FREM:
  4508. C1.mod(C2);
  4509. return getConstantFP(C1, DL, VT);
  4510. case ISD::FCOPYSIGN:
  4511. C1.copySign(C2);
  4512. return getConstantFP(C1, DL, VT);
  4513. default: break;
  4514. }
  4515. }
  4516. if (N1CFP && Opcode == ISD::FP_ROUND) {
  4517. APFloat C1 = N1CFP->getValueAPF(); // make copy
  4518. bool Unused;
  4519. // This can return overflow, underflow, or inexact; we don't care.
  4520. // FIXME need to be more flexible about rounding mode.
  4521. (void) C1.convert(EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
  4522. &Unused);
  4523. return getConstantFP(C1, DL, VT);
  4524. }
  4525. switch (Opcode) {
  4526. case ISD::FADD:
  4527. case ISD::FSUB:
  4528. case ISD::FMUL:
  4529. case ISD::FDIV:
  4530. case ISD::FREM:
  4531. // If both operands are undef, the result is undef. If 1 operand is undef,
  4532. // the result is NaN. This should match the behavior of the IR optimizer.
  4533. if (N1.isUndef() && N2.isUndef())
  4534. return getUNDEF(VT);
  4535. if (N1.isUndef() || N2.isUndef())
  4536. return getConstantFP(APFloat::getNaN(EVTToAPFloatSemantics(VT)), DL, VT);
  4537. }
  4538. return SDValue();
  4539. }
  4540. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
  4541. SDValue N1, SDValue N2, const SDNodeFlags Flags) {
  4542. ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
  4543. ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
  4544. ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
  4545. ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
  4546. // Canonicalize constant to RHS if commutative.
  4547. if (TLI->isCommutativeBinOp(Opcode)) {
  4548. if (N1C && !N2C) {
  4549. std::swap(N1C, N2C);
  4550. std::swap(N1, N2);
  4551. } else if (N1CFP && !N2CFP) {
  4552. std::swap(N1CFP, N2CFP);
  4553. std::swap(N1, N2);
  4554. }
  4555. }
  4556. switch (Opcode) {
  4557. default: break;
  4558. case ISD::TokenFactor:
  4559. assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
  4560. N2.getValueType() == MVT::Other && "Invalid token factor!");
  4561. // Fold trivial token factors.
  4562. if (N1.getOpcode() == ISD::EntryToken) return N2;
  4563. if (N2.getOpcode() == ISD::EntryToken) return N1;
  4564. if (N1 == N2) return N1;
  4565. break;
  4566. case ISD::BUILD_VECTOR: {
  4567. // Attempt to simplify BUILD_VECTOR.
  4568. SDValue Ops[] = {N1, N2};
  4569. if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
  4570. return V;
  4571. break;
  4572. }
  4573. case ISD::CONCAT_VECTORS: {
  4574. SDValue Ops[] = {N1, N2};
  4575. if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
  4576. return V;
  4577. break;
  4578. }
  4579. case ISD::AND:
  4580. assert(VT.isInteger() && "This operator does not apply to FP types!");
  4581. assert(N1.getValueType() == N2.getValueType() &&
  4582. N1.getValueType() == VT && "Binary operator types must match!");
  4583. // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
  4584. // worth handling here.
  4585. if (N2C && N2C->isNullValue())
  4586. return N2;
  4587. if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
  4588. return N1;
  4589. break;
  4590. case ISD::OR:
  4591. case ISD::XOR:
  4592. case ISD::ADD:
  4593. case ISD::SUB:
  4594. assert(VT.isInteger() && "This operator does not apply to FP types!");
  4595. assert(N1.getValueType() == N2.getValueType() &&
  4596. N1.getValueType() == VT && "Binary operator types must match!");
  4597. // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
  4598. // it's worth handling here.
  4599. if (N2C && N2C->isNullValue())
  4600. return N1;
  4601. break;
  4602. case ISD::UDIV:
  4603. case ISD::UREM:
  4604. case ISD::MULHU:
  4605. case ISD::MULHS:
  4606. case ISD::MUL:
  4607. case ISD::SDIV:
  4608. case ISD::SREM:
  4609. case ISD::SMIN:
  4610. case ISD::SMAX:
  4611. case ISD::UMIN:
  4612. case ISD::UMAX:
  4613. case ISD::SADDSAT:
  4614. case ISD::SSUBSAT:
  4615. case ISD::UADDSAT:
  4616. case ISD::USUBSAT:
  4617. assert(VT.isInteger() && "This operator does not apply to FP types!");
  4618. assert(N1.getValueType() == N2.getValueType() &&
  4619. N1.getValueType() == VT && "Binary operator types must match!");
  4620. break;
  4621. case ISD::FADD:
  4622. case ISD::FSUB:
  4623. case ISD::FMUL:
  4624. case ISD::FDIV:
  4625. case ISD::FREM:
  4626. assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
  4627. assert(N1.getValueType() == N2.getValueType() &&
  4628. N1.getValueType() == VT && "Binary operator types must match!");
  4629. if (SDValue V = simplifyFPBinop(Opcode, N1, N2))
  4630. return V;
  4631. break;
  4632. case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
  4633. assert(N1.getValueType() == VT &&
  4634. N1.getValueType().isFloatingPoint() &&
  4635. N2.getValueType().isFloatingPoint() &&
  4636. "Invalid FCOPYSIGN!");
  4637. break;
  4638. case ISD::SHL:
  4639. case ISD::SRA:
  4640. case ISD::SRL:
  4641. if (SDValue V = simplifyShift(N1, N2))
  4642. return V;
  4643. LLVM_FALLTHROUGH;
  4644. case ISD::ROTL:
  4645. case ISD::ROTR:
  4646. assert(VT == N1.getValueType() &&
  4647. "Shift operators return type must be the same as their first arg");
  4648. assert(VT.isInteger() && N2.getValueType().isInteger() &&
  4649. "Shifts only work on integers");
  4650. assert((!VT.isVector() || VT == N2.getValueType()) &&
  4651. "Vector shift amounts must be in the same as their first arg");
  4652. // Verify that the shift amount VT is big enough to hold valid shift
  4653. // amounts. This catches things like trying to shift an i1024 value by an
  4654. // i8, which is easy to fall into in generic code that uses
  4655. // TLI.getShiftAmount().
  4656. assert(N2.getValueSizeInBits() >= Log2_32_Ceil(N1.getValueSizeInBits()) &&
  4657. "Invalid use of small shift amount with oversized value!");
  4658. // Always fold shifts of i1 values so the code generator doesn't need to
  4659. // handle them. Since we know the size of the shift has to be less than the
  4660. // size of the value, the shift/rotate count is guaranteed to be zero.
  4661. if (VT == MVT::i1)
  4662. return N1;
  4663. if (N2C && N2C->isNullValue())
  4664. return N1;
  4665. break;
  4666. case ISD::FP_ROUND:
  4667. assert(VT.isFloatingPoint() &&
  4668. N1.getValueType().isFloatingPoint() &&
  4669. VT.bitsLE(N1.getValueType()) &&
  4670. N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
  4671. "Invalid FP_ROUND!");
  4672. if (N1.getValueType() == VT) return N1; // noop conversion.
  4673. break;
  4674. case ISD::AssertSext:
  4675. case ISD::AssertZext: {
  4676. EVT EVT = cast<VTSDNode>(N2)->getVT();
  4677. assert(VT == N1.getValueType() && "Not an inreg extend!");
  4678. assert(VT.isInteger() && EVT.isInteger() &&
  4679. "Cannot *_EXTEND_INREG FP types");
  4680. assert(!EVT.isVector() &&
  4681. "AssertSExt/AssertZExt type should be the vector element type "
  4682. "rather than the vector type!");
  4683. assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
  4684. if (VT.getScalarType() == EVT) return N1; // noop assertion.
  4685. break;
  4686. }
  4687. case ISD::SIGN_EXTEND_INREG: {
  4688. EVT EVT = cast<VTSDNode>(N2)->getVT();
  4689. assert(VT == N1.getValueType() && "Not an inreg extend!");
  4690. assert(VT.isInteger() && EVT.isInteger() &&
  4691. "Cannot *_EXTEND_INREG FP types");
  4692. assert(EVT.isVector() == VT.isVector() &&
  4693. "SIGN_EXTEND_INREG type should be vector iff the operand "
  4694. "type is vector!");
  4695. assert((!EVT.isVector() ||
  4696. EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
  4697. "Vector element counts must match in SIGN_EXTEND_INREG");
  4698. assert(EVT.bitsLE(VT) && "Not extending!");
  4699. if (EVT == VT) return N1; // Not actually extending
  4700. auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
  4701. unsigned FromBits = EVT.getScalarSizeInBits();
  4702. Val <<= Val.getBitWidth() - FromBits;
  4703. Val.ashrInPlace(Val.getBitWidth() - FromBits);
  4704. return getConstant(Val, DL, ConstantVT);
  4705. };
  4706. if (N1C) {
  4707. const APInt &Val = N1C->getAPIntValue();
  4708. return SignExtendInReg(Val, VT);
  4709. }
  4710. if (ISD::isBuildVectorOfConstantSDNodes(N1.getNode())) {
  4711. SmallVector<SDValue, 8> Ops;
  4712. llvm::EVT OpVT = N1.getOperand(0).getValueType();
  4713. for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
  4714. SDValue Op = N1.getOperand(i);
  4715. if (Op.isUndef()) {
  4716. Ops.push_back(getUNDEF(OpVT));
  4717. continue;
  4718. }
  4719. ConstantSDNode *C = cast<ConstantSDNode>(Op);
  4720. APInt Val = C->getAPIntValue();
  4721. Ops.push_back(SignExtendInReg(Val, OpVT));
  4722. }
  4723. return getBuildVector(VT, DL, Ops);
  4724. }
  4725. break;
  4726. }
  4727. case ISD::EXTRACT_VECTOR_ELT:
  4728. assert(VT.getSizeInBits() >= N1.getValueType().getScalarSizeInBits() &&
  4729. "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
  4730. element type of the vector.");
  4731. // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
  4732. if (N1.isUndef())
  4733. return getUNDEF(VT);
  4734. // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF
  4735. if (N2C && N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
  4736. return getUNDEF(VT);
  4737. // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
  4738. // expanding copies of large vectors from registers.
  4739. if (N2C &&
  4740. N1.getOpcode() == ISD::CONCAT_VECTORS &&
  4741. N1.getNumOperands() > 0) {
  4742. unsigned Factor =
  4743. N1.getOperand(0).getValueType().getVectorNumElements();
  4744. return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
  4745. N1.getOperand(N2C->getZExtValue() / Factor),
  4746. getConstant(N2C->getZExtValue() % Factor, DL,
  4747. N2.getValueType()));
  4748. }
  4749. // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
  4750. // expanding large vector constants.
  4751. if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
  4752. SDValue Elt = N1.getOperand(N2C->getZExtValue());
  4753. if (VT != Elt.getValueType())
  4754. // If the vector element type is not legal, the BUILD_VECTOR operands
  4755. // are promoted and implicitly truncated, and the result implicitly
  4756. // extended. Make that explicit here.
  4757. Elt = getAnyExtOrTrunc(Elt, DL, VT);
  4758. return Elt;
  4759. }
  4760. // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
  4761. // operations are lowered to scalars.
  4762. if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
  4763. // If the indices are the same, return the inserted element else
  4764. // if the indices are known different, extract the element from
  4765. // the original vector.
  4766. SDValue N1Op2 = N1.getOperand(2);
  4767. ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
  4768. if (N1Op2C && N2C) {
  4769. if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
  4770. if (VT == N1.getOperand(1).getValueType())
  4771. return N1.getOperand(1);
  4772. else
  4773. return getSExtOrTrunc(N1.getOperand(1), DL, VT);
  4774. }
  4775. return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
  4776. }
  4777. }
  4778. // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
  4779. // when vector types are scalarized and v1iX is legal.
  4780. // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx)
  4781. if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
  4782. N1.getValueType().getVectorNumElements() == 1) {
  4783. return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
  4784. N1.getOperand(1));
  4785. }
  4786. break;
  4787. case ISD::EXTRACT_ELEMENT:
  4788. assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
  4789. assert(!N1.getValueType().isVector() && !VT.isVector() &&
  4790. (N1.getValueType().isInteger() == VT.isInteger()) &&
  4791. N1.getValueType() != VT &&
  4792. "Wrong types for EXTRACT_ELEMENT!");
  4793. // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
  4794. // 64-bit integers into 32-bit parts. Instead of building the extract of
  4795. // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
  4796. if (N1.getOpcode() == ISD::BUILD_PAIR)
  4797. return N1.getOperand(N2C->getZExtValue());
  4798. // EXTRACT_ELEMENT of a constant int is also very common.
  4799. if (N1C) {
  4800. unsigned ElementSize = VT.getSizeInBits();
  4801. unsigned Shift = ElementSize * N2C->getZExtValue();
  4802. APInt ShiftedVal = N1C->getAPIntValue().lshr(Shift);
  4803. return getConstant(ShiftedVal.trunc(ElementSize), DL, VT);
  4804. }
  4805. break;
  4806. case ISD::EXTRACT_SUBVECTOR:
  4807. if (VT.isSimple() && N1.getValueType().isSimple()) {
  4808. assert(VT.isVector() && N1.getValueType().isVector() &&
  4809. "Extract subvector VTs must be a vectors!");
  4810. assert(VT.getVectorElementType() ==
  4811. N1.getValueType().getVectorElementType() &&
  4812. "Extract subvector VTs must have the same element type!");
  4813. assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
  4814. "Extract subvector must be from larger vector to smaller vector!");
  4815. if (N2C) {
  4816. assert((VT.getVectorNumElements() + N2C->getZExtValue()
  4817. <= N1.getValueType().getVectorNumElements())
  4818. && "Extract subvector overflow!");
  4819. }
  4820. // Trivial extraction.
  4821. if (VT.getSimpleVT() == N1.getSimpleValueType())
  4822. return N1;
  4823. // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
  4824. if (N1.isUndef())
  4825. return getUNDEF(VT);
  4826. // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
  4827. // the concat have the same type as the extract.
  4828. if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
  4829. N1.getNumOperands() > 0 &&
  4830. VT == N1.getOperand(0).getValueType()) {
  4831. unsigned Factor = VT.getVectorNumElements();
  4832. return N1.getOperand(N2C->getZExtValue() / Factor);
  4833. }
  4834. // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
  4835. // during shuffle legalization.
  4836. if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
  4837. VT == N1.getOperand(1).getValueType())
  4838. return N1.getOperand(1);
  4839. }
  4840. break;
  4841. }
  4842. // Perform trivial constant folding.
  4843. if (SDValue SV =
  4844. FoldConstantArithmetic(Opcode, DL, VT, N1.getNode(), N2.getNode()))
  4845. return SV;
  4846. if (SDValue V = foldConstantFPMath(Opcode, DL, VT, N1, N2))
  4847. return V;
  4848. // Canonicalize an UNDEF to the RHS, even over a constant.
  4849. if (N1.isUndef()) {
  4850. if (TLI->isCommutativeBinOp(Opcode)) {
  4851. std::swap(N1, N2);
  4852. } else {
  4853. switch (Opcode) {
  4854. case ISD::SIGN_EXTEND_INREG:
  4855. case ISD::SUB:
  4856. return getUNDEF(VT); // fold op(undef, arg2) -> undef
  4857. case ISD::UDIV:
  4858. case ISD::SDIV:
  4859. case ISD::UREM:
  4860. case ISD::SREM:
  4861. case ISD::SSUBSAT:
  4862. case ISD::USUBSAT:
  4863. return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
  4864. }
  4865. }
  4866. }
  4867. // Fold a bunch of operators when the RHS is undef.
  4868. if (N2.isUndef()) {
  4869. switch (Opcode) {
  4870. case ISD::XOR:
  4871. if (N1.isUndef())
  4872. // Handle undef ^ undef -> 0 special case. This is a common
  4873. // idiom (misuse).
  4874. return getConstant(0, DL, VT);
  4875. LLVM_FALLTHROUGH;
  4876. case ISD::ADD:
  4877. case ISD::SUB:
  4878. case ISD::UDIV:
  4879. case ISD::SDIV:
  4880. case ISD::UREM:
  4881. case ISD::SREM:
  4882. return getUNDEF(VT); // fold op(arg1, undef) -> undef
  4883. case ISD::MUL:
  4884. case ISD::AND:
  4885. case ISD::SSUBSAT:
  4886. case ISD::USUBSAT:
  4887. return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
  4888. case ISD::OR:
  4889. case ISD::SADDSAT:
  4890. case ISD::UADDSAT:
  4891. return getAllOnesConstant(DL, VT);
  4892. }
  4893. }
  4894. // Memoize this node if possible.
  4895. SDNode *N;
  4896. SDVTList VTs = getVTList(VT);
  4897. SDValue Ops[] = {N1, N2};
  4898. if (VT != MVT::Glue) {
  4899. FoldingSetNodeID ID;
  4900. AddNodeIDNode(ID, Opcode, VTs, Ops);
  4901. void *IP = nullptr;
  4902. if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
  4903. E->intersectFlagsWith(Flags);
  4904. return SDValue(E, 0);
  4905. }
  4906. N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
  4907. N->setFlags(Flags);
  4908. createOperands(N, Ops);
  4909. CSEMap.InsertNode(N, IP);
  4910. } else {
  4911. N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
  4912. createOperands(N, Ops);
  4913. }
  4914. InsertNode(N);
  4915. SDValue V = SDValue(N, 0);
  4916. NewSDValueDbgMsg(V, "Creating new node: ", this);
  4917. return V;
  4918. }
  4919. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
  4920. SDValue N1, SDValue N2, SDValue N3,
  4921. const SDNodeFlags Flags) {
  4922. // Perform various simplifications.
  4923. switch (Opcode) {
  4924. case ISD::FMA: {
  4925. assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
  4926. assert(N1.getValueType() == VT && N2.getValueType() == VT &&
  4927. N3.getValueType() == VT && "FMA types must match!");
  4928. ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
  4929. ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
  4930. ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
  4931. if (N1CFP && N2CFP && N3CFP) {
  4932. APFloat V1 = N1CFP->getValueAPF();
  4933. const APFloat &V2 = N2CFP->getValueAPF();
  4934. const APFloat &V3 = N3CFP->getValueAPF();
  4935. V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
  4936. return getConstantFP(V1, DL, VT);
  4937. }
  4938. break;
  4939. }
  4940. case ISD::BUILD_VECTOR: {
  4941. // Attempt to simplify BUILD_VECTOR.
  4942. SDValue Ops[] = {N1, N2, N3};
  4943. if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
  4944. return V;
  4945. break;
  4946. }
  4947. case ISD::CONCAT_VECTORS: {
  4948. SDValue Ops[] = {N1, N2, N3};
  4949. if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
  4950. return V;
  4951. break;
  4952. }
  4953. case ISD::SETCC: {
  4954. assert(VT.isInteger() && "SETCC result type must be an integer!");
  4955. assert(N1.getValueType() == N2.getValueType() &&
  4956. "SETCC operands must have the same type!");
  4957. assert(VT.isVector() == N1.getValueType().isVector() &&
  4958. "SETCC type should be vector iff the operand type is vector!");
  4959. assert((!VT.isVector() ||
  4960. VT.getVectorNumElements() == N1.getValueType().getVectorNumElements()) &&
  4961. "SETCC vector element counts must match!");
  4962. // Use FoldSetCC to simplify SETCC's.
  4963. if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
  4964. return V;
  4965. // Vector constant folding.
  4966. SDValue Ops[] = {N1, N2, N3};
  4967. if (SDValue V = FoldConstantVectorArithmetic(Opcode, DL, VT, Ops)) {
  4968. NewSDValueDbgMsg(V, "New node vector constant folding: ", this);
  4969. return V;
  4970. }
  4971. break;
  4972. }
  4973. case ISD::SELECT:
  4974. case ISD::VSELECT:
  4975. if (SDValue V = simplifySelect(N1, N2, N3))
  4976. return V;
  4977. break;
  4978. case ISD::VECTOR_SHUFFLE:
  4979. llvm_unreachable("should use getVectorShuffle constructor!");
  4980. case ISD::INSERT_VECTOR_ELT: {
  4981. ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3);
  4982. // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF
  4983. if (N3C && N3C->getZExtValue() >= N1.getValueType().getVectorNumElements())
  4984. return getUNDEF(VT);
  4985. break;
  4986. }
  4987. case ISD::INSERT_SUBVECTOR: {
  4988. // Inserting undef into undef is still undef.
  4989. if (N1.isUndef() && N2.isUndef())
  4990. return getUNDEF(VT);
  4991. SDValue Index = N3;
  4992. if (VT.isSimple() && N1.getValueType().isSimple()
  4993. && N2.getValueType().isSimple()) {
  4994. assert(VT.isVector() && N1.getValueType().isVector() &&
  4995. N2.getValueType().isVector() &&
  4996. "Insert subvector VTs must be a vectors");
  4997. assert(VT == N1.getValueType() &&
  4998. "Dest and insert subvector source types must match!");
  4999. assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
  5000. "Insert subvector must be from smaller vector to larger vector!");
  5001. if (isa<ConstantSDNode>(Index)) {
  5002. assert((N2.getValueType().getVectorNumElements() +
  5003. cast<ConstantSDNode>(Index)->getZExtValue()
  5004. <= VT.getVectorNumElements())
  5005. && "Insert subvector overflow!");
  5006. }
  5007. // Trivial insertion.
  5008. if (VT.getSimpleVT() == N2.getSimpleValueType())
  5009. return N2;
  5010. // If this is an insert of an extracted vector into an undef vector, we
  5011. // can just use the input to the extract.
  5012. if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
  5013. N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
  5014. return N2.getOperand(0);
  5015. }
  5016. break;
  5017. }
  5018. case ISD::BITCAST:
  5019. // Fold bit_convert nodes from a type to themselves.
  5020. if (N1.getValueType() == VT)
  5021. return N1;
  5022. break;
  5023. }
  5024. // Memoize node if it doesn't produce a flag.
  5025. SDNode *N;
  5026. SDVTList VTs = getVTList(VT);
  5027. SDValue Ops[] = {N1, N2, N3};
  5028. if (VT != MVT::Glue) {
  5029. FoldingSetNodeID ID;
  5030. AddNodeIDNode(ID, Opcode, VTs, Ops);
  5031. void *IP = nullptr;
  5032. if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
  5033. E->intersectFlagsWith(Flags);
  5034. return SDValue(E, 0);
  5035. }
  5036. N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
  5037. N->setFlags(Flags);
  5038. createOperands(N, Ops);
  5039. CSEMap.InsertNode(N, IP);
  5040. } else {
  5041. N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
  5042. createOperands(N, Ops);
  5043. }
  5044. InsertNode(N);
  5045. SDValue V = SDValue(N, 0);
  5046. NewSDValueDbgMsg(V, "Creating new node: ", this);
  5047. return V;
  5048. }
  5049. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
  5050. SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
  5051. SDValue Ops[] = { N1, N2, N3, N4 };
  5052. return getNode(Opcode, DL, VT, Ops);
  5053. }
  5054. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
  5055. SDValue N1, SDValue N2, SDValue N3, SDValue N4,
  5056. SDValue N5) {
  5057. SDValue Ops[] = { N1, N2, N3, N4, N5 };
  5058. return getNode(Opcode, DL, VT, Ops);
  5059. }
  5060. /// getStackArgumentTokenFactor - Compute a TokenFactor to force all
  5061. /// the incoming stack arguments to be loaded from the stack.
  5062. SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
  5063. SmallVector<SDValue, 8> ArgChains;
  5064. // Include the original chain at the beginning of the list. When this is
  5065. // used by target LowerCall hooks, this helps legalize find the
  5066. // CALLSEQ_BEGIN node.
  5067. ArgChains.push_back(Chain);
  5068. // Add a chain value for each stack argument.
  5069. for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
  5070. UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
  5071. if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
  5072. if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
  5073. if (FI->getIndex() < 0)
  5074. ArgChains.push_back(SDValue(L, 1));
  5075. // Build a tokenfactor for all the chains.
  5076. return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
  5077. }
  5078. /// getMemsetValue - Vectorized representation of the memset value
  5079. /// operand.
  5080. static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
  5081. const SDLoc &dl) {
  5082. assert(!Value.isUndef());
  5083. unsigned NumBits = VT.getScalarSizeInBits();
  5084. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
  5085. assert(C->getAPIntValue().getBitWidth() == 8);
  5086. APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
  5087. if (VT.isInteger()) {
  5088. bool IsOpaque = VT.getSizeInBits() > 64 ||
  5089. !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
  5090. return DAG.getConstant(Val, dl, VT, false, IsOpaque);
  5091. }
  5092. return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
  5093. VT);
  5094. }
  5095. assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
  5096. EVT IntVT = VT.getScalarType();
  5097. if (!IntVT.isInteger())
  5098. IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
  5099. Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
  5100. if (NumBits > 8) {
  5101. // Use a multiplication with 0x010101... to extend the input to the
  5102. // required length.
  5103. APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
  5104. Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
  5105. DAG.getConstant(Magic, dl, IntVT));
  5106. }
  5107. if (VT != Value.getValueType() && !VT.isInteger())
  5108. Value = DAG.getBitcast(VT.getScalarType(), Value);
  5109. if (VT != Value.getValueType())
  5110. Value = DAG.getSplatBuildVector(VT, dl, Value);
  5111. return Value;
  5112. }
  5113. /// getMemsetStringVal - Similar to getMemsetValue. Except this is only
  5114. /// used when a memcpy is turned into a memset when the source is a constant
  5115. /// string ptr.
  5116. static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG,
  5117. const TargetLowering &TLI,
  5118. const ConstantDataArraySlice &Slice) {
  5119. // Handle vector with all elements zero.
  5120. if (Slice.Array == nullptr) {
  5121. if (VT.isInteger())
  5122. return DAG.getConstant(0, dl, VT);
  5123. else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
  5124. return DAG.getConstantFP(0.0, dl, VT);
  5125. else if (VT.isVector()) {
  5126. unsigned NumElts = VT.getVectorNumElements();
  5127. MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
  5128. return DAG.getNode(ISD::BITCAST, dl, VT,
  5129. DAG.getConstant(0, dl,
  5130. EVT::getVectorVT(*DAG.getContext(),
  5131. EltVT, NumElts)));
  5132. } else
  5133. llvm_unreachable("Expected type!");
  5134. }
  5135. assert(!VT.isVector() && "Can't handle vector type here!");
  5136. unsigned NumVTBits = VT.getSizeInBits();
  5137. unsigned NumVTBytes = NumVTBits / 8;
  5138. unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
  5139. APInt Val(NumVTBits, 0);
  5140. if (DAG.getDataLayout().isLittleEndian()) {
  5141. for (unsigned i = 0; i != NumBytes; ++i)
  5142. Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
  5143. } else {
  5144. for (unsigned i = 0; i != NumBytes; ++i)
  5145. Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
  5146. }
  5147. // If the "cost" of materializing the integer immediate is less than the cost
  5148. // of a load, then it is cost effective to turn the load into the immediate.
  5149. Type *Ty = VT.getTypeForEVT(*DAG.getContext());
  5150. if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
  5151. return DAG.getConstant(Val, dl, VT);
  5152. return SDValue(nullptr, 0);
  5153. }
  5154. SDValue SelectionDAG::getMemBasePlusOffset(SDValue Base, unsigned Offset,
  5155. const SDLoc &DL) {
  5156. EVT VT = Base.getValueType();
  5157. return getNode(ISD::ADD, DL, VT, Base, getConstant(Offset, DL, VT));
  5158. }
  5159. /// Returns true if memcpy source is constant data.
  5160. static bool isMemSrcFromConstant(SDValue Src, ConstantDataArraySlice &Slice) {
  5161. uint64_t SrcDelta = 0;
  5162. GlobalAddressSDNode *G = nullptr;
  5163. if (Src.getOpcode() == ISD::GlobalAddress)
  5164. G = cast<GlobalAddressSDNode>(Src);
  5165. else if (Src.getOpcode() == ISD::ADD &&
  5166. Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
  5167. Src.getOperand(1).getOpcode() == ISD::Constant) {
  5168. G = cast<GlobalAddressSDNode>(Src.getOperand(0));
  5169. SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
  5170. }
  5171. if (!G)
  5172. return false;
  5173. return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
  5174. SrcDelta + G->getOffset());
  5175. }
  5176. static bool shouldLowerMemFuncForSize(const MachineFunction &MF) {
  5177. // On Darwin, -Os means optimize for size without hurting performance, so
  5178. // only really optimize for size when -Oz (MinSize) is used.
  5179. if (MF.getTarget().getTargetTriple().isOSDarwin())
  5180. return MF.getFunction().hasMinSize();
  5181. return MF.getFunction().hasOptSize();
  5182. }
  5183. static void chainLoadsAndStoresForMemcpy(SelectionDAG &DAG, const SDLoc &dl,
  5184. SmallVector<SDValue, 32> &OutChains, unsigned From,
  5185. unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
  5186. SmallVector<SDValue, 16> &OutStoreChains) {
  5187. assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
  5188. assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
  5189. SmallVector<SDValue, 16> GluedLoadChains;
  5190. for (unsigned i = From; i < To; ++i) {
  5191. OutChains.push_back(OutLoadChains[i]);
  5192. GluedLoadChains.push_back(OutLoadChains[i]);
  5193. }
  5194. // Chain for all loads.
  5195. SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  5196. GluedLoadChains);
  5197. for (unsigned i = From; i < To; ++i) {
  5198. StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
  5199. SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
  5200. ST->getBasePtr(), ST->getMemoryVT(),
  5201. ST->getMemOperand());
  5202. OutChains.push_back(NewStore);
  5203. }
  5204. }
  5205. static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl,
  5206. SDValue Chain, SDValue Dst, SDValue Src,
  5207. uint64_t Size, unsigned Align,
  5208. bool isVol, bool AlwaysInline,
  5209. MachinePointerInfo DstPtrInfo,
  5210. MachinePointerInfo SrcPtrInfo) {
  5211. // Turn a memcpy of undef to nop.
  5212. // FIXME: We need to honor volatile even is Src is undef.
  5213. if (Src.isUndef())
  5214. return Chain;
  5215. // Expand memcpy to a series of load and store ops if the size operand falls
  5216. // below a certain threshold.
  5217. // TODO: In the AlwaysInline case, if the size is big then generate a loop
  5218. // rather than maybe a humongous number of loads and stores.
  5219. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  5220. const DataLayout &DL = DAG.getDataLayout();
  5221. LLVMContext &C = *DAG.getContext();
  5222. std::vector<EVT> MemOps;
  5223. bool DstAlignCanChange = false;
  5224. MachineFunction &MF = DAG.getMachineFunction();
  5225. MachineFrameInfo &MFI = MF.getFrameInfo();
  5226. bool OptSize = shouldLowerMemFuncForSize(MF);
  5227. FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
  5228. if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
  5229. DstAlignCanChange = true;
  5230. unsigned SrcAlign = DAG.InferPtrAlignment(Src);
  5231. if (Align > SrcAlign)
  5232. SrcAlign = Align;
  5233. ConstantDataArraySlice Slice;
  5234. bool CopyFromConstant = isMemSrcFromConstant(Src, Slice);
  5235. bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
  5236. unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
  5237. if (!TLI.findOptimalMemOpLowering(
  5238. MemOps, Limit, Size, (DstAlignCanChange ? 0 : Align),
  5239. (isZeroConstant ? 0 : SrcAlign), /*IsMemset=*/false,
  5240. /*ZeroMemset=*/false, /*MemcpyStrSrc=*/CopyFromConstant,
  5241. /*AllowOverlap=*/!isVol, DstPtrInfo.getAddrSpace(),
  5242. SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
  5243. return SDValue();
  5244. if (DstAlignCanChange) {
  5245. Type *Ty = MemOps[0].getTypeForEVT(C);
  5246. unsigned NewAlign = (unsigned)DL.getABITypeAlignment(Ty);
  5247. // Don't promote to an alignment that would require dynamic stack
  5248. // realignment.
  5249. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  5250. if (!TRI->needsStackRealignment(MF))
  5251. while (NewAlign > Align &&
  5252. DL.exceedsNaturalStackAlignment(llvm::Align(NewAlign)))
  5253. NewAlign /= 2;
  5254. if (NewAlign > Align) {
  5255. // Give the stack frame object a larger alignment if needed.
  5256. if (MFI.getObjectAlignment(FI->getIndex()) < NewAlign)
  5257. MFI.setObjectAlignment(FI->getIndex(), NewAlign);
  5258. Align = NewAlign;
  5259. }
  5260. }
  5261. MachineMemOperand::Flags MMOFlags =
  5262. isVol ? MachineMemOperand::MOVolatile : MachineMemOperand::MONone;
  5263. SmallVector<SDValue, 16> OutLoadChains;
  5264. SmallVector<SDValue, 16> OutStoreChains;
  5265. SmallVector<SDValue, 32> OutChains;
  5266. unsigned NumMemOps = MemOps.size();
  5267. uint64_t SrcOff = 0, DstOff = 0;
  5268. for (unsigned i = 0; i != NumMemOps; ++i) {
  5269. EVT VT = MemOps[i];
  5270. unsigned VTSize = VT.getSizeInBits() / 8;
  5271. SDValue Value, Store;
  5272. if (VTSize > Size) {
  5273. // Issuing an unaligned load / store pair that overlaps with the previous
  5274. // pair. Adjust the offset accordingly.
  5275. assert(i == NumMemOps-1 && i != 0);
  5276. SrcOff -= VTSize - Size;
  5277. DstOff -= VTSize - Size;
  5278. }
  5279. if (CopyFromConstant &&
  5280. (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
  5281. // It's unlikely a store of a vector immediate can be done in a single
  5282. // instruction. It would require a load from a constantpool first.
  5283. // We only handle zero vectors here.
  5284. // FIXME: Handle other cases where store of vector immediate is done in
  5285. // a single instruction.
  5286. ConstantDataArraySlice SubSlice;
  5287. if (SrcOff < Slice.Length) {
  5288. SubSlice = Slice;
  5289. SubSlice.move(SrcOff);
  5290. } else {
  5291. // This is an out-of-bounds access and hence UB. Pretend we read zero.
  5292. SubSlice.Array = nullptr;
  5293. SubSlice.Offset = 0;
  5294. SubSlice.Length = VTSize;
  5295. }
  5296. Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
  5297. if (Value.getNode()) {
  5298. Store = DAG.getStore(Chain, dl, Value,
  5299. DAG.getMemBasePlusOffset(Dst, DstOff, dl),
  5300. DstPtrInfo.getWithOffset(DstOff), Align,
  5301. MMOFlags);
  5302. OutChains.push_back(Store);
  5303. }
  5304. }
  5305. if (!Store.getNode()) {
  5306. // The type might not be legal for the target. This should only happen
  5307. // if the type is smaller than a legal type, as on PPC, so the right
  5308. // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
  5309. // to Load/Store if NVT==VT.
  5310. // FIXME does the case above also need this?
  5311. EVT NVT = TLI.getTypeToTransformTo(C, VT);
  5312. assert(NVT.bitsGE(VT));
  5313. bool isDereferenceable =
  5314. SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
  5315. MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
  5316. if (isDereferenceable)
  5317. SrcMMOFlags |= MachineMemOperand::MODereferenceable;
  5318. Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
  5319. DAG.getMemBasePlusOffset(Src, SrcOff, dl),
  5320. SrcPtrInfo.getWithOffset(SrcOff), VT,
  5321. MinAlign(SrcAlign, SrcOff), SrcMMOFlags);
  5322. OutLoadChains.push_back(Value.getValue(1));
  5323. Store = DAG.getTruncStore(
  5324. Chain, dl, Value, DAG.getMemBasePlusOffset(Dst, DstOff, dl),
  5325. DstPtrInfo.getWithOffset(DstOff), VT, Align, MMOFlags);
  5326. OutStoreChains.push_back(Store);
  5327. }
  5328. SrcOff += VTSize;
  5329. DstOff += VTSize;
  5330. Size -= VTSize;
  5331. }
  5332. unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
  5333. TLI.getMaxGluedStoresPerMemcpy() : MaxLdStGlue;
  5334. unsigned NumLdStInMemcpy = OutStoreChains.size();
  5335. if (NumLdStInMemcpy) {
  5336. // It may be that memcpy might be converted to memset if it's memcpy
  5337. // of constants. In such a case, we won't have loads and stores, but
  5338. // just stores. In the absence of loads, there is nothing to gang up.
  5339. if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
  5340. // If target does not care, just leave as it.
  5341. for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
  5342. OutChains.push_back(OutLoadChains[i]);
  5343. OutChains.push_back(OutStoreChains[i]);
  5344. }
  5345. } else {
  5346. // Ld/St less than/equal limit set by target.
  5347. if (NumLdStInMemcpy <= GluedLdStLimit) {
  5348. chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
  5349. NumLdStInMemcpy, OutLoadChains,
  5350. OutStoreChains);
  5351. } else {
  5352. unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
  5353. unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
  5354. unsigned GlueIter = 0;
  5355. for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
  5356. unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
  5357. unsigned IndexTo = NumLdStInMemcpy - GlueIter;
  5358. chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
  5359. OutLoadChains, OutStoreChains);
  5360. GlueIter += GluedLdStLimit;
  5361. }
  5362. // Residual ld/st.
  5363. if (RemainingLdStInMemcpy) {
  5364. chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
  5365. RemainingLdStInMemcpy, OutLoadChains,
  5366. OutStoreChains);
  5367. }
  5368. }
  5369. }
  5370. }
  5371. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
  5372. }
  5373. static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl,
  5374. SDValue Chain, SDValue Dst, SDValue Src,
  5375. uint64_t Size, unsigned Align,
  5376. bool isVol, bool AlwaysInline,
  5377. MachinePointerInfo DstPtrInfo,
  5378. MachinePointerInfo SrcPtrInfo) {
  5379. // Turn a memmove of undef to nop.
  5380. // FIXME: We need to honor volatile even is Src is undef.
  5381. if (Src.isUndef())
  5382. return Chain;
  5383. // Expand memmove to a series of load and store ops if the size operand falls
  5384. // below a certain threshold.
  5385. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  5386. const DataLayout &DL = DAG.getDataLayout();
  5387. LLVMContext &C = *DAG.getContext();
  5388. std::vector<EVT> MemOps;
  5389. bool DstAlignCanChange = false;
  5390. MachineFunction &MF = DAG.getMachineFunction();
  5391. MachineFrameInfo &MFI = MF.getFrameInfo();
  5392. bool OptSize = shouldLowerMemFuncForSize(MF);
  5393. FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
  5394. if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
  5395. DstAlignCanChange = true;
  5396. unsigned SrcAlign = DAG.InferPtrAlignment(Src);
  5397. if (Align > SrcAlign)
  5398. SrcAlign = Align;
  5399. unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
  5400. // FIXME: `AllowOverlap` should really be `!isVol` but there is a bug in
  5401. // findOptimalMemOpLowering. Meanwhile, setting it to `false` produces the
  5402. // correct code.
  5403. bool AllowOverlap = false;
  5404. if (!TLI.findOptimalMemOpLowering(
  5405. MemOps, Limit, Size, (DstAlignCanChange ? 0 : Align), SrcAlign,
  5406. /*IsMemset=*/false, /*ZeroMemset=*/false, /*MemcpyStrSrc=*/false,
  5407. AllowOverlap, DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
  5408. MF.getFunction().getAttributes()))
  5409. return SDValue();
  5410. if (DstAlignCanChange) {
  5411. Type *Ty = MemOps[0].getTypeForEVT(C);
  5412. unsigned NewAlign = (unsigned)DL.getABITypeAlignment(Ty);
  5413. if (NewAlign > Align) {
  5414. // Give the stack frame object a larger alignment if needed.
  5415. if (MFI.getObjectAlignment(FI->getIndex()) < NewAlign)
  5416. MFI.setObjectAlignment(FI->getIndex(), NewAlign);
  5417. Align = NewAlign;
  5418. }
  5419. }
  5420. MachineMemOperand::Flags MMOFlags =
  5421. isVol ? MachineMemOperand::MOVolatile : MachineMemOperand::MONone;
  5422. uint64_t SrcOff = 0, DstOff = 0;
  5423. SmallVector<SDValue, 8> LoadValues;
  5424. SmallVector<SDValue, 8> LoadChains;
  5425. SmallVector<SDValue, 8> OutChains;
  5426. unsigned NumMemOps = MemOps.size();
  5427. for (unsigned i = 0; i < NumMemOps; i++) {
  5428. EVT VT = MemOps[i];
  5429. unsigned VTSize = VT.getSizeInBits() / 8;
  5430. SDValue Value;
  5431. bool isDereferenceable =
  5432. SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
  5433. MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
  5434. if (isDereferenceable)
  5435. SrcMMOFlags |= MachineMemOperand::MODereferenceable;
  5436. Value =
  5437. DAG.getLoad(VT, dl, Chain, DAG.getMemBasePlusOffset(Src, SrcOff, dl),
  5438. SrcPtrInfo.getWithOffset(SrcOff), SrcAlign, SrcMMOFlags);
  5439. LoadValues.push_back(Value);
  5440. LoadChains.push_back(Value.getValue(1));
  5441. SrcOff += VTSize;
  5442. }
  5443. Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
  5444. OutChains.clear();
  5445. for (unsigned i = 0; i < NumMemOps; i++) {
  5446. EVT VT = MemOps[i];
  5447. unsigned VTSize = VT.getSizeInBits() / 8;
  5448. SDValue Store;
  5449. Store = DAG.getStore(Chain, dl, LoadValues[i],
  5450. DAG.getMemBasePlusOffset(Dst, DstOff, dl),
  5451. DstPtrInfo.getWithOffset(DstOff), Align, MMOFlags);
  5452. OutChains.push_back(Store);
  5453. DstOff += VTSize;
  5454. }
  5455. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
  5456. }
  5457. /// Lower the call to 'memset' intrinsic function into a series of store
  5458. /// operations.
  5459. ///
  5460. /// \param DAG Selection DAG where lowered code is placed.
  5461. /// \param dl Link to corresponding IR location.
  5462. /// \param Chain Control flow dependency.
  5463. /// \param Dst Pointer to destination memory location.
  5464. /// \param Src Value of byte to write into the memory.
  5465. /// \param Size Number of bytes to write.
  5466. /// \param Align Alignment of the destination in bytes.
  5467. /// \param isVol True if destination is volatile.
  5468. /// \param DstPtrInfo IR information on the memory pointer.
  5469. /// \returns New head in the control flow, if lowering was successful, empty
  5470. /// SDValue otherwise.
  5471. ///
  5472. /// The function tries to replace 'llvm.memset' intrinsic with several store
  5473. /// operations and value calculation code. This is usually profitable for small
  5474. /// memory size.
  5475. static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl,
  5476. SDValue Chain, SDValue Dst, SDValue Src,
  5477. uint64_t Size, unsigned Align, bool isVol,
  5478. MachinePointerInfo DstPtrInfo) {
  5479. // Turn a memset of undef to nop.
  5480. // FIXME: We need to honor volatile even is Src is undef.
  5481. if (Src.isUndef())
  5482. return Chain;
  5483. // Expand memset to a series of load/store ops if the size operand
  5484. // falls below a certain threshold.
  5485. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  5486. std::vector<EVT> MemOps;
  5487. bool DstAlignCanChange = false;
  5488. MachineFunction &MF = DAG.getMachineFunction();
  5489. MachineFrameInfo &MFI = MF.getFrameInfo();
  5490. bool OptSize = shouldLowerMemFuncForSize(MF);
  5491. FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
  5492. if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
  5493. DstAlignCanChange = true;
  5494. bool IsZeroVal =
  5495. isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
  5496. if (!TLI.findOptimalMemOpLowering(
  5497. MemOps, TLI.getMaxStoresPerMemset(OptSize), Size,
  5498. (DstAlignCanChange ? 0 : Align), 0, /*IsMemset=*/true,
  5499. /*ZeroMemset=*/IsZeroVal, /*MemcpyStrSrc=*/false,
  5500. /*AllowOverlap=*/!isVol, DstPtrInfo.getAddrSpace(), ~0u,
  5501. MF.getFunction().getAttributes()))
  5502. return SDValue();
  5503. if (DstAlignCanChange) {
  5504. Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
  5505. unsigned NewAlign = (unsigned)DAG.getDataLayout().getABITypeAlignment(Ty);
  5506. if (NewAlign > Align) {
  5507. // Give the stack frame object a larger alignment if needed.
  5508. if (MFI.getObjectAlignment(FI->getIndex()) < NewAlign)
  5509. MFI.setObjectAlignment(FI->getIndex(), NewAlign);
  5510. Align = NewAlign;
  5511. }
  5512. }
  5513. SmallVector<SDValue, 8> OutChains;
  5514. uint64_t DstOff = 0;
  5515. unsigned NumMemOps = MemOps.size();
  5516. // Find the largest store and generate the bit pattern for it.
  5517. EVT LargestVT = MemOps[0];
  5518. for (unsigned i = 1; i < NumMemOps; i++)
  5519. if (MemOps[i].bitsGT(LargestVT))
  5520. LargestVT = MemOps[i];
  5521. SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
  5522. for (unsigned i = 0; i < NumMemOps; i++) {
  5523. EVT VT = MemOps[i];
  5524. unsigned VTSize = VT.getSizeInBits() / 8;
  5525. if (VTSize > Size) {
  5526. // Issuing an unaligned load / store pair that overlaps with the previous
  5527. // pair. Adjust the offset accordingly.
  5528. assert(i == NumMemOps-1 && i != 0);
  5529. DstOff -= VTSize - Size;
  5530. }
  5531. // If this store is smaller than the largest store see whether we can get
  5532. // the smaller value for free with a truncate.
  5533. SDValue Value = MemSetValue;
  5534. if (VT.bitsLT(LargestVT)) {
  5535. if (!LargestVT.isVector() && !VT.isVector() &&
  5536. TLI.isTruncateFree(LargestVT, VT))
  5537. Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
  5538. else
  5539. Value = getMemsetValue(Src, VT, DAG, dl);
  5540. }
  5541. assert(Value.getValueType() == VT && "Value with wrong type.");
  5542. SDValue Store = DAG.getStore(
  5543. Chain, dl, Value, DAG.getMemBasePlusOffset(Dst, DstOff, dl),
  5544. DstPtrInfo.getWithOffset(DstOff), Align,
  5545. isVol ? MachineMemOperand::MOVolatile : MachineMemOperand::MONone);
  5546. OutChains.push_back(Store);
  5547. DstOff += VT.getSizeInBits() / 8;
  5548. Size -= VTSize;
  5549. }
  5550. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
  5551. }
  5552. static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI,
  5553. unsigned AS) {
  5554. // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
  5555. // pointer operands can be losslessly bitcasted to pointers of address space 0
  5556. if (AS != 0 && !TLI->isNoopAddrSpaceCast(AS, 0)) {
  5557. report_fatal_error("cannot lower memory intrinsic in address space " +
  5558. Twine(AS));
  5559. }
  5560. }
  5561. SDValue SelectionDAG::getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst,
  5562. SDValue Src, SDValue Size, unsigned Align,
  5563. bool isVol, bool AlwaysInline, bool isTailCall,
  5564. MachinePointerInfo DstPtrInfo,
  5565. MachinePointerInfo SrcPtrInfo) {
  5566. assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
  5567. // Check to see if we should lower the memcpy to loads and stores first.
  5568. // For cases within the target-specified limits, this is the best choice.
  5569. ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
  5570. if (ConstantSize) {
  5571. // Memcpy with size zero? Just return the original chain.
  5572. if (ConstantSize->isNullValue())
  5573. return Chain;
  5574. SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
  5575. ConstantSize->getZExtValue(),Align,
  5576. isVol, false, DstPtrInfo, SrcPtrInfo);
  5577. if (Result.getNode())
  5578. return Result;
  5579. }
  5580. // Then check to see if we should lower the memcpy with target-specific
  5581. // code. If the target chooses to do this, this is the next best.
  5582. if (TSI) {
  5583. SDValue Result = TSI->EmitTargetCodeForMemcpy(
  5584. *this, dl, Chain, Dst, Src, Size, Align, isVol, AlwaysInline,
  5585. DstPtrInfo, SrcPtrInfo);
  5586. if (Result.getNode())
  5587. return Result;
  5588. }
  5589. // If we really need inline code and the target declined to provide it,
  5590. // use a (potentially long) sequence of loads and stores.
  5591. if (AlwaysInline) {
  5592. assert(ConstantSize && "AlwaysInline requires a constant size!");
  5593. return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
  5594. ConstantSize->getZExtValue(), Align, isVol,
  5595. true, DstPtrInfo, SrcPtrInfo);
  5596. }
  5597. checkAddrSpaceIsValidForLibcall(TLI, DstPtrInfo.getAddrSpace());
  5598. checkAddrSpaceIsValidForLibcall(TLI, SrcPtrInfo.getAddrSpace());
  5599. // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
  5600. // memcpy is not guaranteed to be safe. libc memcpys aren't required to
  5601. // respect volatile, so they may do things like read or write memory
  5602. // beyond the given memory regions. But fixing this isn't easy, and most
  5603. // people don't care.
  5604. // Emit a library call.
  5605. TargetLowering::ArgListTy Args;
  5606. TargetLowering::ArgListEntry Entry;
  5607. Entry.Ty = Type::getInt8PtrTy(*getContext());
  5608. Entry.Node = Dst; Args.push_back(Entry);
  5609. Entry.Node = Src; Args.push_back(Entry);
  5610. Entry.Ty = getDataLayout().getIntPtrType(*getContext());
  5611. Entry.Node = Size; Args.push_back(Entry);
  5612. // FIXME: pass in SDLoc
  5613. TargetLowering::CallLoweringInfo CLI(*this);
  5614. CLI.setDebugLoc(dl)
  5615. .setChain(Chain)
  5616. .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
  5617. Dst.getValueType().getTypeForEVT(*getContext()),
  5618. getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
  5619. TLI->getPointerTy(getDataLayout())),
  5620. std::move(Args))
  5621. .setDiscardResult()
  5622. .setTailCall(isTailCall);
  5623. std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
  5624. return CallResult.second;
  5625. }
  5626. SDValue SelectionDAG::getAtomicMemcpy(SDValue Chain, const SDLoc &dl,
  5627. SDValue Dst, unsigned DstAlign,
  5628. SDValue Src, unsigned SrcAlign,
  5629. SDValue Size, Type *SizeTy,
  5630. unsigned ElemSz, bool isTailCall,
  5631. MachinePointerInfo DstPtrInfo,
  5632. MachinePointerInfo SrcPtrInfo) {
  5633. // Emit a library call.
  5634. TargetLowering::ArgListTy Args;
  5635. TargetLowering::ArgListEntry Entry;
  5636. Entry.Ty = getDataLayout().getIntPtrType(*getContext());
  5637. Entry.Node = Dst;
  5638. Args.push_back(Entry);
  5639. Entry.Node = Src;
  5640. Args.push_back(Entry);
  5641. Entry.Ty = SizeTy;
  5642. Entry.Node = Size;
  5643. Args.push_back(Entry);
  5644. RTLIB::Libcall LibraryCall =
  5645. RTLIB::getMEMCPY_ELEMENT_UNORDERED_ATOMIC(ElemSz);
  5646. if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
  5647. report_fatal_error("Unsupported element size");
  5648. TargetLowering::CallLoweringInfo CLI(*this);
  5649. CLI.setDebugLoc(dl)
  5650. .setChain(Chain)
  5651. .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
  5652. Type::getVoidTy(*getContext()),
  5653. getExternalSymbol(TLI->getLibcallName(LibraryCall),
  5654. TLI->getPointerTy(getDataLayout())),
  5655. std::move(Args))
  5656. .setDiscardResult()
  5657. .setTailCall(isTailCall);
  5658. std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
  5659. return CallResult.second;
  5660. }
  5661. SDValue SelectionDAG::getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst,
  5662. SDValue Src, SDValue Size, unsigned Align,
  5663. bool isVol, bool isTailCall,
  5664. MachinePointerInfo DstPtrInfo,
  5665. MachinePointerInfo SrcPtrInfo) {
  5666. assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
  5667. // Check to see if we should lower the memmove to loads and stores first.
  5668. // For cases within the target-specified limits, this is the best choice.
  5669. ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
  5670. if (ConstantSize) {
  5671. // Memmove with size zero? Just return the original chain.
  5672. if (ConstantSize->isNullValue())
  5673. return Chain;
  5674. SDValue Result =
  5675. getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
  5676. ConstantSize->getZExtValue(), Align, isVol,
  5677. false, DstPtrInfo, SrcPtrInfo);
  5678. if (Result.getNode())
  5679. return Result;
  5680. }
  5681. // Then check to see if we should lower the memmove with target-specific
  5682. // code. If the target chooses to do this, this is the next best.
  5683. if (TSI) {
  5684. SDValue Result = TSI->EmitTargetCodeForMemmove(
  5685. *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
  5686. if (Result.getNode())
  5687. return Result;
  5688. }
  5689. checkAddrSpaceIsValidForLibcall(TLI, DstPtrInfo.getAddrSpace());
  5690. checkAddrSpaceIsValidForLibcall(TLI, SrcPtrInfo.getAddrSpace());
  5691. // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
  5692. // not be safe. See memcpy above for more details.
  5693. // Emit a library call.
  5694. TargetLowering::ArgListTy Args;
  5695. TargetLowering::ArgListEntry Entry;
  5696. Entry.Ty = Type::getInt8PtrTy(*getContext());
  5697. Entry.Node = Dst; Args.push_back(Entry);
  5698. Entry.Node = Src; Args.push_back(Entry);
  5699. Entry.Ty = getDataLayout().getIntPtrType(*getContext());
  5700. Entry.Node = Size; Args.push_back(Entry);
  5701. // FIXME: pass in SDLoc
  5702. TargetLowering::CallLoweringInfo CLI(*this);
  5703. CLI.setDebugLoc(dl)
  5704. .setChain(Chain)
  5705. .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
  5706. Dst.getValueType().getTypeForEVT(*getContext()),
  5707. getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
  5708. TLI->getPointerTy(getDataLayout())),
  5709. std::move(Args))
  5710. .setDiscardResult()
  5711. .setTailCall(isTailCall);
  5712. std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
  5713. return CallResult.second;
  5714. }
  5715. SDValue SelectionDAG::getAtomicMemmove(SDValue Chain, const SDLoc &dl,
  5716. SDValue Dst, unsigned DstAlign,
  5717. SDValue Src, unsigned SrcAlign,
  5718. SDValue Size, Type *SizeTy,
  5719. unsigned ElemSz, bool isTailCall,
  5720. MachinePointerInfo DstPtrInfo,
  5721. MachinePointerInfo SrcPtrInfo) {
  5722. // Emit a library call.
  5723. TargetLowering::ArgListTy Args;
  5724. TargetLowering::ArgListEntry Entry;
  5725. Entry.Ty = getDataLayout().getIntPtrType(*getContext());
  5726. Entry.Node = Dst;
  5727. Args.push_back(Entry);
  5728. Entry.Node = Src;
  5729. Args.push_back(Entry);
  5730. Entry.Ty = SizeTy;
  5731. Entry.Node = Size;
  5732. Args.push_back(Entry);
  5733. RTLIB::Libcall LibraryCall =
  5734. RTLIB::getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(ElemSz);
  5735. if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
  5736. report_fatal_error("Unsupported element size");
  5737. TargetLowering::CallLoweringInfo CLI(*this);
  5738. CLI.setDebugLoc(dl)
  5739. .setChain(Chain)
  5740. .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
  5741. Type::getVoidTy(*getContext()),
  5742. getExternalSymbol(TLI->getLibcallName(LibraryCall),
  5743. TLI->getPointerTy(getDataLayout())),
  5744. std::move(Args))
  5745. .setDiscardResult()
  5746. .setTailCall(isTailCall);
  5747. std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
  5748. return CallResult.second;
  5749. }
  5750. SDValue SelectionDAG::getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst,
  5751. SDValue Src, SDValue Size, unsigned Align,
  5752. bool isVol, bool isTailCall,
  5753. MachinePointerInfo DstPtrInfo) {
  5754. assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
  5755. // Check to see if we should lower the memset to stores first.
  5756. // For cases within the target-specified limits, this is the best choice.
  5757. ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
  5758. if (ConstantSize) {
  5759. // Memset with size zero? Just return the original chain.
  5760. if (ConstantSize->isNullValue())
  5761. return Chain;
  5762. SDValue Result =
  5763. getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
  5764. Align, isVol, DstPtrInfo);
  5765. if (Result.getNode())
  5766. return Result;
  5767. }
  5768. // Then check to see if we should lower the memset with target-specific
  5769. // code. If the target chooses to do this, this is the next best.
  5770. if (TSI) {
  5771. SDValue Result = TSI->EmitTargetCodeForMemset(
  5772. *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo);
  5773. if (Result.getNode())
  5774. return Result;
  5775. }
  5776. checkAddrSpaceIsValidForLibcall(TLI, DstPtrInfo.getAddrSpace());
  5777. // Emit a library call.
  5778. TargetLowering::ArgListTy Args;
  5779. TargetLowering::ArgListEntry Entry;
  5780. Entry.Node = Dst; Entry.Ty = Type::getInt8PtrTy(*getContext());
  5781. Args.push_back(Entry);
  5782. Entry.Node = Src;
  5783. Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
  5784. Args.push_back(Entry);
  5785. Entry.Node = Size;
  5786. Entry.Ty = getDataLayout().getIntPtrType(*getContext());
  5787. Args.push_back(Entry);
  5788. // FIXME: pass in SDLoc
  5789. TargetLowering::CallLoweringInfo CLI(*this);
  5790. CLI.setDebugLoc(dl)
  5791. .setChain(Chain)
  5792. .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
  5793. Dst.getValueType().getTypeForEVT(*getContext()),
  5794. getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
  5795. TLI->getPointerTy(getDataLayout())),
  5796. std::move(Args))
  5797. .setDiscardResult()
  5798. .setTailCall(isTailCall);
  5799. std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
  5800. return CallResult.second;
  5801. }
  5802. SDValue SelectionDAG::getAtomicMemset(SDValue Chain, const SDLoc &dl,
  5803. SDValue Dst, unsigned DstAlign,
  5804. SDValue Value, SDValue Size, Type *SizeTy,
  5805. unsigned ElemSz, bool isTailCall,
  5806. MachinePointerInfo DstPtrInfo) {
  5807. // Emit a library call.
  5808. TargetLowering::ArgListTy Args;
  5809. TargetLowering::ArgListEntry Entry;
  5810. Entry.Ty = getDataLayout().getIntPtrType(*getContext());
  5811. Entry.Node = Dst;
  5812. Args.push_back(Entry);
  5813. Entry.Ty = Type::getInt8Ty(*getContext());
  5814. Entry.Node = Value;
  5815. Args.push_back(Entry);
  5816. Entry.Ty = SizeTy;
  5817. Entry.Node = Size;
  5818. Args.push_back(Entry);
  5819. RTLIB::Libcall LibraryCall =
  5820. RTLIB::getMEMSET_ELEMENT_UNORDERED_ATOMIC(ElemSz);
  5821. if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
  5822. report_fatal_error("Unsupported element size");
  5823. TargetLowering::CallLoweringInfo CLI(*this);
  5824. CLI.setDebugLoc(dl)
  5825. .setChain(Chain)
  5826. .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
  5827. Type::getVoidTy(*getContext()),
  5828. getExternalSymbol(TLI->getLibcallName(LibraryCall),
  5829. TLI->getPointerTy(getDataLayout())),
  5830. std::move(Args))
  5831. .setDiscardResult()
  5832. .setTailCall(isTailCall);
  5833. std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
  5834. return CallResult.second;
  5835. }
  5836. SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
  5837. SDVTList VTList, ArrayRef<SDValue> Ops,
  5838. MachineMemOperand *MMO) {
  5839. FoldingSetNodeID ID;
  5840. ID.AddInteger(MemVT.getRawBits());
  5841. AddNodeIDNode(ID, Opcode, VTList, Ops);
  5842. ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
  5843. void* IP = nullptr;
  5844. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
  5845. cast<AtomicSDNode>(E)->refineAlignment(MMO);
  5846. return SDValue(E, 0);
  5847. }
  5848. auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
  5849. VTList, MemVT, MMO);
  5850. createOperands(N, Ops);
  5851. CSEMap.InsertNode(N, IP);
  5852. InsertNode(N);
  5853. return SDValue(N, 0);
  5854. }
  5855. SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl,
  5856. EVT MemVT, SDVTList VTs, SDValue Chain,
  5857. SDValue Ptr, SDValue Cmp, SDValue Swp,
  5858. MachineMemOperand *MMO) {
  5859. assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
  5860. Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
  5861. assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
  5862. SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
  5863. return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
  5864. }
  5865. SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
  5866. SDValue Chain, SDValue Ptr, SDValue Val,
  5867. MachineMemOperand *MMO) {
  5868. assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
  5869. Opcode == ISD::ATOMIC_LOAD_SUB ||
  5870. Opcode == ISD::ATOMIC_LOAD_AND ||
  5871. Opcode == ISD::ATOMIC_LOAD_CLR ||
  5872. Opcode == ISD::ATOMIC_LOAD_OR ||
  5873. Opcode == ISD::ATOMIC_LOAD_XOR ||
  5874. Opcode == ISD::ATOMIC_LOAD_NAND ||
  5875. Opcode == ISD::ATOMIC_LOAD_MIN ||
  5876. Opcode == ISD::ATOMIC_LOAD_MAX ||
  5877. Opcode == ISD::ATOMIC_LOAD_UMIN ||
  5878. Opcode == ISD::ATOMIC_LOAD_UMAX ||
  5879. Opcode == ISD::ATOMIC_LOAD_FADD ||
  5880. Opcode == ISD::ATOMIC_LOAD_FSUB ||
  5881. Opcode == ISD::ATOMIC_SWAP ||
  5882. Opcode == ISD::ATOMIC_STORE) &&
  5883. "Invalid Atomic Op");
  5884. EVT VT = Val.getValueType();
  5885. SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
  5886. getVTList(VT, MVT::Other);
  5887. SDValue Ops[] = {Chain, Ptr, Val};
  5888. return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
  5889. }
  5890. SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
  5891. EVT VT, SDValue Chain, SDValue Ptr,
  5892. MachineMemOperand *MMO) {
  5893. assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
  5894. SDVTList VTs = getVTList(VT, MVT::Other);
  5895. SDValue Ops[] = {Chain, Ptr};
  5896. return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
  5897. }
  5898. /// getMergeValues - Create a MERGE_VALUES node from the given operands.
  5899. SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, const SDLoc &dl) {
  5900. if (Ops.size() == 1)
  5901. return Ops[0];
  5902. SmallVector<EVT, 4> VTs;
  5903. VTs.reserve(Ops.size());
  5904. for (unsigned i = 0; i < Ops.size(); ++i)
  5905. VTs.push_back(Ops[i].getValueType());
  5906. return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
  5907. }
  5908. SDValue SelectionDAG::getMemIntrinsicNode(
  5909. unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
  5910. EVT MemVT, MachinePointerInfo PtrInfo, unsigned Align,
  5911. MachineMemOperand::Flags Flags, unsigned Size, const AAMDNodes &AAInfo) {
  5912. if (Align == 0) // Ensure that codegen never sees alignment 0
  5913. Align = getEVTAlignment(MemVT);
  5914. if (!Size)
  5915. Size = MemVT.getStoreSize();
  5916. MachineFunction &MF = getMachineFunction();
  5917. MachineMemOperand *MMO =
  5918. MF.getMachineMemOperand(PtrInfo, Flags, Size, Align, AAInfo);
  5919. return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
  5920. }
  5921. SDValue SelectionDAG::getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl,
  5922. SDVTList VTList,
  5923. ArrayRef<SDValue> Ops, EVT MemVT,
  5924. MachineMemOperand *MMO) {
  5925. assert((Opcode == ISD::INTRINSIC_VOID ||
  5926. Opcode == ISD::INTRINSIC_W_CHAIN ||
  5927. Opcode == ISD::PREFETCH ||
  5928. Opcode == ISD::LIFETIME_START ||
  5929. Opcode == ISD::LIFETIME_END ||
  5930. ((int)Opcode <= std::numeric_limits<int>::max() &&
  5931. (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
  5932. "Opcode is not a memory-accessing opcode!");
  5933. // Memoize the node unless it returns a flag.
  5934. MemIntrinsicSDNode *N;
  5935. if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
  5936. FoldingSetNodeID ID;
  5937. AddNodeIDNode(ID, Opcode, VTList, Ops);
  5938. ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
  5939. Opcode, dl.getIROrder(), VTList, MemVT, MMO));
  5940. ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
  5941. void *IP = nullptr;
  5942. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
  5943. cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
  5944. return SDValue(E, 0);
  5945. }
  5946. N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
  5947. VTList, MemVT, MMO);
  5948. createOperands(N, Ops);
  5949. CSEMap.InsertNode(N, IP);
  5950. } else {
  5951. N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
  5952. VTList, MemVT, MMO);
  5953. createOperands(N, Ops);
  5954. }
  5955. InsertNode(N);
  5956. SDValue V(N, 0);
  5957. NewSDValueDbgMsg(V, "Creating new node: ", this);
  5958. return V;
  5959. }
  5960. SDValue SelectionDAG::getLifetimeNode(bool IsStart, const SDLoc &dl,
  5961. SDValue Chain, int FrameIndex,
  5962. int64_t Size, int64_t Offset) {
  5963. const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
  5964. const auto VTs = getVTList(MVT::Other);
  5965. SDValue Ops[2] = {
  5966. Chain,
  5967. getFrameIndex(FrameIndex,
  5968. getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
  5969. true)};
  5970. FoldingSetNodeID ID;
  5971. AddNodeIDNode(ID, Opcode, VTs, Ops);
  5972. ID.AddInteger(FrameIndex);
  5973. ID.AddInteger(Size);
  5974. ID.AddInteger(Offset);
  5975. void *IP = nullptr;
  5976. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
  5977. return SDValue(E, 0);
  5978. LifetimeSDNode *N = newSDNode<LifetimeSDNode>(
  5979. Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, Size, Offset);
  5980. createOperands(N, Ops);
  5981. CSEMap.InsertNode(N, IP);
  5982. InsertNode(N);
  5983. SDValue V(N, 0);
  5984. NewSDValueDbgMsg(V, "Creating new node: ", this);
  5985. return V;
  5986. }
  5987. /// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
  5988. /// MachinePointerInfo record from it. This is particularly useful because the
  5989. /// code generator has many cases where it doesn't bother passing in a
  5990. /// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
  5991. static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info,
  5992. SelectionDAG &DAG, SDValue Ptr,
  5993. int64_t Offset = 0) {
  5994. // If this is FI+Offset, we can model it.
  5995. if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
  5996. return MachinePointerInfo::getFixedStack(DAG.getMachineFunction(),
  5997. FI->getIndex(), Offset);
  5998. // If this is (FI+Offset1)+Offset2, we can model it.
  5999. if (Ptr.getOpcode() != ISD::ADD ||
  6000. !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
  6001. !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
  6002. return Info;
  6003. int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
  6004. return MachinePointerInfo::getFixedStack(
  6005. DAG.getMachineFunction(), FI,
  6006. Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
  6007. }
  6008. /// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
  6009. /// MachinePointerInfo record from it. This is particularly useful because the
  6010. /// code generator has many cases where it doesn't bother passing in a
  6011. /// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
  6012. static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info,
  6013. SelectionDAG &DAG, SDValue Ptr,
  6014. SDValue OffsetOp) {
  6015. // If the 'Offset' value isn't a constant, we can't handle this.
  6016. if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
  6017. return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
  6018. if (OffsetOp.isUndef())
  6019. return InferPointerInfo(Info, DAG, Ptr);
  6020. return Info;
  6021. }
  6022. SDValue SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
  6023. EVT VT, const SDLoc &dl, SDValue Chain,
  6024. SDValue Ptr, SDValue Offset,
  6025. MachinePointerInfo PtrInfo, EVT MemVT,
  6026. unsigned Alignment,
  6027. MachineMemOperand::Flags MMOFlags,
  6028. const AAMDNodes &AAInfo, const MDNode *Ranges) {
  6029. assert(Chain.getValueType() == MVT::Other &&
  6030. "Invalid chain type");
  6031. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  6032. Alignment = getEVTAlignment(MemVT);
  6033. MMOFlags |= MachineMemOperand::MOLoad;
  6034. assert((MMOFlags & MachineMemOperand::MOStore) == 0);
  6035. // If we don't have a PtrInfo, infer the trivial frame index case to simplify
  6036. // clients.
  6037. if (PtrInfo.V.isNull())
  6038. PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
  6039. MachineFunction &MF = getMachineFunction();
  6040. MachineMemOperand *MMO = MF.getMachineMemOperand(
  6041. PtrInfo, MMOFlags, MemVT.getStoreSize(), Alignment, AAInfo, Ranges);
  6042. return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
  6043. }
  6044. SDValue SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
  6045. EVT VT, const SDLoc &dl, SDValue Chain,
  6046. SDValue Ptr, SDValue Offset, EVT MemVT,
  6047. MachineMemOperand *MMO) {
  6048. if (VT == MemVT) {
  6049. ExtType = ISD::NON_EXTLOAD;
  6050. } else if (ExtType == ISD::NON_EXTLOAD) {
  6051. assert(VT == MemVT && "Non-extending load from different memory type!");
  6052. } else {
  6053. // Extending load.
  6054. assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
  6055. "Should only be an extending load, not truncating!");
  6056. assert(VT.isInteger() == MemVT.isInteger() &&
  6057. "Cannot convert from FP to Int or Int -> FP!");
  6058. assert(VT.isVector() == MemVT.isVector() &&
  6059. "Cannot use an ext load to convert to or from a vector!");
  6060. assert((!VT.isVector() ||
  6061. VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
  6062. "Cannot use an ext load to change the number of vector elements!");
  6063. }
  6064. bool Indexed = AM != ISD::UNINDEXED;
  6065. assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
  6066. SDVTList VTs = Indexed ?
  6067. getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
  6068. SDValue Ops[] = { Chain, Ptr, Offset };
  6069. FoldingSetNodeID ID;
  6070. AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
  6071. ID.AddInteger(MemVT.getRawBits());
  6072. ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
  6073. dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
  6074. ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
  6075. void *IP = nullptr;
  6076. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
  6077. cast<LoadSDNode>(E)->refineAlignment(MMO);
  6078. return SDValue(E, 0);
  6079. }
  6080. auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
  6081. ExtType, MemVT, MMO);
  6082. createOperands(N, Ops);
  6083. CSEMap.InsertNode(N, IP);
  6084. InsertNode(N);
  6085. SDValue V(N, 0);
  6086. NewSDValueDbgMsg(V, "Creating new node: ", this);
  6087. return V;
  6088. }
  6089. SDValue SelectionDAG::getLoad(EVT VT, const SDLoc &dl, SDValue Chain,
  6090. SDValue Ptr, MachinePointerInfo PtrInfo,
  6091. unsigned Alignment,
  6092. MachineMemOperand::Flags MMOFlags,
  6093. const AAMDNodes &AAInfo, const MDNode *Ranges) {
  6094. SDValue Undef = getUNDEF(Ptr.getValueType());
  6095. return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
  6096. PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
  6097. }
  6098. SDValue SelectionDAG::getLoad(EVT VT, const SDLoc &dl, SDValue Chain,
  6099. SDValue Ptr, MachineMemOperand *MMO) {
  6100. SDValue Undef = getUNDEF(Ptr.getValueType());
  6101. return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
  6102. VT, MMO);
  6103. }
  6104. SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl,
  6105. EVT VT, SDValue Chain, SDValue Ptr,
  6106. MachinePointerInfo PtrInfo, EVT MemVT,
  6107. unsigned Alignment,
  6108. MachineMemOperand::Flags MMOFlags,
  6109. const AAMDNodes &AAInfo) {
  6110. SDValue Undef = getUNDEF(Ptr.getValueType());
  6111. return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
  6112. MemVT, Alignment, MMOFlags, AAInfo);
  6113. }
  6114. SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl,
  6115. EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
  6116. MachineMemOperand *MMO) {
  6117. SDValue Undef = getUNDEF(Ptr.getValueType());
  6118. return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
  6119. MemVT, MMO);
  6120. }
  6121. SDValue SelectionDAG::getIndexedLoad(SDValue OrigLoad, const SDLoc &dl,
  6122. SDValue Base, SDValue Offset,
  6123. ISD::MemIndexedMode AM) {
  6124. LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
  6125. assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
  6126. // Don't propagate the invariant or dereferenceable flags.
  6127. auto MMOFlags =
  6128. LD->getMemOperand()->getFlags() &
  6129. ~(MachineMemOperand::MOInvariant | MachineMemOperand::MODereferenceable);
  6130. return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
  6131. LD->getChain(), Base, Offset, LD->getPointerInfo(),
  6132. LD->getMemoryVT(), LD->getAlignment(), MMOFlags,
  6133. LD->getAAInfo());
  6134. }
  6135. SDValue SelectionDAG::getStore(SDValue Chain, const SDLoc &dl, SDValue Val,
  6136. SDValue Ptr, MachinePointerInfo PtrInfo,
  6137. unsigned Alignment,
  6138. MachineMemOperand::Flags MMOFlags,
  6139. const AAMDNodes &AAInfo) {
  6140. assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
  6141. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  6142. Alignment = getEVTAlignment(Val.getValueType());
  6143. MMOFlags |= MachineMemOperand::MOStore;
  6144. assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
  6145. if (PtrInfo.V.isNull())
  6146. PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
  6147. MachineFunction &MF = getMachineFunction();
  6148. MachineMemOperand *MMO = MF.getMachineMemOperand(
  6149. PtrInfo, MMOFlags, Val.getValueType().getStoreSize(), Alignment, AAInfo);
  6150. return getStore(Chain, dl, Val, Ptr, MMO);
  6151. }
  6152. SDValue SelectionDAG::getStore(SDValue Chain, const SDLoc &dl, SDValue Val,
  6153. SDValue Ptr, MachineMemOperand *MMO) {
  6154. assert(Chain.getValueType() == MVT::Other &&
  6155. "Invalid chain type");
  6156. EVT VT = Val.getValueType();
  6157. SDVTList VTs = getVTList(MVT::Other);
  6158. SDValue Undef = getUNDEF(Ptr.getValueType());
  6159. SDValue Ops[] = { Chain, Val, Ptr, Undef };
  6160. FoldingSetNodeID ID;
  6161. AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
  6162. ID.AddInteger(VT.getRawBits());
  6163. ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
  6164. dl.getIROrder(), VTs, ISD::UNINDEXED, false, VT, MMO));
  6165. ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
  6166. void *IP = nullptr;
  6167. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
  6168. cast<StoreSDNode>(E)->refineAlignment(MMO);
  6169. return SDValue(E, 0);
  6170. }
  6171. auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
  6172. ISD::UNINDEXED, false, VT, MMO);
  6173. createOperands(N, Ops);
  6174. CSEMap.InsertNode(N, IP);
  6175. InsertNode(N);
  6176. SDValue V(N, 0);
  6177. NewSDValueDbgMsg(V, "Creating new node: ", this);
  6178. return V;
  6179. }
  6180. SDValue SelectionDAG::getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val,
  6181. SDValue Ptr, MachinePointerInfo PtrInfo,
  6182. EVT SVT, unsigned Alignment,
  6183. MachineMemOperand::Flags MMOFlags,
  6184. const AAMDNodes &AAInfo) {
  6185. assert(Chain.getValueType() == MVT::Other &&
  6186. "Invalid chain type");
  6187. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  6188. Alignment = getEVTAlignment(SVT);
  6189. MMOFlags |= MachineMemOperand::MOStore;
  6190. assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
  6191. if (PtrInfo.V.isNull())
  6192. PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
  6193. MachineFunction &MF = getMachineFunction();
  6194. MachineMemOperand *MMO = MF.getMachineMemOperand(
  6195. PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
  6196. return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
  6197. }
  6198. SDValue SelectionDAG::getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val,
  6199. SDValue Ptr, EVT SVT,
  6200. MachineMemOperand *MMO) {
  6201. EVT VT = Val.getValueType();
  6202. assert(Chain.getValueType() == MVT::Other &&
  6203. "Invalid chain type");
  6204. if (VT == SVT)
  6205. return getStore(Chain, dl, Val, Ptr, MMO);
  6206. assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
  6207. "Should only be a truncating store, not extending!");
  6208. assert(VT.isInteger() == SVT.isInteger() &&
  6209. "Can't do FP-INT conversion!");
  6210. assert(VT.isVector() == SVT.isVector() &&
  6211. "Cannot use trunc store to convert to or from a vector!");
  6212. assert((!VT.isVector() ||
  6213. VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
  6214. "Cannot use trunc store to change the number of vector elements!");
  6215. SDVTList VTs = getVTList(MVT::Other);
  6216. SDValue Undef = getUNDEF(Ptr.getValueType());
  6217. SDValue Ops[] = { Chain, Val, Ptr, Undef };
  6218. FoldingSetNodeID ID;
  6219. AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
  6220. ID.AddInteger(SVT.getRawBits());
  6221. ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
  6222. dl.getIROrder(), VTs, ISD::UNINDEXED, true, SVT, MMO));
  6223. ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
  6224. void *IP = nullptr;
  6225. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
  6226. cast<StoreSDNode>(E)->refineAlignment(MMO);
  6227. return SDValue(E, 0);
  6228. }
  6229. auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
  6230. ISD::UNINDEXED, true, SVT, MMO);
  6231. createOperands(N, Ops);
  6232. CSEMap.InsertNode(N, IP);
  6233. InsertNode(N);
  6234. SDValue V(N, 0);
  6235. NewSDValueDbgMsg(V, "Creating new node: ", this);
  6236. return V;
  6237. }
  6238. SDValue SelectionDAG::getIndexedStore(SDValue OrigStore, const SDLoc &dl,
  6239. SDValue Base, SDValue Offset,
  6240. ISD::MemIndexedMode AM) {
  6241. StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
  6242. assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
  6243. SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
  6244. SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
  6245. FoldingSetNodeID ID;
  6246. AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
  6247. ID.AddInteger(ST->getMemoryVT().getRawBits());
  6248. ID.AddInteger(ST->getRawSubclassData());
  6249. ID.AddInteger(ST->getPointerInfo().getAddrSpace());
  6250. void *IP = nullptr;
  6251. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
  6252. return SDValue(E, 0);
  6253. auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
  6254. ST->isTruncatingStore(), ST->getMemoryVT(),
  6255. ST->getMemOperand());
  6256. createOperands(N, Ops);
  6257. CSEMap.InsertNode(N, IP);
  6258. InsertNode(N);
  6259. SDValue V(N, 0);
  6260. NewSDValueDbgMsg(V, "Creating new node: ", this);
  6261. return V;
  6262. }
  6263. SDValue SelectionDAG::getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain,
  6264. SDValue Ptr, SDValue Mask, SDValue PassThru,
  6265. EVT MemVT, MachineMemOperand *MMO,
  6266. ISD::LoadExtType ExtTy, bool isExpanding) {
  6267. SDVTList VTs = getVTList(VT, MVT::Other);
  6268. SDValue Ops[] = { Chain, Ptr, Mask, PassThru };
  6269. FoldingSetNodeID ID;
  6270. AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
  6271. ID.AddInteger(MemVT.getRawBits());
  6272. ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
  6273. dl.getIROrder(), VTs, ExtTy, isExpanding, MemVT, MMO));
  6274. ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
  6275. void *IP = nullptr;
  6276. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
  6277. cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
  6278. return SDValue(E, 0);
  6279. }
  6280. auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
  6281. ExtTy, isExpanding, MemVT, MMO);
  6282. createOperands(N, Ops);
  6283. CSEMap.InsertNode(N, IP);
  6284. InsertNode(N);
  6285. SDValue V(N, 0);
  6286. NewSDValueDbgMsg(V, "Creating new node: ", this);
  6287. return V;
  6288. }
  6289. SDValue SelectionDAG::getMaskedStore(SDValue Chain, const SDLoc &dl,
  6290. SDValue Val, SDValue Ptr, SDValue Mask,
  6291. EVT MemVT, MachineMemOperand *MMO,
  6292. bool IsTruncating, bool IsCompressing) {
  6293. assert(Chain.getValueType() == MVT::Other &&
  6294. "Invalid chain type");
  6295. SDVTList VTs = getVTList(MVT::Other);
  6296. SDValue Ops[] = { Chain, Val, Ptr, Mask };
  6297. FoldingSetNodeID ID;
  6298. AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
  6299. ID.AddInteger(MemVT.getRawBits());
  6300. ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
  6301. dl.getIROrder(), VTs, IsTruncating, IsCompressing, MemVT, MMO));
  6302. ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
  6303. void *IP = nullptr;
  6304. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
  6305. cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
  6306. return SDValue(E, 0);
  6307. }
  6308. auto *N = newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
  6309. IsTruncating, IsCompressing, MemVT, MMO);
  6310. createOperands(N, Ops);
  6311. CSEMap.InsertNode(N, IP);
  6312. InsertNode(N);
  6313. SDValue V(N, 0);
  6314. NewSDValueDbgMsg(V, "Creating new node: ", this);
  6315. return V;
  6316. }
  6317. SDValue SelectionDAG::getMaskedGather(SDVTList VTs, EVT VT, const SDLoc &dl,
  6318. ArrayRef<SDValue> Ops,
  6319. MachineMemOperand *MMO,
  6320. ISD::MemIndexType IndexType) {
  6321. assert(Ops.size() == 6 && "Incompatible number of operands");
  6322. FoldingSetNodeID ID;
  6323. AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
  6324. ID.AddInteger(VT.getRawBits());
  6325. ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
  6326. dl.getIROrder(), VTs, VT, MMO, IndexType));
  6327. ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
  6328. void *IP = nullptr;
  6329. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
  6330. cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
  6331. return SDValue(E, 0);
  6332. }
  6333. auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
  6334. VTs, VT, MMO, IndexType);
  6335. createOperands(N, Ops);
  6336. assert(N->getPassThru().getValueType() == N->getValueType(0) &&
  6337. "Incompatible type of the PassThru value in MaskedGatherSDNode");
  6338. assert(N->getMask().getValueType().getVectorNumElements() ==
  6339. N->getValueType(0).getVectorNumElements() &&
  6340. "Vector width mismatch between mask and data");
  6341. assert(N->getIndex().getValueType().getVectorNumElements() >=
  6342. N->getValueType(0).getVectorNumElements() &&
  6343. "Vector width mismatch between index and data");
  6344. assert(isa<ConstantSDNode>(N->getScale()) &&
  6345. cast<ConstantSDNode>(N->getScale())->getAPIntValue().isPowerOf2() &&
  6346. "Scale should be a constant power of 2");
  6347. CSEMap.InsertNode(N, IP);
  6348. InsertNode(N);
  6349. SDValue V(N, 0);
  6350. NewSDValueDbgMsg(V, "Creating new node: ", this);
  6351. return V;
  6352. }
  6353. SDValue SelectionDAG::getMaskedScatter(SDVTList VTs, EVT VT, const SDLoc &dl,
  6354. ArrayRef<SDValue> Ops,
  6355. MachineMemOperand *MMO,
  6356. ISD::MemIndexType IndexType) {
  6357. assert(Ops.size() == 6 && "Incompatible number of operands");
  6358. FoldingSetNodeID ID;
  6359. AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
  6360. ID.AddInteger(VT.getRawBits());
  6361. ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
  6362. dl.getIROrder(), VTs, VT, MMO, IndexType));
  6363. ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
  6364. void *IP = nullptr;
  6365. if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
  6366. cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
  6367. return SDValue(E, 0);
  6368. }
  6369. auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
  6370. VTs, VT, MMO, IndexType);
  6371. createOperands(N, Ops);
  6372. assert(N->getMask().getValueType().getVectorNumElements() ==
  6373. N->getValue().getValueType().getVectorNumElements() &&
  6374. "Vector width mismatch between mask and data");
  6375. assert(N->getIndex().getValueType().getVectorNumElements() >=
  6376. N->getValue().getValueType().getVectorNumElements() &&
  6377. "Vector width mismatch between index and data");
  6378. assert(isa<ConstantSDNode>(N->getScale()) &&
  6379. cast<ConstantSDNode>(N->getScale())->getAPIntValue().isPowerOf2() &&
  6380. "Scale should be a constant power of 2");
  6381. CSEMap.InsertNode(N, IP);
  6382. InsertNode(N);
  6383. SDValue V(N, 0);
  6384. NewSDValueDbgMsg(V, "Creating new node: ", this);
  6385. return V;
  6386. }
  6387. SDValue SelectionDAG::simplifySelect(SDValue Cond, SDValue T, SDValue F) {
  6388. // select undef, T, F --> T (if T is a constant), otherwise F
  6389. // select, ?, undef, F --> F
  6390. // select, ?, T, undef --> T
  6391. if (Cond.isUndef())
  6392. return isConstantValueOfAnyType(T) ? T : F;
  6393. if (T.isUndef())
  6394. return F;
  6395. if (F.isUndef())
  6396. return T;
  6397. // select true, T, F --> T
  6398. // select false, T, F --> F
  6399. if (auto *CondC = dyn_cast<ConstantSDNode>(Cond))
  6400. return CondC->isNullValue() ? F : T;
  6401. // TODO: This should simplify VSELECT with constant condition using something
  6402. // like this (but check boolean contents to be complete?):
  6403. // if (ISD::isBuildVectorAllOnes(Cond.getNode()))
  6404. // return T;
  6405. // if (ISD::isBuildVectorAllZeros(Cond.getNode()))
  6406. // return F;
  6407. // select ?, T, T --> T
  6408. if (T == F)
  6409. return T;
  6410. return SDValue();
  6411. }
  6412. SDValue SelectionDAG::simplifyShift(SDValue X, SDValue Y) {
  6413. // shift undef, Y --> 0 (can always assume that the undef value is 0)
  6414. if (X.isUndef())
  6415. return getConstant(0, SDLoc(X.getNode()), X.getValueType());
  6416. // shift X, undef --> undef (because it may shift by the bitwidth)
  6417. if (Y.isUndef())
  6418. return getUNDEF(X.getValueType());
  6419. // shift 0, Y --> 0
  6420. // shift X, 0 --> X
  6421. if (isNullOrNullSplat(X) || isNullOrNullSplat(Y))
  6422. return X;
  6423. // shift X, C >= bitwidth(X) --> undef
  6424. // All vector elements must be too big (or undef) to avoid partial undefs.
  6425. auto isShiftTooBig = [X](ConstantSDNode *Val) {
  6426. return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
  6427. };
  6428. if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
  6429. return getUNDEF(X.getValueType());
  6430. return SDValue();
  6431. }
  6432. // TODO: Use fast-math-flags to enable more simplifications.
  6433. SDValue SelectionDAG::simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y) {
  6434. ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
  6435. if (!YC)
  6436. return SDValue();
  6437. // X + -0.0 --> X
  6438. if (Opcode == ISD::FADD)
  6439. if (YC->getValueAPF().isNegZero())
  6440. return X;
  6441. // X - +0.0 --> X
  6442. if (Opcode == ISD::FSUB)
  6443. if (YC->getValueAPF().isPosZero())
  6444. return X;
  6445. // X * 1.0 --> X
  6446. // X / 1.0 --> X
  6447. if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
  6448. if (YC->getValueAPF().isExactlyValue(1.0))
  6449. return X;
  6450. return SDValue();
  6451. }
  6452. SDValue SelectionDAG::getVAArg(EVT VT, const SDLoc &dl, SDValue Chain,
  6453. SDValue Ptr, SDValue SV, unsigned Align) {
  6454. SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
  6455. return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
  6456. }
  6457. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
  6458. ArrayRef<SDUse> Ops) {
  6459. switch (Ops.size()) {
  6460. case 0: return getNode(Opcode, DL, VT);
  6461. case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
  6462. case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
  6463. case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
  6464. default: break;
  6465. }
  6466. // Copy from an SDUse array into an SDValue array for use with
  6467. // the regular getNode logic.
  6468. SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
  6469. return getNode(Opcode, DL, VT, NewOps);
  6470. }
  6471. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
  6472. ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
  6473. unsigned NumOps = Ops.size();
  6474. switch (NumOps) {
  6475. case 0: return getNode(Opcode, DL, VT);
  6476. case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
  6477. case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
  6478. case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
  6479. default: break;
  6480. }
  6481. switch (Opcode) {
  6482. default: break;
  6483. case ISD::BUILD_VECTOR:
  6484. // Attempt to simplify BUILD_VECTOR.
  6485. if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
  6486. return V;
  6487. break;
  6488. case ISD::CONCAT_VECTORS:
  6489. if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
  6490. return V;
  6491. break;
  6492. case ISD::SELECT_CC:
  6493. assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
  6494. assert(Ops[0].getValueType() == Ops[1].getValueType() &&
  6495. "LHS and RHS of condition must have same type!");
  6496. assert(Ops[2].getValueType() == Ops[3].getValueType() &&
  6497. "True and False arms of SelectCC must have same type!");
  6498. assert(Ops[2].getValueType() == VT &&
  6499. "select_cc node must be of same type as true and false value!");
  6500. break;
  6501. case ISD::BR_CC:
  6502. assert(NumOps == 5 && "BR_CC takes 5 operands!");
  6503. assert(Ops[2].getValueType() == Ops[3].getValueType() &&
  6504. "LHS/RHS of comparison should match types!");
  6505. break;
  6506. }
  6507. // Memoize nodes.
  6508. SDNode *N;
  6509. SDVTList VTs = getVTList(VT);
  6510. if (VT != MVT::Glue) {
  6511. FoldingSetNodeID ID;
  6512. AddNodeIDNode(ID, Opcode, VTs, Ops);
  6513. void *IP = nullptr;
  6514. if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
  6515. return SDValue(E, 0);
  6516. N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
  6517. createOperands(N, Ops);
  6518. CSEMap.InsertNode(N, IP);
  6519. } else {
  6520. N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
  6521. createOperands(N, Ops);
  6522. }
  6523. InsertNode(N);
  6524. SDValue V(N, 0);
  6525. NewSDValueDbgMsg(V, "Creating new node: ", this);
  6526. return V;
  6527. }
  6528. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
  6529. ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
  6530. return getNode(Opcode, DL, getVTList(ResultTys), Ops);
  6531. }
  6532. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
  6533. ArrayRef<SDValue> Ops) {
  6534. if (VTList.NumVTs == 1)
  6535. return getNode(Opcode, DL, VTList.VTs[0], Ops);
  6536. #if 0
  6537. switch (Opcode) {
  6538. // FIXME: figure out how to safely handle things like
  6539. // int foo(int x) { return 1 << (x & 255); }
  6540. // int bar() { return foo(256); }
  6541. case ISD::SRA_PARTS:
  6542. case ISD::SRL_PARTS:
  6543. case ISD::SHL_PARTS:
  6544. if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
  6545. cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
  6546. return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
  6547. else if (N3.getOpcode() == ISD::AND)
  6548. if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
  6549. // If the and is only masking out bits that cannot effect the shift,
  6550. // eliminate the and.
  6551. unsigned NumBits = VT.getScalarSizeInBits()*2;
  6552. if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
  6553. return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
  6554. }
  6555. break;
  6556. }
  6557. #endif
  6558. // Memoize the node unless it returns a flag.
  6559. SDNode *N;
  6560. if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
  6561. FoldingSetNodeID ID;
  6562. AddNodeIDNode(ID, Opcode, VTList, Ops);
  6563. void *IP = nullptr;
  6564. if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
  6565. return SDValue(E, 0);
  6566. N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
  6567. createOperands(N, Ops);
  6568. CSEMap.InsertNode(N, IP);
  6569. } else {
  6570. N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
  6571. createOperands(N, Ops);
  6572. }
  6573. InsertNode(N);
  6574. SDValue V(N, 0);
  6575. NewSDValueDbgMsg(V, "Creating new node: ", this);
  6576. return V;
  6577. }
  6578. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
  6579. SDVTList VTList) {
  6580. return getNode(Opcode, DL, VTList, None);
  6581. }
  6582. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
  6583. SDValue N1) {
  6584. SDValue Ops[] = { N1 };
  6585. return getNode(Opcode, DL, VTList, Ops);
  6586. }
  6587. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
  6588. SDValue N1, SDValue N2) {
  6589. SDValue Ops[] = { N1, N2 };
  6590. return getNode(Opcode, DL, VTList, Ops);
  6591. }
  6592. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
  6593. SDValue N1, SDValue N2, SDValue N3) {
  6594. SDValue Ops[] = { N1, N2, N3 };
  6595. return getNode(Opcode, DL, VTList, Ops);
  6596. }
  6597. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
  6598. SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
  6599. SDValue Ops[] = { N1, N2, N3, N4 };
  6600. return getNode(Opcode, DL, VTList, Ops);
  6601. }
  6602. SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
  6603. SDValue N1, SDValue N2, SDValue N3, SDValue N4,
  6604. SDValue N5) {
  6605. SDValue Ops[] = { N1, N2, N3, N4, N5 };
  6606. return getNode(Opcode, DL, VTList, Ops);
  6607. }
  6608. SDVTList SelectionDAG::getVTList(EVT VT) {
  6609. return makeVTList(SDNode::getValueTypeList(VT), 1);
  6610. }
  6611. SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
  6612. FoldingSetNodeID ID;
  6613. ID.AddInteger(2U);
  6614. ID.AddInteger(VT1.getRawBits());
  6615. ID.AddInteger(VT2.getRawBits());
  6616. void *IP = nullptr;
  6617. SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
  6618. if (!Result) {
  6619. EVT *Array = Allocator.Allocate<EVT>(2);
  6620. Array[0] = VT1;
  6621. Array[1] = VT2;
  6622. Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
  6623. VTListMap.InsertNode(Result, IP);
  6624. }
  6625. return Result->getSDVTList();
  6626. }
  6627. SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
  6628. FoldingSetNodeID ID;
  6629. ID.AddInteger(3U);
  6630. ID.AddInteger(VT1.getRawBits());
  6631. ID.AddInteger(VT2.getRawBits());
  6632. ID.AddInteger(VT3.getRawBits());
  6633. void *IP = nullptr;
  6634. SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
  6635. if (!Result) {
  6636. EVT *Array = Allocator.Allocate<EVT>(3);
  6637. Array[0] = VT1;
  6638. Array[1] = VT2;
  6639. Array[2] = VT3;
  6640. Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
  6641. VTListMap.InsertNode(Result, IP);
  6642. }
  6643. return Result->getSDVTList();
  6644. }
  6645. SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
  6646. FoldingSetNodeID ID;
  6647. ID.AddInteger(4U);
  6648. ID.AddInteger(VT1.getRawBits());
  6649. ID.AddInteger(VT2.getRawBits());
  6650. ID.AddInteger(VT3.getRawBits());
  6651. ID.AddInteger(VT4.getRawBits());
  6652. void *IP = nullptr;
  6653. SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
  6654. if (!Result) {
  6655. EVT *Array = Allocator.Allocate<EVT>(4);
  6656. Array[0] = VT1;
  6657. Array[1] = VT2;
  6658. Array[2] = VT3;
  6659. Array[3] = VT4;
  6660. Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
  6661. VTListMap.InsertNode(Result, IP);
  6662. }
  6663. return Result->getSDVTList();
  6664. }
  6665. SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
  6666. unsigned NumVTs = VTs.size();
  6667. FoldingSetNodeID ID;
  6668. ID.AddInteger(NumVTs);
  6669. for (unsigned index = 0; index < NumVTs; index++) {
  6670. ID.AddInteger(VTs[index].getRawBits());
  6671. }
  6672. void *IP = nullptr;
  6673. SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
  6674. if (!Result) {
  6675. EVT *Array = Allocator.Allocate<EVT>(NumVTs);
  6676. llvm::copy(VTs, Array);
  6677. Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
  6678. VTListMap.InsertNode(Result, IP);
  6679. }
  6680. return Result->getSDVTList();
  6681. }
  6682. /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
  6683. /// specified operands. If the resultant node already exists in the DAG,
  6684. /// this does not modify the specified node, instead it returns the node that
  6685. /// already exists. If the resultant node does not exist in the DAG, the
  6686. /// input node is returned. As a degenerate case, if you specify the same
  6687. /// input operands as the node already has, the input node is returned.
  6688. SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
  6689. assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
  6690. // Check to see if there is no change.
  6691. if (Op == N->getOperand(0)) return N;
  6692. // See if the modified node already exists.
  6693. void *InsertPos = nullptr;
  6694. if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
  6695. return Existing;
  6696. // Nope it doesn't. Remove the node from its current place in the maps.
  6697. if (InsertPos)
  6698. if (!RemoveNodeFromCSEMaps(N))
  6699. InsertPos = nullptr;
  6700. // Now we update the operands.
  6701. N->OperandList[0].set(Op);
  6702. updateDivergence(N);
  6703. // If this gets put into a CSE map, add it.
  6704. if (InsertPos) CSEMap.InsertNode(N, InsertPos);
  6705. return N;
  6706. }
  6707. SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
  6708. assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
  6709. // Check to see if there is no change.
  6710. if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
  6711. return N; // No operands changed, just return the input node.
  6712. // See if the modified node already exists.
  6713. void *InsertPos = nullptr;
  6714. if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
  6715. return Existing;
  6716. // Nope it doesn't. Remove the node from its current place in the maps.
  6717. if (InsertPos)
  6718. if (!RemoveNodeFromCSEMaps(N))
  6719. InsertPos = nullptr;
  6720. // Now we update the operands.
  6721. if (N->OperandList[0] != Op1)
  6722. N->OperandList[0].set(Op1);
  6723. if (N->OperandList[1] != Op2)
  6724. N->OperandList[1].set(Op2);
  6725. updateDivergence(N);
  6726. // If this gets put into a CSE map, add it.
  6727. if (InsertPos) CSEMap.InsertNode(N, InsertPos);
  6728. return N;
  6729. }
  6730. SDNode *SelectionDAG::
  6731. UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
  6732. SDValue Ops[] = { Op1, Op2, Op3 };
  6733. return UpdateNodeOperands(N, Ops);
  6734. }
  6735. SDNode *SelectionDAG::
  6736. UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
  6737. SDValue Op3, SDValue Op4) {
  6738. SDValue Ops[] = { Op1, Op2, Op3, Op4 };
  6739. return UpdateNodeOperands(N, Ops);
  6740. }
  6741. SDNode *SelectionDAG::
  6742. UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
  6743. SDValue Op3, SDValue Op4, SDValue Op5) {
  6744. SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
  6745. return UpdateNodeOperands(N, Ops);
  6746. }
  6747. SDNode *SelectionDAG::
  6748. UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
  6749. unsigned NumOps = Ops.size();
  6750. assert(N->getNumOperands() == NumOps &&
  6751. "Update with wrong number of operands");
  6752. // If no operands changed just return the input node.
  6753. if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
  6754. return N;
  6755. // See if the modified node already exists.
  6756. void *InsertPos = nullptr;
  6757. if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
  6758. return Existing;
  6759. // Nope it doesn't. Remove the node from its current place in the maps.
  6760. if (InsertPos)
  6761. if (!RemoveNodeFromCSEMaps(N))
  6762. InsertPos = nullptr;
  6763. // Now we update the operands.
  6764. for (unsigned i = 0; i != NumOps; ++i)
  6765. if (N->OperandList[i] != Ops[i])
  6766. N->OperandList[i].set(Ops[i]);
  6767. updateDivergence(N);
  6768. // If this gets put into a CSE map, add it.
  6769. if (InsertPos) CSEMap.InsertNode(N, InsertPos);
  6770. return N;
  6771. }
  6772. /// DropOperands - Release the operands and set this node to have
  6773. /// zero operands.
  6774. void SDNode::DropOperands() {
  6775. // Unlike the code in MorphNodeTo that does this, we don't need to
  6776. // watch for dead nodes here.
  6777. for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
  6778. SDUse &Use = *I++;
  6779. Use.set(SDValue());
  6780. }
  6781. }
  6782. void SelectionDAG::setNodeMemRefs(MachineSDNode *N,
  6783. ArrayRef<MachineMemOperand *> NewMemRefs) {
  6784. if (NewMemRefs.empty()) {
  6785. N->clearMemRefs();
  6786. return;
  6787. }
  6788. // Check if we can avoid allocating by storing a single reference directly.
  6789. if (NewMemRefs.size() == 1) {
  6790. N->MemRefs = NewMemRefs[0];
  6791. N->NumMemRefs = 1;
  6792. return;
  6793. }
  6794. MachineMemOperand **MemRefsBuffer =
  6795. Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
  6796. llvm::copy(NewMemRefs, MemRefsBuffer);
  6797. N->MemRefs = MemRefsBuffer;
  6798. N->NumMemRefs = static_cast<int>(NewMemRefs.size());
  6799. }
  6800. /// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
  6801. /// machine opcode.
  6802. ///
  6803. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  6804. EVT VT) {
  6805. SDVTList VTs = getVTList(VT);
  6806. return SelectNodeTo(N, MachineOpc, VTs, None);
  6807. }
  6808. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  6809. EVT VT, SDValue Op1) {
  6810. SDVTList VTs = getVTList(VT);
  6811. SDValue Ops[] = { Op1 };
  6812. return SelectNodeTo(N, MachineOpc, VTs, Ops);
  6813. }
  6814. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  6815. EVT VT, SDValue Op1,
  6816. SDValue Op2) {
  6817. SDVTList VTs = getVTList(VT);
  6818. SDValue Ops[] = { Op1, Op2 };
  6819. return SelectNodeTo(N, MachineOpc, VTs, Ops);
  6820. }
  6821. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  6822. EVT VT, SDValue Op1,
  6823. SDValue Op2, SDValue Op3) {
  6824. SDVTList VTs = getVTList(VT);
  6825. SDValue Ops[] = { Op1, Op2, Op3 };
  6826. return SelectNodeTo(N, MachineOpc, VTs, Ops);
  6827. }
  6828. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  6829. EVT VT, ArrayRef<SDValue> Ops) {
  6830. SDVTList VTs = getVTList(VT);
  6831. return SelectNodeTo(N, MachineOpc, VTs, Ops);
  6832. }
  6833. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  6834. EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
  6835. SDVTList VTs = getVTList(VT1, VT2);
  6836. return SelectNodeTo(N, MachineOpc, VTs, Ops);
  6837. }
  6838. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  6839. EVT VT1, EVT VT2) {
  6840. SDVTList VTs = getVTList(VT1, VT2);
  6841. return SelectNodeTo(N, MachineOpc, VTs, None);
  6842. }
  6843. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  6844. EVT VT1, EVT VT2, EVT VT3,
  6845. ArrayRef<SDValue> Ops) {
  6846. SDVTList VTs = getVTList(VT1, VT2, VT3);
  6847. return SelectNodeTo(N, MachineOpc, VTs, Ops);
  6848. }
  6849. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  6850. EVT VT1, EVT VT2,
  6851. SDValue Op1, SDValue Op2) {
  6852. SDVTList VTs = getVTList(VT1, VT2);
  6853. SDValue Ops[] = { Op1, Op2 };
  6854. return SelectNodeTo(N, MachineOpc, VTs, Ops);
  6855. }
  6856. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  6857. SDVTList VTs,ArrayRef<SDValue> Ops) {
  6858. SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
  6859. // Reset the NodeID to -1.
  6860. New->setNodeId(-1);
  6861. if (New != N) {
  6862. ReplaceAllUsesWith(N, New);
  6863. RemoveDeadNode(N);
  6864. }
  6865. return New;
  6866. }
  6867. /// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
  6868. /// the line number information on the merged node since it is not possible to
  6869. /// preserve the information that operation is associated with multiple lines.
  6870. /// This will make the debugger working better at -O0, were there is a higher
  6871. /// probability having other instructions associated with that line.
  6872. ///
  6873. /// For IROrder, we keep the smaller of the two
  6874. SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
  6875. DebugLoc NLoc = N->getDebugLoc();
  6876. if (NLoc && OptLevel == CodeGenOpt::None && OLoc.getDebugLoc() != NLoc) {
  6877. N->setDebugLoc(DebugLoc());
  6878. }
  6879. unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
  6880. N->setIROrder(Order);
  6881. return N;
  6882. }
  6883. /// MorphNodeTo - This *mutates* the specified node to have the specified
  6884. /// return type, opcode, and operands.
  6885. ///
  6886. /// Note that MorphNodeTo returns the resultant node. If there is already a
  6887. /// node of the specified opcode and operands, it returns that node instead of
  6888. /// the current one. Note that the SDLoc need not be the same.
  6889. ///
  6890. /// Using MorphNodeTo is faster than creating a new node and swapping it in
  6891. /// with ReplaceAllUsesWith both because it often avoids allocating a new
  6892. /// node, and because it doesn't require CSE recalculation for any of
  6893. /// the node's users.
  6894. ///
  6895. /// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
  6896. /// As a consequence it isn't appropriate to use from within the DAG combiner or
  6897. /// the legalizer which maintain worklists that would need to be updated when
  6898. /// deleting things.
  6899. SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
  6900. SDVTList VTs, ArrayRef<SDValue> Ops) {
  6901. // If an identical node already exists, use it.
  6902. void *IP = nullptr;
  6903. if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
  6904. FoldingSetNodeID ID;
  6905. AddNodeIDNode(ID, Opc, VTs, Ops);
  6906. if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
  6907. return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
  6908. }
  6909. if (!RemoveNodeFromCSEMaps(N))
  6910. IP = nullptr;
  6911. // Start the morphing.
  6912. N->NodeType = Opc;
  6913. N->ValueList = VTs.VTs;
  6914. N->NumValues = VTs.NumVTs;
  6915. // Clear the operands list, updating used nodes to remove this from their
  6916. // use list. Keep track of any operands that become dead as a result.
  6917. SmallPtrSet<SDNode*, 16> DeadNodeSet;
  6918. for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
  6919. SDUse &Use = *I++;
  6920. SDNode *Used = Use.getNode();
  6921. Use.set(SDValue());
  6922. if (Used->use_empty())
  6923. DeadNodeSet.insert(Used);
  6924. }
  6925. // For MachineNode, initialize the memory references information.
  6926. if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
  6927. MN->clearMemRefs();
  6928. // Swap for an appropriately sized array from the recycler.
  6929. removeOperands(N);
  6930. createOperands(N, Ops);
  6931. // Delete any nodes that are still dead after adding the uses for the
  6932. // new operands.
  6933. if (!DeadNodeSet.empty()) {
  6934. SmallVector<SDNode *, 16> DeadNodes;
  6935. for (SDNode *N : DeadNodeSet)
  6936. if (N->use_empty())
  6937. DeadNodes.push_back(N);
  6938. RemoveDeadNodes(DeadNodes);
  6939. }
  6940. if (IP)
  6941. CSEMap.InsertNode(N, IP); // Memoize the new node.
  6942. return N;
  6943. }
  6944. SDNode* SelectionDAG::mutateStrictFPToFP(SDNode *Node) {
  6945. unsigned OrigOpc = Node->getOpcode();
  6946. unsigned NewOpc;
  6947. switch (OrigOpc) {
  6948. default:
  6949. llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
  6950. case ISD::STRICT_FADD: NewOpc = ISD::FADD; break;
  6951. case ISD::STRICT_FSUB: NewOpc = ISD::FSUB; break;
  6952. case ISD::STRICT_FMUL: NewOpc = ISD::FMUL; break;
  6953. case ISD::STRICT_FDIV: NewOpc = ISD::FDIV; break;
  6954. case ISD::STRICT_FREM: NewOpc = ISD::FREM; break;
  6955. case ISD::STRICT_FMA: NewOpc = ISD::FMA; break;
  6956. case ISD::STRICT_FSQRT: NewOpc = ISD::FSQRT; break;
  6957. case ISD::STRICT_FPOW: NewOpc = ISD::FPOW; break;
  6958. case ISD::STRICT_FPOWI: NewOpc = ISD::FPOWI; break;
  6959. case ISD::STRICT_FSIN: NewOpc = ISD::FSIN; break;
  6960. case ISD::STRICT_FCOS: NewOpc = ISD::FCOS; break;
  6961. case ISD::STRICT_FEXP: NewOpc = ISD::FEXP; break;
  6962. case ISD::STRICT_FEXP2: NewOpc = ISD::FEXP2; break;
  6963. case ISD::STRICT_FLOG: NewOpc = ISD::FLOG; break;
  6964. case ISD::STRICT_FLOG10: NewOpc = ISD::FLOG10; break;
  6965. case ISD::STRICT_FLOG2: NewOpc = ISD::FLOG2; break;
  6966. case ISD::STRICT_FRINT: NewOpc = ISD::FRINT; break;
  6967. case ISD::STRICT_FNEARBYINT: NewOpc = ISD::FNEARBYINT; break;
  6968. case ISD::STRICT_FMAXNUM: NewOpc = ISD::FMAXNUM; break;
  6969. case ISD::STRICT_FMINNUM: NewOpc = ISD::FMINNUM; break;
  6970. case ISD::STRICT_FCEIL: NewOpc = ISD::FCEIL; break;
  6971. case ISD::STRICT_FFLOOR: NewOpc = ISD::FFLOOR; break;
  6972. case ISD::STRICT_FROUND: NewOpc = ISD::FROUND; break;
  6973. case ISD::STRICT_FTRUNC: NewOpc = ISD::FTRUNC; break;
  6974. case ISD::STRICT_FP_ROUND: NewOpc = ISD::FP_ROUND; break;
  6975. case ISD::STRICT_FP_EXTEND: NewOpc = ISD::FP_EXTEND; break;
  6976. case ISD::STRICT_FP_TO_SINT: NewOpc = ISD::FP_TO_SINT; break;
  6977. case ISD::STRICT_FP_TO_UINT: NewOpc = ISD::FP_TO_UINT; break;
  6978. }
  6979. assert(Node->getNumValues() == 2 && "Unexpected number of results!");
  6980. // We're taking this node out of the chain, so we need to re-link things.
  6981. SDValue InputChain = Node->getOperand(0);
  6982. SDValue OutputChain = SDValue(Node, 1);
  6983. ReplaceAllUsesOfValueWith(OutputChain, InputChain);
  6984. SmallVector<SDValue, 3> Ops;
  6985. for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
  6986. Ops.push_back(Node->getOperand(i));
  6987. SDVTList VTs = getVTList(Node->getValueType(0));
  6988. SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
  6989. // MorphNodeTo can operate in two ways: if an existing node with the
  6990. // specified operands exists, it can just return it. Otherwise, it
  6991. // updates the node in place to have the requested operands.
  6992. if (Res == Node) {
  6993. // If we updated the node in place, reset the node ID. To the isel,
  6994. // this should be just like a newly allocated machine node.
  6995. Res->setNodeId(-1);
  6996. } else {
  6997. ReplaceAllUsesWith(Node, Res);
  6998. RemoveDeadNode(Node);
  6999. }
  7000. return Res;
  7001. }
  7002. /// getMachineNode - These are used for target selectors to create a new node
  7003. /// with specified return type(s), MachineInstr opcode, and operands.
  7004. ///
  7005. /// Note that getMachineNode returns the resultant node. If there is already a
  7006. /// node of the specified opcode and operands, it returns that node instead of
  7007. /// the current one.
  7008. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
  7009. EVT VT) {
  7010. SDVTList VTs = getVTList(VT);
  7011. return getMachineNode(Opcode, dl, VTs, None);
  7012. }
  7013. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
  7014. EVT VT, SDValue Op1) {
  7015. SDVTList VTs = getVTList(VT);
  7016. SDValue Ops[] = { Op1 };
  7017. return getMachineNode(Opcode, dl, VTs, Ops);
  7018. }
  7019. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
  7020. EVT VT, SDValue Op1, SDValue Op2) {
  7021. SDVTList VTs = getVTList(VT);
  7022. SDValue Ops[] = { Op1, Op2 };
  7023. return getMachineNode(Opcode, dl, VTs, Ops);
  7024. }
  7025. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
  7026. EVT VT, SDValue Op1, SDValue Op2,
  7027. SDValue Op3) {
  7028. SDVTList VTs = getVTList(VT);
  7029. SDValue Ops[] = { Op1, Op2, Op3 };
  7030. return getMachineNode(Opcode, dl, VTs, Ops);
  7031. }
  7032. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
  7033. EVT VT, ArrayRef<SDValue> Ops) {
  7034. SDVTList VTs = getVTList(VT);
  7035. return getMachineNode(Opcode, dl, VTs, Ops);
  7036. }
  7037. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
  7038. EVT VT1, EVT VT2, SDValue Op1,
  7039. SDValue Op2) {
  7040. SDVTList VTs = getVTList(VT1, VT2);
  7041. SDValue Ops[] = { Op1, Op2 };
  7042. return getMachineNode(Opcode, dl, VTs, Ops);
  7043. }
  7044. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
  7045. EVT VT1, EVT VT2, SDValue Op1,
  7046. SDValue Op2, SDValue Op3) {
  7047. SDVTList VTs = getVTList(VT1, VT2);
  7048. SDValue Ops[] = { Op1, Op2, Op3 };
  7049. return getMachineNode(Opcode, dl, VTs, Ops);
  7050. }
  7051. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
  7052. EVT VT1, EVT VT2,
  7053. ArrayRef<SDValue> Ops) {
  7054. SDVTList VTs = getVTList(VT1, VT2);
  7055. return getMachineNode(Opcode, dl, VTs, Ops);
  7056. }
  7057. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
  7058. EVT VT1, EVT VT2, EVT VT3,
  7059. SDValue Op1, SDValue Op2) {
  7060. SDVTList VTs = getVTList(VT1, VT2, VT3);
  7061. SDValue Ops[] = { Op1, Op2 };
  7062. return getMachineNode(Opcode, dl, VTs, Ops);
  7063. }
  7064. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
  7065. EVT VT1, EVT VT2, EVT VT3,
  7066. SDValue Op1, SDValue Op2,
  7067. SDValue Op3) {
  7068. SDVTList VTs = getVTList(VT1, VT2, VT3);
  7069. SDValue Ops[] = { Op1, Op2, Op3 };
  7070. return getMachineNode(Opcode, dl, VTs, Ops);
  7071. }
  7072. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
  7073. EVT VT1, EVT VT2, EVT VT3,
  7074. ArrayRef<SDValue> Ops) {
  7075. SDVTList VTs = getVTList(VT1, VT2, VT3);
  7076. return getMachineNode(Opcode, dl, VTs, Ops);
  7077. }
  7078. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
  7079. ArrayRef<EVT> ResultTys,
  7080. ArrayRef<SDValue> Ops) {
  7081. SDVTList VTs = getVTList(ResultTys);
  7082. return getMachineNode(Opcode, dl, VTs, Ops);
  7083. }
  7084. MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &DL,
  7085. SDVTList VTs,
  7086. ArrayRef<SDValue> Ops) {
  7087. bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
  7088. MachineSDNode *N;
  7089. void *IP = nullptr;
  7090. if (DoCSE) {
  7091. FoldingSetNodeID ID;
  7092. AddNodeIDNode(ID, ~Opcode, VTs, Ops);
  7093. IP = nullptr;
  7094. if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
  7095. return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
  7096. }
  7097. }
  7098. // Allocate a new MachineSDNode.
  7099. N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
  7100. createOperands(N, Ops);
  7101. if (DoCSE)
  7102. CSEMap.InsertNode(N, IP);
  7103. InsertNode(N);
  7104. NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
  7105. return N;
  7106. }
  7107. /// getTargetExtractSubreg - A convenience function for creating
  7108. /// TargetOpcode::EXTRACT_SUBREG nodes.
  7109. SDValue SelectionDAG::getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT,
  7110. SDValue Operand) {
  7111. SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
  7112. SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
  7113. VT, Operand, SRIdxVal);
  7114. return SDValue(Subreg, 0);
  7115. }
  7116. /// getTargetInsertSubreg - A convenience function for creating
  7117. /// TargetOpcode::INSERT_SUBREG nodes.
  7118. SDValue SelectionDAG::getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT,
  7119. SDValue Operand, SDValue Subreg) {
  7120. SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
  7121. SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
  7122. VT, Operand, Subreg, SRIdxVal);
  7123. return SDValue(Result, 0);
  7124. }
  7125. /// getNodeIfExists - Get the specified node if it's already available, or
  7126. /// else return NULL.
  7127. SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
  7128. ArrayRef<SDValue> Ops,
  7129. const SDNodeFlags Flags) {
  7130. if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
  7131. FoldingSetNodeID ID;
  7132. AddNodeIDNode(ID, Opcode, VTList, Ops);
  7133. void *IP = nullptr;
  7134. if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
  7135. E->intersectFlagsWith(Flags);
  7136. return E;
  7137. }
  7138. }
  7139. return nullptr;
  7140. }
  7141. /// getDbgValue - Creates a SDDbgValue node.
  7142. ///
  7143. /// SDNode
  7144. SDDbgValue *SelectionDAG::getDbgValue(DIVariable *Var, DIExpression *Expr,
  7145. SDNode *N, unsigned R, bool IsIndirect,
  7146. const DebugLoc &DL, unsigned O) {
  7147. assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
  7148. "Expected inlined-at fields to agree");
  7149. return new (DbgInfo->getAlloc())
  7150. SDDbgValue(Var, Expr, N, R, IsIndirect, DL, O);
  7151. }
  7152. /// Constant
  7153. SDDbgValue *SelectionDAG::getConstantDbgValue(DIVariable *Var,
  7154. DIExpression *Expr,
  7155. const Value *C,
  7156. const DebugLoc &DL, unsigned O) {
  7157. assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
  7158. "Expected inlined-at fields to agree");
  7159. return new (DbgInfo->getAlloc()) SDDbgValue(Var, Expr, C, DL, O);
  7160. }
  7161. /// FrameIndex
  7162. SDDbgValue *SelectionDAG::getFrameIndexDbgValue(DIVariable *Var,
  7163. DIExpression *Expr, unsigned FI,
  7164. bool IsIndirect,
  7165. const DebugLoc &DL,
  7166. unsigned O) {
  7167. assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
  7168. "Expected inlined-at fields to agree");
  7169. return new (DbgInfo->getAlloc())
  7170. SDDbgValue(Var, Expr, FI, IsIndirect, DL, O, SDDbgValue::FRAMEIX);
  7171. }
  7172. /// VReg
  7173. SDDbgValue *SelectionDAG::getVRegDbgValue(DIVariable *Var,
  7174. DIExpression *Expr,
  7175. unsigned VReg, bool IsIndirect,
  7176. const DebugLoc &DL, unsigned O) {
  7177. assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
  7178. "Expected inlined-at fields to agree");
  7179. return new (DbgInfo->getAlloc())
  7180. SDDbgValue(Var, Expr, VReg, IsIndirect, DL, O, SDDbgValue::VREG);
  7181. }
  7182. void SelectionDAG::transferDbgValues(SDValue From, SDValue To,
  7183. unsigned OffsetInBits, unsigned SizeInBits,
  7184. bool InvalidateDbg) {
  7185. SDNode *FromNode = From.getNode();
  7186. SDNode *ToNode = To.getNode();
  7187. assert(FromNode && ToNode && "Can't modify dbg values");
  7188. // PR35338
  7189. // TODO: assert(From != To && "Redundant dbg value transfer");
  7190. // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
  7191. if (From == To || FromNode == ToNode)
  7192. return;
  7193. if (!FromNode->getHasDebugValue())
  7194. return;
  7195. SmallVector<SDDbgValue *, 2> ClonedDVs;
  7196. for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
  7197. if (Dbg->getKind() != SDDbgValue::SDNODE || Dbg->isInvalidated())
  7198. continue;
  7199. // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
  7200. // Just transfer the dbg value attached to From.
  7201. if (Dbg->getResNo() != From.getResNo())
  7202. continue;
  7203. DIVariable *Var = Dbg->getVariable();
  7204. auto *Expr = Dbg->getExpression();
  7205. // If a fragment is requested, update the expression.
  7206. if (SizeInBits) {
  7207. // When splitting a larger (e.g., sign-extended) value whose
  7208. // lower bits are described with an SDDbgValue, do not attempt
  7209. // to transfer the SDDbgValue to the upper bits.
  7210. if (auto FI = Expr->getFragmentInfo())
  7211. if (OffsetInBits + SizeInBits > FI->SizeInBits)
  7212. continue;
  7213. auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
  7214. SizeInBits);
  7215. if (!Fragment)
  7216. continue;
  7217. Expr = *Fragment;
  7218. }
  7219. // Clone the SDDbgValue and move it to To.
  7220. SDDbgValue *Clone =
  7221. getDbgValue(Var, Expr, ToNode, To.getResNo(), Dbg->isIndirect(),
  7222. Dbg->getDebugLoc(), Dbg->getOrder());
  7223. ClonedDVs.push_back(Clone);
  7224. if (InvalidateDbg) {
  7225. // Invalidate value and indicate the SDDbgValue should not be emitted.
  7226. Dbg->setIsInvalidated();
  7227. Dbg->setIsEmitted();
  7228. }
  7229. }
  7230. for (SDDbgValue *Dbg : ClonedDVs)
  7231. AddDbgValue(Dbg, ToNode, false);
  7232. }
  7233. void SelectionDAG::salvageDebugInfo(SDNode &N) {
  7234. if (!N.getHasDebugValue())
  7235. return;
  7236. SmallVector<SDDbgValue *, 2> ClonedDVs;
  7237. for (auto DV : GetDbgValues(&N)) {
  7238. if (DV->isInvalidated())
  7239. continue;
  7240. switch (N.getOpcode()) {
  7241. default:
  7242. break;
  7243. case ISD::ADD:
  7244. SDValue N0 = N.getOperand(0);
  7245. SDValue N1 = N.getOperand(1);
  7246. if (!isConstantIntBuildVectorOrConstantInt(N0) &&
  7247. isConstantIntBuildVectorOrConstantInt(N1)) {
  7248. uint64_t Offset = N.getConstantOperandVal(1);
  7249. // Rewrite an ADD constant node into a DIExpression. Since we are
  7250. // performing arithmetic to compute the variable's *value* in the
  7251. // DIExpression, we need to mark the expression with a
  7252. // DW_OP_stack_value.
  7253. auto *DIExpr = DV->getExpression();
  7254. DIExpr =
  7255. DIExpression::prepend(DIExpr, DIExpression::StackValue, Offset);
  7256. SDDbgValue *Clone =
  7257. getDbgValue(DV->getVariable(), DIExpr, N0.getNode(), N0.getResNo(),
  7258. DV->isIndirect(), DV->getDebugLoc(), DV->getOrder());
  7259. ClonedDVs.push_back(Clone);
  7260. DV->setIsInvalidated();
  7261. DV->setIsEmitted();
  7262. LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
  7263. N0.getNode()->dumprFull(this);
  7264. dbgs() << " into " << *DIExpr << '\n');
  7265. }
  7266. }
  7267. }
  7268. for (SDDbgValue *Dbg : ClonedDVs)
  7269. AddDbgValue(Dbg, Dbg->getSDNode(), false);
  7270. }
  7271. /// Creates a SDDbgLabel node.
  7272. SDDbgLabel *SelectionDAG::getDbgLabel(DILabel *Label,
  7273. const DebugLoc &DL, unsigned O) {
  7274. assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
  7275. "Expected inlined-at fields to agree");
  7276. return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
  7277. }
  7278. namespace {
  7279. /// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
  7280. /// pointed to by a use iterator is deleted, increment the use iterator
  7281. /// so that it doesn't dangle.
  7282. ///
  7283. class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
  7284. SDNode::use_iterator &UI;
  7285. SDNode::use_iterator &UE;
  7286. void NodeDeleted(SDNode *N, SDNode *E) override {
  7287. // Increment the iterator as needed.
  7288. while (UI != UE && N == *UI)
  7289. ++UI;
  7290. }
  7291. public:
  7292. RAUWUpdateListener(SelectionDAG &d,
  7293. SDNode::use_iterator &ui,
  7294. SDNode::use_iterator &ue)
  7295. : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
  7296. };
  7297. } // end anonymous namespace
  7298. /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
  7299. /// This can cause recursive merging of nodes in the DAG.
  7300. ///
  7301. /// This version assumes From has a single result value.
  7302. ///
  7303. void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
  7304. SDNode *From = FromN.getNode();
  7305. assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
  7306. "Cannot replace with this method!");
  7307. assert(From != To.getNode() && "Cannot replace uses of with self");
  7308. // Preserve Debug Values
  7309. transferDbgValues(FromN, To);
  7310. // Iterate over all the existing uses of From. New uses will be added
  7311. // to the beginning of the use list, which we avoid visiting.
  7312. // This specifically avoids visiting uses of From that arise while the
  7313. // replacement is happening, because any such uses would be the result
  7314. // of CSE: If an existing node looks like From after one of its operands
  7315. // is replaced by To, we don't want to replace of all its users with To
  7316. // too. See PR3018 for more info.
  7317. SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
  7318. RAUWUpdateListener Listener(*this, UI, UE);
  7319. while (UI != UE) {
  7320. SDNode *User = *UI;
  7321. // This node is about to morph, remove its old self from the CSE maps.
  7322. RemoveNodeFromCSEMaps(User);
  7323. // A user can appear in a use list multiple times, and when this
  7324. // happens the uses are usually next to each other in the list.
  7325. // To help reduce the number of CSE recomputations, process all
  7326. // the uses of this user that we can find this way.
  7327. do {
  7328. SDUse &Use = UI.getUse();
  7329. ++UI;
  7330. Use.set(To);
  7331. if (To->isDivergent() != From->isDivergent())
  7332. updateDivergence(User);
  7333. } while (UI != UE && *UI == User);
  7334. // Now that we have modified User, add it back to the CSE maps. If it
  7335. // already exists there, recursively merge the results together.
  7336. AddModifiedNodeToCSEMaps(User);
  7337. }
  7338. // If we just RAUW'd the root, take note.
  7339. if (FromN == getRoot())
  7340. setRoot(To);
  7341. }
  7342. /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
  7343. /// This can cause recursive merging of nodes in the DAG.
  7344. ///
  7345. /// This version assumes that for each value of From, there is a
  7346. /// corresponding value in To in the same position with the same type.
  7347. ///
  7348. void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
  7349. #ifndef NDEBUG
  7350. for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
  7351. assert((!From->hasAnyUseOfValue(i) ||
  7352. From->getValueType(i) == To->getValueType(i)) &&
  7353. "Cannot use this version of ReplaceAllUsesWith!");
  7354. #endif
  7355. // Handle the trivial case.
  7356. if (From == To)
  7357. return;
  7358. // Preserve Debug Info. Only do this if there's a use.
  7359. for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
  7360. if (From->hasAnyUseOfValue(i)) {
  7361. assert((i < To->getNumValues()) && "Invalid To location");
  7362. transferDbgValues(SDValue(From, i), SDValue(To, i));
  7363. }
  7364. // Iterate over just the existing users of From. See the comments in
  7365. // the ReplaceAllUsesWith above.
  7366. SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
  7367. RAUWUpdateListener Listener(*this, UI, UE);
  7368. while (UI != UE) {
  7369. SDNode *User = *UI;
  7370. // This node is about to morph, remove its old self from the CSE maps.
  7371. RemoveNodeFromCSEMaps(User);
  7372. // A user can appear in a use list multiple times, and when this
  7373. // happens the uses are usually next to each other in the list.
  7374. // To help reduce the number of CSE recomputations, process all
  7375. // the uses of this user that we can find this way.
  7376. do {
  7377. SDUse &Use = UI.getUse();
  7378. ++UI;
  7379. Use.setNode(To);
  7380. if (To->isDivergent() != From->isDivergent())
  7381. updateDivergence(User);
  7382. } while (UI != UE && *UI == User);
  7383. // Now that we have modified User, add it back to the CSE maps. If it
  7384. // already exists there, recursively merge the results together.
  7385. AddModifiedNodeToCSEMaps(User);
  7386. }
  7387. // If we just RAUW'd the root, take note.
  7388. if (From == getRoot().getNode())
  7389. setRoot(SDValue(To, getRoot().getResNo()));
  7390. }
  7391. /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
  7392. /// This can cause recursive merging of nodes in the DAG.
  7393. ///
  7394. /// This version can replace From with any result values. To must match the
  7395. /// number and types of values returned by From.
  7396. void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
  7397. if (From->getNumValues() == 1) // Handle the simple case efficiently.
  7398. return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
  7399. // Preserve Debug Info.
  7400. for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
  7401. transferDbgValues(SDValue(From, i), To[i]);
  7402. // Iterate over just the existing users of From. See the comments in
  7403. // the ReplaceAllUsesWith above.
  7404. SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
  7405. RAUWUpdateListener Listener(*this, UI, UE);
  7406. while (UI != UE) {
  7407. SDNode *User = *UI;
  7408. // This node is about to morph, remove its old self from the CSE maps.
  7409. RemoveNodeFromCSEMaps(User);
  7410. // A user can appear in a use list multiple times, and when this happens the
  7411. // uses are usually next to each other in the list. To help reduce the
  7412. // number of CSE and divergence recomputations, process all the uses of this
  7413. // user that we can find this way.
  7414. bool To_IsDivergent = false;
  7415. do {
  7416. SDUse &Use = UI.getUse();
  7417. const SDValue &ToOp = To[Use.getResNo()];
  7418. ++UI;
  7419. Use.set(ToOp);
  7420. To_IsDivergent |= ToOp->isDivergent();
  7421. } while (UI != UE && *UI == User);
  7422. if (To_IsDivergent != From->isDivergent())
  7423. updateDivergence(User);
  7424. // Now that we have modified User, add it back to the CSE maps. If it
  7425. // already exists there, recursively merge the results together.
  7426. AddModifiedNodeToCSEMaps(User);
  7427. }
  7428. // If we just RAUW'd the root, take note.
  7429. if (From == getRoot().getNode())
  7430. setRoot(SDValue(To[getRoot().getResNo()]));
  7431. }
  7432. /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
  7433. /// uses of other values produced by From.getNode() alone. The Deleted
  7434. /// vector is handled the same way as for ReplaceAllUsesWith.
  7435. void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
  7436. // Handle the really simple, really trivial case efficiently.
  7437. if (From == To) return;
  7438. // Handle the simple, trivial, case efficiently.
  7439. if (From.getNode()->getNumValues() == 1) {
  7440. ReplaceAllUsesWith(From, To);
  7441. return;
  7442. }
  7443. // Preserve Debug Info.
  7444. transferDbgValues(From, To);
  7445. // Iterate over just the existing users of From. See the comments in
  7446. // the ReplaceAllUsesWith above.
  7447. SDNode::use_iterator UI = From.getNode()->use_begin(),
  7448. UE = From.getNode()->use_end();
  7449. RAUWUpdateListener Listener(*this, UI, UE);
  7450. while (UI != UE) {
  7451. SDNode *User = *UI;
  7452. bool UserRemovedFromCSEMaps = false;
  7453. // A user can appear in a use list multiple times, and when this
  7454. // happens the uses are usually next to each other in the list.
  7455. // To help reduce the number of CSE recomputations, process all
  7456. // the uses of this user that we can find this way.
  7457. do {
  7458. SDUse &Use = UI.getUse();
  7459. // Skip uses of different values from the same node.
  7460. if (Use.getResNo() != From.getResNo()) {
  7461. ++UI;
  7462. continue;
  7463. }
  7464. // If this node hasn't been modified yet, it's still in the CSE maps,
  7465. // so remove its old self from the CSE maps.
  7466. if (!UserRemovedFromCSEMaps) {
  7467. RemoveNodeFromCSEMaps(User);
  7468. UserRemovedFromCSEMaps = true;
  7469. }
  7470. ++UI;
  7471. Use.set(To);
  7472. if (To->isDivergent() != From->isDivergent())
  7473. updateDivergence(User);
  7474. } while (UI != UE && *UI == User);
  7475. // We are iterating over all uses of the From node, so if a use
  7476. // doesn't use the specific value, no changes are made.
  7477. if (!UserRemovedFromCSEMaps)
  7478. continue;
  7479. // Now that we have modified User, add it back to the CSE maps. If it
  7480. // already exists there, recursively merge the results together.
  7481. AddModifiedNodeToCSEMaps(User);
  7482. }
  7483. // If we just RAUW'd the root, take note.
  7484. if (From == getRoot())
  7485. setRoot(To);
  7486. }
  7487. namespace {
  7488. /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
  7489. /// to record information about a use.
  7490. struct UseMemo {
  7491. SDNode *User;
  7492. unsigned Index;
  7493. SDUse *Use;
  7494. };
  7495. /// operator< - Sort Memos by User.
  7496. bool operator<(const UseMemo &L, const UseMemo &R) {
  7497. return (intptr_t)L.User < (intptr_t)R.User;
  7498. }
  7499. } // end anonymous namespace
  7500. void SelectionDAG::updateDivergence(SDNode * N)
  7501. {
  7502. if (TLI->isSDNodeAlwaysUniform(N))
  7503. return;
  7504. bool IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, DA);
  7505. for (auto &Op : N->ops()) {
  7506. if (Op.Val.getValueType() != MVT::Other)
  7507. IsDivergent |= Op.getNode()->isDivergent();
  7508. }
  7509. if (N->SDNodeBits.IsDivergent != IsDivergent) {
  7510. N->SDNodeBits.IsDivergent = IsDivergent;
  7511. for (auto U : N->uses()) {
  7512. updateDivergence(U);
  7513. }
  7514. }
  7515. }
  7516. void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
  7517. DenseMap<SDNode *, unsigned> Degree;
  7518. Order.reserve(AllNodes.size());
  7519. for (auto &N : allnodes()) {
  7520. unsigned NOps = N.getNumOperands();
  7521. Degree[&N] = NOps;
  7522. if (0 == NOps)
  7523. Order.push_back(&N);
  7524. }
  7525. for (size_t I = 0; I != Order.size(); ++I) {
  7526. SDNode *N = Order[I];
  7527. for (auto U : N->uses()) {
  7528. unsigned &UnsortedOps = Degree[U];
  7529. if (0 == --UnsortedOps)
  7530. Order.push_back(U);
  7531. }
  7532. }
  7533. }
  7534. #ifndef NDEBUG
  7535. void SelectionDAG::VerifyDAGDiverence() {
  7536. std::vector<SDNode *> TopoOrder;
  7537. CreateTopologicalOrder(TopoOrder);
  7538. const TargetLowering &TLI = getTargetLoweringInfo();
  7539. DenseMap<const SDNode *, bool> DivergenceMap;
  7540. for (auto &N : allnodes()) {
  7541. DivergenceMap[&N] = false;
  7542. }
  7543. for (auto N : TopoOrder) {
  7544. bool IsDivergent = DivergenceMap[N];
  7545. bool IsSDNodeDivergent = TLI.isSDNodeSourceOfDivergence(N, FLI, DA);
  7546. for (auto &Op : N->ops()) {
  7547. if (Op.Val.getValueType() != MVT::Other)
  7548. IsSDNodeDivergent |= DivergenceMap[Op.getNode()];
  7549. }
  7550. if (!IsDivergent && IsSDNodeDivergent && !TLI.isSDNodeAlwaysUniform(N)) {
  7551. DivergenceMap[N] = true;
  7552. }
  7553. }
  7554. for (auto &N : allnodes()) {
  7555. (void)N;
  7556. assert(DivergenceMap[&N] == N.isDivergent() &&
  7557. "Divergence bit inconsistency detected\n");
  7558. }
  7559. }
  7560. #endif
  7561. /// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
  7562. /// uses of other values produced by From.getNode() alone. The same value
  7563. /// may appear in both the From and To list. The Deleted vector is
  7564. /// handled the same way as for ReplaceAllUsesWith.
  7565. void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
  7566. const SDValue *To,
  7567. unsigned Num){
  7568. // Handle the simple, trivial case efficiently.
  7569. if (Num == 1)
  7570. return ReplaceAllUsesOfValueWith(*From, *To);
  7571. transferDbgValues(*From, *To);
  7572. // Read up all the uses and make records of them. This helps
  7573. // processing new uses that are introduced during the
  7574. // replacement process.
  7575. SmallVector<UseMemo, 4> Uses;
  7576. for (unsigned i = 0; i != Num; ++i) {
  7577. unsigned FromResNo = From[i].getResNo();
  7578. SDNode *FromNode = From[i].getNode();
  7579. for (SDNode::use_iterator UI = FromNode->use_begin(),
  7580. E = FromNode->use_end(); UI != E; ++UI) {
  7581. SDUse &Use = UI.getUse();
  7582. if (Use.getResNo() == FromResNo) {
  7583. UseMemo Memo = { *UI, i, &Use };
  7584. Uses.push_back(Memo);
  7585. }
  7586. }
  7587. }
  7588. // Sort the uses, so that all the uses from a given User are together.
  7589. llvm::sort(Uses);
  7590. for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
  7591. UseIndex != UseIndexEnd; ) {
  7592. // We know that this user uses some value of From. If it is the right
  7593. // value, update it.
  7594. SDNode *User = Uses[UseIndex].User;
  7595. // This node is about to morph, remove its old self from the CSE maps.
  7596. RemoveNodeFromCSEMaps(User);
  7597. // The Uses array is sorted, so all the uses for a given User
  7598. // are next to each other in the list.
  7599. // To help reduce the number of CSE recomputations, process all
  7600. // the uses of this user that we can find this way.
  7601. do {
  7602. unsigned i = Uses[UseIndex].Index;
  7603. SDUse &Use = *Uses[UseIndex].Use;
  7604. ++UseIndex;
  7605. Use.set(To[i]);
  7606. } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
  7607. // Now that we have modified User, add it back to the CSE maps. If it
  7608. // already exists there, recursively merge the results together.
  7609. AddModifiedNodeToCSEMaps(User);
  7610. }
  7611. }
  7612. /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
  7613. /// based on their topological order. It returns the maximum id and a vector
  7614. /// of the SDNodes* in assigned order by reference.
  7615. unsigned SelectionDAG::AssignTopologicalOrder() {
  7616. unsigned DAGSize = 0;
  7617. // SortedPos tracks the progress of the algorithm. Nodes before it are
  7618. // sorted, nodes after it are unsorted. When the algorithm completes
  7619. // it is at the end of the list.
  7620. allnodes_iterator SortedPos = allnodes_begin();
  7621. // Visit all the nodes. Move nodes with no operands to the front of
  7622. // the list immediately. Annotate nodes that do have operands with their
  7623. // operand count. Before we do this, the Node Id fields of the nodes
  7624. // may contain arbitrary values. After, the Node Id fields for nodes
  7625. // before SortedPos will contain the topological sort index, and the
  7626. // Node Id fields for nodes At SortedPos and after will contain the
  7627. // count of outstanding operands.
  7628. for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
  7629. SDNode *N = &*I++;
  7630. checkForCycles(N, this);
  7631. unsigned Degree = N->getNumOperands();
  7632. if (Degree == 0) {
  7633. // A node with no uses, add it to the result array immediately.
  7634. N->setNodeId(DAGSize++);
  7635. allnodes_iterator Q(N);
  7636. if (Q != SortedPos)
  7637. SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
  7638. assert(SortedPos != AllNodes.end() && "Overran node list");
  7639. ++SortedPos;
  7640. } else {
  7641. // Temporarily use the Node Id as scratch space for the degree count.
  7642. N->setNodeId(Degree);
  7643. }
  7644. }
  7645. // Visit all the nodes. As we iterate, move nodes into sorted order,
  7646. // such that by the time the end is reached all nodes will be sorted.
  7647. for (SDNode &Node : allnodes()) {
  7648. SDNode *N = &Node;
  7649. checkForCycles(N, this);
  7650. // N is in sorted position, so all its uses have one less operand
  7651. // that needs to be sorted.
  7652. for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
  7653. UI != UE; ++UI) {
  7654. SDNode *P = *UI;
  7655. unsigned Degree = P->getNodeId();
  7656. assert(Degree != 0 && "Invalid node degree");
  7657. --Degree;
  7658. if (Degree == 0) {
  7659. // All of P's operands are sorted, so P may sorted now.
  7660. P->setNodeId(DAGSize++);
  7661. if (P->getIterator() != SortedPos)
  7662. SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
  7663. assert(SortedPos != AllNodes.end() && "Overran node list");
  7664. ++SortedPos;
  7665. } else {
  7666. // Update P's outstanding operand count.
  7667. P->setNodeId(Degree);
  7668. }
  7669. }
  7670. if (Node.getIterator() == SortedPos) {
  7671. #ifndef NDEBUG
  7672. allnodes_iterator I(N);
  7673. SDNode *S = &*++I;
  7674. dbgs() << "Overran sorted position:\n";
  7675. S->dumprFull(this); dbgs() << "\n";
  7676. dbgs() << "Checking if this is due to cycles\n";
  7677. checkForCycles(this, true);
  7678. #endif
  7679. llvm_unreachable(nullptr);
  7680. }
  7681. }
  7682. assert(SortedPos == AllNodes.end() &&
  7683. "Topological sort incomplete!");
  7684. assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
  7685. "First node in topological sort is not the entry token!");
  7686. assert(AllNodes.front().getNodeId() == 0 &&
  7687. "First node in topological sort has non-zero id!");
  7688. assert(AllNodes.front().getNumOperands() == 0 &&
  7689. "First node in topological sort has operands!");
  7690. assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
  7691. "Last node in topologic sort has unexpected id!");
  7692. assert(AllNodes.back().use_empty() &&
  7693. "Last node in topologic sort has users!");
  7694. assert(DAGSize == allnodes_size() && "Node count mismatch!");
  7695. return DAGSize;
  7696. }
  7697. /// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
  7698. /// value is produced by SD.
  7699. void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
  7700. if (SD) {
  7701. assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
  7702. SD->setHasDebugValue(true);
  7703. }
  7704. DbgInfo->add(DB, SD, isParameter);
  7705. }
  7706. void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) {
  7707. DbgInfo->add(DB);
  7708. }
  7709. SDValue SelectionDAG::makeEquivalentMemoryOrdering(LoadSDNode *OldLoad,
  7710. SDValue NewMemOp) {
  7711. assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
  7712. // The new memory operation must have the same position as the old load in
  7713. // terms of memory dependency. Create a TokenFactor for the old load and new
  7714. // memory operation and update uses of the old load's output chain to use that
  7715. // TokenFactor.
  7716. SDValue OldChain = SDValue(OldLoad, 1);
  7717. SDValue NewChain = SDValue(NewMemOp.getNode(), 1);
  7718. if (OldChain == NewChain || !OldLoad->hasAnyUseOfValue(1))
  7719. return NewChain;
  7720. SDValue TokenFactor =
  7721. getNode(ISD::TokenFactor, SDLoc(OldLoad), MVT::Other, OldChain, NewChain);
  7722. ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
  7723. UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewChain);
  7724. return TokenFactor;
  7725. }
  7726. SDValue SelectionDAG::getSymbolFunctionGlobalAddress(SDValue Op,
  7727. Function **OutFunction) {
  7728. assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
  7729. auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
  7730. auto *Module = MF->getFunction().getParent();
  7731. auto *Function = Module->getFunction(Symbol);
  7732. if (OutFunction != nullptr)
  7733. *OutFunction = Function;
  7734. if (Function != nullptr) {
  7735. auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
  7736. return getGlobalAddress(Function, SDLoc(Op), PtrTy);
  7737. }
  7738. std::string ErrorStr;
  7739. raw_string_ostream ErrorFormatter(ErrorStr);
  7740. ErrorFormatter << "Undefined external symbol ";
  7741. ErrorFormatter << '"' << Symbol << '"';
  7742. ErrorFormatter.flush();
  7743. report_fatal_error(ErrorStr);
  7744. }
  7745. //===----------------------------------------------------------------------===//
  7746. // SDNode Class
  7747. //===----------------------------------------------------------------------===//
  7748. bool llvm::isNullConstant(SDValue V) {
  7749. ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
  7750. return Const != nullptr && Const->isNullValue();
  7751. }
  7752. bool llvm::isNullFPConstant(SDValue V) {
  7753. ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
  7754. return Const != nullptr && Const->isZero() && !Const->isNegative();
  7755. }
  7756. bool llvm::isAllOnesConstant(SDValue V) {
  7757. ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
  7758. return Const != nullptr && Const->isAllOnesValue();
  7759. }
  7760. bool llvm::isOneConstant(SDValue V) {
  7761. ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
  7762. return Const != nullptr && Const->isOne();
  7763. }
  7764. SDValue llvm::peekThroughBitcasts(SDValue V) {
  7765. while (V.getOpcode() == ISD::BITCAST)
  7766. V = V.getOperand(0);
  7767. return V;
  7768. }
  7769. SDValue llvm::peekThroughOneUseBitcasts(SDValue V) {
  7770. while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
  7771. V = V.getOperand(0);
  7772. return V;
  7773. }
  7774. SDValue llvm::peekThroughExtractSubvectors(SDValue V) {
  7775. while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
  7776. V = V.getOperand(0);
  7777. return V;
  7778. }
  7779. bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
  7780. if (V.getOpcode() != ISD::XOR)
  7781. return false;
  7782. V = peekThroughBitcasts(V.getOperand(1));
  7783. unsigned NumBits = V.getScalarValueSizeInBits();
  7784. ConstantSDNode *C =
  7785. isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
  7786. return C && (C->getAPIntValue().countTrailingOnes() >= NumBits);
  7787. }
  7788. ConstantSDNode *llvm::isConstOrConstSplat(SDValue N, bool AllowUndefs,
  7789. bool AllowTruncation) {
  7790. if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
  7791. return CN;
  7792. if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
  7793. BitVector UndefElements;
  7794. ConstantSDNode *CN = BV->getConstantSplatNode(&UndefElements);
  7795. // BuildVectors can truncate their operands. Ignore that case here unless
  7796. // AllowTruncation is set.
  7797. if (CN && (UndefElements.none() || AllowUndefs)) {
  7798. EVT CVT = CN->getValueType(0);
  7799. EVT NSVT = N.getValueType().getScalarType();
  7800. assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
  7801. if (AllowTruncation || (CVT == NSVT))
  7802. return CN;
  7803. }
  7804. }
  7805. return nullptr;
  7806. }
  7807. ConstantSDNode *llvm::isConstOrConstSplat(SDValue N, const APInt &DemandedElts,
  7808. bool AllowUndefs,
  7809. bool AllowTruncation) {
  7810. if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
  7811. return CN;
  7812. if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
  7813. BitVector UndefElements;
  7814. ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
  7815. // BuildVectors can truncate their operands. Ignore that case here unless
  7816. // AllowTruncation is set.
  7817. if (CN && (UndefElements.none() || AllowUndefs)) {
  7818. EVT CVT = CN->getValueType(0);
  7819. EVT NSVT = N.getValueType().getScalarType();
  7820. assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
  7821. if (AllowTruncation || (CVT == NSVT))
  7822. return CN;
  7823. }
  7824. }
  7825. return nullptr;
  7826. }
  7827. ConstantFPSDNode *llvm::isConstOrConstSplatFP(SDValue N, bool AllowUndefs) {
  7828. if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
  7829. return CN;
  7830. if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
  7831. BitVector UndefElements;
  7832. ConstantFPSDNode *CN = BV->getConstantFPSplatNode(&UndefElements);
  7833. if (CN && (UndefElements.none() || AllowUndefs))
  7834. return CN;
  7835. }
  7836. return nullptr;
  7837. }
  7838. ConstantFPSDNode *llvm::isConstOrConstSplatFP(SDValue N,
  7839. const APInt &DemandedElts,
  7840. bool AllowUndefs) {
  7841. if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
  7842. return CN;
  7843. if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
  7844. BitVector UndefElements;
  7845. ConstantFPSDNode *CN =
  7846. BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
  7847. if (CN && (UndefElements.none() || AllowUndefs))
  7848. return CN;
  7849. }
  7850. return nullptr;
  7851. }
  7852. bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
  7853. // TODO: may want to use peekThroughBitcast() here.
  7854. ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
  7855. return C && C->isNullValue();
  7856. }
  7857. bool llvm::isOneOrOneSplat(SDValue N) {
  7858. // TODO: may want to use peekThroughBitcast() here.
  7859. unsigned BitWidth = N.getScalarValueSizeInBits();
  7860. ConstantSDNode *C = isConstOrConstSplat(N);
  7861. return C && C->isOne() && C->getValueSizeInBits(0) == BitWidth;
  7862. }
  7863. bool llvm::isAllOnesOrAllOnesSplat(SDValue N) {
  7864. N = peekThroughBitcasts(N);
  7865. unsigned BitWidth = N.getScalarValueSizeInBits();
  7866. ConstantSDNode *C = isConstOrConstSplat(N);
  7867. return C && C->isAllOnesValue() && C->getValueSizeInBits(0) == BitWidth;
  7868. }
  7869. HandleSDNode::~HandleSDNode() {
  7870. DropOperands();
  7871. }
  7872. GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
  7873. const DebugLoc &DL,
  7874. const GlobalValue *GA, EVT VT,
  7875. int64_t o, unsigned TF)
  7876. : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
  7877. TheGlobal = GA;
  7878. }
  7879. AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, const DebugLoc &dl,
  7880. EVT VT, unsigned SrcAS,
  7881. unsigned DestAS)
  7882. : SDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT)),
  7883. SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
  7884. MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
  7885. SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
  7886. : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
  7887. MemSDNodeBits.IsVolatile = MMO->isVolatile();
  7888. MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
  7889. MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
  7890. MemSDNodeBits.IsInvariant = MMO->isInvariant();
  7891. // We check here that the size of the memory operand fits within the size of
  7892. // the MMO. This is because the MMO might indicate only a possible address
  7893. // range instead of specifying the affected memory addresses precisely.
  7894. assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
  7895. }
  7896. /// Profile - Gather unique data for the node.
  7897. ///
  7898. void SDNode::Profile(FoldingSetNodeID &ID) const {
  7899. AddNodeIDNode(ID, this);
  7900. }
  7901. namespace {
  7902. struct EVTArray {
  7903. std::vector<EVT> VTs;
  7904. EVTArray() {
  7905. VTs.reserve(MVT::LAST_VALUETYPE);
  7906. for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
  7907. VTs.push_back(MVT((MVT::SimpleValueType)i));
  7908. }
  7909. };
  7910. } // end anonymous namespace
  7911. static ManagedStatic<std::set<EVT, EVT::compareRawBits>> EVTs;
  7912. static ManagedStatic<EVTArray> SimpleVTArray;
  7913. static ManagedStatic<sys::SmartMutex<true>> VTMutex;
  7914. /// getValueTypeList - Return a pointer to the specified value type.
  7915. ///
  7916. const EVT *SDNode::getValueTypeList(EVT VT) {
  7917. if (VT.isExtended()) {
  7918. sys::SmartScopedLock<true> Lock(*VTMutex);
  7919. return &(*EVTs->insert(VT).first);
  7920. } else {
  7921. assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
  7922. "Value type out of range!");
  7923. return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
  7924. }
  7925. }
  7926. /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
  7927. /// indicated value. This method ignores uses of other values defined by this
  7928. /// operation.
  7929. bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
  7930. assert(Value < getNumValues() && "Bad value!");
  7931. // TODO: Only iterate over uses of a given value of the node
  7932. for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
  7933. if (UI.getUse().getResNo() == Value) {
  7934. if (NUses == 0)
  7935. return false;
  7936. --NUses;
  7937. }
  7938. }
  7939. // Found exactly the right number of uses?
  7940. return NUses == 0;
  7941. }
  7942. /// hasAnyUseOfValue - Return true if there are any use of the indicated
  7943. /// value. This method ignores uses of other values defined by this operation.
  7944. bool SDNode::hasAnyUseOfValue(unsigned Value) const {
  7945. assert(Value < getNumValues() && "Bad value!");
  7946. for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
  7947. if (UI.getUse().getResNo() == Value)
  7948. return true;
  7949. return false;
  7950. }
  7951. /// isOnlyUserOf - Return true if this node is the only use of N.
  7952. bool SDNode::isOnlyUserOf(const SDNode *N) const {
  7953. bool Seen = false;
  7954. for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
  7955. SDNode *User = *I;
  7956. if (User == this)
  7957. Seen = true;
  7958. else
  7959. return false;
  7960. }
  7961. return Seen;
  7962. }
  7963. /// Return true if the only users of N are contained in Nodes.
  7964. bool SDNode::areOnlyUsersOf(ArrayRef<const SDNode *> Nodes, const SDNode *N) {
  7965. bool Seen = false;
  7966. for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
  7967. SDNode *User = *I;
  7968. if (llvm::any_of(Nodes,
  7969. [&User](const SDNode *Node) { return User == Node; }))
  7970. Seen = true;
  7971. else
  7972. return false;
  7973. }
  7974. return Seen;
  7975. }
  7976. /// isOperand - Return true if this node is an operand of N.
  7977. bool SDValue::isOperandOf(const SDNode *N) const {
  7978. return any_of(N->op_values(), [this](SDValue Op) { return *this == Op; });
  7979. }
  7980. bool SDNode::isOperandOf(const SDNode *N) const {
  7981. return any_of(N->op_values(),
  7982. [this](SDValue Op) { return this == Op.getNode(); });
  7983. }
  7984. /// reachesChainWithoutSideEffects - Return true if this operand (which must
  7985. /// be a chain) reaches the specified operand without crossing any
  7986. /// side-effecting instructions on any chain path. In practice, this looks
  7987. /// through token factors and non-volatile loads. In order to remain efficient,
  7988. /// this only looks a couple of nodes in, it does not do an exhaustive search.
  7989. ///
  7990. /// Note that we only need to examine chains when we're searching for
  7991. /// side-effects; SelectionDAG requires that all side-effects are represented
  7992. /// by chains, even if another operand would force a specific ordering. This
  7993. /// constraint is necessary to allow transformations like splitting loads.
  7994. bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
  7995. unsigned Depth) const {
  7996. if (*this == Dest) return true;
  7997. // Don't search too deeply, we just want to be able to see through
  7998. // TokenFactor's etc.
  7999. if (Depth == 0) return false;
  8000. // If this is a token factor, all inputs to the TF happen in parallel.
  8001. if (getOpcode() == ISD::TokenFactor) {
  8002. // First, try a shallow search.
  8003. if (is_contained((*this)->ops(), Dest)) {
  8004. // We found the chain we want as an operand of this TokenFactor.
  8005. // Essentially, we reach the chain without side-effects if we could
  8006. // serialize the TokenFactor into a simple chain of operations with
  8007. // Dest as the last operation. This is automatically true if the
  8008. // chain has one use: there are no other ordering constraints.
  8009. // If the chain has more than one use, we give up: some other
  8010. // use of Dest might force a side-effect between Dest and the current
  8011. // node.
  8012. if (Dest.hasOneUse())
  8013. return true;
  8014. }
  8015. // Next, try a deep search: check whether every operand of the TokenFactor
  8016. // reaches Dest.
  8017. return llvm::all_of((*this)->ops(), [=](SDValue Op) {
  8018. return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
  8019. });
  8020. }
  8021. // Loads don't have side effects, look through them.
  8022. if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
  8023. if (Ld->isUnordered())
  8024. return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
  8025. }
  8026. return false;
  8027. }
  8028. bool SDNode::hasPredecessor(const SDNode *N) const {
  8029. SmallPtrSet<const SDNode *, 32> Visited;
  8030. SmallVector<const SDNode *, 16> Worklist;
  8031. Worklist.push_back(this);
  8032. return hasPredecessorHelper(N, Visited, Worklist);
  8033. }
  8034. void SDNode::intersectFlagsWith(const SDNodeFlags Flags) {
  8035. this->Flags.intersectWith(Flags);
  8036. }
  8037. SDValue
  8038. SelectionDAG::matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp,
  8039. ArrayRef<ISD::NodeType> CandidateBinOps,
  8040. bool AllowPartials) {
  8041. // The pattern must end in an extract from index 0.
  8042. if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
  8043. !isNullConstant(Extract->getOperand(1)))
  8044. return SDValue();
  8045. // Match against one of the candidate binary ops.
  8046. SDValue Op = Extract->getOperand(0);
  8047. if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
  8048. return Op.getOpcode() == unsigned(BinOp);
  8049. }))
  8050. return SDValue();
  8051. // Floating-point reductions may require relaxed constraints on the final step
  8052. // of the reduction because they may reorder intermediate operations.
  8053. unsigned CandidateBinOp = Op.getOpcode();
  8054. if (Op.getValueType().isFloatingPoint()) {
  8055. SDNodeFlags Flags = Op->getFlags();
  8056. switch (CandidateBinOp) {
  8057. case ISD::FADD:
  8058. if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
  8059. return SDValue();
  8060. break;
  8061. default:
  8062. llvm_unreachable("Unhandled FP opcode for binop reduction");
  8063. }
  8064. }
  8065. // Matching failed - attempt to see if we did enough stages that a partial
  8066. // reduction from a subvector is possible.
  8067. auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
  8068. if (!AllowPartials || !Op)
  8069. return SDValue();
  8070. EVT OpVT = Op.getValueType();
  8071. EVT OpSVT = OpVT.getScalarType();
  8072. EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
  8073. if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
  8074. return SDValue();
  8075. BinOp = (ISD::NodeType)CandidateBinOp;
  8076. return getNode(
  8077. ISD::EXTRACT_SUBVECTOR, SDLoc(Op), SubVT, Op,
  8078. getConstant(0, SDLoc(Op), TLI->getVectorIdxTy(getDataLayout())));
  8079. };
  8080. // At each stage, we're looking for something that looks like:
  8081. // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
  8082. // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
  8083. // i32 undef, i32 undef, i32 undef, i32 undef>
  8084. // %a = binop <8 x i32> %op, %s
  8085. // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
  8086. // we expect something like:
  8087. // <4,5,6,7,u,u,u,u>
  8088. // <2,3,u,u,u,u,u,u>
  8089. // <1,u,u,u,u,u,u,u>
  8090. // While a partial reduction match would be:
  8091. // <2,3,u,u,u,u,u,u>
  8092. // <1,u,u,u,u,u,u,u>
  8093. unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
  8094. SDValue PrevOp;
  8095. for (unsigned i = 0; i < Stages; ++i) {
  8096. unsigned MaskEnd = (1 << i);
  8097. if (Op.getOpcode() != CandidateBinOp)
  8098. return PartialReduction(PrevOp, MaskEnd);
  8099. SDValue Op0 = Op.getOperand(0);
  8100. SDValue Op1 = Op.getOperand(1);
  8101. ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
  8102. if (Shuffle) {
  8103. Op = Op1;
  8104. } else {
  8105. Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
  8106. Op = Op0;
  8107. }
  8108. // The first operand of the shuffle should be the same as the other operand
  8109. // of the binop.
  8110. if (!Shuffle || Shuffle->getOperand(0) != Op)
  8111. return PartialReduction(PrevOp, MaskEnd);
  8112. // Verify the shuffle has the expected (at this stage of the pyramid) mask.
  8113. for (int Index = 0; Index < (int)MaskEnd; ++Index)
  8114. if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
  8115. return PartialReduction(PrevOp, MaskEnd);
  8116. PrevOp = Op;
  8117. }
  8118. BinOp = (ISD::NodeType)CandidateBinOp;
  8119. return Op;
  8120. }
  8121. SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
  8122. assert(N->getNumValues() == 1 &&
  8123. "Can't unroll a vector with multiple results!");
  8124. EVT VT = N->getValueType(0);
  8125. unsigned NE = VT.getVectorNumElements();
  8126. EVT EltVT = VT.getVectorElementType();
  8127. SDLoc dl(N);
  8128. SmallVector<SDValue, 8> Scalars;
  8129. SmallVector<SDValue, 4> Operands(N->getNumOperands());
  8130. // If ResNE is 0, fully unroll the vector op.
  8131. if (ResNE == 0)
  8132. ResNE = NE;
  8133. else if (NE > ResNE)
  8134. NE = ResNE;
  8135. unsigned i;
  8136. for (i= 0; i != NE; ++i) {
  8137. for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
  8138. SDValue Operand = N->getOperand(j);
  8139. EVT OperandVT = Operand.getValueType();
  8140. if (OperandVT.isVector()) {
  8141. // A vector operand; extract a single element.
  8142. EVT OperandEltVT = OperandVT.getVectorElementType();
  8143. Operands[j] =
  8144. getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT, Operand,
  8145. getConstant(i, dl, TLI->getVectorIdxTy(getDataLayout())));
  8146. } else {
  8147. // A scalar operand; just use it as is.
  8148. Operands[j] = Operand;
  8149. }
  8150. }
  8151. switch (N->getOpcode()) {
  8152. default: {
  8153. Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
  8154. N->getFlags()));
  8155. break;
  8156. }
  8157. case ISD::VSELECT:
  8158. Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
  8159. break;
  8160. case ISD::SHL:
  8161. case ISD::SRA:
  8162. case ISD::SRL:
  8163. case ISD::ROTL:
  8164. case ISD::ROTR:
  8165. Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
  8166. getShiftAmountOperand(Operands[0].getValueType(),
  8167. Operands[1])));
  8168. break;
  8169. case ISD::SIGN_EXTEND_INREG: {
  8170. EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
  8171. Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
  8172. Operands[0],
  8173. getValueType(ExtVT)));
  8174. }
  8175. }
  8176. }
  8177. for (; i < ResNE; ++i)
  8178. Scalars.push_back(getUNDEF(EltVT));
  8179. EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
  8180. return getBuildVector(VecVT, dl, Scalars);
  8181. }
  8182. std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
  8183. SDNode *N, unsigned ResNE) {
  8184. unsigned Opcode = N->getOpcode();
  8185. assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
  8186. Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
  8187. Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
  8188. "Expected an overflow opcode");
  8189. EVT ResVT = N->getValueType(0);
  8190. EVT OvVT = N->getValueType(1);
  8191. EVT ResEltVT = ResVT.getVectorElementType();
  8192. EVT OvEltVT = OvVT.getVectorElementType();
  8193. SDLoc dl(N);
  8194. // If ResNE is 0, fully unroll the vector op.
  8195. unsigned NE = ResVT.getVectorNumElements();
  8196. if (ResNE == 0)
  8197. ResNE = NE;
  8198. else if (NE > ResNE)
  8199. NE = ResNE;
  8200. SmallVector<SDValue, 8> LHSScalars;
  8201. SmallVector<SDValue, 8> RHSScalars;
  8202. ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
  8203. ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
  8204. EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
  8205. SDVTList VTs = getVTList(ResEltVT, SVT);
  8206. SmallVector<SDValue, 8> ResScalars;
  8207. SmallVector<SDValue, 8> OvScalars;
  8208. for (unsigned i = 0; i < NE; ++i) {
  8209. SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
  8210. SDValue Ov =
  8211. getSelect(dl, OvEltVT, Res.getValue(1),
  8212. getBoolConstant(true, dl, OvEltVT, ResVT),
  8213. getConstant(0, dl, OvEltVT));
  8214. ResScalars.push_back(Res);
  8215. OvScalars.push_back(Ov);
  8216. }
  8217. ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
  8218. OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
  8219. EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
  8220. EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
  8221. return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
  8222. getBuildVector(NewOvVT, dl, OvScalars));
  8223. }
  8224. bool SelectionDAG::areNonVolatileConsecutiveLoads(LoadSDNode *LD,
  8225. LoadSDNode *Base,
  8226. unsigned Bytes,
  8227. int Dist) const {
  8228. if (LD->isVolatile() || Base->isVolatile())
  8229. return false;
  8230. // TODO: probably too restrictive for atomics, revisit
  8231. if (!LD->isSimple())
  8232. return false;
  8233. if (LD->isIndexed() || Base->isIndexed())
  8234. return false;
  8235. if (LD->getChain() != Base->getChain())
  8236. return false;
  8237. EVT VT = LD->getValueType(0);
  8238. if (VT.getSizeInBits() / 8 != Bytes)
  8239. return false;
  8240. auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
  8241. auto LocDecomp = BaseIndexOffset::match(LD, *this);
  8242. int64_t Offset = 0;
  8243. if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
  8244. return (Dist * Bytes == Offset);
  8245. return false;
  8246. }
  8247. /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
  8248. /// it cannot be inferred.
  8249. unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
  8250. // If this is a GlobalAddress + cst, return the alignment.
  8251. const GlobalValue *GV;
  8252. int64_t GVOffset = 0;
  8253. if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
  8254. unsigned IdxWidth = getDataLayout().getIndexTypeSizeInBits(GV->getType());
  8255. KnownBits Known(IdxWidth);
  8256. llvm::computeKnownBits(GV, Known, getDataLayout());
  8257. unsigned AlignBits = Known.countMinTrailingZeros();
  8258. unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
  8259. if (Align)
  8260. return MinAlign(Align, GVOffset);
  8261. }
  8262. // If this is a direct reference to a stack slot, use information about the
  8263. // stack slot's alignment.
  8264. int FrameIdx = INT_MIN;
  8265. int64_t FrameOffset = 0;
  8266. if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
  8267. FrameIdx = FI->getIndex();
  8268. } else if (isBaseWithConstantOffset(Ptr) &&
  8269. isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
  8270. // Handle FI+Cst
  8271. FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
  8272. FrameOffset = Ptr.getConstantOperandVal(1);
  8273. }
  8274. if (FrameIdx != INT_MIN) {
  8275. const MachineFrameInfo &MFI = getMachineFunction().getFrameInfo();
  8276. unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
  8277. FrameOffset);
  8278. return FIInfoAlign;
  8279. }
  8280. return 0;
  8281. }
  8282. /// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
  8283. /// which is split (or expanded) into two not necessarily identical pieces.
  8284. std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
  8285. // Currently all types are split in half.
  8286. EVT LoVT, HiVT;
  8287. if (!VT.isVector())
  8288. LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
  8289. else
  8290. LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
  8291. return std::make_pair(LoVT, HiVT);
  8292. }
  8293. /// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
  8294. /// low/high part.
  8295. std::pair<SDValue, SDValue>
  8296. SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
  8297. const EVT &HiVT) {
  8298. assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
  8299. N.getValueType().getVectorNumElements() &&
  8300. "More vector elements requested than available!");
  8301. SDValue Lo, Hi;
  8302. Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
  8303. getConstant(0, DL, TLI->getVectorIdxTy(getDataLayout())));
  8304. Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
  8305. getConstant(LoVT.getVectorNumElements(), DL,
  8306. TLI->getVectorIdxTy(getDataLayout())));
  8307. return std::make_pair(Lo, Hi);
  8308. }
  8309. /// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
  8310. SDValue SelectionDAG::WidenVector(const SDValue &N, const SDLoc &DL) {
  8311. EVT VT = N.getValueType();
  8312. EVT WideVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
  8313. NextPowerOf2(VT.getVectorNumElements()));
  8314. return getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, getUNDEF(WideVT), N,
  8315. getConstant(0, DL, TLI->getVectorIdxTy(getDataLayout())));
  8316. }
  8317. void SelectionDAG::ExtractVectorElements(SDValue Op,
  8318. SmallVectorImpl<SDValue> &Args,
  8319. unsigned Start, unsigned Count) {
  8320. EVT VT = Op.getValueType();
  8321. if (Count == 0)
  8322. Count = VT.getVectorNumElements();
  8323. EVT EltVT = VT.getVectorElementType();
  8324. EVT IdxTy = TLI->getVectorIdxTy(getDataLayout());
  8325. SDLoc SL(Op);
  8326. for (unsigned i = Start, e = Start + Count; i != e; ++i) {
  8327. Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
  8328. Op, getConstant(i, SL, IdxTy)));
  8329. }
  8330. }
  8331. // getAddressSpace - Return the address space this GlobalAddress belongs to.
  8332. unsigned GlobalAddressSDNode::getAddressSpace() const {
  8333. return getGlobal()->getType()->getAddressSpace();
  8334. }
  8335. Type *ConstantPoolSDNode::getType() const {
  8336. if (isMachineConstantPoolEntry())
  8337. return Val.MachineCPVal->getType();
  8338. return Val.ConstVal->getType();
  8339. }
  8340. bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
  8341. unsigned &SplatBitSize,
  8342. bool &HasAnyUndefs,
  8343. unsigned MinSplatBits,
  8344. bool IsBigEndian) const {
  8345. EVT VT = getValueType(0);
  8346. assert(VT.isVector() && "Expected a vector type");
  8347. unsigned VecWidth = VT.getSizeInBits();
  8348. if (MinSplatBits > VecWidth)
  8349. return false;
  8350. // FIXME: The widths are based on this node's type, but build vectors can
  8351. // truncate their operands.
  8352. SplatValue = APInt(VecWidth, 0);
  8353. SplatUndef = APInt(VecWidth, 0);
  8354. // Get the bits. Bits with undefined values (when the corresponding element
  8355. // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
  8356. // in SplatValue. If any of the values are not constant, give up and return
  8357. // false.
  8358. unsigned int NumOps = getNumOperands();
  8359. assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
  8360. unsigned EltWidth = VT.getScalarSizeInBits();
  8361. for (unsigned j = 0; j < NumOps; ++j) {
  8362. unsigned i = IsBigEndian ? NumOps - 1 - j : j;
  8363. SDValue OpVal = getOperand(i);
  8364. unsigned BitPos = j * EltWidth;
  8365. if (OpVal.isUndef())
  8366. SplatUndef.setBits(BitPos, BitPos + EltWidth);
  8367. else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
  8368. SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
  8369. else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
  8370. SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
  8371. else
  8372. return false;
  8373. }
  8374. // The build_vector is all constants or undefs. Find the smallest element
  8375. // size that splats the vector.
  8376. HasAnyUndefs = (SplatUndef != 0);
  8377. // FIXME: This does not work for vectors with elements less than 8 bits.
  8378. while (VecWidth > 8) {
  8379. unsigned HalfSize = VecWidth / 2;
  8380. APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
  8381. APInt LowValue = SplatValue.trunc(HalfSize);
  8382. APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
  8383. APInt LowUndef = SplatUndef.trunc(HalfSize);
  8384. // If the two halves do not match (ignoring undef bits), stop here.
  8385. if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
  8386. MinSplatBits > HalfSize)
  8387. break;
  8388. SplatValue = HighValue | LowValue;
  8389. SplatUndef = HighUndef & LowUndef;
  8390. VecWidth = HalfSize;
  8391. }
  8392. SplatBitSize = VecWidth;
  8393. return true;
  8394. }
  8395. SDValue BuildVectorSDNode::getSplatValue(const APInt &DemandedElts,
  8396. BitVector *UndefElements) const {
  8397. if (UndefElements) {
  8398. UndefElements->clear();
  8399. UndefElements->resize(getNumOperands());
  8400. }
  8401. assert(getNumOperands() == DemandedElts.getBitWidth() &&
  8402. "Unexpected vector size");
  8403. if (!DemandedElts)
  8404. return SDValue();
  8405. SDValue Splatted;
  8406. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  8407. if (!DemandedElts[i])
  8408. continue;
  8409. SDValue Op = getOperand(i);
  8410. if (Op.isUndef()) {
  8411. if (UndefElements)
  8412. (*UndefElements)[i] = true;
  8413. } else if (!Splatted) {
  8414. Splatted = Op;
  8415. } else if (Splatted != Op) {
  8416. return SDValue();
  8417. }
  8418. }
  8419. if (!Splatted) {
  8420. unsigned FirstDemandedIdx = DemandedElts.countTrailingZeros();
  8421. assert(getOperand(FirstDemandedIdx).isUndef() &&
  8422. "Can only have a splat without a constant for all undefs.");
  8423. return getOperand(FirstDemandedIdx);
  8424. }
  8425. return Splatted;
  8426. }
  8427. SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
  8428. APInt DemandedElts = APInt::getAllOnesValue(getNumOperands());
  8429. return getSplatValue(DemandedElts, UndefElements);
  8430. }
  8431. ConstantSDNode *
  8432. BuildVectorSDNode::getConstantSplatNode(const APInt &DemandedElts,
  8433. BitVector *UndefElements) const {
  8434. return dyn_cast_or_null<ConstantSDNode>(
  8435. getSplatValue(DemandedElts, UndefElements));
  8436. }
  8437. ConstantSDNode *
  8438. BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
  8439. return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
  8440. }
  8441. ConstantFPSDNode *
  8442. BuildVectorSDNode::getConstantFPSplatNode(const APInt &DemandedElts,
  8443. BitVector *UndefElements) const {
  8444. return dyn_cast_or_null<ConstantFPSDNode>(
  8445. getSplatValue(DemandedElts, UndefElements));
  8446. }
  8447. ConstantFPSDNode *
  8448. BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
  8449. return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
  8450. }
  8451. int32_t
  8452. BuildVectorSDNode::getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements,
  8453. uint32_t BitWidth) const {
  8454. if (ConstantFPSDNode *CN =
  8455. dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
  8456. bool IsExact;
  8457. APSInt IntVal(BitWidth);
  8458. const APFloat &APF = CN->getValueAPF();
  8459. if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
  8460. APFloat::opOK ||
  8461. !IsExact)
  8462. return -1;
  8463. return IntVal.exactLogBase2();
  8464. }
  8465. return -1;
  8466. }
  8467. bool BuildVectorSDNode::isConstant() const {
  8468. for (const SDValue &Op : op_values()) {
  8469. unsigned Opc = Op.getOpcode();
  8470. if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
  8471. return false;
  8472. }
  8473. return true;
  8474. }
  8475. bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
  8476. // Find the first non-undef value in the shuffle mask.
  8477. unsigned i, e;
  8478. for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
  8479. /* search */;
  8480. // If all elements are undefined, this shuffle can be considered a splat
  8481. // (although it should eventually get simplified away completely).
  8482. if (i == e)
  8483. return true;
  8484. // Make sure all remaining elements are either undef or the same as the first
  8485. // non-undef value.
  8486. for (int Idx = Mask[i]; i != e; ++i)
  8487. if (Mask[i] >= 0 && Mask[i] != Idx)
  8488. return false;
  8489. return true;
  8490. }
  8491. // Returns the SDNode if it is a constant integer BuildVector
  8492. // or constant integer.
  8493. SDNode *SelectionDAG::isConstantIntBuildVectorOrConstantInt(SDValue N) {
  8494. if (isa<ConstantSDNode>(N))
  8495. return N.getNode();
  8496. if (ISD::isBuildVectorOfConstantSDNodes(N.getNode()))
  8497. return N.getNode();
  8498. // Treat a GlobalAddress supporting constant offset folding as a
  8499. // constant integer.
  8500. if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N))
  8501. if (GA->getOpcode() == ISD::GlobalAddress &&
  8502. TLI->isOffsetFoldingLegal(GA))
  8503. return GA;
  8504. return nullptr;
  8505. }
  8506. SDNode *SelectionDAG::isConstantFPBuildVectorOrConstantFP(SDValue N) {
  8507. if (isa<ConstantFPSDNode>(N))
  8508. return N.getNode();
  8509. if (ISD::isBuildVectorOfConstantFPSDNodes(N.getNode()))
  8510. return N.getNode();
  8511. return nullptr;
  8512. }
  8513. void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
  8514. assert(!Node->OperandList && "Node already has operands");
  8515. assert(SDNode::getMaxNumOperands() >= Vals.size() &&
  8516. "too many operands to fit into SDNode");
  8517. SDUse *Ops = OperandRecycler.allocate(
  8518. ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
  8519. bool IsDivergent = false;
  8520. for (unsigned I = 0; I != Vals.size(); ++I) {
  8521. Ops[I].setUser(Node);
  8522. Ops[I].setInitial(Vals[I]);
  8523. if (Ops[I].Val.getValueType() != MVT::Other) // Skip Chain. It does not carry divergence.
  8524. IsDivergent = IsDivergent || Ops[I].getNode()->isDivergent();
  8525. }
  8526. Node->NumOperands = Vals.size();
  8527. Node->OperandList = Ops;
  8528. IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, DA);
  8529. if (!TLI->isSDNodeAlwaysUniform(Node))
  8530. Node->SDNodeBits.IsDivergent = IsDivergent;
  8531. checkForCycles(Node);
  8532. }
  8533. SDValue SelectionDAG::getTokenFactor(const SDLoc &DL,
  8534. SmallVectorImpl<SDValue> &Vals) {
  8535. size_t Limit = SDNode::getMaxNumOperands();
  8536. while (Vals.size() > Limit) {
  8537. unsigned SliceIdx = Vals.size() - Limit;
  8538. auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
  8539. SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
  8540. Vals.erase(Vals.begin() + SliceIdx, Vals.end());
  8541. Vals.emplace_back(NewTF);
  8542. }
  8543. return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
  8544. }
  8545. #ifndef NDEBUG
  8546. static void checkForCyclesHelper(const SDNode *N,
  8547. SmallPtrSetImpl<const SDNode*> &Visited,
  8548. SmallPtrSetImpl<const SDNode*> &Checked,
  8549. const llvm::SelectionDAG *DAG) {
  8550. // If this node has already been checked, don't check it again.
  8551. if (Checked.count(N))
  8552. return;
  8553. // If a node has already been visited on this depth-first walk, reject it as
  8554. // a cycle.
  8555. if (!Visited.insert(N).second) {
  8556. errs() << "Detected cycle in SelectionDAG\n";
  8557. dbgs() << "Offending node:\n";
  8558. N->dumprFull(DAG); dbgs() << "\n";
  8559. abort();
  8560. }
  8561. for (const SDValue &Op : N->op_values())
  8562. checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
  8563. Checked.insert(N);
  8564. Visited.erase(N);
  8565. }
  8566. #endif
  8567. void llvm::checkForCycles(const llvm::SDNode *N,
  8568. const llvm::SelectionDAG *DAG,
  8569. bool force) {
  8570. #ifndef NDEBUG
  8571. bool check = force;
  8572. #ifdef EXPENSIVE_CHECKS
  8573. check = true;
  8574. #endif // EXPENSIVE_CHECKS
  8575. if (check) {
  8576. assert(N && "Checking nonexistent SDNode");
  8577. SmallPtrSet<const SDNode*, 32> visited;
  8578. SmallPtrSet<const SDNode*, 32> checked;
  8579. checkForCyclesHelper(N, visited, checked, DAG);
  8580. }
  8581. #endif // !NDEBUG
  8582. }
  8583. void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
  8584. checkForCycles(DAG->getRoot().getNode(), DAG, force);
  8585. }