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