tree-scalar-evolution.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551
  1. /* Scalar evolution detector.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. Contributed by Sebastian Pop <s.pop@laposte.net>
  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. /*
  17. Description:
  18. This pass analyzes the evolution of scalar variables in loop
  19. structures. The algorithm is based on the SSA representation,
  20. and on the loop hierarchy tree. This algorithm is not based on
  21. the notion of versions of a variable, as it was the case for the
  22. previous implementations of the scalar evolution algorithm, but
  23. it assumes that each defined name is unique.
  24. The notation used in this file is called "chains of recurrences",
  25. and has been proposed by Eugene Zima, Robert Van Engelen, and
  26. others for describing induction variables in programs. For example
  27. "b -> {0, +, 2}_1" means that the scalar variable "b" is equal to 0
  28. when entering in the loop_1 and has a step 2 in this loop, in other
  29. words "for (b = 0; b < N; b+=2);". Note that the coefficients of
  30. this chain of recurrence (or chrec [shrek]) can contain the name of
  31. other variables, in which case they are called parametric chrecs.
  32. For example, "b -> {a, +, 2}_1" means that the initial value of "b"
  33. is the value of "a". In most of the cases these parametric chrecs
  34. are fully instantiated before their use because symbolic names can
  35. hide some difficult cases such as self-references described later
  36. (see the Fibonacci example).
  37. A short sketch of the algorithm is:
  38. Given a scalar variable to be analyzed, follow the SSA edge to
  39. its definition:
  40. - When the definition is a GIMPLE_ASSIGN: if the right hand side
  41. (RHS) of the definition cannot be statically analyzed, the answer
  42. of the analyzer is: "don't know".
  43. Otherwise, for all the variables that are not yet analyzed in the
  44. RHS, try to determine their evolution, and finally try to
  45. evaluate the operation of the RHS that gives the evolution
  46. function of the analyzed variable.
  47. - When the definition is a condition-phi-node: determine the
  48. evolution function for all the branches of the phi node, and
  49. finally merge these evolutions (see chrec_merge).
  50. - When the definition is a loop-phi-node: determine its initial
  51. condition, that is the SSA edge defined in an outer loop, and
  52. keep it symbolic. Then determine the SSA edges that are defined
  53. in the body of the loop. Follow the inner edges until ending on
  54. another loop-phi-node of the same analyzed loop. If the reached
  55. loop-phi-node is not the starting loop-phi-node, then we keep
  56. this definition under a symbolic form. If the reached
  57. loop-phi-node is the same as the starting one, then we compute a
  58. symbolic stride on the return path. The result is then the
  59. symbolic chrec {initial_condition, +, symbolic_stride}_loop.
  60. Examples:
  61. Example 1: Illustration of the basic algorithm.
  62. | a = 3
  63. | loop_1
  64. | b = phi (a, c)
  65. | c = b + 1
  66. | if (c > 10) exit_loop
  67. | endloop
  68. Suppose that we want to know the number of iterations of the
  69. loop_1. The exit_loop is controlled by a COND_EXPR (c > 10). We
  70. ask the scalar evolution analyzer two questions: what's the
  71. scalar evolution (scev) of "c", and what's the scev of "10". For
  72. "10" the answer is "10" since it is a scalar constant. For the
  73. scalar variable "c", it follows the SSA edge to its definition,
  74. "c = b + 1", and then asks again what's the scev of "b".
  75. Following the SSA edge, we end on a loop-phi-node "b = phi (a,
  76. c)", where the initial condition is "a", and the inner loop edge
  77. is "c". The initial condition is kept under a symbolic form (it
  78. may be the case that the copy constant propagation has done its
  79. work and we end with the constant "3" as one of the edges of the
  80. loop-phi-node). The update edge is followed to the end of the
  81. loop, and until reaching again the starting loop-phi-node: b -> c
  82. -> b. At this point we have drawn a path from "b" to "b" from
  83. which we compute the stride in the loop: in this example it is
  84. "+1". The resulting scev for "b" is "b -> {a, +, 1}_1". Now
  85. that the scev for "b" is known, it is possible to compute the
  86. scev for "c", that is "c -> {a + 1, +, 1}_1". In order to
  87. determine the number of iterations in the loop_1, we have to
  88. instantiate_parameters (loop_1, {a + 1, +, 1}_1), that gives after some
  89. more analysis the scev {4, +, 1}_1, or in other words, this is
  90. the function "f (x) = x + 4", where x is the iteration count of
  91. the loop_1. Now we have to solve the inequality "x + 4 > 10",
  92. and take the smallest iteration number for which the loop is
  93. exited: x = 7. This loop runs from x = 0 to x = 7, and in total
  94. there are 8 iterations. In terms of loop normalization, we have
  95. created a variable that is implicitly defined, "x" or just "_1",
  96. and all the other analyzed scalars of the loop are defined in
  97. function of this variable:
  98. a -> 3
  99. b -> {3, +, 1}_1
  100. c -> {4, +, 1}_1
  101. or in terms of a C program:
  102. | a = 3
  103. | for (x = 0; x <= 7; x++)
  104. | {
  105. | b = x + 3
  106. | c = x + 4
  107. | }
  108. Example 2a: Illustration of the algorithm on nested loops.
  109. | loop_1
  110. | a = phi (1, b)
  111. | c = a + 2
  112. | loop_2 10 times
  113. | b = phi (c, d)
  114. | d = b + 3
  115. | endloop
  116. | endloop
  117. For analyzing the scalar evolution of "a", the algorithm follows
  118. the SSA edge into the loop's body: "a -> b". "b" is an inner
  119. loop-phi-node, and its analysis as in Example 1, gives:
  120. b -> {c, +, 3}_2
  121. d -> {c + 3, +, 3}_2
  122. Following the SSA edge for the initial condition, we end on "c = a
  123. + 2", and then on the starting loop-phi-node "a". From this point,
  124. the loop stride is computed: back on "c = a + 2" we get a "+2" in
  125. the loop_1, then on the loop-phi-node "b" we compute the overall
  126. effect of the inner loop that is "b = c + 30", and we get a "+30"
  127. in the loop_1. That means that the overall stride in loop_1 is
  128. equal to "+32", and the result is:
  129. a -> {1, +, 32}_1
  130. c -> {3, +, 32}_1
  131. Example 2b: Multivariate chains of recurrences.
  132. | loop_1
  133. | k = phi (0, k + 1)
  134. | loop_2 4 times
  135. | j = phi (0, j + 1)
  136. | loop_3 4 times
  137. | i = phi (0, i + 1)
  138. | A[j + k] = ...
  139. | endloop
  140. | endloop
  141. | endloop
  142. Analyzing the access function of array A with
  143. instantiate_parameters (loop_1, "j + k"), we obtain the
  144. instantiation and the analysis of the scalar variables "j" and "k"
  145. in loop_1. This leads to the scalar evolution {4, +, 1}_1: the end
  146. value of loop_2 for "j" is 4, and the evolution of "k" in loop_1 is
  147. {0, +, 1}_1. To obtain the evolution function in loop_3 and
  148. instantiate the scalar variables up to loop_1, one has to use:
  149. instantiate_scev (block_before_loop (loop_1), loop_3, "j + k").
  150. The result of this call is {{0, +, 1}_1, +, 1}_2.
  151. Example 3: Higher degree polynomials.
  152. | loop_1
  153. | a = phi (2, b)
  154. | c = phi (5, d)
  155. | b = a + 1
  156. | d = c + a
  157. | endloop
  158. a -> {2, +, 1}_1
  159. b -> {3, +, 1}_1
  160. c -> {5, +, a}_1
  161. d -> {5 + a, +, a}_1
  162. instantiate_parameters (loop_1, {5, +, a}_1) -> {5, +, 2, +, 1}_1
  163. instantiate_parameters (loop_1, {5 + a, +, a}_1) -> {7, +, 3, +, 1}_1
  164. Example 4: Lucas, Fibonacci, or mixers in general.
  165. | loop_1
  166. | a = phi (1, b)
  167. | c = phi (3, d)
  168. | b = c
  169. | d = c + a
  170. | endloop
  171. a -> (1, c)_1
  172. c -> {3, +, a}_1
  173. The syntax "(1, c)_1" stands for a PEELED_CHREC that has the
  174. following semantics: during the first iteration of the loop_1, the
  175. variable contains the value 1, and then it contains the value "c".
  176. Note that this syntax is close to the syntax of the loop-phi-node:
  177. "a -> (1, c)_1" vs. "a = phi (1, c)".
  178. The symbolic chrec representation contains all the semantics of the
  179. original code. What is more difficult is to use this information.
  180. Example 5: Flip-flops, or exchangers.
  181. | loop_1
  182. | a = phi (1, b)
  183. | c = phi (3, d)
  184. | b = c
  185. | d = a
  186. | endloop
  187. a -> (1, c)_1
  188. c -> (3, a)_1
  189. Based on these symbolic chrecs, it is possible to refine this
  190. information into the more precise PERIODIC_CHRECs:
  191. a -> |1, 3|_1
  192. c -> |3, 1|_1
  193. This transformation is not yet implemented.
  194. Further readings:
  195. You can find a more detailed description of the algorithm in:
  196. http://icps.u-strasbg.fr/~pop/DEA_03_Pop.pdf
  197. http://icps.u-strasbg.fr/~pop/DEA_03_Pop.ps.gz. But note that
  198. this is a preliminary report and some of the details of the
  199. algorithm have changed. I'm working on a research report that
  200. updates the description of the algorithms to reflect the design
  201. choices used in this implementation.
  202. A set of slides show a high level overview of the algorithm and run
  203. an example through the scalar evolution analyzer:
  204. http://cri.ensmp.fr/~pop/gcc/mar04/slides.pdf
  205. The slides that I have presented at the GCC Summit'04 are available
  206. at: http://cri.ensmp.fr/~pop/gcc/20040604/gccsummit-lno-spop.pdf
  207. */
  208. #include "config.h"
  209. #include "system.h"
  210. #include "coretypes.h"
  211. #include "hash-set.h"
  212. #include "machmode.h"
  213. #include "vec.h"
  214. #include "double-int.h"
  215. #include "input.h"
  216. #include "alias.h"
  217. #include "symtab.h"
  218. #include "options.h"
  219. #include "wide-int.h"
  220. #include "inchash.h"
  221. #include "tree.h"
  222. #include "fold-const.h"
  223. #include "hashtab.h"
  224. #include "tm.h"
  225. #include "hard-reg-set.h"
  226. #include "function.h"
  227. #include "rtl.h"
  228. #include "flags.h"
  229. #include "statistics.h"
  230. #include "real.h"
  231. #include "fixed-value.h"
  232. #include "insn-config.h"
  233. #include "expmed.h"
  234. #include "dojump.h"
  235. #include "explow.h"
  236. #include "calls.h"
  237. #include "emit-rtl.h"
  238. #include "varasm.h"
  239. #include "stmt.h"
  240. #include "expr.h"
  241. #include "gimple-pretty-print.h"
  242. #include "predict.h"
  243. #include "dominance.h"
  244. #include "cfg.h"
  245. #include "basic-block.h"
  246. #include "tree-ssa-alias.h"
  247. #include "internal-fn.h"
  248. #include "gimple-expr.h"
  249. #include "is-a.h"
  250. #include "gimple.h"
  251. #include "gimplify.h"
  252. #include "gimple-iterator.h"
  253. #include "gimplify-me.h"
  254. #include "gimple-ssa.h"
  255. #include "tree-cfg.h"
  256. #include "tree-phinodes.h"
  257. #include "stringpool.h"
  258. #include "tree-ssanames.h"
  259. #include "tree-ssa-loop-ivopts.h"
  260. #include "tree-ssa-loop-manip.h"
  261. #include "tree-ssa-loop-niter.h"
  262. #include "tree-ssa-loop.h"
  263. #include "tree-ssa.h"
  264. #include "cfgloop.h"
  265. #include "tree-chrec.h"
  266. #include "tree-affine.h"
  267. #include "tree-scalar-evolution.h"
  268. #include "dumpfile.h"
  269. #include "params.h"
  270. #include "tree-ssa-propagate.h"
  271. #include "gimple-fold.h"
  272. static tree analyze_scalar_evolution_1 (struct loop *, tree, tree);
  273. static tree analyze_scalar_evolution_for_address_of (struct loop *loop,
  274. tree var);
  275. /* The cached information about an SSA name with version NAME_VERSION,
  276. claiming that below basic block with index INSTANTIATED_BELOW, the
  277. value of the SSA name can be expressed as CHREC. */
  278. struct GTY((for_user)) scev_info_str {
  279. unsigned int name_version;
  280. int instantiated_below;
  281. tree chrec;
  282. };
  283. /* Counters for the scev database. */
  284. static unsigned nb_set_scev = 0;
  285. static unsigned nb_get_scev = 0;
  286. /* The following trees are unique elements. Thus the comparison of
  287. another element to these elements should be done on the pointer to
  288. these trees, and not on their value. */
  289. /* The SSA_NAMEs that are not yet analyzed are qualified with NULL_TREE. */
  290. tree chrec_not_analyzed_yet;
  291. /* Reserved to the cases where the analyzer has detected an
  292. undecidable property at compile time. */
  293. tree chrec_dont_know;
  294. /* When the analyzer has detected that a property will never
  295. happen, then it qualifies it with chrec_known. */
  296. tree chrec_known;
  297. struct scev_info_hasher : ggc_hasher<scev_info_str *>
  298. {
  299. static hashval_t hash (scev_info_str *i);
  300. static bool equal (const scev_info_str *a, const scev_info_str *b);
  301. };
  302. static GTY (()) hash_table<scev_info_hasher> *scalar_evolution_info;
  303. /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
  304. static inline struct scev_info_str *
  305. new_scev_info_str (basic_block instantiated_below, tree var)
  306. {
  307. struct scev_info_str *res;
  308. res = ggc_alloc<scev_info_str> ();
  309. res->name_version = SSA_NAME_VERSION (var);
  310. res->chrec = chrec_not_analyzed_yet;
  311. res->instantiated_below = instantiated_below->index;
  312. return res;
  313. }
  314. /* Computes a hash function for database element ELT. */
  315. hashval_t
  316. scev_info_hasher::hash (scev_info_str *elt)
  317. {
  318. return elt->name_version ^ elt->instantiated_below;
  319. }
  320. /* Compares database elements E1 and E2. */
  321. bool
  322. scev_info_hasher::equal (const scev_info_str *elt1, const scev_info_str *elt2)
  323. {
  324. return (elt1->name_version == elt2->name_version
  325. && elt1->instantiated_below == elt2->instantiated_below);
  326. }
  327. /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block.
  328. A first query on VAR returns chrec_not_analyzed_yet. */
  329. static tree *
  330. find_var_scev_info (basic_block instantiated_below, tree var)
  331. {
  332. struct scev_info_str *res;
  333. struct scev_info_str tmp;
  334. tmp.name_version = SSA_NAME_VERSION (var);
  335. tmp.instantiated_below = instantiated_below->index;
  336. scev_info_str **slot = scalar_evolution_info->find_slot (&tmp, INSERT);
  337. if (!*slot)
  338. *slot = new_scev_info_str (instantiated_below, var);
  339. res = *slot;
  340. return &res->chrec;
  341. }
  342. /* Return true when CHREC contains symbolic names defined in
  343. LOOP_NB. */
  344. bool
  345. chrec_contains_symbols_defined_in_loop (const_tree chrec, unsigned loop_nb)
  346. {
  347. int i, n;
  348. if (chrec == NULL_TREE)
  349. return false;
  350. if (is_gimple_min_invariant (chrec))
  351. return false;
  352. if (TREE_CODE (chrec) == SSA_NAME)
  353. {
  354. gimple def;
  355. loop_p def_loop, loop;
  356. if (SSA_NAME_IS_DEFAULT_DEF (chrec))
  357. return false;
  358. def = SSA_NAME_DEF_STMT (chrec);
  359. def_loop = loop_containing_stmt (def);
  360. loop = get_loop (cfun, loop_nb);
  361. if (def_loop == NULL)
  362. return false;
  363. if (loop == def_loop || flow_loop_nested_p (loop, def_loop))
  364. return true;
  365. return false;
  366. }
  367. n = TREE_OPERAND_LENGTH (chrec);
  368. for (i = 0; i < n; i++)
  369. if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, i),
  370. loop_nb))
  371. return true;
  372. return false;
  373. }
  374. /* Return true when PHI is a loop-phi-node. */
  375. static bool
  376. loop_phi_node_p (gimple phi)
  377. {
  378. /* The implementation of this function is based on the following
  379. property: "all the loop-phi-nodes of a loop are contained in the
  380. loop's header basic block". */
  381. return loop_containing_stmt (phi)->header == gimple_bb (phi);
  382. }
  383. /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
  384. In general, in the case of multivariate evolutions we want to get
  385. the evolution in different loops. LOOP specifies the level for
  386. which to get the evolution.
  387. Example:
  388. | for (j = 0; j < 100; j++)
  389. | {
  390. | for (k = 0; k < 100; k++)
  391. | {
  392. | i = k + j; - Here the value of i is a function of j, k.
  393. | }
  394. | ... = i - Here the value of i is a function of j.
  395. | }
  396. | ... = i - Here the value of i is a scalar.
  397. Example:
  398. | i_0 = ...
  399. | loop_1 10 times
  400. | i_1 = phi (i_0, i_2)
  401. | i_2 = i_1 + 2
  402. | endloop
  403. This loop has the same effect as:
  404. LOOP_1 has the same effect as:
  405. | i_1 = i_0 + 20
  406. The overall effect of the loop, "i_0 + 20" in the previous example,
  407. is obtained by passing in the parameters: LOOP = 1,
  408. EVOLUTION_FN = {i_0, +, 2}_1.
  409. */
  410. tree
  411. compute_overall_effect_of_inner_loop (struct loop *loop, tree evolution_fn)
  412. {
  413. bool val = false;
  414. if (evolution_fn == chrec_dont_know)
  415. return chrec_dont_know;
  416. else if (TREE_CODE (evolution_fn) == POLYNOMIAL_CHREC)
  417. {
  418. struct loop *inner_loop = get_chrec_loop (evolution_fn);
  419. if (inner_loop == loop
  420. || flow_loop_nested_p (loop, inner_loop))
  421. {
  422. tree nb_iter = number_of_latch_executions (inner_loop);
  423. if (nb_iter == chrec_dont_know)
  424. return chrec_dont_know;
  425. else
  426. {
  427. tree res;
  428. /* evolution_fn is the evolution function in LOOP. Get
  429. its value in the nb_iter-th iteration. */
  430. res = chrec_apply (inner_loop->num, evolution_fn, nb_iter);
  431. if (chrec_contains_symbols_defined_in_loop (res, loop->num))
  432. res = instantiate_parameters (loop, res);
  433. /* Continue the computation until ending on a parent of LOOP. */
  434. return compute_overall_effect_of_inner_loop (loop, res);
  435. }
  436. }
  437. else
  438. return evolution_fn;
  439. }
  440. /* If the evolution function is an invariant, there is nothing to do. */
  441. else if (no_evolution_in_loop_p (evolution_fn, loop->num, &val) && val)
  442. return evolution_fn;
  443. else
  444. return chrec_dont_know;
  445. }
  446. /* Associate CHREC to SCALAR. */
  447. static void
  448. set_scalar_evolution (basic_block instantiated_below, tree scalar, tree chrec)
  449. {
  450. tree *scalar_info;
  451. if (TREE_CODE (scalar) != SSA_NAME)
  452. return;
  453. scalar_info = find_var_scev_info (instantiated_below, scalar);
  454. if (dump_file)
  455. {
  456. if (dump_flags & TDF_SCEV)
  457. {
  458. fprintf (dump_file, "(set_scalar_evolution \n");
  459. fprintf (dump_file, " instantiated_below = %d \n",
  460. instantiated_below->index);
  461. fprintf (dump_file, " (scalar = ");
  462. print_generic_expr (dump_file, scalar, 0);
  463. fprintf (dump_file, ")\n (scalar_evolution = ");
  464. print_generic_expr (dump_file, chrec, 0);
  465. fprintf (dump_file, "))\n");
  466. }
  467. if (dump_flags & TDF_STATS)
  468. nb_set_scev++;
  469. }
  470. *scalar_info = chrec;
  471. }
  472. /* Retrieve the chrec associated to SCALAR instantiated below
  473. INSTANTIATED_BELOW block. */
  474. static tree
  475. get_scalar_evolution (basic_block instantiated_below, tree scalar)
  476. {
  477. tree res;
  478. if (dump_file)
  479. {
  480. if (dump_flags & TDF_SCEV)
  481. {
  482. fprintf (dump_file, "(get_scalar_evolution \n");
  483. fprintf (dump_file, " (scalar = ");
  484. print_generic_expr (dump_file, scalar, 0);
  485. fprintf (dump_file, ")\n");
  486. }
  487. if (dump_flags & TDF_STATS)
  488. nb_get_scev++;
  489. }
  490. switch (TREE_CODE (scalar))
  491. {
  492. case SSA_NAME:
  493. res = *find_var_scev_info (instantiated_below, scalar);
  494. break;
  495. case REAL_CST:
  496. case FIXED_CST:
  497. case INTEGER_CST:
  498. res = scalar;
  499. break;
  500. default:
  501. res = chrec_not_analyzed_yet;
  502. break;
  503. }
  504. if (dump_file && (dump_flags & TDF_SCEV))
  505. {
  506. fprintf (dump_file, " (scalar_evolution = ");
  507. print_generic_expr (dump_file, res, 0);
  508. fprintf (dump_file, "))\n");
  509. }
  510. return res;
  511. }
  512. /* Helper function for add_to_evolution. Returns the evolution
  513. function for an assignment of the form "a = b + c", where "a" and
  514. "b" are on the strongly connected component. CHREC_BEFORE is the
  515. information that we already have collected up to this point.
  516. TO_ADD is the evolution of "c".
  517. When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
  518. evolution the expression TO_ADD, otherwise construct an evolution
  519. part for this loop. */
  520. static tree
  521. add_to_evolution_1 (unsigned loop_nb, tree chrec_before, tree to_add,
  522. gimple at_stmt)
  523. {
  524. tree type, left, right;
  525. struct loop *loop = get_loop (cfun, loop_nb), *chloop;
  526. switch (TREE_CODE (chrec_before))
  527. {
  528. case POLYNOMIAL_CHREC:
  529. chloop = get_chrec_loop (chrec_before);
  530. if (chloop == loop
  531. || flow_loop_nested_p (chloop, loop))
  532. {
  533. unsigned var;
  534. type = chrec_type (chrec_before);
  535. /* When there is no evolution part in this loop, build it. */
  536. if (chloop != loop)
  537. {
  538. var = loop_nb;
  539. left = chrec_before;
  540. right = SCALAR_FLOAT_TYPE_P (type)
  541. ? build_real (type, dconst0)
  542. : build_int_cst (type, 0);
  543. }
  544. else
  545. {
  546. var = CHREC_VARIABLE (chrec_before);
  547. left = CHREC_LEFT (chrec_before);
  548. right = CHREC_RIGHT (chrec_before);
  549. }
  550. to_add = chrec_convert (type, to_add, at_stmt);
  551. right = chrec_convert_rhs (type, right, at_stmt);
  552. right = chrec_fold_plus (chrec_type (right), right, to_add);
  553. return build_polynomial_chrec (var, left, right);
  554. }
  555. else
  556. {
  557. gcc_assert (flow_loop_nested_p (loop, chloop));
  558. /* Search the evolution in LOOP_NB. */
  559. left = add_to_evolution_1 (loop_nb, CHREC_LEFT (chrec_before),
  560. to_add, at_stmt);
  561. right = CHREC_RIGHT (chrec_before);
  562. right = chrec_convert_rhs (chrec_type (left), right, at_stmt);
  563. return build_polynomial_chrec (CHREC_VARIABLE (chrec_before),
  564. left, right);
  565. }
  566. default:
  567. /* These nodes do not depend on a loop. */
  568. if (chrec_before == chrec_dont_know)
  569. return chrec_dont_know;
  570. left = chrec_before;
  571. right = chrec_convert_rhs (chrec_type (left), to_add, at_stmt);
  572. return build_polynomial_chrec (loop_nb, left, right);
  573. }
  574. }
  575. /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
  576. of LOOP_NB.
  577. Description (provided for completeness, for those who read code in
  578. a plane, and for my poor 62 bytes brain that would have forgotten
  579. all this in the next two or three months):
  580. The algorithm of translation of programs from the SSA representation
  581. into the chrecs syntax is based on a pattern matching. After having
  582. reconstructed the overall tree expression for a loop, there are only
  583. two cases that can arise:
  584. 1. a = loop-phi (init, a + expr)
  585. 2. a = loop-phi (init, expr)
  586. where EXPR is either a scalar constant with respect to the analyzed
  587. loop (this is a degree 0 polynomial), or an expression containing
  588. other loop-phi definitions (these are higher degree polynomials).
  589. Examples:
  590. 1.
  591. | init = ...
  592. | loop_1
  593. | a = phi (init, a + 5)
  594. | endloop
  595. 2.
  596. | inita = ...
  597. | initb = ...
  598. | loop_1
  599. | a = phi (inita, 2 * b + 3)
  600. | b = phi (initb, b + 1)
  601. | endloop
  602. For the first case, the semantics of the SSA representation is:
  603. | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
  604. that is, there is a loop index "x" that determines the scalar value
  605. of the variable during the loop execution. During the first
  606. iteration, the value is that of the initial condition INIT, while
  607. during the subsequent iterations, it is the sum of the initial
  608. condition with the sum of all the values of EXPR from the initial
  609. iteration to the before last considered iteration.
  610. For the second case, the semantics of the SSA program is:
  611. | a (x) = init, if x = 0;
  612. | expr (x - 1), otherwise.
  613. The second case corresponds to the PEELED_CHREC, whose syntax is
  614. close to the syntax of a loop-phi-node:
  615. | phi (init, expr) vs. (init, expr)_x
  616. The proof of the translation algorithm for the first case is a
  617. proof by structural induction based on the degree of EXPR.
  618. Degree 0:
  619. When EXPR is a constant with respect to the analyzed loop, or in
  620. other words when EXPR is a polynomial of degree 0, the evolution of
  621. the variable A in the loop is an affine function with an initial
  622. condition INIT, and a step EXPR. In order to show this, we start
  623. from the semantics of the SSA representation:
  624. f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
  625. and since "expr (j)" is a constant with respect to "j",
  626. f (x) = init + x * expr
  627. Finally, based on the semantics of the pure sum chrecs, by
  628. identification we get the corresponding chrecs syntax:
  629. f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
  630. f (x) -> {init, +, expr}_x
  631. Higher degree:
  632. Suppose that EXPR is a polynomial of degree N with respect to the
  633. analyzed loop_x for which we have already determined that it is
  634. written under the chrecs syntax:
  635. | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
  636. We start from the semantics of the SSA program:
  637. | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
  638. |
  639. | f (x) = init + \sum_{j = 0}^{x - 1}
  640. | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
  641. |
  642. | f (x) = init + \sum_{j = 0}^{x - 1}
  643. | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
  644. |
  645. | f (x) = init + \sum_{k = 0}^{n - 1}
  646. | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
  647. |
  648. | f (x) = init + \sum_{k = 0}^{n - 1}
  649. | (b_k * \binom{x}{k + 1})
  650. |
  651. | f (x) = init + b_0 * \binom{x}{1} + ...
  652. | + b_{n-1} * \binom{x}{n}
  653. |
  654. | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
  655. | + b_{n-1} * \binom{x}{n}
  656. |
  657. And finally from the definition of the chrecs syntax, we identify:
  658. | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
  659. This shows the mechanism that stands behind the add_to_evolution
  660. function. An important point is that the use of symbolic
  661. parameters avoids the need of an analysis schedule.
  662. Example:
  663. | inita = ...
  664. | initb = ...
  665. | loop_1
  666. | a = phi (inita, a + 2 + b)
  667. | b = phi (initb, b + 1)
  668. | endloop
  669. When analyzing "a", the algorithm keeps "b" symbolically:
  670. | a -> {inita, +, 2 + b}_1
  671. Then, after instantiation, the analyzer ends on the evolution:
  672. | a -> {inita, +, 2 + initb, +, 1}_1
  673. */
  674. static tree
  675. add_to_evolution (unsigned loop_nb, tree chrec_before, enum tree_code code,
  676. tree to_add, gimple at_stmt)
  677. {
  678. tree type = chrec_type (to_add);
  679. tree res = NULL_TREE;
  680. if (to_add == NULL_TREE)
  681. return chrec_before;
  682. /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
  683. instantiated at this point. */
  684. if (TREE_CODE (to_add) == POLYNOMIAL_CHREC)
  685. /* This should not happen. */
  686. return chrec_dont_know;
  687. if (dump_file && (dump_flags & TDF_SCEV))
  688. {
  689. fprintf (dump_file, "(add_to_evolution \n");
  690. fprintf (dump_file, " (loop_nb = %d)\n", loop_nb);
  691. fprintf (dump_file, " (chrec_before = ");
  692. print_generic_expr (dump_file, chrec_before, 0);
  693. fprintf (dump_file, ")\n (to_add = ");
  694. print_generic_expr (dump_file, to_add, 0);
  695. fprintf (dump_file, ")\n");
  696. }
  697. if (code == MINUS_EXPR)
  698. to_add = chrec_fold_multiply (type, to_add, SCALAR_FLOAT_TYPE_P (type)
  699. ? build_real (type, dconstm1)
  700. : build_int_cst_type (type, -1));
  701. res = add_to_evolution_1 (loop_nb, chrec_before, to_add, at_stmt);
  702. if (dump_file && (dump_flags & TDF_SCEV))
  703. {
  704. fprintf (dump_file, " (res = ");
  705. print_generic_expr (dump_file, res, 0);
  706. fprintf (dump_file, "))\n");
  707. }
  708. return res;
  709. }
  710. /* This section selects the loops that will be good candidates for the
  711. scalar evolution analysis. For the moment, greedily select all the
  712. loop nests we could analyze. */
  713. /* For a loop with a single exit edge, return the COND_EXPR that
  714. guards the exit edge. If the expression is too difficult to
  715. analyze, then give up. */
  716. gcond *
  717. get_loop_exit_condition (const struct loop *loop)
  718. {
  719. gcond *res = NULL;
  720. edge exit_edge = single_exit (loop);
  721. if (dump_file && (dump_flags & TDF_SCEV))
  722. fprintf (dump_file, "(get_loop_exit_condition \n ");
  723. if (exit_edge)
  724. {
  725. gimple stmt;
  726. stmt = last_stmt (exit_edge->src);
  727. if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
  728. res = cond_stmt;
  729. }
  730. if (dump_file && (dump_flags & TDF_SCEV))
  731. {
  732. print_gimple_stmt (dump_file, res, 0, 0);
  733. fprintf (dump_file, ")\n");
  734. }
  735. return res;
  736. }
  737. /* Depth first search algorithm. */
  738. typedef enum t_bool {
  739. t_false,
  740. t_true,
  741. t_dont_know
  742. } t_bool;
  743. static t_bool follow_ssa_edge (struct loop *loop, gimple, gphi *,
  744. tree *, int);
  745. /* Follow the ssa edge into the binary expression RHS0 CODE RHS1.
  746. Return true if the strongly connected component has been found. */
  747. static t_bool
  748. follow_ssa_edge_binary (struct loop *loop, gimple at_stmt,
  749. tree type, tree rhs0, enum tree_code code, tree rhs1,
  750. gphi *halting_phi, tree *evolution_of_loop,
  751. int limit)
  752. {
  753. t_bool res = t_false;
  754. tree evol;
  755. switch (code)
  756. {
  757. case POINTER_PLUS_EXPR:
  758. case PLUS_EXPR:
  759. if (TREE_CODE (rhs0) == SSA_NAME)
  760. {
  761. if (TREE_CODE (rhs1) == SSA_NAME)
  762. {
  763. /* Match an assignment under the form:
  764. "a = b + c". */
  765. /* We want only assignments of form "name + name" contribute to
  766. LIMIT, as the other cases do not necessarily contribute to
  767. the complexity of the expression. */
  768. limit++;
  769. evol = *evolution_of_loop;
  770. evol = add_to_evolution
  771. (loop->num,
  772. chrec_convert (type, evol, at_stmt),
  773. code, rhs1, at_stmt);
  774. res = follow_ssa_edge
  775. (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi, &evol, limit);
  776. if (res == t_true)
  777. *evolution_of_loop = evol;
  778. else if (res == t_false)
  779. {
  780. *evolution_of_loop = add_to_evolution
  781. (loop->num,
  782. chrec_convert (type, *evolution_of_loop, at_stmt),
  783. code, rhs0, at_stmt);
  784. res = follow_ssa_edge
  785. (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
  786. evolution_of_loop, limit);
  787. if (res == t_true)
  788. ;
  789. else if (res == t_dont_know)
  790. *evolution_of_loop = chrec_dont_know;
  791. }
  792. else if (res == t_dont_know)
  793. *evolution_of_loop = chrec_dont_know;
  794. }
  795. else
  796. {
  797. /* Match an assignment under the form:
  798. "a = b + ...". */
  799. *evolution_of_loop = add_to_evolution
  800. (loop->num, chrec_convert (type, *evolution_of_loop,
  801. at_stmt),
  802. code, rhs1, at_stmt);
  803. res = follow_ssa_edge
  804. (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
  805. evolution_of_loop, limit);
  806. if (res == t_true)
  807. ;
  808. else if (res == t_dont_know)
  809. *evolution_of_loop = chrec_dont_know;
  810. }
  811. }
  812. else if (TREE_CODE (rhs1) == SSA_NAME)
  813. {
  814. /* Match an assignment under the form:
  815. "a = ... + c". */
  816. *evolution_of_loop = add_to_evolution
  817. (loop->num, chrec_convert (type, *evolution_of_loop,
  818. at_stmt),
  819. code, rhs0, at_stmt);
  820. res = follow_ssa_edge
  821. (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
  822. evolution_of_loop, limit);
  823. if (res == t_true)
  824. ;
  825. else if (res == t_dont_know)
  826. *evolution_of_loop = chrec_dont_know;
  827. }
  828. else
  829. /* Otherwise, match an assignment under the form:
  830. "a = ... + ...". */
  831. /* And there is nothing to do. */
  832. res = t_false;
  833. break;
  834. case MINUS_EXPR:
  835. /* This case is under the form "opnd0 = rhs0 - rhs1". */
  836. if (TREE_CODE (rhs0) == SSA_NAME)
  837. {
  838. /* Match an assignment under the form:
  839. "a = b - ...". */
  840. /* We want only assignments of form "name - name" contribute to
  841. LIMIT, as the other cases do not necessarily contribute to
  842. the complexity of the expression. */
  843. if (TREE_CODE (rhs1) == SSA_NAME)
  844. limit++;
  845. *evolution_of_loop = add_to_evolution
  846. (loop->num, chrec_convert (type, *evolution_of_loop, at_stmt),
  847. MINUS_EXPR, rhs1, at_stmt);
  848. res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
  849. evolution_of_loop, limit);
  850. if (res == t_true)
  851. ;
  852. else if (res == t_dont_know)
  853. *evolution_of_loop = chrec_dont_know;
  854. }
  855. else
  856. /* Otherwise, match an assignment under the form:
  857. "a = ... - ...". */
  858. /* And there is nothing to do. */
  859. res = t_false;
  860. break;
  861. default:
  862. res = t_false;
  863. }
  864. return res;
  865. }
  866. /* Follow the ssa edge into the expression EXPR.
  867. Return true if the strongly connected component has been found. */
  868. static t_bool
  869. follow_ssa_edge_expr (struct loop *loop, gimple at_stmt, tree expr,
  870. gphi *halting_phi, tree *evolution_of_loop,
  871. int limit)
  872. {
  873. enum tree_code code = TREE_CODE (expr);
  874. tree type = TREE_TYPE (expr), rhs0, rhs1;
  875. t_bool res;
  876. /* The EXPR is one of the following cases:
  877. - an SSA_NAME,
  878. - an INTEGER_CST,
  879. - a PLUS_EXPR,
  880. - a POINTER_PLUS_EXPR,
  881. - a MINUS_EXPR,
  882. - an ASSERT_EXPR,
  883. - other cases are not yet handled. */
  884. switch (code)
  885. {
  886. CASE_CONVERT:
  887. /* This assignment is under the form "a_1 = (cast) rhs. */
  888. res = follow_ssa_edge_expr (loop, at_stmt, TREE_OPERAND (expr, 0),
  889. halting_phi, evolution_of_loop, limit);
  890. *evolution_of_loop = chrec_convert (type, *evolution_of_loop, at_stmt);
  891. break;
  892. case INTEGER_CST:
  893. /* This assignment is under the form "a_1 = 7". */
  894. res = t_false;
  895. break;
  896. case SSA_NAME:
  897. /* This assignment is under the form: "a_1 = b_2". */
  898. res = follow_ssa_edge
  899. (loop, SSA_NAME_DEF_STMT (expr), halting_phi, evolution_of_loop, limit);
  900. break;
  901. case POINTER_PLUS_EXPR:
  902. case PLUS_EXPR:
  903. case MINUS_EXPR:
  904. /* This case is under the form "rhs0 +- rhs1". */
  905. rhs0 = TREE_OPERAND (expr, 0);
  906. rhs1 = TREE_OPERAND (expr, 1);
  907. type = TREE_TYPE (rhs0);
  908. STRIP_USELESS_TYPE_CONVERSION (rhs0);
  909. STRIP_USELESS_TYPE_CONVERSION (rhs1);
  910. res = follow_ssa_edge_binary (loop, at_stmt, type, rhs0, code, rhs1,
  911. halting_phi, evolution_of_loop, limit);
  912. break;
  913. case ADDR_EXPR:
  914. /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR. */
  915. if (TREE_CODE (TREE_OPERAND (expr, 0)) == MEM_REF)
  916. {
  917. expr = TREE_OPERAND (expr, 0);
  918. rhs0 = TREE_OPERAND (expr, 0);
  919. rhs1 = TREE_OPERAND (expr, 1);
  920. type = TREE_TYPE (rhs0);
  921. STRIP_USELESS_TYPE_CONVERSION (rhs0);
  922. STRIP_USELESS_TYPE_CONVERSION (rhs1);
  923. res = follow_ssa_edge_binary (loop, at_stmt, type,
  924. rhs0, POINTER_PLUS_EXPR, rhs1,
  925. halting_phi, evolution_of_loop, limit);
  926. }
  927. else
  928. res = t_false;
  929. break;
  930. case ASSERT_EXPR:
  931. /* This assignment is of the form: "a_1 = ASSERT_EXPR <a_2, ...>"
  932. It must be handled as a copy assignment of the form a_1 = a_2. */
  933. rhs0 = ASSERT_EXPR_VAR (expr);
  934. if (TREE_CODE (rhs0) == SSA_NAME)
  935. res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0),
  936. halting_phi, evolution_of_loop, limit);
  937. else
  938. res = t_false;
  939. break;
  940. default:
  941. res = t_false;
  942. break;
  943. }
  944. return res;
  945. }
  946. /* Follow the ssa edge into the right hand side of an assignment STMT.
  947. Return true if the strongly connected component has been found. */
  948. static t_bool
  949. follow_ssa_edge_in_rhs (struct loop *loop, gimple stmt,
  950. gphi *halting_phi, tree *evolution_of_loop,
  951. int limit)
  952. {
  953. enum tree_code code = gimple_assign_rhs_code (stmt);
  954. tree type = gimple_expr_type (stmt), rhs1, rhs2;
  955. t_bool res;
  956. switch (code)
  957. {
  958. CASE_CONVERT:
  959. /* This assignment is under the form "a_1 = (cast) rhs. */
  960. res = follow_ssa_edge_expr (loop, stmt, gimple_assign_rhs1 (stmt),
  961. halting_phi, evolution_of_loop, limit);
  962. *evolution_of_loop = chrec_convert (type, *evolution_of_loop, stmt);
  963. break;
  964. case POINTER_PLUS_EXPR:
  965. case PLUS_EXPR:
  966. case MINUS_EXPR:
  967. rhs1 = gimple_assign_rhs1 (stmt);
  968. rhs2 = gimple_assign_rhs2 (stmt);
  969. type = TREE_TYPE (rhs1);
  970. res = follow_ssa_edge_binary (loop, stmt, type, rhs1, code, rhs2,
  971. halting_phi, evolution_of_loop, limit);
  972. break;
  973. default:
  974. if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
  975. res = follow_ssa_edge_expr (loop, stmt, gimple_assign_rhs1 (stmt),
  976. halting_phi, evolution_of_loop, limit);
  977. else
  978. res = t_false;
  979. break;
  980. }
  981. return res;
  982. }
  983. /* Checks whether the I-th argument of a PHI comes from a backedge. */
  984. static bool
  985. backedge_phi_arg_p (gphi *phi, int i)
  986. {
  987. const_edge e = gimple_phi_arg_edge (phi, i);
  988. /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
  989. about updating it anywhere, and this should work as well most of the
  990. time. */
  991. if (e->flags & EDGE_IRREDUCIBLE_LOOP)
  992. return true;
  993. return false;
  994. }
  995. /* Helper function for one branch of the condition-phi-node. Return
  996. true if the strongly connected component has been found following
  997. this path. */
  998. static inline t_bool
  999. follow_ssa_edge_in_condition_phi_branch (int i,
  1000. struct loop *loop,
  1001. gphi *condition_phi,
  1002. gphi *halting_phi,
  1003. tree *evolution_of_branch,
  1004. tree init_cond, int limit)
  1005. {
  1006. tree branch = PHI_ARG_DEF (condition_phi, i);
  1007. *evolution_of_branch = chrec_dont_know;
  1008. /* Do not follow back edges (they must belong to an irreducible loop, which
  1009. we really do not want to worry about). */
  1010. if (backedge_phi_arg_p (condition_phi, i))
  1011. return t_false;
  1012. if (TREE_CODE (branch) == SSA_NAME)
  1013. {
  1014. *evolution_of_branch = init_cond;
  1015. return follow_ssa_edge (loop, SSA_NAME_DEF_STMT (branch), halting_phi,
  1016. evolution_of_branch, limit);
  1017. }
  1018. /* This case occurs when one of the condition branches sets
  1019. the variable to a constant: i.e. a phi-node like
  1020. "a_2 = PHI <a_7(5), 2(6)>;".
  1021. FIXME: This case have to be refined correctly:
  1022. in some cases it is possible to say something better than
  1023. chrec_dont_know, for example using a wrap-around notation. */
  1024. return t_false;
  1025. }
  1026. /* This function merges the branches of a condition-phi-node in a
  1027. loop. */
  1028. static t_bool
  1029. follow_ssa_edge_in_condition_phi (struct loop *loop,
  1030. gphi *condition_phi,
  1031. gphi *halting_phi,
  1032. tree *evolution_of_loop, int limit)
  1033. {
  1034. int i, n;
  1035. tree init = *evolution_of_loop;
  1036. tree evolution_of_branch;
  1037. t_bool res = follow_ssa_edge_in_condition_phi_branch (0, loop, condition_phi,
  1038. halting_phi,
  1039. &evolution_of_branch,
  1040. init, limit);
  1041. if (res == t_false || res == t_dont_know)
  1042. return res;
  1043. *evolution_of_loop = evolution_of_branch;
  1044. n = gimple_phi_num_args (condition_phi);
  1045. for (i = 1; i < n; i++)
  1046. {
  1047. /* Quickly give up when the evolution of one of the branches is
  1048. not known. */
  1049. if (*evolution_of_loop == chrec_dont_know)
  1050. return t_true;
  1051. /* Increase the limit by the PHI argument number to avoid exponential
  1052. time and memory complexity. */
  1053. res = follow_ssa_edge_in_condition_phi_branch (i, loop, condition_phi,
  1054. halting_phi,
  1055. &evolution_of_branch,
  1056. init, limit + i);
  1057. if (res == t_false || res == t_dont_know)
  1058. return res;
  1059. *evolution_of_loop = chrec_merge (*evolution_of_loop,
  1060. evolution_of_branch);
  1061. }
  1062. return t_true;
  1063. }
  1064. /* Follow an SSA edge in an inner loop. It computes the overall
  1065. effect of the loop, and following the symbolic initial conditions,
  1066. it follows the edges in the parent loop. The inner loop is
  1067. considered as a single statement. */
  1068. static t_bool
  1069. follow_ssa_edge_inner_loop_phi (struct loop *outer_loop,
  1070. gphi *loop_phi_node,
  1071. gphi *halting_phi,
  1072. tree *evolution_of_loop, int limit)
  1073. {
  1074. struct loop *loop = loop_containing_stmt (loop_phi_node);
  1075. tree ev = analyze_scalar_evolution (loop, PHI_RESULT (loop_phi_node));
  1076. /* Sometimes, the inner loop is too difficult to analyze, and the
  1077. result of the analysis is a symbolic parameter. */
  1078. if (ev == PHI_RESULT (loop_phi_node))
  1079. {
  1080. t_bool res = t_false;
  1081. int i, n = gimple_phi_num_args (loop_phi_node);
  1082. for (i = 0; i < n; i++)
  1083. {
  1084. tree arg = PHI_ARG_DEF (loop_phi_node, i);
  1085. basic_block bb;
  1086. /* Follow the edges that exit the inner loop. */
  1087. bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
  1088. if (!flow_bb_inside_loop_p (loop, bb))
  1089. res = follow_ssa_edge_expr (outer_loop, loop_phi_node,
  1090. arg, halting_phi,
  1091. evolution_of_loop, limit);
  1092. if (res == t_true)
  1093. break;
  1094. }
  1095. /* If the path crosses this loop-phi, give up. */
  1096. if (res == t_true)
  1097. *evolution_of_loop = chrec_dont_know;
  1098. return res;
  1099. }
  1100. /* Otherwise, compute the overall effect of the inner loop. */
  1101. ev = compute_overall_effect_of_inner_loop (loop, ev);
  1102. return follow_ssa_edge_expr (outer_loop, loop_phi_node, ev, halting_phi,
  1103. evolution_of_loop, limit);
  1104. }
  1105. /* Follow an SSA edge from a loop-phi-node to itself, constructing a
  1106. path that is analyzed on the return walk. */
  1107. static t_bool
  1108. follow_ssa_edge (struct loop *loop, gimple def, gphi *halting_phi,
  1109. tree *evolution_of_loop, int limit)
  1110. {
  1111. struct loop *def_loop;
  1112. if (gimple_nop_p (def))
  1113. return t_false;
  1114. /* Give up if the path is longer than the MAX that we allow. */
  1115. if (limit > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_COMPLEXITY))
  1116. return t_dont_know;
  1117. def_loop = loop_containing_stmt (def);
  1118. switch (gimple_code (def))
  1119. {
  1120. case GIMPLE_PHI:
  1121. if (!loop_phi_node_p (def))
  1122. /* DEF is a condition-phi-node. Follow the branches, and
  1123. record their evolutions. Finally, merge the collected
  1124. information and set the approximation to the main
  1125. variable. */
  1126. return follow_ssa_edge_in_condition_phi
  1127. (loop, as_a <gphi *> (def), halting_phi, evolution_of_loop,
  1128. limit);
  1129. /* When the analyzed phi is the halting_phi, the
  1130. depth-first search is over: we have found a path from
  1131. the halting_phi to itself in the loop. */
  1132. if (def == halting_phi)
  1133. return t_true;
  1134. /* Otherwise, the evolution of the HALTING_PHI depends
  1135. on the evolution of another loop-phi-node, i.e. the
  1136. evolution function is a higher degree polynomial. */
  1137. if (def_loop == loop)
  1138. return t_false;
  1139. /* Inner loop. */
  1140. if (flow_loop_nested_p (loop, def_loop))
  1141. return follow_ssa_edge_inner_loop_phi
  1142. (loop, as_a <gphi *> (def), halting_phi, evolution_of_loop,
  1143. limit + 1);
  1144. /* Outer loop. */
  1145. return t_false;
  1146. case GIMPLE_ASSIGN:
  1147. return follow_ssa_edge_in_rhs (loop, def, halting_phi,
  1148. evolution_of_loop, limit);
  1149. default:
  1150. /* At this level of abstraction, the program is just a set
  1151. of GIMPLE_ASSIGNs and PHI_NODEs. In principle there is no
  1152. other node to be handled. */
  1153. return t_false;
  1154. }
  1155. }
  1156. /* Simplify PEELED_CHREC represented by (init_cond, arg) in LOOP.
  1157. Handle below case and return the corresponding POLYNOMIAL_CHREC:
  1158. # i_17 = PHI <i_13(5), 0(3)>
  1159. # _20 = PHI <_5(5), start_4(D)(3)>
  1160. ...
  1161. i_13 = i_17 + 1;
  1162. _5 = start_4(D) + i_13;
  1163. Though variable _20 appears as a PEELED_CHREC in the form of
  1164. (start_4, _5)_LOOP, it's a POLYNOMIAL_CHREC like {start_4, 1}_LOOP.
  1165. See PR41488. */
  1166. static tree
  1167. simplify_peeled_chrec (struct loop *loop, tree arg, tree init_cond)
  1168. {
  1169. aff_tree aff1, aff2;
  1170. tree ev, left, right, type, step_val;
  1171. hash_map<tree, name_expansion *> *peeled_chrec_map = NULL;
  1172. ev = instantiate_parameters (loop, analyze_scalar_evolution (loop, arg));
  1173. if (ev == NULL_TREE || TREE_CODE (ev) != POLYNOMIAL_CHREC)
  1174. return chrec_dont_know;
  1175. left = CHREC_LEFT (ev);
  1176. right = CHREC_RIGHT (ev);
  1177. type = TREE_TYPE (left);
  1178. step_val = chrec_fold_plus (type, init_cond, right);
  1179. /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
  1180. if "left" equals to "init + right". */
  1181. if (operand_equal_p (left, step_val, 0))
  1182. {
  1183. if (dump_file && (dump_flags & TDF_SCEV))
  1184. fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
  1185. return build_polynomial_chrec (loop->num, init_cond, right);
  1186. }
  1187. /* Try harder to check if they are equal. */
  1188. tree_to_aff_combination_expand (left, type, &aff1, &peeled_chrec_map);
  1189. tree_to_aff_combination_expand (step_val, type, &aff2, &peeled_chrec_map);
  1190. free_affine_expand_cache (&peeled_chrec_map);
  1191. aff_combination_scale (&aff2, -1);
  1192. aff_combination_add (&aff1, &aff2);
  1193. /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
  1194. if "left" equals to "init + right". */
  1195. if (aff_combination_zero_p (&aff1))
  1196. {
  1197. if (dump_file && (dump_flags & TDF_SCEV))
  1198. fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
  1199. return build_polynomial_chrec (loop->num, init_cond, right);
  1200. }
  1201. return chrec_dont_know;
  1202. }
  1203. /* Given a LOOP_PHI_NODE, this function determines the evolution
  1204. function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
  1205. static tree
  1206. analyze_evolution_in_loop (gphi *loop_phi_node,
  1207. tree init_cond)
  1208. {
  1209. int i, n = gimple_phi_num_args (loop_phi_node);
  1210. tree evolution_function = chrec_not_analyzed_yet;
  1211. struct loop *loop = loop_containing_stmt (loop_phi_node);
  1212. basic_block bb;
  1213. static bool simplify_peeled_chrec_p = true;
  1214. if (dump_file && (dump_flags & TDF_SCEV))
  1215. {
  1216. fprintf (dump_file, "(analyze_evolution_in_loop \n");
  1217. fprintf (dump_file, " (loop_phi_node = ");
  1218. print_gimple_stmt (dump_file, loop_phi_node, 0, 0);
  1219. fprintf (dump_file, ")\n");
  1220. }
  1221. for (i = 0; i < n; i++)
  1222. {
  1223. tree arg = PHI_ARG_DEF (loop_phi_node, i);
  1224. gimple ssa_chain;
  1225. tree ev_fn;
  1226. t_bool res;
  1227. /* Select the edges that enter the loop body. */
  1228. bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
  1229. if (!flow_bb_inside_loop_p (loop, bb))
  1230. continue;
  1231. if (TREE_CODE (arg) == SSA_NAME)
  1232. {
  1233. bool val = false;
  1234. ssa_chain = SSA_NAME_DEF_STMT (arg);
  1235. /* Pass in the initial condition to the follow edge function. */
  1236. ev_fn = init_cond;
  1237. res = follow_ssa_edge (loop, ssa_chain, loop_phi_node, &ev_fn, 0);
  1238. /* If ev_fn has no evolution in the inner loop, and the
  1239. init_cond is not equal to ev_fn, then we have an
  1240. ambiguity between two possible values, as we cannot know
  1241. the number of iterations at this point. */
  1242. if (TREE_CODE (ev_fn) != POLYNOMIAL_CHREC
  1243. && no_evolution_in_loop_p (ev_fn, loop->num, &val) && val
  1244. && !operand_equal_p (init_cond, ev_fn, 0))
  1245. ev_fn = chrec_dont_know;
  1246. }
  1247. else
  1248. res = t_false;
  1249. /* When it is impossible to go back on the same
  1250. loop_phi_node by following the ssa edges, the
  1251. evolution is represented by a peeled chrec, i.e. the
  1252. first iteration, EV_FN has the value INIT_COND, then
  1253. all the other iterations it has the value of ARG.
  1254. For the moment, PEELED_CHREC nodes are not built. */
  1255. if (res != t_true)
  1256. {
  1257. ev_fn = chrec_dont_know;
  1258. /* Try to recognize POLYNOMIAL_CHREC which appears in
  1259. the form of PEELED_CHREC, but guard the process with
  1260. a bool variable to keep the analyzer from infinite
  1261. recurrence for real PEELED_RECs. */
  1262. if (simplify_peeled_chrec_p && TREE_CODE (arg) == SSA_NAME)
  1263. {
  1264. simplify_peeled_chrec_p = false;
  1265. ev_fn = simplify_peeled_chrec (loop, arg, init_cond);
  1266. simplify_peeled_chrec_p = true;
  1267. }
  1268. }
  1269. /* When there are multiple back edges of the loop (which in fact never
  1270. happens currently, but nevertheless), merge their evolutions. */
  1271. evolution_function = chrec_merge (evolution_function, ev_fn);
  1272. }
  1273. if (dump_file && (dump_flags & TDF_SCEV))
  1274. {
  1275. fprintf (dump_file, " (evolution_function = ");
  1276. print_generic_expr (dump_file, evolution_function, 0);
  1277. fprintf (dump_file, "))\n");
  1278. }
  1279. return evolution_function;
  1280. }
  1281. /* Given a loop-phi-node, return the initial conditions of the
  1282. variable on entry of the loop. When the CCP has propagated
  1283. constants into the loop-phi-node, the initial condition is
  1284. instantiated, otherwise the initial condition is kept symbolic.
  1285. This analyzer does not analyze the evolution outside the current
  1286. loop, and leaves this task to the on-demand tree reconstructor. */
  1287. static tree
  1288. analyze_initial_condition (gphi *loop_phi_node)
  1289. {
  1290. int i, n;
  1291. tree init_cond = chrec_not_analyzed_yet;
  1292. struct loop *loop = loop_containing_stmt (loop_phi_node);
  1293. if (dump_file && (dump_flags & TDF_SCEV))
  1294. {
  1295. fprintf (dump_file, "(analyze_initial_condition \n");
  1296. fprintf (dump_file, " (loop_phi_node = \n");
  1297. print_gimple_stmt (dump_file, loop_phi_node, 0, 0);
  1298. fprintf (dump_file, ")\n");
  1299. }
  1300. n = gimple_phi_num_args (loop_phi_node);
  1301. for (i = 0; i < n; i++)
  1302. {
  1303. tree branch = PHI_ARG_DEF (loop_phi_node, i);
  1304. basic_block bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
  1305. /* When the branch is oriented to the loop's body, it does
  1306. not contribute to the initial condition. */
  1307. if (flow_bb_inside_loop_p (loop, bb))
  1308. continue;
  1309. if (init_cond == chrec_not_analyzed_yet)
  1310. {
  1311. init_cond = branch;
  1312. continue;
  1313. }
  1314. if (TREE_CODE (branch) == SSA_NAME)
  1315. {
  1316. init_cond = chrec_dont_know;
  1317. break;
  1318. }
  1319. init_cond = chrec_merge (init_cond, branch);
  1320. }
  1321. /* Ooops -- a loop without an entry??? */
  1322. if (init_cond == chrec_not_analyzed_yet)
  1323. init_cond = chrec_dont_know;
  1324. /* During early loop unrolling we do not have fully constant propagated IL.
  1325. Handle degenerate PHIs here to not miss important unrollings. */
  1326. if (TREE_CODE (init_cond) == SSA_NAME)
  1327. {
  1328. gimple def = SSA_NAME_DEF_STMT (init_cond);
  1329. if (gphi *phi = dyn_cast <gphi *> (def))
  1330. {
  1331. tree res = degenerate_phi_result (phi);
  1332. if (res != NULL_TREE
  1333. /* Only allow invariants here, otherwise we may break
  1334. loop-closed SSA form. */
  1335. && is_gimple_min_invariant (res))
  1336. init_cond = res;
  1337. }
  1338. }
  1339. if (dump_file && (dump_flags & TDF_SCEV))
  1340. {
  1341. fprintf (dump_file, " (init_cond = ");
  1342. print_generic_expr (dump_file, init_cond, 0);
  1343. fprintf (dump_file, "))\n");
  1344. }
  1345. return init_cond;
  1346. }
  1347. /* Analyze the scalar evolution for LOOP_PHI_NODE. */
  1348. static tree
  1349. interpret_loop_phi (struct loop *loop, gphi *loop_phi_node)
  1350. {
  1351. tree res;
  1352. struct loop *phi_loop = loop_containing_stmt (loop_phi_node);
  1353. tree init_cond;
  1354. if (phi_loop != loop)
  1355. {
  1356. struct loop *subloop;
  1357. tree evolution_fn = analyze_scalar_evolution
  1358. (phi_loop, PHI_RESULT (loop_phi_node));
  1359. /* Dive one level deeper. */
  1360. subloop = superloop_at_depth (phi_loop, loop_depth (loop) + 1);
  1361. /* Interpret the subloop. */
  1362. res = compute_overall_effect_of_inner_loop (subloop, evolution_fn);
  1363. return res;
  1364. }
  1365. /* Otherwise really interpret the loop phi. */
  1366. init_cond = analyze_initial_condition (loop_phi_node);
  1367. res = analyze_evolution_in_loop (loop_phi_node, init_cond);
  1368. /* Verify we maintained the correct initial condition throughout
  1369. possible conversions in the SSA chain. */
  1370. if (res != chrec_dont_know)
  1371. {
  1372. tree new_init = res;
  1373. if (CONVERT_EXPR_P (res)
  1374. && TREE_CODE (TREE_OPERAND (res, 0)) == POLYNOMIAL_CHREC)
  1375. new_init = fold_convert (TREE_TYPE (res),
  1376. CHREC_LEFT (TREE_OPERAND (res, 0)));
  1377. else if (TREE_CODE (res) == POLYNOMIAL_CHREC)
  1378. new_init = CHREC_LEFT (res);
  1379. STRIP_USELESS_TYPE_CONVERSION (new_init);
  1380. if (TREE_CODE (new_init) == POLYNOMIAL_CHREC
  1381. || !operand_equal_p (init_cond, new_init, 0))
  1382. return chrec_dont_know;
  1383. }
  1384. return res;
  1385. }
  1386. /* This function merges the branches of a condition-phi-node,
  1387. contained in the outermost loop, and whose arguments are already
  1388. analyzed. */
  1389. static tree
  1390. interpret_condition_phi (struct loop *loop, gphi *condition_phi)
  1391. {
  1392. int i, n = gimple_phi_num_args (condition_phi);
  1393. tree res = chrec_not_analyzed_yet;
  1394. for (i = 0; i < n; i++)
  1395. {
  1396. tree branch_chrec;
  1397. if (backedge_phi_arg_p (condition_phi, i))
  1398. {
  1399. res = chrec_dont_know;
  1400. break;
  1401. }
  1402. branch_chrec = analyze_scalar_evolution
  1403. (loop, PHI_ARG_DEF (condition_phi, i));
  1404. res = chrec_merge (res, branch_chrec);
  1405. }
  1406. return res;
  1407. }
  1408. /* Interpret the operation RHS1 OP RHS2. If we didn't
  1409. analyze this node before, follow the definitions until ending
  1410. either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node. On the
  1411. return path, this function propagates evolutions (ala constant copy
  1412. propagation). OPND1 is not a GIMPLE expression because we could
  1413. analyze the effect of an inner loop: see interpret_loop_phi. */
  1414. static tree
  1415. interpret_rhs_expr (struct loop *loop, gimple at_stmt,
  1416. tree type, tree rhs1, enum tree_code code, tree rhs2)
  1417. {
  1418. tree res, chrec1, chrec2;
  1419. gimple def;
  1420. if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
  1421. {
  1422. if (is_gimple_min_invariant (rhs1))
  1423. return chrec_convert (type, rhs1, at_stmt);
  1424. if (code == SSA_NAME)
  1425. return chrec_convert (type, analyze_scalar_evolution (loop, rhs1),
  1426. at_stmt);
  1427. if (code == ASSERT_EXPR)
  1428. {
  1429. rhs1 = ASSERT_EXPR_VAR (rhs1);
  1430. return chrec_convert (type, analyze_scalar_evolution (loop, rhs1),
  1431. at_stmt);
  1432. }
  1433. }
  1434. switch (code)
  1435. {
  1436. case ADDR_EXPR:
  1437. if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF
  1438. || handled_component_p (TREE_OPERAND (rhs1, 0)))
  1439. {
  1440. machine_mode mode;
  1441. HOST_WIDE_INT bitsize, bitpos;
  1442. int unsignedp;
  1443. int volatilep = 0;
  1444. tree base, offset;
  1445. tree chrec3;
  1446. tree unitpos;
  1447. base = get_inner_reference (TREE_OPERAND (rhs1, 0),
  1448. &bitsize, &bitpos, &offset,
  1449. &mode, &unsignedp, &volatilep, false);
  1450. if (TREE_CODE (base) == MEM_REF)
  1451. {
  1452. rhs2 = TREE_OPERAND (base, 1);
  1453. rhs1 = TREE_OPERAND (base, 0);
  1454. chrec1 = analyze_scalar_evolution (loop, rhs1);
  1455. chrec2 = analyze_scalar_evolution (loop, rhs2);
  1456. chrec1 = chrec_convert (type, chrec1, at_stmt);
  1457. chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
  1458. chrec1 = instantiate_parameters (loop, chrec1);
  1459. chrec2 = instantiate_parameters (loop, chrec2);
  1460. res = chrec_fold_plus (type, chrec1, chrec2);
  1461. }
  1462. else
  1463. {
  1464. chrec1 = analyze_scalar_evolution_for_address_of (loop, base);
  1465. chrec1 = chrec_convert (type, chrec1, at_stmt);
  1466. res = chrec1;
  1467. }
  1468. if (offset != NULL_TREE)
  1469. {
  1470. chrec2 = analyze_scalar_evolution (loop, offset);
  1471. chrec2 = chrec_convert (TREE_TYPE (offset), chrec2, at_stmt);
  1472. chrec2 = instantiate_parameters (loop, chrec2);
  1473. res = chrec_fold_plus (type, res, chrec2);
  1474. }
  1475. if (bitpos != 0)
  1476. {
  1477. gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
  1478. unitpos = size_int (bitpos / BITS_PER_UNIT);
  1479. chrec3 = analyze_scalar_evolution (loop, unitpos);
  1480. chrec3 = chrec_convert (TREE_TYPE (unitpos), chrec3, at_stmt);
  1481. chrec3 = instantiate_parameters (loop, chrec3);
  1482. res = chrec_fold_plus (type, res, chrec3);
  1483. }
  1484. }
  1485. else
  1486. res = chrec_dont_know;
  1487. break;
  1488. case POINTER_PLUS_EXPR:
  1489. chrec1 = analyze_scalar_evolution (loop, rhs1);
  1490. chrec2 = analyze_scalar_evolution (loop, rhs2);
  1491. chrec1 = chrec_convert (type, chrec1, at_stmt);
  1492. chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
  1493. chrec1 = instantiate_parameters (loop, chrec1);
  1494. chrec2 = instantiate_parameters (loop, chrec2);
  1495. res = chrec_fold_plus (type, chrec1, chrec2);
  1496. break;
  1497. case PLUS_EXPR:
  1498. chrec1 = analyze_scalar_evolution (loop, rhs1);
  1499. chrec2 = analyze_scalar_evolution (loop, rhs2);
  1500. chrec1 = chrec_convert (type, chrec1, at_stmt);
  1501. chrec2 = chrec_convert (type, chrec2, at_stmt);
  1502. chrec1 = instantiate_parameters (loop, chrec1);
  1503. chrec2 = instantiate_parameters (loop, chrec2);
  1504. res = chrec_fold_plus (type, chrec1, chrec2);
  1505. break;
  1506. case MINUS_EXPR:
  1507. chrec1 = analyze_scalar_evolution (loop, rhs1);
  1508. chrec2 = analyze_scalar_evolution (loop, rhs2);
  1509. chrec1 = chrec_convert (type, chrec1, at_stmt);
  1510. chrec2 = chrec_convert (type, chrec2, at_stmt);
  1511. chrec1 = instantiate_parameters (loop, chrec1);
  1512. chrec2 = instantiate_parameters (loop, chrec2);
  1513. res = chrec_fold_minus (type, chrec1, chrec2);
  1514. break;
  1515. case NEGATE_EXPR:
  1516. chrec1 = analyze_scalar_evolution (loop, rhs1);
  1517. chrec1 = chrec_convert (type, chrec1, at_stmt);
  1518. /* TYPE may be integer, real or complex, so use fold_convert. */
  1519. chrec1 = instantiate_parameters (loop, chrec1);
  1520. res = chrec_fold_multiply (type, chrec1,
  1521. fold_convert (type, integer_minus_one_node));
  1522. break;
  1523. case BIT_NOT_EXPR:
  1524. /* Handle ~X as -1 - X. */
  1525. chrec1 = analyze_scalar_evolution (loop, rhs1);
  1526. chrec1 = chrec_convert (type, chrec1, at_stmt);
  1527. chrec1 = instantiate_parameters (loop, chrec1);
  1528. res = chrec_fold_minus (type,
  1529. fold_convert (type, integer_minus_one_node),
  1530. chrec1);
  1531. break;
  1532. case MULT_EXPR:
  1533. chrec1 = analyze_scalar_evolution (loop, rhs1);
  1534. chrec2 = analyze_scalar_evolution (loop, rhs2);
  1535. chrec1 = chrec_convert (type, chrec1, at_stmt);
  1536. chrec2 = chrec_convert (type, chrec2, at_stmt);
  1537. chrec1 = instantiate_parameters (loop, chrec1);
  1538. chrec2 = instantiate_parameters (loop, chrec2);
  1539. res = chrec_fold_multiply (type, chrec1, chrec2);
  1540. break;
  1541. CASE_CONVERT:
  1542. /* In case we have a truncation of a widened operation that in
  1543. the truncated type has undefined overflow behavior analyze
  1544. the operation done in an unsigned type of the same precision
  1545. as the final truncation. We cannot derive a scalar evolution
  1546. for the widened operation but for the truncated result. */
  1547. if (TREE_CODE (type) == INTEGER_TYPE
  1548. && TREE_CODE (TREE_TYPE (rhs1)) == INTEGER_TYPE
  1549. && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (rhs1))
  1550. && TYPE_OVERFLOW_UNDEFINED (type)
  1551. && TREE_CODE (rhs1) == SSA_NAME
  1552. && (def = SSA_NAME_DEF_STMT (rhs1))
  1553. && is_gimple_assign (def)
  1554. && TREE_CODE_CLASS (gimple_assign_rhs_code (def)) == tcc_binary
  1555. && TREE_CODE (gimple_assign_rhs2 (def)) == INTEGER_CST)
  1556. {
  1557. tree utype = unsigned_type_for (type);
  1558. chrec1 = interpret_rhs_expr (loop, at_stmt, utype,
  1559. gimple_assign_rhs1 (def),
  1560. gimple_assign_rhs_code (def),
  1561. gimple_assign_rhs2 (def));
  1562. }
  1563. else
  1564. chrec1 = analyze_scalar_evolution (loop, rhs1);
  1565. res = chrec_convert (type, chrec1, at_stmt);
  1566. break;
  1567. default:
  1568. res = chrec_dont_know;
  1569. break;
  1570. }
  1571. return res;
  1572. }
  1573. /* Interpret the expression EXPR. */
  1574. static tree
  1575. interpret_expr (struct loop *loop, gimple at_stmt, tree expr)
  1576. {
  1577. enum tree_code code;
  1578. tree type = TREE_TYPE (expr), op0, op1;
  1579. if (automatically_generated_chrec_p (expr))
  1580. return expr;
  1581. if (TREE_CODE (expr) == POLYNOMIAL_CHREC
  1582. || get_gimple_rhs_class (TREE_CODE (expr)) == GIMPLE_TERNARY_RHS)
  1583. return chrec_dont_know;
  1584. extract_ops_from_tree (expr, &code, &op0, &op1);
  1585. return interpret_rhs_expr (loop, at_stmt, type,
  1586. op0, code, op1);
  1587. }
  1588. /* Interpret the rhs of the assignment STMT. */
  1589. static tree
  1590. interpret_gimple_assign (struct loop *loop, gimple stmt)
  1591. {
  1592. tree type = TREE_TYPE (gimple_assign_lhs (stmt));
  1593. enum tree_code code = gimple_assign_rhs_code (stmt);
  1594. return interpret_rhs_expr (loop, stmt, type,
  1595. gimple_assign_rhs1 (stmt), code,
  1596. gimple_assign_rhs2 (stmt));
  1597. }
  1598. /* This section contains all the entry points:
  1599. - number_of_iterations_in_loop,
  1600. - analyze_scalar_evolution,
  1601. - instantiate_parameters.
  1602. */
  1603. /* Compute and return the evolution function in WRTO_LOOP, the nearest
  1604. common ancestor of DEF_LOOP and USE_LOOP. */
  1605. static tree
  1606. compute_scalar_evolution_in_loop (struct loop *wrto_loop,
  1607. struct loop *def_loop,
  1608. tree ev)
  1609. {
  1610. bool val;
  1611. tree res;
  1612. if (def_loop == wrto_loop)
  1613. return ev;
  1614. def_loop = superloop_at_depth (def_loop, loop_depth (wrto_loop) + 1);
  1615. res = compute_overall_effect_of_inner_loop (def_loop, ev);
  1616. if (no_evolution_in_loop_p (res, wrto_loop->num, &val) && val)
  1617. return res;
  1618. return analyze_scalar_evolution_1 (wrto_loop, res, chrec_not_analyzed_yet);
  1619. }
  1620. /* Helper recursive function. */
  1621. static tree
  1622. analyze_scalar_evolution_1 (struct loop *loop, tree var, tree res)
  1623. {
  1624. tree type = TREE_TYPE (var);
  1625. gimple def;
  1626. basic_block bb;
  1627. struct loop *def_loop;
  1628. if (loop == NULL || TREE_CODE (type) == VECTOR_TYPE)
  1629. return chrec_dont_know;
  1630. if (TREE_CODE (var) != SSA_NAME)
  1631. return interpret_expr (loop, NULL, var);
  1632. def = SSA_NAME_DEF_STMT (var);
  1633. bb = gimple_bb (def);
  1634. def_loop = bb ? bb->loop_father : NULL;
  1635. if (bb == NULL
  1636. || !flow_bb_inside_loop_p (loop, bb))
  1637. {
  1638. /* Keep the symbolic form. */
  1639. res = var;
  1640. goto set_and_end;
  1641. }
  1642. if (res != chrec_not_analyzed_yet)
  1643. {
  1644. if (loop != bb->loop_father)
  1645. res = compute_scalar_evolution_in_loop
  1646. (find_common_loop (loop, bb->loop_father), bb->loop_father, res);
  1647. goto set_and_end;
  1648. }
  1649. if (loop != def_loop)
  1650. {
  1651. res = analyze_scalar_evolution_1 (def_loop, var, chrec_not_analyzed_yet);
  1652. res = compute_scalar_evolution_in_loop (loop, def_loop, res);
  1653. goto set_and_end;
  1654. }
  1655. switch (gimple_code (def))
  1656. {
  1657. case GIMPLE_ASSIGN:
  1658. res = interpret_gimple_assign (loop, def);
  1659. break;
  1660. case GIMPLE_PHI:
  1661. if (loop_phi_node_p (def))
  1662. res = interpret_loop_phi (loop, as_a <gphi *> (def));
  1663. else
  1664. res = interpret_condition_phi (loop, as_a <gphi *> (def));
  1665. break;
  1666. default:
  1667. res = chrec_dont_know;
  1668. break;
  1669. }
  1670. set_and_end:
  1671. /* Keep the symbolic form. */
  1672. if (res == chrec_dont_know)
  1673. res = var;
  1674. if (loop == def_loop)
  1675. set_scalar_evolution (block_before_loop (loop), var, res);
  1676. return res;
  1677. }
  1678. /* Analyzes and returns the scalar evolution of the ssa_name VAR in
  1679. LOOP. LOOP is the loop in which the variable is used.
  1680. Example of use: having a pointer VAR to a SSA_NAME node, STMT a
  1681. pointer to the statement that uses this variable, in order to
  1682. determine the evolution function of the variable, use the following
  1683. calls:
  1684. loop_p loop = loop_containing_stmt (stmt);
  1685. tree chrec_with_symbols = analyze_scalar_evolution (loop, var);
  1686. tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
  1687. */
  1688. tree
  1689. analyze_scalar_evolution (struct loop *loop, tree var)
  1690. {
  1691. tree res;
  1692. if (dump_file && (dump_flags & TDF_SCEV))
  1693. {
  1694. fprintf (dump_file, "(analyze_scalar_evolution \n");
  1695. fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
  1696. fprintf (dump_file, " (scalar = ");
  1697. print_generic_expr (dump_file, var, 0);
  1698. fprintf (dump_file, ")\n");
  1699. }
  1700. res = get_scalar_evolution (block_before_loop (loop), var);
  1701. res = analyze_scalar_evolution_1 (loop, var, res);
  1702. if (dump_file && (dump_flags & TDF_SCEV))
  1703. fprintf (dump_file, ")\n");
  1704. return res;
  1705. }
  1706. /* Analyzes and returns the scalar evolution of VAR address in LOOP. */
  1707. static tree
  1708. analyze_scalar_evolution_for_address_of (struct loop *loop, tree var)
  1709. {
  1710. return analyze_scalar_evolution (loop, build_fold_addr_expr (var));
  1711. }
  1712. /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
  1713. WRTO_LOOP (which should be a superloop of USE_LOOP)
  1714. FOLDED_CASTS is set to true if resolve_mixers used
  1715. chrec_convert_aggressive (TODO -- not really, we are way too conservative
  1716. at the moment in order to keep things simple).
  1717. To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
  1718. example:
  1719. for (i = 0; i < 100; i++) -- loop 1
  1720. {
  1721. for (j = 0; j < 100; j++) -- loop 2
  1722. {
  1723. k1 = i;
  1724. k2 = j;
  1725. use2 (k1, k2);
  1726. for (t = 0; t < 100; t++) -- loop 3
  1727. use3 (k1, k2);
  1728. }
  1729. use1 (k1, k2);
  1730. }
  1731. Both k1 and k2 are invariants in loop3, thus
  1732. analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
  1733. analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
  1734. As they are invariant, it does not matter whether we consider their
  1735. usage in loop 3 or loop 2, hence
  1736. analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
  1737. analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
  1738. analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
  1739. analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
  1740. Similarly for their evolutions with respect to loop 1. The values of K2
  1741. in the use in loop 2 vary independently on loop 1, thus we cannot express
  1742. the evolution with respect to loop 1:
  1743. analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
  1744. analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
  1745. analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
  1746. analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
  1747. The value of k2 in the use in loop 1 is known, though:
  1748. analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
  1749. analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
  1750. */
  1751. static tree
  1752. analyze_scalar_evolution_in_loop (struct loop *wrto_loop, struct loop *use_loop,
  1753. tree version, bool *folded_casts)
  1754. {
  1755. bool val = false;
  1756. tree ev = version, tmp;
  1757. /* We cannot just do
  1758. tmp = analyze_scalar_evolution (use_loop, version);
  1759. ev = resolve_mixers (wrto_loop, tmp);
  1760. as resolve_mixers would query the scalar evolution with respect to
  1761. wrto_loop. For example, in the situation described in the function
  1762. comment, suppose that wrto_loop = loop1, use_loop = loop3 and
  1763. version = k2. Then
  1764. analyze_scalar_evolution (use_loop, version) = k2
  1765. and resolve_mixers (loop1, k2) finds that the value of k2 in loop 1
  1766. is 100, which is a wrong result, since we are interested in the
  1767. value in loop 3.
  1768. Instead, we need to proceed from use_loop to wrto_loop loop by loop,
  1769. each time checking that there is no evolution in the inner loop. */
  1770. if (folded_casts)
  1771. *folded_casts = false;
  1772. while (1)
  1773. {
  1774. tmp = analyze_scalar_evolution (use_loop, ev);
  1775. ev = resolve_mixers (use_loop, tmp);
  1776. if (folded_casts && tmp != ev)
  1777. *folded_casts = true;
  1778. if (use_loop == wrto_loop)
  1779. return ev;
  1780. /* If the value of the use changes in the inner loop, we cannot express
  1781. its value in the outer loop (we might try to return interval chrec,
  1782. but we do not have a user for it anyway) */
  1783. if (!no_evolution_in_loop_p (ev, use_loop->num, &val)
  1784. || !val)
  1785. return chrec_dont_know;
  1786. use_loop = loop_outer (use_loop);
  1787. }
  1788. }
  1789. /* Hashtable helpers for a temporary hash-table used when
  1790. instantiating a CHREC or resolving mixers. For this use
  1791. instantiated_below is always the same. */
  1792. struct instantiate_cache_type
  1793. {
  1794. htab_t map;
  1795. vec<scev_info_str> entries;
  1796. instantiate_cache_type () : map (NULL), entries (vNULL) {}
  1797. ~instantiate_cache_type ();
  1798. tree get (unsigned slot) { return entries[slot].chrec; }
  1799. void set (unsigned slot, tree chrec) { entries[slot].chrec = chrec; }
  1800. };
  1801. instantiate_cache_type::~instantiate_cache_type ()
  1802. {
  1803. if (map != NULL)
  1804. {
  1805. htab_delete (map);
  1806. entries.release ();
  1807. }
  1808. }
  1809. /* Cache to avoid infinite recursion when instantiating an SSA name.
  1810. Live during the outermost instantiate_scev or resolve_mixers call. */
  1811. static instantiate_cache_type *global_cache;
  1812. /* Computes a hash function for database element ELT. */
  1813. static inline hashval_t
  1814. hash_idx_scev_info (const void *elt_)
  1815. {
  1816. unsigned idx = ((size_t) elt_) - 2;
  1817. return scev_info_hasher::hash (&global_cache->entries[idx]);
  1818. }
  1819. /* Compares database elements E1 and E2. */
  1820. static inline int
  1821. eq_idx_scev_info (const void *e1, const void *e2)
  1822. {
  1823. unsigned idx1 = ((size_t) e1) - 2;
  1824. return scev_info_hasher::equal (&global_cache->entries[idx1],
  1825. (const scev_info_str *) e2);
  1826. }
  1827. /* Returns from CACHE the slot number of the cached chrec for NAME. */
  1828. static unsigned
  1829. get_instantiated_value_entry (instantiate_cache_type &cache,
  1830. tree name, basic_block instantiate_below)
  1831. {
  1832. if (!cache.map)
  1833. {
  1834. cache.map = htab_create (10, hash_idx_scev_info, eq_idx_scev_info, NULL);
  1835. cache.entries.create (10);
  1836. }
  1837. scev_info_str e;
  1838. e.name_version = SSA_NAME_VERSION (name);
  1839. e.instantiated_below = instantiate_below->index;
  1840. void **slot = htab_find_slot_with_hash (cache.map, &e,
  1841. scev_info_hasher::hash (&e), INSERT);
  1842. if (!*slot)
  1843. {
  1844. e.chrec = chrec_not_analyzed_yet;
  1845. *slot = (void *)(size_t)(cache.entries.length () + 2);
  1846. cache.entries.safe_push (e);
  1847. }
  1848. return ((size_t)*slot) - 2;
  1849. }
  1850. /* Return the closed_loop_phi node for VAR. If there is none, return
  1851. NULL_TREE. */
  1852. static tree
  1853. loop_closed_phi_def (tree var)
  1854. {
  1855. struct loop *loop;
  1856. edge exit;
  1857. gphi *phi;
  1858. gphi_iterator psi;
  1859. if (var == NULL_TREE
  1860. || TREE_CODE (var) != SSA_NAME)
  1861. return NULL_TREE;
  1862. loop = loop_containing_stmt (SSA_NAME_DEF_STMT (var));
  1863. exit = single_exit (loop);
  1864. if (!exit)
  1865. return NULL_TREE;
  1866. for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
  1867. {
  1868. phi = psi.phi ();
  1869. if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == var)
  1870. return PHI_RESULT (phi);
  1871. }
  1872. return NULL_TREE;
  1873. }
  1874. static tree instantiate_scev_r (basic_block, struct loop *, struct loop *,
  1875. tree, bool, int);
  1876. /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
  1877. and EVOLUTION_LOOP, that were left under a symbolic form.
  1878. CHREC is an SSA_NAME to be instantiated.
  1879. CACHE is the cache of already instantiated values.
  1880. FOLD_CONVERSIONS should be set to true when the conversions that
  1881. may wrap in signed/pointer type are folded, as long as the value of
  1882. the chrec is preserved.
  1883. SIZE_EXPR is used for computing the size of the expression to be
  1884. instantiated, and to stop if it exceeds some limit. */
  1885. static tree
  1886. instantiate_scev_name (basic_block instantiate_below,
  1887. struct loop *evolution_loop, struct loop *inner_loop,
  1888. tree chrec,
  1889. bool fold_conversions,
  1890. int size_expr)
  1891. {
  1892. tree res;
  1893. struct loop *def_loop;
  1894. basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (chrec));
  1895. /* A parameter (or loop invariant and we do not want to include
  1896. evolutions in outer loops), nothing to do. */
  1897. if (!def_bb
  1898. || loop_depth (def_bb->loop_father) == 0
  1899. || dominated_by_p (CDI_DOMINATORS, instantiate_below, def_bb))
  1900. return chrec;
  1901. /* We cache the value of instantiated variable to avoid exponential
  1902. time complexity due to reevaluations. We also store the convenient
  1903. value in the cache in order to prevent infinite recursion -- we do
  1904. not want to instantiate the SSA_NAME if it is in a mixer
  1905. structure. This is used for avoiding the instantiation of
  1906. recursively defined functions, such as:
  1907. | a_2 -> {0, +, 1, +, a_2}_1 */
  1908. unsigned si = get_instantiated_value_entry (*global_cache,
  1909. chrec, instantiate_below);
  1910. if (global_cache->get (si) != chrec_not_analyzed_yet)
  1911. return global_cache->get (si);
  1912. /* On recursion return chrec_dont_know. */
  1913. global_cache->set (si, chrec_dont_know);
  1914. def_loop = find_common_loop (evolution_loop, def_bb->loop_father);
  1915. /* If the analysis yields a parametric chrec, instantiate the
  1916. result again. */
  1917. res = analyze_scalar_evolution (def_loop, chrec);
  1918. /* Don't instantiate default definitions. */
  1919. if (TREE_CODE (res) == SSA_NAME
  1920. && SSA_NAME_IS_DEFAULT_DEF (res))
  1921. ;
  1922. /* Don't instantiate loop-closed-ssa phi nodes. */
  1923. else if (TREE_CODE (res) == SSA_NAME
  1924. && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res)))
  1925. > loop_depth (def_loop))
  1926. {
  1927. if (res == chrec)
  1928. res = loop_closed_phi_def (chrec);
  1929. else
  1930. res = chrec;
  1931. /* When there is no loop_closed_phi_def, it means that the
  1932. variable is not used after the loop: try to still compute the
  1933. value of the variable when exiting the loop. */
  1934. if (res == NULL_TREE)
  1935. {
  1936. loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (chrec));
  1937. res = analyze_scalar_evolution (loop, chrec);
  1938. res = compute_overall_effect_of_inner_loop (loop, res);
  1939. res = instantiate_scev_r (instantiate_below, evolution_loop,
  1940. inner_loop, res,
  1941. fold_conversions, size_expr);
  1942. }
  1943. else if (!dominated_by_p (CDI_DOMINATORS, instantiate_below,
  1944. gimple_bb (SSA_NAME_DEF_STMT (res))))
  1945. res = chrec_dont_know;
  1946. }
  1947. else if (res != chrec_dont_know)
  1948. {
  1949. if (inner_loop
  1950. && def_bb->loop_father != inner_loop
  1951. && !flow_loop_nested_p (def_bb->loop_father, inner_loop))
  1952. /* ??? We could try to compute the overall effect of the loop here. */
  1953. res = chrec_dont_know;
  1954. else
  1955. res = instantiate_scev_r (instantiate_below, evolution_loop,
  1956. inner_loop, res,
  1957. fold_conversions, size_expr);
  1958. }
  1959. /* Store the correct value to the cache. */
  1960. global_cache->set (si, res);
  1961. return res;
  1962. }
  1963. /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
  1964. and EVOLUTION_LOOP, that were left under a symbolic form.
  1965. CHREC is a polynomial chain of recurrence to be instantiated.
  1966. CACHE is the cache of already instantiated values.
  1967. FOLD_CONVERSIONS should be set to true when the conversions that
  1968. may wrap in signed/pointer type are folded, as long as the value of
  1969. the chrec is preserved.
  1970. SIZE_EXPR is used for computing the size of the expression to be
  1971. instantiated, and to stop if it exceeds some limit. */
  1972. static tree
  1973. instantiate_scev_poly (basic_block instantiate_below,
  1974. struct loop *evolution_loop, struct loop *,
  1975. tree chrec, bool fold_conversions, int size_expr)
  1976. {
  1977. tree op1;
  1978. tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
  1979. get_chrec_loop (chrec),
  1980. CHREC_LEFT (chrec), fold_conversions,
  1981. size_expr);
  1982. if (op0 == chrec_dont_know)
  1983. return chrec_dont_know;
  1984. op1 = instantiate_scev_r (instantiate_below, evolution_loop,
  1985. get_chrec_loop (chrec),
  1986. CHREC_RIGHT (chrec), fold_conversions,
  1987. size_expr);
  1988. if (op1 == chrec_dont_know)
  1989. return chrec_dont_know;
  1990. if (CHREC_LEFT (chrec) != op0
  1991. || CHREC_RIGHT (chrec) != op1)
  1992. {
  1993. op1 = chrec_convert_rhs (chrec_type (op0), op1, NULL);
  1994. chrec = build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1);
  1995. }
  1996. return chrec;
  1997. }
  1998. /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
  1999. and EVOLUTION_LOOP, that were left under a symbolic form.
  2000. "C0 CODE C1" is a binary expression of type TYPE to be instantiated.
  2001. CACHE is the cache of already instantiated values.
  2002. FOLD_CONVERSIONS should be set to true when the conversions that
  2003. may wrap in signed/pointer type are folded, as long as the value of
  2004. the chrec is preserved.
  2005. SIZE_EXPR is used for computing the size of the expression to be
  2006. instantiated, and to stop if it exceeds some limit. */
  2007. static tree
  2008. instantiate_scev_binary (basic_block instantiate_below,
  2009. struct loop *evolution_loop, struct loop *inner_loop,
  2010. tree chrec, enum tree_code code,
  2011. tree type, tree c0, tree c1,
  2012. bool fold_conversions, int size_expr)
  2013. {
  2014. tree op1;
  2015. tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
  2016. c0, fold_conversions, size_expr);
  2017. if (op0 == chrec_dont_know)
  2018. return chrec_dont_know;
  2019. op1 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
  2020. c1, fold_conversions, size_expr);
  2021. if (op1 == chrec_dont_know)
  2022. return chrec_dont_know;
  2023. if (c0 != op0
  2024. || c1 != op1)
  2025. {
  2026. op0 = chrec_convert (type, op0, NULL);
  2027. op1 = chrec_convert_rhs (type, op1, NULL);
  2028. switch (code)
  2029. {
  2030. case POINTER_PLUS_EXPR:
  2031. case PLUS_EXPR:
  2032. return chrec_fold_plus (type, op0, op1);
  2033. case MINUS_EXPR:
  2034. return chrec_fold_minus (type, op0, op1);
  2035. case MULT_EXPR:
  2036. return chrec_fold_multiply (type, op0, op1);
  2037. default:
  2038. gcc_unreachable ();
  2039. }
  2040. }
  2041. return chrec ? chrec : fold_build2 (code, type, c0, c1);
  2042. }
  2043. /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
  2044. and EVOLUTION_LOOP, that were left under a symbolic form.
  2045. "CHREC" is an array reference to be instantiated.
  2046. CACHE is the cache of already instantiated values.
  2047. FOLD_CONVERSIONS should be set to true when the conversions that
  2048. may wrap in signed/pointer type are folded, as long as the value of
  2049. the chrec is preserved.
  2050. SIZE_EXPR is used for computing the size of the expression to be
  2051. instantiated, and to stop if it exceeds some limit. */
  2052. static tree
  2053. instantiate_array_ref (basic_block instantiate_below,
  2054. struct loop *evolution_loop, struct loop *inner_loop,
  2055. tree chrec, bool fold_conversions, int size_expr)
  2056. {
  2057. tree res;
  2058. tree index = TREE_OPERAND (chrec, 1);
  2059. tree op1 = instantiate_scev_r (instantiate_below, evolution_loop,
  2060. inner_loop, index,
  2061. fold_conversions, size_expr);
  2062. if (op1 == chrec_dont_know)
  2063. return chrec_dont_know;
  2064. if (chrec && op1 == index)
  2065. return chrec;
  2066. res = unshare_expr (chrec);
  2067. TREE_OPERAND (res, 1) = op1;
  2068. return res;
  2069. }
  2070. /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
  2071. and EVOLUTION_LOOP, that were left under a symbolic form.
  2072. "CHREC" that stands for a convert expression "(TYPE) OP" is to be
  2073. instantiated.
  2074. CACHE is the cache of already instantiated values.
  2075. FOLD_CONVERSIONS should be set to true when the conversions that
  2076. may wrap in signed/pointer type are folded, as long as the value of
  2077. the chrec is preserved.
  2078. SIZE_EXPR is used for computing the size of the expression to be
  2079. instantiated, and to stop if it exceeds some limit. */
  2080. static tree
  2081. instantiate_scev_convert (basic_block instantiate_below,
  2082. struct loop *evolution_loop, struct loop *inner_loop,
  2083. tree chrec, tree type, tree op,
  2084. bool fold_conversions, int size_expr)
  2085. {
  2086. tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
  2087. inner_loop, op,
  2088. fold_conversions, size_expr);
  2089. if (op0 == chrec_dont_know)
  2090. return chrec_dont_know;
  2091. if (fold_conversions)
  2092. {
  2093. tree tmp = chrec_convert_aggressive (type, op0);
  2094. if (tmp)
  2095. return tmp;
  2096. }
  2097. if (chrec && op0 == op)
  2098. return chrec;
  2099. /* If we used chrec_convert_aggressive, we can no longer assume that
  2100. signed chrecs do not overflow, as chrec_convert does, so avoid
  2101. calling it in that case. */
  2102. if (fold_conversions)
  2103. return fold_convert (type, op0);
  2104. return chrec_convert (type, op0, NULL);
  2105. }
  2106. /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
  2107. and EVOLUTION_LOOP, that were left under a symbolic form.
  2108. CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated.
  2109. Handle ~X as -1 - X.
  2110. Handle -X as -1 * X.
  2111. CACHE is the cache of already instantiated values.
  2112. FOLD_CONVERSIONS should be set to true when the conversions that
  2113. may wrap in signed/pointer type are folded, as long as the value of
  2114. the chrec is preserved.
  2115. SIZE_EXPR is used for computing the size of the expression to be
  2116. instantiated, and to stop if it exceeds some limit. */
  2117. static tree
  2118. instantiate_scev_not (basic_block instantiate_below,
  2119. struct loop *evolution_loop, struct loop *inner_loop,
  2120. tree chrec,
  2121. enum tree_code code, tree type, tree op,
  2122. bool fold_conversions, int size_expr)
  2123. {
  2124. tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
  2125. inner_loop, op,
  2126. fold_conversions, size_expr);
  2127. if (op0 == chrec_dont_know)
  2128. return chrec_dont_know;
  2129. if (op != op0)
  2130. {
  2131. op0 = chrec_convert (type, op0, NULL);
  2132. switch (code)
  2133. {
  2134. case BIT_NOT_EXPR:
  2135. return chrec_fold_minus
  2136. (type, fold_convert (type, integer_minus_one_node), op0);
  2137. case NEGATE_EXPR:
  2138. return chrec_fold_multiply
  2139. (type, fold_convert (type, integer_minus_one_node), op0);
  2140. default:
  2141. gcc_unreachable ();
  2142. }
  2143. }
  2144. return chrec ? chrec : fold_build1 (code, type, op0);
  2145. }
  2146. /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
  2147. and EVOLUTION_LOOP, that were left under a symbolic form.
  2148. CHREC is an expression with 3 operands to be instantiated.
  2149. CACHE is the cache of already instantiated values.
  2150. FOLD_CONVERSIONS should be set to true when the conversions that
  2151. may wrap in signed/pointer type are folded, as long as the value of
  2152. the chrec is preserved.
  2153. SIZE_EXPR is used for computing the size of the expression to be
  2154. instantiated, and to stop if it exceeds some limit. */
  2155. static tree
  2156. instantiate_scev_3 (basic_block instantiate_below,
  2157. struct loop *evolution_loop, struct loop *inner_loop,
  2158. tree chrec,
  2159. bool fold_conversions, int size_expr)
  2160. {
  2161. tree op1, op2;
  2162. tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
  2163. inner_loop, TREE_OPERAND (chrec, 0),
  2164. fold_conversions, size_expr);
  2165. if (op0 == chrec_dont_know)
  2166. return chrec_dont_know;
  2167. op1 = instantiate_scev_r (instantiate_below, evolution_loop,
  2168. inner_loop, TREE_OPERAND (chrec, 1),
  2169. fold_conversions, size_expr);
  2170. if (op1 == chrec_dont_know)
  2171. return chrec_dont_know;
  2172. op2 = instantiate_scev_r (instantiate_below, evolution_loop,
  2173. inner_loop, TREE_OPERAND (chrec, 2),
  2174. fold_conversions, size_expr);
  2175. if (op2 == chrec_dont_know)
  2176. return chrec_dont_know;
  2177. if (op0 == TREE_OPERAND (chrec, 0)
  2178. && op1 == TREE_OPERAND (chrec, 1)
  2179. && op2 == TREE_OPERAND (chrec, 2))
  2180. return chrec;
  2181. return fold_build3 (TREE_CODE (chrec),
  2182. TREE_TYPE (chrec), op0, op1, op2);
  2183. }
  2184. /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
  2185. and EVOLUTION_LOOP, that were left under a symbolic form.
  2186. CHREC is an expression with 2 operands to be instantiated.
  2187. CACHE is the cache of already instantiated values.
  2188. FOLD_CONVERSIONS should be set to true when the conversions that
  2189. may wrap in signed/pointer type are folded, as long as the value of
  2190. the chrec is preserved.
  2191. SIZE_EXPR is used for computing the size of the expression to be
  2192. instantiated, and to stop if it exceeds some limit. */
  2193. static tree
  2194. instantiate_scev_2 (basic_block instantiate_below,
  2195. struct loop *evolution_loop, struct loop *inner_loop,
  2196. tree chrec,
  2197. bool fold_conversions, int size_expr)
  2198. {
  2199. tree op1;
  2200. tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
  2201. inner_loop, TREE_OPERAND (chrec, 0),
  2202. fold_conversions, size_expr);
  2203. if (op0 == chrec_dont_know)
  2204. return chrec_dont_know;
  2205. op1 = instantiate_scev_r (instantiate_below, evolution_loop,
  2206. inner_loop, TREE_OPERAND (chrec, 1),
  2207. fold_conversions, size_expr);
  2208. if (op1 == chrec_dont_know)
  2209. return chrec_dont_know;
  2210. if (op0 == TREE_OPERAND (chrec, 0)
  2211. && op1 == TREE_OPERAND (chrec, 1))
  2212. return chrec;
  2213. return fold_build2 (TREE_CODE (chrec), TREE_TYPE (chrec), op0, op1);
  2214. }
  2215. /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
  2216. and EVOLUTION_LOOP, that were left under a symbolic form.
  2217. CHREC is an expression with 2 operands to be instantiated.
  2218. CACHE is the cache of already instantiated values.
  2219. FOLD_CONVERSIONS should be set to true when the conversions that
  2220. may wrap in signed/pointer type are folded, as long as the value of
  2221. the chrec is preserved.
  2222. SIZE_EXPR is used for computing the size of the expression to be
  2223. instantiated, and to stop if it exceeds some limit. */
  2224. static tree
  2225. instantiate_scev_1 (basic_block instantiate_below,
  2226. struct loop *evolution_loop, struct loop *inner_loop,
  2227. tree chrec,
  2228. bool fold_conversions, int size_expr)
  2229. {
  2230. tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
  2231. inner_loop, TREE_OPERAND (chrec, 0),
  2232. fold_conversions, size_expr);
  2233. if (op0 == chrec_dont_know)
  2234. return chrec_dont_know;
  2235. if (op0 == TREE_OPERAND (chrec, 0))
  2236. return chrec;
  2237. return fold_build1 (TREE_CODE (chrec), TREE_TYPE (chrec), op0);
  2238. }
  2239. /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
  2240. and EVOLUTION_LOOP, that were left under a symbolic form.
  2241. CHREC is the scalar evolution to instantiate.
  2242. CACHE is the cache of already instantiated values.
  2243. FOLD_CONVERSIONS should be set to true when the conversions that
  2244. may wrap in signed/pointer type are folded, as long as the value of
  2245. the chrec is preserved.
  2246. SIZE_EXPR is used for computing the size of the expression to be
  2247. instantiated, and to stop if it exceeds some limit. */
  2248. static tree
  2249. instantiate_scev_r (basic_block instantiate_below,
  2250. struct loop *evolution_loop, struct loop *inner_loop,
  2251. tree chrec,
  2252. bool fold_conversions, int size_expr)
  2253. {
  2254. /* Give up if the expression is larger than the MAX that we allow. */
  2255. if (size_expr++ > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE))
  2256. return chrec_dont_know;
  2257. if (chrec == NULL_TREE
  2258. || automatically_generated_chrec_p (chrec)
  2259. || is_gimple_min_invariant (chrec))
  2260. return chrec;
  2261. switch (TREE_CODE (chrec))
  2262. {
  2263. case SSA_NAME:
  2264. return instantiate_scev_name (instantiate_below, evolution_loop,
  2265. inner_loop, chrec,
  2266. fold_conversions, size_expr);
  2267. case POLYNOMIAL_CHREC:
  2268. return instantiate_scev_poly (instantiate_below, evolution_loop,
  2269. inner_loop, chrec,
  2270. fold_conversions, size_expr);
  2271. case POINTER_PLUS_EXPR:
  2272. case PLUS_EXPR:
  2273. case MINUS_EXPR:
  2274. case MULT_EXPR:
  2275. return instantiate_scev_binary (instantiate_below, evolution_loop,
  2276. inner_loop, chrec,
  2277. TREE_CODE (chrec), chrec_type (chrec),
  2278. TREE_OPERAND (chrec, 0),
  2279. TREE_OPERAND (chrec, 1),
  2280. fold_conversions, size_expr);
  2281. CASE_CONVERT:
  2282. return instantiate_scev_convert (instantiate_below, evolution_loop,
  2283. inner_loop, chrec,
  2284. TREE_TYPE (chrec), TREE_OPERAND (chrec, 0),
  2285. fold_conversions, size_expr);
  2286. case NEGATE_EXPR:
  2287. case BIT_NOT_EXPR:
  2288. return instantiate_scev_not (instantiate_below, evolution_loop,
  2289. inner_loop, chrec,
  2290. TREE_CODE (chrec), TREE_TYPE (chrec),
  2291. TREE_OPERAND (chrec, 0),
  2292. fold_conversions, size_expr);
  2293. case ADDR_EXPR:
  2294. case SCEV_NOT_KNOWN:
  2295. return chrec_dont_know;
  2296. case SCEV_KNOWN:
  2297. return chrec_known;
  2298. case ARRAY_REF:
  2299. return instantiate_array_ref (instantiate_below, evolution_loop,
  2300. inner_loop, chrec,
  2301. fold_conversions, size_expr);
  2302. default:
  2303. break;
  2304. }
  2305. if (VL_EXP_CLASS_P (chrec))
  2306. return chrec_dont_know;
  2307. switch (TREE_CODE_LENGTH (TREE_CODE (chrec)))
  2308. {
  2309. case 3:
  2310. return instantiate_scev_3 (instantiate_below, evolution_loop,
  2311. inner_loop, chrec,
  2312. fold_conversions, size_expr);
  2313. case 2:
  2314. return instantiate_scev_2 (instantiate_below, evolution_loop,
  2315. inner_loop, chrec,
  2316. fold_conversions, size_expr);
  2317. case 1:
  2318. return instantiate_scev_1 (instantiate_below, evolution_loop,
  2319. inner_loop, chrec,
  2320. fold_conversions, size_expr);
  2321. case 0:
  2322. return chrec;
  2323. default:
  2324. break;
  2325. }
  2326. /* Too complicated to handle. */
  2327. return chrec_dont_know;
  2328. }
  2329. /* Analyze all the parameters of the chrec that were left under a
  2330. symbolic form. INSTANTIATE_BELOW is the basic block that stops the
  2331. recursive instantiation of parameters: a parameter is a variable
  2332. that is defined in a basic block that dominates INSTANTIATE_BELOW or
  2333. a function parameter. */
  2334. tree
  2335. instantiate_scev (basic_block instantiate_below, struct loop *evolution_loop,
  2336. tree chrec)
  2337. {
  2338. tree res;
  2339. if (dump_file && (dump_flags & TDF_SCEV))
  2340. {
  2341. fprintf (dump_file, "(instantiate_scev \n");
  2342. fprintf (dump_file, " (instantiate_below = %d)\n", instantiate_below->index);
  2343. fprintf (dump_file, " (evolution_loop = %d)\n", evolution_loop->num);
  2344. fprintf (dump_file, " (chrec = ");
  2345. print_generic_expr (dump_file, chrec, 0);
  2346. fprintf (dump_file, ")\n");
  2347. }
  2348. bool destr = false;
  2349. if (!global_cache)
  2350. {
  2351. global_cache = new instantiate_cache_type;
  2352. destr = true;
  2353. }
  2354. res = instantiate_scev_r (instantiate_below, evolution_loop,
  2355. NULL, chrec, false, 0);
  2356. if (destr)
  2357. {
  2358. delete global_cache;
  2359. global_cache = NULL;
  2360. }
  2361. if (dump_file && (dump_flags & TDF_SCEV))
  2362. {
  2363. fprintf (dump_file, " (res = ");
  2364. print_generic_expr (dump_file, res, 0);
  2365. fprintf (dump_file, "))\n");
  2366. }
  2367. return res;
  2368. }
  2369. /* Similar to instantiate_parameters, but does not introduce the
  2370. evolutions in outer loops for LOOP invariants in CHREC, and does not
  2371. care about causing overflows, as long as they do not affect value
  2372. of an expression. */
  2373. tree
  2374. resolve_mixers (struct loop *loop, tree chrec)
  2375. {
  2376. bool destr = false;
  2377. if (!global_cache)
  2378. {
  2379. global_cache = new instantiate_cache_type;
  2380. destr = true;
  2381. }
  2382. tree ret = instantiate_scev_r (block_before_loop (loop), loop, NULL,
  2383. chrec, true, 0);
  2384. if (destr)
  2385. {
  2386. delete global_cache;
  2387. global_cache = NULL;
  2388. }
  2389. return ret;
  2390. }
  2391. /* Entry point for the analysis of the number of iterations pass.
  2392. This function tries to safely approximate the number of iterations
  2393. the loop will run. When this property is not decidable at compile
  2394. time, the result is chrec_dont_know. Otherwise the result is a
  2395. scalar or a symbolic parameter. When the number of iterations may
  2396. be equal to zero and the property cannot be determined at compile
  2397. time, the result is a COND_EXPR that represents in a symbolic form
  2398. the conditions under which the number of iterations is not zero.
  2399. Example of analysis: suppose that the loop has an exit condition:
  2400. "if (b > 49) goto end_loop;"
  2401. and that in a previous analysis we have determined that the
  2402. variable 'b' has an evolution function:
  2403. "EF = {23, +, 5}_2".
  2404. When we evaluate the function at the point 5, i.e. the value of the
  2405. variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
  2406. and EF (6) = 53. In this case the value of 'b' on exit is '53' and
  2407. the loop body has been executed 6 times. */
  2408. tree
  2409. number_of_latch_executions (struct loop *loop)
  2410. {
  2411. edge exit;
  2412. struct tree_niter_desc niter_desc;
  2413. tree may_be_zero;
  2414. tree res;
  2415. /* Determine whether the number of iterations in loop has already
  2416. been computed. */
  2417. res = loop->nb_iterations;
  2418. if (res)
  2419. return res;
  2420. may_be_zero = NULL_TREE;
  2421. if (dump_file && (dump_flags & TDF_SCEV))
  2422. fprintf (dump_file, "(number_of_iterations_in_loop = \n");
  2423. res = chrec_dont_know;
  2424. exit = single_exit (loop);
  2425. if (exit && number_of_iterations_exit (loop, exit, &niter_desc, false))
  2426. {
  2427. may_be_zero = niter_desc.may_be_zero;
  2428. res = niter_desc.niter;
  2429. }
  2430. if (res == chrec_dont_know
  2431. || !may_be_zero
  2432. || integer_zerop (may_be_zero))
  2433. ;
  2434. else if (integer_nonzerop (may_be_zero))
  2435. res = build_int_cst (TREE_TYPE (res), 0);
  2436. else if (COMPARISON_CLASS_P (may_be_zero))
  2437. res = fold_build3 (COND_EXPR, TREE_TYPE (res), may_be_zero,
  2438. build_int_cst (TREE_TYPE (res), 0), res);
  2439. else
  2440. res = chrec_dont_know;
  2441. if (dump_file && (dump_flags & TDF_SCEV))
  2442. {
  2443. fprintf (dump_file, " (set_nb_iterations_in_loop = ");
  2444. print_generic_expr (dump_file, res, 0);
  2445. fprintf (dump_file, "))\n");
  2446. }
  2447. loop->nb_iterations = res;
  2448. return res;
  2449. }
  2450. /* Counters for the stats. */
  2451. struct chrec_stats
  2452. {
  2453. unsigned nb_chrecs;
  2454. unsigned nb_affine;
  2455. unsigned nb_affine_multivar;
  2456. unsigned nb_higher_poly;
  2457. unsigned nb_chrec_dont_know;
  2458. unsigned nb_undetermined;
  2459. };
  2460. /* Reset the counters. */
  2461. static inline void
  2462. reset_chrecs_counters (struct chrec_stats *stats)
  2463. {
  2464. stats->nb_chrecs = 0;
  2465. stats->nb_affine = 0;
  2466. stats->nb_affine_multivar = 0;
  2467. stats->nb_higher_poly = 0;
  2468. stats->nb_chrec_dont_know = 0;
  2469. stats->nb_undetermined = 0;
  2470. }
  2471. /* Dump the contents of a CHREC_STATS structure. */
  2472. static void
  2473. dump_chrecs_stats (FILE *file, struct chrec_stats *stats)
  2474. {
  2475. fprintf (file, "\n(\n");
  2476. fprintf (file, "-----------------------------------------\n");
  2477. fprintf (file, "%d\taffine univariate chrecs\n", stats->nb_affine);
  2478. fprintf (file, "%d\taffine multivariate chrecs\n", stats->nb_affine_multivar);
  2479. fprintf (file, "%d\tdegree greater than 2 polynomials\n",
  2480. stats->nb_higher_poly);
  2481. fprintf (file, "%d\tchrec_dont_know chrecs\n", stats->nb_chrec_dont_know);
  2482. fprintf (file, "-----------------------------------------\n");
  2483. fprintf (file, "%d\ttotal chrecs\n", stats->nb_chrecs);
  2484. fprintf (file, "%d\twith undetermined coefficients\n",
  2485. stats->nb_undetermined);
  2486. fprintf (file, "-----------------------------------------\n");
  2487. fprintf (file, "%d\tchrecs in the scev database\n",
  2488. (int) scalar_evolution_info->elements ());
  2489. fprintf (file, "%d\tsets in the scev database\n", nb_set_scev);
  2490. fprintf (file, "%d\tgets in the scev database\n", nb_get_scev);
  2491. fprintf (file, "-----------------------------------------\n");
  2492. fprintf (file, ")\n\n");
  2493. }
  2494. /* Gather statistics about CHREC. */
  2495. static void
  2496. gather_chrec_stats (tree chrec, struct chrec_stats *stats)
  2497. {
  2498. if (dump_file && (dump_flags & TDF_STATS))
  2499. {
  2500. fprintf (dump_file, "(classify_chrec ");
  2501. print_generic_expr (dump_file, chrec, 0);
  2502. fprintf (dump_file, "\n");
  2503. }
  2504. stats->nb_chrecs++;
  2505. if (chrec == NULL_TREE)
  2506. {
  2507. stats->nb_undetermined++;
  2508. return;
  2509. }
  2510. switch (TREE_CODE (chrec))
  2511. {
  2512. case POLYNOMIAL_CHREC:
  2513. if (evolution_function_is_affine_p (chrec))
  2514. {
  2515. if (dump_file && (dump_flags & TDF_STATS))
  2516. fprintf (dump_file, " affine_univariate\n");
  2517. stats->nb_affine++;
  2518. }
  2519. else if (evolution_function_is_affine_multivariate_p (chrec, 0))
  2520. {
  2521. if (dump_file && (dump_flags & TDF_STATS))
  2522. fprintf (dump_file, " affine_multivariate\n");
  2523. stats->nb_affine_multivar++;
  2524. }
  2525. else
  2526. {
  2527. if (dump_file && (dump_flags & TDF_STATS))
  2528. fprintf (dump_file, " higher_degree_polynomial\n");
  2529. stats->nb_higher_poly++;
  2530. }
  2531. break;
  2532. default:
  2533. break;
  2534. }
  2535. if (chrec_contains_undetermined (chrec))
  2536. {
  2537. if (dump_file && (dump_flags & TDF_STATS))
  2538. fprintf (dump_file, " undetermined\n");
  2539. stats->nb_undetermined++;
  2540. }
  2541. if (dump_file && (dump_flags & TDF_STATS))
  2542. fprintf (dump_file, ")\n");
  2543. }
  2544. /* Classify the chrecs of the whole database. */
  2545. void
  2546. gather_stats_on_scev_database (void)
  2547. {
  2548. struct chrec_stats stats;
  2549. if (!dump_file)
  2550. return;
  2551. reset_chrecs_counters (&stats);
  2552. hash_table<scev_info_hasher>::iterator iter;
  2553. scev_info_str *elt;
  2554. FOR_EACH_HASH_TABLE_ELEMENT (*scalar_evolution_info, elt, scev_info_str *,
  2555. iter)
  2556. gather_chrec_stats (elt->chrec, &stats);
  2557. dump_chrecs_stats (dump_file, &stats);
  2558. }
  2559. /* Initializer. */
  2560. static void
  2561. initialize_scalar_evolutions_analyzer (void)
  2562. {
  2563. /* The elements below are unique. */
  2564. if (chrec_dont_know == NULL_TREE)
  2565. {
  2566. chrec_not_analyzed_yet = NULL_TREE;
  2567. chrec_dont_know = make_node (SCEV_NOT_KNOWN);
  2568. chrec_known = make_node (SCEV_KNOWN);
  2569. TREE_TYPE (chrec_dont_know) = void_type_node;
  2570. TREE_TYPE (chrec_known) = void_type_node;
  2571. }
  2572. }
  2573. /* Initialize the analysis of scalar evolutions for LOOPS. */
  2574. void
  2575. scev_initialize (void)
  2576. {
  2577. struct loop *loop;
  2578. scalar_evolution_info = hash_table<scev_info_hasher>::create_ggc (100);
  2579. initialize_scalar_evolutions_analyzer ();
  2580. FOR_EACH_LOOP (loop, 0)
  2581. {
  2582. loop->nb_iterations = NULL_TREE;
  2583. }
  2584. }
  2585. /* Return true if SCEV is initialized. */
  2586. bool
  2587. scev_initialized_p (void)
  2588. {
  2589. return scalar_evolution_info != NULL;
  2590. }
  2591. /* Cleans up the information cached by the scalar evolutions analysis
  2592. in the hash table. */
  2593. void
  2594. scev_reset_htab (void)
  2595. {
  2596. if (!scalar_evolution_info)
  2597. return;
  2598. scalar_evolution_info->empty ();
  2599. }
  2600. /* Cleans up the information cached by the scalar evolutions analysis
  2601. in the hash table and in the loop->nb_iterations. */
  2602. void
  2603. scev_reset (void)
  2604. {
  2605. struct loop *loop;
  2606. scev_reset_htab ();
  2607. FOR_EACH_LOOP (loop, 0)
  2608. {
  2609. loop->nb_iterations = NULL_TREE;
  2610. }
  2611. }
  2612. /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
  2613. respect to WRTO_LOOP and returns its base and step in IV if possible
  2614. (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
  2615. and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be
  2616. invariant in LOOP. Otherwise we require it to be an integer constant.
  2617. IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
  2618. because it is computed in signed arithmetics). Consequently, adding an
  2619. induction variable
  2620. for (i = IV->base; ; i += IV->step)
  2621. is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
  2622. false for the type of the induction variable, or you can prove that i does
  2623. not wrap by some other argument. Otherwise, this might introduce undefined
  2624. behavior, and
  2625. for (i = iv->base; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
  2626. must be used instead. */
  2627. bool
  2628. simple_iv (struct loop *wrto_loop, struct loop *use_loop, tree op,
  2629. affine_iv *iv, bool allow_nonconstant_step)
  2630. {
  2631. tree type, ev;
  2632. bool folded_casts;
  2633. iv->base = NULL_TREE;
  2634. iv->step = NULL_TREE;
  2635. iv->no_overflow = false;
  2636. type = TREE_TYPE (op);
  2637. if (!POINTER_TYPE_P (type)
  2638. && !INTEGRAL_TYPE_P (type))
  2639. return false;
  2640. ev = analyze_scalar_evolution_in_loop (wrto_loop, use_loop, op,
  2641. &folded_casts);
  2642. if (chrec_contains_undetermined (ev)
  2643. || chrec_contains_symbols_defined_in_loop (ev, wrto_loop->num))
  2644. return false;
  2645. if (tree_does_not_contain_chrecs (ev))
  2646. {
  2647. iv->base = ev;
  2648. iv->step = build_int_cst (TREE_TYPE (ev), 0);
  2649. iv->no_overflow = true;
  2650. return true;
  2651. }
  2652. if (TREE_CODE (ev) != POLYNOMIAL_CHREC
  2653. || CHREC_VARIABLE (ev) != (unsigned) wrto_loop->num)
  2654. return false;
  2655. iv->step = CHREC_RIGHT (ev);
  2656. if ((!allow_nonconstant_step && TREE_CODE (iv->step) != INTEGER_CST)
  2657. || tree_contains_chrecs (iv->step, NULL))
  2658. return false;
  2659. iv->base = CHREC_LEFT (ev);
  2660. if (tree_contains_chrecs (iv->base, NULL))
  2661. return false;
  2662. iv->no_overflow = (!folded_casts && ANY_INTEGRAL_TYPE_P (type)
  2663. && TYPE_OVERFLOW_UNDEFINED (type));
  2664. return true;
  2665. }
  2666. /* Finalize the scalar evolution analysis. */
  2667. void
  2668. scev_finalize (void)
  2669. {
  2670. if (!scalar_evolution_info)
  2671. return;
  2672. scalar_evolution_info->empty ();
  2673. scalar_evolution_info = NULL;
  2674. }
  2675. /* Returns true if the expression EXPR is considered to be too expensive
  2676. for scev_const_prop. */
  2677. bool
  2678. expression_expensive_p (tree expr)
  2679. {
  2680. enum tree_code code;
  2681. if (is_gimple_val (expr))
  2682. return false;
  2683. code = TREE_CODE (expr);
  2684. if (code == TRUNC_DIV_EXPR
  2685. || code == CEIL_DIV_EXPR
  2686. || code == FLOOR_DIV_EXPR
  2687. || code == ROUND_DIV_EXPR
  2688. || code == TRUNC_MOD_EXPR
  2689. || code == CEIL_MOD_EXPR
  2690. || code == FLOOR_MOD_EXPR
  2691. || code == ROUND_MOD_EXPR
  2692. || code == EXACT_DIV_EXPR)
  2693. {
  2694. /* Division by power of two is usually cheap, so we allow it.
  2695. Forbid anything else. */
  2696. if (!integer_pow2p (TREE_OPERAND (expr, 1)))
  2697. return true;
  2698. }
  2699. switch (TREE_CODE_CLASS (code))
  2700. {
  2701. case tcc_binary:
  2702. case tcc_comparison:
  2703. if (expression_expensive_p (TREE_OPERAND (expr, 1)))
  2704. return true;
  2705. /* Fallthru. */
  2706. case tcc_unary:
  2707. return expression_expensive_p (TREE_OPERAND (expr, 0));
  2708. default:
  2709. return true;
  2710. }
  2711. }
  2712. /* Replace ssa names for that scev can prove they are constant by the
  2713. appropriate constants. Also perform final value replacement in loops,
  2714. in case the replacement expressions are cheap.
  2715. We only consider SSA names defined by phi nodes; rest is left to the
  2716. ordinary constant propagation pass. */
  2717. unsigned int
  2718. scev_const_prop (void)
  2719. {
  2720. basic_block bb;
  2721. tree name, type, ev;
  2722. gphi *phi;
  2723. gassign *ass;
  2724. struct loop *loop, *ex_loop;
  2725. bitmap ssa_names_to_remove = NULL;
  2726. unsigned i;
  2727. gphi_iterator psi;
  2728. if (number_of_loops (cfun) <= 1)
  2729. return 0;
  2730. FOR_EACH_BB_FN (bb, cfun)
  2731. {
  2732. loop = bb->loop_father;
  2733. for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
  2734. {
  2735. phi = psi.phi ();
  2736. name = PHI_RESULT (phi);
  2737. if (virtual_operand_p (name))
  2738. continue;
  2739. type = TREE_TYPE (name);
  2740. if (!POINTER_TYPE_P (type)
  2741. && !INTEGRAL_TYPE_P (type))
  2742. continue;
  2743. ev = resolve_mixers (loop, analyze_scalar_evolution (loop, name));
  2744. if (!is_gimple_min_invariant (ev)
  2745. || !may_propagate_copy (name, ev))
  2746. continue;
  2747. /* Replace the uses of the name. */
  2748. if (name != ev)
  2749. replace_uses_by (name, ev);
  2750. if (!ssa_names_to_remove)
  2751. ssa_names_to_remove = BITMAP_ALLOC (NULL);
  2752. bitmap_set_bit (ssa_names_to_remove, SSA_NAME_VERSION (name));
  2753. }
  2754. }
  2755. /* Remove the ssa names that were replaced by constants. We do not
  2756. remove them directly in the previous cycle, since this
  2757. invalidates scev cache. */
  2758. if (ssa_names_to_remove)
  2759. {
  2760. bitmap_iterator bi;
  2761. EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove, 0, i, bi)
  2762. {
  2763. gimple_stmt_iterator psi;
  2764. name = ssa_name (i);
  2765. phi = as_a <gphi *> (SSA_NAME_DEF_STMT (name));
  2766. gcc_assert (gimple_code (phi) == GIMPLE_PHI);
  2767. psi = gsi_for_stmt (phi);
  2768. remove_phi_node (&psi, true);
  2769. }
  2770. BITMAP_FREE (ssa_names_to_remove);
  2771. scev_reset ();
  2772. }
  2773. /* Now the regular final value replacement. */
  2774. FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
  2775. {
  2776. edge exit;
  2777. tree def, rslt, niter;
  2778. gimple_stmt_iterator gsi;
  2779. /* If we do not know exact number of iterations of the loop, we cannot
  2780. replace the final value. */
  2781. exit = single_exit (loop);
  2782. if (!exit)
  2783. continue;
  2784. niter = number_of_latch_executions (loop);
  2785. if (niter == chrec_dont_know)
  2786. continue;
  2787. /* Ensure that it is possible to insert new statements somewhere. */
  2788. if (!single_pred_p (exit->dest))
  2789. split_loop_exit_edge (exit);
  2790. gsi = gsi_after_labels (exit->dest);
  2791. ex_loop = superloop_at_depth (loop,
  2792. loop_depth (exit->dest->loop_father) + 1);
  2793. for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); )
  2794. {
  2795. phi = psi.phi ();
  2796. rslt = PHI_RESULT (phi);
  2797. def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
  2798. if (virtual_operand_p (def))
  2799. {
  2800. gsi_next (&psi);
  2801. continue;
  2802. }
  2803. if (!POINTER_TYPE_P (TREE_TYPE (def))
  2804. && !INTEGRAL_TYPE_P (TREE_TYPE (def)))
  2805. {
  2806. gsi_next (&psi);
  2807. continue;
  2808. }
  2809. bool folded_casts;
  2810. def = analyze_scalar_evolution_in_loop (ex_loop, loop, def,
  2811. &folded_casts);
  2812. def = compute_overall_effect_of_inner_loop (ex_loop, def);
  2813. if (!tree_does_not_contain_chrecs (def)
  2814. || chrec_contains_symbols_defined_in_loop (def, ex_loop->num)
  2815. /* Moving the computation from the loop may prolong life range
  2816. of some ssa names, which may cause problems if they appear
  2817. on abnormal edges. */
  2818. || contains_abnormal_ssa_name_p (def)
  2819. /* Do not emit expensive expressions. The rationale is that
  2820. when someone writes a code like
  2821. while (n > 45) n -= 45;
  2822. he probably knows that n is not large, and does not want it
  2823. to be turned into n %= 45. */
  2824. || expression_expensive_p (def))
  2825. {
  2826. if (dump_file && (dump_flags & TDF_DETAILS))
  2827. {
  2828. fprintf (dump_file, "not replacing:\n ");
  2829. print_gimple_stmt (dump_file, phi, 0, 0);
  2830. fprintf (dump_file, "\n");
  2831. }
  2832. gsi_next (&psi);
  2833. continue;
  2834. }
  2835. /* Eliminate the PHI node and replace it by a computation outside
  2836. the loop. */
  2837. if (dump_file)
  2838. {
  2839. fprintf (dump_file, "\nfinal value replacement:\n ");
  2840. print_gimple_stmt (dump_file, phi, 0, 0);
  2841. fprintf (dump_file, " with\n ");
  2842. }
  2843. def = unshare_expr (def);
  2844. remove_phi_node (&psi, false);
  2845. /* If def's type has undefined overflow and there were folded
  2846. casts, rewrite all stmts added for def into arithmetics
  2847. with defined overflow behavior. */
  2848. if (folded_casts && ANY_INTEGRAL_TYPE_P (TREE_TYPE (def))
  2849. && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (def)))
  2850. {
  2851. gimple_seq stmts;
  2852. gimple_stmt_iterator gsi2;
  2853. def = force_gimple_operand (def, &stmts, true, NULL_TREE);
  2854. gsi2 = gsi_start (stmts);
  2855. while (!gsi_end_p (gsi2))
  2856. {
  2857. gimple stmt = gsi_stmt (gsi2);
  2858. gimple_stmt_iterator gsi3 = gsi2;
  2859. gsi_next (&gsi2);
  2860. gsi_remove (&gsi3, false);
  2861. if (is_gimple_assign (stmt)
  2862. && arith_code_with_undefined_signed_overflow
  2863. (gimple_assign_rhs_code (stmt)))
  2864. gsi_insert_seq_before (&gsi,
  2865. rewrite_to_defined_overflow (stmt),
  2866. GSI_SAME_STMT);
  2867. else
  2868. gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
  2869. }
  2870. }
  2871. else
  2872. def = force_gimple_operand_gsi (&gsi, def, false, NULL_TREE,
  2873. true, GSI_SAME_STMT);
  2874. ass = gimple_build_assign (rslt, def);
  2875. gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
  2876. if (dump_file)
  2877. {
  2878. print_gimple_stmt (dump_file, ass, 0, 0);
  2879. fprintf (dump_file, "\n");
  2880. }
  2881. }
  2882. }
  2883. return 0;
  2884. }
  2885. #include "gt-tree-scalar-evolution.h"