gimple-ssa-strength-reduction.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701
  1. /* Straight-line strength reduction.
  2. Copyright (C) 2012-2015 Free Software Foundation, Inc.
  3. Contributed by Bill Schmidt, IBM <wschmidt@linux.ibm.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* There are many algorithms for performing strength reduction on
  17. loops. This is not one of them. IVOPTS handles strength reduction
  18. of induction variables just fine. This pass is intended to pick
  19. up the crumbs it leaves behind, by considering opportunities for
  20. strength reduction along dominator paths.
  21. Strength reduction addresses explicit multiplies, and certain
  22. multiplies implicit in addressing expressions. It would also be
  23. possible to apply strength reduction to divisions and modulos,
  24. but such opportunities are relatively uncommon.
  25. Strength reduction is also currently restricted to integer operations.
  26. If desired, it could be extended to floating-point operations under
  27. control of something like -funsafe-math-optimizations. */
  28. #include "config.h"
  29. #include "system.h"
  30. #include "coretypes.h"
  31. #include "hash-set.h"
  32. #include "machmode.h"
  33. #include "vec.h"
  34. #include "double-int.h"
  35. #include "input.h"
  36. #include "alias.h"
  37. #include "symtab.h"
  38. #include "options.h"
  39. #include "wide-int.h"
  40. #include "inchash.h"
  41. #include "tree.h"
  42. #include "fold-const.h"
  43. #include "predict.h"
  44. #include "tm.h"
  45. #include "hard-reg-set.h"
  46. #include "function.h"
  47. #include "dominance.h"
  48. #include "cfg.h"
  49. #include "basic-block.h"
  50. #include "tree-ssa-alias.h"
  51. #include "internal-fn.h"
  52. #include "gimple-expr.h"
  53. #include "is-a.h"
  54. #include "gimple.h"
  55. #include "gimple-iterator.h"
  56. #include "gimplify-me.h"
  57. #include "stor-layout.h"
  58. #include "hashtab.h"
  59. #include "rtl.h"
  60. #include "flags.h"
  61. #include "statistics.h"
  62. #include "real.h"
  63. #include "fixed-value.h"
  64. #include "insn-config.h"
  65. #include "expmed.h"
  66. #include "dojump.h"
  67. #include "explow.h"
  68. #include "calls.h"
  69. #include "emit-rtl.h"
  70. #include "varasm.h"
  71. #include "stmt.h"
  72. #include "expr.h"
  73. #include "tree-pass.h"
  74. #include "cfgloop.h"
  75. #include "gimple-pretty-print.h"
  76. #include "gimple-ssa.h"
  77. #include "tree-cfg.h"
  78. #include "tree-phinodes.h"
  79. #include "ssa-iterators.h"
  80. #include "stringpool.h"
  81. #include "tree-ssanames.h"
  82. #include "domwalk.h"
  83. #include "params.h"
  84. #include "tree-ssa-address.h"
  85. #include "tree-affine.h"
  86. #include "wide-int-print.h"
  87. #include "builtins.h"
  88. /* Information about a strength reduction candidate. Each statement
  89. in the candidate table represents an expression of one of the
  90. following forms (the special case of CAND_REF will be described
  91. later):
  92. (CAND_MULT) S1: X = (B + i) * S
  93. (CAND_ADD) S1: X = B + (i * S)
  94. Here X and B are SSA names, i is an integer constant, and S is
  95. either an SSA name or a constant. We call B the "base," i the
  96. "index", and S the "stride."
  97. Any statement S0 that dominates S1 and is of the form:
  98. (CAND_MULT) S0: Y = (B + i') * S
  99. (CAND_ADD) S0: Y = B + (i' * S)
  100. is called a "basis" for S1. In both cases, S1 may be replaced by
  101. S1': X = Y + (i - i') * S,
  102. where (i - i') * S is folded to the extent possible.
  103. All gimple statements are visited in dominator order, and each
  104. statement that may contribute to one of the forms of S1 above is
  105. given at least one entry in the candidate table. Such statements
  106. include addition, pointer addition, subtraction, multiplication,
  107. negation, copies, and nontrivial type casts. If a statement may
  108. represent more than one expression of the forms of S1 above,
  109. multiple "interpretations" are stored in the table and chained
  110. together. Examples:
  111. * An add of two SSA names may treat either operand as the base.
  112. * A multiply of two SSA names, likewise.
  113. * A copy or cast may be thought of as either a CAND_MULT with
  114. i = 0 and S = 1, or as a CAND_ADD with i = 0 or S = 0.
  115. Candidate records are allocated from an obstack. They are addressed
  116. both from a hash table keyed on S1, and from a vector of candidate
  117. pointers arranged in predominator order.
  118. Opportunity note
  119. ----------------
  120. Currently we don't recognize:
  121. S0: Y = (S * i') - B
  122. S1: X = (S * i) - B
  123. as a strength reduction opportunity, even though this S1 would
  124. also be replaceable by the S1' above. This can be added if it
  125. comes up in practice.
  126. Strength reduction in addressing
  127. --------------------------------
  128. There is another kind of candidate known as CAND_REF. A CAND_REF
  129. describes a statement containing a memory reference having
  130. complex addressing that might benefit from strength reduction.
  131. Specifically, we are interested in references for which
  132. get_inner_reference returns a base address, offset, and bitpos as
  133. follows:
  134. base: MEM_REF (T1, C1)
  135. offset: MULT_EXPR (PLUS_EXPR (T2, C2), C3)
  136. bitpos: C4 * BITS_PER_UNIT
  137. Here T1 and T2 are arbitrary trees, and C1, C2, C3, C4 are
  138. arbitrary integer constants. Note that C2 may be zero, in which
  139. case the offset will be MULT_EXPR (T2, C3).
  140. When this pattern is recognized, the original memory reference
  141. can be replaced with:
  142. MEM_REF (POINTER_PLUS_EXPR (T1, MULT_EXPR (T2, C3)),
  143. C1 + (C2 * C3) + C4)
  144. which distributes the multiply to allow constant folding. When
  145. two or more addressing expressions can be represented by MEM_REFs
  146. of this form, differing only in the constants C1, C2, and C4,
  147. making this substitution produces more efficient addressing during
  148. the RTL phases. When there are not at least two expressions with
  149. the same values of T1, T2, and C3, there is nothing to be gained
  150. by the replacement.
  151. Strength reduction of CAND_REFs uses the same infrastructure as
  152. that used by CAND_MULTs and CAND_ADDs. We record T1 in the base (B)
  153. field, MULT_EXPR (T2, C3) in the stride (S) field, and
  154. C1 + (C2 * C3) + C4 in the index (i) field. A basis for a CAND_REF
  155. is thus another CAND_REF with the same B and S values. When at
  156. least two CAND_REFs are chained together using the basis relation,
  157. each of them is replaced as above, resulting in improved code
  158. generation for addressing.
  159. Conditional candidates
  160. ======================
  161. Conditional candidates are best illustrated with an example.
  162. Consider the code sequence:
  163. (1) x_0 = ...;
  164. (2) a_0 = x_0 * 5; MULT (B: x_0; i: 0; S: 5)
  165. if (...)
  166. (3) x_1 = x_0 + 1; ADD (B: x_0, i: 1; S: 1)
  167. (4) x_2 = PHI <x_0, x_1>; PHI (B: x_0, i: 0, S: 1)
  168. (5) x_3 = x_2 + 1; ADD (B: x_2, i: 1, S: 1)
  169. (6) a_1 = x_3 * 5; MULT (B: x_2, i: 1; S: 5)
  170. Here strength reduction is complicated by the uncertain value of x_2.
  171. A legitimate transformation is:
  172. (1) x_0 = ...;
  173. (2) a_0 = x_0 * 5;
  174. if (...)
  175. {
  176. (3) [x_1 = x_0 + 1;]
  177. (3a) t_1 = a_0 + 5;
  178. }
  179. (4) [x_2 = PHI <x_0, x_1>;]
  180. (4a) t_2 = PHI <a_0, t_1>;
  181. (5) [x_3 = x_2 + 1;]
  182. (6r) a_1 = t_2 + 5;
  183. where the bracketed instructions may go dead.
  184. To recognize this opportunity, we have to observe that statement (6)
  185. has a "hidden basis" (2). The hidden basis is unlike a normal basis
  186. in that the statement and the hidden basis have different base SSA
  187. names (x_2 and x_0, respectively). The relationship is established
  188. when a statement's base name (x_2) is defined by a phi statement (4),
  189. each argument of which (x_0, x_1) has an identical "derived base name."
  190. If the argument is defined by a candidate (as x_1 is by (3)) that is a
  191. CAND_ADD having a stride of 1, the derived base name of the argument is
  192. the base name of the candidate (x_0). Otherwise, the argument itself
  193. is its derived base name (as is the case with argument x_0).
  194. The hidden basis for statement (6) is the nearest dominating candidate
  195. whose base name is the derived base name (x_0) of the feeding phi (4),
  196. and whose stride is identical to that of the statement. We can then
  197. create the new "phi basis" (4a) and feeding adds along incoming arcs (3a),
  198. allowing the final replacement of (6) by the strength-reduced (6r).
  199. To facilitate this, a new kind of candidate (CAND_PHI) is introduced.
  200. A CAND_PHI is not a candidate for replacement, but is maintained in the
  201. candidate table to ease discovery of hidden bases. Any phi statement
  202. whose arguments share a common derived base name is entered into the
  203. table with the derived base name, an (arbitrary) index of zero, and a
  204. stride of 1. A statement with a hidden basis can then be detected by
  205. simply looking up its feeding phi definition in the candidate table,
  206. extracting the derived base name, and searching for a basis in the
  207. usual manner after substituting the derived base name.
  208. Note that the transformation is only valid when the original phi and
  209. the statements that define the phi's arguments are all at the same
  210. position in the loop hierarchy. */
  211. /* Index into the candidate vector, offset by 1. VECs are zero-based,
  212. while cand_idx's are one-based, with zero indicating null. */
  213. typedef unsigned cand_idx;
  214. /* The kind of candidate. */
  215. enum cand_kind
  216. {
  217. CAND_MULT,
  218. CAND_ADD,
  219. CAND_REF,
  220. CAND_PHI
  221. };
  222. struct slsr_cand_d
  223. {
  224. /* The candidate statement S1. */
  225. gimple cand_stmt;
  226. /* The base expression B: often an SSA name, but not always. */
  227. tree base_expr;
  228. /* The stride S. */
  229. tree stride;
  230. /* The index constant i. */
  231. widest_int index;
  232. /* The type of the candidate. This is normally the type of base_expr,
  233. but casts may have occurred when combining feeding instructions.
  234. A candidate can only be a basis for candidates of the same final type.
  235. (For CAND_REFs, this is the type to be used for operand 1 of the
  236. replacement MEM_REF.) */
  237. tree cand_type;
  238. /* The kind of candidate (CAND_MULT, etc.). */
  239. enum cand_kind kind;
  240. /* Index of this candidate in the candidate vector. */
  241. cand_idx cand_num;
  242. /* Index of the next candidate record for the same statement.
  243. A statement may be useful in more than one way (e.g., due to
  244. commutativity). So we can have multiple "interpretations"
  245. of a statement. */
  246. cand_idx next_interp;
  247. /* Index of the basis statement S0, if any, in the candidate vector. */
  248. cand_idx basis;
  249. /* First candidate for which this candidate is a basis, if one exists. */
  250. cand_idx dependent;
  251. /* Next candidate having the same basis as this one. */
  252. cand_idx sibling;
  253. /* If this is a conditional candidate, the CAND_PHI candidate
  254. that defines the base SSA name B. */
  255. cand_idx def_phi;
  256. /* Savings that can be expected from eliminating dead code if this
  257. candidate is replaced. */
  258. int dead_savings;
  259. };
  260. typedef struct slsr_cand_d slsr_cand, *slsr_cand_t;
  261. typedef const struct slsr_cand_d *const_slsr_cand_t;
  262. /* Pointers to candidates are chained together as part of a mapping
  263. from base expressions to the candidates that use them. */
  264. struct cand_chain_d
  265. {
  266. /* Base expression for the chain of candidates: often, but not
  267. always, an SSA name. */
  268. tree base_expr;
  269. /* Pointer to a candidate. */
  270. slsr_cand_t cand;
  271. /* Chain pointer. */
  272. struct cand_chain_d *next;
  273. };
  274. typedef struct cand_chain_d cand_chain, *cand_chain_t;
  275. typedef const struct cand_chain_d *const_cand_chain_t;
  276. /* Information about a unique "increment" associated with candidates
  277. having an SSA name for a stride. An increment is the difference
  278. between the index of the candidate and the index of its basis,
  279. i.e., (i - i') as discussed in the module commentary.
  280. When we are not going to generate address arithmetic we treat
  281. increments that differ only in sign as the same, allowing sharing
  282. of the cost of initializers. The absolute value of the increment
  283. is stored in the incr_info. */
  284. struct incr_info_d
  285. {
  286. /* The increment that relates a candidate to its basis. */
  287. widest_int incr;
  288. /* How many times the increment occurs in the candidate tree. */
  289. unsigned count;
  290. /* Cost of replacing candidates using this increment. Negative and
  291. zero costs indicate replacement should be performed. */
  292. int cost;
  293. /* If this increment is profitable but is not -1, 0, or 1, it requires
  294. an initializer T_0 = stride * incr to be found or introduced in the
  295. nearest common dominator of all candidates. This field holds T_0
  296. for subsequent use. */
  297. tree initializer;
  298. /* If the initializer was found to already exist, this is the block
  299. where it was found. */
  300. basic_block init_bb;
  301. };
  302. typedef struct incr_info_d incr_info, *incr_info_t;
  303. /* Candidates are maintained in a vector. If candidate X dominates
  304. candidate Y, then X appears before Y in the vector; but the
  305. converse does not necessarily hold. */
  306. static vec<slsr_cand_t> cand_vec;
  307. enum cost_consts
  308. {
  309. COST_NEUTRAL = 0,
  310. COST_INFINITE = 1000
  311. };
  312. enum stride_status
  313. {
  314. UNKNOWN_STRIDE = 0,
  315. KNOWN_STRIDE = 1
  316. };
  317. enum phi_adjust_status
  318. {
  319. NOT_PHI_ADJUST = 0,
  320. PHI_ADJUST = 1
  321. };
  322. enum count_phis_status
  323. {
  324. DONT_COUNT_PHIS = 0,
  325. COUNT_PHIS = 1
  326. };
  327. /* Pointer map embodying a mapping from statements to candidates. */
  328. static hash_map<gimple, slsr_cand_t> *stmt_cand_map;
  329. /* Obstack for candidates. */
  330. static struct obstack cand_obstack;
  331. /* Obstack for candidate chains. */
  332. static struct obstack chain_obstack;
  333. /* An array INCR_VEC of incr_infos is used during analysis of related
  334. candidates having an SSA name for a stride. INCR_VEC_LEN describes
  335. its current length. MAX_INCR_VEC_LEN is used to avoid costly
  336. pathological cases. */
  337. static incr_info_t incr_vec;
  338. static unsigned incr_vec_len;
  339. const int MAX_INCR_VEC_LEN = 16;
  340. /* For a chain of candidates with unknown stride, indicates whether or not
  341. we must generate pointer arithmetic when replacing statements. */
  342. static bool address_arithmetic_p;
  343. /* Forward function declarations. */
  344. static slsr_cand_t base_cand_from_table (tree);
  345. static tree introduce_cast_before_cand (slsr_cand_t, tree, tree);
  346. static bool legal_cast_p_1 (tree, tree);
  347. /* Produce a pointer to the IDX'th candidate in the candidate vector. */
  348. static slsr_cand_t
  349. lookup_cand (cand_idx idx)
  350. {
  351. return cand_vec[idx - 1];
  352. }
  353. /* Helper for hashing a candidate chain header. */
  354. struct cand_chain_hasher : typed_noop_remove <cand_chain>
  355. {
  356. typedef cand_chain value_type;
  357. typedef cand_chain compare_type;
  358. static inline hashval_t hash (const value_type *);
  359. static inline bool equal (const value_type *, const compare_type *);
  360. };
  361. inline hashval_t
  362. cand_chain_hasher::hash (const value_type *p)
  363. {
  364. tree base_expr = p->base_expr;
  365. return iterative_hash_expr (base_expr, 0);
  366. }
  367. inline bool
  368. cand_chain_hasher::equal (const value_type *chain1, const compare_type *chain2)
  369. {
  370. return operand_equal_p (chain1->base_expr, chain2->base_expr, 0);
  371. }
  372. /* Hash table embodying a mapping from base exprs to chains of candidates. */
  373. static hash_table<cand_chain_hasher> *base_cand_map;
  374. /* Pointer map used by tree_to_aff_combination_expand. */
  375. static hash_map<tree, name_expansion *> *name_expansions;
  376. /* Pointer map embodying a mapping from bases to alternative bases. */
  377. static hash_map<tree, tree> *alt_base_map;
  378. /* Given BASE, use the tree affine combiniation facilities to
  379. find the underlying tree expression for BASE, with any
  380. immediate offset excluded.
  381. N.B. we should eliminate this backtracking with better forward
  382. analysis in a future release. */
  383. static tree
  384. get_alternative_base (tree base)
  385. {
  386. tree *result = alt_base_map->get (base);
  387. if (result == NULL)
  388. {
  389. tree expr;
  390. aff_tree aff;
  391. tree_to_aff_combination_expand (base, TREE_TYPE (base),
  392. &aff, &name_expansions);
  393. aff.offset = 0;
  394. expr = aff_combination_to_tree (&aff);
  395. gcc_assert (!alt_base_map->put (base, base == expr ? NULL : expr));
  396. return expr == base ? NULL : expr;
  397. }
  398. return *result;
  399. }
  400. /* Look in the candidate table for a CAND_PHI that defines BASE and
  401. return it if found; otherwise return NULL. */
  402. static cand_idx
  403. find_phi_def (tree base)
  404. {
  405. slsr_cand_t c;
  406. if (TREE_CODE (base) != SSA_NAME)
  407. return 0;
  408. c = base_cand_from_table (base);
  409. if (!c || c->kind != CAND_PHI)
  410. return 0;
  411. return c->cand_num;
  412. }
  413. /* Helper routine for find_basis_for_candidate. May be called twice:
  414. once for the candidate's base expr, and optionally again either for
  415. the candidate's phi definition or for a CAND_REF's alternative base
  416. expression. */
  417. static slsr_cand_t
  418. find_basis_for_base_expr (slsr_cand_t c, tree base_expr)
  419. {
  420. cand_chain mapping_key;
  421. cand_chain_t chain;
  422. slsr_cand_t basis = NULL;
  423. // Limit potential of N^2 behavior for long candidate chains.
  424. int iters = 0;
  425. int max_iters = PARAM_VALUE (PARAM_MAX_SLSR_CANDIDATE_SCAN);
  426. mapping_key.base_expr = base_expr;
  427. chain = base_cand_map->find (&mapping_key);
  428. for (; chain && iters < max_iters; chain = chain->next, ++iters)
  429. {
  430. slsr_cand_t one_basis = chain->cand;
  431. if (one_basis->kind != c->kind
  432. || one_basis->cand_stmt == c->cand_stmt
  433. || !operand_equal_p (one_basis->stride, c->stride, 0)
  434. || !types_compatible_p (one_basis->cand_type, c->cand_type)
  435. || !dominated_by_p (CDI_DOMINATORS,
  436. gimple_bb (c->cand_stmt),
  437. gimple_bb (one_basis->cand_stmt)))
  438. continue;
  439. if (!basis || basis->cand_num < one_basis->cand_num)
  440. basis = one_basis;
  441. }
  442. return basis;
  443. }
  444. /* Use the base expr from candidate C to look for possible candidates
  445. that can serve as a basis for C. Each potential basis must also
  446. appear in a block that dominates the candidate statement and have
  447. the same stride and type. If more than one possible basis exists,
  448. the one with highest index in the vector is chosen; this will be
  449. the most immediately dominating basis. */
  450. static int
  451. find_basis_for_candidate (slsr_cand_t c)
  452. {
  453. slsr_cand_t basis = find_basis_for_base_expr (c, c->base_expr);
  454. /* If a candidate doesn't have a basis using its base expression,
  455. it may have a basis hidden by one or more intervening phis. */
  456. if (!basis && c->def_phi)
  457. {
  458. basic_block basis_bb, phi_bb;
  459. slsr_cand_t phi_cand = lookup_cand (c->def_phi);
  460. basis = find_basis_for_base_expr (c, phi_cand->base_expr);
  461. if (basis)
  462. {
  463. /* A hidden basis must dominate the phi-definition of the
  464. candidate's base name. */
  465. phi_bb = gimple_bb (phi_cand->cand_stmt);
  466. basis_bb = gimple_bb (basis->cand_stmt);
  467. if (phi_bb == basis_bb
  468. || !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb))
  469. {
  470. basis = NULL;
  471. c->basis = 0;
  472. }
  473. /* If we found a hidden basis, estimate additional dead-code
  474. savings if the phi and its feeding statements can be removed. */
  475. if (basis && has_single_use (gimple_phi_result (phi_cand->cand_stmt)))
  476. c->dead_savings += phi_cand->dead_savings;
  477. }
  478. }
  479. if (flag_expensive_optimizations && !basis && c->kind == CAND_REF)
  480. {
  481. tree alt_base_expr = get_alternative_base (c->base_expr);
  482. if (alt_base_expr)
  483. basis = find_basis_for_base_expr (c, alt_base_expr);
  484. }
  485. if (basis)
  486. {
  487. c->sibling = basis->dependent;
  488. basis->dependent = c->cand_num;
  489. return basis->cand_num;
  490. }
  491. return 0;
  492. }
  493. /* Record a mapping from BASE to C, indicating that C may potentially serve
  494. as a basis using that base expression. BASE may be the same as
  495. C->BASE_EXPR; alternatively BASE can be a different tree that share the
  496. underlining expression of C->BASE_EXPR. */
  497. static void
  498. record_potential_basis (slsr_cand_t c, tree base)
  499. {
  500. cand_chain_t node;
  501. cand_chain **slot;
  502. gcc_assert (base);
  503. node = (cand_chain_t) obstack_alloc (&chain_obstack, sizeof (cand_chain));
  504. node->base_expr = base;
  505. node->cand = c;
  506. node->next = NULL;
  507. slot = base_cand_map->find_slot (node, INSERT);
  508. if (*slot)
  509. {
  510. cand_chain_t head = (cand_chain_t) (*slot);
  511. node->next = head->next;
  512. head->next = node;
  513. }
  514. else
  515. *slot = node;
  516. }
  517. /* Allocate storage for a new candidate and initialize its fields.
  518. Attempt to find a basis for the candidate.
  519. For CAND_REF, an alternative base may also be recorded and used
  520. to find a basis. This helps cases where the expression hidden
  521. behind BASE (which is usually an SSA_NAME) has immediate offset,
  522. e.g.
  523. a2[i][j] = 1;
  524. a2[i + 20][j] = 2; */
  525. static slsr_cand_t
  526. alloc_cand_and_find_basis (enum cand_kind kind, gimple gs, tree base,
  527. const widest_int &index, tree stride, tree ctype,
  528. unsigned savings)
  529. {
  530. slsr_cand_t c = (slsr_cand_t) obstack_alloc (&cand_obstack,
  531. sizeof (slsr_cand));
  532. c->cand_stmt = gs;
  533. c->base_expr = base;
  534. c->stride = stride;
  535. c->index = index;
  536. c->cand_type = ctype;
  537. c->kind = kind;
  538. c->cand_num = cand_vec.length () + 1;
  539. c->next_interp = 0;
  540. c->dependent = 0;
  541. c->sibling = 0;
  542. c->def_phi = kind == CAND_MULT ? find_phi_def (base) : 0;
  543. c->dead_savings = savings;
  544. cand_vec.safe_push (c);
  545. if (kind == CAND_PHI)
  546. c->basis = 0;
  547. else
  548. c->basis = find_basis_for_candidate (c);
  549. record_potential_basis (c, base);
  550. if (flag_expensive_optimizations && kind == CAND_REF)
  551. {
  552. tree alt_base = get_alternative_base (base);
  553. if (alt_base)
  554. record_potential_basis (c, alt_base);
  555. }
  556. return c;
  557. }
  558. /* Determine the target cost of statement GS when compiling according
  559. to SPEED. */
  560. static int
  561. stmt_cost (gimple gs, bool speed)
  562. {
  563. tree lhs, rhs1, rhs2;
  564. machine_mode lhs_mode;
  565. gcc_assert (is_gimple_assign (gs));
  566. lhs = gimple_assign_lhs (gs);
  567. rhs1 = gimple_assign_rhs1 (gs);
  568. lhs_mode = TYPE_MODE (TREE_TYPE (lhs));
  569. switch (gimple_assign_rhs_code (gs))
  570. {
  571. case MULT_EXPR:
  572. rhs2 = gimple_assign_rhs2 (gs);
  573. if (tree_fits_shwi_p (rhs2))
  574. return mult_by_coeff_cost (tree_to_shwi (rhs2), lhs_mode, speed);
  575. gcc_assert (TREE_CODE (rhs1) != INTEGER_CST);
  576. return mul_cost (speed, lhs_mode);
  577. case PLUS_EXPR:
  578. case POINTER_PLUS_EXPR:
  579. case MINUS_EXPR:
  580. return add_cost (speed, lhs_mode);
  581. case NEGATE_EXPR:
  582. return neg_cost (speed, lhs_mode);
  583. CASE_CONVERT:
  584. return convert_cost (lhs_mode, TYPE_MODE (TREE_TYPE (rhs1)), speed);
  585. /* Note that we don't assign costs to copies that in most cases
  586. will go away. */
  587. default:
  588. ;
  589. }
  590. gcc_unreachable ();
  591. return 0;
  592. }
  593. /* Look up the defining statement for BASE_IN and return a pointer
  594. to its candidate in the candidate table, if any; otherwise NULL.
  595. Only CAND_ADD and CAND_MULT candidates are returned. */
  596. static slsr_cand_t
  597. base_cand_from_table (tree base_in)
  598. {
  599. slsr_cand_t *result;
  600. gimple def = SSA_NAME_DEF_STMT (base_in);
  601. if (!def)
  602. return (slsr_cand_t) NULL;
  603. result = stmt_cand_map->get (def);
  604. if (result && (*result)->kind != CAND_REF)
  605. return *result;
  606. return (slsr_cand_t) NULL;
  607. }
  608. /* Add an entry to the statement-to-candidate mapping. */
  609. static void
  610. add_cand_for_stmt (gimple gs, slsr_cand_t c)
  611. {
  612. gcc_assert (!stmt_cand_map->put (gs, c));
  613. }
  614. /* Given PHI which contains a phi statement, determine whether it
  615. satisfies all the requirements of a phi candidate. If so, create
  616. a candidate. Note that a CAND_PHI never has a basis itself, but
  617. is used to help find a basis for subsequent candidates. */
  618. static void
  619. slsr_process_phi (gphi *phi, bool speed)
  620. {
  621. unsigned i;
  622. tree arg0_base = NULL_TREE, base_type;
  623. slsr_cand_t c;
  624. struct loop *cand_loop = gimple_bb (phi)->loop_father;
  625. unsigned savings = 0;
  626. /* A CAND_PHI requires each of its arguments to have the same
  627. derived base name. (See the module header commentary for a
  628. definition of derived base names.) Furthermore, all feeding
  629. definitions must be in the same position in the loop hierarchy
  630. as PHI. */
  631. for (i = 0; i < gimple_phi_num_args (phi); i++)
  632. {
  633. slsr_cand_t arg_cand;
  634. tree arg = gimple_phi_arg_def (phi, i);
  635. tree derived_base_name = NULL_TREE;
  636. gimple arg_stmt = NULL;
  637. basic_block arg_bb = NULL;
  638. if (TREE_CODE (arg) != SSA_NAME)
  639. return;
  640. arg_cand = base_cand_from_table (arg);
  641. if (arg_cand)
  642. {
  643. while (arg_cand->kind != CAND_ADD && arg_cand->kind != CAND_PHI)
  644. {
  645. if (!arg_cand->next_interp)
  646. return;
  647. arg_cand = lookup_cand (arg_cand->next_interp);
  648. }
  649. if (!integer_onep (arg_cand->stride))
  650. return;
  651. derived_base_name = arg_cand->base_expr;
  652. arg_stmt = arg_cand->cand_stmt;
  653. arg_bb = gimple_bb (arg_stmt);
  654. /* Gather potential dead code savings if the phi statement
  655. can be removed later on. */
  656. if (has_single_use (arg))
  657. {
  658. if (gimple_code (arg_stmt) == GIMPLE_PHI)
  659. savings += arg_cand->dead_savings;
  660. else
  661. savings += stmt_cost (arg_stmt, speed);
  662. }
  663. }
  664. else
  665. {
  666. derived_base_name = arg;
  667. if (SSA_NAME_IS_DEFAULT_DEF (arg))
  668. arg_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
  669. else
  670. gimple_bb (SSA_NAME_DEF_STMT (arg));
  671. }
  672. if (!arg_bb || arg_bb->loop_father != cand_loop)
  673. return;
  674. if (i == 0)
  675. arg0_base = derived_base_name;
  676. else if (!operand_equal_p (derived_base_name, arg0_base, 0))
  677. return;
  678. }
  679. /* Create the candidate. "alloc_cand_and_find_basis" is named
  680. misleadingly for this case, as no basis will be sought for a
  681. CAND_PHI. */
  682. base_type = TREE_TYPE (arg0_base);
  683. c = alloc_cand_and_find_basis (CAND_PHI, phi, arg0_base,
  684. 0, integer_one_node, base_type, savings);
  685. /* Add the candidate to the statement-candidate mapping. */
  686. add_cand_for_stmt (phi, c);
  687. }
  688. /* Given PBASE which is a pointer to tree, look up the defining
  689. statement for it and check whether the candidate is in the
  690. form of:
  691. X = B + (1 * S), S is integer constant
  692. X = B + (i * S), S is integer one
  693. If so, set PBASE to the candidate's base_expr and return double
  694. int (i * S).
  695. Otherwise, just return double int zero. */
  696. static widest_int
  697. backtrace_base_for_ref (tree *pbase)
  698. {
  699. tree base_in = *pbase;
  700. slsr_cand_t base_cand;
  701. STRIP_NOPS (base_in);
  702. /* Strip off widening conversion(s) to handle cases where
  703. e.g. 'B' is widened from an 'int' in order to calculate
  704. a 64-bit address. */
  705. if (CONVERT_EXPR_P (base_in)
  706. && legal_cast_p_1 (base_in, TREE_OPERAND (base_in, 0)))
  707. base_in = get_unwidened (base_in, NULL_TREE);
  708. if (TREE_CODE (base_in) != SSA_NAME)
  709. return 0;
  710. base_cand = base_cand_from_table (base_in);
  711. while (base_cand && base_cand->kind != CAND_PHI)
  712. {
  713. if (base_cand->kind == CAND_ADD
  714. && base_cand->index == 1
  715. && TREE_CODE (base_cand->stride) == INTEGER_CST)
  716. {
  717. /* X = B + (1 * S), S is integer constant. */
  718. *pbase = base_cand->base_expr;
  719. return wi::to_widest (base_cand->stride);
  720. }
  721. else if (base_cand->kind == CAND_ADD
  722. && TREE_CODE (base_cand->stride) == INTEGER_CST
  723. && integer_onep (base_cand->stride))
  724. {
  725. /* X = B + (i * S), S is integer one. */
  726. *pbase = base_cand->base_expr;
  727. return base_cand->index;
  728. }
  729. if (base_cand->next_interp)
  730. base_cand = lookup_cand (base_cand->next_interp);
  731. else
  732. base_cand = NULL;
  733. }
  734. return 0;
  735. }
  736. /* Look for the following pattern:
  737. *PBASE: MEM_REF (T1, C1)
  738. *POFFSET: MULT_EXPR (T2, C3) [C2 is zero]
  739. or
  740. MULT_EXPR (PLUS_EXPR (T2, C2), C3)
  741. or
  742. MULT_EXPR (MINUS_EXPR (T2, -C2), C3)
  743. *PINDEX: C4 * BITS_PER_UNIT
  744. If not present, leave the input values unchanged and return FALSE.
  745. Otherwise, modify the input values as follows and return TRUE:
  746. *PBASE: T1
  747. *POFFSET: MULT_EXPR (T2, C3)
  748. *PINDEX: C1 + (C2 * C3) + C4
  749. When T2 is recorded by a CAND_ADD in the form of (T2' + C5), it
  750. will be further restructured to:
  751. *PBASE: T1
  752. *POFFSET: MULT_EXPR (T2', C3)
  753. *PINDEX: C1 + (C2 * C3) + C4 + (C5 * C3) */
  754. static bool
  755. restructure_reference (tree *pbase, tree *poffset, widest_int *pindex,
  756. tree *ptype)
  757. {
  758. tree base = *pbase, offset = *poffset;
  759. widest_int index = *pindex;
  760. tree mult_op0, t1, t2, type;
  761. widest_int c1, c2, c3, c4, c5;
  762. if (!base
  763. || !offset
  764. || TREE_CODE (base) != MEM_REF
  765. || TREE_CODE (offset) != MULT_EXPR
  766. || TREE_CODE (TREE_OPERAND (offset, 1)) != INTEGER_CST
  767. || wi::umod_floor (index, BITS_PER_UNIT) != 0)
  768. return false;
  769. t1 = TREE_OPERAND (base, 0);
  770. c1 = widest_int::from (mem_ref_offset (base), SIGNED);
  771. type = TREE_TYPE (TREE_OPERAND (base, 1));
  772. mult_op0 = TREE_OPERAND (offset, 0);
  773. c3 = wi::to_widest (TREE_OPERAND (offset, 1));
  774. if (TREE_CODE (mult_op0) == PLUS_EXPR)
  775. if (TREE_CODE (TREE_OPERAND (mult_op0, 1)) == INTEGER_CST)
  776. {
  777. t2 = TREE_OPERAND (mult_op0, 0);
  778. c2 = wi::to_widest (TREE_OPERAND (mult_op0, 1));
  779. }
  780. else
  781. return false;
  782. else if (TREE_CODE (mult_op0) == MINUS_EXPR)
  783. if (TREE_CODE (TREE_OPERAND (mult_op0, 1)) == INTEGER_CST)
  784. {
  785. t2 = TREE_OPERAND (mult_op0, 0);
  786. c2 = -wi::to_widest (TREE_OPERAND (mult_op0, 1));
  787. }
  788. else
  789. return false;
  790. else
  791. {
  792. t2 = mult_op0;
  793. c2 = 0;
  794. }
  795. c4 = wi::lrshift (index, LOG2_BITS_PER_UNIT);
  796. c5 = backtrace_base_for_ref (&t2);
  797. *pbase = t1;
  798. *poffset = fold_build2 (MULT_EXPR, sizetype, fold_convert (sizetype, t2),
  799. wide_int_to_tree (sizetype, c3));
  800. *pindex = c1 + c2 * c3 + c4 + c5 * c3;
  801. *ptype = type;
  802. return true;
  803. }
  804. /* Given GS which contains a data reference, create a CAND_REF entry in
  805. the candidate table and attempt to find a basis. */
  806. static void
  807. slsr_process_ref (gimple gs)
  808. {
  809. tree ref_expr, base, offset, type;
  810. HOST_WIDE_INT bitsize, bitpos;
  811. machine_mode mode;
  812. int unsignedp, volatilep;
  813. slsr_cand_t c;
  814. if (gimple_vdef (gs))
  815. ref_expr = gimple_assign_lhs (gs);
  816. else
  817. ref_expr = gimple_assign_rhs1 (gs);
  818. if (!handled_component_p (ref_expr)
  819. || TREE_CODE (ref_expr) == BIT_FIELD_REF
  820. || (TREE_CODE (ref_expr) == COMPONENT_REF
  821. && DECL_BIT_FIELD (TREE_OPERAND (ref_expr, 1))))
  822. return;
  823. base = get_inner_reference (ref_expr, &bitsize, &bitpos, &offset, &mode,
  824. &unsignedp, &volatilep, false);
  825. widest_int index = bitpos;
  826. if (!restructure_reference (&base, &offset, &index, &type))
  827. return;
  828. c = alloc_cand_and_find_basis (CAND_REF, gs, base, index, offset,
  829. type, 0);
  830. /* Add the candidate to the statement-candidate mapping. */
  831. add_cand_for_stmt (gs, c);
  832. }
  833. /* Create a candidate entry for a statement GS, where GS multiplies
  834. two SSA names BASE_IN and STRIDE_IN. Propagate any known information
  835. about the two SSA names into the new candidate. Return the new
  836. candidate. */
  837. static slsr_cand_t
  838. create_mul_ssa_cand (gimple gs, tree base_in, tree stride_in, bool speed)
  839. {
  840. tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
  841. widest_int index;
  842. unsigned savings = 0;
  843. slsr_cand_t c;
  844. slsr_cand_t base_cand = base_cand_from_table (base_in);
  845. /* Look at all interpretations of the base candidate, if necessary,
  846. to find information to propagate into this candidate. */
  847. while (base_cand && !base && base_cand->kind != CAND_PHI)
  848. {
  849. if (base_cand->kind == CAND_MULT && integer_onep (base_cand->stride))
  850. {
  851. /* Y = (B + i') * 1
  852. X = Y * Z
  853. ================
  854. X = (B + i') * Z */
  855. base = base_cand->base_expr;
  856. index = base_cand->index;
  857. stride = stride_in;
  858. ctype = base_cand->cand_type;
  859. if (has_single_use (base_in))
  860. savings = (base_cand->dead_savings
  861. + stmt_cost (base_cand->cand_stmt, speed));
  862. }
  863. else if (base_cand->kind == CAND_ADD
  864. && TREE_CODE (base_cand->stride) == INTEGER_CST)
  865. {
  866. /* Y = B + (i' * S), S constant
  867. X = Y * Z
  868. ============================
  869. X = B + ((i' * S) * Z) */
  870. base = base_cand->base_expr;
  871. index = base_cand->index * wi::to_widest (base_cand->stride);
  872. stride = stride_in;
  873. ctype = base_cand->cand_type;
  874. if (has_single_use (base_in))
  875. savings = (base_cand->dead_savings
  876. + stmt_cost (base_cand->cand_stmt, speed));
  877. }
  878. if (base_cand->next_interp)
  879. base_cand = lookup_cand (base_cand->next_interp);
  880. else
  881. base_cand = NULL;
  882. }
  883. if (!base)
  884. {
  885. /* No interpretations had anything useful to propagate, so
  886. produce X = (Y + 0) * Z. */
  887. base = base_in;
  888. index = 0;
  889. stride = stride_in;
  890. ctype = TREE_TYPE (base_in);
  891. }
  892. c = alloc_cand_and_find_basis (CAND_MULT, gs, base, index, stride,
  893. ctype, savings);
  894. return c;
  895. }
  896. /* Create a candidate entry for a statement GS, where GS multiplies
  897. SSA name BASE_IN by constant STRIDE_IN. Propagate any known
  898. information about BASE_IN into the new candidate. Return the new
  899. candidate. */
  900. static slsr_cand_t
  901. create_mul_imm_cand (gimple gs, tree base_in, tree stride_in, bool speed)
  902. {
  903. tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
  904. widest_int index, temp;
  905. unsigned savings = 0;
  906. slsr_cand_t c;
  907. slsr_cand_t base_cand = base_cand_from_table (base_in);
  908. /* Look at all interpretations of the base candidate, if necessary,
  909. to find information to propagate into this candidate. */
  910. while (base_cand && !base && base_cand->kind != CAND_PHI)
  911. {
  912. if (base_cand->kind == CAND_MULT
  913. && TREE_CODE (base_cand->stride) == INTEGER_CST)
  914. {
  915. /* Y = (B + i') * S, S constant
  916. X = Y * c
  917. ============================
  918. X = (B + i') * (S * c) */
  919. temp = wi::to_widest (base_cand->stride) * wi::to_widest (stride_in);
  920. if (wi::fits_to_tree_p (temp, TREE_TYPE (stride_in)))
  921. {
  922. base = base_cand->base_expr;
  923. index = base_cand->index;
  924. stride = wide_int_to_tree (TREE_TYPE (stride_in), temp);
  925. ctype = base_cand->cand_type;
  926. if (has_single_use (base_in))
  927. savings = (base_cand->dead_savings
  928. + stmt_cost (base_cand->cand_stmt, speed));
  929. }
  930. }
  931. else if (base_cand->kind == CAND_ADD && integer_onep (base_cand->stride))
  932. {
  933. /* Y = B + (i' * 1)
  934. X = Y * c
  935. ===========================
  936. X = (B + i') * c */
  937. base = base_cand->base_expr;
  938. index = base_cand->index;
  939. stride = stride_in;
  940. ctype = base_cand->cand_type;
  941. if (has_single_use (base_in))
  942. savings = (base_cand->dead_savings
  943. + stmt_cost (base_cand->cand_stmt, speed));
  944. }
  945. else if (base_cand->kind == CAND_ADD
  946. && base_cand->index == 1
  947. && TREE_CODE (base_cand->stride) == INTEGER_CST)
  948. {
  949. /* Y = B + (1 * S), S constant
  950. X = Y * c
  951. ===========================
  952. X = (B + S) * c */
  953. base = base_cand->base_expr;
  954. index = wi::to_widest (base_cand->stride);
  955. stride = stride_in;
  956. ctype = base_cand->cand_type;
  957. if (has_single_use (base_in))
  958. savings = (base_cand->dead_savings
  959. + stmt_cost (base_cand->cand_stmt, speed));
  960. }
  961. if (base_cand->next_interp)
  962. base_cand = lookup_cand (base_cand->next_interp);
  963. else
  964. base_cand = NULL;
  965. }
  966. if (!base)
  967. {
  968. /* No interpretations had anything useful to propagate, so
  969. produce X = (Y + 0) * c. */
  970. base = base_in;
  971. index = 0;
  972. stride = stride_in;
  973. ctype = TREE_TYPE (base_in);
  974. }
  975. c = alloc_cand_and_find_basis (CAND_MULT, gs, base, index, stride,
  976. ctype, savings);
  977. return c;
  978. }
  979. /* Given GS which is a multiply of scalar integers, make an appropriate
  980. entry in the candidate table. If this is a multiply of two SSA names,
  981. create two CAND_MULT interpretations and attempt to find a basis for
  982. each of them. Otherwise, create a single CAND_MULT and attempt to
  983. find a basis. */
  984. static void
  985. slsr_process_mul (gimple gs, tree rhs1, tree rhs2, bool speed)
  986. {
  987. slsr_cand_t c, c2;
  988. /* If this is a multiply of an SSA name with itself, it is highly
  989. unlikely that we will get a strength reduction opportunity, so
  990. don't record it as a candidate. This simplifies the logic for
  991. finding a basis, so if this is removed that must be considered. */
  992. if (rhs1 == rhs2)
  993. return;
  994. if (TREE_CODE (rhs2) == SSA_NAME)
  995. {
  996. /* Record an interpretation of this statement in the candidate table
  997. assuming RHS1 is the base expression and RHS2 is the stride. */
  998. c = create_mul_ssa_cand (gs, rhs1, rhs2, speed);
  999. /* Add the first interpretation to the statement-candidate mapping. */
  1000. add_cand_for_stmt (gs, c);
  1001. /* Record another interpretation of this statement assuming RHS1
  1002. is the stride and RHS2 is the base expression. */
  1003. c2 = create_mul_ssa_cand (gs, rhs2, rhs1, speed);
  1004. c->next_interp = c2->cand_num;
  1005. }
  1006. else
  1007. {
  1008. /* Record an interpretation for the multiply-immediate. */
  1009. c = create_mul_imm_cand (gs, rhs1, rhs2, speed);
  1010. /* Add the interpretation to the statement-candidate mapping. */
  1011. add_cand_for_stmt (gs, c);
  1012. }
  1013. }
  1014. /* Create a candidate entry for a statement GS, where GS adds two
  1015. SSA names BASE_IN and ADDEND_IN if SUBTRACT_P is false, and
  1016. subtracts ADDEND_IN from BASE_IN otherwise. Propagate any known
  1017. information about the two SSA names into the new candidate.
  1018. Return the new candidate. */
  1019. static slsr_cand_t
  1020. create_add_ssa_cand (gimple gs, tree base_in, tree addend_in,
  1021. bool subtract_p, bool speed)
  1022. {
  1023. tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL;
  1024. widest_int index;
  1025. unsigned savings = 0;
  1026. slsr_cand_t c;
  1027. slsr_cand_t base_cand = base_cand_from_table (base_in);
  1028. slsr_cand_t addend_cand = base_cand_from_table (addend_in);
  1029. /* The most useful transformation is a multiply-immediate feeding
  1030. an add or subtract. Look for that first. */
  1031. while (addend_cand && !base && addend_cand->kind != CAND_PHI)
  1032. {
  1033. if (addend_cand->kind == CAND_MULT
  1034. && addend_cand->index == 0
  1035. && TREE_CODE (addend_cand->stride) == INTEGER_CST)
  1036. {
  1037. /* Z = (B + 0) * S, S constant
  1038. X = Y +/- Z
  1039. ===========================
  1040. X = Y + ((+/-1 * S) * B) */
  1041. base = base_in;
  1042. index = wi::to_widest (addend_cand->stride);
  1043. if (subtract_p)
  1044. index = -index;
  1045. stride = addend_cand->base_expr;
  1046. ctype = TREE_TYPE (base_in);
  1047. if (has_single_use (addend_in))
  1048. savings = (addend_cand->dead_savings
  1049. + stmt_cost (addend_cand->cand_stmt, speed));
  1050. }
  1051. if (addend_cand->next_interp)
  1052. addend_cand = lookup_cand (addend_cand->next_interp);
  1053. else
  1054. addend_cand = NULL;
  1055. }
  1056. while (base_cand && !base && base_cand->kind != CAND_PHI)
  1057. {
  1058. if (base_cand->kind == CAND_ADD
  1059. && (base_cand->index == 0
  1060. || operand_equal_p (base_cand->stride,
  1061. integer_zero_node, 0)))
  1062. {
  1063. /* Y = B + (i' * S), i' * S = 0
  1064. X = Y +/- Z
  1065. ============================
  1066. X = B + (+/-1 * Z) */
  1067. base = base_cand->base_expr;
  1068. index = subtract_p ? -1 : 1;
  1069. stride = addend_in;
  1070. ctype = base_cand->cand_type;
  1071. if (has_single_use (base_in))
  1072. savings = (base_cand->dead_savings
  1073. + stmt_cost (base_cand->cand_stmt, speed));
  1074. }
  1075. else if (subtract_p)
  1076. {
  1077. slsr_cand_t subtrahend_cand = base_cand_from_table (addend_in);
  1078. while (subtrahend_cand && !base && subtrahend_cand->kind != CAND_PHI)
  1079. {
  1080. if (subtrahend_cand->kind == CAND_MULT
  1081. && subtrahend_cand->index == 0
  1082. && TREE_CODE (subtrahend_cand->stride) == INTEGER_CST)
  1083. {
  1084. /* Z = (B + 0) * S, S constant
  1085. X = Y - Z
  1086. ===========================
  1087. Value: X = Y + ((-1 * S) * B) */
  1088. base = base_in;
  1089. index = wi::to_widest (subtrahend_cand->stride);
  1090. index = -index;
  1091. stride = subtrahend_cand->base_expr;
  1092. ctype = TREE_TYPE (base_in);
  1093. if (has_single_use (addend_in))
  1094. savings = (subtrahend_cand->dead_savings
  1095. + stmt_cost (subtrahend_cand->cand_stmt, speed));
  1096. }
  1097. if (subtrahend_cand->next_interp)
  1098. subtrahend_cand = lookup_cand (subtrahend_cand->next_interp);
  1099. else
  1100. subtrahend_cand = NULL;
  1101. }
  1102. }
  1103. if (base_cand->next_interp)
  1104. base_cand = lookup_cand (base_cand->next_interp);
  1105. else
  1106. base_cand = NULL;
  1107. }
  1108. if (!base)
  1109. {
  1110. /* No interpretations had anything useful to propagate, so
  1111. produce X = Y + (1 * Z). */
  1112. base = base_in;
  1113. index = subtract_p ? -1 : 1;
  1114. stride = addend_in;
  1115. ctype = TREE_TYPE (base_in);
  1116. }
  1117. c = alloc_cand_and_find_basis (CAND_ADD, gs, base, index, stride,
  1118. ctype, savings);
  1119. return c;
  1120. }
  1121. /* Create a candidate entry for a statement GS, where GS adds SSA
  1122. name BASE_IN to constant INDEX_IN. Propagate any known information
  1123. about BASE_IN into the new candidate. Return the new candidate. */
  1124. static slsr_cand_t
  1125. create_add_imm_cand (gimple gs, tree base_in, const widest_int &index_in,
  1126. bool speed)
  1127. {
  1128. enum cand_kind kind = CAND_ADD;
  1129. tree base = NULL_TREE, stride = NULL_TREE, ctype = NULL_TREE;
  1130. widest_int index, multiple;
  1131. unsigned savings = 0;
  1132. slsr_cand_t c;
  1133. slsr_cand_t base_cand = base_cand_from_table (base_in);
  1134. while (base_cand && !base && base_cand->kind != CAND_PHI)
  1135. {
  1136. signop sign = TYPE_SIGN (TREE_TYPE (base_cand->stride));
  1137. if (TREE_CODE (base_cand->stride) == INTEGER_CST
  1138. && wi::multiple_of_p (index_in, wi::to_widest (base_cand->stride),
  1139. sign, &multiple))
  1140. {
  1141. /* Y = (B + i') * S, S constant, c = kS for some integer k
  1142. X = Y + c
  1143. ============================
  1144. X = (B + (i'+ k)) * S
  1145. OR
  1146. Y = B + (i' * S), S constant, c = kS for some integer k
  1147. X = Y + c
  1148. ============================
  1149. X = (B + (i'+ k)) * S */
  1150. kind = base_cand->kind;
  1151. base = base_cand->base_expr;
  1152. index = base_cand->index + multiple;
  1153. stride = base_cand->stride;
  1154. ctype = base_cand->cand_type;
  1155. if (has_single_use (base_in))
  1156. savings = (base_cand->dead_savings
  1157. + stmt_cost (base_cand->cand_stmt, speed));
  1158. }
  1159. if (base_cand->next_interp)
  1160. base_cand = lookup_cand (base_cand->next_interp);
  1161. else
  1162. base_cand = NULL;
  1163. }
  1164. if (!base)
  1165. {
  1166. /* No interpretations had anything useful to propagate, so
  1167. produce X = Y + (c * 1). */
  1168. kind = CAND_ADD;
  1169. base = base_in;
  1170. index = index_in;
  1171. stride = integer_one_node;
  1172. ctype = TREE_TYPE (base_in);
  1173. }
  1174. c = alloc_cand_and_find_basis (kind, gs, base, index, stride,
  1175. ctype, savings);
  1176. return c;
  1177. }
  1178. /* Given GS which is an add or subtract of scalar integers or pointers,
  1179. make at least one appropriate entry in the candidate table. */
  1180. static void
  1181. slsr_process_add (gimple gs, tree rhs1, tree rhs2, bool speed)
  1182. {
  1183. bool subtract_p = gimple_assign_rhs_code (gs) == MINUS_EXPR;
  1184. slsr_cand_t c = NULL, c2;
  1185. if (TREE_CODE (rhs2) == SSA_NAME)
  1186. {
  1187. /* First record an interpretation assuming RHS1 is the base expression
  1188. and RHS2 is the stride. But it doesn't make sense for the
  1189. stride to be a pointer, so don't record a candidate in that case. */
  1190. if (!POINTER_TYPE_P (TREE_TYPE (rhs2)))
  1191. {
  1192. c = create_add_ssa_cand (gs, rhs1, rhs2, subtract_p, speed);
  1193. /* Add the first interpretation to the statement-candidate
  1194. mapping. */
  1195. add_cand_for_stmt (gs, c);
  1196. }
  1197. /* If the two RHS operands are identical, or this is a subtract,
  1198. we're done. */
  1199. if (operand_equal_p (rhs1, rhs2, 0) || subtract_p)
  1200. return;
  1201. /* Otherwise, record another interpretation assuming RHS2 is the
  1202. base expression and RHS1 is the stride, again provided that the
  1203. stride is not a pointer. */
  1204. if (!POINTER_TYPE_P (TREE_TYPE (rhs1)))
  1205. {
  1206. c2 = create_add_ssa_cand (gs, rhs2, rhs1, false, speed);
  1207. if (c)
  1208. c->next_interp = c2->cand_num;
  1209. else
  1210. add_cand_for_stmt (gs, c2);
  1211. }
  1212. }
  1213. else
  1214. {
  1215. /* Record an interpretation for the add-immediate. */
  1216. widest_int index = wi::to_widest (rhs2);
  1217. if (subtract_p)
  1218. index = -index;
  1219. c = create_add_imm_cand (gs, rhs1, index, speed);
  1220. /* Add the interpretation to the statement-candidate mapping. */
  1221. add_cand_for_stmt (gs, c);
  1222. }
  1223. }
  1224. /* Given GS which is a negate of a scalar integer, make an appropriate
  1225. entry in the candidate table. A negate is equivalent to a multiply
  1226. by -1. */
  1227. static void
  1228. slsr_process_neg (gimple gs, tree rhs1, bool speed)
  1229. {
  1230. /* Record a CAND_MULT interpretation for the multiply by -1. */
  1231. slsr_cand_t c = create_mul_imm_cand (gs, rhs1, integer_minus_one_node, speed);
  1232. /* Add the interpretation to the statement-candidate mapping. */
  1233. add_cand_for_stmt (gs, c);
  1234. }
  1235. /* Help function for legal_cast_p, operating on two trees. Checks
  1236. whether it's allowable to cast from RHS to LHS. See legal_cast_p
  1237. for more details. */
  1238. static bool
  1239. legal_cast_p_1 (tree lhs, tree rhs)
  1240. {
  1241. tree lhs_type, rhs_type;
  1242. unsigned lhs_size, rhs_size;
  1243. bool lhs_wraps, rhs_wraps;
  1244. lhs_type = TREE_TYPE (lhs);
  1245. rhs_type = TREE_TYPE (rhs);
  1246. lhs_size = TYPE_PRECISION (lhs_type);
  1247. rhs_size = TYPE_PRECISION (rhs_type);
  1248. lhs_wraps = ANY_INTEGRAL_TYPE_P (lhs_type) && TYPE_OVERFLOW_WRAPS (lhs_type);
  1249. rhs_wraps = ANY_INTEGRAL_TYPE_P (rhs_type) && TYPE_OVERFLOW_WRAPS (rhs_type);
  1250. if (lhs_size < rhs_size
  1251. || (rhs_wraps && !lhs_wraps)
  1252. || (rhs_wraps && lhs_wraps && rhs_size != lhs_size))
  1253. return false;
  1254. return true;
  1255. }
  1256. /* Return TRUE if GS is a statement that defines an SSA name from
  1257. a conversion and is legal for us to combine with an add and multiply
  1258. in the candidate table. For example, suppose we have:
  1259. A = B + i;
  1260. C = (type) A;
  1261. D = C * S;
  1262. Without the type-cast, we would create a CAND_MULT for D with base B,
  1263. index i, and stride S. We want to record this candidate only if it
  1264. is equivalent to apply the type cast following the multiply:
  1265. A = B + i;
  1266. E = A * S;
  1267. D = (type) E;
  1268. We will record the type with the candidate for D. This allows us
  1269. to use a similar previous candidate as a basis. If we have earlier seen
  1270. A' = B + i';
  1271. C' = (type) A';
  1272. D' = C' * S;
  1273. we can replace D with
  1274. D = D' + (i - i') * S;
  1275. But if moving the type-cast would change semantics, we mustn't do this.
  1276. This is legitimate for casts from a non-wrapping integral type to
  1277. any integral type of the same or larger size. It is not legitimate
  1278. to convert a wrapping type to a non-wrapping type, or to a wrapping
  1279. type of a different size. I.e., with a wrapping type, we must
  1280. assume that the addition B + i could wrap, in which case performing
  1281. the multiply before or after one of the "illegal" type casts will
  1282. have different semantics. */
  1283. static bool
  1284. legal_cast_p (gimple gs, tree rhs)
  1285. {
  1286. if (!is_gimple_assign (gs)
  1287. || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs)))
  1288. return false;
  1289. return legal_cast_p_1 (gimple_assign_lhs (gs), rhs);
  1290. }
  1291. /* Given GS which is a cast to a scalar integer type, determine whether
  1292. the cast is legal for strength reduction. If so, make at least one
  1293. appropriate entry in the candidate table. */
  1294. static void
  1295. slsr_process_cast (gimple gs, tree rhs1, bool speed)
  1296. {
  1297. tree lhs, ctype;
  1298. slsr_cand_t base_cand, c, c2;
  1299. unsigned savings = 0;
  1300. if (!legal_cast_p (gs, rhs1))
  1301. return;
  1302. lhs = gimple_assign_lhs (gs);
  1303. base_cand = base_cand_from_table (rhs1);
  1304. ctype = TREE_TYPE (lhs);
  1305. if (base_cand && base_cand->kind != CAND_PHI)
  1306. {
  1307. while (base_cand)
  1308. {
  1309. /* Propagate all data from the base candidate except the type,
  1310. which comes from the cast, and the base candidate's cast,
  1311. which is no longer applicable. */
  1312. if (has_single_use (rhs1))
  1313. savings = (base_cand->dead_savings
  1314. + stmt_cost (base_cand->cand_stmt, speed));
  1315. c = alloc_cand_and_find_basis (base_cand->kind, gs,
  1316. base_cand->base_expr,
  1317. base_cand->index, base_cand->stride,
  1318. ctype, savings);
  1319. if (base_cand->next_interp)
  1320. base_cand = lookup_cand (base_cand->next_interp);
  1321. else
  1322. base_cand = NULL;
  1323. }
  1324. }
  1325. else
  1326. {
  1327. /* If nothing is known about the RHS, create fresh CAND_ADD and
  1328. CAND_MULT interpretations:
  1329. X = Y + (0 * 1)
  1330. X = (Y + 0) * 1
  1331. The first of these is somewhat arbitrary, but the choice of
  1332. 1 for the stride simplifies the logic for propagating casts
  1333. into their uses. */
  1334. c = alloc_cand_and_find_basis (CAND_ADD, gs, rhs1,
  1335. 0, integer_one_node, ctype, 0);
  1336. c2 = alloc_cand_and_find_basis (CAND_MULT, gs, rhs1,
  1337. 0, integer_one_node, ctype, 0);
  1338. c->next_interp = c2->cand_num;
  1339. }
  1340. /* Add the first (or only) interpretation to the statement-candidate
  1341. mapping. */
  1342. add_cand_for_stmt (gs, c);
  1343. }
  1344. /* Given GS which is a copy of a scalar integer type, make at least one
  1345. appropriate entry in the candidate table.
  1346. This interface is included for completeness, but is unnecessary
  1347. if this pass immediately follows a pass that performs copy
  1348. propagation, such as DOM. */
  1349. static void
  1350. slsr_process_copy (gimple gs, tree rhs1, bool speed)
  1351. {
  1352. slsr_cand_t base_cand, c, c2;
  1353. unsigned savings = 0;
  1354. base_cand = base_cand_from_table (rhs1);
  1355. if (base_cand && base_cand->kind != CAND_PHI)
  1356. {
  1357. while (base_cand)
  1358. {
  1359. /* Propagate all data from the base candidate. */
  1360. if (has_single_use (rhs1))
  1361. savings = (base_cand->dead_savings
  1362. + stmt_cost (base_cand->cand_stmt, speed));
  1363. c = alloc_cand_and_find_basis (base_cand->kind, gs,
  1364. base_cand->base_expr,
  1365. base_cand->index, base_cand->stride,
  1366. base_cand->cand_type, savings);
  1367. if (base_cand->next_interp)
  1368. base_cand = lookup_cand (base_cand->next_interp);
  1369. else
  1370. base_cand = NULL;
  1371. }
  1372. }
  1373. else
  1374. {
  1375. /* If nothing is known about the RHS, create fresh CAND_ADD and
  1376. CAND_MULT interpretations:
  1377. X = Y + (0 * 1)
  1378. X = (Y + 0) * 1
  1379. The first of these is somewhat arbitrary, but the choice of
  1380. 1 for the stride simplifies the logic for propagating casts
  1381. into their uses. */
  1382. c = alloc_cand_and_find_basis (CAND_ADD, gs, rhs1,
  1383. 0, integer_one_node, TREE_TYPE (rhs1), 0);
  1384. c2 = alloc_cand_and_find_basis (CAND_MULT, gs, rhs1,
  1385. 0, integer_one_node, TREE_TYPE (rhs1), 0);
  1386. c->next_interp = c2->cand_num;
  1387. }
  1388. /* Add the first (or only) interpretation to the statement-candidate
  1389. mapping. */
  1390. add_cand_for_stmt (gs, c);
  1391. }
  1392. class find_candidates_dom_walker : public dom_walker
  1393. {
  1394. public:
  1395. find_candidates_dom_walker (cdi_direction direction)
  1396. : dom_walker (direction) {}
  1397. virtual void before_dom_children (basic_block);
  1398. };
  1399. /* Find strength-reduction candidates in block BB. */
  1400. void
  1401. find_candidates_dom_walker::before_dom_children (basic_block bb)
  1402. {
  1403. bool speed = optimize_bb_for_speed_p (bb);
  1404. for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
  1405. gsi_next (&gsi))
  1406. slsr_process_phi (gsi.phi (), speed);
  1407. for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
  1408. gsi_next (&gsi))
  1409. {
  1410. gimple gs = gsi_stmt (gsi);
  1411. if (gimple_vuse (gs) && gimple_assign_single_p (gs))
  1412. slsr_process_ref (gs);
  1413. else if (is_gimple_assign (gs)
  1414. && SCALAR_INT_MODE_P
  1415. (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))))
  1416. {
  1417. tree rhs1 = NULL_TREE, rhs2 = NULL_TREE;
  1418. switch (gimple_assign_rhs_code (gs))
  1419. {
  1420. case MULT_EXPR:
  1421. case PLUS_EXPR:
  1422. rhs1 = gimple_assign_rhs1 (gs);
  1423. rhs2 = gimple_assign_rhs2 (gs);
  1424. /* Should never happen, but currently some buggy situations
  1425. in earlier phases put constants in rhs1. */
  1426. if (TREE_CODE (rhs1) != SSA_NAME)
  1427. continue;
  1428. break;
  1429. /* Possible future opportunity: rhs1 of a ptr+ can be
  1430. an ADDR_EXPR. */
  1431. case POINTER_PLUS_EXPR:
  1432. case MINUS_EXPR:
  1433. rhs2 = gimple_assign_rhs2 (gs);
  1434. /* Fall-through. */
  1435. CASE_CONVERT:
  1436. case MODIFY_EXPR:
  1437. case NEGATE_EXPR:
  1438. rhs1 = gimple_assign_rhs1 (gs);
  1439. if (TREE_CODE (rhs1) != SSA_NAME)
  1440. continue;
  1441. break;
  1442. default:
  1443. ;
  1444. }
  1445. switch (gimple_assign_rhs_code (gs))
  1446. {
  1447. case MULT_EXPR:
  1448. slsr_process_mul (gs, rhs1, rhs2, speed);
  1449. break;
  1450. case PLUS_EXPR:
  1451. case POINTER_PLUS_EXPR:
  1452. case MINUS_EXPR:
  1453. slsr_process_add (gs, rhs1, rhs2, speed);
  1454. break;
  1455. case NEGATE_EXPR:
  1456. slsr_process_neg (gs, rhs1, speed);
  1457. break;
  1458. CASE_CONVERT:
  1459. slsr_process_cast (gs, rhs1, speed);
  1460. break;
  1461. case MODIFY_EXPR:
  1462. slsr_process_copy (gs, rhs1, speed);
  1463. break;
  1464. default:
  1465. ;
  1466. }
  1467. }
  1468. }
  1469. }
  1470. /* Dump a candidate for debug. */
  1471. static void
  1472. dump_candidate (slsr_cand_t c)
  1473. {
  1474. fprintf (dump_file, "%3d [%d] ", c->cand_num,
  1475. gimple_bb (c->cand_stmt)->index);
  1476. print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
  1477. switch (c->kind)
  1478. {
  1479. case CAND_MULT:
  1480. fputs (" MULT : (", dump_file);
  1481. print_generic_expr (dump_file, c->base_expr, 0);
  1482. fputs (" + ", dump_file);
  1483. print_decs (c->index, dump_file);
  1484. fputs (") * ", dump_file);
  1485. print_generic_expr (dump_file, c->stride, 0);
  1486. fputs (" : ", dump_file);
  1487. break;
  1488. case CAND_ADD:
  1489. fputs (" ADD : ", dump_file);
  1490. print_generic_expr (dump_file, c->base_expr, 0);
  1491. fputs (" + (", dump_file);
  1492. print_decs (c->index, dump_file);
  1493. fputs (" * ", dump_file);
  1494. print_generic_expr (dump_file, c->stride, 0);
  1495. fputs (") : ", dump_file);
  1496. break;
  1497. case CAND_REF:
  1498. fputs (" REF : ", dump_file);
  1499. print_generic_expr (dump_file, c->base_expr, 0);
  1500. fputs (" + (", dump_file);
  1501. print_generic_expr (dump_file, c->stride, 0);
  1502. fputs (") + ", dump_file);
  1503. print_decs (c->index, dump_file);
  1504. fputs (" : ", dump_file);
  1505. break;
  1506. case CAND_PHI:
  1507. fputs (" PHI : ", dump_file);
  1508. print_generic_expr (dump_file, c->base_expr, 0);
  1509. fputs (" + (unknown * ", dump_file);
  1510. print_generic_expr (dump_file, c->stride, 0);
  1511. fputs (") : ", dump_file);
  1512. break;
  1513. default:
  1514. gcc_unreachable ();
  1515. }
  1516. print_generic_expr (dump_file, c->cand_type, 0);
  1517. fprintf (dump_file, "\n basis: %d dependent: %d sibling: %d\n",
  1518. c->basis, c->dependent, c->sibling);
  1519. fprintf (dump_file, " next-interp: %d dead-savings: %d\n",
  1520. c->next_interp, c->dead_savings);
  1521. if (c->def_phi)
  1522. fprintf (dump_file, " phi: %d\n", c->def_phi);
  1523. fputs ("\n", dump_file);
  1524. }
  1525. /* Dump the candidate vector for debug. */
  1526. static void
  1527. dump_cand_vec (void)
  1528. {
  1529. unsigned i;
  1530. slsr_cand_t c;
  1531. fprintf (dump_file, "\nStrength reduction candidate vector:\n\n");
  1532. FOR_EACH_VEC_ELT (cand_vec, i, c)
  1533. dump_candidate (c);
  1534. }
  1535. /* Callback used to dump the candidate chains hash table. */
  1536. int
  1537. ssa_base_cand_dump_callback (cand_chain **slot, void *ignored ATTRIBUTE_UNUSED)
  1538. {
  1539. const_cand_chain_t chain = *slot;
  1540. cand_chain_t p;
  1541. print_generic_expr (dump_file, chain->base_expr, 0);
  1542. fprintf (dump_file, " -> %d", chain->cand->cand_num);
  1543. for (p = chain->next; p; p = p->next)
  1544. fprintf (dump_file, " -> %d", p->cand->cand_num);
  1545. fputs ("\n", dump_file);
  1546. return 1;
  1547. }
  1548. /* Dump the candidate chains. */
  1549. static void
  1550. dump_cand_chains (void)
  1551. {
  1552. fprintf (dump_file, "\nStrength reduction candidate chains:\n\n");
  1553. base_cand_map->traverse_noresize <void *, ssa_base_cand_dump_callback>
  1554. (NULL);
  1555. fputs ("\n", dump_file);
  1556. }
  1557. /* Dump the increment vector for debug. */
  1558. static void
  1559. dump_incr_vec (void)
  1560. {
  1561. if (dump_file && (dump_flags & TDF_DETAILS))
  1562. {
  1563. unsigned i;
  1564. fprintf (dump_file, "\nIncrement vector:\n\n");
  1565. for (i = 0; i < incr_vec_len; i++)
  1566. {
  1567. fprintf (dump_file, "%3d increment: ", i);
  1568. print_decs (incr_vec[i].incr, dump_file);
  1569. fprintf (dump_file, "\n count: %d", incr_vec[i].count);
  1570. fprintf (dump_file, "\n cost: %d", incr_vec[i].cost);
  1571. fputs ("\n initializer: ", dump_file);
  1572. print_generic_expr (dump_file, incr_vec[i].initializer, 0);
  1573. fputs ("\n\n", dump_file);
  1574. }
  1575. }
  1576. }
  1577. /* Replace *EXPR in candidate C with an equivalent strength-reduced
  1578. data reference. */
  1579. static void
  1580. replace_ref (tree *expr, slsr_cand_t c)
  1581. {
  1582. tree add_expr, mem_ref, acc_type = TREE_TYPE (*expr);
  1583. unsigned HOST_WIDE_INT misalign;
  1584. unsigned align;
  1585. /* Ensure the memory reference carries the minimum alignment
  1586. requirement for the data type. See PR58041. */
  1587. get_object_alignment_1 (*expr, &align, &misalign);
  1588. if (misalign != 0)
  1589. align = (misalign & -misalign);
  1590. if (align < TYPE_ALIGN (acc_type))
  1591. acc_type = build_aligned_type (acc_type, align);
  1592. add_expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (c->base_expr),
  1593. c->base_expr, c->stride);
  1594. mem_ref = fold_build2 (MEM_REF, acc_type, add_expr,
  1595. wide_int_to_tree (c->cand_type, c->index));
  1596. /* Gimplify the base addressing expression for the new MEM_REF tree. */
  1597. gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
  1598. TREE_OPERAND (mem_ref, 0)
  1599. = force_gimple_operand_gsi (&gsi, TREE_OPERAND (mem_ref, 0),
  1600. /*simple_p=*/true, NULL,
  1601. /*before=*/true, GSI_SAME_STMT);
  1602. copy_ref_info (mem_ref, *expr);
  1603. *expr = mem_ref;
  1604. update_stmt (c->cand_stmt);
  1605. }
  1606. /* Replace CAND_REF candidate C, each sibling of candidate C, and each
  1607. dependent of candidate C with an equivalent strength-reduced data
  1608. reference. */
  1609. static void
  1610. replace_refs (slsr_cand_t c)
  1611. {
  1612. if (dump_file && (dump_flags & TDF_DETAILS))
  1613. {
  1614. fputs ("Replacing reference: ", dump_file);
  1615. print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
  1616. }
  1617. if (gimple_vdef (c->cand_stmt))
  1618. {
  1619. tree *lhs = gimple_assign_lhs_ptr (c->cand_stmt);
  1620. replace_ref (lhs, c);
  1621. }
  1622. else
  1623. {
  1624. tree *rhs = gimple_assign_rhs1_ptr (c->cand_stmt);
  1625. replace_ref (rhs, c);
  1626. }
  1627. if (dump_file && (dump_flags & TDF_DETAILS))
  1628. {
  1629. fputs ("With: ", dump_file);
  1630. print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
  1631. fputs ("\n", dump_file);
  1632. }
  1633. if (c->sibling)
  1634. replace_refs (lookup_cand (c->sibling));
  1635. if (c->dependent)
  1636. replace_refs (lookup_cand (c->dependent));
  1637. }
  1638. /* Return TRUE if candidate C is dependent upon a PHI. */
  1639. static bool
  1640. phi_dependent_cand_p (slsr_cand_t c)
  1641. {
  1642. /* A candidate is not necessarily dependent upon a PHI just because
  1643. it has a phi definition for its base name. It may have a basis
  1644. that relies upon the same phi definition, in which case the PHI
  1645. is irrelevant to this candidate. */
  1646. return (c->def_phi
  1647. && c->basis
  1648. && lookup_cand (c->basis)->def_phi != c->def_phi);
  1649. }
  1650. /* Calculate the increment required for candidate C relative to
  1651. its basis. */
  1652. static widest_int
  1653. cand_increment (slsr_cand_t c)
  1654. {
  1655. slsr_cand_t basis;
  1656. /* If the candidate doesn't have a basis, just return its own
  1657. index. This is useful in record_increments to help us find
  1658. an existing initializer. Also, if the candidate's basis is
  1659. hidden by a phi, then its own index will be the increment
  1660. from the newly introduced phi basis. */
  1661. if (!c->basis || phi_dependent_cand_p (c))
  1662. return c->index;
  1663. basis = lookup_cand (c->basis);
  1664. gcc_assert (operand_equal_p (c->base_expr, basis->base_expr, 0));
  1665. return c->index - basis->index;
  1666. }
  1667. /* Calculate the increment required for candidate C relative to
  1668. its basis. If we aren't going to generate pointer arithmetic
  1669. for this candidate, return the absolute value of that increment
  1670. instead. */
  1671. static inline widest_int
  1672. cand_abs_increment (slsr_cand_t c)
  1673. {
  1674. widest_int increment = cand_increment (c);
  1675. if (!address_arithmetic_p && wi::neg_p (increment))
  1676. increment = -increment;
  1677. return increment;
  1678. }
  1679. /* Return TRUE iff candidate C has already been replaced under
  1680. another interpretation. */
  1681. static inline bool
  1682. cand_already_replaced (slsr_cand_t c)
  1683. {
  1684. return (gimple_bb (c->cand_stmt) == 0);
  1685. }
  1686. /* Common logic used by replace_unconditional_candidate and
  1687. replace_conditional_candidate. */
  1688. static void
  1689. replace_mult_candidate (slsr_cand_t c, tree basis_name, widest_int bump)
  1690. {
  1691. tree target_type = TREE_TYPE (gimple_assign_lhs (c->cand_stmt));
  1692. enum tree_code cand_code = gimple_assign_rhs_code (c->cand_stmt);
  1693. /* It is highly unlikely, but possible, that the resulting
  1694. bump doesn't fit in a HWI. Abandon the replacement
  1695. in this case. This does not affect siblings or dependents
  1696. of C. Restriction to signed HWI is conservative for unsigned
  1697. types but allows for safe negation without twisted logic. */
  1698. if (wi::fits_shwi_p (bump)
  1699. && bump.to_shwi () != HOST_WIDE_INT_MIN
  1700. /* It is not useful to replace casts, copies, or adds of
  1701. an SSA name and a constant. */
  1702. && cand_code != MODIFY_EXPR
  1703. && !CONVERT_EXPR_CODE_P (cand_code)
  1704. && cand_code != PLUS_EXPR
  1705. && cand_code != POINTER_PLUS_EXPR
  1706. && cand_code != MINUS_EXPR)
  1707. {
  1708. enum tree_code code = PLUS_EXPR;
  1709. tree bump_tree;
  1710. gimple stmt_to_print = NULL;
  1711. /* If the basis name and the candidate's LHS have incompatible
  1712. types, introduce a cast. */
  1713. if (!useless_type_conversion_p (target_type, TREE_TYPE (basis_name)))
  1714. basis_name = introduce_cast_before_cand (c, target_type, basis_name);
  1715. if (wi::neg_p (bump))
  1716. {
  1717. code = MINUS_EXPR;
  1718. bump = -bump;
  1719. }
  1720. bump_tree = wide_int_to_tree (target_type, bump);
  1721. if (dump_file && (dump_flags & TDF_DETAILS))
  1722. {
  1723. fputs ("Replacing: ", dump_file);
  1724. print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
  1725. }
  1726. if (bump == 0)
  1727. {
  1728. tree lhs = gimple_assign_lhs (c->cand_stmt);
  1729. gassign *copy_stmt = gimple_build_assign (lhs, basis_name);
  1730. gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
  1731. gimple_set_location (copy_stmt, gimple_location (c->cand_stmt));
  1732. gsi_replace (&gsi, copy_stmt, false);
  1733. c->cand_stmt = copy_stmt;
  1734. if (dump_file && (dump_flags & TDF_DETAILS))
  1735. stmt_to_print = copy_stmt;
  1736. }
  1737. else
  1738. {
  1739. tree rhs1, rhs2;
  1740. if (cand_code != NEGATE_EXPR) {
  1741. rhs1 = gimple_assign_rhs1 (c->cand_stmt);
  1742. rhs2 = gimple_assign_rhs2 (c->cand_stmt);
  1743. }
  1744. if (cand_code != NEGATE_EXPR
  1745. && ((operand_equal_p (rhs1, basis_name, 0)
  1746. && operand_equal_p (rhs2, bump_tree, 0))
  1747. || (operand_equal_p (rhs1, bump_tree, 0)
  1748. && operand_equal_p (rhs2, basis_name, 0))))
  1749. {
  1750. if (dump_file && (dump_flags & TDF_DETAILS))
  1751. {
  1752. fputs ("(duplicate, not actually replacing)", dump_file);
  1753. stmt_to_print = c->cand_stmt;
  1754. }
  1755. }
  1756. else
  1757. {
  1758. gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
  1759. gimple_assign_set_rhs_with_ops (&gsi, code,
  1760. basis_name, bump_tree);
  1761. update_stmt (gsi_stmt (gsi));
  1762. c->cand_stmt = gsi_stmt (gsi);
  1763. if (dump_file && (dump_flags & TDF_DETAILS))
  1764. stmt_to_print = gsi_stmt (gsi);
  1765. }
  1766. }
  1767. if (dump_file && (dump_flags & TDF_DETAILS))
  1768. {
  1769. fputs ("With: ", dump_file);
  1770. print_gimple_stmt (dump_file, stmt_to_print, 0, 0);
  1771. fputs ("\n", dump_file);
  1772. }
  1773. }
  1774. }
  1775. /* Replace candidate C with an add or subtract. Note that we only
  1776. operate on CAND_MULTs with known strides, so we will never generate
  1777. a POINTER_PLUS_EXPR. Each candidate X = (B + i) * S is replaced by
  1778. X = Y + ((i - i') * S), as described in the module commentary. The
  1779. folded value ((i - i') * S) is referred to here as the "bump." */
  1780. static void
  1781. replace_unconditional_candidate (slsr_cand_t c)
  1782. {
  1783. slsr_cand_t basis;
  1784. if (cand_already_replaced (c))
  1785. return;
  1786. basis = lookup_cand (c->basis);
  1787. widest_int bump = cand_increment (c) * wi::to_widest (c->stride);
  1788. replace_mult_candidate (c, gimple_assign_lhs (basis->cand_stmt), bump);
  1789. }
  1790. /* Return the index in the increment vector of the given INCREMENT,
  1791. or -1 if not found. The latter can occur if more than
  1792. MAX_INCR_VEC_LEN increments have been found. */
  1793. static inline int
  1794. incr_vec_index (const widest_int &increment)
  1795. {
  1796. unsigned i;
  1797. for (i = 0; i < incr_vec_len && increment != incr_vec[i].incr; i++)
  1798. ;
  1799. if (i < incr_vec_len)
  1800. return i;
  1801. else
  1802. return -1;
  1803. }
  1804. /* Create a new statement along edge E to add BASIS_NAME to the product
  1805. of INCREMENT and the stride of candidate C. Create and return a new
  1806. SSA name from *VAR to be used as the LHS of the new statement.
  1807. KNOWN_STRIDE is true iff C's stride is a constant. */
  1808. static tree
  1809. create_add_on_incoming_edge (slsr_cand_t c, tree basis_name,
  1810. widest_int increment, edge e, location_t loc,
  1811. bool known_stride)
  1812. {
  1813. basic_block insert_bb;
  1814. gimple_stmt_iterator gsi;
  1815. tree lhs, basis_type;
  1816. gassign *new_stmt;
  1817. /* If the add candidate along this incoming edge has the same
  1818. index as C's hidden basis, the hidden basis represents this
  1819. edge correctly. */
  1820. if (increment == 0)
  1821. return basis_name;
  1822. basis_type = TREE_TYPE (basis_name);
  1823. lhs = make_temp_ssa_name (basis_type, NULL, "slsr");
  1824. if (known_stride)
  1825. {
  1826. tree bump_tree;
  1827. enum tree_code code = PLUS_EXPR;
  1828. widest_int bump = increment * wi::to_widest (c->stride);
  1829. if (wi::neg_p (bump))
  1830. {
  1831. code = MINUS_EXPR;
  1832. bump = -bump;
  1833. }
  1834. bump_tree = wide_int_to_tree (basis_type, bump);
  1835. new_stmt = gimple_build_assign (lhs, code, basis_name, bump_tree);
  1836. }
  1837. else
  1838. {
  1839. int i;
  1840. bool negate_incr = (!address_arithmetic_p && wi::neg_p (increment));
  1841. i = incr_vec_index (negate_incr ? -increment : increment);
  1842. gcc_assert (i >= 0);
  1843. if (incr_vec[i].initializer)
  1844. {
  1845. enum tree_code code = negate_incr ? MINUS_EXPR : PLUS_EXPR;
  1846. new_stmt = gimple_build_assign (lhs, code, basis_name,
  1847. incr_vec[i].initializer);
  1848. }
  1849. else if (increment == 1)
  1850. new_stmt = gimple_build_assign (lhs, PLUS_EXPR, basis_name, c->stride);
  1851. else if (increment == -1)
  1852. new_stmt = gimple_build_assign (lhs, MINUS_EXPR, basis_name,
  1853. c->stride);
  1854. else
  1855. gcc_unreachable ();
  1856. }
  1857. insert_bb = single_succ_p (e->src) ? e->src : split_edge (e);
  1858. gsi = gsi_last_bb (insert_bb);
  1859. if (!gsi_end_p (gsi) && is_ctrl_stmt (gsi_stmt (gsi)))
  1860. gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
  1861. else
  1862. gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
  1863. gimple_set_location (new_stmt, loc);
  1864. if (dump_file && (dump_flags & TDF_DETAILS))
  1865. {
  1866. fprintf (dump_file, "Inserting in block %d: ", insert_bb->index);
  1867. print_gimple_stmt (dump_file, new_stmt, 0, 0);
  1868. }
  1869. return lhs;
  1870. }
  1871. /* Given a candidate C with BASIS_NAME being the LHS of C's basis which
  1872. is hidden by the phi node FROM_PHI, create a new phi node in the same
  1873. block as FROM_PHI. The new phi is suitable for use as a basis by C,
  1874. with its phi arguments representing conditional adjustments to the
  1875. hidden basis along conditional incoming paths. Those adjustments are
  1876. made by creating add statements (and sometimes recursively creating
  1877. phis) along those incoming paths. LOC is the location to attach to
  1878. the introduced statements. KNOWN_STRIDE is true iff C's stride is a
  1879. constant. */
  1880. static tree
  1881. create_phi_basis (slsr_cand_t c, gimple from_phi, tree basis_name,
  1882. location_t loc, bool known_stride)
  1883. {
  1884. int i;
  1885. tree name, phi_arg;
  1886. gphi *phi;
  1887. vec<tree> phi_args;
  1888. slsr_cand_t basis = lookup_cand (c->basis);
  1889. int nargs = gimple_phi_num_args (from_phi);
  1890. basic_block phi_bb = gimple_bb (from_phi);
  1891. slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (from_phi));
  1892. phi_args.create (nargs);
  1893. /* Process each argument of the existing phi that represents
  1894. conditionally-executed add candidates. */
  1895. for (i = 0; i < nargs; i++)
  1896. {
  1897. edge e = (*phi_bb->preds)[i];
  1898. tree arg = gimple_phi_arg_def (from_phi, i);
  1899. tree feeding_def;
  1900. /* If the phi argument is the base name of the CAND_PHI, then
  1901. this incoming arc should use the hidden basis. */
  1902. if (operand_equal_p (arg, phi_cand->base_expr, 0))
  1903. if (basis->index == 0)
  1904. feeding_def = gimple_assign_lhs (basis->cand_stmt);
  1905. else
  1906. {
  1907. widest_int incr = -basis->index;
  1908. feeding_def = create_add_on_incoming_edge (c, basis_name, incr,
  1909. e, loc, known_stride);
  1910. }
  1911. else
  1912. {
  1913. gimple arg_def = SSA_NAME_DEF_STMT (arg);
  1914. /* If there is another phi along this incoming edge, we must
  1915. process it in the same fashion to ensure that all basis
  1916. adjustments are made along its incoming edges. */
  1917. if (gimple_code (arg_def) == GIMPLE_PHI)
  1918. feeding_def = create_phi_basis (c, arg_def, basis_name,
  1919. loc, known_stride);
  1920. else
  1921. {
  1922. slsr_cand_t arg_cand = base_cand_from_table (arg);
  1923. widest_int diff = arg_cand->index - basis->index;
  1924. feeding_def = create_add_on_incoming_edge (c, basis_name, diff,
  1925. e, loc, known_stride);
  1926. }
  1927. }
  1928. /* Because of recursion, we need to save the arguments in a vector
  1929. so we can create the PHI statement all at once. Otherwise the
  1930. storage for the half-created PHI can be reclaimed. */
  1931. phi_args.safe_push (feeding_def);
  1932. }
  1933. /* Create the new phi basis. */
  1934. name = make_temp_ssa_name (TREE_TYPE (basis_name), NULL, "slsr");
  1935. phi = create_phi_node (name, phi_bb);
  1936. SSA_NAME_DEF_STMT (name) = phi;
  1937. FOR_EACH_VEC_ELT (phi_args, i, phi_arg)
  1938. {
  1939. edge e = (*phi_bb->preds)[i];
  1940. add_phi_arg (phi, phi_arg, e, loc);
  1941. }
  1942. update_stmt (phi);
  1943. if (dump_file && (dump_flags & TDF_DETAILS))
  1944. {
  1945. fputs ("Introducing new phi basis: ", dump_file);
  1946. print_gimple_stmt (dump_file, phi, 0, 0);
  1947. }
  1948. return name;
  1949. }
  1950. /* Given a candidate C whose basis is hidden by at least one intervening
  1951. phi, introduce a matching number of new phis to represent its basis
  1952. adjusted by conditional increments along possible incoming paths. Then
  1953. replace C as though it were an unconditional candidate, using the new
  1954. basis. */
  1955. static void
  1956. replace_conditional_candidate (slsr_cand_t c)
  1957. {
  1958. tree basis_name, name;
  1959. slsr_cand_t basis;
  1960. location_t loc;
  1961. /* Look up the LHS SSA name from C's basis. This will be the
  1962. RHS1 of the adds we will introduce to create new phi arguments. */
  1963. basis = lookup_cand (c->basis);
  1964. basis_name = gimple_assign_lhs (basis->cand_stmt);
  1965. /* Create a new phi statement which will represent C's true basis
  1966. after the transformation is complete. */
  1967. loc = gimple_location (c->cand_stmt);
  1968. name = create_phi_basis (c, lookup_cand (c->def_phi)->cand_stmt,
  1969. basis_name, loc, KNOWN_STRIDE);
  1970. /* Replace C with an add of the new basis phi and a constant. */
  1971. widest_int bump = c->index * wi::to_widest (c->stride);
  1972. replace_mult_candidate (c, name, bump);
  1973. }
  1974. /* Compute the expected costs of inserting basis adjustments for
  1975. candidate C with phi-definition PHI. The cost of inserting
  1976. one adjustment is given by ONE_ADD_COST. If PHI has arguments
  1977. which are themselves phi results, recursively calculate costs
  1978. for those phis as well. */
  1979. static int
  1980. phi_add_costs (gimple phi, slsr_cand_t c, int one_add_cost)
  1981. {
  1982. unsigned i;
  1983. int cost = 0;
  1984. slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
  1985. /* If we work our way back to a phi that isn't dominated by the hidden
  1986. basis, this isn't a candidate for replacement. Indicate this by
  1987. returning an unreasonably high cost. It's not easy to detect
  1988. these situations when determining the basis, so we defer the
  1989. decision until now. */
  1990. basic_block phi_bb = gimple_bb (phi);
  1991. slsr_cand_t basis = lookup_cand (c->basis);
  1992. basic_block basis_bb = gimple_bb (basis->cand_stmt);
  1993. if (phi_bb == basis_bb || !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb))
  1994. return COST_INFINITE;
  1995. for (i = 0; i < gimple_phi_num_args (phi); i++)
  1996. {
  1997. tree arg = gimple_phi_arg_def (phi, i);
  1998. if (arg != phi_cand->base_expr)
  1999. {
  2000. gimple arg_def = SSA_NAME_DEF_STMT (arg);
  2001. if (gimple_code (arg_def) == GIMPLE_PHI)
  2002. cost += phi_add_costs (arg_def, c, one_add_cost);
  2003. else
  2004. {
  2005. slsr_cand_t arg_cand = base_cand_from_table (arg);
  2006. if (arg_cand->index != c->index)
  2007. cost += one_add_cost;
  2008. }
  2009. }
  2010. }
  2011. return cost;
  2012. }
  2013. /* For candidate C, each sibling of candidate C, and each dependent of
  2014. candidate C, determine whether the candidate is dependent upon a
  2015. phi that hides its basis. If not, replace the candidate unconditionally.
  2016. Otherwise, determine whether the cost of introducing compensation code
  2017. for the candidate is offset by the gains from strength reduction. If
  2018. so, replace the candidate and introduce the compensation code. */
  2019. static void
  2020. replace_uncond_cands_and_profitable_phis (slsr_cand_t c)
  2021. {
  2022. if (phi_dependent_cand_p (c))
  2023. {
  2024. if (c->kind == CAND_MULT)
  2025. {
  2026. /* A candidate dependent upon a phi will replace a multiply by
  2027. a constant with an add, and will insert at most one add for
  2028. each phi argument. Add these costs with the potential dead-code
  2029. savings to determine profitability. */
  2030. bool speed = optimize_bb_for_speed_p (gimple_bb (c->cand_stmt));
  2031. int mult_savings = stmt_cost (c->cand_stmt, speed);
  2032. gimple phi = lookup_cand (c->def_phi)->cand_stmt;
  2033. tree phi_result = gimple_phi_result (phi);
  2034. int one_add_cost = add_cost (speed,
  2035. TYPE_MODE (TREE_TYPE (phi_result)));
  2036. int add_costs = one_add_cost + phi_add_costs (phi, c, one_add_cost);
  2037. int cost = add_costs - mult_savings - c->dead_savings;
  2038. if (dump_file && (dump_flags & TDF_DETAILS))
  2039. {
  2040. fprintf (dump_file, " Conditional candidate %d:\n", c->cand_num);
  2041. fprintf (dump_file, " add_costs = %d\n", add_costs);
  2042. fprintf (dump_file, " mult_savings = %d\n", mult_savings);
  2043. fprintf (dump_file, " dead_savings = %d\n", c->dead_savings);
  2044. fprintf (dump_file, " cost = %d\n", cost);
  2045. if (cost <= COST_NEUTRAL)
  2046. fputs (" Replacing...\n", dump_file);
  2047. else
  2048. fputs (" Not replaced.\n", dump_file);
  2049. }
  2050. if (cost <= COST_NEUTRAL)
  2051. replace_conditional_candidate (c);
  2052. }
  2053. }
  2054. else
  2055. replace_unconditional_candidate (c);
  2056. if (c->sibling)
  2057. replace_uncond_cands_and_profitable_phis (lookup_cand (c->sibling));
  2058. if (c->dependent)
  2059. replace_uncond_cands_and_profitable_phis (lookup_cand (c->dependent));
  2060. }
  2061. /* Count the number of candidates in the tree rooted at C that have
  2062. not already been replaced under other interpretations. */
  2063. static int
  2064. count_candidates (slsr_cand_t c)
  2065. {
  2066. unsigned count = cand_already_replaced (c) ? 0 : 1;
  2067. if (c->sibling)
  2068. count += count_candidates (lookup_cand (c->sibling));
  2069. if (c->dependent)
  2070. count += count_candidates (lookup_cand (c->dependent));
  2071. return count;
  2072. }
  2073. /* Increase the count of INCREMENT by one in the increment vector.
  2074. INCREMENT is associated with candidate C. If INCREMENT is to be
  2075. conditionally executed as part of a conditional candidate replacement,
  2076. IS_PHI_ADJUST is true, otherwise false. If an initializer
  2077. T_0 = stride * I is provided by a candidate that dominates all
  2078. candidates with the same increment, also record T_0 for subsequent use. */
  2079. static void
  2080. record_increment (slsr_cand_t c, widest_int increment, bool is_phi_adjust)
  2081. {
  2082. bool found = false;
  2083. unsigned i;
  2084. /* Treat increments that differ only in sign as identical so as to
  2085. share initializers, unless we are generating pointer arithmetic. */
  2086. if (!address_arithmetic_p && wi::neg_p (increment))
  2087. increment = -increment;
  2088. for (i = 0; i < incr_vec_len; i++)
  2089. {
  2090. if (incr_vec[i].incr == increment)
  2091. {
  2092. incr_vec[i].count++;
  2093. found = true;
  2094. /* If we previously recorded an initializer that doesn't
  2095. dominate this candidate, it's not going to be useful to
  2096. us after all. */
  2097. if (incr_vec[i].initializer
  2098. && !dominated_by_p (CDI_DOMINATORS,
  2099. gimple_bb (c->cand_stmt),
  2100. incr_vec[i].init_bb))
  2101. {
  2102. incr_vec[i].initializer = NULL_TREE;
  2103. incr_vec[i].init_bb = NULL;
  2104. }
  2105. break;
  2106. }
  2107. }
  2108. if (!found && incr_vec_len < MAX_INCR_VEC_LEN - 1)
  2109. {
  2110. /* The first time we see an increment, create the entry for it.
  2111. If this is the root candidate which doesn't have a basis, set
  2112. the count to zero. We're only processing it so it can possibly
  2113. provide an initializer for other candidates. */
  2114. incr_vec[incr_vec_len].incr = increment;
  2115. incr_vec[incr_vec_len].count = c->basis || is_phi_adjust ? 1 : 0;
  2116. incr_vec[incr_vec_len].cost = COST_INFINITE;
  2117. /* Optimistically record the first occurrence of this increment
  2118. as providing an initializer (if it does); we will revise this
  2119. opinion later if it doesn't dominate all other occurrences.
  2120. Exception: increments of -1, 0, 1 never need initializers;
  2121. and phi adjustments don't ever provide initializers. */
  2122. if (c->kind == CAND_ADD
  2123. && !is_phi_adjust
  2124. && c->index == increment
  2125. && (wi::gts_p (increment, 1)
  2126. || wi::lts_p (increment, -1))
  2127. && (gimple_assign_rhs_code (c->cand_stmt) == PLUS_EXPR
  2128. || gimple_assign_rhs_code (c->cand_stmt) == POINTER_PLUS_EXPR))
  2129. {
  2130. tree t0 = NULL_TREE;
  2131. tree rhs1 = gimple_assign_rhs1 (c->cand_stmt);
  2132. tree rhs2 = gimple_assign_rhs2 (c->cand_stmt);
  2133. if (operand_equal_p (rhs1, c->base_expr, 0))
  2134. t0 = rhs2;
  2135. else if (operand_equal_p (rhs2, c->base_expr, 0))
  2136. t0 = rhs1;
  2137. if (t0
  2138. && SSA_NAME_DEF_STMT (t0)
  2139. && gimple_bb (SSA_NAME_DEF_STMT (t0)))
  2140. {
  2141. incr_vec[incr_vec_len].initializer = t0;
  2142. incr_vec[incr_vec_len++].init_bb
  2143. = gimple_bb (SSA_NAME_DEF_STMT (t0));
  2144. }
  2145. else
  2146. {
  2147. incr_vec[incr_vec_len].initializer = NULL_TREE;
  2148. incr_vec[incr_vec_len++].init_bb = NULL;
  2149. }
  2150. }
  2151. else
  2152. {
  2153. incr_vec[incr_vec_len].initializer = NULL_TREE;
  2154. incr_vec[incr_vec_len++].init_bb = NULL;
  2155. }
  2156. }
  2157. }
  2158. /* Given phi statement PHI that hides a candidate from its BASIS, find
  2159. the increments along each incoming arc (recursively handling additional
  2160. phis that may be present) and record them. These increments are the
  2161. difference in index between the index-adjusting statements and the
  2162. index of the basis. */
  2163. static void
  2164. record_phi_increments (slsr_cand_t basis, gimple phi)
  2165. {
  2166. unsigned i;
  2167. slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
  2168. for (i = 0; i < gimple_phi_num_args (phi); i++)
  2169. {
  2170. tree arg = gimple_phi_arg_def (phi, i);
  2171. if (!operand_equal_p (arg, phi_cand->base_expr, 0))
  2172. {
  2173. gimple arg_def = SSA_NAME_DEF_STMT (arg);
  2174. if (gimple_code (arg_def) == GIMPLE_PHI)
  2175. record_phi_increments (basis, arg_def);
  2176. else
  2177. {
  2178. slsr_cand_t arg_cand = base_cand_from_table (arg);
  2179. widest_int diff = arg_cand->index - basis->index;
  2180. record_increment (arg_cand, diff, PHI_ADJUST);
  2181. }
  2182. }
  2183. }
  2184. }
  2185. /* Determine how many times each unique increment occurs in the set
  2186. of candidates rooted at C's parent, recording the data in the
  2187. increment vector. For each unique increment I, if an initializer
  2188. T_0 = stride * I is provided by a candidate that dominates all
  2189. candidates with the same increment, also record T_0 for subsequent
  2190. use. */
  2191. static void
  2192. record_increments (slsr_cand_t c)
  2193. {
  2194. if (!cand_already_replaced (c))
  2195. {
  2196. if (!phi_dependent_cand_p (c))
  2197. record_increment (c, cand_increment (c), NOT_PHI_ADJUST);
  2198. else
  2199. {
  2200. /* A candidate with a basis hidden by a phi will have one
  2201. increment for its relationship to the index represented by
  2202. the phi, and potentially additional increments along each
  2203. incoming edge. For the root of the dependency tree (which
  2204. has no basis), process just the initial index in case it has
  2205. an initializer that can be used by subsequent candidates. */
  2206. record_increment (c, c->index, NOT_PHI_ADJUST);
  2207. if (c->basis)
  2208. record_phi_increments (lookup_cand (c->basis),
  2209. lookup_cand (c->def_phi)->cand_stmt);
  2210. }
  2211. }
  2212. if (c->sibling)
  2213. record_increments (lookup_cand (c->sibling));
  2214. if (c->dependent)
  2215. record_increments (lookup_cand (c->dependent));
  2216. }
  2217. /* Add up and return the costs of introducing add statements that
  2218. require the increment INCR on behalf of candidate C and phi
  2219. statement PHI. Accumulate into *SAVINGS the potential savings
  2220. from removing existing statements that feed PHI and have no other
  2221. uses. */
  2222. static int
  2223. phi_incr_cost (slsr_cand_t c, const widest_int &incr, gimple phi, int *savings)
  2224. {
  2225. unsigned i;
  2226. int cost = 0;
  2227. slsr_cand_t basis = lookup_cand (c->basis);
  2228. slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
  2229. for (i = 0; i < gimple_phi_num_args (phi); i++)
  2230. {
  2231. tree arg = gimple_phi_arg_def (phi, i);
  2232. if (!operand_equal_p (arg, phi_cand->base_expr, 0))
  2233. {
  2234. gimple arg_def = SSA_NAME_DEF_STMT (arg);
  2235. if (gimple_code (arg_def) == GIMPLE_PHI)
  2236. {
  2237. int feeding_savings = 0;
  2238. cost += phi_incr_cost (c, incr, arg_def, &feeding_savings);
  2239. if (has_single_use (gimple_phi_result (arg_def)))
  2240. *savings += feeding_savings;
  2241. }
  2242. else
  2243. {
  2244. slsr_cand_t arg_cand = base_cand_from_table (arg);
  2245. widest_int diff = arg_cand->index - basis->index;
  2246. if (incr == diff)
  2247. {
  2248. tree basis_lhs = gimple_assign_lhs (basis->cand_stmt);
  2249. tree lhs = gimple_assign_lhs (arg_cand->cand_stmt);
  2250. cost += add_cost (true, TYPE_MODE (TREE_TYPE (basis_lhs)));
  2251. if (has_single_use (lhs))
  2252. *savings += stmt_cost (arg_cand->cand_stmt, true);
  2253. }
  2254. }
  2255. }
  2256. }
  2257. return cost;
  2258. }
  2259. /* Return the first candidate in the tree rooted at C that has not
  2260. already been replaced, favoring siblings over dependents. */
  2261. static slsr_cand_t
  2262. unreplaced_cand_in_tree (slsr_cand_t c)
  2263. {
  2264. if (!cand_already_replaced (c))
  2265. return c;
  2266. if (c->sibling)
  2267. {
  2268. slsr_cand_t sib = unreplaced_cand_in_tree (lookup_cand (c->sibling));
  2269. if (sib)
  2270. return sib;
  2271. }
  2272. if (c->dependent)
  2273. {
  2274. slsr_cand_t dep = unreplaced_cand_in_tree (lookup_cand (c->dependent));
  2275. if (dep)
  2276. return dep;
  2277. }
  2278. return NULL;
  2279. }
  2280. /* Return TRUE if the candidates in the tree rooted at C should be
  2281. optimized for speed, else FALSE. We estimate this based on the block
  2282. containing the most dominant candidate in the tree that has not yet
  2283. been replaced. */
  2284. static bool
  2285. optimize_cands_for_speed_p (slsr_cand_t c)
  2286. {
  2287. slsr_cand_t c2 = unreplaced_cand_in_tree (c);
  2288. gcc_assert (c2);
  2289. return optimize_bb_for_speed_p (gimple_bb (c2->cand_stmt));
  2290. }
  2291. /* Add COST_IN to the lowest cost of any dependent path starting at
  2292. candidate C or any of its siblings, counting only candidates along
  2293. such paths with increment INCR. Assume that replacing a candidate
  2294. reduces cost by REPL_SAVINGS. Also account for savings from any
  2295. statements that would go dead. If COUNT_PHIS is true, include
  2296. costs of introducing feeding statements for conditional candidates. */
  2297. static int
  2298. lowest_cost_path (int cost_in, int repl_savings, slsr_cand_t c,
  2299. const widest_int &incr, bool count_phis)
  2300. {
  2301. int local_cost, sib_cost, savings = 0;
  2302. widest_int cand_incr = cand_abs_increment (c);
  2303. if (cand_already_replaced (c))
  2304. local_cost = cost_in;
  2305. else if (incr == cand_incr)
  2306. local_cost = cost_in - repl_savings - c->dead_savings;
  2307. else
  2308. local_cost = cost_in - c->dead_savings;
  2309. if (count_phis
  2310. && phi_dependent_cand_p (c)
  2311. && !cand_already_replaced (c))
  2312. {
  2313. gimple phi = lookup_cand (c->def_phi)->cand_stmt;
  2314. local_cost += phi_incr_cost (c, incr, phi, &savings);
  2315. if (has_single_use (gimple_phi_result (phi)))
  2316. local_cost -= savings;
  2317. }
  2318. if (c->dependent)
  2319. local_cost = lowest_cost_path (local_cost, repl_savings,
  2320. lookup_cand (c->dependent), incr,
  2321. count_phis);
  2322. if (c->sibling)
  2323. {
  2324. sib_cost = lowest_cost_path (cost_in, repl_savings,
  2325. lookup_cand (c->sibling), incr,
  2326. count_phis);
  2327. local_cost = MIN (local_cost, sib_cost);
  2328. }
  2329. return local_cost;
  2330. }
  2331. /* Compute the total savings that would accrue from all replacements
  2332. in the candidate tree rooted at C, counting only candidates with
  2333. increment INCR. Assume that replacing a candidate reduces cost
  2334. by REPL_SAVINGS. Also account for savings from statements that
  2335. would go dead. */
  2336. static int
  2337. total_savings (int repl_savings, slsr_cand_t c, const widest_int &incr,
  2338. bool count_phis)
  2339. {
  2340. int savings = 0;
  2341. widest_int cand_incr = cand_abs_increment (c);
  2342. if (incr == cand_incr && !cand_already_replaced (c))
  2343. savings += repl_savings + c->dead_savings;
  2344. if (count_phis
  2345. && phi_dependent_cand_p (c)
  2346. && !cand_already_replaced (c))
  2347. {
  2348. int phi_savings = 0;
  2349. gimple phi = lookup_cand (c->def_phi)->cand_stmt;
  2350. savings -= phi_incr_cost (c, incr, phi, &phi_savings);
  2351. if (has_single_use (gimple_phi_result (phi)))
  2352. savings += phi_savings;
  2353. }
  2354. if (c->dependent)
  2355. savings += total_savings (repl_savings, lookup_cand (c->dependent), incr,
  2356. count_phis);
  2357. if (c->sibling)
  2358. savings += total_savings (repl_savings, lookup_cand (c->sibling), incr,
  2359. count_phis);
  2360. return savings;
  2361. }
  2362. /* Use target-specific costs to determine and record which increments
  2363. in the current candidate tree are profitable to replace, assuming
  2364. MODE and SPEED. FIRST_DEP is the first dependent of the root of
  2365. the candidate tree.
  2366. One slight limitation here is that we don't account for the possible
  2367. introduction of casts in some cases. See replace_one_candidate for
  2368. the cases where these are introduced. This should probably be cleaned
  2369. up sometime. */
  2370. static void
  2371. analyze_increments (slsr_cand_t first_dep, machine_mode mode, bool speed)
  2372. {
  2373. unsigned i;
  2374. for (i = 0; i < incr_vec_len; i++)
  2375. {
  2376. HOST_WIDE_INT incr = incr_vec[i].incr.to_shwi ();
  2377. /* If somehow this increment is bigger than a HWI, we won't
  2378. be optimizing candidates that use it. And if the increment
  2379. has a count of zero, nothing will be done with it. */
  2380. if (!wi::fits_shwi_p (incr_vec[i].incr) || !incr_vec[i].count)
  2381. incr_vec[i].cost = COST_INFINITE;
  2382. /* Increments of 0, 1, and -1 are always profitable to replace,
  2383. because they always replace a multiply or add with an add or
  2384. copy, and may cause one or more existing instructions to go
  2385. dead. Exception: -1 can't be assumed to be profitable for
  2386. pointer addition. */
  2387. else if (incr == 0
  2388. || incr == 1
  2389. || (incr == -1
  2390. && (gimple_assign_rhs_code (first_dep->cand_stmt)
  2391. != POINTER_PLUS_EXPR)))
  2392. incr_vec[i].cost = COST_NEUTRAL;
  2393. /* FORNOW: If we need to add an initializer, give up if a cast from
  2394. the candidate's type to its stride's type can lose precision.
  2395. This could eventually be handled better by expressly retaining the
  2396. result of a cast to a wider type in the stride. Example:
  2397. short int _1;
  2398. _2 = (int) _1;
  2399. _3 = _2 * 10;
  2400. _4 = x + _3; ADD: x + (10 * _1) : int
  2401. _5 = _2 * 15;
  2402. _6 = x + _3; ADD: x + (15 * _1) : int
  2403. Right now replacing _6 would cause insertion of an initializer
  2404. of the form "short int T = _1 * 5;" followed by a cast to
  2405. int, which could overflow incorrectly. Had we recorded _2 or
  2406. (int)_1 as the stride, this wouldn't happen. However, doing
  2407. this breaks other opportunities, so this will require some
  2408. care. */
  2409. else if (!incr_vec[i].initializer
  2410. && TREE_CODE (first_dep->stride) != INTEGER_CST
  2411. && !legal_cast_p_1 (first_dep->stride,
  2412. gimple_assign_lhs (first_dep->cand_stmt)))
  2413. incr_vec[i].cost = COST_INFINITE;
  2414. /* If we need to add an initializer, make sure we don't introduce
  2415. a multiply by a pointer type, which can happen in certain cast
  2416. scenarios. FIXME: When cleaning up these cast issues, we can
  2417. afford to introduce the multiply provided we cast out to an
  2418. unsigned int of appropriate size. */
  2419. else if (!incr_vec[i].initializer
  2420. && TREE_CODE (first_dep->stride) != INTEGER_CST
  2421. && POINTER_TYPE_P (TREE_TYPE (first_dep->stride)))
  2422. incr_vec[i].cost = COST_INFINITE;
  2423. /* For any other increment, if this is a multiply candidate, we
  2424. must introduce a temporary T and initialize it with
  2425. T_0 = stride * increment. When optimizing for speed, walk the
  2426. candidate tree to calculate the best cost reduction along any
  2427. path; if it offsets the fixed cost of inserting the initializer,
  2428. replacing the increment is profitable. When optimizing for
  2429. size, instead calculate the total cost reduction from replacing
  2430. all candidates with this increment. */
  2431. else if (first_dep->kind == CAND_MULT)
  2432. {
  2433. int cost = mult_by_coeff_cost (incr, mode, speed);
  2434. int repl_savings = mul_cost (speed, mode) - add_cost (speed, mode);
  2435. if (speed)
  2436. cost = lowest_cost_path (cost, repl_savings, first_dep,
  2437. incr_vec[i].incr, COUNT_PHIS);
  2438. else
  2439. cost -= total_savings (repl_savings, first_dep, incr_vec[i].incr,
  2440. COUNT_PHIS);
  2441. incr_vec[i].cost = cost;
  2442. }
  2443. /* If this is an add candidate, the initializer may already
  2444. exist, so only calculate the cost of the initializer if it
  2445. doesn't. We are replacing one add with another here, so the
  2446. known replacement savings is zero. We will account for removal
  2447. of dead instructions in lowest_cost_path or total_savings. */
  2448. else
  2449. {
  2450. int cost = 0;
  2451. if (!incr_vec[i].initializer)
  2452. cost = mult_by_coeff_cost (incr, mode, speed);
  2453. if (speed)
  2454. cost = lowest_cost_path (cost, 0, first_dep, incr_vec[i].incr,
  2455. DONT_COUNT_PHIS);
  2456. else
  2457. cost -= total_savings (0, first_dep, incr_vec[i].incr,
  2458. DONT_COUNT_PHIS);
  2459. incr_vec[i].cost = cost;
  2460. }
  2461. }
  2462. }
  2463. /* Return the nearest common dominator of BB1 and BB2. If the blocks
  2464. are identical, return the earlier of C1 and C2 in *WHERE. Otherwise,
  2465. if the NCD matches BB1, return C1 in *WHERE; if the NCD matches BB2,
  2466. return C2 in *WHERE; and if the NCD matches neither, return NULL in
  2467. *WHERE. Note: It is possible for one of C1 and C2 to be NULL. */
  2468. static basic_block
  2469. ncd_for_two_cands (basic_block bb1, basic_block bb2,
  2470. slsr_cand_t c1, slsr_cand_t c2, slsr_cand_t *where)
  2471. {
  2472. basic_block ncd;
  2473. if (!bb1)
  2474. {
  2475. *where = c2;
  2476. return bb2;
  2477. }
  2478. if (!bb2)
  2479. {
  2480. *where = c1;
  2481. return bb1;
  2482. }
  2483. ncd = nearest_common_dominator (CDI_DOMINATORS, bb1, bb2);
  2484. /* If both candidates are in the same block, the earlier
  2485. candidate wins. */
  2486. if (bb1 == ncd && bb2 == ncd)
  2487. {
  2488. if (!c1 || (c2 && c2->cand_num < c1->cand_num))
  2489. *where = c2;
  2490. else
  2491. *where = c1;
  2492. }
  2493. /* Otherwise, if one of them produced a candidate in the
  2494. dominator, that one wins. */
  2495. else if (bb1 == ncd)
  2496. *where = c1;
  2497. else if (bb2 == ncd)
  2498. *where = c2;
  2499. /* If neither matches the dominator, neither wins. */
  2500. else
  2501. *where = NULL;
  2502. return ncd;
  2503. }
  2504. /* Consider all candidates that feed PHI. Find the nearest common
  2505. dominator of those candidates requiring the given increment INCR.
  2506. Further find and return the nearest common dominator of this result
  2507. with block NCD. If the returned block contains one or more of the
  2508. candidates, return the earliest candidate in the block in *WHERE. */
  2509. static basic_block
  2510. ncd_with_phi (slsr_cand_t c, const widest_int &incr, gphi *phi,
  2511. basic_block ncd, slsr_cand_t *where)
  2512. {
  2513. unsigned i;
  2514. slsr_cand_t basis = lookup_cand (c->basis);
  2515. slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
  2516. for (i = 0; i < gimple_phi_num_args (phi); i++)
  2517. {
  2518. tree arg = gimple_phi_arg_def (phi, i);
  2519. if (!operand_equal_p (arg, phi_cand->base_expr, 0))
  2520. {
  2521. gimple arg_def = SSA_NAME_DEF_STMT (arg);
  2522. if (gimple_code (arg_def) == GIMPLE_PHI)
  2523. ncd = ncd_with_phi (c, incr, as_a <gphi *> (arg_def), ncd,
  2524. where);
  2525. else
  2526. {
  2527. slsr_cand_t arg_cand = base_cand_from_table (arg);
  2528. widest_int diff = arg_cand->index - basis->index;
  2529. basic_block pred = gimple_phi_arg_edge (phi, i)->src;
  2530. if ((incr == diff) || (!address_arithmetic_p && incr == -diff))
  2531. ncd = ncd_for_two_cands (ncd, pred, *where, NULL, where);
  2532. }
  2533. }
  2534. }
  2535. return ncd;
  2536. }
  2537. /* Consider the candidate C together with any candidates that feed
  2538. C's phi dependence (if any). Find and return the nearest common
  2539. dominator of those candidates requiring the given increment INCR.
  2540. If the returned block contains one or more of the candidates,
  2541. return the earliest candidate in the block in *WHERE. */
  2542. static basic_block
  2543. ncd_of_cand_and_phis (slsr_cand_t c, const widest_int &incr, slsr_cand_t *where)
  2544. {
  2545. basic_block ncd = NULL;
  2546. if (cand_abs_increment (c) == incr)
  2547. {
  2548. ncd = gimple_bb (c->cand_stmt);
  2549. *where = c;
  2550. }
  2551. if (phi_dependent_cand_p (c))
  2552. ncd = ncd_with_phi (c, incr,
  2553. as_a <gphi *> (lookup_cand (c->def_phi)->cand_stmt),
  2554. ncd, where);
  2555. return ncd;
  2556. }
  2557. /* Consider all candidates in the tree rooted at C for which INCR
  2558. represents the required increment of C relative to its basis.
  2559. Find and return the basic block that most nearly dominates all
  2560. such candidates. If the returned block contains one or more of
  2561. the candidates, return the earliest candidate in the block in
  2562. *WHERE. */
  2563. static basic_block
  2564. nearest_common_dominator_for_cands (slsr_cand_t c, const widest_int &incr,
  2565. slsr_cand_t *where)
  2566. {
  2567. basic_block sib_ncd = NULL, dep_ncd = NULL, this_ncd = NULL, ncd;
  2568. slsr_cand_t sib_where = NULL, dep_where = NULL, this_where = NULL, new_where;
  2569. /* First find the NCD of all siblings and dependents. */
  2570. if (c->sibling)
  2571. sib_ncd = nearest_common_dominator_for_cands (lookup_cand (c->sibling),
  2572. incr, &sib_where);
  2573. if (c->dependent)
  2574. dep_ncd = nearest_common_dominator_for_cands (lookup_cand (c->dependent),
  2575. incr, &dep_where);
  2576. if (!sib_ncd && !dep_ncd)
  2577. {
  2578. new_where = NULL;
  2579. ncd = NULL;
  2580. }
  2581. else if (sib_ncd && !dep_ncd)
  2582. {
  2583. new_where = sib_where;
  2584. ncd = sib_ncd;
  2585. }
  2586. else if (dep_ncd && !sib_ncd)
  2587. {
  2588. new_where = dep_where;
  2589. ncd = dep_ncd;
  2590. }
  2591. else
  2592. ncd = ncd_for_two_cands (sib_ncd, dep_ncd, sib_where,
  2593. dep_where, &new_where);
  2594. /* If the candidate's increment doesn't match the one we're interested
  2595. in (and nor do any increments for feeding defs of a phi-dependence),
  2596. then the result depends only on siblings and dependents. */
  2597. this_ncd = ncd_of_cand_and_phis (c, incr, &this_where);
  2598. if (!this_ncd || cand_already_replaced (c))
  2599. {
  2600. *where = new_where;
  2601. return ncd;
  2602. }
  2603. /* Otherwise, compare this candidate with the result from all siblings
  2604. and dependents. */
  2605. ncd = ncd_for_two_cands (ncd, this_ncd, new_where, this_where, where);
  2606. return ncd;
  2607. }
  2608. /* Return TRUE if the increment indexed by INDEX is profitable to replace. */
  2609. static inline bool
  2610. profitable_increment_p (unsigned index)
  2611. {
  2612. return (incr_vec[index].cost <= COST_NEUTRAL);
  2613. }
  2614. /* For each profitable increment in the increment vector not equal to
  2615. 0 or 1 (or -1, for non-pointer arithmetic), find the nearest common
  2616. dominator of all statements in the candidate chain rooted at C
  2617. that require that increment, and insert an initializer
  2618. T_0 = stride * increment at that location. Record T_0 with the
  2619. increment record. */
  2620. static void
  2621. insert_initializers (slsr_cand_t c)
  2622. {
  2623. unsigned i;
  2624. for (i = 0; i < incr_vec_len; i++)
  2625. {
  2626. basic_block bb;
  2627. slsr_cand_t where = NULL;
  2628. gassign *init_stmt;
  2629. tree stride_type, new_name, incr_tree;
  2630. widest_int incr = incr_vec[i].incr;
  2631. if (!profitable_increment_p (i)
  2632. || incr == 1
  2633. || (incr == -1
  2634. && gimple_assign_rhs_code (c->cand_stmt) != POINTER_PLUS_EXPR)
  2635. || incr == 0)
  2636. continue;
  2637. /* We may have already identified an existing initializer that
  2638. will suffice. */
  2639. if (incr_vec[i].initializer)
  2640. {
  2641. if (dump_file && (dump_flags & TDF_DETAILS))
  2642. {
  2643. fputs ("Using existing initializer: ", dump_file);
  2644. print_gimple_stmt (dump_file,
  2645. SSA_NAME_DEF_STMT (incr_vec[i].initializer),
  2646. 0, 0);
  2647. }
  2648. continue;
  2649. }
  2650. /* Find the block that most closely dominates all candidates
  2651. with this increment. If there is at least one candidate in
  2652. that block, the earliest one will be returned in WHERE. */
  2653. bb = nearest_common_dominator_for_cands (c, incr, &where);
  2654. /* Create a new SSA name to hold the initializer's value. */
  2655. stride_type = TREE_TYPE (c->stride);
  2656. new_name = make_temp_ssa_name (stride_type, NULL, "slsr");
  2657. incr_vec[i].initializer = new_name;
  2658. /* Create the initializer and insert it in the latest possible
  2659. dominating position. */
  2660. incr_tree = wide_int_to_tree (stride_type, incr);
  2661. init_stmt = gimple_build_assign (new_name, MULT_EXPR,
  2662. c->stride, incr_tree);
  2663. if (where)
  2664. {
  2665. gimple_stmt_iterator gsi = gsi_for_stmt (where->cand_stmt);
  2666. gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT);
  2667. gimple_set_location (init_stmt, gimple_location (where->cand_stmt));
  2668. }
  2669. else
  2670. {
  2671. gimple_stmt_iterator gsi = gsi_last_bb (bb);
  2672. gimple basis_stmt = lookup_cand (c->basis)->cand_stmt;
  2673. if (!gsi_end_p (gsi) && is_ctrl_stmt (gsi_stmt (gsi)))
  2674. gsi_insert_before (&gsi, init_stmt, GSI_SAME_STMT);
  2675. else
  2676. gsi_insert_after (&gsi, init_stmt, GSI_SAME_STMT);
  2677. gimple_set_location (init_stmt, gimple_location (basis_stmt));
  2678. }
  2679. if (dump_file && (dump_flags & TDF_DETAILS))
  2680. {
  2681. fputs ("Inserting initializer: ", dump_file);
  2682. print_gimple_stmt (dump_file, init_stmt, 0, 0);
  2683. }
  2684. }
  2685. }
  2686. /* Return TRUE iff all required increments for candidates feeding PHI
  2687. are profitable to replace on behalf of candidate C. */
  2688. static bool
  2689. all_phi_incrs_profitable (slsr_cand_t c, gimple phi)
  2690. {
  2691. unsigned i;
  2692. slsr_cand_t basis = lookup_cand (c->basis);
  2693. slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
  2694. for (i = 0; i < gimple_phi_num_args (phi); i++)
  2695. {
  2696. tree arg = gimple_phi_arg_def (phi, i);
  2697. if (!operand_equal_p (arg, phi_cand->base_expr, 0))
  2698. {
  2699. gimple arg_def = SSA_NAME_DEF_STMT (arg);
  2700. if (gimple_code (arg_def) == GIMPLE_PHI)
  2701. {
  2702. if (!all_phi_incrs_profitable (c, arg_def))
  2703. return false;
  2704. }
  2705. else
  2706. {
  2707. int j;
  2708. slsr_cand_t arg_cand = base_cand_from_table (arg);
  2709. widest_int increment = arg_cand->index - basis->index;
  2710. if (!address_arithmetic_p && wi::neg_p (increment))
  2711. increment = -increment;
  2712. j = incr_vec_index (increment);
  2713. if (dump_file && (dump_flags & TDF_DETAILS))
  2714. {
  2715. fprintf (dump_file, " Conditional candidate %d, phi: ",
  2716. c->cand_num);
  2717. print_gimple_stmt (dump_file, phi, 0, 0);
  2718. fputs (" increment: ", dump_file);
  2719. print_decs (increment, dump_file);
  2720. if (j < 0)
  2721. fprintf (dump_file,
  2722. "\n Not replaced; incr_vec overflow.\n");
  2723. else {
  2724. fprintf (dump_file, "\n cost: %d\n", incr_vec[j].cost);
  2725. if (profitable_increment_p (j))
  2726. fputs (" Replacing...\n", dump_file);
  2727. else
  2728. fputs (" Not replaced.\n", dump_file);
  2729. }
  2730. }
  2731. if (j < 0 || !profitable_increment_p (j))
  2732. return false;
  2733. }
  2734. }
  2735. }
  2736. return true;
  2737. }
  2738. /* Create a NOP_EXPR that copies FROM_EXPR into a new SSA name of
  2739. type TO_TYPE, and insert it in front of the statement represented
  2740. by candidate C. Use *NEW_VAR to create the new SSA name. Return
  2741. the new SSA name. */
  2742. static tree
  2743. introduce_cast_before_cand (slsr_cand_t c, tree to_type, tree from_expr)
  2744. {
  2745. tree cast_lhs;
  2746. gassign *cast_stmt;
  2747. gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
  2748. cast_lhs = make_temp_ssa_name (to_type, NULL, "slsr");
  2749. cast_stmt = gimple_build_assign (cast_lhs, NOP_EXPR, from_expr);
  2750. gimple_set_location (cast_stmt, gimple_location (c->cand_stmt));
  2751. gsi_insert_before (&gsi, cast_stmt, GSI_SAME_STMT);
  2752. if (dump_file && (dump_flags & TDF_DETAILS))
  2753. {
  2754. fputs (" Inserting: ", dump_file);
  2755. print_gimple_stmt (dump_file, cast_stmt, 0, 0);
  2756. }
  2757. return cast_lhs;
  2758. }
  2759. /* Replace the RHS of the statement represented by candidate C with
  2760. NEW_CODE, NEW_RHS1, and NEW_RHS2, provided that to do so doesn't
  2761. leave C unchanged or just interchange its operands. The original
  2762. operation and operands are in OLD_CODE, OLD_RHS1, and OLD_RHS2.
  2763. If the replacement was made and we are doing a details dump,
  2764. return the revised statement, else NULL. */
  2765. static gimple
  2766. replace_rhs_if_not_dup (enum tree_code new_code, tree new_rhs1, tree new_rhs2,
  2767. enum tree_code old_code, tree old_rhs1, tree old_rhs2,
  2768. slsr_cand_t c)
  2769. {
  2770. if (new_code != old_code
  2771. || ((!operand_equal_p (new_rhs1, old_rhs1, 0)
  2772. || !operand_equal_p (new_rhs2, old_rhs2, 0))
  2773. && (!operand_equal_p (new_rhs1, old_rhs2, 0)
  2774. || !operand_equal_p (new_rhs2, old_rhs1, 0))))
  2775. {
  2776. gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
  2777. gimple_assign_set_rhs_with_ops (&gsi, new_code, new_rhs1, new_rhs2);
  2778. update_stmt (gsi_stmt (gsi));
  2779. c->cand_stmt = gsi_stmt (gsi);
  2780. if (dump_file && (dump_flags & TDF_DETAILS))
  2781. return gsi_stmt (gsi);
  2782. }
  2783. else if (dump_file && (dump_flags & TDF_DETAILS))
  2784. fputs (" (duplicate, not actually replacing)\n", dump_file);
  2785. return NULL;
  2786. }
  2787. /* Strength-reduce the statement represented by candidate C by replacing
  2788. it with an equivalent addition or subtraction. I is the index into
  2789. the increment vector identifying C's increment. NEW_VAR is used to
  2790. create a new SSA name if a cast needs to be introduced. BASIS_NAME
  2791. is the rhs1 to use in creating the add/subtract. */
  2792. static void
  2793. replace_one_candidate (slsr_cand_t c, unsigned i, tree basis_name)
  2794. {
  2795. gimple stmt_to_print = NULL;
  2796. tree orig_rhs1, orig_rhs2;
  2797. tree rhs2;
  2798. enum tree_code orig_code, repl_code;
  2799. widest_int cand_incr;
  2800. orig_code = gimple_assign_rhs_code (c->cand_stmt);
  2801. orig_rhs1 = gimple_assign_rhs1 (c->cand_stmt);
  2802. orig_rhs2 = gimple_assign_rhs2 (c->cand_stmt);
  2803. cand_incr = cand_increment (c);
  2804. if (dump_file && (dump_flags & TDF_DETAILS))
  2805. {
  2806. fputs ("Replacing: ", dump_file);
  2807. print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
  2808. stmt_to_print = c->cand_stmt;
  2809. }
  2810. if (address_arithmetic_p)
  2811. repl_code = POINTER_PLUS_EXPR;
  2812. else
  2813. repl_code = PLUS_EXPR;
  2814. /* If the increment has an initializer T_0, replace the candidate
  2815. statement with an add of the basis name and the initializer. */
  2816. if (incr_vec[i].initializer)
  2817. {
  2818. tree init_type = TREE_TYPE (incr_vec[i].initializer);
  2819. tree orig_type = TREE_TYPE (orig_rhs2);
  2820. if (types_compatible_p (orig_type, init_type))
  2821. rhs2 = incr_vec[i].initializer;
  2822. else
  2823. rhs2 = introduce_cast_before_cand (c, orig_type,
  2824. incr_vec[i].initializer);
  2825. if (incr_vec[i].incr != cand_incr)
  2826. {
  2827. gcc_assert (repl_code == PLUS_EXPR);
  2828. repl_code = MINUS_EXPR;
  2829. }
  2830. stmt_to_print = replace_rhs_if_not_dup (repl_code, basis_name, rhs2,
  2831. orig_code, orig_rhs1, orig_rhs2,
  2832. c);
  2833. }
  2834. /* Otherwise, the increment is one of -1, 0, and 1. Replace
  2835. with a subtract of the stride from the basis name, a copy
  2836. from the basis name, or an add of the stride to the basis
  2837. name, respectively. It may be necessary to introduce a
  2838. cast (or reuse an existing cast). */
  2839. else if (cand_incr == 1)
  2840. {
  2841. tree stride_type = TREE_TYPE (c->stride);
  2842. tree orig_type = TREE_TYPE (orig_rhs2);
  2843. if (types_compatible_p (orig_type, stride_type))
  2844. rhs2 = c->stride;
  2845. else
  2846. rhs2 = introduce_cast_before_cand (c, orig_type, c->stride);
  2847. stmt_to_print = replace_rhs_if_not_dup (repl_code, basis_name, rhs2,
  2848. orig_code, orig_rhs1, orig_rhs2,
  2849. c);
  2850. }
  2851. else if (cand_incr == -1)
  2852. {
  2853. tree stride_type = TREE_TYPE (c->stride);
  2854. tree orig_type = TREE_TYPE (orig_rhs2);
  2855. gcc_assert (repl_code != POINTER_PLUS_EXPR);
  2856. if (types_compatible_p (orig_type, stride_type))
  2857. rhs2 = c->stride;
  2858. else
  2859. rhs2 = introduce_cast_before_cand (c, orig_type, c->stride);
  2860. if (orig_code != MINUS_EXPR
  2861. || !operand_equal_p (basis_name, orig_rhs1, 0)
  2862. || !operand_equal_p (rhs2, orig_rhs2, 0))
  2863. {
  2864. gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
  2865. gimple_assign_set_rhs_with_ops (&gsi, MINUS_EXPR, basis_name, rhs2);
  2866. update_stmt (gsi_stmt (gsi));
  2867. c->cand_stmt = gsi_stmt (gsi);
  2868. if (dump_file && (dump_flags & TDF_DETAILS))
  2869. stmt_to_print = gsi_stmt (gsi);
  2870. }
  2871. else if (dump_file && (dump_flags & TDF_DETAILS))
  2872. fputs (" (duplicate, not actually replacing)\n", dump_file);
  2873. }
  2874. else if (cand_incr == 0)
  2875. {
  2876. tree lhs = gimple_assign_lhs (c->cand_stmt);
  2877. tree lhs_type = TREE_TYPE (lhs);
  2878. tree basis_type = TREE_TYPE (basis_name);
  2879. if (types_compatible_p (lhs_type, basis_type))
  2880. {
  2881. gassign *copy_stmt = gimple_build_assign (lhs, basis_name);
  2882. gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
  2883. gimple_set_location (copy_stmt, gimple_location (c->cand_stmt));
  2884. gsi_replace (&gsi, copy_stmt, false);
  2885. c->cand_stmt = copy_stmt;
  2886. if (dump_file && (dump_flags & TDF_DETAILS))
  2887. stmt_to_print = copy_stmt;
  2888. }
  2889. else
  2890. {
  2891. gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
  2892. gassign *cast_stmt = gimple_build_assign (lhs, NOP_EXPR, basis_name);
  2893. gimple_set_location (cast_stmt, gimple_location (c->cand_stmt));
  2894. gsi_replace (&gsi, cast_stmt, false);
  2895. c->cand_stmt = cast_stmt;
  2896. if (dump_file && (dump_flags & TDF_DETAILS))
  2897. stmt_to_print = cast_stmt;
  2898. }
  2899. }
  2900. else
  2901. gcc_unreachable ();
  2902. if (dump_file && (dump_flags & TDF_DETAILS) && stmt_to_print)
  2903. {
  2904. fputs ("With: ", dump_file);
  2905. print_gimple_stmt (dump_file, stmt_to_print, 0, 0);
  2906. fputs ("\n", dump_file);
  2907. }
  2908. }
  2909. /* For each candidate in the tree rooted at C, replace it with
  2910. an increment if such has been shown to be profitable. */
  2911. static void
  2912. replace_profitable_candidates (slsr_cand_t c)
  2913. {
  2914. if (!cand_already_replaced (c))
  2915. {
  2916. widest_int increment = cand_abs_increment (c);
  2917. enum tree_code orig_code = gimple_assign_rhs_code (c->cand_stmt);
  2918. int i;
  2919. i = incr_vec_index (increment);
  2920. /* Only process profitable increments. Nothing useful can be done
  2921. to a cast or copy. */
  2922. if (i >= 0
  2923. && profitable_increment_p (i)
  2924. && orig_code != MODIFY_EXPR
  2925. && !CONVERT_EXPR_CODE_P (orig_code))
  2926. {
  2927. if (phi_dependent_cand_p (c))
  2928. {
  2929. gimple phi = lookup_cand (c->def_phi)->cand_stmt;
  2930. if (all_phi_incrs_profitable (c, phi))
  2931. {
  2932. /* Look up the LHS SSA name from C's basis. This will be
  2933. the RHS1 of the adds we will introduce to create new
  2934. phi arguments. */
  2935. slsr_cand_t basis = lookup_cand (c->basis);
  2936. tree basis_name = gimple_assign_lhs (basis->cand_stmt);
  2937. /* Create a new phi statement that will represent C's true
  2938. basis after the transformation is complete. */
  2939. location_t loc = gimple_location (c->cand_stmt);
  2940. tree name = create_phi_basis (c, phi, basis_name,
  2941. loc, UNKNOWN_STRIDE);
  2942. /* Replace C with an add of the new basis phi and the
  2943. increment. */
  2944. replace_one_candidate (c, i, name);
  2945. }
  2946. }
  2947. else
  2948. {
  2949. slsr_cand_t basis = lookup_cand (c->basis);
  2950. tree basis_name = gimple_assign_lhs (basis->cand_stmt);
  2951. replace_one_candidate (c, i, basis_name);
  2952. }
  2953. }
  2954. }
  2955. if (c->sibling)
  2956. replace_profitable_candidates (lookup_cand (c->sibling));
  2957. if (c->dependent)
  2958. replace_profitable_candidates (lookup_cand (c->dependent));
  2959. }
  2960. /* Analyze costs of related candidates in the candidate vector,
  2961. and make beneficial replacements. */
  2962. static void
  2963. analyze_candidates_and_replace (void)
  2964. {
  2965. unsigned i;
  2966. slsr_cand_t c;
  2967. /* Each candidate that has a null basis and a non-null
  2968. dependent is the root of a tree of related statements.
  2969. Analyze each tree to determine a subset of those
  2970. statements that can be replaced with maximum benefit. */
  2971. FOR_EACH_VEC_ELT (cand_vec, i, c)
  2972. {
  2973. slsr_cand_t first_dep;
  2974. if (c->basis != 0 || c->dependent == 0)
  2975. continue;
  2976. if (dump_file && (dump_flags & TDF_DETAILS))
  2977. fprintf (dump_file, "\nProcessing dependency tree rooted at %d.\n",
  2978. c->cand_num);
  2979. first_dep = lookup_cand (c->dependent);
  2980. /* If this is a chain of CAND_REFs, unconditionally replace
  2981. each of them with a strength-reduced data reference. */
  2982. if (c->kind == CAND_REF)
  2983. replace_refs (c);
  2984. /* If the common stride of all related candidates is a known
  2985. constant, each candidate without a phi-dependence can be
  2986. profitably replaced. Each replaces a multiply by a single
  2987. add, with the possibility that a feeding add also goes dead.
  2988. A candidate with a phi-dependence is replaced only if the
  2989. compensation code it requires is offset by the strength
  2990. reduction savings. */
  2991. else if (TREE_CODE (c->stride) == INTEGER_CST)
  2992. replace_uncond_cands_and_profitable_phis (first_dep);
  2993. /* When the stride is an SSA name, it may still be profitable
  2994. to replace some or all of the dependent candidates, depending
  2995. on whether the introduced increments can be reused, or are
  2996. less expensive to calculate than the replaced statements. */
  2997. else
  2998. {
  2999. machine_mode mode;
  3000. bool speed;
  3001. /* Determine whether we'll be generating pointer arithmetic
  3002. when replacing candidates. */
  3003. address_arithmetic_p = (c->kind == CAND_ADD
  3004. && POINTER_TYPE_P (c->cand_type));
  3005. /* If all candidates have already been replaced under other
  3006. interpretations, nothing remains to be done. */
  3007. if (!count_candidates (c))
  3008. continue;
  3009. /* Construct an array of increments for this candidate chain. */
  3010. incr_vec = XNEWVEC (incr_info, MAX_INCR_VEC_LEN);
  3011. incr_vec_len = 0;
  3012. record_increments (c);
  3013. /* Determine which increments are profitable to replace. */
  3014. mode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (c->cand_stmt)));
  3015. speed = optimize_cands_for_speed_p (c);
  3016. analyze_increments (first_dep, mode, speed);
  3017. /* Insert initializers of the form T_0 = stride * increment
  3018. for use in profitable replacements. */
  3019. insert_initializers (first_dep);
  3020. dump_incr_vec ();
  3021. /* Perform the replacements. */
  3022. replace_profitable_candidates (first_dep);
  3023. free (incr_vec);
  3024. }
  3025. }
  3026. }
  3027. namespace {
  3028. const pass_data pass_data_strength_reduction =
  3029. {
  3030. GIMPLE_PASS, /* type */
  3031. "slsr", /* name */
  3032. OPTGROUP_NONE, /* optinfo_flags */
  3033. TV_GIMPLE_SLSR, /* tv_id */
  3034. ( PROP_cfg | PROP_ssa ), /* properties_required */
  3035. 0, /* properties_provided */
  3036. 0, /* properties_destroyed */
  3037. 0, /* todo_flags_start */
  3038. 0, /* todo_flags_finish */
  3039. };
  3040. class pass_strength_reduction : public gimple_opt_pass
  3041. {
  3042. public:
  3043. pass_strength_reduction (gcc::context *ctxt)
  3044. : gimple_opt_pass (pass_data_strength_reduction, ctxt)
  3045. {}
  3046. /* opt_pass methods: */
  3047. virtual bool gate (function *) { return flag_tree_slsr; }
  3048. virtual unsigned int execute (function *);
  3049. }; // class pass_strength_reduction
  3050. unsigned
  3051. pass_strength_reduction::execute (function *fun)
  3052. {
  3053. /* Create the obstack where candidates will reside. */
  3054. gcc_obstack_init (&cand_obstack);
  3055. /* Allocate the candidate vector. */
  3056. cand_vec.create (128);
  3057. /* Allocate the mapping from statements to candidate indices. */
  3058. stmt_cand_map = new hash_map<gimple, slsr_cand_t>;
  3059. /* Create the obstack where candidate chains will reside. */
  3060. gcc_obstack_init (&chain_obstack);
  3061. /* Allocate the mapping from base expressions to candidate chains. */
  3062. base_cand_map = new hash_table<cand_chain_hasher> (500);
  3063. /* Allocate the mapping from bases to alternative bases. */
  3064. alt_base_map = new hash_map<tree, tree>;
  3065. /* Initialize the loop optimizer. We need to detect flow across
  3066. back edges, and this gives us dominator information as well. */
  3067. loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
  3068. /* Walk the CFG in predominator order looking for strength reduction
  3069. candidates. */
  3070. find_candidates_dom_walker (CDI_DOMINATORS)
  3071. .walk (fun->cfg->x_entry_block_ptr);
  3072. if (dump_file && (dump_flags & TDF_DETAILS))
  3073. {
  3074. dump_cand_vec ();
  3075. dump_cand_chains ();
  3076. }
  3077. delete alt_base_map;
  3078. free_affine_expand_cache (&name_expansions);
  3079. /* Analyze costs and make appropriate replacements. */
  3080. analyze_candidates_and_replace ();
  3081. loop_optimizer_finalize ();
  3082. delete base_cand_map;
  3083. base_cand_map = NULL;
  3084. obstack_free (&chain_obstack, NULL);
  3085. delete stmt_cand_map;
  3086. cand_vec.release ();
  3087. obstack_free (&cand_obstack, NULL);
  3088. return 0;
  3089. }
  3090. } // anon namespace
  3091. gimple_opt_pass *
  3092. make_pass_strength_reduction (gcc::context *ctxt)
  3093. {
  3094. return new pass_strength_reduction (ctxt);
  3095. }