1 /* Scalar evolution detector.
2 Copyright (C) 2003-2013 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
24 This pass analyzes the evolution of scalar variables in loop
25 structures. The algorithm is based on the SSA representation,
26 and on the loop hierarchy tree. This algorithm is not based on
27 the notion of versions of a variable, as it was the case for the
28 previous implementations of the scalar evolution algorithm, but
29 it assumes that each defined name is unique.
31 The notation used in this file is called "chains of recurrences",
32 and has been proposed by Eugene Zima, Robert Van Engelen, and
33 others for describing induction variables in programs. For example
34 "b -> {0, +, 2}_1" means that the scalar variable "b" is equal to 0
35 when entering in the loop_1 and has a step 2 in this loop, in other
36 words "for (b = 0; b < N; b+=2);". Note that the coefficients of
37 this chain of recurrence (or chrec [shrek]) can contain the name of
38 other variables, in which case they are called parametric chrecs.
39 For example, "b -> {a, +, 2}_1" means that the initial value of "b"
40 is the value of "a". In most of the cases these parametric chrecs
41 are fully instantiated before their use because symbolic names can
42 hide some difficult cases such as self-references described later
43 (see the Fibonacci example).
45 A short sketch of the algorithm is:
47 Given a scalar variable to be analyzed, follow the SSA edge to
50 - When the definition is a GIMPLE_ASSIGN: if the right hand side
51 (RHS) of the definition cannot be statically analyzed, the answer
52 of the analyzer is: "don't know".
53 Otherwise, for all the variables that are not yet analyzed in the
54 RHS, try to determine their evolution, and finally try to
55 evaluate the operation of the RHS that gives the evolution
56 function of the analyzed variable.
58 - When the definition is a condition-phi-node: determine the
59 evolution function for all the branches of the phi node, and
60 finally merge these evolutions (see chrec_merge).
62 - When the definition is a loop-phi-node: determine its initial
63 condition, that is the SSA edge defined in an outer loop, and
64 keep it symbolic. Then determine the SSA edges that are defined
65 in the body of the loop. Follow the inner edges until ending on
66 another loop-phi-node of the same analyzed loop. If the reached
67 loop-phi-node is not the starting loop-phi-node, then we keep
68 this definition under a symbolic form. If the reached
69 loop-phi-node is the same as the starting one, then we compute a
70 symbolic stride on the return path. The result is then the
71 symbolic chrec {initial_condition, +, symbolic_stride}_loop.
75 Example 1: Illustration of the basic algorithm.
81 | if (c > 10) exit_loop
84 Suppose that we want to know the number of iterations of the
85 loop_1. The exit_loop is controlled by a COND_EXPR (c > 10). We
86 ask the scalar evolution analyzer two questions: what's the
87 scalar evolution (scev) of "c", and what's the scev of "10". For
88 "10" the answer is "10" since it is a scalar constant. For the
89 scalar variable "c", it follows the SSA edge to its definition,
90 "c = b + 1", and then asks again what's the scev of "b".
91 Following the SSA edge, we end on a loop-phi-node "b = phi (a,
92 c)", where the initial condition is "a", and the inner loop edge
93 is "c". The initial condition is kept under a symbolic form (it
94 may be the case that the copy constant propagation has done its
95 work and we end with the constant "3" as one of the edges of the
96 loop-phi-node). The update edge is followed to the end of the
97 loop, and until reaching again the starting loop-phi-node: b -> c
98 -> b. At this point we have drawn a path from "b" to "b" from
99 which we compute the stride in the loop: in this example it is
100 "+1". The resulting scev for "b" is "b -> {a, +, 1}_1". Now
101 that the scev for "b" is known, it is possible to compute the
102 scev for "c", that is "c -> {a + 1, +, 1}_1". In order to
103 determine the number of iterations in the loop_1, we have to
104 instantiate_parameters (loop_1, {a + 1, +, 1}_1), that gives after some
105 more analysis the scev {4, +, 1}_1, or in other words, this is
106 the function "f (x) = x + 4", where x is the iteration count of
107 the loop_1. Now we have to solve the inequality "x + 4 > 10",
108 and take the smallest iteration number for which the loop is
109 exited: x = 7. This loop runs from x = 0 to x = 7, and in total
110 there are 8 iterations. In terms of loop normalization, we have
111 created a variable that is implicitly defined, "x" or just "_1",
112 and all the other analyzed scalars of the loop are defined in
113 function of this variable:
119 or in terms of a C program:
122 | for (x = 0; x <= 7; x++)
128 Example 2a: Illustration of the algorithm on nested loops.
139 For analyzing the scalar evolution of "a", the algorithm follows
140 the SSA edge into the loop's body: "a -> b". "b" is an inner
141 loop-phi-node, and its analysis as in Example 1, gives:
146 Following the SSA edge for the initial condition, we end on "c = a
147 + 2", and then on the starting loop-phi-node "a". From this point,
148 the loop stride is computed: back on "c = a + 2" we get a "+2" in
149 the loop_1, then on the loop-phi-node "b" we compute the overall
150 effect of the inner loop that is "b = c + 30", and we get a "+30"
151 in the loop_1. That means that the overall stride in loop_1 is
152 equal to "+32", and the result is:
157 Example 2b: Multivariate chains of recurrences.
170 Analyzing the access function of array A with
171 instantiate_parameters (loop_1, "j + k"), we obtain the
172 instantiation and the analysis of the scalar variables "j" and "k"
173 in loop_1. This leads to the scalar evolution {4, +, 1}_1: the end
174 value of loop_2 for "j" is 4, and the evolution of "k" in loop_1 is
175 {0, +, 1}_1. To obtain the evolution function in loop_3 and
176 instantiate the scalar variables up to loop_1, one has to use:
177 instantiate_scev (block_before_loop (loop_1), loop_3, "j + k").
178 The result of this call is {{0, +, 1}_1, +, 1}_2.
180 Example 3: Higher degree polynomials.
194 instantiate_parameters (loop_1, {5, +, a}_1) -> {5, +, 2, +, 1}_1
195 instantiate_parameters (loop_1, {5 + a, +, a}_1) -> {7, +, 3, +, 1}_1
197 Example 4: Lucas, Fibonacci, or mixers in general.
209 The syntax "(1, c)_1" stands for a PEELED_CHREC that has the
210 following semantics: during the first iteration of the loop_1, the
211 variable contains the value 1, and then it contains the value "c".
212 Note that this syntax is close to the syntax of the loop-phi-node:
213 "a -> (1, c)_1" vs. "a = phi (1, c)".
215 The symbolic chrec representation contains all the semantics of the
216 original code. What is more difficult is to use this information.
218 Example 5: Flip-flops, or exchangers.
230 Based on these symbolic chrecs, it is possible to refine this
231 information into the more precise PERIODIC_CHRECs:
236 This transformation is not yet implemented.
240 You can find a more detailed description of the algorithm in:
241 http://icps.u-strasbg.fr/~pop/DEA_03_Pop.pdf
242 http://icps.u-strasbg.fr/~pop/DEA_03_Pop.ps.gz. But note that
243 this is a preliminary report and some of the details of the
244 algorithm have changed. I'm working on a research report that
245 updates the description of the algorithms to reflect the design
246 choices used in this implementation.
248 A set of slides show a high level overview of the algorithm and run
249 an example through the scalar evolution analyzer:
250 http://cri.ensmp.fr/~pop/gcc/mar04/slides.pdf
252 The slides that I have presented at the GCC Summit'04 are available
253 at: http://cri.ensmp.fr/~pop/gcc/20040604/gccsummit-lno-spop.pdf
258 #include "coretypes.h"
259 #include "hash-table.h"
260 #include "gimple-pretty-print.h"
261 #include "tree-ssa.h"
263 #include "tree-chrec.h"
264 #include "tree-scalar-evolution.h"
265 #include "dumpfile.h"
268 static tree
analyze_scalar_evolution_1 (struct loop
*, tree
, tree
);
269 static tree
analyze_scalar_evolution_for_address_of (struct loop
*loop
,
272 /* The cached information about an SSA name VAR, claiming that below
273 basic block INSTANTIATED_BELOW, the value of VAR can be expressed
276 struct GTY(()) scev_info_str
{
277 basic_block instantiated_below
;
282 /* Counters for the scev database. */
283 static unsigned nb_set_scev
= 0;
284 static unsigned nb_get_scev
= 0;
286 /* The following trees are unique elements. Thus the comparison of
287 another element to these elements should be done on the pointer to
288 these trees, and not on their value. */
290 /* The SSA_NAMEs that are not yet analyzed are qualified with NULL_TREE. */
291 tree chrec_not_analyzed_yet
;
293 /* Reserved to the cases where the analyzer has detected an
294 undecidable property at compile time. */
295 tree chrec_dont_know
;
297 /* When the analyzer has detected that a property will never
298 happen, then it qualifies it with chrec_known. */
301 static GTY ((param_is (struct scev_info_str
))) htab_t scalar_evolution_info
;
304 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
306 static inline struct scev_info_str
*
307 new_scev_info_str (basic_block instantiated_below
, tree var
)
309 struct scev_info_str
*res
;
311 res
= ggc_alloc_scev_info_str ();
313 res
->chrec
= chrec_not_analyzed_yet
;
314 res
->instantiated_below
= instantiated_below
;
319 /* Computes a hash function for database element ELT. */
321 static inline hashval_t
322 hash_scev_info (const void *elt
)
324 return SSA_NAME_VERSION (((const struct scev_info_str
*) elt
)->var
);
327 /* Compares database elements E1 and E2. */
330 eq_scev_info (const void *e1
, const void *e2
)
332 const struct scev_info_str
*elt1
= (const struct scev_info_str
*) e1
;
333 const struct scev_info_str
*elt2
= (const struct scev_info_str
*) e2
;
335 return (elt1
->var
== elt2
->var
336 && elt1
->instantiated_below
== elt2
->instantiated_below
);
339 /* Deletes database element E. */
342 del_scev_info (void *e
)
348 /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block.
349 A first query on VAR returns chrec_not_analyzed_yet. */
352 find_var_scev_info (basic_block instantiated_below
, tree var
)
354 struct scev_info_str
*res
;
355 struct scev_info_str tmp
;
359 tmp
.instantiated_below
= instantiated_below
;
360 slot
= htab_find_slot (scalar_evolution_info
, &tmp
, INSERT
);
363 *slot
= new_scev_info_str (instantiated_below
, var
);
364 res
= (struct scev_info_str
*) *slot
;
369 /* Return true when CHREC contains symbolic names defined in
373 chrec_contains_symbols_defined_in_loop (const_tree chrec
, unsigned loop_nb
)
377 if (chrec
== NULL_TREE
)
380 if (is_gimple_min_invariant (chrec
))
383 if (TREE_CODE (chrec
) == SSA_NAME
)
386 loop_p def_loop
, loop
;
388 if (SSA_NAME_IS_DEFAULT_DEF (chrec
))
391 def
= SSA_NAME_DEF_STMT (chrec
);
392 def_loop
= loop_containing_stmt (def
);
393 loop
= get_loop (cfun
, loop_nb
);
395 if (def_loop
== NULL
)
398 if (loop
== def_loop
|| flow_loop_nested_p (loop
, def_loop
))
404 n
= TREE_OPERAND_LENGTH (chrec
);
405 for (i
= 0; i
< n
; i
++)
406 if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec
, i
),
412 /* Return true when PHI is a loop-phi-node. */
415 loop_phi_node_p (gimple phi
)
417 /* The implementation of this function is based on the following
418 property: "all the loop-phi-nodes of a loop are contained in the
419 loop's header basic block". */
421 return loop_containing_stmt (phi
)->header
== gimple_bb (phi
);
424 /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
425 In general, in the case of multivariate evolutions we want to get
426 the evolution in different loops. LOOP specifies the level for
427 which to get the evolution.
431 | for (j = 0; j < 100; j++)
433 | for (k = 0; k < 100; k++)
435 | i = k + j; - Here the value of i is a function of j, k.
437 | ... = i - Here the value of i is a function of j.
439 | ... = i - Here the value of i is a scalar.
445 | i_1 = phi (i_0, i_2)
449 This loop has the same effect as:
450 LOOP_1 has the same effect as:
454 The overall effect of the loop, "i_0 + 20" in the previous example,
455 is obtained by passing in the parameters: LOOP = 1,
456 EVOLUTION_FN = {i_0, +, 2}_1.
460 compute_overall_effect_of_inner_loop (struct loop
*loop
, tree evolution_fn
)
464 if (evolution_fn
== chrec_dont_know
)
465 return chrec_dont_know
;
467 else if (TREE_CODE (evolution_fn
) == POLYNOMIAL_CHREC
)
469 struct loop
*inner_loop
= get_chrec_loop (evolution_fn
);
471 if (inner_loop
== loop
472 || flow_loop_nested_p (loop
, inner_loop
))
474 tree nb_iter
= number_of_latch_executions (inner_loop
);
476 if (nb_iter
== chrec_dont_know
)
477 return chrec_dont_know
;
482 /* evolution_fn is the evolution function in LOOP. Get
483 its value in the nb_iter-th iteration. */
484 res
= chrec_apply (inner_loop
->num
, evolution_fn
, nb_iter
);
486 if (chrec_contains_symbols_defined_in_loop (res
, loop
->num
))
487 res
= instantiate_parameters (loop
, res
);
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 /* Associate CHREC to SCALAR. */
508 set_scalar_evolution (basic_block instantiated_below
, tree scalar
, tree chrec
)
512 if (TREE_CODE (scalar
) != SSA_NAME
)
515 scalar_info
= find_var_scev_info (instantiated_below
, scalar
);
519 if (dump_flags
& TDF_SCEV
)
521 fprintf (dump_file
, "(set_scalar_evolution \n");
522 fprintf (dump_file
, " instantiated_below = %d \n",
523 instantiated_below
->index
);
524 fprintf (dump_file
, " (scalar = ");
525 print_generic_expr (dump_file
, scalar
, 0);
526 fprintf (dump_file
, ")\n (scalar_evolution = ");
527 print_generic_expr (dump_file
, chrec
, 0);
528 fprintf (dump_file
, "))\n");
530 if (dump_flags
& TDF_STATS
)
534 *scalar_info
= chrec
;
537 /* Retrieve the chrec associated to SCALAR instantiated below
538 INSTANTIATED_BELOW block. */
541 get_scalar_evolution (basic_block instantiated_below
, tree scalar
)
547 if (dump_flags
& TDF_SCEV
)
549 fprintf (dump_file
, "(get_scalar_evolution \n");
550 fprintf (dump_file
, " (scalar = ");
551 print_generic_expr (dump_file
, scalar
, 0);
552 fprintf (dump_file
, ")\n");
554 if (dump_flags
& TDF_STATS
)
558 switch (TREE_CODE (scalar
))
561 res
= *find_var_scev_info (instantiated_below
, scalar
);
571 res
= chrec_not_analyzed_yet
;
575 if (dump_file
&& (dump_flags
& TDF_SCEV
))
577 fprintf (dump_file
, " (scalar_evolution = ");
578 print_generic_expr (dump_file
, res
, 0);
579 fprintf (dump_file
, "))\n");
585 /* Helper function for add_to_evolution. Returns the evolution
586 function for an assignment of the form "a = b + c", where "a" and
587 "b" are on the strongly connected component. CHREC_BEFORE is the
588 information that we already have collected up to this point.
589 TO_ADD is the evolution of "c".
591 When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
592 evolution the expression TO_ADD, otherwise construct an evolution
593 part for this loop. */
596 add_to_evolution_1 (unsigned loop_nb
, tree chrec_before
, tree to_add
,
599 tree type
, left
, right
;
600 struct loop
*loop
= get_loop (cfun
, loop_nb
), *chloop
;
602 switch (TREE_CODE (chrec_before
))
604 case POLYNOMIAL_CHREC
:
605 chloop
= get_chrec_loop (chrec_before
);
607 || flow_loop_nested_p (chloop
, loop
))
611 type
= chrec_type (chrec_before
);
613 /* When there is no evolution part in this loop, build it. */
618 right
= SCALAR_FLOAT_TYPE_P (type
)
619 ? build_real (type
, dconst0
)
620 : build_int_cst (type
, 0);
624 var
= CHREC_VARIABLE (chrec_before
);
625 left
= CHREC_LEFT (chrec_before
);
626 right
= CHREC_RIGHT (chrec_before
);
629 to_add
= chrec_convert (type
, to_add
, at_stmt
);
630 right
= chrec_convert_rhs (type
, right
, at_stmt
);
631 right
= chrec_fold_plus (chrec_type (right
), right
, to_add
);
632 return build_polynomial_chrec (var
, left
, right
);
636 gcc_assert (flow_loop_nested_p (loop
, chloop
));
638 /* Search the evolution in LOOP_NB. */
639 left
= add_to_evolution_1 (loop_nb
, CHREC_LEFT (chrec_before
),
641 right
= CHREC_RIGHT (chrec_before
);
642 right
= chrec_convert_rhs (chrec_type (left
), right
, at_stmt
);
643 return build_polynomial_chrec (CHREC_VARIABLE (chrec_before
),
648 /* These nodes do not depend on a loop. */
649 if (chrec_before
== chrec_dont_know
)
650 return chrec_dont_know
;
653 right
= chrec_convert_rhs (chrec_type (left
), to_add
, at_stmt
);
654 return build_polynomial_chrec (loop_nb
, left
, right
);
658 /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
661 Description (provided for completeness, for those who read code in
662 a plane, and for my poor 62 bytes brain that would have forgotten
663 all this in the next two or three months):
665 The algorithm of translation of programs from the SSA representation
666 into the chrecs syntax is based on a pattern matching. After having
667 reconstructed the overall tree expression for a loop, there are only
668 two cases that can arise:
670 1. a = loop-phi (init, a + expr)
671 2. a = loop-phi (init, expr)
673 where EXPR is either a scalar constant with respect to the analyzed
674 loop (this is a degree 0 polynomial), or an expression containing
675 other loop-phi definitions (these are higher degree polynomials).
682 | a = phi (init, a + 5)
689 | a = phi (inita, 2 * b + 3)
690 | b = phi (initb, b + 1)
693 For the first case, the semantics of the SSA representation is:
695 | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
697 that is, there is a loop index "x" that determines the scalar value
698 of the variable during the loop execution. During the first
699 iteration, the value is that of the initial condition INIT, while
700 during the subsequent iterations, it is the sum of the initial
701 condition with the sum of all the values of EXPR from the initial
702 iteration to the before last considered iteration.
704 For the second case, the semantics of the SSA program is:
706 | a (x) = init, if x = 0;
707 | expr (x - 1), otherwise.
709 The second case corresponds to the PEELED_CHREC, whose syntax is
710 close to the syntax of a loop-phi-node:
712 | phi (init, expr) vs. (init, expr)_x
714 The proof of the translation algorithm for the first case is a
715 proof by structural induction based on the degree of EXPR.
718 When EXPR is a constant with respect to the analyzed loop, or in
719 other words when EXPR is a polynomial of degree 0, the evolution of
720 the variable A in the loop is an affine function with an initial
721 condition INIT, and a step EXPR. In order to show this, we start
722 from the semantics of the SSA representation:
724 f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
726 and since "expr (j)" is a constant with respect to "j",
728 f (x) = init + x * expr
730 Finally, based on the semantics of the pure sum chrecs, by
731 identification we get the corresponding chrecs syntax:
733 f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
734 f (x) -> {init, +, expr}_x
737 Suppose that EXPR is a polynomial of degree N with respect to the
738 analyzed loop_x for which we have already determined that it is
739 written under the chrecs syntax:
741 | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
743 We start from the semantics of the SSA program:
745 | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
747 | f (x) = init + \sum_{j = 0}^{x - 1}
748 | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
750 | f (x) = init + \sum_{j = 0}^{x - 1}
751 | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
753 | f (x) = init + \sum_{k = 0}^{n - 1}
754 | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
756 | f (x) = init + \sum_{k = 0}^{n - 1}
757 | (b_k * \binom{x}{k + 1})
759 | f (x) = init + b_0 * \binom{x}{1} + ...
760 | + b_{n-1} * \binom{x}{n}
762 | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
763 | + b_{n-1} * \binom{x}{n}
766 And finally from the definition of the chrecs syntax, we identify:
767 | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
769 This shows the mechanism that stands behind the add_to_evolution
770 function. An important point is that the use of symbolic
771 parameters avoids the need of an analysis schedule.
778 | a = phi (inita, a + 2 + b)
779 | b = phi (initb, b + 1)
782 When analyzing "a", the algorithm keeps "b" symbolically:
784 | a -> {inita, +, 2 + b}_1
786 Then, after instantiation, the analyzer ends on the evolution:
788 | a -> {inita, +, 2 + initb, +, 1}_1
793 add_to_evolution (unsigned loop_nb
, tree chrec_before
, enum tree_code code
,
794 tree to_add
, gimple at_stmt
)
796 tree type
= chrec_type (to_add
);
797 tree res
= NULL_TREE
;
799 if (to_add
== NULL_TREE
)
802 /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
803 instantiated at this point. */
804 if (TREE_CODE (to_add
) == POLYNOMIAL_CHREC
)
805 /* This should not happen. */
806 return chrec_dont_know
;
808 if (dump_file
&& (dump_flags
& TDF_SCEV
))
810 fprintf (dump_file
, "(add_to_evolution \n");
811 fprintf (dump_file
, " (loop_nb = %d)\n", loop_nb
);
812 fprintf (dump_file
, " (chrec_before = ");
813 print_generic_expr (dump_file
, chrec_before
, 0);
814 fprintf (dump_file
, ")\n (to_add = ");
815 print_generic_expr (dump_file
, to_add
, 0);
816 fprintf (dump_file
, ")\n");
819 if (code
== MINUS_EXPR
)
820 to_add
= chrec_fold_multiply (type
, to_add
, SCALAR_FLOAT_TYPE_P (type
)
821 ? build_real (type
, dconstm1
)
822 : build_int_cst_type (type
, -1));
824 res
= add_to_evolution_1 (loop_nb
, chrec_before
, to_add
, at_stmt
);
826 if (dump_file
&& (dump_flags
& TDF_SCEV
))
828 fprintf (dump_file
, " (res = ");
829 print_generic_expr (dump_file
, res
, 0);
830 fprintf (dump_file
, "))\n");
838 /* This section selects the loops that will be good candidates for the
839 scalar evolution analysis. For the moment, greedily select all the
840 loop nests we could analyze. */
842 /* For a loop with a single exit edge, return the COND_EXPR that
843 guards the exit edge. If the expression is too difficult to
844 analyze, then give up. */
847 get_loop_exit_condition (const struct loop
*loop
)
850 edge exit_edge
= single_exit (loop
);
852 if (dump_file
&& (dump_flags
& TDF_SCEV
))
853 fprintf (dump_file
, "(get_loop_exit_condition \n ");
859 stmt
= last_stmt (exit_edge
->src
);
860 if (gimple_code (stmt
) == GIMPLE_COND
)
864 if (dump_file
&& (dump_flags
& TDF_SCEV
))
866 print_gimple_stmt (dump_file
, res
, 0, 0);
867 fprintf (dump_file
, ")\n");
874 /* Depth first search algorithm. */
876 typedef enum t_bool
{
883 static t_bool
follow_ssa_edge (struct loop
*loop
, gimple
, gimple
, tree
*, int);
885 /* Follow the ssa edge into the binary expression RHS0 CODE RHS1.
886 Return true if the strongly connected component has been found. */
889 follow_ssa_edge_binary (struct loop
*loop
, gimple at_stmt
,
890 tree type
, tree rhs0
, enum tree_code code
, tree rhs1
,
891 gimple halting_phi
, tree
*evolution_of_loop
, int limit
)
893 t_bool res
= t_false
;
898 case POINTER_PLUS_EXPR
:
900 if (TREE_CODE (rhs0
) == SSA_NAME
)
902 if (TREE_CODE (rhs1
) == SSA_NAME
)
904 /* Match an assignment under the form:
907 /* We want only assignments of form "name + name" contribute to
908 LIMIT, as the other cases do not necessarily contribute to
909 the complexity of the expression. */
912 evol
= *evolution_of_loop
;
913 res
= follow_ssa_edge
914 (loop
, SSA_NAME_DEF_STMT (rhs0
), halting_phi
, &evol
, limit
);
917 *evolution_of_loop
= add_to_evolution
919 chrec_convert (type
, evol
, at_stmt
),
920 code
, rhs1
, at_stmt
);
922 else if (res
== t_false
)
924 res
= follow_ssa_edge
925 (loop
, SSA_NAME_DEF_STMT (rhs1
), halting_phi
,
926 evolution_of_loop
, limit
);
929 *evolution_of_loop
= add_to_evolution
931 chrec_convert (type
, *evolution_of_loop
, at_stmt
),
932 code
, rhs0
, at_stmt
);
934 else if (res
== t_dont_know
)
935 *evolution_of_loop
= chrec_dont_know
;
938 else if (res
== t_dont_know
)
939 *evolution_of_loop
= chrec_dont_know
;
944 /* Match an assignment under the form:
946 res
= follow_ssa_edge
947 (loop
, SSA_NAME_DEF_STMT (rhs0
), halting_phi
,
948 evolution_of_loop
, limit
);
950 *evolution_of_loop
= add_to_evolution
951 (loop
->num
, chrec_convert (type
, *evolution_of_loop
,
953 code
, rhs1
, at_stmt
);
955 else if (res
== t_dont_know
)
956 *evolution_of_loop
= chrec_dont_know
;
960 else if (TREE_CODE (rhs1
) == SSA_NAME
)
962 /* Match an assignment under the form:
964 res
= follow_ssa_edge
965 (loop
, SSA_NAME_DEF_STMT (rhs1
), halting_phi
,
966 evolution_of_loop
, limit
);
968 *evolution_of_loop
= add_to_evolution
969 (loop
->num
, chrec_convert (type
, *evolution_of_loop
,
971 code
, rhs0
, at_stmt
);
973 else if (res
== t_dont_know
)
974 *evolution_of_loop
= chrec_dont_know
;
978 /* Otherwise, match an assignment under the form:
980 /* And there is nothing to do. */
985 /* This case is under the form "opnd0 = rhs0 - rhs1". */
986 if (TREE_CODE (rhs0
) == SSA_NAME
)
988 /* Match an assignment under the form:
991 /* We want only assignments of form "name - name" contribute to
992 LIMIT, as the other cases do not necessarily contribute to
993 the complexity of the expression. */
994 if (TREE_CODE (rhs1
) == SSA_NAME
)
997 res
= follow_ssa_edge (loop
, SSA_NAME_DEF_STMT (rhs0
), halting_phi
,
998 evolution_of_loop
, limit
);
1000 *evolution_of_loop
= add_to_evolution
1001 (loop
->num
, chrec_convert (type
, *evolution_of_loop
, at_stmt
),
1002 MINUS_EXPR
, rhs1
, at_stmt
);
1004 else if (res
== t_dont_know
)
1005 *evolution_of_loop
= chrec_dont_know
;
1008 /* Otherwise, match an assignment under the form:
1010 /* And there is nothing to do. */
1021 /* Follow the ssa edge into the expression EXPR.
1022 Return true if the strongly connected component has been found. */
1025 follow_ssa_edge_expr (struct loop
*loop
, gimple at_stmt
, tree expr
,
1026 gimple halting_phi
, tree
*evolution_of_loop
, int limit
)
1028 enum tree_code code
= TREE_CODE (expr
);
1029 tree type
= TREE_TYPE (expr
), rhs0
, rhs1
;
1032 /* The EXPR is one of the following cases:
1036 - a POINTER_PLUS_EXPR,
1039 - other cases are not yet handled. */
1044 /* This assignment is under the form "a_1 = (cast) rhs. */
1045 res
= follow_ssa_edge_expr (loop
, at_stmt
, TREE_OPERAND (expr
, 0),
1046 halting_phi
, evolution_of_loop
, limit
);
1047 *evolution_of_loop
= chrec_convert (type
, *evolution_of_loop
, at_stmt
);
1051 /* This assignment is under the form "a_1 = 7". */
1056 /* This assignment is under the form: "a_1 = b_2". */
1057 res
= follow_ssa_edge
1058 (loop
, SSA_NAME_DEF_STMT (expr
), halting_phi
, evolution_of_loop
, limit
);
1061 case POINTER_PLUS_EXPR
:
1064 /* This case is under the form "rhs0 +- rhs1". */
1065 rhs0
= TREE_OPERAND (expr
, 0);
1066 rhs1
= TREE_OPERAND (expr
, 1);
1067 type
= TREE_TYPE (rhs0
);
1068 STRIP_USELESS_TYPE_CONVERSION (rhs0
);
1069 STRIP_USELESS_TYPE_CONVERSION (rhs1
);
1070 res
= follow_ssa_edge_binary (loop
, at_stmt
, type
, rhs0
, code
, rhs1
,
1071 halting_phi
, evolution_of_loop
, limit
);
1075 /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR. */
1076 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == MEM_REF
)
1078 expr
= TREE_OPERAND (expr
, 0);
1079 rhs0
= TREE_OPERAND (expr
, 0);
1080 rhs1
= TREE_OPERAND (expr
, 1);
1081 type
= TREE_TYPE (rhs0
);
1082 STRIP_USELESS_TYPE_CONVERSION (rhs0
);
1083 STRIP_USELESS_TYPE_CONVERSION (rhs1
);
1084 res
= follow_ssa_edge_binary (loop
, at_stmt
, type
,
1085 rhs0
, POINTER_PLUS_EXPR
, rhs1
,
1086 halting_phi
, evolution_of_loop
, limit
);
1093 /* This assignment is of the form: "a_1 = ASSERT_EXPR <a_2, ...>"
1094 It must be handled as a copy assignment of the form a_1 = a_2. */
1095 rhs0
= ASSERT_EXPR_VAR (expr
);
1096 if (TREE_CODE (rhs0
) == SSA_NAME
)
1097 res
= follow_ssa_edge (loop
, SSA_NAME_DEF_STMT (rhs0
),
1098 halting_phi
, evolution_of_loop
, limit
);
1111 /* Follow the ssa edge into the right hand side of an assignment STMT.
1112 Return true if the strongly connected component has been found. */
1115 follow_ssa_edge_in_rhs (struct loop
*loop
, gimple stmt
,
1116 gimple halting_phi
, tree
*evolution_of_loop
, int limit
)
1118 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1119 tree type
= gimple_expr_type (stmt
), rhs1
, rhs2
;
1125 /* This assignment is under the form "a_1 = (cast) rhs. */
1126 res
= follow_ssa_edge_expr (loop
, stmt
, gimple_assign_rhs1 (stmt
),
1127 halting_phi
, evolution_of_loop
, limit
);
1128 *evolution_of_loop
= chrec_convert (type
, *evolution_of_loop
, stmt
);
1131 case POINTER_PLUS_EXPR
:
1134 rhs1
= gimple_assign_rhs1 (stmt
);
1135 rhs2
= gimple_assign_rhs2 (stmt
);
1136 type
= TREE_TYPE (rhs1
);
1137 res
= follow_ssa_edge_binary (loop
, stmt
, type
, rhs1
, code
, rhs2
,
1138 halting_phi
, evolution_of_loop
, limit
);
1142 if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
1143 res
= follow_ssa_edge_expr (loop
, stmt
, gimple_assign_rhs1 (stmt
),
1144 halting_phi
, evolution_of_loop
, limit
);
1153 /* Checks whether the I-th argument of a PHI comes from a backedge. */
1156 backedge_phi_arg_p (gimple phi
, int i
)
1158 const_edge e
= gimple_phi_arg_edge (phi
, i
);
1160 /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
1161 about updating it anywhere, and this should work as well most of the
1163 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1169 /* Helper function for one branch of the condition-phi-node. Return
1170 true if the strongly connected component has been found following
1173 static inline t_bool
1174 follow_ssa_edge_in_condition_phi_branch (int i
,
1176 gimple condition_phi
,
1178 tree
*evolution_of_branch
,
1179 tree init_cond
, int limit
)
1181 tree branch
= PHI_ARG_DEF (condition_phi
, i
);
1182 *evolution_of_branch
= chrec_dont_know
;
1184 /* Do not follow back edges (they must belong to an irreducible loop, which
1185 we really do not want to worry about). */
1186 if (backedge_phi_arg_p (condition_phi
, i
))
1189 if (TREE_CODE (branch
) == SSA_NAME
)
1191 *evolution_of_branch
= init_cond
;
1192 return follow_ssa_edge (loop
, SSA_NAME_DEF_STMT (branch
), halting_phi
,
1193 evolution_of_branch
, limit
);
1196 /* This case occurs when one of the condition branches sets
1197 the variable to a constant: i.e. a phi-node like
1198 "a_2 = PHI <a_7(5), 2(6)>;".
1200 FIXME: This case have to be refined correctly:
1201 in some cases it is possible to say something better than
1202 chrec_dont_know, for example using a wrap-around notation. */
1206 /* This function merges the branches of a condition-phi-node in a
1210 follow_ssa_edge_in_condition_phi (struct loop
*loop
,
1211 gimple condition_phi
,
1213 tree
*evolution_of_loop
, int limit
)
1216 tree init
= *evolution_of_loop
;
1217 tree evolution_of_branch
;
1218 t_bool res
= follow_ssa_edge_in_condition_phi_branch (0, loop
, condition_phi
,
1220 &evolution_of_branch
,
1222 if (res
== t_false
|| res
== t_dont_know
)
1225 *evolution_of_loop
= evolution_of_branch
;
1227 n
= gimple_phi_num_args (condition_phi
);
1228 for (i
= 1; i
< n
; i
++)
1230 /* Quickly give up when the evolution of one of the branches is
1232 if (*evolution_of_loop
== chrec_dont_know
)
1235 /* Increase the limit by the PHI argument number to avoid exponential
1236 time and memory complexity. */
1237 res
= follow_ssa_edge_in_condition_phi_branch (i
, loop
, condition_phi
,
1239 &evolution_of_branch
,
1241 if (res
== t_false
|| res
== t_dont_know
)
1244 *evolution_of_loop
= chrec_merge (*evolution_of_loop
,
1245 evolution_of_branch
);
1251 /* Follow an SSA edge in an inner loop. It computes the overall
1252 effect of the loop, and following the symbolic initial conditions,
1253 it follows the edges in the parent loop. The inner loop is
1254 considered as a single statement. */
1257 follow_ssa_edge_inner_loop_phi (struct loop
*outer_loop
,
1258 gimple loop_phi_node
,
1260 tree
*evolution_of_loop
, int limit
)
1262 struct loop
*loop
= loop_containing_stmt (loop_phi_node
);
1263 tree ev
= analyze_scalar_evolution (loop
, PHI_RESULT (loop_phi_node
));
1265 /* Sometimes, the inner loop is too difficult to analyze, and the
1266 result of the analysis is a symbolic parameter. */
1267 if (ev
== PHI_RESULT (loop_phi_node
))
1269 t_bool res
= t_false
;
1270 int i
, n
= gimple_phi_num_args (loop_phi_node
);
1272 for (i
= 0; i
< n
; i
++)
1274 tree arg
= PHI_ARG_DEF (loop_phi_node
, i
);
1277 /* Follow the edges that exit the inner loop. */
1278 bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1279 if (!flow_bb_inside_loop_p (loop
, bb
))
1280 res
= follow_ssa_edge_expr (outer_loop
, loop_phi_node
,
1282 evolution_of_loop
, limit
);
1287 /* If the path crosses this loop-phi, give up. */
1289 *evolution_of_loop
= chrec_dont_know
;
1294 /* Otherwise, compute the overall effect of the inner loop. */
1295 ev
= compute_overall_effect_of_inner_loop (loop
, ev
);
1296 return follow_ssa_edge_expr (outer_loop
, loop_phi_node
, ev
, halting_phi
,
1297 evolution_of_loop
, limit
);
1300 /* Follow an SSA edge from a loop-phi-node to itself, constructing a
1301 path that is analyzed on the return walk. */
1304 follow_ssa_edge (struct loop
*loop
, gimple def
, gimple halting_phi
,
1305 tree
*evolution_of_loop
, int limit
)
1307 struct loop
*def_loop
;
1309 if (gimple_nop_p (def
))
1312 /* Give up if the path is longer than the MAX that we allow. */
1313 if (limit
> PARAM_VALUE (PARAM_SCEV_MAX_EXPR_COMPLEXITY
))
1316 def_loop
= loop_containing_stmt (def
);
1318 switch (gimple_code (def
))
1321 if (!loop_phi_node_p (def
))
1322 /* DEF is a condition-phi-node. Follow the branches, and
1323 record their evolutions. Finally, merge the collected
1324 information and set the approximation to the main
1326 return follow_ssa_edge_in_condition_phi
1327 (loop
, def
, halting_phi
, evolution_of_loop
, limit
);
1329 /* When the analyzed phi is the halting_phi, the
1330 depth-first search is over: we have found a path from
1331 the halting_phi to itself in the loop. */
1332 if (def
== halting_phi
)
1335 /* Otherwise, the evolution of the HALTING_PHI depends
1336 on the evolution of another loop-phi-node, i.e. the
1337 evolution function is a higher degree polynomial. */
1338 if (def_loop
== loop
)
1342 if (flow_loop_nested_p (loop
, def_loop
))
1343 return follow_ssa_edge_inner_loop_phi
1344 (loop
, def
, halting_phi
, evolution_of_loop
, limit
+ 1);
1350 return follow_ssa_edge_in_rhs (loop
, def
, halting_phi
,
1351 evolution_of_loop
, limit
);
1354 /* At this level of abstraction, the program is just a set
1355 of GIMPLE_ASSIGNs and PHI_NODEs. In principle there is no
1356 other node to be handled. */
1363 /* Given a LOOP_PHI_NODE, this function determines the evolution
1364 function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
1367 analyze_evolution_in_loop (gimple loop_phi_node
,
1370 int i
, n
= gimple_phi_num_args (loop_phi_node
);
1371 tree evolution_function
= chrec_not_analyzed_yet
;
1372 struct loop
*loop
= loop_containing_stmt (loop_phi_node
);
1375 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1377 fprintf (dump_file
, "(analyze_evolution_in_loop \n");
1378 fprintf (dump_file
, " (loop_phi_node = ");
1379 print_gimple_stmt (dump_file
, loop_phi_node
, 0, 0);
1380 fprintf (dump_file
, ")\n");
1383 for (i
= 0; i
< n
; i
++)
1385 tree arg
= PHI_ARG_DEF (loop_phi_node
, i
);
1390 /* Select the edges that enter the loop body. */
1391 bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1392 if (!flow_bb_inside_loop_p (loop
, bb
))
1395 if (TREE_CODE (arg
) == SSA_NAME
)
1399 ssa_chain
= SSA_NAME_DEF_STMT (arg
);
1401 /* Pass in the initial condition to the follow edge function. */
1403 res
= follow_ssa_edge (loop
, ssa_chain
, loop_phi_node
, &ev_fn
, 0);
1405 /* If ev_fn has no evolution in the inner loop, and the
1406 init_cond is not equal to ev_fn, then we have an
1407 ambiguity between two possible values, as we cannot know
1408 the number of iterations at this point. */
1409 if (TREE_CODE (ev_fn
) != POLYNOMIAL_CHREC
1410 && no_evolution_in_loop_p (ev_fn
, loop
->num
, &val
) && val
1411 && !operand_equal_p (init_cond
, ev_fn
, 0))
1412 ev_fn
= chrec_dont_know
;
1417 /* When it is impossible to go back on the same
1418 loop_phi_node by following the ssa edges, the
1419 evolution is represented by a peeled chrec, i.e. the
1420 first iteration, EV_FN has the value INIT_COND, then
1421 all the other iterations it has the value of ARG.
1422 For the moment, PEELED_CHREC nodes are not built. */
1424 ev_fn
= chrec_dont_know
;
1426 /* When there are multiple back edges of the loop (which in fact never
1427 happens currently, but nevertheless), merge their evolutions. */
1428 evolution_function
= chrec_merge (evolution_function
, ev_fn
);
1431 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1433 fprintf (dump_file
, " (evolution_function = ");
1434 print_generic_expr (dump_file
, evolution_function
, 0);
1435 fprintf (dump_file
, "))\n");
1438 return evolution_function
;
1441 /* Given a loop-phi-node, return the initial conditions of the
1442 variable on entry of the loop. When the CCP has propagated
1443 constants into the loop-phi-node, the initial condition is
1444 instantiated, otherwise the initial condition is kept symbolic.
1445 This analyzer does not analyze the evolution outside the current
1446 loop, and leaves this task to the on-demand tree reconstructor. */
1449 analyze_initial_condition (gimple loop_phi_node
)
1452 tree init_cond
= chrec_not_analyzed_yet
;
1453 struct loop
*loop
= loop_containing_stmt (loop_phi_node
);
1455 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1457 fprintf (dump_file
, "(analyze_initial_condition \n");
1458 fprintf (dump_file
, " (loop_phi_node = \n");
1459 print_gimple_stmt (dump_file
, loop_phi_node
, 0, 0);
1460 fprintf (dump_file
, ")\n");
1463 n
= gimple_phi_num_args (loop_phi_node
);
1464 for (i
= 0; i
< n
; i
++)
1466 tree branch
= PHI_ARG_DEF (loop_phi_node
, i
);
1467 basic_block bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1469 /* When the branch is oriented to the loop's body, it does
1470 not contribute to the initial condition. */
1471 if (flow_bb_inside_loop_p (loop
, bb
))
1474 if (init_cond
== chrec_not_analyzed_yet
)
1480 if (TREE_CODE (branch
) == SSA_NAME
)
1482 init_cond
= chrec_dont_know
;
1486 init_cond
= chrec_merge (init_cond
, branch
);
1489 /* Ooops -- a loop without an entry??? */
1490 if (init_cond
== chrec_not_analyzed_yet
)
1491 init_cond
= chrec_dont_know
;
1493 /* During early loop unrolling we do not have fully constant propagated IL.
1494 Handle degenerate PHIs here to not miss important unrollings. */
1495 if (TREE_CODE (init_cond
) == SSA_NAME
)
1497 gimple def
= SSA_NAME_DEF_STMT (init_cond
);
1499 if (gimple_code (def
) == GIMPLE_PHI
1500 && (res
= degenerate_phi_result (def
)) != NULL_TREE
1501 /* Only allow invariants here, otherwise we may break
1502 loop-closed SSA form. */
1503 && is_gimple_min_invariant (res
))
1507 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1509 fprintf (dump_file
, " (init_cond = ");
1510 print_generic_expr (dump_file
, init_cond
, 0);
1511 fprintf (dump_file
, "))\n");
1517 /* Analyze the scalar evolution for LOOP_PHI_NODE. */
1520 interpret_loop_phi (struct loop
*loop
, gimple loop_phi_node
)
1523 struct loop
*phi_loop
= loop_containing_stmt (loop_phi_node
);
1526 if (phi_loop
!= loop
)
1528 struct loop
*subloop
;
1529 tree evolution_fn
= analyze_scalar_evolution
1530 (phi_loop
, PHI_RESULT (loop_phi_node
));
1532 /* Dive one level deeper. */
1533 subloop
= superloop_at_depth (phi_loop
, loop_depth (loop
) + 1);
1535 /* Interpret the subloop. */
1536 res
= compute_overall_effect_of_inner_loop (subloop
, evolution_fn
);
1540 /* Otherwise really interpret the loop phi. */
1541 init_cond
= analyze_initial_condition (loop_phi_node
);
1542 res
= analyze_evolution_in_loop (loop_phi_node
, init_cond
);
1544 /* Verify we maintained the correct initial condition throughout
1545 possible conversions in the SSA chain. */
1546 if (res
!= chrec_dont_know
)
1548 tree new_init
= res
;
1549 if (CONVERT_EXPR_P (res
)
1550 && TREE_CODE (TREE_OPERAND (res
, 0)) == POLYNOMIAL_CHREC
)
1551 new_init
= fold_convert (TREE_TYPE (res
),
1552 CHREC_LEFT (TREE_OPERAND (res
, 0)));
1553 else if (TREE_CODE (res
) == POLYNOMIAL_CHREC
)
1554 new_init
= CHREC_LEFT (res
);
1555 STRIP_USELESS_TYPE_CONVERSION (new_init
);
1556 if (TREE_CODE (new_init
) == POLYNOMIAL_CHREC
1557 || !operand_equal_p (init_cond
, new_init
, 0))
1558 return chrec_dont_know
;
1564 /* This function merges the branches of a condition-phi-node,
1565 contained in the outermost loop, and whose arguments are already
1569 interpret_condition_phi (struct loop
*loop
, gimple condition_phi
)
1571 int i
, n
= gimple_phi_num_args (condition_phi
);
1572 tree res
= chrec_not_analyzed_yet
;
1574 for (i
= 0; i
< n
; i
++)
1578 if (backedge_phi_arg_p (condition_phi
, i
))
1580 res
= chrec_dont_know
;
1584 branch_chrec
= analyze_scalar_evolution
1585 (loop
, PHI_ARG_DEF (condition_phi
, i
));
1587 res
= chrec_merge (res
, branch_chrec
);
1593 /* Interpret the operation RHS1 OP RHS2. If we didn't
1594 analyze this node before, follow the definitions until ending
1595 either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node. On the
1596 return path, this function propagates evolutions (ala constant copy
1597 propagation). OPND1 is not a GIMPLE expression because we could
1598 analyze the effect of an inner loop: see interpret_loop_phi. */
1601 interpret_rhs_expr (struct loop
*loop
, gimple at_stmt
,
1602 tree type
, tree rhs1
, enum tree_code code
, tree rhs2
)
1604 tree res
, chrec1
, chrec2
;
1607 if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
1609 if (is_gimple_min_invariant (rhs1
))
1610 return chrec_convert (type
, rhs1
, at_stmt
);
1612 if (code
== SSA_NAME
)
1613 return chrec_convert (type
, analyze_scalar_evolution (loop
, rhs1
),
1616 if (code
== ASSERT_EXPR
)
1618 rhs1
= ASSERT_EXPR_VAR (rhs1
);
1619 return chrec_convert (type
, analyze_scalar_evolution (loop
, rhs1
),
1627 if (TREE_CODE (TREE_OPERAND (rhs1
, 0)) == MEM_REF
1628 || handled_component_p (TREE_OPERAND (rhs1
, 0)))
1630 enum machine_mode mode
;
1631 HOST_WIDE_INT bitsize
, bitpos
;
1638 base
= get_inner_reference (TREE_OPERAND (rhs1
, 0),
1639 &bitsize
, &bitpos
, &offset
,
1640 &mode
, &unsignedp
, &volatilep
, false);
1642 if (TREE_CODE (base
) == MEM_REF
)
1644 rhs2
= TREE_OPERAND (base
, 1);
1645 rhs1
= TREE_OPERAND (base
, 0);
1647 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1648 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1649 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1650 chrec2
= chrec_convert (TREE_TYPE (rhs2
), chrec2
, at_stmt
);
1651 chrec1
= instantiate_parameters (loop
, chrec1
);
1652 chrec2
= instantiate_parameters (loop
, chrec2
);
1653 res
= chrec_fold_plus (type
, chrec1
, chrec2
);
1657 chrec1
= analyze_scalar_evolution_for_address_of (loop
, base
);
1658 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1662 if (offset
!= NULL_TREE
)
1664 chrec2
= analyze_scalar_evolution (loop
, offset
);
1665 chrec2
= chrec_convert (TREE_TYPE (offset
), chrec2
, at_stmt
);
1666 chrec2
= instantiate_parameters (loop
, chrec2
);
1667 res
= chrec_fold_plus (type
, res
, chrec2
);
1672 gcc_assert ((bitpos
% BITS_PER_UNIT
) == 0);
1674 unitpos
= size_int (bitpos
/ BITS_PER_UNIT
);
1675 chrec3
= analyze_scalar_evolution (loop
, unitpos
);
1676 chrec3
= chrec_convert (TREE_TYPE (unitpos
), chrec3
, at_stmt
);
1677 chrec3
= instantiate_parameters (loop
, chrec3
);
1678 res
= chrec_fold_plus (type
, res
, chrec3
);
1682 res
= chrec_dont_know
;
1685 case POINTER_PLUS_EXPR
:
1686 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1687 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1688 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1689 chrec2
= chrec_convert (TREE_TYPE (rhs2
), chrec2
, at_stmt
);
1690 chrec1
= instantiate_parameters (loop
, chrec1
);
1691 chrec2
= instantiate_parameters (loop
, chrec2
);
1692 res
= chrec_fold_plus (type
, chrec1
, chrec2
);
1696 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1697 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1698 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1699 chrec2
= chrec_convert (type
, chrec2
, at_stmt
);
1700 chrec1
= instantiate_parameters (loop
, chrec1
);
1701 chrec2
= instantiate_parameters (loop
, chrec2
);
1702 res
= chrec_fold_plus (type
, chrec1
, chrec2
);
1706 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1707 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1708 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1709 chrec2
= chrec_convert (type
, chrec2
, at_stmt
);
1710 chrec1
= instantiate_parameters (loop
, chrec1
);
1711 chrec2
= instantiate_parameters (loop
, chrec2
);
1712 res
= chrec_fold_minus (type
, chrec1
, chrec2
);
1716 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1717 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1718 /* TYPE may be integer, real or complex, so use fold_convert. */
1719 chrec1
= instantiate_parameters (loop
, chrec1
);
1720 res
= chrec_fold_multiply (type
, chrec1
,
1721 fold_convert (type
, integer_minus_one_node
));
1725 /* Handle ~X as -1 - X. */
1726 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1727 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1728 chrec1
= instantiate_parameters (loop
, chrec1
);
1729 res
= chrec_fold_minus (type
,
1730 fold_convert (type
, integer_minus_one_node
),
1735 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1736 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1737 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1738 chrec2
= chrec_convert (type
, chrec2
, at_stmt
);
1739 chrec1
= instantiate_parameters (loop
, chrec1
);
1740 chrec2
= instantiate_parameters (loop
, chrec2
);
1741 res
= chrec_fold_multiply (type
, chrec1
, chrec2
);
1745 /* In case we have a truncation of a widened operation that in
1746 the truncated type has undefined overflow behavior analyze
1747 the operation done in an unsigned type of the same precision
1748 as the final truncation. We cannot derive a scalar evolution
1749 for the widened operation but for the truncated result. */
1750 if (TREE_CODE (type
) == INTEGER_TYPE
1751 && TREE_CODE (TREE_TYPE (rhs1
)) == INTEGER_TYPE
1752 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (rhs1
))
1753 && TYPE_OVERFLOW_UNDEFINED (type
)
1754 && TREE_CODE (rhs1
) == SSA_NAME
1755 && (def
= SSA_NAME_DEF_STMT (rhs1
))
1756 && is_gimple_assign (def
)
1757 && TREE_CODE_CLASS (gimple_assign_rhs_code (def
)) == tcc_binary
1758 && TREE_CODE (gimple_assign_rhs2 (def
)) == INTEGER_CST
)
1760 tree utype
= unsigned_type_for (type
);
1761 chrec1
= interpret_rhs_expr (loop
, at_stmt
, utype
,
1762 gimple_assign_rhs1 (def
),
1763 gimple_assign_rhs_code (def
),
1764 gimple_assign_rhs2 (def
));
1767 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1768 res
= chrec_convert (type
, chrec1
, at_stmt
);
1772 res
= chrec_dont_know
;
1779 /* Interpret the expression EXPR. */
1782 interpret_expr (struct loop
*loop
, gimple at_stmt
, tree expr
)
1784 enum tree_code code
;
1785 tree type
= TREE_TYPE (expr
), op0
, op1
;
1787 if (automatically_generated_chrec_p (expr
))
1790 if (TREE_CODE (expr
) == POLYNOMIAL_CHREC
1791 || get_gimple_rhs_class (TREE_CODE (expr
)) == GIMPLE_TERNARY_RHS
)
1792 return chrec_dont_know
;
1794 extract_ops_from_tree (expr
, &code
, &op0
, &op1
);
1796 return interpret_rhs_expr (loop
, at_stmt
, type
,
1800 /* Interpret the rhs of the assignment STMT. */
1803 interpret_gimple_assign (struct loop
*loop
, gimple stmt
)
1805 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1806 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1808 return interpret_rhs_expr (loop
, stmt
, type
,
1809 gimple_assign_rhs1 (stmt
), code
,
1810 gimple_assign_rhs2 (stmt
));
1815 /* This section contains all the entry points:
1816 - number_of_iterations_in_loop,
1817 - analyze_scalar_evolution,
1818 - instantiate_parameters.
1821 /* Compute and return the evolution function in WRTO_LOOP, the nearest
1822 common ancestor of DEF_LOOP and USE_LOOP. */
1825 compute_scalar_evolution_in_loop (struct loop
*wrto_loop
,
1826 struct loop
*def_loop
,
1832 if (def_loop
== wrto_loop
)
1835 def_loop
= superloop_at_depth (def_loop
, loop_depth (wrto_loop
) + 1);
1836 res
= compute_overall_effect_of_inner_loop (def_loop
, ev
);
1838 if (no_evolution_in_loop_p (res
, wrto_loop
->num
, &val
) && val
)
1841 return analyze_scalar_evolution_1 (wrto_loop
, res
, chrec_not_analyzed_yet
);
1844 /* Helper recursive function. */
1847 analyze_scalar_evolution_1 (struct loop
*loop
, tree var
, tree res
)
1849 tree type
= TREE_TYPE (var
);
1852 struct loop
*def_loop
;
1854 if (loop
== NULL
|| TREE_CODE (type
) == VECTOR_TYPE
)
1855 return chrec_dont_know
;
1857 if (TREE_CODE (var
) != SSA_NAME
)
1858 return interpret_expr (loop
, NULL
, var
);
1860 def
= SSA_NAME_DEF_STMT (var
);
1861 bb
= gimple_bb (def
);
1862 def_loop
= bb
? bb
->loop_father
: NULL
;
1865 || !flow_bb_inside_loop_p (loop
, bb
))
1867 /* Keep the symbolic form. */
1872 if (res
!= chrec_not_analyzed_yet
)
1874 if (loop
!= bb
->loop_father
)
1875 res
= compute_scalar_evolution_in_loop
1876 (find_common_loop (loop
, bb
->loop_father
), bb
->loop_father
, res
);
1881 if (loop
!= def_loop
)
1883 res
= analyze_scalar_evolution_1 (def_loop
, var
, chrec_not_analyzed_yet
);
1884 res
= compute_scalar_evolution_in_loop (loop
, def_loop
, res
);
1889 switch (gimple_code (def
))
1892 res
= interpret_gimple_assign (loop
, def
);
1896 if (loop_phi_node_p (def
))
1897 res
= interpret_loop_phi (loop
, def
);
1899 res
= interpret_condition_phi (loop
, def
);
1903 res
= chrec_dont_know
;
1909 /* Keep the symbolic form. */
1910 if (res
== chrec_dont_know
)
1913 if (loop
== def_loop
)
1914 set_scalar_evolution (block_before_loop (loop
), var
, res
);
1919 /* Analyzes and returns the scalar evolution of the ssa_name VAR in
1920 LOOP. LOOP is the loop in which the variable is used.
1922 Example of use: having a pointer VAR to a SSA_NAME node, STMT a
1923 pointer to the statement that uses this variable, in order to
1924 determine the evolution function of the variable, use the following
1927 loop_p loop = loop_containing_stmt (stmt);
1928 tree chrec_with_symbols = analyze_scalar_evolution (loop, var);
1929 tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
1933 analyze_scalar_evolution (struct loop
*loop
, tree var
)
1937 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1939 fprintf (dump_file
, "(analyze_scalar_evolution \n");
1940 fprintf (dump_file
, " (loop_nb = %d)\n", loop
->num
);
1941 fprintf (dump_file
, " (scalar = ");
1942 print_generic_expr (dump_file
, var
, 0);
1943 fprintf (dump_file
, ")\n");
1946 res
= get_scalar_evolution (block_before_loop (loop
), var
);
1947 res
= analyze_scalar_evolution_1 (loop
, var
, res
);
1949 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1950 fprintf (dump_file
, ")\n");
1955 /* Analyzes and returns the scalar evolution of VAR address in LOOP. */
1958 analyze_scalar_evolution_for_address_of (struct loop
*loop
, tree var
)
1960 return analyze_scalar_evolution (loop
, build_fold_addr_expr (var
));
1963 /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
1964 WRTO_LOOP (which should be a superloop of USE_LOOP)
1966 FOLDED_CASTS is set to true if resolve_mixers used
1967 chrec_convert_aggressive (TODO -- not really, we are way too conservative
1968 at the moment in order to keep things simple).
1970 To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
1973 for (i = 0; i < 100; i++) -- loop 1
1975 for (j = 0; j < 100; j++) -- loop 2
1982 for (t = 0; t < 100; t++) -- loop 3
1989 Both k1 and k2 are invariants in loop3, thus
1990 analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
1991 analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
1993 As they are invariant, it does not matter whether we consider their
1994 usage in loop 3 or loop 2, hence
1995 analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
1996 analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
1997 analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
1998 analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
2000 Similarly for their evolutions with respect to loop 1. The values of K2
2001 in the use in loop 2 vary independently on loop 1, thus we cannot express
2002 the evolution with respect to loop 1:
2003 analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
2004 analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
2005 analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
2006 analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
2008 The value of k2 in the use in loop 1 is known, though:
2009 analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
2010 analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
2014 analyze_scalar_evolution_in_loop (struct loop
*wrto_loop
, struct loop
*use_loop
,
2015 tree version
, bool *folded_casts
)
2018 tree ev
= version
, tmp
;
2020 /* We cannot just do
2022 tmp = analyze_scalar_evolution (use_loop, version);
2023 ev = resolve_mixers (wrto_loop, tmp);
2025 as resolve_mixers would query the scalar evolution with respect to
2026 wrto_loop. For example, in the situation described in the function
2027 comment, suppose that wrto_loop = loop1, use_loop = loop3 and
2030 analyze_scalar_evolution (use_loop, version) = k2
2032 and resolve_mixers (loop1, k2) finds that the value of k2 in loop 1
2033 is 100, which is a wrong result, since we are interested in the
2036 Instead, we need to proceed from use_loop to wrto_loop loop by loop,
2037 each time checking that there is no evolution in the inner loop. */
2040 *folded_casts
= false;
2043 tmp
= analyze_scalar_evolution (use_loop
, ev
);
2044 ev
= resolve_mixers (use_loop
, tmp
);
2046 if (folded_casts
&& tmp
!= ev
)
2047 *folded_casts
= true;
2049 if (use_loop
== wrto_loop
)
2052 /* If the value of the use changes in the inner loop, we cannot express
2053 its value in the outer loop (we might try to return interval chrec,
2054 but we do not have a user for it anyway) */
2055 if (!no_evolution_in_loop_p (ev
, use_loop
->num
, &val
)
2057 return chrec_dont_know
;
2059 use_loop
= loop_outer (use_loop
);
2064 /* Hashtable helpers for a temporary hash-table used when
2065 instantiating a CHREC or resolving mixers. For this use
2066 instantiated_below is always the same. */
2068 struct instantiate_cache_entry
2074 struct instantiate_cache_type
2076 pointer_map
<unsigned> *map
;
2077 vec
<instantiate_cache_entry
> entries
;
2079 instantiate_cache_type () : map (NULL
), entries(vNULL
) {}
2080 ~instantiate_cache_type ();
2081 tree
get (unsigned slot
) { return entries
[slot
].chrec
; }
2082 void set (unsigned slot
, tree chrec
) { entries
[slot
].chrec
= chrec
; }
2085 instantiate_cache_type::~instantiate_cache_type ()
2094 /* Returns from CACHE the slot number of the cached chrec for NAME. */
2097 get_instantiated_value_entry (instantiate_cache_type
&cache
, tree name
)
2101 cache
.map
= new pointer_map
<unsigned>;
2102 cache
.entries
.create (10);
2106 unsigned *slot
= cache
.map
->insert (name
, &existed_p
);
2109 struct instantiate_cache_entry e
;
2111 e
.chrec
= chrec_not_analyzed_yet
;
2112 *slot
= cache
.entries
.length ();
2113 cache
.entries
.safe_push (e
);
2119 /* Cache to avoid infinite recursion when instantiating an SSA name.
2120 Live during the outermost instantiate_scev or resolve_mixers call. */
2121 static instantiate_cache_type
*global_cache
;
2124 /* Return the closed_loop_phi node for VAR. If there is none, return
2128 loop_closed_phi_def (tree var
)
2133 gimple_stmt_iterator psi
;
2135 if (var
== NULL_TREE
2136 || TREE_CODE (var
) != SSA_NAME
)
2139 loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (var
));
2140 exit
= single_exit (loop
);
2144 for (psi
= gsi_start_phis (exit
->dest
); !gsi_end_p (psi
); gsi_next (&psi
))
2146 phi
= gsi_stmt (psi
);
2147 if (PHI_ARG_DEF_FROM_EDGE (phi
, exit
) == var
)
2148 return PHI_RESULT (phi
);
2154 static tree
instantiate_scev_r (basic_block
, struct loop
*, struct loop
*,
2157 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2158 and EVOLUTION_LOOP, that were left under a symbolic form.
2160 CHREC is an SSA_NAME to be instantiated.
2162 CACHE is the cache of already instantiated values.
2164 FOLD_CONVERSIONS should be set to true when the conversions that
2165 may wrap in signed/pointer type are folded, as long as the value of
2166 the chrec is preserved.
2168 SIZE_EXPR is used for computing the size of the expression to be
2169 instantiated, and to stop if it exceeds some limit. */
2172 instantiate_scev_name (basic_block instantiate_below
,
2173 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2175 bool fold_conversions
,
2179 struct loop
*def_loop
;
2180 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (chrec
));
2182 /* A parameter (or loop invariant and we do not want to include
2183 evolutions in outer loops), nothing to do. */
2185 || loop_depth (def_bb
->loop_father
) == 0
2186 || dominated_by_p (CDI_DOMINATORS
, instantiate_below
, def_bb
))
2189 /* We cache the value of instantiated variable to avoid exponential
2190 time complexity due to reevaluations. We also store the convenient
2191 value in the cache in order to prevent infinite recursion -- we do
2192 not want to instantiate the SSA_NAME if it is in a mixer
2193 structure. This is used for avoiding the instantiation of
2194 recursively defined functions, such as:
2196 | a_2 -> {0, +, 1, +, a_2}_1 */
2198 unsigned si
= get_instantiated_value_entry (*global_cache
, chrec
);
2199 if (global_cache
->get (si
) != chrec_not_analyzed_yet
)
2200 return global_cache
->get (si
);
2202 /* On recursion return chrec_dont_know. */
2203 global_cache
->set (si
, chrec_dont_know
);
2205 def_loop
= find_common_loop (evolution_loop
, def_bb
->loop_father
);
2207 /* If the analysis yields a parametric chrec, instantiate the
2209 res
= analyze_scalar_evolution (def_loop
, chrec
);
2211 /* Don't instantiate default definitions. */
2212 if (TREE_CODE (res
) == SSA_NAME
2213 && SSA_NAME_IS_DEFAULT_DEF (res
))
2216 /* Don't instantiate loop-closed-ssa phi nodes. */
2217 else if (TREE_CODE (res
) == SSA_NAME
2218 && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res
)))
2219 > loop_depth (def_loop
))
2222 res
= loop_closed_phi_def (chrec
);
2226 /* When there is no loop_closed_phi_def, it means that the
2227 variable is not used after the loop: try to still compute the
2228 value of the variable when exiting the loop. */
2229 if (res
== NULL_TREE
)
2231 loop_p loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (chrec
));
2232 res
= analyze_scalar_evolution (loop
, chrec
);
2233 res
= compute_overall_effect_of_inner_loop (loop
, res
);
2234 res
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2236 fold_conversions
, size_expr
);
2238 else if (!dominated_by_p (CDI_DOMINATORS
, instantiate_below
,
2239 gimple_bb (SSA_NAME_DEF_STMT (res
))))
2240 res
= chrec_dont_know
;
2243 else if (res
!= chrec_dont_know
)
2246 && def_bb
->loop_father
!= inner_loop
2247 && !flow_loop_nested_p (def_bb
->loop_father
, inner_loop
))
2248 /* ??? We could try to compute the overall effect of the loop here. */
2249 res
= chrec_dont_know
;
2251 res
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2253 fold_conversions
, size_expr
);
2256 /* Store the correct value to the cache. */
2257 global_cache
->set (si
, res
);
2261 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2262 and EVOLUTION_LOOP, that were left under a symbolic form.
2264 CHREC is a polynomial chain of recurrence to be instantiated.
2266 CACHE is the cache of already instantiated values.
2268 FOLD_CONVERSIONS should be set to true when the conversions that
2269 may wrap in signed/pointer type are folded, as long as the value of
2270 the chrec is preserved.
2272 SIZE_EXPR is used for computing the size of the expression to be
2273 instantiated, and to stop if it exceeds some limit. */
2276 instantiate_scev_poly (basic_block instantiate_below
,
2277 struct loop
*evolution_loop
, struct loop
*,
2278 tree chrec
, bool fold_conversions
, int size_expr
)
2281 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2282 get_chrec_loop (chrec
),
2283 CHREC_LEFT (chrec
), fold_conversions
,
2285 if (op0
== chrec_dont_know
)
2286 return chrec_dont_know
;
2288 op1
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2289 get_chrec_loop (chrec
),
2290 CHREC_RIGHT (chrec
), fold_conversions
,
2292 if (op1
== chrec_dont_know
)
2293 return chrec_dont_know
;
2295 if (CHREC_LEFT (chrec
) != op0
2296 || CHREC_RIGHT (chrec
) != op1
)
2298 op1
= chrec_convert_rhs (chrec_type (op0
), op1
, NULL
);
2299 chrec
= build_polynomial_chrec (CHREC_VARIABLE (chrec
), op0
, op1
);
2305 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2306 and EVOLUTION_LOOP, that were left under a symbolic form.
2308 "C0 CODE C1" is a binary expression of type TYPE to be instantiated.
2310 CACHE is the cache of already instantiated values.
2312 FOLD_CONVERSIONS should be set to true when the conversions that
2313 may wrap in signed/pointer type are folded, as long as the value of
2314 the chrec is preserved.
2316 SIZE_EXPR is used for computing the size of the expression to be
2317 instantiated, and to stop if it exceeds some limit. */
2320 instantiate_scev_binary (basic_block instantiate_below
,
2321 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2322 tree chrec
, enum tree_code code
,
2323 tree type
, tree c0
, tree c1
,
2324 bool fold_conversions
, int size_expr
)
2327 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
, inner_loop
,
2328 c0
, fold_conversions
, size_expr
);
2329 if (op0
== chrec_dont_know
)
2330 return chrec_dont_know
;
2332 op1
= instantiate_scev_r (instantiate_below
, evolution_loop
, inner_loop
,
2333 c1
, fold_conversions
, size_expr
);
2334 if (op1
== chrec_dont_know
)
2335 return chrec_dont_know
;
2340 op0
= chrec_convert (type
, op0
, NULL
);
2341 op1
= chrec_convert_rhs (type
, op1
, NULL
);
2345 case POINTER_PLUS_EXPR
:
2347 return chrec_fold_plus (type
, op0
, op1
);
2350 return chrec_fold_minus (type
, op0
, op1
);
2353 return chrec_fold_multiply (type
, op0
, op1
);
2360 return chrec
? chrec
: fold_build2 (code
, type
, c0
, c1
);
2363 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2364 and EVOLUTION_LOOP, that were left under a symbolic form.
2366 "CHREC" is an array reference to be instantiated.
2368 CACHE is the cache of already instantiated values.
2370 FOLD_CONVERSIONS should be set to true when the conversions that
2371 may wrap in signed/pointer type are folded, as long as the value of
2372 the chrec is preserved.
2374 SIZE_EXPR is used for computing the size of the expression to be
2375 instantiated, and to stop if it exceeds some limit. */
2378 instantiate_array_ref (basic_block instantiate_below
,
2379 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2380 tree chrec
, bool fold_conversions
, int size_expr
)
2383 tree index
= TREE_OPERAND (chrec
, 1);
2384 tree op1
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2386 fold_conversions
, size_expr
);
2388 if (op1
== chrec_dont_know
)
2389 return chrec_dont_know
;
2391 if (chrec
&& op1
== index
)
2394 res
= unshare_expr (chrec
);
2395 TREE_OPERAND (res
, 1) = op1
;
2399 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2400 and EVOLUTION_LOOP, that were left under a symbolic form.
2402 "CHREC" that stands for a convert expression "(TYPE) OP" is to be
2405 CACHE is the cache of already instantiated values.
2407 FOLD_CONVERSIONS should be set to true when the conversions that
2408 may wrap in signed/pointer type are folded, as long as the value of
2409 the chrec is preserved.
2411 SIZE_EXPR is used for computing the size of the expression to be
2412 instantiated, and to stop if it exceeds some limit. */
2415 instantiate_scev_convert (basic_block instantiate_below
,
2416 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2417 tree chrec
, tree type
, tree op
,
2418 bool fold_conversions
, int size_expr
)
2420 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2422 fold_conversions
, size_expr
);
2424 if (op0
== chrec_dont_know
)
2425 return chrec_dont_know
;
2427 if (fold_conversions
)
2429 tree tmp
= chrec_convert_aggressive (type
, op0
);
2434 if (chrec
&& op0
== op
)
2437 /* If we used chrec_convert_aggressive, we can no longer assume that
2438 signed chrecs do not overflow, as chrec_convert does, so avoid
2439 calling it in that case. */
2440 if (fold_conversions
)
2441 return fold_convert (type
, op0
);
2443 return chrec_convert (type
, op0
, NULL
);
2446 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2447 and EVOLUTION_LOOP, that were left under a symbolic form.
2449 CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated.
2450 Handle ~X as -1 - X.
2451 Handle -X as -1 * X.
2453 CACHE is the cache of already instantiated values.
2455 FOLD_CONVERSIONS should be set to true when the conversions that
2456 may wrap in signed/pointer type are folded, as long as the value of
2457 the chrec is preserved.
2459 SIZE_EXPR is used for computing the size of the expression to be
2460 instantiated, and to stop if it exceeds some limit. */
2463 instantiate_scev_not (basic_block instantiate_below
,
2464 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2466 enum tree_code code
, tree type
, tree op
,
2467 bool fold_conversions
, int size_expr
)
2469 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2471 fold_conversions
, size_expr
);
2473 if (op0
== chrec_dont_know
)
2474 return chrec_dont_know
;
2478 op0
= chrec_convert (type
, op0
, NULL
);
2483 return chrec_fold_minus
2484 (type
, fold_convert (type
, integer_minus_one_node
), op0
);
2487 return chrec_fold_multiply
2488 (type
, fold_convert (type
, integer_minus_one_node
), op0
);
2495 return chrec
? chrec
: fold_build1 (code
, type
, op0
);
2498 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2499 and EVOLUTION_LOOP, that were left under a symbolic form.
2501 CHREC is an expression with 3 operands to be instantiated.
2503 CACHE is the cache of already instantiated values.
2505 FOLD_CONVERSIONS should be set to true when the conversions that
2506 may wrap in signed/pointer type are folded, as long as the value of
2507 the chrec is preserved.
2509 SIZE_EXPR is used for computing the size of the expression to be
2510 instantiated, and to stop if it exceeds some limit. */
2513 instantiate_scev_3 (basic_block instantiate_below
,
2514 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2516 bool fold_conversions
, int size_expr
)
2519 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2520 inner_loop
, TREE_OPERAND (chrec
, 0),
2521 fold_conversions
, size_expr
);
2522 if (op0
== chrec_dont_know
)
2523 return chrec_dont_know
;
2525 op1
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2526 inner_loop
, TREE_OPERAND (chrec
, 1),
2527 fold_conversions
, size_expr
);
2528 if (op1
== chrec_dont_know
)
2529 return chrec_dont_know
;
2531 op2
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2532 inner_loop
, TREE_OPERAND (chrec
, 2),
2533 fold_conversions
, size_expr
);
2534 if (op2
== chrec_dont_know
)
2535 return chrec_dont_know
;
2537 if (op0
== TREE_OPERAND (chrec
, 0)
2538 && op1
== TREE_OPERAND (chrec
, 1)
2539 && op2
== TREE_OPERAND (chrec
, 2))
2542 return fold_build3 (TREE_CODE (chrec
),
2543 TREE_TYPE (chrec
), op0
, op1
, op2
);
2546 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2547 and EVOLUTION_LOOP, that were left under a symbolic form.
2549 CHREC is an expression with 2 operands to be instantiated.
2551 CACHE is the cache of already instantiated values.
2553 FOLD_CONVERSIONS should be set to true when the conversions that
2554 may wrap in signed/pointer type are folded, as long as the value of
2555 the chrec is preserved.
2557 SIZE_EXPR is used for computing the size of the expression to be
2558 instantiated, and to stop if it exceeds some limit. */
2561 instantiate_scev_2 (basic_block instantiate_below
,
2562 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2564 bool fold_conversions
, int size_expr
)
2567 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2568 inner_loop
, TREE_OPERAND (chrec
, 0),
2569 fold_conversions
, size_expr
);
2570 if (op0
== chrec_dont_know
)
2571 return chrec_dont_know
;
2573 op1
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2574 inner_loop
, TREE_OPERAND (chrec
, 1),
2575 fold_conversions
, size_expr
);
2576 if (op1
== chrec_dont_know
)
2577 return chrec_dont_know
;
2579 if (op0
== TREE_OPERAND (chrec
, 0)
2580 && op1
== TREE_OPERAND (chrec
, 1))
2583 return fold_build2 (TREE_CODE (chrec
), TREE_TYPE (chrec
), op0
, op1
);
2586 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2587 and EVOLUTION_LOOP, that were left under a symbolic form.
2589 CHREC is an expression with 2 operands to be instantiated.
2591 CACHE is the cache of already instantiated values.
2593 FOLD_CONVERSIONS should be set to true when the conversions that
2594 may wrap in signed/pointer type are folded, as long as the value of
2595 the chrec is preserved.
2597 SIZE_EXPR is used for computing the size of the expression to be
2598 instantiated, and to stop if it exceeds some limit. */
2601 instantiate_scev_1 (basic_block instantiate_below
,
2602 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2604 bool fold_conversions
, int size_expr
)
2606 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2607 inner_loop
, TREE_OPERAND (chrec
, 0),
2608 fold_conversions
, size_expr
);
2610 if (op0
== chrec_dont_know
)
2611 return chrec_dont_know
;
2613 if (op0
== TREE_OPERAND (chrec
, 0))
2616 return fold_build1 (TREE_CODE (chrec
), TREE_TYPE (chrec
), op0
);
2619 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2620 and EVOLUTION_LOOP, that were left under a symbolic form.
2622 CHREC is the scalar evolution to instantiate.
2624 CACHE is the cache of already instantiated values.
2626 FOLD_CONVERSIONS should be set to true when the conversions that
2627 may wrap in signed/pointer type are folded, as long as the value of
2628 the chrec is preserved.
2630 SIZE_EXPR is used for computing the size of the expression to be
2631 instantiated, and to stop if it exceeds some limit. */
2634 instantiate_scev_r (basic_block instantiate_below
,
2635 struct loop
*evolution_loop
, struct loop
*inner_loop
,
2637 bool fold_conversions
, int size_expr
)
2639 /* Give up if the expression is larger than the MAX that we allow. */
2640 if (size_expr
++ > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE
))
2641 return chrec_dont_know
;
2643 if (chrec
== NULL_TREE
2644 || automatically_generated_chrec_p (chrec
)
2645 || is_gimple_min_invariant (chrec
))
2648 switch (TREE_CODE (chrec
))
2651 return instantiate_scev_name (instantiate_below
, evolution_loop
,
2653 fold_conversions
, size_expr
);
2655 case POLYNOMIAL_CHREC
:
2656 return instantiate_scev_poly (instantiate_below
, evolution_loop
,
2658 fold_conversions
, size_expr
);
2660 case POINTER_PLUS_EXPR
:
2664 return instantiate_scev_binary (instantiate_below
, evolution_loop
,
2666 TREE_CODE (chrec
), chrec_type (chrec
),
2667 TREE_OPERAND (chrec
, 0),
2668 TREE_OPERAND (chrec
, 1),
2669 fold_conversions
, size_expr
);
2672 return instantiate_scev_convert (instantiate_below
, evolution_loop
,
2674 TREE_TYPE (chrec
), TREE_OPERAND (chrec
, 0),
2675 fold_conversions
, size_expr
);
2679 return instantiate_scev_not (instantiate_below
, evolution_loop
,
2681 TREE_CODE (chrec
), TREE_TYPE (chrec
),
2682 TREE_OPERAND (chrec
, 0),
2683 fold_conversions
, size_expr
);
2686 case SCEV_NOT_KNOWN
:
2687 return chrec_dont_know
;
2693 return instantiate_array_ref (instantiate_below
, evolution_loop
,
2695 fold_conversions
, size_expr
);
2701 if (VL_EXP_CLASS_P (chrec
))
2702 return chrec_dont_know
;
2704 switch (TREE_CODE_LENGTH (TREE_CODE (chrec
)))
2707 return instantiate_scev_3 (instantiate_below
, evolution_loop
,
2709 fold_conversions
, size_expr
);
2712 return instantiate_scev_2 (instantiate_below
, evolution_loop
,
2714 fold_conversions
, size_expr
);
2717 return instantiate_scev_1 (instantiate_below
, evolution_loop
,
2719 fold_conversions
, size_expr
);
2728 /* Too complicated to handle. */
2729 return chrec_dont_know
;
2732 /* Analyze all the parameters of the chrec that were left under a
2733 symbolic form. INSTANTIATE_BELOW is the basic block that stops the
2734 recursive instantiation of parameters: a parameter is a variable
2735 that is defined in a basic block that dominates INSTANTIATE_BELOW or
2736 a function parameter. */
2739 instantiate_scev (basic_block instantiate_below
, struct loop
*evolution_loop
,
2744 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2746 fprintf (dump_file
, "(instantiate_scev \n");
2747 fprintf (dump_file
, " (instantiate_below = %d)\n", instantiate_below
->index
);
2748 fprintf (dump_file
, " (evolution_loop = %d)\n", evolution_loop
->num
);
2749 fprintf (dump_file
, " (chrec = ");
2750 print_generic_expr (dump_file
, chrec
, 0);
2751 fprintf (dump_file
, ")\n");
2757 global_cache
= new instantiate_cache_type
;
2761 res
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2762 NULL
, chrec
, false, 0);
2766 delete global_cache
;
2767 global_cache
= NULL
;
2770 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2772 fprintf (dump_file
, " (res = ");
2773 print_generic_expr (dump_file
, res
, 0);
2774 fprintf (dump_file
, "))\n");
2780 /* Similar to instantiate_parameters, but does not introduce the
2781 evolutions in outer loops for LOOP invariants in CHREC, and does not
2782 care about causing overflows, as long as they do not affect value
2783 of an expression. */
2786 resolve_mixers (struct loop
*loop
, tree chrec
)
2791 global_cache
= new instantiate_cache_type
;
2795 tree ret
= instantiate_scev_r (block_before_loop (loop
), loop
, NULL
,
2800 delete global_cache
;
2801 global_cache
= NULL
;
2807 /* Entry point for the analysis of the number of iterations pass.
2808 This function tries to safely approximate the number of iterations
2809 the loop will run. When this property is not decidable at compile
2810 time, the result is chrec_dont_know. Otherwise the result is a
2811 scalar or a symbolic parameter. When the number of iterations may
2812 be equal to zero and the property cannot be determined at compile
2813 time, the result is a COND_EXPR that represents in a symbolic form
2814 the conditions under which the number of iterations is not zero.
2816 Example of analysis: suppose that the loop has an exit condition:
2818 "if (b > 49) goto end_loop;"
2820 and that in a previous analysis we have determined that the
2821 variable 'b' has an evolution function:
2823 "EF = {23, +, 5}_2".
2825 When we evaluate the function at the point 5, i.e. the value of the
2826 variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2827 and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2828 the loop body has been executed 6 times. */
2831 number_of_latch_executions (struct loop
*loop
)
2834 struct tree_niter_desc niter_desc
;
2838 /* Determine whether the number of iterations in loop has already
2840 res
= loop
->nb_iterations
;
2844 may_be_zero
= NULL_TREE
;
2846 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2847 fprintf (dump_file
, "(number_of_iterations_in_loop = \n");
2849 res
= chrec_dont_know
;
2850 exit
= single_exit (loop
);
2852 if (exit
&& number_of_iterations_exit (loop
, exit
, &niter_desc
, false))
2854 may_be_zero
= niter_desc
.may_be_zero
;
2855 res
= niter_desc
.niter
;
2858 if (res
== chrec_dont_know
2860 || integer_zerop (may_be_zero
))
2862 else if (integer_nonzerop (may_be_zero
))
2863 res
= build_int_cst (TREE_TYPE (res
), 0);
2865 else if (COMPARISON_CLASS_P (may_be_zero
))
2866 res
= fold_build3 (COND_EXPR
, TREE_TYPE (res
), may_be_zero
,
2867 build_int_cst (TREE_TYPE (res
), 0), res
);
2869 res
= chrec_dont_know
;
2871 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2873 fprintf (dump_file
, " (set_nb_iterations_in_loop = ");
2874 print_generic_expr (dump_file
, res
, 0);
2875 fprintf (dump_file
, "))\n");
2878 loop
->nb_iterations
= res
;
2882 /* Returns the number of executions of the exit condition of LOOP,
2883 i.e., the number by one higher than number_of_latch_executions.
2884 Note that unlike number_of_latch_executions, this number does
2885 not necessarily fit in the unsigned variant of the type of
2886 the control variable -- if the number of iterations is a constant,
2887 we return chrec_dont_know if adding one to number_of_latch_executions
2888 overflows; however, in case the number of iterations is symbolic
2889 expression, the caller is responsible for dealing with this
2890 the possible overflow. */
2893 number_of_exit_cond_executions (struct loop
*loop
)
2895 tree ret
= number_of_latch_executions (loop
);
2896 tree type
= chrec_type (ret
);
2898 if (chrec_contains_undetermined (ret
))
2901 ret
= chrec_fold_plus (type
, ret
, build_int_cst (type
, 1));
2902 if (TREE_CODE (ret
) == INTEGER_CST
2903 && TREE_OVERFLOW (ret
))
2904 return chrec_dont_know
;
2911 /* Counters for the stats. */
2917 unsigned nb_affine_multivar
;
2918 unsigned nb_higher_poly
;
2919 unsigned nb_chrec_dont_know
;
2920 unsigned nb_undetermined
;
2923 /* Reset the counters. */
2926 reset_chrecs_counters (struct chrec_stats
*stats
)
2928 stats
->nb_chrecs
= 0;
2929 stats
->nb_affine
= 0;
2930 stats
->nb_affine_multivar
= 0;
2931 stats
->nb_higher_poly
= 0;
2932 stats
->nb_chrec_dont_know
= 0;
2933 stats
->nb_undetermined
= 0;
2936 /* Dump the contents of a CHREC_STATS structure. */
2939 dump_chrecs_stats (FILE *file
, struct chrec_stats
*stats
)
2941 fprintf (file
, "\n(\n");
2942 fprintf (file
, "-----------------------------------------\n");
2943 fprintf (file
, "%d\taffine univariate chrecs\n", stats
->nb_affine
);
2944 fprintf (file
, "%d\taffine multivariate chrecs\n", stats
->nb_affine_multivar
);
2945 fprintf (file
, "%d\tdegree greater than 2 polynomials\n",
2946 stats
->nb_higher_poly
);
2947 fprintf (file
, "%d\tchrec_dont_know chrecs\n", stats
->nb_chrec_dont_know
);
2948 fprintf (file
, "-----------------------------------------\n");
2949 fprintf (file
, "%d\ttotal chrecs\n", stats
->nb_chrecs
);
2950 fprintf (file
, "%d\twith undetermined coefficients\n",
2951 stats
->nb_undetermined
);
2952 fprintf (file
, "-----------------------------------------\n");
2953 fprintf (file
, "%d\tchrecs in the scev database\n",
2954 (int) htab_elements (scalar_evolution_info
));
2955 fprintf (file
, "%d\tsets in the scev database\n", nb_set_scev
);
2956 fprintf (file
, "%d\tgets in the scev database\n", nb_get_scev
);
2957 fprintf (file
, "-----------------------------------------\n");
2958 fprintf (file
, ")\n\n");
2961 /* Gather statistics about CHREC. */
2964 gather_chrec_stats (tree chrec
, struct chrec_stats
*stats
)
2966 if (dump_file
&& (dump_flags
& TDF_STATS
))
2968 fprintf (dump_file
, "(classify_chrec ");
2969 print_generic_expr (dump_file
, chrec
, 0);
2970 fprintf (dump_file
, "\n");
2975 if (chrec
== NULL_TREE
)
2977 stats
->nb_undetermined
++;
2981 switch (TREE_CODE (chrec
))
2983 case POLYNOMIAL_CHREC
:
2984 if (evolution_function_is_affine_p (chrec
))
2986 if (dump_file
&& (dump_flags
& TDF_STATS
))
2987 fprintf (dump_file
, " affine_univariate\n");
2990 else if (evolution_function_is_affine_multivariate_p (chrec
, 0))
2992 if (dump_file
&& (dump_flags
& TDF_STATS
))
2993 fprintf (dump_file
, " affine_multivariate\n");
2994 stats
->nb_affine_multivar
++;
2998 if (dump_file
&& (dump_flags
& TDF_STATS
))
2999 fprintf (dump_file
, " higher_degree_polynomial\n");
3000 stats
->nb_higher_poly
++;
3009 if (chrec_contains_undetermined (chrec
))
3011 if (dump_file
&& (dump_flags
& TDF_STATS
))
3012 fprintf (dump_file
, " undetermined\n");
3013 stats
->nb_undetermined
++;
3016 if (dump_file
&& (dump_flags
& TDF_STATS
))
3017 fprintf (dump_file
, ")\n");
3020 /* Callback for htab_traverse, gathers information on chrecs in the
3024 gather_stats_on_scev_database_1 (void **slot
, void *stats
)
3026 struct scev_info_str
*entry
= (struct scev_info_str
*) *slot
;
3028 gather_chrec_stats (entry
->chrec
, (struct chrec_stats
*) stats
);
3033 /* Classify the chrecs of the whole database. */
3036 gather_stats_on_scev_database (void)
3038 struct chrec_stats stats
;
3043 reset_chrecs_counters (&stats
);
3045 htab_traverse (scalar_evolution_info
, gather_stats_on_scev_database_1
,
3048 dump_chrecs_stats (dump_file
, &stats
);
3056 initialize_scalar_evolutions_analyzer (void)
3058 /* The elements below are unique. */
3059 if (chrec_dont_know
== NULL_TREE
)
3061 chrec_not_analyzed_yet
= NULL_TREE
;
3062 chrec_dont_know
= make_node (SCEV_NOT_KNOWN
);
3063 chrec_known
= make_node (SCEV_KNOWN
);
3064 TREE_TYPE (chrec_dont_know
) = void_type_node
;
3065 TREE_TYPE (chrec_known
) = void_type_node
;
3069 /* Initialize the analysis of scalar evolutions for LOOPS. */
3072 scev_initialize (void)
3078 scalar_evolution_info
= htab_create_ggc (100, hash_scev_info
, eq_scev_info
,
3081 initialize_scalar_evolutions_analyzer ();
3083 FOR_EACH_LOOP (li
, loop
, 0)
3085 loop
->nb_iterations
= NULL_TREE
;
3089 /* Return true if SCEV is initialized. */
3092 scev_initialized_p (void)
3094 return scalar_evolution_info
!= NULL
;
3097 /* Cleans up the information cached by the scalar evolutions analysis
3098 in the hash table. */
3101 scev_reset_htab (void)
3103 if (!scalar_evolution_info
)
3106 htab_empty (scalar_evolution_info
);
3109 /* Cleans up the information cached by the scalar evolutions analysis
3110 in the hash table and in the loop->nb_iterations. */
3123 FOR_EACH_LOOP (li
, loop
, 0)
3125 loop
->nb_iterations
= NULL_TREE
;
3129 /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
3130 respect to WRTO_LOOP and returns its base and step in IV if possible
3131 (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
3132 and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be
3133 invariant in LOOP. Otherwise we require it to be an integer constant.
3135 IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
3136 because it is computed in signed arithmetics). Consequently, adding an
3139 for (i = IV->base; ; i += IV->step)
3141 is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
3142 false for the type of the induction variable, or you can prove that i does
3143 not wrap by some other argument. Otherwise, this might introduce undefined
3146 for (i = iv->base; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
3148 must be used instead. */
3151 simple_iv (struct loop
*wrto_loop
, struct loop
*use_loop
, tree op
,
3152 affine_iv
*iv
, bool allow_nonconstant_step
)
3157 iv
->base
= NULL_TREE
;
3158 iv
->step
= NULL_TREE
;
3159 iv
->no_overflow
= false;
3161 type
= TREE_TYPE (op
);
3162 if (!POINTER_TYPE_P (type
)
3163 && !INTEGRAL_TYPE_P (type
))
3166 ev
= analyze_scalar_evolution_in_loop (wrto_loop
, use_loop
, op
,
3168 if (chrec_contains_undetermined (ev
)
3169 || chrec_contains_symbols_defined_in_loop (ev
, wrto_loop
->num
))
3172 if (tree_does_not_contain_chrecs (ev
))
3175 iv
->step
= build_int_cst (TREE_TYPE (ev
), 0);
3176 iv
->no_overflow
= true;
3180 if (TREE_CODE (ev
) != POLYNOMIAL_CHREC
3181 || CHREC_VARIABLE (ev
) != (unsigned) wrto_loop
->num
)
3184 iv
->step
= CHREC_RIGHT (ev
);
3185 if ((!allow_nonconstant_step
&& TREE_CODE (iv
->step
) != INTEGER_CST
)
3186 || tree_contains_chrecs (iv
->step
, NULL
))
3189 iv
->base
= CHREC_LEFT (ev
);
3190 if (tree_contains_chrecs (iv
->base
, NULL
))
3193 iv
->no_overflow
= !folded_casts
&& TYPE_OVERFLOW_UNDEFINED (type
);
3198 /* Finalize the scalar evolution analysis. */
3201 scev_finalize (void)
3203 if (!scalar_evolution_info
)
3205 htab_delete (scalar_evolution_info
);
3206 scalar_evolution_info
= NULL
;
3209 /* Returns true if the expression EXPR is considered to be too expensive
3210 for scev_const_prop. */
3213 expression_expensive_p (tree expr
)
3215 enum tree_code code
;
3217 if (is_gimple_val (expr
))
3220 code
= TREE_CODE (expr
);
3221 if (code
== TRUNC_DIV_EXPR
3222 || code
== CEIL_DIV_EXPR
3223 || code
== FLOOR_DIV_EXPR
3224 || code
== ROUND_DIV_EXPR
3225 || code
== TRUNC_MOD_EXPR
3226 || code
== CEIL_MOD_EXPR
3227 || code
== FLOOR_MOD_EXPR
3228 || code
== ROUND_MOD_EXPR
3229 || code
== EXACT_DIV_EXPR
)
3231 /* Division by power of two is usually cheap, so we allow it.
3232 Forbid anything else. */
3233 if (!integer_pow2p (TREE_OPERAND (expr
, 1)))
3237 switch (TREE_CODE_CLASS (code
))
3240 case tcc_comparison
:
3241 if (expression_expensive_p (TREE_OPERAND (expr
, 1)))
3246 return expression_expensive_p (TREE_OPERAND (expr
, 0));
3253 /* Replace ssa names for that scev can prove they are constant by the
3254 appropriate constants. Also perform final value replacement in loops,
3255 in case the replacement expressions are cheap.
3257 We only consider SSA names defined by phi nodes; rest is left to the
3258 ordinary constant propagation pass. */
3261 scev_const_prop (void)
3264 tree name
, type
, ev
;
3266 struct loop
*loop
, *ex_loop
;
3267 bitmap ssa_names_to_remove
= NULL
;
3270 gimple_stmt_iterator psi
;
3272 if (number_of_loops (cfun
) <= 1)
3277 loop
= bb
->loop_father
;
3279 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
3281 phi
= gsi_stmt (psi
);
3282 name
= PHI_RESULT (phi
);
3284 if (virtual_operand_p (name
))
3287 type
= TREE_TYPE (name
);
3289 if (!POINTER_TYPE_P (type
)
3290 && !INTEGRAL_TYPE_P (type
))
3293 ev
= resolve_mixers (loop
, analyze_scalar_evolution (loop
, name
));
3294 if (!is_gimple_min_invariant (ev
)
3295 || !may_propagate_copy (name
, ev
))
3298 /* Replace the uses of the name. */
3300 replace_uses_by (name
, ev
);
3302 if (!ssa_names_to_remove
)
3303 ssa_names_to_remove
= BITMAP_ALLOC (NULL
);
3304 bitmap_set_bit (ssa_names_to_remove
, SSA_NAME_VERSION (name
));
3308 /* Remove the ssa names that were replaced by constants. We do not
3309 remove them directly in the previous cycle, since this
3310 invalidates scev cache. */
3311 if (ssa_names_to_remove
)
3315 EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove
, 0, i
, bi
)
3317 gimple_stmt_iterator psi
;
3318 name
= ssa_name (i
);
3319 phi
= SSA_NAME_DEF_STMT (name
);
3321 gcc_assert (gimple_code (phi
) == GIMPLE_PHI
);
3322 psi
= gsi_for_stmt (phi
);
3323 remove_phi_node (&psi
, true);
3326 BITMAP_FREE (ssa_names_to_remove
);
3330 /* Now the regular final value replacement. */
3331 FOR_EACH_LOOP (li
, loop
, LI_FROM_INNERMOST
)
3334 tree def
, rslt
, niter
;
3335 gimple_stmt_iterator bsi
;
3337 /* If we do not know exact number of iterations of the loop, we cannot
3338 replace the final value. */
3339 exit
= single_exit (loop
);
3343 niter
= number_of_latch_executions (loop
);
3344 if (niter
== chrec_dont_know
)
3347 /* Ensure that it is possible to insert new statements somewhere. */
3348 if (!single_pred_p (exit
->dest
))
3349 split_loop_exit_edge (exit
);
3350 bsi
= gsi_after_labels (exit
->dest
);
3352 ex_loop
= superloop_at_depth (loop
,
3353 loop_depth (exit
->dest
->loop_father
) + 1);
3355 for (psi
= gsi_start_phis (exit
->dest
); !gsi_end_p (psi
); )
3357 phi
= gsi_stmt (psi
);
3358 rslt
= PHI_RESULT (phi
);
3359 def
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
3360 if (virtual_operand_p (def
))
3366 if (!POINTER_TYPE_P (TREE_TYPE (def
))
3367 && !INTEGRAL_TYPE_P (TREE_TYPE (def
)))
3373 def
= analyze_scalar_evolution_in_loop (ex_loop
, loop
, def
, NULL
);
3374 def
= compute_overall_effect_of_inner_loop (ex_loop
, def
);
3375 if (!tree_does_not_contain_chrecs (def
)
3376 || chrec_contains_symbols_defined_in_loop (def
, ex_loop
->num
)
3377 /* Moving the computation from the loop may prolong life range
3378 of some ssa names, which may cause problems if they appear
3379 on abnormal edges. */
3380 || contains_abnormal_ssa_name_p (def
)
3381 /* Do not emit expensive expressions. The rationale is that
3382 when someone writes a code like
3384 while (n > 45) n -= 45;
3386 he probably knows that n is not large, and does not want it
3387 to be turned into n %= 45. */
3388 || expression_expensive_p (def
))
3390 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3392 fprintf (dump_file
, "not replacing:\n ");
3393 print_gimple_stmt (dump_file
, phi
, 0, 0);
3394 fprintf (dump_file
, "\n");
3400 /* Eliminate the PHI node and replace it by a computation outside
3404 fprintf (dump_file
, "\nfinal value replacement:\n ");
3405 print_gimple_stmt (dump_file
, phi
, 0, 0);
3406 fprintf (dump_file
, " with\n ");
3408 def
= unshare_expr (def
);
3409 remove_phi_node (&psi
, false);
3411 def
= force_gimple_operand_gsi (&bsi
, def
, false, NULL_TREE
,
3412 true, GSI_SAME_STMT
);
3413 ass
= gimple_build_assign (rslt
, def
);
3414 gsi_insert_before (&bsi
, ass
, GSI_SAME_STMT
);
3417 print_gimple_stmt (dump_file
, ass
, 0, 0);
3418 fprintf (dump_file
, "\n");
3425 #include "gt-tree-scalar-evolution.h"