tree-ssa-pre.c 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997
  1. /* SSA-PRE for trees.
  2. Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
  4. <stevenb@suse.de>
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. GCC is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "tm.h"
  21. #include "hash-set.h"
  22. #include "machmode.h"
  23. #include "vec.h"
  24. #include "double-int.h"
  25. #include "input.h"
  26. #include "alias.h"
  27. #include "symtab.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "fold-const.h"
  32. #include "predict.h"
  33. #include "hard-reg-set.h"
  34. #include "function.h"
  35. #include "dominance.h"
  36. #include "cfg.h"
  37. #include "cfganal.h"
  38. #include "basic-block.h"
  39. #include "gimple-pretty-print.h"
  40. #include "tree-inline.h"
  41. #include "hash-table.h"
  42. #include "tree-ssa-alias.h"
  43. #include "internal-fn.h"
  44. #include "gimple-fold.h"
  45. #include "tree-eh.h"
  46. #include "gimple-expr.h"
  47. #include "is-a.h"
  48. #include "gimple.h"
  49. #include "gimplify.h"
  50. #include "gimple-iterator.h"
  51. #include "gimplify-me.h"
  52. #include "gimple-ssa.h"
  53. #include "tree-cfg.h"
  54. #include "tree-phinodes.h"
  55. #include "ssa-iterators.h"
  56. #include "stringpool.h"
  57. #include "tree-ssanames.h"
  58. #include "tree-ssa-loop.h"
  59. #include "tree-into-ssa.h"
  60. #include "hashtab.h"
  61. #include "rtl.h"
  62. #include "flags.h"
  63. #include "statistics.h"
  64. #include "real.h"
  65. #include "fixed-value.h"
  66. #include "insn-config.h"
  67. #include "expmed.h"
  68. #include "dojump.h"
  69. #include "explow.h"
  70. #include "calls.h"
  71. #include "emit-rtl.h"
  72. #include "varasm.h"
  73. #include "stmt.h"
  74. #include "expr.h"
  75. #include "tree-dfa.h"
  76. #include "tree-ssa.h"
  77. #include "tree-iterator.h"
  78. #include "alloc-pool.h"
  79. #include "obstack.h"
  80. #include "tree-pass.h"
  81. #include "langhooks.h"
  82. #include "cfgloop.h"
  83. #include "tree-ssa-sccvn.h"
  84. #include "tree-scalar-evolution.h"
  85. #include "params.h"
  86. #include "dbgcnt.h"
  87. #include "domwalk.h"
  88. #include "hash-map.h"
  89. #include "plugin-api.h"
  90. #include "ipa-ref.h"
  91. #include "cgraph.h"
  92. #include "symbol-summary.h"
  93. #include "ipa-prop.h"
  94. #include "tree-ssa-propagate.h"
  95. #include "ipa-utils.h"
  96. #include "tree-cfgcleanup.h"
  97. /* TODO:
  98. 1. Avail sets can be shared by making an avail_find_leader that
  99. walks up the dominator tree and looks in those avail sets.
  100. This might affect code optimality, it's unclear right now.
  101. 2. Strength reduction can be performed by anticipating expressions
  102. we can repair later on.
  103. 3. We can do back-substitution or smarter value numbering to catch
  104. commutative expressions split up over multiple statements.
  105. */
  106. /* For ease of terminology, "expression node" in the below refers to
  107. every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
  108. represent the actual statement containing the expressions we care about,
  109. and we cache the value number by putting it in the expression. */
  110. /* Basic algorithm
  111. First we walk the statements to generate the AVAIL sets, the
  112. EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
  113. generation of values/expressions by a given block. We use them
  114. when computing the ANTIC sets. The AVAIL sets consist of
  115. SSA_NAME's that represent values, so we know what values are
  116. available in what blocks. AVAIL is a forward dataflow problem. In
  117. SSA, values are never killed, so we don't need a kill set, or a
  118. fixpoint iteration, in order to calculate the AVAIL sets. In
  119. traditional parlance, AVAIL sets tell us the downsafety of the
  120. expressions/values.
  121. Next, we generate the ANTIC sets. These sets represent the
  122. anticipatable expressions. ANTIC is a backwards dataflow
  123. problem. An expression is anticipatable in a given block if it could
  124. be generated in that block. This means that if we had to perform
  125. an insertion in that block, of the value of that expression, we
  126. could. Calculating the ANTIC sets requires phi translation of
  127. expressions, because the flow goes backwards through phis. We must
  128. iterate to a fixpoint of the ANTIC sets, because we have a kill
  129. set. Even in SSA form, values are not live over the entire
  130. function, only from their definition point onwards. So we have to
  131. remove values from the ANTIC set once we go past the definition
  132. point of the leaders that make them up.
  133. compute_antic/compute_antic_aux performs this computation.
  134. Third, we perform insertions to make partially redundant
  135. expressions fully redundant.
  136. An expression is partially redundant (excluding partial
  137. anticipation) if:
  138. 1. It is AVAIL in some, but not all, of the predecessors of a
  139. given block.
  140. 2. It is ANTIC in all the predecessors.
  141. In order to make it fully redundant, we insert the expression into
  142. the predecessors where it is not available, but is ANTIC.
  143. For the partial anticipation case, we only perform insertion if it
  144. is partially anticipated in some block, and fully available in all
  145. of the predecessors.
  146. insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
  147. performs these steps.
  148. Fourth, we eliminate fully redundant expressions.
  149. This is a simple statement walk that replaces redundant
  150. calculations with the now available values. */
  151. /* Representations of value numbers:
  152. Value numbers are represented by a representative SSA_NAME. We
  153. will create fake SSA_NAME's in situations where we need a
  154. representative but do not have one (because it is a complex
  155. expression). In order to facilitate storing the value numbers in
  156. bitmaps, and keep the number of wasted SSA_NAME's down, we also
  157. associate a value_id with each value number, and create full blown
  158. ssa_name's only where we actually need them (IE in operands of
  159. existing expressions).
  160. Theoretically you could replace all the value_id's with
  161. SSA_NAME_VERSION, but this would allocate a large number of
  162. SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
  163. It would also require an additional indirection at each point we
  164. use the value id. */
  165. /* Representation of expressions on value numbers:
  166. Expressions consisting of value numbers are represented the same
  167. way as our VN internally represents them, with an additional
  168. "pre_expr" wrapping around them in order to facilitate storing all
  169. of the expressions in the same sets. */
  170. /* Representation of sets:
  171. The dataflow sets do not need to be sorted in any particular order
  172. for the majority of their lifetime, are simply represented as two
  173. bitmaps, one that keeps track of values present in the set, and one
  174. that keeps track of expressions present in the set.
  175. When we need them in topological order, we produce it on demand by
  176. transforming the bitmap into an array and sorting it into topo
  177. order. */
  178. /* Type of expression, used to know which member of the PRE_EXPR union
  179. is valid. */
  180. enum pre_expr_kind
  181. {
  182. NAME,
  183. NARY,
  184. REFERENCE,
  185. CONSTANT
  186. };
  187. typedef union pre_expr_union_d
  188. {
  189. tree name;
  190. tree constant;
  191. vn_nary_op_t nary;
  192. vn_reference_t reference;
  193. } pre_expr_union;
  194. typedef struct pre_expr_d : typed_noop_remove <pre_expr_d>
  195. {
  196. enum pre_expr_kind kind;
  197. unsigned int id;
  198. pre_expr_union u;
  199. /* hash_table support. */
  200. typedef pre_expr_d value_type;
  201. typedef pre_expr_d compare_type;
  202. static inline hashval_t hash (const pre_expr_d *);
  203. static inline int equal (const pre_expr_d *, const pre_expr_d *);
  204. } *pre_expr;
  205. #define PRE_EXPR_NAME(e) (e)->u.name
  206. #define PRE_EXPR_NARY(e) (e)->u.nary
  207. #define PRE_EXPR_REFERENCE(e) (e)->u.reference
  208. #define PRE_EXPR_CONSTANT(e) (e)->u.constant
  209. /* Compare E1 and E1 for equality. */
  210. inline int
  211. pre_expr_d::equal (const value_type *e1, const compare_type *e2)
  212. {
  213. if (e1->kind != e2->kind)
  214. return false;
  215. switch (e1->kind)
  216. {
  217. case CONSTANT:
  218. return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
  219. PRE_EXPR_CONSTANT (e2));
  220. case NAME:
  221. return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
  222. case NARY:
  223. return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
  224. case REFERENCE:
  225. return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
  226. PRE_EXPR_REFERENCE (e2));
  227. default:
  228. gcc_unreachable ();
  229. }
  230. }
  231. /* Hash E. */
  232. inline hashval_t
  233. pre_expr_d::hash (const value_type *e)
  234. {
  235. switch (e->kind)
  236. {
  237. case CONSTANT:
  238. return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e));
  239. case NAME:
  240. return SSA_NAME_VERSION (PRE_EXPR_NAME (e));
  241. case NARY:
  242. return PRE_EXPR_NARY (e)->hashcode;
  243. case REFERENCE:
  244. return PRE_EXPR_REFERENCE (e)->hashcode;
  245. default:
  246. gcc_unreachable ();
  247. }
  248. }
  249. /* Next global expression id number. */
  250. static unsigned int next_expression_id;
  251. /* Mapping from expression to id number we can use in bitmap sets. */
  252. static vec<pre_expr> expressions;
  253. static hash_table<pre_expr_d> *expression_to_id;
  254. static vec<unsigned> name_to_id;
  255. /* Allocate an expression id for EXPR. */
  256. static inline unsigned int
  257. alloc_expression_id (pre_expr expr)
  258. {
  259. struct pre_expr_d **slot;
  260. /* Make sure we won't overflow. */
  261. gcc_assert (next_expression_id + 1 > next_expression_id);
  262. expr->id = next_expression_id++;
  263. expressions.safe_push (expr);
  264. if (expr->kind == NAME)
  265. {
  266. unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
  267. /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
  268. re-allocations by using vec::reserve upfront. */
  269. unsigned old_len = name_to_id.length ();
  270. name_to_id.reserve (num_ssa_names - old_len);
  271. name_to_id.quick_grow_cleared (num_ssa_names);
  272. gcc_assert (name_to_id[version] == 0);
  273. name_to_id[version] = expr->id;
  274. }
  275. else
  276. {
  277. slot = expression_to_id->find_slot (expr, INSERT);
  278. gcc_assert (!*slot);
  279. *slot = expr;
  280. }
  281. return next_expression_id - 1;
  282. }
  283. /* Return the expression id for tree EXPR. */
  284. static inline unsigned int
  285. get_expression_id (const pre_expr expr)
  286. {
  287. return expr->id;
  288. }
  289. static inline unsigned int
  290. lookup_expression_id (const pre_expr expr)
  291. {
  292. struct pre_expr_d **slot;
  293. if (expr->kind == NAME)
  294. {
  295. unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
  296. if (name_to_id.length () <= version)
  297. return 0;
  298. return name_to_id[version];
  299. }
  300. else
  301. {
  302. slot = expression_to_id->find_slot (expr, NO_INSERT);
  303. if (!slot)
  304. return 0;
  305. return ((pre_expr)*slot)->id;
  306. }
  307. }
  308. /* Return the existing expression id for EXPR, or create one if one
  309. does not exist yet. */
  310. static inline unsigned int
  311. get_or_alloc_expression_id (pre_expr expr)
  312. {
  313. unsigned int id = lookup_expression_id (expr);
  314. if (id == 0)
  315. return alloc_expression_id (expr);
  316. return expr->id = id;
  317. }
  318. /* Return the expression that has expression id ID */
  319. static inline pre_expr
  320. expression_for_id (unsigned int id)
  321. {
  322. return expressions[id];
  323. }
  324. /* Free the expression id field in all of our expressions,
  325. and then destroy the expressions array. */
  326. static void
  327. clear_expression_ids (void)
  328. {
  329. expressions.release ();
  330. }
  331. static alloc_pool pre_expr_pool;
  332. /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
  333. static pre_expr
  334. get_or_alloc_expr_for_name (tree name)
  335. {
  336. struct pre_expr_d expr;
  337. pre_expr result;
  338. unsigned int result_id;
  339. expr.kind = NAME;
  340. expr.id = 0;
  341. PRE_EXPR_NAME (&expr) = name;
  342. result_id = lookup_expression_id (&expr);
  343. if (result_id != 0)
  344. return expression_for_id (result_id);
  345. result = (pre_expr) pool_alloc (pre_expr_pool);
  346. result->kind = NAME;
  347. PRE_EXPR_NAME (result) = name;
  348. alloc_expression_id (result);
  349. return result;
  350. }
  351. /* An unordered bitmap set. One bitmap tracks values, the other,
  352. expressions. */
  353. typedef struct bitmap_set
  354. {
  355. bitmap_head expressions;
  356. bitmap_head values;
  357. } *bitmap_set_t;
  358. #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
  359. EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
  360. #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
  361. EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
  362. /* Mapping from value id to expressions with that value_id. */
  363. static vec<bitmap> value_expressions;
  364. /* Sets that we need to keep track of. */
  365. typedef struct bb_bitmap_sets
  366. {
  367. /* The EXP_GEN set, which represents expressions/values generated in
  368. a basic block. */
  369. bitmap_set_t exp_gen;
  370. /* The PHI_GEN set, which represents PHI results generated in a
  371. basic block. */
  372. bitmap_set_t phi_gen;
  373. /* The TMP_GEN set, which represents results/temporaries generated
  374. in a basic block. IE the LHS of an expression. */
  375. bitmap_set_t tmp_gen;
  376. /* The AVAIL_OUT set, which represents which values are available in
  377. a given basic block. */
  378. bitmap_set_t avail_out;
  379. /* The ANTIC_IN set, which represents which values are anticipatable
  380. in a given basic block. */
  381. bitmap_set_t antic_in;
  382. /* The PA_IN set, which represents which values are
  383. partially anticipatable in a given basic block. */
  384. bitmap_set_t pa_in;
  385. /* The NEW_SETS set, which is used during insertion to augment the
  386. AVAIL_OUT set of blocks with the new insertions performed during
  387. the current iteration. */
  388. bitmap_set_t new_sets;
  389. /* A cache for value_dies_in_block_x. */
  390. bitmap expr_dies;
  391. /* The live virtual operand on successor edges. */
  392. tree vop_on_exit;
  393. /* True if we have visited this block during ANTIC calculation. */
  394. unsigned int visited : 1;
  395. /* True when the block contains a call that might not return. */
  396. unsigned int contains_may_not_return_call : 1;
  397. } *bb_value_sets_t;
  398. #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
  399. #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
  400. #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
  401. #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
  402. #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
  403. #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
  404. #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
  405. #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
  406. #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
  407. #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
  408. #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
  409. /* Basic block list in postorder. */
  410. static int *postorder;
  411. static int postorder_num;
  412. /* This structure is used to keep track of statistics on what
  413. optimization PRE was able to perform. */
  414. static struct
  415. {
  416. /* The number of RHS computations eliminated by PRE. */
  417. int eliminations;
  418. /* The number of new expressions/temporaries generated by PRE. */
  419. int insertions;
  420. /* The number of inserts found due to partial anticipation */
  421. int pa_insert;
  422. /* The number of new PHI nodes added by PRE. */
  423. int phis;
  424. } pre_stats;
  425. static bool do_partial_partial;
  426. static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
  427. static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
  428. static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
  429. static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
  430. static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
  431. static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
  432. static void bitmap_insert_into_set_1 (bitmap_set_t, pre_expr,
  433. unsigned int, bool);
  434. static bitmap_set_t bitmap_set_new (void);
  435. static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
  436. tree);
  437. static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
  438. static unsigned int get_expr_value_id (pre_expr);
  439. /* We can add and remove elements and entries to and from sets
  440. and hash tables, so we use alloc pools for them. */
  441. static alloc_pool bitmap_set_pool;
  442. static bitmap_obstack grand_bitmap_obstack;
  443. /* Set of blocks with statements that have had their EH properties changed. */
  444. static bitmap need_eh_cleanup;
  445. /* Set of blocks with statements that have had their AB properties changed. */
  446. static bitmap need_ab_cleanup;
  447. /* A three tuple {e, pred, v} used to cache phi translations in the
  448. phi_translate_table. */
  449. typedef struct expr_pred_trans_d : typed_free_remove<expr_pred_trans_d>
  450. {
  451. /* The expression. */
  452. pre_expr e;
  453. /* The predecessor block along which we translated the expression. */
  454. basic_block pred;
  455. /* The value that resulted from the translation. */
  456. pre_expr v;
  457. /* The hashcode for the expression, pred pair. This is cached for
  458. speed reasons. */
  459. hashval_t hashcode;
  460. /* hash_table support. */
  461. typedef expr_pred_trans_d value_type;
  462. typedef expr_pred_trans_d compare_type;
  463. static inline hashval_t hash (const value_type *);
  464. static inline int equal (const value_type *, const compare_type *);
  465. } *expr_pred_trans_t;
  466. typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
  467. inline hashval_t
  468. expr_pred_trans_d::hash (const expr_pred_trans_d *e)
  469. {
  470. return e->hashcode;
  471. }
  472. inline int
  473. expr_pred_trans_d::equal (const value_type *ve1,
  474. const compare_type *ve2)
  475. {
  476. basic_block b1 = ve1->pred;
  477. basic_block b2 = ve2->pred;
  478. /* If they are not translations for the same basic block, they can't
  479. be equal. */
  480. if (b1 != b2)
  481. return false;
  482. return pre_expr_d::equal (ve1->e, ve2->e);
  483. }
  484. /* The phi_translate_table caches phi translations for a given
  485. expression and predecessor. */
  486. static hash_table<expr_pred_trans_d> *phi_translate_table;
  487. /* Add the tuple mapping from {expression E, basic block PRED} to
  488. the phi translation table and return whether it pre-existed. */
  489. static inline bool
  490. phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
  491. {
  492. expr_pred_trans_t *slot;
  493. expr_pred_trans_d tem;
  494. hashval_t hash = iterative_hash_hashval_t (pre_expr_d::hash (e),
  495. pred->index);
  496. tem.e = e;
  497. tem.pred = pred;
  498. tem.hashcode = hash;
  499. slot = phi_translate_table->find_slot_with_hash (&tem, hash, INSERT);
  500. if (*slot)
  501. {
  502. *entry = *slot;
  503. return true;
  504. }
  505. *entry = *slot = XNEW (struct expr_pred_trans_d);
  506. (*entry)->e = e;
  507. (*entry)->pred = pred;
  508. (*entry)->hashcode = hash;
  509. return false;
  510. }
  511. /* Add expression E to the expression set of value id V. */
  512. static void
  513. add_to_value (unsigned int v, pre_expr e)
  514. {
  515. bitmap set;
  516. gcc_checking_assert (get_expr_value_id (e) == v);
  517. if (v >= value_expressions.length ())
  518. {
  519. value_expressions.safe_grow_cleared (v + 1);
  520. }
  521. set = value_expressions[v];
  522. if (!set)
  523. {
  524. set = BITMAP_ALLOC (&grand_bitmap_obstack);
  525. value_expressions[v] = set;
  526. }
  527. bitmap_set_bit (set, get_or_alloc_expression_id (e));
  528. }
  529. /* Create a new bitmap set and return it. */
  530. static bitmap_set_t
  531. bitmap_set_new (void)
  532. {
  533. bitmap_set_t ret = (bitmap_set_t) pool_alloc (bitmap_set_pool);
  534. bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
  535. bitmap_initialize (&ret->values, &grand_bitmap_obstack);
  536. return ret;
  537. }
  538. /* Return the value id for a PRE expression EXPR. */
  539. static unsigned int
  540. get_expr_value_id (pre_expr expr)
  541. {
  542. unsigned int id;
  543. switch (expr->kind)
  544. {
  545. case CONSTANT:
  546. id = get_constant_value_id (PRE_EXPR_CONSTANT (expr));
  547. break;
  548. case NAME:
  549. id = VN_INFO (PRE_EXPR_NAME (expr))->value_id;
  550. break;
  551. case NARY:
  552. id = PRE_EXPR_NARY (expr)->value_id;
  553. break;
  554. case REFERENCE:
  555. id = PRE_EXPR_REFERENCE (expr)->value_id;
  556. break;
  557. default:
  558. gcc_unreachable ();
  559. }
  560. /* ??? We cannot assert that expr has a value-id (it can be 0), because
  561. we assign value-ids only to expressions that have a result
  562. in set_hashtable_value_ids. */
  563. return id;
  564. }
  565. /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */
  566. static tree
  567. sccvn_valnum_from_value_id (unsigned int val)
  568. {
  569. bitmap_iterator bi;
  570. unsigned int i;
  571. bitmap exprset = value_expressions[val];
  572. EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
  573. {
  574. pre_expr vexpr = expression_for_id (i);
  575. if (vexpr->kind == NAME)
  576. return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
  577. else if (vexpr->kind == CONSTANT)
  578. return PRE_EXPR_CONSTANT (vexpr);
  579. }
  580. return NULL_TREE;
  581. }
  582. /* Remove an expression EXPR from a bitmapped set. */
  583. static void
  584. bitmap_remove_from_set (bitmap_set_t set, pre_expr expr)
  585. {
  586. unsigned int val = get_expr_value_id (expr);
  587. if (!value_id_constant_p (val))
  588. {
  589. bitmap_clear_bit (&set->values, val);
  590. bitmap_clear_bit (&set->expressions, get_expression_id (expr));
  591. }
  592. }
  593. static void
  594. bitmap_insert_into_set_1 (bitmap_set_t set, pre_expr expr,
  595. unsigned int val, bool allow_constants)
  596. {
  597. if (allow_constants || !value_id_constant_p (val))
  598. {
  599. /* We specifically expect this and only this function to be able to
  600. insert constants into a set. */
  601. bitmap_set_bit (&set->values, val);
  602. bitmap_set_bit (&set->expressions, get_or_alloc_expression_id (expr));
  603. }
  604. }
  605. /* Insert an expression EXPR into a bitmapped set. */
  606. static void
  607. bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
  608. {
  609. bitmap_insert_into_set_1 (set, expr, get_expr_value_id (expr), false);
  610. }
  611. /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
  612. static void
  613. bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
  614. {
  615. bitmap_copy (&dest->expressions, &orig->expressions);
  616. bitmap_copy (&dest->values, &orig->values);
  617. }
  618. /* Free memory used up by SET. */
  619. static void
  620. bitmap_set_free (bitmap_set_t set)
  621. {
  622. bitmap_clear (&set->expressions);
  623. bitmap_clear (&set->values);
  624. }
  625. /* Generate an topological-ordered array of bitmap set SET. */
  626. static vec<pre_expr>
  627. sorted_array_from_bitmap_set (bitmap_set_t set)
  628. {
  629. unsigned int i, j;
  630. bitmap_iterator bi, bj;
  631. vec<pre_expr> result;
  632. /* Pre-allocate enough space for the array. */
  633. result.create (bitmap_count_bits (&set->expressions));
  634. FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
  635. {
  636. /* The number of expressions having a given value is usually
  637. relatively small. Thus, rather than making a vector of all
  638. the expressions and sorting it by value-id, we walk the values
  639. and check in the reverse mapping that tells us what expressions
  640. have a given value, to filter those in our set. As a result,
  641. the expressions are inserted in value-id order, which means
  642. topological order.
  643. If this is somehow a significant lose for some cases, we can
  644. choose which set to walk based on the set size. */
  645. bitmap exprset = value_expressions[i];
  646. EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bj)
  647. {
  648. if (bitmap_bit_p (&set->expressions, j))
  649. result.quick_push (expression_for_id (j));
  650. }
  651. }
  652. return result;
  653. }
  654. /* Perform bitmapped set operation DEST &= ORIG. */
  655. static void
  656. bitmap_set_and (bitmap_set_t dest, bitmap_set_t orig)
  657. {
  658. bitmap_iterator bi;
  659. unsigned int i;
  660. if (dest != orig)
  661. {
  662. bitmap_head temp;
  663. bitmap_initialize (&temp, &grand_bitmap_obstack);
  664. bitmap_and_into (&dest->values, &orig->values);
  665. bitmap_copy (&temp, &dest->expressions);
  666. EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi)
  667. {
  668. pre_expr expr = expression_for_id (i);
  669. unsigned int value_id = get_expr_value_id (expr);
  670. if (!bitmap_bit_p (&dest->values, value_id))
  671. bitmap_clear_bit (&dest->expressions, i);
  672. }
  673. bitmap_clear (&temp);
  674. }
  675. }
  676. /* Subtract all values and expressions contained in ORIG from DEST. */
  677. static bitmap_set_t
  678. bitmap_set_subtract (bitmap_set_t dest, bitmap_set_t orig)
  679. {
  680. bitmap_set_t result = bitmap_set_new ();
  681. bitmap_iterator bi;
  682. unsigned int i;
  683. bitmap_and_compl (&result->expressions, &dest->expressions,
  684. &orig->expressions);
  685. FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
  686. {
  687. pre_expr expr = expression_for_id (i);
  688. unsigned int value_id = get_expr_value_id (expr);
  689. bitmap_set_bit (&result->values, value_id);
  690. }
  691. return result;
  692. }
  693. /* Subtract all the values in bitmap set B from bitmap set A. */
  694. static void
  695. bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
  696. {
  697. unsigned int i;
  698. bitmap_iterator bi;
  699. bitmap_head temp;
  700. bitmap_initialize (&temp, &grand_bitmap_obstack);
  701. bitmap_copy (&temp, &a->expressions);
  702. EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi)
  703. {
  704. pre_expr expr = expression_for_id (i);
  705. if (bitmap_set_contains_value (b, get_expr_value_id (expr)))
  706. bitmap_remove_from_set (a, expr);
  707. }
  708. bitmap_clear (&temp);
  709. }
  710. /* Return true if bitmapped set SET contains the value VALUE_ID. */
  711. static bool
  712. bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
  713. {
  714. if (value_id_constant_p (value_id))
  715. return true;
  716. if (!set || bitmap_empty_p (&set->expressions))
  717. return false;
  718. return bitmap_bit_p (&set->values, value_id);
  719. }
  720. static inline bool
  721. bitmap_set_contains_expr (bitmap_set_t set, const pre_expr expr)
  722. {
  723. return bitmap_bit_p (&set->expressions, get_expression_id (expr));
  724. }
  725. /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
  726. static void
  727. bitmap_set_replace_value (bitmap_set_t set, unsigned int lookfor,
  728. const pre_expr expr)
  729. {
  730. bitmap exprset;
  731. unsigned int i;
  732. bitmap_iterator bi;
  733. if (value_id_constant_p (lookfor))
  734. return;
  735. if (!bitmap_set_contains_value (set, lookfor))
  736. return;
  737. /* The number of expressions having a given value is usually
  738. significantly less than the total number of expressions in SET.
  739. Thus, rather than check, for each expression in SET, whether it
  740. has the value LOOKFOR, we walk the reverse mapping that tells us
  741. what expressions have a given value, and see if any of those
  742. expressions are in our set. For large testcases, this is about
  743. 5-10x faster than walking the bitmap. If this is somehow a
  744. significant lose for some cases, we can choose which set to walk
  745. based on the set size. */
  746. exprset = value_expressions[lookfor];
  747. EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
  748. {
  749. if (bitmap_clear_bit (&set->expressions, i))
  750. {
  751. bitmap_set_bit (&set->expressions, get_expression_id (expr));
  752. return;
  753. }
  754. }
  755. gcc_unreachable ();
  756. }
  757. /* Return true if two bitmap sets are equal. */
  758. static bool
  759. bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
  760. {
  761. return bitmap_equal_p (&a->values, &b->values);
  762. }
  763. /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
  764. and add it otherwise. */
  765. static void
  766. bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
  767. {
  768. unsigned int val = get_expr_value_id (expr);
  769. if (bitmap_set_contains_value (set, val))
  770. bitmap_set_replace_value (set, val, expr);
  771. else
  772. bitmap_insert_into_set (set, expr);
  773. }
  774. /* Insert EXPR into SET if EXPR's value is not already present in
  775. SET. */
  776. static void
  777. bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
  778. {
  779. unsigned int val = get_expr_value_id (expr);
  780. gcc_checking_assert (expr->id == get_or_alloc_expression_id (expr));
  781. /* Constant values are always considered to be part of the set. */
  782. if (value_id_constant_p (val))
  783. return;
  784. /* If the value membership changed, add the expression. */
  785. if (bitmap_set_bit (&set->values, val))
  786. bitmap_set_bit (&set->expressions, expr->id);
  787. }
  788. /* Print out EXPR to outfile. */
  789. static void
  790. print_pre_expr (FILE *outfile, const pre_expr expr)
  791. {
  792. switch (expr->kind)
  793. {
  794. case CONSTANT:
  795. print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr), 0);
  796. break;
  797. case NAME:
  798. print_generic_expr (outfile, PRE_EXPR_NAME (expr), 0);
  799. break;
  800. case NARY:
  801. {
  802. unsigned int i;
  803. vn_nary_op_t nary = PRE_EXPR_NARY (expr);
  804. fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
  805. for (i = 0; i < nary->length; i++)
  806. {
  807. print_generic_expr (outfile, nary->op[i], 0);
  808. if (i != (unsigned) nary->length - 1)
  809. fprintf (outfile, ",");
  810. }
  811. fprintf (outfile, "}");
  812. }
  813. break;
  814. case REFERENCE:
  815. {
  816. vn_reference_op_t vro;
  817. unsigned int i;
  818. vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
  819. fprintf (outfile, "{");
  820. for (i = 0;
  821. ref->operands.iterate (i, &vro);
  822. i++)
  823. {
  824. bool closebrace = false;
  825. if (vro->opcode != SSA_NAME
  826. && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
  827. {
  828. fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
  829. if (vro->op0)
  830. {
  831. fprintf (outfile, "<");
  832. closebrace = true;
  833. }
  834. }
  835. if (vro->op0)
  836. {
  837. print_generic_expr (outfile, vro->op0, 0);
  838. if (vro->op1)
  839. {
  840. fprintf (outfile, ",");
  841. print_generic_expr (outfile, vro->op1, 0);
  842. }
  843. if (vro->op2)
  844. {
  845. fprintf (outfile, ",");
  846. print_generic_expr (outfile, vro->op2, 0);
  847. }
  848. }
  849. if (closebrace)
  850. fprintf (outfile, ">");
  851. if (i != ref->operands.length () - 1)
  852. fprintf (outfile, ",");
  853. }
  854. fprintf (outfile, "}");
  855. if (ref->vuse)
  856. {
  857. fprintf (outfile, "@");
  858. print_generic_expr (outfile, ref->vuse, 0);
  859. }
  860. }
  861. break;
  862. }
  863. }
  864. void debug_pre_expr (pre_expr);
  865. /* Like print_pre_expr but always prints to stderr. */
  866. DEBUG_FUNCTION void
  867. debug_pre_expr (pre_expr e)
  868. {
  869. print_pre_expr (stderr, e);
  870. fprintf (stderr, "\n");
  871. }
  872. /* Print out SET to OUTFILE. */
  873. static void
  874. print_bitmap_set (FILE *outfile, bitmap_set_t set,
  875. const char *setname, int blockindex)
  876. {
  877. fprintf (outfile, "%s[%d] := { ", setname, blockindex);
  878. if (set)
  879. {
  880. bool first = true;
  881. unsigned i;
  882. bitmap_iterator bi;
  883. FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
  884. {
  885. const pre_expr expr = expression_for_id (i);
  886. if (!first)
  887. fprintf (outfile, ", ");
  888. first = false;
  889. print_pre_expr (outfile, expr);
  890. fprintf (outfile, " (%04d)", get_expr_value_id (expr));
  891. }
  892. }
  893. fprintf (outfile, " }\n");
  894. }
  895. void debug_bitmap_set (bitmap_set_t);
  896. DEBUG_FUNCTION void
  897. debug_bitmap_set (bitmap_set_t set)
  898. {
  899. print_bitmap_set (stderr, set, "debug", 0);
  900. }
  901. void debug_bitmap_sets_for (basic_block);
  902. DEBUG_FUNCTION void
  903. debug_bitmap_sets_for (basic_block bb)
  904. {
  905. print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
  906. print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
  907. print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
  908. print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
  909. print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
  910. if (do_partial_partial)
  911. print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
  912. print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
  913. }
  914. /* Print out the expressions that have VAL to OUTFILE. */
  915. static void
  916. print_value_expressions (FILE *outfile, unsigned int val)
  917. {
  918. bitmap set = value_expressions[val];
  919. if (set)
  920. {
  921. bitmap_set x;
  922. char s[10];
  923. sprintf (s, "%04d", val);
  924. x.expressions = *set;
  925. print_bitmap_set (outfile, &x, s, 0);
  926. }
  927. }
  928. DEBUG_FUNCTION void
  929. debug_value_expressions (unsigned int val)
  930. {
  931. print_value_expressions (stderr, val);
  932. }
  933. /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
  934. represent it. */
  935. static pre_expr
  936. get_or_alloc_expr_for_constant (tree constant)
  937. {
  938. unsigned int result_id;
  939. unsigned int value_id;
  940. struct pre_expr_d expr;
  941. pre_expr newexpr;
  942. expr.kind = CONSTANT;
  943. PRE_EXPR_CONSTANT (&expr) = constant;
  944. result_id = lookup_expression_id (&expr);
  945. if (result_id != 0)
  946. return expression_for_id (result_id);
  947. newexpr = (pre_expr) pool_alloc (pre_expr_pool);
  948. newexpr->kind = CONSTANT;
  949. PRE_EXPR_CONSTANT (newexpr) = constant;
  950. alloc_expression_id (newexpr);
  951. value_id = get_or_alloc_constant_value_id (constant);
  952. add_to_value (value_id, newexpr);
  953. return newexpr;
  954. }
  955. /* Given a value id V, find the actual tree representing the constant
  956. value if there is one, and return it. Return NULL if we can't find
  957. a constant. */
  958. static tree
  959. get_constant_for_value_id (unsigned int v)
  960. {
  961. if (value_id_constant_p (v))
  962. {
  963. unsigned int i;
  964. bitmap_iterator bi;
  965. bitmap exprset = value_expressions[v];
  966. EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
  967. {
  968. pre_expr expr = expression_for_id (i);
  969. if (expr->kind == CONSTANT)
  970. return PRE_EXPR_CONSTANT (expr);
  971. }
  972. }
  973. return NULL;
  974. }
  975. /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
  976. Currently only supports constants and SSA_NAMES. */
  977. static pre_expr
  978. get_or_alloc_expr_for (tree t)
  979. {
  980. if (TREE_CODE (t) == SSA_NAME)
  981. return get_or_alloc_expr_for_name (t);
  982. else if (is_gimple_min_invariant (t))
  983. return get_or_alloc_expr_for_constant (t);
  984. else
  985. {
  986. /* More complex expressions can result from SCCVN expression
  987. simplification that inserts values for them. As they all
  988. do not have VOPs the get handled by the nary ops struct. */
  989. vn_nary_op_t result;
  990. unsigned int result_id;
  991. vn_nary_op_lookup (t, &result);
  992. if (result != NULL)
  993. {
  994. pre_expr e = (pre_expr) pool_alloc (pre_expr_pool);
  995. e->kind = NARY;
  996. PRE_EXPR_NARY (e) = result;
  997. result_id = lookup_expression_id (e);
  998. if (result_id != 0)
  999. {
  1000. pool_free (pre_expr_pool, e);
  1001. e = expression_for_id (result_id);
  1002. return e;
  1003. }
  1004. alloc_expression_id (e);
  1005. return e;
  1006. }
  1007. }
  1008. return NULL;
  1009. }
  1010. /* Return the folded version of T if T, when folded, is a gimple
  1011. min_invariant. Otherwise, return T. */
  1012. static pre_expr
  1013. fully_constant_expression (pre_expr e)
  1014. {
  1015. switch (e->kind)
  1016. {
  1017. case CONSTANT:
  1018. return e;
  1019. case NARY:
  1020. {
  1021. vn_nary_op_t nary = PRE_EXPR_NARY (e);
  1022. switch (TREE_CODE_CLASS (nary->opcode))
  1023. {
  1024. case tcc_binary:
  1025. case tcc_comparison:
  1026. {
  1027. /* We have to go from trees to pre exprs to value ids to
  1028. constants. */
  1029. tree naryop0 = nary->op[0];
  1030. tree naryop1 = nary->op[1];
  1031. tree result;
  1032. if (!is_gimple_min_invariant (naryop0))
  1033. {
  1034. pre_expr rep0 = get_or_alloc_expr_for (naryop0);
  1035. unsigned int vrep0 = get_expr_value_id (rep0);
  1036. tree const0 = get_constant_for_value_id (vrep0);
  1037. if (const0)
  1038. naryop0 = fold_convert (TREE_TYPE (naryop0), const0);
  1039. }
  1040. if (!is_gimple_min_invariant (naryop1))
  1041. {
  1042. pre_expr rep1 = get_or_alloc_expr_for (naryop1);
  1043. unsigned int vrep1 = get_expr_value_id (rep1);
  1044. tree const1 = get_constant_for_value_id (vrep1);
  1045. if (const1)
  1046. naryop1 = fold_convert (TREE_TYPE (naryop1), const1);
  1047. }
  1048. result = fold_binary (nary->opcode, nary->type,
  1049. naryop0, naryop1);
  1050. if (result && is_gimple_min_invariant (result))
  1051. return get_or_alloc_expr_for_constant (result);
  1052. /* We might have simplified the expression to a
  1053. SSA_NAME for example from x_1 * 1. But we cannot
  1054. insert a PHI for x_1 unconditionally as x_1 might
  1055. not be available readily. */
  1056. return e;
  1057. }
  1058. case tcc_reference:
  1059. if (nary->opcode != REALPART_EXPR
  1060. && nary->opcode != IMAGPART_EXPR
  1061. && nary->opcode != VIEW_CONVERT_EXPR)
  1062. return e;
  1063. /* Fallthrough. */
  1064. case tcc_unary:
  1065. {
  1066. /* We have to go from trees to pre exprs to value ids to
  1067. constants. */
  1068. tree naryop0 = nary->op[0];
  1069. tree const0, result;
  1070. if (is_gimple_min_invariant (naryop0))
  1071. const0 = naryop0;
  1072. else
  1073. {
  1074. pre_expr rep0 = get_or_alloc_expr_for (naryop0);
  1075. unsigned int vrep0 = get_expr_value_id (rep0);
  1076. const0 = get_constant_for_value_id (vrep0);
  1077. }
  1078. result = NULL;
  1079. if (const0)
  1080. {
  1081. tree type1 = TREE_TYPE (nary->op[0]);
  1082. const0 = fold_convert (type1, const0);
  1083. result = fold_unary (nary->opcode, nary->type, const0);
  1084. }
  1085. if (result && is_gimple_min_invariant (result))
  1086. return get_or_alloc_expr_for_constant (result);
  1087. return e;
  1088. }
  1089. default:
  1090. return e;
  1091. }
  1092. }
  1093. case REFERENCE:
  1094. {
  1095. vn_reference_t ref = PRE_EXPR_REFERENCE (e);
  1096. tree folded;
  1097. if ((folded = fully_constant_vn_reference_p (ref)))
  1098. return get_or_alloc_expr_for_constant (folded);
  1099. return e;
  1100. }
  1101. default:
  1102. return e;
  1103. }
  1104. return e;
  1105. }
  1106. /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
  1107. it has the value it would have in BLOCK. Set *SAME_VALID to true
  1108. in case the new vuse doesn't change the value id of the OPERANDS. */
  1109. static tree
  1110. translate_vuse_through_block (vec<vn_reference_op_s> operands,
  1111. alias_set_type set, tree type, tree vuse,
  1112. basic_block phiblock,
  1113. basic_block block, bool *same_valid)
  1114. {
  1115. gimple phi = SSA_NAME_DEF_STMT (vuse);
  1116. ao_ref ref;
  1117. edge e = NULL;
  1118. bool use_oracle;
  1119. *same_valid = true;
  1120. if (gimple_bb (phi) != phiblock)
  1121. return vuse;
  1122. use_oracle = ao_ref_init_from_vn_reference (&ref, set, type, operands);
  1123. /* Use the alias-oracle to find either the PHI node in this block,
  1124. the first VUSE used in this block that is equivalent to vuse or
  1125. the first VUSE which definition in this block kills the value. */
  1126. if (gimple_code (phi) == GIMPLE_PHI)
  1127. e = find_edge (block, phiblock);
  1128. else if (use_oracle)
  1129. while (!stmt_may_clobber_ref_p_1 (phi, &ref))
  1130. {
  1131. vuse = gimple_vuse (phi);
  1132. phi = SSA_NAME_DEF_STMT (vuse);
  1133. if (gimple_bb (phi) != phiblock)
  1134. return vuse;
  1135. if (gimple_code (phi) == GIMPLE_PHI)
  1136. {
  1137. e = find_edge (block, phiblock);
  1138. break;
  1139. }
  1140. }
  1141. else
  1142. return NULL_TREE;
  1143. if (e)
  1144. {
  1145. if (use_oracle)
  1146. {
  1147. bitmap visited = NULL;
  1148. unsigned int cnt;
  1149. /* Try to find a vuse that dominates this phi node by skipping
  1150. non-clobbering statements. */
  1151. vuse = get_continuation_for_phi (phi, &ref, &cnt, &visited, false,
  1152. NULL, NULL);
  1153. if (visited)
  1154. BITMAP_FREE (visited);
  1155. }
  1156. else
  1157. vuse = NULL_TREE;
  1158. if (!vuse)
  1159. {
  1160. /* If we didn't find any, the value ID can't stay the same,
  1161. but return the translated vuse. */
  1162. *same_valid = false;
  1163. vuse = PHI_ARG_DEF (phi, e->dest_idx);
  1164. }
  1165. /* ??? We would like to return vuse here as this is the canonical
  1166. upmost vdef that this reference is associated with. But during
  1167. insertion of the references into the hash tables we only ever
  1168. directly insert with their direct gimple_vuse, hence returning
  1169. something else would make us not find the other expression. */
  1170. return PHI_ARG_DEF (phi, e->dest_idx);
  1171. }
  1172. return NULL_TREE;
  1173. }
  1174. /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
  1175. SET2. This is used to avoid making a set consisting of the union
  1176. of PA_IN and ANTIC_IN during insert. */
  1177. static inline pre_expr
  1178. find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2)
  1179. {
  1180. pre_expr result;
  1181. result = bitmap_find_leader (set1, val);
  1182. if (!result && set2)
  1183. result = bitmap_find_leader (set2, val);
  1184. return result;
  1185. }
  1186. /* Get the tree type for our PRE expression e. */
  1187. static tree
  1188. get_expr_type (const pre_expr e)
  1189. {
  1190. switch (e->kind)
  1191. {
  1192. case NAME:
  1193. return TREE_TYPE (PRE_EXPR_NAME (e));
  1194. case CONSTANT:
  1195. return TREE_TYPE (PRE_EXPR_CONSTANT (e));
  1196. case REFERENCE:
  1197. return PRE_EXPR_REFERENCE (e)->type;
  1198. case NARY:
  1199. return PRE_EXPR_NARY (e)->type;
  1200. }
  1201. gcc_unreachable ();
  1202. }
  1203. /* Get a representative SSA_NAME for a given expression.
  1204. Since all of our sub-expressions are treated as values, we require
  1205. them to be SSA_NAME's for simplicity.
  1206. Prior versions of GVNPRE used to use "value handles" here, so that
  1207. an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
  1208. either case, the operands are really values (IE we do not expect
  1209. them to be usable without finding leaders). */
  1210. static tree
  1211. get_representative_for (const pre_expr e)
  1212. {
  1213. tree name;
  1214. unsigned int value_id = get_expr_value_id (e);
  1215. switch (e->kind)
  1216. {
  1217. case NAME:
  1218. return PRE_EXPR_NAME (e);
  1219. case CONSTANT:
  1220. return PRE_EXPR_CONSTANT (e);
  1221. case NARY:
  1222. case REFERENCE:
  1223. {
  1224. /* Go through all of the expressions representing this value
  1225. and pick out an SSA_NAME. */
  1226. unsigned int i;
  1227. bitmap_iterator bi;
  1228. bitmap exprs = value_expressions[value_id];
  1229. EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
  1230. {
  1231. pre_expr rep = expression_for_id (i);
  1232. if (rep->kind == NAME)
  1233. return PRE_EXPR_NAME (rep);
  1234. else if (rep->kind == CONSTANT)
  1235. return PRE_EXPR_CONSTANT (rep);
  1236. }
  1237. }
  1238. break;
  1239. }
  1240. /* If we reached here we couldn't find an SSA_NAME. This can
  1241. happen when we've discovered a value that has never appeared in
  1242. the program as set to an SSA_NAME, as the result of phi translation.
  1243. Create one here.
  1244. ??? We should be able to re-use this when we insert the statement
  1245. to compute it. */
  1246. name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
  1247. VN_INFO_GET (name)->value_id = value_id;
  1248. VN_INFO (name)->valnum = name;
  1249. /* ??? For now mark this SSA name for release by SCCVN. */
  1250. VN_INFO (name)->needs_insertion = true;
  1251. add_to_value (value_id, get_or_alloc_expr_for_name (name));
  1252. if (dump_file && (dump_flags & TDF_DETAILS))
  1253. {
  1254. fprintf (dump_file, "Created SSA_NAME representative ");
  1255. print_generic_expr (dump_file, name, 0);
  1256. fprintf (dump_file, " for expression:");
  1257. print_pre_expr (dump_file, e);
  1258. fprintf (dump_file, " (%04d)\n", value_id);
  1259. }
  1260. return name;
  1261. }
  1262. static pre_expr
  1263. phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
  1264. basic_block pred, basic_block phiblock);
  1265. /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
  1266. the phis in PRED. Return NULL if we can't find a leader for each part
  1267. of the translated expression. */
  1268. static pre_expr
  1269. phi_translate_1 (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
  1270. basic_block pred, basic_block phiblock)
  1271. {
  1272. switch (expr->kind)
  1273. {
  1274. case NARY:
  1275. {
  1276. unsigned int i;
  1277. bool changed = false;
  1278. vn_nary_op_t nary = PRE_EXPR_NARY (expr);
  1279. vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
  1280. sizeof_vn_nary_op (nary->length));
  1281. memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
  1282. for (i = 0; i < newnary->length; i++)
  1283. {
  1284. if (TREE_CODE (newnary->op[i]) != SSA_NAME)
  1285. continue;
  1286. else
  1287. {
  1288. pre_expr leader, result;
  1289. unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
  1290. leader = find_leader_in_sets (op_val_id, set1, set2);
  1291. result = phi_translate (leader, set1, set2, pred, phiblock);
  1292. if (result && result != leader)
  1293. {
  1294. tree name = get_representative_for (result);
  1295. if (!name)
  1296. return NULL;
  1297. newnary->op[i] = name;
  1298. }
  1299. else if (!result)
  1300. return NULL;
  1301. changed |= newnary->op[i] != nary->op[i];
  1302. }
  1303. }
  1304. if (changed)
  1305. {
  1306. pre_expr constant;
  1307. unsigned int new_val_id;
  1308. tree result = vn_nary_op_lookup_pieces (newnary->length,
  1309. newnary->opcode,
  1310. newnary->type,
  1311. &newnary->op[0],
  1312. &nary);
  1313. if (result && is_gimple_min_invariant (result))
  1314. return get_or_alloc_expr_for_constant (result);
  1315. expr = (pre_expr) pool_alloc (pre_expr_pool);
  1316. expr->kind = NARY;
  1317. expr->id = 0;
  1318. if (nary)
  1319. {
  1320. PRE_EXPR_NARY (expr) = nary;
  1321. constant = fully_constant_expression (expr);
  1322. if (constant != expr)
  1323. return constant;
  1324. new_val_id = nary->value_id;
  1325. get_or_alloc_expression_id (expr);
  1326. }
  1327. else
  1328. {
  1329. new_val_id = get_next_value_id ();
  1330. value_expressions.safe_grow_cleared (get_max_value_id () + 1);
  1331. nary = vn_nary_op_insert_pieces (newnary->length,
  1332. newnary->opcode,
  1333. newnary->type,
  1334. &newnary->op[0],
  1335. result, new_val_id);
  1336. PRE_EXPR_NARY (expr) = nary;
  1337. constant = fully_constant_expression (expr);
  1338. if (constant != expr)
  1339. return constant;
  1340. get_or_alloc_expression_id (expr);
  1341. }
  1342. add_to_value (new_val_id, expr);
  1343. }
  1344. return expr;
  1345. }
  1346. break;
  1347. case REFERENCE:
  1348. {
  1349. vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
  1350. vec<vn_reference_op_s> operands = ref->operands;
  1351. tree vuse = ref->vuse;
  1352. tree newvuse = vuse;
  1353. vec<vn_reference_op_s> newoperands = vNULL;
  1354. bool changed = false, same_valid = true;
  1355. unsigned int i, n;
  1356. vn_reference_op_t operand;
  1357. vn_reference_t newref;
  1358. for (i = 0; operands.iterate (i, &operand); i++)
  1359. {
  1360. pre_expr opresult;
  1361. pre_expr leader;
  1362. tree op[3];
  1363. tree type = operand->type;
  1364. vn_reference_op_s newop = *operand;
  1365. op[0] = operand->op0;
  1366. op[1] = operand->op1;
  1367. op[2] = operand->op2;
  1368. for (n = 0; n < 3; ++n)
  1369. {
  1370. unsigned int op_val_id;
  1371. if (!op[n])
  1372. continue;
  1373. if (TREE_CODE (op[n]) != SSA_NAME)
  1374. {
  1375. /* We can't possibly insert these. */
  1376. if (n != 0
  1377. && !is_gimple_min_invariant (op[n]))
  1378. break;
  1379. continue;
  1380. }
  1381. op_val_id = VN_INFO (op[n])->value_id;
  1382. leader = find_leader_in_sets (op_val_id, set1, set2);
  1383. if (!leader)
  1384. break;
  1385. opresult = phi_translate (leader, set1, set2, pred, phiblock);
  1386. if (!opresult)
  1387. break;
  1388. if (opresult != leader)
  1389. {
  1390. tree name = get_representative_for (opresult);
  1391. if (!name)
  1392. break;
  1393. changed |= name != op[n];
  1394. op[n] = name;
  1395. }
  1396. }
  1397. if (n != 3)
  1398. {
  1399. newoperands.release ();
  1400. return NULL;
  1401. }
  1402. if (!changed)
  1403. continue;
  1404. if (!newoperands.exists ())
  1405. newoperands = operands.copy ();
  1406. /* We may have changed from an SSA_NAME to a constant */
  1407. if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
  1408. newop.opcode = TREE_CODE (op[0]);
  1409. newop.type = type;
  1410. newop.op0 = op[0];
  1411. newop.op1 = op[1];
  1412. newop.op2 = op[2];
  1413. newoperands[i] = newop;
  1414. }
  1415. gcc_checking_assert (i == operands.length ());
  1416. if (vuse)
  1417. {
  1418. newvuse = translate_vuse_through_block (newoperands.exists ()
  1419. ? newoperands : operands,
  1420. ref->set, ref->type,
  1421. vuse, phiblock, pred,
  1422. &same_valid);
  1423. if (newvuse == NULL_TREE)
  1424. {
  1425. newoperands.release ();
  1426. return NULL;
  1427. }
  1428. }
  1429. if (changed || newvuse != vuse)
  1430. {
  1431. unsigned int new_val_id;
  1432. pre_expr constant;
  1433. tree result = vn_reference_lookup_pieces (newvuse, ref->set,
  1434. ref->type,
  1435. newoperands.exists ()
  1436. ? newoperands : operands,
  1437. &newref, VN_WALK);
  1438. if (result)
  1439. newoperands.release ();
  1440. /* We can always insert constants, so if we have a partial
  1441. redundant constant load of another type try to translate it
  1442. to a constant of appropriate type. */
  1443. if (result && is_gimple_min_invariant (result))
  1444. {
  1445. tree tem = result;
  1446. if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
  1447. {
  1448. tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
  1449. if (tem && !is_gimple_min_invariant (tem))
  1450. tem = NULL_TREE;
  1451. }
  1452. if (tem)
  1453. return get_or_alloc_expr_for_constant (tem);
  1454. }
  1455. /* If we'd have to convert things we would need to validate
  1456. if we can insert the translated expression. So fail
  1457. here for now - we cannot insert an alias with a different
  1458. type in the VN tables either, as that would assert. */
  1459. if (result
  1460. && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
  1461. return NULL;
  1462. else if (!result && newref
  1463. && !useless_type_conversion_p (ref->type, newref->type))
  1464. {
  1465. newoperands.release ();
  1466. return NULL;
  1467. }
  1468. expr = (pre_expr) pool_alloc (pre_expr_pool);
  1469. expr->kind = REFERENCE;
  1470. expr->id = 0;
  1471. if (newref)
  1472. {
  1473. PRE_EXPR_REFERENCE (expr) = newref;
  1474. constant = fully_constant_expression (expr);
  1475. if (constant != expr)
  1476. return constant;
  1477. new_val_id = newref->value_id;
  1478. get_or_alloc_expression_id (expr);
  1479. }
  1480. else
  1481. {
  1482. if (changed || !same_valid)
  1483. {
  1484. new_val_id = get_next_value_id ();
  1485. value_expressions.safe_grow_cleared
  1486. (get_max_value_id () + 1);
  1487. }
  1488. else
  1489. new_val_id = ref->value_id;
  1490. if (!newoperands.exists ())
  1491. newoperands = operands.copy ();
  1492. newref = vn_reference_insert_pieces (newvuse, ref->set,
  1493. ref->type,
  1494. newoperands,
  1495. result, new_val_id);
  1496. newoperands = vNULL;
  1497. PRE_EXPR_REFERENCE (expr) = newref;
  1498. constant = fully_constant_expression (expr);
  1499. if (constant != expr)
  1500. return constant;
  1501. get_or_alloc_expression_id (expr);
  1502. }
  1503. add_to_value (new_val_id, expr);
  1504. }
  1505. newoperands.release ();
  1506. return expr;
  1507. }
  1508. break;
  1509. case NAME:
  1510. {
  1511. tree name = PRE_EXPR_NAME (expr);
  1512. gimple def_stmt = SSA_NAME_DEF_STMT (name);
  1513. /* If the SSA name is defined by a PHI node in this block,
  1514. translate it. */
  1515. if (gimple_code (def_stmt) == GIMPLE_PHI
  1516. && gimple_bb (def_stmt) == phiblock)
  1517. {
  1518. edge e = find_edge (pred, gimple_bb (def_stmt));
  1519. tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
  1520. /* Handle constant. */
  1521. if (is_gimple_min_invariant (def))
  1522. return get_or_alloc_expr_for_constant (def);
  1523. return get_or_alloc_expr_for_name (def);
  1524. }
  1525. /* Otherwise return it unchanged - it will get removed if its
  1526. value is not available in PREDs AVAIL_OUT set of expressions
  1527. by the subtraction of TMP_GEN. */
  1528. return expr;
  1529. }
  1530. default:
  1531. gcc_unreachable ();
  1532. }
  1533. }
  1534. /* Wrapper around phi_translate_1 providing caching functionality. */
  1535. static pre_expr
  1536. phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
  1537. basic_block pred, basic_block phiblock)
  1538. {
  1539. expr_pred_trans_t slot = NULL;
  1540. pre_expr phitrans;
  1541. if (!expr)
  1542. return NULL;
  1543. /* Constants contain no values that need translation. */
  1544. if (expr->kind == CONSTANT)
  1545. return expr;
  1546. if (value_id_constant_p (get_expr_value_id (expr)))
  1547. return expr;
  1548. /* Don't add translations of NAMEs as those are cheap to translate. */
  1549. if (expr->kind != NAME)
  1550. {
  1551. if (phi_trans_add (&slot, expr, pred))
  1552. return slot->v;
  1553. /* Store NULL for the value we want to return in the case of
  1554. recursing. */
  1555. slot->v = NULL;
  1556. }
  1557. /* Translate. */
  1558. phitrans = phi_translate_1 (expr, set1, set2, pred, phiblock);
  1559. if (slot)
  1560. {
  1561. if (phitrans)
  1562. slot->v = phitrans;
  1563. else
  1564. /* Remove failed translations again, they cause insert
  1565. iteration to not pick up new opportunities reliably. */
  1566. phi_translate_table->remove_elt_with_hash (slot, slot->hashcode);
  1567. }
  1568. return phitrans;
  1569. }
  1570. /* For each expression in SET, translate the values through phi nodes
  1571. in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
  1572. expressions in DEST. */
  1573. static void
  1574. phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
  1575. basic_block phiblock)
  1576. {
  1577. vec<pre_expr> exprs;
  1578. pre_expr expr;
  1579. int i;
  1580. if (gimple_seq_empty_p (phi_nodes (phiblock)))
  1581. {
  1582. bitmap_set_copy (dest, set);
  1583. return;
  1584. }
  1585. exprs = sorted_array_from_bitmap_set (set);
  1586. FOR_EACH_VEC_ELT (exprs, i, expr)
  1587. {
  1588. pre_expr translated;
  1589. translated = phi_translate (expr, set, NULL, pred, phiblock);
  1590. if (!translated)
  1591. continue;
  1592. /* We might end up with multiple expressions from SET being
  1593. translated to the same value. In this case we do not want
  1594. to retain the NARY or REFERENCE expression but prefer a NAME
  1595. which would be the leader. */
  1596. if (translated->kind == NAME)
  1597. bitmap_value_replace_in_set (dest, translated);
  1598. else
  1599. bitmap_value_insert_into_set (dest, translated);
  1600. }
  1601. exprs.release ();
  1602. }
  1603. /* Find the leader for a value (i.e., the name representing that
  1604. value) in a given set, and return it. Return NULL if no leader
  1605. is found. */
  1606. static pre_expr
  1607. bitmap_find_leader (bitmap_set_t set, unsigned int val)
  1608. {
  1609. if (value_id_constant_p (val))
  1610. {
  1611. unsigned int i;
  1612. bitmap_iterator bi;
  1613. bitmap exprset = value_expressions[val];
  1614. EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
  1615. {
  1616. pre_expr expr = expression_for_id (i);
  1617. if (expr->kind == CONSTANT)
  1618. return expr;
  1619. }
  1620. }
  1621. if (bitmap_set_contains_value (set, val))
  1622. {
  1623. /* Rather than walk the entire bitmap of expressions, and see
  1624. whether any of them has the value we are looking for, we look
  1625. at the reverse mapping, which tells us the set of expressions
  1626. that have a given value (IE value->expressions with that
  1627. value) and see if any of those expressions are in our set.
  1628. The number of expressions per value is usually significantly
  1629. less than the number of expressions in the set. In fact, for
  1630. large testcases, doing it this way is roughly 5-10x faster
  1631. than walking the bitmap.
  1632. If this is somehow a significant lose for some cases, we can
  1633. choose which set to walk based on which set is smaller. */
  1634. unsigned int i;
  1635. bitmap_iterator bi;
  1636. bitmap exprset = value_expressions[val];
  1637. EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
  1638. return expression_for_id (i);
  1639. }
  1640. return NULL;
  1641. }
  1642. /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
  1643. BLOCK by seeing if it is not killed in the block. Note that we are
  1644. only determining whether there is a store that kills it. Because
  1645. of the order in which clean iterates over values, we are guaranteed
  1646. that altered operands will have caused us to be eliminated from the
  1647. ANTIC_IN set already. */
  1648. static bool
  1649. value_dies_in_block_x (pre_expr expr, basic_block block)
  1650. {
  1651. tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
  1652. vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
  1653. gimple def;
  1654. gimple_stmt_iterator gsi;
  1655. unsigned id = get_expression_id (expr);
  1656. bool res = false;
  1657. ao_ref ref;
  1658. if (!vuse)
  1659. return false;
  1660. /* Lookup a previously calculated result. */
  1661. if (EXPR_DIES (block)
  1662. && bitmap_bit_p (EXPR_DIES (block), id * 2))
  1663. return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
  1664. /* A memory expression {e, VUSE} dies in the block if there is a
  1665. statement that may clobber e. If, starting statement walk from the
  1666. top of the basic block, a statement uses VUSE there can be no kill
  1667. inbetween that use and the original statement that loaded {e, VUSE},
  1668. so we can stop walking. */
  1669. ref.base = NULL_TREE;
  1670. for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
  1671. {
  1672. tree def_vuse, def_vdef;
  1673. def = gsi_stmt (gsi);
  1674. def_vuse = gimple_vuse (def);
  1675. def_vdef = gimple_vdef (def);
  1676. /* Not a memory statement. */
  1677. if (!def_vuse)
  1678. continue;
  1679. /* Not a may-def. */
  1680. if (!def_vdef)
  1681. {
  1682. /* A load with the same VUSE, we're done. */
  1683. if (def_vuse == vuse)
  1684. break;
  1685. continue;
  1686. }
  1687. /* Init ref only if we really need it. */
  1688. if (ref.base == NULL_TREE
  1689. && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->type,
  1690. refx->operands))
  1691. {
  1692. res = true;
  1693. break;
  1694. }
  1695. /* If the statement may clobber expr, it dies. */
  1696. if (stmt_may_clobber_ref_p_1 (def, &ref))
  1697. {
  1698. res = true;
  1699. break;
  1700. }
  1701. }
  1702. /* Remember the result. */
  1703. if (!EXPR_DIES (block))
  1704. EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
  1705. bitmap_set_bit (EXPR_DIES (block), id * 2);
  1706. if (res)
  1707. bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
  1708. return res;
  1709. }
  1710. /* Determine if OP is valid in SET1 U SET2, which it is when the union
  1711. contains its value-id. */
  1712. static bool
  1713. op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
  1714. {
  1715. if (op && TREE_CODE (op) == SSA_NAME)
  1716. {
  1717. unsigned int value_id = VN_INFO (op)->value_id;
  1718. if (!(bitmap_set_contains_value (set1, value_id)
  1719. || (set2 && bitmap_set_contains_value (set2, value_id))))
  1720. return false;
  1721. }
  1722. return true;
  1723. }
  1724. /* Determine if the expression EXPR is valid in SET1 U SET2.
  1725. ONLY SET2 CAN BE NULL.
  1726. This means that we have a leader for each part of the expression
  1727. (if it consists of values), or the expression is an SSA_NAME.
  1728. For loads/calls, we also see if the vuse is killed in this block. */
  1729. static bool
  1730. valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
  1731. {
  1732. switch (expr->kind)
  1733. {
  1734. case NAME:
  1735. /* By construction all NAMEs are available. Non-available
  1736. NAMEs are removed by subtracting TMP_GEN from the sets. */
  1737. return true;
  1738. case NARY:
  1739. {
  1740. unsigned int i;
  1741. vn_nary_op_t nary = PRE_EXPR_NARY (expr);
  1742. for (i = 0; i < nary->length; i++)
  1743. if (!op_valid_in_sets (set1, set2, nary->op[i]))
  1744. return false;
  1745. return true;
  1746. }
  1747. break;
  1748. case REFERENCE:
  1749. {
  1750. vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
  1751. vn_reference_op_t vro;
  1752. unsigned int i;
  1753. FOR_EACH_VEC_ELT (ref->operands, i, vro)
  1754. {
  1755. if (!op_valid_in_sets (set1, set2, vro->op0)
  1756. || !op_valid_in_sets (set1, set2, vro->op1)
  1757. || !op_valid_in_sets (set1, set2, vro->op2))
  1758. return false;
  1759. }
  1760. return true;
  1761. }
  1762. default:
  1763. gcc_unreachable ();
  1764. }
  1765. }
  1766. /* Clean the set of expressions that are no longer valid in SET1 or
  1767. SET2. This means expressions that are made up of values we have no
  1768. leaders for in SET1 or SET2. This version is used for partial
  1769. anticipation, which means it is not valid in either ANTIC_IN or
  1770. PA_IN. */
  1771. static void
  1772. dependent_clean (bitmap_set_t set1, bitmap_set_t set2)
  1773. {
  1774. vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
  1775. pre_expr expr;
  1776. int i;
  1777. FOR_EACH_VEC_ELT (exprs, i, expr)
  1778. {
  1779. if (!valid_in_sets (set1, set2, expr))
  1780. bitmap_remove_from_set (set1, expr);
  1781. }
  1782. exprs.release ();
  1783. }
  1784. /* Clean the set of expressions that are no longer valid in SET. This
  1785. means expressions that are made up of values we have no leaders for
  1786. in SET. */
  1787. static void
  1788. clean (bitmap_set_t set)
  1789. {
  1790. vec<pre_expr> exprs = sorted_array_from_bitmap_set (set);
  1791. pre_expr expr;
  1792. int i;
  1793. FOR_EACH_VEC_ELT (exprs, i, expr)
  1794. {
  1795. if (!valid_in_sets (set, NULL, expr))
  1796. bitmap_remove_from_set (set, expr);
  1797. }
  1798. exprs.release ();
  1799. }
  1800. /* Clean the set of expressions that are no longer valid in SET because
  1801. they are clobbered in BLOCK or because they trap and may not be executed. */
  1802. static void
  1803. prune_clobbered_mems (bitmap_set_t set, basic_block block)
  1804. {
  1805. bitmap_iterator bi;
  1806. unsigned i;
  1807. FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
  1808. {
  1809. pre_expr expr = expression_for_id (i);
  1810. if (expr->kind == REFERENCE)
  1811. {
  1812. vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
  1813. if (ref->vuse)
  1814. {
  1815. gimple def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
  1816. if (!gimple_nop_p (def_stmt)
  1817. && ((gimple_bb (def_stmt) != block
  1818. && !dominated_by_p (CDI_DOMINATORS,
  1819. block, gimple_bb (def_stmt)))
  1820. || (gimple_bb (def_stmt) == block
  1821. && value_dies_in_block_x (expr, block))))
  1822. bitmap_remove_from_set (set, expr);
  1823. }
  1824. }
  1825. else if (expr->kind == NARY)
  1826. {
  1827. vn_nary_op_t nary = PRE_EXPR_NARY (expr);
  1828. /* If the NARY may trap make sure the block does not contain
  1829. a possible exit point.
  1830. ??? This is overly conservative if we translate AVAIL_OUT
  1831. as the available expression might be after the exit point. */
  1832. if (BB_MAY_NOTRETURN (block)
  1833. && vn_nary_may_trap (nary))
  1834. bitmap_remove_from_set (set, expr);
  1835. }
  1836. }
  1837. }
  1838. static sbitmap has_abnormal_preds;
  1839. /* List of blocks that may have changed during ANTIC computation and
  1840. thus need to be iterated over. */
  1841. static sbitmap changed_blocks;
  1842. /* Compute the ANTIC set for BLOCK.
  1843. If succs(BLOCK) > 1 then
  1844. ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
  1845. else if succs(BLOCK) == 1 then
  1846. ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
  1847. ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
  1848. */
  1849. static bool
  1850. compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
  1851. {
  1852. bool changed = false;
  1853. bitmap_set_t S, old, ANTIC_OUT;
  1854. bitmap_iterator bi;
  1855. unsigned int bii;
  1856. edge e;
  1857. edge_iterator ei;
  1858. old = ANTIC_OUT = S = NULL;
  1859. BB_VISITED (block) = 1;
  1860. /* If any edges from predecessors are abnormal, antic_in is empty,
  1861. so do nothing. */
  1862. if (block_has_abnormal_pred_edge)
  1863. goto maybe_dump_sets;
  1864. old = ANTIC_IN (block);
  1865. ANTIC_OUT = bitmap_set_new ();
  1866. /* If the block has no successors, ANTIC_OUT is empty. */
  1867. if (EDGE_COUNT (block->succs) == 0)
  1868. ;
  1869. /* If we have one successor, we could have some phi nodes to
  1870. translate through. */
  1871. else if (single_succ_p (block))
  1872. {
  1873. basic_block succ_bb = single_succ (block);
  1874. gcc_assert (BB_VISITED (succ_bb));
  1875. phi_translate_set (ANTIC_OUT, ANTIC_IN (succ_bb), block, succ_bb);
  1876. }
  1877. /* If we have multiple successors, we take the intersection of all of
  1878. them. Note that in the case of loop exit phi nodes, we may have
  1879. phis to translate through. */
  1880. else
  1881. {
  1882. size_t i;
  1883. basic_block bprime, first = NULL;
  1884. auto_vec<basic_block> worklist (EDGE_COUNT (block->succs));
  1885. FOR_EACH_EDGE (e, ei, block->succs)
  1886. {
  1887. if (!first
  1888. && BB_VISITED (e->dest))
  1889. first = e->dest;
  1890. else if (BB_VISITED (e->dest))
  1891. worklist.quick_push (e->dest);
  1892. }
  1893. /* Of multiple successors we have to have visited one already
  1894. which is guaranteed by iteration order. */
  1895. gcc_assert (first != NULL);
  1896. phi_translate_set (ANTIC_OUT, ANTIC_IN (first), block, first);
  1897. FOR_EACH_VEC_ELT (worklist, i, bprime)
  1898. {
  1899. if (!gimple_seq_empty_p (phi_nodes (bprime)))
  1900. {
  1901. bitmap_set_t tmp = bitmap_set_new ();
  1902. phi_translate_set (tmp, ANTIC_IN (bprime), block, bprime);
  1903. bitmap_set_and (ANTIC_OUT, tmp);
  1904. bitmap_set_free (tmp);
  1905. }
  1906. else
  1907. bitmap_set_and (ANTIC_OUT, ANTIC_IN (bprime));
  1908. }
  1909. }
  1910. /* Prune expressions that are clobbered in block and thus become
  1911. invalid if translated from ANTIC_OUT to ANTIC_IN. */
  1912. prune_clobbered_mems (ANTIC_OUT, block);
  1913. /* Generate ANTIC_OUT - TMP_GEN. */
  1914. S = bitmap_set_subtract (ANTIC_OUT, TMP_GEN (block));
  1915. /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
  1916. ANTIC_IN (block) = bitmap_set_subtract (EXP_GEN (block),
  1917. TMP_GEN (block));
  1918. /* Then union in the ANTIC_OUT - TMP_GEN values,
  1919. to get ANTIC_OUT U EXP_GEN - TMP_GEN */
  1920. FOR_EACH_EXPR_ID_IN_SET (S, bii, bi)
  1921. bitmap_value_insert_into_set (ANTIC_IN (block),
  1922. expression_for_id (bii));
  1923. clean (ANTIC_IN (block));
  1924. if (!bitmap_set_equal (old, ANTIC_IN (block)))
  1925. {
  1926. changed = true;
  1927. bitmap_set_bit (changed_blocks, block->index);
  1928. FOR_EACH_EDGE (e, ei, block->preds)
  1929. bitmap_set_bit (changed_blocks, e->src->index);
  1930. }
  1931. else
  1932. bitmap_clear_bit (changed_blocks, block->index);
  1933. maybe_dump_sets:
  1934. if (dump_file && (dump_flags & TDF_DETAILS))
  1935. {
  1936. if (ANTIC_OUT)
  1937. print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
  1938. print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
  1939. block->index);
  1940. if (S)
  1941. print_bitmap_set (dump_file, S, "S", block->index);
  1942. }
  1943. if (old)
  1944. bitmap_set_free (old);
  1945. if (S)
  1946. bitmap_set_free (S);
  1947. if (ANTIC_OUT)
  1948. bitmap_set_free (ANTIC_OUT);
  1949. return changed;
  1950. }
  1951. /* Compute PARTIAL_ANTIC for BLOCK.
  1952. If succs(BLOCK) > 1 then
  1953. PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
  1954. in ANTIC_OUT for all succ(BLOCK)
  1955. else if succs(BLOCK) == 1 then
  1956. PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
  1957. PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
  1958. - ANTIC_IN[BLOCK])
  1959. */
  1960. static bool
  1961. compute_partial_antic_aux (basic_block block,
  1962. bool block_has_abnormal_pred_edge)
  1963. {
  1964. bool changed = false;
  1965. bitmap_set_t old_PA_IN;
  1966. bitmap_set_t PA_OUT;
  1967. edge e;
  1968. edge_iterator ei;
  1969. unsigned long max_pa = PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH);
  1970. old_PA_IN = PA_OUT = NULL;
  1971. /* If any edges from predecessors are abnormal, antic_in is empty,
  1972. so do nothing. */
  1973. if (block_has_abnormal_pred_edge)
  1974. goto maybe_dump_sets;
  1975. /* If there are too many partially anticipatable values in the
  1976. block, phi_translate_set can take an exponential time: stop
  1977. before the translation starts. */
  1978. if (max_pa
  1979. && single_succ_p (block)
  1980. && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
  1981. goto maybe_dump_sets;
  1982. old_PA_IN = PA_IN (block);
  1983. PA_OUT = bitmap_set_new ();
  1984. /* If the block has no successors, ANTIC_OUT is empty. */
  1985. if (EDGE_COUNT (block->succs) == 0)
  1986. ;
  1987. /* If we have one successor, we could have some phi nodes to
  1988. translate through. Note that we can't phi translate across DFS
  1989. back edges in partial antic, because it uses a union operation on
  1990. the successors. For recurrences like IV's, we will end up
  1991. generating a new value in the set on each go around (i + 3 (VH.1)
  1992. VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
  1993. else if (single_succ_p (block))
  1994. {
  1995. basic_block succ = single_succ (block);
  1996. if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK))
  1997. phi_translate_set (PA_OUT, PA_IN (succ), block, succ);
  1998. }
  1999. /* If we have multiple successors, we take the union of all of
  2000. them. */
  2001. else
  2002. {
  2003. size_t i;
  2004. basic_block bprime;
  2005. auto_vec<basic_block> worklist (EDGE_COUNT (block->succs));
  2006. FOR_EACH_EDGE (e, ei, block->succs)
  2007. {
  2008. if (e->flags & EDGE_DFS_BACK)
  2009. continue;
  2010. worklist.quick_push (e->dest);
  2011. }
  2012. if (worklist.length () > 0)
  2013. {
  2014. FOR_EACH_VEC_ELT (worklist, i, bprime)
  2015. {
  2016. unsigned int i;
  2017. bitmap_iterator bi;
  2018. FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi)
  2019. bitmap_value_insert_into_set (PA_OUT,
  2020. expression_for_id (i));
  2021. if (!gimple_seq_empty_p (phi_nodes (bprime)))
  2022. {
  2023. bitmap_set_t pa_in = bitmap_set_new ();
  2024. phi_translate_set (pa_in, PA_IN (bprime), block, bprime);
  2025. FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
  2026. bitmap_value_insert_into_set (PA_OUT,
  2027. expression_for_id (i));
  2028. bitmap_set_free (pa_in);
  2029. }
  2030. else
  2031. FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi)
  2032. bitmap_value_insert_into_set (PA_OUT,
  2033. expression_for_id (i));
  2034. }
  2035. }
  2036. }
  2037. /* Prune expressions that are clobbered in block and thus become
  2038. invalid if translated from PA_OUT to PA_IN. */
  2039. prune_clobbered_mems (PA_OUT, block);
  2040. /* PA_IN starts with PA_OUT - TMP_GEN.
  2041. Then we subtract things from ANTIC_IN. */
  2042. PA_IN (block) = bitmap_set_subtract (PA_OUT, TMP_GEN (block));
  2043. /* For partial antic, we want to put back in the phi results, since
  2044. we will properly avoid making them partially antic over backedges. */
  2045. bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
  2046. bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
  2047. /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
  2048. bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
  2049. dependent_clean (PA_IN (block), ANTIC_IN (block));
  2050. if (!bitmap_set_equal (old_PA_IN, PA_IN (block)))
  2051. {
  2052. changed = true;
  2053. bitmap_set_bit (changed_blocks, block->index);
  2054. FOR_EACH_EDGE (e, ei, block->preds)
  2055. bitmap_set_bit (changed_blocks, e->src->index);
  2056. }
  2057. else
  2058. bitmap_clear_bit (changed_blocks, block->index);
  2059. maybe_dump_sets:
  2060. if (dump_file && (dump_flags & TDF_DETAILS))
  2061. {
  2062. if (PA_OUT)
  2063. print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
  2064. print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
  2065. }
  2066. if (old_PA_IN)
  2067. bitmap_set_free (old_PA_IN);
  2068. if (PA_OUT)
  2069. bitmap_set_free (PA_OUT);
  2070. return changed;
  2071. }
  2072. /* Compute ANTIC and partial ANTIC sets. */
  2073. static void
  2074. compute_antic (void)
  2075. {
  2076. bool changed = true;
  2077. int num_iterations = 0;
  2078. basic_block block;
  2079. int i;
  2080. /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
  2081. We pre-build the map of blocks with incoming abnormal edges here. */
  2082. has_abnormal_preds = sbitmap_alloc (last_basic_block_for_fn (cfun));
  2083. bitmap_clear (has_abnormal_preds);
  2084. FOR_ALL_BB_FN (block, cfun)
  2085. {
  2086. edge_iterator ei;
  2087. edge e;
  2088. FOR_EACH_EDGE (e, ei, block->preds)
  2089. {
  2090. e->flags &= ~EDGE_DFS_BACK;
  2091. if (e->flags & EDGE_ABNORMAL)
  2092. {
  2093. bitmap_set_bit (has_abnormal_preds, block->index);
  2094. break;
  2095. }
  2096. }
  2097. BB_VISITED (block) = 0;
  2098. /* While we are here, give empty ANTIC_IN sets to each block. */
  2099. ANTIC_IN (block) = bitmap_set_new ();
  2100. PA_IN (block) = bitmap_set_new ();
  2101. }
  2102. /* At the exit block we anticipate nothing. */
  2103. BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
  2104. changed_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun) + 1);
  2105. bitmap_ones (changed_blocks);
  2106. while (changed)
  2107. {
  2108. if (dump_file && (dump_flags & TDF_DETAILS))
  2109. fprintf (dump_file, "Starting iteration %d\n", num_iterations);
  2110. /* ??? We need to clear our PHI translation cache here as the
  2111. ANTIC sets shrink and we restrict valid translations to
  2112. those having operands with leaders in ANTIC. Same below
  2113. for PA ANTIC computation. */
  2114. num_iterations++;
  2115. changed = false;
  2116. for (i = postorder_num - 1; i >= 0; i--)
  2117. {
  2118. if (bitmap_bit_p (changed_blocks, postorder[i]))
  2119. {
  2120. basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
  2121. changed |= compute_antic_aux (block,
  2122. bitmap_bit_p (has_abnormal_preds,
  2123. block->index));
  2124. }
  2125. }
  2126. /* Theoretically possible, but *highly* unlikely. */
  2127. gcc_checking_assert (num_iterations < 500);
  2128. }
  2129. statistics_histogram_event (cfun, "compute_antic iterations",
  2130. num_iterations);
  2131. if (do_partial_partial)
  2132. {
  2133. bitmap_ones (changed_blocks);
  2134. mark_dfs_back_edges ();
  2135. num_iterations = 0;
  2136. changed = true;
  2137. while (changed)
  2138. {
  2139. if (dump_file && (dump_flags & TDF_DETAILS))
  2140. fprintf (dump_file, "Starting iteration %d\n", num_iterations);
  2141. num_iterations++;
  2142. changed = false;
  2143. for (i = postorder_num - 1 ; i >= 0; i--)
  2144. {
  2145. if (bitmap_bit_p (changed_blocks, postorder[i]))
  2146. {
  2147. basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
  2148. changed
  2149. |= compute_partial_antic_aux (block,
  2150. bitmap_bit_p (has_abnormal_preds,
  2151. block->index));
  2152. }
  2153. }
  2154. /* Theoretically possible, but *highly* unlikely. */
  2155. gcc_checking_assert (num_iterations < 500);
  2156. }
  2157. statistics_histogram_event (cfun, "compute_partial_antic iterations",
  2158. num_iterations);
  2159. }
  2160. sbitmap_free (has_abnormal_preds);
  2161. sbitmap_free (changed_blocks);
  2162. }
  2163. /* Inserted expressions are placed onto this worklist, which is used
  2164. for performing quick dead code elimination of insertions we made
  2165. that didn't turn out to be necessary. */
  2166. static bitmap inserted_exprs;
  2167. /* The actual worker for create_component_ref_by_pieces. */
  2168. static tree
  2169. create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
  2170. unsigned int *operand, gimple_seq *stmts)
  2171. {
  2172. vn_reference_op_t currop = &ref->operands[*operand];
  2173. tree genop;
  2174. ++*operand;
  2175. switch (currop->opcode)
  2176. {
  2177. case CALL_EXPR:
  2178. {
  2179. tree folded, sc = NULL_TREE;
  2180. unsigned int nargs = 0;
  2181. tree fn, *args;
  2182. if (TREE_CODE (currop->op0) == FUNCTION_DECL)
  2183. fn = currop->op0;
  2184. else
  2185. fn = find_or_generate_expression (block, currop->op0, stmts);
  2186. if (!fn)
  2187. return NULL_TREE;
  2188. if (currop->op1)
  2189. {
  2190. sc = find_or_generate_expression (block, currop->op1, stmts);
  2191. if (!sc)
  2192. return NULL_TREE;
  2193. }
  2194. args = XNEWVEC (tree, ref->operands.length () - 1);
  2195. while (*operand < ref->operands.length ())
  2196. {
  2197. args[nargs] = create_component_ref_by_pieces_1 (block, ref,
  2198. operand, stmts);
  2199. if (!args[nargs])
  2200. return NULL_TREE;
  2201. nargs++;
  2202. }
  2203. folded = build_call_array (currop->type,
  2204. (TREE_CODE (fn) == FUNCTION_DECL
  2205. ? build_fold_addr_expr (fn) : fn),
  2206. nargs, args);
  2207. if (currop->with_bounds)
  2208. CALL_WITH_BOUNDS_P (folded) = true;
  2209. free (args);
  2210. if (sc)
  2211. CALL_EXPR_STATIC_CHAIN (folded) = sc;
  2212. return folded;
  2213. }
  2214. case MEM_REF:
  2215. {
  2216. tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
  2217. stmts);
  2218. if (!baseop)
  2219. return NULL_TREE;
  2220. tree offset = currop->op0;
  2221. if (TREE_CODE (baseop) == ADDR_EXPR
  2222. && handled_component_p (TREE_OPERAND (baseop, 0)))
  2223. {
  2224. HOST_WIDE_INT off;
  2225. tree base;
  2226. base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
  2227. &off);
  2228. gcc_assert (base);
  2229. offset = int_const_binop (PLUS_EXPR, offset,
  2230. build_int_cst (TREE_TYPE (offset),
  2231. off));
  2232. baseop = build_fold_addr_expr (base);
  2233. }
  2234. return fold_build2 (MEM_REF, currop->type, baseop, offset);
  2235. }
  2236. case TARGET_MEM_REF:
  2237. {
  2238. tree genop0 = NULL_TREE, genop1 = NULL_TREE;
  2239. vn_reference_op_t nextop = &ref->operands[++*operand];
  2240. tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
  2241. stmts);
  2242. if (!baseop)
  2243. return NULL_TREE;
  2244. if (currop->op0)
  2245. {
  2246. genop0 = find_or_generate_expression (block, currop->op0, stmts);
  2247. if (!genop0)
  2248. return NULL_TREE;
  2249. }
  2250. if (nextop->op0)
  2251. {
  2252. genop1 = find_or_generate_expression (block, nextop->op0, stmts);
  2253. if (!genop1)
  2254. return NULL_TREE;
  2255. }
  2256. return build5 (TARGET_MEM_REF, currop->type,
  2257. baseop, currop->op2, genop0, currop->op1, genop1);
  2258. }
  2259. case ADDR_EXPR:
  2260. if (currop->op0)
  2261. {
  2262. gcc_assert (is_gimple_min_invariant (currop->op0));
  2263. return currop->op0;
  2264. }
  2265. /* Fallthrough. */
  2266. case REALPART_EXPR:
  2267. case IMAGPART_EXPR:
  2268. case VIEW_CONVERT_EXPR:
  2269. {
  2270. tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
  2271. stmts);
  2272. if (!genop0)
  2273. return NULL_TREE;
  2274. return fold_build1 (currop->opcode, currop->type, genop0);
  2275. }
  2276. case WITH_SIZE_EXPR:
  2277. {
  2278. tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
  2279. stmts);
  2280. if (!genop0)
  2281. return NULL_TREE;
  2282. tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
  2283. if (!genop1)
  2284. return NULL_TREE;
  2285. return fold_build2 (currop->opcode, currop->type, genop0, genop1);
  2286. }
  2287. case BIT_FIELD_REF:
  2288. {
  2289. tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
  2290. stmts);
  2291. if (!genop0)
  2292. return NULL_TREE;
  2293. tree op1 = currop->op0;
  2294. tree op2 = currop->op1;
  2295. return fold_build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
  2296. }
  2297. /* For array ref vn_reference_op's, operand 1 of the array ref
  2298. is op0 of the reference op and operand 3 of the array ref is
  2299. op1. */
  2300. case ARRAY_RANGE_REF:
  2301. case ARRAY_REF:
  2302. {
  2303. tree genop0;
  2304. tree genop1 = currop->op0;
  2305. tree genop2 = currop->op1;
  2306. tree genop3 = currop->op2;
  2307. genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
  2308. stmts);
  2309. if (!genop0)
  2310. return NULL_TREE;
  2311. genop1 = find_or_generate_expression (block, genop1, stmts);
  2312. if (!genop1)
  2313. return NULL_TREE;
  2314. if (genop2)
  2315. {
  2316. tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
  2317. /* Drop zero minimum index if redundant. */
  2318. if (integer_zerop (genop2)
  2319. && (!domain_type
  2320. || integer_zerop (TYPE_MIN_VALUE (domain_type))))
  2321. genop2 = NULL_TREE;
  2322. else
  2323. {
  2324. genop2 = find_or_generate_expression (block, genop2, stmts);
  2325. if (!genop2)
  2326. return NULL_TREE;
  2327. }
  2328. }
  2329. if (genop3)
  2330. {
  2331. tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
  2332. /* We can't always put a size in units of the element alignment
  2333. here as the element alignment may be not visible. See
  2334. PR43783. Simply drop the element size for constant
  2335. sizes. */
  2336. if (tree_int_cst_equal (genop3, TYPE_SIZE_UNIT (elmt_type)))
  2337. genop3 = NULL_TREE;
  2338. else
  2339. {
  2340. genop3 = size_binop (EXACT_DIV_EXPR, genop3,
  2341. size_int (TYPE_ALIGN_UNIT (elmt_type)));
  2342. genop3 = find_or_generate_expression (block, genop3, stmts);
  2343. if (!genop3)
  2344. return NULL_TREE;
  2345. }
  2346. }
  2347. return build4 (currop->opcode, currop->type, genop0, genop1,
  2348. genop2, genop3);
  2349. }
  2350. case COMPONENT_REF:
  2351. {
  2352. tree op0;
  2353. tree op1;
  2354. tree genop2 = currop->op1;
  2355. op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
  2356. if (!op0)
  2357. return NULL_TREE;
  2358. /* op1 should be a FIELD_DECL, which are represented by themselves. */
  2359. op1 = currop->op0;
  2360. if (genop2)
  2361. {
  2362. genop2 = find_or_generate_expression (block, genop2, stmts);
  2363. if (!genop2)
  2364. return NULL_TREE;
  2365. }
  2366. return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
  2367. }
  2368. case SSA_NAME:
  2369. {
  2370. genop = find_or_generate_expression (block, currop->op0, stmts);
  2371. return genop;
  2372. }
  2373. case STRING_CST:
  2374. case INTEGER_CST:
  2375. case COMPLEX_CST:
  2376. case VECTOR_CST:
  2377. case REAL_CST:
  2378. case CONSTRUCTOR:
  2379. case VAR_DECL:
  2380. case PARM_DECL:
  2381. case CONST_DECL:
  2382. case RESULT_DECL:
  2383. case FUNCTION_DECL:
  2384. return currop->op0;
  2385. default:
  2386. gcc_unreachable ();
  2387. }
  2388. }
  2389. /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
  2390. COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
  2391. trying to rename aggregates into ssa form directly, which is a no no.
  2392. Thus, this routine doesn't create temporaries, it just builds a
  2393. single access expression for the array, calling
  2394. find_or_generate_expression to build the innermost pieces.
  2395. This function is a subroutine of create_expression_by_pieces, and
  2396. should not be called on it's own unless you really know what you
  2397. are doing. */
  2398. static tree
  2399. create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
  2400. gimple_seq *stmts)
  2401. {
  2402. unsigned int op = 0;
  2403. return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
  2404. }
  2405. /* Find a simple leader for an expression, or generate one using
  2406. create_expression_by_pieces from a NARY expression for the value.
  2407. BLOCK is the basic_block we are looking for leaders in.
  2408. OP is the tree expression to find a leader for or generate.
  2409. Returns the leader or NULL_TREE on failure. */
  2410. static tree
  2411. find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
  2412. {
  2413. pre_expr expr = get_or_alloc_expr_for (op);
  2414. unsigned int lookfor = get_expr_value_id (expr);
  2415. pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
  2416. if (leader)
  2417. {
  2418. if (leader->kind == NAME)
  2419. return PRE_EXPR_NAME (leader);
  2420. else if (leader->kind == CONSTANT)
  2421. return PRE_EXPR_CONSTANT (leader);
  2422. /* Defer. */
  2423. return NULL_TREE;
  2424. }
  2425. /* It must be a complex expression, so generate it recursively. Note
  2426. that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
  2427. where the insert algorithm fails to insert a required expression. */
  2428. bitmap exprset = value_expressions[lookfor];
  2429. bitmap_iterator bi;
  2430. unsigned int i;
  2431. EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
  2432. {
  2433. pre_expr temp = expression_for_id (i);
  2434. /* We cannot insert random REFERENCE expressions at arbitrary
  2435. places. We can insert NARYs which eventually re-materializes
  2436. its operand values. */
  2437. if (temp->kind == NARY)
  2438. return create_expression_by_pieces (block, temp, stmts,
  2439. get_expr_type (expr));
  2440. }
  2441. /* Defer. */
  2442. return NULL_TREE;
  2443. }
  2444. #define NECESSARY GF_PLF_1
  2445. /* Create an expression in pieces, so that we can handle very complex
  2446. expressions that may be ANTIC, but not necessary GIMPLE.
  2447. BLOCK is the basic block the expression will be inserted into,
  2448. EXPR is the expression to insert (in value form)
  2449. STMTS is a statement list to append the necessary insertions into.
  2450. This function will die if we hit some value that shouldn't be
  2451. ANTIC but is (IE there is no leader for it, or its components).
  2452. The function returns NULL_TREE in case a different antic expression
  2453. has to be inserted first.
  2454. This function may also generate expressions that are themselves
  2455. partially or fully redundant. Those that are will be either made
  2456. fully redundant during the next iteration of insert (for partially
  2457. redundant ones), or eliminated by eliminate (for fully redundant
  2458. ones). */
  2459. static tree
  2460. create_expression_by_pieces (basic_block block, pre_expr expr,
  2461. gimple_seq *stmts, tree type)
  2462. {
  2463. tree name;
  2464. tree folded;
  2465. gimple_seq forced_stmts = NULL;
  2466. unsigned int value_id;
  2467. gimple_stmt_iterator gsi;
  2468. tree exprtype = type ? type : get_expr_type (expr);
  2469. pre_expr nameexpr;
  2470. gassign *newstmt;
  2471. switch (expr->kind)
  2472. {
  2473. /* We may hit the NAME/CONSTANT case if we have to convert types
  2474. that value numbering saw through. */
  2475. case NAME:
  2476. folded = PRE_EXPR_NAME (expr);
  2477. break;
  2478. case CONSTANT:
  2479. folded = PRE_EXPR_CONSTANT (expr);
  2480. break;
  2481. case REFERENCE:
  2482. {
  2483. vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
  2484. folded = create_component_ref_by_pieces (block, ref, stmts);
  2485. if (!folded)
  2486. return NULL_TREE;
  2487. }
  2488. break;
  2489. case NARY:
  2490. {
  2491. vn_nary_op_t nary = PRE_EXPR_NARY (expr);
  2492. tree *genop = XALLOCAVEC (tree, nary->length);
  2493. unsigned i;
  2494. for (i = 0; i < nary->length; ++i)
  2495. {
  2496. genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
  2497. if (!genop[i])
  2498. return NULL_TREE;
  2499. /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
  2500. may have conversions stripped. */
  2501. if (nary->opcode == POINTER_PLUS_EXPR)
  2502. {
  2503. if (i == 0)
  2504. genop[i] = gimple_convert (&forced_stmts,
  2505. nary->type, genop[i]);
  2506. else if (i == 1)
  2507. genop[i] = gimple_convert (&forced_stmts,
  2508. sizetype, genop[i]);
  2509. }
  2510. else
  2511. genop[i] = gimple_convert (&forced_stmts,
  2512. TREE_TYPE (nary->op[i]), genop[i]);
  2513. }
  2514. if (nary->opcode == CONSTRUCTOR)
  2515. {
  2516. vec<constructor_elt, va_gc> *elts = NULL;
  2517. for (i = 0; i < nary->length; ++i)
  2518. CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
  2519. folded = build_constructor (nary->type, elts);
  2520. }
  2521. else
  2522. {
  2523. switch (nary->length)
  2524. {
  2525. case 1:
  2526. folded = fold_build1 (nary->opcode, nary->type,
  2527. genop[0]);
  2528. break;
  2529. case 2:
  2530. folded = fold_build2 (nary->opcode, nary->type,
  2531. genop[0], genop[1]);
  2532. break;
  2533. case 3:
  2534. folded = fold_build3 (nary->opcode, nary->type,
  2535. genop[0], genop[1], genop[2]);
  2536. break;
  2537. default:
  2538. gcc_unreachable ();
  2539. }
  2540. }
  2541. }
  2542. break;
  2543. default:
  2544. gcc_unreachable ();
  2545. }
  2546. if (!useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
  2547. folded = fold_convert (exprtype, folded);
  2548. /* Force the generated expression to be a sequence of GIMPLE
  2549. statements.
  2550. We have to call unshare_expr because force_gimple_operand may
  2551. modify the tree we pass to it. */
  2552. gimple_seq tem = NULL;
  2553. folded = force_gimple_operand (unshare_expr (folded), &tem,
  2554. false, NULL);
  2555. gimple_seq_add_seq_without_update (&forced_stmts, tem);
  2556. /* If we have any intermediate expressions to the value sets, add them
  2557. to the value sets and chain them in the instruction stream. */
  2558. if (forced_stmts)
  2559. {
  2560. gsi = gsi_start (forced_stmts);
  2561. for (; !gsi_end_p (gsi); gsi_next (&gsi))
  2562. {
  2563. gimple stmt = gsi_stmt (gsi);
  2564. tree forcedname = gimple_get_lhs (stmt);
  2565. pre_expr nameexpr;
  2566. if (TREE_CODE (forcedname) == SSA_NAME)
  2567. {
  2568. bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
  2569. VN_INFO_GET (forcedname)->valnum = forcedname;
  2570. VN_INFO (forcedname)->value_id = get_next_value_id ();
  2571. nameexpr = get_or_alloc_expr_for_name (forcedname);
  2572. add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
  2573. bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
  2574. bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
  2575. }
  2576. gimple_set_vuse (stmt, BB_LIVE_VOP_ON_EXIT (block));
  2577. gimple_set_modified (stmt, true);
  2578. }
  2579. gimple_seq_add_seq (stmts, forced_stmts);
  2580. }
  2581. name = make_temp_ssa_name (exprtype, NULL, "pretmp");
  2582. newstmt = gimple_build_assign (name, folded);
  2583. gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
  2584. gimple_set_modified (newstmt, true);
  2585. gimple_set_plf (newstmt, NECESSARY, false);
  2586. gimple_seq_add_stmt (stmts, newstmt);
  2587. bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (name));
  2588. /* Fold the last statement. */
  2589. gsi = gsi_last (*stmts);
  2590. if (fold_stmt_inplace (&gsi))
  2591. update_stmt (gsi_stmt (gsi));
  2592. /* Add a value number to the temporary.
  2593. The value may already exist in either NEW_SETS, or AVAIL_OUT, because
  2594. we are creating the expression by pieces, and this particular piece of
  2595. the expression may have been represented. There is no harm in replacing
  2596. here. */
  2597. value_id = get_expr_value_id (expr);
  2598. VN_INFO_GET (name)->value_id = value_id;
  2599. VN_INFO (name)->valnum = sccvn_valnum_from_value_id (value_id);
  2600. if (VN_INFO (name)->valnum == NULL_TREE)
  2601. VN_INFO (name)->valnum = name;
  2602. gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
  2603. nameexpr = get_or_alloc_expr_for_name (name);
  2604. add_to_value (value_id, nameexpr);
  2605. if (NEW_SETS (block))
  2606. bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
  2607. bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
  2608. pre_stats.insertions++;
  2609. if (dump_file && (dump_flags & TDF_DETAILS))
  2610. {
  2611. fprintf (dump_file, "Inserted ");
  2612. print_gimple_stmt (dump_file, newstmt, 0, 0);
  2613. fprintf (dump_file, " in predecessor %d (%04d)\n",
  2614. block->index, value_id);
  2615. }
  2616. return name;
  2617. }
  2618. /* Insert the to-be-made-available values of expression EXPRNUM for each
  2619. predecessor, stored in AVAIL, into the predecessors of BLOCK, and
  2620. merge the result with a phi node, given the same value number as
  2621. NODE. Return true if we have inserted new stuff. */
  2622. static bool
  2623. insert_into_preds_of_block (basic_block block, unsigned int exprnum,
  2624. vec<pre_expr> avail)
  2625. {
  2626. pre_expr expr = expression_for_id (exprnum);
  2627. pre_expr newphi;
  2628. unsigned int val = get_expr_value_id (expr);
  2629. edge pred;
  2630. bool insertions = false;
  2631. bool nophi = false;
  2632. basic_block bprime;
  2633. pre_expr eprime;
  2634. edge_iterator ei;
  2635. tree type = get_expr_type (expr);
  2636. tree temp;
  2637. gphi *phi;
  2638. /* Make sure we aren't creating an induction variable. */
  2639. if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
  2640. {
  2641. bool firstinsideloop = false;
  2642. bool secondinsideloop = false;
  2643. firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
  2644. EDGE_PRED (block, 0)->src);
  2645. secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
  2646. EDGE_PRED (block, 1)->src);
  2647. /* Induction variables only have one edge inside the loop. */
  2648. if ((firstinsideloop ^ secondinsideloop)
  2649. && expr->kind != REFERENCE)
  2650. {
  2651. if (dump_file && (dump_flags & TDF_DETAILS))
  2652. fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
  2653. nophi = true;
  2654. }
  2655. }
  2656. /* Make the necessary insertions. */
  2657. FOR_EACH_EDGE (pred, ei, block->preds)
  2658. {
  2659. gimple_seq stmts = NULL;
  2660. tree builtexpr;
  2661. bprime = pred->src;
  2662. eprime = avail[pred->dest_idx];
  2663. if (eprime->kind != NAME && eprime->kind != CONSTANT)
  2664. {
  2665. builtexpr = create_expression_by_pieces (bprime, eprime,
  2666. &stmts, type);
  2667. gcc_assert (!(pred->flags & EDGE_ABNORMAL));
  2668. gsi_insert_seq_on_edge (pred, stmts);
  2669. if (!builtexpr)
  2670. {
  2671. /* We cannot insert a PHI node if we failed to insert
  2672. on one edge. */
  2673. nophi = true;
  2674. continue;
  2675. }
  2676. avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
  2677. insertions = true;
  2678. }
  2679. else if (eprime->kind == CONSTANT)
  2680. {
  2681. /* Constants may not have the right type, fold_convert
  2682. should give us back a constant with the right type. */
  2683. tree constant = PRE_EXPR_CONSTANT (eprime);
  2684. if (!useless_type_conversion_p (type, TREE_TYPE (constant)))
  2685. {
  2686. tree builtexpr = fold_convert (type, constant);
  2687. if (!is_gimple_min_invariant (builtexpr))
  2688. {
  2689. tree forcedexpr = force_gimple_operand (builtexpr,
  2690. &stmts, true,
  2691. NULL);
  2692. if (!is_gimple_min_invariant (forcedexpr))
  2693. {
  2694. if (forcedexpr != builtexpr)
  2695. {
  2696. VN_INFO_GET (forcedexpr)->valnum = PRE_EXPR_CONSTANT (eprime);
  2697. VN_INFO (forcedexpr)->value_id = get_expr_value_id (eprime);
  2698. }
  2699. if (stmts)
  2700. {
  2701. gimple_stmt_iterator gsi;
  2702. gsi = gsi_start (stmts);
  2703. for (; !gsi_end_p (gsi); gsi_next (&gsi))
  2704. {
  2705. gimple stmt = gsi_stmt (gsi);
  2706. tree lhs = gimple_get_lhs (stmt);
  2707. if (TREE_CODE (lhs) == SSA_NAME)
  2708. bitmap_set_bit (inserted_exprs,
  2709. SSA_NAME_VERSION (lhs));
  2710. gimple_set_plf (stmt, NECESSARY, false);
  2711. }
  2712. gsi_insert_seq_on_edge (pred, stmts);
  2713. }
  2714. avail[pred->dest_idx]
  2715. = get_or_alloc_expr_for_name (forcedexpr);
  2716. }
  2717. }
  2718. else
  2719. avail[pred->dest_idx]
  2720. = get_or_alloc_expr_for_constant (builtexpr);
  2721. }
  2722. }
  2723. else if (eprime->kind == NAME)
  2724. {
  2725. /* We may have to do a conversion because our value
  2726. numbering can look through types in certain cases, but
  2727. our IL requires all operands of a phi node have the same
  2728. type. */
  2729. tree name = PRE_EXPR_NAME (eprime);
  2730. if (!useless_type_conversion_p (type, TREE_TYPE (name)))
  2731. {
  2732. tree builtexpr;
  2733. tree forcedexpr;
  2734. builtexpr = fold_convert (type, name);
  2735. forcedexpr = force_gimple_operand (builtexpr,
  2736. &stmts, true,
  2737. NULL);
  2738. if (forcedexpr != name)
  2739. {
  2740. VN_INFO_GET (forcedexpr)->valnum = VN_INFO (name)->valnum;
  2741. VN_INFO (forcedexpr)->value_id = VN_INFO (name)->value_id;
  2742. }
  2743. if (stmts)
  2744. {
  2745. gimple_stmt_iterator gsi;
  2746. gsi = gsi_start (stmts);
  2747. for (; !gsi_end_p (gsi); gsi_next (&gsi))
  2748. {
  2749. gimple stmt = gsi_stmt (gsi);
  2750. tree lhs = gimple_get_lhs (stmt);
  2751. if (TREE_CODE (lhs) == SSA_NAME)
  2752. bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
  2753. gimple_set_plf (stmt, NECESSARY, false);
  2754. }
  2755. gsi_insert_seq_on_edge (pred, stmts);
  2756. }
  2757. avail[pred->dest_idx] = get_or_alloc_expr_for_name (forcedexpr);
  2758. }
  2759. }
  2760. }
  2761. /* If we didn't want a phi node, and we made insertions, we still have
  2762. inserted new stuff, and thus return true. If we didn't want a phi node,
  2763. and didn't make insertions, we haven't added anything new, so return
  2764. false. */
  2765. if (nophi && insertions)
  2766. return true;
  2767. else if (nophi && !insertions)
  2768. return false;
  2769. /* Now build a phi for the new variable. */
  2770. temp = make_temp_ssa_name (type, NULL, "prephitmp");
  2771. phi = create_phi_node (temp, block);
  2772. gimple_set_plf (phi, NECESSARY, false);
  2773. VN_INFO_GET (temp)->value_id = val;
  2774. VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
  2775. if (VN_INFO (temp)->valnum == NULL_TREE)
  2776. VN_INFO (temp)->valnum = temp;
  2777. bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
  2778. FOR_EACH_EDGE (pred, ei, block->preds)
  2779. {
  2780. pre_expr ae = avail[pred->dest_idx];
  2781. gcc_assert (get_expr_type (ae) == type
  2782. || useless_type_conversion_p (type, get_expr_type (ae)));
  2783. if (ae->kind == CONSTANT)
  2784. add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
  2785. pred, UNKNOWN_LOCATION);
  2786. else
  2787. add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
  2788. }
  2789. newphi = get_or_alloc_expr_for_name (temp);
  2790. add_to_value (val, newphi);
  2791. /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
  2792. this insertion, since we test for the existence of this value in PHI_GEN
  2793. before proceeding with the partial redundancy checks in insert_aux.
  2794. The value may exist in AVAIL_OUT, in particular, it could be represented
  2795. by the expression we are trying to eliminate, in which case we want the
  2796. replacement to occur. If it's not existing in AVAIL_OUT, we want it
  2797. inserted there.
  2798. Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
  2799. this block, because if it did, it would have existed in our dominator's
  2800. AVAIL_OUT, and would have been skipped due to the full redundancy check.
  2801. */
  2802. bitmap_insert_into_set (PHI_GEN (block), newphi);
  2803. bitmap_value_replace_in_set (AVAIL_OUT (block),
  2804. newphi);
  2805. bitmap_insert_into_set (NEW_SETS (block),
  2806. newphi);
  2807. /* If we insert a PHI node for a conversion of another PHI node
  2808. in the same basic-block try to preserve range information.
  2809. This is important so that followup loop passes receive optimal
  2810. number of iteration analysis results. See PR61743. */
  2811. if (expr->kind == NARY
  2812. && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
  2813. && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
  2814. && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
  2815. && INTEGRAL_TYPE_P (type)
  2816. && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
  2817. && (TYPE_PRECISION (type)
  2818. >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
  2819. && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
  2820. {
  2821. wide_int min, max;
  2822. if (get_range_info (expr->u.nary->op[0], &min, &max) == VR_RANGE
  2823. && !wi::neg_p (min, SIGNED)
  2824. && !wi::neg_p (max, SIGNED))
  2825. /* Just handle extension and sign-changes of all-positive ranges. */
  2826. set_range_info (temp,
  2827. SSA_NAME_RANGE_TYPE (expr->u.nary->op[0]),
  2828. wide_int_storage::from (min, TYPE_PRECISION (type),
  2829. TYPE_SIGN (type)),
  2830. wide_int_storage::from (max, TYPE_PRECISION (type),
  2831. TYPE_SIGN (type)));
  2832. }
  2833. if (dump_file && (dump_flags & TDF_DETAILS))
  2834. {
  2835. fprintf (dump_file, "Created phi ");
  2836. print_gimple_stmt (dump_file, phi, 0, 0);
  2837. fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
  2838. }
  2839. pre_stats.phis++;
  2840. return true;
  2841. }
  2842. /* Perform insertion of partially redundant values.
  2843. For BLOCK, do the following:
  2844. 1. Propagate the NEW_SETS of the dominator into the current block.
  2845. If the block has multiple predecessors,
  2846. 2a. Iterate over the ANTIC expressions for the block to see if
  2847. any of them are partially redundant.
  2848. 2b. If so, insert them into the necessary predecessors to make
  2849. the expression fully redundant.
  2850. 2c. Insert a new PHI merging the values of the predecessors.
  2851. 2d. Insert the new PHI, and the new expressions, into the
  2852. NEW_SETS set.
  2853. 3. Recursively call ourselves on the dominator children of BLOCK.
  2854. Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
  2855. do_regular_insertion and do_partial_insertion.
  2856. */
  2857. static bool
  2858. do_regular_insertion (basic_block block, basic_block dom)
  2859. {
  2860. bool new_stuff = false;
  2861. vec<pre_expr> exprs;
  2862. pre_expr expr;
  2863. auto_vec<pre_expr> avail;
  2864. int i;
  2865. exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
  2866. avail.safe_grow (EDGE_COUNT (block->preds));
  2867. FOR_EACH_VEC_ELT (exprs, i, expr)
  2868. {
  2869. if (expr->kind == NARY
  2870. || expr->kind == REFERENCE)
  2871. {
  2872. unsigned int val;
  2873. bool by_some = false;
  2874. bool cant_insert = false;
  2875. bool all_same = true;
  2876. pre_expr first_s = NULL;
  2877. edge pred;
  2878. basic_block bprime;
  2879. pre_expr eprime = NULL;
  2880. edge_iterator ei;
  2881. pre_expr edoubleprime = NULL;
  2882. bool do_insertion = false;
  2883. val = get_expr_value_id (expr);
  2884. if (bitmap_set_contains_value (PHI_GEN (block), val))
  2885. continue;
  2886. if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
  2887. {
  2888. if (dump_file && (dump_flags & TDF_DETAILS))
  2889. {
  2890. fprintf (dump_file, "Found fully redundant value: ");
  2891. print_pre_expr (dump_file, expr);
  2892. fprintf (dump_file, "\n");
  2893. }
  2894. continue;
  2895. }
  2896. FOR_EACH_EDGE (pred, ei, block->preds)
  2897. {
  2898. unsigned int vprime;
  2899. /* We should never run insertion for the exit block
  2900. and so not come across fake pred edges. */
  2901. gcc_assert (!(pred->flags & EDGE_FAKE));
  2902. bprime = pred->src;
  2903. eprime = phi_translate (expr, ANTIC_IN (block), NULL,
  2904. bprime, block);
  2905. /* eprime will generally only be NULL if the
  2906. value of the expression, translated
  2907. through the PHI for this predecessor, is
  2908. undefined. If that is the case, we can't
  2909. make the expression fully redundant,
  2910. because its value is undefined along a
  2911. predecessor path. We can thus break out
  2912. early because it doesn't matter what the
  2913. rest of the results are. */
  2914. if (eprime == NULL)
  2915. {
  2916. avail[pred->dest_idx] = NULL;
  2917. cant_insert = true;
  2918. break;
  2919. }
  2920. eprime = fully_constant_expression (eprime);
  2921. vprime = get_expr_value_id (eprime);
  2922. edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
  2923. vprime);
  2924. if (edoubleprime == NULL)
  2925. {
  2926. avail[pred->dest_idx] = eprime;
  2927. all_same = false;
  2928. }
  2929. else
  2930. {
  2931. avail[pred->dest_idx] = edoubleprime;
  2932. by_some = true;
  2933. /* We want to perform insertions to remove a redundancy on
  2934. a path in the CFG we want to optimize for speed. */
  2935. if (optimize_edge_for_speed_p (pred))
  2936. do_insertion = true;
  2937. if (first_s == NULL)
  2938. first_s = edoubleprime;
  2939. else if (!pre_expr_d::equal (first_s, edoubleprime))
  2940. all_same = false;
  2941. }
  2942. }
  2943. /* If we can insert it, it's not the same value
  2944. already existing along every predecessor, and
  2945. it's defined by some predecessor, it is
  2946. partially redundant. */
  2947. if (!cant_insert && !all_same && by_some)
  2948. {
  2949. if (!do_insertion)
  2950. {
  2951. if (dump_file && (dump_flags & TDF_DETAILS))
  2952. {
  2953. fprintf (dump_file, "Skipping partial redundancy for "
  2954. "expression ");
  2955. print_pre_expr (dump_file, expr);
  2956. fprintf (dump_file, " (%04d), no redundancy on to be "
  2957. "optimized for speed edge\n", val);
  2958. }
  2959. }
  2960. else if (dbg_cnt (treepre_insert))
  2961. {
  2962. if (dump_file && (dump_flags & TDF_DETAILS))
  2963. {
  2964. fprintf (dump_file, "Found partial redundancy for "
  2965. "expression ");
  2966. print_pre_expr (dump_file, expr);
  2967. fprintf (dump_file, " (%04d)\n",
  2968. get_expr_value_id (expr));
  2969. }
  2970. if (insert_into_preds_of_block (block,
  2971. get_expression_id (expr),
  2972. avail))
  2973. new_stuff = true;
  2974. }
  2975. }
  2976. /* If all edges produce the same value and that value is
  2977. an invariant, then the PHI has the same value on all
  2978. edges. Note this. */
  2979. else if (!cant_insert && all_same)
  2980. {
  2981. gcc_assert (edoubleprime->kind == CONSTANT
  2982. || edoubleprime->kind == NAME);
  2983. tree temp = make_temp_ssa_name (get_expr_type (expr),
  2984. NULL, "pretmp");
  2985. gassign *assign
  2986. = gimple_build_assign (temp,
  2987. edoubleprime->kind == CONSTANT ?
  2988. PRE_EXPR_CONSTANT (edoubleprime) :
  2989. PRE_EXPR_NAME (edoubleprime));
  2990. gimple_stmt_iterator gsi = gsi_after_labels (block);
  2991. gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
  2992. gimple_set_plf (assign, NECESSARY, false);
  2993. VN_INFO_GET (temp)->value_id = val;
  2994. VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
  2995. if (VN_INFO (temp)->valnum == NULL_TREE)
  2996. VN_INFO (temp)->valnum = temp;
  2997. bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
  2998. pre_expr newe = get_or_alloc_expr_for_name (temp);
  2999. add_to_value (val, newe);
  3000. bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
  3001. bitmap_insert_into_set (NEW_SETS (block), newe);
  3002. }
  3003. }
  3004. }
  3005. exprs.release ();
  3006. return new_stuff;
  3007. }
  3008. /* Perform insertion for partially anticipatable expressions. There
  3009. is only one case we will perform insertion for these. This case is
  3010. if the expression is partially anticipatable, and fully available.
  3011. In this case, we know that putting it earlier will enable us to
  3012. remove the later computation. */
  3013. static bool
  3014. do_partial_partial_insertion (basic_block block, basic_block dom)
  3015. {
  3016. bool new_stuff = false;
  3017. vec<pre_expr> exprs;
  3018. pre_expr expr;
  3019. auto_vec<pre_expr> avail;
  3020. int i;
  3021. exprs = sorted_array_from_bitmap_set (PA_IN (block));
  3022. avail.safe_grow (EDGE_COUNT (block->preds));
  3023. FOR_EACH_VEC_ELT (exprs, i, expr)
  3024. {
  3025. if (expr->kind == NARY
  3026. || expr->kind == REFERENCE)
  3027. {
  3028. unsigned int val;
  3029. bool by_all = true;
  3030. bool cant_insert = false;
  3031. edge pred;
  3032. basic_block bprime;
  3033. pre_expr eprime = NULL;
  3034. edge_iterator ei;
  3035. val = get_expr_value_id (expr);
  3036. if (bitmap_set_contains_value (PHI_GEN (block), val))
  3037. continue;
  3038. if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
  3039. continue;
  3040. FOR_EACH_EDGE (pred, ei, block->preds)
  3041. {
  3042. unsigned int vprime;
  3043. pre_expr edoubleprime;
  3044. /* We should never run insertion for the exit block
  3045. and so not come across fake pred edges. */
  3046. gcc_assert (!(pred->flags & EDGE_FAKE));
  3047. bprime = pred->src;
  3048. eprime = phi_translate (expr, ANTIC_IN (block),
  3049. PA_IN (block),
  3050. bprime, block);
  3051. /* eprime will generally only be NULL if the
  3052. value of the expression, translated
  3053. through the PHI for this predecessor, is
  3054. undefined. If that is the case, we can't
  3055. make the expression fully redundant,
  3056. because its value is undefined along a
  3057. predecessor path. We can thus break out
  3058. early because it doesn't matter what the
  3059. rest of the results are. */
  3060. if (eprime == NULL)
  3061. {
  3062. avail[pred->dest_idx] = NULL;
  3063. cant_insert = true;
  3064. break;
  3065. }
  3066. eprime = fully_constant_expression (eprime);
  3067. vprime = get_expr_value_id (eprime);
  3068. edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
  3069. avail[pred->dest_idx] = edoubleprime;
  3070. if (edoubleprime == NULL)
  3071. {
  3072. by_all = false;
  3073. break;
  3074. }
  3075. }
  3076. /* If we can insert it, it's not the same value
  3077. already existing along every predecessor, and
  3078. it's defined by some predecessor, it is
  3079. partially redundant. */
  3080. if (!cant_insert && by_all)
  3081. {
  3082. edge succ;
  3083. bool do_insertion = false;
  3084. /* Insert only if we can remove a later expression on a path
  3085. that we want to optimize for speed.
  3086. The phi node that we will be inserting in BLOCK is not free,
  3087. and inserting it for the sake of !optimize_for_speed successor
  3088. may cause regressions on the speed path. */
  3089. FOR_EACH_EDGE (succ, ei, block->succs)
  3090. {
  3091. if (bitmap_set_contains_value (PA_IN (succ->dest), val)
  3092. || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
  3093. {
  3094. if (optimize_edge_for_speed_p (succ))
  3095. do_insertion = true;
  3096. }
  3097. }
  3098. if (!do_insertion)
  3099. {
  3100. if (dump_file && (dump_flags & TDF_DETAILS))
  3101. {
  3102. fprintf (dump_file, "Skipping partial partial redundancy "
  3103. "for expression ");
  3104. print_pre_expr (dump_file, expr);
  3105. fprintf (dump_file, " (%04d), not (partially) anticipated "
  3106. "on any to be optimized for speed edges\n", val);
  3107. }
  3108. }
  3109. else if (dbg_cnt (treepre_insert))
  3110. {
  3111. pre_stats.pa_insert++;
  3112. if (dump_file && (dump_flags & TDF_DETAILS))
  3113. {
  3114. fprintf (dump_file, "Found partial partial redundancy "
  3115. "for expression ");
  3116. print_pre_expr (dump_file, expr);
  3117. fprintf (dump_file, " (%04d)\n",
  3118. get_expr_value_id (expr));
  3119. }
  3120. if (insert_into_preds_of_block (block,
  3121. get_expression_id (expr),
  3122. avail))
  3123. new_stuff = true;
  3124. }
  3125. }
  3126. }
  3127. }
  3128. exprs.release ();
  3129. return new_stuff;
  3130. }
  3131. static bool
  3132. insert_aux (basic_block block)
  3133. {
  3134. basic_block son;
  3135. bool new_stuff = false;
  3136. if (block)
  3137. {
  3138. basic_block dom;
  3139. dom = get_immediate_dominator (CDI_DOMINATORS, block);
  3140. if (dom)
  3141. {
  3142. unsigned i;
  3143. bitmap_iterator bi;
  3144. bitmap_set_t newset = NEW_SETS (dom);
  3145. if (newset)
  3146. {
  3147. /* Note that we need to value_replace both NEW_SETS, and
  3148. AVAIL_OUT. For both the case of NEW_SETS, the value may be
  3149. represented by some non-simple expression here that we want
  3150. to replace it with. */
  3151. FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
  3152. {
  3153. pre_expr expr = expression_for_id (i);
  3154. bitmap_value_replace_in_set (NEW_SETS (block), expr);
  3155. bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
  3156. }
  3157. }
  3158. if (!single_pred_p (block))
  3159. {
  3160. new_stuff |= do_regular_insertion (block, dom);
  3161. if (do_partial_partial)
  3162. new_stuff |= do_partial_partial_insertion (block, dom);
  3163. }
  3164. }
  3165. }
  3166. for (son = first_dom_son (CDI_DOMINATORS, block);
  3167. son;
  3168. son = next_dom_son (CDI_DOMINATORS, son))
  3169. {
  3170. new_stuff |= insert_aux (son);
  3171. }
  3172. return new_stuff;
  3173. }
  3174. /* Perform insertion of partially redundant values. */
  3175. static void
  3176. insert (void)
  3177. {
  3178. bool new_stuff = true;
  3179. basic_block bb;
  3180. int num_iterations = 0;
  3181. FOR_ALL_BB_FN (bb, cfun)
  3182. NEW_SETS (bb) = bitmap_set_new ();
  3183. while (new_stuff)
  3184. {
  3185. num_iterations++;
  3186. if (dump_file && dump_flags & TDF_DETAILS)
  3187. fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
  3188. new_stuff = insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun));
  3189. /* Clear the NEW sets before the next iteration. We have already
  3190. fully propagated its contents. */
  3191. if (new_stuff)
  3192. FOR_ALL_BB_FN (bb, cfun)
  3193. bitmap_set_free (NEW_SETS (bb));
  3194. }
  3195. statistics_histogram_event (cfun, "insert iterations", num_iterations);
  3196. }
  3197. /* Compute the AVAIL set for all basic blocks.
  3198. This function performs value numbering of the statements in each basic
  3199. block. The AVAIL sets are built from information we glean while doing
  3200. this value numbering, since the AVAIL sets contain only one entry per
  3201. value.
  3202. AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
  3203. AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
  3204. static void
  3205. compute_avail (void)
  3206. {
  3207. basic_block block, son;
  3208. basic_block *worklist;
  3209. size_t sp = 0;
  3210. unsigned i;
  3211. /* We pretend that default definitions are defined in the entry block.
  3212. This includes function arguments and the static chain decl. */
  3213. for (i = 1; i < num_ssa_names; ++i)
  3214. {
  3215. tree name = ssa_name (i);
  3216. pre_expr e;
  3217. if (!name
  3218. || !SSA_NAME_IS_DEFAULT_DEF (name)
  3219. || has_zero_uses (name)
  3220. || virtual_operand_p (name))
  3221. continue;
  3222. e = get_or_alloc_expr_for_name (name);
  3223. add_to_value (get_expr_value_id (e), e);
  3224. bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)), e);
  3225. bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
  3226. e);
  3227. }
  3228. if (dump_file && (dump_flags & TDF_DETAILS))
  3229. {
  3230. print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
  3231. "tmp_gen", ENTRY_BLOCK);
  3232. print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
  3233. "avail_out", ENTRY_BLOCK);
  3234. }
  3235. /* Allocate the worklist. */
  3236. worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
  3237. /* Seed the algorithm by putting the dominator children of the entry
  3238. block on the worklist. */
  3239. for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun));
  3240. son;
  3241. son = next_dom_son (CDI_DOMINATORS, son))
  3242. worklist[sp++] = son;
  3243. BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun))
  3244. = ssa_default_def (cfun, gimple_vop (cfun));
  3245. /* Loop until the worklist is empty. */
  3246. while (sp)
  3247. {
  3248. gimple stmt;
  3249. basic_block dom;
  3250. /* Pick a block from the worklist. */
  3251. block = worklist[--sp];
  3252. /* Initially, the set of available values in BLOCK is that of
  3253. its immediate dominator. */
  3254. dom = get_immediate_dominator (CDI_DOMINATORS, block);
  3255. if (dom)
  3256. {
  3257. bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
  3258. BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
  3259. }
  3260. /* Generate values for PHI nodes. */
  3261. for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
  3262. gsi_next (&gsi))
  3263. {
  3264. tree result = gimple_phi_result (gsi.phi ());
  3265. /* We have no need for virtual phis, as they don't represent
  3266. actual computations. */
  3267. if (virtual_operand_p (result))
  3268. {
  3269. BB_LIVE_VOP_ON_EXIT (block) = result;
  3270. continue;
  3271. }
  3272. pre_expr e = get_or_alloc_expr_for_name (result);
  3273. add_to_value (get_expr_value_id (e), e);
  3274. bitmap_value_insert_into_set (AVAIL_OUT (block), e);
  3275. bitmap_insert_into_set (PHI_GEN (block), e);
  3276. }
  3277. BB_MAY_NOTRETURN (block) = 0;
  3278. /* Now compute value numbers and populate value sets with all
  3279. the expressions computed in BLOCK. */
  3280. for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
  3281. gsi_next (&gsi))
  3282. {
  3283. ssa_op_iter iter;
  3284. tree op;
  3285. stmt = gsi_stmt (gsi);
  3286. /* Cache whether the basic-block has any non-visible side-effect
  3287. or control flow.
  3288. If this isn't a call or it is the last stmt in the
  3289. basic-block then the CFG represents things correctly. */
  3290. if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
  3291. {
  3292. /* Non-looping const functions always return normally.
  3293. Otherwise the call might not return or have side-effects
  3294. that forbids hoisting possibly trapping expressions
  3295. before it. */
  3296. int flags = gimple_call_flags (stmt);
  3297. if (!(flags & ECF_CONST)
  3298. || (flags & ECF_LOOPING_CONST_OR_PURE))
  3299. BB_MAY_NOTRETURN (block) = 1;
  3300. }
  3301. FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
  3302. {
  3303. pre_expr e = get_or_alloc_expr_for_name (op);
  3304. add_to_value (get_expr_value_id (e), e);
  3305. bitmap_insert_into_set (TMP_GEN (block), e);
  3306. bitmap_value_insert_into_set (AVAIL_OUT (block), e);
  3307. }
  3308. if (gimple_vdef (stmt))
  3309. BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
  3310. if (gimple_has_side_effects (stmt)
  3311. || stmt_could_throw_p (stmt)
  3312. || is_gimple_debug (stmt))
  3313. continue;
  3314. FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
  3315. {
  3316. if (ssa_undefined_value_p (op))
  3317. continue;
  3318. pre_expr e = get_or_alloc_expr_for_name (op);
  3319. bitmap_value_insert_into_set (EXP_GEN (block), e);
  3320. }
  3321. switch (gimple_code (stmt))
  3322. {
  3323. case GIMPLE_RETURN:
  3324. continue;
  3325. case GIMPLE_CALL:
  3326. {
  3327. vn_reference_t ref;
  3328. vn_reference_s ref1;
  3329. pre_expr result = NULL;
  3330. /* We can value number only calls to real functions. */
  3331. if (gimple_call_internal_p (stmt))
  3332. continue;
  3333. vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
  3334. if (!ref)
  3335. continue;
  3336. /* If the value of the call is not invalidated in
  3337. this block until it is computed, add the expression
  3338. to EXP_GEN. */
  3339. if (!gimple_vuse (stmt)
  3340. || gimple_code
  3341. (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
  3342. || gimple_bb (SSA_NAME_DEF_STMT
  3343. (gimple_vuse (stmt))) != block)
  3344. {
  3345. result = (pre_expr) pool_alloc (pre_expr_pool);
  3346. result->kind = REFERENCE;
  3347. result->id = 0;
  3348. PRE_EXPR_REFERENCE (result) = ref;
  3349. get_or_alloc_expression_id (result);
  3350. add_to_value (get_expr_value_id (result), result);
  3351. bitmap_value_insert_into_set (EXP_GEN (block), result);
  3352. }
  3353. continue;
  3354. }
  3355. case GIMPLE_ASSIGN:
  3356. {
  3357. pre_expr result = NULL;
  3358. switch (vn_get_stmt_kind (stmt))
  3359. {
  3360. case VN_NARY:
  3361. {
  3362. enum tree_code code = gimple_assign_rhs_code (stmt);
  3363. vn_nary_op_t nary;
  3364. /* COND_EXPR and VEC_COND_EXPR are awkward in
  3365. that they contain an embedded complex expression.
  3366. Don't even try to shove those through PRE. */
  3367. if (code == COND_EXPR
  3368. || code == VEC_COND_EXPR)
  3369. continue;
  3370. vn_nary_op_lookup_stmt (stmt, &nary);
  3371. if (!nary)
  3372. continue;
  3373. /* If the NARY traps and there was a preceding
  3374. point in the block that might not return avoid
  3375. adding the nary to EXP_GEN. */
  3376. if (BB_MAY_NOTRETURN (block)
  3377. && vn_nary_may_trap (nary))
  3378. continue;
  3379. result = (pre_expr) pool_alloc (pre_expr_pool);
  3380. result->kind = NARY;
  3381. result->id = 0;
  3382. PRE_EXPR_NARY (result) = nary;
  3383. break;
  3384. }
  3385. case VN_REFERENCE:
  3386. {
  3387. vn_reference_t ref;
  3388. vn_reference_lookup (gimple_assign_rhs1 (stmt),
  3389. gimple_vuse (stmt),
  3390. VN_WALK, &ref);
  3391. if (!ref)
  3392. continue;
  3393. /* If the value of the reference is not invalidated in
  3394. this block until it is computed, add the expression
  3395. to EXP_GEN. */
  3396. if (gimple_vuse (stmt))
  3397. {
  3398. gimple def_stmt;
  3399. bool ok = true;
  3400. def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
  3401. while (!gimple_nop_p (def_stmt)
  3402. && gimple_code (def_stmt) != GIMPLE_PHI
  3403. && gimple_bb (def_stmt) == block)
  3404. {
  3405. if (stmt_may_clobber_ref_p
  3406. (def_stmt, gimple_assign_rhs1 (stmt)))
  3407. {
  3408. ok = false;
  3409. break;
  3410. }
  3411. def_stmt
  3412. = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
  3413. }
  3414. if (!ok)
  3415. continue;
  3416. }
  3417. result = (pre_expr) pool_alloc (pre_expr_pool);
  3418. result->kind = REFERENCE;
  3419. result->id = 0;
  3420. PRE_EXPR_REFERENCE (result) = ref;
  3421. break;
  3422. }
  3423. default:
  3424. continue;
  3425. }
  3426. get_or_alloc_expression_id (result);
  3427. add_to_value (get_expr_value_id (result), result);
  3428. bitmap_value_insert_into_set (EXP_GEN (block), result);
  3429. continue;
  3430. }
  3431. default:
  3432. break;
  3433. }
  3434. }
  3435. if (dump_file && (dump_flags & TDF_DETAILS))
  3436. {
  3437. print_bitmap_set (dump_file, EXP_GEN (block),
  3438. "exp_gen", block->index);
  3439. print_bitmap_set (dump_file, PHI_GEN (block),
  3440. "phi_gen", block->index);
  3441. print_bitmap_set (dump_file, TMP_GEN (block),
  3442. "tmp_gen", block->index);
  3443. print_bitmap_set (dump_file, AVAIL_OUT (block),
  3444. "avail_out", block->index);
  3445. }
  3446. /* Put the dominator children of BLOCK on the worklist of blocks
  3447. to compute available sets for. */
  3448. for (son = first_dom_son (CDI_DOMINATORS, block);
  3449. son;
  3450. son = next_dom_son (CDI_DOMINATORS, son))
  3451. worklist[sp++] = son;
  3452. }
  3453. free (worklist);
  3454. }
  3455. /* Local state for the eliminate domwalk. */
  3456. static vec<gimple> el_to_remove;
  3457. static vec<gimple> el_to_fixup;
  3458. static unsigned int el_todo;
  3459. static vec<tree> el_avail;
  3460. static vec<tree> el_avail_stack;
  3461. /* Return a leader for OP that is available at the current point of the
  3462. eliminate domwalk. */
  3463. static tree
  3464. eliminate_avail (tree op)
  3465. {
  3466. tree valnum = VN_INFO (op)->valnum;
  3467. if (TREE_CODE (valnum) == SSA_NAME)
  3468. {
  3469. if (SSA_NAME_IS_DEFAULT_DEF (valnum))
  3470. return valnum;
  3471. if (el_avail.length () > SSA_NAME_VERSION (valnum))
  3472. return el_avail[SSA_NAME_VERSION (valnum)];
  3473. }
  3474. else if (is_gimple_min_invariant (valnum))
  3475. return valnum;
  3476. return NULL_TREE;
  3477. }
  3478. /* At the current point of the eliminate domwalk make OP available. */
  3479. static void
  3480. eliminate_push_avail (tree op)
  3481. {
  3482. tree valnum = VN_INFO (op)->valnum;
  3483. if (TREE_CODE (valnum) == SSA_NAME)
  3484. {
  3485. if (el_avail.length () <= SSA_NAME_VERSION (valnum))
  3486. el_avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1);
  3487. tree pushop = op;
  3488. if (el_avail[SSA_NAME_VERSION (valnum)])
  3489. pushop = el_avail[SSA_NAME_VERSION (valnum)];
  3490. el_avail_stack.safe_push (pushop);
  3491. el_avail[SSA_NAME_VERSION (valnum)] = op;
  3492. }
  3493. }
  3494. /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
  3495. the leader for the expression if insertion was successful. */
  3496. static tree
  3497. eliminate_insert (gimple_stmt_iterator *gsi, tree val)
  3498. {
  3499. tree expr = vn_get_expr_for (val);
  3500. if (!CONVERT_EXPR_P (expr)
  3501. && TREE_CODE (expr) != VIEW_CONVERT_EXPR)
  3502. return NULL_TREE;
  3503. tree op = TREE_OPERAND (expr, 0);
  3504. tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (op) : op;
  3505. if (!leader)
  3506. return NULL_TREE;
  3507. tree res = make_temp_ssa_name (TREE_TYPE (val), NULL, "pretmp");
  3508. gassign *tem = gimple_build_assign (res,
  3509. fold_build1 (TREE_CODE (expr),
  3510. TREE_TYPE (expr), leader));
  3511. gsi_insert_before (gsi, tem, GSI_SAME_STMT);
  3512. VN_INFO_GET (res)->valnum = val;
  3513. if (TREE_CODE (leader) == SSA_NAME)
  3514. gimple_set_plf (SSA_NAME_DEF_STMT (leader), NECESSARY, true);
  3515. pre_stats.insertions++;
  3516. if (dump_file && (dump_flags & TDF_DETAILS))
  3517. {
  3518. fprintf (dump_file, "Inserted ");
  3519. print_gimple_stmt (dump_file, tem, 0, 0);
  3520. }
  3521. return res;
  3522. }
  3523. class eliminate_dom_walker : public dom_walker
  3524. {
  3525. public:
  3526. eliminate_dom_walker (cdi_direction direction, bool do_pre_)
  3527. : dom_walker (direction), do_pre (do_pre_) {}
  3528. virtual void before_dom_children (basic_block);
  3529. virtual void after_dom_children (basic_block);
  3530. bool do_pre;
  3531. };
  3532. /* Perform elimination for the basic-block B during the domwalk. */
  3533. void
  3534. eliminate_dom_walker::before_dom_children (basic_block b)
  3535. {
  3536. /* Mark new bb. */
  3537. el_avail_stack.safe_push (NULL_TREE);
  3538. /* ??? If we do nothing for unreachable blocks then this will confuse
  3539. tailmerging. Eventually we can reduce its reliance on SCCVN now
  3540. that we fully copy/constant-propagate (most) things. */
  3541. for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
  3542. {
  3543. gphi *phi = gsi.phi ();
  3544. tree res = PHI_RESULT (phi);
  3545. if (virtual_operand_p (res))
  3546. {
  3547. gsi_next (&gsi);
  3548. continue;
  3549. }
  3550. tree sprime = eliminate_avail (res);
  3551. if (sprime
  3552. && sprime != res)
  3553. {
  3554. if (dump_file && (dump_flags & TDF_DETAILS))
  3555. {
  3556. fprintf (dump_file, "Replaced redundant PHI node defining ");
  3557. print_generic_expr (dump_file, res, 0);
  3558. fprintf (dump_file, " with ");
  3559. print_generic_expr (dump_file, sprime, 0);
  3560. fprintf (dump_file, "\n");
  3561. }
  3562. /* If we inserted this PHI node ourself, it's not an elimination. */
  3563. if (inserted_exprs
  3564. && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
  3565. pre_stats.phis--;
  3566. else
  3567. pre_stats.eliminations++;
  3568. /* If we will propagate into all uses don't bother to do
  3569. anything. */
  3570. if (may_propagate_copy (res, sprime))
  3571. {
  3572. /* Mark the PHI for removal. */
  3573. el_to_remove.safe_push (phi);
  3574. gsi_next (&gsi);
  3575. continue;
  3576. }
  3577. remove_phi_node (&gsi, false);
  3578. if (inserted_exprs
  3579. && !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res))
  3580. && TREE_CODE (sprime) == SSA_NAME)
  3581. gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true);
  3582. if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
  3583. sprime = fold_convert (TREE_TYPE (res), sprime);
  3584. gimple stmt = gimple_build_assign (res, sprime);
  3585. /* ??? It cannot yet be necessary (DOM walk). */
  3586. gimple_set_plf (stmt, NECESSARY, gimple_plf (phi, NECESSARY));
  3587. gimple_stmt_iterator gsi2 = gsi_after_labels (b);
  3588. gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
  3589. continue;
  3590. }
  3591. eliminate_push_avail (res);
  3592. gsi_next (&gsi);
  3593. }
  3594. for (gimple_stmt_iterator gsi = gsi_start_bb (b);
  3595. !gsi_end_p (gsi);
  3596. gsi_next (&gsi))
  3597. {
  3598. tree sprime = NULL_TREE;
  3599. gimple stmt = gsi_stmt (gsi);
  3600. tree lhs = gimple_get_lhs (stmt);
  3601. if (lhs && TREE_CODE (lhs) == SSA_NAME
  3602. && !gimple_has_volatile_ops (stmt)
  3603. /* See PR43491. Do not replace a global register variable when
  3604. it is a the RHS of an assignment. Do replace local register
  3605. variables since gcc does not guarantee a local variable will
  3606. be allocated in register.
  3607. ??? The fix isn't effective here. This should instead
  3608. be ensured by not value-numbering them the same but treating
  3609. them like volatiles? */
  3610. && !(gimple_assign_single_p (stmt)
  3611. && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
  3612. && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
  3613. && is_global_var (gimple_assign_rhs1 (stmt)))))
  3614. {
  3615. sprime = eliminate_avail (lhs);
  3616. if (!sprime)
  3617. {
  3618. /* If there is no existing usable leader but SCCVN thinks
  3619. it has an expression it wants to use as replacement,
  3620. insert that. */
  3621. tree val = VN_INFO (lhs)->valnum;
  3622. if (val != VN_TOP
  3623. && TREE_CODE (val) == SSA_NAME
  3624. && VN_INFO (val)->needs_insertion
  3625. && VN_INFO (val)->expr != NULL_TREE
  3626. && (sprime = eliminate_insert (&gsi, val)) != NULL_TREE)
  3627. eliminate_push_avail (sprime);
  3628. }
  3629. /* If this now constitutes a copy duplicate points-to
  3630. and range info appropriately. This is especially
  3631. important for inserted code. See tree-ssa-copy.c
  3632. for similar code. */
  3633. if (sprime
  3634. && TREE_CODE (sprime) == SSA_NAME)
  3635. {
  3636. basic_block sprime_b = gimple_bb (SSA_NAME_DEF_STMT (sprime));
  3637. if (POINTER_TYPE_P (TREE_TYPE (lhs))
  3638. && SSA_NAME_PTR_INFO (lhs)
  3639. && !SSA_NAME_PTR_INFO (sprime))
  3640. {
  3641. duplicate_ssa_name_ptr_info (sprime,
  3642. SSA_NAME_PTR_INFO (lhs));
  3643. if (b != sprime_b)
  3644. mark_ptr_info_alignment_unknown
  3645. (SSA_NAME_PTR_INFO (sprime));
  3646. }
  3647. else if (!POINTER_TYPE_P (TREE_TYPE (lhs))
  3648. && SSA_NAME_RANGE_INFO (lhs)
  3649. && !SSA_NAME_RANGE_INFO (sprime)
  3650. && b == sprime_b)
  3651. duplicate_ssa_name_range_info (sprime,
  3652. SSA_NAME_RANGE_TYPE (lhs),
  3653. SSA_NAME_RANGE_INFO (lhs));
  3654. }
  3655. /* Inhibit the use of an inserted PHI on a loop header when
  3656. the address of the memory reference is a simple induction
  3657. variable. In other cases the vectorizer won't do anything
  3658. anyway (either it's loop invariant or a complicated
  3659. expression). */
  3660. if (sprime
  3661. && TREE_CODE (sprime) == SSA_NAME
  3662. && do_pre
  3663. && flag_tree_loop_vectorize
  3664. && loop_outer (b->loop_father)
  3665. && has_zero_uses (sprime)
  3666. && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
  3667. && gimple_assign_load_p (stmt))
  3668. {
  3669. gimple def_stmt = SSA_NAME_DEF_STMT (sprime);
  3670. basic_block def_bb = gimple_bb (def_stmt);
  3671. if (gimple_code (def_stmt) == GIMPLE_PHI
  3672. && b->loop_father->header == def_bb)
  3673. {
  3674. ssa_op_iter iter;
  3675. tree op;
  3676. bool found = false;
  3677. FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
  3678. {
  3679. affine_iv iv;
  3680. def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
  3681. if (def_bb
  3682. && flow_bb_inside_loop_p (b->loop_father, def_bb)
  3683. && simple_iv (b->loop_father,
  3684. b->loop_father, op, &iv, true))
  3685. {
  3686. found = true;
  3687. break;
  3688. }
  3689. }
  3690. if (found)
  3691. {
  3692. if (dump_file && (dump_flags & TDF_DETAILS))
  3693. {
  3694. fprintf (dump_file, "Not replacing ");
  3695. print_gimple_expr (dump_file, stmt, 0, 0);
  3696. fprintf (dump_file, " with ");
  3697. print_generic_expr (dump_file, sprime, 0);
  3698. fprintf (dump_file, " which would add a loop"
  3699. " carried dependence to loop %d\n",
  3700. b->loop_father->num);
  3701. }
  3702. /* Don't keep sprime available. */
  3703. sprime = NULL_TREE;
  3704. }
  3705. }
  3706. }
  3707. if (sprime)
  3708. {
  3709. /* If we can propagate the value computed for LHS into
  3710. all uses don't bother doing anything with this stmt. */
  3711. if (may_propagate_copy (lhs, sprime))
  3712. {
  3713. /* Mark it for removal. */
  3714. el_to_remove.safe_push (stmt);
  3715. /* ??? Don't count copy/constant propagations. */
  3716. if (gimple_assign_single_p (stmt)
  3717. && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
  3718. || gimple_assign_rhs1 (stmt) == sprime))
  3719. continue;
  3720. if (dump_file && (dump_flags & TDF_DETAILS))
  3721. {
  3722. fprintf (dump_file, "Replaced ");
  3723. print_gimple_expr (dump_file, stmt, 0, 0);
  3724. fprintf (dump_file, " with ");
  3725. print_generic_expr (dump_file, sprime, 0);
  3726. fprintf (dump_file, " in all uses of ");
  3727. print_gimple_stmt (dump_file, stmt, 0, 0);
  3728. }
  3729. pre_stats.eliminations++;
  3730. continue;
  3731. }
  3732. /* If this is an assignment from our leader (which
  3733. happens in the case the value-number is a constant)
  3734. then there is nothing to do. */
  3735. if (gimple_assign_single_p (stmt)
  3736. && sprime == gimple_assign_rhs1 (stmt))
  3737. continue;
  3738. /* Else replace its RHS. */
  3739. bool can_make_abnormal_goto
  3740. = is_gimple_call (stmt)
  3741. && stmt_can_make_abnormal_goto (stmt);
  3742. if (dump_file && (dump_flags & TDF_DETAILS))
  3743. {
  3744. fprintf (dump_file, "Replaced ");
  3745. print_gimple_expr (dump_file, stmt, 0, 0);
  3746. fprintf (dump_file, " with ");
  3747. print_generic_expr (dump_file, sprime, 0);
  3748. fprintf (dump_file, " in ");
  3749. print_gimple_stmt (dump_file, stmt, 0, 0);
  3750. }
  3751. if (TREE_CODE (sprime) == SSA_NAME)
  3752. gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
  3753. NECESSARY, true);
  3754. pre_stats.eliminations++;
  3755. gimple orig_stmt = stmt;
  3756. if (!useless_type_conversion_p (TREE_TYPE (lhs),
  3757. TREE_TYPE (sprime)))
  3758. sprime = fold_convert (TREE_TYPE (lhs), sprime);
  3759. tree vdef = gimple_vdef (stmt);
  3760. tree vuse = gimple_vuse (stmt);
  3761. propagate_tree_value_into_stmt (&gsi, sprime);
  3762. stmt = gsi_stmt (gsi);
  3763. update_stmt (stmt);
  3764. if (vdef != gimple_vdef (stmt))
  3765. VN_INFO (vdef)->valnum = vuse;
  3766. /* If we removed EH side-effects from the statement, clean
  3767. its EH information. */
  3768. if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
  3769. {
  3770. bitmap_set_bit (need_eh_cleanup,
  3771. gimple_bb (stmt)->index);
  3772. if (dump_file && (dump_flags & TDF_DETAILS))
  3773. fprintf (dump_file, " Removed EH side-effects.\n");
  3774. }
  3775. /* Likewise for AB side-effects. */
  3776. if (can_make_abnormal_goto
  3777. && !stmt_can_make_abnormal_goto (stmt))
  3778. {
  3779. bitmap_set_bit (need_ab_cleanup,
  3780. gimple_bb (stmt)->index);
  3781. if (dump_file && (dump_flags & TDF_DETAILS))
  3782. fprintf (dump_file, " Removed AB side-effects.\n");
  3783. }
  3784. continue;
  3785. }
  3786. }
  3787. /* If the statement is a scalar store, see if the expression
  3788. has the same value number as its rhs. If so, the store is
  3789. dead. */
  3790. if (gimple_assign_single_p (stmt)
  3791. && !gimple_has_volatile_ops (stmt)
  3792. && !is_gimple_reg (gimple_assign_lhs (stmt))
  3793. && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
  3794. || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
  3795. {
  3796. tree val;
  3797. tree rhs = gimple_assign_rhs1 (stmt);
  3798. val = vn_reference_lookup (gimple_assign_lhs (stmt),
  3799. gimple_vuse (stmt), VN_WALK, NULL);
  3800. if (TREE_CODE (rhs) == SSA_NAME)
  3801. rhs = VN_INFO (rhs)->valnum;
  3802. if (val
  3803. && operand_equal_p (val, rhs, 0))
  3804. {
  3805. if (dump_file && (dump_flags & TDF_DETAILS))
  3806. {
  3807. fprintf (dump_file, "Deleted redundant store ");
  3808. print_gimple_stmt (dump_file, stmt, 0, 0);
  3809. }
  3810. /* Queue stmt for removal. */
  3811. el_to_remove.safe_push (stmt);
  3812. continue;
  3813. }
  3814. }
  3815. bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
  3816. bool was_noreturn = (is_gimple_call (stmt)
  3817. && gimple_call_noreturn_p (stmt));
  3818. tree vdef = gimple_vdef (stmt);
  3819. tree vuse = gimple_vuse (stmt);
  3820. /* If we didn't replace the whole stmt (or propagate the result
  3821. into all uses), replace all uses on this stmt with their
  3822. leaders. */
  3823. use_operand_p use_p;
  3824. ssa_op_iter iter;
  3825. FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
  3826. {
  3827. tree use = USE_FROM_PTR (use_p);
  3828. /* ??? The call code above leaves stmt operands un-updated. */
  3829. if (TREE_CODE (use) != SSA_NAME)
  3830. continue;
  3831. tree sprime = eliminate_avail (use);
  3832. if (sprime && sprime != use
  3833. && may_propagate_copy (use, sprime)
  3834. /* We substitute into debug stmts to avoid excessive
  3835. debug temporaries created by removed stmts, but we need
  3836. to avoid doing so for inserted sprimes as we never want
  3837. to create debug temporaries for them. */
  3838. && (!inserted_exprs
  3839. || TREE_CODE (sprime) != SSA_NAME
  3840. || !is_gimple_debug (stmt)
  3841. || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
  3842. {
  3843. propagate_value (use_p, sprime);
  3844. gimple_set_modified (stmt, true);
  3845. if (TREE_CODE (sprime) == SSA_NAME
  3846. && !is_gimple_debug (stmt))
  3847. gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
  3848. NECESSARY, true);
  3849. }
  3850. }
  3851. /* Visit indirect calls and turn them into direct calls if
  3852. possible using the devirtualization machinery. */
  3853. if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
  3854. {
  3855. tree fn = gimple_call_fn (call_stmt);
  3856. if (fn
  3857. && flag_devirtualize
  3858. && virtual_method_call_p (fn))
  3859. {
  3860. tree otr_type = obj_type_ref_class (fn);
  3861. tree instance;
  3862. ipa_polymorphic_call_context context (current_function_decl, fn, stmt, &instance);
  3863. bool final;
  3864. context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn), otr_type, stmt);
  3865. vec <cgraph_node *>targets
  3866. = possible_polymorphic_call_targets (obj_type_ref_class (fn),
  3867. tree_to_uhwi
  3868. (OBJ_TYPE_REF_TOKEN (fn)),
  3869. context,
  3870. &final);
  3871. if (dump_file)
  3872. dump_possible_polymorphic_call_targets (dump_file,
  3873. obj_type_ref_class (fn),
  3874. tree_to_uhwi
  3875. (OBJ_TYPE_REF_TOKEN (fn)),
  3876. context);
  3877. if (final && targets.length () <= 1 && dbg_cnt (devirt))
  3878. {
  3879. tree fn;
  3880. if (targets.length () == 1)
  3881. fn = targets[0]->decl;
  3882. else
  3883. fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
  3884. if (dump_enabled_p ())
  3885. {
  3886. location_t loc = gimple_location_safe (stmt);
  3887. dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
  3888. "converting indirect call to "
  3889. "function %s\n",
  3890. cgraph_node::get (fn)->name ());
  3891. }
  3892. gimple_call_set_fndecl (call_stmt, fn);
  3893. maybe_remove_unused_call_args (cfun, call_stmt);
  3894. gimple_set_modified (stmt, true);
  3895. }
  3896. }
  3897. }
  3898. if (gimple_modified_p (stmt))
  3899. {
  3900. /* If a formerly non-invariant ADDR_EXPR is turned into an
  3901. invariant one it was on a separate stmt. */
  3902. if (gimple_assign_single_p (stmt)
  3903. && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
  3904. recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
  3905. gimple old_stmt = stmt;
  3906. if (is_gimple_call (stmt))
  3907. {
  3908. /* ??? Only fold calls inplace for now, this may create new
  3909. SSA names which in turn will confuse free_scc_vn SSA name
  3910. release code. */
  3911. fold_stmt_inplace (&gsi);
  3912. /* When changing a call into a noreturn call, cfg cleanup
  3913. is needed to fix up the noreturn call. */
  3914. if (!was_noreturn && gimple_call_noreturn_p (stmt))
  3915. el_to_fixup.safe_push (stmt);
  3916. }
  3917. else
  3918. {
  3919. fold_stmt (&gsi);
  3920. stmt = gsi_stmt (gsi);
  3921. if ((gimple_code (stmt) == GIMPLE_COND
  3922. && (gimple_cond_true_p (as_a <gcond *> (stmt))
  3923. || gimple_cond_false_p (as_a <gcond *> (stmt))))
  3924. || (gimple_code (stmt) == GIMPLE_SWITCH
  3925. && TREE_CODE (gimple_switch_index (
  3926. as_a <gswitch *> (stmt)))
  3927. == INTEGER_CST))
  3928. el_todo |= TODO_cleanup_cfg;
  3929. }
  3930. /* If we removed EH side-effects from the statement, clean
  3931. its EH information. */
  3932. if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
  3933. {
  3934. bitmap_set_bit (need_eh_cleanup,
  3935. gimple_bb (stmt)->index);
  3936. if (dump_file && (dump_flags & TDF_DETAILS))
  3937. fprintf (dump_file, " Removed EH side-effects.\n");
  3938. }
  3939. /* Likewise for AB side-effects. */
  3940. if (can_make_abnormal_goto
  3941. && !stmt_can_make_abnormal_goto (stmt))
  3942. {
  3943. bitmap_set_bit (need_ab_cleanup,
  3944. gimple_bb (stmt)->index);
  3945. if (dump_file && (dump_flags & TDF_DETAILS))
  3946. fprintf (dump_file, " Removed AB side-effects.\n");
  3947. }
  3948. update_stmt (stmt);
  3949. if (vdef != gimple_vdef (stmt))
  3950. VN_INFO (vdef)->valnum = vuse;
  3951. }
  3952. /* Make new values available - for fully redundant LHS we
  3953. continue with the next stmt above and skip this. */
  3954. def_operand_p defp;
  3955. FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
  3956. eliminate_push_avail (DEF_FROM_PTR (defp));
  3957. }
  3958. /* Replace destination PHI arguments. */
  3959. edge_iterator ei;
  3960. edge e;
  3961. FOR_EACH_EDGE (e, ei, b->succs)
  3962. {
  3963. for (gphi_iterator gsi = gsi_start_phis (e->dest);
  3964. !gsi_end_p (gsi);
  3965. gsi_next (&gsi))
  3966. {
  3967. gphi *phi = gsi.phi ();
  3968. use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
  3969. tree arg = USE_FROM_PTR (use_p);
  3970. if (TREE_CODE (arg) != SSA_NAME
  3971. || virtual_operand_p (arg))
  3972. continue;
  3973. tree sprime = eliminate_avail (arg);
  3974. if (sprime && may_propagate_copy (arg, sprime))
  3975. {
  3976. propagate_value (use_p, sprime);
  3977. if (TREE_CODE (sprime) == SSA_NAME)
  3978. gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true);
  3979. }
  3980. }
  3981. }
  3982. }
  3983. /* Make no longer available leaders no longer available. */
  3984. void
  3985. eliminate_dom_walker::after_dom_children (basic_block)
  3986. {
  3987. tree entry;
  3988. while ((entry = el_avail_stack.pop ()) != NULL_TREE)
  3989. {
  3990. tree valnum = VN_INFO (entry)->valnum;
  3991. tree old = el_avail[SSA_NAME_VERSION (valnum)];
  3992. if (old == entry)
  3993. el_avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
  3994. else
  3995. el_avail[SSA_NAME_VERSION (valnum)] = entry;
  3996. }
  3997. }
  3998. /* Eliminate fully redundant computations. */
  3999. static unsigned int
  4000. eliminate (bool do_pre)
  4001. {
  4002. gimple_stmt_iterator gsi;
  4003. gimple stmt;
  4004. need_eh_cleanup = BITMAP_ALLOC (NULL);
  4005. need_ab_cleanup = BITMAP_ALLOC (NULL);
  4006. el_to_remove.create (0);
  4007. el_to_fixup.create (0);
  4008. el_todo = 0;
  4009. el_avail.create (num_ssa_names);
  4010. el_avail_stack.create (0);
  4011. eliminate_dom_walker (CDI_DOMINATORS,
  4012. do_pre).walk (cfun->cfg->x_entry_block_ptr);
  4013. el_avail.release ();
  4014. el_avail_stack.release ();
  4015. /* We cannot remove stmts during BB walk, especially not release SSA
  4016. names there as this confuses the VN machinery. The stmts ending
  4017. up in el_to_remove are either stores or simple copies.
  4018. Remove stmts in reverse order to make debug stmt creation possible. */
  4019. while (!el_to_remove.is_empty ())
  4020. {
  4021. stmt = el_to_remove.pop ();
  4022. if (dump_file && (dump_flags & TDF_DETAILS))
  4023. {
  4024. fprintf (dump_file, "Removing dead stmt ");
  4025. print_gimple_stmt (dump_file, stmt, 0, 0);
  4026. }
  4027. tree lhs;
  4028. if (gimple_code (stmt) == GIMPLE_PHI)
  4029. lhs = gimple_phi_result (stmt);
  4030. else
  4031. lhs = gimple_get_lhs (stmt);
  4032. if (inserted_exprs
  4033. && TREE_CODE (lhs) == SSA_NAME)
  4034. bitmap_clear_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
  4035. gsi = gsi_for_stmt (stmt);
  4036. if (gimple_code (stmt) == GIMPLE_PHI)
  4037. remove_phi_node (&gsi, true);
  4038. else
  4039. {
  4040. basic_block bb = gimple_bb (stmt);
  4041. unlink_stmt_vdef (stmt);
  4042. if (gsi_remove (&gsi, true))
  4043. bitmap_set_bit (need_eh_cleanup, bb->index);
  4044. release_defs (stmt);
  4045. }
  4046. /* Removing a stmt may expose a forwarder block. */
  4047. el_todo |= TODO_cleanup_cfg;
  4048. }
  4049. el_to_remove.release ();
  4050. /* Fixup stmts that became noreturn calls. This may require splitting
  4051. blocks and thus isn't possible during the dominator walk. Do this
  4052. in reverse order so we don't inadvertedly remove a stmt we want to
  4053. fixup by visiting a dominating now noreturn call first. */
  4054. while (!el_to_fixup.is_empty ())
  4055. {
  4056. stmt = el_to_fixup.pop ();
  4057. if (dump_file && (dump_flags & TDF_DETAILS))
  4058. {
  4059. fprintf (dump_file, "Fixing up noreturn call ");
  4060. print_gimple_stmt (dump_file, stmt, 0, 0);
  4061. }
  4062. if (fixup_noreturn_call (stmt))
  4063. el_todo |= TODO_cleanup_cfg;
  4064. }
  4065. el_to_fixup.release ();
  4066. return el_todo;
  4067. }
  4068. /* Perform CFG cleanups made necessary by elimination. */
  4069. static unsigned
  4070. fini_eliminate (void)
  4071. {
  4072. bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
  4073. bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
  4074. if (do_eh_cleanup)
  4075. gimple_purge_all_dead_eh_edges (need_eh_cleanup);
  4076. if (do_ab_cleanup)
  4077. gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
  4078. BITMAP_FREE (need_eh_cleanup);
  4079. BITMAP_FREE (need_ab_cleanup);
  4080. if (do_eh_cleanup || do_ab_cleanup)
  4081. return TODO_cleanup_cfg;
  4082. return 0;
  4083. }
  4084. /* Borrow a bit of tree-ssa-dce.c for the moment.
  4085. XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
  4086. this may be a bit faster, and we may want critical edges kept split. */
  4087. /* If OP's defining statement has not already been determined to be necessary,
  4088. mark that statement necessary. Return the stmt, if it is newly
  4089. necessary. */
  4090. static inline gimple
  4091. mark_operand_necessary (tree op)
  4092. {
  4093. gimple stmt;
  4094. gcc_assert (op);
  4095. if (TREE_CODE (op) != SSA_NAME)
  4096. return NULL;
  4097. stmt = SSA_NAME_DEF_STMT (op);
  4098. gcc_assert (stmt);
  4099. if (gimple_plf (stmt, NECESSARY)
  4100. || gimple_nop_p (stmt))
  4101. return NULL;
  4102. gimple_set_plf (stmt, NECESSARY, true);
  4103. return stmt;
  4104. }
  4105. /* Because we don't follow exactly the standard PRE algorithm, and decide not
  4106. to insert PHI nodes sometimes, and because value numbering of casts isn't
  4107. perfect, we sometimes end up inserting dead code. This simple DCE-like
  4108. pass removes any insertions we made that weren't actually used. */
  4109. static void
  4110. remove_dead_inserted_code (void)
  4111. {
  4112. bitmap worklist;
  4113. unsigned i;
  4114. bitmap_iterator bi;
  4115. gimple t;
  4116. worklist = BITMAP_ALLOC (NULL);
  4117. EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
  4118. {
  4119. t = SSA_NAME_DEF_STMT (ssa_name (i));
  4120. if (gimple_plf (t, NECESSARY))
  4121. bitmap_set_bit (worklist, i);
  4122. }
  4123. while (!bitmap_empty_p (worklist))
  4124. {
  4125. i = bitmap_first_set_bit (worklist);
  4126. bitmap_clear_bit (worklist, i);
  4127. t = SSA_NAME_DEF_STMT (ssa_name (i));
  4128. /* PHI nodes are somewhat special in that each PHI alternative has
  4129. data and control dependencies. All the statements feeding the
  4130. PHI node's arguments are always necessary. */
  4131. if (gimple_code (t) == GIMPLE_PHI)
  4132. {
  4133. unsigned k;
  4134. for (k = 0; k < gimple_phi_num_args (t); k++)
  4135. {
  4136. tree arg = PHI_ARG_DEF (t, k);
  4137. if (TREE_CODE (arg) == SSA_NAME)
  4138. {
  4139. gimple n = mark_operand_necessary (arg);
  4140. if (n)
  4141. bitmap_set_bit (worklist, SSA_NAME_VERSION (arg));
  4142. }
  4143. }
  4144. }
  4145. else
  4146. {
  4147. /* Propagate through the operands. Examine all the USE, VUSE and
  4148. VDEF operands in this statement. Mark all the statements
  4149. which feed this statement's uses as necessary. */
  4150. ssa_op_iter iter;
  4151. tree use;
  4152. /* The operands of VDEF expressions are also needed as they
  4153. represent potential definitions that may reach this
  4154. statement (VDEF operands allow us to follow def-def
  4155. links). */
  4156. FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
  4157. {
  4158. gimple n = mark_operand_necessary (use);
  4159. if (n)
  4160. bitmap_set_bit (worklist, SSA_NAME_VERSION (use));
  4161. }
  4162. }
  4163. }
  4164. EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
  4165. {
  4166. t = SSA_NAME_DEF_STMT (ssa_name (i));
  4167. if (!gimple_plf (t, NECESSARY))
  4168. {
  4169. gimple_stmt_iterator gsi;
  4170. if (dump_file && (dump_flags & TDF_DETAILS))
  4171. {
  4172. fprintf (dump_file, "Removing unnecessary insertion:");
  4173. print_gimple_stmt (dump_file, t, 0, 0);
  4174. }
  4175. gsi = gsi_for_stmt (t);
  4176. if (gimple_code (t) == GIMPLE_PHI)
  4177. remove_phi_node (&gsi, true);
  4178. else
  4179. {
  4180. gsi_remove (&gsi, true);
  4181. release_defs (t);
  4182. }
  4183. }
  4184. }
  4185. BITMAP_FREE (worklist);
  4186. }
  4187. /* Initialize data structures used by PRE. */
  4188. static void
  4189. init_pre (void)
  4190. {
  4191. basic_block bb;
  4192. next_expression_id = 1;
  4193. expressions.create (0);
  4194. expressions.safe_push (NULL);
  4195. value_expressions.create (get_max_value_id () + 1);
  4196. value_expressions.safe_grow_cleared (get_max_value_id () + 1);
  4197. name_to_id.create (0);
  4198. inserted_exprs = BITMAP_ALLOC (NULL);
  4199. connect_infinite_loops_to_exit ();
  4200. memset (&pre_stats, 0, sizeof (pre_stats));
  4201. postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
  4202. postorder_num = inverted_post_order_compute (postorder);
  4203. alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
  4204. calculate_dominance_info (CDI_POST_DOMINATORS);
  4205. calculate_dominance_info (CDI_DOMINATORS);
  4206. bitmap_obstack_initialize (&grand_bitmap_obstack);
  4207. phi_translate_table = new hash_table<expr_pred_trans_d> (5110);
  4208. expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
  4209. bitmap_set_pool = create_alloc_pool ("Bitmap sets",
  4210. sizeof (struct bitmap_set), 30);
  4211. pre_expr_pool = create_alloc_pool ("pre_expr nodes",
  4212. sizeof (struct pre_expr_d), 30);
  4213. FOR_ALL_BB_FN (bb, cfun)
  4214. {
  4215. EXP_GEN (bb) = bitmap_set_new ();
  4216. PHI_GEN (bb) = bitmap_set_new ();
  4217. TMP_GEN (bb) = bitmap_set_new ();
  4218. AVAIL_OUT (bb) = bitmap_set_new ();
  4219. }
  4220. }
  4221. /* Deallocate data structures used by PRE. */
  4222. static void
  4223. fini_pre ()
  4224. {
  4225. free (postorder);
  4226. value_expressions.release ();
  4227. BITMAP_FREE (inserted_exprs);
  4228. bitmap_obstack_release (&grand_bitmap_obstack);
  4229. free_alloc_pool (bitmap_set_pool);
  4230. free_alloc_pool (pre_expr_pool);
  4231. delete phi_translate_table;
  4232. phi_translate_table = NULL;
  4233. delete expression_to_id;
  4234. expression_to_id = NULL;
  4235. name_to_id.release ();
  4236. free_aux_for_blocks ();
  4237. free_dominance_info (CDI_POST_DOMINATORS);
  4238. }
  4239. namespace {
  4240. const pass_data pass_data_pre =
  4241. {
  4242. GIMPLE_PASS, /* type */
  4243. "pre", /* name */
  4244. OPTGROUP_NONE, /* optinfo_flags */
  4245. TV_TREE_PRE, /* tv_id */
  4246. /* PROP_no_crit_edges is ensured by placing pass_split_crit_edges before
  4247. pass_pre. */
  4248. ( PROP_no_crit_edges | PROP_cfg | PROP_ssa ), /* properties_required */
  4249. 0, /* properties_provided */
  4250. PROP_no_crit_edges, /* properties_destroyed */
  4251. TODO_rebuild_alias, /* todo_flags_start */
  4252. 0, /* todo_flags_finish */
  4253. };
  4254. class pass_pre : public gimple_opt_pass
  4255. {
  4256. public:
  4257. pass_pre (gcc::context *ctxt)
  4258. : gimple_opt_pass (pass_data_pre, ctxt)
  4259. {}
  4260. /* opt_pass methods: */
  4261. virtual bool gate (function *) { return flag_tree_pre != 0; }
  4262. virtual unsigned int execute (function *);
  4263. }; // class pass_pre
  4264. unsigned int
  4265. pass_pre::execute (function *fun)
  4266. {
  4267. unsigned int todo = 0;
  4268. do_partial_partial =
  4269. flag_tree_partial_pre && optimize_function_for_speed_p (fun);
  4270. /* This has to happen before SCCVN runs because
  4271. loop_optimizer_init may create new phis, etc. */
  4272. loop_optimizer_init (LOOPS_NORMAL);
  4273. if (!run_scc_vn (VN_WALK))
  4274. {
  4275. loop_optimizer_finalize ();
  4276. return 0;
  4277. }
  4278. init_pre ();
  4279. scev_initialize ();
  4280. /* Collect and value number expressions computed in each basic block. */
  4281. compute_avail ();
  4282. /* Insert can get quite slow on an incredibly large number of basic
  4283. blocks due to some quadratic behavior. Until this behavior is
  4284. fixed, don't run it when he have an incredibly large number of
  4285. bb's. If we aren't going to run insert, there is no point in
  4286. computing ANTIC, either, even though it's plenty fast. */
  4287. if (n_basic_blocks_for_fn (fun) < 4000)
  4288. {
  4289. compute_antic ();
  4290. insert ();
  4291. }
  4292. /* Make sure to remove fake edges before committing our inserts.
  4293. This makes sure we don't end up with extra critical edges that
  4294. we would need to split. */
  4295. remove_fake_exit_edges ();
  4296. gsi_commit_edge_inserts ();
  4297. /* Eliminate folds statements which might (should not...) end up
  4298. not keeping virtual operands up-to-date. */
  4299. gcc_assert (!need_ssa_update_p (fun));
  4300. /* Remove all the redundant expressions. */
  4301. todo |= eliminate (true);
  4302. statistics_counter_event (fun, "Insertions", pre_stats.insertions);
  4303. statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
  4304. statistics_counter_event (fun, "New PHIs", pre_stats.phis);
  4305. statistics_counter_event (fun, "Eliminated", pre_stats.eliminations);
  4306. clear_expression_ids ();
  4307. remove_dead_inserted_code ();
  4308. scev_finalize ();
  4309. fini_pre ();
  4310. todo |= fini_eliminate ();
  4311. loop_optimizer_finalize ();
  4312. /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
  4313. case we can merge the block with the remaining predecessor of the block.
  4314. It should either:
  4315. - call merge_blocks after each tail merge iteration
  4316. - call merge_blocks after all tail merge iterations
  4317. - mark TODO_cleanup_cfg when necessary
  4318. - share the cfg cleanup with fini_pre. */
  4319. todo |= tail_merge_optimize (todo);
  4320. free_scc_vn ();
  4321. /* Tail merging invalidates the virtual SSA web, together with
  4322. cfg-cleanup opportunities exposed by PRE this will wreck the
  4323. SSA updating machinery. So make sure to run update-ssa
  4324. manually, before eventually scheduling cfg-cleanup as part of
  4325. the todo. */
  4326. update_ssa (TODO_update_ssa_only_virtuals);
  4327. return todo;
  4328. }
  4329. } // anon namespace
  4330. gimple_opt_pass *
  4331. make_pass_pre (gcc::context *ctxt)
  4332. {
  4333. return new pass_pre (ctxt);
  4334. }
  4335. namespace {
  4336. const pass_data pass_data_fre =
  4337. {
  4338. GIMPLE_PASS, /* type */
  4339. "fre", /* name */
  4340. OPTGROUP_NONE, /* optinfo_flags */
  4341. TV_TREE_FRE, /* tv_id */
  4342. ( PROP_cfg | PROP_ssa ), /* properties_required */
  4343. 0, /* properties_provided */
  4344. 0, /* properties_destroyed */
  4345. 0, /* todo_flags_start */
  4346. 0, /* todo_flags_finish */
  4347. };
  4348. class pass_fre : public gimple_opt_pass
  4349. {
  4350. public:
  4351. pass_fre (gcc::context *ctxt)
  4352. : gimple_opt_pass (pass_data_fre, ctxt)
  4353. {}
  4354. /* opt_pass methods: */
  4355. opt_pass * clone () { return new pass_fre (m_ctxt); }
  4356. virtual bool gate (function *) { return flag_tree_fre != 0; }
  4357. virtual unsigned int execute (function *);
  4358. }; // class pass_fre
  4359. unsigned int
  4360. pass_fre::execute (function *fun)
  4361. {
  4362. unsigned int todo = 0;
  4363. if (!run_scc_vn (VN_WALKREWRITE))
  4364. return 0;
  4365. memset (&pre_stats, 0, sizeof (pre_stats));
  4366. /* Remove all the redundant expressions. */
  4367. todo |= eliminate (false);
  4368. todo |= fini_eliminate ();
  4369. free_scc_vn ();
  4370. statistics_counter_event (fun, "Insertions", pre_stats.insertions);
  4371. statistics_counter_event (fun, "Eliminated", pre_stats.eliminations);
  4372. return todo;
  4373. }
  4374. } // anon namespace
  4375. gimple_opt_pass *
  4376. make_pass_fre (gcc::context *ctxt)
  4377. {
  4378. return new pass_fre (ctxt);
  4379. }