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