sel-sched.c 242 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756
  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 "rtl-error.h"
  20. #include "tm_p.h"
  21. #include "hard-reg-set.h"
  22. #include "regs.h"
  23. #include "hashtab.h"
  24. #include "hash-set.h"
  25. #include "vec.h"
  26. #include "machmode.h"
  27. #include "input.h"
  28. #include "function.h"
  29. #include "predict.h"
  30. #include "dominance.h"
  31. #include "cfg.h"
  32. #include "cfgbuild.h"
  33. #include "basic-block.h"
  34. #include "flags.h"
  35. #include "insn-config.h"
  36. #include "insn-attr.h"
  37. #include "except.h"
  38. #include "recog.h"
  39. #include "params.h"
  40. #include "target.h"
  41. #include "output.h"
  42. #include "sched-int.h"
  43. #include "ggc.h"
  44. #include "symtab.h"
  45. #include "wide-int.h"
  46. #include "inchash.h"
  47. #include "tree.h"
  48. #include "langhooks.h"
  49. #include "rtlhooks-def.h"
  50. #include "emit-rtl.h"
  51. #include "ira.h"
  52. #include "rtl-iter.h"
  53. #ifdef INSN_SCHEDULING
  54. #include "sel-sched-ir.h"
  55. #include "sel-sched-dump.h"
  56. #include "sel-sched.h"
  57. #include "dbgcnt.h"
  58. /* Implementation of selective scheduling approach.
  59. The below implementation follows the original approach with the following
  60. changes:
  61. o the scheduler works after register allocation (but can be also tuned
  62. to work before RA);
  63. o some instructions are not copied or register renamed;
  64. o conditional jumps are not moved with code duplication;
  65. o several jumps in one parallel group are not supported;
  66. o when pipelining outer loops, code motion through inner loops
  67. is not supported;
  68. o control and data speculation are supported;
  69. o some improvements for better compile time/performance were made.
  70. Terminology
  71. ===========
  72. A vinsn, or virtual insn, is an insn with additional data characterizing
  73. insn pattern, such as LHS, RHS, register sets used/set/clobbered, etc.
  74. Vinsns also act as smart pointers to save memory by reusing them in
  75. different expressions. A vinsn is described by vinsn_t type.
  76. An expression is a vinsn with additional data characterizing its properties
  77. at some point in the control flow graph. The data may be its usefulness,
  78. priority, speculative status, whether it was renamed/subsituted, etc.
  79. An expression is described by expr_t type.
  80. Availability set (av_set) is a set of expressions at a given control flow
  81. point. It is represented as av_set_t. The expressions in av sets are kept
  82. sorted in the terms of expr_greater_p function. It allows to truncate
  83. the set while leaving the best expressions.
  84. A fence is a point through which code motion is prohibited. On each step,
  85. we gather a parallel group of insns at a fence. It is possible to have
  86. multiple fences. A fence is represented via fence_t.
  87. A boundary is the border between the fence group and the rest of the code.
  88. Currently, we never have more than one boundary per fence, as we finalize
  89. the fence group when a jump is scheduled. A boundary is represented
  90. via bnd_t.
  91. High-level overview
  92. ===================
  93. The scheduler finds regions to schedule, schedules each one, and finalizes.
  94. The regions are formed starting from innermost loops, so that when the inner
  95. loop is pipelined, its prologue can be scheduled together with yet unprocessed
  96. outer loop. The rest of acyclic regions are found using extend_rgns:
  97. the blocks that are not yet allocated to any regions are traversed in top-down
  98. order, and a block is added to a region to which all its predecessors belong;
  99. otherwise, the block starts its own region.
  100. The main scheduling loop (sel_sched_region_2) consists of just
  101. scheduling on each fence and updating fences. For each fence,
  102. we fill a parallel group of insns (fill_insns) until some insns can be added.
  103. First, we compute available exprs (av-set) at the boundary of the current
  104. group. Second, we choose the best expression from it. If the stall is
  105. required to schedule any of the expressions, we advance the current cycle
  106. appropriately. So, the final group does not exactly correspond to a VLIW
  107. word. Third, we move the chosen expression to the boundary (move_op)
  108. and update the intermediate av sets and liveness sets. We quit fill_insns
  109. when either no insns left for scheduling or we have scheduled enough insns
  110. so we feel like advancing a scheduling point.
  111. Computing available expressions
  112. ===============================
  113. The computation (compute_av_set) is a bottom-up traversal. At each insn,
  114. we're moving the union of its successors' sets through it via
  115. moveup_expr_set. The dependent expressions are removed. Local
  116. transformations (substitution, speculation) are applied to move more
  117. exprs. Then the expr corresponding to the current insn is added.
  118. The result is saved on each basic block header.
  119. When traversing the CFG, we're moving down for no more than max_ws insns.
  120. Also, we do not move down to ineligible successors (is_ineligible_successor),
  121. which include moving along a back-edge, moving to already scheduled code,
  122. and moving to another fence. The first two restrictions are lifted during
  123. pipelining, which allows us to move insns along a back-edge. We always have
  124. an acyclic region for scheduling because we forbid motion through fences.
  125. Choosing the best expression
  126. ============================
  127. We sort the final availability set via sel_rank_for_schedule, then we remove
  128. expressions which are not yet ready (tick_check_p) or which dest registers
  129. cannot be used. For some of them, we choose another register via
  130. find_best_reg. To do this, we run find_used_regs to calculate the set of
  131. registers which cannot be used. The find_used_regs function performs
  132. a traversal of code motion paths for an expr. We consider for renaming
  133. only registers which are from the same regclass as the original one and
  134. using which does not interfere with any live ranges. Finally, we convert
  135. the resulting set to the ready list format and use max_issue and reorder*
  136. hooks similarly to the Haifa scheduler.
  137. Scheduling the best expression
  138. ==============================
  139. We run the move_op routine to perform the same type of code motion paths
  140. traversal as in find_used_regs. (These are working via the same driver,
  141. code_motion_path_driver.) When moving down the CFG, we look for original
  142. instruction that gave birth to a chosen expression. We undo
  143. the transformations performed on an expression via the history saved in it.
  144. When found, we remove the instruction or leave a reg-reg copy/speculation
  145. check if needed. On a way up, we insert bookkeeping copies at each join
  146. point. If a copy is not needed, it will be removed later during this
  147. traversal. We update the saved av sets and liveness sets on the way up, too.
  148. Finalizing the schedule
  149. =======================
  150. When pipelining, we reschedule the blocks from which insns were pipelined
  151. to get a tighter schedule. On Itanium, we also perform bundling via
  152. the same routine from ia64.c.
  153. Dependence analysis changes
  154. ===========================
  155. We augmented the sched-deps.c with hooks that get called when a particular
  156. dependence is found in a particular part of an insn. Using these hooks, we
  157. can do several actions such as: determine whether an insn can be moved through
  158. another (has_dependence_p, moveup_expr); find out whether an insn can be
  159. scheduled on the current cycle (tick_check_p); find out registers that
  160. are set/used/clobbered by an insn and find out all the strange stuff that
  161. restrict its movement, like SCHED_GROUP_P or CANT_MOVE (done in
  162. init_global_and_expr_for_insn).
  163. Initialization changes
  164. ======================
  165. There are parts of haifa-sched.c, sched-deps.c, and sched-rgn.c that are
  166. reused in all of the schedulers. We have split up the initialization of data
  167. of such parts into different functions prefixed with scheduler type and
  168. postfixed with the type of data initialized: {,sel_,haifa_}sched_{init,finish},
  169. sched_rgn_init/finish, sched_deps_init/finish, sched_init_{luids/bbs}, etc.
  170. The same splitting is done with current_sched_info structure:
  171. dependence-related parts are in sched_deps_info, common part is in
  172. common_sched_info, and haifa/sel/etc part is in current_sched_info.
  173. Target contexts
  174. ===============
  175. As we now have multiple-point scheduling, this would not work with backends
  176. which save some of the scheduler state to use it in the target hooks.
  177. For this purpose, we introduce a concept of target contexts, which
  178. encapsulate such information. The backend should implement simple routines
  179. of allocating/freeing/setting such a context. The scheduler calls these
  180. as target hooks and handles the target context as an opaque pointer (similar
  181. to the DFA state type, state_t).
  182. Various speedups
  183. ================
  184. As the correct data dependence graph is not supported during scheduling (which
  185. is to be changed in mid-term), we cache as much of the dependence analysis
  186. results as possible to avoid reanalyzing. This includes: bitmap caches on
  187. each insn in stream of the region saying yes/no for a query with a pair of
  188. UIDs; hashtables with the previously done transformations on each insn in
  189. stream; a vector keeping a history of transformations on each expr.
  190. Also, we try to minimize the dependence context used on each fence to check
  191. whether the given expression is ready for scheduling by removing from it
  192. insns that are definitely completed the execution. The results of
  193. tick_check_p checks are also cached in a vector on each fence.
  194. We keep a valid liveness set on each insn in a region to avoid the high
  195. cost of recomputation on large basic blocks.
  196. Finally, we try to minimize the number of needed updates to the availability
  197. sets. The updates happen in two cases: when fill_insns terminates,
  198. we advance all fences and increase the stage number to show that the region
  199. has changed and the sets are to be recomputed; and when the next iteration
  200. of a loop in fill_insns happens (but this one reuses the saved av sets
  201. on bb headers.) Thus, we try to break the fill_insns loop only when
  202. "significant" number of insns from the current scheduling window was
  203. scheduled. This should be made a target param.
  204. TODO: correctly support the data dependence graph at all stages and get rid
  205. of all caches. This should speed up the scheduler.
  206. TODO: implement moving cond jumps with bookkeeping copies on both targets.
  207. TODO: tune the scheduler before RA so it does not create too much pseudos.
  208. References:
  209. S.-M. Moon and K. Ebcioglu. Parallelizing nonnumerical code with
  210. selective scheduling and software pipelining.
  211. ACM TOPLAS, Vol 19, No. 6, pages 853--898, Nov. 1997.
  212. Andrey Belevantsev, Maxim Kuvyrkov, Vladimir Makarov, Dmitry Melnik,
  213. and Dmitry Zhurikhin. An interblock VLIW-targeted instruction scheduler
  214. for GCC. In Proceedings of GCC Developers' Summit 2006.
  215. Arutyun Avetisyan, Andrey Belevantsev, and Dmitry Melnik. GCC Instruction
  216. Scheduler and Software Pipeliner on the Itanium Platform. EPIC-7 Workshop.
  217. http://rogue.colorado.edu/EPIC7/.
  218. */
  219. /* True when pipelining is enabled. */
  220. bool pipelining_p;
  221. /* True if bookkeeping is enabled. */
  222. bool bookkeeping_p;
  223. /* Maximum number of insns that are eligible for renaming. */
  224. int max_insns_to_rename;
  225. /* Definitions of local types and macros. */
  226. /* Represents possible outcomes of moving an expression through an insn. */
  227. enum MOVEUP_EXPR_CODE
  228. {
  229. /* The expression is not changed. */
  230. MOVEUP_EXPR_SAME,
  231. /* Not changed, but requires a new destination register. */
  232. MOVEUP_EXPR_AS_RHS,
  233. /* Cannot be moved. */
  234. MOVEUP_EXPR_NULL,
  235. /* Changed (substituted or speculated). */
  236. MOVEUP_EXPR_CHANGED
  237. };
  238. /* The container to be passed into rtx search & replace functions. */
  239. struct rtx_search_arg
  240. {
  241. /* What we are searching for. */
  242. rtx x;
  243. /* The occurrence counter. */
  244. int n;
  245. };
  246. typedef struct rtx_search_arg *rtx_search_arg_p;
  247. /* This struct contains precomputed hard reg sets that are needed when
  248. computing registers available for renaming. */
  249. struct hard_regs_data
  250. {
  251. /* For every mode, this stores registers available for use with
  252. that mode. */
  253. HARD_REG_SET regs_for_mode[NUM_MACHINE_MODES];
  254. /* True when regs_for_mode[mode] is initialized. */
  255. bool regs_for_mode_ok[NUM_MACHINE_MODES];
  256. /* For every register, it has regs that are ok to rename into it.
  257. The register in question is always set. If not, this means
  258. that the whole set is not computed yet. */
  259. HARD_REG_SET regs_for_rename[FIRST_PSEUDO_REGISTER];
  260. /* For every mode, this stores registers not available due to
  261. call clobbering. */
  262. HARD_REG_SET regs_for_call_clobbered[NUM_MACHINE_MODES];
  263. /* All registers that are used or call used. */
  264. HARD_REG_SET regs_ever_used;
  265. #ifdef STACK_REGS
  266. /* Stack registers. */
  267. HARD_REG_SET stack_regs;
  268. #endif
  269. };
  270. /* Holds the results of computation of available for renaming and
  271. unavailable hard registers. */
  272. struct reg_rename
  273. {
  274. /* These are unavailable due to calls crossing, globalness, etc. */
  275. HARD_REG_SET unavailable_hard_regs;
  276. /* These are *available* for renaming. */
  277. HARD_REG_SET available_for_renaming;
  278. /* Whether this code motion path crosses a call. */
  279. bool crosses_call;
  280. };
  281. /* A global structure that contains the needed information about harg
  282. regs. */
  283. static struct hard_regs_data sel_hrd;
  284. /* This structure holds local data used in code_motion_path_driver hooks on
  285. the same or adjacent levels of recursion. Here we keep those parameters
  286. that are not used in code_motion_path_driver routine itself, but only in
  287. its hooks. Moreover, all parameters that can be modified in hooks are
  288. in this structure, so all other parameters passed explicitly to hooks are
  289. read-only. */
  290. struct cmpd_local_params
  291. {
  292. /* Local params used in move_op_* functions. */
  293. /* Edges for bookkeeping generation. */
  294. edge e1, e2;
  295. /* C_EXPR merged from all successors and locally allocated temporary C_EXPR. */
  296. expr_t c_expr_merged, c_expr_local;
  297. /* Local params used in fur_* functions. */
  298. /* Copy of the ORIGINAL_INSN list, stores the original insns already
  299. found before entering the current level of code_motion_path_driver. */
  300. def_list_t old_original_insns;
  301. /* Local params used in move_op_* functions. */
  302. /* True when we have removed last insn in the block which was
  303. also a boundary. Do not update anything or create bookkeeping copies. */
  304. BOOL_BITFIELD removed_last_insn : 1;
  305. };
  306. /* Stores the static parameters for move_op_* calls. */
  307. struct moveop_static_params
  308. {
  309. /* Destination register. */
  310. rtx dest;
  311. /* Current C_EXPR. */
  312. expr_t c_expr;
  313. /* An UID of expr_vliw which is to be moved up. If we find other exprs,
  314. they are to be removed. */
  315. int uid;
  316. #ifdef ENABLE_CHECKING
  317. /* This is initialized to the insn on which the driver stopped its traversal. */
  318. insn_t failed_insn;
  319. #endif
  320. /* True if we scheduled an insn with different register. */
  321. bool was_renamed;
  322. };
  323. /* Stores the static parameters for fur_* calls. */
  324. struct fur_static_params
  325. {
  326. /* Set of registers unavailable on the code motion path. */
  327. regset used_regs;
  328. /* Pointer to the list of original insns definitions. */
  329. def_list_t *original_insns;
  330. /* True if a code motion path contains a CALL insn. */
  331. bool crosses_call;
  332. };
  333. typedef struct fur_static_params *fur_static_params_p;
  334. typedef struct cmpd_local_params *cmpd_local_params_p;
  335. typedef struct moveop_static_params *moveop_static_params_p;
  336. /* Set of hooks and parameters that determine behaviour specific to
  337. move_op or find_used_regs functions. */
  338. struct code_motion_path_driver_info_def
  339. {
  340. /* Called on enter to the basic block. */
  341. int (*on_enter) (insn_t, cmpd_local_params_p, void *, bool);
  342. /* Called when original expr is found. */
  343. void (*orig_expr_found) (insn_t, expr_t, cmpd_local_params_p, void *);
  344. /* Called while descending current basic block if current insn is not
  345. the original EXPR we're searching for. */
  346. bool (*orig_expr_not_found) (insn_t, av_set_t, void *);
  347. /* Function to merge C_EXPRes from different successors. */
  348. void (*merge_succs) (insn_t, insn_t, int, cmpd_local_params_p, void *);
  349. /* Function to finalize merge from different successors and possibly
  350. deallocate temporary data structures used for merging. */
  351. void (*after_merge_succs) (cmpd_local_params_p, void *);
  352. /* Called on the backward stage of recursion to do moveup_expr.
  353. Used only with move_op_*. */
  354. void (*ascend) (insn_t, void *);
  355. /* Called on the ascending pass, before returning from the current basic
  356. block or from the whole traversal. */
  357. void (*at_first_insn) (insn_t, cmpd_local_params_p, void *);
  358. /* When processing successors in move_op we need only descend into
  359. SUCCS_NORMAL successors, while in find_used_regs we need SUCCS_ALL. */
  360. int succ_flags;
  361. /* The routine name to print in dumps ("move_op" of "find_used_regs"). */
  362. const char *routine_name;
  363. };
  364. /* Global pointer to current hooks, either points to MOVE_OP_HOOKS or
  365. FUR_HOOKS. */
  366. struct code_motion_path_driver_info_def *code_motion_path_driver_info;
  367. /* Set of hooks for performing move_op and find_used_regs routines with
  368. code_motion_path_driver. */
  369. extern struct code_motion_path_driver_info_def move_op_hooks, fur_hooks;
  370. /* True if/when we want to emulate Haifa scheduler in the common code.
  371. This is used in sched_rgn_local_init and in various places in
  372. sched-deps.c. */
  373. int sched_emulate_haifa_p;
  374. /* GLOBAL_LEVEL is used to discard information stored in basic block headers
  375. av_sets. Av_set of bb header is valid if its (bb header's) level is equal
  376. to GLOBAL_LEVEL. And invalid if lesser. This is primarily used to advance
  377. scheduling window. */
  378. int global_level;
  379. /* Current fences. */
  380. flist_t fences;
  381. /* True when separable insns should be scheduled as RHSes. */
  382. static bool enable_schedule_as_rhs_p;
  383. /* Used in verify_target_availability to assert that target reg is reported
  384. unavailabile by both TARGET_UNAVAILABLE and find_used_regs only if
  385. we haven't scheduled anything on the previous fence.
  386. if scheduled_something_on_previous_fence is true, TARGET_UNAVAILABLE can
  387. have more conservative value than the one returned by the
  388. find_used_regs, thus we shouldn't assert that these values are equal. */
  389. static bool scheduled_something_on_previous_fence;
  390. /* All newly emitted insns will have their uids greater than this value. */
  391. static int first_emitted_uid;
  392. /* Set of basic blocks that are forced to start new ebbs. This is a subset
  393. of all the ebb heads. */
  394. static bitmap_head _forced_ebb_heads;
  395. bitmap_head *forced_ebb_heads = &_forced_ebb_heads;
  396. /* Blocks that need to be rescheduled after pipelining. */
  397. bitmap blocks_to_reschedule = NULL;
  398. /* True when the first lv set should be ignored when updating liveness. */
  399. static bool ignore_first = false;
  400. /* Number of insns max_issue has initialized data structures for. */
  401. static int max_issue_size = 0;
  402. /* Whether we can issue more instructions. */
  403. static int can_issue_more;
  404. /* Maximum software lookahead window size, reduced when rescheduling after
  405. pipelining. */
  406. static int max_ws;
  407. /* Number of insns scheduled in current region. */
  408. static int num_insns_scheduled;
  409. /* A vector of expressions is used to be able to sort them. */
  410. static vec<expr_t> vec_av_set = vNULL;
  411. /* A vector of vinsns is used to hold temporary lists of vinsns. */
  412. typedef vec<vinsn_t> vinsn_vec_t;
  413. /* This vector has the exprs which may still present in av_sets, but actually
  414. can't be moved up due to bookkeeping created during code motion to another
  415. fence. See comment near the call to update_and_record_unavailable_insns
  416. for the detailed explanations. */
  417. static vinsn_vec_t vec_bookkeeping_blocked_vinsns = vinsn_vec_t ();
  418. /* This vector has vinsns which are scheduled with renaming on the first fence
  419. and then seen on the second. For expressions with such vinsns, target
  420. availability information may be wrong. */
  421. static vinsn_vec_t vec_target_unavailable_vinsns = vinsn_vec_t ();
  422. /* Vector to store temporary nops inserted in move_op to prevent removal
  423. of empty bbs. */
  424. static vec<insn_t> vec_temp_moveop_nops = vNULL;
  425. /* These bitmaps record original instructions scheduled on the current
  426. iteration and bookkeeping copies created by them. */
  427. static bitmap current_originators = NULL;
  428. static bitmap current_copies = NULL;
  429. /* This bitmap marks the blocks visited by code_motion_path_driver so we don't
  430. visit them afterwards. */
  431. static bitmap code_motion_visited_blocks = NULL;
  432. /* Variables to accumulate different statistics. */
  433. /* The number of bookkeeping copies created. */
  434. static int stat_bookkeeping_copies;
  435. /* The number of insns that required bookkeeiping for their scheduling. */
  436. static int stat_insns_needed_bookkeeping;
  437. /* The number of insns that got renamed. */
  438. static int stat_renamed_scheduled;
  439. /* The number of substitutions made during scheduling. */
  440. static int stat_substitutions_total;
  441. /* Forward declarations of static functions. */
  442. static bool rtx_ok_for_substitution_p (rtx, rtx);
  443. static int sel_rank_for_schedule (const void *, const void *);
  444. static av_set_t find_sequential_best_exprs (bnd_t, expr_t, bool);
  445. static basic_block find_block_for_bookkeeping (edge e1, edge e2, bool lax);
  446. static rtx get_dest_from_orig_ops (av_set_t);
  447. static basic_block generate_bookkeeping_insn (expr_t, edge, edge);
  448. static bool find_used_regs (insn_t, av_set_t, regset, struct reg_rename *,
  449. def_list_t *);
  450. static bool move_op (insn_t, av_set_t, expr_t, rtx, expr_t, bool*);
  451. static int code_motion_path_driver (insn_t, av_set_t, ilist_t,
  452. cmpd_local_params_p, void *);
  453. static void sel_sched_region_1 (void);
  454. static void sel_sched_region_2 (int);
  455. static av_set_t compute_av_set_inside_bb (insn_t, ilist_t, int, bool);
  456. static void debug_state (state_t);
  457. /* Functions that work with fences. */
  458. /* Advance one cycle on FENCE. */
  459. static void
  460. advance_one_cycle (fence_t fence)
  461. {
  462. unsigned i;
  463. int cycle;
  464. rtx_insn *insn;
  465. advance_state (FENCE_STATE (fence));
  466. cycle = ++FENCE_CYCLE (fence);
  467. FENCE_ISSUED_INSNS (fence) = 0;
  468. FENCE_STARTS_CYCLE_P (fence) = 1;
  469. can_issue_more = issue_rate;
  470. FENCE_ISSUE_MORE (fence) = can_issue_more;
  471. for (i = 0; vec_safe_iterate (FENCE_EXECUTING_INSNS (fence), i, &insn); )
  472. {
  473. if (INSN_READY_CYCLE (insn) < cycle)
  474. {
  475. remove_from_deps (FENCE_DC (fence), insn);
  476. FENCE_EXECUTING_INSNS (fence)->unordered_remove (i);
  477. continue;
  478. }
  479. i++;
  480. }
  481. if (sched_verbose >= 2)
  482. {
  483. sel_print ("Finished a cycle. Current cycle = %d\n", FENCE_CYCLE (fence));
  484. debug_state (FENCE_STATE (fence));
  485. }
  486. }
  487. /* Returns true when SUCC in a fallthru bb of INSN, possibly
  488. skipping empty basic blocks. */
  489. static bool
  490. in_fallthru_bb_p (rtx insn, rtx succ)
  491. {
  492. basic_block bb = BLOCK_FOR_INSN (insn);
  493. edge e;
  494. if (bb == BLOCK_FOR_INSN (succ))
  495. return true;
  496. e = find_fallthru_edge_from (bb);
  497. if (e)
  498. bb = e->dest;
  499. else
  500. return false;
  501. while (sel_bb_empty_p (bb))
  502. bb = bb->next_bb;
  503. return bb == BLOCK_FOR_INSN (succ);
  504. }
  505. /* Construct successor fences from OLD_FENCEs and put them in NEW_FENCES.
  506. When a successor will continue a ebb, transfer all parameters of a fence
  507. to the new fence. ORIG_MAX_SEQNO is the maximal seqno before this round
  508. of scheduling helping to distinguish between the old and the new code. */
  509. static void
  510. extract_new_fences_from (flist_t old_fences, flist_tail_t new_fences,
  511. int orig_max_seqno)
  512. {
  513. bool was_here_p = false;
  514. insn_t insn = NULL;
  515. insn_t succ;
  516. succ_iterator si;
  517. ilist_iterator ii;
  518. fence_t fence = FLIST_FENCE (old_fences);
  519. basic_block bb;
  520. /* Get the only element of FENCE_BNDS (fence). */
  521. FOR_EACH_INSN (insn, ii, FENCE_BNDS (fence))
  522. {
  523. gcc_assert (!was_here_p);
  524. was_here_p = true;
  525. }
  526. gcc_assert (was_here_p && insn != NULL_RTX);
  527. /* When in the "middle" of the block, just move this fence
  528. to the new list. */
  529. bb = BLOCK_FOR_INSN (insn);
  530. if (! sel_bb_end_p (insn)
  531. || (single_succ_p (bb)
  532. && single_pred_p (single_succ (bb))))
  533. {
  534. insn_t succ;
  535. succ = (sel_bb_end_p (insn)
  536. ? sel_bb_head (single_succ (bb))
  537. : NEXT_INSN (insn));
  538. if (INSN_SEQNO (succ) > 0
  539. && INSN_SEQNO (succ) <= orig_max_seqno
  540. && INSN_SCHED_TIMES (succ) <= 0)
  541. {
  542. FENCE_INSN (fence) = succ;
  543. move_fence_to_fences (old_fences, new_fences);
  544. if (sched_verbose >= 1)
  545. sel_print ("Fence %d continues as %d[%d] (state continue)\n",
  546. INSN_UID (insn), INSN_UID (succ), BLOCK_NUM (succ));
  547. }
  548. return;
  549. }
  550. /* Otherwise copy fence's structures to (possibly) multiple successors. */
  551. FOR_EACH_SUCC_1 (succ, si, insn, SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
  552. {
  553. int seqno = INSN_SEQNO (succ);
  554. if (0 < seqno && seqno <= orig_max_seqno
  555. && (pipelining_p || INSN_SCHED_TIMES (succ) <= 0))
  556. {
  557. bool b = (in_same_ebb_p (insn, succ)
  558. || in_fallthru_bb_p (insn, succ));
  559. if (sched_verbose >= 1)
  560. sel_print ("Fence %d continues as %d[%d] (state %s)\n",
  561. INSN_UID (insn), INSN_UID (succ),
  562. BLOCK_NUM (succ), b ? "continue" : "reset");
  563. if (b)
  564. add_dirty_fence_to_fences (new_fences, succ, fence);
  565. else
  566. {
  567. /* Mark block of the SUCC as head of the new ebb. */
  568. bitmap_set_bit (forced_ebb_heads, BLOCK_NUM (succ));
  569. add_clean_fence_to_fences (new_fences, succ, fence);
  570. }
  571. }
  572. }
  573. }
  574. /* Functions to support substitution. */
  575. /* Returns whether INSN with dependence status DS is eligible for
  576. substitution, i.e. it's a copy operation x := y, and RHS that is
  577. moved up through this insn should be substituted. */
  578. static bool
  579. can_substitute_through_p (insn_t insn, ds_t ds)
  580. {
  581. /* We can substitute only true dependencies. */
  582. if ((ds & DEP_OUTPUT)
  583. || (ds & DEP_ANTI)
  584. || ! INSN_RHS (insn)
  585. || ! INSN_LHS (insn))
  586. return false;
  587. /* Now we just need to make sure the INSN_RHS consists of only one
  588. simple REG rtx. */
  589. if (REG_P (INSN_LHS (insn))
  590. && REG_P (INSN_RHS (insn)))
  591. return true;
  592. return false;
  593. }
  594. /* Substitute all occurrences of INSN's destination in EXPR' vinsn with INSN's
  595. source (if INSN is eligible for substitution). Returns TRUE if
  596. substitution was actually performed, FALSE otherwise. Substitution might
  597. be not performed because it's either EXPR' vinsn doesn't contain INSN's
  598. destination or the resulting insn is invalid for the target machine.
  599. When UNDO is true, perform unsubstitution instead (the difference is in
  600. the part of rtx on which validate_replace_rtx is called). */
  601. static bool
  602. substitute_reg_in_expr (expr_t expr, insn_t insn, bool undo)
  603. {
  604. rtx *where;
  605. bool new_insn_valid;
  606. vinsn_t *vi = &EXPR_VINSN (expr);
  607. bool has_rhs = VINSN_RHS (*vi) != NULL;
  608. rtx old, new_rtx;
  609. /* Do not try to replace in SET_DEST. Although we'll choose new
  610. register for the RHS, we don't want to change RHS' original reg.
  611. If the insn is not SET, we may still be able to substitute something
  612. in it, and if we're here (don't have deps), it doesn't write INSN's
  613. dest. */
  614. where = (has_rhs
  615. ? &VINSN_RHS (*vi)
  616. : &PATTERN (VINSN_INSN_RTX (*vi)));
  617. old = undo ? INSN_RHS (insn) : INSN_LHS (insn);
  618. /* Substitute if INSN has a form of x:=y and LHS(INSN) occurs in *VI. */
  619. if (rtx_ok_for_substitution_p (old, *where))
  620. {
  621. rtx_insn *new_insn;
  622. rtx *where_replace;
  623. /* We should copy these rtxes before substitution. */
  624. new_rtx = copy_rtx (undo ? INSN_LHS (insn) : INSN_RHS (insn));
  625. new_insn = create_copy_of_insn_rtx (VINSN_INSN_RTX (*vi));
  626. /* Where we'll replace.
  627. WHERE_REPLACE should point inside NEW_INSN, so INSN_RHS couldn't be
  628. used instead of SET_SRC. */
  629. where_replace = (has_rhs
  630. ? &SET_SRC (PATTERN (new_insn))
  631. : &PATTERN (new_insn));
  632. new_insn_valid
  633. = validate_replace_rtx_part_nosimplify (old, new_rtx, where_replace,
  634. new_insn);
  635. /* ??? Actually, constrain_operands result depends upon choice of
  636. destination register. E.g. if we allow single register to be an rhs,
  637. and if we try to move dx=ax(as rhs) through ax=dx, we'll result
  638. in invalid insn dx=dx, so we'll loose this rhs here.
  639. Just can't come up with significant testcase for this, so just
  640. leaving it for now. */
  641. if (new_insn_valid)
  642. {
  643. change_vinsn_in_expr (expr,
  644. create_vinsn_from_insn_rtx (new_insn, false));
  645. /* Do not allow clobbering the address register of speculative
  646. insns. */
  647. if ((EXPR_SPEC_DONE_DS (expr) & SPECULATIVE)
  648. && register_unavailable_p (VINSN_REG_USES (EXPR_VINSN (expr)),
  649. expr_dest_reg (expr)))
  650. EXPR_TARGET_AVAILABLE (expr) = false;
  651. return true;
  652. }
  653. else
  654. return false;
  655. }
  656. else
  657. return false;
  658. }
  659. /* Return the number of places WHAT appears within WHERE.
  660. Bail out when we found a reference occupying several hard registers. */
  661. static int
  662. count_occurrences_equiv (const_rtx what, const_rtx where)
  663. {
  664. int count = 0;
  665. subrtx_iterator::array_type array;
  666. FOR_EACH_SUBRTX (iter, array, where, NONCONST)
  667. {
  668. const_rtx x = *iter;
  669. if (REG_P (x) && REGNO (x) == REGNO (what))
  670. {
  671. /* Bail out if mode is different or more than one register is
  672. used. */
  673. if (GET_MODE (x) != GET_MODE (what)
  674. || (HARD_REGISTER_P (x)
  675. && hard_regno_nregs[REGNO (x)][GET_MODE (x)] > 1))
  676. return 0;
  677. count += 1;
  678. }
  679. else if (GET_CODE (x) == SUBREG
  680. && (!REG_P (SUBREG_REG (x))
  681. || REGNO (SUBREG_REG (x)) == REGNO (what)))
  682. /* ??? Do not support substituting regs inside subregs. In that case,
  683. simplify_subreg will be called by validate_replace_rtx, and
  684. unsubstitution will fail later. */
  685. return 0;
  686. }
  687. return count;
  688. }
  689. /* Returns TRUE if WHAT is found in WHERE rtx tree. */
  690. static bool
  691. rtx_ok_for_substitution_p (rtx what, rtx where)
  692. {
  693. return (count_occurrences_equiv (what, where) > 0);
  694. }
  695. /* Functions to support register renaming. */
  696. /* Substitute VI's set source with REGNO. Returns newly created pattern
  697. that has REGNO as its source. */
  698. static rtx_insn *
  699. create_insn_rtx_with_rhs (vinsn_t vi, rtx rhs_rtx)
  700. {
  701. rtx lhs_rtx;
  702. rtx pattern;
  703. rtx_insn *insn_rtx;
  704. lhs_rtx = copy_rtx (VINSN_LHS (vi));
  705. pattern = gen_rtx_SET (VOIDmode, lhs_rtx, rhs_rtx);
  706. insn_rtx = create_insn_rtx_from_pattern (pattern, NULL_RTX);
  707. return insn_rtx;
  708. }
  709. /* Returns whether INSN's src can be replaced with register number
  710. NEW_SRC_REG. E.g. the following insn is valid for i386:
  711. (insn:HI 2205 6585 2207 727 ../../gcc/libiberty/regex.c:3337
  712. (set (mem/s:QI (plus:SI (plus:SI (reg/f:SI 7 sp)
  713. (reg:SI 0 ax [orig:770 c1 ] [770]))
  714. (const_int 288 [0x120])) [0 str S1 A8])
  715. (const_int 0 [0x0])) 43 {*movqi_1} (nil)
  716. (nil))
  717. But if we change (const_int 0 [0x0]) to (reg:QI 4 si), it will be invalid
  718. because of operand constraints:
  719. (define_insn "*movqi_1"
  720. [(set (match_operand:QI 0 "nonimmediate_operand" "=q,q ,q ,r,r ,?r,m")
  721. (match_operand:QI 1 "general_operand" " q,qn,qm,q,rn,qm,qn")
  722. )]
  723. So do constrain_operands here, before choosing NEW_SRC_REG as best
  724. reg for rhs. */
  725. static bool
  726. replace_src_with_reg_ok_p (insn_t insn, rtx new_src_reg)
  727. {
  728. vinsn_t vi = INSN_VINSN (insn);
  729. machine_mode mode;
  730. rtx dst_loc;
  731. bool res;
  732. gcc_assert (VINSN_SEPARABLE_P (vi));
  733. get_dest_and_mode (insn, &dst_loc, &mode);
  734. gcc_assert (mode == GET_MODE (new_src_reg));
  735. if (REG_P (dst_loc) && REGNO (new_src_reg) == REGNO (dst_loc))
  736. return true;
  737. /* See whether SET_SRC can be replaced with this register. */
  738. validate_change (insn, &SET_SRC (PATTERN (insn)), new_src_reg, 1);
  739. res = verify_changes (0);
  740. cancel_changes (0);
  741. return res;
  742. }
  743. /* Returns whether INSN still be valid after replacing it's DEST with
  744. register NEW_REG. */
  745. static bool
  746. replace_dest_with_reg_ok_p (insn_t insn, rtx new_reg)
  747. {
  748. vinsn_t vi = INSN_VINSN (insn);
  749. bool res;
  750. /* We should deal here only with separable insns. */
  751. gcc_assert (VINSN_SEPARABLE_P (vi));
  752. gcc_assert (GET_MODE (VINSN_LHS (vi)) == GET_MODE (new_reg));
  753. /* See whether SET_DEST can be replaced with this register. */
  754. validate_change (insn, &SET_DEST (PATTERN (insn)), new_reg, 1);
  755. res = verify_changes (0);
  756. cancel_changes (0);
  757. return res;
  758. }
  759. /* Create a pattern with rhs of VI and lhs of LHS_RTX. */
  760. static rtx_insn *
  761. create_insn_rtx_with_lhs (vinsn_t vi, rtx lhs_rtx)
  762. {
  763. rtx rhs_rtx;
  764. rtx pattern;
  765. rtx_insn *insn_rtx;
  766. rhs_rtx = copy_rtx (VINSN_RHS (vi));
  767. pattern = gen_rtx_SET (VOIDmode, lhs_rtx, rhs_rtx);
  768. insn_rtx = create_insn_rtx_from_pattern (pattern, NULL_RTX);
  769. return insn_rtx;
  770. }
  771. /* Substitute lhs in the given expression EXPR for the register with number
  772. NEW_REGNO. SET_DEST may be arbitrary rtx, not only register. */
  773. static void
  774. replace_dest_with_reg_in_expr (expr_t expr, rtx new_reg)
  775. {
  776. rtx_insn *insn_rtx;
  777. vinsn_t vinsn;
  778. insn_rtx = create_insn_rtx_with_lhs (EXPR_VINSN (expr), new_reg);
  779. vinsn = create_vinsn_from_insn_rtx (insn_rtx, false);
  780. change_vinsn_in_expr (expr, vinsn);
  781. EXPR_WAS_RENAMED (expr) = 1;
  782. EXPR_TARGET_AVAILABLE (expr) = 1;
  783. }
  784. /* Returns whether VI writes either one of the USED_REGS registers or,
  785. if a register is a hard one, one of the UNAVAILABLE_HARD_REGS registers. */
  786. static bool
  787. vinsn_writes_one_of_regs_p (vinsn_t vi, regset used_regs,
  788. HARD_REG_SET unavailable_hard_regs)
  789. {
  790. unsigned regno;
  791. reg_set_iterator rsi;
  792. EXECUTE_IF_SET_IN_REG_SET (VINSN_REG_SETS (vi), 0, regno, rsi)
  793. {
  794. if (REGNO_REG_SET_P (used_regs, regno))
  795. return true;
  796. if (HARD_REGISTER_NUM_P (regno)
  797. && TEST_HARD_REG_BIT (unavailable_hard_regs, regno))
  798. return true;
  799. }
  800. EXECUTE_IF_SET_IN_REG_SET (VINSN_REG_CLOBBERS (vi), 0, regno, rsi)
  801. {
  802. if (REGNO_REG_SET_P (used_regs, regno))
  803. return true;
  804. if (HARD_REGISTER_NUM_P (regno)
  805. && TEST_HARD_REG_BIT (unavailable_hard_regs, regno))
  806. return true;
  807. }
  808. return false;
  809. }
  810. /* Returns register class of the output register in INSN.
  811. Returns NO_REGS for call insns because some targets have constraints on
  812. destination register of a call insn.
  813. Code adopted from regrename.c::build_def_use. */
  814. static enum reg_class
  815. get_reg_class (rtx_insn *insn)
  816. {
  817. int i, n_ops;
  818. extract_constrain_insn (insn);
  819. preprocess_constraints (insn);
  820. n_ops = recog_data.n_operands;
  821. const operand_alternative *op_alt = which_op_alt ();
  822. if (asm_noperands (PATTERN (insn)) > 0)
  823. {
  824. for (i = 0; i < n_ops; i++)
  825. if (recog_data.operand_type[i] == OP_OUT)
  826. {
  827. rtx *loc = recog_data.operand_loc[i];
  828. rtx op = *loc;
  829. enum reg_class cl = alternative_class (op_alt, i);
  830. if (REG_P (op)
  831. && REGNO (op) == ORIGINAL_REGNO (op))
  832. continue;
  833. return cl;
  834. }
  835. }
  836. else if (!CALL_P (insn))
  837. {
  838. for (i = 0; i < n_ops + recog_data.n_dups; i++)
  839. {
  840. int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
  841. enum reg_class cl = alternative_class (op_alt, opn);
  842. if (recog_data.operand_type[opn] == OP_OUT ||
  843. recog_data.operand_type[opn] == OP_INOUT)
  844. return cl;
  845. }
  846. }
  847. /* Insns like
  848. (insn (set (reg:CCZ 17 flags) (compare:CCZ ...)))
  849. may result in returning NO_REGS, cause flags is written implicitly through
  850. CMP insn, which has no OP_OUT | OP_INOUT operands. */
  851. return NO_REGS;
  852. }
  853. #ifdef HARD_REGNO_RENAME_OK
  854. /* Calculate HARD_REGNO_RENAME_OK data for REGNO. */
  855. static void
  856. init_hard_regno_rename (int regno)
  857. {
  858. int cur_reg;
  859. SET_HARD_REG_BIT (sel_hrd.regs_for_rename[regno], regno);
  860. for (cur_reg = 0; cur_reg < FIRST_PSEUDO_REGISTER; cur_reg++)
  861. {
  862. /* We are not interested in renaming in other regs. */
  863. if (!TEST_HARD_REG_BIT (sel_hrd.regs_ever_used, cur_reg))
  864. continue;
  865. if (HARD_REGNO_RENAME_OK (regno, cur_reg))
  866. SET_HARD_REG_BIT (sel_hrd.regs_for_rename[regno], cur_reg);
  867. }
  868. }
  869. #endif
  870. /* A wrapper around HARD_REGNO_RENAME_OK that will look into the hard regs
  871. data first. */
  872. static inline bool
  873. sel_hard_regno_rename_ok (int from ATTRIBUTE_UNUSED, int to ATTRIBUTE_UNUSED)
  874. {
  875. #ifdef HARD_REGNO_RENAME_OK
  876. /* Check whether this is all calculated. */
  877. if (TEST_HARD_REG_BIT (sel_hrd.regs_for_rename[from], from))
  878. return TEST_HARD_REG_BIT (sel_hrd.regs_for_rename[from], to);
  879. init_hard_regno_rename (from);
  880. return TEST_HARD_REG_BIT (sel_hrd.regs_for_rename[from], to);
  881. #else
  882. return true;
  883. #endif
  884. }
  885. /* Calculate set of registers that are capable of holding MODE. */
  886. static void
  887. init_regs_for_mode (machine_mode mode)
  888. {
  889. int cur_reg;
  890. CLEAR_HARD_REG_SET (sel_hrd.regs_for_mode[mode]);
  891. CLEAR_HARD_REG_SET (sel_hrd.regs_for_call_clobbered[mode]);
  892. for (cur_reg = 0; cur_reg < FIRST_PSEUDO_REGISTER; cur_reg++)
  893. {
  894. int nregs;
  895. int i;
  896. /* See whether it accepts all modes that occur in
  897. original insns. */
  898. if (! HARD_REGNO_MODE_OK (cur_reg, mode))
  899. continue;
  900. nregs = hard_regno_nregs[cur_reg][mode];
  901. for (i = nregs - 1; i >= 0; --i)
  902. if (fixed_regs[cur_reg + i]
  903. || global_regs[cur_reg + i]
  904. /* Can't use regs which aren't saved by
  905. the prologue. */
  906. || !TEST_HARD_REG_BIT (sel_hrd.regs_ever_used, cur_reg + i)
  907. /* Can't use regs with non-null REG_BASE_VALUE, because adjusting
  908. it affects aliasing globally and invalidates all AV sets. */
  909. || get_reg_base_value (cur_reg + i)
  910. #ifdef LEAF_REGISTERS
  911. /* We can't use a non-leaf register if we're in a
  912. leaf function. */
  913. || (crtl->is_leaf
  914. && !LEAF_REGISTERS[cur_reg + i])
  915. #endif
  916. )
  917. break;
  918. if (i >= 0)
  919. continue;
  920. if (HARD_REGNO_CALL_PART_CLOBBERED (cur_reg, mode))
  921. SET_HARD_REG_BIT (sel_hrd.regs_for_call_clobbered[mode],
  922. cur_reg);
  923. /* If the CUR_REG passed all the checks above,
  924. then it's ok. */
  925. SET_HARD_REG_BIT (sel_hrd.regs_for_mode[mode], cur_reg);
  926. }
  927. sel_hrd.regs_for_mode_ok[mode] = true;
  928. }
  929. /* Init all register sets gathered in HRD. */
  930. static void
  931. init_hard_regs_data (void)
  932. {
  933. int cur_reg = 0;
  934. int cur_mode = 0;
  935. CLEAR_HARD_REG_SET (sel_hrd.regs_ever_used);
  936. for (cur_reg = 0; cur_reg < FIRST_PSEUDO_REGISTER; cur_reg++)
  937. if (df_regs_ever_live_p (cur_reg) || call_used_regs[cur_reg])
  938. SET_HARD_REG_BIT (sel_hrd.regs_ever_used, cur_reg);
  939. /* Initialize registers that are valid based on mode when this is
  940. really needed. */
  941. for (cur_mode = 0; cur_mode < NUM_MACHINE_MODES; cur_mode++)
  942. sel_hrd.regs_for_mode_ok[cur_mode] = false;
  943. /* Mark that all HARD_REGNO_RENAME_OK is not calculated. */
  944. for (cur_reg = 0; cur_reg < FIRST_PSEUDO_REGISTER; cur_reg++)
  945. CLEAR_HARD_REG_SET (sel_hrd.regs_for_rename[cur_reg]);
  946. #ifdef STACK_REGS
  947. CLEAR_HARD_REG_SET (sel_hrd.stack_regs);
  948. for (cur_reg = FIRST_STACK_REG; cur_reg <= LAST_STACK_REG; cur_reg++)
  949. SET_HARD_REG_BIT (sel_hrd.stack_regs, cur_reg);
  950. #endif
  951. }
  952. /* Mark hardware regs in REG_RENAME_P that are not suitable
  953. for renaming rhs in INSN due to hardware restrictions (register class,
  954. modes compatibility etc). This doesn't affect original insn's dest reg,
  955. if it isn't in USED_REGS. DEF is a definition insn of rhs for which the
  956. destination register is sought. LHS (DEF->ORIG_INSN) may be REG or MEM.
  957. Registers that are in used_regs are always marked in
  958. unavailable_hard_regs as well. */
  959. static void
  960. mark_unavailable_hard_regs (def_t def, struct reg_rename *reg_rename_p,
  961. regset used_regs ATTRIBUTE_UNUSED)
  962. {
  963. machine_mode mode;
  964. enum reg_class cl = NO_REGS;
  965. rtx orig_dest;
  966. unsigned cur_reg, regno;
  967. hard_reg_set_iterator hrsi;
  968. gcc_assert (GET_CODE (PATTERN (def->orig_insn)) == SET);
  969. gcc_assert (reg_rename_p);
  970. orig_dest = SET_DEST (PATTERN (def->orig_insn));
  971. /* We have decided not to rename 'mem = something;' insns, as 'something'
  972. is usually a register. */
  973. if (!REG_P (orig_dest))
  974. return;
  975. regno = REGNO (orig_dest);
  976. /* If before reload, don't try to work with pseudos. */
  977. if (!reload_completed && !HARD_REGISTER_NUM_P (regno))
  978. return;
  979. if (reload_completed)
  980. cl = get_reg_class (def->orig_insn);
  981. /* Stop if the original register is one of the fixed_regs, global_regs or
  982. frame pointer, or we could not discover its class. */
  983. if (fixed_regs[regno]
  984. || global_regs[regno]
  985. #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
  986. || (frame_pointer_needed && regno == HARD_FRAME_POINTER_REGNUM)
  987. #else
  988. || (frame_pointer_needed && regno == FRAME_POINTER_REGNUM)
  989. #endif
  990. || (reload_completed && cl == NO_REGS))
  991. {
  992. SET_HARD_REG_SET (reg_rename_p->unavailable_hard_regs);
  993. /* Give a chance for original register, if it isn't in used_regs. */
  994. if (!def->crosses_call)
  995. CLEAR_HARD_REG_BIT (reg_rename_p->unavailable_hard_regs, regno);
  996. return;
  997. }
  998. /* If something allocated on stack in this function, mark frame pointer
  999. register unavailable, considering also modes.
  1000. FIXME: it is enough to do this once per all original defs. */
  1001. if (frame_pointer_needed)
  1002. {
  1003. add_to_hard_reg_set (&reg_rename_p->unavailable_hard_regs,
  1004. Pmode, FRAME_POINTER_REGNUM);
  1005. if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
  1006. add_to_hard_reg_set (&reg_rename_p->unavailable_hard_regs,
  1007. Pmode, HARD_FRAME_POINTER_REGNUM);
  1008. }
  1009. #ifdef STACK_REGS
  1010. /* For the stack registers the presence of FIRST_STACK_REG in USED_REGS
  1011. is equivalent to as if all stack regs were in this set.
  1012. I.e. no stack register can be renamed, and even if it's an original
  1013. register here we make sure it won't be lifted over it's previous def
  1014. (it's previous def will appear as if it's a FIRST_STACK_REG def.
  1015. The HARD_REGNO_RENAME_OK covers other cases in condition below. */
  1016. if (IN_RANGE (REGNO (orig_dest), FIRST_STACK_REG, LAST_STACK_REG)
  1017. && REGNO_REG_SET_P (used_regs, FIRST_STACK_REG))
  1018. IOR_HARD_REG_SET (reg_rename_p->unavailable_hard_regs,
  1019. sel_hrd.stack_regs);
  1020. #endif
  1021. /* If there's a call on this path, make regs from call_used_reg_set
  1022. unavailable. */
  1023. if (def->crosses_call)
  1024. IOR_HARD_REG_SET (reg_rename_p->unavailable_hard_regs,
  1025. call_used_reg_set);
  1026. /* Stop here before reload: we need FRAME_REGS, STACK_REGS, and crosses_call,
  1027. but not register classes. */
  1028. if (!reload_completed)
  1029. return;
  1030. /* Leave regs as 'available' only from the current
  1031. register class. */
  1032. COPY_HARD_REG_SET (reg_rename_p->available_for_renaming,
  1033. reg_class_contents[cl]);
  1034. mode = GET_MODE (orig_dest);
  1035. /* Leave only registers available for this mode. */
  1036. if (!sel_hrd.regs_for_mode_ok[mode])
  1037. init_regs_for_mode (mode);
  1038. AND_HARD_REG_SET (reg_rename_p->available_for_renaming,
  1039. sel_hrd.regs_for_mode[mode]);
  1040. /* Exclude registers that are partially call clobbered. */
  1041. if (def->crosses_call
  1042. && ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
  1043. AND_COMPL_HARD_REG_SET (reg_rename_p->available_for_renaming,
  1044. sel_hrd.regs_for_call_clobbered[mode]);
  1045. /* Leave only those that are ok to rename. */
  1046. EXECUTE_IF_SET_IN_HARD_REG_SET (reg_rename_p->available_for_renaming,
  1047. 0, cur_reg, hrsi)
  1048. {
  1049. int nregs;
  1050. int i;
  1051. nregs = hard_regno_nregs[cur_reg][mode];
  1052. gcc_assert (nregs > 0);
  1053. for (i = nregs - 1; i >= 0; --i)
  1054. if (! sel_hard_regno_rename_ok (regno + i, cur_reg + i))
  1055. break;
  1056. if (i >= 0)
  1057. CLEAR_HARD_REG_BIT (reg_rename_p->available_for_renaming,
  1058. cur_reg);
  1059. }
  1060. AND_COMPL_HARD_REG_SET (reg_rename_p->available_for_renaming,
  1061. reg_rename_p->unavailable_hard_regs);
  1062. /* Regno is always ok from the renaming part of view, but it really
  1063. could be in *unavailable_hard_regs already, so set it here instead
  1064. of there. */
  1065. SET_HARD_REG_BIT (reg_rename_p->available_for_renaming, regno);
  1066. }
  1067. /* reg_rename_tick[REG1] > reg_rename_tick[REG2] if REG1 was chosen as the
  1068. best register more recently than REG2. */
  1069. static int reg_rename_tick[FIRST_PSEUDO_REGISTER];
  1070. /* Indicates the number of times renaming happened before the current one. */
  1071. static int reg_rename_this_tick;
  1072. /* Choose the register among free, that is suitable for storing
  1073. the rhs value.
  1074. ORIGINAL_INSNS is the list of insns where the operation (rhs)
  1075. originally appears. There could be multiple original operations
  1076. for single rhs since we moving it up and merging along different
  1077. paths.
  1078. Some code is adapted from regrename.c (regrename_optimize).
  1079. If original register is available, function returns it.
  1080. Otherwise it performs the checks, so the new register should
  1081. comply with the following:
  1082. - it should not violate any live ranges (such registers are in
  1083. REG_RENAME_P->available_for_renaming set);
  1084. - it should not be in the HARD_REGS_USED regset;
  1085. - it should be in the class compatible with original uses;
  1086. - it should not be clobbered through reference with different mode;
  1087. - if we're in the leaf function, then the new register should
  1088. not be in the LEAF_REGISTERS;
  1089. - etc.
  1090. If several registers meet the conditions, the register with smallest
  1091. tick is returned to achieve more even register allocation.
  1092. If original register seems to be ok, we set *IS_ORIG_REG_P_PTR to true.
  1093. If no register satisfies the above conditions, NULL_RTX is returned. */
  1094. static rtx
  1095. choose_best_reg_1 (HARD_REG_SET hard_regs_used,
  1096. struct reg_rename *reg_rename_p,
  1097. def_list_t original_insns, bool *is_orig_reg_p_ptr)
  1098. {
  1099. int best_new_reg;
  1100. unsigned cur_reg;
  1101. machine_mode mode = VOIDmode;
  1102. unsigned regno, i, n;
  1103. hard_reg_set_iterator hrsi;
  1104. def_list_iterator di;
  1105. def_t def;
  1106. /* If original register is available, return it. */
  1107. *is_orig_reg_p_ptr = true;
  1108. FOR_EACH_DEF (def, di, original_insns)
  1109. {
  1110. rtx orig_dest = SET_DEST (PATTERN (def->orig_insn));
  1111. gcc_assert (REG_P (orig_dest));
  1112. /* Check that all original operations have the same mode.
  1113. This is done for the next loop; if we'd return from this
  1114. loop, we'd check only part of them, but in this case
  1115. it doesn't matter. */
  1116. if (mode == VOIDmode)
  1117. mode = GET_MODE (orig_dest);
  1118. gcc_assert (mode == GET_MODE (orig_dest));
  1119. regno = REGNO (orig_dest);
  1120. for (i = 0, n = hard_regno_nregs[regno][mode]; i < n; i++)
  1121. if (TEST_HARD_REG_BIT (hard_regs_used, regno + i))
  1122. break;
  1123. /* All hard registers are available. */
  1124. if (i == n)
  1125. {
  1126. gcc_assert (mode != VOIDmode);
  1127. /* Hard registers should not be shared. */
  1128. return gen_rtx_REG (mode, regno);
  1129. }
  1130. }
  1131. *is_orig_reg_p_ptr = false;
  1132. best_new_reg = -1;
  1133. /* Among all available regs choose the register that was
  1134. allocated earliest. */
  1135. EXECUTE_IF_SET_IN_HARD_REG_SET (reg_rename_p->available_for_renaming,
  1136. 0, cur_reg, hrsi)
  1137. if (! TEST_HARD_REG_BIT (hard_regs_used, cur_reg))
  1138. {
  1139. /* Check that all hard regs for mode are available. */
  1140. for (i = 1, n = hard_regno_nregs[cur_reg][mode]; i < n; i++)
  1141. if (TEST_HARD_REG_BIT (hard_regs_used, cur_reg + i)
  1142. || !TEST_HARD_REG_BIT (reg_rename_p->available_for_renaming,
  1143. cur_reg + i))
  1144. break;
  1145. if (i < n)
  1146. continue;
  1147. /* All hard registers are available. */
  1148. if (best_new_reg < 0
  1149. || reg_rename_tick[cur_reg] < reg_rename_tick[best_new_reg])
  1150. {
  1151. best_new_reg = cur_reg;
  1152. /* Return immediately when we know there's no better reg. */
  1153. if (! reg_rename_tick[best_new_reg])
  1154. break;
  1155. }
  1156. }
  1157. if (best_new_reg >= 0)
  1158. {
  1159. /* Use the check from the above loop. */
  1160. gcc_assert (mode != VOIDmode);
  1161. return gen_rtx_REG (mode, best_new_reg);
  1162. }
  1163. return NULL_RTX;
  1164. }
  1165. /* A wrapper around choose_best_reg_1 () to verify that we make correct
  1166. assumptions about available registers in the function. */
  1167. static rtx
  1168. choose_best_reg (HARD_REG_SET hard_regs_used, struct reg_rename *reg_rename_p,
  1169. def_list_t original_insns, bool *is_orig_reg_p_ptr)
  1170. {
  1171. rtx best_reg = choose_best_reg_1 (hard_regs_used, reg_rename_p,
  1172. original_insns, is_orig_reg_p_ptr);
  1173. /* FIXME loop over hard_regno_nregs here. */
  1174. gcc_assert (best_reg == NULL_RTX
  1175. || TEST_HARD_REG_BIT (sel_hrd.regs_ever_used, REGNO (best_reg)));
  1176. return best_reg;
  1177. }
  1178. /* Choose the pseudo register for storing rhs value. As this is supposed
  1179. to work before reload, we return either the original register or make
  1180. the new one. The parameters are the same that in choose_nest_reg_1
  1181. functions, except that USED_REGS may contain pseudos.
  1182. If we work with hard regs, check also REG_RENAME_P->UNAVAILABLE_HARD_REGS.
  1183. TODO: take into account register pressure while doing this. Up to this
  1184. moment, this function would never return NULL for pseudos, but we should
  1185. not rely on this. */
  1186. static rtx
  1187. choose_best_pseudo_reg (regset used_regs,
  1188. struct reg_rename *reg_rename_p,
  1189. def_list_t original_insns, bool *is_orig_reg_p_ptr)
  1190. {
  1191. def_list_iterator i;
  1192. def_t def;
  1193. machine_mode mode = VOIDmode;
  1194. bool bad_hard_regs = false;
  1195. /* We should not use this after reload. */
  1196. gcc_assert (!reload_completed);
  1197. /* If original register is available, return it. */
  1198. *is_orig_reg_p_ptr = true;
  1199. FOR_EACH_DEF (def, i, original_insns)
  1200. {
  1201. rtx dest = SET_DEST (PATTERN (def->orig_insn));
  1202. int orig_regno;
  1203. gcc_assert (REG_P (dest));
  1204. /* Check that all original operations have the same mode. */
  1205. if (mode == VOIDmode)
  1206. mode = GET_MODE (dest);
  1207. else
  1208. gcc_assert (mode == GET_MODE (dest));
  1209. orig_regno = REGNO (dest);
  1210. if (!REGNO_REG_SET_P (used_regs, orig_regno))
  1211. {
  1212. if (orig_regno < FIRST_PSEUDO_REGISTER)
  1213. {
  1214. gcc_assert (df_regs_ever_live_p (orig_regno));
  1215. /* For hard registers, we have to check hardware imposed
  1216. limitations (frame/stack registers, calls crossed). */
  1217. if (!TEST_HARD_REG_BIT (reg_rename_p->unavailable_hard_regs,
  1218. orig_regno))
  1219. {
  1220. /* Don't let register cross a call if it doesn't already
  1221. cross one. This condition is written in accordance with
  1222. that in sched-deps.c sched_analyze_reg(). */
  1223. if (!reg_rename_p->crosses_call
  1224. || REG_N_CALLS_CROSSED (orig_regno) > 0)
  1225. return gen_rtx_REG (mode, orig_regno);
  1226. }
  1227. bad_hard_regs = true;
  1228. }
  1229. else
  1230. return dest;
  1231. }
  1232. }
  1233. *is_orig_reg_p_ptr = false;
  1234. /* We had some original hard registers that couldn't be used.
  1235. Those were likely special. Don't try to create a pseudo. */
  1236. if (bad_hard_regs)
  1237. return NULL_RTX;
  1238. /* We haven't found a register from original operations. Get a new one.
  1239. FIXME: control register pressure somehow. */
  1240. {
  1241. rtx new_reg = gen_reg_rtx (mode);
  1242. gcc_assert (mode != VOIDmode);
  1243. max_regno = max_reg_num ();
  1244. maybe_extend_reg_info_p ();
  1245. REG_N_CALLS_CROSSED (REGNO (new_reg)) = reg_rename_p->crosses_call ? 1 : 0;
  1246. return new_reg;
  1247. }
  1248. }
  1249. /* True when target of EXPR is available due to EXPR_TARGET_AVAILABLE,
  1250. USED_REGS and REG_RENAME_P->UNAVAILABLE_HARD_REGS. */
  1251. static void
  1252. verify_target_availability (expr_t expr, regset used_regs,
  1253. struct reg_rename *reg_rename_p)
  1254. {
  1255. unsigned n, i, regno;
  1256. machine_mode mode;
  1257. bool target_available, live_available, hard_available;
  1258. if (!REG_P (EXPR_LHS (expr)) || EXPR_TARGET_AVAILABLE (expr) < 0)
  1259. return;
  1260. regno = expr_dest_regno (expr);
  1261. mode = GET_MODE (EXPR_LHS (expr));
  1262. target_available = EXPR_TARGET_AVAILABLE (expr) == 1;
  1263. n = HARD_REGISTER_NUM_P (regno) ? hard_regno_nregs[regno][mode] : 1;
  1264. live_available = hard_available = true;
  1265. for (i = 0; i < n; i++)
  1266. {
  1267. if (bitmap_bit_p (used_regs, regno + i))
  1268. live_available = false;
  1269. if (TEST_HARD_REG_BIT (reg_rename_p->unavailable_hard_regs, regno + i))
  1270. hard_available = false;
  1271. }
  1272. /* When target is not available, it may be due to hard register
  1273. restrictions, e.g. crosses calls, so we check hard_available too. */
  1274. if (target_available)
  1275. gcc_assert (live_available);
  1276. else
  1277. /* Check only if we haven't scheduled something on the previous fence,
  1278. cause due to MAX_SOFTWARE_LOOKAHEAD_WINDOW_SIZE issues
  1279. and having more than one fence, we may end having targ_un in a block
  1280. in which successors target register is actually available.
  1281. The last condition handles the case when a dependence from a call insn
  1282. was created in sched-deps.c for insns with destination registers that
  1283. never crossed a call before, but do cross one after our code motion.
  1284. FIXME: in the latter case, we just uselessly called find_used_regs,
  1285. because we can't move this expression with any other register
  1286. as well. */
  1287. gcc_assert (scheduled_something_on_previous_fence || !live_available
  1288. || !hard_available
  1289. || (!reload_completed && reg_rename_p->crosses_call
  1290. && REG_N_CALLS_CROSSED (regno) == 0));
  1291. }
  1292. /* Collect unavailable registers due to liveness for EXPR from BNDS
  1293. into USED_REGS. Save additional information about available
  1294. registers and unavailable due to hardware restriction registers
  1295. into REG_RENAME_P structure. Save original insns into ORIGINAL_INSNS
  1296. list. */
  1297. static void
  1298. collect_unavailable_regs_from_bnds (expr_t expr, blist_t bnds, regset used_regs,
  1299. struct reg_rename *reg_rename_p,
  1300. def_list_t *original_insns)
  1301. {
  1302. for (; bnds; bnds = BLIST_NEXT (bnds))
  1303. {
  1304. bool res;
  1305. av_set_t orig_ops = NULL;
  1306. bnd_t bnd = BLIST_BND (bnds);
  1307. /* If the chosen best expr doesn't belong to current boundary,
  1308. skip it. */
  1309. if (!av_set_is_in_p (BND_AV1 (bnd), EXPR_VINSN (expr)))
  1310. continue;
  1311. /* Put in ORIG_OPS all exprs from this boundary that became
  1312. RES on top. */
  1313. orig_ops = find_sequential_best_exprs (bnd, expr, false);
  1314. /* Compute used regs and OR it into the USED_REGS. */
  1315. res = find_used_regs (BND_TO (bnd), orig_ops, used_regs,
  1316. reg_rename_p, original_insns);
  1317. /* FIXME: the assert is true until we'd have several boundaries. */
  1318. gcc_assert (res);
  1319. av_set_clear (&orig_ops);
  1320. }
  1321. }
  1322. /* Return TRUE if it is possible to replace LHSes of ORIG_INSNS with BEST_REG.
  1323. If BEST_REG is valid, replace LHS of EXPR with it. */
  1324. static bool
  1325. try_replace_dest_reg (ilist_t orig_insns, rtx best_reg, expr_t expr)
  1326. {
  1327. /* Try whether we'll be able to generate the insn
  1328. 'dest := best_reg' at the place of the original operation. */
  1329. for (; orig_insns; orig_insns = ILIST_NEXT (orig_insns))
  1330. {
  1331. insn_t orig_insn = DEF_LIST_DEF (orig_insns)->orig_insn;
  1332. gcc_assert (EXPR_SEPARABLE_P (INSN_EXPR (orig_insn)));
  1333. if (REGNO (best_reg) != REGNO (INSN_LHS (orig_insn))
  1334. && (! replace_src_with_reg_ok_p (orig_insn, best_reg)
  1335. || ! replace_dest_with_reg_ok_p (orig_insn, best_reg)))
  1336. return false;
  1337. }
  1338. /* Make sure that EXPR has the right destination
  1339. register. */
  1340. if (expr_dest_regno (expr) != REGNO (best_reg))
  1341. replace_dest_with_reg_in_expr (expr, best_reg);
  1342. else
  1343. EXPR_TARGET_AVAILABLE (expr) = 1;
  1344. return true;
  1345. }
  1346. /* Select and assign best register to EXPR searching from BNDS.
  1347. Set *IS_ORIG_REG_P to TRUE if original register was selected.
  1348. Return FALSE if no register can be chosen, which could happen when:
  1349. * EXPR_SEPARABLE_P is true but we were unable to find suitable register;
  1350. * EXPR_SEPARABLE_P is false but the insn sets/clobbers one of the registers
  1351. that are used on the moving path. */
  1352. static bool
  1353. find_best_reg_for_expr (expr_t expr, blist_t bnds, bool *is_orig_reg_p)
  1354. {
  1355. static struct reg_rename reg_rename_data;
  1356. regset used_regs;
  1357. def_list_t original_insns = NULL;
  1358. bool reg_ok;
  1359. *is_orig_reg_p = false;
  1360. /* Don't bother to do anything if this insn doesn't set any registers. */
  1361. if (bitmap_empty_p (VINSN_REG_SETS (EXPR_VINSN (expr)))
  1362. && bitmap_empty_p (VINSN_REG_CLOBBERS (EXPR_VINSN (expr))))
  1363. return true;
  1364. used_regs = get_clear_regset_from_pool ();
  1365. CLEAR_HARD_REG_SET (reg_rename_data.unavailable_hard_regs);
  1366. collect_unavailable_regs_from_bnds (expr, bnds, used_regs, &reg_rename_data,
  1367. &original_insns);
  1368. #ifdef ENABLE_CHECKING
  1369. /* If after reload, make sure we're working with hard regs here. */
  1370. if (reload_completed)
  1371. {
  1372. reg_set_iterator rsi;
  1373. unsigned i;
  1374. EXECUTE_IF_SET_IN_REG_SET (used_regs, FIRST_PSEUDO_REGISTER, i, rsi)
  1375. gcc_unreachable ();
  1376. }
  1377. #endif
  1378. if (EXPR_SEPARABLE_P (expr))
  1379. {
  1380. rtx best_reg = NULL_RTX;
  1381. /* Check that we have computed availability of a target register
  1382. correctly. */
  1383. verify_target_availability (expr, used_regs, &reg_rename_data);
  1384. /* Turn everything in hard regs after reload. */
  1385. if (reload_completed)
  1386. {
  1387. HARD_REG_SET hard_regs_used;
  1388. REG_SET_TO_HARD_REG_SET (hard_regs_used, used_regs);
  1389. /* Join hard registers unavailable due to register class
  1390. restrictions and live range intersection. */
  1391. IOR_HARD_REG_SET (hard_regs_used,
  1392. reg_rename_data.unavailable_hard_regs);
  1393. best_reg = choose_best_reg (hard_regs_used, &reg_rename_data,
  1394. original_insns, is_orig_reg_p);
  1395. }
  1396. else
  1397. best_reg = choose_best_pseudo_reg (used_regs, &reg_rename_data,
  1398. original_insns, is_orig_reg_p);
  1399. if (!best_reg)
  1400. reg_ok = false;
  1401. else if (*is_orig_reg_p)
  1402. {
  1403. /* In case of unification BEST_REG may be different from EXPR's LHS
  1404. when EXPR's LHS is unavailable, and there is another LHS among
  1405. ORIGINAL_INSNS. */
  1406. reg_ok = try_replace_dest_reg (original_insns, best_reg, expr);
  1407. }
  1408. else
  1409. {
  1410. /* Forbid renaming of low-cost insns. */
  1411. if (sel_vinsn_cost (EXPR_VINSN (expr)) < 2)
  1412. reg_ok = false;
  1413. else
  1414. reg_ok = try_replace_dest_reg (original_insns, best_reg, expr);
  1415. }
  1416. }
  1417. else
  1418. {
  1419. /* If !EXPR_SCHEDULE_AS_RHS (EXPR), just make sure INSN doesn't set
  1420. any of the HARD_REGS_USED set. */
  1421. if (vinsn_writes_one_of_regs_p (EXPR_VINSN (expr), used_regs,
  1422. reg_rename_data.unavailable_hard_regs))
  1423. {
  1424. reg_ok = false;
  1425. gcc_assert (EXPR_TARGET_AVAILABLE (expr) <= 0);
  1426. }
  1427. else
  1428. {
  1429. reg_ok = true;
  1430. gcc_assert (EXPR_TARGET_AVAILABLE (expr) != 0);
  1431. }
  1432. }
  1433. ilist_clear (&original_insns);
  1434. return_regset_to_pool (used_regs);
  1435. return reg_ok;
  1436. }
  1437. /* Return true if dependence described by DS can be overcomed. */
  1438. static bool
  1439. can_speculate_dep_p (ds_t ds)
  1440. {
  1441. if (spec_info == NULL)
  1442. return false;
  1443. /* Leave only speculative data. */
  1444. ds &= SPECULATIVE;
  1445. if (ds == 0)
  1446. return false;
  1447. {
  1448. /* FIXME: make sched-deps.c produce only those non-hard dependencies,
  1449. that we can overcome. */
  1450. ds_t spec_mask = spec_info->mask;
  1451. if ((ds & spec_mask) != ds)
  1452. return false;
  1453. }
  1454. if (ds_weak (ds) < spec_info->data_weakness_cutoff)
  1455. return false;
  1456. return true;
  1457. }
  1458. /* Get a speculation check instruction.
  1459. C_EXPR is a speculative expression,
  1460. CHECK_DS describes speculations that should be checked,
  1461. ORIG_INSN is the original non-speculative insn in the stream. */
  1462. static insn_t
  1463. create_speculation_check (expr_t c_expr, ds_t check_ds, insn_t orig_insn)
  1464. {
  1465. rtx check_pattern;
  1466. rtx_insn *insn_rtx;
  1467. insn_t insn;
  1468. basic_block recovery_block;
  1469. rtx_insn *label;
  1470. /* Create a recovery block if target is going to emit branchy check, or if
  1471. ORIG_INSN was speculative already. */
  1472. if (targetm.sched.needs_block_p (check_ds)
  1473. || EXPR_SPEC_DONE_DS (INSN_EXPR (orig_insn)) != 0)
  1474. {
  1475. recovery_block = sel_create_recovery_block (orig_insn);
  1476. label = BB_HEAD (recovery_block);
  1477. }
  1478. else
  1479. {
  1480. recovery_block = NULL;
  1481. label = NULL;
  1482. }
  1483. /* Get pattern of the check. */
  1484. check_pattern = targetm.sched.gen_spec_check (EXPR_INSN_RTX (c_expr), label,
  1485. check_ds);
  1486. gcc_assert (check_pattern != NULL);
  1487. /* Emit check. */
  1488. insn_rtx = create_insn_rtx_from_pattern (check_pattern, label);
  1489. insn = sel_gen_insn_from_rtx_after (insn_rtx, INSN_EXPR (orig_insn),
  1490. INSN_SEQNO (orig_insn), orig_insn);
  1491. /* Make check to be non-speculative. */
  1492. EXPR_SPEC_DONE_DS (INSN_EXPR (insn)) = 0;
  1493. INSN_SPEC_CHECKED_DS (insn) = check_ds;
  1494. /* Decrease priority of check by difference of load/check instruction
  1495. latencies. */
  1496. EXPR_PRIORITY (INSN_EXPR (insn)) -= (sel_vinsn_cost (INSN_VINSN (orig_insn))
  1497. - sel_vinsn_cost (INSN_VINSN (insn)));
  1498. /* Emit copy of original insn (though with replaced target register,
  1499. if needed) to the recovery block. */
  1500. if (recovery_block != NULL)
  1501. {
  1502. rtx twin_rtx;
  1503. twin_rtx = copy_rtx (PATTERN (EXPR_INSN_RTX (c_expr)));
  1504. twin_rtx = create_insn_rtx_from_pattern (twin_rtx, NULL_RTX);
  1505. sel_gen_recovery_insn_from_rtx_after (twin_rtx,
  1506. INSN_EXPR (orig_insn),
  1507. INSN_SEQNO (insn),
  1508. bb_note (recovery_block));
  1509. }
  1510. /* If we've generated a data speculation check, make sure
  1511. that all the bookkeeping instruction we'll create during
  1512. this move_op () will allocate an ALAT entry so that the
  1513. check won't fail.
  1514. In case of control speculation we must convert C_EXPR to control
  1515. speculative mode, because failing to do so will bring us an exception
  1516. thrown by the non-control-speculative load. */
  1517. check_ds = ds_get_max_dep_weak (check_ds);
  1518. speculate_expr (c_expr, check_ds);
  1519. return insn;
  1520. }
  1521. /* True when INSN is a "regN = regN" copy. */
  1522. static bool
  1523. identical_copy_p (rtx insn)
  1524. {
  1525. rtx lhs, rhs, pat;
  1526. pat = PATTERN (insn);
  1527. if (GET_CODE (pat) != SET)
  1528. return false;
  1529. lhs = SET_DEST (pat);
  1530. if (!REG_P (lhs))
  1531. return false;
  1532. rhs = SET_SRC (pat);
  1533. if (!REG_P (rhs))
  1534. return false;
  1535. return REGNO (lhs) == REGNO (rhs);
  1536. }
  1537. /* Undo all transformations on *AV_PTR that were done when
  1538. moving through INSN. */
  1539. static void
  1540. undo_transformations (av_set_t *av_ptr, rtx_insn *insn)
  1541. {
  1542. av_set_iterator av_iter;
  1543. expr_t expr;
  1544. av_set_t new_set = NULL;
  1545. /* First, kill any EXPR that uses registers set by an insn. This is
  1546. required for correctness. */
  1547. FOR_EACH_EXPR_1 (expr, av_iter, av_ptr)
  1548. if (!sched_insns_conditions_mutex_p (insn, EXPR_INSN_RTX (expr))
  1549. && bitmap_intersect_p (INSN_REG_SETS (insn),
  1550. VINSN_REG_USES (EXPR_VINSN (expr)))
  1551. /* When an insn looks like 'r1 = r1', we could substitute through
  1552. it, but the above condition will still hold. This happened with
  1553. gcc.c-torture/execute/961125-1.c. */
  1554. && !identical_copy_p (insn))
  1555. {
  1556. if (sched_verbose >= 6)
  1557. sel_print ("Expr %d removed due to use/set conflict\n",
  1558. INSN_UID (EXPR_INSN_RTX (expr)));
  1559. av_set_iter_remove (&av_iter);
  1560. }
  1561. /* Undo transformations looking at the history vector. */
  1562. FOR_EACH_EXPR (expr, av_iter, *av_ptr)
  1563. {
  1564. int index = find_in_history_vect (EXPR_HISTORY_OF_CHANGES (expr),
  1565. insn, EXPR_VINSN (expr), true);
  1566. if (index >= 0)
  1567. {
  1568. expr_history_def *phist;
  1569. phist = &EXPR_HISTORY_OF_CHANGES (expr)[index];
  1570. switch (phist->type)
  1571. {
  1572. case TRANS_SPECULATION:
  1573. {
  1574. ds_t old_ds, new_ds;
  1575. /* Compute the difference between old and new speculative
  1576. statuses: that's what we need to check.
  1577. Earlier we used to assert that the status will really
  1578. change. This no longer works because only the probability
  1579. bits in the status may have changed during compute_av_set,
  1580. and in the case of merging different probabilities of the
  1581. same speculative status along different paths we do not
  1582. record this in the history vector. */
  1583. old_ds = phist->spec_ds;
  1584. new_ds = EXPR_SPEC_DONE_DS (expr);
  1585. old_ds &= SPECULATIVE;
  1586. new_ds &= SPECULATIVE;
  1587. new_ds &= ~old_ds;
  1588. EXPR_SPEC_TO_CHECK_DS (expr) |= new_ds;
  1589. break;
  1590. }
  1591. case TRANS_SUBSTITUTION:
  1592. {
  1593. expr_def _tmp_expr, *tmp_expr = &_tmp_expr;
  1594. vinsn_t new_vi;
  1595. bool add = true;
  1596. new_vi = phist->old_expr_vinsn;
  1597. gcc_assert (VINSN_SEPARABLE_P (new_vi)
  1598. == EXPR_SEPARABLE_P (expr));
  1599. copy_expr (tmp_expr, expr);
  1600. if (vinsn_equal_p (phist->new_expr_vinsn,
  1601. EXPR_VINSN (tmp_expr)))
  1602. change_vinsn_in_expr (tmp_expr, new_vi);
  1603. else
  1604. /* This happens when we're unsubstituting on a bookkeeping
  1605. copy, which was in turn substituted. The history is wrong
  1606. in this case. Do it the hard way. */
  1607. add = substitute_reg_in_expr (tmp_expr, insn, true);
  1608. if (add)
  1609. av_set_add (&new_set, tmp_expr);
  1610. clear_expr (tmp_expr);
  1611. break;
  1612. }
  1613. default:
  1614. gcc_unreachable ();
  1615. }
  1616. }
  1617. }
  1618. av_set_union_and_clear (av_ptr, &new_set, NULL);
  1619. }
  1620. /* Moveup_* helpers for code motion and computing av sets. */
  1621. /* Propagates EXPR inside an insn group through THROUGH_INSN.
  1622. The difference from the below function is that only substitution is
  1623. performed. */
  1624. static enum MOVEUP_EXPR_CODE
  1625. moveup_expr_inside_insn_group (expr_t expr, insn_t through_insn)
  1626. {
  1627. vinsn_t vi = EXPR_VINSN (expr);
  1628. ds_t *has_dep_p;
  1629. ds_t full_ds;
  1630. /* Do this only inside insn group. */
  1631. gcc_assert (INSN_SCHED_CYCLE (through_insn) > 0);
  1632. full_ds = has_dependence_p (expr, through_insn, &has_dep_p);
  1633. if (full_ds == 0)
  1634. return MOVEUP_EXPR_SAME;
  1635. /* Substitution is the possible choice in this case. */
  1636. if (has_dep_p[DEPS_IN_RHS])
  1637. {
  1638. /* Can't substitute UNIQUE VINSNs. */
  1639. gcc_assert (!VINSN_UNIQUE_P (vi));
  1640. if (can_substitute_through_p (through_insn,
  1641. has_dep_p[DEPS_IN_RHS])
  1642. && substitute_reg_in_expr (expr, through_insn, false))
  1643. {
  1644. EXPR_WAS_SUBSTITUTED (expr) = true;
  1645. return MOVEUP_EXPR_CHANGED;
  1646. }
  1647. /* Don't care about this, as even true dependencies may be allowed
  1648. in an insn group. */
  1649. return MOVEUP_EXPR_SAME;
  1650. }
  1651. /* This can catch output dependencies in COND_EXECs. */
  1652. if (has_dep_p[DEPS_IN_INSN])
  1653. return MOVEUP_EXPR_NULL;
  1654. /* This is either an output or an anti dependence, which usually have
  1655. a zero latency. Allow this here, if we'd be wrong, tick_check_p
  1656. will fix this. */
  1657. gcc_assert (has_dep_p[DEPS_IN_LHS]);
  1658. return MOVEUP_EXPR_AS_RHS;
  1659. }
  1660. /* True when a trapping EXPR cannot be moved through THROUGH_INSN. */
  1661. #define CANT_MOVE_TRAPPING(expr, through_insn) \
  1662. (VINSN_MAY_TRAP_P (EXPR_VINSN (expr)) \
  1663. && !sel_insn_has_single_succ_p ((through_insn), SUCCS_ALL) \
  1664. && !sel_insn_is_speculation_check (through_insn))
  1665. /* True when a conflict on a target register was found during moveup_expr. */
  1666. static bool was_target_conflict = false;
  1667. /* Return true when moving a debug INSN across THROUGH_INSN will
  1668. create a bookkeeping block. We don't want to create such blocks,
  1669. for they would cause codegen differences between compilations with
  1670. and without debug info. */
  1671. static bool
  1672. moving_insn_creates_bookkeeping_block_p (insn_t insn,
  1673. insn_t through_insn)
  1674. {
  1675. basic_block bbi, bbt;
  1676. edge e1, e2;
  1677. edge_iterator ei1, ei2;
  1678. if (!bookkeeping_can_be_created_if_moved_through_p (through_insn))
  1679. {
  1680. if (sched_verbose >= 9)
  1681. sel_print ("no bookkeeping required: ");
  1682. return FALSE;
  1683. }
  1684. bbi = BLOCK_FOR_INSN (insn);
  1685. if (EDGE_COUNT (bbi->preds) == 1)
  1686. {
  1687. if (sched_verbose >= 9)
  1688. sel_print ("only one pred edge: ");
  1689. return TRUE;
  1690. }
  1691. bbt = BLOCK_FOR_INSN (through_insn);
  1692. FOR_EACH_EDGE (e1, ei1, bbt->succs)
  1693. {
  1694. FOR_EACH_EDGE (e2, ei2, bbi->preds)
  1695. {
  1696. if (find_block_for_bookkeeping (e1, e2, TRUE))
  1697. {
  1698. if (sched_verbose >= 9)
  1699. sel_print ("found existing block: ");
  1700. return FALSE;
  1701. }
  1702. }
  1703. }
  1704. if (sched_verbose >= 9)
  1705. sel_print ("would create bookkeeping block: ");
  1706. return TRUE;
  1707. }
  1708. /* Return true when the conflict with newly created implicit clobbers
  1709. between EXPR and THROUGH_INSN is found because of renaming. */
  1710. static bool
  1711. implicit_clobber_conflict_p (insn_t through_insn, expr_t expr)
  1712. {
  1713. HARD_REG_SET temp;
  1714. rtx_insn *insn;
  1715. rtx reg, rhs, pat;
  1716. hard_reg_set_iterator hrsi;
  1717. unsigned regno;
  1718. bool valid;
  1719. /* Make a new pseudo register. */
  1720. reg = gen_reg_rtx (GET_MODE (EXPR_LHS (expr)));
  1721. max_regno = max_reg_num ();
  1722. maybe_extend_reg_info_p ();
  1723. /* Validate a change and bail out early. */
  1724. insn = EXPR_INSN_RTX (expr);
  1725. validate_change (insn, &SET_DEST (PATTERN (insn)), reg, true);
  1726. valid = verify_changes (0);
  1727. cancel_changes (0);
  1728. if (!valid)
  1729. {
  1730. if (sched_verbose >= 6)
  1731. sel_print ("implicit clobbers failed validation, ");
  1732. return true;
  1733. }
  1734. /* Make a new insn with it. */
  1735. rhs = copy_rtx (VINSN_RHS (EXPR_VINSN (expr)));
  1736. pat = gen_rtx_SET (VOIDmode, reg, rhs);
  1737. start_sequence ();
  1738. insn = emit_insn (pat);
  1739. end_sequence ();
  1740. /* Calculate implicit clobbers. */
  1741. extract_insn (insn);
  1742. preprocess_constraints (insn);
  1743. ira_implicitly_set_insn_hard_regs (&temp);
  1744. AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
  1745. /* If any implicit clobber registers intersect with regular ones in
  1746. through_insn, we have a dependency and thus bail out. */
  1747. EXECUTE_IF_SET_IN_HARD_REG_SET (temp, 0, regno, hrsi)
  1748. {
  1749. vinsn_t vi = INSN_VINSN (through_insn);
  1750. if (bitmap_bit_p (VINSN_REG_SETS (vi), regno)
  1751. || bitmap_bit_p (VINSN_REG_CLOBBERS (vi), regno)
  1752. || bitmap_bit_p (VINSN_REG_USES (vi), regno))
  1753. return true;
  1754. }
  1755. return false;
  1756. }
  1757. /* Modifies EXPR so it can be moved through the THROUGH_INSN,
  1758. performing necessary transformations. Record the type of transformation
  1759. made in PTRANS_TYPE, when it is not NULL. When INSIDE_INSN_GROUP,
  1760. permit all dependencies except true ones, and try to remove those
  1761. too via forward substitution. All cases when a non-eliminable
  1762. non-zero cost dependency exists inside an insn group will be fixed
  1763. in tick_check_p instead. */
  1764. static enum MOVEUP_EXPR_CODE
  1765. moveup_expr (expr_t expr, insn_t through_insn, bool inside_insn_group,
  1766. enum local_trans_type *ptrans_type)
  1767. {
  1768. vinsn_t vi = EXPR_VINSN (expr);
  1769. insn_t insn = VINSN_INSN_RTX (vi);
  1770. bool was_changed = false;
  1771. bool as_rhs = false;
  1772. ds_t *has_dep_p;
  1773. ds_t full_ds;
  1774. /* ??? We use dependencies of non-debug insns on debug insns to
  1775. indicate that the debug insns need to be reset if the non-debug
  1776. insn is pulled ahead of it. It's hard to figure out how to
  1777. introduce such a notion in sel-sched, but it already fails to
  1778. support debug insns in other ways, so we just go ahead and
  1779. let the deug insns go corrupt for now. */
  1780. if (DEBUG_INSN_P (through_insn) && !DEBUG_INSN_P (insn))
  1781. return MOVEUP_EXPR_SAME;
  1782. /* When inside_insn_group, delegate to the helper. */
  1783. if (inside_insn_group)
  1784. return moveup_expr_inside_insn_group (expr, through_insn);
  1785. /* Deal with unique insns and control dependencies. */
  1786. if (VINSN_UNIQUE_P (vi))
  1787. {
  1788. /* We can move jumps without side-effects or jumps that are
  1789. mutually exclusive with instruction THROUGH_INSN (all in cases
  1790. dependencies allow to do so and jump is not speculative). */
  1791. if (control_flow_insn_p (insn))
  1792. {
  1793. basic_block fallthru_bb;
  1794. /* Do not move checks and do not move jumps through other
  1795. jumps. */
  1796. if (control_flow_insn_p (through_insn)
  1797. || sel_insn_is_speculation_check (insn))
  1798. return MOVEUP_EXPR_NULL;
  1799. /* Don't move jumps through CFG joins. */
  1800. if (bookkeeping_can_be_created_if_moved_through_p (through_insn))
  1801. return MOVEUP_EXPR_NULL;
  1802. /* The jump should have a clear fallthru block, and
  1803. this block should be in the current region. */
  1804. if ((fallthru_bb = fallthru_bb_of_jump (insn)) == NULL
  1805. || ! in_current_region_p (fallthru_bb))
  1806. return MOVEUP_EXPR_NULL;
  1807. /* And it should be mutually exclusive with through_insn. */
  1808. if (! sched_insns_conditions_mutex_p (insn, through_insn)
  1809. && ! DEBUG_INSN_P (through_insn))
  1810. return MOVEUP_EXPR_NULL;
  1811. }
  1812. /* Don't move what we can't move. */
  1813. if (EXPR_CANT_MOVE (expr)
  1814. && BLOCK_FOR_INSN (through_insn) != BLOCK_FOR_INSN (insn))
  1815. return MOVEUP_EXPR_NULL;
  1816. /* Don't move SCHED_GROUP instruction through anything.
  1817. If we don't force this, then it will be possible to start
  1818. scheduling a sched_group before all its dependencies are
  1819. resolved.
  1820. ??? Haifa deals with this issue by delaying the SCHED_GROUP
  1821. as late as possible through rank_for_schedule. */
  1822. if (SCHED_GROUP_P (insn))
  1823. return MOVEUP_EXPR_NULL;
  1824. }
  1825. else
  1826. gcc_assert (!control_flow_insn_p (insn));
  1827. /* Don't move debug insns if this would require bookkeeping. */
  1828. if (DEBUG_INSN_P (insn)
  1829. && BLOCK_FOR_INSN (through_insn) != BLOCK_FOR_INSN (insn)
  1830. && moving_insn_creates_bookkeeping_block_p (insn, through_insn))
  1831. return MOVEUP_EXPR_NULL;
  1832. /* Deal with data dependencies. */
  1833. was_target_conflict = false;
  1834. full_ds = has_dependence_p (expr, through_insn, &has_dep_p);
  1835. if (full_ds == 0)
  1836. {
  1837. if (!CANT_MOVE_TRAPPING (expr, through_insn))
  1838. return MOVEUP_EXPR_SAME;
  1839. }
  1840. else
  1841. {
  1842. /* We can move UNIQUE insn up only as a whole and unchanged,
  1843. so it shouldn't have any dependencies. */
  1844. if (VINSN_UNIQUE_P (vi))
  1845. return MOVEUP_EXPR_NULL;
  1846. }
  1847. if (full_ds != 0 && can_speculate_dep_p (full_ds))
  1848. {
  1849. int res;
  1850. res = speculate_expr (expr, full_ds);
  1851. if (res >= 0)
  1852. {
  1853. /* Speculation was successful. */
  1854. full_ds = 0;
  1855. was_changed = (res > 0);
  1856. if (res == 2)
  1857. was_target_conflict = true;
  1858. if (ptrans_type)
  1859. *ptrans_type = TRANS_SPECULATION;
  1860. sel_clear_has_dependence ();
  1861. }
  1862. }
  1863. if (has_dep_p[DEPS_IN_INSN])
  1864. /* We have some dependency that cannot be discarded. */
  1865. return MOVEUP_EXPR_NULL;
  1866. if (has_dep_p[DEPS_IN_LHS])
  1867. {
  1868. /* Only separable insns can be moved up with the new register.
  1869. Anyways, we should mark that the original register is
  1870. unavailable. */
  1871. if (!enable_schedule_as_rhs_p || !EXPR_SEPARABLE_P (expr))
  1872. return MOVEUP_EXPR_NULL;
  1873. /* When renaming a hard register to a pseudo before reload, extra
  1874. dependencies can occur from the implicit clobbers of the insn.
  1875. Filter out such cases here. */
  1876. if (!reload_completed && REG_P (EXPR_LHS (expr))
  1877. && HARD_REGISTER_P (EXPR_LHS (expr))
  1878. && implicit_clobber_conflict_p (through_insn, expr))
  1879. {
  1880. if (sched_verbose >= 6)
  1881. sel_print ("implicit clobbers conflict detected, ");
  1882. return MOVEUP_EXPR_NULL;
  1883. }
  1884. EXPR_TARGET_AVAILABLE (expr) = false;
  1885. was_target_conflict = true;
  1886. as_rhs = true;
  1887. }
  1888. /* At this point we have either separable insns, that will be lifted
  1889. up only as RHSes, or non-separable insns with no dependency in lhs.
  1890. If dependency is in RHS, then try to perform substitution and move up
  1891. substituted RHS:
  1892. Ex. 1: Ex.2
  1893. y = x; y = x;
  1894. z = y*2; y = y*2;
  1895. In Ex.1 y*2 can be substituted for x*2 and the whole operation can be
  1896. moved above y=x assignment as z=x*2.
  1897. In Ex.2 y*2 also can be substituted for x*2, but only the right hand
  1898. side can be moved because of the output dependency. The operation was
  1899. cropped to its rhs above. */
  1900. if (has_dep_p[DEPS_IN_RHS])
  1901. {
  1902. ds_t *rhs_dsp = &has_dep_p[DEPS_IN_RHS];
  1903. /* Can't substitute UNIQUE VINSNs. */
  1904. gcc_assert (!VINSN_UNIQUE_P (vi));
  1905. if (can_speculate_dep_p (*rhs_dsp))
  1906. {
  1907. int res;
  1908. res = speculate_expr (expr, *rhs_dsp);
  1909. if (res >= 0)
  1910. {
  1911. /* Speculation was successful. */
  1912. *rhs_dsp = 0;
  1913. was_changed = (res > 0);
  1914. if (res == 2)
  1915. was_target_conflict = true;
  1916. if (ptrans_type)
  1917. *ptrans_type = TRANS_SPECULATION;
  1918. }
  1919. else
  1920. return MOVEUP_EXPR_NULL;
  1921. }
  1922. else if (can_substitute_through_p (through_insn,
  1923. *rhs_dsp)
  1924. && substitute_reg_in_expr (expr, through_insn, false))
  1925. {
  1926. /* ??? We cannot perform substitution AND speculation on the same
  1927. insn. */
  1928. gcc_assert (!was_changed);
  1929. was_changed = true;
  1930. if (ptrans_type)
  1931. *ptrans_type = TRANS_SUBSTITUTION;
  1932. EXPR_WAS_SUBSTITUTED (expr) = true;
  1933. }
  1934. else
  1935. return MOVEUP_EXPR_NULL;
  1936. }
  1937. /* Don't move trapping insns through jumps.
  1938. This check should be at the end to give a chance to control speculation
  1939. to perform its duties. */
  1940. if (CANT_MOVE_TRAPPING (expr, through_insn))
  1941. return MOVEUP_EXPR_NULL;
  1942. return (was_changed
  1943. ? MOVEUP_EXPR_CHANGED
  1944. : (as_rhs
  1945. ? MOVEUP_EXPR_AS_RHS
  1946. : MOVEUP_EXPR_SAME));
  1947. }
  1948. /* Try to look at bitmap caches for EXPR and INSN pair, return true
  1949. if successful. When INSIDE_INSN_GROUP, also try ignore dependencies
  1950. that can exist within a parallel group. Write to RES the resulting
  1951. code for moveup_expr. */
  1952. static bool
  1953. try_bitmap_cache (expr_t expr, insn_t insn,
  1954. bool inside_insn_group,
  1955. enum MOVEUP_EXPR_CODE *res)
  1956. {
  1957. int expr_uid = INSN_UID (EXPR_INSN_RTX (expr));
  1958. /* First check whether we've analyzed this situation already. */
  1959. if (bitmap_bit_p (INSN_ANALYZED_DEPS (insn), expr_uid))
  1960. {
  1961. if (bitmap_bit_p (INSN_FOUND_DEPS (insn), expr_uid))
  1962. {
  1963. if (sched_verbose >= 6)
  1964. sel_print ("removed (cached)\n");
  1965. *res = MOVEUP_EXPR_NULL;
  1966. return true;
  1967. }
  1968. else
  1969. {
  1970. if (sched_verbose >= 6)
  1971. sel_print ("unchanged (cached)\n");
  1972. *res = MOVEUP_EXPR_SAME;
  1973. return true;
  1974. }
  1975. }
  1976. else if (bitmap_bit_p (INSN_FOUND_DEPS (insn), expr_uid))
  1977. {
  1978. if (inside_insn_group)
  1979. {
  1980. if (sched_verbose >= 6)
  1981. sel_print ("unchanged (as RHS, cached, inside insn group)\n");
  1982. *res = MOVEUP_EXPR_SAME;
  1983. return true;
  1984. }
  1985. else
  1986. EXPR_TARGET_AVAILABLE (expr) = false;
  1987. /* This is the only case when propagation result can change over time,
  1988. as we can dynamically switch off scheduling as RHS. In this case,
  1989. just check the flag to reach the correct decision. */
  1990. if (enable_schedule_as_rhs_p)
  1991. {
  1992. if (sched_verbose >= 6)
  1993. sel_print ("unchanged (as RHS, cached)\n");
  1994. *res = MOVEUP_EXPR_AS_RHS;
  1995. return true;
  1996. }
  1997. else
  1998. {
  1999. if (sched_verbose >= 6)
  2000. sel_print ("removed (cached as RHS, but renaming"
  2001. " is now disabled)\n");
  2002. *res = MOVEUP_EXPR_NULL;
  2003. return true;
  2004. }
  2005. }
  2006. return false;
  2007. }
  2008. /* Try to look at bitmap caches for EXPR and INSN pair, return true
  2009. if successful. Write to RES the resulting code for moveup_expr. */
  2010. static bool
  2011. try_transformation_cache (expr_t expr, insn_t insn,
  2012. enum MOVEUP_EXPR_CODE *res)
  2013. {
  2014. struct transformed_insns *pti
  2015. = (struct transformed_insns *)
  2016. htab_find_with_hash (INSN_TRANSFORMED_INSNS (insn),
  2017. &EXPR_VINSN (expr),
  2018. VINSN_HASH_RTX (EXPR_VINSN (expr)));
  2019. if (pti)
  2020. {
  2021. /* This EXPR was already moved through this insn and was
  2022. changed as a result. Fetch the proper data from
  2023. the hashtable. */
  2024. insert_in_history_vect (&EXPR_HISTORY_OF_CHANGES (expr),
  2025. INSN_UID (insn), pti->type,
  2026. pti->vinsn_old, pti->vinsn_new,
  2027. EXPR_SPEC_DONE_DS (expr));
  2028. if (INSN_IN_STREAM_P (VINSN_INSN_RTX (pti->vinsn_new)))
  2029. pti->vinsn_new = vinsn_copy (pti->vinsn_new, true);
  2030. change_vinsn_in_expr (expr, pti->vinsn_new);
  2031. if (pti->was_target_conflict)
  2032. EXPR_TARGET_AVAILABLE (expr) = false;
  2033. if (pti->type == TRANS_SPECULATION)
  2034. {
  2035. EXPR_SPEC_DONE_DS (expr) = pti->ds;
  2036. EXPR_NEEDS_SPEC_CHECK_P (expr) |= pti->needs_check;
  2037. }
  2038. if (sched_verbose >= 6)
  2039. {
  2040. sel_print ("changed (cached): ");
  2041. dump_expr (expr);
  2042. sel_print ("\n");
  2043. }
  2044. *res = MOVEUP_EXPR_CHANGED;
  2045. return true;
  2046. }
  2047. return false;
  2048. }
  2049. /* Update bitmap caches on INSN with result RES of propagating EXPR. */
  2050. static void
  2051. update_bitmap_cache (expr_t expr, insn_t insn, bool inside_insn_group,
  2052. enum MOVEUP_EXPR_CODE res)
  2053. {
  2054. int expr_uid = INSN_UID (EXPR_INSN_RTX (expr));
  2055. /* Do not cache result of propagating jumps through an insn group,
  2056. as it is always true, which is not useful outside the group. */
  2057. if (inside_insn_group)
  2058. return;
  2059. if (res == MOVEUP_EXPR_NULL)
  2060. {
  2061. bitmap_set_bit (INSN_ANALYZED_DEPS (insn), expr_uid);
  2062. bitmap_set_bit (INSN_FOUND_DEPS (insn), expr_uid);
  2063. }
  2064. else if (res == MOVEUP_EXPR_SAME)
  2065. {
  2066. bitmap_set_bit (INSN_ANALYZED_DEPS (insn), expr_uid);
  2067. bitmap_clear_bit (INSN_FOUND_DEPS (insn), expr_uid);
  2068. }
  2069. else if (res == MOVEUP_EXPR_AS_RHS)
  2070. {
  2071. bitmap_clear_bit (INSN_ANALYZED_DEPS (insn), expr_uid);
  2072. bitmap_set_bit (INSN_FOUND_DEPS (insn), expr_uid);
  2073. }
  2074. else
  2075. gcc_unreachable ();
  2076. }
  2077. /* Update hashtable on INSN with changed EXPR, old EXPR_OLD_VINSN
  2078. and transformation type TRANS_TYPE. */
  2079. static void
  2080. update_transformation_cache (expr_t expr, insn_t insn,
  2081. bool inside_insn_group,
  2082. enum local_trans_type trans_type,
  2083. vinsn_t expr_old_vinsn)
  2084. {
  2085. struct transformed_insns *pti;
  2086. if (inside_insn_group)
  2087. return;
  2088. pti = XNEW (struct transformed_insns);
  2089. pti->vinsn_old = expr_old_vinsn;
  2090. pti->vinsn_new = EXPR_VINSN (expr);
  2091. pti->type = trans_type;
  2092. pti->was_target_conflict = was_target_conflict;
  2093. pti->ds = EXPR_SPEC_DONE_DS (expr);
  2094. pti->needs_check = EXPR_NEEDS_SPEC_CHECK_P (expr);
  2095. vinsn_attach (pti->vinsn_old);
  2096. vinsn_attach (pti->vinsn_new);
  2097. *((struct transformed_insns **)
  2098. htab_find_slot_with_hash (INSN_TRANSFORMED_INSNS (insn),
  2099. pti, VINSN_HASH_RTX (expr_old_vinsn),
  2100. INSERT)) = pti;
  2101. }
  2102. /* Same as moveup_expr, but first looks up the result of
  2103. transformation in caches. */
  2104. static enum MOVEUP_EXPR_CODE
  2105. moveup_expr_cached (expr_t expr, insn_t insn, bool inside_insn_group)
  2106. {
  2107. enum MOVEUP_EXPR_CODE res;
  2108. bool got_answer = false;
  2109. if (sched_verbose >= 6)
  2110. {
  2111. sel_print ("Moving ");
  2112. dump_expr (expr);
  2113. sel_print (" through %d: ", INSN_UID (insn));
  2114. }
  2115. if (DEBUG_INSN_P (EXPR_INSN_RTX (expr))
  2116. && (sel_bb_head (BLOCK_FOR_INSN (EXPR_INSN_RTX (expr)))
  2117. == EXPR_INSN_RTX (expr)))
  2118. /* Don't use cached information for debug insns that are heads of
  2119. basic blocks. */;
  2120. else if (try_bitmap_cache (expr, insn, inside_insn_group, &res))
  2121. /* When inside insn group, we do not want remove stores conflicting
  2122. with previosly issued loads. */
  2123. got_answer = ! inside_insn_group || res != MOVEUP_EXPR_NULL;
  2124. else if (try_transformation_cache (expr, insn, &res))
  2125. got_answer = true;
  2126. if (! got_answer)
  2127. {
  2128. /* Invoke moveup_expr and record the results. */
  2129. vinsn_t expr_old_vinsn = EXPR_VINSN (expr);
  2130. ds_t expr_old_spec_ds = EXPR_SPEC_DONE_DS (expr);
  2131. int expr_uid = INSN_UID (VINSN_INSN_RTX (expr_old_vinsn));
  2132. bool unique_p = VINSN_UNIQUE_P (expr_old_vinsn);
  2133. enum local_trans_type trans_type = TRANS_SUBSTITUTION;
  2134. /* ??? Invent something better than this. We can't allow old_vinsn
  2135. to go, we need it for the history vector. */
  2136. vinsn_attach (expr_old_vinsn);
  2137. res = moveup_expr (expr, insn, inside_insn_group,
  2138. &trans_type);
  2139. switch (res)
  2140. {
  2141. case MOVEUP_EXPR_NULL:
  2142. update_bitmap_cache (expr, insn, inside_insn_group, res);
  2143. if (sched_verbose >= 6)
  2144. sel_print ("removed\n");
  2145. break;
  2146. case MOVEUP_EXPR_SAME:
  2147. update_bitmap_cache (expr, insn, inside_insn_group, res);
  2148. if (sched_verbose >= 6)
  2149. sel_print ("unchanged\n");
  2150. break;
  2151. case MOVEUP_EXPR_AS_RHS:
  2152. gcc_assert (!unique_p || inside_insn_group);
  2153. update_bitmap_cache (expr, insn, inside_insn_group, res);
  2154. if (sched_verbose >= 6)
  2155. sel_print ("unchanged (as RHS)\n");
  2156. break;
  2157. case MOVEUP_EXPR_CHANGED:
  2158. gcc_assert (INSN_UID (EXPR_INSN_RTX (expr)) != expr_uid
  2159. || EXPR_SPEC_DONE_DS (expr) != expr_old_spec_ds);
  2160. insert_in_history_vect (&EXPR_HISTORY_OF_CHANGES (expr),
  2161. INSN_UID (insn), trans_type,
  2162. expr_old_vinsn, EXPR_VINSN (expr),
  2163. expr_old_spec_ds);
  2164. update_transformation_cache (expr, insn, inside_insn_group,
  2165. trans_type, expr_old_vinsn);
  2166. if (sched_verbose >= 6)
  2167. {
  2168. sel_print ("changed: ");
  2169. dump_expr (expr);
  2170. sel_print ("\n");
  2171. }
  2172. break;
  2173. default:
  2174. gcc_unreachable ();
  2175. }
  2176. vinsn_detach (expr_old_vinsn);
  2177. }
  2178. return res;
  2179. }
  2180. /* Moves an av set AVP up through INSN, performing necessary
  2181. transformations. */
  2182. static void
  2183. moveup_set_expr (av_set_t *avp, insn_t insn, bool inside_insn_group)
  2184. {
  2185. av_set_iterator i;
  2186. expr_t expr;
  2187. FOR_EACH_EXPR_1 (expr, i, avp)
  2188. {
  2189. switch (moveup_expr_cached (expr, insn, inside_insn_group))
  2190. {
  2191. case MOVEUP_EXPR_SAME:
  2192. case MOVEUP_EXPR_AS_RHS:
  2193. break;
  2194. case MOVEUP_EXPR_NULL:
  2195. av_set_iter_remove (&i);
  2196. break;
  2197. case MOVEUP_EXPR_CHANGED:
  2198. expr = merge_with_other_exprs (avp, &i, expr);
  2199. break;
  2200. default:
  2201. gcc_unreachable ();
  2202. }
  2203. }
  2204. }
  2205. /* Moves AVP set along PATH. */
  2206. static void
  2207. moveup_set_inside_insn_group (av_set_t *avp, ilist_t path)
  2208. {
  2209. int last_cycle;
  2210. if (sched_verbose >= 6)
  2211. sel_print ("Moving expressions up in the insn group...\n");
  2212. if (! path)
  2213. return;
  2214. last_cycle = INSN_SCHED_CYCLE (ILIST_INSN (path));
  2215. while (path
  2216. && INSN_SCHED_CYCLE (ILIST_INSN (path)) == last_cycle)
  2217. {
  2218. moveup_set_expr (avp, ILIST_INSN (path), true);
  2219. path = ILIST_NEXT (path);
  2220. }
  2221. }
  2222. /* Returns true if after moving EXPR along PATH it equals to EXPR_VLIW. */
  2223. static bool
  2224. equal_after_moveup_path_p (expr_t expr, ilist_t path, expr_t expr_vliw)
  2225. {
  2226. expr_def _tmp, *tmp = &_tmp;
  2227. int last_cycle;
  2228. bool res = true;
  2229. copy_expr_onside (tmp, expr);
  2230. last_cycle = path ? INSN_SCHED_CYCLE (ILIST_INSN (path)) : 0;
  2231. while (path
  2232. && res
  2233. && INSN_SCHED_CYCLE (ILIST_INSN (path)) == last_cycle)
  2234. {
  2235. res = (moveup_expr_cached (tmp, ILIST_INSN (path), true)
  2236. != MOVEUP_EXPR_NULL);
  2237. path = ILIST_NEXT (path);
  2238. }
  2239. if (res)
  2240. {
  2241. vinsn_t tmp_vinsn = EXPR_VINSN (tmp);
  2242. vinsn_t expr_vliw_vinsn = EXPR_VINSN (expr_vliw);
  2243. if (tmp_vinsn != expr_vliw_vinsn)
  2244. res = vinsn_equal_p (tmp_vinsn, expr_vliw_vinsn);
  2245. }
  2246. clear_expr (tmp);
  2247. return res;
  2248. }
  2249. /* Functions that compute av and lv sets. */
  2250. /* Returns true if INSN is not a downward continuation of the given path P in
  2251. the current stage. */
  2252. static bool
  2253. is_ineligible_successor (insn_t insn, ilist_t p)
  2254. {
  2255. insn_t prev_insn;
  2256. /* Check if insn is not deleted. */
  2257. if (PREV_INSN (insn) && NEXT_INSN (PREV_INSN (insn)) != insn)
  2258. gcc_unreachable ();
  2259. else if (NEXT_INSN (insn) && PREV_INSN (NEXT_INSN (insn)) != insn)
  2260. gcc_unreachable ();
  2261. /* If it's the first insn visited, then the successor is ok. */
  2262. if (!p)
  2263. return false;
  2264. prev_insn = ILIST_INSN (p);
  2265. if (/* a backward edge. */
  2266. INSN_SEQNO (insn) < INSN_SEQNO (prev_insn)
  2267. /* is already visited. */
  2268. || (INSN_SEQNO (insn) == INSN_SEQNO (prev_insn)
  2269. && (ilist_is_in_p (p, insn)
  2270. /* We can reach another fence here and still seqno of insn
  2271. would be equal to seqno of prev_insn. This is possible
  2272. when prev_insn is a previously created bookkeeping copy.
  2273. In that case it'd get a seqno of insn. Thus, check here
  2274. whether insn is in current fence too. */
  2275. || IN_CURRENT_FENCE_P (insn)))
  2276. /* Was already scheduled on this round. */
  2277. || (INSN_SEQNO (insn) > INSN_SEQNO (prev_insn)
  2278. && IN_CURRENT_FENCE_P (insn))
  2279. /* An insn from another fence could also be
  2280. scheduled earlier even if this insn is not in
  2281. a fence list right now. Check INSN_SCHED_CYCLE instead. */
  2282. || (!pipelining_p
  2283. && INSN_SCHED_TIMES (insn) > 0))
  2284. return true;
  2285. else
  2286. return false;
  2287. }
  2288. /* Computes the av_set below the last bb insn INSN, doing all the 'dirty work'
  2289. of handling multiple successors and properly merging its av_sets. P is
  2290. the current path traversed. WS is the size of lookahead window.
  2291. Return the av set computed. */
  2292. static av_set_t
  2293. compute_av_set_at_bb_end (insn_t insn, ilist_t p, int ws)
  2294. {
  2295. struct succs_info *sinfo;
  2296. av_set_t expr_in_all_succ_branches = NULL;
  2297. int is;
  2298. insn_t succ, zero_succ = NULL;
  2299. av_set_t av1 = NULL;
  2300. gcc_assert (sel_bb_end_p (insn));
  2301. /* Find different kind of successors needed for correct computing of
  2302. SPEC and TARGET_AVAILABLE attributes. */
  2303. sinfo = compute_succs_info (insn, SUCCS_NORMAL);
  2304. /* Debug output. */
  2305. if (sched_verbose >= 6)
  2306. {
  2307. sel_print ("successors of bb end (%d): ", INSN_UID (insn));
  2308. dump_insn_vector (sinfo->succs_ok);
  2309. sel_print ("\n");
  2310. if (sinfo->succs_ok_n != sinfo->all_succs_n)
  2311. sel_print ("real successors num: %d\n", sinfo->all_succs_n);
  2312. }
  2313. /* Add insn to the tail of current path. */
  2314. ilist_add (&p, insn);
  2315. FOR_EACH_VEC_ELT (sinfo->succs_ok, is, succ)
  2316. {
  2317. av_set_t succ_set;
  2318. /* We will edit SUCC_SET and EXPR_SPEC field of its elements. */
  2319. succ_set = compute_av_set_inside_bb (succ, p, ws, true);
  2320. av_set_split_usefulness (succ_set,
  2321. sinfo->probs_ok[is],
  2322. sinfo->all_prob);
  2323. if (sinfo->all_succs_n > 1)
  2324. {
  2325. /* Find EXPR'es that came from *all* successors and save them
  2326. into expr_in_all_succ_branches. This set will be used later
  2327. for calculating speculation attributes of EXPR'es. */
  2328. if (is == 0)
  2329. {
  2330. expr_in_all_succ_branches = av_set_copy (succ_set);
  2331. /* Remember the first successor for later. */
  2332. zero_succ = succ;
  2333. }
  2334. else
  2335. {
  2336. av_set_iterator i;
  2337. expr_t expr;
  2338. FOR_EACH_EXPR_1 (expr, i, &expr_in_all_succ_branches)
  2339. if (!av_set_is_in_p (succ_set, EXPR_VINSN (expr)))
  2340. av_set_iter_remove (&i);
  2341. }
  2342. }
  2343. /* Union the av_sets. Check liveness restrictions on target registers
  2344. in special case of two successors. */
  2345. if (sinfo->succs_ok_n == 2 && is == 1)
  2346. {
  2347. basic_block bb0 = BLOCK_FOR_INSN (zero_succ);
  2348. basic_block bb1 = BLOCK_FOR_INSN (succ);
  2349. gcc_assert (BB_LV_SET_VALID_P (bb0) && BB_LV_SET_VALID_P (bb1));
  2350. av_set_union_and_live (&av1, &succ_set,
  2351. BB_LV_SET (bb0),
  2352. BB_LV_SET (bb1),
  2353. insn);
  2354. }
  2355. else
  2356. av_set_union_and_clear (&av1, &succ_set, insn);
  2357. }
  2358. /* Check liveness restrictions via hard way when there are more than
  2359. two successors. */
  2360. if (sinfo->succs_ok_n > 2)
  2361. FOR_EACH_VEC_ELT (sinfo->succs_ok, is, succ)
  2362. {
  2363. basic_block succ_bb = BLOCK_FOR_INSN (succ);
  2364. gcc_assert (BB_LV_SET_VALID_P (succ_bb));
  2365. mark_unavailable_targets (av1, BB_AV_SET (succ_bb),
  2366. BB_LV_SET (succ_bb));
  2367. }
  2368. /* Finally, check liveness restrictions on paths leaving the region. */
  2369. if (sinfo->all_succs_n > sinfo->succs_ok_n)
  2370. FOR_EACH_VEC_ELT (sinfo->succs_other, is, succ)
  2371. mark_unavailable_targets
  2372. (av1, NULL, BB_LV_SET (BLOCK_FOR_INSN (succ)));
  2373. if (sinfo->all_succs_n > 1)
  2374. {
  2375. av_set_iterator i;
  2376. expr_t expr;
  2377. /* Increase the spec attribute of all EXPR'es that didn't come
  2378. from all successors. */
  2379. FOR_EACH_EXPR (expr, i, av1)
  2380. if (!av_set_is_in_p (expr_in_all_succ_branches, EXPR_VINSN (expr)))
  2381. EXPR_SPEC (expr)++;
  2382. av_set_clear (&expr_in_all_succ_branches);
  2383. /* Do not move conditional branches through other
  2384. conditional branches. So, remove all conditional
  2385. branches from av_set if current operator is a conditional
  2386. branch. */
  2387. av_set_substract_cond_branches (&av1);
  2388. }
  2389. ilist_remove (&p);
  2390. free_succs_info (sinfo);
  2391. if (sched_verbose >= 6)
  2392. {
  2393. sel_print ("av_succs (%d): ", INSN_UID (insn));
  2394. dump_av_set (av1);
  2395. sel_print ("\n");
  2396. }
  2397. return av1;
  2398. }
  2399. /* This function computes av_set for the FIRST_INSN by dragging valid
  2400. av_set through all basic block insns either from the end of basic block
  2401. (computed using compute_av_set_at_bb_end) or from the insn on which
  2402. MAX_WS was exceeded. It uses compute_av_set_at_bb_end to compute av_set
  2403. below the basic block and handling conditional branches.
  2404. FIRST_INSN - the basic block head, P - path consisting of the insns
  2405. traversed on the way to the FIRST_INSN (the path is sparse, only bb heads
  2406. and bb ends are added to the path), WS - current window size,
  2407. NEED_COPY_P - true if we'll make a copy of av_set before returning it. */
  2408. static av_set_t
  2409. compute_av_set_inside_bb (insn_t first_insn, ilist_t p, int ws,
  2410. bool need_copy_p)
  2411. {
  2412. insn_t cur_insn;
  2413. int end_ws = ws;
  2414. insn_t bb_end = sel_bb_end (BLOCK_FOR_INSN (first_insn));
  2415. insn_t after_bb_end = NEXT_INSN (bb_end);
  2416. insn_t last_insn;
  2417. av_set_t av = NULL;
  2418. basic_block cur_bb = BLOCK_FOR_INSN (first_insn);
  2419. /* Return NULL if insn is not on the legitimate downward path. */
  2420. if (is_ineligible_successor (first_insn, p))
  2421. {
  2422. if (sched_verbose >= 6)
  2423. sel_print ("Insn %d is ineligible_successor\n", INSN_UID (first_insn));
  2424. return NULL;
  2425. }
  2426. /* If insn already has valid av(insn) computed, just return it. */
  2427. if (AV_SET_VALID_P (first_insn))
  2428. {
  2429. av_set_t av_set;
  2430. if (sel_bb_head_p (first_insn))
  2431. av_set = BB_AV_SET (BLOCK_FOR_INSN (first_insn));
  2432. else
  2433. av_set = NULL;
  2434. if (sched_verbose >= 6)
  2435. {
  2436. sel_print ("Insn %d has a valid av set: ", INSN_UID (first_insn));
  2437. dump_av_set (av_set);
  2438. sel_print ("\n");
  2439. }
  2440. return need_copy_p ? av_set_copy (av_set) : av_set;
  2441. }
  2442. ilist_add (&p, first_insn);
  2443. /* As the result after this loop have completed, in LAST_INSN we'll
  2444. have the insn which has valid av_set to start backward computation
  2445. from: it either will be NULL because on it the window size was exceeded
  2446. or other valid av_set as returned by compute_av_set for the last insn
  2447. of the basic block. */
  2448. for (last_insn = first_insn; last_insn != after_bb_end;
  2449. last_insn = NEXT_INSN (last_insn))
  2450. {
  2451. /* We may encounter valid av_set not only on bb_head, but also on
  2452. those insns on which previously MAX_WS was exceeded. */
  2453. if (AV_SET_VALID_P (last_insn))
  2454. {
  2455. if (sched_verbose >= 6)
  2456. sel_print ("Insn %d has a valid empty av set\n", INSN_UID (last_insn));
  2457. break;
  2458. }
  2459. /* The special case: the last insn of the BB may be an
  2460. ineligible_successor due to its SEQ_NO that was set on
  2461. it as a bookkeeping. */
  2462. if (last_insn != first_insn
  2463. && is_ineligible_successor (last_insn, p))
  2464. {
  2465. if (sched_verbose >= 6)
  2466. sel_print ("Insn %d is ineligible_successor\n", INSN_UID (last_insn));
  2467. break;
  2468. }
  2469. if (DEBUG_INSN_P (last_insn))
  2470. continue;
  2471. if (end_ws > max_ws)
  2472. {
  2473. /* We can reach max lookahead size at bb_header, so clean av_set
  2474. first. */
  2475. INSN_WS_LEVEL (last_insn) = global_level;
  2476. if (sched_verbose >= 6)
  2477. sel_print ("Insn %d is beyond the software lookahead window size\n",
  2478. INSN_UID (last_insn));
  2479. break;
  2480. }
  2481. end_ws++;
  2482. }
  2483. /* Get the valid av_set into AV above the LAST_INSN to start backward
  2484. computation from. It either will be empty av_set or av_set computed from
  2485. the successors on the last insn of the current bb. */
  2486. if (last_insn != after_bb_end)
  2487. {
  2488. av = NULL;
  2489. /* This is needed only to obtain av_sets that are identical to
  2490. those computed by the old compute_av_set version. */
  2491. if (last_insn == first_insn && !INSN_NOP_P (last_insn))
  2492. av_set_add (&av, INSN_EXPR (last_insn));
  2493. }
  2494. else
  2495. /* END_WS is always already increased by 1 if LAST_INSN == AFTER_BB_END. */
  2496. av = compute_av_set_at_bb_end (bb_end, p, end_ws);
  2497. /* Compute av_set in AV starting from below the LAST_INSN up to
  2498. location above the FIRST_INSN. */
  2499. for (cur_insn = PREV_INSN (last_insn); cur_insn != PREV_INSN (first_insn);
  2500. cur_insn = PREV_INSN (cur_insn))
  2501. if (!INSN_NOP_P (cur_insn))
  2502. {
  2503. expr_t expr;
  2504. moveup_set_expr (&av, cur_insn, false);
  2505. /* If the expression for CUR_INSN is already in the set,
  2506. replace it by the new one. */
  2507. expr = av_set_lookup (av, INSN_VINSN (cur_insn));
  2508. if (expr != NULL)
  2509. {
  2510. clear_expr (expr);
  2511. copy_expr (expr, INSN_EXPR (cur_insn));
  2512. }
  2513. else
  2514. av_set_add (&av, INSN_EXPR (cur_insn));
  2515. }
  2516. /* Clear stale bb_av_set. */
  2517. if (sel_bb_head_p (first_insn))
  2518. {
  2519. av_set_clear (&BB_AV_SET (cur_bb));
  2520. BB_AV_SET (cur_bb) = need_copy_p ? av_set_copy (av) : av;
  2521. BB_AV_LEVEL (cur_bb) = global_level;
  2522. }
  2523. if (sched_verbose >= 6)
  2524. {
  2525. sel_print ("Computed av set for insn %d: ", INSN_UID (first_insn));
  2526. dump_av_set (av);
  2527. sel_print ("\n");
  2528. }
  2529. ilist_remove (&p);
  2530. return av;
  2531. }
  2532. /* Compute av set before INSN.
  2533. INSN - the current operation (actual rtx INSN)
  2534. P - the current path, which is list of insns visited so far
  2535. WS - software lookahead window size.
  2536. UNIQUE_P - TRUE, if returned av_set will be changed, hence
  2537. if we want to save computed av_set in s_i_d, we should make a copy of it.
  2538. In the resulting set we will have only expressions that don't have delay
  2539. stalls and nonsubstitutable dependences. */
  2540. static av_set_t
  2541. compute_av_set (insn_t insn, ilist_t p, int ws, bool unique_p)
  2542. {
  2543. return compute_av_set_inside_bb (insn, p, ws, unique_p);
  2544. }
  2545. /* Propagate a liveness set LV through INSN. */
  2546. static void
  2547. propagate_lv_set (regset lv, insn_t insn)
  2548. {
  2549. gcc_assert (INSN_P (insn));
  2550. if (INSN_NOP_P (insn))
  2551. return;
  2552. df_simulate_one_insn_backwards (BLOCK_FOR_INSN (insn), insn, lv);
  2553. }
  2554. /* Return livness set at the end of BB. */
  2555. static regset
  2556. compute_live_after_bb (basic_block bb)
  2557. {
  2558. edge e;
  2559. edge_iterator ei;
  2560. regset lv = get_clear_regset_from_pool ();
  2561. gcc_assert (!ignore_first);
  2562. FOR_EACH_EDGE (e, ei, bb->succs)
  2563. if (sel_bb_empty_p (e->dest))
  2564. {
  2565. if (! BB_LV_SET_VALID_P (e->dest))
  2566. {
  2567. gcc_unreachable ();
  2568. gcc_assert (BB_LV_SET (e->dest) == NULL);
  2569. BB_LV_SET (e->dest) = compute_live_after_bb (e->dest);
  2570. BB_LV_SET_VALID_P (e->dest) = true;
  2571. }
  2572. IOR_REG_SET (lv, BB_LV_SET (e->dest));
  2573. }
  2574. else
  2575. IOR_REG_SET (lv, compute_live (sel_bb_head (e->dest)));
  2576. return lv;
  2577. }
  2578. /* Compute the set of all live registers at the point before INSN and save
  2579. it at INSN if INSN is bb header. */
  2580. regset
  2581. compute_live (insn_t insn)
  2582. {
  2583. basic_block bb = BLOCK_FOR_INSN (insn);
  2584. insn_t final, temp;
  2585. regset lv;
  2586. /* Return the valid set if we're already on it. */
  2587. if (!ignore_first)
  2588. {
  2589. regset src = NULL;
  2590. if (sel_bb_head_p (insn) && BB_LV_SET_VALID_P (bb))
  2591. src = BB_LV_SET (bb);
  2592. else
  2593. {
  2594. gcc_assert (in_current_region_p (bb));
  2595. if (INSN_LIVE_VALID_P (insn))
  2596. src = INSN_LIVE (insn);
  2597. }
  2598. if (src)
  2599. {
  2600. lv = get_regset_from_pool ();
  2601. COPY_REG_SET (lv, src);
  2602. if (sel_bb_head_p (insn) && ! BB_LV_SET_VALID_P (bb))
  2603. {
  2604. COPY_REG_SET (BB_LV_SET (bb), lv);
  2605. BB_LV_SET_VALID_P (bb) = true;
  2606. }
  2607. return_regset_to_pool (lv);
  2608. return lv;
  2609. }
  2610. }
  2611. /* We've skipped the wrong lv_set. Don't skip the right one. */
  2612. ignore_first = false;
  2613. gcc_assert (in_current_region_p (bb));
  2614. /* Find a valid LV set in this block or below, if needed.
  2615. Start searching from the next insn: either ignore_first is true, or
  2616. INSN doesn't have a correct live set. */
  2617. temp = NEXT_INSN (insn);
  2618. final = NEXT_INSN (BB_END (bb));
  2619. while (temp != final && ! INSN_LIVE_VALID_P (temp))
  2620. temp = NEXT_INSN (temp);
  2621. if (temp == final)
  2622. {
  2623. lv = compute_live_after_bb (bb);
  2624. temp = PREV_INSN (temp);
  2625. }
  2626. else
  2627. {
  2628. lv = get_regset_from_pool ();
  2629. COPY_REG_SET (lv, INSN_LIVE (temp));
  2630. }
  2631. /* Put correct lv sets on the insns which have bad sets. */
  2632. final = PREV_INSN (insn);
  2633. while (temp != final)
  2634. {
  2635. propagate_lv_set (lv, temp);
  2636. COPY_REG_SET (INSN_LIVE (temp), lv);
  2637. INSN_LIVE_VALID_P (temp) = true;
  2638. temp = PREV_INSN (temp);
  2639. }
  2640. /* Also put it in a BB. */
  2641. if (sel_bb_head_p (insn))
  2642. {
  2643. basic_block bb = BLOCK_FOR_INSN (insn);
  2644. COPY_REG_SET (BB_LV_SET (bb), lv);
  2645. BB_LV_SET_VALID_P (bb) = true;
  2646. }
  2647. /* We return LV to the pool, but will not clear it there. Thus we can
  2648. legimatelly use LV till the next use of regset_pool_get (). */
  2649. return_regset_to_pool (lv);
  2650. return lv;
  2651. }
  2652. /* Update liveness sets for INSN. */
  2653. static inline void
  2654. update_liveness_on_insn (rtx_insn *insn)
  2655. {
  2656. ignore_first = true;
  2657. compute_live (insn);
  2658. }
  2659. /* Compute liveness below INSN and write it into REGS. */
  2660. static inline void
  2661. compute_live_below_insn (rtx_insn *insn, regset regs)
  2662. {
  2663. rtx_insn *succ;
  2664. succ_iterator si;
  2665. FOR_EACH_SUCC_1 (succ, si, insn, SUCCS_ALL)
  2666. IOR_REG_SET (regs, compute_live (succ));
  2667. }
  2668. /* Update the data gathered in av and lv sets starting from INSN. */
  2669. static void
  2670. update_data_sets (rtx_insn *insn)
  2671. {
  2672. update_liveness_on_insn (insn);
  2673. if (sel_bb_head_p (insn))
  2674. {
  2675. gcc_assert (AV_LEVEL (insn) != 0);
  2676. BB_AV_LEVEL (BLOCK_FOR_INSN (insn)) = -1;
  2677. compute_av_set (insn, NULL, 0, 0);
  2678. }
  2679. }
  2680. /* Helper for move_op () and find_used_regs ().
  2681. Return speculation type for which a check should be created on the place
  2682. of INSN. EXPR is one of the original ops we are searching for. */
  2683. static ds_t
  2684. get_spec_check_type_for_insn (insn_t insn, expr_t expr)
  2685. {
  2686. ds_t to_check_ds;
  2687. ds_t already_checked_ds = EXPR_SPEC_DONE_DS (INSN_EXPR (insn));
  2688. to_check_ds = EXPR_SPEC_TO_CHECK_DS (expr);
  2689. if (targetm.sched.get_insn_checked_ds)
  2690. already_checked_ds |= targetm.sched.get_insn_checked_ds (insn);
  2691. if (spec_info != NULL
  2692. && (spec_info->flags & SEL_SCHED_SPEC_DONT_CHECK_CONTROL))
  2693. already_checked_ds |= BEGIN_CONTROL;
  2694. already_checked_ds = ds_get_speculation_types (already_checked_ds);
  2695. to_check_ds &= ~already_checked_ds;
  2696. return to_check_ds;
  2697. }
  2698. /* Find the set of registers that are unavailable for storing expres
  2699. while moving ORIG_OPS up on the path starting from INSN due to
  2700. liveness (USED_REGS) or hardware restrictions (REG_RENAME_P).
  2701. All the original operations found during the traversal are saved in the
  2702. ORIGINAL_INSNS list.
  2703. REG_RENAME_P denotes the set of hardware registers that
  2704. can not be used with renaming due to the register class restrictions,
  2705. mode restrictions and other (the register we'll choose should be
  2706. compatible class with the original uses, shouldn't be in call_used_regs,
  2707. should be HARD_REGNO_RENAME_OK etc).
  2708. Returns TRUE if we've found all original insns, FALSE otherwise.
  2709. This function utilizes code_motion_path_driver (formerly find_used_regs_1)
  2710. to traverse the code motion paths. This helper function finds registers
  2711. that are not available for storing expres while moving ORIG_OPS up on the
  2712. path starting from INSN. A register considered as used on the moving path,
  2713. if one of the following conditions is not satisfied:
  2714. (1) a register not set or read on any path from xi to an instance of
  2715. the original operation,
  2716. (2) not among the live registers of the point immediately following the
  2717. first original operation on a given downward path, except for the
  2718. original target register of the operation,
  2719. (3) not live on the other path of any conditional branch that is passed
  2720. by the operation, in case original operations are not present on
  2721. both paths of the conditional branch.
  2722. All the original operations found during the traversal are saved in the
  2723. ORIGINAL_INSNS list.
  2724. REG_RENAME_P->CROSSES_CALL is true, if there is a call insn on the path
  2725. from INSN to original insn. In this case CALL_USED_REG_SET will be added
  2726. to unavailable hard regs at the point original operation is found. */
  2727. static bool
  2728. find_used_regs (insn_t insn, av_set_t orig_ops, regset used_regs,
  2729. struct reg_rename *reg_rename_p, def_list_t *original_insns)
  2730. {
  2731. def_list_iterator i;
  2732. def_t def;
  2733. int res;
  2734. bool needs_spec_check_p = false;
  2735. expr_t expr;
  2736. av_set_iterator expr_iter;
  2737. struct fur_static_params sparams;
  2738. struct cmpd_local_params lparams;
  2739. /* We haven't visited any blocks yet. */
  2740. bitmap_clear (code_motion_visited_blocks);
  2741. /* Init parameters for code_motion_path_driver. */
  2742. sparams.crosses_call = false;
  2743. sparams.original_insns = original_insns;
  2744. sparams.used_regs = used_regs;
  2745. /* Set the appropriate hooks and data. */
  2746. code_motion_path_driver_info = &fur_hooks;
  2747. res = code_motion_path_driver (insn, orig_ops, NULL, &lparams, &sparams);
  2748. reg_rename_p->crosses_call |= sparams.crosses_call;
  2749. gcc_assert (res == 1);
  2750. gcc_assert (original_insns && *original_insns);
  2751. /* ??? We calculate whether an expression needs a check when computing
  2752. av sets. This information is not as precise as it could be due to
  2753. merging this bit in merge_expr. We can do better in find_used_regs,
  2754. but we want to avoid multiple traversals of the same code motion
  2755. paths. */
  2756. FOR_EACH_EXPR (expr, expr_iter, orig_ops)
  2757. needs_spec_check_p |= EXPR_NEEDS_SPEC_CHECK_P (expr);
  2758. /* Mark hardware regs in REG_RENAME_P that are not suitable
  2759. for renaming expr in INSN due to hardware restrictions (register class,
  2760. modes compatibility etc). */
  2761. FOR_EACH_DEF (def, i, *original_insns)
  2762. {
  2763. vinsn_t vinsn = INSN_VINSN (def->orig_insn);
  2764. if (VINSN_SEPARABLE_P (vinsn))
  2765. mark_unavailable_hard_regs (def, reg_rename_p, used_regs);
  2766. /* Do not allow clobbering of ld.[sa] address in case some of the
  2767. original operations need a check. */
  2768. if (needs_spec_check_p)
  2769. IOR_REG_SET (used_regs, VINSN_REG_USES (vinsn));
  2770. }
  2771. return true;
  2772. }
  2773. /* Functions to choose the best insn from available ones. */
  2774. /* Adjusts the priority for EXPR using the backend *_adjust_priority hook. */
  2775. static int
  2776. sel_target_adjust_priority (expr_t expr)
  2777. {
  2778. int priority = EXPR_PRIORITY (expr);
  2779. int new_priority;
  2780. if (targetm.sched.adjust_priority)
  2781. new_priority = targetm.sched.adjust_priority (EXPR_INSN_RTX (expr), priority);
  2782. else
  2783. new_priority = priority;
  2784. /* If the priority has changed, adjust EXPR_PRIORITY_ADJ accordingly. */
  2785. EXPR_PRIORITY_ADJ (expr) = new_priority - EXPR_PRIORITY (expr);
  2786. gcc_assert (EXPR_PRIORITY_ADJ (expr) >= 0);
  2787. if (sched_verbose >= 4)
  2788. sel_print ("sel_target_adjust_priority: insn %d, %d+%d = %d.\n",
  2789. INSN_UID (EXPR_INSN_RTX (expr)), EXPR_PRIORITY (expr),
  2790. EXPR_PRIORITY_ADJ (expr), new_priority);
  2791. return new_priority;
  2792. }
  2793. /* Rank two available exprs for schedule. Never return 0 here. */
  2794. static int
  2795. sel_rank_for_schedule (const void *x, const void *y)
  2796. {
  2797. expr_t tmp = *(const expr_t *) y;
  2798. expr_t tmp2 = *(const expr_t *) x;
  2799. insn_t tmp_insn, tmp2_insn;
  2800. vinsn_t tmp_vinsn, tmp2_vinsn;
  2801. int val;
  2802. tmp_vinsn = EXPR_VINSN (tmp);
  2803. tmp2_vinsn = EXPR_VINSN (tmp2);
  2804. tmp_insn = EXPR_INSN_RTX (tmp);
  2805. tmp2_insn = EXPR_INSN_RTX (tmp2);
  2806. /* Schedule debug insns as early as possible. */
  2807. if (DEBUG_INSN_P (tmp_insn) && !DEBUG_INSN_P (tmp2_insn))
  2808. return -1;
  2809. else if (DEBUG_INSN_P (tmp2_insn))
  2810. return 1;
  2811. /* Prefer SCHED_GROUP_P insns to any others. */
  2812. if (SCHED_GROUP_P (tmp_insn) != SCHED_GROUP_P (tmp2_insn))
  2813. {
  2814. if (VINSN_UNIQUE_P (tmp_vinsn) && VINSN_UNIQUE_P (tmp2_vinsn))
  2815. return SCHED_GROUP_P (tmp2_insn) ? 1 : -1;
  2816. /* Now uniqueness means SCHED_GROUP_P is set, because schedule groups
  2817. cannot be cloned. */
  2818. if (VINSN_UNIQUE_P (tmp2_vinsn))
  2819. return 1;
  2820. return -1;
  2821. }
  2822. /* Discourage scheduling of speculative checks. */
  2823. val = (sel_insn_is_speculation_check (tmp_insn)
  2824. - sel_insn_is_speculation_check (tmp2_insn));
  2825. if (val)
  2826. return val;
  2827. /* Prefer not scheduled insn over scheduled one. */
  2828. if (EXPR_SCHED_TIMES (tmp) > 0 || EXPR_SCHED_TIMES (tmp2) > 0)
  2829. {
  2830. val = EXPR_SCHED_TIMES (tmp) - EXPR_SCHED_TIMES (tmp2);
  2831. if (val)
  2832. return val;
  2833. }
  2834. /* Prefer jump over non-jump instruction. */
  2835. if (control_flow_insn_p (tmp_insn) && !control_flow_insn_p (tmp2_insn))
  2836. return -1;
  2837. else if (control_flow_insn_p (tmp2_insn) && !control_flow_insn_p (tmp_insn))
  2838. return 1;
  2839. /* Prefer an expr with greater priority. */
  2840. if (EXPR_USEFULNESS (tmp) != 0 && EXPR_USEFULNESS (tmp2) != 0)
  2841. {
  2842. int p2 = EXPR_PRIORITY (tmp2) + EXPR_PRIORITY_ADJ (tmp2),
  2843. p1 = EXPR_PRIORITY (tmp) + EXPR_PRIORITY_ADJ (tmp);
  2844. val = p2 * EXPR_USEFULNESS (tmp2) - p1 * EXPR_USEFULNESS (tmp);
  2845. }
  2846. else
  2847. val = EXPR_PRIORITY (tmp2) - EXPR_PRIORITY (tmp)
  2848. + EXPR_PRIORITY_ADJ (tmp2) - EXPR_PRIORITY_ADJ (tmp);
  2849. if (val)
  2850. return val;
  2851. if (spec_info != NULL && spec_info->mask != 0)
  2852. /* This code was taken from haifa-sched.c: rank_for_schedule (). */
  2853. {
  2854. ds_t ds1, ds2;
  2855. dw_t dw1, dw2;
  2856. int dw;
  2857. ds1 = EXPR_SPEC_DONE_DS (tmp);
  2858. if (ds1)
  2859. dw1 = ds_weak (ds1);
  2860. else
  2861. dw1 = NO_DEP_WEAK;
  2862. ds2 = EXPR_SPEC_DONE_DS (tmp2);
  2863. if (ds2)
  2864. dw2 = ds_weak (ds2);
  2865. else
  2866. dw2 = NO_DEP_WEAK;
  2867. dw = dw2 - dw1;
  2868. if (dw > (NO_DEP_WEAK / 8) || dw < -(NO_DEP_WEAK / 8))
  2869. return dw;
  2870. }
  2871. /* Prefer an old insn to a bookkeeping insn. */
  2872. if (INSN_UID (tmp_insn) < first_emitted_uid
  2873. && INSN_UID (tmp2_insn) >= first_emitted_uid)
  2874. return -1;
  2875. if (INSN_UID (tmp_insn) >= first_emitted_uid
  2876. && INSN_UID (tmp2_insn) < first_emitted_uid)
  2877. return 1;
  2878. /* Prefer an insn with smaller UID, as a last resort.
  2879. We can't safely use INSN_LUID as it is defined only for those insns
  2880. that are in the stream. */
  2881. return INSN_UID (tmp_insn) - INSN_UID (tmp2_insn);
  2882. }
  2883. /* Filter out expressions from av set pointed to by AV_PTR
  2884. that are pipelined too many times. */
  2885. static void
  2886. process_pipelined_exprs (av_set_t *av_ptr)
  2887. {
  2888. expr_t expr;
  2889. av_set_iterator si;
  2890. /* Don't pipeline already pipelined code as that would increase
  2891. number of unnecessary register moves. */
  2892. FOR_EACH_EXPR_1 (expr, si, av_ptr)
  2893. {
  2894. if (EXPR_SCHED_TIMES (expr)
  2895. >= PARAM_VALUE (PARAM_SELSCHED_MAX_SCHED_TIMES))
  2896. av_set_iter_remove (&si);
  2897. }
  2898. }
  2899. /* Filter speculative insns from AV_PTR if we don't want them. */
  2900. static void
  2901. process_spec_exprs (av_set_t *av_ptr)
  2902. {
  2903. expr_t expr;
  2904. av_set_iterator si;
  2905. if (spec_info == NULL)
  2906. return;
  2907. /* Scan *AV_PTR to find out if we want to consider speculative
  2908. instructions for scheduling. */
  2909. FOR_EACH_EXPR_1 (expr, si, av_ptr)
  2910. {
  2911. ds_t ds;
  2912. ds = EXPR_SPEC_DONE_DS (expr);
  2913. /* The probability of a success is too low - don't speculate. */
  2914. if ((ds & SPECULATIVE)
  2915. && (ds_weak (ds) < spec_info->data_weakness_cutoff
  2916. || EXPR_USEFULNESS (expr) < spec_info->control_weakness_cutoff
  2917. || (pipelining_p && false
  2918. && (ds & DATA_SPEC)
  2919. && (ds & CONTROL_SPEC))))
  2920. {
  2921. av_set_iter_remove (&si);
  2922. continue;
  2923. }
  2924. }
  2925. }
  2926. /* Search for any use-like insns in AV_PTR and decide on scheduling
  2927. them. Return one when found, and NULL otherwise.
  2928. Note that we check here whether a USE could be scheduled to avoid
  2929. an infinite loop later. */
  2930. static expr_t
  2931. process_use_exprs (av_set_t *av_ptr)
  2932. {
  2933. expr_t expr;
  2934. av_set_iterator si;
  2935. bool uses_present_p = false;
  2936. bool try_uses_p = true;
  2937. FOR_EACH_EXPR_1 (expr, si, av_ptr)
  2938. {
  2939. /* This will also initialize INSN_CODE for later use. */
  2940. if (recog_memoized (EXPR_INSN_RTX (expr)) < 0)
  2941. {
  2942. /* If we have a USE in *AV_PTR that was not scheduled yet,
  2943. do so because it will do good only. */
  2944. if (EXPR_SCHED_TIMES (expr) <= 0)
  2945. {
  2946. if (EXPR_TARGET_AVAILABLE (expr) == 1)
  2947. return expr;
  2948. av_set_iter_remove (&si);
  2949. }
  2950. else
  2951. {
  2952. gcc_assert (pipelining_p);
  2953. uses_present_p = true;
  2954. }
  2955. }
  2956. else
  2957. try_uses_p = false;
  2958. }
  2959. if (uses_present_p)
  2960. {
  2961. /* If we don't want to schedule any USEs right now and we have some
  2962. in *AV_PTR, remove them, else just return the first one found. */
  2963. if (!try_uses_p)
  2964. {
  2965. FOR_EACH_EXPR_1 (expr, si, av_ptr)
  2966. if (INSN_CODE (EXPR_INSN_RTX (expr)) < 0)
  2967. av_set_iter_remove (&si);
  2968. }
  2969. else
  2970. {
  2971. FOR_EACH_EXPR_1 (expr, si, av_ptr)
  2972. {
  2973. gcc_assert (INSN_CODE (EXPR_INSN_RTX (expr)) < 0);
  2974. if (EXPR_TARGET_AVAILABLE (expr) == 1)
  2975. return expr;
  2976. av_set_iter_remove (&si);
  2977. }
  2978. }
  2979. }
  2980. return NULL;
  2981. }
  2982. /* Lookup EXPR in VINSN_VEC and return TRUE if found. Also check patterns from
  2983. EXPR's history of changes. */
  2984. static bool
  2985. vinsn_vec_has_expr_p (vinsn_vec_t vinsn_vec, expr_t expr)
  2986. {
  2987. vinsn_t vinsn, expr_vinsn;
  2988. int n;
  2989. unsigned i;
  2990. /* Start with checking expr itself and then proceed with all the old forms
  2991. of expr taken from its history vector. */
  2992. for (i = 0, expr_vinsn = EXPR_VINSN (expr);
  2993. expr_vinsn;
  2994. expr_vinsn = (i < EXPR_HISTORY_OF_CHANGES (expr).length ()
  2995. ? EXPR_HISTORY_OF_CHANGES (expr)[i++].old_expr_vinsn
  2996. : NULL))
  2997. FOR_EACH_VEC_ELT (vinsn_vec, n, vinsn)
  2998. if (VINSN_SEPARABLE_P (vinsn))
  2999. {
  3000. if (vinsn_equal_p (vinsn, expr_vinsn))
  3001. return true;
  3002. }
  3003. else
  3004. {
  3005. /* For non-separable instructions, the blocking insn can have
  3006. another pattern due to substitution, and we can't choose
  3007. different register as in the above case. Check all registers
  3008. being written instead. */
  3009. if (bitmap_intersect_p (VINSN_REG_SETS (vinsn),
  3010. VINSN_REG_SETS (expr_vinsn)))
  3011. return true;
  3012. }
  3013. return false;
  3014. }
  3015. #ifdef ENABLE_CHECKING
  3016. /* Return true if either of expressions from ORIG_OPS can be blocked
  3017. by previously created bookkeeping code. STATIC_PARAMS points to static
  3018. parameters of move_op. */
  3019. static bool
  3020. av_set_could_be_blocked_by_bookkeeping_p (av_set_t orig_ops, void *static_params)
  3021. {
  3022. expr_t expr;
  3023. av_set_iterator iter;
  3024. moveop_static_params_p sparams;
  3025. /* This checks that expressions in ORIG_OPS are not blocked by bookkeeping
  3026. created while scheduling on another fence. */
  3027. FOR_EACH_EXPR (expr, iter, orig_ops)
  3028. if (vinsn_vec_has_expr_p (vec_bookkeeping_blocked_vinsns, expr))
  3029. return true;
  3030. gcc_assert (code_motion_path_driver_info == &move_op_hooks);
  3031. sparams = (moveop_static_params_p) static_params;
  3032. /* Expressions can be also blocked by bookkeeping created during current
  3033. move_op. */
  3034. if (bitmap_bit_p (current_copies, INSN_UID (sparams->failed_insn)))
  3035. FOR_EACH_EXPR (expr, iter, orig_ops)
  3036. if (moveup_expr_cached (expr, sparams->failed_insn, false) != MOVEUP_EXPR_NULL)
  3037. return true;
  3038. /* Expressions in ORIG_OPS may have wrong destination register due to
  3039. renaming. Check with the right register instead. */
  3040. if (sparams->dest && REG_P (sparams->dest))
  3041. {
  3042. rtx reg = sparams->dest;
  3043. vinsn_t failed_vinsn = INSN_VINSN (sparams->failed_insn);
  3044. if (register_unavailable_p (VINSN_REG_SETS (failed_vinsn), reg)
  3045. || register_unavailable_p (VINSN_REG_USES (failed_vinsn), reg)
  3046. || register_unavailable_p (VINSN_REG_CLOBBERS (failed_vinsn), reg))
  3047. return true;
  3048. }
  3049. return false;
  3050. }
  3051. #endif
  3052. /* Clear VINSN_VEC and detach vinsns. */
  3053. static void
  3054. vinsn_vec_clear (vinsn_vec_t *vinsn_vec)
  3055. {
  3056. unsigned len = vinsn_vec->length ();
  3057. if (len > 0)
  3058. {
  3059. vinsn_t vinsn;
  3060. int n;
  3061. FOR_EACH_VEC_ELT (*vinsn_vec, n, vinsn)
  3062. vinsn_detach (vinsn);
  3063. vinsn_vec->block_remove (0, len);
  3064. }
  3065. }
  3066. /* Add the vinsn of EXPR to the VINSN_VEC. */
  3067. static void
  3068. vinsn_vec_add (vinsn_vec_t *vinsn_vec, expr_t expr)
  3069. {
  3070. vinsn_attach (EXPR_VINSN (expr));
  3071. vinsn_vec->safe_push (EXPR_VINSN (expr));
  3072. }
  3073. /* Free the vector representing blocked expressions. */
  3074. static void
  3075. vinsn_vec_free (vinsn_vec_t &vinsn_vec)
  3076. {
  3077. vinsn_vec.release ();
  3078. }
  3079. /* Increase EXPR_PRIORITY_ADJ for INSN by AMOUNT. */
  3080. void sel_add_to_insn_priority (rtx insn, int amount)
  3081. {
  3082. EXPR_PRIORITY_ADJ (INSN_EXPR (insn)) += amount;
  3083. if (sched_verbose >= 2)
  3084. sel_print ("sel_add_to_insn_priority: insn %d, by %d (now %d+%d).\n",
  3085. INSN_UID (insn), amount, EXPR_PRIORITY (INSN_EXPR (insn)),
  3086. EXPR_PRIORITY_ADJ (INSN_EXPR (insn)));
  3087. }
  3088. /* Turn AV into a vector, filter inappropriate insns and sort it. Return
  3089. true if there is something to schedule. BNDS and FENCE are current
  3090. boundaries and fence, respectively. If we need to stall for some cycles
  3091. before an expr from AV would become available, write this number to
  3092. *PNEED_STALL. */
  3093. static bool
  3094. fill_vec_av_set (av_set_t av, blist_t bnds, fence_t fence,
  3095. int *pneed_stall)
  3096. {
  3097. av_set_iterator si;
  3098. expr_t expr;
  3099. int sched_next_worked = 0, stalled, n;
  3100. static int av_max_prio, est_ticks_till_branch;
  3101. int min_need_stall = -1;
  3102. deps_t dc = BND_DC (BLIST_BND (bnds));
  3103. /* Bail out early when the ready list contained only USEs/CLOBBERs that are
  3104. already scheduled. */
  3105. if (av == NULL)
  3106. return false;
  3107. /* Empty vector from the previous stuff. */
  3108. if (vec_av_set.length () > 0)
  3109. vec_av_set.block_remove (0, vec_av_set.length ());
  3110. /* Turn the set into a vector for sorting and call sel_target_adjust_priority
  3111. for each insn. */
  3112. gcc_assert (vec_av_set.is_empty ());
  3113. FOR_EACH_EXPR (expr, si, av)
  3114. {
  3115. vec_av_set.safe_push (expr);
  3116. gcc_assert (EXPR_PRIORITY_ADJ (expr) == 0 || *pneed_stall);
  3117. /* Adjust priority using target backend hook. */
  3118. sel_target_adjust_priority (expr);
  3119. }
  3120. /* Sort the vector. */
  3121. vec_av_set.qsort (sel_rank_for_schedule);
  3122. /* We record maximal priority of insns in av set for current instruction
  3123. group. */
  3124. if (FENCE_STARTS_CYCLE_P (fence))
  3125. av_max_prio = est_ticks_till_branch = INT_MIN;
  3126. /* Filter out inappropriate expressions. Loop's direction is reversed to
  3127. visit "best" instructions first. We assume that vec::unordered_remove
  3128. moves last element in place of one being deleted. */
  3129. for (n = vec_av_set.length () - 1, stalled = 0; n >= 0; n--)
  3130. {
  3131. expr_t expr = vec_av_set[n];
  3132. insn_t insn = EXPR_INSN_RTX (expr);
  3133. signed char target_available;
  3134. bool is_orig_reg_p = true;
  3135. int need_cycles, new_prio;
  3136. bool fence_insn_p = INSN_UID (insn) == INSN_UID (FENCE_INSN (fence));
  3137. /* Don't allow any insns other than from SCHED_GROUP if we have one. */
  3138. if (FENCE_SCHED_NEXT (fence) && insn != FENCE_SCHED_NEXT (fence))
  3139. {
  3140. vec_av_set.unordered_remove (n);
  3141. continue;
  3142. }
  3143. /* Set number of sched_next insns (just in case there
  3144. could be several). */
  3145. if (FENCE_SCHED_NEXT (fence))
  3146. sched_next_worked++;
  3147. /* Check all liveness requirements and try renaming.
  3148. FIXME: try to minimize calls to this. */
  3149. target_available = EXPR_TARGET_AVAILABLE (expr);
  3150. /* If insn was already scheduled on the current fence,
  3151. set TARGET_AVAILABLE to -1 no matter what expr's attribute says. */
  3152. if (vinsn_vec_has_expr_p (vec_target_unavailable_vinsns, expr)
  3153. && !fence_insn_p)
  3154. target_available = -1;
  3155. /* If the availability of the EXPR is invalidated by the insertion of
  3156. bookkeeping earlier, make sure that we won't choose this expr for
  3157. scheduling if it's not separable, and if it is separable, then
  3158. we have to recompute the set of available registers for it. */
  3159. if (vinsn_vec_has_expr_p (vec_bookkeeping_blocked_vinsns, expr))
  3160. {
  3161. vec_av_set.unordered_remove (n);
  3162. if (sched_verbose >= 4)
  3163. sel_print ("Expr %d is blocked by bookkeeping inserted earlier\n",
  3164. INSN_UID (insn));
  3165. continue;
  3166. }
  3167. if (target_available == true)
  3168. {
  3169. /* Do nothing -- we can use an existing register. */
  3170. is_orig_reg_p = EXPR_SEPARABLE_P (expr);
  3171. }
  3172. else if (/* Non-separable instruction will never
  3173. get another register. */
  3174. (target_available == false
  3175. && !EXPR_SEPARABLE_P (expr))
  3176. /* Don't try to find a register for low-priority expression. */
  3177. || (int) vec_av_set.length () - 1 - n >= max_insns_to_rename
  3178. /* ??? FIXME: Don't try to rename data speculation. */
  3179. || (EXPR_SPEC_DONE_DS (expr) & BEGIN_DATA)
  3180. || ! find_best_reg_for_expr (expr, bnds, &is_orig_reg_p))
  3181. {
  3182. vec_av_set.unordered_remove (n);
  3183. if (sched_verbose >= 4)
  3184. sel_print ("Expr %d has no suitable target register\n",
  3185. INSN_UID (insn));
  3186. /* A fence insn should not get here. */
  3187. gcc_assert (!fence_insn_p);
  3188. continue;
  3189. }
  3190. /* At this point a fence insn should always be available. */
  3191. gcc_assert (!fence_insn_p
  3192. || INSN_UID (FENCE_INSN (fence)) == INSN_UID (EXPR_INSN_RTX (expr)));
  3193. /* Filter expressions that need to be renamed or speculated when
  3194. pipelining, because compensating register copies or speculation
  3195. checks are likely to be placed near the beginning of the loop,
  3196. causing a stall. */
  3197. if (pipelining_p && EXPR_ORIG_SCHED_CYCLE (expr) > 0
  3198. && (!is_orig_reg_p || EXPR_SPEC_DONE_DS (expr) != 0))
  3199. {
  3200. /* Estimation of number of cycles until loop branch for
  3201. renaming/speculation to be successful. */
  3202. int need_n_ticks_till_branch = sel_vinsn_cost (EXPR_VINSN (expr));
  3203. if ((int) current_loop_nest->ninsns < 9)
  3204. {
  3205. vec_av_set.unordered_remove (n);
  3206. if (sched_verbose >= 4)
  3207. sel_print ("Pipelining expr %d will likely cause stall\n",
  3208. INSN_UID (insn));
  3209. continue;
  3210. }
  3211. if ((int) current_loop_nest->ninsns - num_insns_scheduled
  3212. < need_n_ticks_till_branch * issue_rate / 2
  3213. && est_ticks_till_branch < need_n_ticks_till_branch)
  3214. {
  3215. vec_av_set.unordered_remove (n);
  3216. if (sched_verbose >= 4)
  3217. sel_print ("Pipelining expr %d will likely cause stall\n",
  3218. INSN_UID (insn));
  3219. continue;
  3220. }
  3221. }
  3222. /* We want to schedule speculation checks as late as possible. Discard
  3223. them from av set if there are instructions with higher priority. */
  3224. if (sel_insn_is_speculation_check (insn)
  3225. && EXPR_PRIORITY (expr) < av_max_prio)
  3226. {
  3227. stalled++;
  3228. min_need_stall = min_need_stall < 0 ? 1 : MIN (min_need_stall, 1);
  3229. vec_av_set.unordered_remove (n);
  3230. if (sched_verbose >= 4)
  3231. sel_print ("Delaying speculation check %d until its first use\n",
  3232. INSN_UID (insn));
  3233. continue;
  3234. }
  3235. /* Ignore EXPRs available from pipelining to update AV_MAX_PRIO. */
  3236. if (EXPR_ORIG_SCHED_CYCLE (expr) <= 0)
  3237. av_max_prio = MAX (av_max_prio, EXPR_PRIORITY (expr));
  3238. /* Don't allow any insns whose data is not yet ready.
  3239. Check first whether we've already tried them and failed. */
  3240. if (INSN_UID (insn) < FENCE_READY_TICKS_SIZE (fence))
  3241. {
  3242. need_cycles = (FENCE_READY_TICKS (fence)[INSN_UID (insn)]
  3243. - FENCE_CYCLE (fence));
  3244. if (EXPR_ORIG_SCHED_CYCLE (expr) <= 0)
  3245. est_ticks_till_branch = MAX (est_ticks_till_branch,
  3246. EXPR_PRIORITY (expr) + need_cycles);
  3247. if (need_cycles > 0)
  3248. {
  3249. stalled++;
  3250. min_need_stall = (min_need_stall < 0
  3251. ? need_cycles
  3252. : MIN (min_need_stall, need_cycles));
  3253. vec_av_set.unordered_remove (n);
  3254. if (sched_verbose >= 4)
  3255. sel_print ("Expr %d is not ready until cycle %d (cached)\n",
  3256. INSN_UID (insn),
  3257. FENCE_READY_TICKS (fence)[INSN_UID (insn)]);
  3258. continue;
  3259. }
  3260. }
  3261. /* Now resort to dependence analysis to find whether EXPR might be
  3262. stalled due to dependencies from FENCE's context. */
  3263. need_cycles = tick_check_p (expr, dc, fence);
  3264. new_prio = EXPR_PRIORITY (expr) + EXPR_PRIORITY_ADJ (expr) + need_cycles;
  3265. if (EXPR_ORIG_SCHED_CYCLE (expr) <= 0)
  3266. est_ticks_till_branch = MAX (est_ticks_till_branch,
  3267. new_prio);
  3268. if (need_cycles > 0)
  3269. {
  3270. if (INSN_UID (insn) >= FENCE_READY_TICKS_SIZE (fence))
  3271. {
  3272. int new_size = INSN_UID (insn) * 3 / 2;
  3273. FENCE_READY_TICKS (fence)
  3274. = (int *) xrecalloc (FENCE_READY_TICKS (fence),
  3275. new_size, FENCE_READY_TICKS_SIZE (fence),
  3276. sizeof (int));
  3277. }
  3278. FENCE_READY_TICKS (fence)[INSN_UID (insn)]
  3279. = FENCE_CYCLE (fence) + need_cycles;
  3280. stalled++;
  3281. min_need_stall = (min_need_stall < 0
  3282. ? need_cycles
  3283. : MIN (min_need_stall, need_cycles));
  3284. vec_av_set.unordered_remove (n);
  3285. if (sched_verbose >= 4)
  3286. sel_print ("Expr %d is not ready yet until cycle %d\n",
  3287. INSN_UID (insn),
  3288. FENCE_READY_TICKS (fence)[INSN_UID (insn)]);
  3289. continue;
  3290. }
  3291. if (sched_verbose >= 4)
  3292. sel_print ("Expr %d is ok\n", INSN_UID (insn));
  3293. min_need_stall = 0;
  3294. }
  3295. /* Clear SCHED_NEXT. */
  3296. if (FENCE_SCHED_NEXT (fence))
  3297. {
  3298. gcc_assert (sched_next_worked == 1);
  3299. FENCE_SCHED_NEXT (fence) = NULL;
  3300. }
  3301. /* No need to stall if this variable was not initialized. */
  3302. if (min_need_stall < 0)
  3303. min_need_stall = 0;
  3304. if (vec_av_set.is_empty ())
  3305. {
  3306. /* We need to set *pneed_stall here, because later we skip this code
  3307. when ready list is empty. */
  3308. *pneed_stall = min_need_stall;
  3309. return false;
  3310. }
  3311. else
  3312. gcc_assert (min_need_stall == 0);
  3313. /* Sort the vector. */
  3314. vec_av_set.qsort (sel_rank_for_schedule);
  3315. if (sched_verbose >= 4)
  3316. {
  3317. sel_print ("Total ready exprs: %d, stalled: %d\n",
  3318. vec_av_set.length (), stalled);
  3319. sel_print ("Sorted av set (%d): ", vec_av_set.length ());
  3320. FOR_EACH_VEC_ELT (vec_av_set, n, expr)
  3321. dump_expr (expr);
  3322. sel_print ("\n");
  3323. }
  3324. *pneed_stall = 0;
  3325. return true;
  3326. }
  3327. /* Convert a vectored and sorted av set to the ready list that
  3328. the rest of the backend wants to see. */
  3329. static void
  3330. convert_vec_av_set_to_ready (void)
  3331. {
  3332. int n;
  3333. expr_t expr;
  3334. /* Allocate and fill the ready list from the sorted vector. */
  3335. ready.n_ready = vec_av_set.length ();
  3336. ready.first = ready.n_ready - 1;
  3337. gcc_assert (ready.n_ready > 0);
  3338. if (ready.n_ready > max_issue_size)
  3339. {
  3340. max_issue_size = ready.n_ready;
  3341. sched_extend_ready_list (ready.n_ready);
  3342. }
  3343. FOR_EACH_VEC_ELT (vec_av_set, n, expr)
  3344. {
  3345. vinsn_t vi = EXPR_VINSN (expr);
  3346. insn_t insn = VINSN_INSN_RTX (vi);
  3347. ready_try[n] = 0;
  3348. ready.vec[n] = insn;
  3349. }
  3350. }
  3351. /* Initialize ready list from *AV_PTR for the max_issue () call.
  3352. If any unrecognizable insn found in *AV_PTR, return it (and skip
  3353. max_issue). BND and FENCE are current boundary and fence,
  3354. respectively. If we need to stall for some cycles before an expr
  3355. from *AV_PTR would become available, write this number to *PNEED_STALL. */
  3356. static expr_t
  3357. fill_ready_list (av_set_t *av_ptr, blist_t bnds, fence_t fence,
  3358. int *pneed_stall)
  3359. {
  3360. expr_t expr;
  3361. /* We do not support multiple boundaries per fence. */
  3362. gcc_assert (BLIST_NEXT (bnds) == NULL);
  3363. /* Process expressions required special handling, i.e. pipelined,
  3364. speculative and recog() < 0 expressions first. */
  3365. process_pipelined_exprs (av_ptr);
  3366. process_spec_exprs (av_ptr);
  3367. /* A USE could be scheduled immediately. */
  3368. expr = process_use_exprs (av_ptr);
  3369. if (expr)
  3370. {
  3371. *pneed_stall = 0;
  3372. return expr;
  3373. }
  3374. /* Turn the av set to a vector for sorting. */
  3375. if (! fill_vec_av_set (*av_ptr, bnds, fence, pneed_stall))
  3376. {
  3377. ready.n_ready = 0;
  3378. return NULL;
  3379. }
  3380. /* Build the final ready list. */
  3381. convert_vec_av_set_to_ready ();
  3382. return NULL;
  3383. }
  3384. /* Wrapper for dfa_new_cycle (). Returns TRUE if cycle was advanced. */
  3385. static bool
  3386. sel_dfa_new_cycle (insn_t insn, fence_t fence)
  3387. {
  3388. int last_scheduled_cycle = FENCE_LAST_SCHEDULED_INSN (fence)
  3389. ? INSN_SCHED_CYCLE (FENCE_LAST_SCHEDULED_INSN (fence))
  3390. : FENCE_CYCLE (fence) - 1;
  3391. bool res = false;
  3392. int sort_p = 0;
  3393. if (!targetm.sched.dfa_new_cycle)
  3394. return false;
  3395. memcpy (curr_state, FENCE_STATE (fence), dfa_state_size);
  3396. while (!sort_p && targetm.sched.dfa_new_cycle (sched_dump, sched_verbose,
  3397. insn, last_scheduled_cycle,
  3398. FENCE_CYCLE (fence), &sort_p))
  3399. {
  3400. memcpy (FENCE_STATE (fence), curr_state, dfa_state_size);
  3401. advance_one_cycle (fence);
  3402. memcpy (curr_state, FENCE_STATE (fence), dfa_state_size);
  3403. res = true;
  3404. }
  3405. return res;
  3406. }
  3407. /* Invoke reorder* target hooks on the ready list. Return the number of insns
  3408. we can issue. FENCE is the current fence. */
  3409. static int
  3410. invoke_reorder_hooks (fence_t fence)
  3411. {
  3412. int issue_more;
  3413. bool ran_hook = false;
  3414. /* Call the reorder hook at the beginning of the cycle, and call
  3415. the reorder2 hook in the middle of the cycle. */
  3416. if (FENCE_ISSUED_INSNS (fence) == 0)
  3417. {
  3418. if (targetm.sched.reorder
  3419. && !SCHED_GROUP_P (ready_element (&ready, 0))
  3420. && ready.n_ready > 1)
  3421. {
  3422. /* Don't give reorder the most prioritized insn as it can break
  3423. pipelining. */
  3424. if (pipelining_p)
  3425. --ready.n_ready;
  3426. issue_more
  3427. = targetm.sched.reorder (sched_dump, sched_verbose,
  3428. ready_lastpos (&ready),
  3429. &ready.n_ready, FENCE_CYCLE (fence));
  3430. if (pipelining_p)
  3431. ++ready.n_ready;
  3432. ran_hook = true;
  3433. }
  3434. else
  3435. /* Initialize can_issue_more for variable_issue. */
  3436. issue_more = issue_rate;
  3437. }
  3438. else if (targetm.sched.reorder2
  3439. && !SCHED_GROUP_P (ready_element (&ready, 0)))
  3440. {
  3441. if (ready.n_ready == 1)
  3442. issue_more =
  3443. targetm.sched.reorder2 (sched_dump, sched_verbose,
  3444. ready_lastpos (&ready),
  3445. &ready.n_ready, FENCE_CYCLE (fence));
  3446. else
  3447. {
  3448. if (pipelining_p)
  3449. --ready.n_ready;
  3450. issue_more =
  3451. targetm.sched.reorder2 (sched_dump, sched_verbose,
  3452. ready.n_ready
  3453. ? ready_lastpos (&ready) : NULL,
  3454. &ready.n_ready, FENCE_CYCLE (fence));
  3455. if (pipelining_p)
  3456. ++ready.n_ready;
  3457. }
  3458. ran_hook = true;
  3459. }
  3460. else
  3461. issue_more = FENCE_ISSUE_MORE (fence);
  3462. /* Ensure that ready list and vec_av_set are in line with each other,
  3463. i.e. vec_av_set[i] == ready_element (&ready, i). */
  3464. if (issue_more && ran_hook)
  3465. {
  3466. int i, j, n;
  3467. rtx_insn **arr = ready.vec;
  3468. expr_t *vec = vec_av_set.address ();
  3469. for (i = 0, n = ready.n_ready; i < n; i++)
  3470. if (EXPR_INSN_RTX (vec[i]) != arr[i])
  3471. {
  3472. expr_t tmp;
  3473. for (j = i; j < n; j++)
  3474. if (EXPR_INSN_RTX (vec[j]) == arr[i])
  3475. break;
  3476. gcc_assert (j < n);
  3477. tmp = vec[i];
  3478. vec[i] = vec[j];
  3479. vec[j] = tmp;
  3480. }
  3481. }
  3482. return issue_more;
  3483. }
  3484. /* Return an EXPR corresponding to INDEX element of ready list, if
  3485. FOLLOW_READY_ELEMENT is true (i.e., an expr of
  3486. ready_element (&ready, INDEX) will be returned), and to INDEX element of
  3487. ready.vec otherwise. */
  3488. static inline expr_t
  3489. find_expr_for_ready (int index, bool follow_ready_element)
  3490. {
  3491. expr_t expr;
  3492. int real_index;
  3493. real_index = follow_ready_element ? ready.first - index : index;
  3494. expr = vec_av_set[real_index];
  3495. gcc_assert (ready.vec[real_index] == EXPR_INSN_RTX (expr));
  3496. return expr;
  3497. }
  3498. /* Calculate insns worth trying via lookahead_guard hook. Return a number
  3499. of such insns found. */
  3500. static int
  3501. invoke_dfa_lookahead_guard (void)
  3502. {
  3503. int i, n;
  3504. bool have_hook
  3505. = targetm.sched.first_cycle_multipass_dfa_lookahead_guard != NULL;
  3506. if (sched_verbose >= 2)
  3507. sel_print ("ready after reorder: ");
  3508. for (i = 0, n = 0; i < ready.n_ready; i++)
  3509. {
  3510. expr_t expr;
  3511. insn_t insn;
  3512. int r;
  3513. /* In this loop insn is Ith element of the ready list given by
  3514. ready_element, not Ith element of ready.vec. */
  3515. insn = ready_element (&ready, i);
  3516. if (! have_hook || i == 0)
  3517. r = 0;
  3518. else
  3519. r = targetm.sched.first_cycle_multipass_dfa_lookahead_guard (insn, i);
  3520. gcc_assert (INSN_CODE (insn) >= 0);
  3521. /* Only insns with ready_try = 0 can get here
  3522. from fill_ready_list. */
  3523. gcc_assert (ready_try [i] == 0);
  3524. ready_try[i] = r;
  3525. if (!r)
  3526. n++;
  3527. expr = find_expr_for_ready (i, true);
  3528. if (sched_verbose >= 2)
  3529. {
  3530. dump_vinsn (EXPR_VINSN (expr));
  3531. sel_print (":%d; ", ready_try[i]);
  3532. }
  3533. }
  3534. if (sched_verbose >= 2)
  3535. sel_print ("\n");
  3536. return n;
  3537. }
  3538. /* Calculate the number of privileged insns and return it. */
  3539. static int
  3540. calculate_privileged_insns (void)
  3541. {
  3542. expr_t cur_expr, min_spec_expr = NULL;
  3543. int privileged_n = 0, i;
  3544. for (i = 0; i < ready.n_ready; i++)
  3545. {
  3546. if (ready_try[i])
  3547. continue;
  3548. if (! min_spec_expr)
  3549. min_spec_expr = find_expr_for_ready (i, true);
  3550. cur_expr = find_expr_for_ready (i, true);
  3551. if (EXPR_SPEC (cur_expr) > EXPR_SPEC (min_spec_expr))
  3552. break;
  3553. ++privileged_n;
  3554. }
  3555. if (i == ready.n_ready)
  3556. privileged_n = 0;
  3557. if (sched_verbose >= 2)
  3558. sel_print ("privileged_n: %d insns with SPEC %d\n",
  3559. privileged_n, privileged_n ? EXPR_SPEC (min_spec_expr) : -1);
  3560. return privileged_n;
  3561. }
  3562. /* Call the rest of the hooks after the choice was made. Return
  3563. the number of insns that still can be issued given that the current
  3564. number is ISSUE_MORE. FENCE and BEST_INSN are the current fence
  3565. and the insn chosen for scheduling, respectively. */
  3566. static int
  3567. invoke_aftermath_hooks (fence_t fence, rtx_insn *best_insn, int issue_more)
  3568. {
  3569. gcc_assert (INSN_P (best_insn));
  3570. /* First, call dfa_new_cycle, and then variable_issue, if available. */
  3571. sel_dfa_new_cycle (best_insn, fence);
  3572. if (targetm.sched.variable_issue)
  3573. {
  3574. memcpy (curr_state, FENCE_STATE (fence), dfa_state_size);
  3575. issue_more =
  3576. targetm.sched.variable_issue (sched_dump, sched_verbose, best_insn,
  3577. issue_more);
  3578. memcpy (FENCE_STATE (fence), curr_state, dfa_state_size);
  3579. }
  3580. else if (GET_CODE (PATTERN (best_insn)) != USE
  3581. && GET_CODE (PATTERN (best_insn)) != CLOBBER)
  3582. issue_more--;
  3583. return issue_more;
  3584. }
  3585. /* Estimate the cost of issuing INSN on DFA state STATE. */
  3586. static int
  3587. estimate_insn_cost (rtx_insn *insn, state_t state)
  3588. {
  3589. static state_t temp = NULL;
  3590. int cost;
  3591. if (!temp)
  3592. temp = xmalloc (dfa_state_size);
  3593. memcpy (temp, state, dfa_state_size);
  3594. cost = state_transition (temp, insn);
  3595. if (cost < 0)
  3596. return 0;
  3597. else if (cost == 0)
  3598. return 1;
  3599. return cost;
  3600. }
  3601. /* Return the cost of issuing EXPR on the FENCE as estimated by DFA.
  3602. This function properly handles ASMs, USEs etc. */
  3603. static int
  3604. get_expr_cost (expr_t expr, fence_t fence)
  3605. {
  3606. rtx_insn *insn = EXPR_INSN_RTX (expr);
  3607. if (recog_memoized (insn) < 0)
  3608. {
  3609. if (!FENCE_STARTS_CYCLE_P (fence)
  3610. && INSN_ASM_P (insn))
  3611. /* This is asm insn which is tryed to be issued on the
  3612. cycle not first. Issue it on the next cycle. */
  3613. return 1;
  3614. else
  3615. /* A USE insn, or something else we don't need to
  3616. understand. We can't pass these directly to
  3617. state_transition because it will trigger a
  3618. fatal error for unrecognizable insns. */
  3619. return 0;
  3620. }
  3621. else
  3622. return estimate_insn_cost (insn, FENCE_STATE (fence));
  3623. }
  3624. /* Find the best insn for scheduling, either via max_issue or just take
  3625. the most prioritized available. */
  3626. static int
  3627. choose_best_insn (fence_t fence, int privileged_n, int *index)
  3628. {
  3629. int can_issue = 0;
  3630. if (dfa_lookahead > 0)
  3631. {
  3632. cycle_issued_insns = FENCE_ISSUED_INSNS (fence);
  3633. /* TODO: pass equivalent of first_cycle_insn_p to max_issue (). */
  3634. can_issue = max_issue (&ready, privileged_n,
  3635. FENCE_STATE (fence), true, index);
  3636. if (sched_verbose >= 2)
  3637. sel_print ("max_issue: we can issue %d insns, already did %d insns\n",
  3638. can_issue, FENCE_ISSUED_INSNS (fence));
  3639. }
  3640. else
  3641. {
  3642. /* We can't use max_issue; just return the first available element. */
  3643. int i;
  3644. for (i = 0; i < ready.n_ready; i++)
  3645. {
  3646. expr_t expr = find_expr_for_ready (i, true);
  3647. if (get_expr_cost (expr, fence) < 1)
  3648. {
  3649. can_issue = can_issue_more;
  3650. *index = i;
  3651. if (sched_verbose >= 2)
  3652. sel_print ("using %dth insn from the ready list\n", i + 1);
  3653. break;
  3654. }
  3655. }
  3656. if (i == ready.n_ready)
  3657. {
  3658. can_issue = 0;
  3659. *index = -1;
  3660. }
  3661. }
  3662. return can_issue;
  3663. }
  3664. /* Choose the best expr from *AV_VLIW_PTR and a suitable register for it.
  3665. BNDS and FENCE are current boundaries and scheduling fence respectively.
  3666. Return the expr found and NULL if nothing can be issued atm.
  3667. Write to PNEED_STALL the number of cycles to stall if no expr was found. */
  3668. static expr_t
  3669. find_best_expr (av_set_t *av_vliw_ptr, blist_t bnds, fence_t fence,
  3670. int *pneed_stall)
  3671. {
  3672. expr_t best;
  3673. /* Choose the best insn for scheduling via:
  3674. 1) sorting the ready list based on priority;
  3675. 2) calling the reorder hook;
  3676. 3) calling max_issue. */
  3677. best = fill_ready_list (av_vliw_ptr, bnds, fence, pneed_stall);
  3678. if (best == NULL && ready.n_ready > 0)
  3679. {
  3680. int privileged_n, index;
  3681. can_issue_more = invoke_reorder_hooks (fence);
  3682. if (can_issue_more > 0)
  3683. {
  3684. /* Try choosing the best insn until we find one that is could be
  3685. scheduled due to liveness restrictions on its destination register.
  3686. In the future, we'd like to choose once and then just probe insns
  3687. in the order of their priority. */
  3688. invoke_dfa_lookahead_guard ();
  3689. privileged_n = calculate_privileged_insns ();
  3690. can_issue_more = choose_best_insn (fence, privileged_n, &index);
  3691. if (can_issue_more)
  3692. best = find_expr_for_ready (index, true);
  3693. }
  3694. /* We had some available insns, so if we can't issue them,
  3695. we have a stall. */
  3696. if (can_issue_more == 0)
  3697. {
  3698. best = NULL;
  3699. *pneed_stall = 1;
  3700. }
  3701. }
  3702. if (best != NULL)
  3703. {
  3704. can_issue_more = invoke_aftermath_hooks (fence, EXPR_INSN_RTX (best),
  3705. can_issue_more);
  3706. if (targetm.sched.variable_issue
  3707. && can_issue_more == 0)
  3708. *pneed_stall = 1;
  3709. }
  3710. if (sched_verbose >= 2)
  3711. {
  3712. if (best != NULL)
  3713. {
  3714. sel_print ("Best expression (vliw form): ");
  3715. dump_expr (best);
  3716. sel_print ("; cycle %d\n", FENCE_CYCLE (fence));
  3717. }
  3718. else
  3719. sel_print ("No best expr found!\n");
  3720. }
  3721. return best;
  3722. }
  3723. /* Functions that implement the core of the scheduler. */
  3724. /* Emit an instruction from EXPR with SEQNO and VINSN after
  3725. PLACE_TO_INSERT. */
  3726. static insn_t
  3727. emit_insn_from_expr_after (expr_t expr, vinsn_t vinsn, int seqno,
  3728. insn_t place_to_insert)
  3729. {
  3730. /* This assert fails when we have identical instructions
  3731. one of which dominates the other. In this case move_op ()
  3732. finds the first instruction and doesn't search for second one.
  3733. The solution would be to compute av_set after the first found
  3734. insn and, if insn present in that set, continue searching.
  3735. For now we workaround this issue in move_op. */
  3736. gcc_assert (!INSN_IN_STREAM_P (EXPR_INSN_RTX (expr)));
  3737. if (EXPR_WAS_RENAMED (expr))
  3738. {
  3739. unsigned regno = expr_dest_regno (expr);
  3740. if (HARD_REGISTER_NUM_P (regno))
  3741. {
  3742. df_set_regs_ever_live (regno, true);
  3743. reg_rename_tick[regno] = ++reg_rename_this_tick;
  3744. }
  3745. }
  3746. return sel_gen_insn_from_expr_after (expr, vinsn, seqno,
  3747. place_to_insert);
  3748. }
  3749. /* Return TRUE if BB can hold bookkeeping code. */
  3750. static bool
  3751. block_valid_for_bookkeeping_p (basic_block bb)
  3752. {
  3753. insn_t bb_end = BB_END (bb);
  3754. if (!in_current_region_p (bb) || EDGE_COUNT (bb->succs) > 1)
  3755. return false;
  3756. if (INSN_P (bb_end))
  3757. {
  3758. if (INSN_SCHED_TIMES (bb_end) > 0)
  3759. return false;
  3760. }
  3761. else
  3762. gcc_assert (NOTE_INSN_BASIC_BLOCK_P (bb_end));
  3763. return true;
  3764. }
  3765. /* Attempt to find a block that can hold bookkeeping code for path(s) incoming
  3766. into E2->dest, except from E1->src (there may be a sequence of empty basic
  3767. blocks between E1->src and E2->dest). Return found block, or NULL if new
  3768. one must be created. If LAX holds, don't assume there is a simple path
  3769. from E1->src to E2->dest. */
  3770. static basic_block
  3771. find_block_for_bookkeeping (edge e1, edge e2, bool lax)
  3772. {
  3773. basic_block candidate_block = NULL;
  3774. edge e;
  3775. /* Loop over edges from E1 to E2, inclusive. */
  3776. for (e = e1; !lax || e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun); e =
  3777. EDGE_SUCC (e->dest, 0))
  3778. {
  3779. if (EDGE_COUNT (e->dest->preds) == 2)
  3780. {
  3781. if (candidate_block == NULL)
  3782. candidate_block = (EDGE_PRED (e->dest, 0) == e
  3783. ? EDGE_PRED (e->dest, 1)->src
  3784. : EDGE_PRED (e->dest, 0)->src);
  3785. else
  3786. /* Found additional edge leading to path from e1 to e2
  3787. from aside. */
  3788. return NULL;
  3789. }
  3790. else if (EDGE_COUNT (e->dest->preds) > 2)
  3791. /* Several edges leading to path from e1 to e2 from aside. */
  3792. return NULL;
  3793. if (e == e2)
  3794. return ((!lax || candidate_block)
  3795. && block_valid_for_bookkeeping_p (candidate_block)
  3796. ? candidate_block
  3797. : NULL);
  3798. if (lax && EDGE_COUNT (e->dest->succs) != 1)
  3799. return NULL;
  3800. }
  3801. if (lax)
  3802. return NULL;
  3803. gcc_unreachable ();
  3804. }
  3805. /* Create new basic block for bookkeeping code for path(s) incoming into
  3806. E2->dest, except from E1->src. Return created block. */
  3807. static basic_block
  3808. create_block_for_bookkeeping (edge e1, edge e2)
  3809. {
  3810. basic_block new_bb, bb = e2->dest;
  3811. /* Check that we don't spoil the loop structure. */
  3812. if (current_loop_nest)
  3813. {
  3814. basic_block latch = current_loop_nest->latch;
  3815. /* We do not split header. */
  3816. gcc_assert (e2->dest != current_loop_nest->header);
  3817. /* We do not redirect the only edge to the latch block. */
  3818. gcc_assert (e1->dest != latch
  3819. || !single_pred_p (latch)
  3820. || e1 != single_pred_edge (latch));
  3821. }
  3822. /* Split BB to insert BOOK_INSN there. */
  3823. new_bb = sched_split_block (bb, NULL);
  3824. /* Move note_list from the upper bb. */
  3825. gcc_assert (BB_NOTE_LIST (new_bb) == NULL_RTX);
  3826. BB_NOTE_LIST (new_bb) = BB_NOTE_LIST (bb);
  3827. BB_NOTE_LIST (bb) = NULL;
  3828. gcc_assert (e2->dest == bb);
  3829. /* Skip block for bookkeeping copy when leaving E1->src. */
  3830. if (e1->flags & EDGE_FALLTHRU)
  3831. sel_redirect_edge_and_branch_force (e1, new_bb);
  3832. else
  3833. sel_redirect_edge_and_branch (e1, new_bb);
  3834. gcc_assert (e1->dest == new_bb);
  3835. gcc_assert (sel_bb_empty_p (bb));
  3836. /* To keep basic block numbers in sync between debug and non-debug
  3837. compilations, we have to rotate blocks here. Consider that we
  3838. started from (a,b)->d, (c,d)->e, and d contained only debug
  3839. insns. It would have been removed before if the debug insns
  3840. weren't there, so we'd have split e rather than d. So what we do
  3841. now is to swap the block numbers of new_bb and
  3842. single_succ(new_bb) == e, so that the insns that were in e before
  3843. get the new block number. */
  3844. if (MAY_HAVE_DEBUG_INSNS)
  3845. {
  3846. basic_block succ;
  3847. insn_t insn = sel_bb_head (new_bb);
  3848. insn_t last;
  3849. if (DEBUG_INSN_P (insn)
  3850. && single_succ_p (new_bb)
  3851. && (succ = single_succ (new_bb))
  3852. && succ != EXIT_BLOCK_PTR_FOR_FN (cfun)
  3853. && DEBUG_INSN_P ((last = sel_bb_end (new_bb))))
  3854. {
  3855. while (insn != last && (DEBUG_INSN_P (insn) || NOTE_P (insn)))
  3856. insn = NEXT_INSN (insn);
  3857. if (insn == last)
  3858. {
  3859. sel_global_bb_info_def gbi;
  3860. sel_region_bb_info_def rbi;
  3861. int i;
  3862. if (sched_verbose >= 2)
  3863. sel_print ("Swapping block ids %i and %i\n",
  3864. new_bb->index, succ->index);
  3865. i = new_bb->index;
  3866. new_bb->index = succ->index;
  3867. succ->index = i;
  3868. SET_BASIC_BLOCK_FOR_FN (cfun, new_bb->index, new_bb);
  3869. SET_BASIC_BLOCK_FOR_FN (cfun, succ->index, succ);
  3870. memcpy (&gbi, SEL_GLOBAL_BB_INFO (new_bb), sizeof (gbi));
  3871. memcpy (SEL_GLOBAL_BB_INFO (new_bb), SEL_GLOBAL_BB_INFO (succ),
  3872. sizeof (gbi));
  3873. memcpy (SEL_GLOBAL_BB_INFO (succ), &gbi, sizeof (gbi));
  3874. memcpy (&rbi, SEL_REGION_BB_INFO (new_bb), sizeof (rbi));
  3875. memcpy (SEL_REGION_BB_INFO (new_bb), SEL_REGION_BB_INFO (succ),
  3876. sizeof (rbi));
  3877. memcpy (SEL_REGION_BB_INFO (succ), &rbi, sizeof (rbi));
  3878. i = BLOCK_TO_BB (new_bb->index);
  3879. BLOCK_TO_BB (new_bb->index) = BLOCK_TO_BB (succ->index);
  3880. BLOCK_TO_BB (succ->index) = i;
  3881. i = CONTAINING_RGN (new_bb->index);
  3882. CONTAINING_RGN (new_bb->index) = CONTAINING_RGN (succ->index);
  3883. CONTAINING_RGN (succ->index) = i;
  3884. for (i = 0; i < current_nr_blocks; i++)
  3885. if (BB_TO_BLOCK (i) == succ->index)
  3886. BB_TO_BLOCK (i) = new_bb->index;
  3887. else if (BB_TO_BLOCK (i) == new_bb->index)
  3888. BB_TO_BLOCK (i) = succ->index;
  3889. FOR_BB_INSNS (new_bb, insn)
  3890. if (INSN_P (insn))
  3891. EXPR_ORIG_BB_INDEX (INSN_EXPR (insn)) = new_bb->index;
  3892. FOR_BB_INSNS (succ, insn)
  3893. if (INSN_P (insn))
  3894. EXPR_ORIG_BB_INDEX (INSN_EXPR (insn)) = succ->index;
  3895. if (bitmap_clear_bit (code_motion_visited_blocks, new_bb->index))
  3896. bitmap_set_bit (code_motion_visited_blocks, succ->index);
  3897. gcc_assert (LABEL_P (BB_HEAD (new_bb))
  3898. && LABEL_P (BB_HEAD (succ)));
  3899. if (sched_verbose >= 4)
  3900. sel_print ("Swapping code labels %i and %i\n",
  3901. CODE_LABEL_NUMBER (BB_HEAD (new_bb)),
  3902. CODE_LABEL_NUMBER (BB_HEAD (succ)));
  3903. i = CODE_LABEL_NUMBER (BB_HEAD (new_bb));
  3904. CODE_LABEL_NUMBER (BB_HEAD (new_bb))
  3905. = CODE_LABEL_NUMBER (BB_HEAD (succ));
  3906. CODE_LABEL_NUMBER (BB_HEAD (succ)) = i;
  3907. }
  3908. }
  3909. }
  3910. return bb;
  3911. }
  3912. /* Return insn after which we must insert bookkeeping code for path(s) incoming
  3913. into E2->dest, except from E1->src. If the returned insn immediately
  3914. precedes a fence, assign that fence to *FENCE_TO_REWIND. */
  3915. static insn_t
  3916. find_place_for_bookkeeping (edge e1, edge e2, fence_t *fence_to_rewind)
  3917. {
  3918. insn_t place_to_insert;
  3919. /* Find a basic block that can hold bookkeeping. If it can be found, do not
  3920. create new basic block, but insert bookkeeping there. */
  3921. basic_block book_block = find_block_for_bookkeeping (e1, e2, FALSE);
  3922. if (book_block)
  3923. {
  3924. place_to_insert = BB_END (book_block);
  3925. /* Don't use a block containing only debug insns for
  3926. bookkeeping, this causes scheduling differences between debug
  3927. and non-debug compilations, for the block would have been
  3928. removed already. */
  3929. if (DEBUG_INSN_P (place_to_insert))
  3930. {
  3931. rtx_insn *insn = sel_bb_head (book_block);
  3932. while (insn != place_to_insert &&
  3933. (DEBUG_INSN_P (insn) || NOTE_P (insn)))
  3934. insn = NEXT_INSN (insn);
  3935. if (insn == place_to_insert)
  3936. book_block = NULL;
  3937. }
  3938. }
  3939. if (!book_block)
  3940. {
  3941. book_block = create_block_for_bookkeeping (e1, e2);
  3942. place_to_insert = BB_END (book_block);
  3943. if (sched_verbose >= 9)
  3944. sel_print ("New block is %i, split from bookkeeping block %i\n",
  3945. EDGE_SUCC (book_block, 0)->dest->index, book_block->index);
  3946. }
  3947. else
  3948. {
  3949. if (sched_verbose >= 9)
  3950. sel_print ("Pre-existing bookkeeping block is %i\n", book_block->index);
  3951. }
  3952. *fence_to_rewind = NULL;
  3953. /* If basic block ends with a jump, insert bookkeeping code right before it.
  3954. Notice if we are crossing a fence when taking PREV_INSN. */
  3955. if (INSN_P (place_to_insert) && control_flow_insn_p (place_to_insert))
  3956. {
  3957. *fence_to_rewind = flist_lookup (fences, place_to_insert);
  3958. place_to_insert = PREV_INSN (place_to_insert);
  3959. }
  3960. return place_to_insert;
  3961. }
  3962. /* Find a proper seqno for bookkeeing insn inserted at PLACE_TO_INSERT
  3963. for JOIN_POINT. */
  3964. static int
  3965. find_seqno_for_bookkeeping (insn_t place_to_insert, insn_t join_point)
  3966. {
  3967. int seqno;
  3968. rtx next;
  3969. /* Check if we are about to insert bookkeeping copy before a jump, and use
  3970. jump's seqno for the copy; otherwise, use JOIN_POINT's seqno. */
  3971. next = NEXT_INSN (place_to_insert);
  3972. if (INSN_P (next)
  3973. && JUMP_P (next)
  3974. && BLOCK_FOR_INSN (next) == BLOCK_FOR_INSN (place_to_insert))
  3975. {
  3976. gcc_assert (INSN_SCHED_TIMES (next) == 0);
  3977. seqno = INSN_SEQNO (next);
  3978. }
  3979. else if (INSN_SEQNO (join_point) > 0)
  3980. seqno = INSN_SEQNO (join_point);
  3981. else
  3982. {
  3983. seqno = get_seqno_by_preds (place_to_insert);
  3984. /* Sometimes the fences can move in such a way that there will be
  3985. no instructions with positive seqno around this bookkeeping.
  3986. This means that there will be no way to get to it by a regular
  3987. fence movement. Never mind because we pick up such pieces for
  3988. rescheduling anyways, so any positive value will do for now. */
  3989. if (seqno < 0)
  3990. {
  3991. gcc_assert (pipelining_p);
  3992. seqno = 1;
  3993. }
  3994. }
  3995. gcc_assert (seqno > 0);
  3996. return seqno;
  3997. }
  3998. /* Insert bookkeeping copy of C_EXPS's insn after PLACE_TO_INSERT, assigning
  3999. NEW_SEQNO to it. Return created insn. */
  4000. static insn_t
  4001. emit_bookkeeping_insn (insn_t place_to_insert, expr_t c_expr, int new_seqno)
  4002. {
  4003. rtx_insn *new_insn_rtx = create_copy_of_insn_rtx (EXPR_INSN_RTX (c_expr));
  4004. vinsn_t new_vinsn
  4005. = create_vinsn_from_insn_rtx (new_insn_rtx,
  4006. VINSN_UNIQUE_P (EXPR_VINSN (c_expr)));
  4007. insn_t new_insn = emit_insn_from_expr_after (c_expr, new_vinsn, new_seqno,
  4008. place_to_insert);
  4009. INSN_SCHED_TIMES (new_insn) = 0;
  4010. bitmap_set_bit (current_copies, INSN_UID (new_insn));
  4011. return new_insn;
  4012. }
  4013. /* Generate a bookkeeping copy of C_EXPR's insn for path(s) incoming into to
  4014. E2->dest, except from E1->src (there may be a sequence of empty blocks
  4015. between E1->src and E2->dest). Return block containing the copy.
  4016. All scheduler data is initialized for the newly created insn. */
  4017. static basic_block
  4018. generate_bookkeeping_insn (expr_t c_expr, edge e1, edge e2)
  4019. {
  4020. insn_t join_point, place_to_insert, new_insn;
  4021. int new_seqno;
  4022. bool need_to_exchange_data_sets;
  4023. fence_t fence_to_rewind;
  4024. if (sched_verbose >= 4)
  4025. sel_print ("Generating bookkeeping insn (%d->%d)\n", e1->src->index,
  4026. e2->dest->index);
  4027. join_point = sel_bb_head (e2->dest);
  4028. place_to_insert = find_place_for_bookkeeping (e1, e2, &fence_to_rewind);
  4029. new_seqno = find_seqno_for_bookkeeping (place_to_insert, join_point);
  4030. need_to_exchange_data_sets
  4031. = sel_bb_empty_p (BLOCK_FOR_INSN (place_to_insert));
  4032. new_insn = emit_bookkeeping_insn (place_to_insert, c_expr, new_seqno);
  4033. if (fence_to_rewind)
  4034. FENCE_INSN (fence_to_rewind) = new_insn;
  4035. /* When inserting bookkeeping insn in new block, av sets should be
  4036. following: old basic block (that now holds bookkeeping) data sets are
  4037. the same as was before generation of bookkeeping, and new basic block
  4038. (that now hold all other insns of old basic block) data sets are
  4039. invalid. So exchange data sets for these basic blocks as sel_split_block
  4040. mistakenly exchanges them in this case. Cannot do it earlier because
  4041. when single instruction is added to new basic block it should hold NULL
  4042. lv_set. */
  4043. if (need_to_exchange_data_sets)
  4044. exchange_data_sets (BLOCK_FOR_INSN (new_insn),
  4045. BLOCK_FOR_INSN (join_point));
  4046. stat_bookkeeping_copies++;
  4047. return BLOCK_FOR_INSN (new_insn);
  4048. }
  4049. /* Remove from AV_PTR all insns that may need bookkeeping when scheduling
  4050. on FENCE, but we are unable to copy them. */
  4051. static void
  4052. remove_insns_that_need_bookkeeping (fence_t fence, av_set_t *av_ptr)
  4053. {
  4054. expr_t expr;
  4055. av_set_iterator i;
  4056. /* An expression does not need bookkeeping if it is available on all paths
  4057. from current block to original block and current block dominates
  4058. original block. We check availability on all paths by examining
  4059. EXPR_SPEC; this is not equivalent, because it may be positive even
  4060. if expr is available on all paths (but if expr is not available on
  4061. any path, EXPR_SPEC will be positive). */
  4062. FOR_EACH_EXPR_1 (expr, i, av_ptr)
  4063. {
  4064. if (!control_flow_insn_p (EXPR_INSN_RTX (expr))
  4065. && (!bookkeeping_p || VINSN_UNIQUE_P (EXPR_VINSN (expr)))
  4066. && (EXPR_SPEC (expr)
  4067. || !EXPR_ORIG_BB_INDEX (expr)
  4068. || !dominated_by_p (CDI_DOMINATORS,
  4069. BASIC_BLOCK_FOR_FN (cfun,
  4070. EXPR_ORIG_BB_INDEX (expr)),
  4071. BLOCK_FOR_INSN (FENCE_INSN (fence)))))
  4072. {
  4073. if (sched_verbose >= 4)
  4074. sel_print ("Expr %d removed because it would need bookkeeping, which "
  4075. "cannot be created\n", INSN_UID (EXPR_INSN_RTX (expr)));
  4076. av_set_iter_remove (&i);
  4077. }
  4078. }
  4079. }
  4080. /* Moving conditional jump through some instructions.
  4081. Consider example:
  4082. ... <- current scheduling point
  4083. NOTE BASIC BLOCK: <- bb header
  4084. (p8) add r14=r14+0x9;;
  4085. (p8) mov [r14]=r23
  4086. (!p8) jump L1;;
  4087. NOTE BASIC BLOCK:
  4088. ...
  4089. We can schedule jump one cycle earlier, than mov, because they cannot be
  4090. executed together as their predicates are mutually exclusive.
  4091. This is done in this way: first, new fallthrough basic block is created
  4092. after jump (it is always can be done, because there already should be a
  4093. fallthrough block, where control flow goes in case of predicate being true -
  4094. in our example; otherwise there should be a dependence between those
  4095. instructions and jump and we cannot schedule jump right now);
  4096. next, all instructions between jump and current scheduling point are moved
  4097. to this new block. And the result is this:
  4098. NOTE BASIC BLOCK:
  4099. (!p8) jump L1 <- current scheduling point
  4100. NOTE BASIC BLOCK: <- bb header
  4101. (p8) add r14=r14+0x9;;
  4102. (p8) mov [r14]=r23
  4103. NOTE BASIC BLOCK:
  4104. ...
  4105. */
  4106. static void
  4107. move_cond_jump (rtx_insn *insn, bnd_t bnd)
  4108. {
  4109. edge ft_edge;
  4110. basic_block block_from, block_next, block_new, block_bnd, bb;
  4111. rtx_insn *next, *prev, *link, *head;
  4112. block_from = BLOCK_FOR_INSN (insn);
  4113. block_bnd = BLOCK_FOR_INSN (BND_TO (bnd));
  4114. prev = BND_TO (bnd);
  4115. #ifdef ENABLE_CHECKING
  4116. /* Moving of jump should not cross any other jumps or beginnings of new
  4117. basic blocks. The only exception is when we move a jump through
  4118. mutually exclusive insns along fallthru edges. */
  4119. if (block_from != block_bnd)
  4120. {
  4121. bb = block_from;
  4122. for (link = PREV_INSN (insn); link != PREV_INSN (prev);
  4123. link = PREV_INSN (link))
  4124. {
  4125. if (INSN_P (link))
  4126. gcc_assert (sched_insns_conditions_mutex_p (insn, link));
  4127. if (BLOCK_FOR_INSN (link) && BLOCK_FOR_INSN (link) != bb)
  4128. {
  4129. gcc_assert (single_pred (bb) == BLOCK_FOR_INSN (link));
  4130. bb = BLOCK_FOR_INSN (link);
  4131. }
  4132. }
  4133. }
  4134. #endif
  4135. /* Jump is moved to the boundary. */
  4136. next = PREV_INSN (insn);
  4137. BND_TO (bnd) = insn;
  4138. ft_edge = find_fallthru_edge_from (block_from);
  4139. block_next = ft_edge->dest;
  4140. /* There must be a fallthrough block (or where should go
  4141. control flow in case of false jump predicate otherwise?). */
  4142. gcc_assert (block_next);
  4143. /* Create new empty basic block after source block. */
  4144. block_new = sel_split_edge (ft_edge);
  4145. gcc_assert (block_new->next_bb == block_next
  4146. && block_from->next_bb == block_new);
  4147. /* Move all instructions except INSN to BLOCK_NEW. */
  4148. bb = block_bnd;
  4149. head = BB_HEAD (block_new);
  4150. while (bb != block_from->next_bb)
  4151. {
  4152. rtx_insn *from, *to;
  4153. from = bb == block_bnd ? prev : sel_bb_head (bb);
  4154. to = bb == block_from ? next : sel_bb_end (bb);
  4155. /* The jump being moved can be the first insn in the block.
  4156. In this case we don't have to move anything in this block. */
  4157. if (NEXT_INSN (to) != from)
  4158. {
  4159. reorder_insns (from, to, head);
  4160. for (link = to; link != head; link = PREV_INSN (link))
  4161. EXPR_ORIG_BB_INDEX (INSN_EXPR (link)) = block_new->index;
  4162. head = to;
  4163. }
  4164. /* Cleanup possibly empty blocks left. */
  4165. block_next = bb->next_bb;
  4166. if (bb != block_from)
  4167. tidy_control_flow (bb, false);
  4168. bb = block_next;
  4169. }
  4170. /* Assert there is no jump to BLOCK_NEW, only fallthrough edge. */
  4171. gcc_assert (NOTE_INSN_BASIC_BLOCK_P (BB_HEAD (block_new)));
  4172. gcc_assert (!sel_bb_empty_p (block_from)
  4173. && !sel_bb_empty_p (block_new));
  4174. /* Update data sets for BLOCK_NEW to represent that INSN and
  4175. instructions from the other branch of INSN is no longer
  4176. available at BLOCK_NEW. */
  4177. BB_AV_LEVEL (block_new) = global_level;
  4178. gcc_assert (BB_LV_SET (block_new) == NULL);
  4179. BB_LV_SET (block_new) = get_clear_regset_from_pool ();
  4180. update_data_sets (sel_bb_head (block_new));
  4181. /* INSN is a new basic block header - so prepare its data
  4182. structures and update availability and liveness sets. */
  4183. update_data_sets (insn);
  4184. if (sched_verbose >= 4)
  4185. sel_print ("Moving jump %d\n", INSN_UID (insn));
  4186. }
  4187. /* Remove nops generated during move_op for preventing removal of empty
  4188. basic blocks. */
  4189. static void
  4190. remove_temp_moveop_nops (bool full_tidying)
  4191. {
  4192. int i;
  4193. insn_t insn;
  4194. FOR_EACH_VEC_ELT (vec_temp_moveop_nops, i, insn)
  4195. {
  4196. gcc_assert (INSN_NOP_P (insn));
  4197. return_nop_to_pool (insn, full_tidying);
  4198. }
  4199. /* Empty the vector. */
  4200. if (vec_temp_moveop_nops.length () > 0)
  4201. vec_temp_moveop_nops.block_remove (0, vec_temp_moveop_nops.length ());
  4202. }
  4203. /* Records the maximal UID before moving up an instruction. Used for
  4204. distinguishing between bookkeeping copies and original insns. */
  4205. static int max_uid_before_move_op = 0;
  4206. /* Remove from AV_VLIW_P all instructions but next when debug counter
  4207. tells us so. Next instruction is fetched from BNDS. */
  4208. static void
  4209. remove_insns_for_debug (blist_t bnds, av_set_t *av_vliw_p)
  4210. {
  4211. if (! dbg_cnt (sel_sched_insn_cnt))
  4212. /* Leave only the next insn in av_vliw. */
  4213. {
  4214. av_set_iterator av_it;
  4215. expr_t expr;
  4216. bnd_t bnd = BLIST_BND (bnds);
  4217. insn_t next = BND_TO (bnd);
  4218. gcc_assert (BLIST_NEXT (bnds) == NULL);
  4219. FOR_EACH_EXPR_1 (expr, av_it, av_vliw_p)
  4220. if (EXPR_INSN_RTX (expr) != next)
  4221. av_set_iter_remove (&av_it);
  4222. }
  4223. }
  4224. /* Compute available instructions on BNDS. FENCE is the current fence. Write
  4225. the computed set to *AV_VLIW_P. */
  4226. static void
  4227. compute_av_set_on_boundaries (fence_t fence, blist_t bnds, av_set_t *av_vliw_p)
  4228. {
  4229. if (sched_verbose >= 2)
  4230. {
  4231. sel_print ("Boundaries: ");
  4232. dump_blist (bnds);
  4233. sel_print ("\n");
  4234. }
  4235. for (; bnds; bnds = BLIST_NEXT (bnds))
  4236. {
  4237. bnd_t bnd = BLIST_BND (bnds);
  4238. av_set_t av1_copy;
  4239. insn_t bnd_to = BND_TO (bnd);
  4240. /* Rewind BND->TO to the basic block header in case some bookkeeping
  4241. instructions were inserted before BND->TO and it needs to be
  4242. adjusted. */
  4243. if (sel_bb_head_p (bnd_to))
  4244. gcc_assert (INSN_SCHED_TIMES (bnd_to) == 0);
  4245. else
  4246. while (INSN_SCHED_TIMES (PREV_INSN (bnd_to)) == 0)
  4247. {
  4248. bnd_to = PREV_INSN (bnd_to);
  4249. if (sel_bb_head_p (bnd_to))
  4250. break;
  4251. }
  4252. if (BND_TO (bnd) != bnd_to)
  4253. {
  4254. gcc_assert (FENCE_INSN (fence) == BND_TO (bnd));
  4255. FENCE_INSN (fence) = bnd_to;
  4256. BND_TO (bnd) = bnd_to;
  4257. }
  4258. av_set_clear (&BND_AV (bnd));
  4259. BND_AV (bnd) = compute_av_set (BND_TO (bnd), NULL, 0, true);
  4260. av_set_clear (&BND_AV1 (bnd));
  4261. BND_AV1 (bnd) = av_set_copy (BND_AV (bnd));
  4262. moveup_set_inside_insn_group (&BND_AV1 (bnd), NULL);
  4263. av1_copy = av_set_copy (BND_AV1 (bnd));
  4264. av_set_union_and_clear (av_vliw_p, &av1_copy, NULL);
  4265. }
  4266. if (sched_verbose >= 2)
  4267. {
  4268. sel_print ("Available exprs (vliw form): ");
  4269. dump_av_set (*av_vliw_p);
  4270. sel_print ("\n");
  4271. }
  4272. }
  4273. /* Calculate the sequential av set on BND corresponding to the EXPR_VLIW
  4274. expression. When FOR_MOVEOP is true, also replace the register of
  4275. expressions found with the register from EXPR_VLIW. */
  4276. static av_set_t
  4277. find_sequential_best_exprs (bnd_t bnd, expr_t expr_vliw, bool for_moveop)
  4278. {
  4279. av_set_t expr_seq = NULL;
  4280. expr_t expr;
  4281. av_set_iterator i;
  4282. FOR_EACH_EXPR (expr, i, BND_AV (bnd))
  4283. {
  4284. if (equal_after_moveup_path_p (expr, NULL, expr_vliw))
  4285. {
  4286. if (for_moveop)
  4287. {
  4288. /* The sequential expression has the right form to pass
  4289. to move_op except when renaming happened. Put the
  4290. correct register in EXPR then. */
  4291. if (EXPR_SEPARABLE_P (expr) && REG_P (EXPR_LHS (expr)))
  4292. {
  4293. if (expr_dest_regno (expr) != expr_dest_regno (expr_vliw))
  4294. {
  4295. replace_dest_with_reg_in_expr (expr, EXPR_LHS (expr_vliw));
  4296. stat_renamed_scheduled++;
  4297. }
  4298. /* Also put the correct TARGET_AVAILABLE bit on the expr.
  4299. This is needed when renaming came up with original
  4300. register. */
  4301. else if (EXPR_TARGET_AVAILABLE (expr)
  4302. != EXPR_TARGET_AVAILABLE (expr_vliw))
  4303. {
  4304. gcc_assert (EXPR_TARGET_AVAILABLE (expr_vliw) == 1);
  4305. EXPR_TARGET_AVAILABLE (expr) = 1;
  4306. }
  4307. }
  4308. if (EXPR_WAS_SUBSTITUTED (expr))
  4309. stat_substitutions_total++;
  4310. }
  4311. av_set_add (&expr_seq, expr);
  4312. /* With substitution inside insn group, it is possible
  4313. that more than one expression in expr_seq will correspond
  4314. to expr_vliw. In this case, choose one as the attempt to
  4315. move both leads to miscompiles. */
  4316. break;
  4317. }
  4318. }
  4319. if (for_moveop && sched_verbose >= 2)
  4320. {
  4321. sel_print ("Best expression(s) (sequential form): ");
  4322. dump_av_set (expr_seq);
  4323. sel_print ("\n");
  4324. }
  4325. return expr_seq;
  4326. }
  4327. /* Move nop to previous block. */
  4328. static void ATTRIBUTE_UNUSED
  4329. move_nop_to_previous_block (insn_t nop, basic_block prev_bb)
  4330. {
  4331. insn_t prev_insn, next_insn, note;
  4332. gcc_assert (sel_bb_head_p (nop)
  4333. && prev_bb == BLOCK_FOR_INSN (nop)->prev_bb);
  4334. note = bb_note (BLOCK_FOR_INSN (nop));
  4335. prev_insn = sel_bb_end (prev_bb);
  4336. next_insn = NEXT_INSN (nop);
  4337. gcc_assert (prev_insn != NULL_RTX
  4338. && PREV_INSN (note) == prev_insn);
  4339. SET_NEXT_INSN (prev_insn) = nop;
  4340. SET_PREV_INSN (nop) = prev_insn;
  4341. SET_PREV_INSN (note) = nop;
  4342. SET_NEXT_INSN (note) = next_insn;
  4343. SET_NEXT_INSN (nop) = note;
  4344. SET_PREV_INSN (next_insn) = note;
  4345. BB_END (prev_bb) = nop;
  4346. BLOCK_FOR_INSN (nop) = prev_bb;
  4347. }
  4348. /* Prepare a place to insert the chosen expression on BND. */
  4349. static insn_t
  4350. prepare_place_to_insert (bnd_t bnd)
  4351. {
  4352. insn_t place_to_insert;
  4353. /* Init place_to_insert before calling move_op, as the later
  4354. can possibly remove BND_TO (bnd). */
  4355. if (/* If this is not the first insn scheduled. */
  4356. BND_PTR (bnd))
  4357. {
  4358. /* Add it after last scheduled. */
  4359. place_to_insert = ILIST_INSN (BND_PTR (bnd));
  4360. if (DEBUG_INSN_P (place_to_insert))
  4361. {
  4362. ilist_t l = BND_PTR (bnd);
  4363. while ((l = ILIST_NEXT (l)) &&
  4364. DEBUG_INSN_P (ILIST_INSN (l)))
  4365. ;
  4366. if (!l)
  4367. place_to_insert = NULL;
  4368. }
  4369. }
  4370. else
  4371. place_to_insert = NULL;
  4372. if (!place_to_insert)
  4373. {
  4374. /* Add it before BND_TO. The difference is in the
  4375. basic block, where INSN will be added. */
  4376. place_to_insert = get_nop_from_pool (BND_TO (bnd));
  4377. gcc_assert (BLOCK_FOR_INSN (place_to_insert)
  4378. == BLOCK_FOR_INSN (BND_TO (bnd)));
  4379. }
  4380. return place_to_insert;
  4381. }
  4382. /* Find original instructions for EXPR_SEQ and move it to BND boundary.
  4383. Return the expression to emit in C_EXPR. */
  4384. static bool
  4385. move_exprs_to_boundary (bnd_t bnd, expr_t expr_vliw,
  4386. av_set_t expr_seq, expr_t c_expr)
  4387. {
  4388. bool b, should_move;
  4389. unsigned book_uid;
  4390. bitmap_iterator bi;
  4391. int n_bookkeeping_copies_before_moveop;
  4392. /* Make a move. This call will remove the original operation,
  4393. insert all necessary bookkeeping instructions and update the
  4394. data sets. After that all we have to do is add the operation
  4395. at before BND_TO (BND). */
  4396. n_bookkeeping_copies_before_moveop = stat_bookkeeping_copies;
  4397. max_uid_before_move_op = get_max_uid ();
  4398. bitmap_clear (current_copies);
  4399. bitmap_clear (current_originators);
  4400. b = move_op (BND_TO (bnd), expr_seq, expr_vliw,
  4401. get_dest_from_orig_ops (expr_seq), c_expr, &should_move);
  4402. /* We should be able to find the expression we've chosen for
  4403. scheduling. */
  4404. gcc_assert (b);
  4405. if (stat_bookkeeping_copies > n_bookkeeping_copies_before_moveop)
  4406. stat_insns_needed_bookkeeping++;
  4407. EXECUTE_IF_SET_IN_BITMAP (current_copies, 0, book_uid, bi)
  4408. {
  4409. unsigned uid;
  4410. bitmap_iterator bi;
  4411. /* We allocate these bitmaps lazily. */
  4412. if (! INSN_ORIGINATORS_BY_UID (book_uid))
  4413. INSN_ORIGINATORS_BY_UID (book_uid) = BITMAP_ALLOC (NULL);
  4414. bitmap_copy (INSN_ORIGINATORS_BY_UID (book_uid),
  4415. current_originators);
  4416. /* Transitively add all originators' originators. */
  4417. EXECUTE_IF_SET_IN_BITMAP (current_originators, 0, uid, bi)
  4418. if (INSN_ORIGINATORS_BY_UID (uid))
  4419. bitmap_ior_into (INSN_ORIGINATORS_BY_UID (book_uid),
  4420. INSN_ORIGINATORS_BY_UID (uid));
  4421. }
  4422. return should_move;
  4423. }
  4424. /* Debug a DFA state as an array of bytes. */
  4425. static void
  4426. debug_state (state_t state)
  4427. {
  4428. unsigned char *p;
  4429. unsigned int i, size = dfa_state_size;
  4430. sel_print ("state (%u):", size);
  4431. for (i = 0, p = (unsigned char *) state; i < size; i++)
  4432. sel_print (" %d", p[i]);
  4433. sel_print ("\n");
  4434. }
  4435. /* Advance state on FENCE with INSN. Return true if INSN is
  4436. an ASM, and we should advance state once more. */
  4437. static bool
  4438. advance_state_on_fence (fence_t fence, insn_t insn)
  4439. {
  4440. bool asm_p;
  4441. if (recog_memoized (insn) >= 0)
  4442. {
  4443. int res;
  4444. state_t temp_state = alloca (dfa_state_size);
  4445. gcc_assert (!INSN_ASM_P (insn));
  4446. asm_p = false;
  4447. memcpy (temp_state, FENCE_STATE (fence), dfa_state_size);
  4448. res = state_transition (FENCE_STATE (fence), insn);
  4449. gcc_assert (res < 0);
  4450. if (memcmp (temp_state, FENCE_STATE (fence), dfa_state_size))
  4451. {
  4452. FENCE_ISSUED_INSNS (fence)++;
  4453. /* We should never issue more than issue_rate insns. */
  4454. if (FENCE_ISSUED_INSNS (fence) > issue_rate)
  4455. gcc_unreachable ();
  4456. }
  4457. }
  4458. else
  4459. {
  4460. /* This could be an ASM insn which we'd like to schedule
  4461. on the next cycle. */
  4462. asm_p = INSN_ASM_P (insn);
  4463. if (!FENCE_STARTS_CYCLE_P (fence) && asm_p)
  4464. advance_one_cycle (fence);
  4465. }
  4466. if (sched_verbose >= 2)
  4467. debug_state (FENCE_STATE (fence));
  4468. if (!DEBUG_INSN_P (insn))
  4469. FENCE_STARTS_CYCLE_P (fence) = 0;
  4470. FENCE_ISSUE_MORE (fence) = can_issue_more;
  4471. return asm_p;
  4472. }
  4473. /* Update FENCE on which INSN was scheduled and this INSN, too. NEED_STALL
  4474. is nonzero if we need to stall after issuing INSN. */
  4475. static void
  4476. update_fence_and_insn (fence_t fence, insn_t insn, int need_stall)
  4477. {
  4478. bool asm_p;
  4479. /* First, reflect that something is scheduled on this fence. */
  4480. asm_p = advance_state_on_fence (fence, insn);
  4481. FENCE_LAST_SCHEDULED_INSN (fence) = insn;
  4482. vec_safe_push (FENCE_EXECUTING_INSNS (fence), insn);
  4483. if (SCHED_GROUP_P (insn))
  4484. {
  4485. FENCE_SCHED_NEXT (fence) = INSN_SCHED_NEXT (insn);
  4486. SCHED_GROUP_P (insn) = 0;
  4487. }
  4488. else
  4489. FENCE_SCHED_NEXT (fence) = NULL;
  4490. if (INSN_UID (insn) < FENCE_READY_TICKS_SIZE (fence))
  4491. FENCE_READY_TICKS (fence) [INSN_UID (insn)] = 0;
  4492. /* Set instruction scheduling info. This will be used in bundling,
  4493. pipelining, tick computations etc. */
  4494. ++INSN_SCHED_TIMES (insn);
  4495. EXPR_TARGET_AVAILABLE (INSN_EXPR (insn)) = true;
  4496. EXPR_ORIG_SCHED_CYCLE (INSN_EXPR (insn)) = FENCE_CYCLE (fence);
  4497. INSN_AFTER_STALL_P (insn) = FENCE_AFTER_STALL_P (fence);
  4498. INSN_SCHED_CYCLE (insn) = FENCE_CYCLE (fence);
  4499. /* This does not account for adjust_cost hooks, just add the biggest
  4500. constant the hook may add to the latency. TODO: make this
  4501. a target dependent constant. */
  4502. INSN_READY_CYCLE (insn)
  4503. = INSN_SCHED_CYCLE (insn) + (INSN_CODE (insn) < 0
  4504. ? 1
  4505. : maximal_insn_latency (insn) + 1);
  4506. /* Change these fields last, as they're used above. */
  4507. FENCE_AFTER_STALL_P (fence) = 0;
  4508. if (asm_p || need_stall)
  4509. advance_one_cycle (fence);
  4510. /* Indicate that we've scheduled something on this fence. */
  4511. FENCE_SCHEDULED_P (fence) = true;
  4512. scheduled_something_on_previous_fence = true;
  4513. /* Print debug information when insn's fields are updated. */
  4514. if (sched_verbose >= 2)
  4515. {
  4516. sel_print ("Scheduling insn: ");
  4517. dump_insn_1 (insn, 1);
  4518. sel_print ("\n");
  4519. }
  4520. }
  4521. /* Update boundary BND (and, if needed, FENCE) with INSN, remove the
  4522. old boundary from BNDSP, add new boundaries to BNDS_TAIL_P and
  4523. return it. */
  4524. static blist_t *
  4525. update_boundaries (fence_t fence, bnd_t bnd, insn_t insn, blist_t *bndsp,
  4526. blist_t *bnds_tailp)
  4527. {
  4528. succ_iterator si;
  4529. insn_t succ;
  4530. advance_deps_context (BND_DC (bnd), insn);
  4531. FOR_EACH_SUCC_1 (succ, si, insn,
  4532. SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
  4533. {
  4534. ilist_t ptr = ilist_copy (BND_PTR (bnd));
  4535. ilist_add (&ptr, insn);
  4536. if (DEBUG_INSN_P (insn) && sel_bb_end_p (insn)
  4537. && is_ineligible_successor (succ, ptr))
  4538. {
  4539. ilist_clear (&ptr);
  4540. continue;
  4541. }
  4542. if (FENCE_INSN (fence) == insn && !sel_bb_end_p (insn))
  4543. {
  4544. if (sched_verbose >= 9)
  4545. sel_print ("Updating fence insn from %i to %i\n",
  4546. INSN_UID (insn), INSN_UID (succ));
  4547. FENCE_INSN (fence) = succ;
  4548. }
  4549. blist_add (bnds_tailp, succ, ptr, BND_DC (bnd));
  4550. bnds_tailp = &BLIST_NEXT (*bnds_tailp);
  4551. }
  4552. blist_remove (bndsp);
  4553. return bnds_tailp;
  4554. }
  4555. /* Schedule EXPR_VLIW on BND. Return the insn emitted. */
  4556. static insn_t
  4557. schedule_expr_on_boundary (bnd_t bnd, expr_t expr_vliw, int seqno)
  4558. {
  4559. av_set_t expr_seq;
  4560. expr_t c_expr = XALLOCA (expr_def);
  4561. insn_t place_to_insert;
  4562. insn_t insn;
  4563. bool should_move;
  4564. expr_seq = find_sequential_best_exprs (bnd, expr_vliw, true);
  4565. /* In case of scheduling a jump skipping some other instructions,
  4566. prepare CFG. After this, jump is at the boundary and can be
  4567. scheduled as usual insn by MOVE_OP. */
  4568. if (vinsn_cond_branch_p (EXPR_VINSN (expr_vliw)))
  4569. {
  4570. insn = EXPR_INSN_RTX (expr_vliw);
  4571. /* Speculative jumps are not handled. */
  4572. if (insn != BND_TO (bnd)
  4573. && !sel_insn_is_speculation_check (insn))
  4574. move_cond_jump (insn, bnd);
  4575. }
  4576. /* Find a place for C_EXPR to schedule. */
  4577. place_to_insert = prepare_place_to_insert (bnd);
  4578. should_move = move_exprs_to_boundary (bnd, expr_vliw, expr_seq, c_expr);
  4579. clear_expr (c_expr);
  4580. /* Add the instruction. The corner case to care about is when
  4581. the expr_seq set has more than one expr, and we chose the one that
  4582. is not equal to expr_vliw. Then expr_vliw may be insn in stream, and
  4583. we can't use it. Generate the new vinsn. */
  4584. if (INSN_IN_STREAM_P (EXPR_INSN_RTX (expr_vliw)))
  4585. {
  4586. vinsn_t vinsn_new;
  4587. vinsn_new = vinsn_copy (EXPR_VINSN (expr_vliw), false);
  4588. change_vinsn_in_expr (expr_vliw, vinsn_new);
  4589. should_move = false;
  4590. }
  4591. if (should_move)
  4592. insn = sel_move_insn (expr_vliw, seqno, place_to_insert);
  4593. else
  4594. insn = emit_insn_from_expr_after (expr_vliw, NULL, seqno,
  4595. place_to_insert);
  4596. /* Return the nops generated for preserving of data sets back
  4597. into pool. */
  4598. if (INSN_NOP_P (place_to_insert))
  4599. return_nop_to_pool (place_to_insert, !DEBUG_INSN_P (insn));
  4600. remove_temp_moveop_nops (!DEBUG_INSN_P (insn));
  4601. av_set_clear (&expr_seq);
  4602. /* Save the expression scheduled so to reset target availability if we'll
  4603. meet it later on the same fence. */
  4604. if (EXPR_WAS_RENAMED (expr_vliw))
  4605. vinsn_vec_add (&vec_target_unavailable_vinsns, INSN_EXPR (insn));
  4606. /* Check that the recent movement didn't destroyed loop
  4607. structure. */
  4608. gcc_assert (!pipelining_p
  4609. || current_loop_nest == NULL
  4610. || loop_latch_edge (current_loop_nest));
  4611. return insn;
  4612. }
  4613. /* Stall for N cycles on FENCE. */
  4614. static void
  4615. stall_for_cycles (fence_t fence, int n)
  4616. {
  4617. int could_more;
  4618. could_more = n > 1 || FENCE_ISSUED_INSNS (fence) < issue_rate;
  4619. while (n--)
  4620. advance_one_cycle (fence);
  4621. if (could_more)
  4622. FENCE_AFTER_STALL_P (fence) = 1;
  4623. }
  4624. /* Gather a parallel group of insns at FENCE and assign their seqno
  4625. to SEQNO. All scheduled insns are gathered in SCHEDULED_INSNS_TAILPP
  4626. list for later recalculation of seqnos. */
  4627. static void
  4628. fill_insns (fence_t fence, int seqno, ilist_t **scheduled_insns_tailpp)
  4629. {
  4630. blist_t bnds = NULL, *bnds_tailp;
  4631. av_set_t av_vliw = NULL;
  4632. insn_t insn = FENCE_INSN (fence);
  4633. if (sched_verbose >= 2)
  4634. sel_print ("Starting fill_insns for insn %d, cycle %d\n",
  4635. INSN_UID (insn), FENCE_CYCLE (fence));
  4636. blist_add (&bnds, insn, NULL, FENCE_DC (fence));
  4637. bnds_tailp = &BLIST_NEXT (bnds);
  4638. set_target_context (FENCE_TC (fence));
  4639. can_issue_more = FENCE_ISSUE_MORE (fence);
  4640. target_bb = INSN_BB (insn);
  4641. /* Do while we can add any operation to the current group. */
  4642. do
  4643. {
  4644. blist_t *bnds_tailp1, *bndsp;
  4645. expr_t expr_vliw;
  4646. int need_stall = false;
  4647. int was_stall = 0, scheduled_insns = 0;
  4648. int max_insns = pipelining_p ? issue_rate : 2 * issue_rate;
  4649. int max_stall = pipelining_p ? 1 : 3;
  4650. bool last_insn_was_debug = false;
  4651. bool was_debug_bb_end_p = false;
  4652. compute_av_set_on_boundaries (fence, bnds, &av_vliw);
  4653. remove_insns_that_need_bookkeeping (fence, &av_vliw);
  4654. remove_insns_for_debug (bnds, &av_vliw);
  4655. /* Return early if we have nothing to schedule. */
  4656. if (av_vliw == NULL)
  4657. break;
  4658. /* Choose the best expression and, if needed, destination register
  4659. for it. */
  4660. do
  4661. {
  4662. expr_vliw = find_best_expr (&av_vliw, bnds, fence, &need_stall);
  4663. if (! expr_vliw && need_stall)
  4664. {
  4665. /* All expressions required a stall. Do not recompute av sets
  4666. as we'll get the same answer (modulo the insns between
  4667. the fence and its boundary, which will not be available for
  4668. pipelining).
  4669. If we are going to stall for too long, break to recompute av
  4670. sets and bring more insns for pipelining. */
  4671. was_stall++;
  4672. if (need_stall <= 3)
  4673. stall_for_cycles (fence, need_stall);
  4674. else
  4675. {
  4676. stall_for_cycles (fence, 1);
  4677. break;
  4678. }
  4679. }
  4680. }
  4681. while (! expr_vliw && need_stall);
  4682. /* Now either we've selected expr_vliw or we have nothing to schedule. */
  4683. if (!expr_vliw)
  4684. {
  4685. av_set_clear (&av_vliw);
  4686. break;
  4687. }
  4688. bndsp = &bnds;
  4689. bnds_tailp1 = bnds_tailp;
  4690. do
  4691. /* This code will be executed only once until we'd have several
  4692. boundaries per fence. */
  4693. {
  4694. bnd_t bnd = BLIST_BND (*bndsp);
  4695. if (!av_set_is_in_p (BND_AV1 (bnd), EXPR_VINSN (expr_vliw)))
  4696. {
  4697. bndsp = &BLIST_NEXT (*bndsp);
  4698. continue;
  4699. }
  4700. insn = schedule_expr_on_boundary (bnd, expr_vliw, seqno);
  4701. last_insn_was_debug = DEBUG_INSN_P (insn);
  4702. if (last_insn_was_debug)
  4703. was_debug_bb_end_p = (insn == BND_TO (bnd) && sel_bb_end_p (insn));
  4704. update_fence_and_insn (fence, insn, need_stall);
  4705. bnds_tailp = update_boundaries (fence, bnd, insn, bndsp, bnds_tailp);
  4706. /* Add insn to the list of scheduled on this cycle instructions. */
  4707. ilist_add (*scheduled_insns_tailpp, insn);
  4708. *scheduled_insns_tailpp = &ILIST_NEXT (**scheduled_insns_tailpp);
  4709. }
  4710. while (*bndsp != *bnds_tailp1);
  4711. av_set_clear (&av_vliw);
  4712. if (!last_insn_was_debug)
  4713. scheduled_insns++;
  4714. /* We currently support information about candidate blocks only for
  4715. one 'target_bb' block. Hence we can't schedule after jump insn,
  4716. as this will bring two boundaries and, hence, necessity to handle
  4717. information for two or more blocks concurrently. */
  4718. if ((last_insn_was_debug ? was_debug_bb_end_p : sel_bb_end_p (insn))
  4719. || (was_stall
  4720. && (was_stall >= max_stall
  4721. || scheduled_insns >= max_insns)))
  4722. break;
  4723. }
  4724. while (bnds);
  4725. gcc_assert (!FENCE_BNDS (fence));
  4726. /* Update boundaries of the FENCE. */
  4727. while (bnds)
  4728. {
  4729. ilist_t ptr = BND_PTR (BLIST_BND (bnds));
  4730. if (ptr)
  4731. {
  4732. insn = ILIST_INSN (ptr);
  4733. if (!ilist_is_in_p (FENCE_BNDS (fence), insn))
  4734. ilist_add (&FENCE_BNDS (fence), insn);
  4735. }
  4736. blist_remove (&bnds);
  4737. }
  4738. /* Update target context on the fence. */
  4739. reset_target_context (FENCE_TC (fence), false);
  4740. }
  4741. /* All exprs in ORIG_OPS must have the same destination register or memory.
  4742. Return that destination. */
  4743. static rtx
  4744. get_dest_from_orig_ops (av_set_t orig_ops)
  4745. {
  4746. rtx dest = NULL_RTX;
  4747. av_set_iterator av_it;
  4748. expr_t expr;
  4749. bool first_p = true;
  4750. FOR_EACH_EXPR (expr, av_it, orig_ops)
  4751. {
  4752. rtx x = EXPR_LHS (expr);
  4753. if (first_p)
  4754. {
  4755. first_p = false;
  4756. dest = x;
  4757. }
  4758. else
  4759. gcc_assert (dest == x
  4760. || (dest != NULL_RTX && x != NULL_RTX
  4761. && rtx_equal_p (dest, x)));
  4762. }
  4763. return dest;
  4764. }
  4765. /* Update data sets for the bookkeeping block and record those expressions
  4766. which become no longer available after inserting this bookkeeping. */
  4767. static void
  4768. update_and_record_unavailable_insns (basic_block book_block)
  4769. {
  4770. av_set_iterator i;
  4771. av_set_t old_av_set = NULL;
  4772. expr_t cur_expr;
  4773. rtx_insn *bb_end = sel_bb_end (book_block);
  4774. /* First, get correct liveness in the bookkeeping block. The problem is
  4775. the range between the bookeeping insn and the end of block. */
  4776. update_liveness_on_insn (bb_end);
  4777. if (control_flow_insn_p (bb_end))
  4778. update_liveness_on_insn (PREV_INSN (bb_end));
  4779. /* If there's valid av_set on BOOK_BLOCK, then there might exist another
  4780. fence above, where we may choose to schedule an insn which is
  4781. actually blocked from moving up with the bookkeeping we create here. */
  4782. if (AV_SET_VALID_P (sel_bb_head (book_block)))
  4783. {
  4784. old_av_set = av_set_copy (BB_AV_SET (book_block));
  4785. update_data_sets (sel_bb_head (book_block));
  4786. /* Traverse all the expressions in the old av_set and check whether
  4787. CUR_EXPR is in new AV_SET. */
  4788. FOR_EACH_EXPR (cur_expr, i, old_av_set)
  4789. {
  4790. expr_t new_expr = av_set_lookup (BB_AV_SET (book_block),
  4791. EXPR_VINSN (cur_expr));
  4792. if (! new_expr
  4793. /* In this case, we can just turn off the E_T_A bit, but we can't
  4794. represent this information with the current vector. */
  4795. || EXPR_TARGET_AVAILABLE (new_expr)
  4796. != EXPR_TARGET_AVAILABLE (cur_expr))
  4797. /* Unfortunately, the below code could be also fired up on
  4798. separable insns, e.g. when moving insns through the new
  4799. speculation check as in PR 53701. */
  4800. vinsn_vec_add (&vec_bookkeeping_blocked_vinsns, cur_expr);
  4801. }
  4802. av_set_clear (&old_av_set);
  4803. }
  4804. }
  4805. /* The main effect of this function is that sparams->c_expr is merged
  4806. with (or copied to) lparams->c_expr_merged. If there's only one successor,
  4807. we avoid merging anything by copying sparams->c_expr to lparams->c_expr_merged.
  4808. lparams->c_expr_merged is copied back to sparams->c_expr after all
  4809. successors has been traversed. lparams->c_expr_local is an expr allocated
  4810. on stack in the caller function, and is used if there is more than one
  4811. successor.
  4812. SUCC is one of the SUCCS_NORMAL successors of INSN,
  4813. MOVEOP_DRV_CALL_RES is the result of call code_motion_path_driver on succ,
  4814. LPARAMS and STATIC_PARAMS contain the parameters described above. */
  4815. static void
  4816. move_op_merge_succs (insn_t insn ATTRIBUTE_UNUSED,
  4817. insn_t succ ATTRIBUTE_UNUSED,
  4818. int moveop_drv_call_res,
  4819. cmpd_local_params_p lparams, void *static_params)
  4820. {
  4821. moveop_static_params_p sparams = (moveop_static_params_p) static_params;
  4822. /* Nothing to do, if original expr wasn't found below. */
  4823. if (moveop_drv_call_res != 1)
  4824. return;
  4825. /* If this is a first successor. */
  4826. if (!lparams->c_expr_merged)
  4827. {
  4828. lparams->c_expr_merged = sparams->c_expr;
  4829. sparams->c_expr = lparams->c_expr_local;
  4830. }
  4831. else
  4832. {
  4833. /* We must merge all found expressions to get reasonable
  4834. EXPR_SPEC_DONE_DS for the resulting insn. If we don't
  4835. do so then we can first find the expr with epsilon
  4836. speculation success probability and only then with the
  4837. good probability. As a result the insn will get epsilon
  4838. probability and will never be scheduled because of
  4839. weakness_cutoff in find_best_expr.
  4840. We call merge_expr_data here instead of merge_expr
  4841. because due to speculation C_EXPR and X may have the
  4842. same insns with different speculation types. And as of
  4843. now such insns are considered non-equal.
  4844. However, EXPR_SCHED_TIMES is different -- we must get
  4845. SCHED_TIMES from a real insn, not a bookkeeping copy.
  4846. We force this here. Instead, we may consider merging
  4847. SCHED_TIMES to the maximum instead of minimum in the
  4848. below function. */
  4849. int old_times = EXPR_SCHED_TIMES (lparams->c_expr_merged);
  4850. merge_expr_data (lparams->c_expr_merged, sparams->c_expr, NULL);
  4851. if (EXPR_SCHED_TIMES (sparams->c_expr) == 0)
  4852. EXPR_SCHED_TIMES (lparams->c_expr_merged) = old_times;
  4853. clear_expr (sparams->c_expr);
  4854. }
  4855. }
  4856. /* Add used regs for the successor SUCC into SPARAMS->USED_REGS.
  4857. SUCC is one of the SUCCS_NORMAL successors of INSN,
  4858. MOVEOP_DRV_CALL_RES is the result of call code_motion_path_driver on succ or 0,
  4859. if SUCC is one of SUCCS_BACK or SUCCS_OUT.
  4860. STATIC_PARAMS contain USED_REGS set. */
  4861. static void
  4862. fur_merge_succs (insn_t insn ATTRIBUTE_UNUSED, insn_t succ,
  4863. int moveop_drv_call_res,
  4864. cmpd_local_params_p lparams ATTRIBUTE_UNUSED,
  4865. void *static_params)
  4866. {
  4867. regset succ_live;
  4868. fur_static_params_p sparams = (fur_static_params_p) static_params;
  4869. /* Here we compute live regsets only for branches that do not lie
  4870. on the code motion paths. These branches correspond to value
  4871. MOVEOP_DRV_CALL_RES==0 and include SUCCS_BACK and SUCCS_OUT, though
  4872. for such branches code_motion_path_driver is not called. */
  4873. if (moveop_drv_call_res != 0)
  4874. return;
  4875. /* Mark all registers that do not meet the following condition:
  4876. (3) not live on the other path of any conditional branch
  4877. that is passed by the operation, in case original
  4878. operations are not present on both paths of the
  4879. conditional branch. */
  4880. succ_live = compute_live (succ);
  4881. IOR_REG_SET (sparams->used_regs, succ_live);
  4882. }
  4883. /* This function is called after the last successor. Copies LP->C_EXPR_MERGED
  4884. into SP->CEXPR. */
  4885. static void
  4886. move_op_after_merge_succs (cmpd_local_params_p lp, void *sparams)
  4887. {
  4888. moveop_static_params_p sp = (moveop_static_params_p) sparams;
  4889. sp->c_expr = lp->c_expr_merged;
  4890. }
  4891. /* Track bookkeeping copies created, insns scheduled, and blocks for
  4892. rescheduling when INSN is found by move_op. */
  4893. static void
  4894. track_scheduled_insns_and_blocks (rtx insn)
  4895. {
  4896. /* Even if this insn can be a copy that will be removed during current move_op,
  4897. we still need to count it as an originator. */
  4898. bitmap_set_bit (current_originators, INSN_UID (insn));
  4899. if (!bitmap_clear_bit (current_copies, INSN_UID (insn)))
  4900. {
  4901. /* Note that original block needs to be rescheduled, as we pulled an
  4902. instruction out of it. */
  4903. if (INSN_SCHED_TIMES (insn) > 0)
  4904. bitmap_set_bit (blocks_to_reschedule, BLOCK_FOR_INSN (insn)->index);
  4905. else if (INSN_UID (insn) < first_emitted_uid && !DEBUG_INSN_P (insn))
  4906. num_insns_scheduled++;
  4907. }
  4908. /* For instructions we must immediately remove insn from the
  4909. stream, so subsequent update_data_sets () won't include this
  4910. insn into av_set.
  4911. For expr we must make insn look like "INSN_REG (insn) := c_expr". */
  4912. if (INSN_UID (insn) > max_uid_before_move_op)
  4913. stat_bookkeeping_copies--;
  4914. }
  4915. /* Emit a register-register copy for INSN if needed. Return true if
  4916. emitted one. PARAMS is the move_op static parameters. */
  4917. static bool
  4918. maybe_emit_renaming_copy (rtx_insn *insn,
  4919. moveop_static_params_p params)
  4920. {
  4921. bool insn_emitted = false;
  4922. rtx cur_reg;
  4923. /* Bail out early when expression can not be renamed at all. */
  4924. if (!EXPR_SEPARABLE_P (params->c_expr))
  4925. return false;
  4926. cur_reg = expr_dest_reg (params->c_expr);
  4927. gcc_assert (cur_reg && params->dest && REG_P (params->dest));
  4928. /* If original operation has expr and the register chosen for
  4929. that expr is not original operation's dest reg, substitute
  4930. operation's right hand side with the register chosen. */
  4931. if (REGNO (params->dest) != REGNO (cur_reg))
  4932. {
  4933. insn_t reg_move_insn, reg_move_insn_rtx;
  4934. reg_move_insn_rtx = create_insn_rtx_with_rhs (INSN_VINSN (insn),
  4935. params->dest);
  4936. reg_move_insn = sel_gen_insn_from_rtx_after (reg_move_insn_rtx,
  4937. INSN_EXPR (insn),
  4938. INSN_SEQNO (insn),
  4939. insn);
  4940. EXPR_SPEC_DONE_DS (INSN_EXPR (reg_move_insn)) = 0;
  4941. replace_dest_with_reg_in_expr (params->c_expr, params->dest);
  4942. insn_emitted = true;
  4943. params->was_renamed = true;
  4944. }
  4945. return insn_emitted;
  4946. }
  4947. /* Emit a speculative check for INSN speculated as EXPR if needed.
  4948. Return true if we've emitted one. PARAMS is the move_op static
  4949. parameters. */
  4950. static bool
  4951. maybe_emit_speculative_check (rtx_insn *insn, expr_t expr,
  4952. moveop_static_params_p params)
  4953. {
  4954. bool insn_emitted = false;
  4955. insn_t x;
  4956. ds_t check_ds;
  4957. check_ds = get_spec_check_type_for_insn (insn, expr);
  4958. if (check_ds != 0)
  4959. {
  4960. /* A speculation check should be inserted. */
  4961. x = create_speculation_check (params->c_expr, check_ds, insn);
  4962. insn_emitted = true;
  4963. }
  4964. else
  4965. {
  4966. EXPR_SPEC_DONE_DS (INSN_EXPR (insn)) = 0;
  4967. x = insn;
  4968. }
  4969. gcc_assert (EXPR_SPEC_DONE_DS (INSN_EXPR (x)) == 0
  4970. && EXPR_SPEC_TO_CHECK_DS (INSN_EXPR (x)) == 0);
  4971. return insn_emitted;
  4972. }
  4973. /* Handle transformations that leave an insn in place of original
  4974. insn such as renaming/speculation. Return true if one of such
  4975. transformations actually happened, and we have emitted this insn. */
  4976. static bool
  4977. handle_emitting_transformations (rtx_insn *insn, expr_t expr,
  4978. moveop_static_params_p params)
  4979. {
  4980. bool insn_emitted = false;
  4981. insn_emitted = maybe_emit_renaming_copy (insn, params);
  4982. insn_emitted |= maybe_emit_speculative_check (insn, expr, params);
  4983. return insn_emitted;
  4984. }
  4985. /* If INSN is the only insn in the basic block (not counting JUMP,
  4986. which may be a jump to next insn, and DEBUG_INSNs), we want to
  4987. leave a NOP there till the return to fill_insns. */
  4988. static bool
  4989. need_nop_to_preserve_insn_bb (rtx_insn *insn)
  4990. {
  4991. insn_t bb_head, bb_end, bb_next, in_next;
  4992. basic_block bb = BLOCK_FOR_INSN (insn);
  4993. bb_head = sel_bb_head (bb);
  4994. bb_end = sel_bb_end (bb);
  4995. if (bb_head == bb_end)
  4996. return true;
  4997. while (bb_head != bb_end && DEBUG_INSN_P (bb_head))
  4998. bb_head = NEXT_INSN (bb_head);
  4999. if (bb_head == bb_end)
  5000. return true;
  5001. while (bb_head != bb_end && DEBUG_INSN_P (bb_end))
  5002. bb_end = PREV_INSN (bb_end);
  5003. if (bb_head == bb_end)
  5004. return true;
  5005. bb_next = NEXT_INSN (bb_head);
  5006. while (bb_next != bb_end && DEBUG_INSN_P (bb_next))
  5007. bb_next = NEXT_INSN (bb_next);
  5008. if (bb_next == bb_end && JUMP_P (bb_end))
  5009. return true;
  5010. in_next = NEXT_INSN (insn);
  5011. while (DEBUG_INSN_P (in_next))
  5012. in_next = NEXT_INSN (in_next);
  5013. if (IN_CURRENT_FENCE_P (in_next))
  5014. return true;
  5015. return false;
  5016. }
  5017. /* Remove INSN from stream. When ONLY_DISCONNECT is true, its data
  5018. is not removed but reused when INSN is re-emitted. */
  5019. static void
  5020. remove_insn_from_stream (rtx_insn *insn, bool only_disconnect)
  5021. {
  5022. /* If there's only one insn in the BB, make sure that a nop is
  5023. inserted into it, so the basic block won't disappear when we'll
  5024. delete INSN below with sel_remove_insn. It should also survive
  5025. till the return to fill_insns. */
  5026. if (need_nop_to_preserve_insn_bb (insn))
  5027. {
  5028. insn_t nop = get_nop_from_pool (insn);
  5029. gcc_assert (INSN_NOP_P (nop));
  5030. vec_temp_moveop_nops.safe_push (nop);
  5031. }
  5032. sel_remove_insn (insn, only_disconnect, false);
  5033. }
  5034. /* This function is called when original expr is found.
  5035. INSN - current insn traversed, EXPR - the corresponding expr found.
  5036. LPARAMS is the local parameters of code modion driver, STATIC_PARAMS
  5037. is static parameters of move_op. */
  5038. static void
  5039. move_op_orig_expr_found (insn_t insn, expr_t expr,
  5040. cmpd_local_params_p lparams ATTRIBUTE_UNUSED,
  5041. void *static_params)
  5042. {
  5043. bool only_disconnect;
  5044. moveop_static_params_p params = (moveop_static_params_p) static_params;
  5045. copy_expr_onside (params->c_expr, INSN_EXPR (insn));
  5046. track_scheduled_insns_and_blocks (insn);
  5047. handle_emitting_transformations (insn, expr, params);
  5048. only_disconnect = params->uid == INSN_UID (insn);
  5049. /* Mark that we've disconnected an insn. */
  5050. if (only_disconnect)
  5051. params->uid = -1;
  5052. remove_insn_from_stream (insn, only_disconnect);
  5053. }
  5054. /* The function is called when original expr is found.
  5055. INSN - current insn traversed, EXPR - the corresponding expr found,
  5056. crosses_call and original_insns in STATIC_PARAMS are updated. */
  5057. static void
  5058. fur_orig_expr_found (insn_t insn, expr_t expr ATTRIBUTE_UNUSED,
  5059. cmpd_local_params_p lparams ATTRIBUTE_UNUSED,
  5060. void *static_params)
  5061. {
  5062. fur_static_params_p params = (fur_static_params_p) static_params;
  5063. regset tmp;
  5064. if (CALL_P (insn))
  5065. params->crosses_call = true;
  5066. def_list_add (params->original_insns, insn, params->crosses_call);
  5067. /* Mark the registers that do not meet the following condition:
  5068. (2) not among the live registers of the point
  5069. immediately following the first original operation on
  5070. a given downward path, except for the original target
  5071. register of the operation. */
  5072. tmp = get_clear_regset_from_pool ();
  5073. compute_live_below_insn (insn, tmp);
  5074. AND_COMPL_REG_SET (tmp, INSN_REG_SETS (insn));
  5075. AND_COMPL_REG_SET (tmp, INSN_REG_CLOBBERS (insn));
  5076. IOR_REG_SET (params->used_regs, tmp);
  5077. return_regset_to_pool (tmp);
  5078. /* (*1) We need to add to USED_REGS registers that are read by
  5079. INSN's lhs. This may lead to choosing wrong src register.
  5080. E.g. (scheduling const expr enabled):
  5081. 429: ax=0x0 <- Can't use AX for this expr (0x0)
  5082. 433: dx=[bp-0x18]
  5083. 427: [ax+dx+0x1]=ax
  5084. REG_DEAD: ax
  5085. 168: di=dx
  5086. REG_DEAD: dx
  5087. */
  5088. /* FIXME: see comment above and enable MEM_P
  5089. in vinsn_separable_p. */
  5090. gcc_assert (!VINSN_SEPARABLE_P (INSN_VINSN (insn))
  5091. || !MEM_P (INSN_LHS (insn)));
  5092. }
  5093. /* This function is called on the ascending pass, before returning from
  5094. current basic block. */
  5095. static void
  5096. move_op_at_first_insn (insn_t insn, cmpd_local_params_p lparams,
  5097. void *static_params)
  5098. {
  5099. moveop_static_params_p sparams = (moveop_static_params_p) static_params;
  5100. basic_block book_block = NULL;
  5101. /* When we have removed the boundary insn for scheduling, which also
  5102. happened to be the end insn in its bb, we don't need to update sets. */
  5103. if (!lparams->removed_last_insn
  5104. && lparams->e1
  5105. && sel_bb_head_p (insn))
  5106. {
  5107. /* We should generate bookkeeping code only if we are not at the
  5108. top level of the move_op. */
  5109. if (sel_num_cfg_preds_gt_1 (insn))
  5110. book_block = generate_bookkeeping_insn (sparams->c_expr,
  5111. lparams->e1, lparams->e2);
  5112. /* Update data sets for the current insn. */
  5113. update_data_sets (insn);
  5114. }
  5115. /* If bookkeeping code was inserted, we need to update av sets of basic
  5116. block that received bookkeeping. After generation of bookkeeping insn,
  5117. bookkeeping block does not contain valid av set because we are not following
  5118. the original algorithm in every detail with regards to e.g. renaming
  5119. simple reg-reg copies. Consider example:
  5120. bookkeeping block scheduling fence
  5121. \ /
  5122. \ join /
  5123. ----------
  5124. | |
  5125. ----------
  5126. / \
  5127. / \
  5128. r1 := r2 r1 := r3
  5129. We try to schedule insn "r1 := r3" on the current
  5130. scheduling fence. Also, note that av set of bookkeeping block
  5131. contain both insns "r1 := r2" and "r1 := r3". When the insn has
  5132. been scheduled, the CFG is as follows:
  5133. r1 := r3 r1 := r3
  5134. bookkeeping block scheduling fence
  5135. \ /
  5136. \ join /
  5137. ----------
  5138. | |
  5139. ----------
  5140. / \
  5141. / \
  5142. r1 := r2
  5143. Here, insn "r1 := r3" was scheduled at the current scheduling point
  5144. and bookkeeping code was generated at the bookeeping block. This
  5145. way insn "r1 := r2" is no longer available as a whole instruction
  5146. (but only as expr) ahead of insn "r1 := r3" in bookkeeping block.
  5147. This situation is handled by calling update_data_sets.
  5148. Since update_data_sets is called only on the bookkeeping block, and
  5149. it also may have predecessors with av_sets, containing instructions that
  5150. are no longer available, we save all such expressions that become
  5151. unavailable during data sets update on the bookkeeping block in
  5152. VEC_BOOKKEEPING_BLOCKED_VINSNS. Later we avoid selecting such
  5153. expressions for scheduling. This allows us to avoid recomputation of
  5154. av_sets outside the code motion path. */
  5155. if (book_block)
  5156. update_and_record_unavailable_insns (book_block);
  5157. /* If INSN was previously marked for deletion, it's time to do it. */
  5158. if (lparams->removed_last_insn)
  5159. insn = PREV_INSN (insn);
  5160. /* Do not tidy control flow at the topmost moveop, as we can erroneously
  5161. kill a block with a single nop in which the insn should be emitted. */
  5162. if (lparams->e1)
  5163. tidy_control_flow (BLOCK_FOR_INSN (insn), true);
  5164. }
  5165. /* This function is called on the ascending pass, before returning from the
  5166. current basic block. */
  5167. static void
  5168. fur_at_first_insn (insn_t insn,
  5169. cmpd_local_params_p lparams ATTRIBUTE_UNUSED,
  5170. void *static_params ATTRIBUTE_UNUSED)
  5171. {
  5172. gcc_assert (!sel_bb_head_p (insn) || AV_SET_VALID_P (insn)
  5173. || AV_LEVEL (insn) == -1);
  5174. }
  5175. /* Called on the backward stage of recursion to call moveup_expr for insn
  5176. and sparams->c_expr. */
  5177. static void
  5178. move_op_ascend (insn_t insn, void *static_params)
  5179. {
  5180. enum MOVEUP_EXPR_CODE res;
  5181. moveop_static_params_p sparams = (moveop_static_params_p) static_params;
  5182. if (! INSN_NOP_P (insn))
  5183. {
  5184. res = moveup_expr_cached (sparams->c_expr, insn, false);
  5185. gcc_assert (res != MOVEUP_EXPR_NULL);
  5186. }
  5187. /* Update liveness for this insn as it was invalidated. */
  5188. update_liveness_on_insn (insn);
  5189. }
  5190. /* This function is called on enter to the basic block.
  5191. Returns TRUE if this block already have been visited and
  5192. code_motion_path_driver should return 1, FALSE otherwise. */
  5193. static int
  5194. fur_on_enter (insn_t insn ATTRIBUTE_UNUSED, cmpd_local_params_p local_params,
  5195. void *static_params, bool visited_p)
  5196. {
  5197. fur_static_params_p sparams = (fur_static_params_p) static_params;
  5198. if (visited_p)
  5199. {
  5200. /* If we have found something below this block, there should be at
  5201. least one insn in ORIGINAL_INSNS. */
  5202. gcc_assert (*sparams->original_insns);
  5203. /* Adjust CROSSES_CALL, since we may have come to this block along
  5204. different path. */
  5205. DEF_LIST_DEF (*sparams->original_insns)->crosses_call
  5206. |= sparams->crosses_call;
  5207. }
  5208. else
  5209. local_params->old_original_insns = *sparams->original_insns;
  5210. return 1;
  5211. }
  5212. /* Same as above but for move_op. */
  5213. static int
  5214. move_op_on_enter (insn_t insn ATTRIBUTE_UNUSED,
  5215. cmpd_local_params_p local_params ATTRIBUTE_UNUSED,
  5216. void *static_params ATTRIBUTE_UNUSED, bool visited_p)
  5217. {
  5218. if (visited_p)
  5219. return -1;
  5220. return 1;
  5221. }
  5222. /* This function is called while descending current basic block if current
  5223. insn is not the original EXPR we're searching for.
  5224. Return value: FALSE, if code_motion_path_driver should perform a local
  5225. cleanup and return 0 itself;
  5226. TRUE, if code_motion_path_driver should continue. */
  5227. static bool
  5228. move_op_orig_expr_not_found (insn_t insn, av_set_t orig_ops ATTRIBUTE_UNUSED,
  5229. void *static_params)
  5230. {
  5231. moveop_static_params_p sparams = (moveop_static_params_p) static_params;
  5232. #ifdef ENABLE_CHECKING
  5233. sparams->failed_insn = insn;
  5234. #endif
  5235. /* If we're scheduling separate expr, in order to generate correct code
  5236. we need to stop the search at bookkeeping code generated with the
  5237. same destination register or memory. */
  5238. if (lhs_of_insn_equals_to_dest_p (insn, sparams->dest))
  5239. return false;
  5240. return true;
  5241. }
  5242. /* This function is called while descending current basic block if current
  5243. insn is not the original EXPR we're searching for.
  5244. Return value: TRUE (code_motion_path_driver should continue). */
  5245. static bool
  5246. fur_orig_expr_not_found (insn_t insn, av_set_t orig_ops, void *static_params)
  5247. {
  5248. bool mutexed;
  5249. expr_t r;
  5250. av_set_iterator avi;
  5251. fur_static_params_p sparams = (fur_static_params_p) static_params;
  5252. if (CALL_P (insn))
  5253. sparams->crosses_call = true;
  5254. else if (DEBUG_INSN_P (insn))
  5255. return true;
  5256. /* If current insn we are looking at cannot be executed together
  5257. with original insn, then we can skip it safely.
  5258. Example: ORIG_OPS = { (p6) r14 = sign_extend (r15); }
  5259. INSN = (!p6) r14 = r14 + 1;
  5260. Here we can schedule ORIG_OP with lhs = r14, though only
  5261. looking at the set of used and set registers of INSN we must
  5262. forbid it. So, add set/used in INSN registers to the
  5263. untouchable set only if there is an insn in ORIG_OPS that can
  5264. affect INSN. */
  5265. mutexed = true;
  5266. FOR_EACH_EXPR (r, avi, orig_ops)
  5267. if (!sched_insns_conditions_mutex_p (insn, EXPR_INSN_RTX (r)))
  5268. {
  5269. mutexed = false;
  5270. break;
  5271. }
  5272. /* Mark all registers that do not meet the following condition:
  5273. (1) Not set or read on any path from xi to an instance of the
  5274. original operation. */
  5275. if (!mutexed)
  5276. {
  5277. IOR_REG_SET (sparams->used_regs, INSN_REG_SETS (insn));
  5278. IOR_REG_SET (sparams->used_regs, INSN_REG_USES (insn));
  5279. IOR_REG_SET (sparams->used_regs, INSN_REG_CLOBBERS (insn));
  5280. }
  5281. return true;
  5282. }
  5283. /* Hooks and data to perform move_op operations with code_motion_path_driver. */
  5284. struct code_motion_path_driver_info_def move_op_hooks = {
  5285. move_op_on_enter,
  5286. move_op_orig_expr_found,
  5287. move_op_orig_expr_not_found,
  5288. move_op_merge_succs,
  5289. move_op_after_merge_succs,
  5290. move_op_ascend,
  5291. move_op_at_first_insn,
  5292. SUCCS_NORMAL,
  5293. "move_op"
  5294. };
  5295. /* Hooks and data to perform find_used_regs operations
  5296. with code_motion_path_driver. */
  5297. struct code_motion_path_driver_info_def fur_hooks = {
  5298. fur_on_enter,
  5299. fur_orig_expr_found,
  5300. fur_orig_expr_not_found,
  5301. fur_merge_succs,
  5302. NULL, /* fur_after_merge_succs */
  5303. NULL, /* fur_ascend */
  5304. fur_at_first_insn,
  5305. SUCCS_ALL,
  5306. "find_used_regs"
  5307. };
  5308. /* Traverse all successors of INSN. For each successor that is SUCCS_NORMAL
  5309. code_motion_path_driver is called recursively. Original operation
  5310. was found at least on one path that is starting with one of INSN's
  5311. successors (this fact is asserted). ORIG_OPS is expressions we're looking
  5312. for, PATH is the path we've traversed, STATIC_PARAMS is the parameters
  5313. of either move_op or find_used_regs depending on the caller.
  5314. Return 0 if we haven't found expression, 1 if we found it, -1 if we don't
  5315. know for sure at this point. */
  5316. static int
  5317. code_motion_process_successors (insn_t insn, av_set_t orig_ops,
  5318. ilist_t path, void *static_params)
  5319. {
  5320. int res = 0;
  5321. succ_iterator succ_i;
  5322. insn_t succ;
  5323. basic_block bb;
  5324. int old_index;
  5325. unsigned old_succs;
  5326. struct cmpd_local_params lparams;
  5327. expr_def _x;
  5328. lparams.c_expr_local = &_x;
  5329. lparams.c_expr_merged = NULL;
  5330. /* We need to process only NORMAL succs for move_op, and collect live
  5331. registers from ALL branches (including those leading out of the
  5332. region) for find_used_regs.
  5333. In move_op, there can be a case when insn's bb number has changed
  5334. due to created bookkeeping. This happens very rare, as we need to
  5335. move expression from the beginning to the end of the same block.
  5336. Rescan successors in this case. */
  5337. rescan:
  5338. bb = BLOCK_FOR_INSN (insn);
  5339. old_index = bb->index;
  5340. old_succs = EDGE_COUNT (bb->succs);
  5341. FOR_EACH_SUCC_1 (succ, succ_i, insn, code_motion_path_driver_info->succ_flags)
  5342. {
  5343. int b;
  5344. lparams.e1 = succ_i.e1;
  5345. lparams.e2 = succ_i.e2;
  5346. /* Go deep into recursion only for NORMAL edges (non-backedges within the
  5347. current region). */
  5348. if (succ_i.current_flags == SUCCS_NORMAL)
  5349. b = code_motion_path_driver (succ, orig_ops, path, &lparams,
  5350. static_params);
  5351. else
  5352. b = 0;
  5353. /* Merge c_expres found or unify live register sets from different
  5354. successors. */
  5355. code_motion_path_driver_info->merge_succs (insn, succ, b, &lparams,
  5356. static_params);
  5357. if (b == 1)
  5358. res = b;
  5359. else if (b == -1 && res != 1)
  5360. res = b;
  5361. /* We have simplified the control flow below this point. In this case,
  5362. the iterator becomes invalid. We need to try again.
  5363. If we have removed the insn itself, it could be only an
  5364. unconditional jump. Thus, do not rescan but break immediately --
  5365. we have already visited the only successor block. */
  5366. if (!BLOCK_FOR_INSN (insn))
  5367. {
  5368. if (sched_verbose >= 6)
  5369. sel_print ("Not doing rescan: already visited the only successor"
  5370. " of block %d\n", old_index);
  5371. break;
  5372. }
  5373. if (BLOCK_FOR_INSN (insn)->index != old_index
  5374. || EDGE_COUNT (bb->succs) != old_succs)
  5375. {
  5376. if (sched_verbose >= 6)
  5377. sel_print ("Rescan: CFG was simplified below insn %d, block %d\n",
  5378. INSN_UID (insn), BLOCK_FOR_INSN (insn)->index);
  5379. insn = sel_bb_end (BLOCK_FOR_INSN (insn));
  5380. goto rescan;
  5381. }
  5382. }
  5383. #ifdef ENABLE_CHECKING
  5384. /* Here, RES==1 if original expr was found at least for one of the
  5385. successors. After the loop, RES may happen to have zero value
  5386. only if at some point the expr searched is present in av_set, but is
  5387. not found below. In most cases, this situation is an error.
  5388. The exception is when the original operation is blocked by
  5389. bookkeeping generated for another fence or for another path in current
  5390. move_op. */
  5391. gcc_assert (res == 1
  5392. || (res == 0
  5393. && av_set_could_be_blocked_by_bookkeeping_p (orig_ops,
  5394. static_params))
  5395. || res == -1);
  5396. #endif
  5397. /* Merge data, clean up, etc. */
  5398. if (res != -1 && code_motion_path_driver_info->after_merge_succs)
  5399. code_motion_path_driver_info->after_merge_succs (&lparams, static_params);
  5400. return res;
  5401. }
  5402. /* Perform a cleanup when the driver is about to terminate. ORIG_OPS_P
  5403. is the pointer to the av set with expressions we were looking for,
  5404. PATH_P is the pointer to the traversed path. */
  5405. static inline void
  5406. code_motion_path_driver_cleanup (av_set_t *orig_ops_p, ilist_t *path_p)
  5407. {
  5408. ilist_remove (path_p);
  5409. av_set_clear (orig_ops_p);
  5410. }
  5411. /* The driver function that implements move_op or find_used_regs
  5412. functionality dependent whether code_motion_path_driver_INFO is set to
  5413. &MOVE_OP_HOOKS or &FUR_HOOKS. This function implements the common parts
  5414. of code (CFG traversal etc) that are shared among both functions. INSN
  5415. is the insn we're starting the search from, ORIG_OPS are the expressions
  5416. we're searching for, PATH is traversed path, LOCAL_PARAMS_IN are local
  5417. parameters of the driver, and STATIC_PARAMS are static parameters of
  5418. the caller.
  5419. Returns whether original instructions were found. Note that top-level
  5420. code_motion_path_driver always returns true. */
  5421. static int
  5422. code_motion_path_driver (insn_t insn, av_set_t orig_ops, ilist_t path,
  5423. cmpd_local_params_p local_params_in,
  5424. void *static_params)
  5425. {
  5426. expr_t expr = NULL;
  5427. basic_block bb = BLOCK_FOR_INSN (insn);
  5428. insn_t first_insn, bb_tail, before_first;
  5429. bool removed_last_insn = false;
  5430. if (sched_verbose >= 6)
  5431. {
  5432. sel_print ("%s (", code_motion_path_driver_info->routine_name);
  5433. dump_insn (insn);
  5434. sel_print (",");
  5435. dump_av_set (orig_ops);
  5436. sel_print (")\n");
  5437. }
  5438. gcc_assert (orig_ops);
  5439. /* If no original operations exist below this insn, return immediately. */
  5440. if (is_ineligible_successor (insn, path))
  5441. {
  5442. if (sched_verbose >= 6)
  5443. sel_print ("Insn %d is ineligible successor\n", INSN_UID (insn));
  5444. return false;
  5445. }
  5446. /* The block can have invalid av set, in which case it was created earlier
  5447. during move_op. Return immediately. */
  5448. if (sel_bb_head_p (insn))
  5449. {
  5450. if (! AV_SET_VALID_P (insn))
  5451. {
  5452. if (sched_verbose >= 6)
  5453. sel_print ("Returned from block %d as it had invalid av set\n",
  5454. bb->index);
  5455. return false;
  5456. }
  5457. if (bitmap_bit_p (code_motion_visited_blocks, bb->index))
  5458. {
  5459. /* We have already found an original operation on this branch, do not
  5460. go any further and just return TRUE here. If we don't stop here,
  5461. function can have exponential behaviour even on the small code
  5462. with many different paths (e.g. with data speculation and
  5463. recovery blocks). */
  5464. if (sched_verbose >= 6)
  5465. sel_print ("Block %d already visited in this traversal\n", bb->index);
  5466. if (code_motion_path_driver_info->on_enter)
  5467. return code_motion_path_driver_info->on_enter (insn,
  5468. local_params_in,
  5469. static_params,
  5470. true);
  5471. }
  5472. }
  5473. if (code_motion_path_driver_info->on_enter)
  5474. code_motion_path_driver_info->on_enter (insn, local_params_in,
  5475. static_params, false);
  5476. orig_ops = av_set_copy (orig_ops);
  5477. /* Filter the orig_ops set. */
  5478. if (AV_SET_VALID_P (insn))
  5479. av_set_code_motion_filter (&orig_ops, AV_SET (insn));
  5480. /* If no more original ops, return immediately. */
  5481. if (!orig_ops)
  5482. {
  5483. if (sched_verbose >= 6)
  5484. sel_print ("No intersection with av set of block %d\n", bb->index);
  5485. return false;
  5486. }
  5487. /* For non-speculative insns we have to leave only one form of the
  5488. original operation, because if we don't, we may end up with
  5489. different C_EXPRes and, consequently, with bookkeepings for different
  5490. expression forms along the same code motion path. That may lead to
  5491. generation of incorrect code. So for each code motion we stick to
  5492. the single form of the instruction, except for speculative insns
  5493. which we need to keep in different forms with all speculation
  5494. types. */
  5495. av_set_leave_one_nonspec (&orig_ops);
  5496. /* It is not possible that all ORIG_OPS are filtered out. */
  5497. gcc_assert (orig_ops);
  5498. /* It is enough to place only heads and tails of visited basic blocks into
  5499. the PATH. */
  5500. ilist_add (&path, insn);
  5501. first_insn = insn;
  5502. bb_tail = sel_bb_end (bb);
  5503. /* Descend the basic block in search of the original expr; this part
  5504. corresponds to the part of the original move_op procedure executed
  5505. before the recursive call. */
  5506. for (;;)
  5507. {
  5508. /* Look at the insn and decide if it could be an ancestor of currently
  5509. scheduling operation. If it is so, then the insn "dest = op" could
  5510. either be replaced with "dest = reg", because REG now holds the result
  5511. of OP, or just removed, if we've scheduled the insn as a whole.
  5512. If this insn doesn't contain currently scheduling OP, then proceed
  5513. with searching and look at its successors. Operations we're searching
  5514. for could have changed when moving up through this insn via
  5515. substituting. In this case, perform unsubstitution on them first.
  5516. When traversing the DAG below this insn is finished, insert
  5517. bookkeeping code, if the insn is a joint point, and remove
  5518. leftovers. */
  5519. expr = av_set_lookup (orig_ops, INSN_VINSN (insn));
  5520. if (expr)
  5521. {
  5522. insn_t last_insn = PREV_INSN (insn);
  5523. /* We have found the original operation. */
  5524. if (sched_verbose >= 6)
  5525. sel_print ("Found original operation at insn %d\n", INSN_UID (insn));
  5526. code_motion_path_driver_info->orig_expr_found
  5527. (insn, expr, local_params_in, static_params);
  5528. /* Step back, so on the way back we'll start traversing from the
  5529. previous insn (or we'll see that it's bb_note and skip that
  5530. loop). */
  5531. if (insn == first_insn)
  5532. {
  5533. first_insn = NEXT_INSN (last_insn);
  5534. removed_last_insn = sel_bb_end_p (last_insn);
  5535. }
  5536. insn = last_insn;
  5537. break;
  5538. }
  5539. else
  5540. {
  5541. /* We haven't found the original expr, continue descending the basic
  5542. block. */
  5543. if (code_motion_path_driver_info->orig_expr_not_found
  5544. (insn, orig_ops, static_params))
  5545. {
  5546. /* Av set ops could have been changed when moving through this
  5547. insn. To find them below it, we have to un-substitute them. */
  5548. undo_transformations (&orig_ops, insn);
  5549. }
  5550. else
  5551. {
  5552. /* Clean up and return, if the hook tells us to do so. It may
  5553. happen if we've encountered the previously created
  5554. bookkeeping. */
  5555. code_motion_path_driver_cleanup (&orig_ops, &path);
  5556. return -1;
  5557. }
  5558. gcc_assert (orig_ops);
  5559. }
  5560. /* Stop at insn if we got to the end of BB. */
  5561. if (insn == bb_tail)
  5562. break;
  5563. insn = NEXT_INSN (insn);
  5564. }
  5565. /* Here INSN either points to the insn before the original insn (may be
  5566. bb_note, if original insn was a bb_head) or to the bb_end. */
  5567. if (!expr)
  5568. {
  5569. int res;
  5570. rtx_insn *last_insn = PREV_INSN (insn);
  5571. bool added_to_path;
  5572. gcc_assert (insn == sel_bb_end (bb));
  5573. /* Add bb tail to PATH (but it doesn't make any sense if it's a bb_head -
  5574. it's already in PATH then). */
  5575. if (insn != first_insn)
  5576. {
  5577. ilist_add (&path, insn);
  5578. added_to_path = true;
  5579. }
  5580. else
  5581. added_to_path = false;
  5582. /* Process_successors should be able to find at least one
  5583. successor for which code_motion_path_driver returns TRUE. */
  5584. res = code_motion_process_successors (insn, orig_ops,
  5585. path, static_params);
  5586. /* Jump in the end of basic block could have been removed or replaced
  5587. during code_motion_process_successors, so recompute insn as the
  5588. last insn in bb. */
  5589. if (NEXT_INSN (last_insn) != insn)
  5590. {
  5591. insn = sel_bb_end (bb);
  5592. first_insn = sel_bb_head (bb);
  5593. }
  5594. /* Remove bb tail from path. */
  5595. if (added_to_path)
  5596. ilist_remove (&path);
  5597. if (res != 1)
  5598. {
  5599. /* This is the case when one of the original expr is no longer available
  5600. due to bookkeeping created on this branch with the same register.
  5601. In the original algorithm, which doesn't have update_data_sets call
  5602. on a bookkeeping block, it would simply result in returning
  5603. FALSE when we've encountered a previously generated bookkeeping
  5604. insn in moveop_orig_expr_not_found. */
  5605. code_motion_path_driver_cleanup (&orig_ops, &path);
  5606. return res;
  5607. }
  5608. }
  5609. /* Don't need it any more. */
  5610. av_set_clear (&orig_ops);
  5611. /* Backward pass: now, when we have C_EXPR computed, we'll drag it to
  5612. the beginning of the basic block. */
  5613. before_first = PREV_INSN (first_insn);
  5614. while (insn != before_first)
  5615. {
  5616. if (code_motion_path_driver_info->ascend)
  5617. code_motion_path_driver_info->ascend (insn, static_params);
  5618. insn = PREV_INSN (insn);
  5619. }
  5620. /* Now we're at the bb head. */
  5621. insn = first_insn;
  5622. ilist_remove (&path);
  5623. local_params_in->removed_last_insn = removed_last_insn;
  5624. code_motion_path_driver_info->at_first_insn (insn, local_params_in, static_params);
  5625. /* This should be the very last operation as at bb head we could change
  5626. the numbering by creating bookkeeping blocks. */
  5627. if (removed_last_insn)
  5628. insn = PREV_INSN (insn);
  5629. /* If we have simplified the control flow and removed the first jump insn,
  5630. there's no point in marking this block in the visited blocks bitmap. */
  5631. if (BLOCK_FOR_INSN (insn))
  5632. bitmap_set_bit (code_motion_visited_blocks, BLOCK_FOR_INSN (insn)->index);
  5633. return true;
  5634. }
  5635. /* Move up the operations from ORIG_OPS set traversing the dag starting
  5636. from INSN. PATH represents the edges traversed so far.
  5637. DEST is the register chosen for scheduling the current expr. Insert
  5638. bookkeeping code in the join points. EXPR_VLIW is the chosen expression,
  5639. C_EXPR is how it looks like at the given cfg point.
  5640. Set *SHOULD_MOVE to indicate whether we have only disconnected
  5641. one of the insns found.
  5642. Returns whether original instructions were found, which is asserted
  5643. to be true in the caller. */
  5644. static bool
  5645. move_op (insn_t insn, av_set_t orig_ops, expr_t expr_vliw,
  5646. rtx dest, expr_t c_expr, bool *should_move)
  5647. {
  5648. struct moveop_static_params sparams;
  5649. struct cmpd_local_params lparams;
  5650. int res;
  5651. /* Init params for code_motion_path_driver. */
  5652. sparams.dest = dest;
  5653. sparams.c_expr = c_expr;
  5654. sparams.uid = INSN_UID (EXPR_INSN_RTX (expr_vliw));
  5655. #ifdef ENABLE_CHECKING
  5656. sparams.failed_insn = NULL;
  5657. #endif
  5658. sparams.was_renamed = false;
  5659. lparams.e1 = NULL;
  5660. /* We haven't visited any blocks yet. */
  5661. bitmap_clear (code_motion_visited_blocks);
  5662. /* Set appropriate hooks and data. */
  5663. code_motion_path_driver_info = &move_op_hooks;
  5664. res = code_motion_path_driver (insn, orig_ops, NULL, &lparams, &sparams);
  5665. gcc_assert (res != -1);
  5666. if (sparams.was_renamed)
  5667. EXPR_WAS_RENAMED (expr_vliw) = true;
  5668. *should_move = (sparams.uid == -1);
  5669. return res;
  5670. }
  5671. /* Functions that work with regions. */
  5672. /* Current number of seqno used in init_seqno and init_seqno_1. */
  5673. static int cur_seqno;
  5674. /* A helper for init_seqno. Traverse the region starting from BB and
  5675. compute seqnos for visited insns, marking visited bbs in VISITED_BBS.
  5676. Clear visited blocks from BLOCKS_TO_RESCHEDULE. */
  5677. static void
  5678. init_seqno_1 (basic_block bb, sbitmap visited_bbs, bitmap blocks_to_reschedule)
  5679. {
  5680. int bbi = BLOCK_TO_BB (bb->index);
  5681. insn_t insn, note = bb_note (bb);
  5682. insn_t succ_insn;
  5683. succ_iterator si;
  5684. bitmap_set_bit (visited_bbs, bbi);
  5685. if (blocks_to_reschedule)
  5686. bitmap_clear_bit (blocks_to_reschedule, bb->index);
  5687. FOR_EACH_SUCC_1 (succ_insn, si, BB_END (bb),
  5688. SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
  5689. {
  5690. basic_block succ = BLOCK_FOR_INSN (succ_insn);
  5691. int succ_bbi = BLOCK_TO_BB (succ->index);
  5692. gcc_assert (in_current_region_p (succ));
  5693. if (!bitmap_bit_p (visited_bbs, succ_bbi))
  5694. {
  5695. gcc_assert (succ_bbi > bbi);
  5696. init_seqno_1 (succ, visited_bbs, blocks_to_reschedule);
  5697. }
  5698. else if (blocks_to_reschedule)
  5699. bitmap_set_bit (forced_ebb_heads, succ->index);
  5700. }
  5701. for (insn = BB_END (bb); insn != note; insn = PREV_INSN (insn))
  5702. INSN_SEQNO (insn) = cur_seqno--;
  5703. }
  5704. /* Initialize seqnos for the current region. BLOCKS_TO_RESCHEDULE contains
  5705. blocks on which we're rescheduling when pipelining, FROM is the block where
  5706. traversing region begins (it may not be the head of the region when
  5707. pipelining, but the head of the loop instead).
  5708. Returns the maximal seqno found. */
  5709. static int
  5710. init_seqno (bitmap blocks_to_reschedule, basic_block from)
  5711. {
  5712. sbitmap visited_bbs;
  5713. bitmap_iterator bi;
  5714. unsigned bbi;
  5715. visited_bbs = sbitmap_alloc (current_nr_blocks);
  5716. if (blocks_to_reschedule)
  5717. {
  5718. bitmap_ones (visited_bbs);
  5719. EXECUTE_IF_SET_IN_BITMAP (blocks_to_reschedule, 0, bbi, bi)
  5720. {
  5721. gcc_assert (BLOCK_TO_BB (bbi) < current_nr_blocks);
  5722. bitmap_clear_bit (visited_bbs, BLOCK_TO_BB (bbi));
  5723. }
  5724. }
  5725. else
  5726. {
  5727. bitmap_clear (visited_bbs);
  5728. from = EBB_FIRST_BB (0);
  5729. }
  5730. cur_seqno = sched_max_luid - 1;
  5731. init_seqno_1 (from, visited_bbs, blocks_to_reschedule);
  5732. /* cur_seqno may be positive if the number of instructions is less than
  5733. sched_max_luid - 1 (when rescheduling or if some instructions have been
  5734. removed by the call to purge_empty_blocks in sel_sched_region_1). */
  5735. gcc_assert (cur_seqno >= 0);
  5736. sbitmap_free (visited_bbs);
  5737. return sched_max_luid - 1;
  5738. }
  5739. /* Initialize scheduling parameters for current region. */
  5740. static void
  5741. sel_setup_region_sched_flags (void)
  5742. {
  5743. enable_schedule_as_rhs_p = 1;
  5744. bookkeeping_p = 1;
  5745. pipelining_p = (bookkeeping_p
  5746. && (flag_sel_sched_pipelining != 0)
  5747. && current_loop_nest != NULL
  5748. && loop_has_exit_edges (current_loop_nest));
  5749. max_insns_to_rename = PARAM_VALUE (PARAM_SELSCHED_INSNS_TO_RENAME);
  5750. max_ws = MAX_WS;
  5751. }
  5752. /* Return true if all basic blocks of current region are empty. */
  5753. static bool
  5754. current_region_empty_p (void)
  5755. {
  5756. int i;
  5757. for (i = 0; i < current_nr_blocks; i++)
  5758. if (! sel_bb_empty_p (BASIC_BLOCK_FOR_FN (cfun, BB_TO_BLOCK (i))))
  5759. return false;
  5760. return true;
  5761. }
  5762. /* Prepare and verify loop nest for pipelining. */
  5763. static void
  5764. setup_current_loop_nest (int rgn, bb_vec_t *bbs)
  5765. {
  5766. current_loop_nest = get_loop_nest_for_rgn (rgn);
  5767. if (!current_loop_nest)
  5768. return;
  5769. /* If this loop has any saved loop preheaders from nested loops,
  5770. add these basic blocks to the current region. */
  5771. sel_add_loop_preheaders (bbs);
  5772. /* Check that we're starting with a valid information. */
  5773. gcc_assert (loop_latch_edge (current_loop_nest));
  5774. gcc_assert (LOOP_MARKED_FOR_PIPELINING_P (current_loop_nest));
  5775. }
  5776. /* Compute instruction priorities for current region. */
  5777. static void
  5778. sel_compute_priorities (int rgn)
  5779. {
  5780. sched_rgn_compute_dependencies (rgn);
  5781. /* Compute insn priorities in haifa style. Then free haifa style
  5782. dependencies that we've calculated for this. */
  5783. compute_priorities ();
  5784. if (sched_verbose >= 5)
  5785. debug_rgn_dependencies (0);
  5786. free_rgn_deps ();
  5787. }
  5788. /* Init scheduling data for RGN. Returns true when this region should not
  5789. be scheduled. */
  5790. static bool
  5791. sel_region_init (int rgn)
  5792. {
  5793. int i;
  5794. bb_vec_t bbs;
  5795. rgn_setup_region (rgn);
  5796. /* Even if sched_is_disabled_for_current_region_p() is true, we still
  5797. do region initialization here so the region can be bundled correctly,
  5798. but we'll skip the scheduling in sel_sched_region (). */
  5799. if (current_region_empty_p ())
  5800. return true;
  5801. bbs.create (current_nr_blocks);
  5802. for (i = 0; i < current_nr_blocks; i++)
  5803. bbs.quick_push (BASIC_BLOCK_FOR_FN (cfun, BB_TO_BLOCK (i)));
  5804. sel_init_bbs (bbs);
  5805. if (flag_sel_sched_pipelining)
  5806. setup_current_loop_nest (rgn, &bbs);
  5807. sel_setup_region_sched_flags ();
  5808. /* Initialize luids and dependence analysis which both sel-sched and haifa
  5809. need. */
  5810. sched_init_luids (bbs);
  5811. sched_deps_init (false);
  5812. /* Initialize haifa data. */
  5813. rgn_setup_sched_infos ();
  5814. sel_set_sched_flags ();
  5815. haifa_init_h_i_d (bbs);
  5816. sel_compute_priorities (rgn);
  5817. init_deps_global ();
  5818. /* Main initialization. */
  5819. sel_setup_sched_infos ();
  5820. sel_init_global_and_expr (bbs);
  5821. bbs.release ();
  5822. blocks_to_reschedule = BITMAP_ALLOC (NULL);
  5823. /* Init correct liveness sets on each instruction of a single-block loop.
  5824. This is the only situation when we can't update liveness when calling
  5825. compute_live for the first insn of the loop. */
  5826. if (current_loop_nest)
  5827. {
  5828. int header =
  5829. (sel_is_loop_preheader_p (BASIC_BLOCK_FOR_FN (cfun, BB_TO_BLOCK (0)))
  5830. ? 1
  5831. : 0);
  5832. if (current_nr_blocks == header + 1)
  5833. update_liveness_on_insn
  5834. (sel_bb_head (BASIC_BLOCK_FOR_FN (cfun, BB_TO_BLOCK (header))));
  5835. }
  5836. /* Set hooks so that no newly generated insn will go out unnoticed. */
  5837. sel_register_cfg_hooks ();
  5838. /* !!! We call target.sched.init () for the whole region, but we invoke
  5839. targetm.sched.finish () for every ebb. */
  5840. if (targetm.sched.init)
  5841. /* None of the arguments are actually used in any target. */
  5842. targetm.sched.init (sched_dump, sched_verbose, -1);
  5843. first_emitted_uid = get_max_uid () + 1;
  5844. preheader_removed = false;
  5845. /* Reset register allocation ticks array. */
  5846. memset (reg_rename_tick, 0, sizeof reg_rename_tick);
  5847. reg_rename_this_tick = 0;
  5848. bitmap_initialize (forced_ebb_heads, 0);
  5849. bitmap_clear (forced_ebb_heads);
  5850. setup_nop_vinsn ();
  5851. current_copies = BITMAP_ALLOC (NULL);
  5852. current_originators = BITMAP_ALLOC (NULL);
  5853. code_motion_visited_blocks = BITMAP_ALLOC (NULL);
  5854. return false;
  5855. }
  5856. /* Simplify insns after the scheduling. */
  5857. static void
  5858. simplify_changed_insns (void)
  5859. {
  5860. int i;
  5861. for (i = 0; i < current_nr_blocks; i++)
  5862. {
  5863. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, BB_TO_BLOCK (i));
  5864. rtx_insn *insn;
  5865. FOR_BB_INSNS (bb, insn)
  5866. if (INSN_P (insn))
  5867. {
  5868. expr_t expr = INSN_EXPR (insn);
  5869. if (EXPR_WAS_SUBSTITUTED (expr))
  5870. validate_simplify_insn (insn);
  5871. }
  5872. }
  5873. }
  5874. /* Find boundaries of the EBB starting from basic block BB, marking blocks of
  5875. this EBB in SCHEDULED_BLOCKS and appropriately filling in HEAD, TAIL,
  5876. PREV_HEAD, and NEXT_TAIL fields of CURRENT_SCHED_INFO structure. */
  5877. static void
  5878. find_ebb_boundaries (basic_block bb, bitmap scheduled_blocks)
  5879. {
  5880. rtx_insn *head, *tail;
  5881. basic_block bb1 = bb;
  5882. if (sched_verbose >= 2)
  5883. sel_print ("Finishing schedule in bbs: ");
  5884. do
  5885. {
  5886. bitmap_set_bit (scheduled_blocks, BLOCK_TO_BB (bb1->index));
  5887. if (sched_verbose >= 2)
  5888. sel_print ("%d; ", bb1->index);
  5889. }
  5890. while (!bb_ends_ebb_p (bb1) && (bb1 = bb_next_bb (bb1)));
  5891. if (sched_verbose >= 2)
  5892. sel_print ("\n");
  5893. get_ebb_head_tail (bb, bb1, &head, &tail);
  5894. current_sched_info->head = head;
  5895. current_sched_info->tail = tail;
  5896. current_sched_info->prev_head = PREV_INSN (head);
  5897. current_sched_info->next_tail = NEXT_INSN (tail);
  5898. }
  5899. /* Regenerate INSN_SCHED_CYCLEs for insns of current EBB. */
  5900. static void
  5901. reset_sched_cycles_in_current_ebb (void)
  5902. {
  5903. int last_clock = 0;
  5904. int haifa_last_clock = -1;
  5905. int haifa_clock = 0;
  5906. int issued_insns = 0;
  5907. insn_t insn;
  5908. if (targetm.sched.init)
  5909. {
  5910. /* None of the arguments are actually used in any target.
  5911. NB: We should have md_reset () hook for cases like this. */
  5912. targetm.sched.init (sched_dump, sched_verbose, -1);
  5913. }
  5914. state_reset (curr_state);
  5915. advance_state (curr_state);
  5916. for (insn = current_sched_info->head;
  5917. insn != current_sched_info->next_tail;
  5918. insn = NEXT_INSN (insn))
  5919. {
  5920. int cost, haifa_cost;
  5921. int sort_p;
  5922. bool asm_p, real_insn, after_stall, all_issued;
  5923. int clock;
  5924. if (!INSN_P (insn))
  5925. continue;
  5926. asm_p = false;
  5927. real_insn = recog_memoized (insn) >= 0;
  5928. clock = INSN_SCHED_CYCLE (insn);
  5929. cost = clock - last_clock;
  5930. /* Initialize HAIFA_COST. */
  5931. if (! real_insn)
  5932. {
  5933. asm_p = INSN_ASM_P (insn);
  5934. if (asm_p)
  5935. /* This is asm insn which *had* to be scheduled first
  5936. on the cycle. */
  5937. haifa_cost = 1;
  5938. else
  5939. /* This is a use/clobber insn. It should not change
  5940. cost. */
  5941. haifa_cost = 0;
  5942. }
  5943. else
  5944. haifa_cost = estimate_insn_cost (insn, curr_state);
  5945. /* Stall for whatever cycles we've stalled before. */
  5946. after_stall = 0;
  5947. if (INSN_AFTER_STALL_P (insn) && cost > haifa_cost)
  5948. {
  5949. haifa_cost = cost;
  5950. after_stall = 1;
  5951. }
  5952. all_issued = issued_insns == issue_rate;
  5953. if (haifa_cost == 0 && all_issued)
  5954. haifa_cost = 1;
  5955. if (haifa_cost > 0)
  5956. {
  5957. int i = 0;
  5958. while (haifa_cost--)
  5959. {
  5960. advance_state (curr_state);
  5961. issued_insns = 0;
  5962. i++;
  5963. if (sched_verbose >= 2)
  5964. {
  5965. sel_print ("advance_state (state_transition)\n");
  5966. debug_state (curr_state);
  5967. }
  5968. /* The DFA may report that e.g. insn requires 2 cycles to be
  5969. issued, but on the next cycle it says that insn is ready
  5970. to go. Check this here. */
  5971. if (!after_stall
  5972. && real_insn
  5973. && haifa_cost > 0
  5974. && estimate_insn_cost (insn, curr_state) == 0)
  5975. break;
  5976. /* When the data dependency stall is longer than the DFA stall,
  5977. and when we have issued exactly issue_rate insns and stalled,
  5978. it could be that after this longer stall the insn will again
  5979. become unavailable to the DFA restrictions. Looks strange
  5980. but happens e.g. on x86-64. So recheck DFA on the last
  5981. iteration. */
  5982. if ((after_stall || all_issued)
  5983. && real_insn
  5984. && haifa_cost == 0)
  5985. haifa_cost = estimate_insn_cost (insn, curr_state);
  5986. }
  5987. haifa_clock += i;
  5988. if (sched_verbose >= 2)
  5989. sel_print ("haifa clock: %d\n", haifa_clock);
  5990. }
  5991. else
  5992. gcc_assert (haifa_cost == 0);
  5993. if (sched_verbose >= 2)
  5994. sel_print ("Haifa cost for insn %d: %d\n", INSN_UID (insn), haifa_cost);
  5995. if (targetm.sched.dfa_new_cycle)
  5996. while (targetm.sched.dfa_new_cycle (sched_dump, sched_verbose, insn,
  5997. haifa_last_clock, haifa_clock,
  5998. &sort_p))
  5999. {
  6000. advance_state (curr_state);
  6001. issued_insns = 0;
  6002. haifa_clock++;
  6003. if (sched_verbose >= 2)
  6004. {
  6005. sel_print ("advance_state (dfa_new_cycle)\n");
  6006. debug_state (curr_state);
  6007. sel_print ("haifa clock: %d\n", haifa_clock + 1);
  6008. }
  6009. }
  6010. if (real_insn)
  6011. {
  6012. static state_t temp = NULL;
  6013. if (!temp)
  6014. temp = xmalloc (dfa_state_size);
  6015. memcpy (temp, curr_state, dfa_state_size);
  6016. cost = state_transition (curr_state, insn);
  6017. if (memcmp (temp, curr_state, dfa_state_size))
  6018. issued_insns++;
  6019. if (sched_verbose >= 2)
  6020. {
  6021. sel_print ("scheduled insn %d, clock %d\n", INSN_UID (insn),
  6022. haifa_clock + 1);
  6023. debug_state (curr_state);
  6024. }
  6025. gcc_assert (cost < 0);
  6026. }
  6027. if (targetm.sched.variable_issue)
  6028. targetm.sched.variable_issue (sched_dump, sched_verbose, insn, 0);
  6029. INSN_SCHED_CYCLE (insn) = haifa_clock;
  6030. last_clock = clock;
  6031. haifa_last_clock = haifa_clock;
  6032. }
  6033. }
  6034. /* Put TImode markers on insns starting a new issue group. */
  6035. static void
  6036. put_TImodes (void)
  6037. {
  6038. int last_clock = -1;
  6039. insn_t insn;
  6040. for (insn = current_sched_info->head; insn != current_sched_info->next_tail;
  6041. insn = NEXT_INSN (insn))
  6042. {
  6043. int cost, clock;
  6044. if (!INSN_P (insn))
  6045. continue;
  6046. clock = INSN_SCHED_CYCLE (insn);
  6047. cost = (last_clock == -1) ? 1 : clock - last_clock;
  6048. gcc_assert (cost >= 0);
  6049. if (issue_rate > 1
  6050. && GET_CODE (PATTERN (insn)) != USE
  6051. && GET_CODE (PATTERN (insn)) != CLOBBER)
  6052. {
  6053. if (reload_completed && cost > 0)
  6054. PUT_MODE (insn, TImode);
  6055. last_clock = clock;
  6056. }
  6057. if (sched_verbose >= 2)
  6058. sel_print ("Cost for insn %d is %d\n", INSN_UID (insn), cost);
  6059. }
  6060. }
  6061. /* Perform MD_FINISH on EBBs comprising current region. When
  6062. RESET_SCHED_CYCLES_P is true, run a pass emulating the scheduler
  6063. to produce correct sched cycles on insns. */
  6064. static void
  6065. sel_region_target_finish (bool reset_sched_cycles_p)
  6066. {
  6067. int i;
  6068. bitmap scheduled_blocks = BITMAP_ALLOC (NULL);
  6069. for (i = 0; i < current_nr_blocks; i++)
  6070. {
  6071. if (bitmap_bit_p (scheduled_blocks, i))
  6072. continue;
  6073. /* While pipelining outer loops, skip bundling for loop
  6074. preheaders. Those will be rescheduled in the outer loop. */
  6075. if (sel_is_loop_preheader_p (EBB_FIRST_BB (i)))
  6076. continue;
  6077. find_ebb_boundaries (EBB_FIRST_BB (i), scheduled_blocks);
  6078. if (no_real_insns_p (current_sched_info->head, current_sched_info->tail))
  6079. continue;
  6080. if (reset_sched_cycles_p)
  6081. reset_sched_cycles_in_current_ebb ();
  6082. if (targetm.sched.init)
  6083. targetm.sched.init (sched_dump, sched_verbose, -1);
  6084. put_TImodes ();
  6085. if (targetm.sched.finish)
  6086. {
  6087. targetm.sched.finish (sched_dump, sched_verbose);
  6088. /* Extend luids so that insns generated by the target will
  6089. get zero luid. */
  6090. sched_extend_luids ();
  6091. }
  6092. }
  6093. BITMAP_FREE (scheduled_blocks);
  6094. }
  6095. /* Free the scheduling data for the current region. When RESET_SCHED_CYCLES_P
  6096. is true, make an additional pass emulating scheduler to get correct insn
  6097. cycles for md_finish calls. */
  6098. static void
  6099. sel_region_finish (bool reset_sched_cycles_p)
  6100. {
  6101. simplify_changed_insns ();
  6102. sched_finish_ready_list ();
  6103. free_nop_pool ();
  6104. /* Free the vectors. */
  6105. vec_av_set.release ();
  6106. BITMAP_FREE (current_copies);
  6107. BITMAP_FREE (current_originators);
  6108. BITMAP_FREE (code_motion_visited_blocks);
  6109. vinsn_vec_free (vec_bookkeeping_blocked_vinsns);
  6110. vinsn_vec_free (vec_target_unavailable_vinsns);
  6111. /* If LV_SET of the region head should be updated, do it now because
  6112. there will be no other chance. */
  6113. {
  6114. succ_iterator si;
  6115. insn_t insn;
  6116. FOR_EACH_SUCC_1 (insn, si, bb_note (EBB_FIRST_BB (0)),
  6117. SUCCS_NORMAL | SUCCS_SKIP_TO_LOOP_EXITS)
  6118. {
  6119. basic_block bb = BLOCK_FOR_INSN (insn);
  6120. if (!BB_LV_SET_VALID_P (bb))
  6121. compute_live (insn);
  6122. }
  6123. }
  6124. /* Emulate the Haifa scheduler for bundling. */
  6125. if (reload_completed)
  6126. sel_region_target_finish (reset_sched_cycles_p);
  6127. sel_finish_global_and_expr ();
  6128. bitmap_clear (forced_ebb_heads);
  6129. free_nop_vinsn ();
  6130. finish_deps_global ();
  6131. sched_finish_luids ();
  6132. h_d_i_d.release ();
  6133. sel_finish_bbs ();
  6134. BITMAP_FREE (blocks_to_reschedule);
  6135. sel_unregister_cfg_hooks ();
  6136. max_issue_size = 0;
  6137. }
  6138. /* Functions that implement the scheduler driver. */
  6139. /* Schedule a parallel instruction group on each of FENCES. MAX_SEQNO
  6140. is the current maximum seqno. SCHEDULED_INSNS_TAILPP is the list
  6141. of insns scheduled -- these would be postprocessed later. */
  6142. static void
  6143. schedule_on_fences (flist_t fences, int max_seqno,
  6144. ilist_t **scheduled_insns_tailpp)
  6145. {
  6146. flist_t old_fences = fences;
  6147. if (sched_verbose >= 1)
  6148. {
  6149. sel_print ("\nScheduling on fences: ");
  6150. dump_flist (fences);
  6151. sel_print ("\n");
  6152. }
  6153. scheduled_something_on_previous_fence = false;
  6154. for (; fences; fences = FLIST_NEXT (fences))
  6155. {
  6156. fence_t fence = NULL;
  6157. int seqno = 0;
  6158. flist_t fences2;
  6159. bool first_p = true;
  6160. /* Choose the next fence group to schedule.
  6161. The fact that insn can be scheduled only once
  6162. on the cycle is guaranteed by two properties:
  6163. 1. seqnos of parallel groups decrease with each iteration.
  6164. 2. If is_ineligible_successor () sees the larger seqno, it
  6165. checks if candidate insn is_in_current_fence_p (). */
  6166. for (fences2 = old_fences; fences2; fences2 = FLIST_NEXT (fences2))
  6167. {
  6168. fence_t f = FLIST_FENCE (fences2);
  6169. if (!FENCE_PROCESSED_P (f))
  6170. {
  6171. int i = INSN_SEQNO (FENCE_INSN (f));
  6172. if (first_p || i > seqno)
  6173. {
  6174. seqno = i;
  6175. fence = f;
  6176. first_p = false;
  6177. }
  6178. else
  6179. /* ??? Seqnos of different groups should be different. */
  6180. gcc_assert (1 || i != seqno);
  6181. }
  6182. }
  6183. gcc_assert (fence);
  6184. /* As FENCE is nonnull, SEQNO is initialized. */
  6185. seqno -= max_seqno + 1;
  6186. fill_insns (fence, seqno, scheduled_insns_tailpp);
  6187. FENCE_PROCESSED_P (fence) = true;
  6188. }
  6189. /* All av_sets are invalidated by GLOBAL_LEVEL increase, thus we
  6190. don't need to keep bookkeeping-invalidated and target-unavailable
  6191. vinsns any more. */
  6192. vinsn_vec_clear (&vec_bookkeeping_blocked_vinsns);
  6193. vinsn_vec_clear (&vec_target_unavailable_vinsns);
  6194. }
  6195. /* Calculate MIN_SEQNO and MAX_SEQNO. */
  6196. static void
  6197. find_min_max_seqno (flist_t fences, int *min_seqno, int *max_seqno)
  6198. {
  6199. *min_seqno = *max_seqno = INSN_SEQNO (FENCE_INSN (FLIST_FENCE (fences)));
  6200. /* The first element is already processed. */
  6201. while ((fences = FLIST_NEXT (fences)))
  6202. {
  6203. int seqno = INSN_SEQNO (FENCE_INSN (FLIST_FENCE (fences)));
  6204. if (*min_seqno > seqno)
  6205. *min_seqno = seqno;
  6206. else if (*max_seqno < seqno)
  6207. *max_seqno = seqno;
  6208. }
  6209. }
  6210. /* Calculate new fences from FENCES. Write the current time to PTIME. */
  6211. static flist_t
  6212. calculate_new_fences (flist_t fences, int orig_max_seqno, int *ptime)
  6213. {
  6214. flist_t old_fences = fences;
  6215. struct flist_tail_def _new_fences, *new_fences = &_new_fences;
  6216. int max_time = 0;
  6217. flist_tail_init (new_fences);
  6218. for (; fences; fences = FLIST_NEXT (fences))
  6219. {
  6220. fence_t fence = FLIST_FENCE (fences);
  6221. insn_t insn;
  6222. if (!FENCE_BNDS (fence))
  6223. {
  6224. /* This fence doesn't have any successors. */
  6225. if (!FENCE_SCHEDULED_P (fence))
  6226. {
  6227. /* Nothing was scheduled on this fence. */
  6228. int seqno;
  6229. insn = FENCE_INSN (fence);
  6230. seqno = INSN_SEQNO (insn);
  6231. gcc_assert (seqno > 0 && seqno <= orig_max_seqno);
  6232. if (sched_verbose >= 1)
  6233. sel_print ("Fence %d[%d] has not changed\n",
  6234. INSN_UID (insn),
  6235. BLOCK_NUM (insn));
  6236. move_fence_to_fences (fences, new_fences);
  6237. }
  6238. }
  6239. else
  6240. extract_new_fences_from (fences, new_fences, orig_max_seqno);
  6241. max_time = MAX (max_time, FENCE_CYCLE (fence));
  6242. }
  6243. flist_clear (&old_fences);
  6244. *ptime = max_time;
  6245. return FLIST_TAIL_HEAD (new_fences);
  6246. }
  6247. /* Update seqnos of insns given by PSCHEDULED_INSNS. MIN_SEQNO and MAX_SEQNO
  6248. are the miminum and maximum seqnos of the group, HIGHEST_SEQNO_IN_USE is
  6249. the highest seqno used in a region. Return the updated highest seqno. */
  6250. static int
  6251. update_seqnos_and_stage (int min_seqno, int max_seqno,
  6252. int highest_seqno_in_use,
  6253. ilist_t *pscheduled_insns)
  6254. {
  6255. int new_hs;
  6256. ilist_iterator ii;
  6257. insn_t insn;
  6258. /* Actually, new_hs is the seqno of the instruction, that was
  6259. scheduled first (i.e. it is the first one in SCHEDULED_INSNS). */
  6260. if (*pscheduled_insns)
  6261. {
  6262. new_hs = (INSN_SEQNO (ILIST_INSN (*pscheduled_insns))
  6263. + highest_seqno_in_use + max_seqno - min_seqno + 2);
  6264. gcc_assert (new_hs > highest_seqno_in_use);
  6265. }
  6266. else
  6267. new_hs = highest_seqno_in_use;
  6268. FOR_EACH_INSN (insn, ii, *pscheduled_insns)
  6269. {
  6270. gcc_assert (INSN_SEQNO (insn) < 0);
  6271. INSN_SEQNO (insn) += highest_seqno_in_use + max_seqno - min_seqno + 2;
  6272. gcc_assert (INSN_SEQNO (insn) <= new_hs);
  6273. /* When not pipelining, purge unneeded insn info on the scheduled insns.
  6274. For example, having reg_last array of INSN_DEPS_CONTEXT in memory may
  6275. require > 1GB of memory e.g. on limit-fnargs.c. */
  6276. if (! pipelining_p)
  6277. free_data_for_scheduled_insn (insn);
  6278. }
  6279. ilist_clear (pscheduled_insns);
  6280. global_level++;
  6281. return new_hs;
  6282. }
  6283. /* The main driver for scheduling a region. This function is responsible
  6284. for correct propagation of fences (i.e. scheduling points) and creating
  6285. a group of parallel insns at each of them. It also supports
  6286. pipelining. ORIG_MAX_SEQNO is the maximal seqno before this pass
  6287. of scheduling. */
  6288. static void
  6289. sel_sched_region_2 (int orig_max_seqno)
  6290. {
  6291. int highest_seqno_in_use = orig_max_seqno;
  6292. int max_time = 0;
  6293. stat_bookkeeping_copies = 0;
  6294. stat_insns_needed_bookkeeping = 0;
  6295. stat_renamed_scheduled = 0;
  6296. stat_substitutions_total = 0;
  6297. num_insns_scheduled = 0;
  6298. while (fences)
  6299. {
  6300. int min_seqno, max_seqno;
  6301. ilist_t scheduled_insns = NULL;
  6302. ilist_t *scheduled_insns_tailp = &scheduled_insns;
  6303. find_min_max_seqno (fences, &min_seqno, &max_seqno);
  6304. schedule_on_fences (fences, max_seqno, &scheduled_insns_tailp);
  6305. fences = calculate_new_fences (fences, orig_max_seqno, &max_time);
  6306. highest_seqno_in_use = update_seqnos_and_stage (min_seqno, max_seqno,
  6307. highest_seqno_in_use,
  6308. &scheduled_insns);
  6309. }
  6310. if (sched_verbose >= 1)
  6311. {
  6312. sel_print ("Total scheduling time: %d cycles\n", max_time);
  6313. sel_print ("Scheduled %d bookkeeping copies, %d insns needed "
  6314. "bookkeeping, %d insns renamed, %d insns substituted\n",
  6315. stat_bookkeeping_copies,
  6316. stat_insns_needed_bookkeeping,
  6317. stat_renamed_scheduled,
  6318. stat_substitutions_total);
  6319. }
  6320. }
  6321. /* Schedule a region. When pipelining, search for possibly never scheduled
  6322. bookkeeping code and schedule it. Reschedule pipelined code without
  6323. pipelining after. */
  6324. static void
  6325. sel_sched_region_1 (void)
  6326. {
  6327. int orig_max_seqno;
  6328. /* Remove empty blocks that might be in the region from the beginning. */
  6329. purge_empty_blocks ();
  6330. orig_max_seqno = init_seqno (NULL, NULL);
  6331. gcc_assert (orig_max_seqno >= 1);
  6332. /* When pipelining outer loops, create fences on the loop header,
  6333. not preheader. */
  6334. fences = NULL;
  6335. if (current_loop_nest)
  6336. init_fences (BB_END (EBB_FIRST_BB (0)));
  6337. else
  6338. init_fences (bb_note (EBB_FIRST_BB (0)));
  6339. global_level = 1;
  6340. sel_sched_region_2 (orig_max_seqno);
  6341. gcc_assert (fences == NULL);
  6342. if (pipelining_p)
  6343. {
  6344. int i;
  6345. basic_block bb;
  6346. struct flist_tail_def _new_fences;
  6347. flist_tail_t new_fences = &_new_fences;
  6348. bool do_p = true;
  6349. pipelining_p = false;
  6350. max_ws = MIN (max_ws, issue_rate * 3 / 2);
  6351. bookkeeping_p = false;
  6352. enable_schedule_as_rhs_p = false;
  6353. /* Schedule newly created code, that has not been scheduled yet. */
  6354. do_p = true;
  6355. while (do_p)
  6356. {
  6357. do_p = false;
  6358. for (i = 0; i < current_nr_blocks; i++)
  6359. {
  6360. basic_block bb = EBB_FIRST_BB (i);
  6361. if (bitmap_bit_p (blocks_to_reschedule, bb->index))
  6362. {
  6363. if (! bb_ends_ebb_p (bb))
  6364. bitmap_set_bit (blocks_to_reschedule, bb_next_bb (bb)->index);
  6365. if (sel_bb_empty_p (bb))
  6366. {
  6367. bitmap_clear_bit (blocks_to_reschedule, bb->index);
  6368. continue;
  6369. }
  6370. clear_outdated_rtx_info (bb);
  6371. if (sel_insn_is_speculation_check (BB_END (bb))
  6372. && JUMP_P (BB_END (bb)))
  6373. bitmap_set_bit (blocks_to_reschedule,
  6374. BRANCH_EDGE (bb)->dest->index);
  6375. }
  6376. else if (! sel_bb_empty_p (bb)
  6377. && INSN_SCHED_TIMES (sel_bb_head (bb)) <= 0)
  6378. bitmap_set_bit (blocks_to_reschedule, bb->index);
  6379. }
  6380. for (i = 0; i < current_nr_blocks; i++)
  6381. {
  6382. bb = EBB_FIRST_BB (i);
  6383. /* While pipelining outer loops, skip bundling for loop
  6384. preheaders. Those will be rescheduled in the outer
  6385. loop. */
  6386. if (sel_is_loop_preheader_p (bb))
  6387. {
  6388. clear_outdated_rtx_info (bb);
  6389. continue;
  6390. }
  6391. if (bitmap_bit_p (blocks_to_reschedule, bb->index))
  6392. {
  6393. flist_tail_init (new_fences);
  6394. orig_max_seqno = init_seqno (blocks_to_reschedule, bb);
  6395. /* Mark BB as head of the new ebb. */
  6396. bitmap_set_bit (forced_ebb_heads, bb->index);
  6397. gcc_assert (fences == NULL);
  6398. init_fences (bb_note (bb));
  6399. sel_sched_region_2 (orig_max_seqno);
  6400. do_p = true;
  6401. break;
  6402. }
  6403. }
  6404. }
  6405. }
  6406. }
  6407. /* Schedule the RGN region. */
  6408. void
  6409. sel_sched_region (int rgn)
  6410. {
  6411. bool schedule_p;
  6412. bool reset_sched_cycles_p;
  6413. if (sel_region_init (rgn))
  6414. return;
  6415. if (sched_verbose >= 1)
  6416. sel_print ("Scheduling region %d\n", rgn);
  6417. schedule_p = (!sched_is_disabled_for_current_region_p ()
  6418. && dbg_cnt (sel_sched_region_cnt));
  6419. reset_sched_cycles_p = pipelining_p;
  6420. if (schedule_p)
  6421. sel_sched_region_1 ();
  6422. else
  6423. /* Force initialization of INSN_SCHED_CYCLEs for correct bundling. */
  6424. reset_sched_cycles_p = true;
  6425. sel_region_finish (reset_sched_cycles_p);
  6426. }
  6427. /* Perform global init for the scheduler. */
  6428. static void
  6429. sel_global_init (void)
  6430. {
  6431. calculate_dominance_info (CDI_DOMINATORS);
  6432. alloc_sched_pools ();
  6433. /* Setup the infos for sched_init. */
  6434. sel_setup_sched_infos ();
  6435. setup_sched_dump ();
  6436. sched_rgn_init (false);
  6437. sched_init ();
  6438. sched_init_bbs ();
  6439. /* Reset AFTER_RECOVERY if it has been set by the 1st scheduler pass. */
  6440. after_recovery = 0;
  6441. can_issue_more = issue_rate;
  6442. sched_extend_target ();
  6443. sched_deps_init (true);
  6444. setup_nop_and_exit_insns ();
  6445. sel_extend_global_bb_info ();
  6446. init_lv_sets ();
  6447. init_hard_regs_data ();
  6448. }
  6449. /* Free the global data of the scheduler. */
  6450. static void
  6451. sel_global_finish (void)
  6452. {
  6453. free_bb_note_pool ();
  6454. free_lv_sets ();
  6455. sel_finish_global_bb_info ();
  6456. free_regset_pool ();
  6457. free_nop_and_exit_insns ();
  6458. sched_rgn_finish ();
  6459. sched_deps_finish ();
  6460. sched_finish ();
  6461. if (current_loops)
  6462. sel_finish_pipelining ();
  6463. free_sched_pools ();
  6464. free_dominance_info (CDI_DOMINATORS);
  6465. }
  6466. /* Return true when we need to skip selective scheduling. Used for debugging. */
  6467. bool
  6468. maybe_skip_selective_scheduling (void)
  6469. {
  6470. return ! dbg_cnt (sel_sched_cnt);
  6471. }
  6472. /* The entry point. */
  6473. void
  6474. run_selective_scheduling (void)
  6475. {
  6476. int rgn;
  6477. if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
  6478. return;
  6479. sel_global_init ();
  6480. for (rgn = 0; rgn < nr_regions; rgn++)
  6481. sel_sched_region (rgn);
  6482. sel_global_finish ();
  6483. }
  6484. #endif