SelectionDAG.cpp 232 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470
  1. //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This implements the SelectionDAG class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/SelectionDAG.h"
  14. #include "SDNodeOrdering.h"
  15. #include "SDNodeDbgValue.h"
  16. #include "llvm/Constants.h"
  17. #include "llvm/Analysis/DebugInfo.h"
  18. #include "llvm/Analysis/ValueTracking.h"
  19. #include "llvm/Function.h"
  20. #include "llvm/GlobalAlias.h"
  21. #include "llvm/GlobalVariable.h"
  22. #include "llvm/Intrinsics.h"
  23. #include "llvm/DerivedTypes.h"
  24. #include "llvm/Assembly/Writer.h"
  25. #include "llvm/CallingConv.h"
  26. #include "llvm/CodeGen/MachineBasicBlock.h"
  27. #include "llvm/CodeGen/MachineConstantPool.h"
  28. #include "llvm/CodeGen/MachineFrameInfo.h"
  29. #include "llvm/CodeGen/MachineModuleInfo.h"
  30. #include "llvm/CodeGen/PseudoSourceValue.h"
  31. #include "llvm/Target/TargetRegisterInfo.h"
  32. #include "llvm/Target/TargetData.h"
  33. #include "llvm/Target/TargetFrameInfo.h"
  34. #include "llvm/Target/TargetLowering.h"
  35. #include "llvm/Target/TargetSelectionDAGInfo.h"
  36. #include "llvm/Target/TargetOptions.h"
  37. #include "llvm/Target/TargetInstrInfo.h"
  38. #include "llvm/Target/TargetIntrinsicInfo.h"
  39. #include "llvm/Target/TargetMachine.h"
  40. #include "llvm/Support/CommandLine.h"
  41. #include "llvm/Support/Debug.h"
  42. #include "llvm/Support/ErrorHandling.h"
  43. #include "llvm/Support/ManagedStatic.h"
  44. #include "llvm/Support/MathExtras.h"
  45. #include "llvm/Support/raw_ostream.h"
  46. #include "llvm/System/Mutex.h"
  47. #include "llvm/ADT/SetVector.h"
  48. #include "llvm/ADT/SmallPtrSet.h"
  49. #include "llvm/ADT/SmallSet.h"
  50. #include "llvm/ADT/SmallVector.h"
  51. #include "llvm/ADT/StringExtras.h"
  52. #include <algorithm>
  53. #include <cmath>
  54. using namespace llvm;
  55. /// makeVTList - Return an instance of the SDVTList struct initialized with the
  56. /// specified members.
  57. static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
  58. SDVTList Res = {VTs, NumVTs};
  59. return Res;
  60. }
  61. static const fltSemantics *EVTToAPFloatSemantics(EVT VT) {
  62. switch (VT.getSimpleVT().SimpleTy) {
  63. default: llvm_unreachable("Unknown FP format");
  64. case MVT::f32: return &APFloat::IEEEsingle;
  65. case MVT::f64: return &APFloat::IEEEdouble;
  66. case MVT::f80: return &APFloat::x87DoubleExtended;
  67. case MVT::f128: return &APFloat::IEEEquad;
  68. case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
  69. }
  70. }
  71. SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
  72. //===----------------------------------------------------------------------===//
  73. // ConstantFPSDNode Class
  74. //===----------------------------------------------------------------------===//
  75. /// isExactlyValue - We don't rely on operator== working on double values, as
  76. /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
  77. /// As such, this method can be used to do an exact bit-for-bit comparison of
  78. /// two floating point values.
  79. bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
  80. return getValueAPF().bitwiseIsEqual(V);
  81. }
  82. bool ConstantFPSDNode::isValueValidForType(EVT VT,
  83. const APFloat& Val) {
  84. assert(VT.isFloatingPoint() && "Can only convert between FP types");
  85. // PPC long double cannot be converted to any other type.
  86. if (VT == MVT::ppcf128 ||
  87. &Val.getSemantics() == &APFloat::PPCDoubleDouble)
  88. return false;
  89. // convert modifies in place, so make a copy.
  90. APFloat Val2 = APFloat(Val);
  91. bool losesInfo;
  92. (void) Val2.convert(*EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
  93. &losesInfo);
  94. return !losesInfo;
  95. }
  96. //===----------------------------------------------------------------------===//
  97. // ISD Namespace
  98. //===----------------------------------------------------------------------===//
  99. /// isBuildVectorAllOnes - Return true if the specified node is a
  100. /// BUILD_VECTOR where all of the elements are ~0 or undef.
  101. bool ISD::isBuildVectorAllOnes(const SDNode *N) {
  102. // Look through a bit convert.
  103. if (N->getOpcode() == ISD::BIT_CONVERT)
  104. N = N->getOperand(0).getNode();
  105. if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
  106. unsigned i = 0, e = N->getNumOperands();
  107. // Skip over all of the undef values.
  108. while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
  109. ++i;
  110. // Do not accept an all-undef vector.
  111. if (i == e) return false;
  112. // Do not accept build_vectors that aren't all constants or which have non-~0
  113. // elements.
  114. SDValue NotZero = N->getOperand(i);
  115. if (isa<ConstantSDNode>(NotZero)) {
  116. if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
  117. return false;
  118. } else if (isa<ConstantFPSDNode>(NotZero)) {
  119. if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
  120. bitcastToAPInt().isAllOnesValue())
  121. return false;
  122. } else
  123. return false;
  124. // Okay, we have at least one ~0 value, check to see if the rest match or are
  125. // undefs.
  126. for (++i; i != e; ++i)
  127. if (N->getOperand(i) != NotZero &&
  128. N->getOperand(i).getOpcode() != ISD::UNDEF)
  129. return false;
  130. return true;
  131. }
  132. /// isBuildVectorAllZeros - Return true if the specified node is a
  133. /// BUILD_VECTOR where all of the elements are 0 or undef.
  134. bool ISD::isBuildVectorAllZeros(const SDNode *N) {
  135. // Look through a bit convert.
  136. if (N->getOpcode() == ISD::BIT_CONVERT)
  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).getOpcode() == ISD::UNDEF)
  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.
  147. SDValue Zero = N->getOperand(i);
  148. if (isa<ConstantSDNode>(Zero)) {
  149. if (!cast<ConstantSDNode>(Zero)->isNullValue())
  150. return false;
  151. } else if (isa<ConstantFPSDNode>(Zero)) {
  152. if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
  153. return false;
  154. } else
  155. return false;
  156. // Okay, we have at least one 0 value, check to see if the rest match or are
  157. // undefs.
  158. for (++i; i != e; ++i)
  159. if (N->getOperand(i) != Zero &&
  160. N->getOperand(i).getOpcode() != ISD::UNDEF)
  161. return false;
  162. return true;
  163. }
  164. /// isScalarToVector - Return true if the specified node is a
  165. /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
  166. /// element is not an undef.
  167. bool ISD::isScalarToVector(const SDNode *N) {
  168. if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
  169. return true;
  170. if (N->getOpcode() != ISD::BUILD_VECTOR)
  171. return false;
  172. if (N->getOperand(0).getOpcode() == ISD::UNDEF)
  173. return false;
  174. unsigned NumElems = N->getNumOperands();
  175. for (unsigned i = 1; i < NumElems; ++i) {
  176. SDValue V = N->getOperand(i);
  177. if (V.getOpcode() != ISD::UNDEF)
  178. return false;
  179. }
  180. return true;
  181. }
  182. /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
  183. /// when given the operation for (X op Y).
  184. ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
  185. // To perform this operation, we just need to swap the L and G bits of the
  186. // operation.
  187. unsigned OldL = (Operation >> 2) & 1;
  188. unsigned OldG = (Operation >> 1) & 1;
  189. return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
  190. (OldL << 1) | // New G bit
  191. (OldG << 2)); // New L bit.
  192. }
  193. /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
  194. /// 'op' is a valid SetCC operation.
  195. ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
  196. unsigned Operation = Op;
  197. if (isInteger)
  198. Operation ^= 7; // Flip L, G, E bits, but not U.
  199. else
  200. Operation ^= 15; // Flip all of the condition bits.
  201. if (Operation > ISD::SETTRUE2)
  202. Operation &= ~8; // Don't let N and U bits get set.
  203. return ISD::CondCode(Operation);
  204. }
  205. /// isSignedOp - For an integer comparison, return 1 if the comparison is a
  206. /// signed operation and 2 if the result is an unsigned comparison. Return zero
  207. /// if the operation does not depend on the sign of the input (setne and seteq).
  208. static int isSignedOp(ISD::CondCode Opcode) {
  209. switch (Opcode) {
  210. default: llvm_unreachable("Illegal integer setcc operation!");
  211. case ISD::SETEQ:
  212. case ISD::SETNE: return 0;
  213. case ISD::SETLT:
  214. case ISD::SETLE:
  215. case ISD::SETGT:
  216. case ISD::SETGE: return 1;
  217. case ISD::SETULT:
  218. case ISD::SETULE:
  219. case ISD::SETUGT:
  220. case ISD::SETUGE: return 2;
  221. }
  222. }
  223. /// getSetCCOrOperation - Return the result of a logical OR between different
  224. /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
  225. /// returns SETCC_INVALID if it is not possible to represent the resultant
  226. /// comparison.
  227. ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
  228. bool isInteger) {
  229. if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
  230. // Cannot fold a signed integer setcc with an unsigned integer setcc.
  231. return ISD::SETCC_INVALID;
  232. unsigned Op = Op1 | Op2; // Combine all of the condition bits.
  233. // If the N and U bits get set then the resultant comparison DOES suddenly
  234. // care about orderedness, and is true when ordered.
  235. if (Op > ISD::SETTRUE2)
  236. Op &= ~16; // Clear the U bit if the N bit is set.
  237. // Canonicalize illegal integer setcc's.
  238. if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
  239. Op = ISD::SETNE;
  240. return ISD::CondCode(Op);
  241. }
  242. /// getSetCCAndOperation - Return the result of a logical AND between different
  243. /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
  244. /// function returns zero if it is not possible to represent the resultant
  245. /// comparison.
  246. ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
  247. bool isInteger) {
  248. if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
  249. // Cannot fold a signed setcc with an unsigned setcc.
  250. return ISD::SETCC_INVALID;
  251. // Combine all of the condition bits.
  252. ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
  253. // Canonicalize illegal integer setcc's.
  254. if (isInteger) {
  255. switch (Result) {
  256. default: break;
  257. case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
  258. case ISD::SETOEQ: // SETEQ & SETU[LG]E
  259. case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
  260. case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
  261. case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
  262. }
  263. }
  264. return Result;
  265. }
  266. //===----------------------------------------------------------------------===//
  267. // SDNode Profile Support
  268. //===----------------------------------------------------------------------===//
  269. /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
  270. ///
  271. static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
  272. ID.AddInteger(OpC);
  273. }
  274. /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
  275. /// solely with their pointer.
  276. static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
  277. ID.AddPointer(VTList.VTs);
  278. }
  279. /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
  280. ///
  281. static void AddNodeIDOperands(FoldingSetNodeID &ID,
  282. const SDValue *Ops, unsigned NumOps) {
  283. for (; NumOps; --NumOps, ++Ops) {
  284. ID.AddPointer(Ops->getNode());
  285. ID.AddInteger(Ops->getResNo());
  286. }
  287. }
  288. /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
  289. ///
  290. static void AddNodeIDOperands(FoldingSetNodeID &ID,
  291. const SDUse *Ops, unsigned NumOps) {
  292. for (; NumOps; --NumOps, ++Ops) {
  293. ID.AddPointer(Ops->getNode());
  294. ID.AddInteger(Ops->getResNo());
  295. }
  296. }
  297. static void AddNodeIDNode(FoldingSetNodeID &ID,
  298. unsigned short OpC, SDVTList VTList,
  299. const SDValue *OpList, unsigned N) {
  300. AddNodeIDOpcode(ID, OpC);
  301. AddNodeIDValueTypes(ID, VTList);
  302. AddNodeIDOperands(ID, OpList, N);
  303. }
  304. /// AddNodeIDCustom - If this is an SDNode with special info, add this info to
  305. /// the NodeID data.
  306. static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
  307. switch (N->getOpcode()) {
  308. case ISD::TargetExternalSymbol:
  309. case ISD::ExternalSymbol:
  310. llvm_unreachable("Should only be used on nodes with operands");
  311. default: break; // Normal nodes don't need extra info.
  312. case ISD::TargetConstant:
  313. case ISD::Constant:
  314. ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
  315. break;
  316. case ISD::TargetConstantFP:
  317. case ISD::ConstantFP: {
  318. ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
  319. break;
  320. }
  321. case ISD::TargetGlobalAddress:
  322. case ISD::GlobalAddress:
  323. case ISD::TargetGlobalTLSAddress:
  324. case ISD::GlobalTLSAddress: {
  325. const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
  326. ID.AddPointer(GA->getGlobal());
  327. ID.AddInteger(GA->getOffset());
  328. ID.AddInteger(GA->getTargetFlags());
  329. break;
  330. }
  331. case ISD::BasicBlock:
  332. ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
  333. break;
  334. case ISD::Register:
  335. ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
  336. break;
  337. case ISD::SRCVALUE:
  338. ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
  339. break;
  340. case ISD::FrameIndex:
  341. case ISD::TargetFrameIndex:
  342. ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
  343. break;
  344. case ISD::JumpTable:
  345. case ISD::TargetJumpTable:
  346. ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
  347. ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
  348. break;
  349. case ISD::ConstantPool:
  350. case ISD::TargetConstantPool: {
  351. const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
  352. ID.AddInteger(CP->getAlignment());
  353. ID.AddInteger(CP->getOffset());
  354. if (CP->isMachineConstantPoolEntry())
  355. CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
  356. else
  357. ID.AddPointer(CP->getConstVal());
  358. ID.AddInteger(CP->getTargetFlags());
  359. break;
  360. }
  361. case ISD::LOAD: {
  362. const LoadSDNode *LD = cast<LoadSDNode>(N);
  363. ID.AddInteger(LD->getMemoryVT().getRawBits());
  364. ID.AddInteger(LD->getRawSubclassData());
  365. break;
  366. }
  367. case ISD::STORE: {
  368. const StoreSDNode *ST = cast<StoreSDNode>(N);
  369. ID.AddInteger(ST->getMemoryVT().getRawBits());
  370. ID.AddInteger(ST->getRawSubclassData());
  371. break;
  372. }
  373. case ISD::ATOMIC_CMP_SWAP:
  374. case ISD::ATOMIC_SWAP:
  375. case ISD::ATOMIC_LOAD_ADD:
  376. case ISD::ATOMIC_LOAD_SUB:
  377. case ISD::ATOMIC_LOAD_AND:
  378. case ISD::ATOMIC_LOAD_OR:
  379. case ISD::ATOMIC_LOAD_XOR:
  380. case ISD::ATOMIC_LOAD_NAND:
  381. case ISD::ATOMIC_LOAD_MIN:
  382. case ISD::ATOMIC_LOAD_MAX:
  383. case ISD::ATOMIC_LOAD_UMIN:
  384. case ISD::ATOMIC_LOAD_UMAX: {
  385. const AtomicSDNode *AT = cast<AtomicSDNode>(N);
  386. ID.AddInteger(AT->getMemoryVT().getRawBits());
  387. ID.AddInteger(AT->getRawSubclassData());
  388. break;
  389. }
  390. case ISD::VECTOR_SHUFFLE: {
  391. const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
  392. for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
  393. i != e; ++i)
  394. ID.AddInteger(SVN->getMaskElt(i));
  395. break;
  396. }
  397. case ISD::TargetBlockAddress:
  398. case ISD::BlockAddress: {
  399. ID.AddPointer(cast<BlockAddressSDNode>(N)->getBlockAddress());
  400. ID.AddInteger(cast<BlockAddressSDNode>(N)->getTargetFlags());
  401. break;
  402. }
  403. } // end switch (N->getOpcode())
  404. }
  405. /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
  406. /// data.
  407. static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
  408. AddNodeIDOpcode(ID, N->getOpcode());
  409. // Add the return value info.
  410. AddNodeIDValueTypes(ID, N->getVTList());
  411. // Add the operand info.
  412. AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
  413. // Handle SDNode leafs with special info.
  414. AddNodeIDCustom(ID, N);
  415. }
  416. /// encodeMemSDNodeFlags - Generic routine for computing a value for use in
  417. /// the CSE map that carries volatility, temporalness, indexing mode, and
  418. /// extension/truncation information.
  419. ///
  420. static inline unsigned
  421. encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
  422. bool isNonTemporal) {
  423. assert((ConvType & 3) == ConvType &&
  424. "ConvType may not require more than 2 bits!");
  425. assert((AM & 7) == AM &&
  426. "AM may not require more than 3 bits!");
  427. return ConvType |
  428. (AM << 2) |
  429. (isVolatile << 5) |
  430. (isNonTemporal << 6);
  431. }
  432. //===----------------------------------------------------------------------===//
  433. // SelectionDAG Class
  434. //===----------------------------------------------------------------------===//
  435. /// doNotCSE - Return true if CSE should not be performed for this node.
  436. static bool doNotCSE(SDNode *N) {
  437. if (N->getValueType(0) == MVT::Flag)
  438. return true; // Never CSE anything that produces a flag.
  439. switch (N->getOpcode()) {
  440. default: break;
  441. case ISD::HANDLENODE:
  442. case ISD::EH_LABEL:
  443. return true; // Never CSE these nodes.
  444. }
  445. // Check that remaining values produced are not flags.
  446. for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
  447. if (N->getValueType(i) == MVT::Flag)
  448. return true; // Never CSE anything that produces a flag.
  449. return false;
  450. }
  451. /// RemoveDeadNodes - This method deletes all unreachable nodes in the
  452. /// SelectionDAG.
  453. void SelectionDAG::RemoveDeadNodes() {
  454. // Create a dummy node (which is not added to allnodes), that adds a reference
  455. // to the root node, preventing it from being deleted.
  456. HandleSDNode Dummy(getRoot());
  457. SmallVector<SDNode*, 128> DeadNodes;
  458. // Add all obviously-dead nodes to the DeadNodes worklist.
  459. for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
  460. if (I->use_empty())
  461. DeadNodes.push_back(I);
  462. RemoveDeadNodes(DeadNodes);
  463. // If the root changed (e.g. it was a dead load, update the root).
  464. setRoot(Dummy.getValue());
  465. }
  466. /// RemoveDeadNodes - This method deletes the unreachable nodes in the
  467. /// given list, and any nodes that become unreachable as a result.
  468. void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
  469. DAGUpdateListener *UpdateListener) {
  470. // Process the worklist, deleting the nodes and adding their uses to the
  471. // worklist.
  472. while (!DeadNodes.empty()) {
  473. SDNode *N = DeadNodes.pop_back_val();
  474. if (UpdateListener)
  475. UpdateListener->NodeDeleted(N, 0);
  476. // Take the node out of the appropriate CSE map.
  477. RemoveNodeFromCSEMaps(N);
  478. // Next, brutally remove the operand list. This is safe to do, as there are
  479. // no cycles in the graph.
  480. for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
  481. SDUse &Use = *I++;
  482. SDNode *Operand = Use.getNode();
  483. Use.set(SDValue());
  484. // Now that we removed this operand, see if there are no uses of it left.
  485. if (Operand->use_empty())
  486. DeadNodes.push_back(Operand);
  487. }
  488. DeallocateNode(N);
  489. }
  490. }
  491. void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
  492. SmallVector<SDNode*, 16> DeadNodes(1, N);
  493. RemoveDeadNodes(DeadNodes, UpdateListener);
  494. }
  495. void SelectionDAG::DeleteNode(SDNode *N) {
  496. // First take this out of the appropriate CSE map.
  497. RemoveNodeFromCSEMaps(N);
  498. // Finally, remove uses due to operands of this node, remove from the
  499. // AllNodes list, and delete the node.
  500. DeleteNodeNotInCSEMaps(N);
  501. }
  502. void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
  503. assert(N != AllNodes.begin() && "Cannot delete the entry node!");
  504. assert(N->use_empty() && "Cannot delete a node that is not dead!");
  505. // Drop all of the operands and decrement used node's use counts.
  506. N->DropOperands();
  507. DeallocateNode(N);
  508. }
  509. void SelectionDAG::DeallocateNode(SDNode *N) {
  510. if (N->OperandsNeedDelete)
  511. delete[] N->OperandList;
  512. // Set the opcode to DELETED_NODE to help catch bugs when node
  513. // memory is reallocated.
  514. N->NodeType = ISD::DELETED_NODE;
  515. NodeAllocator.Deallocate(AllNodes.remove(N));
  516. // Remove the ordering of this node.
  517. Ordering->remove(N);
  518. // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
  519. SmallVector<SDDbgValue*, 2> &DbgVals = DbgInfo->getSDDbgValues(N);
  520. for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
  521. DbgVals[i]->setIsInvalidated();
  522. }
  523. /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
  524. /// correspond to it. This is useful when we're about to delete or repurpose
  525. /// the node. We don't want future request for structurally identical nodes
  526. /// to return N anymore.
  527. bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
  528. bool Erased = false;
  529. switch (N->getOpcode()) {
  530. case ISD::EntryToken:
  531. llvm_unreachable("EntryToken should not be in CSEMaps!");
  532. return false;
  533. case ISD::HANDLENODE: return false; // noop.
  534. case ISD::CONDCODE:
  535. assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
  536. "Cond code doesn't exist!");
  537. Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
  538. CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
  539. break;
  540. case ISD::ExternalSymbol:
  541. Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
  542. break;
  543. case ISD::TargetExternalSymbol: {
  544. ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
  545. Erased = TargetExternalSymbols.erase(
  546. std::pair<std::string,unsigned char>(ESN->getSymbol(),
  547. ESN->getTargetFlags()));
  548. break;
  549. }
  550. case ISD::VALUETYPE: {
  551. EVT VT = cast<VTSDNode>(N)->getVT();
  552. if (VT.isExtended()) {
  553. Erased = ExtendedValueTypeNodes.erase(VT);
  554. } else {
  555. Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
  556. ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
  557. }
  558. break;
  559. }
  560. default:
  561. // Remove it from the CSE Map.
  562. Erased = CSEMap.RemoveNode(N);
  563. break;
  564. }
  565. #ifndef NDEBUG
  566. // Verify that the node was actually in one of the CSE maps, unless it has a
  567. // flag result (which cannot be CSE'd) or is one of the special cases that are
  568. // not subject to CSE.
  569. if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
  570. !N->isMachineOpcode() && !doNotCSE(N)) {
  571. N->dump(this);
  572. dbgs() << "\n";
  573. llvm_unreachable("Node is not in map!");
  574. }
  575. #endif
  576. return Erased;
  577. }
  578. /// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
  579. /// maps and modified in place. Add it back to the CSE maps, unless an identical
  580. /// node already exists, in which case transfer all its users to the existing
  581. /// node. This transfer can potentially trigger recursive merging.
  582. ///
  583. void
  584. SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
  585. DAGUpdateListener *UpdateListener) {
  586. // For node types that aren't CSE'd, just act as if no identical node
  587. // already exists.
  588. if (!doNotCSE(N)) {
  589. SDNode *Existing = CSEMap.GetOrInsertNode(N);
  590. if (Existing != N) {
  591. // If there was already an existing matching node, use ReplaceAllUsesWith
  592. // to replace the dead one with the existing one. This can cause
  593. // recursive merging of other unrelated nodes down the line.
  594. ReplaceAllUsesWith(N, Existing, UpdateListener);
  595. // N is now dead. Inform the listener if it exists and delete it.
  596. if (UpdateListener)
  597. UpdateListener->NodeDeleted(N, Existing);
  598. DeleteNodeNotInCSEMaps(N);
  599. return;
  600. }
  601. }
  602. // If the node doesn't already exist, we updated it. Inform a listener if
  603. // it exists.
  604. if (UpdateListener)
  605. UpdateListener->NodeUpdated(N);
  606. }
  607. /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
  608. /// were replaced with those specified. If this node is never memoized,
  609. /// return null, otherwise return a pointer to the slot it would take. If a
  610. /// node already exists with these operands, the slot will be non-null.
  611. SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
  612. void *&InsertPos) {
  613. if (doNotCSE(N))
  614. return 0;
  615. SDValue Ops[] = { Op };
  616. FoldingSetNodeID ID;
  617. AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
  618. AddNodeIDCustom(ID, N);
  619. SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
  620. return Node;
  621. }
  622. /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
  623. /// were replaced with those specified. If this node is never memoized,
  624. /// return null, otherwise return a pointer to the slot it would take. If a
  625. /// node already exists with these operands, the slot will be non-null.
  626. SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
  627. SDValue Op1, SDValue Op2,
  628. void *&InsertPos) {
  629. if (doNotCSE(N))
  630. return 0;
  631. SDValue Ops[] = { Op1, Op2 };
  632. FoldingSetNodeID ID;
  633. AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
  634. AddNodeIDCustom(ID, N);
  635. SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
  636. return Node;
  637. }
  638. /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
  639. /// were replaced with those specified. If this node is never memoized,
  640. /// return null, otherwise return a pointer to the slot it would take. If a
  641. /// node already exists with these operands, the slot will be non-null.
  642. SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
  643. const SDValue *Ops,unsigned NumOps,
  644. void *&InsertPos) {
  645. if (doNotCSE(N))
  646. return 0;
  647. FoldingSetNodeID ID;
  648. AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
  649. AddNodeIDCustom(ID, N);
  650. SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
  651. return Node;
  652. }
  653. /// VerifyNode - Sanity check the given node. Aborts if it is invalid.
  654. void SelectionDAG::VerifyNode(SDNode *N) {
  655. switch (N->getOpcode()) {
  656. default:
  657. break;
  658. case ISD::BUILD_PAIR: {
  659. EVT VT = N->getValueType(0);
  660. assert(N->getNumValues() == 1 && "Too many results!");
  661. assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
  662. "Wrong return type!");
  663. assert(N->getNumOperands() == 2 && "Wrong number of operands!");
  664. assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
  665. "Mismatched operand types!");
  666. assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
  667. "Wrong operand type!");
  668. assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
  669. "Wrong return type size");
  670. break;
  671. }
  672. case ISD::BUILD_VECTOR: {
  673. assert(N->getNumValues() == 1 && "Too many results!");
  674. assert(N->getValueType(0).isVector() && "Wrong return type!");
  675. assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
  676. "Wrong number of operands!");
  677. EVT EltVT = N->getValueType(0).getVectorElementType();
  678. for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
  679. assert((I->getValueType() == EltVT ||
  680. (EltVT.isInteger() && I->getValueType().isInteger() &&
  681. EltVT.bitsLE(I->getValueType()))) &&
  682. "Wrong operand type!");
  683. break;
  684. }
  685. }
  686. }
  687. /// getEVTAlignment - Compute the default alignment value for the
  688. /// given type.
  689. ///
  690. unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
  691. const Type *Ty = VT == MVT::iPTR ?
  692. PointerType::get(Type::getInt8Ty(*getContext()), 0) :
  693. VT.getTypeForEVT(*getContext());
  694. return TLI.getTargetData()->getABITypeAlignment(Ty);
  695. }
  696. // EntryNode could meaningfully have debug info if we can find it...
  697. SelectionDAG::SelectionDAG(const TargetMachine &tm)
  698. : TM(tm), TLI(*tm.getTargetLowering()), TSI(*tm.getSelectionDAGInfo()),
  699. EntryNode(ISD::EntryToken, DebugLoc(), getVTList(MVT::Other)),
  700. Root(getEntryNode()), Ordering(0) {
  701. AllNodes.push_back(&EntryNode);
  702. Ordering = new SDNodeOrdering();
  703. DbgInfo = new SDDbgInfo();
  704. }
  705. void SelectionDAG::init(MachineFunction &mf) {
  706. MF = &mf;
  707. Context = &mf.getFunction()->getContext();
  708. }
  709. SelectionDAG::~SelectionDAG() {
  710. allnodes_clear();
  711. delete Ordering;
  712. delete DbgInfo;
  713. }
  714. void SelectionDAG::allnodes_clear() {
  715. assert(&*AllNodes.begin() == &EntryNode);
  716. AllNodes.remove(AllNodes.begin());
  717. while (!AllNodes.empty())
  718. DeallocateNode(AllNodes.begin());
  719. }
  720. void SelectionDAG::clear() {
  721. allnodes_clear();
  722. OperandAllocator.Reset();
  723. CSEMap.clear();
  724. ExtendedValueTypeNodes.clear();
  725. ExternalSymbols.clear();
  726. TargetExternalSymbols.clear();
  727. std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
  728. static_cast<CondCodeSDNode*>(0));
  729. std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
  730. static_cast<SDNode*>(0));
  731. EntryNode.UseList = 0;
  732. AllNodes.push_back(&EntryNode);
  733. Root = getEntryNode();
  734. Ordering->clear();
  735. DbgInfo->clear();
  736. }
  737. SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
  738. return VT.bitsGT(Op.getValueType()) ?
  739. getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
  740. getNode(ISD::TRUNCATE, DL, VT, Op);
  741. }
  742. SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
  743. return VT.bitsGT(Op.getValueType()) ?
  744. getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
  745. getNode(ISD::TRUNCATE, DL, VT, Op);
  746. }
  747. SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) {
  748. assert(!VT.isVector() &&
  749. "getZeroExtendInReg should use the vector element type instead of "
  750. "the vector type!");
  751. if (Op.getValueType() == VT) return Op;
  752. unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
  753. APInt Imm = APInt::getLowBitsSet(BitWidth,
  754. VT.getSizeInBits());
  755. return getNode(ISD::AND, DL, Op.getValueType(), Op,
  756. getConstant(Imm, Op.getValueType()));
  757. }
  758. /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
  759. ///
  760. SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) {
  761. EVT EltVT = VT.getScalarType();
  762. SDValue NegOne =
  763. getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
  764. return getNode(ISD::XOR, DL, VT, Val, NegOne);
  765. }
  766. SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
  767. EVT EltVT = VT.getScalarType();
  768. assert((EltVT.getSizeInBits() >= 64 ||
  769. (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
  770. "getConstant with a uint64_t value that doesn't fit in the type!");
  771. return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
  772. }
  773. SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
  774. return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
  775. }
  776. SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
  777. assert(VT.isInteger() && "Cannot create FP integer constant!");
  778. EVT EltVT = VT.getScalarType();
  779. assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
  780. "APInt size does not match type size!");
  781. unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
  782. FoldingSetNodeID ID;
  783. AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
  784. ID.AddPointer(&Val);
  785. void *IP = 0;
  786. SDNode *N = NULL;
  787. if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
  788. if (!VT.isVector())
  789. return SDValue(N, 0);
  790. if (!N) {
  791. N = new (NodeAllocator) ConstantSDNode(isT, &Val, EltVT);
  792. CSEMap.InsertNode(N, IP);
  793. AllNodes.push_back(N);
  794. }
  795. SDValue Result(N, 0);
  796. if (VT.isVector()) {
  797. SmallVector<SDValue, 8> Ops;
  798. Ops.assign(VT.getVectorNumElements(), Result);
  799. Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
  800. }
  801. return Result;
  802. }
  803. SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
  804. return getConstant(Val, TLI.getPointerTy(), isTarget);
  805. }
  806. SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
  807. return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
  808. }
  809. SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
  810. assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
  811. EVT EltVT = VT.getScalarType();
  812. // Do the map lookup using the actual bit pattern for the floating point
  813. // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
  814. // we don't have issues with SNANs.
  815. unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
  816. FoldingSetNodeID ID;
  817. AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
  818. ID.AddPointer(&V);
  819. void *IP = 0;
  820. SDNode *N = NULL;
  821. if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
  822. if (!VT.isVector())
  823. return SDValue(N, 0);
  824. if (!N) {
  825. N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
  826. CSEMap.InsertNode(N, IP);
  827. AllNodes.push_back(N);
  828. }
  829. SDValue Result(N, 0);
  830. if (VT.isVector()) {
  831. SmallVector<SDValue, 8> Ops;
  832. Ops.assign(VT.getVectorNumElements(), Result);
  833. // FIXME DebugLoc info might be appropriate here
  834. Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
  835. }
  836. return Result;
  837. }
  838. SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
  839. EVT EltVT = VT.getScalarType();
  840. if (EltVT==MVT::f32)
  841. return getConstantFP(APFloat((float)Val), VT, isTarget);
  842. else if (EltVT==MVT::f64)
  843. return getConstantFP(APFloat(Val), VT, isTarget);
  844. else if (EltVT==MVT::f80 || EltVT==MVT::f128) {
  845. bool ignored;
  846. APFloat apf = APFloat(Val);
  847. apf.convert(*EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
  848. &ignored);
  849. return getConstantFP(apf, VT, isTarget);
  850. } else {
  851. assert(0 && "Unsupported type in getConstantFP");
  852. return SDValue();
  853. }
  854. }
  855. SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, DebugLoc DL,
  856. EVT VT, int64_t Offset,
  857. bool isTargetGA,
  858. unsigned char TargetFlags) {
  859. assert((TargetFlags == 0 || isTargetGA) &&
  860. "Cannot set target flags on target-independent globals");
  861. // Truncate (with sign-extension) the offset value to the pointer size.
  862. EVT PTy = TLI.getPointerTy();
  863. unsigned BitWidth = PTy.getSizeInBits();
  864. if (BitWidth < 64)
  865. Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
  866. const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
  867. if (!GVar) {
  868. // If GV is an alias then use the aliasee for determining thread-localness.
  869. if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
  870. GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
  871. }
  872. unsigned Opc;
  873. if (GVar && GVar->isThreadLocal())
  874. Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
  875. else
  876. Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
  877. FoldingSetNodeID ID;
  878. AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
  879. ID.AddPointer(GV);
  880. ID.AddInteger(Offset);
  881. ID.AddInteger(TargetFlags);
  882. void *IP = 0;
  883. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  884. return SDValue(E, 0);
  885. SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL, GV, VT,
  886. Offset, TargetFlags);
  887. CSEMap.InsertNode(N, IP);
  888. AllNodes.push_back(N);
  889. return SDValue(N, 0);
  890. }
  891. SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
  892. unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
  893. FoldingSetNodeID ID;
  894. AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
  895. ID.AddInteger(FI);
  896. void *IP = 0;
  897. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  898. return SDValue(E, 0);
  899. SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
  900. CSEMap.InsertNode(N, IP);
  901. AllNodes.push_back(N);
  902. return SDValue(N, 0);
  903. }
  904. SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
  905. unsigned char TargetFlags) {
  906. assert((TargetFlags == 0 || isTarget) &&
  907. "Cannot set target flags on target-independent jump tables");
  908. unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
  909. FoldingSetNodeID ID;
  910. AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
  911. ID.AddInteger(JTI);
  912. ID.AddInteger(TargetFlags);
  913. void *IP = 0;
  914. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  915. return SDValue(E, 0);
  916. SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
  917. TargetFlags);
  918. CSEMap.InsertNode(N, IP);
  919. AllNodes.push_back(N);
  920. return SDValue(N, 0);
  921. }
  922. SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
  923. unsigned Alignment, int Offset,
  924. bool isTarget,
  925. unsigned char TargetFlags) {
  926. assert((TargetFlags == 0 || isTarget) &&
  927. "Cannot set target flags on target-independent globals");
  928. if (Alignment == 0)
  929. Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
  930. unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
  931. FoldingSetNodeID ID;
  932. AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
  933. ID.AddInteger(Alignment);
  934. ID.AddInteger(Offset);
  935. ID.AddPointer(C);
  936. ID.AddInteger(TargetFlags);
  937. void *IP = 0;
  938. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  939. return SDValue(E, 0);
  940. SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
  941. Alignment, TargetFlags);
  942. CSEMap.InsertNode(N, IP);
  943. AllNodes.push_back(N);
  944. return SDValue(N, 0);
  945. }
  946. SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
  947. unsigned Alignment, int Offset,
  948. bool isTarget,
  949. unsigned char TargetFlags) {
  950. assert((TargetFlags == 0 || isTarget) &&
  951. "Cannot set target flags on target-independent globals");
  952. if (Alignment == 0)
  953. Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
  954. unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
  955. FoldingSetNodeID ID;
  956. AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
  957. ID.AddInteger(Alignment);
  958. ID.AddInteger(Offset);
  959. C->AddSelectionDAGCSEId(ID);
  960. ID.AddInteger(TargetFlags);
  961. void *IP = 0;
  962. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  963. return SDValue(E, 0);
  964. SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
  965. Alignment, TargetFlags);
  966. CSEMap.InsertNode(N, IP);
  967. AllNodes.push_back(N);
  968. return SDValue(N, 0);
  969. }
  970. SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
  971. FoldingSetNodeID ID;
  972. AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
  973. ID.AddPointer(MBB);
  974. void *IP = 0;
  975. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  976. return SDValue(E, 0);
  977. SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
  978. CSEMap.InsertNode(N, IP);
  979. AllNodes.push_back(N);
  980. return SDValue(N, 0);
  981. }
  982. SDValue SelectionDAG::getValueType(EVT VT) {
  983. if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
  984. ValueTypeNodes.size())
  985. ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
  986. SDNode *&N = VT.isExtended() ?
  987. ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
  988. if (N) return SDValue(N, 0);
  989. N = new (NodeAllocator) VTSDNode(VT);
  990. AllNodes.push_back(N);
  991. return SDValue(N, 0);
  992. }
  993. SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
  994. SDNode *&N = ExternalSymbols[Sym];
  995. if (N) return SDValue(N, 0);
  996. N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
  997. AllNodes.push_back(N);
  998. return SDValue(N, 0);
  999. }
  1000. SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
  1001. unsigned char TargetFlags) {
  1002. SDNode *&N =
  1003. TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
  1004. TargetFlags)];
  1005. if (N) return SDValue(N, 0);
  1006. N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
  1007. AllNodes.push_back(N);
  1008. return SDValue(N, 0);
  1009. }
  1010. SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
  1011. if ((unsigned)Cond >= CondCodeNodes.size())
  1012. CondCodeNodes.resize(Cond+1);
  1013. if (CondCodeNodes[Cond] == 0) {
  1014. CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
  1015. CondCodeNodes[Cond] = N;
  1016. AllNodes.push_back(N);
  1017. }
  1018. return SDValue(CondCodeNodes[Cond], 0);
  1019. }
  1020. // commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
  1021. // the shuffle mask M that point at N1 to point at N2, and indices that point
  1022. // N2 to point at N1.
  1023. static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
  1024. std::swap(N1, N2);
  1025. int NElts = M.size();
  1026. for (int i = 0; i != NElts; ++i) {
  1027. if (M[i] >= NElts)
  1028. M[i] -= NElts;
  1029. else if (M[i] >= 0)
  1030. M[i] += NElts;
  1031. }
  1032. }
  1033. SDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1,
  1034. SDValue N2, const int *Mask) {
  1035. assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
  1036. assert(VT.isVector() && N1.getValueType().isVector() &&
  1037. "Vector Shuffle VTs must be a vectors");
  1038. assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
  1039. && "Vector Shuffle VTs must have same element type");
  1040. // Canonicalize shuffle undef, undef -> undef
  1041. if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
  1042. return getUNDEF(VT);
  1043. // Validate that all indices in Mask are within the range of the elements
  1044. // input to the shuffle.
  1045. unsigned NElts = VT.getVectorNumElements();
  1046. SmallVector<int, 8> MaskVec;
  1047. for (unsigned i = 0; i != NElts; ++i) {
  1048. assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
  1049. MaskVec.push_back(Mask[i]);
  1050. }
  1051. // Canonicalize shuffle v, v -> v, undef
  1052. if (N1 == N2) {
  1053. N2 = getUNDEF(VT);
  1054. for (unsigned i = 0; i != NElts; ++i)
  1055. if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
  1056. }
  1057. // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
  1058. if (N1.getOpcode() == ISD::UNDEF)
  1059. commuteShuffle(N1, N2, MaskVec);
  1060. // Canonicalize all index into lhs, -> shuffle lhs, undef
  1061. // Canonicalize all index into rhs, -> shuffle rhs, undef
  1062. bool AllLHS = true, AllRHS = true;
  1063. bool N2Undef = N2.getOpcode() == ISD::UNDEF;
  1064. for (unsigned i = 0; i != NElts; ++i) {
  1065. if (MaskVec[i] >= (int)NElts) {
  1066. if (N2Undef)
  1067. MaskVec[i] = -1;
  1068. else
  1069. AllLHS = false;
  1070. } else if (MaskVec[i] >= 0) {
  1071. AllRHS = false;
  1072. }
  1073. }
  1074. if (AllLHS && AllRHS)
  1075. return getUNDEF(VT);
  1076. if (AllLHS && !N2Undef)
  1077. N2 = getUNDEF(VT);
  1078. if (AllRHS) {
  1079. N1 = getUNDEF(VT);
  1080. commuteShuffle(N1, N2, MaskVec);
  1081. }
  1082. // If Identity shuffle, or all shuffle in to undef, return that node.
  1083. bool AllUndef = true;
  1084. bool Identity = true;
  1085. for (unsigned i = 0; i != NElts; ++i) {
  1086. if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
  1087. if (MaskVec[i] >= 0) AllUndef = false;
  1088. }
  1089. if (Identity && NElts == N1.getValueType().getVectorNumElements())
  1090. return N1;
  1091. if (AllUndef)
  1092. return getUNDEF(VT);
  1093. FoldingSetNodeID ID;
  1094. SDValue Ops[2] = { N1, N2 };
  1095. AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
  1096. for (unsigned i = 0; i != NElts; ++i)
  1097. ID.AddInteger(MaskVec[i]);
  1098. void* IP = 0;
  1099. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1100. return SDValue(E, 0);
  1101. // Allocate the mask array for the node out of the BumpPtrAllocator, since
  1102. // SDNode doesn't have access to it. This memory will be "leaked" when
  1103. // the node is deallocated, but recovered when the NodeAllocator is released.
  1104. int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
  1105. memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
  1106. ShuffleVectorSDNode *N =
  1107. new (NodeAllocator) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
  1108. CSEMap.InsertNode(N, IP);
  1109. AllNodes.push_back(N);
  1110. return SDValue(N, 0);
  1111. }
  1112. SDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl,
  1113. SDValue Val, SDValue DTy,
  1114. SDValue STy, SDValue Rnd, SDValue Sat,
  1115. ISD::CvtCode Code) {
  1116. // If the src and dest types are the same and the conversion is between
  1117. // integer types of the same sign or two floats, no conversion is necessary.
  1118. if (DTy == STy &&
  1119. (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
  1120. return Val;
  1121. FoldingSetNodeID ID;
  1122. SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
  1123. AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
  1124. void* IP = 0;
  1125. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1126. return SDValue(E, 0);
  1127. CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl, Ops, 5,
  1128. Code);
  1129. CSEMap.InsertNode(N, IP);
  1130. AllNodes.push_back(N);
  1131. return SDValue(N, 0);
  1132. }
  1133. SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
  1134. FoldingSetNodeID ID;
  1135. AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
  1136. ID.AddInteger(RegNo);
  1137. void *IP = 0;
  1138. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1139. return SDValue(E, 0);
  1140. SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
  1141. CSEMap.InsertNode(N, IP);
  1142. AllNodes.push_back(N);
  1143. return SDValue(N, 0);
  1144. }
  1145. SDValue SelectionDAG::getEHLabel(DebugLoc dl, SDValue Root, MCSymbol *Label) {
  1146. FoldingSetNodeID ID;
  1147. SDValue Ops[] = { Root };
  1148. AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
  1149. ID.AddPointer(Label);
  1150. void *IP = 0;
  1151. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1152. return SDValue(E, 0);
  1153. SDNode *N = new (NodeAllocator) EHLabelSDNode(dl, Root, Label);
  1154. CSEMap.InsertNode(N, IP);
  1155. AllNodes.push_back(N);
  1156. return SDValue(N, 0);
  1157. }
  1158. SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
  1159. bool isTarget,
  1160. unsigned char TargetFlags) {
  1161. unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
  1162. FoldingSetNodeID ID;
  1163. AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
  1164. ID.AddPointer(BA);
  1165. ID.AddInteger(TargetFlags);
  1166. void *IP = 0;
  1167. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1168. return SDValue(E, 0);
  1169. SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, TargetFlags);
  1170. CSEMap.InsertNode(N, IP);
  1171. AllNodes.push_back(N);
  1172. return SDValue(N, 0);
  1173. }
  1174. SDValue SelectionDAG::getSrcValue(const Value *V) {
  1175. assert((!V || V->getType()->isPointerTy()) &&
  1176. "SrcValue is not a pointer?");
  1177. FoldingSetNodeID ID;
  1178. AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
  1179. ID.AddPointer(V);
  1180. void *IP = 0;
  1181. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1182. return SDValue(E, 0);
  1183. SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
  1184. CSEMap.InsertNode(N, IP);
  1185. AllNodes.push_back(N);
  1186. return SDValue(N, 0);
  1187. }
  1188. /// getMDNode - Return an MDNodeSDNode which holds an MDNode.
  1189. SDValue SelectionDAG::getMDNode(const MDNode *MD) {
  1190. FoldingSetNodeID ID;
  1191. AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
  1192. ID.AddPointer(MD);
  1193. void *IP = 0;
  1194. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1195. return SDValue(E, 0);
  1196. SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
  1197. CSEMap.InsertNode(N, IP);
  1198. AllNodes.push_back(N);
  1199. return SDValue(N, 0);
  1200. }
  1201. /// getShiftAmountOperand - Return the specified value casted to
  1202. /// the target's desired shift amount type.
  1203. SDValue SelectionDAG::getShiftAmountOperand(SDValue Op) {
  1204. EVT OpTy = Op.getValueType();
  1205. MVT ShTy = TLI.getShiftAmountTy();
  1206. if (OpTy == ShTy || OpTy.isVector()) return Op;
  1207. ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
  1208. return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
  1209. }
  1210. /// CreateStackTemporary - Create a stack temporary, suitable for holding the
  1211. /// specified value type.
  1212. SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
  1213. MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
  1214. unsigned ByteSize = VT.getStoreSize();
  1215. const Type *Ty = VT.getTypeForEVT(*getContext());
  1216. unsigned StackAlign =
  1217. std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
  1218. int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
  1219. return getFrameIndex(FrameIdx, TLI.getPointerTy());
  1220. }
  1221. /// CreateStackTemporary - Create a stack temporary suitable for holding
  1222. /// either of the specified value types.
  1223. SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
  1224. unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
  1225. VT2.getStoreSizeInBits())/8;
  1226. const Type *Ty1 = VT1.getTypeForEVT(*getContext());
  1227. const Type *Ty2 = VT2.getTypeForEVT(*getContext());
  1228. const TargetData *TD = TLI.getTargetData();
  1229. unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
  1230. TD->getPrefTypeAlignment(Ty2));
  1231. MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
  1232. int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
  1233. return getFrameIndex(FrameIdx, TLI.getPointerTy());
  1234. }
  1235. SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
  1236. SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
  1237. // These setcc operations always fold.
  1238. switch (Cond) {
  1239. default: break;
  1240. case ISD::SETFALSE:
  1241. case ISD::SETFALSE2: return getConstant(0, VT);
  1242. case ISD::SETTRUE:
  1243. case ISD::SETTRUE2: return getConstant(1, VT);
  1244. case ISD::SETOEQ:
  1245. case ISD::SETOGT:
  1246. case ISD::SETOGE:
  1247. case ISD::SETOLT:
  1248. case ISD::SETOLE:
  1249. case ISD::SETONE:
  1250. case ISD::SETO:
  1251. case ISD::SETUO:
  1252. case ISD::SETUEQ:
  1253. case ISD::SETUNE:
  1254. assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
  1255. break;
  1256. }
  1257. if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
  1258. const APInt &C2 = N2C->getAPIntValue();
  1259. if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
  1260. const APInt &C1 = N1C->getAPIntValue();
  1261. switch (Cond) {
  1262. default: llvm_unreachable("Unknown integer setcc!");
  1263. case ISD::SETEQ: return getConstant(C1 == C2, VT);
  1264. case ISD::SETNE: return getConstant(C1 != C2, VT);
  1265. case ISD::SETULT: return getConstant(C1.ult(C2), VT);
  1266. case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
  1267. case ISD::SETULE: return getConstant(C1.ule(C2), VT);
  1268. case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
  1269. case ISD::SETLT: return getConstant(C1.slt(C2), VT);
  1270. case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
  1271. case ISD::SETLE: return getConstant(C1.sle(C2), VT);
  1272. case ISD::SETGE: return getConstant(C1.sge(C2), VT);
  1273. }
  1274. }
  1275. }
  1276. if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
  1277. if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
  1278. // No compile time operations on this type yet.
  1279. if (N1C->getValueType(0) == MVT::ppcf128)
  1280. return SDValue();
  1281. APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
  1282. switch (Cond) {
  1283. default: break;
  1284. case ISD::SETEQ: if (R==APFloat::cmpUnordered)
  1285. return getUNDEF(VT);
  1286. // fall through
  1287. case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
  1288. case ISD::SETNE: if (R==APFloat::cmpUnordered)
  1289. return getUNDEF(VT);
  1290. // fall through
  1291. case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
  1292. R==APFloat::cmpLessThan, VT);
  1293. case ISD::SETLT: if (R==APFloat::cmpUnordered)
  1294. return getUNDEF(VT);
  1295. // fall through
  1296. case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
  1297. case ISD::SETGT: if (R==APFloat::cmpUnordered)
  1298. return getUNDEF(VT);
  1299. // fall through
  1300. case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
  1301. case ISD::SETLE: if (R==APFloat::cmpUnordered)
  1302. return getUNDEF(VT);
  1303. // fall through
  1304. case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
  1305. R==APFloat::cmpEqual, VT);
  1306. case ISD::SETGE: if (R==APFloat::cmpUnordered)
  1307. return getUNDEF(VT);
  1308. // fall through
  1309. case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
  1310. R==APFloat::cmpEqual, VT);
  1311. case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
  1312. case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
  1313. case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
  1314. R==APFloat::cmpEqual, VT);
  1315. case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
  1316. case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
  1317. R==APFloat::cmpLessThan, VT);
  1318. case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
  1319. R==APFloat::cmpUnordered, VT);
  1320. case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
  1321. case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
  1322. }
  1323. } else {
  1324. // Ensure that the constant occurs on the RHS.
  1325. return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
  1326. }
  1327. }
  1328. // Could not fold it.
  1329. return SDValue();
  1330. }
  1331. /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
  1332. /// use this predicate to simplify operations downstream.
  1333. bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
  1334. // This predicate is not safe for vector operations.
  1335. if (Op.getValueType().isVector())
  1336. return false;
  1337. unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
  1338. return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
  1339. }
  1340. /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
  1341. /// this predicate to simplify operations downstream. Mask is known to be zero
  1342. /// for bits that V cannot have.
  1343. bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
  1344. unsigned Depth) const {
  1345. APInt KnownZero, KnownOne;
  1346. ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
  1347. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1348. return (KnownZero & Mask) == Mask;
  1349. }
  1350. /// ComputeMaskedBits - Determine which of the bits specified in Mask are
  1351. /// known to be either zero or one and return them in the KnownZero/KnownOne
  1352. /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
  1353. /// processing.
  1354. void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
  1355. APInt &KnownZero, APInt &KnownOne,
  1356. unsigned Depth) const {
  1357. unsigned BitWidth = Mask.getBitWidth();
  1358. assert(BitWidth == Op.getValueType().getScalarType().getSizeInBits() &&
  1359. "Mask size mismatches value type size!");
  1360. KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
  1361. if (Depth == 6 || Mask == 0)
  1362. return; // Limit search depth.
  1363. APInt KnownZero2, KnownOne2;
  1364. switch (Op.getOpcode()) {
  1365. case ISD::Constant:
  1366. // We know all of the bits for a constant!
  1367. KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
  1368. KnownZero = ~KnownOne & Mask;
  1369. return;
  1370. case ISD::AND:
  1371. // If either the LHS or the RHS are Zero, the result is zero.
  1372. ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
  1373. ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
  1374. KnownZero2, KnownOne2, Depth+1);
  1375. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1376. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1377. // Output known-1 bits are only known if set in both the LHS & RHS.
  1378. KnownOne &= KnownOne2;
  1379. // Output known-0 are known to be clear if zero in either the LHS | RHS.
  1380. KnownZero |= KnownZero2;
  1381. return;
  1382. case ISD::OR:
  1383. ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
  1384. ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
  1385. KnownZero2, KnownOne2, Depth+1);
  1386. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1387. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1388. // Output known-0 bits are only known if clear in both the LHS & RHS.
  1389. KnownZero &= KnownZero2;
  1390. // Output known-1 are known to be set if set in either the LHS | RHS.
  1391. KnownOne |= KnownOne2;
  1392. return;
  1393. case ISD::XOR: {
  1394. ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
  1395. ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
  1396. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1397. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1398. // Output known-0 bits are known if clear or set in both the LHS & RHS.
  1399. APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
  1400. // Output known-1 are known to be set if set in only one of the LHS, RHS.
  1401. KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
  1402. KnownZero = KnownZeroOut;
  1403. return;
  1404. }
  1405. case ISD::MUL: {
  1406. APInt Mask2 = APInt::getAllOnesValue(BitWidth);
  1407. ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
  1408. ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
  1409. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1410. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1411. // If low bits are zero in either operand, output low known-0 bits.
  1412. // Also compute a conserative estimate for high known-0 bits.
  1413. // More trickiness is possible, but this is sufficient for the
  1414. // interesting case of alignment computation.
  1415. KnownOne.clear();
  1416. unsigned TrailZ = KnownZero.countTrailingOnes() +
  1417. KnownZero2.countTrailingOnes();
  1418. unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
  1419. KnownZero2.countLeadingOnes(),
  1420. BitWidth) - BitWidth;
  1421. TrailZ = std::min(TrailZ, BitWidth);
  1422. LeadZ = std::min(LeadZ, BitWidth);
  1423. KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
  1424. APInt::getHighBitsSet(BitWidth, LeadZ);
  1425. KnownZero &= Mask;
  1426. return;
  1427. }
  1428. case ISD::UDIV: {
  1429. // For the purposes of computing leading zeros we can conservatively
  1430. // treat a udiv as a logical right shift by the power of 2 known to
  1431. // be less than the denominator.
  1432. APInt AllOnes = APInt::getAllOnesValue(BitWidth);
  1433. ComputeMaskedBits(Op.getOperand(0),
  1434. AllOnes, KnownZero2, KnownOne2, Depth+1);
  1435. unsigned LeadZ = KnownZero2.countLeadingOnes();
  1436. KnownOne2.clear();
  1437. KnownZero2.clear();
  1438. ComputeMaskedBits(Op.getOperand(1),
  1439. AllOnes, KnownZero2, KnownOne2, Depth+1);
  1440. unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
  1441. if (RHSUnknownLeadingOnes != BitWidth)
  1442. LeadZ = std::min(BitWidth,
  1443. LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
  1444. KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
  1445. return;
  1446. }
  1447. case ISD::SELECT:
  1448. ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
  1449. ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
  1450. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1451. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1452. // Only known if known in both the LHS and RHS.
  1453. KnownOne &= KnownOne2;
  1454. KnownZero &= KnownZero2;
  1455. return;
  1456. case ISD::SELECT_CC:
  1457. ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
  1458. ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
  1459. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1460. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1461. // Only known if known in both the LHS and RHS.
  1462. KnownOne &= KnownOne2;
  1463. KnownZero &= KnownZero2;
  1464. return;
  1465. case ISD::SADDO:
  1466. case ISD::UADDO:
  1467. case ISD::SSUBO:
  1468. case ISD::USUBO:
  1469. case ISD::SMULO:
  1470. case ISD::UMULO:
  1471. if (Op.getResNo() != 1)
  1472. return;
  1473. // The boolean result conforms to getBooleanContents. Fall through.
  1474. case ISD::SETCC:
  1475. // If we know the result of a setcc has the top bits zero, use this info.
  1476. if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
  1477. BitWidth > 1)
  1478. KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
  1479. return;
  1480. case ISD::SHL:
  1481. // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
  1482. if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1483. unsigned ShAmt = SA->getZExtValue();
  1484. // If the shift count is an invalid immediate, don't do anything.
  1485. if (ShAmt >= BitWidth)
  1486. return;
  1487. ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
  1488. KnownZero, KnownOne, Depth+1);
  1489. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1490. KnownZero <<= ShAmt;
  1491. KnownOne <<= ShAmt;
  1492. // low bits known zero.
  1493. KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
  1494. }
  1495. return;
  1496. case ISD::SRL:
  1497. // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
  1498. if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1499. unsigned ShAmt = SA->getZExtValue();
  1500. // If the shift count is an invalid immediate, don't do anything.
  1501. if (ShAmt >= BitWidth)
  1502. return;
  1503. ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
  1504. KnownZero, KnownOne, Depth+1);
  1505. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1506. KnownZero = KnownZero.lshr(ShAmt);
  1507. KnownOne = KnownOne.lshr(ShAmt);
  1508. APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
  1509. KnownZero |= HighBits; // High bits known zero.
  1510. }
  1511. return;
  1512. case ISD::SRA:
  1513. if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1514. unsigned ShAmt = SA->getZExtValue();
  1515. // If the shift count is an invalid immediate, don't do anything.
  1516. if (ShAmt >= BitWidth)
  1517. return;
  1518. APInt InDemandedMask = (Mask << ShAmt);
  1519. // If any of the demanded bits are produced by the sign extension, we also
  1520. // demand the input sign bit.
  1521. APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
  1522. if (HighBits.getBoolValue())
  1523. InDemandedMask |= APInt::getSignBit(BitWidth);
  1524. ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
  1525. Depth+1);
  1526. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1527. KnownZero = KnownZero.lshr(ShAmt);
  1528. KnownOne = KnownOne.lshr(ShAmt);
  1529. // Handle the sign bits.
  1530. APInt SignBit = APInt::getSignBit(BitWidth);
  1531. SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
  1532. if (KnownZero.intersects(SignBit)) {
  1533. KnownZero |= HighBits; // New bits are known zero.
  1534. } else if (KnownOne.intersects(SignBit)) {
  1535. KnownOne |= HighBits; // New bits are known one.
  1536. }
  1537. }
  1538. return;
  1539. case ISD::SIGN_EXTEND_INREG: {
  1540. EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
  1541. unsigned EBits = EVT.getScalarType().getSizeInBits();
  1542. // Sign extension. Compute the demanded bits in the result that are not
  1543. // present in the input.
  1544. APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
  1545. APInt InSignBit = APInt::getSignBit(EBits);
  1546. APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
  1547. // If the sign extended bits are demanded, we know that the sign
  1548. // bit is demanded.
  1549. InSignBit.zext(BitWidth);
  1550. if (NewBits.getBoolValue())
  1551. InputDemandedBits |= InSignBit;
  1552. ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
  1553. KnownZero, KnownOne, Depth+1);
  1554. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1555. // If the sign bit of the input is known set or clear, then we know the
  1556. // top bits of the result.
  1557. if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
  1558. KnownZero |= NewBits;
  1559. KnownOne &= ~NewBits;
  1560. } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
  1561. KnownOne |= NewBits;
  1562. KnownZero &= ~NewBits;
  1563. } else { // Input sign bit unknown
  1564. KnownZero &= ~NewBits;
  1565. KnownOne &= ~NewBits;
  1566. }
  1567. return;
  1568. }
  1569. case ISD::CTTZ:
  1570. case ISD::CTLZ:
  1571. case ISD::CTPOP: {
  1572. unsigned LowBits = Log2_32(BitWidth)+1;
  1573. KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
  1574. KnownOne.clear();
  1575. return;
  1576. }
  1577. case ISD::LOAD: {
  1578. if (ISD::isZEXTLoad(Op.getNode())) {
  1579. LoadSDNode *LD = cast<LoadSDNode>(Op);
  1580. EVT VT = LD->getMemoryVT();
  1581. unsigned MemBits = VT.getScalarType().getSizeInBits();
  1582. KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
  1583. }
  1584. return;
  1585. }
  1586. case ISD::ZERO_EXTEND: {
  1587. EVT InVT = Op.getOperand(0).getValueType();
  1588. unsigned InBits = InVT.getScalarType().getSizeInBits();
  1589. APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
  1590. APInt InMask = Mask;
  1591. InMask.trunc(InBits);
  1592. KnownZero.trunc(InBits);
  1593. KnownOne.trunc(InBits);
  1594. ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
  1595. KnownZero.zext(BitWidth);
  1596. KnownOne.zext(BitWidth);
  1597. KnownZero |= NewBits;
  1598. return;
  1599. }
  1600. case ISD::SIGN_EXTEND: {
  1601. EVT InVT = Op.getOperand(0).getValueType();
  1602. unsigned InBits = InVT.getScalarType().getSizeInBits();
  1603. APInt InSignBit = APInt::getSignBit(InBits);
  1604. APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
  1605. APInt InMask = Mask;
  1606. InMask.trunc(InBits);
  1607. // If any of the sign extended bits are demanded, we know that the sign
  1608. // bit is demanded. Temporarily set this bit in the mask for our callee.
  1609. if (NewBits.getBoolValue())
  1610. InMask |= InSignBit;
  1611. KnownZero.trunc(InBits);
  1612. KnownOne.trunc(InBits);
  1613. ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
  1614. // Note if the sign bit is known to be zero or one.
  1615. bool SignBitKnownZero = KnownZero.isNegative();
  1616. bool SignBitKnownOne = KnownOne.isNegative();
  1617. assert(!(SignBitKnownZero && SignBitKnownOne) &&
  1618. "Sign bit can't be known to be both zero and one!");
  1619. // If the sign bit wasn't actually demanded by our caller, we don't
  1620. // want it set in the KnownZero and KnownOne result values. Reset the
  1621. // mask and reapply it to the result values.
  1622. InMask = Mask;
  1623. InMask.trunc(InBits);
  1624. KnownZero &= InMask;
  1625. KnownOne &= InMask;
  1626. KnownZero.zext(BitWidth);
  1627. KnownOne.zext(BitWidth);
  1628. // If the sign bit is known zero or one, the top bits match.
  1629. if (SignBitKnownZero)
  1630. KnownZero |= NewBits;
  1631. else if (SignBitKnownOne)
  1632. KnownOne |= NewBits;
  1633. return;
  1634. }
  1635. case ISD::ANY_EXTEND: {
  1636. EVT InVT = Op.getOperand(0).getValueType();
  1637. unsigned InBits = InVT.getScalarType().getSizeInBits();
  1638. APInt InMask = Mask;
  1639. InMask.trunc(InBits);
  1640. KnownZero.trunc(InBits);
  1641. KnownOne.trunc(InBits);
  1642. ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
  1643. KnownZero.zext(BitWidth);
  1644. KnownOne.zext(BitWidth);
  1645. return;
  1646. }
  1647. case ISD::TRUNCATE: {
  1648. EVT InVT = Op.getOperand(0).getValueType();
  1649. unsigned InBits = InVT.getScalarType().getSizeInBits();
  1650. APInt InMask = Mask;
  1651. InMask.zext(InBits);
  1652. KnownZero.zext(InBits);
  1653. KnownOne.zext(InBits);
  1654. ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
  1655. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1656. KnownZero.trunc(BitWidth);
  1657. KnownOne.trunc(BitWidth);
  1658. break;
  1659. }
  1660. case ISD::AssertZext: {
  1661. EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
  1662. APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
  1663. ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
  1664. KnownOne, Depth+1);
  1665. KnownZero |= (~InMask) & Mask;
  1666. return;
  1667. }
  1668. case ISD::FGETSIGN:
  1669. // All bits are zero except the low bit.
  1670. KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
  1671. return;
  1672. case ISD::SUB: {
  1673. if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
  1674. // We know that the top bits of C-X are clear if X contains less bits
  1675. // than C (i.e. no wrap-around can happen). For example, 20-X is
  1676. // positive if we can prove that X is >= 0 and < 16.
  1677. if (CLHS->getAPIntValue().isNonNegative()) {
  1678. unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
  1679. // NLZ can't be BitWidth with no sign bit
  1680. APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
  1681. ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
  1682. Depth+1);
  1683. // If all of the MaskV bits are known to be zero, then we know the
  1684. // output top bits are zero, because we now know that the output is
  1685. // from [0-C].
  1686. if ((KnownZero2 & MaskV) == MaskV) {
  1687. unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
  1688. // Top bits known zero.
  1689. KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
  1690. }
  1691. }
  1692. }
  1693. }
  1694. // fall through
  1695. case ISD::ADD: {
  1696. // Output known-0 bits are known if clear or set in both the low clear bits
  1697. // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
  1698. // low 3 bits clear.
  1699. APInt Mask2 = APInt::getLowBitsSet(BitWidth,
  1700. BitWidth - Mask.countLeadingZeros());
  1701. ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
  1702. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1703. unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
  1704. ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
  1705. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1706. KnownZeroOut = std::min(KnownZeroOut,
  1707. KnownZero2.countTrailingOnes());
  1708. KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
  1709. return;
  1710. }
  1711. case ISD::SREM:
  1712. if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1713. const APInt &RA = Rem->getAPIntValue().abs();
  1714. if (RA.isPowerOf2()) {
  1715. APInt LowBits = RA - 1;
  1716. APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
  1717. ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
  1718. // The low bits of the first operand are unchanged by the srem.
  1719. KnownZero = KnownZero2 & LowBits;
  1720. KnownOne = KnownOne2 & LowBits;
  1721. // If the first operand is non-negative or has all low bits zero, then
  1722. // the upper bits are all zero.
  1723. if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
  1724. KnownZero |= ~LowBits;
  1725. // If the first operand is negative and not all low bits are zero, then
  1726. // the upper bits are all one.
  1727. if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
  1728. KnownOne |= ~LowBits;
  1729. KnownZero &= Mask;
  1730. KnownOne &= Mask;
  1731. assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
  1732. }
  1733. }
  1734. return;
  1735. case ISD::UREM: {
  1736. if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1737. const APInt &RA = Rem->getAPIntValue();
  1738. if (RA.isPowerOf2()) {
  1739. APInt LowBits = (RA - 1);
  1740. APInt Mask2 = LowBits & Mask;
  1741. KnownZero |= ~LowBits & Mask;
  1742. ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
  1743. assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
  1744. break;
  1745. }
  1746. }
  1747. // Since the result is less than or equal to either operand, any leading
  1748. // zero bits in either operand must also exist in the result.
  1749. APInt AllOnes = APInt::getAllOnesValue(BitWidth);
  1750. ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
  1751. Depth+1);
  1752. ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
  1753. Depth+1);
  1754. uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
  1755. KnownZero2.countLeadingOnes());
  1756. KnownOne.clear();
  1757. KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
  1758. return;
  1759. }
  1760. default:
  1761. // Allow the target to implement this method for its nodes.
  1762. if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
  1763. case ISD::INTRINSIC_WO_CHAIN:
  1764. case ISD::INTRINSIC_W_CHAIN:
  1765. case ISD::INTRINSIC_VOID:
  1766. TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this,
  1767. Depth);
  1768. }
  1769. return;
  1770. }
  1771. }
  1772. /// ComputeNumSignBits - Return the number of times the sign bit of the
  1773. /// register is replicated into the other bits. We know that at least 1 bit
  1774. /// is always equal to the sign bit (itself), but other cases can give us
  1775. /// information. For example, immediately after an "SRA X, 2", we know that
  1776. /// the top 3 bits are all equal to each other, so we return 3.
  1777. unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
  1778. EVT VT = Op.getValueType();
  1779. assert(VT.isInteger() && "Invalid VT!");
  1780. unsigned VTBits = VT.getScalarType().getSizeInBits();
  1781. unsigned Tmp, Tmp2;
  1782. unsigned FirstAnswer = 1;
  1783. if (Depth == 6)
  1784. return 1; // Limit search depth.
  1785. switch (Op.getOpcode()) {
  1786. default: break;
  1787. case ISD::AssertSext:
  1788. Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
  1789. return VTBits-Tmp+1;
  1790. case ISD::AssertZext:
  1791. Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
  1792. return VTBits-Tmp;
  1793. case ISD::Constant: {
  1794. const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
  1795. // If negative, return # leading ones.
  1796. if (Val.isNegative())
  1797. return Val.countLeadingOnes();
  1798. // Return # leading zeros.
  1799. return Val.countLeadingZeros();
  1800. }
  1801. case ISD::SIGN_EXTEND:
  1802. Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
  1803. return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
  1804. case ISD::SIGN_EXTEND_INREG:
  1805. // Max of the input and what this extends.
  1806. Tmp =
  1807. cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
  1808. Tmp = VTBits-Tmp+1;
  1809. Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1810. return std::max(Tmp, Tmp2);
  1811. case ISD::SRA:
  1812. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1813. // SRA X, C -> adds C sign bits.
  1814. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1815. Tmp += C->getZExtValue();
  1816. if (Tmp > VTBits) Tmp = VTBits;
  1817. }
  1818. return Tmp;
  1819. case ISD::SHL:
  1820. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1821. // shl destroys sign bits.
  1822. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1823. if (C->getZExtValue() >= VTBits || // Bad shift.
  1824. C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
  1825. return Tmp - C->getZExtValue();
  1826. }
  1827. break;
  1828. case ISD::AND:
  1829. case ISD::OR:
  1830. case ISD::XOR: // NOT is handled here.
  1831. // Logical binary ops preserve the number of sign bits at the worst.
  1832. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1833. if (Tmp != 1) {
  1834. Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
  1835. FirstAnswer = std::min(Tmp, Tmp2);
  1836. // We computed what we know about the sign bits as our first
  1837. // answer. Now proceed to the generic code that uses
  1838. // ComputeMaskedBits, and pick whichever answer is better.
  1839. }
  1840. break;
  1841. case ISD::SELECT:
  1842. Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
  1843. if (Tmp == 1) return 1; // Early out.
  1844. Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
  1845. return std::min(Tmp, Tmp2);
  1846. case ISD::SADDO:
  1847. case ISD::UADDO:
  1848. case ISD::SSUBO:
  1849. case ISD::USUBO:
  1850. case ISD::SMULO:
  1851. case ISD::UMULO:
  1852. if (Op.getResNo() != 1)
  1853. break;
  1854. // The boolean result conforms to getBooleanContents. Fall through.
  1855. case ISD::SETCC:
  1856. // If setcc returns 0/-1, all bits are sign bits.
  1857. if (TLI.getBooleanContents() ==
  1858. TargetLowering::ZeroOrNegativeOneBooleanContent)
  1859. return VTBits;
  1860. break;
  1861. case ISD::ROTL:
  1862. case ISD::ROTR:
  1863. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1864. unsigned RotAmt = C->getZExtValue() & (VTBits-1);
  1865. // Handle rotate right by N like a rotate left by 32-N.
  1866. if (Op.getOpcode() == ISD::ROTR)
  1867. RotAmt = (VTBits-RotAmt) & (VTBits-1);
  1868. // If we aren't rotating out all of the known-in sign bits, return the
  1869. // number that are left. This handles rotl(sext(x), 1) for example.
  1870. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1871. if (Tmp > RotAmt+1) return Tmp-RotAmt;
  1872. }
  1873. break;
  1874. case ISD::ADD:
  1875. // Add can have at most one carry bit. Thus we know that the output
  1876. // is, at worst, one more bit than the inputs.
  1877. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1878. if (Tmp == 1) return 1; // Early out.
  1879. // Special case decrementing a value (ADD X, -1):
  1880. if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
  1881. if (CRHS->isAllOnesValue()) {
  1882. APInt KnownZero, KnownOne;
  1883. APInt Mask = APInt::getAllOnesValue(VTBits);
  1884. ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
  1885. // If the input is known to be 0 or 1, the output is 0/-1, which is all
  1886. // sign bits set.
  1887. if ((KnownZero | APInt(VTBits, 1)) == Mask)
  1888. return VTBits;
  1889. // If we are subtracting one from a positive number, there is no carry
  1890. // out of the result.
  1891. if (KnownZero.isNegative())
  1892. return Tmp;
  1893. }
  1894. Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
  1895. if (Tmp2 == 1) return 1;
  1896. return std::min(Tmp, Tmp2)-1;
  1897. break;
  1898. case ISD::SUB:
  1899. Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
  1900. if (Tmp2 == 1) return 1;
  1901. // Handle NEG.
  1902. if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
  1903. if (CLHS->isNullValue()) {
  1904. APInt KnownZero, KnownOne;
  1905. APInt Mask = APInt::getAllOnesValue(VTBits);
  1906. ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
  1907. // If the input is known to be 0 or 1, the output is 0/-1, which is all
  1908. // sign bits set.
  1909. if ((KnownZero | APInt(VTBits, 1)) == Mask)
  1910. return VTBits;
  1911. // If the input is known to be positive (the sign bit is known clear),
  1912. // the output of the NEG has the same number of sign bits as the input.
  1913. if (KnownZero.isNegative())
  1914. return Tmp2;
  1915. // Otherwise, we treat this like a SUB.
  1916. }
  1917. // Sub can have at most one carry bit. Thus we know that the output
  1918. // is, at worst, one more bit than the inputs.
  1919. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1920. if (Tmp == 1) return 1; // Early out.
  1921. return std::min(Tmp, Tmp2)-1;
  1922. break;
  1923. case ISD::TRUNCATE:
  1924. // FIXME: it's tricky to do anything useful for this, but it is an important
  1925. // case for targets like X86.
  1926. break;
  1927. }
  1928. // Handle LOADX separately here. EXTLOAD case will fallthrough.
  1929. if (Op.getOpcode() == ISD::LOAD) {
  1930. LoadSDNode *LD = cast<LoadSDNode>(Op);
  1931. unsigned ExtType = LD->getExtensionType();
  1932. switch (ExtType) {
  1933. default: break;
  1934. case ISD::SEXTLOAD: // '17' bits known
  1935. Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
  1936. return VTBits-Tmp+1;
  1937. case ISD::ZEXTLOAD: // '16' bits known
  1938. Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
  1939. return VTBits-Tmp;
  1940. }
  1941. }
  1942. // Allow the target to implement this method for its nodes.
  1943. if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
  1944. Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
  1945. Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
  1946. Op.getOpcode() == ISD::INTRINSIC_VOID) {
  1947. unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
  1948. if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
  1949. }
  1950. // Finally, if we can prove that the top bits of the result are 0's or 1's,
  1951. // use this information.
  1952. APInt KnownZero, KnownOne;
  1953. APInt Mask = APInt::getAllOnesValue(VTBits);
  1954. ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
  1955. if (KnownZero.isNegative()) { // sign bit is 0
  1956. Mask = KnownZero;
  1957. } else if (KnownOne.isNegative()) { // sign bit is 1;
  1958. Mask = KnownOne;
  1959. } else {
  1960. // Nothing known.
  1961. return FirstAnswer;
  1962. }
  1963. // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
  1964. // the number of identical bits in the top of the input value.
  1965. Mask = ~Mask;
  1966. Mask <<= Mask.getBitWidth()-VTBits;
  1967. // Return # leading zeros. We use 'min' here in case Val was zero before
  1968. // shifting. We don't want to return '64' as for an i32 "0".
  1969. return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
  1970. }
  1971. bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
  1972. // If we're told that NaNs won't happen, assume they won't.
  1973. if (FiniteOnlyFPMath())
  1974. return true;
  1975. // If the value is a constant, we can obviously see if it is a NaN or not.
  1976. if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
  1977. return !C->getValueAPF().isNaN();
  1978. // TODO: Recognize more cases here.
  1979. return false;
  1980. }
  1981. bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
  1982. // If the value is a constant, we can obviously see if it is a zero or not.
  1983. if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
  1984. return !C->isZero();
  1985. // TODO: Recognize more cases here.
  1986. return false;
  1987. }
  1988. bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
  1989. // Check the obvious case.
  1990. if (A == B) return true;
  1991. // For for negative and positive zero.
  1992. if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
  1993. if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
  1994. if (CA->isZero() && CB->isZero()) return true;
  1995. // Otherwise they may not be equal.
  1996. return false;
  1997. }
  1998. bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
  1999. GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
  2000. if (!GA) return false;
  2001. if (GA->getOffset() != 0) return false;
  2002. const GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
  2003. if (!GV) return false;
  2004. return MF->getMMI().hasDebugInfo();
  2005. }
  2006. /// getShuffleScalarElt - Returns the scalar element that will make up the ith
  2007. /// element of the result of the vector shuffle.
  2008. SDValue SelectionDAG::getShuffleScalarElt(const ShuffleVectorSDNode *N,
  2009. unsigned i) {
  2010. EVT VT = N->getValueType(0);
  2011. if (N->getMaskElt(i) < 0)
  2012. return getUNDEF(VT.getVectorElementType());
  2013. unsigned Index = N->getMaskElt(i);
  2014. unsigned NumElems = VT.getVectorNumElements();
  2015. SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
  2016. Index %= NumElems;
  2017. if (V.getOpcode() == ISD::BIT_CONVERT) {
  2018. V = V.getOperand(0);
  2019. EVT VVT = V.getValueType();
  2020. if (!VVT.isVector() || VVT.getVectorNumElements() != (unsigned)NumElems)
  2021. return SDValue();
  2022. }
  2023. if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
  2024. return (Index == 0) ? V.getOperand(0)
  2025. : getUNDEF(VT.getVectorElementType());
  2026. if (V.getOpcode() == ISD::BUILD_VECTOR)
  2027. return V.getOperand(Index);
  2028. if (const ShuffleVectorSDNode *SVN = dyn_cast<ShuffleVectorSDNode>(V))
  2029. return getShuffleScalarElt(SVN, Index);
  2030. return SDValue();
  2031. }
  2032. /// getNode - Gets or creates the specified node.
  2033. ///
  2034. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
  2035. FoldingSetNodeID ID;
  2036. AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
  2037. void *IP = 0;
  2038. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  2039. return SDValue(E, 0);
  2040. SDNode *N = new (NodeAllocator) SDNode(Opcode, DL, getVTList(VT));
  2041. CSEMap.InsertNode(N, IP);
  2042. AllNodes.push_back(N);
  2043. #ifndef NDEBUG
  2044. VerifyNode(N);
  2045. #endif
  2046. return SDValue(N, 0);
  2047. }
  2048. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
  2049. EVT VT, SDValue Operand) {
  2050. // Constant fold unary operations with an integer constant operand.
  2051. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
  2052. const APInt &Val = C->getAPIntValue();
  2053. switch (Opcode) {
  2054. default: break;
  2055. case ISD::SIGN_EXTEND:
  2056. return getConstant(APInt(Val).sextOrTrunc(VT.getSizeInBits()), VT);
  2057. case ISD::ANY_EXTEND:
  2058. case ISD::ZERO_EXTEND:
  2059. case ISD::TRUNCATE:
  2060. return getConstant(APInt(Val).zextOrTrunc(VT.getSizeInBits()), VT);
  2061. case ISD::UINT_TO_FP:
  2062. case ISD::SINT_TO_FP: {
  2063. const uint64_t zero[] = {0, 0};
  2064. // No compile time operations on ppcf128.
  2065. if (VT == MVT::ppcf128) break;
  2066. APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
  2067. (void)apf.convertFromAPInt(Val,
  2068. Opcode==ISD::SINT_TO_FP,
  2069. APFloat::rmNearestTiesToEven);
  2070. return getConstantFP(apf, VT);
  2071. }
  2072. case ISD::BIT_CONVERT:
  2073. if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
  2074. return getConstantFP(Val.bitsToFloat(), VT);
  2075. else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
  2076. return getConstantFP(Val.bitsToDouble(), VT);
  2077. break;
  2078. case ISD::BSWAP:
  2079. return getConstant(Val.byteSwap(), VT);
  2080. case ISD::CTPOP:
  2081. return getConstant(Val.countPopulation(), VT);
  2082. case ISD::CTLZ:
  2083. return getConstant(Val.countLeadingZeros(), VT);
  2084. case ISD::CTTZ:
  2085. return getConstant(Val.countTrailingZeros(), VT);
  2086. }
  2087. }
  2088. // Constant fold unary operations with a floating point constant operand.
  2089. if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
  2090. APFloat V = C->getValueAPF(); // make copy
  2091. if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
  2092. switch (Opcode) {
  2093. case ISD::FNEG:
  2094. V.changeSign();
  2095. return getConstantFP(V, VT);
  2096. case ISD::FABS:
  2097. V.clearSign();
  2098. return getConstantFP(V, VT);
  2099. case ISD::FP_ROUND:
  2100. case ISD::FP_EXTEND: {
  2101. bool ignored;
  2102. // This can return overflow, underflow, or inexact; we don't care.
  2103. // FIXME need to be more flexible about rounding mode.
  2104. (void)V.convert(*EVTToAPFloatSemantics(VT),
  2105. APFloat::rmNearestTiesToEven, &ignored);
  2106. return getConstantFP(V, VT);
  2107. }
  2108. case ISD::FP_TO_SINT:
  2109. case ISD::FP_TO_UINT: {
  2110. integerPart x[2];
  2111. bool ignored;
  2112. assert(integerPartWidth >= 64);
  2113. // FIXME need to be more flexible about rounding mode.
  2114. APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
  2115. Opcode==ISD::FP_TO_SINT,
  2116. APFloat::rmTowardZero, &ignored);
  2117. if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
  2118. break;
  2119. APInt api(VT.getSizeInBits(), 2, x);
  2120. return getConstant(api, VT);
  2121. }
  2122. case ISD::BIT_CONVERT:
  2123. if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
  2124. return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
  2125. else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
  2126. return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
  2127. break;
  2128. }
  2129. }
  2130. }
  2131. unsigned OpOpcode = Operand.getNode()->getOpcode();
  2132. switch (Opcode) {
  2133. case ISD::TokenFactor:
  2134. case ISD::MERGE_VALUES:
  2135. case ISD::CONCAT_VECTORS:
  2136. return Operand; // Factor, merge or concat of one node? No need.
  2137. case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
  2138. case ISD::FP_EXTEND:
  2139. assert(VT.isFloatingPoint() &&
  2140. Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
  2141. if (Operand.getValueType() == VT) return Operand; // noop conversion.
  2142. assert((!VT.isVector() ||
  2143. VT.getVectorNumElements() ==
  2144. Operand.getValueType().getVectorNumElements()) &&
  2145. "Vector element count mismatch!");
  2146. if (Operand.getOpcode() == ISD::UNDEF)
  2147. return getUNDEF(VT);
  2148. break;
  2149. case ISD::SIGN_EXTEND:
  2150. assert(VT.isInteger() && Operand.getValueType().isInteger() &&
  2151. "Invalid SIGN_EXTEND!");
  2152. if (Operand.getValueType() == VT) return Operand; // noop extension
  2153. assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
  2154. "Invalid sext node, dst < src!");
  2155. assert((!VT.isVector() ||
  2156. VT.getVectorNumElements() ==
  2157. Operand.getValueType().getVectorNumElements()) &&
  2158. "Vector element count mismatch!");
  2159. if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
  2160. return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
  2161. break;
  2162. case ISD::ZERO_EXTEND:
  2163. assert(VT.isInteger() && Operand.getValueType().isInteger() &&
  2164. "Invalid ZERO_EXTEND!");
  2165. if (Operand.getValueType() == VT) return Operand; // noop extension
  2166. assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
  2167. "Invalid zext node, dst < src!");
  2168. assert((!VT.isVector() ||
  2169. VT.getVectorNumElements() ==
  2170. Operand.getValueType().getVectorNumElements()) &&
  2171. "Vector element count mismatch!");
  2172. if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
  2173. return getNode(ISD::ZERO_EXTEND, DL, VT,
  2174. Operand.getNode()->getOperand(0));
  2175. break;
  2176. case ISD::ANY_EXTEND:
  2177. assert(VT.isInteger() && Operand.getValueType().isInteger() &&
  2178. "Invalid ANY_EXTEND!");
  2179. if (Operand.getValueType() == VT) return Operand; // noop extension
  2180. assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
  2181. "Invalid anyext node, dst < src!");
  2182. assert((!VT.isVector() ||
  2183. VT.getVectorNumElements() ==
  2184. Operand.getValueType().getVectorNumElements()) &&
  2185. "Vector element count mismatch!");
  2186. if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
  2187. OpOpcode == ISD::ANY_EXTEND)
  2188. // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
  2189. return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
  2190. // (ext (trunx x)) -> x
  2191. if (OpOpcode == ISD::TRUNCATE) {
  2192. SDValue OpOp = Operand.getNode()->getOperand(0);
  2193. if (OpOp.getValueType() == VT)
  2194. return OpOp;
  2195. }
  2196. break;
  2197. case ISD::TRUNCATE:
  2198. assert(VT.isInteger() && Operand.getValueType().isInteger() &&
  2199. "Invalid TRUNCATE!");
  2200. if (Operand.getValueType() == VT) return Operand; // noop truncate
  2201. assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
  2202. "Invalid truncate node, src < dst!");
  2203. assert((!VT.isVector() ||
  2204. VT.getVectorNumElements() ==
  2205. Operand.getValueType().getVectorNumElements()) &&
  2206. "Vector element count mismatch!");
  2207. if (OpOpcode == ISD::TRUNCATE)
  2208. return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
  2209. else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
  2210. OpOpcode == ISD::ANY_EXTEND) {
  2211. // If the source is smaller than the dest, we still need an extend.
  2212. if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
  2213. .bitsLT(VT.getScalarType()))
  2214. return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
  2215. else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
  2216. return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
  2217. else
  2218. return Operand.getNode()->getOperand(0);
  2219. }
  2220. break;
  2221. case ISD::BIT_CONVERT:
  2222. // Basic sanity checking.
  2223. assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
  2224. && "Cannot BIT_CONVERT between types of different sizes!");
  2225. if (VT == Operand.getValueType()) return Operand; // noop conversion.
  2226. if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
  2227. return getNode(ISD::BIT_CONVERT, DL, VT, Operand.getOperand(0));
  2228. if (OpOpcode == ISD::UNDEF)
  2229. return getUNDEF(VT);
  2230. break;
  2231. case ISD::SCALAR_TO_VECTOR:
  2232. assert(VT.isVector() && !Operand.getValueType().isVector() &&
  2233. (VT.getVectorElementType() == Operand.getValueType() ||
  2234. (VT.getVectorElementType().isInteger() &&
  2235. Operand.getValueType().isInteger() &&
  2236. VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
  2237. "Illegal SCALAR_TO_VECTOR node!");
  2238. if (OpOpcode == ISD::UNDEF)
  2239. return getUNDEF(VT);
  2240. // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
  2241. if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
  2242. isa<ConstantSDNode>(Operand.getOperand(1)) &&
  2243. Operand.getConstantOperandVal(1) == 0 &&
  2244. Operand.getOperand(0).getValueType() == VT)
  2245. return Operand.getOperand(0);
  2246. break;
  2247. case ISD::FNEG:
  2248. // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
  2249. if (UnsafeFPMath && OpOpcode == ISD::FSUB)
  2250. return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
  2251. Operand.getNode()->getOperand(0));
  2252. if (OpOpcode == ISD::FNEG) // --X -> X
  2253. return Operand.getNode()->getOperand(0);
  2254. break;
  2255. case ISD::FABS:
  2256. if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
  2257. return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
  2258. break;
  2259. }
  2260. SDNode *N;
  2261. SDVTList VTs = getVTList(VT);
  2262. if (VT != MVT::Flag) { // Don't CSE flag producing nodes
  2263. FoldingSetNodeID ID;
  2264. SDValue Ops[1] = { Operand };
  2265. AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
  2266. void *IP = 0;
  2267. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  2268. return SDValue(E, 0);
  2269. N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
  2270. CSEMap.InsertNode(N, IP);
  2271. } else {
  2272. N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
  2273. }
  2274. AllNodes.push_back(N);
  2275. #ifndef NDEBUG
  2276. VerifyNode(N);
  2277. #endif
  2278. return SDValue(N, 0);
  2279. }
  2280. SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
  2281. EVT VT,
  2282. ConstantSDNode *Cst1,
  2283. ConstantSDNode *Cst2) {
  2284. const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
  2285. switch (Opcode) {
  2286. case ISD::ADD: return getConstant(C1 + C2, VT);
  2287. case ISD::SUB: return getConstant(C1 - C2, VT);
  2288. case ISD::MUL: return getConstant(C1 * C2, VT);
  2289. case ISD::UDIV:
  2290. if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
  2291. break;
  2292. case ISD::UREM:
  2293. if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
  2294. break;
  2295. case ISD::SDIV:
  2296. if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
  2297. break;
  2298. case ISD::SREM:
  2299. if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
  2300. break;
  2301. case ISD::AND: return getConstant(C1 & C2, VT);
  2302. case ISD::OR: return getConstant(C1 | C2, VT);
  2303. case ISD::XOR: return getConstant(C1 ^ C2, VT);
  2304. case ISD::SHL: return getConstant(C1 << C2, VT);
  2305. case ISD::SRL: return getConstant(C1.lshr(C2), VT);
  2306. case ISD::SRA: return getConstant(C1.ashr(C2), VT);
  2307. case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
  2308. case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
  2309. default: break;
  2310. }
  2311. return SDValue();
  2312. }
  2313. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
  2314. SDValue N1, SDValue N2) {
  2315. ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
  2316. ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
  2317. switch (Opcode) {
  2318. default: break;
  2319. case ISD::TokenFactor:
  2320. assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
  2321. N2.getValueType() == MVT::Other && "Invalid token factor!");
  2322. // Fold trivial token factors.
  2323. if (N1.getOpcode() == ISD::EntryToken) return N2;
  2324. if (N2.getOpcode() == ISD::EntryToken) return N1;
  2325. if (N1 == N2) return N1;
  2326. break;
  2327. case ISD::CONCAT_VECTORS:
  2328. // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
  2329. // one big BUILD_VECTOR.
  2330. if (N1.getOpcode() == ISD::BUILD_VECTOR &&
  2331. N2.getOpcode() == ISD::BUILD_VECTOR) {
  2332. SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
  2333. Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
  2334. return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
  2335. }
  2336. break;
  2337. case ISD::AND:
  2338. assert(VT.isInteger() && "This operator does not apply to FP types!");
  2339. assert(N1.getValueType() == N2.getValueType() &&
  2340. N1.getValueType() == VT && "Binary operator types must match!");
  2341. // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
  2342. // worth handling here.
  2343. if (N2C && N2C->isNullValue())
  2344. return N2;
  2345. if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
  2346. return N1;
  2347. break;
  2348. case ISD::OR:
  2349. case ISD::XOR:
  2350. case ISD::ADD:
  2351. case ISD::SUB:
  2352. assert(VT.isInteger() && "This operator does not apply to FP types!");
  2353. assert(N1.getValueType() == N2.getValueType() &&
  2354. N1.getValueType() == VT && "Binary operator types must match!");
  2355. // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
  2356. // it's worth handling here.
  2357. if (N2C && N2C->isNullValue())
  2358. return N1;
  2359. break;
  2360. case ISD::UDIV:
  2361. case ISD::UREM:
  2362. case ISD::MULHU:
  2363. case ISD::MULHS:
  2364. case ISD::MUL:
  2365. case ISD::SDIV:
  2366. case ISD::SREM:
  2367. assert(VT.isInteger() && "This operator does not apply to FP types!");
  2368. assert(N1.getValueType() == N2.getValueType() &&
  2369. N1.getValueType() == VT && "Binary operator types must match!");
  2370. break;
  2371. case ISD::FADD:
  2372. case ISD::FSUB:
  2373. case ISD::FMUL:
  2374. case ISD::FDIV:
  2375. case ISD::FREM:
  2376. if (UnsafeFPMath) {
  2377. if (Opcode == ISD::FADD) {
  2378. // 0+x --> x
  2379. if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
  2380. if (CFP->getValueAPF().isZero())
  2381. return N2;
  2382. // x+0 --> x
  2383. if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
  2384. if (CFP->getValueAPF().isZero())
  2385. return N1;
  2386. } else if (Opcode == ISD::FSUB) {
  2387. // x-0 --> x
  2388. if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
  2389. if (CFP->getValueAPF().isZero())
  2390. return N1;
  2391. }
  2392. }
  2393. assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
  2394. assert(N1.getValueType() == N2.getValueType() &&
  2395. N1.getValueType() == VT && "Binary operator types must match!");
  2396. break;
  2397. case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
  2398. assert(N1.getValueType() == VT &&
  2399. N1.getValueType().isFloatingPoint() &&
  2400. N2.getValueType().isFloatingPoint() &&
  2401. "Invalid FCOPYSIGN!");
  2402. break;
  2403. case ISD::SHL:
  2404. case ISD::SRA:
  2405. case ISD::SRL:
  2406. case ISD::ROTL:
  2407. case ISD::ROTR:
  2408. assert(VT == N1.getValueType() &&
  2409. "Shift operators return type must be the same as their first arg");
  2410. assert(VT.isInteger() && N2.getValueType().isInteger() &&
  2411. "Shifts only work on integers");
  2412. // Always fold shifts of i1 values so the code generator doesn't need to
  2413. // handle them. Since we know the size of the shift has to be less than the
  2414. // size of the value, the shift/rotate count is guaranteed to be zero.
  2415. if (VT == MVT::i1)
  2416. return N1;
  2417. if (N2C && N2C->isNullValue())
  2418. return N1;
  2419. break;
  2420. case ISD::FP_ROUND_INREG: {
  2421. EVT EVT = cast<VTSDNode>(N2)->getVT();
  2422. assert(VT == N1.getValueType() && "Not an inreg round!");
  2423. assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
  2424. "Cannot FP_ROUND_INREG integer types");
  2425. assert(EVT.isVector() == VT.isVector() &&
  2426. "FP_ROUND_INREG type should be vector iff the operand "
  2427. "type is vector!");
  2428. assert((!EVT.isVector() ||
  2429. EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
  2430. "Vector element counts must match in FP_ROUND_INREG");
  2431. assert(EVT.bitsLE(VT) && "Not rounding down!");
  2432. if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
  2433. break;
  2434. }
  2435. case ISD::FP_ROUND:
  2436. assert(VT.isFloatingPoint() &&
  2437. N1.getValueType().isFloatingPoint() &&
  2438. VT.bitsLE(N1.getValueType()) &&
  2439. isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
  2440. if (N1.getValueType() == VT) return N1; // noop conversion.
  2441. break;
  2442. case ISD::AssertSext:
  2443. case ISD::AssertZext: {
  2444. EVT EVT = cast<VTSDNode>(N2)->getVT();
  2445. assert(VT == N1.getValueType() && "Not an inreg extend!");
  2446. assert(VT.isInteger() && EVT.isInteger() &&
  2447. "Cannot *_EXTEND_INREG FP types");
  2448. assert(!EVT.isVector() &&
  2449. "AssertSExt/AssertZExt type should be the vector element type "
  2450. "rather than the vector type!");
  2451. assert(EVT.bitsLE(VT) && "Not extending!");
  2452. if (VT == EVT) return N1; // noop assertion.
  2453. break;
  2454. }
  2455. case ISD::SIGN_EXTEND_INREG: {
  2456. EVT EVT = cast<VTSDNode>(N2)->getVT();
  2457. assert(VT == N1.getValueType() && "Not an inreg extend!");
  2458. assert(VT.isInteger() && EVT.isInteger() &&
  2459. "Cannot *_EXTEND_INREG FP types");
  2460. assert(EVT.isVector() == VT.isVector() &&
  2461. "SIGN_EXTEND_INREG type should be vector iff the operand "
  2462. "type is vector!");
  2463. assert((!EVT.isVector() ||
  2464. EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
  2465. "Vector element counts must match in SIGN_EXTEND_INREG");
  2466. assert(EVT.bitsLE(VT) && "Not extending!");
  2467. if (EVT == VT) return N1; // Not actually extending
  2468. if (N1C) {
  2469. APInt Val = N1C->getAPIntValue();
  2470. unsigned FromBits = EVT.getScalarType().getSizeInBits();
  2471. Val <<= Val.getBitWidth()-FromBits;
  2472. Val = Val.ashr(Val.getBitWidth()-FromBits);
  2473. return getConstant(Val, VT);
  2474. }
  2475. break;
  2476. }
  2477. case ISD::EXTRACT_VECTOR_ELT:
  2478. // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
  2479. if (N1.getOpcode() == ISD::UNDEF)
  2480. return getUNDEF(VT);
  2481. // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
  2482. // expanding copies of large vectors from registers.
  2483. if (N2C &&
  2484. N1.getOpcode() == ISD::CONCAT_VECTORS &&
  2485. N1.getNumOperands() > 0) {
  2486. unsigned Factor =
  2487. N1.getOperand(0).getValueType().getVectorNumElements();
  2488. return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
  2489. N1.getOperand(N2C->getZExtValue() / Factor),
  2490. getConstant(N2C->getZExtValue() % Factor,
  2491. N2.getValueType()));
  2492. }
  2493. // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
  2494. // expanding large vector constants.
  2495. if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
  2496. SDValue Elt = N1.getOperand(N2C->getZExtValue());
  2497. EVT VEltTy = N1.getValueType().getVectorElementType();
  2498. if (Elt.getValueType() != VEltTy) {
  2499. // If the vector element type is not legal, the BUILD_VECTOR operands
  2500. // are promoted and implicitly truncated. Make that explicit here.
  2501. Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt);
  2502. }
  2503. if (VT != VEltTy) {
  2504. // If the vector element type is not legal, the EXTRACT_VECTOR_ELT
  2505. // result is implicitly extended.
  2506. Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt);
  2507. }
  2508. return Elt;
  2509. }
  2510. // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
  2511. // operations are lowered to scalars.
  2512. if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
  2513. // If the indices are the same, return the inserted element else
  2514. // if the indices are known different, extract the element from
  2515. // the original vector.
  2516. SDValue N1Op2 = N1.getOperand(2);
  2517. ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
  2518. if (N1Op2C && N2C) {
  2519. if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
  2520. if (VT == N1.getOperand(1).getValueType())
  2521. return N1.getOperand(1);
  2522. else
  2523. return getSExtOrTrunc(N1.getOperand(1), DL, VT);
  2524. }
  2525. return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
  2526. }
  2527. }
  2528. break;
  2529. case ISD::EXTRACT_ELEMENT:
  2530. assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
  2531. assert(!N1.getValueType().isVector() && !VT.isVector() &&
  2532. (N1.getValueType().isInteger() == VT.isInteger()) &&
  2533. "Wrong types for EXTRACT_ELEMENT!");
  2534. // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
  2535. // 64-bit integers into 32-bit parts. Instead of building the extract of
  2536. // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
  2537. if (N1.getOpcode() == ISD::BUILD_PAIR)
  2538. return N1.getOperand(N2C->getZExtValue());
  2539. // EXTRACT_ELEMENT of a constant int is also very common.
  2540. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
  2541. unsigned ElementSize = VT.getSizeInBits();
  2542. unsigned Shift = ElementSize * N2C->getZExtValue();
  2543. APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
  2544. return getConstant(ShiftedVal.trunc(ElementSize), VT);
  2545. }
  2546. break;
  2547. case ISD::EXTRACT_SUBVECTOR:
  2548. if (N1.getValueType() == VT) // Trivial extraction.
  2549. return N1;
  2550. break;
  2551. }
  2552. if (N1C) {
  2553. if (N2C) {
  2554. SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
  2555. if (SV.getNode()) return SV;
  2556. } else { // Cannonicalize constant to RHS if commutative
  2557. if (isCommutativeBinOp(Opcode)) {
  2558. std::swap(N1C, N2C);
  2559. std::swap(N1, N2);
  2560. }
  2561. }
  2562. }
  2563. // Constant fold FP operations.
  2564. ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
  2565. ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
  2566. if (N1CFP) {
  2567. if (!N2CFP && isCommutativeBinOp(Opcode)) {
  2568. // Cannonicalize constant to RHS if commutative
  2569. std::swap(N1CFP, N2CFP);
  2570. std::swap(N1, N2);
  2571. } else if (N2CFP && VT != MVT::ppcf128) {
  2572. APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
  2573. APFloat::opStatus s;
  2574. switch (Opcode) {
  2575. case ISD::FADD:
  2576. s = V1.add(V2, APFloat::rmNearestTiesToEven);
  2577. if (s != APFloat::opInvalidOp)
  2578. return getConstantFP(V1, VT);
  2579. break;
  2580. case ISD::FSUB:
  2581. s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
  2582. if (s!=APFloat::opInvalidOp)
  2583. return getConstantFP(V1, VT);
  2584. break;
  2585. case ISD::FMUL:
  2586. s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
  2587. if (s!=APFloat::opInvalidOp)
  2588. return getConstantFP(V1, VT);
  2589. break;
  2590. case ISD::FDIV:
  2591. s = V1.divide(V2, APFloat::rmNearestTiesToEven);
  2592. if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
  2593. return getConstantFP(V1, VT);
  2594. break;
  2595. case ISD::FREM :
  2596. s = V1.mod(V2, APFloat::rmNearestTiesToEven);
  2597. if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
  2598. return getConstantFP(V1, VT);
  2599. break;
  2600. case ISD::FCOPYSIGN:
  2601. V1.copySign(V2);
  2602. return getConstantFP(V1, VT);
  2603. default: break;
  2604. }
  2605. }
  2606. }
  2607. // Canonicalize an UNDEF to the RHS, even over a constant.
  2608. if (N1.getOpcode() == ISD::UNDEF) {
  2609. if (isCommutativeBinOp(Opcode)) {
  2610. std::swap(N1, N2);
  2611. } else {
  2612. switch (Opcode) {
  2613. case ISD::FP_ROUND_INREG:
  2614. case ISD::SIGN_EXTEND_INREG:
  2615. case ISD::SUB:
  2616. case ISD::FSUB:
  2617. case ISD::FDIV:
  2618. case ISD::FREM:
  2619. case ISD::SRA:
  2620. return N1; // fold op(undef, arg2) -> undef
  2621. case ISD::UDIV:
  2622. case ISD::SDIV:
  2623. case ISD::UREM:
  2624. case ISD::SREM:
  2625. case ISD::SRL:
  2626. case ISD::SHL:
  2627. if (!VT.isVector())
  2628. return getConstant(0, VT); // fold op(undef, arg2) -> 0
  2629. // For vectors, we can't easily build an all zero vector, just return
  2630. // the LHS.
  2631. return N2;
  2632. }
  2633. }
  2634. }
  2635. // Fold a bunch of operators when the RHS is undef.
  2636. if (N2.getOpcode() == ISD::UNDEF) {
  2637. switch (Opcode) {
  2638. case ISD::XOR:
  2639. if (N1.getOpcode() == ISD::UNDEF)
  2640. // Handle undef ^ undef -> 0 special case. This is a common
  2641. // idiom (misuse).
  2642. return getConstant(0, VT);
  2643. // fallthrough
  2644. case ISD::ADD:
  2645. case ISD::ADDC:
  2646. case ISD::ADDE:
  2647. case ISD::SUB:
  2648. case ISD::UDIV:
  2649. case ISD::SDIV:
  2650. case ISD::UREM:
  2651. case ISD::SREM:
  2652. return N2; // fold op(arg1, undef) -> undef
  2653. case ISD::FADD:
  2654. case ISD::FSUB:
  2655. case ISD::FMUL:
  2656. case ISD::FDIV:
  2657. case ISD::FREM:
  2658. if (UnsafeFPMath)
  2659. return N2;
  2660. break;
  2661. case ISD::MUL:
  2662. case ISD::AND:
  2663. case ISD::SRL:
  2664. case ISD::SHL:
  2665. if (!VT.isVector())
  2666. return getConstant(0, VT); // fold op(arg1, undef) -> 0
  2667. // For vectors, we can't easily build an all zero vector, just return
  2668. // the LHS.
  2669. return N1;
  2670. case ISD::OR:
  2671. if (!VT.isVector())
  2672. return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
  2673. // For vectors, we can't easily build an all one vector, just return
  2674. // the LHS.
  2675. return N1;
  2676. case ISD::SRA:
  2677. return N1;
  2678. }
  2679. }
  2680. // Memoize this node if possible.
  2681. SDNode *N;
  2682. SDVTList VTs = getVTList(VT);
  2683. if (VT != MVT::Flag) {
  2684. SDValue Ops[] = { N1, N2 };
  2685. FoldingSetNodeID ID;
  2686. AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
  2687. void *IP = 0;
  2688. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  2689. return SDValue(E, 0);
  2690. N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
  2691. CSEMap.InsertNode(N, IP);
  2692. } else {
  2693. N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
  2694. }
  2695. AllNodes.push_back(N);
  2696. #ifndef NDEBUG
  2697. VerifyNode(N);
  2698. #endif
  2699. return SDValue(N, 0);
  2700. }
  2701. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
  2702. SDValue N1, SDValue N2, SDValue N3) {
  2703. // Perform various simplifications.
  2704. ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
  2705. switch (Opcode) {
  2706. case ISD::CONCAT_VECTORS:
  2707. // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
  2708. // one big BUILD_VECTOR.
  2709. if (N1.getOpcode() == ISD::BUILD_VECTOR &&
  2710. N2.getOpcode() == ISD::BUILD_VECTOR &&
  2711. N3.getOpcode() == ISD::BUILD_VECTOR) {
  2712. SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
  2713. Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
  2714. Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
  2715. return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
  2716. }
  2717. break;
  2718. case ISD::SETCC: {
  2719. // Use FoldSetCC to simplify SETCC's.
  2720. SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
  2721. if (Simp.getNode()) return Simp;
  2722. break;
  2723. }
  2724. case ISD::SELECT:
  2725. if (N1C) {
  2726. if (N1C->getZExtValue())
  2727. return N2; // select true, X, Y -> X
  2728. else
  2729. return N3; // select false, X, Y -> Y
  2730. }
  2731. if (N2 == N3) return N2; // select C, X, X -> X
  2732. break;
  2733. case ISD::VECTOR_SHUFFLE:
  2734. llvm_unreachable("should use getVectorShuffle constructor!");
  2735. break;
  2736. case ISD::BIT_CONVERT:
  2737. // Fold bit_convert nodes from a type to themselves.
  2738. if (N1.getValueType() == VT)
  2739. return N1;
  2740. break;
  2741. }
  2742. // Memoize node if it doesn't produce a flag.
  2743. SDNode *N;
  2744. SDVTList VTs = getVTList(VT);
  2745. if (VT != MVT::Flag) {
  2746. SDValue Ops[] = { N1, N2, N3 };
  2747. FoldingSetNodeID ID;
  2748. AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
  2749. void *IP = 0;
  2750. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  2751. return SDValue(E, 0);
  2752. N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
  2753. CSEMap.InsertNode(N, IP);
  2754. } else {
  2755. N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
  2756. }
  2757. AllNodes.push_back(N);
  2758. #ifndef NDEBUG
  2759. VerifyNode(N);
  2760. #endif
  2761. return SDValue(N, 0);
  2762. }
  2763. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
  2764. SDValue N1, SDValue N2, SDValue N3,
  2765. SDValue N4) {
  2766. SDValue Ops[] = { N1, N2, N3, N4 };
  2767. return getNode(Opcode, DL, VT, Ops, 4);
  2768. }
  2769. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
  2770. SDValue N1, SDValue N2, SDValue N3,
  2771. SDValue N4, SDValue N5) {
  2772. SDValue Ops[] = { N1, N2, N3, N4, N5 };
  2773. return getNode(Opcode, DL, VT, Ops, 5);
  2774. }
  2775. /// getStackArgumentTokenFactor - Compute a TokenFactor to force all
  2776. /// the incoming stack arguments to be loaded from the stack.
  2777. SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
  2778. SmallVector<SDValue, 8> ArgChains;
  2779. // Include the original chain at the beginning of the list. When this is
  2780. // used by target LowerCall hooks, this helps legalize find the
  2781. // CALLSEQ_BEGIN node.
  2782. ArgChains.push_back(Chain);
  2783. // Add a chain value for each stack argument.
  2784. for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
  2785. UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
  2786. if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
  2787. if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
  2788. if (FI->getIndex() < 0)
  2789. ArgChains.push_back(SDValue(L, 1));
  2790. // Build a tokenfactor for all the chains.
  2791. return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
  2792. &ArgChains[0], ArgChains.size());
  2793. }
  2794. /// getMemsetValue - Vectorized representation of the memset value
  2795. /// operand.
  2796. static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
  2797. DebugLoc dl) {
  2798. assert(Value.getOpcode() != ISD::UNDEF);
  2799. unsigned NumBits = VT.getScalarType().getSizeInBits();
  2800. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
  2801. APInt Val = APInt(NumBits, C->getZExtValue() & 255);
  2802. unsigned Shift = 8;
  2803. for (unsigned i = NumBits; i > 8; i >>= 1) {
  2804. Val = (Val << Shift) | Val;
  2805. Shift <<= 1;
  2806. }
  2807. if (VT.isInteger())
  2808. return DAG.getConstant(Val, VT);
  2809. return DAG.getConstantFP(APFloat(Val), VT);
  2810. }
  2811. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  2812. Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
  2813. unsigned Shift = 8;
  2814. for (unsigned i = NumBits; i > 8; i >>= 1) {
  2815. Value = DAG.getNode(ISD::OR, dl, VT,
  2816. DAG.getNode(ISD::SHL, dl, VT, Value,
  2817. DAG.getConstant(Shift,
  2818. TLI.getShiftAmountTy())),
  2819. Value);
  2820. Shift <<= 1;
  2821. }
  2822. return Value;
  2823. }
  2824. /// getMemsetStringVal - Similar to getMemsetValue. Except this is only
  2825. /// used when a memcpy is turned into a memset when the source is a constant
  2826. /// string ptr.
  2827. static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
  2828. const TargetLowering &TLI,
  2829. std::string &Str, unsigned Offset) {
  2830. // Handle vector with all elements zero.
  2831. if (Str.empty()) {
  2832. if (VT.isInteger())
  2833. return DAG.getConstant(0, VT);
  2834. else if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
  2835. VT.getSimpleVT().SimpleTy == MVT::f64)
  2836. return DAG.getConstantFP(0.0, VT);
  2837. else if (VT.isVector()) {
  2838. unsigned NumElts = VT.getVectorNumElements();
  2839. MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
  2840. return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
  2841. DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
  2842. EltVT, NumElts)));
  2843. } else
  2844. llvm_unreachable("Expected type!");
  2845. }
  2846. assert(!VT.isVector() && "Can't handle vector type here!");
  2847. unsigned NumBits = VT.getSizeInBits();
  2848. unsigned MSB = NumBits / 8;
  2849. uint64_t Val = 0;
  2850. if (TLI.isLittleEndian())
  2851. Offset = Offset + MSB - 1;
  2852. for (unsigned i = 0; i != MSB; ++i) {
  2853. Val = (Val << 8) | (unsigned char)Str[Offset];
  2854. Offset += TLI.isLittleEndian() ? -1 : 1;
  2855. }
  2856. return DAG.getConstant(Val, VT);
  2857. }
  2858. /// getMemBasePlusOffset - Returns base and offset node for the
  2859. ///
  2860. static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
  2861. SelectionDAG &DAG) {
  2862. EVT VT = Base.getValueType();
  2863. return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
  2864. VT, Base, DAG.getConstant(Offset, VT));
  2865. }
  2866. /// isMemSrcFromString - Returns true if memcpy source is a string constant.
  2867. ///
  2868. static bool isMemSrcFromString(SDValue Src, std::string &Str) {
  2869. unsigned SrcDelta = 0;
  2870. GlobalAddressSDNode *G = NULL;
  2871. if (Src.getOpcode() == ISD::GlobalAddress)
  2872. G = cast<GlobalAddressSDNode>(Src);
  2873. else if (Src.getOpcode() == ISD::ADD &&
  2874. Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
  2875. Src.getOperand(1).getOpcode() == ISD::Constant) {
  2876. G = cast<GlobalAddressSDNode>(Src.getOperand(0));
  2877. SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
  2878. }
  2879. if (!G)
  2880. return false;
  2881. const GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
  2882. if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
  2883. return true;
  2884. return false;
  2885. }
  2886. /// FindOptimalMemOpLowering - Determines the optimial series memory ops
  2887. /// to replace the memset / memcpy. Return true if the number of memory ops
  2888. /// is below the threshold. It returns the types of the sequence of
  2889. /// memory ops to perform memset / memcpy by reference.
  2890. static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
  2891. unsigned Limit, uint64_t Size,
  2892. unsigned DstAlign, unsigned SrcAlign,
  2893. bool NonScalarIntSafe,
  2894. bool MemcpyStrSrc,
  2895. SelectionDAG &DAG,
  2896. const TargetLowering &TLI) {
  2897. assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
  2898. "Expecting memcpy / memset source to meet alignment requirement!");
  2899. // If 'SrcAlign' is zero, that means the memory operation does not need load
  2900. // the value, i.e. memset or memcpy from constant string. Otherwise, it's
  2901. // the inferred alignment of the source. 'DstAlign', on the other hand, is the
  2902. // specified alignment of the memory operation. If it is zero, that means
  2903. // it's possible to change the alignment of the destination. 'MemcpyStrSrc'
  2904. // indicates whether the memcpy source is constant so it does not need to be
  2905. // loaded.
  2906. EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
  2907. NonScalarIntSafe, MemcpyStrSrc,
  2908. DAG.getMachineFunction());
  2909. if (VT == MVT::Other) {
  2910. if (DstAlign >= TLI.getTargetData()->getPointerPrefAlignment() ||
  2911. TLI.allowsUnalignedMemoryAccesses(VT)) {
  2912. VT = TLI.getPointerTy();
  2913. } else {
  2914. switch (DstAlign & 7) {
  2915. case 0: VT = MVT::i64; break;
  2916. case 4: VT = MVT::i32; break;
  2917. case 2: VT = MVT::i16; break;
  2918. default: VT = MVT::i8; break;
  2919. }
  2920. }
  2921. MVT LVT = MVT::i64;
  2922. while (!TLI.isTypeLegal(LVT))
  2923. LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
  2924. assert(LVT.isInteger());
  2925. if (VT.bitsGT(LVT))
  2926. VT = LVT;
  2927. }
  2928. // If we're optimizing for size, and there is a limit, bump the maximum number
  2929. // of operations inserted down to 4. This is a wild guess that approximates
  2930. // the size of a call to memcpy or memset (3 arguments + call).
  2931. if (Limit != ~0U) {
  2932. const Function *F = DAG.getMachineFunction().getFunction();
  2933. if (F->hasFnAttr(Attribute::OptimizeForSize))
  2934. Limit = 4;
  2935. }
  2936. unsigned NumMemOps = 0;
  2937. while (Size != 0) {
  2938. unsigned VTSize = VT.getSizeInBits() / 8;
  2939. while (VTSize > Size) {
  2940. // For now, only use non-vector load / store's for the left-over pieces.
  2941. if (VT.isVector() || VT.isFloatingPoint()) {
  2942. VT = MVT::i64;
  2943. while (!TLI.isTypeLegal(VT))
  2944. VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
  2945. VTSize = VT.getSizeInBits() / 8;
  2946. } else {
  2947. // This can result in a type that is not legal on the target, e.g.
  2948. // 1 or 2 bytes on PPC.
  2949. VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
  2950. VTSize >>= 1;
  2951. }
  2952. }
  2953. if (++NumMemOps > Limit)
  2954. return false;
  2955. MemOps.push_back(VT);
  2956. Size -= VTSize;
  2957. }
  2958. return true;
  2959. }
  2960. static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
  2961. SDValue Chain, SDValue Dst,
  2962. SDValue Src, uint64_t Size,
  2963. unsigned Align, bool isVol,
  2964. bool AlwaysInline,
  2965. const Value *DstSV, uint64_t DstSVOff,
  2966. const Value *SrcSV, uint64_t SrcSVOff) {
  2967. // Turn a memcpy of undef to nop.
  2968. if (Src.getOpcode() == ISD::UNDEF)
  2969. return Chain;
  2970. // Expand memcpy to a series of load and store ops if the size operand falls
  2971. // below a certain threshold.
  2972. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  2973. std::vector<EVT> MemOps;
  2974. bool DstAlignCanChange = false;
  2975. MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
  2976. FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
  2977. if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
  2978. DstAlignCanChange = true;
  2979. unsigned SrcAlign = DAG.InferPtrAlignment(Src);
  2980. if (Align > SrcAlign)
  2981. SrcAlign = Align;
  2982. std::string Str;
  2983. bool CopyFromStr = isMemSrcFromString(Src, Str);
  2984. bool isZeroStr = CopyFromStr && Str.empty();
  2985. unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy();
  2986. if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
  2987. (DstAlignCanChange ? 0 : Align),
  2988. (isZeroStr ? 0 : SrcAlign),
  2989. true, CopyFromStr, DAG, TLI))
  2990. return SDValue();
  2991. if (DstAlignCanChange) {
  2992. const Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
  2993. unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
  2994. if (NewAlign > Align) {
  2995. // Give the stack frame object a larger alignment if needed.
  2996. if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
  2997. MFI->setObjectAlignment(FI->getIndex(), NewAlign);
  2998. Align = NewAlign;
  2999. }
  3000. }
  3001. SmallVector<SDValue, 8> OutChains;
  3002. unsigned NumMemOps = MemOps.size();
  3003. uint64_t SrcOff = 0, DstOff = 0;
  3004. for (unsigned i = 0; i != NumMemOps; ++i) {
  3005. EVT VT = MemOps[i];
  3006. unsigned VTSize = VT.getSizeInBits() / 8;
  3007. SDValue Value, Store;
  3008. if (CopyFromStr &&
  3009. (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
  3010. // It's unlikely a store of a vector immediate can be done in a single
  3011. // instruction. It would require a load from a constantpool first.
  3012. // We only handle zero vectors here.
  3013. // FIXME: Handle other cases where store of vector immediate is done in
  3014. // a single instruction.
  3015. Value = getMemsetStringVal(VT, dl, DAG, TLI, Str, SrcOff);
  3016. Store = DAG.getStore(Chain, dl, Value,
  3017. getMemBasePlusOffset(Dst, DstOff, DAG),
  3018. DstSV, DstSVOff + DstOff, isVol, false, Align);
  3019. } else {
  3020. // The type might not be legal for the target. This should only happen
  3021. // if the type is smaller than a legal type, as on PPC, so the right
  3022. // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
  3023. // to Load/Store if NVT==VT.
  3024. // FIXME does the case above also need this?
  3025. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  3026. assert(NVT.bitsGE(VT));
  3027. Value = DAG.getExtLoad(ISD::EXTLOAD, NVT, dl, Chain,
  3028. getMemBasePlusOffset(Src, SrcOff, DAG),
  3029. SrcSV, SrcSVOff + SrcOff, VT, isVol, false,
  3030. MinAlign(SrcAlign, SrcOff));
  3031. Store = DAG.getTruncStore(Chain, dl, Value,
  3032. getMemBasePlusOffset(Dst, DstOff, DAG),
  3033. DstSV, DstSVOff + DstOff, VT, isVol, false,
  3034. Align);
  3035. }
  3036. OutChains.push_back(Store);
  3037. SrcOff += VTSize;
  3038. DstOff += VTSize;
  3039. }
  3040. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  3041. &OutChains[0], OutChains.size());
  3042. }
  3043. static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
  3044. SDValue Chain, SDValue Dst,
  3045. SDValue Src, uint64_t Size,
  3046. unsigned Align, bool isVol,
  3047. bool AlwaysInline,
  3048. const Value *DstSV, uint64_t DstSVOff,
  3049. const Value *SrcSV, uint64_t SrcSVOff) {
  3050. // Turn a memmove of undef to nop.
  3051. if (Src.getOpcode() == ISD::UNDEF)
  3052. return Chain;
  3053. // Expand memmove to a series of load and store ops if the size operand falls
  3054. // below a certain threshold.
  3055. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  3056. std::vector<EVT> MemOps;
  3057. bool DstAlignCanChange = false;
  3058. MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
  3059. FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
  3060. if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
  3061. DstAlignCanChange = true;
  3062. unsigned SrcAlign = DAG.InferPtrAlignment(Src);
  3063. if (Align > SrcAlign)
  3064. SrcAlign = Align;
  3065. unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove();
  3066. if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
  3067. (DstAlignCanChange ? 0 : Align),
  3068. SrcAlign, true, false, DAG, TLI))
  3069. return SDValue();
  3070. if (DstAlignCanChange) {
  3071. const Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
  3072. unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
  3073. if (NewAlign > Align) {
  3074. // Give the stack frame object a larger alignment if needed.
  3075. if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
  3076. MFI->setObjectAlignment(FI->getIndex(), NewAlign);
  3077. Align = NewAlign;
  3078. }
  3079. }
  3080. uint64_t SrcOff = 0, DstOff = 0;
  3081. SmallVector<SDValue, 8> LoadValues;
  3082. SmallVector<SDValue, 8> LoadChains;
  3083. SmallVector<SDValue, 8> OutChains;
  3084. unsigned NumMemOps = MemOps.size();
  3085. for (unsigned i = 0; i < NumMemOps; i++) {
  3086. EVT VT = MemOps[i];
  3087. unsigned VTSize = VT.getSizeInBits() / 8;
  3088. SDValue Value, Store;
  3089. Value = DAG.getLoad(VT, dl, Chain,
  3090. getMemBasePlusOffset(Src, SrcOff, DAG),
  3091. SrcSV, SrcSVOff + SrcOff, isVol, false, SrcAlign);
  3092. LoadValues.push_back(Value);
  3093. LoadChains.push_back(Value.getValue(1));
  3094. SrcOff += VTSize;
  3095. }
  3096. Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  3097. &LoadChains[0], LoadChains.size());
  3098. OutChains.clear();
  3099. for (unsigned i = 0; i < NumMemOps; i++) {
  3100. EVT VT = MemOps[i];
  3101. unsigned VTSize = VT.getSizeInBits() / 8;
  3102. SDValue Value, Store;
  3103. Store = DAG.getStore(Chain, dl, LoadValues[i],
  3104. getMemBasePlusOffset(Dst, DstOff, DAG),
  3105. DstSV, DstSVOff + DstOff, isVol, false, Align);
  3106. OutChains.push_back(Store);
  3107. DstOff += VTSize;
  3108. }
  3109. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  3110. &OutChains[0], OutChains.size());
  3111. }
  3112. static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
  3113. SDValue Chain, SDValue Dst,
  3114. SDValue Src, uint64_t Size,
  3115. unsigned Align, bool isVol,
  3116. const Value *DstSV, uint64_t DstSVOff) {
  3117. // Turn a memset of undef to nop.
  3118. if (Src.getOpcode() == ISD::UNDEF)
  3119. return Chain;
  3120. // Expand memset to a series of load/store ops if the size operand
  3121. // falls below a certain threshold.
  3122. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  3123. std::vector<EVT> MemOps;
  3124. bool DstAlignCanChange = false;
  3125. MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
  3126. FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
  3127. if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
  3128. DstAlignCanChange = true;
  3129. bool NonScalarIntSafe =
  3130. isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
  3131. if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(),
  3132. Size, (DstAlignCanChange ? 0 : Align), 0,
  3133. NonScalarIntSafe, false, DAG, TLI))
  3134. return SDValue();
  3135. if (DstAlignCanChange) {
  3136. const Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
  3137. unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
  3138. if (NewAlign > Align) {
  3139. // Give the stack frame object a larger alignment if needed.
  3140. if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
  3141. MFI->setObjectAlignment(FI->getIndex(), NewAlign);
  3142. Align = NewAlign;
  3143. }
  3144. }
  3145. SmallVector<SDValue, 8> OutChains;
  3146. uint64_t DstOff = 0;
  3147. unsigned NumMemOps = MemOps.size();
  3148. for (unsigned i = 0; i < NumMemOps; i++) {
  3149. EVT VT = MemOps[i];
  3150. unsigned VTSize = VT.getSizeInBits() / 8;
  3151. SDValue Value = getMemsetValue(Src, VT, DAG, dl);
  3152. SDValue Store = DAG.getStore(Chain, dl, Value,
  3153. getMemBasePlusOffset(Dst, DstOff, DAG),
  3154. DstSV, DstSVOff + DstOff, isVol, false, 0);
  3155. OutChains.push_back(Store);
  3156. DstOff += VTSize;
  3157. }
  3158. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  3159. &OutChains[0], OutChains.size());
  3160. }
  3161. SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
  3162. SDValue Src, SDValue Size,
  3163. unsigned Align, bool isVol, bool AlwaysInline,
  3164. const Value *DstSV, uint64_t DstSVOff,
  3165. const Value *SrcSV, uint64_t SrcSVOff) {
  3166. // Check to see if we should lower the memcpy to loads and stores first.
  3167. // For cases within the target-specified limits, this is the best choice.
  3168. ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
  3169. if (ConstantSize) {
  3170. // Memcpy with size zero? Just return the original chain.
  3171. if (ConstantSize->isNullValue())
  3172. return Chain;
  3173. SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
  3174. ConstantSize->getZExtValue(),Align,
  3175. isVol, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
  3176. if (Result.getNode())
  3177. return Result;
  3178. }
  3179. // Then check to see if we should lower the memcpy with target-specific
  3180. // code. If the target chooses to do this, this is the next best.
  3181. SDValue Result =
  3182. TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
  3183. isVol, AlwaysInline,
  3184. DstSV, DstSVOff, SrcSV, SrcSVOff);
  3185. if (Result.getNode())
  3186. return Result;
  3187. // If we really need inline code and the target declined to provide it,
  3188. // use a (potentially long) sequence of loads and stores.
  3189. if (AlwaysInline) {
  3190. assert(ConstantSize && "AlwaysInline requires a constant size!");
  3191. return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
  3192. ConstantSize->getZExtValue(), Align, isVol,
  3193. true, DstSV, DstSVOff, SrcSV, SrcSVOff);
  3194. }
  3195. // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
  3196. // memcpy is not guaranteed to be safe. libc memcpys aren't required to
  3197. // respect volatile, so they may do things like read or write memory
  3198. // beyond the given memory regions. But fixing this isn't easy, and most
  3199. // people don't care.
  3200. // Emit a library call.
  3201. TargetLowering::ArgListTy Args;
  3202. TargetLowering::ArgListEntry Entry;
  3203. Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
  3204. Entry.Node = Dst; Args.push_back(Entry);
  3205. Entry.Node = Src; Args.push_back(Entry);
  3206. Entry.Node = Size; Args.push_back(Entry);
  3207. // FIXME: pass in DebugLoc
  3208. std::pair<SDValue,SDValue> CallResult =
  3209. TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
  3210. false, false, false, false, 0,
  3211. TLI.getLibcallCallingConv(RTLIB::MEMCPY), false,
  3212. /*isReturnValueUsed=*/false,
  3213. getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
  3214. TLI.getPointerTy()),
  3215. Args, *this, dl);
  3216. return CallResult.second;
  3217. }
  3218. SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
  3219. SDValue Src, SDValue Size,
  3220. unsigned Align, bool isVol,
  3221. const Value *DstSV, uint64_t DstSVOff,
  3222. const Value *SrcSV, uint64_t SrcSVOff) {
  3223. // Check to see if we should lower the memmove to loads and stores first.
  3224. // For cases within the target-specified limits, this is the best choice.
  3225. ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
  3226. if (ConstantSize) {
  3227. // Memmove with size zero? Just return the original chain.
  3228. if (ConstantSize->isNullValue())
  3229. return Chain;
  3230. SDValue Result =
  3231. getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
  3232. ConstantSize->getZExtValue(), Align, isVol,
  3233. false, DstSV, DstSVOff, SrcSV, SrcSVOff);
  3234. if (Result.getNode())
  3235. return Result;
  3236. }
  3237. // Then check to see if we should lower the memmove with target-specific
  3238. // code. If the target chooses to do this, this is the next best.
  3239. SDValue Result =
  3240. TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
  3241. DstSV, DstSVOff, SrcSV, SrcSVOff);
  3242. if (Result.getNode())
  3243. return Result;
  3244. // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
  3245. // not be safe. See memcpy above for more details.
  3246. // Emit a library call.
  3247. TargetLowering::ArgListTy Args;
  3248. TargetLowering::ArgListEntry Entry;
  3249. Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
  3250. Entry.Node = Dst; Args.push_back(Entry);
  3251. Entry.Node = Src; Args.push_back(Entry);
  3252. Entry.Node = Size; Args.push_back(Entry);
  3253. // FIXME: pass in DebugLoc
  3254. std::pair<SDValue,SDValue> CallResult =
  3255. TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
  3256. false, false, false, false, 0,
  3257. TLI.getLibcallCallingConv(RTLIB::MEMMOVE), false,
  3258. /*isReturnValueUsed=*/false,
  3259. getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
  3260. TLI.getPointerTy()),
  3261. Args, *this, dl);
  3262. return CallResult.second;
  3263. }
  3264. SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
  3265. SDValue Src, SDValue Size,
  3266. unsigned Align, bool isVol,
  3267. const Value *DstSV, uint64_t DstSVOff) {
  3268. // Check to see if we should lower the memset to stores first.
  3269. // For cases within the target-specified limits, this is the best choice.
  3270. ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
  3271. if (ConstantSize) {
  3272. // Memset with size zero? Just return the original chain.
  3273. if (ConstantSize->isNullValue())
  3274. return Chain;
  3275. SDValue Result =
  3276. getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
  3277. Align, isVol, DstSV, DstSVOff);
  3278. if (Result.getNode())
  3279. return Result;
  3280. }
  3281. // Then check to see if we should lower the memset with target-specific
  3282. // code. If the target chooses to do this, this is the next best.
  3283. SDValue Result =
  3284. TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
  3285. DstSV, DstSVOff);
  3286. if (Result.getNode())
  3287. return Result;
  3288. // Emit a library call.
  3289. const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext());
  3290. TargetLowering::ArgListTy Args;
  3291. TargetLowering::ArgListEntry Entry;
  3292. Entry.Node = Dst; Entry.Ty = IntPtrTy;
  3293. Args.push_back(Entry);
  3294. // Extend or truncate the argument to be an i32 value for the call.
  3295. if (Src.getValueType().bitsGT(MVT::i32))
  3296. Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
  3297. else
  3298. Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
  3299. Entry.Node = Src;
  3300. Entry.Ty = Type::getInt32Ty(*getContext());
  3301. Entry.isSExt = true;
  3302. Args.push_back(Entry);
  3303. Entry.Node = Size;
  3304. Entry.Ty = IntPtrTy;
  3305. Entry.isSExt = false;
  3306. Args.push_back(Entry);
  3307. // FIXME: pass in DebugLoc
  3308. std::pair<SDValue,SDValue> CallResult =
  3309. TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
  3310. false, false, false, false, 0,
  3311. TLI.getLibcallCallingConv(RTLIB::MEMSET), false,
  3312. /*isReturnValueUsed=*/false,
  3313. getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
  3314. TLI.getPointerTy()),
  3315. Args, *this, dl);
  3316. return CallResult.second;
  3317. }
  3318. SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
  3319. SDValue Chain,
  3320. SDValue Ptr, SDValue Cmp,
  3321. SDValue Swp, const Value* PtrVal,
  3322. unsigned Alignment) {
  3323. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  3324. Alignment = getEVTAlignment(MemVT);
  3325. // Check if the memory reference references a frame index
  3326. if (!PtrVal)
  3327. if (const FrameIndexSDNode *FI =
  3328. dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
  3329. PtrVal = PseudoSourceValue::getFixedStack(FI->getIndex());
  3330. MachineFunction &MF = getMachineFunction();
  3331. unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
  3332. // For now, atomics are considered to be volatile always.
  3333. Flags |= MachineMemOperand::MOVolatile;
  3334. MachineMemOperand *MMO =
  3335. MF.getMachineMemOperand(PtrVal, Flags, 0,
  3336. MemVT.getStoreSize(), Alignment);
  3337. return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO);
  3338. }
  3339. SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
  3340. SDValue Chain,
  3341. SDValue Ptr, SDValue Cmp,
  3342. SDValue Swp, MachineMemOperand *MMO) {
  3343. assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
  3344. assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
  3345. EVT VT = Cmp.getValueType();
  3346. SDVTList VTs = getVTList(VT, MVT::Other);
  3347. FoldingSetNodeID ID;
  3348. ID.AddInteger(MemVT.getRawBits());
  3349. SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
  3350. AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
  3351. void* IP = 0;
  3352. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
  3353. cast<AtomicSDNode>(E)->refineAlignment(MMO);
  3354. return SDValue(E, 0);
  3355. }
  3356. SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
  3357. Ptr, Cmp, Swp, MMO);
  3358. CSEMap.InsertNode(N, IP);
  3359. AllNodes.push_back(N);
  3360. return SDValue(N, 0);
  3361. }
  3362. SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
  3363. SDValue Chain,
  3364. SDValue Ptr, SDValue Val,
  3365. const Value* PtrVal,
  3366. unsigned Alignment) {
  3367. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  3368. Alignment = getEVTAlignment(MemVT);
  3369. // Check if the memory reference references a frame index
  3370. if (!PtrVal)
  3371. if (const FrameIndexSDNode *FI =
  3372. dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
  3373. PtrVal = PseudoSourceValue::getFixedStack(FI->getIndex());
  3374. MachineFunction &MF = getMachineFunction();
  3375. unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
  3376. // For now, atomics are considered to be volatile always.
  3377. Flags |= MachineMemOperand::MOVolatile;
  3378. MachineMemOperand *MMO =
  3379. MF.getMachineMemOperand(PtrVal, Flags, 0,
  3380. MemVT.getStoreSize(), Alignment);
  3381. return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO);
  3382. }
  3383. SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
  3384. SDValue Chain,
  3385. SDValue Ptr, SDValue Val,
  3386. MachineMemOperand *MMO) {
  3387. assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
  3388. Opcode == ISD::ATOMIC_LOAD_SUB ||
  3389. Opcode == ISD::ATOMIC_LOAD_AND ||
  3390. Opcode == ISD::ATOMIC_LOAD_OR ||
  3391. Opcode == ISD::ATOMIC_LOAD_XOR ||
  3392. Opcode == ISD::ATOMIC_LOAD_NAND ||
  3393. Opcode == ISD::ATOMIC_LOAD_MIN ||
  3394. Opcode == ISD::ATOMIC_LOAD_MAX ||
  3395. Opcode == ISD::ATOMIC_LOAD_UMIN ||
  3396. Opcode == ISD::ATOMIC_LOAD_UMAX ||
  3397. Opcode == ISD::ATOMIC_SWAP) &&
  3398. "Invalid Atomic Op");
  3399. EVT VT = Val.getValueType();
  3400. SDVTList VTs = getVTList(VT, MVT::Other);
  3401. FoldingSetNodeID ID;
  3402. ID.AddInteger(MemVT.getRawBits());
  3403. SDValue Ops[] = {Chain, Ptr, Val};
  3404. AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
  3405. void* IP = 0;
  3406. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
  3407. cast<AtomicSDNode>(E)->refineAlignment(MMO);
  3408. return SDValue(E, 0);
  3409. }
  3410. SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
  3411. Ptr, Val, MMO);
  3412. CSEMap.InsertNode(N, IP);
  3413. AllNodes.push_back(N);
  3414. return SDValue(N, 0);
  3415. }
  3416. /// getMergeValues - Create a MERGE_VALUES node from the given operands.
  3417. /// Allowed to return something different (and simpler) if Simplify is true.
  3418. SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
  3419. DebugLoc dl) {
  3420. if (NumOps == 1)
  3421. return Ops[0];
  3422. SmallVector<EVT, 4> VTs;
  3423. VTs.reserve(NumOps);
  3424. for (unsigned i = 0; i < NumOps; ++i)
  3425. VTs.push_back(Ops[i].getValueType());
  3426. return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
  3427. Ops, NumOps);
  3428. }
  3429. SDValue
  3430. SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
  3431. const EVT *VTs, unsigned NumVTs,
  3432. const SDValue *Ops, unsigned NumOps,
  3433. EVT MemVT, const Value *srcValue, int SVOff,
  3434. unsigned Align, bool Vol,
  3435. bool ReadMem, bool WriteMem) {
  3436. return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
  3437. MemVT, srcValue, SVOff, Align, Vol,
  3438. ReadMem, WriteMem);
  3439. }
  3440. SDValue
  3441. SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
  3442. const SDValue *Ops, unsigned NumOps,
  3443. EVT MemVT, const Value *srcValue, int SVOff,
  3444. unsigned Align, bool Vol,
  3445. bool ReadMem, bool WriteMem) {
  3446. if (Align == 0) // Ensure that codegen never sees alignment 0
  3447. Align = getEVTAlignment(MemVT);
  3448. MachineFunction &MF = getMachineFunction();
  3449. unsigned Flags = 0;
  3450. if (WriteMem)
  3451. Flags |= MachineMemOperand::MOStore;
  3452. if (ReadMem)
  3453. Flags |= MachineMemOperand::MOLoad;
  3454. if (Vol)
  3455. Flags |= MachineMemOperand::MOVolatile;
  3456. MachineMemOperand *MMO =
  3457. MF.getMachineMemOperand(srcValue, Flags, SVOff,
  3458. MemVT.getStoreSize(), Align);
  3459. return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
  3460. }
  3461. SDValue
  3462. SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
  3463. const SDValue *Ops, unsigned NumOps,
  3464. EVT MemVT, MachineMemOperand *MMO) {
  3465. assert((Opcode == ISD::INTRINSIC_VOID ||
  3466. Opcode == ISD::INTRINSIC_W_CHAIN ||
  3467. (Opcode <= INT_MAX &&
  3468. (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
  3469. "Opcode is not a memory-accessing opcode!");
  3470. // Memoize the node unless it returns a flag.
  3471. MemIntrinsicSDNode *N;
  3472. if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
  3473. FoldingSetNodeID ID;
  3474. AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
  3475. void *IP = 0;
  3476. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
  3477. cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
  3478. return SDValue(E, 0);
  3479. }
  3480. N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
  3481. MemVT, MMO);
  3482. CSEMap.InsertNode(N, IP);
  3483. } else {
  3484. N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
  3485. MemVT, MMO);
  3486. }
  3487. AllNodes.push_back(N);
  3488. return SDValue(N, 0);
  3489. }
  3490. SDValue
  3491. SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
  3492. EVT VT, DebugLoc dl, SDValue Chain,
  3493. SDValue Ptr, SDValue Offset,
  3494. const Value *SV, int SVOffset, EVT MemVT,
  3495. bool isVolatile, bool isNonTemporal,
  3496. unsigned Alignment) {
  3497. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  3498. Alignment = getEVTAlignment(VT);
  3499. // Check if the memory reference references a frame index
  3500. if (!SV)
  3501. if (const FrameIndexSDNode *FI =
  3502. dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
  3503. SV = PseudoSourceValue::getFixedStack(FI->getIndex());
  3504. MachineFunction &MF = getMachineFunction();
  3505. unsigned Flags = MachineMemOperand::MOLoad;
  3506. if (isVolatile)
  3507. Flags |= MachineMemOperand::MOVolatile;
  3508. if (isNonTemporal)
  3509. Flags |= MachineMemOperand::MONonTemporal;
  3510. MachineMemOperand *MMO =
  3511. MF.getMachineMemOperand(SV, Flags, SVOffset,
  3512. MemVT.getStoreSize(), Alignment);
  3513. return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
  3514. }
  3515. SDValue
  3516. SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
  3517. EVT VT, DebugLoc dl, SDValue Chain,
  3518. SDValue Ptr, SDValue Offset, EVT MemVT,
  3519. MachineMemOperand *MMO) {
  3520. if (VT == MemVT) {
  3521. ExtType = ISD::NON_EXTLOAD;
  3522. } else if (ExtType == ISD::NON_EXTLOAD) {
  3523. assert(VT == MemVT && "Non-extending load from different memory type!");
  3524. } else {
  3525. // Extending load.
  3526. assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
  3527. "Should only be an extending load, not truncating!");
  3528. assert(VT.isInteger() == MemVT.isInteger() &&
  3529. "Cannot convert from FP to Int or Int -> FP!");
  3530. assert(VT.isVector() == MemVT.isVector() &&
  3531. "Cannot use trunc store to convert to or from a vector!");
  3532. assert((!VT.isVector() ||
  3533. VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
  3534. "Cannot use trunc store to change the number of vector elements!");
  3535. }
  3536. bool Indexed = AM != ISD::UNINDEXED;
  3537. assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
  3538. "Unindexed load with an offset!");
  3539. SDVTList VTs = Indexed ?
  3540. getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
  3541. SDValue Ops[] = { Chain, Ptr, Offset };
  3542. FoldingSetNodeID ID;
  3543. AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
  3544. ID.AddInteger(MemVT.getRawBits());
  3545. ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
  3546. MMO->isNonTemporal()));
  3547. void *IP = 0;
  3548. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
  3549. cast<LoadSDNode>(E)->refineAlignment(MMO);
  3550. return SDValue(E, 0);
  3551. }
  3552. SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl, VTs, AM, ExtType,
  3553. MemVT, MMO);
  3554. CSEMap.InsertNode(N, IP);
  3555. AllNodes.push_back(N);
  3556. return SDValue(N, 0);
  3557. }
  3558. SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
  3559. SDValue Chain, SDValue Ptr,
  3560. const Value *SV, int SVOffset,
  3561. bool isVolatile, bool isNonTemporal,
  3562. unsigned Alignment) {
  3563. SDValue Undef = getUNDEF(Ptr.getValueType());
  3564. return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
  3565. SV, SVOffset, VT, isVolatile, isNonTemporal, Alignment);
  3566. }
  3567. SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, EVT VT, DebugLoc dl,
  3568. SDValue Chain, SDValue Ptr,
  3569. const Value *SV,
  3570. int SVOffset, EVT MemVT,
  3571. bool isVolatile, bool isNonTemporal,
  3572. unsigned Alignment) {
  3573. SDValue Undef = getUNDEF(Ptr.getValueType());
  3574. return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
  3575. SV, SVOffset, MemVT, isVolatile, isNonTemporal, Alignment);
  3576. }
  3577. SDValue
  3578. SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
  3579. SDValue Offset, ISD::MemIndexedMode AM) {
  3580. LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
  3581. assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
  3582. "Load is already a indexed load!");
  3583. return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
  3584. LD->getChain(), Base, Offset, LD->getSrcValue(),
  3585. LD->getSrcValueOffset(), LD->getMemoryVT(),
  3586. LD->isVolatile(), LD->isNonTemporal(), LD->getAlignment());
  3587. }
  3588. SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
  3589. SDValue Ptr, const Value *SV, int SVOffset,
  3590. bool isVolatile, bool isNonTemporal,
  3591. unsigned Alignment) {
  3592. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  3593. Alignment = getEVTAlignment(Val.getValueType());
  3594. // Check if the memory reference references a frame index
  3595. if (!SV)
  3596. if (const FrameIndexSDNode *FI =
  3597. dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
  3598. SV = PseudoSourceValue::getFixedStack(FI->getIndex());
  3599. MachineFunction &MF = getMachineFunction();
  3600. unsigned Flags = MachineMemOperand::MOStore;
  3601. if (isVolatile)
  3602. Flags |= MachineMemOperand::MOVolatile;
  3603. if (isNonTemporal)
  3604. Flags |= MachineMemOperand::MONonTemporal;
  3605. MachineMemOperand *MMO =
  3606. MF.getMachineMemOperand(SV, Flags, SVOffset,
  3607. Val.getValueType().getStoreSize(), Alignment);
  3608. return getStore(Chain, dl, Val, Ptr, MMO);
  3609. }
  3610. SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
  3611. SDValue Ptr, MachineMemOperand *MMO) {
  3612. EVT VT = Val.getValueType();
  3613. SDVTList VTs = getVTList(MVT::Other);
  3614. SDValue Undef = getUNDEF(Ptr.getValueType());
  3615. SDValue Ops[] = { Chain, Val, Ptr, Undef };
  3616. FoldingSetNodeID ID;
  3617. AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
  3618. ID.AddInteger(VT.getRawBits());
  3619. ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
  3620. MMO->isNonTemporal()));
  3621. void *IP = 0;
  3622. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
  3623. cast<StoreSDNode>(E)->refineAlignment(MMO);
  3624. return SDValue(E, 0);
  3625. }
  3626. SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
  3627. false, VT, MMO);
  3628. CSEMap.InsertNode(N, IP);
  3629. AllNodes.push_back(N);
  3630. return SDValue(N, 0);
  3631. }
  3632. SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
  3633. SDValue Ptr, const Value *SV,
  3634. int SVOffset, EVT SVT,
  3635. bool isVolatile, bool isNonTemporal,
  3636. unsigned Alignment) {
  3637. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  3638. Alignment = getEVTAlignment(SVT);
  3639. // Check if the memory reference references a frame index
  3640. if (!SV)
  3641. if (const FrameIndexSDNode *FI =
  3642. dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
  3643. SV = PseudoSourceValue::getFixedStack(FI->getIndex());
  3644. MachineFunction &MF = getMachineFunction();
  3645. unsigned Flags = MachineMemOperand::MOStore;
  3646. if (isVolatile)
  3647. Flags |= MachineMemOperand::MOVolatile;
  3648. if (isNonTemporal)
  3649. Flags |= MachineMemOperand::MONonTemporal;
  3650. MachineMemOperand *MMO =
  3651. MF.getMachineMemOperand(SV, Flags, SVOffset, SVT.getStoreSize(), Alignment);
  3652. return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
  3653. }
  3654. SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
  3655. SDValue Ptr, EVT SVT,
  3656. MachineMemOperand *MMO) {
  3657. EVT VT = Val.getValueType();
  3658. if (VT == SVT)
  3659. return getStore(Chain, dl, Val, Ptr, MMO);
  3660. assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
  3661. "Should only be a truncating store, not extending!");
  3662. assert(VT.isInteger() == SVT.isInteger() &&
  3663. "Can't do FP-INT conversion!");
  3664. assert(VT.isVector() == SVT.isVector() &&
  3665. "Cannot use trunc store to convert to or from a vector!");
  3666. assert((!VT.isVector() ||
  3667. VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
  3668. "Cannot use trunc store to change the number of vector elements!");
  3669. SDVTList VTs = getVTList(MVT::Other);
  3670. SDValue Undef = getUNDEF(Ptr.getValueType());
  3671. SDValue Ops[] = { Chain, Val, Ptr, Undef };
  3672. FoldingSetNodeID ID;
  3673. AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
  3674. ID.AddInteger(SVT.getRawBits());
  3675. ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
  3676. MMO->isNonTemporal()));
  3677. void *IP = 0;
  3678. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
  3679. cast<StoreSDNode>(E)->refineAlignment(MMO);
  3680. return SDValue(E, 0);
  3681. }
  3682. SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
  3683. true, SVT, MMO);
  3684. CSEMap.InsertNode(N, IP);
  3685. AllNodes.push_back(N);
  3686. return SDValue(N, 0);
  3687. }
  3688. SDValue
  3689. SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
  3690. SDValue Offset, ISD::MemIndexedMode AM) {
  3691. StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
  3692. assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
  3693. "Store is already a indexed store!");
  3694. SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
  3695. SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
  3696. FoldingSetNodeID ID;
  3697. AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
  3698. ID.AddInteger(ST->getMemoryVT().getRawBits());
  3699. ID.AddInteger(ST->getRawSubclassData());
  3700. void *IP = 0;
  3701. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  3702. return SDValue(E, 0);
  3703. SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, AM,
  3704. ST->isTruncatingStore(),
  3705. ST->getMemoryVT(),
  3706. ST->getMemOperand());
  3707. CSEMap.InsertNode(N, IP);
  3708. AllNodes.push_back(N);
  3709. return SDValue(N, 0);
  3710. }
  3711. SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
  3712. SDValue Chain, SDValue Ptr,
  3713. SDValue SV,
  3714. unsigned Align) {
  3715. SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
  3716. return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
  3717. }
  3718. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
  3719. const SDUse *Ops, unsigned NumOps) {
  3720. switch (NumOps) {
  3721. case 0: return getNode(Opcode, DL, VT);
  3722. case 1: return getNode(Opcode, DL, VT, Ops[0]);
  3723. case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
  3724. case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
  3725. default: break;
  3726. }
  3727. // Copy from an SDUse array into an SDValue array for use with
  3728. // the regular getNode logic.
  3729. SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
  3730. return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
  3731. }
  3732. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
  3733. const SDValue *Ops, unsigned NumOps) {
  3734. switch (NumOps) {
  3735. case 0: return getNode(Opcode, DL, VT);
  3736. case 1: return getNode(Opcode, DL, VT, Ops[0]);
  3737. case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
  3738. case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
  3739. default: break;
  3740. }
  3741. switch (Opcode) {
  3742. default: break;
  3743. case ISD::SELECT_CC: {
  3744. assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
  3745. assert(Ops[0].getValueType() == Ops[1].getValueType() &&
  3746. "LHS and RHS of condition must have same type!");
  3747. assert(Ops[2].getValueType() == Ops[3].getValueType() &&
  3748. "True and False arms of SelectCC must have same type!");
  3749. assert(Ops[2].getValueType() == VT &&
  3750. "select_cc node must be of same type as true and false value!");
  3751. break;
  3752. }
  3753. case ISD::BR_CC: {
  3754. assert(NumOps == 5 && "BR_CC takes 5 operands!");
  3755. assert(Ops[2].getValueType() == Ops[3].getValueType() &&
  3756. "LHS/RHS of comparison should match types!");
  3757. break;
  3758. }
  3759. }
  3760. // Memoize nodes.
  3761. SDNode *N;
  3762. SDVTList VTs = getVTList(VT);
  3763. if (VT != MVT::Flag) {
  3764. FoldingSetNodeID ID;
  3765. AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
  3766. void *IP = 0;
  3767. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  3768. return SDValue(E, 0);
  3769. N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
  3770. CSEMap.InsertNode(N, IP);
  3771. } else {
  3772. N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
  3773. }
  3774. AllNodes.push_back(N);
  3775. #ifndef NDEBUG
  3776. VerifyNode(N);
  3777. #endif
  3778. return SDValue(N, 0);
  3779. }
  3780. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
  3781. const std::vector<EVT> &ResultTys,
  3782. const SDValue *Ops, unsigned NumOps) {
  3783. return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
  3784. Ops, NumOps);
  3785. }
  3786. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
  3787. const EVT *VTs, unsigned NumVTs,
  3788. const SDValue *Ops, unsigned NumOps) {
  3789. if (NumVTs == 1)
  3790. return getNode(Opcode, DL, VTs[0], Ops, NumOps);
  3791. return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
  3792. }
  3793. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
  3794. const SDValue *Ops, unsigned NumOps) {
  3795. if (VTList.NumVTs == 1)
  3796. return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
  3797. #if 0
  3798. switch (Opcode) {
  3799. // FIXME: figure out how to safely handle things like
  3800. // int foo(int x) { return 1 << (x & 255); }
  3801. // int bar() { return foo(256); }
  3802. case ISD::SRA_PARTS:
  3803. case ISD::SRL_PARTS:
  3804. case ISD::SHL_PARTS:
  3805. if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
  3806. cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
  3807. return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
  3808. else if (N3.getOpcode() == ISD::AND)
  3809. if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
  3810. // If the and is only masking out bits that cannot effect the shift,
  3811. // eliminate the and.
  3812. unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
  3813. if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
  3814. return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
  3815. }
  3816. break;
  3817. }
  3818. #endif
  3819. // Memoize the node unless it returns a flag.
  3820. SDNode *N;
  3821. if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
  3822. FoldingSetNodeID ID;
  3823. AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
  3824. void *IP = 0;
  3825. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  3826. return SDValue(E, 0);
  3827. if (NumOps == 1) {
  3828. N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
  3829. } else if (NumOps == 2) {
  3830. N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
  3831. } else if (NumOps == 3) {
  3832. N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
  3833. Ops[2]);
  3834. } else {
  3835. N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
  3836. }
  3837. CSEMap.InsertNode(N, IP);
  3838. } else {
  3839. if (NumOps == 1) {
  3840. N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
  3841. } else if (NumOps == 2) {
  3842. N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
  3843. } else if (NumOps == 3) {
  3844. N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
  3845. Ops[2]);
  3846. } else {
  3847. N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
  3848. }
  3849. }
  3850. AllNodes.push_back(N);
  3851. #ifndef NDEBUG
  3852. VerifyNode(N);
  3853. #endif
  3854. return SDValue(N, 0);
  3855. }
  3856. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
  3857. return getNode(Opcode, DL, VTList, 0, 0);
  3858. }
  3859. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
  3860. SDValue N1) {
  3861. SDValue Ops[] = { N1 };
  3862. return getNode(Opcode, DL, VTList, Ops, 1);
  3863. }
  3864. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
  3865. SDValue N1, SDValue N2) {
  3866. SDValue Ops[] = { N1, N2 };
  3867. return getNode(Opcode, DL, VTList, Ops, 2);
  3868. }
  3869. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
  3870. SDValue N1, SDValue N2, SDValue N3) {
  3871. SDValue Ops[] = { N1, N2, N3 };
  3872. return getNode(Opcode, DL, VTList, Ops, 3);
  3873. }
  3874. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
  3875. SDValue N1, SDValue N2, SDValue N3,
  3876. SDValue N4) {
  3877. SDValue Ops[] = { N1, N2, N3, N4 };
  3878. return getNode(Opcode, DL, VTList, Ops, 4);
  3879. }
  3880. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
  3881. SDValue N1, SDValue N2, SDValue N3,
  3882. SDValue N4, SDValue N5) {
  3883. SDValue Ops[] = { N1, N2, N3, N4, N5 };
  3884. return getNode(Opcode, DL, VTList, Ops, 5);
  3885. }
  3886. SDVTList SelectionDAG::getVTList(EVT VT) {
  3887. return makeVTList(SDNode::getValueTypeList(VT), 1);
  3888. }
  3889. SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
  3890. for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
  3891. E = VTList.rend(); I != E; ++I)
  3892. if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
  3893. return *I;
  3894. EVT *Array = Allocator.Allocate<EVT>(2);
  3895. Array[0] = VT1;
  3896. Array[1] = VT2;
  3897. SDVTList Result = makeVTList(Array, 2);
  3898. VTList.push_back(Result);
  3899. return Result;
  3900. }
  3901. SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
  3902. for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
  3903. E = VTList.rend(); I != E; ++I)
  3904. if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
  3905. I->VTs[2] == VT3)
  3906. return *I;
  3907. EVT *Array = Allocator.Allocate<EVT>(3);
  3908. Array[0] = VT1;
  3909. Array[1] = VT2;
  3910. Array[2] = VT3;
  3911. SDVTList Result = makeVTList(Array, 3);
  3912. VTList.push_back(Result);
  3913. return Result;
  3914. }
  3915. SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
  3916. for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
  3917. E = VTList.rend(); I != E; ++I)
  3918. if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
  3919. I->VTs[2] == VT3 && I->VTs[3] == VT4)
  3920. return *I;
  3921. EVT *Array = Allocator.Allocate<EVT>(4);
  3922. Array[0] = VT1;
  3923. Array[1] = VT2;
  3924. Array[2] = VT3;
  3925. Array[3] = VT4;
  3926. SDVTList Result = makeVTList(Array, 4);
  3927. VTList.push_back(Result);
  3928. return Result;
  3929. }
  3930. SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
  3931. switch (NumVTs) {
  3932. case 0: llvm_unreachable("Cannot have nodes without results!");
  3933. case 1: return getVTList(VTs[0]);
  3934. case 2: return getVTList(VTs[0], VTs[1]);
  3935. case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
  3936. case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]);
  3937. default: break;
  3938. }
  3939. for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
  3940. E = VTList.rend(); I != E; ++I) {
  3941. if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
  3942. continue;
  3943. bool NoMatch = false;
  3944. for (unsigned i = 2; i != NumVTs; ++i)
  3945. if (VTs[i] != I->VTs[i]) {
  3946. NoMatch = true;
  3947. break;
  3948. }
  3949. if (!NoMatch)
  3950. return *I;
  3951. }
  3952. EVT *Array = Allocator.Allocate<EVT>(NumVTs);
  3953. std::copy(VTs, VTs+NumVTs, Array);
  3954. SDVTList Result = makeVTList(Array, NumVTs);
  3955. VTList.push_back(Result);
  3956. return Result;
  3957. }
  3958. /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
  3959. /// specified operands. If the resultant node already exists in the DAG,
  3960. /// this does not modify the specified node, instead it returns the node that
  3961. /// already exists. If the resultant node does not exist in the DAG, the
  3962. /// input node is returned. As a degenerate case, if you specify the same
  3963. /// input operands as the node already has, the input node is returned.
  3964. SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
  3965. assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
  3966. // Check to see if there is no change.
  3967. if (Op == N->getOperand(0)) return N;
  3968. // See if the modified node already exists.
  3969. void *InsertPos = 0;
  3970. if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
  3971. return Existing;
  3972. // Nope it doesn't. Remove the node from its current place in the maps.
  3973. if (InsertPos)
  3974. if (!RemoveNodeFromCSEMaps(N))
  3975. InsertPos = 0;
  3976. // Now we update the operands.
  3977. N->OperandList[0].set(Op);
  3978. // If this gets put into a CSE map, add it.
  3979. if (InsertPos) CSEMap.InsertNode(N, InsertPos);
  3980. return N;
  3981. }
  3982. SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
  3983. assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
  3984. // Check to see if there is no change.
  3985. if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
  3986. return N; // No operands changed, just return the input node.
  3987. // See if the modified node already exists.
  3988. void *InsertPos = 0;
  3989. if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
  3990. return Existing;
  3991. // Nope it doesn't. Remove the node from its current place in the maps.
  3992. if (InsertPos)
  3993. if (!RemoveNodeFromCSEMaps(N))
  3994. InsertPos = 0;
  3995. // Now we update the operands.
  3996. if (N->OperandList[0] != Op1)
  3997. N->OperandList[0].set(Op1);
  3998. if (N->OperandList[1] != Op2)
  3999. N->OperandList[1].set(Op2);
  4000. // If this gets put into a CSE map, add it.
  4001. if (InsertPos) CSEMap.InsertNode(N, InsertPos);
  4002. return N;
  4003. }
  4004. SDNode *SelectionDAG::
  4005. UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
  4006. SDValue Ops[] = { Op1, Op2, Op3 };
  4007. return UpdateNodeOperands(N, Ops, 3);
  4008. }
  4009. SDNode *SelectionDAG::
  4010. UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
  4011. SDValue Op3, SDValue Op4) {
  4012. SDValue Ops[] = { Op1, Op2, Op3, Op4 };
  4013. return UpdateNodeOperands(N, Ops, 4);
  4014. }
  4015. SDNode *SelectionDAG::
  4016. UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
  4017. SDValue Op3, SDValue Op4, SDValue Op5) {
  4018. SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
  4019. return UpdateNodeOperands(N, Ops, 5);
  4020. }
  4021. SDNode *SelectionDAG::
  4022. UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
  4023. assert(N->getNumOperands() == NumOps &&
  4024. "Update with wrong number of operands");
  4025. // Check to see if there is no change.
  4026. bool AnyChange = false;
  4027. for (unsigned i = 0; i != NumOps; ++i) {
  4028. if (Ops[i] != N->getOperand(i)) {
  4029. AnyChange = true;
  4030. break;
  4031. }
  4032. }
  4033. // No operands changed, just return the input node.
  4034. if (!AnyChange) return N;
  4035. // See if the modified node already exists.
  4036. void *InsertPos = 0;
  4037. if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
  4038. return Existing;
  4039. // Nope it doesn't. Remove the node from its current place in the maps.
  4040. if (InsertPos)
  4041. if (!RemoveNodeFromCSEMaps(N))
  4042. InsertPos = 0;
  4043. // Now we update the operands.
  4044. for (unsigned i = 0; i != NumOps; ++i)
  4045. if (N->OperandList[i] != Ops[i])
  4046. N->OperandList[i].set(Ops[i]);
  4047. // If this gets put into a CSE map, add it.
  4048. if (InsertPos) CSEMap.InsertNode(N, InsertPos);
  4049. return N;
  4050. }
  4051. /// DropOperands - Release the operands and set this node to have
  4052. /// zero operands.
  4053. void SDNode::DropOperands() {
  4054. // Unlike the code in MorphNodeTo that does this, we don't need to
  4055. // watch for dead nodes here.
  4056. for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
  4057. SDUse &Use = *I++;
  4058. Use.set(SDValue());
  4059. }
  4060. }
  4061. /// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
  4062. /// machine opcode.
  4063. ///
  4064. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4065. EVT VT) {
  4066. SDVTList VTs = getVTList(VT);
  4067. return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
  4068. }
  4069. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4070. EVT VT, SDValue Op1) {
  4071. SDVTList VTs = getVTList(VT);
  4072. SDValue Ops[] = { Op1 };
  4073. return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
  4074. }
  4075. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4076. EVT VT, SDValue Op1,
  4077. SDValue Op2) {
  4078. SDVTList VTs = getVTList(VT);
  4079. SDValue Ops[] = { Op1, Op2 };
  4080. return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
  4081. }
  4082. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4083. EVT VT, SDValue Op1,
  4084. SDValue Op2, SDValue Op3) {
  4085. SDVTList VTs = getVTList(VT);
  4086. SDValue Ops[] = { Op1, Op2, Op3 };
  4087. return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
  4088. }
  4089. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4090. EVT VT, const SDValue *Ops,
  4091. unsigned NumOps) {
  4092. SDVTList VTs = getVTList(VT);
  4093. return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
  4094. }
  4095. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4096. EVT VT1, EVT VT2, const SDValue *Ops,
  4097. unsigned NumOps) {
  4098. SDVTList VTs = getVTList(VT1, VT2);
  4099. return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
  4100. }
  4101. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4102. EVT VT1, EVT VT2) {
  4103. SDVTList VTs = getVTList(VT1, VT2);
  4104. return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
  4105. }
  4106. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4107. EVT VT1, EVT VT2, EVT VT3,
  4108. const SDValue *Ops, unsigned NumOps) {
  4109. SDVTList VTs = getVTList(VT1, VT2, VT3);
  4110. return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
  4111. }
  4112. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4113. EVT VT1, EVT VT2, EVT VT3, EVT VT4,
  4114. const SDValue *Ops, unsigned NumOps) {
  4115. SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
  4116. return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
  4117. }
  4118. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4119. EVT VT1, EVT VT2,
  4120. SDValue Op1) {
  4121. SDVTList VTs = getVTList(VT1, VT2);
  4122. SDValue Ops[] = { Op1 };
  4123. return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
  4124. }
  4125. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4126. EVT VT1, EVT VT2,
  4127. SDValue Op1, SDValue Op2) {
  4128. SDVTList VTs = getVTList(VT1, VT2);
  4129. SDValue Ops[] = { Op1, Op2 };
  4130. return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
  4131. }
  4132. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4133. EVT VT1, EVT VT2,
  4134. SDValue Op1, SDValue Op2,
  4135. SDValue Op3) {
  4136. SDVTList VTs = getVTList(VT1, VT2);
  4137. SDValue Ops[] = { Op1, Op2, Op3 };
  4138. return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
  4139. }
  4140. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4141. EVT VT1, EVT VT2, EVT VT3,
  4142. SDValue Op1, SDValue Op2,
  4143. SDValue Op3) {
  4144. SDVTList VTs = getVTList(VT1, VT2, VT3);
  4145. SDValue Ops[] = { Op1, Op2, Op3 };
  4146. return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
  4147. }
  4148. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4149. SDVTList VTs, const SDValue *Ops,
  4150. unsigned NumOps) {
  4151. N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
  4152. // Reset the NodeID to -1.
  4153. N->setNodeId(-1);
  4154. return N;
  4155. }
  4156. /// MorphNodeTo - This *mutates* the specified node to have the specified
  4157. /// return type, opcode, and operands.
  4158. ///
  4159. /// Note that MorphNodeTo returns the resultant node. If there is already a
  4160. /// node of the specified opcode and operands, it returns that node instead of
  4161. /// the current one. Note that the DebugLoc need not be the same.
  4162. ///
  4163. /// Using MorphNodeTo is faster than creating a new node and swapping it in
  4164. /// with ReplaceAllUsesWith both because it often avoids allocating a new
  4165. /// node, and because it doesn't require CSE recalculation for any of
  4166. /// the node's users.
  4167. ///
  4168. SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
  4169. SDVTList VTs, const SDValue *Ops,
  4170. unsigned NumOps) {
  4171. // If an identical node already exists, use it.
  4172. void *IP = 0;
  4173. if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
  4174. FoldingSetNodeID ID;
  4175. AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
  4176. if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
  4177. return ON;
  4178. }
  4179. if (!RemoveNodeFromCSEMaps(N))
  4180. IP = 0;
  4181. // Start the morphing.
  4182. N->NodeType = Opc;
  4183. N->ValueList = VTs.VTs;
  4184. N->NumValues = VTs.NumVTs;
  4185. // Clear the operands list, updating used nodes to remove this from their
  4186. // use list. Keep track of any operands that become dead as a result.
  4187. SmallPtrSet<SDNode*, 16> DeadNodeSet;
  4188. for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
  4189. SDUse &Use = *I++;
  4190. SDNode *Used = Use.getNode();
  4191. Use.set(SDValue());
  4192. if (Used->use_empty())
  4193. DeadNodeSet.insert(Used);
  4194. }
  4195. if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
  4196. // Initialize the memory references information.
  4197. MN->setMemRefs(0, 0);
  4198. // If NumOps is larger than the # of operands we can have in a
  4199. // MachineSDNode, reallocate the operand list.
  4200. if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
  4201. if (MN->OperandsNeedDelete)
  4202. delete[] MN->OperandList;
  4203. if (NumOps > array_lengthof(MN->LocalOperands))
  4204. // We're creating a final node that will live unmorphed for the
  4205. // remainder of the current SelectionDAG iteration, so we can allocate
  4206. // the operands directly out of a pool with no recycling metadata.
  4207. MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
  4208. Ops, NumOps);
  4209. else
  4210. MN->InitOperands(MN->LocalOperands, Ops, NumOps);
  4211. MN->OperandsNeedDelete = false;
  4212. } else
  4213. MN->InitOperands(MN->OperandList, Ops, NumOps);
  4214. } else {
  4215. // If NumOps is larger than the # of operands we currently have, reallocate
  4216. // the operand list.
  4217. if (NumOps > N->NumOperands) {
  4218. if (N->OperandsNeedDelete)
  4219. delete[] N->OperandList;
  4220. N->InitOperands(new SDUse[NumOps], Ops, NumOps);
  4221. N->OperandsNeedDelete = true;
  4222. } else
  4223. N->InitOperands(N->OperandList, Ops, NumOps);
  4224. }
  4225. // Delete any nodes that are still dead after adding the uses for the
  4226. // new operands.
  4227. if (!DeadNodeSet.empty()) {
  4228. SmallVector<SDNode *, 16> DeadNodes;
  4229. for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
  4230. E = DeadNodeSet.end(); I != E; ++I)
  4231. if ((*I)->use_empty())
  4232. DeadNodes.push_back(*I);
  4233. RemoveDeadNodes(DeadNodes);
  4234. }
  4235. if (IP)
  4236. CSEMap.InsertNode(N, IP); // Memoize the new node.
  4237. return N;
  4238. }
  4239. /// getMachineNode - These are used for target selectors to create a new node
  4240. /// with specified return type(s), MachineInstr opcode, and operands.
  4241. ///
  4242. /// Note that getMachineNode returns the resultant node. If there is already a
  4243. /// node of the specified opcode and operands, it returns that node instead of
  4244. /// the current one.
  4245. MachineSDNode *
  4246. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
  4247. SDVTList VTs = getVTList(VT);
  4248. return getMachineNode(Opcode, dl, VTs, 0, 0);
  4249. }
  4250. MachineSDNode *
  4251. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
  4252. SDVTList VTs = getVTList(VT);
  4253. SDValue Ops[] = { Op1 };
  4254. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4255. }
  4256. MachineSDNode *
  4257. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
  4258. SDValue Op1, SDValue Op2) {
  4259. SDVTList VTs = getVTList(VT);
  4260. SDValue Ops[] = { Op1, Op2 };
  4261. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4262. }
  4263. MachineSDNode *
  4264. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
  4265. SDValue Op1, SDValue Op2, SDValue Op3) {
  4266. SDVTList VTs = getVTList(VT);
  4267. SDValue Ops[] = { Op1, Op2, Op3 };
  4268. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4269. }
  4270. MachineSDNode *
  4271. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
  4272. const SDValue *Ops, unsigned NumOps) {
  4273. SDVTList VTs = getVTList(VT);
  4274. return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
  4275. }
  4276. MachineSDNode *
  4277. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
  4278. SDVTList VTs = getVTList(VT1, VT2);
  4279. return getMachineNode(Opcode, dl, VTs, 0, 0);
  4280. }
  4281. MachineSDNode *
  4282. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4283. EVT VT1, EVT VT2, SDValue Op1) {
  4284. SDVTList VTs = getVTList(VT1, VT2);
  4285. SDValue Ops[] = { Op1 };
  4286. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4287. }
  4288. MachineSDNode *
  4289. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4290. EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
  4291. SDVTList VTs = getVTList(VT1, VT2);
  4292. SDValue Ops[] = { Op1, Op2 };
  4293. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4294. }
  4295. MachineSDNode *
  4296. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4297. EVT VT1, EVT VT2, SDValue Op1,
  4298. SDValue Op2, SDValue Op3) {
  4299. SDVTList VTs = getVTList(VT1, VT2);
  4300. SDValue Ops[] = { Op1, Op2, Op3 };
  4301. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4302. }
  4303. MachineSDNode *
  4304. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4305. EVT VT1, EVT VT2,
  4306. const SDValue *Ops, unsigned NumOps) {
  4307. SDVTList VTs = getVTList(VT1, VT2);
  4308. return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
  4309. }
  4310. MachineSDNode *
  4311. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4312. EVT VT1, EVT VT2, EVT VT3,
  4313. SDValue Op1, SDValue Op2) {
  4314. SDVTList VTs = getVTList(VT1, VT2, VT3);
  4315. SDValue Ops[] = { Op1, Op2 };
  4316. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4317. }
  4318. MachineSDNode *
  4319. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4320. EVT VT1, EVT VT2, EVT VT3,
  4321. SDValue Op1, SDValue Op2, SDValue Op3) {
  4322. SDVTList VTs = getVTList(VT1, VT2, VT3);
  4323. SDValue Ops[] = { Op1, Op2, Op3 };
  4324. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4325. }
  4326. MachineSDNode *
  4327. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4328. EVT VT1, EVT VT2, EVT VT3,
  4329. const SDValue *Ops, unsigned NumOps) {
  4330. SDVTList VTs = getVTList(VT1, VT2, VT3);
  4331. return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
  4332. }
  4333. MachineSDNode *
  4334. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
  4335. EVT VT2, EVT VT3, EVT VT4,
  4336. const SDValue *Ops, unsigned NumOps) {
  4337. SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
  4338. return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
  4339. }
  4340. MachineSDNode *
  4341. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4342. const std::vector<EVT> &ResultTys,
  4343. const SDValue *Ops, unsigned NumOps) {
  4344. SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
  4345. return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
  4346. }
  4347. MachineSDNode *
  4348. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
  4349. const SDValue *Ops, unsigned NumOps) {
  4350. bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Flag;
  4351. MachineSDNode *N;
  4352. void *IP;
  4353. if (DoCSE) {
  4354. FoldingSetNodeID ID;
  4355. AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
  4356. IP = 0;
  4357. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  4358. return cast<MachineSDNode>(E);
  4359. }
  4360. // Allocate a new MachineSDNode.
  4361. N = new (NodeAllocator) MachineSDNode(~Opcode, DL, VTs);
  4362. // Initialize the operands list.
  4363. if (NumOps > array_lengthof(N->LocalOperands))
  4364. // We're creating a final node that will live unmorphed for the
  4365. // remainder of the current SelectionDAG iteration, so we can allocate
  4366. // the operands directly out of a pool with no recycling metadata.
  4367. N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
  4368. Ops, NumOps);
  4369. else
  4370. N->InitOperands(N->LocalOperands, Ops, NumOps);
  4371. N->OperandsNeedDelete = false;
  4372. if (DoCSE)
  4373. CSEMap.InsertNode(N, IP);
  4374. AllNodes.push_back(N);
  4375. #ifndef NDEBUG
  4376. VerifyNode(N);
  4377. #endif
  4378. return N;
  4379. }
  4380. /// getTargetExtractSubreg - A convenience function for creating
  4381. /// TargetOpcode::EXTRACT_SUBREG nodes.
  4382. SDValue
  4383. SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
  4384. SDValue Operand) {
  4385. SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
  4386. SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
  4387. VT, Operand, SRIdxVal);
  4388. return SDValue(Subreg, 0);
  4389. }
  4390. /// getTargetInsertSubreg - A convenience function for creating
  4391. /// TargetOpcode::INSERT_SUBREG nodes.
  4392. SDValue
  4393. SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
  4394. SDValue Operand, SDValue Subreg) {
  4395. SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
  4396. SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
  4397. VT, Operand, Subreg, SRIdxVal);
  4398. return SDValue(Result, 0);
  4399. }
  4400. /// getNodeIfExists - Get the specified node if it's already available, or
  4401. /// else return NULL.
  4402. SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
  4403. const SDValue *Ops, unsigned NumOps) {
  4404. if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
  4405. FoldingSetNodeID ID;
  4406. AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
  4407. void *IP = 0;
  4408. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  4409. return E;
  4410. }
  4411. return NULL;
  4412. }
  4413. /// getDbgValue - Creates a SDDbgValue node.
  4414. ///
  4415. SDDbgValue *
  4416. SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
  4417. DebugLoc DL, unsigned O) {
  4418. return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
  4419. }
  4420. SDDbgValue *
  4421. SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
  4422. DebugLoc DL, unsigned O) {
  4423. return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
  4424. }
  4425. SDDbgValue *
  4426. SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
  4427. DebugLoc DL, unsigned O) {
  4428. return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
  4429. }
  4430. namespace {
  4431. /// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
  4432. /// pointed to by a use iterator is deleted, increment the use iterator
  4433. /// so that it doesn't dangle.
  4434. ///
  4435. /// This class also manages a "downlink" DAGUpdateListener, to forward
  4436. /// messages to ReplaceAllUsesWith's callers.
  4437. ///
  4438. class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
  4439. SelectionDAG::DAGUpdateListener *DownLink;
  4440. SDNode::use_iterator &UI;
  4441. SDNode::use_iterator &UE;
  4442. virtual void NodeDeleted(SDNode *N, SDNode *E) {
  4443. // Increment the iterator as needed.
  4444. while (UI != UE && N == *UI)
  4445. ++UI;
  4446. // Then forward the message.
  4447. if (DownLink) DownLink->NodeDeleted(N, E);
  4448. }
  4449. virtual void NodeUpdated(SDNode *N) {
  4450. // Just forward the message.
  4451. if (DownLink) DownLink->NodeUpdated(N);
  4452. }
  4453. public:
  4454. RAUWUpdateListener(SelectionDAG::DAGUpdateListener *dl,
  4455. SDNode::use_iterator &ui,
  4456. SDNode::use_iterator &ue)
  4457. : DownLink(dl), UI(ui), UE(ue) {}
  4458. };
  4459. }
  4460. /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
  4461. /// This can cause recursive merging of nodes in the DAG.
  4462. ///
  4463. /// This version assumes From has a single result value.
  4464. ///
  4465. void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
  4466. DAGUpdateListener *UpdateListener) {
  4467. SDNode *From = FromN.getNode();
  4468. assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
  4469. "Cannot replace with this method!");
  4470. assert(From != To.getNode() && "Cannot replace uses of with self");
  4471. // Iterate over all the existing uses of From. New uses will be added
  4472. // to the beginning of the use list, which we avoid visiting.
  4473. // This specifically avoids visiting uses of From that arise while the
  4474. // replacement is happening, because any such uses would be the result
  4475. // of CSE: If an existing node looks like From after one of its operands
  4476. // is replaced by To, we don't want to replace of all its users with To
  4477. // too. See PR3018 for more info.
  4478. SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
  4479. RAUWUpdateListener Listener(UpdateListener, UI, UE);
  4480. while (UI != UE) {
  4481. SDNode *User = *UI;
  4482. // This node is about to morph, remove its old self from the CSE maps.
  4483. RemoveNodeFromCSEMaps(User);
  4484. // A user can appear in a use list multiple times, and when this
  4485. // happens the uses are usually next to each other in the list.
  4486. // To help reduce the number of CSE recomputations, process all
  4487. // the uses of this user that we can find this way.
  4488. do {
  4489. SDUse &Use = UI.getUse();
  4490. ++UI;
  4491. Use.set(To);
  4492. } while (UI != UE && *UI == User);
  4493. // Now that we have modified User, add it back to the CSE maps. If it
  4494. // already exists there, recursively merge the results together.
  4495. AddModifiedNodeToCSEMaps(User, &Listener);
  4496. }
  4497. }
  4498. /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
  4499. /// This can cause recursive merging of nodes in the DAG.
  4500. ///
  4501. /// This version assumes that for each value of From, there is a
  4502. /// corresponding value in To in the same position with the same type.
  4503. ///
  4504. void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
  4505. DAGUpdateListener *UpdateListener) {
  4506. #ifndef NDEBUG
  4507. for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
  4508. assert((!From->hasAnyUseOfValue(i) ||
  4509. From->getValueType(i) == To->getValueType(i)) &&
  4510. "Cannot use this version of ReplaceAllUsesWith!");
  4511. #endif
  4512. // Handle the trivial case.
  4513. if (From == To)
  4514. return;
  4515. // Iterate over just the existing users of From. See the comments in
  4516. // the ReplaceAllUsesWith above.
  4517. SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
  4518. RAUWUpdateListener Listener(UpdateListener, UI, UE);
  4519. while (UI != UE) {
  4520. SDNode *User = *UI;
  4521. // This node is about to morph, remove its old self from the CSE maps.
  4522. RemoveNodeFromCSEMaps(User);
  4523. // A user can appear in a use list multiple times, and when this
  4524. // happens the uses are usually next to each other in the list.
  4525. // To help reduce the number of CSE recomputations, process all
  4526. // the uses of this user that we can find this way.
  4527. do {
  4528. SDUse &Use = UI.getUse();
  4529. ++UI;
  4530. Use.setNode(To);
  4531. } while (UI != UE && *UI == User);
  4532. // Now that we have modified User, add it back to the CSE maps. If it
  4533. // already exists there, recursively merge the results together.
  4534. AddModifiedNodeToCSEMaps(User, &Listener);
  4535. }
  4536. }
  4537. /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
  4538. /// This can cause recursive merging of nodes in the DAG.
  4539. ///
  4540. /// This version can replace From with any result values. To must match the
  4541. /// number and types of values returned by From.
  4542. void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
  4543. const SDValue *To,
  4544. DAGUpdateListener *UpdateListener) {
  4545. if (From->getNumValues() == 1) // Handle the simple case efficiently.
  4546. return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
  4547. // Iterate over just the existing users of From. See the comments in
  4548. // the ReplaceAllUsesWith above.
  4549. SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
  4550. RAUWUpdateListener Listener(UpdateListener, UI, UE);
  4551. while (UI != UE) {
  4552. SDNode *User = *UI;
  4553. // This node is about to morph, remove its old self from the CSE maps.
  4554. RemoveNodeFromCSEMaps(User);
  4555. // A user can appear in a use list multiple times, and when this
  4556. // happens the uses are usually next to each other in the list.
  4557. // To help reduce the number of CSE recomputations, process all
  4558. // the uses of this user that we can find this way.
  4559. do {
  4560. SDUse &Use = UI.getUse();
  4561. const SDValue &ToOp = To[Use.getResNo()];
  4562. ++UI;
  4563. Use.set(ToOp);
  4564. } while (UI != UE && *UI == User);
  4565. // Now that we have modified User, add it back to the CSE maps. If it
  4566. // already exists there, recursively merge the results together.
  4567. AddModifiedNodeToCSEMaps(User, &Listener);
  4568. }
  4569. }
  4570. /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
  4571. /// uses of other values produced by From.getNode() alone. The Deleted
  4572. /// vector is handled the same way as for ReplaceAllUsesWith.
  4573. void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
  4574. DAGUpdateListener *UpdateListener){
  4575. // Handle the really simple, really trivial case efficiently.
  4576. if (From == To) return;
  4577. // Handle the simple, trivial, case efficiently.
  4578. if (From.getNode()->getNumValues() == 1) {
  4579. ReplaceAllUsesWith(From, To, UpdateListener);
  4580. return;
  4581. }
  4582. // Iterate over just the existing users of From. See the comments in
  4583. // the ReplaceAllUsesWith above.
  4584. SDNode::use_iterator UI = From.getNode()->use_begin(),
  4585. UE = From.getNode()->use_end();
  4586. RAUWUpdateListener Listener(UpdateListener, UI, UE);
  4587. while (UI != UE) {
  4588. SDNode *User = *UI;
  4589. bool UserRemovedFromCSEMaps = false;
  4590. // A user can appear in a use list multiple times, and when this
  4591. // happens the uses are usually next to each other in the list.
  4592. // To help reduce the number of CSE recomputations, process all
  4593. // the uses of this user that we can find this way.
  4594. do {
  4595. SDUse &Use = UI.getUse();
  4596. // Skip uses of different values from the same node.
  4597. if (Use.getResNo() != From.getResNo()) {
  4598. ++UI;
  4599. continue;
  4600. }
  4601. // If this node hasn't been modified yet, it's still in the CSE maps,
  4602. // so remove its old self from the CSE maps.
  4603. if (!UserRemovedFromCSEMaps) {
  4604. RemoveNodeFromCSEMaps(User);
  4605. UserRemovedFromCSEMaps = true;
  4606. }
  4607. ++UI;
  4608. Use.set(To);
  4609. } while (UI != UE && *UI == User);
  4610. // We are iterating over all uses of the From node, so if a use
  4611. // doesn't use the specific value, no changes are made.
  4612. if (!UserRemovedFromCSEMaps)
  4613. continue;
  4614. // Now that we have modified User, add it back to the CSE maps. If it
  4615. // already exists there, recursively merge the results together.
  4616. AddModifiedNodeToCSEMaps(User, &Listener);
  4617. }
  4618. }
  4619. namespace {
  4620. /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
  4621. /// to record information about a use.
  4622. struct UseMemo {
  4623. SDNode *User;
  4624. unsigned Index;
  4625. SDUse *Use;
  4626. };
  4627. /// operator< - Sort Memos by User.
  4628. bool operator<(const UseMemo &L, const UseMemo &R) {
  4629. return (intptr_t)L.User < (intptr_t)R.User;
  4630. }
  4631. }
  4632. /// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
  4633. /// uses of other values produced by From.getNode() alone. The same value
  4634. /// may appear in both the From and To list. The Deleted vector is
  4635. /// handled the same way as for ReplaceAllUsesWith.
  4636. void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
  4637. const SDValue *To,
  4638. unsigned Num,
  4639. DAGUpdateListener *UpdateListener){
  4640. // Handle the simple, trivial case efficiently.
  4641. if (Num == 1)
  4642. return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
  4643. // Read up all the uses and make records of them. This helps
  4644. // processing new uses that are introduced during the
  4645. // replacement process.
  4646. SmallVector<UseMemo, 4> Uses;
  4647. for (unsigned i = 0; i != Num; ++i) {
  4648. unsigned FromResNo = From[i].getResNo();
  4649. SDNode *FromNode = From[i].getNode();
  4650. for (SDNode::use_iterator UI = FromNode->use_begin(),
  4651. E = FromNode->use_end(); UI != E; ++UI) {
  4652. SDUse &Use = UI.getUse();
  4653. if (Use.getResNo() == FromResNo) {
  4654. UseMemo Memo = { *UI, i, &Use };
  4655. Uses.push_back(Memo);
  4656. }
  4657. }
  4658. }
  4659. // Sort the uses, so that all the uses from a given User are together.
  4660. std::sort(Uses.begin(), Uses.end());
  4661. for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
  4662. UseIndex != UseIndexEnd; ) {
  4663. // We know that this user uses some value of From. If it is the right
  4664. // value, update it.
  4665. SDNode *User = Uses[UseIndex].User;
  4666. // This node is about to morph, remove its old self from the CSE maps.
  4667. RemoveNodeFromCSEMaps(User);
  4668. // The Uses array is sorted, so all the uses for a given User
  4669. // are next to each other in the list.
  4670. // To help reduce the number of CSE recomputations, process all
  4671. // the uses of this user that we can find this way.
  4672. do {
  4673. unsigned i = Uses[UseIndex].Index;
  4674. SDUse &Use = *Uses[UseIndex].Use;
  4675. ++UseIndex;
  4676. Use.set(To[i]);
  4677. } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
  4678. // Now that we have modified User, add it back to the CSE maps. If it
  4679. // already exists there, recursively merge the results together.
  4680. AddModifiedNodeToCSEMaps(User, UpdateListener);
  4681. }
  4682. }
  4683. /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
  4684. /// based on their topological order. It returns the maximum id and a vector
  4685. /// of the SDNodes* in assigned order by reference.
  4686. unsigned SelectionDAG::AssignTopologicalOrder() {
  4687. unsigned DAGSize = 0;
  4688. // SortedPos tracks the progress of the algorithm. Nodes before it are
  4689. // sorted, nodes after it are unsorted. When the algorithm completes
  4690. // it is at the end of the list.
  4691. allnodes_iterator SortedPos = allnodes_begin();
  4692. // Visit all the nodes. Move nodes with no operands to the front of
  4693. // the list immediately. Annotate nodes that do have operands with their
  4694. // operand count. Before we do this, the Node Id fields of the nodes
  4695. // may contain arbitrary values. After, the Node Id fields for nodes
  4696. // before SortedPos will contain the topological sort index, and the
  4697. // Node Id fields for nodes At SortedPos and after will contain the
  4698. // count of outstanding operands.
  4699. for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
  4700. SDNode *N = I++;
  4701. checkForCycles(N);
  4702. unsigned Degree = N->getNumOperands();
  4703. if (Degree == 0) {
  4704. // A node with no uses, add it to the result array immediately.
  4705. N->setNodeId(DAGSize++);
  4706. allnodes_iterator Q = N;
  4707. if (Q != SortedPos)
  4708. SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
  4709. assert(SortedPos != AllNodes.end() && "Overran node list");
  4710. ++SortedPos;
  4711. } else {
  4712. // Temporarily use the Node Id as scratch space for the degree count.
  4713. N->setNodeId(Degree);
  4714. }
  4715. }
  4716. // Visit all the nodes. As we iterate, moves nodes into sorted order,
  4717. // such that by the time the end is reached all nodes will be sorted.
  4718. for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
  4719. SDNode *N = I;
  4720. checkForCycles(N);
  4721. // N is in sorted position, so all its uses have one less operand
  4722. // that needs to be sorted.
  4723. for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
  4724. UI != UE; ++UI) {
  4725. SDNode *P = *UI;
  4726. unsigned Degree = P->getNodeId();
  4727. assert(Degree != 0 && "Invalid node degree");
  4728. --Degree;
  4729. if (Degree == 0) {
  4730. // All of P's operands are sorted, so P may sorted now.
  4731. P->setNodeId(DAGSize++);
  4732. if (P != SortedPos)
  4733. SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
  4734. assert(SortedPos != AllNodes.end() && "Overran node list");
  4735. ++SortedPos;
  4736. } else {
  4737. // Update P's outstanding operand count.
  4738. P->setNodeId(Degree);
  4739. }
  4740. }
  4741. if (I == SortedPos) {
  4742. #ifndef NDEBUG
  4743. SDNode *S = ++I;
  4744. dbgs() << "Overran sorted position:\n";
  4745. S->dumprFull();
  4746. #endif
  4747. llvm_unreachable(0);
  4748. }
  4749. }
  4750. assert(SortedPos == AllNodes.end() &&
  4751. "Topological sort incomplete!");
  4752. assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
  4753. "First node in topological sort is not the entry token!");
  4754. assert(AllNodes.front().getNodeId() == 0 &&
  4755. "First node in topological sort has non-zero id!");
  4756. assert(AllNodes.front().getNumOperands() == 0 &&
  4757. "First node in topological sort has operands!");
  4758. assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
  4759. "Last node in topologic sort has unexpected id!");
  4760. assert(AllNodes.back().use_empty() &&
  4761. "Last node in topologic sort has users!");
  4762. assert(DAGSize == allnodes_size() && "Node count mismatch!");
  4763. return DAGSize;
  4764. }
  4765. /// AssignOrdering - Assign an order to the SDNode.
  4766. void SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) {
  4767. assert(SD && "Trying to assign an order to a null node!");
  4768. Ordering->add(SD, Order);
  4769. }
  4770. /// GetOrdering - Get the order for the SDNode.
  4771. unsigned SelectionDAG::GetOrdering(const SDNode *SD) const {
  4772. assert(SD && "Trying to get the order of a null node!");
  4773. return Ordering->getOrder(SD);
  4774. }
  4775. /// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
  4776. /// value is produced by SD.
  4777. void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
  4778. DbgInfo->add(DB, SD, isParameter);
  4779. if (SD)
  4780. SD->setHasDebugValue(true);
  4781. }
  4782. //===----------------------------------------------------------------------===//
  4783. // SDNode Class
  4784. //===----------------------------------------------------------------------===//
  4785. HandleSDNode::~HandleSDNode() {
  4786. DropOperands();
  4787. }
  4788. GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, DebugLoc DL,
  4789. const GlobalValue *GA,
  4790. EVT VT, int64_t o, unsigned char TF)
  4791. : SDNode(Opc, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
  4792. TheGlobal = GA;
  4793. }
  4794. MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
  4795. MachineMemOperand *mmo)
  4796. : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
  4797. SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
  4798. MMO->isNonTemporal());
  4799. assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
  4800. assert(isNonTemporal() == MMO->isNonTemporal() &&
  4801. "Non-temporal encoding error!");
  4802. assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
  4803. }
  4804. MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
  4805. const SDValue *Ops, unsigned NumOps, EVT memvt,
  4806. MachineMemOperand *mmo)
  4807. : SDNode(Opc, dl, VTs, Ops, NumOps),
  4808. MemoryVT(memvt), MMO(mmo) {
  4809. SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
  4810. MMO->isNonTemporal());
  4811. assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
  4812. assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
  4813. }
  4814. /// Profile - Gather unique data for the node.
  4815. ///
  4816. void SDNode::Profile(FoldingSetNodeID &ID) const {
  4817. AddNodeIDNode(ID, this);
  4818. }
  4819. namespace {
  4820. struct EVTArray {
  4821. std::vector<EVT> VTs;
  4822. EVTArray() {
  4823. VTs.reserve(MVT::LAST_VALUETYPE);
  4824. for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
  4825. VTs.push_back(MVT((MVT::SimpleValueType)i));
  4826. }
  4827. };
  4828. }
  4829. static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
  4830. static ManagedStatic<EVTArray> SimpleVTArray;
  4831. static ManagedStatic<sys::SmartMutex<true> > VTMutex;
  4832. /// getValueTypeList - Return a pointer to the specified value type.
  4833. ///
  4834. const EVT *SDNode::getValueTypeList(EVT VT) {
  4835. if (VT.isExtended()) {
  4836. sys::SmartScopedLock<true> Lock(*VTMutex);
  4837. return &(*EVTs->insert(VT).first);
  4838. } else {
  4839. assert(VT.getSimpleVT().SimpleTy < MVT::LAST_VALUETYPE &&
  4840. "Value type out of range!");
  4841. return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
  4842. }
  4843. }
  4844. /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
  4845. /// indicated value. This method ignores uses of other values defined by this
  4846. /// operation.
  4847. bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
  4848. assert(Value < getNumValues() && "Bad value!");
  4849. // TODO: Only iterate over uses of a given value of the node
  4850. for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
  4851. if (UI.getUse().getResNo() == Value) {
  4852. if (NUses == 0)
  4853. return false;
  4854. --NUses;
  4855. }
  4856. }
  4857. // Found exactly the right number of uses?
  4858. return NUses == 0;
  4859. }
  4860. /// hasAnyUseOfValue - Return true if there are any use of the indicated
  4861. /// value. This method ignores uses of other values defined by this operation.
  4862. bool SDNode::hasAnyUseOfValue(unsigned Value) const {
  4863. assert(Value < getNumValues() && "Bad value!");
  4864. for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
  4865. if (UI.getUse().getResNo() == Value)
  4866. return true;
  4867. return false;
  4868. }
  4869. /// isOnlyUserOf - Return true if this node is the only use of N.
  4870. ///
  4871. bool SDNode::isOnlyUserOf(SDNode *N) const {
  4872. bool Seen = false;
  4873. for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
  4874. SDNode *User = *I;
  4875. if (User == this)
  4876. Seen = true;
  4877. else
  4878. return false;
  4879. }
  4880. return Seen;
  4881. }
  4882. /// isOperand - Return true if this node is an operand of N.
  4883. ///
  4884. bool SDValue::isOperandOf(SDNode *N) const {
  4885. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
  4886. if (*this == N->getOperand(i))
  4887. return true;
  4888. return false;
  4889. }
  4890. bool SDNode::isOperandOf(SDNode *N) const {
  4891. for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
  4892. if (this == N->OperandList[i].getNode())
  4893. return true;
  4894. return false;
  4895. }
  4896. /// reachesChainWithoutSideEffects - Return true if this operand (which must
  4897. /// be a chain) reaches the specified operand without crossing any
  4898. /// side-effecting instructions. In practice, this looks through token
  4899. /// factors and non-volatile loads. In order to remain efficient, this only
  4900. /// looks a couple of nodes in, it does not do an exhaustive search.
  4901. bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
  4902. unsigned Depth) const {
  4903. if (*this == Dest) return true;
  4904. // Don't search too deeply, we just want to be able to see through
  4905. // TokenFactor's etc.
  4906. if (Depth == 0) return false;
  4907. // If this is a token factor, all inputs to the TF happen in parallel. If any
  4908. // of the operands of the TF reach dest, then we can do the xform.
  4909. if (getOpcode() == ISD::TokenFactor) {
  4910. for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
  4911. if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
  4912. return true;
  4913. return false;
  4914. }
  4915. // Loads don't have side effects, look through them.
  4916. if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
  4917. if (!Ld->isVolatile())
  4918. return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
  4919. }
  4920. return false;
  4921. }
  4922. /// isPredecessorOf - Return true if this node is a predecessor of N. This node
  4923. /// is either an operand of N or it can be reached by traversing up the operands.
  4924. /// NOTE: this is an expensive method. Use it carefully.
  4925. bool SDNode::isPredecessorOf(SDNode *N) const {
  4926. SmallPtrSet<SDNode *, 32> Visited;
  4927. SmallVector<SDNode *, 16> Worklist;
  4928. Worklist.push_back(N);
  4929. do {
  4930. N = Worklist.pop_back_val();
  4931. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  4932. SDNode *Op = N->getOperand(i).getNode();
  4933. if (Op == this)
  4934. return true;
  4935. if (Visited.insert(Op))
  4936. Worklist.push_back(Op);
  4937. }
  4938. } while (!Worklist.empty());
  4939. return false;
  4940. }
  4941. uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
  4942. assert(Num < NumOperands && "Invalid child # of SDNode!");
  4943. return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
  4944. }
  4945. std::string SDNode::getOperationName(const SelectionDAG *G) const {
  4946. switch (getOpcode()) {
  4947. default:
  4948. if (getOpcode() < ISD::BUILTIN_OP_END)
  4949. return "<<Unknown DAG Node>>";
  4950. if (isMachineOpcode()) {
  4951. if (G)
  4952. if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
  4953. if (getMachineOpcode() < TII->getNumOpcodes())
  4954. return TII->get(getMachineOpcode()).getName();
  4955. return "<<Unknown Machine Node #" + utostr(getOpcode()) + ">>";
  4956. }
  4957. if (G) {
  4958. const TargetLowering &TLI = G->getTargetLoweringInfo();
  4959. const char *Name = TLI.getTargetNodeName(getOpcode());
  4960. if (Name) return Name;
  4961. return "<<Unknown Target Node #" + utostr(getOpcode()) + ">>";
  4962. }
  4963. return "<<Unknown Node #" + utostr(getOpcode()) + ">>";
  4964. #ifndef NDEBUG
  4965. case ISD::DELETED_NODE:
  4966. return "<<Deleted Node!>>";
  4967. #endif
  4968. case ISD::PREFETCH: return "Prefetch";
  4969. case ISD::MEMBARRIER: return "MemBarrier";
  4970. case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
  4971. case ISD::ATOMIC_SWAP: return "AtomicSwap";
  4972. case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
  4973. case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
  4974. case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
  4975. case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
  4976. case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
  4977. case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
  4978. case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
  4979. case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
  4980. case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
  4981. case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
  4982. case ISD::PCMARKER: return "PCMarker";
  4983. case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
  4984. case ISD::SRCVALUE: return "SrcValue";
  4985. case ISD::MDNODE_SDNODE: return "MDNode";
  4986. case ISD::EntryToken: return "EntryToken";
  4987. case ISD::TokenFactor: return "TokenFactor";
  4988. case ISD::AssertSext: return "AssertSext";
  4989. case ISD::AssertZext: return "AssertZext";
  4990. case ISD::BasicBlock: return "BasicBlock";
  4991. case ISD::VALUETYPE: return "ValueType";
  4992. case ISD::Register: return "Register";
  4993. case ISD::Constant: return "Constant";
  4994. case ISD::ConstantFP: return "ConstantFP";
  4995. case ISD::GlobalAddress: return "GlobalAddress";
  4996. case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
  4997. case ISD::FrameIndex: return "FrameIndex";
  4998. case ISD::JumpTable: return "JumpTable";
  4999. case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
  5000. case ISD::RETURNADDR: return "RETURNADDR";
  5001. case ISD::FRAMEADDR: return "FRAMEADDR";
  5002. case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
  5003. case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
  5004. case ISD::LSDAADDR: return "LSDAADDR";
  5005. case ISD::EHSELECTION: return "EHSELECTION";
  5006. case ISD::EH_RETURN: return "EH_RETURN";
  5007. case ISD::EH_SJLJ_SETJMP: return "EH_SJLJ_SETJMP";
  5008. case ISD::EH_SJLJ_LONGJMP: return "EH_SJLJ_LONGJMP";
  5009. case ISD::ConstantPool: return "ConstantPool";
  5010. case ISD::ExternalSymbol: return "ExternalSymbol";
  5011. case ISD::BlockAddress: return "BlockAddress";
  5012. case ISD::INTRINSIC_WO_CHAIN:
  5013. case ISD::INTRINSIC_VOID:
  5014. case ISD::INTRINSIC_W_CHAIN: {
  5015. unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1;
  5016. unsigned IID = cast<ConstantSDNode>(getOperand(OpNo))->getZExtValue();
  5017. if (IID < Intrinsic::num_intrinsics)
  5018. return Intrinsic::getName((Intrinsic::ID)IID);
  5019. else if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo())
  5020. return TII->getName(IID);
  5021. llvm_unreachable("Invalid intrinsic ID");
  5022. }
  5023. case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
  5024. case ISD::TargetConstant: return "TargetConstant";
  5025. case ISD::TargetConstantFP:return "TargetConstantFP";
  5026. case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
  5027. case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
  5028. case ISD::TargetFrameIndex: return "TargetFrameIndex";
  5029. case ISD::TargetJumpTable: return "TargetJumpTable";
  5030. case ISD::TargetConstantPool: return "TargetConstantPool";
  5031. case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
  5032. case ISD::TargetBlockAddress: return "TargetBlockAddress";
  5033. case ISD::CopyToReg: return "CopyToReg";
  5034. case ISD::CopyFromReg: return "CopyFromReg";
  5035. case ISD::UNDEF: return "undef";
  5036. case ISD::MERGE_VALUES: return "merge_values";
  5037. case ISD::INLINEASM: return "inlineasm";
  5038. case ISD::EH_LABEL: return "eh_label";
  5039. case ISD::HANDLENODE: return "handlenode";
  5040. // Unary operators
  5041. case ISD::FABS: return "fabs";
  5042. case ISD::FNEG: return "fneg";
  5043. case ISD::FSQRT: return "fsqrt";
  5044. case ISD::FSIN: return "fsin";
  5045. case ISD::FCOS: return "fcos";
  5046. case ISD::FTRUNC: return "ftrunc";
  5047. case ISD::FFLOOR: return "ffloor";
  5048. case ISD::FCEIL: return "fceil";
  5049. case ISD::FRINT: return "frint";
  5050. case ISD::FNEARBYINT: return "fnearbyint";
  5051. case ISD::FEXP: return "fexp";
  5052. case ISD::FEXP2: return "fexp2";
  5053. case ISD::FLOG: return "flog";
  5054. case ISD::FLOG2: return "flog2";
  5055. case ISD::FLOG10: return "flog10";
  5056. // Binary operators
  5057. case ISD::ADD: return "add";
  5058. case ISD::SUB: return "sub";
  5059. case ISD::MUL: return "mul";
  5060. case ISD::MULHU: return "mulhu";
  5061. case ISD::MULHS: return "mulhs";
  5062. case ISD::SDIV: return "sdiv";
  5063. case ISD::UDIV: return "udiv";
  5064. case ISD::SREM: return "srem";
  5065. case ISD::UREM: return "urem";
  5066. case ISD::SMUL_LOHI: return "smul_lohi";
  5067. case ISD::UMUL_LOHI: return "umul_lohi";
  5068. case ISD::SDIVREM: return "sdivrem";
  5069. case ISD::UDIVREM: return "udivrem";
  5070. case ISD::AND: return "and";
  5071. case ISD::OR: return "or";
  5072. case ISD::XOR: return "xor";
  5073. case ISD::SHL: return "shl";
  5074. case ISD::SRA: return "sra";
  5075. case ISD::SRL: return "srl";
  5076. case ISD::ROTL: return "rotl";
  5077. case ISD::ROTR: return "rotr";
  5078. case ISD::FADD: return "fadd";
  5079. case ISD::FSUB: return "fsub";
  5080. case ISD::FMUL: return "fmul";
  5081. case ISD::FDIV: return "fdiv";
  5082. case ISD::FREM: return "frem";
  5083. case ISD::FCOPYSIGN: return "fcopysign";
  5084. case ISD::FGETSIGN: return "fgetsign";
  5085. case ISD::FPOW: return "fpow";
  5086. case ISD::FPOWI: return "fpowi";
  5087. case ISD::SETCC: return "setcc";
  5088. case ISD::VSETCC: return "vsetcc";
  5089. case ISD::SELECT: return "select";
  5090. case ISD::SELECT_CC: return "select_cc";
  5091. case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
  5092. case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
  5093. case ISD::CONCAT_VECTORS: return "concat_vectors";
  5094. case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
  5095. case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
  5096. case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
  5097. case ISD::CARRY_FALSE: return "carry_false";
  5098. case ISD::ADDC: return "addc";
  5099. case ISD::ADDE: return "adde";
  5100. case ISD::SADDO: return "saddo";
  5101. case ISD::UADDO: return "uaddo";
  5102. case ISD::SSUBO: return "ssubo";
  5103. case ISD::USUBO: return "usubo";
  5104. case ISD::SMULO: return "smulo";
  5105. case ISD::UMULO: return "umulo";
  5106. case ISD::SUBC: return "subc";
  5107. case ISD::SUBE: return "sube";
  5108. case ISD::SHL_PARTS: return "shl_parts";
  5109. case ISD::SRA_PARTS: return "sra_parts";
  5110. case ISD::SRL_PARTS: return "srl_parts";
  5111. // Conversion operators.
  5112. case ISD::SIGN_EXTEND: return "sign_extend";
  5113. case ISD::ZERO_EXTEND: return "zero_extend";
  5114. case ISD::ANY_EXTEND: return "any_extend";
  5115. case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
  5116. case ISD::TRUNCATE: return "truncate";
  5117. case ISD::FP_ROUND: return "fp_round";
  5118. case ISD::FLT_ROUNDS_: return "flt_rounds";
  5119. case ISD::FP_ROUND_INREG: return "fp_round_inreg";
  5120. case ISD::FP_EXTEND: return "fp_extend";
  5121. case ISD::SINT_TO_FP: return "sint_to_fp";
  5122. case ISD::UINT_TO_FP: return "uint_to_fp";
  5123. case ISD::FP_TO_SINT: return "fp_to_sint";
  5124. case ISD::FP_TO_UINT: return "fp_to_uint";
  5125. case ISD::BIT_CONVERT: return "bit_convert";
  5126. case ISD::FP16_TO_FP32: return "fp16_to_fp32";
  5127. case ISD::FP32_TO_FP16: return "fp32_to_fp16";
  5128. case ISD::CONVERT_RNDSAT: {
  5129. switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
  5130. default: llvm_unreachable("Unknown cvt code!");
  5131. case ISD::CVT_FF: return "cvt_ff";
  5132. case ISD::CVT_FS: return "cvt_fs";
  5133. case ISD::CVT_FU: return "cvt_fu";
  5134. case ISD::CVT_SF: return "cvt_sf";
  5135. case ISD::CVT_UF: return "cvt_uf";
  5136. case ISD::CVT_SS: return "cvt_ss";
  5137. case ISD::CVT_SU: return "cvt_su";
  5138. case ISD::CVT_US: return "cvt_us";
  5139. case ISD::CVT_UU: return "cvt_uu";
  5140. }
  5141. }
  5142. // Control flow instructions
  5143. case ISD::BR: return "br";
  5144. case ISD::BRIND: return "brind";
  5145. case ISD::BR_JT: return "br_jt";
  5146. case ISD::BRCOND: return "brcond";
  5147. case ISD::BR_CC: return "br_cc";
  5148. case ISD::CALLSEQ_START: return "callseq_start";
  5149. case ISD::CALLSEQ_END: return "callseq_end";
  5150. // Other operators
  5151. case ISD::LOAD: return "load";
  5152. case ISD::STORE: return "store";
  5153. case ISD::VAARG: return "vaarg";
  5154. case ISD::VACOPY: return "vacopy";
  5155. case ISD::VAEND: return "vaend";
  5156. case ISD::VASTART: return "vastart";
  5157. case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
  5158. case ISD::EXTRACT_ELEMENT: return "extract_element";
  5159. case ISD::BUILD_PAIR: return "build_pair";
  5160. case ISD::STACKSAVE: return "stacksave";
  5161. case ISD::STACKRESTORE: return "stackrestore";
  5162. case ISD::TRAP: return "trap";
  5163. // Bit manipulation
  5164. case ISD::BSWAP: return "bswap";
  5165. case ISD::CTPOP: return "ctpop";
  5166. case ISD::CTTZ: return "cttz";
  5167. case ISD::CTLZ: return "ctlz";
  5168. // Trampolines
  5169. case ISD::TRAMPOLINE: return "trampoline";
  5170. case ISD::CONDCODE:
  5171. switch (cast<CondCodeSDNode>(this)->get()) {
  5172. default: llvm_unreachable("Unknown setcc condition!");
  5173. case ISD::SETOEQ: return "setoeq";
  5174. case ISD::SETOGT: return "setogt";
  5175. case ISD::SETOGE: return "setoge";
  5176. case ISD::SETOLT: return "setolt";
  5177. case ISD::SETOLE: return "setole";
  5178. case ISD::SETONE: return "setone";
  5179. case ISD::SETO: return "seto";
  5180. case ISD::SETUO: return "setuo";
  5181. case ISD::SETUEQ: return "setue";
  5182. case ISD::SETUGT: return "setugt";
  5183. case ISD::SETUGE: return "setuge";
  5184. case ISD::SETULT: return "setult";
  5185. case ISD::SETULE: return "setule";
  5186. case ISD::SETUNE: return "setune";
  5187. case ISD::SETEQ: return "seteq";
  5188. case ISD::SETGT: return "setgt";
  5189. case ISD::SETGE: return "setge";
  5190. case ISD::SETLT: return "setlt";
  5191. case ISD::SETLE: return "setle";
  5192. case ISD::SETNE: return "setne";
  5193. }
  5194. }
  5195. }
  5196. const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
  5197. switch (AM) {
  5198. default:
  5199. return "";
  5200. case ISD::PRE_INC:
  5201. return "<pre-inc>";
  5202. case ISD::PRE_DEC:
  5203. return "<pre-dec>";
  5204. case ISD::POST_INC:
  5205. return "<post-inc>";
  5206. case ISD::POST_DEC:
  5207. return "<post-dec>";
  5208. }
  5209. }
  5210. std::string ISD::ArgFlagsTy::getArgFlagsString() {
  5211. std::string S = "< ";
  5212. if (isZExt())
  5213. S += "zext ";
  5214. if (isSExt())
  5215. S += "sext ";
  5216. if (isInReg())
  5217. S += "inreg ";
  5218. if (isSRet())
  5219. S += "sret ";
  5220. if (isByVal())
  5221. S += "byval ";
  5222. if (isNest())
  5223. S += "nest ";
  5224. if (getByValAlign())
  5225. S += "byval-align:" + utostr(getByValAlign()) + " ";
  5226. if (getOrigAlign())
  5227. S += "orig-align:" + utostr(getOrigAlign()) + " ";
  5228. if (getByValSize())
  5229. S += "byval-size:" + utostr(getByValSize()) + " ";
  5230. return S + ">";
  5231. }
  5232. void SDNode::dump() const { dump(0); }
  5233. void SDNode::dump(const SelectionDAG *G) const {
  5234. print(dbgs(), G);
  5235. }
  5236. void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
  5237. OS << (void*)this << ": ";
  5238. for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
  5239. if (i) OS << ",";
  5240. if (getValueType(i) == MVT::Other)
  5241. OS << "ch";
  5242. else
  5243. OS << getValueType(i).getEVTString();
  5244. }
  5245. OS << " = " << getOperationName(G);
  5246. }
  5247. void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
  5248. if (const MachineSDNode *MN = dyn_cast<MachineSDNode>(this)) {
  5249. if (!MN->memoperands_empty()) {
  5250. OS << "<";
  5251. OS << "Mem:";
  5252. for (MachineSDNode::mmo_iterator i = MN->memoperands_begin(),
  5253. e = MN->memoperands_end(); i != e; ++i) {
  5254. OS << **i;
  5255. if (next(i) != e)
  5256. OS << " ";
  5257. }
  5258. OS << ">";
  5259. }
  5260. } else if (const ShuffleVectorSDNode *SVN =
  5261. dyn_cast<ShuffleVectorSDNode>(this)) {
  5262. OS << "<";
  5263. for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) {
  5264. int Idx = SVN->getMaskElt(i);
  5265. if (i) OS << ",";
  5266. if (Idx < 0)
  5267. OS << "u";
  5268. else
  5269. OS << Idx;
  5270. }
  5271. OS << ">";
  5272. } else if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
  5273. OS << '<' << CSDN->getAPIntValue() << '>';
  5274. } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
  5275. if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
  5276. OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
  5277. else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
  5278. OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
  5279. else {
  5280. OS << "<APFloat(";
  5281. CSDN->getValueAPF().bitcastToAPInt().dump();
  5282. OS << ")>";
  5283. }
  5284. } else if (const GlobalAddressSDNode *GADN =
  5285. dyn_cast<GlobalAddressSDNode>(this)) {
  5286. int64_t offset = GADN->getOffset();
  5287. OS << '<';
  5288. WriteAsOperand(OS, GADN->getGlobal());
  5289. OS << '>';
  5290. if (offset > 0)
  5291. OS << " + " << offset;
  5292. else
  5293. OS << " " << offset;
  5294. if (unsigned int TF = GADN->getTargetFlags())
  5295. OS << " [TF=" << TF << ']';
  5296. } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
  5297. OS << "<" << FIDN->getIndex() << ">";
  5298. } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
  5299. OS << "<" << JTDN->getIndex() << ">";
  5300. if (unsigned int TF = JTDN->getTargetFlags())
  5301. OS << " [TF=" << TF << ']';
  5302. } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
  5303. int offset = CP->getOffset();
  5304. if (CP->isMachineConstantPoolEntry())
  5305. OS << "<" << *CP->getMachineCPVal() << ">";
  5306. else
  5307. OS << "<" << *CP->getConstVal() << ">";
  5308. if (offset > 0)
  5309. OS << " + " << offset;
  5310. else
  5311. OS << " " << offset;
  5312. if (unsigned int TF = CP->getTargetFlags())
  5313. OS << " [TF=" << TF << ']';
  5314. } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
  5315. OS << "<";
  5316. const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
  5317. if (LBB)
  5318. OS << LBB->getName() << " ";
  5319. OS << (const void*)BBDN->getBasicBlock() << ">";
  5320. } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
  5321. if (G && R->getReg() &&
  5322. TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
  5323. OS << " %" << G->getTarget().getRegisterInfo()->getName(R->getReg());
  5324. } else {
  5325. OS << " %reg" << R->getReg();
  5326. }
  5327. } else if (const ExternalSymbolSDNode *ES =
  5328. dyn_cast<ExternalSymbolSDNode>(this)) {
  5329. OS << "'" << ES->getSymbol() << "'";
  5330. if (unsigned int TF = ES->getTargetFlags())
  5331. OS << " [TF=" << TF << ']';
  5332. } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
  5333. if (M->getValue())
  5334. OS << "<" << M->getValue() << ">";
  5335. else
  5336. OS << "<null>";
  5337. } else if (const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(this)) {
  5338. if (MD->getMD())
  5339. OS << "<" << MD->getMD() << ">";
  5340. else
  5341. OS << "<null>";
  5342. } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
  5343. OS << ":" << N->getVT().getEVTString();
  5344. }
  5345. else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
  5346. OS << "<" << *LD->getMemOperand();
  5347. bool doExt = true;
  5348. switch (LD->getExtensionType()) {
  5349. default: doExt = false; break;
  5350. case ISD::EXTLOAD: OS << ", anyext"; break;
  5351. case ISD::SEXTLOAD: OS << ", sext"; break;
  5352. case ISD::ZEXTLOAD: OS << ", zext"; break;
  5353. }
  5354. if (doExt)
  5355. OS << " from " << LD->getMemoryVT().getEVTString();
  5356. const char *AM = getIndexedModeName(LD->getAddressingMode());
  5357. if (*AM)
  5358. OS << ", " << AM;
  5359. OS << ">";
  5360. } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
  5361. OS << "<" << *ST->getMemOperand();
  5362. if (ST->isTruncatingStore())
  5363. OS << ", trunc to " << ST->getMemoryVT().getEVTString();
  5364. const char *AM = getIndexedModeName(ST->getAddressingMode());
  5365. if (*AM)
  5366. OS << ", " << AM;
  5367. OS << ">";
  5368. } else if (const MemSDNode* M = dyn_cast<MemSDNode>(this)) {
  5369. OS << "<" << *M->getMemOperand() << ">";
  5370. } else if (const BlockAddressSDNode *BA =
  5371. dyn_cast<BlockAddressSDNode>(this)) {
  5372. OS << "<";
  5373. WriteAsOperand(OS, BA->getBlockAddress()->getFunction(), false);
  5374. OS << ", ";
  5375. WriteAsOperand(OS, BA->getBlockAddress()->getBasicBlock(), false);
  5376. OS << ">";
  5377. if (unsigned int TF = BA->getTargetFlags())
  5378. OS << " [TF=" << TF << ']';
  5379. }
  5380. if (G)
  5381. if (unsigned Order = G->GetOrdering(this))
  5382. OS << " [ORD=" << Order << ']';
  5383. if (getNodeId() != -1)
  5384. OS << " [ID=" << getNodeId() << ']';
  5385. DebugLoc dl = getDebugLoc();
  5386. if (G && !dl.isUnknown()) {
  5387. DIScope
  5388. Scope(dl.getScope(G->getMachineFunction().getFunction()->getContext()));
  5389. OS << " dbg:";
  5390. // Omit the directory, since it's usually long and uninteresting.
  5391. if (Scope.Verify())
  5392. OS << Scope.getFilename();
  5393. else
  5394. OS << "<unknown>";
  5395. OS << ':' << dl.getLine();
  5396. if (dl.getCol() != 0)
  5397. OS << ':' << dl.getCol();
  5398. }
  5399. }
  5400. void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
  5401. print_types(OS, G);
  5402. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  5403. if (i) OS << ", "; else OS << " ";
  5404. OS << (void*)getOperand(i).getNode();
  5405. if (unsigned RN = getOperand(i).getResNo())
  5406. OS << ":" << RN;
  5407. }
  5408. print_details(OS, G);
  5409. }
  5410. static void printrWithDepthHelper(raw_ostream &OS, const SDNode *N,
  5411. const SelectionDAG *G, unsigned depth,
  5412. unsigned indent)
  5413. {
  5414. if (depth == 0)
  5415. return;
  5416. OS.indent(indent);
  5417. N->print(OS, G);
  5418. if (depth < 1)
  5419. return;
  5420. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  5421. OS << '\n';
  5422. printrWithDepthHelper(OS, N->getOperand(i).getNode(), G, depth-1, indent+2);
  5423. }
  5424. }
  5425. void SDNode::printrWithDepth(raw_ostream &OS, const SelectionDAG *G,
  5426. unsigned depth) const {
  5427. printrWithDepthHelper(OS, this, G, depth, 0);
  5428. }
  5429. void SDNode::printrFull(raw_ostream &OS, const SelectionDAG *G) const {
  5430. // Don't print impossibly deep things.
  5431. printrWithDepth(OS, G, 100);
  5432. }
  5433. void SDNode::dumprWithDepth(const SelectionDAG *G, unsigned depth) const {
  5434. printrWithDepth(dbgs(), G, depth);
  5435. }
  5436. void SDNode::dumprFull(const SelectionDAG *G) const {
  5437. // Don't print impossibly deep things.
  5438. dumprWithDepth(G, 100);
  5439. }
  5440. static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
  5441. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
  5442. if (N->getOperand(i).getNode()->hasOneUse())
  5443. DumpNodes(N->getOperand(i).getNode(), indent+2, G);
  5444. else
  5445. dbgs() << "\n" << std::string(indent+2, ' ')
  5446. << (void*)N->getOperand(i).getNode() << ": <multiple use>";
  5447. dbgs() << "\n";
  5448. dbgs().indent(indent);
  5449. N->dump(G);
  5450. }
  5451. SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
  5452. assert(N->getNumValues() == 1 &&
  5453. "Can't unroll a vector with multiple results!");
  5454. EVT VT = N->getValueType(0);
  5455. unsigned NE = VT.getVectorNumElements();
  5456. EVT EltVT = VT.getVectorElementType();
  5457. DebugLoc dl = N->getDebugLoc();
  5458. SmallVector<SDValue, 8> Scalars;
  5459. SmallVector<SDValue, 4> Operands(N->getNumOperands());
  5460. // If ResNE is 0, fully unroll the vector op.
  5461. if (ResNE == 0)
  5462. ResNE = NE;
  5463. else if (NE > ResNE)
  5464. NE = ResNE;
  5465. unsigned i;
  5466. for (i= 0; i != NE; ++i) {
  5467. for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
  5468. SDValue Operand = N->getOperand(j);
  5469. EVT OperandVT = Operand.getValueType();
  5470. if (OperandVT.isVector()) {
  5471. // A vector operand; extract a single element.
  5472. EVT OperandEltVT = OperandVT.getVectorElementType();
  5473. Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
  5474. OperandEltVT,
  5475. Operand,
  5476. getConstant(i, MVT::i32));
  5477. } else {
  5478. // A scalar operand; just use it as is.
  5479. Operands[j] = Operand;
  5480. }
  5481. }
  5482. switch (N->getOpcode()) {
  5483. default:
  5484. Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
  5485. &Operands[0], Operands.size()));
  5486. break;
  5487. case ISD::SHL:
  5488. case ISD::SRA:
  5489. case ISD::SRL:
  5490. case ISD::ROTL:
  5491. case ISD::ROTR:
  5492. Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
  5493. getShiftAmountOperand(Operands[1])));
  5494. break;
  5495. case ISD::SIGN_EXTEND_INREG:
  5496. case ISD::FP_ROUND_INREG: {
  5497. EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
  5498. Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
  5499. Operands[0],
  5500. getValueType(ExtVT)));
  5501. }
  5502. }
  5503. }
  5504. for (; i < ResNE; ++i)
  5505. Scalars.push_back(getUNDEF(EltVT));
  5506. return getNode(ISD::BUILD_VECTOR, dl,
  5507. EVT::getVectorVT(*getContext(), EltVT, ResNE),
  5508. &Scalars[0], Scalars.size());
  5509. }
  5510. /// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
  5511. /// location that is 'Dist' units away from the location that the 'Base' load
  5512. /// is loading from.
  5513. bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
  5514. unsigned Bytes, int Dist) const {
  5515. if (LD->getChain() != Base->getChain())
  5516. return false;
  5517. EVT VT = LD->getValueType(0);
  5518. if (VT.getSizeInBits() / 8 != Bytes)
  5519. return false;
  5520. SDValue Loc = LD->getOperand(1);
  5521. SDValue BaseLoc = Base->getOperand(1);
  5522. if (Loc.getOpcode() == ISD::FrameIndex) {
  5523. if (BaseLoc.getOpcode() != ISD::FrameIndex)
  5524. return false;
  5525. const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
  5526. int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
  5527. int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
  5528. int FS = MFI->getObjectSize(FI);
  5529. int BFS = MFI->getObjectSize(BFI);
  5530. if (FS != BFS || FS != (int)Bytes) return false;
  5531. return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
  5532. }
  5533. if (Loc.getOpcode() == ISD::ADD && Loc.getOperand(0) == BaseLoc) {
  5534. ConstantSDNode *V = dyn_cast<ConstantSDNode>(Loc.getOperand(1));
  5535. if (V && (V->getSExtValue() == Dist*Bytes))
  5536. return true;
  5537. }
  5538. const GlobalValue *GV1 = NULL;
  5539. const GlobalValue *GV2 = NULL;
  5540. int64_t Offset1 = 0;
  5541. int64_t Offset2 = 0;
  5542. bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
  5543. bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
  5544. if (isGA1 && isGA2 && GV1 == GV2)
  5545. return Offset1 == (Offset2 + Dist*Bytes);
  5546. return false;
  5547. }
  5548. /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
  5549. /// it cannot be inferred.
  5550. unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
  5551. // If this is a GlobalAddress + cst, return the alignment.
  5552. const GlobalValue *GV;
  5553. int64_t GVOffset = 0;
  5554. if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
  5555. // If GV has specified alignment, then use it. Otherwise, use the preferred
  5556. // alignment.
  5557. unsigned Align = GV->getAlignment();
  5558. if (!Align) {
  5559. if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
  5560. if (GVar->hasInitializer()) {
  5561. const TargetData *TD = TLI.getTargetData();
  5562. Align = TD->getPreferredAlignment(GVar);
  5563. }
  5564. }
  5565. }
  5566. return MinAlign(Align, GVOffset);
  5567. }
  5568. // If this is a direct reference to a stack slot, use information about the
  5569. // stack slot's alignment.
  5570. int FrameIdx = 1 << 31;
  5571. int64_t FrameOffset = 0;
  5572. if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
  5573. FrameIdx = FI->getIndex();
  5574. } else if (Ptr.getOpcode() == ISD::ADD &&
  5575. isa<ConstantSDNode>(Ptr.getOperand(1)) &&
  5576. isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
  5577. FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
  5578. FrameOffset = Ptr.getConstantOperandVal(1);
  5579. }
  5580. if (FrameIdx != (1 << 31)) {
  5581. // FIXME: Handle FI+CST.
  5582. const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
  5583. unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
  5584. FrameOffset);
  5585. return FIInfoAlign;
  5586. }
  5587. return 0;
  5588. }
  5589. void SelectionDAG::dump() const {
  5590. dbgs() << "SelectionDAG has " << AllNodes.size() << " nodes:";
  5591. for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
  5592. I != E; ++I) {
  5593. const SDNode *N = I;
  5594. if (!N->hasOneUse() && N != getRoot().getNode())
  5595. DumpNodes(N, 2, this);
  5596. }
  5597. if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
  5598. dbgs() << "\n\n";
  5599. }
  5600. void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
  5601. print_types(OS, G);
  5602. print_details(OS, G);
  5603. }
  5604. typedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet;
  5605. static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
  5606. const SelectionDAG *G, VisitedSDNodeSet &once) {
  5607. if (!once.insert(N)) // If we've been here before, return now.
  5608. return;
  5609. // Dump the current SDNode, but don't end the line yet.
  5610. OS << std::string(indent, ' ');
  5611. N->printr(OS, G);
  5612. // Having printed this SDNode, walk the children:
  5613. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  5614. const SDNode *child = N->getOperand(i).getNode();
  5615. if (i) OS << ",";
  5616. OS << " ";
  5617. if (child->getNumOperands() == 0) {
  5618. // This child has no grandchildren; print it inline right here.
  5619. child->printr(OS, G);
  5620. once.insert(child);
  5621. } else { // Just the address. FIXME: also print the child's opcode.
  5622. OS << (void*)child;
  5623. if (unsigned RN = N->getOperand(i).getResNo())
  5624. OS << ":" << RN;
  5625. }
  5626. }
  5627. OS << "\n";
  5628. // Dump children that have grandchildren on their own line(s).
  5629. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  5630. const SDNode *child = N->getOperand(i).getNode();
  5631. DumpNodesr(OS, child, indent+2, G, once);
  5632. }
  5633. }
  5634. void SDNode::dumpr() const {
  5635. VisitedSDNodeSet once;
  5636. DumpNodesr(dbgs(), this, 0, 0, once);
  5637. }
  5638. void SDNode::dumpr(const SelectionDAG *G) const {
  5639. VisitedSDNodeSet once;
  5640. DumpNodesr(dbgs(), this, 0, G, once);
  5641. }
  5642. // getAddressSpace - Return the address space this GlobalAddress belongs to.
  5643. unsigned GlobalAddressSDNode::getAddressSpace() const {
  5644. return getGlobal()->getType()->getAddressSpace();
  5645. }
  5646. const Type *ConstantPoolSDNode::getType() const {
  5647. if (isMachineConstantPoolEntry())
  5648. return Val.MachineCPVal->getType();
  5649. return Val.ConstVal->getType();
  5650. }
  5651. bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
  5652. APInt &SplatUndef,
  5653. unsigned &SplatBitSize,
  5654. bool &HasAnyUndefs,
  5655. unsigned MinSplatBits,
  5656. bool isBigEndian) {
  5657. EVT VT = getValueType(0);
  5658. assert(VT.isVector() && "Expected a vector type");
  5659. unsigned sz = VT.getSizeInBits();
  5660. if (MinSplatBits > sz)
  5661. return false;
  5662. SplatValue = APInt(sz, 0);
  5663. SplatUndef = APInt(sz, 0);
  5664. // Get the bits. Bits with undefined values (when the corresponding element
  5665. // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
  5666. // in SplatValue. If any of the values are not constant, give up and return
  5667. // false.
  5668. unsigned int nOps = getNumOperands();
  5669. assert(nOps > 0 && "isConstantSplat has 0-size build vector");
  5670. unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
  5671. for (unsigned j = 0; j < nOps; ++j) {
  5672. unsigned i = isBigEndian ? nOps-1-j : j;
  5673. SDValue OpVal = getOperand(i);
  5674. unsigned BitPos = j * EltBitSize;
  5675. if (OpVal.getOpcode() == ISD::UNDEF)
  5676. SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
  5677. else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
  5678. SplatValue |= APInt(CN->getAPIntValue()).zextOrTrunc(EltBitSize).
  5679. zextOrTrunc(sz) << BitPos;
  5680. else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
  5681. SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
  5682. else
  5683. return false;
  5684. }
  5685. // The build_vector is all constants or undefs. Find the smallest element
  5686. // size that splats the vector.
  5687. HasAnyUndefs = (SplatUndef != 0);
  5688. while (sz > 8) {
  5689. unsigned HalfSize = sz / 2;
  5690. APInt HighValue = APInt(SplatValue).lshr(HalfSize).trunc(HalfSize);
  5691. APInt LowValue = APInt(SplatValue).trunc(HalfSize);
  5692. APInt HighUndef = APInt(SplatUndef).lshr(HalfSize).trunc(HalfSize);
  5693. APInt LowUndef = APInt(SplatUndef).trunc(HalfSize);
  5694. // If the two halves do not match (ignoring undef bits), stop here.
  5695. if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
  5696. MinSplatBits > HalfSize)
  5697. break;
  5698. SplatValue = HighValue | LowValue;
  5699. SplatUndef = HighUndef & LowUndef;
  5700. sz = HalfSize;
  5701. }
  5702. SplatBitSize = sz;
  5703. return true;
  5704. }
  5705. bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
  5706. // Find the first non-undef value in the shuffle mask.
  5707. unsigned i, e;
  5708. for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
  5709. /* search */;
  5710. assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
  5711. // Make sure all remaining elements are either undef or the same as the first
  5712. // non-undef value.
  5713. for (int Idx = Mask[i]; i != e; ++i)
  5714. if (Mask[i] >= 0 && Mask[i] != Idx)
  5715. return false;
  5716. return true;
  5717. }
  5718. #ifdef XDEBUG
  5719. static void checkForCyclesHelper(const SDNode *N,
  5720. SmallPtrSet<const SDNode*, 32> &Visited,
  5721. SmallPtrSet<const SDNode*, 32> &Checked) {
  5722. // If this node has already been checked, don't check it again.
  5723. if (Checked.count(N))
  5724. return;
  5725. // If a node has already been visited on this depth-first walk, reject it as
  5726. // a cycle.
  5727. if (!Visited.insert(N)) {
  5728. dbgs() << "Offending node:\n";
  5729. N->dumprFull();
  5730. errs() << "Detected cycle in SelectionDAG\n";
  5731. abort();
  5732. }
  5733. for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
  5734. checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
  5735. Checked.insert(N);
  5736. Visited.erase(N);
  5737. }
  5738. #endif
  5739. void llvm::checkForCycles(const llvm::SDNode *N) {
  5740. #ifdef XDEBUG
  5741. assert(N && "Checking nonexistant SDNode");
  5742. SmallPtrSet<const SDNode*, 32> visited;
  5743. SmallPtrSet<const SDNode*, 32> checked;
  5744. checkForCyclesHelper(N, visited, checked);
  5745. #endif
  5746. }
  5747. void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
  5748. checkForCycles(DAG->getRoot().getNode());
  5749. }