sel-sched-ir.c 171 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468
  1. /* Instruction scheduling pass. Selective scheduler and pipeliner.
  2. Copyright (C) 2006-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. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "diagnostic-core.h"
  20. #include "rtl.h"
  21. #include "tm_p.h"
  22. #include "hard-reg-set.h"
  23. #include "regs.h"
  24. #include "hashtab.h"
  25. #include "hash-set.h"
  26. #include "vec.h"
  27. #include "machmode.h"
  28. #include "input.h"
  29. #include "function.h"
  30. #include "predict.h"
  31. #include "dominance.h"
  32. #include "cfg.h"
  33. #include "cfgrtl.h"
  34. #include "cfganal.h"
  35. #include "cfgbuild.h"
  36. #include "basic-block.h"
  37. #include "flags.h"
  38. #include "insn-config.h"
  39. #include "insn-attr.h"
  40. #include "except.h"
  41. #include "recog.h"
  42. #include "params.h"
  43. #include "target.h"
  44. #include "sched-int.h"
  45. #include "ggc.h"
  46. #include "symtab.h"
  47. #include "wide-int.h"
  48. #include "inchash.h"
  49. #include "tree.h"
  50. #include "langhooks.h"
  51. #include "rtlhooks-def.h"
  52. #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
  53. #ifdef INSN_SCHEDULING
  54. #include "sel-sched-ir.h"
  55. /* We don't have to use it except for sel_print_insn. */
  56. #include "sel-sched-dump.h"
  57. /* A vector holding bb info for whole scheduling pass. */
  58. vec<sel_global_bb_info_def>
  59. sel_global_bb_info = vNULL;
  60. /* A vector holding bb info. */
  61. vec<sel_region_bb_info_def>
  62. sel_region_bb_info = vNULL;
  63. /* A pool for allocating all lists. */
  64. alloc_pool sched_lists_pool;
  65. /* This contains information about successors for compute_av_set. */
  66. struct succs_info current_succs;
  67. /* Data structure to describe interaction with the generic scheduler utils. */
  68. static struct common_sched_info_def sel_common_sched_info;
  69. /* The loop nest being pipelined. */
  70. struct loop *current_loop_nest;
  71. /* LOOP_NESTS is a vector containing the corresponding loop nest for
  72. each region. */
  73. static vec<loop_p> loop_nests = vNULL;
  74. /* Saves blocks already in loop regions, indexed by bb->index. */
  75. static sbitmap bbs_in_loop_rgns = NULL;
  76. /* CFG hooks that are saved before changing create_basic_block hook. */
  77. static struct cfg_hooks orig_cfg_hooks;
  78. /* Array containing reverse topological index of function basic blocks,
  79. indexed by BB->INDEX. */
  80. static int *rev_top_order_index = NULL;
  81. /* Length of the above array. */
  82. static int rev_top_order_index_len = -1;
  83. /* A regset pool structure. */
  84. static struct
  85. {
  86. /* The stack to which regsets are returned. */
  87. regset *v;
  88. /* Its pointer. */
  89. int n;
  90. /* Its size. */
  91. int s;
  92. /* In VV we save all generated regsets so that, when destructing the
  93. pool, we can compare it with V and check that every regset was returned
  94. back to pool. */
  95. regset *vv;
  96. /* The pointer of VV stack. */
  97. int nn;
  98. /* Its size. */
  99. int ss;
  100. /* The difference between allocated and returned regsets. */
  101. int diff;
  102. } regset_pool = { NULL, 0, 0, NULL, 0, 0, 0 };
  103. /* This represents the nop pool. */
  104. static struct
  105. {
  106. /* The vector which holds previously emitted nops. */
  107. insn_t *v;
  108. /* Its pointer. */
  109. int n;
  110. /* Its size. */
  111. int s;
  112. } nop_pool = { NULL, 0, 0 };
  113. /* The pool for basic block notes. */
  114. static vec<rtx_note *> bb_note_pool;
  115. /* A NOP pattern used to emit placeholder insns. */
  116. rtx nop_pattern = NULL_RTX;
  117. /* A special instruction that resides in EXIT_BLOCK.
  118. EXIT_INSN is successor of the insns that lead to EXIT_BLOCK. */
  119. rtx_insn *exit_insn = NULL;
  120. /* TRUE if while scheduling current region, which is loop, its preheader
  121. was removed. */
  122. bool preheader_removed = false;
  123. /* Forward static declarations. */
  124. static void fence_clear (fence_t);
  125. static void deps_init_id (idata_t, insn_t, bool);
  126. static void init_id_from_df (idata_t, insn_t, bool);
  127. static expr_t set_insn_init (expr_t, vinsn_t, int);
  128. static void cfg_preds (basic_block, insn_t **, int *);
  129. static void prepare_insn_expr (insn_t, int);
  130. static void free_history_vect (vec<expr_history_def> &);
  131. static void move_bb_info (basic_block, basic_block);
  132. static void remove_empty_bb (basic_block, bool);
  133. static void sel_merge_blocks (basic_block, basic_block);
  134. static void sel_remove_loop_preheader (void);
  135. static bool bb_has_removable_jump_to_p (basic_block, basic_block);
  136. static bool insn_is_the_only_one_in_bb_p (insn_t);
  137. static void create_initial_data_sets (basic_block);
  138. static void free_av_set (basic_block);
  139. static void invalidate_av_set (basic_block);
  140. static void extend_insn_data (void);
  141. static void sel_init_new_insn (insn_t, int, int = -1);
  142. static void finish_insns (void);
  143. /* Various list functions. */
  144. /* Copy an instruction list L. */
  145. ilist_t
  146. ilist_copy (ilist_t l)
  147. {
  148. ilist_t head = NULL, *tailp = &head;
  149. while (l)
  150. {
  151. ilist_add (tailp, ILIST_INSN (l));
  152. tailp = &ILIST_NEXT (*tailp);
  153. l = ILIST_NEXT (l);
  154. }
  155. return head;
  156. }
  157. /* Invert an instruction list L. */
  158. ilist_t
  159. ilist_invert (ilist_t l)
  160. {
  161. ilist_t res = NULL;
  162. while (l)
  163. {
  164. ilist_add (&res, ILIST_INSN (l));
  165. l = ILIST_NEXT (l);
  166. }
  167. return res;
  168. }
  169. /* Add a new boundary to the LP list with parameters TO, PTR, and DC. */
  170. void
  171. blist_add (blist_t *lp, insn_t to, ilist_t ptr, deps_t dc)
  172. {
  173. bnd_t bnd;
  174. _list_add (lp);
  175. bnd = BLIST_BND (*lp);
  176. BND_TO (bnd) = to;
  177. BND_PTR (bnd) = ptr;
  178. BND_AV (bnd) = NULL;
  179. BND_AV1 (bnd) = NULL;
  180. BND_DC (bnd) = dc;
  181. }
  182. /* Remove the list note pointed to by LP. */
  183. void
  184. blist_remove (blist_t *lp)
  185. {
  186. bnd_t b = BLIST_BND (*lp);
  187. av_set_clear (&BND_AV (b));
  188. av_set_clear (&BND_AV1 (b));
  189. ilist_clear (&BND_PTR (b));
  190. _list_remove (lp);
  191. }
  192. /* Init a fence tail L. */
  193. void
  194. flist_tail_init (flist_tail_t l)
  195. {
  196. FLIST_TAIL_HEAD (l) = NULL;
  197. FLIST_TAIL_TAILP (l) = &FLIST_TAIL_HEAD (l);
  198. }
  199. /* Try to find fence corresponding to INSN in L. */
  200. fence_t
  201. flist_lookup (flist_t l, insn_t insn)
  202. {
  203. while (l)
  204. {
  205. if (FENCE_INSN (FLIST_FENCE (l)) == insn)
  206. return FLIST_FENCE (l);
  207. l = FLIST_NEXT (l);
  208. }
  209. return NULL;
  210. }
  211. /* Init the fields of F before running fill_insns. */
  212. static void
  213. init_fence_for_scheduling (fence_t f)
  214. {
  215. FENCE_BNDS (f) = NULL;
  216. FENCE_PROCESSED_P (f) = false;
  217. FENCE_SCHEDULED_P (f) = false;
  218. }
  219. /* Add new fence consisting of INSN and STATE to the list pointed to by LP. */
  220. static void
  221. flist_add (flist_t *lp, insn_t insn, state_t state, deps_t dc, void *tc,
  222. insn_t last_scheduled_insn, vec<rtx_insn *, va_gc> *executing_insns,
  223. int *ready_ticks, int ready_ticks_size, insn_t sched_next,
  224. int cycle, int cycle_issued_insns, int issue_more,
  225. bool starts_cycle_p, bool after_stall_p)
  226. {
  227. fence_t f;
  228. _list_add (lp);
  229. f = FLIST_FENCE (*lp);
  230. FENCE_INSN (f) = insn;
  231. gcc_assert (state != NULL);
  232. FENCE_STATE (f) = state;
  233. FENCE_CYCLE (f) = cycle;
  234. FENCE_ISSUED_INSNS (f) = cycle_issued_insns;
  235. FENCE_STARTS_CYCLE_P (f) = starts_cycle_p;
  236. FENCE_AFTER_STALL_P (f) = after_stall_p;
  237. gcc_assert (dc != NULL);
  238. FENCE_DC (f) = dc;
  239. gcc_assert (tc != NULL || targetm.sched.alloc_sched_context == NULL);
  240. FENCE_TC (f) = tc;
  241. FENCE_LAST_SCHEDULED_INSN (f) = last_scheduled_insn;
  242. FENCE_ISSUE_MORE (f) = issue_more;
  243. FENCE_EXECUTING_INSNS (f) = executing_insns;
  244. FENCE_READY_TICKS (f) = ready_ticks;
  245. FENCE_READY_TICKS_SIZE (f) = ready_ticks_size;
  246. FENCE_SCHED_NEXT (f) = sched_next;
  247. init_fence_for_scheduling (f);
  248. }
  249. /* Remove the head node of the list pointed to by LP. */
  250. static void
  251. flist_remove (flist_t *lp)
  252. {
  253. if (FENCE_INSN (FLIST_FENCE (*lp)))
  254. fence_clear (FLIST_FENCE (*lp));
  255. _list_remove (lp);
  256. }
  257. /* Clear the fence list pointed to by LP. */
  258. void
  259. flist_clear (flist_t *lp)
  260. {
  261. while (*lp)
  262. flist_remove (lp);
  263. }
  264. /* Add ORIGINAL_INSN the def list DL honoring CROSSES_CALL. */
  265. void
  266. def_list_add (def_list_t *dl, insn_t original_insn, bool crosses_call)
  267. {
  268. def_t d;
  269. _list_add (dl);
  270. d = DEF_LIST_DEF (*dl);
  271. d->orig_insn = original_insn;
  272. d->crosses_call = crosses_call;
  273. }
  274. /* Functions to work with target contexts. */
  275. /* Bulk target context. It is convenient for debugging purposes to ensure
  276. that there are no uninitialized (null) target contexts. */
  277. static tc_t bulk_tc = (tc_t) 1;
  278. /* Target hooks wrappers. In the future we can provide some default
  279. implementations for them. */
  280. /* Allocate a store for the target context. */
  281. static tc_t
  282. alloc_target_context (void)
  283. {
  284. return (targetm.sched.alloc_sched_context
  285. ? targetm.sched.alloc_sched_context () : bulk_tc);
  286. }
  287. /* Init target context TC.
  288. If CLEAN_P is true, then make TC as it is beginning of the scheduler.
  289. Overwise, copy current backend context to TC. */
  290. static void
  291. init_target_context (tc_t tc, bool clean_p)
  292. {
  293. if (targetm.sched.init_sched_context)
  294. targetm.sched.init_sched_context (tc, clean_p);
  295. }
  296. /* Allocate and initialize a target context. Meaning of CLEAN_P is the same as
  297. int init_target_context (). */
  298. tc_t
  299. create_target_context (bool clean_p)
  300. {
  301. tc_t tc = alloc_target_context ();
  302. init_target_context (tc, clean_p);
  303. return tc;
  304. }
  305. /* Copy TC to the current backend context. */
  306. void
  307. set_target_context (tc_t tc)
  308. {
  309. if (targetm.sched.set_sched_context)
  310. targetm.sched.set_sched_context (tc);
  311. }
  312. /* TC is about to be destroyed. Free any internal data. */
  313. static void
  314. clear_target_context (tc_t tc)
  315. {
  316. if (targetm.sched.clear_sched_context)
  317. targetm.sched.clear_sched_context (tc);
  318. }
  319. /* Clear and free it. */
  320. static void
  321. delete_target_context (tc_t tc)
  322. {
  323. clear_target_context (tc);
  324. if (targetm.sched.free_sched_context)
  325. targetm.sched.free_sched_context (tc);
  326. }
  327. /* Make a copy of FROM in TO.
  328. NB: May be this should be a hook. */
  329. static void
  330. copy_target_context (tc_t to, tc_t from)
  331. {
  332. tc_t tmp = create_target_context (false);
  333. set_target_context (from);
  334. init_target_context (to, false);
  335. set_target_context (tmp);
  336. delete_target_context (tmp);
  337. }
  338. /* Create a copy of TC. */
  339. static tc_t
  340. create_copy_of_target_context (tc_t tc)
  341. {
  342. tc_t copy = alloc_target_context ();
  343. copy_target_context (copy, tc);
  344. return copy;
  345. }
  346. /* Clear TC and initialize it according to CLEAN_P. The meaning of CLEAN_P
  347. is the same as in init_target_context (). */
  348. void
  349. reset_target_context (tc_t tc, bool clean_p)
  350. {
  351. clear_target_context (tc);
  352. init_target_context (tc, clean_p);
  353. }
  354. /* Functions to work with dependence contexts.
  355. Dc (aka deps context, aka deps_t, aka struct deps_desc *) is short for dependence
  356. context. It accumulates information about processed insns to decide if
  357. current insn is dependent on the processed ones. */
  358. /* Make a copy of FROM in TO. */
  359. static void
  360. copy_deps_context (deps_t to, deps_t from)
  361. {
  362. init_deps (to, false);
  363. deps_join (to, from);
  364. }
  365. /* Allocate store for dep context. */
  366. static deps_t
  367. alloc_deps_context (void)
  368. {
  369. return XNEW (struct deps_desc);
  370. }
  371. /* Allocate and initialize dep context. */
  372. static deps_t
  373. create_deps_context (void)
  374. {
  375. deps_t dc = alloc_deps_context ();
  376. init_deps (dc, false);
  377. return dc;
  378. }
  379. /* Create a copy of FROM. */
  380. static deps_t
  381. create_copy_of_deps_context (deps_t from)
  382. {
  383. deps_t to = alloc_deps_context ();
  384. copy_deps_context (to, from);
  385. return to;
  386. }
  387. /* Clean up internal data of DC. */
  388. static void
  389. clear_deps_context (deps_t dc)
  390. {
  391. free_deps (dc);
  392. }
  393. /* Clear and free DC. */
  394. static void
  395. delete_deps_context (deps_t dc)
  396. {
  397. clear_deps_context (dc);
  398. free (dc);
  399. }
  400. /* Clear and init DC. */
  401. static void
  402. reset_deps_context (deps_t dc)
  403. {
  404. clear_deps_context (dc);
  405. init_deps (dc, false);
  406. }
  407. /* This structure describes the dependence analysis hooks for advancing
  408. dependence context. */
  409. static struct sched_deps_info_def advance_deps_context_sched_deps_info =
  410. {
  411. NULL,
  412. NULL, /* start_insn */
  413. NULL, /* finish_insn */
  414. NULL, /* start_lhs */
  415. NULL, /* finish_lhs */
  416. NULL, /* start_rhs */
  417. NULL, /* finish_rhs */
  418. haifa_note_reg_set,
  419. haifa_note_reg_clobber,
  420. haifa_note_reg_use,
  421. NULL, /* note_mem_dep */
  422. NULL, /* note_dep */
  423. 0, 0, 0
  424. };
  425. /* Process INSN and add its impact on DC. */
  426. void
  427. advance_deps_context (deps_t dc, insn_t insn)
  428. {
  429. sched_deps_info = &advance_deps_context_sched_deps_info;
  430. deps_analyze_insn (dc, insn);
  431. }
  432. /* Functions to work with DFA states. */
  433. /* Allocate store for a DFA state. */
  434. static state_t
  435. state_alloc (void)
  436. {
  437. return xmalloc (dfa_state_size);
  438. }
  439. /* Allocate and initialize DFA state. */
  440. static state_t
  441. state_create (void)
  442. {
  443. state_t state = state_alloc ();
  444. state_reset (state);
  445. advance_state (state);
  446. return state;
  447. }
  448. /* Free DFA state. */
  449. static void
  450. state_free (state_t state)
  451. {
  452. free (state);
  453. }
  454. /* Make a copy of FROM in TO. */
  455. static void
  456. state_copy (state_t to, state_t from)
  457. {
  458. memcpy (to, from, dfa_state_size);
  459. }
  460. /* Create a copy of FROM. */
  461. static state_t
  462. state_create_copy (state_t from)
  463. {
  464. state_t to = state_alloc ();
  465. state_copy (to, from);
  466. return to;
  467. }
  468. /* Functions to work with fences. */
  469. /* Clear the fence. */
  470. static void
  471. fence_clear (fence_t f)
  472. {
  473. state_t s = FENCE_STATE (f);
  474. deps_t dc = FENCE_DC (f);
  475. void *tc = FENCE_TC (f);
  476. ilist_clear (&FENCE_BNDS (f));
  477. gcc_assert ((s != NULL && dc != NULL && tc != NULL)
  478. || (s == NULL && dc == NULL && tc == NULL));
  479. free (s);
  480. if (dc != NULL)
  481. delete_deps_context (dc);
  482. if (tc != NULL)
  483. delete_target_context (tc);
  484. vec_free (FENCE_EXECUTING_INSNS (f));
  485. free (FENCE_READY_TICKS (f));
  486. FENCE_READY_TICKS (f) = NULL;
  487. }
  488. /* Init a list of fences with successors of OLD_FENCE. */
  489. void
  490. init_fences (insn_t old_fence)
  491. {
  492. insn_t succ;
  493. succ_iterator si;
  494. bool first = true;
  495. int ready_ticks_size = get_max_uid () + 1;
  496. FOR_EACH_SUCC_1 (succ, si, old_fence,
  497. SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
  498. {
  499. if (first)
  500. first = false;
  501. else
  502. gcc_assert (flag_sel_sched_pipelining_outer_loops);
  503. flist_add (&fences, succ,
  504. state_create (),
  505. create_deps_context () /* dc */,
  506. create_target_context (true) /* tc */,
  507. NULL /* last_scheduled_insn */,
  508. NULL, /* executing_insns */
  509. XCNEWVEC (int, ready_ticks_size), /* ready_ticks */
  510. ready_ticks_size,
  511. NULL /* sched_next */,
  512. 1 /* cycle */, 0 /* cycle_issued_insns */,
  513. issue_rate, /* issue_more */
  514. 1 /* starts_cycle_p */, 0 /* after_stall_p */);
  515. }
  516. }
  517. /* Merges two fences (filling fields of fence F with resulting values) by
  518. following rules: 1) state, target context and last scheduled insn are
  519. propagated from fallthrough edge if it is available;
  520. 2) deps context and cycle is propagated from more probable edge;
  521. 3) all other fields are set to corresponding constant values.
  522. INSN, STATE, DC, TC, LAST_SCHEDULED_INSN, EXECUTING_INSNS,
  523. READY_TICKS, READY_TICKS_SIZE, SCHED_NEXT, CYCLE, ISSUE_MORE
  524. and AFTER_STALL_P are the corresponding fields of the second fence. */
  525. static void
  526. merge_fences (fence_t f, insn_t insn,
  527. state_t state, deps_t dc, void *tc,
  528. rtx_insn *last_scheduled_insn,
  529. vec<rtx_insn *, va_gc> *executing_insns,
  530. int *ready_ticks, int ready_ticks_size,
  531. rtx sched_next, int cycle, int issue_more, bool after_stall_p)
  532. {
  533. insn_t last_scheduled_insn_old = FENCE_LAST_SCHEDULED_INSN (f);
  534. gcc_assert (sel_bb_head_p (FENCE_INSN (f))
  535. && !sched_next && !FENCE_SCHED_NEXT (f));
  536. /* Check if we can decide which path fences came.
  537. If we can't (or don't want to) - reset all. */
  538. if (last_scheduled_insn == NULL
  539. || last_scheduled_insn_old == NULL
  540. /* This is a case when INSN is reachable on several paths from
  541. one insn (this can happen when pipelining of outer loops is on and
  542. there are two edges: one going around of inner loop and the other -
  543. right through it; in such case just reset everything). */
  544. || last_scheduled_insn == last_scheduled_insn_old)
  545. {
  546. state_reset (FENCE_STATE (f));
  547. state_free (state);
  548. reset_deps_context (FENCE_DC (f));
  549. delete_deps_context (dc);
  550. reset_target_context (FENCE_TC (f), true);
  551. delete_target_context (tc);
  552. if (cycle > FENCE_CYCLE (f))
  553. FENCE_CYCLE (f) = cycle;
  554. FENCE_LAST_SCHEDULED_INSN (f) = NULL;
  555. FENCE_ISSUE_MORE (f) = issue_rate;
  556. vec_free (executing_insns);
  557. free (ready_ticks);
  558. if (FENCE_EXECUTING_INSNS (f))
  559. FENCE_EXECUTING_INSNS (f)->block_remove (0,
  560. FENCE_EXECUTING_INSNS (f)->length ());
  561. if (FENCE_READY_TICKS (f))
  562. memset (FENCE_READY_TICKS (f), 0, FENCE_READY_TICKS_SIZE (f));
  563. }
  564. else
  565. {
  566. edge edge_old = NULL, edge_new = NULL;
  567. edge candidate;
  568. succ_iterator si;
  569. insn_t succ;
  570. /* Find fallthrough edge. */
  571. gcc_assert (BLOCK_FOR_INSN (insn)->prev_bb);
  572. candidate = find_fallthru_edge_from (BLOCK_FOR_INSN (insn)->prev_bb);
  573. if (!candidate
  574. || (candidate->src != BLOCK_FOR_INSN (last_scheduled_insn)
  575. && candidate->src != BLOCK_FOR_INSN (last_scheduled_insn_old)))
  576. {
  577. /* No fallthrough edge leading to basic block of INSN. */
  578. state_reset (FENCE_STATE (f));
  579. state_free (state);
  580. reset_target_context (FENCE_TC (f), true);
  581. delete_target_context (tc);
  582. FENCE_LAST_SCHEDULED_INSN (f) = NULL;
  583. FENCE_ISSUE_MORE (f) = issue_rate;
  584. }
  585. else
  586. if (candidate->src == BLOCK_FOR_INSN (last_scheduled_insn))
  587. {
  588. /* Would be weird if same insn is successor of several fallthrough
  589. edges. */
  590. gcc_assert (BLOCK_FOR_INSN (insn)->prev_bb
  591. != BLOCK_FOR_INSN (last_scheduled_insn_old));
  592. state_free (FENCE_STATE (f));
  593. FENCE_STATE (f) = state;
  594. delete_target_context (FENCE_TC (f));
  595. FENCE_TC (f) = tc;
  596. FENCE_LAST_SCHEDULED_INSN (f) = last_scheduled_insn;
  597. FENCE_ISSUE_MORE (f) = issue_more;
  598. }
  599. else
  600. {
  601. /* Leave STATE, TC and LAST_SCHEDULED_INSN fields untouched. */
  602. state_free (state);
  603. delete_target_context (tc);
  604. gcc_assert (BLOCK_FOR_INSN (insn)->prev_bb
  605. != BLOCK_FOR_INSN (last_scheduled_insn));
  606. }
  607. /* Find edge of first predecessor (last_scheduled_insn_old->insn). */
  608. FOR_EACH_SUCC_1 (succ, si, last_scheduled_insn_old,
  609. SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
  610. {
  611. if (succ == insn)
  612. {
  613. /* No same successor allowed from several edges. */
  614. gcc_assert (!edge_old);
  615. edge_old = si.e1;
  616. }
  617. }
  618. /* Find edge of second predecessor (last_scheduled_insn->insn). */
  619. FOR_EACH_SUCC_1 (succ, si, last_scheduled_insn,
  620. SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
  621. {
  622. if (succ == insn)
  623. {
  624. /* No same successor allowed from several edges. */
  625. gcc_assert (!edge_new);
  626. edge_new = si.e1;
  627. }
  628. }
  629. /* Check if we can choose most probable predecessor. */
  630. if (edge_old == NULL || edge_new == NULL)
  631. {
  632. reset_deps_context (FENCE_DC (f));
  633. delete_deps_context (dc);
  634. vec_free (executing_insns);
  635. free (ready_ticks);
  636. FENCE_CYCLE (f) = MAX (FENCE_CYCLE (f), cycle);
  637. if (FENCE_EXECUTING_INSNS (f))
  638. FENCE_EXECUTING_INSNS (f)->block_remove (0,
  639. FENCE_EXECUTING_INSNS (f)->length ());
  640. if (FENCE_READY_TICKS (f))
  641. memset (FENCE_READY_TICKS (f), 0, FENCE_READY_TICKS_SIZE (f));
  642. }
  643. else
  644. if (edge_new->probability > edge_old->probability)
  645. {
  646. delete_deps_context (FENCE_DC (f));
  647. FENCE_DC (f) = dc;
  648. vec_free (FENCE_EXECUTING_INSNS (f));
  649. FENCE_EXECUTING_INSNS (f) = executing_insns;
  650. free (FENCE_READY_TICKS (f));
  651. FENCE_READY_TICKS (f) = ready_ticks;
  652. FENCE_READY_TICKS_SIZE (f) = ready_ticks_size;
  653. FENCE_CYCLE (f) = cycle;
  654. }
  655. else
  656. {
  657. /* Leave DC and CYCLE untouched. */
  658. delete_deps_context (dc);
  659. vec_free (executing_insns);
  660. free (ready_ticks);
  661. }
  662. }
  663. /* Fill remaining invariant fields. */
  664. if (after_stall_p)
  665. FENCE_AFTER_STALL_P (f) = 1;
  666. FENCE_ISSUED_INSNS (f) = 0;
  667. FENCE_STARTS_CYCLE_P (f) = 1;
  668. FENCE_SCHED_NEXT (f) = NULL;
  669. }
  670. /* Add a new fence to NEW_FENCES list, initializing it from all
  671. other parameters. */
  672. static void
  673. add_to_fences (flist_tail_t new_fences, insn_t insn,
  674. state_t state, deps_t dc, void *tc,
  675. rtx_insn *last_scheduled_insn,
  676. vec<rtx_insn *, va_gc> *executing_insns, int *ready_ticks,
  677. int ready_ticks_size, rtx_insn *sched_next, int cycle,
  678. int cycle_issued_insns, int issue_rate,
  679. bool starts_cycle_p, bool after_stall_p)
  680. {
  681. fence_t f = flist_lookup (FLIST_TAIL_HEAD (new_fences), insn);
  682. if (! f)
  683. {
  684. flist_add (FLIST_TAIL_TAILP (new_fences), insn, state, dc, tc,
  685. last_scheduled_insn, executing_insns, ready_ticks,
  686. ready_ticks_size, sched_next, cycle, cycle_issued_insns,
  687. issue_rate, starts_cycle_p, after_stall_p);
  688. FLIST_TAIL_TAILP (new_fences)
  689. = &FLIST_NEXT (*FLIST_TAIL_TAILP (new_fences));
  690. }
  691. else
  692. {
  693. merge_fences (f, insn, state, dc, tc, last_scheduled_insn,
  694. executing_insns, ready_ticks, ready_ticks_size,
  695. sched_next, cycle, issue_rate, after_stall_p);
  696. }
  697. }
  698. /* Move the first fence in the OLD_FENCES list to NEW_FENCES. */
  699. void
  700. move_fence_to_fences (flist_t old_fences, flist_tail_t new_fences)
  701. {
  702. fence_t f, old;
  703. flist_t *tailp = FLIST_TAIL_TAILP (new_fences);
  704. old = FLIST_FENCE (old_fences);
  705. f = flist_lookup (FLIST_TAIL_HEAD (new_fences),
  706. FENCE_INSN (FLIST_FENCE (old_fences)));
  707. if (f)
  708. {
  709. merge_fences (f, old->insn, old->state, old->dc, old->tc,
  710. old->last_scheduled_insn, old->executing_insns,
  711. old->ready_ticks, old->ready_ticks_size,
  712. old->sched_next, old->cycle, old->issue_more,
  713. old->after_stall_p);
  714. }
  715. else
  716. {
  717. _list_add (tailp);
  718. FLIST_TAIL_TAILP (new_fences) = &FLIST_NEXT (*tailp);
  719. *FLIST_FENCE (*tailp) = *old;
  720. init_fence_for_scheduling (FLIST_FENCE (*tailp));
  721. }
  722. FENCE_INSN (old) = NULL;
  723. }
  724. /* Add a new fence to NEW_FENCES list and initialize most of its data
  725. as a clean one. */
  726. void
  727. add_clean_fence_to_fences (flist_tail_t new_fences, insn_t succ, fence_t fence)
  728. {
  729. int ready_ticks_size = get_max_uid () + 1;
  730. add_to_fences (new_fences,
  731. succ, state_create (), create_deps_context (),
  732. create_target_context (true),
  733. NULL, NULL,
  734. XCNEWVEC (int, ready_ticks_size), ready_ticks_size,
  735. NULL, FENCE_CYCLE (fence) + 1,
  736. 0, issue_rate, 1, FENCE_AFTER_STALL_P (fence));
  737. }
  738. /* Add a new fence to NEW_FENCES list and initialize all of its data
  739. from FENCE and SUCC. */
  740. void
  741. add_dirty_fence_to_fences (flist_tail_t new_fences, insn_t succ, fence_t fence)
  742. {
  743. int * new_ready_ticks
  744. = XNEWVEC (int, FENCE_READY_TICKS_SIZE (fence));
  745. memcpy (new_ready_ticks, FENCE_READY_TICKS (fence),
  746. FENCE_READY_TICKS_SIZE (fence) * sizeof (int));
  747. add_to_fences (new_fences,
  748. succ, state_create_copy (FENCE_STATE (fence)),
  749. create_copy_of_deps_context (FENCE_DC (fence)),
  750. create_copy_of_target_context (FENCE_TC (fence)),
  751. FENCE_LAST_SCHEDULED_INSN (fence),
  752. vec_safe_copy (FENCE_EXECUTING_INSNS (fence)),
  753. new_ready_ticks,
  754. FENCE_READY_TICKS_SIZE (fence),
  755. FENCE_SCHED_NEXT (fence),
  756. FENCE_CYCLE (fence),
  757. FENCE_ISSUED_INSNS (fence),
  758. FENCE_ISSUE_MORE (fence),
  759. FENCE_STARTS_CYCLE_P (fence),
  760. FENCE_AFTER_STALL_P (fence));
  761. }
  762. /* Functions to work with regset and nop pools. */
  763. /* Returns the new regset from pool. It might have some of the bits set
  764. from the previous usage. */
  765. regset
  766. get_regset_from_pool (void)
  767. {
  768. regset rs;
  769. if (regset_pool.n != 0)
  770. rs = regset_pool.v[--regset_pool.n];
  771. else
  772. /* We need to create the regset. */
  773. {
  774. rs = ALLOC_REG_SET (&reg_obstack);
  775. if (regset_pool.nn == regset_pool.ss)
  776. regset_pool.vv = XRESIZEVEC (regset, regset_pool.vv,
  777. (regset_pool.ss = 2 * regset_pool.ss + 1));
  778. regset_pool.vv[regset_pool.nn++] = rs;
  779. }
  780. regset_pool.diff++;
  781. return rs;
  782. }
  783. /* Same as above, but returns the empty regset. */
  784. regset
  785. get_clear_regset_from_pool (void)
  786. {
  787. regset rs = get_regset_from_pool ();
  788. CLEAR_REG_SET (rs);
  789. return rs;
  790. }
  791. /* Return regset RS to the pool for future use. */
  792. void
  793. return_regset_to_pool (regset rs)
  794. {
  795. gcc_assert (rs);
  796. regset_pool.diff--;
  797. if (regset_pool.n == regset_pool.s)
  798. regset_pool.v = XRESIZEVEC (regset, regset_pool.v,
  799. (regset_pool.s = 2 * regset_pool.s + 1));
  800. regset_pool.v[regset_pool.n++] = rs;
  801. }
  802. #ifdef ENABLE_CHECKING
  803. /* This is used as a qsort callback for sorting regset pool stacks.
  804. X and XX are addresses of two regsets. They are never equal. */
  805. static int
  806. cmp_v_in_regset_pool (const void *x, const void *xx)
  807. {
  808. uintptr_t r1 = (uintptr_t) *((const regset *) x);
  809. uintptr_t r2 = (uintptr_t) *((const regset *) xx);
  810. if (r1 > r2)
  811. return 1;
  812. else if (r1 < r2)
  813. return -1;
  814. gcc_unreachable ();
  815. }
  816. #endif
  817. /* Free the regset pool possibly checking for memory leaks. */
  818. void
  819. free_regset_pool (void)
  820. {
  821. #ifdef ENABLE_CHECKING
  822. {
  823. regset *v = regset_pool.v;
  824. int i = 0;
  825. int n = regset_pool.n;
  826. regset *vv = regset_pool.vv;
  827. int ii = 0;
  828. int nn = regset_pool.nn;
  829. int diff = 0;
  830. gcc_assert (n <= nn);
  831. /* Sort both vectors so it will be possible to compare them. */
  832. qsort (v, n, sizeof (*v), cmp_v_in_regset_pool);
  833. qsort (vv, nn, sizeof (*vv), cmp_v_in_regset_pool);
  834. while (ii < nn)
  835. {
  836. if (v[i] == vv[ii])
  837. i++;
  838. else
  839. /* VV[II] was lost. */
  840. diff++;
  841. ii++;
  842. }
  843. gcc_assert (diff == regset_pool.diff);
  844. }
  845. #endif
  846. /* If not true - we have a memory leak. */
  847. gcc_assert (regset_pool.diff == 0);
  848. while (regset_pool.n)
  849. {
  850. --regset_pool.n;
  851. FREE_REG_SET (regset_pool.v[regset_pool.n]);
  852. }
  853. free (regset_pool.v);
  854. regset_pool.v = NULL;
  855. regset_pool.s = 0;
  856. free (regset_pool.vv);
  857. regset_pool.vv = NULL;
  858. regset_pool.nn = 0;
  859. regset_pool.ss = 0;
  860. regset_pool.diff = 0;
  861. }
  862. /* Functions to work with nop pools. NOP insns are used as temporary
  863. placeholders of the insns being scheduled to allow correct update of
  864. the data sets. When update is finished, NOPs are deleted. */
  865. /* A vinsn that is used to represent a nop. This vinsn is shared among all
  866. nops sel-sched generates. */
  867. static vinsn_t nop_vinsn = NULL;
  868. /* Emit a nop before INSN, taking it from pool. */
  869. insn_t
  870. get_nop_from_pool (insn_t insn)
  871. {
  872. rtx nop_pat;
  873. insn_t nop;
  874. bool old_p = nop_pool.n != 0;
  875. int flags;
  876. if (old_p)
  877. nop_pat = nop_pool.v[--nop_pool.n];
  878. else
  879. nop_pat = nop_pattern;
  880. nop = emit_insn_before (nop_pat, insn);
  881. if (old_p)
  882. flags = INSN_INIT_TODO_SSID;
  883. else
  884. flags = INSN_INIT_TODO_LUID | INSN_INIT_TODO_SSID;
  885. set_insn_init (INSN_EXPR (insn), nop_vinsn, INSN_SEQNO (insn));
  886. sel_init_new_insn (nop, flags);
  887. return nop;
  888. }
  889. /* Remove NOP from the instruction stream and return it to the pool. */
  890. void
  891. return_nop_to_pool (insn_t nop, bool full_tidying)
  892. {
  893. gcc_assert (INSN_IN_STREAM_P (nop));
  894. sel_remove_insn (nop, false, full_tidying);
  895. /* We'll recycle this nop. */
  896. nop->set_undeleted ();
  897. if (nop_pool.n == nop_pool.s)
  898. nop_pool.v = XRESIZEVEC (rtx_insn *, nop_pool.v,
  899. (nop_pool.s = 2 * nop_pool.s + 1));
  900. nop_pool.v[nop_pool.n++] = nop;
  901. }
  902. /* Free the nop pool. */
  903. void
  904. free_nop_pool (void)
  905. {
  906. nop_pool.n = 0;
  907. nop_pool.s = 0;
  908. free (nop_pool.v);
  909. nop_pool.v = NULL;
  910. }
  911. /* Skip unspec to support ia64 speculation. Called from rtx_equal_p_cb.
  912. The callback is given two rtxes XX and YY and writes the new rtxes
  913. to NX and NY in case some needs to be skipped. */
  914. static int
  915. skip_unspecs_callback (const_rtx *xx, const_rtx *yy, rtx *nx, rtx* ny)
  916. {
  917. const_rtx x = *xx;
  918. const_rtx y = *yy;
  919. if (GET_CODE (x) == UNSPEC
  920. && (targetm.sched.skip_rtx_p == NULL
  921. || targetm.sched.skip_rtx_p (x)))
  922. {
  923. *nx = XVECEXP (x, 0, 0);
  924. *ny = CONST_CAST_RTX (y);
  925. return 1;
  926. }
  927. if (GET_CODE (y) == UNSPEC
  928. && (targetm.sched.skip_rtx_p == NULL
  929. || targetm.sched.skip_rtx_p (y)))
  930. {
  931. *nx = CONST_CAST_RTX (x);
  932. *ny = XVECEXP (y, 0, 0);
  933. return 1;
  934. }
  935. return 0;
  936. }
  937. /* Callback, called from hash_rtx_cb. Helps to hash UNSPEC rtx X in a correct way
  938. to support ia64 speculation. When changes are needed, new rtx X and new mode
  939. NMODE are written, and the callback returns true. */
  940. static int
  941. hash_with_unspec_callback (const_rtx x, machine_mode mode ATTRIBUTE_UNUSED,
  942. rtx *nx, machine_mode* nmode)
  943. {
  944. if (GET_CODE (x) == UNSPEC
  945. && targetm.sched.skip_rtx_p
  946. && targetm.sched.skip_rtx_p (x))
  947. {
  948. *nx = XVECEXP (x, 0 ,0);
  949. *nmode = VOIDmode;
  950. return 1;
  951. }
  952. return 0;
  953. }
  954. /* Returns LHS and RHS are ok to be scheduled separately. */
  955. static bool
  956. lhs_and_rhs_separable_p (rtx lhs, rtx rhs)
  957. {
  958. if (lhs == NULL || rhs == NULL)
  959. return false;
  960. /* Do not schedule constants as rhs: no point to use reg, if const
  961. can be used. Moreover, scheduling const as rhs may lead to mode
  962. mismatch cause consts don't have modes but they could be merged
  963. from branches where the same const used in different modes. */
  964. if (CONSTANT_P (rhs))
  965. return false;
  966. /* ??? Do not rename predicate registers to avoid ICEs in bundling. */
  967. if (COMPARISON_P (rhs))
  968. return false;
  969. /* Do not allow single REG to be an rhs. */
  970. if (REG_P (rhs))
  971. return false;
  972. /* See comment at find_used_regs_1 (*1) for explanation of this
  973. restriction. */
  974. /* FIXME: remove this later. */
  975. if (MEM_P (lhs))
  976. return false;
  977. /* This will filter all tricky things like ZERO_EXTRACT etc.
  978. For now we don't handle it. */
  979. if (!REG_P (lhs) && !MEM_P (lhs))
  980. return false;
  981. return true;
  982. }
  983. /* Initialize vinsn VI for INSN. Only for use from vinsn_create (). When
  984. FORCE_UNIQUE_P is true, the resulting vinsn will not be clonable. This is
  985. used e.g. for insns from recovery blocks. */
  986. static void
  987. vinsn_init (vinsn_t vi, insn_t insn, bool force_unique_p)
  988. {
  989. hash_rtx_callback_function hrcf;
  990. int insn_class;
  991. VINSN_INSN_RTX (vi) = insn;
  992. VINSN_COUNT (vi) = 0;
  993. vi->cost = -1;
  994. if (INSN_NOP_P (insn))
  995. return;
  996. if (DF_INSN_UID_SAFE_GET (INSN_UID (insn)) != NULL)
  997. init_id_from_df (VINSN_ID (vi), insn, force_unique_p);
  998. else
  999. deps_init_id (VINSN_ID (vi), insn, force_unique_p);
  1000. /* Hash vinsn depending on whether it is separable or not. */
  1001. hrcf = targetm.sched.skip_rtx_p ? hash_with_unspec_callback : NULL;
  1002. if (VINSN_SEPARABLE_P (vi))
  1003. {
  1004. rtx rhs = VINSN_RHS (vi);
  1005. VINSN_HASH (vi) = hash_rtx_cb (rhs, GET_MODE (rhs),
  1006. NULL, NULL, false, hrcf);
  1007. VINSN_HASH_RTX (vi) = hash_rtx_cb (VINSN_PATTERN (vi),
  1008. VOIDmode, NULL, NULL,
  1009. false, hrcf);
  1010. }
  1011. else
  1012. {
  1013. VINSN_HASH (vi) = hash_rtx_cb (VINSN_PATTERN (vi), VOIDmode,
  1014. NULL, NULL, false, hrcf);
  1015. VINSN_HASH_RTX (vi) = VINSN_HASH (vi);
  1016. }
  1017. insn_class = haifa_classify_insn (insn);
  1018. if (insn_class >= 2
  1019. && (!targetm.sched.get_insn_spec_ds
  1020. || ((targetm.sched.get_insn_spec_ds (insn) & BEGIN_CONTROL)
  1021. == 0)))
  1022. VINSN_MAY_TRAP_P (vi) = true;
  1023. else
  1024. VINSN_MAY_TRAP_P (vi) = false;
  1025. }
  1026. /* Indicate that VI has become the part of an rtx object. */
  1027. void
  1028. vinsn_attach (vinsn_t vi)
  1029. {
  1030. /* Assert that VI is not pending for deletion. */
  1031. gcc_assert (VINSN_INSN_RTX (vi));
  1032. VINSN_COUNT (vi)++;
  1033. }
  1034. /* Create and init VI from the INSN. Use UNIQUE_P for determining the correct
  1035. VINSN_TYPE (VI). */
  1036. static vinsn_t
  1037. vinsn_create (insn_t insn, bool force_unique_p)
  1038. {
  1039. vinsn_t vi = XCNEW (struct vinsn_def);
  1040. vinsn_init (vi, insn, force_unique_p);
  1041. return vi;
  1042. }
  1043. /* Return a copy of VI. When REATTACH_P is true, detach VI and attach
  1044. the copy. */
  1045. vinsn_t
  1046. vinsn_copy (vinsn_t vi, bool reattach_p)
  1047. {
  1048. rtx_insn *copy;
  1049. bool unique = VINSN_UNIQUE_P (vi);
  1050. vinsn_t new_vi;
  1051. copy = create_copy_of_insn_rtx (VINSN_INSN_RTX (vi));
  1052. new_vi = create_vinsn_from_insn_rtx (copy, unique);
  1053. if (reattach_p)
  1054. {
  1055. vinsn_detach (vi);
  1056. vinsn_attach (new_vi);
  1057. }
  1058. return new_vi;
  1059. }
  1060. /* Delete the VI vinsn and free its data. */
  1061. static void
  1062. vinsn_delete (vinsn_t vi)
  1063. {
  1064. gcc_assert (VINSN_COUNT (vi) == 0);
  1065. if (!INSN_NOP_P (VINSN_INSN_RTX (vi)))
  1066. {
  1067. return_regset_to_pool (VINSN_REG_SETS (vi));
  1068. return_regset_to_pool (VINSN_REG_USES (vi));
  1069. return_regset_to_pool (VINSN_REG_CLOBBERS (vi));
  1070. }
  1071. free (vi);
  1072. }
  1073. /* Indicate that VI is no longer a part of some rtx object.
  1074. Remove VI if it is no longer needed. */
  1075. void
  1076. vinsn_detach (vinsn_t vi)
  1077. {
  1078. gcc_assert (VINSN_COUNT (vi) > 0);
  1079. if (--VINSN_COUNT (vi) == 0)
  1080. vinsn_delete (vi);
  1081. }
  1082. /* Returns TRUE if VI is a branch. */
  1083. bool
  1084. vinsn_cond_branch_p (vinsn_t vi)
  1085. {
  1086. insn_t insn;
  1087. if (!VINSN_UNIQUE_P (vi))
  1088. return false;
  1089. insn = VINSN_INSN_RTX (vi);
  1090. if (BB_END (BLOCK_FOR_INSN (insn)) != insn)
  1091. return false;
  1092. return control_flow_insn_p (insn);
  1093. }
  1094. /* Return latency of INSN. */
  1095. static int
  1096. sel_insn_rtx_cost (rtx_insn *insn)
  1097. {
  1098. int cost;
  1099. /* A USE insn, or something else we don't need to
  1100. understand. We can't pass these directly to
  1101. result_ready_cost or insn_default_latency because it will
  1102. trigger a fatal error for unrecognizable insns. */
  1103. if (recog_memoized (insn) < 0)
  1104. cost = 0;
  1105. else
  1106. {
  1107. cost = insn_default_latency (insn);
  1108. if (cost < 0)
  1109. cost = 0;
  1110. }
  1111. return cost;
  1112. }
  1113. /* Return the cost of the VI.
  1114. !!! FIXME: Unify with haifa-sched.c: insn_cost (). */
  1115. int
  1116. sel_vinsn_cost (vinsn_t vi)
  1117. {
  1118. int cost = vi->cost;
  1119. if (cost < 0)
  1120. {
  1121. cost = sel_insn_rtx_cost (VINSN_INSN_RTX (vi));
  1122. vi->cost = cost;
  1123. }
  1124. return cost;
  1125. }
  1126. /* Functions for insn emitting. */
  1127. /* Emit new insn after AFTER based on PATTERN and initialize its data from
  1128. EXPR and SEQNO. */
  1129. insn_t
  1130. sel_gen_insn_from_rtx_after (rtx pattern, expr_t expr, int seqno, insn_t after)
  1131. {
  1132. insn_t new_insn;
  1133. gcc_assert (EXPR_TARGET_AVAILABLE (expr) == true);
  1134. new_insn = emit_insn_after (pattern, after);
  1135. set_insn_init (expr, NULL, seqno);
  1136. sel_init_new_insn (new_insn, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SSID);
  1137. return new_insn;
  1138. }
  1139. /* Force newly generated vinsns to be unique. */
  1140. static bool init_insn_force_unique_p = false;
  1141. /* Emit new speculation recovery insn after AFTER based on PATTERN and
  1142. initialize its data from EXPR and SEQNO. */
  1143. insn_t
  1144. sel_gen_recovery_insn_from_rtx_after (rtx pattern, expr_t expr, int seqno,
  1145. insn_t after)
  1146. {
  1147. insn_t insn;
  1148. gcc_assert (!init_insn_force_unique_p);
  1149. init_insn_force_unique_p = true;
  1150. insn = sel_gen_insn_from_rtx_after (pattern, expr, seqno, after);
  1151. CANT_MOVE (insn) = 1;
  1152. init_insn_force_unique_p = false;
  1153. return insn;
  1154. }
  1155. /* Emit new insn after AFTER based on EXPR and SEQNO. If VINSN is not NULL,
  1156. take it as a new vinsn instead of EXPR's vinsn.
  1157. We simplify insns later, after scheduling region in
  1158. simplify_changed_insns. */
  1159. insn_t
  1160. sel_gen_insn_from_expr_after (expr_t expr, vinsn_t vinsn, int seqno,
  1161. insn_t after)
  1162. {
  1163. expr_t emit_expr;
  1164. insn_t insn;
  1165. int flags;
  1166. emit_expr = set_insn_init (expr, vinsn ? vinsn : EXPR_VINSN (expr),
  1167. seqno);
  1168. insn = EXPR_INSN_RTX (emit_expr);
  1169. /* The insn may come from the transformation cache, which may hold already
  1170. deleted insns, so mark it as not deleted. */
  1171. insn->set_undeleted ();
  1172. add_insn_after (insn, after, BLOCK_FOR_INSN (insn));
  1173. flags = INSN_INIT_TODO_SSID;
  1174. if (INSN_LUID (insn) == 0)
  1175. flags |= INSN_INIT_TODO_LUID;
  1176. sel_init_new_insn (insn, flags);
  1177. return insn;
  1178. }
  1179. /* Move insn from EXPR after AFTER. */
  1180. insn_t
  1181. sel_move_insn (expr_t expr, int seqno, insn_t after)
  1182. {
  1183. insn_t insn = EXPR_INSN_RTX (expr);
  1184. basic_block bb = BLOCK_FOR_INSN (after);
  1185. insn_t next = NEXT_INSN (after);
  1186. /* Assert that in move_op we disconnected this insn properly. */
  1187. gcc_assert (EXPR_VINSN (INSN_EXPR (insn)) != NULL);
  1188. SET_PREV_INSN (insn) = after;
  1189. SET_NEXT_INSN (insn) = next;
  1190. SET_NEXT_INSN (after) = insn;
  1191. SET_PREV_INSN (next) = insn;
  1192. /* Update links from insn to bb and vice versa. */
  1193. df_insn_change_bb (insn, bb);
  1194. if (BB_END (bb) == after)
  1195. BB_END (bb) = insn;
  1196. prepare_insn_expr (insn, seqno);
  1197. return insn;
  1198. }
  1199. /* Functions to work with right-hand sides. */
  1200. /* Search for a hash value determined by UID/NEW_VINSN in a sorted vector
  1201. VECT and return true when found. Use NEW_VINSN for comparison only when
  1202. COMPARE_VINSNS is true. Write to INDP the index on which
  1203. the search has stopped, such that inserting the new element at INDP will
  1204. retain VECT's sort order. */
  1205. static bool
  1206. find_in_history_vect_1 (vec<expr_history_def> vect,
  1207. unsigned uid, vinsn_t new_vinsn,
  1208. bool compare_vinsns, int *indp)
  1209. {
  1210. expr_history_def *arr;
  1211. int i, j, len = vect.length ();
  1212. if (len == 0)
  1213. {
  1214. *indp = 0;
  1215. return false;
  1216. }
  1217. arr = vect.address ();
  1218. i = 0, j = len - 1;
  1219. while (i <= j)
  1220. {
  1221. unsigned auid = arr[i].uid;
  1222. vinsn_t avinsn = arr[i].new_expr_vinsn;
  1223. if (auid == uid
  1224. /* When undoing transformation on a bookkeeping copy, the new vinsn
  1225. may not be exactly equal to the one that is saved in the vector.
  1226. This is because the insn whose copy we're checking was possibly
  1227. substituted itself. */
  1228. && (! compare_vinsns
  1229. || vinsn_equal_p (avinsn, new_vinsn)))
  1230. {
  1231. *indp = i;
  1232. return true;
  1233. }
  1234. else if (auid > uid)
  1235. break;
  1236. i++;
  1237. }
  1238. *indp = i;
  1239. return false;
  1240. }
  1241. /* Search for a uid of INSN and NEW_VINSN in a sorted vector VECT. Return
  1242. the position found or -1, if no such value is in vector.
  1243. Search also for UIDs of insn's originators, if ORIGINATORS_P is true. */
  1244. int
  1245. find_in_history_vect (vec<expr_history_def> vect, rtx insn,
  1246. vinsn_t new_vinsn, bool originators_p)
  1247. {
  1248. int ind;
  1249. if (find_in_history_vect_1 (vect, INSN_UID (insn), new_vinsn,
  1250. false, &ind))
  1251. return ind;
  1252. if (INSN_ORIGINATORS (insn) && originators_p)
  1253. {
  1254. unsigned uid;
  1255. bitmap_iterator bi;
  1256. EXECUTE_IF_SET_IN_BITMAP (INSN_ORIGINATORS (insn), 0, uid, bi)
  1257. if (find_in_history_vect_1 (vect, uid, new_vinsn, false, &ind))
  1258. return ind;
  1259. }
  1260. return -1;
  1261. }
  1262. /* Insert new element in a sorted history vector pointed to by PVECT,
  1263. if it is not there already. The element is searched using
  1264. UID/NEW_EXPR_VINSN pair. TYPE, OLD_EXPR_VINSN and SPEC_DS save
  1265. the history of a transformation. */
  1266. void
  1267. insert_in_history_vect (vec<expr_history_def> *pvect,
  1268. unsigned uid, enum local_trans_type type,
  1269. vinsn_t old_expr_vinsn, vinsn_t new_expr_vinsn,
  1270. ds_t spec_ds)
  1271. {
  1272. vec<expr_history_def> vect = *pvect;
  1273. expr_history_def temp;
  1274. bool res;
  1275. int ind;
  1276. res = find_in_history_vect_1 (vect, uid, new_expr_vinsn, true, &ind);
  1277. if (res)
  1278. {
  1279. expr_history_def *phist = &vect[ind];
  1280. /* It is possible that speculation types of expressions that were
  1281. propagated through different paths will be different here. In this
  1282. case, merge the status to get the correct check later. */
  1283. if (phist->spec_ds != spec_ds)
  1284. phist->spec_ds = ds_max_merge (phist->spec_ds, spec_ds);
  1285. return;
  1286. }
  1287. temp.uid = uid;
  1288. temp.old_expr_vinsn = old_expr_vinsn;
  1289. temp.new_expr_vinsn = new_expr_vinsn;
  1290. temp.spec_ds = spec_ds;
  1291. temp.type = type;
  1292. vinsn_attach (old_expr_vinsn);
  1293. vinsn_attach (new_expr_vinsn);
  1294. vect.safe_insert (ind, temp);
  1295. *pvect = vect;
  1296. }
  1297. /* Free history vector PVECT. */
  1298. static void
  1299. free_history_vect (vec<expr_history_def> &pvect)
  1300. {
  1301. unsigned i;
  1302. expr_history_def *phist;
  1303. if (! pvect.exists ())
  1304. return;
  1305. for (i = 0; pvect.iterate (i, &phist); i++)
  1306. {
  1307. vinsn_detach (phist->old_expr_vinsn);
  1308. vinsn_detach (phist->new_expr_vinsn);
  1309. }
  1310. pvect.release ();
  1311. }
  1312. /* Merge vector FROM to PVECT. */
  1313. static void
  1314. merge_history_vect (vec<expr_history_def> *pvect,
  1315. vec<expr_history_def> from)
  1316. {
  1317. expr_history_def *phist;
  1318. int i;
  1319. /* We keep this vector sorted. */
  1320. for (i = 0; from.iterate (i, &phist); i++)
  1321. insert_in_history_vect (pvect, phist->uid, phist->type,
  1322. phist->old_expr_vinsn, phist->new_expr_vinsn,
  1323. phist->spec_ds);
  1324. }
  1325. /* Compare two vinsns as rhses if possible and as vinsns otherwise. */
  1326. bool
  1327. vinsn_equal_p (vinsn_t x, vinsn_t y)
  1328. {
  1329. rtx_equal_p_callback_function repcf;
  1330. if (x == y)
  1331. return true;
  1332. if (VINSN_TYPE (x) != VINSN_TYPE (y))
  1333. return false;
  1334. if (VINSN_HASH (x) != VINSN_HASH (y))
  1335. return false;
  1336. repcf = targetm.sched.skip_rtx_p ? skip_unspecs_callback : NULL;
  1337. if (VINSN_SEPARABLE_P (x))
  1338. {
  1339. /* Compare RHSes of VINSNs. */
  1340. gcc_assert (VINSN_RHS (x));
  1341. gcc_assert (VINSN_RHS (y));
  1342. return rtx_equal_p_cb (VINSN_RHS (x), VINSN_RHS (y), repcf);
  1343. }
  1344. return rtx_equal_p_cb (VINSN_PATTERN (x), VINSN_PATTERN (y), repcf);
  1345. }
  1346. /* Functions for working with expressions. */
  1347. /* Initialize EXPR. */
  1348. static void
  1349. init_expr (expr_t expr, vinsn_t vi, int spec, int use, int priority,
  1350. int sched_times, int orig_bb_index, ds_t spec_done_ds,
  1351. ds_t spec_to_check_ds, int orig_sched_cycle,
  1352. vec<expr_history_def> history,
  1353. signed char target_available,
  1354. bool was_substituted, bool was_renamed, bool needs_spec_check_p,
  1355. bool cant_move)
  1356. {
  1357. vinsn_attach (vi);
  1358. EXPR_VINSN (expr) = vi;
  1359. EXPR_SPEC (expr) = spec;
  1360. EXPR_USEFULNESS (expr) = use;
  1361. EXPR_PRIORITY (expr) = priority;
  1362. EXPR_PRIORITY_ADJ (expr) = 0;
  1363. EXPR_SCHED_TIMES (expr) = sched_times;
  1364. EXPR_ORIG_BB_INDEX (expr) = orig_bb_index;
  1365. EXPR_ORIG_SCHED_CYCLE (expr) = orig_sched_cycle;
  1366. EXPR_SPEC_DONE_DS (expr) = spec_done_ds;
  1367. EXPR_SPEC_TO_CHECK_DS (expr) = spec_to_check_ds;
  1368. if (history.exists ())
  1369. EXPR_HISTORY_OF_CHANGES (expr) = history;
  1370. else
  1371. EXPR_HISTORY_OF_CHANGES (expr).create (0);
  1372. EXPR_TARGET_AVAILABLE (expr) = target_available;
  1373. EXPR_WAS_SUBSTITUTED (expr) = was_substituted;
  1374. EXPR_WAS_RENAMED (expr) = was_renamed;
  1375. EXPR_NEEDS_SPEC_CHECK_P (expr) = needs_spec_check_p;
  1376. EXPR_CANT_MOVE (expr) = cant_move;
  1377. }
  1378. /* Make a copy of the expr FROM into the expr TO. */
  1379. void
  1380. copy_expr (expr_t to, expr_t from)
  1381. {
  1382. vec<expr_history_def> temp = vNULL;
  1383. if (EXPR_HISTORY_OF_CHANGES (from).exists ())
  1384. {
  1385. unsigned i;
  1386. expr_history_def *phist;
  1387. temp = EXPR_HISTORY_OF_CHANGES (from).copy ();
  1388. for (i = 0;
  1389. temp.iterate (i, &phist);
  1390. i++)
  1391. {
  1392. vinsn_attach (phist->old_expr_vinsn);
  1393. vinsn_attach (phist->new_expr_vinsn);
  1394. }
  1395. }
  1396. init_expr (to, EXPR_VINSN (from), EXPR_SPEC (from),
  1397. EXPR_USEFULNESS (from), EXPR_PRIORITY (from),
  1398. EXPR_SCHED_TIMES (from), EXPR_ORIG_BB_INDEX (from),
  1399. EXPR_SPEC_DONE_DS (from), EXPR_SPEC_TO_CHECK_DS (from),
  1400. EXPR_ORIG_SCHED_CYCLE (from), temp,
  1401. EXPR_TARGET_AVAILABLE (from), EXPR_WAS_SUBSTITUTED (from),
  1402. EXPR_WAS_RENAMED (from), EXPR_NEEDS_SPEC_CHECK_P (from),
  1403. EXPR_CANT_MOVE (from));
  1404. }
  1405. /* Same, but the final expr will not ever be in av sets, so don't copy
  1406. "uninteresting" data such as bitmap cache. */
  1407. void
  1408. copy_expr_onside (expr_t to, expr_t from)
  1409. {
  1410. init_expr (to, EXPR_VINSN (from), EXPR_SPEC (from), EXPR_USEFULNESS (from),
  1411. EXPR_PRIORITY (from), EXPR_SCHED_TIMES (from), 0,
  1412. EXPR_SPEC_DONE_DS (from), EXPR_SPEC_TO_CHECK_DS (from), 0,
  1413. vNULL,
  1414. EXPR_TARGET_AVAILABLE (from), EXPR_WAS_SUBSTITUTED (from),
  1415. EXPR_WAS_RENAMED (from), EXPR_NEEDS_SPEC_CHECK_P (from),
  1416. EXPR_CANT_MOVE (from));
  1417. }
  1418. /* Prepare the expr of INSN for scheduling. Used when moving insn and when
  1419. initializing new insns. */
  1420. static void
  1421. prepare_insn_expr (insn_t insn, int seqno)
  1422. {
  1423. expr_t expr = INSN_EXPR (insn);
  1424. ds_t ds;
  1425. INSN_SEQNO (insn) = seqno;
  1426. EXPR_ORIG_BB_INDEX (expr) = BLOCK_NUM (insn);
  1427. EXPR_SPEC (expr) = 0;
  1428. EXPR_ORIG_SCHED_CYCLE (expr) = 0;
  1429. EXPR_WAS_SUBSTITUTED (expr) = 0;
  1430. EXPR_WAS_RENAMED (expr) = 0;
  1431. EXPR_TARGET_AVAILABLE (expr) = 1;
  1432. INSN_LIVE_VALID_P (insn) = false;
  1433. /* ??? If this expression is speculative, make its dependence
  1434. as weak as possible. We can filter this expression later
  1435. in process_spec_exprs, because we do not distinguish
  1436. between the status we got during compute_av_set and the
  1437. existing status. To be fixed. */
  1438. ds = EXPR_SPEC_DONE_DS (expr);
  1439. if (ds)
  1440. EXPR_SPEC_DONE_DS (expr) = ds_get_max_dep_weak (ds);
  1441. free_history_vect (EXPR_HISTORY_OF_CHANGES (expr));
  1442. }
  1443. /* Update target_available bits when merging exprs TO and FROM. SPLIT_POINT
  1444. is non-null when expressions are merged from different successors at
  1445. a split point. */
  1446. static void
  1447. update_target_availability (expr_t to, expr_t from, insn_t split_point)
  1448. {
  1449. if (EXPR_TARGET_AVAILABLE (to) < 0
  1450. || EXPR_TARGET_AVAILABLE (from) < 0)
  1451. EXPR_TARGET_AVAILABLE (to) = -1;
  1452. else
  1453. {
  1454. /* We try to detect the case when one of the expressions
  1455. can only be reached through another one. In this case,
  1456. we can do better. */
  1457. if (split_point == NULL)
  1458. {
  1459. int toind, fromind;
  1460. toind = EXPR_ORIG_BB_INDEX (to);
  1461. fromind = EXPR_ORIG_BB_INDEX (from);
  1462. if (toind && toind == fromind)
  1463. /* Do nothing -- everything is done in
  1464. merge_with_other_exprs. */
  1465. ;
  1466. else
  1467. EXPR_TARGET_AVAILABLE (to) = -1;
  1468. }
  1469. else if (EXPR_TARGET_AVAILABLE (from) == 0
  1470. && EXPR_LHS (from)
  1471. && REG_P (EXPR_LHS (from))
  1472. && REGNO (EXPR_LHS (to)) != REGNO (EXPR_LHS (from)))
  1473. EXPR_TARGET_AVAILABLE (to) = -1;
  1474. else
  1475. EXPR_TARGET_AVAILABLE (to) &= EXPR_TARGET_AVAILABLE (from);
  1476. }
  1477. }
  1478. /* Update speculation bits when merging exprs TO and FROM. SPLIT_POINT
  1479. is non-null when expressions are merged from different successors at
  1480. a split point. */
  1481. static void
  1482. update_speculative_bits (expr_t to, expr_t from, insn_t split_point)
  1483. {
  1484. ds_t old_to_ds, old_from_ds;
  1485. old_to_ds = EXPR_SPEC_DONE_DS (to);
  1486. old_from_ds = EXPR_SPEC_DONE_DS (from);
  1487. EXPR_SPEC_DONE_DS (to) = ds_max_merge (old_to_ds, old_from_ds);
  1488. EXPR_SPEC_TO_CHECK_DS (to) |= EXPR_SPEC_TO_CHECK_DS (from);
  1489. EXPR_NEEDS_SPEC_CHECK_P (to) |= EXPR_NEEDS_SPEC_CHECK_P (from);
  1490. /* When merging e.g. control & data speculative exprs, or a control
  1491. speculative with a control&data speculative one, we really have
  1492. to change vinsn too. Also, when speculative status is changed,
  1493. we also need to record this as a transformation in expr's history. */
  1494. if ((old_to_ds & SPECULATIVE) || (old_from_ds & SPECULATIVE))
  1495. {
  1496. old_to_ds = ds_get_speculation_types (old_to_ds);
  1497. old_from_ds = ds_get_speculation_types (old_from_ds);
  1498. if (old_to_ds != old_from_ds)
  1499. {
  1500. ds_t record_ds;
  1501. /* When both expressions are speculative, we need to change
  1502. the vinsn first. */
  1503. if ((old_to_ds & SPECULATIVE) && (old_from_ds & SPECULATIVE))
  1504. {
  1505. int res;
  1506. res = speculate_expr (to, EXPR_SPEC_DONE_DS (to));
  1507. gcc_assert (res >= 0);
  1508. }
  1509. if (split_point != NULL)
  1510. {
  1511. /* Record the change with proper status. */
  1512. record_ds = EXPR_SPEC_DONE_DS (to) & SPECULATIVE;
  1513. record_ds &= ~(old_to_ds & SPECULATIVE);
  1514. record_ds &= ~(old_from_ds & SPECULATIVE);
  1515. insert_in_history_vect (&EXPR_HISTORY_OF_CHANGES (to),
  1516. INSN_UID (split_point), TRANS_SPECULATION,
  1517. EXPR_VINSN (from), EXPR_VINSN (to),
  1518. record_ds);
  1519. }
  1520. }
  1521. }
  1522. }
  1523. /* Merge bits of FROM expr to TO expr. When SPLIT_POINT is not NULL,
  1524. this is done along different paths. */
  1525. void
  1526. merge_expr_data (expr_t to, expr_t from, insn_t split_point)
  1527. {
  1528. /* Choose the maximum of the specs of merged exprs. This is required
  1529. for correctness of bookkeeping. */
  1530. if (EXPR_SPEC (to) < EXPR_SPEC (from))
  1531. EXPR_SPEC (to) = EXPR_SPEC (from);
  1532. if (split_point)
  1533. EXPR_USEFULNESS (to) += EXPR_USEFULNESS (from);
  1534. else
  1535. EXPR_USEFULNESS (to) = MAX (EXPR_USEFULNESS (to),
  1536. EXPR_USEFULNESS (from));
  1537. if (EXPR_PRIORITY (to) < EXPR_PRIORITY (from))
  1538. EXPR_PRIORITY (to) = EXPR_PRIORITY (from);
  1539. if (EXPR_SCHED_TIMES (to) > EXPR_SCHED_TIMES (from))
  1540. EXPR_SCHED_TIMES (to) = EXPR_SCHED_TIMES (from);
  1541. if (EXPR_ORIG_BB_INDEX (to) != EXPR_ORIG_BB_INDEX (from))
  1542. EXPR_ORIG_BB_INDEX (to) = 0;
  1543. EXPR_ORIG_SCHED_CYCLE (to) = MIN (EXPR_ORIG_SCHED_CYCLE (to),
  1544. EXPR_ORIG_SCHED_CYCLE (from));
  1545. EXPR_WAS_SUBSTITUTED (to) |= EXPR_WAS_SUBSTITUTED (from);
  1546. EXPR_WAS_RENAMED (to) |= EXPR_WAS_RENAMED (from);
  1547. EXPR_CANT_MOVE (to) |= EXPR_CANT_MOVE (from);
  1548. merge_history_vect (&EXPR_HISTORY_OF_CHANGES (to),
  1549. EXPR_HISTORY_OF_CHANGES (from));
  1550. update_target_availability (to, from, split_point);
  1551. update_speculative_bits (to, from, split_point);
  1552. }
  1553. /* Merge bits of FROM expr to TO expr. Vinsns in the exprs should be equal
  1554. in terms of vinsn_equal_p. SPLIT_POINT is non-null when expressions
  1555. are merged from different successors at a split point. */
  1556. void
  1557. merge_expr (expr_t to, expr_t from, insn_t split_point)
  1558. {
  1559. vinsn_t to_vi = EXPR_VINSN (to);
  1560. vinsn_t from_vi = EXPR_VINSN (from);
  1561. gcc_assert (vinsn_equal_p (to_vi, from_vi));
  1562. /* Make sure that speculative pattern is propagated into exprs that
  1563. have non-speculative one. This will provide us with consistent
  1564. speculative bits and speculative patterns inside expr. */
  1565. if ((EXPR_SPEC_DONE_DS (from) != 0
  1566. && EXPR_SPEC_DONE_DS (to) == 0)
  1567. /* Do likewise for volatile insns, so that we always retain
  1568. the may_trap_p bit on the resulting expression. */
  1569. || (VINSN_MAY_TRAP_P (EXPR_VINSN (from))
  1570. && !VINSN_MAY_TRAP_P (EXPR_VINSN (to))))
  1571. change_vinsn_in_expr (to, EXPR_VINSN (from));
  1572. merge_expr_data (to, from, split_point);
  1573. gcc_assert (EXPR_USEFULNESS (to) <= REG_BR_PROB_BASE);
  1574. }
  1575. /* Clear the information of this EXPR. */
  1576. void
  1577. clear_expr (expr_t expr)
  1578. {
  1579. vinsn_detach (EXPR_VINSN (expr));
  1580. EXPR_VINSN (expr) = NULL;
  1581. free_history_vect (EXPR_HISTORY_OF_CHANGES (expr));
  1582. }
  1583. /* For a given LV_SET, mark EXPR having unavailable target register. */
  1584. static void
  1585. set_unavailable_target_for_expr (expr_t expr, regset lv_set)
  1586. {
  1587. if (EXPR_SEPARABLE_P (expr))
  1588. {
  1589. if (REG_P (EXPR_LHS (expr))
  1590. && register_unavailable_p (lv_set, EXPR_LHS (expr)))
  1591. {
  1592. /* If it's an insn like r1 = use (r1, ...), and it exists in
  1593. different forms in each of the av_sets being merged, we can't say
  1594. whether original destination register is available or not.
  1595. However, this still works if destination register is not used
  1596. in the original expression: if the branch at which LV_SET we're
  1597. looking here is not actually 'other branch' in sense that same
  1598. expression is available through it (but it can't be determined
  1599. at computation stage because of transformations on one of the
  1600. branches), it still won't affect the availability.
  1601. Liveness of a register somewhere on a code motion path means
  1602. it's either read somewhere on a codemotion path, live on
  1603. 'other' branch, live at the point immediately following
  1604. the original operation, or is read by the original operation.
  1605. The latter case is filtered out in the condition below.
  1606. It still doesn't cover the case when register is defined and used
  1607. somewhere within the code motion path, and in this case we could
  1608. miss a unifying code motion along both branches using a renamed
  1609. register, but it won't affect a code correctness since upon
  1610. an actual code motion a bookkeeping code would be generated. */
  1611. if (register_unavailable_p (VINSN_REG_USES (EXPR_VINSN (expr)),
  1612. EXPR_LHS (expr)))
  1613. EXPR_TARGET_AVAILABLE (expr) = -1;
  1614. else
  1615. EXPR_TARGET_AVAILABLE (expr) = false;
  1616. }
  1617. }
  1618. else
  1619. {
  1620. unsigned regno;
  1621. reg_set_iterator rsi;
  1622. EXECUTE_IF_SET_IN_REG_SET (VINSN_REG_SETS (EXPR_VINSN (expr)),
  1623. 0, regno, rsi)
  1624. if (bitmap_bit_p (lv_set, regno))
  1625. {
  1626. EXPR_TARGET_AVAILABLE (expr) = false;
  1627. break;
  1628. }
  1629. EXECUTE_IF_SET_IN_REG_SET (VINSN_REG_CLOBBERS (EXPR_VINSN (expr)),
  1630. 0, regno, rsi)
  1631. if (bitmap_bit_p (lv_set, regno))
  1632. {
  1633. EXPR_TARGET_AVAILABLE (expr) = false;
  1634. break;
  1635. }
  1636. }
  1637. }
  1638. /* Try to make EXPR speculative. Return 1 when EXPR's pattern
  1639. or dependence status have changed, 2 when also the target register
  1640. became unavailable, 0 if nothing had to be changed. */
  1641. int
  1642. speculate_expr (expr_t expr, ds_t ds)
  1643. {
  1644. int res;
  1645. rtx_insn *orig_insn_rtx;
  1646. rtx spec_pat;
  1647. ds_t target_ds, current_ds;
  1648. /* Obtain the status we need to put on EXPR. */
  1649. target_ds = (ds & SPECULATIVE);
  1650. current_ds = EXPR_SPEC_DONE_DS (expr);
  1651. ds = ds_full_merge (current_ds, target_ds, NULL_RTX, NULL_RTX);
  1652. orig_insn_rtx = EXPR_INSN_RTX (expr);
  1653. res = sched_speculate_insn (orig_insn_rtx, ds, &spec_pat);
  1654. switch (res)
  1655. {
  1656. case 0:
  1657. EXPR_SPEC_DONE_DS (expr) = ds;
  1658. return current_ds != ds ? 1 : 0;
  1659. case 1:
  1660. {
  1661. rtx_insn *spec_insn_rtx =
  1662. create_insn_rtx_from_pattern (spec_pat, NULL_RTX);
  1663. vinsn_t spec_vinsn = create_vinsn_from_insn_rtx (spec_insn_rtx, false);
  1664. change_vinsn_in_expr (expr, spec_vinsn);
  1665. EXPR_SPEC_DONE_DS (expr) = ds;
  1666. EXPR_NEEDS_SPEC_CHECK_P (expr) = true;
  1667. /* Do not allow clobbering the address register of speculative
  1668. insns. */
  1669. if (register_unavailable_p (VINSN_REG_USES (EXPR_VINSN (expr)),
  1670. expr_dest_reg (expr)))
  1671. {
  1672. EXPR_TARGET_AVAILABLE (expr) = false;
  1673. return 2;
  1674. }
  1675. return 1;
  1676. }
  1677. case -1:
  1678. return -1;
  1679. default:
  1680. gcc_unreachable ();
  1681. return -1;
  1682. }
  1683. }
  1684. /* Return a destination register, if any, of EXPR. */
  1685. rtx
  1686. expr_dest_reg (expr_t expr)
  1687. {
  1688. rtx dest = VINSN_LHS (EXPR_VINSN (expr));
  1689. if (dest != NULL_RTX && REG_P (dest))
  1690. return dest;
  1691. return NULL_RTX;
  1692. }
  1693. /* Returns the REGNO of the R's destination. */
  1694. unsigned
  1695. expr_dest_regno (expr_t expr)
  1696. {
  1697. rtx dest = expr_dest_reg (expr);
  1698. gcc_assert (dest != NULL_RTX);
  1699. return REGNO (dest);
  1700. }
  1701. /* For a given LV_SET, mark all expressions in JOIN_SET, but not present in
  1702. AV_SET having unavailable target register. */
  1703. void
  1704. mark_unavailable_targets (av_set_t join_set, av_set_t av_set, regset lv_set)
  1705. {
  1706. expr_t expr;
  1707. av_set_iterator avi;
  1708. FOR_EACH_EXPR (expr, avi, join_set)
  1709. if (av_set_lookup (av_set, EXPR_VINSN (expr)) == NULL)
  1710. set_unavailable_target_for_expr (expr, lv_set);
  1711. }
  1712. /* Returns true if REG (at least partially) is present in REGS. */
  1713. bool
  1714. register_unavailable_p (regset regs, rtx reg)
  1715. {
  1716. unsigned regno, end_regno;
  1717. regno = REGNO (reg);
  1718. if (bitmap_bit_p (regs, regno))
  1719. return true;
  1720. end_regno = END_REGNO (reg);
  1721. while (++regno < end_regno)
  1722. if (bitmap_bit_p (regs, regno))
  1723. return true;
  1724. return false;
  1725. }
  1726. /* Av set functions. */
  1727. /* Add a new element to av set SETP.
  1728. Return the element added. */
  1729. static av_set_t
  1730. av_set_add_element (av_set_t *setp)
  1731. {
  1732. /* Insert at the beginning of the list. */
  1733. _list_add (setp);
  1734. return *setp;
  1735. }
  1736. /* Add EXPR to SETP. */
  1737. void
  1738. av_set_add (av_set_t *setp, expr_t expr)
  1739. {
  1740. av_set_t elem;
  1741. gcc_assert (!INSN_NOP_P (EXPR_INSN_RTX (expr)));
  1742. elem = av_set_add_element (setp);
  1743. copy_expr (_AV_SET_EXPR (elem), expr);
  1744. }
  1745. /* Same, but do not copy EXPR. */
  1746. static void
  1747. av_set_add_nocopy (av_set_t *setp, expr_t expr)
  1748. {
  1749. av_set_t elem;
  1750. elem = av_set_add_element (setp);
  1751. *_AV_SET_EXPR (elem) = *expr;
  1752. }
  1753. /* Remove expr pointed to by IP from the av_set. */
  1754. void
  1755. av_set_iter_remove (av_set_iterator *ip)
  1756. {
  1757. clear_expr (_AV_SET_EXPR (*ip->lp));
  1758. _list_iter_remove (ip);
  1759. }
  1760. /* Search for an expr in SET, such that it's equivalent to SOUGHT_VINSN in the
  1761. sense of vinsn_equal_p function. Return NULL if no such expr is
  1762. in SET was found. */
  1763. expr_t
  1764. av_set_lookup (av_set_t set, vinsn_t sought_vinsn)
  1765. {
  1766. expr_t expr;
  1767. av_set_iterator i;
  1768. FOR_EACH_EXPR (expr, i, set)
  1769. if (vinsn_equal_p (EXPR_VINSN (expr), sought_vinsn))
  1770. return expr;
  1771. return NULL;
  1772. }
  1773. /* Same, but also remove the EXPR found. */
  1774. static expr_t
  1775. av_set_lookup_and_remove (av_set_t *setp, vinsn_t sought_vinsn)
  1776. {
  1777. expr_t expr;
  1778. av_set_iterator i;
  1779. FOR_EACH_EXPR_1 (expr, i, setp)
  1780. if (vinsn_equal_p (EXPR_VINSN (expr), sought_vinsn))
  1781. {
  1782. _list_iter_remove_nofree (&i);
  1783. return expr;
  1784. }
  1785. return NULL;
  1786. }
  1787. /* Search for an expr in SET, such that it's equivalent to EXPR in the
  1788. sense of vinsn_equal_p function of their vinsns, but not EXPR itself.
  1789. Returns NULL if no such expr is in SET was found. */
  1790. static expr_t
  1791. av_set_lookup_other_equiv_expr (av_set_t set, expr_t expr)
  1792. {
  1793. expr_t cur_expr;
  1794. av_set_iterator i;
  1795. FOR_EACH_EXPR (cur_expr, i, set)
  1796. {
  1797. if (cur_expr == expr)
  1798. continue;
  1799. if (vinsn_equal_p (EXPR_VINSN (cur_expr), EXPR_VINSN (expr)))
  1800. return cur_expr;
  1801. }
  1802. return NULL;
  1803. }
  1804. /* If other expression is already in AVP, remove one of them. */
  1805. expr_t
  1806. merge_with_other_exprs (av_set_t *avp, av_set_iterator *ip, expr_t expr)
  1807. {
  1808. expr_t expr2;
  1809. expr2 = av_set_lookup_other_equiv_expr (*avp, expr);
  1810. if (expr2 != NULL)
  1811. {
  1812. /* Reset target availability on merge, since taking it only from one
  1813. of the exprs would be controversial for different code. */
  1814. EXPR_TARGET_AVAILABLE (expr2) = -1;
  1815. EXPR_USEFULNESS (expr2) = 0;
  1816. merge_expr (expr2, expr, NULL);
  1817. /* Fix usefulness as it should be now REG_BR_PROB_BASE. */
  1818. EXPR_USEFULNESS (expr2) = REG_BR_PROB_BASE;
  1819. av_set_iter_remove (ip);
  1820. return expr2;
  1821. }
  1822. return expr;
  1823. }
  1824. /* Return true if there is an expr that correlates to VI in SET. */
  1825. bool
  1826. av_set_is_in_p (av_set_t set, vinsn_t vi)
  1827. {
  1828. return av_set_lookup (set, vi) != NULL;
  1829. }
  1830. /* Return a copy of SET. */
  1831. av_set_t
  1832. av_set_copy (av_set_t set)
  1833. {
  1834. expr_t expr;
  1835. av_set_iterator i;
  1836. av_set_t res = NULL;
  1837. FOR_EACH_EXPR (expr, i, set)
  1838. av_set_add (&res, expr);
  1839. return res;
  1840. }
  1841. /* Join two av sets that do not have common elements by attaching second set
  1842. (pointed to by FROMP) to the end of first set (TO_TAILP must point to
  1843. _AV_SET_NEXT of first set's last element). */
  1844. static void
  1845. join_distinct_sets (av_set_t *to_tailp, av_set_t *fromp)
  1846. {
  1847. gcc_assert (*to_tailp == NULL);
  1848. *to_tailp = *fromp;
  1849. *fromp = NULL;
  1850. }
  1851. /* Makes set pointed to by TO to be the union of TO and FROM. Clear av_set
  1852. pointed to by FROMP afterwards. */
  1853. void
  1854. av_set_union_and_clear (av_set_t *top, av_set_t *fromp, insn_t insn)
  1855. {
  1856. expr_t expr1;
  1857. av_set_iterator i;
  1858. /* Delete from TOP all exprs, that present in FROMP. */
  1859. FOR_EACH_EXPR_1 (expr1, i, top)
  1860. {
  1861. expr_t expr2 = av_set_lookup (*fromp, EXPR_VINSN (expr1));
  1862. if (expr2)
  1863. {
  1864. merge_expr (expr2, expr1, insn);
  1865. av_set_iter_remove (&i);
  1866. }
  1867. }
  1868. join_distinct_sets (i.lp, fromp);
  1869. }
  1870. /* Same as above, but also update availability of target register in
  1871. TOP judging by TO_LV_SET and FROM_LV_SET. */
  1872. void
  1873. av_set_union_and_live (av_set_t *top, av_set_t *fromp, regset to_lv_set,
  1874. regset from_lv_set, insn_t insn)
  1875. {
  1876. expr_t expr1;
  1877. av_set_iterator i;
  1878. av_set_t *to_tailp, in_both_set = NULL;
  1879. /* Delete from TOP all expres, that present in FROMP. */
  1880. FOR_EACH_EXPR_1 (expr1, i, top)
  1881. {
  1882. expr_t expr2 = av_set_lookup_and_remove (fromp, EXPR_VINSN (expr1));
  1883. if (expr2)
  1884. {
  1885. /* It may be that the expressions have different destination
  1886. registers, in which case we need to check liveness here. */
  1887. if (EXPR_SEPARABLE_P (expr1))
  1888. {
  1889. int regno1 = (REG_P (EXPR_LHS (expr1))
  1890. ? (int) expr_dest_regno (expr1) : -1);
  1891. int regno2 = (REG_P (EXPR_LHS (expr2))
  1892. ? (int) expr_dest_regno (expr2) : -1);
  1893. /* ??? We don't have a way to check restrictions for
  1894. *other* register on the current path, we did it only
  1895. for the current target register. Give up. */
  1896. if (regno1 != regno2)
  1897. EXPR_TARGET_AVAILABLE (expr2) = -1;
  1898. }
  1899. else if (EXPR_INSN_RTX (expr1) != EXPR_INSN_RTX (expr2))
  1900. EXPR_TARGET_AVAILABLE (expr2) = -1;
  1901. merge_expr (expr2, expr1, insn);
  1902. av_set_add_nocopy (&in_both_set, expr2);
  1903. av_set_iter_remove (&i);
  1904. }
  1905. else
  1906. /* EXPR1 is present in TOP, but not in FROMP. Check it on
  1907. FROM_LV_SET. */
  1908. set_unavailable_target_for_expr (expr1, from_lv_set);
  1909. }
  1910. to_tailp = i.lp;
  1911. /* These expressions are not present in TOP. Check liveness
  1912. restrictions on TO_LV_SET. */
  1913. FOR_EACH_EXPR (expr1, i, *fromp)
  1914. set_unavailable_target_for_expr (expr1, to_lv_set);
  1915. join_distinct_sets (i.lp, &in_both_set);
  1916. join_distinct_sets (to_tailp, fromp);
  1917. }
  1918. /* Clear av_set pointed to by SETP. */
  1919. void
  1920. av_set_clear (av_set_t *setp)
  1921. {
  1922. expr_t expr;
  1923. av_set_iterator i;
  1924. FOR_EACH_EXPR_1 (expr, i, setp)
  1925. av_set_iter_remove (&i);
  1926. gcc_assert (*setp == NULL);
  1927. }
  1928. /* Leave only one non-speculative element in the SETP. */
  1929. void
  1930. av_set_leave_one_nonspec (av_set_t *setp)
  1931. {
  1932. expr_t expr;
  1933. av_set_iterator i;
  1934. bool has_one_nonspec = false;
  1935. /* Keep all speculative exprs, and leave one non-speculative
  1936. (the first one). */
  1937. FOR_EACH_EXPR_1 (expr, i, setp)
  1938. {
  1939. if (!EXPR_SPEC_DONE_DS (expr))
  1940. {
  1941. if (has_one_nonspec)
  1942. av_set_iter_remove (&i);
  1943. else
  1944. has_one_nonspec = true;
  1945. }
  1946. }
  1947. }
  1948. /* Return the N'th element of the SET. */
  1949. expr_t
  1950. av_set_element (av_set_t set, int n)
  1951. {
  1952. expr_t expr;
  1953. av_set_iterator i;
  1954. FOR_EACH_EXPR (expr, i, set)
  1955. if (n-- == 0)
  1956. return expr;
  1957. gcc_unreachable ();
  1958. return NULL;
  1959. }
  1960. /* Deletes all expressions from AVP that are conditional branches (IFs). */
  1961. void
  1962. av_set_substract_cond_branches (av_set_t *avp)
  1963. {
  1964. av_set_iterator i;
  1965. expr_t expr;
  1966. FOR_EACH_EXPR_1 (expr, i, avp)
  1967. if (vinsn_cond_branch_p (EXPR_VINSN (expr)))
  1968. av_set_iter_remove (&i);
  1969. }
  1970. /* Multiplies usefulness attribute of each member of av-set *AVP by
  1971. value PROB / ALL_PROB. */
  1972. void
  1973. av_set_split_usefulness (av_set_t av, int prob, int all_prob)
  1974. {
  1975. av_set_iterator i;
  1976. expr_t expr;
  1977. FOR_EACH_EXPR (expr, i, av)
  1978. EXPR_USEFULNESS (expr) = (all_prob
  1979. ? (EXPR_USEFULNESS (expr) * prob) / all_prob
  1980. : 0);
  1981. }
  1982. /* Leave in AVP only those expressions, which are present in AV,
  1983. and return it, merging history expressions. */
  1984. void
  1985. av_set_code_motion_filter (av_set_t *avp, av_set_t av)
  1986. {
  1987. av_set_iterator i;
  1988. expr_t expr, expr2;
  1989. FOR_EACH_EXPR_1 (expr, i, avp)
  1990. if ((expr2 = av_set_lookup (av, EXPR_VINSN (expr))) == NULL)
  1991. av_set_iter_remove (&i);
  1992. else
  1993. /* When updating av sets in bookkeeping blocks, we can add more insns
  1994. there which will be transformed but the upper av sets will not
  1995. reflect those transformations. We then fail to undo those
  1996. when searching for such insns. So merge the history saved
  1997. in the av set of the block we are processing. */
  1998. merge_history_vect (&EXPR_HISTORY_OF_CHANGES (expr),
  1999. EXPR_HISTORY_OF_CHANGES (expr2));
  2000. }
  2001. /* Dependence hooks to initialize insn data. */
  2002. /* This is used in hooks callable from dependence analysis when initializing
  2003. instruction's data. */
  2004. static struct
  2005. {
  2006. /* Where the dependence was found (lhs/rhs). */
  2007. deps_where_t where;
  2008. /* The actual data object to initialize. */
  2009. idata_t id;
  2010. /* True when the insn should not be made clonable. */
  2011. bool force_unique_p;
  2012. /* True when insn should be treated as of type USE, i.e. never renamed. */
  2013. bool force_use_p;
  2014. } deps_init_id_data;
  2015. /* Setup ID for INSN. FORCE_UNIQUE_P is true when INSN should not be
  2016. clonable. */
  2017. static void
  2018. setup_id_for_insn (idata_t id, insn_t insn, bool force_unique_p)
  2019. {
  2020. int type;
  2021. /* Determine whether INSN could be cloned and return appropriate vinsn type.
  2022. That clonable insns which can be separated into lhs and rhs have type SET.
  2023. Other clonable insns have type USE. */
  2024. type = GET_CODE (insn);
  2025. /* Only regular insns could be cloned. */
  2026. if (type == INSN && !force_unique_p)
  2027. type = SET;
  2028. else if (type == JUMP_INSN && simplejump_p (insn))
  2029. type = PC;
  2030. else if (type == DEBUG_INSN)
  2031. type = !force_unique_p ? USE : INSN;
  2032. IDATA_TYPE (id) = type;
  2033. IDATA_REG_SETS (id) = get_clear_regset_from_pool ();
  2034. IDATA_REG_USES (id) = get_clear_regset_from_pool ();
  2035. IDATA_REG_CLOBBERS (id) = get_clear_regset_from_pool ();
  2036. }
  2037. /* Start initializing insn data. */
  2038. static void
  2039. deps_init_id_start_insn (insn_t insn)
  2040. {
  2041. gcc_assert (deps_init_id_data.where == DEPS_IN_NOWHERE);
  2042. setup_id_for_insn (deps_init_id_data.id, insn,
  2043. deps_init_id_data.force_unique_p);
  2044. deps_init_id_data.where = DEPS_IN_INSN;
  2045. }
  2046. /* Start initializing lhs data. */
  2047. static void
  2048. deps_init_id_start_lhs (rtx lhs)
  2049. {
  2050. gcc_assert (deps_init_id_data.where == DEPS_IN_INSN);
  2051. gcc_assert (IDATA_LHS (deps_init_id_data.id) == NULL);
  2052. if (IDATA_TYPE (deps_init_id_data.id) == SET)
  2053. {
  2054. IDATA_LHS (deps_init_id_data.id) = lhs;
  2055. deps_init_id_data.where = DEPS_IN_LHS;
  2056. }
  2057. }
  2058. /* Finish initializing lhs data. */
  2059. static void
  2060. deps_init_id_finish_lhs (void)
  2061. {
  2062. deps_init_id_data.where = DEPS_IN_INSN;
  2063. }
  2064. /* Note a set of REGNO. */
  2065. static void
  2066. deps_init_id_note_reg_set (int regno)
  2067. {
  2068. haifa_note_reg_set (regno);
  2069. if (deps_init_id_data.where == DEPS_IN_RHS)
  2070. deps_init_id_data.force_use_p = true;
  2071. if (IDATA_TYPE (deps_init_id_data.id) != PC)
  2072. SET_REGNO_REG_SET (IDATA_REG_SETS (deps_init_id_data.id), regno);
  2073. #ifdef STACK_REGS
  2074. /* Make instructions that set stack registers to be ineligible for
  2075. renaming to avoid issues with find_used_regs. */
  2076. if (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG))
  2077. deps_init_id_data.force_use_p = true;
  2078. #endif
  2079. }
  2080. /* Note a clobber of REGNO. */
  2081. static void
  2082. deps_init_id_note_reg_clobber (int regno)
  2083. {
  2084. haifa_note_reg_clobber (regno);
  2085. if (deps_init_id_data.where == DEPS_IN_RHS)
  2086. deps_init_id_data.force_use_p = true;
  2087. if (IDATA_TYPE (deps_init_id_data.id) != PC)
  2088. SET_REGNO_REG_SET (IDATA_REG_CLOBBERS (deps_init_id_data.id), regno);
  2089. }
  2090. /* Note a use of REGNO. */
  2091. static void
  2092. deps_init_id_note_reg_use (int regno)
  2093. {
  2094. haifa_note_reg_use (regno);
  2095. if (IDATA_TYPE (deps_init_id_data.id) != PC)
  2096. SET_REGNO_REG_SET (IDATA_REG_USES (deps_init_id_data.id), regno);
  2097. }
  2098. /* Start initializing rhs data. */
  2099. static void
  2100. deps_init_id_start_rhs (rtx rhs)
  2101. {
  2102. gcc_assert (deps_init_id_data.where == DEPS_IN_INSN);
  2103. /* And there was no sel_deps_reset_to_insn (). */
  2104. if (IDATA_LHS (deps_init_id_data.id) != NULL)
  2105. {
  2106. IDATA_RHS (deps_init_id_data.id) = rhs;
  2107. deps_init_id_data.where = DEPS_IN_RHS;
  2108. }
  2109. }
  2110. /* Finish initializing rhs data. */
  2111. static void
  2112. deps_init_id_finish_rhs (void)
  2113. {
  2114. gcc_assert (deps_init_id_data.where == DEPS_IN_RHS
  2115. || deps_init_id_data.where == DEPS_IN_INSN);
  2116. deps_init_id_data.where = DEPS_IN_INSN;
  2117. }
  2118. /* Finish initializing insn data. */
  2119. static void
  2120. deps_init_id_finish_insn (void)
  2121. {
  2122. gcc_assert (deps_init_id_data.where == DEPS_IN_INSN);
  2123. if (IDATA_TYPE (deps_init_id_data.id) == SET)
  2124. {
  2125. rtx lhs = IDATA_LHS (deps_init_id_data.id);
  2126. rtx rhs = IDATA_RHS (deps_init_id_data.id);
  2127. if (lhs == NULL || rhs == NULL || !lhs_and_rhs_separable_p (lhs, rhs)
  2128. || deps_init_id_data.force_use_p)
  2129. {
  2130. /* This should be a USE, as we don't want to schedule its RHS
  2131. separately. However, we still want to have them recorded
  2132. for the purposes of substitution. That's why we don't
  2133. simply call downgrade_to_use () here. */
  2134. gcc_assert (IDATA_TYPE (deps_init_id_data.id) == SET);
  2135. gcc_assert (!lhs == !rhs);
  2136. IDATA_TYPE (deps_init_id_data.id) = USE;
  2137. }
  2138. }
  2139. deps_init_id_data.where = DEPS_IN_NOWHERE;
  2140. }
  2141. /* This is dependence info used for initializing insn's data. */
  2142. static struct sched_deps_info_def deps_init_id_sched_deps_info;
  2143. /* This initializes most of the static part of the above structure. */
  2144. static const struct sched_deps_info_def const_deps_init_id_sched_deps_info =
  2145. {
  2146. NULL,
  2147. deps_init_id_start_insn,
  2148. deps_init_id_finish_insn,
  2149. deps_init_id_start_lhs,
  2150. deps_init_id_finish_lhs,
  2151. deps_init_id_start_rhs,
  2152. deps_init_id_finish_rhs,
  2153. deps_init_id_note_reg_set,
  2154. deps_init_id_note_reg_clobber,
  2155. deps_init_id_note_reg_use,
  2156. NULL, /* note_mem_dep */
  2157. NULL, /* note_dep */
  2158. 0, /* use_cselib */
  2159. 0, /* use_deps_list */
  2160. 0 /* generate_spec_deps */
  2161. };
  2162. /* Initialize INSN's lhs and rhs in ID. When FORCE_UNIQUE_P is true,
  2163. we don't actually need information about lhs and rhs. */
  2164. static void
  2165. setup_id_lhs_rhs (idata_t id, insn_t insn, bool force_unique_p)
  2166. {
  2167. rtx pat = PATTERN (insn);
  2168. if (NONJUMP_INSN_P (insn)
  2169. && GET_CODE (pat) == SET
  2170. && !force_unique_p)
  2171. {
  2172. IDATA_RHS (id) = SET_SRC (pat);
  2173. IDATA_LHS (id) = SET_DEST (pat);
  2174. }
  2175. else
  2176. IDATA_LHS (id) = IDATA_RHS (id) = NULL;
  2177. }
  2178. /* Possibly downgrade INSN to USE. */
  2179. static void
  2180. maybe_downgrade_id_to_use (idata_t id, insn_t insn)
  2181. {
  2182. bool must_be_use = false;
  2183. df_ref def;
  2184. rtx lhs = IDATA_LHS (id);
  2185. rtx rhs = IDATA_RHS (id);
  2186. /* We downgrade only SETs. */
  2187. if (IDATA_TYPE (id) != SET)
  2188. return;
  2189. if (!lhs || !lhs_and_rhs_separable_p (lhs, rhs))
  2190. {
  2191. IDATA_TYPE (id) = USE;
  2192. return;
  2193. }
  2194. FOR_EACH_INSN_DEF (def, insn)
  2195. {
  2196. if (DF_REF_INSN (def)
  2197. && DF_REF_FLAGS_IS_SET (def, DF_REF_PRE_POST_MODIFY)
  2198. && loc_mentioned_in_p (DF_REF_LOC (def), IDATA_RHS (id)))
  2199. {
  2200. must_be_use = true;
  2201. break;
  2202. }
  2203. #ifdef STACK_REGS
  2204. /* Make instructions that set stack registers to be ineligible for
  2205. renaming to avoid issues with find_used_regs. */
  2206. if (IN_RANGE (DF_REF_REGNO (def), FIRST_STACK_REG, LAST_STACK_REG))
  2207. {
  2208. must_be_use = true;
  2209. break;
  2210. }
  2211. #endif
  2212. }
  2213. if (must_be_use)
  2214. IDATA_TYPE (id) = USE;
  2215. }
  2216. /* Setup register sets describing INSN in ID. */
  2217. static void
  2218. setup_id_reg_sets (idata_t id, insn_t insn)
  2219. {
  2220. struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
  2221. df_ref def, use;
  2222. regset tmp = get_clear_regset_from_pool ();
  2223. FOR_EACH_INSN_INFO_DEF (def, insn_info)
  2224. {
  2225. unsigned int regno = DF_REF_REGNO (def);
  2226. /* Post modifies are treated like clobbers by sched-deps.c. */
  2227. if (DF_REF_FLAGS_IS_SET (def, (DF_REF_MUST_CLOBBER
  2228. | DF_REF_PRE_POST_MODIFY)))
  2229. SET_REGNO_REG_SET (IDATA_REG_CLOBBERS (id), regno);
  2230. else if (! DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
  2231. {
  2232. SET_REGNO_REG_SET (IDATA_REG_SETS (id), regno);
  2233. #ifdef STACK_REGS
  2234. /* For stack registers, treat writes to them as writes
  2235. to the first one to be consistent with sched-deps.c. */
  2236. if (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG))
  2237. SET_REGNO_REG_SET (IDATA_REG_SETS (id), FIRST_STACK_REG);
  2238. #endif
  2239. }
  2240. /* Mark special refs that generate read/write def pair. */
  2241. if (DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)
  2242. || regno == STACK_POINTER_REGNUM)
  2243. bitmap_set_bit (tmp, regno);
  2244. }
  2245. FOR_EACH_INSN_INFO_USE (use, insn_info)
  2246. {
  2247. unsigned int regno = DF_REF_REGNO (use);
  2248. /* When these refs are met for the first time, skip them, as
  2249. these uses are just counterparts of some defs. */
  2250. if (bitmap_bit_p (tmp, regno))
  2251. bitmap_clear_bit (tmp, regno);
  2252. else if (! DF_REF_FLAGS_IS_SET (use, DF_REF_CALL_STACK_USAGE))
  2253. {
  2254. SET_REGNO_REG_SET (IDATA_REG_USES (id), regno);
  2255. #ifdef STACK_REGS
  2256. /* For stack registers, treat reads from them as reads from
  2257. the first one to be consistent with sched-deps.c. */
  2258. if (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG))
  2259. SET_REGNO_REG_SET (IDATA_REG_USES (id), FIRST_STACK_REG);
  2260. #endif
  2261. }
  2262. }
  2263. return_regset_to_pool (tmp);
  2264. }
  2265. /* Initialize instruction data for INSN in ID using DF's data. */
  2266. static void
  2267. init_id_from_df (idata_t id, insn_t insn, bool force_unique_p)
  2268. {
  2269. gcc_assert (DF_INSN_UID_SAFE_GET (INSN_UID (insn)) != NULL);
  2270. setup_id_for_insn (id, insn, force_unique_p);
  2271. setup_id_lhs_rhs (id, insn, force_unique_p);
  2272. if (INSN_NOP_P (insn))
  2273. return;
  2274. maybe_downgrade_id_to_use (id, insn);
  2275. setup_id_reg_sets (id, insn);
  2276. }
  2277. /* Initialize instruction data for INSN in ID. */
  2278. static void
  2279. deps_init_id (idata_t id, insn_t insn, bool force_unique_p)
  2280. {
  2281. struct deps_desc _dc, *dc = &_dc;
  2282. deps_init_id_data.where = DEPS_IN_NOWHERE;
  2283. deps_init_id_data.id = id;
  2284. deps_init_id_data.force_unique_p = force_unique_p;
  2285. deps_init_id_data.force_use_p = false;
  2286. init_deps (dc, false);
  2287. memcpy (&deps_init_id_sched_deps_info,
  2288. &const_deps_init_id_sched_deps_info,
  2289. sizeof (deps_init_id_sched_deps_info));
  2290. if (spec_info != NULL)
  2291. deps_init_id_sched_deps_info.generate_spec_deps = 1;
  2292. sched_deps_info = &deps_init_id_sched_deps_info;
  2293. deps_analyze_insn (dc, insn);
  2294. free_deps (dc);
  2295. deps_init_id_data.id = NULL;
  2296. }
  2297. struct sched_scan_info_def
  2298. {
  2299. /* This hook notifies scheduler frontend to extend its internal per basic
  2300. block data structures. This hook should be called once before a series of
  2301. calls to bb_init (). */
  2302. void (*extend_bb) (void);
  2303. /* This hook makes scheduler frontend to initialize its internal data
  2304. structures for the passed basic block. */
  2305. void (*init_bb) (basic_block);
  2306. /* This hook notifies scheduler frontend to extend its internal per insn data
  2307. structures. This hook should be called once before a series of calls to
  2308. insn_init (). */
  2309. void (*extend_insn) (void);
  2310. /* This hook makes scheduler frontend to initialize its internal data
  2311. structures for the passed insn. */
  2312. void (*init_insn) (insn_t);
  2313. };
  2314. /* A driver function to add a set of basic blocks (BBS) to the
  2315. scheduling region. */
  2316. static void
  2317. sched_scan (const struct sched_scan_info_def *ssi, bb_vec_t bbs)
  2318. {
  2319. unsigned i;
  2320. basic_block bb;
  2321. if (ssi->extend_bb)
  2322. ssi->extend_bb ();
  2323. if (ssi->init_bb)
  2324. FOR_EACH_VEC_ELT (bbs, i, bb)
  2325. ssi->init_bb (bb);
  2326. if (ssi->extend_insn)
  2327. ssi->extend_insn ();
  2328. if (ssi->init_insn)
  2329. FOR_EACH_VEC_ELT (bbs, i, bb)
  2330. {
  2331. rtx_insn *insn;
  2332. FOR_BB_INSNS (bb, insn)
  2333. ssi->init_insn (insn);
  2334. }
  2335. }
  2336. /* Implement hooks for collecting fundamental insn properties like if insn is
  2337. an ASM or is within a SCHED_GROUP. */
  2338. /* True when a "one-time init" data for INSN was already inited. */
  2339. static bool
  2340. first_time_insn_init (insn_t insn)
  2341. {
  2342. return INSN_LIVE (insn) == NULL;
  2343. }
  2344. /* Hash an entry in a transformed_insns hashtable. */
  2345. static hashval_t
  2346. hash_transformed_insns (const void *p)
  2347. {
  2348. return VINSN_HASH_RTX (((const struct transformed_insns *) p)->vinsn_old);
  2349. }
  2350. /* Compare the entries in a transformed_insns hashtable. */
  2351. static int
  2352. eq_transformed_insns (const void *p, const void *q)
  2353. {
  2354. rtx_insn *i1 =
  2355. VINSN_INSN_RTX (((const struct transformed_insns *) p)->vinsn_old);
  2356. rtx_insn *i2 =
  2357. VINSN_INSN_RTX (((const struct transformed_insns *) q)->vinsn_old);
  2358. if (INSN_UID (i1) == INSN_UID (i2))
  2359. return 1;
  2360. return rtx_equal_p (PATTERN (i1), PATTERN (i2));
  2361. }
  2362. /* Free an entry in a transformed_insns hashtable. */
  2363. static void
  2364. free_transformed_insns (void *p)
  2365. {
  2366. struct transformed_insns *pti = (struct transformed_insns *) p;
  2367. vinsn_detach (pti->vinsn_old);
  2368. vinsn_detach (pti->vinsn_new);
  2369. free (pti);
  2370. }
  2371. /* Init the s_i_d data for INSN which should be inited just once, when
  2372. we first see the insn. */
  2373. static void
  2374. init_first_time_insn_data (insn_t insn)
  2375. {
  2376. /* This should not be set if this is the first time we init data for
  2377. insn. */
  2378. gcc_assert (first_time_insn_init (insn));
  2379. /* These are needed for nops too. */
  2380. INSN_LIVE (insn) = get_regset_from_pool ();
  2381. INSN_LIVE_VALID_P (insn) = false;
  2382. if (!INSN_NOP_P (insn))
  2383. {
  2384. INSN_ANALYZED_DEPS (insn) = BITMAP_ALLOC (NULL);
  2385. INSN_FOUND_DEPS (insn) = BITMAP_ALLOC (NULL);
  2386. INSN_TRANSFORMED_INSNS (insn)
  2387. = htab_create (16, hash_transformed_insns,
  2388. eq_transformed_insns, free_transformed_insns);
  2389. init_deps (&INSN_DEPS_CONTEXT (insn), true);
  2390. }
  2391. }
  2392. /* Free almost all above data for INSN that is scheduled already.
  2393. Used for extra-large basic blocks. */
  2394. void
  2395. free_data_for_scheduled_insn (insn_t insn)
  2396. {
  2397. gcc_assert (! first_time_insn_init (insn));
  2398. if (! INSN_ANALYZED_DEPS (insn))
  2399. return;
  2400. BITMAP_FREE (INSN_ANALYZED_DEPS (insn));
  2401. BITMAP_FREE (INSN_FOUND_DEPS (insn));
  2402. htab_delete (INSN_TRANSFORMED_INSNS (insn));
  2403. /* This is allocated only for bookkeeping insns. */
  2404. if (INSN_ORIGINATORS (insn))
  2405. BITMAP_FREE (INSN_ORIGINATORS (insn));
  2406. free_deps (&INSN_DEPS_CONTEXT (insn));
  2407. INSN_ANALYZED_DEPS (insn) = NULL;
  2408. /* Clear the readonly flag so we would ICE when trying to recalculate
  2409. the deps context (as we believe that it should not happen). */
  2410. (&INSN_DEPS_CONTEXT (insn))->readonly = 0;
  2411. }
  2412. /* Free the same data as above for INSN. */
  2413. static void
  2414. free_first_time_insn_data (insn_t insn)
  2415. {
  2416. gcc_assert (! first_time_insn_init (insn));
  2417. free_data_for_scheduled_insn (insn);
  2418. return_regset_to_pool (INSN_LIVE (insn));
  2419. INSN_LIVE (insn) = NULL;
  2420. INSN_LIVE_VALID_P (insn) = false;
  2421. }
  2422. /* Initialize region-scope data structures for basic blocks. */
  2423. static void
  2424. init_global_and_expr_for_bb (basic_block bb)
  2425. {
  2426. if (sel_bb_empty_p (bb))
  2427. return;
  2428. invalidate_av_set (bb);
  2429. }
  2430. /* Data for global dependency analysis (to initialize CANT_MOVE and
  2431. SCHED_GROUP_P). */
  2432. static struct
  2433. {
  2434. /* Previous insn. */
  2435. insn_t prev_insn;
  2436. } init_global_data;
  2437. /* Determine if INSN is in the sched_group, is an asm or should not be
  2438. cloned. After that initialize its expr. */
  2439. static void
  2440. init_global_and_expr_for_insn (insn_t insn)
  2441. {
  2442. if (LABEL_P (insn))
  2443. return;
  2444. if (NOTE_INSN_BASIC_BLOCK_P (insn))
  2445. {
  2446. init_global_data.prev_insn = NULL;
  2447. return;
  2448. }
  2449. gcc_assert (INSN_P (insn));
  2450. if (SCHED_GROUP_P (insn))
  2451. /* Setup a sched_group. */
  2452. {
  2453. insn_t prev_insn = init_global_data.prev_insn;
  2454. if (prev_insn)
  2455. INSN_SCHED_NEXT (prev_insn) = insn;
  2456. init_global_data.prev_insn = insn;
  2457. }
  2458. else
  2459. init_global_data.prev_insn = NULL;
  2460. if (GET_CODE (PATTERN (insn)) == ASM_INPUT
  2461. || asm_noperands (PATTERN (insn)) >= 0)
  2462. /* Mark INSN as an asm. */
  2463. INSN_ASM_P (insn) = true;
  2464. {
  2465. bool force_unique_p;
  2466. ds_t spec_done_ds;
  2467. /* Certain instructions cannot be cloned, and frame related insns and
  2468. the insn adjacent to NOTE_INSN_EPILOGUE_BEG cannot be moved out of
  2469. their block. */
  2470. if (prologue_epilogue_contains (insn))
  2471. {
  2472. if (RTX_FRAME_RELATED_P (insn))
  2473. CANT_MOVE (insn) = 1;
  2474. else
  2475. {
  2476. rtx note;
  2477. for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
  2478. if (REG_NOTE_KIND (note) == REG_SAVE_NOTE
  2479. && ((enum insn_note) INTVAL (XEXP (note, 0))
  2480. == NOTE_INSN_EPILOGUE_BEG))
  2481. {
  2482. CANT_MOVE (insn) = 1;
  2483. break;
  2484. }
  2485. }
  2486. force_unique_p = true;
  2487. }
  2488. else
  2489. if (CANT_MOVE (insn)
  2490. || INSN_ASM_P (insn)
  2491. || SCHED_GROUP_P (insn)
  2492. || CALL_P (insn)
  2493. /* Exception handling insns are always unique. */
  2494. || (cfun->can_throw_non_call_exceptions && can_throw_internal (insn))
  2495. /* TRAP_IF though have an INSN code is control_flow_insn_p (). */
  2496. || control_flow_insn_p (insn)
  2497. || volatile_insn_p (PATTERN (insn))
  2498. || (targetm.cannot_copy_insn_p
  2499. && targetm.cannot_copy_insn_p (insn)))
  2500. force_unique_p = true;
  2501. else
  2502. force_unique_p = false;
  2503. if (targetm.sched.get_insn_spec_ds)
  2504. {
  2505. spec_done_ds = targetm.sched.get_insn_spec_ds (insn);
  2506. spec_done_ds = ds_get_max_dep_weak (spec_done_ds);
  2507. }
  2508. else
  2509. spec_done_ds = 0;
  2510. /* Initialize INSN's expr. */
  2511. init_expr (INSN_EXPR (insn), vinsn_create (insn, force_unique_p), 0,
  2512. REG_BR_PROB_BASE, INSN_PRIORITY (insn), 0, BLOCK_NUM (insn),
  2513. spec_done_ds, 0, 0, vNULL, true,
  2514. false, false, false, CANT_MOVE (insn));
  2515. }
  2516. init_first_time_insn_data (insn);
  2517. }
  2518. /* Scan the region and initialize instruction data for basic blocks BBS. */
  2519. void
  2520. sel_init_global_and_expr (bb_vec_t bbs)
  2521. {
  2522. /* ??? It would be nice to implement push / pop scheme for sched_infos. */
  2523. const struct sched_scan_info_def ssi =
  2524. {
  2525. NULL, /* extend_bb */
  2526. init_global_and_expr_for_bb, /* init_bb */
  2527. extend_insn_data, /* extend_insn */
  2528. init_global_and_expr_for_insn /* init_insn */
  2529. };
  2530. sched_scan (&ssi, bbs);
  2531. }
  2532. /* Finalize region-scope data structures for basic blocks. */
  2533. static void
  2534. finish_global_and_expr_for_bb (basic_block bb)
  2535. {
  2536. av_set_clear (&BB_AV_SET (bb));
  2537. BB_AV_LEVEL (bb) = 0;
  2538. }
  2539. /* Finalize INSN's data. */
  2540. static void
  2541. finish_global_and_expr_insn (insn_t insn)
  2542. {
  2543. if (LABEL_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
  2544. return;
  2545. gcc_assert (INSN_P (insn));
  2546. if (INSN_LUID (insn) > 0)
  2547. {
  2548. free_first_time_insn_data (insn);
  2549. INSN_WS_LEVEL (insn) = 0;
  2550. CANT_MOVE (insn) = 0;
  2551. /* We can no longer assert this, as vinsns of this insn could be
  2552. easily live in other insn's caches. This should be changed to
  2553. a counter-like approach among all vinsns. */
  2554. gcc_assert (true || VINSN_COUNT (INSN_VINSN (insn)) == 1);
  2555. clear_expr (INSN_EXPR (insn));
  2556. }
  2557. }
  2558. /* Finalize per instruction data for the whole region. */
  2559. void
  2560. sel_finish_global_and_expr (void)
  2561. {
  2562. {
  2563. bb_vec_t bbs;
  2564. int i;
  2565. bbs.create (current_nr_blocks);
  2566. for (i = 0; i < current_nr_blocks; i++)
  2567. bbs.quick_push (BASIC_BLOCK_FOR_FN (cfun, BB_TO_BLOCK (i)));
  2568. /* Clear AV_SETs and INSN_EXPRs. */
  2569. {
  2570. const struct sched_scan_info_def ssi =
  2571. {
  2572. NULL, /* extend_bb */
  2573. finish_global_and_expr_for_bb, /* init_bb */
  2574. NULL, /* extend_insn */
  2575. finish_global_and_expr_insn /* init_insn */
  2576. };
  2577. sched_scan (&ssi, bbs);
  2578. }
  2579. bbs.release ();
  2580. }
  2581. finish_insns ();
  2582. }
  2583. /* In the below hooks, we merely calculate whether or not a dependence
  2584. exists, and in what part of insn. However, we will need more data
  2585. when we'll start caching dependence requests. */
  2586. /* Container to hold information for dependency analysis. */
  2587. static struct
  2588. {
  2589. deps_t dc;
  2590. /* A variable to track which part of rtx we are scanning in
  2591. sched-deps.c: sched_analyze_insn (). */
  2592. deps_where_t where;
  2593. /* Current producer. */
  2594. insn_t pro;
  2595. /* Current consumer. */
  2596. vinsn_t con;
  2597. /* Is SEL_DEPS_HAS_DEP_P[DEPS_IN_X] is true, then X has a dependence.
  2598. X is from { INSN, LHS, RHS }. */
  2599. ds_t has_dep_p[DEPS_IN_NOWHERE];
  2600. } has_dependence_data;
  2601. /* Start analyzing dependencies of INSN. */
  2602. static void
  2603. has_dependence_start_insn (insn_t insn ATTRIBUTE_UNUSED)
  2604. {
  2605. gcc_assert (has_dependence_data.where == DEPS_IN_NOWHERE);
  2606. has_dependence_data.where = DEPS_IN_INSN;
  2607. }
  2608. /* Finish analyzing dependencies of an insn. */
  2609. static void
  2610. has_dependence_finish_insn (void)
  2611. {
  2612. gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
  2613. has_dependence_data.where = DEPS_IN_NOWHERE;
  2614. }
  2615. /* Start analyzing dependencies of LHS. */
  2616. static void
  2617. has_dependence_start_lhs (rtx lhs ATTRIBUTE_UNUSED)
  2618. {
  2619. gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
  2620. if (VINSN_LHS (has_dependence_data.con) != NULL)
  2621. has_dependence_data.where = DEPS_IN_LHS;
  2622. }
  2623. /* Finish analyzing dependencies of an lhs. */
  2624. static void
  2625. has_dependence_finish_lhs (void)
  2626. {
  2627. has_dependence_data.where = DEPS_IN_INSN;
  2628. }
  2629. /* Start analyzing dependencies of RHS. */
  2630. static void
  2631. has_dependence_start_rhs (rtx rhs ATTRIBUTE_UNUSED)
  2632. {
  2633. gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
  2634. if (VINSN_RHS (has_dependence_data.con) != NULL)
  2635. has_dependence_data.where = DEPS_IN_RHS;
  2636. }
  2637. /* Start analyzing dependencies of an rhs. */
  2638. static void
  2639. has_dependence_finish_rhs (void)
  2640. {
  2641. gcc_assert (has_dependence_data.where == DEPS_IN_RHS
  2642. || has_dependence_data.where == DEPS_IN_INSN);
  2643. has_dependence_data.where = DEPS_IN_INSN;
  2644. }
  2645. /* Note a set of REGNO. */
  2646. static void
  2647. has_dependence_note_reg_set (int regno)
  2648. {
  2649. struct deps_reg *reg_last = &has_dependence_data.dc->reg_last[regno];
  2650. if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
  2651. VINSN_INSN_RTX
  2652. (has_dependence_data.con)))
  2653. {
  2654. ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
  2655. if (reg_last->sets != NULL
  2656. || reg_last->clobbers != NULL)
  2657. *dsp = (*dsp & ~SPECULATIVE) | DEP_OUTPUT;
  2658. if (reg_last->uses || reg_last->implicit_sets)
  2659. *dsp = (*dsp & ~SPECULATIVE) | DEP_ANTI;
  2660. }
  2661. }
  2662. /* Note a clobber of REGNO. */
  2663. static void
  2664. has_dependence_note_reg_clobber (int regno)
  2665. {
  2666. struct deps_reg *reg_last = &has_dependence_data.dc->reg_last[regno];
  2667. if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
  2668. VINSN_INSN_RTX
  2669. (has_dependence_data.con)))
  2670. {
  2671. ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
  2672. if (reg_last->sets)
  2673. *dsp = (*dsp & ~SPECULATIVE) | DEP_OUTPUT;
  2674. if (reg_last->uses || reg_last->implicit_sets)
  2675. *dsp = (*dsp & ~SPECULATIVE) | DEP_ANTI;
  2676. }
  2677. }
  2678. /* Note a use of REGNO. */
  2679. static void
  2680. has_dependence_note_reg_use (int regno)
  2681. {
  2682. struct deps_reg *reg_last = &has_dependence_data.dc->reg_last[regno];
  2683. if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
  2684. VINSN_INSN_RTX
  2685. (has_dependence_data.con)))
  2686. {
  2687. ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
  2688. if (reg_last->sets)
  2689. *dsp = (*dsp & ~SPECULATIVE) | DEP_TRUE;
  2690. if (reg_last->clobbers || reg_last->implicit_sets)
  2691. *dsp = (*dsp & ~SPECULATIVE) | DEP_ANTI;
  2692. /* Merge BE_IN_SPEC bits into *DSP when the dependency producer
  2693. is actually a check insn. We need to do this for any register
  2694. read-read dependency with the check unless we track properly
  2695. all registers written by BE_IN_SPEC-speculated insns, as
  2696. we don't have explicit dependence lists. See PR 53975. */
  2697. if (reg_last->uses)
  2698. {
  2699. ds_t pro_spec_checked_ds;
  2700. pro_spec_checked_ds = INSN_SPEC_CHECKED_DS (has_dependence_data.pro);
  2701. pro_spec_checked_ds = ds_get_max_dep_weak (pro_spec_checked_ds);
  2702. if (pro_spec_checked_ds != 0)
  2703. *dsp = ds_full_merge (*dsp, pro_spec_checked_ds,
  2704. NULL_RTX, NULL_RTX);
  2705. }
  2706. }
  2707. }
  2708. /* Note a memory dependence. */
  2709. static void
  2710. has_dependence_note_mem_dep (rtx mem ATTRIBUTE_UNUSED,
  2711. rtx pending_mem ATTRIBUTE_UNUSED,
  2712. insn_t pending_insn ATTRIBUTE_UNUSED,
  2713. ds_t ds ATTRIBUTE_UNUSED)
  2714. {
  2715. if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
  2716. VINSN_INSN_RTX (has_dependence_data.con)))
  2717. {
  2718. ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
  2719. *dsp = ds_full_merge (ds, *dsp, pending_mem, mem);
  2720. }
  2721. }
  2722. /* Note a dependence. */
  2723. static void
  2724. has_dependence_note_dep (insn_t pro ATTRIBUTE_UNUSED,
  2725. ds_t ds ATTRIBUTE_UNUSED)
  2726. {
  2727. if (!sched_insns_conditions_mutex_p (has_dependence_data.pro,
  2728. VINSN_INSN_RTX (has_dependence_data.con)))
  2729. {
  2730. ds_t *dsp = &has_dependence_data.has_dep_p[has_dependence_data.where];
  2731. *dsp = ds_full_merge (ds, *dsp, NULL_RTX, NULL_RTX);
  2732. }
  2733. }
  2734. /* Mark the insn as having a hard dependence that prevents speculation. */
  2735. void
  2736. sel_mark_hard_insn (rtx insn)
  2737. {
  2738. int i;
  2739. /* Only work when we're in has_dependence_p mode.
  2740. ??? This is a hack, this should actually be a hook. */
  2741. if (!has_dependence_data.dc || !has_dependence_data.pro)
  2742. return;
  2743. gcc_assert (insn == VINSN_INSN_RTX (has_dependence_data.con));
  2744. gcc_assert (has_dependence_data.where == DEPS_IN_INSN);
  2745. for (i = 0; i < DEPS_IN_NOWHERE; i++)
  2746. has_dependence_data.has_dep_p[i] &= ~SPECULATIVE;
  2747. }
  2748. /* This structure holds the hooks for the dependency analysis used when
  2749. actually processing dependencies in the scheduler. */
  2750. static struct sched_deps_info_def has_dependence_sched_deps_info;
  2751. /* This initializes most of the fields of the above structure. */
  2752. static const struct sched_deps_info_def const_has_dependence_sched_deps_info =
  2753. {
  2754. NULL,
  2755. has_dependence_start_insn,
  2756. has_dependence_finish_insn,
  2757. has_dependence_start_lhs,
  2758. has_dependence_finish_lhs,
  2759. has_dependence_start_rhs,
  2760. has_dependence_finish_rhs,
  2761. has_dependence_note_reg_set,
  2762. has_dependence_note_reg_clobber,
  2763. has_dependence_note_reg_use,
  2764. has_dependence_note_mem_dep,
  2765. has_dependence_note_dep,
  2766. 0, /* use_cselib */
  2767. 0, /* use_deps_list */
  2768. 0 /* generate_spec_deps */
  2769. };
  2770. /* Initialize has_dependence_sched_deps_info with extra spec field. */
  2771. static void
  2772. setup_has_dependence_sched_deps_info (void)
  2773. {
  2774. memcpy (&has_dependence_sched_deps_info,
  2775. &const_has_dependence_sched_deps_info,
  2776. sizeof (has_dependence_sched_deps_info));
  2777. if (spec_info != NULL)
  2778. has_dependence_sched_deps_info.generate_spec_deps = 1;
  2779. sched_deps_info = &has_dependence_sched_deps_info;
  2780. }
  2781. /* Remove all dependences found and recorded in has_dependence_data array. */
  2782. void
  2783. sel_clear_has_dependence (void)
  2784. {
  2785. int i;
  2786. for (i = 0; i < DEPS_IN_NOWHERE; i++)
  2787. has_dependence_data.has_dep_p[i] = 0;
  2788. }
  2789. /* Return nonzero if EXPR has is dependent upon PRED. Return the pointer
  2790. to the dependence information array in HAS_DEP_PP. */
  2791. ds_t
  2792. has_dependence_p (expr_t expr, insn_t pred, ds_t **has_dep_pp)
  2793. {
  2794. int i;
  2795. ds_t ds;
  2796. struct deps_desc *dc;
  2797. if (INSN_SIMPLEJUMP_P (pred))
  2798. /* Unconditional jump is just a transfer of control flow.
  2799. Ignore it. */
  2800. return false;
  2801. dc = &INSN_DEPS_CONTEXT (pred);
  2802. /* We init this field lazily. */
  2803. if (dc->reg_last == NULL)
  2804. init_deps_reg_last (dc);
  2805. if (!dc->readonly)
  2806. {
  2807. has_dependence_data.pro = NULL;
  2808. /* Initialize empty dep context with information about PRED. */
  2809. advance_deps_context (dc, pred);
  2810. dc->readonly = 1;
  2811. }
  2812. has_dependence_data.where = DEPS_IN_NOWHERE;
  2813. has_dependence_data.pro = pred;
  2814. has_dependence_data.con = EXPR_VINSN (expr);
  2815. has_dependence_data.dc = dc;
  2816. sel_clear_has_dependence ();
  2817. /* Now catch all dependencies that would be generated between PRED and
  2818. INSN. */
  2819. setup_has_dependence_sched_deps_info ();
  2820. deps_analyze_insn (dc, EXPR_INSN_RTX (expr));
  2821. has_dependence_data.dc = NULL;
  2822. /* When a barrier was found, set DEPS_IN_INSN bits. */
  2823. if (dc->last_reg_pending_barrier == TRUE_BARRIER)
  2824. has_dependence_data.has_dep_p[DEPS_IN_INSN] = DEP_TRUE;
  2825. else if (dc->last_reg_pending_barrier == MOVE_BARRIER)
  2826. has_dependence_data.has_dep_p[DEPS_IN_INSN] = DEP_ANTI;
  2827. /* Do not allow stores to memory to move through checks. Currently
  2828. we don't move this to sched-deps.c as the check doesn't have
  2829. obvious places to which this dependence can be attached.
  2830. FIMXE: this should go to a hook. */
  2831. if (EXPR_LHS (expr)
  2832. && MEM_P (EXPR_LHS (expr))
  2833. && sel_insn_is_speculation_check (pred))
  2834. has_dependence_data.has_dep_p[DEPS_IN_INSN] = DEP_ANTI;
  2835. *has_dep_pp = has_dependence_data.has_dep_p;
  2836. ds = 0;
  2837. for (i = 0; i < DEPS_IN_NOWHERE; i++)
  2838. ds = ds_full_merge (ds, has_dependence_data.has_dep_p[i],
  2839. NULL_RTX, NULL_RTX);
  2840. return ds;
  2841. }
  2842. /* Dependence hooks implementation that checks dependence latency constraints
  2843. on the insns being scheduled. The entry point for these routines is
  2844. tick_check_p predicate. */
  2845. static struct
  2846. {
  2847. /* An expr we are currently checking. */
  2848. expr_t expr;
  2849. /* A minimal cycle for its scheduling. */
  2850. int cycle;
  2851. /* Whether we have seen a true dependence while checking. */
  2852. bool seen_true_dep_p;
  2853. } tick_check_data;
  2854. /* Update minimal scheduling cycle for tick_check_insn given that it depends
  2855. on PRO with status DS and weight DW. */
  2856. static void
  2857. tick_check_dep_with_dw (insn_t pro_insn, ds_t ds, dw_t dw)
  2858. {
  2859. expr_t con_expr = tick_check_data.expr;
  2860. insn_t con_insn = EXPR_INSN_RTX (con_expr);
  2861. if (con_insn != pro_insn)
  2862. {
  2863. enum reg_note dt;
  2864. int tick;
  2865. if (/* PROducer was removed from above due to pipelining. */
  2866. !INSN_IN_STREAM_P (pro_insn)
  2867. /* Or PROducer was originally on the next iteration regarding the
  2868. CONsumer. */
  2869. || (INSN_SCHED_TIMES (pro_insn)
  2870. - EXPR_SCHED_TIMES (con_expr)) > 1)
  2871. /* Don't count this dependence. */
  2872. return;
  2873. dt = ds_to_dt (ds);
  2874. if (dt == REG_DEP_TRUE)
  2875. tick_check_data.seen_true_dep_p = true;
  2876. gcc_assert (INSN_SCHED_CYCLE (pro_insn) > 0);
  2877. {
  2878. dep_def _dep, *dep = &_dep;
  2879. init_dep (dep, pro_insn, con_insn, dt);
  2880. tick = INSN_SCHED_CYCLE (pro_insn) + dep_cost_1 (dep, dw);
  2881. }
  2882. /* When there are several kinds of dependencies between pro and con,
  2883. only REG_DEP_TRUE should be taken into account. */
  2884. if (tick > tick_check_data.cycle
  2885. && (dt == REG_DEP_TRUE || !tick_check_data.seen_true_dep_p))
  2886. tick_check_data.cycle = tick;
  2887. }
  2888. }
  2889. /* An implementation of note_dep hook. */
  2890. static void
  2891. tick_check_note_dep (insn_t pro, ds_t ds)
  2892. {
  2893. tick_check_dep_with_dw (pro, ds, 0);
  2894. }
  2895. /* An implementation of note_mem_dep hook. */
  2896. static void
  2897. tick_check_note_mem_dep (rtx mem1, rtx mem2, insn_t pro, ds_t ds)
  2898. {
  2899. dw_t dw;
  2900. dw = (ds_to_dt (ds) == REG_DEP_TRUE
  2901. ? estimate_dep_weak (mem1, mem2)
  2902. : 0);
  2903. tick_check_dep_with_dw (pro, ds, dw);
  2904. }
  2905. /* This structure contains hooks for dependence analysis used when determining
  2906. whether an insn is ready for scheduling. */
  2907. static struct sched_deps_info_def tick_check_sched_deps_info =
  2908. {
  2909. NULL,
  2910. NULL,
  2911. NULL,
  2912. NULL,
  2913. NULL,
  2914. NULL,
  2915. NULL,
  2916. haifa_note_reg_set,
  2917. haifa_note_reg_clobber,
  2918. haifa_note_reg_use,
  2919. tick_check_note_mem_dep,
  2920. tick_check_note_dep,
  2921. 0, 0, 0
  2922. };
  2923. /* Estimate number of cycles from the current cycle of FENCE until EXPR can be
  2924. scheduled. Return 0 if all data from producers in DC is ready. */
  2925. int
  2926. tick_check_p (expr_t expr, deps_t dc, fence_t fence)
  2927. {
  2928. int cycles_left;
  2929. /* Initialize variables. */
  2930. tick_check_data.expr = expr;
  2931. tick_check_data.cycle = 0;
  2932. tick_check_data.seen_true_dep_p = false;
  2933. sched_deps_info = &tick_check_sched_deps_info;
  2934. gcc_assert (!dc->readonly);
  2935. dc->readonly = 1;
  2936. deps_analyze_insn (dc, EXPR_INSN_RTX (expr));
  2937. dc->readonly = 0;
  2938. cycles_left = tick_check_data.cycle - FENCE_CYCLE (fence);
  2939. return cycles_left >= 0 ? cycles_left : 0;
  2940. }
  2941. /* Functions to work with insns. */
  2942. /* Returns true if LHS of INSN is the same as DEST of an insn
  2943. being moved. */
  2944. bool
  2945. lhs_of_insn_equals_to_dest_p (insn_t insn, rtx dest)
  2946. {
  2947. rtx lhs = INSN_LHS (insn);
  2948. if (lhs == NULL || dest == NULL)
  2949. return false;
  2950. return rtx_equal_p (lhs, dest);
  2951. }
  2952. /* Return s_i_d entry of INSN. Callable from debugger. */
  2953. sel_insn_data_def
  2954. insn_sid (insn_t insn)
  2955. {
  2956. return *SID (insn);
  2957. }
  2958. /* True when INSN is a speculative check. We can tell this by looking
  2959. at the data structures of the selective scheduler, not by examining
  2960. the pattern. */
  2961. bool
  2962. sel_insn_is_speculation_check (rtx insn)
  2963. {
  2964. return s_i_d.exists () && !! INSN_SPEC_CHECKED_DS (insn);
  2965. }
  2966. /* Extracts machine mode MODE and destination location DST_LOC
  2967. for given INSN. */
  2968. void
  2969. get_dest_and_mode (rtx insn, rtx *dst_loc, machine_mode *mode)
  2970. {
  2971. rtx pat = PATTERN (insn);
  2972. gcc_assert (dst_loc);
  2973. gcc_assert (GET_CODE (pat) == SET);
  2974. *dst_loc = SET_DEST (pat);
  2975. gcc_assert (*dst_loc);
  2976. gcc_assert (MEM_P (*dst_loc) || REG_P (*dst_loc));
  2977. if (mode)
  2978. *mode = GET_MODE (*dst_loc);
  2979. }
  2980. /* Returns true when moving through JUMP will result in bookkeeping
  2981. creation. */
  2982. bool
  2983. bookkeeping_can_be_created_if_moved_through_p (insn_t jump)
  2984. {
  2985. insn_t succ;
  2986. succ_iterator si;
  2987. FOR_EACH_SUCC (succ, si, jump)
  2988. if (sel_num_cfg_preds_gt_1 (succ))
  2989. return true;
  2990. return false;
  2991. }
  2992. /* Return 'true' if INSN is the only one in its basic block. */
  2993. static bool
  2994. insn_is_the_only_one_in_bb_p (insn_t insn)
  2995. {
  2996. return sel_bb_head_p (insn) && sel_bb_end_p (insn);
  2997. }
  2998. #ifdef ENABLE_CHECKING
  2999. /* Check that the region we're scheduling still has at most one
  3000. backedge. */
  3001. static void
  3002. verify_backedges (void)
  3003. {
  3004. if (pipelining_p)
  3005. {
  3006. int i, n = 0;
  3007. edge e;
  3008. edge_iterator ei;
  3009. for (i = 0; i < current_nr_blocks; i++)
  3010. FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (cfun, BB_TO_BLOCK (i))->succs)
  3011. if (in_current_region_p (e->dest)
  3012. && BLOCK_TO_BB (e->dest->index) < i)
  3013. n++;
  3014. gcc_assert (n <= 1);
  3015. }
  3016. }
  3017. #endif
  3018. /* Functions to work with control flow. */
  3019. /* Recompute BLOCK_TO_BB and BB_FOR_BLOCK for current region so that blocks
  3020. are sorted in topological order (it might have been invalidated by
  3021. redirecting an edge). */
  3022. static void
  3023. sel_recompute_toporder (void)
  3024. {
  3025. int i, n, rgn;
  3026. int *postorder, n_blocks;
  3027. postorder = XALLOCAVEC (int, n_basic_blocks_for_fn (cfun));
  3028. n_blocks = post_order_compute (postorder, false, false);
  3029. rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
  3030. for (n = 0, i = n_blocks - 1; i >= 0; i--)
  3031. if (CONTAINING_RGN (postorder[i]) == rgn)
  3032. {
  3033. BLOCK_TO_BB (postorder[i]) = n;
  3034. BB_TO_BLOCK (n) = postorder[i];
  3035. n++;
  3036. }
  3037. /* Assert that we updated info for all blocks. We may miss some blocks if
  3038. this function is called when redirecting an edge made a block
  3039. unreachable, but that block is not deleted yet. */
  3040. gcc_assert (n == RGN_NR_BLOCKS (rgn));
  3041. }
  3042. /* Tidy the possibly empty block BB. */
  3043. static bool
  3044. maybe_tidy_empty_bb (basic_block bb)
  3045. {
  3046. basic_block succ_bb, pred_bb, note_bb;
  3047. vec<basic_block> dom_bbs;
  3048. edge e;
  3049. edge_iterator ei;
  3050. bool rescan_p;
  3051. /* Keep empty bb only if this block immediately precedes EXIT and
  3052. has incoming non-fallthrough edge, or it has no predecessors or
  3053. successors. Otherwise remove it. */
  3054. if (!sel_bb_empty_p (bb)
  3055. || (single_succ_p (bb)
  3056. && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
  3057. && (!single_pred_p (bb)
  3058. || !(single_pred_edge (bb)->flags & EDGE_FALLTHRU)))
  3059. || EDGE_COUNT (bb->preds) == 0
  3060. || EDGE_COUNT (bb->succs) == 0)
  3061. return false;
  3062. /* Do not attempt to redirect complex edges. */
  3063. FOR_EACH_EDGE (e, ei, bb->preds)
  3064. if (e->flags & EDGE_COMPLEX)
  3065. return false;
  3066. else if (e->flags & EDGE_FALLTHRU)
  3067. {
  3068. rtx note;
  3069. /* If prev bb ends with asm goto, see if any of the
  3070. ASM_OPERANDS_LABELs don't point to the fallthru
  3071. label. Do not attempt to redirect it in that case. */
  3072. if (JUMP_P (BB_END (e->src))
  3073. && (note = extract_asm_operands (PATTERN (BB_END (e->src)))))
  3074. {
  3075. int i, n = ASM_OPERANDS_LABEL_LENGTH (note);
  3076. for (i = 0; i < n; ++i)
  3077. if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (bb))
  3078. return false;
  3079. }
  3080. }
  3081. free_data_sets (bb);
  3082. /* Do not delete BB if it has more than one successor.
  3083. That can occur when we moving a jump. */
  3084. if (!single_succ_p (bb))
  3085. {
  3086. gcc_assert (can_merge_blocks_p (bb->prev_bb, bb));
  3087. sel_merge_blocks (bb->prev_bb, bb);
  3088. return true;
  3089. }
  3090. succ_bb = single_succ (bb);
  3091. rescan_p = true;
  3092. pred_bb = NULL;
  3093. dom_bbs.create (0);
  3094. /* Save a pred/succ from the current region to attach the notes to. */
  3095. note_bb = NULL;
  3096. FOR_EACH_EDGE (e, ei, bb->preds)
  3097. if (in_current_region_p (e->src))
  3098. {
  3099. note_bb = e->src;
  3100. break;
  3101. }
  3102. if (note_bb == NULL)
  3103. note_bb = succ_bb;
  3104. /* Redirect all non-fallthru edges to the next bb. */
  3105. while (rescan_p)
  3106. {
  3107. rescan_p = false;
  3108. FOR_EACH_EDGE (e, ei, bb->preds)
  3109. {
  3110. pred_bb = e->src;
  3111. if (!(e->flags & EDGE_FALLTHRU))
  3112. {
  3113. /* We can not invalidate computed topological order by moving
  3114. the edge destination block (E->SUCC) along a fallthru edge.
  3115. We will update dominators here only when we'll get
  3116. an unreachable block when redirecting, otherwise
  3117. sel_redirect_edge_and_branch will take care of it. */
  3118. if (e->dest != bb
  3119. && single_pred_p (e->dest))
  3120. dom_bbs.safe_push (e->dest);
  3121. sel_redirect_edge_and_branch (e, succ_bb);
  3122. rescan_p = true;
  3123. break;
  3124. }
  3125. /* If the edge is fallthru, but PRED_BB ends in a conditional jump
  3126. to BB (so there is no non-fallthru edge from PRED_BB to BB), we
  3127. still have to adjust it. */
  3128. else if (single_succ_p (pred_bb) && any_condjump_p (BB_END (pred_bb)))
  3129. {
  3130. /* If possible, try to remove the unneeded conditional jump. */
  3131. if (INSN_SCHED_TIMES (BB_END (pred_bb)) == 0
  3132. && !IN_CURRENT_FENCE_P (BB_END (pred_bb)))
  3133. {
  3134. if (!sel_remove_insn (BB_END (pred_bb), false, false))
  3135. tidy_fallthru_edge (e);
  3136. }
  3137. else
  3138. sel_redirect_edge_and_branch (e, succ_bb);
  3139. rescan_p = true;
  3140. break;
  3141. }
  3142. }
  3143. }
  3144. if (can_merge_blocks_p (bb->prev_bb, bb))
  3145. sel_merge_blocks (bb->prev_bb, bb);
  3146. else
  3147. {
  3148. /* This is a block without fallthru predecessor. Just delete it. */
  3149. gcc_assert (note_bb);
  3150. move_bb_info (note_bb, bb);
  3151. remove_empty_bb (bb, true);
  3152. }
  3153. if (!dom_bbs.is_empty ())
  3154. {
  3155. dom_bbs.safe_push (succ_bb);
  3156. iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, false);
  3157. dom_bbs.release ();
  3158. }
  3159. return true;
  3160. }
  3161. /* Tidy the control flow after we have removed original insn from
  3162. XBB. Return true if we have removed some blocks. When FULL_TIDYING
  3163. is true, also try to optimize control flow on non-empty blocks. */
  3164. bool
  3165. tidy_control_flow (basic_block xbb, bool full_tidying)
  3166. {
  3167. bool changed = true;
  3168. insn_t first, last;
  3169. /* First check whether XBB is empty. */
  3170. changed = maybe_tidy_empty_bb (xbb);
  3171. if (changed || !full_tidying)
  3172. return changed;
  3173. /* Check if there is a unnecessary jump after insn left. */
  3174. if (bb_has_removable_jump_to_p (xbb, xbb->next_bb)
  3175. && INSN_SCHED_TIMES (BB_END (xbb)) == 0
  3176. && !IN_CURRENT_FENCE_P (BB_END (xbb)))
  3177. {
  3178. if (sel_remove_insn (BB_END (xbb), false, false))
  3179. return true;
  3180. tidy_fallthru_edge (EDGE_SUCC (xbb, 0));
  3181. }
  3182. first = sel_bb_head (xbb);
  3183. last = sel_bb_end (xbb);
  3184. if (MAY_HAVE_DEBUG_INSNS)
  3185. {
  3186. if (first != last && DEBUG_INSN_P (first))
  3187. do
  3188. first = NEXT_INSN (first);
  3189. while (first != last && (DEBUG_INSN_P (first) || NOTE_P (first)));
  3190. if (first != last && DEBUG_INSN_P (last))
  3191. do
  3192. last = PREV_INSN (last);
  3193. while (first != last && (DEBUG_INSN_P (last) || NOTE_P (last)));
  3194. }
  3195. /* Check if there is an unnecessary jump in previous basic block leading
  3196. to next basic block left after removing INSN from stream.
  3197. If it is so, remove that jump and redirect edge to current
  3198. basic block (where there was INSN before deletion). This way
  3199. when NOP will be deleted several instructions later with its
  3200. basic block we will not get a jump to next instruction, which
  3201. can be harmful. */
  3202. if (first == last
  3203. && !sel_bb_empty_p (xbb)
  3204. && INSN_NOP_P (last)
  3205. /* Flow goes fallthru from current block to the next. */
  3206. && EDGE_COUNT (xbb->succs) == 1
  3207. && (EDGE_SUCC (xbb, 0)->flags & EDGE_FALLTHRU)
  3208. /* When successor is an EXIT block, it may not be the next block. */
  3209. && single_succ (xbb) != EXIT_BLOCK_PTR_FOR_FN (cfun)
  3210. /* And unconditional jump in previous basic block leads to
  3211. next basic block of XBB and this jump can be safely removed. */
  3212. && in_current_region_p (xbb->prev_bb)
  3213. && bb_has_removable_jump_to_p (xbb->prev_bb, xbb->next_bb)
  3214. && INSN_SCHED_TIMES (BB_END (xbb->prev_bb)) == 0
  3215. /* Also this jump is not at the scheduling boundary. */
  3216. && !IN_CURRENT_FENCE_P (BB_END (xbb->prev_bb)))
  3217. {
  3218. bool recompute_toporder_p;
  3219. /* Clear data structures of jump - jump itself will be removed
  3220. by sel_redirect_edge_and_branch. */
  3221. clear_expr (INSN_EXPR (BB_END (xbb->prev_bb)));
  3222. recompute_toporder_p
  3223. = sel_redirect_edge_and_branch (EDGE_SUCC (xbb->prev_bb, 0), xbb);
  3224. gcc_assert (EDGE_SUCC (xbb->prev_bb, 0)->flags & EDGE_FALLTHRU);
  3225. /* It can turn out that after removing unused jump, basic block
  3226. that contained that jump, becomes empty too. In such case
  3227. remove it too. */
  3228. if (sel_bb_empty_p (xbb->prev_bb))
  3229. changed = maybe_tidy_empty_bb (xbb->prev_bb);
  3230. if (recompute_toporder_p)
  3231. sel_recompute_toporder ();
  3232. }
  3233. #ifdef ENABLE_CHECKING
  3234. verify_backedges ();
  3235. verify_dominators (CDI_DOMINATORS);
  3236. #endif
  3237. return changed;
  3238. }
  3239. /* Purge meaningless empty blocks in the middle of a region. */
  3240. void
  3241. purge_empty_blocks (void)
  3242. {
  3243. int i;
  3244. /* Do not attempt to delete the first basic block in the region. */
  3245. for (i = 1; i < current_nr_blocks; )
  3246. {
  3247. basic_block b = BASIC_BLOCK_FOR_FN (cfun, BB_TO_BLOCK (i));
  3248. if (maybe_tidy_empty_bb (b))
  3249. continue;
  3250. i++;
  3251. }
  3252. }
  3253. /* Rip-off INSN from the insn stream. When ONLY_DISCONNECT is true,
  3254. do not delete insn's data, because it will be later re-emitted.
  3255. Return true if we have removed some blocks afterwards. */
  3256. bool
  3257. sel_remove_insn (insn_t insn, bool only_disconnect, bool full_tidying)
  3258. {
  3259. basic_block bb = BLOCK_FOR_INSN (insn);
  3260. gcc_assert (INSN_IN_STREAM_P (insn));
  3261. if (DEBUG_INSN_P (insn) && BB_AV_SET_VALID_P (bb))
  3262. {
  3263. expr_t expr;
  3264. av_set_iterator i;
  3265. /* When we remove a debug insn that is head of a BB, it remains
  3266. in the AV_SET of the block, but it shouldn't. */
  3267. FOR_EACH_EXPR_1 (expr, i, &BB_AV_SET (bb))
  3268. if (EXPR_INSN_RTX (expr) == insn)
  3269. {
  3270. av_set_iter_remove (&i);
  3271. break;
  3272. }
  3273. }
  3274. if (only_disconnect)
  3275. remove_insn (insn);
  3276. else
  3277. {
  3278. delete_insn (insn);
  3279. clear_expr (INSN_EXPR (insn));
  3280. }
  3281. /* It is necessary to NULL these fields in case we are going to re-insert
  3282. INSN into the insns stream, as will usually happen in the ONLY_DISCONNECT
  3283. case, but also for NOPs that we will return to the nop pool. */
  3284. SET_PREV_INSN (insn) = NULL_RTX;
  3285. SET_NEXT_INSN (insn) = NULL_RTX;
  3286. set_block_for_insn (insn, NULL);
  3287. return tidy_control_flow (bb, full_tidying);
  3288. }
  3289. /* Estimate number of the insns in BB. */
  3290. static int
  3291. sel_estimate_number_of_insns (basic_block bb)
  3292. {
  3293. int res = 0;
  3294. insn_t insn = NEXT_INSN (BB_HEAD (bb)), next_tail = NEXT_INSN (BB_END (bb));
  3295. for (; insn != next_tail; insn = NEXT_INSN (insn))
  3296. if (NONDEBUG_INSN_P (insn))
  3297. res++;
  3298. return res;
  3299. }
  3300. /* We don't need separate luids for notes or labels. */
  3301. static int
  3302. sel_luid_for_non_insn (rtx x)
  3303. {
  3304. gcc_assert (NOTE_P (x) || LABEL_P (x));
  3305. return -1;
  3306. }
  3307. /* Find the proper seqno for inserting at INSN by successors.
  3308. Return -1 if no successors with positive seqno exist. */
  3309. static int
  3310. get_seqno_by_succs (rtx_insn *insn)
  3311. {
  3312. basic_block bb = BLOCK_FOR_INSN (insn);
  3313. rtx_insn *tmp = insn, *end = BB_END (bb);
  3314. int seqno;
  3315. insn_t succ = NULL;
  3316. succ_iterator si;
  3317. while (tmp != end)
  3318. {
  3319. tmp = NEXT_INSN (tmp);
  3320. if (INSN_P (tmp))
  3321. return INSN_SEQNO (tmp);
  3322. }
  3323. seqno = INT_MAX;
  3324. FOR_EACH_SUCC_1 (succ, si, end, SUCCS_NORMAL)
  3325. if (INSN_SEQNO (succ) > 0)
  3326. seqno = MIN (seqno, INSN_SEQNO (succ));
  3327. if (seqno == INT_MAX)
  3328. return -1;
  3329. return seqno;
  3330. }
  3331. /* Compute seqno for INSN by its preds or succs. Use OLD_SEQNO to compute
  3332. seqno in corner cases. */
  3333. static int
  3334. get_seqno_for_a_jump (insn_t insn, int old_seqno)
  3335. {
  3336. int seqno;
  3337. gcc_assert (INSN_SIMPLEJUMP_P (insn));
  3338. if (!sel_bb_head_p (insn))
  3339. seqno = INSN_SEQNO (PREV_INSN (insn));
  3340. else
  3341. {
  3342. basic_block bb = BLOCK_FOR_INSN (insn);
  3343. if (single_pred_p (bb)
  3344. && !in_current_region_p (single_pred (bb)))
  3345. {
  3346. /* We can have preds outside a region when splitting edges
  3347. for pipelining of an outer loop. Use succ instead.
  3348. There should be only one of them. */
  3349. insn_t succ = NULL;
  3350. succ_iterator si;
  3351. bool first = true;
  3352. gcc_assert (flag_sel_sched_pipelining_outer_loops
  3353. && current_loop_nest);
  3354. FOR_EACH_SUCC_1 (succ, si, insn,
  3355. SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
  3356. {
  3357. gcc_assert (first);
  3358. first = false;
  3359. }
  3360. gcc_assert (succ != NULL);
  3361. seqno = INSN_SEQNO (succ);
  3362. }
  3363. else
  3364. {
  3365. insn_t *preds;
  3366. int n;
  3367. cfg_preds (BLOCK_FOR_INSN (insn), &preds, &n);
  3368. gcc_assert (n > 0);
  3369. /* For one predecessor, use simple method. */
  3370. if (n == 1)
  3371. seqno = INSN_SEQNO (preds[0]);
  3372. else
  3373. seqno = get_seqno_by_preds (insn);
  3374. free (preds);
  3375. }
  3376. }
  3377. /* We were unable to find a good seqno among preds. */
  3378. if (seqno < 0)
  3379. seqno = get_seqno_by_succs (insn);
  3380. if (seqno < 0)
  3381. {
  3382. /* The only case where this could be here legally is that the only
  3383. unscheduled insn was a conditional jump that got removed and turned
  3384. into this unconditional one. Initialize from the old seqno
  3385. of that jump passed down to here. */
  3386. seqno = old_seqno;
  3387. }
  3388. gcc_assert (seqno >= 0);
  3389. return seqno;
  3390. }
  3391. /* Find the proper seqno for inserting at INSN. Returns -1 if no predecessors
  3392. with positive seqno exist. */
  3393. int
  3394. get_seqno_by_preds (rtx_insn *insn)
  3395. {
  3396. basic_block bb = BLOCK_FOR_INSN (insn);
  3397. rtx_insn *tmp = insn, *head = BB_HEAD (bb);
  3398. insn_t *preds;
  3399. int n, i, seqno;
  3400. while (tmp != head)
  3401. {
  3402. tmp = PREV_INSN (tmp);
  3403. if (INSN_P (tmp))
  3404. return INSN_SEQNO (tmp);
  3405. }
  3406. cfg_preds (bb, &preds, &n);
  3407. for (i = 0, seqno = -1; i < n; i++)
  3408. seqno = MAX (seqno, INSN_SEQNO (preds[i]));
  3409. return seqno;
  3410. }
  3411. /* Extend pass-scope data structures for basic blocks. */
  3412. void
  3413. sel_extend_global_bb_info (void)
  3414. {
  3415. sel_global_bb_info.safe_grow_cleared (last_basic_block_for_fn (cfun));
  3416. }
  3417. /* Extend region-scope data structures for basic blocks. */
  3418. static void
  3419. extend_region_bb_info (void)
  3420. {
  3421. sel_region_bb_info.safe_grow_cleared (last_basic_block_for_fn (cfun));
  3422. }
  3423. /* Extend all data structures to fit for all basic blocks. */
  3424. static void
  3425. extend_bb_info (void)
  3426. {
  3427. sel_extend_global_bb_info ();
  3428. extend_region_bb_info ();
  3429. }
  3430. /* Finalize pass-scope data structures for basic blocks. */
  3431. void
  3432. sel_finish_global_bb_info (void)
  3433. {
  3434. sel_global_bb_info.release ();
  3435. }
  3436. /* Finalize region-scope data structures for basic blocks. */
  3437. static void
  3438. finish_region_bb_info (void)
  3439. {
  3440. sel_region_bb_info.release ();
  3441. }
  3442. /* Data for each insn in current region. */
  3443. vec<sel_insn_data_def> s_i_d = vNULL;
  3444. /* Extend data structures for insns from current region. */
  3445. static void
  3446. extend_insn_data (void)
  3447. {
  3448. int reserve;
  3449. sched_extend_target ();
  3450. sched_deps_init (false);
  3451. /* Extend data structures for insns from current region. */
  3452. reserve = (sched_max_luid + 1 - s_i_d.length ());
  3453. if (reserve > 0 && ! s_i_d.space (reserve))
  3454. {
  3455. int size;
  3456. if (sched_max_luid / 2 > 1024)
  3457. size = sched_max_luid + 1024;
  3458. else
  3459. size = 3 * sched_max_luid / 2;
  3460. s_i_d.safe_grow_cleared (size);
  3461. }
  3462. }
  3463. /* Finalize data structures for insns from current region. */
  3464. static void
  3465. finish_insns (void)
  3466. {
  3467. unsigned i;
  3468. /* Clear here all dependence contexts that may have left from insns that were
  3469. removed during the scheduling. */
  3470. for (i = 0; i < s_i_d.length (); i++)
  3471. {
  3472. sel_insn_data_def *sid_entry = &s_i_d[i];
  3473. if (sid_entry->live)
  3474. return_regset_to_pool (sid_entry->live);
  3475. if (sid_entry->analyzed_deps)
  3476. {
  3477. BITMAP_FREE (sid_entry->analyzed_deps);
  3478. BITMAP_FREE (sid_entry->found_deps);
  3479. htab_delete (sid_entry->transformed_insns);
  3480. free_deps (&sid_entry->deps_context);
  3481. }
  3482. if (EXPR_VINSN (&sid_entry->expr))
  3483. {
  3484. clear_expr (&sid_entry->expr);
  3485. /* Also, clear CANT_MOVE bit here, because we really don't want it
  3486. to be passed to the next region. */
  3487. CANT_MOVE_BY_LUID (i) = 0;
  3488. }
  3489. }
  3490. s_i_d.release ();
  3491. }
  3492. /* A proxy to pass initialization data to init_insn (). */
  3493. static sel_insn_data_def _insn_init_ssid;
  3494. static sel_insn_data_t insn_init_ssid = &_insn_init_ssid;
  3495. /* If true create a new vinsn. Otherwise use the one from EXPR. */
  3496. static bool insn_init_create_new_vinsn_p;
  3497. /* Set all necessary data for initialization of the new insn[s]. */
  3498. static expr_t
  3499. set_insn_init (expr_t expr, vinsn_t vi, int seqno)
  3500. {
  3501. expr_t x = &insn_init_ssid->expr;
  3502. copy_expr_onside (x, expr);
  3503. if (vi != NULL)
  3504. {
  3505. insn_init_create_new_vinsn_p = false;
  3506. change_vinsn_in_expr (x, vi);
  3507. }
  3508. else
  3509. insn_init_create_new_vinsn_p = true;
  3510. insn_init_ssid->seqno = seqno;
  3511. return x;
  3512. }
  3513. /* Init data for INSN. */
  3514. static void
  3515. init_insn_data (insn_t insn)
  3516. {
  3517. expr_t expr;
  3518. sel_insn_data_t ssid = insn_init_ssid;
  3519. /* The fields mentioned below are special and hence are not being
  3520. propagated to the new insns. */
  3521. gcc_assert (!ssid->asm_p && ssid->sched_next == NULL
  3522. && !ssid->after_stall_p && ssid->sched_cycle == 0);
  3523. gcc_assert (INSN_P (insn) && INSN_LUID (insn) > 0);
  3524. expr = INSN_EXPR (insn);
  3525. copy_expr (expr, &ssid->expr);
  3526. prepare_insn_expr (insn, ssid->seqno);
  3527. if (insn_init_create_new_vinsn_p)
  3528. change_vinsn_in_expr (expr, vinsn_create (insn, init_insn_force_unique_p));
  3529. if (first_time_insn_init (insn))
  3530. init_first_time_insn_data (insn);
  3531. }
  3532. /* This is used to initialize spurious jumps generated by
  3533. sel_redirect_edge (). OLD_SEQNO is used for initializing seqnos
  3534. in corner cases within get_seqno_for_a_jump. */
  3535. static void
  3536. init_simplejump_data (insn_t insn, int old_seqno)
  3537. {
  3538. init_expr (INSN_EXPR (insn), vinsn_create (insn, false), 0,
  3539. REG_BR_PROB_BASE, 0, 0, 0, 0, 0, 0,
  3540. vNULL, true, false, false,
  3541. false, true);
  3542. INSN_SEQNO (insn) = get_seqno_for_a_jump (insn, old_seqno);
  3543. init_first_time_insn_data (insn);
  3544. }
  3545. /* Perform deferred initialization of insns. This is used to process
  3546. a new jump that may be created by redirect_edge. OLD_SEQNO is used
  3547. for initializing simplejumps in init_simplejump_data. */
  3548. static void
  3549. sel_init_new_insn (insn_t insn, int flags, int old_seqno)
  3550. {
  3551. /* We create data structures for bb when the first insn is emitted in it. */
  3552. if (INSN_P (insn)
  3553. && INSN_IN_STREAM_P (insn)
  3554. && insn_is_the_only_one_in_bb_p (insn))
  3555. {
  3556. extend_bb_info ();
  3557. create_initial_data_sets (BLOCK_FOR_INSN (insn));
  3558. }
  3559. if (flags & INSN_INIT_TODO_LUID)
  3560. {
  3561. sched_extend_luids ();
  3562. sched_init_insn_luid (insn);
  3563. }
  3564. if (flags & INSN_INIT_TODO_SSID)
  3565. {
  3566. extend_insn_data ();
  3567. init_insn_data (insn);
  3568. clear_expr (&insn_init_ssid->expr);
  3569. }
  3570. if (flags & INSN_INIT_TODO_SIMPLEJUMP)
  3571. {
  3572. extend_insn_data ();
  3573. init_simplejump_data (insn, old_seqno);
  3574. }
  3575. gcc_assert (CONTAINING_RGN (BLOCK_NUM (insn))
  3576. == CONTAINING_RGN (BB_TO_BLOCK (0)));
  3577. }
  3578. /* Functions to init/finish work with lv sets. */
  3579. /* Init BB_LV_SET of BB from DF_LR_IN set of BB. */
  3580. static void
  3581. init_lv_set (basic_block bb)
  3582. {
  3583. gcc_assert (!BB_LV_SET_VALID_P (bb));
  3584. BB_LV_SET (bb) = get_regset_from_pool ();
  3585. COPY_REG_SET (BB_LV_SET (bb), DF_LR_IN (bb));
  3586. BB_LV_SET_VALID_P (bb) = true;
  3587. }
  3588. /* Copy liveness information to BB from FROM_BB. */
  3589. static void
  3590. copy_lv_set_from (basic_block bb, basic_block from_bb)
  3591. {
  3592. gcc_assert (!BB_LV_SET_VALID_P (bb));
  3593. COPY_REG_SET (BB_LV_SET (bb), BB_LV_SET (from_bb));
  3594. BB_LV_SET_VALID_P (bb) = true;
  3595. }
  3596. /* Initialize lv set of all bb headers. */
  3597. void
  3598. init_lv_sets (void)
  3599. {
  3600. basic_block bb;
  3601. /* Initialize of LV sets. */
  3602. FOR_EACH_BB_FN (bb, cfun)
  3603. init_lv_set (bb);
  3604. /* Don't forget EXIT_BLOCK. */
  3605. init_lv_set (EXIT_BLOCK_PTR_FOR_FN (cfun));
  3606. }
  3607. /* Release lv set of HEAD. */
  3608. static void
  3609. free_lv_set (basic_block bb)
  3610. {
  3611. gcc_assert (BB_LV_SET (bb) != NULL);
  3612. return_regset_to_pool (BB_LV_SET (bb));
  3613. BB_LV_SET (bb) = NULL;
  3614. BB_LV_SET_VALID_P (bb) = false;
  3615. }
  3616. /* Finalize lv sets of all bb headers. */
  3617. void
  3618. free_lv_sets (void)
  3619. {
  3620. basic_block bb;
  3621. /* Don't forget EXIT_BLOCK. */
  3622. free_lv_set (EXIT_BLOCK_PTR_FOR_FN (cfun));
  3623. /* Free LV sets. */
  3624. FOR_EACH_BB_FN (bb, cfun)
  3625. if (BB_LV_SET (bb))
  3626. free_lv_set (bb);
  3627. }
  3628. /* Mark AV_SET for BB as invalid, so this set will be updated the next time
  3629. compute_av() processes BB. This function is called when creating new basic
  3630. blocks, as well as for blocks (either new or existing) where new jumps are
  3631. created when the control flow is being updated. */
  3632. static void
  3633. invalidate_av_set (basic_block bb)
  3634. {
  3635. BB_AV_LEVEL (bb) = -1;
  3636. }
  3637. /* Create initial data sets for BB (they will be invalid). */
  3638. static void
  3639. create_initial_data_sets (basic_block bb)
  3640. {
  3641. if (BB_LV_SET (bb))
  3642. BB_LV_SET_VALID_P (bb) = false;
  3643. else
  3644. BB_LV_SET (bb) = get_regset_from_pool ();
  3645. invalidate_av_set (bb);
  3646. }
  3647. /* Free av set of BB. */
  3648. static void
  3649. free_av_set (basic_block bb)
  3650. {
  3651. av_set_clear (&BB_AV_SET (bb));
  3652. BB_AV_LEVEL (bb) = 0;
  3653. }
  3654. /* Free data sets of BB. */
  3655. void
  3656. free_data_sets (basic_block bb)
  3657. {
  3658. free_lv_set (bb);
  3659. free_av_set (bb);
  3660. }
  3661. /* Exchange lv sets of TO and FROM. */
  3662. static void
  3663. exchange_lv_sets (basic_block to, basic_block from)
  3664. {
  3665. {
  3666. regset to_lv_set = BB_LV_SET (to);
  3667. BB_LV_SET (to) = BB_LV_SET (from);
  3668. BB_LV_SET (from) = to_lv_set;
  3669. }
  3670. {
  3671. bool to_lv_set_valid_p = BB_LV_SET_VALID_P (to);
  3672. BB_LV_SET_VALID_P (to) = BB_LV_SET_VALID_P (from);
  3673. BB_LV_SET_VALID_P (from) = to_lv_set_valid_p;
  3674. }
  3675. }
  3676. /* Exchange av sets of TO and FROM. */
  3677. static void
  3678. exchange_av_sets (basic_block to, basic_block from)
  3679. {
  3680. {
  3681. av_set_t to_av_set = BB_AV_SET (to);
  3682. BB_AV_SET (to) = BB_AV_SET (from);
  3683. BB_AV_SET (from) = to_av_set;
  3684. }
  3685. {
  3686. int to_av_level = BB_AV_LEVEL (to);
  3687. BB_AV_LEVEL (to) = BB_AV_LEVEL (from);
  3688. BB_AV_LEVEL (from) = to_av_level;
  3689. }
  3690. }
  3691. /* Exchange data sets of TO and FROM. */
  3692. void
  3693. exchange_data_sets (basic_block to, basic_block from)
  3694. {
  3695. exchange_lv_sets (to, from);
  3696. exchange_av_sets (to, from);
  3697. }
  3698. /* Copy data sets of FROM to TO. */
  3699. void
  3700. copy_data_sets (basic_block to, basic_block from)
  3701. {
  3702. gcc_assert (!BB_LV_SET_VALID_P (to) && !BB_AV_SET_VALID_P (to));
  3703. gcc_assert (BB_AV_SET (to) == NULL);
  3704. BB_AV_LEVEL (to) = BB_AV_LEVEL (from);
  3705. BB_LV_SET_VALID_P (to) = BB_LV_SET_VALID_P (from);
  3706. if (BB_AV_SET_VALID_P (from))
  3707. {
  3708. BB_AV_SET (to) = av_set_copy (BB_AV_SET (from));
  3709. }
  3710. if (BB_LV_SET_VALID_P (from))
  3711. {
  3712. gcc_assert (BB_LV_SET (to) != NULL);
  3713. COPY_REG_SET (BB_LV_SET (to), BB_LV_SET (from));
  3714. }
  3715. }
  3716. /* Return an av set for INSN, if any. */
  3717. av_set_t
  3718. get_av_set (insn_t insn)
  3719. {
  3720. av_set_t av_set;
  3721. gcc_assert (AV_SET_VALID_P (insn));
  3722. if (sel_bb_head_p (insn))
  3723. av_set = BB_AV_SET (BLOCK_FOR_INSN (insn));
  3724. else
  3725. av_set = NULL;
  3726. return av_set;
  3727. }
  3728. /* Implementation of AV_LEVEL () macro. Return AV_LEVEL () of INSN. */
  3729. int
  3730. get_av_level (insn_t insn)
  3731. {
  3732. int av_level;
  3733. gcc_assert (INSN_P (insn));
  3734. if (sel_bb_head_p (insn))
  3735. av_level = BB_AV_LEVEL (BLOCK_FOR_INSN (insn));
  3736. else
  3737. av_level = INSN_WS_LEVEL (insn);
  3738. return av_level;
  3739. }
  3740. /* Variables to work with control-flow graph. */
  3741. /* The basic block that already has been processed by the sched_data_update (),
  3742. but hasn't been in sel_add_bb () yet. */
  3743. static vec<basic_block>
  3744. last_added_blocks = vNULL;
  3745. /* A pool for allocating successor infos. */
  3746. static struct
  3747. {
  3748. /* A stack for saving succs_info structures. */
  3749. struct succs_info *stack;
  3750. /* Its size. */
  3751. int size;
  3752. /* Top of the stack. */
  3753. int top;
  3754. /* Maximal value of the top. */
  3755. int max_top;
  3756. } succs_info_pool;
  3757. /* Functions to work with control-flow graph. */
  3758. /* Return basic block note of BB. */
  3759. rtx_insn *
  3760. sel_bb_head (basic_block bb)
  3761. {
  3762. rtx_insn *head;
  3763. if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
  3764. {
  3765. gcc_assert (exit_insn != NULL_RTX);
  3766. head = exit_insn;
  3767. }
  3768. else
  3769. {
  3770. insn_t note;
  3771. note = bb_note (bb);
  3772. head = next_nonnote_insn (note);
  3773. if (head && (BARRIER_P (head) || BLOCK_FOR_INSN (head) != bb))
  3774. head = NULL;
  3775. }
  3776. return head;
  3777. }
  3778. /* Return true if INSN is a basic block header. */
  3779. bool
  3780. sel_bb_head_p (insn_t insn)
  3781. {
  3782. return sel_bb_head (BLOCK_FOR_INSN (insn)) == insn;
  3783. }
  3784. /* Return last insn of BB. */
  3785. rtx_insn *
  3786. sel_bb_end (basic_block bb)
  3787. {
  3788. if (sel_bb_empty_p (bb))
  3789. return NULL;
  3790. gcc_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
  3791. return BB_END (bb);
  3792. }
  3793. /* Return true if INSN is the last insn in its basic block. */
  3794. bool
  3795. sel_bb_end_p (insn_t insn)
  3796. {
  3797. return insn == sel_bb_end (BLOCK_FOR_INSN (insn));
  3798. }
  3799. /* Return true if BB consist of single NOTE_INSN_BASIC_BLOCK. */
  3800. bool
  3801. sel_bb_empty_p (basic_block bb)
  3802. {
  3803. return sel_bb_head (bb) == NULL;
  3804. }
  3805. /* True when BB belongs to the current scheduling region. */
  3806. bool
  3807. in_current_region_p (basic_block bb)
  3808. {
  3809. if (bb->index < NUM_FIXED_BLOCKS)
  3810. return false;
  3811. return CONTAINING_RGN (bb->index) == CONTAINING_RGN (BB_TO_BLOCK (0));
  3812. }
  3813. /* Return the block which is a fallthru bb of a conditional jump JUMP. */
  3814. basic_block
  3815. fallthru_bb_of_jump (const rtx_insn *jump)
  3816. {
  3817. if (!JUMP_P (jump))
  3818. return NULL;
  3819. if (!any_condjump_p (jump))
  3820. return NULL;
  3821. /* A basic block that ends with a conditional jump may still have one successor
  3822. (and be followed by a barrier), we are not interested. */
  3823. if (single_succ_p (BLOCK_FOR_INSN (jump)))
  3824. return NULL;
  3825. return FALLTHRU_EDGE (BLOCK_FOR_INSN (jump))->dest;
  3826. }
  3827. /* Remove all notes from BB. */
  3828. static void
  3829. init_bb (basic_block bb)
  3830. {
  3831. remove_notes (bb_note (bb), BB_END (bb));
  3832. BB_NOTE_LIST (bb) = note_list;
  3833. }
  3834. void
  3835. sel_init_bbs (bb_vec_t bbs)
  3836. {
  3837. const struct sched_scan_info_def ssi =
  3838. {
  3839. extend_bb_info, /* extend_bb */
  3840. init_bb, /* init_bb */
  3841. NULL, /* extend_insn */
  3842. NULL /* init_insn */
  3843. };
  3844. sched_scan (&ssi, bbs);
  3845. }
  3846. /* Restore notes for the whole region. */
  3847. static void
  3848. sel_restore_notes (void)
  3849. {
  3850. int bb;
  3851. insn_t insn;
  3852. for (bb = 0; bb < current_nr_blocks; bb++)
  3853. {
  3854. basic_block first, last;
  3855. first = EBB_FIRST_BB (bb);
  3856. last = EBB_LAST_BB (bb)->next_bb;
  3857. do
  3858. {
  3859. note_list = BB_NOTE_LIST (first);
  3860. restore_other_notes (NULL, first);
  3861. BB_NOTE_LIST (first) = NULL;
  3862. FOR_BB_INSNS (first, insn)
  3863. if (NONDEBUG_INSN_P (insn))
  3864. reemit_notes (insn);
  3865. first = first->next_bb;
  3866. }
  3867. while (first != last);
  3868. }
  3869. }
  3870. /* Free per-bb data structures. */
  3871. void
  3872. sel_finish_bbs (void)
  3873. {
  3874. sel_restore_notes ();
  3875. /* Remove current loop preheader from this loop. */
  3876. if (current_loop_nest)
  3877. sel_remove_loop_preheader ();
  3878. finish_region_bb_info ();
  3879. }
  3880. /* Return true if INSN has a single successor of type FLAGS. */
  3881. bool
  3882. sel_insn_has_single_succ_p (insn_t insn, int flags)
  3883. {
  3884. insn_t succ;
  3885. succ_iterator si;
  3886. bool first_p = true;
  3887. FOR_EACH_SUCC_1 (succ, si, insn, flags)
  3888. {
  3889. if (first_p)
  3890. first_p = false;
  3891. else
  3892. return false;
  3893. }
  3894. return true;
  3895. }
  3896. /* Allocate successor's info. */
  3897. static struct succs_info *
  3898. alloc_succs_info (void)
  3899. {
  3900. if (succs_info_pool.top == succs_info_pool.max_top)
  3901. {
  3902. int i;
  3903. if (++succs_info_pool.max_top >= succs_info_pool.size)
  3904. gcc_unreachable ();
  3905. i = ++succs_info_pool.top;
  3906. succs_info_pool.stack[i].succs_ok.create (10);
  3907. succs_info_pool.stack[i].succs_other.create (10);
  3908. succs_info_pool.stack[i].probs_ok.create (10);
  3909. }
  3910. else
  3911. succs_info_pool.top++;
  3912. return &succs_info_pool.stack[succs_info_pool.top];
  3913. }
  3914. /* Free successor's info. */
  3915. void
  3916. free_succs_info (struct succs_info * sinfo)
  3917. {
  3918. gcc_assert (succs_info_pool.top >= 0
  3919. && &succs_info_pool.stack[succs_info_pool.top] == sinfo);
  3920. succs_info_pool.top--;
  3921. /* Clear stale info. */
  3922. sinfo->succs_ok.block_remove (0, sinfo->succs_ok.length ());
  3923. sinfo->succs_other.block_remove (0, sinfo->succs_other.length ());
  3924. sinfo->probs_ok.block_remove (0, sinfo->probs_ok.length ());
  3925. sinfo->all_prob = 0;
  3926. sinfo->succs_ok_n = 0;
  3927. sinfo->all_succs_n = 0;
  3928. }
  3929. /* Compute successor info for INSN. FLAGS are the flags passed
  3930. to the FOR_EACH_SUCC_1 iterator. */
  3931. struct succs_info *
  3932. compute_succs_info (insn_t insn, short flags)
  3933. {
  3934. succ_iterator si;
  3935. insn_t succ;
  3936. struct succs_info *sinfo = alloc_succs_info ();
  3937. /* Traverse *all* successors and decide what to do with each. */
  3938. FOR_EACH_SUCC_1 (succ, si, insn, SUCCS_ALL)
  3939. {
  3940. /* FIXME: this doesn't work for skipping to loop exits, as we don't
  3941. perform code motion through inner loops. */
  3942. short current_flags = si.current_flags & ~SUCCS_SKIP_TO_LOOP_EXITS;
  3943. if (current_flags & flags)
  3944. {
  3945. sinfo->succs_ok.safe_push (succ);
  3946. sinfo->probs_ok.safe_push (
  3947. /* FIXME: Improve calculation when skipping
  3948. inner loop to exits. */
  3949. si.bb_end ? si.e1->probability : REG_BR_PROB_BASE);
  3950. sinfo->succs_ok_n++;
  3951. }
  3952. else
  3953. sinfo->succs_other.safe_push (succ);
  3954. /* Compute all_prob. */
  3955. if (!si.bb_end)
  3956. sinfo->all_prob = REG_BR_PROB_BASE;
  3957. else
  3958. sinfo->all_prob += si.e1->probability;
  3959. sinfo->all_succs_n++;
  3960. }
  3961. return sinfo;
  3962. }
  3963. /* Return the predecessors of BB in PREDS and their number in N.
  3964. Empty blocks are skipped. SIZE is used to allocate PREDS. */
  3965. static void
  3966. cfg_preds_1 (basic_block bb, insn_t **preds, int *n, int *size)
  3967. {
  3968. edge e;
  3969. edge_iterator ei;
  3970. gcc_assert (BLOCK_TO_BB (bb->index) != 0);
  3971. FOR_EACH_EDGE (e, ei, bb->preds)
  3972. {
  3973. basic_block pred_bb = e->src;
  3974. insn_t bb_end = BB_END (pred_bb);
  3975. if (!in_current_region_p (pred_bb))
  3976. {
  3977. gcc_assert (flag_sel_sched_pipelining_outer_loops
  3978. && current_loop_nest);
  3979. continue;
  3980. }
  3981. if (sel_bb_empty_p (pred_bb))
  3982. cfg_preds_1 (pred_bb, preds, n, size);
  3983. else
  3984. {
  3985. if (*n == *size)
  3986. *preds = XRESIZEVEC (insn_t, *preds,
  3987. (*size = 2 * *size + 1));
  3988. (*preds)[(*n)++] = bb_end;
  3989. }
  3990. }
  3991. gcc_assert (*n != 0
  3992. || (flag_sel_sched_pipelining_outer_loops
  3993. && current_loop_nest));
  3994. }
  3995. /* Find all predecessors of BB and record them in PREDS and their number
  3996. in N. Empty blocks are skipped, and only normal (forward in-region)
  3997. edges are processed. */
  3998. static void
  3999. cfg_preds (basic_block bb, insn_t **preds, int *n)
  4000. {
  4001. int size = 0;
  4002. *preds = NULL;
  4003. *n = 0;
  4004. cfg_preds_1 (bb, preds, n, &size);
  4005. }
  4006. /* Returns true if we are moving INSN through join point. */
  4007. bool
  4008. sel_num_cfg_preds_gt_1 (insn_t insn)
  4009. {
  4010. basic_block bb;
  4011. if (!sel_bb_head_p (insn) || INSN_BB (insn) == 0)
  4012. return false;
  4013. bb = BLOCK_FOR_INSN (insn);
  4014. while (1)
  4015. {
  4016. if (EDGE_COUNT (bb->preds) > 1)
  4017. return true;
  4018. gcc_assert (EDGE_PRED (bb, 0)->dest == bb);
  4019. bb = EDGE_PRED (bb, 0)->src;
  4020. if (!sel_bb_empty_p (bb))
  4021. break;
  4022. }
  4023. return false;
  4024. }
  4025. /* Returns true when BB should be the end of an ebb. Adapted from the
  4026. code in sched-ebb.c. */
  4027. bool
  4028. bb_ends_ebb_p (basic_block bb)
  4029. {
  4030. basic_block next_bb = bb_next_bb (bb);
  4031. edge e;
  4032. if (next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
  4033. || bitmap_bit_p (forced_ebb_heads, next_bb->index)
  4034. || (LABEL_P (BB_HEAD (next_bb))
  4035. /* NB: LABEL_NUSES () is not maintained outside of jump.c.
  4036. Work around that. */
  4037. && !single_pred_p (next_bb)))
  4038. return true;
  4039. if (!in_current_region_p (next_bb))
  4040. return true;
  4041. e = find_fallthru_edge (bb->succs);
  4042. if (e)
  4043. {
  4044. gcc_assert (e->dest == next_bb);
  4045. return false;
  4046. }
  4047. return true;
  4048. }
  4049. /* Returns true when INSN and SUCC are in the same EBB, given that SUCC is a
  4050. successor of INSN. */
  4051. bool
  4052. in_same_ebb_p (insn_t insn, insn_t succ)
  4053. {
  4054. basic_block ptr = BLOCK_FOR_INSN (insn);
  4055. for (;;)
  4056. {
  4057. if (ptr == BLOCK_FOR_INSN (succ))
  4058. return true;
  4059. if (bb_ends_ebb_p (ptr))
  4060. return false;
  4061. ptr = bb_next_bb (ptr);
  4062. }
  4063. gcc_unreachable ();
  4064. return false;
  4065. }
  4066. /* Recomputes the reverse topological order for the function and
  4067. saves it in REV_TOP_ORDER_INDEX. REV_TOP_ORDER_INDEX_LEN is also
  4068. modified appropriately. */
  4069. static void
  4070. recompute_rev_top_order (void)
  4071. {
  4072. int *postorder;
  4073. int n_blocks, i;
  4074. if (!rev_top_order_index
  4075. || rev_top_order_index_len < last_basic_block_for_fn (cfun))
  4076. {
  4077. rev_top_order_index_len = last_basic_block_for_fn (cfun);
  4078. rev_top_order_index = XRESIZEVEC (int, rev_top_order_index,
  4079. rev_top_order_index_len);
  4080. }
  4081. postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
  4082. n_blocks = post_order_compute (postorder, true, false);
  4083. gcc_assert (n_basic_blocks_for_fn (cfun) == n_blocks);
  4084. /* Build reverse function: for each basic block with BB->INDEX == K
  4085. rev_top_order_index[K] is it's reverse topological sort number. */
  4086. for (i = 0; i < n_blocks; i++)
  4087. {
  4088. gcc_assert (postorder[i] < rev_top_order_index_len);
  4089. rev_top_order_index[postorder[i]] = i;
  4090. }
  4091. free (postorder);
  4092. }
  4093. /* Clear all flags from insns in BB that could spoil its rescheduling. */
  4094. void
  4095. clear_outdated_rtx_info (basic_block bb)
  4096. {
  4097. rtx_insn *insn;
  4098. FOR_BB_INSNS (bb, insn)
  4099. if (INSN_P (insn))
  4100. {
  4101. SCHED_GROUP_P (insn) = 0;
  4102. INSN_AFTER_STALL_P (insn) = 0;
  4103. INSN_SCHED_TIMES (insn) = 0;
  4104. EXPR_PRIORITY_ADJ (INSN_EXPR (insn)) = 0;
  4105. /* We cannot use the changed caches, as previously we could ignore
  4106. the LHS dependence due to enabled renaming and transform
  4107. the expression, and currently we'll be unable to do this. */
  4108. htab_empty (INSN_TRANSFORMED_INSNS (insn));
  4109. }
  4110. }
  4111. /* Add BB_NOTE to the pool of available basic block notes. */
  4112. static void
  4113. return_bb_to_pool (basic_block bb)
  4114. {
  4115. rtx note = bb_note (bb);
  4116. gcc_assert (NOTE_BASIC_BLOCK (note) == bb
  4117. && bb->aux == NULL);
  4118. /* It turns out that current cfg infrastructure does not support
  4119. reuse of basic blocks. Don't bother for now. */
  4120. /*bb_note_pool.safe_push (note);*/
  4121. }
  4122. /* Get a bb_note from pool or return NULL_RTX if pool is empty. */
  4123. static rtx_note *
  4124. get_bb_note_from_pool (void)
  4125. {
  4126. if (bb_note_pool.is_empty ())
  4127. return NULL;
  4128. else
  4129. {
  4130. rtx_note *note = bb_note_pool.pop ();
  4131. SET_PREV_INSN (note) = NULL_RTX;
  4132. SET_NEXT_INSN (note) = NULL_RTX;
  4133. return note;
  4134. }
  4135. }
  4136. /* Free bb_note_pool. */
  4137. void
  4138. free_bb_note_pool (void)
  4139. {
  4140. bb_note_pool.release ();
  4141. }
  4142. /* Setup scheduler pool and successor structure. */
  4143. void
  4144. alloc_sched_pools (void)
  4145. {
  4146. int succs_size;
  4147. succs_size = MAX_WS + 1;
  4148. succs_info_pool.stack = XCNEWVEC (struct succs_info, succs_size);
  4149. succs_info_pool.size = succs_size;
  4150. succs_info_pool.top = -1;
  4151. succs_info_pool.max_top = -1;
  4152. sched_lists_pool = create_alloc_pool ("sel-sched-lists",
  4153. sizeof (struct _list_node), 500);
  4154. }
  4155. /* Free the pools. */
  4156. void
  4157. free_sched_pools (void)
  4158. {
  4159. int i;
  4160. free_alloc_pool (sched_lists_pool);
  4161. gcc_assert (succs_info_pool.top == -1);
  4162. for (i = 0; i <= succs_info_pool.max_top; i++)
  4163. {
  4164. succs_info_pool.stack[i].succs_ok.release ();
  4165. succs_info_pool.stack[i].succs_other.release ();
  4166. succs_info_pool.stack[i].probs_ok.release ();
  4167. }
  4168. free (succs_info_pool.stack);
  4169. }
  4170. /* Returns a position in RGN where BB can be inserted retaining
  4171. topological order. */
  4172. static int
  4173. find_place_to_insert_bb (basic_block bb, int rgn)
  4174. {
  4175. bool has_preds_outside_rgn = false;
  4176. edge e;
  4177. edge_iterator ei;
  4178. /* Find whether we have preds outside the region. */
  4179. FOR_EACH_EDGE (e, ei, bb->preds)
  4180. if (!in_current_region_p (e->src))
  4181. {
  4182. has_preds_outside_rgn = true;
  4183. break;
  4184. }
  4185. /* Recompute the top order -- needed when we have > 1 pred
  4186. and in case we don't have preds outside. */
  4187. if (flag_sel_sched_pipelining_outer_loops
  4188. && (has_preds_outside_rgn || EDGE_COUNT (bb->preds) > 1))
  4189. {
  4190. int i, bbi = bb->index, cur_bbi;
  4191. recompute_rev_top_order ();
  4192. for (i = RGN_NR_BLOCKS (rgn) - 1; i >= 0; i--)
  4193. {
  4194. cur_bbi = BB_TO_BLOCK (i);
  4195. if (rev_top_order_index[bbi]
  4196. < rev_top_order_index[cur_bbi])
  4197. break;
  4198. }
  4199. /* We skipped the right block, so we increase i. We accommodate
  4200. it for increasing by step later, so we decrease i. */
  4201. return (i + 1) - 1;
  4202. }
  4203. else if (has_preds_outside_rgn)
  4204. {
  4205. /* This is the case when we generate an extra empty block
  4206. to serve as region head during pipelining. */
  4207. e = EDGE_SUCC (bb, 0);
  4208. gcc_assert (EDGE_COUNT (bb->succs) == 1
  4209. && in_current_region_p (EDGE_SUCC (bb, 0)->dest)
  4210. && (BLOCK_TO_BB (e->dest->index) == 0));
  4211. return -1;
  4212. }
  4213. /* We don't have preds outside the region. We should have
  4214. the only pred, because the multiple preds case comes from
  4215. the pipelining of outer loops, and that is handled above.
  4216. Just take the bbi of this single pred. */
  4217. if (EDGE_COUNT (bb->succs) > 0)
  4218. {
  4219. int pred_bbi;
  4220. gcc_assert (EDGE_COUNT (bb->preds) == 1);
  4221. pred_bbi = EDGE_PRED (bb, 0)->src->index;
  4222. return BLOCK_TO_BB (pred_bbi);
  4223. }
  4224. else
  4225. /* BB has no successors. It is safe to put it in the end. */
  4226. return current_nr_blocks - 1;
  4227. }
  4228. /* Deletes an empty basic block freeing its data. */
  4229. static void
  4230. delete_and_free_basic_block (basic_block bb)
  4231. {
  4232. gcc_assert (sel_bb_empty_p (bb));
  4233. if (BB_LV_SET (bb))
  4234. free_lv_set (bb);
  4235. bitmap_clear_bit (blocks_to_reschedule, bb->index);
  4236. /* Can't assert av_set properties because we use sel_aremove_bb
  4237. when removing loop preheader from the region. At the point of
  4238. removing the preheader we already have deallocated sel_region_bb_info. */
  4239. gcc_assert (BB_LV_SET (bb) == NULL
  4240. && !BB_LV_SET_VALID_P (bb)
  4241. && BB_AV_LEVEL (bb) == 0
  4242. && BB_AV_SET (bb) == NULL);
  4243. delete_basic_block (bb);
  4244. }
  4245. /* Add BB to the current region and update the region data. */
  4246. static void
  4247. add_block_to_current_region (basic_block bb)
  4248. {
  4249. int i, pos, bbi = -2, rgn;
  4250. rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
  4251. bbi = find_place_to_insert_bb (bb, rgn);
  4252. bbi += 1;
  4253. pos = RGN_BLOCKS (rgn) + bbi;
  4254. gcc_assert (RGN_HAS_REAL_EBB (rgn) == 0
  4255. && ebb_head[bbi] == pos);
  4256. /* Make a place for the new block. */
  4257. extend_regions ();
  4258. for (i = RGN_BLOCKS (rgn + 1) - 1; i >= pos; i--)
  4259. BLOCK_TO_BB (rgn_bb_table[i])++;
  4260. memmove (rgn_bb_table + pos + 1,
  4261. rgn_bb_table + pos,
  4262. (RGN_BLOCKS (nr_regions) - pos) * sizeof (*rgn_bb_table));
  4263. /* Initialize data for BB. */
  4264. rgn_bb_table[pos] = bb->index;
  4265. BLOCK_TO_BB (bb->index) = bbi;
  4266. CONTAINING_RGN (bb->index) = rgn;
  4267. RGN_NR_BLOCKS (rgn)++;
  4268. for (i = rgn + 1; i <= nr_regions; i++)
  4269. RGN_BLOCKS (i)++;
  4270. }
  4271. /* Remove BB from the current region and update the region data. */
  4272. static void
  4273. remove_bb_from_region (basic_block bb)
  4274. {
  4275. int i, pos, bbi = -2, rgn;
  4276. rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
  4277. bbi = BLOCK_TO_BB (bb->index);
  4278. pos = RGN_BLOCKS (rgn) + bbi;
  4279. gcc_assert (RGN_HAS_REAL_EBB (rgn) == 0
  4280. && ebb_head[bbi] == pos);
  4281. for (i = RGN_BLOCKS (rgn + 1) - 1; i >= pos; i--)
  4282. BLOCK_TO_BB (rgn_bb_table[i])--;
  4283. memmove (rgn_bb_table + pos,
  4284. rgn_bb_table + pos + 1,
  4285. (RGN_BLOCKS (nr_regions) - pos) * sizeof (*rgn_bb_table));
  4286. RGN_NR_BLOCKS (rgn)--;
  4287. for (i = rgn + 1; i <= nr_regions; i++)
  4288. RGN_BLOCKS (i)--;
  4289. }
  4290. /* Add BB to the current region and update all data. If BB is NULL, add all
  4291. blocks from last_added_blocks vector. */
  4292. static void
  4293. sel_add_bb (basic_block bb)
  4294. {
  4295. /* Extend luids so that new notes will receive zero luids. */
  4296. sched_extend_luids ();
  4297. sched_init_bbs ();
  4298. sel_init_bbs (last_added_blocks);
  4299. /* When bb is passed explicitly, the vector should contain
  4300. the only element that equals to bb; otherwise, the vector
  4301. should not be NULL. */
  4302. gcc_assert (last_added_blocks.exists ());
  4303. if (bb != NULL)
  4304. {
  4305. gcc_assert (last_added_blocks.length () == 1
  4306. && last_added_blocks[0] == bb);
  4307. add_block_to_current_region (bb);
  4308. /* We associate creating/deleting data sets with the first insn
  4309. appearing / disappearing in the bb. */
  4310. if (!sel_bb_empty_p (bb) && BB_LV_SET (bb) == NULL)
  4311. create_initial_data_sets (bb);
  4312. last_added_blocks.release ();
  4313. }
  4314. else
  4315. /* BB is NULL - process LAST_ADDED_BLOCKS instead. */
  4316. {
  4317. int i;
  4318. basic_block temp_bb = NULL;
  4319. for (i = 0;
  4320. last_added_blocks.iterate (i, &bb); i++)
  4321. {
  4322. add_block_to_current_region (bb);
  4323. temp_bb = bb;
  4324. }
  4325. /* We need to fetch at least one bb so we know the region
  4326. to update. */
  4327. gcc_assert (temp_bb != NULL);
  4328. bb = temp_bb;
  4329. last_added_blocks.release ();
  4330. }
  4331. rgn_setup_region (CONTAINING_RGN (bb->index));
  4332. }
  4333. /* Remove BB from the current region and update all data.
  4334. If REMOVE_FROM_CFG_PBB is true, also remove the block cfom cfg. */
  4335. static void
  4336. sel_remove_bb (basic_block bb, bool remove_from_cfg_p)
  4337. {
  4338. unsigned idx = bb->index;
  4339. gcc_assert (bb != NULL && BB_NOTE_LIST (bb) == NULL_RTX);
  4340. remove_bb_from_region (bb);
  4341. return_bb_to_pool (bb);
  4342. bitmap_clear_bit (blocks_to_reschedule, idx);
  4343. if (remove_from_cfg_p)
  4344. {
  4345. basic_block succ = single_succ (bb);
  4346. delete_and_free_basic_block (bb);
  4347. set_immediate_dominator (CDI_DOMINATORS, succ,
  4348. recompute_dominator (CDI_DOMINATORS, succ));
  4349. }
  4350. rgn_setup_region (CONTAINING_RGN (idx));
  4351. }
  4352. /* Concatenate info of EMPTY_BB to info of MERGE_BB. */
  4353. static void
  4354. move_bb_info (basic_block merge_bb, basic_block empty_bb)
  4355. {
  4356. if (in_current_region_p (merge_bb))
  4357. concat_note_lists (BB_NOTE_LIST (empty_bb),
  4358. &BB_NOTE_LIST (merge_bb));
  4359. BB_NOTE_LIST (empty_bb) = NULL;
  4360. }
  4361. /* Remove EMPTY_BB. If REMOVE_FROM_CFG_P is false, remove EMPTY_BB from
  4362. region, but keep it in CFG. */
  4363. static void
  4364. remove_empty_bb (basic_block empty_bb, bool remove_from_cfg_p)
  4365. {
  4366. /* The block should contain just a note or a label.
  4367. We try to check whether it is unused below. */
  4368. gcc_assert (BB_HEAD (empty_bb) == BB_END (empty_bb)
  4369. || LABEL_P (BB_HEAD (empty_bb)));
  4370. /* If basic block has predecessors or successors, redirect them. */
  4371. if (remove_from_cfg_p
  4372. && (EDGE_COUNT (empty_bb->preds) > 0
  4373. || EDGE_COUNT (empty_bb->succs) > 0))
  4374. {
  4375. basic_block pred;
  4376. basic_block succ;
  4377. /* We need to init PRED and SUCC before redirecting edges. */
  4378. if (EDGE_COUNT (empty_bb->preds) > 0)
  4379. {
  4380. edge e;
  4381. gcc_assert (EDGE_COUNT (empty_bb->preds) == 1);
  4382. e = EDGE_PRED (empty_bb, 0);
  4383. gcc_assert (e->src == empty_bb->prev_bb
  4384. && (e->flags & EDGE_FALLTHRU));
  4385. pred = empty_bb->prev_bb;
  4386. }
  4387. else
  4388. pred = NULL;
  4389. if (EDGE_COUNT (empty_bb->succs) > 0)
  4390. {
  4391. /* We do not check fallthruness here as above, because
  4392. after removing a jump the edge may actually be not fallthru. */
  4393. gcc_assert (EDGE_COUNT (empty_bb->succs) == 1);
  4394. succ = EDGE_SUCC (empty_bb, 0)->dest;
  4395. }
  4396. else
  4397. succ = NULL;
  4398. if (EDGE_COUNT (empty_bb->preds) > 0 && succ != NULL)
  4399. {
  4400. edge e = EDGE_PRED (empty_bb, 0);
  4401. if (e->flags & EDGE_FALLTHRU)
  4402. redirect_edge_succ_nodup (e, succ);
  4403. else
  4404. sel_redirect_edge_and_branch (EDGE_PRED (empty_bb, 0), succ);
  4405. }
  4406. if (EDGE_COUNT (empty_bb->succs) > 0 && pred != NULL)
  4407. {
  4408. edge e = EDGE_SUCC (empty_bb, 0);
  4409. if (find_edge (pred, e->dest) == NULL)
  4410. redirect_edge_pred (e, pred);
  4411. }
  4412. }
  4413. /* Finish removing. */
  4414. sel_remove_bb (empty_bb, remove_from_cfg_p);
  4415. }
  4416. /* An implementation of create_basic_block hook, which additionally updates
  4417. per-bb data structures. */
  4418. static basic_block
  4419. sel_create_basic_block (void *headp, void *endp, basic_block after)
  4420. {
  4421. basic_block new_bb;
  4422. rtx_note *new_bb_note;
  4423. gcc_assert (flag_sel_sched_pipelining_outer_loops
  4424. || !last_added_blocks.exists ());
  4425. new_bb_note = get_bb_note_from_pool ();
  4426. if (new_bb_note == NULL_RTX)
  4427. new_bb = orig_cfg_hooks.create_basic_block (headp, endp, after);
  4428. else
  4429. {
  4430. new_bb = create_basic_block_structure ((rtx_insn *) headp,
  4431. (rtx_insn *) endp,
  4432. new_bb_note, after);
  4433. new_bb->aux = NULL;
  4434. }
  4435. last_added_blocks.safe_push (new_bb);
  4436. return new_bb;
  4437. }
  4438. /* Implement sched_init_only_bb (). */
  4439. static void
  4440. sel_init_only_bb (basic_block bb, basic_block after)
  4441. {
  4442. gcc_assert (after == NULL);
  4443. extend_regions ();
  4444. rgn_make_new_region_out_of_new_block (bb);
  4445. }
  4446. /* Update the latch when we've splitted or merged it from FROM block to TO.
  4447. This should be checked for all outer loops, too. */
  4448. static void
  4449. change_loops_latches (basic_block from, basic_block to)
  4450. {
  4451. gcc_assert (from != to);
  4452. if (current_loop_nest)
  4453. {
  4454. struct loop *loop;
  4455. for (loop = current_loop_nest; loop; loop = loop_outer (loop))
  4456. if (considered_for_pipelining_p (loop) && loop->latch == from)
  4457. {
  4458. gcc_assert (loop == current_loop_nest);
  4459. loop->latch = to;
  4460. gcc_assert (loop_latch_edge (loop));
  4461. }
  4462. }
  4463. }
  4464. /* Splits BB on two basic blocks, adding it to the region and extending
  4465. per-bb data structures. Returns the newly created bb. */
  4466. static basic_block
  4467. sel_split_block (basic_block bb, rtx after)
  4468. {
  4469. basic_block new_bb;
  4470. insn_t insn;
  4471. new_bb = sched_split_block_1 (bb, after);
  4472. sel_add_bb (new_bb);
  4473. /* This should be called after sel_add_bb, because this uses
  4474. CONTAINING_RGN for the new block, which is not yet initialized.
  4475. FIXME: this function may be a no-op now. */
  4476. change_loops_latches (bb, new_bb);
  4477. /* Update ORIG_BB_INDEX for insns moved into the new block. */
  4478. FOR_BB_INSNS (new_bb, insn)
  4479. if (INSN_P (insn))
  4480. EXPR_ORIG_BB_INDEX (INSN_EXPR (insn)) = new_bb->index;
  4481. if (sel_bb_empty_p (bb))
  4482. {
  4483. gcc_assert (!sel_bb_empty_p (new_bb));
  4484. /* NEW_BB has data sets that need to be updated and BB holds
  4485. data sets that should be removed. Exchange these data sets
  4486. so that we won't lose BB's valid data sets. */
  4487. exchange_data_sets (new_bb, bb);
  4488. free_data_sets (bb);
  4489. }
  4490. if (!sel_bb_empty_p (new_bb)
  4491. && bitmap_bit_p (blocks_to_reschedule, bb->index))
  4492. bitmap_set_bit (blocks_to_reschedule, new_bb->index);
  4493. return new_bb;
  4494. }
  4495. /* If BB ends with a jump insn whose ID is bigger then PREV_MAX_UID, return it.
  4496. Otherwise returns NULL. */
  4497. static rtx_insn *
  4498. check_for_new_jump (basic_block bb, int prev_max_uid)
  4499. {
  4500. rtx_insn *end;
  4501. end = sel_bb_end (bb);
  4502. if (end && INSN_UID (end) >= prev_max_uid)
  4503. return end;
  4504. return NULL;
  4505. }
  4506. /* Look for a new jump either in FROM_BB block or in newly created JUMP_BB block.
  4507. New means having UID at least equal to PREV_MAX_UID. */
  4508. static rtx_insn *
  4509. find_new_jump (basic_block from, basic_block jump_bb, int prev_max_uid)
  4510. {
  4511. rtx_insn *jump;
  4512. /* Return immediately if no new insns were emitted. */
  4513. if (get_max_uid () == prev_max_uid)
  4514. return NULL;
  4515. /* Now check both blocks for new jumps. It will ever be only one. */
  4516. if ((jump = check_for_new_jump (from, prev_max_uid)))
  4517. return jump;
  4518. if (jump_bb != NULL
  4519. && (jump = check_for_new_jump (jump_bb, prev_max_uid)))
  4520. return jump;
  4521. return NULL;
  4522. }
  4523. /* Splits E and adds the newly created basic block to the current region.
  4524. Returns this basic block. */
  4525. basic_block
  4526. sel_split_edge (edge e)
  4527. {
  4528. basic_block new_bb, src, other_bb = NULL;
  4529. int prev_max_uid;
  4530. rtx_insn *jump;
  4531. src = e->src;
  4532. prev_max_uid = get_max_uid ();
  4533. new_bb = split_edge (e);
  4534. if (flag_sel_sched_pipelining_outer_loops
  4535. && current_loop_nest)
  4536. {
  4537. int i;
  4538. basic_block bb;
  4539. /* Some of the basic blocks might not have been added to the loop.
  4540. Add them here, until this is fixed in force_fallthru. */
  4541. for (i = 0;
  4542. last_added_blocks.iterate (i, &bb); i++)
  4543. if (!bb->loop_father)
  4544. {
  4545. add_bb_to_loop (bb, e->dest->loop_father);
  4546. gcc_assert (!other_bb && (new_bb->index != bb->index));
  4547. other_bb = bb;
  4548. }
  4549. }
  4550. /* Add all last_added_blocks to the region. */
  4551. sel_add_bb (NULL);
  4552. jump = find_new_jump (src, new_bb, prev_max_uid);
  4553. if (jump)
  4554. sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
  4555. /* Put the correct lv set on this block. */
  4556. if (other_bb && !sel_bb_empty_p (other_bb))
  4557. compute_live (sel_bb_head (other_bb));
  4558. return new_bb;
  4559. }
  4560. /* Implement sched_create_empty_bb (). */
  4561. static basic_block
  4562. sel_create_empty_bb (basic_block after)
  4563. {
  4564. basic_block new_bb;
  4565. new_bb = sched_create_empty_bb_1 (after);
  4566. /* We'll explicitly initialize NEW_BB via sel_init_only_bb () a bit
  4567. later. */
  4568. gcc_assert (last_added_blocks.length () == 1
  4569. && last_added_blocks[0] == new_bb);
  4570. last_added_blocks.release ();
  4571. return new_bb;
  4572. }
  4573. /* Implement sched_create_recovery_block. ORIG_INSN is where block
  4574. will be splitted to insert a check. */
  4575. basic_block
  4576. sel_create_recovery_block (insn_t orig_insn)
  4577. {
  4578. basic_block first_bb, second_bb, recovery_block;
  4579. basic_block before_recovery = NULL;
  4580. rtx_insn *jump;
  4581. first_bb = BLOCK_FOR_INSN (orig_insn);
  4582. if (sel_bb_end_p (orig_insn))
  4583. {
  4584. /* Avoid introducing an empty block while splitting. */
  4585. gcc_assert (single_succ_p (first_bb));
  4586. second_bb = single_succ (first_bb);
  4587. }
  4588. else
  4589. second_bb = sched_split_block (first_bb, orig_insn);
  4590. recovery_block = sched_create_recovery_block (&before_recovery);
  4591. if (before_recovery)
  4592. copy_lv_set_from (before_recovery, EXIT_BLOCK_PTR_FOR_FN (cfun));
  4593. gcc_assert (sel_bb_empty_p (recovery_block));
  4594. sched_create_recovery_edges (first_bb, recovery_block, second_bb);
  4595. if (current_loops != NULL)
  4596. add_bb_to_loop (recovery_block, first_bb->loop_father);
  4597. sel_add_bb (recovery_block);
  4598. jump = BB_END (recovery_block);
  4599. gcc_assert (sel_bb_head (recovery_block) == jump);
  4600. sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP);
  4601. return recovery_block;
  4602. }
  4603. /* Merge basic block B into basic block A. */
  4604. static void
  4605. sel_merge_blocks (basic_block a, basic_block b)
  4606. {
  4607. gcc_assert (sel_bb_empty_p (b)
  4608. && EDGE_COUNT (b->preds) == 1
  4609. && EDGE_PRED (b, 0)->src == b->prev_bb);
  4610. move_bb_info (b->prev_bb, b);
  4611. remove_empty_bb (b, false);
  4612. merge_blocks (a, b);
  4613. change_loops_latches (b, a);
  4614. }
  4615. /* A wrapper for redirect_edge_and_branch_force, which also initializes
  4616. data structures for possibly created bb and insns. */
  4617. void
  4618. sel_redirect_edge_and_branch_force (edge e, basic_block to)
  4619. {
  4620. basic_block jump_bb, src, orig_dest = e->dest;
  4621. int prev_max_uid;
  4622. rtx_insn *jump;
  4623. int old_seqno = -1;
  4624. /* This function is now used only for bookkeeping code creation, where
  4625. we'll never get the single pred of orig_dest block and thus will not
  4626. hit unreachable blocks when updating dominator info. */
  4627. gcc_assert (!sel_bb_empty_p (e->src)
  4628. && !single_pred_p (orig_dest));
  4629. src = e->src;
  4630. prev_max_uid = get_max_uid ();
  4631. /* Compute and pass old_seqno down to sel_init_new_insn only for the case
  4632. when the conditional jump being redirected may become unconditional. */
  4633. if (any_condjump_p (BB_END (src))
  4634. && INSN_SEQNO (BB_END (src)) >= 0)
  4635. old_seqno = INSN_SEQNO (BB_END (src));
  4636. jump_bb = redirect_edge_and_branch_force (e, to);
  4637. if (jump_bb != NULL)
  4638. sel_add_bb (jump_bb);
  4639. /* This function could not be used to spoil the loop structure by now,
  4640. thus we don't care to update anything. But check it to be sure. */
  4641. if (current_loop_nest
  4642. && pipelining_p)
  4643. gcc_assert (loop_latch_edge (current_loop_nest));
  4644. jump = find_new_jump (src, jump_bb, prev_max_uid);
  4645. if (jump)
  4646. sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP,
  4647. old_seqno);
  4648. set_immediate_dominator (CDI_DOMINATORS, to,
  4649. recompute_dominator (CDI_DOMINATORS, to));
  4650. set_immediate_dominator (CDI_DOMINATORS, orig_dest,
  4651. recompute_dominator (CDI_DOMINATORS, orig_dest));
  4652. }
  4653. /* A wrapper for redirect_edge_and_branch. Return TRUE if blocks connected by
  4654. redirected edge are in reverse topological order. */
  4655. bool
  4656. sel_redirect_edge_and_branch (edge e, basic_block to)
  4657. {
  4658. bool latch_edge_p;
  4659. basic_block src, orig_dest = e->dest;
  4660. int prev_max_uid;
  4661. rtx_insn *jump;
  4662. edge redirected;
  4663. bool recompute_toporder_p = false;
  4664. bool maybe_unreachable = single_pred_p (orig_dest);
  4665. int old_seqno = -1;
  4666. latch_edge_p = (pipelining_p
  4667. && current_loop_nest
  4668. && e == loop_latch_edge (current_loop_nest));
  4669. src = e->src;
  4670. prev_max_uid = get_max_uid ();
  4671. /* Compute and pass old_seqno down to sel_init_new_insn only for the case
  4672. when the conditional jump being redirected may become unconditional. */
  4673. if (any_condjump_p (BB_END (src))
  4674. && INSN_SEQNO (BB_END (src)) >= 0)
  4675. old_seqno = INSN_SEQNO (BB_END (src));
  4676. redirected = redirect_edge_and_branch (e, to);
  4677. gcc_assert (redirected && !last_added_blocks.exists ());
  4678. /* When we've redirected a latch edge, update the header. */
  4679. if (latch_edge_p)
  4680. {
  4681. current_loop_nest->header = to;
  4682. gcc_assert (loop_latch_edge (current_loop_nest));
  4683. }
  4684. /* In rare situations, the topological relation between the blocks connected
  4685. by the redirected edge can change (see PR42245 for an example). Update
  4686. block_to_bb/bb_to_block. */
  4687. if (CONTAINING_RGN (e->src->index) == CONTAINING_RGN (to->index)
  4688. && BLOCK_TO_BB (e->src->index) > BLOCK_TO_BB (to->index))
  4689. recompute_toporder_p = true;
  4690. jump = find_new_jump (src, NULL, prev_max_uid);
  4691. if (jump)
  4692. sel_init_new_insn (jump, INSN_INIT_TODO_LUID | INSN_INIT_TODO_SIMPLEJUMP, old_seqno);
  4693. /* Only update dominator info when we don't have unreachable blocks.
  4694. Otherwise we'll update in maybe_tidy_empty_bb. */
  4695. if (!maybe_unreachable)
  4696. {
  4697. set_immediate_dominator (CDI_DOMINATORS, to,
  4698. recompute_dominator (CDI_DOMINATORS, to));
  4699. set_immediate_dominator (CDI_DOMINATORS, orig_dest,
  4700. recompute_dominator (CDI_DOMINATORS, orig_dest));
  4701. }
  4702. return recompute_toporder_p;
  4703. }
  4704. /* This variable holds the cfg hooks used by the selective scheduler. */
  4705. static struct cfg_hooks sel_cfg_hooks;
  4706. /* Register sel-sched cfg hooks. */
  4707. void
  4708. sel_register_cfg_hooks (void)
  4709. {
  4710. sched_split_block = sel_split_block;
  4711. orig_cfg_hooks = get_cfg_hooks ();
  4712. sel_cfg_hooks = orig_cfg_hooks;
  4713. sel_cfg_hooks.create_basic_block = sel_create_basic_block;
  4714. set_cfg_hooks (sel_cfg_hooks);
  4715. sched_init_only_bb = sel_init_only_bb;
  4716. sched_split_block = sel_split_block;
  4717. sched_create_empty_bb = sel_create_empty_bb;
  4718. }
  4719. /* Unregister sel-sched cfg hooks. */
  4720. void
  4721. sel_unregister_cfg_hooks (void)
  4722. {
  4723. sched_create_empty_bb = NULL;
  4724. sched_split_block = NULL;
  4725. sched_init_only_bb = NULL;
  4726. set_cfg_hooks (orig_cfg_hooks);
  4727. }
  4728. /* Emit an insn rtx based on PATTERN. If a jump insn is wanted,
  4729. LABEL is where this jump should be directed. */
  4730. rtx_insn *
  4731. create_insn_rtx_from_pattern (rtx pattern, rtx label)
  4732. {
  4733. rtx_insn *insn_rtx;
  4734. gcc_assert (!INSN_P (pattern));
  4735. start_sequence ();
  4736. if (label == NULL_RTX)
  4737. insn_rtx = emit_insn (pattern);
  4738. else if (DEBUG_INSN_P (label))
  4739. insn_rtx = emit_debug_insn (pattern);
  4740. else
  4741. {
  4742. insn_rtx = emit_jump_insn (pattern);
  4743. JUMP_LABEL (insn_rtx) = label;
  4744. ++LABEL_NUSES (label);
  4745. }
  4746. end_sequence ();
  4747. sched_extend_luids ();
  4748. sched_extend_target ();
  4749. sched_deps_init (false);
  4750. /* Initialize INSN_CODE now. */
  4751. recog_memoized (insn_rtx);
  4752. return insn_rtx;
  4753. }
  4754. /* Create a new vinsn for INSN_RTX. FORCE_UNIQUE_P is true when the vinsn
  4755. must not be clonable. */
  4756. vinsn_t
  4757. create_vinsn_from_insn_rtx (rtx_insn *insn_rtx, bool force_unique_p)
  4758. {
  4759. gcc_assert (INSN_P (insn_rtx) && !INSN_IN_STREAM_P (insn_rtx));
  4760. /* If VINSN_TYPE is not USE, retain its uniqueness. */
  4761. return vinsn_create (insn_rtx, force_unique_p);
  4762. }
  4763. /* Create a copy of INSN_RTX. */
  4764. rtx_insn *
  4765. create_copy_of_insn_rtx (rtx insn_rtx)
  4766. {
  4767. rtx_insn *res;
  4768. rtx link;
  4769. if (DEBUG_INSN_P (insn_rtx))
  4770. return create_insn_rtx_from_pattern (copy_rtx (PATTERN (insn_rtx)),
  4771. insn_rtx);
  4772. gcc_assert (NONJUMP_INSN_P (insn_rtx));
  4773. res = create_insn_rtx_from_pattern (copy_rtx (PATTERN (insn_rtx)),
  4774. NULL_RTX);
  4775. /* Copy all REG_NOTES except REG_EQUAL/REG_EQUIV and REG_LABEL_OPERAND
  4776. since mark_jump_label will make them. REG_LABEL_TARGETs are created
  4777. there too, but are supposed to be sticky, so we copy them. */
  4778. for (link = REG_NOTES (insn_rtx); link; link = XEXP (link, 1))
  4779. if (REG_NOTE_KIND (link) != REG_LABEL_OPERAND
  4780. && REG_NOTE_KIND (link) != REG_EQUAL
  4781. && REG_NOTE_KIND (link) != REG_EQUIV)
  4782. {
  4783. if (GET_CODE (link) == EXPR_LIST)
  4784. add_reg_note (res, REG_NOTE_KIND (link),
  4785. copy_insn_1 (XEXP (link, 0)));
  4786. else
  4787. add_reg_note (res, REG_NOTE_KIND (link), XEXP (link, 0));
  4788. }
  4789. return res;
  4790. }
  4791. /* Change vinsn field of EXPR to hold NEW_VINSN. */
  4792. void
  4793. change_vinsn_in_expr (expr_t expr, vinsn_t new_vinsn)
  4794. {
  4795. vinsn_detach (EXPR_VINSN (expr));
  4796. EXPR_VINSN (expr) = new_vinsn;
  4797. vinsn_attach (new_vinsn);
  4798. }
  4799. /* Helpers for global init. */
  4800. /* This structure is used to be able to call existing bundling mechanism
  4801. and calculate insn priorities. */
  4802. static struct haifa_sched_info sched_sel_haifa_sched_info =
  4803. {
  4804. NULL, /* init_ready_list */
  4805. NULL, /* can_schedule_ready_p */
  4806. NULL, /* schedule_more_p */
  4807. NULL, /* new_ready */
  4808. NULL, /* rgn_rank */
  4809. sel_print_insn, /* rgn_print_insn */
  4810. contributes_to_priority,
  4811. NULL, /* insn_finishes_block_p */
  4812. NULL, NULL,
  4813. NULL, NULL,
  4814. 0, 0,
  4815. NULL, /* add_remove_insn */
  4816. NULL, /* begin_schedule_ready */
  4817. NULL, /* begin_move_insn */
  4818. NULL, /* advance_target_bb */
  4819. NULL,
  4820. NULL,
  4821. SEL_SCHED | NEW_BBS
  4822. };
  4823. /* Setup special insns used in the scheduler. */
  4824. void
  4825. setup_nop_and_exit_insns (void)
  4826. {
  4827. gcc_assert (nop_pattern == NULL_RTX
  4828. && exit_insn == NULL_RTX);
  4829. nop_pattern = constm1_rtx;
  4830. start_sequence ();
  4831. emit_insn (nop_pattern);
  4832. exit_insn = get_insns ();
  4833. end_sequence ();
  4834. set_block_for_insn (exit_insn, EXIT_BLOCK_PTR_FOR_FN (cfun));
  4835. }
  4836. /* Free special insns used in the scheduler. */
  4837. void
  4838. free_nop_and_exit_insns (void)
  4839. {
  4840. exit_insn = NULL;
  4841. nop_pattern = NULL_RTX;
  4842. }
  4843. /* Setup a special vinsn used in new insns initialization. */
  4844. void
  4845. setup_nop_vinsn (void)
  4846. {
  4847. nop_vinsn = vinsn_create (exit_insn, false);
  4848. vinsn_attach (nop_vinsn);
  4849. }
  4850. /* Free a special vinsn used in new insns initialization. */
  4851. void
  4852. free_nop_vinsn (void)
  4853. {
  4854. gcc_assert (VINSN_COUNT (nop_vinsn) == 1);
  4855. vinsn_detach (nop_vinsn);
  4856. nop_vinsn = NULL;
  4857. }
  4858. /* Call a set_sched_flags hook. */
  4859. void
  4860. sel_set_sched_flags (void)
  4861. {
  4862. /* ??? This means that set_sched_flags were called, and we decided to
  4863. support speculation. However, set_sched_flags also modifies flags
  4864. on current_sched_info, doing this only at global init. And we
  4865. sometimes change c_s_i later. So put the correct flags again. */
  4866. if (spec_info && targetm.sched.set_sched_flags)
  4867. targetm.sched.set_sched_flags (spec_info);
  4868. }
  4869. /* Setup pointers to global sched info structures. */
  4870. void
  4871. sel_setup_sched_infos (void)
  4872. {
  4873. rgn_setup_common_sched_info ();
  4874. memcpy (&sel_common_sched_info, common_sched_info,
  4875. sizeof (sel_common_sched_info));
  4876. sel_common_sched_info.fix_recovery_cfg = NULL;
  4877. sel_common_sched_info.add_block = NULL;
  4878. sel_common_sched_info.estimate_number_of_insns
  4879. = sel_estimate_number_of_insns;
  4880. sel_common_sched_info.luid_for_non_insn = sel_luid_for_non_insn;
  4881. sel_common_sched_info.sched_pass_id = SCHED_SEL_PASS;
  4882. common_sched_info = &sel_common_sched_info;
  4883. current_sched_info = &sched_sel_haifa_sched_info;
  4884. current_sched_info->sched_max_insns_priority =
  4885. get_rgn_sched_max_insns_priority ();
  4886. sel_set_sched_flags ();
  4887. }
  4888. /* Adds basic block BB to region RGN at the position *BB_ORD_INDEX,
  4889. *BB_ORD_INDEX after that is increased. */
  4890. static void
  4891. sel_add_block_to_region (basic_block bb, int *bb_ord_index, int rgn)
  4892. {
  4893. RGN_NR_BLOCKS (rgn) += 1;
  4894. RGN_DONT_CALC_DEPS (rgn) = 0;
  4895. RGN_HAS_REAL_EBB (rgn) = 0;
  4896. CONTAINING_RGN (bb->index) = rgn;
  4897. BLOCK_TO_BB (bb->index) = *bb_ord_index;
  4898. rgn_bb_table[RGN_BLOCKS (rgn) + *bb_ord_index] = bb->index;
  4899. (*bb_ord_index)++;
  4900. /* FIXME: it is true only when not scheduling ebbs. */
  4901. RGN_BLOCKS (rgn + 1) = RGN_BLOCKS (rgn) + RGN_NR_BLOCKS (rgn);
  4902. }
  4903. /* Functions to support pipelining of outer loops. */
  4904. /* Creates a new empty region and returns it's number. */
  4905. static int
  4906. sel_create_new_region (void)
  4907. {
  4908. int new_rgn_number = nr_regions;
  4909. RGN_NR_BLOCKS (new_rgn_number) = 0;
  4910. /* FIXME: This will work only when EBBs are not created. */
  4911. if (new_rgn_number != 0)
  4912. RGN_BLOCKS (new_rgn_number) = RGN_BLOCKS (new_rgn_number - 1) +
  4913. RGN_NR_BLOCKS (new_rgn_number - 1);
  4914. else
  4915. RGN_BLOCKS (new_rgn_number) = 0;
  4916. /* Set the blocks of the next region so the other functions may
  4917. calculate the number of blocks in the region. */
  4918. RGN_BLOCKS (new_rgn_number + 1) = RGN_BLOCKS (new_rgn_number) +
  4919. RGN_NR_BLOCKS (new_rgn_number);
  4920. nr_regions++;
  4921. return new_rgn_number;
  4922. }
  4923. /* If X has a smaller topological sort number than Y, returns -1;
  4924. if greater, returns 1. */
  4925. static int
  4926. bb_top_order_comparator (const void *x, const void *y)
  4927. {
  4928. basic_block bb1 = *(const basic_block *) x;
  4929. basic_block bb2 = *(const basic_block *) y;
  4930. gcc_assert (bb1 == bb2
  4931. || rev_top_order_index[bb1->index]
  4932. != rev_top_order_index[bb2->index]);
  4933. /* It's a reverse topological order in REV_TOP_ORDER_INDEX, so
  4934. bbs with greater number should go earlier. */
  4935. if (rev_top_order_index[bb1->index] > rev_top_order_index[bb2->index])
  4936. return -1;
  4937. else
  4938. return 1;
  4939. }
  4940. /* Create a region for LOOP and return its number. If we don't want
  4941. to pipeline LOOP, return -1. */
  4942. static int
  4943. make_region_from_loop (struct loop *loop)
  4944. {
  4945. unsigned int i;
  4946. int new_rgn_number = -1;
  4947. struct loop *inner;
  4948. /* Basic block index, to be assigned to BLOCK_TO_BB. */
  4949. int bb_ord_index = 0;
  4950. basic_block *loop_blocks;
  4951. basic_block preheader_block;
  4952. if (loop->num_nodes
  4953. > (unsigned) PARAM_VALUE (PARAM_MAX_PIPELINE_REGION_BLOCKS))
  4954. return -1;
  4955. /* Don't pipeline loops whose latch belongs to some of its inner loops. */
  4956. for (inner = loop->inner; inner; inner = inner->inner)
  4957. if (flow_bb_inside_loop_p (inner, loop->latch))
  4958. return -1;
  4959. loop->ninsns = num_loop_insns (loop);
  4960. if ((int) loop->ninsns > PARAM_VALUE (PARAM_MAX_PIPELINE_REGION_INSNS))
  4961. return -1;
  4962. loop_blocks = get_loop_body_in_custom_order (loop, bb_top_order_comparator);
  4963. for (i = 0; i < loop->num_nodes; i++)
  4964. if (loop_blocks[i]->flags & BB_IRREDUCIBLE_LOOP)
  4965. {
  4966. free (loop_blocks);
  4967. return -1;
  4968. }
  4969. preheader_block = loop_preheader_edge (loop)->src;
  4970. gcc_assert (preheader_block);
  4971. gcc_assert (loop_blocks[0] == loop->header);
  4972. new_rgn_number = sel_create_new_region ();
  4973. sel_add_block_to_region (preheader_block, &bb_ord_index, new_rgn_number);
  4974. bitmap_set_bit (bbs_in_loop_rgns, preheader_block->index);
  4975. for (i = 0; i < loop->num_nodes; i++)
  4976. {
  4977. /* Add only those blocks that haven't been scheduled in the inner loop.
  4978. The exception is the basic blocks with bookkeeping code - they should
  4979. be added to the region (and they actually don't belong to the loop
  4980. body, but to the region containing that loop body). */
  4981. gcc_assert (new_rgn_number >= 0);
  4982. if (! bitmap_bit_p (bbs_in_loop_rgns, loop_blocks[i]->index))
  4983. {
  4984. sel_add_block_to_region (loop_blocks[i], &bb_ord_index,
  4985. new_rgn_number);
  4986. bitmap_set_bit (bbs_in_loop_rgns, loop_blocks[i]->index);
  4987. }
  4988. }
  4989. free (loop_blocks);
  4990. MARK_LOOP_FOR_PIPELINING (loop);
  4991. return new_rgn_number;
  4992. }
  4993. /* Create a new region from preheader blocks LOOP_BLOCKS. */
  4994. void
  4995. make_region_from_loop_preheader (vec<basic_block> *&loop_blocks)
  4996. {
  4997. unsigned int i;
  4998. int new_rgn_number = -1;
  4999. basic_block bb;
  5000. /* Basic block index, to be assigned to BLOCK_TO_BB. */
  5001. int bb_ord_index = 0;
  5002. new_rgn_number = sel_create_new_region ();
  5003. FOR_EACH_VEC_ELT (*loop_blocks, i, bb)
  5004. {
  5005. gcc_assert (new_rgn_number >= 0);
  5006. sel_add_block_to_region (bb, &bb_ord_index, new_rgn_number);
  5007. }
  5008. vec_free (loop_blocks);
  5009. }
  5010. /* Create region(s) from loop nest LOOP, such that inner loops will be
  5011. pipelined before outer loops. Returns true when a region for LOOP
  5012. is created. */
  5013. static bool
  5014. make_regions_from_loop_nest (struct loop *loop)
  5015. {
  5016. struct loop *cur_loop;
  5017. int rgn_number;
  5018. /* Traverse all inner nodes of the loop. */
  5019. for (cur_loop = loop->inner; cur_loop; cur_loop = cur_loop->next)
  5020. if (! bitmap_bit_p (bbs_in_loop_rgns, cur_loop->header->index))
  5021. return false;
  5022. /* At this moment all regular inner loops should have been pipelined.
  5023. Try to create a region from this loop. */
  5024. rgn_number = make_region_from_loop (loop);
  5025. if (rgn_number < 0)
  5026. return false;
  5027. loop_nests.safe_push (loop);
  5028. return true;
  5029. }
  5030. /* Initalize data structures needed. */
  5031. void
  5032. sel_init_pipelining (void)
  5033. {
  5034. /* Collect loop information to be used in outer loops pipelining. */
  5035. loop_optimizer_init (LOOPS_HAVE_PREHEADERS
  5036. | LOOPS_HAVE_FALLTHRU_PREHEADERS
  5037. | LOOPS_HAVE_RECORDED_EXITS
  5038. | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS);
  5039. current_loop_nest = NULL;
  5040. bbs_in_loop_rgns = sbitmap_alloc (last_basic_block_for_fn (cfun));
  5041. bitmap_clear (bbs_in_loop_rgns);
  5042. recompute_rev_top_order ();
  5043. }
  5044. /* Returns a struct loop for region RGN. */
  5045. loop_p
  5046. get_loop_nest_for_rgn (unsigned int rgn)
  5047. {
  5048. /* Regions created with extend_rgns don't have corresponding loop nests,
  5049. because they don't represent loops. */
  5050. if (rgn < loop_nests.length ())
  5051. return loop_nests[rgn];
  5052. else
  5053. return NULL;
  5054. }
  5055. /* True when LOOP was included into pipelining regions. */
  5056. bool
  5057. considered_for_pipelining_p (struct loop *loop)
  5058. {
  5059. if (loop_depth (loop) == 0)
  5060. return false;
  5061. /* Now, the loop could be too large or irreducible. Check whether its
  5062. region is in LOOP_NESTS.
  5063. We determine the region number of LOOP as the region number of its
  5064. latch. We can't use header here, because this header could be
  5065. just removed preheader and it will give us the wrong region number.
  5066. Latch can't be used because it could be in the inner loop too. */
  5067. if (LOOP_MARKED_FOR_PIPELINING_P (loop))
  5068. {
  5069. int rgn = CONTAINING_RGN (loop->latch->index);
  5070. gcc_assert ((unsigned) rgn < loop_nests.length ());
  5071. return true;
  5072. }
  5073. return false;
  5074. }
  5075. /* Makes regions from the rest of the blocks, after loops are chosen
  5076. for pipelining. */
  5077. static void
  5078. make_regions_from_the_rest (void)
  5079. {
  5080. int cur_rgn_blocks;
  5081. int *loop_hdr;
  5082. int i;
  5083. basic_block bb;
  5084. edge e;
  5085. edge_iterator ei;
  5086. int *degree;
  5087. /* Index in rgn_bb_table where to start allocating new regions. */
  5088. cur_rgn_blocks = nr_regions ? RGN_BLOCKS (nr_regions) : 0;
  5089. /* Make regions from all the rest basic blocks - those that don't belong to
  5090. any loop or belong to irreducible loops. Prepare the data structures
  5091. for extend_rgns. */
  5092. /* LOOP_HDR[I] == -1 if I-th bb doesn't belong to any loop,
  5093. LOOP_HDR[I] == LOOP_HDR[J] iff basic blocks I and J reside within the same
  5094. loop. */
  5095. loop_hdr = XNEWVEC (int, last_basic_block_for_fn (cfun));
  5096. degree = XCNEWVEC (int, last_basic_block_for_fn (cfun));
  5097. /* For each basic block that belongs to some loop assign the number
  5098. of innermost loop it belongs to. */
  5099. for (i = 0; i < last_basic_block_for_fn (cfun); i++)
  5100. loop_hdr[i] = -1;
  5101. FOR_EACH_BB_FN (bb, cfun)
  5102. {
  5103. if (bb->loop_father && bb->loop_father->num != 0
  5104. && !(bb->flags & BB_IRREDUCIBLE_LOOP))
  5105. loop_hdr[bb->index] = bb->loop_father->num;
  5106. }
  5107. /* For each basic block degree is calculated as the number of incoming
  5108. edges, that are going out of bbs that are not yet scheduled.
  5109. The basic blocks that are scheduled have degree value of zero. */
  5110. FOR_EACH_BB_FN (bb, cfun)
  5111. {
  5112. degree[bb->index] = 0;
  5113. if (!bitmap_bit_p (bbs_in_loop_rgns, bb->index))
  5114. {
  5115. FOR_EACH_EDGE (e, ei, bb->preds)
  5116. if (!bitmap_bit_p (bbs_in_loop_rgns, e->src->index))
  5117. degree[bb->index]++;
  5118. }
  5119. else
  5120. degree[bb->index] = -1;
  5121. }
  5122. extend_rgns (degree, &cur_rgn_blocks, bbs_in_loop_rgns, loop_hdr);
  5123. /* Any block that did not end up in a region is placed into a region
  5124. by itself. */
  5125. FOR_EACH_BB_FN (bb, cfun)
  5126. if (degree[bb->index] >= 0)
  5127. {
  5128. rgn_bb_table[cur_rgn_blocks] = bb->index;
  5129. RGN_NR_BLOCKS (nr_regions) = 1;
  5130. RGN_BLOCKS (nr_regions) = cur_rgn_blocks++;
  5131. RGN_DONT_CALC_DEPS (nr_regions) = 0;
  5132. RGN_HAS_REAL_EBB (nr_regions) = 0;
  5133. CONTAINING_RGN (bb->index) = nr_regions++;
  5134. BLOCK_TO_BB (bb->index) = 0;
  5135. }
  5136. free (degree);
  5137. free (loop_hdr);
  5138. }
  5139. /* Free data structures used in pipelining of loops. */
  5140. void sel_finish_pipelining (void)
  5141. {
  5142. struct loop *loop;
  5143. /* Release aux fields so we don't free them later by mistake. */
  5144. FOR_EACH_LOOP (loop, 0)
  5145. loop->aux = NULL;
  5146. loop_optimizer_finalize ();
  5147. loop_nests.release ();
  5148. free (rev_top_order_index);
  5149. rev_top_order_index = NULL;
  5150. }
  5151. /* This function replaces the find_rgns when
  5152. FLAG_SEL_SCHED_PIPELINING_OUTER_LOOPS is set. */
  5153. void
  5154. sel_find_rgns (void)
  5155. {
  5156. sel_init_pipelining ();
  5157. extend_regions ();
  5158. if (current_loops)
  5159. {
  5160. loop_p loop;
  5161. FOR_EACH_LOOP (loop, (flag_sel_sched_pipelining_outer_loops
  5162. ? LI_FROM_INNERMOST
  5163. : LI_ONLY_INNERMOST))
  5164. make_regions_from_loop_nest (loop);
  5165. }
  5166. /* Make regions from all the rest basic blocks and schedule them.
  5167. These blocks include blocks that don't belong to any loop or belong
  5168. to irreducible loops. */
  5169. make_regions_from_the_rest ();
  5170. /* We don't need bbs_in_loop_rgns anymore. */
  5171. sbitmap_free (bbs_in_loop_rgns);
  5172. bbs_in_loop_rgns = NULL;
  5173. }
  5174. /* Add the preheader blocks from previous loop to current region taking
  5175. it from LOOP_PREHEADER_BLOCKS (current_loop_nest) and record them in *BBS.
  5176. This function is only used with -fsel-sched-pipelining-outer-loops. */
  5177. void
  5178. sel_add_loop_preheaders (bb_vec_t *bbs)
  5179. {
  5180. int i;
  5181. basic_block bb;
  5182. vec<basic_block> *preheader_blocks
  5183. = LOOP_PREHEADER_BLOCKS (current_loop_nest);
  5184. if (!preheader_blocks)
  5185. return;
  5186. for (i = 0; preheader_blocks->iterate (i, &bb); i++)
  5187. {
  5188. bbs->safe_push (bb);
  5189. last_added_blocks.safe_push (bb);
  5190. sel_add_bb (bb);
  5191. }
  5192. vec_free (preheader_blocks);
  5193. }
  5194. /* While pipelining outer loops, returns TRUE if BB is a loop preheader.
  5195. Please note that the function should also work when pipelining_p is
  5196. false, because it is used when deciding whether we should or should
  5197. not reschedule pipelined code. */
  5198. bool
  5199. sel_is_loop_preheader_p (basic_block bb)
  5200. {
  5201. if (current_loop_nest)
  5202. {
  5203. struct loop *outer;
  5204. if (preheader_removed)
  5205. return false;
  5206. /* Preheader is the first block in the region. */
  5207. if (BLOCK_TO_BB (bb->index) == 0)
  5208. return true;
  5209. /* We used to find a preheader with the topological information.
  5210. Check that the above code is equivalent to what we did before. */
  5211. if (in_current_region_p (current_loop_nest->header))
  5212. gcc_assert (!(BLOCK_TO_BB (bb->index)
  5213. < BLOCK_TO_BB (current_loop_nest->header->index)));
  5214. /* Support the situation when the latch block of outer loop
  5215. could be from here. */
  5216. for (outer = loop_outer (current_loop_nest);
  5217. outer;
  5218. outer = loop_outer (outer))
  5219. if (considered_for_pipelining_p (outer) && outer->latch == bb)
  5220. gcc_unreachable ();
  5221. }
  5222. return false;
  5223. }
  5224. /* Check whether JUMP_BB ends with a jump insn that leads only to DEST_BB and
  5225. can be removed, making the corresponding edge fallthrough (assuming that
  5226. all basic blocks between JUMP_BB and DEST_BB are empty). */
  5227. static bool
  5228. bb_has_removable_jump_to_p (basic_block jump_bb, basic_block dest_bb)
  5229. {
  5230. if (!onlyjump_p (BB_END (jump_bb))
  5231. || tablejump_p (BB_END (jump_bb), NULL, NULL))
  5232. return false;
  5233. /* Several outgoing edges, abnormal edge or destination of jump is
  5234. not DEST_BB. */
  5235. if (EDGE_COUNT (jump_bb->succs) != 1
  5236. || EDGE_SUCC (jump_bb, 0)->flags & (EDGE_ABNORMAL | EDGE_CROSSING)
  5237. || EDGE_SUCC (jump_bb, 0)->dest != dest_bb)
  5238. return false;
  5239. /* If not anything of the upper. */
  5240. return true;
  5241. }
  5242. /* Removes the loop preheader from the current region and saves it in
  5243. PREHEADER_BLOCKS of the father loop, so they will be added later to
  5244. region that represents an outer loop. */
  5245. static void
  5246. sel_remove_loop_preheader (void)
  5247. {
  5248. int i, old_len;
  5249. int cur_rgn = CONTAINING_RGN (BB_TO_BLOCK (0));
  5250. basic_block bb;
  5251. bool all_empty_p = true;
  5252. vec<basic_block> *preheader_blocks
  5253. = LOOP_PREHEADER_BLOCKS (loop_outer (current_loop_nest));
  5254. vec_check_alloc (preheader_blocks, 0);
  5255. gcc_assert (current_loop_nest);
  5256. old_len = preheader_blocks->length ();
  5257. /* Add blocks that aren't within the current loop to PREHEADER_BLOCKS. */
  5258. for (i = 0; i < RGN_NR_BLOCKS (cur_rgn); i++)
  5259. {
  5260. bb = BASIC_BLOCK_FOR_FN (cfun, BB_TO_BLOCK (i));
  5261. /* If the basic block belongs to region, but doesn't belong to
  5262. corresponding loop, then it should be a preheader. */
  5263. if (sel_is_loop_preheader_p (bb))
  5264. {
  5265. preheader_blocks->safe_push (bb);
  5266. if (BB_END (bb) != bb_note (bb))
  5267. all_empty_p = false;
  5268. }
  5269. }
  5270. /* Remove these blocks only after iterating over the whole region. */
  5271. for (i = preheader_blocks->length () - 1; i >= old_len; i--)
  5272. {
  5273. bb = (*preheader_blocks)[i];
  5274. sel_remove_bb (bb, false);
  5275. }
  5276. if (!considered_for_pipelining_p (loop_outer (current_loop_nest)))
  5277. {
  5278. if (!all_empty_p)
  5279. /* Immediately create new region from preheader. */
  5280. make_region_from_loop_preheader (preheader_blocks);
  5281. else
  5282. {
  5283. /* If all preheader blocks are empty - dont create new empty region.
  5284. Instead, remove them completely. */
  5285. FOR_EACH_VEC_ELT (*preheader_blocks, i, bb)
  5286. {
  5287. edge e;
  5288. edge_iterator ei;
  5289. basic_block prev_bb = bb->prev_bb, next_bb = bb->next_bb;
  5290. /* Redirect all incoming edges to next basic block. */
  5291. for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
  5292. {
  5293. if (! (e->flags & EDGE_FALLTHRU))
  5294. redirect_edge_and_branch (e, bb->next_bb);
  5295. else
  5296. redirect_edge_succ (e, bb->next_bb);
  5297. }
  5298. gcc_assert (BB_NOTE_LIST (bb) == NULL);
  5299. delete_and_free_basic_block (bb);
  5300. /* Check if after deleting preheader there is a nonconditional
  5301. jump in PREV_BB that leads to the next basic block NEXT_BB.
  5302. If it is so - delete this jump and clear data sets of its
  5303. basic block if it becomes empty. */
  5304. if (next_bb->prev_bb == prev_bb
  5305. && prev_bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  5306. && bb_has_removable_jump_to_p (prev_bb, next_bb))
  5307. {
  5308. redirect_edge_and_branch (EDGE_SUCC (prev_bb, 0), next_bb);
  5309. if (BB_END (prev_bb) == bb_note (prev_bb))
  5310. free_data_sets (prev_bb);
  5311. }
  5312. set_immediate_dominator (CDI_DOMINATORS, next_bb,
  5313. recompute_dominator (CDI_DOMINATORS,
  5314. next_bb));
  5315. }
  5316. }
  5317. vec_free (preheader_blocks);
  5318. }
  5319. else
  5320. /* Store preheader within the father's loop structure. */
  5321. SET_LOOP_PREHEADER_BLOCKS (loop_outer (current_loop_nest),
  5322. preheader_blocks);
  5323. }
  5324. #endif