combine.c 457 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257
  1. /* Optimize by combining instructions for GNU compiler.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This module is essentially the "combiner" phase of the U. of Arizona
  16. Portable Optimizer, but redone to work on our list-structured
  17. representation for RTL instead of their string representation.
  18. The LOG_LINKS of each insn identify the most recent assignment
  19. to each REG used in the insn. It is a list of previous insns,
  20. each of which contains a SET for a REG that is used in this insn
  21. and not used or set in between. LOG_LINKs never cross basic blocks.
  22. They were set up by the preceding pass (lifetime analysis).
  23. We try to combine each pair of insns joined by a logical link.
  24. We also try to combine triplets of insns A, B and C when C has
  25. a link back to B and B has a link back to A. Likewise for a
  26. small number of quadruplets of insns A, B, C and D for which
  27. there's high likelihood of of success.
  28. LOG_LINKS does not have links for use of the CC0. They don't
  29. need to, because the insn that sets the CC0 is always immediately
  30. before the insn that tests it. So we always regard a branch
  31. insn as having a logical link to the preceding insn. The same is true
  32. for an insn explicitly using CC0.
  33. We check (with use_crosses_set_p) to avoid combining in such a way
  34. as to move a computation to a place where its value would be different.
  35. Combination is done by mathematically substituting the previous
  36. insn(s) values for the regs they set into the expressions in
  37. the later insns that refer to these regs. If the result is a valid insn
  38. for our target machine, according to the machine description,
  39. we install it, delete the earlier insns, and update the data flow
  40. information (LOG_LINKS and REG_NOTES) for what we did.
  41. There are a few exceptions where the dataflow information isn't
  42. completely updated (however this is only a local issue since it is
  43. regenerated before the next pass that uses it):
  44. - reg_live_length is not updated
  45. - reg_n_refs is not adjusted in the rare case when a register is
  46. no longer required in a computation
  47. - there are extremely rare cases (see distribute_notes) when a
  48. REG_DEAD note is lost
  49. - a LOG_LINKS entry that refers to an insn with multiple SETs may be
  50. removed because there is no way to know which register it was
  51. linking
  52. To simplify substitution, we combine only when the earlier insn(s)
  53. consist of only a single assignment. To simplify updating afterward,
  54. we never combine when a subroutine call appears in the middle.
  55. Since we do not represent assignments to CC0 explicitly except when that
  56. is all an insn does, there is no LOG_LINKS entry in an insn that uses
  57. the condition code for the insn that set the condition code.
  58. Fortunately, these two insns must be consecutive.
  59. Therefore, every JUMP_INSN is taken to have an implicit logical link
  60. to the preceding insn. This is not quite right, since non-jumps can
  61. also use the condition code; but in practice such insns would not
  62. combine anyway. */
  63. #include "config.h"
  64. #include "system.h"
  65. #include "coretypes.h"
  66. #include "tm.h"
  67. #include "rtl.h"
  68. #include "hash-set.h"
  69. #include "machmode.h"
  70. #include "vec.h"
  71. #include "double-int.h"
  72. #include "input.h"
  73. #include "alias.h"
  74. #include "symtab.h"
  75. #include "wide-int.h"
  76. #include "inchash.h"
  77. #include "tree.h"
  78. #include "stor-layout.h"
  79. #include "tm_p.h"
  80. #include "flags.h"
  81. #include "regs.h"
  82. #include "hard-reg-set.h"
  83. #include "predict.h"
  84. #include "function.h"
  85. #include "dominance.h"
  86. #include "cfg.h"
  87. #include "cfgrtl.h"
  88. #include "cfgcleanup.h"
  89. #include "basic-block.h"
  90. #include "insn-config.h"
  91. /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
  92. #include "hashtab.h"
  93. #include "statistics.h"
  94. #include "real.h"
  95. #include "fixed-value.h"
  96. #include "expmed.h"
  97. #include "dojump.h"
  98. #include "explow.h"
  99. #include "calls.h"
  100. #include "emit-rtl.h"
  101. #include "varasm.h"
  102. #include "stmt.h"
  103. #include "expr.h"
  104. #include "insn-attr.h"
  105. #include "recog.h"
  106. #include "diagnostic-core.h"
  107. #include "target.h"
  108. #include "insn-codes.h"
  109. #include "optabs.h"
  110. #include "rtlhooks-def.h"
  111. #include "params.h"
  112. #include "tree-pass.h"
  113. #include "df.h"
  114. #include "valtrack.h"
  115. #include "hash-map.h"
  116. #include "is-a.h"
  117. #include "plugin-api.h"
  118. #include "ipa-ref.h"
  119. #include "cgraph.h"
  120. #include "obstack.h"
  121. #include "rtl-iter.h"
  122. /* Number of attempts to combine instructions in this function. */
  123. static int combine_attempts;
  124. /* Number of attempts that got as far as substitution in this function. */
  125. static int combine_merges;
  126. /* Number of instructions combined with added SETs in this function. */
  127. static int combine_extras;
  128. /* Number of instructions combined in this function. */
  129. static int combine_successes;
  130. /* Totals over entire compilation. */
  131. static int total_attempts, total_merges, total_extras, total_successes;
  132. /* combine_instructions may try to replace the right hand side of the
  133. second instruction with the value of an associated REG_EQUAL note
  134. before throwing it at try_combine. That is problematic when there
  135. is a REG_DEAD note for a register used in the old right hand side
  136. and can cause distribute_notes to do wrong things. This is the
  137. second instruction if it has been so modified, null otherwise. */
  138. static rtx_insn *i2mod;
  139. /* When I2MOD is nonnull, this is a copy of the old right hand side. */
  140. static rtx i2mod_old_rhs;
  141. /* When I2MOD is nonnull, this is a copy of the new right hand side. */
  142. static rtx i2mod_new_rhs;
  143. typedef struct reg_stat_struct {
  144. /* Record last point of death of (hard or pseudo) register n. */
  145. rtx_insn *last_death;
  146. /* Record last point of modification of (hard or pseudo) register n. */
  147. rtx_insn *last_set;
  148. /* The next group of fields allows the recording of the last value assigned
  149. to (hard or pseudo) register n. We use this information to see if an
  150. operation being processed is redundant given a prior operation performed
  151. on the register. For example, an `and' with a constant is redundant if
  152. all the zero bits are already known to be turned off.
  153. We use an approach similar to that used by cse, but change it in the
  154. following ways:
  155. (1) We do not want to reinitialize at each label.
  156. (2) It is useful, but not critical, to know the actual value assigned
  157. to a register. Often just its form is helpful.
  158. Therefore, we maintain the following fields:
  159. last_set_value the last value assigned
  160. last_set_label records the value of label_tick when the
  161. register was assigned
  162. last_set_table_tick records the value of label_tick when a
  163. value using the register is assigned
  164. last_set_invalid set to nonzero when it is not valid
  165. to use the value of this register in some
  166. register's value
  167. To understand the usage of these tables, it is important to understand
  168. the distinction between the value in last_set_value being valid and
  169. the register being validly contained in some other expression in the
  170. table.
  171. (The next two parameters are out of date).
  172. reg_stat[i].last_set_value is valid if it is nonzero, and either
  173. reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
  174. Register I may validly appear in any expression returned for the value
  175. of another register if reg_n_sets[i] is 1. It may also appear in the
  176. value for register J if reg_stat[j].last_set_invalid is zero, or
  177. reg_stat[i].last_set_label < reg_stat[j].last_set_label.
  178. If an expression is found in the table containing a register which may
  179. not validly appear in an expression, the register is replaced by
  180. something that won't match, (clobber (const_int 0)). */
  181. /* Record last value assigned to (hard or pseudo) register n. */
  182. rtx last_set_value;
  183. /* Record the value of label_tick when an expression involving register n
  184. is placed in last_set_value. */
  185. int last_set_table_tick;
  186. /* Record the value of label_tick when the value for register n is placed in
  187. last_set_value. */
  188. int last_set_label;
  189. /* These fields are maintained in parallel with last_set_value and are
  190. used to store the mode in which the register was last set, the bits
  191. that were known to be zero when it was last set, and the number of
  192. sign bits copies it was known to have when it was last set. */
  193. unsigned HOST_WIDE_INT last_set_nonzero_bits;
  194. char last_set_sign_bit_copies;
  195. ENUM_BITFIELD(machine_mode) last_set_mode : 8;
  196. /* Set nonzero if references to register n in expressions should not be
  197. used. last_set_invalid is set nonzero when this register is being
  198. assigned to and last_set_table_tick == label_tick. */
  199. char last_set_invalid;
  200. /* Some registers that are set more than once and used in more than one
  201. basic block are nevertheless always set in similar ways. For example,
  202. a QImode register may be loaded from memory in two places on a machine
  203. where byte loads zero extend.
  204. We record in the following fields if a register has some leading bits
  205. that are always equal to the sign bit, and what we know about the
  206. nonzero bits of a register, specifically which bits are known to be
  207. zero.
  208. If an entry is zero, it means that we don't know anything special. */
  209. unsigned char sign_bit_copies;
  210. unsigned HOST_WIDE_INT nonzero_bits;
  211. /* Record the value of the label_tick when the last truncation
  212. happened. The field truncated_to_mode is only valid if
  213. truncation_label == label_tick. */
  214. int truncation_label;
  215. /* Record the last truncation seen for this register. If truncation
  216. is not a nop to this mode we might be able to save an explicit
  217. truncation if we know that value already contains a truncated
  218. value. */
  219. ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
  220. } reg_stat_type;
  221. static vec<reg_stat_type> reg_stat;
  222. /* One plus the highest pseudo for which we track REG_N_SETS.
  223. regstat_init_n_sets_and_refs allocates the array for REG_N_SETS just once,
  224. but during combine_split_insns new pseudos can be created. As we don't have
  225. updated DF information in that case, it is hard to initialize the array
  226. after growing. The combiner only cares about REG_N_SETS (regno) == 1,
  227. so instead of growing the arrays, just assume all newly created pseudos
  228. during combine might be set multiple times. */
  229. static unsigned int reg_n_sets_max;
  230. /* Record the luid of the last insn that invalidated memory
  231. (anything that writes memory, and subroutine calls, but not pushes). */
  232. static int mem_last_set;
  233. /* Record the luid of the last CALL_INSN
  234. so we can tell whether a potential combination crosses any calls. */
  235. static int last_call_luid;
  236. /* When `subst' is called, this is the insn that is being modified
  237. (by combining in a previous insn). The PATTERN of this insn
  238. is still the old pattern partially modified and it should not be
  239. looked at, but this may be used to examine the successors of the insn
  240. to judge whether a simplification is valid. */
  241. static rtx_insn *subst_insn;
  242. /* This is the lowest LUID that `subst' is currently dealing with.
  243. get_last_value will not return a value if the register was set at or
  244. after this LUID. If not for this mechanism, we could get confused if
  245. I2 or I1 in try_combine were an insn that used the old value of a register
  246. to obtain a new value. In that case, we might erroneously get the
  247. new value of the register when we wanted the old one. */
  248. static int subst_low_luid;
  249. /* This contains any hard registers that are used in newpat; reg_dead_at_p
  250. must consider all these registers to be always live. */
  251. static HARD_REG_SET newpat_used_regs;
  252. /* This is an insn to which a LOG_LINKS entry has been added. If this
  253. insn is the earlier than I2 or I3, combine should rescan starting at
  254. that location. */
  255. static rtx_insn *added_links_insn;
  256. /* Basic block in which we are performing combines. */
  257. static basic_block this_basic_block;
  258. static bool optimize_this_for_speed_p;
  259. /* Length of the currently allocated uid_insn_cost array. */
  260. static int max_uid_known;
  261. /* The following array records the insn_rtx_cost for every insn
  262. in the instruction stream. */
  263. static int *uid_insn_cost;
  264. /* The following array records the LOG_LINKS for every insn in the
  265. instruction stream as struct insn_link pointers. */
  266. struct insn_link {
  267. rtx_insn *insn;
  268. unsigned int regno;
  269. struct insn_link *next;
  270. };
  271. static struct insn_link **uid_log_links;
  272. #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
  273. #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
  274. #define FOR_EACH_LOG_LINK(L, INSN) \
  275. for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
  276. /* Links for LOG_LINKS are allocated from this obstack. */
  277. static struct obstack insn_link_obstack;
  278. /* Allocate a link. */
  279. static inline struct insn_link *
  280. alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
  281. {
  282. struct insn_link *l
  283. = (struct insn_link *) obstack_alloc (&insn_link_obstack,
  284. sizeof (struct insn_link));
  285. l->insn = insn;
  286. l->regno = regno;
  287. l->next = next;
  288. return l;
  289. }
  290. /* Incremented for each basic block. */
  291. static int label_tick;
  292. /* Reset to label_tick for each extended basic block in scanning order. */
  293. static int label_tick_ebb_start;
  294. /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
  295. largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
  296. static machine_mode nonzero_bits_mode;
  297. /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
  298. be safely used. It is zero while computing them and after combine has
  299. completed. This former test prevents propagating values based on
  300. previously set values, which can be incorrect if a variable is modified
  301. in a loop. */
  302. static int nonzero_sign_valid;
  303. /* Record one modification to rtl structure
  304. to be undone by storing old_contents into *where. */
  305. enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
  306. struct undo
  307. {
  308. struct undo *next;
  309. enum undo_kind kind;
  310. union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
  311. union { rtx *r; int *i; struct insn_link **l; } where;
  312. };
  313. /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
  314. num_undo says how many are currently recorded.
  315. other_insn is nonzero if we have modified some other insn in the process
  316. of working on subst_insn. It must be verified too. */
  317. struct undobuf
  318. {
  319. struct undo *undos;
  320. struct undo *frees;
  321. rtx_insn *other_insn;
  322. };
  323. static struct undobuf undobuf;
  324. /* Number of times the pseudo being substituted for
  325. was found and replaced. */
  326. static int n_occurrences;
  327. static rtx reg_nonzero_bits_for_combine (const_rtx, machine_mode, const_rtx,
  328. machine_mode,
  329. unsigned HOST_WIDE_INT,
  330. unsigned HOST_WIDE_INT *);
  331. static rtx reg_num_sign_bit_copies_for_combine (const_rtx, machine_mode, const_rtx,
  332. machine_mode,
  333. unsigned int, unsigned int *);
  334. static void do_SUBST (rtx *, rtx);
  335. static void do_SUBST_INT (int *, int);
  336. static void init_reg_last (void);
  337. static void setup_incoming_promotions (rtx_insn *);
  338. static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
  339. static int cant_combine_insn_p (rtx_insn *);
  340. static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
  341. rtx_insn *, rtx_insn *, rtx *, rtx *);
  342. static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
  343. static int contains_muldiv (rtx);
  344. static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
  345. int *, rtx_insn *);
  346. static void undo_all (void);
  347. static void undo_commit (void);
  348. static rtx *find_split_point (rtx *, rtx_insn *, bool);
  349. static rtx subst (rtx, rtx, rtx, int, int, int);
  350. static rtx combine_simplify_rtx (rtx, machine_mode, int, int);
  351. static rtx simplify_if_then_else (rtx);
  352. static rtx simplify_set (rtx);
  353. static rtx simplify_logical (rtx);
  354. static rtx expand_compound_operation (rtx);
  355. static const_rtx expand_field_assignment (const_rtx);
  356. static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT,
  357. rtx, unsigned HOST_WIDE_INT, int, int, int);
  358. static rtx extract_left_shift (rtx, int);
  359. static int get_pos_from_mask (unsigned HOST_WIDE_INT,
  360. unsigned HOST_WIDE_INT *);
  361. static rtx canon_reg_for_combine (rtx, rtx);
  362. static rtx force_to_mode (rtx, machine_mode,
  363. unsigned HOST_WIDE_INT, int);
  364. static rtx if_then_else_cond (rtx, rtx *, rtx *);
  365. static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
  366. static int rtx_equal_for_field_assignment_p (rtx, rtx, bool = false);
  367. static rtx make_field_assignment (rtx);
  368. static rtx apply_distributive_law (rtx);
  369. static rtx distribute_and_simplify_rtx (rtx, int);
  370. static rtx simplify_and_const_int_1 (machine_mode, rtx,
  371. unsigned HOST_WIDE_INT);
  372. static rtx simplify_and_const_int (rtx, machine_mode, rtx,
  373. unsigned HOST_WIDE_INT);
  374. static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
  375. HOST_WIDE_INT, machine_mode, int *);
  376. static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
  377. static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
  378. int);
  379. static int recog_for_combine (rtx *, rtx_insn *, rtx *);
  380. static rtx gen_lowpart_for_combine (machine_mode, rtx);
  381. static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
  382. rtx, rtx *);
  383. static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
  384. static void update_table_tick (rtx);
  385. static void record_value_for_reg (rtx, rtx_insn *, rtx);
  386. static void check_promoted_subreg (rtx_insn *, rtx);
  387. static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
  388. static void record_dead_and_set_regs (rtx_insn *);
  389. static int get_last_value_validate (rtx *, rtx_insn *, int, int);
  390. static rtx get_last_value (const_rtx);
  391. static int use_crosses_set_p (const_rtx, int);
  392. static void reg_dead_at_p_1 (rtx, const_rtx, void *);
  393. static int reg_dead_at_p (rtx, rtx_insn *);
  394. static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
  395. static int reg_bitfield_target_p (rtx, rtx);
  396. static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
  397. static void distribute_links (struct insn_link *);
  398. static void mark_used_regs_combine (rtx);
  399. static void record_promoted_value (rtx_insn *, rtx);
  400. static bool unmentioned_reg_p (rtx, rtx);
  401. static void record_truncated_values (rtx *, void *);
  402. static bool reg_truncated_to_mode (machine_mode, const_rtx);
  403. static rtx gen_lowpart_or_truncate (machine_mode, rtx);
  404. /* It is not safe to use ordinary gen_lowpart in combine.
  405. See comments in gen_lowpart_for_combine. */
  406. #undef RTL_HOOKS_GEN_LOWPART
  407. #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
  408. /* Our implementation of gen_lowpart never emits a new pseudo. */
  409. #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
  410. #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
  411. #undef RTL_HOOKS_REG_NONZERO_REG_BITS
  412. #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
  413. #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
  414. #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
  415. #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
  416. #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
  417. static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
  418. /* Convenience wrapper for the canonicalize_comparison target hook.
  419. Target hooks cannot use enum rtx_code. */
  420. static inline void
  421. target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
  422. bool op0_preserve_value)
  423. {
  424. int code_int = (int)*code;
  425. targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
  426. *code = (enum rtx_code)code_int;
  427. }
  428. /* Try to split PATTERN found in INSN. This returns NULL_RTX if
  429. PATTERN can not be split. Otherwise, it returns an insn sequence.
  430. This is a wrapper around split_insns which ensures that the
  431. reg_stat vector is made larger if the splitter creates a new
  432. register. */
  433. static rtx_insn *
  434. combine_split_insns (rtx pattern, rtx insn)
  435. {
  436. rtx_insn *ret;
  437. unsigned int nregs;
  438. ret = safe_as_a <rtx_insn *> (split_insns (pattern, insn));
  439. nregs = max_reg_num ();
  440. if (nregs > reg_stat.length ())
  441. reg_stat.safe_grow_cleared (nregs);
  442. return ret;
  443. }
  444. /* This is used by find_single_use to locate an rtx in LOC that
  445. contains exactly one use of DEST, which is typically either a REG
  446. or CC0. It returns a pointer to the innermost rtx expression
  447. containing DEST. Appearances of DEST that are being used to
  448. totally replace it are not counted. */
  449. static rtx *
  450. find_single_use_1 (rtx dest, rtx *loc)
  451. {
  452. rtx x = *loc;
  453. enum rtx_code code = GET_CODE (x);
  454. rtx *result = NULL;
  455. rtx *this_result;
  456. int i;
  457. const char *fmt;
  458. switch (code)
  459. {
  460. case CONST:
  461. case LABEL_REF:
  462. case SYMBOL_REF:
  463. CASE_CONST_ANY:
  464. case CLOBBER:
  465. return 0;
  466. case SET:
  467. /* If the destination is anything other than CC0, PC, a REG or a SUBREG
  468. of a REG that occupies all of the REG, the insn uses DEST if
  469. it is mentioned in the destination or the source. Otherwise, we
  470. need just check the source. */
  471. if (GET_CODE (SET_DEST (x)) != CC0
  472. && GET_CODE (SET_DEST (x)) != PC
  473. && !REG_P (SET_DEST (x))
  474. && ! (GET_CODE (SET_DEST (x)) == SUBREG
  475. && REG_P (SUBREG_REG (SET_DEST (x)))
  476. && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
  477. + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
  478. == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
  479. + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
  480. break;
  481. return find_single_use_1 (dest, &SET_SRC (x));
  482. case MEM:
  483. case SUBREG:
  484. return find_single_use_1 (dest, &XEXP (x, 0));
  485. default:
  486. break;
  487. }
  488. /* If it wasn't one of the common cases above, check each expression and
  489. vector of this code. Look for a unique usage of DEST. */
  490. fmt = GET_RTX_FORMAT (code);
  491. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  492. {
  493. if (fmt[i] == 'e')
  494. {
  495. if (dest == XEXP (x, i)
  496. || (REG_P (dest) && REG_P (XEXP (x, i))
  497. && REGNO (dest) == REGNO (XEXP (x, i))))
  498. this_result = loc;
  499. else
  500. this_result = find_single_use_1 (dest, &XEXP (x, i));
  501. if (result == NULL)
  502. result = this_result;
  503. else if (this_result)
  504. /* Duplicate usage. */
  505. return NULL;
  506. }
  507. else if (fmt[i] == 'E')
  508. {
  509. int j;
  510. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  511. {
  512. if (XVECEXP (x, i, j) == dest
  513. || (REG_P (dest)
  514. && REG_P (XVECEXP (x, i, j))
  515. && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
  516. this_result = loc;
  517. else
  518. this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
  519. if (result == NULL)
  520. result = this_result;
  521. else if (this_result)
  522. return NULL;
  523. }
  524. }
  525. }
  526. return result;
  527. }
  528. /* See if DEST, produced in INSN, is used only a single time in the
  529. sequel. If so, return a pointer to the innermost rtx expression in which
  530. it is used.
  531. If PLOC is nonzero, *PLOC is set to the insn containing the single use.
  532. If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
  533. care about REG_DEAD notes or LOG_LINKS.
  534. Otherwise, we find the single use by finding an insn that has a
  535. LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
  536. only referenced once in that insn, we know that it must be the first
  537. and last insn referencing DEST. */
  538. static rtx *
  539. find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
  540. {
  541. basic_block bb;
  542. rtx_insn *next;
  543. rtx *result;
  544. struct insn_link *link;
  545. #ifdef HAVE_cc0
  546. if (dest == cc0_rtx)
  547. {
  548. next = NEXT_INSN (insn);
  549. if (next == 0
  550. || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
  551. return 0;
  552. result = find_single_use_1 (dest, &PATTERN (next));
  553. if (result && ploc)
  554. *ploc = next;
  555. return result;
  556. }
  557. #endif
  558. if (!REG_P (dest))
  559. return 0;
  560. bb = BLOCK_FOR_INSN (insn);
  561. for (next = NEXT_INSN (insn);
  562. next && BLOCK_FOR_INSN (next) == bb;
  563. next = NEXT_INSN (next))
  564. if (INSN_P (next) && dead_or_set_p (next, dest))
  565. {
  566. FOR_EACH_LOG_LINK (link, next)
  567. if (link->insn == insn && link->regno == REGNO (dest))
  568. break;
  569. if (link)
  570. {
  571. result = find_single_use_1 (dest, &PATTERN (next));
  572. if (ploc)
  573. *ploc = next;
  574. return result;
  575. }
  576. }
  577. return 0;
  578. }
  579. /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
  580. insn. The substitution can be undone by undo_all. If INTO is already
  581. set to NEWVAL, do not record this change. Because computing NEWVAL might
  582. also call SUBST, we have to compute it before we put anything into
  583. the undo table. */
  584. static void
  585. do_SUBST (rtx *into, rtx newval)
  586. {
  587. struct undo *buf;
  588. rtx oldval = *into;
  589. if (oldval == newval)
  590. return;
  591. /* We'd like to catch as many invalid transformations here as
  592. possible. Unfortunately, there are way too many mode changes
  593. that are perfectly valid, so we'd waste too much effort for
  594. little gain doing the checks here. Focus on catching invalid
  595. transformations involving integer constants. */
  596. if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
  597. && CONST_INT_P (newval))
  598. {
  599. /* Sanity check that we're replacing oldval with a CONST_INT
  600. that is a valid sign-extension for the original mode. */
  601. gcc_assert (INTVAL (newval)
  602. == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
  603. /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
  604. CONST_INT is not valid, because after the replacement, the
  605. original mode would be gone. Unfortunately, we can't tell
  606. when do_SUBST is called to replace the operand thereof, so we
  607. perform this test on oldval instead, checking whether an
  608. invalid replacement took place before we got here. */
  609. gcc_assert (!(GET_CODE (oldval) == SUBREG
  610. && CONST_INT_P (SUBREG_REG (oldval))));
  611. gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
  612. && CONST_INT_P (XEXP (oldval, 0))));
  613. }
  614. if (undobuf.frees)
  615. buf = undobuf.frees, undobuf.frees = buf->next;
  616. else
  617. buf = XNEW (struct undo);
  618. buf->kind = UNDO_RTX;
  619. buf->where.r = into;
  620. buf->old_contents.r = oldval;
  621. *into = newval;
  622. buf->next = undobuf.undos, undobuf.undos = buf;
  623. }
  624. #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
  625. /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
  626. for the value of a HOST_WIDE_INT value (including CONST_INT) is
  627. not safe. */
  628. static void
  629. do_SUBST_INT (int *into, int newval)
  630. {
  631. struct undo *buf;
  632. int oldval = *into;
  633. if (oldval == newval)
  634. return;
  635. if (undobuf.frees)
  636. buf = undobuf.frees, undobuf.frees = buf->next;
  637. else
  638. buf = XNEW (struct undo);
  639. buf->kind = UNDO_INT;
  640. buf->where.i = into;
  641. buf->old_contents.i = oldval;
  642. *into = newval;
  643. buf->next = undobuf.undos, undobuf.undos = buf;
  644. }
  645. #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
  646. /* Similar to SUBST, but just substitute the mode. This is used when
  647. changing the mode of a pseudo-register, so that any other
  648. references to the entry in the regno_reg_rtx array will change as
  649. well. */
  650. static void
  651. do_SUBST_MODE (rtx *into, machine_mode newval)
  652. {
  653. struct undo *buf;
  654. machine_mode oldval = GET_MODE (*into);
  655. if (oldval == newval)
  656. return;
  657. if (undobuf.frees)
  658. buf = undobuf.frees, undobuf.frees = buf->next;
  659. else
  660. buf = XNEW (struct undo);
  661. buf->kind = UNDO_MODE;
  662. buf->where.r = into;
  663. buf->old_contents.m = oldval;
  664. adjust_reg_mode (*into, newval);
  665. buf->next = undobuf.undos, undobuf.undos = buf;
  666. }
  667. #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
  668. #ifndef HAVE_cc0
  669. /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
  670. static void
  671. do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
  672. {
  673. struct undo *buf;
  674. struct insn_link * oldval = *into;
  675. if (oldval == newval)
  676. return;
  677. if (undobuf.frees)
  678. buf = undobuf.frees, undobuf.frees = buf->next;
  679. else
  680. buf = XNEW (struct undo);
  681. buf->kind = UNDO_LINKS;
  682. buf->where.l = into;
  683. buf->old_contents.l = oldval;
  684. *into = newval;
  685. buf->next = undobuf.undos, undobuf.undos = buf;
  686. }
  687. #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
  688. #endif
  689. /* Subroutine of try_combine. Determine whether the replacement patterns
  690. NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
  691. than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
  692. that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
  693. undobuf.other_insn may also both be NULL_RTX. Return false if the cost
  694. of all the instructions can be estimated and the replacements are more
  695. expensive than the original sequence. */
  696. static bool
  697. combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
  698. rtx newpat, rtx newi2pat, rtx newotherpat)
  699. {
  700. int i0_cost, i1_cost, i2_cost, i3_cost;
  701. int new_i2_cost, new_i3_cost;
  702. int old_cost, new_cost;
  703. /* Lookup the original insn_rtx_costs. */
  704. i2_cost = INSN_COST (i2);
  705. i3_cost = INSN_COST (i3);
  706. if (i1)
  707. {
  708. i1_cost = INSN_COST (i1);
  709. if (i0)
  710. {
  711. i0_cost = INSN_COST (i0);
  712. old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
  713. ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
  714. }
  715. else
  716. {
  717. old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
  718. ? i1_cost + i2_cost + i3_cost : 0);
  719. i0_cost = 0;
  720. }
  721. }
  722. else
  723. {
  724. old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
  725. i1_cost = i0_cost = 0;
  726. }
  727. /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
  728. correct that. */
  729. if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
  730. old_cost -= i1_cost;
  731. /* Calculate the replacement insn_rtx_costs. */
  732. new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
  733. if (newi2pat)
  734. {
  735. new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
  736. new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
  737. ? new_i2_cost + new_i3_cost : 0;
  738. }
  739. else
  740. {
  741. new_cost = new_i3_cost;
  742. new_i2_cost = 0;
  743. }
  744. if (undobuf.other_insn)
  745. {
  746. int old_other_cost, new_other_cost;
  747. old_other_cost = INSN_COST (undobuf.other_insn);
  748. new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
  749. if (old_other_cost > 0 && new_other_cost > 0)
  750. {
  751. old_cost += old_other_cost;
  752. new_cost += new_other_cost;
  753. }
  754. else
  755. old_cost = 0;
  756. }
  757. /* Disallow this combination if both new_cost and old_cost are greater than
  758. zero, and new_cost is greater than old cost. */
  759. int reject = old_cost > 0 && new_cost > old_cost;
  760. if (dump_file)
  761. {
  762. fprintf (dump_file, "%s combination of insns ",
  763. reject ? "rejecting" : "allowing");
  764. if (i0)
  765. fprintf (dump_file, "%d, ", INSN_UID (i0));
  766. if (i1 && INSN_UID (i1) != INSN_UID (i2))
  767. fprintf (dump_file, "%d, ", INSN_UID (i1));
  768. fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
  769. fprintf (dump_file, "original costs ");
  770. if (i0)
  771. fprintf (dump_file, "%d + ", i0_cost);
  772. if (i1 && INSN_UID (i1) != INSN_UID (i2))
  773. fprintf (dump_file, "%d + ", i1_cost);
  774. fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
  775. if (newi2pat)
  776. fprintf (dump_file, "replacement costs %d + %d = %d\n",
  777. new_i2_cost, new_i3_cost, new_cost);
  778. else
  779. fprintf (dump_file, "replacement cost %d\n", new_cost);
  780. }
  781. if (reject)
  782. return false;
  783. /* Update the uid_insn_cost array with the replacement costs. */
  784. INSN_COST (i2) = new_i2_cost;
  785. INSN_COST (i3) = new_i3_cost;
  786. if (i1)
  787. {
  788. INSN_COST (i1) = 0;
  789. if (i0)
  790. INSN_COST (i0) = 0;
  791. }
  792. return true;
  793. }
  794. /* Delete any insns that copy a register to itself. */
  795. static void
  796. delete_noop_moves (void)
  797. {
  798. rtx_insn *insn, *next;
  799. basic_block bb;
  800. FOR_EACH_BB_FN (bb, cfun)
  801. {
  802. for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
  803. {
  804. next = NEXT_INSN (insn);
  805. if (INSN_P (insn) && noop_move_p (insn))
  806. {
  807. if (dump_file)
  808. fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
  809. delete_insn_and_edges (insn);
  810. }
  811. }
  812. }
  813. }
  814. /* Return false if we do not want to (or cannot) combine DEF. */
  815. static bool
  816. can_combine_def_p (df_ref def)
  817. {
  818. /* Do not consider if it is pre/post modification in MEM. */
  819. if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
  820. return false;
  821. unsigned int regno = DF_REF_REGNO (def);
  822. /* Do not combine frame pointer adjustments. */
  823. if ((regno == FRAME_POINTER_REGNUM
  824. && (!reload_completed || frame_pointer_needed))
  825. #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
  826. || (regno == HARD_FRAME_POINTER_REGNUM
  827. && (!reload_completed || frame_pointer_needed))
  828. #endif
  829. #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
  830. || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
  831. #endif
  832. )
  833. return false;
  834. return true;
  835. }
  836. /* Return false if we do not want to (or cannot) combine USE. */
  837. static bool
  838. can_combine_use_p (df_ref use)
  839. {
  840. /* Do not consider the usage of the stack pointer by function call. */
  841. if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
  842. return false;
  843. return true;
  844. }
  845. /* Fill in log links field for all insns. */
  846. static void
  847. create_log_links (void)
  848. {
  849. basic_block bb;
  850. rtx_insn **next_use;
  851. rtx_insn *insn;
  852. df_ref def, use;
  853. next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
  854. /* Pass through each block from the end, recording the uses of each
  855. register and establishing log links when def is encountered.
  856. Note that we do not clear next_use array in order to save time,
  857. so we have to test whether the use is in the same basic block as def.
  858. There are a few cases below when we do not consider the definition or
  859. usage -- these are taken from original flow.c did. Don't ask me why it is
  860. done this way; I don't know and if it works, I don't want to know. */
  861. FOR_EACH_BB_FN (bb, cfun)
  862. {
  863. FOR_BB_INSNS_REVERSE (bb, insn)
  864. {
  865. if (!NONDEBUG_INSN_P (insn))
  866. continue;
  867. /* Log links are created only once. */
  868. gcc_assert (!LOG_LINKS (insn));
  869. FOR_EACH_INSN_DEF (def, insn)
  870. {
  871. unsigned int regno = DF_REF_REGNO (def);
  872. rtx_insn *use_insn;
  873. if (!next_use[regno])
  874. continue;
  875. if (!can_combine_def_p (def))
  876. continue;
  877. use_insn = next_use[regno];
  878. next_use[regno] = NULL;
  879. if (BLOCK_FOR_INSN (use_insn) != bb)
  880. continue;
  881. /* flow.c claimed:
  882. We don't build a LOG_LINK for hard registers contained
  883. in ASM_OPERANDs. If these registers get replaced,
  884. we might wind up changing the semantics of the insn,
  885. even if reload can make what appear to be valid
  886. assignments later. */
  887. if (regno < FIRST_PSEUDO_REGISTER
  888. && asm_noperands (PATTERN (use_insn)) >= 0)
  889. continue;
  890. /* Don't add duplicate links between instructions. */
  891. struct insn_link *links;
  892. FOR_EACH_LOG_LINK (links, use_insn)
  893. if (insn == links->insn && regno == links->regno)
  894. break;
  895. if (!links)
  896. LOG_LINKS (use_insn)
  897. = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
  898. }
  899. FOR_EACH_INSN_USE (use, insn)
  900. if (can_combine_use_p (use))
  901. next_use[DF_REF_REGNO (use)] = insn;
  902. }
  903. }
  904. free (next_use);
  905. }
  906. /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
  907. true if we found a LOG_LINK that proves that A feeds B. This only works
  908. if there are no instructions between A and B which could have a link
  909. depending on A, since in that case we would not record a link for B.
  910. We also check the implicit dependency created by a cc0 setter/user
  911. pair. */
  912. static bool
  913. insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
  914. {
  915. struct insn_link *links;
  916. FOR_EACH_LOG_LINK (links, b)
  917. if (links->insn == a)
  918. return true;
  919. #ifdef HAVE_cc0
  920. if (sets_cc0_p (a))
  921. return true;
  922. #endif
  923. return false;
  924. }
  925. /* Main entry point for combiner. F is the first insn of the function.
  926. NREGS is the first unused pseudo-reg number.
  927. Return nonzero if the combiner has turned an indirect jump
  928. instruction into a direct jump. */
  929. static int
  930. combine_instructions (rtx_insn *f, unsigned int nregs)
  931. {
  932. rtx_insn *insn, *next;
  933. #ifdef HAVE_cc0
  934. rtx_insn *prev;
  935. #endif
  936. struct insn_link *links, *nextlinks;
  937. rtx_insn *first;
  938. basic_block last_bb;
  939. int new_direct_jump_p = 0;
  940. for (first = f; first && !INSN_P (first); )
  941. first = NEXT_INSN (first);
  942. if (!first)
  943. return 0;
  944. combine_attempts = 0;
  945. combine_merges = 0;
  946. combine_extras = 0;
  947. combine_successes = 0;
  948. rtl_hooks = combine_rtl_hooks;
  949. reg_stat.safe_grow_cleared (nregs);
  950. init_recog_no_volatile ();
  951. /* Allocate array for insn info. */
  952. max_uid_known = get_max_uid ();
  953. uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
  954. uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
  955. gcc_obstack_init (&insn_link_obstack);
  956. nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
  957. /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
  958. problems when, for example, we have j <<= 1 in a loop. */
  959. nonzero_sign_valid = 0;
  960. label_tick = label_tick_ebb_start = 1;
  961. /* Scan all SETs and see if we can deduce anything about what
  962. bits are known to be zero for some registers and how many copies
  963. of the sign bit are known to exist for those registers.
  964. Also set any known values so that we can use it while searching
  965. for what bits are known to be set. */
  966. setup_incoming_promotions (first);
  967. /* Allow the entry block and the first block to fall into the same EBB.
  968. Conceptually the incoming promotions are assigned to the entry block. */
  969. last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
  970. create_log_links ();
  971. FOR_EACH_BB_FN (this_basic_block, cfun)
  972. {
  973. optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
  974. last_call_luid = 0;
  975. mem_last_set = -1;
  976. label_tick++;
  977. if (!single_pred_p (this_basic_block)
  978. || single_pred (this_basic_block) != last_bb)
  979. label_tick_ebb_start = label_tick;
  980. last_bb = this_basic_block;
  981. FOR_BB_INSNS (this_basic_block, insn)
  982. if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
  983. {
  984. #ifdef AUTO_INC_DEC
  985. rtx links;
  986. #endif
  987. subst_low_luid = DF_INSN_LUID (insn);
  988. subst_insn = insn;
  989. note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
  990. insn);
  991. record_dead_and_set_regs (insn);
  992. #ifdef AUTO_INC_DEC
  993. for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
  994. if (REG_NOTE_KIND (links) == REG_INC)
  995. set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
  996. insn);
  997. #endif
  998. /* Record the current insn_rtx_cost of this instruction. */
  999. if (NONJUMP_INSN_P (insn))
  1000. INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
  1001. optimize_this_for_speed_p);
  1002. if (dump_file)
  1003. fprintf (dump_file, "insn_cost %d: %d\n",
  1004. INSN_UID (insn), INSN_COST (insn));
  1005. }
  1006. }
  1007. nonzero_sign_valid = 1;
  1008. /* Now scan all the insns in forward order. */
  1009. label_tick = label_tick_ebb_start = 1;
  1010. init_reg_last ();
  1011. setup_incoming_promotions (first);
  1012. last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
  1013. int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
  1014. FOR_EACH_BB_FN (this_basic_block, cfun)
  1015. {
  1016. rtx_insn *last_combined_insn = NULL;
  1017. optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
  1018. last_call_luid = 0;
  1019. mem_last_set = -1;
  1020. label_tick++;
  1021. if (!single_pred_p (this_basic_block)
  1022. || single_pred (this_basic_block) != last_bb)
  1023. label_tick_ebb_start = label_tick;
  1024. last_bb = this_basic_block;
  1025. rtl_profile_for_bb (this_basic_block);
  1026. for (insn = BB_HEAD (this_basic_block);
  1027. insn != NEXT_INSN (BB_END (this_basic_block));
  1028. insn = next ? next : NEXT_INSN (insn))
  1029. {
  1030. next = 0;
  1031. if (!NONDEBUG_INSN_P (insn))
  1032. continue;
  1033. while (last_combined_insn
  1034. && last_combined_insn->deleted ())
  1035. last_combined_insn = PREV_INSN (last_combined_insn);
  1036. if (last_combined_insn == NULL_RTX
  1037. || BARRIER_P (last_combined_insn)
  1038. || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
  1039. || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
  1040. last_combined_insn = insn;
  1041. /* See if we know about function return values before this
  1042. insn based upon SUBREG flags. */
  1043. check_promoted_subreg (insn, PATTERN (insn));
  1044. /* See if we can find hardregs and subreg of pseudos in
  1045. narrower modes. This could help turning TRUNCATEs
  1046. into SUBREGs. */
  1047. note_uses (&PATTERN (insn), record_truncated_values, NULL);
  1048. /* Try this insn with each insn it links back to. */
  1049. FOR_EACH_LOG_LINK (links, insn)
  1050. if ((next = try_combine (insn, links->insn, NULL,
  1051. NULL, &new_direct_jump_p,
  1052. last_combined_insn)) != 0)
  1053. {
  1054. statistics_counter_event (cfun, "two-insn combine", 1);
  1055. goto retry;
  1056. }
  1057. /* Try each sequence of three linked insns ending with this one. */
  1058. if (max_combine >= 3)
  1059. FOR_EACH_LOG_LINK (links, insn)
  1060. {
  1061. rtx_insn *link = links->insn;
  1062. /* If the linked insn has been replaced by a note, then there
  1063. is no point in pursuing this chain any further. */
  1064. if (NOTE_P (link))
  1065. continue;
  1066. FOR_EACH_LOG_LINK (nextlinks, link)
  1067. if ((next = try_combine (insn, link, nextlinks->insn,
  1068. NULL, &new_direct_jump_p,
  1069. last_combined_insn)) != 0)
  1070. {
  1071. statistics_counter_event (cfun, "three-insn combine", 1);
  1072. goto retry;
  1073. }
  1074. }
  1075. #ifdef HAVE_cc0
  1076. /* Try to combine a jump insn that uses CC0
  1077. with a preceding insn that sets CC0, and maybe with its
  1078. logical predecessor as well.
  1079. This is how we make decrement-and-branch insns.
  1080. We need this special code because data flow connections
  1081. via CC0 do not get entered in LOG_LINKS. */
  1082. if (JUMP_P (insn)
  1083. && (prev = prev_nonnote_insn (insn)) != 0
  1084. && NONJUMP_INSN_P (prev)
  1085. && sets_cc0_p (PATTERN (prev)))
  1086. {
  1087. if ((next = try_combine (insn, prev, NULL, NULL,
  1088. &new_direct_jump_p,
  1089. last_combined_insn)) != 0)
  1090. goto retry;
  1091. FOR_EACH_LOG_LINK (nextlinks, prev)
  1092. if ((next = try_combine (insn, prev, nextlinks->insn,
  1093. NULL, &new_direct_jump_p,
  1094. last_combined_insn)) != 0)
  1095. goto retry;
  1096. }
  1097. /* Do the same for an insn that explicitly references CC0. */
  1098. if (NONJUMP_INSN_P (insn)
  1099. && (prev = prev_nonnote_insn (insn)) != 0
  1100. && NONJUMP_INSN_P (prev)
  1101. && sets_cc0_p (PATTERN (prev))
  1102. && GET_CODE (PATTERN (insn)) == SET
  1103. && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
  1104. {
  1105. if ((next = try_combine (insn, prev, NULL, NULL,
  1106. &new_direct_jump_p,
  1107. last_combined_insn)) != 0)
  1108. goto retry;
  1109. FOR_EACH_LOG_LINK (nextlinks, prev)
  1110. if ((next = try_combine (insn, prev, nextlinks->insn,
  1111. NULL, &new_direct_jump_p,
  1112. last_combined_insn)) != 0)
  1113. goto retry;
  1114. }
  1115. /* Finally, see if any of the insns that this insn links to
  1116. explicitly references CC0. If so, try this insn, that insn,
  1117. and its predecessor if it sets CC0. */
  1118. FOR_EACH_LOG_LINK (links, insn)
  1119. if (NONJUMP_INSN_P (links->insn)
  1120. && GET_CODE (PATTERN (links->insn)) == SET
  1121. && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
  1122. && (prev = prev_nonnote_insn (links->insn)) != 0
  1123. && NONJUMP_INSN_P (prev)
  1124. && sets_cc0_p (PATTERN (prev))
  1125. && (next = try_combine (insn, links->insn,
  1126. prev, NULL, &new_direct_jump_p,
  1127. last_combined_insn)) != 0)
  1128. goto retry;
  1129. #endif
  1130. /* Try combining an insn with two different insns whose results it
  1131. uses. */
  1132. if (max_combine >= 3)
  1133. FOR_EACH_LOG_LINK (links, insn)
  1134. for (nextlinks = links->next; nextlinks;
  1135. nextlinks = nextlinks->next)
  1136. if ((next = try_combine (insn, links->insn,
  1137. nextlinks->insn, NULL,
  1138. &new_direct_jump_p,
  1139. last_combined_insn)) != 0)
  1140. {
  1141. statistics_counter_event (cfun, "three-insn combine", 1);
  1142. goto retry;
  1143. }
  1144. /* Try four-instruction combinations. */
  1145. if (max_combine >= 4)
  1146. FOR_EACH_LOG_LINK (links, insn)
  1147. {
  1148. struct insn_link *next1;
  1149. rtx_insn *link = links->insn;
  1150. /* If the linked insn has been replaced by a note, then there
  1151. is no point in pursuing this chain any further. */
  1152. if (NOTE_P (link))
  1153. continue;
  1154. FOR_EACH_LOG_LINK (next1, link)
  1155. {
  1156. rtx_insn *link1 = next1->insn;
  1157. if (NOTE_P (link1))
  1158. continue;
  1159. /* I0 -> I1 -> I2 -> I3. */
  1160. FOR_EACH_LOG_LINK (nextlinks, link1)
  1161. if ((next = try_combine (insn, link, link1,
  1162. nextlinks->insn,
  1163. &new_direct_jump_p,
  1164. last_combined_insn)) != 0)
  1165. {
  1166. statistics_counter_event (cfun, "four-insn combine", 1);
  1167. goto retry;
  1168. }
  1169. /* I0, I1 -> I2, I2 -> I3. */
  1170. for (nextlinks = next1->next; nextlinks;
  1171. nextlinks = nextlinks->next)
  1172. if ((next = try_combine (insn, link, link1,
  1173. nextlinks->insn,
  1174. &new_direct_jump_p,
  1175. last_combined_insn)) != 0)
  1176. {
  1177. statistics_counter_event (cfun, "four-insn combine", 1);
  1178. goto retry;
  1179. }
  1180. }
  1181. for (next1 = links->next; next1; next1 = next1->next)
  1182. {
  1183. rtx_insn *link1 = next1->insn;
  1184. if (NOTE_P (link1))
  1185. continue;
  1186. /* I0 -> I2; I1, I2 -> I3. */
  1187. FOR_EACH_LOG_LINK (nextlinks, link)
  1188. if ((next = try_combine (insn, link, link1,
  1189. nextlinks->insn,
  1190. &new_direct_jump_p,
  1191. last_combined_insn)) != 0)
  1192. {
  1193. statistics_counter_event (cfun, "four-insn combine", 1);
  1194. goto retry;
  1195. }
  1196. /* I0 -> I1; I1, I2 -> I3. */
  1197. FOR_EACH_LOG_LINK (nextlinks, link1)
  1198. if ((next = try_combine (insn, link, link1,
  1199. nextlinks->insn,
  1200. &new_direct_jump_p,
  1201. last_combined_insn)) != 0)
  1202. {
  1203. statistics_counter_event (cfun, "four-insn combine", 1);
  1204. goto retry;
  1205. }
  1206. }
  1207. }
  1208. /* Try this insn with each REG_EQUAL note it links back to. */
  1209. FOR_EACH_LOG_LINK (links, insn)
  1210. {
  1211. rtx set, note;
  1212. rtx_insn *temp = links->insn;
  1213. if ((set = single_set (temp)) != 0
  1214. && (note = find_reg_equal_equiv_note (temp)) != 0
  1215. && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
  1216. /* Avoid using a register that may already been marked
  1217. dead by an earlier instruction. */
  1218. && ! unmentioned_reg_p (note, SET_SRC (set))
  1219. && (GET_MODE (note) == VOIDmode
  1220. ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
  1221. : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
  1222. {
  1223. /* Temporarily replace the set's source with the
  1224. contents of the REG_EQUAL note. The insn will
  1225. be deleted or recognized by try_combine. */
  1226. rtx orig = SET_SRC (set);
  1227. SET_SRC (set) = note;
  1228. i2mod = temp;
  1229. i2mod_old_rhs = copy_rtx (orig);
  1230. i2mod_new_rhs = copy_rtx (note);
  1231. next = try_combine (insn, i2mod, NULL, NULL,
  1232. &new_direct_jump_p,
  1233. last_combined_insn);
  1234. i2mod = NULL;
  1235. if (next)
  1236. {
  1237. statistics_counter_event (cfun, "insn-with-note combine", 1);
  1238. goto retry;
  1239. }
  1240. SET_SRC (set) = orig;
  1241. }
  1242. }
  1243. if (!NOTE_P (insn))
  1244. record_dead_and_set_regs (insn);
  1245. retry:
  1246. ;
  1247. }
  1248. }
  1249. default_rtl_profile ();
  1250. clear_bb_flags ();
  1251. new_direct_jump_p |= purge_all_dead_edges ();
  1252. delete_noop_moves ();
  1253. /* Clean up. */
  1254. obstack_free (&insn_link_obstack, NULL);
  1255. free (uid_log_links);
  1256. free (uid_insn_cost);
  1257. reg_stat.release ();
  1258. {
  1259. struct undo *undo, *next;
  1260. for (undo = undobuf.frees; undo; undo = next)
  1261. {
  1262. next = undo->next;
  1263. free (undo);
  1264. }
  1265. undobuf.frees = 0;
  1266. }
  1267. total_attempts += combine_attempts;
  1268. total_merges += combine_merges;
  1269. total_extras += combine_extras;
  1270. total_successes += combine_successes;
  1271. nonzero_sign_valid = 0;
  1272. rtl_hooks = general_rtl_hooks;
  1273. /* Make recognizer allow volatile MEMs again. */
  1274. init_recog ();
  1275. return new_direct_jump_p;
  1276. }
  1277. /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
  1278. static void
  1279. init_reg_last (void)
  1280. {
  1281. unsigned int i;
  1282. reg_stat_type *p;
  1283. FOR_EACH_VEC_ELT (reg_stat, i, p)
  1284. memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
  1285. }
  1286. /* Set up any promoted values for incoming argument registers. */
  1287. static void
  1288. setup_incoming_promotions (rtx_insn *first)
  1289. {
  1290. tree arg;
  1291. bool strictly_local = false;
  1292. for (arg = DECL_ARGUMENTS (current_function_decl); arg;
  1293. arg = DECL_CHAIN (arg))
  1294. {
  1295. rtx x, reg = DECL_INCOMING_RTL (arg);
  1296. int uns1, uns3;
  1297. machine_mode mode1, mode2, mode3, mode4;
  1298. /* Only continue if the incoming argument is in a register. */
  1299. if (!REG_P (reg))
  1300. continue;
  1301. /* Determine, if possible, whether all call sites of the current
  1302. function lie within the current compilation unit. (This does
  1303. take into account the exporting of a function via taking its
  1304. address, and so forth.) */
  1305. strictly_local = cgraph_node::local_info (current_function_decl)->local;
  1306. /* The mode and signedness of the argument before any promotions happen
  1307. (equal to the mode of the pseudo holding it at that stage). */
  1308. mode1 = TYPE_MODE (TREE_TYPE (arg));
  1309. uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
  1310. /* The mode and signedness of the argument after any source language and
  1311. TARGET_PROMOTE_PROTOTYPES-driven promotions. */
  1312. mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
  1313. uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
  1314. /* The mode and signedness of the argument as it is actually passed,
  1315. see assign_parm_setup_reg in function.c. */
  1316. mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
  1317. TREE_TYPE (cfun->decl), 0);
  1318. /* The mode of the register in which the argument is being passed. */
  1319. mode4 = GET_MODE (reg);
  1320. /* Eliminate sign extensions in the callee when:
  1321. (a) A mode promotion has occurred; */
  1322. if (mode1 == mode3)
  1323. continue;
  1324. /* (b) The mode of the register is the same as the mode of
  1325. the argument as it is passed; */
  1326. if (mode3 != mode4)
  1327. continue;
  1328. /* (c) There's no language level extension; */
  1329. if (mode1 == mode2)
  1330. ;
  1331. /* (c.1) All callers are from the current compilation unit. If that's
  1332. the case we don't have to rely on an ABI, we only have to know
  1333. what we're generating right now, and we know that we will do the
  1334. mode1 to mode2 promotion with the given sign. */
  1335. else if (!strictly_local)
  1336. continue;
  1337. /* (c.2) The combination of the two promotions is useful. This is
  1338. true when the signs match, or if the first promotion is unsigned.
  1339. In the later case, (sign_extend (zero_extend x)) is the same as
  1340. (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
  1341. else if (uns1)
  1342. uns3 = true;
  1343. else if (uns3)
  1344. continue;
  1345. /* Record that the value was promoted from mode1 to mode3,
  1346. so that any sign extension at the head of the current
  1347. function may be eliminated. */
  1348. x = gen_rtx_CLOBBER (mode1, const0_rtx);
  1349. x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
  1350. record_value_for_reg (reg, first, x);
  1351. }
  1352. }
  1353. /* Called via note_stores. If X is a pseudo that is narrower than
  1354. HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
  1355. If we are setting only a portion of X and we can't figure out what
  1356. portion, assume all bits will be used since we don't know what will
  1357. be happening.
  1358. Similarly, set how many bits of X are known to be copies of the sign bit
  1359. at all locations in the function. This is the smallest number implied
  1360. by any set of X. */
  1361. static void
  1362. set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
  1363. {
  1364. rtx_insn *insn = (rtx_insn *) data;
  1365. unsigned int num;
  1366. if (REG_P (x)
  1367. && REGNO (x) >= FIRST_PSEUDO_REGISTER
  1368. /* If this register is undefined at the start of the file, we can't
  1369. say what its contents were. */
  1370. && ! REGNO_REG_SET_P
  1371. (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
  1372. && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
  1373. {
  1374. reg_stat_type *rsp = &reg_stat[REGNO (x)];
  1375. if (set == 0 || GET_CODE (set) == CLOBBER)
  1376. {
  1377. rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
  1378. rsp->sign_bit_copies = 1;
  1379. return;
  1380. }
  1381. /* If this register is being initialized using itself, and the
  1382. register is uninitialized in this basic block, and there are
  1383. no LOG_LINKS which set the register, then part of the
  1384. register is uninitialized. In that case we can't assume
  1385. anything about the number of nonzero bits.
  1386. ??? We could do better if we checked this in
  1387. reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
  1388. could avoid making assumptions about the insn which initially
  1389. sets the register, while still using the information in other
  1390. insns. We would have to be careful to check every insn
  1391. involved in the combination. */
  1392. if (insn
  1393. && reg_referenced_p (x, PATTERN (insn))
  1394. && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
  1395. REGNO (x)))
  1396. {
  1397. struct insn_link *link;
  1398. FOR_EACH_LOG_LINK (link, insn)
  1399. if (dead_or_set_p (link->insn, x))
  1400. break;
  1401. if (!link)
  1402. {
  1403. rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
  1404. rsp->sign_bit_copies = 1;
  1405. return;
  1406. }
  1407. }
  1408. /* If this is a complex assignment, see if we can convert it into a
  1409. simple assignment. */
  1410. set = expand_field_assignment (set);
  1411. /* If this is a simple assignment, or we have a paradoxical SUBREG,
  1412. set what we know about X. */
  1413. if (SET_DEST (set) == x
  1414. || (paradoxical_subreg_p (SET_DEST (set))
  1415. && SUBREG_REG (SET_DEST (set)) == x))
  1416. {
  1417. rtx src = SET_SRC (set);
  1418. #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
  1419. /* If X is narrower than a word and SRC is a non-negative
  1420. constant that would appear negative in the mode of X,
  1421. sign-extend it for use in reg_stat[].nonzero_bits because some
  1422. machines (maybe most) will actually do the sign-extension
  1423. and this is the conservative approach.
  1424. ??? For 2.5, try to tighten up the MD files in this regard
  1425. instead of this kludge. */
  1426. if (GET_MODE_PRECISION (GET_MODE (x)) < BITS_PER_WORD
  1427. && CONST_INT_P (src)
  1428. && INTVAL (src) > 0
  1429. && val_signbit_known_set_p (GET_MODE (x), INTVAL (src)))
  1430. src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (GET_MODE (x)));
  1431. #endif
  1432. /* Don't call nonzero_bits if it cannot change anything. */
  1433. if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
  1434. rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
  1435. num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
  1436. if (rsp->sign_bit_copies == 0
  1437. || rsp->sign_bit_copies > num)
  1438. rsp->sign_bit_copies = num;
  1439. }
  1440. else
  1441. {
  1442. rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
  1443. rsp->sign_bit_copies = 1;
  1444. }
  1445. }
  1446. }
  1447. /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
  1448. optionally insns that were previously combined into I3 or that will be
  1449. combined into the merger of INSN and I3. The order is PRED, PRED2,
  1450. INSN, SUCC, SUCC2, I3.
  1451. Return 0 if the combination is not allowed for any reason.
  1452. If the combination is allowed, *PDEST will be set to the single
  1453. destination of INSN and *PSRC to the single source, and this function
  1454. will return 1. */
  1455. static int
  1456. can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
  1457. rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
  1458. rtx *pdest, rtx *psrc)
  1459. {
  1460. int i;
  1461. const_rtx set = 0;
  1462. rtx src, dest;
  1463. rtx_insn *p;
  1464. #ifdef AUTO_INC_DEC
  1465. rtx link;
  1466. #endif
  1467. bool all_adjacent = true;
  1468. int (*is_volatile_p) (const_rtx);
  1469. if (succ)
  1470. {
  1471. if (succ2)
  1472. {
  1473. if (next_active_insn (succ2) != i3)
  1474. all_adjacent = false;
  1475. if (next_active_insn (succ) != succ2)
  1476. all_adjacent = false;
  1477. }
  1478. else if (next_active_insn (succ) != i3)
  1479. all_adjacent = false;
  1480. if (next_active_insn (insn) != succ)
  1481. all_adjacent = false;
  1482. }
  1483. else if (next_active_insn (insn) != i3)
  1484. all_adjacent = false;
  1485. /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
  1486. or a PARALLEL consisting of such a SET and CLOBBERs.
  1487. If INSN has CLOBBER parallel parts, ignore them for our processing.
  1488. By definition, these happen during the execution of the insn. When it
  1489. is merged with another insn, all bets are off. If they are, in fact,
  1490. needed and aren't also supplied in I3, they may be added by
  1491. recog_for_combine. Otherwise, it won't match.
  1492. We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
  1493. note.
  1494. Get the source and destination of INSN. If more than one, can't
  1495. combine. */
  1496. if (GET_CODE (PATTERN (insn)) == SET)
  1497. set = PATTERN (insn);
  1498. else if (GET_CODE (PATTERN (insn)) == PARALLEL
  1499. && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
  1500. {
  1501. for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
  1502. {
  1503. rtx elt = XVECEXP (PATTERN (insn), 0, i);
  1504. switch (GET_CODE (elt))
  1505. {
  1506. /* This is important to combine floating point insns
  1507. for the SH4 port. */
  1508. case USE:
  1509. /* Combining an isolated USE doesn't make sense.
  1510. We depend here on combinable_i3pat to reject them. */
  1511. /* The code below this loop only verifies that the inputs of
  1512. the SET in INSN do not change. We call reg_set_between_p
  1513. to verify that the REG in the USE does not change between
  1514. I3 and INSN.
  1515. If the USE in INSN was for a pseudo register, the matching
  1516. insn pattern will likely match any register; combining this
  1517. with any other USE would only be safe if we knew that the
  1518. used registers have identical values, or if there was
  1519. something to tell them apart, e.g. different modes. For
  1520. now, we forgo such complicated tests and simply disallow
  1521. combining of USES of pseudo registers with any other USE. */
  1522. if (REG_P (XEXP (elt, 0))
  1523. && GET_CODE (PATTERN (i3)) == PARALLEL)
  1524. {
  1525. rtx i3pat = PATTERN (i3);
  1526. int i = XVECLEN (i3pat, 0) - 1;
  1527. unsigned int regno = REGNO (XEXP (elt, 0));
  1528. do
  1529. {
  1530. rtx i3elt = XVECEXP (i3pat, 0, i);
  1531. if (GET_CODE (i3elt) == USE
  1532. && REG_P (XEXP (i3elt, 0))
  1533. && (REGNO (XEXP (i3elt, 0)) == regno
  1534. ? reg_set_between_p (XEXP (elt, 0),
  1535. PREV_INSN (insn), i3)
  1536. : regno >= FIRST_PSEUDO_REGISTER))
  1537. return 0;
  1538. }
  1539. while (--i >= 0);
  1540. }
  1541. break;
  1542. /* We can ignore CLOBBERs. */
  1543. case CLOBBER:
  1544. break;
  1545. case SET:
  1546. /* Ignore SETs whose result isn't used but not those that
  1547. have side-effects. */
  1548. if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
  1549. && insn_nothrow_p (insn)
  1550. && !side_effects_p (elt))
  1551. break;
  1552. /* If we have already found a SET, this is a second one and
  1553. so we cannot combine with this insn. */
  1554. if (set)
  1555. return 0;
  1556. set = elt;
  1557. break;
  1558. default:
  1559. /* Anything else means we can't combine. */
  1560. return 0;
  1561. }
  1562. }
  1563. if (set == 0
  1564. /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
  1565. so don't do anything with it. */
  1566. || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
  1567. return 0;
  1568. }
  1569. else
  1570. return 0;
  1571. if (set == 0)
  1572. return 0;
  1573. /* The simplification in expand_field_assignment may call back to
  1574. get_last_value, so set safe guard here. */
  1575. subst_low_luid = DF_INSN_LUID (insn);
  1576. set = expand_field_assignment (set);
  1577. src = SET_SRC (set), dest = SET_DEST (set);
  1578. /* Don't eliminate a store in the stack pointer. */
  1579. if (dest == stack_pointer_rtx
  1580. /* Don't combine with an insn that sets a register to itself if it has
  1581. a REG_EQUAL note. This may be part of a LIBCALL sequence. */
  1582. || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
  1583. /* Can't merge an ASM_OPERANDS. */
  1584. || GET_CODE (src) == ASM_OPERANDS
  1585. /* Can't merge a function call. */
  1586. || GET_CODE (src) == CALL
  1587. /* Don't eliminate a function call argument. */
  1588. || (CALL_P (i3)
  1589. && (find_reg_fusage (i3, USE, dest)
  1590. || (REG_P (dest)
  1591. && REGNO (dest) < FIRST_PSEUDO_REGISTER
  1592. && global_regs[REGNO (dest)])))
  1593. /* Don't substitute into an incremented register. */
  1594. || FIND_REG_INC_NOTE (i3, dest)
  1595. || (succ && FIND_REG_INC_NOTE (succ, dest))
  1596. || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
  1597. /* Don't substitute into a non-local goto, this confuses CFG. */
  1598. || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
  1599. /* Make sure that DEST is not used after SUCC but before I3. */
  1600. || (!all_adjacent
  1601. && ((succ2
  1602. && (reg_used_between_p (dest, succ2, i3)
  1603. || reg_used_between_p (dest, succ, succ2)))
  1604. || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
  1605. /* Make sure that the value that is to be substituted for the register
  1606. does not use any registers whose values alter in between. However,
  1607. If the insns are adjacent, a use can't cross a set even though we
  1608. think it might (this can happen for a sequence of insns each setting
  1609. the same destination; last_set of that register might point to
  1610. a NOTE). If INSN has a REG_EQUIV note, the register is always
  1611. equivalent to the memory so the substitution is valid even if there
  1612. are intervening stores. Also, don't move a volatile asm or
  1613. UNSPEC_VOLATILE across any other insns. */
  1614. || (! all_adjacent
  1615. && (((!MEM_P (src)
  1616. || ! find_reg_note (insn, REG_EQUIV, src))
  1617. && use_crosses_set_p (src, DF_INSN_LUID (insn)))
  1618. || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
  1619. || GET_CODE (src) == UNSPEC_VOLATILE))
  1620. /* Don't combine across a CALL_INSN, because that would possibly
  1621. change whether the life span of some REGs crosses calls or not,
  1622. and it is a pain to update that information.
  1623. Exception: if source is a constant, moving it later can't hurt.
  1624. Accept that as a special case. */
  1625. || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
  1626. return 0;
  1627. /* DEST must either be a REG or CC0. */
  1628. if (REG_P (dest))
  1629. {
  1630. /* If register alignment is being enforced for multi-word items in all
  1631. cases except for parameters, it is possible to have a register copy
  1632. insn referencing a hard register that is not allowed to contain the
  1633. mode being copied and which would not be valid as an operand of most
  1634. insns. Eliminate this problem by not combining with such an insn.
  1635. Also, on some machines we don't want to extend the life of a hard
  1636. register. */
  1637. if (REG_P (src)
  1638. && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
  1639. && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
  1640. /* Don't extend the life of a hard register unless it is
  1641. user variable (if we have few registers) or it can't
  1642. fit into the desired register (meaning something special
  1643. is going on).
  1644. Also avoid substituting a return register into I3, because
  1645. reload can't handle a conflict with constraints of other
  1646. inputs. */
  1647. || (REGNO (src) < FIRST_PSEUDO_REGISTER
  1648. && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
  1649. return 0;
  1650. }
  1651. else if (GET_CODE (dest) != CC0)
  1652. return 0;
  1653. if (GET_CODE (PATTERN (i3)) == PARALLEL)
  1654. for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
  1655. if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
  1656. {
  1657. rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
  1658. /* If the clobber represents an earlyclobber operand, we must not
  1659. substitute an expression containing the clobbered register.
  1660. As we do not analyze the constraint strings here, we have to
  1661. make the conservative assumption. However, if the register is
  1662. a fixed hard reg, the clobber cannot represent any operand;
  1663. we leave it up to the machine description to either accept or
  1664. reject use-and-clobber patterns. */
  1665. if (!REG_P (reg)
  1666. || REGNO (reg) >= FIRST_PSEUDO_REGISTER
  1667. || !fixed_regs[REGNO (reg)])
  1668. if (reg_overlap_mentioned_p (reg, src))
  1669. return 0;
  1670. }
  1671. /* If INSN contains anything volatile, or is an `asm' (whether volatile
  1672. or not), reject, unless nothing volatile comes between it and I3 */
  1673. if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
  1674. {
  1675. /* Make sure neither succ nor succ2 contains a volatile reference. */
  1676. if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
  1677. return 0;
  1678. if (succ != 0 && volatile_refs_p (PATTERN (succ)))
  1679. return 0;
  1680. /* We'll check insns between INSN and I3 below. */
  1681. }
  1682. /* If INSN is an asm, and DEST is a hard register, reject, since it has
  1683. to be an explicit register variable, and was chosen for a reason. */
  1684. if (GET_CODE (src) == ASM_OPERANDS
  1685. && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
  1686. return 0;
  1687. /* If INSN contains volatile references (specifically volatile MEMs),
  1688. we cannot combine across any other volatile references.
  1689. Even if INSN doesn't contain volatile references, any intervening
  1690. volatile insn might affect machine state. */
  1691. is_volatile_p = volatile_refs_p (PATTERN (insn))
  1692. ? volatile_refs_p
  1693. : volatile_insn_p;
  1694. for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
  1695. if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
  1696. return 0;
  1697. /* If INSN contains an autoincrement or autodecrement, make sure that
  1698. register is not used between there and I3, and not already used in
  1699. I3 either. Neither must it be used in PRED or SUCC, if they exist.
  1700. Also insist that I3 not be a jump; if it were one
  1701. and the incremented register were spilled, we would lose. */
  1702. #ifdef AUTO_INC_DEC
  1703. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1704. if (REG_NOTE_KIND (link) == REG_INC
  1705. && (JUMP_P (i3)
  1706. || reg_used_between_p (XEXP (link, 0), insn, i3)
  1707. || (pred != NULL_RTX
  1708. && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
  1709. || (pred2 != NULL_RTX
  1710. && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
  1711. || (succ != NULL_RTX
  1712. && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
  1713. || (succ2 != NULL_RTX
  1714. && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
  1715. || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
  1716. return 0;
  1717. #endif
  1718. #ifdef HAVE_cc0
  1719. /* Don't combine an insn that follows a CC0-setting insn.
  1720. An insn that uses CC0 must not be separated from the one that sets it.
  1721. We do, however, allow I2 to follow a CC0-setting insn if that insn
  1722. is passed as I1; in that case it will be deleted also.
  1723. We also allow combining in this case if all the insns are adjacent
  1724. because that would leave the two CC0 insns adjacent as well.
  1725. It would be more logical to test whether CC0 occurs inside I1 or I2,
  1726. but that would be much slower, and this ought to be equivalent. */
  1727. p = prev_nonnote_insn (insn);
  1728. if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
  1729. && ! all_adjacent)
  1730. return 0;
  1731. #endif
  1732. /* If we get here, we have passed all the tests and the combination is
  1733. to be allowed. */
  1734. *pdest = dest;
  1735. *psrc = src;
  1736. return 1;
  1737. }
  1738. /* LOC is the location within I3 that contains its pattern or the component
  1739. of a PARALLEL of the pattern. We validate that it is valid for combining.
  1740. One problem is if I3 modifies its output, as opposed to replacing it
  1741. entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
  1742. doing so would produce an insn that is not equivalent to the original insns.
  1743. Consider:
  1744. (set (reg:DI 101) (reg:DI 100))
  1745. (set (subreg:SI (reg:DI 101) 0) <foo>)
  1746. This is NOT equivalent to:
  1747. (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
  1748. (set (reg:DI 101) (reg:DI 100))])
  1749. Not only does this modify 100 (in which case it might still be valid
  1750. if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
  1751. We can also run into a problem if I2 sets a register that I1
  1752. uses and I1 gets directly substituted into I3 (not via I2). In that
  1753. case, we would be getting the wrong value of I2DEST into I3, so we
  1754. must reject the combination. This case occurs when I2 and I1 both
  1755. feed into I3, rather than when I1 feeds into I2, which feeds into I3.
  1756. If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
  1757. of a SET must prevent combination from occurring. The same situation
  1758. can occur for I0, in which case I0_NOT_IN_SRC is set.
  1759. Before doing the above check, we first try to expand a field assignment
  1760. into a set of logical operations.
  1761. If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
  1762. we place a register that is both set and used within I3. If more than one
  1763. such register is detected, we fail.
  1764. Return 1 if the combination is valid, zero otherwise. */
  1765. static int
  1766. combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
  1767. int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
  1768. {
  1769. rtx x = *loc;
  1770. if (GET_CODE (x) == SET)
  1771. {
  1772. rtx set = x ;
  1773. rtx dest = SET_DEST (set);
  1774. rtx src = SET_SRC (set);
  1775. rtx inner_dest = dest;
  1776. rtx subdest;
  1777. while (GET_CODE (inner_dest) == STRICT_LOW_PART
  1778. || GET_CODE (inner_dest) == SUBREG
  1779. || GET_CODE (inner_dest) == ZERO_EXTRACT)
  1780. inner_dest = XEXP (inner_dest, 0);
  1781. /* Check for the case where I3 modifies its output, as discussed
  1782. above. We don't want to prevent pseudos from being combined
  1783. into the address of a MEM, so only prevent the combination if
  1784. i1 or i2 set the same MEM. */
  1785. if ((inner_dest != dest &&
  1786. (!MEM_P (inner_dest)
  1787. || rtx_equal_p (i2dest, inner_dest)
  1788. || (i1dest && rtx_equal_p (i1dest, inner_dest))
  1789. || (i0dest && rtx_equal_p (i0dest, inner_dest)))
  1790. && (reg_overlap_mentioned_p (i2dest, inner_dest)
  1791. || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
  1792. || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
  1793. /* This is the same test done in can_combine_p except we can't test
  1794. all_adjacent; we don't have to, since this instruction will stay
  1795. in place, thus we are not considering increasing the lifetime of
  1796. INNER_DEST.
  1797. Also, if this insn sets a function argument, combining it with
  1798. something that might need a spill could clobber a previous
  1799. function argument; the all_adjacent test in can_combine_p also
  1800. checks this; here, we do a more specific test for this case. */
  1801. || (REG_P (inner_dest)
  1802. && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
  1803. && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
  1804. GET_MODE (inner_dest))))
  1805. || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
  1806. || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
  1807. return 0;
  1808. /* If DEST is used in I3, it is being killed in this insn, so
  1809. record that for later. We have to consider paradoxical
  1810. subregs here, since they kill the whole register, but we
  1811. ignore partial subregs, STRICT_LOW_PART, etc.
  1812. Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
  1813. STACK_POINTER_REGNUM, since these are always considered to be
  1814. live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
  1815. subdest = dest;
  1816. if (GET_CODE (subdest) == SUBREG
  1817. && (GET_MODE_SIZE (GET_MODE (subdest))
  1818. >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
  1819. subdest = SUBREG_REG (subdest);
  1820. if (pi3dest_killed
  1821. && REG_P (subdest)
  1822. && reg_referenced_p (subdest, PATTERN (i3))
  1823. && REGNO (subdest) != FRAME_POINTER_REGNUM
  1824. #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
  1825. && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
  1826. #endif
  1827. #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
  1828. && (REGNO (subdest) != ARG_POINTER_REGNUM
  1829. || ! fixed_regs [REGNO (subdest)])
  1830. #endif
  1831. && REGNO (subdest) != STACK_POINTER_REGNUM)
  1832. {
  1833. if (*pi3dest_killed)
  1834. return 0;
  1835. *pi3dest_killed = subdest;
  1836. }
  1837. }
  1838. else if (GET_CODE (x) == PARALLEL)
  1839. {
  1840. int i;
  1841. for (i = 0; i < XVECLEN (x, 0); i++)
  1842. if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
  1843. i1_not_in_src, i0_not_in_src, pi3dest_killed))
  1844. return 0;
  1845. }
  1846. return 1;
  1847. }
  1848. /* Return 1 if X is an arithmetic expression that contains a multiplication
  1849. and division. We don't count multiplications by powers of two here. */
  1850. static int
  1851. contains_muldiv (rtx x)
  1852. {
  1853. switch (GET_CODE (x))
  1854. {
  1855. case MOD: case DIV: case UMOD: case UDIV:
  1856. return 1;
  1857. case MULT:
  1858. return ! (CONST_INT_P (XEXP (x, 1))
  1859. && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
  1860. default:
  1861. if (BINARY_P (x))
  1862. return contains_muldiv (XEXP (x, 0))
  1863. || contains_muldiv (XEXP (x, 1));
  1864. if (UNARY_P (x))
  1865. return contains_muldiv (XEXP (x, 0));
  1866. return 0;
  1867. }
  1868. }
  1869. /* Determine whether INSN can be used in a combination. Return nonzero if
  1870. not. This is used in try_combine to detect early some cases where we
  1871. can't perform combinations. */
  1872. static int
  1873. cant_combine_insn_p (rtx_insn *insn)
  1874. {
  1875. rtx set;
  1876. rtx src, dest;
  1877. /* If this isn't really an insn, we can't do anything.
  1878. This can occur when flow deletes an insn that it has merged into an
  1879. auto-increment address. */
  1880. if (! INSN_P (insn))
  1881. return 1;
  1882. /* Never combine loads and stores involving hard regs that are likely
  1883. to be spilled. The register allocator can usually handle such
  1884. reg-reg moves by tying. If we allow the combiner to make
  1885. substitutions of likely-spilled regs, reload might die.
  1886. As an exception, we allow combinations involving fixed regs; these are
  1887. not available to the register allocator so there's no risk involved. */
  1888. set = single_set (insn);
  1889. if (! set)
  1890. return 0;
  1891. src = SET_SRC (set);
  1892. dest = SET_DEST (set);
  1893. if (GET_CODE (src) == SUBREG)
  1894. src = SUBREG_REG (src);
  1895. if (GET_CODE (dest) == SUBREG)
  1896. dest = SUBREG_REG (dest);
  1897. if (REG_P (src) && REG_P (dest)
  1898. && ((HARD_REGISTER_P (src)
  1899. && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
  1900. && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
  1901. || (HARD_REGISTER_P (dest)
  1902. && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
  1903. && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
  1904. return 1;
  1905. return 0;
  1906. }
  1907. struct likely_spilled_retval_info
  1908. {
  1909. unsigned regno, nregs;
  1910. unsigned mask;
  1911. };
  1912. /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
  1913. hard registers that are known to be written to / clobbered in full. */
  1914. static void
  1915. likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
  1916. {
  1917. struct likely_spilled_retval_info *const info =
  1918. (struct likely_spilled_retval_info *) data;
  1919. unsigned regno, nregs;
  1920. unsigned new_mask;
  1921. if (!REG_P (XEXP (set, 0)))
  1922. return;
  1923. regno = REGNO (x);
  1924. if (regno >= info->regno + info->nregs)
  1925. return;
  1926. nregs = hard_regno_nregs[regno][GET_MODE (x)];
  1927. if (regno + nregs <= info->regno)
  1928. return;
  1929. new_mask = (2U << (nregs - 1)) - 1;
  1930. if (regno < info->regno)
  1931. new_mask >>= info->regno - regno;
  1932. else
  1933. new_mask <<= regno - info->regno;
  1934. info->mask &= ~new_mask;
  1935. }
  1936. /* Return nonzero iff part of the return value is live during INSN, and
  1937. it is likely spilled. This can happen when more than one insn is needed
  1938. to copy the return value, e.g. when we consider to combine into the
  1939. second copy insn for a complex value. */
  1940. static int
  1941. likely_spilled_retval_p (rtx_insn *insn)
  1942. {
  1943. rtx_insn *use = BB_END (this_basic_block);
  1944. rtx reg;
  1945. rtx_insn *p;
  1946. unsigned regno, nregs;
  1947. /* We assume here that no machine mode needs more than
  1948. 32 hard registers when the value overlaps with a register
  1949. for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
  1950. unsigned mask;
  1951. struct likely_spilled_retval_info info;
  1952. if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
  1953. return 0;
  1954. reg = XEXP (PATTERN (use), 0);
  1955. if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
  1956. return 0;
  1957. regno = REGNO (reg);
  1958. nregs = hard_regno_nregs[regno][GET_MODE (reg)];
  1959. if (nregs == 1)
  1960. return 0;
  1961. mask = (2U << (nregs - 1)) - 1;
  1962. /* Disregard parts of the return value that are set later. */
  1963. info.regno = regno;
  1964. info.nregs = nregs;
  1965. info.mask = mask;
  1966. for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
  1967. if (INSN_P (p))
  1968. note_stores (PATTERN (p), likely_spilled_retval_1, &info);
  1969. mask = info.mask;
  1970. /* Check if any of the (probably) live return value registers is
  1971. likely spilled. */
  1972. nregs --;
  1973. do
  1974. {
  1975. if ((mask & 1 << nregs)
  1976. && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
  1977. return 1;
  1978. } while (nregs--);
  1979. return 0;
  1980. }
  1981. /* Adjust INSN after we made a change to its destination.
  1982. Changing the destination can invalidate notes that say something about
  1983. the results of the insn and a LOG_LINK pointing to the insn. */
  1984. static void
  1985. adjust_for_new_dest (rtx_insn *insn)
  1986. {
  1987. /* For notes, be conservative and simply remove them. */
  1988. remove_reg_equal_equiv_notes (insn);
  1989. /* The new insn will have a destination that was previously the destination
  1990. of an insn just above it. Call distribute_links to make a LOG_LINK from
  1991. the next use of that destination. */
  1992. rtx set = single_set (insn);
  1993. gcc_assert (set);
  1994. rtx reg = SET_DEST (set);
  1995. while (GET_CODE (reg) == ZERO_EXTRACT
  1996. || GET_CODE (reg) == STRICT_LOW_PART
  1997. || GET_CODE (reg) == SUBREG)
  1998. reg = XEXP (reg, 0);
  1999. gcc_assert (REG_P (reg));
  2000. distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
  2001. df_insn_rescan (insn);
  2002. }
  2003. /* Return TRUE if combine can reuse reg X in mode MODE.
  2004. ADDED_SETS is nonzero if the original set is still required. */
  2005. static bool
  2006. can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
  2007. {
  2008. unsigned int regno;
  2009. if (!REG_P (x))
  2010. return false;
  2011. regno = REGNO (x);
  2012. /* Allow hard registers if the new mode is legal, and occupies no more
  2013. registers than the old mode. */
  2014. if (regno < FIRST_PSEUDO_REGISTER)
  2015. return (HARD_REGNO_MODE_OK (regno, mode)
  2016. && (hard_regno_nregs[regno][GET_MODE (x)]
  2017. >= hard_regno_nregs[regno][mode]));
  2018. /* Or a pseudo that is only used once. */
  2019. return (regno < reg_n_sets_max
  2020. && REG_N_SETS (regno) == 1
  2021. && !added_sets
  2022. && !REG_USERVAR_P (x));
  2023. }
  2024. /* Check whether X, the destination of a set, refers to part of
  2025. the register specified by REG. */
  2026. static bool
  2027. reg_subword_p (rtx x, rtx reg)
  2028. {
  2029. /* Check that reg is an integer mode register. */
  2030. if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
  2031. return false;
  2032. if (GET_CODE (x) == STRICT_LOW_PART
  2033. || GET_CODE (x) == ZERO_EXTRACT)
  2034. x = XEXP (x, 0);
  2035. return GET_CODE (x) == SUBREG
  2036. && SUBREG_REG (x) == reg
  2037. && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
  2038. }
  2039. /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
  2040. Note that the INSN should be deleted *after* removing dead edges, so
  2041. that the kept edge is the fallthrough edge for a (set (pc) (pc))
  2042. but not for a (set (pc) (label_ref FOO)). */
  2043. static void
  2044. update_cfg_for_uncondjump (rtx_insn *insn)
  2045. {
  2046. basic_block bb = BLOCK_FOR_INSN (insn);
  2047. gcc_assert (BB_END (bb) == insn);
  2048. purge_dead_edges (bb);
  2049. delete_insn (insn);
  2050. if (EDGE_COUNT (bb->succs) == 1)
  2051. {
  2052. rtx_insn *insn;
  2053. single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
  2054. /* Remove barriers from the footer if there are any. */
  2055. for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
  2056. if (BARRIER_P (insn))
  2057. {
  2058. if (PREV_INSN (insn))
  2059. SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
  2060. else
  2061. BB_FOOTER (bb) = NEXT_INSN (insn);
  2062. if (NEXT_INSN (insn))
  2063. SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
  2064. }
  2065. else if (LABEL_P (insn))
  2066. break;
  2067. }
  2068. }
  2069. /* Return whether PAT is a PARALLEL of exactly N register SETs followed
  2070. by an arbitrary number of CLOBBERs. */
  2071. static bool
  2072. is_parallel_of_n_reg_sets (rtx pat, int n)
  2073. {
  2074. if (GET_CODE (pat) != PARALLEL)
  2075. return false;
  2076. int len = XVECLEN (pat, 0);
  2077. if (len < n)
  2078. return false;
  2079. int i;
  2080. for (i = 0; i < n; i++)
  2081. if (GET_CODE (XVECEXP (pat, 0, i)) != SET
  2082. || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
  2083. return false;
  2084. for ( ; i < len; i++)
  2085. if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
  2086. return false;
  2087. return true;
  2088. }
  2089. #ifndef HAVE_cc0
  2090. /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
  2091. CLOBBERs), can be split into individual SETs in that order, without
  2092. changing semantics. */
  2093. static bool
  2094. can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
  2095. {
  2096. if (!insn_nothrow_p (insn))
  2097. return false;
  2098. rtx pat = PATTERN (insn);
  2099. int i, j;
  2100. for (i = 0; i < n; i++)
  2101. {
  2102. if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
  2103. return false;
  2104. rtx reg = SET_DEST (XVECEXP (pat, 0, i));
  2105. for (j = i + 1; j < n; j++)
  2106. if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
  2107. return false;
  2108. }
  2109. return true;
  2110. }
  2111. #endif
  2112. /* Try to combine the insns I0, I1 and I2 into I3.
  2113. Here I0, I1 and I2 appear earlier than I3.
  2114. I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
  2115. I3.
  2116. If we are combining more than two insns and the resulting insn is not
  2117. recognized, try splitting it into two insns. If that happens, I2 and I3
  2118. are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
  2119. Otherwise, I0, I1 and I2 are pseudo-deleted.
  2120. Return 0 if the combination does not work. Then nothing is changed.
  2121. If we did the combination, return the insn at which combine should
  2122. resume scanning.
  2123. Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
  2124. new direct jump instruction.
  2125. LAST_COMBINED_INSN is either I3, or some insn after I3 that has
  2126. been I3 passed to an earlier try_combine within the same basic
  2127. block. */
  2128. static rtx_insn *
  2129. try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
  2130. int *new_direct_jump_p, rtx_insn *last_combined_insn)
  2131. {
  2132. /* New patterns for I3 and I2, respectively. */
  2133. rtx newpat, newi2pat = 0;
  2134. rtvec newpat_vec_with_clobbers = 0;
  2135. int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
  2136. /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
  2137. dead. */
  2138. int added_sets_0, added_sets_1, added_sets_2;
  2139. /* Total number of SETs to put into I3. */
  2140. int total_sets;
  2141. /* Nonzero if I2's or I1's body now appears in I3. */
  2142. int i2_is_used = 0, i1_is_used = 0;
  2143. /* INSN_CODEs for new I3, new I2, and user of condition code. */
  2144. int insn_code_number, i2_code_number = 0, other_code_number = 0;
  2145. /* Contains I3 if the destination of I3 is used in its source, which means
  2146. that the old life of I3 is being killed. If that usage is placed into
  2147. I2 and not in I3, a REG_DEAD note must be made. */
  2148. rtx i3dest_killed = 0;
  2149. /* SET_DEST and SET_SRC of I2, I1 and I0. */
  2150. rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
  2151. /* Copy of SET_SRC of I1 and I0, if needed. */
  2152. rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
  2153. /* Set if I2DEST was reused as a scratch register. */
  2154. bool i2scratch = false;
  2155. /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
  2156. rtx i0pat = 0, i1pat = 0, i2pat = 0;
  2157. /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
  2158. int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
  2159. int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
  2160. int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
  2161. int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
  2162. /* Notes that must be added to REG_NOTES in I3 and I2. */
  2163. rtx new_i3_notes, new_i2_notes;
  2164. /* Notes that we substituted I3 into I2 instead of the normal case. */
  2165. int i3_subst_into_i2 = 0;
  2166. /* Notes that I1, I2 or I3 is a MULT operation. */
  2167. int have_mult = 0;
  2168. int swap_i2i3 = 0;
  2169. int changed_i3_dest = 0;
  2170. int maxreg;
  2171. rtx_insn *temp_insn;
  2172. rtx temp_expr;
  2173. struct insn_link *link;
  2174. rtx other_pat = 0;
  2175. rtx new_other_notes;
  2176. int i;
  2177. /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
  2178. never be). */
  2179. if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
  2180. return 0;
  2181. /* Only try four-insn combinations when there's high likelihood of
  2182. success. Look for simple insns, such as loads of constants or
  2183. binary operations involving a constant. */
  2184. if (i0)
  2185. {
  2186. int i;
  2187. int ngood = 0;
  2188. int nshift = 0;
  2189. rtx set0, set3;
  2190. if (!flag_expensive_optimizations)
  2191. return 0;
  2192. for (i = 0; i < 4; i++)
  2193. {
  2194. rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
  2195. rtx set = single_set (insn);
  2196. rtx src;
  2197. if (!set)
  2198. continue;
  2199. src = SET_SRC (set);
  2200. if (CONSTANT_P (src))
  2201. {
  2202. ngood += 2;
  2203. break;
  2204. }
  2205. else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
  2206. ngood++;
  2207. else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
  2208. || GET_CODE (src) == LSHIFTRT)
  2209. nshift++;
  2210. }
  2211. /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
  2212. are likely manipulating its value. Ideally we'll be able to combine
  2213. all four insns into a bitfield insertion of some kind.
  2214. Note the source in I0 might be inside a sign/zero extension and the
  2215. memory modes in I0 and I3 might be different. So extract the address
  2216. from the destination of I3 and search for it in the source of I0.
  2217. In the event that there's a match but the source/dest do not actually
  2218. refer to the same memory, the worst that happens is we try some
  2219. combinations that we wouldn't have otherwise. */
  2220. if ((set0 = single_set (i0))
  2221. /* Ensure the source of SET0 is a MEM, possibly buried inside
  2222. an extension. */
  2223. && (GET_CODE (SET_SRC (set0)) == MEM
  2224. || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
  2225. || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
  2226. && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
  2227. && (set3 = single_set (i3))
  2228. /* Ensure the destination of SET3 is a MEM. */
  2229. && GET_CODE (SET_DEST (set3)) == MEM
  2230. /* Would it be better to extract the base address for the MEM
  2231. in SET3 and look for that? I don't have cases where it matters
  2232. but I could envision such cases. */
  2233. && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
  2234. ngood += 2;
  2235. if (ngood < 2 && nshift < 2)
  2236. return 0;
  2237. }
  2238. /* Exit early if one of the insns involved can't be used for
  2239. combinations. */
  2240. if (CALL_P (i2)
  2241. || (i1 && CALL_P (i1))
  2242. || (i0 && CALL_P (i0))
  2243. || cant_combine_insn_p (i3)
  2244. || cant_combine_insn_p (i2)
  2245. || (i1 && cant_combine_insn_p (i1))
  2246. || (i0 && cant_combine_insn_p (i0))
  2247. || likely_spilled_retval_p (i3))
  2248. return 0;
  2249. combine_attempts++;
  2250. undobuf.other_insn = 0;
  2251. /* Reset the hard register usage information. */
  2252. CLEAR_HARD_REG_SET (newpat_used_regs);
  2253. if (dump_file && (dump_flags & TDF_DETAILS))
  2254. {
  2255. if (i0)
  2256. fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
  2257. INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
  2258. else if (i1)
  2259. fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
  2260. INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
  2261. else
  2262. fprintf (dump_file, "\nTrying %d -> %d:\n",
  2263. INSN_UID (i2), INSN_UID (i3));
  2264. }
  2265. /* If multiple insns feed into one of I2 or I3, they can be in any
  2266. order. To simplify the code below, reorder them in sequence. */
  2267. if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
  2268. temp_insn = i2, i2 = i0, i0 = temp_insn;
  2269. if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
  2270. temp_insn = i1, i1 = i0, i0 = temp_insn;
  2271. if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
  2272. temp_insn = i1, i1 = i2, i2 = temp_insn;
  2273. added_links_insn = 0;
  2274. /* First check for one important special case that the code below will
  2275. not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
  2276. and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
  2277. we may be able to replace that destination with the destination of I3.
  2278. This occurs in the common code where we compute both a quotient and
  2279. remainder into a structure, in which case we want to do the computation
  2280. directly into the structure to avoid register-register copies.
  2281. Note that this case handles both multiple sets in I2 and also cases
  2282. where I2 has a number of CLOBBERs inside the PARALLEL.
  2283. We make very conservative checks below and only try to handle the
  2284. most common cases of this. For example, we only handle the case
  2285. where I2 and I3 are adjacent to avoid making difficult register
  2286. usage tests. */
  2287. if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
  2288. && REG_P (SET_SRC (PATTERN (i3)))
  2289. && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
  2290. && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
  2291. && GET_CODE (PATTERN (i2)) == PARALLEL
  2292. && ! side_effects_p (SET_DEST (PATTERN (i3)))
  2293. /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
  2294. below would need to check what is inside (and reg_overlap_mentioned_p
  2295. doesn't support those codes anyway). Don't allow those destinations;
  2296. the resulting insn isn't likely to be recognized anyway. */
  2297. && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
  2298. && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
  2299. && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
  2300. SET_DEST (PATTERN (i3)))
  2301. && next_active_insn (i2) == i3)
  2302. {
  2303. rtx p2 = PATTERN (i2);
  2304. /* Make sure that the destination of I3,
  2305. which we are going to substitute into one output of I2,
  2306. is not used within another output of I2. We must avoid making this:
  2307. (parallel [(set (mem (reg 69)) ...)
  2308. (set (reg 69) ...)])
  2309. which is not well-defined as to order of actions.
  2310. (Besides, reload can't handle output reloads for this.)
  2311. The problem can also happen if the dest of I3 is a memory ref,
  2312. if another dest in I2 is an indirect memory ref. */
  2313. for (i = 0; i < XVECLEN (p2, 0); i++)
  2314. if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
  2315. || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
  2316. && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
  2317. SET_DEST (XVECEXP (p2, 0, i))))
  2318. break;
  2319. /* Make sure this PARALLEL is not an asm. We do not allow combining
  2320. that usually (see can_combine_p), so do not here either. */
  2321. for (i = 0; i < XVECLEN (p2, 0); i++)
  2322. if (GET_CODE (XVECEXP (p2, 0, i)) == SET
  2323. && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
  2324. break;
  2325. if (i == XVECLEN (p2, 0))
  2326. for (i = 0; i < XVECLEN (p2, 0); i++)
  2327. if (GET_CODE (XVECEXP (p2, 0, i)) == SET
  2328. && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
  2329. {
  2330. combine_merges++;
  2331. subst_insn = i3;
  2332. subst_low_luid = DF_INSN_LUID (i2);
  2333. added_sets_2 = added_sets_1 = added_sets_0 = 0;
  2334. i2src = SET_SRC (XVECEXP (p2, 0, i));
  2335. i2dest = SET_DEST (XVECEXP (p2, 0, i));
  2336. i2dest_killed = dead_or_set_p (i2, i2dest);
  2337. /* Replace the dest in I2 with our dest and make the resulting
  2338. insn the new pattern for I3. Then skip to where we validate
  2339. the pattern. Everything was set up above. */
  2340. SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
  2341. newpat = p2;
  2342. i3_subst_into_i2 = 1;
  2343. goto validate_replacement;
  2344. }
  2345. }
  2346. /* If I2 is setting a pseudo to a constant and I3 is setting some
  2347. sub-part of it to another constant, merge them by making a new
  2348. constant. */
  2349. if (i1 == 0
  2350. && (temp_expr = single_set (i2)) != 0
  2351. && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
  2352. && GET_CODE (PATTERN (i3)) == SET
  2353. && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
  2354. && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
  2355. {
  2356. rtx dest = SET_DEST (PATTERN (i3));
  2357. int offset = -1;
  2358. int width = 0;
  2359. if (GET_CODE (dest) == ZERO_EXTRACT)
  2360. {
  2361. if (CONST_INT_P (XEXP (dest, 1))
  2362. && CONST_INT_P (XEXP (dest, 2)))
  2363. {
  2364. width = INTVAL (XEXP (dest, 1));
  2365. offset = INTVAL (XEXP (dest, 2));
  2366. dest = XEXP (dest, 0);
  2367. if (BITS_BIG_ENDIAN)
  2368. offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
  2369. }
  2370. }
  2371. else
  2372. {
  2373. if (GET_CODE (dest) == STRICT_LOW_PART)
  2374. dest = XEXP (dest, 0);
  2375. width = GET_MODE_PRECISION (GET_MODE (dest));
  2376. offset = 0;
  2377. }
  2378. if (offset >= 0)
  2379. {
  2380. /* If this is the low part, we're done. */
  2381. if (subreg_lowpart_p (dest))
  2382. ;
  2383. /* Handle the case where inner is twice the size of outer. */
  2384. else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
  2385. == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
  2386. offset += GET_MODE_PRECISION (GET_MODE (dest));
  2387. /* Otherwise give up for now. */
  2388. else
  2389. offset = -1;
  2390. }
  2391. if (offset >= 0)
  2392. {
  2393. rtx inner = SET_SRC (PATTERN (i3));
  2394. rtx outer = SET_SRC (temp_expr);
  2395. wide_int o
  2396. = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
  2397. std::make_pair (inner, GET_MODE (dest)),
  2398. offset, width);
  2399. combine_merges++;
  2400. subst_insn = i3;
  2401. subst_low_luid = DF_INSN_LUID (i2);
  2402. added_sets_2 = added_sets_1 = added_sets_0 = 0;
  2403. i2dest = SET_DEST (temp_expr);
  2404. i2dest_killed = dead_or_set_p (i2, i2dest);
  2405. /* Replace the source in I2 with the new constant and make the
  2406. resulting insn the new pattern for I3. Then skip to where we
  2407. validate the pattern. Everything was set up above. */
  2408. SUBST (SET_SRC (temp_expr),
  2409. immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
  2410. newpat = PATTERN (i2);
  2411. /* The dest of I3 has been replaced with the dest of I2. */
  2412. changed_i3_dest = 1;
  2413. goto validate_replacement;
  2414. }
  2415. }
  2416. #ifndef HAVE_cc0
  2417. /* If we have no I1 and I2 looks like:
  2418. (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
  2419. (set Y OP)])
  2420. make up a dummy I1 that is
  2421. (set Y OP)
  2422. and change I2 to be
  2423. (set (reg:CC X) (compare:CC Y (const_int 0)))
  2424. (We can ignore any trailing CLOBBERs.)
  2425. This undoes a previous combination and allows us to match a branch-and-
  2426. decrement insn. */
  2427. if (i1 == 0
  2428. && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
  2429. && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
  2430. == MODE_CC)
  2431. && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
  2432. && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
  2433. && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
  2434. SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
  2435. && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
  2436. && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
  2437. {
  2438. /* We make I1 with the same INSN_UID as I2. This gives it
  2439. the same DF_INSN_LUID for value tracking. Our fake I1 will
  2440. never appear in the insn stream so giving it the same INSN_UID
  2441. as I2 will not cause a problem. */
  2442. i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
  2443. XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
  2444. -1, NULL_RTX);
  2445. INSN_UID (i1) = INSN_UID (i2);
  2446. SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
  2447. SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
  2448. SET_DEST (PATTERN (i1)));
  2449. unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
  2450. SUBST_LINK (LOG_LINKS (i2),
  2451. alloc_insn_link (i1, regno, LOG_LINKS (i2)));
  2452. }
  2453. /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
  2454. make those two SETs separate I1 and I2 insns, and make an I0 that is
  2455. the original I1. */
  2456. if (i0 == 0
  2457. && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
  2458. && can_split_parallel_of_n_reg_sets (i2, 2)
  2459. && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
  2460. && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
  2461. {
  2462. /* If there is no I1, there is no I0 either. */
  2463. i0 = i1;
  2464. /* We make I1 with the same INSN_UID as I2. This gives it
  2465. the same DF_INSN_LUID for value tracking. Our fake I1 will
  2466. never appear in the insn stream so giving it the same INSN_UID
  2467. as I2 will not cause a problem. */
  2468. i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
  2469. XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
  2470. -1, NULL_RTX);
  2471. INSN_UID (i1) = INSN_UID (i2);
  2472. SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
  2473. }
  2474. #endif
  2475. /* Verify that I2 and I1 are valid for combining. */
  2476. if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
  2477. || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
  2478. &i1dest, &i1src))
  2479. || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
  2480. &i0dest, &i0src)))
  2481. {
  2482. undo_all ();
  2483. return 0;
  2484. }
  2485. /* Record whether I2DEST is used in I2SRC and similarly for the other
  2486. cases. Knowing this will help in register status updating below. */
  2487. i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
  2488. i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
  2489. i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
  2490. i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
  2491. i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
  2492. i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
  2493. i2dest_killed = dead_or_set_p (i2, i2dest);
  2494. i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
  2495. i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
  2496. /* For the earlier insns, determine which of the subsequent ones they
  2497. feed. */
  2498. i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
  2499. i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
  2500. i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
  2501. : (!reg_overlap_mentioned_p (i1dest, i0dest)
  2502. && reg_overlap_mentioned_p (i0dest, i2src))));
  2503. /* Ensure that I3's pattern can be the destination of combines. */
  2504. if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
  2505. i1 && i2dest_in_i1src && !i1_feeds_i2_n,
  2506. i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
  2507. || (i1dest_in_i0src && !i0_feeds_i1_n)),
  2508. &i3dest_killed))
  2509. {
  2510. undo_all ();
  2511. return 0;
  2512. }
  2513. /* See if any of the insns is a MULT operation. Unless one is, we will
  2514. reject a combination that is, since it must be slower. Be conservative
  2515. here. */
  2516. if (GET_CODE (i2src) == MULT
  2517. || (i1 != 0 && GET_CODE (i1src) == MULT)
  2518. || (i0 != 0 && GET_CODE (i0src) == MULT)
  2519. || (GET_CODE (PATTERN (i3)) == SET
  2520. && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
  2521. have_mult = 1;
  2522. /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
  2523. We used to do this EXCEPT in one case: I3 has a post-inc in an
  2524. output operand. However, that exception can give rise to insns like
  2525. mov r3,(r3)+
  2526. which is a famous insn on the PDP-11 where the value of r3 used as the
  2527. source was model-dependent. Avoid this sort of thing. */
  2528. #if 0
  2529. if (!(GET_CODE (PATTERN (i3)) == SET
  2530. && REG_P (SET_SRC (PATTERN (i3)))
  2531. && MEM_P (SET_DEST (PATTERN (i3)))
  2532. && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
  2533. || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
  2534. /* It's not the exception. */
  2535. #endif
  2536. #ifdef AUTO_INC_DEC
  2537. {
  2538. rtx link;
  2539. for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
  2540. if (REG_NOTE_KIND (link) == REG_INC
  2541. && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
  2542. || (i1 != 0
  2543. && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
  2544. {
  2545. undo_all ();
  2546. return 0;
  2547. }
  2548. }
  2549. #endif
  2550. /* See if the SETs in I1 or I2 need to be kept around in the merged
  2551. instruction: whenever the value set there is still needed past I3.
  2552. For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
  2553. For the SET in I1, we have two cases: if I1 and I2 independently feed
  2554. into I3, the set in I1 needs to be kept around unless I1DEST dies
  2555. or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
  2556. in I1 needs to be kept around unless I1DEST dies or is set in either
  2557. I2 or I3. The same considerations apply to I0. */
  2558. added_sets_2 = !dead_or_set_p (i3, i2dest);
  2559. if (i1)
  2560. added_sets_1 = !(dead_or_set_p (i3, i1dest)
  2561. || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
  2562. else
  2563. added_sets_1 = 0;
  2564. if (i0)
  2565. added_sets_0 = !(dead_or_set_p (i3, i0dest)
  2566. || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
  2567. || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
  2568. && dead_or_set_p (i2, i0dest)));
  2569. else
  2570. added_sets_0 = 0;
  2571. /* We are about to copy insns for the case where they need to be kept
  2572. around. Check that they can be copied in the merged instruction. */
  2573. if (targetm.cannot_copy_insn_p
  2574. && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
  2575. || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
  2576. || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
  2577. {
  2578. undo_all ();
  2579. return 0;
  2580. }
  2581. /* If the set in I2 needs to be kept around, we must make a copy of
  2582. PATTERN (I2), so that when we substitute I1SRC for I1DEST in
  2583. PATTERN (I2), we are only substituting for the original I1DEST, not into
  2584. an already-substituted copy. This also prevents making self-referential
  2585. rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
  2586. I2DEST. */
  2587. if (added_sets_2)
  2588. {
  2589. if (GET_CODE (PATTERN (i2)) == PARALLEL)
  2590. i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
  2591. else
  2592. i2pat = copy_rtx (PATTERN (i2));
  2593. }
  2594. if (added_sets_1)
  2595. {
  2596. if (GET_CODE (PATTERN (i1)) == PARALLEL)
  2597. i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
  2598. else
  2599. i1pat = copy_rtx (PATTERN (i1));
  2600. }
  2601. if (added_sets_0)
  2602. {
  2603. if (GET_CODE (PATTERN (i0)) == PARALLEL)
  2604. i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
  2605. else
  2606. i0pat = copy_rtx (PATTERN (i0));
  2607. }
  2608. combine_merges++;
  2609. /* Substitute in the latest insn for the regs set by the earlier ones. */
  2610. maxreg = max_reg_num ();
  2611. subst_insn = i3;
  2612. #ifndef HAVE_cc0
  2613. /* Many machines that don't use CC0 have insns that can both perform an
  2614. arithmetic operation and set the condition code. These operations will
  2615. be represented as a PARALLEL with the first element of the vector
  2616. being a COMPARE of an arithmetic operation with the constant zero.
  2617. The second element of the vector will set some pseudo to the result
  2618. of the same arithmetic operation. If we simplify the COMPARE, we won't
  2619. match such a pattern and so will generate an extra insn. Here we test
  2620. for this case, where both the comparison and the operation result are
  2621. needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
  2622. I2SRC. Later we will make the PARALLEL that contains I2. */
  2623. if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
  2624. && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
  2625. && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
  2626. && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
  2627. {
  2628. rtx newpat_dest;
  2629. rtx *cc_use_loc = NULL;
  2630. rtx_insn *cc_use_insn = NULL;
  2631. rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
  2632. machine_mode compare_mode, orig_compare_mode;
  2633. enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
  2634. newpat = PATTERN (i3);
  2635. newpat_dest = SET_DEST (newpat);
  2636. compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
  2637. if (undobuf.other_insn == 0
  2638. && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
  2639. &cc_use_insn)))
  2640. {
  2641. compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
  2642. compare_code = simplify_compare_const (compare_code,
  2643. GET_MODE (i2dest), op0, &op1);
  2644. target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
  2645. }
  2646. /* Do the rest only if op1 is const0_rtx, which may be the
  2647. result of simplification. */
  2648. if (op1 == const0_rtx)
  2649. {
  2650. /* If a single use of the CC is found, prepare to modify it
  2651. when SELECT_CC_MODE returns a new CC-class mode, or when
  2652. the above simplify_compare_const() returned a new comparison
  2653. operator. undobuf.other_insn is assigned the CC use insn
  2654. when modifying it. */
  2655. if (cc_use_loc)
  2656. {
  2657. #ifdef SELECT_CC_MODE
  2658. machine_mode new_mode
  2659. = SELECT_CC_MODE (compare_code, op0, op1);
  2660. if (new_mode != orig_compare_mode
  2661. && can_change_dest_mode (SET_DEST (newpat),
  2662. added_sets_2, new_mode))
  2663. {
  2664. unsigned int regno = REGNO (newpat_dest);
  2665. compare_mode = new_mode;
  2666. if (regno < FIRST_PSEUDO_REGISTER)
  2667. newpat_dest = gen_rtx_REG (compare_mode, regno);
  2668. else
  2669. {
  2670. SUBST_MODE (regno_reg_rtx[regno], compare_mode);
  2671. newpat_dest = regno_reg_rtx[regno];
  2672. }
  2673. }
  2674. #endif
  2675. /* Cases for modifying the CC-using comparison. */
  2676. if (compare_code != orig_compare_code
  2677. /* ??? Do we need to verify the zero rtx? */
  2678. && XEXP (*cc_use_loc, 1) == const0_rtx)
  2679. {
  2680. /* Replace cc_use_loc with entire new RTX. */
  2681. SUBST (*cc_use_loc,
  2682. gen_rtx_fmt_ee (compare_code, compare_mode,
  2683. newpat_dest, const0_rtx));
  2684. undobuf.other_insn = cc_use_insn;
  2685. }
  2686. else if (compare_mode != orig_compare_mode)
  2687. {
  2688. /* Just replace the CC reg with a new mode. */
  2689. SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
  2690. undobuf.other_insn = cc_use_insn;
  2691. }
  2692. }
  2693. /* Now we modify the current newpat:
  2694. First, SET_DEST(newpat) is updated if the CC mode has been
  2695. altered. For targets without SELECT_CC_MODE, this should be
  2696. optimized away. */
  2697. if (compare_mode != orig_compare_mode)
  2698. SUBST (SET_DEST (newpat), newpat_dest);
  2699. /* This is always done to propagate i2src into newpat. */
  2700. SUBST (SET_SRC (newpat),
  2701. gen_rtx_COMPARE (compare_mode, op0, op1));
  2702. /* Create new version of i2pat if needed; the below PARALLEL
  2703. creation needs this to work correctly. */
  2704. if (! rtx_equal_p (i2src, op0))
  2705. i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
  2706. i2_is_used = 1;
  2707. }
  2708. }
  2709. #endif
  2710. if (i2_is_used == 0)
  2711. {
  2712. /* It is possible that the source of I2 or I1 may be performing
  2713. an unneeded operation, such as a ZERO_EXTEND of something
  2714. that is known to have the high part zero. Handle that case
  2715. by letting subst look at the inner insns.
  2716. Another way to do this would be to have a function that tries
  2717. to simplify a single insn instead of merging two or more
  2718. insns. We don't do this because of the potential of infinite
  2719. loops and because of the potential extra memory required.
  2720. However, doing it the way we are is a bit of a kludge and
  2721. doesn't catch all cases.
  2722. But only do this if -fexpensive-optimizations since it slows
  2723. things down and doesn't usually win.
  2724. This is not done in the COMPARE case above because the
  2725. unmodified I2PAT is used in the PARALLEL and so a pattern
  2726. with a modified I2SRC would not match. */
  2727. if (flag_expensive_optimizations)
  2728. {
  2729. /* Pass pc_rtx so no substitutions are done, just
  2730. simplifications. */
  2731. if (i1)
  2732. {
  2733. subst_low_luid = DF_INSN_LUID (i1);
  2734. i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
  2735. }
  2736. subst_low_luid = DF_INSN_LUID (i2);
  2737. i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
  2738. }
  2739. n_occurrences = 0; /* `subst' counts here */
  2740. subst_low_luid = DF_INSN_LUID (i2);
  2741. /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
  2742. copy of I2SRC each time we substitute it, in order to avoid creating
  2743. self-referential RTL when we will be substituting I1SRC for I1DEST
  2744. later. Likewise if I0 feeds into I2, either directly or indirectly
  2745. through I1, and I0DEST is in I0SRC. */
  2746. newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
  2747. (i1_feeds_i2_n && i1dest_in_i1src)
  2748. || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
  2749. && i0dest_in_i0src));
  2750. substed_i2 = 1;
  2751. /* Record whether I2's body now appears within I3's body. */
  2752. i2_is_used = n_occurrences;
  2753. }
  2754. /* If we already got a failure, don't try to do more. Otherwise, try to
  2755. substitute I1 if we have it. */
  2756. if (i1 && GET_CODE (newpat) != CLOBBER)
  2757. {
  2758. /* Check that an autoincrement side-effect on I1 has not been lost.
  2759. This happens if I1DEST is mentioned in I2 and dies there, and
  2760. has disappeared from the new pattern. */
  2761. if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
  2762. && i1_feeds_i2_n
  2763. && dead_or_set_p (i2, i1dest)
  2764. && !reg_overlap_mentioned_p (i1dest, newpat))
  2765. /* Before we can do this substitution, we must redo the test done
  2766. above (see detailed comments there) that ensures I1DEST isn't
  2767. mentioned in any SETs in NEWPAT that are field assignments. */
  2768. || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
  2769. 0, 0, 0))
  2770. {
  2771. undo_all ();
  2772. return 0;
  2773. }
  2774. n_occurrences = 0;
  2775. subst_low_luid = DF_INSN_LUID (i1);
  2776. /* If the following substitution will modify I1SRC, make a copy of it
  2777. for the case where it is substituted for I1DEST in I2PAT later. */
  2778. if (added_sets_2 && i1_feeds_i2_n)
  2779. i1src_copy = copy_rtx (i1src);
  2780. /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
  2781. copy of I1SRC each time we substitute it, in order to avoid creating
  2782. self-referential RTL when we will be substituting I0SRC for I0DEST
  2783. later. */
  2784. newpat = subst (newpat, i1dest, i1src, 0, 0,
  2785. i0_feeds_i1_n && i0dest_in_i0src);
  2786. substed_i1 = 1;
  2787. /* Record whether I1's body now appears within I3's body. */
  2788. i1_is_used = n_occurrences;
  2789. }
  2790. /* Likewise for I0 if we have it. */
  2791. if (i0 && GET_CODE (newpat) != CLOBBER)
  2792. {
  2793. if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
  2794. && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
  2795. || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
  2796. && !reg_overlap_mentioned_p (i0dest, newpat))
  2797. || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
  2798. 0, 0, 0))
  2799. {
  2800. undo_all ();
  2801. return 0;
  2802. }
  2803. /* If the following substitution will modify I0SRC, make a copy of it
  2804. for the case where it is substituted for I0DEST in I1PAT later. */
  2805. if (added_sets_1 && i0_feeds_i1_n)
  2806. i0src_copy = copy_rtx (i0src);
  2807. /* And a copy for I0DEST in I2PAT substitution. */
  2808. if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
  2809. || (i0_feeds_i2_n)))
  2810. i0src_copy2 = copy_rtx (i0src);
  2811. n_occurrences = 0;
  2812. subst_low_luid = DF_INSN_LUID (i0);
  2813. newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
  2814. substed_i0 = 1;
  2815. }
  2816. /* Fail if an autoincrement side-effect has been duplicated. Be careful
  2817. to count all the ways that I2SRC and I1SRC can be used. */
  2818. if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
  2819. && i2_is_used + added_sets_2 > 1)
  2820. || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
  2821. && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
  2822. > 1))
  2823. || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
  2824. && (n_occurrences + added_sets_0
  2825. + (added_sets_1 && i0_feeds_i1_n)
  2826. + (added_sets_2 && i0_feeds_i2_n)
  2827. > 1))
  2828. /* Fail if we tried to make a new register. */
  2829. || max_reg_num () != maxreg
  2830. /* Fail if we couldn't do something and have a CLOBBER. */
  2831. || GET_CODE (newpat) == CLOBBER
  2832. /* Fail if this new pattern is a MULT and we didn't have one before
  2833. at the outer level. */
  2834. || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
  2835. && ! have_mult))
  2836. {
  2837. undo_all ();
  2838. return 0;
  2839. }
  2840. /* If the actions of the earlier insns must be kept
  2841. in addition to substituting them into the latest one,
  2842. we must make a new PARALLEL for the latest insn
  2843. to hold additional the SETs. */
  2844. if (added_sets_0 || added_sets_1 || added_sets_2)
  2845. {
  2846. int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
  2847. combine_extras++;
  2848. if (GET_CODE (newpat) == PARALLEL)
  2849. {
  2850. rtvec old = XVEC (newpat, 0);
  2851. total_sets = XVECLEN (newpat, 0) + extra_sets;
  2852. newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
  2853. memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
  2854. sizeof (old->elem[0]) * old->num_elem);
  2855. }
  2856. else
  2857. {
  2858. rtx old = newpat;
  2859. total_sets = 1 + extra_sets;
  2860. newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
  2861. XVECEXP (newpat, 0, 0) = old;
  2862. }
  2863. if (added_sets_0)
  2864. XVECEXP (newpat, 0, --total_sets) = i0pat;
  2865. if (added_sets_1)
  2866. {
  2867. rtx t = i1pat;
  2868. if (i0_feeds_i1_n)
  2869. t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
  2870. XVECEXP (newpat, 0, --total_sets) = t;
  2871. }
  2872. if (added_sets_2)
  2873. {
  2874. rtx t = i2pat;
  2875. if (i1_feeds_i2_n)
  2876. t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
  2877. i0_feeds_i1_n && i0dest_in_i0src);
  2878. if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
  2879. t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
  2880. XVECEXP (newpat, 0, --total_sets) = t;
  2881. }
  2882. }
  2883. validate_replacement:
  2884. /* Note which hard regs this insn has as inputs. */
  2885. mark_used_regs_combine (newpat);
  2886. /* If recog_for_combine fails, it strips existing clobbers. If we'll
  2887. consider splitting this pattern, we might need these clobbers. */
  2888. if (i1 && GET_CODE (newpat) == PARALLEL
  2889. && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
  2890. {
  2891. int len = XVECLEN (newpat, 0);
  2892. newpat_vec_with_clobbers = rtvec_alloc (len);
  2893. for (i = 0; i < len; i++)
  2894. RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
  2895. }
  2896. /* We have recognized nothing yet. */
  2897. insn_code_number = -1;
  2898. /* See if this is a PARALLEL of two SETs where one SET's destination is
  2899. a register that is unused and this isn't marked as an instruction that
  2900. might trap in an EH region. In that case, we just need the other SET.
  2901. We prefer this over the PARALLEL.
  2902. This can occur when simplifying a divmod insn. We *must* test for this
  2903. case here because the code below that splits two independent SETs doesn't
  2904. handle this case correctly when it updates the register status.
  2905. It's pointless doing this if we originally had two sets, one from
  2906. i3, and one from i2. Combining then splitting the parallel results
  2907. in the original i2 again plus an invalid insn (which we delete).
  2908. The net effect is only to move instructions around, which makes
  2909. debug info less accurate. */
  2910. if (!(added_sets_2 && i1 == 0)
  2911. && is_parallel_of_n_reg_sets (newpat, 2)
  2912. && asm_noperands (newpat) < 0)
  2913. {
  2914. rtx set0 = XVECEXP (newpat, 0, 0);
  2915. rtx set1 = XVECEXP (newpat, 0, 1);
  2916. rtx oldpat = newpat;
  2917. if (((REG_P (SET_DEST (set1))
  2918. && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
  2919. || (GET_CODE (SET_DEST (set1)) == SUBREG
  2920. && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
  2921. && insn_nothrow_p (i3)
  2922. && !side_effects_p (SET_SRC (set1)))
  2923. {
  2924. newpat = set0;
  2925. insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
  2926. }
  2927. else if (((REG_P (SET_DEST (set0))
  2928. && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
  2929. || (GET_CODE (SET_DEST (set0)) == SUBREG
  2930. && find_reg_note (i3, REG_UNUSED,
  2931. SUBREG_REG (SET_DEST (set0)))))
  2932. && insn_nothrow_p (i3)
  2933. && !side_effects_p (SET_SRC (set0)))
  2934. {
  2935. newpat = set1;
  2936. insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
  2937. if (insn_code_number >= 0)
  2938. changed_i3_dest = 1;
  2939. }
  2940. if (insn_code_number < 0)
  2941. newpat = oldpat;
  2942. }
  2943. /* Is the result of combination a valid instruction? */
  2944. if (insn_code_number < 0)
  2945. insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
  2946. /* If we were combining three insns and the result is a simple SET
  2947. with no ASM_OPERANDS that wasn't recognized, try to split it into two
  2948. insns. There are two ways to do this. It can be split using a
  2949. machine-specific method (like when you have an addition of a large
  2950. constant) or by combine in the function find_split_point. */
  2951. if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
  2952. && asm_noperands (newpat) < 0)
  2953. {
  2954. rtx parallel, *split;
  2955. rtx_insn *m_split_insn;
  2956. /* See if the MD file can split NEWPAT. If it can't, see if letting it
  2957. use I2DEST as a scratch register will help. In the latter case,
  2958. convert I2DEST to the mode of the source of NEWPAT if we can. */
  2959. m_split_insn = combine_split_insns (newpat, i3);
  2960. /* We can only use I2DEST as a scratch reg if it doesn't overlap any
  2961. inputs of NEWPAT. */
  2962. /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
  2963. possible to try that as a scratch reg. This would require adding
  2964. more code to make it work though. */
  2965. if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
  2966. {
  2967. machine_mode new_mode = GET_MODE (SET_DEST (newpat));
  2968. /* First try to split using the original register as a
  2969. scratch register. */
  2970. parallel = gen_rtx_PARALLEL (VOIDmode,
  2971. gen_rtvec (2, newpat,
  2972. gen_rtx_CLOBBER (VOIDmode,
  2973. i2dest)));
  2974. m_split_insn = combine_split_insns (parallel, i3);
  2975. /* If that didn't work, try changing the mode of I2DEST if
  2976. we can. */
  2977. if (m_split_insn == 0
  2978. && new_mode != GET_MODE (i2dest)
  2979. && new_mode != VOIDmode
  2980. && can_change_dest_mode (i2dest, added_sets_2, new_mode))
  2981. {
  2982. machine_mode old_mode = GET_MODE (i2dest);
  2983. rtx ni2dest;
  2984. if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
  2985. ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
  2986. else
  2987. {
  2988. SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
  2989. ni2dest = regno_reg_rtx[REGNO (i2dest)];
  2990. }
  2991. parallel = (gen_rtx_PARALLEL
  2992. (VOIDmode,
  2993. gen_rtvec (2, newpat,
  2994. gen_rtx_CLOBBER (VOIDmode,
  2995. ni2dest))));
  2996. m_split_insn = combine_split_insns (parallel, i3);
  2997. if (m_split_insn == 0
  2998. && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
  2999. {
  3000. struct undo *buf;
  3001. adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
  3002. buf = undobuf.undos;
  3003. undobuf.undos = buf->next;
  3004. buf->next = undobuf.frees;
  3005. undobuf.frees = buf;
  3006. }
  3007. }
  3008. i2scratch = m_split_insn != 0;
  3009. }
  3010. /* If recog_for_combine has discarded clobbers, try to use them
  3011. again for the split. */
  3012. if (m_split_insn == 0 && newpat_vec_with_clobbers)
  3013. {
  3014. parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
  3015. m_split_insn = combine_split_insns (parallel, i3);
  3016. }
  3017. if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
  3018. {
  3019. rtx m_split_pat = PATTERN (m_split_insn);
  3020. insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
  3021. if (insn_code_number >= 0)
  3022. newpat = m_split_pat;
  3023. }
  3024. else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
  3025. && (next_nonnote_nondebug_insn (i2) == i3
  3026. || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
  3027. {
  3028. rtx i2set, i3set;
  3029. rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
  3030. newi2pat = PATTERN (m_split_insn);
  3031. i3set = single_set (NEXT_INSN (m_split_insn));
  3032. i2set = single_set (m_split_insn);
  3033. i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
  3034. /* If I2 or I3 has multiple SETs, we won't know how to track
  3035. register status, so don't use these insns. If I2's destination
  3036. is used between I2 and I3, we also can't use these insns. */
  3037. if (i2_code_number >= 0 && i2set && i3set
  3038. && (next_nonnote_nondebug_insn (i2) == i3
  3039. || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
  3040. insn_code_number = recog_for_combine (&newi3pat, i3,
  3041. &new_i3_notes);
  3042. if (insn_code_number >= 0)
  3043. newpat = newi3pat;
  3044. /* It is possible that both insns now set the destination of I3.
  3045. If so, we must show an extra use of it. */
  3046. if (insn_code_number >= 0)
  3047. {
  3048. rtx new_i3_dest = SET_DEST (i3set);
  3049. rtx new_i2_dest = SET_DEST (i2set);
  3050. while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
  3051. || GET_CODE (new_i3_dest) == STRICT_LOW_PART
  3052. || GET_CODE (new_i3_dest) == SUBREG)
  3053. new_i3_dest = XEXP (new_i3_dest, 0);
  3054. while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
  3055. || GET_CODE (new_i2_dest) == STRICT_LOW_PART
  3056. || GET_CODE (new_i2_dest) == SUBREG)
  3057. new_i2_dest = XEXP (new_i2_dest, 0);
  3058. if (REG_P (new_i3_dest)
  3059. && REG_P (new_i2_dest)
  3060. && REGNO (new_i3_dest) == REGNO (new_i2_dest)
  3061. && REGNO (new_i2_dest) < reg_n_sets_max)
  3062. INC_REG_N_SETS (REGNO (new_i2_dest), 1);
  3063. }
  3064. }
  3065. /* If we can split it and use I2DEST, go ahead and see if that
  3066. helps things be recognized. Verify that none of the registers
  3067. are set between I2 and I3. */
  3068. if (insn_code_number < 0
  3069. && (split = find_split_point (&newpat, i3, false)) != 0
  3070. #ifdef HAVE_cc0
  3071. && REG_P (i2dest)
  3072. #endif
  3073. /* We need I2DEST in the proper mode. If it is a hard register
  3074. or the only use of a pseudo, we can change its mode.
  3075. Make sure we don't change a hard register to have a mode that
  3076. isn't valid for it, or change the number of registers. */
  3077. && (GET_MODE (*split) == GET_MODE (i2dest)
  3078. || GET_MODE (*split) == VOIDmode
  3079. || can_change_dest_mode (i2dest, added_sets_2,
  3080. GET_MODE (*split)))
  3081. && (next_nonnote_nondebug_insn (i2) == i3
  3082. || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
  3083. /* We can't overwrite I2DEST if its value is still used by
  3084. NEWPAT. */
  3085. && ! reg_referenced_p (i2dest, newpat))
  3086. {
  3087. rtx newdest = i2dest;
  3088. enum rtx_code split_code = GET_CODE (*split);
  3089. machine_mode split_mode = GET_MODE (*split);
  3090. bool subst_done = false;
  3091. newi2pat = NULL_RTX;
  3092. i2scratch = true;
  3093. /* *SPLIT may be part of I2SRC, so make sure we have the
  3094. original expression around for later debug processing.
  3095. We should not need I2SRC any more in other cases. */
  3096. if (MAY_HAVE_DEBUG_INSNS)
  3097. i2src = copy_rtx (i2src);
  3098. else
  3099. i2src = NULL;
  3100. /* Get NEWDEST as a register in the proper mode. We have already
  3101. validated that we can do this. */
  3102. if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
  3103. {
  3104. if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
  3105. newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
  3106. else
  3107. {
  3108. SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
  3109. newdest = regno_reg_rtx[REGNO (i2dest)];
  3110. }
  3111. }
  3112. /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
  3113. an ASHIFT. This can occur if it was inside a PLUS and hence
  3114. appeared to be a memory address. This is a kludge. */
  3115. if (split_code == MULT
  3116. && CONST_INT_P (XEXP (*split, 1))
  3117. && INTVAL (XEXP (*split, 1)) > 0
  3118. && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
  3119. {
  3120. SUBST (*split, gen_rtx_ASHIFT (split_mode,
  3121. XEXP (*split, 0), GEN_INT (i)));
  3122. /* Update split_code because we may not have a multiply
  3123. anymore. */
  3124. split_code = GET_CODE (*split);
  3125. }
  3126. #ifdef INSN_SCHEDULING
  3127. /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
  3128. be written as a ZERO_EXTEND. */
  3129. if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
  3130. {
  3131. #ifdef LOAD_EXTEND_OP
  3132. /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
  3133. what it really is. */
  3134. if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
  3135. == SIGN_EXTEND)
  3136. SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
  3137. SUBREG_REG (*split)));
  3138. else
  3139. #endif
  3140. SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
  3141. SUBREG_REG (*split)));
  3142. }
  3143. #endif
  3144. /* Attempt to split binary operators using arithmetic identities. */
  3145. if (BINARY_P (SET_SRC (newpat))
  3146. && split_mode == GET_MODE (SET_SRC (newpat))
  3147. && ! side_effects_p (SET_SRC (newpat)))
  3148. {
  3149. rtx setsrc = SET_SRC (newpat);
  3150. machine_mode mode = GET_MODE (setsrc);
  3151. enum rtx_code code = GET_CODE (setsrc);
  3152. rtx src_op0 = XEXP (setsrc, 0);
  3153. rtx src_op1 = XEXP (setsrc, 1);
  3154. /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
  3155. if (rtx_equal_p (src_op0, src_op1))
  3156. {
  3157. newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
  3158. SUBST (XEXP (setsrc, 0), newdest);
  3159. SUBST (XEXP (setsrc, 1), newdest);
  3160. subst_done = true;
  3161. }
  3162. /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
  3163. else if ((code == PLUS || code == MULT)
  3164. && GET_CODE (src_op0) == code
  3165. && GET_CODE (XEXP (src_op0, 0)) == code
  3166. && (INTEGRAL_MODE_P (mode)
  3167. || (FLOAT_MODE_P (mode)
  3168. && flag_unsafe_math_optimizations)))
  3169. {
  3170. rtx p = XEXP (XEXP (src_op0, 0), 0);
  3171. rtx q = XEXP (XEXP (src_op0, 0), 1);
  3172. rtx r = XEXP (src_op0, 1);
  3173. rtx s = src_op1;
  3174. /* Split both "((X op Y) op X) op Y" and
  3175. "((X op Y) op Y) op X" as "T op T" where T is
  3176. "X op Y". */
  3177. if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
  3178. || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
  3179. {
  3180. newi2pat = gen_rtx_SET (VOIDmode, newdest,
  3181. XEXP (src_op0, 0));
  3182. SUBST (XEXP (setsrc, 0), newdest);
  3183. SUBST (XEXP (setsrc, 1), newdest);
  3184. subst_done = true;
  3185. }
  3186. /* Split "((X op X) op Y) op Y)" as "T op T" where
  3187. T is "X op Y". */
  3188. else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
  3189. {
  3190. rtx tmp = simplify_gen_binary (code, mode, p, r);
  3191. newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
  3192. SUBST (XEXP (setsrc, 0), newdest);
  3193. SUBST (XEXP (setsrc, 1), newdest);
  3194. subst_done = true;
  3195. }
  3196. }
  3197. }
  3198. if (!subst_done)
  3199. {
  3200. newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
  3201. SUBST (*split, newdest);
  3202. }
  3203. i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
  3204. /* recog_for_combine might have added CLOBBERs to newi2pat.
  3205. Make sure NEWPAT does not depend on the clobbered regs. */
  3206. if (GET_CODE (newi2pat) == PARALLEL)
  3207. for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
  3208. if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
  3209. {
  3210. rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
  3211. if (reg_overlap_mentioned_p (reg, newpat))
  3212. {
  3213. undo_all ();
  3214. return 0;
  3215. }
  3216. }
  3217. /* If the split point was a MULT and we didn't have one before,
  3218. don't use one now. */
  3219. if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
  3220. insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
  3221. }
  3222. }
  3223. /* Check for a case where we loaded from memory in a narrow mode and
  3224. then sign extended it, but we need both registers. In that case,
  3225. we have a PARALLEL with both loads from the same memory location.
  3226. We can split this into a load from memory followed by a register-register
  3227. copy. This saves at least one insn, more if register allocation can
  3228. eliminate the copy.
  3229. We cannot do this if the destination of the first assignment is a
  3230. condition code register or cc0. We eliminate this case by making sure
  3231. the SET_DEST and SET_SRC have the same mode.
  3232. We cannot do this if the destination of the second assignment is
  3233. a register that we have already assumed is zero-extended. Similarly
  3234. for a SUBREG of such a register. */
  3235. else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
  3236. && GET_CODE (newpat) == PARALLEL
  3237. && XVECLEN (newpat, 0) == 2
  3238. && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
  3239. && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
  3240. && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
  3241. == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
  3242. && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
  3243. && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
  3244. XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
  3245. && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
  3246. DF_INSN_LUID (i2))
  3247. && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
  3248. && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
  3249. && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
  3250. (REG_P (temp_expr)
  3251. && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
  3252. && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
  3253. && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
  3254. && (reg_stat[REGNO (temp_expr)].nonzero_bits
  3255. != GET_MODE_MASK (word_mode))))
  3256. && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
  3257. && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
  3258. (REG_P (temp_expr)
  3259. && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
  3260. && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
  3261. && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
  3262. && (reg_stat[REGNO (temp_expr)].nonzero_bits
  3263. != GET_MODE_MASK (word_mode)))))
  3264. && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
  3265. SET_SRC (XVECEXP (newpat, 0, 1)))
  3266. && ! find_reg_note (i3, REG_UNUSED,
  3267. SET_DEST (XVECEXP (newpat, 0, 0))))
  3268. {
  3269. rtx ni2dest;
  3270. newi2pat = XVECEXP (newpat, 0, 0);
  3271. ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
  3272. newpat = XVECEXP (newpat, 0, 1);
  3273. SUBST (SET_SRC (newpat),
  3274. gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
  3275. i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
  3276. if (i2_code_number >= 0)
  3277. insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
  3278. if (insn_code_number >= 0)
  3279. swap_i2i3 = 1;
  3280. }
  3281. /* Similarly, check for a case where we have a PARALLEL of two independent
  3282. SETs but we started with three insns. In this case, we can do the sets
  3283. as two separate insns. This case occurs when some SET allows two
  3284. other insns to combine, but the destination of that SET is still live.
  3285. Also do this if we started with two insns and (at least) one of the
  3286. resulting sets is a noop; this noop will be deleted later. */
  3287. else if (insn_code_number < 0 && asm_noperands (newpat) < 0
  3288. && GET_CODE (newpat) == PARALLEL
  3289. && XVECLEN (newpat, 0) == 2
  3290. && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
  3291. && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
  3292. && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
  3293. || set_noop_p (XVECEXP (newpat, 0, 1)))
  3294. && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
  3295. && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
  3296. && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
  3297. && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
  3298. && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
  3299. XVECEXP (newpat, 0, 0))
  3300. && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
  3301. XVECEXP (newpat, 0, 1))
  3302. && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
  3303. && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
  3304. {
  3305. rtx set0 = XVECEXP (newpat, 0, 0);
  3306. rtx set1 = XVECEXP (newpat, 0, 1);
  3307. /* Normally, it doesn't matter which of the two is done first,
  3308. but the one that references cc0 can't be the second, and
  3309. one which uses any regs/memory set in between i2 and i3 can't
  3310. be first. The PARALLEL might also have been pre-existing in i3,
  3311. so we need to make sure that we won't wrongly hoist a SET to i2
  3312. that would conflict with a death note present in there. */
  3313. if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
  3314. && !(REG_P (SET_DEST (set1))
  3315. && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
  3316. && !(GET_CODE (SET_DEST (set1)) == SUBREG
  3317. && find_reg_note (i2, REG_DEAD,
  3318. SUBREG_REG (SET_DEST (set1))))
  3319. #ifdef HAVE_cc0
  3320. && !reg_referenced_p (cc0_rtx, set0)
  3321. #endif
  3322. /* If I3 is a jump, ensure that set0 is a jump so that
  3323. we do not create invalid RTL. */
  3324. && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
  3325. )
  3326. {
  3327. newi2pat = set1;
  3328. newpat = set0;
  3329. }
  3330. else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
  3331. && !(REG_P (SET_DEST (set0))
  3332. && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
  3333. && !(GET_CODE (SET_DEST (set0)) == SUBREG
  3334. && find_reg_note (i2, REG_DEAD,
  3335. SUBREG_REG (SET_DEST (set0))))
  3336. #ifdef HAVE_cc0
  3337. && !reg_referenced_p (cc0_rtx, set1)
  3338. #endif
  3339. /* If I3 is a jump, ensure that set1 is a jump so that
  3340. we do not create invalid RTL. */
  3341. && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
  3342. )
  3343. {
  3344. newi2pat = set0;
  3345. newpat = set1;
  3346. }
  3347. else
  3348. {
  3349. undo_all ();
  3350. return 0;
  3351. }
  3352. i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
  3353. if (i2_code_number >= 0)
  3354. {
  3355. /* recog_for_combine might have added CLOBBERs to newi2pat.
  3356. Make sure NEWPAT does not depend on the clobbered regs. */
  3357. if (GET_CODE (newi2pat) == PARALLEL)
  3358. {
  3359. for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
  3360. if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
  3361. {
  3362. rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
  3363. if (reg_overlap_mentioned_p (reg, newpat))
  3364. {
  3365. undo_all ();
  3366. return 0;
  3367. }
  3368. }
  3369. }
  3370. insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
  3371. }
  3372. }
  3373. /* If it still isn't recognized, fail and change things back the way they
  3374. were. */
  3375. if ((insn_code_number < 0
  3376. /* Is the result a reasonable ASM_OPERANDS? */
  3377. && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
  3378. {
  3379. undo_all ();
  3380. return 0;
  3381. }
  3382. /* If we had to change another insn, make sure it is valid also. */
  3383. if (undobuf.other_insn)
  3384. {
  3385. CLEAR_HARD_REG_SET (newpat_used_regs);
  3386. other_pat = PATTERN (undobuf.other_insn);
  3387. other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
  3388. &new_other_notes);
  3389. if (other_code_number < 0 && ! check_asm_operands (other_pat))
  3390. {
  3391. undo_all ();
  3392. return 0;
  3393. }
  3394. }
  3395. #ifdef HAVE_cc0
  3396. /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
  3397. they are adjacent to each other or not. */
  3398. {
  3399. rtx_insn *p = prev_nonnote_insn (i3);
  3400. if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
  3401. && sets_cc0_p (newi2pat))
  3402. {
  3403. undo_all ();
  3404. return 0;
  3405. }
  3406. }
  3407. #endif
  3408. /* Only allow this combination if insn_rtx_costs reports that the
  3409. replacement instructions are cheaper than the originals. */
  3410. if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
  3411. {
  3412. undo_all ();
  3413. return 0;
  3414. }
  3415. if (MAY_HAVE_DEBUG_INSNS)
  3416. {
  3417. struct undo *undo;
  3418. for (undo = undobuf.undos; undo; undo = undo->next)
  3419. if (undo->kind == UNDO_MODE)
  3420. {
  3421. rtx reg = *undo->where.r;
  3422. machine_mode new_mode = GET_MODE (reg);
  3423. machine_mode old_mode = undo->old_contents.m;
  3424. /* Temporarily revert mode back. */
  3425. adjust_reg_mode (reg, old_mode);
  3426. if (reg == i2dest && i2scratch)
  3427. {
  3428. /* If we used i2dest as a scratch register with a
  3429. different mode, substitute it for the original
  3430. i2src while its original mode is temporarily
  3431. restored, and then clear i2scratch so that we don't
  3432. do it again later. */
  3433. propagate_for_debug (i2, last_combined_insn, reg, i2src,
  3434. this_basic_block);
  3435. i2scratch = false;
  3436. /* Put back the new mode. */
  3437. adjust_reg_mode (reg, new_mode);
  3438. }
  3439. else
  3440. {
  3441. rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
  3442. rtx_insn *first, *last;
  3443. if (reg == i2dest)
  3444. {
  3445. first = i2;
  3446. last = last_combined_insn;
  3447. }
  3448. else
  3449. {
  3450. first = i3;
  3451. last = undobuf.other_insn;
  3452. gcc_assert (last);
  3453. if (DF_INSN_LUID (last)
  3454. < DF_INSN_LUID (last_combined_insn))
  3455. last = last_combined_insn;
  3456. }
  3457. /* We're dealing with a reg that changed mode but not
  3458. meaning, so we want to turn it into a subreg for
  3459. the new mode. However, because of REG sharing and
  3460. because its mode had already changed, we have to do
  3461. it in two steps. First, replace any debug uses of
  3462. reg, with its original mode temporarily restored,
  3463. with this copy we have created; then, replace the
  3464. copy with the SUBREG of the original shared reg,
  3465. once again changed to the new mode. */
  3466. propagate_for_debug (first, last, reg, tempreg,
  3467. this_basic_block);
  3468. adjust_reg_mode (reg, new_mode);
  3469. propagate_for_debug (first, last, tempreg,
  3470. lowpart_subreg (old_mode, reg, new_mode),
  3471. this_basic_block);
  3472. }
  3473. }
  3474. }
  3475. /* If we will be able to accept this, we have made a
  3476. change to the destination of I3. This requires us to
  3477. do a few adjustments. */
  3478. if (changed_i3_dest)
  3479. {
  3480. PATTERN (i3) = newpat;
  3481. adjust_for_new_dest (i3);
  3482. }
  3483. /* We now know that we can do this combination. Merge the insns and
  3484. update the status of registers and LOG_LINKS. */
  3485. if (undobuf.other_insn)
  3486. {
  3487. rtx note, next;
  3488. PATTERN (undobuf.other_insn) = other_pat;
  3489. /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
  3490. ensure that they are still valid. Then add any non-duplicate
  3491. notes added by recog_for_combine. */
  3492. for (note = REG_NOTES (undobuf.other_insn); note; note = next)
  3493. {
  3494. next = XEXP (note, 1);
  3495. if ((REG_NOTE_KIND (note) == REG_DEAD
  3496. && !reg_referenced_p (XEXP (note, 0),
  3497. PATTERN (undobuf.other_insn)))
  3498. ||(REG_NOTE_KIND (note) == REG_UNUSED
  3499. && !reg_set_p (XEXP (note, 0),
  3500. PATTERN (undobuf.other_insn))))
  3501. remove_note (undobuf.other_insn, note);
  3502. }
  3503. distribute_notes (new_other_notes, undobuf.other_insn,
  3504. undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
  3505. NULL_RTX);
  3506. }
  3507. if (swap_i2i3)
  3508. {
  3509. rtx_insn *insn;
  3510. struct insn_link *link;
  3511. rtx ni2dest;
  3512. /* I3 now uses what used to be its destination and which is now
  3513. I2's destination. This requires us to do a few adjustments. */
  3514. PATTERN (i3) = newpat;
  3515. adjust_for_new_dest (i3);
  3516. /* We need a LOG_LINK from I3 to I2. But we used to have one,
  3517. so we still will.
  3518. However, some later insn might be using I2's dest and have
  3519. a LOG_LINK pointing at I3. We must remove this link.
  3520. The simplest way to remove the link is to point it at I1,
  3521. which we know will be a NOTE. */
  3522. /* newi2pat is usually a SET here; however, recog_for_combine might
  3523. have added some clobbers. */
  3524. if (GET_CODE (newi2pat) == PARALLEL)
  3525. ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
  3526. else
  3527. ni2dest = SET_DEST (newi2pat);
  3528. for (insn = NEXT_INSN (i3);
  3529. insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
  3530. || insn != BB_HEAD (this_basic_block->next_bb));
  3531. insn = NEXT_INSN (insn))
  3532. {
  3533. if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
  3534. {
  3535. FOR_EACH_LOG_LINK (link, insn)
  3536. if (link->insn == i3)
  3537. link->insn = i1;
  3538. break;
  3539. }
  3540. }
  3541. }
  3542. {
  3543. rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
  3544. struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
  3545. rtx midnotes = 0;
  3546. int from_luid;
  3547. /* Compute which registers we expect to eliminate. newi2pat may be setting
  3548. either i3dest or i2dest, so we must check it. */
  3549. rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
  3550. || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
  3551. || !i2dest_killed
  3552. ? 0 : i2dest);
  3553. /* For i1, we need to compute both local elimination and global
  3554. elimination information with respect to newi2pat because i1dest
  3555. may be the same as i3dest, in which case newi2pat may be setting
  3556. i1dest. Global information is used when distributing REG_DEAD
  3557. note for i2 and i3, in which case it does matter if newi2pat sets
  3558. i1dest or not.
  3559. Local information is used when distributing REG_DEAD note for i1,
  3560. in which case it doesn't matter if newi2pat sets i1dest or not.
  3561. See PR62151, if we have four insns combination:
  3562. i0: r0 <- i0src
  3563. i1: r1 <- i1src (using r0)
  3564. REG_DEAD (r0)
  3565. i2: r0 <- i2src (using r1)
  3566. i3: r3 <- i3src (using r0)
  3567. ix: using r0
  3568. From i1's point of view, r0 is eliminated, no matter if it is set
  3569. by newi2pat or not. In other words, REG_DEAD info for r0 in i1
  3570. should be discarded.
  3571. Note local information only affects cases in forms like "I1->I2->I3",
  3572. "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
  3573. "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
  3574. i0dest anyway. */
  3575. rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
  3576. || !i1dest_killed
  3577. ? 0 : i1dest);
  3578. rtx elim_i1 = (local_elim_i1 == 0
  3579. || (newi2pat && reg_set_p (i1dest, newi2pat))
  3580. ? 0 : i1dest);
  3581. /* Same case as i1. */
  3582. rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
  3583. ? 0 : i0dest);
  3584. rtx elim_i0 = (local_elim_i0 == 0
  3585. || (newi2pat && reg_set_p (i0dest, newi2pat))
  3586. ? 0 : i0dest);
  3587. /* Get the old REG_NOTES and LOG_LINKS from all our insns and
  3588. clear them. */
  3589. i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
  3590. i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
  3591. if (i1)
  3592. i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
  3593. if (i0)
  3594. i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
  3595. /* Ensure that we do not have something that should not be shared but
  3596. occurs multiple times in the new insns. Check this by first
  3597. resetting all the `used' flags and then copying anything is shared. */
  3598. reset_used_flags (i3notes);
  3599. reset_used_flags (i2notes);
  3600. reset_used_flags (i1notes);
  3601. reset_used_flags (i0notes);
  3602. reset_used_flags (newpat);
  3603. reset_used_flags (newi2pat);
  3604. if (undobuf.other_insn)
  3605. reset_used_flags (PATTERN (undobuf.other_insn));
  3606. i3notes = copy_rtx_if_shared (i3notes);
  3607. i2notes = copy_rtx_if_shared (i2notes);
  3608. i1notes = copy_rtx_if_shared (i1notes);
  3609. i0notes = copy_rtx_if_shared (i0notes);
  3610. newpat = copy_rtx_if_shared (newpat);
  3611. newi2pat = copy_rtx_if_shared (newi2pat);
  3612. if (undobuf.other_insn)
  3613. reset_used_flags (PATTERN (undobuf.other_insn));
  3614. INSN_CODE (i3) = insn_code_number;
  3615. PATTERN (i3) = newpat;
  3616. if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
  3617. {
  3618. rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
  3619. reset_used_flags (call_usage);
  3620. call_usage = copy_rtx (call_usage);
  3621. if (substed_i2)
  3622. {
  3623. /* I2SRC must still be meaningful at this point. Some splitting
  3624. operations can invalidate I2SRC, but those operations do not
  3625. apply to calls. */
  3626. gcc_assert (i2src);
  3627. replace_rtx (call_usage, i2dest, i2src);
  3628. }
  3629. if (substed_i1)
  3630. replace_rtx (call_usage, i1dest, i1src);
  3631. if (substed_i0)
  3632. replace_rtx (call_usage, i0dest, i0src);
  3633. CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
  3634. }
  3635. if (undobuf.other_insn)
  3636. INSN_CODE (undobuf.other_insn) = other_code_number;
  3637. /* We had one special case above where I2 had more than one set and
  3638. we replaced a destination of one of those sets with the destination
  3639. of I3. In that case, we have to update LOG_LINKS of insns later
  3640. in this basic block. Note that this (expensive) case is rare.
  3641. Also, in this case, we must pretend that all REG_NOTEs for I2
  3642. actually came from I3, so that REG_UNUSED notes from I2 will be
  3643. properly handled. */
  3644. if (i3_subst_into_i2)
  3645. {
  3646. for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
  3647. if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
  3648. || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
  3649. && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
  3650. && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
  3651. && ! find_reg_note (i2, REG_UNUSED,
  3652. SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
  3653. for (temp_insn = NEXT_INSN (i2);
  3654. temp_insn
  3655. && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
  3656. || BB_HEAD (this_basic_block) != temp_insn);
  3657. temp_insn = NEXT_INSN (temp_insn))
  3658. if (temp_insn != i3 && INSN_P (temp_insn))
  3659. FOR_EACH_LOG_LINK (link, temp_insn)
  3660. if (link->insn == i2)
  3661. link->insn = i3;
  3662. if (i3notes)
  3663. {
  3664. rtx link = i3notes;
  3665. while (XEXP (link, 1))
  3666. link = XEXP (link, 1);
  3667. XEXP (link, 1) = i2notes;
  3668. }
  3669. else
  3670. i3notes = i2notes;
  3671. i2notes = 0;
  3672. }
  3673. LOG_LINKS (i3) = NULL;
  3674. REG_NOTES (i3) = 0;
  3675. LOG_LINKS (i2) = NULL;
  3676. REG_NOTES (i2) = 0;
  3677. if (newi2pat)
  3678. {
  3679. if (MAY_HAVE_DEBUG_INSNS && i2scratch)
  3680. propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
  3681. this_basic_block);
  3682. INSN_CODE (i2) = i2_code_number;
  3683. PATTERN (i2) = newi2pat;
  3684. }
  3685. else
  3686. {
  3687. if (MAY_HAVE_DEBUG_INSNS && i2src)
  3688. propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
  3689. this_basic_block);
  3690. SET_INSN_DELETED (i2);
  3691. }
  3692. if (i1)
  3693. {
  3694. LOG_LINKS (i1) = NULL;
  3695. REG_NOTES (i1) = 0;
  3696. if (MAY_HAVE_DEBUG_INSNS)
  3697. propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
  3698. this_basic_block);
  3699. SET_INSN_DELETED (i1);
  3700. }
  3701. if (i0)
  3702. {
  3703. LOG_LINKS (i0) = NULL;
  3704. REG_NOTES (i0) = 0;
  3705. if (MAY_HAVE_DEBUG_INSNS)
  3706. propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
  3707. this_basic_block);
  3708. SET_INSN_DELETED (i0);
  3709. }
  3710. /* Get death notes for everything that is now used in either I3 or
  3711. I2 and used to die in a previous insn. If we built two new
  3712. patterns, move from I1 to I2 then I2 to I3 so that we get the
  3713. proper movement on registers that I2 modifies. */
  3714. if (i0)
  3715. from_luid = DF_INSN_LUID (i0);
  3716. else if (i1)
  3717. from_luid = DF_INSN_LUID (i1);
  3718. else
  3719. from_luid = DF_INSN_LUID (i2);
  3720. if (newi2pat)
  3721. move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
  3722. move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
  3723. /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
  3724. if (i3notes)
  3725. distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
  3726. elim_i2, elim_i1, elim_i0);
  3727. if (i2notes)
  3728. distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
  3729. elim_i2, elim_i1, elim_i0);
  3730. if (i1notes)
  3731. distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
  3732. elim_i2, local_elim_i1, local_elim_i0);
  3733. if (i0notes)
  3734. distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
  3735. elim_i2, elim_i1, local_elim_i0);
  3736. if (midnotes)
  3737. distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
  3738. elim_i2, elim_i1, elim_i0);
  3739. /* Distribute any notes added to I2 or I3 by recog_for_combine. We
  3740. know these are REG_UNUSED and want them to go to the desired insn,
  3741. so we always pass it as i3. */
  3742. if (newi2pat && new_i2_notes)
  3743. distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
  3744. NULL_RTX);
  3745. if (new_i3_notes)
  3746. distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
  3747. NULL_RTX);
  3748. /* If I3DEST was used in I3SRC, it really died in I3. We may need to
  3749. put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
  3750. I3DEST, the death must be somewhere before I2, not I3. If we passed I3
  3751. in that case, it might delete I2. Similarly for I2 and I1.
  3752. Show an additional death due to the REG_DEAD note we make here. If
  3753. we discard it in distribute_notes, we will decrement it again. */
  3754. if (i3dest_killed)
  3755. {
  3756. rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
  3757. if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
  3758. distribute_notes (new_note, NULL, i2, NULL, elim_i2,
  3759. elim_i1, elim_i0);
  3760. else
  3761. distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
  3762. elim_i2, elim_i1, elim_i0);
  3763. }
  3764. if (i2dest_in_i2src)
  3765. {
  3766. rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
  3767. if (newi2pat && reg_set_p (i2dest, newi2pat))
  3768. distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
  3769. NULL_RTX, NULL_RTX);
  3770. else
  3771. distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
  3772. NULL_RTX, NULL_RTX, NULL_RTX);
  3773. }
  3774. if (i1dest_in_i1src)
  3775. {
  3776. rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
  3777. if (newi2pat && reg_set_p (i1dest, newi2pat))
  3778. distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
  3779. NULL_RTX, NULL_RTX);
  3780. else
  3781. distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
  3782. NULL_RTX, NULL_RTX, NULL_RTX);
  3783. }
  3784. if (i0dest_in_i0src)
  3785. {
  3786. rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
  3787. if (newi2pat && reg_set_p (i0dest, newi2pat))
  3788. distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
  3789. NULL_RTX, NULL_RTX);
  3790. else
  3791. distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
  3792. NULL_RTX, NULL_RTX, NULL_RTX);
  3793. }
  3794. distribute_links (i3links);
  3795. distribute_links (i2links);
  3796. distribute_links (i1links);
  3797. distribute_links (i0links);
  3798. if (REG_P (i2dest))
  3799. {
  3800. struct insn_link *link;
  3801. rtx_insn *i2_insn = 0;
  3802. rtx i2_val = 0, set;
  3803. /* The insn that used to set this register doesn't exist, and
  3804. this life of the register may not exist either. See if one of
  3805. I3's links points to an insn that sets I2DEST. If it does,
  3806. that is now the last known value for I2DEST. If we don't update
  3807. this and I2 set the register to a value that depended on its old
  3808. contents, we will get confused. If this insn is used, thing
  3809. will be set correctly in combine_instructions. */
  3810. FOR_EACH_LOG_LINK (link, i3)
  3811. if ((set = single_set (link->insn)) != 0
  3812. && rtx_equal_p (i2dest, SET_DEST (set)))
  3813. i2_insn = link->insn, i2_val = SET_SRC (set);
  3814. record_value_for_reg (i2dest, i2_insn, i2_val);
  3815. /* If the reg formerly set in I2 died only once and that was in I3,
  3816. zero its use count so it won't make `reload' do any work. */
  3817. if (! added_sets_2
  3818. && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
  3819. && ! i2dest_in_i2src
  3820. && REGNO (i2dest) < reg_n_sets_max)
  3821. INC_REG_N_SETS (REGNO (i2dest), -1);
  3822. }
  3823. if (i1 && REG_P (i1dest))
  3824. {
  3825. struct insn_link *link;
  3826. rtx_insn *i1_insn = 0;
  3827. rtx i1_val = 0, set;
  3828. FOR_EACH_LOG_LINK (link, i3)
  3829. if ((set = single_set (link->insn)) != 0
  3830. && rtx_equal_p (i1dest, SET_DEST (set)))
  3831. i1_insn = link->insn, i1_val = SET_SRC (set);
  3832. record_value_for_reg (i1dest, i1_insn, i1_val);
  3833. if (! added_sets_1
  3834. && ! i1dest_in_i1src
  3835. && REGNO (i1dest) < reg_n_sets_max)
  3836. INC_REG_N_SETS (REGNO (i1dest), -1);
  3837. }
  3838. if (i0 && REG_P (i0dest))
  3839. {
  3840. struct insn_link *link;
  3841. rtx_insn *i0_insn = 0;
  3842. rtx i0_val = 0, set;
  3843. FOR_EACH_LOG_LINK (link, i3)
  3844. if ((set = single_set (link->insn)) != 0
  3845. && rtx_equal_p (i0dest, SET_DEST (set)))
  3846. i0_insn = link->insn, i0_val = SET_SRC (set);
  3847. record_value_for_reg (i0dest, i0_insn, i0_val);
  3848. if (! added_sets_0
  3849. && ! i0dest_in_i0src
  3850. && REGNO (i0dest) < reg_n_sets_max)
  3851. INC_REG_N_SETS (REGNO (i0dest), -1);
  3852. }
  3853. /* Update reg_stat[].nonzero_bits et al for any changes that may have
  3854. been made to this insn. The order is important, because newi2pat
  3855. can affect nonzero_bits of newpat. */
  3856. if (newi2pat)
  3857. note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
  3858. note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
  3859. }
  3860. if (undobuf.other_insn != NULL_RTX)
  3861. {
  3862. if (dump_file)
  3863. {
  3864. fprintf (dump_file, "modifying other_insn ");
  3865. dump_insn_slim (dump_file, undobuf.other_insn);
  3866. }
  3867. df_insn_rescan (undobuf.other_insn);
  3868. }
  3869. if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
  3870. {
  3871. if (dump_file)
  3872. {
  3873. fprintf (dump_file, "modifying insn i0 ");
  3874. dump_insn_slim (dump_file, i0);
  3875. }
  3876. df_insn_rescan (i0);
  3877. }
  3878. if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
  3879. {
  3880. if (dump_file)
  3881. {
  3882. fprintf (dump_file, "modifying insn i1 ");
  3883. dump_insn_slim (dump_file, i1);
  3884. }
  3885. df_insn_rescan (i1);
  3886. }
  3887. if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
  3888. {
  3889. if (dump_file)
  3890. {
  3891. fprintf (dump_file, "modifying insn i2 ");
  3892. dump_insn_slim (dump_file, i2);
  3893. }
  3894. df_insn_rescan (i2);
  3895. }
  3896. if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
  3897. {
  3898. if (dump_file)
  3899. {
  3900. fprintf (dump_file, "modifying insn i3 ");
  3901. dump_insn_slim (dump_file, i3);
  3902. }
  3903. df_insn_rescan (i3);
  3904. }
  3905. /* Set new_direct_jump_p if a new return or simple jump instruction
  3906. has been created. Adjust the CFG accordingly. */
  3907. if (returnjump_p (i3) || any_uncondjump_p (i3))
  3908. {
  3909. *new_direct_jump_p = 1;
  3910. mark_jump_label (PATTERN (i3), i3, 0);
  3911. update_cfg_for_uncondjump (i3);
  3912. }
  3913. if (undobuf.other_insn != NULL_RTX
  3914. && (returnjump_p (undobuf.other_insn)
  3915. || any_uncondjump_p (undobuf.other_insn)))
  3916. {
  3917. *new_direct_jump_p = 1;
  3918. update_cfg_for_uncondjump (undobuf.other_insn);
  3919. }
  3920. /* A noop might also need cleaning up of CFG, if it comes from the
  3921. simplification of a jump. */
  3922. if (JUMP_P (i3)
  3923. && GET_CODE (newpat) == SET
  3924. && SET_SRC (newpat) == pc_rtx
  3925. && SET_DEST (newpat) == pc_rtx)
  3926. {
  3927. *new_direct_jump_p = 1;
  3928. update_cfg_for_uncondjump (i3);
  3929. }
  3930. if (undobuf.other_insn != NULL_RTX
  3931. && JUMP_P (undobuf.other_insn)
  3932. && GET_CODE (PATTERN (undobuf.other_insn)) == SET
  3933. && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
  3934. && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
  3935. {
  3936. *new_direct_jump_p = 1;
  3937. update_cfg_for_uncondjump (undobuf.other_insn);
  3938. }
  3939. combine_successes++;
  3940. undo_commit ();
  3941. if (added_links_insn
  3942. && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
  3943. && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
  3944. return added_links_insn;
  3945. else
  3946. return newi2pat ? i2 : i3;
  3947. }
  3948. /* Undo all the modifications recorded in undobuf. */
  3949. static void
  3950. undo_all (void)
  3951. {
  3952. struct undo *undo, *next;
  3953. for (undo = undobuf.undos; undo; undo = next)
  3954. {
  3955. next = undo->next;
  3956. switch (undo->kind)
  3957. {
  3958. case UNDO_RTX:
  3959. *undo->where.r = undo->old_contents.r;
  3960. break;
  3961. case UNDO_INT:
  3962. *undo->where.i = undo->old_contents.i;
  3963. break;
  3964. case UNDO_MODE:
  3965. adjust_reg_mode (*undo->where.r, undo->old_contents.m);
  3966. break;
  3967. case UNDO_LINKS:
  3968. *undo->where.l = undo->old_contents.l;
  3969. break;
  3970. default:
  3971. gcc_unreachable ();
  3972. }
  3973. undo->next = undobuf.frees;
  3974. undobuf.frees = undo;
  3975. }
  3976. undobuf.undos = 0;
  3977. }
  3978. /* We've committed to accepting the changes we made. Move all
  3979. of the undos to the free list. */
  3980. static void
  3981. undo_commit (void)
  3982. {
  3983. struct undo *undo, *next;
  3984. for (undo = undobuf.undos; undo; undo = next)
  3985. {
  3986. next = undo->next;
  3987. undo->next = undobuf.frees;
  3988. undobuf.frees = undo;
  3989. }
  3990. undobuf.undos = 0;
  3991. }
  3992. /* Find the innermost point within the rtx at LOC, possibly LOC itself,
  3993. where we have an arithmetic expression and return that point. LOC will
  3994. be inside INSN.
  3995. try_combine will call this function to see if an insn can be split into
  3996. two insns. */
  3997. static rtx *
  3998. find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
  3999. {
  4000. rtx x = *loc;
  4001. enum rtx_code code = GET_CODE (x);
  4002. rtx *split;
  4003. unsigned HOST_WIDE_INT len = 0;
  4004. HOST_WIDE_INT pos = 0;
  4005. int unsignedp = 0;
  4006. rtx inner = NULL_RTX;
  4007. /* First special-case some codes. */
  4008. switch (code)
  4009. {
  4010. case SUBREG:
  4011. #ifdef INSN_SCHEDULING
  4012. /* If we are making a paradoxical SUBREG invalid, it becomes a split
  4013. point. */
  4014. if (MEM_P (SUBREG_REG (x)))
  4015. return loc;
  4016. #endif
  4017. return find_split_point (&SUBREG_REG (x), insn, false);
  4018. case MEM:
  4019. #ifdef HAVE_lo_sum
  4020. /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
  4021. using LO_SUM and HIGH. */
  4022. if (GET_CODE (XEXP (x, 0)) == CONST
  4023. || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
  4024. {
  4025. machine_mode address_mode = get_address_mode (x);
  4026. SUBST (XEXP (x, 0),
  4027. gen_rtx_LO_SUM (address_mode,
  4028. gen_rtx_HIGH (address_mode, XEXP (x, 0)),
  4029. XEXP (x, 0)));
  4030. return &XEXP (XEXP (x, 0), 0);
  4031. }
  4032. #endif
  4033. /* If we have a PLUS whose second operand is a constant and the
  4034. address is not valid, perhaps will can split it up using
  4035. the machine-specific way to split large constants. We use
  4036. the first pseudo-reg (one of the virtual regs) as a placeholder;
  4037. it will not remain in the result. */
  4038. if (GET_CODE (XEXP (x, 0)) == PLUS
  4039. && CONST_INT_P (XEXP (XEXP (x, 0), 1))
  4040. && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
  4041. MEM_ADDR_SPACE (x)))
  4042. {
  4043. rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
  4044. rtx_insn *seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
  4045. XEXP (x, 0)),
  4046. subst_insn);
  4047. /* This should have produced two insns, each of which sets our
  4048. placeholder. If the source of the second is a valid address,
  4049. we can make put both sources together and make a split point
  4050. in the middle. */
  4051. if (seq
  4052. && NEXT_INSN (seq) != NULL_RTX
  4053. && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
  4054. && NONJUMP_INSN_P (seq)
  4055. && GET_CODE (PATTERN (seq)) == SET
  4056. && SET_DEST (PATTERN (seq)) == reg
  4057. && ! reg_mentioned_p (reg,
  4058. SET_SRC (PATTERN (seq)))
  4059. && NONJUMP_INSN_P (NEXT_INSN (seq))
  4060. && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
  4061. && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
  4062. && memory_address_addr_space_p
  4063. (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
  4064. MEM_ADDR_SPACE (x)))
  4065. {
  4066. rtx src1 = SET_SRC (PATTERN (seq));
  4067. rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
  4068. /* Replace the placeholder in SRC2 with SRC1. If we can
  4069. find where in SRC2 it was placed, that can become our
  4070. split point and we can replace this address with SRC2.
  4071. Just try two obvious places. */
  4072. src2 = replace_rtx (src2, reg, src1);
  4073. split = 0;
  4074. if (XEXP (src2, 0) == src1)
  4075. split = &XEXP (src2, 0);
  4076. else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
  4077. && XEXP (XEXP (src2, 0), 0) == src1)
  4078. split = &XEXP (XEXP (src2, 0), 0);
  4079. if (split)
  4080. {
  4081. SUBST (XEXP (x, 0), src2);
  4082. return split;
  4083. }
  4084. }
  4085. /* If that didn't work, perhaps the first operand is complex and
  4086. needs to be computed separately, so make a split point there.
  4087. This will occur on machines that just support REG + CONST
  4088. and have a constant moved through some previous computation. */
  4089. else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
  4090. && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
  4091. && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
  4092. return &XEXP (XEXP (x, 0), 0);
  4093. }
  4094. /* If we have a PLUS whose first operand is complex, try computing it
  4095. separately by making a split there. */
  4096. if (GET_CODE (XEXP (x, 0)) == PLUS
  4097. && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
  4098. MEM_ADDR_SPACE (x))
  4099. && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
  4100. && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
  4101. && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
  4102. return &XEXP (XEXP (x, 0), 0);
  4103. break;
  4104. case SET:
  4105. #ifdef HAVE_cc0
  4106. /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
  4107. ZERO_EXTRACT, the most likely reason why this doesn't match is that
  4108. we need to put the operand into a register. So split at that
  4109. point. */
  4110. if (SET_DEST (x) == cc0_rtx
  4111. && GET_CODE (SET_SRC (x)) != COMPARE
  4112. && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
  4113. && !OBJECT_P (SET_SRC (x))
  4114. && ! (GET_CODE (SET_SRC (x)) == SUBREG
  4115. && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
  4116. return &SET_SRC (x);
  4117. #endif
  4118. /* See if we can split SET_SRC as it stands. */
  4119. split = find_split_point (&SET_SRC (x), insn, true);
  4120. if (split && split != &SET_SRC (x))
  4121. return split;
  4122. /* See if we can split SET_DEST as it stands. */
  4123. split = find_split_point (&SET_DEST (x), insn, false);
  4124. if (split && split != &SET_DEST (x))
  4125. return split;
  4126. /* See if this is a bitfield assignment with everything constant. If
  4127. so, this is an IOR of an AND, so split it into that. */
  4128. if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
  4129. && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
  4130. && CONST_INT_P (XEXP (SET_DEST (x), 1))
  4131. && CONST_INT_P (XEXP (SET_DEST (x), 2))
  4132. && CONST_INT_P (SET_SRC (x))
  4133. && ((INTVAL (XEXP (SET_DEST (x), 1))
  4134. + INTVAL (XEXP (SET_DEST (x), 2)))
  4135. <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
  4136. && ! side_effects_p (XEXP (SET_DEST (x), 0)))
  4137. {
  4138. HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
  4139. unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
  4140. unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
  4141. rtx dest = XEXP (SET_DEST (x), 0);
  4142. machine_mode mode = GET_MODE (dest);
  4143. unsigned HOST_WIDE_INT mask
  4144. = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
  4145. rtx or_mask;
  4146. if (BITS_BIG_ENDIAN)
  4147. pos = GET_MODE_PRECISION (mode) - len - pos;
  4148. or_mask = gen_int_mode (src << pos, mode);
  4149. if (src == mask)
  4150. SUBST (SET_SRC (x),
  4151. simplify_gen_binary (IOR, mode, dest, or_mask));
  4152. else
  4153. {
  4154. rtx negmask = gen_int_mode (~(mask << pos), mode);
  4155. SUBST (SET_SRC (x),
  4156. simplify_gen_binary (IOR, mode,
  4157. simplify_gen_binary (AND, mode,
  4158. dest, negmask),
  4159. or_mask));
  4160. }
  4161. SUBST (SET_DEST (x), dest);
  4162. split = find_split_point (&SET_SRC (x), insn, true);
  4163. if (split && split != &SET_SRC (x))
  4164. return split;
  4165. }
  4166. /* Otherwise, see if this is an operation that we can split into two.
  4167. If so, try to split that. */
  4168. code = GET_CODE (SET_SRC (x));
  4169. switch (code)
  4170. {
  4171. case AND:
  4172. /* If we are AND'ing with a large constant that is only a single
  4173. bit and the result is only being used in a context where we
  4174. need to know if it is zero or nonzero, replace it with a bit
  4175. extraction. This will avoid the large constant, which might
  4176. have taken more than one insn to make. If the constant were
  4177. not a valid argument to the AND but took only one insn to make,
  4178. this is no worse, but if it took more than one insn, it will
  4179. be better. */
  4180. if (CONST_INT_P (XEXP (SET_SRC (x), 1))
  4181. && REG_P (XEXP (SET_SRC (x), 0))
  4182. && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
  4183. && REG_P (SET_DEST (x))
  4184. && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
  4185. && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
  4186. && XEXP (*split, 0) == SET_DEST (x)
  4187. && XEXP (*split, 1) == const0_rtx)
  4188. {
  4189. rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
  4190. XEXP (SET_SRC (x), 0),
  4191. pos, NULL_RTX, 1, 1, 0, 0);
  4192. if (extraction != 0)
  4193. {
  4194. SUBST (SET_SRC (x), extraction);
  4195. return find_split_point (loc, insn, false);
  4196. }
  4197. }
  4198. break;
  4199. case NE:
  4200. /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
  4201. is known to be on, this can be converted into a NEG of a shift. */
  4202. if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
  4203. && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
  4204. && 1 <= (pos = exact_log2
  4205. (nonzero_bits (XEXP (SET_SRC (x), 0),
  4206. GET_MODE (XEXP (SET_SRC (x), 0))))))
  4207. {
  4208. machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
  4209. SUBST (SET_SRC (x),
  4210. gen_rtx_NEG (mode,
  4211. gen_rtx_LSHIFTRT (mode,
  4212. XEXP (SET_SRC (x), 0),
  4213. GEN_INT (pos))));
  4214. split = find_split_point (&SET_SRC (x), insn, true);
  4215. if (split && split != &SET_SRC (x))
  4216. return split;
  4217. }
  4218. break;
  4219. case SIGN_EXTEND:
  4220. inner = XEXP (SET_SRC (x), 0);
  4221. /* We can't optimize if either mode is a partial integer
  4222. mode as we don't know how many bits are significant
  4223. in those modes. */
  4224. if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
  4225. || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
  4226. break;
  4227. pos = 0;
  4228. len = GET_MODE_PRECISION (GET_MODE (inner));
  4229. unsignedp = 0;
  4230. break;
  4231. case SIGN_EXTRACT:
  4232. case ZERO_EXTRACT:
  4233. if (CONST_INT_P (XEXP (SET_SRC (x), 1))
  4234. && CONST_INT_P (XEXP (SET_SRC (x), 2)))
  4235. {
  4236. inner = XEXP (SET_SRC (x), 0);
  4237. len = INTVAL (XEXP (SET_SRC (x), 1));
  4238. pos = INTVAL (XEXP (SET_SRC (x), 2));
  4239. if (BITS_BIG_ENDIAN)
  4240. pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
  4241. unsignedp = (code == ZERO_EXTRACT);
  4242. }
  4243. break;
  4244. default:
  4245. break;
  4246. }
  4247. if (len && pos >= 0
  4248. && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
  4249. {
  4250. machine_mode mode = GET_MODE (SET_SRC (x));
  4251. /* For unsigned, we have a choice of a shift followed by an
  4252. AND or two shifts. Use two shifts for field sizes where the
  4253. constant might be too large. We assume here that we can
  4254. always at least get 8-bit constants in an AND insn, which is
  4255. true for every current RISC. */
  4256. if (unsignedp && len <= 8)
  4257. {
  4258. unsigned HOST_WIDE_INT mask
  4259. = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
  4260. SUBST (SET_SRC (x),
  4261. gen_rtx_AND (mode,
  4262. gen_rtx_LSHIFTRT
  4263. (mode, gen_lowpart (mode, inner),
  4264. GEN_INT (pos)),
  4265. gen_int_mode (mask, mode)));
  4266. split = find_split_point (&SET_SRC (x), insn, true);
  4267. if (split && split != &SET_SRC (x))
  4268. return split;
  4269. }
  4270. else
  4271. {
  4272. SUBST (SET_SRC (x),
  4273. gen_rtx_fmt_ee
  4274. (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
  4275. gen_rtx_ASHIFT (mode,
  4276. gen_lowpart (mode, inner),
  4277. GEN_INT (GET_MODE_PRECISION (mode)
  4278. - len - pos)),
  4279. GEN_INT (GET_MODE_PRECISION (mode) - len)));
  4280. split = find_split_point (&SET_SRC (x), insn, true);
  4281. if (split && split != &SET_SRC (x))
  4282. return split;
  4283. }
  4284. }
  4285. /* See if this is a simple operation with a constant as the second
  4286. operand. It might be that this constant is out of range and hence
  4287. could be used as a split point. */
  4288. if (BINARY_P (SET_SRC (x))
  4289. && CONSTANT_P (XEXP (SET_SRC (x), 1))
  4290. && (OBJECT_P (XEXP (SET_SRC (x), 0))
  4291. || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
  4292. && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
  4293. return &XEXP (SET_SRC (x), 1);
  4294. /* Finally, see if this is a simple operation with its first operand
  4295. not in a register. The operation might require this operand in a
  4296. register, so return it as a split point. We can always do this
  4297. because if the first operand were another operation, we would have
  4298. already found it as a split point. */
  4299. if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
  4300. && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
  4301. return &XEXP (SET_SRC (x), 0);
  4302. return 0;
  4303. case AND:
  4304. case IOR:
  4305. /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
  4306. it is better to write this as (not (ior A B)) so we can split it.
  4307. Similarly for IOR. */
  4308. if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
  4309. {
  4310. SUBST (*loc,
  4311. gen_rtx_NOT (GET_MODE (x),
  4312. gen_rtx_fmt_ee (code == IOR ? AND : IOR,
  4313. GET_MODE (x),
  4314. XEXP (XEXP (x, 0), 0),
  4315. XEXP (XEXP (x, 1), 0))));
  4316. return find_split_point (loc, insn, set_src);
  4317. }
  4318. /* Many RISC machines have a large set of logical insns. If the
  4319. second operand is a NOT, put it first so we will try to split the
  4320. other operand first. */
  4321. if (GET_CODE (XEXP (x, 1)) == NOT)
  4322. {
  4323. rtx tem = XEXP (x, 0);
  4324. SUBST (XEXP (x, 0), XEXP (x, 1));
  4325. SUBST (XEXP (x, 1), tem);
  4326. }
  4327. break;
  4328. case PLUS:
  4329. case MINUS:
  4330. /* Canonicalization can produce (minus A (mult B C)), where C is a
  4331. constant. It may be better to try splitting (plus (mult B -C) A)
  4332. instead if this isn't a multiply by a power of two. */
  4333. if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
  4334. && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
  4335. && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
  4336. {
  4337. machine_mode mode = GET_MODE (x);
  4338. unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
  4339. HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
  4340. SUBST (*loc, gen_rtx_PLUS (mode,
  4341. gen_rtx_MULT (mode,
  4342. XEXP (XEXP (x, 1), 0),
  4343. gen_int_mode (other_int,
  4344. mode)),
  4345. XEXP (x, 0)));
  4346. return find_split_point (loc, insn, set_src);
  4347. }
  4348. /* Split at a multiply-accumulate instruction. However if this is
  4349. the SET_SRC, we likely do not have such an instruction and it's
  4350. worthless to try this split. */
  4351. if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
  4352. return loc;
  4353. default:
  4354. break;
  4355. }
  4356. /* Otherwise, select our actions depending on our rtx class. */
  4357. switch (GET_RTX_CLASS (code))
  4358. {
  4359. case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
  4360. case RTX_TERNARY:
  4361. split = find_split_point (&XEXP (x, 2), insn, false);
  4362. if (split)
  4363. return split;
  4364. /* ... fall through ... */
  4365. case RTX_BIN_ARITH:
  4366. case RTX_COMM_ARITH:
  4367. case RTX_COMPARE:
  4368. case RTX_COMM_COMPARE:
  4369. split = find_split_point (&XEXP (x, 1), insn, false);
  4370. if (split)
  4371. return split;
  4372. /* ... fall through ... */
  4373. case RTX_UNARY:
  4374. /* Some machines have (and (shift ...) ...) insns. If X is not
  4375. an AND, but XEXP (X, 0) is, use it as our split point. */
  4376. if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
  4377. return &XEXP (x, 0);
  4378. split = find_split_point (&XEXP (x, 0), insn, false);
  4379. if (split)
  4380. return split;
  4381. return loc;
  4382. default:
  4383. /* Otherwise, we don't have a split point. */
  4384. return 0;
  4385. }
  4386. }
  4387. /* Throughout X, replace FROM with TO, and return the result.
  4388. The result is TO if X is FROM;
  4389. otherwise the result is X, but its contents may have been modified.
  4390. If they were modified, a record was made in undobuf so that
  4391. undo_all will (among other things) return X to its original state.
  4392. If the number of changes necessary is too much to record to undo,
  4393. the excess changes are not made, so the result is invalid.
  4394. The changes already made can still be undone.
  4395. undobuf.num_undo is incremented for such changes, so by testing that
  4396. the caller can tell whether the result is valid.
  4397. `n_occurrences' is incremented each time FROM is replaced.
  4398. IN_DEST is nonzero if we are processing the SET_DEST of a SET.
  4399. IN_COND is nonzero if we are at the top level of a condition.
  4400. UNIQUE_COPY is nonzero if each substitution must be unique. We do this
  4401. by copying if `n_occurrences' is nonzero. */
  4402. static rtx
  4403. subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
  4404. {
  4405. enum rtx_code code = GET_CODE (x);
  4406. machine_mode op0_mode = VOIDmode;
  4407. const char *fmt;
  4408. int len, i;
  4409. rtx new_rtx;
  4410. /* Two expressions are equal if they are identical copies of a shared
  4411. RTX or if they are both registers with the same register number
  4412. and mode. */
  4413. #define COMBINE_RTX_EQUAL_P(X,Y) \
  4414. ((X) == (Y) \
  4415. || (REG_P (X) && REG_P (Y) \
  4416. && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
  4417. /* Do not substitute into clobbers of regs -- this will never result in
  4418. valid RTL. */
  4419. if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
  4420. return x;
  4421. if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
  4422. {
  4423. n_occurrences++;
  4424. return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
  4425. }
  4426. /* If X and FROM are the same register but different modes, they
  4427. will not have been seen as equal above. However, the log links code
  4428. will make a LOG_LINKS entry for that case. If we do nothing, we
  4429. will try to rerecognize our original insn and, when it succeeds,
  4430. we will delete the feeding insn, which is incorrect.
  4431. So force this insn not to match in this (rare) case. */
  4432. if (! in_dest && code == REG && REG_P (from)
  4433. && reg_overlap_mentioned_p (x, from))
  4434. return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
  4435. /* If this is an object, we are done unless it is a MEM or LO_SUM, both
  4436. of which may contain things that can be combined. */
  4437. if (code != MEM && code != LO_SUM && OBJECT_P (x))
  4438. return x;
  4439. /* It is possible to have a subexpression appear twice in the insn.
  4440. Suppose that FROM is a register that appears within TO.
  4441. Then, after that subexpression has been scanned once by `subst',
  4442. the second time it is scanned, TO may be found. If we were
  4443. to scan TO here, we would find FROM within it and create a
  4444. self-referent rtl structure which is completely wrong. */
  4445. if (COMBINE_RTX_EQUAL_P (x, to))
  4446. return to;
  4447. /* Parallel asm_operands need special attention because all of the
  4448. inputs are shared across the arms. Furthermore, unsharing the
  4449. rtl results in recognition failures. Failure to handle this case
  4450. specially can result in circular rtl.
  4451. Solve this by doing a normal pass across the first entry of the
  4452. parallel, and only processing the SET_DESTs of the subsequent
  4453. entries. Ug. */
  4454. if (code == PARALLEL
  4455. && GET_CODE (XVECEXP (x, 0, 0)) == SET
  4456. && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
  4457. {
  4458. new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
  4459. /* If this substitution failed, this whole thing fails. */
  4460. if (GET_CODE (new_rtx) == CLOBBER
  4461. && XEXP (new_rtx, 0) == const0_rtx)
  4462. return new_rtx;
  4463. SUBST (XVECEXP (x, 0, 0), new_rtx);
  4464. for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
  4465. {
  4466. rtx dest = SET_DEST (XVECEXP (x, 0, i));
  4467. if (!REG_P (dest)
  4468. && GET_CODE (dest) != CC0
  4469. && GET_CODE (dest) != PC)
  4470. {
  4471. new_rtx = subst (dest, from, to, 0, 0, unique_copy);
  4472. /* If this substitution failed, this whole thing fails. */
  4473. if (GET_CODE (new_rtx) == CLOBBER
  4474. && XEXP (new_rtx, 0) == const0_rtx)
  4475. return new_rtx;
  4476. SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
  4477. }
  4478. }
  4479. }
  4480. else
  4481. {
  4482. len = GET_RTX_LENGTH (code);
  4483. fmt = GET_RTX_FORMAT (code);
  4484. /* We don't need to process a SET_DEST that is a register, CC0,
  4485. or PC, so set up to skip this common case. All other cases
  4486. where we want to suppress replacing something inside a
  4487. SET_SRC are handled via the IN_DEST operand. */
  4488. if (code == SET
  4489. && (REG_P (SET_DEST (x))
  4490. || GET_CODE (SET_DEST (x)) == CC0
  4491. || GET_CODE (SET_DEST (x)) == PC))
  4492. fmt = "ie";
  4493. /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
  4494. constant. */
  4495. if (fmt[0] == 'e')
  4496. op0_mode = GET_MODE (XEXP (x, 0));
  4497. for (i = 0; i < len; i++)
  4498. {
  4499. if (fmt[i] == 'E')
  4500. {
  4501. int j;
  4502. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  4503. {
  4504. if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
  4505. {
  4506. new_rtx = (unique_copy && n_occurrences
  4507. ? copy_rtx (to) : to);
  4508. n_occurrences++;
  4509. }
  4510. else
  4511. {
  4512. new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
  4513. unique_copy);
  4514. /* If this substitution failed, this whole thing
  4515. fails. */
  4516. if (GET_CODE (new_rtx) == CLOBBER
  4517. && XEXP (new_rtx, 0) == const0_rtx)
  4518. return new_rtx;
  4519. }
  4520. SUBST (XVECEXP (x, i, j), new_rtx);
  4521. }
  4522. }
  4523. else if (fmt[i] == 'e')
  4524. {
  4525. /* If this is a register being set, ignore it. */
  4526. new_rtx = XEXP (x, i);
  4527. if (in_dest
  4528. && i == 0
  4529. && (((code == SUBREG || code == ZERO_EXTRACT)
  4530. && REG_P (new_rtx))
  4531. || code == STRICT_LOW_PART))
  4532. ;
  4533. else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
  4534. {
  4535. /* In general, don't install a subreg involving two
  4536. modes not tieable. It can worsen register
  4537. allocation, and can even make invalid reload
  4538. insns, since the reg inside may need to be copied
  4539. from in the outside mode, and that may be invalid
  4540. if it is an fp reg copied in integer mode.
  4541. We allow two exceptions to this: It is valid if
  4542. it is inside another SUBREG and the mode of that
  4543. SUBREG and the mode of the inside of TO is
  4544. tieable and it is valid if X is a SET that copies
  4545. FROM to CC0. */
  4546. if (GET_CODE (to) == SUBREG
  4547. && ! MODES_TIEABLE_P (GET_MODE (to),
  4548. GET_MODE (SUBREG_REG (to)))
  4549. && ! (code == SUBREG
  4550. && MODES_TIEABLE_P (GET_MODE (x),
  4551. GET_MODE (SUBREG_REG (to))))
  4552. #ifdef HAVE_cc0
  4553. && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
  4554. #endif
  4555. )
  4556. return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
  4557. if (code == SUBREG
  4558. && REG_P (to)
  4559. && REGNO (to) < FIRST_PSEUDO_REGISTER
  4560. && simplify_subreg_regno (REGNO (to), GET_MODE (to),
  4561. SUBREG_BYTE (x),
  4562. GET_MODE (x)) < 0)
  4563. return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
  4564. new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
  4565. n_occurrences++;
  4566. }
  4567. else
  4568. /* If we are in a SET_DEST, suppress most cases unless we
  4569. have gone inside a MEM, in which case we want to
  4570. simplify the address. We assume here that things that
  4571. are actually part of the destination have their inner
  4572. parts in the first expression. This is true for SUBREG,
  4573. STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
  4574. things aside from REG and MEM that should appear in a
  4575. SET_DEST. */
  4576. new_rtx = subst (XEXP (x, i), from, to,
  4577. (((in_dest
  4578. && (code == SUBREG || code == STRICT_LOW_PART
  4579. || code == ZERO_EXTRACT))
  4580. || code == SET)
  4581. && i == 0),
  4582. code == IF_THEN_ELSE && i == 0,
  4583. unique_copy);
  4584. /* If we found that we will have to reject this combination,
  4585. indicate that by returning the CLOBBER ourselves, rather than
  4586. an expression containing it. This will speed things up as
  4587. well as prevent accidents where two CLOBBERs are considered
  4588. to be equal, thus producing an incorrect simplification. */
  4589. if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
  4590. return new_rtx;
  4591. if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
  4592. {
  4593. machine_mode mode = GET_MODE (x);
  4594. x = simplify_subreg (GET_MODE (x), new_rtx,
  4595. GET_MODE (SUBREG_REG (x)),
  4596. SUBREG_BYTE (x));
  4597. if (! x)
  4598. x = gen_rtx_CLOBBER (mode, const0_rtx);
  4599. }
  4600. else if (CONST_SCALAR_INT_P (new_rtx)
  4601. && GET_CODE (x) == ZERO_EXTEND)
  4602. {
  4603. x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
  4604. new_rtx, GET_MODE (XEXP (x, 0)));
  4605. gcc_assert (x);
  4606. }
  4607. else
  4608. SUBST (XEXP (x, i), new_rtx);
  4609. }
  4610. }
  4611. }
  4612. /* Check if we are loading something from the constant pool via float
  4613. extension; in this case we would undo compress_float_constant
  4614. optimization and degenerate constant load to an immediate value. */
  4615. if (GET_CODE (x) == FLOAT_EXTEND
  4616. && MEM_P (XEXP (x, 0))
  4617. && MEM_READONLY_P (XEXP (x, 0)))
  4618. {
  4619. rtx tmp = avoid_constant_pool_reference (x);
  4620. if (x != tmp)
  4621. return x;
  4622. }
  4623. /* Try to simplify X. If the simplification changed the code, it is likely
  4624. that further simplification will help, so loop, but limit the number
  4625. of repetitions that will be performed. */
  4626. for (i = 0; i < 4; i++)
  4627. {
  4628. /* If X is sufficiently simple, don't bother trying to do anything
  4629. with it. */
  4630. if (code != CONST_INT && code != REG && code != CLOBBER)
  4631. x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
  4632. if (GET_CODE (x) == code)
  4633. break;
  4634. code = GET_CODE (x);
  4635. /* We no longer know the original mode of operand 0 since we
  4636. have changed the form of X) */
  4637. op0_mode = VOIDmode;
  4638. }
  4639. return x;
  4640. }
  4641. /* Simplify X, a piece of RTL. We just operate on the expression at the
  4642. outer level; call `subst' to simplify recursively. Return the new
  4643. expression.
  4644. OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
  4645. if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
  4646. of a condition. */
  4647. static rtx
  4648. combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
  4649. int in_cond)
  4650. {
  4651. enum rtx_code code = GET_CODE (x);
  4652. machine_mode mode = GET_MODE (x);
  4653. rtx temp;
  4654. int i;
  4655. /* If this is a commutative operation, put a constant last and a complex
  4656. expression first. We don't need to do this for comparisons here. */
  4657. if (COMMUTATIVE_ARITH_P (x)
  4658. && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
  4659. {
  4660. temp = XEXP (x, 0);
  4661. SUBST (XEXP (x, 0), XEXP (x, 1));
  4662. SUBST (XEXP (x, 1), temp);
  4663. }
  4664. /* If this is a simple operation applied to an IF_THEN_ELSE, try
  4665. applying it to the arms of the IF_THEN_ELSE. This often simplifies
  4666. things. Check for cases where both arms are testing the same
  4667. condition.
  4668. Don't do anything if all operands are very simple. */
  4669. if ((BINARY_P (x)
  4670. && ((!OBJECT_P (XEXP (x, 0))
  4671. && ! (GET_CODE (XEXP (x, 0)) == SUBREG
  4672. && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
  4673. || (!OBJECT_P (XEXP (x, 1))
  4674. && ! (GET_CODE (XEXP (x, 1)) == SUBREG
  4675. && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
  4676. || (UNARY_P (x)
  4677. && (!OBJECT_P (XEXP (x, 0))
  4678. && ! (GET_CODE (XEXP (x, 0)) == SUBREG
  4679. && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
  4680. {
  4681. rtx cond, true_rtx, false_rtx;
  4682. cond = if_then_else_cond (x, &true_rtx, &false_rtx);
  4683. if (cond != 0
  4684. /* If everything is a comparison, what we have is highly unlikely
  4685. to be simpler, so don't use it. */
  4686. && ! (COMPARISON_P (x)
  4687. && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
  4688. {
  4689. rtx cop1 = const0_rtx;
  4690. enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
  4691. if (cond_code == NE && COMPARISON_P (cond))
  4692. return x;
  4693. /* Simplify the alternative arms; this may collapse the true and
  4694. false arms to store-flag values. Be careful to use copy_rtx
  4695. here since true_rtx or false_rtx might share RTL with x as a
  4696. result of the if_then_else_cond call above. */
  4697. true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
  4698. false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
  4699. /* If true_rtx and false_rtx are not general_operands, an if_then_else
  4700. is unlikely to be simpler. */
  4701. if (general_operand (true_rtx, VOIDmode)
  4702. && general_operand (false_rtx, VOIDmode))
  4703. {
  4704. enum rtx_code reversed;
  4705. /* Restarting if we generate a store-flag expression will cause
  4706. us to loop. Just drop through in this case. */
  4707. /* If the result values are STORE_FLAG_VALUE and zero, we can
  4708. just make the comparison operation. */
  4709. if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
  4710. x = simplify_gen_relational (cond_code, mode, VOIDmode,
  4711. cond, cop1);
  4712. else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
  4713. && ((reversed = reversed_comparison_code_parts
  4714. (cond_code, cond, cop1, NULL))
  4715. != UNKNOWN))
  4716. x = simplify_gen_relational (reversed, mode, VOIDmode,
  4717. cond, cop1);
  4718. /* Likewise, we can make the negate of a comparison operation
  4719. if the result values are - STORE_FLAG_VALUE and zero. */
  4720. else if (CONST_INT_P (true_rtx)
  4721. && INTVAL (true_rtx) == - STORE_FLAG_VALUE
  4722. && false_rtx == const0_rtx)
  4723. x = simplify_gen_unary (NEG, mode,
  4724. simplify_gen_relational (cond_code,
  4725. mode, VOIDmode,
  4726. cond, cop1),
  4727. mode);
  4728. else if (CONST_INT_P (false_rtx)
  4729. && INTVAL (false_rtx) == - STORE_FLAG_VALUE
  4730. && true_rtx == const0_rtx
  4731. && ((reversed = reversed_comparison_code_parts
  4732. (cond_code, cond, cop1, NULL))
  4733. != UNKNOWN))
  4734. x = simplify_gen_unary (NEG, mode,
  4735. simplify_gen_relational (reversed,
  4736. mode, VOIDmode,
  4737. cond, cop1),
  4738. mode);
  4739. else
  4740. return gen_rtx_IF_THEN_ELSE (mode,
  4741. simplify_gen_relational (cond_code,
  4742. mode,
  4743. VOIDmode,
  4744. cond,
  4745. cop1),
  4746. true_rtx, false_rtx);
  4747. code = GET_CODE (x);
  4748. op0_mode = VOIDmode;
  4749. }
  4750. }
  4751. }
  4752. /* Try to fold this expression in case we have constants that weren't
  4753. present before. */
  4754. temp = 0;
  4755. switch (GET_RTX_CLASS (code))
  4756. {
  4757. case RTX_UNARY:
  4758. if (op0_mode == VOIDmode)
  4759. op0_mode = GET_MODE (XEXP (x, 0));
  4760. temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
  4761. break;
  4762. case RTX_COMPARE:
  4763. case RTX_COMM_COMPARE:
  4764. {
  4765. machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
  4766. if (cmp_mode == VOIDmode)
  4767. {
  4768. cmp_mode = GET_MODE (XEXP (x, 1));
  4769. if (cmp_mode == VOIDmode)
  4770. cmp_mode = op0_mode;
  4771. }
  4772. temp = simplify_relational_operation (code, mode, cmp_mode,
  4773. XEXP (x, 0), XEXP (x, 1));
  4774. }
  4775. break;
  4776. case RTX_COMM_ARITH:
  4777. case RTX_BIN_ARITH:
  4778. temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
  4779. break;
  4780. case RTX_BITFIELD_OPS:
  4781. case RTX_TERNARY:
  4782. temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
  4783. XEXP (x, 1), XEXP (x, 2));
  4784. break;
  4785. default:
  4786. break;
  4787. }
  4788. if (temp)
  4789. {
  4790. x = temp;
  4791. code = GET_CODE (temp);
  4792. op0_mode = VOIDmode;
  4793. mode = GET_MODE (temp);
  4794. }
  4795. /* First see if we can apply the inverse distributive law. */
  4796. if (code == PLUS || code == MINUS
  4797. || code == AND || code == IOR || code == XOR)
  4798. {
  4799. x = apply_distributive_law (x);
  4800. code = GET_CODE (x);
  4801. op0_mode = VOIDmode;
  4802. }
  4803. /* If CODE is an associative operation not otherwise handled, see if we
  4804. can associate some operands. This can win if they are constants or
  4805. if they are logically related (i.e. (a & b) & a). */
  4806. if ((code == PLUS || code == MINUS || code == MULT || code == DIV
  4807. || code == AND || code == IOR || code == XOR
  4808. || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
  4809. && ((INTEGRAL_MODE_P (mode) && code != DIV)
  4810. || (flag_associative_math && FLOAT_MODE_P (mode))))
  4811. {
  4812. if (GET_CODE (XEXP (x, 0)) == code)
  4813. {
  4814. rtx other = XEXP (XEXP (x, 0), 0);
  4815. rtx inner_op0 = XEXP (XEXP (x, 0), 1);
  4816. rtx inner_op1 = XEXP (x, 1);
  4817. rtx inner;
  4818. /* Make sure we pass the constant operand if any as the second
  4819. one if this is a commutative operation. */
  4820. if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
  4821. {
  4822. rtx tem = inner_op0;
  4823. inner_op0 = inner_op1;
  4824. inner_op1 = tem;
  4825. }
  4826. inner = simplify_binary_operation (code == MINUS ? PLUS
  4827. : code == DIV ? MULT
  4828. : code,
  4829. mode, inner_op0, inner_op1);
  4830. /* For commutative operations, try the other pair if that one
  4831. didn't simplify. */
  4832. if (inner == 0 && COMMUTATIVE_ARITH_P (x))
  4833. {
  4834. other = XEXP (XEXP (x, 0), 1);
  4835. inner = simplify_binary_operation (code, mode,
  4836. XEXP (XEXP (x, 0), 0),
  4837. XEXP (x, 1));
  4838. }
  4839. if (inner)
  4840. return simplify_gen_binary (code, mode, other, inner);
  4841. }
  4842. }
  4843. /* A little bit of algebraic simplification here. */
  4844. switch (code)
  4845. {
  4846. case MEM:
  4847. /* Ensure that our address has any ASHIFTs converted to MULT in case
  4848. address-recognizing predicates are called later. */
  4849. temp = make_compound_operation (XEXP (x, 0), MEM);
  4850. SUBST (XEXP (x, 0), temp);
  4851. break;
  4852. case SUBREG:
  4853. if (op0_mode == VOIDmode)
  4854. op0_mode = GET_MODE (SUBREG_REG (x));
  4855. /* See if this can be moved to simplify_subreg. */
  4856. if (CONSTANT_P (SUBREG_REG (x))
  4857. && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
  4858. /* Don't call gen_lowpart if the inner mode
  4859. is VOIDmode and we cannot simplify it, as SUBREG without
  4860. inner mode is invalid. */
  4861. && (GET_MODE (SUBREG_REG (x)) != VOIDmode
  4862. || gen_lowpart_common (mode, SUBREG_REG (x))))
  4863. return gen_lowpart (mode, SUBREG_REG (x));
  4864. if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
  4865. break;
  4866. {
  4867. rtx temp;
  4868. temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
  4869. SUBREG_BYTE (x));
  4870. if (temp)
  4871. return temp;
  4872. /* If op is known to have all lower bits zero, the result is zero. */
  4873. if (!in_dest
  4874. && SCALAR_INT_MODE_P (mode)
  4875. && SCALAR_INT_MODE_P (op0_mode)
  4876. && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
  4877. && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
  4878. && HWI_COMPUTABLE_MODE_P (op0_mode)
  4879. && (nonzero_bits (SUBREG_REG (x), op0_mode)
  4880. & GET_MODE_MASK (mode)) == 0)
  4881. return CONST0_RTX (mode);
  4882. }
  4883. /* Don't change the mode of the MEM if that would change the meaning
  4884. of the address. */
  4885. if (MEM_P (SUBREG_REG (x))
  4886. && (MEM_VOLATILE_P (SUBREG_REG (x))
  4887. || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
  4888. MEM_ADDR_SPACE (SUBREG_REG (x)))))
  4889. return gen_rtx_CLOBBER (mode, const0_rtx);
  4890. /* Note that we cannot do any narrowing for non-constants since
  4891. we might have been counting on using the fact that some bits were
  4892. zero. We now do this in the SET. */
  4893. break;
  4894. case NEG:
  4895. temp = expand_compound_operation (XEXP (x, 0));
  4896. /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
  4897. replaced by (lshiftrt X C). This will convert
  4898. (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
  4899. if (GET_CODE (temp) == ASHIFTRT
  4900. && CONST_INT_P (XEXP (temp, 1))
  4901. && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
  4902. return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
  4903. INTVAL (XEXP (temp, 1)));
  4904. /* If X has only a single bit that might be nonzero, say, bit I, convert
  4905. (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
  4906. MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
  4907. (sign_extract X 1 Y). But only do this if TEMP isn't a register
  4908. or a SUBREG of one since we'd be making the expression more
  4909. complex if it was just a register. */
  4910. if (!REG_P (temp)
  4911. && ! (GET_CODE (temp) == SUBREG
  4912. && REG_P (SUBREG_REG (temp)))
  4913. && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
  4914. {
  4915. rtx temp1 = simplify_shift_const
  4916. (NULL_RTX, ASHIFTRT, mode,
  4917. simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
  4918. GET_MODE_PRECISION (mode) - 1 - i),
  4919. GET_MODE_PRECISION (mode) - 1 - i);
  4920. /* If all we did was surround TEMP with the two shifts, we
  4921. haven't improved anything, so don't use it. Otherwise,
  4922. we are better off with TEMP1. */
  4923. if (GET_CODE (temp1) != ASHIFTRT
  4924. || GET_CODE (XEXP (temp1, 0)) != ASHIFT
  4925. || XEXP (XEXP (temp1, 0), 0) != temp)
  4926. return temp1;
  4927. }
  4928. break;
  4929. case TRUNCATE:
  4930. /* We can't handle truncation to a partial integer mode here
  4931. because we don't know the real bitsize of the partial
  4932. integer mode. */
  4933. if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
  4934. break;
  4935. if (HWI_COMPUTABLE_MODE_P (mode))
  4936. SUBST (XEXP (x, 0),
  4937. force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
  4938. GET_MODE_MASK (mode), 0));
  4939. /* We can truncate a constant value and return it. */
  4940. if (CONST_INT_P (XEXP (x, 0)))
  4941. return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
  4942. /* Similarly to what we do in simplify-rtx.c, a truncate of a register
  4943. whose value is a comparison can be replaced with a subreg if
  4944. STORE_FLAG_VALUE permits. */
  4945. if (HWI_COMPUTABLE_MODE_P (mode)
  4946. && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
  4947. && (temp = get_last_value (XEXP (x, 0)))
  4948. && COMPARISON_P (temp))
  4949. return gen_lowpart (mode, XEXP (x, 0));
  4950. break;
  4951. case CONST:
  4952. /* (const (const X)) can become (const X). Do it this way rather than
  4953. returning the inner CONST since CONST can be shared with a
  4954. REG_EQUAL note. */
  4955. if (GET_CODE (XEXP (x, 0)) == CONST)
  4956. SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  4957. break;
  4958. #ifdef HAVE_lo_sum
  4959. case LO_SUM:
  4960. /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
  4961. can add in an offset. find_split_point will split this address up
  4962. again if it doesn't match. */
  4963. if (GET_CODE (XEXP (x, 0)) == HIGH
  4964. && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
  4965. return XEXP (x, 1);
  4966. break;
  4967. #endif
  4968. case PLUS:
  4969. /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
  4970. when c is (const_int (pow2 + 1) / 2) is a sign extension of a
  4971. bit-field and can be replaced by either a sign_extend or a
  4972. sign_extract. The `and' may be a zero_extend and the two
  4973. <c>, -<c> constants may be reversed. */
  4974. if (GET_CODE (XEXP (x, 0)) == XOR
  4975. && CONST_INT_P (XEXP (x, 1))
  4976. && CONST_INT_P (XEXP (XEXP (x, 0), 1))
  4977. && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
  4978. && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
  4979. || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
  4980. && HWI_COMPUTABLE_MODE_P (mode)
  4981. && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
  4982. && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
  4983. && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
  4984. == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
  4985. || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
  4986. && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
  4987. == (unsigned int) i + 1))))
  4988. return simplify_shift_const
  4989. (NULL_RTX, ASHIFTRT, mode,
  4990. simplify_shift_const (NULL_RTX, ASHIFT, mode,
  4991. XEXP (XEXP (XEXP (x, 0), 0), 0),
  4992. GET_MODE_PRECISION (mode) - (i + 1)),
  4993. GET_MODE_PRECISION (mode) - (i + 1));
  4994. /* If only the low-order bit of X is possibly nonzero, (plus x -1)
  4995. can become (ashiftrt (ashift (xor x 1) C) C) where C is
  4996. the bitsize of the mode - 1. This allows simplification of
  4997. "a = (b & 8) == 0;" */
  4998. if (XEXP (x, 1) == constm1_rtx
  4999. && !REG_P (XEXP (x, 0))
  5000. && ! (GET_CODE (XEXP (x, 0)) == SUBREG
  5001. && REG_P (SUBREG_REG (XEXP (x, 0))))
  5002. && nonzero_bits (XEXP (x, 0), mode) == 1)
  5003. return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
  5004. simplify_shift_const (NULL_RTX, ASHIFT, mode,
  5005. gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
  5006. GET_MODE_PRECISION (mode) - 1),
  5007. GET_MODE_PRECISION (mode) - 1);
  5008. /* If we are adding two things that have no bits in common, convert
  5009. the addition into an IOR. This will often be further simplified,
  5010. for example in cases like ((a & 1) + (a & 2)), which can
  5011. become a & 3. */
  5012. if (HWI_COMPUTABLE_MODE_P (mode)
  5013. && (nonzero_bits (XEXP (x, 0), mode)
  5014. & nonzero_bits (XEXP (x, 1), mode)) == 0)
  5015. {
  5016. /* Try to simplify the expression further. */
  5017. rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
  5018. temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
  5019. /* If we could, great. If not, do not go ahead with the IOR
  5020. replacement, since PLUS appears in many special purpose
  5021. address arithmetic instructions. */
  5022. if (GET_CODE (temp) != CLOBBER
  5023. && (GET_CODE (temp) != IOR
  5024. || ((XEXP (temp, 0) != XEXP (x, 0)
  5025. || XEXP (temp, 1) != XEXP (x, 1))
  5026. && (XEXP (temp, 0) != XEXP (x, 1)
  5027. || XEXP (temp, 1) != XEXP (x, 0)))))
  5028. return temp;
  5029. }
  5030. break;
  5031. case MINUS:
  5032. /* (minus <foo> (and <foo> (const_int -pow2))) becomes
  5033. (and <foo> (const_int pow2-1)) */
  5034. if (GET_CODE (XEXP (x, 1)) == AND
  5035. && CONST_INT_P (XEXP (XEXP (x, 1), 1))
  5036. && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
  5037. && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
  5038. return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
  5039. -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
  5040. break;
  5041. case MULT:
  5042. /* If we have (mult (plus A B) C), apply the distributive law and then
  5043. the inverse distributive law to see if things simplify. This
  5044. occurs mostly in addresses, often when unrolling loops. */
  5045. if (GET_CODE (XEXP (x, 0)) == PLUS)
  5046. {
  5047. rtx result = distribute_and_simplify_rtx (x, 0);
  5048. if (result)
  5049. return result;
  5050. }
  5051. /* Try simplify a*(b/c) as (a*b)/c. */
  5052. if (FLOAT_MODE_P (mode) && flag_associative_math
  5053. && GET_CODE (XEXP (x, 0)) == DIV)
  5054. {
  5055. rtx tem = simplify_binary_operation (MULT, mode,
  5056. XEXP (XEXP (x, 0), 0),
  5057. XEXP (x, 1));
  5058. if (tem)
  5059. return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
  5060. }
  5061. break;
  5062. case UDIV:
  5063. /* If this is a divide by a power of two, treat it as a shift if
  5064. its first operand is a shift. */
  5065. if (CONST_INT_P (XEXP (x, 1))
  5066. && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
  5067. && (GET_CODE (XEXP (x, 0)) == ASHIFT
  5068. || GET_CODE (XEXP (x, 0)) == LSHIFTRT
  5069. || GET_CODE (XEXP (x, 0)) == ASHIFTRT
  5070. || GET_CODE (XEXP (x, 0)) == ROTATE
  5071. || GET_CODE (XEXP (x, 0)) == ROTATERT))
  5072. return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
  5073. break;
  5074. case EQ: case NE:
  5075. case GT: case GTU: case GE: case GEU:
  5076. case LT: case LTU: case LE: case LEU:
  5077. case UNEQ: case LTGT:
  5078. case UNGT: case UNGE:
  5079. case UNLT: case UNLE:
  5080. case UNORDERED: case ORDERED:
  5081. /* If the first operand is a condition code, we can't do anything
  5082. with it. */
  5083. if (GET_CODE (XEXP (x, 0)) == COMPARE
  5084. || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
  5085. && ! CC0_P (XEXP (x, 0))))
  5086. {
  5087. rtx op0 = XEXP (x, 0);
  5088. rtx op1 = XEXP (x, 1);
  5089. enum rtx_code new_code;
  5090. if (GET_CODE (op0) == COMPARE)
  5091. op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
  5092. /* Simplify our comparison, if possible. */
  5093. new_code = simplify_comparison (code, &op0, &op1);
  5094. /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
  5095. if only the low-order bit is possibly nonzero in X (such as when
  5096. X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
  5097. (xor X 1) or (minus 1 X); we use the former. Finally, if X is
  5098. known to be either 0 or -1, NE becomes a NEG and EQ becomes
  5099. (plus X 1).
  5100. Remove any ZERO_EXTRACT we made when thinking this was a
  5101. comparison. It may now be simpler to use, e.g., an AND. If a
  5102. ZERO_EXTRACT is indeed appropriate, it will be placed back by
  5103. the call to make_compound_operation in the SET case.
  5104. Don't apply these optimizations if the caller would
  5105. prefer a comparison rather than a value.
  5106. E.g., for the condition in an IF_THEN_ELSE most targets need
  5107. an explicit comparison. */
  5108. if (in_cond)
  5109. ;
  5110. else if (STORE_FLAG_VALUE == 1
  5111. && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
  5112. && op1 == const0_rtx
  5113. && mode == GET_MODE (op0)
  5114. && nonzero_bits (op0, mode) == 1)
  5115. return gen_lowpart (mode,
  5116. expand_compound_operation (op0));
  5117. else if (STORE_FLAG_VALUE == 1
  5118. && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
  5119. && op1 == const0_rtx
  5120. && mode == GET_MODE (op0)
  5121. && (num_sign_bit_copies (op0, mode)
  5122. == GET_MODE_PRECISION (mode)))
  5123. {
  5124. op0 = expand_compound_operation (op0);
  5125. return simplify_gen_unary (NEG, mode,
  5126. gen_lowpart (mode, op0),
  5127. mode);
  5128. }
  5129. else if (STORE_FLAG_VALUE == 1
  5130. && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
  5131. && op1 == const0_rtx
  5132. && mode == GET_MODE (op0)
  5133. && nonzero_bits (op0, mode) == 1)
  5134. {
  5135. op0 = expand_compound_operation (op0);
  5136. return simplify_gen_binary (XOR, mode,
  5137. gen_lowpart (mode, op0),
  5138. const1_rtx);
  5139. }
  5140. else if (STORE_FLAG_VALUE == 1
  5141. && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
  5142. && op1 == const0_rtx
  5143. && mode == GET_MODE (op0)
  5144. && (num_sign_bit_copies (op0, mode)
  5145. == GET_MODE_PRECISION (mode)))
  5146. {
  5147. op0 = expand_compound_operation (op0);
  5148. return plus_constant (mode, gen_lowpart (mode, op0), 1);
  5149. }
  5150. /* If STORE_FLAG_VALUE is -1, we have cases similar to
  5151. those above. */
  5152. if (in_cond)
  5153. ;
  5154. else if (STORE_FLAG_VALUE == -1
  5155. && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
  5156. && op1 == const0_rtx
  5157. && mode == GET_MODE (op0)
  5158. && (num_sign_bit_copies (op0, mode)
  5159. == GET_MODE_PRECISION (mode)))
  5160. return gen_lowpart (mode,
  5161. expand_compound_operation (op0));
  5162. else if (STORE_FLAG_VALUE == -1
  5163. && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
  5164. && op1 == const0_rtx
  5165. && mode == GET_MODE (op0)
  5166. && nonzero_bits (op0, mode) == 1)
  5167. {
  5168. op0 = expand_compound_operation (op0);
  5169. return simplify_gen_unary (NEG, mode,
  5170. gen_lowpart (mode, op0),
  5171. mode);
  5172. }
  5173. else if (STORE_FLAG_VALUE == -1
  5174. && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
  5175. && op1 == const0_rtx
  5176. && mode == GET_MODE (op0)
  5177. && (num_sign_bit_copies (op0, mode)
  5178. == GET_MODE_PRECISION (mode)))
  5179. {
  5180. op0 = expand_compound_operation (op0);
  5181. return simplify_gen_unary (NOT, mode,
  5182. gen_lowpart (mode, op0),
  5183. mode);
  5184. }
  5185. /* If X is 0/1, (eq X 0) is X-1. */
  5186. else if (STORE_FLAG_VALUE == -1
  5187. && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
  5188. && op1 == const0_rtx
  5189. && mode == GET_MODE (op0)
  5190. && nonzero_bits (op0, mode) == 1)
  5191. {
  5192. op0 = expand_compound_operation (op0);
  5193. return plus_constant (mode, gen_lowpart (mode, op0), -1);
  5194. }
  5195. /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
  5196. one bit that might be nonzero, we can convert (ne x 0) to
  5197. (ashift x c) where C puts the bit in the sign bit. Remove any
  5198. AND with STORE_FLAG_VALUE when we are done, since we are only
  5199. going to test the sign bit. */
  5200. if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
  5201. && HWI_COMPUTABLE_MODE_P (mode)
  5202. && val_signbit_p (mode, STORE_FLAG_VALUE)
  5203. && op1 == const0_rtx
  5204. && mode == GET_MODE (op0)
  5205. && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
  5206. {
  5207. x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
  5208. expand_compound_operation (op0),
  5209. GET_MODE_PRECISION (mode) - 1 - i);
  5210. if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
  5211. return XEXP (x, 0);
  5212. else
  5213. return x;
  5214. }
  5215. /* If the code changed, return a whole new comparison.
  5216. We also need to avoid using SUBST in cases where
  5217. simplify_comparison has widened a comparison with a CONST_INT,
  5218. since in that case the wider CONST_INT may fail the sanity
  5219. checks in do_SUBST. */
  5220. if (new_code != code
  5221. || (CONST_INT_P (op1)
  5222. && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
  5223. && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
  5224. return gen_rtx_fmt_ee (new_code, mode, op0, op1);
  5225. /* Otherwise, keep this operation, but maybe change its operands.
  5226. This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
  5227. SUBST (XEXP (x, 0), op0);
  5228. SUBST (XEXP (x, 1), op1);
  5229. }
  5230. break;
  5231. case IF_THEN_ELSE:
  5232. return simplify_if_then_else (x);
  5233. case ZERO_EXTRACT:
  5234. case SIGN_EXTRACT:
  5235. case ZERO_EXTEND:
  5236. case SIGN_EXTEND:
  5237. /* If we are processing SET_DEST, we are done. */
  5238. if (in_dest)
  5239. return x;
  5240. return expand_compound_operation (x);
  5241. case SET:
  5242. return simplify_set (x);
  5243. case AND:
  5244. case IOR:
  5245. return simplify_logical (x);
  5246. case ASHIFT:
  5247. case LSHIFTRT:
  5248. case ASHIFTRT:
  5249. case ROTATE:
  5250. case ROTATERT:
  5251. /* If this is a shift by a constant amount, simplify it. */
  5252. if (CONST_INT_P (XEXP (x, 1)))
  5253. return simplify_shift_const (x, code, mode, XEXP (x, 0),
  5254. INTVAL (XEXP (x, 1)));
  5255. else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
  5256. SUBST (XEXP (x, 1),
  5257. force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
  5258. ((unsigned HOST_WIDE_INT) 1
  5259. << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
  5260. - 1,
  5261. 0));
  5262. break;
  5263. default:
  5264. break;
  5265. }
  5266. return x;
  5267. }
  5268. /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
  5269. static rtx
  5270. simplify_if_then_else (rtx x)
  5271. {
  5272. machine_mode mode = GET_MODE (x);
  5273. rtx cond = XEXP (x, 0);
  5274. rtx true_rtx = XEXP (x, 1);
  5275. rtx false_rtx = XEXP (x, 2);
  5276. enum rtx_code true_code = GET_CODE (cond);
  5277. int comparison_p = COMPARISON_P (cond);
  5278. rtx temp;
  5279. int i;
  5280. enum rtx_code false_code;
  5281. rtx reversed;
  5282. /* Simplify storing of the truth value. */
  5283. if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
  5284. return simplify_gen_relational (true_code, mode, VOIDmode,
  5285. XEXP (cond, 0), XEXP (cond, 1));
  5286. /* Also when the truth value has to be reversed. */
  5287. if (comparison_p
  5288. && true_rtx == const0_rtx && false_rtx == const_true_rtx
  5289. && (reversed = reversed_comparison (cond, mode)))
  5290. return reversed;
  5291. /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
  5292. in it is being compared against certain values. Get the true and false
  5293. comparisons and see if that says anything about the value of each arm. */
  5294. if (comparison_p
  5295. && ((false_code = reversed_comparison_code (cond, NULL))
  5296. != UNKNOWN)
  5297. && REG_P (XEXP (cond, 0)))
  5298. {
  5299. HOST_WIDE_INT nzb;
  5300. rtx from = XEXP (cond, 0);
  5301. rtx true_val = XEXP (cond, 1);
  5302. rtx false_val = true_val;
  5303. int swapped = 0;
  5304. /* If FALSE_CODE is EQ, swap the codes and arms. */
  5305. if (false_code == EQ)
  5306. {
  5307. swapped = 1, true_code = EQ, false_code = NE;
  5308. temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
  5309. }
  5310. /* If we are comparing against zero and the expression being tested has
  5311. only a single bit that might be nonzero, that is its value when it is
  5312. not equal to zero. Similarly if it is known to be -1 or 0. */
  5313. if (true_code == EQ && true_val == const0_rtx
  5314. && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
  5315. {
  5316. false_code = EQ;
  5317. false_val = gen_int_mode (nzb, GET_MODE (from));
  5318. }
  5319. else if (true_code == EQ && true_val == const0_rtx
  5320. && (num_sign_bit_copies (from, GET_MODE (from))
  5321. == GET_MODE_PRECISION (GET_MODE (from))))
  5322. {
  5323. false_code = EQ;
  5324. false_val = constm1_rtx;
  5325. }
  5326. /* Now simplify an arm if we know the value of the register in the
  5327. branch and it is used in the arm. Be careful due to the potential
  5328. of locally-shared RTL. */
  5329. if (reg_mentioned_p (from, true_rtx))
  5330. true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
  5331. from, true_val),
  5332. pc_rtx, pc_rtx, 0, 0, 0);
  5333. if (reg_mentioned_p (from, false_rtx))
  5334. false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
  5335. from, false_val),
  5336. pc_rtx, pc_rtx, 0, 0, 0);
  5337. SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
  5338. SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
  5339. true_rtx = XEXP (x, 1);
  5340. false_rtx = XEXP (x, 2);
  5341. true_code = GET_CODE (cond);
  5342. }
  5343. /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
  5344. reversed, do so to avoid needing two sets of patterns for
  5345. subtract-and-branch insns. Similarly if we have a constant in the true
  5346. arm, the false arm is the same as the first operand of the comparison, or
  5347. the false arm is more complicated than the true arm. */
  5348. if (comparison_p
  5349. && reversed_comparison_code (cond, NULL) != UNKNOWN
  5350. && (true_rtx == pc_rtx
  5351. || (CONSTANT_P (true_rtx)
  5352. && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
  5353. || true_rtx == const0_rtx
  5354. || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
  5355. || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
  5356. && !OBJECT_P (false_rtx))
  5357. || reg_mentioned_p (true_rtx, false_rtx)
  5358. || rtx_equal_p (false_rtx, XEXP (cond, 0))))
  5359. {
  5360. true_code = reversed_comparison_code (cond, NULL);
  5361. SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
  5362. SUBST (XEXP (x, 1), false_rtx);
  5363. SUBST (XEXP (x, 2), true_rtx);
  5364. temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
  5365. cond = XEXP (x, 0);
  5366. /* It is possible that the conditional has been simplified out. */
  5367. true_code = GET_CODE (cond);
  5368. comparison_p = COMPARISON_P (cond);
  5369. }
  5370. /* If the two arms are identical, we don't need the comparison. */
  5371. if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
  5372. return true_rtx;
  5373. /* Convert a == b ? b : a to "a". */
  5374. if (true_code == EQ && ! side_effects_p (cond)
  5375. && !HONOR_NANS (mode)
  5376. && rtx_equal_p (XEXP (cond, 0), false_rtx)
  5377. && rtx_equal_p (XEXP (cond, 1), true_rtx))
  5378. return false_rtx;
  5379. else if (true_code == NE && ! side_effects_p (cond)
  5380. && !HONOR_NANS (mode)
  5381. && rtx_equal_p (XEXP (cond, 0), true_rtx)
  5382. && rtx_equal_p (XEXP (cond, 1), false_rtx))
  5383. return true_rtx;
  5384. /* Look for cases where we have (abs x) or (neg (abs X)). */
  5385. if (GET_MODE_CLASS (mode) == MODE_INT
  5386. && comparison_p
  5387. && XEXP (cond, 1) == const0_rtx
  5388. && GET_CODE (false_rtx) == NEG
  5389. && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
  5390. && rtx_equal_p (true_rtx, XEXP (cond, 0))
  5391. && ! side_effects_p (true_rtx))
  5392. switch (true_code)
  5393. {
  5394. case GT:
  5395. case GE:
  5396. return simplify_gen_unary (ABS, mode, true_rtx, mode);
  5397. case LT:
  5398. case LE:
  5399. return
  5400. simplify_gen_unary (NEG, mode,
  5401. simplify_gen_unary (ABS, mode, true_rtx, mode),
  5402. mode);
  5403. default:
  5404. break;
  5405. }
  5406. /* Look for MIN or MAX. */
  5407. if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
  5408. && comparison_p
  5409. && rtx_equal_p (XEXP (cond, 0), true_rtx)
  5410. && rtx_equal_p (XEXP (cond, 1), false_rtx)
  5411. && ! side_effects_p (cond))
  5412. switch (true_code)
  5413. {
  5414. case GE:
  5415. case GT:
  5416. return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
  5417. case LE:
  5418. case LT:
  5419. return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
  5420. case GEU:
  5421. case GTU:
  5422. return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
  5423. case LEU:
  5424. case LTU:
  5425. return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
  5426. default:
  5427. break;
  5428. }
  5429. /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
  5430. second operand is zero, this can be done as (OP Z (mult COND C2)) where
  5431. C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
  5432. SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
  5433. We can do this kind of thing in some cases when STORE_FLAG_VALUE is
  5434. neither 1 or -1, but it isn't worth checking for. */
  5435. if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
  5436. && comparison_p
  5437. && GET_MODE_CLASS (mode) == MODE_INT
  5438. && ! side_effects_p (x))
  5439. {
  5440. rtx t = make_compound_operation (true_rtx, SET);
  5441. rtx f = make_compound_operation (false_rtx, SET);
  5442. rtx cond_op0 = XEXP (cond, 0);
  5443. rtx cond_op1 = XEXP (cond, 1);
  5444. enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
  5445. machine_mode m = mode;
  5446. rtx z = 0, c1 = NULL_RTX;
  5447. if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
  5448. || GET_CODE (t) == IOR || GET_CODE (t) == XOR
  5449. || GET_CODE (t) == ASHIFT
  5450. || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
  5451. && rtx_equal_p (XEXP (t, 0), f))
  5452. c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
  5453. /* If an identity-zero op is commutative, check whether there
  5454. would be a match if we swapped the operands. */
  5455. else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
  5456. || GET_CODE (t) == XOR)
  5457. && rtx_equal_p (XEXP (t, 1), f))
  5458. c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
  5459. else if (GET_CODE (t) == SIGN_EXTEND
  5460. && (GET_CODE (XEXP (t, 0)) == PLUS
  5461. || GET_CODE (XEXP (t, 0)) == MINUS
  5462. || GET_CODE (XEXP (t, 0)) == IOR
  5463. || GET_CODE (XEXP (t, 0)) == XOR
  5464. || GET_CODE (XEXP (t, 0)) == ASHIFT
  5465. || GET_CODE (XEXP (t, 0)) == LSHIFTRT
  5466. || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
  5467. && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
  5468. && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
  5469. && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
  5470. && (num_sign_bit_copies (f, GET_MODE (f))
  5471. > (unsigned int)
  5472. (GET_MODE_PRECISION (mode)
  5473. - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
  5474. {
  5475. c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
  5476. extend_op = SIGN_EXTEND;
  5477. m = GET_MODE (XEXP (t, 0));
  5478. }
  5479. else if (GET_CODE (t) == SIGN_EXTEND
  5480. && (GET_CODE (XEXP (t, 0)) == PLUS
  5481. || GET_CODE (XEXP (t, 0)) == IOR
  5482. || GET_CODE (XEXP (t, 0)) == XOR)
  5483. && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
  5484. && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
  5485. && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
  5486. && (num_sign_bit_copies (f, GET_MODE (f))
  5487. > (unsigned int)
  5488. (GET_MODE_PRECISION (mode)
  5489. - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
  5490. {
  5491. c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
  5492. extend_op = SIGN_EXTEND;
  5493. m = GET_MODE (XEXP (t, 0));
  5494. }
  5495. else if (GET_CODE (t) == ZERO_EXTEND
  5496. && (GET_CODE (XEXP (t, 0)) == PLUS
  5497. || GET_CODE (XEXP (t, 0)) == MINUS
  5498. || GET_CODE (XEXP (t, 0)) == IOR
  5499. || GET_CODE (XEXP (t, 0)) == XOR
  5500. || GET_CODE (XEXP (t, 0)) == ASHIFT
  5501. || GET_CODE (XEXP (t, 0)) == LSHIFTRT
  5502. || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
  5503. && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
  5504. && HWI_COMPUTABLE_MODE_P (mode)
  5505. && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
  5506. && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
  5507. && ((nonzero_bits (f, GET_MODE (f))
  5508. & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
  5509. == 0))
  5510. {
  5511. c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
  5512. extend_op = ZERO_EXTEND;
  5513. m = GET_MODE (XEXP (t, 0));
  5514. }
  5515. else if (GET_CODE (t) == ZERO_EXTEND
  5516. && (GET_CODE (XEXP (t, 0)) == PLUS
  5517. || GET_CODE (XEXP (t, 0)) == IOR
  5518. || GET_CODE (XEXP (t, 0)) == XOR)
  5519. && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
  5520. && HWI_COMPUTABLE_MODE_P (mode)
  5521. && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
  5522. && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
  5523. && ((nonzero_bits (f, GET_MODE (f))
  5524. & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
  5525. == 0))
  5526. {
  5527. c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
  5528. extend_op = ZERO_EXTEND;
  5529. m = GET_MODE (XEXP (t, 0));
  5530. }
  5531. if (z)
  5532. {
  5533. temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
  5534. cond_op0, cond_op1),
  5535. pc_rtx, pc_rtx, 0, 0, 0);
  5536. temp = simplify_gen_binary (MULT, m, temp,
  5537. simplify_gen_binary (MULT, m, c1,
  5538. const_true_rtx));
  5539. temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
  5540. temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
  5541. if (extend_op != UNKNOWN)
  5542. temp = simplify_gen_unary (extend_op, mode, temp, m);
  5543. return temp;
  5544. }
  5545. }
  5546. /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
  5547. 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
  5548. negation of a single bit, we can convert this operation to a shift. We
  5549. can actually do this more generally, but it doesn't seem worth it. */
  5550. if (true_code == NE && XEXP (cond, 1) == const0_rtx
  5551. && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
  5552. && ((1 == nonzero_bits (XEXP (cond, 0), mode)
  5553. && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
  5554. || ((num_sign_bit_copies (XEXP (cond, 0), mode)
  5555. == GET_MODE_PRECISION (mode))
  5556. && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
  5557. return
  5558. simplify_shift_const (NULL_RTX, ASHIFT, mode,
  5559. gen_lowpart (mode, XEXP (cond, 0)), i);
  5560. /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
  5561. if (true_code == NE && XEXP (cond, 1) == const0_rtx
  5562. && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
  5563. && GET_MODE (XEXP (cond, 0)) == mode
  5564. && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
  5565. == nonzero_bits (XEXP (cond, 0), mode)
  5566. && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
  5567. return XEXP (cond, 0);
  5568. return x;
  5569. }
  5570. /* Simplify X, a SET expression. Return the new expression. */
  5571. static rtx
  5572. simplify_set (rtx x)
  5573. {
  5574. rtx src = SET_SRC (x);
  5575. rtx dest = SET_DEST (x);
  5576. machine_mode mode
  5577. = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
  5578. rtx_insn *other_insn;
  5579. rtx *cc_use;
  5580. /* (set (pc) (return)) gets written as (return). */
  5581. if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
  5582. return src;
  5583. /* Now that we know for sure which bits of SRC we are using, see if we can
  5584. simplify the expression for the object knowing that we only need the
  5585. low-order bits. */
  5586. if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
  5587. {
  5588. src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
  5589. SUBST (SET_SRC (x), src);
  5590. }
  5591. /* If we are setting CC0 or if the source is a COMPARE, look for the use of
  5592. the comparison result and try to simplify it unless we already have used
  5593. undobuf.other_insn. */
  5594. if ((GET_MODE_CLASS (mode) == MODE_CC
  5595. || GET_CODE (src) == COMPARE
  5596. || CC0_P (dest))
  5597. && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
  5598. && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
  5599. && COMPARISON_P (*cc_use)
  5600. && rtx_equal_p (XEXP (*cc_use, 0), dest))
  5601. {
  5602. enum rtx_code old_code = GET_CODE (*cc_use);
  5603. enum rtx_code new_code;
  5604. rtx op0, op1, tmp;
  5605. int other_changed = 0;
  5606. rtx inner_compare = NULL_RTX;
  5607. machine_mode compare_mode = GET_MODE (dest);
  5608. if (GET_CODE (src) == COMPARE)
  5609. {
  5610. op0 = XEXP (src, 0), op1 = XEXP (src, 1);
  5611. if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
  5612. {
  5613. inner_compare = op0;
  5614. op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
  5615. }
  5616. }
  5617. else
  5618. op0 = src, op1 = CONST0_RTX (GET_MODE (src));
  5619. tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
  5620. op0, op1);
  5621. if (!tmp)
  5622. new_code = old_code;
  5623. else if (!CONSTANT_P (tmp))
  5624. {
  5625. new_code = GET_CODE (tmp);
  5626. op0 = XEXP (tmp, 0);
  5627. op1 = XEXP (tmp, 1);
  5628. }
  5629. else
  5630. {
  5631. rtx pat = PATTERN (other_insn);
  5632. undobuf.other_insn = other_insn;
  5633. SUBST (*cc_use, tmp);
  5634. /* Attempt to simplify CC user. */
  5635. if (GET_CODE (pat) == SET)
  5636. {
  5637. rtx new_rtx = simplify_rtx (SET_SRC (pat));
  5638. if (new_rtx != NULL_RTX)
  5639. SUBST (SET_SRC (pat), new_rtx);
  5640. }
  5641. /* Convert X into a no-op move. */
  5642. SUBST (SET_DEST (x), pc_rtx);
  5643. SUBST (SET_SRC (x), pc_rtx);
  5644. return x;
  5645. }
  5646. /* Simplify our comparison, if possible. */
  5647. new_code = simplify_comparison (new_code, &op0, &op1);
  5648. #ifdef SELECT_CC_MODE
  5649. /* If this machine has CC modes other than CCmode, check to see if we
  5650. need to use a different CC mode here. */
  5651. if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
  5652. compare_mode = GET_MODE (op0);
  5653. else if (inner_compare
  5654. && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
  5655. && new_code == old_code
  5656. && op0 == XEXP (inner_compare, 0)
  5657. && op1 == XEXP (inner_compare, 1))
  5658. compare_mode = GET_MODE (inner_compare);
  5659. else
  5660. compare_mode = SELECT_CC_MODE (new_code, op0, op1);
  5661. #ifndef HAVE_cc0
  5662. /* If the mode changed, we have to change SET_DEST, the mode in the
  5663. compare, and the mode in the place SET_DEST is used. If SET_DEST is
  5664. a hard register, just build new versions with the proper mode. If it
  5665. is a pseudo, we lose unless it is only time we set the pseudo, in
  5666. which case we can safely change its mode. */
  5667. if (compare_mode != GET_MODE (dest))
  5668. {
  5669. if (can_change_dest_mode (dest, 0, compare_mode))
  5670. {
  5671. unsigned int regno = REGNO (dest);
  5672. rtx new_dest;
  5673. if (regno < FIRST_PSEUDO_REGISTER)
  5674. new_dest = gen_rtx_REG (compare_mode, regno);
  5675. else
  5676. {
  5677. SUBST_MODE (regno_reg_rtx[regno], compare_mode);
  5678. new_dest = regno_reg_rtx[regno];
  5679. }
  5680. SUBST (SET_DEST (x), new_dest);
  5681. SUBST (XEXP (*cc_use, 0), new_dest);
  5682. other_changed = 1;
  5683. dest = new_dest;
  5684. }
  5685. }
  5686. #endif /* cc0 */
  5687. #endif /* SELECT_CC_MODE */
  5688. /* If the code changed, we have to build a new comparison in
  5689. undobuf.other_insn. */
  5690. if (new_code != old_code)
  5691. {
  5692. int other_changed_previously = other_changed;
  5693. unsigned HOST_WIDE_INT mask;
  5694. rtx old_cc_use = *cc_use;
  5695. SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
  5696. dest, const0_rtx));
  5697. other_changed = 1;
  5698. /* If the only change we made was to change an EQ into an NE or
  5699. vice versa, OP0 has only one bit that might be nonzero, and OP1
  5700. is zero, check if changing the user of the condition code will
  5701. produce a valid insn. If it won't, we can keep the original code
  5702. in that insn by surrounding our operation with an XOR. */
  5703. if (((old_code == NE && new_code == EQ)
  5704. || (old_code == EQ && new_code == NE))
  5705. && ! other_changed_previously && op1 == const0_rtx
  5706. && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
  5707. && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
  5708. {
  5709. rtx pat = PATTERN (other_insn), note = 0;
  5710. if ((recog_for_combine (&pat, other_insn, &note) < 0
  5711. && ! check_asm_operands (pat)))
  5712. {
  5713. *cc_use = old_cc_use;
  5714. other_changed = 0;
  5715. op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
  5716. gen_int_mode (mask,
  5717. GET_MODE (op0)));
  5718. }
  5719. }
  5720. }
  5721. if (other_changed)
  5722. undobuf.other_insn = other_insn;
  5723. /* Otherwise, if we didn't previously have a COMPARE in the
  5724. correct mode, we need one. */
  5725. if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
  5726. {
  5727. SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
  5728. src = SET_SRC (x);
  5729. }
  5730. else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
  5731. {
  5732. SUBST (SET_SRC (x), op0);
  5733. src = SET_SRC (x);
  5734. }
  5735. /* Otherwise, update the COMPARE if needed. */
  5736. else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
  5737. {
  5738. SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
  5739. src = SET_SRC (x);
  5740. }
  5741. }
  5742. else
  5743. {
  5744. /* Get SET_SRC in a form where we have placed back any
  5745. compound expressions. Then do the checks below. */
  5746. src = make_compound_operation (src, SET);
  5747. SUBST (SET_SRC (x), src);
  5748. }
  5749. /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
  5750. and X being a REG or (subreg (reg)), we may be able to convert this to
  5751. (set (subreg:m2 x) (op)).
  5752. We can always do this if M1 is narrower than M2 because that means that
  5753. we only care about the low bits of the result.
  5754. However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
  5755. perform a narrower operation than requested since the high-order bits will
  5756. be undefined. On machine where it is defined, this transformation is safe
  5757. as long as M1 and M2 have the same number of words. */
  5758. if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
  5759. && !OBJECT_P (SUBREG_REG (src))
  5760. && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
  5761. / UNITS_PER_WORD)
  5762. == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
  5763. + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
  5764. #ifndef WORD_REGISTER_OPERATIONS
  5765. && (GET_MODE_SIZE (GET_MODE (src))
  5766. < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
  5767. #endif
  5768. #ifdef CANNOT_CHANGE_MODE_CLASS
  5769. && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
  5770. && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
  5771. GET_MODE (SUBREG_REG (src)),
  5772. GET_MODE (src)))
  5773. #endif
  5774. && (REG_P (dest)
  5775. || (GET_CODE (dest) == SUBREG
  5776. && REG_P (SUBREG_REG (dest)))))
  5777. {
  5778. SUBST (SET_DEST (x),
  5779. gen_lowpart (GET_MODE (SUBREG_REG (src)),
  5780. dest));
  5781. SUBST (SET_SRC (x), SUBREG_REG (src));
  5782. src = SET_SRC (x), dest = SET_DEST (x);
  5783. }
  5784. #ifdef HAVE_cc0
  5785. /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
  5786. in SRC. */
  5787. if (dest == cc0_rtx
  5788. && GET_CODE (src) == SUBREG
  5789. && subreg_lowpart_p (src)
  5790. && (GET_MODE_PRECISION (GET_MODE (src))
  5791. < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
  5792. {
  5793. rtx inner = SUBREG_REG (src);
  5794. machine_mode inner_mode = GET_MODE (inner);
  5795. /* Here we make sure that we don't have a sign bit on. */
  5796. if (val_signbit_known_clear_p (GET_MODE (src),
  5797. nonzero_bits (inner, inner_mode)))
  5798. {
  5799. SUBST (SET_SRC (x), inner);
  5800. src = SET_SRC (x);
  5801. }
  5802. }
  5803. #endif
  5804. #ifdef LOAD_EXTEND_OP
  5805. /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
  5806. would require a paradoxical subreg. Replace the subreg with a
  5807. zero_extend to avoid the reload that would otherwise be required. */
  5808. if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
  5809. && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
  5810. && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
  5811. && SUBREG_BYTE (src) == 0
  5812. && paradoxical_subreg_p (src)
  5813. && MEM_P (SUBREG_REG (src)))
  5814. {
  5815. SUBST (SET_SRC (x),
  5816. gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
  5817. GET_MODE (src), SUBREG_REG (src)));
  5818. src = SET_SRC (x);
  5819. }
  5820. #endif
  5821. /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
  5822. are comparing an item known to be 0 or -1 against 0, use a logical
  5823. operation instead. Check for one of the arms being an IOR of the other
  5824. arm with some value. We compute three terms to be IOR'ed together. In
  5825. practice, at most two will be nonzero. Then we do the IOR's. */
  5826. if (GET_CODE (dest) != PC
  5827. && GET_CODE (src) == IF_THEN_ELSE
  5828. && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
  5829. && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
  5830. && XEXP (XEXP (src, 0), 1) == const0_rtx
  5831. && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
  5832. #ifdef HAVE_conditional_move
  5833. && ! can_conditionally_move_p (GET_MODE (src))
  5834. #endif
  5835. && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
  5836. GET_MODE (XEXP (XEXP (src, 0), 0)))
  5837. == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
  5838. && ! side_effects_p (src))
  5839. {
  5840. rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
  5841. ? XEXP (src, 1) : XEXP (src, 2));
  5842. rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
  5843. ? XEXP (src, 2) : XEXP (src, 1));
  5844. rtx term1 = const0_rtx, term2, term3;
  5845. if (GET_CODE (true_rtx) == IOR
  5846. && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
  5847. term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
  5848. else if (GET_CODE (true_rtx) == IOR
  5849. && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
  5850. term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
  5851. else if (GET_CODE (false_rtx) == IOR
  5852. && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
  5853. term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
  5854. else if (GET_CODE (false_rtx) == IOR
  5855. && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
  5856. term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
  5857. term2 = simplify_gen_binary (AND, GET_MODE (src),
  5858. XEXP (XEXP (src, 0), 0), true_rtx);
  5859. term3 = simplify_gen_binary (AND, GET_MODE (src),
  5860. simplify_gen_unary (NOT, GET_MODE (src),
  5861. XEXP (XEXP (src, 0), 0),
  5862. GET_MODE (src)),
  5863. false_rtx);
  5864. SUBST (SET_SRC (x),
  5865. simplify_gen_binary (IOR, GET_MODE (src),
  5866. simplify_gen_binary (IOR, GET_MODE (src),
  5867. term1, term2),
  5868. term3));
  5869. src = SET_SRC (x);
  5870. }
  5871. /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
  5872. whole thing fail. */
  5873. if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
  5874. return src;
  5875. else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
  5876. return dest;
  5877. else
  5878. /* Convert this into a field assignment operation, if possible. */
  5879. return make_field_assignment (x);
  5880. }
  5881. /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
  5882. result. */
  5883. static rtx
  5884. simplify_logical (rtx x)
  5885. {
  5886. machine_mode mode = GET_MODE (x);
  5887. rtx op0 = XEXP (x, 0);
  5888. rtx op1 = XEXP (x, 1);
  5889. switch (GET_CODE (x))
  5890. {
  5891. case AND:
  5892. /* We can call simplify_and_const_int only if we don't lose
  5893. any (sign) bits when converting INTVAL (op1) to
  5894. "unsigned HOST_WIDE_INT". */
  5895. if (CONST_INT_P (op1)
  5896. && (HWI_COMPUTABLE_MODE_P (mode)
  5897. || INTVAL (op1) > 0))
  5898. {
  5899. x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
  5900. if (GET_CODE (x) != AND)
  5901. return x;
  5902. op0 = XEXP (x, 0);
  5903. op1 = XEXP (x, 1);
  5904. }
  5905. /* If we have any of (and (ior A B) C) or (and (xor A B) C),
  5906. apply the distributive law and then the inverse distributive
  5907. law to see if things simplify. */
  5908. if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
  5909. {
  5910. rtx result = distribute_and_simplify_rtx (x, 0);
  5911. if (result)
  5912. return result;
  5913. }
  5914. if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
  5915. {
  5916. rtx result = distribute_and_simplify_rtx (x, 1);
  5917. if (result)
  5918. return result;
  5919. }
  5920. break;
  5921. case IOR:
  5922. /* If we have (ior (and A B) C), apply the distributive law and then
  5923. the inverse distributive law to see if things simplify. */
  5924. if (GET_CODE (op0) == AND)
  5925. {
  5926. rtx result = distribute_and_simplify_rtx (x, 0);
  5927. if (result)
  5928. return result;
  5929. }
  5930. if (GET_CODE (op1) == AND)
  5931. {
  5932. rtx result = distribute_and_simplify_rtx (x, 1);
  5933. if (result)
  5934. return result;
  5935. }
  5936. break;
  5937. default:
  5938. gcc_unreachable ();
  5939. }
  5940. return x;
  5941. }
  5942. /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
  5943. operations" because they can be replaced with two more basic operations.
  5944. ZERO_EXTEND is also considered "compound" because it can be replaced with
  5945. an AND operation, which is simpler, though only one operation.
  5946. The function expand_compound_operation is called with an rtx expression
  5947. and will convert it to the appropriate shifts and AND operations,
  5948. simplifying at each stage.
  5949. The function make_compound_operation is called to convert an expression
  5950. consisting of shifts and ANDs into the equivalent compound expression.
  5951. It is the inverse of this function, loosely speaking. */
  5952. static rtx
  5953. expand_compound_operation (rtx x)
  5954. {
  5955. unsigned HOST_WIDE_INT pos = 0, len;
  5956. int unsignedp = 0;
  5957. unsigned int modewidth;
  5958. rtx tem;
  5959. switch (GET_CODE (x))
  5960. {
  5961. case ZERO_EXTEND:
  5962. unsignedp = 1;
  5963. case SIGN_EXTEND:
  5964. /* We can't necessarily use a const_int for a multiword mode;
  5965. it depends on implicitly extending the value.
  5966. Since we don't know the right way to extend it,
  5967. we can't tell whether the implicit way is right.
  5968. Even for a mode that is no wider than a const_int,
  5969. we can't win, because we need to sign extend one of its bits through
  5970. the rest of it, and we don't know which bit. */
  5971. if (CONST_INT_P (XEXP (x, 0)))
  5972. return x;
  5973. /* Return if (subreg:MODE FROM 0) is not a safe replacement for
  5974. (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
  5975. because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
  5976. reloaded. If not for that, MEM's would very rarely be safe.
  5977. Reject MODEs bigger than a word, because we might not be able
  5978. to reference a two-register group starting with an arbitrary register
  5979. (and currently gen_lowpart might crash for a SUBREG). */
  5980. if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
  5981. return x;
  5982. /* Reject MODEs that aren't scalar integers because turning vector
  5983. or complex modes into shifts causes problems. */
  5984. if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
  5985. return x;
  5986. len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
  5987. /* If the inner object has VOIDmode (the only way this can happen
  5988. is if it is an ASM_OPERANDS), we can't do anything since we don't
  5989. know how much masking to do. */
  5990. if (len == 0)
  5991. return x;
  5992. break;
  5993. case ZERO_EXTRACT:
  5994. unsignedp = 1;
  5995. /* ... fall through ... */
  5996. case SIGN_EXTRACT:
  5997. /* If the operand is a CLOBBER, just return it. */
  5998. if (GET_CODE (XEXP (x, 0)) == CLOBBER)
  5999. return XEXP (x, 0);
  6000. if (!CONST_INT_P (XEXP (x, 1))
  6001. || !CONST_INT_P (XEXP (x, 2))
  6002. || GET_MODE (XEXP (x, 0)) == VOIDmode)
  6003. return x;
  6004. /* Reject MODEs that aren't scalar integers because turning vector
  6005. or complex modes into shifts causes problems. */
  6006. if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
  6007. return x;
  6008. len = INTVAL (XEXP (x, 1));
  6009. pos = INTVAL (XEXP (x, 2));
  6010. /* This should stay within the object being extracted, fail otherwise. */
  6011. if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
  6012. return x;
  6013. if (BITS_BIG_ENDIAN)
  6014. pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
  6015. break;
  6016. default:
  6017. return x;
  6018. }
  6019. /* Convert sign extension to zero extension, if we know that the high
  6020. bit is not set, as this is easier to optimize. It will be converted
  6021. back to cheaper alternative in make_extraction. */
  6022. if (GET_CODE (x) == SIGN_EXTEND
  6023. && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
  6024. && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
  6025. & ~(((unsigned HOST_WIDE_INT)
  6026. GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
  6027. >> 1))
  6028. == 0)))
  6029. {
  6030. rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
  6031. rtx temp2 = expand_compound_operation (temp);
  6032. /* Make sure this is a profitable operation. */
  6033. if (set_src_cost (x, optimize_this_for_speed_p)
  6034. > set_src_cost (temp2, optimize_this_for_speed_p))
  6035. return temp2;
  6036. else if (set_src_cost (x, optimize_this_for_speed_p)
  6037. > set_src_cost (temp, optimize_this_for_speed_p))
  6038. return temp;
  6039. else
  6040. return x;
  6041. }
  6042. /* We can optimize some special cases of ZERO_EXTEND. */
  6043. if (GET_CODE (x) == ZERO_EXTEND)
  6044. {
  6045. /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
  6046. know that the last value didn't have any inappropriate bits
  6047. set. */
  6048. if (GET_CODE (XEXP (x, 0)) == TRUNCATE
  6049. && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
  6050. && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
  6051. && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
  6052. & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
  6053. return XEXP (XEXP (x, 0), 0);
  6054. /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
  6055. if (GET_CODE (XEXP (x, 0)) == SUBREG
  6056. && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
  6057. && subreg_lowpart_p (XEXP (x, 0))
  6058. && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
  6059. && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
  6060. & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
  6061. return SUBREG_REG (XEXP (x, 0));
  6062. /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
  6063. is a comparison and STORE_FLAG_VALUE permits. This is like
  6064. the first case, but it works even when GET_MODE (x) is larger
  6065. than HOST_WIDE_INT. */
  6066. if (GET_CODE (XEXP (x, 0)) == TRUNCATE
  6067. && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
  6068. && COMPARISON_P (XEXP (XEXP (x, 0), 0))
  6069. && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
  6070. <= HOST_BITS_PER_WIDE_INT)
  6071. && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
  6072. return XEXP (XEXP (x, 0), 0);
  6073. /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
  6074. if (GET_CODE (XEXP (x, 0)) == SUBREG
  6075. && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
  6076. && subreg_lowpart_p (XEXP (x, 0))
  6077. && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
  6078. && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
  6079. <= HOST_BITS_PER_WIDE_INT)
  6080. && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
  6081. return SUBREG_REG (XEXP (x, 0));
  6082. }
  6083. /* If we reach here, we want to return a pair of shifts. The inner
  6084. shift is a left shift of BITSIZE - POS - LEN bits. The outer
  6085. shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
  6086. logical depending on the value of UNSIGNEDP.
  6087. If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
  6088. converted into an AND of a shift.
  6089. We must check for the case where the left shift would have a negative
  6090. count. This can happen in a case like (x >> 31) & 255 on machines
  6091. that can't shift by a constant. On those machines, we would first
  6092. combine the shift with the AND to produce a variable-position
  6093. extraction. Then the constant of 31 would be substituted in
  6094. to produce such a position. */
  6095. modewidth = GET_MODE_PRECISION (GET_MODE (x));
  6096. if (modewidth >= pos + len)
  6097. {
  6098. machine_mode mode = GET_MODE (x);
  6099. tem = gen_lowpart (mode, XEXP (x, 0));
  6100. if (!tem || GET_CODE (tem) == CLOBBER)
  6101. return x;
  6102. tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
  6103. tem, modewidth - pos - len);
  6104. tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
  6105. mode, tem, modewidth - len);
  6106. }
  6107. else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
  6108. tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
  6109. simplify_shift_const (NULL_RTX, LSHIFTRT,
  6110. GET_MODE (x),
  6111. XEXP (x, 0), pos),
  6112. ((unsigned HOST_WIDE_INT) 1 << len) - 1);
  6113. else
  6114. /* Any other cases we can't handle. */
  6115. return x;
  6116. /* If we couldn't do this for some reason, return the original
  6117. expression. */
  6118. if (GET_CODE (tem) == CLOBBER)
  6119. return x;
  6120. return tem;
  6121. }
  6122. /* X is a SET which contains an assignment of one object into
  6123. a part of another (such as a bit-field assignment, STRICT_LOW_PART,
  6124. or certain SUBREGS). If possible, convert it into a series of
  6125. logical operations.
  6126. We half-heartedly support variable positions, but do not at all
  6127. support variable lengths. */
  6128. static const_rtx
  6129. expand_field_assignment (const_rtx x)
  6130. {
  6131. rtx inner;
  6132. rtx pos; /* Always counts from low bit. */
  6133. int len;
  6134. rtx mask, cleared, masked;
  6135. machine_mode compute_mode;
  6136. /* Loop until we find something we can't simplify. */
  6137. while (1)
  6138. {
  6139. if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
  6140. && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
  6141. {
  6142. inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
  6143. len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
  6144. pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
  6145. }
  6146. else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
  6147. && CONST_INT_P (XEXP (SET_DEST (x), 1)))
  6148. {
  6149. inner = XEXP (SET_DEST (x), 0);
  6150. len = INTVAL (XEXP (SET_DEST (x), 1));
  6151. pos = XEXP (SET_DEST (x), 2);
  6152. /* A constant position should stay within the width of INNER. */
  6153. if (CONST_INT_P (pos)
  6154. && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
  6155. break;
  6156. if (BITS_BIG_ENDIAN)
  6157. {
  6158. if (CONST_INT_P (pos))
  6159. pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
  6160. - INTVAL (pos));
  6161. else if (GET_CODE (pos) == MINUS
  6162. && CONST_INT_P (XEXP (pos, 1))
  6163. && (INTVAL (XEXP (pos, 1))
  6164. == GET_MODE_PRECISION (GET_MODE (inner)) - len))
  6165. /* If position is ADJUST - X, new position is X. */
  6166. pos = XEXP (pos, 0);
  6167. else
  6168. {
  6169. HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
  6170. pos = simplify_gen_binary (MINUS, GET_MODE (pos),
  6171. gen_int_mode (prec - len,
  6172. GET_MODE (pos)),
  6173. pos);
  6174. }
  6175. }
  6176. }
  6177. /* A SUBREG between two modes that occupy the same numbers of words
  6178. can be done by moving the SUBREG to the source. */
  6179. else if (GET_CODE (SET_DEST (x)) == SUBREG
  6180. /* We need SUBREGs to compute nonzero_bits properly. */
  6181. && nonzero_sign_valid
  6182. && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
  6183. + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
  6184. == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
  6185. + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
  6186. {
  6187. x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
  6188. gen_lowpart
  6189. (GET_MODE (SUBREG_REG (SET_DEST (x))),
  6190. SET_SRC (x)));
  6191. continue;
  6192. }
  6193. else
  6194. break;
  6195. while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
  6196. inner = SUBREG_REG (inner);
  6197. compute_mode = GET_MODE (inner);
  6198. /* Don't attempt bitwise arithmetic on non scalar integer modes. */
  6199. if (! SCALAR_INT_MODE_P (compute_mode))
  6200. {
  6201. machine_mode imode;
  6202. /* Don't do anything for vector or complex integral types. */
  6203. if (! FLOAT_MODE_P (compute_mode))
  6204. break;
  6205. /* Try to find an integral mode to pun with. */
  6206. imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
  6207. if (imode == BLKmode)
  6208. break;
  6209. compute_mode = imode;
  6210. inner = gen_lowpart (imode, inner);
  6211. }
  6212. /* Compute a mask of LEN bits, if we can do this on the host machine. */
  6213. if (len >= HOST_BITS_PER_WIDE_INT)
  6214. break;
  6215. /* Now compute the equivalent expression. Make a copy of INNER
  6216. for the SET_DEST in case it is a MEM into which we will substitute;
  6217. we don't want shared RTL in that case. */
  6218. mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
  6219. compute_mode);
  6220. cleared = simplify_gen_binary (AND, compute_mode,
  6221. simplify_gen_unary (NOT, compute_mode,
  6222. simplify_gen_binary (ASHIFT,
  6223. compute_mode,
  6224. mask, pos),
  6225. compute_mode),
  6226. inner);
  6227. masked = simplify_gen_binary (ASHIFT, compute_mode,
  6228. simplify_gen_binary (
  6229. AND, compute_mode,
  6230. gen_lowpart (compute_mode, SET_SRC (x)),
  6231. mask),
  6232. pos);
  6233. x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
  6234. simplify_gen_binary (IOR, compute_mode,
  6235. cleared, masked));
  6236. }
  6237. return x;
  6238. }
  6239. /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
  6240. it is an RTX that represents the (variable) starting position; otherwise,
  6241. POS is the (constant) starting bit position. Both are counted from the LSB.
  6242. UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
  6243. IN_DEST is nonzero if this is a reference in the destination of a SET.
  6244. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
  6245. a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
  6246. be used.
  6247. IN_COMPARE is nonzero if we are in a COMPARE. This means that a
  6248. ZERO_EXTRACT should be built even for bits starting at bit 0.
  6249. MODE is the desired mode of the result (if IN_DEST == 0).
  6250. The result is an RTX for the extraction or NULL_RTX if the target
  6251. can't handle it. */
  6252. static rtx
  6253. make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
  6254. rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
  6255. int in_dest, int in_compare)
  6256. {
  6257. /* This mode describes the size of the storage area
  6258. to fetch the overall value from. Within that, we
  6259. ignore the POS lowest bits, etc. */
  6260. machine_mode is_mode = GET_MODE (inner);
  6261. machine_mode inner_mode;
  6262. machine_mode wanted_inner_mode;
  6263. machine_mode wanted_inner_reg_mode = word_mode;
  6264. machine_mode pos_mode = word_mode;
  6265. machine_mode extraction_mode = word_mode;
  6266. machine_mode tmode = mode_for_size (len, MODE_INT, 1);
  6267. rtx new_rtx = 0;
  6268. rtx orig_pos_rtx = pos_rtx;
  6269. HOST_WIDE_INT orig_pos;
  6270. if (pos_rtx && CONST_INT_P (pos_rtx))
  6271. pos = INTVAL (pos_rtx), pos_rtx = 0;
  6272. if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
  6273. {
  6274. /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
  6275. consider just the QI as the memory to extract from.
  6276. The subreg adds or removes high bits; its mode is
  6277. irrelevant to the meaning of this extraction,
  6278. since POS and LEN count from the lsb. */
  6279. if (MEM_P (SUBREG_REG (inner)))
  6280. is_mode = GET_MODE (SUBREG_REG (inner));
  6281. inner = SUBREG_REG (inner);
  6282. }
  6283. else if (GET_CODE (inner) == ASHIFT
  6284. && CONST_INT_P (XEXP (inner, 1))
  6285. && pos_rtx == 0 && pos == 0
  6286. && len > UINTVAL (XEXP (inner, 1)))
  6287. {
  6288. /* We're extracting the least significant bits of an rtx
  6289. (ashift X (const_int C)), where LEN > C. Extract the
  6290. least significant (LEN - C) bits of X, giving an rtx
  6291. whose mode is MODE, then shift it left C times. */
  6292. new_rtx = make_extraction (mode, XEXP (inner, 0),
  6293. 0, 0, len - INTVAL (XEXP (inner, 1)),
  6294. unsignedp, in_dest, in_compare);
  6295. if (new_rtx != 0)
  6296. return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
  6297. }
  6298. else if (GET_CODE (inner) == TRUNCATE)
  6299. inner = XEXP (inner, 0);
  6300. inner_mode = GET_MODE (inner);
  6301. /* See if this can be done without an extraction. We never can if the
  6302. width of the field is not the same as that of some integer mode. For
  6303. registers, we can only avoid the extraction if the position is at the
  6304. low-order bit and this is either not in the destination or we have the
  6305. appropriate STRICT_LOW_PART operation available.
  6306. For MEM, we can avoid an extract if the field starts on an appropriate
  6307. boundary and we can change the mode of the memory reference. */
  6308. if (tmode != BLKmode
  6309. && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
  6310. && !MEM_P (inner)
  6311. && (inner_mode == tmode
  6312. || !REG_P (inner)
  6313. || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
  6314. || reg_truncated_to_mode (tmode, inner))
  6315. && (! in_dest
  6316. || (REG_P (inner)
  6317. && have_insn_for (STRICT_LOW_PART, tmode))))
  6318. || (MEM_P (inner) && pos_rtx == 0
  6319. && (pos
  6320. % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
  6321. : BITS_PER_UNIT)) == 0
  6322. /* We can't do this if we are widening INNER_MODE (it
  6323. may not be aligned, for one thing). */
  6324. && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
  6325. && (inner_mode == tmode
  6326. || (! mode_dependent_address_p (XEXP (inner, 0),
  6327. MEM_ADDR_SPACE (inner))
  6328. && ! MEM_VOLATILE_P (inner))))))
  6329. {
  6330. /* If INNER is a MEM, make a new MEM that encompasses just the desired
  6331. field. If the original and current mode are the same, we need not
  6332. adjust the offset. Otherwise, we do if bytes big endian.
  6333. If INNER is not a MEM, get a piece consisting of just the field
  6334. of interest (in this case POS % BITS_PER_WORD must be 0). */
  6335. if (MEM_P (inner))
  6336. {
  6337. HOST_WIDE_INT offset;
  6338. /* POS counts from lsb, but make OFFSET count in memory order. */
  6339. if (BYTES_BIG_ENDIAN)
  6340. offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
  6341. else
  6342. offset = pos / BITS_PER_UNIT;
  6343. new_rtx = adjust_address_nv (inner, tmode, offset);
  6344. }
  6345. else if (REG_P (inner))
  6346. {
  6347. if (tmode != inner_mode)
  6348. {
  6349. /* We can't call gen_lowpart in a DEST since we
  6350. always want a SUBREG (see below) and it would sometimes
  6351. return a new hard register. */
  6352. if (pos || in_dest)
  6353. {
  6354. HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
  6355. if (WORDS_BIG_ENDIAN
  6356. && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
  6357. final_word = ((GET_MODE_SIZE (inner_mode)
  6358. - GET_MODE_SIZE (tmode))
  6359. / UNITS_PER_WORD) - final_word;
  6360. final_word *= UNITS_PER_WORD;
  6361. if (BYTES_BIG_ENDIAN &&
  6362. GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
  6363. final_word += (GET_MODE_SIZE (inner_mode)
  6364. - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
  6365. /* Avoid creating invalid subregs, for example when
  6366. simplifying (x>>32)&255. */
  6367. if (!validate_subreg (tmode, inner_mode, inner, final_word))
  6368. return NULL_RTX;
  6369. new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
  6370. }
  6371. else
  6372. new_rtx = gen_lowpart (tmode, inner);
  6373. }
  6374. else
  6375. new_rtx = inner;
  6376. }
  6377. else
  6378. new_rtx = force_to_mode (inner, tmode,
  6379. len >= HOST_BITS_PER_WIDE_INT
  6380. ? ~(unsigned HOST_WIDE_INT) 0
  6381. : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
  6382. 0);
  6383. /* If this extraction is going into the destination of a SET,
  6384. make a STRICT_LOW_PART unless we made a MEM. */
  6385. if (in_dest)
  6386. return (MEM_P (new_rtx) ? new_rtx
  6387. : (GET_CODE (new_rtx) != SUBREG
  6388. ? gen_rtx_CLOBBER (tmode, const0_rtx)
  6389. : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
  6390. if (mode == tmode)
  6391. return new_rtx;
  6392. if (CONST_SCALAR_INT_P (new_rtx))
  6393. return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
  6394. mode, new_rtx, tmode);
  6395. /* If we know that no extraneous bits are set, and that the high
  6396. bit is not set, convert the extraction to the cheaper of
  6397. sign and zero extension, that are equivalent in these cases. */
  6398. if (flag_expensive_optimizations
  6399. && (HWI_COMPUTABLE_MODE_P (tmode)
  6400. && ((nonzero_bits (new_rtx, tmode)
  6401. & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
  6402. == 0)))
  6403. {
  6404. rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
  6405. rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
  6406. /* Prefer ZERO_EXTENSION, since it gives more information to
  6407. backends. */
  6408. if (set_src_cost (temp, optimize_this_for_speed_p)
  6409. <= set_src_cost (temp1, optimize_this_for_speed_p))
  6410. return temp;
  6411. return temp1;
  6412. }
  6413. /* Otherwise, sign- or zero-extend unless we already are in the
  6414. proper mode. */
  6415. return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
  6416. mode, new_rtx));
  6417. }
  6418. /* Unless this is a COMPARE or we have a funny memory reference,
  6419. don't do anything with zero-extending field extracts starting at
  6420. the low-order bit since they are simple AND operations. */
  6421. if (pos_rtx == 0 && pos == 0 && ! in_dest
  6422. && ! in_compare && unsignedp)
  6423. return 0;
  6424. /* Unless INNER is not MEM, reject this if we would be spanning bytes or
  6425. if the position is not a constant and the length is not 1. In all
  6426. other cases, we would only be going outside our object in cases when
  6427. an original shift would have been undefined. */
  6428. if (MEM_P (inner)
  6429. && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
  6430. || (pos_rtx != 0 && len != 1)))
  6431. return 0;
  6432. enum extraction_pattern pattern = (in_dest ? EP_insv
  6433. : unsignedp ? EP_extzv : EP_extv);
  6434. /* If INNER is not from memory, we want it to have the mode of a register
  6435. extraction pattern's structure operand, or word_mode if there is no
  6436. such pattern. The same applies to extraction_mode and pos_mode
  6437. and their respective operands.
  6438. For memory, assume that the desired extraction_mode and pos_mode
  6439. are the same as for a register operation, since at present we don't
  6440. have named patterns for aligned memory structures. */
  6441. struct extraction_insn insn;
  6442. if (get_best_reg_extraction_insn (&insn, pattern,
  6443. GET_MODE_BITSIZE (inner_mode), mode))
  6444. {
  6445. wanted_inner_reg_mode = insn.struct_mode;
  6446. pos_mode = insn.pos_mode;
  6447. extraction_mode = insn.field_mode;
  6448. }
  6449. /* Never narrow an object, since that might not be safe. */
  6450. if (mode != VOIDmode
  6451. && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
  6452. extraction_mode = mode;
  6453. if (!MEM_P (inner))
  6454. wanted_inner_mode = wanted_inner_reg_mode;
  6455. else
  6456. {
  6457. /* Be careful not to go beyond the extracted object and maintain the
  6458. natural alignment of the memory. */
  6459. wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
  6460. while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
  6461. > GET_MODE_BITSIZE (wanted_inner_mode))
  6462. {
  6463. wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
  6464. gcc_assert (wanted_inner_mode != VOIDmode);
  6465. }
  6466. }
  6467. orig_pos = pos;
  6468. if (BITS_BIG_ENDIAN)
  6469. {
  6470. /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
  6471. BITS_BIG_ENDIAN style. If position is constant, compute new
  6472. position. Otherwise, build subtraction.
  6473. Note that POS is relative to the mode of the original argument.
  6474. If it's a MEM we need to recompute POS relative to that.
  6475. However, if we're extracting from (or inserting into) a register,
  6476. we want to recompute POS relative to wanted_inner_mode. */
  6477. int width = (MEM_P (inner)
  6478. ? GET_MODE_BITSIZE (is_mode)
  6479. : GET_MODE_BITSIZE (wanted_inner_mode));
  6480. if (pos_rtx == 0)
  6481. pos = width - len - pos;
  6482. else
  6483. pos_rtx
  6484. = gen_rtx_MINUS (GET_MODE (pos_rtx),
  6485. gen_int_mode (width - len, GET_MODE (pos_rtx)),
  6486. pos_rtx);
  6487. /* POS may be less than 0 now, but we check for that below.
  6488. Note that it can only be less than 0 if !MEM_P (inner). */
  6489. }
  6490. /* If INNER has a wider mode, and this is a constant extraction, try to
  6491. make it smaller and adjust the byte to point to the byte containing
  6492. the value. */
  6493. if (wanted_inner_mode != VOIDmode
  6494. && inner_mode != wanted_inner_mode
  6495. && ! pos_rtx
  6496. && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
  6497. && MEM_P (inner)
  6498. && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
  6499. && ! MEM_VOLATILE_P (inner))
  6500. {
  6501. int offset = 0;
  6502. /* The computations below will be correct if the machine is big
  6503. endian in both bits and bytes or little endian in bits and bytes.
  6504. If it is mixed, we must adjust. */
  6505. /* If bytes are big endian and we had a paradoxical SUBREG, we must
  6506. adjust OFFSET to compensate. */
  6507. if (BYTES_BIG_ENDIAN
  6508. && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
  6509. offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
  6510. /* We can now move to the desired byte. */
  6511. offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
  6512. * GET_MODE_SIZE (wanted_inner_mode);
  6513. pos %= GET_MODE_BITSIZE (wanted_inner_mode);
  6514. if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
  6515. && is_mode != wanted_inner_mode)
  6516. offset = (GET_MODE_SIZE (is_mode)
  6517. - GET_MODE_SIZE (wanted_inner_mode) - offset);
  6518. inner = adjust_address_nv (inner, wanted_inner_mode, offset);
  6519. }
  6520. /* If INNER is not memory, get it into the proper mode. If we are changing
  6521. its mode, POS must be a constant and smaller than the size of the new
  6522. mode. */
  6523. else if (!MEM_P (inner))
  6524. {
  6525. /* On the LHS, don't create paradoxical subregs implicitely truncating
  6526. the register unless TRULY_NOOP_TRUNCATION. */
  6527. if (in_dest
  6528. && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
  6529. wanted_inner_mode))
  6530. return NULL_RTX;
  6531. if (GET_MODE (inner) != wanted_inner_mode
  6532. && (pos_rtx != 0
  6533. || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
  6534. return NULL_RTX;
  6535. if (orig_pos < 0)
  6536. return NULL_RTX;
  6537. inner = force_to_mode (inner, wanted_inner_mode,
  6538. pos_rtx
  6539. || len + orig_pos >= HOST_BITS_PER_WIDE_INT
  6540. ? ~(unsigned HOST_WIDE_INT) 0
  6541. : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
  6542. << orig_pos),
  6543. 0);
  6544. }
  6545. /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
  6546. have to zero extend. Otherwise, we can just use a SUBREG. */
  6547. if (pos_rtx != 0
  6548. && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
  6549. {
  6550. rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
  6551. GET_MODE (pos_rtx));
  6552. /* If we know that no extraneous bits are set, and that the high
  6553. bit is not set, convert extraction to cheaper one - either
  6554. SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
  6555. cases. */
  6556. if (flag_expensive_optimizations
  6557. && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
  6558. && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
  6559. & ~(((unsigned HOST_WIDE_INT)
  6560. GET_MODE_MASK (GET_MODE (pos_rtx)))
  6561. >> 1))
  6562. == 0)))
  6563. {
  6564. rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
  6565. GET_MODE (pos_rtx));
  6566. /* Prefer ZERO_EXTENSION, since it gives more information to
  6567. backends. */
  6568. if (set_src_cost (temp1, optimize_this_for_speed_p)
  6569. < set_src_cost (temp, optimize_this_for_speed_p))
  6570. temp = temp1;
  6571. }
  6572. pos_rtx = temp;
  6573. }
  6574. /* Make POS_RTX unless we already have it and it is correct. If we don't
  6575. have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
  6576. be a CONST_INT. */
  6577. if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
  6578. pos_rtx = orig_pos_rtx;
  6579. else if (pos_rtx == 0)
  6580. pos_rtx = GEN_INT (pos);
  6581. /* Make the required operation. See if we can use existing rtx. */
  6582. new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
  6583. extraction_mode, inner, GEN_INT (len), pos_rtx);
  6584. if (! in_dest)
  6585. new_rtx = gen_lowpart (mode, new_rtx);
  6586. return new_rtx;
  6587. }
  6588. /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
  6589. with any other operations in X. Return X without that shift if so. */
  6590. static rtx
  6591. extract_left_shift (rtx x, int count)
  6592. {
  6593. enum rtx_code code = GET_CODE (x);
  6594. machine_mode mode = GET_MODE (x);
  6595. rtx tem;
  6596. switch (code)
  6597. {
  6598. case ASHIFT:
  6599. /* This is the shift itself. If it is wide enough, we will return
  6600. either the value being shifted if the shift count is equal to
  6601. COUNT or a shift for the difference. */
  6602. if (CONST_INT_P (XEXP (x, 1))
  6603. && INTVAL (XEXP (x, 1)) >= count)
  6604. return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
  6605. INTVAL (XEXP (x, 1)) - count);
  6606. break;
  6607. case NEG: case NOT:
  6608. if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
  6609. return simplify_gen_unary (code, mode, tem, mode);
  6610. break;
  6611. case PLUS: case IOR: case XOR: case AND:
  6612. /* If we can safely shift this constant and we find the inner shift,
  6613. make a new operation. */
  6614. if (CONST_INT_P (XEXP (x, 1))
  6615. && (UINTVAL (XEXP (x, 1))
  6616. & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
  6617. && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
  6618. {
  6619. HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
  6620. return simplify_gen_binary (code, mode, tem,
  6621. gen_int_mode (val, mode));
  6622. }
  6623. break;
  6624. default:
  6625. break;
  6626. }
  6627. return 0;
  6628. }
  6629. /* Look at the expression rooted at X. Look for expressions
  6630. equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
  6631. Form these expressions.
  6632. Return the new rtx, usually just X.
  6633. Also, for machines like the VAX that don't have logical shift insns,
  6634. try to convert logical to arithmetic shift operations in cases where
  6635. they are equivalent. This undoes the canonicalizations to logical
  6636. shifts done elsewhere.
  6637. We try, as much as possible, to re-use rtl expressions to save memory.
  6638. IN_CODE says what kind of expression we are processing. Normally, it is
  6639. SET. In a memory address (inside a MEM, PLUS or minus, the latter two
  6640. being kludges), it is MEM. When processing the arguments of a comparison
  6641. or a COMPARE against zero, it is COMPARE. */
  6642. rtx
  6643. make_compound_operation (rtx x, enum rtx_code in_code)
  6644. {
  6645. enum rtx_code code = GET_CODE (x);
  6646. machine_mode mode = GET_MODE (x);
  6647. int mode_width = GET_MODE_PRECISION (mode);
  6648. rtx rhs, lhs;
  6649. enum rtx_code next_code;
  6650. int i, j;
  6651. rtx new_rtx = 0;
  6652. rtx tem;
  6653. const char *fmt;
  6654. /* Select the code to be used in recursive calls. Once we are inside an
  6655. address, we stay there. If we have a comparison, set to COMPARE,
  6656. but once inside, go back to our default of SET. */
  6657. next_code = (code == MEM ? MEM
  6658. : ((code == PLUS || code == MINUS)
  6659. && SCALAR_INT_MODE_P (mode)) ? MEM
  6660. : ((code == COMPARE || COMPARISON_P (x))
  6661. && XEXP (x, 1) == const0_rtx) ? COMPARE
  6662. : in_code == COMPARE ? SET : in_code);
  6663. /* Process depending on the code of this operation. If NEW is set
  6664. nonzero, it will be returned. */
  6665. switch (code)
  6666. {
  6667. case ASHIFT:
  6668. /* Convert shifts by constants into multiplications if inside
  6669. an address. */
  6670. if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
  6671. && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
  6672. && INTVAL (XEXP (x, 1)) >= 0
  6673. && SCALAR_INT_MODE_P (mode))
  6674. {
  6675. HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
  6676. HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
  6677. new_rtx = make_compound_operation (XEXP (x, 0), next_code);
  6678. if (GET_CODE (new_rtx) == NEG)
  6679. {
  6680. new_rtx = XEXP (new_rtx, 0);
  6681. multval = -multval;
  6682. }
  6683. multval = trunc_int_for_mode (multval, mode);
  6684. new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
  6685. }
  6686. break;
  6687. case PLUS:
  6688. lhs = XEXP (x, 0);
  6689. rhs = XEXP (x, 1);
  6690. lhs = make_compound_operation (lhs, next_code);
  6691. rhs = make_compound_operation (rhs, next_code);
  6692. if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
  6693. && SCALAR_INT_MODE_P (mode))
  6694. {
  6695. tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
  6696. XEXP (lhs, 1));
  6697. new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
  6698. }
  6699. else if (GET_CODE (lhs) == MULT
  6700. && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
  6701. {
  6702. tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
  6703. simplify_gen_unary (NEG, mode,
  6704. XEXP (lhs, 1),
  6705. mode));
  6706. new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
  6707. }
  6708. else
  6709. {
  6710. SUBST (XEXP (x, 0), lhs);
  6711. SUBST (XEXP (x, 1), rhs);
  6712. goto maybe_swap;
  6713. }
  6714. x = gen_lowpart (mode, new_rtx);
  6715. goto maybe_swap;
  6716. case MINUS:
  6717. lhs = XEXP (x, 0);
  6718. rhs = XEXP (x, 1);
  6719. lhs = make_compound_operation (lhs, next_code);
  6720. rhs = make_compound_operation (rhs, next_code);
  6721. if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
  6722. && SCALAR_INT_MODE_P (mode))
  6723. {
  6724. tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
  6725. XEXP (rhs, 1));
  6726. new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
  6727. }
  6728. else if (GET_CODE (rhs) == MULT
  6729. && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
  6730. {
  6731. tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
  6732. simplify_gen_unary (NEG, mode,
  6733. XEXP (rhs, 1),
  6734. mode));
  6735. new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
  6736. }
  6737. else
  6738. {
  6739. SUBST (XEXP (x, 0), lhs);
  6740. SUBST (XEXP (x, 1), rhs);
  6741. return x;
  6742. }
  6743. return gen_lowpart (mode, new_rtx);
  6744. case AND:
  6745. /* If the second operand is not a constant, we can't do anything
  6746. with it. */
  6747. if (!CONST_INT_P (XEXP (x, 1)))
  6748. break;
  6749. /* If the constant is a power of two minus one and the first operand
  6750. is a logical right shift, make an extraction. */
  6751. if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
  6752. && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
  6753. {
  6754. new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
  6755. new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
  6756. 0, in_code == COMPARE);
  6757. }
  6758. /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
  6759. else if (GET_CODE (XEXP (x, 0)) == SUBREG
  6760. && subreg_lowpart_p (XEXP (x, 0))
  6761. && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
  6762. && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
  6763. {
  6764. new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
  6765. next_code);
  6766. new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
  6767. XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
  6768. 0, in_code == COMPARE);
  6769. }
  6770. /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
  6771. else if ((GET_CODE (XEXP (x, 0)) == XOR
  6772. || GET_CODE (XEXP (x, 0)) == IOR)
  6773. && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
  6774. && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
  6775. && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
  6776. {
  6777. /* Apply the distributive law, and then try to make extractions. */
  6778. new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
  6779. gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
  6780. XEXP (x, 1)),
  6781. gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
  6782. XEXP (x, 1)));
  6783. new_rtx = make_compound_operation (new_rtx, in_code);
  6784. }
  6785. /* If we are have (and (rotate X C) M) and C is larger than the number
  6786. of bits in M, this is an extraction. */
  6787. else if (GET_CODE (XEXP (x, 0)) == ROTATE
  6788. && CONST_INT_P (XEXP (XEXP (x, 0), 1))
  6789. && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
  6790. && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
  6791. {
  6792. new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
  6793. new_rtx = make_extraction (mode, new_rtx,
  6794. (GET_MODE_PRECISION (mode)
  6795. - INTVAL (XEXP (XEXP (x, 0), 1))),
  6796. NULL_RTX, i, 1, 0, in_code == COMPARE);
  6797. }
  6798. /* On machines without logical shifts, if the operand of the AND is
  6799. a logical shift and our mask turns off all the propagated sign
  6800. bits, we can replace the logical shift with an arithmetic shift. */
  6801. else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
  6802. && !have_insn_for (LSHIFTRT, mode)
  6803. && have_insn_for (ASHIFTRT, mode)
  6804. && CONST_INT_P (XEXP (XEXP (x, 0), 1))
  6805. && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
  6806. && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
  6807. && mode_width <= HOST_BITS_PER_WIDE_INT)
  6808. {
  6809. unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
  6810. mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
  6811. if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
  6812. SUBST (XEXP (x, 0),
  6813. gen_rtx_ASHIFTRT (mode,
  6814. make_compound_operation
  6815. (XEXP (XEXP (x, 0), 0), next_code),
  6816. XEXP (XEXP (x, 0), 1)));
  6817. }
  6818. /* If the constant is one less than a power of two, this might be
  6819. representable by an extraction even if no shift is present.
  6820. If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
  6821. we are in a COMPARE. */
  6822. else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
  6823. new_rtx = make_extraction (mode,
  6824. make_compound_operation (XEXP (x, 0),
  6825. next_code),
  6826. 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
  6827. /* If we are in a comparison and this is an AND with a power of two,
  6828. convert this into the appropriate bit extract. */
  6829. else if (in_code == COMPARE
  6830. && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
  6831. new_rtx = make_extraction (mode,
  6832. make_compound_operation (XEXP (x, 0),
  6833. next_code),
  6834. i, NULL_RTX, 1, 1, 0, 1);
  6835. break;
  6836. case LSHIFTRT:
  6837. /* If the sign bit is known to be zero, replace this with an
  6838. arithmetic shift. */
  6839. if (have_insn_for (ASHIFTRT, mode)
  6840. && ! have_insn_for (LSHIFTRT, mode)
  6841. && mode_width <= HOST_BITS_PER_WIDE_INT
  6842. && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
  6843. {
  6844. new_rtx = gen_rtx_ASHIFTRT (mode,
  6845. make_compound_operation (XEXP (x, 0),
  6846. next_code),
  6847. XEXP (x, 1));
  6848. break;
  6849. }
  6850. /* ... fall through ... */
  6851. case ASHIFTRT:
  6852. lhs = XEXP (x, 0);
  6853. rhs = XEXP (x, 1);
  6854. /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
  6855. this is a SIGN_EXTRACT. */
  6856. if (CONST_INT_P (rhs)
  6857. && GET_CODE (lhs) == ASHIFT
  6858. && CONST_INT_P (XEXP (lhs, 1))
  6859. && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
  6860. && INTVAL (XEXP (lhs, 1)) >= 0
  6861. && INTVAL (rhs) < mode_width)
  6862. {
  6863. new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
  6864. new_rtx = make_extraction (mode, new_rtx,
  6865. INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
  6866. NULL_RTX, mode_width - INTVAL (rhs),
  6867. code == LSHIFTRT, 0, in_code == COMPARE);
  6868. break;
  6869. }
  6870. /* See if we have operations between an ASHIFTRT and an ASHIFT.
  6871. If so, try to merge the shifts into a SIGN_EXTEND. We could
  6872. also do this for some cases of SIGN_EXTRACT, but it doesn't
  6873. seem worth the effort; the case checked for occurs on Alpha. */
  6874. if (!OBJECT_P (lhs)
  6875. && ! (GET_CODE (lhs) == SUBREG
  6876. && (OBJECT_P (SUBREG_REG (lhs))))
  6877. && CONST_INT_P (rhs)
  6878. && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
  6879. && INTVAL (rhs) < mode_width
  6880. && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
  6881. new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
  6882. 0, NULL_RTX, mode_width - INTVAL (rhs),
  6883. code == LSHIFTRT, 0, in_code == COMPARE);
  6884. break;
  6885. case SUBREG:
  6886. /* Call ourselves recursively on the inner expression. If we are
  6887. narrowing the object and it has a different RTL code from
  6888. what it originally did, do this SUBREG as a force_to_mode. */
  6889. {
  6890. rtx inner = SUBREG_REG (x), simplified;
  6891. enum rtx_code subreg_code = in_code;
  6892. /* If in_code is COMPARE, it isn't always safe to pass it through
  6893. to the recursive make_compound_operation call. */
  6894. if (subreg_code == COMPARE
  6895. && (!subreg_lowpart_p (x)
  6896. || GET_CODE (inner) == SUBREG
  6897. /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
  6898. is (const_int 0), rather than
  6899. (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
  6900. || (GET_CODE (inner) == AND
  6901. && CONST_INT_P (XEXP (inner, 1))
  6902. && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
  6903. && exact_log2 (UINTVAL (XEXP (inner, 1)))
  6904. >= GET_MODE_BITSIZE (mode))))
  6905. subreg_code = SET;
  6906. tem = make_compound_operation (inner, subreg_code);
  6907. simplified
  6908. = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
  6909. if (simplified)
  6910. tem = simplified;
  6911. if (GET_CODE (tem) != GET_CODE (inner)
  6912. && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
  6913. && subreg_lowpart_p (x))
  6914. {
  6915. rtx newer
  6916. = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
  6917. /* If we have something other than a SUBREG, we might have
  6918. done an expansion, so rerun ourselves. */
  6919. if (GET_CODE (newer) != SUBREG)
  6920. newer = make_compound_operation (newer, in_code);
  6921. /* force_to_mode can expand compounds. If it just re-expanded the
  6922. compound, use gen_lowpart to convert to the desired mode. */
  6923. if (rtx_equal_p (newer, x)
  6924. /* Likewise if it re-expanded the compound only partially.
  6925. This happens for SUBREG of ZERO_EXTRACT if they extract
  6926. the same number of bits. */
  6927. || (GET_CODE (newer) == SUBREG
  6928. && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
  6929. || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
  6930. && GET_CODE (inner) == AND
  6931. && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
  6932. return gen_lowpart (GET_MODE (x), tem);
  6933. return newer;
  6934. }
  6935. if (simplified)
  6936. return tem;
  6937. }
  6938. break;
  6939. default:
  6940. break;
  6941. }
  6942. if (new_rtx)
  6943. {
  6944. x = gen_lowpart (mode, new_rtx);
  6945. code = GET_CODE (x);
  6946. }
  6947. /* Now recursively process each operand of this operation. We need to
  6948. handle ZERO_EXTEND specially so that we don't lose track of the
  6949. inner mode. */
  6950. if (GET_CODE (x) == ZERO_EXTEND)
  6951. {
  6952. new_rtx = make_compound_operation (XEXP (x, 0), next_code);
  6953. tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
  6954. new_rtx, GET_MODE (XEXP (x, 0)));
  6955. if (tem)
  6956. return tem;
  6957. SUBST (XEXP (x, 0), new_rtx);
  6958. return x;
  6959. }
  6960. fmt = GET_RTX_FORMAT (code);
  6961. for (i = 0; i < GET_RTX_LENGTH (code); i++)
  6962. if (fmt[i] == 'e')
  6963. {
  6964. new_rtx = make_compound_operation (XEXP (x, i), next_code);
  6965. SUBST (XEXP (x, i), new_rtx);
  6966. }
  6967. else if (fmt[i] == 'E')
  6968. for (j = 0; j < XVECLEN (x, i); j++)
  6969. {
  6970. new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
  6971. SUBST (XVECEXP (x, i, j), new_rtx);
  6972. }
  6973. maybe_swap:
  6974. /* If this is a commutative operation, the changes to the operands
  6975. may have made it noncanonical. */
  6976. if (COMMUTATIVE_ARITH_P (x)
  6977. && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
  6978. {
  6979. tem = XEXP (x, 0);
  6980. SUBST (XEXP (x, 0), XEXP (x, 1));
  6981. SUBST (XEXP (x, 1), tem);
  6982. }
  6983. return x;
  6984. }
  6985. /* Given M see if it is a value that would select a field of bits
  6986. within an item, but not the entire word. Return -1 if not.
  6987. Otherwise, return the starting position of the field, where 0 is the
  6988. low-order bit.
  6989. *PLEN is set to the length of the field. */
  6990. static int
  6991. get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
  6992. {
  6993. /* Get the bit number of the first 1 bit from the right, -1 if none. */
  6994. int pos = m ? ctz_hwi (m) : -1;
  6995. int len = 0;
  6996. if (pos >= 0)
  6997. /* Now shift off the low-order zero bits and see if we have a
  6998. power of two minus 1. */
  6999. len = exact_log2 ((m >> pos) + 1);
  7000. if (len <= 0)
  7001. pos = -1;
  7002. *plen = len;
  7003. return pos;
  7004. }
  7005. /* If X refers to a register that equals REG in value, replace these
  7006. references with REG. */
  7007. static rtx
  7008. canon_reg_for_combine (rtx x, rtx reg)
  7009. {
  7010. rtx op0, op1, op2;
  7011. const char *fmt;
  7012. int i;
  7013. bool copied;
  7014. enum rtx_code code = GET_CODE (x);
  7015. switch (GET_RTX_CLASS (code))
  7016. {
  7017. case RTX_UNARY:
  7018. op0 = canon_reg_for_combine (XEXP (x, 0), reg);
  7019. if (op0 != XEXP (x, 0))
  7020. return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
  7021. GET_MODE (reg));
  7022. break;
  7023. case RTX_BIN_ARITH:
  7024. case RTX_COMM_ARITH:
  7025. op0 = canon_reg_for_combine (XEXP (x, 0), reg);
  7026. op1 = canon_reg_for_combine (XEXP (x, 1), reg);
  7027. if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  7028. return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
  7029. break;
  7030. case RTX_COMPARE:
  7031. case RTX_COMM_COMPARE:
  7032. op0 = canon_reg_for_combine (XEXP (x, 0), reg);
  7033. op1 = canon_reg_for_combine (XEXP (x, 1), reg);
  7034. if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  7035. return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
  7036. GET_MODE (op0), op0, op1);
  7037. break;
  7038. case RTX_TERNARY:
  7039. case RTX_BITFIELD_OPS:
  7040. op0 = canon_reg_for_combine (XEXP (x, 0), reg);
  7041. op1 = canon_reg_for_combine (XEXP (x, 1), reg);
  7042. op2 = canon_reg_for_combine (XEXP (x, 2), reg);
  7043. if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
  7044. return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
  7045. GET_MODE (op0), op0, op1, op2);
  7046. case RTX_OBJ:
  7047. if (REG_P (x))
  7048. {
  7049. if (rtx_equal_p (get_last_value (reg), x)
  7050. || rtx_equal_p (reg, get_last_value (x)))
  7051. return reg;
  7052. else
  7053. break;
  7054. }
  7055. /* fall through */
  7056. default:
  7057. fmt = GET_RTX_FORMAT (code);
  7058. copied = false;
  7059. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  7060. if (fmt[i] == 'e')
  7061. {
  7062. rtx op = canon_reg_for_combine (XEXP (x, i), reg);
  7063. if (op != XEXP (x, i))
  7064. {
  7065. if (!copied)
  7066. {
  7067. copied = true;
  7068. x = copy_rtx (x);
  7069. }
  7070. XEXP (x, i) = op;
  7071. }
  7072. }
  7073. else if (fmt[i] == 'E')
  7074. {
  7075. int j;
  7076. for (j = 0; j < XVECLEN (x, i); j++)
  7077. {
  7078. rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
  7079. if (op != XVECEXP (x, i, j))
  7080. {
  7081. if (!copied)
  7082. {
  7083. copied = true;
  7084. x = copy_rtx (x);
  7085. }
  7086. XVECEXP (x, i, j) = op;
  7087. }
  7088. }
  7089. }
  7090. break;
  7091. }
  7092. return x;
  7093. }
  7094. /* Return X converted to MODE. If the value is already truncated to
  7095. MODE we can just return a subreg even though in the general case we
  7096. would need an explicit truncation. */
  7097. static rtx
  7098. gen_lowpart_or_truncate (machine_mode mode, rtx x)
  7099. {
  7100. if (!CONST_INT_P (x)
  7101. && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
  7102. && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
  7103. && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
  7104. {
  7105. /* Bit-cast X into an integer mode. */
  7106. if (!SCALAR_INT_MODE_P (GET_MODE (x)))
  7107. x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
  7108. x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
  7109. x, GET_MODE (x));
  7110. }
  7111. return gen_lowpart (mode, x);
  7112. }
  7113. /* See if X can be simplified knowing that we will only refer to it in
  7114. MODE and will only refer to those bits that are nonzero in MASK.
  7115. If other bits are being computed or if masking operations are done
  7116. that select a superset of the bits in MASK, they can sometimes be
  7117. ignored.
  7118. Return a possibly simplified expression, but always convert X to
  7119. MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
  7120. If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
  7121. are all off in X. This is used when X will be complemented, by either
  7122. NOT, NEG, or XOR. */
  7123. static rtx
  7124. force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
  7125. int just_select)
  7126. {
  7127. enum rtx_code code = GET_CODE (x);
  7128. int next_select = just_select || code == XOR || code == NOT || code == NEG;
  7129. machine_mode op_mode;
  7130. unsigned HOST_WIDE_INT fuller_mask, nonzero;
  7131. rtx op0, op1, temp;
  7132. /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
  7133. code below will do the wrong thing since the mode of such an
  7134. expression is VOIDmode.
  7135. Also do nothing if X is a CLOBBER; this can happen if X was
  7136. the return value from a call to gen_lowpart. */
  7137. if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
  7138. return x;
  7139. /* We want to perform the operation in its present mode unless we know
  7140. that the operation is valid in MODE, in which case we do the operation
  7141. in MODE. */
  7142. op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
  7143. && have_insn_for (code, mode))
  7144. ? mode : GET_MODE (x));
  7145. /* It is not valid to do a right-shift in a narrower mode
  7146. than the one it came in with. */
  7147. if ((code == LSHIFTRT || code == ASHIFTRT)
  7148. && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
  7149. op_mode = GET_MODE (x);
  7150. /* Truncate MASK to fit OP_MODE. */
  7151. if (op_mode)
  7152. mask &= GET_MODE_MASK (op_mode);
  7153. /* When we have an arithmetic operation, or a shift whose count we
  7154. do not know, we need to assume that all bits up to the highest-order
  7155. bit in MASK will be needed. This is how we form such a mask. */
  7156. if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
  7157. fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
  7158. else
  7159. fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
  7160. - 1);
  7161. /* Determine what bits of X are guaranteed to be (non)zero. */
  7162. nonzero = nonzero_bits (x, mode);
  7163. /* If none of the bits in X are needed, return a zero. */
  7164. if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
  7165. x = const0_rtx;
  7166. /* If X is a CONST_INT, return a new one. Do this here since the
  7167. test below will fail. */
  7168. if (CONST_INT_P (x))
  7169. {
  7170. if (SCALAR_INT_MODE_P (mode))
  7171. return gen_int_mode (INTVAL (x) & mask, mode);
  7172. else
  7173. {
  7174. x = GEN_INT (INTVAL (x) & mask);
  7175. return gen_lowpart_common (mode, x);
  7176. }
  7177. }
  7178. /* If X is narrower than MODE and we want all the bits in X's mode, just
  7179. get X in the proper mode. */
  7180. if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
  7181. && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
  7182. return gen_lowpart (mode, x);
  7183. /* We can ignore the effect of a SUBREG if it narrows the mode or
  7184. if the constant masks to zero all the bits the mode doesn't have. */
  7185. if (GET_CODE (x) == SUBREG
  7186. && subreg_lowpart_p (x)
  7187. && ((GET_MODE_SIZE (GET_MODE (x))
  7188. < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
  7189. || (0 == (mask
  7190. & GET_MODE_MASK (GET_MODE (x))
  7191. & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
  7192. return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
  7193. /* The arithmetic simplifications here only work for scalar integer modes. */
  7194. if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
  7195. return gen_lowpart_or_truncate (mode, x);
  7196. switch (code)
  7197. {
  7198. case CLOBBER:
  7199. /* If X is a (clobber (const_int)), return it since we know we are
  7200. generating something that won't match. */
  7201. return x;
  7202. case SIGN_EXTEND:
  7203. case ZERO_EXTEND:
  7204. case ZERO_EXTRACT:
  7205. case SIGN_EXTRACT:
  7206. x = expand_compound_operation (x);
  7207. if (GET_CODE (x) != code)
  7208. return force_to_mode (x, mode, mask, next_select);
  7209. break;
  7210. case TRUNCATE:
  7211. /* Similarly for a truncate. */
  7212. return force_to_mode (XEXP (x, 0), mode, mask, next_select);
  7213. case AND:
  7214. /* If this is an AND with a constant, convert it into an AND
  7215. whose constant is the AND of that constant with MASK. If it
  7216. remains an AND of MASK, delete it since it is redundant. */
  7217. if (CONST_INT_P (XEXP (x, 1)))
  7218. {
  7219. x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
  7220. mask & INTVAL (XEXP (x, 1)));
  7221. /* If X is still an AND, see if it is an AND with a mask that
  7222. is just some low-order bits. If so, and it is MASK, we don't
  7223. need it. */
  7224. if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
  7225. && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
  7226. == mask))
  7227. x = XEXP (x, 0);
  7228. /* If it remains an AND, try making another AND with the bits
  7229. in the mode mask that aren't in MASK turned on. If the
  7230. constant in the AND is wide enough, this might make a
  7231. cheaper constant. */
  7232. if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
  7233. && GET_MODE_MASK (GET_MODE (x)) != mask
  7234. && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
  7235. {
  7236. unsigned HOST_WIDE_INT cval
  7237. = UINTVAL (XEXP (x, 1))
  7238. | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
  7239. rtx y;
  7240. y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
  7241. gen_int_mode (cval, GET_MODE (x)));
  7242. if (set_src_cost (y, optimize_this_for_speed_p)
  7243. < set_src_cost (x, optimize_this_for_speed_p))
  7244. x = y;
  7245. }
  7246. break;
  7247. }
  7248. goto binop;
  7249. case PLUS:
  7250. /* In (and (plus FOO C1) M), if M is a mask that just turns off
  7251. low-order bits (as in an alignment operation) and FOO is already
  7252. aligned to that boundary, mask C1 to that boundary as well.
  7253. This may eliminate that PLUS and, later, the AND. */
  7254. {
  7255. unsigned int width = GET_MODE_PRECISION (mode);
  7256. unsigned HOST_WIDE_INT smask = mask;
  7257. /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
  7258. number, sign extend it. */
  7259. if (width < HOST_BITS_PER_WIDE_INT
  7260. && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
  7261. smask |= HOST_WIDE_INT_M1U << width;
  7262. if (CONST_INT_P (XEXP (x, 1))
  7263. && exact_log2 (- smask) >= 0
  7264. && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
  7265. && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
  7266. return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
  7267. (INTVAL (XEXP (x, 1)) & smask)),
  7268. mode, smask, next_select);
  7269. }
  7270. /* ... fall through ... */
  7271. case MULT:
  7272. /* For PLUS, MINUS and MULT, we need any bits less significant than the
  7273. most significant bit in MASK since carries from those bits will
  7274. affect the bits we are interested in. */
  7275. mask = fuller_mask;
  7276. goto binop;
  7277. case MINUS:
  7278. /* If X is (minus C Y) where C's least set bit is larger than any bit
  7279. in the mask, then we may replace with (neg Y). */
  7280. if (CONST_INT_P (XEXP (x, 0))
  7281. && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
  7282. {
  7283. x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
  7284. GET_MODE (x));
  7285. return force_to_mode (x, mode, mask, next_select);
  7286. }
  7287. /* Similarly, if C contains every bit in the fuller_mask, then we may
  7288. replace with (not Y). */
  7289. if (CONST_INT_P (XEXP (x, 0))
  7290. && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
  7291. {
  7292. x = simplify_gen_unary (NOT, GET_MODE (x),
  7293. XEXP (x, 1), GET_MODE (x));
  7294. return force_to_mode (x, mode, mask, next_select);
  7295. }
  7296. mask = fuller_mask;
  7297. goto binop;
  7298. case IOR:
  7299. case XOR:
  7300. /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
  7301. LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
  7302. operation which may be a bitfield extraction. Ensure that the
  7303. constant we form is not wider than the mode of X. */
  7304. if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
  7305. && CONST_INT_P (XEXP (XEXP (x, 0), 1))
  7306. && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
  7307. && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
  7308. && CONST_INT_P (XEXP (x, 1))
  7309. && ((INTVAL (XEXP (XEXP (x, 0), 1))
  7310. + floor_log2 (INTVAL (XEXP (x, 1))))
  7311. < GET_MODE_PRECISION (GET_MODE (x)))
  7312. && (UINTVAL (XEXP (x, 1))
  7313. & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
  7314. {
  7315. temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
  7316. << INTVAL (XEXP (XEXP (x, 0), 1)),
  7317. GET_MODE (x));
  7318. temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
  7319. XEXP (XEXP (x, 0), 0), temp);
  7320. x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
  7321. XEXP (XEXP (x, 0), 1));
  7322. return force_to_mode (x, mode, mask, next_select);
  7323. }
  7324. binop:
  7325. /* For most binary operations, just propagate into the operation and
  7326. change the mode if we have an operation of that mode. */
  7327. op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
  7328. op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
  7329. /* If we ended up truncating both operands, truncate the result of the
  7330. operation instead. */
  7331. if (GET_CODE (op0) == TRUNCATE
  7332. && GET_CODE (op1) == TRUNCATE)
  7333. {
  7334. op0 = XEXP (op0, 0);
  7335. op1 = XEXP (op1, 0);
  7336. }
  7337. op0 = gen_lowpart_or_truncate (op_mode, op0);
  7338. op1 = gen_lowpart_or_truncate (op_mode, op1);
  7339. if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  7340. x = simplify_gen_binary (code, op_mode, op0, op1);
  7341. break;
  7342. case ASHIFT:
  7343. /* For left shifts, do the same, but just for the first operand.
  7344. However, we cannot do anything with shifts where we cannot
  7345. guarantee that the counts are smaller than the size of the mode
  7346. because such a count will have a different meaning in a
  7347. wider mode. */
  7348. if (! (CONST_INT_P (XEXP (x, 1))
  7349. && INTVAL (XEXP (x, 1)) >= 0
  7350. && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
  7351. && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
  7352. && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
  7353. < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
  7354. break;
  7355. /* If the shift count is a constant and we can do arithmetic in
  7356. the mode of the shift, refine which bits we need. Otherwise, use the
  7357. conservative form of the mask. */
  7358. if (CONST_INT_P (XEXP (x, 1))
  7359. && INTVAL (XEXP (x, 1)) >= 0
  7360. && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
  7361. && HWI_COMPUTABLE_MODE_P (op_mode))
  7362. mask >>= INTVAL (XEXP (x, 1));
  7363. else
  7364. mask = fuller_mask;
  7365. op0 = gen_lowpart_or_truncate (op_mode,
  7366. force_to_mode (XEXP (x, 0), op_mode,
  7367. mask, next_select));
  7368. if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
  7369. x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
  7370. break;
  7371. case LSHIFTRT:
  7372. /* Here we can only do something if the shift count is a constant,
  7373. this shift constant is valid for the host, and we can do arithmetic
  7374. in OP_MODE. */
  7375. if (CONST_INT_P (XEXP (x, 1))
  7376. && INTVAL (XEXP (x, 1)) >= 0
  7377. && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
  7378. && HWI_COMPUTABLE_MODE_P (op_mode))
  7379. {
  7380. rtx inner = XEXP (x, 0);
  7381. unsigned HOST_WIDE_INT inner_mask;
  7382. /* Select the mask of the bits we need for the shift operand. */
  7383. inner_mask = mask << INTVAL (XEXP (x, 1));
  7384. /* We can only change the mode of the shift if we can do arithmetic
  7385. in the mode of the shift and INNER_MASK is no wider than the
  7386. width of X's mode. */
  7387. if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
  7388. op_mode = GET_MODE (x);
  7389. inner = force_to_mode (inner, op_mode, inner_mask, next_select);
  7390. if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
  7391. x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
  7392. }
  7393. /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
  7394. shift and AND produces only copies of the sign bit (C2 is one less
  7395. than a power of two), we can do this with just a shift. */
  7396. if (GET_CODE (x) == LSHIFTRT
  7397. && CONST_INT_P (XEXP (x, 1))
  7398. /* The shift puts one of the sign bit copies in the least significant
  7399. bit. */
  7400. && ((INTVAL (XEXP (x, 1))
  7401. + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
  7402. >= GET_MODE_PRECISION (GET_MODE (x)))
  7403. && exact_log2 (mask + 1) >= 0
  7404. /* Number of bits left after the shift must be more than the mask
  7405. needs. */
  7406. && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
  7407. <= GET_MODE_PRECISION (GET_MODE (x)))
  7408. /* Must be more sign bit copies than the mask needs. */
  7409. && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
  7410. >= exact_log2 (mask + 1)))
  7411. x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
  7412. GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
  7413. - exact_log2 (mask + 1)));
  7414. goto shiftrt;
  7415. case ASHIFTRT:
  7416. /* If we are just looking for the sign bit, we don't need this shift at
  7417. all, even if it has a variable count. */
  7418. if (val_signbit_p (GET_MODE (x), mask))
  7419. return force_to_mode (XEXP (x, 0), mode, mask, next_select);
  7420. /* If this is a shift by a constant, get a mask that contains those bits
  7421. that are not copies of the sign bit. We then have two cases: If
  7422. MASK only includes those bits, this can be a logical shift, which may
  7423. allow simplifications. If MASK is a single-bit field not within
  7424. those bits, we are requesting a copy of the sign bit and hence can
  7425. shift the sign bit to the appropriate location. */
  7426. if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
  7427. && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
  7428. {
  7429. int i;
  7430. /* If the considered data is wider than HOST_WIDE_INT, we can't
  7431. represent a mask for all its bits in a single scalar.
  7432. But we only care about the lower bits, so calculate these. */
  7433. if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
  7434. {
  7435. nonzero = ~(unsigned HOST_WIDE_INT) 0;
  7436. /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
  7437. is the number of bits a full-width mask would have set.
  7438. We need only shift if these are fewer than nonzero can
  7439. hold. If not, we must keep all bits set in nonzero. */
  7440. if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
  7441. < HOST_BITS_PER_WIDE_INT)
  7442. nonzero >>= INTVAL (XEXP (x, 1))
  7443. + HOST_BITS_PER_WIDE_INT
  7444. - GET_MODE_PRECISION (GET_MODE (x)) ;
  7445. }
  7446. else
  7447. {
  7448. nonzero = GET_MODE_MASK (GET_MODE (x));
  7449. nonzero >>= INTVAL (XEXP (x, 1));
  7450. }
  7451. if ((mask & ~nonzero) == 0)
  7452. {
  7453. x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
  7454. XEXP (x, 0), INTVAL (XEXP (x, 1)));
  7455. if (GET_CODE (x) != ASHIFTRT)
  7456. return force_to_mode (x, mode, mask, next_select);
  7457. }
  7458. else if ((i = exact_log2 (mask)) >= 0)
  7459. {
  7460. x = simplify_shift_const
  7461. (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
  7462. GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
  7463. if (GET_CODE (x) != ASHIFTRT)
  7464. return force_to_mode (x, mode, mask, next_select);
  7465. }
  7466. }
  7467. /* If MASK is 1, convert this to an LSHIFTRT. This can be done
  7468. even if the shift count isn't a constant. */
  7469. if (mask == 1)
  7470. x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
  7471. XEXP (x, 0), XEXP (x, 1));
  7472. shiftrt:
  7473. /* If this is a zero- or sign-extension operation that just affects bits
  7474. we don't care about, remove it. Be sure the call above returned
  7475. something that is still a shift. */
  7476. if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
  7477. && CONST_INT_P (XEXP (x, 1))
  7478. && INTVAL (XEXP (x, 1)) >= 0
  7479. && (INTVAL (XEXP (x, 1))
  7480. <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
  7481. && GET_CODE (XEXP (x, 0)) == ASHIFT
  7482. && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
  7483. return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
  7484. next_select);
  7485. break;
  7486. case ROTATE:
  7487. case ROTATERT:
  7488. /* If the shift count is constant and we can do computations
  7489. in the mode of X, compute where the bits we care about are.
  7490. Otherwise, we can't do anything. Don't change the mode of
  7491. the shift or propagate MODE into the shift, though. */
  7492. if (CONST_INT_P (XEXP (x, 1))
  7493. && INTVAL (XEXP (x, 1)) >= 0)
  7494. {
  7495. temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
  7496. GET_MODE (x),
  7497. gen_int_mode (mask, GET_MODE (x)),
  7498. XEXP (x, 1));
  7499. if (temp && CONST_INT_P (temp))
  7500. x = simplify_gen_binary (code, GET_MODE (x),
  7501. force_to_mode (XEXP (x, 0), GET_MODE (x),
  7502. INTVAL (temp), next_select),
  7503. XEXP (x, 1));
  7504. }
  7505. break;
  7506. case NEG:
  7507. /* If we just want the low-order bit, the NEG isn't needed since it
  7508. won't change the low-order bit. */
  7509. if (mask == 1)
  7510. return force_to_mode (XEXP (x, 0), mode, mask, just_select);
  7511. /* We need any bits less significant than the most significant bit in
  7512. MASK since carries from those bits will affect the bits we are
  7513. interested in. */
  7514. mask = fuller_mask;
  7515. goto unop;
  7516. case NOT:
  7517. /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
  7518. same as the XOR case above. Ensure that the constant we form is not
  7519. wider than the mode of X. */
  7520. if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
  7521. && CONST_INT_P (XEXP (XEXP (x, 0), 1))
  7522. && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
  7523. && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
  7524. < GET_MODE_PRECISION (GET_MODE (x)))
  7525. && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
  7526. {
  7527. temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
  7528. GET_MODE (x));
  7529. temp = simplify_gen_binary (XOR, GET_MODE (x),
  7530. XEXP (XEXP (x, 0), 0), temp);
  7531. x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
  7532. temp, XEXP (XEXP (x, 0), 1));
  7533. return force_to_mode (x, mode, mask, next_select);
  7534. }
  7535. /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
  7536. use the full mask inside the NOT. */
  7537. mask = fuller_mask;
  7538. unop:
  7539. op0 = gen_lowpart_or_truncate (op_mode,
  7540. force_to_mode (XEXP (x, 0), mode, mask,
  7541. next_select));
  7542. if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
  7543. x = simplify_gen_unary (code, op_mode, op0, op_mode);
  7544. break;
  7545. case NE:
  7546. /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
  7547. in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
  7548. which is equal to STORE_FLAG_VALUE. */
  7549. if ((mask & ~STORE_FLAG_VALUE) == 0
  7550. && XEXP (x, 1) == const0_rtx
  7551. && GET_MODE (XEXP (x, 0)) == mode
  7552. && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
  7553. && (nonzero_bits (XEXP (x, 0), mode)
  7554. == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
  7555. return force_to_mode (XEXP (x, 0), mode, mask, next_select);
  7556. break;
  7557. case IF_THEN_ELSE:
  7558. /* We have no way of knowing if the IF_THEN_ELSE can itself be
  7559. written in a narrower mode. We play it safe and do not do so. */
  7560. op0 = gen_lowpart_or_truncate (GET_MODE (x),
  7561. force_to_mode (XEXP (x, 1), mode,
  7562. mask, next_select));
  7563. op1 = gen_lowpart_or_truncate (GET_MODE (x),
  7564. force_to_mode (XEXP (x, 2), mode,
  7565. mask, next_select));
  7566. if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
  7567. x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
  7568. GET_MODE (XEXP (x, 0)), XEXP (x, 0),
  7569. op0, op1);
  7570. break;
  7571. default:
  7572. break;
  7573. }
  7574. /* Ensure we return a value of the proper mode. */
  7575. return gen_lowpart_or_truncate (mode, x);
  7576. }
  7577. /* Return nonzero if X is an expression that has one of two values depending on
  7578. whether some other value is zero or nonzero. In that case, we return the
  7579. value that is being tested, *PTRUE is set to the value if the rtx being
  7580. returned has a nonzero value, and *PFALSE is set to the other alternative.
  7581. If we return zero, we set *PTRUE and *PFALSE to X. */
  7582. static rtx
  7583. if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
  7584. {
  7585. machine_mode mode = GET_MODE (x);
  7586. enum rtx_code code = GET_CODE (x);
  7587. rtx cond0, cond1, true0, true1, false0, false1;
  7588. unsigned HOST_WIDE_INT nz;
  7589. /* If we are comparing a value against zero, we are done. */
  7590. if ((code == NE || code == EQ)
  7591. && XEXP (x, 1) == const0_rtx)
  7592. {
  7593. *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
  7594. *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
  7595. return XEXP (x, 0);
  7596. }
  7597. /* If this is a unary operation whose operand has one of two values, apply
  7598. our opcode to compute those values. */
  7599. else if (UNARY_P (x)
  7600. && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
  7601. {
  7602. *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
  7603. *pfalse = simplify_gen_unary (code, mode, false0,
  7604. GET_MODE (XEXP (x, 0)));
  7605. return cond0;
  7606. }
  7607. /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
  7608. make can't possibly match and would suppress other optimizations. */
  7609. else if (code == COMPARE)
  7610. ;
  7611. /* If this is a binary operation, see if either side has only one of two
  7612. values. If either one does or if both do and they are conditional on
  7613. the same value, compute the new true and false values. */
  7614. else if (BINARY_P (x))
  7615. {
  7616. cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
  7617. cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
  7618. if ((cond0 != 0 || cond1 != 0)
  7619. && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
  7620. {
  7621. /* If if_then_else_cond returned zero, then true/false are the
  7622. same rtl. We must copy one of them to prevent invalid rtl
  7623. sharing. */
  7624. if (cond0 == 0)
  7625. true0 = copy_rtx (true0);
  7626. else if (cond1 == 0)
  7627. true1 = copy_rtx (true1);
  7628. if (COMPARISON_P (x))
  7629. {
  7630. *ptrue = simplify_gen_relational (code, mode, VOIDmode,
  7631. true0, true1);
  7632. *pfalse = simplify_gen_relational (code, mode, VOIDmode,
  7633. false0, false1);
  7634. }
  7635. else
  7636. {
  7637. *ptrue = simplify_gen_binary (code, mode, true0, true1);
  7638. *pfalse = simplify_gen_binary (code, mode, false0, false1);
  7639. }
  7640. return cond0 ? cond0 : cond1;
  7641. }
  7642. /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
  7643. operands is zero when the other is nonzero, and vice-versa,
  7644. and STORE_FLAG_VALUE is 1 or -1. */
  7645. if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
  7646. && (code == PLUS || code == IOR || code == XOR || code == MINUS
  7647. || code == UMAX)
  7648. && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
  7649. {
  7650. rtx op0 = XEXP (XEXP (x, 0), 1);
  7651. rtx op1 = XEXP (XEXP (x, 1), 1);
  7652. cond0 = XEXP (XEXP (x, 0), 0);
  7653. cond1 = XEXP (XEXP (x, 1), 0);
  7654. if (COMPARISON_P (cond0)
  7655. && COMPARISON_P (cond1)
  7656. && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
  7657. && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
  7658. && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
  7659. || ((swap_condition (GET_CODE (cond0))
  7660. == reversed_comparison_code (cond1, NULL))
  7661. && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
  7662. && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
  7663. && ! side_effects_p (x))
  7664. {
  7665. *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
  7666. *pfalse = simplify_gen_binary (MULT, mode,
  7667. (code == MINUS
  7668. ? simplify_gen_unary (NEG, mode,
  7669. op1, mode)
  7670. : op1),
  7671. const_true_rtx);
  7672. return cond0;
  7673. }
  7674. }
  7675. /* Similarly for MULT, AND and UMIN, except that for these the result
  7676. is always zero. */
  7677. if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
  7678. && (code == MULT || code == AND || code == UMIN)
  7679. && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
  7680. {
  7681. cond0 = XEXP (XEXP (x, 0), 0);
  7682. cond1 = XEXP (XEXP (x, 1), 0);
  7683. if (COMPARISON_P (cond0)
  7684. && COMPARISON_P (cond1)
  7685. && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
  7686. && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
  7687. && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
  7688. || ((swap_condition (GET_CODE (cond0))
  7689. == reversed_comparison_code (cond1, NULL))
  7690. && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
  7691. && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
  7692. && ! side_effects_p (x))
  7693. {
  7694. *ptrue = *pfalse = const0_rtx;
  7695. return cond0;
  7696. }
  7697. }
  7698. }
  7699. else if (code == IF_THEN_ELSE)
  7700. {
  7701. /* If we have IF_THEN_ELSE already, extract the condition and
  7702. canonicalize it if it is NE or EQ. */
  7703. cond0 = XEXP (x, 0);
  7704. *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
  7705. if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
  7706. return XEXP (cond0, 0);
  7707. else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
  7708. {
  7709. *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
  7710. return XEXP (cond0, 0);
  7711. }
  7712. else
  7713. return cond0;
  7714. }
  7715. /* If X is a SUBREG, we can narrow both the true and false values
  7716. if the inner expression, if there is a condition. */
  7717. else if (code == SUBREG
  7718. && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
  7719. &true0, &false0)))
  7720. {
  7721. true0 = simplify_gen_subreg (mode, true0,
  7722. GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
  7723. false0 = simplify_gen_subreg (mode, false0,
  7724. GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
  7725. if (true0 && false0)
  7726. {
  7727. *ptrue = true0;
  7728. *pfalse = false0;
  7729. return cond0;
  7730. }
  7731. }
  7732. /* If X is a constant, this isn't special and will cause confusions
  7733. if we treat it as such. Likewise if it is equivalent to a constant. */
  7734. else if (CONSTANT_P (x)
  7735. || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
  7736. ;
  7737. /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
  7738. will be least confusing to the rest of the compiler. */
  7739. else if (mode == BImode)
  7740. {
  7741. *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
  7742. return x;
  7743. }
  7744. /* If X is known to be either 0 or -1, those are the true and
  7745. false values when testing X. */
  7746. else if (x == constm1_rtx || x == const0_rtx
  7747. || (mode != VOIDmode
  7748. && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
  7749. {
  7750. *ptrue = constm1_rtx, *pfalse = const0_rtx;
  7751. return x;
  7752. }
  7753. /* Likewise for 0 or a single bit. */
  7754. else if (HWI_COMPUTABLE_MODE_P (mode)
  7755. && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
  7756. {
  7757. *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
  7758. return x;
  7759. }
  7760. /* Otherwise fail; show no condition with true and false values the same. */
  7761. *ptrue = *pfalse = x;
  7762. return 0;
  7763. }
  7764. /* Return the value of expression X given the fact that condition COND
  7765. is known to be true when applied to REG as its first operand and VAL
  7766. as its second. X is known to not be shared and so can be modified in
  7767. place.
  7768. We only handle the simplest cases, and specifically those cases that
  7769. arise with IF_THEN_ELSE expressions. */
  7770. static rtx
  7771. known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
  7772. {
  7773. enum rtx_code code = GET_CODE (x);
  7774. rtx temp;
  7775. const char *fmt;
  7776. int i, j;
  7777. if (side_effects_p (x))
  7778. return x;
  7779. /* If either operand of the condition is a floating point value,
  7780. then we have to avoid collapsing an EQ comparison. */
  7781. if (cond == EQ
  7782. && rtx_equal_p (x, reg)
  7783. && ! FLOAT_MODE_P (GET_MODE (x))
  7784. && ! FLOAT_MODE_P (GET_MODE (val)))
  7785. return val;
  7786. if (cond == UNEQ && rtx_equal_p (x, reg))
  7787. return val;
  7788. /* If X is (abs REG) and we know something about REG's relationship
  7789. with zero, we may be able to simplify this. */
  7790. if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
  7791. switch (cond)
  7792. {
  7793. case GE: case GT: case EQ:
  7794. return XEXP (x, 0);
  7795. case LT: case LE:
  7796. return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
  7797. XEXP (x, 0),
  7798. GET_MODE (XEXP (x, 0)));
  7799. default:
  7800. break;
  7801. }
  7802. /* The only other cases we handle are MIN, MAX, and comparisons if the
  7803. operands are the same as REG and VAL. */
  7804. else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
  7805. {
  7806. if (rtx_equal_p (XEXP (x, 0), val))
  7807. cond = swap_condition (cond), temp = val, val = reg, reg = temp;
  7808. if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
  7809. {
  7810. if (COMPARISON_P (x))
  7811. {
  7812. if (comparison_dominates_p (cond, code))
  7813. return const_true_rtx;
  7814. code = reversed_comparison_code (x, NULL);
  7815. if (code != UNKNOWN
  7816. && comparison_dominates_p (cond, code))
  7817. return const0_rtx;
  7818. else
  7819. return x;
  7820. }
  7821. else if (code == SMAX || code == SMIN
  7822. || code == UMIN || code == UMAX)
  7823. {
  7824. int unsignedp = (code == UMIN || code == UMAX);
  7825. /* Do not reverse the condition when it is NE or EQ.
  7826. This is because we cannot conclude anything about
  7827. the value of 'SMAX (x, y)' when x is not equal to y,
  7828. but we can when x equals y. */
  7829. if ((code == SMAX || code == UMAX)
  7830. && ! (cond == EQ || cond == NE))
  7831. cond = reverse_condition (cond);
  7832. switch (cond)
  7833. {
  7834. case GE: case GT:
  7835. return unsignedp ? x : XEXP (x, 1);
  7836. case LE: case LT:
  7837. return unsignedp ? x : XEXP (x, 0);
  7838. case GEU: case GTU:
  7839. return unsignedp ? XEXP (x, 1) : x;
  7840. case LEU: case LTU:
  7841. return unsignedp ? XEXP (x, 0) : x;
  7842. default:
  7843. break;
  7844. }
  7845. }
  7846. }
  7847. }
  7848. else if (code == SUBREG)
  7849. {
  7850. machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
  7851. rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
  7852. if (SUBREG_REG (x) != r)
  7853. {
  7854. /* We must simplify subreg here, before we lose track of the
  7855. original inner_mode. */
  7856. new_rtx = simplify_subreg (GET_MODE (x), r,
  7857. inner_mode, SUBREG_BYTE (x));
  7858. if (new_rtx)
  7859. return new_rtx;
  7860. else
  7861. SUBST (SUBREG_REG (x), r);
  7862. }
  7863. return x;
  7864. }
  7865. /* We don't have to handle SIGN_EXTEND here, because even in the
  7866. case of replacing something with a modeless CONST_INT, a
  7867. CONST_INT is already (supposed to be) a valid sign extension for
  7868. its narrower mode, which implies it's already properly
  7869. sign-extended for the wider mode. Now, for ZERO_EXTEND, the
  7870. story is different. */
  7871. else if (code == ZERO_EXTEND)
  7872. {
  7873. machine_mode inner_mode = GET_MODE (XEXP (x, 0));
  7874. rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
  7875. if (XEXP (x, 0) != r)
  7876. {
  7877. /* We must simplify the zero_extend here, before we lose
  7878. track of the original inner_mode. */
  7879. new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
  7880. r, inner_mode);
  7881. if (new_rtx)
  7882. return new_rtx;
  7883. else
  7884. SUBST (XEXP (x, 0), r);
  7885. }
  7886. return x;
  7887. }
  7888. fmt = GET_RTX_FORMAT (code);
  7889. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  7890. {
  7891. if (fmt[i] == 'e')
  7892. SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
  7893. else if (fmt[i] == 'E')
  7894. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  7895. SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
  7896. cond, reg, val));
  7897. }
  7898. return x;
  7899. }
  7900. /* See if X and Y are equal for the purposes of seeing if we can rewrite an
  7901. assignment as a field assignment. */
  7902. static int
  7903. rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
  7904. {
  7905. if (widen_x && GET_MODE (x) != GET_MODE (y))
  7906. {
  7907. if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (y)))
  7908. return 0;
  7909. if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
  7910. return 0;
  7911. /* For big endian, adjust the memory offset. */
  7912. if (BYTES_BIG_ENDIAN)
  7913. x = adjust_address_nv (x, GET_MODE (y),
  7914. -subreg_lowpart_offset (GET_MODE (x),
  7915. GET_MODE (y)));
  7916. else
  7917. x = adjust_address_nv (x, GET_MODE (y), 0);
  7918. }
  7919. if (x == y || rtx_equal_p (x, y))
  7920. return 1;
  7921. if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
  7922. return 0;
  7923. /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
  7924. Note that all SUBREGs of MEM are paradoxical; otherwise they
  7925. would have been rewritten. */
  7926. if (MEM_P (x) && GET_CODE (y) == SUBREG
  7927. && MEM_P (SUBREG_REG (y))
  7928. && rtx_equal_p (SUBREG_REG (y),
  7929. gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
  7930. return 1;
  7931. if (MEM_P (y) && GET_CODE (x) == SUBREG
  7932. && MEM_P (SUBREG_REG (x))
  7933. && rtx_equal_p (SUBREG_REG (x),
  7934. gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
  7935. return 1;
  7936. /* We used to see if get_last_value of X and Y were the same but that's
  7937. not correct. In one direction, we'll cause the assignment to have
  7938. the wrong destination and in the case, we'll import a register into this
  7939. insn that might have already have been dead. So fail if none of the
  7940. above cases are true. */
  7941. return 0;
  7942. }
  7943. /* See if X, a SET operation, can be rewritten as a bit-field assignment.
  7944. Return that assignment if so.
  7945. We only handle the most common cases. */
  7946. static rtx
  7947. make_field_assignment (rtx x)
  7948. {
  7949. rtx dest = SET_DEST (x);
  7950. rtx src = SET_SRC (x);
  7951. rtx assign;
  7952. rtx rhs, lhs;
  7953. HOST_WIDE_INT c1;
  7954. HOST_WIDE_INT pos;
  7955. unsigned HOST_WIDE_INT len;
  7956. rtx other;
  7957. machine_mode mode;
  7958. /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
  7959. a clear of a one-bit field. We will have changed it to
  7960. (and (rotate (const_int -2) POS) DEST), so check for that. Also check
  7961. for a SUBREG. */
  7962. if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
  7963. && CONST_INT_P (XEXP (XEXP (src, 0), 0))
  7964. && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
  7965. && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
  7966. {
  7967. assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
  7968. 1, 1, 1, 0);
  7969. if (assign != 0)
  7970. return gen_rtx_SET (VOIDmode, assign, const0_rtx);
  7971. return x;
  7972. }
  7973. if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
  7974. && subreg_lowpart_p (XEXP (src, 0))
  7975. && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
  7976. < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
  7977. && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
  7978. && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
  7979. && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
  7980. && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
  7981. {
  7982. assign = make_extraction (VOIDmode, dest, 0,
  7983. XEXP (SUBREG_REG (XEXP (src, 0)), 1),
  7984. 1, 1, 1, 0);
  7985. if (assign != 0)
  7986. return gen_rtx_SET (VOIDmode, assign, const0_rtx);
  7987. return x;
  7988. }
  7989. /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
  7990. one-bit field. */
  7991. if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
  7992. && XEXP (XEXP (src, 0), 0) == const1_rtx
  7993. && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
  7994. {
  7995. assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
  7996. 1, 1, 1, 0);
  7997. if (assign != 0)
  7998. return gen_rtx_SET (VOIDmode, assign, const1_rtx);
  7999. return x;
  8000. }
  8001. /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
  8002. SRC is an AND with all bits of that field set, then we can discard
  8003. the AND. */
  8004. if (GET_CODE (dest) == ZERO_EXTRACT
  8005. && CONST_INT_P (XEXP (dest, 1))
  8006. && GET_CODE (src) == AND
  8007. && CONST_INT_P (XEXP (src, 1)))
  8008. {
  8009. HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
  8010. unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
  8011. unsigned HOST_WIDE_INT ze_mask;
  8012. if (width >= HOST_BITS_PER_WIDE_INT)
  8013. ze_mask = -1;
  8014. else
  8015. ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
  8016. /* Complete overlap. We can remove the source AND. */
  8017. if ((and_mask & ze_mask) == ze_mask)
  8018. return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
  8019. /* Partial overlap. We can reduce the source AND. */
  8020. if ((and_mask & ze_mask) != and_mask)
  8021. {
  8022. mode = GET_MODE (src);
  8023. src = gen_rtx_AND (mode, XEXP (src, 0),
  8024. gen_int_mode (and_mask & ze_mask, mode));
  8025. return gen_rtx_SET (VOIDmode, dest, src);
  8026. }
  8027. }
  8028. /* The other case we handle is assignments into a constant-position
  8029. field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
  8030. a mask that has all one bits except for a group of zero bits and
  8031. OTHER is known to have zeros where C1 has ones, this is such an
  8032. assignment. Compute the position and length from C1. Shift OTHER
  8033. to the appropriate position, force it to the required mode, and
  8034. make the extraction. Check for the AND in both operands. */
  8035. /* One or more SUBREGs might obscure the constant-position field
  8036. assignment. The first one we are likely to encounter is an outer
  8037. narrowing SUBREG, which we can just strip for the purposes of
  8038. identifying the constant-field assignment. */
  8039. if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src))
  8040. src = SUBREG_REG (src);
  8041. if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
  8042. return x;
  8043. rhs = expand_compound_operation (XEXP (src, 0));
  8044. lhs = expand_compound_operation (XEXP (src, 1));
  8045. if (GET_CODE (rhs) == AND
  8046. && CONST_INT_P (XEXP (rhs, 1))
  8047. && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
  8048. c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
  8049. /* The second SUBREG that might get in the way is a paradoxical
  8050. SUBREG around the first operand of the AND. We want to
  8051. pretend the operand is as wide as the destination here. We
  8052. do this by adjusting the MEM to wider mode for the sole
  8053. purpose of the call to rtx_equal_for_field_assignment_p. Also
  8054. note this trick only works for MEMs. */
  8055. else if (GET_CODE (rhs) == AND
  8056. && paradoxical_subreg_p (XEXP (rhs, 0))
  8057. && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
  8058. && CONST_INT_P (XEXP (rhs, 1))
  8059. && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
  8060. dest, true))
  8061. c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
  8062. else if (GET_CODE (lhs) == AND
  8063. && CONST_INT_P (XEXP (lhs, 1))
  8064. && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
  8065. c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
  8066. /* The second SUBREG that might get in the way is a paradoxical
  8067. SUBREG around the first operand of the AND. We want to
  8068. pretend the operand is as wide as the destination here. We
  8069. do this by adjusting the MEM to wider mode for the sole
  8070. purpose of the call to rtx_equal_for_field_assignment_p. Also
  8071. note this trick only works for MEMs. */
  8072. else if (GET_CODE (lhs) == AND
  8073. && paradoxical_subreg_p (XEXP (lhs, 0))
  8074. && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
  8075. && CONST_INT_P (XEXP (lhs, 1))
  8076. && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
  8077. dest, true))
  8078. c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
  8079. else
  8080. return x;
  8081. pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
  8082. if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
  8083. || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
  8084. || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
  8085. return x;
  8086. assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
  8087. if (assign == 0)
  8088. return x;
  8089. /* The mode to use for the source is the mode of the assignment, or of
  8090. what is inside a possible STRICT_LOW_PART. */
  8091. mode = (GET_CODE (assign) == STRICT_LOW_PART
  8092. ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
  8093. /* Shift OTHER right POS places and make it the source, restricting it
  8094. to the proper length and mode. */
  8095. src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
  8096. GET_MODE (src),
  8097. other, pos),
  8098. dest);
  8099. src = force_to_mode (src, mode,
  8100. GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
  8101. ? ~(unsigned HOST_WIDE_INT) 0
  8102. : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
  8103. 0);
  8104. /* If SRC is masked by an AND that does not make a difference in
  8105. the value being stored, strip it. */
  8106. if (GET_CODE (assign) == ZERO_EXTRACT
  8107. && CONST_INT_P (XEXP (assign, 1))
  8108. && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
  8109. && GET_CODE (src) == AND
  8110. && CONST_INT_P (XEXP (src, 1))
  8111. && UINTVAL (XEXP (src, 1))
  8112. == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
  8113. src = XEXP (src, 0);
  8114. return gen_rtx_SET (VOIDmode, assign, src);
  8115. }
  8116. /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
  8117. if so. */
  8118. static rtx
  8119. apply_distributive_law (rtx x)
  8120. {
  8121. enum rtx_code code = GET_CODE (x);
  8122. enum rtx_code inner_code;
  8123. rtx lhs, rhs, other;
  8124. rtx tem;
  8125. /* Distributivity is not true for floating point as it can change the
  8126. value. So we don't do it unless -funsafe-math-optimizations. */
  8127. if (FLOAT_MODE_P (GET_MODE (x))
  8128. && ! flag_unsafe_math_optimizations)
  8129. return x;
  8130. /* The outer operation can only be one of the following: */
  8131. if (code != IOR && code != AND && code != XOR
  8132. && code != PLUS && code != MINUS)
  8133. return x;
  8134. lhs = XEXP (x, 0);
  8135. rhs = XEXP (x, 1);
  8136. /* If either operand is a primitive we can't do anything, so get out
  8137. fast. */
  8138. if (OBJECT_P (lhs) || OBJECT_P (rhs))
  8139. return x;
  8140. lhs = expand_compound_operation (lhs);
  8141. rhs = expand_compound_operation (rhs);
  8142. inner_code = GET_CODE (lhs);
  8143. if (inner_code != GET_CODE (rhs))
  8144. return x;
  8145. /* See if the inner and outer operations distribute. */
  8146. switch (inner_code)
  8147. {
  8148. case LSHIFTRT:
  8149. case ASHIFTRT:
  8150. case AND:
  8151. case IOR:
  8152. /* These all distribute except over PLUS. */
  8153. if (code == PLUS || code == MINUS)
  8154. return x;
  8155. break;
  8156. case MULT:
  8157. if (code != PLUS && code != MINUS)
  8158. return x;
  8159. break;
  8160. case ASHIFT:
  8161. /* This is also a multiply, so it distributes over everything. */
  8162. break;
  8163. /* This used to handle SUBREG, but this turned out to be counter-
  8164. productive, since (subreg (op ...)) usually is not handled by
  8165. insn patterns, and this "optimization" therefore transformed
  8166. recognizable patterns into unrecognizable ones. Therefore the
  8167. SUBREG case was removed from here.
  8168. It is possible that distributing SUBREG over arithmetic operations
  8169. leads to an intermediate result than can then be optimized further,
  8170. e.g. by moving the outer SUBREG to the other side of a SET as done
  8171. in simplify_set. This seems to have been the original intent of
  8172. handling SUBREGs here.
  8173. However, with current GCC this does not appear to actually happen,
  8174. at least on major platforms. If some case is found where removing
  8175. the SUBREG case here prevents follow-on optimizations, distributing
  8176. SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
  8177. default:
  8178. return x;
  8179. }
  8180. /* Set LHS and RHS to the inner operands (A and B in the example
  8181. above) and set OTHER to the common operand (C in the example).
  8182. There is only one way to do this unless the inner operation is
  8183. commutative. */
  8184. if (COMMUTATIVE_ARITH_P (lhs)
  8185. && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
  8186. other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
  8187. else if (COMMUTATIVE_ARITH_P (lhs)
  8188. && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
  8189. other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
  8190. else if (COMMUTATIVE_ARITH_P (lhs)
  8191. && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
  8192. other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
  8193. else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
  8194. other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
  8195. else
  8196. return x;
  8197. /* Form the new inner operation, seeing if it simplifies first. */
  8198. tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
  8199. /* There is one exception to the general way of distributing:
  8200. (a | c) ^ (b | c) -> (a ^ b) & ~c */
  8201. if (code == XOR && inner_code == IOR)
  8202. {
  8203. inner_code = AND;
  8204. other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
  8205. }
  8206. /* We may be able to continuing distributing the result, so call
  8207. ourselves recursively on the inner operation before forming the
  8208. outer operation, which we return. */
  8209. return simplify_gen_binary (inner_code, GET_MODE (x),
  8210. apply_distributive_law (tem), other);
  8211. }
  8212. /* See if X is of the form (* (+ A B) C), and if so convert to
  8213. (+ (* A C) (* B C)) and try to simplify.
  8214. Most of the time, this results in no change. However, if some of
  8215. the operands are the same or inverses of each other, simplifications
  8216. will result.
  8217. For example, (and (ior A B) (not B)) can occur as the result of
  8218. expanding a bit field assignment. When we apply the distributive
  8219. law to this, we get (ior (and (A (not B))) (and (B (not B)))),
  8220. which then simplifies to (and (A (not B))).
  8221. Note that no checks happen on the validity of applying the inverse
  8222. distributive law. This is pointless since we can do it in the
  8223. few places where this routine is called.
  8224. N is the index of the term that is decomposed (the arithmetic operation,
  8225. i.e. (+ A B) in the first example above). !N is the index of the term that
  8226. is distributed, i.e. of C in the first example above. */
  8227. static rtx
  8228. distribute_and_simplify_rtx (rtx x, int n)
  8229. {
  8230. machine_mode mode;
  8231. enum rtx_code outer_code, inner_code;
  8232. rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
  8233. /* Distributivity is not true for floating point as it can change the
  8234. value. So we don't do it unless -funsafe-math-optimizations. */
  8235. if (FLOAT_MODE_P (GET_MODE (x))
  8236. && ! flag_unsafe_math_optimizations)
  8237. return NULL_RTX;
  8238. decomposed = XEXP (x, n);
  8239. if (!ARITHMETIC_P (decomposed))
  8240. return NULL_RTX;
  8241. mode = GET_MODE (x);
  8242. outer_code = GET_CODE (x);
  8243. distributed = XEXP (x, !n);
  8244. inner_code = GET_CODE (decomposed);
  8245. inner_op0 = XEXP (decomposed, 0);
  8246. inner_op1 = XEXP (decomposed, 1);
  8247. /* Special case (and (xor B C) (not A)), which is equivalent to
  8248. (xor (ior A B) (ior A C)) */
  8249. if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
  8250. {
  8251. distributed = XEXP (distributed, 0);
  8252. outer_code = IOR;
  8253. }
  8254. if (n == 0)
  8255. {
  8256. /* Distribute the second term. */
  8257. new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
  8258. new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
  8259. }
  8260. else
  8261. {
  8262. /* Distribute the first term. */
  8263. new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
  8264. new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
  8265. }
  8266. tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
  8267. new_op0, new_op1));
  8268. if (GET_CODE (tmp) != outer_code
  8269. && (set_src_cost (tmp, optimize_this_for_speed_p)
  8270. < set_src_cost (x, optimize_this_for_speed_p)))
  8271. return tmp;
  8272. return NULL_RTX;
  8273. }
  8274. /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
  8275. in MODE. Return an equivalent form, if different from (and VAROP
  8276. (const_int CONSTOP)). Otherwise, return NULL_RTX. */
  8277. static rtx
  8278. simplify_and_const_int_1 (machine_mode mode, rtx varop,
  8279. unsigned HOST_WIDE_INT constop)
  8280. {
  8281. unsigned HOST_WIDE_INT nonzero;
  8282. unsigned HOST_WIDE_INT orig_constop;
  8283. rtx orig_varop;
  8284. int i;
  8285. orig_varop = varop;
  8286. orig_constop = constop;
  8287. if (GET_CODE (varop) == CLOBBER)
  8288. return NULL_RTX;
  8289. /* Simplify VAROP knowing that we will be only looking at some of the
  8290. bits in it.
  8291. Note by passing in CONSTOP, we guarantee that the bits not set in
  8292. CONSTOP are not significant and will never be examined. We must
  8293. ensure that is the case by explicitly masking out those bits
  8294. before returning. */
  8295. varop = force_to_mode (varop, mode, constop, 0);
  8296. /* If VAROP is a CLOBBER, we will fail so return it. */
  8297. if (GET_CODE (varop) == CLOBBER)
  8298. return varop;
  8299. /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
  8300. to VAROP and return the new constant. */
  8301. if (CONST_INT_P (varop))
  8302. return gen_int_mode (INTVAL (varop) & constop, mode);
  8303. /* See what bits may be nonzero in VAROP. Unlike the general case of
  8304. a call to nonzero_bits, here we don't care about bits outside
  8305. MODE. */
  8306. nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
  8307. /* Turn off all bits in the constant that are known to already be zero.
  8308. Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
  8309. which is tested below. */
  8310. constop &= nonzero;
  8311. /* If we don't have any bits left, return zero. */
  8312. if (constop == 0)
  8313. return const0_rtx;
  8314. /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
  8315. a power of two, we can replace this with an ASHIFT. */
  8316. if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
  8317. && (i = exact_log2 (constop)) >= 0)
  8318. return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
  8319. /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
  8320. or XOR, then try to apply the distributive law. This may eliminate
  8321. operations if either branch can be simplified because of the AND.
  8322. It may also make some cases more complex, but those cases probably
  8323. won't match a pattern either with or without this. */
  8324. if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
  8325. return
  8326. gen_lowpart
  8327. (mode,
  8328. apply_distributive_law
  8329. (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
  8330. simplify_and_const_int (NULL_RTX,
  8331. GET_MODE (varop),
  8332. XEXP (varop, 0),
  8333. constop),
  8334. simplify_and_const_int (NULL_RTX,
  8335. GET_MODE (varop),
  8336. XEXP (varop, 1),
  8337. constop))));
  8338. /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
  8339. the AND and see if one of the operands simplifies to zero. If so, we
  8340. may eliminate it. */
  8341. if (GET_CODE (varop) == PLUS
  8342. && exact_log2 (constop + 1) >= 0)
  8343. {
  8344. rtx o0, o1;
  8345. o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
  8346. o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
  8347. if (o0 == const0_rtx)
  8348. return o1;
  8349. if (o1 == const0_rtx)
  8350. return o0;
  8351. }
  8352. /* Make a SUBREG if necessary. If we can't make it, fail. */
  8353. varop = gen_lowpart (mode, varop);
  8354. if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
  8355. return NULL_RTX;
  8356. /* If we are only masking insignificant bits, return VAROP. */
  8357. if (constop == nonzero)
  8358. return varop;
  8359. if (varop == orig_varop && constop == orig_constop)
  8360. return NULL_RTX;
  8361. /* Otherwise, return an AND. */
  8362. return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
  8363. }
  8364. /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
  8365. in MODE.
  8366. Return an equivalent form, if different from X. Otherwise, return X. If
  8367. X is zero, we are to always construct the equivalent form. */
  8368. static rtx
  8369. simplify_and_const_int (rtx x, machine_mode mode, rtx varop,
  8370. unsigned HOST_WIDE_INT constop)
  8371. {
  8372. rtx tem = simplify_and_const_int_1 (mode, varop, constop);
  8373. if (tem)
  8374. return tem;
  8375. if (!x)
  8376. x = simplify_gen_binary (AND, GET_MODE (varop), varop,
  8377. gen_int_mode (constop, mode));
  8378. if (GET_MODE (x) != mode)
  8379. x = gen_lowpart (mode, x);
  8380. return x;
  8381. }
  8382. /* Given a REG, X, compute which bits in X can be nonzero.
  8383. We don't care about bits outside of those defined in MODE.
  8384. For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
  8385. a shift, AND, or zero_extract, we can do better. */
  8386. static rtx
  8387. reg_nonzero_bits_for_combine (const_rtx x, machine_mode mode,
  8388. const_rtx known_x ATTRIBUTE_UNUSED,
  8389. machine_mode known_mode ATTRIBUTE_UNUSED,
  8390. unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
  8391. unsigned HOST_WIDE_INT *nonzero)
  8392. {
  8393. rtx tem;
  8394. reg_stat_type *rsp;
  8395. /* If X is a register whose nonzero bits value is current, use it.
  8396. Otherwise, if X is a register whose value we can find, use that
  8397. value. Otherwise, use the previously-computed global nonzero bits
  8398. for this register. */
  8399. rsp = &reg_stat[REGNO (x)];
  8400. if (rsp->last_set_value != 0
  8401. && (rsp->last_set_mode == mode
  8402. || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
  8403. && GET_MODE_CLASS (mode) == MODE_INT))
  8404. && ((rsp->last_set_label >= label_tick_ebb_start
  8405. && rsp->last_set_label < label_tick)
  8406. || (rsp->last_set_label == label_tick
  8407. && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
  8408. || (REGNO (x) >= FIRST_PSEUDO_REGISTER
  8409. && REGNO (x) < reg_n_sets_max
  8410. && REG_N_SETS (REGNO (x)) == 1
  8411. && !REGNO_REG_SET_P
  8412. (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
  8413. REGNO (x)))))
  8414. {
  8415. unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
  8416. if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
  8417. /* We don't know anything about the upper bits. */
  8418. mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
  8419. *nonzero &= mask;
  8420. return NULL;
  8421. }
  8422. tem = get_last_value (x);
  8423. if (tem)
  8424. {
  8425. #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
  8426. /* If X is narrower than MODE and TEM is a non-negative
  8427. constant that would appear negative in the mode of X,
  8428. sign-extend it for use in reg_nonzero_bits because some
  8429. machines (maybe most) will actually do the sign-extension
  8430. and this is the conservative approach.
  8431. ??? For 2.5, try to tighten up the MD files in this regard
  8432. instead of this kludge. */
  8433. if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
  8434. && CONST_INT_P (tem)
  8435. && INTVAL (tem) > 0
  8436. && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
  8437. tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
  8438. #endif
  8439. return tem;
  8440. }
  8441. else if (nonzero_sign_valid && rsp->nonzero_bits)
  8442. {
  8443. unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
  8444. if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
  8445. /* We don't know anything about the upper bits. */
  8446. mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
  8447. *nonzero &= mask;
  8448. }
  8449. return NULL;
  8450. }
  8451. /* Return the number of bits at the high-order end of X that are known to
  8452. be equal to the sign bit. X will be used in mode MODE; if MODE is
  8453. VOIDmode, X will be used in its own mode. The returned value will always
  8454. be between 1 and the number of bits in MODE. */
  8455. static rtx
  8456. reg_num_sign_bit_copies_for_combine (const_rtx x, machine_mode mode,
  8457. const_rtx known_x ATTRIBUTE_UNUSED,
  8458. machine_mode known_mode
  8459. ATTRIBUTE_UNUSED,
  8460. unsigned int known_ret ATTRIBUTE_UNUSED,
  8461. unsigned int *result)
  8462. {
  8463. rtx tem;
  8464. reg_stat_type *rsp;
  8465. rsp = &reg_stat[REGNO (x)];
  8466. if (rsp->last_set_value != 0
  8467. && rsp->last_set_mode == mode
  8468. && ((rsp->last_set_label >= label_tick_ebb_start
  8469. && rsp->last_set_label < label_tick)
  8470. || (rsp->last_set_label == label_tick
  8471. && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
  8472. || (REGNO (x) >= FIRST_PSEUDO_REGISTER
  8473. && REGNO (x) < reg_n_sets_max
  8474. && REG_N_SETS (REGNO (x)) == 1
  8475. && !REGNO_REG_SET_P
  8476. (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
  8477. REGNO (x)))))
  8478. {
  8479. *result = rsp->last_set_sign_bit_copies;
  8480. return NULL;
  8481. }
  8482. tem = get_last_value (x);
  8483. if (tem != 0)
  8484. return tem;
  8485. if (nonzero_sign_valid && rsp->sign_bit_copies != 0
  8486. && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
  8487. *result = rsp->sign_bit_copies;
  8488. return NULL;
  8489. }
  8490. /* Return the number of "extended" bits there are in X, when interpreted
  8491. as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
  8492. unsigned quantities, this is the number of high-order zero bits.
  8493. For signed quantities, this is the number of copies of the sign bit
  8494. minus 1. In both case, this function returns the number of "spare"
  8495. bits. For example, if two quantities for which this function returns
  8496. at least 1 are added, the addition is known not to overflow.
  8497. This function will always return 0 unless called during combine, which
  8498. implies that it must be called from a define_split. */
  8499. unsigned int
  8500. extended_count (const_rtx x, machine_mode mode, int unsignedp)
  8501. {
  8502. if (nonzero_sign_valid == 0)
  8503. return 0;
  8504. return (unsignedp
  8505. ? (HWI_COMPUTABLE_MODE_P (mode)
  8506. ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
  8507. - floor_log2 (nonzero_bits (x, mode)))
  8508. : 0)
  8509. : num_sign_bit_copies (x, mode) - 1);
  8510. }
  8511. /* This function is called from `simplify_shift_const' to merge two
  8512. outer operations. Specifically, we have already found that we need
  8513. to perform operation *POP0 with constant *PCONST0 at the outermost
  8514. position. We would now like to also perform OP1 with constant CONST1
  8515. (with *POP0 being done last).
  8516. Return 1 if we can do the operation and update *POP0 and *PCONST0 with
  8517. the resulting operation. *PCOMP_P is set to 1 if we would need to
  8518. complement the innermost operand, otherwise it is unchanged.
  8519. MODE is the mode in which the operation will be done. No bits outside
  8520. the width of this mode matter. It is assumed that the width of this mode
  8521. is smaller than or equal to HOST_BITS_PER_WIDE_INT.
  8522. If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
  8523. IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
  8524. result is simply *PCONST0.
  8525. If the resulting operation cannot be expressed as one operation, we
  8526. return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
  8527. static int
  8528. merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, machine_mode mode, int *pcomp_p)
  8529. {
  8530. enum rtx_code op0 = *pop0;
  8531. HOST_WIDE_INT const0 = *pconst0;
  8532. const0 &= GET_MODE_MASK (mode);
  8533. const1 &= GET_MODE_MASK (mode);
  8534. /* If OP0 is an AND, clear unimportant bits in CONST1. */
  8535. if (op0 == AND)
  8536. const1 &= const0;
  8537. /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
  8538. if OP0 is SET. */
  8539. if (op1 == UNKNOWN || op0 == SET)
  8540. return 1;
  8541. else if (op0 == UNKNOWN)
  8542. op0 = op1, const0 = const1;
  8543. else if (op0 == op1)
  8544. {
  8545. switch (op0)
  8546. {
  8547. case AND:
  8548. const0 &= const1;
  8549. break;
  8550. case IOR:
  8551. const0 |= const1;
  8552. break;
  8553. case XOR:
  8554. const0 ^= const1;
  8555. break;
  8556. case PLUS:
  8557. const0 += const1;
  8558. break;
  8559. case NEG:
  8560. op0 = UNKNOWN;
  8561. break;
  8562. default:
  8563. break;
  8564. }
  8565. }
  8566. /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
  8567. else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
  8568. return 0;
  8569. /* If the two constants aren't the same, we can't do anything. The
  8570. remaining six cases can all be done. */
  8571. else if (const0 != const1)
  8572. return 0;
  8573. else
  8574. switch (op0)
  8575. {
  8576. case IOR:
  8577. if (op1 == AND)
  8578. /* (a & b) | b == b */
  8579. op0 = SET;
  8580. else /* op1 == XOR */
  8581. /* (a ^ b) | b == a | b */
  8582. {;}
  8583. break;
  8584. case XOR:
  8585. if (op1 == AND)
  8586. /* (a & b) ^ b == (~a) & b */
  8587. op0 = AND, *pcomp_p = 1;
  8588. else /* op1 == IOR */
  8589. /* (a | b) ^ b == a & ~b */
  8590. op0 = AND, const0 = ~const0;
  8591. break;
  8592. case AND:
  8593. if (op1 == IOR)
  8594. /* (a | b) & b == b */
  8595. op0 = SET;
  8596. else /* op1 == XOR */
  8597. /* (a ^ b) & b) == (~a) & b */
  8598. *pcomp_p = 1;
  8599. break;
  8600. default:
  8601. break;
  8602. }
  8603. /* Check for NO-OP cases. */
  8604. const0 &= GET_MODE_MASK (mode);
  8605. if (const0 == 0
  8606. && (op0 == IOR || op0 == XOR || op0 == PLUS))
  8607. op0 = UNKNOWN;
  8608. else if (const0 == 0 && op0 == AND)
  8609. op0 = SET;
  8610. else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
  8611. && op0 == AND)
  8612. op0 = UNKNOWN;
  8613. *pop0 = op0;
  8614. /* ??? Slightly redundant with the above mask, but not entirely.
  8615. Moving this above means we'd have to sign-extend the mode mask
  8616. for the final test. */
  8617. if (op0 != UNKNOWN && op0 != NEG)
  8618. *pconst0 = trunc_int_for_mode (const0, mode);
  8619. return 1;
  8620. }
  8621. /* A helper to simplify_shift_const_1 to determine the mode we can perform
  8622. the shift in. The original shift operation CODE is performed on OP in
  8623. ORIG_MODE. Return the wider mode MODE if we can perform the operation
  8624. in that mode. Return ORIG_MODE otherwise. We can also assume that the
  8625. result of the shift is subject to operation OUTER_CODE with operand
  8626. OUTER_CONST. */
  8627. static machine_mode
  8628. try_widen_shift_mode (enum rtx_code code, rtx op, int count,
  8629. machine_mode orig_mode, machine_mode mode,
  8630. enum rtx_code outer_code, HOST_WIDE_INT outer_const)
  8631. {
  8632. if (orig_mode == mode)
  8633. return mode;
  8634. gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
  8635. /* In general we can't perform in wider mode for right shift and rotate. */
  8636. switch (code)
  8637. {
  8638. case ASHIFTRT:
  8639. /* We can still widen if the bits brought in from the left are identical
  8640. to the sign bit of ORIG_MODE. */
  8641. if (num_sign_bit_copies (op, mode)
  8642. > (unsigned) (GET_MODE_PRECISION (mode)
  8643. - GET_MODE_PRECISION (orig_mode)))
  8644. return mode;
  8645. return orig_mode;
  8646. case LSHIFTRT:
  8647. /* Similarly here but with zero bits. */
  8648. if (HWI_COMPUTABLE_MODE_P (mode)
  8649. && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
  8650. return mode;
  8651. /* We can also widen if the bits brought in will be masked off. This
  8652. operation is performed in ORIG_MODE. */
  8653. if (outer_code == AND)
  8654. {
  8655. int care_bits = low_bitmask_len (orig_mode, outer_const);
  8656. if (care_bits >= 0
  8657. && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
  8658. return mode;
  8659. }
  8660. /* fall through */
  8661. case ROTATE:
  8662. return orig_mode;
  8663. case ROTATERT:
  8664. gcc_unreachable ();
  8665. default:
  8666. return mode;
  8667. }
  8668. }
  8669. /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
  8670. of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
  8671. if we cannot simplify it. Otherwise, return a simplified value.
  8672. The shift is normally computed in the widest mode we find in VAROP, as
  8673. long as it isn't a different number of words than RESULT_MODE. Exceptions
  8674. are ASHIFTRT and ROTATE, which are always done in their original mode. */
  8675. static rtx
  8676. simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
  8677. rtx varop, int orig_count)
  8678. {
  8679. enum rtx_code orig_code = code;
  8680. rtx orig_varop = varop;
  8681. int count;
  8682. machine_mode mode = result_mode;
  8683. machine_mode shift_mode, tmode;
  8684. unsigned int mode_words
  8685. = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
  8686. /* We form (outer_op (code varop count) (outer_const)). */
  8687. enum rtx_code outer_op = UNKNOWN;
  8688. HOST_WIDE_INT outer_const = 0;
  8689. int complement_p = 0;
  8690. rtx new_rtx, x;
  8691. /* Make sure and truncate the "natural" shift on the way in. We don't
  8692. want to do this inside the loop as it makes it more difficult to
  8693. combine shifts. */
  8694. if (SHIFT_COUNT_TRUNCATED)
  8695. orig_count &= GET_MODE_BITSIZE (mode) - 1;
  8696. /* If we were given an invalid count, don't do anything except exactly
  8697. what was requested. */
  8698. if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
  8699. return NULL_RTX;
  8700. count = orig_count;
  8701. /* Unless one of the branches of the `if' in this loop does a `continue',
  8702. we will `break' the loop after the `if'. */
  8703. while (count != 0)
  8704. {
  8705. /* If we have an operand of (clobber (const_int 0)), fail. */
  8706. if (GET_CODE (varop) == CLOBBER)
  8707. return NULL_RTX;
  8708. /* Convert ROTATERT to ROTATE. */
  8709. if (code == ROTATERT)
  8710. {
  8711. unsigned int bitsize = GET_MODE_PRECISION (result_mode);
  8712. code = ROTATE;
  8713. if (VECTOR_MODE_P (result_mode))
  8714. count = bitsize / GET_MODE_NUNITS (result_mode) - count;
  8715. else
  8716. count = bitsize - count;
  8717. }
  8718. shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
  8719. mode, outer_op, outer_const);
  8720. /* Handle cases where the count is greater than the size of the mode
  8721. minus 1. For ASHIFT, use the size minus one as the count (this can
  8722. occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
  8723. take the count modulo the size. For other shifts, the result is
  8724. zero.
  8725. Since these shifts are being produced by the compiler by combining
  8726. multiple operations, each of which are defined, we know what the
  8727. result is supposed to be. */
  8728. if (count > (GET_MODE_PRECISION (shift_mode) - 1))
  8729. {
  8730. if (code == ASHIFTRT)
  8731. count = GET_MODE_PRECISION (shift_mode) - 1;
  8732. else if (code == ROTATE || code == ROTATERT)
  8733. count %= GET_MODE_PRECISION (shift_mode);
  8734. else
  8735. {
  8736. /* We can't simply return zero because there may be an
  8737. outer op. */
  8738. varop = const0_rtx;
  8739. count = 0;
  8740. break;
  8741. }
  8742. }
  8743. /* If we discovered we had to complement VAROP, leave. Making a NOT
  8744. here would cause an infinite loop. */
  8745. if (complement_p)
  8746. break;
  8747. /* An arithmetic right shift of a quantity known to be -1 or 0
  8748. is a no-op. */
  8749. if (code == ASHIFTRT
  8750. && (num_sign_bit_copies (varop, shift_mode)
  8751. == GET_MODE_PRECISION (shift_mode)))
  8752. {
  8753. count = 0;
  8754. break;
  8755. }
  8756. /* If we are doing an arithmetic right shift and discarding all but
  8757. the sign bit copies, this is equivalent to doing a shift by the
  8758. bitsize minus one. Convert it into that shift because it will often
  8759. allow other simplifications. */
  8760. if (code == ASHIFTRT
  8761. && (count + num_sign_bit_copies (varop, shift_mode)
  8762. >= GET_MODE_PRECISION (shift_mode)))
  8763. count = GET_MODE_PRECISION (shift_mode) - 1;
  8764. /* We simplify the tests below and elsewhere by converting
  8765. ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
  8766. `make_compound_operation' will convert it to an ASHIFTRT for
  8767. those machines (such as VAX) that don't have an LSHIFTRT. */
  8768. if (code == ASHIFTRT
  8769. && val_signbit_known_clear_p (shift_mode,
  8770. nonzero_bits (varop, shift_mode)))
  8771. code = LSHIFTRT;
  8772. if (((code == LSHIFTRT
  8773. && HWI_COMPUTABLE_MODE_P (shift_mode)
  8774. && !(nonzero_bits (varop, shift_mode) >> count))
  8775. || (code == ASHIFT
  8776. && HWI_COMPUTABLE_MODE_P (shift_mode)
  8777. && !((nonzero_bits (varop, shift_mode) << count)
  8778. & GET_MODE_MASK (shift_mode))))
  8779. && !side_effects_p (varop))
  8780. varop = const0_rtx;
  8781. switch (GET_CODE (varop))
  8782. {
  8783. case SIGN_EXTEND:
  8784. case ZERO_EXTEND:
  8785. case SIGN_EXTRACT:
  8786. case ZERO_EXTRACT:
  8787. new_rtx = expand_compound_operation (varop);
  8788. if (new_rtx != varop)
  8789. {
  8790. varop = new_rtx;
  8791. continue;
  8792. }
  8793. break;
  8794. case MEM:
  8795. /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
  8796. minus the width of a smaller mode, we can do this with a
  8797. SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
  8798. if ((code == ASHIFTRT || code == LSHIFTRT)
  8799. && ! mode_dependent_address_p (XEXP (varop, 0),
  8800. MEM_ADDR_SPACE (varop))
  8801. && ! MEM_VOLATILE_P (varop)
  8802. && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
  8803. MODE_INT, 1)) != BLKmode)
  8804. {
  8805. new_rtx = adjust_address_nv (varop, tmode,
  8806. BYTES_BIG_ENDIAN ? 0
  8807. : count / BITS_PER_UNIT);
  8808. varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
  8809. : ZERO_EXTEND, mode, new_rtx);
  8810. count = 0;
  8811. continue;
  8812. }
  8813. break;
  8814. case SUBREG:
  8815. /* If VAROP is a SUBREG, strip it as long as the inner operand has
  8816. the same number of words as what we've seen so far. Then store
  8817. the widest mode in MODE. */
  8818. if (subreg_lowpart_p (varop)
  8819. && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
  8820. > GET_MODE_SIZE (GET_MODE (varop)))
  8821. && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
  8822. + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
  8823. == mode_words
  8824. && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
  8825. && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
  8826. {
  8827. varop = SUBREG_REG (varop);
  8828. if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
  8829. mode = GET_MODE (varop);
  8830. continue;
  8831. }
  8832. break;
  8833. case MULT:
  8834. /* Some machines use MULT instead of ASHIFT because MULT
  8835. is cheaper. But it is still better on those machines to
  8836. merge two shifts into one. */
  8837. if (CONST_INT_P (XEXP (varop, 1))
  8838. && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
  8839. {
  8840. varop
  8841. = simplify_gen_binary (ASHIFT, GET_MODE (varop),
  8842. XEXP (varop, 0),
  8843. GEN_INT (exact_log2 (
  8844. UINTVAL (XEXP (varop, 1)))));
  8845. continue;
  8846. }
  8847. break;
  8848. case UDIV:
  8849. /* Similar, for when divides are cheaper. */
  8850. if (CONST_INT_P (XEXP (varop, 1))
  8851. && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
  8852. {
  8853. varop
  8854. = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
  8855. XEXP (varop, 0),
  8856. GEN_INT (exact_log2 (
  8857. UINTVAL (XEXP (varop, 1)))));
  8858. continue;
  8859. }
  8860. break;
  8861. case ASHIFTRT:
  8862. /* If we are extracting just the sign bit of an arithmetic
  8863. right shift, that shift is not needed. However, the sign
  8864. bit of a wider mode may be different from what would be
  8865. interpreted as the sign bit in a narrower mode, so, if
  8866. the result is narrower, don't discard the shift. */
  8867. if (code == LSHIFTRT
  8868. && count == (GET_MODE_BITSIZE (result_mode) - 1)
  8869. && (GET_MODE_BITSIZE (result_mode)
  8870. >= GET_MODE_BITSIZE (GET_MODE (varop))))
  8871. {
  8872. varop = XEXP (varop, 0);
  8873. continue;
  8874. }
  8875. /* ... fall through ... */
  8876. case LSHIFTRT:
  8877. case ASHIFT:
  8878. case ROTATE:
  8879. /* Here we have two nested shifts. The result is usually the
  8880. AND of a new shift with a mask. We compute the result below. */
  8881. if (CONST_INT_P (XEXP (varop, 1))
  8882. && INTVAL (XEXP (varop, 1)) >= 0
  8883. && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
  8884. && HWI_COMPUTABLE_MODE_P (result_mode)
  8885. && HWI_COMPUTABLE_MODE_P (mode)
  8886. && !VECTOR_MODE_P (result_mode))
  8887. {
  8888. enum rtx_code first_code = GET_CODE (varop);
  8889. unsigned int first_count = INTVAL (XEXP (varop, 1));
  8890. unsigned HOST_WIDE_INT mask;
  8891. rtx mask_rtx;
  8892. /* We have one common special case. We can't do any merging if
  8893. the inner code is an ASHIFTRT of a smaller mode. However, if
  8894. we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
  8895. with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
  8896. we can convert it to
  8897. (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
  8898. This simplifies certain SIGN_EXTEND operations. */
  8899. if (code == ASHIFT && first_code == ASHIFTRT
  8900. && count == (GET_MODE_PRECISION (result_mode)
  8901. - GET_MODE_PRECISION (GET_MODE (varop))))
  8902. {
  8903. /* C3 has the low-order C1 bits zero. */
  8904. mask = GET_MODE_MASK (mode)
  8905. & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
  8906. varop = simplify_and_const_int (NULL_RTX, result_mode,
  8907. XEXP (varop, 0), mask);
  8908. varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
  8909. varop, count);
  8910. count = first_count;
  8911. code = ASHIFTRT;
  8912. continue;
  8913. }
  8914. /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
  8915. than C1 high-order bits equal to the sign bit, we can convert
  8916. this to either an ASHIFT or an ASHIFTRT depending on the
  8917. two counts.
  8918. We cannot do this if VAROP's mode is not SHIFT_MODE. */
  8919. if (code == ASHIFTRT && first_code == ASHIFT
  8920. && GET_MODE (varop) == shift_mode
  8921. && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
  8922. > first_count))
  8923. {
  8924. varop = XEXP (varop, 0);
  8925. count -= first_count;
  8926. if (count < 0)
  8927. {
  8928. count = -count;
  8929. code = ASHIFT;
  8930. }
  8931. continue;
  8932. }
  8933. /* There are some cases we can't do. If CODE is ASHIFTRT,
  8934. we can only do this if FIRST_CODE is also ASHIFTRT.
  8935. We can't do the case when CODE is ROTATE and FIRST_CODE is
  8936. ASHIFTRT.
  8937. If the mode of this shift is not the mode of the outer shift,
  8938. we can't do this if either shift is a right shift or ROTATE.
  8939. Finally, we can't do any of these if the mode is too wide
  8940. unless the codes are the same.
  8941. Handle the case where the shift codes are the same
  8942. first. */
  8943. if (code == first_code)
  8944. {
  8945. if (GET_MODE (varop) != result_mode
  8946. && (code == ASHIFTRT || code == LSHIFTRT
  8947. || code == ROTATE))
  8948. break;
  8949. count += first_count;
  8950. varop = XEXP (varop, 0);
  8951. continue;
  8952. }
  8953. if (code == ASHIFTRT
  8954. || (code == ROTATE && first_code == ASHIFTRT)
  8955. || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
  8956. || (GET_MODE (varop) != result_mode
  8957. && (first_code == ASHIFTRT || first_code == LSHIFTRT
  8958. || first_code == ROTATE
  8959. || code == ROTATE)))
  8960. break;
  8961. /* To compute the mask to apply after the shift, shift the
  8962. nonzero bits of the inner shift the same way the
  8963. outer shift will. */
  8964. mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
  8965. result_mode);
  8966. mask_rtx
  8967. = simplify_const_binary_operation (code, result_mode, mask_rtx,
  8968. GEN_INT (count));
  8969. /* Give up if we can't compute an outer operation to use. */
  8970. if (mask_rtx == 0
  8971. || !CONST_INT_P (mask_rtx)
  8972. || ! merge_outer_ops (&outer_op, &outer_const, AND,
  8973. INTVAL (mask_rtx),
  8974. result_mode, &complement_p))
  8975. break;
  8976. /* If the shifts are in the same direction, we add the
  8977. counts. Otherwise, we subtract them. */
  8978. if ((code == ASHIFTRT || code == LSHIFTRT)
  8979. == (first_code == ASHIFTRT || first_code == LSHIFTRT))
  8980. count += first_count;
  8981. else
  8982. count -= first_count;
  8983. /* If COUNT is positive, the new shift is usually CODE,
  8984. except for the two exceptions below, in which case it is
  8985. FIRST_CODE. If the count is negative, FIRST_CODE should
  8986. always be used */
  8987. if (count > 0
  8988. && ((first_code == ROTATE && code == ASHIFT)
  8989. || (first_code == ASHIFTRT && code == LSHIFTRT)))
  8990. code = first_code;
  8991. else if (count < 0)
  8992. code = first_code, count = -count;
  8993. varop = XEXP (varop, 0);
  8994. continue;
  8995. }
  8996. /* If we have (A << B << C) for any shift, we can convert this to
  8997. (A << C << B). This wins if A is a constant. Only try this if
  8998. B is not a constant. */
  8999. else if (GET_CODE (varop) == code
  9000. && CONST_INT_P (XEXP (varop, 0))
  9001. && !CONST_INT_P (XEXP (varop, 1)))
  9002. {
  9003. rtx new_rtx = simplify_const_binary_operation (code, mode,
  9004. XEXP (varop, 0),
  9005. GEN_INT (count));
  9006. varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
  9007. count = 0;
  9008. continue;
  9009. }
  9010. break;
  9011. case NOT:
  9012. if (VECTOR_MODE_P (mode))
  9013. break;
  9014. /* Make this fit the case below. */
  9015. varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
  9016. continue;
  9017. case IOR:
  9018. case AND:
  9019. case XOR:
  9020. /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
  9021. with C the size of VAROP - 1 and the shift is logical if
  9022. STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
  9023. we have an (le X 0) operation. If we have an arithmetic shift
  9024. and STORE_FLAG_VALUE is 1 or we have a logical shift with
  9025. STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
  9026. if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
  9027. && XEXP (XEXP (varop, 0), 1) == constm1_rtx
  9028. && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
  9029. && (code == LSHIFTRT || code == ASHIFTRT)
  9030. && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
  9031. && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
  9032. {
  9033. count = 0;
  9034. varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
  9035. const0_rtx);
  9036. if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
  9037. varop = gen_rtx_NEG (GET_MODE (varop), varop);
  9038. continue;
  9039. }
  9040. /* If we have (shift (logical)), move the logical to the outside
  9041. to allow it to possibly combine with another logical and the
  9042. shift to combine with another shift. This also canonicalizes to
  9043. what a ZERO_EXTRACT looks like. Also, some machines have
  9044. (and (shift)) insns. */
  9045. if (CONST_INT_P (XEXP (varop, 1))
  9046. /* We can't do this if we have (ashiftrt (xor)) and the
  9047. constant has its sign bit set in shift_mode with shift_mode
  9048. wider than result_mode. */
  9049. && !(code == ASHIFTRT && GET_CODE (varop) == XOR
  9050. && result_mode != shift_mode
  9051. && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
  9052. shift_mode))
  9053. && (new_rtx = simplify_const_binary_operation
  9054. (code, result_mode,
  9055. gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
  9056. GEN_INT (count))) != 0
  9057. && CONST_INT_P (new_rtx)
  9058. && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
  9059. INTVAL (new_rtx), result_mode, &complement_p))
  9060. {
  9061. varop = XEXP (varop, 0);
  9062. continue;
  9063. }
  9064. /* If we can't do that, try to simplify the shift in each arm of the
  9065. logical expression, make a new logical expression, and apply
  9066. the inverse distributive law. This also can't be done for
  9067. (ashiftrt (xor)) where we've widened the shift and the constant
  9068. changes the sign bit. */
  9069. if (CONST_INT_P (XEXP (varop, 1))
  9070. && !(code == ASHIFTRT && GET_CODE (varop) == XOR
  9071. && result_mode != shift_mode
  9072. && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
  9073. shift_mode)))
  9074. {
  9075. rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
  9076. XEXP (varop, 0), count);
  9077. rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
  9078. XEXP (varop, 1), count);
  9079. varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
  9080. lhs, rhs);
  9081. varop = apply_distributive_law (varop);
  9082. count = 0;
  9083. continue;
  9084. }
  9085. break;
  9086. case EQ:
  9087. /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
  9088. says that the sign bit can be tested, FOO has mode MODE, C is
  9089. GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
  9090. that may be nonzero. */
  9091. if (code == LSHIFTRT
  9092. && XEXP (varop, 1) == const0_rtx
  9093. && GET_MODE (XEXP (varop, 0)) == result_mode
  9094. && count == (GET_MODE_PRECISION (result_mode) - 1)
  9095. && HWI_COMPUTABLE_MODE_P (result_mode)
  9096. && STORE_FLAG_VALUE == -1
  9097. && nonzero_bits (XEXP (varop, 0), result_mode) == 1
  9098. && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
  9099. &complement_p))
  9100. {
  9101. varop = XEXP (varop, 0);
  9102. count = 0;
  9103. continue;
  9104. }
  9105. break;
  9106. case NEG:
  9107. /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
  9108. than the number of bits in the mode is equivalent to A. */
  9109. if (code == LSHIFTRT
  9110. && count == (GET_MODE_PRECISION (result_mode) - 1)
  9111. && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
  9112. {
  9113. varop = XEXP (varop, 0);
  9114. count = 0;
  9115. continue;
  9116. }
  9117. /* NEG commutes with ASHIFT since it is multiplication. Move the
  9118. NEG outside to allow shifts to combine. */
  9119. if (code == ASHIFT
  9120. && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
  9121. &complement_p))
  9122. {
  9123. varop = XEXP (varop, 0);
  9124. continue;
  9125. }
  9126. break;
  9127. case PLUS:
  9128. /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
  9129. is one less than the number of bits in the mode is
  9130. equivalent to (xor A 1). */
  9131. if (code == LSHIFTRT
  9132. && count == (GET_MODE_PRECISION (result_mode) - 1)
  9133. && XEXP (varop, 1) == constm1_rtx
  9134. && nonzero_bits (XEXP (varop, 0), result_mode) == 1
  9135. && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
  9136. &complement_p))
  9137. {
  9138. count = 0;
  9139. varop = XEXP (varop, 0);
  9140. continue;
  9141. }
  9142. /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
  9143. that might be nonzero in BAR are those being shifted out and those
  9144. bits are known zero in FOO, we can replace the PLUS with FOO.
  9145. Similarly in the other operand order. This code occurs when
  9146. we are computing the size of a variable-size array. */
  9147. if ((code == ASHIFTRT || code == LSHIFTRT)
  9148. && count < HOST_BITS_PER_WIDE_INT
  9149. && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
  9150. && (nonzero_bits (XEXP (varop, 1), result_mode)
  9151. & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
  9152. {
  9153. varop = XEXP (varop, 0);
  9154. continue;
  9155. }
  9156. else if ((code == ASHIFTRT || code == LSHIFTRT)
  9157. && count < HOST_BITS_PER_WIDE_INT
  9158. && HWI_COMPUTABLE_MODE_P (result_mode)
  9159. && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
  9160. >> count)
  9161. && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
  9162. & nonzero_bits (XEXP (varop, 1),
  9163. result_mode)))
  9164. {
  9165. varop = XEXP (varop, 1);
  9166. continue;
  9167. }
  9168. /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
  9169. if (code == ASHIFT
  9170. && CONST_INT_P (XEXP (varop, 1))
  9171. && (new_rtx = simplify_const_binary_operation
  9172. (ASHIFT, result_mode,
  9173. gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
  9174. GEN_INT (count))) != 0
  9175. && CONST_INT_P (new_rtx)
  9176. && merge_outer_ops (&outer_op, &outer_const, PLUS,
  9177. INTVAL (new_rtx), result_mode, &complement_p))
  9178. {
  9179. varop = XEXP (varop, 0);
  9180. continue;
  9181. }
  9182. /* Check for 'PLUS signbit', which is the canonical form of 'XOR
  9183. signbit', and attempt to change the PLUS to an XOR and move it to
  9184. the outer operation as is done above in the AND/IOR/XOR case
  9185. leg for shift(logical). See details in logical handling above
  9186. for reasoning in doing so. */
  9187. if (code == LSHIFTRT
  9188. && CONST_INT_P (XEXP (varop, 1))
  9189. && mode_signbit_p (result_mode, XEXP (varop, 1))
  9190. && (new_rtx = simplify_const_binary_operation
  9191. (code, result_mode,
  9192. gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
  9193. GEN_INT (count))) != 0
  9194. && CONST_INT_P (new_rtx)
  9195. && merge_outer_ops (&outer_op, &outer_const, XOR,
  9196. INTVAL (new_rtx), result_mode, &complement_p))
  9197. {
  9198. varop = XEXP (varop, 0);
  9199. continue;
  9200. }
  9201. break;
  9202. case MINUS:
  9203. /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
  9204. with C the size of VAROP - 1 and the shift is logical if
  9205. STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
  9206. we have a (gt X 0) operation. If the shift is arithmetic with
  9207. STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
  9208. we have a (neg (gt X 0)) operation. */
  9209. if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
  9210. && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
  9211. && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
  9212. && (code == LSHIFTRT || code == ASHIFTRT)
  9213. && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
  9214. && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
  9215. && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
  9216. {
  9217. count = 0;
  9218. varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
  9219. const0_rtx);
  9220. if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
  9221. varop = gen_rtx_NEG (GET_MODE (varop), varop);
  9222. continue;
  9223. }
  9224. break;
  9225. case TRUNCATE:
  9226. /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
  9227. if the truncate does not affect the value. */
  9228. if (code == LSHIFTRT
  9229. && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
  9230. && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
  9231. && (INTVAL (XEXP (XEXP (varop, 0), 1))
  9232. >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
  9233. - GET_MODE_PRECISION (GET_MODE (varop)))))
  9234. {
  9235. rtx varop_inner = XEXP (varop, 0);
  9236. varop_inner
  9237. = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
  9238. XEXP (varop_inner, 0),
  9239. GEN_INT
  9240. (count + INTVAL (XEXP (varop_inner, 1))));
  9241. varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
  9242. count = 0;
  9243. continue;
  9244. }
  9245. break;
  9246. default:
  9247. break;
  9248. }
  9249. break;
  9250. }
  9251. shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
  9252. outer_op, outer_const);
  9253. /* We have now finished analyzing the shift. The result should be
  9254. a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
  9255. OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
  9256. to the result of the shift. OUTER_CONST is the relevant constant,
  9257. but we must turn off all bits turned off in the shift. */
  9258. if (outer_op == UNKNOWN
  9259. && orig_code == code && orig_count == count
  9260. && varop == orig_varop
  9261. && shift_mode == GET_MODE (varop))
  9262. return NULL_RTX;
  9263. /* Make a SUBREG if necessary. If we can't make it, fail. */
  9264. varop = gen_lowpart (shift_mode, varop);
  9265. if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
  9266. return NULL_RTX;
  9267. /* If we have an outer operation and we just made a shift, it is
  9268. possible that we could have simplified the shift were it not
  9269. for the outer operation. So try to do the simplification
  9270. recursively. */
  9271. if (outer_op != UNKNOWN)
  9272. x = simplify_shift_const_1 (code, shift_mode, varop, count);
  9273. else
  9274. x = NULL_RTX;
  9275. if (x == NULL_RTX)
  9276. x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
  9277. /* If we were doing an LSHIFTRT in a wider mode than it was originally,
  9278. turn off all the bits that the shift would have turned off. */
  9279. if (orig_code == LSHIFTRT && result_mode != shift_mode)
  9280. x = simplify_and_const_int (NULL_RTX, shift_mode, x,
  9281. GET_MODE_MASK (result_mode) >> orig_count);
  9282. /* Do the remainder of the processing in RESULT_MODE. */
  9283. x = gen_lowpart_or_truncate (result_mode, x);
  9284. /* If COMPLEMENT_P is set, we have to complement X before doing the outer
  9285. operation. */
  9286. if (complement_p)
  9287. x = simplify_gen_unary (NOT, result_mode, x, result_mode);
  9288. if (outer_op != UNKNOWN)
  9289. {
  9290. if (GET_RTX_CLASS (outer_op) != RTX_UNARY
  9291. && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
  9292. outer_const = trunc_int_for_mode (outer_const, result_mode);
  9293. if (outer_op == AND)
  9294. x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
  9295. else if (outer_op == SET)
  9296. {
  9297. /* This means that we have determined that the result is
  9298. equivalent to a constant. This should be rare. */
  9299. if (!side_effects_p (x))
  9300. x = GEN_INT (outer_const);
  9301. }
  9302. else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
  9303. x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
  9304. else
  9305. x = simplify_gen_binary (outer_op, result_mode, x,
  9306. GEN_INT (outer_const));
  9307. }
  9308. return x;
  9309. }
  9310. /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
  9311. The result of the shift is RESULT_MODE. If we cannot simplify it,
  9312. return X or, if it is NULL, synthesize the expression with
  9313. simplify_gen_binary. Otherwise, return a simplified value.
  9314. The shift is normally computed in the widest mode we find in VAROP, as
  9315. long as it isn't a different number of words than RESULT_MODE. Exceptions
  9316. are ASHIFTRT and ROTATE, which are always done in their original mode. */
  9317. static rtx
  9318. simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
  9319. rtx varop, int count)
  9320. {
  9321. rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
  9322. if (tem)
  9323. return tem;
  9324. if (!x)
  9325. x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
  9326. if (GET_MODE (x) != result_mode)
  9327. x = gen_lowpart (result_mode, x);
  9328. return x;
  9329. }
  9330. /* Like recog, but we receive the address of a pointer to a new pattern.
  9331. We try to match the rtx that the pointer points to.
  9332. If that fails, we may try to modify or replace the pattern,
  9333. storing the replacement into the same pointer object.
  9334. Modifications include deletion or addition of CLOBBERs.
  9335. PNOTES is a pointer to a location where any REG_UNUSED notes added for
  9336. the CLOBBERs are placed.
  9337. The value is the final insn code from the pattern ultimately matched,
  9338. or -1. */
  9339. static int
  9340. recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
  9341. {
  9342. rtx pat = *pnewpat;
  9343. rtx pat_without_clobbers;
  9344. int insn_code_number;
  9345. int num_clobbers_to_add = 0;
  9346. int i;
  9347. rtx notes = NULL_RTX;
  9348. rtx old_notes, old_pat;
  9349. int old_icode;
  9350. /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
  9351. we use to indicate that something didn't match. If we find such a
  9352. thing, force rejection. */
  9353. if (GET_CODE (pat) == PARALLEL)
  9354. for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
  9355. if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
  9356. && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
  9357. return -1;
  9358. old_pat = PATTERN (insn);
  9359. old_notes = REG_NOTES (insn);
  9360. PATTERN (insn) = pat;
  9361. REG_NOTES (insn) = NULL_RTX;
  9362. insn_code_number = recog (pat, insn, &num_clobbers_to_add);
  9363. if (dump_file && (dump_flags & TDF_DETAILS))
  9364. {
  9365. if (insn_code_number < 0)
  9366. fputs ("Failed to match this instruction:\n", dump_file);
  9367. else
  9368. fputs ("Successfully matched this instruction:\n", dump_file);
  9369. print_rtl_single (dump_file, pat);
  9370. }
  9371. /* If it isn't, there is the possibility that we previously had an insn
  9372. that clobbered some register as a side effect, but the combined
  9373. insn doesn't need to do that. So try once more without the clobbers
  9374. unless this represents an ASM insn. */
  9375. if (insn_code_number < 0 && ! check_asm_operands (pat)
  9376. && GET_CODE (pat) == PARALLEL)
  9377. {
  9378. int pos;
  9379. for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
  9380. if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
  9381. {
  9382. if (i != pos)
  9383. SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
  9384. pos++;
  9385. }
  9386. SUBST_INT (XVECLEN (pat, 0), pos);
  9387. if (pos == 1)
  9388. pat = XVECEXP (pat, 0, 0);
  9389. PATTERN (insn) = pat;
  9390. insn_code_number = recog (pat, insn, &num_clobbers_to_add);
  9391. if (dump_file && (dump_flags & TDF_DETAILS))
  9392. {
  9393. if (insn_code_number < 0)
  9394. fputs ("Failed to match this instruction:\n", dump_file);
  9395. else
  9396. fputs ("Successfully matched this instruction:\n", dump_file);
  9397. print_rtl_single (dump_file, pat);
  9398. }
  9399. }
  9400. pat_without_clobbers = pat;
  9401. PATTERN (insn) = old_pat;
  9402. REG_NOTES (insn) = old_notes;
  9403. /* Recognize all noop sets, these will be killed by followup pass. */
  9404. if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
  9405. insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
  9406. /* If we had any clobbers to add, make a new pattern than contains
  9407. them. Then check to make sure that all of them are dead. */
  9408. if (num_clobbers_to_add)
  9409. {
  9410. rtx newpat = gen_rtx_PARALLEL (VOIDmode,
  9411. rtvec_alloc (GET_CODE (pat) == PARALLEL
  9412. ? (XVECLEN (pat, 0)
  9413. + num_clobbers_to_add)
  9414. : num_clobbers_to_add + 1));
  9415. if (GET_CODE (pat) == PARALLEL)
  9416. for (i = 0; i < XVECLEN (pat, 0); i++)
  9417. XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
  9418. else
  9419. XVECEXP (newpat, 0, 0) = pat;
  9420. add_clobbers (newpat, insn_code_number);
  9421. for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
  9422. i < XVECLEN (newpat, 0); i++)
  9423. {
  9424. if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
  9425. && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
  9426. return -1;
  9427. if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
  9428. {
  9429. gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
  9430. notes = alloc_reg_note (REG_UNUSED,
  9431. XEXP (XVECEXP (newpat, 0, i), 0), notes);
  9432. }
  9433. }
  9434. pat = newpat;
  9435. }
  9436. if (insn_code_number >= 0
  9437. && insn_code_number != NOOP_MOVE_INSN_CODE)
  9438. {
  9439. old_pat = PATTERN (insn);
  9440. old_notes = REG_NOTES (insn);
  9441. old_icode = INSN_CODE (insn);
  9442. PATTERN (insn) = pat;
  9443. REG_NOTES (insn) = notes;
  9444. /* Allow targets to reject combined insn. */
  9445. if (!targetm.legitimate_combined_insn (insn))
  9446. {
  9447. if (dump_file && (dump_flags & TDF_DETAILS))
  9448. fputs ("Instruction not appropriate for target.",
  9449. dump_file);
  9450. /* Callers expect recog_for_combine to strip
  9451. clobbers from the pattern on failure. */
  9452. pat = pat_without_clobbers;
  9453. notes = NULL_RTX;
  9454. insn_code_number = -1;
  9455. }
  9456. PATTERN (insn) = old_pat;
  9457. REG_NOTES (insn) = old_notes;
  9458. INSN_CODE (insn) = old_icode;
  9459. }
  9460. *pnewpat = pat;
  9461. *pnotes = notes;
  9462. return insn_code_number;
  9463. }
  9464. /* Like gen_lowpart_general but for use by combine. In combine it
  9465. is not possible to create any new pseudoregs. However, it is
  9466. safe to create invalid memory addresses, because combine will
  9467. try to recognize them and all they will do is make the combine
  9468. attempt fail.
  9469. If for some reason this cannot do its job, an rtx
  9470. (clobber (const_int 0)) is returned.
  9471. An insn containing that will not be recognized. */
  9472. static rtx
  9473. gen_lowpart_for_combine (machine_mode omode, rtx x)
  9474. {
  9475. machine_mode imode = GET_MODE (x);
  9476. unsigned int osize = GET_MODE_SIZE (omode);
  9477. unsigned int isize = GET_MODE_SIZE (imode);
  9478. rtx result;
  9479. if (omode == imode)
  9480. return x;
  9481. /* We can only support MODE being wider than a word if X is a
  9482. constant integer or has a mode the same size. */
  9483. if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
  9484. && ! (CONST_SCALAR_INT_P (x) || isize == osize))
  9485. goto fail;
  9486. /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
  9487. won't know what to do. So we will strip off the SUBREG here and
  9488. process normally. */
  9489. if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
  9490. {
  9491. x = SUBREG_REG (x);
  9492. /* For use in case we fall down into the address adjustments
  9493. further below, we need to adjust the known mode and size of
  9494. x; imode and isize, since we just adjusted x. */
  9495. imode = GET_MODE (x);
  9496. if (imode == omode)
  9497. return x;
  9498. isize = GET_MODE_SIZE (imode);
  9499. }
  9500. result = gen_lowpart_common (omode, x);
  9501. if (result)
  9502. return result;
  9503. if (MEM_P (x))
  9504. {
  9505. int offset = 0;
  9506. /* Refuse to work on a volatile memory ref or one with a mode-dependent
  9507. address. */
  9508. if (MEM_VOLATILE_P (x)
  9509. || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
  9510. goto fail;
  9511. /* If we want to refer to something bigger than the original memref,
  9512. generate a paradoxical subreg instead. That will force a reload
  9513. of the original memref X. */
  9514. if (isize < osize)
  9515. return gen_rtx_SUBREG (omode, x, 0);
  9516. if (WORDS_BIG_ENDIAN)
  9517. offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
  9518. /* Adjust the address so that the address-after-the-data is
  9519. unchanged. */
  9520. if (BYTES_BIG_ENDIAN)
  9521. offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
  9522. return adjust_address_nv (x, omode, offset);
  9523. }
  9524. /* If X is a comparison operator, rewrite it in a new mode. This
  9525. probably won't match, but may allow further simplifications. */
  9526. else if (COMPARISON_P (x))
  9527. return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
  9528. /* If we couldn't simplify X any other way, just enclose it in a
  9529. SUBREG. Normally, this SUBREG won't match, but some patterns may
  9530. include an explicit SUBREG or we may simplify it further in combine. */
  9531. else
  9532. {
  9533. int offset = 0;
  9534. rtx res;
  9535. offset = subreg_lowpart_offset (omode, imode);
  9536. if (imode == VOIDmode)
  9537. {
  9538. imode = int_mode_for_mode (omode);
  9539. x = gen_lowpart_common (imode, x);
  9540. if (x == NULL)
  9541. goto fail;
  9542. }
  9543. res = simplify_gen_subreg (omode, x, imode, offset);
  9544. if (res)
  9545. return res;
  9546. }
  9547. fail:
  9548. return gen_rtx_CLOBBER (omode, const0_rtx);
  9549. }
  9550. /* Try to simplify a comparison between OP0 and a constant OP1,
  9551. where CODE is the comparison code that will be tested, into a
  9552. (CODE OP0 const0_rtx) form.
  9553. The result is a possibly different comparison code to use.
  9554. *POP1 may be updated. */
  9555. static enum rtx_code
  9556. simplify_compare_const (enum rtx_code code, machine_mode mode,
  9557. rtx op0, rtx *pop1)
  9558. {
  9559. unsigned int mode_width = GET_MODE_PRECISION (mode);
  9560. HOST_WIDE_INT const_op = INTVAL (*pop1);
  9561. /* Get the constant we are comparing against and turn off all bits
  9562. not on in our mode. */
  9563. if (mode != VOIDmode)
  9564. const_op = trunc_int_for_mode (const_op, mode);
  9565. /* If we are comparing against a constant power of two and the value
  9566. being compared can only have that single bit nonzero (e.g., it was
  9567. `and'ed with that bit), we can replace this with a comparison
  9568. with zero. */
  9569. if (const_op
  9570. && (code == EQ || code == NE || code == GE || code == GEU
  9571. || code == LT || code == LTU)
  9572. && mode_width - 1 < HOST_BITS_PER_WIDE_INT
  9573. && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
  9574. && (nonzero_bits (op0, mode)
  9575. == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
  9576. {
  9577. code = (code == EQ || code == GE || code == GEU ? NE : EQ);
  9578. const_op = 0;
  9579. }
  9580. /* Similarly, if we are comparing a value known to be either -1 or
  9581. 0 with -1, change it to the opposite comparison against zero. */
  9582. if (const_op == -1
  9583. && (code == EQ || code == NE || code == GT || code == LE
  9584. || code == GEU || code == LTU)
  9585. && num_sign_bit_copies (op0, mode) == mode_width)
  9586. {
  9587. code = (code == EQ || code == LE || code == GEU ? NE : EQ);
  9588. const_op = 0;
  9589. }
  9590. /* Do some canonicalizations based on the comparison code. We prefer
  9591. comparisons against zero and then prefer equality comparisons.
  9592. If we can reduce the size of a constant, we will do that too. */
  9593. switch (code)
  9594. {
  9595. case LT:
  9596. /* < C is equivalent to <= (C - 1) */
  9597. if (const_op > 0)
  9598. {
  9599. const_op -= 1;
  9600. code = LE;
  9601. /* ... fall through to LE case below. */
  9602. }
  9603. else
  9604. break;
  9605. case LE:
  9606. /* <= C is equivalent to < (C + 1); we do this for C < 0 */
  9607. if (const_op < 0)
  9608. {
  9609. const_op += 1;
  9610. code = LT;
  9611. }
  9612. /* If we are doing a <= 0 comparison on a value known to have
  9613. a zero sign bit, we can replace this with == 0. */
  9614. else if (const_op == 0
  9615. && mode_width - 1 < HOST_BITS_PER_WIDE_INT
  9616. && (nonzero_bits (op0, mode)
  9617. & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
  9618. == 0)
  9619. code = EQ;
  9620. break;
  9621. case GE:
  9622. /* >= C is equivalent to > (C - 1). */
  9623. if (const_op > 0)
  9624. {
  9625. const_op -= 1;
  9626. code = GT;
  9627. /* ... fall through to GT below. */
  9628. }
  9629. else
  9630. break;
  9631. case GT:
  9632. /* > C is equivalent to >= (C + 1); we do this for C < 0. */
  9633. if (const_op < 0)
  9634. {
  9635. const_op += 1;
  9636. code = GE;
  9637. }
  9638. /* If we are doing a > 0 comparison on a value known to have
  9639. a zero sign bit, we can replace this with != 0. */
  9640. else if (const_op == 0
  9641. && mode_width - 1 < HOST_BITS_PER_WIDE_INT
  9642. && (nonzero_bits (op0, mode)
  9643. & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
  9644. == 0)
  9645. code = NE;
  9646. break;
  9647. case LTU:
  9648. /* < C is equivalent to <= (C - 1). */
  9649. if (const_op > 0)
  9650. {
  9651. const_op -= 1;
  9652. code = LEU;
  9653. /* ... fall through ... */
  9654. }
  9655. /* (unsigned) < 0x80000000 is equivalent to >= 0. */
  9656. else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
  9657. && (unsigned HOST_WIDE_INT) const_op
  9658. == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
  9659. {
  9660. const_op = 0;
  9661. code = GE;
  9662. break;
  9663. }
  9664. else
  9665. break;
  9666. case LEU:
  9667. /* unsigned <= 0 is equivalent to == 0 */
  9668. if (const_op == 0)
  9669. code = EQ;
  9670. /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
  9671. else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
  9672. && (unsigned HOST_WIDE_INT) const_op
  9673. == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
  9674. {
  9675. const_op = 0;
  9676. code = GE;
  9677. }
  9678. break;
  9679. case GEU:
  9680. /* >= C is equivalent to > (C - 1). */
  9681. if (const_op > 1)
  9682. {
  9683. const_op -= 1;
  9684. code = GTU;
  9685. /* ... fall through ... */
  9686. }
  9687. /* (unsigned) >= 0x80000000 is equivalent to < 0. */
  9688. else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
  9689. && (unsigned HOST_WIDE_INT) const_op
  9690. == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
  9691. {
  9692. const_op = 0;
  9693. code = LT;
  9694. break;
  9695. }
  9696. else
  9697. break;
  9698. case GTU:
  9699. /* unsigned > 0 is equivalent to != 0 */
  9700. if (const_op == 0)
  9701. code = NE;
  9702. /* (unsigned) > 0x7fffffff is equivalent to < 0. */
  9703. else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
  9704. && (unsigned HOST_WIDE_INT) const_op
  9705. == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
  9706. {
  9707. const_op = 0;
  9708. code = LT;
  9709. }
  9710. break;
  9711. default:
  9712. break;
  9713. }
  9714. *pop1 = GEN_INT (const_op);
  9715. return code;
  9716. }
  9717. /* Simplify a comparison between *POP0 and *POP1 where CODE is the
  9718. comparison code that will be tested.
  9719. The result is a possibly different comparison code to use. *POP0 and
  9720. *POP1 may be updated.
  9721. It is possible that we might detect that a comparison is either always
  9722. true or always false. However, we do not perform general constant
  9723. folding in combine, so this knowledge isn't useful. Such tautologies
  9724. should have been detected earlier. Hence we ignore all such cases. */
  9725. static enum rtx_code
  9726. simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
  9727. {
  9728. rtx op0 = *pop0;
  9729. rtx op1 = *pop1;
  9730. rtx tem, tem1;
  9731. int i;
  9732. machine_mode mode, tmode;
  9733. /* Try a few ways of applying the same transformation to both operands. */
  9734. while (1)
  9735. {
  9736. #ifndef WORD_REGISTER_OPERATIONS
  9737. /* The test below this one won't handle SIGN_EXTENDs on these machines,
  9738. so check specially. */
  9739. if (code != GTU && code != GEU && code != LTU && code != LEU
  9740. && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
  9741. && GET_CODE (XEXP (op0, 0)) == ASHIFT
  9742. && GET_CODE (XEXP (op1, 0)) == ASHIFT
  9743. && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
  9744. && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
  9745. && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
  9746. == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
  9747. && CONST_INT_P (XEXP (op0, 1))
  9748. && XEXP (op0, 1) == XEXP (op1, 1)
  9749. && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
  9750. && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
  9751. && (INTVAL (XEXP (op0, 1))
  9752. == (GET_MODE_PRECISION (GET_MODE (op0))
  9753. - (GET_MODE_PRECISION
  9754. (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
  9755. {
  9756. op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
  9757. op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
  9758. }
  9759. #endif
  9760. /* If both operands are the same constant shift, see if we can ignore the
  9761. shift. We can if the shift is a rotate or if the bits shifted out of
  9762. this shift are known to be zero for both inputs and if the type of
  9763. comparison is compatible with the shift. */
  9764. if (GET_CODE (op0) == GET_CODE (op1)
  9765. && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
  9766. && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
  9767. || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
  9768. && (code != GT && code != LT && code != GE && code != LE))
  9769. || (GET_CODE (op0) == ASHIFTRT
  9770. && (code != GTU && code != LTU
  9771. && code != GEU && code != LEU)))
  9772. && CONST_INT_P (XEXP (op0, 1))
  9773. && INTVAL (XEXP (op0, 1)) >= 0
  9774. && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
  9775. && XEXP (op0, 1) == XEXP (op1, 1))
  9776. {
  9777. machine_mode mode = GET_MODE (op0);
  9778. unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
  9779. int shift_count = INTVAL (XEXP (op0, 1));
  9780. if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
  9781. mask &= (mask >> shift_count) << shift_count;
  9782. else if (GET_CODE (op0) == ASHIFT)
  9783. mask = (mask & (mask << shift_count)) >> shift_count;
  9784. if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
  9785. && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
  9786. op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
  9787. else
  9788. break;
  9789. }
  9790. /* If both operands are AND's of a paradoxical SUBREG by constant, the
  9791. SUBREGs are of the same mode, and, in both cases, the AND would
  9792. be redundant if the comparison was done in the narrower mode,
  9793. do the comparison in the narrower mode (e.g., we are AND'ing with 1
  9794. and the operand's possibly nonzero bits are 0xffffff01; in that case
  9795. if we only care about QImode, we don't need the AND). This case
  9796. occurs if the output mode of an scc insn is not SImode and
  9797. STORE_FLAG_VALUE == 1 (e.g., the 386).
  9798. Similarly, check for a case where the AND's are ZERO_EXTEND
  9799. operations from some narrower mode even though a SUBREG is not
  9800. present. */
  9801. else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
  9802. && CONST_INT_P (XEXP (op0, 1))
  9803. && CONST_INT_P (XEXP (op1, 1)))
  9804. {
  9805. rtx inner_op0 = XEXP (op0, 0);
  9806. rtx inner_op1 = XEXP (op1, 0);
  9807. HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
  9808. HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
  9809. int changed = 0;
  9810. if (paradoxical_subreg_p (inner_op0)
  9811. && GET_CODE (inner_op1) == SUBREG
  9812. && (GET_MODE (SUBREG_REG (inner_op0))
  9813. == GET_MODE (SUBREG_REG (inner_op1)))
  9814. && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
  9815. <= HOST_BITS_PER_WIDE_INT)
  9816. && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
  9817. GET_MODE (SUBREG_REG (inner_op0)))))
  9818. && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
  9819. GET_MODE (SUBREG_REG (inner_op1))))))
  9820. {
  9821. op0 = SUBREG_REG (inner_op0);
  9822. op1 = SUBREG_REG (inner_op1);
  9823. /* The resulting comparison is always unsigned since we masked
  9824. off the original sign bit. */
  9825. code = unsigned_condition (code);
  9826. changed = 1;
  9827. }
  9828. else if (c0 == c1)
  9829. for (tmode = GET_CLASS_NARROWEST_MODE
  9830. (GET_MODE_CLASS (GET_MODE (op0)));
  9831. tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
  9832. if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
  9833. {
  9834. op0 = gen_lowpart (tmode, inner_op0);
  9835. op1 = gen_lowpart (tmode, inner_op1);
  9836. code = unsigned_condition (code);
  9837. changed = 1;
  9838. break;
  9839. }
  9840. if (! changed)
  9841. break;
  9842. }
  9843. /* If both operands are NOT, we can strip off the outer operation
  9844. and adjust the comparison code for swapped operands; similarly for
  9845. NEG, except that this must be an equality comparison. */
  9846. else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
  9847. || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
  9848. && (code == EQ || code == NE)))
  9849. op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
  9850. else
  9851. break;
  9852. }
  9853. /* If the first operand is a constant, swap the operands and adjust the
  9854. comparison code appropriately, but don't do this if the second operand
  9855. is already a constant integer. */
  9856. if (swap_commutative_operands_p (op0, op1))
  9857. {
  9858. tem = op0, op0 = op1, op1 = tem;
  9859. code = swap_condition (code);
  9860. }
  9861. /* We now enter a loop during which we will try to simplify the comparison.
  9862. For the most part, we only are concerned with comparisons with zero,
  9863. but some things may really be comparisons with zero but not start
  9864. out looking that way. */
  9865. while (CONST_INT_P (op1))
  9866. {
  9867. machine_mode mode = GET_MODE (op0);
  9868. unsigned int mode_width = GET_MODE_PRECISION (mode);
  9869. unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
  9870. int equality_comparison_p;
  9871. int sign_bit_comparison_p;
  9872. int unsigned_comparison_p;
  9873. HOST_WIDE_INT const_op;
  9874. /* We only want to handle integral modes. This catches VOIDmode,
  9875. CCmode, and the floating-point modes. An exception is that we
  9876. can handle VOIDmode if OP0 is a COMPARE or a comparison
  9877. operation. */
  9878. if (GET_MODE_CLASS (mode) != MODE_INT
  9879. && ! (mode == VOIDmode
  9880. && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
  9881. break;
  9882. /* Try to simplify the compare to constant, possibly changing the
  9883. comparison op, and/or changing op1 to zero. */
  9884. code = simplify_compare_const (code, mode, op0, &op1);
  9885. const_op = INTVAL (op1);
  9886. /* Compute some predicates to simplify code below. */
  9887. equality_comparison_p = (code == EQ || code == NE);
  9888. sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
  9889. unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
  9890. || code == GEU);
  9891. /* If this is a sign bit comparison and we can do arithmetic in
  9892. MODE, say that we will only be needing the sign bit of OP0. */
  9893. if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
  9894. op0 = force_to_mode (op0, mode,
  9895. (unsigned HOST_WIDE_INT) 1
  9896. << (GET_MODE_PRECISION (mode) - 1),
  9897. 0);
  9898. /* Now try cases based on the opcode of OP0. If none of the cases
  9899. does a "continue", we exit this loop immediately after the
  9900. switch. */
  9901. switch (GET_CODE (op0))
  9902. {
  9903. case ZERO_EXTRACT:
  9904. /* If we are extracting a single bit from a variable position in
  9905. a constant that has only a single bit set and are comparing it
  9906. with zero, we can convert this into an equality comparison
  9907. between the position and the location of the single bit. */
  9908. /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
  9909. have already reduced the shift count modulo the word size. */
  9910. if (!SHIFT_COUNT_TRUNCATED
  9911. && CONST_INT_P (XEXP (op0, 0))
  9912. && XEXP (op0, 1) == const1_rtx
  9913. && equality_comparison_p && const_op == 0
  9914. && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
  9915. {
  9916. if (BITS_BIG_ENDIAN)
  9917. i = BITS_PER_WORD - 1 - i;
  9918. op0 = XEXP (op0, 2);
  9919. op1 = GEN_INT (i);
  9920. const_op = i;
  9921. /* Result is nonzero iff shift count is equal to I. */
  9922. code = reverse_condition (code);
  9923. continue;
  9924. }
  9925. /* ... fall through ... */
  9926. case SIGN_EXTRACT:
  9927. tem = expand_compound_operation (op0);
  9928. if (tem != op0)
  9929. {
  9930. op0 = tem;
  9931. continue;
  9932. }
  9933. break;
  9934. case NOT:
  9935. /* If testing for equality, we can take the NOT of the constant. */
  9936. if (equality_comparison_p
  9937. && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
  9938. {
  9939. op0 = XEXP (op0, 0);
  9940. op1 = tem;
  9941. continue;
  9942. }
  9943. /* If just looking at the sign bit, reverse the sense of the
  9944. comparison. */
  9945. if (sign_bit_comparison_p)
  9946. {
  9947. op0 = XEXP (op0, 0);
  9948. code = (code == GE ? LT : GE);
  9949. continue;
  9950. }
  9951. break;
  9952. case NEG:
  9953. /* If testing for equality, we can take the NEG of the constant. */
  9954. if (equality_comparison_p
  9955. && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
  9956. {
  9957. op0 = XEXP (op0, 0);
  9958. op1 = tem;
  9959. continue;
  9960. }
  9961. /* The remaining cases only apply to comparisons with zero. */
  9962. if (const_op != 0)
  9963. break;
  9964. /* When X is ABS or is known positive,
  9965. (neg X) is < 0 if and only if X != 0. */
  9966. if (sign_bit_comparison_p
  9967. && (GET_CODE (XEXP (op0, 0)) == ABS
  9968. || (mode_width <= HOST_BITS_PER_WIDE_INT
  9969. && (nonzero_bits (XEXP (op0, 0), mode)
  9970. & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
  9971. == 0)))
  9972. {
  9973. op0 = XEXP (op0, 0);
  9974. code = (code == LT ? NE : EQ);
  9975. continue;
  9976. }
  9977. /* If we have NEG of something whose two high-order bits are the
  9978. same, we know that "(-a) < 0" is equivalent to "a > 0". */
  9979. if (num_sign_bit_copies (op0, mode) >= 2)
  9980. {
  9981. op0 = XEXP (op0, 0);
  9982. code = swap_condition (code);
  9983. continue;
  9984. }
  9985. break;
  9986. case ROTATE:
  9987. /* If we are testing equality and our count is a constant, we
  9988. can perform the inverse operation on our RHS. */
  9989. if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
  9990. && (tem = simplify_binary_operation (ROTATERT, mode,
  9991. op1, XEXP (op0, 1))) != 0)
  9992. {
  9993. op0 = XEXP (op0, 0);
  9994. op1 = tem;
  9995. continue;
  9996. }
  9997. /* If we are doing a < 0 or >= 0 comparison, it means we are testing
  9998. a particular bit. Convert it to an AND of a constant of that
  9999. bit. This will be converted into a ZERO_EXTRACT. */
  10000. if (const_op == 0 && sign_bit_comparison_p
  10001. && CONST_INT_P (XEXP (op0, 1))
  10002. && mode_width <= HOST_BITS_PER_WIDE_INT)
  10003. {
  10004. op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
  10005. ((unsigned HOST_WIDE_INT) 1
  10006. << (mode_width - 1
  10007. - INTVAL (XEXP (op0, 1)))));
  10008. code = (code == LT ? NE : EQ);
  10009. continue;
  10010. }
  10011. /* Fall through. */
  10012. case ABS:
  10013. /* ABS is ignorable inside an equality comparison with zero. */
  10014. if (const_op == 0 && equality_comparison_p)
  10015. {
  10016. op0 = XEXP (op0, 0);
  10017. continue;
  10018. }
  10019. break;
  10020. case SIGN_EXTEND:
  10021. /* Can simplify (compare (zero/sign_extend FOO) CONST) to
  10022. (compare FOO CONST) if CONST fits in FOO's mode and we
  10023. are either testing inequality or have an unsigned
  10024. comparison with ZERO_EXTEND or a signed comparison with
  10025. SIGN_EXTEND. But don't do it if we don't have a compare
  10026. insn of the given mode, since we'd have to revert it
  10027. later on, and then we wouldn't know whether to sign- or
  10028. zero-extend. */
  10029. mode = GET_MODE (XEXP (op0, 0));
  10030. if (GET_MODE_CLASS (mode) == MODE_INT
  10031. && ! unsigned_comparison_p
  10032. && HWI_COMPUTABLE_MODE_P (mode)
  10033. && trunc_int_for_mode (const_op, mode) == const_op
  10034. && have_insn_for (COMPARE, mode))
  10035. {
  10036. op0 = XEXP (op0, 0);
  10037. continue;
  10038. }
  10039. break;
  10040. case SUBREG:
  10041. /* Check for the case where we are comparing A - C1 with C2, that is
  10042. (subreg:MODE (plus (A) (-C1))) op (C2)
  10043. with C1 a constant, and try to lift the SUBREG, i.e. to do the
  10044. comparison in the wider mode. One of the following two conditions
  10045. must be true in order for this to be valid:
  10046. 1. The mode extension results in the same bit pattern being added
  10047. on both sides and the comparison is equality or unsigned. As
  10048. C2 has been truncated to fit in MODE, the pattern can only be
  10049. all 0s or all 1s.
  10050. 2. The mode extension results in the sign bit being copied on
  10051. each side.
  10052. The difficulty here is that we have predicates for A but not for
  10053. (A - C1) so we need to check that C1 is within proper bounds so
  10054. as to perturbate A as little as possible. */
  10055. if (mode_width <= HOST_BITS_PER_WIDE_INT
  10056. && subreg_lowpart_p (op0)
  10057. && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
  10058. && GET_CODE (SUBREG_REG (op0)) == PLUS
  10059. && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
  10060. {
  10061. machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
  10062. rtx a = XEXP (SUBREG_REG (op0), 0);
  10063. HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
  10064. if ((c1 > 0
  10065. && (unsigned HOST_WIDE_INT) c1
  10066. < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
  10067. && (equality_comparison_p || unsigned_comparison_p)
  10068. /* (A - C1) zero-extends if it is positive and sign-extends
  10069. if it is negative, C2 both zero- and sign-extends. */
  10070. && ((0 == (nonzero_bits (a, inner_mode)
  10071. & ~GET_MODE_MASK (mode))
  10072. && const_op >= 0)
  10073. /* (A - C1) sign-extends if it is positive and 1-extends
  10074. if it is negative, C2 both sign- and 1-extends. */
  10075. || (num_sign_bit_copies (a, inner_mode)
  10076. > (unsigned int) (GET_MODE_PRECISION (inner_mode)
  10077. - mode_width)
  10078. && const_op < 0)))
  10079. || ((unsigned HOST_WIDE_INT) c1
  10080. < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
  10081. /* (A - C1) always sign-extends, like C2. */
  10082. && num_sign_bit_copies (a, inner_mode)
  10083. > (unsigned int) (GET_MODE_PRECISION (inner_mode)
  10084. - (mode_width - 1))))
  10085. {
  10086. op0 = SUBREG_REG (op0);
  10087. continue;
  10088. }
  10089. }
  10090. /* If the inner mode is narrower and we are extracting the low part,
  10091. we can treat the SUBREG as if it were a ZERO_EXTEND. */
  10092. if (subreg_lowpart_p (op0)
  10093. && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
  10094. /* Fall through */ ;
  10095. else
  10096. break;
  10097. /* ... fall through ... */
  10098. case ZERO_EXTEND:
  10099. mode = GET_MODE (XEXP (op0, 0));
  10100. if (GET_MODE_CLASS (mode) == MODE_INT
  10101. && (unsigned_comparison_p || equality_comparison_p)
  10102. && HWI_COMPUTABLE_MODE_P (mode)
  10103. && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
  10104. && const_op >= 0
  10105. && have_insn_for (COMPARE, mode))
  10106. {
  10107. op0 = XEXP (op0, 0);
  10108. continue;
  10109. }
  10110. break;
  10111. case PLUS:
  10112. /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
  10113. this for equality comparisons due to pathological cases involving
  10114. overflows. */
  10115. if (equality_comparison_p
  10116. && 0 != (tem = simplify_binary_operation (MINUS, mode,
  10117. op1, XEXP (op0, 1))))
  10118. {
  10119. op0 = XEXP (op0, 0);
  10120. op1 = tem;
  10121. continue;
  10122. }
  10123. /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
  10124. if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
  10125. && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
  10126. {
  10127. op0 = XEXP (XEXP (op0, 0), 0);
  10128. code = (code == LT ? EQ : NE);
  10129. continue;
  10130. }
  10131. break;
  10132. case MINUS:
  10133. /* We used to optimize signed comparisons against zero, but that
  10134. was incorrect. Unsigned comparisons against zero (GTU, LEU)
  10135. arrive here as equality comparisons, or (GEU, LTU) are
  10136. optimized away. No need to special-case them. */
  10137. /* (eq (minus A B) C) -> (eq A (plus B C)) or
  10138. (eq B (minus A C)), whichever simplifies. We can only do
  10139. this for equality comparisons due to pathological cases involving
  10140. overflows. */
  10141. if (equality_comparison_p
  10142. && 0 != (tem = simplify_binary_operation (PLUS, mode,
  10143. XEXP (op0, 1), op1)))
  10144. {
  10145. op0 = XEXP (op0, 0);
  10146. op1 = tem;
  10147. continue;
  10148. }
  10149. if (equality_comparison_p
  10150. && 0 != (tem = simplify_binary_operation (MINUS, mode,
  10151. XEXP (op0, 0), op1)))
  10152. {
  10153. op0 = XEXP (op0, 1);
  10154. op1 = tem;
  10155. continue;
  10156. }
  10157. /* The sign bit of (minus (ashiftrt X C) X), where C is the number
  10158. of bits in X minus 1, is one iff X > 0. */
  10159. if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
  10160. && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
  10161. && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
  10162. && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
  10163. {
  10164. op0 = XEXP (op0, 1);
  10165. code = (code == GE ? LE : GT);
  10166. continue;
  10167. }
  10168. break;
  10169. case XOR:
  10170. /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
  10171. if C is zero or B is a constant. */
  10172. if (equality_comparison_p
  10173. && 0 != (tem = simplify_binary_operation (XOR, mode,
  10174. XEXP (op0, 1), op1)))
  10175. {
  10176. op0 = XEXP (op0, 0);
  10177. op1 = tem;
  10178. continue;
  10179. }
  10180. break;
  10181. case EQ: case NE:
  10182. case UNEQ: case LTGT:
  10183. case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
  10184. case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
  10185. case UNORDERED: case ORDERED:
  10186. /* We can't do anything if OP0 is a condition code value, rather
  10187. than an actual data value. */
  10188. if (const_op != 0
  10189. || CC0_P (XEXP (op0, 0))
  10190. || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
  10191. break;
  10192. /* Get the two operands being compared. */
  10193. if (GET_CODE (XEXP (op0, 0)) == COMPARE)
  10194. tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
  10195. else
  10196. tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
  10197. /* Check for the cases where we simply want the result of the
  10198. earlier test or the opposite of that result. */
  10199. if (code == NE || code == EQ
  10200. || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
  10201. && (code == LT || code == GE)))
  10202. {
  10203. enum rtx_code new_code;
  10204. if (code == LT || code == NE)
  10205. new_code = GET_CODE (op0);
  10206. else
  10207. new_code = reversed_comparison_code (op0, NULL);
  10208. if (new_code != UNKNOWN)
  10209. {
  10210. code = new_code;
  10211. op0 = tem;
  10212. op1 = tem1;
  10213. continue;
  10214. }
  10215. }
  10216. break;
  10217. case IOR:
  10218. /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
  10219. iff X <= 0. */
  10220. if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
  10221. && XEXP (XEXP (op0, 0), 1) == constm1_rtx
  10222. && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
  10223. {
  10224. op0 = XEXP (op0, 1);
  10225. code = (code == GE ? GT : LE);
  10226. continue;
  10227. }
  10228. break;
  10229. case AND:
  10230. /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
  10231. will be converted to a ZERO_EXTRACT later. */
  10232. if (const_op == 0 && equality_comparison_p
  10233. && GET_CODE (XEXP (op0, 0)) == ASHIFT
  10234. && XEXP (XEXP (op0, 0), 0) == const1_rtx)
  10235. {
  10236. op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
  10237. XEXP (XEXP (op0, 0), 1));
  10238. op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
  10239. continue;
  10240. }
  10241. /* If we are comparing (and (lshiftrt X C1) C2) for equality with
  10242. zero and X is a comparison and C1 and C2 describe only bits set
  10243. in STORE_FLAG_VALUE, we can compare with X. */
  10244. if (const_op == 0 && equality_comparison_p
  10245. && mode_width <= HOST_BITS_PER_WIDE_INT
  10246. && CONST_INT_P (XEXP (op0, 1))
  10247. && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
  10248. && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
  10249. && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
  10250. && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
  10251. {
  10252. mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
  10253. << INTVAL (XEXP (XEXP (op0, 0), 1)));
  10254. if ((~STORE_FLAG_VALUE & mask) == 0
  10255. && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
  10256. || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
  10257. && COMPARISON_P (tem))))
  10258. {
  10259. op0 = XEXP (XEXP (op0, 0), 0);
  10260. continue;
  10261. }
  10262. }
  10263. /* If we are doing an equality comparison of an AND of a bit equal
  10264. to the sign bit, replace this with a LT or GE comparison of
  10265. the underlying value. */
  10266. if (equality_comparison_p
  10267. && const_op == 0
  10268. && CONST_INT_P (XEXP (op0, 1))
  10269. && mode_width <= HOST_BITS_PER_WIDE_INT
  10270. && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
  10271. == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
  10272. {
  10273. op0 = XEXP (op0, 0);
  10274. code = (code == EQ ? GE : LT);
  10275. continue;
  10276. }
  10277. /* If this AND operation is really a ZERO_EXTEND from a narrower
  10278. mode, the constant fits within that mode, and this is either an
  10279. equality or unsigned comparison, try to do this comparison in
  10280. the narrower mode.
  10281. Note that in:
  10282. (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
  10283. -> (ne:DI (reg:SI 4) (const_int 0))
  10284. unless TRULY_NOOP_TRUNCATION allows it or the register is
  10285. known to hold a value of the required mode the
  10286. transformation is invalid. */
  10287. if ((equality_comparison_p || unsigned_comparison_p)
  10288. && CONST_INT_P (XEXP (op0, 1))
  10289. && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
  10290. & GET_MODE_MASK (mode))
  10291. + 1)) >= 0
  10292. && const_op >> i == 0
  10293. && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
  10294. && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
  10295. || (REG_P (XEXP (op0, 0))
  10296. && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
  10297. {
  10298. op0 = gen_lowpart (tmode, XEXP (op0, 0));
  10299. continue;
  10300. }
  10301. /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
  10302. fits in both M1 and M2 and the SUBREG is either paradoxical
  10303. or represents the low part, permute the SUBREG and the AND
  10304. and try again. */
  10305. if (GET_CODE (XEXP (op0, 0)) == SUBREG)
  10306. {
  10307. unsigned HOST_WIDE_INT c1;
  10308. tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
  10309. /* Require an integral mode, to avoid creating something like
  10310. (AND:SF ...). */
  10311. if (SCALAR_INT_MODE_P (tmode)
  10312. /* It is unsafe to commute the AND into the SUBREG if the
  10313. SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
  10314. not defined. As originally written the upper bits
  10315. have a defined value due to the AND operation.
  10316. However, if we commute the AND inside the SUBREG then
  10317. they no longer have defined values and the meaning of
  10318. the code has been changed. */
  10319. && (0
  10320. #ifdef WORD_REGISTER_OPERATIONS
  10321. || (mode_width > GET_MODE_PRECISION (tmode)
  10322. && mode_width <= BITS_PER_WORD)
  10323. #endif
  10324. || (mode_width <= GET_MODE_PRECISION (tmode)
  10325. && subreg_lowpart_p (XEXP (op0, 0))))
  10326. && CONST_INT_P (XEXP (op0, 1))
  10327. && mode_width <= HOST_BITS_PER_WIDE_INT
  10328. && HWI_COMPUTABLE_MODE_P (tmode)
  10329. && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
  10330. && (c1 & ~GET_MODE_MASK (tmode)) == 0
  10331. && c1 != mask
  10332. && c1 != GET_MODE_MASK (tmode))
  10333. {
  10334. op0 = simplify_gen_binary (AND, tmode,
  10335. SUBREG_REG (XEXP (op0, 0)),
  10336. gen_int_mode (c1, tmode));
  10337. op0 = gen_lowpart (mode, op0);
  10338. continue;
  10339. }
  10340. }
  10341. /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
  10342. if (const_op == 0 && equality_comparison_p
  10343. && XEXP (op0, 1) == const1_rtx
  10344. && GET_CODE (XEXP (op0, 0)) == NOT)
  10345. {
  10346. op0 = simplify_and_const_int (NULL_RTX, mode,
  10347. XEXP (XEXP (op0, 0), 0), 1);
  10348. code = (code == NE ? EQ : NE);
  10349. continue;
  10350. }
  10351. /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
  10352. (eq (and (lshiftrt X) 1) 0).
  10353. Also handle the case where (not X) is expressed using xor. */
  10354. if (const_op == 0 && equality_comparison_p
  10355. && XEXP (op0, 1) == const1_rtx
  10356. && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
  10357. {
  10358. rtx shift_op = XEXP (XEXP (op0, 0), 0);
  10359. rtx shift_count = XEXP (XEXP (op0, 0), 1);
  10360. if (GET_CODE (shift_op) == NOT
  10361. || (GET_CODE (shift_op) == XOR
  10362. && CONST_INT_P (XEXP (shift_op, 1))
  10363. && CONST_INT_P (shift_count)
  10364. && HWI_COMPUTABLE_MODE_P (mode)
  10365. && (UINTVAL (XEXP (shift_op, 1))
  10366. == (unsigned HOST_WIDE_INT) 1
  10367. << INTVAL (shift_count))))
  10368. {
  10369. op0
  10370. = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
  10371. op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
  10372. code = (code == NE ? EQ : NE);
  10373. continue;
  10374. }
  10375. }
  10376. break;
  10377. case ASHIFT:
  10378. /* If we have (compare (ashift FOO N) (const_int C)) and
  10379. the high order N bits of FOO (N+1 if an inequality comparison)
  10380. are known to be zero, we can do this by comparing FOO with C
  10381. shifted right N bits so long as the low-order N bits of C are
  10382. zero. */
  10383. if (CONST_INT_P (XEXP (op0, 1))
  10384. && INTVAL (XEXP (op0, 1)) >= 0
  10385. && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
  10386. < HOST_BITS_PER_WIDE_INT)
  10387. && (((unsigned HOST_WIDE_INT) const_op
  10388. & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
  10389. - 1)) == 0)
  10390. && mode_width <= HOST_BITS_PER_WIDE_INT
  10391. && (nonzero_bits (XEXP (op0, 0), mode)
  10392. & ~(mask >> (INTVAL (XEXP (op0, 1))
  10393. + ! equality_comparison_p))) == 0)
  10394. {
  10395. /* We must perform a logical shift, not an arithmetic one,
  10396. as we want the top N bits of C to be zero. */
  10397. unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
  10398. temp >>= INTVAL (XEXP (op0, 1));
  10399. op1 = gen_int_mode (temp, mode);
  10400. op0 = XEXP (op0, 0);
  10401. continue;
  10402. }
  10403. /* If we are doing a sign bit comparison, it means we are testing
  10404. a particular bit. Convert it to the appropriate AND. */
  10405. if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
  10406. && mode_width <= HOST_BITS_PER_WIDE_INT)
  10407. {
  10408. op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
  10409. ((unsigned HOST_WIDE_INT) 1
  10410. << (mode_width - 1
  10411. - INTVAL (XEXP (op0, 1)))));
  10412. code = (code == LT ? NE : EQ);
  10413. continue;
  10414. }
  10415. /* If this an equality comparison with zero and we are shifting
  10416. the low bit to the sign bit, we can convert this to an AND of the
  10417. low-order bit. */
  10418. if (const_op == 0 && equality_comparison_p
  10419. && CONST_INT_P (XEXP (op0, 1))
  10420. && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
  10421. {
  10422. op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
  10423. continue;
  10424. }
  10425. break;
  10426. case ASHIFTRT:
  10427. /* If this is an equality comparison with zero, we can do this
  10428. as a logical shift, which might be much simpler. */
  10429. if (equality_comparison_p && const_op == 0
  10430. && CONST_INT_P (XEXP (op0, 1)))
  10431. {
  10432. op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
  10433. XEXP (op0, 0),
  10434. INTVAL (XEXP (op0, 1)));
  10435. continue;
  10436. }
  10437. /* If OP0 is a sign extension and CODE is not an unsigned comparison,
  10438. do the comparison in a narrower mode. */
  10439. if (! unsigned_comparison_p
  10440. && CONST_INT_P (XEXP (op0, 1))
  10441. && GET_CODE (XEXP (op0, 0)) == ASHIFT
  10442. && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
  10443. && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
  10444. MODE_INT, 1)) != BLKmode
  10445. && (((unsigned HOST_WIDE_INT) const_op
  10446. + (GET_MODE_MASK (tmode) >> 1) + 1)
  10447. <= GET_MODE_MASK (tmode)))
  10448. {
  10449. op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
  10450. continue;
  10451. }
  10452. /* Likewise if OP0 is a PLUS of a sign extension with a
  10453. constant, which is usually represented with the PLUS
  10454. between the shifts. */
  10455. if (! unsigned_comparison_p
  10456. && CONST_INT_P (XEXP (op0, 1))
  10457. && GET_CODE (XEXP (op0, 0)) == PLUS
  10458. && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
  10459. && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
  10460. && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
  10461. && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
  10462. MODE_INT, 1)) != BLKmode
  10463. && (((unsigned HOST_WIDE_INT) const_op
  10464. + (GET_MODE_MASK (tmode) >> 1) + 1)
  10465. <= GET_MODE_MASK (tmode)))
  10466. {
  10467. rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
  10468. rtx add_const = XEXP (XEXP (op0, 0), 1);
  10469. rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
  10470. add_const, XEXP (op0, 1));
  10471. op0 = simplify_gen_binary (PLUS, tmode,
  10472. gen_lowpart (tmode, inner),
  10473. new_const);
  10474. continue;
  10475. }
  10476. /* ... fall through ... */
  10477. case LSHIFTRT:
  10478. /* If we have (compare (xshiftrt FOO N) (const_int C)) and
  10479. the low order N bits of FOO are known to be zero, we can do this
  10480. by comparing FOO with C shifted left N bits so long as no
  10481. overflow occurs. Even if the low order N bits of FOO aren't known
  10482. to be zero, if the comparison is >= or < we can use the same
  10483. optimization and for > or <= by setting all the low
  10484. order N bits in the comparison constant. */
  10485. if (CONST_INT_P (XEXP (op0, 1))
  10486. && INTVAL (XEXP (op0, 1)) > 0
  10487. && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
  10488. && mode_width <= HOST_BITS_PER_WIDE_INT
  10489. && (((unsigned HOST_WIDE_INT) const_op
  10490. + (GET_CODE (op0) != LSHIFTRT
  10491. ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
  10492. + 1)
  10493. : 0))
  10494. <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
  10495. {
  10496. unsigned HOST_WIDE_INT low_bits
  10497. = (nonzero_bits (XEXP (op0, 0), mode)
  10498. & (((unsigned HOST_WIDE_INT) 1
  10499. << INTVAL (XEXP (op0, 1))) - 1));
  10500. if (low_bits == 0 || !equality_comparison_p)
  10501. {
  10502. /* If the shift was logical, then we must make the condition
  10503. unsigned. */
  10504. if (GET_CODE (op0) == LSHIFTRT)
  10505. code = unsigned_condition (code);
  10506. const_op <<= INTVAL (XEXP (op0, 1));
  10507. if (low_bits != 0
  10508. && (code == GT || code == GTU
  10509. || code == LE || code == LEU))
  10510. const_op
  10511. |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
  10512. op1 = GEN_INT (const_op);
  10513. op0 = XEXP (op0, 0);
  10514. continue;
  10515. }
  10516. }
  10517. /* If we are using this shift to extract just the sign bit, we
  10518. can replace this with an LT or GE comparison. */
  10519. if (const_op == 0
  10520. && (equality_comparison_p || sign_bit_comparison_p)
  10521. && CONST_INT_P (XEXP (op0, 1))
  10522. && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
  10523. {
  10524. op0 = XEXP (op0, 0);
  10525. code = (code == NE || code == GT ? LT : GE);
  10526. continue;
  10527. }
  10528. break;
  10529. default:
  10530. break;
  10531. }
  10532. break;
  10533. }
  10534. /* Now make any compound operations involved in this comparison. Then,
  10535. check for an outmost SUBREG on OP0 that is not doing anything or is
  10536. paradoxical. The latter transformation must only be performed when
  10537. it is known that the "extra" bits will be the same in op0 and op1 or
  10538. that they don't matter. There are three cases to consider:
  10539. 1. SUBREG_REG (op0) is a register. In this case the bits are don't
  10540. care bits and we can assume they have any convenient value. So
  10541. making the transformation is safe.
  10542. 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
  10543. In this case the upper bits of op0 are undefined. We should not make
  10544. the simplification in that case as we do not know the contents of
  10545. those bits.
  10546. 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
  10547. UNKNOWN. In that case we know those bits are zeros or ones. We must
  10548. also be sure that they are the same as the upper bits of op1.
  10549. We can never remove a SUBREG for a non-equality comparison because
  10550. the sign bit is in a different place in the underlying object. */
  10551. op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
  10552. op1 = make_compound_operation (op1, SET);
  10553. if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
  10554. && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
  10555. && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
  10556. && (code == NE || code == EQ))
  10557. {
  10558. if (paradoxical_subreg_p (op0))
  10559. {
  10560. /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
  10561. implemented. */
  10562. if (REG_P (SUBREG_REG (op0)))
  10563. {
  10564. op0 = SUBREG_REG (op0);
  10565. op1 = gen_lowpart (GET_MODE (op0), op1);
  10566. }
  10567. }
  10568. else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
  10569. <= HOST_BITS_PER_WIDE_INT)
  10570. && (nonzero_bits (SUBREG_REG (op0),
  10571. GET_MODE (SUBREG_REG (op0)))
  10572. & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
  10573. {
  10574. tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
  10575. if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
  10576. & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
  10577. op0 = SUBREG_REG (op0), op1 = tem;
  10578. }
  10579. }
  10580. /* We now do the opposite procedure: Some machines don't have compare
  10581. insns in all modes. If OP0's mode is an integer mode smaller than a
  10582. word and we can't do a compare in that mode, see if there is a larger
  10583. mode for which we can do the compare. There are a number of cases in
  10584. which we can use the wider mode. */
  10585. mode = GET_MODE (op0);
  10586. if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
  10587. && GET_MODE_SIZE (mode) < UNITS_PER_WORD
  10588. && ! have_insn_for (COMPARE, mode))
  10589. for (tmode = GET_MODE_WIDER_MODE (mode);
  10590. (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
  10591. tmode = GET_MODE_WIDER_MODE (tmode))
  10592. if (have_insn_for (COMPARE, tmode))
  10593. {
  10594. int zero_extended;
  10595. /* If this is a test for negative, we can make an explicit
  10596. test of the sign bit. Test this first so we can use
  10597. a paradoxical subreg to extend OP0. */
  10598. if (op1 == const0_rtx && (code == LT || code == GE)
  10599. && HWI_COMPUTABLE_MODE_P (mode))
  10600. {
  10601. unsigned HOST_WIDE_INT sign
  10602. = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
  10603. op0 = simplify_gen_binary (AND, tmode,
  10604. gen_lowpart (tmode, op0),
  10605. gen_int_mode (sign, tmode));
  10606. code = (code == LT) ? NE : EQ;
  10607. break;
  10608. }
  10609. /* If the only nonzero bits in OP0 and OP1 are those in the
  10610. narrower mode and this is an equality or unsigned comparison,
  10611. we can use the wider mode. Similarly for sign-extended
  10612. values, in which case it is true for all comparisons. */
  10613. zero_extended = ((code == EQ || code == NE
  10614. || code == GEU || code == GTU
  10615. || code == LEU || code == LTU)
  10616. && (nonzero_bits (op0, tmode)
  10617. & ~GET_MODE_MASK (mode)) == 0
  10618. && ((CONST_INT_P (op1)
  10619. || (nonzero_bits (op1, tmode)
  10620. & ~GET_MODE_MASK (mode)) == 0)));
  10621. if (zero_extended
  10622. || ((num_sign_bit_copies (op0, tmode)
  10623. > (unsigned int) (GET_MODE_PRECISION (tmode)
  10624. - GET_MODE_PRECISION (mode)))
  10625. && (num_sign_bit_copies (op1, tmode)
  10626. > (unsigned int) (GET_MODE_PRECISION (tmode)
  10627. - GET_MODE_PRECISION (mode)))))
  10628. {
  10629. /* If OP0 is an AND and we don't have an AND in MODE either,
  10630. make a new AND in the proper mode. */
  10631. if (GET_CODE (op0) == AND
  10632. && !have_insn_for (AND, mode))
  10633. op0 = simplify_gen_binary (AND, tmode,
  10634. gen_lowpart (tmode,
  10635. XEXP (op0, 0)),
  10636. gen_lowpart (tmode,
  10637. XEXP (op0, 1)));
  10638. else
  10639. {
  10640. if (zero_extended)
  10641. {
  10642. op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
  10643. op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
  10644. }
  10645. else
  10646. {
  10647. op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
  10648. op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
  10649. }
  10650. break;
  10651. }
  10652. }
  10653. }
  10654. /* We may have changed the comparison operands. Re-canonicalize. */
  10655. if (swap_commutative_operands_p (op0, op1))
  10656. {
  10657. tem = op0, op0 = op1, op1 = tem;
  10658. code = swap_condition (code);
  10659. }
  10660. /* If this machine only supports a subset of valid comparisons, see if we
  10661. can convert an unsupported one into a supported one. */
  10662. target_canonicalize_comparison (&code, &op0, &op1, 0);
  10663. *pop0 = op0;
  10664. *pop1 = op1;
  10665. return code;
  10666. }
  10667. /* Utility function for record_value_for_reg. Count number of
  10668. rtxs in X. */
  10669. static int
  10670. count_rtxs (rtx x)
  10671. {
  10672. enum rtx_code code = GET_CODE (x);
  10673. const char *fmt;
  10674. int i, j, ret = 1;
  10675. if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
  10676. || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
  10677. {
  10678. rtx x0 = XEXP (x, 0);
  10679. rtx x1 = XEXP (x, 1);
  10680. if (x0 == x1)
  10681. return 1 + 2 * count_rtxs (x0);
  10682. if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
  10683. || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
  10684. && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
  10685. return 2 + 2 * count_rtxs (x0)
  10686. + count_rtxs (x == XEXP (x1, 0)
  10687. ? XEXP (x1, 1) : XEXP (x1, 0));
  10688. if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
  10689. || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
  10690. && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
  10691. return 2 + 2 * count_rtxs (x1)
  10692. + count_rtxs (x == XEXP (x0, 0)
  10693. ? XEXP (x0, 1) : XEXP (x0, 0));
  10694. }
  10695. fmt = GET_RTX_FORMAT (code);
  10696. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  10697. if (fmt[i] == 'e')
  10698. ret += count_rtxs (XEXP (x, i));
  10699. else if (fmt[i] == 'E')
  10700. for (j = 0; j < XVECLEN (x, i); j++)
  10701. ret += count_rtxs (XVECEXP (x, i, j));
  10702. return ret;
  10703. }
  10704. /* Utility function for following routine. Called when X is part of a value
  10705. being stored into last_set_value. Sets last_set_table_tick
  10706. for each register mentioned. Similar to mention_regs in cse.c */
  10707. static void
  10708. update_table_tick (rtx x)
  10709. {
  10710. enum rtx_code code = GET_CODE (x);
  10711. const char *fmt = GET_RTX_FORMAT (code);
  10712. int i, j;
  10713. if (code == REG)
  10714. {
  10715. unsigned int regno = REGNO (x);
  10716. unsigned int endregno = END_REGNO (x);
  10717. unsigned int r;
  10718. for (r = regno; r < endregno; r++)
  10719. {
  10720. reg_stat_type *rsp = &reg_stat[r];
  10721. rsp->last_set_table_tick = label_tick;
  10722. }
  10723. return;
  10724. }
  10725. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  10726. if (fmt[i] == 'e')
  10727. {
  10728. /* Check for identical subexpressions. If x contains
  10729. identical subexpression we only have to traverse one of
  10730. them. */
  10731. if (i == 0 && ARITHMETIC_P (x))
  10732. {
  10733. /* Note that at this point x1 has already been
  10734. processed. */
  10735. rtx x0 = XEXP (x, 0);
  10736. rtx x1 = XEXP (x, 1);
  10737. /* If x0 and x1 are identical then there is no need to
  10738. process x0. */
  10739. if (x0 == x1)
  10740. break;
  10741. /* If x0 is identical to a subexpression of x1 then while
  10742. processing x1, x0 has already been processed. Thus we
  10743. are done with x. */
  10744. if (ARITHMETIC_P (x1)
  10745. && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
  10746. break;
  10747. /* If x1 is identical to a subexpression of x0 then we
  10748. still have to process the rest of x0. */
  10749. if (ARITHMETIC_P (x0)
  10750. && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
  10751. {
  10752. update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
  10753. break;
  10754. }
  10755. }
  10756. update_table_tick (XEXP (x, i));
  10757. }
  10758. else if (fmt[i] == 'E')
  10759. for (j = 0; j < XVECLEN (x, i); j++)
  10760. update_table_tick (XVECEXP (x, i, j));
  10761. }
  10762. /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
  10763. are saying that the register is clobbered and we no longer know its
  10764. value. If INSN is zero, don't update reg_stat[].last_set; this is
  10765. only permitted with VALUE also zero and is used to invalidate the
  10766. register. */
  10767. static void
  10768. record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
  10769. {
  10770. unsigned int regno = REGNO (reg);
  10771. unsigned int endregno = END_REGNO (reg);
  10772. unsigned int i;
  10773. reg_stat_type *rsp;
  10774. /* If VALUE contains REG and we have a previous value for REG, substitute
  10775. the previous value. */
  10776. if (value && insn && reg_overlap_mentioned_p (reg, value))
  10777. {
  10778. rtx tem;
  10779. /* Set things up so get_last_value is allowed to see anything set up to
  10780. our insn. */
  10781. subst_low_luid = DF_INSN_LUID (insn);
  10782. tem = get_last_value (reg);
  10783. /* If TEM is simply a binary operation with two CLOBBERs as operands,
  10784. it isn't going to be useful and will take a lot of time to process,
  10785. so just use the CLOBBER. */
  10786. if (tem)
  10787. {
  10788. if (ARITHMETIC_P (tem)
  10789. && GET_CODE (XEXP (tem, 0)) == CLOBBER
  10790. && GET_CODE (XEXP (tem, 1)) == CLOBBER)
  10791. tem = XEXP (tem, 0);
  10792. else if (count_occurrences (value, reg, 1) >= 2)
  10793. {
  10794. /* If there are two or more occurrences of REG in VALUE,
  10795. prevent the value from growing too much. */
  10796. if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
  10797. tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
  10798. }
  10799. value = replace_rtx (copy_rtx (value), reg, tem);
  10800. }
  10801. }
  10802. /* For each register modified, show we don't know its value, that
  10803. we don't know about its bitwise content, that its value has been
  10804. updated, and that we don't know the location of the death of the
  10805. register. */
  10806. for (i = regno; i < endregno; i++)
  10807. {
  10808. rsp = &reg_stat[i];
  10809. if (insn)
  10810. rsp->last_set = insn;
  10811. rsp->last_set_value = 0;
  10812. rsp->last_set_mode = VOIDmode;
  10813. rsp->last_set_nonzero_bits = 0;
  10814. rsp->last_set_sign_bit_copies = 0;
  10815. rsp->last_death = 0;
  10816. rsp->truncated_to_mode = VOIDmode;
  10817. }
  10818. /* Mark registers that are being referenced in this value. */
  10819. if (value)
  10820. update_table_tick (value);
  10821. /* Now update the status of each register being set.
  10822. If someone is using this register in this block, set this register
  10823. to invalid since we will get confused between the two lives in this
  10824. basic block. This makes using this register always invalid. In cse, we
  10825. scan the table to invalidate all entries using this register, but this
  10826. is too much work for us. */
  10827. for (i = regno; i < endregno; i++)
  10828. {
  10829. rsp = &reg_stat[i];
  10830. rsp->last_set_label = label_tick;
  10831. if (!insn
  10832. || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
  10833. rsp->last_set_invalid = 1;
  10834. else
  10835. rsp->last_set_invalid = 0;
  10836. }
  10837. /* The value being assigned might refer to X (like in "x++;"). In that
  10838. case, we must replace it with (clobber (const_int 0)) to prevent
  10839. infinite loops. */
  10840. rsp = &reg_stat[regno];
  10841. if (value && !get_last_value_validate (&value, insn, label_tick, 0))
  10842. {
  10843. value = copy_rtx (value);
  10844. if (!get_last_value_validate (&value, insn, label_tick, 1))
  10845. value = 0;
  10846. }
  10847. /* For the main register being modified, update the value, the mode, the
  10848. nonzero bits, and the number of sign bit copies. */
  10849. rsp->last_set_value = value;
  10850. if (value)
  10851. {
  10852. machine_mode mode = GET_MODE (reg);
  10853. subst_low_luid = DF_INSN_LUID (insn);
  10854. rsp->last_set_mode = mode;
  10855. if (GET_MODE_CLASS (mode) == MODE_INT
  10856. && HWI_COMPUTABLE_MODE_P (mode))
  10857. mode = nonzero_bits_mode;
  10858. rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
  10859. rsp->last_set_sign_bit_copies
  10860. = num_sign_bit_copies (value, GET_MODE (reg));
  10861. }
  10862. }
  10863. /* Called via note_stores from record_dead_and_set_regs to handle one
  10864. SET or CLOBBER in an insn. DATA is the instruction in which the
  10865. set is occurring. */
  10866. static void
  10867. record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
  10868. {
  10869. rtx_insn *record_dead_insn = (rtx_insn *) data;
  10870. if (GET_CODE (dest) == SUBREG)
  10871. dest = SUBREG_REG (dest);
  10872. if (!record_dead_insn)
  10873. {
  10874. if (REG_P (dest))
  10875. record_value_for_reg (dest, NULL, NULL_RTX);
  10876. return;
  10877. }
  10878. if (REG_P (dest))
  10879. {
  10880. /* If we are setting the whole register, we know its value. Otherwise
  10881. show that we don't know the value. We can handle SUBREG in
  10882. some cases. */
  10883. if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
  10884. record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
  10885. else if (GET_CODE (setter) == SET
  10886. && GET_CODE (SET_DEST (setter)) == SUBREG
  10887. && SUBREG_REG (SET_DEST (setter)) == dest
  10888. && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
  10889. && subreg_lowpart_p (SET_DEST (setter)))
  10890. record_value_for_reg (dest, record_dead_insn,
  10891. gen_lowpart (GET_MODE (dest),
  10892. SET_SRC (setter)));
  10893. else
  10894. record_value_for_reg (dest, record_dead_insn, NULL_RTX);
  10895. }
  10896. else if (MEM_P (dest)
  10897. /* Ignore pushes, they clobber nothing. */
  10898. && ! push_operand (dest, GET_MODE (dest)))
  10899. mem_last_set = DF_INSN_LUID (record_dead_insn);
  10900. }
  10901. /* Update the records of when each REG was most recently set or killed
  10902. for the things done by INSN. This is the last thing done in processing
  10903. INSN in the combiner loop.
  10904. We update reg_stat[], in particular fields last_set, last_set_value,
  10905. last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
  10906. last_death, and also the similar information mem_last_set (which insn
  10907. most recently modified memory) and last_call_luid (which insn was the
  10908. most recent subroutine call). */
  10909. static void
  10910. record_dead_and_set_regs (rtx_insn *insn)
  10911. {
  10912. rtx link;
  10913. unsigned int i;
  10914. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  10915. {
  10916. if (REG_NOTE_KIND (link) == REG_DEAD
  10917. && REG_P (XEXP (link, 0)))
  10918. {
  10919. unsigned int regno = REGNO (XEXP (link, 0));
  10920. unsigned int endregno = END_REGNO (XEXP (link, 0));
  10921. for (i = regno; i < endregno; i++)
  10922. {
  10923. reg_stat_type *rsp;
  10924. rsp = &reg_stat[i];
  10925. rsp->last_death = insn;
  10926. }
  10927. }
  10928. else if (REG_NOTE_KIND (link) == REG_INC)
  10929. record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
  10930. }
  10931. if (CALL_P (insn))
  10932. {
  10933. hard_reg_set_iterator hrsi;
  10934. EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
  10935. {
  10936. reg_stat_type *rsp;
  10937. rsp = &reg_stat[i];
  10938. rsp->last_set_invalid = 1;
  10939. rsp->last_set = insn;
  10940. rsp->last_set_value = 0;
  10941. rsp->last_set_mode = VOIDmode;
  10942. rsp->last_set_nonzero_bits = 0;
  10943. rsp->last_set_sign_bit_copies = 0;
  10944. rsp->last_death = 0;
  10945. rsp->truncated_to_mode = VOIDmode;
  10946. }
  10947. last_call_luid = mem_last_set = DF_INSN_LUID (insn);
  10948. /* We can't combine into a call pattern. Remember, though, that
  10949. the return value register is set at this LUID. We could
  10950. still replace a register with the return value from the
  10951. wrong subroutine call! */
  10952. note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
  10953. }
  10954. else
  10955. note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
  10956. }
  10957. /* If a SUBREG has the promoted bit set, it is in fact a property of the
  10958. register present in the SUBREG, so for each such SUBREG go back and
  10959. adjust nonzero and sign bit information of the registers that are
  10960. known to have some zero/sign bits set.
  10961. This is needed because when combine blows the SUBREGs away, the
  10962. information on zero/sign bits is lost and further combines can be
  10963. missed because of that. */
  10964. static void
  10965. record_promoted_value (rtx_insn *insn, rtx subreg)
  10966. {
  10967. struct insn_link *links;
  10968. rtx set;
  10969. unsigned int regno = REGNO (SUBREG_REG (subreg));
  10970. machine_mode mode = GET_MODE (subreg);
  10971. if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
  10972. return;
  10973. for (links = LOG_LINKS (insn); links;)
  10974. {
  10975. reg_stat_type *rsp;
  10976. insn = links->insn;
  10977. set = single_set (insn);
  10978. if (! set || !REG_P (SET_DEST (set))
  10979. || REGNO (SET_DEST (set)) != regno
  10980. || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
  10981. {
  10982. links = links->next;
  10983. continue;
  10984. }
  10985. rsp = &reg_stat[regno];
  10986. if (rsp->last_set == insn)
  10987. {
  10988. if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
  10989. rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
  10990. }
  10991. if (REG_P (SET_SRC (set)))
  10992. {
  10993. regno = REGNO (SET_SRC (set));
  10994. links = LOG_LINKS (insn);
  10995. }
  10996. else
  10997. break;
  10998. }
  10999. }
  11000. /* Check if X, a register, is known to contain a value already
  11001. truncated to MODE. In this case we can use a subreg to refer to
  11002. the truncated value even though in the generic case we would need
  11003. an explicit truncation. */
  11004. static bool
  11005. reg_truncated_to_mode (machine_mode mode, const_rtx x)
  11006. {
  11007. reg_stat_type *rsp = &reg_stat[REGNO (x)];
  11008. machine_mode truncated = rsp->truncated_to_mode;
  11009. if (truncated == 0
  11010. || rsp->truncation_label < label_tick_ebb_start)
  11011. return false;
  11012. if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
  11013. return true;
  11014. if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
  11015. return true;
  11016. return false;
  11017. }
  11018. /* If X is a hard reg or a subreg record the mode that the register is
  11019. accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
  11020. to turn a truncate into a subreg using this information. Return true
  11021. if traversing X is complete. */
  11022. static bool
  11023. record_truncated_value (rtx x)
  11024. {
  11025. machine_mode truncated_mode;
  11026. reg_stat_type *rsp;
  11027. if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
  11028. {
  11029. machine_mode original_mode = GET_MODE (SUBREG_REG (x));
  11030. truncated_mode = GET_MODE (x);
  11031. if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
  11032. return true;
  11033. if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
  11034. return true;
  11035. x = SUBREG_REG (x);
  11036. }
  11037. /* ??? For hard-regs we now record everything. We might be able to
  11038. optimize this using last_set_mode. */
  11039. else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
  11040. truncated_mode = GET_MODE (x);
  11041. else
  11042. return false;
  11043. rsp = &reg_stat[REGNO (x)];
  11044. if (rsp->truncated_to_mode == 0
  11045. || rsp->truncation_label < label_tick_ebb_start
  11046. || (GET_MODE_SIZE (truncated_mode)
  11047. < GET_MODE_SIZE (rsp->truncated_to_mode)))
  11048. {
  11049. rsp->truncated_to_mode = truncated_mode;
  11050. rsp->truncation_label = label_tick;
  11051. }
  11052. return true;
  11053. }
  11054. /* Callback for note_uses. Find hardregs and subregs of pseudos and
  11055. the modes they are used in. This can help truning TRUNCATEs into
  11056. SUBREGs. */
  11057. static void
  11058. record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
  11059. {
  11060. subrtx_var_iterator::array_type array;
  11061. FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
  11062. if (record_truncated_value (*iter))
  11063. iter.skip_subrtxes ();
  11064. }
  11065. /* Scan X for promoted SUBREGs. For each one found,
  11066. note what it implies to the registers used in it. */
  11067. static void
  11068. check_promoted_subreg (rtx_insn *insn, rtx x)
  11069. {
  11070. if (GET_CODE (x) == SUBREG
  11071. && SUBREG_PROMOTED_VAR_P (x)
  11072. && REG_P (SUBREG_REG (x)))
  11073. record_promoted_value (insn, x);
  11074. else
  11075. {
  11076. const char *format = GET_RTX_FORMAT (GET_CODE (x));
  11077. int i, j;
  11078. for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
  11079. switch (format[i])
  11080. {
  11081. case 'e':
  11082. check_promoted_subreg (insn, XEXP (x, i));
  11083. break;
  11084. case 'V':
  11085. case 'E':
  11086. if (XVEC (x, i) != 0)
  11087. for (j = 0; j < XVECLEN (x, i); j++)
  11088. check_promoted_subreg (insn, XVECEXP (x, i, j));
  11089. break;
  11090. }
  11091. }
  11092. }
  11093. /* Verify that all the registers and memory references mentioned in *LOC are
  11094. still valid. *LOC was part of a value set in INSN when label_tick was
  11095. equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
  11096. the invalid references with (clobber (const_int 0)) and return 1. This
  11097. replacement is useful because we often can get useful information about
  11098. the form of a value (e.g., if it was produced by a shift that always
  11099. produces -1 or 0) even though we don't know exactly what registers it
  11100. was produced from. */
  11101. static int
  11102. get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
  11103. {
  11104. rtx x = *loc;
  11105. const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
  11106. int len = GET_RTX_LENGTH (GET_CODE (x));
  11107. int i, j;
  11108. if (REG_P (x))
  11109. {
  11110. unsigned int regno = REGNO (x);
  11111. unsigned int endregno = END_REGNO (x);
  11112. unsigned int j;
  11113. for (j = regno; j < endregno; j++)
  11114. {
  11115. reg_stat_type *rsp = &reg_stat[j];
  11116. if (rsp->last_set_invalid
  11117. /* If this is a pseudo-register that was only set once and not
  11118. live at the beginning of the function, it is always valid. */
  11119. || (! (regno >= FIRST_PSEUDO_REGISTER
  11120. && regno < reg_n_sets_max
  11121. && REG_N_SETS (regno) == 1
  11122. && (!REGNO_REG_SET_P
  11123. (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
  11124. regno)))
  11125. && rsp->last_set_label > tick))
  11126. {
  11127. if (replace)
  11128. *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
  11129. return replace;
  11130. }
  11131. }
  11132. return 1;
  11133. }
  11134. /* If this is a memory reference, make sure that there were no stores after
  11135. it that might have clobbered the value. We don't have alias info, so we
  11136. assume any store invalidates it. Moreover, we only have local UIDs, so
  11137. we also assume that there were stores in the intervening basic blocks. */
  11138. else if (MEM_P (x) && !MEM_READONLY_P (x)
  11139. && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
  11140. {
  11141. if (replace)
  11142. *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
  11143. return replace;
  11144. }
  11145. for (i = 0; i < len; i++)
  11146. {
  11147. if (fmt[i] == 'e')
  11148. {
  11149. /* Check for identical subexpressions. If x contains
  11150. identical subexpression we only have to traverse one of
  11151. them. */
  11152. if (i == 1 && ARITHMETIC_P (x))
  11153. {
  11154. /* Note that at this point x0 has already been checked
  11155. and found valid. */
  11156. rtx x0 = XEXP (x, 0);
  11157. rtx x1 = XEXP (x, 1);
  11158. /* If x0 and x1 are identical then x is also valid. */
  11159. if (x0 == x1)
  11160. return 1;
  11161. /* If x1 is identical to a subexpression of x0 then
  11162. while checking x0, x1 has already been checked. Thus
  11163. it is valid and so as x. */
  11164. if (ARITHMETIC_P (x0)
  11165. && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
  11166. return 1;
  11167. /* If x0 is identical to a subexpression of x1 then x is
  11168. valid iff the rest of x1 is valid. */
  11169. if (ARITHMETIC_P (x1)
  11170. && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
  11171. return
  11172. get_last_value_validate (&XEXP (x1,
  11173. x0 == XEXP (x1, 0) ? 1 : 0),
  11174. insn, tick, replace);
  11175. }
  11176. if (get_last_value_validate (&XEXP (x, i), insn, tick,
  11177. replace) == 0)
  11178. return 0;
  11179. }
  11180. else if (fmt[i] == 'E')
  11181. for (j = 0; j < XVECLEN (x, i); j++)
  11182. if (get_last_value_validate (&XVECEXP (x, i, j),
  11183. insn, tick, replace) == 0)
  11184. return 0;
  11185. }
  11186. /* If we haven't found a reason for it to be invalid, it is valid. */
  11187. return 1;
  11188. }
  11189. /* Get the last value assigned to X, if known. Some registers
  11190. in the value may be replaced with (clobber (const_int 0)) if their value
  11191. is known longer known reliably. */
  11192. static rtx
  11193. get_last_value (const_rtx x)
  11194. {
  11195. unsigned int regno;
  11196. rtx value;
  11197. reg_stat_type *rsp;
  11198. /* If this is a non-paradoxical SUBREG, get the value of its operand and
  11199. then convert it to the desired mode. If this is a paradoxical SUBREG,
  11200. we cannot predict what values the "extra" bits might have. */
  11201. if (GET_CODE (x) == SUBREG
  11202. && subreg_lowpart_p (x)
  11203. && !paradoxical_subreg_p (x)
  11204. && (value = get_last_value (SUBREG_REG (x))) != 0)
  11205. return gen_lowpart (GET_MODE (x), value);
  11206. if (!REG_P (x))
  11207. return 0;
  11208. regno = REGNO (x);
  11209. rsp = &reg_stat[regno];
  11210. value = rsp->last_set_value;
  11211. /* If we don't have a value, or if it isn't for this basic block and
  11212. it's either a hard register, set more than once, or it's a live
  11213. at the beginning of the function, return 0.
  11214. Because if it's not live at the beginning of the function then the reg
  11215. is always set before being used (is never used without being set).
  11216. And, if it's set only once, and it's always set before use, then all
  11217. uses must have the same last value, even if it's not from this basic
  11218. block. */
  11219. if (value == 0
  11220. || (rsp->last_set_label < label_tick_ebb_start
  11221. && (regno < FIRST_PSEUDO_REGISTER
  11222. || regno >= reg_n_sets_max
  11223. || REG_N_SETS (regno) != 1
  11224. || REGNO_REG_SET_P
  11225. (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
  11226. return 0;
  11227. /* If the value was set in a later insn than the ones we are processing,
  11228. we can't use it even if the register was only set once. */
  11229. if (rsp->last_set_label == label_tick
  11230. && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
  11231. return 0;
  11232. /* If the value has all its registers valid, return it. */
  11233. if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
  11234. return value;
  11235. /* Otherwise, make a copy and replace any invalid register with
  11236. (clobber (const_int 0)). If that fails for some reason, return 0. */
  11237. value = copy_rtx (value);
  11238. if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
  11239. return value;
  11240. return 0;
  11241. }
  11242. /* Return nonzero if expression X refers to a REG or to memory
  11243. that is set in an instruction more recent than FROM_LUID. */
  11244. static int
  11245. use_crosses_set_p (const_rtx x, int from_luid)
  11246. {
  11247. const char *fmt;
  11248. int i;
  11249. enum rtx_code code = GET_CODE (x);
  11250. if (code == REG)
  11251. {
  11252. unsigned int regno = REGNO (x);
  11253. unsigned endreg = END_REGNO (x);
  11254. #ifdef PUSH_ROUNDING
  11255. /* Don't allow uses of the stack pointer to be moved,
  11256. because we don't know whether the move crosses a push insn. */
  11257. if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
  11258. return 1;
  11259. #endif
  11260. for (; regno < endreg; regno++)
  11261. {
  11262. reg_stat_type *rsp = &reg_stat[regno];
  11263. if (rsp->last_set
  11264. && rsp->last_set_label == label_tick
  11265. && DF_INSN_LUID (rsp->last_set) > from_luid)
  11266. return 1;
  11267. }
  11268. return 0;
  11269. }
  11270. if (code == MEM && mem_last_set > from_luid)
  11271. return 1;
  11272. fmt = GET_RTX_FORMAT (code);
  11273. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  11274. {
  11275. if (fmt[i] == 'E')
  11276. {
  11277. int j;
  11278. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  11279. if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
  11280. return 1;
  11281. }
  11282. else if (fmt[i] == 'e'
  11283. && use_crosses_set_p (XEXP (x, i), from_luid))
  11284. return 1;
  11285. }
  11286. return 0;
  11287. }
  11288. /* Define three variables used for communication between the following
  11289. routines. */
  11290. static unsigned int reg_dead_regno, reg_dead_endregno;
  11291. static int reg_dead_flag;
  11292. /* Function called via note_stores from reg_dead_at_p.
  11293. If DEST is within [reg_dead_regno, reg_dead_endregno), set
  11294. reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
  11295. static void
  11296. reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
  11297. {
  11298. unsigned int regno, endregno;
  11299. if (!REG_P (dest))
  11300. return;
  11301. regno = REGNO (dest);
  11302. endregno = END_REGNO (dest);
  11303. if (reg_dead_endregno > regno && reg_dead_regno < endregno)
  11304. reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
  11305. }
  11306. /* Return nonzero if REG is known to be dead at INSN.
  11307. We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
  11308. referencing REG, it is dead. If we hit a SET referencing REG, it is
  11309. live. Otherwise, see if it is live or dead at the start of the basic
  11310. block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
  11311. must be assumed to be always live. */
  11312. static int
  11313. reg_dead_at_p (rtx reg, rtx_insn *insn)
  11314. {
  11315. basic_block block;
  11316. unsigned int i;
  11317. /* Set variables for reg_dead_at_p_1. */
  11318. reg_dead_regno = REGNO (reg);
  11319. reg_dead_endregno = END_REGNO (reg);
  11320. reg_dead_flag = 0;
  11321. /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
  11322. we allow the machine description to decide whether use-and-clobber
  11323. patterns are OK. */
  11324. if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
  11325. {
  11326. for (i = reg_dead_regno; i < reg_dead_endregno; i++)
  11327. if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
  11328. return 0;
  11329. }
  11330. /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
  11331. beginning of basic block. */
  11332. block = BLOCK_FOR_INSN (insn);
  11333. for (;;)
  11334. {
  11335. if (INSN_P (insn))
  11336. {
  11337. if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
  11338. return 1;
  11339. note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
  11340. if (reg_dead_flag)
  11341. return reg_dead_flag == 1 ? 1 : 0;
  11342. if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
  11343. return 1;
  11344. }
  11345. if (insn == BB_HEAD (block))
  11346. break;
  11347. insn = PREV_INSN (insn);
  11348. }
  11349. /* Look at live-in sets for the basic block that we were in. */
  11350. for (i = reg_dead_regno; i < reg_dead_endregno; i++)
  11351. if (REGNO_REG_SET_P (df_get_live_in (block), i))
  11352. return 0;
  11353. return 1;
  11354. }
  11355. /* Note hard registers in X that are used. */
  11356. static void
  11357. mark_used_regs_combine (rtx x)
  11358. {
  11359. RTX_CODE code = GET_CODE (x);
  11360. unsigned int regno;
  11361. int i;
  11362. switch (code)
  11363. {
  11364. case LABEL_REF:
  11365. case SYMBOL_REF:
  11366. case CONST:
  11367. CASE_CONST_ANY:
  11368. case PC:
  11369. case ADDR_VEC:
  11370. case ADDR_DIFF_VEC:
  11371. case ASM_INPUT:
  11372. #ifdef HAVE_cc0
  11373. /* CC0 must die in the insn after it is set, so we don't need to take
  11374. special note of it here. */
  11375. case CC0:
  11376. #endif
  11377. return;
  11378. case CLOBBER:
  11379. /* If we are clobbering a MEM, mark any hard registers inside the
  11380. address as used. */
  11381. if (MEM_P (XEXP (x, 0)))
  11382. mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
  11383. return;
  11384. case REG:
  11385. regno = REGNO (x);
  11386. /* A hard reg in a wide mode may really be multiple registers.
  11387. If so, mark all of them just like the first. */
  11388. if (regno < FIRST_PSEUDO_REGISTER)
  11389. {
  11390. /* None of this applies to the stack, frame or arg pointers. */
  11391. if (regno == STACK_POINTER_REGNUM
  11392. #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
  11393. || regno == HARD_FRAME_POINTER_REGNUM
  11394. #endif
  11395. #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
  11396. || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
  11397. #endif
  11398. || regno == FRAME_POINTER_REGNUM)
  11399. return;
  11400. add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
  11401. }
  11402. return;
  11403. case SET:
  11404. {
  11405. /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
  11406. the address. */
  11407. rtx testreg = SET_DEST (x);
  11408. while (GET_CODE (testreg) == SUBREG
  11409. || GET_CODE (testreg) == ZERO_EXTRACT
  11410. || GET_CODE (testreg) == STRICT_LOW_PART)
  11411. testreg = XEXP (testreg, 0);
  11412. if (MEM_P (testreg))
  11413. mark_used_regs_combine (XEXP (testreg, 0));
  11414. mark_used_regs_combine (SET_SRC (x));
  11415. }
  11416. return;
  11417. default:
  11418. break;
  11419. }
  11420. /* Recursively scan the operands of this expression. */
  11421. {
  11422. const char *fmt = GET_RTX_FORMAT (code);
  11423. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  11424. {
  11425. if (fmt[i] == 'e')
  11426. mark_used_regs_combine (XEXP (x, i));
  11427. else if (fmt[i] == 'E')
  11428. {
  11429. int j;
  11430. for (j = 0; j < XVECLEN (x, i); j++)
  11431. mark_used_regs_combine (XVECEXP (x, i, j));
  11432. }
  11433. }
  11434. }
  11435. }
  11436. /* Remove register number REGNO from the dead registers list of INSN.
  11437. Return the note used to record the death, if there was one. */
  11438. rtx
  11439. remove_death (unsigned int regno, rtx_insn *insn)
  11440. {
  11441. rtx note = find_regno_note (insn, REG_DEAD, regno);
  11442. if (note)
  11443. remove_note (insn, note);
  11444. return note;
  11445. }
  11446. /* For each register (hardware or pseudo) used within expression X, if its
  11447. death is in an instruction with luid between FROM_LUID (inclusive) and
  11448. TO_INSN (exclusive), put a REG_DEAD note for that register in the
  11449. list headed by PNOTES.
  11450. That said, don't move registers killed by maybe_kill_insn.
  11451. This is done when X is being merged by combination into TO_INSN. These
  11452. notes will then be distributed as needed. */
  11453. static void
  11454. move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
  11455. rtx *pnotes)
  11456. {
  11457. const char *fmt;
  11458. int len, i;
  11459. enum rtx_code code = GET_CODE (x);
  11460. if (code == REG)
  11461. {
  11462. unsigned int regno = REGNO (x);
  11463. rtx_insn *where_dead = reg_stat[regno].last_death;
  11464. /* Don't move the register if it gets killed in between from and to. */
  11465. if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
  11466. && ! reg_referenced_p (x, maybe_kill_insn))
  11467. return;
  11468. if (where_dead
  11469. && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
  11470. && DF_INSN_LUID (where_dead) >= from_luid
  11471. && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
  11472. {
  11473. rtx note = remove_death (regno, where_dead);
  11474. /* It is possible for the call above to return 0. This can occur
  11475. when last_death points to I2 or I1 that we combined with.
  11476. In that case make a new note.
  11477. We must also check for the case where X is a hard register
  11478. and NOTE is a death note for a range of hard registers
  11479. including X. In that case, we must put REG_DEAD notes for
  11480. the remaining registers in place of NOTE. */
  11481. if (note != 0 && regno < FIRST_PSEUDO_REGISTER
  11482. && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
  11483. > GET_MODE_SIZE (GET_MODE (x))))
  11484. {
  11485. unsigned int deadregno = REGNO (XEXP (note, 0));
  11486. unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
  11487. unsigned int ourend = END_HARD_REGNO (x);
  11488. unsigned int i;
  11489. for (i = deadregno; i < deadend; i++)
  11490. if (i < regno || i >= ourend)
  11491. add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
  11492. }
  11493. /* If we didn't find any note, or if we found a REG_DEAD note that
  11494. covers only part of the given reg, and we have a multi-reg hard
  11495. register, then to be safe we must check for REG_DEAD notes
  11496. for each register other than the first. They could have
  11497. their own REG_DEAD notes lying around. */
  11498. else if ((note == 0
  11499. || (note != 0
  11500. && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
  11501. < GET_MODE_SIZE (GET_MODE (x)))))
  11502. && regno < FIRST_PSEUDO_REGISTER
  11503. && hard_regno_nregs[regno][GET_MODE (x)] > 1)
  11504. {
  11505. unsigned int ourend = END_HARD_REGNO (x);
  11506. unsigned int i, offset;
  11507. rtx oldnotes = 0;
  11508. if (note)
  11509. offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
  11510. else
  11511. offset = 1;
  11512. for (i = regno + offset; i < ourend; i++)
  11513. move_deaths (regno_reg_rtx[i],
  11514. maybe_kill_insn, from_luid, to_insn, &oldnotes);
  11515. }
  11516. if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
  11517. {
  11518. XEXP (note, 1) = *pnotes;
  11519. *pnotes = note;
  11520. }
  11521. else
  11522. *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
  11523. }
  11524. return;
  11525. }
  11526. else if (GET_CODE (x) == SET)
  11527. {
  11528. rtx dest = SET_DEST (x);
  11529. move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
  11530. /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
  11531. that accesses one word of a multi-word item, some
  11532. piece of everything register in the expression is used by
  11533. this insn, so remove any old death. */
  11534. /* ??? So why do we test for equality of the sizes? */
  11535. if (GET_CODE (dest) == ZERO_EXTRACT
  11536. || GET_CODE (dest) == STRICT_LOW_PART
  11537. || (GET_CODE (dest) == SUBREG
  11538. && (((GET_MODE_SIZE (GET_MODE (dest))
  11539. + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
  11540. == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
  11541. + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
  11542. {
  11543. move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
  11544. return;
  11545. }
  11546. /* If this is some other SUBREG, we know it replaces the entire
  11547. value, so use that as the destination. */
  11548. if (GET_CODE (dest) == SUBREG)
  11549. dest = SUBREG_REG (dest);
  11550. /* If this is a MEM, adjust deaths of anything used in the address.
  11551. For a REG (the only other possibility), the entire value is
  11552. being replaced so the old value is not used in this insn. */
  11553. if (MEM_P (dest))
  11554. move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
  11555. to_insn, pnotes);
  11556. return;
  11557. }
  11558. else if (GET_CODE (x) == CLOBBER)
  11559. return;
  11560. len = GET_RTX_LENGTH (code);
  11561. fmt = GET_RTX_FORMAT (code);
  11562. for (i = 0; i < len; i++)
  11563. {
  11564. if (fmt[i] == 'E')
  11565. {
  11566. int j;
  11567. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  11568. move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
  11569. to_insn, pnotes);
  11570. }
  11571. else if (fmt[i] == 'e')
  11572. move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
  11573. }
  11574. }
  11575. /* Return 1 if X is the target of a bit-field assignment in BODY, the
  11576. pattern of an insn. X must be a REG. */
  11577. static int
  11578. reg_bitfield_target_p (rtx x, rtx body)
  11579. {
  11580. int i;
  11581. if (GET_CODE (body) == SET)
  11582. {
  11583. rtx dest = SET_DEST (body);
  11584. rtx target;
  11585. unsigned int regno, tregno, endregno, endtregno;
  11586. if (GET_CODE (dest) == ZERO_EXTRACT)
  11587. target = XEXP (dest, 0);
  11588. else if (GET_CODE (dest) == STRICT_LOW_PART)
  11589. target = SUBREG_REG (XEXP (dest, 0));
  11590. else
  11591. return 0;
  11592. if (GET_CODE (target) == SUBREG)
  11593. target = SUBREG_REG (target);
  11594. if (!REG_P (target))
  11595. return 0;
  11596. tregno = REGNO (target), regno = REGNO (x);
  11597. if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
  11598. return target == x;
  11599. endtregno = end_hard_regno (GET_MODE (target), tregno);
  11600. endregno = end_hard_regno (GET_MODE (x), regno);
  11601. return endregno > tregno && regno < endtregno;
  11602. }
  11603. else if (GET_CODE (body) == PARALLEL)
  11604. for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
  11605. if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
  11606. return 1;
  11607. return 0;
  11608. }
  11609. /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
  11610. as appropriate. I3 and I2 are the insns resulting from the combination
  11611. insns including FROM (I2 may be zero).
  11612. ELIM_I2 and ELIM_I1 are either zero or registers that we know will
  11613. not need REG_DEAD notes because they are being substituted for. This
  11614. saves searching in the most common cases.
  11615. Each note in the list is either ignored or placed on some insns, depending
  11616. on the type of note. */
  11617. static void
  11618. distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
  11619. rtx elim_i2, rtx elim_i1, rtx elim_i0)
  11620. {
  11621. rtx note, next_note;
  11622. rtx tem_note;
  11623. rtx_insn *tem_insn;
  11624. for (note = notes; note; note = next_note)
  11625. {
  11626. rtx_insn *place = 0, *place2 = 0;
  11627. next_note = XEXP (note, 1);
  11628. switch (REG_NOTE_KIND (note))
  11629. {
  11630. case REG_BR_PROB:
  11631. case REG_BR_PRED:
  11632. /* Doesn't matter much where we put this, as long as it's somewhere.
  11633. It is preferable to keep these notes on branches, which is most
  11634. likely to be i3. */
  11635. place = i3;
  11636. break;
  11637. case REG_NON_LOCAL_GOTO:
  11638. if (JUMP_P (i3))
  11639. place = i3;
  11640. else
  11641. {
  11642. gcc_assert (i2 && JUMP_P (i2));
  11643. place = i2;
  11644. }
  11645. break;
  11646. case REG_EH_REGION:
  11647. /* These notes must remain with the call or trapping instruction. */
  11648. if (CALL_P (i3))
  11649. place = i3;
  11650. else if (i2 && CALL_P (i2))
  11651. place = i2;
  11652. else
  11653. {
  11654. gcc_assert (cfun->can_throw_non_call_exceptions);
  11655. if (may_trap_p (i3))
  11656. place = i3;
  11657. else if (i2 && may_trap_p (i2))
  11658. place = i2;
  11659. /* ??? Otherwise assume we've combined things such that we
  11660. can now prove that the instructions can't trap. Drop the
  11661. note in this case. */
  11662. }
  11663. break;
  11664. case REG_ARGS_SIZE:
  11665. /* ??? How to distribute between i3-i1. Assume i3 contains the
  11666. entire adjustment. Assert i3 contains at least some adjust. */
  11667. if (!noop_move_p (i3))
  11668. {
  11669. int old_size, args_size = INTVAL (XEXP (note, 0));
  11670. /* fixup_args_size_notes looks at REG_NORETURN note,
  11671. so ensure the note is placed there first. */
  11672. if (CALL_P (i3))
  11673. {
  11674. rtx *np;
  11675. for (np = &next_note; *np; np = &XEXP (*np, 1))
  11676. if (REG_NOTE_KIND (*np) == REG_NORETURN)
  11677. {
  11678. rtx n = *np;
  11679. *np = XEXP (n, 1);
  11680. XEXP (n, 1) = REG_NOTES (i3);
  11681. REG_NOTES (i3) = n;
  11682. break;
  11683. }
  11684. }
  11685. old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
  11686. /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
  11687. REG_ARGS_SIZE note to all noreturn calls, allow that here. */
  11688. gcc_assert (old_size != args_size
  11689. || (CALL_P (i3)
  11690. && !ACCUMULATE_OUTGOING_ARGS
  11691. && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
  11692. }
  11693. break;
  11694. case REG_NORETURN:
  11695. case REG_SETJMP:
  11696. case REG_TM:
  11697. case REG_CALL_DECL:
  11698. /* These notes must remain with the call. It should not be
  11699. possible for both I2 and I3 to be a call. */
  11700. if (CALL_P (i3))
  11701. place = i3;
  11702. else
  11703. {
  11704. gcc_assert (i2 && CALL_P (i2));
  11705. place = i2;
  11706. }
  11707. break;
  11708. case REG_UNUSED:
  11709. /* Any clobbers for i3 may still exist, and so we must process
  11710. REG_UNUSED notes from that insn.
  11711. Any clobbers from i2 or i1 can only exist if they were added by
  11712. recog_for_combine. In that case, recog_for_combine created the
  11713. necessary REG_UNUSED notes. Trying to keep any original
  11714. REG_UNUSED notes from these insns can cause incorrect output
  11715. if it is for the same register as the original i3 dest.
  11716. In that case, we will notice that the register is set in i3,
  11717. and then add a REG_UNUSED note for the destination of i3, which
  11718. is wrong. However, it is possible to have REG_UNUSED notes from
  11719. i2 or i1 for register which were both used and clobbered, so
  11720. we keep notes from i2 or i1 if they will turn into REG_DEAD
  11721. notes. */
  11722. /* If this register is set or clobbered in I3, put the note there
  11723. unless there is one already. */
  11724. if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
  11725. {
  11726. if (from_insn != i3)
  11727. break;
  11728. if (! (REG_P (XEXP (note, 0))
  11729. ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
  11730. : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
  11731. place = i3;
  11732. }
  11733. /* Otherwise, if this register is used by I3, then this register
  11734. now dies here, so we must put a REG_DEAD note here unless there
  11735. is one already. */
  11736. else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
  11737. && ! (REG_P (XEXP (note, 0))
  11738. ? find_regno_note (i3, REG_DEAD,
  11739. REGNO (XEXP (note, 0)))
  11740. : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
  11741. {
  11742. PUT_REG_NOTE_KIND (note, REG_DEAD);
  11743. place = i3;
  11744. }
  11745. break;
  11746. case REG_EQUAL:
  11747. case REG_EQUIV:
  11748. case REG_NOALIAS:
  11749. /* These notes say something about results of an insn. We can
  11750. only support them if they used to be on I3 in which case they
  11751. remain on I3. Otherwise they are ignored.
  11752. If the note refers to an expression that is not a constant, we
  11753. must also ignore the note since we cannot tell whether the
  11754. equivalence is still true. It might be possible to do
  11755. slightly better than this (we only have a problem if I2DEST
  11756. or I1DEST is present in the expression), but it doesn't
  11757. seem worth the trouble. */
  11758. if (from_insn == i3
  11759. && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
  11760. place = i3;
  11761. break;
  11762. case REG_INC:
  11763. /* These notes say something about how a register is used. They must
  11764. be present on any use of the register in I2 or I3. */
  11765. if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
  11766. place = i3;
  11767. if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
  11768. {
  11769. if (place)
  11770. place2 = i2;
  11771. else
  11772. place = i2;
  11773. }
  11774. break;
  11775. case REG_LABEL_TARGET:
  11776. case REG_LABEL_OPERAND:
  11777. /* This can show up in several ways -- either directly in the
  11778. pattern, or hidden off in the constant pool with (or without?)
  11779. a REG_EQUAL note. */
  11780. /* ??? Ignore the without-reg_equal-note problem for now. */
  11781. if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
  11782. || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
  11783. && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
  11784. && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
  11785. place = i3;
  11786. if (i2
  11787. && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
  11788. || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
  11789. && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
  11790. && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
  11791. {
  11792. if (place)
  11793. place2 = i2;
  11794. else
  11795. place = i2;
  11796. }
  11797. /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
  11798. as a JUMP_LABEL or decrement LABEL_NUSES if it's already
  11799. there. */
  11800. if (place && JUMP_P (place)
  11801. && REG_NOTE_KIND (note) == REG_LABEL_TARGET
  11802. && (JUMP_LABEL (place) == NULL
  11803. || JUMP_LABEL (place) == XEXP (note, 0)))
  11804. {
  11805. rtx label = JUMP_LABEL (place);
  11806. if (!label)
  11807. JUMP_LABEL (place) = XEXP (note, 0);
  11808. else if (LABEL_P (label))
  11809. LABEL_NUSES (label)--;
  11810. }
  11811. if (place2 && JUMP_P (place2)
  11812. && REG_NOTE_KIND (note) == REG_LABEL_TARGET
  11813. && (JUMP_LABEL (place2) == NULL
  11814. || JUMP_LABEL (place2) == XEXP (note, 0)))
  11815. {
  11816. rtx label = JUMP_LABEL (place2);
  11817. if (!label)
  11818. JUMP_LABEL (place2) = XEXP (note, 0);
  11819. else if (LABEL_P (label))
  11820. LABEL_NUSES (label)--;
  11821. place2 = 0;
  11822. }
  11823. break;
  11824. case REG_NONNEG:
  11825. /* This note says something about the value of a register prior
  11826. to the execution of an insn. It is too much trouble to see
  11827. if the note is still correct in all situations. It is better
  11828. to simply delete it. */
  11829. break;
  11830. case REG_DEAD:
  11831. /* If we replaced the right hand side of FROM_INSN with a
  11832. REG_EQUAL note, the original use of the dying register
  11833. will not have been combined into I3 and I2. In such cases,
  11834. FROM_INSN is guaranteed to be the first of the combined
  11835. instructions, so we simply need to search back before
  11836. FROM_INSN for the previous use or set of this register,
  11837. then alter the notes there appropriately.
  11838. If the register is used as an input in I3, it dies there.
  11839. Similarly for I2, if it is nonzero and adjacent to I3.
  11840. If the register is not used as an input in either I3 or I2
  11841. and it is not one of the registers we were supposed to eliminate,
  11842. there are two possibilities. We might have a non-adjacent I2
  11843. or we might have somehow eliminated an additional register
  11844. from a computation. For example, we might have had A & B where
  11845. we discover that B will always be zero. In this case we will
  11846. eliminate the reference to A.
  11847. In both cases, we must search to see if we can find a previous
  11848. use of A and put the death note there. */
  11849. if (from_insn
  11850. && from_insn == i2mod
  11851. && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
  11852. tem_insn = from_insn;
  11853. else
  11854. {
  11855. if (from_insn
  11856. && CALL_P (from_insn)
  11857. && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
  11858. place = from_insn;
  11859. else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
  11860. place = i3;
  11861. else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
  11862. && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
  11863. place = i2;
  11864. else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
  11865. && !(i2mod
  11866. && reg_overlap_mentioned_p (XEXP (note, 0),
  11867. i2mod_old_rhs)))
  11868. || rtx_equal_p (XEXP (note, 0), elim_i1)
  11869. || rtx_equal_p (XEXP (note, 0), elim_i0))
  11870. break;
  11871. tem_insn = i3;
  11872. /* If the new I2 sets the same register that is marked dead
  11873. in the note, the note now should not be put on I2, as the
  11874. note refers to a previous incarnation of the reg. */
  11875. if (i2 != 0 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
  11876. tem_insn = i2;
  11877. }
  11878. if (place == 0)
  11879. {
  11880. basic_block bb = this_basic_block;
  11881. for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
  11882. {
  11883. if (!NONDEBUG_INSN_P (tem_insn))
  11884. {
  11885. if (tem_insn == BB_HEAD (bb))
  11886. break;
  11887. continue;
  11888. }
  11889. /* If the register is being set at TEM_INSN, see if that is all
  11890. TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
  11891. into a REG_UNUSED note instead. Don't delete sets to
  11892. global register vars. */
  11893. if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
  11894. || !global_regs[REGNO (XEXP (note, 0))])
  11895. && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
  11896. {
  11897. rtx set = single_set (tem_insn);
  11898. rtx inner_dest = 0;
  11899. #ifdef HAVE_cc0
  11900. rtx_insn *cc0_setter = NULL;
  11901. #endif
  11902. if (set != 0)
  11903. for (inner_dest = SET_DEST (set);
  11904. (GET_CODE (inner_dest) == STRICT_LOW_PART
  11905. || GET_CODE (inner_dest) == SUBREG
  11906. || GET_CODE (inner_dest) == ZERO_EXTRACT);
  11907. inner_dest = XEXP (inner_dest, 0))
  11908. ;
  11909. /* Verify that it was the set, and not a clobber that
  11910. modified the register.
  11911. CC0 targets must be careful to maintain setter/user
  11912. pairs. If we cannot delete the setter due to side
  11913. effects, mark the user with an UNUSED note instead
  11914. of deleting it. */
  11915. if (set != 0 && ! side_effects_p (SET_SRC (set))
  11916. && rtx_equal_p (XEXP (note, 0), inner_dest)
  11917. #ifdef HAVE_cc0
  11918. && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
  11919. || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
  11920. && sets_cc0_p (PATTERN (cc0_setter)) > 0))
  11921. #endif
  11922. )
  11923. {
  11924. /* Move the notes and links of TEM_INSN elsewhere.
  11925. This might delete other dead insns recursively.
  11926. First set the pattern to something that won't use
  11927. any register. */
  11928. rtx old_notes = REG_NOTES (tem_insn);
  11929. PATTERN (tem_insn) = pc_rtx;
  11930. REG_NOTES (tem_insn) = NULL;
  11931. distribute_notes (old_notes, tem_insn, tem_insn, NULL,
  11932. NULL_RTX, NULL_RTX, NULL_RTX);
  11933. distribute_links (LOG_LINKS (tem_insn));
  11934. SET_INSN_DELETED (tem_insn);
  11935. if (tem_insn == i2)
  11936. i2 = NULL;
  11937. #ifdef HAVE_cc0
  11938. /* Delete the setter too. */
  11939. if (cc0_setter)
  11940. {
  11941. PATTERN (cc0_setter) = pc_rtx;
  11942. old_notes = REG_NOTES (cc0_setter);
  11943. REG_NOTES (cc0_setter) = NULL;
  11944. distribute_notes (old_notes, cc0_setter,
  11945. cc0_setter, NULL,
  11946. NULL_RTX, NULL_RTX, NULL_RTX);
  11947. distribute_links (LOG_LINKS (cc0_setter));
  11948. SET_INSN_DELETED (cc0_setter);
  11949. if (cc0_setter == i2)
  11950. i2 = NULL;
  11951. }
  11952. #endif
  11953. }
  11954. else
  11955. {
  11956. PUT_REG_NOTE_KIND (note, REG_UNUSED);
  11957. /* If there isn't already a REG_UNUSED note, put one
  11958. here. Do not place a REG_DEAD note, even if
  11959. the register is also used here; that would not
  11960. match the algorithm used in lifetime analysis
  11961. and can cause the consistency check in the
  11962. scheduler to fail. */
  11963. if (! find_regno_note (tem_insn, REG_UNUSED,
  11964. REGNO (XEXP (note, 0))))
  11965. place = tem_insn;
  11966. break;
  11967. }
  11968. }
  11969. else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
  11970. || (CALL_P (tem_insn)
  11971. && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
  11972. {
  11973. place = tem_insn;
  11974. /* If we are doing a 3->2 combination, and we have a
  11975. register which formerly died in i3 and was not used
  11976. by i2, which now no longer dies in i3 and is used in
  11977. i2 but does not die in i2, and place is between i2
  11978. and i3, then we may need to move a link from place to
  11979. i2. */
  11980. if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
  11981. && from_insn
  11982. && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
  11983. && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
  11984. {
  11985. struct insn_link *links = LOG_LINKS (place);
  11986. LOG_LINKS (place) = NULL;
  11987. distribute_links (links);
  11988. }
  11989. break;
  11990. }
  11991. if (tem_insn == BB_HEAD (bb))
  11992. break;
  11993. }
  11994. }
  11995. /* If the register is set or already dead at PLACE, we needn't do
  11996. anything with this note if it is still a REG_DEAD note.
  11997. We check here if it is set at all, not if is it totally replaced,
  11998. which is what `dead_or_set_p' checks, so also check for it being
  11999. set partially. */
  12000. if (place && REG_NOTE_KIND (note) == REG_DEAD)
  12001. {
  12002. unsigned int regno = REGNO (XEXP (note, 0));
  12003. reg_stat_type *rsp = &reg_stat[regno];
  12004. if (dead_or_set_p (place, XEXP (note, 0))
  12005. || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
  12006. {
  12007. /* Unless the register previously died in PLACE, clear
  12008. last_death. [I no longer understand why this is
  12009. being done.] */
  12010. if (rsp->last_death != place)
  12011. rsp->last_death = 0;
  12012. place = 0;
  12013. }
  12014. else
  12015. rsp->last_death = place;
  12016. /* If this is a death note for a hard reg that is occupying
  12017. multiple registers, ensure that we are still using all
  12018. parts of the object. If we find a piece of the object
  12019. that is unused, we must arrange for an appropriate REG_DEAD
  12020. note to be added for it. However, we can't just emit a USE
  12021. and tag the note to it, since the register might actually
  12022. be dead; so we recourse, and the recursive call then finds
  12023. the previous insn that used this register. */
  12024. if (place && regno < FIRST_PSEUDO_REGISTER
  12025. && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
  12026. {
  12027. unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
  12028. bool all_used = true;
  12029. unsigned int i;
  12030. for (i = regno; i < endregno; i++)
  12031. if ((! refers_to_regno_p (i, PATTERN (place))
  12032. && ! find_regno_fusage (place, USE, i))
  12033. || dead_or_set_regno_p (place, i))
  12034. {
  12035. all_used = false;
  12036. break;
  12037. }
  12038. if (! all_used)
  12039. {
  12040. /* Put only REG_DEAD notes for pieces that are
  12041. not already dead or set. */
  12042. for (i = regno; i < endregno;
  12043. i += hard_regno_nregs[i][reg_raw_mode[i]])
  12044. {
  12045. rtx piece = regno_reg_rtx[i];
  12046. basic_block bb = this_basic_block;
  12047. if (! dead_or_set_p (place, piece)
  12048. && ! reg_bitfield_target_p (piece,
  12049. PATTERN (place)))
  12050. {
  12051. rtx new_note = alloc_reg_note (REG_DEAD, piece,
  12052. NULL_RTX);
  12053. distribute_notes (new_note, place, place,
  12054. NULL, NULL_RTX, NULL_RTX,
  12055. NULL_RTX);
  12056. }
  12057. else if (! refers_to_regno_p (i, PATTERN (place))
  12058. && ! find_regno_fusage (place, USE, i))
  12059. for (tem_insn = PREV_INSN (place); ;
  12060. tem_insn = PREV_INSN (tem_insn))
  12061. {
  12062. if (!NONDEBUG_INSN_P (tem_insn))
  12063. {
  12064. if (tem_insn == BB_HEAD (bb))
  12065. break;
  12066. continue;
  12067. }
  12068. if (dead_or_set_p (tem_insn, piece)
  12069. || reg_bitfield_target_p (piece,
  12070. PATTERN (tem_insn)))
  12071. {
  12072. add_reg_note (tem_insn, REG_UNUSED, piece);
  12073. break;
  12074. }
  12075. }
  12076. }
  12077. place = 0;
  12078. }
  12079. }
  12080. }
  12081. break;
  12082. default:
  12083. /* Any other notes should not be present at this point in the
  12084. compilation. */
  12085. gcc_unreachable ();
  12086. }
  12087. if (place)
  12088. {
  12089. XEXP (note, 1) = REG_NOTES (place);
  12090. REG_NOTES (place) = note;
  12091. }
  12092. if (place2)
  12093. add_shallow_copy_of_reg_note (place2, note);
  12094. }
  12095. }
  12096. /* Similarly to above, distribute the LOG_LINKS that used to be present on
  12097. I3, I2, and I1 to new locations. This is also called to add a link
  12098. pointing at I3 when I3's destination is changed. */
  12099. static void
  12100. distribute_links (struct insn_link *links)
  12101. {
  12102. struct insn_link *link, *next_link;
  12103. for (link = links; link; link = next_link)
  12104. {
  12105. rtx_insn *place = 0;
  12106. rtx_insn *insn;
  12107. rtx set, reg;
  12108. next_link = link->next;
  12109. /* If the insn that this link points to is a NOTE, ignore it. */
  12110. if (NOTE_P (link->insn))
  12111. continue;
  12112. set = 0;
  12113. rtx pat = PATTERN (link->insn);
  12114. if (GET_CODE (pat) == SET)
  12115. set = pat;
  12116. else if (GET_CODE (pat) == PARALLEL)
  12117. {
  12118. int i;
  12119. for (i = 0; i < XVECLEN (pat, 0); i++)
  12120. {
  12121. set = XVECEXP (pat, 0, i);
  12122. if (GET_CODE (set) != SET)
  12123. continue;
  12124. reg = SET_DEST (set);
  12125. while (GET_CODE (reg) == ZERO_EXTRACT
  12126. || GET_CODE (reg) == STRICT_LOW_PART
  12127. || GET_CODE (reg) == SUBREG)
  12128. reg = XEXP (reg, 0);
  12129. if (!REG_P (reg))
  12130. continue;
  12131. if (REGNO (reg) == link->regno)
  12132. break;
  12133. }
  12134. if (i == XVECLEN (pat, 0))
  12135. continue;
  12136. }
  12137. else
  12138. continue;
  12139. reg = SET_DEST (set);
  12140. while (GET_CODE (reg) == ZERO_EXTRACT
  12141. || GET_CODE (reg) == STRICT_LOW_PART
  12142. || GET_CODE (reg) == SUBREG)
  12143. reg = XEXP (reg, 0);
  12144. /* A LOG_LINK is defined as being placed on the first insn that uses
  12145. a register and points to the insn that sets the register. Start
  12146. searching at the next insn after the target of the link and stop
  12147. when we reach a set of the register or the end of the basic block.
  12148. Note that this correctly handles the link that used to point from
  12149. I3 to I2. Also note that not much searching is typically done here
  12150. since most links don't point very far away. */
  12151. for (insn = NEXT_INSN (link->insn);
  12152. (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
  12153. || BB_HEAD (this_basic_block->next_bb) != insn));
  12154. insn = NEXT_INSN (insn))
  12155. if (DEBUG_INSN_P (insn))
  12156. continue;
  12157. else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
  12158. {
  12159. if (reg_referenced_p (reg, PATTERN (insn)))
  12160. place = insn;
  12161. break;
  12162. }
  12163. else if (CALL_P (insn)
  12164. && find_reg_fusage (insn, USE, reg))
  12165. {
  12166. place = insn;
  12167. break;
  12168. }
  12169. else if (INSN_P (insn) && reg_set_p (reg, insn))
  12170. break;
  12171. /* If we found a place to put the link, place it there unless there
  12172. is already a link to the same insn as LINK at that point. */
  12173. if (place)
  12174. {
  12175. struct insn_link *link2;
  12176. FOR_EACH_LOG_LINK (link2, place)
  12177. if (link2->insn == link->insn && link2->regno == link->regno)
  12178. break;
  12179. if (link2 == NULL)
  12180. {
  12181. link->next = LOG_LINKS (place);
  12182. LOG_LINKS (place) = link;
  12183. /* Set added_links_insn to the earliest insn we added a
  12184. link to. */
  12185. if (added_links_insn == 0
  12186. || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
  12187. added_links_insn = place;
  12188. }
  12189. }
  12190. }
  12191. }
  12192. /* Check for any register or memory mentioned in EQUIV that is not
  12193. mentioned in EXPR. This is used to restrict EQUIV to "specializations"
  12194. of EXPR where some registers may have been replaced by constants. */
  12195. static bool
  12196. unmentioned_reg_p (rtx equiv, rtx expr)
  12197. {
  12198. subrtx_iterator::array_type array;
  12199. FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
  12200. {
  12201. const_rtx x = *iter;
  12202. if ((REG_P (x) || MEM_P (x))
  12203. && !reg_mentioned_p (x, expr))
  12204. return true;
  12205. }
  12206. return false;
  12207. }
  12208. DEBUG_FUNCTION void
  12209. dump_combine_stats (FILE *file)
  12210. {
  12211. fprintf
  12212. (file,
  12213. ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
  12214. combine_attempts, combine_merges, combine_extras, combine_successes);
  12215. }
  12216. void
  12217. dump_combine_total_stats (FILE *file)
  12218. {
  12219. fprintf
  12220. (file,
  12221. "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
  12222. total_attempts, total_merges, total_extras, total_successes);
  12223. }
  12224. /* Try combining insns through substitution. */
  12225. static unsigned int
  12226. rest_of_handle_combine (void)
  12227. {
  12228. int rebuild_jump_labels_after_combine;
  12229. df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
  12230. df_note_add_problem ();
  12231. df_analyze ();
  12232. regstat_init_n_sets_and_refs ();
  12233. reg_n_sets_max = max_reg_num ();
  12234. rebuild_jump_labels_after_combine
  12235. = combine_instructions (get_insns (), max_reg_num ());
  12236. /* Combining insns may have turned an indirect jump into a
  12237. direct jump. Rebuild the JUMP_LABEL fields of jumping
  12238. instructions. */
  12239. if (rebuild_jump_labels_after_combine)
  12240. {
  12241. timevar_push (TV_JUMP);
  12242. rebuild_jump_labels (get_insns ());
  12243. cleanup_cfg (0);
  12244. timevar_pop (TV_JUMP);
  12245. }
  12246. regstat_free_n_sets_and_refs ();
  12247. return 0;
  12248. }
  12249. namespace {
  12250. const pass_data pass_data_combine =
  12251. {
  12252. RTL_PASS, /* type */
  12253. "combine", /* name */
  12254. OPTGROUP_NONE, /* optinfo_flags */
  12255. TV_COMBINE, /* tv_id */
  12256. PROP_cfglayout, /* properties_required */
  12257. 0, /* properties_provided */
  12258. 0, /* properties_destroyed */
  12259. 0, /* todo_flags_start */
  12260. TODO_df_finish, /* todo_flags_finish */
  12261. };
  12262. class pass_combine : public rtl_opt_pass
  12263. {
  12264. public:
  12265. pass_combine (gcc::context *ctxt)
  12266. : rtl_opt_pass (pass_data_combine, ctxt)
  12267. {}
  12268. /* opt_pass methods: */
  12269. virtual bool gate (function *) { return (optimize > 0); }
  12270. virtual unsigned int execute (function *)
  12271. {
  12272. return rest_of_handle_combine ();
  12273. }
  12274. }; // class pass_combine
  12275. } // anon namespace
  12276. rtl_opt_pass *
  12277. make_pass_combine (gcc::context *ctxt)
  12278. {
  12279. return new pass_combine (ctxt);
  12280. }