sched-deps.c 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938
  1. /* Instruction scheduling pass. This file computes dependencies between
  2. instructions.
  3. Copyright (C) 1992-2015 Free Software Foundation, Inc.
  4. Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
  5. and currently maintained by, Jim Wilson (wilson@cygnus.com)
  6. This file is part of GCC.
  7. GCC is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 3, or (at your option) any later
  10. version.
  11. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  12. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with GCC; see the file COPYING3. If not see
  17. <http://www.gnu.org/licenses/>. */
  18. #include "config.h"
  19. #include "system.h"
  20. #include "coretypes.h"
  21. #include "tm.h"
  22. #include "diagnostic-core.h"
  23. #include "rtl.h"
  24. #include "hash-set.h"
  25. #include "machmode.h"
  26. #include "vec.h"
  27. #include "double-int.h"
  28. #include "input.h"
  29. #include "alias.h"
  30. #include "symtab.h"
  31. #include "wide-int.h"
  32. #include "inchash.h"
  33. #include "tree.h" /* FIXME: Used by call_may_noreturn_p. */
  34. #include "tm_p.h"
  35. #include "hard-reg-set.h"
  36. #include "regs.h"
  37. #include "input.h"
  38. #include "function.h"
  39. #include "flags.h"
  40. #include "insn-config.h"
  41. #include "insn-attr.h"
  42. #include "except.h"
  43. #include "recog.h"
  44. #include "emit-rtl.h"
  45. #include "dominance.h"
  46. #include "cfg.h"
  47. #include "cfgbuild.h"
  48. #include "predict.h"
  49. #include "basic-block.h"
  50. #include "sched-int.h"
  51. #include "params.h"
  52. #include "cselib.h"
  53. #include "ira.h"
  54. #include "target.h"
  55. #ifdef INSN_SCHEDULING
  56. #ifdef ENABLE_CHECKING
  57. #define CHECK (true)
  58. #else
  59. #define CHECK (false)
  60. #endif
  61. /* Holds current parameters for the dependency analyzer. */
  62. struct sched_deps_info_def *sched_deps_info;
  63. /* The data is specific to the Haifa scheduler. */
  64. vec<haifa_deps_insn_data_def>
  65. h_d_i_d = vNULL;
  66. /* Return the major type present in the DS. */
  67. enum reg_note
  68. ds_to_dk (ds_t ds)
  69. {
  70. if (ds & DEP_TRUE)
  71. return REG_DEP_TRUE;
  72. if (ds & DEP_OUTPUT)
  73. return REG_DEP_OUTPUT;
  74. if (ds & DEP_CONTROL)
  75. return REG_DEP_CONTROL;
  76. gcc_assert (ds & DEP_ANTI);
  77. return REG_DEP_ANTI;
  78. }
  79. /* Return equivalent dep_status. */
  80. ds_t
  81. dk_to_ds (enum reg_note dk)
  82. {
  83. switch (dk)
  84. {
  85. case REG_DEP_TRUE:
  86. return DEP_TRUE;
  87. case REG_DEP_OUTPUT:
  88. return DEP_OUTPUT;
  89. case REG_DEP_CONTROL:
  90. return DEP_CONTROL;
  91. default:
  92. gcc_assert (dk == REG_DEP_ANTI);
  93. return DEP_ANTI;
  94. }
  95. }
  96. /* Functions to operate with dependence information container - dep_t. */
  97. /* Init DEP with the arguments. */
  98. void
  99. init_dep_1 (dep_t dep, rtx_insn *pro, rtx_insn *con, enum reg_note type, ds_t ds)
  100. {
  101. DEP_PRO (dep) = pro;
  102. DEP_CON (dep) = con;
  103. DEP_TYPE (dep) = type;
  104. DEP_STATUS (dep) = ds;
  105. DEP_COST (dep) = UNKNOWN_DEP_COST;
  106. DEP_NONREG (dep) = 0;
  107. DEP_MULTIPLE (dep) = 0;
  108. DEP_REPLACE (dep) = NULL;
  109. }
  110. /* Init DEP with the arguments.
  111. While most of the scheduler (including targets) only need the major type
  112. of the dependency, it is convenient to hide full dep_status from them. */
  113. void
  114. init_dep (dep_t dep, rtx_insn *pro, rtx_insn *con, enum reg_note kind)
  115. {
  116. ds_t ds;
  117. if ((current_sched_info->flags & USE_DEPS_LIST))
  118. ds = dk_to_ds (kind);
  119. else
  120. ds = 0;
  121. init_dep_1 (dep, pro, con, kind, ds);
  122. }
  123. /* Make a copy of FROM in TO. */
  124. static void
  125. copy_dep (dep_t to, dep_t from)
  126. {
  127. memcpy (to, from, sizeof (*to));
  128. }
  129. static void dump_ds (FILE *, ds_t);
  130. /* Define flags for dump_dep (). */
  131. /* Dump producer of the dependence. */
  132. #define DUMP_DEP_PRO (2)
  133. /* Dump consumer of the dependence. */
  134. #define DUMP_DEP_CON (4)
  135. /* Dump type of the dependence. */
  136. #define DUMP_DEP_TYPE (8)
  137. /* Dump status of the dependence. */
  138. #define DUMP_DEP_STATUS (16)
  139. /* Dump all information about the dependence. */
  140. #define DUMP_DEP_ALL (DUMP_DEP_PRO | DUMP_DEP_CON | DUMP_DEP_TYPE \
  141. |DUMP_DEP_STATUS)
  142. /* Dump DEP to DUMP.
  143. FLAGS is a bit mask specifying what information about DEP needs
  144. to be printed.
  145. If FLAGS has the very first bit set, then dump all information about DEP
  146. and propagate this bit into the callee dump functions. */
  147. static void
  148. dump_dep (FILE *dump, dep_t dep, int flags)
  149. {
  150. if (flags & 1)
  151. flags |= DUMP_DEP_ALL;
  152. fprintf (dump, "<");
  153. if (flags & DUMP_DEP_PRO)
  154. fprintf (dump, "%d; ", INSN_UID (DEP_PRO (dep)));
  155. if (flags & DUMP_DEP_CON)
  156. fprintf (dump, "%d; ", INSN_UID (DEP_CON (dep)));
  157. if (flags & DUMP_DEP_TYPE)
  158. {
  159. char t;
  160. enum reg_note type = DEP_TYPE (dep);
  161. switch (type)
  162. {
  163. case REG_DEP_TRUE:
  164. t = 't';
  165. break;
  166. case REG_DEP_OUTPUT:
  167. t = 'o';
  168. break;
  169. case REG_DEP_CONTROL:
  170. t = 'c';
  171. break;
  172. case REG_DEP_ANTI:
  173. t = 'a';
  174. break;
  175. default:
  176. gcc_unreachable ();
  177. break;
  178. }
  179. fprintf (dump, "%c; ", t);
  180. }
  181. if (flags & DUMP_DEP_STATUS)
  182. {
  183. if (current_sched_info->flags & USE_DEPS_LIST)
  184. dump_ds (dump, DEP_STATUS (dep));
  185. }
  186. fprintf (dump, ">");
  187. }
  188. /* Default flags for dump_dep (). */
  189. static int dump_dep_flags = (DUMP_DEP_PRO | DUMP_DEP_CON);
  190. /* Dump all fields of DEP to STDERR. */
  191. void
  192. sd_debug_dep (dep_t dep)
  193. {
  194. dump_dep (stderr, dep, 1);
  195. fprintf (stderr, "\n");
  196. }
  197. /* Determine whether DEP is a dependency link of a non-debug insn on a
  198. debug insn. */
  199. static inline bool
  200. depl_on_debug_p (dep_link_t dep)
  201. {
  202. return (DEBUG_INSN_P (DEP_LINK_PRO (dep))
  203. && !DEBUG_INSN_P (DEP_LINK_CON (dep)));
  204. }
  205. /* Functions to operate with a single link from the dependencies lists -
  206. dep_link_t. */
  207. /* Attach L to appear after link X whose &DEP_LINK_NEXT (X) is given by
  208. PREV_NEXT_P. */
  209. static void
  210. attach_dep_link (dep_link_t l, dep_link_t *prev_nextp)
  211. {
  212. dep_link_t next = *prev_nextp;
  213. gcc_assert (DEP_LINK_PREV_NEXTP (l) == NULL
  214. && DEP_LINK_NEXT (l) == NULL);
  215. /* Init node being inserted. */
  216. DEP_LINK_PREV_NEXTP (l) = prev_nextp;
  217. DEP_LINK_NEXT (l) = next;
  218. /* Fix next node. */
  219. if (next != NULL)
  220. {
  221. gcc_assert (DEP_LINK_PREV_NEXTP (next) == prev_nextp);
  222. DEP_LINK_PREV_NEXTP (next) = &DEP_LINK_NEXT (l);
  223. }
  224. /* Fix prev node. */
  225. *prev_nextp = l;
  226. }
  227. /* Add dep_link LINK to deps_list L. */
  228. static void
  229. add_to_deps_list (dep_link_t link, deps_list_t l)
  230. {
  231. attach_dep_link (link, &DEPS_LIST_FIRST (l));
  232. /* Don't count debug deps. */
  233. if (!depl_on_debug_p (link))
  234. ++DEPS_LIST_N_LINKS (l);
  235. }
  236. /* Detach dep_link L from the list. */
  237. static void
  238. detach_dep_link (dep_link_t l)
  239. {
  240. dep_link_t *prev_nextp = DEP_LINK_PREV_NEXTP (l);
  241. dep_link_t next = DEP_LINK_NEXT (l);
  242. *prev_nextp = next;
  243. if (next != NULL)
  244. DEP_LINK_PREV_NEXTP (next) = prev_nextp;
  245. DEP_LINK_PREV_NEXTP (l) = NULL;
  246. DEP_LINK_NEXT (l) = NULL;
  247. }
  248. /* Remove link LINK from list LIST. */
  249. static void
  250. remove_from_deps_list (dep_link_t link, deps_list_t list)
  251. {
  252. detach_dep_link (link);
  253. /* Don't count debug deps. */
  254. if (!depl_on_debug_p (link))
  255. --DEPS_LIST_N_LINKS (list);
  256. }
  257. /* Move link LINK from list FROM to list TO. */
  258. static void
  259. move_dep_link (dep_link_t link, deps_list_t from, deps_list_t to)
  260. {
  261. remove_from_deps_list (link, from);
  262. add_to_deps_list (link, to);
  263. }
  264. /* Return true of LINK is not attached to any list. */
  265. static bool
  266. dep_link_is_detached_p (dep_link_t link)
  267. {
  268. return DEP_LINK_PREV_NEXTP (link) == NULL;
  269. }
  270. /* Pool to hold all dependency nodes (dep_node_t). */
  271. static alloc_pool dn_pool;
  272. /* Number of dep_nodes out there. */
  273. static int dn_pool_diff = 0;
  274. /* Create a dep_node. */
  275. static dep_node_t
  276. create_dep_node (void)
  277. {
  278. dep_node_t n = (dep_node_t) pool_alloc (dn_pool);
  279. dep_link_t back = DEP_NODE_BACK (n);
  280. dep_link_t forw = DEP_NODE_FORW (n);
  281. DEP_LINK_NODE (back) = n;
  282. DEP_LINK_NEXT (back) = NULL;
  283. DEP_LINK_PREV_NEXTP (back) = NULL;
  284. DEP_LINK_NODE (forw) = n;
  285. DEP_LINK_NEXT (forw) = NULL;
  286. DEP_LINK_PREV_NEXTP (forw) = NULL;
  287. ++dn_pool_diff;
  288. return n;
  289. }
  290. /* Delete dep_node N. N must not be connected to any deps_list. */
  291. static void
  292. delete_dep_node (dep_node_t n)
  293. {
  294. gcc_assert (dep_link_is_detached_p (DEP_NODE_BACK (n))
  295. && dep_link_is_detached_p (DEP_NODE_FORW (n)));
  296. XDELETE (DEP_REPLACE (DEP_NODE_DEP (n)));
  297. --dn_pool_diff;
  298. pool_free (dn_pool, n);
  299. }
  300. /* Pool to hold dependencies lists (deps_list_t). */
  301. static alloc_pool dl_pool;
  302. /* Number of deps_lists out there. */
  303. static int dl_pool_diff = 0;
  304. /* Functions to operate with dependences lists - deps_list_t. */
  305. /* Return true if list L is empty. */
  306. static bool
  307. deps_list_empty_p (deps_list_t l)
  308. {
  309. return DEPS_LIST_N_LINKS (l) == 0;
  310. }
  311. /* Create a new deps_list. */
  312. static deps_list_t
  313. create_deps_list (void)
  314. {
  315. deps_list_t l = (deps_list_t) pool_alloc (dl_pool);
  316. DEPS_LIST_FIRST (l) = NULL;
  317. DEPS_LIST_N_LINKS (l) = 0;
  318. ++dl_pool_diff;
  319. return l;
  320. }
  321. /* Free deps_list L. */
  322. static void
  323. free_deps_list (deps_list_t l)
  324. {
  325. gcc_assert (deps_list_empty_p (l));
  326. --dl_pool_diff;
  327. pool_free (dl_pool, l);
  328. }
  329. /* Return true if there is no dep_nodes and deps_lists out there.
  330. After the region is scheduled all the dependency nodes and lists
  331. should [generally] be returned to pool. */
  332. bool
  333. deps_pools_are_empty_p (void)
  334. {
  335. return dn_pool_diff == 0 && dl_pool_diff == 0;
  336. }
  337. /* Remove all elements from L. */
  338. static void
  339. clear_deps_list (deps_list_t l)
  340. {
  341. do
  342. {
  343. dep_link_t link = DEPS_LIST_FIRST (l);
  344. if (link == NULL)
  345. break;
  346. remove_from_deps_list (link, l);
  347. }
  348. while (1);
  349. }
  350. /* Decide whether a dependency should be treated as a hard or a speculative
  351. dependency. */
  352. static bool
  353. dep_spec_p (dep_t dep)
  354. {
  355. if (current_sched_info->flags & DO_SPECULATION)
  356. {
  357. if (DEP_STATUS (dep) & SPECULATIVE)
  358. return true;
  359. }
  360. if (current_sched_info->flags & DO_PREDICATION)
  361. {
  362. if (DEP_TYPE (dep) == REG_DEP_CONTROL)
  363. return true;
  364. }
  365. if (DEP_REPLACE (dep) != NULL)
  366. return true;
  367. return false;
  368. }
  369. static regset reg_pending_sets;
  370. static regset reg_pending_clobbers;
  371. static regset reg_pending_uses;
  372. static regset reg_pending_control_uses;
  373. static enum reg_pending_barrier_mode reg_pending_barrier;
  374. /* Hard registers implicitly clobbered or used (or may be implicitly
  375. clobbered or used) by the currently analyzed insn. For example,
  376. insn in its constraint has one register class. Even if there is
  377. currently no hard register in the insn, the particular hard
  378. register will be in the insn after reload pass because the
  379. constraint requires it. */
  380. static HARD_REG_SET implicit_reg_pending_clobbers;
  381. static HARD_REG_SET implicit_reg_pending_uses;
  382. /* To speed up the test for duplicate dependency links we keep a
  383. record of dependencies created by add_dependence when the average
  384. number of instructions in a basic block is very large.
  385. Studies have shown that there is typically around 5 instructions between
  386. branches for typical C code. So we can make a guess that the average
  387. basic block is approximately 5 instructions long; we will choose 100X
  388. the average size as a very large basic block.
  389. Each insn has associated bitmaps for its dependencies. Each bitmap
  390. has enough entries to represent a dependency on any other insn in
  391. the insn chain. All bitmap for true dependencies cache is
  392. allocated then the rest two ones are also allocated. */
  393. static bitmap_head *true_dependency_cache = NULL;
  394. static bitmap_head *output_dependency_cache = NULL;
  395. static bitmap_head *anti_dependency_cache = NULL;
  396. static bitmap_head *control_dependency_cache = NULL;
  397. static bitmap_head *spec_dependency_cache = NULL;
  398. static int cache_size;
  399. /* True if we should mark added dependencies as a non-register deps. */
  400. static bool mark_as_hard;
  401. static int deps_may_trap_p (const_rtx);
  402. static void add_dependence_1 (rtx_insn *, rtx_insn *, enum reg_note);
  403. static void add_dependence_list (rtx_insn *, rtx_insn_list *, int,
  404. enum reg_note, bool);
  405. static void add_dependence_list_and_free (struct deps_desc *, rtx_insn *,
  406. rtx_insn_list **, int, enum reg_note,
  407. bool);
  408. static void delete_all_dependences (rtx);
  409. static void chain_to_prev_insn (rtx_insn *);
  410. static void flush_pending_lists (struct deps_desc *, rtx_insn *, int, int);
  411. static void sched_analyze_1 (struct deps_desc *, rtx, rtx_insn *);
  412. static void sched_analyze_2 (struct deps_desc *, rtx, rtx_insn *);
  413. static void sched_analyze_insn (struct deps_desc *, rtx, rtx_insn *);
  414. static bool sched_has_condition_p (const rtx_insn *);
  415. static int conditions_mutex_p (const_rtx, const_rtx, bool, bool);
  416. static enum DEPS_ADJUST_RESULT maybe_add_or_update_dep_1 (dep_t, bool,
  417. rtx, rtx);
  418. static enum DEPS_ADJUST_RESULT add_or_update_dep_1 (dep_t, bool, rtx, rtx);
  419. #ifdef ENABLE_CHECKING
  420. static void check_dep (dep_t, bool);
  421. #endif
  422. /* Return nonzero if a load of the memory reference MEM can cause a trap. */
  423. static int
  424. deps_may_trap_p (const_rtx mem)
  425. {
  426. const_rtx addr = XEXP (mem, 0);
  427. if (REG_P (addr) && REGNO (addr) >= FIRST_PSEUDO_REGISTER)
  428. {
  429. const_rtx t = get_reg_known_value (REGNO (addr));
  430. if (t)
  431. addr = t;
  432. }
  433. return rtx_addr_can_trap_p (addr);
  434. }
  435. /* Find the condition under which INSN is executed. If REV is not NULL,
  436. it is set to TRUE when the returned comparison should be reversed
  437. to get the actual condition. */
  438. static rtx
  439. sched_get_condition_with_rev_uncached (const rtx_insn *insn, bool *rev)
  440. {
  441. rtx pat = PATTERN (insn);
  442. rtx src;
  443. if (rev)
  444. *rev = false;
  445. if (GET_CODE (pat) == COND_EXEC)
  446. return COND_EXEC_TEST (pat);
  447. if (!any_condjump_p (insn) || !onlyjump_p (insn))
  448. return 0;
  449. src = SET_SRC (pc_set (insn));
  450. if (XEXP (src, 2) == pc_rtx)
  451. return XEXP (src, 0);
  452. else if (XEXP (src, 1) == pc_rtx)
  453. {
  454. rtx cond = XEXP (src, 0);
  455. enum rtx_code revcode = reversed_comparison_code (cond, insn);
  456. if (revcode == UNKNOWN)
  457. return 0;
  458. if (rev)
  459. *rev = true;
  460. return cond;
  461. }
  462. return 0;
  463. }
  464. /* Return the condition under which INSN does not execute (i.e. the
  465. not-taken condition for a conditional branch), or NULL if we cannot
  466. find such a condition. The caller should make a copy of the condition
  467. before using it. */
  468. rtx
  469. sched_get_reverse_condition_uncached (const rtx_insn *insn)
  470. {
  471. bool rev;
  472. rtx cond = sched_get_condition_with_rev_uncached (insn, &rev);
  473. if (cond == NULL_RTX)
  474. return cond;
  475. if (!rev)
  476. {
  477. enum rtx_code revcode = reversed_comparison_code (cond, insn);
  478. cond = gen_rtx_fmt_ee (revcode, GET_MODE (cond),
  479. XEXP (cond, 0),
  480. XEXP (cond, 1));
  481. }
  482. return cond;
  483. }
  484. /* Caching variant of sched_get_condition_with_rev_uncached.
  485. We only do actual work the first time we come here for an insn; the
  486. results are cached in INSN_CACHED_COND and INSN_REVERSE_COND. */
  487. static rtx
  488. sched_get_condition_with_rev (const rtx_insn *insn, bool *rev)
  489. {
  490. bool tmp;
  491. if (INSN_LUID (insn) == 0)
  492. return sched_get_condition_with_rev_uncached (insn, rev);
  493. if (INSN_CACHED_COND (insn) == const_true_rtx)
  494. return NULL_RTX;
  495. if (INSN_CACHED_COND (insn) != NULL_RTX)
  496. {
  497. if (rev)
  498. *rev = INSN_REVERSE_COND (insn);
  499. return INSN_CACHED_COND (insn);
  500. }
  501. INSN_CACHED_COND (insn) = sched_get_condition_with_rev_uncached (insn, &tmp);
  502. INSN_REVERSE_COND (insn) = tmp;
  503. if (INSN_CACHED_COND (insn) == NULL_RTX)
  504. {
  505. INSN_CACHED_COND (insn) = const_true_rtx;
  506. return NULL_RTX;
  507. }
  508. if (rev)
  509. *rev = INSN_REVERSE_COND (insn);
  510. return INSN_CACHED_COND (insn);
  511. }
  512. /* True when we can find a condition under which INSN is executed. */
  513. static bool
  514. sched_has_condition_p (const rtx_insn *insn)
  515. {
  516. return !! sched_get_condition_with_rev (insn, NULL);
  517. }
  518. /* Return nonzero if conditions COND1 and COND2 can never be both true. */
  519. static int
  520. conditions_mutex_p (const_rtx cond1, const_rtx cond2, bool rev1, bool rev2)
  521. {
  522. if (COMPARISON_P (cond1)
  523. && COMPARISON_P (cond2)
  524. && GET_CODE (cond1) ==
  525. (rev1==rev2
  526. ? reversed_comparison_code (cond2, NULL)
  527. : GET_CODE (cond2))
  528. && rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
  529. && XEXP (cond1, 1) == XEXP (cond2, 1))
  530. return 1;
  531. return 0;
  532. }
  533. /* Return true if insn1 and insn2 can never depend on one another because
  534. the conditions under which they are executed are mutually exclusive. */
  535. bool
  536. sched_insns_conditions_mutex_p (const rtx_insn *insn1, const rtx_insn *insn2)
  537. {
  538. rtx cond1, cond2;
  539. bool rev1 = false, rev2 = false;
  540. /* df doesn't handle conditional lifetimes entirely correctly;
  541. calls mess up the conditional lifetimes. */
  542. if (!CALL_P (insn1) && !CALL_P (insn2))
  543. {
  544. cond1 = sched_get_condition_with_rev (insn1, &rev1);
  545. cond2 = sched_get_condition_with_rev (insn2, &rev2);
  546. if (cond1 && cond2
  547. && conditions_mutex_p (cond1, cond2, rev1, rev2)
  548. /* Make sure first instruction doesn't affect condition of second
  549. instruction if switched. */
  550. && !modified_in_p (cond1, insn2)
  551. /* Make sure second instruction doesn't affect condition of first
  552. instruction if switched. */
  553. && !modified_in_p (cond2, insn1))
  554. return true;
  555. }
  556. return false;
  557. }
  558. /* Return true if INSN can potentially be speculated with type DS. */
  559. bool
  560. sched_insn_is_legitimate_for_speculation_p (const rtx_insn *insn, ds_t ds)
  561. {
  562. if (HAS_INTERNAL_DEP (insn))
  563. return false;
  564. if (!NONJUMP_INSN_P (insn))
  565. return false;
  566. if (SCHED_GROUP_P (insn))
  567. return false;
  568. if (IS_SPECULATION_CHECK_P (CONST_CAST_RTX_INSN (insn)))
  569. return false;
  570. if (side_effects_p (PATTERN (insn)))
  571. return false;
  572. if (ds & BE_IN_SPEC)
  573. /* The following instructions, which depend on a speculatively scheduled
  574. instruction, cannot be speculatively scheduled along. */
  575. {
  576. if (may_trap_or_fault_p (PATTERN (insn)))
  577. /* If instruction might fault, it cannot be speculatively scheduled.
  578. For control speculation it's obvious why and for data speculation
  579. it's because the insn might get wrong input if speculation
  580. wasn't successful. */
  581. return false;
  582. if ((ds & BE_IN_DATA)
  583. && sched_has_condition_p (insn))
  584. /* If this is a predicated instruction, then it cannot be
  585. speculatively scheduled. See PR35659. */
  586. return false;
  587. }
  588. return true;
  589. }
  590. /* Initialize LIST_PTR to point to one of the lists present in TYPES_PTR,
  591. initialize RESOLVED_P_PTR with true if that list consists of resolved deps,
  592. and remove the type of returned [through LIST_PTR] list from TYPES_PTR.
  593. This function is used to switch sd_iterator to the next list.
  594. !!! For internal use only. Might consider moving it to sched-int.h. */
  595. void
  596. sd_next_list (const_rtx insn, sd_list_types_def *types_ptr,
  597. deps_list_t *list_ptr, bool *resolved_p_ptr)
  598. {
  599. sd_list_types_def types = *types_ptr;
  600. if (types & SD_LIST_HARD_BACK)
  601. {
  602. *list_ptr = INSN_HARD_BACK_DEPS (insn);
  603. *resolved_p_ptr = false;
  604. *types_ptr = types & ~SD_LIST_HARD_BACK;
  605. }
  606. else if (types & SD_LIST_SPEC_BACK)
  607. {
  608. *list_ptr = INSN_SPEC_BACK_DEPS (insn);
  609. *resolved_p_ptr = false;
  610. *types_ptr = types & ~SD_LIST_SPEC_BACK;
  611. }
  612. else if (types & SD_LIST_FORW)
  613. {
  614. *list_ptr = INSN_FORW_DEPS (insn);
  615. *resolved_p_ptr = false;
  616. *types_ptr = types & ~SD_LIST_FORW;
  617. }
  618. else if (types & SD_LIST_RES_BACK)
  619. {
  620. *list_ptr = INSN_RESOLVED_BACK_DEPS (insn);
  621. *resolved_p_ptr = true;
  622. *types_ptr = types & ~SD_LIST_RES_BACK;
  623. }
  624. else if (types & SD_LIST_RES_FORW)
  625. {
  626. *list_ptr = INSN_RESOLVED_FORW_DEPS (insn);
  627. *resolved_p_ptr = true;
  628. *types_ptr = types & ~SD_LIST_RES_FORW;
  629. }
  630. else
  631. {
  632. *list_ptr = NULL;
  633. *resolved_p_ptr = false;
  634. *types_ptr = SD_LIST_NONE;
  635. }
  636. }
  637. /* Return the summary size of INSN's lists defined by LIST_TYPES. */
  638. int
  639. sd_lists_size (const_rtx insn, sd_list_types_def list_types)
  640. {
  641. int size = 0;
  642. while (list_types != SD_LIST_NONE)
  643. {
  644. deps_list_t list;
  645. bool resolved_p;
  646. sd_next_list (insn, &list_types, &list, &resolved_p);
  647. if (list)
  648. size += DEPS_LIST_N_LINKS (list);
  649. }
  650. return size;
  651. }
  652. /* Return true if INSN's lists defined by LIST_TYPES are all empty. */
  653. bool
  654. sd_lists_empty_p (const_rtx insn, sd_list_types_def list_types)
  655. {
  656. while (list_types != SD_LIST_NONE)
  657. {
  658. deps_list_t list;
  659. bool resolved_p;
  660. sd_next_list (insn, &list_types, &list, &resolved_p);
  661. if (!deps_list_empty_p (list))
  662. return false;
  663. }
  664. return true;
  665. }
  666. /* Initialize data for INSN. */
  667. void
  668. sd_init_insn (rtx insn)
  669. {
  670. INSN_HARD_BACK_DEPS (insn) = create_deps_list ();
  671. INSN_SPEC_BACK_DEPS (insn) = create_deps_list ();
  672. INSN_RESOLVED_BACK_DEPS (insn) = create_deps_list ();
  673. INSN_FORW_DEPS (insn) = create_deps_list ();
  674. INSN_RESOLVED_FORW_DEPS (insn) = create_deps_list ();
  675. /* ??? It would be nice to allocate dependency caches here. */
  676. }
  677. /* Free data for INSN. */
  678. void
  679. sd_finish_insn (rtx insn)
  680. {
  681. /* ??? It would be nice to deallocate dependency caches here. */
  682. free_deps_list (INSN_HARD_BACK_DEPS (insn));
  683. INSN_HARD_BACK_DEPS (insn) = NULL;
  684. free_deps_list (INSN_SPEC_BACK_DEPS (insn));
  685. INSN_SPEC_BACK_DEPS (insn) = NULL;
  686. free_deps_list (INSN_RESOLVED_BACK_DEPS (insn));
  687. INSN_RESOLVED_BACK_DEPS (insn) = NULL;
  688. free_deps_list (INSN_FORW_DEPS (insn));
  689. INSN_FORW_DEPS (insn) = NULL;
  690. free_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
  691. INSN_RESOLVED_FORW_DEPS (insn) = NULL;
  692. }
  693. /* Find a dependency between producer PRO and consumer CON.
  694. Search through resolved dependency lists if RESOLVED_P is true.
  695. If no such dependency is found return NULL,
  696. otherwise return the dependency and initialize SD_IT_PTR [if it is nonnull]
  697. with an iterator pointing to it. */
  698. static dep_t
  699. sd_find_dep_between_no_cache (rtx pro, rtx con, bool resolved_p,
  700. sd_iterator_def *sd_it_ptr)
  701. {
  702. sd_list_types_def pro_list_type;
  703. sd_list_types_def con_list_type;
  704. sd_iterator_def sd_it;
  705. dep_t dep;
  706. bool found_p = false;
  707. if (resolved_p)
  708. {
  709. pro_list_type = SD_LIST_RES_FORW;
  710. con_list_type = SD_LIST_RES_BACK;
  711. }
  712. else
  713. {
  714. pro_list_type = SD_LIST_FORW;
  715. con_list_type = SD_LIST_BACK;
  716. }
  717. /* Walk through either back list of INSN or forw list of ELEM
  718. depending on which one is shorter. */
  719. if (sd_lists_size (con, con_list_type) < sd_lists_size (pro, pro_list_type))
  720. {
  721. /* Find the dep_link with producer PRO in consumer's back_deps. */
  722. FOR_EACH_DEP (con, con_list_type, sd_it, dep)
  723. if (DEP_PRO (dep) == pro)
  724. {
  725. found_p = true;
  726. break;
  727. }
  728. }
  729. else
  730. {
  731. /* Find the dep_link with consumer CON in producer's forw_deps. */
  732. FOR_EACH_DEP (pro, pro_list_type, sd_it, dep)
  733. if (DEP_CON (dep) == con)
  734. {
  735. found_p = true;
  736. break;
  737. }
  738. }
  739. if (found_p)
  740. {
  741. if (sd_it_ptr != NULL)
  742. *sd_it_ptr = sd_it;
  743. return dep;
  744. }
  745. return NULL;
  746. }
  747. /* Find a dependency between producer PRO and consumer CON.
  748. Use dependency [if available] to check if dependency is present at all.
  749. Search through resolved dependency lists if RESOLVED_P is true.
  750. If the dependency or NULL if none found. */
  751. dep_t
  752. sd_find_dep_between (rtx pro, rtx con, bool resolved_p)
  753. {
  754. if (true_dependency_cache != NULL)
  755. /* Avoiding the list walk below can cut compile times dramatically
  756. for some code. */
  757. {
  758. int elem_luid = INSN_LUID (pro);
  759. int insn_luid = INSN_LUID (con);
  760. if (!bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid)
  761. && !bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid)
  762. && !bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid)
  763. && !bitmap_bit_p (&control_dependency_cache[insn_luid], elem_luid))
  764. return NULL;
  765. }
  766. return sd_find_dep_between_no_cache (pro, con, resolved_p, NULL);
  767. }
  768. /* Add or update a dependence described by DEP.
  769. MEM1 and MEM2, if non-null, correspond to memory locations in case of
  770. data speculation.
  771. The function returns a value indicating if an old entry has been changed
  772. or a new entry has been added to insn's backward deps.
  773. This function merely checks if producer and consumer is the same insn
  774. and doesn't create a dep in this case. Actual manipulation of
  775. dependence data structures is performed in add_or_update_dep_1. */
  776. static enum DEPS_ADJUST_RESULT
  777. maybe_add_or_update_dep_1 (dep_t dep, bool resolved_p, rtx mem1, rtx mem2)
  778. {
  779. rtx_insn *elem = DEP_PRO (dep);
  780. rtx_insn *insn = DEP_CON (dep);
  781. gcc_assert (INSN_P (insn) && INSN_P (elem));
  782. /* Don't depend an insn on itself. */
  783. if (insn == elem)
  784. {
  785. if (sched_deps_info->generate_spec_deps)
  786. /* INSN has an internal dependence, which we can't overcome. */
  787. HAS_INTERNAL_DEP (insn) = 1;
  788. return DEP_NODEP;
  789. }
  790. return add_or_update_dep_1 (dep, resolved_p, mem1, mem2);
  791. }
  792. /* Ask dependency caches what needs to be done for dependence DEP.
  793. Return DEP_CREATED if new dependence should be created and there is no
  794. need to try to find one searching the dependencies lists.
  795. Return DEP_PRESENT if there already is a dependence described by DEP and
  796. hence nothing is to be done.
  797. Return DEP_CHANGED if there already is a dependence, but it should be
  798. updated to incorporate additional information from DEP. */
  799. static enum DEPS_ADJUST_RESULT
  800. ask_dependency_caches (dep_t dep)
  801. {
  802. int elem_luid = INSN_LUID (DEP_PRO (dep));
  803. int insn_luid = INSN_LUID (DEP_CON (dep));
  804. gcc_assert (true_dependency_cache != NULL
  805. && output_dependency_cache != NULL
  806. && anti_dependency_cache != NULL
  807. && control_dependency_cache != NULL);
  808. if (!(current_sched_info->flags & USE_DEPS_LIST))
  809. {
  810. enum reg_note present_dep_type;
  811. if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
  812. present_dep_type = REG_DEP_TRUE;
  813. else if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
  814. present_dep_type = REG_DEP_OUTPUT;
  815. else if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
  816. present_dep_type = REG_DEP_ANTI;
  817. else if (bitmap_bit_p (&control_dependency_cache[insn_luid], elem_luid))
  818. present_dep_type = REG_DEP_CONTROL;
  819. else
  820. /* There is no existing dep so it should be created. */
  821. return DEP_CREATED;
  822. if ((int) DEP_TYPE (dep) >= (int) present_dep_type)
  823. /* DEP does not add anything to the existing dependence. */
  824. return DEP_PRESENT;
  825. }
  826. else
  827. {
  828. ds_t present_dep_types = 0;
  829. if (bitmap_bit_p (&true_dependency_cache[insn_luid], elem_luid))
  830. present_dep_types |= DEP_TRUE;
  831. if (bitmap_bit_p (&output_dependency_cache[insn_luid], elem_luid))
  832. present_dep_types |= DEP_OUTPUT;
  833. if (bitmap_bit_p (&anti_dependency_cache[insn_luid], elem_luid))
  834. present_dep_types |= DEP_ANTI;
  835. if (bitmap_bit_p (&control_dependency_cache[insn_luid], elem_luid))
  836. present_dep_types |= DEP_CONTROL;
  837. if (present_dep_types == 0)
  838. /* There is no existing dep so it should be created. */
  839. return DEP_CREATED;
  840. if (!(current_sched_info->flags & DO_SPECULATION)
  841. || !bitmap_bit_p (&spec_dependency_cache[insn_luid], elem_luid))
  842. {
  843. if ((present_dep_types | (DEP_STATUS (dep) & DEP_TYPES))
  844. == present_dep_types)
  845. /* DEP does not add anything to the existing dependence. */
  846. return DEP_PRESENT;
  847. }
  848. else
  849. {
  850. /* Only true dependencies can be data speculative and
  851. only anti dependencies can be control speculative. */
  852. gcc_assert ((present_dep_types & (DEP_TRUE | DEP_ANTI))
  853. == present_dep_types);
  854. /* if (DEP is SPECULATIVE) then
  855. ..we should update DEP_STATUS
  856. else
  857. ..we should reset existing dep to non-speculative. */
  858. }
  859. }
  860. return DEP_CHANGED;
  861. }
  862. /* Set dependency caches according to DEP. */
  863. static void
  864. set_dependency_caches (dep_t dep)
  865. {
  866. int elem_luid = INSN_LUID (DEP_PRO (dep));
  867. int insn_luid = INSN_LUID (DEP_CON (dep));
  868. if (!(current_sched_info->flags & USE_DEPS_LIST))
  869. {
  870. switch (DEP_TYPE (dep))
  871. {
  872. case REG_DEP_TRUE:
  873. bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
  874. break;
  875. case REG_DEP_OUTPUT:
  876. bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
  877. break;
  878. case REG_DEP_ANTI:
  879. bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
  880. break;
  881. case REG_DEP_CONTROL:
  882. bitmap_set_bit (&control_dependency_cache[insn_luid], elem_luid);
  883. break;
  884. default:
  885. gcc_unreachable ();
  886. }
  887. }
  888. else
  889. {
  890. ds_t ds = DEP_STATUS (dep);
  891. if (ds & DEP_TRUE)
  892. bitmap_set_bit (&true_dependency_cache[insn_luid], elem_luid);
  893. if (ds & DEP_OUTPUT)
  894. bitmap_set_bit (&output_dependency_cache[insn_luid], elem_luid);
  895. if (ds & DEP_ANTI)
  896. bitmap_set_bit (&anti_dependency_cache[insn_luid], elem_luid);
  897. if (ds & DEP_CONTROL)
  898. bitmap_set_bit (&control_dependency_cache[insn_luid], elem_luid);
  899. if (ds & SPECULATIVE)
  900. {
  901. gcc_assert (current_sched_info->flags & DO_SPECULATION);
  902. bitmap_set_bit (&spec_dependency_cache[insn_luid], elem_luid);
  903. }
  904. }
  905. }
  906. /* Type of dependence DEP have changed from OLD_TYPE. Update dependency
  907. caches accordingly. */
  908. static void
  909. update_dependency_caches (dep_t dep, enum reg_note old_type)
  910. {
  911. int elem_luid = INSN_LUID (DEP_PRO (dep));
  912. int insn_luid = INSN_LUID (DEP_CON (dep));
  913. /* Clear corresponding cache entry because type of the link
  914. may have changed. Keep them if we use_deps_list. */
  915. if (!(current_sched_info->flags & USE_DEPS_LIST))
  916. {
  917. switch (old_type)
  918. {
  919. case REG_DEP_OUTPUT:
  920. bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
  921. break;
  922. case REG_DEP_ANTI:
  923. bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
  924. break;
  925. case REG_DEP_CONTROL:
  926. bitmap_clear_bit (&control_dependency_cache[insn_luid], elem_luid);
  927. break;
  928. default:
  929. gcc_unreachable ();
  930. }
  931. }
  932. set_dependency_caches (dep);
  933. }
  934. /* Convert a dependence pointed to by SD_IT to be non-speculative. */
  935. static void
  936. change_spec_dep_to_hard (sd_iterator_def sd_it)
  937. {
  938. dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
  939. dep_link_t link = DEP_NODE_BACK (node);
  940. dep_t dep = DEP_NODE_DEP (node);
  941. rtx_insn *elem = DEP_PRO (dep);
  942. rtx_insn *insn = DEP_CON (dep);
  943. move_dep_link (link, INSN_SPEC_BACK_DEPS (insn), INSN_HARD_BACK_DEPS (insn));
  944. DEP_STATUS (dep) &= ~SPECULATIVE;
  945. if (true_dependency_cache != NULL)
  946. /* Clear the cache entry. */
  947. bitmap_clear_bit (&spec_dependency_cache[INSN_LUID (insn)],
  948. INSN_LUID (elem));
  949. }
  950. /* Update DEP to incorporate information from NEW_DEP.
  951. SD_IT points to DEP in case it should be moved to another list.
  952. MEM1 and MEM2, if nonnull, correspond to memory locations in case if
  953. data-speculative dependence should be updated. */
  954. static enum DEPS_ADJUST_RESULT
  955. update_dep (dep_t dep, dep_t new_dep,
  956. sd_iterator_def sd_it ATTRIBUTE_UNUSED,
  957. rtx mem1 ATTRIBUTE_UNUSED,
  958. rtx mem2 ATTRIBUTE_UNUSED)
  959. {
  960. enum DEPS_ADJUST_RESULT res = DEP_PRESENT;
  961. enum reg_note old_type = DEP_TYPE (dep);
  962. bool was_spec = dep_spec_p (dep);
  963. DEP_NONREG (dep) |= DEP_NONREG (new_dep);
  964. DEP_MULTIPLE (dep) = 1;
  965. /* If this is a more restrictive type of dependence than the
  966. existing one, then change the existing dependence to this
  967. type. */
  968. if ((int) DEP_TYPE (new_dep) < (int) old_type)
  969. {
  970. DEP_TYPE (dep) = DEP_TYPE (new_dep);
  971. res = DEP_CHANGED;
  972. }
  973. if (current_sched_info->flags & USE_DEPS_LIST)
  974. /* Update DEP_STATUS. */
  975. {
  976. ds_t dep_status = DEP_STATUS (dep);
  977. ds_t ds = DEP_STATUS (new_dep);
  978. ds_t new_status = ds | dep_status;
  979. if (new_status & SPECULATIVE)
  980. {
  981. /* Either existing dep or a dep we're adding or both are
  982. speculative. */
  983. if (!(ds & SPECULATIVE)
  984. || !(dep_status & SPECULATIVE))
  985. /* The new dep can't be speculative. */
  986. new_status &= ~SPECULATIVE;
  987. else
  988. {
  989. /* Both are speculative. Merge probabilities. */
  990. if (mem1 != NULL)
  991. {
  992. dw_t dw;
  993. dw = estimate_dep_weak (mem1, mem2);
  994. ds = set_dep_weak (ds, BEGIN_DATA, dw);
  995. }
  996. new_status = ds_merge (dep_status, ds);
  997. }
  998. }
  999. ds = new_status;
  1000. if (dep_status != ds)
  1001. {
  1002. DEP_STATUS (dep) = ds;
  1003. res = DEP_CHANGED;
  1004. }
  1005. }
  1006. if (was_spec && !dep_spec_p (dep))
  1007. /* The old dep was speculative, but now it isn't. */
  1008. change_spec_dep_to_hard (sd_it);
  1009. if (true_dependency_cache != NULL
  1010. && res == DEP_CHANGED)
  1011. update_dependency_caches (dep, old_type);
  1012. return res;
  1013. }
  1014. /* Add or update a dependence described by DEP.
  1015. MEM1 and MEM2, if non-null, correspond to memory locations in case of
  1016. data speculation.
  1017. The function returns a value indicating if an old entry has been changed
  1018. or a new entry has been added to insn's backward deps or nothing has
  1019. been updated at all. */
  1020. static enum DEPS_ADJUST_RESULT
  1021. add_or_update_dep_1 (dep_t new_dep, bool resolved_p,
  1022. rtx mem1 ATTRIBUTE_UNUSED, rtx mem2 ATTRIBUTE_UNUSED)
  1023. {
  1024. bool maybe_present_p = true;
  1025. bool present_p = false;
  1026. gcc_assert (INSN_P (DEP_PRO (new_dep)) && INSN_P (DEP_CON (new_dep))
  1027. && DEP_PRO (new_dep) != DEP_CON (new_dep));
  1028. #ifdef ENABLE_CHECKING
  1029. check_dep (new_dep, mem1 != NULL);
  1030. #endif
  1031. if (true_dependency_cache != NULL)
  1032. {
  1033. switch (ask_dependency_caches (new_dep))
  1034. {
  1035. case DEP_PRESENT:
  1036. dep_t present_dep;
  1037. sd_iterator_def sd_it;
  1038. present_dep = sd_find_dep_between_no_cache (DEP_PRO (new_dep),
  1039. DEP_CON (new_dep),
  1040. resolved_p, &sd_it);
  1041. DEP_MULTIPLE (present_dep) = 1;
  1042. return DEP_PRESENT;
  1043. case DEP_CHANGED:
  1044. maybe_present_p = true;
  1045. present_p = true;
  1046. break;
  1047. case DEP_CREATED:
  1048. maybe_present_p = false;
  1049. present_p = false;
  1050. break;
  1051. default:
  1052. gcc_unreachable ();
  1053. break;
  1054. }
  1055. }
  1056. /* Check that we don't already have this dependence. */
  1057. if (maybe_present_p)
  1058. {
  1059. dep_t present_dep;
  1060. sd_iterator_def sd_it;
  1061. gcc_assert (true_dependency_cache == NULL || present_p);
  1062. present_dep = sd_find_dep_between_no_cache (DEP_PRO (new_dep),
  1063. DEP_CON (new_dep),
  1064. resolved_p, &sd_it);
  1065. if (present_dep != NULL)
  1066. /* We found an existing dependency between ELEM and INSN. */
  1067. return update_dep (present_dep, new_dep, sd_it, mem1, mem2);
  1068. else
  1069. /* We didn't find a dep, it shouldn't present in the cache. */
  1070. gcc_assert (!present_p);
  1071. }
  1072. /* Might want to check one level of transitivity to save conses.
  1073. This check should be done in maybe_add_or_update_dep_1.
  1074. Since we made it to add_or_update_dep_1, we must create
  1075. (or update) a link. */
  1076. if (mem1 != NULL_RTX)
  1077. {
  1078. gcc_assert (sched_deps_info->generate_spec_deps);
  1079. DEP_STATUS (new_dep) = set_dep_weak (DEP_STATUS (new_dep), BEGIN_DATA,
  1080. estimate_dep_weak (mem1, mem2));
  1081. }
  1082. sd_add_dep (new_dep, resolved_p);
  1083. return DEP_CREATED;
  1084. }
  1085. /* Initialize BACK_LIST_PTR with consumer's backward list and
  1086. FORW_LIST_PTR with producer's forward list. If RESOLVED_P is true
  1087. initialize with lists that hold resolved deps. */
  1088. static void
  1089. get_back_and_forw_lists (dep_t dep, bool resolved_p,
  1090. deps_list_t *back_list_ptr,
  1091. deps_list_t *forw_list_ptr)
  1092. {
  1093. rtx_insn *con = DEP_CON (dep);
  1094. if (!resolved_p)
  1095. {
  1096. if (dep_spec_p (dep))
  1097. *back_list_ptr = INSN_SPEC_BACK_DEPS (con);
  1098. else
  1099. *back_list_ptr = INSN_HARD_BACK_DEPS (con);
  1100. *forw_list_ptr = INSN_FORW_DEPS (DEP_PRO (dep));
  1101. }
  1102. else
  1103. {
  1104. *back_list_ptr = INSN_RESOLVED_BACK_DEPS (con);
  1105. *forw_list_ptr = INSN_RESOLVED_FORW_DEPS (DEP_PRO (dep));
  1106. }
  1107. }
  1108. /* Add dependence described by DEP.
  1109. If RESOLVED_P is true treat the dependence as a resolved one. */
  1110. void
  1111. sd_add_dep (dep_t dep, bool resolved_p)
  1112. {
  1113. dep_node_t n = create_dep_node ();
  1114. deps_list_t con_back_deps;
  1115. deps_list_t pro_forw_deps;
  1116. rtx_insn *elem = DEP_PRO (dep);
  1117. rtx_insn *insn = DEP_CON (dep);
  1118. gcc_assert (INSN_P (insn) && INSN_P (elem) && insn != elem);
  1119. if ((current_sched_info->flags & DO_SPECULATION) == 0
  1120. || !sched_insn_is_legitimate_for_speculation_p (insn, DEP_STATUS (dep)))
  1121. DEP_STATUS (dep) &= ~SPECULATIVE;
  1122. copy_dep (DEP_NODE_DEP (n), dep);
  1123. get_back_and_forw_lists (dep, resolved_p, &con_back_deps, &pro_forw_deps);
  1124. add_to_deps_list (DEP_NODE_BACK (n), con_back_deps);
  1125. #ifdef ENABLE_CHECKING
  1126. check_dep (dep, false);
  1127. #endif
  1128. add_to_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
  1129. /* If we are adding a dependency to INSN's LOG_LINKs, then note that
  1130. in the bitmap caches of dependency information. */
  1131. if (true_dependency_cache != NULL)
  1132. set_dependency_caches (dep);
  1133. }
  1134. /* Add or update backward dependence between INSN and ELEM
  1135. with given type DEP_TYPE and dep_status DS.
  1136. This function is a convenience wrapper. */
  1137. enum DEPS_ADJUST_RESULT
  1138. sd_add_or_update_dep (dep_t dep, bool resolved_p)
  1139. {
  1140. return add_or_update_dep_1 (dep, resolved_p, NULL_RTX, NULL_RTX);
  1141. }
  1142. /* Resolved dependence pointed to by SD_IT.
  1143. SD_IT will advance to the next element. */
  1144. void
  1145. sd_resolve_dep (sd_iterator_def sd_it)
  1146. {
  1147. dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
  1148. dep_t dep = DEP_NODE_DEP (node);
  1149. rtx_insn *pro = DEP_PRO (dep);
  1150. rtx_insn *con = DEP_CON (dep);
  1151. if (dep_spec_p (dep))
  1152. move_dep_link (DEP_NODE_BACK (node), INSN_SPEC_BACK_DEPS (con),
  1153. INSN_RESOLVED_BACK_DEPS (con));
  1154. else
  1155. move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
  1156. INSN_RESOLVED_BACK_DEPS (con));
  1157. move_dep_link (DEP_NODE_FORW (node), INSN_FORW_DEPS (pro),
  1158. INSN_RESOLVED_FORW_DEPS (pro));
  1159. }
  1160. /* Perform the inverse operation of sd_resolve_dep. Restore the dependence
  1161. pointed to by SD_IT to unresolved state. */
  1162. void
  1163. sd_unresolve_dep (sd_iterator_def sd_it)
  1164. {
  1165. dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
  1166. dep_t dep = DEP_NODE_DEP (node);
  1167. rtx_insn *pro = DEP_PRO (dep);
  1168. rtx_insn *con = DEP_CON (dep);
  1169. if (dep_spec_p (dep))
  1170. move_dep_link (DEP_NODE_BACK (node), INSN_RESOLVED_BACK_DEPS (con),
  1171. INSN_SPEC_BACK_DEPS (con));
  1172. else
  1173. move_dep_link (DEP_NODE_BACK (node), INSN_RESOLVED_BACK_DEPS (con),
  1174. INSN_HARD_BACK_DEPS (con));
  1175. move_dep_link (DEP_NODE_FORW (node), INSN_RESOLVED_FORW_DEPS (pro),
  1176. INSN_FORW_DEPS (pro));
  1177. }
  1178. /* Make TO depend on all the FROM's producers.
  1179. If RESOLVED_P is true add dependencies to the resolved lists. */
  1180. void
  1181. sd_copy_back_deps (rtx_insn *to, rtx_insn *from, bool resolved_p)
  1182. {
  1183. sd_list_types_def list_type;
  1184. sd_iterator_def sd_it;
  1185. dep_t dep;
  1186. list_type = resolved_p ? SD_LIST_RES_BACK : SD_LIST_BACK;
  1187. FOR_EACH_DEP (from, list_type, sd_it, dep)
  1188. {
  1189. dep_def _new_dep, *new_dep = &_new_dep;
  1190. copy_dep (new_dep, dep);
  1191. DEP_CON (new_dep) = to;
  1192. sd_add_dep (new_dep, resolved_p);
  1193. }
  1194. }
  1195. /* Remove a dependency referred to by SD_IT.
  1196. SD_IT will point to the next dependence after removal. */
  1197. void
  1198. sd_delete_dep (sd_iterator_def sd_it)
  1199. {
  1200. dep_node_t n = DEP_LINK_NODE (*sd_it.linkp);
  1201. dep_t dep = DEP_NODE_DEP (n);
  1202. rtx_insn *pro = DEP_PRO (dep);
  1203. rtx_insn *con = DEP_CON (dep);
  1204. deps_list_t con_back_deps;
  1205. deps_list_t pro_forw_deps;
  1206. if (true_dependency_cache != NULL)
  1207. {
  1208. int elem_luid = INSN_LUID (pro);
  1209. int insn_luid = INSN_LUID (con);
  1210. bitmap_clear_bit (&true_dependency_cache[insn_luid], elem_luid);
  1211. bitmap_clear_bit (&anti_dependency_cache[insn_luid], elem_luid);
  1212. bitmap_clear_bit (&control_dependency_cache[insn_luid], elem_luid);
  1213. bitmap_clear_bit (&output_dependency_cache[insn_luid], elem_luid);
  1214. if (current_sched_info->flags & DO_SPECULATION)
  1215. bitmap_clear_bit (&spec_dependency_cache[insn_luid], elem_luid);
  1216. }
  1217. get_back_and_forw_lists (dep, sd_it.resolved_p,
  1218. &con_back_deps, &pro_forw_deps);
  1219. remove_from_deps_list (DEP_NODE_BACK (n), con_back_deps);
  1220. remove_from_deps_list (DEP_NODE_FORW (n), pro_forw_deps);
  1221. delete_dep_node (n);
  1222. }
  1223. /* Dump size of the lists. */
  1224. #define DUMP_LISTS_SIZE (2)
  1225. /* Dump dependencies of the lists. */
  1226. #define DUMP_LISTS_DEPS (4)
  1227. /* Dump all information about the lists. */
  1228. #define DUMP_LISTS_ALL (DUMP_LISTS_SIZE | DUMP_LISTS_DEPS)
  1229. /* Dump deps_lists of INSN specified by TYPES to DUMP.
  1230. FLAGS is a bit mask specifying what information about the lists needs
  1231. to be printed.
  1232. If FLAGS has the very first bit set, then dump all information about
  1233. the lists and propagate this bit into the callee dump functions. */
  1234. static void
  1235. dump_lists (FILE *dump, rtx insn, sd_list_types_def types, int flags)
  1236. {
  1237. sd_iterator_def sd_it;
  1238. dep_t dep;
  1239. int all;
  1240. all = (flags & 1);
  1241. if (all)
  1242. flags |= DUMP_LISTS_ALL;
  1243. fprintf (dump, "[");
  1244. if (flags & DUMP_LISTS_SIZE)
  1245. fprintf (dump, "%d; ", sd_lists_size (insn, types));
  1246. if (flags & DUMP_LISTS_DEPS)
  1247. {
  1248. FOR_EACH_DEP (insn, types, sd_it, dep)
  1249. {
  1250. dump_dep (dump, dep, dump_dep_flags | all);
  1251. fprintf (dump, " ");
  1252. }
  1253. }
  1254. }
  1255. /* Dump all information about deps_lists of INSN specified by TYPES
  1256. to STDERR. */
  1257. void
  1258. sd_debug_lists (rtx insn, sd_list_types_def types)
  1259. {
  1260. dump_lists (stderr, insn, types, 1);
  1261. fprintf (stderr, "\n");
  1262. }
  1263. /* A wrapper around add_dependence_1, to add a dependence of CON on
  1264. PRO, with type DEP_TYPE. This function implements special handling
  1265. for REG_DEP_CONTROL dependencies. For these, we optionally promote
  1266. the type to REG_DEP_ANTI if we can determine that predication is
  1267. impossible; otherwise we add additional true dependencies on the
  1268. INSN_COND_DEPS list of the jump (which PRO must be). */
  1269. void
  1270. add_dependence (rtx_insn *con, rtx_insn *pro, enum reg_note dep_type)
  1271. {
  1272. if (dep_type == REG_DEP_CONTROL
  1273. && !(current_sched_info->flags & DO_PREDICATION))
  1274. dep_type = REG_DEP_ANTI;
  1275. /* A REG_DEP_CONTROL dependence may be eliminated through predication,
  1276. so we must also make the insn dependent on the setter of the
  1277. condition. */
  1278. if (dep_type == REG_DEP_CONTROL)
  1279. {
  1280. rtx_insn *real_pro = pro;
  1281. rtx_insn *other = real_insn_for_shadow (real_pro);
  1282. rtx cond;
  1283. if (other != NULL_RTX)
  1284. real_pro = other;
  1285. cond = sched_get_reverse_condition_uncached (real_pro);
  1286. /* Verify that the insn does not use a different value in
  1287. the condition register than the one that was present at
  1288. the jump. */
  1289. if (cond == NULL_RTX)
  1290. dep_type = REG_DEP_ANTI;
  1291. else if (INSN_CACHED_COND (real_pro) == const_true_rtx)
  1292. {
  1293. HARD_REG_SET uses;
  1294. CLEAR_HARD_REG_SET (uses);
  1295. note_uses (&PATTERN (con), record_hard_reg_uses, &uses);
  1296. if (TEST_HARD_REG_BIT (uses, REGNO (XEXP (cond, 0))))
  1297. dep_type = REG_DEP_ANTI;
  1298. }
  1299. if (dep_type == REG_DEP_CONTROL)
  1300. {
  1301. if (sched_verbose >= 5)
  1302. fprintf (sched_dump, "making DEP_CONTROL for %d\n",
  1303. INSN_UID (real_pro));
  1304. add_dependence_list (con, INSN_COND_DEPS (real_pro), 0,
  1305. REG_DEP_TRUE, false);
  1306. }
  1307. }
  1308. add_dependence_1 (con, pro, dep_type);
  1309. }
  1310. /* A convenience wrapper to operate on an entire list. HARD should be
  1311. true if DEP_NONREG should be set on newly created dependencies. */
  1312. static void
  1313. add_dependence_list (rtx_insn *insn, rtx_insn_list *list, int uncond,
  1314. enum reg_note dep_type, bool hard)
  1315. {
  1316. mark_as_hard = hard;
  1317. for (; list; list = list->next ())
  1318. {
  1319. if (uncond || ! sched_insns_conditions_mutex_p (insn, list->insn ()))
  1320. add_dependence (insn, list->insn (), dep_type);
  1321. }
  1322. mark_as_hard = false;
  1323. }
  1324. /* Similar, but free *LISTP at the same time, when the context
  1325. is not readonly. HARD should be true if DEP_NONREG should be set on
  1326. newly created dependencies. */
  1327. static void
  1328. add_dependence_list_and_free (struct deps_desc *deps, rtx_insn *insn,
  1329. rtx_insn_list **listp,
  1330. int uncond, enum reg_note dep_type, bool hard)
  1331. {
  1332. add_dependence_list (insn, *listp, uncond, dep_type, hard);
  1333. /* We don't want to short-circuit dependencies involving debug
  1334. insns, because they may cause actual dependencies to be
  1335. disregarded. */
  1336. if (deps->readonly || DEBUG_INSN_P (insn))
  1337. return;
  1338. free_INSN_LIST_list (listp);
  1339. }
  1340. /* Remove all occurrences of INSN from LIST. Return the number of
  1341. occurrences removed. */
  1342. static int
  1343. remove_from_dependence_list (rtx insn, rtx_insn_list **listp)
  1344. {
  1345. int removed = 0;
  1346. while (*listp)
  1347. {
  1348. if ((*listp)->insn () == insn)
  1349. {
  1350. remove_free_INSN_LIST_node (listp);
  1351. removed++;
  1352. continue;
  1353. }
  1354. listp = (rtx_insn_list **)&XEXP (*listp, 1);
  1355. }
  1356. return removed;
  1357. }
  1358. /* Same as above, but process two lists at once. */
  1359. static int
  1360. remove_from_both_dependence_lists (rtx insn,
  1361. rtx_insn_list **listp,
  1362. rtx_expr_list **exprp)
  1363. {
  1364. int removed = 0;
  1365. while (*listp)
  1366. {
  1367. if (XEXP (*listp, 0) == insn)
  1368. {
  1369. remove_free_INSN_LIST_node (listp);
  1370. remove_free_EXPR_LIST_node (exprp);
  1371. removed++;
  1372. continue;
  1373. }
  1374. listp = (rtx_insn_list **)&XEXP (*listp, 1);
  1375. exprp = (rtx_expr_list **)&XEXP (*exprp, 1);
  1376. }
  1377. return removed;
  1378. }
  1379. /* Clear all dependencies for an insn. */
  1380. static void
  1381. delete_all_dependences (rtx insn)
  1382. {
  1383. sd_iterator_def sd_it;
  1384. dep_t dep;
  1385. /* The below cycle can be optimized to clear the caches and back_deps
  1386. in one call but that would provoke duplication of code from
  1387. delete_dep (). */
  1388. for (sd_it = sd_iterator_start (insn, SD_LIST_BACK);
  1389. sd_iterator_cond (&sd_it, &dep);)
  1390. sd_delete_dep (sd_it);
  1391. }
  1392. /* All insns in a scheduling group except the first should only have
  1393. dependencies on the previous insn in the group. So we find the
  1394. first instruction in the scheduling group by walking the dependence
  1395. chains backwards. Then we add the dependencies for the group to
  1396. the previous nonnote insn. */
  1397. static void
  1398. chain_to_prev_insn (rtx_insn *insn)
  1399. {
  1400. sd_iterator_def sd_it;
  1401. dep_t dep;
  1402. rtx_insn *prev_nonnote;
  1403. FOR_EACH_DEP (insn, SD_LIST_BACK, sd_it, dep)
  1404. {
  1405. rtx_insn *i = insn;
  1406. rtx_insn *pro = DEP_PRO (dep);
  1407. do
  1408. {
  1409. i = prev_nonnote_insn (i);
  1410. if (pro == i)
  1411. goto next_link;
  1412. } while (SCHED_GROUP_P (i) || DEBUG_INSN_P (i));
  1413. if (! sched_insns_conditions_mutex_p (i, pro))
  1414. add_dependence (i, pro, DEP_TYPE (dep));
  1415. next_link:;
  1416. }
  1417. delete_all_dependences (insn);
  1418. prev_nonnote = prev_nonnote_nondebug_insn (insn);
  1419. if (BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (prev_nonnote)
  1420. && ! sched_insns_conditions_mutex_p (insn, prev_nonnote))
  1421. add_dependence (insn, prev_nonnote, REG_DEP_ANTI);
  1422. }
  1423. /* Process an insn's memory dependencies. There are four kinds of
  1424. dependencies:
  1425. (0) read dependence: read follows read
  1426. (1) true dependence: read follows write
  1427. (2) output dependence: write follows write
  1428. (3) anti dependence: write follows read
  1429. We are careful to build only dependencies which actually exist, and
  1430. use transitivity to avoid building too many links. */
  1431. /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
  1432. The MEM is a memory reference contained within INSN, which we are saving
  1433. so that we can do memory aliasing on it. */
  1434. static void
  1435. add_insn_mem_dependence (struct deps_desc *deps, bool read_p,
  1436. rtx_insn *insn, rtx mem)
  1437. {
  1438. rtx_insn_list **insn_list;
  1439. rtx_insn_list *insn_node;
  1440. rtx_expr_list **mem_list;
  1441. rtx_expr_list *mem_node;
  1442. gcc_assert (!deps->readonly);
  1443. if (read_p)
  1444. {
  1445. insn_list = &deps->pending_read_insns;
  1446. mem_list = &deps->pending_read_mems;
  1447. if (!DEBUG_INSN_P (insn))
  1448. deps->pending_read_list_length++;
  1449. }
  1450. else
  1451. {
  1452. insn_list = &deps->pending_write_insns;
  1453. mem_list = &deps->pending_write_mems;
  1454. deps->pending_write_list_length++;
  1455. }
  1456. insn_node = alloc_INSN_LIST (insn, *insn_list);
  1457. *insn_list = insn_node;
  1458. if (sched_deps_info->use_cselib)
  1459. {
  1460. mem = shallow_copy_rtx (mem);
  1461. XEXP (mem, 0) = cselib_subst_to_values_from_insn (XEXP (mem, 0),
  1462. GET_MODE (mem), insn);
  1463. }
  1464. mem_node = alloc_EXPR_LIST (VOIDmode, canon_rtx (mem), *mem_list);
  1465. *mem_list = mem_node;
  1466. }
  1467. /* Make a dependency between every memory reference on the pending lists
  1468. and INSN, thus flushing the pending lists. FOR_READ is true if emitting
  1469. dependencies for a read operation, similarly with FOR_WRITE. */
  1470. static void
  1471. flush_pending_lists (struct deps_desc *deps, rtx_insn *insn, int for_read,
  1472. int for_write)
  1473. {
  1474. if (for_write)
  1475. {
  1476. add_dependence_list_and_free (deps, insn, &deps->pending_read_insns,
  1477. 1, REG_DEP_ANTI, true);
  1478. if (!deps->readonly)
  1479. {
  1480. free_EXPR_LIST_list (&deps->pending_read_mems);
  1481. deps->pending_read_list_length = 0;
  1482. }
  1483. }
  1484. add_dependence_list_and_free (deps, insn, &deps->pending_write_insns, 1,
  1485. for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT,
  1486. true);
  1487. add_dependence_list_and_free (deps, insn,
  1488. &deps->last_pending_memory_flush, 1,
  1489. for_read ? REG_DEP_ANTI : REG_DEP_OUTPUT,
  1490. true);
  1491. add_dependence_list_and_free (deps, insn, &deps->pending_jump_insns, 1,
  1492. REG_DEP_ANTI, true);
  1493. if (DEBUG_INSN_P (insn))
  1494. {
  1495. if (for_write)
  1496. free_INSN_LIST_list (&deps->pending_read_insns);
  1497. free_INSN_LIST_list (&deps->pending_write_insns);
  1498. free_INSN_LIST_list (&deps->last_pending_memory_flush);
  1499. free_INSN_LIST_list (&deps->pending_jump_insns);
  1500. }
  1501. if (!deps->readonly)
  1502. {
  1503. free_EXPR_LIST_list (&deps->pending_write_mems);
  1504. deps->pending_write_list_length = 0;
  1505. deps->last_pending_memory_flush = alloc_INSN_LIST (insn, NULL_RTX);
  1506. deps->pending_flush_length = 1;
  1507. }
  1508. mark_as_hard = false;
  1509. }
  1510. /* Instruction which dependencies we are analyzing. */
  1511. static rtx_insn *cur_insn = NULL;
  1512. /* Implement hooks for haifa scheduler. */
  1513. static void
  1514. haifa_start_insn (rtx_insn *insn)
  1515. {
  1516. gcc_assert (insn && !cur_insn);
  1517. cur_insn = insn;
  1518. }
  1519. static void
  1520. haifa_finish_insn (void)
  1521. {
  1522. cur_insn = NULL;
  1523. }
  1524. void
  1525. haifa_note_reg_set (int regno)
  1526. {
  1527. SET_REGNO_REG_SET (reg_pending_sets, regno);
  1528. }
  1529. void
  1530. haifa_note_reg_clobber (int regno)
  1531. {
  1532. SET_REGNO_REG_SET (reg_pending_clobbers, regno);
  1533. }
  1534. void
  1535. haifa_note_reg_use (int regno)
  1536. {
  1537. SET_REGNO_REG_SET (reg_pending_uses, regno);
  1538. }
  1539. static void
  1540. haifa_note_mem_dep (rtx mem, rtx pending_mem, rtx_insn *pending_insn, ds_t ds)
  1541. {
  1542. if (!(ds & SPECULATIVE))
  1543. {
  1544. mem = NULL_RTX;
  1545. pending_mem = NULL_RTX;
  1546. }
  1547. else
  1548. gcc_assert (ds & BEGIN_DATA);
  1549. {
  1550. dep_def _dep, *dep = &_dep;
  1551. init_dep_1 (dep, pending_insn, cur_insn, ds_to_dt (ds),
  1552. current_sched_info->flags & USE_DEPS_LIST ? ds : 0);
  1553. DEP_NONREG (dep) = 1;
  1554. maybe_add_or_update_dep_1 (dep, false, pending_mem, mem);
  1555. }
  1556. }
  1557. static void
  1558. haifa_note_dep (rtx_insn *elem, ds_t ds)
  1559. {
  1560. dep_def _dep;
  1561. dep_t dep = &_dep;
  1562. init_dep (dep, elem, cur_insn, ds_to_dt (ds));
  1563. if (mark_as_hard)
  1564. DEP_NONREG (dep) = 1;
  1565. maybe_add_or_update_dep_1 (dep, false, NULL_RTX, NULL_RTX);
  1566. }
  1567. static void
  1568. note_reg_use (int r)
  1569. {
  1570. if (sched_deps_info->note_reg_use)
  1571. sched_deps_info->note_reg_use (r);
  1572. }
  1573. static void
  1574. note_reg_set (int r)
  1575. {
  1576. if (sched_deps_info->note_reg_set)
  1577. sched_deps_info->note_reg_set (r);
  1578. }
  1579. static void
  1580. note_reg_clobber (int r)
  1581. {
  1582. if (sched_deps_info->note_reg_clobber)
  1583. sched_deps_info->note_reg_clobber (r);
  1584. }
  1585. static void
  1586. note_mem_dep (rtx m1, rtx m2, rtx_insn *e, ds_t ds)
  1587. {
  1588. if (sched_deps_info->note_mem_dep)
  1589. sched_deps_info->note_mem_dep (m1, m2, e, ds);
  1590. }
  1591. static void
  1592. note_dep (rtx_insn *e, ds_t ds)
  1593. {
  1594. if (sched_deps_info->note_dep)
  1595. sched_deps_info->note_dep (e, ds);
  1596. }
  1597. /* Return corresponding to DS reg_note. */
  1598. enum reg_note
  1599. ds_to_dt (ds_t ds)
  1600. {
  1601. if (ds & DEP_TRUE)
  1602. return REG_DEP_TRUE;
  1603. else if (ds & DEP_OUTPUT)
  1604. return REG_DEP_OUTPUT;
  1605. else if (ds & DEP_ANTI)
  1606. return REG_DEP_ANTI;
  1607. else
  1608. {
  1609. gcc_assert (ds & DEP_CONTROL);
  1610. return REG_DEP_CONTROL;
  1611. }
  1612. }
  1613. /* Functions for computation of info needed for register pressure
  1614. sensitive insn scheduling. */
  1615. /* Allocate and return reg_use_data structure for REGNO and INSN. */
  1616. static struct reg_use_data *
  1617. create_insn_reg_use (int regno, rtx_insn *insn)
  1618. {
  1619. struct reg_use_data *use;
  1620. use = (struct reg_use_data *) xmalloc (sizeof (struct reg_use_data));
  1621. use->regno = regno;
  1622. use->insn = insn;
  1623. use->next_insn_use = INSN_REG_USE_LIST (insn);
  1624. INSN_REG_USE_LIST (insn) = use;
  1625. return use;
  1626. }
  1627. /* Allocate reg_set_data structure for REGNO and INSN. */
  1628. static void
  1629. create_insn_reg_set (int regno, rtx insn)
  1630. {
  1631. struct reg_set_data *set;
  1632. set = (struct reg_set_data *) xmalloc (sizeof (struct reg_set_data));
  1633. set->regno = regno;
  1634. set->insn = insn;
  1635. set->next_insn_set = INSN_REG_SET_LIST (insn);
  1636. INSN_REG_SET_LIST (insn) = set;
  1637. }
  1638. /* Set up insn register uses for INSN and dependency context DEPS. */
  1639. static void
  1640. setup_insn_reg_uses (struct deps_desc *deps, rtx_insn *insn)
  1641. {
  1642. unsigned i;
  1643. reg_set_iterator rsi;
  1644. struct reg_use_data *use, *use2, *next;
  1645. struct deps_reg *reg_last;
  1646. EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
  1647. {
  1648. if (i < FIRST_PSEUDO_REGISTER
  1649. && TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
  1650. continue;
  1651. if (find_regno_note (insn, REG_DEAD, i) == NULL_RTX
  1652. && ! REGNO_REG_SET_P (reg_pending_sets, i)
  1653. && ! REGNO_REG_SET_P (reg_pending_clobbers, i))
  1654. /* Ignore use which is not dying. */
  1655. continue;
  1656. use = create_insn_reg_use (i, insn);
  1657. use->next_regno_use = use;
  1658. reg_last = &deps->reg_last[i];
  1659. /* Create the cycle list of uses. */
  1660. for (rtx_insn_list *list = reg_last->uses; list; list = list->next ())
  1661. {
  1662. use2 = create_insn_reg_use (i, list->insn ());
  1663. next = use->next_regno_use;
  1664. use->next_regno_use = use2;
  1665. use2->next_regno_use = next;
  1666. }
  1667. }
  1668. }
  1669. /* Register pressure info for the currently processed insn. */
  1670. static struct reg_pressure_data reg_pressure_info[N_REG_CLASSES];
  1671. /* Return TRUE if INSN has the use structure for REGNO. */
  1672. static bool
  1673. insn_use_p (rtx insn, int regno)
  1674. {
  1675. struct reg_use_data *use;
  1676. for (use = INSN_REG_USE_LIST (insn); use != NULL; use = use->next_insn_use)
  1677. if (use->regno == regno)
  1678. return true;
  1679. return false;
  1680. }
  1681. /* Update the register pressure info after birth of pseudo register REGNO
  1682. in INSN. Arguments CLOBBER_P and UNUSED_P say correspondingly that
  1683. the register is in clobber or unused after the insn. */
  1684. static void
  1685. mark_insn_pseudo_birth (rtx insn, int regno, bool clobber_p, bool unused_p)
  1686. {
  1687. int incr, new_incr;
  1688. enum reg_class cl;
  1689. gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
  1690. cl = sched_regno_pressure_class[regno];
  1691. if (cl != NO_REGS)
  1692. {
  1693. incr = ira_reg_class_max_nregs[cl][PSEUDO_REGNO_MODE (regno)];
  1694. if (clobber_p)
  1695. {
  1696. new_incr = reg_pressure_info[cl].clobber_increase + incr;
  1697. reg_pressure_info[cl].clobber_increase = new_incr;
  1698. }
  1699. else if (unused_p)
  1700. {
  1701. new_incr = reg_pressure_info[cl].unused_set_increase + incr;
  1702. reg_pressure_info[cl].unused_set_increase = new_incr;
  1703. }
  1704. else
  1705. {
  1706. new_incr = reg_pressure_info[cl].set_increase + incr;
  1707. reg_pressure_info[cl].set_increase = new_incr;
  1708. if (! insn_use_p (insn, regno))
  1709. reg_pressure_info[cl].change += incr;
  1710. create_insn_reg_set (regno, insn);
  1711. }
  1712. gcc_assert (new_incr < (1 << INCREASE_BITS));
  1713. }
  1714. }
  1715. /* Like mark_insn_pseudo_regno_birth except that NREGS saying how many
  1716. hard registers involved in the birth. */
  1717. static void
  1718. mark_insn_hard_regno_birth (rtx insn, int regno, int nregs,
  1719. bool clobber_p, bool unused_p)
  1720. {
  1721. enum reg_class cl;
  1722. int new_incr, last = regno + nregs;
  1723. while (regno < last)
  1724. {
  1725. gcc_assert (regno < FIRST_PSEUDO_REGISTER);
  1726. if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
  1727. {
  1728. cl = sched_regno_pressure_class[regno];
  1729. if (cl != NO_REGS)
  1730. {
  1731. if (clobber_p)
  1732. {
  1733. new_incr = reg_pressure_info[cl].clobber_increase + 1;
  1734. reg_pressure_info[cl].clobber_increase = new_incr;
  1735. }
  1736. else if (unused_p)
  1737. {
  1738. new_incr = reg_pressure_info[cl].unused_set_increase + 1;
  1739. reg_pressure_info[cl].unused_set_increase = new_incr;
  1740. }
  1741. else
  1742. {
  1743. new_incr = reg_pressure_info[cl].set_increase + 1;
  1744. reg_pressure_info[cl].set_increase = new_incr;
  1745. if (! insn_use_p (insn, regno))
  1746. reg_pressure_info[cl].change += 1;
  1747. create_insn_reg_set (regno, insn);
  1748. }
  1749. gcc_assert (new_incr < (1 << INCREASE_BITS));
  1750. }
  1751. }
  1752. regno++;
  1753. }
  1754. }
  1755. /* Update the register pressure info after birth of pseudo or hard
  1756. register REG in INSN. Arguments CLOBBER_P and UNUSED_P say
  1757. correspondingly that the register is in clobber or unused after the
  1758. insn. */
  1759. static void
  1760. mark_insn_reg_birth (rtx insn, rtx reg, bool clobber_p, bool unused_p)
  1761. {
  1762. int regno;
  1763. if (GET_CODE (reg) == SUBREG)
  1764. reg = SUBREG_REG (reg);
  1765. if (! REG_P (reg))
  1766. return;
  1767. regno = REGNO (reg);
  1768. if (regno < FIRST_PSEUDO_REGISTER)
  1769. mark_insn_hard_regno_birth (insn, regno,
  1770. hard_regno_nregs[regno][GET_MODE (reg)],
  1771. clobber_p, unused_p);
  1772. else
  1773. mark_insn_pseudo_birth (insn, regno, clobber_p, unused_p);
  1774. }
  1775. /* Update the register pressure info after death of pseudo register
  1776. REGNO. */
  1777. static void
  1778. mark_pseudo_death (int regno)
  1779. {
  1780. int incr;
  1781. enum reg_class cl;
  1782. gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
  1783. cl = sched_regno_pressure_class[regno];
  1784. if (cl != NO_REGS)
  1785. {
  1786. incr = ira_reg_class_max_nregs[cl][PSEUDO_REGNO_MODE (regno)];
  1787. reg_pressure_info[cl].change -= incr;
  1788. }
  1789. }
  1790. /* Like mark_pseudo_death except that NREGS saying how many hard
  1791. registers involved in the death. */
  1792. static void
  1793. mark_hard_regno_death (int regno, int nregs)
  1794. {
  1795. enum reg_class cl;
  1796. int last = regno + nregs;
  1797. while (regno < last)
  1798. {
  1799. gcc_assert (regno < FIRST_PSEUDO_REGISTER);
  1800. if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
  1801. {
  1802. cl = sched_regno_pressure_class[regno];
  1803. if (cl != NO_REGS)
  1804. reg_pressure_info[cl].change -= 1;
  1805. }
  1806. regno++;
  1807. }
  1808. }
  1809. /* Update the register pressure info after death of pseudo or hard
  1810. register REG. */
  1811. static void
  1812. mark_reg_death (rtx reg)
  1813. {
  1814. int regno;
  1815. if (GET_CODE (reg) == SUBREG)
  1816. reg = SUBREG_REG (reg);
  1817. if (! REG_P (reg))
  1818. return;
  1819. regno = REGNO (reg);
  1820. if (regno < FIRST_PSEUDO_REGISTER)
  1821. mark_hard_regno_death (regno, hard_regno_nregs[regno][GET_MODE (reg)]);
  1822. else
  1823. mark_pseudo_death (regno);
  1824. }
  1825. /* Process SETTER of REG. DATA is an insn containing the setter. */
  1826. static void
  1827. mark_insn_reg_store (rtx reg, const_rtx setter, void *data)
  1828. {
  1829. if (setter != NULL_RTX && GET_CODE (setter) != SET)
  1830. return;
  1831. mark_insn_reg_birth
  1832. ((rtx) data, reg, false,
  1833. find_reg_note ((const_rtx) data, REG_UNUSED, reg) != NULL_RTX);
  1834. }
  1835. /* Like mark_insn_reg_store except notice just CLOBBERs; ignore SETs. */
  1836. static void
  1837. mark_insn_reg_clobber (rtx reg, const_rtx setter, void *data)
  1838. {
  1839. if (GET_CODE (setter) == CLOBBER)
  1840. mark_insn_reg_birth ((rtx) data, reg, true, false);
  1841. }
  1842. /* Set up reg pressure info related to INSN. */
  1843. void
  1844. init_insn_reg_pressure_info (rtx insn)
  1845. {
  1846. int i, len;
  1847. enum reg_class cl;
  1848. static struct reg_pressure_data *pressure_info;
  1849. rtx link;
  1850. gcc_assert (sched_pressure != SCHED_PRESSURE_NONE);
  1851. if (! INSN_P (insn))
  1852. return;
  1853. for (i = 0; i < ira_pressure_classes_num; i++)
  1854. {
  1855. cl = ira_pressure_classes[i];
  1856. reg_pressure_info[cl].clobber_increase = 0;
  1857. reg_pressure_info[cl].set_increase = 0;
  1858. reg_pressure_info[cl].unused_set_increase = 0;
  1859. reg_pressure_info[cl].change = 0;
  1860. }
  1861. note_stores (PATTERN (insn), mark_insn_reg_clobber, insn);
  1862. note_stores (PATTERN (insn), mark_insn_reg_store, insn);
  1863. #ifdef AUTO_INC_DEC
  1864. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1865. if (REG_NOTE_KIND (link) == REG_INC)
  1866. mark_insn_reg_store (XEXP (link, 0), NULL_RTX, insn);
  1867. #endif
  1868. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1869. if (REG_NOTE_KIND (link) == REG_DEAD)
  1870. mark_reg_death (XEXP (link, 0));
  1871. len = sizeof (struct reg_pressure_data) * ira_pressure_classes_num;
  1872. pressure_info
  1873. = INSN_REG_PRESSURE (insn) = (struct reg_pressure_data *) xmalloc (len);
  1874. if (sched_pressure == SCHED_PRESSURE_WEIGHTED)
  1875. INSN_MAX_REG_PRESSURE (insn) = (int *) xcalloc (ira_pressure_classes_num
  1876. * sizeof (int), 1);
  1877. for (i = 0; i < ira_pressure_classes_num; i++)
  1878. {
  1879. cl = ira_pressure_classes[i];
  1880. pressure_info[i].clobber_increase
  1881. = reg_pressure_info[cl].clobber_increase;
  1882. pressure_info[i].set_increase = reg_pressure_info[cl].set_increase;
  1883. pressure_info[i].unused_set_increase
  1884. = reg_pressure_info[cl].unused_set_increase;
  1885. pressure_info[i].change = reg_pressure_info[cl].change;
  1886. }
  1887. }
  1888. /* Internal variable for sched_analyze_[12] () functions.
  1889. If it is nonzero, this means that sched_analyze_[12] looks
  1890. at the most toplevel SET. */
  1891. static bool can_start_lhs_rhs_p;
  1892. /* Extend reg info for the deps context DEPS given that
  1893. we have just generated a register numbered REGNO. */
  1894. static void
  1895. extend_deps_reg_info (struct deps_desc *deps, int regno)
  1896. {
  1897. int max_regno = regno + 1;
  1898. gcc_assert (!reload_completed);
  1899. /* In a readonly context, it would not hurt to extend info,
  1900. but it should not be needed. */
  1901. if (reload_completed && deps->readonly)
  1902. {
  1903. deps->max_reg = max_regno;
  1904. return;
  1905. }
  1906. if (max_regno > deps->max_reg)
  1907. {
  1908. deps->reg_last = XRESIZEVEC (struct deps_reg, deps->reg_last,
  1909. max_regno);
  1910. memset (&deps->reg_last[deps->max_reg],
  1911. 0, (max_regno - deps->max_reg)
  1912. * sizeof (struct deps_reg));
  1913. deps->max_reg = max_regno;
  1914. }
  1915. }
  1916. /* Extends REG_INFO_P if needed. */
  1917. void
  1918. maybe_extend_reg_info_p (void)
  1919. {
  1920. /* Extend REG_INFO_P, if needed. */
  1921. if ((unsigned int)max_regno - 1 >= reg_info_p_size)
  1922. {
  1923. size_t new_reg_info_p_size = max_regno + 128;
  1924. gcc_assert (!reload_completed && sel_sched_p ());
  1925. reg_info_p = (struct reg_info_t *) xrecalloc (reg_info_p,
  1926. new_reg_info_p_size,
  1927. reg_info_p_size,
  1928. sizeof (*reg_info_p));
  1929. reg_info_p_size = new_reg_info_p_size;
  1930. }
  1931. }
  1932. /* Analyze a single reference to register (reg:MODE REGNO) in INSN.
  1933. The type of the reference is specified by REF and can be SET,
  1934. CLOBBER, PRE_DEC, POST_DEC, PRE_INC, POST_INC or USE. */
  1935. static void
  1936. sched_analyze_reg (struct deps_desc *deps, int regno, machine_mode mode,
  1937. enum rtx_code ref, rtx_insn *insn)
  1938. {
  1939. /* We could emit new pseudos in renaming. Extend the reg structures. */
  1940. if (!reload_completed && sel_sched_p ()
  1941. && (regno >= max_reg_num () - 1 || regno >= deps->max_reg))
  1942. extend_deps_reg_info (deps, regno);
  1943. maybe_extend_reg_info_p ();
  1944. /* A hard reg in a wide mode may really be multiple registers.
  1945. If so, mark all of them just like the first. */
  1946. if (regno < FIRST_PSEUDO_REGISTER)
  1947. {
  1948. int i = hard_regno_nregs[regno][mode];
  1949. if (ref == SET)
  1950. {
  1951. while (--i >= 0)
  1952. note_reg_set (regno + i);
  1953. }
  1954. else if (ref == USE)
  1955. {
  1956. while (--i >= 0)
  1957. note_reg_use (regno + i);
  1958. }
  1959. else
  1960. {
  1961. while (--i >= 0)
  1962. note_reg_clobber (regno + i);
  1963. }
  1964. }
  1965. /* ??? Reload sometimes emits USEs and CLOBBERs of pseudos that
  1966. it does not reload. Ignore these as they have served their
  1967. purpose already. */
  1968. else if (regno >= deps->max_reg)
  1969. {
  1970. enum rtx_code code = GET_CODE (PATTERN (insn));
  1971. gcc_assert (code == USE || code == CLOBBER);
  1972. }
  1973. else
  1974. {
  1975. if (ref == SET)
  1976. note_reg_set (regno);
  1977. else if (ref == USE)
  1978. note_reg_use (regno);
  1979. else
  1980. note_reg_clobber (regno);
  1981. /* Pseudos that are REG_EQUIV to something may be replaced
  1982. by that during reloading. We need only add dependencies for
  1983. the address in the REG_EQUIV note. */
  1984. if (!reload_completed && get_reg_known_equiv_p (regno))
  1985. {
  1986. rtx t = get_reg_known_value (regno);
  1987. if (MEM_P (t))
  1988. sched_analyze_2 (deps, XEXP (t, 0), insn);
  1989. }
  1990. /* Don't let it cross a call after scheduling if it doesn't
  1991. already cross one. */
  1992. if (REG_N_CALLS_CROSSED (regno) == 0)
  1993. {
  1994. if (!deps->readonly && ref == USE && !DEBUG_INSN_P (insn))
  1995. deps->sched_before_next_call
  1996. = alloc_INSN_LIST (insn, deps->sched_before_next_call);
  1997. else
  1998. add_dependence_list (insn, deps->last_function_call, 1,
  1999. REG_DEP_ANTI, false);
  2000. }
  2001. }
  2002. }
  2003. /* Analyze a single SET, CLOBBER, PRE_DEC, POST_DEC, PRE_INC or POST_INC
  2004. rtx, X, creating all dependencies generated by the write to the
  2005. destination of X, and reads of everything mentioned. */
  2006. static void
  2007. sched_analyze_1 (struct deps_desc *deps, rtx x, rtx_insn *insn)
  2008. {
  2009. rtx dest = XEXP (x, 0);
  2010. enum rtx_code code = GET_CODE (x);
  2011. bool cslr_p = can_start_lhs_rhs_p;
  2012. can_start_lhs_rhs_p = false;
  2013. gcc_assert (dest);
  2014. if (dest == 0)
  2015. return;
  2016. if (cslr_p && sched_deps_info->start_lhs)
  2017. sched_deps_info->start_lhs (dest);
  2018. if (GET_CODE (dest) == PARALLEL)
  2019. {
  2020. int i;
  2021. for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
  2022. if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
  2023. sched_analyze_1 (deps,
  2024. gen_rtx_CLOBBER (VOIDmode,
  2025. XEXP (XVECEXP (dest, 0, i), 0)),
  2026. insn);
  2027. if (cslr_p && sched_deps_info->finish_lhs)
  2028. sched_deps_info->finish_lhs ();
  2029. if (code == SET)
  2030. {
  2031. can_start_lhs_rhs_p = cslr_p;
  2032. sched_analyze_2 (deps, SET_SRC (x), insn);
  2033. can_start_lhs_rhs_p = false;
  2034. }
  2035. return;
  2036. }
  2037. while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
  2038. || GET_CODE (dest) == ZERO_EXTRACT)
  2039. {
  2040. if (GET_CODE (dest) == STRICT_LOW_PART
  2041. || GET_CODE (dest) == ZERO_EXTRACT
  2042. || df_read_modify_subreg_p (dest))
  2043. {
  2044. /* These both read and modify the result. We must handle
  2045. them as writes to get proper dependencies for following
  2046. instructions. We must handle them as reads to get proper
  2047. dependencies from this to previous instructions.
  2048. Thus we need to call sched_analyze_2. */
  2049. sched_analyze_2 (deps, XEXP (dest, 0), insn);
  2050. }
  2051. if (GET_CODE (dest) == ZERO_EXTRACT)
  2052. {
  2053. /* The second and third arguments are values read by this insn. */
  2054. sched_analyze_2 (deps, XEXP (dest, 1), insn);
  2055. sched_analyze_2 (deps, XEXP (dest, 2), insn);
  2056. }
  2057. dest = XEXP (dest, 0);
  2058. }
  2059. if (REG_P (dest))
  2060. {
  2061. int regno = REGNO (dest);
  2062. machine_mode mode = GET_MODE (dest);
  2063. sched_analyze_reg (deps, regno, mode, code, insn);
  2064. #ifdef STACK_REGS
  2065. /* Treat all writes to a stack register as modifying the TOS. */
  2066. if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
  2067. {
  2068. /* Avoid analyzing the same register twice. */
  2069. if (regno != FIRST_STACK_REG)
  2070. sched_analyze_reg (deps, FIRST_STACK_REG, mode, code, insn);
  2071. add_to_hard_reg_set (&implicit_reg_pending_uses, mode,
  2072. FIRST_STACK_REG);
  2073. }
  2074. #endif
  2075. }
  2076. else if (MEM_P (dest))
  2077. {
  2078. /* Writing memory. */
  2079. rtx t = dest;
  2080. if (sched_deps_info->use_cselib)
  2081. {
  2082. machine_mode address_mode = get_address_mode (dest);
  2083. t = shallow_copy_rtx (dest);
  2084. cselib_lookup_from_insn (XEXP (t, 0), address_mode, 1,
  2085. GET_MODE (t), insn);
  2086. XEXP (t, 0)
  2087. = cselib_subst_to_values_from_insn (XEXP (t, 0), GET_MODE (t),
  2088. insn);
  2089. }
  2090. t = canon_rtx (t);
  2091. /* Pending lists can't get larger with a readonly context. */
  2092. if (!deps->readonly
  2093. && ((deps->pending_read_list_length + deps->pending_write_list_length)
  2094. >= MAX_PENDING_LIST_LENGTH))
  2095. {
  2096. /* Flush all pending reads and writes to prevent the pending lists
  2097. from getting any larger. Insn scheduling runs too slowly when
  2098. these lists get long. When compiling GCC with itself,
  2099. this flush occurs 8 times for sparc, and 10 times for m88k using
  2100. the default value of 32. */
  2101. flush_pending_lists (deps, insn, false, true);
  2102. }
  2103. else
  2104. {
  2105. rtx_insn_list *pending;
  2106. rtx_expr_list *pending_mem;
  2107. pending = deps->pending_read_insns;
  2108. pending_mem = deps->pending_read_mems;
  2109. while (pending)
  2110. {
  2111. if (anti_dependence (pending_mem->element (), t)
  2112. && ! sched_insns_conditions_mutex_p (insn, pending->insn ()))
  2113. note_mem_dep (t, pending_mem->element (), pending->insn (),
  2114. DEP_ANTI);
  2115. pending = pending->next ();
  2116. pending_mem = pending_mem->next ();
  2117. }
  2118. pending = deps->pending_write_insns;
  2119. pending_mem = deps->pending_write_mems;
  2120. while (pending)
  2121. {
  2122. if (output_dependence (pending_mem->element (), t)
  2123. && ! sched_insns_conditions_mutex_p (insn, pending->insn ()))
  2124. note_mem_dep (t, pending_mem->element (),
  2125. pending->insn (),
  2126. DEP_OUTPUT);
  2127. pending = pending->next ();
  2128. pending_mem = pending_mem-> next ();
  2129. }
  2130. add_dependence_list (insn, deps->last_pending_memory_flush, 1,
  2131. REG_DEP_ANTI, true);
  2132. add_dependence_list (insn, deps->pending_jump_insns, 1,
  2133. REG_DEP_CONTROL, true);
  2134. if (!deps->readonly)
  2135. add_insn_mem_dependence (deps, false, insn, dest);
  2136. }
  2137. sched_analyze_2 (deps, XEXP (dest, 0), insn);
  2138. }
  2139. if (cslr_p && sched_deps_info->finish_lhs)
  2140. sched_deps_info->finish_lhs ();
  2141. /* Analyze reads. */
  2142. if (GET_CODE (x) == SET)
  2143. {
  2144. can_start_lhs_rhs_p = cslr_p;
  2145. sched_analyze_2 (deps, SET_SRC (x), insn);
  2146. can_start_lhs_rhs_p = false;
  2147. }
  2148. }
  2149. /* Analyze the uses of memory and registers in rtx X in INSN. */
  2150. static void
  2151. sched_analyze_2 (struct deps_desc *deps, rtx x, rtx_insn *insn)
  2152. {
  2153. int i;
  2154. int j;
  2155. enum rtx_code code;
  2156. const char *fmt;
  2157. bool cslr_p = can_start_lhs_rhs_p;
  2158. can_start_lhs_rhs_p = false;
  2159. gcc_assert (x);
  2160. if (x == 0)
  2161. return;
  2162. if (cslr_p && sched_deps_info->start_rhs)
  2163. sched_deps_info->start_rhs (x);
  2164. code = GET_CODE (x);
  2165. switch (code)
  2166. {
  2167. CASE_CONST_ANY:
  2168. case SYMBOL_REF:
  2169. case CONST:
  2170. case LABEL_REF:
  2171. /* Ignore constants. */
  2172. if (cslr_p && sched_deps_info->finish_rhs)
  2173. sched_deps_info->finish_rhs ();
  2174. return;
  2175. #ifdef HAVE_cc0
  2176. case CC0:
  2177. /* User of CC0 depends on immediately preceding insn. */
  2178. SCHED_GROUP_P (insn) = 1;
  2179. /* Don't move CC0 setter to another block (it can set up the
  2180. same flag for previous CC0 users which is safe). */
  2181. CANT_MOVE (prev_nonnote_insn (insn)) = 1;
  2182. if (cslr_p && sched_deps_info->finish_rhs)
  2183. sched_deps_info->finish_rhs ();
  2184. return;
  2185. #endif
  2186. case REG:
  2187. {
  2188. int regno = REGNO (x);
  2189. machine_mode mode = GET_MODE (x);
  2190. sched_analyze_reg (deps, regno, mode, USE, insn);
  2191. #ifdef STACK_REGS
  2192. /* Treat all reads of a stack register as modifying the TOS. */
  2193. if (regno >= FIRST_STACK_REG && regno <= LAST_STACK_REG)
  2194. {
  2195. /* Avoid analyzing the same register twice. */
  2196. if (regno != FIRST_STACK_REG)
  2197. sched_analyze_reg (deps, FIRST_STACK_REG, mode, USE, insn);
  2198. sched_analyze_reg (deps, FIRST_STACK_REG, mode, SET, insn);
  2199. }
  2200. #endif
  2201. if (cslr_p && sched_deps_info->finish_rhs)
  2202. sched_deps_info->finish_rhs ();
  2203. return;
  2204. }
  2205. case MEM:
  2206. {
  2207. /* Reading memory. */
  2208. rtx u;
  2209. rtx_insn_list *pending;
  2210. rtx_expr_list *pending_mem;
  2211. rtx t = x;
  2212. if (sched_deps_info->use_cselib)
  2213. {
  2214. machine_mode address_mode = get_address_mode (t);
  2215. t = shallow_copy_rtx (t);
  2216. cselib_lookup_from_insn (XEXP (t, 0), address_mode, 1,
  2217. GET_MODE (t), insn);
  2218. XEXP (t, 0)
  2219. = cselib_subst_to_values_from_insn (XEXP (t, 0), GET_MODE (t),
  2220. insn);
  2221. }
  2222. if (!DEBUG_INSN_P (insn))
  2223. {
  2224. t = canon_rtx (t);
  2225. pending = deps->pending_read_insns;
  2226. pending_mem = deps->pending_read_mems;
  2227. while (pending)
  2228. {
  2229. if (read_dependence (pending_mem->element (), t)
  2230. && ! sched_insns_conditions_mutex_p (insn,
  2231. pending->insn ()))
  2232. note_mem_dep (t, pending_mem->element (),
  2233. pending->insn (),
  2234. DEP_ANTI);
  2235. pending = pending->next ();
  2236. pending_mem = pending_mem->next ();
  2237. }
  2238. pending = deps->pending_write_insns;
  2239. pending_mem = deps->pending_write_mems;
  2240. while (pending)
  2241. {
  2242. if (true_dependence (pending_mem->element (), VOIDmode, t)
  2243. && ! sched_insns_conditions_mutex_p (insn,
  2244. pending->insn ()))
  2245. note_mem_dep (t, pending_mem->element (),
  2246. pending->insn (),
  2247. sched_deps_info->generate_spec_deps
  2248. ? BEGIN_DATA | DEP_TRUE : DEP_TRUE);
  2249. pending = pending->next ();
  2250. pending_mem = pending_mem->next ();
  2251. }
  2252. for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
  2253. add_dependence (insn, as_a <rtx_insn *> (XEXP (u, 0)),
  2254. REG_DEP_ANTI);
  2255. for (u = deps->pending_jump_insns; u; u = XEXP (u, 1))
  2256. if (deps_may_trap_p (x))
  2257. {
  2258. if ((sched_deps_info->generate_spec_deps)
  2259. && sel_sched_p () && (spec_info->mask & BEGIN_CONTROL))
  2260. {
  2261. ds_t ds = set_dep_weak (DEP_ANTI, BEGIN_CONTROL,
  2262. MAX_DEP_WEAK);
  2263. note_dep (as_a <rtx_insn *> (XEXP (u, 0)), ds);
  2264. }
  2265. else
  2266. add_dependence (insn, as_a <rtx_insn *> (XEXP (u, 0)),
  2267. REG_DEP_CONTROL);
  2268. }
  2269. }
  2270. /* Always add these dependencies to pending_reads, since
  2271. this insn may be followed by a write. */
  2272. if (!deps->readonly)
  2273. {
  2274. if ((deps->pending_read_list_length
  2275. + deps->pending_write_list_length)
  2276. >= MAX_PENDING_LIST_LENGTH
  2277. && !DEBUG_INSN_P (insn))
  2278. flush_pending_lists (deps, insn, true, true);
  2279. add_insn_mem_dependence (deps, true, insn, x);
  2280. }
  2281. sched_analyze_2 (deps, XEXP (x, 0), insn);
  2282. if (cslr_p && sched_deps_info->finish_rhs)
  2283. sched_deps_info->finish_rhs ();
  2284. return;
  2285. }
  2286. /* Force pending stores to memory in case a trap handler needs them. */
  2287. case TRAP_IF:
  2288. flush_pending_lists (deps, insn, true, false);
  2289. break;
  2290. case PREFETCH:
  2291. if (PREFETCH_SCHEDULE_BARRIER_P (x))
  2292. reg_pending_barrier = TRUE_BARRIER;
  2293. /* Prefetch insn contains addresses only. So if the prefetch
  2294. address has no registers, there will be no dependencies on
  2295. the prefetch insn. This is wrong with result code
  2296. correctness point of view as such prefetch can be moved below
  2297. a jump insn which usually generates MOVE_BARRIER preventing
  2298. to move insns containing registers or memories through the
  2299. barrier. It is also wrong with generated code performance
  2300. point of view as prefetch withouth dependecies will have a
  2301. tendency to be issued later instead of earlier. It is hard
  2302. to generate accurate dependencies for prefetch insns as
  2303. prefetch has only the start address but it is better to have
  2304. something than nothing. */
  2305. if (!deps->readonly)
  2306. {
  2307. rtx x = gen_rtx_MEM (Pmode, XEXP (PATTERN (insn), 0));
  2308. if (sched_deps_info->use_cselib)
  2309. cselib_lookup_from_insn (x, Pmode, true, VOIDmode, insn);
  2310. add_insn_mem_dependence (deps, true, insn, x);
  2311. }
  2312. break;
  2313. case UNSPEC_VOLATILE:
  2314. flush_pending_lists (deps, insn, true, true);
  2315. /* FALLTHRU */
  2316. case ASM_OPERANDS:
  2317. case ASM_INPUT:
  2318. {
  2319. /* Traditional and volatile asm instructions must be considered to use
  2320. and clobber all hard registers, all pseudo-registers and all of
  2321. memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
  2322. Consider for instance a volatile asm that changes the fpu rounding
  2323. mode. An insn should not be moved across this even if it only uses
  2324. pseudo-regs because it might give an incorrectly rounded result. */
  2325. if ((code != ASM_OPERANDS || MEM_VOLATILE_P (x))
  2326. && !DEBUG_INSN_P (insn))
  2327. reg_pending_barrier = TRUE_BARRIER;
  2328. /* For all ASM_OPERANDS, we must traverse the vector of input operands.
  2329. We can not just fall through here since then we would be confused
  2330. by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
  2331. traditional asms unlike their normal usage. */
  2332. if (code == ASM_OPERANDS)
  2333. {
  2334. for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
  2335. sched_analyze_2 (deps, ASM_OPERANDS_INPUT (x, j), insn);
  2336. if (cslr_p && sched_deps_info->finish_rhs)
  2337. sched_deps_info->finish_rhs ();
  2338. return;
  2339. }
  2340. break;
  2341. }
  2342. case PRE_DEC:
  2343. case POST_DEC:
  2344. case PRE_INC:
  2345. case POST_INC:
  2346. /* These both read and modify the result. We must handle them as writes
  2347. to get proper dependencies for following instructions. We must handle
  2348. them as reads to get proper dependencies from this to previous
  2349. instructions. Thus we need to pass them to both sched_analyze_1
  2350. and sched_analyze_2. We must call sched_analyze_2 first in order
  2351. to get the proper antecedent for the read. */
  2352. sched_analyze_2 (deps, XEXP (x, 0), insn);
  2353. sched_analyze_1 (deps, x, insn);
  2354. if (cslr_p && sched_deps_info->finish_rhs)
  2355. sched_deps_info->finish_rhs ();
  2356. return;
  2357. case POST_MODIFY:
  2358. case PRE_MODIFY:
  2359. /* op0 = op0 + op1 */
  2360. sched_analyze_2 (deps, XEXP (x, 0), insn);
  2361. sched_analyze_2 (deps, XEXP (x, 1), insn);
  2362. sched_analyze_1 (deps, x, insn);
  2363. if (cslr_p && sched_deps_info->finish_rhs)
  2364. sched_deps_info->finish_rhs ();
  2365. return;
  2366. default:
  2367. break;
  2368. }
  2369. /* Other cases: walk the insn. */
  2370. fmt = GET_RTX_FORMAT (code);
  2371. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2372. {
  2373. if (fmt[i] == 'e')
  2374. sched_analyze_2 (deps, XEXP (x, i), insn);
  2375. else if (fmt[i] == 'E')
  2376. for (j = 0; j < XVECLEN (x, i); j++)
  2377. sched_analyze_2 (deps, XVECEXP (x, i, j), insn);
  2378. }
  2379. if (cslr_p && sched_deps_info->finish_rhs)
  2380. sched_deps_info->finish_rhs ();
  2381. }
  2382. /* Try to group two fuseable insns together to prevent scheduler
  2383. from scheduling them apart. */
  2384. static void
  2385. sched_macro_fuse_insns (rtx_insn *insn)
  2386. {
  2387. rtx_insn *prev;
  2388. if (any_condjump_p (insn))
  2389. {
  2390. unsigned int condreg1, condreg2;
  2391. rtx cc_reg_1;
  2392. targetm.fixed_condition_code_regs (&condreg1, &condreg2);
  2393. cc_reg_1 = gen_rtx_REG (CCmode, condreg1);
  2394. prev = prev_nonnote_nondebug_insn (insn);
  2395. if (!reg_referenced_p (cc_reg_1, PATTERN (insn))
  2396. || !prev
  2397. || !modified_in_p (cc_reg_1, prev))
  2398. return;
  2399. }
  2400. else
  2401. {
  2402. rtx insn_set = single_set (insn);
  2403. prev = prev_nonnote_nondebug_insn (insn);
  2404. if (!prev
  2405. || !insn_set
  2406. || !single_set (prev))
  2407. return;
  2408. }
  2409. if (targetm.sched.macro_fusion_pair_p (prev, insn))
  2410. SCHED_GROUP_P (insn) = 1;
  2411. }
  2412. /* Analyze an INSN with pattern X to find all dependencies. */
  2413. static void
  2414. sched_analyze_insn (struct deps_desc *deps, rtx x, rtx_insn *insn)
  2415. {
  2416. RTX_CODE code = GET_CODE (x);
  2417. rtx link;
  2418. unsigned i;
  2419. reg_set_iterator rsi;
  2420. if (! reload_completed)
  2421. {
  2422. HARD_REG_SET temp;
  2423. extract_insn (insn);
  2424. preprocess_constraints (insn);
  2425. ira_implicitly_set_insn_hard_regs (&temp);
  2426. AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
  2427. IOR_HARD_REG_SET (implicit_reg_pending_clobbers, temp);
  2428. }
  2429. can_start_lhs_rhs_p = (NONJUMP_INSN_P (insn)
  2430. && code == SET);
  2431. /* Group compare and branch insns for macro-fusion. */
  2432. if (targetm.sched.macro_fusion_p
  2433. && targetm.sched.macro_fusion_p ())
  2434. sched_macro_fuse_insns (insn);
  2435. if (may_trap_p (x))
  2436. /* Avoid moving trapping instructions across function calls that might
  2437. not always return. */
  2438. add_dependence_list (insn, deps->last_function_call_may_noreturn,
  2439. 1, REG_DEP_ANTI, true);
  2440. /* We must avoid creating a situation in which two successors of the
  2441. current block have different unwind info after scheduling. If at any
  2442. point the two paths re-join this leads to incorrect unwind info. */
  2443. /* ??? There are certain situations involving a forced frame pointer in
  2444. which, with extra effort, we could fix up the unwind info at a later
  2445. CFG join. However, it seems better to notice these cases earlier
  2446. during prologue generation and avoid marking the frame pointer setup
  2447. as frame-related at all. */
  2448. if (RTX_FRAME_RELATED_P (insn))
  2449. {
  2450. /* Make sure prologue insn is scheduled before next jump. */
  2451. deps->sched_before_next_jump
  2452. = alloc_INSN_LIST (insn, deps->sched_before_next_jump);
  2453. /* Make sure epilogue insn is scheduled after preceding jumps. */
  2454. add_dependence_list (insn, deps->pending_jump_insns, 1, REG_DEP_ANTI,
  2455. true);
  2456. }
  2457. if (code == COND_EXEC)
  2458. {
  2459. sched_analyze_2 (deps, COND_EXEC_TEST (x), insn);
  2460. /* ??? Should be recording conditions so we reduce the number of
  2461. false dependencies. */
  2462. x = COND_EXEC_CODE (x);
  2463. code = GET_CODE (x);
  2464. }
  2465. if (code == SET || code == CLOBBER)
  2466. {
  2467. sched_analyze_1 (deps, x, insn);
  2468. /* Bare clobber insns are used for letting life analysis, reg-stack
  2469. and others know that a value is dead. Depend on the last call
  2470. instruction so that reg-stack won't get confused. */
  2471. if (code == CLOBBER)
  2472. add_dependence_list (insn, deps->last_function_call, 1,
  2473. REG_DEP_OUTPUT, true);
  2474. }
  2475. else if (code == PARALLEL)
  2476. {
  2477. for (i = XVECLEN (x, 0); i--;)
  2478. {
  2479. rtx sub = XVECEXP (x, 0, i);
  2480. code = GET_CODE (sub);
  2481. if (code == COND_EXEC)
  2482. {
  2483. sched_analyze_2 (deps, COND_EXEC_TEST (sub), insn);
  2484. sub = COND_EXEC_CODE (sub);
  2485. code = GET_CODE (sub);
  2486. }
  2487. if (code == SET || code == CLOBBER)
  2488. sched_analyze_1 (deps, sub, insn);
  2489. else
  2490. sched_analyze_2 (deps, sub, insn);
  2491. }
  2492. }
  2493. else
  2494. sched_analyze_2 (deps, x, insn);
  2495. /* Mark registers CLOBBERED or used by called function. */
  2496. if (CALL_P (insn))
  2497. {
  2498. for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
  2499. {
  2500. if (GET_CODE (XEXP (link, 0)) == CLOBBER)
  2501. sched_analyze_1 (deps, XEXP (link, 0), insn);
  2502. else if (GET_CODE (XEXP (link, 0)) != SET)
  2503. sched_analyze_2 (deps, XEXP (link, 0), insn);
  2504. }
  2505. /* Don't schedule anything after a tail call, tail call needs
  2506. to use at least all call-saved registers. */
  2507. if (SIBLING_CALL_P (insn))
  2508. reg_pending_barrier = TRUE_BARRIER;
  2509. else if (find_reg_note (insn, REG_SETJMP, NULL))
  2510. reg_pending_barrier = MOVE_BARRIER;
  2511. }
  2512. if (JUMP_P (insn))
  2513. {
  2514. rtx next;
  2515. next = next_nonnote_nondebug_insn (insn);
  2516. if (next && BARRIER_P (next))
  2517. reg_pending_barrier = MOVE_BARRIER;
  2518. else
  2519. {
  2520. rtx_insn_list *pending;
  2521. rtx_expr_list *pending_mem;
  2522. if (sched_deps_info->compute_jump_reg_dependencies)
  2523. {
  2524. (*sched_deps_info->compute_jump_reg_dependencies)
  2525. (insn, reg_pending_control_uses);
  2526. /* Make latency of jump equal to 0 by using anti-dependence. */
  2527. EXECUTE_IF_SET_IN_REG_SET (reg_pending_control_uses, 0, i, rsi)
  2528. {
  2529. struct deps_reg *reg_last = &deps->reg_last[i];
  2530. add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI,
  2531. false);
  2532. add_dependence_list (insn, reg_last->implicit_sets,
  2533. 0, REG_DEP_ANTI, false);
  2534. add_dependence_list (insn, reg_last->clobbers, 0,
  2535. REG_DEP_ANTI, false);
  2536. }
  2537. }
  2538. /* All memory writes and volatile reads must happen before the
  2539. jump. Non-volatile reads must happen before the jump iff
  2540. the result is needed by the above register used mask. */
  2541. pending = deps->pending_write_insns;
  2542. pending_mem = deps->pending_write_mems;
  2543. while (pending)
  2544. {
  2545. if (! sched_insns_conditions_mutex_p (insn, pending->insn ()))
  2546. add_dependence (insn, pending->insn (),
  2547. REG_DEP_OUTPUT);
  2548. pending = pending->next ();
  2549. pending_mem = pending_mem->next ();
  2550. }
  2551. pending = deps->pending_read_insns;
  2552. pending_mem = deps->pending_read_mems;
  2553. while (pending)
  2554. {
  2555. if (MEM_VOLATILE_P (pending_mem->element ())
  2556. && ! sched_insns_conditions_mutex_p (insn, pending->insn ()))
  2557. add_dependence (insn, pending->insn (),
  2558. REG_DEP_OUTPUT);
  2559. pending = pending->next ();
  2560. pending_mem = pending_mem->next ();
  2561. }
  2562. add_dependence_list (insn, deps->last_pending_memory_flush, 1,
  2563. REG_DEP_ANTI, true);
  2564. add_dependence_list (insn, deps->pending_jump_insns, 1,
  2565. REG_DEP_ANTI, true);
  2566. }
  2567. }
  2568. /* If this instruction can throw an exception, then moving it changes
  2569. where block boundaries fall. This is mighty confusing elsewhere.
  2570. Therefore, prevent such an instruction from being moved. Same for
  2571. non-jump instructions that define block boundaries.
  2572. ??? Unclear whether this is still necessary in EBB mode. If not,
  2573. add_branch_dependences should be adjusted for RGN mode instead. */
  2574. if (((CALL_P (insn) || JUMP_P (insn)) && can_throw_internal (insn))
  2575. || (NONJUMP_INSN_P (insn) && control_flow_insn_p (insn)))
  2576. reg_pending_barrier = MOVE_BARRIER;
  2577. if (sched_pressure != SCHED_PRESSURE_NONE)
  2578. {
  2579. setup_insn_reg_uses (deps, insn);
  2580. init_insn_reg_pressure_info (insn);
  2581. }
  2582. /* Add register dependencies for insn. */
  2583. if (DEBUG_INSN_P (insn))
  2584. {
  2585. rtx_insn *prev = deps->last_debug_insn;
  2586. rtx u;
  2587. if (!deps->readonly)
  2588. deps->last_debug_insn = insn;
  2589. if (prev)
  2590. add_dependence (insn, prev, REG_DEP_ANTI);
  2591. add_dependence_list (insn, deps->last_function_call, 1,
  2592. REG_DEP_ANTI, false);
  2593. if (!sel_sched_p ())
  2594. for (u = deps->last_pending_memory_flush; u; u = XEXP (u, 1))
  2595. add_dependence (insn, as_a <rtx_insn *> (XEXP (u, 0)), REG_DEP_ANTI);
  2596. EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
  2597. {
  2598. struct deps_reg *reg_last = &deps->reg_last[i];
  2599. add_dependence_list (insn, reg_last->sets, 1, REG_DEP_ANTI, false);
  2600. /* There's no point in making REG_DEP_CONTROL dependencies for
  2601. debug insns. */
  2602. add_dependence_list (insn, reg_last->clobbers, 1, REG_DEP_ANTI,
  2603. false);
  2604. if (!deps->readonly)
  2605. reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
  2606. }
  2607. CLEAR_REG_SET (reg_pending_uses);
  2608. /* Quite often, a debug insn will refer to stuff in the
  2609. previous instruction, but the reason we want this
  2610. dependency here is to make sure the scheduler doesn't
  2611. gratuitously move a debug insn ahead. This could dirty
  2612. DF flags and cause additional analysis that wouldn't have
  2613. occurred in compilation without debug insns, and such
  2614. additional analysis can modify the generated code. */
  2615. prev = PREV_INSN (insn);
  2616. if (prev && NONDEBUG_INSN_P (prev))
  2617. add_dependence (insn, prev, REG_DEP_ANTI);
  2618. }
  2619. else
  2620. {
  2621. regset_head set_or_clobbered;
  2622. EXECUTE_IF_SET_IN_REG_SET (reg_pending_uses, 0, i, rsi)
  2623. {
  2624. struct deps_reg *reg_last = &deps->reg_last[i];
  2625. add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE, false);
  2626. add_dependence_list (insn, reg_last->implicit_sets, 0, REG_DEP_ANTI,
  2627. false);
  2628. add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE,
  2629. false);
  2630. if (!deps->readonly)
  2631. {
  2632. reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
  2633. reg_last->uses_length++;
  2634. }
  2635. }
  2636. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  2637. if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i))
  2638. {
  2639. struct deps_reg *reg_last = &deps->reg_last[i];
  2640. add_dependence_list (insn, reg_last->sets, 0, REG_DEP_TRUE, false);
  2641. add_dependence_list (insn, reg_last->implicit_sets, 0,
  2642. REG_DEP_ANTI, false);
  2643. add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_TRUE,
  2644. false);
  2645. if (!deps->readonly)
  2646. {
  2647. reg_last->uses = alloc_INSN_LIST (insn, reg_last->uses);
  2648. reg_last->uses_length++;
  2649. }
  2650. }
  2651. if (targetm.sched.exposed_pipeline)
  2652. {
  2653. INIT_REG_SET (&set_or_clobbered);
  2654. bitmap_ior (&set_or_clobbered, reg_pending_clobbers,
  2655. reg_pending_sets);
  2656. EXECUTE_IF_SET_IN_REG_SET (&set_or_clobbered, 0, i, rsi)
  2657. {
  2658. struct deps_reg *reg_last = &deps->reg_last[i];
  2659. rtx list;
  2660. for (list = reg_last->uses; list; list = XEXP (list, 1))
  2661. {
  2662. rtx other = XEXP (list, 0);
  2663. if (INSN_CACHED_COND (other) != const_true_rtx
  2664. && refers_to_regno_p (i, INSN_CACHED_COND (other)))
  2665. INSN_CACHED_COND (other) = const_true_rtx;
  2666. }
  2667. }
  2668. }
  2669. /* If the current insn is conditional, we can't free any
  2670. of the lists. */
  2671. if (sched_has_condition_p (insn))
  2672. {
  2673. EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
  2674. {
  2675. struct deps_reg *reg_last = &deps->reg_last[i];
  2676. add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT,
  2677. false);
  2678. add_dependence_list (insn, reg_last->implicit_sets, 0,
  2679. REG_DEP_ANTI, false);
  2680. add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
  2681. false);
  2682. add_dependence_list (insn, reg_last->control_uses, 0,
  2683. REG_DEP_CONTROL, false);
  2684. if (!deps->readonly)
  2685. {
  2686. reg_last->clobbers
  2687. = alloc_INSN_LIST (insn, reg_last->clobbers);
  2688. reg_last->clobbers_length++;
  2689. }
  2690. }
  2691. EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
  2692. {
  2693. struct deps_reg *reg_last = &deps->reg_last[i];
  2694. add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT,
  2695. false);
  2696. add_dependence_list (insn, reg_last->implicit_sets, 0,
  2697. REG_DEP_ANTI, false);
  2698. add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_OUTPUT,
  2699. false);
  2700. add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
  2701. false);
  2702. add_dependence_list (insn, reg_last->control_uses, 0,
  2703. REG_DEP_CONTROL, false);
  2704. if (!deps->readonly)
  2705. reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
  2706. }
  2707. }
  2708. else
  2709. {
  2710. EXECUTE_IF_SET_IN_REG_SET (reg_pending_clobbers, 0, i, rsi)
  2711. {
  2712. struct deps_reg *reg_last = &deps->reg_last[i];
  2713. if (reg_last->uses_length >= MAX_PENDING_LIST_LENGTH
  2714. || reg_last->clobbers_length >= MAX_PENDING_LIST_LENGTH)
  2715. {
  2716. add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
  2717. REG_DEP_OUTPUT, false);
  2718. add_dependence_list_and_free (deps, insn,
  2719. &reg_last->implicit_sets, 0,
  2720. REG_DEP_ANTI, false);
  2721. add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
  2722. REG_DEP_ANTI, false);
  2723. add_dependence_list_and_free (deps, insn,
  2724. &reg_last->control_uses, 0,
  2725. REG_DEP_ANTI, false);
  2726. add_dependence_list_and_free (deps, insn,
  2727. &reg_last->clobbers, 0,
  2728. REG_DEP_OUTPUT, false);
  2729. if (!deps->readonly)
  2730. {
  2731. reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
  2732. reg_last->clobbers_length = 0;
  2733. reg_last->uses_length = 0;
  2734. }
  2735. }
  2736. else
  2737. {
  2738. add_dependence_list (insn, reg_last->sets, 0, REG_DEP_OUTPUT,
  2739. false);
  2740. add_dependence_list (insn, reg_last->implicit_sets, 0,
  2741. REG_DEP_ANTI, false);
  2742. add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
  2743. false);
  2744. add_dependence_list (insn, reg_last->control_uses, 0,
  2745. REG_DEP_CONTROL, false);
  2746. }
  2747. if (!deps->readonly)
  2748. {
  2749. reg_last->clobbers_length++;
  2750. reg_last->clobbers
  2751. = alloc_INSN_LIST (insn, reg_last->clobbers);
  2752. }
  2753. }
  2754. EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i, rsi)
  2755. {
  2756. struct deps_reg *reg_last = &deps->reg_last[i];
  2757. add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
  2758. REG_DEP_OUTPUT, false);
  2759. add_dependence_list_and_free (deps, insn,
  2760. &reg_last->implicit_sets,
  2761. 0, REG_DEP_ANTI, false);
  2762. add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
  2763. REG_DEP_OUTPUT, false);
  2764. add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
  2765. REG_DEP_ANTI, false);
  2766. add_dependence_list (insn, reg_last->control_uses, 0,
  2767. REG_DEP_CONTROL, false);
  2768. if (!deps->readonly)
  2769. {
  2770. reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
  2771. reg_last->uses_length = 0;
  2772. reg_last->clobbers_length = 0;
  2773. }
  2774. }
  2775. }
  2776. if (!deps->readonly)
  2777. {
  2778. EXECUTE_IF_SET_IN_REG_SET (reg_pending_control_uses, 0, i, rsi)
  2779. {
  2780. struct deps_reg *reg_last = &deps->reg_last[i];
  2781. reg_last->control_uses
  2782. = alloc_INSN_LIST (insn, reg_last->control_uses);
  2783. }
  2784. }
  2785. }
  2786. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  2787. if (TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
  2788. {
  2789. struct deps_reg *reg_last = &deps->reg_last[i];
  2790. add_dependence_list (insn, reg_last->sets, 0, REG_DEP_ANTI, false);
  2791. add_dependence_list (insn, reg_last->clobbers, 0, REG_DEP_ANTI, false);
  2792. add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI, false);
  2793. add_dependence_list (insn, reg_last->control_uses, 0, REG_DEP_ANTI,
  2794. false);
  2795. if (!deps->readonly)
  2796. reg_last->implicit_sets
  2797. = alloc_INSN_LIST (insn, reg_last->implicit_sets);
  2798. }
  2799. if (!deps->readonly)
  2800. {
  2801. IOR_REG_SET (&deps->reg_last_in_use, reg_pending_uses);
  2802. IOR_REG_SET (&deps->reg_last_in_use, reg_pending_clobbers);
  2803. IOR_REG_SET (&deps->reg_last_in_use, reg_pending_sets);
  2804. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  2805. if (TEST_HARD_REG_BIT (implicit_reg_pending_uses, i)
  2806. || TEST_HARD_REG_BIT (implicit_reg_pending_clobbers, i))
  2807. SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
  2808. /* Set up the pending barrier found. */
  2809. deps->last_reg_pending_barrier = reg_pending_barrier;
  2810. }
  2811. CLEAR_REG_SET (reg_pending_uses);
  2812. CLEAR_REG_SET (reg_pending_clobbers);
  2813. CLEAR_REG_SET (reg_pending_sets);
  2814. CLEAR_REG_SET (reg_pending_control_uses);
  2815. CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
  2816. CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
  2817. /* Add dependencies if a scheduling barrier was found. */
  2818. if (reg_pending_barrier)
  2819. {
  2820. /* In the case of barrier the most added dependencies are not
  2821. real, so we use anti-dependence here. */
  2822. if (sched_has_condition_p (insn))
  2823. {
  2824. EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
  2825. {
  2826. struct deps_reg *reg_last = &deps->reg_last[i];
  2827. add_dependence_list (insn, reg_last->uses, 0, REG_DEP_ANTI,
  2828. true);
  2829. add_dependence_list (insn, reg_last->sets, 0,
  2830. reg_pending_barrier == TRUE_BARRIER
  2831. ? REG_DEP_TRUE : REG_DEP_ANTI, true);
  2832. add_dependence_list (insn, reg_last->implicit_sets, 0,
  2833. REG_DEP_ANTI, true);
  2834. add_dependence_list (insn, reg_last->clobbers, 0,
  2835. reg_pending_barrier == TRUE_BARRIER
  2836. ? REG_DEP_TRUE : REG_DEP_ANTI, true);
  2837. }
  2838. }
  2839. else
  2840. {
  2841. EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
  2842. {
  2843. struct deps_reg *reg_last = &deps->reg_last[i];
  2844. add_dependence_list_and_free (deps, insn, &reg_last->uses, 0,
  2845. REG_DEP_ANTI, true);
  2846. add_dependence_list_and_free (deps, insn,
  2847. &reg_last->control_uses, 0,
  2848. REG_DEP_CONTROL, true);
  2849. add_dependence_list_and_free (deps, insn, &reg_last->sets, 0,
  2850. reg_pending_barrier == TRUE_BARRIER
  2851. ? REG_DEP_TRUE : REG_DEP_ANTI,
  2852. true);
  2853. add_dependence_list_and_free (deps, insn,
  2854. &reg_last->implicit_sets, 0,
  2855. REG_DEP_ANTI, true);
  2856. add_dependence_list_and_free (deps, insn, &reg_last->clobbers, 0,
  2857. reg_pending_barrier == TRUE_BARRIER
  2858. ? REG_DEP_TRUE : REG_DEP_ANTI,
  2859. true);
  2860. if (!deps->readonly)
  2861. {
  2862. reg_last->uses_length = 0;
  2863. reg_last->clobbers_length = 0;
  2864. }
  2865. }
  2866. }
  2867. if (!deps->readonly)
  2868. for (i = 0; i < (unsigned)deps->max_reg; i++)
  2869. {
  2870. struct deps_reg *reg_last = &deps->reg_last[i];
  2871. reg_last->sets = alloc_INSN_LIST (insn, reg_last->sets);
  2872. SET_REGNO_REG_SET (&deps->reg_last_in_use, i);
  2873. }
  2874. /* Don't flush pending lists on speculative checks for
  2875. selective scheduling. */
  2876. if (!sel_sched_p () || !sel_insn_is_speculation_check (insn))
  2877. flush_pending_lists (deps, insn, true, true);
  2878. reg_pending_barrier = NOT_A_BARRIER;
  2879. }
  2880. /* If a post-call group is still open, see if it should remain so.
  2881. This insn must be a simple move of a hard reg to a pseudo or
  2882. vice-versa.
  2883. We must avoid moving these insns for correctness on targets
  2884. with small register classes, and for special registers like
  2885. PIC_OFFSET_TABLE_REGNUM. For simplicity, extend this to all
  2886. hard regs for all targets. */
  2887. if (deps->in_post_call_group_p)
  2888. {
  2889. rtx tmp, set = single_set (insn);
  2890. int src_regno, dest_regno;
  2891. if (set == NULL)
  2892. {
  2893. if (DEBUG_INSN_P (insn))
  2894. /* We don't want to mark debug insns as part of the same
  2895. sched group. We know they really aren't, but if we use
  2896. debug insns to tell that a call group is over, we'll
  2897. get different code if debug insns are not there and
  2898. instructions that follow seem like they should be part
  2899. of the call group.
  2900. Also, if we did, chain_to_prev_insn would move the
  2901. deps of the debug insn to the call insn, modifying
  2902. non-debug post-dependency counts of the debug insn
  2903. dependencies and otherwise messing with the scheduling
  2904. order.
  2905. Instead, let such debug insns be scheduled freely, but
  2906. keep the call group open in case there are insns that
  2907. should be part of it afterwards. Since we grant debug
  2908. insns higher priority than even sched group insns, it
  2909. will all turn out all right. */
  2910. goto debug_dont_end_call_group;
  2911. else
  2912. goto end_call_group;
  2913. }
  2914. tmp = SET_DEST (set);
  2915. if (GET_CODE (tmp) == SUBREG)
  2916. tmp = SUBREG_REG (tmp);
  2917. if (REG_P (tmp))
  2918. dest_regno = REGNO (tmp);
  2919. else
  2920. goto end_call_group;
  2921. tmp = SET_SRC (set);
  2922. if (GET_CODE (tmp) == SUBREG)
  2923. tmp = SUBREG_REG (tmp);
  2924. if ((GET_CODE (tmp) == PLUS
  2925. || GET_CODE (tmp) == MINUS)
  2926. && REG_P (XEXP (tmp, 0))
  2927. && REGNO (XEXP (tmp, 0)) == STACK_POINTER_REGNUM
  2928. && dest_regno == STACK_POINTER_REGNUM)
  2929. src_regno = STACK_POINTER_REGNUM;
  2930. else if (REG_P (tmp))
  2931. src_regno = REGNO (tmp);
  2932. else
  2933. goto end_call_group;
  2934. if (src_regno < FIRST_PSEUDO_REGISTER
  2935. || dest_regno < FIRST_PSEUDO_REGISTER)
  2936. {
  2937. if (!deps->readonly
  2938. && deps->in_post_call_group_p == post_call_initial)
  2939. deps->in_post_call_group_p = post_call;
  2940. if (!sel_sched_p () || sched_emulate_haifa_p)
  2941. {
  2942. SCHED_GROUP_P (insn) = 1;
  2943. CANT_MOVE (insn) = 1;
  2944. }
  2945. }
  2946. else
  2947. {
  2948. end_call_group:
  2949. if (!deps->readonly)
  2950. deps->in_post_call_group_p = not_post_call;
  2951. }
  2952. }
  2953. debug_dont_end_call_group:
  2954. if ((current_sched_info->flags & DO_SPECULATION)
  2955. && !sched_insn_is_legitimate_for_speculation_p (insn, 0))
  2956. /* INSN has an internal dependency (e.g. r14 = [r14]) and thus cannot
  2957. be speculated. */
  2958. {
  2959. if (sel_sched_p ())
  2960. sel_mark_hard_insn (insn);
  2961. else
  2962. {
  2963. sd_iterator_def sd_it;
  2964. dep_t dep;
  2965. for (sd_it = sd_iterator_start (insn, SD_LIST_SPEC_BACK);
  2966. sd_iterator_cond (&sd_it, &dep);)
  2967. change_spec_dep_to_hard (sd_it);
  2968. }
  2969. }
  2970. /* We do not yet have code to adjust REG_ARGS_SIZE, therefore we must
  2971. honor their original ordering. */
  2972. if (find_reg_note (insn, REG_ARGS_SIZE, NULL))
  2973. {
  2974. if (deps->last_args_size)
  2975. add_dependence (insn, deps->last_args_size, REG_DEP_OUTPUT);
  2976. deps->last_args_size = insn;
  2977. }
  2978. }
  2979. /* Return TRUE if INSN might not always return normally (e.g. call exit,
  2980. longjmp, loop forever, ...). */
  2981. /* FIXME: Why can't this function just use flags_from_decl_or_type and
  2982. test for ECF_NORETURN? */
  2983. static bool
  2984. call_may_noreturn_p (rtx insn)
  2985. {
  2986. rtx call;
  2987. /* const or pure calls that aren't looping will always return. */
  2988. if (RTL_CONST_OR_PURE_CALL_P (insn)
  2989. && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn))
  2990. return false;
  2991. call = get_call_rtx_from (insn);
  2992. if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
  2993. {
  2994. rtx symbol = XEXP (XEXP (call, 0), 0);
  2995. if (SYMBOL_REF_DECL (symbol)
  2996. && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
  2997. {
  2998. if (DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
  2999. == BUILT_IN_NORMAL)
  3000. switch (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol)))
  3001. {
  3002. case BUILT_IN_BCMP:
  3003. case BUILT_IN_BCOPY:
  3004. case BUILT_IN_BZERO:
  3005. case BUILT_IN_INDEX:
  3006. case BUILT_IN_MEMCHR:
  3007. case BUILT_IN_MEMCMP:
  3008. case BUILT_IN_MEMCPY:
  3009. case BUILT_IN_MEMMOVE:
  3010. case BUILT_IN_MEMPCPY:
  3011. case BUILT_IN_MEMSET:
  3012. case BUILT_IN_RINDEX:
  3013. case BUILT_IN_STPCPY:
  3014. case BUILT_IN_STPNCPY:
  3015. case BUILT_IN_STRCAT:
  3016. case BUILT_IN_STRCHR:
  3017. case BUILT_IN_STRCMP:
  3018. case BUILT_IN_STRCPY:
  3019. case BUILT_IN_STRCSPN:
  3020. case BUILT_IN_STRLEN:
  3021. case BUILT_IN_STRNCAT:
  3022. case BUILT_IN_STRNCMP:
  3023. case BUILT_IN_STRNCPY:
  3024. case BUILT_IN_STRPBRK:
  3025. case BUILT_IN_STRRCHR:
  3026. case BUILT_IN_STRSPN:
  3027. case BUILT_IN_STRSTR:
  3028. /* Assume certain string/memory builtins always return. */
  3029. return false;
  3030. default:
  3031. break;
  3032. }
  3033. }
  3034. }
  3035. /* For all other calls assume that they might not always return. */
  3036. return true;
  3037. }
  3038. /* Return true if INSN should be made dependent on the previous instruction
  3039. group, and if all INSN's dependencies should be moved to the first
  3040. instruction of that group. */
  3041. static bool
  3042. chain_to_prev_insn_p (rtx insn)
  3043. {
  3044. rtx prev, x;
  3045. /* INSN forms a group with the previous instruction. */
  3046. if (SCHED_GROUP_P (insn))
  3047. return true;
  3048. /* If the previous instruction clobbers a register R and this one sets
  3049. part of R, the clobber was added specifically to help us track the
  3050. liveness of R. There's no point scheduling the clobber and leaving
  3051. INSN behind, especially if we move the clobber to another block. */
  3052. prev = prev_nonnote_nondebug_insn (insn);
  3053. if (prev
  3054. && INSN_P (prev)
  3055. && BLOCK_FOR_INSN (prev) == BLOCK_FOR_INSN (insn)
  3056. && GET_CODE (PATTERN (prev)) == CLOBBER)
  3057. {
  3058. x = XEXP (PATTERN (prev), 0);
  3059. if (set_of (x, insn))
  3060. return true;
  3061. }
  3062. return false;
  3063. }
  3064. /* Analyze INSN with DEPS as a context. */
  3065. void
  3066. deps_analyze_insn (struct deps_desc *deps, rtx_insn *insn)
  3067. {
  3068. if (sched_deps_info->start_insn)
  3069. sched_deps_info->start_insn (insn);
  3070. /* Record the condition for this insn. */
  3071. if (NONDEBUG_INSN_P (insn))
  3072. {
  3073. rtx t;
  3074. sched_get_condition_with_rev (insn, NULL);
  3075. t = INSN_CACHED_COND (insn);
  3076. INSN_COND_DEPS (insn) = NULL;
  3077. if (reload_completed
  3078. && (current_sched_info->flags & DO_PREDICATION)
  3079. && COMPARISON_P (t)
  3080. && REG_P (XEXP (t, 0))
  3081. && CONSTANT_P (XEXP (t, 1)))
  3082. {
  3083. unsigned int regno;
  3084. int nregs;
  3085. rtx_insn_list *cond_deps = NULL;
  3086. t = XEXP (t, 0);
  3087. regno = REGNO (t);
  3088. nregs = hard_regno_nregs[regno][GET_MODE (t)];
  3089. while (nregs-- > 0)
  3090. {
  3091. struct deps_reg *reg_last = &deps->reg_last[regno + nregs];
  3092. cond_deps = concat_INSN_LIST (reg_last->sets, cond_deps);
  3093. cond_deps = concat_INSN_LIST (reg_last->clobbers, cond_deps);
  3094. cond_deps = concat_INSN_LIST (reg_last->implicit_sets, cond_deps);
  3095. }
  3096. INSN_COND_DEPS (insn) = cond_deps;
  3097. }
  3098. }
  3099. if (JUMP_P (insn))
  3100. {
  3101. /* Make each JUMP_INSN (but not a speculative check)
  3102. a scheduling barrier for memory references. */
  3103. if (!deps->readonly
  3104. && !(sel_sched_p ()
  3105. && sel_insn_is_speculation_check (insn)))
  3106. {
  3107. /* Keep the list a reasonable size. */
  3108. if (deps->pending_flush_length++ >= MAX_PENDING_LIST_LENGTH)
  3109. flush_pending_lists (deps, insn, true, true);
  3110. else
  3111. deps->pending_jump_insns
  3112. = alloc_INSN_LIST (insn, deps->pending_jump_insns);
  3113. }
  3114. /* For each insn which shouldn't cross a jump, add a dependence. */
  3115. add_dependence_list_and_free (deps, insn,
  3116. &deps->sched_before_next_jump, 1,
  3117. REG_DEP_ANTI, true);
  3118. sched_analyze_insn (deps, PATTERN (insn), insn);
  3119. }
  3120. else if (NONJUMP_INSN_P (insn) || DEBUG_INSN_P (insn))
  3121. {
  3122. sched_analyze_insn (deps, PATTERN (insn), insn);
  3123. }
  3124. else if (CALL_P (insn))
  3125. {
  3126. int i;
  3127. CANT_MOVE (insn) = 1;
  3128. if (find_reg_note (insn, REG_SETJMP, NULL))
  3129. {
  3130. /* This is setjmp. Assume that all registers, not just
  3131. hard registers, may be clobbered by this call. */
  3132. reg_pending_barrier = MOVE_BARRIER;
  3133. }
  3134. else
  3135. {
  3136. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  3137. /* A call may read and modify global register variables. */
  3138. if (global_regs[i])
  3139. {
  3140. SET_REGNO_REG_SET (reg_pending_sets, i);
  3141. SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
  3142. }
  3143. /* Other call-clobbered hard regs may be clobbered.
  3144. Since we only have a choice between 'might be clobbered'
  3145. and 'definitely not clobbered', we must include all
  3146. partly call-clobbered registers here. */
  3147. else if (HARD_REGNO_CALL_PART_CLOBBERED (i, reg_raw_mode[i])
  3148. || TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
  3149. SET_REGNO_REG_SET (reg_pending_clobbers, i);
  3150. /* We don't know what set of fixed registers might be used
  3151. by the function, but it is certain that the stack pointer
  3152. is among them, but be conservative. */
  3153. else if (fixed_regs[i])
  3154. SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
  3155. /* The frame pointer is normally not used by the function
  3156. itself, but by the debugger. */
  3157. /* ??? MIPS o32 is an exception. It uses the frame pointer
  3158. in the macro expansion of jal but does not represent this
  3159. fact in the call_insn rtl. */
  3160. else if (i == FRAME_POINTER_REGNUM
  3161. || (i == HARD_FRAME_POINTER_REGNUM
  3162. && (! reload_completed || frame_pointer_needed)))
  3163. SET_HARD_REG_BIT (implicit_reg_pending_uses, i);
  3164. }
  3165. /* For each insn which shouldn't cross a call, add a dependence
  3166. between that insn and this call insn. */
  3167. add_dependence_list_and_free (deps, insn,
  3168. &deps->sched_before_next_call, 1,
  3169. REG_DEP_ANTI, true);
  3170. sched_analyze_insn (deps, PATTERN (insn), insn);
  3171. /* If CALL would be in a sched group, then this will violate
  3172. convention that sched group insns have dependencies only on the
  3173. previous instruction.
  3174. Of course one can say: "Hey! What about head of the sched group?"
  3175. And I will answer: "Basic principles (one dep per insn) are always
  3176. the same." */
  3177. gcc_assert (!SCHED_GROUP_P (insn));
  3178. /* In the absence of interprocedural alias analysis, we must flush
  3179. all pending reads and writes, and start new dependencies starting
  3180. from here. But only flush writes for constant calls (which may
  3181. be passed a pointer to something we haven't written yet). */
  3182. flush_pending_lists (deps, insn, true, ! RTL_CONST_OR_PURE_CALL_P (insn));
  3183. if (!deps->readonly)
  3184. {
  3185. /* Remember the last function call for limiting lifetimes. */
  3186. free_INSN_LIST_list (&deps->last_function_call);
  3187. deps->last_function_call = alloc_INSN_LIST (insn, NULL_RTX);
  3188. if (call_may_noreturn_p (insn))
  3189. {
  3190. /* Remember the last function call that might not always return
  3191. normally for limiting moves of trapping insns. */
  3192. free_INSN_LIST_list (&deps->last_function_call_may_noreturn);
  3193. deps->last_function_call_may_noreturn
  3194. = alloc_INSN_LIST (insn, NULL_RTX);
  3195. }
  3196. /* Before reload, begin a post-call group, so as to keep the
  3197. lifetimes of hard registers correct. */
  3198. if (! reload_completed)
  3199. deps->in_post_call_group_p = post_call;
  3200. }
  3201. }
  3202. if (sched_deps_info->use_cselib)
  3203. cselib_process_insn (insn);
  3204. if (sched_deps_info->finish_insn)
  3205. sched_deps_info->finish_insn ();
  3206. /* Fixup the dependencies in the sched group. */
  3207. if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
  3208. && chain_to_prev_insn_p (insn)
  3209. && !sel_sched_p ())
  3210. chain_to_prev_insn (insn);
  3211. }
  3212. /* Initialize DEPS for the new block beginning with HEAD. */
  3213. void
  3214. deps_start_bb (struct deps_desc *deps, rtx_insn *head)
  3215. {
  3216. gcc_assert (!deps->readonly);
  3217. /* Before reload, if the previous block ended in a call, show that
  3218. we are inside a post-call group, so as to keep the lifetimes of
  3219. hard registers correct. */
  3220. if (! reload_completed && !LABEL_P (head))
  3221. {
  3222. rtx_insn *insn = prev_nonnote_nondebug_insn (head);
  3223. if (insn && CALL_P (insn))
  3224. deps->in_post_call_group_p = post_call_initial;
  3225. }
  3226. }
  3227. /* Analyze every insn between HEAD and TAIL inclusive, creating backward
  3228. dependencies for each insn. */
  3229. void
  3230. sched_analyze (struct deps_desc *deps, rtx_insn *head, rtx_insn *tail)
  3231. {
  3232. rtx_insn *insn;
  3233. if (sched_deps_info->use_cselib)
  3234. cselib_init (CSELIB_RECORD_MEMORY);
  3235. deps_start_bb (deps, head);
  3236. for (insn = head;; insn = NEXT_INSN (insn))
  3237. {
  3238. if (INSN_P (insn))
  3239. {
  3240. /* And initialize deps_lists. */
  3241. sd_init_insn (insn);
  3242. /* Clean up SCHED_GROUP_P which may be set by last
  3243. scheduler pass. */
  3244. if (SCHED_GROUP_P (insn))
  3245. SCHED_GROUP_P (insn) = 0;
  3246. }
  3247. deps_analyze_insn (deps, insn);
  3248. if (insn == tail)
  3249. {
  3250. if (sched_deps_info->use_cselib)
  3251. cselib_finish ();
  3252. return;
  3253. }
  3254. }
  3255. gcc_unreachable ();
  3256. }
  3257. /* Helper for sched_free_deps ().
  3258. Delete INSN's (RESOLVED_P) backward dependencies. */
  3259. static void
  3260. delete_dep_nodes_in_back_deps (rtx insn, bool resolved_p)
  3261. {
  3262. sd_iterator_def sd_it;
  3263. dep_t dep;
  3264. sd_list_types_def types;
  3265. if (resolved_p)
  3266. types = SD_LIST_RES_BACK;
  3267. else
  3268. types = SD_LIST_BACK;
  3269. for (sd_it = sd_iterator_start (insn, types);
  3270. sd_iterator_cond (&sd_it, &dep);)
  3271. {
  3272. dep_link_t link = *sd_it.linkp;
  3273. dep_node_t node = DEP_LINK_NODE (link);
  3274. deps_list_t back_list;
  3275. deps_list_t forw_list;
  3276. get_back_and_forw_lists (dep, resolved_p, &back_list, &forw_list);
  3277. remove_from_deps_list (link, back_list);
  3278. delete_dep_node (node);
  3279. }
  3280. }
  3281. /* Delete (RESOLVED_P) dependencies between HEAD and TAIL together with
  3282. deps_lists. */
  3283. void
  3284. sched_free_deps (rtx_insn *head, rtx_insn *tail, bool resolved_p)
  3285. {
  3286. rtx_insn *insn;
  3287. rtx_insn *next_tail = NEXT_INSN (tail);
  3288. /* We make two passes since some insns may be scheduled before their
  3289. dependencies are resolved. */
  3290. for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
  3291. if (INSN_P (insn) && INSN_LUID (insn) > 0)
  3292. {
  3293. /* Clear forward deps and leave the dep_nodes to the
  3294. corresponding back_deps list. */
  3295. if (resolved_p)
  3296. clear_deps_list (INSN_RESOLVED_FORW_DEPS (insn));
  3297. else
  3298. clear_deps_list (INSN_FORW_DEPS (insn));
  3299. }
  3300. for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
  3301. if (INSN_P (insn) && INSN_LUID (insn) > 0)
  3302. {
  3303. /* Clear resolved back deps together with its dep_nodes. */
  3304. delete_dep_nodes_in_back_deps (insn, resolved_p);
  3305. sd_finish_insn (insn);
  3306. }
  3307. }
  3308. /* Initialize variables for region data dependence analysis.
  3309. When LAZY_REG_LAST is true, do not allocate reg_last array
  3310. of struct deps_desc immediately. */
  3311. void
  3312. init_deps (struct deps_desc *deps, bool lazy_reg_last)
  3313. {
  3314. int max_reg = (reload_completed ? FIRST_PSEUDO_REGISTER : max_reg_num ());
  3315. deps->max_reg = max_reg;
  3316. if (lazy_reg_last)
  3317. deps->reg_last = NULL;
  3318. else
  3319. deps->reg_last = XCNEWVEC (struct deps_reg, max_reg);
  3320. INIT_REG_SET (&deps->reg_last_in_use);
  3321. deps->pending_read_insns = 0;
  3322. deps->pending_read_mems = 0;
  3323. deps->pending_write_insns = 0;
  3324. deps->pending_write_mems = 0;
  3325. deps->pending_jump_insns = 0;
  3326. deps->pending_read_list_length = 0;
  3327. deps->pending_write_list_length = 0;
  3328. deps->pending_flush_length = 0;
  3329. deps->last_pending_memory_flush = 0;
  3330. deps->last_function_call = 0;
  3331. deps->last_function_call_may_noreturn = 0;
  3332. deps->sched_before_next_call = 0;
  3333. deps->sched_before_next_jump = 0;
  3334. deps->in_post_call_group_p = not_post_call;
  3335. deps->last_debug_insn = 0;
  3336. deps->last_args_size = 0;
  3337. deps->last_reg_pending_barrier = NOT_A_BARRIER;
  3338. deps->readonly = 0;
  3339. }
  3340. /* Init only reg_last field of DEPS, which was not allocated before as
  3341. we inited DEPS lazily. */
  3342. void
  3343. init_deps_reg_last (struct deps_desc *deps)
  3344. {
  3345. gcc_assert (deps && deps->max_reg > 0);
  3346. gcc_assert (deps->reg_last == NULL);
  3347. deps->reg_last = XCNEWVEC (struct deps_reg, deps->max_reg);
  3348. }
  3349. /* Free insn lists found in DEPS. */
  3350. void
  3351. free_deps (struct deps_desc *deps)
  3352. {
  3353. unsigned i;
  3354. reg_set_iterator rsi;
  3355. /* We set max_reg to 0 when this context was already freed. */
  3356. if (deps->max_reg == 0)
  3357. {
  3358. gcc_assert (deps->reg_last == NULL);
  3359. return;
  3360. }
  3361. deps->max_reg = 0;
  3362. free_INSN_LIST_list (&deps->pending_read_insns);
  3363. free_EXPR_LIST_list (&deps->pending_read_mems);
  3364. free_INSN_LIST_list (&deps->pending_write_insns);
  3365. free_EXPR_LIST_list (&deps->pending_write_mems);
  3366. free_INSN_LIST_list (&deps->last_pending_memory_flush);
  3367. /* Without the EXECUTE_IF_SET, this loop is executed max_reg * nr_regions
  3368. times. For a testcase with 42000 regs and 8000 small basic blocks,
  3369. this loop accounted for nearly 60% (84 sec) of the total -O2 runtime. */
  3370. EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
  3371. {
  3372. struct deps_reg *reg_last = &deps->reg_last[i];
  3373. if (reg_last->uses)
  3374. free_INSN_LIST_list (&reg_last->uses);
  3375. if (reg_last->sets)
  3376. free_INSN_LIST_list (&reg_last->sets);
  3377. if (reg_last->implicit_sets)
  3378. free_INSN_LIST_list (&reg_last->implicit_sets);
  3379. if (reg_last->control_uses)
  3380. free_INSN_LIST_list (&reg_last->control_uses);
  3381. if (reg_last->clobbers)
  3382. free_INSN_LIST_list (&reg_last->clobbers);
  3383. }
  3384. CLEAR_REG_SET (&deps->reg_last_in_use);
  3385. /* As we initialize reg_last lazily, it is possible that we didn't allocate
  3386. it at all. */
  3387. free (deps->reg_last);
  3388. deps->reg_last = NULL;
  3389. deps = NULL;
  3390. }
  3391. /* Remove INSN from dependence contexts DEPS. */
  3392. void
  3393. remove_from_deps (struct deps_desc *deps, rtx_insn *insn)
  3394. {
  3395. int removed;
  3396. unsigned i;
  3397. reg_set_iterator rsi;
  3398. removed = remove_from_both_dependence_lists (insn, &deps->pending_read_insns,
  3399. &deps->pending_read_mems);
  3400. if (!DEBUG_INSN_P (insn))
  3401. deps->pending_read_list_length -= removed;
  3402. removed = remove_from_both_dependence_lists (insn, &deps->pending_write_insns,
  3403. &deps->pending_write_mems);
  3404. deps->pending_write_list_length -= removed;
  3405. removed = remove_from_dependence_list (insn, &deps->pending_jump_insns);
  3406. deps->pending_flush_length -= removed;
  3407. removed = remove_from_dependence_list (insn, &deps->last_pending_memory_flush);
  3408. deps->pending_flush_length -= removed;
  3409. EXECUTE_IF_SET_IN_REG_SET (&deps->reg_last_in_use, 0, i, rsi)
  3410. {
  3411. struct deps_reg *reg_last = &deps->reg_last[i];
  3412. if (reg_last->uses)
  3413. remove_from_dependence_list (insn, &reg_last->uses);
  3414. if (reg_last->sets)
  3415. remove_from_dependence_list (insn, &reg_last->sets);
  3416. if (reg_last->implicit_sets)
  3417. remove_from_dependence_list (insn, &reg_last->implicit_sets);
  3418. if (reg_last->clobbers)
  3419. remove_from_dependence_list (insn, &reg_last->clobbers);
  3420. if (!reg_last->uses && !reg_last->sets && !reg_last->implicit_sets
  3421. && !reg_last->clobbers)
  3422. CLEAR_REGNO_REG_SET (&deps->reg_last_in_use, i);
  3423. }
  3424. if (CALL_P (insn))
  3425. {
  3426. remove_from_dependence_list (insn, &deps->last_function_call);
  3427. remove_from_dependence_list (insn,
  3428. &deps->last_function_call_may_noreturn);
  3429. }
  3430. remove_from_dependence_list (insn, &deps->sched_before_next_call);
  3431. }
  3432. /* Init deps data vector. */
  3433. static void
  3434. init_deps_data_vector (void)
  3435. {
  3436. int reserve = (sched_max_luid + 1 - h_d_i_d.length ());
  3437. if (reserve > 0 && ! h_d_i_d.space (reserve))
  3438. h_d_i_d.safe_grow_cleared (3 * sched_max_luid / 2);
  3439. }
  3440. /* If it is profitable to use them, initialize or extend (depending on
  3441. GLOBAL_P) dependency data. */
  3442. void
  3443. sched_deps_init (bool global_p)
  3444. {
  3445. /* Average number of insns in the basic block.
  3446. '+ 1' is used to make it nonzero. */
  3447. int insns_in_block = sched_max_luid / n_basic_blocks_for_fn (cfun) + 1;
  3448. init_deps_data_vector ();
  3449. /* We use another caching mechanism for selective scheduling, so
  3450. we don't use this one. */
  3451. if (!sel_sched_p () && global_p && insns_in_block > 100 * 5)
  3452. {
  3453. /* ?!? We could save some memory by computing a per-region luid mapping
  3454. which could reduce both the number of vectors in the cache and the
  3455. size of each vector. Instead we just avoid the cache entirely unless
  3456. the average number of instructions in a basic block is very high. See
  3457. the comment before the declaration of true_dependency_cache for
  3458. what we consider "very high". */
  3459. cache_size = 0;
  3460. extend_dependency_caches (sched_max_luid, true);
  3461. }
  3462. if (global_p)
  3463. {
  3464. dl_pool = create_alloc_pool ("deps_list", sizeof (struct _deps_list),
  3465. /* Allocate lists for one block at a time. */
  3466. insns_in_block);
  3467. dn_pool = create_alloc_pool ("dep_node", sizeof (struct _dep_node),
  3468. /* Allocate nodes for one block at a time.
  3469. We assume that average insn has
  3470. 5 producers. */
  3471. 5 * insns_in_block);
  3472. }
  3473. }
  3474. /* Create or extend (depending on CREATE_P) dependency caches to
  3475. size N. */
  3476. void
  3477. extend_dependency_caches (int n, bool create_p)
  3478. {
  3479. if (create_p || true_dependency_cache)
  3480. {
  3481. int i, luid = cache_size + n;
  3482. true_dependency_cache = XRESIZEVEC (bitmap_head, true_dependency_cache,
  3483. luid);
  3484. output_dependency_cache = XRESIZEVEC (bitmap_head,
  3485. output_dependency_cache, luid);
  3486. anti_dependency_cache = XRESIZEVEC (bitmap_head, anti_dependency_cache,
  3487. luid);
  3488. control_dependency_cache = XRESIZEVEC (bitmap_head, control_dependency_cache,
  3489. luid);
  3490. if (current_sched_info->flags & DO_SPECULATION)
  3491. spec_dependency_cache = XRESIZEVEC (bitmap_head, spec_dependency_cache,
  3492. luid);
  3493. for (i = cache_size; i < luid; i++)
  3494. {
  3495. bitmap_initialize (&true_dependency_cache[i], 0);
  3496. bitmap_initialize (&output_dependency_cache[i], 0);
  3497. bitmap_initialize (&anti_dependency_cache[i], 0);
  3498. bitmap_initialize (&control_dependency_cache[i], 0);
  3499. if (current_sched_info->flags & DO_SPECULATION)
  3500. bitmap_initialize (&spec_dependency_cache[i], 0);
  3501. }
  3502. cache_size = luid;
  3503. }
  3504. }
  3505. /* Finalize dependency information for the whole function. */
  3506. void
  3507. sched_deps_finish (void)
  3508. {
  3509. gcc_assert (deps_pools_are_empty_p ());
  3510. free_alloc_pool_if_empty (&dn_pool);
  3511. free_alloc_pool_if_empty (&dl_pool);
  3512. gcc_assert (dn_pool == NULL && dl_pool == NULL);
  3513. h_d_i_d.release ();
  3514. cache_size = 0;
  3515. if (true_dependency_cache)
  3516. {
  3517. int i;
  3518. for (i = 0; i < cache_size; i++)
  3519. {
  3520. bitmap_clear (&true_dependency_cache[i]);
  3521. bitmap_clear (&output_dependency_cache[i]);
  3522. bitmap_clear (&anti_dependency_cache[i]);
  3523. bitmap_clear (&control_dependency_cache[i]);
  3524. if (sched_deps_info->generate_spec_deps)
  3525. bitmap_clear (&spec_dependency_cache[i]);
  3526. }
  3527. free (true_dependency_cache);
  3528. true_dependency_cache = NULL;
  3529. free (output_dependency_cache);
  3530. output_dependency_cache = NULL;
  3531. free (anti_dependency_cache);
  3532. anti_dependency_cache = NULL;
  3533. free (control_dependency_cache);
  3534. control_dependency_cache = NULL;
  3535. if (sched_deps_info->generate_spec_deps)
  3536. {
  3537. free (spec_dependency_cache);
  3538. spec_dependency_cache = NULL;
  3539. }
  3540. }
  3541. }
  3542. /* Initialize some global variables needed by the dependency analysis
  3543. code. */
  3544. void
  3545. init_deps_global (void)
  3546. {
  3547. CLEAR_HARD_REG_SET (implicit_reg_pending_clobbers);
  3548. CLEAR_HARD_REG_SET (implicit_reg_pending_uses);
  3549. reg_pending_sets = ALLOC_REG_SET (&reg_obstack);
  3550. reg_pending_clobbers = ALLOC_REG_SET (&reg_obstack);
  3551. reg_pending_uses = ALLOC_REG_SET (&reg_obstack);
  3552. reg_pending_control_uses = ALLOC_REG_SET (&reg_obstack);
  3553. reg_pending_barrier = NOT_A_BARRIER;
  3554. if (!sel_sched_p () || sched_emulate_haifa_p)
  3555. {
  3556. sched_deps_info->start_insn = haifa_start_insn;
  3557. sched_deps_info->finish_insn = haifa_finish_insn;
  3558. sched_deps_info->note_reg_set = haifa_note_reg_set;
  3559. sched_deps_info->note_reg_clobber = haifa_note_reg_clobber;
  3560. sched_deps_info->note_reg_use = haifa_note_reg_use;
  3561. sched_deps_info->note_mem_dep = haifa_note_mem_dep;
  3562. sched_deps_info->note_dep = haifa_note_dep;
  3563. }
  3564. }
  3565. /* Free everything used by the dependency analysis code. */
  3566. void
  3567. finish_deps_global (void)
  3568. {
  3569. FREE_REG_SET (reg_pending_sets);
  3570. FREE_REG_SET (reg_pending_clobbers);
  3571. FREE_REG_SET (reg_pending_uses);
  3572. FREE_REG_SET (reg_pending_control_uses);
  3573. }
  3574. /* Estimate the weakness of dependence between MEM1 and MEM2. */
  3575. dw_t
  3576. estimate_dep_weak (rtx mem1, rtx mem2)
  3577. {
  3578. rtx r1, r2;
  3579. if (mem1 == mem2)
  3580. /* MEMs are the same - don't speculate. */
  3581. return MIN_DEP_WEAK;
  3582. r1 = XEXP (mem1, 0);
  3583. r2 = XEXP (mem2, 0);
  3584. if (r1 == r2
  3585. || (REG_P (r1) && REG_P (r2)
  3586. && REGNO (r1) == REGNO (r2)))
  3587. /* Again, MEMs are the same. */
  3588. return MIN_DEP_WEAK;
  3589. else if ((REG_P (r1) && !REG_P (r2))
  3590. || (!REG_P (r1) && REG_P (r2)))
  3591. /* Different addressing modes - reason to be more speculative,
  3592. than usual. */
  3593. return NO_DEP_WEAK - (NO_DEP_WEAK - UNCERTAIN_DEP_WEAK) / 2;
  3594. else
  3595. /* We can't say anything about the dependence. */
  3596. return UNCERTAIN_DEP_WEAK;
  3597. }
  3598. /* Add or update backward dependence between INSN and ELEM with type DEP_TYPE.
  3599. This function can handle same INSN and ELEM (INSN == ELEM).
  3600. It is a convenience wrapper. */
  3601. static void
  3602. add_dependence_1 (rtx_insn *insn, rtx_insn *elem, enum reg_note dep_type)
  3603. {
  3604. ds_t ds;
  3605. bool internal;
  3606. if (dep_type == REG_DEP_TRUE)
  3607. ds = DEP_TRUE;
  3608. else if (dep_type == REG_DEP_OUTPUT)
  3609. ds = DEP_OUTPUT;
  3610. else if (dep_type == REG_DEP_CONTROL)
  3611. ds = DEP_CONTROL;
  3612. else
  3613. {
  3614. gcc_assert (dep_type == REG_DEP_ANTI);
  3615. ds = DEP_ANTI;
  3616. }
  3617. /* When add_dependence is called from inside sched-deps.c, we expect
  3618. cur_insn to be non-null. */
  3619. internal = cur_insn != NULL;
  3620. if (internal)
  3621. gcc_assert (insn == cur_insn);
  3622. else
  3623. cur_insn = insn;
  3624. note_dep (elem, ds);
  3625. if (!internal)
  3626. cur_insn = NULL;
  3627. }
  3628. /* Return weakness of speculative type TYPE in the dep_status DS,
  3629. without checking to prevent ICEs on malformed input. */
  3630. static dw_t
  3631. get_dep_weak_1 (ds_t ds, ds_t type)
  3632. {
  3633. ds = ds & type;
  3634. switch (type)
  3635. {
  3636. case BEGIN_DATA: ds >>= BEGIN_DATA_BITS_OFFSET; break;
  3637. case BE_IN_DATA: ds >>= BE_IN_DATA_BITS_OFFSET; break;
  3638. case BEGIN_CONTROL: ds >>= BEGIN_CONTROL_BITS_OFFSET; break;
  3639. case BE_IN_CONTROL: ds >>= BE_IN_CONTROL_BITS_OFFSET; break;
  3640. default: gcc_unreachable ();
  3641. }
  3642. return (dw_t) ds;
  3643. }
  3644. /* Return weakness of speculative type TYPE in the dep_status DS. */
  3645. dw_t
  3646. get_dep_weak (ds_t ds, ds_t type)
  3647. {
  3648. dw_t dw = get_dep_weak_1 (ds, type);
  3649. gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
  3650. return dw;
  3651. }
  3652. /* Return the dep_status, which has the same parameters as DS, except for
  3653. speculative type TYPE, that will have weakness DW. */
  3654. ds_t
  3655. set_dep_weak (ds_t ds, ds_t type, dw_t dw)
  3656. {
  3657. gcc_assert (MIN_DEP_WEAK <= dw && dw <= MAX_DEP_WEAK);
  3658. ds &= ~type;
  3659. switch (type)
  3660. {
  3661. case BEGIN_DATA: ds |= ((ds_t) dw) << BEGIN_DATA_BITS_OFFSET; break;
  3662. case BE_IN_DATA: ds |= ((ds_t) dw) << BE_IN_DATA_BITS_OFFSET; break;
  3663. case BEGIN_CONTROL: ds |= ((ds_t) dw) << BEGIN_CONTROL_BITS_OFFSET; break;
  3664. case BE_IN_CONTROL: ds |= ((ds_t) dw) << BE_IN_CONTROL_BITS_OFFSET; break;
  3665. default: gcc_unreachable ();
  3666. }
  3667. return ds;
  3668. }
  3669. /* Return the join of two dep_statuses DS1 and DS2.
  3670. If MAX_P is true then choose the greater probability,
  3671. otherwise multiply probabilities.
  3672. This function assumes that both DS1 and DS2 contain speculative bits. */
  3673. static ds_t
  3674. ds_merge_1 (ds_t ds1, ds_t ds2, bool max_p)
  3675. {
  3676. ds_t ds, t;
  3677. gcc_assert ((ds1 & SPECULATIVE) && (ds2 & SPECULATIVE));
  3678. ds = (ds1 & DEP_TYPES) | (ds2 & DEP_TYPES);
  3679. t = FIRST_SPEC_TYPE;
  3680. do
  3681. {
  3682. if ((ds1 & t) && !(ds2 & t))
  3683. ds |= ds1 & t;
  3684. else if (!(ds1 & t) && (ds2 & t))
  3685. ds |= ds2 & t;
  3686. else if ((ds1 & t) && (ds2 & t))
  3687. {
  3688. dw_t dw1 = get_dep_weak (ds1, t);
  3689. dw_t dw2 = get_dep_weak (ds2, t);
  3690. ds_t dw;
  3691. if (!max_p)
  3692. {
  3693. dw = ((ds_t) dw1) * ((ds_t) dw2);
  3694. dw /= MAX_DEP_WEAK;
  3695. if (dw < MIN_DEP_WEAK)
  3696. dw = MIN_DEP_WEAK;
  3697. }
  3698. else
  3699. {
  3700. if (dw1 >= dw2)
  3701. dw = dw1;
  3702. else
  3703. dw = dw2;
  3704. }
  3705. ds = set_dep_weak (ds, t, (dw_t) dw);
  3706. }
  3707. if (t == LAST_SPEC_TYPE)
  3708. break;
  3709. t <<= SPEC_TYPE_SHIFT;
  3710. }
  3711. while (1);
  3712. return ds;
  3713. }
  3714. /* Return the join of two dep_statuses DS1 and DS2.
  3715. This function assumes that both DS1 and DS2 contain speculative bits. */
  3716. ds_t
  3717. ds_merge (ds_t ds1, ds_t ds2)
  3718. {
  3719. return ds_merge_1 (ds1, ds2, false);
  3720. }
  3721. /* Return the join of two dep_statuses DS1 and DS2. */
  3722. ds_t
  3723. ds_full_merge (ds_t ds, ds_t ds2, rtx mem1, rtx mem2)
  3724. {
  3725. ds_t new_status = ds | ds2;
  3726. if (new_status & SPECULATIVE)
  3727. {
  3728. if ((ds && !(ds & SPECULATIVE))
  3729. || (ds2 && !(ds2 & SPECULATIVE)))
  3730. /* Then this dep can't be speculative. */
  3731. new_status &= ~SPECULATIVE;
  3732. else
  3733. {
  3734. /* Both are speculative. Merging probabilities. */
  3735. if (mem1)
  3736. {
  3737. dw_t dw;
  3738. dw = estimate_dep_weak (mem1, mem2);
  3739. ds = set_dep_weak (ds, BEGIN_DATA, dw);
  3740. }
  3741. if (!ds)
  3742. new_status = ds2;
  3743. else if (!ds2)
  3744. new_status = ds;
  3745. else
  3746. new_status = ds_merge (ds2, ds);
  3747. }
  3748. }
  3749. return new_status;
  3750. }
  3751. /* Return the join of DS1 and DS2. Use maximum instead of multiplying
  3752. probabilities. */
  3753. ds_t
  3754. ds_max_merge (ds_t ds1, ds_t ds2)
  3755. {
  3756. if (ds1 == 0 && ds2 == 0)
  3757. return 0;
  3758. if (ds1 == 0 && ds2 != 0)
  3759. return ds2;
  3760. if (ds1 != 0 && ds2 == 0)
  3761. return ds1;
  3762. return ds_merge_1 (ds1, ds2, true);
  3763. }
  3764. /* Return the probability of speculation success for the speculation
  3765. status DS. */
  3766. dw_t
  3767. ds_weak (ds_t ds)
  3768. {
  3769. ds_t res = 1, dt;
  3770. int n = 0;
  3771. dt = FIRST_SPEC_TYPE;
  3772. do
  3773. {
  3774. if (ds & dt)
  3775. {
  3776. res *= (ds_t) get_dep_weak (ds, dt);
  3777. n++;
  3778. }
  3779. if (dt == LAST_SPEC_TYPE)
  3780. break;
  3781. dt <<= SPEC_TYPE_SHIFT;
  3782. }
  3783. while (1);
  3784. gcc_assert (n);
  3785. while (--n)
  3786. res /= MAX_DEP_WEAK;
  3787. if (res < MIN_DEP_WEAK)
  3788. res = MIN_DEP_WEAK;
  3789. gcc_assert (res <= MAX_DEP_WEAK);
  3790. return (dw_t) res;
  3791. }
  3792. /* Return a dep status that contains all speculation types of DS. */
  3793. ds_t
  3794. ds_get_speculation_types (ds_t ds)
  3795. {
  3796. if (ds & BEGIN_DATA)
  3797. ds |= BEGIN_DATA;
  3798. if (ds & BE_IN_DATA)
  3799. ds |= BE_IN_DATA;
  3800. if (ds & BEGIN_CONTROL)
  3801. ds |= BEGIN_CONTROL;
  3802. if (ds & BE_IN_CONTROL)
  3803. ds |= BE_IN_CONTROL;
  3804. return ds & SPECULATIVE;
  3805. }
  3806. /* Return a dep status that contains maximal weakness for each speculation
  3807. type present in DS. */
  3808. ds_t
  3809. ds_get_max_dep_weak (ds_t ds)
  3810. {
  3811. if (ds & BEGIN_DATA)
  3812. ds = set_dep_weak (ds, BEGIN_DATA, MAX_DEP_WEAK);
  3813. if (ds & BE_IN_DATA)
  3814. ds = set_dep_weak (ds, BE_IN_DATA, MAX_DEP_WEAK);
  3815. if (ds & BEGIN_CONTROL)
  3816. ds = set_dep_weak (ds, BEGIN_CONTROL, MAX_DEP_WEAK);
  3817. if (ds & BE_IN_CONTROL)
  3818. ds = set_dep_weak (ds, BE_IN_CONTROL, MAX_DEP_WEAK);
  3819. return ds;
  3820. }
  3821. /* Dump information about the dependence status S. */
  3822. static void
  3823. dump_ds (FILE *f, ds_t s)
  3824. {
  3825. fprintf (f, "{");
  3826. if (s & BEGIN_DATA)
  3827. fprintf (f, "BEGIN_DATA: %d; ", get_dep_weak_1 (s, BEGIN_DATA));
  3828. if (s & BE_IN_DATA)
  3829. fprintf (f, "BE_IN_DATA: %d; ", get_dep_weak_1 (s, BE_IN_DATA));
  3830. if (s & BEGIN_CONTROL)
  3831. fprintf (f, "BEGIN_CONTROL: %d; ", get_dep_weak_1 (s, BEGIN_CONTROL));
  3832. if (s & BE_IN_CONTROL)
  3833. fprintf (f, "BE_IN_CONTROL: %d; ", get_dep_weak_1 (s, BE_IN_CONTROL));
  3834. if (s & HARD_DEP)
  3835. fprintf (f, "HARD_DEP; ");
  3836. if (s & DEP_TRUE)
  3837. fprintf (f, "DEP_TRUE; ");
  3838. if (s & DEP_OUTPUT)
  3839. fprintf (f, "DEP_OUTPUT; ");
  3840. if (s & DEP_ANTI)
  3841. fprintf (f, "DEP_ANTI; ");
  3842. if (s & DEP_CONTROL)
  3843. fprintf (f, "DEP_CONTROL; ");
  3844. fprintf (f, "}");
  3845. }
  3846. DEBUG_FUNCTION void
  3847. debug_ds (ds_t s)
  3848. {
  3849. dump_ds (stderr, s);
  3850. fprintf (stderr, "\n");
  3851. }
  3852. #ifdef ENABLE_CHECKING
  3853. /* Verify that dependence type and status are consistent.
  3854. If RELAXED_P is true, then skip dep_weakness checks. */
  3855. static void
  3856. check_dep (dep_t dep, bool relaxed_p)
  3857. {
  3858. enum reg_note dt = DEP_TYPE (dep);
  3859. ds_t ds = DEP_STATUS (dep);
  3860. gcc_assert (DEP_PRO (dep) != DEP_CON (dep));
  3861. if (!(current_sched_info->flags & USE_DEPS_LIST))
  3862. {
  3863. gcc_assert (ds == 0);
  3864. return;
  3865. }
  3866. /* Check that dependence type contains the same bits as the status. */
  3867. if (dt == REG_DEP_TRUE)
  3868. gcc_assert (ds & DEP_TRUE);
  3869. else if (dt == REG_DEP_OUTPUT)
  3870. gcc_assert ((ds & DEP_OUTPUT)
  3871. && !(ds & DEP_TRUE));
  3872. else if (dt == REG_DEP_ANTI)
  3873. gcc_assert ((ds & DEP_ANTI)
  3874. && !(ds & (DEP_OUTPUT | DEP_TRUE)));
  3875. else
  3876. gcc_assert (dt == REG_DEP_CONTROL
  3877. && (ds & DEP_CONTROL)
  3878. && !(ds & (DEP_OUTPUT | DEP_ANTI | DEP_TRUE)));
  3879. /* HARD_DEP can not appear in dep_status of a link. */
  3880. gcc_assert (!(ds & HARD_DEP));
  3881. /* Check that dependence status is set correctly when speculation is not
  3882. supported. */
  3883. if (!sched_deps_info->generate_spec_deps)
  3884. gcc_assert (!(ds & SPECULATIVE));
  3885. else if (ds & SPECULATIVE)
  3886. {
  3887. if (!relaxed_p)
  3888. {
  3889. ds_t type = FIRST_SPEC_TYPE;
  3890. /* Check that dependence weakness is in proper range. */
  3891. do
  3892. {
  3893. if (ds & type)
  3894. get_dep_weak (ds, type);
  3895. if (type == LAST_SPEC_TYPE)
  3896. break;
  3897. type <<= SPEC_TYPE_SHIFT;
  3898. }
  3899. while (1);
  3900. }
  3901. if (ds & BEGIN_SPEC)
  3902. {
  3903. /* Only true dependence can be data speculative. */
  3904. if (ds & BEGIN_DATA)
  3905. gcc_assert (ds & DEP_TRUE);
  3906. /* Control dependencies in the insn scheduler are represented by
  3907. anti-dependencies, therefore only anti dependence can be
  3908. control speculative. */
  3909. if (ds & BEGIN_CONTROL)
  3910. gcc_assert (ds & DEP_ANTI);
  3911. }
  3912. else
  3913. {
  3914. /* Subsequent speculations should resolve true dependencies. */
  3915. gcc_assert ((ds & DEP_TYPES) == DEP_TRUE);
  3916. }
  3917. /* Check that true and anti dependencies can't have other speculative
  3918. statuses. */
  3919. if (ds & DEP_TRUE)
  3920. gcc_assert (ds & (BEGIN_DATA | BE_IN_SPEC));
  3921. /* An output dependence can't be speculative at all. */
  3922. gcc_assert (!(ds & DEP_OUTPUT));
  3923. if (ds & DEP_ANTI)
  3924. gcc_assert (ds & BEGIN_CONTROL);
  3925. }
  3926. }
  3927. #endif /* ENABLE_CHECKING */
  3928. /* The following code discovers opportunities to switch a memory reference
  3929. and an increment by modifying the address. We ensure that this is done
  3930. only for dependencies that are only used to show a single register
  3931. dependence (using DEP_NONREG and DEP_MULTIPLE), and so that every memory
  3932. instruction involved is subject to only one dep that can cause a pattern
  3933. change.
  3934. When we discover a suitable dependency, we fill in the dep_replacement
  3935. structure to show how to modify the memory reference. */
  3936. /* Holds information about a pair of memory reference and register increment
  3937. insns which depend on each other, but could possibly be interchanged. */
  3938. struct mem_inc_info
  3939. {
  3940. rtx_insn *inc_insn;
  3941. rtx_insn *mem_insn;
  3942. rtx *mem_loc;
  3943. /* A register occurring in the memory address for which we wish to break
  3944. the dependence. This must be identical to the destination register of
  3945. the increment. */
  3946. rtx mem_reg0;
  3947. /* Any kind of index that is added to that register. */
  3948. rtx mem_index;
  3949. /* The constant offset used in the memory address. */
  3950. HOST_WIDE_INT mem_constant;
  3951. /* The constant added in the increment insn. Negated if the increment is
  3952. after the memory address. */
  3953. HOST_WIDE_INT inc_constant;
  3954. /* The source register used in the increment. May be different from mem_reg0
  3955. if the increment occurs before the memory address. */
  3956. rtx inc_input;
  3957. };
  3958. /* Verify that the memory location described in MII can be replaced with
  3959. one using NEW_ADDR. Return the new memory reference or NULL_RTX. The
  3960. insn remains unchanged by this function. */
  3961. static rtx
  3962. attempt_change (struct mem_inc_info *mii, rtx new_addr)
  3963. {
  3964. rtx mem = *mii->mem_loc;
  3965. rtx new_mem;
  3966. /* Jump through a lot of hoops to keep the attributes up to date. We
  3967. do not want to call one of the change address variants that take
  3968. an offset even though we know the offset in many cases. These
  3969. assume you are changing where the address is pointing by the
  3970. offset. */
  3971. new_mem = replace_equiv_address_nv (mem, new_addr);
  3972. if (! validate_change (mii->mem_insn, mii->mem_loc, new_mem, 0))
  3973. {
  3974. if (sched_verbose >= 5)
  3975. fprintf (sched_dump, "validation failure\n");
  3976. return NULL_RTX;
  3977. }
  3978. /* Put back the old one. */
  3979. validate_change (mii->mem_insn, mii->mem_loc, mem, 0);
  3980. return new_mem;
  3981. }
  3982. /* Return true if INSN is of a form "a = b op c" where a and b are
  3983. regs. op is + if c is a reg and +|- if c is a const. Fill in
  3984. informantion in MII about what is found.
  3985. BEFORE_MEM indicates whether the increment is found before or after
  3986. a corresponding memory reference. */
  3987. static bool
  3988. parse_add_or_inc (struct mem_inc_info *mii, rtx_insn *insn, bool before_mem)
  3989. {
  3990. rtx pat = single_set (insn);
  3991. rtx src, cst;
  3992. bool regs_equal;
  3993. if (RTX_FRAME_RELATED_P (insn) || !pat)
  3994. return false;
  3995. /* Result must be single reg. */
  3996. if (!REG_P (SET_DEST (pat)))
  3997. return false;
  3998. if (GET_CODE (SET_SRC (pat)) != PLUS)
  3999. return false;
  4000. mii->inc_insn = insn;
  4001. src = SET_SRC (pat);
  4002. mii->inc_input = XEXP (src, 0);
  4003. if (!REG_P (XEXP (src, 0)))
  4004. return false;
  4005. if (!rtx_equal_p (SET_DEST (pat), mii->mem_reg0))
  4006. return false;
  4007. cst = XEXP (src, 1);
  4008. if (!CONST_INT_P (cst))
  4009. return false;
  4010. mii->inc_constant = INTVAL (cst);
  4011. regs_equal = rtx_equal_p (mii->inc_input, mii->mem_reg0);
  4012. if (!before_mem)
  4013. {
  4014. mii->inc_constant = -mii->inc_constant;
  4015. if (!regs_equal)
  4016. return false;
  4017. }
  4018. if (regs_equal && REGNO (SET_DEST (pat)) == STACK_POINTER_REGNUM)
  4019. {
  4020. /* Note that the sign has already been reversed for !before_mem. */
  4021. #ifdef STACK_GROWS_DOWNWARD
  4022. return mii->inc_constant > 0;
  4023. #else
  4024. return mii->inc_constant < 0;
  4025. #endif
  4026. }
  4027. return true;
  4028. }
  4029. /* Once a suitable mem reference has been found and the corresponding data
  4030. in MII has been filled in, this function is called to find a suitable
  4031. add or inc insn involving the register we found in the memory
  4032. reference. */
  4033. static bool
  4034. find_inc (struct mem_inc_info *mii, bool backwards)
  4035. {
  4036. sd_iterator_def sd_it;
  4037. dep_t dep;
  4038. sd_it = sd_iterator_start (mii->mem_insn,
  4039. backwards ? SD_LIST_HARD_BACK : SD_LIST_FORW);
  4040. while (sd_iterator_cond (&sd_it, &dep))
  4041. {
  4042. dep_node_t node = DEP_LINK_NODE (*sd_it.linkp);
  4043. rtx_insn *pro = DEP_PRO (dep);
  4044. rtx_insn *con = DEP_CON (dep);
  4045. rtx_insn *inc_cand = backwards ? pro : con;
  4046. if (DEP_NONREG (dep) || DEP_MULTIPLE (dep))
  4047. goto next;
  4048. if (parse_add_or_inc (mii, inc_cand, backwards))
  4049. {
  4050. struct dep_replacement *desc;
  4051. df_ref def;
  4052. rtx newaddr, newmem;
  4053. if (sched_verbose >= 5)
  4054. fprintf (sched_dump, "candidate mem/inc pair: %d %d\n",
  4055. INSN_UID (mii->mem_insn), INSN_UID (inc_cand));
  4056. /* Need to assure that none of the operands of the inc
  4057. instruction are assigned to by the mem insn. */
  4058. FOR_EACH_INSN_DEF (def, mii->mem_insn)
  4059. if (reg_overlap_mentioned_p (DF_REF_REG (def), mii->inc_input)
  4060. || reg_overlap_mentioned_p (DF_REF_REG (def), mii->mem_reg0))
  4061. {
  4062. if (sched_verbose >= 5)
  4063. fprintf (sched_dump,
  4064. "inc conflicts with store failure.\n");
  4065. goto next;
  4066. }
  4067. newaddr = mii->inc_input;
  4068. if (mii->mem_index != NULL_RTX)
  4069. newaddr = gen_rtx_PLUS (GET_MODE (newaddr), newaddr,
  4070. mii->mem_index);
  4071. newaddr = plus_constant (GET_MODE (newaddr), newaddr,
  4072. mii->mem_constant + mii->inc_constant);
  4073. newmem = attempt_change (mii, newaddr);
  4074. if (newmem == NULL_RTX)
  4075. goto next;
  4076. if (sched_verbose >= 5)
  4077. fprintf (sched_dump, "successful address replacement\n");
  4078. desc = XCNEW (struct dep_replacement);
  4079. DEP_REPLACE (dep) = desc;
  4080. desc->loc = mii->mem_loc;
  4081. desc->newval = newmem;
  4082. desc->orig = *desc->loc;
  4083. desc->insn = mii->mem_insn;
  4084. move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con),
  4085. INSN_SPEC_BACK_DEPS (con));
  4086. if (backwards)
  4087. {
  4088. FOR_EACH_DEP (mii->inc_insn, SD_LIST_BACK, sd_it, dep)
  4089. add_dependence_1 (mii->mem_insn, DEP_PRO (dep),
  4090. REG_DEP_TRUE);
  4091. }
  4092. else
  4093. {
  4094. FOR_EACH_DEP (mii->inc_insn, SD_LIST_FORW, sd_it, dep)
  4095. add_dependence_1 (DEP_CON (dep), mii->mem_insn,
  4096. REG_DEP_ANTI);
  4097. }
  4098. return true;
  4099. }
  4100. next:
  4101. sd_iterator_next (&sd_it);
  4102. }
  4103. return false;
  4104. }
  4105. /* A recursive function that walks ADDRESS_OF_X to find memory references
  4106. which could be modified during scheduling. We call find_inc for each
  4107. one we find that has a recognizable form. MII holds information about
  4108. the pair of memory/increment instructions.
  4109. We ensure that every instruction with a memory reference (which will be
  4110. the location of the replacement) is assigned at most one breakable
  4111. dependency. */
  4112. static bool
  4113. find_mem (struct mem_inc_info *mii, rtx *address_of_x)
  4114. {
  4115. rtx x = *address_of_x;
  4116. enum rtx_code code = GET_CODE (x);
  4117. const char *const fmt = GET_RTX_FORMAT (code);
  4118. int i;
  4119. if (code == MEM)
  4120. {
  4121. rtx reg0 = XEXP (x, 0);
  4122. mii->mem_loc = address_of_x;
  4123. mii->mem_index = NULL_RTX;
  4124. mii->mem_constant = 0;
  4125. if (GET_CODE (reg0) == PLUS && CONST_INT_P (XEXP (reg0, 1)))
  4126. {
  4127. mii->mem_constant = INTVAL (XEXP (reg0, 1));
  4128. reg0 = XEXP (reg0, 0);
  4129. }
  4130. if (GET_CODE (reg0) == PLUS)
  4131. {
  4132. mii->mem_index = XEXP (reg0, 1);
  4133. reg0 = XEXP (reg0, 0);
  4134. }
  4135. if (REG_P (reg0))
  4136. {
  4137. df_ref use;
  4138. int occurrences = 0;
  4139. /* Make sure this reg appears only once in this insn. Can't use
  4140. count_occurrences since that only works for pseudos. */
  4141. FOR_EACH_INSN_USE (use, mii->mem_insn)
  4142. if (reg_overlap_mentioned_p (reg0, DF_REF_REG (use)))
  4143. if (++occurrences > 1)
  4144. {
  4145. if (sched_verbose >= 5)
  4146. fprintf (sched_dump, "mem count failure\n");
  4147. return false;
  4148. }
  4149. mii->mem_reg0 = reg0;
  4150. return find_inc (mii, true) || find_inc (mii, false);
  4151. }
  4152. return false;
  4153. }
  4154. if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
  4155. {
  4156. /* If REG occurs inside a MEM used in a bit-field reference,
  4157. that is unacceptable. */
  4158. return false;
  4159. }
  4160. /* Time for some deep diving. */
  4161. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  4162. {
  4163. if (fmt[i] == 'e')
  4164. {
  4165. if (find_mem (mii, &XEXP (x, i)))
  4166. return true;
  4167. }
  4168. else if (fmt[i] == 'E')
  4169. {
  4170. int j;
  4171. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  4172. if (find_mem (mii, &XVECEXP (x, i, j)))
  4173. return true;
  4174. }
  4175. }
  4176. return false;
  4177. }
  4178. /* Examine the instructions between HEAD and TAIL and try to find
  4179. dependencies that can be broken by modifying one of the patterns. */
  4180. void
  4181. find_modifiable_mems (rtx_insn *head, rtx_insn *tail)
  4182. {
  4183. rtx_insn *insn, *next_tail = NEXT_INSN (tail);
  4184. int success_in_block = 0;
  4185. for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
  4186. {
  4187. struct mem_inc_info mii;
  4188. if (!NONDEBUG_INSN_P (insn) || RTX_FRAME_RELATED_P (insn))
  4189. continue;
  4190. mii.mem_insn = insn;
  4191. if (find_mem (&mii, &PATTERN (insn)))
  4192. success_in_block++;
  4193. }
  4194. if (success_in_block && sched_verbose >= 5)
  4195. fprintf (sched_dump, "%d candidates for address modification found.\n",
  4196. success_in_block);
  4197. }
  4198. #endif /* INSN_SCHEDULING */