SelectionDAG.cpp 208 KB

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