gcse.c 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166
  1. /* Partial redundancy elimination / Hoisting for RTL.
  2. Copyright (C) 1997-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* TODO
  16. - reordering of memory allocation and freeing to be more space efficient
  17. - calc rough register pressure information and use the info to drive all
  18. kinds of code motion (including code hoisting) in a unified way.
  19. */
  20. /* References searched while implementing this.
  21. Compilers Principles, Techniques and Tools
  22. Aho, Sethi, Ullman
  23. Addison-Wesley, 1988
  24. Global Optimization by Suppression of Partial Redundancies
  25. E. Morel, C. Renvoise
  26. communications of the acm, Vol. 22, Num. 2, Feb. 1979
  27. A Portable Machine-Independent Global Optimizer - Design and Measurements
  28. Frederick Chow
  29. Stanford Ph.D. thesis, Dec. 1983
  30. A Fast Algorithm for Code Movement Optimization
  31. D.M. Dhamdhere
  32. SIGPLAN Notices, Vol. 23, Num. 10, Oct. 1988
  33. A Solution to a Problem with Morel and Renvoise's
  34. Global Optimization by Suppression of Partial Redundancies
  35. K-H Drechsler, M.P. Stadel
  36. ACM TOPLAS, Vol. 10, Num. 4, Oct. 1988
  37. Practical Adaptation of the Global Optimization
  38. Algorithm of Morel and Renvoise
  39. D.M. Dhamdhere
  40. ACM TOPLAS, Vol. 13, Num. 2. Apr. 1991
  41. Efficiently Computing Static Single Assignment Form and the Control
  42. Dependence Graph
  43. R. Cytron, J. Ferrante, B.K. Rosen, M.N. Wegman, and F.K. Zadeck
  44. ACM TOPLAS, Vol. 13, Num. 4, Oct. 1991
  45. Lazy Code Motion
  46. J. Knoop, O. Ruthing, B. Steffen
  47. ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
  48. What's In a Region? Or Computing Control Dependence Regions in Near-Linear
  49. Time for Reducible Flow Control
  50. Thomas Ball
  51. ACM Letters on Programming Languages and Systems,
  52. Vol. 2, Num. 1-4, Mar-Dec 1993
  53. An Efficient Representation for Sparse Sets
  54. Preston Briggs, Linda Torczon
  55. ACM Letters on Programming Languages and Systems,
  56. Vol. 2, Num. 1-4, Mar-Dec 1993
  57. A Variation of Knoop, Ruthing, and Steffen's Lazy Code Motion
  58. K-H Drechsler, M.P. Stadel
  59. ACM SIGPLAN Notices, Vol. 28, Num. 5, May 1993
  60. Partial Dead Code Elimination
  61. J. Knoop, O. Ruthing, B. Steffen
  62. ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
  63. Effective Partial Redundancy Elimination
  64. P. Briggs, K.D. Cooper
  65. ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
  66. The Program Structure Tree: Computing Control Regions in Linear Time
  67. R. Johnson, D. Pearson, K. Pingali
  68. ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
  69. Optimal Code Motion: Theory and Practice
  70. J. Knoop, O. Ruthing, B. Steffen
  71. ACM TOPLAS, Vol. 16, Num. 4, Jul. 1994
  72. The power of assignment motion
  73. J. Knoop, O. Ruthing, B. Steffen
  74. ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
  75. Global code motion / global value numbering
  76. C. Click
  77. ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
  78. Value Driven Redundancy Elimination
  79. L.T. Simpson
  80. Rice University Ph.D. thesis, Apr. 1996
  81. Value Numbering
  82. L.T. Simpson
  83. Massively Scalar Compiler Project, Rice University, Sep. 1996
  84. High Performance Compilers for Parallel Computing
  85. Michael Wolfe
  86. Addison-Wesley, 1996
  87. Advanced Compiler Design and Implementation
  88. Steven Muchnick
  89. Morgan Kaufmann, 1997
  90. Building an Optimizing Compiler
  91. Robert Morgan
  92. Digital Press, 1998
  93. People wishing to speed up the code here should read:
  94. Elimination Algorithms for Data Flow Analysis
  95. B.G. Ryder, M.C. Paull
  96. ACM Computing Surveys, Vol. 18, Num. 3, Sep. 1986
  97. How to Analyze Large Programs Efficiently and Informatively
  98. D.M. Dhamdhere, B.K. Rosen, F.K. Zadeck
  99. ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
  100. People wishing to do something different can find various possibilities
  101. in the above papers and elsewhere.
  102. */
  103. #include "config.h"
  104. #include "system.h"
  105. #include "coretypes.h"
  106. #include "tm.h"
  107. #include "diagnostic-core.h"
  108. #include "toplev.h"
  109. #include "hard-reg-set.h"
  110. #include "rtl.h"
  111. #include "hash-set.h"
  112. #include "machmode.h"
  113. #include "vec.h"
  114. #include "double-int.h"
  115. #include "input.h"
  116. #include "alias.h"
  117. #include "symtab.h"
  118. #include "wide-int.h"
  119. #include "inchash.h"
  120. #include "tree.h"
  121. #include "tm_p.h"
  122. #include "regs.h"
  123. #include "ira.h"
  124. #include "flags.h"
  125. #include "insn-config.h"
  126. #include "recog.h"
  127. #include "predict.h"
  128. #include "function.h"
  129. #include "dominance.h"
  130. #include "cfg.h"
  131. #include "cfgrtl.h"
  132. #include "cfganal.h"
  133. #include "lcm.h"
  134. #include "cfgcleanup.h"
  135. #include "basic-block.h"
  136. #include "hashtab.h"
  137. #include "statistics.h"
  138. #include "real.h"
  139. #include "fixed-value.h"
  140. #include "expmed.h"
  141. #include "dojump.h"
  142. #include "explow.h"
  143. #include "calls.h"
  144. #include "emit-rtl.h"
  145. #include "varasm.h"
  146. #include "stmt.h"
  147. #include "expr.h"
  148. #include "except.h"
  149. #include "ggc.h"
  150. #include "params.h"
  151. #include "cselib.h"
  152. #include "intl.h"
  153. #include "obstack.h"
  154. #include "tree-pass.h"
  155. #include "hash-table.h"
  156. #include "df.h"
  157. #include "dbgcnt.h"
  158. #include "target.h"
  159. #include "gcse.h"
  160. #include "gcse-common.h"
  161. /* We support GCSE via Partial Redundancy Elimination. PRE optimizations
  162. are a superset of those done by classic GCSE.
  163. Two passes of copy/constant propagation are done around PRE or hoisting
  164. because the first one enables more GCSE and the second one helps to clean
  165. up the copies that PRE and HOIST create. This is needed more for PRE than
  166. for HOIST because code hoisting will try to use an existing register
  167. containing the common subexpression rather than create a new one. This is
  168. harder to do for PRE because of the code motion (which HOIST doesn't do).
  169. Expressions we are interested in GCSE-ing are of the form
  170. (set (pseudo-reg) (expression)).
  171. Function want_to_gcse_p says what these are.
  172. In addition, expressions in REG_EQUAL notes are candidates for GCSE-ing.
  173. This allows PRE to hoist expressions that are expressed in multiple insns,
  174. such as complex address calculations (e.g. for PIC code, or loads with a
  175. high part and a low part).
  176. PRE handles moving invariant expressions out of loops (by treating them as
  177. partially redundant).
  178. **********************
  179. We used to support multiple passes but there are diminishing returns in
  180. doing so. The first pass usually makes 90% of the changes that are doable.
  181. A second pass can make a few more changes made possible by the first pass.
  182. Experiments show any further passes don't make enough changes to justify
  183. the expense.
  184. A study of spec92 using an unlimited number of passes:
  185. [1 pass] = 1208 substitutions, [2] = 577, [3] = 202, [4] = 192, [5] = 83,
  186. [6] = 34, [7] = 17, [8] = 9, [9] = 4, [10] = 4, [11] = 2,
  187. [12] = 2, [13] = 1, [15] = 1, [16] = 2, [41] = 1
  188. It was found doing copy propagation between each pass enables further
  189. substitutions.
  190. This study was done before expressions in REG_EQUAL notes were added as
  191. candidate expressions for optimization, and before the GIMPLE optimizers
  192. were added. Probably, multiple passes is even less efficient now than
  193. at the time when the study was conducted.
  194. PRE is quite expensive in complicated functions because the DFA can take
  195. a while to converge. Hence we only perform one pass.
  196. **********************
  197. The steps for PRE are:
  198. 1) Build the hash table of expressions we wish to GCSE (expr_hash_table).
  199. 2) Perform the data flow analysis for PRE.
  200. 3) Delete the redundant instructions
  201. 4) Insert the required copies [if any] that make the partially
  202. redundant instructions fully redundant.
  203. 5) For other reaching expressions, insert an instruction to copy the value
  204. to a newly created pseudo that will reach the redundant instruction.
  205. The deletion is done first so that when we do insertions we
  206. know which pseudo reg to use.
  207. Various papers have argued that PRE DFA is expensive (O(n^2)) and others
  208. argue it is not. The number of iterations for the algorithm to converge
  209. is typically 2-4 so I don't view it as that expensive (relatively speaking).
  210. PRE GCSE depends heavily on the second CPROP pass to clean up the copies
  211. we create. To make an expression reach the place where it's redundant,
  212. the result of the expression is copied to a new register, and the redundant
  213. expression is deleted by replacing it with this new register. Classic GCSE
  214. doesn't have this problem as much as it computes the reaching defs of
  215. each register in each block and thus can try to use an existing
  216. register. */
  217. /* GCSE global vars. */
  218. struct target_gcse default_target_gcse;
  219. #if SWITCHABLE_TARGET
  220. struct target_gcse *this_target_gcse = &default_target_gcse;
  221. #endif
  222. /* Set to non-zero if CSE should run after all GCSE optimizations are done. */
  223. int flag_rerun_cse_after_global_opts;
  224. /* An obstack for our working variables. */
  225. static struct obstack gcse_obstack;
  226. /* Hash table of expressions. */
  227. struct gcse_expr
  228. {
  229. /* The expression. */
  230. rtx expr;
  231. /* Index in the available expression bitmaps. */
  232. int bitmap_index;
  233. /* Next entry with the same hash. */
  234. struct gcse_expr *next_same_hash;
  235. /* List of anticipatable occurrences in basic blocks in the function.
  236. An "anticipatable occurrence" is one that is the first occurrence in the
  237. basic block, the operands are not modified in the basic block prior
  238. to the occurrence and the output is not used between the start of
  239. the block and the occurrence. */
  240. struct gcse_occr *antic_occr;
  241. /* List of available occurrence in basic blocks in the function.
  242. An "available occurrence" is one that is the last occurrence in the
  243. basic block and the operands are not modified by following statements in
  244. the basic block [including this insn]. */
  245. struct gcse_occr *avail_occr;
  246. /* Non-null if the computation is PRE redundant.
  247. The value is the newly created pseudo-reg to record a copy of the
  248. expression in all the places that reach the redundant copy. */
  249. rtx reaching_reg;
  250. /* Maximum distance in instructions this expression can travel.
  251. We avoid moving simple expressions for more than a few instructions
  252. to keep register pressure under control.
  253. A value of "0" removes restrictions on how far the expression can
  254. travel. */
  255. int max_distance;
  256. };
  257. /* Occurrence of an expression.
  258. There is one per basic block. If a pattern appears more than once the
  259. last appearance is used [or first for anticipatable expressions]. */
  260. struct gcse_occr
  261. {
  262. /* Next occurrence of this expression. */
  263. struct gcse_occr *next;
  264. /* The insn that computes the expression. */
  265. rtx_insn *insn;
  266. /* Nonzero if this [anticipatable] occurrence has been deleted. */
  267. char deleted_p;
  268. /* Nonzero if this [available] occurrence has been copied to
  269. reaching_reg. */
  270. /* ??? This is mutually exclusive with deleted_p, so they could share
  271. the same byte. */
  272. char copied_p;
  273. };
  274. typedef struct gcse_occr *occr_t;
  275. /* Expression hash tables.
  276. Each hash table is an array of buckets.
  277. ??? It is known that if it were an array of entries, structure elements
  278. `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
  279. not clear whether in the final analysis a sufficient amount of memory would
  280. be saved as the size of the available expression bitmaps would be larger
  281. [one could build a mapping table without holes afterwards though].
  282. Someday I'll perform the computation and figure it out. */
  283. struct gcse_hash_table_d
  284. {
  285. /* The table itself.
  286. This is an array of `expr_hash_table_size' elements. */
  287. struct gcse_expr **table;
  288. /* Size of the hash table, in elements. */
  289. unsigned int size;
  290. /* Number of hash table elements. */
  291. unsigned int n_elems;
  292. };
  293. /* Expression hash table. */
  294. static struct gcse_hash_table_d expr_hash_table;
  295. /* This is a list of expressions which are MEMs and will be used by load
  296. or store motion.
  297. Load motion tracks MEMs which aren't killed by anything except itself,
  298. i.e. loads and stores to a single location.
  299. We can then allow movement of these MEM refs with a little special
  300. allowance. (all stores copy the same value to the reaching reg used
  301. for the loads). This means all values used to store into memory must have
  302. no side effects so we can re-issue the setter value. */
  303. struct ls_expr
  304. {
  305. struct gcse_expr * expr; /* Gcse expression reference for LM. */
  306. rtx pattern; /* Pattern of this mem. */
  307. rtx pattern_regs; /* List of registers mentioned by the mem. */
  308. rtx_insn_list *loads; /* INSN list of loads seen. */
  309. rtx_insn_list *stores; /* INSN list of stores seen. */
  310. struct ls_expr * next; /* Next in the list. */
  311. int invalid; /* Invalid for some reason. */
  312. int index; /* If it maps to a bitmap index. */
  313. unsigned int hash_index; /* Index when in a hash table. */
  314. rtx reaching_reg; /* Register to use when re-writing. */
  315. };
  316. /* Head of the list of load/store memory refs. */
  317. static struct ls_expr * pre_ldst_mems = NULL;
  318. struct pre_ldst_expr_hasher : typed_noop_remove <ls_expr>
  319. {
  320. typedef ls_expr value_type;
  321. typedef value_type compare_type;
  322. static inline hashval_t hash (const value_type *);
  323. static inline bool equal (const value_type *, const compare_type *);
  324. };
  325. /* Hashtable helpers. */
  326. inline hashval_t
  327. pre_ldst_expr_hasher::hash (const value_type *x)
  328. {
  329. int do_not_record_p = 0;
  330. return
  331. hash_rtx (x->pattern, GET_MODE (x->pattern), &do_not_record_p, NULL, false);
  332. }
  333. static int expr_equiv_p (const_rtx, const_rtx);
  334. inline bool
  335. pre_ldst_expr_hasher::equal (const value_type *ptr1,
  336. const compare_type *ptr2)
  337. {
  338. return expr_equiv_p (ptr1->pattern, ptr2->pattern);
  339. }
  340. /* Hashtable for the load/store memory refs. */
  341. static hash_table<pre_ldst_expr_hasher> *pre_ldst_table;
  342. /* Bitmap containing one bit for each register in the program.
  343. Used when performing GCSE to track which registers have been set since
  344. the start of the basic block. */
  345. static regset reg_set_bitmap;
  346. /* Array, indexed by basic block number for a list of insns which modify
  347. memory within that block. */
  348. static vec<rtx_insn *> *modify_mem_list;
  349. static bitmap modify_mem_list_set;
  350. /* This array parallels modify_mem_list, except that it stores MEMs
  351. being set and their canonicalized memory addresses. */
  352. static vec<modify_pair> *canon_modify_mem_list;
  353. /* Bitmap indexed by block numbers to record which blocks contain
  354. function calls. */
  355. static bitmap blocks_with_calls;
  356. /* Various variables for statistics gathering. */
  357. /* Memory used in a pass.
  358. This isn't intended to be absolutely precise. Its intent is only
  359. to keep an eye on memory usage. */
  360. static int bytes_used;
  361. /* GCSE substitutions made. */
  362. static int gcse_subst_count;
  363. /* Number of copy instructions created. */
  364. static int gcse_create_count;
  365. /* Doing code hoisting. */
  366. static bool doing_code_hoisting_p = false;
  367. /* For available exprs */
  368. static sbitmap *ae_kill;
  369. /* Data stored for each basic block. */
  370. struct bb_data
  371. {
  372. /* Maximal register pressure inside basic block for given register class
  373. (defined only for the pressure classes). */
  374. int max_reg_pressure[N_REG_CLASSES];
  375. /* Recorded register pressure of basic block before trying to hoist
  376. an expression. Will be used to restore the register pressure
  377. if the expression should not be hoisted. */
  378. int old_pressure;
  379. /* Recorded register live_in info of basic block during code hoisting
  380. process. BACKUP is used to record live_in info before trying to
  381. hoist an expression, and will be used to restore LIVE_IN if the
  382. expression should not be hoisted. */
  383. bitmap live_in, backup;
  384. };
  385. #define BB_DATA(bb) ((struct bb_data *) (bb)->aux)
  386. static basic_block curr_bb;
  387. /* Current register pressure for each pressure class. */
  388. static int curr_reg_pressure[N_REG_CLASSES];
  389. static void compute_can_copy (void);
  390. static void *gmalloc (size_t) ATTRIBUTE_MALLOC;
  391. static void *gcalloc (size_t, size_t) ATTRIBUTE_MALLOC;
  392. static void *gcse_alloc (unsigned long);
  393. static void alloc_gcse_mem (void);
  394. static void free_gcse_mem (void);
  395. static void hash_scan_insn (rtx_insn *, struct gcse_hash_table_d *);
  396. static void hash_scan_set (rtx, rtx_insn *, struct gcse_hash_table_d *);
  397. static void hash_scan_clobber (rtx, rtx_insn *, struct gcse_hash_table_d *);
  398. static void hash_scan_call (rtx, rtx_insn *, struct gcse_hash_table_d *);
  399. static int want_to_gcse_p (rtx, int *);
  400. static int oprs_unchanged_p (const_rtx, const rtx_insn *, int);
  401. static int oprs_anticipatable_p (const_rtx, const rtx_insn *);
  402. static int oprs_available_p (const_rtx, const rtx_insn *);
  403. static void insert_expr_in_table (rtx, machine_mode, rtx_insn *, int, int,
  404. int, struct gcse_hash_table_d *);
  405. static unsigned int hash_expr (const_rtx, machine_mode, int *, int);
  406. static void record_last_reg_set_info (rtx, int);
  407. static void record_last_mem_set_info (rtx_insn *);
  408. static void record_last_set_info (rtx, const_rtx, void *);
  409. static void compute_hash_table (struct gcse_hash_table_d *);
  410. static void alloc_hash_table (struct gcse_hash_table_d *);
  411. static void free_hash_table (struct gcse_hash_table_d *);
  412. static void compute_hash_table_work (struct gcse_hash_table_d *);
  413. static void dump_hash_table (FILE *, const char *, struct gcse_hash_table_d *);
  414. static void compute_local_properties (sbitmap *, sbitmap *, sbitmap *,
  415. struct gcse_hash_table_d *);
  416. static void mems_conflict_for_gcse_p (rtx, const_rtx, void *);
  417. static int load_killed_in_block_p (const_basic_block, int, const_rtx, int);
  418. static void alloc_pre_mem (int, int);
  419. static void free_pre_mem (void);
  420. static struct edge_list *compute_pre_data (void);
  421. static int pre_expr_reaches_here_p (basic_block, struct gcse_expr *,
  422. basic_block);
  423. static void insert_insn_end_basic_block (struct gcse_expr *, basic_block);
  424. static void pre_insert_copy_insn (struct gcse_expr *, rtx_insn *);
  425. static void pre_insert_copies (void);
  426. static int pre_delete (void);
  427. static int pre_gcse (struct edge_list *);
  428. static int one_pre_gcse_pass (void);
  429. static void add_label_notes (rtx, rtx);
  430. static void alloc_code_hoist_mem (int, int);
  431. static void free_code_hoist_mem (void);
  432. static void compute_code_hoist_vbeinout (void);
  433. static void compute_code_hoist_data (void);
  434. static int should_hoist_expr_to_dom (basic_block, struct gcse_expr *, basic_block,
  435. sbitmap, int, int *, enum reg_class,
  436. int *, bitmap, rtx_insn *);
  437. static int hoist_code (void);
  438. static enum reg_class get_regno_pressure_class (int regno, int *nregs);
  439. static enum reg_class get_pressure_class_and_nregs (rtx_insn *insn, int *nregs);
  440. static int one_code_hoisting_pass (void);
  441. static rtx_insn *process_insert_insn (struct gcse_expr *);
  442. static int pre_edge_insert (struct edge_list *, struct gcse_expr **);
  443. static int pre_expr_reaches_here_p_work (basic_block, struct gcse_expr *,
  444. basic_block, char *);
  445. static struct ls_expr * ldst_entry (rtx);
  446. static void free_ldst_entry (struct ls_expr *);
  447. static void free_ld_motion_mems (void);
  448. static void print_ldst_list (FILE *);
  449. static struct ls_expr * find_rtx_in_ldst (rtx);
  450. static int simple_mem (const_rtx);
  451. static void invalidate_any_buried_refs (rtx);
  452. static void compute_ld_motion_mems (void);
  453. static void trim_ld_motion_mems (void);
  454. static void update_ld_motion_stores (struct gcse_expr *);
  455. static void clear_modify_mem_tables (void);
  456. static void free_modify_mem_tables (void);
  457. static rtx gcse_emit_move_after (rtx, rtx, rtx_insn *);
  458. static bool is_too_expensive (const char *);
  459. #define GNEW(T) ((T *) gmalloc (sizeof (T)))
  460. #define GCNEW(T) ((T *) gcalloc (1, sizeof (T)))
  461. #define GNEWVEC(T, N) ((T *) gmalloc (sizeof (T) * (N)))
  462. #define GCNEWVEC(T, N) ((T *) gcalloc ((N), sizeof (T)))
  463. #define GNEWVAR(T, S) ((T *) gmalloc ((S)))
  464. #define GCNEWVAR(T, S) ((T *) gcalloc (1, (S)))
  465. #define GOBNEW(T) ((T *) gcse_alloc (sizeof (T)))
  466. #define GOBNEWVAR(T, S) ((T *) gcse_alloc ((S)))
  467. /* Misc. utilities. */
  468. #define can_copy \
  469. (this_target_gcse->x_can_copy)
  470. #define can_copy_init_p \
  471. (this_target_gcse->x_can_copy_init_p)
  472. /* Compute which modes support reg/reg copy operations. */
  473. static void
  474. compute_can_copy (void)
  475. {
  476. int i;
  477. #ifndef AVOID_CCMODE_COPIES
  478. rtx reg, insn;
  479. #endif
  480. memset (can_copy, 0, NUM_MACHINE_MODES);
  481. start_sequence ();
  482. for (i = 0; i < NUM_MACHINE_MODES; i++)
  483. if (GET_MODE_CLASS (i) == MODE_CC)
  484. {
  485. #ifdef AVOID_CCMODE_COPIES
  486. can_copy[i] = 0;
  487. #else
  488. reg = gen_rtx_REG ((machine_mode) i, LAST_VIRTUAL_REGISTER + 1);
  489. insn = emit_insn (gen_rtx_SET (VOIDmode, reg, reg));
  490. if (recog (PATTERN (insn), insn, NULL) >= 0)
  491. can_copy[i] = 1;
  492. #endif
  493. }
  494. else
  495. can_copy[i] = 1;
  496. end_sequence ();
  497. }
  498. /* Returns whether the mode supports reg/reg copy operations. */
  499. bool
  500. can_copy_p (machine_mode mode)
  501. {
  502. if (! can_copy_init_p)
  503. {
  504. compute_can_copy ();
  505. can_copy_init_p = true;
  506. }
  507. return can_copy[mode] != 0;
  508. }
  509. /* Cover function to xmalloc to record bytes allocated. */
  510. static void *
  511. gmalloc (size_t size)
  512. {
  513. bytes_used += size;
  514. return xmalloc (size);
  515. }
  516. /* Cover function to xcalloc to record bytes allocated. */
  517. static void *
  518. gcalloc (size_t nelem, size_t elsize)
  519. {
  520. bytes_used += nelem * elsize;
  521. return xcalloc (nelem, elsize);
  522. }
  523. /* Cover function to obstack_alloc. */
  524. static void *
  525. gcse_alloc (unsigned long size)
  526. {
  527. bytes_used += size;
  528. return obstack_alloc (&gcse_obstack, size);
  529. }
  530. /* Allocate memory for the reg/memory set tracking tables.
  531. This is called at the start of each pass. */
  532. static void
  533. alloc_gcse_mem (void)
  534. {
  535. /* Allocate vars to track sets of regs. */
  536. reg_set_bitmap = ALLOC_REG_SET (NULL);
  537. /* Allocate array to keep a list of insns which modify memory in each
  538. basic block. The two typedefs are needed to work around the
  539. pre-processor limitation with template types in macro arguments. */
  540. typedef vec<rtx_insn *> vec_rtx_heap;
  541. typedef vec<modify_pair> vec_modify_pair_heap;
  542. modify_mem_list = GCNEWVEC (vec_rtx_heap, last_basic_block_for_fn (cfun));
  543. canon_modify_mem_list = GCNEWVEC (vec_modify_pair_heap,
  544. last_basic_block_for_fn (cfun));
  545. modify_mem_list_set = BITMAP_ALLOC (NULL);
  546. blocks_with_calls = BITMAP_ALLOC (NULL);
  547. }
  548. /* Free memory allocated by alloc_gcse_mem. */
  549. static void
  550. free_gcse_mem (void)
  551. {
  552. FREE_REG_SET (reg_set_bitmap);
  553. free_modify_mem_tables ();
  554. BITMAP_FREE (modify_mem_list_set);
  555. BITMAP_FREE (blocks_with_calls);
  556. }
  557. /* Compute the local properties of each recorded expression.
  558. Local properties are those that are defined by the block, irrespective of
  559. other blocks.
  560. An expression is transparent in a block if its operands are not modified
  561. in the block.
  562. An expression is computed (locally available) in a block if it is computed
  563. at least once and expression would contain the same value if the
  564. computation was moved to the end of the block.
  565. An expression is locally anticipatable in a block if it is computed at
  566. least once and expression would contain the same value if the computation
  567. was moved to the beginning of the block.
  568. We call this routine for pre and code hoisting. They all compute
  569. basically the same information and thus can easily share this code.
  570. TRANSP, COMP, and ANTLOC are destination sbitmaps for recording local
  571. properties. If NULL, then it is not necessary to compute or record that
  572. particular property.
  573. TABLE controls which hash table to look at. */
  574. static void
  575. compute_local_properties (sbitmap *transp, sbitmap *comp, sbitmap *antloc,
  576. struct gcse_hash_table_d *table)
  577. {
  578. unsigned int i;
  579. /* Initialize any bitmaps that were passed in. */
  580. if (transp)
  581. {
  582. bitmap_vector_ones (transp, last_basic_block_for_fn (cfun));
  583. }
  584. if (comp)
  585. bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
  586. if (antloc)
  587. bitmap_vector_clear (antloc, last_basic_block_for_fn (cfun));
  588. for (i = 0; i < table->size; i++)
  589. {
  590. struct gcse_expr *expr;
  591. for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
  592. {
  593. int indx = expr->bitmap_index;
  594. struct gcse_occr *occr;
  595. /* The expression is transparent in this block if it is not killed.
  596. We start by assuming all are transparent [none are killed], and
  597. then reset the bits for those that are. */
  598. if (transp)
  599. compute_transp (expr->expr, indx, transp,
  600. blocks_with_calls,
  601. modify_mem_list_set,
  602. canon_modify_mem_list);
  603. /* The occurrences recorded in antic_occr are exactly those that
  604. we want to set to nonzero in ANTLOC. */
  605. if (antloc)
  606. for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
  607. {
  608. bitmap_set_bit (antloc[BLOCK_FOR_INSN (occr->insn)->index], indx);
  609. /* While we're scanning the table, this is a good place to
  610. initialize this. */
  611. occr->deleted_p = 0;
  612. }
  613. /* The occurrences recorded in avail_occr are exactly those that
  614. we want to set to nonzero in COMP. */
  615. if (comp)
  616. for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
  617. {
  618. bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
  619. /* While we're scanning the table, this is a good place to
  620. initialize this. */
  621. occr->copied_p = 0;
  622. }
  623. /* While we're scanning the table, this is a good place to
  624. initialize this. */
  625. expr->reaching_reg = 0;
  626. }
  627. }
  628. }
  629. /* Hash table support. */
  630. struct reg_avail_info
  631. {
  632. basic_block last_bb;
  633. int first_set;
  634. int last_set;
  635. };
  636. static struct reg_avail_info *reg_avail_info;
  637. static basic_block current_bb;
  638. /* See whether X, the source of a set, is something we want to consider for
  639. GCSE. */
  640. static int
  641. want_to_gcse_p (rtx x, int *max_distance_ptr)
  642. {
  643. #ifdef STACK_REGS
  644. /* On register stack architectures, don't GCSE constants from the
  645. constant pool, as the benefits are often swamped by the overhead
  646. of shuffling the register stack between basic blocks. */
  647. if (IS_STACK_MODE (GET_MODE (x)))
  648. x = avoid_constant_pool_reference (x);
  649. #endif
  650. /* GCSE'ing constants:
  651. We do not specifically distinguish between constant and non-constant
  652. expressions in PRE and Hoist. We use set_src_cost below to limit
  653. the maximum distance simple expressions can travel.
  654. Nevertheless, constants are much easier to GCSE, and, hence,
  655. it is easy to overdo the optimizations. Usually, excessive PRE and
  656. Hoisting of constant leads to increased register pressure.
  657. RA can deal with this by rematerialing some of the constants.
  658. Therefore, it is important that the back-end generates sets of constants
  659. in a way that allows reload rematerialize them under high register
  660. pressure, i.e., a pseudo register with REG_EQUAL to constant
  661. is set only once. Failing to do so will result in IRA/reload
  662. spilling such constants under high register pressure instead of
  663. rematerializing them. */
  664. switch (GET_CODE (x))
  665. {
  666. case REG:
  667. case SUBREG:
  668. case CALL:
  669. return 0;
  670. CASE_CONST_ANY:
  671. if (!doing_code_hoisting_p)
  672. /* Do not PRE constants. */
  673. return 0;
  674. /* FALLTHRU */
  675. default:
  676. if (doing_code_hoisting_p)
  677. /* PRE doesn't implement max_distance restriction. */
  678. {
  679. int cost;
  680. int max_distance;
  681. gcc_assert (!optimize_function_for_speed_p (cfun)
  682. && optimize_function_for_size_p (cfun));
  683. cost = set_src_cost (x, 0);
  684. if (cost < COSTS_N_INSNS (GCSE_UNRESTRICTED_COST))
  685. {
  686. max_distance = (GCSE_COST_DISTANCE_RATIO * cost) / 10;
  687. if (max_distance == 0)
  688. return 0;
  689. gcc_assert (max_distance > 0);
  690. }
  691. else
  692. max_distance = 0;
  693. if (max_distance_ptr)
  694. *max_distance_ptr = max_distance;
  695. }
  696. return can_assign_to_reg_without_clobbers_p (x);
  697. }
  698. }
  699. /* Used internally by can_assign_to_reg_without_clobbers_p. */
  700. static GTY(()) rtx_insn *test_insn;
  701. /* Return true if we can assign X to a pseudo register such that the
  702. resulting insn does not result in clobbering a hard register as a
  703. side-effect.
  704. Additionally, if the target requires it, check that the resulting insn
  705. can be copied. If it cannot, this means that X is special and probably
  706. has hidden side-effects we don't want to mess with.
  707. This function is typically used by code motion passes, to verify
  708. that it is safe to insert an insn without worrying about clobbering
  709. maybe live hard regs. */
  710. bool
  711. can_assign_to_reg_without_clobbers_p (rtx x)
  712. {
  713. int num_clobbers = 0;
  714. int icode;
  715. bool can_assign = false;
  716. /* If this is a valid operand, we are OK. If it's VOIDmode, we aren't. */
  717. if (general_operand (x, GET_MODE (x)))
  718. return 1;
  719. else if (GET_MODE (x) == VOIDmode)
  720. return 0;
  721. /* Otherwise, check if we can make a valid insn from it. First initialize
  722. our test insn if we haven't already. */
  723. if (test_insn == 0)
  724. {
  725. test_insn
  726. = make_insn_raw (gen_rtx_SET (VOIDmode,
  727. gen_rtx_REG (word_mode,
  728. FIRST_PSEUDO_REGISTER * 2),
  729. const0_rtx));
  730. SET_NEXT_INSN (test_insn) = SET_PREV_INSN (test_insn) = 0;
  731. INSN_LOCATION (test_insn) = UNKNOWN_LOCATION;
  732. }
  733. /* Now make an insn like the one we would make when GCSE'ing and see if
  734. valid. */
  735. PUT_MODE (SET_DEST (PATTERN (test_insn)), GET_MODE (x));
  736. SET_SRC (PATTERN (test_insn)) = x;
  737. icode = recog (PATTERN (test_insn), test_insn, &num_clobbers);
  738. /* If the test insn is valid and doesn't need clobbers, and the target also
  739. has no objections, we're good. */
  740. if (icode >= 0
  741. && (num_clobbers == 0 || !added_clobbers_hard_reg_p (icode))
  742. && ! (targetm.cannot_copy_insn_p
  743. && targetm.cannot_copy_insn_p (test_insn)))
  744. can_assign = true;
  745. /* Make sure test_insn doesn't have any pointers into GC space. */
  746. SET_SRC (PATTERN (test_insn)) = NULL_RTX;
  747. return can_assign;
  748. }
  749. /* Return nonzero if the operands of expression X are unchanged from the
  750. start of INSN's basic block up to but not including INSN (if AVAIL_P == 0),
  751. or from INSN to the end of INSN's basic block (if AVAIL_P != 0). */
  752. static int
  753. oprs_unchanged_p (const_rtx x, const rtx_insn *insn, int avail_p)
  754. {
  755. int i, j;
  756. enum rtx_code code;
  757. const char *fmt;
  758. if (x == 0)
  759. return 1;
  760. code = GET_CODE (x);
  761. switch (code)
  762. {
  763. case REG:
  764. {
  765. struct reg_avail_info *info = &reg_avail_info[REGNO (x)];
  766. if (info->last_bb != current_bb)
  767. return 1;
  768. if (avail_p)
  769. return info->last_set < DF_INSN_LUID (insn);
  770. else
  771. return info->first_set >= DF_INSN_LUID (insn);
  772. }
  773. case MEM:
  774. if (! flag_gcse_lm
  775. || load_killed_in_block_p (current_bb, DF_INSN_LUID (insn),
  776. x, avail_p))
  777. return 0;
  778. else
  779. return oprs_unchanged_p (XEXP (x, 0), insn, avail_p);
  780. case PRE_DEC:
  781. case PRE_INC:
  782. case POST_DEC:
  783. case POST_INC:
  784. case PRE_MODIFY:
  785. case POST_MODIFY:
  786. return 0;
  787. case PC:
  788. case CC0: /*FIXME*/
  789. case CONST:
  790. CASE_CONST_ANY:
  791. case SYMBOL_REF:
  792. case LABEL_REF:
  793. case ADDR_VEC:
  794. case ADDR_DIFF_VEC:
  795. return 1;
  796. default:
  797. break;
  798. }
  799. for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
  800. {
  801. if (fmt[i] == 'e')
  802. {
  803. /* If we are about to do the last recursive call needed at this
  804. level, change it into iteration. This function is called enough
  805. to be worth it. */
  806. if (i == 0)
  807. return oprs_unchanged_p (XEXP (x, i), insn, avail_p);
  808. else if (! oprs_unchanged_p (XEXP (x, i), insn, avail_p))
  809. return 0;
  810. }
  811. else if (fmt[i] == 'E')
  812. for (j = 0; j < XVECLEN (x, i); j++)
  813. if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, avail_p))
  814. return 0;
  815. }
  816. return 1;
  817. }
  818. /* Info passed from load_killed_in_block_p to mems_conflict_for_gcse_p. */
  819. struct mem_conflict_info
  820. {
  821. /* A memory reference for a load instruction, mems_conflict_for_gcse_p will
  822. see if a memory store conflicts with this memory load. */
  823. const_rtx mem;
  824. /* True if mems_conflict_for_gcse_p finds a conflict between two memory
  825. references. */
  826. bool conflict;
  827. };
  828. /* DEST is the output of an instruction. If it is a memory reference and
  829. possibly conflicts with the load found in DATA, then communicate this
  830. information back through DATA. */
  831. static void
  832. mems_conflict_for_gcse_p (rtx dest, const_rtx setter ATTRIBUTE_UNUSED,
  833. void *data)
  834. {
  835. struct mem_conflict_info *mci = (struct mem_conflict_info *) data;
  836. while (GET_CODE (dest) == SUBREG
  837. || GET_CODE (dest) == ZERO_EXTRACT
  838. || GET_CODE (dest) == STRICT_LOW_PART)
  839. dest = XEXP (dest, 0);
  840. /* If DEST is not a MEM, then it will not conflict with the load. Note
  841. that function calls are assumed to clobber memory, but are handled
  842. elsewhere. */
  843. if (! MEM_P (dest))
  844. return;
  845. /* If we are setting a MEM in our list of specially recognized MEMs,
  846. don't mark as killed this time. */
  847. if (pre_ldst_mems != NULL && expr_equiv_p (dest, mci->mem))
  848. {
  849. if (!find_rtx_in_ldst (dest))
  850. mci->conflict = true;
  851. return;
  852. }
  853. if (true_dependence (dest, GET_MODE (dest), mci->mem))
  854. mci->conflict = true;
  855. }
  856. /* Return nonzero if the expression in X (a memory reference) is killed
  857. in block BB before or after the insn with the LUID in UID_LIMIT.
  858. AVAIL_P is nonzero for kills after UID_LIMIT, and zero for kills
  859. before UID_LIMIT.
  860. To check the entire block, set UID_LIMIT to max_uid + 1 and
  861. AVAIL_P to 0. */
  862. static int
  863. load_killed_in_block_p (const_basic_block bb, int uid_limit, const_rtx x,
  864. int avail_p)
  865. {
  866. vec<rtx_insn *> list = modify_mem_list[bb->index];
  867. rtx_insn *setter;
  868. unsigned ix;
  869. /* If this is a readonly then we aren't going to be changing it. */
  870. if (MEM_READONLY_P (x))
  871. return 0;
  872. FOR_EACH_VEC_ELT_REVERSE (list, ix, setter)
  873. {
  874. struct mem_conflict_info mci;
  875. /* Ignore entries in the list that do not apply. */
  876. if ((avail_p
  877. && DF_INSN_LUID (setter) < uid_limit)
  878. || (! avail_p
  879. && DF_INSN_LUID (setter) > uid_limit))
  880. continue;
  881. /* If SETTER is a call everything is clobbered. Note that calls
  882. to pure functions are never put on the list, so we need not
  883. worry about them. */
  884. if (CALL_P (setter))
  885. return 1;
  886. /* SETTER must be an INSN of some kind that sets memory. Call
  887. note_stores to examine each hunk of memory that is modified. */
  888. mci.mem = x;
  889. mci.conflict = false;
  890. note_stores (PATTERN (setter), mems_conflict_for_gcse_p, &mci);
  891. if (mci.conflict)
  892. return 1;
  893. }
  894. return 0;
  895. }
  896. /* Return nonzero if the operands of expression X are unchanged from
  897. the start of INSN's basic block up to but not including INSN. */
  898. static int
  899. oprs_anticipatable_p (const_rtx x, const rtx_insn *insn)
  900. {
  901. return oprs_unchanged_p (x, insn, 0);
  902. }
  903. /* Return nonzero if the operands of expression X are unchanged from
  904. INSN to the end of INSN's basic block. */
  905. static int
  906. oprs_available_p (const_rtx x, const rtx_insn *insn)
  907. {
  908. return oprs_unchanged_p (x, insn, 1);
  909. }
  910. /* Hash expression X.
  911. MODE is only used if X is a CONST_INT. DO_NOT_RECORD_P is a boolean
  912. indicating if a volatile operand is found or if the expression contains
  913. something we don't want to insert in the table. HASH_TABLE_SIZE is
  914. the current size of the hash table to be probed. */
  915. static unsigned int
  916. hash_expr (const_rtx x, machine_mode mode, int *do_not_record_p,
  917. int hash_table_size)
  918. {
  919. unsigned int hash;
  920. *do_not_record_p = 0;
  921. hash = hash_rtx (x, mode, do_not_record_p, NULL, /*have_reg_qty=*/false);
  922. return hash % hash_table_size;
  923. }
  924. /* Return nonzero if exp1 is equivalent to exp2. */
  925. static int
  926. expr_equiv_p (const_rtx x, const_rtx y)
  927. {
  928. return exp_equiv_p (x, y, 0, true);
  929. }
  930. /* Insert expression X in INSN in the hash TABLE.
  931. If it is already present, record it as the last occurrence in INSN's
  932. basic block.
  933. MODE is the mode of the value X is being stored into.
  934. It is only used if X is a CONST_INT.
  935. ANTIC_P is nonzero if X is an anticipatable expression.
  936. AVAIL_P is nonzero if X is an available expression.
  937. MAX_DISTANCE is the maximum distance in instructions this expression can
  938. be moved. */
  939. static void
  940. insert_expr_in_table (rtx x, machine_mode mode, rtx_insn *insn,
  941. int antic_p,
  942. int avail_p, int max_distance, struct gcse_hash_table_d *table)
  943. {
  944. int found, do_not_record_p;
  945. unsigned int hash;
  946. struct gcse_expr *cur_expr, *last_expr = NULL;
  947. struct gcse_occr *antic_occr, *avail_occr;
  948. hash = hash_expr (x, mode, &do_not_record_p, table->size);
  949. /* Do not insert expression in table if it contains volatile operands,
  950. or if hash_expr determines the expression is something we don't want
  951. to or can't handle. */
  952. if (do_not_record_p)
  953. return;
  954. cur_expr = table->table[hash];
  955. found = 0;
  956. while (cur_expr && 0 == (found = expr_equiv_p (cur_expr->expr, x)))
  957. {
  958. /* If the expression isn't found, save a pointer to the end of
  959. the list. */
  960. last_expr = cur_expr;
  961. cur_expr = cur_expr->next_same_hash;
  962. }
  963. if (! found)
  964. {
  965. cur_expr = GOBNEW (struct gcse_expr);
  966. bytes_used += sizeof (struct gcse_expr);
  967. if (table->table[hash] == NULL)
  968. /* This is the first pattern that hashed to this index. */
  969. table->table[hash] = cur_expr;
  970. else
  971. /* Add EXPR to end of this hash chain. */
  972. last_expr->next_same_hash = cur_expr;
  973. /* Set the fields of the expr element. */
  974. cur_expr->expr = x;
  975. cur_expr->bitmap_index = table->n_elems++;
  976. cur_expr->next_same_hash = NULL;
  977. cur_expr->antic_occr = NULL;
  978. cur_expr->avail_occr = NULL;
  979. gcc_assert (max_distance >= 0);
  980. cur_expr->max_distance = max_distance;
  981. }
  982. else
  983. gcc_assert (cur_expr->max_distance == max_distance);
  984. /* Now record the occurrence(s). */
  985. if (antic_p)
  986. {
  987. antic_occr = cur_expr->antic_occr;
  988. if (antic_occr
  989. && BLOCK_FOR_INSN (antic_occr->insn) != BLOCK_FOR_INSN (insn))
  990. antic_occr = NULL;
  991. if (antic_occr)
  992. /* Found another instance of the expression in the same basic block.
  993. Prefer the currently recorded one. We want the first one in the
  994. block and the block is scanned from start to end. */
  995. ; /* nothing to do */
  996. else
  997. {
  998. /* First occurrence of this expression in this basic block. */
  999. antic_occr = GOBNEW (struct gcse_occr);
  1000. bytes_used += sizeof (struct gcse_occr);
  1001. antic_occr->insn = insn;
  1002. antic_occr->next = cur_expr->antic_occr;
  1003. antic_occr->deleted_p = 0;
  1004. cur_expr->antic_occr = antic_occr;
  1005. }
  1006. }
  1007. if (avail_p)
  1008. {
  1009. avail_occr = cur_expr->avail_occr;
  1010. if (avail_occr
  1011. && BLOCK_FOR_INSN (avail_occr->insn) == BLOCK_FOR_INSN (insn))
  1012. {
  1013. /* Found another instance of the expression in the same basic block.
  1014. Prefer this occurrence to the currently recorded one. We want
  1015. the last one in the block and the block is scanned from start
  1016. to end. */
  1017. avail_occr->insn = insn;
  1018. }
  1019. else
  1020. {
  1021. /* First occurrence of this expression in this basic block. */
  1022. avail_occr = GOBNEW (struct gcse_occr);
  1023. bytes_used += sizeof (struct gcse_occr);
  1024. avail_occr->insn = insn;
  1025. avail_occr->next = cur_expr->avail_occr;
  1026. avail_occr->deleted_p = 0;
  1027. cur_expr->avail_occr = avail_occr;
  1028. }
  1029. }
  1030. }
  1031. /* Scan SET present in INSN and add an entry to the hash TABLE. */
  1032. static void
  1033. hash_scan_set (rtx set, rtx_insn *insn, struct gcse_hash_table_d *table)
  1034. {
  1035. rtx src = SET_SRC (set);
  1036. rtx dest = SET_DEST (set);
  1037. rtx note;
  1038. if (GET_CODE (src) == CALL)
  1039. hash_scan_call (src, insn, table);
  1040. else if (REG_P (dest))
  1041. {
  1042. unsigned int regno = REGNO (dest);
  1043. int max_distance = 0;
  1044. /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
  1045. This allows us to do a single GCSE pass and still eliminate
  1046. redundant constants, addresses or other expressions that are
  1047. constructed with multiple instructions.
  1048. However, keep the original SRC if INSN is a simple reg-reg move.
  1049. In this case, there will almost always be a REG_EQUAL note on the
  1050. insn that sets SRC. By recording the REG_EQUAL value here as SRC
  1051. for INSN, we miss copy propagation opportunities and we perform the
  1052. same PRE GCSE operation repeatedly on the same REG_EQUAL value if we
  1053. do more than one PRE GCSE pass.
  1054. Note that this does not impede profitable constant propagations. We
  1055. "look through" reg-reg sets in lookup_avail_set. */
  1056. note = find_reg_equal_equiv_note (insn);
  1057. if (note != 0
  1058. && REG_NOTE_KIND (note) == REG_EQUAL
  1059. && !REG_P (src)
  1060. && want_to_gcse_p (XEXP (note, 0), NULL))
  1061. src = XEXP (note, 0), set = gen_rtx_SET (VOIDmode, dest, src);
  1062. /* Only record sets of pseudo-regs in the hash table. */
  1063. if (regno >= FIRST_PSEUDO_REGISTER
  1064. /* Don't GCSE something if we can't do a reg/reg copy. */
  1065. && can_copy_p (GET_MODE (dest))
  1066. /* GCSE commonly inserts instruction after the insn. We can't
  1067. do that easily for EH edges so disable GCSE on these for now. */
  1068. /* ??? We can now easily create new EH landing pads at the
  1069. gimple level, for splitting edges; there's no reason we
  1070. can't do the same thing at the rtl level. */
  1071. && !can_throw_internal (insn)
  1072. /* Is SET_SRC something we want to gcse? */
  1073. && want_to_gcse_p (src, &max_distance)
  1074. /* Don't CSE a nop. */
  1075. && ! set_noop_p (set)
  1076. /* Don't GCSE if it has attached REG_EQUIV note.
  1077. At this point this only function parameters should have
  1078. REG_EQUIV notes and if the argument slot is used somewhere
  1079. explicitly, it means address of parameter has been taken,
  1080. so we should not extend the lifetime of the pseudo. */
  1081. && (note == NULL_RTX || ! MEM_P (XEXP (note, 0))))
  1082. {
  1083. /* An expression is not anticipatable if its operands are
  1084. modified before this insn or if this is not the only SET in
  1085. this insn. The latter condition does not have to mean that
  1086. SRC itself is not anticipatable, but we just will not be
  1087. able to handle code motion of insns with multiple sets. */
  1088. int antic_p = oprs_anticipatable_p (src, insn)
  1089. && !multiple_sets (insn);
  1090. /* An expression is not available if its operands are
  1091. subsequently modified, including this insn. It's also not
  1092. available if this is a branch, because we can't insert
  1093. a set after the branch. */
  1094. int avail_p = (oprs_available_p (src, insn)
  1095. && ! JUMP_P (insn));
  1096. insert_expr_in_table (src, GET_MODE (dest), insn, antic_p, avail_p,
  1097. max_distance, table);
  1098. }
  1099. }
  1100. /* In case of store we want to consider the memory value as available in
  1101. the REG stored in that memory. This makes it possible to remove
  1102. redundant loads from due to stores to the same location. */
  1103. else if (flag_gcse_las && REG_P (src) && MEM_P (dest))
  1104. {
  1105. unsigned int regno = REGNO (src);
  1106. int max_distance = 0;
  1107. /* Only record sets of pseudo-regs in the hash table. */
  1108. if (regno >= FIRST_PSEUDO_REGISTER
  1109. /* Don't GCSE something if we can't do a reg/reg copy. */
  1110. && can_copy_p (GET_MODE (src))
  1111. /* GCSE commonly inserts instruction after the insn. We can't
  1112. do that easily for EH edges so disable GCSE on these for now. */
  1113. && !can_throw_internal (insn)
  1114. /* Is SET_DEST something we want to gcse? */
  1115. && want_to_gcse_p (dest, &max_distance)
  1116. /* Don't CSE a nop. */
  1117. && ! set_noop_p (set)
  1118. /* Don't GCSE if it has attached REG_EQUIV note.
  1119. At this point this only function parameters should have
  1120. REG_EQUIV notes and if the argument slot is used somewhere
  1121. explicitly, it means address of parameter has been taken,
  1122. so we should not extend the lifetime of the pseudo. */
  1123. && ((note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) == 0
  1124. || ! MEM_P (XEXP (note, 0))))
  1125. {
  1126. /* Stores are never anticipatable. */
  1127. int antic_p = 0;
  1128. /* An expression is not available if its operands are
  1129. subsequently modified, including this insn. It's also not
  1130. available if this is a branch, because we can't insert
  1131. a set after the branch. */
  1132. int avail_p = oprs_available_p (dest, insn)
  1133. && ! JUMP_P (insn);
  1134. /* Record the memory expression (DEST) in the hash table. */
  1135. insert_expr_in_table (dest, GET_MODE (dest), insn,
  1136. antic_p, avail_p, max_distance, table);
  1137. }
  1138. }
  1139. }
  1140. static void
  1141. hash_scan_clobber (rtx x ATTRIBUTE_UNUSED, rtx_insn *insn ATTRIBUTE_UNUSED,
  1142. struct gcse_hash_table_d *table ATTRIBUTE_UNUSED)
  1143. {
  1144. /* Currently nothing to do. */
  1145. }
  1146. static void
  1147. hash_scan_call (rtx x ATTRIBUTE_UNUSED, rtx_insn *insn ATTRIBUTE_UNUSED,
  1148. struct gcse_hash_table_d *table ATTRIBUTE_UNUSED)
  1149. {
  1150. /* Currently nothing to do. */
  1151. }
  1152. /* Process INSN and add hash table entries as appropriate. */
  1153. static void
  1154. hash_scan_insn (rtx_insn *insn, struct gcse_hash_table_d *table)
  1155. {
  1156. rtx pat = PATTERN (insn);
  1157. int i;
  1158. /* Pick out the sets of INSN and for other forms of instructions record
  1159. what's been modified. */
  1160. if (GET_CODE (pat) == SET)
  1161. hash_scan_set (pat, insn, table);
  1162. else if (GET_CODE (pat) == CLOBBER)
  1163. hash_scan_clobber (pat, insn, table);
  1164. else if (GET_CODE (pat) == CALL)
  1165. hash_scan_call (pat, insn, table);
  1166. else if (GET_CODE (pat) == PARALLEL)
  1167. for (i = 0; i < XVECLEN (pat, 0); i++)
  1168. {
  1169. rtx x = XVECEXP (pat, 0, i);
  1170. if (GET_CODE (x) == SET)
  1171. hash_scan_set (x, insn, table);
  1172. else if (GET_CODE (x) == CLOBBER)
  1173. hash_scan_clobber (x, insn, table);
  1174. else if (GET_CODE (x) == CALL)
  1175. hash_scan_call (x, insn, table);
  1176. }
  1177. }
  1178. /* Dump the hash table TABLE to file FILE under the name NAME. */
  1179. static void
  1180. dump_hash_table (FILE *file, const char *name, struct gcse_hash_table_d *table)
  1181. {
  1182. int i;
  1183. /* Flattened out table, so it's printed in proper order. */
  1184. struct gcse_expr **flat_table;
  1185. unsigned int *hash_val;
  1186. struct gcse_expr *expr;
  1187. flat_table = XCNEWVEC (struct gcse_expr *, table->n_elems);
  1188. hash_val = XNEWVEC (unsigned int, table->n_elems);
  1189. for (i = 0; i < (int) table->size; i++)
  1190. for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
  1191. {
  1192. flat_table[expr->bitmap_index] = expr;
  1193. hash_val[expr->bitmap_index] = i;
  1194. }
  1195. fprintf (file, "%s hash table (%d buckets, %d entries)\n",
  1196. name, table->size, table->n_elems);
  1197. for (i = 0; i < (int) table->n_elems; i++)
  1198. if (flat_table[i] != 0)
  1199. {
  1200. expr = flat_table[i];
  1201. fprintf (file, "Index %d (hash value %d; max distance %d)\n ",
  1202. expr->bitmap_index, hash_val[i], expr->max_distance);
  1203. print_rtl (file, expr->expr);
  1204. fprintf (file, "\n");
  1205. }
  1206. fprintf (file, "\n");
  1207. free (flat_table);
  1208. free (hash_val);
  1209. }
  1210. /* Record register first/last/block set information for REGNO in INSN.
  1211. first_set records the first place in the block where the register
  1212. is set and is used to compute "anticipatability".
  1213. last_set records the last place in the block where the register
  1214. is set and is used to compute "availability".
  1215. last_bb records the block for which first_set and last_set are
  1216. valid, as a quick test to invalidate them. */
  1217. static void
  1218. record_last_reg_set_info (rtx insn, int regno)
  1219. {
  1220. struct reg_avail_info *info = &reg_avail_info[regno];
  1221. int luid = DF_INSN_LUID (insn);
  1222. info->last_set = luid;
  1223. if (info->last_bb != current_bb)
  1224. {
  1225. info->last_bb = current_bb;
  1226. info->first_set = luid;
  1227. }
  1228. }
  1229. /* Record memory modification information for INSN. We do not actually care
  1230. about the memory location(s) that are set, or even how they are set (consider
  1231. a CALL_INSN). We merely need to record which insns modify memory. */
  1232. static void
  1233. record_last_mem_set_info (rtx_insn *insn)
  1234. {
  1235. if (! flag_gcse_lm)
  1236. return;
  1237. record_last_mem_set_info_common (insn, modify_mem_list,
  1238. canon_modify_mem_list,
  1239. modify_mem_list_set,
  1240. blocks_with_calls);
  1241. }
  1242. /* Called from compute_hash_table via note_stores to handle one
  1243. SET or CLOBBER in an insn. DATA is really the instruction in which
  1244. the SET is taking place. */
  1245. static void
  1246. record_last_set_info (rtx dest, const_rtx setter ATTRIBUTE_UNUSED, void *data)
  1247. {
  1248. rtx_insn *last_set_insn = (rtx_insn *) data;
  1249. if (GET_CODE (dest) == SUBREG)
  1250. dest = SUBREG_REG (dest);
  1251. if (REG_P (dest))
  1252. record_last_reg_set_info (last_set_insn, REGNO (dest));
  1253. else if (MEM_P (dest)
  1254. /* Ignore pushes, they clobber nothing. */
  1255. && ! push_operand (dest, GET_MODE (dest)))
  1256. record_last_mem_set_info (last_set_insn);
  1257. }
  1258. /* Top level function to create an expression hash table.
  1259. Expression entries are placed in the hash table if
  1260. - they are of the form (set (pseudo-reg) src),
  1261. - src is something we want to perform GCSE on,
  1262. - none of the operands are subsequently modified in the block
  1263. Currently src must be a pseudo-reg or a const_int.
  1264. TABLE is the table computed. */
  1265. static void
  1266. compute_hash_table_work (struct gcse_hash_table_d *table)
  1267. {
  1268. int i;
  1269. /* re-Cache any INSN_LIST nodes we have allocated. */
  1270. clear_modify_mem_tables ();
  1271. /* Some working arrays used to track first and last set in each block. */
  1272. reg_avail_info = GNEWVEC (struct reg_avail_info, max_reg_num ());
  1273. for (i = 0; i < max_reg_num (); ++i)
  1274. reg_avail_info[i].last_bb = NULL;
  1275. FOR_EACH_BB_FN (current_bb, cfun)
  1276. {
  1277. rtx_insn *insn;
  1278. unsigned int regno;
  1279. /* First pass over the instructions records information used to
  1280. determine when registers and memory are first and last set. */
  1281. FOR_BB_INSNS (current_bb, insn)
  1282. {
  1283. if (!NONDEBUG_INSN_P (insn))
  1284. continue;
  1285. if (CALL_P (insn))
  1286. {
  1287. hard_reg_set_iterator hrsi;
  1288. EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call,
  1289. 0, regno, hrsi)
  1290. record_last_reg_set_info (insn, regno);
  1291. if (! RTL_CONST_OR_PURE_CALL_P (insn))
  1292. record_last_mem_set_info (insn);
  1293. }
  1294. note_stores (PATTERN (insn), record_last_set_info, insn);
  1295. }
  1296. /* The next pass builds the hash table. */
  1297. FOR_BB_INSNS (current_bb, insn)
  1298. if (NONDEBUG_INSN_P (insn))
  1299. hash_scan_insn (insn, table);
  1300. }
  1301. free (reg_avail_info);
  1302. reg_avail_info = NULL;
  1303. }
  1304. /* Allocate space for the set/expr hash TABLE.
  1305. It is used to determine the number of buckets to use. */
  1306. static void
  1307. alloc_hash_table (struct gcse_hash_table_d *table)
  1308. {
  1309. int n;
  1310. n = get_max_insn_count ();
  1311. table->size = n / 4;
  1312. if (table->size < 11)
  1313. table->size = 11;
  1314. /* Attempt to maintain efficient use of hash table.
  1315. Making it an odd number is simplest for now.
  1316. ??? Later take some measurements. */
  1317. table->size |= 1;
  1318. n = table->size * sizeof (struct gcse_expr *);
  1319. table->table = GNEWVAR (struct gcse_expr *, n);
  1320. }
  1321. /* Free things allocated by alloc_hash_table. */
  1322. static void
  1323. free_hash_table (struct gcse_hash_table_d *table)
  1324. {
  1325. free (table->table);
  1326. }
  1327. /* Compute the expression hash table TABLE. */
  1328. static void
  1329. compute_hash_table (struct gcse_hash_table_d *table)
  1330. {
  1331. /* Initialize count of number of entries in hash table. */
  1332. table->n_elems = 0;
  1333. memset (table->table, 0, table->size * sizeof (struct gcse_expr *));
  1334. compute_hash_table_work (table);
  1335. }
  1336. /* Expression tracking support. */
  1337. /* Clear canon_modify_mem_list and modify_mem_list tables. */
  1338. static void
  1339. clear_modify_mem_tables (void)
  1340. {
  1341. unsigned i;
  1342. bitmap_iterator bi;
  1343. EXECUTE_IF_SET_IN_BITMAP (modify_mem_list_set, 0, i, bi)
  1344. {
  1345. modify_mem_list[i].release ();
  1346. canon_modify_mem_list[i].release ();
  1347. }
  1348. bitmap_clear (modify_mem_list_set);
  1349. bitmap_clear (blocks_with_calls);
  1350. }
  1351. /* Release memory used by modify_mem_list_set. */
  1352. static void
  1353. free_modify_mem_tables (void)
  1354. {
  1355. clear_modify_mem_tables ();
  1356. free (modify_mem_list);
  1357. free (canon_modify_mem_list);
  1358. modify_mem_list = 0;
  1359. canon_modify_mem_list = 0;
  1360. }
  1361. /* Compute PRE+LCM working variables. */
  1362. /* Local properties of expressions. */
  1363. /* Nonzero for expressions that are transparent in the block. */
  1364. static sbitmap *transp;
  1365. /* Nonzero for expressions that are computed (available) in the block. */
  1366. static sbitmap *comp;
  1367. /* Nonzero for expressions that are locally anticipatable in the block. */
  1368. static sbitmap *antloc;
  1369. /* Nonzero for expressions where this block is an optimal computation
  1370. point. */
  1371. static sbitmap *pre_optimal;
  1372. /* Nonzero for expressions which are redundant in a particular block. */
  1373. static sbitmap *pre_redundant;
  1374. /* Nonzero for expressions which should be inserted on a specific edge. */
  1375. static sbitmap *pre_insert_map;
  1376. /* Nonzero for expressions which should be deleted in a specific block. */
  1377. static sbitmap *pre_delete_map;
  1378. /* Allocate vars used for PRE analysis. */
  1379. static void
  1380. alloc_pre_mem (int n_blocks, int n_exprs)
  1381. {
  1382. transp = sbitmap_vector_alloc (n_blocks, n_exprs);
  1383. comp = sbitmap_vector_alloc (n_blocks, n_exprs);
  1384. antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
  1385. pre_optimal = NULL;
  1386. pre_redundant = NULL;
  1387. pre_insert_map = NULL;
  1388. pre_delete_map = NULL;
  1389. ae_kill = sbitmap_vector_alloc (n_blocks, n_exprs);
  1390. /* pre_insert and pre_delete are allocated later. */
  1391. }
  1392. /* Free vars used for PRE analysis. */
  1393. static void
  1394. free_pre_mem (void)
  1395. {
  1396. sbitmap_vector_free (transp);
  1397. sbitmap_vector_free (comp);
  1398. /* ANTLOC and AE_KILL are freed just after pre_lcm finishes. */
  1399. if (pre_optimal)
  1400. sbitmap_vector_free (pre_optimal);
  1401. if (pre_redundant)
  1402. sbitmap_vector_free (pre_redundant);
  1403. if (pre_insert_map)
  1404. sbitmap_vector_free (pre_insert_map);
  1405. if (pre_delete_map)
  1406. sbitmap_vector_free (pre_delete_map);
  1407. transp = comp = NULL;
  1408. pre_optimal = pre_redundant = pre_insert_map = pre_delete_map = NULL;
  1409. }
  1410. /* Remove certain expressions from anticipatable and transparent
  1411. sets of basic blocks that have incoming abnormal edge.
  1412. For PRE remove potentially trapping expressions to avoid placing
  1413. them on abnormal edges. For hoisting remove memory references that
  1414. can be clobbered by calls. */
  1415. static void
  1416. prune_expressions (bool pre_p)
  1417. {
  1418. sbitmap prune_exprs;
  1419. struct gcse_expr *expr;
  1420. unsigned int ui;
  1421. basic_block bb;
  1422. prune_exprs = sbitmap_alloc (expr_hash_table.n_elems);
  1423. bitmap_clear (prune_exprs);
  1424. for (ui = 0; ui < expr_hash_table.size; ui++)
  1425. {
  1426. for (expr = expr_hash_table.table[ui]; expr; expr = expr->next_same_hash)
  1427. {
  1428. /* Note potentially trapping expressions. */
  1429. if (may_trap_p (expr->expr))
  1430. {
  1431. bitmap_set_bit (prune_exprs, expr->bitmap_index);
  1432. continue;
  1433. }
  1434. if (!pre_p && MEM_P (expr->expr))
  1435. /* Note memory references that can be clobbered by a call.
  1436. We do not split abnormal edges in hoisting, so would
  1437. a memory reference get hoisted along an abnormal edge,
  1438. it would be placed /before/ the call. Therefore, only
  1439. constant memory references can be hoisted along abnormal
  1440. edges. */
  1441. {
  1442. if (GET_CODE (XEXP (expr->expr, 0)) == SYMBOL_REF
  1443. && CONSTANT_POOL_ADDRESS_P (XEXP (expr->expr, 0)))
  1444. continue;
  1445. if (MEM_READONLY_P (expr->expr)
  1446. && !MEM_VOLATILE_P (expr->expr)
  1447. && MEM_NOTRAP_P (expr->expr))
  1448. /* Constant memory reference, e.g., a PIC address. */
  1449. continue;
  1450. /* ??? Optimally, we would use interprocedural alias
  1451. analysis to determine if this mem is actually killed
  1452. by this call. */
  1453. bitmap_set_bit (prune_exprs, expr->bitmap_index);
  1454. }
  1455. }
  1456. }
  1457. FOR_EACH_BB_FN (bb, cfun)
  1458. {
  1459. edge e;
  1460. edge_iterator ei;
  1461. /* If the current block is the destination of an abnormal edge, we
  1462. kill all trapping (for PRE) and memory (for hoist) expressions
  1463. because we won't be able to properly place the instruction on
  1464. the edge. So make them neither anticipatable nor transparent.
  1465. This is fairly conservative.
  1466. ??? For hoisting it may be necessary to check for set-and-jump
  1467. instructions here, not just for abnormal edges. The general problem
  1468. is that when an expression cannot not be placed right at the end of
  1469. a basic block we should account for any side-effects of a subsequent
  1470. jump instructions that could clobber the expression. It would
  1471. be best to implement this check along the lines of
  1472. should_hoist_expr_to_dom where the target block is already known
  1473. and, hence, there's no need to conservatively prune expressions on
  1474. "intermediate" set-and-jump instructions. */
  1475. FOR_EACH_EDGE (e, ei, bb->preds)
  1476. if ((e->flags & EDGE_ABNORMAL)
  1477. && (pre_p || CALL_P (BB_END (e->src))))
  1478. {
  1479. bitmap_and_compl (antloc[bb->index],
  1480. antloc[bb->index], prune_exprs);
  1481. bitmap_and_compl (transp[bb->index],
  1482. transp[bb->index], prune_exprs);
  1483. break;
  1484. }
  1485. }
  1486. sbitmap_free (prune_exprs);
  1487. }
  1488. /* It may be necessary to insert a large number of insns on edges to
  1489. make the existing occurrences of expressions fully redundant. This
  1490. routine examines the set of insertions and deletions and if the ratio
  1491. of insertions to deletions is too high for a particular expression, then
  1492. the expression is removed from the insertion/deletion sets.
  1493. N_ELEMS is the number of elements in the hash table. */
  1494. static void
  1495. prune_insertions_deletions (int n_elems)
  1496. {
  1497. sbitmap_iterator sbi;
  1498. sbitmap prune_exprs;
  1499. /* We always use I to iterate over blocks/edges and J to iterate over
  1500. expressions. */
  1501. unsigned int i, j;
  1502. /* Counts for the number of times an expression needs to be inserted and
  1503. number of times an expression can be removed as a result. */
  1504. int *insertions = GCNEWVEC (int, n_elems);
  1505. int *deletions = GCNEWVEC (int, n_elems);
  1506. /* Set of expressions which require too many insertions relative to
  1507. the number of deletions achieved. We will prune these out of the
  1508. insertion/deletion sets. */
  1509. prune_exprs = sbitmap_alloc (n_elems);
  1510. bitmap_clear (prune_exprs);
  1511. /* Iterate over the edges counting the number of times each expression
  1512. needs to be inserted. */
  1513. for (i = 0; i < (unsigned) n_edges_for_fn (cfun); i++)
  1514. {
  1515. EXECUTE_IF_SET_IN_BITMAP (pre_insert_map[i], 0, j, sbi)
  1516. insertions[j]++;
  1517. }
  1518. /* Similarly for deletions, but those occur in blocks rather than on
  1519. edges. */
  1520. for (i = 0; i < (unsigned) last_basic_block_for_fn (cfun); i++)
  1521. {
  1522. EXECUTE_IF_SET_IN_BITMAP (pre_delete_map[i], 0, j, sbi)
  1523. deletions[j]++;
  1524. }
  1525. /* Now that we have accurate counts, iterate over the elements in the
  1526. hash table and see if any need too many insertions relative to the
  1527. number of evaluations that can be removed. If so, mark them in
  1528. PRUNE_EXPRS. */
  1529. for (j = 0; j < (unsigned) n_elems; j++)
  1530. if (deletions[j]
  1531. && ((unsigned) insertions[j] / deletions[j]) > MAX_GCSE_INSERTION_RATIO)
  1532. bitmap_set_bit (prune_exprs, j);
  1533. /* Now prune PRE_INSERT_MAP and PRE_DELETE_MAP based on PRUNE_EXPRS. */
  1534. EXECUTE_IF_SET_IN_BITMAP (prune_exprs, 0, j, sbi)
  1535. {
  1536. for (i = 0; i < (unsigned) n_edges_for_fn (cfun); i++)
  1537. bitmap_clear_bit (pre_insert_map[i], j);
  1538. for (i = 0; i < (unsigned) last_basic_block_for_fn (cfun); i++)
  1539. bitmap_clear_bit (pre_delete_map[i], j);
  1540. }
  1541. sbitmap_free (prune_exprs);
  1542. free (insertions);
  1543. free (deletions);
  1544. }
  1545. /* Top level routine to do the dataflow analysis needed by PRE. */
  1546. static struct edge_list *
  1547. compute_pre_data (void)
  1548. {
  1549. struct edge_list *edge_list;
  1550. basic_block bb;
  1551. compute_local_properties (transp, comp, antloc, &expr_hash_table);
  1552. prune_expressions (true);
  1553. bitmap_vector_clear (ae_kill, last_basic_block_for_fn (cfun));
  1554. /* Compute ae_kill for each basic block using:
  1555. ~(TRANSP | COMP)
  1556. */
  1557. FOR_EACH_BB_FN (bb, cfun)
  1558. {
  1559. bitmap_ior (ae_kill[bb->index], transp[bb->index], comp[bb->index]);
  1560. bitmap_not (ae_kill[bb->index], ae_kill[bb->index]);
  1561. }
  1562. edge_list = pre_edge_lcm (expr_hash_table.n_elems, transp, comp, antloc,
  1563. ae_kill, &pre_insert_map, &pre_delete_map);
  1564. sbitmap_vector_free (antloc);
  1565. antloc = NULL;
  1566. sbitmap_vector_free (ae_kill);
  1567. ae_kill = NULL;
  1568. prune_insertions_deletions (expr_hash_table.n_elems);
  1569. return edge_list;
  1570. }
  1571. /* PRE utilities */
  1572. /* Return nonzero if an occurrence of expression EXPR in OCCR_BB would reach
  1573. block BB.
  1574. VISITED is a pointer to a working buffer for tracking which BB's have
  1575. been visited. It is NULL for the top-level call.
  1576. We treat reaching expressions that go through blocks containing the same
  1577. reaching expression as "not reaching". E.g. if EXPR is generated in blocks
  1578. 2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
  1579. 2 as not reaching. The intent is to improve the probability of finding
  1580. only one reaching expression and to reduce register lifetimes by picking
  1581. the closest such expression. */
  1582. static int
  1583. pre_expr_reaches_here_p_work (basic_block occr_bb, struct gcse_expr *expr,
  1584. basic_block bb, char *visited)
  1585. {
  1586. edge pred;
  1587. edge_iterator ei;
  1588. FOR_EACH_EDGE (pred, ei, bb->preds)
  1589. {
  1590. basic_block pred_bb = pred->src;
  1591. if (pred->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
  1592. /* Has predecessor has already been visited? */
  1593. || visited[pred_bb->index])
  1594. ;/* Nothing to do. */
  1595. /* Does this predecessor generate this expression? */
  1596. else if (bitmap_bit_p (comp[pred_bb->index], expr->bitmap_index))
  1597. {
  1598. /* Is this the occurrence we're looking for?
  1599. Note that there's only one generating occurrence per block
  1600. so we just need to check the block number. */
  1601. if (occr_bb == pred_bb)
  1602. return 1;
  1603. visited[pred_bb->index] = 1;
  1604. }
  1605. /* Ignore this predecessor if it kills the expression. */
  1606. else if (! bitmap_bit_p (transp[pred_bb->index], expr->bitmap_index))
  1607. visited[pred_bb->index] = 1;
  1608. /* Neither gen nor kill. */
  1609. else
  1610. {
  1611. visited[pred_bb->index] = 1;
  1612. if (pre_expr_reaches_here_p_work (occr_bb, expr, pred_bb, visited))
  1613. return 1;
  1614. }
  1615. }
  1616. /* All paths have been checked. */
  1617. return 0;
  1618. }
  1619. /* The wrapper for pre_expr_reaches_here_work that ensures that any
  1620. memory allocated for that function is returned. */
  1621. static int
  1622. pre_expr_reaches_here_p (basic_block occr_bb, struct gcse_expr *expr, basic_block bb)
  1623. {
  1624. int rval;
  1625. char *visited = XCNEWVEC (char, last_basic_block_for_fn (cfun));
  1626. rval = pre_expr_reaches_here_p_work (occr_bb, expr, bb, visited);
  1627. free (visited);
  1628. return rval;
  1629. }
  1630. /* Generate RTL to copy an EXPR to its `reaching_reg' and return it. */
  1631. static rtx_insn *
  1632. process_insert_insn (struct gcse_expr *expr)
  1633. {
  1634. rtx reg = expr->reaching_reg;
  1635. /* Copy the expression to make sure we don't have any sharing issues. */
  1636. rtx exp = copy_rtx (expr->expr);
  1637. rtx_insn *pat;
  1638. start_sequence ();
  1639. /* If the expression is something that's an operand, like a constant,
  1640. just copy it to a register. */
  1641. if (general_operand (exp, GET_MODE (reg)))
  1642. emit_move_insn (reg, exp);
  1643. /* Otherwise, make a new insn to compute this expression and make sure the
  1644. insn will be recognized (this also adds any needed CLOBBERs). */
  1645. else
  1646. {
  1647. rtx_insn *insn = emit_insn (gen_rtx_SET (VOIDmode, reg, exp));
  1648. if (insn_invalid_p (insn, false))
  1649. gcc_unreachable ();
  1650. }
  1651. pat = get_insns ();
  1652. end_sequence ();
  1653. return pat;
  1654. }
  1655. /* Add EXPR to the end of basic block BB.
  1656. This is used by both the PRE and code hoisting. */
  1657. static void
  1658. insert_insn_end_basic_block (struct gcse_expr *expr, basic_block bb)
  1659. {
  1660. rtx_insn *insn = BB_END (bb);
  1661. rtx_insn *new_insn;
  1662. rtx reg = expr->reaching_reg;
  1663. int regno = REGNO (reg);
  1664. rtx_insn *pat, *pat_end;
  1665. pat = process_insert_insn (expr);
  1666. gcc_assert (pat && INSN_P (pat));
  1667. pat_end = pat;
  1668. while (NEXT_INSN (pat_end) != NULL_RTX)
  1669. pat_end = NEXT_INSN (pat_end);
  1670. /* If the last insn is a jump, insert EXPR in front [taking care to
  1671. handle cc0, etc. properly]. Similarly we need to care trapping
  1672. instructions in presence of non-call exceptions. */
  1673. if (JUMP_P (insn)
  1674. || (NONJUMP_INSN_P (insn)
  1675. && (!single_succ_p (bb)
  1676. || single_succ_edge (bb)->flags & EDGE_ABNORMAL)))
  1677. {
  1678. #ifdef HAVE_cc0
  1679. /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts
  1680. if cc0 isn't set. */
  1681. rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
  1682. if (note)
  1683. insn = safe_as_a <rtx_insn *> (XEXP (note, 0));
  1684. else
  1685. {
  1686. rtx_insn *maybe_cc0_setter = prev_nonnote_insn (insn);
  1687. if (maybe_cc0_setter
  1688. && INSN_P (maybe_cc0_setter)
  1689. && sets_cc0_p (PATTERN (maybe_cc0_setter)))
  1690. insn = maybe_cc0_setter;
  1691. }
  1692. #endif
  1693. /* FIXME: What if something in cc0/jump uses value set in new insn? */
  1694. new_insn = emit_insn_before_noloc (pat, insn, bb);
  1695. }
  1696. /* Likewise if the last insn is a call, as will happen in the presence
  1697. of exception handling. */
  1698. else if (CALL_P (insn)
  1699. && (!single_succ_p (bb)
  1700. || single_succ_edge (bb)->flags & EDGE_ABNORMAL))
  1701. {
  1702. /* Keeping in mind targets with small register classes and parameters
  1703. in registers, we search backward and place the instructions before
  1704. the first parameter is loaded. Do this for everyone for consistency
  1705. and a presumption that we'll get better code elsewhere as well. */
  1706. /* Since different machines initialize their parameter registers
  1707. in different orders, assume nothing. Collect the set of all
  1708. parameter registers. */
  1709. insn = find_first_parameter_load (insn, BB_HEAD (bb));
  1710. /* If we found all the parameter loads, then we want to insert
  1711. before the first parameter load.
  1712. If we did not find all the parameter loads, then we might have
  1713. stopped on the head of the block, which could be a CODE_LABEL.
  1714. If we inserted before the CODE_LABEL, then we would be putting
  1715. the insn in the wrong basic block. In that case, put the insn
  1716. after the CODE_LABEL. Also, respect NOTE_INSN_BASIC_BLOCK. */
  1717. while (LABEL_P (insn)
  1718. || NOTE_INSN_BASIC_BLOCK_P (insn))
  1719. insn = NEXT_INSN (insn);
  1720. new_insn = emit_insn_before_noloc (pat, insn, bb);
  1721. }
  1722. else
  1723. new_insn = emit_insn_after_noloc (pat, insn, bb);
  1724. while (1)
  1725. {
  1726. if (INSN_P (pat))
  1727. add_label_notes (PATTERN (pat), new_insn);
  1728. if (pat == pat_end)
  1729. break;
  1730. pat = NEXT_INSN (pat);
  1731. }
  1732. gcse_create_count++;
  1733. if (dump_file)
  1734. {
  1735. fprintf (dump_file, "PRE/HOIST: end of bb %d, insn %d, ",
  1736. bb->index, INSN_UID (new_insn));
  1737. fprintf (dump_file, "copying expression %d to reg %d\n",
  1738. expr->bitmap_index, regno);
  1739. }
  1740. }
  1741. /* Insert partially redundant expressions on edges in the CFG to make
  1742. the expressions fully redundant. */
  1743. static int
  1744. pre_edge_insert (struct edge_list *edge_list, struct gcse_expr **index_map)
  1745. {
  1746. int e, i, j, num_edges, set_size, did_insert = 0;
  1747. sbitmap *inserted;
  1748. /* Where PRE_INSERT_MAP is nonzero, we add the expression on that edge
  1749. if it reaches any of the deleted expressions. */
  1750. set_size = pre_insert_map[0]->size;
  1751. num_edges = NUM_EDGES (edge_list);
  1752. inserted = sbitmap_vector_alloc (num_edges, expr_hash_table.n_elems);
  1753. bitmap_vector_clear (inserted, num_edges);
  1754. for (e = 0; e < num_edges; e++)
  1755. {
  1756. int indx;
  1757. basic_block bb = INDEX_EDGE_PRED_BB (edge_list, e);
  1758. for (i = indx = 0; i < set_size; i++, indx += SBITMAP_ELT_BITS)
  1759. {
  1760. SBITMAP_ELT_TYPE insert = pre_insert_map[e]->elms[i];
  1761. for (j = indx;
  1762. insert && j < (int) expr_hash_table.n_elems;
  1763. j++, insert >>= 1)
  1764. if ((insert & 1) != 0 && index_map[j]->reaching_reg != NULL_RTX)
  1765. {
  1766. struct gcse_expr *expr = index_map[j];
  1767. struct gcse_occr *occr;
  1768. /* Now look at each deleted occurrence of this expression. */
  1769. for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
  1770. {
  1771. if (! occr->deleted_p)
  1772. continue;
  1773. /* Insert this expression on this edge if it would
  1774. reach the deleted occurrence in BB. */
  1775. if (!bitmap_bit_p (inserted[e], j))
  1776. {
  1777. rtx_insn *insn;
  1778. edge eg = INDEX_EDGE (edge_list, e);
  1779. /* We can't insert anything on an abnormal and
  1780. critical edge, so we insert the insn at the end of
  1781. the previous block. There are several alternatives
  1782. detailed in Morgans book P277 (sec 10.5) for
  1783. handling this situation. This one is easiest for
  1784. now. */
  1785. if (eg->flags & EDGE_ABNORMAL)
  1786. insert_insn_end_basic_block (index_map[j], bb);
  1787. else
  1788. {
  1789. insn = process_insert_insn (index_map[j]);
  1790. insert_insn_on_edge (insn, eg);
  1791. }
  1792. if (dump_file)
  1793. {
  1794. fprintf (dump_file, "PRE: edge (%d,%d), ",
  1795. bb->index,
  1796. INDEX_EDGE_SUCC_BB (edge_list, e)->index);
  1797. fprintf (dump_file, "copy expression %d\n",
  1798. expr->bitmap_index);
  1799. }
  1800. update_ld_motion_stores (expr);
  1801. bitmap_set_bit (inserted[e], j);
  1802. did_insert = 1;
  1803. gcse_create_count++;
  1804. }
  1805. }
  1806. }
  1807. }
  1808. }
  1809. sbitmap_vector_free (inserted);
  1810. return did_insert;
  1811. }
  1812. /* Copy the result of EXPR->EXPR generated by INSN to EXPR->REACHING_REG.
  1813. Given "old_reg <- expr" (INSN), instead of adding after it
  1814. reaching_reg <- old_reg
  1815. it's better to do the following:
  1816. reaching_reg <- expr
  1817. old_reg <- reaching_reg
  1818. because this way copy propagation can discover additional PRE
  1819. opportunities. But if this fails, we try the old way.
  1820. When "expr" is a store, i.e.
  1821. given "MEM <- old_reg", instead of adding after it
  1822. reaching_reg <- old_reg
  1823. it's better to add it before as follows:
  1824. reaching_reg <- old_reg
  1825. MEM <- reaching_reg. */
  1826. static void
  1827. pre_insert_copy_insn (struct gcse_expr *expr, rtx_insn *insn)
  1828. {
  1829. rtx reg = expr->reaching_reg;
  1830. int regno = REGNO (reg);
  1831. int indx = expr->bitmap_index;
  1832. rtx pat = PATTERN (insn);
  1833. rtx set, first_set, new_insn;
  1834. rtx old_reg;
  1835. int i;
  1836. /* This block matches the logic in hash_scan_insn. */
  1837. switch (GET_CODE (pat))
  1838. {
  1839. case SET:
  1840. set = pat;
  1841. break;
  1842. case PARALLEL:
  1843. /* Search through the parallel looking for the set whose
  1844. source was the expression that we're interested in. */
  1845. first_set = NULL_RTX;
  1846. set = NULL_RTX;
  1847. for (i = 0; i < XVECLEN (pat, 0); i++)
  1848. {
  1849. rtx x = XVECEXP (pat, 0, i);
  1850. if (GET_CODE (x) == SET)
  1851. {
  1852. /* If the source was a REG_EQUAL or REG_EQUIV note, we
  1853. may not find an equivalent expression, but in this
  1854. case the PARALLEL will have a single set. */
  1855. if (first_set == NULL_RTX)
  1856. first_set = x;
  1857. if (expr_equiv_p (SET_SRC (x), expr->expr))
  1858. {
  1859. set = x;
  1860. break;
  1861. }
  1862. }
  1863. }
  1864. gcc_assert (first_set);
  1865. if (set == NULL_RTX)
  1866. set = first_set;
  1867. break;
  1868. default:
  1869. gcc_unreachable ();
  1870. }
  1871. if (REG_P (SET_DEST (set)))
  1872. {
  1873. old_reg = SET_DEST (set);
  1874. /* Check if we can modify the set destination in the original insn. */
  1875. if (validate_change (insn, &SET_DEST (set), reg, 0))
  1876. {
  1877. new_insn = gen_move_insn (old_reg, reg);
  1878. new_insn = emit_insn_after (new_insn, insn);
  1879. }
  1880. else
  1881. {
  1882. new_insn = gen_move_insn (reg, old_reg);
  1883. new_insn = emit_insn_after (new_insn, insn);
  1884. }
  1885. }
  1886. else /* This is possible only in case of a store to memory. */
  1887. {
  1888. old_reg = SET_SRC (set);
  1889. new_insn = gen_move_insn (reg, old_reg);
  1890. /* Check if we can modify the set source in the original insn. */
  1891. if (validate_change (insn, &SET_SRC (set), reg, 0))
  1892. new_insn = emit_insn_before (new_insn, insn);
  1893. else
  1894. new_insn = emit_insn_after (new_insn, insn);
  1895. }
  1896. gcse_create_count++;
  1897. if (dump_file)
  1898. fprintf (dump_file,
  1899. "PRE: bb %d, insn %d, copy expression %d in insn %d to reg %d\n",
  1900. BLOCK_FOR_INSN (insn)->index, INSN_UID (new_insn), indx,
  1901. INSN_UID (insn), regno);
  1902. }
  1903. /* Copy available expressions that reach the redundant expression
  1904. to `reaching_reg'. */
  1905. static void
  1906. pre_insert_copies (void)
  1907. {
  1908. unsigned int i, added_copy;
  1909. struct gcse_expr *expr;
  1910. struct gcse_occr *occr;
  1911. struct gcse_occr *avail;
  1912. /* For each available expression in the table, copy the result to
  1913. `reaching_reg' if the expression reaches a deleted one.
  1914. ??? The current algorithm is rather brute force.
  1915. Need to do some profiling. */
  1916. for (i = 0; i < expr_hash_table.size; i++)
  1917. for (expr = expr_hash_table.table[i]; expr; expr = expr->next_same_hash)
  1918. {
  1919. /* If the basic block isn't reachable, PPOUT will be TRUE. However,
  1920. we don't want to insert a copy here because the expression may not
  1921. really be redundant. So only insert an insn if the expression was
  1922. deleted. This test also avoids further processing if the
  1923. expression wasn't deleted anywhere. */
  1924. if (expr->reaching_reg == NULL)
  1925. continue;
  1926. /* Set when we add a copy for that expression. */
  1927. added_copy = 0;
  1928. for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
  1929. {
  1930. if (! occr->deleted_p)
  1931. continue;
  1932. for (avail = expr->avail_occr; avail != NULL; avail = avail->next)
  1933. {
  1934. rtx_insn *insn = avail->insn;
  1935. /* No need to handle this one if handled already. */
  1936. if (avail->copied_p)
  1937. continue;
  1938. /* Don't handle this one if it's a redundant one. */
  1939. if (insn->deleted ())
  1940. continue;
  1941. /* Or if the expression doesn't reach the deleted one. */
  1942. if (! pre_expr_reaches_here_p (BLOCK_FOR_INSN (avail->insn),
  1943. expr,
  1944. BLOCK_FOR_INSN (occr->insn)))
  1945. continue;
  1946. added_copy = 1;
  1947. /* Copy the result of avail to reaching_reg. */
  1948. pre_insert_copy_insn (expr, insn);
  1949. avail->copied_p = 1;
  1950. }
  1951. }
  1952. if (added_copy)
  1953. update_ld_motion_stores (expr);
  1954. }
  1955. }
  1956. struct set_data
  1957. {
  1958. rtx_insn *insn;
  1959. const_rtx set;
  1960. int nsets;
  1961. };
  1962. /* Increment number of sets and record set in DATA. */
  1963. static void
  1964. record_set_data (rtx dest, const_rtx set, void *data)
  1965. {
  1966. struct set_data *s = (struct set_data *)data;
  1967. if (GET_CODE (set) == SET)
  1968. {
  1969. /* We allow insns having multiple sets, where all but one are
  1970. dead as single set insns. In the common case only a single
  1971. set is present, so we want to avoid checking for REG_UNUSED
  1972. notes unless necessary. */
  1973. if (s->nsets == 1
  1974. && find_reg_note (s->insn, REG_UNUSED, SET_DEST (s->set))
  1975. && !side_effects_p (s->set))
  1976. s->nsets = 0;
  1977. if (!s->nsets)
  1978. {
  1979. /* Record this set. */
  1980. s->nsets += 1;
  1981. s->set = set;
  1982. }
  1983. else if (!find_reg_note (s->insn, REG_UNUSED, dest)
  1984. || side_effects_p (set))
  1985. s->nsets += 1;
  1986. }
  1987. }
  1988. static const_rtx
  1989. single_set_gcse (rtx_insn *insn)
  1990. {
  1991. struct set_data s;
  1992. rtx pattern;
  1993. gcc_assert (INSN_P (insn));
  1994. /* Optimize common case. */
  1995. pattern = PATTERN (insn);
  1996. if (GET_CODE (pattern) == SET)
  1997. return pattern;
  1998. s.insn = insn;
  1999. s.nsets = 0;
  2000. note_stores (pattern, record_set_data, &s);
  2001. /* Considered invariant insns have exactly one set. */
  2002. gcc_assert (s.nsets == 1);
  2003. return s.set;
  2004. }
  2005. /* Emit move from SRC to DEST noting the equivalence with expression computed
  2006. in INSN. */
  2007. static rtx
  2008. gcse_emit_move_after (rtx dest, rtx src, rtx_insn *insn)
  2009. {
  2010. rtx_insn *new_rtx;
  2011. const_rtx set = single_set_gcse (insn);
  2012. rtx set2;
  2013. rtx note;
  2014. rtx eqv = NULL_RTX;
  2015. /* This should never fail since we're creating a reg->reg copy
  2016. we've verified to be valid. */
  2017. new_rtx = emit_insn_after (gen_move_insn (dest, src), insn);
  2018. /* Note the equivalence for local CSE pass. Take the note from the old
  2019. set if there was one. Otherwise record the SET_SRC from the old set
  2020. unless DEST is also an operand of the SET_SRC. */
  2021. set2 = single_set (new_rtx);
  2022. if (!set2 || !rtx_equal_p (SET_DEST (set2), dest))
  2023. return new_rtx;
  2024. if ((note = find_reg_equal_equiv_note (insn)))
  2025. eqv = XEXP (note, 0);
  2026. else if (! REG_P (dest)
  2027. || ! reg_mentioned_p (dest, SET_SRC (set)))
  2028. eqv = SET_SRC (set);
  2029. if (eqv != NULL_RTX)
  2030. set_unique_reg_note (new_rtx, REG_EQUAL, copy_insn_1 (eqv));
  2031. return new_rtx;
  2032. }
  2033. /* Delete redundant computations.
  2034. Deletion is done by changing the insn to copy the `reaching_reg' of
  2035. the expression into the result of the SET. It is left to later passes
  2036. to propagate the copy or eliminate it.
  2037. Return nonzero if a change is made. */
  2038. static int
  2039. pre_delete (void)
  2040. {
  2041. unsigned int i;
  2042. int changed;
  2043. struct gcse_expr *expr;
  2044. struct gcse_occr *occr;
  2045. changed = 0;
  2046. for (i = 0; i < expr_hash_table.size; i++)
  2047. for (expr = expr_hash_table.table[i]; expr; expr = expr->next_same_hash)
  2048. {
  2049. int indx = expr->bitmap_index;
  2050. /* We only need to search antic_occr since we require ANTLOC != 0. */
  2051. for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
  2052. {
  2053. rtx_insn *insn = occr->insn;
  2054. rtx set;
  2055. basic_block bb = BLOCK_FOR_INSN (insn);
  2056. /* We only delete insns that have a single_set. */
  2057. if (bitmap_bit_p (pre_delete_map[bb->index], indx)
  2058. && (set = single_set (insn)) != 0
  2059. && dbg_cnt (pre_insn))
  2060. {
  2061. /* Create a pseudo-reg to store the result of reaching
  2062. expressions into. Get the mode for the new pseudo from
  2063. the mode of the original destination pseudo. */
  2064. if (expr->reaching_reg == NULL)
  2065. expr->reaching_reg = gen_reg_rtx_and_attrs (SET_DEST (set));
  2066. gcse_emit_move_after (SET_DEST (set), expr->reaching_reg, insn);
  2067. delete_insn (insn);
  2068. occr->deleted_p = 1;
  2069. changed = 1;
  2070. gcse_subst_count++;
  2071. if (dump_file)
  2072. {
  2073. fprintf (dump_file,
  2074. "PRE: redundant insn %d (expression %d) in ",
  2075. INSN_UID (insn), indx);
  2076. fprintf (dump_file, "bb %d, reaching reg is %d\n",
  2077. bb->index, REGNO (expr->reaching_reg));
  2078. }
  2079. }
  2080. }
  2081. }
  2082. return changed;
  2083. }
  2084. /* Perform GCSE optimizations using PRE.
  2085. This is called by one_pre_gcse_pass after all the dataflow analysis
  2086. has been done.
  2087. This is based on the original Morel-Renvoise paper Fred Chow's thesis, and
  2088. lazy code motion from Knoop, Ruthing and Steffen as described in Advanced
  2089. Compiler Design and Implementation.
  2090. ??? A new pseudo reg is created to hold the reaching expression. The nice
  2091. thing about the classical approach is that it would try to use an existing
  2092. reg. If the register can't be adequately optimized [i.e. we introduce
  2093. reload problems], one could add a pass here to propagate the new register
  2094. through the block.
  2095. ??? We don't handle single sets in PARALLELs because we're [currently] not
  2096. able to copy the rest of the parallel when we insert copies to create full
  2097. redundancies from partial redundancies. However, there's no reason why we
  2098. can't handle PARALLELs in the cases where there are no partial
  2099. redundancies. */
  2100. static int
  2101. pre_gcse (struct edge_list *edge_list)
  2102. {
  2103. unsigned int i;
  2104. int did_insert, changed;
  2105. struct gcse_expr **index_map;
  2106. struct gcse_expr *expr;
  2107. /* Compute a mapping from expression number (`bitmap_index') to
  2108. hash table entry. */
  2109. index_map = XCNEWVEC (struct gcse_expr *, expr_hash_table.n_elems);
  2110. for (i = 0; i < expr_hash_table.size; i++)
  2111. for (expr = expr_hash_table.table[i]; expr; expr = expr->next_same_hash)
  2112. index_map[expr->bitmap_index] = expr;
  2113. /* Delete the redundant insns first so that
  2114. - we know what register to use for the new insns and for the other
  2115. ones with reaching expressions
  2116. - we know which insns are redundant when we go to create copies */
  2117. changed = pre_delete ();
  2118. did_insert = pre_edge_insert (edge_list, index_map);
  2119. /* In other places with reaching expressions, copy the expression to the
  2120. specially allocated pseudo-reg that reaches the redundant expr. */
  2121. pre_insert_copies ();
  2122. if (did_insert)
  2123. {
  2124. commit_edge_insertions ();
  2125. changed = 1;
  2126. }
  2127. free (index_map);
  2128. return changed;
  2129. }
  2130. /* Top level routine to perform one PRE GCSE pass.
  2131. Return nonzero if a change was made. */
  2132. static int
  2133. one_pre_gcse_pass (void)
  2134. {
  2135. int changed = 0;
  2136. gcse_subst_count = 0;
  2137. gcse_create_count = 0;
  2138. /* Return if there's nothing to do, or it is too expensive. */
  2139. if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
  2140. || is_too_expensive (_("PRE disabled")))
  2141. return 0;
  2142. /* We need alias. */
  2143. init_alias_analysis ();
  2144. bytes_used = 0;
  2145. gcc_obstack_init (&gcse_obstack);
  2146. alloc_gcse_mem ();
  2147. alloc_hash_table (&expr_hash_table);
  2148. add_noreturn_fake_exit_edges ();
  2149. if (flag_gcse_lm)
  2150. compute_ld_motion_mems ();
  2151. compute_hash_table (&expr_hash_table);
  2152. if (flag_gcse_lm)
  2153. trim_ld_motion_mems ();
  2154. if (dump_file)
  2155. dump_hash_table (dump_file, "Expression", &expr_hash_table);
  2156. if (expr_hash_table.n_elems > 0)
  2157. {
  2158. struct edge_list *edge_list;
  2159. alloc_pre_mem (last_basic_block_for_fn (cfun), expr_hash_table.n_elems);
  2160. edge_list = compute_pre_data ();
  2161. changed |= pre_gcse (edge_list);
  2162. free_edge_list (edge_list);
  2163. free_pre_mem ();
  2164. }
  2165. if (flag_gcse_lm)
  2166. free_ld_motion_mems ();
  2167. remove_fake_exit_edges ();
  2168. free_hash_table (&expr_hash_table);
  2169. free_gcse_mem ();
  2170. obstack_free (&gcse_obstack, NULL);
  2171. /* We are finished with alias. */
  2172. end_alias_analysis ();
  2173. if (dump_file)
  2174. {
  2175. fprintf (dump_file, "PRE GCSE of %s, %d basic blocks, %d bytes needed, ",
  2176. current_function_name (), n_basic_blocks_for_fn (cfun),
  2177. bytes_used);
  2178. fprintf (dump_file, "%d substs, %d insns created\n",
  2179. gcse_subst_count, gcse_create_count);
  2180. }
  2181. return changed;
  2182. }
  2183. /* If X contains any LABEL_REF's, add REG_LABEL_OPERAND notes for them
  2184. to INSN. If such notes are added to an insn which references a
  2185. CODE_LABEL, the LABEL_NUSES count is incremented. We have to add
  2186. that note, because the following loop optimization pass requires
  2187. them. */
  2188. /* ??? If there was a jump optimization pass after gcse and before loop,
  2189. then we would not need to do this here, because jump would add the
  2190. necessary REG_LABEL_OPERAND and REG_LABEL_TARGET notes. */
  2191. static void
  2192. add_label_notes (rtx x, rtx insn)
  2193. {
  2194. enum rtx_code code = GET_CODE (x);
  2195. int i, j;
  2196. const char *fmt;
  2197. if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
  2198. {
  2199. /* This code used to ignore labels that referred to dispatch tables to
  2200. avoid flow generating (slightly) worse code.
  2201. We no longer ignore such label references (see LABEL_REF handling in
  2202. mark_jump_label for additional information). */
  2203. /* There's no reason for current users to emit jump-insns with
  2204. such a LABEL_REF, so we don't have to handle REG_LABEL_TARGET
  2205. notes. */
  2206. gcc_assert (!JUMP_P (insn));
  2207. add_reg_note (insn, REG_LABEL_OPERAND, LABEL_REF_LABEL (x));
  2208. if (LABEL_P (LABEL_REF_LABEL (x)))
  2209. LABEL_NUSES (LABEL_REF_LABEL (x))++;
  2210. return;
  2211. }
  2212. for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
  2213. {
  2214. if (fmt[i] == 'e')
  2215. add_label_notes (XEXP (x, i), insn);
  2216. else if (fmt[i] == 'E')
  2217. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  2218. add_label_notes (XVECEXP (x, i, j), insn);
  2219. }
  2220. }
  2221. /* Code Hoisting variables and subroutines. */
  2222. /* Very busy expressions. */
  2223. static sbitmap *hoist_vbein;
  2224. static sbitmap *hoist_vbeout;
  2225. /* ??? We could compute post dominators and run this algorithm in
  2226. reverse to perform tail merging, doing so would probably be
  2227. more effective than the tail merging code in jump.c.
  2228. It's unclear if tail merging could be run in parallel with
  2229. code hoisting. It would be nice. */
  2230. /* Allocate vars used for code hoisting analysis. */
  2231. static void
  2232. alloc_code_hoist_mem (int n_blocks, int n_exprs)
  2233. {
  2234. antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
  2235. transp = sbitmap_vector_alloc (n_blocks, n_exprs);
  2236. comp = sbitmap_vector_alloc (n_blocks, n_exprs);
  2237. hoist_vbein = sbitmap_vector_alloc (n_blocks, n_exprs);
  2238. hoist_vbeout = sbitmap_vector_alloc (n_blocks, n_exprs);
  2239. }
  2240. /* Free vars used for code hoisting analysis. */
  2241. static void
  2242. free_code_hoist_mem (void)
  2243. {
  2244. sbitmap_vector_free (antloc);
  2245. sbitmap_vector_free (transp);
  2246. sbitmap_vector_free (comp);
  2247. sbitmap_vector_free (hoist_vbein);
  2248. sbitmap_vector_free (hoist_vbeout);
  2249. free_dominance_info (CDI_DOMINATORS);
  2250. }
  2251. /* Compute the very busy expressions at entry/exit from each block.
  2252. An expression is very busy if all paths from a given point
  2253. compute the expression. */
  2254. static void
  2255. compute_code_hoist_vbeinout (void)
  2256. {
  2257. int changed, passes;
  2258. basic_block bb;
  2259. bitmap_vector_clear (hoist_vbeout, last_basic_block_for_fn (cfun));
  2260. bitmap_vector_clear (hoist_vbein, last_basic_block_for_fn (cfun));
  2261. passes = 0;
  2262. changed = 1;
  2263. while (changed)
  2264. {
  2265. changed = 0;
  2266. /* We scan the blocks in the reverse order to speed up
  2267. the convergence. */
  2268. FOR_EACH_BB_REVERSE_FN (bb, cfun)
  2269. {
  2270. if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
  2271. {
  2272. bitmap_intersection_of_succs (hoist_vbeout[bb->index],
  2273. hoist_vbein, bb);
  2274. /* Include expressions in VBEout that are calculated
  2275. in BB and available at its end. */
  2276. bitmap_ior (hoist_vbeout[bb->index],
  2277. hoist_vbeout[bb->index], comp[bb->index]);
  2278. }
  2279. changed |= bitmap_or_and (hoist_vbein[bb->index],
  2280. antloc[bb->index],
  2281. hoist_vbeout[bb->index],
  2282. transp[bb->index]);
  2283. }
  2284. passes++;
  2285. }
  2286. if (dump_file)
  2287. {
  2288. fprintf (dump_file, "hoisting vbeinout computation: %d passes\n", passes);
  2289. FOR_EACH_BB_FN (bb, cfun)
  2290. {
  2291. fprintf (dump_file, "vbein (%d): ", bb->index);
  2292. dump_bitmap_file (dump_file, hoist_vbein[bb->index]);
  2293. fprintf (dump_file, "vbeout(%d): ", bb->index);
  2294. dump_bitmap_file (dump_file, hoist_vbeout[bb->index]);
  2295. }
  2296. }
  2297. }
  2298. /* Top level routine to do the dataflow analysis needed by code hoisting. */
  2299. static void
  2300. compute_code_hoist_data (void)
  2301. {
  2302. compute_local_properties (transp, comp, antloc, &expr_hash_table);
  2303. prune_expressions (false);
  2304. compute_code_hoist_vbeinout ();
  2305. calculate_dominance_info (CDI_DOMINATORS);
  2306. if (dump_file)
  2307. fprintf (dump_file, "\n");
  2308. }
  2309. /* Update register pressure for BB when hoisting an expression from
  2310. instruction FROM, if live ranges of inputs are shrunk. Also
  2311. maintain live_in information if live range of register referred
  2312. in FROM is shrunk.
  2313. Return 0 if register pressure doesn't change, otherwise return
  2314. the number by which register pressure is decreased.
  2315. NOTE: Register pressure won't be increased in this function. */
  2316. static int
  2317. update_bb_reg_pressure (basic_block bb, rtx_insn *from)
  2318. {
  2319. rtx dreg;
  2320. rtx_insn *insn;
  2321. basic_block succ_bb;
  2322. df_ref use, op_ref;
  2323. edge succ;
  2324. edge_iterator ei;
  2325. int decreased_pressure = 0;
  2326. int nregs;
  2327. enum reg_class pressure_class;
  2328. FOR_EACH_INSN_USE (use, from)
  2329. {
  2330. dreg = DF_REF_REAL_REG (use);
  2331. /* The live range of register is shrunk only if it isn't:
  2332. 1. referred on any path from the end of this block to EXIT, or
  2333. 2. referred by insns other than FROM in this block. */
  2334. FOR_EACH_EDGE (succ, ei, bb->succs)
  2335. {
  2336. succ_bb = succ->dest;
  2337. if (succ_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
  2338. continue;
  2339. if (bitmap_bit_p (BB_DATA (succ_bb)->live_in, REGNO (dreg)))
  2340. break;
  2341. }
  2342. if (succ != NULL)
  2343. continue;
  2344. op_ref = DF_REG_USE_CHAIN (REGNO (dreg));
  2345. for (; op_ref; op_ref = DF_REF_NEXT_REG (op_ref))
  2346. {
  2347. if (!DF_REF_INSN_INFO (op_ref))
  2348. continue;
  2349. insn = DF_REF_INSN (op_ref);
  2350. if (BLOCK_FOR_INSN (insn) == bb
  2351. && NONDEBUG_INSN_P (insn) && insn != from)
  2352. break;
  2353. }
  2354. pressure_class = get_regno_pressure_class (REGNO (dreg), &nregs);
  2355. /* Decrease register pressure and update live_in information for
  2356. this block. */
  2357. if (!op_ref && pressure_class != NO_REGS)
  2358. {
  2359. decreased_pressure += nregs;
  2360. BB_DATA (bb)->max_reg_pressure[pressure_class] -= nregs;
  2361. bitmap_clear_bit (BB_DATA (bb)->live_in, REGNO (dreg));
  2362. }
  2363. }
  2364. return decreased_pressure;
  2365. }
  2366. /* Determine if the expression EXPR should be hoisted to EXPR_BB up in
  2367. flow graph, if it can reach BB unimpared. Stop the search if the
  2368. expression would need to be moved more than DISTANCE instructions.
  2369. DISTANCE is the number of instructions through which EXPR can be
  2370. hoisted up in flow graph.
  2371. BB_SIZE points to an array which contains the number of instructions
  2372. for each basic block.
  2373. PRESSURE_CLASS and NREGS are register class and number of hard registers
  2374. for storing EXPR.
  2375. HOISTED_BBS points to a bitmap indicating basic blocks through which
  2376. EXPR is hoisted.
  2377. FROM is the instruction from which EXPR is hoisted.
  2378. It's unclear exactly what Muchnick meant by "unimpared". It seems
  2379. to me that the expression must either be computed or transparent in
  2380. *every* block in the path(s) from EXPR_BB to BB. Any other definition
  2381. would allow the expression to be hoisted out of loops, even if
  2382. the expression wasn't a loop invariant.
  2383. Contrast this to reachability for PRE where an expression is
  2384. considered reachable if *any* path reaches instead of *all*
  2385. paths. */
  2386. static int
  2387. should_hoist_expr_to_dom (basic_block expr_bb, struct gcse_expr *expr,
  2388. basic_block bb, sbitmap visited, int distance,
  2389. int *bb_size, enum reg_class pressure_class,
  2390. int *nregs, bitmap hoisted_bbs, rtx_insn *from)
  2391. {
  2392. unsigned int i;
  2393. edge pred;
  2394. edge_iterator ei;
  2395. sbitmap_iterator sbi;
  2396. int visited_allocated_locally = 0;
  2397. int decreased_pressure = 0;
  2398. if (flag_ira_hoist_pressure)
  2399. {
  2400. /* Record old information of basic block BB when it is visited
  2401. at the first time. */
  2402. if (!bitmap_bit_p (hoisted_bbs, bb->index))
  2403. {
  2404. struct bb_data *data = BB_DATA (bb);
  2405. bitmap_copy (data->backup, data->live_in);
  2406. data->old_pressure = data->max_reg_pressure[pressure_class];
  2407. }
  2408. decreased_pressure = update_bb_reg_pressure (bb, from);
  2409. }
  2410. /* Terminate the search if distance, for which EXPR is allowed to move,
  2411. is exhausted. */
  2412. if (distance > 0)
  2413. {
  2414. if (flag_ira_hoist_pressure)
  2415. {
  2416. /* Prefer to hoist EXPR if register pressure is decreased. */
  2417. if (decreased_pressure > *nregs)
  2418. distance += bb_size[bb->index];
  2419. /* Let EXPR be hoisted through basic block at no cost if one
  2420. of following conditions is satisfied:
  2421. 1. The basic block has low register pressure.
  2422. 2. Register pressure won't be increases after hoisting EXPR.
  2423. Constant expressions is handled conservatively, because
  2424. hoisting constant expression aggressively results in worse
  2425. code. This decision is made by the observation of CSiBE
  2426. on ARM target, while it has no obvious effect on other
  2427. targets like x86, x86_64, mips and powerpc. */
  2428. else if (CONST_INT_P (expr->expr)
  2429. || (BB_DATA (bb)->max_reg_pressure[pressure_class]
  2430. >= ira_class_hard_regs_num[pressure_class]
  2431. && decreased_pressure < *nregs))
  2432. distance -= bb_size[bb->index];
  2433. }
  2434. else
  2435. distance -= bb_size[bb->index];
  2436. if (distance <= 0)
  2437. return 0;
  2438. }
  2439. else
  2440. gcc_assert (distance == 0);
  2441. if (visited == NULL)
  2442. {
  2443. visited_allocated_locally = 1;
  2444. visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
  2445. bitmap_clear (visited);
  2446. }
  2447. FOR_EACH_EDGE (pred, ei, bb->preds)
  2448. {
  2449. basic_block pred_bb = pred->src;
  2450. if (pred->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  2451. break;
  2452. else if (pred_bb == expr_bb)
  2453. continue;
  2454. else if (bitmap_bit_p (visited, pred_bb->index))
  2455. continue;
  2456. else if (! bitmap_bit_p (transp[pred_bb->index], expr->bitmap_index))
  2457. break;
  2458. /* Not killed. */
  2459. else
  2460. {
  2461. bitmap_set_bit (visited, pred_bb->index);
  2462. if (! should_hoist_expr_to_dom (expr_bb, expr, pred_bb,
  2463. visited, distance, bb_size,
  2464. pressure_class, nregs,
  2465. hoisted_bbs, from))
  2466. break;
  2467. }
  2468. }
  2469. if (visited_allocated_locally)
  2470. {
  2471. /* If EXPR can be hoisted to expr_bb, record basic blocks through
  2472. which EXPR is hoisted in hoisted_bbs. */
  2473. if (flag_ira_hoist_pressure && !pred)
  2474. {
  2475. /* Record the basic block from which EXPR is hoisted. */
  2476. bitmap_set_bit (visited, bb->index);
  2477. EXECUTE_IF_SET_IN_BITMAP (visited, 0, i, sbi)
  2478. bitmap_set_bit (hoisted_bbs, i);
  2479. }
  2480. sbitmap_free (visited);
  2481. }
  2482. return (pred == NULL);
  2483. }
  2484. /* Find occurrence in BB. */
  2485. static struct gcse_occr *
  2486. find_occr_in_bb (struct gcse_occr *occr, basic_block bb)
  2487. {
  2488. /* Find the right occurrence of this expression. */
  2489. while (occr && BLOCK_FOR_INSN (occr->insn) != bb)
  2490. occr = occr->next;
  2491. return occr;
  2492. }
  2493. /* Actually perform code hoisting.
  2494. The code hoisting pass can hoist multiple computations of the same
  2495. expression along dominated path to a dominating basic block, like
  2496. from b2/b3 to b1 as depicted below:
  2497. b1 ------
  2498. /\ |
  2499. / \ |
  2500. bx by distance
  2501. / \ |
  2502. / \ |
  2503. b2 b3 ------
  2504. Unfortunately code hoisting generally extends the live range of an
  2505. output pseudo register, which increases register pressure and hurts
  2506. register allocation. To address this issue, an attribute MAX_DISTANCE
  2507. is computed and attached to each expression. The attribute is computed
  2508. from rtx cost of the corresponding expression and it's used to control
  2509. how long the expression can be hoisted up in flow graph. As the
  2510. expression is hoisted up in flow graph, GCC decreases its DISTANCE
  2511. and stops the hoist if DISTANCE reaches 0. Code hoisting can decrease
  2512. register pressure if live ranges of inputs are shrunk.
  2513. Option "-fira-hoist-pressure" implements register pressure directed
  2514. hoist based on upper method. The rationale is:
  2515. 1. Calculate register pressure for each basic block by reusing IRA
  2516. facility.
  2517. 2. When expression is hoisted through one basic block, GCC checks
  2518. the change of live ranges for inputs/output. The basic block's
  2519. register pressure will be increased because of extended live
  2520. range of output. However, register pressure will be decreased
  2521. if the live ranges of inputs are shrunk.
  2522. 3. After knowing how hoisting affects register pressure, GCC prefers
  2523. to hoist the expression if it can decrease register pressure, by
  2524. increasing DISTANCE of the corresponding expression.
  2525. 4. If hoisting the expression increases register pressure, GCC checks
  2526. register pressure of the basic block and decrease DISTANCE only if
  2527. the register pressure is high. In other words, expression will be
  2528. hoisted through at no cost if the basic block has low register
  2529. pressure.
  2530. 5. Update register pressure information for basic blocks through
  2531. which expression is hoisted. */
  2532. static int
  2533. hoist_code (void)
  2534. {
  2535. basic_block bb, dominated;
  2536. vec<basic_block> dom_tree_walk;
  2537. unsigned int dom_tree_walk_index;
  2538. vec<basic_block> domby;
  2539. unsigned int i, j, k;
  2540. struct gcse_expr **index_map;
  2541. struct gcse_expr *expr;
  2542. int *to_bb_head;
  2543. int *bb_size;
  2544. int changed = 0;
  2545. struct bb_data *data;
  2546. /* Basic blocks that have occurrences reachable from BB. */
  2547. bitmap from_bbs;
  2548. /* Basic blocks through which expr is hoisted. */
  2549. bitmap hoisted_bbs = NULL;
  2550. bitmap_iterator bi;
  2551. /* Compute a mapping from expression number (`bitmap_index') to
  2552. hash table entry. */
  2553. index_map = XCNEWVEC (struct gcse_expr *, expr_hash_table.n_elems);
  2554. for (i = 0; i < expr_hash_table.size; i++)
  2555. for (expr = expr_hash_table.table[i]; expr; expr = expr->next_same_hash)
  2556. index_map[expr->bitmap_index] = expr;
  2557. /* Calculate sizes of basic blocks and note how far
  2558. each instruction is from the start of its block. We then use this
  2559. data to restrict distance an expression can travel. */
  2560. to_bb_head = XCNEWVEC (int, get_max_uid ());
  2561. bb_size = XCNEWVEC (int, last_basic_block_for_fn (cfun));
  2562. FOR_EACH_BB_FN (bb, cfun)
  2563. {
  2564. rtx_insn *insn;
  2565. int to_head;
  2566. to_head = 0;
  2567. FOR_BB_INSNS (bb, insn)
  2568. {
  2569. /* Don't count debug instructions to avoid them affecting
  2570. decision choices. */
  2571. if (NONDEBUG_INSN_P (insn))
  2572. to_bb_head[INSN_UID (insn)] = to_head++;
  2573. }
  2574. bb_size[bb->index] = to_head;
  2575. }
  2576. gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1
  2577. && (EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0)->dest
  2578. == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb));
  2579. from_bbs = BITMAP_ALLOC (NULL);
  2580. if (flag_ira_hoist_pressure)
  2581. hoisted_bbs = BITMAP_ALLOC (NULL);
  2582. dom_tree_walk = get_all_dominated_blocks (CDI_DOMINATORS,
  2583. ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb);
  2584. /* Walk over each basic block looking for potentially hoistable
  2585. expressions, nothing gets hoisted from the entry block. */
  2586. FOR_EACH_VEC_ELT (dom_tree_walk, dom_tree_walk_index, bb)
  2587. {
  2588. domby = get_dominated_to_depth (CDI_DOMINATORS, bb, MAX_HOIST_DEPTH);
  2589. if (domby.length () == 0)
  2590. continue;
  2591. /* Examine each expression that is very busy at the exit of this
  2592. block. These are the potentially hoistable expressions. */
  2593. for (i = 0; i < SBITMAP_SIZE (hoist_vbeout[bb->index]); i++)
  2594. {
  2595. if (bitmap_bit_p (hoist_vbeout[bb->index], i))
  2596. {
  2597. int nregs = 0;
  2598. enum reg_class pressure_class = NO_REGS;
  2599. /* Current expression. */
  2600. struct gcse_expr *expr = index_map[i];
  2601. /* Number of occurrences of EXPR that can be hoisted to BB. */
  2602. int hoistable = 0;
  2603. /* Occurrences reachable from BB. */
  2604. vec<occr_t> occrs_to_hoist = vNULL;
  2605. /* We want to insert the expression into BB only once, so
  2606. note when we've inserted it. */
  2607. int insn_inserted_p;
  2608. occr_t occr;
  2609. /* If an expression is computed in BB and is available at end of
  2610. BB, hoist all occurrences dominated by BB to BB. */
  2611. if (bitmap_bit_p (comp[bb->index], i))
  2612. {
  2613. occr = find_occr_in_bb (expr->antic_occr, bb);
  2614. if (occr)
  2615. {
  2616. /* An occurrence might've been already deleted
  2617. while processing a dominator of BB. */
  2618. if (!occr->deleted_p)
  2619. {
  2620. gcc_assert (NONDEBUG_INSN_P (occr->insn));
  2621. hoistable++;
  2622. }
  2623. }
  2624. else
  2625. hoistable++;
  2626. }
  2627. /* We've found a potentially hoistable expression, now
  2628. we look at every block BB dominates to see if it
  2629. computes the expression. */
  2630. FOR_EACH_VEC_ELT (domby, j, dominated)
  2631. {
  2632. int max_distance;
  2633. /* Ignore self dominance. */
  2634. if (bb == dominated)
  2635. continue;
  2636. /* We've found a dominated block, now see if it computes
  2637. the busy expression and whether or not moving that
  2638. expression to the "beginning" of that block is safe. */
  2639. if (!bitmap_bit_p (antloc[dominated->index], i))
  2640. continue;
  2641. occr = find_occr_in_bb (expr->antic_occr, dominated);
  2642. gcc_assert (occr);
  2643. /* An occurrence might've been already deleted
  2644. while processing a dominator of BB. */
  2645. if (occr->deleted_p)
  2646. continue;
  2647. gcc_assert (NONDEBUG_INSN_P (occr->insn));
  2648. max_distance = expr->max_distance;
  2649. if (max_distance > 0)
  2650. /* Adjust MAX_DISTANCE to account for the fact that
  2651. OCCR won't have to travel all of DOMINATED, but
  2652. only part of it. */
  2653. max_distance += (bb_size[dominated->index]
  2654. - to_bb_head[INSN_UID (occr->insn)]);
  2655. pressure_class = get_pressure_class_and_nregs (occr->insn,
  2656. &nregs);
  2657. /* Note if the expression should be hoisted from the dominated
  2658. block to BB if it can reach DOMINATED unimpared.
  2659. Keep track of how many times this expression is hoistable
  2660. from a dominated block into BB. */
  2661. if (should_hoist_expr_to_dom (bb, expr, dominated, NULL,
  2662. max_distance, bb_size,
  2663. pressure_class, &nregs,
  2664. hoisted_bbs, occr->insn))
  2665. {
  2666. hoistable++;
  2667. occrs_to_hoist.safe_push (occr);
  2668. bitmap_set_bit (from_bbs, dominated->index);
  2669. }
  2670. }
  2671. /* If we found more than one hoistable occurrence of this
  2672. expression, then note it in the vector of expressions to
  2673. hoist. It makes no sense to hoist things which are computed
  2674. in only one BB, and doing so tends to pessimize register
  2675. allocation. One could increase this value to try harder
  2676. to avoid any possible code expansion due to register
  2677. allocation issues; however experiments have shown that
  2678. the vast majority of hoistable expressions are only movable
  2679. from two successors, so raising this threshold is likely
  2680. to nullify any benefit we get from code hoisting. */
  2681. if (hoistable > 1 && dbg_cnt (hoist_insn))
  2682. {
  2683. /* If (hoistable != vec::length), then there is
  2684. an occurrence of EXPR in BB itself. Don't waste
  2685. time looking for LCA in this case. */
  2686. if ((unsigned) hoistable == occrs_to_hoist.length ())
  2687. {
  2688. basic_block lca;
  2689. lca = nearest_common_dominator_for_set (CDI_DOMINATORS,
  2690. from_bbs);
  2691. if (lca != bb)
  2692. /* Punt, it's better to hoist these occurrences to
  2693. LCA. */
  2694. occrs_to_hoist.release ();
  2695. }
  2696. }
  2697. else
  2698. /* Punt, no point hoisting a single occurrence. */
  2699. occrs_to_hoist.release ();
  2700. if (flag_ira_hoist_pressure
  2701. && !occrs_to_hoist.is_empty ())
  2702. {
  2703. /* Increase register pressure of basic blocks to which
  2704. expr is hoisted because of extended live range of
  2705. output. */
  2706. data = BB_DATA (bb);
  2707. data->max_reg_pressure[pressure_class] += nregs;
  2708. EXECUTE_IF_SET_IN_BITMAP (hoisted_bbs, 0, k, bi)
  2709. {
  2710. data = BB_DATA (BASIC_BLOCK_FOR_FN (cfun, k));
  2711. data->max_reg_pressure[pressure_class] += nregs;
  2712. }
  2713. }
  2714. else if (flag_ira_hoist_pressure)
  2715. {
  2716. /* Restore register pressure and live_in info for basic
  2717. blocks recorded in hoisted_bbs when expr will not be
  2718. hoisted. */
  2719. EXECUTE_IF_SET_IN_BITMAP (hoisted_bbs, 0, k, bi)
  2720. {
  2721. data = BB_DATA (BASIC_BLOCK_FOR_FN (cfun, k));
  2722. bitmap_copy (data->live_in, data->backup);
  2723. data->max_reg_pressure[pressure_class]
  2724. = data->old_pressure;
  2725. }
  2726. }
  2727. if (flag_ira_hoist_pressure)
  2728. bitmap_clear (hoisted_bbs);
  2729. insn_inserted_p = 0;
  2730. /* Walk through occurrences of I'th expressions we want
  2731. to hoist to BB and make the transformations. */
  2732. FOR_EACH_VEC_ELT (occrs_to_hoist, j, occr)
  2733. {
  2734. rtx_insn *insn;
  2735. const_rtx set;
  2736. gcc_assert (!occr->deleted_p);
  2737. insn = occr->insn;
  2738. set = single_set_gcse (insn);
  2739. /* Create a pseudo-reg to store the result of reaching
  2740. expressions into. Get the mode for the new pseudo
  2741. from the mode of the original destination pseudo.
  2742. It is important to use new pseudos whenever we
  2743. emit a set. This will allow reload to use
  2744. rematerialization for such registers. */
  2745. if (!insn_inserted_p)
  2746. expr->reaching_reg
  2747. = gen_reg_rtx_and_attrs (SET_DEST (set));
  2748. gcse_emit_move_after (SET_DEST (set), expr->reaching_reg,
  2749. insn);
  2750. delete_insn (insn);
  2751. occr->deleted_p = 1;
  2752. changed = 1;
  2753. gcse_subst_count++;
  2754. if (!insn_inserted_p)
  2755. {
  2756. insert_insn_end_basic_block (expr, bb);
  2757. insn_inserted_p = 1;
  2758. }
  2759. }
  2760. occrs_to_hoist.release ();
  2761. bitmap_clear (from_bbs);
  2762. }
  2763. }
  2764. domby.release ();
  2765. }
  2766. dom_tree_walk.release ();
  2767. BITMAP_FREE (from_bbs);
  2768. if (flag_ira_hoist_pressure)
  2769. BITMAP_FREE (hoisted_bbs);
  2770. free (bb_size);
  2771. free (to_bb_head);
  2772. free (index_map);
  2773. return changed;
  2774. }
  2775. /* Return pressure class and number of needed hard registers (through
  2776. *NREGS) of register REGNO. */
  2777. static enum reg_class
  2778. get_regno_pressure_class (int regno, int *nregs)
  2779. {
  2780. if (regno >= FIRST_PSEUDO_REGISTER)
  2781. {
  2782. enum reg_class pressure_class;
  2783. pressure_class = reg_allocno_class (regno);
  2784. pressure_class = ira_pressure_class_translate[pressure_class];
  2785. *nregs
  2786. = ira_reg_class_max_nregs[pressure_class][PSEUDO_REGNO_MODE (regno)];
  2787. return pressure_class;
  2788. }
  2789. else if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno)
  2790. && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
  2791. {
  2792. *nregs = 1;
  2793. return ira_pressure_class_translate[REGNO_REG_CLASS (regno)];
  2794. }
  2795. else
  2796. {
  2797. *nregs = 0;
  2798. return NO_REGS;
  2799. }
  2800. }
  2801. /* Return pressure class and number of hard registers (through *NREGS)
  2802. for destination of INSN. */
  2803. static enum reg_class
  2804. get_pressure_class_and_nregs (rtx_insn *insn, int *nregs)
  2805. {
  2806. rtx reg;
  2807. enum reg_class pressure_class;
  2808. const_rtx set = single_set_gcse (insn);
  2809. reg = SET_DEST (set);
  2810. if (GET_CODE (reg) == SUBREG)
  2811. reg = SUBREG_REG (reg);
  2812. if (MEM_P (reg))
  2813. {
  2814. *nregs = 0;
  2815. pressure_class = NO_REGS;
  2816. }
  2817. else
  2818. {
  2819. gcc_assert (REG_P (reg));
  2820. pressure_class = reg_allocno_class (REGNO (reg));
  2821. pressure_class = ira_pressure_class_translate[pressure_class];
  2822. *nregs
  2823. = ira_reg_class_max_nregs[pressure_class][GET_MODE (SET_SRC (set))];
  2824. }
  2825. return pressure_class;
  2826. }
  2827. /* Increase (if INCR_P) or decrease current register pressure for
  2828. register REGNO. */
  2829. static void
  2830. change_pressure (int regno, bool incr_p)
  2831. {
  2832. int nregs;
  2833. enum reg_class pressure_class;
  2834. pressure_class = get_regno_pressure_class (regno, &nregs);
  2835. if (! incr_p)
  2836. curr_reg_pressure[pressure_class] -= nregs;
  2837. else
  2838. {
  2839. curr_reg_pressure[pressure_class] += nregs;
  2840. if (BB_DATA (curr_bb)->max_reg_pressure[pressure_class]
  2841. < curr_reg_pressure[pressure_class])
  2842. BB_DATA (curr_bb)->max_reg_pressure[pressure_class]
  2843. = curr_reg_pressure[pressure_class];
  2844. }
  2845. }
  2846. /* Calculate register pressure for each basic block by walking insns
  2847. from last to first. */
  2848. static void
  2849. calculate_bb_reg_pressure (void)
  2850. {
  2851. int i;
  2852. unsigned int j;
  2853. rtx_insn *insn;
  2854. basic_block bb;
  2855. bitmap curr_regs_live;
  2856. bitmap_iterator bi;
  2857. ira_setup_eliminable_regset ();
  2858. curr_regs_live = BITMAP_ALLOC (&reg_obstack);
  2859. FOR_EACH_BB_FN (bb, cfun)
  2860. {
  2861. curr_bb = bb;
  2862. BB_DATA (bb)->live_in = BITMAP_ALLOC (NULL);
  2863. BB_DATA (bb)->backup = BITMAP_ALLOC (NULL);
  2864. bitmap_copy (BB_DATA (bb)->live_in, df_get_live_in (bb));
  2865. bitmap_copy (curr_regs_live, df_get_live_out (bb));
  2866. for (i = 0; i < ira_pressure_classes_num; i++)
  2867. curr_reg_pressure[ira_pressure_classes[i]] = 0;
  2868. EXECUTE_IF_SET_IN_BITMAP (curr_regs_live, 0, j, bi)
  2869. change_pressure (j, true);
  2870. FOR_BB_INSNS_REVERSE (bb, insn)
  2871. {
  2872. rtx dreg;
  2873. int regno;
  2874. df_ref def, use;
  2875. if (! NONDEBUG_INSN_P (insn))
  2876. continue;
  2877. FOR_EACH_INSN_DEF (def, insn)
  2878. {
  2879. dreg = DF_REF_REAL_REG (def);
  2880. gcc_assert (REG_P (dreg));
  2881. regno = REGNO (dreg);
  2882. if (!(DF_REF_FLAGS (def)
  2883. & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
  2884. {
  2885. if (bitmap_clear_bit (curr_regs_live, regno))
  2886. change_pressure (regno, false);
  2887. }
  2888. }
  2889. FOR_EACH_INSN_USE (use, insn)
  2890. {
  2891. dreg = DF_REF_REAL_REG (use);
  2892. gcc_assert (REG_P (dreg));
  2893. regno = REGNO (dreg);
  2894. if (bitmap_set_bit (curr_regs_live, regno))
  2895. change_pressure (regno, true);
  2896. }
  2897. }
  2898. }
  2899. BITMAP_FREE (curr_regs_live);
  2900. if (dump_file == NULL)
  2901. return;
  2902. fprintf (dump_file, "\nRegister Pressure: \n");
  2903. FOR_EACH_BB_FN (bb, cfun)
  2904. {
  2905. fprintf (dump_file, " Basic block %d: \n", bb->index);
  2906. for (i = 0; (int) i < ira_pressure_classes_num; i++)
  2907. {
  2908. enum reg_class pressure_class;
  2909. pressure_class = ira_pressure_classes[i];
  2910. if (BB_DATA (bb)->max_reg_pressure[pressure_class] == 0)
  2911. continue;
  2912. fprintf (dump_file, " %s=%d\n", reg_class_names[pressure_class],
  2913. BB_DATA (bb)->max_reg_pressure[pressure_class]);
  2914. }
  2915. }
  2916. fprintf (dump_file, "\n");
  2917. }
  2918. /* Top level routine to perform one code hoisting (aka unification) pass
  2919. Return nonzero if a change was made. */
  2920. static int
  2921. one_code_hoisting_pass (void)
  2922. {
  2923. int changed = 0;
  2924. gcse_subst_count = 0;
  2925. gcse_create_count = 0;
  2926. /* Return if there's nothing to do, or it is too expensive. */
  2927. if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
  2928. || is_too_expensive (_("GCSE disabled")))
  2929. return 0;
  2930. doing_code_hoisting_p = true;
  2931. /* Calculate register pressure for each basic block. */
  2932. if (flag_ira_hoist_pressure)
  2933. {
  2934. regstat_init_n_sets_and_refs ();
  2935. ira_set_pseudo_classes (false, dump_file);
  2936. alloc_aux_for_blocks (sizeof (struct bb_data));
  2937. calculate_bb_reg_pressure ();
  2938. regstat_free_n_sets_and_refs ();
  2939. }
  2940. /* We need alias. */
  2941. init_alias_analysis ();
  2942. bytes_used = 0;
  2943. gcc_obstack_init (&gcse_obstack);
  2944. alloc_gcse_mem ();
  2945. alloc_hash_table (&expr_hash_table);
  2946. compute_hash_table (&expr_hash_table);
  2947. if (dump_file)
  2948. dump_hash_table (dump_file, "Code Hosting Expressions", &expr_hash_table);
  2949. if (expr_hash_table.n_elems > 0)
  2950. {
  2951. alloc_code_hoist_mem (last_basic_block_for_fn (cfun),
  2952. expr_hash_table.n_elems);
  2953. compute_code_hoist_data ();
  2954. changed = hoist_code ();
  2955. free_code_hoist_mem ();
  2956. }
  2957. if (flag_ira_hoist_pressure)
  2958. {
  2959. free_aux_for_blocks ();
  2960. free_reg_info ();
  2961. }
  2962. free_hash_table (&expr_hash_table);
  2963. free_gcse_mem ();
  2964. obstack_free (&gcse_obstack, NULL);
  2965. /* We are finished with alias. */
  2966. end_alias_analysis ();
  2967. if (dump_file)
  2968. {
  2969. fprintf (dump_file, "HOIST of %s, %d basic blocks, %d bytes needed, ",
  2970. current_function_name (), n_basic_blocks_for_fn (cfun),
  2971. bytes_used);
  2972. fprintf (dump_file, "%d substs, %d insns created\n",
  2973. gcse_subst_count, gcse_create_count);
  2974. }
  2975. doing_code_hoisting_p = false;
  2976. return changed;
  2977. }
  2978. /* Here we provide the things required to do store motion towards the exit.
  2979. In order for this to be effective, gcse also needed to be taught how to
  2980. move a load when it is killed only by a store to itself.
  2981. int i;
  2982. float a[10];
  2983. void foo(float scale)
  2984. {
  2985. for (i=0; i<10; i++)
  2986. a[i] *= scale;
  2987. }
  2988. 'i' is both loaded and stored to in the loop. Normally, gcse cannot move
  2989. the load out since its live around the loop, and stored at the bottom
  2990. of the loop.
  2991. The 'Load Motion' referred to and implemented in this file is
  2992. an enhancement to gcse which when using edge based LCM, recognizes
  2993. this situation and allows gcse to move the load out of the loop.
  2994. Once gcse has hoisted the load, store motion can then push this
  2995. load towards the exit, and we end up with no loads or stores of 'i'
  2996. in the loop. */
  2997. /* This will search the ldst list for a matching expression. If it
  2998. doesn't find one, we create one and initialize it. */
  2999. static struct ls_expr *
  3000. ldst_entry (rtx x)
  3001. {
  3002. int do_not_record_p = 0;
  3003. struct ls_expr * ptr;
  3004. unsigned int hash;
  3005. ls_expr **slot;
  3006. struct ls_expr e;
  3007. hash = hash_rtx (x, GET_MODE (x), &do_not_record_p,
  3008. NULL, /*have_reg_qty=*/false);
  3009. e.pattern = x;
  3010. slot = pre_ldst_table->find_slot_with_hash (&e, hash, INSERT);
  3011. if (*slot)
  3012. return *slot;
  3013. ptr = XNEW (struct ls_expr);
  3014. ptr->next = pre_ldst_mems;
  3015. ptr->expr = NULL;
  3016. ptr->pattern = x;
  3017. ptr->pattern_regs = NULL_RTX;
  3018. ptr->loads = NULL;
  3019. ptr->stores = NULL;
  3020. ptr->reaching_reg = NULL_RTX;
  3021. ptr->invalid = 0;
  3022. ptr->index = 0;
  3023. ptr->hash_index = hash;
  3024. pre_ldst_mems = ptr;
  3025. *slot = ptr;
  3026. return ptr;
  3027. }
  3028. /* Free up an individual ldst entry. */
  3029. static void
  3030. free_ldst_entry (struct ls_expr * ptr)
  3031. {
  3032. free_INSN_LIST_list (& ptr->loads);
  3033. free_INSN_LIST_list (& ptr->stores);
  3034. free (ptr);
  3035. }
  3036. /* Free up all memory associated with the ldst list. */
  3037. static void
  3038. free_ld_motion_mems (void)
  3039. {
  3040. delete pre_ldst_table;
  3041. pre_ldst_table = NULL;
  3042. while (pre_ldst_mems)
  3043. {
  3044. struct ls_expr * tmp = pre_ldst_mems;
  3045. pre_ldst_mems = pre_ldst_mems->next;
  3046. free_ldst_entry (tmp);
  3047. }
  3048. pre_ldst_mems = NULL;
  3049. }
  3050. /* Dump debugging info about the ldst list. */
  3051. static void
  3052. print_ldst_list (FILE * file)
  3053. {
  3054. struct ls_expr * ptr;
  3055. fprintf (file, "LDST list: \n");
  3056. for (ptr = pre_ldst_mems; ptr != NULL; ptr = ptr->next)
  3057. {
  3058. fprintf (file, " Pattern (%3d): ", ptr->index);
  3059. print_rtl (file, ptr->pattern);
  3060. fprintf (file, "\n Loads : ");
  3061. if (ptr->loads)
  3062. print_rtl (file, ptr->loads);
  3063. else
  3064. fprintf (file, "(nil)");
  3065. fprintf (file, "\n Stores : ");
  3066. if (ptr->stores)
  3067. print_rtl (file, ptr->stores);
  3068. else
  3069. fprintf (file, "(nil)");
  3070. fprintf (file, "\n\n");
  3071. }
  3072. fprintf (file, "\n");
  3073. }
  3074. /* Returns 1 if X is in the list of ldst only expressions. */
  3075. static struct ls_expr *
  3076. find_rtx_in_ldst (rtx x)
  3077. {
  3078. struct ls_expr e;
  3079. ls_expr **slot;
  3080. if (!pre_ldst_table)
  3081. return NULL;
  3082. e.pattern = x;
  3083. slot = pre_ldst_table->find_slot (&e, NO_INSERT);
  3084. if (!slot || (*slot)->invalid)
  3085. return NULL;
  3086. return *slot;
  3087. }
  3088. /* Load Motion for loads which only kill themselves. */
  3089. /* Return true if x, a MEM, is a simple access with no side effects.
  3090. These are the types of loads we consider for the ld_motion list,
  3091. otherwise we let the usual aliasing take care of it. */
  3092. static int
  3093. simple_mem (const_rtx x)
  3094. {
  3095. if (MEM_VOLATILE_P (x))
  3096. return 0;
  3097. if (GET_MODE (x) == BLKmode)
  3098. return 0;
  3099. /* If we are handling exceptions, we must be careful with memory references
  3100. that may trap. If we are not, the behavior is undefined, so we may just
  3101. continue. */
  3102. if (cfun->can_throw_non_call_exceptions && may_trap_p (x))
  3103. return 0;
  3104. if (side_effects_p (x))
  3105. return 0;
  3106. /* Do not consider function arguments passed on stack. */
  3107. if (reg_mentioned_p (stack_pointer_rtx, x))
  3108. return 0;
  3109. if (flag_float_store && FLOAT_MODE_P (GET_MODE (x)))
  3110. return 0;
  3111. return 1;
  3112. }
  3113. /* Make sure there isn't a buried reference in this pattern anywhere.
  3114. If there is, invalidate the entry for it since we're not capable
  3115. of fixing it up just yet.. We have to be sure we know about ALL
  3116. loads since the aliasing code will allow all entries in the
  3117. ld_motion list to not-alias itself. If we miss a load, we will get
  3118. the wrong value since gcse might common it and we won't know to
  3119. fix it up. */
  3120. static void
  3121. invalidate_any_buried_refs (rtx x)
  3122. {
  3123. const char * fmt;
  3124. int i, j;
  3125. struct ls_expr * ptr;
  3126. /* Invalidate it in the list. */
  3127. if (MEM_P (x) && simple_mem (x))
  3128. {
  3129. ptr = ldst_entry (x);
  3130. ptr->invalid = 1;
  3131. }
  3132. /* Recursively process the insn. */
  3133. fmt = GET_RTX_FORMAT (GET_CODE (x));
  3134. for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
  3135. {
  3136. if (fmt[i] == 'e')
  3137. invalidate_any_buried_refs (XEXP (x, i));
  3138. else if (fmt[i] == 'E')
  3139. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  3140. invalidate_any_buried_refs (XVECEXP (x, i, j));
  3141. }
  3142. }
  3143. /* Find all the 'simple' MEMs which are used in LOADs and STORES. Simple
  3144. being defined as MEM loads and stores to symbols, with no side effects
  3145. and no registers in the expression. For a MEM destination, we also
  3146. check that the insn is still valid if we replace the destination with a
  3147. REG, as is done in update_ld_motion_stores. If there are any uses/defs
  3148. which don't match this criteria, they are invalidated and trimmed out
  3149. later. */
  3150. static void
  3151. compute_ld_motion_mems (void)
  3152. {
  3153. struct ls_expr * ptr;
  3154. basic_block bb;
  3155. rtx_insn *insn;
  3156. pre_ldst_mems = NULL;
  3157. pre_ldst_table = new hash_table<pre_ldst_expr_hasher> (13);
  3158. FOR_EACH_BB_FN (bb, cfun)
  3159. {
  3160. FOR_BB_INSNS (bb, insn)
  3161. {
  3162. if (NONDEBUG_INSN_P (insn))
  3163. {
  3164. if (GET_CODE (PATTERN (insn)) == SET)
  3165. {
  3166. rtx src = SET_SRC (PATTERN (insn));
  3167. rtx dest = SET_DEST (PATTERN (insn));
  3168. rtx note = find_reg_equal_equiv_note (insn);
  3169. rtx src_eq;
  3170. /* Check for a simple LOAD... */
  3171. if (MEM_P (src) && simple_mem (src))
  3172. {
  3173. ptr = ldst_entry (src);
  3174. if (REG_P (dest))
  3175. ptr->loads = alloc_INSN_LIST (insn, ptr->loads);
  3176. else
  3177. ptr->invalid = 1;
  3178. }
  3179. else
  3180. {
  3181. /* Make sure there isn't a buried load somewhere. */
  3182. invalidate_any_buried_refs (src);
  3183. }
  3184. if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
  3185. src_eq = XEXP (note, 0);
  3186. else
  3187. src_eq = NULL_RTX;
  3188. if (src_eq != NULL_RTX
  3189. && !(MEM_P (src_eq) && simple_mem (src_eq)))
  3190. invalidate_any_buried_refs (src_eq);
  3191. /* Check for stores. Don't worry about aliased ones, they
  3192. will block any movement we might do later. We only care
  3193. about this exact pattern since those are the only
  3194. circumstance that we will ignore the aliasing info. */
  3195. if (MEM_P (dest) && simple_mem (dest))
  3196. {
  3197. ptr = ldst_entry (dest);
  3198. if (! MEM_P (src)
  3199. && GET_CODE (src) != ASM_OPERANDS
  3200. /* Check for REG manually since want_to_gcse_p
  3201. returns 0 for all REGs. */
  3202. && can_assign_to_reg_without_clobbers_p (src))
  3203. ptr->stores = alloc_INSN_LIST (insn, ptr->stores);
  3204. else
  3205. ptr->invalid = 1;
  3206. }
  3207. }
  3208. else
  3209. invalidate_any_buried_refs (PATTERN (insn));
  3210. }
  3211. }
  3212. }
  3213. }
  3214. /* Remove any references that have been either invalidated or are not in the
  3215. expression list for pre gcse. */
  3216. static void
  3217. trim_ld_motion_mems (void)
  3218. {
  3219. struct ls_expr * * last = & pre_ldst_mems;
  3220. struct ls_expr * ptr = pre_ldst_mems;
  3221. while (ptr != NULL)
  3222. {
  3223. struct gcse_expr * expr;
  3224. /* Delete if entry has been made invalid. */
  3225. if (! ptr->invalid)
  3226. {
  3227. /* Delete if we cannot find this mem in the expression list. */
  3228. unsigned int hash = ptr->hash_index % expr_hash_table.size;
  3229. for (expr = expr_hash_table.table[hash];
  3230. expr != NULL;
  3231. expr = expr->next_same_hash)
  3232. if (expr_equiv_p (expr->expr, ptr->pattern))
  3233. break;
  3234. }
  3235. else
  3236. expr = (struct gcse_expr *) 0;
  3237. if (expr)
  3238. {
  3239. /* Set the expression field if we are keeping it. */
  3240. ptr->expr = expr;
  3241. last = & ptr->next;
  3242. ptr = ptr->next;
  3243. }
  3244. else
  3245. {
  3246. *last = ptr->next;
  3247. pre_ldst_table->remove_elt_with_hash (ptr, ptr->hash_index);
  3248. free_ldst_entry (ptr);
  3249. ptr = * last;
  3250. }
  3251. }
  3252. /* Show the world what we've found. */
  3253. if (dump_file && pre_ldst_mems != NULL)
  3254. print_ldst_list (dump_file);
  3255. }
  3256. /* This routine will take an expression which we are replacing with
  3257. a reaching register, and update any stores that are needed if
  3258. that expression is in the ld_motion list. Stores are updated by
  3259. copying their SRC to the reaching register, and then storing
  3260. the reaching register into the store location. These keeps the
  3261. correct value in the reaching register for the loads. */
  3262. static void
  3263. update_ld_motion_stores (struct gcse_expr * expr)
  3264. {
  3265. struct ls_expr * mem_ptr;
  3266. if ((mem_ptr = find_rtx_in_ldst (expr->expr)))
  3267. {
  3268. /* We can try to find just the REACHED stores, but is shouldn't
  3269. matter to set the reaching reg everywhere... some might be
  3270. dead and should be eliminated later. */
  3271. /* We replace (set mem expr) with (set reg expr) (set mem reg)
  3272. where reg is the reaching reg used in the load. We checked in
  3273. compute_ld_motion_mems that we can replace (set mem expr) with
  3274. (set reg expr) in that insn. */
  3275. rtx list = mem_ptr->stores;
  3276. for ( ; list != NULL_RTX; list = XEXP (list, 1))
  3277. {
  3278. rtx_insn *insn = as_a <rtx_insn *> (XEXP (list, 0));
  3279. rtx pat = PATTERN (insn);
  3280. rtx src = SET_SRC (pat);
  3281. rtx reg = expr->reaching_reg;
  3282. rtx copy;
  3283. /* If we've already copied it, continue. */
  3284. if (expr->reaching_reg == src)
  3285. continue;
  3286. if (dump_file)
  3287. {
  3288. fprintf (dump_file, "PRE: store updated with reaching reg ");
  3289. print_rtl (dump_file, reg);
  3290. fprintf (dump_file, ":\n ");
  3291. print_inline_rtx (dump_file, insn, 8);
  3292. fprintf (dump_file, "\n");
  3293. }
  3294. copy = gen_move_insn (reg, copy_rtx (SET_SRC (pat)));
  3295. emit_insn_before (copy, insn);
  3296. SET_SRC (pat) = reg;
  3297. df_insn_rescan (insn);
  3298. /* un-recognize this pattern since it's probably different now. */
  3299. INSN_CODE (insn) = -1;
  3300. gcse_create_count++;
  3301. }
  3302. }
  3303. }
  3304. /* Return true if the graph is too expensive to optimize. PASS is the
  3305. optimization about to be performed. */
  3306. static bool
  3307. is_too_expensive (const char *pass)
  3308. {
  3309. /* Trying to perform global optimizations on flow graphs which have
  3310. a high connectivity will take a long time and is unlikely to be
  3311. particularly useful.
  3312. In normal circumstances a cfg should have about twice as many
  3313. edges as blocks. But we do not want to punish small functions
  3314. which have a couple switch statements. Rather than simply
  3315. threshold the number of blocks, uses something with a more
  3316. graceful degradation. */
  3317. if (n_edges_for_fn (cfun) > 20000 + n_basic_blocks_for_fn (cfun) * 4)
  3318. {
  3319. warning (OPT_Wdisabled_optimization,
  3320. "%s: %d basic blocks and %d edges/basic block",
  3321. pass, n_basic_blocks_for_fn (cfun),
  3322. n_edges_for_fn (cfun) / n_basic_blocks_for_fn (cfun));
  3323. return true;
  3324. }
  3325. /* If allocating memory for the dataflow bitmaps would take up too much
  3326. storage it's better just to disable the optimization. */
  3327. if ((n_basic_blocks_for_fn (cfun)
  3328. * SBITMAP_SET_SIZE (max_reg_num ())
  3329. * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
  3330. {
  3331. warning (OPT_Wdisabled_optimization,
  3332. "%s: %d basic blocks and %d registers",
  3333. pass, n_basic_blocks_for_fn (cfun), max_reg_num ());
  3334. return true;
  3335. }
  3336. return false;
  3337. }
  3338. static unsigned int
  3339. execute_rtl_pre (void)
  3340. {
  3341. int changed;
  3342. delete_unreachable_blocks ();
  3343. df_analyze ();
  3344. changed = one_pre_gcse_pass ();
  3345. flag_rerun_cse_after_global_opts |= changed;
  3346. if (changed)
  3347. cleanup_cfg (0);
  3348. return 0;
  3349. }
  3350. static unsigned int
  3351. execute_rtl_hoist (void)
  3352. {
  3353. int changed;
  3354. delete_unreachable_blocks ();
  3355. df_analyze ();
  3356. changed = one_code_hoisting_pass ();
  3357. flag_rerun_cse_after_global_opts |= changed;
  3358. if (changed)
  3359. cleanup_cfg (0);
  3360. return 0;
  3361. }
  3362. namespace {
  3363. const pass_data pass_data_rtl_pre =
  3364. {
  3365. RTL_PASS, /* type */
  3366. "rtl pre", /* name */
  3367. OPTGROUP_NONE, /* optinfo_flags */
  3368. TV_PRE, /* tv_id */
  3369. PROP_cfglayout, /* properties_required */
  3370. 0, /* properties_provided */
  3371. 0, /* properties_destroyed */
  3372. 0, /* todo_flags_start */
  3373. TODO_df_finish, /* todo_flags_finish */
  3374. };
  3375. class pass_rtl_pre : public rtl_opt_pass
  3376. {
  3377. public:
  3378. pass_rtl_pre (gcc::context *ctxt)
  3379. : rtl_opt_pass (pass_data_rtl_pre, ctxt)
  3380. {}
  3381. /* opt_pass methods: */
  3382. virtual bool gate (function *);
  3383. virtual unsigned int execute (function *) { return execute_rtl_pre (); }
  3384. }; // class pass_rtl_pre
  3385. /* We do not construct an accurate cfg in functions which call
  3386. setjmp, so none of these passes runs if the function calls
  3387. setjmp.
  3388. FIXME: Should just handle setjmp via REG_SETJMP notes. */
  3389. bool
  3390. pass_rtl_pre::gate (function *fun)
  3391. {
  3392. return optimize > 0 && flag_gcse
  3393. && !fun->calls_setjmp
  3394. && optimize_function_for_speed_p (fun)
  3395. && dbg_cnt (pre);
  3396. }
  3397. } // anon namespace
  3398. rtl_opt_pass *
  3399. make_pass_rtl_pre (gcc::context *ctxt)
  3400. {
  3401. return new pass_rtl_pre (ctxt);
  3402. }
  3403. namespace {
  3404. const pass_data pass_data_rtl_hoist =
  3405. {
  3406. RTL_PASS, /* type */
  3407. "hoist", /* name */
  3408. OPTGROUP_NONE, /* optinfo_flags */
  3409. TV_HOIST, /* tv_id */
  3410. PROP_cfglayout, /* properties_required */
  3411. 0, /* properties_provided */
  3412. 0, /* properties_destroyed */
  3413. 0, /* todo_flags_start */
  3414. TODO_df_finish, /* todo_flags_finish */
  3415. };
  3416. class pass_rtl_hoist : public rtl_opt_pass
  3417. {
  3418. public:
  3419. pass_rtl_hoist (gcc::context *ctxt)
  3420. : rtl_opt_pass (pass_data_rtl_hoist, ctxt)
  3421. {}
  3422. /* opt_pass methods: */
  3423. virtual bool gate (function *);
  3424. virtual unsigned int execute (function *) { return execute_rtl_hoist (); }
  3425. }; // class pass_rtl_hoist
  3426. bool
  3427. pass_rtl_hoist::gate (function *)
  3428. {
  3429. return optimize > 0 && flag_gcse
  3430. && !cfun->calls_setjmp
  3431. /* It does not make sense to run code hoisting unless we are optimizing
  3432. for code size -- it rarely makes programs faster, and can make then
  3433. bigger if we did PRE (when optimizing for space, we don't run PRE). */
  3434. && optimize_function_for_size_p (cfun)
  3435. && dbg_cnt (hoist);
  3436. }
  3437. } // anon namespace
  3438. rtl_opt_pass *
  3439. make_pass_rtl_hoist (gcc::context *ctxt)
  3440. {
  3441. return new pass_rtl_hoist (ctxt);
  3442. }
  3443. /* Reset all state within gcse.c so that we can rerun the compiler
  3444. within the same process. For use by toplev::finalize. */
  3445. void
  3446. gcse_c_finalize (void)
  3447. {
  3448. test_insn = NULL;
  3449. }
  3450. #include "gt-gcse.h"