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