1 /* Scalar evolution detector.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <s.pop@laposte.net>
5 This file is part of GCC.
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
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
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, 51 Franklin Street, Fifth Floor, Boston, MA
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.
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).
46 A short sketch of the algorithm is:
48 Given a scalar variable to be analyzed, follow the SSA edge to
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.
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).
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.
76 Example 1: Illustration of the basic algorithm.
82 | if (c > 10) exit_loop
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:
120 or in terms of a C program:
123 | for (x = 0; x <= 7; x++)
129 Example 2: Illustration of the algorithm on nested loops.
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:
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:
158 Example 3: Higher degree polynomials.
172 instantiate_parameters ({5, +, a}_1) -> {5, +, 2, +, 1}_1
173 instantiate_parameters ({5 + a, +, a}_1) -> {7, +, 3, +, 1}_1
175 Example 4: Lucas, Fibonacci, or mixers in general.
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)".
193 The symbolic chrec representation contains all the semantics of the
194 original code. What is more difficult is to use this information.
196 Example 5: Flip-flops, or exchangers.
208 Based on these symbolic chrecs, it is possible to refine this
209 information into the more precise PERIODIC_CHRECs:
214 This transformation is not yet implemented.
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.
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
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
236 #include "coretypes.h"
242 /* These RTL headers are needed for basic-block.h. */
244 #include "basic-block.h"
245 #include "diagnostic.h"
246 #include "tree-flow.h"
247 #include "tree-dump.h"
250 #include "tree-chrec.h"
251 #include "tree-scalar-evolution.h"
252 #include "tree-pass.h"
255 static tree analyze_scalar_evolution_1 (struct loop *, tree, tree);
256 static tree resolve_mixers (struct loop *, tree);
258 /* The cached information about a ssa name VAR, claiming that inside LOOP,
259 the value of VAR can be expressed as CHREC. */
267 /* Counters for the scev database. */
268 static unsigned nb_set_scev = 0;
269 static unsigned nb_get_scev = 0;
271 /* The following trees are unique elements. Thus the comparison of
272 another element to these elements should be done on the pointer to
273 these trees, and not on their value. */
275 /* The SSA_NAMEs that are not yet analyzed are qualified with NULL_TREE. */
276 tree chrec_not_analyzed_yet;
278 /* Reserved to the cases where the analyzer has detected an
279 undecidable property at compile time. */
280 tree chrec_dont_know;
282 /* When the analyzer has detected that a property will never
283 happen, then it qualifies it with chrec_known. */
286 static bitmap already_instantiated;
288 static htab_t scalar_evolution_info;
291 /* Constructs a new SCEV_INFO_STR structure. */
293 static inline struct scev_info_str *
294 new_scev_info_str (tree var)
296 struct scev_info_str *res;
298 res = xmalloc (sizeof (struct scev_info_str));
300 res->chrec = chrec_not_analyzed_yet;
305 /* Computes a hash function for database element ELT. */
308 hash_scev_info (const void *elt)
310 return SSA_NAME_VERSION (((struct scev_info_str *) elt)->var);
313 /* Compares database elements E1 and E2. */
316 eq_scev_info (const void *e1, const void *e2)
318 const struct scev_info_str *elt1 = e1;
319 const struct scev_info_str *elt2 = e2;
321 return elt1->var == elt2->var;
324 /* Deletes database element E. */
327 del_scev_info (void *e)
332 /* Get the index corresponding to VAR in the current LOOP. If
333 it's the first time we ask for this VAR, then we return
334 chrec_not_analyzed_yet for this VAR and return its index. */
337 find_var_scev_info (tree var)
339 struct scev_info_str *res;
340 struct scev_info_str tmp;
344 slot = htab_find_slot (scalar_evolution_info, &tmp, INSERT);
347 *slot = new_scev_info_str (var);
353 /* Return true when CHREC contains symbolic names defined in
357 chrec_contains_symbols_defined_in_loop (tree chrec, unsigned loop_nb)
359 if (chrec == NULL_TREE)
362 if (TREE_INVARIANT (chrec))
365 if (TREE_CODE (chrec) == VAR_DECL
366 || TREE_CODE (chrec) == PARM_DECL
367 || TREE_CODE (chrec) == FUNCTION_DECL
368 || TREE_CODE (chrec) == LABEL_DECL
369 || TREE_CODE (chrec) == RESULT_DECL
370 || TREE_CODE (chrec) == FIELD_DECL)
373 if (TREE_CODE (chrec) == SSA_NAME)
375 tree def = SSA_NAME_DEF_STMT (chrec);
376 struct loop *def_loop = loop_containing_stmt (def);
377 struct loop *loop = current_loops->parray[loop_nb];
379 if (def_loop == NULL)
382 if (loop == def_loop || flow_loop_nested_p (loop, def_loop))
388 switch (TREE_CODE_LENGTH (TREE_CODE (chrec)))
391 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, 2),
396 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, 1),
401 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, 0),
410 /* Return true when PHI is a loop-phi-node. */
413 loop_phi_node_p (tree phi)
415 /* The implementation of this function is based on the following
416 property: "all the loop-phi-nodes of a loop are contained in the
417 loop's header basic block". */
419 return loop_containing_stmt (phi)->header == bb_for_stmt (phi);
422 /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
423 In general, in the case of multivariate evolutions we want to get
424 the evolution in different loops. LOOP specifies the level for
425 which to get the evolution.
429 | for (j = 0; j < 100; j++)
431 | for (k = 0; k < 100; k++)
433 | i = k + j; - Here the value of i is a function of j, k.
435 | ... = i - Here the value of i is a function of j.
437 | ... = i - Here the value of i is a scalar.
443 | i_1 = phi (i_0, i_2)
447 This loop has the same effect as:
448 LOOP_1 has the same effect as:
452 The overall effect of the loop, "i_0 + 20" in the previous example,
453 is obtained by passing in the parameters: LOOP = 1,
454 EVOLUTION_FN = {i_0, +, 2}_1.
458 compute_overall_effect_of_inner_loop (struct loop *loop, tree evolution_fn)
462 if (evolution_fn == chrec_dont_know)
463 return chrec_dont_know;
465 else if (TREE_CODE (evolution_fn) == POLYNOMIAL_CHREC)
467 if (CHREC_VARIABLE (evolution_fn) >= (unsigned) loop->num)
469 struct loop *inner_loop =
470 current_loops->parray[CHREC_VARIABLE (evolution_fn)];
471 tree nb_iter = number_of_iterations_in_loop (inner_loop);
473 if (nb_iter == chrec_dont_know)
474 return chrec_dont_know;
479 /* Number of iterations is off by one (the ssa name we
480 analyze must be defined before the exit). */
481 nb_iter = chrec_fold_minus (chrec_type (nb_iter),
483 build_int_cst_type (chrec_type (nb_iter), 1));
485 /* evolution_fn is the evolution function in LOOP. Get
486 its value in the nb_iter-th iteration. */
487 res = chrec_apply (inner_loop->num, evolution_fn, nb_iter);
489 /* Continue the computation until ending on a parent of LOOP. */
490 return compute_overall_effect_of_inner_loop (loop, res);
497 /* If the evolution function is an invariant, there is nothing to do. */
498 else if (no_evolution_in_loop_p (evolution_fn, loop->num, &val) && val)
502 return chrec_dont_know;
505 /* Determine whether the CHREC is always positive/negative. If the expression
506 cannot be statically analyzed, return false, otherwise set the answer into
510 chrec_is_positive (tree chrec, bool *value)
517 switch (TREE_CODE (chrec))
519 case POLYNOMIAL_CHREC:
520 if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
521 || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
524 /* FIXME -- overflows. */
525 if (value0 == value1)
531 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
532 and the proof consists in showing that the sign never
533 changes during the execution of the loop, from 0 to
534 loop->nb_iterations. */
535 if (!evolution_function_is_affine_p (chrec))
538 nb_iter = number_of_iterations_in_loop
539 (current_loops->parray[CHREC_VARIABLE (chrec)]);
541 if (chrec_contains_undetermined (nb_iter))
544 nb_iter = chrec_fold_minus
545 (chrec_type (nb_iter), nb_iter,
546 build_int_cst (chrec_type (nb_iter), 1));
549 /* TODO -- If the test is after the exit, we may decrease the number of
550 iterations by one. */
552 nb_iter = chrec_fold_minus
553 (chrec_type (nb_iter), nb_iter,
554 build_int_cst (chrec_type (nb_iter), 1));
557 end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
559 if (!chrec_is_positive (end_value, &value2))
563 return value0 == value1;
566 *value = (tree_int_cst_sgn (chrec) == 1);
574 /* Associate CHREC to SCALAR. */
577 set_scalar_evolution (tree scalar, tree chrec)
581 if (TREE_CODE (scalar) != SSA_NAME)
584 scalar_info = find_var_scev_info (scalar);
588 if (dump_flags & TDF_DETAILS)
590 fprintf (dump_file, "(set_scalar_evolution \n");
591 fprintf (dump_file, " (scalar = ");
592 print_generic_expr (dump_file, scalar, 0);
593 fprintf (dump_file, ")\n (scalar_evolution = ");
594 print_generic_expr (dump_file, chrec, 0);
595 fprintf (dump_file, "))\n");
597 if (dump_flags & TDF_STATS)
601 *scalar_info = chrec;
604 /* Retrieve the chrec associated to SCALAR in the LOOP. */
607 get_scalar_evolution (tree scalar)
613 if (dump_flags & TDF_DETAILS)
615 fprintf (dump_file, "(get_scalar_evolution \n");
616 fprintf (dump_file, " (scalar = ");
617 print_generic_expr (dump_file, scalar, 0);
618 fprintf (dump_file, ")\n");
620 if (dump_flags & TDF_STATS)
624 switch (TREE_CODE (scalar))
627 res = *find_var_scev_info (scalar);
636 res = chrec_not_analyzed_yet;
640 if (dump_file && (dump_flags & TDF_DETAILS))
642 fprintf (dump_file, " (scalar_evolution = ");
643 print_generic_expr (dump_file, res, 0);
644 fprintf (dump_file, "))\n");
650 /* Helper function for add_to_evolution. Returns the evolution
651 function for an assignment of the form "a = b + c", where "a" and
652 "b" are on the strongly connected component. CHREC_BEFORE is the
653 information that we already have collected up to this point.
654 TO_ADD is the evolution of "c".
656 When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
657 evolution the expression TO_ADD, otherwise construct an evolution
658 part for this loop. */
661 add_to_evolution_1 (unsigned loop_nb,
665 switch (TREE_CODE (chrec_before))
667 case POLYNOMIAL_CHREC:
668 if (CHREC_VARIABLE (chrec_before) <= loop_nb)
672 tree type = chrec_type (chrec_before);
674 /* When there is no evolution part in this loop, build it. */
675 if (CHREC_VARIABLE (chrec_before) < loop_nb)
679 right = SCALAR_FLOAT_TYPE_P (type)
680 ? build_real (type, dconst0)
681 : build_int_cst (type, 0);
685 var = CHREC_VARIABLE (chrec_before);
686 left = CHREC_LEFT (chrec_before);
687 right = CHREC_RIGHT (chrec_before);
690 return build_polynomial_chrec
691 (var, left, chrec_fold_plus (type, right, to_add));
694 /* Search the evolution in LOOP_NB. */
695 return build_polynomial_chrec
696 (CHREC_VARIABLE (chrec_before),
697 add_to_evolution_1 (loop_nb, CHREC_LEFT (chrec_before), to_add),
698 CHREC_RIGHT (chrec_before));
701 /* These nodes do not depend on a loop. */
702 if (chrec_before == chrec_dont_know)
703 return chrec_dont_know;
704 return build_polynomial_chrec (loop_nb, chrec_before, to_add);
708 /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
711 Description (provided for completeness, for those who read code in
712 a plane, and for my poor 62 bytes brain that would have forgotten
713 all this in the next two or three months):
715 The algorithm of translation of programs from the SSA representation
716 into the chrecs syntax is based on a pattern matching. After having
717 reconstructed the overall tree expression for a loop, there are only
718 two cases that can arise:
720 1. a = loop-phi (init, a + expr)
721 2. a = loop-phi (init, expr)
723 where EXPR is either a scalar constant with respect to the analyzed
724 loop (this is a degree 0 polynomial), or an expression containing
725 other loop-phi definitions (these are higher degree polynomials).
732 | a = phi (init, a + 5)
739 | a = phi (inita, 2 * b + 3)
740 | b = phi (initb, b + 1)
743 For the first case, the semantics of the SSA representation is:
745 | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
747 that is, there is a loop index "x" that determines the scalar value
748 of the variable during the loop execution. During the first
749 iteration, the value is that of the initial condition INIT, while
750 during the subsequent iterations, it is the sum of the initial
751 condition with the sum of all the values of EXPR from the initial
752 iteration to the before last considered iteration.
754 For the second case, the semantics of the SSA program is:
756 | a (x) = init, if x = 0;
757 | expr (x - 1), otherwise.
759 The second case corresponds to the PEELED_CHREC, whose syntax is
760 close to the syntax of a loop-phi-node:
762 | phi (init, expr) vs. (init, expr)_x
764 The proof of the translation algorithm for the first case is a
765 proof by structural induction based on the degree of EXPR.
768 When EXPR is a constant with respect to the analyzed loop, or in
769 other words when EXPR is a polynomial of degree 0, the evolution of
770 the variable A in the loop is an affine function with an initial
771 condition INIT, and a step EXPR. In order to show this, we start
772 from the semantics of the SSA representation:
774 f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
776 and since "expr (j)" is a constant with respect to "j",
778 f (x) = init + x * expr
780 Finally, based on the semantics of the pure sum chrecs, by
781 identification we get the corresponding chrecs syntax:
783 f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
784 f (x) -> {init, +, expr}_x
787 Suppose that EXPR is a polynomial of degree N with respect to the
788 analyzed loop_x for which we have already determined that it is
789 written under the chrecs syntax:
791 | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
793 We start from the semantics of the SSA program:
795 | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
797 | f (x) = init + \sum_{j = 0}^{x - 1}
798 | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
800 | f (x) = init + \sum_{j = 0}^{x - 1}
801 | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
803 | f (x) = init + \sum_{k = 0}^{n - 1}
804 | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
806 | f (x) = init + \sum_{k = 0}^{n - 1}
807 | (b_k * \binom{x}{k + 1})
809 | f (x) = init + b_0 * \binom{x}{1} + ...
810 | + b_{n-1} * \binom{x}{n}
812 | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
813 | + b_{n-1} * \binom{x}{n}
816 And finally from the definition of the chrecs syntax, we identify:
817 | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
819 This shows the mechanism that stands behind the add_to_evolution
820 function. An important point is that the use of symbolic
821 parameters avoids the need of an analysis schedule.
828 | a = phi (inita, a + 2 + b)
829 | b = phi (initb, b + 1)
832 When analyzing "a", the algorithm keeps "b" symbolically:
834 | a -> {inita, +, 2 + b}_1
836 Then, after instantiation, the analyzer ends on the evolution:
838 | a -> {inita, +, 2 + initb, +, 1}_1
843 add_to_evolution (unsigned loop_nb,
848 tree type = chrec_type (to_add);
849 tree res = NULL_TREE;
851 if (to_add == NULL_TREE)
854 /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
855 instantiated at this point. */
856 if (TREE_CODE (to_add) == POLYNOMIAL_CHREC)
857 /* This should not happen. */
858 return chrec_dont_know;
860 if (dump_file && (dump_flags & TDF_DETAILS))
862 fprintf (dump_file, "(add_to_evolution \n");
863 fprintf (dump_file, " (loop_nb = %d)\n", loop_nb);
864 fprintf (dump_file, " (chrec_before = ");
865 print_generic_expr (dump_file, chrec_before, 0);
866 fprintf (dump_file, ")\n (to_add = ");
867 print_generic_expr (dump_file, to_add, 0);
868 fprintf (dump_file, ")\n");
871 if (code == MINUS_EXPR)
872 to_add = chrec_fold_multiply (type, to_add, SCALAR_FLOAT_TYPE_P (type)
873 ? build_real (type, dconstm1)
874 : build_int_cst_type (type, -1));
876 res = add_to_evolution_1 (loop_nb, chrec_before, to_add);
878 if (dump_file && (dump_flags & TDF_DETAILS))
880 fprintf (dump_file, " (res = ");
881 print_generic_expr (dump_file, res, 0);
882 fprintf (dump_file, "))\n");
888 /* Helper function. */
891 set_nb_iterations_in_loop (struct loop *loop,
894 res = chrec_fold_plus (chrec_type (res), res,
895 build_int_cst_type (chrec_type (res), 1));
897 /* FIXME HWI: However we want to store one iteration less than the
898 count of the loop in order to be compatible with the other
899 nb_iter computations in loop-iv. This also allows the
900 representation of nb_iters that are equal to MAX_INT. */
901 if (TREE_CODE (res) == INTEGER_CST
902 && (TREE_INT_CST_LOW (res) == 0
903 || TREE_OVERFLOW (res)))
904 res = chrec_dont_know;
906 if (dump_file && (dump_flags & TDF_DETAILS))
908 fprintf (dump_file, " (set_nb_iterations_in_loop = ");
909 print_generic_expr (dump_file, res, 0);
910 fprintf (dump_file, "))\n");
913 loop->nb_iterations = res;
919 /* This section selects the loops that will be good candidates for the
920 scalar evolution analysis. For the moment, greedily select all the
921 loop nests we could analyze. */
923 /* Return true when it is possible to analyze the condition expression
927 analyzable_condition (tree expr)
931 if (TREE_CODE (expr) != COND_EXPR)
934 condition = TREE_OPERAND (expr, 0);
936 switch (TREE_CODE (condition))
956 /* For a loop with a single exit edge, return the COND_EXPR that
957 guards the exit edge. If the expression is too difficult to
958 analyze, then give up. */
961 get_loop_exit_condition (struct loop *loop)
963 tree res = NULL_TREE;
964 edge exit_edge = loop->single_exit;
967 if (dump_file && (dump_flags & TDF_DETAILS))
968 fprintf (dump_file, "(get_loop_exit_condition \n ");
974 expr = last_stmt (exit_edge->src);
975 if (analyzable_condition (expr))
979 if (dump_file && (dump_flags & TDF_DETAILS))
981 print_generic_expr (dump_file, res, 0);
982 fprintf (dump_file, ")\n");
988 /* Recursively determine and enqueue the exit conditions for a loop. */
991 get_exit_conditions_rec (struct loop *loop,
992 VEC(tree,heap) **exit_conditions)
997 /* Recurse on the inner loops, then on the next (sibling) loops. */
998 get_exit_conditions_rec (loop->inner, exit_conditions);
999 get_exit_conditions_rec (loop->next, exit_conditions);
1001 if (loop->single_exit)
1003 tree loop_condition = get_loop_exit_condition (loop);
1006 VEC_safe_push (tree, heap, *exit_conditions, loop_condition);
1010 /* Select the candidate loop nests for the analysis. This function
1011 initializes the EXIT_CONDITIONS array. */
1014 select_loops_exit_conditions (struct loops *loops,
1015 VEC(tree,heap) **exit_conditions)
1017 struct loop *function_body = loops->parray[0];
1019 get_exit_conditions_rec (function_body->inner, exit_conditions);
1023 /* Depth first search algorithm. */
1025 static bool follow_ssa_edge (struct loop *loop, tree, tree, tree *);
1027 /* Follow the ssa edge into the right hand side RHS of an assignment.
1028 Return true if the strongly connected component has been found. */
1031 follow_ssa_edge_in_rhs (struct loop *loop,
1035 tree *evolution_of_loop)
1039 tree type_rhs = TREE_TYPE (rhs);
1041 /* The RHS is one of the following cases:
1047 - other cases are not yet handled. */
1048 switch (TREE_CODE (rhs))
1051 /* This assignment is under the form "a_1 = (cast) rhs. */
1052 res = follow_ssa_edge_in_rhs (loop, at_stmt, TREE_OPERAND (rhs, 0),
1053 halting_phi, evolution_of_loop);
1054 *evolution_of_loop = chrec_convert (TREE_TYPE (rhs),
1055 *evolution_of_loop, at_stmt);
1059 /* This assignment is under the form "a_1 = 7". */
1064 /* This assignment is under the form: "a_1 = b_2". */
1065 res = follow_ssa_edge
1066 (loop, SSA_NAME_DEF_STMT (rhs), halting_phi, evolution_of_loop);
1070 /* This case is under the form "rhs0 + rhs1". */
1071 rhs0 = TREE_OPERAND (rhs, 0);
1072 rhs1 = TREE_OPERAND (rhs, 1);
1073 STRIP_TYPE_NOPS (rhs0);
1074 STRIP_TYPE_NOPS (rhs1);
1076 if (TREE_CODE (rhs0) == SSA_NAME)
1078 if (TREE_CODE (rhs1) == SSA_NAME)
1080 /* Match an assignment under the form:
1082 res = follow_ssa_edge
1083 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1087 *evolution_of_loop = add_to_evolution
1089 chrec_convert (type_rhs, *evolution_of_loop, at_stmt),
1094 res = follow_ssa_edge
1095 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
1099 *evolution_of_loop = add_to_evolution
1101 chrec_convert (type_rhs, *evolution_of_loop, at_stmt),
1108 /* Match an assignment under the form:
1110 res = follow_ssa_edge
1111 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1114 *evolution_of_loop = add_to_evolution
1115 (loop->num, chrec_convert (type_rhs, *evolution_of_loop,
1121 else if (TREE_CODE (rhs1) == SSA_NAME)
1123 /* Match an assignment under the form:
1125 res = follow_ssa_edge
1126 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
1129 *evolution_of_loop = add_to_evolution
1130 (loop->num, chrec_convert (type_rhs, *evolution_of_loop,
1136 /* Otherwise, match an assignment under the form:
1138 /* And there is nothing to do. */
1144 /* This case is under the form "opnd0 = rhs0 - rhs1". */
1145 rhs0 = TREE_OPERAND (rhs, 0);
1146 rhs1 = TREE_OPERAND (rhs, 1);
1147 STRIP_TYPE_NOPS (rhs0);
1148 STRIP_TYPE_NOPS (rhs1);
1150 if (TREE_CODE (rhs0) == SSA_NAME)
1152 /* Match an assignment under the form:
1154 res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1157 *evolution_of_loop = add_to_evolution
1158 (loop->num, chrec_convert (type_rhs, *evolution_of_loop,
1163 /* Otherwise, match an assignment under the form:
1165 /* And there is nothing to do. */
1171 /* This case is under the form "opnd0 = rhs0 * rhs1". */
1172 rhs0 = TREE_OPERAND (rhs, 0);
1173 rhs1 = TREE_OPERAND (rhs, 1);
1174 STRIP_TYPE_NOPS (rhs0);
1175 STRIP_TYPE_NOPS (rhs1);
1177 if (TREE_CODE (rhs0) == SSA_NAME)
1179 if (TREE_CODE (rhs1) == SSA_NAME)
1181 /* Match an assignment under the form:
1183 res = follow_ssa_edge
1184 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1188 *evolution_of_loop = chrec_dont_know;
1192 res = follow_ssa_edge
1193 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
1197 *evolution_of_loop = chrec_dont_know;
1203 /* Match an assignment under the form:
1205 res = follow_ssa_edge
1206 (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
1209 *evolution_of_loop = chrec_dont_know;
1213 else if (TREE_CODE (rhs1) == SSA_NAME)
1215 /* Match an assignment under the form:
1217 res = follow_ssa_edge
1218 (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
1221 *evolution_of_loop = chrec_dont_know;
1225 /* Otherwise, match an assignment under the form:
1227 /* And there is nothing to do. */
1234 /* This assignment is of the form: "a_1 = ASSERT_EXPR <a_2, ...>"
1235 It must be handled as a copy assignment of the form a_1 = a_2. */
1236 tree op0 = ASSERT_EXPR_VAR (rhs);
1237 if (TREE_CODE (op0) == SSA_NAME)
1238 res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (op0),
1239 halting_phi, evolution_of_loop);
1254 /* Checks whether the I-th argument of a PHI comes from a backedge. */
1257 backedge_phi_arg_p (tree phi, int i)
1259 edge e = PHI_ARG_EDGE (phi, i);
1261 /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
1262 about updating it anywhere, and this should work as well most of the
1264 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1270 /* Helper function for one branch of the condition-phi-node. Return
1271 true if the strongly connected component has been found following
1275 follow_ssa_edge_in_condition_phi_branch (int i,
1279 tree *evolution_of_branch,
1282 tree branch = PHI_ARG_DEF (condition_phi, i);
1283 *evolution_of_branch = chrec_dont_know;
1285 /* Do not follow back edges (they must belong to an irreducible loop, which
1286 we really do not want to worry about). */
1287 if (backedge_phi_arg_p (condition_phi, i))
1290 if (TREE_CODE (branch) == SSA_NAME)
1292 *evolution_of_branch = init_cond;
1293 return follow_ssa_edge (loop, SSA_NAME_DEF_STMT (branch), halting_phi,
1294 evolution_of_branch);
1297 /* This case occurs when one of the condition branches sets
1298 the variable to a constant: i.e. a phi-node like
1299 "a_2 = PHI <a_7(5), 2(6)>;".
1301 FIXME: This case have to be refined correctly:
1302 in some cases it is possible to say something better than
1303 chrec_dont_know, for example using a wrap-around notation. */
1307 /* This function merges the branches of a condition-phi-node in a
1311 follow_ssa_edge_in_condition_phi (struct loop *loop,
1314 tree *evolution_of_loop)
1317 tree init = *evolution_of_loop;
1318 tree evolution_of_branch;
1320 if (!follow_ssa_edge_in_condition_phi_branch (0, loop, condition_phi,
1322 &evolution_of_branch,
1325 *evolution_of_loop = evolution_of_branch;
1327 for (i = 1; i < PHI_NUM_ARGS (condition_phi); i++)
1329 /* Quickly give up when the evolution of one of the branches is
1331 if (*evolution_of_loop == chrec_dont_know)
1334 if (!follow_ssa_edge_in_condition_phi_branch (i, loop, condition_phi,
1336 &evolution_of_branch,
1340 *evolution_of_loop = chrec_merge (*evolution_of_loop,
1341 evolution_of_branch);
1347 /* Follow an SSA edge in an inner loop. It computes the overall
1348 effect of the loop, and following the symbolic initial conditions,
1349 it follows the edges in the parent loop. The inner loop is
1350 considered as a single statement. */
1353 follow_ssa_edge_inner_loop_phi (struct loop *outer_loop,
1356 tree *evolution_of_loop)
1358 struct loop *loop = loop_containing_stmt (loop_phi_node);
1359 tree ev = analyze_scalar_evolution (loop, PHI_RESULT (loop_phi_node));
1361 /* Sometimes, the inner loop is too difficult to analyze, and the
1362 result of the analysis is a symbolic parameter. */
1363 if (ev == PHI_RESULT (loop_phi_node))
1368 for (i = 0; i < PHI_NUM_ARGS (loop_phi_node); i++)
1370 tree arg = PHI_ARG_DEF (loop_phi_node, i);
1373 /* Follow the edges that exit the inner loop. */
1374 bb = PHI_ARG_EDGE (loop_phi_node, i)->src;
1375 if (!flow_bb_inside_loop_p (loop, bb))
1376 res = res || follow_ssa_edge_in_rhs (outer_loop, loop_phi_node,
1381 /* If the path crosses this loop-phi, give up. */
1383 *evolution_of_loop = chrec_dont_know;
1388 /* Otherwise, compute the overall effect of the inner loop. */
1389 ev = compute_overall_effect_of_inner_loop (loop, ev);
1390 return follow_ssa_edge_in_rhs (outer_loop, loop_phi_node, ev, halting_phi,
1394 /* Follow an SSA edge from a loop-phi-node to itself, constructing a
1395 path that is analyzed on the return walk. */
1398 follow_ssa_edge (struct loop *loop,
1401 tree *evolution_of_loop)
1403 struct loop *def_loop;
1405 if (TREE_CODE (def) == NOP_EXPR)
1408 def_loop = loop_containing_stmt (def);
1410 switch (TREE_CODE (def))
1413 if (!loop_phi_node_p (def))
1414 /* DEF is a condition-phi-node. Follow the branches, and
1415 record their evolutions. Finally, merge the collected
1416 information and set the approximation to the main
1418 return follow_ssa_edge_in_condition_phi
1419 (loop, def, halting_phi, evolution_of_loop);
1421 /* When the analyzed phi is the halting_phi, the
1422 depth-first search is over: we have found a path from
1423 the halting_phi to itself in the loop. */
1424 if (def == halting_phi)
1427 /* Otherwise, the evolution of the HALTING_PHI depends
1428 on the evolution of another loop-phi-node, i.e. the
1429 evolution function is a higher degree polynomial. */
1430 if (def_loop == loop)
1434 if (flow_loop_nested_p (loop, def_loop))
1435 return follow_ssa_edge_inner_loop_phi
1436 (loop, def, halting_phi, evolution_of_loop);
1442 return follow_ssa_edge_in_rhs (loop, def,
1443 TREE_OPERAND (def, 1),
1448 /* At this level of abstraction, the program is just a set
1449 of MODIFY_EXPRs and PHI_NODEs. In principle there is no
1450 other node to be handled. */
1457 /* Given a LOOP_PHI_NODE, this function determines the evolution
1458 function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
1461 analyze_evolution_in_loop (tree loop_phi_node,
1465 tree evolution_function = chrec_not_analyzed_yet;
1466 struct loop *loop = loop_containing_stmt (loop_phi_node);
1469 if (dump_file && (dump_flags & TDF_DETAILS))
1471 fprintf (dump_file, "(analyze_evolution_in_loop \n");
1472 fprintf (dump_file, " (loop_phi_node = ");
1473 print_generic_expr (dump_file, loop_phi_node, 0);
1474 fprintf (dump_file, ")\n");
1477 for (i = 0; i < PHI_NUM_ARGS (loop_phi_node); i++)
1479 tree arg = PHI_ARG_DEF (loop_phi_node, i);
1480 tree ssa_chain, ev_fn;
1483 /* Select the edges that enter the loop body. */
1484 bb = PHI_ARG_EDGE (loop_phi_node, i)->src;
1485 if (!flow_bb_inside_loop_p (loop, bb))
1488 if (TREE_CODE (arg) == SSA_NAME)
1490 ssa_chain = SSA_NAME_DEF_STMT (arg);
1492 /* Pass in the initial condition to the follow edge function. */
1494 res = follow_ssa_edge (loop, ssa_chain, loop_phi_node, &ev_fn);
1499 /* When it is impossible to go back on the same
1500 loop_phi_node by following the ssa edges, the
1501 evolution is represented by a peeled chrec, i.e. the
1502 first iteration, EV_FN has the value INIT_COND, then
1503 all the other iterations it has the value of ARG.
1504 For the moment, PEELED_CHREC nodes are not built. */
1506 ev_fn = chrec_dont_know;
1508 /* When there are multiple back edges of the loop (which in fact never
1509 happens currently, but nevertheless), merge their evolutions. */
1510 evolution_function = chrec_merge (evolution_function, ev_fn);
1513 if (dump_file && (dump_flags & TDF_DETAILS))
1515 fprintf (dump_file, " (evolution_function = ");
1516 print_generic_expr (dump_file, evolution_function, 0);
1517 fprintf (dump_file, "))\n");
1520 return evolution_function;
1523 /* Given a loop-phi-node, return the initial conditions of the
1524 variable on entry of the loop. When the CCP has propagated
1525 constants into the loop-phi-node, the initial condition is
1526 instantiated, otherwise the initial condition is kept symbolic.
1527 This analyzer does not analyze the evolution outside the current
1528 loop, and leaves this task to the on-demand tree reconstructor. */
1531 analyze_initial_condition (tree loop_phi_node)
1534 tree init_cond = chrec_not_analyzed_yet;
1535 struct loop *loop = bb_for_stmt (loop_phi_node)->loop_father;
1537 if (dump_file && (dump_flags & TDF_DETAILS))
1539 fprintf (dump_file, "(analyze_initial_condition \n");
1540 fprintf (dump_file, " (loop_phi_node = \n");
1541 print_generic_expr (dump_file, loop_phi_node, 0);
1542 fprintf (dump_file, ")\n");
1545 for (i = 0; i < PHI_NUM_ARGS (loop_phi_node); i++)
1547 tree branch = PHI_ARG_DEF (loop_phi_node, i);
1548 basic_block bb = PHI_ARG_EDGE (loop_phi_node, i)->src;
1550 /* When the branch is oriented to the loop's body, it does
1551 not contribute to the initial condition. */
1552 if (flow_bb_inside_loop_p (loop, bb))
1555 if (init_cond == chrec_not_analyzed_yet)
1561 if (TREE_CODE (branch) == SSA_NAME)
1563 init_cond = chrec_dont_know;
1567 init_cond = chrec_merge (init_cond, branch);
1570 /* Ooops -- a loop without an entry??? */
1571 if (init_cond == chrec_not_analyzed_yet)
1572 init_cond = chrec_dont_know;
1574 if (dump_file && (dump_flags & TDF_DETAILS))
1576 fprintf (dump_file, " (init_cond = ");
1577 print_generic_expr (dump_file, init_cond, 0);
1578 fprintf (dump_file, "))\n");
1584 /* Analyze the scalar evolution for LOOP_PHI_NODE. */
1587 interpret_loop_phi (struct loop *loop, tree loop_phi_node)
1590 struct loop *phi_loop = loop_containing_stmt (loop_phi_node);
1593 if (phi_loop != loop)
1595 struct loop *subloop;
1596 tree evolution_fn = analyze_scalar_evolution
1597 (phi_loop, PHI_RESULT (loop_phi_node));
1599 /* Dive one level deeper. */
1600 subloop = superloop_at_depth (phi_loop, loop->depth + 1);
1602 /* Interpret the subloop. */
1603 res = compute_overall_effect_of_inner_loop (subloop, evolution_fn);
1607 /* Otherwise really interpret the loop phi. */
1608 init_cond = analyze_initial_condition (loop_phi_node);
1609 res = analyze_evolution_in_loop (loop_phi_node, init_cond);
1614 /* This function merges the branches of a condition-phi-node,
1615 contained in the outermost loop, and whose arguments are already
1619 interpret_condition_phi (struct loop *loop, tree condition_phi)
1622 tree res = chrec_not_analyzed_yet;
1624 for (i = 0; i < PHI_NUM_ARGS (condition_phi); i++)
1628 if (backedge_phi_arg_p (condition_phi, i))
1630 res = chrec_dont_know;
1634 branch_chrec = analyze_scalar_evolution
1635 (loop, PHI_ARG_DEF (condition_phi, i));
1637 res = chrec_merge (res, branch_chrec);
1643 /* Interpret the right hand side of a modify_expr OPND1. If we didn't
1644 analyze this node before, follow the definitions until ending
1645 either on an analyzed modify_expr, or on a loop-phi-node. On the
1646 return path, this function propagates evolutions (ala constant copy
1647 propagation). OPND1 is not a GIMPLE expression because we could
1648 analyze the effect of an inner loop: see interpret_loop_phi. */
1651 interpret_rhs_modify_expr (struct loop *loop, tree at_stmt,
1652 tree opnd1, tree type)
1654 tree res, opnd10, opnd11, chrec10, chrec11;
1656 if (is_gimple_min_invariant (opnd1))
1657 return chrec_convert (type, opnd1, at_stmt);
1659 switch (TREE_CODE (opnd1))
1662 opnd10 = TREE_OPERAND (opnd1, 0);
1663 opnd11 = TREE_OPERAND (opnd1, 1);
1664 chrec10 = analyze_scalar_evolution (loop, opnd10);
1665 chrec11 = analyze_scalar_evolution (loop, opnd11);
1666 chrec10 = chrec_convert (type, chrec10, at_stmt);
1667 chrec11 = chrec_convert (type, chrec11, at_stmt);
1668 res = chrec_fold_plus (type, chrec10, chrec11);
1672 opnd10 = TREE_OPERAND (opnd1, 0);
1673 opnd11 = TREE_OPERAND (opnd1, 1);
1674 chrec10 = analyze_scalar_evolution (loop, opnd10);
1675 chrec11 = analyze_scalar_evolution (loop, opnd11);
1676 chrec10 = chrec_convert (type, chrec10, at_stmt);
1677 chrec11 = chrec_convert (type, chrec11, at_stmt);
1678 res = chrec_fold_minus (type, chrec10, chrec11);
1682 opnd10 = TREE_OPERAND (opnd1, 0);
1683 chrec10 = analyze_scalar_evolution (loop, opnd10);
1684 chrec10 = chrec_convert (type, chrec10, at_stmt);
1685 res = chrec_fold_multiply (type, chrec10, SCALAR_FLOAT_TYPE_P (type)
1686 ? build_real (type, dconstm1)
1687 : build_int_cst_type (type, -1));
1691 opnd10 = TREE_OPERAND (opnd1, 0);
1692 opnd11 = TREE_OPERAND (opnd1, 1);
1693 chrec10 = analyze_scalar_evolution (loop, opnd10);
1694 chrec11 = analyze_scalar_evolution (loop, opnd11);
1695 chrec10 = chrec_convert (type, chrec10, at_stmt);
1696 chrec11 = chrec_convert (type, chrec11, at_stmt);
1697 res = chrec_fold_multiply (type, chrec10, chrec11);
1701 res = chrec_convert (type, analyze_scalar_evolution (loop, opnd1),
1706 opnd10 = ASSERT_EXPR_VAR (opnd1);
1707 res = chrec_convert (type, analyze_scalar_evolution (loop, opnd10),
1713 opnd10 = TREE_OPERAND (opnd1, 0);
1714 chrec10 = analyze_scalar_evolution (loop, opnd10);
1715 res = chrec_convert (type, chrec10, at_stmt);
1719 res = chrec_dont_know;
1728 /* This section contains all the entry points:
1729 - number_of_iterations_in_loop,
1730 - analyze_scalar_evolution,
1731 - instantiate_parameters.
1734 /* Compute and return the evolution function in WRTO_LOOP, the nearest
1735 common ancestor of DEF_LOOP and USE_LOOP. */
1738 compute_scalar_evolution_in_loop (struct loop *wrto_loop,
1739 struct loop *def_loop,
1743 if (def_loop == wrto_loop)
1746 def_loop = superloop_at_depth (def_loop, wrto_loop->depth + 1);
1747 res = compute_overall_effect_of_inner_loop (def_loop, ev);
1749 return analyze_scalar_evolution_1 (wrto_loop, res, chrec_not_analyzed_yet);
1752 /* Helper recursive function. */
1755 analyze_scalar_evolution_1 (struct loop *loop, tree var, tree res)
1757 tree def, type = TREE_TYPE (var);
1759 struct loop *def_loop;
1762 return chrec_dont_know;
1764 if (TREE_CODE (var) != SSA_NAME)
1765 return interpret_rhs_modify_expr (loop, NULL_TREE, var, type);
1767 def = SSA_NAME_DEF_STMT (var);
1768 bb = bb_for_stmt (def);
1769 def_loop = bb ? bb->loop_father : NULL;
1772 || !flow_bb_inside_loop_p (loop, bb))
1774 /* Keep the symbolic form. */
1779 if (res != chrec_not_analyzed_yet)
1781 if (loop != bb->loop_father)
1782 res = compute_scalar_evolution_in_loop
1783 (find_common_loop (loop, bb->loop_father), bb->loop_father, res);
1788 if (loop != def_loop)
1790 res = analyze_scalar_evolution_1 (def_loop, var, chrec_not_analyzed_yet);
1791 res = compute_scalar_evolution_in_loop (loop, def_loop, res);
1796 switch (TREE_CODE (def))
1799 res = interpret_rhs_modify_expr (loop, def, TREE_OPERAND (def, 1), type);
1803 if (loop_phi_node_p (def))
1804 res = interpret_loop_phi (loop, def);
1806 res = interpret_condition_phi (loop, def);
1810 res = chrec_dont_know;
1816 /* Keep the symbolic form. */
1817 if (res == chrec_dont_know)
1820 if (loop == def_loop)
1821 set_scalar_evolution (var, res);
1826 /* Entry point for the scalar evolution analyzer.
1827 Analyzes and returns the scalar evolution of the ssa_name VAR.
1828 LOOP_NB is the identifier number of the loop in which the variable
1831 Example of use: having a pointer VAR to a SSA_NAME node, STMT a
1832 pointer to the statement that uses this variable, in order to
1833 determine the evolution function of the variable, use the following
1836 unsigned loop_nb = loop_containing_stmt (stmt)->num;
1837 tree chrec_with_symbols = analyze_scalar_evolution (loop_nb, var);
1838 tree chrec_instantiated = instantiate_parameters
1839 (loop_nb, chrec_with_symbols);
1843 analyze_scalar_evolution (struct loop *loop, tree var)
1847 if (dump_file && (dump_flags & TDF_DETAILS))
1849 fprintf (dump_file, "(analyze_scalar_evolution \n");
1850 fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
1851 fprintf (dump_file, " (scalar = ");
1852 print_generic_expr (dump_file, var, 0);
1853 fprintf (dump_file, ")\n");
1856 res = analyze_scalar_evolution_1 (loop, var, get_scalar_evolution (var));
1858 if (TREE_CODE (var) == SSA_NAME && res == chrec_dont_know)
1861 if (dump_file && (dump_flags & TDF_DETAILS))
1862 fprintf (dump_file, ")\n");
1867 /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
1868 WRTO_LOOP (which should be a superloop of both USE_LOOP and definition
1872 analyze_scalar_evolution_in_loop (struct loop *wrto_loop, struct loop *use_loop,
1880 ev = analyze_scalar_evolution (use_loop, ev);
1881 ev = resolve_mixers (use_loop, ev);
1883 if (use_loop == wrto_loop)
1886 /* If the value of the use changes in the inner loop, we cannot express
1887 its value in the outer loop (we might try to return interval chrec,
1888 but we do not have a user for it anyway) */
1889 if (!no_evolution_in_loop_p (ev, use_loop->num, &val)
1891 return chrec_dont_know;
1893 use_loop = use_loop->outer;
1897 /* Returns instantiated value for VERSION in CACHE. */
1900 get_instantiated_value (htab_t cache, tree version)
1902 struct scev_info_str *info, pattern;
1904 pattern.var = version;
1905 info = htab_find (cache, &pattern);
1913 /* Sets instantiated value for VERSION to VAL in CACHE. */
1916 set_instantiated_value (htab_t cache, tree version, tree val)
1918 struct scev_info_str *info, pattern;
1921 pattern.var = version;
1922 slot = htab_find_slot (cache, &pattern, INSERT);
1927 info = *slot = new_scev_info_str (version);
1931 /* Return the closed_loop_phi node for VAR. If there is none, return
1935 loop_closed_phi_def (tree var)
1941 if (var == NULL_TREE
1942 || TREE_CODE (var) != SSA_NAME)
1945 loop = loop_containing_stmt (SSA_NAME_DEF_STMT (var));
1946 exit = loop->single_exit;
1950 for (phi = phi_nodes (exit->dest); phi; phi = PHI_CHAIN (phi))
1951 if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == var)
1952 return PHI_RESULT (phi);
1957 /* Analyze all the parameters of the chrec that were left under a symbolic form,
1958 with respect to LOOP. CHREC is the chrec to instantiate. CACHE is the cache
1959 of already instantiated values. FLAGS modify the way chrecs are
1962 /* Values for FLAGS. */
1965 INSERT_SUPERLOOP_CHRECS = 1, /* Loop invariants are replaced with chrecs
1967 FOLD_CONVERSIONS = 2 /* The conversions that may wrap in
1968 signed/pointer type are folded, as long as the
1969 value of the chrec is preserved. */
1973 instantiate_parameters_1 (struct loop *loop, tree chrec, int flags, htab_t cache)
1975 tree res, op0, op1, op2;
1977 struct loop *def_loop;
1979 if (automatically_generated_chrec_p (chrec)
1980 || is_gimple_min_invariant (chrec))
1983 switch (TREE_CODE (chrec))
1986 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (chrec));
1988 /* A parameter (or loop invariant and we do not want to include
1989 evolutions in outer loops), nothing to do. */
1991 || (!(flags & INSERT_SUPERLOOP_CHRECS)
1992 && !flow_bb_inside_loop_p (loop, def_bb)))
1995 /* We cache the value of instantiated variable to avoid exponential
1996 time complexity due to reevaluations. We also store the convenient
1997 value in the cache in order to prevent infinite recursion -- we do
1998 not want to instantiate the SSA_NAME if it is in a mixer
1999 structure. This is used for avoiding the instantiation of
2000 recursively defined functions, such as:
2002 | a_2 -> {0, +, 1, +, a_2}_1 */
2004 res = get_instantiated_value (cache, chrec);
2008 /* Store the convenient value for chrec in the structure. If it
2009 is defined outside of the loop, we may just leave it in symbolic
2010 form, otherwise we need to admit that we do not know its behavior
2012 res = !flow_bb_inside_loop_p (loop, def_bb) ? chrec : chrec_dont_know;
2013 set_instantiated_value (cache, chrec, res);
2015 /* To make things even more complicated, instantiate_parameters_1
2016 calls analyze_scalar_evolution that may call # of iterations
2017 analysis that may in turn call instantiate_parameters_1 again.
2018 To prevent the infinite recursion, keep also the bitmap of
2019 ssa names that are being instantiated globally. */
2020 if (bitmap_bit_p (already_instantiated, SSA_NAME_VERSION (chrec)))
2023 def_loop = find_common_loop (loop, def_bb->loop_father);
2025 /* If the analysis yields a parametric chrec, instantiate the
2027 bitmap_set_bit (already_instantiated, SSA_NAME_VERSION (chrec));
2028 res = analyze_scalar_evolution (def_loop, chrec);
2030 /* Don't instantiate loop-closed-ssa phi nodes. */
2031 if (TREE_CODE (res) == SSA_NAME
2032 && (loop_containing_stmt (SSA_NAME_DEF_STMT (res)) == NULL
2033 || (loop_containing_stmt (SSA_NAME_DEF_STMT (res))->depth
2034 > def_loop->depth)))
2037 res = loop_closed_phi_def (chrec);
2041 if (res == NULL_TREE)
2042 res = chrec_dont_know;
2045 else if (res != chrec_dont_know)
2046 res = instantiate_parameters_1 (loop, res, flags, cache);
2048 bitmap_clear_bit (already_instantiated, SSA_NAME_VERSION (chrec));
2050 /* Store the correct value to the cache. */
2051 set_instantiated_value (cache, chrec, res);
2054 case POLYNOMIAL_CHREC:
2055 op0 = instantiate_parameters_1 (loop, CHREC_LEFT (chrec),
2057 if (op0 == chrec_dont_know)
2058 return chrec_dont_know;
2060 op1 = instantiate_parameters_1 (loop, CHREC_RIGHT (chrec),
2062 if (op1 == chrec_dont_know)
2063 return chrec_dont_know;
2065 if (CHREC_LEFT (chrec) != op0
2066 || CHREC_RIGHT (chrec) != op1)
2067 chrec = build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1);
2071 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
2073 if (op0 == chrec_dont_know)
2074 return chrec_dont_know;
2076 op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
2078 if (op1 == chrec_dont_know)
2079 return chrec_dont_know;
2081 if (TREE_OPERAND (chrec, 0) != op0
2082 || TREE_OPERAND (chrec, 1) != op1)
2083 chrec = chrec_fold_plus (TREE_TYPE (chrec), op0, op1);
2087 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
2089 if (op0 == chrec_dont_know)
2090 return chrec_dont_know;
2092 op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
2094 if (op1 == chrec_dont_know)
2095 return chrec_dont_know;
2097 if (TREE_OPERAND (chrec, 0) != op0
2098 || TREE_OPERAND (chrec, 1) != op1)
2099 chrec = chrec_fold_minus (TREE_TYPE (chrec), op0, op1);
2103 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
2105 if (op0 == chrec_dont_know)
2106 return chrec_dont_know;
2108 op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
2110 if (op1 == chrec_dont_know)
2111 return chrec_dont_know;
2113 if (TREE_OPERAND (chrec, 0) != op0
2114 || TREE_OPERAND (chrec, 1) != op1)
2115 chrec = chrec_fold_multiply (TREE_TYPE (chrec), op0, op1);
2120 case NON_LVALUE_EXPR:
2121 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
2123 if (op0 == chrec_dont_know)
2124 return chrec_dont_know;
2126 if (flags & FOLD_CONVERSIONS)
2128 tree tmp = chrec_convert_aggressive (TREE_TYPE (chrec), op0);
2133 if (op0 == TREE_OPERAND (chrec, 0))
2136 return chrec_convert (TREE_TYPE (chrec), op0, NULL_TREE);
2138 case SCEV_NOT_KNOWN:
2139 return chrec_dont_know;
2148 switch (TREE_CODE_LENGTH (TREE_CODE (chrec)))
2151 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
2153 if (op0 == chrec_dont_know)
2154 return chrec_dont_know;
2156 op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
2158 if (op1 == chrec_dont_know)
2159 return chrec_dont_know;
2161 op2 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 2),
2163 if (op2 == chrec_dont_know)
2164 return chrec_dont_know;
2166 if (op0 == TREE_OPERAND (chrec, 0)
2167 && op1 == TREE_OPERAND (chrec, 1)
2168 && op2 == TREE_OPERAND (chrec, 2))
2171 return fold_build3 (TREE_CODE (chrec),
2172 TREE_TYPE (chrec), op0, op1, op2);
2175 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
2177 if (op0 == chrec_dont_know)
2178 return chrec_dont_know;
2180 op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
2182 if (op1 == chrec_dont_know)
2183 return chrec_dont_know;
2185 if (op0 == TREE_OPERAND (chrec, 0)
2186 && op1 == TREE_OPERAND (chrec, 1))
2188 return fold_build2 (TREE_CODE (chrec), TREE_TYPE (chrec), op0, op1);
2191 op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
2193 if (op0 == chrec_dont_know)
2194 return chrec_dont_know;
2195 if (op0 == TREE_OPERAND (chrec, 0))
2197 return fold_build1 (TREE_CODE (chrec), TREE_TYPE (chrec), op0);
2206 /* Too complicated to handle. */
2207 return chrec_dont_know;
2210 /* Analyze all the parameters of the chrec that were left under a
2211 symbolic form. LOOP is the loop in which symbolic names have to
2212 be analyzed and instantiated. */
2215 instantiate_parameters (struct loop *loop,
2219 htab_t cache = htab_create (10, hash_scev_info, eq_scev_info, del_scev_info);
2221 if (dump_file && (dump_flags & TDF_DETAILS))
2223 fprintf (dump_file, "(instantiate_parameters \n");
2224 fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
2225 fprintf (dump_file, " (chrec = ");
2226 print_generic_expr (dump_file, chrec, 0);
2227 fprintf (dump_file, ")\n");
2230 res = instantiate_parameters_1 (loop, chrec, INSERT_SUPERLOOP_CHRECS, cache);
2232 if (dump_file && (dump_flags & TDF_DETAILS))
2234 fprintf (dump_file, " (res = ");
2235 print_generic_expr (dump_file, res, 0);
2236 fprintf (dump_file, "))\n");
2239 htab_delete (cache);
2244 /* Similar to instantiate_parameters, but does not introduce the
2245 evolutions in outer loops for LOOP invariants in CHREC, and does not
2246 care about causing overflows, as long as they do not affect value
2247 of an expression. */
2250 resolve_mixers (struct loop *loop, tree chrec)
2252 htab_t cache = htab_create (10, hash_scev_info, eq_scev_info, del_scev_info);
2253 tree ret = instantiate_parameters_1 (loop, chrec, FOLD_CONVERSIONS, cache);
2254 htab_delete (cache);
2258 /* Entry point for the analysis of the number of iterations pass.
2259 This function tries to safely approximate the number of iterations
2260 the loop will run. When this property is not decidable at compile
2261 time, the result is chrec_dont_know. Otherwise the result is
2262 a scalar or a symbolic parameter.
2264 Example of analysis: suppose that the loop has an exit condition:
2266 "if (b > 49) goto end_loop;"
2268 and that in a previous analysis we have determined that the
2269 variable 'b' has an evolution function:
2271 "EF = {23, +, 5}_2".
2273 When we evaluate the function at the point 5, i.e. the value of the
2274 variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2275 and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2276 the loop body has been executed 6 times. */
2279 number_of_iterations_in_loop (struct loop *loop)
2283 struct tree_niter_desc niter_desc;
2285 /* Determine whether the number_of_iterations_in_loop has already
2287 res = loop->nb_iterations;
2290 res = chrec_dont_know;
2292 if (dump_file && (dump_flags & TDF_DETAILS))
2293 fprintf (dump_file, "(number_of_iterations_in_loop\n");
2295 exit = loop->single_exit;
2299 if (!number_of_iterations_exit (loop, exit, &niter_desc, false))
2302 type = TREE_TYPE (niter_desc.niter);
2303 if (integer_nonzerop (niter_desc.may_be_zero))
2304 res = build_int_cst (type, 0);
2305 else if (integer_zerop (niter_desc.may_be_zero))
2306 res = niter_desc.niter;
2308 res = chrec_dont_know;
2311 return set_nb_iterations_in_loop (loop, res);
2314 /* One of the drivers for testing the scalar evolutions analysis.
2315 This function computes the number of iterations for all the loops
2316 from the EXIT_CONDITIONS array. */
2319 number_of_iterations_for_all_loops (VEC(tree,heap) **exit_conditions)
2322 unsigned nb_chrec_dont_know_loops = 0;
2323 unsigned nb_static_loops = 0;
2326 for (i = 0; VEC_iterate (tree, *exit_conditions, i, cond); i++)
2328 tree res = number_of_iterations_in_loop (loop_containing_stmt (cond));
2329 if (chrec_contains_undetermined (res))
2330 nb_chrec_dont_know_loops++;
2337 fprintf (dump_file, "\n(\n");
2338 fprintf (dump_file, "-----------------------------------------\n");
2339 fprintf (dump_file, "%d\tnb_chrec_dont_know_loops\n", nb_chrec_dont_know_loops);
2340 fprintf (dump_file, "%d\tnb_static_loops\n", nb_static_loops);
2341 fprintf (dump_file, "%d\tnb_total_loops\n", current_loops->num);
2342 fprintf (dump_file, "-----------------------------------------\n");
2343 fprintf (dump_file, ")\n\n");
2345 print_loop_ir (dump_file);
2351 /* Counters for the stats. */
2357 unsigned nb_affine_multivar;
2358 unsigned nb_higher_poly;
2359 unsigned nb_chrec_dont_know;
2360 unsigned nb_undetermined;
2363 /* Reset the counters. */
2366 reset_chrecs_counters (struct chrec_stats *stats)
2368 stats->nb_chrecs = 0;
2369 stats->nb_affine = 0;
2370 stats->nb_affine_multivar = 0;
2371 stats->nb_higher_poly = 0;
2372 stats->nb_chrec_dont_know = 0;
2373 stats->nb_undetermined = 0;
2376 /* Dump the contents of a CHREC_STATS structure. */
2379 dump_chrecs_stats (FILE *file, struct chrec_stats *stats)
2381 fprintf (file, "\n(\n");
2382 fprintf (file, "-----------------------------------------\n");
2383 fprintf (file, "%d\taffine univariate chrecs\n", stats->nb_affine);
2384 fprintf (file, "%d\taffine multivariate chrecs\n", stats->nb_affine_multivar);
2385 fprintf (file, "%d\tdegree greater than 2 polynomials\n",
2386 stats->nb_higher_poly);
2387 fprintf (file, "%d\tchrec_dont_know chrecs\n", stats->nb_chrec_dont_know);
2388 fprintf (file, "-----------------------------------------\n");
2389 fprintf (file, "%d\ttotal chrecs\n", stats->nb_chrecs);
2390 fprintf (file, "%d\twith undetermined coefficients\n",
2391 stats->nb_undetermined);
2392 fprintf (file, "-----------------------------------------\n");
2393 fprintf (file, "%d\tchrecs in the scev database\n",
2394 (int) htab_elements (scalar_evolution_info));
2395 fprintf (file, "%d\tsets in the scev database\n", nb_set_scev);
2396 fprintf (file, "%d\tgets in the scev database\n", nb_get_scev);
2397 fprintf (file, "-----------------------------------------\n");
2398 fprintf (file, ")\n\n");
2401 /* Gather statistics about CHREC. */
2404 gather_chrec_stats (tree chrec, struct chrec_stats *stats)
2406 if (dump_file && (dump_flags & TDF_STATS))
2408 fprintf (dump_file, "(classify_chrec ");
2409 print_generic_expr (dump_file, chrec, 0);
2410 fprintf (dump_file, "\n");
2415 if (chrec == NULL_TREE)
2417 stats->nb_undetermined++;
2421 switch (TREE_CODE (chrec))
2423 case POLYNOMIAL_CHREC:
2424 if (evolution_function_is_affine_p (chrec))
2426 if (dump_file && (dump_flags & TDF_STATS))
2427 fprintf (dump_file, " affine_univariate\n");
2430 else if (evolution_function_is_affine_multivariate_p (chrec))
2432 if (dump_file && (dump_flags & TDF_STATS))
2433 fprintf (dump_file, " affine_multivariate\n");
2434 stats->nb_affine_multivar++;
2438 if (dump_file && (dump_flags & TDF_STATS))
2439 fprintf (dump_file, " higher_degree_polynomial\n");
2440 stats->nb_higher_poly++;
2449 if (chrec_contains_undetermined (chrec))
2451 if (dump_file && (dump_flags & TDF_STATS))
2452 fprintf (dump_file, " undetermined\n");
2453 stats->nb_undetermined++;
2456 if (dump_file && (dump_flags & TDF_STATS))
2457 fprintf (dump_file, ")\n");
2460 /* One of the drivers for testing the scalar evolutions analysis.
2461 This function analyzes the scalar evolution of all the scalars
2462 defined as loop phi nodes in one of the loops from the
2463 EXIT_CONDITIONS array.
2465 TODO Optimization: A loop is in canonical form if it contains only
2466 a single scalar loop phi node. All the other scalars that have an
2467 evolution in the loop are rewritten in function of this single
2468 index. This allows the parallelization of the loop. */
2471 analyze_scalar_evolution_for_all_loop_phi_nodes (VEC(tree,heap) **exit_conditions)
2474 struct chrec_stats stats;
2477 reset_chrecs_counters (&stats);
2479 for (i = 0; VEC_iterate (tree, *exit_conditions, i, cond); i++)
2485 loop = loop_containing_stmt (cond);
2488 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2489 if (is_gimple_reg (PHI_RESULT (phi)))
2491 chrec = instantiate_parameters
2493 analyze_scalar_evolution (loop, PHI_RESULT (phi)));
2495 if (dump_file && (dump_flags & TDF_STATS))
2496 gather_chrec_stats (chrec, &stats);
2500 if (dump_file && (dump_flags & TDF_STATS))
2501 dump_chrecs_stats (dump_file, &stats);
2504 /* Callback for htab_traverse, gathers information on chrecs in the
2508 gather_stats_on_scev_database_1 (void **slot, void *stats)
2510 struct scev_info_str *entry = *slot;
2512 gather_chrec_stats (entry->chrec, stats);
2517 /* Classify the chrecs of the whole database. */
2520 gather_stats_on_scev_database (void)
2522 struct chrec_stats stats;
2527 reset_chrecs_counters (&stats);
2529 htab_traverse (scalar_evolution_info, gather_stats_on_scev_database_1,
2532 dump_chrecs_stats (dump_file, &stats);
2540 initialize_scalar_evolutions_analyzer (void)
2542 /* The elements below are unique. */
2543 if (chrec_dont_know == NULL_TREE)
2545 chrec_not_analyzed_yet = NULL_TREE;
2546 chrec_dont_know = make_node (SCEV_NOT_KNOWN);
2547 chrec_known = make_node (SCEV_KNOWN);
2548 TREE_TYPE (chrec_dont_know) = void_type_node;
2549 TREE_TYPE (chrec_known) = void_type_node;
2553 /* Initialize the analysis of scalar evolutions for LOOPS. */
2556 scev_initialize (struct loops *loops)
2559 current_loops = loops;
2561 scalar_evolution_info = htab_create (100, hash_scev_info,
2562 eq_scev_info, del_scev_info);
2563 already_instantiated = BITMAP_ALLOC (NULL);
2565 initialize_scalar_evolutions_analyzer ();
2567 for (i = 1; i < loops->num; i++)
2568 if (loops->parray[i])
2569 loops->parray[i]->nb_iterations = NULL_TREE;
2572 /* Cleans up the information cached by the scalar evolutions analysis. */
2580 if (!scalar_evolution_info || !current_loops)
2583 htab_empty (scalar_evolution_info);
2584 for (i = 1; i < current_loops->num; i++)
2586 loop = current_loops->parray[i];
2588 loop->nb_iterations = NULL_TREE;
2592 /* Checks whether OP behaves as a simple affine iv of LOOP in STMT and returns
2593 its BASE and STEP if possible. If ALLOW_NONCONSTANT_STEP is true, we
2594 want STEP to be invariant in LOOP. Otherwise we require it to be an
2595 integer constant. */
2598 simple_iv (struct loop *loop, tree stmt, tree op, tree *base, tree *step,
2599 bool allow_nonconstant_step)
2601 basic_block bb = bb_for_stmt (stmt);
2607 type = TREE_TYPE (op);
2608 if (TREE_CODE (type) != INTEGER_TYPE
2609 && TREE_CODE (type) != POINTER_TYPE)
2612 ev = analyze_scalar_evolution_in_loop (loop, bb->loop_father, op);
2613 if (chrec_contains_undetermined (ev))
2616 if (tree_does_not_contain_chrecs (ev)
2617 && !chrec_contains_symbols_defined_in_loop (ev, loop->num))
2623 if (TREE_CODE (ev) != POLYNOMIAL_CHREC
2624 || CHREC_VARIABLE (ev) != (unsigned) loop->num)
2627 *step = CHREC_RIGHT (ev);
2628 if (allow_nonconstant_step)
2630 if (tree_contains_chrecs (*step, NULL)
2631 || chrec_contains_symbols_defined_in_loop (*step, loop->num))
2634 else if (TREE_CODE (*step) != INTEGER_CST)
2637 *base = CHREC_LEFT (ev);
2638 if (tree_contains_chrecs (*base, NULL)
2639 || chrec_contains_symbols_defined_in_loop (*base, loop->num))
2645 /* Runs the analysis of scalar evolutions. */
2648 scev_analysis (void)
2650 VEC(tree,heap) *exit_conditions;
2652 exit_conditions = VEC_alloc (tree, heap, 37);
2653 select_loops_exit_conditions (current_loops, &exit_conditions);
2655 if (dump_file && (dump_flags & TDF_STATS))
2656 analyze_scalar_evolution_for_all_loop_phi_nodes (&exit_conditions);
2658 number_of_iterations_for_all_loops (&exit_conditions);
2659 VEC_free (tree, heap, exit_conditions);
2662 /* Finalize the scalar evolution analysis. */
2665 scev_finalize (void)
2667 htab_delete (scalar_evolution_info);
2668 BITMAP_FREE (already_instantiated);
2671 /* Replace ssa names for that scev can prove they are constant by the
2672 appropriate constants. Also perform final value replacement in loops,
2673 in case the replacement expressions are cheap.
2675 We only consider SSA names defined by phi nodes; rest is left to the
2676 ordinary constant propagation pass. */
2679 scev_const_prop (void)
2682 tree name, phi, next_phi, type, ev;
2683 struct loop *loop, *ex_loop;
2684 bitmap ssa_names_to_remove = NULL;
2692 loop = bb->loop_father;
2694 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2696 name = PHI_RESULT (phi);
2698 if (!is_gimple_reg (name))
2701 type = TREE_TYPE (name);
2703 if (!POINTER_TYPE_P (type)
2704 && !INTEGRAL_TYPE_P (type))
2707 ev = resolve_mixers (loop, analyze_scalar_evolution (loop, name));
2708 if (!is_gimple_min_invariant (ev)
2709 || !may_propagate_copy (name, ev))
2712 /* Replace the uses of the name. */
2714 replace_uses_by (name, ev);
2716 if (!ssa_names_to_remove)
2717 ssa_names_to_remove = BITMAP_ALLOC (NULL);
2718 bitmap_set_bit (ssa_names_to_remove, SSA_NAME_VERSION (name));
2722 /* Remove the ssa names that were replaced by constants. We do not remove them
2723 directly in the previous cycle, since this invalidates scev cache. */
2724 if (ssa_names_to_remove)
2729 EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove, 0, i, bi)
2731 name = ssa_name (i);
2732 phi = SSA_NAME_DEF_STMT (name);
2734 gcc_assert (TREE_CODE (phi) == PHI_NODE);
2735 remove_phi_node (phi, NULL);
2738 BITMAP_FREE (ssa_names_to_remove);
2742 /* Now the regular final value replacement. */
2743 for (i = current_loops->num - 1; i > 0; i--)
2748 loop = current_loops->parray[i];
2752 /* If we do not know exact number of iterations of the loop, we cannot
2753 replace the final value. */
2754 exit = loop->single_exit;
2756 || number_of_iterations_in_loop (loop) == chrec_dont_know)
2758 ex_loop = exit->dest->loop_father;
2760 for (phi = phi_nodes (exit->dest); phi; phi = next_phi)
2762 next_phi = PHI_CHAIN (phi);
2763 def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
2764 if (!is_gimple_reg (def)
2765 || expr_invariant_in_loop_p (loop, def))
2768 if (!POINTER_TYPE_P (TREE_TYPE (def))
2769 && !INTEGRAL_TYPE_P (TREE_TYPE (def)))
2772 def = analyze_scalar_evolution_in_loop (ex_loop, ex_loop, def);
2773 if (!tree_does_not_contain_chrecs (def)
2774 || chrec_contains_symbols_defined_in_loop (def, loop->num)
2775 || def == PHI_RESULT (phi)
2776 || (TREE_CODE (def) == SSA_NAME
2777 && loop_containing_stmt (SSA_NAME_DEF_STMT (def))
2778 && loop_containing_stmt (phi)
2779 && loop_containing_stmt (SSA_NAME_DEF_STMT (def))
2780 == loop_containing_stmt (phi)))
2783 /* If computing the expression is expensive, let it remain in
2784 loop. TODO -- we should take the cost of computing the expression
2785 in loop into account. */
2786 if (force_expr_to_var_cost (def) >= target_spill_cost)
2788 def = unshare_expr (def);
2790 if (is_gimple_val (def))
2793 def = force_gimple_operand (def, &stmts, true,
2794 SSA_NAME_VAR (PHI_RESULT (phi)));
2795 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, exit), def);
2797 compute_phi_arg_on_exit (exit, stmts, def);