1 /* Scalar evolution detector.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Sebastian Pop <s.pop@laposte.net>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 GIMPLE_ASSIGN: 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 (loop_1, {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 2a: 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 2b: Multivariate chains of recurrences.
171 Analyzing the access function of array A with
172 instantiate_parameters (loop_1, "j + k"), we obtain the
173 instantiation and the analysis of the scalar variables "j" and "k"
174 in loop_1. This leads to the scalar evolution {4, +, 1}_1: the end
175 value of loop_2 for "j" is 4, and the evolution of "k" in loop_1 is
176 {0, +, 1}_1. To obtain the evolution function in loop_3 and
177 instantiate the scalar variables up to loop_1, one has to use:
178 instantiate_scev (block_before_loop (loop_1), loop_3, "j + k").
179 The result of this call is {{0, +, 1}_1, +, 1}_2.
181 Example 3: Higher degree polynomials.
195 instantiate_parameters (loop_1, {5, +, a}_1) -> {5, +, 2, +, 1}_1
196 instantiate_parameters (loop_1, {5 + a, +, a}_1) -> {7, +, 3, +, 1}_1
198 Example 4: Lucas, Fibonacci, or mixers in general.
210 The syntax "(1, c)_1" stands for a PEELED_CHREC that has the
211 following semantics: during the first iteration of the loop_1, the
212 variable contains the value 1, and then it contains the value "c".
213 Note that this syntax is close to the syntax of the loop-phi-node:
214 "a -> (1, c)_1" vs. "a = phi (1, c)".
216 The symbolic chrec representation contains all the semantics of the
217 original code. What is more difficult is to use this information.
219 Example 5: Flip-flops, or exchangers.
231 Based on these symbolic chrecs, it is possible to refine this
232 information into the more precise PERIODIC_CHRECs:
237 This transformation is not yet implemented.
241 You can find a more detailed description of the algorithm in:
242 http://icps.u-strasbg.fr/~pop/DEA_03_Pop.pdf
243 http://icps.u-strasbg.fr/~pop/DEA_03_Pop.ps.gz. But note that
244 this is a preliminary report and some of the details of the
245 algorithm have changed. I'm working on a research report that
246 updates the description of the algorithms to reflect the design
247 choices used in this implementation.
249 A set of slides show a high level overview of the algorithm and run
250 an example through the scalar evolution analyzer:
251 http://cri.ensmp.fr/~pop/gcc/mar04/slides.pdf
253 The slides that I have presented at the GCC Summit'04 are available
254 at: http://cri.ensmp.fr/~pop/gcc/20040604/gccsummit-lno-spop.pdf
259 #include "coretypes.h"
265 /* These RTL headers are needed for basic-block.h. */
267 #include "basic-block.h"
268 #include "diagnostic.h"
269 #include "tree-flow.h"
270 #include "tree-dump.h"
273 #include "tree-chrec.h"
274 #include "tree-scalar-evolution.h"
275 #include "tree-pass.h"
279 static tree
analyze_scalar_evolution_1 (struct loop
*, tree
, tree
);
281 /* The cached information about an SSA name VAR, claiming that below
282 basic block INSTANTIATED_BELOW, the value of VAR can be expressed
285 struct GTY(()) scev_info_str
{
286 basic_block instantiated_below
;
291 /* Counters for the scev database. */
292 static unsigned nb_set_scev
= 0;
293 static unsigned nb_get_scev
= 0;
295 /* The following trees are unique elements. Thus the comparison of
296 another element to these elements should be done on the pointer to
297 these trees, and not on their value. */
299 /* The SSA_NAMEs that are not yet analyzed are qualified with NULL_TREE. */
300 tree chrec_not_analyzed_yet
;
302 /* Reserved to the cases where the analyzer has detected an
303 undecidable property at compile time. */
304 tree chrec_dont_know
;
306 /* When the analyzer has detected that a property will never
307 happen, then it qualifies it with chrec_known. */
310 static GTY ((param_is (struct scev_info_str
))) htab_t scalar_evolution_info
;
313 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
315 static inline struct scev_info_str
*
316 new_scev_info_str (basic_block instantiated_below
, tree var
)
318 struct scev_info_str
*res
;
320 res
= GGC_NEW (struct scev_info_str
);
322 res
->chrec
= chrec_not_analyzed_yet
;
323 res
->instantiated_below
= instantiated_below
;
328 /* Computes a hash function for database element ELT. */
331 hash_scev_info (const void *elt
)
333 return SSA_NAME_VERSION (((const struct scev_info_str
*) elt
)->var
);
336 /* Compares database elements E1 and E2. */
339 eq_scev_info (const void *e1
, const void *e2
)
341 const struct scev_info_str
*elt1
= (const struct scev_info_str
*) e1
;
342 const struct scev_info_str
*elt2
= (const struct scev_info_str
*) e2
;
344 return (elt1
->var
== elt2
->var
345 && elt1
->instantiated_below
== elt2
->instantiated_below
);
348 /* Deletes database element E. */
351 del_scev_info (void *e
)
356 /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block.
357 A first query on VAR returns chrec_not_analyzed_yet. */
360 find_var_scev_info (basic_block instantiated_below
, tree var
)
362 struct scev_info_str
*res
;
363 struct scev_info_str tmp
;
367 tmp
.instantiated_below
= instantiated_below
;
368 slot
= htab_find_slot (scalar_evolution_info
, &tmp
, INSERT
);
371 *slot
= new_scev_info_str (instantiated_below
, var
);
372 res
= (struct scev_info_str
*) *slot
;
377 /* Return true when CHREC contains symbolic names defined in
381 chrec_contains_symbols_defined_in_loop (const_tree chrec
, unsigned loop_nb
)
385 if (chrec
== NULL_TREE
)
388 if (is_gimple_min_invariant (chrec
))
391 if (TREE_CODE (chrec
) == VAR_DECL
392 || TREE_CODE (chrec
) == PARM_DECL
393 || TREE_CODE (chrec
) == FUNCTION_DECL
394 || TREE_CODE (chrec
) == LABEL_DECL
395 || TREE_CODE (chrec
) == RESULT_DECL
396 || TREE_CODE (chrec
) == FIELD_DECL
)
399 if (TREE_CODE (chrec
) == SSA_NAME
)
401 gimple def
= SSA_NAME_DEF_STMT (chrec
);
402 struct loop
*def_loop
= loop_containing_stmt (def
);
403 struct loop
*loop
= get_loop (loop_nb
);
405 if (def_loop
== NULL
)
408 if (loop
== def_loop
|| flow_loop_nested_p (loop
, def_loop
))
414 n
= TREE_OPERAND_LENGTH (chrec
);
415 for (i
= 0; i
< n
; i
++)
416 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec
, i
),
422 /* Return true when PHI is a loop-phi-node. */
425 loop_phi_node_p (gimple phi
)
427 /* The implementation of this function is based on the following
428 property: "all the loop-phi-nodes of a loop are contained in the
429 loop's header basic block". */
431 return loop_containing_stmt (phi
)->header
== gimple_bb (phi
);
434 /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
435 In general, in the case of multivariate evolutions we want to get
436 the evolution in different loops. LOOP specifies the level for
437 which to get the evolution.
441 | for (j = 0; j < 100; j++)
443 | for (k = 0; k < 100; k++)
445 | i = k + j; - Here the value of i is a function of j, k.
447 | ... = i - Here the value of i is a function of j.
449 | ... = i - Here the value of i is a scalar.
455 | i_1 = phi (i_0, i_2)
459 This loop has the same effect as:
460 LOOP_1 has the same effect as:
464 The overall effect of the loop, "i_0 + 20" in the previous example,
465 is obtained by passing in the parameters: LOOP = 1,
466 EVOLUTION_FN = {i_0, +, 2}_1.
470 compute_overall_effect_of_inner_loop (struct loop
*loop
, tree evolution_fn
)
474 if (evolution_fn
== chrec_dont_know
)
475 return chrec_dont_know
;
477 else if (TREE_CODE (evolution_fn
) == POLYNOMIAL_CHREC
)
479 struct loop
*inner_loop
= get_chrec_loop (evolution_fn
);
481 if (inner_loop
== loop
482 || flow_loop_nested_p (loop
, inner_loop
))
484 tree nb_iter
= number_of_latch_executions (inner_loop
);
486 if (nb_iter
== chrec_dont_know
)
487 return chrec_dont_know
;
492 /* evolution_fn is the evolution function in LOOP. Get
493 its value in the nb_iter-th iteration. */
494 res
= chrec_apply (inner_loop
->num
, evolution_fn
, nb_iter
);
496 /* Continue the computation until ending on a parent of LOOP. */
497 return compute_overall_effect_of_inner_loop (loop
, res
);
504 /* If the evolution function is an invariant, there is nothing to do. */
505 else if (no_evolution_in_loop_p (evolution_fn
, loop
->num
, &val
) && val
)
509 return chrec_dont_know
;
512 /* Determine whether the CHREC is always positive/negative. If the expression
513 cannot be statically analyzed, return false, otherwise set the answer into
517 chrec_is_positive (tree chrec
, bool *value
)
519 bool value0
, value1
, value2
;
520 tree end_value
, nb_iter
;
522 switch (TREE_CODE (chrec
))
524 case POLYNOMIAL_CHREC
:
525 if (!chrec_is_positive (CHREC_LEFT (chrec
), &value0
)
526 || !chrec_is_positive (CHREC_RIGHT (chrec
), &value1
))
529 /* FIXME -- overflows. */
530 if (value0
== value1
)
536 /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
537 and the proof consists in showing that the sign never
538 changes during the execution of the loop, from 0 to
539 loop->nb_iterations. */
540 if (!evolution_function_is_affine_p (chrec
))
543 nb_iter
= number_of_latch_executions (get_chrec_loop (chrec
));
544 if (chrec_contains_undetermined (nb_iter
))
548 /* TODO -- If the test is after the exit, we may decrease the number of
549 iterations by one. */
551 nb_iter
= chrec_fold_minus (type
, nb_iter
, build_int_cst (type
, 1));
554 end_value
= chrec_apply (CHREC_VARIABLE (chrec
), chrec
, nb_iter
);
556 if (!chrec_is_positive (end_value
, &value2
))
560 return value0
== value1
;
563 *value
= (tree_int_cst_sgn (chrec
) == 1);
571 /* Associate CHREC to SCALAR. */
574 set_scalar_evolution (basic_block instantiated_below
, tree scalar
, tree chrec
)
578 if (TREE_CODE (scalar
) != SSA_NAME
)
581 scalar_info
= find_var_scev_info (instantiated_below
, scalar
);
585 if (dump_flags
& TDF_DETAILS
)
587 fprintf (dump_file
, "(set_scalar_evolution \n");
588 fprintf (dump_file
, " instantiated_below = %d \n",
589 instantiated_below
->index
);
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");
596 if (dump_flags
& TDF_STATS
)
600 *scalar_info
= chrec
;
603 /* Retrieve the chrec associated to SCALAR instantiated below
604 INSTANTIATED_BELOW block. */
607 get_scalar_evolution (basic_block instantiated_below
, 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 (instantiated_below
, scalar
);
637 res
= chrec_not_analyzed_yet
;
641 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
643 fprintf (dump_file
, " (scalar_evolution = ");
644 print_generic_expr (dump_file
, res
, 0);
645 fprintf (dump_file
, "))\n");
651 /* Helper function for add_to_evolution. Returns the evolution
652 function for an assignment of the form "a = b + c", where "a" and
653 "b" are on the strongly connected component. CHREC_BEFORE is the
654 information that we already have collected up to this point.
655 TO_ADD is the evolution of "c".
657 When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
658 evolution the expression TO_ADD, otherwise construct an evolution
659 part for this loop. */
662 add_to_evolution_1 (unsigned loop_nb
, tree chrec_before
, tree to_add
,
665 tree type
, left
, right
;
666 struct loop
*loop
= get_loop (loop_nb
), *chloop
;
668 switch (TREE_CODE (chrec_before
))
670 case POLYNOMIAL_CHREC
:
671 chloop
= get_chrec_loop (chrec_before
);
673 || flow_loop_nested_p (chloop
, loop
))
677 type
= chrec_type (chrec_before
);
679 /* When there is no evolution part in this loop, build it. */
684 right
= SCALAR_FLOAT_TYPE_P (type
)
685 ? build_real (type
, dconst0
)
686 : build_int_cst (type
, 0);
690 var
= CHREC_VARIABLE (chrec_before
);
691 left
= CHREC_LEFT (chrec_before
);
692 right
= CHREC_RIGHT (chrec_before
);
695 to_add
= chrec_convert (type
, to_add
, at_stmt
);
696 right
= chrec_convert_rhs (type
, right
, at_stmt
);
697 right
= chrec_fold_plus (chrec_type (right
), right
, to_add
);
698 return build_polynomial_chrec (var
, left
, right
);
702 gcc_assert (flow_loop_nested_p (loop
, chloop
));
704 /* Search the evolution in LOOP_NB. */
705 left
= add_to_evolution_1 (loop_nb
, CHREC_LEFT (chrec_before
),
707 right
= CHREC_RIGHT (chrec_before
);
708 right
= chrec_convert_rhs (chrec_type (left
), right
, at_stmt
);
709 return build_polynomial_chrec (CHREC_VARIABLE (chrec_before
),
714 /* These nodes do not depend on a loop. */
715 if (chrec_before
== chrec_dont_know
)
716 return chrec_dont_know
;
719 right
= chrec_convert_rhs (chrec_type (left
), to_add
, at_stmt
);
720 return build_polynomial_chrec (loop_nb
, left
, right
);
724 /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
727 Description (provided for completeness, for those who read code in
728 a plane, and for my poor 62 bytes brain that would have forgotten
729 all this in the next two or three months):
731 The algorithm of translation of programs from the SSA representation
732 into the chrecs syntax is based on a pattern matching. After having
733 reconstructed the overall tree expression for a loop, there are only
734 two cases that can arise:
736 1. a = loop-phi (init, a + expr)
737 2. a = loop-phi (init, expr)
739 where EXPR is either a scalar constant with respect to the analyzed
740 loop (this is a degree 0 polynomial), or an expression containing
741 other loop-phi definitions (these are higher degree polynomials).
748 | a = phi (init, a + 5)
755 | a = phi (inita, 2 * b + 3)
756 | b = phi (initb, b + 1)
759 For the first case, the semantics of the SSA representation is:
761 | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
763 that is, there is a loop index "x" that determines the scalar value
764 of the variable during the loop execution. During the first
765 iteration, the value is that of the initial condition INIT, while
766 during the subsequent iterations, it is the sum of the initial
767 condition with the sum of all the values of EXPR from the initial
768 iteration to the before last considered iteration.
770 For the second case, the semantics of the SSA program is:
772 | a (x) = init, if x = 0;
773 | expr (x - 1), otherwise.
775 The second case corresponds to the PEELED_CHREC, whose syntax is
776 close to the syntax of a loop-phi-node:
778 | phi (init, expr) vs. (init, expr)_x
780 The proof of the translation algorithm for the first case is a
781 proof by structural induction based on the degree of EXPR.
784 When EXPR is a constant with respect to the analyzed loop, or in
785 other words when EXPR is a polynomial of degree 0, the evolution of
786 the variable A in the loop is an affine function with an initial
787 condition INIT, and a step EXPR. In order to show this, we start
788 from the semantics of the SSA representation:
790 f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
792 and since "expr (j)" is a constant with respect to "j",
794 f (x) = init + x * expr
796 Finally, based on the semantics of the pure sum chrecs, by
797 identification we get the corresponding chrecs syntax:
799 f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
800 f (x) -> {init, +, expr}_x
803 Suppose that EXPR is a polynomial of degree N with respect to the
804 analyzed loop_x for which we have already determined that it is
805 written under the chrecs syntax:
807 | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
809 We start from the semantics of the SSA program:
811 | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
813 | f (x) = init + \sum_{j = 0}^{x - 1}
814 | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
816 | f (x) = init + \sum_{j = 0}^{x - 1}
817 | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
819 | f (x) = init + \sum_{k = 0}^{n - 1}
820 | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
822 | f (x) = init + \sum_{k = 0}^{n - 1}
823 | (b_k * \binom{x}{k + 1})
825 | f (x) = init + b_0 * \binom{x}{1} + ...
826 | + b_{n-1} * \binom{x}{n}
828 | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
829 | + b_{n-1} * \binom{x}{n}
832 And finally from the definition of the chrecs syntax, we identify:
833 | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
835 This shows the mechanism that stands behind the add_to_evolution
836 function. An important point is that the use of symbolic
837 parameters avoids the need of an analysis schedule.
844 | a = phi (inita, a + 2 + b)
845 | b = phi (initb, b + 1)
848 When analyzing "a", the algorithm keeps "b" symbolically:
850 | a -> {inita, +, 2 + b}_1
852 Then, after instantiation, the analyzer ends on the evolution:
854 | a -> {inita, +, 2 + initb, +, 1}_1
859 add_to_evolution (unsigned loop_nb
, tree chrec_before
, enum tree_code code
,
860 tree to_add
, gimple at_stmt
)
862 tree type
= chrec_type (to_add
);
863 tree res
= NULL_TREE
;
865 if (to_add
== NULL_TREE
)
868 /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
869 instantiated at this point. */
870 if (TREE_CODE (to_add
) == POLYNOMIAL_CHREC
)
871 /* This should not happen. */
872 return chrec_dont_know
;
874 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
876 fprintf (dump_file
, "(add_to_evolution \n");
877 fprintf (dump_file
, " (loop_nb = %d)\n", loop_nb
);
878 fprintf (dump_file
, " (chrec_before = ");
879 print_generic_expr (dump_file
, chrec_before
, 0);
880 fprintf (dump_file
, ")\n (to_add = ");
881 print_generic_expr (dump_file
, to_add
, 0);
882 fprintf (dump_file
, ")\n");
885 if (code
== MINUS_EXPR
)
886 to_add
= chrec_fold_multiply (type
, to_add
, SCALAR_FLOAT_TYPE_P (type
)
887 ? build_real (type
, dconstm1
)
888 : build_int_cst_type (type
, -1));
890 res
= add_to_evolution_1 (loop_nb
, chrec_before
, to_add
, at_stmt
);
892 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
894 fprintf (dump_file
, " (res = ");
895 print_generic_expr (dump_file
, res
, 0);
896 fprintf (dump_file
, "))\n");
902 /* Helper function. */
905 set_nb_iterations_in_loop (struct loop
*loop
,
908 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
910 fprintf (dump_file
, " (set_nb_iterations_in_loop = ");
911 print_generic_expr (dump_file
, res
, 0);
912 fprintf (dump_file
, "))\n");
915 loop
->nb_iterations
= res
;
921 /* This section selects the loops that will be good candidates for the
922 scalar evolution analysis. For the moment, greedily select all the
923 loop nests we could analyze. */
925 /* For a loop with a single exit edge, return the COND_EXPR that
926 guards the exit edge. If the expression is too difficult to
927 analyze, then give up. */
930 get_loop_exit_condition (const struct loop
*loop
)
933 edge exit_edge
= single_exit (loop
);
935 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
936 fprintf (dump_file
, "(get_loop_exit_condition \n ");
942 stmt
= last_stmt (exit_edge
->src
);
943 if (gimple_code (stmt
) == GIMPLE_COND
)
947 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
949 print_gimple_stmt (dump_file
, res
, 0, 0);
950 fprintf (dump_file
, ")\n");
956 /* Recursively determine and enqueue the exit conditions for a loop. */
959 get_exit_conditions_rec (struct loop
*loop
,
960 VEC(gimple
,heap
) **exit_conditions
)
965 /* Recurse on the inner loops, then on the next (sibling) loops. */
966 get_exit_conditions_rec (loop
->inner
, exit_conditions
);
967 get_exit_conditions_rec (loop
->next
, exit_conditions
);
969 if (single_exit (loop
))
971 gimple loop_condition
= get_loop_exit_condition (loop
);
974 VEC_safe_push (gimple
, heap
, *exit_conditions
, loop_condition
);
978 /* Select the candidate loop nests for the analysis. This function
979 initializes the EXIT_CONDITIONS array. */
982 select_loops_exit_conditions (VEC(gimple
,heap
) **exit_conditions
)
984 struct loop
*function_body
= current_loops
->tree_root
;
986 get_exit_conditions_rec (function_body
->inner
, exit_conditions
);
990 /* Depth first search algorithm. */
992 typedef enum t_bool
{
999 static t_bool
follow_ssa_edge (struct loop
*loop
, gimple
, gimple
, tree
*, int);
1001 /* Follow the ssa edge into the binary expression RHS0 CODE RHS1.
1002 Return true if the strongly connected component has been found. */
1005 follow_ssa_edge_binary (struct loop
*loop
, gimple at_stmt
,
1006 tree type
, tree rhs0
, enum tree_code code
, tree rhs1
,
1007 gimple halting_phi
, tree
*evolution_of_loop
, int limit
)
1009 t_bool res
= t_false
;
1014 case POINTER_PLUS_EXPR
:
1016 if (TREE_CODE (rhs0
) == SSA_NAME
)
1018 if (TREE_CODE (rhs1
) == SSA_NAME
)
1020 /* Match an assignment under the form:
1023 /* We want only assignments of form "name + name" contribute to
1024 LIMIT, as the other cases do not necessarily contribute to
1025 the complexity of the expression. */
1028 evol
= *evolution_of_loop
;
1029 res
= follow_ssa_edge
1030 (loop
, SSA_NAME_DEF_STMT (rhs0
), halting_phi
, &evol
, limit
);
1033 *evolution_of_loop
= add_to_evolution
1035 chrec_convert (type
, evol
, at_stmt
),
1036 code
, rhs1
, at_stmt
);
1038 else if (res
== t_false
)
1040 res
= follow_ssa_edge
1041 (loop
, SSA_NAME_DEF_STMT (rhs1
), halting_phi
,
1042 evolution_of_loop
, limit
);
1045 *evolution_of_loop
= add_to_evolution
1047 chrec_convert (type
, *evolution_of_loop
, at_stmt
),
1048 code
, rhs0
, at_stmt
);
1050 else if (res
== t_dont_know
)
1051 *evolution_of_loop
= chrec_dont_know
;
1054 else if (res
== t_dont_know
)
1055 *evolution_of_loop
= chrec_dont_know
;
1060 /* Match an assignment under the form:
1062 res
= follow_ssa_edge
1063 (loop
, SSA_NAME_DEF_STMT (rhs0
), halting_phi
,
1064 evolution_of_loop
, limit
);
1066 *evolution_of_loop
= add_to_evolution
1067 (loop
->num
, chrec_convert (type
, *evolution_of_loop
,
1069 code
, rhs1
, at_stmt
);
1071 else if (res
== t_dont_know
)
1072 *evolution_of_loop
= chrec_dont_know
;
1076 else if (TREE_CODE (rhs1
) == SSA_NAME
)
1078 /* Match an assignment under the form:
1080 res
= follow_ssa_edge
1081 (loop
, SSA_NAME_DEF_STMT (rhs1
), halting_phi
,
1082 evolution_of_loop
, limit
);
1084 *evolution_of_loop
= add_to_evolution
1085 (loop
->num
, chrec_convert (type
, *evolution_of_loop
,
1087 code
, rhs0
, at_stmt
);
1089 else if (res
== t_dont_know
)
1090 *evolution_of_loop
= chrec_dont_know
;
1094 /* Otherwise, match an assignment under the form:
1096 /* And there is nothing to do. */
1101 /* This case is under the form "opnd0 = rhs0 - rhs1". */
1102 if (TREE_CODE (rhs0
) == SSA_NAME
)
1104 /* Match an assignment under the form:
1107 /* We want only assignments of form "name - name" contribute to
1108 LIMIT, as the other cases do not necessarily contribute to
1109 the complexity of the expression. */
1110 if (TREE_CODE (rhs1
) == SSA_NAME
)
1113 res
= follow_ssa_edge (loop
, SSA_NAME_DEF_STMT (rhs0
), halting_phi
,
1114 evolution_of_loop
, limit
);
1116 *evolution_of_loop
= add_to_evolution
1117 (loop
->num
, chrec_convert (type
, *evolution_of_loop
, at_stmt
),
1118 MINUS_EXPR
, rhs1
, at_stmt
);
1120 else if (res
== t_dont_know
)
1121 *evolution_of_loop
= chrec_dont_know
;
1124 /* Otherwise, match an assignment under the form:
1126 /* And there is nothing to do. */
1137 /* Follow the ssa edge into the expression EXPR.
1138 Return true if the strongly connected component has been found. */
1141 follow_ssa_edge_expr (struct loop
*loop
, gimple at_stmt
, tree expr
,
1142 gimple halting_phi
, tree
*evolution_of_loop
, int limit
)
1144 t_bool res
= t_false
;
1146 tree type
= TREE_TYPE (expr
);
1147 enum tree_code code
;
1149 /* The EXPR is one of the following cases:
1153 - a POINTER_PLUS_EXPR,
1156 - other cases are not yet handled. */
1157 code
= TREE_CODE (expr
);
1161 /* This assignment is under the form "a_1 = (cast) rhs. */
1162 res
= follow_ssa_edge_expr (loop
, at_stmt
, TREE_OPERAND (expr
, 0),
1163 halting_phi
, evolution_of_loop
, limit
);
1164 *evolution_of_loop
= chrec_convert (type
, *evolution_of_loop
, at_stmt
);
1168 /* This assignment is under the form "a_1 = 7". */
1173 /* This assignment is under the form: "a_1 = b_2". */
1174 res
= follow_ssa_edge
1175 (loop
, SSA_NAME_DEF_STMT (expr
), halting_phi
, evolution_of_loop
, limit
);
1178 case POINTER_PLUS_EXPR
:
1181 /* This case is under the form "rhs0 +- rhs1". */
1182 rhs0
= TREE_OPERAND (expr
, 0);
1183 rhs1
= TREE_OPERAND (expr
, 1);
1184 STRIP_TYPE_NOPS (rhs0
);
1185 STRIP_TYPE_NOPS (rhs1
);
1186 return follow_ssa_edge_binary (loop
, at_stmt
, type
, rhs0
, code
, rhs1
,
1187 halting_phi
, evolution_of_loop
, limit
);
1191 /* This assignment is of the form: "a_1 = ASSERT_EXPR <a_2, ...>"
1192 It must be handled as a copy assignment of the form a_1 = a_2. */
1193 tree op0
= ASSERT_EXPR_VAR (expr
);
1194 if (TREE_CODE (op0
) == SSA_NAME
)
1195 res
= follow_ssa_edge (loop
, SSA_NAME_DEF_STMT (op0
),
1196 halting_phi
, evolution_of_loop
, limit
);
1211 /* Follow the ssa edge into the right hand side of an assignment STMT.
1212 Return true if the strongly connected component has been found. */
1215 follow_ssa_edge_in_rhs (struct loop
*loop
, gimple stmt
,
1216 gimple halting_phi
, tree
*evolution_of_loop
, int limit
)
1218 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1219 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1221 switch (get_gimple_rhs_class (code
))
1223 case GIMPLE_BINARY_RHS
:
1224 return follow_ssa_edge_binary (loop
, stmt
, type
,
1225 gimple_assign_rhs1 (stmt
), code
,
1226 gimple_assign_rhs2 (stmt
),
1227 halting_phi
, evolution_of_loop
, limit
);
1228 case GIMPLE_SINGLE_RHS
:
1229 return follow_ssa_edge_expr (loop
, stmt
, gimple_assign_rhs1 (stmt
),
1230 halting_phi
, evolution_of_loop
, limit
);
1231 case GIMPLE_UNARY_RHS
:
1232 if (code
== NOP_EXPR
)
1234 /* This assignment is under the form "a_1 = (cast) rhs. */
1236 = follow_ssa_edge_expr (loop
, stmt
, gimple_assign_rhs1 (stmt
),
1237 halting_phi
, evolution_of_loop
, limit
);
1238 *evolution_of_loop
= chrec_convert (type
, *evolution_of_loop
, stmt
);
1248 /* Checks whether the I-th argument of a PHI comes from a backedge. */
1251 backedge_phi_arg_p (gimple phi
, int i
)
1253 const_edge e
= gimple_phi_arg_edge (phi
, i
);
1255 /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
1256 about updating it anywhere, and this should work as well most of the
1258 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1264 /* Helper function for one branch of the condition-phi-node. Return
1265 true if the strongly connected component has been found following
1268 static inline t_bool
1269 follow_ssa_edge_in_condition_phi_branch (int i
,
1271 gimple condition_phi
,
1273 tree
*evolution_of_branch
,
1274 tree init_cond
, int limit
)
1276 tree branch
= PHI_ARG_DEF (condition_phi
, i
);
1277 *evolution_of_branch
= chrec_dont_know
;
1279 /* Do not follow back edges (they must belong to an irreducible loop, which
1280 we really do not want to worry about). */
1281 if (backedge_phi_arg_p (condition_phi
, i
))
1284 if (TREE_CODE (branch
) == SSA_NAME
)
1286 *evolution_of_branch
= init_cond
;
1287 return follow_ssa_edge (loop
, SSA_NAME_DEF_STMT (branch
), halting_phi
,
1288 evolution_of_branch
, limit
);
1291 /* This case occurs when one of the condition branches sets
1292 the variable to a constant: i.e. a phi-node like
1293 "a_2 = PHI <a_7(5), 2(6)>;".
1295 FIXME: This case have to be refined correctly:
1296 in some cases it is possible to say something better than
1297 chrec_dont_know, for example using a wrap-around notation. */
1301 /* This function merges the branches of a condition-phi-node in a
1305 follow_ssa_edge_in_condition_phi (struct loop
*loop
,
1306 gimple condition_phi
,
1308 tree
*evolution_of_loop
, int limit
)
1311 tree init
= *evolution_of_loop
;
1312 tree evolution_of_branch
;
1313 t_bool res
= follow_ssa_edge_in_condition_phi_branch (0, loop
, condition_phi
,
1315 &evolution_of_branch
,
1317 if (res
== t_false
|| res
== t_dont_know
)
1320 *evolution_of_loop
= evolution_of_branch
;
1322 /* If the phi node is just a copy, do not increase the limit. */
1323 n
= gimple_phi_num_args (condition_phi
);
1327 for (i
= 1; i
< n
; i
++)
1329 /* Quickly give up when the evolution of one of the branches is
1331 if (*evolution_of_loop
== chrec_dont_know
)
1334 res
= follow_ssa_edge_in_condition_phi_branch (i
, loop
, condition_phi
,
1336 &evolution_of_branch
,
1338 if (res
== t_false
|| res
== t_dont_know
)
1341 *evolution_of_loop
= chrec_merge (*evolution_of_loop
,
1342 evolution_of_branch
);
1348 /* Follow an SSA edge in an inner loop. It computes the overall
1349 effect of the loop, and following the symbolic initial conditions,
1350 it follows the edges in the parent loop. The inner loop is
1351 considered as a single statement. */
1354 follow_ssa_edge_inner_loop_phi (struct loop
*outer_loop
,
1355 gimple loop_phi_node
,
1357 tree
*evolution_of_loop
, int limit
)
1359 struct loop
*loop
= loop_containing_stmt (loop_phi_node
);
1360 tree ev
= analyze_scalar_evolution (loop
, PHI_RESULT (loop_phi_node
));
1362 /* Sometimes, the inner loop is too difficult to analyze, and the
1363 result of the analysis is a symbolic parameter. */
1364 if (ev
== PHI_RESULT (loop_phi_node
))
1366 t_bool res
= t_false
;
1367 int i
, n
= gimple_phi_num_args (loop_phi_node
);
1369 for (i
= 0; i
< n
; i
++)
1371 tree arg
= PHI_ARG_DEF (loop_phi_node
, i
);
1374 /* Follow the edges that exit the inner loop. */
1375 bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1376 if (!flow_bb_inside_loop_p (loop
, bb
))
1377 res
= follow_ssa_edge_expr (outer_loop
, loop_phi_node
,
1379 evolution_of_loop
, limit
);
1384 /* If the path crosses this loop-phi, give up. */
1386 *evolution_of_loop
= chrec_dont_know
;
1391 /* Otherwise, compute the overall effect of the inner loop. */
1392 ev
= compute_overall_effect_of_inner_loop (loop
, ev
);
1393 return follow_ssa_edge_expr (outer_loop
, loop_phi_node
, ev
, halting_phi
,
1394 evolution_of_loop
, limit
);
1397 /* Follow an SSA edge from a loop-phi-node to itself, constructing a
1398 path that is analyzed on the return walk. */
1401 follow_ssa_edge (struct loop
*loop
, gimple def
, gimple halting_phi
,
1402 tree
*evolution_of_loop
, int limit
)
1404 struct loop
*def_loop
;
1406 if (gimple_nop_p (def
))
1409 /* Give up if the path is longer than the MAX that we allow. */
1410 if (limit
> PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE
))
1413 def_loop
= loop_containing_stmt (def
);
1415 switch (gimple_code (def
))
1418 if (!loop_phi_node_p (def
))
1419 /* DEF is a condition-phi-node. Follow the branches, and
1420 record their evolutions. Finally, merge the collected
1421 information and set the approximation to the main
1423 return follow_ssa_edge_in_condition_phi
1424 (loop
, def
, halting_phi
, evolution_of_loop
, limit
);
1426 /* When the analyzed phi is the halting_phi, the
1427 depth-first search is over: we have found a path from
1428 the halting_phi to itself in the loop. */
1429 if (def
== halting_phi
)
1432 /* Otherwise, the evolution of the HALTING_PHI depends
1433 on the evolution of another loop-phi-node, i.e. the
1434 evolution function is a higher degree polynomial. */
1435 if (def_loop
== loop
)
1439 if (flow_loop_nested_p (loop
, def_loop
))
1440 return follow_ssa_edge_inner_loop_phi
1441 (loop
, def
, halting_phi
, evolution_of_loop
, limit
+ 1);
1447 return follow_ssa_edge_in_rhs (loop
, def
, halting_phi
,
1448 evolution_of_loop
, limit
);
1451 /* At this level of abstraction, the program is just a set
1452 of GIMPLE_ASSIGNs and PHI_NODEs. In principle there is no
1453 other node to be handled. */
1460 /* Given a LOOP_PHI_NODE, this function determines the evolution
1461 function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
1464 analyze_evolution_in_loop (gimple loop_phi_node
,
1467 int i
, n
= gimple_phi_num_args (loop_phi_node
);
1468 tree evolution_function
= chrec_not_analyzed_yet
;
1469 struct loop
*loop
= loop_containing_stmt (loop_phi_node
);
1472 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1474 fprintf (dump_file
, "(analyze_evolution_in_loop \n");
1475 fprintf (dump_file
, " (loop_phi_node = ");
1476 print_gimple_stmt (dump_file
, loop_phi_node
, 0, 0);
1477 fprintf (dump_file
, ")\n");
1480 for (i
= 0; i
< n
; i
++)
1482 tree arg
= PHI_ARG_DEF (loop_phi_node
, i
);
1487 /* Select the edges that enter the loop body. */
1488 bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1489 if (!flow_bb_inside_loop_p (loop
, bb
))
1492 if (TREE_CODE (arg
) == SSA_NAME
)
1494 ssa_chain
= SSA_NAME_DEF_STMT (arg
);
1496 /* Pass in the initial condition to the follow edge function. */
1498 res
= follow_ssa_edge (loop
, ssa_chain
, loop_phi_node
, &ev_fn
, 0);
1503 /* When it is impossible to go back on the same
1504 loop_phi_node by following the ssa edges, the
1505 evolution is represented by a peeled chrec, i.e. the
1506 first iteration, EV_FN has the value INIT_COND, then
1507 all the other iterations it has the value of ARG.
1508 For the moment, PEELED_CHREC nodes are not built. */
1510 ev_fn
= chrec_dont_know
;
1512 /* When there are multiple back edges of the loop (which in fact never
1513 happens currently, but nevertheless), merge their evolutions. */
1514 evolution_function
= chrec_merge (evolution_function
, ev_fn
);
1517 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1519 fprintf (dump_file
, " (evolution_function = ");
1520 print_generic_expr (dump_file
, evolution_function
, 0);
1521 fprintf (dump_file
, "))\n");
1524 return evolution_function
;
1527 /* Given a loop-phi-node, return the initial conditions of the
1528 variable on entry of the loop. When the CCP has propagated
1529 constants into the loop-phi-node, the initial condition is
1530 instantiated, otherwise the initial condition is kept symbolic.
1531 This analyzer does not analyze the evolution outside the current
1532 loop, and leaves this task to the on-demand tree reconstructor. */
1535 analyze_initial_condition (gimple loop_phi_node
)
1538 tree init_cond
= chrec_not_analyzed_yet
;
1539 struct loop
*loop
= loop_containing_stmt (loop_phi_node
);
1541 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1543 fprintf (dump_file
, "(analyze_initial_condition \n");
1544 fprintf (dump_file
, " (loop_phi_node = \n");
1545 print_gimple_stmt (dump_file
, loop_phi_node
, 0, 0);
1546 fprintf (dump_file
, ")\n");
1549 n
= gimple_phi_num_args (loop_phi_node
);
1550 for (i
= 0; i
< n
; i
++)
1552 tree branch
= PHI_ARG_DEF (loop_phi_node
, i
);
1553 basic_block bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1555 /* When the branch is oriented to the loop's body, it does
1556 not contribute to the initial condition. */
1557 if (flow_bb_inside_loop_p (loop
, bb
))
1560 if (init_cond
== chrec_not_analyzed_yet
)
1566 if (TREE_CODE (branch
) == SSA_NAME
)
1568 init_cond
= chrec_dont_know
;
1572 init_cond
= chrec_merge (init_cond
, branch
);
1575 /* Ooops -- a loop without an entry??? */
1576 if (init_cond
== chrec_not_analyzed_yet
)
1577 init_cond
= chrec_dont_know
;
1579 /* During early loop unrolling we do not have fully constant propagated IL.
1580 Handle degenerate PHIs here to not miss important unrollings. */
1581 if (TREE_CODE (init_cond
) == SSA_NAME
)
1583 gimple def
= SSA_NAME_DEF_STMT (init_cond
);
1585 if (gimple_code (def
) == GIMPLE_PHI
1586 && (res
= degenerate_phi_result (def
)) != NULL_TREE
1587 /* Only allow invariants here, otherwise we may break
1588 loop-closed SSA form. */
1589 && is_gimple_min_invariant (res
))
1593 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1595 fprintf (dump_file
, " (init_cond = ");
1596 print_generic_expr (dump_file
, init_cond
, 0);
1597 fprintf (dump_file
, "))\n");
1603 /* Analyze the scalar evolution for LOOP_PHI_NODE. */
1606 interpret_loop_phi (struct loop
*loop
, gimple loop_phi_node
)
1609 struct loop
*phi_loop
= loop_containing_stmt (loop_phi_node
);
1612 if (phi_loop
!= loop
)
1614 struct loop
*subloop
;
1615 tree evolution_fn
= analyze_scalar_evolution
1616 (phi_loop
, PHI_RESULT (loop_phi_node
));
1618 /* Dive one level deeper. */
1619 subloop
= superloop_at_depth (phi_loop
, loop_depth (loop
) + 1);
1621 /* Interpret the subloop. */
1622 res
= compute_overall_effect_of_inner_loop (subloop
, evolution_fn
);
1626 /* Otherwise really interpret the loop phi. */
1627 init_cond
= analyze_initial_condition (loop_phi_node
);
1628 res
= analyze_evolution_in_loop (loop_phi_node
, init_cond
);
1633 /* This function merges the branches of a condition-phi-node,
1634 contained in the outermost loop, and whose arguments are already
1638 interpret_condition_phi (struct loop
*loop
, gimple condition_phi
)
1640 int i
, n
= gimple_phi_num_args (condition_phi
);
1641 tree res
= chrec_not_analyzed_yet
;
1643 for (i
= 0; i
< n
; i
++)
1647 if (backedge_phi_arg_p (condition_phi
, i
))
1649 res
= chrec_dont_know
;
1653 branch_chrec
= analyze_scalar_evolution
1654 (loop
, PHI_ARG_DEF (condition_phi
, i
));
1656 res
= chrec_merge (res
, branch_chrec
);
1662 /* Interpret the operation RHS1 OP RHS2. If we didn't
1663 analyze this node before, follow the definitions until ending
1664 either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node. On the
1665 return path, this function propagates evolutions (ala constant copy
1666 propagation). OPND1 is not a GIMPLE expression because we could
1667 analyze the effect of an inner loop: see interpret_loop_phi. */
1670 interpret_rhs_expr (struct loop
*loop
, gimple at_stmt
,
1671 tree type
, tree rhs1
, enum tree_code code
, tree rhs2
)
1673 tree res
, chrec1
, chrec2
;
1675 if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
1677 if (is_gimple_min_invariant (rhs1
))
1678 return chrec_convert (type
, rhs1
, at_stmt
);
1680 if (code
== SSA_NAME
)
1681 return chrec_convert (type
, analyze_scalar_evolution (loop
, rhs1
),
1684 if (code
== ASSERT_EXPR
)
1686 rhs1
= ASSERT_EXPR_VAR (rhs1
);
1687 return chrec_convert (type
, analyze_scalar_evolution (loop
, rhs1
),
1691 return chrec_dont_know
;
1696 case POINTER_PLUS_EXPR
:
1697 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1698 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1699 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1700 chrec2
= chrec_convert (sizetype
, chrec2
, at_stmt
);
1701 res
= chrec_fold_plus (type
, chrec1
, chrec2
);
1705 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1706 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1707 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1708 chrec2
= chrec_convert (type
, chrec2
, at_stmt
);
1709 res
= chrec_fold_plus (type
, chrec1
, chrec2
);
1713 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1714 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1715 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1716 chrec2
= chrec_convert (type
, chrec2
, at_stmt
);
1717 res
= chrec_fold_minus (type
, chrec1
, chrec2
);
1721 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1722 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1723 /* TYPE may be integer, real or complex, so use fold_convert. */
1724 res
= chrec_fold_multiply (type
, chrec1
,
1725 fold_convert (type
, integer_minus_one_node
));
1729 /* Handle ~X as -1 - X. */
1730 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1731 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1732 res
= chrec_fold_minus (type
,
1733 fold_convert (type
, integer_minus_one_node
),
1738 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1739 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1740 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1741 chrec2
= chrec_convert (type
, chrec2
, at_stmt
);
1742 res
= chrec_fold_multiply (type
, chrec1
, chrec2
);
1746 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1747 res
= chrec_convert (type
, chrec1
, at_stmt
);
1751 res
= chrec_dont_know
;
1758 /* Interpret the expression EXPR. */
1761 interpret_expr (struct loop
*loop
, gimple at_stmt
, tree expr
)
1763 enum tree_code code
;
1764 tree type
= TREE_TYPE (expr
), op0
, op1
;
1766 if (automatically_generated_chrec_p (expr
))
1769 if (TREE_CODE (expr
) == POLYNOMIAL_CHREC
)
1770 return chrec_dont_know
;
1772 extract_ops_from_tree (expr
, &code
, &op0
, &op1
);
1774 return interpret_rhs_expr (loop
, at_stmt
, type
,
1778 /* Interpret the rhs of the assignment STMT. */
1781 interpret_gimple_assign (struct loop
*loop
, gimple stmt
)
1783 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1784 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1786 return interpret_rhs_expr (loop
, stmt
, type
,
1787 gimple_assign_rhs1 (stmt
), code
,
1788 gimple_assign_rhs2 (stmt
));
1793 /* This section contains all the entry points:
1794 - number_of_iterations_in_loop,
1795 - analyze_scalar_evolution,
1796 - instantiate_parameters.
1799 /* Compute and return the evolution function in WRTO_LOOP, the nearest
1800 common ancestor of DEF_LOOP and USE_LOOP. */
1803 compute_scalar_evolution_in_loop (struct loop
*wrto_loop
,
1804 struct loop
*def_loop
,
1808 if (def_loop
== wrto_loop
)
1811 def_loop
= superloop_at_depth (def_loop
, loop_depth (wrto_loop
) + 1);
1812 res
= compute_overall_effect_of_inner_loop (def_loop
, ev
);
1814 return analyze_scalar_evolution_1 (wrto_loop
, res
, chrec_not_analyzed_yet
);
1817 /* Helper recursive function. */
1820 analyze_scalar_evolution_1 (struct loop
*loop
, tree var
, tree res
)
1822 tree type
= TREE_TYPE (var
);
1825 struct loop
*def_loop
;
1827 if (loop
== NULL
|| TREE_CODE (type
) == VECTOR_TYPE
)
1828 return chrec_dont_know
;
1830 if (TREE_CODE (var
) != SSA_NAME
)
1831 return interpret_expr (loop
, NULL
, var
);
1833 def
= SSA_NAME_DEF_STMT (var
);
1834 bb
= gimple_bb (def
);
1835 def_loop
= bb
? bb
->loop_father
: NULL
;
1838 || !flow_bb_inside_loop_p (loop
, bb
))
1840 /* Keep the symbolic form. */
1845 if (res
!= chrec_not_analyzed_yet
)
1847 if (loop
!= bb
->loop_father
)
1848 res
= compute_scalar_evolution_in_loop
1849 (find_common_loop (loop
, bb
->loop_father
), bb
->loop_father
, res
);
1854 if (loop
!= def_loop
)
1856 res
= analyze_scalar_evolution_1 (def_loop
, var
, chrec_not_analyzed_yet
);
1857 res
= compute_scalar_evolution_in_loop (loop
, def_loop
, res
);
1862 switch (gimple_code (def
))
1865 res
= interpret_gimple_assign (loop
, def
);
1869 if (loop_phi_node_p (def
))
1870 res
= interpret_loop_phi (loop
, def
);
1872 res
= interpret_condition_phi (loop
, def
);
1876 res
= chrec_dont_know
;
1882 /* Keep the symbolic form. */
1883 if (res
== chrec_dont_know
)
1886 if (loop
== def_loop
)
1887 set_scalar_evolution (block_before_loop (loop
), var
, res
);
1892 /* Entry point for the scalar evolution analyzer.
1893 Analyzes and returns the scalar evolution of the ssa_name VAR.
1894 LOOP_NB is the identifier number of the loop in which the variable
1897 Example of use: having a pointer VAR to a SSA_NAME node, STMT a
1898 pointer to the statement that uses this variable, in order to
1899 determine the evolution function of the variable, use the following
1902 unsigned loop_nb = loop_containing_stmt (stmt)->num;
1903 tree chrec_with_symbols = analyze_scalar_evolution (loop_nb, var);
1904 tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
1908 analyze_scalar_evolution (struct loop
*loop
, tree var
)
1912 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1914 fprintf (dump_file
, "(analyze_scalar_evolution \n");
1915 fprintf (dump_file
, " (loop_nb = %d)\n", loop
->num
);
1916 fprintf (dump_file
, " (scalar = ");
1917 print_generic_expr (dump_file
, var
, 0);
1918 fprintf (dump_file
, ")\n");
1921 res
= get_scalar_evolution (block_before_loop (loop
), var
);
1922 res
= analyze_scalar_evolution_1 (loop
, var
, res
);
1924 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1925 fprintf (dump_file
, ")\n");
1930 /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
1931 WRTO_LOOP (which should be a superloop of USE_LOOP)
1933 FOLDED_CASTS is set to true if resolve_mixers used
1934 chrec_convert_aggressive (TODO -- not really, we are way too conservative
1935 at the moment in order to keep things simple).
1937 To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
1940 for (i = 0; i < 100; i++) -- loop 1
1942 for (j = 0; j < 100; j++) -- loop 2
1949 for (t = 0; t < 100; t++) -- loop 3
1956 Both k1 and k2 are invariants in loop3, thus
1957 analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
1958 analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
1960 As they are invariant, it does not matter whether we consider their
1961 usage in loop 3 or loop 2, hence
1962 analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
1963 analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
1964 analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
1965 analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
1967 Similarly for their evolutions with respect to loop 1. The values of K2
1968 in the use in loop 2 vary independently on loop 1, thus we cannot express
1969 the evolution with respect to loop 1:
1970 analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
1971 analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
1972 analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
1973 analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
1975 The value of k2 in the use in loop 1 is known, though:
1976 analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
1977 analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
1981 analyze_scalar_evolution_in_loop (struct loop
*wrto_loop
, struct loop
*use_loop
,
1982 tree version
, bool *folded_casts
)
1985 tree ev
= version
, tmp
;
1987 /* We cannot just do
1989 tmp = analyze_scalar_evolution (use_loop, version);
1990 ev = resolve_mixers (wrto_loop, tmp);
1992 as resolve_mixers would query the scalar evolution with respect to
1993 wrto_loop. For example, in the situation described in the function
1994 comment, suppose that wrto_loop = loop1, use_loop = loop3 and
1997 analyze_scalar_evolution (use_loop, version) = k2
1999 and resolve_mixers (loop1, k2) finds that the value of k2 in loop 1
2000 is 100, which is a wrong result, since we are interested in the
2003 Instead, we need to proceed from use_loop to wrto_loop loop by loop,
2004 each time checking that there is no evolution in the inner loop. */
2007 *folded_casts
= false;
2010 tmp
= analyze_scalar_evolution (use_loop
, ev
);
2011 ev
= resolve_mixers (use_loop
, tmp
);
2013 if (folded_casts
&& tmp
!= ev
)
2014 *folded_casts
= true;
2016 if (use_loop
== wrto_loop
)
2019 /* If the value of the use changes in the inner loop, we cannot express
2020 its value in the outer loop (we might try to return interval chrec,
2021 but we do not have a user for it anyway) */
2022 if (!no_evolution_in_loop_p (ev
, use_loop
->num
, &val
)
2024 return chrec_dont_know
;
2026 use_loop
= loop_outer (use_loop
);
2030 /* Returns from CACHE the value for VERSION instantiated below
2031 INSTANTIATED_BELOW block. */
2034 get_instantiated_value (htab_t cache
, basic_block instantiated_below
,
2037 struct scev_info_str
*info
, pattern
;
2039 pattern
.var
= version
;
2040 pattern
.instantiated_below
= instantiated_below
;
2041 info
= (struct scev_info_str
*) htab_find (cache
, &pattern
);
2049 /* Sets in CACHE the value of VERSION instantiated below basic block
2050 INSTANTIATED_BELOW to VAL. */
2053 set_instantiated_value (htab_t cache
, basic_block instantiated_below
,
2054 tree version
, tree val
)
2056 struct scev_info_str
*info
, pattern
;
2059 pattern
.var
= version
;
2060 pattern
.instantiated_below
= instantiated_below
;
2061 slot
= htab_find_slot (cache
, &pattern
, INSERT
);
2064 *slot
= new_scev_info_str (instantiated_below
, version
);
2065 info
= (struct scev_info_str
*) *slot
;
2069 /* Return the closed_loop_phi node for VAR. If there is none, return
2073 loop_closed_phi_def (tree var
)
2078 gimple_stmt_iterator psi
;
2080 if (var
== NULL_TREE
2081 || TREE_CODE (var
) != SSA_NAME
)
2084 loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (var
));
2085 exit
= single_exit (loop
);
2089 for (psi
= gsi_start_phis (exit
->dest
); !gsi_end_p (psi
); gsi_next (&psi
))
2091 phi
= gsi_stmt (psi
);
2092 if (PHI_ARG_DEF_FROM_EDGE (phi
, exit
) == var
)
2093 return PHI_RESULT (phi
);
2099 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2100 and EVOLUTION_LOOP, that were left under a symbolic form.
2102 CHREC is the scalar evolution to instantiate.
2104 CACHE is the cache of already instantiated values.
2106 FOLD_CONVERSIONS should be set to true when the conversions that
2107 may wrap in signed/pointer type are folded, as long as the value of
2108 the chrec is preserved.
2110 SIZE_EXPR is used for computing the size of the expression to be
2111 instantiated, and to stop if it exceeds some limit. */
2114 instantiate_scev_1 (basic_block instantiate_below
,
2115 struct loop
*evolution_loop
, tree chrec
,
2116 bool fold_conversions
, htab_t cache
, int size_expr
)
2118 tree res
, op0
, op1
, op2
;
2120 struct loop
*def_loop
;
2121 tree type
= chrec_type (chrec
);
2123 /* Give up if the expression is larger than the MAX that we allow. */
2124 if (size_expr
++ > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE
))
2125 return chrec_dont_know
;
2127 if (automatically_generated_chrec_p (chrec
)
2128 || is_gimple_min_invariant (chrec
))
2131 switch (TREE_CODE (chrec
))
2134 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (chrec
));
2136 /* A parameter (or loop invariant and we do not want to include
2137 evolutions in outer loops), nothing to do. */
2139 || loop_depth (def_bb
->loop_father
) == 0
2140 || dominated_by_p (CDI_DOMINATORS
, instantiate_below
, def_bb
))
2143 /* We cache the value of instantiated variable to avoid exponential
2144 time complexity due to reevaluations. We also store the convenient
2145 value in the cache in order to prevent infinite recursion -- we do
2146 not want to instantiate the SSA_NAME if it is in a mixer
2147 structure. This is used for avoiding the instantiation of
2148 recursively defined functions, such as:
2150 | a_2 -> {0, +, 1, +, a_2}_1 */
2152 res
= get_instantiated_value (cache
, instantiate_below
, chrec
);
2156 res
= chrec_dont_know
;
2157 set_instantiated_value (cache
, instantiate_below
, chrec
, res
);
2159 def_loop
= find_common_loop (evolution_loop
, def_bb
->loop_father
);
2161 /* If the analysis yields a parametric chrec, instantiate the
2163 res
= analyze_scalar_evolution (def_loop
, chrec
);
2165 /* Don't instantiate loop-closed-ssa phi nodes. */
2166 if (TREE_CODE (res
) == SSA_NAME
2167 && (loop_containing_stmt (SSA_NAME_DEF_STMT (res
)) == NULL
2168 || (loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res
)))
2169 > loop_depth (def_loop
))))
2172 res
= loop_closed_phi_def (chrec
);
2176 if (res
== NULL_TREE
)
2177 res
= chrec_dont_know
;
2180 else if (res
!= chrec_dont_know
)
2181 res
= instantiate_scev_1 (instantiate_below
, evolution_loop
, res
,
2182 fold_conversions
, cache
, size_expr
);
2184 /* Store the correct value to the cache. */
2185 set_instantiated_value (cache
, instantiate_below
, chrec
, res
);
2188 case POLYNOMIAL_CHREC
:
2189 op0
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2190 CHREC_LEFT (chrec
), fold_conversions
, cache
,
2192 if (op0
== chrec_dont_know
)
2193 return chrec_dont_know
;
2195 op1
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2196 CHREC_RIGHT (chrec
), fold_conversions
, cache
,
2198 if (op1
== chrec_dont_know
)
2199 return chrec_dont_know
;
2201 if (CHREC_LEFT (chrec
) != op0
2202 || CHREC_RIGHT (chrec
) != op1
)
2204 op1
= chrec_convert_rhs (chrec_type (op0
), op1
, NULL
);
2205 chrec
= build_polynomial_chrec (CHREC_VARIABLE (chrec
), op0
, op1
);
2209 case POINTER_PLUS_EXPR
:
2211 op0
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2212 TREE_OPERAND (chrec
, 0), fold_conversions
, cache
,
2214 if (op0
== chrec_dont_know
)
2215 return chrec_dont_know
;
2217 op1
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2218 TREE_OPERAND (chrec
, 1), fold_conversions
, cache
,
2220 if (op1
== chrec_dont_know
)
2221 return chrec_dont_know
;
2223 if (TREE_OPERAND (chrec
, 0) != op0
2224 || TREE_OPERAND (chrec
, 1) != op1
)
2226 op0
= chrec_convert (type
, op0
, NULL
);
2227 op1
= chrec_convert_rhs (type
, op1
, NULL
);
2228 chrec
= chrec_fold_plus (type
, op0
, op1
);
2233 op0
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2234 TREE_OPERAND (chrec
, 0), fold_conversions
, cache
,
2236 if (op0
== chrec_dont_know
)
2237 return chrec_dont_know
;
2239 op1
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2240 TREE_OPERAND (chrec
, 1),
2241 fold_conversions
, cache
, size_expr
);
2242 if (op1
== chrec_dont_know
)
2243 return chrec_dont_know
;
2245 if (TREE_OPERAND (chrec
, 0) != op0
2246 || TREE_OPERAND (chrec
, 1) != op1
)
2248 op0
= chrec_convert (type
, op0
, NULL
);
2249 op1
= chrec_convert (type
, op1
, NULL
);
2250 chrec
= chrec_fold_minus (type
, op0
, op1
);
2255 op0
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2256 TREE_OPERAND (chrec
, 0),
2257 fold_conversions
, cache
, size_expr
);
2258 if (op0
== chrec_dont_know
)
2259 return chrec_dont_know
;
2261 op1
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2262 TREE_OPERAND (chrec
, 1),
2263 fold_conversions
, cache
, size_expr
);
2264 if (op1
== chrec_dont_know
)
2265 return chrec_dont_know
;
2267 if (TREE_OPERAND (chrec
, 0) != op0
2268 || TREE_OPERAND (chrec
, 1) != op1
)
2270 op0
= chrec_convert (type
, op0
, NULL
);
2271 op1
= chrec_convert (type
, op1
, NULL
);
2272 chrec
= chrec_fold_multiply (type
, op0
, op1
);
2277 op0
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2278 TREE_OPERAND (chrec
, 0),
2279 fold_conversions
, cache
, size_expr
);
2280 if (op0
== chrec_dont_know
)
2281 return chrec_dont_know
;
2283 if (fold_conversions
)
2285 tree tmp
= chrec_convert_aggressive (TREE_TYPE (chrec
), op0
);
2290 if (op0
== TREE_OPERAND (chrec
, 0))
2293 /* If we used chrec_convert_aggressive, we can no longer assume that
2294 signed chrecs do not overflow, as chrec_convert does, so avoid
2295 calling it in that case. */
2296 if (fold_conversions
)
2297 return fold_convert (TREE_TYPE (chrec
), op0
);
2299 return chrec_convert (TREE_TYPE (chrec
), op0
, NULL
);
2302 /* Handle ~X as -1 - X. */
2303 op0
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2304 TREE_OPERAND (chrec
, 0),
2305 fold_conversions
, cache
, size_expr
);
2306 if (op0
== chrec_dont_know
)
2307 return chrec_dont_know
;
2309 if (TREE_OPERAND (chrec
, 0) != op0
)
2311 op0
= chrec_convert (type
, op0
, NULL
);
2312 chrec
= chrec_fold_minus (type
,
2314 integer_minus_one_node
),
2319 case SCEV_NOT_KNOWN
:
2320 return chrec_dont_know
;
2329 if (VL_EXP_CLASS_P (chrec
))
2330 return chrec_dont_know
;
2332 switch (TREE_CODE_LENGTH (TREE_CODE (chrec
)))
2335 op0
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2336 TREE_OPERAND (chrec
, 0),
2337 fold_conversions
, cache
, size_expr
);
2338 if (op0
== chrec_dont_know
)
2339 return chrec_dont_know
;
2341 op1
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2342 TREE_OPERAND (chrec
, 1),
2343 fold_conversions
, cache
, size_expr
);
2344 if (op1
== chrec_dont_know
)
2345 return chrec_dont_know
;
2347 op2
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2348 TREE_OPERAND (chrec
, 2),
2349 fold_conversions
, cache
, size_expr
);
2350 if (op2
== chrec_dont_know
)
2351 return chrec_dont_know
;
2353 if (op0
== TREE_OPERAND (chrec
, 0)
2354 && op1
== TREE_OPERAND (chrec
, 1)
2355 && op2
== TREE_OPERAND (chrec
, 2))
2358 return fold_build3 (TREE_CODE (chrec
),
2359 TREE_TYPE (chrec
), op0
, op1
, op2
);
2362 op0
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2363 TREE_OPERAND (chrec
, 0),
2364 fold_conversions
, cache
, size_expr
);
2365 if (op0
== chrec_dont_know
)
2366 return chrec_dont_know
;
2368 op1
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2369 TREE_OPERAND (chrec
, 1),
2370 fold_conversions
, cache
, size_expr
);
2371 if (op1
== chrec_dont_know
)
2372 return chrec_dont_know
;
2374 if (op0
== TREE_OPERAND (chrec
, 0)
2375 && op1
== TREE_OPERAND (chrec
, 1))
2377 return fold_build2 (TREE_CODE (chrec
), TREE_TYPE (chrec
), op0
, op1
);
2380 op0
= instantiate_scev_1 (instantiate_below
, evolution_loop
,
2381 TREE_OPERAND (chrec
, 0),
2382 fold_conversions
, cache
, size_expr
);
2383 if (op0
== chrec_dont_know
)
2384 return chrec_dont_know
;
2385 if (op0
== TREE_OPERAND (chrec
, 0))
2387 return fold_build1 (TREE_CODE (chrec
), TREE_TYPE (chrec
), op0
);
2396 /* Too complicated to handle. */
2397 return chrec_dont_know
;
2400 /* Analyze all the parameters of the chrec that were left under a
2401 symbolic form. INSTANTIATE_BELOW is the basic block that stops the
2402 recursive instantiation of parameters: a parameter is a variable
2403 that is defined in a basic block that dominates INSTANTIATE_BELOW or
2404 a function parameter. */
2407 instantiate_scev (basic_block instantiate_below
, struct loop
*evolution_loop
,
2411 htab_t cache
= htab_create (10, hash_scev_info
, eq_scev_info
, del_scev_info
);
2413 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2415 fprintf (dump_file
, "(instantiate_scev \n");
2416 fprintf (dump_file
, " (instantiate_below = %d)\n", instantiate_below
->index
);
2417 fprintf (dump_file
, " (evolution_loop = %d)\n", evolution_loop
->num
);
2418 fprintf (dump_file
, " (chrec = ");
2419 print_generic_expr (dump_file
, chrec
, 0);
2420 fprintf (dump_file
, ")\n");
2423 res
= instantiate_scev_1 (instantiate_below
, evolution_loop
, chrec
, false,
2426 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2428 fprintf (dump_file
, " (res = ");
2429 print_generic_expr (dump_file
, res
, 0);
2430 fprintf (dump_file
, "))\n");
2433 htab_delete (cache
);
2438 /* Similar to instantiate_parameters, but does not introduce the
2439 evolutions in outer loops for LOOP invariants in CHREC, and does not
2440 care about causing overflows, as long as they do not affect value
2441 of an expression. */
2444 resolve_mixers (struct loop
*loop
, tree chrec
)
2446 htab_t cache
= htab_create (10, hash_scev_info
, eq_scev_info
, del_scev_info
);
2447 tree ret
= instantiate_scev_1 (block_before_loop (loop
), loop
, chrec
, true,
2449 htab_delete (cache
);
2453 /* Entry point for the analysis of the number of iterations pass.
2454 This function tries to safely approximate the number of iterations
2455 the loop will run. When this property is not decidable at compile
2456 time, the result is chrec_dont_know. Otherwise the result is
2457 a scalar or a symbolic parameter.
2459 Example of analysis: suppose that the loop has an exit condition:
2461 "if (b > 49) goto end_loop;"
2463 and that in a previous analysis we have determined that the
2464 variable 'b' has an evolution function:
2466 "EF = {23, +, 5}_2".
2468 When we evaluate the function at the point 5, i.e. the value of the
2469 variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2470 and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2471 the loop body has been executed 6 times. */
2474 number_of_latch_executions (struct loop
*loop
)
2478 struct tree_niter_desc niter_desc
;
2480 /* Determine whether the number_of_iterations_in_loop has already
2482 res
= loop
->nb_iterations
;
2485 res
= chrec_dont_know
;
2487 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2488 fprintf (dump_file
, "(number_of_iterations_in_loop\n");
2490 exit
= single_exit (loop
);
2494 if (!number_of_iterations_exit (loop
, exit
, &niter_desc
, false))
2497 type
= TREE_TYPE (niter_desc
.niter
);
2498 if (integer_nonzerop (niter_desc
.may_be_zero
))
2499 res
= build_int_cst (type
, 0);
2500 else if (integer_zerop (niter_desc
.may_be_zero
))
2501 res
= niter_desc
.niter
;
2503 res
= chrec_dont_know
;
2506 return set_nb_iterations_in_loop (loop
, res
);
2509 /* Returns the number of executions of the exit condition of LOOP,
2510 i.e., the number by one higher than number_of_latch_executions.
2511 Note that unlike number_of_latch_executions, this number does
2512 not necessarily fit in the unsigned variant of the type of
2513 the control variable -- if the number of iterations is a constant,
2514 we return chrec_dont_know if adding one to number_of_latch_executions
2515 overflows; however, in case the number of iterations is symbolic
2516 expression, the caller is responsible for dealing with this
2517 the possible overflow. */
2520 number_of_exit_cond_executions (struct loop
*loop
)
2522 tree ret
= number_of_latch_executions (loop
);
2523 tree type
= chrec_type (ret
);
2525 if (chrec_contains_undetermined (ret
))
2528 ret
= chrec_fold_plus (type
, ret
, build_int_cst (type
, 1));
2529 if (TREE_CODE (ret
) == INTEGER_CST
2530 && TREE_OVERFLOW (ret
))
2531 return chrec_dont_know
;
2536 /* One of the drivers for testing the scalar evolutions analysis.
2537 This function computes the number of iterations for all the loops
2538 from the EXIT_CONDITIONS array. */
2541 number_of_iterations_for_all_loops (VEC(gimple
,heap
) **exit_conditions
)
2544 unsigned nb_chrec_dont_know_loops
= 0;
2545 unsigned nb_static_loops
= 0;
2548 for (i
= 0; VEC_iterate (gimple
, *exit_conditions
, i
, cond
); i
++)
2550 tree res
= number_of_latch_executions (loop_containing_stmt (cond
));
2551 if (chrec_contains_undetermined (res
))
2552 nb_chrec_dont_know_loops
++;
2559 fprintf (dump_file
, "\n(\n");
2560 fprintf (dump_file
, "-----------------------------------------\n");
2561 fprintf (dump_file
, "%d\tnb_chrec_dont_know_loops\n", nb_chrec_dont_know_loops
);
2562 fprintf (dump_file
, "%d\tnb_static_loops\n", nb_static_loops
);
2563 fprintf (dump_file
, "%d\tnb_total_loops\n", number_of_loops ());
2564 fprintf (dump_file
, "-----------------------------------------\n");
2565 fprintf (dump_file
, ")\n\n");
2567 print_loops (dump_file
, 3);
2573 /* Counters for the stats. */
2579 unsigned nb_affine_multivar
;
2580 unsigned nb_higher_poly
;
2581 unsigned nb_chrec_dont_know
;
2582 unsigned nb_undetermined
;
2585 /* Reset the counters. */
2588 reset_chrecs_counters (struct chrec_stats
*stats
)
2590 stats
->nb_chrecs
= 0;
2591 stats
->nb_affine
= 0;
2592 stats
->nb_affine_multivar
= 0;
2593 stats
->nb_higher_poly
= 0;
2594 stats
->nb_chrec_dont_know
= 0;
2595 stats
->nb_undetermined
= 0;
2598 /* Dump the contents of a CHREC_STATS structure. */
2601 dump_chrecs_stats (FILE *file
, struct chrec_stats
*stats
)
2603 fprintf (file
, "\n(\n");
2604 fprintf (file
, "-----------------------------------------\n");
2605 fprintf (file
, "%d\taffine univariate chrecs\n", stats
->nb_affine
);
2606 fprintf (file
, "%d\taffine multivariate chrecs\n", stats
->nb_affine_multivar
);
2607 fprintf (file
, "%d\tdegree greater than 2 polynomials\n",
2608 stats
->nb_higher_poly
);
2609 fprintf (file
, "%d\tchrec_dont_know chrecs\n", stats
->nb_chrec_dont_know
);
2610 fprintf (file
, "-----------------------------------------\n");
2611 fprintf (file
, "%d\ttotal chrecs\n", stats
->nb_chrecs
);
2612 fprintf (file
, "%d\twith undetermined coefficients\n",
2613 stats
->nb_undetermined
);
2614 fprintf (file
, "-----------------------------------------\n");
2615 fprintf (file
, "%d\tchrecs in the scev database\n",
2616 (int) htab_elements (scalar_evolution_info
));
2617 fprintf (file
, "%d\tsets in the scev database\n", nb_set_scev
);
2618 fprintf (file
, "%d\tgets in the scev database\n", nb_get_scev
);
2619 fprintf (file
, "-----------------------------------------\n");
2620 fprintf (file
, ")\n\n");
2623 /* Gather statistics about CHREC. */
2626 gather_chrec_stats (tree chrec
, struct chrec_stats
*stats
)
2628 if (dump_file
&& (dump_flags
& TDF_STATS
))
2630 fprintf (dump_file
, "(classify_chrec ");
2631 print_generic_expr (dump_file
, chrec
, 0);
2632 fprintf (dump_file
, "\n");
2637 if (chrec
== NULL_TREE
)
2639 stats
->nb_undetermined
++;
2643 switch (TREE_CODE (chrec
))
2645 case POLYNOMIAL_CHREC
:
2646 if (evolution_function_is_affine_p (chrec
))
2648 if (dump_file
&& (dump_flags
& TDF_STATS
))
2649 fprintf (dump_file
, " affine_univariate\n");
2652 else if (evolution_function_is_affine_multivariate_p (chrec
, 0))
2654 if (dump_file
&& (dump_flags
& TDF_STATS
))
2655 fprintf (dump_file
, " affine_multivariate\n");
2656 stats
->nb_affine_multivar
++;
2660 if (dump_file
&& (dump_flags
& TDF_STATS
))
2661 fprintf (dump_file
, " higher_degree_polynomial\n");
2662 stats
->nb_higher_poly
++;
2671 if (chrec_contains_undetermined (chrec
))
2673 if (dump_file
&& (dump_flags
& TDF_STATS
))
2674 fprintf (dump_file
, " undetermined\n");
2675 stats
->nb_undetermined
++;
2678 if (dump_file
&& (dump_flags
& TDF_STATS
))
2679 fprintf (dump_file
, ")\n");
2682 /* One of the drivers for testing the scalar evolutions analysis.
2683 This function analyzes the scalar evolution of all the scalars
2684 defined as loop phi nodes in one of the loops from the
2685 EXIT_CONDITIONS array.
2687 TODO Optimization: A loop is in canonical form if it contains only
2688 a single scalar loop phi node. All the other scalars that have an
2689 evolution in the loop are rewritten in function of this single
2690 index. This allows the parallelization of the loop. */
2693 analyze_scalar_evolution_for_all_loop_phi_nodes (VEC(gimple
,heap
) **exit_conditions
)
2696 struct chrec_stats stats
;
2698 gimple_stmt_iterator psi
;
2700 reset_chrecs_counters (&stats
);
2702 for (i
= 0; VEC_iterate (gimple
, *exit_conditions
, i
, cond
); i
++)
2708 loop
= loop_containing_stmt (cond
);
2711 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
2713 phi
= gsi_stmt (psi
);
2714 if (is_gimple_reg (PHI_RESULT (phi
)))
2716 chrec
= instantiate_parameters
2718 analyze_scalar_evolution (loop
, PHI_RESULT (phi
)));
2720 if (dump_file
&& (dump_flags
& TDF_STATS
))
2721 gather_chrec_stats (chrec
, &stats
);
2726 if (dump_file
&& (dump_flags
& TDF_STATS
))
2727 dump_chrecs_stats (dump_file
, &stats
);
2730 /* Callback for htab_traverse, gathers information on chrecs in the
2734 gather_stats_on_scev_database_1 (void **slot
, void *stats
)
2736 struct scev_info_str
*entry
= (struct scev_info_str
*) *slot
;
2738 gather_chrec_stats (entry
->chrec
, (struct chrec_stats
*) stats
);
2743 /* Classify the chrecs of the whole database. */
2746 gather_stats_on_scev_database (void)
2748 struct chrec_stats stats
;
2753 reset_chrecs_counters (&stats
);
2755 htab_traverse (scalar_evolution_info
, gather_stats_on_scev_database_1
,
2758 dump_chrecs_stats (dump_file
, &stats
);
2766 initialize_scalar_evolutions_analyzer (void)
2768 /* The elements below are unique. */
2769 if (chrec_dont_know
== NULL_TREE
)
2771 chrec_not_analyzed_yet
= NULL_TREE
;
2772 chrec_dont_know
= make_node (SCEV_NOT_KNOWN
);
2773 chrec_known
= make_node (SCEV_KNOWN
);
2774 TREE_TYPE (chrec_dont_know
) = void_type_node
;
2775 TREE_TYPE (chrec_known
) = void_type_node
;
2779 /* Initialize the analysis of scalar evolutions for LOOPS. */
2782 scev_initialize (void)
2787 scalar_evolution_info
= htab_create_alloc (100,
2794 initialize_scalar_evolutions_analyzer ();
2796 FOR_EACH_LOOP (li
, loop
, 0)
2798 loop
->nb_iterations
= NULL_TREE
;
2802 /* Cleans up the information cached by the scalar evolutions analysis. */
2810 if (!scalar_evolution_info
|| !current_loops
)
2813 htab_empty (scalar_evolution_info
);
2814 FOR_EACH_LOOP (li
, loop
, 0)
2816 loop
->nb_iterations
= NULL_TREE
;
2820 /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
2821 respect to WRTO_LOOP and returns its base and step in IV if possible
2822 (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
2823 and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be
2824 invariant in LOOP. Otherwise we require it to be an integer constant.
2826 IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
2827 because it is computed in signed arithmetics). Consequently, adding an
2830 for (i = IV->base; ; i += IV->step)
2832 is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
2833 false for the type of the induction variable, or you can prove that i does
2834 not wrap by some other argument. Otherwise, this might introduce undefined
2837 for (i = iv->base; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
2839 must be used instead. */
2842 simple_iv (struct loop
*wrto_loop
, struct loop
*use_loop
, tree op
,
2843 affine_iv
*iv
, bool allow_nonconstant_step
)
2848 iv
->base
= NULL_TREE
;
2849 iv
->step
= NULL_TREE
;
2850 iv
->no_overflow
= false;
2852 type
= TREE_TYPE (op
);
2853 if (TREE_CODE (type
) != INTEGER_TYPE
2854 && TREE_CODE (type
) != POINTER_TYPE
)
2857 ev
= analyze_scalar_evolution_in_loop (wrto_loop
, use_loop
, op
,
2859 if (chrec_contains_undetermined (ev
)
2860 || chrec_contains_symbols_defined_in_loop (ev
, wrto_loop
->num
))
2863 if (tree_does_not_contain_chrecs (ev
))
2866 iv
->step
= build_int_cst (TREE_TYPE (ev
), 0);
2867 iv
->no_overflow
= true;
2871 if (TREE_CODE (ev
) != POLYNOMIAL_CHREC
2872 || CHREC_VARIABLE (ev
) != (unsigned) wrto_loop
->num
)
2875 iv
->step
= CHREC_RIGHT (ev
);
2876 if ((!allow_nonconstant_step
&& TREE_CODE (iv
->step
) != INTEGER_CST
)
2877 || tree_contains_chrecs (iv
->step
, NULL
))
2880 iv
->base
= CHREC_LEFT (ev
);
2881 if (tree_contains_chrecs (iv
->base
, NULL
))
2884 iv
->no_overflow
= !folded_casts
&& TYPE_OVERFLOW_UNDEFINED (type
);
2889 /* Runs the analysis of scalar evolutions. */
2892 scev_analysis (void)
2894 VEC(gimple
,heap
) *exit_conditions
;
2896 exit_conditions
= VEC_alloc (gimple
, heap
, 37);
2897 select_loops_exit_conditions (&exit_conditions
);
2899 if (dump_file
&& (dump_flags
& TDF_STATS
))
2900 analyze_scalar_evolution_for_all_loop_phi_nodes (&exit_conditions
);
2902 number_of_iterations_for_all_loops (&exit_conditions
);
2903 VEC_free (gimple
, heap
, exit_conditions
);
2906 /* Finalize the scalar evolution analysis. */
2909 scev_finalize (void)
2911 if (!scalar_evolution_info
)
2913 htab_delete (scalar_evolution_info
);
2914 scalar_evolution_info
= NULL
;
2917 /* Returns true if the expression EXPR is considered to be too expensive
2918 for scev_const_prop. */
2921 expression_expensive_p (tree expr
)
2923 enum tree_code code
;
2925 if (is_gimple_val (expr
))
2928 code
= TREE_CODE (expr
);
2929 if (code
== TRUNC_DIV_EXPR
2930 || code
== CEIL_DIV_EXPR
2931 || code
== FLOOR_DIV_EXPR
2932 || code
== ROUND_DIV_EXPR
2933 || code
== TRUNC_MOD_EXPR
2934 || code
== CEIL_MOD_EXPR
2935 || code
== FLOOR_MOD_EXPR
2936 || code
== ROUND_MOD_EXPR
2937 || code
== EXACT_DIV_EXPR
)
2939 /* Division by power of two is usually cheap, so we allow it.
2940 Forbid anything else. */
2941 if (!integer_pow2p (TREE_OPERAND (expr
, 1)))
2945 switch (TREE_CODE_CLASS (code
))
2948 case tcc_comparison
:
2949 if (expression_expensive_p (TREE_OPERAND (expr
, 1)))
2954 return expression_expensive_p (TREE_OPERAND (expr
, 0));
2961 /* Replace ssa names for that scev can prove they are constant by the
2962 appropriate constants. Also perform final value replacement in loops,
2963 in case the replacement expressions are cheap.
2965 We only consider SSA names defined by phi nodes; rest is left to the
2966 ordinary constant propagation pass. */
2969 scev_const_prop (void)
2972 tree name
, type
, ev
;
2974 struct loop
*loop
, *ex_loop
;
2975 bitmap ssa_names_to_remove
= NULL
;
2978 gimple_stmt_iterator psi
;
2980 if (number_of_loops () <= 1)
2985 loop
= bb
->loop_father
;
2987 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
2989 phi
= gsi_stmt (psi
);
2990 name
= PHI_RESULT (phi
);
2992 if (!is_gimple_reg (name
))
2995 type
= TREE_TYPE (name
);
2997 if (!POINTER_TYPE_P (type
)
2998 && !INTEGRAL_TYPE_P (type
))
3001 ev
= resolve_mixers (loop
, analyze_scalar_evolution (loop
, name
));
3002 if (!is_gimple_min_invariant (ev
)
3003 || !may_propagate_copy (name
, ev
))
3006 /* Replace the uses of the name. */
3008 replace_uses_by (name
, ev
);
3010 if (!ssa_names_to_remove
)
3011 ssa_names_to_remove
= BITMAP_ALLOC (NULL
);
3012 bitmap_set_bit (ssa_names_to_remove
, SSA_NAME_VERSION (name
));
3016 /* Remove the ssa names that were replaced by constants. We do not
3017 remove them directly in the previous cycle, since this
3018 invalidates scev cache. */
3019 if (ssa_names_to_remove
)
3023 EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove
, 0, i
, bi
)
3025 gimple_stmt_iterator psi
;
3026 name
= ssa_name (i
);
3027 phi
= SSA_NAME_DEF_STMT (name
);
3029 gcc_assert (gimple_code (phi
) == GIMPLE_PHI
);
3030 psi
= gsi_for_stmt (phi
);
3031 remove_phi_node (&psi
, true);
3034 BITMAP_FREE (ssa_names_to_remove
);
3038 /* Now the regular final value replacement. */
3039 FOR_EACH_LOOP (li
, loop
, LI_FROM_INNERMOST
)
3042 tree def
, rslt
, niter
;
3043 gimple_stmt_iterator bsi
;
3045 /* If we do not know exact number of iterations of the loop, we cannot
3046 replace the final value. */
3047 exit
= single_exit (loop
);
3051 niter
= number_of_latch_executions (loop
);
3052 if (niter
== chrec_dont_know
)
3055 /* Ensure that it is possible to insert new statements somewhere. */
3056 if (!single_pred_p (exit
->dest
))
3057 split_loop_exit_edge (exit
);
3058 bsi
= gsi_after_labels (exit
->dest
);
3060 ex_loop
= superloop_at_depth (loop
,
3061 loop_depth (exit
->dest
->loop_father
) + 1);
3063 for (psi
= gsi_start_phis (exit
->dest
); !gsi_end_p (psi
); )
3065 phi
= gsi_stmt (psi
);
3066 rslt
= PHI_RESULT (phi
);
3067 def
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
3068 if (!is_gimple_reg (def
))
3074 if (!POINTER_TYPE_P (TREE_TYPE (def
))
3075 && !INTEGRAL_TYPE_P (TREE_TYPE (def
)))
3081 def
= analyze_scalar_evolution_in_loop (ex_loop
, loop
, def
, NULL
);
3082 def
= compute_overall_effect_of_inner_loop (ex_loop
, def
);
3083 if (!tree_does_not_contain_chrecs (def
)
3084 || chrec_contains_symbols_defined_in_loop (def
, ex_loop
->num
)
3085 /* Moving the computation from the loop may prolong life range
3086 of some ssa names, which may cause problems if they appear
3087 on abnormal edges. */
3088 || contains_abnormal_ssa_name_p (def
)
3089 /* Do not emit expensive expressions. The rationale is that
3090 when someone writes a code like
3092 while (n > 45) n -= 45;
3094 he probably knows that n is not large, and does not want it
3095 to be turned into n %= 45. */
3096 || expression_expensive_p (def
))
3102 /* Eliminate the PHI node and replace it by a computation outside
3104 def
= unshare_expr (def
);
3105 remove_phi_node (&psi
, false);
3107 def
= force_gimple_operand_gsi (&bsi
, def
, false, NULL_TREE
,
3108 true, GSI_SAME_STMT
);
3109 ass
= gimple_build_assign (rslt
, def
);
3110 gsi_insert_before (&bsi
, ass
, GSI_SAME_STMT
);
3116 #include "gt-tree-scalar-evolution.h"